xref: /freebsd/sbin/nvmecontrol/nvmecontrol.c (revision 445ed7b40948c160f2f7d363d2d0ae1ffac4aabd)
1 /*-
2  * Copyright (C) 2012-2013 Intel Corporation
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/stat.h>
33 
34 #include <ctype.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "nvmecontrol.h"
47 
48 typedef void (*nvme_fn_t)(int argc, char *argv[]);
49 
50 static struct nvme_function {
51 	const char	*name;
52 	nvme_fn_t	fn;
53 	const char	*usage;
54 } funcs[] = {
55 	{"devlist",	devlist,	DEVLIST_USAGE},
56 	{"identify",	identify,	IDENTIFY_USAGE},
57 	{"perftest",	perftest,	PERFTEST_USAGE},
58 	{"reset",	reset,		RESET_USAGE},
59 	{"logpage",	logpage,	LOGPAGE_USAGE},
60 	{"firmware",	firmware,	FIRMWARE_USAGE},
61 	{NULL,		NULL,		NULL},
62 };
63 
64 static void
65 usage(void)
66 {
67 	struct nvme_function *f;
68 
69 	f = funcs;
70 	fprintf(stderr, "usage:\n");
71 	while (f->name != NULL) {
72 		fprintf(stderr, "%s", f->usage);
73 		f++;
74 	}
75 	exit(1);
76 }
77 
78 static void
79 print_bytes(void *data, uint32_t length)
80 {
81 	uint32_t	i, j;
82 	uint8_t		*p, *end;
83 
84 	end = (uint8_t *)data + length;
85 
86 	for (i = 0; i < length; i++) {
87 		p = (uint8_t *)data + (i*16);
88 		printf("%03x: ", i*16);
89 		for (j = 0; j < 16 && p < end; j++)
90 			printf("%02x ", *p++);
91 		if (p >= end)
92 			break;
93 		printf("\n");
94 	}
95 	printf("\n");
96 }
97 
98 static void
99 print_dwords(void *data, uint32_t length)
100 {
101 	uint32_t	*p;
102 	uint32_t	i, j;
103 
104 	p = (uint32_t *)data;
105 	length /= sizeof(uint32_t);
106 
107 	for (i = 0; i < length; i+=8) {
108 		printf("%03x: ", i*4);
109 		for (j = 0; j < 8; j++)
110 			printf("%08x ", p[i+j]);
111 		printf("\n");
112 	}
113 
114 	printf("\n");
115 }
116 
117 void
118 print_hex(void *data, uint32_t length)
119 {
120 	if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
121 		print_dwords(data, length);
122 	else
123 		print_bytes(data, length);
124 }
125 
126 void
127 read_controller_data(int fd, struct nvme_controller_data *cdata)
128 {
129 	struct nvme_pt_command	pt;
130 
131 	memset(&pt, 0, sizeof(pt));
132 	pt.cmd.opc = NVME_OPC_IDENTIFY;
133 	pt.cmd.cdw10 = 1;
134 	pt.buf = cdata;
135 	pt.len = sizeof(*cdata);
136 	pt.is_read = 1;
137 
138 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
139 		err(1, "identify request failed");
140 
141 	if (nvme_completion_is_error(&pt.cpl))
142 		errx(1, "identify request returned error");
143 }
144 
145 void
146 read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
147 {
148 	struct nvme_pt_command	pt;
149 
150 	memset(&pt, 0, sizeof(pt));
151 	pt.cmd.opc = NVME_OPC_IDENTIFY;
152 	pt.cmd.nsid = nsid;
153 	pt.buf = nsdata;
154 	pt.len = sizeof(*nsdata);
155 	pt.is_read = 1;
156 
157 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
158 		err(1, "identify request failed");
159 
160 	if (nvme_completion_is_error(&pt.cpl))
161 		errx(1, "identify request returned error");
162 }
163 
164 int
165 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
166 {
167 	char		full_path[64];
168 
169 	if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
170 		if (show_error)
171 			warnx("controller/namespace ids must begin with '%s'",
172 			    NVME_CTRLR_PREFIX);
173 		if (exit_on_error)
174 			exit(1);
175 		else
176 			return (EINVAL);
177 	}
178 
179 	snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
180 	*fd = open(full_path, O_RDWR);
181 	if (*fd < 0) {
182 		if (show_error)
183 			warn("could not open %s", full_path);
184 		if (exit_on_error)
185 			exit(1);
186 		else
187 			return (errno);
188 	}
189 
190 	return (0);
191 }
192 
193 void
194 parse_ns_str(const char *ns_str, char *ctrlr_str, int *nsid)
195 {
196 	char	*nsloc;
197 
198 	/*
199 	 * Pull the namespace id from the string. +2 skips past the "ns" part
200 	 *  of the string.  Don't search past 10 characters into the string,
201 	 *  otherwise we know it is malformed.
202 	 */
203 	nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
204 	if (nsloc != NULL)
205 		*nsid = strtol(nsloc + 2, NULL, 10);
206 	if (nsloc == NULL || (*nsid == 0 && errno != 0))
207 		errx(1, "invalid namespace ID '%s'", ns_str);
208 
209 	/*
210 	 * The controller string will include only the nvmX part of the
211 	 *  nvmeXnsY string.
212 	 */
213 	snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
214 }
215 
216 int
217 main(int argc, char *argv[])
218 {
219 	struct nvme_function *f;
220 
221 	if (argc < 2)
222 		usage();
223 
224 	f = funcs;
225 	while (f->name != NULL) {
226 		if (strcmp(argv[1], f->name) == 0)
227 			f->fn(argc-1, &argv[1]);
228 		f++;
229 	}
230 
231 	usage();
232 
233 	return (0);
234 }
235