]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sbin/nvmecontrol/nvmecontrol.c
MFC r252266:
[FreeBSD/stable/9.git] / sbin / nvmecontrol / nvmecontrol.c
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 <errno.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sysexits.h>
43 #include <unistd.h>
44
45 #include "nvmecontrol.h"
46
47 static void perftest_usage(void);
48
49 static void
50 usage(void)
51 {
52         fprintf(stderr, "usage:\n");
53         fprintf(stderr, DEVLIST_USAGE);
54         fprintf(stderr, IDENTIFY_USAGE);
55         fprintf(stderr, RESET_USAGE);
56         fprintf(stderr, PERFTEST_USAGE);
57         exit(EX_USAGE);
58 }
59
60 void
61 read_controller_data(int fd, struct nvme_controller_data *cdata)
62 {
63         struct nvme_pt_command  pt;
64
65         memset(&pt, 0, sizeof(pt));
66         pt.cmd.opc = NVME_OPC_IDENTIFY;
67         pt.cmd.cdw10 = 1;
68         pt.buf = cdata;
69         pt.len = sizeof(*cdata);
70         pt.is_read = 1;
71
72         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
73                 printf("Identify request failed. errno=%d (%s)\n",
74                     errno, strerror(errno));
75                 exit(EX_IOERR);
76         }
77
78         if (nvme_completion_is_error(&pt.cpl)) {
79                 printf("Passthrough command returned error.\n");
80                 exit(EX_IOERR);
81         }
82 }
83
84 void
85 read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
86 {
87         struct nvme_pt_command  pt;
88
89         memset(&pt, 0, sizeof(pt));
90         pt.cmd.opc = NVME_OPC_IDENTIFY;
91         pt.cmd.nsid = nsid;
92         pt.buf = nsdata;
93         pt.len = sizeof(*nsdata);
94         pt.is_read = 1;
95
96         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
97                 printf("Identify request failed. errno=%d (%s)\n",
98                     errno, strerror(errno));
99                 exit(EX_IOERR);
100         }
101
102         if (nvme_completion_is_error(&pt.cpl)) {
103                 printf("Passthrough command returned error.\n");
104                 exit(EX_IOERR);
105         }
106 }
107
108 int
109 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
110 {
111         struct stat     devstat;
112         char            full_path[64];
113
114         snprintf(full_path, sizeof(full_path), "/dev/%s", str);
115         if (stat(full_path, &devstat) != 0) {
116                 if (show_error)
117                         fprintf(stderr, "error\n");
118                 if (exit_on_error)
119                         exit(EX_NOINPUT);
120                 else
121                         return (EX_NOINPUT);
122         }
123
124         *fd = open(full_path, O_RDWR);
125         if (*fd < 0) {
126                 if (show_error)
127                         printf("Could not open %s. errno=%d (%s)\n", full_path,
128                             errno, strerror(errno));
129                 if (exit_on_error)
130                         exit(EX_NOPERM);
131                 else
132                         return (EX_NOPERM);
133         }
134
135         return (EX_OK);
136 }
137
138 static void
139 print_perftest(struct nvme_io_test *io_test, bool perthread)
140 {
141         uint32_t i, io_completed = 0, iops, mbps;
142
143         for (i = 0; i < io_test->num_threads; i++)
144                 io_completed += io_test->io_completed[i];
145
146         iops = io_completed/io_test->time;
147         mbps = iops * io_test->size / (1024*1024);
148
149         printf("Threads: %2d Size: %6d %5s Time: %3d IO/s: %7d MB/s: %4d\n",
150             io_test->num_threads, io_test->size,
151             io_test->opc == NVME_OPC_READ ? "READ" : "WRITE",
152             io_test->time, iops, mbps);
153
154         if (perthread)
155                 for (i = 0; i < io_test->num_threads; i++)
156                         printf("\t%3d: %8d IO/s\n", i,
157                             io_test->io_completed[i]/io_test->time);
158
159         exit(1);
160 }
161
162 static void
163 perftest_usage(void)
164 {
165         fprintf(stderr, "usage:\n");
166         fprintf(stderr, PERFTEST_USAGE);
167         exit(EX_USAGE);
168 }
169
170 static void
171 perftest(int argc, char *argv[])
172 {
173         struct nvme_io_test             io_test;
174         int                             fd;
175         char                            ch;
176         char                            *p;
177         u_long                          ioctl_cmd = NVME_IO_TEST;
178         bool                            nflag, oflag, sflag, tflag;
179         int                             perthread = 0;
180
181         nflag = oflag = sflag = tflag = false;
182
183         memset(&io_test, 0, sizeof(io_test));
184
185         while ((ch = getopt(argc, argv, "f:i:n:o:ps:t:")) != -1) {
186                 switch (ch) {
187                 case 'f':
188                         if (!strcmp(optarg, "refthread"))
189                                 io_test.flags |= NVME_TEST_FLAG_REFTHREAD;
190                         break;
191                 case 'i':
192                         if (!strcmp(optarg, "bio") ||
193                             !strcmp(optarg, "wait"))
194                                 ioctl_cmd = NVME_BIO_TEST;
195                         else if (!strcmp(optarg, "io") ||
196                                  !strcmp(optarg, "intr"))
197                                 ioctl_cmd = NVME_IO_TEST;
198                         break;
199                 case 'n':
200                         nflag = true;
201                         io_test.num_threads = strtoul(optarg, &p, 0);
202                         if (p != NULL && *p != '\0') {
203                                 fprintf(stderr,
204                                     "\"%s\" not valid number of threads.\n",
205                                     optarg);
206                                 perftest_usage();
207                         } else if (io_test.num_threads == 0 ||
208                                    io_test.num_threads > 128) {
209                                 fprintf(stderr,
210                                     "\"%s\" not valid number of threads.\n",
211                                     optarg);
212                                 perftest_usage();
213                         }
214                         break;
215                 case 'o':
216                         oflag = true;
217                         if (!strcmp(optarg, "read") || !strcmp(optarg, "READ"))
218                                 io_test.opc = NVME_OPC_READ;
219                         else if (!strcmp(optarg, "write") ||
220                                  !strcmp(optarg, "WRITE"))
221                                 io_test.opc = NVME_OPC_WRITE;
222                         else {
223                                 fprintf(stderr, "\"%s\" not valid opcode.\n",
224                                     optarg);
225                                 perftest_usage();
226                         }
227                         break;
228                 case 'p':
229                         perthread = 1;
230                         break;
231                 case 's':
232                         sflag = true;
233                         io_test.size = strtoul(optarg, &p, 0);
234                         if (p == NULL || *p == '\0' || toupper(*p) == 'B') {
235                                 // do nothing
236                         } else if (toupper(*p) == 'K') {
237                                 io_test.size *= 1024;
238                         } else if (toupper(*p) == 'M') {
239                                 io_test.size *= 1024 * 1024;
240                         } else {
241                                 fprintf(stderr, "\"%s\" not valid size.\n",
242                                     optarg);
243                                 perftest_usage();
244                         }
245                         break;
246                 case 't':
247                         tflag = true;
248                         io_test.time = strtoul(optarg, &p, 0);
249                         if (p != NULL && *p != '\0') {
250                                 fprintf(stderr,
251                                     "\"%s\" not valid time duration.\n",
252                                     optarg);
253                                 perftest_usage();
254                         }
255                         break;
256                 }
257         }
258
259         if (!nflag || !oflag || !sflag || !tflag || optind >= argc)
260                 perftest_usage();
261
262         open_dev(argv[optind], &fd, 1, 1);
263         if (ioctl(fd, ioctl_cmd, &io_test) < 0) {
264                 fprintf(stderr, "NVME_IO_TEST failed. errno=%d (%s)\n", errno,
265                     strerror(errno));
266                 close(fd);
267                 exit(EX_IOERR);
268         }
269
270         close(fd);
271         print_perftest(&io_test, perthread);
272         exit(EX_OK);
273 }
274
275 static void
276 reset_usage(void)
277 {
278         fprintf(stderr, "usage:\n");
279         fprintf(stderr, RESET_USAGE);
280         exit(EX_USAGE);
281 }
282
283 static void
284 reset_ctrlr(int argc, char *argv[])
285 {
286         int     ch, fd;
287
288         while ((ch = getopt(argc, argv, "")) != -1) {
289                 switch ((char)ch) {
290                 default:
291                         reset_usage();
292                 }
293         }
294
295         open_dev(argv[optind], &fd, 1, 1);
296         if (ioctl(fd, NVME_RESET_CONTROLLER) < 0) {
297                 printf("Reset request to %s failed. errno=%d (%s)\n",
298                     argv[optind], errno, strerror(errno));
299                 exit(EX_IOERR);
300         }
301
302         exit(EX_OK);
303 }
304
305 int
306 main(int argc, char *argv[])
307 {
308
309         if (argc < 2)
310                 usage();
311
312         if (strcmp(argv[1], "devlist") == 0)
313                 devlist(argc-1, &argv[1]);
314         else if (strcmp(argv[1], "identify") == 0)
315                 identify(argc-1, &argv[1]);
316         else if (strcmp(argv[1], "perftest") == 0)
317                 perftest(argc-1, &argv[1]);
318         else if (strcmp(argv[1], "reset") == 0)
319                 reset_ctrlr(argc-1, &argv[1]);
320
321         usage();
322
323         return (0);
324 }
325