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