]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/nvmecontrol.c
Create generic command / arg parsing routines
[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 <dlfcn.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <paths.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "nvmecontrol.h"
50
51 static void
52 print_bytes(void *data, uint32_t length)
53 {
54         uint32_t        i, j;
55         uint8_t         *p, *end;
56
57         end = (uint8_t *)data + length;
58
59         for (i = 0; i < length; i++) {
60                 p = (uint8_t *)data + (i*16);
61                 printf("%03x: ", i*16);
62                 for (j = 0; j < 16 && p < end; j++)
63                         printf("%02x ", *p++);
64                 if (p >= end)
65                         break;
66                 printf("\n");
67         }
68         printf("\n");
69 }
70
71 static void
72 print_dwords(void *data, uint32_t length)
73 {
74         uint32_t        *p;
75         uint32_t        i, j;
76
77         p = (uint32_t *)data;
78         length /= sizeof(uint32_t);
79
80         for (i = 0; i < length; i+=8) {
81                 printf("%03x: ", i*4);
82                 for (j = 0; j < 8; j++)
83                         printf("%08x ", p[i+j]);
84                 printf("\n");
85         }
86
87         printf("\n");
88 }
89
90 void
91 print_hex(void *data, uint32_t length)
92 {
93         if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
94                 print_dwords(data, length);
95         else
96                 print_bytes(data, length);
97 }
98
99 void
100 read_controller_data(int fd, struct nvme_controller_data *cdata)
101 {
102         struct nvme_pt_command  pt;
103
104         memset(&pt, 0, sizeof(pt));
105         pt.cmd.opc = NVME_OPC_IDENTIFY;
106         pt.cmd.cdw10 = htole32(1);
107         pt.buf = cdata;
108         pt.len = sizeof(*cdata);
109         pt.is_read = 1;
110
111         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
112                 err(1, "identify request failed");
113
114         /* Convert data to host endian */
115         nvme_controller_data_swapbytes(cdata);
116
117         if (nvme_completion_is_error(&pt.cpl))
118                 errx(1, "identify request returned error");
119 }
120
121 void
122 read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
123 {
124         struct nvme_pt_command  pt;
125
126         memset(&pt, 0, sizeof(pt));
127         pt.cmd.opc = NVME_OPC_IDENTIFY;
128         pt.cmd.nsid = htole32(nsid);
129         pt.buf = nsdata;
130         pt.len = sizeof(*nsdata);
131         pt.is_read = 1;
132
133         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
134                 err(1, "identify request failed");
135
136         /* Convert data to host endian */
137         nvme_namespace_data_swapbytes(nsdata);
138
139         if (nvme_completion_is_error(&pt.cpl))
140                 errx(1, "identify request returned error");
141 }
142
143 int
144 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
145 {
146         char            full_path[64];
147
148         if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
149                 if (show_error)
150                         warnx("controller/namespace ids must begin with '%s'",
151                             NVME_CTRLR_PREFIX);
152                 if (exit_on_error)
153                         exit(1);
154                 else
155                         return (EINVAL);
156         }
157
158         snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
159         *fd = open(full_path, O_RDWR);
160         if (*fd < 0) {
161                 if (show_error)
162                         warn("could not open %s", full_path);
163                 if (exit_on_error)
164                         exit(1);
165                 else
166                         return (errno);
167         }
168
169         return (0);
170 }
171
172 void
173 parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid)
174 {
175         char    *nsloc;
176
177         /*
178          * Pull the namespace id from the string. +2 skips past the "ns" part
179          *  of the string.  Don't search past 10 characters into the string,
180          *  otherwise we know it is malformed.
181          */
182         nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
183         if (nsloc != NULL)
184                 *nsid = strtol(nsloc + 2, NULL, 10);
185         if (nsloc == NULL || (*nsid == 0 && errno != 0))
186                 errx(1, "invalid namespace ID '%s'", ns_str);
187
188         /*
189          * The controller string will include only the nvmX part of the
190          *  nvmeXnsY string.
191          */
192         snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
193 }
194
195 int
196 main(int argc, char *argv[])
197 {
198
199         cmd_init();
200
201         cmd_load_dir("/lib/nvmecontrol", NULL, NULL);
202         cmd_load_dir("/usr/local/lib/nvmecontrol", NULL, NULL);
203
204         cmd_dispatch(argc, argv, NULL);
205
206         return (0);
207 }