nvmecontrol.c revision 252667
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: stable/9/sbin/nvmecontrol/nvmecontrol.c 252667 2013-07-04 00:00:59Z jimharris $");
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
47typedef void (*nvme_fn_t)(int argc, char *argv[]);
48
49static 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
61static void
62usage(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
75void
76read_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
99void
100read_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
123int
124open_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	if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
130		if (show_error)
131			fprintf(stderr,
132			    "Controller/namespace IDs must begin with '%s'.\n",
133			    NVME_CTRLR_PREFIX);
134		if (exit_on_error)
135			exit(EX_USAGE);
136		else
137			return (EX_USAGE);
138	}
139
140	snprintf(full_path, sizeof(full_path), "/dev/%s", str);
141	if (stat(full_path, &devstat) != 0) {
142		if (show_error)
143			fprintf(stderr, "Could not stat %s. errno=%d (%s)\n",
144			    full_path, errno, strerror(errno));
145		if (exit_on_error)
146			exit(EX_NOINPUT);
147		else
148			return (EX_NOINPUT);
149	}
150
151	*fd = open(full_path, O_RDWR);
152	if (*fd < 0) {
153		if (show_error)
154			fprintf(stderr, "Could not open %s. errno=%d (%s)\n",
155			    full_path, errno, strerror(errno));
156		if (exit_on_error)
157			exit(EX_NOPERM);
158		else
159			return (EX_NOPERM);
160	}
161
162	return (EX_OK);
163}
164
165int
166main(int argc, char *argv[])
167{
168	struct nvme_function *f;
169
170	if (argc < 2)
171		usage();
172
173	f = funcs;
174	while (f->name != NULL) {
175		if (strcmp(argv[1], f->name) == 0)
176			f->fn(argc-1, &argv[1]);
177		f++;
178	}
179
180	usage();
181
182	return (0);
183}
184
185