1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2017 Netflix, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/ioccom.h>
30 #include <sys/endian.h>
31
32 #include <ctype.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41 #include <stdbool.h>
42
43 #include "nvmecontrol.h"
44
45 /* Tables for command line parsing */
46
47 static cmd_fn_t wdc;
48 static cmd_fn_t wdc_cap_diag;
49
50 #define NONE 0xffffffffu
51 #define NONE64 0xffffffffffffffffull
52 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
53 #define OPT_END { NULL, 0, arg_none, NULL, NULL }
54
55 static struct cmd wdc_cmd = {
56 .name = "wdc", .fn = wdc, .descr = "wdc vendor specific commands", .ctx_size = 0, .opts = NULL, .args = NULL,
57 };
58
59 CMD_COMMAND(wdc_cmd);
60
61 static struct options
62 {
63 const char *template;
64 const char *dev;
65 uint8_t data_area;
66 } opt = {
67 .template = NULL,
68 .dev = NULL,
69 .data_area = 0,
70 };
71
72 static const struct opts opts[] = {
73 OPT("template", 'o', arg_string, opt, template,
74 "Template for paths to use for different logs"),
75 OPT("data-area", 'd', arg_uint8, opt, data_area,
76 "Data-area to retrieve up to"),
77 OPT_END
78 };
79
80 static const struct args args[] = {
81 { arg_string, &opt.dev, "controller-id" },
82 { arg_none, NULL, NULL },
83 };
84
85 static struct cmd cap_diag_cmd = {
86 .name = "cap-diag",
87 .fn = wdc_cap_diag,
88 .descr = "Retrieve the cap-diag logs from the drive",
89 .ctx_size = sizeof(struct options),
90 .opts = opts,
91 .args = args,
92 };
93
94 CMD_SUBCOMMAND(wdc_cmd, cap_diag_cmd);
95
96 #define WDC_NVME_VID 0x1c58
97 #define WDC_NVME_VID_2 0x1b96
98 #define WDC_NVME_VID_3 0x15b7
99
100 #define WDC_NVME_TOC_SIZE 0x8
101 #define WDC_NVME_LOG_SIZE_HDR_LEN 0x8
102 #define WDC_NVME_CAP_DIAG_OPCODE_E6 0xe6
103 #define WDC_NVME_CAP_DIAG_CMD 0x0000
104 #define WDC_NVME_CAP_DIAG_OPCODE_FA 0xfa
105 #define WDC_NVME_DUI_MAX_SECTIONS_V0 0x3c
106 #define WDC_NVME_DUI_MAX_SECTIONS_V1 0x3a
107 #define WDC_NVME_DUI_MAX_SECTIONS_V2 0x26
108 #define WDC_NVME_DUI_MAX_SECTIONS_V3 0x23
109
110 typedef enum wdc_dui_header {
111 WDC_DUI_HEADER_VER_0 = 0,
112 WDC_DUI_HEADER_VER_1,
113 WDC_DUI_HEADER_VER_2,
114 WDC_DUI_HEADER_VER_3,
115 } wdc_dui_header;
116
117 static void
wdc_append_serial_name(int fd,char * buf,size_t len,const char * suffix)118 wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix)
119 {
120 struct nvme_controller_data cdata;
121 char sn[NVME_SERIAL_NUMBER_LENGTH + 1];
122 char *walker;
123
124 len -= strlen(buf);
125 buf += strlen(buf);
126 if (read_controller_data(fd, &cdata))
127 errx(EX_IOERR, "Identify request failed");
128 memcpy(sn, cdata.sn, NVME_SERIAL_NUMBER_LENGTH);
129 walker = sn + NVME_SERIAL_NUMBER_LENGTH - 1;
130 while (walker > sn && *walker == ' ')
131 walker--;
132 *++walker = '\0';
133 snprintf(buf, len, "_%s_%s.bin", sn, suffix);
134 }
135
136 static void
wdc_get_data(int fd,uint32_t opcode,uint32_t len,uint32_t off,uint32_t cmd,uint8_t * buffer,size_t buflen,bool e6lg_flag)137 wdc_get_data(int fd, uint32_t opcode, uint32_t len, uint32_t off, uint32_t cmd,
138 uint8_t *buffer, size_t buflen, bool e6lg_flag)
139 {
140 struct nvme_pt_command pt;
141
142 memset(&pt, 0, sizeof(pt));
143 pt.cmd.opc = opcode;
144 pt.cmd.cdw10 = htole32(len / sizeof(uint32_t));
145 pt.cmd.cdw12 = htole32(cmd);
146 if (e6lg_flag)
147 pt.cmd.cdw11 = htole32(off / sizeof(uint32_t));
148 else
149 pt.cmd.cdw13 = htole32(off / sizeof(uint32_t));
150 pt.buf = buffer;
151 pt.len = buflen;
152 pt.is_read = 1;
153
154 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
155 err(EX_IOERR, "wdc_get_data request failed");
156 if (nvme_completion_is_error(&pt.cpl))
157 errx(EX_IOERR, "wdc_get_data request returned error");
158 }
159
160 static void
wdc_do_dump_e6(int fd,char * tmpl,const char * suffix,uint32_t opcode,uint32_t cmd,int len_off)161 wdc_do_dump_e6(int fd, char *tmpl, const char *suffix, uint32_t opcode,
162 uint32_t cmd, int len_off)
163 {
164 int first;
165 int fd2;
166 uint8_t *buf, *hdr;
167 uint64_t max_xfer_size;
168 uint32_t len, offset;
169 size_t resid;
170 bool e6lg_flag = false;
171
172 wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
173
174 /* Read Log Dump header */
175 len = WDC_NVME_LOG_SIZE_HDR_LEN;
176 offset = 0;
177 hdr = malloc(len);
178 if (hdr == NULL)
179 errx(EX_OSERR, "Can't get buffer to read dump");
180 wdc_get_data(fd, opcode, len, offset, cmd, hdr, len, false);
181 if (memcmp("E6LG", hdr, 4) == 0) {
182 e6lg_flag = true;
183 }
184
185 /* XXX overwrite protection? */
186 fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC, 0644);
187 if (fd2 < 0)
188 err(EX_CANTCREAT, "open %s", tmpl);
189 if (ioctl(fd, NVME_GET_MAX_XFER_SIZE, &max_xfer_size) < 0)
190 err(EX_IOERR, "query max transfer size failed");
191 buf = aligned_alloc(PAGE_SIZE, max_xfer_size);
192 if (buf == NULL)
193 errx(EX_OSERR, "Can't get buffer to read dump");
194 offset = 0;
195 len = max_xfer_size;
196 first = 1;
197
198 do {
199 resid = MIN(len, max_xfer_size);
200 wdc_get_data(fd, opcode, resid, offset, cmd, buf, resid, e6lg_flag);
201
202 if (first) {
203 len = be32dec(buf + len_off);
204 if (len == 0)
205 errx(EX_PROTOCOL, "No data for %s", suffix);
206
207 printf("Dumping %d bytes of version %d.%d log to %s\n", len,
208 buf[8], buf[9], tmpl);
209 /*
210 * Adjust amount to dump if total dump < 1MB,
211 * though it likely doesn't matter to the WDC
212 * analysis tools.
213 */
214 if (resid > len)
215 resid = len;
216 first = 0;
217 }
218 if (write(fd2, buf, resid) != (ssize_t)resid)
219 err(EX_IOERR, "write");
220 offset += resid;
221 len -= resid;
222 } while (len > 0);
223 free(hdr);
224 free(buf);
225 close(fd2);
226 }
227
228 static void
wdc_get_data_dui(int fd,uint32_t opcode,uint32_t len,uint64_t off,uint8_t * buffer,size_t buflen)229 wdc_get_data_dui(int fd, uint32_t opcode, uint32_t len, uint64_t off,
230 uint8_t *buffer, size_t buflen)
231 {
232 struct nvme_pt_command pt;
233
234 memset(&pt, 0, sizeof(pt));
235 pt.cmd.opc = opcode;
236 pt.cmd.nsid = NONE;
237 pt.cmd.cdw10 = htole32((len / sizeof(uint32_t)) - 1) ;
238 pt.cmd.cdw12 = htole32(off & 0xFFFFFFFFu);
239 pt.cmd.cdw13 = htole32(off >> 32);
240 pt.buf = buffer;
241 pt.len = buflen;
242 pt.is_read = 1;
243
244 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
245 err(EX_IOERR, "wdc_get_data_dui request failed");
246 if (nvme_completion_is_error(&pt.cpl))
247 errx(EX_IOERR, "wdc_get_data_dui request returned error");
248 }
249
250 static uint8_t
wdc_get_dui_max_sections(uint16_t header_ver)251 wdc_get_dui_max_sections(uint16_t header_ver)
252 {
253 switch (header_ver) {
254 case WDC_DUI_HEADER_VER_0:
255 return WDC_NVME_DUI_MAX_SECTIONS_V0;
256 case WDC_DUI_HEADER_VER_1:
257 return WDC_NVME_DUI_MAX_SECTIONS_V1;
258 case WDC_DUI_HEADER_VER_2:
259 return WDC_NVME_DUI_MAX_SECTIONS_V2;
260 case WDC_DUI_HEADER_VER_3:
261 return WDC_NVME_DUI_MAX_SECTIONS_V3;
262 }
263 return 0;
264 }
265
266 static void
wdc_get_dui_log_size(int fd,uint32_t opcode,uint8_t data_area,uint64_t * log_size,int len_off)267 wdc_get_dui_log_size(int fd, uint32_t opcode, uint8_t data_area,
268 uint64_t *log_size, int len_off)
269 {
270 uint8_t *hdr, *tofree;
271 uint8_t max_sections;
272 int i, j;
273 uint16_t hdr_ver;
274 uint16_t len;
275 uint64_t dui_size;
276
277 dui_size = 0;
278 len = 1024;
279 tofree = hdr = (uint8_t*)malloc(len);
280 if (hdr == NULL)
281 errx(EX_OSERR, "Can't get buffer to read header");
282 wdc_get_data_dui(fd, opcode, len, 0, hdr, len);
283
284 hdr += len_off;
285 hdr_ver = ((*hdr & 0xF) != 0)? *hdr : le16dec(hdr);
286 max_sections = wdc_get_dui_max_sections(hdr_ver);
287
288 if (hdr_ver == 0 || hdr_ver == 1) {
289 dui_size = (uint64_t)le32dec(hdr + 4);
290 if (dui_size == 0) {
291 hdr += 8;
292 for (i = 0, j = 0; i < (int)max_sections; i++, j+=8)
293 dui_size += (uint64_t)le32dec(hdr + j + 4);
294 }
295 } else if (hdr_ver == 2 || hdr_ver == 3) {
296 if (data_area == 0) {
297 dui_size = le64dec(hdr + 4);
298 if (dui_size == 0) {
299 hdr += 12;
300 for (i = 0, j = 0 ; i < (int)max_sections; i++, j+=12)
301 dui_size += le64dec(hdr + j + 4);
302 }
303 } else {
304 hdr += 12;
305 for (i = 0, j = 0; i < (int)max_sections; i++, j+=12) {
306 if (le16dec(hdr + j + 2) <= data_area)
307 dui_size += le64dec(hdr + j + 4);
308 else
309 break;
310 }
311 }
312 }
313 else
314 errx(EX_PROTOCOL, "ERROR : No valid header ");
315
316 *log_size = dui_size;
317 free(tofree);
318 }
319
320 static void
wdc_do_dump_dui(int fd,char * tmpl,uint8_t data_area,const char * suffix,uint32_t opcode,int len_off)321 wdc_do_dump_dui(int fd, char *tmpl, uint8_t data_area,
322 const char *suffix, uint32_t opcode, int len_off)
323 {
324 int fd2, first;
325 uint8_t *buf;
326 uint64_t max_xfer_size;
327 uint16_t hdr_ver;
328 uint64_t log_len, offset;
329 size_t resid;
330
331 wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
332 wdc_get_dui_log_size(fd, opcode, data_area, &log_len, len_off);
333 if (log_len == 0)
334 errx(EX_PROTOCOL, "No data for %s", suffix);
335 fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC, 0644);
336 if (fd2 < 0)
337 err(EX_CANTCREAT, "open %s", tmpl);
338 if (ioctl(fd, NVME_GET_MAX_XFER_SIZE, &max_xfer_size) < 0)
339 err(EX_IOERR, "query max transfer size failed");
340 buf = aligned_alloc(PAGE_SIZE, max_xfer_size);
341 if (buf == NULL)
342 errx(EX_OSERR, "Can't get buffer to read dump");
343 offset = 0;
344 first = 1;
345
346 while (log_len > 0) {
347 resid = MIN(log_len, max_xfer_size);
348 wdc_get_data_dui(fd, opcode, resid, offset, buf, resid);
349 if (first) {
350 hdr_ver = ((buf[len_off] & 0xF) != 0) ?
351 (buf[len_off]) : (le16dec(buf + len_off));
352 printf("Dumping %jd bytes of version %d log to %s\n",
353 (uintmax_t)log_len, hdr_ver, tmpl);
354 first = 0;
355 }
356 if (write(fd2, buf, resid) != (ssize_t)resid)
357 err(EX_IOERR, "write");
358 offset += resid;
359 log_len -= resid;
360 }
361
362 free(buf);
363 close(fd2);
364 }
365
366 static void
wdc_cap_diag(const struct cmd * f,int argc,char * argv[])367 wdc_cap_diag(const struct cmd *f, int argc, char *argv[])
368 {
369 char tmpl[MAXPATHLEN];
370 int fd;
371 struct nvme_controller_data cdata;
372 uint32_t vid;
373
374 if (arg_parse(argc, argv, f))
375 return;
376 if (opt.template == NULL) {
377 fprintf(stderr, "Missing template arg.\n");
378 arg_help(argc, argv, f);
379 }
380 if (opt.data_area > 4) {
381 fprintf(stderr, "Data area range 1-4, supplied %d.\n", opt.data_area);
382 arg_help(argc, argv, f);
383 }
384 strlcpy(tmpl, opt.template, sizeof(tmpl));
385 open_dev(opt.dev, &fd, 1, 1);
386 if (read_controller_data(fd, &cdata))
387 errx(EX_IOERR, "Identify request failed");
388 vid = cdata.vid;
389
390 switch (vid) {
391 case WDC_NVME_VID :
392 case WDC_NVME_VID_2 :
393 wdc_do_dump_e6(fd, tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE_E6,
394 WDC_NVME_CAP_DIAG_CMD, 4);
395 break;
396 case WDC_NVME_VID_3 :
397 wdc_do_dump_dui(fd, tmpl, opt.data_area, "cap_diag",
398 WDC_NVME_CAP_DIAG_OPCODE_FA, 512);
399 break;
400 default:
401 errx(EX_UNAVAILABLE, "ERROR : WDC: unsupported device (%#x) for this command", vid);
402 }
403 close(fd);
404 exit(0);
405 }
406
407 static void
wdc(const struct cmd * nf __unused,int argc,char * argv[])408 wdc(const struct cmd *nf __unused, int argc, char *argv[])
409 {
410
411 cmd_dispatch(argc, argv, &wdc_cmd);
412 }
413
414 /*
415 * HGST's 0xc1 page. This is a grab bag of additional data. Please see
416 * https://www.hgst.com/sites/default/files/resources/US_SN150_ProdManual.pdf
417 * https://www.hgst.com/sites/default/files/resources/US_SN100_ProdManual.pdf
418 * Appendix A for details
419 */
420
421 typedef void (*subprint_fn_t)(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
422
423 struct subpage_print
424 {
425 uint16_t key;
426 subprint_fn_t fn;
427 };
428
429 static void print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
430 static void print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
431 static void print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
432 static void print_hgst_info_self_test(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
433 static void print_hgst_info_background_scan(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
434 static void print_hgst_info_erase_errors(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
435 static void print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
436 static void print_hgst_info_temp_history(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
437 static void print_hgst_info_ssd_perf(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
438 static void print_hgst_info_firmware_load(void *buf, uint16_t subtype, uint8_t res, uint32_t size);
439
440 static struct subpage_print hgst_subpage[] = {
441 { 0x02, print_hgst_info_write_errors },
442 { 0x03, print_hgst_info_read_errors },
443 { 0x05, print_hgst_info_verify_errors },
444 { 0x10, print_hgst_info_self_test },
445 { 0x15, print_hgst_info_background_scan },
446 { 0x30, print_hgst_info_erase_errors },
447 { 0x31, print_hgst_info_erase_counts },
448 { 0x32, print_hgst_info_temp_history },
449 { 0x37, print_hgst_info_ssd_perf },
450 { 0x38, print_hgst_info_firmware_load },
451 };
452
453 /* Print a subpage that is basically just key value pairs */
454 static void
print_hgst_info_subpage_gen(void * buf,uint16_t subtype __unused,uint32_t size,const struct kv_name * kv,size_t kv_count)455 print_hgst_info_subpage_gen(void *buf, uint16_t subtype __unused, uint32_t size,
456 const struct kv_name *kv, size_t kv_count)
457 {
458 uint8_t *wsp, *esp;
459 uint16_t ptype;
460 uint8_t plen;
461 uint64_t param;
462 int i;
463
464 wsp = buf;
465 esp = wsp + size;
466 while (wsp < esp) {
467 ptype = le16dec(wsp);
468 wsp += 2;
469 wsp++; /* Flags, just ignore */
470 plen = *wsp++;
471 param = 0;
472 for (i = 0; i < plen && wsp < esp; i++)
473 param |= (uint64_t)*wsp++ << (i * 8);
474 printf(" %-30s: %jd\n", kv_lookup(kv, kv_count, ptype), (uintmax_t)param);
475 }
476 }
477
478 static void
print_hgst_info_write_errors(void * buf,uint16_t subtype,uint8_t res __unused,uint32_t size)479 print_hgst_info_write_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
480 {
481 static struct kv_name kv[] =
482 {
483 { 0x0000, "Corrected Without Delay" },
484 { 0x0001, "Corrected Maybe Delayed" },
485 { 0x0002, "Re-Writes" },
486 { 0x0003, "Errors Corrected" },
487 { 0x0004, "Correct Algorithm Used" },
488 { 0x0005, "Bytes Processed" },
489 { 0x0006, "Uncorrected Errors" },
490 { 0x8000, "Flash Write Commands" },
491 { 0x8001, "HGST Special" },
492 };
493
494 printf("Write Errors Subpage:\n");
495 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
496 }
497
498 static void
print_hgst_info_read_errors(void * buf,uint16_t subtype,uint8_t res __unused,uint32_t size)499 print_hgst_info_read_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
500 {
501 static struct kv_name kv[] =
502 {
503 { 0x0000, "Corrected Without Delay" },
504 { 0x0001, "Corrected Maybe Delayed" },
505 { 0x0002, "Re-Reads" },
506 { 0x0003, "Errors Corrected" },
507 { 0x0004, "Correct Algorithm Used" },
508 { 0x0005, "Bytes Processed" },
509 { 0x0006, "Uncorrected Errors" },
510 { 0x8000, "Flash Read Commands" },
511 { 0x8001, "XOR Recovered" },
512 { 0x8002, "Total Corrected Bits" },
513 };
514
515 printf("Read Errors Subpage:\n");
516 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
517 }
518
519 static void
print_hgst_info_verify_errors(void * buf,uint16_t subtype,uint8_t res __unused,uint32_t size)520 print_hgst_info_verify_errors(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
521 {
522 static struct kv_name kv[] =
523 {
524 { 0x0000, "Corrected Without Delay" },
525 { 0x0001, "Corrected Maybe Delayed" },
526 { 0x0002, "Re-Reads" },
527 { 0x0003, "Errors Corrected" },
528 { 0x0004, "Correct Algorithm Used" },
529 { 0x0005, "Bytes Processed" },
530 { 0x0006, "Uncorrected Errors" },
531 { 0x8000, "Commands Processed" },
532 };
533
534 printf("Verify Errors Subpage:\n");
535 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
536 }
537
538 static void
print_hgst_info_self_test(void * buf,uint16_t subtype __unused,uint8_t res __unused,uint32_t size)539 print_hgst_info_self_test(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
540 {
541 size_t i;
542 uint8_t *walker = buf;
543 uint16_t code, hrs;
544 uint32_t lba;
545
546 printf("Self Test Subpage:\n");
547 for (i = 0; i < size / 20; i++) { /* Each entry is 20 bytes */
548 code = le16dec(walker);
549 walker += 2;
550 walker++; /* Ignore fixed flags */
551 if (*walker == 0) /* Last entry is zero length */
552 break;
553 if (*walker++ != 0x10) {
554 printf("Bad length for self test report\n");
555 return;
556 }
557 printf(" %-30s: %d\n", "Recent Test", code);
558 printf(" %-28s: %#x\n", "Self-Test Results", *walker & 0xf);
559 printf(" %-28s: %#x\n", "Self-Test Code", (*walker >> 5) & 0x7);
560 walker++;
561 printf(" %-28s: %#x\n", "Self-Test Number", *walker++);
562 hrs = le16dec(walker);
563 walker += 2;
564 lba = le32dec(walker);
565 walker += 4;
566 printf(" %-28s: %u\n", "Total Power On Hrs", hrs);
567 printf(" %-28s: %#jx (%jd)\n", "LBA", (uintmax_t)lba, (uintmax_t)lba);
568 printf(" %-28s: %#x\n", "Sense Key", *walker++ & 0xf);
569 printf(" %-28s: %#x\n", "Additional Sense Code", *walker++);
570 printf(" %-28s: %#x\n", "Additional Sense Qualifier", *walker++);
571 printf(" %-28s: %#x\n", "Vendor Specific Detail", *walker++);
572 }
573 }
574
575 static void
print_hgst_info_background_scan(void * buf,uint16_t subtype __unused,uint8_t res __unused,uint32_t size)576 print_hgst_info_background_scan(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
577 {
578 uint8_t *walker = buf;
579 uint8_t status;
580 uint16_t code, nscan, progress;
581 uint32_t pom, nand;
582
583 printf("Background Media Scan Subpage:\n");
584 /* Decode the header */
585 code = le16dec(walker);
586 walker += 2;
587 walker++; /* Ignore fixed flags */
588 if (*walker++ != 0x10) {
589 printf("Bad length for background scan header\n");
590 return;
591 }
592 if (code != 0) {
593 printf("Expected code 0, found code %#x\n", code);
594 return;
595 }
596 pom = le32dec(walker);
597 walker += 4;
598 walker++; /* Reserved */
599 status = *walker++;
600 nscan = le16dec(walker);
601 walker += 2;
602 progress = le16dec(walker);
603 walker += 2;
604 walker += 6; /* Reserved */
605 printf(" %-30s: %d\n", "Power On Minutes", pom);
606 printf(" %-30s: %x (%s)\n", "BMS Status", status,
607 status == 0 ? "idle" : (status == 1 ? "active" : (status == 8 ? "suspended" : "unknown")));
608 printf(" %-30s: %d\n", "Number of BMS", nscan);
609 printf(" %-30s: %d\n", "Progress Current BMS", progress);
610 /* Report retirements */
611 if (walker - (uint8_t *)buf != 20) {
612 printf("Coding error, offset not 20\n");
613 return;
614 }
615 size -= 20;
616 printf(" %-30s: %d\n", "BMS retirements", size / 0x18);
617 while (size > 0) {
618 code = le16dec(walker);
619 walker += 2;
620 walker++;
621 if (*walker++ != 0x14) {
622 printf("Bad length parameter\n");
623 return;
624 }
625 pom = le32dec(walker);
626 walker += 4;
627 /*
628 * Spec sheet says the following are hard coded, if true, just
629 * print the NAND retirement.
630 */
631 if (walker[0] == 0x41 &&
632 walker[1] == 0x0b &&
633 walker[2] == 0x01 &&
634 walker[3] == 0x00 &&
635 walker[4] == 0x00 &&
636 walker[5] == 0x00 &&
637 walker[6] == 0x00 &&
638 walker[7] == 0x00) {
639 walker += 8;
640 walker += 4; /* Skip reserved */
641 nand = le32dec(walker);
642 walker += 4;
643 printf(" %-30s: %d\n", "Retirement number", code);
644 printf(" %-28s: %#x\n", "NAND (C/T)BBBPPP", nand);
645 } else {
646 printf("Parameter %#x entry corrupt\n", code);
647 walker += 16;
648 }
649 }
650 }
651
652 static void
print_hgst_info_erase_errors(void * buf,uint16_t subtype __unused,uint8_t res __unused,uint32_t size)653 print_hgst_info_erase_errors(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size)
654 {
655 static struct kv_name kv[] =
656 {
657 { 0x0000, "Corrected Without Delay" },
658 { 0x0001, "Corrected Maybe Delayed" },
659 { 0x0002, "Re-Erase" },
660 { 0x0003, "Errors Corrected" },
661 { 0x0004, "Correct Algorithm Used" },
662 { 0x0005, "Bytes Processed" },
663 { 0x0006, "Uncorrected Errors" },
664 { 0x8000, "Flash Erase Commands" },
665 { 0x8001, "Mfg Defect Count" },
666 { 0x8002, "Grown Defect Count" },
667 { 0x8003, "Erase Count -- User" },
668 { 0x8004, "Erase Count -- System" },
669 };
670
671 printf("Erase Errors Subpage:\n");
672 print_hgst_info_subpage_gen(buf, subtype, size, kv, nitems(kv));
673 }
674
675 static void
print_hgst_info_erase_counts(void * buf,uint16_t subtype,uint8_t res __unused,uint32_t size)676 print_hgst_info_erase_counts(void *buf, uint16_t subtype, uint8_t res __unused, uint32_t size)
677 {
678 /* My drive doesn't export this -- so not coding up */
679 printf("XXX: Erase counts subpage: %p, %#x %d\n", buf, subtype, size);
680 }
681
682 static void
print_hgst_info_temp_history(void * buf,uint16_t subtype __unused,uint8_t res __unused,uint32_t size __unused)683 print_hgst_info_temp_history(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
684 {
685 uint8_t *walker = buf;
686 uint32_t min;
687
688 printf("Temperature History:\n");
689 printf(" %-30s: %d C\n", "Current Temperature", *walker++);
690 printf(" %-30s: %d C\n", "Reference Temperature", *walker++);
691 printf(" %-30s: %d C\n", "Maximum Temperature", *walker++);
692 printf(" %-30s: %d C\n", "Minimum Temperature", *walker++);
693 min = le32dec(walker);
694 walker += 4;
695 printf(" %-30s: %d:%02d:00\n", "Max Temperature Time", min / 60, min % 60);
696 min = le32dec(walker);
697 walker += 4;
698 printf(" %-30s: %d:%02d:00\n", "Over Temperature Duration", min / 60, min % 60);
699 min = le32dec(walker);
700 walker += 4;
701 printf(" %-30s: %d:%02d:00\n", "Min Temperature Time", min / 60, min % 60);
702 }
703
704 static void
print_hgst_info_ssd_perf(void * buf,uint16_t subtype __unused,uint8_t res,uint32_t size __unused)705 print_hgst_info_ssd_perf(void *buf, uint16_t subtype __unused, uint8_t res, uint32_t size __unused)
706 {
707 uint8_t *walker = buf;
708 uint64_t val;
709
710 printf("SSD Performance Subpage Type %d:\n", res);
711 val = le64dec(walker);
712 walker += 8;
713 printf(" %-30s: %ju\n", "Host Read Commands", val);
714 val = le64dec(walker);
715 walker += 8;
716 printf(" %-30s: %ju\n", "Host Read Blocks", val);
717 val = le64dec(walker);
718 walker += 8;
719 printf(" %-30s: %ju\n", "Host Cache Read Hits Commands", val);
720 val = le64dec(walker);
721 walker += 8;
722 printf(" %-30s: %ju\n", "Host Cache Read Hits Blocks", val);
723 val = le64dec(walker);
724 walker += 8;
725 printf(" %-30s: %ju\n", "Host Read Commands Stalled", val);
726 val = le64dec(walker);
727 walker += 8;
728 printf(" %-30s: %ju\n", "Host Write Commands", val);
729 val = le64dec(walker);
730 walker += 8;
731 printf(" %-30s: %ju\n", "Host Write Blocks", val);
732 val = le64dec(walker);
733 walker += 8;
734 printf(" %-30s: %ju\n", "Host Write Odd Start Commands", val);
735 val = le64dec(walker);
736 walker += 8;
737 printf(" %-30s: %ju\n", "Host Write Odd End Commands", val);
738 val = le64dec(walker);
739 walker += 8;
740 printf(" %-30s: %ju\n", "Host Write Commands Stalled", val);
741 val = le64dec(walker);
742 walker += 8;
743 printf(" %-30s: %ju\n", "NAND Read Commands", val);
744 val = le64dec(walker);
745 walker += 8;
746 printf(" %-30s: %ju\n", "NAND Read Blocks", val);
747 val = le64dec(walker);
748 walker += 8;
749 printf(" %-30s: %ju\n", "NAND Write Commands", val);
750 val = le64dec(walker);
751 walker += 8;
752 printf(" %-30s: %ju\n", "NAND Write Blocks", val);
753 val = le64dec(walker);
754 walker += 8;
755 printf(" %-30s: %ju\n", "NAND Read Before Writes", val);
756 }
757
758 static void
print_hgst_info_firmware_load(void * buf,uint16_t subtype __unused,uint8_t res __unused,uint32_t size __unused)759 print_hgst_info_firmware_load(void *buf, uint16_t subtype __unused, uint8_t res __unused, uint32_t size __unused)
760 {
761 uint8_t *walker = buf;
762
763 printf("Firmware Load Subpage:\n");
764 printf(" %-30s: %d\n", "Firmware Downloads", le32dec(walker));
765 }
766
767 static void
kv_indirect(void * buf,uint32_t subtype,uint8_t res,uint32_t size,struct subpage_print * sp,size_t nsp)768 kv_indirect(void *buf, uint32_t subtype, uint8_t res, uint32_t size, struct subpage_print *sp, size_t nsp)
769 {
770 size_t i;
771
772 for (i = 0; i < nsp; i++, sp++) {
773 if (sp->key == subtype) {
774 sp->fn(buf, subtype, res, size);
775 return;
776 }
777 }
778 printf("No handler for page type %x\n", subtype);
779 }
780
781 static void
print_hgst_info_log(const struct nvme_controller_data * cdata __unused,void * buf,uint32_t size __unused)782 print_hgst_info_log(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused)
783 {
784 uint8_t *walker, *end, *subpage;
785 uint16_t len;
786 uint8_t subtype, res;
787
788 printf("HGST Extra Info Log\n");
789 printf("===================\n");
790
791 walker = buf;
792 walker += 2; /* Page count */
793 len = le16dec(walker);
794 walker += 2;
795 end = walker + len; /* Length is exclusive of this header */
796
797 while (walker < end) {
798 subpage = walker + 4;
799 subtype = *walker++ & 0x3f; /* subtype */
800 res = *walker++; /* Reserved */
801 len = le16dec(walker);
802 walker += len + 2; /* Length, not incl header */
803 if (walker > end) {
804 printf("Ooops! Off the end of the list\n");
805 break;
806 }
807 kv_indirect(subpage, subtype, res, len, hgst_subpage, nitems(hgst_subpage));
808 }
809 }
810
811 NVME_LOGPAGE(hgst_info,
812 HGST_INFO_LOG, "hgst", "Detailed Health/SMART",
813 print_hgst_info_log, DEFAULT_SIZE);
814 NVME_LOGPAGE(wdc_info,
815 HGST_INFO_LOG, "wdc", "Detailed Health/SMART",
816 print_hgst_info_log, DEFAULT_SIZE);
817