]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/wdc.c
MFC r316105 (by ngie): Don't use K&R style prototypes; ANSIfy them
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / wdc.c
1 /*-
2  * Copyright (c) 2017 Netflix, Inc
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
32 #include <sys/endian.h>
33
34 #include <ctype.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "nvmecontrol.h"
44
45 #define WDC_NVME_TOC_SIZE       8
46
47 #define WDC_NVME_CAP_DIAG_OPCODE        0xe6
48 #define WDC_NVME_CAP_DIAG_CMD           0x0000
49
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
57
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
61
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[]);
67
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"
73
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},
80         {NULL,                  NULL,                   NULL},
81 };
82
83 static void
84 wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix)
85 {
86         struct nvme_controller_data     cdata;
87         char sn[NVME_SERIAL_NUMBER_LENGTH + 1];
88         char *walker;
89
90         len -= strlen(buf);
91         buf += strlen(buf);
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 == ' ')
96                 walker--;
97         *++walker = '\0';
98         snprintf(buf, len, "%s%s.bin", sn, suffix);
99 }
100
101 static void
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)
104 {
105         struct nvme_pt_command  pt;
106
107         memset(&pt, 0, sizeof(pt));
108         pt.cmd.opc = opcode;
109         pt.cmd.cdw10 = len / sizeof(uint32_t);  /* - 1 like all the others ??? */
110         pt.cmd.cdw11 = off / sizeof(uint32_t);
111         pt.cmd.cdw12 = cmd;
112         pt.buf = buffer;
113         pt.len = buflen;
114         pt.is_read = 1;
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);
117
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");
122 }
123
124 static void
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)
127 {
128         int fd2;
129         uint8_t *buf;
130         uint32_t len, offset;
131         ssize_t resid;
132
133         wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
134
135         buf = aligned_alloc(PAGE_SIZE, WDC_NVME_TOC_SIZE);
136         if (buf == NULL)
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);
141
142         if (len == 0)
143                 errx(1, "No data for %s", suffix);
144
145         printf("Dumping %d bytes to %s\n", len, tmpl);
146         /* XXX overwrite protection? */
147         fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC);
148         if (fd2 < 0)
149                 err(1, "open %s", tmpl);
150         offset = 0;
151         buf = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE);
152         if (buf == NULL)
153                 errx(1, "Can't get buffer to read dump");
154         while (len > 0) {
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)
158                         err(1, "write");
159                 offset += resid;
160                 len -= resid;
161         }
162         free(buf);
163         close(fd2);
164 }
165
166 static void
167 wdc_do_clear_dump(int fd, uint32_t opcode, uint32_t cmd)
168 {
169         struct nvme_pt_command  pt;
170
171         memset(&pt, 0, sizeof(pt));
172         pt.cmd.opc = opcode;
173         pt.cmd.cdw12 = cmd;
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");
178 }
179
180 static void
181 wdc_cap_diag_usage(void)
182 {
183         fprintf(stderr, "usage:\n");
184         fprintf(stderr, WDC_CAP_DIAG_USAGE);
185         exit(1);
186 }
187
188 static void
189 wdc_cap_diag(int argc, char *argv[])
190 {
191         char path_tmpl[MAXPATHLEN];
192         int ch, fd;
193
194         path_tmpl[0] = '\0';
195         while ((ch = getopt(argc, argv, "o:")) != -1) {
196                 switch ((char)ch) {
197                 case 'o':
198                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
199                         break;
200                 default:
201                         wdc_cap_diag_usage();
202                 }
203         }
204         /* Check that a controller was specified. */
205         if (optind >= argc)
206                 wdc_cap_diag_usage();
207         open_dev(argv[optind], &fd, 1, 1);
208
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);
211
212         close(fd);
213
214         exit(1);        
215 }
216
217 static void
218 wdc_drive_log_usage(void)
219 {
220         fprintf(stderr, "usage:\n");
221         fprintf(stderr, WDC_DRIVE_LOG_USAGE);
222         exit(1);
223 }
224
225 static void
226 wdc_drive_log(int argc, char *argv[])
227 {
228         char path_tmpl[MAXPATHLEN];
229         int ch, fd;
230
231         path_tmpl[0] = '\0';
232         while ((ch = getopt(argc, argv, "o:")) != -1) {
233                 switch ((char)ch) {
234                 case 'o':
235                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
236                         break;
237                 default:
238                         wdc_drive_log_usage();
239                 }
240         }
241         /* Check that a controller was specified. */
242         if (optind >= argc)
243                 wdc_drive_log_usage();
244         open_dev(argv[optind], &fd, 1, 1);
245
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);
248
249         close(fd);
250
251         exit(1);
252 }
253
254 static void
255 wdc_get_crash_dump_usage(void)
256 {
257         fprintf(stderr, "usage:\n");
258         fprintf(stderr, WDC_CAP_DIAG_USAGE);
259         exit(1);
260 }
261
262 static void
263 wdc_get_crash_dump(int argc, char *argv[])
264 {
265         char path_tmpl[MAXPATHLEN];
266         int ch, fd;
267
268         while ((ch = getopt(argc, argv, "o:")) != -1) {
269                 switch ((char)ch) {
270                 case 'o':
271                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
272                         break;
273                 default:
274                         wdc_get_crash_dump_usage();
275                 }
276         }
277         /* Check that a controller was specified. */
278         if (optind >= argc)
279                 wdc_get_crash_dump_usage();
280         open_dev(argv[optind], &fd, 1, 1);
281
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);
291
292         close(fd);
293
294         exit(1);
295 }
296
297 static void
298 wdc_purge(int argc, char *argv[])
299 {
300         char path_tmpl[MAXPATHLEN];
301         int ch;
302
303         while ((ch = getopt(argc, argv, "o:")) != -1) {
304                 switch ((char)ch) {
305                 case 'o':
306                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
307                         break;
308                 default:
309                         wdc_cap_diag_usage();
310                 }
311         }
312
313         printf("purge has not been implemented.\n");
314         exit(1);
315 }
316
317 static void
318 wdc_purge_monitor(int argc, char *argv[])
319 {
320         char path_tmpl[MAXPATHLEN];
321         int ch;
322
323         while ((ch = getopt(argc, argv, "o:")) != -1) {
324                 switch ((char)ch) {
325                 case 'o':
326                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
327                         break;
328                 default:
329                         wdc_cap_diag_usage();
330                 }
331         }
332
333         printf("purge has not been implemented.\n");
334         exit(1);
335 }
336
337 void
338 wdc(int argc, char *argv[])
339 {
340
341         dispatch(argc, argv, wdc_funcs);
342 }