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