1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 EMC Corp. 5 * All rights reserved. 6 * 7 * Copyright (C) 2012-2013 Intel Corporation 8 * All rights reserved. 9 * Copyright (C) 2016-2023 Warner Losh <imp@FreeBSD.org> 10 * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/ioccom.h> 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <fcntl.h> 40 #include <stdbool.h> 41 #include <stddef.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <sysexits.h> 46 #include <unistd.h> 47 #include <sys/endian.h> 48 49 #include "nvmecontrol.h" 50 51 /* Tables for command line parsing */ 52 53 static cmd_fn_t logpage; 54 55 #define NONE 0xffffffffu 56 static struct options { 57 bool binary; 58 bool hex; 59 uint32_t page; 60 uint8_t lsp; 61 uint16_t lsi; 62 bool rae; 63 const char *vendor; 64 const char *dev; 65 } opt = { 66 .binary = false, 67 .hex = false, 68 .page = NONE, 69 .lsp = 0, 70 .lsi = 0, 71 .rae = false, 72 .vendor = NULL, 73 .dev = NULL, 74 }; 75 76 static const struct opts logpage_opts[] = { 77 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 78 OPT("binary", 'b', arg_none, opt, binary, 79 "Dump the log page as binary"), 80 OPT("hex", 'x', arg_none, opt, hex, 81 "Dump the log page as hex"), 82 OPT("page", 'p', arg_uint32, opt, page, 83 "Page to dump"), 84 OPT("lsp", 'f', arg_uint8, opt, lsp, 85 "Log Specific Field"), 86 OPT("lsi", 'i', arg_uint16, opt, lsi, 87 "Log Specific Identifier"), 88 OPT("rae", 'r', arg_none, opt, rae, 89 "Retain Asynchronous Event"), 90 OPT("vendor", 'v', arg_string, opt, vendor, 91 "Vendor specific formatting"), 92 { NULL, 0, arg_none, NULL, NULL } 93 }; 94 #undef OPT 95 96 static const struct args logpage_args[] = { 97 { arg_string, &opt.dev, "<controller id|namespace id>" }, 98 { arg_none, NULL, NULL }, 99 }; 100 101 static struct cmd logpage_cmd = { 102 .name = "logpage", 103 .fn = logpage, 104 .descr = "Print logpages in human-readable form", 105 .ctx_size = sizeof(opt), 106 .opts = logpage_opts, 107 .args = logpage_args, 108 }; 109 110 CMD_COMMAND(logpage_cmd); 111 112 /* End of tables for command line parsing */ 113 114 #define MAX_FW_SLOTS (7) 115 116 static SLIST_HEAD(,logpage_function) logpages; 117 118 static int 119 logpage_compare(struct logpage_function *a, struct logpage_function *b) 120 { 121 int c; 122 123 if ((a->vendor == NULL) != (b->vendor == NULL)) 124 return (a->vendor == NULL ? -1 : 1); 125 if (a->vendor != NULL) { 126 c = strcmp(a->vendor, b->vendor); 127 if (c != 0) 128 return (c); 129 } 130 return ((int)a->log_page - (int)b->log_page); 131 } 132 133 void 134 logpage_register(struct logpage_function *p) 135 { 136 struct logpage_function *l, *a; 137 138 a = NULL; 139 l = SLIST_FIRST(&logpages); 140 while (l != NULL) { 141 if (logpage_compare(l, p) > 0) 142 break; 143 a = l; 144 l = SLIST_NEXT(l, link); 145 } 146 if (a == NULL) 147 SLIST_INSERT_HEAD(&logpages, p, link); 148 else 149 SLIST_INSERT_AFTER(a, p, link); 150 } 151 152 const char * 153 kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key) 154 { 155 static char bad[32]; 156 size_t i; 157 158 for (i = 0; i < kv_count; i++, kv++) 159 if (kv->key == key) 160 return kv->name; 161 snprintf(bad, sizeof(bad), "Attribute %#x", key); 162 return bad; 163 } 164 165 static void 166 print_log_hex(const struct nvme_controller_data *cdata __unused, void *data, uint32_t length) 167 { 168 169 print_hex(data, length); 170 } 171 172 static void 173 print_bin(const struct nvme_controller_data *cdata __unused, void *data, uint32_t length) 174 { 175 176 write(STDOUT_FILENO, data, length); 177 } 178 179 static void * 180 get_log_buffer(uint32_t size) 181 { 182 void *buf; 183 184 if ((buf = malloc(size)) == NULL) 185 errx(EX_OSERR, "unable to malloc %u bytes", size); 186 187 memset(buf, 0, size); 188 return (buf); 189 } 190 191 void 192 read_logpage(int fd, uint8_t log_page, uint32_t nsid, uint8_t lsp, 193 uint16_t lsi, uint8_t rae, void *payload, uint32_t payload_size) 194 { 195 struct nvme_pt_command pt; 196 struct nvme_error_information_entry *err_entry; 197 u_int i, err_pages, numd; 198 199 numd = payload_size / sizeof(uint32_t) - 1; 200 memset(&pt, 0, sizeof(pt)); 201 pt.cmd.opc = NVME_OPC_GET_LOG_PAGE; 202 pt.cmd.nsid = htole32(nsid); 203 pt.cmd.cdw10 = htole32( 204 (numd << 16) | /* NUMDL */ 205 (rae << 15) | /* RAE */ 206 (lsp << 8) | /* LSP */ 207 log_page); /* LID */ 208 pt.cmd.cdw11 = htole32( 209 ((uint32_t)lsi << 16) | /* LSI */ 210 (numd >> 16)); /* NUMDU */ 211 pt.cmd.cdw12 = 0; /* LPOL */ 212 pt.cmd.cdw13 = 0; /* LPOU */ 213 pt.cmd.cdw14 = 0; /* UUID Index */ 214 pt.buf = payload; 215 pt.len = payload_size; 216 pt.is_read = 1; 217 218 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 219 err(EX_IOERR, "get log page request failed"); 220 221 /* Convert data to host endian */ 222 switch (log_page) { 223 case NVME_LOG_ERROR: 224 err_entry = (struct nvme_error_information_entry *)payload; 225 err_pages = payload_size / sizeof(struct nvme_error_information_entry); 226 for (i = 0; i < err_pages; i++) 227 nvme_error_information_entry_swapbytes(err_entry++); 228 break; 229 case NVME_LOG_HEALTH_INFORMATION: 230 nvme_health_information_page_swapbytes( 231 (struct nvme_health_information_page *)payload); 232 break; 233 case NVME_LOG_CHANGED_NAMESPACE: 234 nvme_ns_list_swapbytes((struct nvme_ns_list *)payload); 235 break; 236 case NVME_LOG_DEVICE_SELF_TEST: 237 nvme_device_self_test_swapbytes( 238 (struct nvme_device_self_test_page *)payload); 239 break; 240 case NVME_LOG_COMMAND_EFFECT: 241 nvme_command_effects_page_swapbytes( 242 (struct nvme_command_effects_page *)payload); 243 break; 244 case NVME_LOG_RES_NOTIFICATION: 245 nvme_res_notification_page_swapbytes( 246 (struct nvme_res_notification_page *)payload); 247 break; 248 case NVME_LOG_SANITIZE_STATUS: 249 nvme_sanitize_status_page_swapbytes( 250 (struct nvme_sanitize_status_page *)payload); 251 break; 252 case INTEL_LOG_TEMP_STATS: 253 intel_log_temp_stats_swapbytes( 254 (struct intel_log_temp_stats *)payload); 255 break; 256 default: 257 break; 258 } 259 260 if (nvme_completion_is_error(&pt.cpl)) 261 errx(EX_IOERR, "get log page request returned error"); 262 } 263 264 static void 265 print_log_error(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size) 266 { 267 int i, nentries; 268 uint16_t status; 269 uint8_t p, sc, sct, m, dnr; 270 struct nvme_error_information_entry *entry = buf; 271 272 printf("Error Information Log\n"); 273 printf("=====================\n"); 274 275 if (entry->error_count == 0) { 276 printf("No error entries found\n"); 277 return; 278 } 279 280 nentries = size/sizeof(struct nvme_error_information_entry); 281 for (i = 0; i < nentries; i++, entry++) { 282 if (entry->error_count == 0) 283 break; 284 285 status = entry->status; 286 287 p = NVME_STATUS_GET_P(status); 288 sc = NVME_STATUS_GET_SC(status); 289 sct = NVME_STATUS_GET_SCT(status); 290 m = NVME_STATUS_GET_M(status); 291 dnr = NVME_STATUS_GET_DNR(status); 292 293 printf("Entry %02d\n", i + 1); 294 printf("=========\n"); 295 printf(" Error count: %ju\n", entry->error_count); 296 printf(" Submission queue ID: %u\n", entry->sqid); 297 printf(" Command ID: %u\n", entry->cid); 298 /* TODO: Export nvme_status_string structures from kernel? */ 299 printf(" Status:\n"); 300 printf(" Phase tag: %d\n", p); 301 printf(" Status code: %d\n", sc); 302 printf(" Status code type: %d\n", sct); 303 printf(" More: %d\n", m); 304 printf(" DNR: %d\n", dnr); 305 printf(" Error location: %u\n", entry->error_location); 306 printf(" LBA: %ju\n", entry->lba); 307 printf(" Namespace ID: %u\n", entry->nsid); 308 printf(" Vendor specific info: %u\n", entry->vendor_specific); 309 printf(" Transport type: %u\n", entry->trtype); 310 printf(" Command specific info:%ju\n", entry->csi); 311 printf(" Transport specific: %u\n", entry->ttsi); 312 } 313 } 314 315 void 316 print_temp_K(uint16_t t) 317 { 318 printf("%u K, %2.2f C, %3.2f F\n", t, (float)t - 273.15, (float)t * 9 / 5 - 459.67); 319 } 320 321 void 322 print_temp_C(uint16_t t) 323 { 324 printf("%2.2f K, %u C, %3.2f F\n", (float)t + 273.15, t, (float)t * 9 / 5 + 32); 325 } 326 327 static void 328 print_log_health(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused) 329 { 330 struct nvme_health_information_page *health = buf; 331 char cbuf[UINT128_DIG + 1]; 332 uint8_t warning; 333 int i; 334 335 warning = health->critical_warning; 336 337 printf("SMART/Health Information Log\n"); 338 printf("============================\n"); 339 340 printf("Critical Warning State: 0x%02x\n", warning); 341 printf(" Available spare: %d\n", 342 !!(warning & NVME_CRIT_WARN_ST_AVAILABLE_SPARE)); 343 printf(" Temperature: %d\n", 344 !!(warning & NVME_CRIT_WARN_ST_TEMPERATURE)); 345 printf(" Device reliability: %d\n", 346 !!(warning & NVME_CRIT_WARN_ST_DEVICE_RELIABILITY)); 347 printf(" Read only: %d\n", 348 !!(warning & NVME_CRIT_WARN_ST_READ_ONLY)); 349 printf(" Volatile memory backup: %d\n", 350 !!(warning & NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP)); 351 printf("Temperature: "); 352 print_temp_K(health->temperature); 353 printf("Available spare: %u\n", 354 health->available_spare); 355 printf("Available spare threshold: %u\n", 356 health->available_spare_threshold); 357 printf("Percentage used: %u\n", 358 health->percentage_used); 359 360 printf("Data units (512,000 byte) read: %s\n", 361 uint128_to_str(to128(health->data_units_read), cbuf, sizeof(cbuf))); 362 printf("Data units written: %s\n", 363 uint128_to_str(to128(health->data_units_written), cbuf, sizeof(cbuf))); 364 printf("Host read commands: %s\n", 365 uint128_to_str(to128(health->host_read_commands), cbuf, sizeof(cbuf))); 366 printf("Host write commands: %s\n", 367 uint128_to_str(to128(health->host_write_commands), cbuf, sizeof(cbuf))); 368 printf("Controller busy time (minutes): %s\n", 369 uint128_to_str(to128(health->controller_busy_time), cbuf, sizeof(cbuf))); 370 printf("Power cycles: %s\n", 371 uint128_to_str(to128(health->power_cycles), cbuf, sizeof(cbuf))); 372 printf("Power on hours: %s\n", 373 uint128_to_str(to128(health->power_on_hours), cbuf, sizeof(cbuf))); 374 printf("Unsafe shutdowns: %s\n", 375 uint128_to_str(to128(health->unsafe_shutdowns), cbuf, sizeof(cbuf))); 376 printf("Media errors: %s\n", 377 uint128_to_str(to128(health->media_errors), cbuf, sizeof(cbuf))); 378 printf("No. error info log entries: %s\n", 379 uint128_to_str(to128(health->num_error_info_log_entries), cbuf, sizeof(cbuf))); 380 381 printf("Warning Temp Composite Time: %d\n", health->warning_temp_time); 382 printf("Error Temp Composite Time: %d\n", health->error_temp_time); 383 for (i = 0; i < 8; i++) { 384 if (health->temp_sensor[i] == 0) 385 continue; 386 printf("Temperature Sensor %d: ", i + 1); 387 print_temp_K(health->temp_sensor[i]); 388 } 389 printf("Temperature 1 Transition Count: %d\n", health->tmt1tc); 390 printf("Temperature 2 Transition Count: %d\n", health->tmt2tc); 391 printf("Total Time For Temperature 1: %d\n", health->ttftmt1); 392 printf("Total Time For Temperature 2: %d\n", health->ttftmt2); 393 } 394 395 static void 396 print_log_firmware(const struct nvme_controller_data *cdata, void *buf, uint32_t size __unused) 397 { 398 int i, slots; 399 const char *status; 400 struct nvme_firmware_page *fw = buf; 401 uint8_t afi_slot; 402 uint16_t oacs_fw; 403 uint8_t fw_num_slots; 404 405 afi_slot = NVMEV(NVME_FIRMWARE_PAGE_AFI_SLOT, fw->afi); 406 407 oacs_fw = NVMEV(NVME_CTRLR_DATA_OACS_FIRMWARE, cdata->oacs); 408 fw_num_slots = NVMEV(NVME_CTRLR_DATA_FRMW_NUM_SLOTS, cdata->frmw); 409 410 printf("Firmware Slot Log\n"); 411 printf("=================\n"); 412 413 if (oacs_fw == 0) 414 slots = 1; 415 else 416 slots = MIN(fw_num_slots, MAX_FW_SLOTS); 417 418 for (i = 0; i < slots; i++) { 419 printf("Slot %d: ", i + 1); 420 if (afi_slot == i + 1) 421 status = " Active"; 422 else 423 status = "Inactive"; 424 425 if (fw->revision[i][0] == '\0') 426 printf("Empty\n"); 427 else 428 printf("[%s] %.8s\n", status, fw->revision[i]); 429 } 430 } 431 432 static void 433 print_log_ns(const struct nvme_controller_data *cdata __unused, void *buf, 434 uint32_t size __unused) 435 { 436 struct nvme_ns_list *nsl; 437 u_int i; 438 439 nsl = (struct nvme_ns_list *)buf; 440 printf("Changed Namespace List\n"); 441 printf("======================\n"); 442 443 for (i = 0; i < nitems(nsl->ns) && nsl->ns[i] != 0; i++) { 444 printf("%08x\n", nsl->ns[i]); 445 } 446 } 447 448 static void 449 print_log_command_effects(const struct nvme_controller_data *cdata __unused, 450 void *buf, uint32_t size __unused) 451 { 452 struct nvme_command_effects_page *ce; 453 u_int i; 454 uint32_t s; 455 456 ce = (struct nvme_command_effects_page *)buf; 457 printf("Commands Supported and Effects\n"); 458 printf("==============================\n"); 459 printf(" Command\tLBCC\tNCC\tNIC\tCCC\tCSE\tUUID\n"); 460 461 for (i = 0; i < 255; i++) { 462 s = ce->acs[i]; 463 if (NVMEV(NVME_CE_PAGE_CSUP, s) == 0) 464 continue; 465 printf("Admin\t%02x\t%s\t%s\t%s\t%s\t%u\t%s\n", i, 466 NVMEV(NVME_CE_PAGE_LBCC, s) != 0 ? "Yes" : "No", 467 NVMEV(NVME_CE_PAGE_NCC, s) != 0 ? "Yes" : "No", 468 NVMEV(NVME_CE_PAGE_NIC, s) != 0 ? "Yes" : "No", 469 NVMEV(NVME_CE_PAGE_CCC, s) != 0 ? "Yes" : "No", 470 NVMEV(NVME_CE_PAGE_CSE, s), 471 NVMEV(NVME_CE_PAGE_UUID, s) != 0 ? "Yes" : "No"); 472 } 473 for (i = 0; i < 255; i++) { 474 s = ce->iocs[i]; 475 if (NVMEV(NVME_CE_PAGE_CSUP, s) == 0) 476 continue; 477 printf("I/O\t%02x\t%s\t%s\t%s\t%s\t%u\t%s\n", i, 478 NVMEV(NVME_CE_PAGE_LBCC, s) != 0 ? "Yes" : "No", 479 NVMEV(NVME_CE_PAGE_NCC, s) != 0 ? "Yes" : "No", 480 NVMEV(NVME_CE_PAGE_NIC, s) != 0 ? "Yes" : "No", 481 NVMEV(NVME_CE_PAGE_CCC, s) != 0 ? "Yes" : "No", 482 NVMEV(NVME_CE_PAGE_CSE, s), 483 NVMEV(NVME_CE_PAGE_UUID, s) != 0 ? "Yes" : "No"); 484 } 485 } 486 487 static void 488 print_log_res_notification(const struct nvme_controller_data *cdata __unused, 489 void *buf, uint32_t size __unused) 490 { 491 struct nvme_res_notification_page *rn; 492 493 rn = (struct nvme_res_notification_page *)buf; 494 printf("Reservation Notification\n"); 495 printf("========================\n"); 496 497 printf("Log Page Count: %ju\n", rn->log_page_count); 498 printf("Log Page Type: "); 499 switch (rn->log_page_type) { 500 case 0: 501 printf("Empty Log Page\n"); 502 break; 503 case 1: 504 printf("Registration Preempted\n"); 505 break; 506 case 2: 507 printf("Reservation Released\n"); 508 break; 509 case 3: 510 printf("Reservation Preempted\n"); 511 break; 512 default: 513 printf("Unknown %x\n", rn->log_page_type); 514 break; 515 }; 516 printf("Number of Available Log Pages: %d\n", rn->available_log_pages); 517 printf("Namespace ID: 0x%x\n", rn->nsid); 518 } 519 520 static void 521 print_log_sanitize_status(const struct nvme_controller_data *cdata __unused, 522 void *buf, uint32_t size __unused) 523 { 524 struct nvme_sanitize_status_page *ss; 525 u_int p; 526 527 ss = (struct nvme_sanitize_status_page *)buf; 528 printf("Sanitize Status\n"); 529 printf("===============\n"); 530 531 printf("Sanitize Progress: %u%% (%u/65535)\n", 532 (ss->sprog * 100 + 32768) / 65536, ss->sprog); 533 printf("Sanitize Status: "); 534 switch (NVMEV(NVME_SS_PAGE_SSTAT_STATUS, ss->sstat)) { 535 case NVME_SS_PAGE_SSTAT_STATUS_NEVER: 536 printf("Never sanitized"); 537 break; 538 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED: 539 printf("Completed"); 540 break; 541 case NVME_SS_PAGE_SSTAT_STATUS_INPROG: 542 printf("In Progress"); 543 break; 544 case NVME_SS_PAGE_SSTAT_STATUS_FAILED: 545 printf("Failed"); 546 break; 547 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD: 548 printf("Completed with deallocation"); 549 break; 550 default: 551 printf("Unknown"); 552 break; 553 } 554 p = NVMEV(NVME_SS_PAGE_SSTAT_PASSES, ss->sstat); 555 if (p > 0) 556 printf(", %d passes", p); 557 if (NVMEV(NVME_SS_PAGE_SSTAT_GDE, ss->sstat) != 0) 558 printf(", Global Data Erased"); 559 printf("\n"); 560 printf("Sanitize Command Dword 10: 0x%x\n", ss->scdw10); 561 printf("Time For Overwrite: %u sec\n", ss->etfo); 562 printf("Time For Block Erase: %u sec\n", ss->etfbe); 563 printf("Time For Crypto Erase: %u sec\n", ss->etfce); 564 printf("Time For Overwrite No-Deallocate: %u sec\n", ss->etfownd); 565 printf("Time For Block Erase No-Deallocate: %u sec\n", ss->etfbewnd); 566 printf("Time For Crypto Erase No-Deallocate: %u sec\n", ss->etfcewnd); 567 } 568 569 static const char * 570 self_test_res[] = { 571 [0] = "completed without error", 572 [1] = "aborted by a Device Self-test command", 573 [2] = "aborted by a Controller Level Reset", 574 [3] = "aborted due to namespace removal", 575 [4] = "aborted due to Format NVM command", 576 [5] = "failed due to fatal or unknown test error", 577 [6] = "completed with an unknown segment that failed", 578 [7] = "completed with one or more failed segments", 579 [8] = "aborted for unknown reason", 580 [9] = "aborted due to a sanitize operation", 581 }; 582 static uint32_t self_test_res_max = nitems(self_test_res); 583 584 static void 585 print_log_self_test_status(const struct nvme_controller_data *cdata __unused, 586 void *buf, uint32_t size __unused) 587 { 588 struct nvme_device_self_test_page *dst; 589 uint32_t r; 590 591 dst = buf; 592 printf("Device Self-test Status\n"); 593 printf("=======================\n"); 594 595 printf("Current Operation: "); 596 switch (dst->curr_operation) { 597 case 0x0: 598 printf("No device self-test operation in progress\n"); 599 break; 600 case 0x1: 601 printf("Short device self-test operation in progress\n"); 602 break; 603 case 0x2: 604 printf("Extended device self-test operation in progress\n"); 605 break; 606 case 0xe: 607 printf("Vendor specific\n"); 608 break; 609 default: 610 printf("Reserved (0x%x)\n", dst->curr_operation); 611 } 612 613 if (dst->curr_operation != 0) 614 printf("Current Completion: %u%%\n", dst->curr_compl & 0x7f); 615 616 printf("Results\n"); 617 for (r = 0; r < 20; r++) { 618 uint64_t failing_lba; 619 uint8_t code, res; 620 621 code = (dst->result[r].status >> 4) & 0xf; 622 res = dst->result[r].status & 0xf; 623 624 if (res == 0xf) 625 continue; 626 627 printf("[%2u] ", r); 628 switch (code) { 629 case 0x1: 630 printf("Short device self-test"); 631 break; 632 case 0x2: 633 printf("Extended device self-test"); 634 break; 635 case 0xe: 636 printf("Vendor specific"); 637 break; 638 default: 639 printf("Reserved (0x%x)", code); 640 } 641 if (res < self_test_res_max) 642 printf(" %s", self_test_res[res]); 643 else 644 printf(" Reserved status 0x%x", res); 645 646 if (res == 7) 647 printf(" starting in segment %u", dst->result[r].segment_num); 648 649 #define BIT(b) (1 << (b)) 650 if (dst->result[r].valid_diag_info & BIT(0)) 651 printf(" NSID=0x%x", dst->result[r].nsid); 652 if (dst->result[r].valid_diag_info & BIT(1)) { 653 memcpy(&failing_lba, dst->result[r].failing_lba, 654 sizeof(failing_lba)); 655 printf(" FLBA=0x%jx", failing_lba); 656 } 657 if (dst->result[r].valid_diag_info & BIT(2)) 658 printf(" SCT=0x%x", dst->result[r].status_code_type); 659 if (dst->result[r].valid_diag_info & BIT(3)) 660 printf(" SC=0x%x", dst->result[r].status_code); 661 #undef BIT 662 printf("\n"); 663 } 664 } 665 666 /* 667 * Table of log page printer / sizing. 668 * 669 * Make sure you keep all the pages of one vendor together so -v help 670 * lists all the vendors pages. 671 */ 672 NVME_LOGPAGE(error, 673 NVME_LOG_ERROR, NULL, "Drive Error Log", 674 print_log_error, 0); 675 NVME_LOGPAGE(health, 676 NVME_LOG_HEALTH_INFORMATION, NULL, "Health/SMART Data", 677 print_log_health, sizeof(struct nvme_health_information_page)); 678 NVME_LOGPAGE(fw, 679 NVME_LOG_FIRMWARE_SLOT, NULL, "Firmware Information", 680 print_log_firmware, sizeof(struct nvme_firmware_page)); 681 NVME_LOGPAGE(ns, 682 NVME_LOG_CHANGED_NAMESPACE, NULL, "Changed Namespace List", 683 print_log_ns, sizeof(struct nvme_ns_list)); 684 NVME_LOGPAGE(ce, 685 NVME_LOG_COMMAND_EFFECT, NULL, "Commands Supported and Effects", 686 print_log_command_effects, sizeof(struct nvme_command_effects_page)); 687 NVME_LOGPAGE(dst, 688 NVME_LOG_DEVICE_SELF_TEST, NULL, "Device Self-test", 689 print_log_self_test_status, sizeof(struct nvme_device_self_test_page)); 690 NVME_LOGPAGE(thi, 691 NVME_LOG_TELEMETRY_HOST_INITIATED, NULL, "Telemetry Host-Initiated", 692 NULL, DEFAULT_SIZE); 693 NVME_LOGPAGE(tci, 694 NVME_LOG_TELEMETRY_CONTROLLER_INITIATED, NULL, "Telemetry Controller-Initiated", 695 NULL, DEFAULT_SIZE); 696 NVME_LOGPAGE(egi, 697 NVME_LOG_ENDURANCE_GROUP_INFORMATION, NULL, "Endurance Group Information", 698 NULL, DEFAULT_SIZE); 699 NVME_LOGPAGE(plpns, 700 NVME_LOG_PREDICTABLE_LATENCY_PER_NVM_SET, NULL, "Predictable Latency Per NVM Set", 701 NULL, DEFAULT_SIZE); 702 NVME_LOGPAGE(ple, 703 NVME_LOG_PREDICTABLE_LATENCY_EVENT_AGGREGATE, NULL, "Predictable Latency Event Aggregate", 704 NULL, DEFAULT_SIZE); 705 NVME_LOGPAGE(ana, 706 NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS, NULL, "Asymmetric Namespace Access", 707 NULL, DEFAULT_SIZE); 708 NVME_LOGPAGE(pel, 709 NVME_LOG_PERSISTENT_EVENT_LOG, NULL, "Persistent Event Log", 710 NULL, DEFAULT_SIZE); 711 NVME_LOGPAGE(lbasi, 712 NVME_LOG_LBA_STATUS_INFORMATION, NULL, "LBA Status Information", 713 NULL, DEFAULT_SIZE); 714 NVME_LOGPAGE(egea, 715 NVME_LOG_ENDURANCE_GROUP_EVENT_AGGREGATE, NULL, "Endurance Group Event Aggregate", 716 NULL, DEFAULT_SIZE); 717 NVME_LOGPAGE(res_notification, 718 NVME_LOG_RES_NOTIFICATION, NULL, "Reservation Notification", 719 print_log_res_notification, sizeof(struct nvme_res_notification_page)); 720 NVME_LOGPAGE(sanitize_status, 721 NVME_LOG_SANITIZE_STATUS, NULL, "Sanitize Status", 722 print_log_sanitize_status, sizeof(struct nvme_sanitize_status_page)); 723 724 static void 725 logpage_help(void) 726 { 727 const struct logpage_function *f; 728 const char *v; 729 730 fprintf(stderr, "\n"); 731 fprintf(stderr, "%-8s %-10s %s\n", "Page", "Vendor","Page Name"); 732 fprintf(stderr, "-------- ---------- ----------\n"); 733 SLIST_FOREACH(f, &logpages, link) { 734 v = f->vendor == NULL ? "-" : f->vendor; 735 fprintf(stderr, "0x%02x %-10s %s\n", f->log_page, v, f->name); 736 } 737 738 exit(EX_USAGE); 739 } 740 741 static void 742 logpage(const struct cmd *f, int argc, char *argv[]) 743 { 744 int fd; 745 char *path; 746 uint32_t nsid, size; 747 void *buf; 748 const struct logpage_function *lpf; 749 struct nvme_controller_data cdata; 750 print_fn_t print_fn; 751 uint8_t ns_smart; 752 753 if (arg_parse(argc, argv, f)) 754 return; 755 if (opt.hex && opt.binary) { 756 fprintf(stderr, 757 "Can't specify both binary and hex\n"); 758 arg_help(argc, argv, f); 759 } 760 if (opt.vendor != NULL && strcmp(opt.vendor, "help") == 0) 761 logpage_help(); 762 if (opt.page == NONE) { 763 fprintf(stderr, "Missing page_id (-p).\n"); 764 arg_help(argc, argv, f); 765 } 766 open_dev(opt.dev, &fd, 0, 1); 767 get_nsid(fd, &path, &nsid); 768 if (nsid == 0) { 769 nsid = NVME_GLOBAL_NAMESPACE_TAG; 770 } else { 771 close(fd); 772 open_dev(path, &fd, 0, 1); 773 } 774 free(path); 775 776 if (read_controller_data(fd, &cdata)) 777 errx(EX_IOERR, "Identify request failed"); 778 779 ns_smart = NVMEV(NVME_CTRLR_DATA_LPA_NS_SMART, cdata.lpa); 780 781 /* 782 * The log page attributes indicate whether or not the controller 783 * supports the SMART/Health information log page on a per 784 * namespace basis. 785 */ 786 if (nsid != NVME_GLOBAL_NAMESPACE_TAG) { 787 if (opt.page != NVME_LOG_HEALTH_INFORMATION) 788 errx(EX_USAGE, "log page %d valid only at controller level", 789 opt.page); 790 if (ns_smart == 0) 791 errx(EX_UNAVAILABLE, 792 "controller does not support per namespace " 793 "smart/health information"); 794 } 795 796 print_fn = print_log_hex; 797 size = DEFAULT_SIZE; 798 if (opt.binary) 799 print_fn = print_bin; 800 if (!opt.binary && !opt.hex) { 801 /* 802 * See if there is a pretty print function for the specified log 803 * page. If one isn't found, we just revert to the default 804 * (print_hex). If there was a vendor specified by the user, and 805 * the page is vendor specific, don't match the print function 806 * unless the vendors match. 807 */ 808 SLIST_FOREACH(lpf, &logpages, link) { 809 if (lpf->vendor != NULL && opt.vendor != NULL && 810 strcmp(lpf->vendor, opt.vendor) != 0) 811 continue; 812 if (opt.page != lpf->log_page) 813 continue; 814 if (lpf->print_fn != NULL) 815 print_fn = lpf->print_fn; 816 size = lpf->size; 817 break; 818 } 819 } 820 821 if (opt.page == NVME_LOG_ERROR) { 822 size = sizeof(struct nvme_error_information_entry); 823 size *= (cdata.elpe + 1); 824 } 825 826 /* Read the log page */ 827 buf = get_log_buffer(size); 828 read_logpage(fd, opt.page, nsid, opt.lsp, opt.lsi, opt.rae, buf, size); 829 print_fn(&cdata, buf, size); 830 831 close(fd); 832 exit(0); 833 } 834