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