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