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