summaryrefslogtreecommitdiffstats
path: root/freebsd/sbin/nvmecontrol/sanitize.c
blob: cc8e2417b2ffa1baedfa4c46f899d49360d0c423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <machine/rtems-bsd-user-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/ioccom.h>

#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nvmecontrol.h"

/* Tables for command line parsing */

static cmd_fn_t sanitize;

static struct options {
	bool		ause;
	bool		ndas;
	bool		oipbp;
	bool		reportonly;
	uint8_t		owpass;
	uint32_t	ovrpat;
	const char	*sanact;
	const char	*dev;
} opt = {
	.ause = false,
	.ndas = false,
	.oipbp = false,
	.reportonly = false,
	.owpass = 1,
	.ovrpat = 0,
	.sanact = NULL,
	.dev = NULL,
};

static const struct opts sanitize_opts[] = {
#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
	OPT("ause", 'U', arg_none, opt, ause,
	    "Allow Unrestricted Sanitize Exit"),
	OPT("ndas", 'd', arg_none, opt, ndas,
	    "No Deallocate After Sanitize"),
	OPT("oipbp", 'I', arg_none, opt, oipbp,
	    "Overwrite Invert Pattern Between Passes"),
	OPT("reportonly", 'r', arg_none, opt, reportonly,
	    "Report previous sanitize status"),
	OPT("owpass", 'c', arg_uint8, opt, owpass,
	    "Overwrite Pass Count"),
	OPT("ovrpat", 'p', arg_uint32, opt, ovrpat,
	    "Overwrite Pattern"),
	OPT("sanact", 'a', arg_string, opt, sanact,
	    "Sanitize Action (block, overwrite, crypto)"),
	{ NULL, 0, arg_none, NULL, NULL }
};
#undef OPT

static const struct args sanitize_args[] = {
	{ arg_string, &opt.dev, "controller-id" },
	{ arg_none, NULL, NULL },
};

static struct cmd sanitize_cmd = {
	.name = "sanitize",
	.fn = sanitize,
	.descr = "Sanitize NVM subsystem",
	.ctx_size = sizeof(opt),
	.opts = sanitize_opts,
	.args = sanitize_args,
};

CMD_COMMAND(sanitize_cmd);

/* End of tables for command line parsing */

static void
sanitize(const struct cmd *f, int argc, char *argv[])
{
	struct nvme_controller_data	cd;
	struct nvme_pt_command		pt;
	struct nvme_sanitize_status_page ss;
	char				*path;
	uint32_t			nsid;
	int				sanact = 0, fd, delay = 1;

	if (arg_parse(argc, argv, f))
		return;

	if (opt.sanact == NULL) {
		if (!opt.reportonly) {
			fprintf(stderr, "Sanitize Action is not specified\n");
			arg_help(argc, argv, f);
		}
	} else {
		if (strcmp(opt.sanact, "exitfailure") == 0)
			sanact = 1;
		else if (strcmp(opt.sanact, "block") == 0)
			sanact = 2;
		else if (strcmp(opt.sanact, "overwrite") == 0)
			sanact = 3;
		else if (strcmp(opt.sanact, "crypto") == 0)
			sanact = 4;
		else {
			fprintf(stderr, "Incorrect Sanitize Action value\n");
			arg_help(argc, argv, f);
		}
	}
	if (opt.owpass == 0 || opt.owpass > 16) {
		fprintf(stderr, "Incorrect Overwrite Pass Count value\n");
		arg_help(argc, argv, f);
	}

	open_dev(opt.dev, &fd, 1, 1);
	get_nsid(fd, &path, &nsid);
	if (nsid != 0) {
		close(fd);
		open_dev(path, &fd, 1, 1);
	}
	free(path);

	if (opt.reportonly)
		goto wait;

	/* Check that controller can execute this command. */
	read_controller_data(fd, &cd);
	if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_BES_SHIFT) &
	     NVME_CTRLR_DATA_SANICAP_BES_MASK) == 0 && sanact == 2)
		errx(1, "controller does not support Block Erase");
	if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_OWS_SHIFT) &
	     NVME_CTRLR_DATA_SANICAP_OWS_MASK) == 0 && sanact == 3)
		errx(1, "controller does not support Overwrite");
	if (((cd.sanicap >> NVME_CTRLR_DATA_SANICAP_CES_SHIFT) &
	     NVME_CTRLR_DATA_SANICAP_CES_MASK) == 0 && sanact == 4)
		errx(1, "controller does not support Crypto Erase");

	/*
	 * If controller supports only one namespace, we may sanitize it.
	 * If there can be more, make user explicit in his commands.
	 */
	if (nsid != 0 && cd.nn > 1)
		errx(1, "can't sanitize one of namespaces, specify controller");

	memset(&pt, 0, sizeof(pt));
	pt.cmd.opc = NVME_OPC_SANITIZE;
	pt.cmd.cdw10 = htole32((opt.ndas << 9) | (opt.oipbp << 8) |
	    ((opt.owpass & 0xf) << 4) | (opt.ause << 3) | sanact);
	pt.cmd.cdw11 = htole32(opt.ovrpat);

	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
		err(1, "sanitize request failed");

	if (nvme_completion_is_error(&pt.cpl))
		errx(1, "sanitize request returned error");

wait:
	read_logpage(fd, NVME_LOG_SANITIZE_STATUS,
	    NVME_GLOBAL_NAMESPACE_TAG, 0, 0, 0, &ss, sizeof(ss));
	switch ((ss.sstat >> NVME_SS_PAGE_SSTAT_STATUS_SHIFT) &
	    NVME_SS_PAGE_SSTAT_STATUS_MASK) {
	case NVME_SS_PAGE_SSTAT_STATUS_NEVER:
		printf("Never sanitized");
		break;
	case NVME_SS_PAGE_SSTAT_STATUS_COMPLETED:
		printf("Sanitize completed");
		break;
	case NVME_SS_PAGE_SSTAT_STATUS_INPROG:
		printf("Sanitize in progress: %u%% (%u/65535)\r",
		    (ss.sprog * 100 + 32768) / 65536, ss.sprog);
		fflush(stdout);
		if (delay < 16)
			delay++;
		sleep(delay);
		goto wait;
	case NVME_SS_PAGE_SSTAT_STATUS_FAILED:
		printf("Sanitize failed");
		break;
	case NVME_SS_PAGE_SSTAT_STATUS_COMPLETEDWD:
		printf("Sanitize completed with deallocation");
		break;
	default:
		printf("Sanitize status unknown");
		break;
	}
	if (delay > 1)
		printf("                       ");
	printf("\n");

	close(fd);
	exit(0);
}