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