1 /* 2 * Copyright (c) 2003-2004 HighPoint Technologies, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 /* 29 * hptproc.c sysctl support 30 */ 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> 37 38 #include <dev/hptmv/global.h> 39 #include <dev/hptmv/hptintf.h> 40 #include <dev/hptmv/osbsd.h> 41 #include <dev/hptmv/access601.h> 42 43 int hpt_rescan_all(void); 44 45 /***************************************************************************/ 46 47 static char hptproc_buffer[256]; 48 49 #define FORMAL_HANDLER_ARGS struct sysctl_oid *oidp, void *arg1, int arg2, \ 50 struct sysctl_req *req 51 #define REAL_HANDLER_ARGS oidp, arg1, arg2, req 52 typedef struct sysctl_req HPT_GET_INFO; 53 54 static int hpt_set_asc_info(IAL_ADAPTER_T *pAdapter, char *buffer,int length) 55 { 56 int orig_length = length+4; 57 PVBus _vbus_p = &pAdapter->VBus; 58 PVDevice pArray; 59 PVDevice pSubArray, pVDev; 60 UINT i, iarray, ichan; 61 struct cam_periph *periph = NULL; 62 intrmask_t oldspl; 63 64 #ifdef SUPPORT_ARRAY 65 if (length>=8 && strncmp(buffer, "rebuild ", 8)==0) 66 { 67 buffer+=8; 68 length-=8; 69 if (length>=5 && strncmp(buffer, "start", 5)==0) 70 { 71 oldspl = lock_driver(); 72 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++) 73 if ((pArray=ArrayTables(i))->u.array.dArStamp==0) 74 continue; 75 else{ 76 if (pArray->u.array.rf_need_rebuild && !pArray->u.array.rf_rebuilding) 77 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, 78 (UCHAR)((pArray->u.array.CriticalMembers || pArray->VDeviceType == VD_RAID_1)? DUPLICATE : REBUILD_PARITY)); 79 } 80 unlock_driver(oldspl); 81 return orig_length; 82 } 83 else if (length>=4 && strncmp(buffer, "stop", 4)==0) 84 { 85 oldspl = lock_driver(); 86 for(i = 0; i < MAX_ARRAY_PER_VBUS; i++) 87 if ((pArray=ArrayTables(i))->u.array.dArStamp==0) 88 continue; 89 else{ 90 if (pArray->u.array.rf_rebuilding) 91 pArray->u.array.rf_abort_rebuild = 1; 92 } 93 unlock_driver(oldspl); 94 return orig_length; 95 } 96 else if (length>=3 && buffer[1]==','&& buffer[0]>='1'&& buffer[2]>='1') 97 { 98 iarray = buffer[0]-'1'; 99 ichan = buffer[2]-'1'; 100 101 if(iarray >= MAX_VDEVICE_PER_VBUS || ichan >= MV_SATA_CHANNELS_NUM) return -EINVAL; 102 103 pArray = _vbus_p->pVDevice[iarray]; 104 if (!pArray || (pArray->vf_online == 0)) return -EINVAL; 105 106 for (i=0;i<MV_SATA_CHANNELS_NUM;i++) 107 if(i == ichan) 108 goto rebuild; 109 110 return -EINVAL; 111 112 rebuild: 113 pVDev = &pAdapter->VDevices[ichan]; 114 if(!pVDev->u.disk.df_on_line || pVDev->pParent) return -EINVAL; 115 116 /* Not allow to use a mounted disk ??? test*/ 117 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++) 118 if(pVDev == _vbus_p->pVDevice[i]) 119 { 120 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId,i); 121 if (periph != NULL && periph->refcount == 1) 122 { 123 hpt_printk(("Can not use disk used by OS!\n")); 124 return -EINVAL; 125 } 126 /* the Mounted Disk isn't delete */ 127 } 128 129 switch(pArray->VDeviceType) 130 { 131 case VD_RAID_1: 132 case VD_RAID_5: 133 { 134 pSubArray = pArray; 135 loop: 136 oldspl = lock_driver(); 137 if(hpt_add_disk_to_array(_VBUS_P VDEV_TO_ID(pSubArray), VDEV_TO_ID(pVDev)) == -1) { 138 unlock_driver(oldspl); 139 return -EINVAL; 140 } 141 pArray->u.array.rf_auto_rebuild = 0; 142 pArray->u.array.rf_abort_rebuild = 0; 143 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, DUPLICATE); 144 unlock_driver(oldspl); 145 break; 146 } 147 case VD_RAID_0: 148 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++) 149 if(pArray->u.array.pMember[i] && mIsArray(pArray->u.array.pMember[i]) && 150 (pArray->u.array.pMember[i]->u.array.rf_broken == 1)) 151 { 152 pSubArray = pArray->u.array.pMember[i]; 153 goto loop; 154 } 155 default: 156 return -EINVAL; 157 } 158 return orig_length; 159 } 160 } 161 else if (length>=7 && strncmp(buffer, "verify ", 7)==0) 162 { 163 buffer+=7; 164 length-=7; 165 if (length>=6 && strncmp(buffer, "start ", 6)==0) 166 { 167 buffer+=6; 168 length-=6; 169 if (length>=1 && *buffer>='1') 170 { 171 iarray = *buffer-'1'; 172 if(iarray >= MAX_VDEVICE_PER_VBUS) return -EINVAL; 173 174 pArray = _vbus_p->pVDevice[iarray]; 175 if (!pArray || (pArray->vf_online == 0)) return -EINVAL; 176 177 if(pArray->VDeviceType != VD_RAID_1 && pArray->VDeviceType != VD_RAID_5) 178 return -EINVAL; 179 180 if (!(pArray->u.array.rf_need_rebuild || 181 pArray->u.array.rf_rebuilding || 182 pArray->u.array.rf_verifying || 183 pArray->u.array.rf_initializing)) 184 { 185 oldspl = lock_driver(); 186 pArray->u.array.RebuildSectors = 0; 187 hpt_queue_dpc((HPT_DPC)hpt_rebuild_data_block, pAdapter, pArray, VERIFY); 188 unlock_driver(oldspl); 189 } 190 return orig_length; 191 } 192 } 193 else if (length>=5 && strncmp(buffer, "stop ", 5)==0) 194 { 195 buffer+=5; 196 length-=5; 197 if (length>=1 && *buffer>='1') 198 { 199 iarray = *buffer-'1'; 200 if(iarray >= MAX_VDEVICE_PER_VBUS) return -EINVAL; 201 202 pArray = _vbus_p->pVDevice[iarray]; 203 if (!pArray || (pArray->vf_online == 0)) return -EINVAL; 204 if(pArray->u.array.rf_verifying) 205 { 206 oldspl = lock_driver(); 207 pArray->u.array.rf_abort_rebuild = 1; 208 unlock_driver(oldspl); 209 } 210 return orig_length; 211 } 212 } 213 } 214 else 215 #ifdef _RAID5N_ 216 if (length>=10 && strncmp(buffer, "writeback ", 10)==0) { 217 buffer+=10; 218 length-=10; 219 if (length>=1 && *buffer>='0' && *buffer<='1') { 220 _vbus_(r5.enable_write_back) = *buffer-'0'; 221 if (_vbus_(r5.enable_write_back)) 222 hpt_printk(("RAID5 write back enabled")); 223 return orig_length; 224 } 225 } 226 else 227 #endif 228 #endif /* SUPPORT_ARRAY */ 229 if (0) {} /* just to compile */ 230 #if DBGUG 231 else if (length>=9 && strncmp(buffer, "dbglevel ", 9)==0) { 232 buffer+=9; 233 length-=9; 234 if (length>=1 && *buffer>='0' && *buffer<='3') { 235 hpt_dbg_level = *buffer-'0'; 236 return orig_length; 237 } 238 } 239 else if (length>=8 && strncmp(buffer, "disable ", 8)==0) { 240 /* TO DO */ 241 } 242 #endif 243 244 return -EINVAL; 245 } 246 247 /* 248 * Since we have only one sysctl node, add adapter ID in the command 249 * line string: e.g. "hpt 0 rebuild start" 250 */ 251 static int hpt_set_info(int length) 252 { 253 int retval; 254 255 #ifdef SUPPORT_IOCTL 256 PUCHAR ke_area; 257 int err; 258 DWORD dwRet; 259 PHPT_IOCTL_PARAM32 piop; 260 #endif 261 char *buffer = hptproc_buffer; 262 if (length >= 6) { 263 if (strncmp(buffer,"hpt ",4) == 0) { 264 IAL_ADAPTER_T *pAdapter; 265 retval = buffer[4]-'0'; 266 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) { 267 if (pAdapter->mvSataAdapter.adapterId==retval) 268 return (retval = hpt_set_asc_info(pAdapter, buffer+6, length-6)) >= 0? retval : -EINVAL; 269 } 270 return -EINVAL; 271 } 272 #ifdef SUPPORT_IOCTL 273 piop = (PHPT_IOCTL_PARAM32)buffer; 274 if (piop->Magic == HPT_IOCTL_MAGIC) { 275 KdPrintE(("ioctl=%d in=%x len=%d out=%x len=%ld\n", 276 piop->dwIoControlCode, 277 piop->lpInBuffer, 278 piop->nInBufferSize, 279 piop->lpOutBuffer, 280 (u_long)piop->nOutBufferSize)); 281 282 /* 283 * map buffer to kernel. 284 */ 285 if (piop->nInBufferSize+piop->nOutBufferSize > PAGE_SIZE) { 286 KdPrintE(("User buffer too large\n")); 287 return -EINVAL; 288 } 289 290 ke_area = malloc(piop->nInBufferSize+piop->nOutBufferSize, M_DEVBUF, M_NOWAIT); 291 if (ke_area == NULL) { 292 KdPrintE(("Couldn't allocate kernel mem.\n")); 293 return -EINVAL; 294 } 295 296 if (piop->nInBufferSize) 297 copyin((void*)(ULONG_PTR)piop->lpInBuffer, ke_area, piop->nInBufferSize); 298 299 /* 300 * call kernel handler. 301 */ 302 err = Kernel_DeviceIoControl(&gIal_Adapter->VBus, 303 piop->dwIoControlCode, ke_area, piop->nInBufferSize, 304 ke_area + piop->nInBufferSize, piop->nOutBufferSize, &dwRet); 305 306 if (err==0) { 307 if (piop->nOutBufferSize) 308 copyout(ke_area + piop->nInBufferSize, (void*)(ULONG_PTR)piop->lpOutBuffer, piop->nOutBufferSize); 309 310 if (piop->lpBytesReturned) 311 copyout(&dwRet, (void*)(ULONG_PTR)piop->lpBytesReturned, sizeof(DWORD)); 312 313 free(ke_area, M_DEVBUF); 314 return length; 315 } 316 else KdPrintW(("Kernel_ioctl(): return %d\n", err)); 317 318 free(ke_area, M_DEVBUF); 319 return -EINVAL; 320 } else { 321 KdPrintW(("Wrong signature: %x\n", piop->Magic)); 322 return -EINVAL; 323 } 324 #endif 325 } 326 327 return -EINVAL; 328 } 329 330 #define shortswap(w) ((WORD)((w)>>8 | ((w) & 0xFF)<<8)) 331 332 static void get_disk_name(char *name, PDevice pDev) 333 { 334 int i; 335 MV_SATA_CHANNEL *pMvSataChannel = pDev->mv; 336 IDENTIFY_DATA2 *pIdentifyData = (IDENTIFY_DATA2 *)pMvSataChannel->identifyDevice; 337 338 for (i = 0; i < 10; i++) 339 ((WORD*)name)[i] = shortswap(pIdentifyData->ModelNumber[i]); 340 name[20] = '\0'; 341 } 342 343 static int hpt_copy_info(HPT_GET_INFO *pinfo, char *fmt, ...) 344 { 345 int printfretval; 346 va_list ap; 347 348 if(fmt == NULL) { 349 *hptproc_buffer = 0; 350 return (SYSCTL_OUT(pinfo, hptproc_buffer, 1)); 351 } 352 else 353 { 354 va_start(ap, fmt); 355 printfretval = vsnprintf(hptproc_buffer, sizeof(hptproc_buffer), fmt, ap); 356 va_end(ap); 357 return(SYSCTL_OUT(pinfo, hptproc_buffer, strlen(hptproc_buffer))); 358 } 359 } 360 361 static void hpt_copy_disk_info(HPT_GET_INFO *pinfo, PVDevice pVDev, UINT iChan) 362 { 363 char name[32], arrayname[16]; 364 365 get_disk_name(name, &pVDev->u.disk); 366 367 #ifdef SUPPORT_ARRAY 368 if(pVDev->pParent) 369 memcpy(arrayname, pVDev->pParent->u.array.ArrayName, MAX_ARRAY_NAME); 370 else 371 #endif 372 arrayname[0]=0; 373 374 hpt_copy_info(pinfo, "Channel %d %s %5dMB %s %s\n", 375 iChan+1, 376 name, pVDev->VDeviceCapacity>>11, 377 ((!pVDev->u.disk.df_on_line)? "Disabled" : 378 ((pVDev->VDeviceType != VD_SPARE)?"Normal ":"Spare ")), arrayname); 379 } 380 381 #ifdef SUPPORT_ARRAY 382 static void hpt_copy_array_info(HPT_GET_INFO *pinfo, int nld, PVDevice pArray) 383 { 384 int i; 385 char *sType=0, *sStatus=0; 386 char buf[32]; 387 PVDevice pTmpArray; 388 389 switch (pArray->VDeviceType) { 390 case VD_RAID_0: 391 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++) 392 if(pArray->u.array.pMember[i]) { 393 if(mIsArray(pArray->u.array.pMember[i])) 394 sType = "RAID 1/0 "; 395 /* TO DO */ 396 else 397 sType = "RAID 0 "; 398 break; 399 } 400 break; 401 402 case VD_RAID_1: 403 sType = "RAID 1 "; 404 break; 405 406 case VD_JBOD: 407 sType = "JBOD "; 408 break; 409 410 case VD_RAID_5: 411 sType = "RAID 5 "; 412 break; 413 414 default: 415 sType = "N/A "; 416 break; 417 } 418 419 if (pArray->vf_online == 0) 420 sStatus = "Disabled"; 421 else if (pArray->u.array.rf_broken) 422 sStatus = "Critical"; 423 for (i = 0; (UCHAR)i < pArray->u.array.bArnMember; i++) 424 { 425 if (!sStatus) 426 { 427 if(mIsArray(pArray->u.array.pMember[i])) 428 pTmpArray = pArray->u.array.pMember[i]; 429 else 430 pTmpArray = pArray; 431 432 if (pTmpArray->u.array.rf_rebuilding) { 433 #ifdef DEBUG 434 sprintf(buf, "Rebuilding %dMB", (pTmpArray->u.array.RebuildSectors>>11)); 435 #else 436 sprintf(buf, "Rebuilding %d%%", (pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11)); 437 #endif 438 sStatus = buf; 439 } 440 else if (pTmpArray->u.array.rf_verifying) { 441 sprintf(buf, "Verifying %d%%", (pTmpArray->u.array.RebuildSectors>>11)*100/((pTmpArray->VDeviceCapacity/(pTmpArray->u.array.bArnMember-1))>>11)); 442 sStatus = buf; 443 } 444 else if (pTmpArray->u.array.rf_need_rebuild) 445 sStatus = "Critical"; 446 else if (pTmpArray->u.array.rf_broken) 447 sStatus = "Critical"; 448 449 if(pTmpArray == pArray) goto out; 450 } 451 else 452 goto out; 453 } 454 out: 455 if (!sStatus) sStatus = "Normal"; 456 hpt_copy_info(pinfo, "%2d %11s %-20s %5dMB %-16s", nld, sType, pArray->u.array.ArrayName, pArray->VDeviceCapacity>>11, sStatus); 457 } 458 #endif 459 460 static int hpt_get_info(IAL_ADAPTER_T *pAdapter, HPT_GET_INFO *pinfo) 461 { 462 PVBus _vbus_p = &pAdapter->VBus; 463 struct cam_periph *periph = NULL; 464 UINT channel,j,i; 465 PVDevice pVDev; 466 467 #ifndef FOR_DEMO 468 if (pAdapter->beeping) { 469 intrmask_t oldspl = lock_driver(); 470 pAdapter->beeping = 0; 471 BeepOff(pAdapter->mvSataAdapter.adapterIoBaseAddress); 472 unlock_driver(oldspl); 473 } 474 #endif 475 476 hpt_copy_info(pinfo, "Controller #%d:\n\n", pAdapter->mvSataAdapter.adapterId); 477 478 hpt_copy_info(pinfo, "Physical device list\n"); 479 hpt_copy_info(pinfo, "Channel Model Capacity Status Array\n"); 480 hpt_copy_info(pinfo, "-------------------------------------------------------------------\n"); 481 482 for (channel = 0; channel < MV_SATA_CHANNELS_NUM; channel++) 483 { 484 pVDev = &(pAdapter->VDevices[channel]); 485 if(pVDev->u.disk.df_on_line) 486 hpt_copy_disk_info(pinfo, pVDev, channel); 487 } 488 489 hpt_copy_info(pinfo, "\nLogical device list\n"); 490 hpt_copy_info(pinfo, "No. Type Name Capacity Status OsDisk\n"); 491 hpt_copy_info(pinfo, "--------------------------------------------------------------------------\n"); 492 493 j=1; 494 for(i = 0; i < MAX_VDEVICE_PER_VBUS; i++){ 495 pVDev = _vbus_p->pVDevice[i]; 496 if(pVDev){ 497 j=i+1; 498 #ifdef SUPPORT_ARRAY 499 if (mIsArray(pVDev)) 500 { 501 is_array: 502 hpt_copy_array_info(pinfo, j, pVDev); 503 } 504 else 505 #endif 506 { 507 char name[32]; 508 /* it may be add to an array after driver loaded, check it */ 509 #ifdef SUPPORT_ARRAY 510 if (pVDev->pParent) 511 /* in this case, pVDev can only be a RAID 1 source disk. */ 512 if (pVDev->pParent->VDeviceType==VD_RAID_1 && pVDev==pVDev->pParent->u.array.pMember[0]) 513 goto is_array; 514 #endif 515 get_disk_name(name, &pVDev->u.disk); 516 517 hpt_copy_info(pinfo, "%2d %s %s %5dMB %-16s", 518 j, "Single disk", name, pVDev->VDeviceCapacity>>11, 519 /* gmm 2001-6-19: Check if pDev has been added to an array. */ 520 ((pVDev->pParent) ? "Unavailable" : "Normal")); 521 } 522 periph = hpt_get_periph(pAdapter->mvSataAdapter.adapterId, i); 523 if (periph == NULL) 524 hpt_copy_info(pinfo," %s\n","not registered"); 525 else 526 hpt_copy_info(pinfo," %s%d\n", periph->periph_name, periph->unit_number); 527 } 528 } 529 return 0; 530 } 531 532 static inline int hpt_proc_in(FORMAL_HANDLER_ARGS, int *len) 533 { 534 int i, error=0; 535 536 *len = 0; 537 if ((req->newlen - req->newidx) >= sizeof(hptproc_buffer)) { 538 error = EINVAL; 539 } else { 540 i = (req->newlen - req->newidx); 541 error = SYSCTL_IN(req, hptproc_buffer, i); 542 if (!error) 543 *len = i; 544 (hptproc_buffer)[i] = '\0'; 545 } 546 return (error); 547 } 548 549 static int hpt_status(FORMAL_HANDLER_ARGS) 550 { 551 int length, error=0, retval=0; 552 IAL_ADAPTER_T *pAdapter; 553 554 error = hpt_proc_in(REAL_HANDLER_ARGS, &length); 555 556 if (req->newptr != NULL) 557 { 558 if (error || length == 0) 559 { 560 KdPrint(("error!\n")); 561 retval = EINVAL; 562 goto out; 563 } 564 565 if (hpt_set_info(length) >= 0) 566 retval = 0; 567 else 568 retval = EINVAL; 569 goto out; 570 } 571 572 hpt_copy_info(req, "%s Version %s\n", DRIVER_NAME, DRIVER_VERSION); 573 for (pAdapter=gIal_Adapter; pAdapter; pAdapter=pAdapter->next) { 574 if (hpt_get_info(pAdapter, req) < 0) { 575 retval = EINVAL; 576 break; 577 } 578 } 579 580 hpt_copy_info(req, NULL); 581 goto out; 582 583 out: 584 return (retval); 585 } 586 587 588 #define xhptregister_node(name) hptregister_node(name) 589 590 #if (__FreeBSD_version < 500043) 591 #define hptregister_node(name) \ 592 SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node") \ 593 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \ 594 NULL, 0, hpt_status, "A", "Get/Set " #name " state") 595 #else 596 #define hptregister_node(name) \ 597 SYSCTL_NODE(, OID_AUTO, name, CTLFLAG_RW, 0, "Get/Set " #name " state root node"); \ 598 SYSCTL_OID(_ ## name, OID_AUTO, status, CTLTYPE_STRING|CTLFLAG_RW, \ 599 NULL, 0, hpt_status, "A", "Get/Set " #name " state"); 600 #endif 601 602 xhptregister_node(PROC_DIR_NAME); 603