]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sbin/nvmecontrol/nvmecontrol.c
MFC r252269:
[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 typedef void (*nvme_fn_t)(int argc, char *argv[]);
48
49 static struct nvme_function {
50         const char      *name;
51         nvme_fn_t       fn;
52         const char      *usage;
53 } funcs[] = {
54         {"devlist",     devlist,        DEVLIST_USAGE},
55         {"identify",    identify,       IDENTIFY_USAGE},
56         {"perftest",    perftest,       PERFTEST_USAGE},
57         {"reset",       reset,          RESET_USAGE},
58         {NULL,          NULL,           NULL},
59 };
60
61 static void
62 usage(void)
63 {
64         struct nvme_function *f;
65
66         f = funcs;
67         fprintf(stderr, "usage:\n");
68         while (f->name != NULL) {
69                 fprintf(stderr, "%s", f->usage);
70                 f++;
71         }
72         exit(EX_USAGE);
73 }
74
75 void
76 read_controller_data(int fd, struct nvme_controller_data *cdata)
77 {
78         struct nvme_pt_command  pt;
79
80         memset(&pt, 0, sizeof(pt));
81         pt.cmd.opc = NVME_OPC_IDENTIFY;
82         pt.cmd.cdw10 = 1;
83         pt.buf = cdata;
84         pt.len = sizeof(*cdata);
85         pt.is_read = 1;
86
87         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
88                 printf("Identify request failed. errno=%d (%s)\n",
89                     errno, strerror(errno));
90                 exit(EX_IOERR);
91         }
92
93         if (nvme_completion_is_error(&pt.cpl)) {
94                 printf("Passthrough command returned error.\n");
95                 exit(EX_IOERR);
96         }
97 }
98
99 void
100 read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
101 {
102         struct nvme_pt_command  pt;
103
104         memset(&pt, 0, sizeof(pt));
105         pt.cmd.opc = NVME_OPC_IDENTIFY;
106         pt.cmd.nsid = nsid;
107         pt.buf = nsdata;
108         pt.len = sizeof(*nsdata);
109         pt.is_read = 1;
110
111         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
112                 printf("Identify request failed. errno=%d (%s)\n",
113                     errno, strerror(errno));
114                 exit(EX_IOERR);
115         }
116
117         if (nvme_completion_is_error(&pt.cpl)) {
118                 printf("Passthrough command returned error.\n");
119                 exit(EX_IOERR);
120         }
121 }
122
123 int
124 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
125 {
126         struct stat     devstat;
127         char            full_path[64];
128
129         snprintf(full_path, sizeof(full_path), "/dev/%s", str);
130         if (stat(full_path, &devstat) != 0) {
131                 if (show_error)
132                         fprintf(stderr, "error\n");
133                 if (exit_on_error)
134                         exit(EX_NOINPUT);
135                 else
136                         return (EX_NOINPUT);
137         }
138
139         *fd = open(full_path, O_RDWR);
140         if (*fd < 0) {
141                 if (show_error)
142                         printf("Could not open %s. errno=%d (%s)\n", full_path,
143                             errno, strerror(errno));
144                 if (exit_on_error)
145                         exit(EX_NOPERM);
146                 else
147                         return (EX_NOPERM);
148         }
149
150         return (EX_OK);
151 }
152
153 int
154 main(int argc, char *argv[])
155 {
156         struct nvme_function *f;
157
158         if (argc < 2)
159                 usage();
160
161         f = funcs;
162         while (f->name != NULL) {
163                 if (strcmp(argv[1], f->name) == 0)
164                         f->fn(argc-1, &argv[1]);
165                 f++;
166         }
167
168         usage();
169
170         return (0);
171 }
172