1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2018 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/ioccom.h> 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <fcntl.h> 38 #include <stdbool.h> 39 #include <stddef.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "nvmecontrol.h" 46 47 #define NONE 0xffffffffu 48 #define SES_NONE 0 49 #define SES_USER 1 50 #define SES_CRYPTO 2 51 52 /* Tables for command line parsing */ 53 54 static cmd_fn_t format; 55 56 static struct options { 57 uint32_t lbaf; 58 uint32_t ms; 59 uint32_t pi; 60 uint32_t pil; 61 uint32_t ses; 62 bool Eflag; 63 bool Cflag; 64 const char *dev; 65 } opt = { 66 .lbaf = NONE, 67 .ms = NONE, 68 .pi = NONE, 69 .pil = NONE, 70 .ses = SES_NONE, 71 .Eflag = false, 72 .Cflag = false, 73 .dev = NULL, 74 }; 75 76 static const struct opts format_opts[] = { 77 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 78 OPT("crypto", 'C', arg_none, opt, Cflag, 79 "Crptographically erase user data by forgetting key"), 80 OPT("erase", 'E', arg_none, opt, Eflag, 81 "Erase user data"), 82 OPT("lbaf", 'f', arg_uint32, opt, lbaf, 83 "Set the LBA Format to apply to the media"), 84 OPT("ms", 'm', arg_uint32, opt, ms, 85 "Slot to activate and/or download format to"), 86 OPT("pi", 'p', arg_uint32, opt, pi, 87 "Slot to activate and/or download format to"), 88 OPT("pil", 'l', arg_uint32, opt, pil, 89 "Slot to activate and/or download format to"), 90 OPT("ses", 's', arg_uint32, opt, ses, 91 "Slot to activate and/or download format to"), 92 { NULL, 0, arg_none, NULL, NULL } 93 }; 94 #undef OPT 95 96 static const struct args format_args[] = { 97 { arg_string, &opt.dev, "controller-id|namespace-id" }, 98 { arg_none, NULL, NULL }, 99 }; 100 101 static struct cmd format_cmd = { 102 .name = "format", 103 .fn = format, 104 .descr = "Format/erase one or all the namespaces.", 105 .ctx_size = sizeof(opt), 106 .opts = format_opts, 107 .args = format_args, 108 }; 109 110 CMD_COMMAND(format_cmd); 111 112 /* End of tables for command line parsing */ 113 114 static void 115 format(const struct cmd *f, int argc, char *argv[]) 116 { 117 struct nvme_controller_data cd; 118 struct nvme_namespace_data nsd; 119 struct nvme_pt_command pt; 120 char path[64]; 121 const char *target; 122 uint32_t nsid; 123 int lbaf, ms, pi, pil, ses, fd; 124 125 if (arg_parse(argc, argv, f)) 126 return; 127 128 if (opt.Eflag || opt.Cflag || opt.ses != SES_NONE) { 129 fprintf(stderr, 130 "Only one of -E, -C or -s may be specified\n"); 131 arg_help(argc, argv, f); 132 } 133 134 target = opt.dev; 135 lbaf = opt.lbaf; 136 ms = opt.ms; 137 pi = opt.pi; 138 pil = opt.pil; 139 if (opt.Eflag) 140 ses = SES_USER; 141 else if (opt.Cflag) 142 ses = SES_CRYPTO; 143 else 144 ses = opt.ses; 145 146 /* 147 * Check if the specified device node exists before continuing. 148 * This is a cleaner check for cases where the correct controller 149 * is specified, but an invalid namespace on that controller. 150 */ 151 open_dev(target, &fd, 1, 1); 152 153 /* 154 * If device node contains "ns", we consider it a namespace, 155 * otherwise, consider it a controller. 156 */ 157 if (strstr(target, NVME_NS_PREFIX) == NULL) { 158 nsid = NVME_GLOBAL_NAMESPACE_TAG; 159 } else { 160 /* 161 * We send FORMAT commands to the controller, not the namespace, 162 * since it is an admin cmd. The namespace ID will be specified 163 * in the command itself. So parse the namespace's device node 164 * string to get the controller substring and namespace ID. 165 */ 166 close(fd); 167 parse_ns_str(target, path, &nsid); 168 open_dev(path, &fd, 1, 1); 169 } 170 171 /* Check that controller can execute this command. */ 172 read_controller_data(fd, &cd); 173 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_FORMAT_SHIFT) & 174 NVME_CTRLR_DATA_OACS_FORMAT_MASK) == 0) 175 errx(1, "controller does not support format"); 176 if (((cd.fna >> NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT) & 177 NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK) == 0 && ses == SES_CRYPTO) 178 errx(1, "controller does not support cryptographic erase"); 179 180 if (nsid != NVME_GLOBAL_NAMESPACE_TAG) { 181 if (((cd.fna >> NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT) & 182 NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK) && ses == SES_NONE) 183 errx(1, "controller does not support per-NS format"); 184 if (((cd.fna >> NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT) & 185 NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK) && ses != SES_NONE) 186 errx(1, "controller does not support per-NS erase"); 187 188 /* Try to keep previous namespace parameters. */ 189 read_namespace_data(fd, nsid, &nsd); 190 if (lbaf < 0) 191 lbaf = (nsd.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) 192 & NVME_NS_DATA_FLBAS_FORMAT_MASK; 193 if (lbaf > nsd.nlbaf) 194 errx(1, "LBA format is out of range"); 195 if (ms < 0) 196 ms = (nsd.flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT) 197 & NVME_NS_DATA_FLBAS_EXTENDED_MASK; 198 if (pi < 0) 199 pi = (nsd.dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) 200 & NVME_NS_DATA_DPS_MD_START_MASK; 201 if (pil < 0) 202 pil = (nsd.dps >> NVME_NS_DATA_DPS_PIT_SHIFT) 203 & NVME_NS_DATA_DPS_PIT_MASK; 204 } else { 205 206 /* We have no previous parameters, so default to zeroes. */ 207 if (lbaf < 0) 208 lbaf = 0; 209 if (ms < 0) 210 ms = 0; 211 if (pi < 0) 212 pi = 0; 213 if (pil < 0) 214 pil = 0; 215 } 216 217 memset(&pt, 0, sizeof(pt)); 218 pt.cmd.opc = NVME_OPC_FORMAT_NVM; 219 pt.cmd.nsid = htole32(nsid); 220 pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) + 221 (ms << 4) + lbaf); 222 223 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 224 err(1, "format request failed"); 225 226 if (nvme_completion_is_error(&pt.cpl)) 227 errx(1, "format request returned error"); 228 close(fd); 229 exit(0); 230 } 231