]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/format.c
pfsync: Provide documentation regarding message version
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / format.c
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/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/ioccom.h>
31
32 #include <ctype.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <unistd.h>
42
43 #include "nvmecontrol.h"
44
45 #define NONE 0xffffffffu
46 #define SES_NONE 0
47 #define SES_USER 1
48 #define SES_CRYPTO 2
49
50 /* Tables for command line parsing */
51
52 static cmd_fn_t format;
53
54 static struct options {
55         uint32_t        lbaf;
56         uint32_t        ms;
57         uint32_t        pi;
58         uint32_t        pil;
59         uint32_t        ses;
60         bool            Eflag;
61         bool            Cflag;
62         const char      *dev;
63 } opt = {
64         .lbaf = NONE,
65         .ms = NONE,
66         .pi = NONE,
67         .pil = NONE,
68         .ses = SES_NONE,
69         .Eflag = false,
70         .Cflag = false,
71         .dev = NULL,
72 };
73
74 static const struct opts format_opts[] = {
75 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
76         OPT("crypto", 'C', arg_none, opt, Cflag,
77             "Crptographic erase"),
78         OPT("erase", 'E', arg_none, opt, Eflag,
79             "User data erase"),
80         OPT("lbaf", 'f', arg_uint32, opt, lbaf,
81             "LBA Format to apply to the media"),
82         OPT("ms", 'm', arg_uint32, opt, ms,
83             "Metadata settings"),
84         OPT("pi", 'p', arg_uint32, opt, pi,
85             "Protective information"),
86         OPT("pil", 'l', arg_uint32, opt, pil,
87             "Protective information location"),
88         OPT("ses", 's', arg_uint32, opt, ses,
89             "Secure erase settings"),
90         { NULL, 0, arg_none, NULL, NULL }
91 };
92 #undef OPT
93
94 static const struct args format_args[] = {
95         { arg_string, &opt.dev, "controller-id|namespace-id" },
96         { arg_none, NULL, NULL },
97 };
98
99 static struct cmd format_cmd = {
100         .name = "format",
101         .fn = format,
102         .descr = "Format/erase one or all the namespaces",
103         .ctx_size = sizeof(opt),
104         .opts = format_opts,
105         .args = format_args,
106 };
107
108 CMD_COMMAND(format_cmd);
109
110 /* End of tables for command line parsing */
111
112 static void
113 format(const struct cmd *f, int argc, char *argv[])
114 {
115         struct nvme_controller_data     cd;
116         struct nvme_namespace_data      nsd;
117         struct nvme_pt_command          pt;
118         char                            *path;
119         const char                      *target;
120         uint32_t                        nsid;
121         int                             lbaf, ms, pi, pil, ses, fd;
122
123         if (arg_parse(argc, argv, f))
124                 return;
125
126         if ((int)opt.Eflag + opt.Cflag + (opt.ses != SES_NONE) > 1) {
127                 fprintf(stderr,
128                     "Only one of -E, -C or -s may be specified\n");
129                 arg_help(argc, argv, f);
130         }
131
132         target = opt.dev;
133         lbaf = opt.lbaf;
134         ms = opt.ms;
135         pi = opt.pi;
136         pil = opt.pil;
137         if (opt.Eflag)
138                 ses = SES_USER;
139         else if (opt.Cflag)
140                 ses = SES_CRYPTO;
141         else
142                 ses = opt.ses;
143
144         open_dev(target, &fd, 1, 1);
145         get_nsid(fd, &path, &nsid);
146         if (nsid == 0) {
147                 nsid = NVME_GLOBAL_NAMESPACE_TAG;
148         } else {
149                 /*
150                  * We send FORMAT commands to the controller, not the namespace,
151                  * since it is an admin cmd.  The namespace ID will be specified
152                  * in the command itself.  So parse the namespace's device node
153                  * string to get the controller substring and namespace ID.
154                  */
155                 close(fd);
156                 open_dev(path, &fd, 1, 1);
157         }
158         free(path);
159
160         /* Check that controller can execute this command. */
161         if (read_controller_data(fd, &cd))
162                 errx(EX_IOERR, "Identify request failed");
163         if (((cd.oacs >> NVME_CTRLR_DATA_OACS_FORMAT_SHIFT) &
164             NVME_CTRLR_DATA_OACS_FORMAT_MASK) == 0)
165                 errx(EX_UNAVAILABLE, "controller does not support format");
166         if (((cd.fna >> NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT) &
167             NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK) == 0 && ses == SES_CRYPTO)
168                 errx(EX_UNAVAILABLE, "controller does not support cryptographic erase");
169
170         if (nsid != NVME_GLOBAL_NAMESPACE_TAG) {
171                 if (((cd.fna >> NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT) &
172                     NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK) && ses == SES_NONE)
173                         errx(EX_UNAVAILABLE, "controller does not support per-NS format");
174                 if (((cd.fna >> NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT) &
175                     NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK) && ses != SES_NONE)
176                         errx(EX_UNAVAILABLE, "controller does not support per-NS erase");
177
178                 /* Try to keep previous namespace parameters. */
179                 if (read_namespace_data(fd, nsid, &nsd))
180                         errx(EX_IOERR, "Identify request failed");
181                 if (lbaf < 0)
182                         lbaf = (nsd.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT)
183                             & NVME_NS_DATA_FLBAS_FORMAT_MASK;
184                 if (lbaf > nsd.nlbaf)
185                         errx(EX_USAGE, "LBA format is out of range");
186                 if (ms < 0)
187                         ms = (nsd.flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT)
188                             & NVME_NS_DATA_FLBAS_EXTENDED_MASK;
189                 if (pi < 0)
190                         pi = (nsd.dps >> NVME_NS_DATA_DPS_MD_START_SHIFT)
191                             & NVME_NS_DATA_DPS_MD_START_MASK;
192                 if (pil < 0)
193                         pil = (nsd.dps >> NVME_NS_DATA_DPS_PIT_SHIFT)
194                             & NVME_NS_DATA_DPS_PIT_MASK;
195         } else {
196
197                 /* We have no previous parameters, so default to zeroes. */
198                 if (lbaf < 0)
199                         lbaf = 0;
200                 if (ms < 0)
201                         ms = 0;
202                 if (pi < 0)
203                         pi = 0;
204                 if (pil < 0)
205                         pil = 0;
206         }
207
208         memset(&pt, 0, sizeof(pt));
209         pt.cmd.opc = NVME_OPC_FORMAT_NVM;
210         pt.cmd.nsid = htole32(nsid);
211         pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) +
212             (ms << 4) + lbaf);
213
214         if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
215                 err(EX_IOERR, "format request failed");
216
217         if (nvme_completion_is_error(&pt.cpl))
218                 errx(EX_IOERR, "format request returned error");
219         close(fd);
220         exit(0);
221 }