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, ®ister_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
resvacquire(const struct cmd * f,int argc,char * argv[])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.cdw10 = htole32((acquire_opt.racqa & 7) |
257 (acquire_opt.rtype << 8));
258 pt.buf = &data;
259 pt.len = sizeof(data);
260 pt.is_read = 0;
261
262 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
263 err(EX_IOERR, "acquire request failed");
264
265 if (nvme_completion_is_error(&pt.cpl))
266 errx(EX_IOERR, "acquire request returned error");
267
268 close(fd);
269 exit(0);
270 }
271
272 static void
resvregister(const struct cmd * f,int argc,char * argv[])273 resvregister(const struct cmd *f, int argc, char *argv[])
274 {
275 struct nvme_pt_command pt;
276 uint64_t data[2];
277 int fd;
278 uint32_t nsid;
279
280 if (arg_parse(argc, argv, f))
281 return;
282 open_dev(register_opt.dev, &fd, 0, 1);
283 get_nsid(fd, NULL, &nsid);
284 if (nsid == 0) {
285 fprintf(stderr, "This command require namespace-id\n");
286 arg_help(argc, argv, f);
287 }
288
289 data[0] = htole64(register_opt.crkey);
290 data[1] = htole64(register_opt.nrkey);
291
292 memset(&pt, 0, sizeof(pt));
293 pt.cmd.opc = NVME_OPC_RESERVATION_REGISTER;
294 pt.cmd.cdw10 = htole32((register_opt.rrega & 7) |
295 (register_opt.iekey << 3) | (register_opt.cptpl << 30));
296 pt.buf = &data;
297 pt.len = sizeof(data);
298 pt.is_read = 0;
299
300 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
301 err(EX_IOERR, "register request failed");
302
303 if (nvme_completion_is_error(&pt.cpl))
304 errx(EX_IOERR, "register request returned error");
305
306 close(fd);
307 exit(0);
308 }
309
310 static void
resvrelease(const struct cmd * f,int argc,char * argv[])311 resvrelease(const struct cmd *f, int argc, char *argv[])
312 {
313 struct nvme_pt_command pt;
314 uint64_t data[1];
315 int fd;
316 uint32_t nsid;
317
318 if (arg_parse(argc, argv, f))
319 return;
320 open_dev(release_opt.dev, &fd, 0, 1);
321 get_nsid(fd, NULL, &nsid);
322 if (nsid == 0) {
323 fprintf(stderr, "This command require namespace-id\n");
324 arg_help(argc, argv, f);
325 }
326
327 data[0] = htole64(release_opt.crkey);
328
329 memset(&pt, 0, sizeof(pt));
330 pt.cmd.opc = NVME_OPC_RESERVATION_RELEASE;
331 pt.cmd.cdw10 = htole32((release_opt.rrela & 7) |
332 (release_opt.rtype << 8));
333 pt.buf = &data;
334 pt.len = sizeof(data);
335 pt.is_read = 0;
336
337 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
338 err(EX_IOERR, "release request failed");
339
340 if (nvme_completion_is_error(&pt.cpl))
341 errx(EX_IOERR, "release request returned error");
342
343 close(fd);
344 exit(0);
345 }
346
347 static void
resvreport(const struct cmd * f,int argc,char * argv[])348 resvreport(const struct cmd *f, int argc, char *argv[])
349 {
350 struct nvme_pt_command pt;
351 struct nvme_resv_status *s;
352 struct nvme_resv_status_ext *e;
353 uint8_t data[4096] __aligned(4);
354 int fd;
355 u_int i, n;
356 uint32_t nsid;
357
358 if (arg_parse(argc, argv, f))
359 return;
360 open_dev(report_opt.dev, &fd, 0, 1);
361 get_nsid(fd, NULL, &nsid);
362 if (nsid == 0) {
363 fprintf(stderr, "This command require namespace-id\n");
364 arg_help(argc, argv, f);
365 }
366
367 bzero(data, sizeof(data));
368 memset(&pt, 0, sizeof(pt));
369 pt.cmd.opc = NVME_OPC_RESERVATION_REPORT;
370 pt.cmd.cdw10 = htole32(sizeof(data) / 4 - 1);
371 pt.cmd.cdw11 = htole32(report_opt.eds); /* EDS */
372 pt.buf = &data;
373 pt.len = sizeof(data);
374 pt.is_read = 1;
375
376 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
377 err(EX_IOERR, "report request failed");
378
379 if (nvme_completion_is_error(&pt.cpl))
380 errx(EX_IOERR, "report request returned error");
381
382 close(fd);
383
384 if (report_opt.eds)
385 nvme_resv_status_ext_swapbytes((void *)data, sizeof(data));
386 else
387 nvme_resv_status_swapbytes((void *)data, sizeof(data));
388
389 if (report_opt.hex) {
390 i = sizeof(data);
391 if (!report_opt.verbose) {
392 for (; i > 64; i--) {
393 if (data[i - 1] != 0)
394 break;
395 }
396 }
397 print_hex(&data, i);
398 exit(0);
399 }
400
401 s = (struct nvme_resv_status *)data;
402 n = (s->regctl[1] << 8) | s->regctl[0];
403 printf("Generation: %u\n", s->gen);
404 printf("Reservation Type: %u\n", s->rtype);
405 printf("Number of Registered Controllers: %u\n", n);
406 printf("Persist Through Power Loss State: %u\n", s->ptpls);
407 if (report_opt.eds) {
408 e = (struct nvme_resv_status_ext *)data;
409 n = MIN(n, (sizeof(data) - sizeof(e)) / sizeof(e->ctrlr[0]));
410 for (i = 0; i < n; i++) {
411 printf("Controller ID: 0x%04x\n",
412 e->ctrlr[i].ctrlr_id);
413 printf(" Reservation Status: %u\n",
414 e->ctrlr[i].rcsts);
415 printf(" Reservation Key: 0x%08jx\n",
416 e->ctrlr[i].rkey);
417 printf(" Host Identifier: 0x%08jx%08jx\n",
418 e->ctrlr[i].hostid[0], e->ctrlr[i].hostid[1]);
419 }
420 } else {
421 n = MIN(n, (sizeof(data) - sizeof(s)) / sizeof(s->ctrlr[0]));
422 for (i = 0; i < n; i++) {
423 printf("Controller ID: 0x%04x\n",
424 s->ctrlr[i].ctrlr_id);
425 printf(" Reservation Status: %u\n",
426 s->ctrlr[i].rcsts);
427 printf(" Host Identifier: 0x%08jx\n",
428 s->ctrlr[i].hostid);
429 printf(" Reservation Key: 0x%08jx\n",
430 s->ctrlr[i].rkey);
431 }
432 }
433 exit(0);
434 }
435
436 static void
resv(const struct cmd * nf __unused,int argc,char * argv[])437 resv(const struct cmd *nf __unused, int argc, char *argv[])
438 {
439
440 cmd_dispatch(argc, argv, &resv_cmd);
441 }
442