2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Chuck Tuffli
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/ioccom.h>
45 #include "nvmecontrol.h"
47 #define SELFTEST_CODE_NONE 0xffu
48 #define SELFTEST_CODE_MAX 0xfu
50 static struct options {
52 uint8_t stc; /* Self-test Code */
55 .stc = SELFTEST_CODE_NONE,
59 selftest_op(int fd, uint32_t nsid, uint8_t stc)
61 struct nvme_pt_command pt;
63 memset(&pt, 0, sizeof(pt));
64 pt.cmd.opc = NVME_OPC_DEVICE_SELF_TEST;
65 pt.cmd.nsid = htole32(nsid);
66 pt.cmd.cdw10 = htole32(stc);
68 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
69 err(EX_IOERR, "self-test request failed");
71 if (NVME_STATUS_GET_SCT(pt.cpl.status) == NVME_SCT_COMMAND_SPECIFIC &&
72 NVME_STATUS_GET_SC(pt.cpl.status) == NVME_SC_SELF_TEST_IN_PROGRESS)
73 errx(EX_UNAVAILABLE, "device self-test in progress");
74 else if (nvme_completion_is_error(&pt.cpl))
75 errx(EX_IOERR, "self-test request returned error");
79 selftest(const struct cmd *f, int argc, char *argv[])
81 struct nvme_controller_data cdata;
86 if (arg_parse(argc, argv, f))
89 open_dev(opt.dev, &fd, 1, 1);
90 get_nsid(fd, &path, &nsid);
93 open_dev(path, &fd, 1, 1);
97 if (opt.stc == SELFTEST_CODE_NONE)
98 errx(EX_USAGE, "must specify a Self-test Code");
99 else if (opt.stc > SELFTEST_CODE_MAX)
100 errx(EX_DATAERR, "illegal Self-test Code 0x%x", opt.stc);
102 if (read_controller_data(fd, &cdata))
103 errx(EX_IOERR, "Identify request failed");
105 if (((cdata.oacs >> NVME_CTRLR_DATA_OACS_SELFTEST_SHIFT) &
106 NVME_CTRLR_DATA_OACS_SELFTEST_MASK) == 0)
107 errx(EX_UNAVAILABLE, "controller does not support self-test");
109 selftest_op(fd, nsid, opt.stc);
115 static const struct opts selftest_opts[] = {
116 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
117 OPT("test-code", 'c', arg_uint8, opt, stc,
118 "Self-test Code to execute"),
119 { NULL, 0, arg_none, NULL, NULL }
123 static struct args selftest_args[] = {
124 { arg_string, &opt.dev, "controller-id|namespace-id" },
125 { arg_none, NULL, NULL },
128 static struct cmd selftest_cmd = {
131 .descr = "Start device self-test",
132 .ctx_size = sizeof(opt),
133 .opts = selftest_opts,
134 .args = selftest_args,
137 CMD_COMMAND(selftest_cmd);