xref: /freebsd/sbin/nvmecontrol/resv.c (revision a50d73d5782a351ad83e8d1f84d11720a12e70d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
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.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/ioccom.h>
33 
34 #include <err.h>
35 #include <fcntl.h>
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43 
44 #include "nvmecontrol.h"
45 
46 /* Tables for command line parsing */
47 
48 static cmd_fn_t resv;
49 static cmd_fn_t resvacquire;
50 static cmd_fn_t resvregister;
51 static cmd_fn_t resvrelease;
52 static cmd_fn_t resvreport;
53 
54 #define NONE 0xffffffffu
55 #define NONE64 0xffffffffffffffffull
56 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
57 #define OPT_END	{ NULL, 0, arg_none, NULL, NULL }
58 
59 static struct cmd resv_cmd = {
60 	.name = "resv",
61 	.fn = resv,
62 	.descr = "Reservation commands",
63 	.ctx_size = 0,
64 	.opts = NULL,
65 	.args = NULL,
66 };
67 
68 CMD_COMMAND(resv_cmd);
69 
70 static struct acquire_options {
71 	uint64_t	crkey;
72 	uint64_t	prkey;
73 	uint8_t		rtype;
74 	uint8_t		racqa;
75 	const char	*dev;
76 } acquire_opt = {
77 	.crkey = 0,
78 	.prkey = 0,
79 	.rtype = 0,
80 	.racqa = 0,
81 	.dev = NULL,
82 };
83 
84 static const struct opts acquire_opts[] = {
85 	OPT("crkey", 'c', arg_uint64, acquire_opt, crkey,
86 	    "Current Reservation Key"),
87 	OPT("prkey", 'p', arg_uint64, acquire_opt, prkey,
88 	    "Preempt Reservation Key"),
89 	OPT("rtype", 't', arg_uint8, acquire_opt, rtype,
90 	    "Reservation Type"),
91 	OPT("racqa", 'a', arg_uint8, acquire_opt, racqa,
92 	    "Acquire Action (0=acq, 1=pre, 2=pre+ab)"),
93 	{ NULL, 0, arg_none, NULL, NULL }
94 };
95 
96 static const struct args acquire_args[] = {
97 	{ arg_string, &acquire_opt.dev, "namespace-id" },
98 	{ arg_none, NULL, NULL },
99 };
100 
101 static struct cmd acquire_cmd = {
102 	.name = "acquire",
103 	.fn = resvacquire,
104 	.descr = "Acquire/preempt reservation",
105 	.ctx_size = sizeof(acquire_opt),
106 	.opts = acquire_opts,
107 	.args = acquire_args,
108 };
109 
110 CMD_SUBCOMMAND(resv_cmd, acquire_cmd);
111 
112 static struct register_options {
113 	uint64_t	crkey;
114 	uint64_t	nrkey;
115 	uint8_t		rrega;
116 	bool		iekey;
117 	uint8_t		cptpl;
118 	const char	*dev;
119 } register_opt = {
120 	.crkey = 0,
121 	.nrkey = 0,
122 	.rrega = 0,
123 	.iekey = false,
124 	.cptpl = 0,
125 	.dev = NULL,
126 };
127 
128 static const struct opts register_opts[] = {
129 	OPT("crkey", 'c', arg_uint64, register_opt, crkey,
130 	    "Current Reservation Key"),
131 	OPT("nrkey", 'k', arg_uint64, register_opt, nrkey,
132 	    "New Reservation Key"),
133 	OPT("rrega", 'r', arg_uint8, register_opt, rrega,
134 	    "Register Action (0=reg, 1=unreg, 2=replace)"),
135 	OPT("iekey", 'i', arg_none, register_opt, iekey,
136 	    "Ignore Existing Key"),
137 	OPT("cptpl", 'p', arg_uint8, register_opt, cptpl,
138 	    "Change Persist Through Power Loss State"),
139 	{ NULL, 0, arg_none, NULL, NULL }
140 };
141 
142 static const struct args register_args[] = {
143 	{ arg_string, &register_opt.dev, "namespace-id" },
144 	{ arg_none, NULL, NULL },
145 };
146 
147 static struct cmd register_cmd = {
148 	.name = "register",
149 	.fn = resvregister,
150 	.descr = "Register/unregister reservation",
151 	.ctx_size = sizeof(register_opt),
152 	.opts = register_opts,
153 	.args = register_args,
154 };
155 
156 CMD_SUBCOMMAND(resv_cmd, register_cmd);
157 
158 static struct release_options {
159 	uint64_t	crkey;
160 	uint8_t		rtype;
161 	uint8_t		rrela;
162 	const char	*dev;
163 } release_opt = {
164 	.crkey = 0,
165 	.rtype = 0,
166 	.rrela = 0,
167 	.dev = NULL,
168 };
169 
170 static const struct opts release_opts[] = {
171 	OPT("crkey", 'c', arg_uint64, release_opt, crkey,
172 	    "Current Reservation Key"),
173 	OPT("rtype", 't', arg_uint8, release_opt, rtype,
174 	    "Reservation Type"),
175 	OPT("rrela", 'a', arg_uint8, release_opt, rrela,
176 	    "Release Action (0=release, 1=clear)"),
177 	{ NULL, 0, arg_none, NULL, NULL }
178 };
179 
180 static const struct args release_args[] = {
181 	{ arg_string, &release_opt.dev, "namespace-id" },
182 	{ arg_none, NULL, NULL },
183 };
184 
185 static struct cmd release_cmd = {
186 	.name = "release",
187 	.fn = resvrelease,
188 	.descr = "Release/clear reservation",
189 	.ctx_size = sizeof(release_opt),
190 	.opts = release_opts,
191 	.args = release_args,
192 };
193 
194 CMD_SUBCOMMAND(resv_cmd, release_cmd);
195 
196 static struct report_options {
197 	bool		hex;
198 	bool		verbose;
199 	bool		eds;
200 	const char	*dev;
201 } report_opt = {
202 	.hex = false,
203 	.verbose = false,
204 	.eds = false,
205 	.dev = NULL,
206 };
207 
208 static const struct opts report_opts[] = {
209 	OPT("hex", 'x', arg_none, report_opt, hex,
210 	    "Print reservation status in hex"),
211 	OPT("verbose", 'v', arg_none, report_opt, verbose,
212 	    "More verbosity"),
213 	OPT("eds", 'e', arg_none, report_opt, eds,
214 	    "Extended Data Structure"),
215 	{ NULL, 0, arg_none, NULL, NULL }
216 };
217 
218 static const struct args report_args[] = {
219 	{ arg_string, &report_opt.dev, "namespace-id" },
220 	{ arg_none, NULL, NULL },
221 };
222 
223 static struct cmd report_cmd = {
224 	.name = "report",
225 	.fn = resvreport,
226 	.descr = "Print reservation status",
227 	.ctx_size = sizeof(report_opt),
228 	.opts = report_opts,
229 	.args = report_args,
230 };
231 
232 CMD_SUBCOMMAND(resv_cmd, report_cmd);
233 
234 /* handles NVME_OPC_RESERVATION_* NVM commands */
235 
236 static void
237 resvacquire(const struct cmd *f, int argc, char *argv[])
238 {
239 	struct nvme_pt_command	pt;
240 	uint64_t	data[2];
241 	int		fd;
242 	uint32_t	nsid;
243 
244 	if (arg_parse(argc, argv, f))
245 		return;
246 	open_dev(acquire_opt.dev, &fd, 0, 1);
247 	get_nsid(fd, NULL, &nsid);
248 	if (nsid == 0) {
249 		fprintf(stderr, "This command require namespace-id\n");
250 		arg_help(argc, argv, f);
251 	}
252 
253 	data[0] = htole64(acquire_opt.crkey);
254 	data[1] = htole64(acquire_opt.prkey);
255 
256 	memset(&pt, 0, sizeof(pt));
257 	pt.cmd.opc = NVME_OPC_RESERVATION_ACQUIRE;
258 	pt.cmd.nsid = htole32(nsid);
259 	pt.cmd.cdw10 = htole32((acquire_opt.racqa & 7) |
260 	    (acquire_opt.rtype << 8));
261 	pt.buf = &data;
262 	pt.len = sizeof(data);
263 	pt.is_read = 0;
264 
265 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
266 		err(EX_IOERR, "acquire request failed");
267 
268 	if (nvme_completion_is_error(&pt.cpl))
269 		errx(EX_IOERR, "acquire request returned error");
270 
271 	close(fd);
272 	exit(0);
273 }
274 
275 static void
276 resvregister(const struct cmd *f, int argc, char *argv[])
277 {
278 	struct nvme_pt_command	pt;
279 	uint64_t	data[2];
280 	int		fd;
281 	uint32_t	nsid;
282 
283 	if (arg_parse(argc, argv, f))
284 		return;
285 	open_dev(register_opt.dev, &fd, 0, 1);
286 	get_nsid(fd, NULL, &nsid);
287 	if (nsid == 0) {
288 		fprintf(stderr, "This command require namespace-id\n");
289 		arg_help(argc, argv, f);
290 	}
291 
292 	data[0] = htole64(register_opt.crkey);
293 	data[1] = htole64(register_opt.nrkey);
294 
295 	memset(&pt, 0, sizeof(pt));
296 	pt.cmd.opc = NVME_OPC_RESERVATION_REGISTER;
297 	pt.cmd.nsid = htole32(nsid);
298 	pt.cmd.cdw10 = htole32((register_opt.rrega & 7) |
299 	    (register_opt.iekey << 3) | (register_opt.cptpl << 30));
300 	pt.buf = &data;
301 	pt.len = sizeof(data);
302 	pt.is_read = 0;
303 
304 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
305 		err(EX_IOERR, "register request failed");
306 
307 	if (nvme_completion_is_error(&pt.cpl))
308 		errx(EX_IOERR, "register request returned error");
309 
310 	close(fd);
311 	exit(0);
312 }
313 
314 static void
315 resvrelease(const struct cmd *f, int argc, char *argv[])
316 {
317 	struct nvme_pt_command	pt;
318 	uint64_t	data[1];
319 	int		fd;
320 	uint32_t	nsid;
321 
322 	if (arg_parse(argc, argv, f))
323 		return;
324 	open_dev(release_opt.dev, &fd, 0, 1);
325 	get_nsid(fd, NULL, &nsid);
326 	if (nsid == 0) {
327 		fprintf(stderr, "This command require namespace-id\n");
328 		arg_help(argc, argv, f);
329 	}
330 
331 	data[0] = htole64(release_opt.crkey);
332 
333 	memset(&pt, 0, sizeof(pt));
334 	pt.cmd.opc = NVME_OPC_RESERVATION_RELEASE;
335 	pt.cmd.nsid = htole32(nsid);
336 	pt.cmd.cdw10 = htole32((release_opt.rrela & 7) |
337 	    (release_opt.rtype << 8));
338 	pt.buf = &data;
339 	pt.len = sizeof(data);
340 	pt.is_read = 0;
341 
342 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
343 		err(EX_IOERR, "release request failed");
344 
345 	if (nvme_completion_is_error(&pt.cpl))
346 		errx(EX_IOERR, "release request returned error");
347 
348 	close(fd);
349 	exit(0);
350 }
351 
352 static void
353 resvreport(const struct cmd *f, int argc, char *argv[])
354 {
355 	struct nvme_pt_command	pt;
356 	struct nvme_resv_status	*s;
357 	struct nvme_resv_status_ext *e;
358 	uint8_t		data[4096] __aligned(4);
359 	int		fd;
360 	u_int		i, n;
361 	uint32_t	nsid;
362 
363 	if (arg_parse(argc, argv, f))
364 		return;
365 	open_dev(report_opt.dev, &fd, 0, 1);
366 	get_nsid(fd, NULL, &nsid);
367 	if (nsid == 0) {
368 		fprintf(stderr, "This command require namespace-id\n");
369 		arg_help(argc, argv, f);
370 	}
371 
372 	bzero(data, sizeof(data));
373 	memset(&pt, 0, sizeof(pt));
374 	pt.cmd.opc = NVME_OPC_RESERVATION_REPORT;
375 	pt.cmd.nsid = htole32(nsid);
376 	pt.cmd.cdw10 = htole32(sizeof(data) / 4 - 1);
377 	pt.cmd.cdw11 = htole32(report_opt.eds);	/* EDS */
378 	pt.buf = &data;
379 	pt.len = sizeof(data);
380 	pt.is_read = 1;
381 
382 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
383 		err(EX_IOERR, "report request failed");
384 
385 	if (nvme_completion_is_error(&pt.cpl))
386 		errx(EX_IOERR, "report request returned error");
387 
388 	close(fd);
389 
390 	if (report_opt.eds)
391 		nvme_resv_status_ext_swapbytes((void *)data, sizeof(data));
392 	else
393 		nvme_resv_status_swapbytes((void *)data, sizeof(data));
394 
395 	if (report_opt.hex) {
396 		i = sizeof(data);
397 		if (!report_opt.verbose) {
398 			for (; i > 64; i--) {
399 				if (data[i - 1] != 0)
400 					break;
401 			}
402 		}
403 		print_hex(&data, i);
404 		exit(0);
405 	}
406 
407 	s = (struct nvme_resv_status *)data;
408 	n = (s->regctl[1] << 8) | s->regctl[0];
409 	printf("Generation:                       %u\n", s->gen);
410 	printf("Reservation Type:                 %u\n", s->rtype);
411 	printf("Number of Registered Controllers: %u\n", n);
412 	printf("Persist Through Power Loss State: %u\n", s->ptpls);
413 	if (report_opt.eds) {
414 		e = (struct nvme_resv_status_ext *)data;
415 		n = MIN(n, (sizeof(data) - sizeof(e)) / sizeof(e->ctrlr[0]));
416 		for (i = 0; i < n; i++) {
417 			printf("Controller ID:                    0x%04x\n",
418 			    e->ctrlr[i].ctrlr_id);
419 			printf("  Reservation Status:             %u\n",
420 			    e->ctrlr[i].rcsts);
421 			printf("  Reservation Key:                0x%08jx\n",
422 			    e->ctrlr[i].rkey);
423 			printf("  Host Identifier:                0x%08jx%08jx\n",
424 			    e->ctrlr[i].hostid[0], e->ctrlr[i].hostid[1]);
425 		}
426 	} else {
427 		n = MIN(n, (sizeof(data) - sizeof(s)) / sizeof(s->ctrlr[0]));
428 		for (i = 0; i < n; i++) {
429 			printf("Controller ID:                    0x%04x\n",
430 			    s->ctrlr[i].ctrlr_id);
431 			printf("  Reservation Status:             %u\n",
432 			    s->ctrlr[i].rcsts);
433 			printf("  Host Identifier:                0x%08jx\n",
434 			    s->ctrlr[i].hostid);
435 			printf("  Reservation Key:                0x%08jx\n",
436 			    s->ctrlr[i].rkey);
437 		}
438 	}
439 	exit(0);
440 }
441 
442 static void
443 resv(const struct cmd *nf __unused, int argc, char *argv[])
444 {
445 
446 	cmd_dispatch(argc, argv, &resv_cmd);
447 }
448