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