xref: /freebsd/sbin/nvmecontrol/sanitize.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 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 /* 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
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 (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_BES_SHIFT) &
159 	     NVME_CTRLR_DATA_SANICAP_BES_MASK) == 0 && sanact == 2)
160 		errx(EX_UNAVAILABLE, "controller does not support Block Erase");
161 	if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_OWS_SHIFT) &
162 	     NVME_CTRLR_DATA_SANICAP_OWS_MASK) == 0 && sanact == 3)
163 		errx(EX_UNAVAILABLE, "controller does not support Overwrite");
164 	if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_CES_SHIFT) &
165 	     NVME_CTRLR_DATA_SANICAP_CES_MASK) == 0 && sanact == 4)
166 		errx(EX_UNAVAILABLE, "controller does not support Crypto Erase");
167 
168 	/*
169 	 * If controller supports only one namespace, we may sanitize it.
170 	 * If there can be more, make user explicit in his commands.
171 	 */
172 	if (nsid != 0 && cd.nn > 1)
173 		errx(EX_UNAVAILABLE, "can't sanitize one of namespaces, specify controller");
174 
175 	memset(&pt, 0, sizeof(pt));
176 	pt.cmd.opc = NVME_OPC_SANITIZE;
177 	pt.cmd.cdw10 = htole32((opt.ndas << 9) | (opt.oipbp << 8) |
178 	    ((opt.owpass & 0xf) << 4) | (opt.ause << 3) | sanact);
179 	pt.cmd.cdw11 = htole32(opt.ovrpat);
180 
181 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
182 		err(EX_IOERR, "sanitize request failed");
183 
184 	if (nvme_completion_is_error(&pt.cpl))
185 		errx(EX_IOERR, "sanitize request returned error");
186 
187 wait:
188 	read_logpage(fd, NVME_LOG_SANITIZE_STATUS,
189 	    NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &ss, sizeof(ss));
190 	switch ((ss.sstat >> NVME_SS_PAGE_SSTAT_STATUS_SHIFT) &
191 	    NVME_SS_PAGE_SSTAT_STATUS_MASK) {
192 	case NVME_SS_PAGE_SSTAT_STATUS_NEVER:
193 		printf("Never sanitized");
194 		break;
195 	case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED:
196 		printf("Sanitize completed");
197 		break;
198 	case NVME_SS_PAGE_SSTAT_STATUS_INPROG:
199 		printf("Sanitize in progress: %u%% (%u/65535)\r",
200 		    (ss.sprog * 100 + 32768) / 65536, ss.sprog);
201 		fflush(stdout);
202 		if (delay < 16)
203 			delay++;
204 		sleep(delay);
205 		goto wait;
206 	case NVME_SS_PAGE_SSTAT_STATUS_FAILED:
207 		printf("Sanitize failed");
208 		break;
209 	case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD:
210 		printf("Sanitize completed with deallocation");
211 		break;
212 	default:
213 		printf("Sanitize status unknown");
214 		break;
215 	}
216 	if (delay > 1)
217 		printf("                       ");
218 	printf("\n");
219 
220 	close(fd);
221 	exit(0);
222 }
223