nvmectl.c revision 1.2.6.1 1 /* $NetBSD: nvmectl.c,v 1.2.6.1 2017/05/02 03:19:16 pgoyette Exp $ */
2
3 /*-
4 * Copyright (C) 2012-2013 Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: nvmectl.c,v 1.2.6.1 2017/05/02 03:19:16 pgoyette Exp $");
32 #if 0
33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/nvmecontrol.c 314229 2017-02-25 00:09:12Z imp $");
34 #endif
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/ioccom.h>
39 #include <sys/stat.h>
40
41 #include <ctype.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <paths.h>
46 #include <stdbool.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "nvmectl.h"
54
55 static struct nvme_function funcs[] = {
56 {"devlist", devlist, DEVLIST_USAGE},
57 {"identify", identify, IDENTIFY_USAGE},
58 #ifdef PERFTEST_USAGE
59 {"perftest", perftest, PERFTEST_USAGE},
60 #endif
61 #ifdef RESET_USAGE
62 {"reset", reset, RESET_USAGE},
63 #endif
64 {"logpage", logpage, LOGPAGE_USAGE},
65 #ifdef FIRMWARE_USAGE
66 {"firmware", firmware, FIRMWARE_USAGE},
67 #endif
68 {"power", power, POWER_USAGE},
69 {"wdc", wdc, WDC_USAGE},
70 {NULL, NULL, NULL},
71 };
72
73 void
74 gen_usage(struct nvme_function *f)
75 {
76
77 fprintf(stderr, "usage:\n");
78 while (f->name != NULL) {
79 fprintf(stderr, "%s", f->usage);
80 f++;
81 }
82 exit(1);
83 }
84
85 void
86 dispatch(int argc, char *argv[], struct nvme_function *tbl)
87 {
88 struct nvme_function *f = tbl;
89
90 if (argv[1] == NULL) {
91 gen_usage(tbl);
92 return;
93 }
94
95 while (f->name != NULL) {
96 if (strcmp(argv[1], f->name) == 0)
97 f->fn(argc-1, &argv[1]);
98 f++;
99 }
100
101 fprintf(stderr, "Unknown command: %s\n", argv[1]);
102 gen_usage(tbl);
103 }
104
105 static void
106 print_bytes(void *data, uint32_t length)
107 {
108 uint32_t i, j;
109 uint8_t *p, *end;
110
111 end = (uint8_t *)data + length;
112
113 for (i = 0; i < length; i++) {
114 p = (uint8_t *)data + (i*16);
115 printf("%03x: ", i*16);
116 for (j = 0; j < 16 && p < end; j++)
117 printf("%02x ", *p++);
118 if (p >= end)
119 break;
120 printf("\n");
121 }
122 printf("\n");
123 }
124
125 static void
126 print_dwords(void *data, uint32_t length)
127 {
128 uint32_t *p;
129 uint32_t i, j;
130
131 p = (uint32_t *)data;
132 length /= sizeof(uint32_t);
133
134 for (i = 0; i < length; i+=8) {
135 printf("%03x: ", i*4);
136 for (j = 0; j < 8; j++)
137 printf("%08x ", p[i+j]);
138 printf("\n");
139 }
140
141 printf("\n");
142 }
143
144 void
145 print_hex(void *data, uint32_t length)
146 {
147 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
148 print_dwords(data, length);
149 else
150 print_bytes(data, length);
151 }
152
153 void
154 read_controller_data(int fd, struct nvm_identify_controller *cdata)
155 {
156 struct nvme_pt_command pt;
157
158 memset(&pt, 0, sizeof(pt));
159 pt.cmd.opcode = NVM_ADMIN_IDENTIFY;
160 pt.cmd.cdw10 = 1;
161 pt.buf = cdata;
162 pt.len = sizeof(*cdata);
163 pt.is_read = 1;
164
165 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
166 err(1, "identify request failed");
167
168 if (nvme_completion_is_error(&pt.cpl))
169 errx(1, "identify request returned error");
170 }
171
172 void
173 read_namespace_data(int fd, int nsid, struct nvm_identify_namespace *nsdata)
174 {
175 struct nvme_pt_command pt;
176
177 memset(&pt, 0, sizeof(pt));
178 pt.cmd.opcode = NVM_ADMIN_IDENTIFY;
179 pt.cmd.nsid = nsid;
180 pt.buf = nsdata;
181 pt.len = sizeof(*nsdata);
182 pt.is_read = 1;
183
184 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
185 err(1, "identify request failed");
186
187 if (nvme_completion_is_error(&pt.cpl))
188 errx(1, "identify request returned error");
189 }
190
191 int
192 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
193 {
194 char full_path[64];
195
196 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
197 if (show_error)
198 warnx("controller/namespace ids must begin with '%s'",
199 NVME_CTRLR_PREFIX);
200 if (exit_on_error)
201 exit(1);
202 else
203 return (EINVAL);
204 }
205
206 snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
207 *fd = open(full_path, O_RDWR);
208 if (*fd < 0) {
209 if (show_error)
210 warn("could not open %s", full_path);
211 if (exit_on_error)
212 exit(1);
213 else
214 return (errno);
215 }
216
217 return (0);
218 }
219
220 void
221 parse_ns_str(const char *ns_str, char *ctrlr_str, int *nsid)
222 {
223 char *nsloc;
224
225 /*
226 * Pull the namespace id from the string. +2 skips past the "ns" part
227 * of the string. Don't search past 10 characters into the string,
228 * otherwise we know it is malformed.
229 */
230 nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
231 if (nsloc != NULL)
232 *nsid = strtol(nsloc + 2, NULL, 10);
233 if (nsloc == NULL || (*nsid == 0 && errno != 0))
234 errx(1, "invalid namespace ID '%s'", ns_str);
235
236 /*
237 * The controller string will include only the nvmX part of the
238 * nvmeXnsY string.
239 */
240 snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
241 }
242
243 void
244 nvme_strvis(u_char *dst, int dlen, const u_char *src, int slen)
245 {
246 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
247 /* Trim leading and trailing blanks and NULs. */
248 while (slen > 0 && STRVIS_ISWHITE(src[0]))
249 ++src, --slen;
250 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
251 --slen;
252
253 while (slen > 0) {
254 if (*src < 0x20 || *src >= 0x80) {
255 /* non-printable characters */
256 dlen -= 4;
257 if (dlen < 1)
258 break;
259 *dst++ = '\\';
260 *dst++ = ((*src & 0300) >> 6) + '0';
261 *dst++ = ((*src & 0070) >> 3) + '0';
262 *dst++ = ((*src & 0007) >> 0) + '0';
263 } else if (*src == '\\') {
264 /* quote characters */
265 dlen -= 2;
266 if (dlen < 1)
267 break;
268 *dst++ = '\\';
269 *dst++ = '\\';
270 } else {
271 /* normal characters */
272 if (--dlen < 1)
273 break;
274 *dst++ = *src;
275 }
276 ++src, --slen;
277 }
278
279 *dst++ = 0;
280 }
281
282 int
283 main(int argc, char *argv[])
284 {
285
286 if (argc < 2)
287 gen_usage(funcs);
288
289 dispatch(argc, argv, funcs);
290
291 return (0);
292 }
293