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