1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2018-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 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/param.h>
29#include <sys/ioccom.h>
30
31#include <ctype.h>
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#define NONE 0xffffffffu
45#define SES_NONE 0
46#define SES_USER 1
47#define SES_CRYPTO 2
48
49/* Tables for command line parsing */
50
51static cmd_fn_t format;
52
53static struct options {
54	uint32_t	lbaf;
55	uint32_t	ms;
56	uint32_t	pi;
57	uint32_t	pil;
58	uint32_t	ses;
59	bool		Eflag;
60	bool		Cflag;
61	const char	*dev;
62} opt = {
63	.lbaf = NONE,
64	.ms = NONE,
65	.pi = NONE,
66	.pil = NONE,
67	.ses = SES_NONE,
68	.Eflag = false,
69	.Cflag = false,
70	.dev = NULL,
71};
72
73static const struct opts format_opts[] = {
74#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
75	OPT("crypto", 'C', arg_none, opt, Cflag,
76	    "Crptographic erase"),
77	OPT("erase", 'E', arg_none, opt, Eflag,
78	    "User data erase"),
79	OPT("lbaf", 'f', arg_uint32, opt, lbaf,
80	    "LBA Format to apply to the media"),
81	OPT("ms", 'm', arg_uint32, opt, ms,
82	    "Metadata settings"),
83	OPT("pi", 'p', arg_uint32, opt, pi,
84	    "Protective information"),
85	OPT("pil", 'l', arg_uint32, opt, pil,
86	    "Protective information location"),
87	OPT("ses", 's', arg_uint32, opt, ses,
88	    "Secure erase settings"),
89	{ NULL, 0, arg_none, NULL, NULL }
90};
91#undef OPT
92
93static const struct args format_args[] = {
94	{ arg_string, &opt.dev, "controller-id|namespace-id" },
95	{ arg_none, NULL, NULL },
96};
97
98static struct cmd format_cmd = {
99	.name = "format",
100	.fn = format,
101	.descr = "Format/erase one or all the namespaces",
102	.ctx_size = sizeof(opt),
103	.opts = format_opts,
104	.args = format_args,
105};
106
107CMD_COMMAND(format_cmd);
108
109/* End of tables for command line parsing */
110
111static void
112format(const struct cmd *f, int argc, char *argv[])
113{
114	struct nvme_controller_data	cd;
115	struct nvme_namespace_data	nsd;
116	struct nvme_pt_command		pt;
117	char				*path;
118	const char			*target;
119	uint32_t			nsid;
120	int				lbaf, ms, pi, pil, ses, fd;
121
122	if (arg_parse(argc, argv, f))
123		return;
124
125	if ((int)opt.Eflag + opt.Cflag + (opt.ses != SES_NONE) > 1) {
126		fprintf(stderr,
127		    "Only one of -E, -C or -s may be specified\n");
128		arg_help(argc, argv, f);
129	}
130
131	target = opt.dev;
132	lbaf = opt.lbaf;
133	ms = opt.ms;
134	pi = opt.pi;
135	pil = opt.pil;
136	if (opt.Eflag)
137		ses = SES_USER;
138	else if (opt.Cflag)
139		ses = SES_CRYPTO;
140	else
141		ses = opt.ses;
142
143	open_dev(target, &fd, 1, 1);
144	get_nsid(fd, &path, &nsid);
145	if (nsid == 0) {
146		nsid = NVME_GLOBAL_NAMESPACE_TAG;
147	} else {
148		/*
149		 * We send FORMAT commands to the controller, not the namespace,
150		 * since it is an admin cmd.  The namespace ID will be specified
151		 * in the command itself.  So parse the namespace's device node
152		 * string to get the controller substring and namespace ID.
153		 */
154		close(fd);
155		open_dev(path, &fd, 1, 1);
156	}
157	free(path);
158
159	/* Check that controller can execute this command. */
160	if (read_controller_data(fd, &cd))
161		errx(EX_IOERR, "Identify request failed");
162	if (NVMEV(NVME_CTRLR_DATA_OACS_FORMAT, cd.oacs) == 0)
163		errx(EX_UNAVAILABLE, "controller does not support format");
164	if (NVMEV(NVME_CTRLR_DATA_FNA_CRYPTO_ERASE, cd.fna) == 0 &&
165	    ses == SES_CRYPTO)
166		errx(EX_UNAVAILABLE, "controller does not support cryptographic erase");
167
168	if (nsid != NVME_GLOBAL_NAMESPACE_TAG) {
169		if (NVMEV(NVME_CTRLR_DATA_FNA_FORMAT_ALL, cd.fna) &&
170		    ses == SES_NONE)
171			errx(EX_UNAVAILABLE, "controller does not support per-NS format");
172		if (NVMEV(NVME_CTRLR_DATA_FNA_ERASE_ALL, cd.fna) &&
173		    ses != SES_NONE)
174			errx(EX_UNAVAILABLE, "controller does not support per-NS erase");
175
176		/* Try to keep previous namespace parameters. */
177		if (read_namespace_data(fd, nsid, &nsd))
178			errx(EX_IOERR, "Identify request failed");
179		if (lbaf < 0)
180			lbaf = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsd.flbas);
181		if (lbaf > nsd.nlbaf)
182			errx(EX_USAGE, "LBA format is out of range");
183		if (ms < 0)
184			ms = NVMEV(NVME_NS_DATA_FLBAS_EXTENDED, nsd.flbas);
185		if (pi < 0)
186			pi = NVMEV(NVME_NS_DATA_DPS_MD_START, nsd.dps);
187		if (pil < 0)
188			pil = NVMEV(NVME_NS_DATA_DPS_PIT, nsd.dps);
189	} else {
190
191		/* We have no previous parameters, so default to zeroes. */
192		if (lbaf < 0)
193			lbaf = 0;
194		if (ms < 0)
195			ms = 0;
196		if (pi < 0)
197			pi = 0;
198		if (pil < 0)
199			pil = 0;
200	}
201
202	memset(&pt, 0, sizeof(pt));
203	pt.cmd.opc = NVME_OPC_FORMAT_NVM;
204	pt.cmd.nsid = htole32(nsid);
205	pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) +
206	    (ms << 4) + lbaf);
207
208	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
209		err(EX_IOERR, "format request failed");
210
211	if (nvme_completion_is_error(&pt.cpl))
212		errx(EX_IOERR, "format request returned error");
213	close(fd);
214	exit(0);
215}
216