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