2 * Copyright (c) 2004-2005 HighPoint Technologies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * hptproc.c sysctl support
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/sysctl.h>
36 #include <machine/stdarg.h>
42 #include <dev/hptmv/global.h>
43 #include <dev/hptmv/hptintf.h>
44 #include <dev/hptmv/osbsd.h>
45 #include <dev/hptmv/access601.h>
47 int hpt_rescan_all(void);
49 /***************************************************************************/
51 static char hptproc_buffer[256];
52 extern char DRIVER_VERSION[];
54 #define FORMAL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1, int arg2, \
55 struct sysctl_req *req
56 #define REAL_HANDLER_ARGS oidp, arg1, arg2, req
57 typedef struct sysctl_req HPT_GET_INFO;
60 hpt_set_asc_info(IAL_ADAPTER_T *pAdapter, char *buffer,int length)
62 int orig_length = length+4;
63 PVBus _vbus_p = &pAdapter->VBus;
65 PVDevice pSubArray, pVDev;
66 UINT i, iarray, ichan;
67 struct cam_periph *periph = NULL;
71 if (length>=8 && strncmp(buffer, "rebuild ", 8)==0)
75 if (length>=5 && strncmp(buffer, "start", 5)==0)
77 oldspl = lock_driver();
78 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
79 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
82 if (pArray->u.array.rf_need_rebuild && !pArray->u.array.rf_rebuilding)
83 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray,
84 (UCHAR)((pArray->u.array.CriticalMembers || pArray->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY));
86 unlock_driver(oldspl);
89 else if (length>=4 && strncmp(buffer, "stop", 4)==0)
91 oldspl = lock_driver();
92 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++)
93 if ((pArray=ArrayTables(i))->u.array.dArStamp==0)
96 if (pArray->u.array.rf_rebuilding)
97 pArray->u.array.rf_abort_rebuild = 1;
99 unlock_driver(oldspl);
102 else if (length>=3 && buffer[1]==','&& buffer[0]>='1'&& buffer[2]>='1')
104 iarray = buffer[0]-'1';
105 ichan = buffer[2]-'1';
107 if(iarray >= MAX_VDEVICE_PER_VBUS || ichan >= MV_SATA_CHANNELS_NUM) return -EINVAL;
109 pArray = _vbus_p->pVDevice[iarray];
110 if (!pArray || (pArray->vf_online == 0)) return -EINVAL;
112 for (i=0;i<MV_SATA_CHANNELS_NUM;i++)
119 pVDev = &pAdapter->VDevices[ichan];
120 if(!pVDev->u.disk.df_on_line || pVDev->pParent) return -EINVAL;
122 /* Not allow to use a mounted disk ??? test*/
123 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++)
124 if(pVDev == _vbus_p->pVDevice[i])
126 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId,i);
127 if (periph != NULL && periph->refcount >= 1)
129 hpt_printk(("Can not use disk used by OS!\n"));
132 /* the Mounted Disk isn't delete */
135 switch(pArray->VDeviceType)
142 oldspl = lock_driver();
143 if(hpt_add_disk_to_array(_VBUS_P VDEV_TO_ID(pSubArray), VDEV_TO_ID(pVDev)) == -1) {
144 unlock_driver(oldspl);
147 pSubArray->u.array.rf_auto_rebuild = 0;
148 pSubArray->u.array.rf_abort_rebuild = 0;
149 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pSubArray, DUPLICATE);
150 unlock_driver(oldspl);
154 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
155 if(pArray->u.array.pMember[i] && mIsArray(pArray->u.array.pMember[i]) &&
156 (pArray->u.array.pMember[i]->u.array.rf_broken == 1))
158 pSubArray = pArray->u.array.pMember[i];
167 else if (length>=7 && strncmp(buffer, "verify ", 7)==0)
171 if (length>=6 && strncmp(buffer, "start ", 6)==0)
175 if (length>=1 && *buffer>='1')
177 iarray = *buffer-'1';
178 if(iarray >= MAX_VDEVICE_PER_VBUS) return -EINVAL;
180 pArray = _vbus_p->pVDevice[iarray];
181 if (!pArray || (pArray->vf_online == 0)) return -EINVAL;
183 if(pArray->VDeviceType != VD_RAID_1 && pArray->VDeviceType != VD_RAID_5)
186 if (!(pArray->u.array.rf_need_rebuild ||
187 pArray->u.array.rf_rebuilding ||
188 pArray->u.array.rf_verifying ||
189 pArray->u.array.rf_initializing))
191 oldspl = lock_driver();
192 pArray->u.array.RebuildSectors = 0;
193 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, VERIFY);
194 unlock_driver(oldspl);
199 else if (length>=5 && strncmp(buffer, "stop ", 5)==0)
203 if (length>=1 && *buffer>='1')
205 iarray = *buffer-'1';
206 if(iarray >= MAX_VDEVICE_PER_VBUS) return -EINVAL;
208 pArray = _vbus_p->pVDevice[iarray];
209 if (!pArray || (pArray->vf_online == 0)) return -EINVAL;
210 if(pArray->u.array.rf_verifying)
212 oldspl = lock_driver();
213 pArray->u.array.rf_abort_rebuild = 1;
214 unlock_driver(oldspl);
222 if (length>=10 && strncmp(buffer, "writeback ", 10)==0) {
225 if (length>=1 && *buffer>='0' && *buffer<='1') {
226 _vbus_(r5.enable_write_back) = *buffer-'0';
227 if (_vbus_(r5.enable_write_back))
228 hpt_printk(("RAID5 write back enabled"));
235 if (0) {} /* just to compile */
237 else if (length>=9 && strncmp(buffer, "dbglevel ", 9)==0) {
240 if (length>=1 && *buffer>='0' && *buffer<='3') {
241 hpt_dbg_level = *buffer-'0';
245 else if (length>=8 && strncmp(buffer, "disable ", 8)==0) {
254 * Since we have only one sysctl node, add adapter ID in the command
255 * line string: e.g. "hpt 0 rebuild start"
258 hpt_set_info(int length)
266 PHPT_IOCTL_PARAM piop;
268 char *buffer = hptproc_buffer;
270 if (strncmp(buffer,"hpt ",4) == 0) {
271 IAL_ADAPTER_T *pAdapter;
272 retval = buffer[4]-'0';
273 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) {
274 if (pAdapter->mvSataAdapter.adapterId==retval)
275 return (retval = hpt_set_asc_info(pAdapter, buffer+6, length-6)) >= 0? retval : -EINVAL;
280 piop = (PHPT_IOCTL_PARAM)buffer;
281 if (piop->Magic == HPT_IOCTL_MAGIC) {
282 KdPrintE(("ioctl=%d in=%p len=%d out=%p len=%d\n",
283 piop->dwIoControlCode,
287 piop->nOutBufferSize));
290 * map buffer to kernel.
292 if (piop->nInBufferSize+piop->nOutBufferSize > PAGE_SIZE) {
293 KdPrintE(("User buffer too large\n"));
297 ke_area = malloc(piop->nInBufferSize+piop->nOutBufferSize, M_DEVBUF, M_NOWAIT);
298 if (ke_area == NULL) {
299 KdPrintE(("Couldn't allocate kernel mem.\n"));
303 if (piop->nInBufferSize)
304 copyin((void*)(ULONG_PTR)piop->lpInBuffer, ke_area, piop->nInBufferSize);
307 * call kernel handler.
309 err = Kernel_DeviceIoControl(&gIal_Adapter->VBus,
310 piop->dwIoControlCode, ke_area, piop->nInBufferSize,
311 ke_area + piop->nInBufferSize, piop->nOutBufferSize, &dwRet);
314 if (piop->nOutBufferSize)
315 copyout(ke_area + piop->nInBufferSize, (void*)(ULONG_PTR)piop->lpOutBuffer, piop->nOutBufferSize);
317 if (piop->lpBytesReturned)
318 copyout(&dwRet, (void*)(ULONG_PTR)piop->lpBytesReturned, sizeof(DWORD));
320 free(ke_area, M_DEVBUF);
323 else KdPrintW(("Kernel_ioctl(): return %d\n", err));
325 free(ke_area, M_DEVBUF);
328 KdPrintW(("Wrong signature: %x\n", piop->Magic));
337 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8))
340 get_disk_name(char *name, PDevice pDev)
343 MV_SATA_CHANNEL *pMvSataChannel = pDev->mv;
344 IDENTIFY_DATA2 *pIdentifyData = (IDENTIFY_DATA2 *)pMvSataChannel->identifyDevice;
346 for (i = 0; i < 10; i++)
347 ((WORD*)name)[i] = shortswap(pIdentifyData->ModelNumber[i]);
352 hpt_copy_info(HPT_GET_INFO *pinfo, char *fmt, ...)
359 return (SYSCTL_OUT(pinfo, hptproc_buffer, 1));
364 printfretval = vsnprintf(hptproc_buffer, sizeof(hptproc_buffer), fmt, ap);
366 return(SYSCTL_OUT(pinfo, hptproc_buffer, strlen(hptproc_buffer)));
371 hpt_copy_disk_info(HPT_GET_INFO *pinfo, PVDevice pVDev, UINT iChan)
373 char name[32], arrayname[16], *status;
375 get_disk_name(name, &pVDev->u.disk);
377 if (!pVDev->u.disk.df_on_line)
379 else if (pVDev->VDeviceType==VD_SPARE)
386 memcpy(arrayname, pVDev->pParent->u.array.ArrayName, MAX_ARRAY_NAME);
387 if (pVDev->pParent->u.array.CriticalMembers & (1<<pVDev->bSerialNumber))
394 hpt_copy_info(pinfo, "Channel %d %s %5dMB %s %s\n",
396 name, pVDev->VDeviceCapacity>>11, status, arrayname);
401 hpt_copy_array_info(HPT_GET_INFO *pinfo, int nld, PVDevice pArray)
404 char *sType=0, *sStatus=0;
408 switch (pArray->VDeviceType) {
410 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
411 if(pArray->u.array.pMember[i]) {
412 if(mIsArray(pArray->u.array.pMember[i]))
438 if (pArray->vf_online == 0)
439 sStatus = "Disabled";
440 else if (pArray->u.array.rf_broken)
441 sStatus = "Critical";
442 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++)
446 if(mIsArray(pArray->u.array.pMember[i]))
447 pTmpArray = pArray->u.array.pMember[i];
451 if (pTmpArray->u.array.rf_rebuilding) {
453 sprintf(buf, "Rebuilding %dMB", (pTmpArray->u.array.RebuildSectors>>11));
455 sprintf(buf, "Rebuilding %d%%", (pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11));
459 else if (pTmpArray->u.array.rf_verifying) {
460 sprintf(buf, "Verifying %d%%", (pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11));
463 else if (pTmpArray->u.array.rf_need_rebuild)
464 sStatus = "Critical";
465 else if (pTmpArray->u.array.rf_broken)
466 sStatus = "Critical";
468 if(pTmpArray == pArray) goto out;
474 if (!sStatus) sStatus = "Normal";
475 hpt_copy_info(pinfo, "%2d %11s %-20s %5dMB %-16s", nld, sType, pArray->u.array.ArrayName, pArray->VDeviceCapacity>>11, sStatus);
480 hpt_get_info(IAL_ADAPTER_T *pAdapter, HPT_GET_INFO *pinfo)
482 PVBus _vbus_p = &pAdapter->VBus;
483 struct cam_periph *periph = NULL;
488 if (pAdapter->beeping) {
489 intrmask_t oldspl = lock_driver();
490 pAdapter->beeping = 0;
491 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress);
492 unlock_driver(oldspl);
496 hpt_copy_info(pinfo, "Controller #%d:\n\n", pAdapter->mvSataAdapter.adapterId);
498 hpt_copy_info(pinfo, "Physical device list\n");
499 hpt_copy_info(pinfo, "Channel Model Capacity Status Array\n");
500 hpt_copy_info(pinfo, "-------------------------------------------------------------------\n");
502 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++)
504 pVDev = &(pAdapter->VDevices[channel]);
505 if(pVDev->u.disk.df_on_line)
506 hpt_copy_disk_info(pinfo, pVDev, channel);
509 hpt_copy_info(pinfo, "\nLogical device list\n");
510 hpt_copy_info(pinfo, "No. Type Name Capacity Status OsDisk\n");
511 hpt_copy_info(pinfo, "--------------------------------------------------------------------------\n");
514 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++){
515 pVDev = _vbus_p->pVDevice[i];
522 hpt_copy_array_info(pinfo, j, pVDev);
528 /* it may be add to an array after driver loaded, check it */
531 /* in this case, pVDev can only be a RAID 1 source disk. */
532 if (pVDev->pParent->VDeviceType==VD_RAID_1 && pVDev==pVDev->pParent->u.array.pMember[0])
535 get_disk_name(name, &pVDev->u.disk);
537 hpt_copy_info(pinfo, "%2d %s %s %5dMB %-16s",
538 j, "Single disk", name, pVDev->VDeviceCapacity>>11,
539 /* gmm 2001-6-19: Check if pDev has been added to an array. */
540 ((pVDev->pParent) ? "Unavailable" : "Normal"));
542 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId, i);
544 hpt_copy_info(pinfo," %s\n","not registered");
546 hpt_copy_info(pinfo," %s%d\n", periph->periph_name, periph->unit_number);
553 hpt_proc_in(FORMAL_HANDLER_ARGS, int *len)
558 if ((req->newlen - req->newidx) >= sizeof(hptproc_buffer)) {
561 i = (req->newlen - req->newidx);
562 error = SYSCTL_IN(req, hptproc_buffer, i);
565 (hptproc_buffer)[i] = '\0';
571 hpt_status(FORMAL_HANDLER_ARGS)
573 int length, error=0, retval=0;
574 IAL_ADAPTER_T *pAdapter;
576 error = hpt_proc_in(REAL_HANDLER_ARGS, &length);
578 if (req->newptr != NULL)
580 if (error || length == 0)
582 KdPrint(("error!\n"));
587 if (hpt_set_info(length) >= 0)
594 hpt_copy_info(req, "%s Version %s\n", DRIVER_NAME, DRIVER_VERSION);
595 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) {
596 if (hpt_get_info(pAdapter, req) < 0) {
602 hpt_copy_info(req, NULL);
610 #define xhptregister_node(name) hptregister_node(name)
612 #if (__FreeBSD_version < 500043)
613 #define hptregister_node(name) \
614 SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node") \
615 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
616 NULL, 0, hpt_status, "A", "Get/Set " #name " state")
618 #define hptregister_node(name) \
619 SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node"); \
620 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \
621 NULL, 0, hpt_status, "A", "Get/Set " #name " state");
624 xhptregister_node(PROC_DIR_NAME);