1/*- 2 * Copyright (c) 2002, Pierre David <Pierre.David@crc.u-strasbg.fr> 3 * Copyright (c) 2006, Jung-uk Kim <jkim@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * 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. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <fcntl.h> 34#include <errno.h> 35#include <unistd.h> 36 37#include <sys/ioctl.h> 38 39#include <machine/param.h> 40 41#include <dev/pci/amrio.h> 42#include <dev/pci/amrreg.h> 43 44#define NATTEMPTS 5 45#define SLEEPTIME 100000 /* microseconds */ 46 47static int nattempts = NATTEMPTS; /* # of attempts before giving up */ 48static int sleeptime = SLEEPTIME; /* between attempts, in ms */ 49 50#define AMR_BUFSIZE 1024 51 52static int enq_result = AMR_STATUS_FAILED; 53static char enq_buffer[AMR_BUFSIZE]; 54 55#define AMR_MAX_NCTRLS 16 56#define AMR_MAX_NSDEVS 16 57 58static u_int8_t nschan = 0; 59 60/* 61 * Include lookup tables, and a function to match a code to a string. 62 * 63 * XXX Lookup tables cannot be included, since they require symbols from 64 * amrreg.h which need in turn the _KERNEL define. 65 */ 66 67/* #define AMR_DEFINE_TABLES */ 68/* #include "amr_tables.h" */ 69 70static int amr_ioctl_enquiry(int, u_int8_t, u_int8_t, u_int8_t); 71__dead static void usage(const char *); 72static int describe_card(int, int, int); 73static char * describe_property(u_int8_t, char *); 74static const char * describe_state(int, u_int8_t); 75static void describe_battery(int, int, int, int, int); 76static void describe_one_volume(int, int, u_int32_t, u_int8_t, u_int8_t); 77static void describe_one_drive(int, int, u_int8_t); 78static void describe_drive(int, int, int, int, int); 79 80/* 81 * Offsets in an amr_user_ioctl.au_cmd [] array See amrio.h 82 */ 83 84#define MB_COMMAND 0 85#define MB_CHANNEL 1 86#define MB_PARAM 2 87#define MB_PAD 3 88#define MB_DRIVE 4 89 90#define FIRMWARE_40LD 1 91#define FIRMWARE_8LD 2 92 93static struct { 94 const char *product; 95 const uint32_t signature; 96} prodtable[] = { 97 { "Series 431", AMR_SIG_431 }, 98 { "Series 438", AMR_SIG_438 }, 99 { "Series 762", AMR_SIG_762 }, 100 { "Integrated HP NetRAID (T5)", AMR_SIG_T5 }, 101 { "Series 466", AMR_SIG_466 }, 102 { "Series 467", AMR_SIG_467 }, 103 { "Integrated HP NetRAID (T7)", AMR_SIG_T7 }, 104 { "Series 490", AMR_SIG_490 } 105}; 106 107static struct { 108 const int code; 109 const char *ifyes, *ifno; 110} proptable[] = { 111 { AMR_DRV_WRITEBACK, 112 "writeback", "write-through" }, 113 { AMR_DRV_READHEAD, 114 "read-ahead", "no-read-ahead" }, 115 { AMR_DRV_ADAPTIVE, 116 "adaptative-io", "no-adaptative-io" } 117}; 118 119static struct { 120 const int code; 121 const char *status; 122} statetable[] = { 123 { AMR_DRV_OFFLINE, "offline" }, 124 { AMR_DRV_DEGRADED, "degraded" }, 125 { AMR_DRV_OPTIMAL, "optimal" }, 126 { AMR_DRV_ONLINE, "online" }, 127 { AMR_DRV_FAILED, "failed" }, 128 { AMR_DRV_REBUILD, "rebuild" }, 129 { AMR_DRV_HOTSPARE, "hotspare" } 130}; 131 132static struct { 133 const u_int8_t code; 134 const char *status; 135} battable[] = { 136 { AMR_BATT_MODULE_MISSING, "not present" }, 137 { AMR_BATT_LOW_VOLTAGE, "low voltage" }, 138 { AMR_BATT_TEMP_HIGH, "high temperature" }, 139 { AMR_BATT_PACK_MISSING, "pack missing" }, 140 { AMR_BATT_CYCLES_EXCEEDED, "cycle exceeded" } 141}; 142 143static struct { 144 const u_int8_t code; 145 const char *status; 146} bcstatble[] = { 147 { AMR_BATT_CHARGE_DONE, "charge done" }, 148 { AMR_BATT_CHARGE_INPROG, "charge in progress" }, 149 { AMR_BATT_CHARGE_FAIL, "charge failed" } 150}; 151 152#define NTAB(tab) (sizeof tab / sizeof tab [0]) 153 154static int 155amr_ioctl_enquiry(int fd, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual) 156{ 157 struct amr_user_ioctl am; 158 int r, i; 159 160 am.au_cmd[MB_COMMAND] = cmd; 161 am.au_cmd[MB_CHANNEL] = cmdsub; 162 am.au_cmd[MB_PARAM] = cmdqual; 163 am.au_cmd[MB_PAD] = 0; 164 am.au_cmd[MB_DRIVE] = 0; 165 166 am.au_buffer = enq_buffer; 167 am.au_length = AMR_BUFSIZE; 168 am.au_direction = AMR_IO_READ; 169 am.au_status = 0; 170 171 i = 0; 172 r = -1; 173 while (i < nattempts && r == -1) { 174 r = ioctl(fd, AMR_IO_COMMAND, &am); 175 if (r == -1) { 176 if (errno != EBUSY) { 177 perror("ioctl enquiry"); 178 exit(1); 179 } else 180 usleep(sleeptime); 181 } 182 i++; 183 } 184 return am.au_status; 185} 186 187static void 188usage(const char *prog) 189{ 190 fprintf(stderr, "usage: %s stat [-a num] [-b] " 191 "[-c ctlr|-f dev] [-g] [-l vol]\n\t\t" 192 "[-p drive|-s bus[:target]] [-t usec] [-v]\n\n\t" 193 "-a num\t\tnumber of retries\n\t" 194 "-b\t\tbattery status\n\t" 195 "-c ctrl\t\tcontroller ID\n\t" 196 "-f dev\t\tdevice path\n\t" 197 "-g\t\tprint global parameters\n\t" 198 "-l vol\t\tlogical volume ID\n\t" 199 "-p drive\tphysical drive ID\n\t" 200 "-s bus[:target]\tSCSI bus (and optinal target)\n\t" 201 "-t usec\t\tsleep time between retries\n\t" 202 "-v\t\tverbose output\n", 203 prog); 204 exit(1); 205} 206 207/****************************************************************************** 208 * Card description 209 */ 210 211static int 212describe_card(int fd, int verbosity, int globalparam) 213{ 214 struct amr_enquiry *ae; 215 uint32_t cardtype; 216 217 /* 218 * Try the 40LD firmware interface 219 */ 220 221 enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG, 222 AMR_CONFIG_PRODUCT_INFO, 0); 223 if (enq_result == AMR_STATUS_SUCCESS) { 224 struct amr_prodinfo *ap; 225 226 ap = (struct amr_prodinfo *)enq_buffer; 227 nschan = ap->ap_nschan; 228 if (globalparam) { 229 printf("Product\t\t\t<%.80s>\n", ap->ap_product); 230 printf("Firmware\t\t%.16s\n", ap->ap_firmware); 231 printf("BIOS\t\t\t%.16s\n", ap->ap_bios); 232 printf("SCSI channels\t\t%d\n", ap->ap_nschan); 233 printf("Fibre loops\t\t%d\n", ap->ap_fcloops); 234 printf("Memory size\t\t%d MB\n", ap->ap_memsize); 235 if (verbosity >= 1) { 236 printf("Ioctl\t\t\t%d (%s)\n", FIRMWARE_40LD, 237 "40LD"); 238 printf("Signature\t\t0x%08x\n", 239 ap->ap_signature); 240 printf("Configsig\t\t0x%08x\n", 241 ap->ap_configsig); 242 printf("Subsystem\t\t0x%04x\n", 243 ap->ap_subsystem); 244 printf("Subvendor\t\t0x%04x\n", 245 ap->ap_subvendor); 246 printf("Notify counters\t\t%d\n", 247 ap->ap_numnotifyctr); 248 } 249 } 250 return FIRMWARE_40LD; 251 } 252 /* 253 * Try the 8LD firmware interface 254 */ 255 256 enq_result = amr_ioctl_enquiry(fd, AMR_CMD_EXT_ENQUIRY2, 0, 0); 257 ae = (struct amr_enquiry *)enq_buffer; 258 if (enq_result == AMR_STATUS_SUCCESS) { 259 cardtype = ae->ae_signature; 260 } else { 261 enq_result = amr_ioctl_enquiry(fd, AMR_CMD_ENQUIRY, 0, 0); 262 cardtype = 0; 263 } 264 265 if (enq_result == AMR_STATUS_SUCCESS) { 266 267 if (globalparam) { 268 const char *product = NULL; 269 char bios[100], firmware[100]; 270 size_t i; 271 272 for (i = 0; i < NTAB(prodtable); i++) { 273 if (cardtype == prodtable[i].signature) { 274 product = prodtable[i].product; 275 break; 276 } 277 } 278 if (product == NULL) 279 product = "unknown card signature"; 280 281 /* 282 * HP NetRaid controllers have a special encoding of 283 * the firmware and BIOS versions. The AMI version 284 * seems to have it as strings whereas the HP version 285 * does it with a leading uppercase character and two 286 * binary numbers. 287 */ 288 289 if (ae->ae_adapter.aa_firmware[2] >= 'A' && 290 ae->ae_adapter.aa_firmware[2] <= 'Z' && 291 ae->ae_adapter.aa_firmware[1] < ' ' && 292 ae->ae_adapter.aa_firmware[0] < ' ' && 293 ae->ae_adapter.aa_bios[2] >= 'A' && 294 ae->ae_adapter.aa_bios[2] <= 'Z' && 295 ae->ae_adapter.aa_bios[1] < ' ' && 296 ae->ae_adapter.aa_bios[0] < ' ') { 297 298 /* 299 * looks like we have an HP NetRaid version 300 * of the MegaRaid 301 */ 302 303 if (cardtype == AMR_SIG_438) { 304 /* 305 * the AMI 438 is a NetRaid 3si in 306 * HP-land 307 */ 308 product = "HP NetRaid 3si"; 309 } 310 sprintf(firmware, "%c.%02d.%02d", 311 ae->ae_adapter.aa_firmware[2], 312 ae->ae_adapter.aa_firmware[1], 313 ae->ae_adapter.aa_firmware[0]); 314 sprintf(bios, "%c.%02d.%02d", 315 ae->ae_adapter.aa_bios[2], 316 ae->ae_adapter.aa_bios[1], 317 ae->ae_adapter.aa_bios[0]); 318 } else { 319 sprintf(firmware, "%.4s", 320 ae->ae_adapter.aa_firmware); 321 sprintf(bios, "%.4s", ae->ae_adapter.aa_bios); 322 } 323 324 printf("Ioctl = %d (%s)\n", FIRMWARE_8LD, "8LD"); 325 printf("Product =\t<%s>\n", product); 326 printf("Firmware =\t%s\n", firmware); 327 printf("BIOS =\t%s\n", bios); 328 /* printf ("SCSI Channels =\t%d\n", ae->ae_nschan); */ 329 /* printf ("Fibre Loops =\t%d\n", ae->ae_fcloops); */ 330 printf("Memory size =\t%d MB\n", 331 ae->ae_adapter.aa_memorysize); 332 /* 333 * printf ("Notify counters =\t%d\n", 334 * ae->ae_numnotifyctr) ; 335 */ 336 } 337 return FIRMWARE_8LD; 338 } 339 /* 340 * Neither firmware interface succeeded. Abort. 341 */ 342 343 fprintf(stderr, "Firmware interface not supported\n"); 344 exit(1); 345 346} 347 348static char * 349describe_property(u_int8_t prop, char *buffer) 350{ 351 size_t i; 352 353 strcpy(buffer, "<"); 354 for (i = 0; i < NTAB(proptable); i++) { 355 if (i > 0) 356 strcat(buffer, ","); 357 if (prop & proptable[i].code) 358 strcat(buffer, proptable[i].ifyes); 359 else 360 strcat(buffer, proptable[i].ifno); 361 } 362 strcat(buffer, ">"); 363 364 return buffer; 365} 366 367static const char * 368describe_state(int verbosity, u_int8_t state) 369{ 370 size_t i; 371 372 if ((AMR_DRV_PREVSTATE(state) == AMR_DRV_CURSTATE(state)) && 373 (AMR_DRV_CURSTATE(state) == AMR_DRV_OFFLINE) && verbosity == 0) 374 return NULL; 375 376 for (i = 0; i < NTAB(statetable); i++) 377 if (AMR_DRV_CURSTATE(state) == statetable[i].code) 378 return (statetable[i].status); 379 380 return NULL; 381} 382 383/****************************************************************************** 384 * Battery status 385 */ 386static void 387describe_battery(int fd, int verbosity, int fwint, int bflags, int globalparam) 388{ 389 u_int8_t batt_status; 390 size_t i; 391 392 if (fwint == FIRMWARE_40LD) { 393 enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG, 394 AMR_CONFIG_ENQ3, AMR_CONFIG_ENQ3_SOLICITED_FULL); 395 if (enq_result == AMR_STATUS_SUCCESS) { 396 struct amr_enquiry3 *ae3; 397 398 ae3 = (struct amr_enquiry3 *)enq_buffer; 399 if (bflags || globalparam) { 400 batt_status = ae3->ae_batterystatus; 401 printf("Battery status\t\t"); 402 for (i = 0; i < NTAB(battable); i++) { 403 if (batt_status & battable[i].code) 404 printf("%s, ", battable[i].status); 405 } 406 if (!(batt_status & 407 (AMR_BATT_MODULE_MISSING|AMR_BATT_PACK_MISSING))) { 408 for (i = 0; i < NTAB(bcstatble); i++) 409 if (bcstatble[i].code == 410 (batt_status & AMR_BATT_CHARGE_MASK)) 411 printf("%s", bcstatble[i].status); 412 } else 413 printf("charge unknown"); 414 if (verbosity) 415 printf(" (0x%02x)", batt_status); 416 printf("\n"); 417 } 418 } 419 } else if (fwint == FIRMWARE_8LD) { 420 /* Nothing to do here. */ 421 return; 422 } else { 423 fprintf(stderr, "Firmware interface not supported.\n"); 424 exit(1); 425 } 426 427 return; 428} 429 430/****************************************************************************** 431 * Logical volumes 432 */ 433 434static void 435describe_one_volume(int ldrv, int verbosity, 436 u_int32_t size, u_int8_t state, u_int8_t prop) 437{ 438 float szgb; 439 int raid_level; 440 char propstr[MAXPATHLEN]; 441 const char *statestr; 442 443 szgb = ((float)size) / (1024 * 1024 * 2); /* size in GB */ 444 445 raid_level = prop & AMR_DRV_RAID_MASK; 446 447 printf("Logical volume %d\t", ldrv); 448 statestr = describe_state(verbosity, state); 449 printf("%s ", statestr); 450 printf("(%.2f GB, RAID%d", szgb, raid_level); 451 if (verbosity >= 1) { 452 describe_property(prop, propstr); 453 printf(" %s", propstr); 454 } 455 printf(")\n"); 456} 457 458/****************************************************************************** 459 * Physical drives 460 */ 461 462static void 463describe_one_drive(int pdrv, int verbosity, u_int8_t state) 464{ 465 const char *statestr; 466 467 statestr = describe_state(verbosity, state); 468 if (statestr) { 469 if (nschan > 0) 470 printf("Physical drive %d:%d\t%s\n", 471 pdrv / AMR_MAX_NSDEVS, pdrv % AMR_MAX_NSDEVS, 472 statestr); 473 else 474 printf("Physical drive %d:\t%s\n", pdrv, statestr); 475 } 476} 477 478static void 479describe_drive(int verbosity, int fwint, int ldrv, int sbus, int sdev) 480{ 481 int drv, pdrv = -1; 482 483 if (sbus > -1 && sdev > -1) 484 pdrv = (sbus * AMR_MAX_NSDEVS) + sdev; 485 if (nschan != 0) { 486 if (sbus > -1 && sbus >= nschan) { 487 fprintf(stderr, "SCSI channel %d does not exist.\n", sbus); 488 exit(1); 489 } else if (sdev > -1 && sdev >= AMR_MAX_NSDEVS) { 490 fprintf(stderr, "SCSI device %d:%d does not exist.\n", 491 sbus, sdev); 492 exit(1); 493 } 494 } 495 if (fwint == FIRMWARE_40LD) { 496 if (enq_result == AMR_STATUS_SUCCESS) { 497 struct amr_enquiry3 *ae3; 498 499 ae3 = (struct amr_enquiry3 *)enq_buffer; 500 if ((ldrv < 0 && sbus < 0) || ldrv >= 0) { 501 if (ldrv >= ae3->ae_numldrives) { 502 fprintf(stderr, "Logical volume %d " 503 "does not exist.\n", ldrv); 504 exit(1); 505 } 506 if (ldrv < 0) { 507 for (drv = 0; 508 drv < ae3->ae_numldrives; 509 drv++) 510 describe_one_volume(drv, 511 verbosity, 512 ae3->ae_drivesize[drv], 513 ae3->ae_drivestate[drv], 514 ae3->ae_driveprop[drv]); 515 } else { 516 describe_one_volume(ldrv, 517 verbosity, 518 ae3->ae_drivesize[ldrv], 519 ae3->ae_drivestate[ldrv], 520 ae3->ae_driveprop[ldrv]); 521 } 522 } 523 if ((ldrv < 0 && sbus < 0) || sbus >= 0) { 524 if (pdrv >= AMR_40LD_MAXPHYSDRIVES || 525 (nschan != 0 && pdrv >= (nschan * AMR_MAX_NSDEVS))) { 526 fprintf(stderr, "Physical drive %d " 527 "is out of range.\n", pdrv); 528 exit(1); 529 } 530 if (sbus < 0) { 531 for (drv = 0; 532 drv < AMR_40LD_MAXPHYSDRIVES; 533 drv++) { 534 if (nschan != 0 && 535 drv >= (nschan * AMR_MAX_NSDEVS)) 536 break; 537 describe_one_drive(drv, 538 verbosity, 539 ae3->ae_pdrivestate[drv]); 540 } 541 } else if (sdev < 0) { 542 for (drv = sbus * AMR_MAX_NSDEVS; 543 drv < ((sbus + 1) * AMR_MAX_NSDEVS); 544 drv++) { 545 if (nschan != 0 && 546 drv >= (nschan * AMR_MAX_NSDEVS)) 547 break; 548 describe_one_drive(drv, 549 verbosity, 550 ae3->ae_pdrivestate[drv]); 551 } 552 } else { 553 if (nschan != 0 && 554 pdrv < (nschan * AMR_MAX_NSDEVS)) 555 describe_one_drive(pdrv, 1, 556 ae3->ae_pdrivestate[pdrv]); 557 } 558 } 559 } 560 } else if (fwint == FIRMWARE_8LD) { 561 /* Nothing to do here. */ 562 return; 563 } else { 564 fprintf(stderr, "Firmware interface not supported.\n"); 565 exit(1); 566 } 567} 568 569/****************************************************************************** 570 * Main function 571 */ 572 573int 574main(int argc, char *argv[]) 575{ 576 int i; 577 int fd = -1; 578 int globalparam = 0, verbosity = 0; 579 int bflags = 0, fflags = 0, sflags = 0; 580 int lvolno = -1, physno = -1; 581 int sbusno = -1, targetno = -1; 582 char filename[MAXPATHLEN]; 583 char sdev[MAXPATHLEN]; 584 char *pdev; 585 586 extern char *optarg; 587 extern int optind; 588 589 /* 590 * Parse arguments 591 */ 592 if (argc < 2) 593 usage(argv[0]); 594 if (strcmp(argv[1], "stat") != 0) /* only stat implemented for now */ 595 usage(argv[0]); 596 597 optind = 2; 598 while ((i = getopt(argc, argv, "a:bc:f:gl:p:s:t:v")) != -1) 599 switch (i) { 600 case 'a': 601 nattempts = atoi(optarg); 602 break; 603 case 'b': 604 bflags++; 605 break; 606 case 'f': 607 snprintf(filename, MAXPATHLEN, "%s", optarg); 608 filename[MAXPATHLEN - 1] = '\0'; 609 fflags++; 610 break; 611 case 'g': 612 globalparam = 1; 613 break; 614 case 'l': 615 lvolno = atoi(optarg); 616 break; 617 case 'p': 618 physno = atoi(optarg); 619 break; 620 case 's': 621 snprintf(sdev, MAXPATHLEN, "%s", optarg); 622 sdev[MAXPATHLEN - 1] = '\0'; 623 sflags++; 624 break; 625 case 't': 626 sleeptime = atoi(optarg); 627 break; 628 case 'v': 629 verbosity++; 630 break; 631 case '?': 632 default: 633 usage(argv[0]); 634 } 635 argc -= optind; 636 argv += optind; 637 638 if (argc != 0) 639 usage(argv[0]); 640 641 if (!fflags) { 642 snprintf(filename, MAXPATHLEN, "/dev/amr0"); 643 } 644 645 fd = open(filename, O_RDONLY); 646 if (fd == -1) { 647 perror("open"); 648 exit(1); 649 } 650 if (ioctl(fd, AMR_IO_VERSION, &i) == -1) { 651 perror("ioctl version"); 652 exit(1); 653 } 654 655 if (sflags) { 656 if(physno > -1) 657 usage(argv[0]); 658 else { 659 sbusno = atoi(sdev); 660 if ((pdev = index(sdev, ':'))) 661 targetno = atoi(++pdev); 662 } 663 } else if (physno > -1) { 664 sbusno = physno / AMR_MAX_NSDEVS; 665 targetno = physno % AMR_MAX_NSDEVS; 666 } 667 668 if (globalparam && verbosity >= 1) 669 printf("Version\t\t\t%d\n", i); 670#if 0 671 if (i != 1) { 672 fprintf(stderr, "Driver version (%d) not supported\n", i); 673 exit(1); 674 } 675#endif 676 677 i = describe_card(fd, verbosity, globalparam); 678 describe_battery(fd, verbosity, i, bflags, globalparam); 679 if (!bflags || lvolno > -1 || physno > -1 || sbusno > -1 || targetno > -1) 680 describe_drive(verbosity, i, lvolno, sbusno, targetno); 681 682 return 0; 683} 684