summaryrefslogtreecommitdiffstats
path: root/freebsd/sbin/nvmecontrol/resv.c
blob: 5f615941a119973e914773a7aa95c615ebdfca88 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <machine/rtems-bsd-user-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org>
 *
 * 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,
 *    without modification, immediately at the beginning of the file.
 * 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 ``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 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 <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nvmecontrol.h"

/* Tables for command line parsing */

static cmd_fn_t resv;
static cmd_fn_t resvacquire;
static cmd_fn_t resvregister;
static cmd_fn_t resvrelease;
static cmd_fn_t resvreport;

#define NONE 0xffffffffu
#define NONE64 0xffffffffffffffffull
#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
#define OPT_END	{ NULL, 0, arg_none, NULL, NULL }

static struct cmd resv_cmd = {
	.name = "resv",
	.fn = resv,
	.descr = "Reservation commands",
	.ctx_size = 0,
	.opts = NULL,
	.args = NULL,
};

CMD_COMMAND(resv_cmd);

static struct acquire_options {
	uint64_t	crkey;
	uint64_t	prkey;
	uint8_t		rtype;
	uint8_t		racqa;
	const char	*dev;
} acquire_opt = {
	.crkey = 0,
	.prkey = 0,
	.rtype = 0,
	.racqa = 0,
	.dev = NULL,
};

static const struct opts acquire_opts[] = {
	OPT("crkey", 'c', arg_uint64, acquire_opt, crkey,
	    "Current Reservation Key"),
	OPT("prkey", 'p', arg_uint64, acquire_opt, prkey,
	    "Preempt Reservation Key"),
	OPT("rtype", 't', arg_uint8, acquire_opt, rtype,
	    "Reservation Type"),
	OPT("racqa", 'a', arg_uint8, acquire_opt, racqa,
	    "Acquire Action (0=acq, 1=pre, 2=pre+ab)"),
	{ NULL, 0, arg_none, NULL, NULL }
};

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

static struct cmd acquire_cmd = {
	.name = "acquire",
	.fn = resvacquire,
	.descr = "Acquire/preempt reservation",
	.ctx_size = sizeof(acquire_opt),
	.opts = acquire_opts,
	.args = acquire_args,
};

CMD_SUBCOMMAND(resv_cmd, acquire_cmd);

static struct register_options {
	uint64_t	crkey;
	uint64_t	nrkey;
	uint8_t		rrega;
	bool		iekey;
	uint8_t		cptpl;
	const char	*dev;
} register_opt = {
	.crkey = 0,
	.nrkey = 0,
	.rrega = 0,
	.iekey = false,
	.cptpl = 0,
	.dev = NULL,
};

static const struct opts register_opts[] = {
	OPT("crkey", 'c', arg_uint64, register_opt, crkey,
	    "Current Reservation Key"),
	OPT("nrkey", 'k', arg_uint64, register_opt, nrkey,
	    "New Reservation Key"),
	OPT("rrega", 'r', arg_uint8, register_opt, rrega,
	    "Register Action (0=reg, 1=unreg, 2=replace)"),
	OPT("iekey", 'i', arg_none, register_opt, iekey,
	    "Ignore Existing Key"),
	OPT("cptpl", 'p', arg_uint8, register_opt, cptpl,
	    "Change Persist Through Power Loss State"),
	{ NULL, 0, arg_none, NULL, NULL }
};

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

static struct cmd register_cmd = {
	.name = "register",
	.fn = resvregister,
	.descr = "Register/unregister reservation",
	.ctx_size = sizeof(register_opt),
	.opts = register_opts,
	.args = register_args,
};

CMD_SUBCOMMAND(resv_cmd, register_cmd);

static struct release_options {
	uint64_t	crkey;
	uint8_t		rtype;
	uint8_t		rrela;
	const char	*dev;
} release_opt = {
	.crkey = 0,
	.rtype = 0,
	.rrela = 0,
	.dev = NULL,
};

static const struct opts release_opts[] = {
	OPT("crkey", 'c', arg_uint64, release_opt, crkey,
	    "Current Reservation Key"),
	OPT("rtype", 't', arg_uint8, release_opt, rtype,
	    "Reservation Type"),
	OPT("rrela", 'a', arg_uint8, release_opt, rrela,
	    "Release Action (0=release, 1=clear)"),
	{ NULL, 0, arg_none, NULL, NULL }
};

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

static struct cmd release_cmd = {
	.name = "release",
	.fn = resvrelease,
	.descr = "Release/clear reservation",
	.ctx_size = sizeof(release_opt),
	.opts = release_opts,
	.args = release_args,
};

CMD_SUBCOMMAND(resv_cmd, release_cmd);

static struct report_options {
	bool		hex;
	bool		verbose;
	bool		eds;
	const char	*dev;
} report_opt = {
	.hex = false,
	.verbose = false,
	.eds = false,
	.dev = NULL,
};

static const struct opts report_opts[] = {
	OPT("hex", 'x', arg_none, report_opt, hex,
	    "Print reservation status in hex"),
	OPT("verbose", 'v', arg_none, report_opt, verbose,
	    "More verbosity"),
	OPT("eds", 'e', arg_none, report_opt, eds,
	    "Extended Data Structure"),
	{ NULL, 0, arg_none, NULL, NULL }
};

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

static struct cmd report_cmd = {
	.name = "report",
	.fn = resvreport,
	.descr = "Print reservation status",
	.ctx_size = sizeof(report_opt),
	.opts = report_opts,
	.args = report_args,
};

CMD_SUBCOMMAND(resv_cmd, report_cmd);

/* handles NVME_OPC_RESERVATION_* NVM commands */

static void
resvacquire(const struct cmd *f, int argc, char *argv[])
{
	struct nvme_pt_command	pt;
	uint64_t	data[2];
	int		fd;
	uint32_t	nsid;

	if (arg_parse(argc, argv, f))
		return;
	open_dev(acquire_opt.dev, &fd, 1, 1);
	get_nsid(fd, NULL, &nsid);
	if (nsid == 0) {
		fprintf(stderr, "This command require namespace-id\n");
		arg_help(argc, argv, f);
	}

	data[0] = htole64(acquire_opt.crkey);
	data[1] = htole64(acquire_opt.prkey);

	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = NVME_OPC_RESERVATION_ACQUIRE;
	pt.cmd.cdw10 = htole32((acquire_opt.racqa & 7) |
	    (acquire_opt.rtype << 8));
	pt.buf = &data;
	pt.len = sizeof(data);
	pt.is_read = 0;

	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "acquire request failed");

	if (nvme_completion_is_error(&pt.cpl))
		errx(1, "acquire request returned error");

	close(fd);
	exit(0);
}

static void
resvregister(const struct cmd *f, int argc, char *argv[])
{
	struct nvme_pt_command	pt;
	uint64_t	data[2];
	int		fd;
	uint32_t	nsid;

	if (arg_parse(argc, argv, f))
		return;
	open_dev(register_opt.dev, &fd, 1, 1);
	get_nsid(fd, NULL, &nsid);
	if (nsid == 0) {
		fprintf(stderr, "This command require namespace-id\n");
		arg_help(argc, argv, f);
	}

	data[0] = htole64(register_opt.crkey);
	data[1] = htole64(register_opt.nrkey);

	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = NVME_OPC_RESERVATION_REGISTER;
	pt.cmd.cdw10 = htole32((register_opt.rrega & 7) |
	    (register_opt.iekey << 3) | (register_opt.cptpl << 30));
	pt.buf = &data;
	pt.len = sizeof(data);
	pt.is_read = 0;

	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "register request failed");

	if (nvme_completion_is_error(&pt.cpl))
		errx(1, "register request returned error");

	close(fd);
	exit(0);
}

static void
resvrelease(const struct cmd *f, int argc, char *argv[])
{
	struct nvme_pt_command	pt;
	uint64_t	data[1];
	int		fd;
	uint32_t	nsid;

	if (arg_parse(argc, argv, f))
		return;
	open_dev(release_opt.dev, &fd, 1, 1);
	get_nsid(fd, NULL, &nsid);
	if (nsid == 0) {
		fprintf(stderr, "This command require namespace-id\n");
		arg_help(argc, argv, f);
	}

	data[0] = htole64(release_opt.crkey);

	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = NVME_OPC_RESERVATION_RELEASE;
	pt.cmd.cdw10 = htole32((release_opt.rrela & 7) |
	    (release_opt.rtype << 8));
	pt.buf = &data;
	pt.len = sizeof(data);
	pt.is_read = 0;

	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "release request failed");

	if (nvme_completion_is_error(&pt.cpl))
		errx(1, "release request returned error");

	close(fd);
	exit(0);
}

static void
resvreport(const struct cmd *f, int argc, char *argv[])
{
	struct nvme_pt_command	pt;
	struct nvme_resv_status	*s;
	struct nvme_resv_status_ext *e;
	uint8_t		data[4096] __aligned(4);
	int		fd;
	u_int		i, n;
	uint32_t	nsid;

	if (arg_parse(argc, argv, f))
		return;
	open_dev(report_opt.dev, &fd, 1, 1);
	get_nsid(fd, NULL, &nsid);
	if (nsid == 0) {
		fprintf(stderr, "This command require namespace-id\n");
		arg_help(argc, argv, f);
	}

	bzero(data, sizeof(data));
	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = NVME_OPC_RESERVATION_REPORT;
	pt.cmd.cdw10 = htole32(sizeof(data) / 4 - 1);
	pt.cmd.cdw11 = htole32(report_opt.eds);	/* EDS */
	pt.buf = &data;
	pt.len = sizeof(data);
	pt.is_read = 1;

	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "report request failed");

	if (nvme_completion_is_error(&pt.cpl))
		errx(1, "report request returned error");

	close(fd);

	if (report_opt.eds)
		nvme_resv_status_ext_swapbytes((void *)data, sizeof(data));
	else
		nvme_resv_status_swapbytes((void *)data, sizeof(data));

	if (report_opt.hex) {
		i = sizeof(data);
		if (!report_opt.verbose) {
			for (; i > 64; i--) {
				if (data[i - 1] != 0)
					break;
			}
		}
		print_hex(&data, i);
		exit(0);
	}

	s = (struct nvme_resv_status *)data;
	n = (s->regctl[1] << 8) | s->regctl[0];
	printf("Generation:                       %u\n", s->gen);
	printf("Reservation Type:                 %u\n", s->rtype);
	printf("Number of Registered Controllers: %u\n", n);
	printf("Persist Through Power Loss State: %u\n", s->ptpls);
	if (report_opt.eds) {
		e = (struct nvme_resv_status_ext *)data;
		n = MIN(n, (sizeof(data) - sizeof(e)) / sizeof(e->ctrlr[0]));
		for (i = 0; i < n; i++) {
			printf("Controller ID:                    0x%04x\n",
			    e->ctrlr[i].ctrlr_id);
			printf("  Reservation Status:             %u\n",
			    e->ctrlr[i].rcsts);
			printf("  Reservation Key:                0x%08jx\n",
			    e->ctrlr[i].rkey);
			printf("  Host Identifier:                0x%08jx%08jx\n",
			    e->ctrlr[i].hostid[0], e->ctrlr[i].hostid[1]);
		}
	} else {
		n = MIN(n, (sizeof(data) - sizeof(s)) / sizeof(s->ctrlr[0]));
		for (i = 0; i < n; i++) {
			printf("Controller ID:                    0x%04x\n",
			    s->ctrlr[i].ctrlr_id);
			printf("  Reservation Status:             %u\n",
			    s->ctrlr[i].rcsts);
			printf("  Host Identifier:                0x%08jx\n",
			    s->ctrlr[i].hostid);
			printf("  Reservation Key:                0x%08jx\n",
			    s->ctrlr[i].rkey);
		}
	}
	exit(0);
}

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

	cmd_dispatch(argc, argv, &resv_cmd);
}