2 * Copyright (c) 2017 Netflix, Inc
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
32 #include <sys/endian.h>
43 #include "nvmecontrol.h"
45 #define WDC_NVME_TOC_SIZE 8
47 #define WDC_NVME_CAP_DIAG_OPCODE 0xe6
48 #define WDC_NVME_CAP_DIAG_CMD 0x0000
50 #define WDC_NVME_DIAG_OPCODE 0xc6
51 #define WDC_NVME_DRIVE_LOG_SIZE_CMD 0x0120
52 #define WDC_NVME_DRIVE_LOG_CMD 0x0020
53 #define WDC_NVME_CRASH_DUMP_SIZE_CMD 0x0320
54 #define WDC_NVME_CRASH_DUMP_CMD 0x0420
55 #define WDC_NVME_PFAIL_DUMP_SIZE_CMD 0x0520
56 #define WDC_NVME_PFAIL_DUMP_CMD 0x0620
58 #define WDC_NVME_CLEAR_DUMP_OPCODE 0xff
59 #define WDC_NVME_CLEAR_CRASH_DUMP_CMD 0x0503
60 #define WDC_NVME_CLEAR_PFAIL_DUMP_CMD 0x0603
62 static void wdc_cap_diag(int argc, char *argv[]);
63 static void wdc_drive_log(int argc, char *argv[]);
64 static void wdc_get_crash_dump(int argc, char *argv[]);
65 static void wdc_purge(int argc, char *argv[]);
66 static void wdc_purge_monitor(int argc, char *argv[]);
68 #define WDC_CAP_DIAG_USAGE "\tnvmecontrol wdc cap-diag [-o path-template]\n"
69 #define WDC_DRIVE_LOG_USAGE "\tnvmecontrol wdc drive-log [-o path-template]\n"
70 #define WDC_GET_CRASH_DUMP_USAGE "\tnvmecontrol wdc get-crash-dump [-o path-template]\n"
71 #define WDC_PURGE_USAGE "\tnvmecontrol wdc purge [-o path-template]\n"
72 #define WDC_PURGE_MONITOR_USAGE "\tnvmecontrol wdc purge-monitor\n"
74 static struct nvme_function wdc_funcs[] = {
75 {"cap-diag", wdc_cap_diag, WDC_CAP_DIAG_USAGE},
76 {"drive-log", wdc_drive_log, WDC_DRIVE_LOG_USAGE},
77 {"get-crash-dump", wdc_get_crash_dump, WDC_GET_CRASH_DUMP_USAGE},
78 {"purge", wdc_purge, WDC_PURGE_USAGE},
79 {"purge_monitor", wdc_purge_monitor, WDC_PURGE_MONITOR_USAGE},
84 wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix)
86 struct nvme_controller_data cdata;
87 char sn[NVME_SERIAL_NUMBER_LENGTH + 1];
92 read_controller_data(fd, &cdata);
93 memcpy(sn, cdata.sn, NVME_SERIAL_NUMBER_LENGTH);
94 walker = sn + NVME_SERIAL_NUMBER_LENGTH - 1;
95 while (walker > sn && *walker == ' ')
98 snprintf(buf, len, "%s%s.bin", sn, suffix);
102 wdc_get_data(int fd, uint32_t opcode, uint32_t len, uint32_t off, uint32_t cmd,
103 uint8_t *buffer, size_t buflen)
105 struct nvme_pt_command pt;
107 memset(&pt, 0, sizeof(pt));
109 pt.cmd.cdw10 = len / sizeof(uint32_t); /* - 1 like all the others ??? */
110 pt.cmd.cdw11 = off / sizeof(uint32_t);
115 // printf("opcode %#x cdw10(len) %#x cdw11(offset?) %#x cdw12(cmd/sub) %#x buflen %zd\n",
116 // (int)opcode, (int)cdw10, (int)cdw11, (int)cdw12, buflen);
118 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
119 err(1, "wdc_get_data request failed");
120 if (nvme_completion_is_error(&pt.cpl))
121 errx(1, "wdc_get_data request returned error");
125 wdc_do_dump(int fd, char *tmpl, const char *suffix, uint32_t opcode,
126 uint32_t size_cmd, uint32_t cmd, int len_off)
130 uint32_t len, offset;
133 wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
135 buf = aligned_alloc(PAGE_SIZE, WDC_NVME_TOC_SIZE);
137 errx(1, "Can't get buffer to get size");
138 wdc_get_data(fd, opcode, WDC_NVME_TOC_SIZE,
139 0, size_cmd, buf, WDC_NVME_TOC_SIZE);
140 len = be32dec(buf + len_off);
143 errx(1, "No data for %s", suffix);
145 printf("Dumping %d bytes to %s\n", len, tmpl);
146 /* XXX overwrite protection? */
147 fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC);
149 err(1, "open %s", tmpl);
151 buf = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE);
153 errx(1, "Can't get buffer to read dump");
155 resid = len > NVME_MAX_XFER_SIZE ? NVME_MAX_XFER_SIZE : len;
156 wdc_get_data(fd, opcode, resid, offset, cmd, buf, resid);
157 if (write(fd2, buf, resid) != resid)
167 wdc_do_clear_dump(int fd, uint32_t opcode, uint32_t cmd)
169 struct nvme_pt_command pt;
171 memset(&pt, 0, sizeof(pt));
174 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
175 err(1, "wdc_do_clear_dump request failed");
176 if (nvme_completion_is_error(&pt.cpl))
177 errx(1, "wdc_do_clear_dump request returned error");
181 wdc_cap_diag_usage(void)
183 fprintf(stderr, "usage:\n");
184 fprintf(stderr, WDC_CAP_DIAG_USAGE);
189 wdc_cap_diag(int argc, char *argv[])
191 char path_tmpl[MAXPATHLEN];
195 while ((ch = getopt(argc, argv, "o:")) != -1) {
198 strlcpy(path_tmpl, optarg, MAXPATHLEN);
201 wdc_cap_diag_usage();
204 /* Check that a controller was specified. */
206 wdc_cap_diag_usage();
207 open_dev(argv[optind], &fd, 1, 1);
209 wdc_do_dump(fd, path_tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE,
210 WDC_NVME_CAP_DIAG_CMD, WDC_NVME_CAP_DIAG_CMD, 4);
218 wdc_drive_log_usage(void)
220 fprintf(stderr, "usage:\n");
221 fprintf(stderr, WDC_DRIVE_LOG_USAGE);
226 wdc_drive_log(int argc, char *argv[])
228 char path_tmpl[MAXPATHLEN];
232 while ((ch = getopt(argc, argv, "o:")) != -1) {
235 strlcpy(path_tmpl, optarg, MAXPATHLEN);
238 wdc_drive_log_usage();
241 /* Check that a controller was specified. */
243 wdc_drive_log_usage();
244 open_dev(argv[optind], &fd, 1, 1);
246 wdc_do_dump(fd, path_tmpl, "drive_log", WDC_NVME_DIAG_OPCODE,
247 WDC_NVME_DRIVE_LOG_SIZE_CMD, WDC_NVME_DRIVE_LOG_CMD, 0);
255 wdc_get_crash_dump_usage(void)
257 fprintf(stderr, "usage:\n");
258 fprintf(stderr, WDC_CAP_DIAG_USAGE);
263 wdc_get_crash_dump(int argc, char *argv[])
265 char path_tmpl[MAXPATHLEN];
268 while ((ch = getopt(argc, argv, "o:")) != -1) {
271 strlcpy(path_tmpl, optarg, MAXPATHLEN);
274 wdc_get_crash_dump_usage();
277 /* Check that a controller was specified. */
279 wdc_get_crash_dump_usage();
280 open_dev(argv[optind], &fd, 1, 1);
282 wdc_do_dump(fd, path_tmpl, "crash_dump", WDC_NVME_DIAG_OPCODE,
283 WDC_NVME_CRASH_DUMP_SIZE_CMD, WDC_NVME_CRASH_DUMP_CMD, 0);
284 wdc_do_clear_dump(fd, WDC_NVME_CLEAR_DUMP_OPCODE,
285 WDC_NVME_CLEAR_CRASH_DUMP_CMD);
286 // wdc_led_beacon_disable(fd);
287 wdc_do_dump(fd, path_tmpl, "pfail_dump", WDC_NVME_DIAG_OPCODE,
288 WDC_NVME_PFAIL_DUMP_SIZE_CMD, WDC_NVME_PFAIL_DUMP_CMD, 0);
289 wdc_do_clear_dump(fd, WDC_NVME_CLEAR_DUMP_OPCODE,
290 WDC_NVME_CLEAR_PFAIL_DUMP_CMD);
298 wdc_purge(int argc, char *argv[])
300 char path_tmpl[MAXPATHLEN];
303 while ((ch = getopt(argc, argv, "o:")) != -1) {
306 strlcpy(path_tmpl, optarg, MAXPATHLEN);
309 wdc_cap_diag_usage();
313 printf("purge has not been implemented.\n");
318 wdc_purge_monitor(int argc, char *argv[])
320 char path_tmpl[MAXPATHLEN];
323 while ((ch = getopt(argc, argv, "o:")) != -1) {
326 strlcpy(path_tmpl, optarg, MAXPATHLEN);
329 wdc_cap_diag_usage();
333 printf("purge has not been implemented.\n");
338 wdc(int argc, char *argv[])
341 dispatch(argc, argv, wdc_funcs);