xref: /freebsd/sbin/nvmecontrol/selftest.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Chuck Tuffli
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 #define SELFTEST_CODE_NONE 0xffu
46 #define SELFTEST_CODE_MAX  0xfu
47 
48 static struct options {
49 	const char	*dev;
50 	uint8_t		stc;	/* Self-test Code */
51 } opt = {
52 	.dev = NULL,
53 	.stc = SELFTEST_CODE_NONE,
54 };
55 
56 static void
57 selftest_op(int fd, uint32_t nsid, uint8_t stc)
58 {
59 	struct nvme_pt_command	pt;
60 
61 	memset(&pt, 0, sizeof(pt));
62 	pt.cmd.opc = NVME_OPC_DEVICE_SELF_TEST;
63 	pt.cmd.nsid = htole32(nsid);
64 	pt.cmd.cdw10 = htole32(stc);
65 
66 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
67 		err(EX_IOERR, "self-test request failed");
68 
69 	if (NVME_STATUS_GET_SCT(pt.cpl.status) == NVME_SCT_COMMAND_SPECIFIC &&
70 	    NVME_STATUS_GET_SC(pt.cpl.status) == NVME_SC_SELF_TEST_IN_PROGRESS)
71 		errx(EX_UNAVAILABLE, "device self-test in progress");
72 	else if (nvme_completion_is_error(&pt.cpl))
73 		errx(EX_IOERR, "self-test request returned error");
74 }
75 
76 static void
77 selftest(const struct cmd *f, int argc, char *argv[])
78 {
79 	struct nvme_controller_data	cdata;
80 	int				fd;
81 	char				*path;
82 	uint32_t			nsid;
83 
84 	if (arg_parse(argc, argv, f))
85 		return;
86 
87 	open_dev(opt.dev, &fd, 1, 1);
88 	get_nsid(fd, &path, &nsid);
89 	if (nsid != 0) {
90 		close(fd);
91 		open_dev(path, &fd, 1, 1);
92 	}
93 	free(path);
94 
95 	if (opt.stc == SELFTEST_CODE_NONE)
96 		errx(EX_USAGE, "must specify a Self-test Code");
97 	else if (opt.stc > SELFTEST_CODE_MAX)
98 		errx(EX_DATAERR, "illegal Self-test Code 0x%x", opt.stc);
99 
100 	if (read_controller_data(fd, &cdata))
101 		errx(EX_IOERR, "Identify request failed");
102 
103 	if (((cdata.oacs >> NVME_CTRLR_DATA_OACS_SELFTEST_SHIFT) &
104 	     NVME_CTRLR_DATA_OACS_SELFTEST_MASK) == 0)
105 		errx(EX_UNAVAILABLE, "controller does not support self-test");
106 
107 	selftest_op(fd, nsid, opt.stc);
108 
109 	close(fd);
110 	exit(0);
111 }
112 
113 static const struct opts selftest_opts[] = {
114 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
115 	OPT("test-code", 'c', arg_uint8, opt, stc,
116 	    "Self-test Code to execute"),
117 	{ NULL, 0, arg_none, NULL, NULL }
118 };
119 #undef OPT
120 
121 static struct args selftest_args[] = {
122 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
123 	{ arg_none, NULL, NULL },
124 };
125 
126 static struct cmd selftest_cmd = {
127 	.name = "selftest",
128 	.fn = selftest,
129 	.descr = "Start device self-test",
130 	.ctx_size = sizeof(opt),
131 	.opts = selftest_opts,
132 	.args = selftest_args,
133 };
134 
135 CMD_COMMAND(selftest_cmd);
136