2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ioccom.h>
45 #include "nvmecontrol.h"
47 typedef void (*nvme_fn_t)(int argc, char *argv[]);
49 static struct nvme_function {
54 {"devlist", devlist, DEVLIST_USAGE},
55 {"identify", identify, IDENTIFY_USAGE},
56 {"perftest", perftest, PERFTEST_USAGE},
57 {"reset", reset, RESET_USAGE},
64 struct nvme_function *f;
67 fprintf(stderr, "usage:\n");
68 while (f->name != NULL) {
69 fprintf(stderr, "%s", f->usage);
76 read_controller_data(int fd, struct nvme_controller_data *cdata)
78 struct nvme_pt_command pt;
80 memset(&pt, 0, sizeof(pt));
81 pt.cmd.opc = NVME_OPC_IDENTIFY;
84 pt.len = sizeof(*cdata);
87 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
88 printf("Identify request failed. errno=%d (%s)\n",
89 errno, strerror(errno));
93 if (nvme_completion_is_error(&pt.cpl)) {
94 printf("Passthrough command returned error.\n");
100 read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
102 struct nvme_pt_command pt;
104 memset(&pt, 0, sizeof(pt));
105 pt.cmd.opc = NVME_OPC_IDENTIFY;
108 pt.len = sizeof(*nsdata);
111 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) {
112 printf("Identify request failed. errno=%d (%s)\n",
113 errno, strerror(errno));
117 if (nvme_completion_is_error(&pt.cpl)) {
118 printf("Passthrough command returned error.\n");
124 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
129 snprintf(full_path, sizeof(full_path), "/dev/%s", str);
130 if (stat(full_path, &devstat) != 0) {
132 fprintf(stderr, "error\n");
139 *fd = open(full_path, O_RDWR);
142 printf("Could not open %s. errno=%d (%s)\n", full_path,
143 errno, strerror(errno));
154 main(int argc, char *argv[])
156 struct nvme_function *f;
162 while (f->name != NULL) {
163 if (strcmp(argv[1], f->name) == 0)
164 f->fn(argc-1, &argv[1]);