2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * hptproc.c sysctl support
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/sysctl.h>
38 #include <machine/stdarg.h>
44 #include <dev/hptmv/global.h>
45 #include <dev/hptmv/hptintf.h>
46 #include <dev/hptmv/osbsd.h>
47 #include <dev/hptmv/access601.h>
49 int hpt_rescan_all(void);
51 /***************************************************************************/
53 static char hptproc_buffer[256];
54 extern char DRIVER_VERSION[];
56 typedef struct sysctl_req HPT_GET_INFO;
59 hpt_set_asc_info(IAL_ADAPTER_T *pAdapter, char *buffer,int length)
61 int orig_length = length+4;
62 PVBus _vbus_p = &pAdapter->VBus;
64 PVDevice pSubArray, pVDev;
65 UINT i, iarray, ichan;
66 struct cam_periph *periph = NULL;
68 mtx_lock(&pAdapter->lock);
70 if (length>=8 && strncmp(buffer, "rebuild ", 8)==0)
74 if (length>=5 && strncmp(buffer, "start", 5)==0)
76 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
77 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
80 if (pArray->u.array.rf_need_rebuild && !pArray->u.array.rf_rebuilding)
81 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray,
82 (UCHAR)((pArray->u.array.CriticalMembers || pArray->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
84 mtx_unlock(&pAdapter->lock);
87 else if (length>=4 && strncmp(buffer, "stop", 4)==0)
89 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
90 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
93 if (pArray->u.array.rf_rebuilding)
94 pArray->u.array.rf_abort_rebuild = 1;
96 mtx_unlock(&pAdapter->lock);
99 else if (length>=3 && buffer[1]==','&& buffer[0]>='1'&& buffer[2]>='1')
101 iarray = buffer[0]-'1';
102 ichan = buffer[2]-'1';
104 if(iarray >= MAX_VDEVICE_PER_VBUS || ichan >= MV_SATA_CHANNELS_NUM) return -EINVAL;
106 pArray = _vbus_p->pVDevice[iarray];
107 if (!pArray || (pArray->vf_online == 0)) {
108 mtx_unlock(&pAdapter->lock);
112 for (i=0;i<MV_SATA_CHANNELS_NUM;i++)
116 mtx_unlock(&pAdapter->lock);
120 pVDev = &pAdapter->VDevices[ichan];
121 if(!pVDev->u.disk.df_on_line || pVDev->pParent) {
122 mtx_unlock(&pAdapter->lock);
126 /* Not allow to use a mounted disk ??? test*/
127 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++)
128 if(pVDev == _vbus_p->pVDevice[i])
130 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId,i);
131 if (periph != NULL && periph->refcount >= 1)
133 hpt_printk(("Can not use disk used by OS!\n"));
134 mtx_unlock(&pAdapter->lock);
137 /* the Mounted Disk isn't delete */
140 switch(pArray->VDeviceType)
147 if(hpt_add_disk_to_array(_VBUS_P VDEV_TO_ID(pSubArray), VDEV_TO_ID(pVDev)) == -1) {
148 mtx_unlock(&pAdapter->lock);
151 pSubArray->u.array.rf_auto_rebuild = 0;
152 pSubArray->u.array.rf_abort_rebuild = 0;
153 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pSubArray, DUPLICATE);
157 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
158 if(pArray->u.array.pMember[i] && mIsArray(pArray->u.array.pMember[i]) &&
159 (pArray->u.array.pMember[i]->u.array.rf_broken == 1))
161 pSubArray = pArray->u.array.pMember[i];
165 mtx_unlock(&pAdapter->lock);
168 mtx_unlock(&pAdapter->lock);
172 else if (length>=7 && strncmp(buffer, "verify ", 7)==0)
176 if (length>=6 && strncmp(buffer, "start ", 6)==0)
180 if (length>=1 && *buffer>='1')
182 iarray = *buffer-'1';
183 if(iarray >= MAX_VDEVICE_PER_VBUS) {
184 mtx_unlock(&pAdapter->lock);
188 pArray = _vbus_p->pVDevice[iarray];
189 if (!pArray || (pArray->vf_online == 0)) {
190 mtx_unlock(&pAdapter->lock);
194 if(pArray->VDeviceType != VD_RAID_1 && pArray->VDeviceType != VD_RAID_5) {
195 mtx_unlock(&pAdapter->lock);
199 if (!(pArray->u.array.rf_need_rebuild ||
200 pArray->u.array.rf_rebuilding ||
201 pArray->u.array.rf_verifying ||
202 pArray->u.array.rf_initializing))
204 pArray->u.array.RebuildSectors = 0;
205 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, VERIFY);
207 mtx_unlock(&pAdapter->lock);
211 else if (length>=5 && strncmp(buffer, "stop ", 5)==0)
215 if (length>=1 && *buffer>='1')
217 iarray = *buffer-'1';
218 if(iarray >= MAX_VDEVICE_PER_VBUS) {
219 mtx_unlock(&pAdapter->lock);
223 pArray = _vbus_p->pVDevice[iarray];
224 if (!pArray || (pArray->vf_online == 0)) {
225 mtx_unlock(&pAdapter->lock);
228 if(pArray->u.array.rf_verifying)
230 pArray->u.array.rf_abort_rebuild = 1;
232 mtx_unlock(&pAdapter->lock);
239 if (length>=10 && strncmp(buffer, "writeback ", 10)==0) {
242 if (length>=1 && *buffer>='0' && *buffer<='1') {
243 _vbus_(r5.enable_write_back) = *buffer-'0';
244 if (_vbus_(r5.enable_write_back))
245 hpt_printk(("RAID5 write back enabled"));
246 mtx_unlock(&pAdapter->lock);
253 if (0) {} /* just to compile */
255 else if (length>=9 && strncmp(buffer, "dbglevel ", 9)==0) {
258 if (length>=1 && *buffer>='0' && *buffer<='3') {
259 hpt_dbg_level = *buffer-'0';
260 mtx_unlock(&pAdapter->lock);
264 else if (length>=8 && strncmp(buffer, "disable ", 8)==0) {
268 mtx_unlock(&pAdapter->lock);
274 * Since we have only one sysctl node, add adapter ID in the command
275 * line string: e.g. "hpt 0 rebuild start"
278 hpt_set_info(int length)
286 PHPT_IOCTL_PARAM piop;
288 char *buffer = hptproc_buffer;
290 if (strncmp(buffer,"hpt ",4) == 0) {
291 IAL_ADAPTER_T *pAdapter;
292 retval = buffer[4]-'0';
293 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) {
294 if (pAdapter->mvSataAdapter.adapterId==retval)
295 return (retval = hpt_set_asc_info(pAdapter, buffer+6, length-6)) >= 0? retval : -EINVAL;
300 piop = (PHPT_IOCTL_PARAM)buffer;
301 if (piop->Magic == HPT_IOCTL_MAGIC ||
302 piop->Magic == HPT_IOCTL_MAGIC32) {
303 KdPrintE(("ioctl=%d in=%p len=%d out=%p len=%d\n",
304 piop->dwIoControlCode,
308 piop->nOutBufferSize));
311 * map buffer to kernel.
313 if (piop->nInBufferSize > PAGE_SIZE ||
314 piop->nOutBufferSize > PAGE_SIZE ||
315 piop->nInBufferSize+piop->nOutBufferSize > PAGE_SIZE) {
316 KdPrintE(("User buffer too large\n"));
320 ke_area = malloc(piop->nInBufferSize+piop->nOutBufferSize, M_DEVBUF, M_NOWAIT);
321 if (ke_area == NULL) {
322 KdPrintE(("Couldn't allocate kernel mem.\n"));
326 if (piop->nInBufferSize) {
327 if (copyin((void*)(ULONG_PTR)piop->lpInBuffer, ke_area, piop->nInBufferSize) != 0) {
328 KdPrintE(("Failed to copyin from lpInBuffer\n"));
329 free(ke_area, M_DEVBUF);
335 * call kernel handler.
337 err = Kernel_DeviceIoControl(&gIal_Adapter->VBus,
338 piop->dwIoControlCode, ke_area, piop->nInBufferSize,
339 ke_area + piop->nInBufferSize, piop->nOutBufferSize, &dwRet);
342 if (piop->nOutBufferSize)
343 copyout(ke_area + piop->nInBufferSize, (void*)(ULONG_PTR)piop->lpOutBuffer, piop->nOutBufferSize);
345 if (piop->lpBytesReturned)
346 copyout(&dwRet, (void*)(ULONG_PTR)piop->lpBytesReturned, sizeof(DWORD));
348 free(ke_area, M_DEVBUF);
351 else KdPrintW(("Kernel_ioctl(): return %d\n", err));
353 free(ke_area, M_DEVBUF);
356 KdPrintW(("Wrong signature: %x\n", piop->Magic));
365 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
368 get_disk_name(char *name, PDevice pDev)
371 MV_SATA_CHANNEL *pMvSataChannel = pDev->mv;
372 IDENTIFY_DATA2 *pIdentifyData = (IDENTIFY_DATA2 *)pMvSataChannel->identifyDevice;
374 for (i = 0; i < 10; i++)
375 ((WORD*)name)[i] = shortswap(pIdentifyData->ModelNumber[i]);
380 hpt_copy_info(HPT_GET_INFO *pinfo, char *fmt, ...)
387 return (SYSCTL_OUT(pinfo, hptproc_buffer, 1));
392 printfretval = vsnprintf(hptproc_buffer, sizeof(hptproc_buffer), fmt, ap);
394 return(SYSCTL_OUT(pinfo, hptproc_buffer, strlen(hptproc_buffer)));
399 hpt_copy_disk_info(HPT_GET_INFO *pinfo, PVDevice pVDev, UINT iChan)
401 char name[32], arrayname[16], *status;
403 get_disk_name(name, &pVDev->u.disk);
405 if (!pVDev->u.disk.df_on_line)
407 else if (pVDev->VDeviceType==VD_SPARE)
414 memcpy(arrayname, pVDev->pParent->u.array.ArrayName, MAX_ARRAY_NAME);
415 if (pVDev->pParent->u.array.CriticalMembers & (1<<pVDev->bSerialNumber))
422 hpt_copy_info(pinfo, "Channel %d %s %5dMB %s %s\n",
424 name, pVDev->VDeviceCapacity>>11, status, arrayname);
429 hpt_copy_array_info(HPT_GET_INFO *pinfo, int nld, PVDevice pArray)
432 char *sType = NULL, *sStatus = NULL;
436 switch (pArray->VDeviceType) {
438 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
439 if(pArray->u.array.pMember[i]) {
440 if(mIsArray(pArray->u.array.pMember[i]))
466 if (pArray->vf_online == 0)
467 sStatus = "Disabled";
468 else if (pArray->u.array.rf_broken)
469 sStatus = "Critical";
470 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
474 if(mIsArray(pArray->u.array.pMember[i]))
475 pTmpArray = pArray->u.array.pMember[i];
479 if (pTmpArray->u.array.rf_rebuilding) {
481 sprintf(buf, "Rebuilding %lldMB", (pTmpArray->u.array.RebuildSectors>>11));
483 sprintf(buf, "Rebuilding %d%%", (UINT)((pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11)));
487 else if (pTmpArray->u.array.rf_verifying) {
488 sprintf(buf, "Verifying %d%%", (UINT)((pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11)));
491 else if (pTmpArray->u.array.rf_need_rebuild)
492 sStatus = "Critical";
493 else if (pTmpArray->u.array.rf_broken)
494 sStatus = "Critical";
496 if(pTmpArray == pArray) goto out;
502 if (!sStatus) sStatus = "Normal";
503 hpt_copy_info(pinfo, "%2d %11s %-20s %5lldMB %-16s", nld, sType, pArray->u.array.ArrayName, pArray->VDeviceCapacity>>11, sStatus);
508 hpt_get_info(IAL_ADAPTER_T *pAdapter, HPT_GET_INFO *pinfo)
510 PVBus _vbus_p = &pAdapter->VBus;
511 struct cam_periph *periph = NULL;
516 mtx_lock(&pAdapter->lock);
517 if (pAdapter->beeping) {
518 pAdapter->beeping = 0;
519 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
521 mtx_unlock(&pAdapter->lock);
524 hpt_copy_info(pinfo, "Controller #%d:\n\n", pAdapter->mvSataAdapter.adapterId);
526 hpt_copy_info(pinfo, "Physical device list\n");
527 hpt_copy_info(pinfo, "Channel Model Capacity Status Array\n");
528 hpt_copy_info(pinfo, "-------------------------------------------------------------------\n");
530 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
532 pVDev = &(pAdapter->VDevices[channel]);
533 if(pVDev->u.disk.df_on_line)
534 hpt_copy_disk_info(pinfo, pVDev, channel);
537 hpt_copy_info(pinfo, "\nLogical device list\n");
538 hpt_copy_info(pinfo, "No. Type Name Capacity Status OsDisk\n");
539 hpt_copy_info(pinfo, "--------------------------------------------------------------------------\n");
542 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++){
543 pVDev = _vbus_p->pVDevice[i];
550 hpt_copy_array_info(pinfo, j, pVDev);
556 /* it may be add to an array after driver loaded, check it */
559 /* in this case, pVDev can only be a RAID 1 source disk. */
560 if (pVDev->pParent->VDeviceType==VD_RAID_1 && pVDev==pVDev->pParent->u.array.pMember[0])
563 get_disk_name(name, &pVDev->u.disk);
565 hpt_copy_info(pinfo, "%2d %s %s %5dMB %-16s",
566 j, "Single disk", name, pVDev->VDeviceCapacity>>11,
567 /* gmm 2001-6-19: Check if pDev has been added to an array. */
568 ((pVDev->pParent) ? "Unavailable" : "Normal"));
570 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId, i);
572 hpt_copy_info(pinfo," %s\n","not registered");
574 hpt_copy_info(pinfo," %s%d\n", periph->periph_name, periph->unit_number);
581 hpt_proc_in(SYSCTL_HANDLER_ARGS, int *len)
586 if ((req->newlen - req->newidx) >= sizeof(hptproc_buffer)) {
589 i = (req->newlen - req->newidx);
590 error = SYSCTL_IN(req, hptproc_buffer, i);
593 (hptproc_buffer)[i] = '\0';
599 hpt_status(SYSCTL_HANDLER_ARGS)
601 int length, error=0, retval=0;
602 IAL_ADAPTER_T *pAdapter;
604 error = hpt_proc_in(oidp, arg1, arg2, req, &length);
606 if (req->newptr != NULL)
608 if (error || length == 0)
610 KdPrint(("error!\n"));
615 if (hpt_set_info(length) >= 0)
622 hpt_copy_info(req, "%s Version %s\n", DRIVER_NAME, DRIVER_VERSION);
623 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) {
624 if (hpt_get_info(pAdapter, req) < 0) {
630 hpt_copy_info(req, NULL);
638 #define xhptregister_node(name) hptregister_node(name)
640 #if __FreeBSD_version >= 1100024
641 #define hptregister_node(name) \
642 SYSCTL_ROOT_NODE(OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node"); \
643 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
644 NULL, 0, hpt_status, "A", "Get/Set " #name " state")
646 #define hptregister_node(name) \
647 SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node"); \
648 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
649 NULL, 0, hpt_status, "A", "Get/Set " #name " state")
652 xhptregister_node(PROC_DIR_NAME);