2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 EMC Corp.
7 * Copyright (C) 2012-2013 Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/ioccom.h>
47 #include <sys/endian.h>
49 #include "nvmecontrol.h"
51 #define DEFAULT_SIZE (4096)
52 #define MAX_FW_SLOTS (7)
54 typedef void (*print_fn_t)(const struct nvme_controller_data *cdata, void *buf, uint32_t size);
63 kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key)
68 for (i = 0; i < kv_count; i++, kv++)
71 snprintf(bad, sizeof(bad), "Attribute %#x", key);
76 print_log_hex(const struct nvme_controller_data *cdata __unused, void *data, uint32_t length)
79 print_hex(data, length);
83 print_bin(const struct nvme_controller_data *cdata __unused, void *data, uint32_t length)
86 write(STDOUT_FILENO, data, length);
90 get_log_buffer(uint32_t size)
94 if ((buf = malloc(size)) == NULL)
95 errx(1, "unable to malloc %u bytes", size);
102 read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload,
103 uint32_t payload_size)
105 struct nvme_pt_command pt;
106 struct nvme_error_information_entry *err_entry;
109 memset(&pt, 0, sizeof(pt));
110 pt.cmd.opc = NVME_OPC_GET_LOG_PAGE;
111 pt.cmd.nsid = htole32(nsid);
112 pt.cmd.cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16;
113 pt.cmd.cdw10 |= log_page;
114 pt.cmd.cdw10 = htole32(pt.cmd.cdw10);
116 pt.len = payload_size;
119 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
120 err(1, "get log page request failed");
122 /* Convert data to host endian */
125 err_entry = (struct nvme_error_information_entry *)payload;
126 err_pages = payload_size / sizeof(struct nvme_error_information_entry);
127 for (i = 0; i < err_pages; i++)
128 nvme_error_information_entry_swapbytes(err_entry++);
130 case NVME_LOG_HEALTH_INFORMATION:
131 nvme_health_information_page_swapbytes(
132 (struct nvme_health_information_page *)payload);
134 case NVME_LOG_FIRMWARE_SLOT:
135 nvme_firmware_page_swapbytes(
136 (struct nvme_firmware_page *)payload);
138 case INTEL_LOG_TEMP_STATS:
139 intel_log_temp_stats_swapbytes(
140 (struct intel_log_temp_stats *)payload);
146 if (nvme_completion_is_error(&pt.cpl))
147 errx(1, "get log page request returned error");
151 print_log_error(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
155 uint8_t p, sc, sct, m, dnr;
156 struct nvme_error_information_entry *entry = buf;
158 printf("Error Information Log\n");
159 printf("=====================\n");
161 if (entry->error_count == 0) {
162 printf("No error entries found\n");
166 nentries = size/sizeof(struct nvme_error_information_entry);
167 for (i = 0; i < nentries; i++, entry++) {
168 if (entry->error_count == 0)
171 status = entry->status;
173 p = NVME_STATUS_GET_P(status);
174 sc = NVME_STATUS_GET_SC(status);
175 sct = NVME_STATUS_GET_SCT(status);
176 m = NVME_STATUS_GET_M(status);
177 dnr = NVME_STATUS_GET_DNR(status);
179 printf("Entry %02d\n", i + 1);
180 printf("=========\n");
181 printf(" Error count: %ju\n", entry->error_count);
182 printf(" Submission queue ID: %u\n", entry->sqid);
183 printf(" Command ID: %u\n", entry->cid);
184 /* TODO: Export nvme_status_string structures from kernel? */
185 printf(" Status:\n");
186 printf(" Phase tag: %d\n", p);
187 printf(" Status code: %d\n", sc);
188 printf(" Status code type: %d\n", sct);
189 printf(" More: %d\n", m);
190 printf(" DNR: %d\n", dnr);
191 printf(" Error location: %u\n", entry->error_location);
192 printf(" LBA: %ju\n", entry->lba);
193 printf(" Namespace ID: %u\n", entry->nsid);
194 printf(" Vendor specific info: %u\n", entry->vendor_specific);
199 print_temp(uint16_t t)
201 printf("%u K, %2.2f C, %3.2f F\n", t, (float)t - 273.15, (float)t * 9 / 5 - 459.67);
206 print_log_health(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
208 struct nvme_health_information_page *health = buf;
209 char cbuf[UINT128_DIG + 1];
213 warning = health->critical_warning;
215 printf("SMART/Health Information Log\n");
216 printf("============================\n");
218 printf("Critical Warning State: 0x%02x\n", warning);
219 printf(" Available spare: %d\n",
220 !!(warning & NVME_CRIT_WARN_ST_AVAILABLE_SPARE));
221 printf(" Temperature: %d\n",
222 !!(warning & NVME_CRIT_WARN_ST_TEMPERATURE));
223 printf(" Device reliability: %d\n",
224 !!(warning & NVME_CRIT_WARN_ST_DEVICE_RELIABILITY));
225 printf(" Read only: %d\n",
226 !!(warning & NVME_CRIT_WARN_ST_READ_ONLY));
227 printf(" Volatile memory backup: %d\n",
228 !!(warning & NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP));
229 printf("Temperature: ");
230 print_temp(health->temperature);
231 printf("Available spare: %u\n",
232 health->available_spare);
233 printf("Available spare threshold: %u\n",
234 health->available_spare_threshold);
235 printf("Percentage used: %u\n",
236 health->percentage_used);
238 printf("Data units (512,000 byte) read: %s\n",
239 uint128_to_str(to128(health->data_units_read), cbuf, sizeof(cbuf)));
240 printf("Data units written: %s\n",
241 uint128_to_str(to128(health->data_units_written), cbuf, sizeof(cbuf)));
242 printf("Host read commands: %s\n",
243 uint128_to_str(to128(health->host_read_commands), cbuf, sizeof(cbuf)));
244 printf("Host write commands: %s\n",
245 uint128_to_str(to128(health->host_write_commands), cbuf, sizeof(cbuf)));
246 printf("Controller busy time (minutes): %s\n",
247 uint128_to_str(to128(health->controller_busy_time), cbuf, sizeof(cbuf)));
248 printf("Power cycles: %s\n",
249 uint128_to_str(to128(health->power_cycles), cbuf, sizeof(cbuf)));
250 printf("Power on hours: %s\n",
251 uint128_to_str(to128(health->power_on_hours), cbuf, sizeof(cbuf)));
252 printf("Unsafe shutdowns: %s\n",
253 uint128_to_str(to128(health->unsafe_shutdowns), cbuf, sizeof(cbuf)));
254 printf("Media errors: %s\n",
255 uint128_to_str(to128(health->media_errors), cbuf, sizeof(cbuf)));
256 printf("No. error info log entries: %s\n",
257 uint128_to_str(to128(health->num_error_info_log_entries), cbuf, sizeof(cbuf)));
259 printf("Warning Temp Composite Time: %d\n", health->warning_temp_time);
260 printf("Error Temp Composite Time: %d\n", health->error_temp_time);
261 for (i = 0; i < 8; i++) {
262 if (health->temp_sensor[i] == 0)
264 printf("Temperature Sensor %d: ", i + 1);
265 print_temp(health->temp_sensor[i]);
270 print_log_firmware(const struct nvme_controller_data *cdata, void *buf, uint32_t size __unused)
274 struct nvme_firmware_page *fw = buf;
277 uint8_t fw_num_slots;
279 afi_slot = fw->afi >> NVME_FIRMWARE_PAGE_AFI_SLOT_SHIFT;
280 afi_slot &= NVME_FIRMWARE_PAGE_AFI_SLOT_MASK;
282 oacs_fw = (cdata->oacs >> NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT) &
283 NVME_CTRLR_DATA_OACS_FIRMWARE_MASK;
284 fw_num_slots = (cdata->frmw >> NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT) &
285 NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK;
287 printf("Firmware Slot Log\n");
288 printf("=================\n");
293 slots = MIN(fw_num_slots, MAX_FW_SLOTS);
295 for (i = 0; i < slots; i++) {
296 printf("Slot %d: ", i + 1);
297 if (afi_slot == i + 1)
302 if (fw->revision[i] == 0LLU)
305 if (isprint(*(char *)&fw->revision[i]))
306 printf("[%s] %.8s\n", status,
307 (char *)&fw->revision[i]);
309 printf("[%s] %016jx\n", status,
315 * Intel specific log pages from
316 * http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/ssd-dc-p3700-spec.pdf
318 * Though the version as of this date has a typo for the size of log page 0xca,
319 * offset 147: it is only 1 byte, not 6.
322 print_intel_temp_stats(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
324 struct intel_log_temp_stats *temp = buf;
326 printf("Intel Temperature Log\n");
327 printf("=====================\n");
330 print_temp(temp->current);
331 printf("Overtemp Last Flags %#jx\n", (uintmax_t)temp->overtemp_flag_last);
332 printf("Overtemp Lifetime Flags %#jx\n", (uintmax_t)temp->overtemp_flag_life);
333 printf("Max Temperature ");
334 print_temp(temp->max_temp);
335 printf("Min Temperature ");
336 print_temp(temp->min_temp);
337 printf("Max Operating Temperature ");
338 print_temp(temp->max_oper_temp);
339 printf("Min Operating Temperature ");
340 print_temp(temp->min_oper_temp);
341 printf("Estimated Temperature Offset: %ju C/K\n", (uintmax_t)temp->est_offset);
345 * Format from Table 22, section 5.7 IO Command Latency Statistics.
346 * Read and write stats pages have identical encoding.
349 print_intel_read_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
351 const char *walker = buf;
354 printf("Major: %d\n", le16dec(walker + 0));
355 printf("Minor: %d\n", le16dec(walker + 2));
356 for (i = 0; i < 32; i++)
357 printf("%4dus-%4dus: %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 4 + i * 4));
358 for (i = 1; i < 32; i++)
359 printf("%4dms-%4dms: %ju\n", i, i + 1, (uintmax_t)le32dec(walker + 132 + i * 4));
360 for (i = 1; i < 32; i++)
361 printf("%4dms-%4dms: %ju\n", i * 32, (i + 1) * 32, (uintmax_t)le32dec(walker + 256 + i * 4));
365 print_intel_read_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
368 printf("Intel Read Latency Log\n");
369 printf("======================\n");
370 print_intel_read_write_lat_log(cdata, buf, size);
374 print_intel_write_lat_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size)
377 printf("Intel Write Latency Log\n");
378 printf("=======================\n");
379 print_intel_read_write_lat_log(cdata, buf, size);
383 * Table 19. 5.4 SMART Attributes. Samsung also implements this and some extra data not documented.
386 print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
388 uint8_t *walker = buf;
389 uint8_t *end = walker + 150;
394 static struct kv_name kv[] =
396 { 0xab, "Program Fail Count" },
397 { 0xac, "Erase Fail Count" },
398 { 0xad, "Wear Leveling Count" },
399 { 0xb8, "End to End Error Count" },
400 { 0xc7, "CRC Error Count" },
401 { 0xe2, "Timed: Media Wear" },
402 { 0xe3, "Timed: Host Read %" },
403 { 0xe4, "Timed: Elapsed Time" },
404 { 0xea, "Thermal Throttle Status" },
405 { 0xf0, "Retry Buffer Overflows" },
406 { 0xf3, "PLL Lock Loss Count" },
407 { 0xf4, "NAND Bytes Written" },
408 { 0xf5, "Host Bytes Written" },
411 printf("Additional SMART Data Log\n");
412 printf("=========================\n");
415 * walker[1,2] = reserved
416 * walker[3] = Normalized Value
417 * walker[4] = reserved
418 * walker[5..10] = Little Endian Raw value
419 * (or other represenations)
420 * walker[11] = reserved
422 while (walker < end) {
423 name = kv_lookup(kv, nitems(kv), *walker);
424 normalized = walker[3];
425 raw = le48dec(walker + 5);
430 printf("%-32s: %3d min: %u max: %u ave: %u\n", name, normalized,
431 le16dec(walker + 5), le16dec(walker + 7), le16dec(walker + 9));
434 printf("%-32s: %3d %.3f%%\n", name, normalized, raw / 1024.0);
437 printf("%-32s: %3d %d%% %d times\n", name, normalized, walker[5], le32dec(walker+6));
440 printf("%-32s: %3d %ju\n", name, normalized, (uintmax_t)raw);
448 * HGST's 0xc1 page. This is a grab bag of additional data. Please see
449 * https://www.hgst.com/sites/default/files/resources/US_SN150_ProdManual.pdf
450 * https://www.hgst.com/sites/default/files/resources/US_SN100_ProdManual.pdf
451 * Appendix A for details
454 typedef void (*subprint_fn_t)(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
462 static void print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
463 static void print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
464 static void print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
465 static void print_hgst_info_self_test(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
466 static void print_hgst_info_background_scan(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
467 static void print_hgst_info_erase_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
468 static void print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
469 static void print_hgst_info_temp_history(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
470 static void print_hgst_info_ssd_perf(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
471 static void print_hgst_info_firmware_load(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
473 static struct subpage_print hgst_subpage[] = {
474 { 0x02, print_hgst_info_write_errors },
475 { 0x03, print_hgst_info_read_errors },
476 { 0x05, print_hgst_info_verify_errors },
477 { 0x10, print_hgst_info_self_test },
478 { 0x15, print_hgst_info_background_scan },
479 { 0x30, print_hgst_info_erase_errors },
480 { 0x31, print_hgst_info_erase_counts },
481 { 0x32, print_hgst_info_temp_history },
482 { 0x37, print_hgst_info_ssd_perf },
483 { 0x38, print_hgst_info_firmware_load },
486 /* Print a subpage that is basically just key value pairs */
488 print_hgst_info_subpage_gen(void *buf, uint16_t subtype __unused, uint32_t size,
489 const struct kv_name *kv, size_t kv_count)
500 ptype = le16dec(wsp);
502 wsp++; /* Flags, just ignore */
505 for (i = 0; i < plen; i++)
506 param |= (uint64_t)*wsp++ << (i * 8);
507 printf(" %-30s: %jd\n", kv_lookup(kv, kv_count, ptype), (uintmax_t)param);
512 print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
514 static struct kv_name kv[] =
516 { 0x0000, "Corrected Without Delay" },
517 { 0x0001, "Corrected Maybe Delayed" },
518 { 0x0002, "Re-Writes" },
519 { 0x0003, "Errors Corrected" },
520 { 0x0004, "Correct Algorithm Used" },
521 { 0x0005, "Bytes Processed" },
522 { 0x0006, "Uncorrected Errors" },
523 { 0x8000, "Flash Write Commands" },
524 { 0x8001, "HGST Special" },
527 printf("Write Errors Subpage:\n");
528 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
532 print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
534 static struct kv_name kv[] =
536 { 0x0000, "Corrected Without Delay" },
537 { 0x0001, "Corrected Maybe Delayed" },
538 { 0x0002, "Re-Reads" },
539 { 0x0003, "Errors Corrected" },
540 { 0x0004, "Correct Algorithm Used" },
541 { 0x0005, "Bytes Processed" },
542 { 0x0006, "Uncorrected Errors" },
543 { 0x8000, "Flash Read Commands" },
544 { 0x8001, "XOR Recovered" },
545 { 0x8002, "Total Corrected Bits" },
548 printf("Read Errors Subpage:\n");
549 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
553 print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
555 static struct kv_name kv[] =
557 { 0x0000, "Corrected Without Delay" },
558 { 0x0001, "Corrected Maybe Delayed" },
559 { 0x0002, "Re-Reads" },
560 { 0x0003, "Errors Corrected" },
561 { 0x0004, "Correct Algorithm Used" },
562 { 0x0005, "Bytes Processed" },
563 { 0x0006, "Uncorrected Errors" },
564 { 0x8000, "Commands Processed" },
567 printf("Verify Errors Subpage:\n");
568 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
572 print_hgst_info_self_test(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
575 uint8_t *walker = buf;
579 printf("Self Test Subpage:\n");
580 for (i = 0; i < size / 20; i++) { /* Each entry is 20 bytes */
581 code = le16dec(walker);
583 walker++; /* Ignore fixed flags */
584 if (*walker == 0) /* Last entry is zero length */
586 if (*walker++ != 0x10) {
587 printf("Bad length for self test report\n");
590 printf(" %-30s: %d\n", "Recent Test", code);
591 printf(" %-28s: %#x\n", "Self-Test Results", *walker & 0xf);
592 printf(" %-28s: %#x\n", "Self-Test Code", (*walker >> 5) & 0x7);
594 printf(" %-28s: %#x\n", "Self-Test Number", *walker++);
595 hrs = le16dec(walker);
597 lba = le32dec(walker);
599 printf(" %-28s: %u\n", "Total Power On Hrs", hrs);
600 printf(" %-28s: %#jx (%jd)\n", "LBA", (uintmax_t)lba, (uintmax_t)lba);
601 printf(" %-28s: %#x\n", "Sense Key", *walker++ & 0xf);
602 printf(" %-28s: %#x\n", "Additional Sense Code", *walker++);
603 printf(" %-28s: %#x\n", "Additional Sense Qualifier", *walker++);
604 printf(" %-28s: %#x\n", "Vendor Specific Detail", *walker++);
609 print_hgst_info_background_scan(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
611 uint8_t *walker = buf;
613 uint16_t code, nscan, progress;
616 printf("Background Media Scan Subpage:\n");
617 /* Decode the header */
618 code = le16dec(walker);
620 walker++; /* Ignore fixed flags */
621 if (*walker++ != 0x10) {
622 printf("Bad length for background scan header\n");
626 printf("Expceted code 0, found code %#x\n", code);
629 pom = le32dec(walker);
631 walker++; /* Reserved */
633 nscan = le16dec(walker);
635 progress = le16dec(walker);
637 walker += 6; /* Reserved */
638 printf(" %-30s: %d\n", "Power On Minutes", pom);
639 printf(" %-30s: %x (%s)\n", "BMS Status", status,
640 status == 0 ? "idle" : (status == 1 ? "active" : (status == 8 ? "suspended" : "unknown")));
641 printf(" %-30s: %d\n", "Number of BMS", nscan);
642 printf(" %-30s: %d\n", "Progress Current BMS", progress);
643 /* Report retirements */
644 if (walker - (uint8_t *)buf != 20) {
645 printf("Coding error, offset not 20\n");
649 printf(" %-30s: %d\n", "BMS retirements", size / 0x18);
651 code = le16dec(walker);
654 if (*walker++ != 0x14) {
655 printf("Bad length parameter\n");
658 pom = le32dec(walker);
661 * Spec sheet says the following are hard coded, if true, just
662 * print the NAND retirement.
664 if (walker[0] == 0x41 &&
673 walker += 4; /* Skip reserved */
674 nand = le32dec(walker);
676 printf(" %-30s: %d\n", "Retirement number", code);
677 printf(" %-28s: %#x\n", "NAND (C/T)BBBPPP", nand);
679 printf("Parameter %#x entry corrupt\n", code);
686 print_hgst_info_erase_errors(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
688 static struct kv_name kv[] =
690 { 0x0000, "Corrected Without Delay" },
691 { 0x0001, "Corrected Maybe Delayed" },
692 { 0x0002, "Re-Erase" },
693 { 0x0003, "Errors Corrected" },
694 { 0x0004, "Correct Algorithm Used" },
695 { 0x0005, "Bytes Processed" },
696 { 0x0006, "Uncorrected Errors" },
697 { 0x8000, "Flash Erase Commands" },
698 { 0x8001, "Mfg Defect Count" },
699 { 0x8002, "Grown Defect Count" },
700 { 0x8003, "Erase Count -- User" },
701 { 0x8004, "Erase Count -- System" },
704 printf("Erase Errors Subpage:\n");
705 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
709 print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
711 /* My drive doesn't export this -- so not coding up */
712 printf("XXX: Erase counts subpage: %p, %#x %d\n", buf, subtype, size);
716 print_hgst_info_temp_history(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
718 uint8_t *walker = buf;
721 printf("Temperature History:\n");
722 printf(" %-30s: %d C\n", "Current Temperature", *walker++);
723 printf(" %-30s: %d C\n", "Reference Temperature", *walker++);
724 printf(" %-30s: %d C\n", "Maximum Temperature", *walker++);
725 printf(" %-30s: %d C\n", "Minimum Temperature", *walker++);
726 min = le32dec(walker);
728 printf(" %-30s: %d:%02d:00\n", "Max Temperature Time", min / 60, min % 60);
729 min = le32dec(walker);
731 printf(" %-30s: %d:%02d:00\n", "Over Temperature Duration", min / 60, min % 60);
732 min = le32dec(walker);
734 printf(" %-30s: %d:%02d:00\n", "Min Temperature Time", min / 60, min % 60);
738 print_hgst_info_ssd_perf(void *buf, uint16_t subtype __unused, uint8_t res, uint32_t size __unused)
740 uint8_t *walker = buf;
743 printf("SSD Performance Subpage Type %d:\n", res);
744 val = le64dec(walker);
746 printf(" %-30s: %ju\n", "Host Read Commands", val);
747 val = le64dec(walker);
749 printf(" %-30s: %ju\n", "Host Read Blocks", val);
750 val = le64dec(walker);
752 printf(" %-30s: %ju\n", "Host Cache Read Hits Commands", val);
753 val = le64dec(walker);
755 printf(" %-30s: %ju\n", "Host Cache Read Hits Blocks", val);
756 val = le64dec(walker);
758 printf(" %-30s: %ju\n", "Host Read Commands Stalled", val);
759 val = le64dec(walker);
761 printf(" %-30s: %ju\n", "Host Write Commands", val);
762 val = le64dec(walker);
764 printf(" %-30s: %ju\n", "Host Write Blocks", val);
765 val = le64dec(walker);
767 printf(" %-30s: %ju\n", "Host Write Odd Start Commands", val);
768 val = le64dec(walker);
770 printf(" %-30s: %ju\n", "Host Write Odd End Commands", val);
771 val = le64dec(walker);
773 printf(" %-30s: %ju\n", "Host Write Commands Stalled", val);
774 val = le64dec(walker);
776 printf(" %-30s: %ju\n", "NAND Read Commands", val);
777 val = le64dec(walker);
779 printf(" %-30s: %ju\n", "NAND Read Blocks", val);
780 val = le64dec(walker);
782 printf(" %-30s: %ju\n", "NAND Write Commands", val);
783 val = le64dec(walker);
785 printf(" %-30s: %ju\n", "NAND Write Blocks", val);
786 val = le64dec(walker);
788 printf(" %-30s: %ju\n", "NAND Read Before Writes", val);
792 print_hgst_info_firmware_load(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
794 uint8_t *walker = buf;
796 printf("Firmware Load Subpage:\n");
797 printf(" %-30s: %d\n", "Firmware Downloads", le32dec(walker));
801 kv_indirect(void *buf, uint32_t subtype, uint8_t res, uint32_t size, struct subpage_print *sp, size_t nsp)
805 for (i = 0; i < nsp; i++, sp++) {
806 if (sp->key == subtype) {
807 sp->fn(buf, subtype, res, size);
811 printf("No handler for page type %x\n", subtype);
815 print_hgst_info_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
817 uint8_t *walker, *end, *subpage;
820 uint8_t subtype, res;
822 printf("HGST Extra Info Log\n");
823 printf("===================\n");
828 len = le16dec(walker);
830 end = walker + len; /* Length is exclusive of this header */
832 while (walker < end) {
833 subpage = walker + 4;
834 subtype = *walker++ & 0x3f; /* subtype */
835 res = *walker++; /* Reserved */
836 len = le16dec(walker);
837 walker += len + 2; /* Length, not incl header */
839 printf("Ooops! Off the end of the list\n");
842 kv_indirect(subpage, subtype, res, len, hgst_subpage, nitems(hgst_subpage));
847 * Table of log page printer / sizing.
849 * This includes Intel specific pages that are widely implemented.
850 * Make sure you keep all the pages of one vendor together so -v help
851 * lists all the vendors pages.
853 static struct logpage_function {
860 {NVME_LOG_ERROR, NULL, "Drive Error Log",
862 {NVME_LOG_HEALTH_INFORMATION, NULL, "Health/SMART Data",
863 print_log_health, sizeof(struct nvme_health_information_page)},
864 {NVME_LOG_FIRMWARE_SLOT, NULL, "Firmware Information",
865 print_log_firmware, sizeof(struct nvme_firmware_page)},
866 {HGST_INFO_LOG, "hgst", "Detailed Health/SMART",
867 print_hgst_info_log, DEFAULT_SIZE},
868 {HGST_INFO_LOG, "wds", "Detailed Health/SMART",
869 print_hgst_info_log, DEFAULT_SIZE},
870 {INTEL_LOG_TEMP_STATS, "intel", "Temperature Stats",
871 print_intel_temp_stats, sizeof(struct intel_log_temp_stats)},
872 {INTEL_LOG_READ_LAT_LOG, "intel", "Read Latencies",
873 print_intel_read_lat_log, DEFAULT_SIZE},
874 {INTEL_LOG_WRITE_LAT_LOG, "intel", "Write Latencies",
875 print_intel_write_lat_log, DEFAULT_SIZE},
876 {INTEL_LOG_ADD_SMART, "intel", "Extra Health/SMART Data",
877 print_intel_add_smart, DEFAULT_SIZE},
878 {INTEL_LOG_ADD_SMART, "samsung", "Extra Health/SMART Data",
879 print_intel_add_smart, DEFAULT_SIZE},
881 {0, NULL, NULL, NULL, 0},
887 fprintf(stderr, "usage:\n");
888 fprintf(stderr, LOGPAGE_USAGE);
895 struct logpage_function *f;
898 fprintf(stderr, "\n");
899 fprintf(stderr, "%-8s %-10s %s\n", "Page", "Vendor","Page Name");
900 fprintf(stderr, "-------- ---------- ----------\n");
901 for (f = logfuncs; f->log_page > 0; f++) {
902 v = f->vendor == NULL ? "-" : f->vendor;
903 fprintf(stderr, "0x%02x %-10s %s\n", f->log_page, v, f->name);
910 logpage(int argc, char *argv[])
913 int log_page = 0, pageflag = false;
914 int binflag = false, hexflag = false, ns_specified;
920 const char *vendor = NULL;
921 struct logpage_function *f;
922 struct nvme_controller_data cdata;
926 while ((opt = getopt(argc, argv, "bp:xv:")) != -1) {
932 if (strcmp(optarg, "help") == 0)
935 /* TODO: Add human-readable ASCII page IDs */
936 log_page = strtol(optarg, &p, 0);
937 if (p != NULL && *p != '\0') {
939 "\"%s\" not valid log page id.\n",
949 if (strcmp(optarg, "help") == 0)
957 printf("Missing page_id (-p).\n");
961 /* Check that a controller and/or namespace was specified. */
965 if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) {
967 parse_ns_str(argv[optind], cname, &nsid);
968 open_dev(cname, &fd, 1, 1);
970 ns_specified = false;
971 nsid = NVME_GLOBAL_NAMESPACE_TAG;
972 open_dev(argv[optind], &fd, 1, 1);
975 read_controller_data(fd, &cdata);
977 ns_smart = (cdata.lpa >> NVME_CTRLR_DATA_LPA_NS_SMART_SHIFT) &
978 NVME_CTRLR_DATA_LPA_NS_SMART_MASK;
981 * The log page attribtues indicate whether or not the controller
982 * supports the SMART/Health information log page on a per
986 if (log_page != NVME_LOG_HEALTH_INFORMATION)
987 errx(1, "log page %d valid only at controller level",
991 "controller does not support per namespace "
992 "smart/health information");
995 print_fn = print_log_hex;
998 print_fn = print_bin;
999 if (!binflag && !hexflag) {
1001 * See if there is a pretty print function for the specified log
1002 * page. If one isn't found, we just revert to the default
1003 * (print_hex). If there was a vendor specified bt the user, and
1004 * the page is vendor specific, don't match the print function
1005 * unless the vendors match.
1007 for (f = logfuncs; f->log_page > 0; f++) {
1008 if (f->vendor != NULL && vendor != NULL &&
1009 strcmp(f->vendor, vendor) != 0)
1011 if (log_page != f->log_page)
1013 print_fn = f->print_fn;
1019 if (log_page == NVME_LOG_ERROR) {
1020 size = sizeof(struct nvme_error_information_entry);
1021 size *= (cdata.elpe + 1);
1024 /* Read the log page */
1025 buf = get_log_buffer(size);
1026 read_logpage(fd, log_page, nsid, buf, size);
1027 print_fn(&cdata, buf, size);