1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2017 Joyent, Inc. 14 * Copyright 2021 Oxide Computer Company 15 * Copyright 2022 Tintri by DDN, Inc. All rights reserved. 16 */ 17 18 /* 19 * nvmeadm -- NVMe administration utility 20 * 21 * nvmeadm [-v] [-d] [-h] <command> [<ctl>[/<ns>][,...]] [args] 22 * commands: list 23 * identify 24 * get-logpage <logpage name> 25 * get-features <feature>[,...] 26 * format ... 27 * secure-erase ... 28 * detach ... 29 * attach ... 30 * list-firmware ... 31 * load-firmware ... 32 * commit-firmware ... 33 * activate-firmware ... 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <unistd.h> 39 #include <fcntl.h> 40 #include <strings.h> 41 #include <ctype.h> 42 #include <err.h> 43 #include <sys/sunddi.h> 44 #include <libdevinfo.h> 45 46 #include <sys/nvme.h> 47 48 #include "nvmeadm.h" 49 50 struct nvme_feature { 51 char *f_name; 52 char *f_short; 53 uint8_t f_feature; 54 size_t f_bufsize; 55 uint_t f_getflags; 56 int (*f_get)(int, const nvme_feature_t *, const nvme_process_arg_t *); 57 void (*f_print)(uint64_t, void *, size_t, nvme_identify_ctrl_t *, 58 nvme_version_t *); 59 }; 60 61 #define NVMEADM_F_CTRL 1 62 #define NVMEADM_F_NS 2 63 #define NVMEADM_F_BOTH (NVMEADM_F_CTRL | NVMEADM_F_NS) 64 65 #define NVMEADM_C_MULTI 1 66 #define NVMEADM_C_EXCL 2 67 68 struct nvmeadm_cmd { 69 char *c_name; 70 const char *c_desc; 71 const char *c_flagdesc; 72 int (*c_func)(int, const nvme_process_arg_t *); 73 void (*c_usage)(const char *); 74 void (*c_optparse)(nvme_process_arg_t *); 75 int c_flags; 76 }; 77 78 79 static void usage(const nvmeadm_cmd_t *); 80 static void nvme_walk(nvme_process_arg_t *, di_node_t); 81 static boolean_t nvme_match(nvme_process_arg_t *); 82 83 static int nvme_process(di_node_t, di_minor_t, void *); 84 85 static int do_list(int, const nvme_process_arg_t *); 86 static int do_identify(int, const nvme_process_arg_t *); 87 static int do_get_logpage_error(int, const nvme_process_arg_t *); 88 static int do_get_logpage_health(int, const nvme_process_arg_t *); 89 static int do_get_logpage_fwslot(int, const nvme_process_arg_t *); 90 static int do_get_logpage(int, const nvme_process_arg_t *); 91 static int do_get_feat_common(int, const nvme_feature_t *, 92 const nvme_process_arg_t *); 93 static int do_get_feat_intr_vect(int, const nvme_feature_t *, 94 const nvme_process_arg_t *); 95 static int do_get_feat_temp_thresh(int, const nvme_feature_t *, 96 const nvme_process_arg_t *); 97 static int do_get_features(int, const nvme_process_arg_t *); 98 static int do_format(int, const nvme_process_arg_t *); 99 static int do_secure_erase(int, const nvme_process_arg_t *); 100 static int do_attach_detach(int, const nvme_process_arg_t *); 101 static int do_firmware_load(int, const nvme_process_arg_t *); 102 static int do_firmware_commit(int, const nvme_process_arg_t *); 103 static int do_firmware_activate(int, const nvme_process_arg_t *); 104 105 static void optparse_list(nvme_process_arg_t *); 106 static void optparse_secure_erase(nvme_process_arg_t *); 107 108 static void usage_list(const char *); 109 static void usage_identify(const char *); 110 static void usage_get_logpage(const char *); 111 static void usage_get_features(const char *); 112 static void usage_format(const char *); 113 static void usage_secure_erase(const char *); 114 static void usage_attach_detach(const char *); 115 static void usage_firmware_list(const char *); 116 static void usage_firmware_load(const char *); 117 static void usage_firmware_commit(const char *); 118 static void usage_firmware_activate(const char *); 119 120 int verbose; 121 int debug; 122 123 #define NVMEADM_O_SE_CRYPTO 0x00000004 124 125 static int exitcode; 126 127 static const nvmeadm_cmd_t nvmeadm_cmds[] = { 128 { 129 "list", 130 "list controllers and namespaces", 131 " -p\t\tprint parsable output\n" 132 " -o field\tselect a field for parsable output\n", 133 do_list, usage_list, optparse_list, 134 NVMEADM_C_MULTI 135 }, 136 { 137 "identify", 138 "identify controllers and/or namespaces", 139 NULL, 140 do_identify, usage_identify, NULL, 141 NVMEADM_C_MULTI 142 }, 143 { 144 "get-logpage", 145 "get a log page from controllers and/or namespaces", 146 NULL, 147 do_get_logpage, usage_get_logpage, NULL, 148 NVMEADM_C_MULTI 149 }, 150 { 151 "get-features", 152 "get features from controllers and/or namespaces", 153 NULL, 154 do_get_features, usage_get_features, NULL, 155 NVMEADM_C_MULTI 156 }, 157 { 158 "format", 159 "format namespace(s) of a controller", 160 NULL, 161 do_format, usage_format, NULL, 162 NVMEADM_C_EXCL 163 }, 164 { 165 "secure-erase", 166 "secure erase namespace(s) of a controller", 167 " -c Do a cryptographic erase.", 168 do_secure_erase, usage_secure_erase, optparse_secure_erase, 169 NVMEADM_C_EXCL 170 }, 171 { 172 "detach", 173 "detach blkdev(4D) from namespace(s) of a controller", 174 NULL, 175 do_attach_detach, usage_attach_detach, NULL, 176 NVMEADM_C_EXCL 177 }, 178 { 179 "attach", 180 "attach blkdev(4D) to namespace(s) of a controller", 181 NULL, 182 do_attach_detach, usage_attach_detach, NULL, 183 NVMEADM_C_EXCL 184 }, 185 { 186 "list-firmware", 187 "list firmware on a controller", 188 NULL, 189 do_get_logpage_fwslot, usage_firmware_list, NULL, 190 0 191 }, 192 { 193 "load-firmware", 194 "load firmware to a controller", 195 NULL, 196 do_firmware_load, usage_firmware_load, NULL, 197 0 198 }, 199 { 200 "commit-firmware", 201 "commit downloaded firmware to a slot of a controller", 202 NULL, 203 do_firmware_commit, usage_firmware_commit, NULL, 204 0 205 }, 206 { 207 "activate-firmware", 208 "activate a firmware slot of a controller", 209 NULL, 210 do_firmware_activate, usage_firmware_activate, NULL, 211 0 212 }, 213 { 214 NULL, NULL, NULL, 215 NULL, NULL, NULL, 0 216 } 217 }; 218 219 static const nvme_feature_t features[] = { 220 { "Arbitration", "", 221 NVME_FEAT_ARBITRATION, 0, NVMEADM_F_CTRL, 222 do_get_feat_common, nvme_print_feat_arbitration }, 223 { "Power Management", "", 224 NVME_FEAT_POWER_MGMT, 0, NVMEADM_F_CTRL, 225 do_get_feat_common, nvme_print_feat_power_mgmt }, 226 { "LBA Range Type", "range", 227 NVME_FEAT_LBA_RANGE, NVME_LBA_RANGE_BUFSIZE, NVMEADM_F_NS, 228 do_get_feat_common, nvme_print_feat_lba_range }, 229 { "Temperature Threshold", "", 230 NVME_FEAT_TEMPERATURE, 0, NVMEADM_F_CTRL, 231 do_get_feat_temp_thresh, nvme_print_feat_temperature }, 232 { "Error Recovery", "", 233 NVME_FEAT_ERROR, 0, NVMEADM_F_CTRL, 234 do_get_feat_common, nvme_print_feat_error }, 235 { "Volatile Write Cache", "cache", 236 NVME_FEAT_WRITE_CACHE, 0, NVMEADM_F_CTRL, 237 do_get_feat_common, nvme_print_feat_write_cache }, 238 { "Number of Queues", "queues", 239 NVME_FEAT_NQUEUES, 0, NVMEADM_F_CTRL, 240 do_get_feat_common, nvme_print_feat_nqueues }, 241 { "Interrupt Coalescing", "coalescing", 242 NVME_FEAT_INTR_COAL, 0, NVMEADM_F_CTRL, 243 do_get_feat_common, nvme_print_feat_intr_coal }, 244 { "Interrupt Vector Configuration", "vector", 245 NVME_FEAT_INTR_VECT, 0, NVMEADM_F_CTRL, 246 do_get_feat_intr_vect, nvme_print_feat_intr_vect }, 247 { "Write Atomicity", "atomicity", 248 NVME_FEAT_WRITE_ATOM, 0, NVMEADM_F_CTRL, 249 do_get_feat_common, nvme_print_feat_write_atom }, 250 { "Asynchronous Event Configuration", "event", 251 NVME_FEAT_ASYNC_EVENT, 0, NVMEADM_F_CTRL, 252 do_get_feat_common, nvme_print_feat_async_event }, 253 { "Autonomous Power State Transition", "", 254 NVME_FEAT_AUTO_PST, NVME_AUTO_PST_BUFSIZE, NVMEADM_F_CTRL, 255 do_get_feat_common, nvme_print_feat_auto_pst }, 256 { "Software Progress Marker", "progress", 257 NVME_FEAT_PROGRESS, 0, NVMEADM_F_CTRL, 258 do_get_feat_common, nvme_print_feat_progress }, 259 { NULL, NULL, 0, 0, B_FALSE, NULL } 260 }; 261 262 263 int 264 main(int argc, char **argv) 265 { 266 int c; 267 const nvmeadm_cmd_t *cmd; 268 di_node_t node; 269 nvme_process_arg_t npa = { 0 }; 270 int help = 0; 271 char *tmp, *lasts = NULL; 272 char *ctrl = NULL; 273 274 while ((c = getopt(argc, argv, "dhv")) != -1) { 275 switch (c) { 276 case 'd': 277 debug++; 278 break; 279 case 'v': 280 verbose++; 281 break; 282 case 'h': 283 help++; 284 break; 285 case '?': 286 usage(NULL); 287 exit(-1); 288 } 289 } 290 291 if (optind == argc) { 292 usage(NULL); 293 if (help) 294 exit(0); 295 else 296 exit(-1); 297 } 298 299 /* Look up the specified command in the command table. */ 300 for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++) 301 if (strcmp(cmd->c_name, argv[optind]) == 0) 302 break; 303 304 if (cmd->c_name == NULL) { 305 usage(NULL); 306 exit(-1); 307 } 308 309 if (help) { 310 usage(cmd); 311 exit(0); 312 } 313 314 npa.npa_cmd = cmd; 315 npa.npa_interactive = B_TRUE; 316 npa.npa_excl = ((cmd->c_flags & NVMEADM_C_EXCL) != 0); 317 318 optind++; 319 320 /* 321 * Store the remaining arguments for use by the command. Give the 322 * command a chance to process the options across the board before going 323 * into each controller. 324 */ 325 npa.npa_argc = argc - optind; 326 npa.npa_argv = &argv[optind]; 327 328 if (cmd->c_optparse != NULL) { 329 cmd->c_optparse(&npa); 330 } 331 332 /* 333 * All commands but "list" require a ctl/ns argument. However, this 334 * should not be passed through to the command in its subsequent 335 * arguments. 336 */ 337 if ((npa.npa_argc == 0 || (strncmp(npa.npa_argv[0], "nvme", 4) != 0)) && 338 cmd->c_func != do_list) { 339 warnx("missing controller/namespace name"); 340 usage(cmd); 341 exit(-1); 342 } 343 344 if (npa.npa_argc > 0) { 345 ctrl = npa.npa_argv[0]; 346 npa.npa_argv++; 347 npa.npa_argc--; 348 } else { 349 ctrl = NULL; 350 } 351 352 /* 353 * Make sure we're not running commands on multiple controllers that 354 * aren't allowed to do that. 355 */ 356 if (ctrl != NULL && strchr(ctrl, ',') != NULL && 357 (cmd->c_flags & NVMEADM_C_MULTI) == 0) { 358 warnx("%s not allowed on multiple controllers", 359 cmd->c_name); 360 usage(cmd); 361 exit(-1); 362 } 363 364 /* 365 * Get controller/namespace arguments and run command. 366 */ 367 npa.npa_name = strtok_r(ctrl, ",", &lasts); 368 do { 369 if (npa.npa_name != NULL) { 370 tmp = strchr(npa.npa_name, '/'); 371 if (tmp != NULL) { 372 *tmp++ = '\0'; 373 npa.npa_nsid = tmp; 374 npa.npa_isns = B_TRUE; 375 } 376 } 377 378 if ((node = di_init("/", DINFOSUBTREE | DINFOMINOR)) == NULL) 379 err(-1, "failed to initialize libdevinfo"); 380 nvme_walk(&npa, node); 381 di_fini(node); 382 383 if (npa.npa_found == 0) { 384 if (npa.npa_name != NULL) { 385 warnx("%s%.*s%.*s: no such controller or " 386 "namespace", npa.npa_name, 387 npa.npa_isns ? -1 : 0, "/", 388 npa.npa_isns ? -1 : 0, npa.npa_nsid); 389 } else { 390 warnx("no controllers found"); 391 } 392 exitcode--; 393 } 394 npa.npa_found = 0; 395 npa.npa_name = strtok_r(NULL, ",", &lasts); 396 } while (npa.npa_name != NULL); 397 398 exit(exitcode); 399 } 400 401 static void 402 nvme_oferr(const char *fmt, ...) 403 { 404 va_list ap; 405 406 va_start(ap, fmt); 407 verrx(-1, fmt, ap); 408 } 409 410 static void 411 usage(const nvmeadm_cmd_t *cmd) 412 { 413 const char *progname = getprogname(); 414 415 (void) fprintf(stderr, "usage:\n"); 416 (void) fprintf(stderr, " %s -h %s\n", progname, 417 cmd != NULL ? cmd->c_name : "[<command>]"); 418 (void) fprintf(stderr, " %s [-dv] ", progname); 419 420 if (cmd != NULL) { 421 cmd->c_usage(cmd->c_name); 422 } else { 423 (void) fprintf(stderr, 424 "<command> <ctl>[/<ns>][,...] [<args>]\n"); 425 (void) fprintf(stderr, 426 "\n Manage NVMe controllers and namespaces.\n"); 427 (void) fprintf(stderr, "\ncommands:\n"); 428 429 for (cmd = &nvmeadm_cmds[0]; cmd->c_name != NULL; cmd++) 430 (void) fprintf(stderr, " %-18s - %s\n", 431 cmd->c_name, cmd->c_desc); 432 } 433 (void) fprintf(stderr, "\n%s flags:\n" 434 " -h\t\tprint usage information\n" 435 " -d\t\tprint information useful for debugging %s\n" 436 " -v\t\tprint verbose information\n", 437 progname, progname); 438 439 if (cmd != NULL && cmd->c_flagdesc != NULL) { 440 (void) fprintf(stderr, "\n%s %s flags:\n", 441 progname, cmd->c_name); 442 (void) fprintf(stderr, "%s\n", cmd->c_flagdesc); 443 } 444 } 445 446 static boolean_t 447 nvme_match(nvme_process_arg_t *npa) 448 { 449 char *name; 450 char *nsid = NULL; 451 452 if (npa->npa_name == NULL) 453 return (B_TRUE); 454 455 if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node), 456 di_instance(npa->npa_node)) < 0) 457 err(-1, "nvme_match()"); 458 459 if (strcmp(name, npa->npa_name) != 0) { 460 free(name); 461 return (B_FALSE); 462 } 463 464 free(name); 465 466 if (npa->npa_isns) { 467 if (npa->npa_nsid == NULL) 468 return (B_TRUE); 469 470 nsid = di_minor_name(npa->npa_minor); 471 472 if (nsid == NULL || strcmp(npa->npa_nsid, nsid) != 0) 473 return (B_FALSE); 474 } 475 476 return (B_TRUE); 477 } 478 479 char * 480 nvme_dskname(const nvme_process_arg_t *npa) 481 { 482 char *path = NULL; 483 di_node_t child; 484 di_dim_t dim; 485 char *addr; 486 char *disk_ctd; 487 char *diskname = NULL; 488 489 dim = di_dim_init(); 490 491 for (child = di_child_node(npa->npa_node); 492 child != DI_NODE_NIL; 493 child = di_sibling_node(child)) { 494 addr = di_bus_addr(child); 495 if (addr == NULL) 496 continue; 497 498 if (addr[0] == 'w') 499 addr++; 500 501 if (strncasecmp(addr, di_minor_name(npa->npa_minor), 502 strchrnul(addr, ',') - addr) != 0) 503 continue; 504 505 path = di_dim_path_dev(dim, di_driver_name(child), 506 di_instance(child), "c"); 507 508 /* 509 * Error out if we didn't get a path, or if it's too short for 510 * the following operations to be safe. 511 */ 512 if (path == NULL || strlen(path) < 2) 513 goto fail; 514 515 /* Chop off 's0' and get everything past the last '/' */ 516 path[strlen(path) - 2] = '\0'; 517 disk_ctd = strrchr(path, '/'); 518 if (disk_ctd == NULL) 519 goto fail; 520 diskname = strdup(++disk_ctd); 521 if (diskname == NULL) 522 goto fail; 523 524 free(path); 525 break; 526 } 527 528 di_dim_fini(dim); 529 530 return (diskname); 531 532 fail: 533 free(path); 534 err(-1, "nvme_dskname"); 535 } 536 537 static int 538 nvme_process(di_node_t node, di_minor_t minor, void *arg) 539 { 540 nvme_process_arg_t *npa = arg; 541 int fd; 542 543 npa->npa_node = node; 544 npa->npa_minor = minor; 545 546 if (!nvme_match(npa)) 547 return (DI_WALK_CONTINUE); 548 549 if ((fd = nvme_open(minor, npa->npa_excl)) < 0) 550 return (DI_WALK_CONTINUE); 551 552 npa->npa_found++; 553 554 npa->npa_path = di_devfs_path(node); 555 if (npa->npa_path == NULL) 556 goto out; 557 558 npa->npa_version = nvme_version(fd); 559 if (npa->npa_version == NULL) 560 goto out; 561 562 npa->npa_idctl = nvme_identify_ctrl(fd); 563 if (npa->npa_idctl == NULL) 564 goto out; 565 566 npa->npa_idns = nvme_identify_nsid(fd); 567 if (npa->npa_idns == NULL) 568 goto out; 569 570 if (npa->npa_isns) { 571 npa->npa_ignored = nvme_is_ignored_ns(fd); 572 if (!npa->npa_ignored) 573 npa->npa_dsk = nvme_dskname(npa); 574 } 575 576 577 exitcode += npa->npa_cmd->c_func(fd, npa); 578 579 out: 580 di_devfs_path_free(npa->npa_path); 581 free(npa->npa_version); 582 free(npa->npa_idctl); 583 free(npa->npa_idns); 584 free(npa->npa_dsk); 585 586 npa->npa_version = NULL; 587 npa->npa_idctl = NULL; 588 npa->npa_idns = NULL; 589 npa->npa_dsk = NULL; 590 591 nvme_close(fd); 592 593 return (DI_WALK_CONTINUE); 594 } 595 596 static void 597 nvme_walk(nvme_process_arg_t *npa, di_node_t node) 598 { 599 char *minor_nodetype = DDI_NT_NVME_NEXUS; 600 601 if (npa->npa_isns) 602 minor_nodetype = DDI_NT_NVME_ATTACHMENT_POINT; 603 604 (void) di_walk_minor(node, minor_nodetype, 0, npa, nvme_process); 605 } 606 607 static void 608 usage_list(const char *c_name) 609 { 610 (void) fprintf(stderr, "%s " 611 "[-p -o field[,...]] [<ctl>[/<ns>][,...]\n\n" 612 " List NVMe controllers and their namespaces. If no " 613 "controllers and/or name-\n spaces are specified, all " 614 "controllers and namespaces in the system will be\n " 615 "listed.\n", c_name); 616 } 617 618 static void 619 optparse_list(nvme_process_arg_t *npa) 620 { 621 int c; 622 uint_t oflags = 0; 623 boolean_t parse = B_FALSE; 624 const char *fields = NULL; 625 626 optind = 0; 627 while ((c = getopt(npa->npa_argc, npa->npa_argv, ":o:p")) != -1) { 628 switch (c) { 629 case 'o': 630 fields = optarg; 631 break; 632 case 'p': 633 parse = B_TRUE; 634 oflags |= OFMT_PARSABLE; 635 break; 636 case '?': 637 errx(-1, "unknown list option: -%c", optopt); 638 break; 639 case ':': 640 errx(-1, "option -%c requires an argument", optopt); 641 default: 642 break; 643 } 644 } 645 646 if (fields != NULL && !parse) { 647 errx(-1, "-o can only be used when in parsable mode (-p)"); 648 } 649 650 if (parse && fields == NULL) { 651 errx(-1, "parsable mode (-p) requires one to specify output " 652 "fields with -o"); 653 } 654 655 if (parse) { 656 ofmt_status_t oferr; 657 658 oferr = ofmt_open(fields, nvme_list_ofmt, oflags, 0, 659 &npa->npa_ofmt); 660 ofmt_check(oferr, B_TRUE, npa->npa_ofmt, nvme_oferr, warnx); 661 } 662 663 npa->npa_argc -= optind; 664 npa->npa_argv += optind; 665 } 666 667 static int 668 do_list_nsid(int fd, const nvme_process_arg_t *npa) 669 { 670 _NOTE(ARGUNUSED(fd)); 671 const uint_t format = npa->npa_idns->id_flbas.lba_format; 672 const uint_t bshift = npa->npa_idns->id_lbaf[format].lbaf_lbads; 673 674 /* 675 * Some devices have extra namespaces with illegal block sizes and 676 * zero blocks. Don't list them when verbose operation isn't requested. 677 */ 678 if ((bshift < 9 || npa->npa_idns->id_nsize == 0) && verbose == 0) 679 return (0); 680 681 if (npa->npa_ofmt == NULL) { 682 (void) printf(" %s/%s (%s): ", npa->npa_name, 683 di_minor_name(npa->npa_minor), 684 npa->npa_dsk != NULL ? npa->npa_dsk : "unattached"); 685 nvme_print_nsid_summary(npa->npa_idns); 686 } else { 687 ofmt_print(npa->npa_ofmt, (void *)npa); 688 } 689 690 return (0); 691 } 692 693 static int 694 do_list(int fd, const nvme_process_arg_t *npa) 695 { 696 _NOTE(ARGUNUSED(fd)); 697 698 nvme_process_arg_t ns_npa = { 0 }; 699 nvmeadm_cmd_t cmd = { 0 }; 700 char *name; 701 702 if (asprintf(&name, "%s%d", di_driver_name(npa->npa_node), 703 di_instance(npa->npa_node)) < 0) 704 err(-1, "do_list()"); 705 706 if (npa->npa_ofmt == NULL) { 707 (void) printf("%s: ", name); 708 nvme_print_ctrl_summary(npa->npa_idctl, npa->npa_version); 709 } 710 711 ns_npa.npa_name = name; 712 ns_npa.npa_isns = B_TRUE; 713 ns_npa.npa_nsid = npa->npa_nsid; 714 cmd = *(npa->npa_cmd); 715 cmd.c_func = do_list_nsid; 716 ns_npa.npa_cmd = &cmd; 717 ns_npa.npa_ofmt = npa->npa_ofmt; 718 ns_npa.npa_idctl = npa->npa_idctl; 719 720 nvme_walk(&ns_npa, npa->npa_node); 721 722 free(name); 723 724 return (exitcode); 725 } 726 727 static void 728 usage_identify(const char *c_name) 729 { 730 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...]\n\n" 731 " Print detailed information about the specified NVMe " 732 "controllers and/or name-\n spaces.\n", c_name); 733 } 734 735 static int 736 do_identify(int fd, const nvme_process_arg_t *npa) 737 { 738 if (!npa->npa_isns) { 739 nvme_capabilities_t *cap; 740 741 cap = nvme_capabilities(fd); 742 if (cap == NULL) 743 return (-1); 744 745 (void) printf("%s: ", npa->npa_name); 746 nvme_print_identify_ctrl(npa->npa_idctl, cap, 747 npa->npa_version); 748 749 free(cap); 750 } else { 751 (void) printf("%s/%s: ", npa->npa_name, 752 di_minor_name(npa->npa_minor)); 753 nvme_print_identify_nsid(npa->npa_idns, 754 npa->npa_version); 755 } 756 757 return (0); 758 } 759 760 static void 761 usage_get_logpage(const char *c_name) 762 { 763 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...] <logpage>\n\n" 764 " Print the specified log page of the specified NVMe " 765 "controllers and/or name-\n spaces. Supported log pages " 766 "are error, health, and firmware.\n", c_name); 767 } 768 769 static void 770 usage_firmware_list(const char *c_name) 771 { 772 (void) fprintf(stderr, "%s <ctl>\n\n" 773 " Print the log page that contains the list of firmware " 774 "images installed on the specified NVMe controller.\n", c_name); 775 } 776 777 static int 778 do_get_logpage_error(int fd, const nvme_process_arg_t *npa) 779 { 780 int nlog = npa->npa_idctl->id_elpe + 1; 781 size_t bufsize = sizeof (nvme_error_log_entry_t) * nlog; 782 nvme_error_log_entry_t *elog; 783 784 if (npa->npa_isns) 785 errx(-1, "Error Log not available on a per-namespace basis"); 786 787 elog = nvme_get_logpage(fd, NVME_LOGPAGE_ERROR, &bufsize); 788 789 if (elog == NULL) 790 return (-1); 791 792 nlog = bufsize / sizeof (nvme_error_log_entry_t); 793 794 (void) printf("%s: ", npa->npa_name); 795 nvme_print_error_log(nlog, elog, npa->npa_version); 796 797 free(elog); 798 799 return (0); 800 } 801 802 static int 803 do_get_logpage_health(int fd, const nvme_process_arg_t *npa) 804 { 805 size_t bufsize = sizeof (nvme_health_log_t); 806 nvme_health_log_t *hlog; 807 808 if (npa->npa_isns) { 809 if (npa->npa_idctl->id_lpa.lp_smart == 0) 810 errx(-1, "SMART/Health information not available " 811 "on a per-namespace basis on this controller"); 812 } 813 814 hlog = nvme_get_logpage(fd, NVME_LOGPAGE_HEALTH, &bufsize); 815 816 if (hlog == NULL) 817 return (-1); 818 819 (void) printf("%s: ", npa->npa_name); 820 nvme_print_health_log(hlog, npa->npa_idctl, npa->npa_version); 821 822 free(hlog); 823 824 return (0); 825 } 826 827 static int 828 do_get_logpage_fwslot(int fd, const nvme_process_arg_t *npa) 829 { 830 size_t bufsize = sizeof (nvme_fwslot_log_t); 831 nvme_fwslot_log_t *fwlog; 832 833 if (npa->npa_isns) 834 errx(-1, "Firmware Slot information not available on a " 835 "per-namespace basis"); 836 837 fwlog = nvme_get_logpage(fd, NVME_LOGPAGE_FWSLOT, &bufsize); 838 839 if (fwlog == NULL) 840 return (-1); 841 842 (void) printf("%s: ", npa->npa_name); 843 nvme_print_fwslot_log(fwlog, npa->npa_idctl); 844 845 free(fwlog); 846 847 return (0); 848 } 849 850 static int 851 do_get_logpage(int fd, const nvme_process_arg_t *npa) 852 { 853 int ret = 0; 854 int (*func)(int, const nvme_process_arg_t *); 855 856 if (npa->npa_argc < 1) { 857 warnx("missing logpage name"); 858 usage(npa->npa_cmd); 859 exit(-1); 860 } 861 862 if (strcmp(npa->npa_argv[0], "error") == 0) 863 func = do_get_logpage_error; 864 else if (strcmp(npa->npa_argv[0], "health") == 0) 865 func = do_get_logpage_health; 866 else if (strcmp(npa->npa_argv[0], "firmware") == 0) 867 func = do_get_logpage_fwslot; 868 else 869 errx(-1, "invalid log page: %s", npa->npa_argv[0]); 870 871 ret = func(fd, npa); 872 return (ret); 873 } 874 875 static void 876 usage_get_features(const char *c_name) 877 { 878 const nvme_feature_t *feat; 879 880 (void) fprintf(stderr, "%s <ctl>[/<ns>][,...] [<feature>[,...]]\n\n" 881 " Print the specified features of the specified NVMe controllers " 882 "and/or\n namespaces. Supported features are:\n\n", c_name); 883 (void) fprintf(stderr, " %-35s %-14s %s\n", 884 "FEATURE NAME", "SHORT NAME", "CONTROLLER/NAMESPACE"); 885 for (feat = &features[0]; feat->f_feature != 0; feat++) { 886 char *type; 887 888 if ((feat->f_getflags & NVMEADM_F_BOTH) == NVMEADM_F_BOTH) 889 type = "both"; 890 else if ((feat->f_getflags & NVMEADM_F_CTRL) != 0) 891 type = "controller only"; 892 else 893 type = "namespace only"; 894 895 (void) fprintf(stderr, " %-35s %-14s %s\n", 896 feat->f_name, feat->f_short, type); 897 } 898 899 } 900 901 static int 902 do_get_feat_common(int fd, const nvme_feature_t *feat, 903 const nvme_process_arg_t *npa) 904 { 905 void *buf = NULL; 906 size_t bufsize = feat->f_bufsize; 907 uint64_t res; 908 909 if (nvme_get_feature(fd, feat->f_feature, 0, &res, &bufsize, &buf) 910 == B_FALSE) 911 return (EINVAL); 912 913 nvme_print(2, feat->f_name, -1, NULL); 914 feat->f_print(res, buf, bufsize, npa->npa_idctl, npa->npa_version); 915 free(buf); 916 917 return (0); 918 } 919 920 static int 921 do_get_feat_temp_thresh_one(int fd, const nvme_feature_t *feat, 922 const char *label, uint16_t tmpsel, uint16_t thsel, 923 const nvme_process_arg_t *npa) 924 { 925 uint64_t res; 926 void *buf = NULL; 927 size_t bufsize = feat->f_bufsize; 928 nvme_temp_threshold_t tt; 929 930 tt.r = 0; 931 tt.b.tt_tmpsel = tmpsel; 932 tt.b.tt_thsel = thsel; 933 934 if (!nvme_get_feature(fd, feat->f_feature, tt.r, &res, &bufsize, 935 &buf)) { 936 return (EINVAL); 937 } 938 939 feat->f_print(res, (void *)label, 0, npa->npa_idctl, npa->npa_version); 940 free(buf); 941 return (0); 942 } 943 944 /* 945 * In NVMe 1.2, the specification allowed for up to 8 sensors to be on the 946 * device and changed the main device to have a composite temperature sensor. As 947 * a result, there is a set of thresholds for each sensor. In addition, they 948 * added both an over-temperature and under-temperature threshold. Since most 949 * devices don't actually implement all the sensors, we get the health page and 950 * see which sensors have a non-zero value to determine how to proceed. 951 */ 952 static int 953 do_get_feat_temp_thresh(int fd, const nvme_feature_t *feat, 954 const nvme_process_arg_t *npa) 955 { 956 int ret; 957 size_t bufsize = sizeof (nvme_health_log_t); 958 nvme_health_log_t *hlog; 959 960 nvme_print(2, feat->f_name, -1, NULL); 961 if ((ret = do_get_feat_temp_thresh_one(fd, feat, 962 "Composite Over Temp. Threshold", 0, NVME_TEMP_THRESH_OVER, 963 npa)) != 0) { 964 return (ret); 965 } 966 967 if (!nvme_version_check(npa->npa_version, 1, 2)) { 968 return (0); 969 } 970 971 if ((ret = do_get_feat_temp_thresh_one(fd, feat, 972 "Composite Under Temp. Threshold", 0, NVME_TEMP_THRESH_UNDER, 973 npa)) != 0) { 974 return (ret); 975 } 976 977 hlog = nvme_get_logpage(fd, NVME_LOGPAGE_HEALTH, &bufsize); 978 if (hlog == NULL) { 979 warnx("failed to get health log page, unable to get " 980 "thresholds for additional sensors"); 981 return (0); 982 } 983 984 if (hlog->hl_temp_sensor_1 != 0) { 985 (void) do_get_feat_temp_thresh_one(fd, feat, 986 "Temp. Sensor 1 Over Temp. Threshold", 1, 987 NVME_TEMP_THRESH_OVER, npa); 988 (void) do_get_feat_temp_thresh_one(fd, feat, 989 "Temp. Sensor 1 Under Temp. Threshold", 1, 990 NVME_TEMP_THRESH_UNDER, npa); 991 } 992 993 if (hlog->hl_temp_sensor_2 != 0) { 994 (void) do_get_feat_temp_thresh_one(fd, feat, 995 "Temp. Sensor 2 Over Temp. Threshold", 2, 996 NVME_TEMP_THRESH_OVER, npa); 997 (void) do_get_feat_temp_thresh_one(fd, feat, 998 "Temp. Sensor 2 Under Temp. Threshold", 2, 999 NVME_TEMP_THRESH_UNDER, npa); 1000 } 1001 1002 if (hlog->hl_temp_sensor_3 != 0) { 1003 (void) do_get_feat_temp_thresh_one(fd, feat, 1004 "Temp. Sensor 3 Over Temp. Threshold", 3, 1005 NVME_TEMP_THRESH_OVER, npa); 1006 (void) do_get_feat_temp_thresh_one(fd, feat, 1007 "Temp. Sensor 3 Under Temp. Threshold", 3, 1008 NVME_TEMP_THRESH_UNDER, npa); 1009 } 1010 1011 if (hlog->hl_temp_sensor_4 != 0) { 1012 (void) do_get_feat_temp_thresh_one(fd, feat, 1013 "Temp. Sensor 4 Over Temp. Threshold", 4, 1014 NVME_TEMP_THRESH_OVER, npa); 1015 (void) do_get_feat_temp_thresh_one(fd, feat, 1016 "Temp. Sensor 4 Under Temp. Threshold", 4, 1017 NVME_TEMP_THRESH_UNDER, npa); 1018 } 1019 1020 if (hlog->hl_temp_sensor_5 != 0) { 1021 (void) do_get_feat_temp_thresh_one(fd, feat, 1022 "Temp. Sensor 5 Over Temp. Threshold", 5, 1023 NVME_TEMP_THRESH_OVER, npa); 1024 (void) do_get_feat_temp_thresh_one(fd, feat, 1025 "Temp. Sensor 5 Under Temp. Threshold", 5, 1026 NVME_TEMP_THRESH_UNDER, npa); 1027 } 1028 1029 if (hlog->hl_temp_sensor_6 != 0) { 1030 (void) do_get_feat_temp_thresh_one(fd, feat, 1031 "Temp. Sensor 6 Over Temp. Threshold", 6, 1032 NVME_TEMP_THRESH_OVER, npa); 1033 (void) do_get_feat_temp_thresh_one(fd, feat, 1034 "Temp. Sensor 6 Under Temp. Threshold", 6, 1035 NVME_TEMP_THRESH_UNDER, npa); 1036 } 1037 1038 if (hlog->hl_temp_sensor_7 != 0) { 1039 (void) do_get_feat_temp_thresh_one(fd, feat, 1040 "Temp. Sensor 7 Over Temp. Threshold", 7, 1041 NVME_TEMP_THRESH_OVER, npa); 1042 (void) do_get_feat_temp_thresh_one(fd, feat, 1043 "Temp. Sensor 7 Under Temp. Threshold", 7, 1044 NVME_TEMP_THRESH_UNDER, npa); 1045 } 1046 1047 if (hlog->hl_temp_sensor_8 != 0) { 1048 (void) do_get_feat_temp_thresh_one(fd, feat, 1049 "Temp. Sensor 8 Over Temp. Threshold", 8, 1050 NVME_TEMP_THRESH_OVER, npa); 1051 (void) do_get_feat_temp_thresh_one(fd, feat, 1052 "Temp. Sensor 8 Under Temp. Threshold", 8, 1053 NVME_TEMP_THRESH_UNDER, npa); 1054 } 1055 free(hlog); 1056 return (0); 1057 } 1058 1059 static int 1060 do_get_feat_intr_vect(int fd, const nvme_feature_t *feat, 1061 const nvme_process_arg_t *npa) 1062 { 1063 uint64_t res; 1064 uint64_t arg; 1065 int intr_cnt; 1066 1067 intr_cnt = nvme_intr_cnt(fd); 1068 1069 if (intr_cnt == -1) 1070 return (EINVAL); 1071 1072 nvme_print(2, feat->f_name, -1, NULL); 1073 1074 for (arg = 0; arg < intr_cnt; arg++) { 1075 if (nvme_get_feature(fd, feat->f_feature, arg, &res, NULL, NULL) 1076 == B_FALSE) 1077 return (EINVAL); 1078 1079 feat->f_print(res, NULL, 0, npa->npa_idctl, npa->npa_version); 1080 } 1081 1082 return (0); 1083 } 1084 1085 static int 1086 do_get_features(int fd, const nvme_process_arg_t *npa) 1087 { 1088 const nvme_feature_t *feat; 1089 char *f, *flist, *lasts; 1090 boolean_t header_printed = B_FALSE; 1091 1092 if (npa->npa_argc > 1) 1093 errx(-1, "unexpected arguments"); 1094 1095 /* 1096 * No feature list given, print all supported features. 1097 */ 1098 if (npa->npa_argc == 0) { 1099 (void) printf("%s: Get Features\n", npa->npa_name); 1100 for (feat = &features[0]; feat->f_feature != 0; feat++) { 1101 if ((npa->npa_isns && 1102 (feat->f_getflags & NVMEADM_F_NS) == 0) || 1103 (!npa->npa_isns && 1104 (feat->f_getflags & NVMEADM_F_CTRL) == 0)) 1105 continue; 1106 1107 (void) feat->f_get(fd, feat, npa); 1108 } 1109 1110 return (0); 1111 } 1112 1113 /* 1114 * Process feature list. 1115 */ 1116 flist = strdup(npa->npa_argv[0]); 1117 if (flist == NULL) 1118 err(-1, "do_get_features"); 1119 1120 for (f = strtok_r(flist, ",", &lasts); 1121 f != NULL; 1122 f = strtok_r(NULL, ",", &lasts)) { 1123 while (isspace(*f)) 1124 f++; 1125 1126 for (feat = &features[0]; feat->f_feature != 0; feat++) { 1127 if (strncasecmp(feat->f_name, f, strlen(f)) == 0 || 1128 strncasecmp(feat->f_short, f, strlen(f)) == 0) 1129 break; 1130 } 1131 1132 if (feat->f_feature == 0) { 1133 warnx("unknown feature %s", f); 1134 continue; 1135 } 1136 1137 if ((npa->npa_isns && 1138 (feat->f_getflags & NVMEADM_F_NS) == 0) || 1139 (!npa->npa_isns && 1140 (feat->f_getflags & NVMEADM_F_CTRL) == 0)) { 1141 warnx("feature %s %s supported for namespaces", 1142 feat->f_name, 1143 (feat->f_getflags & NVMEADM_F_NS) != 0 ? 1144 "only" : "not"); 1145 continue; 1146 } 1147 1148 if (!header_printed) { 1149 (void) printf("%s: Get Features\n", npa->npa_name); 1150 header_printed = B_TRUE; 1151 } 1152 1153 if (feat->f_get(fd, feat, npa) != 0) { 1154 warnx("unsupported feature: %s", feat->f_name); 1155 continue; 1156 } 1157 } 1158 1159 free(flist); 1160 return (0); 1161 } 1162 1163 static int 1164 do_format_common(int fd, const nvme_process_arg_t *npa, unsigned long lbaf, 1165 unsigned long ses) 1166 { 1167 nvme_process_arg_t ns_npa = { 0 }; 1168 nvmeadm_cmd_t cmd = { 0 }; 1169 1170 cmd = *(npa->npa_cmd); 1171 cmd.c_func = do_attach_detach; 1172 cmd.c_name = "detach"; 1173 ns_npa = *npa; 1174 ns_npa.npa_cmd = &cmd; 1175 1176 if (do_attach_detach(fd, &ns_npa) != 0) 1177 return (exitcode); 1178 if (nvme_format_nvm(fd, lbaf, ses) == B_FALSE) { 1179 warn("%s failed", npa->npa_cmd->c_name); 1180 exitcode += -1; 1181 } 1182 cmd.c_name = "attach"; 1183 exitcode += do_attach_detach(fd, &ns_npa); 1184 1185 return (exitcode); 1186 } 1187 1188 static void 1189 usage_format(const char *c_name) 1190 { 1191 (void) fprintf(stderr, "%s <ctl>[/<ns>] [<lba-format>]\n\n" 1192 " Format one or all namespaces of the specified NVMe " 1193 "controller. Supported LBA\n formats can be queried with " 1194 "the \"%s identify\" command on the namespace\n to be " 1195 "formatted.\n", c_name, getprogname()); 1196 } 1197 1198 static int 1199 do_format(int fd, const nvme_process_arg_t *npa) 1200 { 1201 unsigned long lbaf; 1202 1203 if (npa->npa_idctl->id_oacs.oa_format == 0) 1204 errx(-1, "%s not supported", npa->npa_cmd->c_name); 1205 1206 if (npa->npa_isns && npa->npa_idctl->id_fna.fn_format != 0) 1207 errx(-1, "%s not supported on individual namespace", 1208 npa->npa_cmd->c_name); 1209 1210 1211 if (npa->npa_argc > 0) { 1212 errno = 0; 1213 lbaf = strtoul(npa->npa_argv[0], NULL, 10); 1214 1215 if (errno != 0 || lbaf > NVME_FRMT_MAX_LBAF) 1216 errx(-1, "invalid LBA format %d", lbaf + 1); 1217 1218 if (npa->npa_idns->id_lbaf[lbaf].lbaf_ms != 0) 1219 errx(-1, "LBA formats with metadata not supported"); 1220 } else { 1221 lbaf = npa->npa_idns->id_flbas.lba_format; 1222 } 1223 1224 return (do_format_common(fd, npa, lbaf, 0)); 1225 } 1226 1227 static void 1228 usage_secure_erase(const char *c_name) 1229 { 1230 (void) fprintf(stderr, "%s [-c] <ctl>[/<ns>]\n\n" 1231 " Secure-Erase one or all namespaces of the specified " 1232 "NVMe controller.\n", c_name); 1233 } 1234 1235 static void 1236 optparse_secure_erase(nvme_process_arg_t *npa) 1237 { 1238 int c; 1239 1240 optind = 0; 1241 while ((c = getopt(npa->npa_argc, npa->npa_argv, ":c")) != -1) { 1242 switch (c) { 1243 case 'c': 1244 npa->npa_cmdflags |= NVMEADM_O_SE_CRYPTO; 1245 break; 1246 case '?': 1247 errx(-1, "unknown secure-erase option: -%c", optopt); 1248 break; 1249 } 1250 } 1251 1252 npa->npa_argc -= optind; 1253 npa->npa_argv += optind; 1254 } 1255 1256 static int 1257 do_secure_erase(int fd, const nvme_process_arg_t *npa) 1258 { 1259 unsigned long lbaf; 1260 uint8_t ses = NVME_FRMT_SES_USER; 1261 1262 if (npa->npa_argc > 0) 1263 errx(-1, "Too many arguments"); 1264 1265 if (npa->npa_idctl->id_oacs.oa_format == 0) 1266 errx(-1, "%s not supported", npa->npa_cmd->c_name); 1267 1268 if (npa->npa_isns && npa->npa_idctl->id_fna.fn_sec_erase != 0) 1269 errx(-1, "%s not supported on individual namespace", 1270 npa->npa_cmd->c_name); 1271 1272 if ((npa->npa_cmdflags & NVMEADM_O_SE_CRYPTO) != 0) 1273 ses = NVME_FRMT_SES_CRYPTO; 1274 1275 if (ses == NVME_FRMT_SES_CRYPTO && 1276 npa->npa_idctl->id_fna.fn_crypt_erase == 0) 1277 errx(-1, "cryptographic %s not supported", 1278 npa->npa_cmd->c_name); 1279 1280 lbaf = npa->npa_idns->id_flbas.lba_format; 1281 1282 return (do_format_common(fd, npa, lbaf, ses)); 1283 } 1284 1285 static void 1286 usage_attach_detach(const char *c_name) 1287 { 1288 (void) fprintf(stderr, "%s <ctl>[/<ns>]\n\n" 1289 " %c%s blkdev(4D) %s one or all namespaces of the " 1290 "specified NVMe controller.\n", 1291 c_name, toupper(c_name[0]), &c_name[1], 1292 c_name[0] == 'd' ? "from" : "to"); 1293 } 1294 1295 static int 1296 do_attach_detach(int fd, const nvme_process_arg_t *npa) 1297 { 1298 char *c_name = npa->npa_cmd->c_name; 1299 1300 if (!npa->npa_isns) { 1301 nvme_process_arg_t ns_npa = { 0 }; 1302 1303 ns_npa.npa_name = npa->npa_name; 1304 ns_npa.npa_isns = B_TRUE; 1305 ns_npa.npa_cmd = npa->npa_cmd; 1306 ns_npa.npa_excl = npa->npa_excl; 1307 1308 nvme_walk(&ns_npa, npa->npa_node); 1309 1310 return (exitcode); 1311 } 1312 1313 /* 1314 * Unless the user interactively requested a particular namespace to be 1315 * attached or detached, don't even try to attach or detach namespaces 1316 * that are ignored by the driver, thereby avoiding printing pointless 1317 * error messages. 1318 */ 1319 if (!npa->npa_interactive && npa->npa_ignored) 1320 return (0); 1321 1322 if ((c_name[0] == 'd' ? nvme_detach : nvme_attach)(fd) 1323 == B_FALSE) { 1324 warn("%s failed", c_name); 1325 return (-1); 1326 } 1327 1328 return (0); 1329 } 1330 1331 static void 1332 usage_firmware_load(const char *c_name) 1333 { 1334 (void) fprintf(stderr, "%s <ctl> <image file> [<offset>]\n\n" 1335 " Load firmware <image file> to offset <offset>.\n" 1336 " The firmware needs to be committed to a slot using " 1337 "\"nvmeadm commit-firmware\"\n command.\n", c_name); 1338 } 1339 1340 /* 1341 * Read exactly len bytes, or until eof. 1342 */ 1343 static ssize_t 1344 read_block(int fd, char *buf, size_t len) 1345 { 1346 size_t remain; 1347 ssize_t bytes; 1348 1349 remain = len; 1350 while (remain > 0) { 1351 bytes = read(fd, buf, remain); 1352 if (bytes == 0) 1353 break; 1354 1355 if (bytes < 0) { 1356 if (errno == EINTR) 1357 continue; 1358 1359 return (-1); 1360 } 1361 1362 buf += bytes; 1363 remain -= bytes; 1364 } 1365 1366 return (len - remain); 1367 } 1368 1369 /* 1370 * Convert a string to a valid firmware upload offset (in bytes). 1371 */ 1372 static offset_t 1373 get_fw_offsetb(char *str) 1374 { 1375 longlong_t offsetb; 1376 char *valend; 1377 1378 errno = 0; 1379 offsetb = strtoll(str, &valend, 0); 1380 if (errno != 0 || *valend != '\0' || offsetb < 0 || 1381 offsetb > NVME_FW_OFFSETB_MAX) 1382 errx(-1, "Offset must be numeric and in the range of 0 to %llu", 1383 NVME_FW_OFFSETB_MAX); 1384 1385 if ((offsetb & NVME_DWORD_MASK) != 0) 1386 errx(-1, "Offset must be multiple of %d", NVME_DWORD_SIZE); 1387 1388 return ((offset_t)offsetb); 1389 } 1390 1391 #define FIRMWARE_READ_BLKSIZE (64 * 1024) /* 64K */ 1392 1393 static int 1394 do_firmware_load(int fd, const nvme_process_arg_t *npa) 1395 { 1396 int fw_fd; 1397 ssize_t len; 1398 offset_t offset = 0; 1399 size_t size; 1400 uint16_t sc; 1401 char buf[FIRMWARE_READ_BLKSIZE]; 1402 1403 if (npa->npa_argc > 2) 1404 errx(-1, "Too many arguments"); 1405 1406 if (npa->npa_argc == 0) 1407 errx(-1, "Requires firmware file name, and an " 1408 "optional offset"); 1409 1410 if (npa->npa_isns) 1411 errx(-1, "Firmware loading not available on a per-namespace " 1412 "basis"); 1413 1414 if (npa->npa_argc == 2) 1415 offset = get_fw_offsetb(npa->npa_argv[1]); 1416 1417 fw_fd = open(npa->npa_argv[0], O_RDONLY); 1418 if (fw_fd < 0) 1419 errx(-1, "Failed to open \"%s\": %s", npa->npa_argv[0], 1420 strerror(errno)); 1421 1422 size = 0; 1423 do { 1424 len = read_block(fw_fd, buf, sizeof (buf)); 1425 1426 if (len < 0) 1427 errx(-1, "Error reading \"%s\": %s", npa->npa_argv[0], 1428 strerror(errno)); 1429 1430 if (len == 0) 1431 break; 1432 1433 if (!nvme_firmware_load(fd, buf, len, offset, &sc)) 1434 errx(-1, "Error loading \"%s\": %s", npa->npa_argv[0], 1435 nvme_fw_error(errno, sc)); 1436 1437 offset += len; 1438 size += len; 1439 } while (len == sizeof (buf)); 1440 1441 (void) close(fw_fd); 1442 1443 if (verbose) 1444 (void) printf("%zu bytes downloaded.\n", size); 1445 1446 return (0); 1447 } 1448 1449 /* 1450 * Convert str to a valid firmware slot number. 1451 */ 1452 static uint_t 1453 get_slot_number(char *str) 1454 { 1455 longlong_t slot; 1456 char *valend; 1457 1458 errno = 0; 1459 slot = strtoll(str, &valend, 0); 1460 if (errno != 0 || *valend != '\0' || 1461 slot < NVME_FW_SLOT_MIN || slot > NVME_FW_SLOT_MAX) 1462 errx(-1, "Slot must be numeric and in the range of %d to %d", 1463 NVME_FW_SLOT_MIN, NVME_FW_SLOT_MAX); 1464 1465 return ((uint_t)slot); 1466 } 1467 1468 static void 1469 usage_firmware_commit(const char *c_name) 1470 { 1471 (void) fprintf(stderr, "%s <ctl> <slot>\n\n" 1472 " Commit previously downloaded firmware to slot <slot>.\n" 1473 " The firmware is only activated after a " 1474 "\"nvmeadm activate-firmware\" command.\n", c_name); 1475 } 1476 1477 static int 1478 do_firmware_commit(int fd, const nvme_process_arg_t *npa) 1479 { 1480 uint_t slot; 1481 uint16_t sc; 1482 1483 if (npa->npa_argc > 1) 1484 errx(-1, "Too many arguments"); 1485 1486 if (npa->npa_argc == 0) 1487 errx(-1, "Firmware slot number is required"); 1488 1489 if (npa->npa_isns) 1490 errx(-1, "Firmware committing not available on a per-namespace " 1491 "basis"); 1492 1493 slot = get_slot_number(npa->npa_argv[0]); 1494 1495 if (slot == 1 && npa->npa_idctl->id_frmw.fw_readonly) 1496 errx(-1, "Cannot commit firmware to slot 1: slot is read-only"); 1497 1498 if (!nvme_firmware_commit(fd, slot, NVME_FWC_SAVE, &sc)) 1499 errx(-1, "Failed to commit firmware to slot %u: %s", 1500 slot, nvme_fw_error(errno, sc)); 1501 1502 if (verbose) 1503 (void) printf("Firmware committed to slot %u.\n", slot); 1504 1505 return (0); 1506 } 1507 1508 static void 1509 usage_firmware_activate(const char *c_name) 1510 { 1511 (void) fprintf(stderr, "%s <ctl> <slot>\n\n" 1512 " Activate firmware in slot <slot>.\n" 1513 " The firmware will be in use after the next system reset.\n", 1514 c_name); 1515 } 1516 1517 static int 1518 do_firmware_activate(int fd, const nvme_process_arg_t *npa) 1519 { 1520 uint_t slot; 1521 uint16_t sc; 1522 1523 if (npa->npa_argc > 1) 1524 errx(-1, "Too many arguments"); 1525 1526 if (npa->npa_argc == 0) 1527 errx(-1, "Firmware slot number is required"); 1528 1529 if (npa->npa_isns) 1530 errx(-1, "Firmware activation not available on a per-namespace " 1531 "basis"); 1532 1533 slot = get_slot_number(npa->npa_argv[0]); 1534 1535 if (!nvme_firmware_commit(fd, slot, NVME_FWC_ACTIVATE, &sc)) 1536 errx(-1, "Failed to activate slot %u: %s", slot, 1537 nvme_fw_error(errno, sc)); 1538 1539 if (verbose) 1540 printf("Slot %u activated: %s.\n", slot, 1541 nvme_fw_error(errno, sc)); 1542 1543 return (0); 1544 } 1545 1546 /* 1547 * While the NVME_VERSION_ATLEAST macro exists, specifying a version of 1.0 1548 * causes GCC to helpfully flag the -Wtype-limits warning because a uint_t is 1549 * always >= 0. In many cases it's useful to always indicate what version 1550 * something was added in to simplify code (e.g. nvmeadm_print_bit) and we'd 1551 * rather just say it's version 1.0 rather than making folks realize that a 1552 * hardcoded true is equivalent. Therefore we have this function which can't 1553 * trigger this warning today (and adds a minor amount of type safety). If GCC 1554 * or clang get smart enough to see through this, then we'll have to just 1555 * disable the warning for the single minor comparison (and reformat this a bit 1556 * to minimize the impact). 1557 */ 1558 boolean_t 1559 nvme_version_check(nvme_version_t *vers, uint_t major, uint_t minor) 1560 { 1561 if (vers->v_major > major) { 1562 return (B_TRUE); 1563 } 1564 1565 return (vers->v_major == major && vers->v_minor >= minor); 1566 } 1567