2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2013 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
50 #include "nvmecontrol.h"
52 SET_CONCAT_DEF(top, struct nvme_function);
55 print_usage(const struct nvme_function *f)
59 bool need_prefix = true;
66 fputs(" nvmecontrol ", stderr);
71 need_prefix = (ch == '\n');
78 gen_usage_set(const struct nvme_function * const *f, const struct nvme_function * const *flimit)
81 fprintf(stderr, "usage:\n");
90 usage(const struct nvme_function *f)
93 fprintf(stderr, "usage:\n");
99 dispatch_set(int argc, char *argv[], const struct nvme_function * const *tbl,
100 const struct nvme_function * const *tbl_limit)
102 const struct nvme_function * const *f = tbl;
104 if (argv[1] == NULL) {
105 gen_usage_set(tbl, tbl_limit);
109 while (f < tbl_limit) {
110 if (strcmp(argv[1], (*f)->name) == 0) {
111 (*f)->fn(*f, argc-1, &argv[1]);
117 fprintf(stderr, "Unknown command: %s\n", argv[1]);
118 gen_usage_set(tbl, tbl_limit);
122 set_concat_add(struct set_concat *m, void *b, void *e)
130 * Args are really pointers to arrays of pointers, but C's
131 * casting rules kinda suck since you can't directly cast
132 * struct foo ** to a void **.
138 if (m->begin != NULL)
139 cur_n = m->limit - m->begin;
140 m->begin = reallocarray(m->begin, cur_n + add_n, sizeof(void *));
141 if (m->begin == NULL)
142 err(1, "expanding concat set");
143 memcpy(m->begin + cur_n, bp, add_n * sizeof(void *));
144 m->limit = m->begin + cur_n + add_n;
148 print_bytes(void *data, uint32_t length)
153 end = (uint8_t *)data + length;
155 for (i = 0; i < length; i++) {
156 p = (uint8_t *)data + (i*16);
157 printf("%03x: ", i*16);
158 for (j = 0; j < 16 && p < end; j++)
159 printf("%02x ", *p++);
168 print_dwords(void *data, uint32_t length)
173 p = (uint32_t *)data;
174 length /= sizeof(uint32_t);
176 for (i = 0; i < length; i+=8) {
177 printf("%03x: ", i*4);
178 for (j = 0; j < 8; j++)
179 printf("%08x ", p[i+j]);
187 print_hex(void *data, uint32_t length)
189 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
190 print_dwords(data, length);
192 print_bytes(data, length);
196 read_controller_data(int fd, struct nvme_controller_data *cdata)
198 struct nvme_pt_command pt;
200 memset(&pt, 0, sizeof(pt));
201 pt.cmd.opc = NVME_OPC_IDENTIFY;
202 pt.cmd.cdw10 = htole32(1);
204 pt.len = sizeof(*cdata);
207 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
208 err(1, "identify request failed");
210 /* Convert data to host endian */
211 nvme_controller_data_swapbytes(cdata);
213 if (nvme_completion_is_error(&pt.cpl))
214 errx(1, "identify request returned error");
218 read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
220 struct nvme_pt_command pt;
222 memset(&pt, 0, sizeof(pt));
223 pt.cmd.opc = NVME_OPC_IDENTIFY;
224 pt.cmd.nsid = htole32(nsid);
226 pt.len = sizeof(*nsdata);
229 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
230 err(1, "identify request failed");
232 /* Convert data to host endian */
233 nvme_namespace_data_swapbytes(nsdata);
235 if (nvme_completion_is_error(&pt.cpl))
236 errx(1, "identify request returned error");
240 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
244 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
246 warnx("controller/namespace ids must begin with '%s'",
254 snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
255 *fd = open(full_path, O_RDWR);
258 warn("could not open %s", full_path);
269 parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid)
274 * Pull the namespace id from the string. +2 skips past the "ns" part
275 * of the string. Don't search past 10 characters into the string,
276 * otherwise we know it is malformed.
278 nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
280 *nsid = strtol(nsloc + 2, NULL, 10);
281 if (nsloc == NULL || (*nsid == 0 && errno != 0))
282 errx(1, "invalid namespace ID '%s'", ns_str);
285 * The controller string will include only the nvmX part of the
288 snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
292 * Loads all the .so's from the specified directory.
295 load_dir(const char *dir)
305 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
306 if (strcmp(".so", dent->d_name + dent->d_namlen - 3) != 0)
308 asprintf(&path, "%s/%s", dir, dent->d_name);
310 err(1, "Can't malloc for path, giving up.");
311 if ((h = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)
312 warnx("Can't load %s: %s", path, dlerror());
315 * Add in the top (for cli commands)linker sets. We have
316 * to do this by hand because linker sets aren't
317 * automatically merged.
321 begin = dlsym(h, "__start_set_top");
322 limit = dlsym(h, "__stop_set_top");
324 add_to_top(begin, limit);
325 /* log pages use constructors so are done on load */
334 main(int argc, char *argv[])
337 add_to_top(NVME_CMD_BEGIN(top), NVME_CMD_LIMIT(top));
339 load_dir("/lib/nvmecontrol");
340 load_dir("/usr/local/lib/nvmecontrol");
343 gen_usage_set(top_begin(), top_limit());
345 dispatch_set(argc, argv, top_begin(), top_limit());