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