summaryrefslogtreecommitdiffstats
path: root/freebsd/sbin/nvmecontrol/passthru.c
blob: 979cc87391b0f0eb055462ff2e61838f8247ab63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <machine/rtems-bsd-user-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2012-2013 Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/ioccom.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nvmecontrol.h"
#include "comnd.h"

static struct options {
	uint8_t		opcode;
	uint8_t		flags;
	uint16_t	rsvd;
	uint32_t	nsid;
	uint32_t	data_len;
	uint32_t	metadata_len;
	uint32_t	timeout;
	uint32_t	cdw2;
	uint32_t	cdw3;
	uint32_t	cdw10;
	uint32_t	cdw11;
	uint32_t	cdw12;
	uint32_t	cdw13;
	uint32_t	cdw14;
	uint32_t	cdw15;
	const char	*ifn;
	bool		binary;
	bool		show_command;
	bool		dry_run;
	bool		read;
	bool		write;
	uint8_t		prefill;
	const char	*dev;
} opt = {
	.binary = false,
	.cdw10 = 0,
	.cdw11 = 0,
	.cdw12 = 0,
	.cdw13 = 0,
	.cdw14 = 0,
	.cdw15 = 0,
	.cdw2 = 0,
	.cdw3 = 0,
	.data_len = 0,
	.dry_run = false,
	.flags = 0,
	.ifn = "",
	.metadata_len = 0,
	.nsid = 0,
	.opcode = 0,
	.prefill = 0,
	.read = false,
	.rsvd = 0,
	.show_command = false,
	.timeout = 0,
	.write = false,
	.dev = NULL,
};

/*
 * Argument names and short names selected to match the nvme-cli program
 * so vendor-siupplied formulas work out of the box on FreeBSD with a simple
 * s/nvme/nvmecontrol/.
 */
#define ARG(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }

static struct opts opts[] = {
	ARG("opcode",		'o',	arg_uint8,	opt, opcode,
	    "NVMe command opcode (required)"),
	ARG("cdw2",		'2',	arg_uint32,	opt, cdw2,
	    "Command dword 2 value"),
	ARG("cdw3",		'3',	arg_uint32,	opt, cdw3,
	    "Command dword 3 value"),
	ARG("cdw10",		'4',	arg_uint32,	opt, cdw10,
	    "Command dword 10 value"),
	ARG("cdw11",		'5',	arg_uint32,	opt, cdw11,
	    "Command dword 11 value"),
	ARG("cdw12",		'6',	arg_uint32,	opt, cdw12,
	    "Command dword 12 value"),
	ARG("cdw13",		'7',	arg_uint32,	opt, cdw13,
	    "Command dword 13 value"),
	ARG("cdw14",		'8',	arg_uint32,	opt, cdw14,
	    "Command dword 14 value"),
	ARG("cdw15",		'9',	arg_uint32,	opt, cdw15,
	    "Command dword 15 value"),
	ARG("data-len",		'l',	arg_uint32,	opt, data_len,
	    "Length of data for I/O (bytes)"),
	ARG("metadata-len",	'm',	arg_uint32,	opt, metadata_len,
	    "Length of metadata segment (bytes) (igored)"),
	ARG("flags",		'f',	arg_uint8,	opt, flags,
	    "NVMe command flags"),
	ARG("input-file",	'i',	arg_path,	opt, ifn,
	    "Input file to send (default stdin)"),
	ARG("namespace-id",	'n',	arg_uint32,	opt, nsid,
	    "Namespace id (ignored on FreeBSD)"),
	ARG("prefill",		'p',	arg_uint8,	opt, prefill,
	    "Value to prefill payload with"),
	ARG("rsvd",		'R',	arg_uint16,	opt, rsvd,
	    "Reserved field value"),
	ARG("timeout",		't',	arg_uint32,	opt, timeout,
	    "Command timeout (ms)"),
	ARG("raw-binary",	'b',	arg_none,	opt, binary,
	    "Output in binary format"),
	ARG("dry-run",		'd',	arg_none,	opt, dry_run,
	    "Don't actually execute the command"),
	ARG("read",		'r',	arg_none,	opt, read,
	    "Command reads data from device"),
	ARG("show-command",	's',	arg_none,	opt, show_command,
	    "Show all the command values on stdout"),
	ARG("write",		'w',	arg_none,	opt, write,
	    "Command writes data to device"),
	{ NULL, 0, arg_none, NULL, NULL }
};

static const struct args args[] = {
	{ arg_string, &opt.dev, "controller-id|namespace-id" },
	{ arg_none, NULL, NULL },
};

static void
passthru(const struct cmd *f, int argc, char *argv[])
{
	int	fd = -1, ifd = -1;
	void	*data = NULL, *metadata = NULL;
	struct nvme_pt_command	pt;

	arg_parse(argc, argv, f);
	open_dev(argv[optind], &fd, 1, 1);

	if (opt.read && opt.write)
		errx(1, "need exactly one of --read or --write");
	if (opt.data_len != 0 && !opt.read && !opt.write)
		errx(1, "need exactly one of --read or --write");
	if (*opt.ifn && (ifd = open(opt.ifn, O_RDONLY)) == -1) {
		warn("open %s", opt.ifn);
		goto cleanup;
	}
#if notyet	/* No support in kernel for this */
	if (opt.metadata_len != 0) {
		if (posix_memalign(&metadata, getpagesize(), opt.metadata_len)) {
			warn("can't allocate %d bytes for metadata", metadata_len);
			goto cleanup;
		}
	}
#else
	if (opt.metadata_len != 0)
		errx(1, "metadata not supported on FreeBSD");
#endif
	if (opt.data_len) {
		if (posix_memalign(&data, getpagesize(), opt.data_len)) {
			warn("can't allocate %d bytes for data", opt.data_len);
			goto cleanup;
		}
		memset(data, opt.prefill, opt.data_len);
		if (opt.write && read(ifd, data, opt.data_len) < 0) {
			warn("read %s", *opt.ifn ? opt.ifn : "stdin");
			goto cleanup;
		}
	}
	if (opt.show_command) {
		fprintf(stderr, "opcode       : %#02x\n", opt.opcode);
		fprintf(stderr, "flags        : %#02x\n", opt.flags);
		fprintf(stderr, "rsvd1        : %#04x\n", opt.rsvd);
		fprintf(stderr, "nsid         : %#04x\n", opt.nsid);
		fprintf(stderr, "cdw2         : %#08x\n", opt.cdw2);
		fprintf(stderr, "cdw3         : %#08x\n", opt.cdw3);
		fprintf(stderr, "data_len     : %#08x\n", opt.data_len);
		fprintf(stderr, "metadata_len : %#08x\n", opt.metadata_len);
		fprintf(stderr, "data         : %p\n", data);
		fprintf(stderr, "metadata     : %p\n", metadata);
		fprintf(stderr, "cdw10        : %#08x\n", opt.cdw10);
		fprintf(stderr, "cdw11        : %#08x\n", opt.cdw11);
		fprintf(stderr, "cdw12        : %#08x\n", opt.cdw12);
		fprintf(stderr, "cdw13        : %#08x\n", opt.cdw13);
		fprintf(stderr, "cdw14        : %#08x\n", opt.cdw14);
		fprintf(stderr, "cdw15        : %#08x\n", opt.cdw15);
		fprintf(stderr, "timeout_ms   : %d\n", opt.timeout);
	}
	if (opt.dry_run) {
		errno = 0;
		warn("Doing a dry-run, no actual I/O");
		goto cleanup;
	}

	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = opt.opcode;
	pt.cmd.fuse = opt.flags;
	pt.cmd.cid = htole16(opt.rsvd);
	pt.cmd.nsid = opt.nsid;				/* XXX note: kernel overrides this */
	pt.cmd.rsvd2 = htole32(opt.cdw2);
	pt.cmd.rsvd3 = htole32(opt.cdw3);
	pt.cmd.cdw10 = htole32(opt.cdw10);
	pt.cmd.cdw11 = htole32(opt.cdw11);
	pt.cmd.cdw12 = htole32(opt.cdw12);
	pt.cmd.cdw13 = htole32(opt.cdw13);
	pt.cmd.cdw14 = htole32(opt.cdw14);
	pt.cmd.cdw15 = htole32(opt.cdw15);
	pt.buf = data;
	pt.len = opt.data_len;
	pt.is_read = opt.read;

	errno = 0;
	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "passthrough request failed");
	/* XXX report status */
	if (opt.read) {
		if (opt.binary)
			write(STDOUT_FILENO, data, opt.data_len);
		else {
			/* print status here */
			print_hex(data, opt.data_len);
		}
	}
cleanup:
	if (errno)
		exit(1);
}

static void
admin_passthru(const struct cmd *nf, int argc, char *argv[])
{

	passthru(nf, argc, argv);
}

static void
io_passthru(const struct cmd *nf, int argc, char *argv[])
{

	passthru(nf, argc, argv);
}

static struct cmd admin_pass_cmd = {
	.name = "admin-passthru",
	.fn = admin_passthru,
	.ctx_size = sizeof(struct options),
	.opts = opts,
	.args = args,
	.descr = "Send a pass through Admin command to the specified device",
};

static struct cmd io_pass_cmd = {
	.name = "io-passthru",
	.fn = io_passthru,
	.ctx_size = sizeof(struct options),
	.opts = opts,
	.args = args,
	.descr = "Send a pass through Admin command to the specified device",
};

CMD_COMMAND(admin_pass_cmd);
CMD_COMMAND(io_pass_cmd);