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