]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/nvmecontrol.c
Move Intel specific log pages to intel.c
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / nvmecontrol.c
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 SET_DECLARE(top, struct nvme_function);
51
52 static void
53 print_usage(const struct nvme_function *f)
54 {
55         const char *cp;
56         char ch;
57         bool need_prefix = true;
58
59         cp = f->usage;
60         while (*cp) {
61                 ch = *cp++;
62                 if (need_prefix) {
63                         if (ch != ' ')
64                                 fputs("        nvmecontrol ", stderr);
65                         else
66                                 fputs("                    ", stderr);
67                 }
68                 fputc(ch, stderr);
69                 need_prefix = (ch == '\n');
70         }
71         if (!need_prefix)
72                 fputc('\n', stderr);
73 }
74
75 static void
76 gen_usage_set(struct nvme_function **f, struct nvme_function **flimit)
77 {
78
79         fprintf(stderr, "usage:\n");
80         while (f < flimit) {
81                 print_usage(*f);
82                 f++;
83         }
84         exit(1);
85 }
86
87 void
88 usage(const struct nvme_function *f)
89 {
90
91         fprintf(stderr, "usage:\n");
92         print_usage(f);
93         exit(1);
94 }
95
96 void
97 dispatch_set(int argc, char *argv[], struct nvme_function **tbl,
98     struct nvme_function **tbl_limit)
99 {
100         struct nvme_function **f = tbl;
101
102         if (argv[1] == NULL) {
103                 gen_usage_set(tbl, tbl_limit);
104                 return;
105         }
106
107         while (f < tbl_limit) {
108                 if (strcmp(argv[1], (*f)->name) == 0) {
109                         (*f)->fn(*f, argc-1, &argv[1]);
110                         return;
111                 }
112                 f++;
113         }
114
115         fprintf(stderr, "Unknown command: %s\n", argv[1]);
116         gen_usage_set(tbl, tbl_limit);
117 }
118
119 static void
120 print_bytes(void *data, uint32_t length)
121 {
122         uint32_t        i, j;
123         uint8_t         *p, *end;
124
125         end = (uint8_t *)data + length;
126
127         for (i = 0; i < length; i++) {
128                 p = (uint8_t *)data + (i*16);
129                 printf("%03x: ", i*16);
130                 for (j = 0; j < 16 && p < end; j++)
131                         printf("%02x ", *p++);
132                 if (p >= end)
133                         break;
134                 printf("\n");
135         }
136         printf("\n");
137 }
138
139 static void
140 print_dwords(void *data, uint32_t length)
141 {
142         uint32_t        *p;
143         uint32_t        i, j;
144
145         p = (uint32_t *)data;
146         length /= sizeof(uint32_t);
147
148         for (i = 0; i < length; i+=8) {
149                 printf("%03x: ", i*4);
150                 for (j = 0; j < 8; j++)
151                         printf("%08x ", p[i+j]);
152                 printf("\n");
153         }
154
155         printf("\n");
156 }
157
158 void
159 print_hex(void *data, uint32_t length)
160 {
161         if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
162                 print_dwords(data, length);
163         else
164                 print_bytes(data, length);
165 }
166
167 void
168 read_controller_data(int fd, struct nvme_controller_data *cdata)
169 {
170         struct nvme_pt_command  pt;
171
172         memset(&pt, 0, sizeof(pt));
173         pt.cmd.opc = NVME_OPC_IDENTIFY;
174         pt.cmd.cdw10 = htole32(1);
175         pt.buf = cdata;
176         pt.len = sizeof(*cdata);
177         pt.is_read = 1;
178
179         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
180                 err(1, "identify request failed");
181
182         /* Convert data to host endian */
183         nvme_controller_data_swapbytes(cdata);
184
185         if (nvme_completion_is_error(&pt.cpl))
186                 errx(1, "identify request returned error");
187 }
188
189 void
190 read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
191 {
192         struct nvme_pt_command  pt;
193
194         memset(&pt, 0, sizeof(pt));
195         pt.cmd.opc = NVME_OPC_IDENTIFY;
196         pt.cmd.nsid = htole32(nsid);
197         pt.buf = nsdata;
198         pt.len = sizeof(*nsdata);
199         pt.is_read = 1;
200
201         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
202                 err(1, "identify request failed");
203
204         /* Convert data to host endian */
205         nvme_namespace_data_swapbytes(nsdata);
206
207         if (nvme_completion_is_error(&pt.cpl))
208                 errx(1, "identify request returned error");
209 }
210
211 int
212 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
213 {
214         char            full_path[64];
215
216         if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
217                 if (show_error)
218                         warnx("controller/namespace ids must begin with '%s'",
219                             NVME_CTRLR_PREFIX);
220                 if (exit_on_error)
221                         exit(1);
222                 else
223                         return (EINVAL);
224         }
225
226         snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
227         *fd = open(full_path, O_RDWR);
228         if (*fd < 0) {
229                 if (show_error)
230                         warn("could not open %s", full_path);
231                 if (exit_on_error)
232                         exit(1);
233                 else
234                         return (errno);
235         }
236
237         return (0);
238 }
239
240 void
241 parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid)
242 {
243         char    *nsloc;
244
245         /*
246          * Pull the namespace id from the string. +2 skips past the "ns" part
247          *  of the string.  Don't search past 10 characters into the string,
248          *  otherwise we know it is malformed.
249          */
250         nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
251         if (nsloc != NULL)
252                 *nsid = strtol(nsloc + 2, NULL, 10);
253         if (nsloc == NULL || (*nsid == 0 && errno != 0))
254                 errx(1, "invalid namespace ID '%s'", ns_str);
255
256         /*
257          * The controller string will include only the nvmX part of the
258          *  nvmeXnsY string.
259          */
260         snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
261 }
262
263 int
264 main(int argc, char *argv[])
265 {
266
267         if (argc < 2)
268                 gen_usage_set(SET_BEGIN(top), SET_LIMIT(top));
269
270         DISPATCH(argc, argv, top);
271
272         return (0);
273 }