1 /*- 2 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org> 3 * Copyright (c) 2015 Allan Jude <allanjude@FreeBSD.org> 4 * Copyright (c) 2000 by Matthew Jacob 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/endian.h> 33 #include <sys/param.h> 34 #include <sys/ioctl.h> 35 #include <sys/types.h> 36 37 #include <err.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <getopt.h> 41 #include <glob.h> 42 #include <stdbool.h> 43 #include <stddef.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <libxo/xo.h> 50 51 #include <cam/scsi/scsi_enc.h> 52 53 #include "eltsub.h" 54 55 #define SESUTIL_XO_VERSION "1" 56 57 static int encstatus(int argc, char **argv); 58 static int fault(int argc, char **argv); 59 static int locate(int argc, char **argv); 60 static int objmap(int argc, char **argv); 61 static int sesled(int argc, char **argv, bool fault); 62 static void sesutil_print(bool *title, const char *fmt, ...) __printflike(2,3); 63 64 static struct command { 65 const char *name; 66 const char *param; 67 const char *desc; 68 int (*exec)(int argc, char **argv); 69 } cmds[] = { 70 { "fault", 71 "(<disk>|<sesid>|all) (on|off)", 72 "Change the state of the fault LED associated with a disk", 73 fault }, 74 { "locate", 75 "(<disk>|<sesid>|all) (on|off)", 76 "Change the state of the locate LED associated with a disk", 77 locate }, 78 { "map", "", 79 "Print a map of the devices managed by the enclosure", objmap } , 80 { "status", "", "Print the status of the enclosure", 81 encstatus }, 82 }; 83 84 static const int nbcmds = nitems(cmds); 85 static const char *uflag; 86 87 static void 88 usage(FILE *out, const char *subcmd) 89 { 90 int i; 91 92 if (subcmd == NULL) { 93 fprintf(out, "Usage: %s [-u /dev/ses<N>] <command> [options]\n", 94 getprogname()); 95 fprintf(out, "Commands supported:\n"); 96 } 97 for (i = 0; i < nbcmds; i++) { 98 if (subcmd != NULL) { 99 if (strcmp(subcmd, cmds[i].name) == 0) { 100 fprintf(out, "Usage: %s %s [-u /dev/ses<N>] " 101 "%s\n\t%s\n", getprogname(), subcmd, 102 cmds[i].param, cmds[i].desc); 103 break; 104 } 105 continue; 106 } 107 fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name, 108 cmds[i].param, cmds[i].desc); 109 } 110 111 exit(EXIT_FAILURE); 112 } 113 114 static void 115 do_led(int fd, unsigned int idx, elm_type_t type, bool onoff, bool setfault) 116 { 117 int state = onoff ? 1 : 0; 118 encioc_elm_status_t o; 119 struct ses_ctrl_dev_slot *slot; 120 121 o.elm_idx = idx; 122 if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) { 123 close(fd); 124 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); 125 } 126 slot = (struct ses_ctrl_dev_slot *) &o.cstat[0]; 127 switch (type) { 128 case ELMTYP_DEVICE: 129 case ELMTYP_ARRAY_DEV: 130 ses_ctrl_common_set_select(&slot->common, 1); 131 if (setfault) 132 ses_ctrl_dev_slot_set_rqst_fault(slot, state); 133 else 134 ses_ctrl_dev_slot_set_rqst_ident(slot, state); 135 break; 136 default: 137 return; 138 } 139 if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) { 140 close(fd); 141 xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); 142 } 143 } 144 145 static bool 146 disk_match(const char *devnames, const char *disk, size_t len) 147 { 148 const char *dname; 149 150 dname = devnames; 151 while ((dname = strstr(dname, disk)) != NULL) { 152 if (dname[len] == '\0' || dname[len] == ',') { 153 return (true); 154 } 155 dname++; 156 } 157 158 return (false); 159 } 160 161 static int 162 sesled(int argc, char **argv, bool setfault) 163 { 164 encioc_elm_devnames_t objdn; 165 encioc_element_t *objp; 166 glob_t g; 167 char *disk, *endptr; 168 size_t len, i, ndisks; 169 int fd; 170 unsigned int nobj, j, sesid; 171 bool all, isses, onoff; 172 173 isses = false; 174 all = false; 175 onoff = false; 176 177 if (argc != 3) { 178 usage(stderr, (setfault ? "fault" : "locate")); 179 } 180 181 disk = argv[1]; 182 183 sesid = strtoul(disk, &endptr, 10); 184 if (*endptr == '\0') { 185 endptr = strrchr(uflag, '*'); 186 if (endptr != NULL && *endptr == '*') { 187 xo_warnx("Must specifying a SES device (-u) to use a SES " 188 "id# to identify a disk"); 189 usage(stderr, (setfault ? "fault" : "locate")); 190 } 191 isses = true; 192 } 193 194 if (strcmp(argv[2], "on") == 0) { 195 onoff = true; 196 } else if (strcmp(argv[2], "off") == 0) { 197 onoff = false; 198 } else { 199 usage(stderr, (setfault ? "fault" : "locate")); 200 } 201 202 if (strcmp(disk, "all") == 0) { 203 all = true; 204 } 205 len = strlen(disk); 206 207 /* Get the list of ses devices */ 208 if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) == 209 GLOB_NOMATCH) { 210 globfree(&g); 211 xo_errx(EXIT_FAILURE, "No SES devices found"); 212 } 213 214 ndisks = 0; 215 for (i = 0; i < g.gl_pathc; i++) { 216 /* ensure we only got numbers after ses */ 217 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 218 strlen(g.gl_pathv[i] + 8)) { 219 continue; 220 } 221 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 222 /* 223 * Don't treat non-access errors as critical if we are 224 * accessing all devices 225 */ 226 if (errno == EACCES && g.gl_pathc > 1) { 227 xo_err(EXIT_FAILURE, "unable to access SES device"); 228 } 229 xo_warn("unable to access SES device: %s", g.gl_pathv[i]); 230 continue; 231 } 232 233 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { 234 close(fd); 235 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); 236 } 237 238 objp = calloc(nobj, sizeof(encioc_element_t)); 239 if (objp == NULL) { 240 close(fd); 241 xo_err(EXIT_FAILURE, "calloc()"); 242 } 243 244 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) { 245 free(objp); 246 close(fd); 247 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); 248 } 249 250 if (isses) { 251 if (sesid >= nobj) { 252 free(objp); 253 close(fd); 254 xo_errx(EXIT_FAILURE, 255 "Requested SES ID does not exist"); 256 } 257 do_led(fd, sesid, objp[sesid].elm_type, onoff, setfault); 258 ndisks++; 259 free(objp); 260 close(fd); 261 break; 262 } 263 for (j = 0; j < nobj; j++) { 264 const int devnames_size = 128; 265 char devnames[devnames_size]; 266 267 if (all) { 268 do_led(fd, objp[j].elm_idx, objp[j].elm_type, 269 onoff, setfault); 270 continue; 271 } 272 memset(&objdn, 0, sizeof(objdn)); 273 memset(devnames, 0, devnames_size); 274 objdn.elm_idx = objp[j].elm_idx; 275 objdn.elm_names_size = devnames_size; 276 objdn.elm_devnames = devnames; 277 if (ioctl(fd, ENCIOC_GETELMDEVNAMES, 278 (caddr_t) &objdn) <0) { 279 continue; 280 } 281 if (objdn.elm_names_len > 0) { 282 if (disk_match(objdn.elm_devnames, disk, len)) { 283 do_led(fd, objdn.elm_idx, objp[j].elm_type, 284 onoff, setfault); 285 ndisks++; 286 break; 287 } 288 } 289 } 290 free(objp); 291 close(fd); 292 } 293 globfree(&g); 294 if (ndisks == 0 && all == false) { 295 xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'", 296 disk); 297 } 298 299 return (EXIT_SUCCESS); 300 } 301 302 static int 303 locate(int argc, char **argv) 304 { 305 306 return (sesled(argc, argv, false)); 307 } 308 309 static int 310 fault(int argc, char **argv) 311 { 312 313 return (sesled(argc, argv, true)); 314 } 315 316 #define TEMPERATURE_OFFSET 20 317 static void 318 sesutil_print(bool *title, const char *fmt, ...) 319 { 320 va_list args; 321 322 if (!*title) { 323 xo_open_container("extra_status"); 324 xo_emit("\t\tExtra status:\n"); 325 *title = true; 326 } 327 va_start(args, fmt); 328 xo_emit_hv(NULL, fmt, args); 329 va_end(args); 330 } 331 332 static void 333 print_extra_status(int eletype, u_char *cstat) 334 { 335 bool title = false; 336 337 if (cstat[0] & 0x40) { 338 sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n"); 339 } 340 if (cstat[0] & 0x20) { 341 sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n"); 342 } 343 if (cstat[0] & 0x10) { 344 sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n"); 345 } 346 switch (eletype) { 347 case ELMTYP_DEVICE: 348 case ELMTYP_ARRAY_DEV: 349 if (cstat[2] & 0x02) { 350 sesutil_print(&title, "\t\t- LED={q:led/locate}\n"); 351 } 352 if (cstat[2] & 0x20) { 353 sesutil_print(&title, "\t\t- LED={q:led/fault}\n"); 354 } 355 break; 356 case ELMTYP_FAN: 357 sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n", 358 (((0x7 & cstat[1]) << 8) + cstat[2]) * 10); 359 break; 360 case ELMTYP_THERM: 361 if (cstat[2]) { 362 sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n", 363 cstat[2] - TEMPERATURE_OFFSET); 364 } else { 365 sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n"); 366 } 367 break; 368 case ELMTYP_VOM: 369 sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n", 370 be16dec(cstat + 2) / 100.0); 371 break; 372 } 373 if (title) { 374 xo_close_container("extra_status"); 375 } 376 } 377 378 static int 379 objmap(int argc, char **argv __unused) 380 { 381 encioc_string_t stri; 382 encioc_elm_devnames_t e_devname; 383 encioc_elm_status_t e_status; 384 encioc_elm_desc_t e_desc; 385 encioc_element_t *e_ptr; 386 glob_t g; 387 int fd; 388 unsigned int j, nobj; 389 size_t i; 390 char str[32]; 391 392 if (argc != 1) { 393 usage(stderr, "map"); 394 } 395 396 /* Get the list of ses devices */ 397 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { 398 globfree(&g); 399 xo_errx(EXIT_FAILURE, "No SES devices found"); 400 } 401 xo_set_version(SESUTIL_XO_VERSION); 402 xo_open_container("sesutil"); 403 xo_open_list("enclosures"); 404 for (i = 0; i < g.gl_pathc; i++) { 405 /* ensure we only got numbers after ses */ 406 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 407 strlen(g.gl_pathv[i] + 8)) { 408 continue; 409 } 410 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 411 /* 412 * Don't treat non-access errors as critical if we are 413 * accessing all devices 414 */ 415 if (errno == EACCES && g.gl_pathc > 1) { 416 xo_err(EXIT_FAILURE, "unable to access SES device"); 417 } 418 xo_warn("unable to access SES device: %s", g.gl_pathv[i]); 419 continue; 420 } 421 422 if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { 423 close(fd); 424 xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); 425 } 426 427 e_ptr = calloc(nobj, sizeof(encioc_element_t)); 428 if (e_ptr == NULL) { 429 close(fd); 430 xo_err(EXIT_FAILURE, "calloc()"); 431 } 432 433 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) { 434 close(fd); 435 xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); 436 } 437 438 xo_open_instance("enclosures"); 439 xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5); 440 stri.bufsiz = sizeof(str); 441 stri.buf = &str[0]; 442 if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0) 443 xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf); 444 stri.bufsiz = sizeof(str); 445 stri.buf = &str[0]; 446 if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0) 447 xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf); 448 449 xo_open_list("elements"); 450 for (j = 0; j < nobj; j++) { 451 /* Get the status of the element */ 452 memset(&e_status, 0, sizeof(e_status)); 453 e_status.elm_idx = e_ptr[j].elm_idx; 454 if (ioctl(fd, ENCIOC_GETELMSTAT, 455 (caddr_t) &e_status) < 0) { 456 close(fd); 457 xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); 458 } 459 /* Get the description of the element */ 460 memset(&e_desc, 0, sizeof(e_desc)); 461 e_desc.elm_idx = e_ptr[j].elm_idx; 462 e_desc.elm_desc_len = UINT16_MAX; 463 e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); 464 if (e_desc.elm_desc_str == NULL) { 465 close(fd); 466 xo_err(EXIT_FAILURE, "calloc()"); 467 } 468 if (ioctl(fd, ENCIOC_GETELMDESC, 469 (caddr_t) &e_desc) < 0) { 470 close(fd); 471 xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); 472 } 473 /* Get the device name(s) of the element */ 474 memset(&e_devname, 0, sizeof(e_devname)); 475 e_devname.elm_idx = e_ptr[j].elm_idx; 476 e_devname.elm_names_size = 128; 477 e_devname.elm_devnames = calloc(128, sizeof(char)); 478 if (e_devname.elm_devnames == NULL) { 479 close(fd); 480 xo_err(EXIT_FAILURE, "calloc()"); 481 } 482 if (ioctl(fd, ENCIOC_GETELMDEVNAMES, 483 (caddr_t) &e_devname) <0) { 484 /* We don't care if this fails */ 485 e_devname.elm_devnames[0] = '\0'; 486 } 487 xo_open_instance("elements"); 488 xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx, 489 geteltnm(e_ptr[j].elm_type)); 490 xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n", 491 scode2ascii(e_status.cstat[0]), e_status.cstat[0], 492 e_status.cstat[1], e_status.cstat[2], 493 e_status.cstat[3]); 494 if (e_desc.elm_desc_len > 0) { 495 xo_emit("\t\tDescription: {:description/%s}\n", 496 e_desc.elm_desc_str); 497 } 498 if (e_devname.elm_names_len > 0) { 499 xo_emit("\t\tDevice Names: {:device_names/%s}\n", 500 e_devname.elm_devnames); 501 } 502 print_extra_status(e_ptr[j].elm_type, e_status.cstat); 503 xo_close_instance("elements"); 504 free(e_devname.elm_devnames); 505 } 506 xo_close_list("elements"); 507 free(e_ptr); 508 close(fd); 509 } 510 globfree(&g); 511 xo_close_list("enclosures"); 512 xo_close_container("sesutil"); 513 xo_finish(); 514 515 return (EXIT_SUCCESS); 516 } 517 518 static int 519 encstatus(int argc, char **argv __unused) 520 { 521 glob_t g; 522 int fd, status; 523 size_t i, e; 524 u_char estat; 525 526 status = 0; 527 if (argc != 1) { 528 usage(stderr, "status"); 529 } 530 531 /* Get the list of ses devices */ 532 if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { 533 globfree(&g); 534 xo_errx(EXIT_FAILURE, "No SES devices found"); 535 } 536 537 xo_set_version(SESUTIL_XO_VERSION); 538 xo_open_container("sesutil"); 539 xo_open_list("enclosures"); 540 for (i = 0; i < g.gl_pathc; i++) { 541 /* ensure we only got numbers after ses */ 542 if (strspn(g.gl_pathv[i] + 8, "0123456789") != 543 strlen(g.gl_pathv[i] + 8)) { 544 continue; 545 } 546 if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { 547 /* 548 * Don't treat non-access errors as critical if we are 549 * accessing all devices 550 */ 551 if (errno == EACCES && g.gl_pathc > 1) { 552 xo_err(EXIT_FAILURE, "unable to access SES device"); 553 } 554 xo_warn("unable to access SES device: %s", g.gl_pathv[i]); 555 continue; 556 } 557 558 if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) { 559 xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT"); 560 close(fd); 561 } 562 563 xo_open_instance("enclosures"); 564 xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5); 565 e = 0; 566 if (estat == 0) { 567 if (status == 0) { 568 status = 1; 569 } 570 xo_emit("{q:status/OK}"); 571 } else { 572 if (estat & SES_ENCSTAT_INFO) { 573 xo_emit("{lq:status/INFO}"); 574 e++; 575 } 576 if (estat & SES_ENCSTAT_NONCRITICAL) { 577 if (e) 578 xo_emit(","); 579 xo_emit("{lq:status/NONCRITICAL}"); 580 e++; 581 } 582 if (estat & SES_ENCSTAT_CRITICAL) { 583 if (e) 584 xo_emit(","); 585 xo_emit("{lq:status/CRITICAL}"); 586 e++; 587 status = -1; 588 } 589 if (estat & SES_ENCSTAT_UNRECOV) { 590 if (e) 591 xo_emit(","); 592 xo_emit("{lq:status/UNRECOV}"); 593 e++; 594 status = -1; 595 } 596 } 597 xo_close_instance("enclosures"); 598 xo_emit("\n"); 599 close(fd); 600 } 601 globfree(&g); 602 603 xo_close_list("enclosures"); 604 xo_close_container("sesutil"); 605 xo_finish(); 606 607 if (status == 1) { 608 return (EXIT_SUCCESS); 609 } else { 610 return (EXIT_FAILURE); 611 } 612 } 613 614 int 615 main(int argc, char **argv) 616 { 617 int i, ch; 618 struct command *cmd = NULL; 619 620 argc = xo_parse_args(argc, argv); 621 if (argc < 0) 622 exit(1); 623 624 uflag = "/dev/ses[0-9]*"; 625 while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) { 626 switch (ch) { 627 case 'u': 628 uflag = optarg; 629 break; 630 case '?': 631 default: 632 usage(stderr, NULL); 633 } 634 } 635 argc -= optind; 636 argv += optind; 637 638 if (argc < 1) { 639 warnx("Missing command"); 640 usage(stderr, NULL); 641 } 642 643 for (i = 0; i < nbcmds; i++) { 644 if (strcmp(argv[0], cmds[i].name) == 0) { 645 cmd = &cmds[i]; 646 break; 647 } 648 } 649 650 if (cmd == NULL) { 651 warnx("unknown command %s", argv[0]); 652 usage(stderr, NULL); 653 } 654 655 return (cmd->exec(argc, argv)); 656 } 657