1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/ioccom.h> 33 34 #include <ctype.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <stdbool.h> 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <sysexits.h> 43 #include <unistd.h> 44 45 #include "nvmecontrol.h" 46 47 #define SELFTEST_CODE_NONE 0xffu 48 #define SELFTEST_CODE_MAX 0xfu 49 50 static struct options { 51 const char *dev; 52 uint8_t stc; /* Self-test Code */ 53 } opt = { 54 .dev = NULL, 55 .stc = SELFTEST_CODE_NONE, 56 }; 57 58 static void 59 selftest_op(int fd, uint32_t nsid, uint8_t stc) 60 { 61 struct nvme_pt_command pt; 62 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); 67 68 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 69 err(EX_IOERR, "self-test request failed"); 70 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"); 76 } 77 78 static void 79 selftest(const struct cmd *f, int argc, char *argv[]) 80 { 81 struct nvme_controller_data cdata; 82 int fd; 83 char *path; 84 uint32_t nsid; 85 86 if (arg_parse(argc, argv, f)) 87 return; 88 89 open_dev(opt.dev, &fd, 1, 1); 90 get_nsid(fd, &path, &nsid); 91 if (nsid != 0) { 92 close(fd); 93 open_dev(path, &fd, 1, 1); 94 } 95 free(path); 96 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); 101 102 if (read_controller_data(fd, &cdata)) 103 errx(EX_IOERR, "Identify request failed"); 104 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"); 108 109 selftest_op(fd, nsid, opt.stc); 110 111 close(fd); 112 exit(0); 113 } 114 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 } 120 }; 121 #undef OPT 122 123 static struct args selftest_args[] = { 124 { arg_string, &opt.dev, "controller-id|namespace-id" }, 125 { arg_none, NULL, NULL }, 126 }; 127 128 static struct cmd selftest_cmd = { 129 .name = "selftest", 130 .fn = selftest, 131 .descr = "Start device self-test", 132 .ctx_size = sizeof(opt), 133 .opts = selftest_opts, 134 .args = selftest_args, 135 }; 136 137 CMD_COMMAND(selftest_cmd); 138