2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/ioccom.h>
44 #include "nvmecontrol.h"
46 /* Tables for command line parsing */
49 static cmd_fn_t resvacquire;
50 static cmd_fn_t resvregister;
51 static cmd_fn_t resvrelease;
52 static cmd_fn_t resvreport;
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 }
59 static struct cmd resv_cmd = {
62 .descr = "Reservation commands",
68 CMD_COMMAND(resv_cmd);
70 static struct acquire_options {
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,
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 }
96 static const struct args acquire_args[] = {
97 { arg_string, &acquire_opt.dev, "namespace-id" },
98 { arg_none, NULL, NULL },
101 static struct cmd acquire_cmd = {
104 .descr = "Acquire/preempt reservation",
105 .ctx_size = sizeof(acquire_opt),
106 .opts = acquire_opts,
107 .args = acquire_args,
110 CMD_SUBCOMMAND(resv_cmd, acquire_cmd);
112 static struct register_options {
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 }
142 static const struct args register_args[] = {
143 { arg_string, ®ister_opt.dev, "namespace-id" },
144 { arg_none, NULL, NULL },
147 static struct cmd register_cmd = {
150 .descr = "Register/unregister reservation",
151 .ctx_size = sizeof(register_opt),
152 .opts = register_opts,
153 .args = register_args,
156 CMD_SUBCOMMAND(resv_cmd, register_cmd);
158 static struct release_options {
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,
175 OPT("rrela", 'a', arg_uint8, release_opt, rrela,
176 "Release Action (0=release, 1=clear)"),
177 { NULL, 0, arg_none, NULL, NULL }
180 static const struct args release_args[] = {
181 { arg_string, &release_opt.dev, "namespace-id" },
182 { arg_none, NULL, NULL },
185 static struct cmd release_cmd = {
188 .descr = "Release/clear reservation",
189 .ctx_size = sizeof(release_opt),
190 .opts = release_opts,
191 .args = release_args,
194 CMD_SUBCOMMAND(resv_cmd, release_cmd);
196 static struct report_options {
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,
213 OPT("eds", 'e', arg_none, report_opt, eds,
214 "Extended Data Structure"),
215 { NULL, 0, arg_none, NULL, NULL }
218 static const struct args report_args[] = {
219 { arg_string, &report_opt.dev, "namespace-id" },
220 { arg_none, NULL, NULL },
223 static struct cmd report_cmd = {
226 .descr = "Print reservation status",
227 .ctx_size = sizeof(report_opt),
232 CMD_SUBCOMMAND(resv_cmd, report_cmd);
234 /* handles NVME_OPC_RESERVATION_* NVM commands */
237 resvacquire(const struct cmd *f, int argc, char *argv[])
239 struct nvme_pt_command pt;
244 if (arg_parse(argc, argv, f))
246 open_dev(acquire_opt.dev, &fd, 0, 1);
247 get_nsid(fd, NULL, &nsid);
249 fprintf(stderr, "This command require namespace-id\n");
250 arg_help(argc, argv, f);
253 data[0] = htole64(acquire_opt.crkey);
254 data[1] = htole64(acquire_opt.prkey);
256 memset(&pt, 0, sizeof(pt));
257 pt.cmd.opc = NVME_OPC_RESERVATION_ACQUIRE;
258 pt.cmd.cdw10 = htole32((acquire_opt.racqa & 7) |
259 (acquire_opt.rtype << 8));
261 pt.len = sizeof(data);
264 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
265 err(EX_IOERR, "acquire request failed");
267 if (nvme_completion_is_error(&pt.cpl))
268 errx(EX_IOERR, "acquire request returned error");
275 resvregister(const struct cmd *f, int argc, char *argv[])
277 struct nvme_pt_command pt;
282 if (arg_parse(argc, argv, f))
284 open_dev(register_opt.dev, &fd, 0, 1);
285 get_nsid(fd, NULL, &nsid);
287 fprintf(stderr, "This command require namespace-id\n");
288 arg_help(argc, argv, f);
291 data[0] = htole64(register_opt.crkey);
292 data[1] = htole64(register_opt.nrkey);
294 memset(&pt, 0, sizeof(pt));
295 pt.cmd.opc = NVME_OPC_RESERVATION_REGISTER;
296 pt.cmd.cdw10 = htole32((register_opt.rrega & 7) |
297 (register_opt.iekey << 3) | (register_opt.cptpl << 30));
299 pt.len = sizeof(data);
302 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
303 err(EX_IOERR, "register request failed");
305 if (nvme_completion_is_error(&pt.cpl))
306 errx(EX_IOERR, "register request returned error");
313 resvrelease(const struct cmd *f, int argc, char *argv[])
315 struct nvme_pt_command pt;
320 if (arg_parse(argc, argv, f))
322 open_dev(release_opt.dev, &fd, 0, 1);
323 get_nsid(fd, NULL, &nsid);
325 fprintf(stderr, "This command require namespace-id\n");
326 arg_help(argc, argv, f);
329 data[0] = htole64(release_opt.crkey);
331 memset(&pt, 0, sizeof(pt));
332 pt.cmd.opc = NVME_OPC_RESERVATION_RELEASE;
333 pt.cmd.cdw10 = htole32((release_opt.rrela & 7) |
334 (release_opt.rtype << 8));
336 pt.len = sizeof(data);
339 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
340 err(EX_IOERR, "release request failed");
342 if (nvme_completion_is_error(&pt.cpl))
343 errx(EX_IOERR, "release request returned error");
350 resvreport(const struct cmd *f, int argc, char *argv[])
352 struct nvme_pt_command pt;
353 struct nvme_resv_status *s;
354 struct nvme_resv_status_ext *e;
355 uint8_t data[4096] __aligned(4);
360 if (arg_parse(argc, argv, f))
362 open_dev(report_opt.dev, &fd, 0, 1);
363 get_nsid(fd, NULL, &nsid);
365 fprintf(stderr, "This command require namespace-id\n");
366 arg_help(argc, argv, f);
369 bzero(data, sizeof(data));
370 memset(&pt, 0, sizeof(pt));
371 pt.cmd.opc = NVME_OPC_RESERVATION_REPORT;
372 pt.cmd.cdw10 = htole32(sizeof(data) / 4 - 1);
373 pt.cmd.cdw11 = htole32(report_opt.eds); /* EDS */
375 pt.len = sizeof(data);
378 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
379 err(EX_IOERR, "report request failed");
381 if (nvme_completion_is_error(&pt.cpl))
382 errx(EX_IOERR, "report request returned error");
387 nvme_resv_status_ext_swapbytes((void *)data, sizeof(data));
389 nvme_resv_status_swapbytes((void *)data, sizeof(data));
391 if (report_opt.hex) {
393 if (!report_opt.verbose) {
394 for (; i > 64; i--) {
395 if (data[i - 1] != 0)
403 s = (struct nvme_resv_status *)data;
404 n = (s->regctl[1] << 8) | s->regctl[0];
405 printf("Generation: %u\n", s->gen);
406 printf("Reservation Type: %u\n", s->rtype);
407 printf("Number of Registered Controllers: %u\n", n);
408 printf("Persist Through Power Loss State: %u\n", s->ptpls);
409 if (report_opt.eds) {
410 e = (struct nvme_resv_status_ext *)data;
411 n = MIN(n, (sizeof(data) - sizeof(e)) / sizeof(e->ctrlr[0]));
412 for (i = 0; i < n; i++) {
413 printf("Controller ID: 0x%04x\n",
414 e->ctrlr[i].ctrlr_id);
415 printf(" Reservation Status: %u\n",
417 printf(" Reservation Key: 0x%08jx\n",
419 printf(" Host Identifier: 0x%08jx%08jx\n",
420 e->ctrlr[i].hostid[0], e->ctrlr[i].hostid[1]);
423 n = MIN(n, (sizeof(data) - sizeof(s)) / sizeof(s->ctrlr[0]));
424 for (i = 0; i < n; i++) {
425 printf("Controller ID: 0x%04x\n",
426 s->ctrlr[i].ctrlr_id);
427 printf(" Reservation Status: %u\n",
429 printf(" Host Identifier: 0x%08jx\n",
431 printf(" Reservation Key: 0x%08jx\n",
439 resv(const struct cmd *nf __unused, int argc, char *argv[])
442 cmd_dispatch(argc, argv, &resv_cmd);