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