2 * Copyright (c) 2017 Netflix, Inc
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
32 #include <sys/endian.h>
43 #include "nvmecontrol.h"
48 SET_DECLARE(wdc, struct nvme_function);
50 #define WDC_NVME_TOC_SIZE 8
52 #define WDC_NVME_CAP_DIAG_OPCODE 0xe6
53 #define WDC_NVME_CAP_DIAG_CMD 0x0000
55 static void wdc_cap_diag(struct nvme_function *nf, int argc, char *argv[]);
57 #define WDC_CAP_DIAG_USAGE "wdc cap-diag [-o path-template]\n"
59 NVME_COMMAND(wdc, cap-diag, wdc_cap_diag, WDC_CAP_DIAG_USAGE);
62 wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix)
64 struct nvme_controller_data cdata;
65 char sn[NVME_SERIAL_NUMBER_LENGTH + 1];
70 read_controller_data(fd, &cdata);
71 memcpy(sn, cdata.sn, NVME_SERIAL_NUMBER_LENGTH);
72 walker = sn + NVME_SERIAL_NUMBER_LENGTH - 1;
73 while (walker > sn && *walker == ' ')
76 snprintf(buf, len, "%s%s.bin", sn, suffix);
80 wdc_get_data(int fd, uint32_t opcode, uint32_t len, uint32_t off, uint32_t cmd,
81 uint8_t *buffer, size_t buflen)
83 struct nvme_pt_command pt;
85 memset(&pt, 0, sizeof(pt));
87 pt.cmd.cdw10 = htole32(len / sizeof(uint32_t)); /* - 1 like all the others ??? */
88 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t));
89 pt.cmd.cdw12 = htole32(cmd);
93 // printf("opcode %#x cdw10(len) %#x cdw11(offset?) %#x cdw12(cmd/sub) %#x buflen %zd\n",
94 // (int)opcode, (int)cdw10, (int)cdw11, (int)cdw12, buflen);
96 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
97 err(1, "wdc_get_data request failed");
98 if (nvme_completion_is_error(&pt.cpl))
99 errx(1, "wdc_get_data request returned error");
103 wdc_do_dump(int fd, char *tmpl, const char *suffix, uint32_t opcode,
104 uint32_t cmd, int len_off)
109 uint32_t len, offset;
112 wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
114 /* XXX overwrite protection? */
115 fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC, 0644);
117 err(1, "open %s", tmpl);
118 buf = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE);
120 errx(1, "Can't get buffer to read dump");
122 len = NVME_MAX_XFER_SIZE;
126 resid = len > NVME_MAX_XFER_SIZE ? NVME_MAX_XFER_SIZE : len;
127 wdc_get_data(fd, opcode, resid, offset, cmd, buf, resid);
130 len = be32dec(buf + len_off);
132 errx(1, "No data for %s", suffix);
133 if (memcmp("E6LG", buf, 4) != 0)
134 printf("Expected header of E6LG, found '%4.4s' instead\n",
136 printf("Dumping %d bytes of version %d.%d log to %s\n", len,
137 buf[8], buf[9], tmpl);
139 * Adjust amount to dump if total dump < 1MB,
140 * though it likely doesn't matter to the WDC
147 if (write(fd2, buf, resid) != (ssize_t)resid)
157 wdc_cap_diag(struct nvme_function *nf, int argc, char *argv[])
159 char path_tmpl[MAXPATHLEN];
163 while ((ch = getopt(argc, argv, "o:")) != -1) {
166 strlcpy(path_tmpl, optarg, MAXPATHLEN);
172 /* Check that a controller was specified. */
175 open_dev(argv[optind], &fd, 1, 1);
177 wdc_do_dump(fd, path_tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE,
178 WDC_NVME_CAP_DIAG_CMD, 4);
186 wdc(struct nvme_function *nf __unused, int argc, char *argv[])
189 DISPATCH(argc, argv, wdc);
193 * HGST's 0xc1 page. This is a grab bag of additional data. Please see
194 * https://www.hgst.com/sites/default/files/resources/US_SN150_ProdManual.pdf
195 * https://www.hgst.com/sites/default/files/resources/US_SN100_ProdManual.pdf
196 * Appendix A for details
199 typedef void (*subprint_fn_t)(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
207 static void print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
208 static void print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
209 static void print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
210 static void print_hgst_info_self_test(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
211 static void print_hgst_info_background_scan(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
212 static void print_hgst_info_erase_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
213 static void print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
214 static void print_hgst_info_temp_history(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
215 static void print_hgst_info_ssd_perf(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
216 static void print_hgst_info_firmware_load(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
218 static struct subpage_print hgst_subpage[] = {
219 { 0x02, print_hgst_info_write_errors },
220 { 0x03, print_hgst_info_read_errors },
221 { 0x05, print_hgst_info_verify_errors },
222 { 0x10, print_hgst_info_self_test },
223 { 0x15, print_hgst_info_background_scan },
224 { 0x30, print_hgst_info_erase_errors },
225 { 0x31, print_hgst_info_erase_counts },
226 { 0x32, print_hgst_info_temp_history },
227 { 0x37, print_hgst_info_ssd_perf },
228 { 0x38, print_hgst_info_firmware_load },
231 /* Print a subpage that is basically just key value pairs */
233 print_hgst_info_subpage_gen(void *buf, uint16_t subtype __unused, uint32_t size,
234 const struct kv_name *kv, size_t kv_count)
245 ptype = le16dec(wsp);
247 wsp++; /* Flags, just ignore */
250 for (i = 0; i < plen; i++)
251 param |= (uint64_t)*wsp++ << (i * 8);
252 printf(" %-30s: %jd\n", kv_lookup(kv, kv_count, ptype), (uintmax_t)param);
257 print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
259 static struct kv_name kv[] =
261 { 0x0000, "Corrected Without Delay" },
262 { 0x0001, "Corrected Maybe Delayed" },
263 { 0x0002, "Re-Writes" },
264 { 0x0003, "Errors Corrected" },
265 { 0x0004, "Correct Algorithm Used" },
266 { 0x0005, "Bytes Processed" },
267 { 0x0006, "Uncorrected Errors" },
268 { 0x8000, "Flash Write Commands" },
269 { 0x8001, "HGST Special" },
272 printf("Write Errors Subpage:\n");
273 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
277 print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
279 static struct kv_name kv[] =
281 { 0x0000, "Corrected Without Delay" },
282 { 0x0001, "Corrected Maybe Delayed" },
283 { 0x0002, "Re-Reads" },
284 { 0x0003, "Errors Corrected" },
285 { 0x0004, "Correct Algorithm Used" },
286 { 0x0005, "Bytes Processed" },
287 { 0x0006, "Uncorrected Errors" },
288 { 0x8000, "Flash Read Commands" },
289 { 0x8001, "XOR Recovered" },
290 { 0x8002, "Total Corrected Bits" },
293 printf("Read Errors Subpage:\n");
294 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
298 print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
300 static struct kv_name kv[] =
302 { 0x0000, "Corrected Without Delay" },
303 { 0x0001, "Corrected Maybe Delayed" },
304 { 0x0002, "Re-Reads" },
305 { 0x0003, "Errors Corrected" },
306 { 0x0004, "Correct Algorithm Used" },
307 { 0x0005, "Bytes Processed" },
308 { 0x0006, "Uncorrected Errors" },
309 { 0x8000, "Commands Processed" },
312 printf("Verify Errors Subpage:\n");
313 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
317 print_hgst_info_self_test(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
320 uint8_t *walker = buf;
324 printf("Self Test Subpage:\n");
325 for (i = 0; i < size / 20; i++) { /* Each entry is 20 bytes */
326 code = le16dec(walker);
328 walker++; /* Ignore fixed flags */
329 if (*walker == 0) /* Last entry is zero length */
331 if (*walker++ != 0x10) {
332 printf("Bad length for self test report\n");
335 printf(" %-30s: %d\n", "Recent Test", code);
336 printf(" %-28s: %#x\n", "Self-Test Results", *walker & 0xf);
337 printf(" %-28s: %#x\n", "Self-Test Code", (*walker >> 5) & 0x7);
339 printf(" %-28s: %#x\n", "Self-Test Number", *walker++);
340 hrs = le16dec(walker);
342 lba = le32dec(walker);
344 printf(" %-28s: %u\n", "Total Power On Hrs", hrs);
345 printf(" %-28s: %#jx (%jd)\n", "LBA", (uintmax_t)lba, (uintmax_t)lba);
346 printf(" %-28s: %#x\n", "Sense Key", *walker++ & 0xf);
347 printf(" %-28s: %#x\n", "Additional Sense Code", *walker++);
348 printf(" %-28s: %#x\n", "Additional Sense Qualifier", *walker++);
349 printf(" %-28s: %#x\n", "Vendor Specific Detail", *walker++);
354 print_hgst_info_background_scan(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
356 uint8_t *walker = buf;
358 uint16_t code, nscan, progress;
361 printf("Background Media Scan Subpage:\n");
362 /* Decode the header */
363 code = le16dec(walker);
365 walker++; /* Ignore fixed flags */
366 if (*walker++ != 0x10) {
367 printf("Bad length for background scan header\n");
371 printf("Expceted code 0, found code %#x\n", code);
374 pom = le32dec(walker);
376 walker++; /* Reserved */
378 nscan = le16dec(walker);
380 progress = le16dec(walker);
382 walker += 6; /* Reserved */
383 printf(" %-30s: %d\n", "Power On Minutes", pom);
384 printf(" %-30s: %x (%s)\n", "BMS Status", status,
385 status == 0 ? "idle" : (status == 1 ? "active" : (status == 8 ? "suspended" : "unknown")));
386 printf(" %-30s: %d\n", "Number of BMS", nscan);
387 printf(" %-30s: %d\n", "Progress Current BMS", progress);
388 /* Report retirements */
389 if (walker - (uint8_t *)buf != 20) {
390 printf("Coding error, offset not 20\n");
394 printf(" %-30s: %d\n", "BMS retirements", size / 0x18);
396 code = le16dec(walker);
399 if (*walker++ != 0x14) {
400 printf("Bad length parameter\n");
403 pom = le32dec(walker);
406 * Spec sheet says the following are hard coded, if true, just
407 * print the NAND retirement.
409 if (walker[0] == 0x41 &&
418 walker += 4; /* Skip reserved */
419 nand = le32dec(walker);
421 printf(" %-30s: %d\n", "Retirement number", code);
422 printf(" %-28s: %#x\n", "NAND (C/T)BBBPPP", nand);
424 printf("Parameter %#x entry corrupt\n", code);
431 print_hgst_info_erase_errors(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
433 static struct kv_name kv[] =
435 { 0x0000, "Corrected Without Delay" },
436 { 0x0001, "Corrected Maybe Delayed" },
437 { 0x0002, "Re-Erase" },
438 { 0x0003, "Errors Corrected" },
439 { 0x0004, "Correct Algorithm Used" },
440 { 0x0005, "Bytes Processed" },
441 { 0x0006, "Uncorrected Errors" },
442 { 0x8000, "Flash Erase Commands" },
443 { 0x8001, "Mfg Defect Count" },
444 { 0x8002, "Grown Defect Count" },
445 { 0x8003, "Erase Count -- User" },
446 { 0x8004, "Erase Count -- System" },
449 printf("Erase Errors Subpage:\n");
450 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
454 print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
456 /* My drive doesn't export this -- so not coding up */
457 printf("XXX: Erase counts subpage: %p, %#x %d\n", buf, subtype, size);
461 print_hgst_info_temp_history(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
463 uint8_t *walker = buf;
466 printf("Temperature History:\n");
467 printf(" %-30s: %d C\n", "Current Temperature", *walker++);
468 printf(" %-30s: %d C\n", "Reference Temperature", *walker++);
469 printf(" %-30s: %d C\n", "Maximum Temperature", *walker++);
470 printf(" %-30s: %d C\n", "Minimum Temperature", *walker++);
471 min = le32dec(walker);
473 printf(" %-30s: %d:%02d:00\n", "Max Temperature Time", min / 60, min % 60);
474 min = le32dec(walker);
476 printf(" %-30s: %d:%02d:00\n", "Over Temperature Duration", min / 60, min % 60);
477 min = le32dec(walker);
479 printf(" %-30s: %d:%02d:00\n", "Min Temperature Time", min / 60, min % 60);
483 print_hgst_info_ssd_perf(void *buf, uint16_t subtype __unused, uint8_t res, uint32_t size __unused)
485 uint8_t *walker = buf;
488 printf("SSD Performance Subpage Type %d:\n", res);
489 val = le64dec(walker);
491 printf(" %-30s: %ju\n", "Host Read Commands", val);
492 val = le64dec(walker);
494 printf(" %-30s: %ju\n", "Host Read Blocks", val);
495 val = le64dec(walker);
497 printf(" %-30s: %ju\n", "Host Cache Read Hits Commands", val);
498 val = le64dec(walker);
500 printf(" %-30s: %ju\n", "Host Cache Read Hits Blocks", val);
501 val = le64dec(walker);
503 printf(" %-30s: %ju\n", "Host Read Commands Stalled", val);
504 val = le64dec(walker);
506 printf(" %-30s: %ju\n", "Host Write Commands", val);
507 val = le64dec(walker);
509 printf(" %-30s: %ju\n", "Host Write Blocks", val);
510 val = le64dec(walker);
512 printf(" %-30s: %ju\n", "Host Write Odd Start Commands", val);
513 val = le64dec(walker);
515 printf(" %-30s: %ju\n", "Host Write Odd End Commands", val);
516 val = le64dec(walker);
518 printf(" %-30s: %ju\n", "Host Write Commands Stalled", val);
519 val = le64dec(walker);
521 printf(" %-30s: %ju\n", "NAND Read Commands", val);
522 val = le64dec(walker);
524 printf(" %-30s: %ju\n", "NAND Read Blocks", val);
525 val = le64dec(walker);
527 printf(" %-30s: %ju\n", "NAND Write Commands", val);
528 val = le64dec(walker);
530 printf(" %-30s: %ju\n", "NAND Write Blocks", val);
531 val = le64dec(walker);
533 printf(" %-30s: %ju\n", "NAND Read Before Writes", val);
537 print_hgst_info_firmware_load(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
539 uint8_t *walker = buf;
541 printf("Firmware Load Subpage:\n");
542 printf(" %-30s: %d\n", "Firmware Downloads", le32dec(walker));
546 kv_indirect(void *buf, uint32_t subtype, uint8_t res, uint32_t size, struct subpage_print *sp, size_t nsp)
550 for (i = 0; i < nsp; i++, sp++) {
551 if (sp->key == subtype) {
552 sp->fn(buf, subtype, res, size);
556 printf("No handler for page type %x\n", subtype);
560 print_hgst_info_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
562 uint8_t *walker, *end, *subpage;
565 uint8_t subtype, res;
567 printf("HGST Extra Info Log\n");
568 printf("===================\n");
573 len = le16dec(walker);
575 end = walker + len; /* Length is exclusive of this header */
577 while (walker < end) {
578 subpage = walker + 4;
579 subtype = *walker++ & 0x3f; /* subtype */
580 res = *walker++; /* Reserved */
581 len = le16dec(walker);
582 walker += len + 2; /* Length, not incl header */
584 printf("Ooops! Off the end of the list\n");
587 kv_indirect(subpage, subtype, res, len, hgst_subpage, nitems(hgst_subpage));
591 NVME_LOGPAGE(hgst_info,
592 HGST_INFO_LOG, "hgst", "Detailed Health/SMART",
593 print_hgst_info_log, DEFAULT_SIZE);
594 NVME_LOGPAGE(wdc_info,
595 HGST_INFO_LOG, "wdc", "Detailed Health/SMART",
596 print_hgst_info_log, DEFAULT_SIZE);
597 NVME_LOGPAGE(wds_info,
598 HGST_INFO_LOG, "wds", "Detailed Health/SMART",
599 print_hgst_info_log, DEFAULT_SIZE);
600 NVME_COMMAND(top, wdc, wdc, WDC_USAGE);