]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/wdc.c
Add the ability to dump log pages directly in binary to stdout.
[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, resid, offset;
131
132         wdc_append_serial_name(fd, tmpl, MAXPATHLEN, suffix);
133
134         buf = aligned_alloc(PAGE_SIZE, WDC_NVME_TOC_SIZE);
135         if (buf == NULL)
136                 errx(1, "Can't get buffer to get size");
137         wdc_get_data(fd, opcode, WDC_NVME_TOC_SIZE,
138             0, size_cmd, buf, WDC_NVME_TOC_SIZE);
139         len = be32dec(buf + len_off);
140
141         if (len == 0)
142                 errx(1, "No data for %s", suffix);
143
144         printf("Dumping %d bytes to %s\n", len, tmpl);
145         /* XXX overwrite protection? */
146         fd2 = open(tmpl, O_WRONLY | O_CREAT | O_TRUNC);
147         if (fd2 < 0)
148                 err(1, "open %s", tmpl);
149         offset = 0;
150         buf = aligned_alloc(PAGE_SIZE, NVME_MAX_XFER_SIZE);
151         if (buf == NULL)
152                 errx(1, "Can't get buffer to read dump");
153         while (len > 0) {
154                 resid = len > NVME_MAX_XFER_SIZE ? NVME_MAX_XFER_SIZE : len;
155                 wdc_get_data(fd, opcode, resid, offset, cmd, buf, resid);
156                 if (write(fd2, buf, resid) != resid)
157                         err(1, "write");
158                 offset += resid;
159                 len -= resid;
160         }
161         free(buf);
162         close(fd2);
163 }
164
165 static void
166 wdc_do_clear_dump(int fd, uint32_t opcode, uint32_t cmd)
167 {
168         struct nvme_pt_command  pt;
169
170         memset(&pt, 0, sizeof(pt));
171         pt.cmd.opc = opcode;
172         pt.cmd.cdw12 = cmd;
173         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
174                 err(1, "wdc_do_clear_dump request failed");
175         if (nvme_completion_is_error(&pt.cpl))
176                 errx(1, "wdc_do_clear_dump request returned error");
177 }
178
179 static void
180 wdc_cap_diag_usage()
181 {
182         fprintf(stderr, "usage:\n");
183         fprintf(stderr, WDC_CAP_DIAG_USAGE);
184         exit(1);
185 }
186
187 static void
188 wdc_cap_diag(int argc, char *argv[])
189 {
190         char path_tmpl[MAXPATHLEN];
191         int ch, fd;
192
193         path_tmpl[0] = '\0';
194         while ((ch = getopt(argc, argv, "o:")) != -1) {
195                 switch ((char)ch) {
196                 case 'o':
197                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
198                         break;
199                 default:
200                         wdc_cap_diag_usage();
201                 }
202         }
203         /* Check that a controller was specified. */
204         if (optind >= argc)
205                 wdc_cap_diag_usage();
206         open_dev(argv[optind], &fd, 1, 1);
207
208         wdc_do_dump(fd, path_tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE,
209             WDC_NVME_CAP_DIAG_CMD, WDC_NVME_CAP_DIAG_CMD, 4);
210
211         close(fd);
212
213         exit(1);        
214 }
215
216 static void
217 wdc_drive_log_usage()
218 {
219         fprintf(stderr, "usage:\n");
220         fprintf(stderr, WDC_DRIVE_LOG_USAGE);
221         exit(1);
222 }
223
224 static void
225 wdc_drive_log(int argc, char *argv[])
226 {
227         char path_tmpl[MAXPATHLEN];
228         int ch, fd;
229
230         path_tmpl[0] = '\0';
231         while ((ch = getopt(argc, argv, "o:")) != -1) {
232                 switch ((char)ch) {
233                 case 'o':
234                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
235                         break;
236                 default:
237                         wdc_drive_log_usage();
238                 }
239         }
240         /* Check that a controller was specified. */
241         if (optind >= argc)
242                 wdc_drive_log_usage();
243         open_dev(argv[optind], &fd, 1, 1);
244
245         wdc_do_dump(fd, path_tmpl, "drive_log", WDC_NVME_DIAG_OPCODE,
246             WDC_NVME_DRIVE_LOG_SIZE_CMD, WDC_NVME_DRIVE_LOG_CMD, 0);
247
248         close(fd);
249
250         exit(1);
251 }
252
253 static void
254 wdc_get_crash_dump_usage()
255 {
256         fprintf(stderr, "usage:\n");
257         fprintf(stderr, WDC_CAP_DIAG_USAGE);
258         exit(1);
259 }
260
261 static void
262 wdc_get_crash_dump(int argc, char *argv[])
263 {
264         char path_tmpl[MAXPATHLEN];
265         int ch, fd;
266
267         while ((ch = getopt(argc, argv, "o:")) != -1) {
268                 switch ((char)ch) {
269                 case 'o':
270                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
271                         break;
272                 default:
273                         wdc_get_crash_dump_usage();
274                 }
275         }
276         /* Check that a controller was specified. */
277         if (optind >= argc)
278                 wdc_get_crash_dump_usage();
279         open_dev(argv[optind], &fd, 1, 1);
280
281         wdc_do_dump(fd, path_tmpl, "crash_dump", WDC_NVME_DIAG_OPCODE,
282             WDC_NVME_CRASH_DUMP_SIZE_CMD, WDC_NVME_CRASH_DUMP_CMD, 0);
283         wdc_do_clear_dump(fd, WDC_NVME_CLEAR_DUMP_OPCODE,
284             WDC_NVME_CLEAR_CRASH_DUMP_CMD);
285 //      wdc_led_beacon_disable(fd);
286         wdc_do_dump(fd, path_tmpl, "pfail_dump", WDC_NVME_DIAG_OPCODE,
287             WDC_NVME_PFAIL_DUMP_SIZE_CMD, WDC_NVME_PFAIL_DUMP_CMD, 0);
288         wdc_do_clear_dump(fd, WDC_NVME_CLEAR_DUMP_OPCODE,
289                 WDC_NVME_CLEAR_PFAIL_DUMP_CMD);
290
291         close(fd);
292
293         exit(1);
294 }
295
296 static void
297 wdc_purge(int argc, char *argv[])
298 {
299         char path_tmpl[MAXPATHLEN];
300         int ch;
301
302         while ((ch = getopt(argc, argv, "o:")) != -1) {
303                 switch ((char)ch) {
304                 case 'o':
305                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
306                         break;
307                 default:
308                         wdc_cap_diag_usage();
309                 }
310         }
311
312         printf("purge has not been implemented.\n");
313         exit(1);
314 }
315
316 static void
317 wdc_purge_monitor(int argc, char *argv[])
318 {
319         char path_tmpl[MAXPATHLEN];
320         int ch;
321
322         while ((ch = getopt(argc, argv, "o:")) != -1) {
323                 switch ((char)ch) {
324                 case 'o':
325                         strlcpy(path_tmpl, optarg, MAXPATHLEN);
326                         break;
327                 default:
328                         wdc_cap_diag_usage();
329                 }
330         }
331
332         printf("purge has not been implemented.\n");
333         exit(1);
334 }
335
336 void
337 wdc(int argc, char *argv[])
338 {
339
340         dispatch(argc, argv, wdc_funcs);
341 }