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>
48 #include "nvmecontrol.h"
51 static struct nvme_function funcs[] = {
52 {"devlist", devlist, DEVLIST_USAGE},
53 {"identify", identify, IDENTIFY_USAGE},
54 {"perftest", perftest, PERFTEST_USAGE},
55 {"reset", reset, RESET_USAGE},
56 {"logpage", logpage, LOGPAGE_USAGE},
57 {"firmware", firmware, FIRMWARE_USAGE},
58 {"format", format, FORMAT_USAGE},
59 {"power", power, POWER_USAGE},
60 {"wdc", wdc, WDC_USAGE},
66 gen_usage(struct nvme_function *f)
69 fprintf(stderr, "usage:\n");
70 while (f->name != NULL) {
71 fprintf(stderr, "%s", f->usage);
78 dispatch(int argc, char *argv[], struct nvme_function *tbl)
80 struct nvme_function *f = tbl;
82 if (argv[1] == NULL) {
87 while (f->name != NULL) {
88 if (strcmp(argv[1], f->name) == 0)
89 f->fn(argc-1, &argv[1]);
93 fprintf(stderr, "Unknown command: %s\n", argv[1]);
98 print_bytes(void *data, uint32_t length)
103 end = (uint8_t *)data + length;
105 for (i = 0; i < length; i++) {
106 p = (uint8_t *)data + (i*16);
107 printf("%03x: ", i*16);
108 for (j = 0; j < 16 && p < end; j++)
109 printf("%02x ", *p++);
118 print_dwords(void *data, uint32_t length)
123 p = (uint32_t *)data;
124 length /= sizeof(uint32_t);
126 for (i = 0; i < length; i+=8) {
127 printf("%03x: ", i*4);
128 for (j = 0; j < 8; j++)
129 printf("%08x ", p[i+j]);
137 print_hex(void *data, uint32_t length)
139 if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
140 print_dwords(data, length);
142 print_bytes(data, length);
146 read_controller_data(int fd, struct nvme_controller_data *cdata)
148 struct nvme_pt_command pt;
150 memset(&pt, 0, sizeof(pt));
151 pt.cmd.opc = NVME_OPC_IDENTIFY;
152 pt.cmd.cdw10 = htole32(1);
154 pt.len = sizeof(*cdata);
157 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
158 err(1, "identify request failed");
160 /* Convert data to host endian */
161 nvme_controller_data_swapbytes(cdata);
163 if (nvme_completion_is_error(&pt.cpl))
164 errx(1, "identify request returned error");
168 read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
170 struct nvme_pt_command pt;
172 memset(&pt, 0, sizeof(pt));
173 pt.cmd.opc = NVME_OPC_IDENTIFY;
174 pt.cmd.nsid = htole32(nsid);
176 pt.len = sizeof(*nsdata);
179 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
180 err(1, "identify request failed");
182 /* Convert data to host endian */
183 nvme_namespace_data_swapbytes(nsdata);
185 if (nvme_completion_is_error(&pt.cpl))
186 errx(1, "identify request returned error");
190 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
194 if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
196 warnx("controller/namespace ids must begin with '%s'",
204 snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
205 *fd = open(full_path, O_RDWR);
208 warn("could not open %s", full_path);
219 parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid)
224 * Pull the namespace id from the string. +2 skips past the "ns" part
225 * of the string. Don't search past 10 characters into the string,
226 * otherwise we know it is malformed.
228 nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
230 *nsid = strtol(nsloc + 2, NULL, 10);
231 if (nsloc == NULL || (*nsid == 0 && errno != 0))
232 errx(1, "invalid namespace ID '%s'", ns_str);
235 * The controller string will include only the nvmX part of the
238 snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
242 main(int argc, char *argv[])
248 dispatch(argc, argv, funcs);