1635c517aSAlexander Motin /*- 2635c517aSAlexander Motin * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3635c517aSAlexander Motin * 4a7bf63beSAlexander Motin * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org> 5635c517aSAlexander Motin * 6635c517aSAlexander Motin * Redistribution and use in source and binary forms, with or without 7635c517aSAlexander Motin * modification, are permitted provided that the following conditions 8635c517aSAlexander Motin * are met: 9635c517aSAlexander Motin * 1. Redistributions of source code must retain the above copyright 10635c517aSAlexander Motin * notice, this list of conditions and the following disclaimer. 11635c517aSAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 12635c517aSAlexander Motin * notice, this list of conditions and the following disclaimer in the 13635c517aSAlexander Motin * documentation and/or other materials provided with the distribution. 14635c517aSAlexander Motin * 15635c517aSAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16635c517aSAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17635c517aSAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18635c517aSAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19635c517aSAlexander Motin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20635c517aSAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21635c517aSAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22635c517aSAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23635c517aSAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24635c517aSAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25635c517aSAlexander Motin * SUCH DAMAGE. 26635c517aSAlexander Motin */ 27635c517aSAlexander Motin 28635c517aSAlexander Motin #include <sys/cdefs.h> 29635c517aSAlexander Motin __FBSDID("$FreeBSD$"); 30635c517aSAlexander Motin 31635c517aSAlexander Motin #include <sys/param.h> 32635c517aSAlexander Motin #include <sys/ioccom.h> 33635c517aSAlexander Motin 34635c517aSAlexander Motin #include <ctype.h> 35635c517aSAlexander Motin #include <err.h> 36635c517aSAlexander Motin #include <fcntl.h> 37f634b4c1SWarner Losh #include <stdbool.h> 38635c517aSAlexander Motin #include <stddef.h> 39635c517aSAlexander Motin #include <stdio.h> 40635c517aSAlexander Motin #include <stdlib.h> 41635c517aSAlexander Motin #include <string.h> 42*5dc463f9SAlexander Motin #include <sysexits.h> 43635c517aSAlexander Motin #include <unistd.h> 44635c517aSAlexander Motin 45635c517aSAlexander Motin #include "nvmecontrol.h" 46635c517aSAlexander Motin 47f634b4c1SWarner Losh #define NONE 0xffffffffu 48f634b4c1SWarner Losh #define SES_NONE 0 49f634b4c1SWarner Losh #define SES_USER 1 50f634b4c1SWarner Losh #define SES_CRYPTO 2 51f634b4c1SWarner Losh 52f634b4c1SWarner Losh /* Tables for command line parsing */ 53f634b4c1SWarner Losh 54f634b4c1SWarner Losh static cmd_fn_t format; 55f634b4c1SWarner Losh 56f634b4c1SWarner Losh static struct options { 57f634b4c1SWarner Losh uint32_t lbaf; 58f634b4c1SWarner Losh uint32_t ms; 59f634b4c1SWarner Losh uint32_t pi; 60f634b4c1SWarner Losh uint32_t pil; 61f634b4c1SWarner Losh uint32_t ses; 62f634b4c1SWarner Losh bool Eflag; 63f634b4c1SWarner Losh bool Cflag; 64f634b4c1SWarner Losh const char *dev; 65f634b4c1SWarner Losh } opt = { 66f634b4c1SWarner Losh .lbaf = NONE, 67f634b4c1SWarner Losh .ms = NONE, 68f634b4c1SWarner Losh .pi = NONE, 69f634b4c1SWarner Losh .pil = NONE, 70f634b4c1SWarner Losh .ses = SES_NONE, 71f634b4c1SWarner Losh .Eflag = false, 72f634b4c1SWarner Losh .Cflag = false, 73f634b4c1SWarner Losh .dev = NULL, 74f634b4c1SWarner Losh }; 75f634b4c1SWarner Losh 76f634b4c1SWarner Losh static const struct opts format_opts[] = { 77f634b4c1SWarner Losh #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 78f634b4c1SWarner Losh OPT("crypto", 'C', arg_none, opt, Cflag, 79e843651bSAlexander Motin "Crptographic erase"), 80f634b4c1SWarner Losh OPT("erase", 'E', arg_none, opt, Eflag, 81e843651bSAlexander Motin "User data erase"), 82f634b4c1SWarner Losh OPT("lbaf", 'f', arg_uint32, opt, lbaf, 83e843651bSAlexander Motin "LBA Format to apply to the media"), 84f634b4c1SWarner Losh OPT("ms", 'm', arg_uint32, opt, ms, 85e843651bSAlexander Motin "Metadata settings"), 86f634b4c1SWarner Losh OPT("pi", 'p', arg_uint32, opt, pi, 87e843651bSAlexander Motin "Protective information"), 88f634b4c1SWarner Losh OPT("pil", 'l', arg_uint32, opt, pil, 89e843651bSAlexander Motin "Protective information location"), 90f634b4c1SWarner Losh OPT("ses", 's', arg_uint32, opt, ses, 91e843651bSAlexander Motin "Secure erase settings"), 92f634b4c1SWarner Losh { NULL, 0, arg_none, NULL, NULL } 93f634b4c1SWarner Losh }; 94f634b4c1SWarner Losh #undef OPT 95f634b4c1SWarner Losh 96f634b4c1SWarner Losh static const struct args format_args[] = { 97f634b4c1SWarner Losh { arg_string, &opt.dev, "controller-id|namespace-id" }, 98f634b4c1SWarner Losh { arg_none, NULL, NULL }, 99f634b4c1SWarner Losh }; 100f634b4c1SWarner Losh 101f634b4c1SWarner Losh static struct cmd format_cmd = { 102f634b4c1SWarner Losh .name = "format", 103f634b4c1SWarner Losh .fn = format, 104e843651bSAlexander Motin .descr = "Format/erase one or all the namespaces", 105f634b4c1SWarner Losh .ctx_size = sizeof(opt), 106f634b4c1SWarner Losh .opts = format_opts, 107f634b4c1SWarner Losh .args = format_args, 108f634b4c1SWarner Losh }; 109f634b4c1SWarner Losh 110f634b4c1SWarner Losh CMD_COMMAND(format_cmd); 111f634b4c1SWarner Losh 112f634b4c1SWarner Losh /* End of tables for command line parsing */ 113a13a291aSWarner Losh 114635c517aSAlexander Motin static void 115f634b4c1SWarner Losh format(const struct cmd *f, int argc, char *argv[]) 116635c517aSAlexander Motin { 117635c517aSAlexander Motin struct nvme_controller_data cd; 118635c517aSAlexander Motin struct nvme_namespace_data nsd; 119635c517aSAlexander Motin struct nvme_pt_command pt; 120a7bf63beSAlexander Motin char *path; 121f634b4c1SWarner Losh const char *target; 122635c517aSAlexander Motin uint32_t nsid; 123f634b4c1SWarner Losh int lbaf, ms, pi, pil, ses, fd; 124635c517aSAlexander Motin 125f634b4c1SWarner Losh if (arg_parse(argc, argv, f)) 126f634b4c1SWarner Losh return; 127635c517aSAlexander Motin 128ea44065cSAlexander Motin if ((int)opt.Eflag + opt.Cflag + (opt.ses != SES_NONE) > 1) { 129f634b4c1SWarner Losh fprintf(stderr, 130f634b4c1SWarner Losh "Only one of -E, -C or -s may be specified\n"); 131f634b4c1SWarner Losh arg_help(argc, argv, f); 132635c517aSAlexander Motin } 133635c517aSAlexander Motin 134f634b4c1SWarner Losh target = opt.dev; 135f634b4c1SWarner Losh lbaf = opt.lbaf; 136f634b4c1SWarner Losh ms = opt.ms; 137f634b4c1SWarner Losh pi = opt.pi; 138f634b4c1SWarner Losh pil = opt.pil; 139f634b4c1SWarner Losh if (opt.Eflag) 140f634b4c1SWarner Losh ses = SES_USER; 141f634b4c1SWarner Losh else if (opt.Cflag) 142f634b4c1SWarner Losh ses = SES_CRYPTO; 143f634b4c1SWarner Losh else 144f634b4c1SWarner Losh ses = opt.ses; 145635c517aSAlexander Motin 146635c517aSAlexander Motin open_dev(target, &fd, 1, 1); 147a7bf63beSAlexander Motin get_nsid(fd, &path, &nsid); 148a7bf63beSAlexander Motin if (nsid == 0) { 149635c517aSAlexander Motin nsid = NVME_GLOBAL_NAMESPACE_TAG; 150635c517aSAlexander Motin } else { 151635c517aSAlexander Motin /* 152635c517aSAlexander Motin * We send FORMAT commands to the controller, not the namespace, 153635c517aSAlexander Motin * since it is an admin cmd. The namespace ID will be specified 154635c517aSAlexander Motin * in the command itself. So parse the namespace's device node 155635c517aSAlexander Motin * string to get the controller substring and namespace ID. 156635c517aSAlexander Motin */ 157635c517aSAlexander Motin close(fd); 158635c517aSAlexander Motin open_dev(path, &fd, 1, 1); 159011bbaa5SAlexander Motin } 160a7bf63beSAlexander Motin free(path); 161635c517aSAlexander Motin 162635c517aSAlexander Motin /* Check that controller can execute this command. */ 163*5dc463f9SAlexander Motin if (read_controller_data(fd, &cd)) 164*5dc463f9SAlexander Motin errx(EX_IOERR, "Identify request failed"); 165011bbaa5SAlexander Motin if (((cd.oacs >> NVME_CTRLR_DATA_OACS_FORMAT_SHIFT) & 166011bbaa5SAlexander Motin NVME_CTRLR_DATA_OACS_FORMAT_MASK) == 0) 167*5dc463f9SAlexander Motin errx(EX_UNAVAILABLE, "controller does not support format"); 168011bbaa5SAlexander Motin if (((cd.fna >> NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT) & 169f634b4c1SWarner Losh NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK) == 0 && ses == SES_CRYPTO) 170*5dc463f9SAlexander Motin errx(EX_UNAVAILABLE, "controller does not support cryptographic erase"); 171011bbaa5SAlexander Motin 172011bbaa5SAlexander Motin if (nsid != NVME_GLOBAL_NAMESPACE_TAG) { 173011bbaa5SAlexander Motin if (((cd.fna >> NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT) & 174f634b4c1SWarner Losh NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK) && ses == SES_NONE) 175*5dc463f9SAlexander Motin errx(EX_UNAVAILABLE, "controller does not support per-NS format"); 176011bbaa5SAlexander Motin if (((cd.fna >> NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT) & 177f634b4c1SWarner Losh NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK) && ses != SES_NONE) 178*5dc463f9SAlexander Motin errx(EX_UNAVAILABLE, "controller does not support per-NS erase"); 179635c517aSAlexander Motin 180635c517aSAlexander Motin /* Try to keep previous namespace parameters. */ 181*5dc463f9SAlexander Motin if (read_namespace_data(fd, nsid, &nsd)) 182*5dc463f9SAlexander Motin errx(EX_IOERR, "Identify request failed"); 183635c517aSAlexander Motin if (lbaf < 0) 184635c517aSAlexander Motin lbaf = (nsd.flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) 185635c517aSAlexander Motin & NVME_NS_DATA_FLBAS_FORMAT_MASK; 186011bbaa5SAlexander Motin if (lbaf > nsd.nlbaf) 187*5dc463f9SAlexander Motin errx(EX_USAGE, "LBA format is out of range"); 188f634b4c1SWarner Losh if (ms < 0) 189f634b4c1SWarner Losh ms = (nsd.flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT) 190635c517aSAlexander Motin & NVME_NS_DATA_FLBAS_EXTENDED_MASK; 191635c517aSAlexander Motin if (pi < 0) 192635c517aSAlexander Motin pi = (nsd.dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) 193635c517aSAlexander Motin & NVME_NS_DATA_DPS_MD_START_MASK; 194635c517aSAlexander Motin if (pil < 0) 195635c517aSAlexander Motin pil = (nsd.dps >> NVME_NS_DATA_DPS_PIT_SHIFT) 196635c517aSAlexander Motin & NVME_NS_DATA_DPS_PIT_MASK; 197011bbaa5SAlexander Motin } else { 198011bbaa5SAlexander Motin 199011bbaa5SAlexander Motin /* We have no previous parameters, so default to zeroes. */ 200011bbaa5SAlexander Motin if (lbaf < 0) 201011bbaa5SAlexander Motin lbaf = 0; 202f634b4c1SWarner Losh if (ms < 0) 203f634b4c1SWarner Losh ms = 0; 204011bbaa5SAlexander Motin if (pi < 0) 205011bbaa5SAlexander Motin pi = 0; 206011bbaa5SAlexander Motin if (pil < 0) 207011bbaa5SAlexander Motin pil = 0; 208635c517aSAlexander Motin } 209635c517aSAlexander Motin 210635c517aSAlexander Motin memset(&pt, 0, sizeof(pt)); 2119544e6dcSChuck Tuffli pt.cmd.opc = NVME_OPC_FORMAT_NVM; 212635c517aSAlexander Motin pt.cmd.nsid = htole32(nsid); 213635c517aSAlexander Motin pt.cmd.cdw10 = htole32((ses << 9) + (pil << 8) + (pi << 5) + 214f634b4c1SWarner Losh (ms << 4) + lbaf); 215635c517aSAlexander Motin 216635c517aSAlexander Motin if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0) 217*5dc463f9SAlexander Motin err(EX_IOERR, "format request failed"); 218635c517aSAlexander Motin 219635c517aSAlexander Motin if (nvme_completion_is_error(&pt.cpl)) 220*5dc463f9SAlexander Motin errx(EX_IOERR, "format request returned error"); 221635c517aSAlexander Motin close(fd); 222635c517aSAlexander Motin exit(0); 223635c517aSAlexander Motin } 224