1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2019 Alexander Motin <[email protected]>
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 /* Tables for command line parsing */
46
47 static cmd_fn_t sanitize;
48
49 static struct options {
50 bool ause;
51 bool ndas;
52 bool oipbp;
53 bool reportonly;
54 uint8_t owpass;
55 uint32_t ovrpat;
56 const char *sanact;
57 const char *dev;
58 } opt = {
59 .ause = false,
60 .ndas = false,
61 .oipbp = false,
62 .reportonly = false,
63 .owpass = 1,
64 .ovrpat = 0,
65 .sanact = NULL,
66 .dev = NULL,
67 };
68
69 static const struct opts sanitize_opts[] = {
70 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
71 OPT("ause", 'U', arg_none, opt, ause,
72 "Allow Unrestricted Sanitize Exit"),
73 OPT("ndas", 'd', arg_none, opt, ndas,
74 "No Deallocate After Sanitize"),
75 OPT("oipbp", 'I', arg_none, opt, oipbp,
76 "Overwrite Invert Pattern Between Passes"),
77 OPT("reportonly", 'r', arg_none, opt, reportonly,
78 "Report previous sanitize status"),
79 OPT("owpass", 'c', arg_uint8, opt, owpass,
80 "Overwrite Pass Count"),
81 OPT("ovrpat", 'p', arg_uint32, opt, ovrpat,
82 "Overwrite Pattern"),
83 OPT("sanact", 'a', arg_string, opt, sanact,
84 "Sanitize Action (block, overwrite, crypto)"),
85 { NULL, 0, arg_none, NULL, NULL }
86 };
87 #undef OPT
88
89 static const struct args sanitize_args[] = {
90 { arg_string, &opt.dev, "controller-id" },
91 { arg_none, NULL, NULL },
92 };
93
94 static struct cmd sanitize_cmd = {
95 .name = "sanitize",
96 .fn = sanitize,
97 .descr = "Sanitize NVM subsystem",
98 .ctx_size = sizeof(opt),
99 .opts = sanitize_opts,
100 .args = sanitize_args,
101 };
102
103 CMD_COMMAND(sanitize_cmd);
104
105 /* End of tables for command line parsing */
106
107 static void
sanitize(const struct cmd * f,int argc,char * argv[])108 sanitize(const struct cmd *f, int argc, char *argv[])
109 {
110 struct nvme_controller_data cd;
111 struct nvme_pt_command pt;
112 struct nvme_sanitize_status_page ss;
113 char *path;
114 uint32_t nsid;
115 int sanact = 0, fd, delay = 1;
116
117 if (arg_parse(argc, argv, f))
118 return;
119
120 if (opt.sanact == NULL) {
121 if (!opt.reportonly) {
122 fprintf(stderr, "Sanitize Action is not specified\n");
123 arg_help(argc, argv, f);
124 }
125 } else {
126 if (strcmp(opt.sanact, "exitfailure") == 0)
127 sanact = 1;
128 else if (strcmp(opt.sanact, "block") == 0)
129 sanact = 2;
130 else if (strcmp(opt.sanact, "overwrite") == 0)
131 sanact = 3;
132 else if (strcmp(opt.sanact, "crypto") == 0)
133 sanact = 4;
134 else {
135 fprintf(stderr, "Incorrect Sanitize Action value\n");
136 arg_help(argc, argv, f);
137 }
138 }
139 if (opt.owpass == 0 || opt.owpass > 16) {
140 fprintf(stderr, "Incorrect Overwrite Pass Count value\n");
141 arg_help(argc, argv, f);
142 }
143
144 open_dev(opt.dev, &fd, 1, 1);
145 get_nsid(fd, &path, &nsid);
146 if (nsid != 0) {
147 close(fd);
148 open_dev(path, &fd, 1, 1);
149 }
150 free(path);
151
152 if (opt.reportonly)
153 goto wait;
154
155 /* Check that controller can execute this command. */
156 if (read_controller_data(fd, &cd))
157 errx(EX_IOERR, "Identify request failed");
158 if (NVMEV(NVME_CTRLR_DATA_SANICAP_BES, cd.sanicap) == 0 && sanact == 2)
159 errx(EX_UNAVAILABLE, "controller does not support Block Erase");
160 if (NVMEV(NVME_CTRLR_DATA_SANICAP_OWS, cd.sanicap) == 0 && sanact == 3)
161 errx(EX_UNAVAILABLE, "controller does not support Overwrite");
162 if (NVMEV(NVME_CTRLR_DATA_SANICAP_CES, cd.sanicap) == 0 && sanact == 4)
163 errx(EX_UNAVAILABLE, "controller does not support Crypto Erase");
164
165 /*
166 * If controller supports only one namespace, we may sanitize it.
167 * If there can be more, make user explicit in his commands.
168 */
169 if (nsid != 0 && cd.nn > 1)
170 errx(EX_UNAVAILABLE, "can't sanitize one of namespaces, specify controller");
171
172 memset(&pt, 0, sizeof(pt));
173 pt.cmd.opc = NVME_OPC_SANITIZE;
174 pt.cmd.cdw10 = htole32((opt.ndas << 9) | (opt.oipbp << 8) |
175 ((opt.owpass & 0xf) << 4) | (opt.ause << 3) | sanact);
176 pt.cmd.cdw11 = htole32(opt.ovrpat);
177
178 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
179 err(EX_IOERR, "sanitize request failed");
180
181 if (nvme_completion_is_error(&pt.cpl))
182 errx(EX_IOERR, "sanitize request returned error");
183
184 wait:
185 read_logpage(fd, NVME_LOG_SANITIZE_STATUS,
186 NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0,
187 0, 0, 0, 0, &ss, sizeof(ss));
188 switch (NVMEV(NVME_SS_PAGE_SSTAT_STATUS, ss.sstat)) {
189 case NVME_SS_PAGE_SSTAT_STATUS_NEVER:
190 printf("Never sanitized");
191 break;
192 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED:
193 printf("Sanitize completed");
194 break;
195 case NVME_SS_PAGE_SSTAT_STATUS_INPROG:
196 printf("Sanitize in progress: %u%% (%u/65535)\r",
197 (ss.sprog * 100 + 32768) / 65536, ss.sprog);
198 fflush(stdout);
199 if (delay < 16)
200 delay++;
201 sleep(delay);
202 goto wait;
203 case NVME_SS_PAGE_SSTAT_STATUS_FAILED:
204 printf("Sanitize failed");
205 break;
206 case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD:
207 printf("Sanitize completed with deallocation");
208 break;
209 default:
210 printf("Sanitize status unknown");
211 break;
212 }
213 if (delay > 1)
214 printf(" ");
215 printf("\n");
216
217 close(fd);
218 exit(0);
219 }
220