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