1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2012-2013 Intel Corporation 5 * All rights reserved. 6 * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 32 #include <ctype.h> 33 #include <err.h> 34 #include <fcntl.h> 35 #include <stdbool.h> 36 #include <stddef.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <sysexits.h> 41 #include <unistd.h> 42 43 #include "nvmecontrol.h" 44 #include "nvmecontrol_ext.h" 45 46 #define NONE 0xfffffffeu 47 48 static struct options { 49 bool hex; 50 bool verbose; 51 const char *dev; 52 uint32_t nsid; 53 } opt = { 54 .hex = false, 55 .verbose = false, 56 .dev = NULL, 57 .nsid = NONE, 58 }; 59 60 void 61 print_namespace(struct nvme_namespace_data *nsdata) 62 { 63 char cbuf[UINT128_DIG + 1]; 64 uint32_t i; 65 uint32_t lbaf, lbads, ms, rp; 66 uint8_t thin_prov, ptype; 67 uint8_t flbas_fmt, t; 68 69 thin_prov = (nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT) & 70 NVME_NS_DATA_NSFEAT_THIN_PROV_MASK; 71 72 flbas_fmt = (nsdata->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) & 73 NVME_NS_DATA_FLBAS_FORMAT_MASK; 74 75 printf("Size: %lld blocks\n", 76 (long long)nsdata->nsze); 77 printf("Capacity: %lld blocks\n", 78 (long long)nsdata->ncap); 79 printf("Utilization: %lld blocks\n", 80 (long long)nsdata->nuse); 81 printf("Thin Provisioning: %s\n", 82 thin_prov ? "Supported" : "Not Supported"); 83 printf("Number of LBA Formats: %d\n", nsdata->nlbaf+1); 84 printf("Current LBA Format: LBA Format #%02d", flbas_fmt); 85 if (nsdata->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_MS_SHIFT & NVME_NS_DATA_LBAF_MS_MASK) 86 printf(" %s metadata\n", nsdata->flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT & 87 NVME_NS_DATA_FLBAS_EXTENDED_MASK ? "Extended" : "Separate"); 88 else 89 printf("\n"); 90 printf("Metadata Capabilities\n"); 91 printf(" Extended: %s\n", 92 nsdata->mc >> NVME_NS_DATA_MC_EXTENDED_SHIFT & NVME_NS_DATA_MC_EXTENDED_MASK ? "Supported" : "Not Supported"); 93 printf(" Separate: %s\n", 94 nsdata->mc >> NVME_NS_DATA_MC_POINTER_SHIFT & NVME_NS_DATA_MC_POINTER_MASK ? "Supported" : "Not Supported"); 95 printf("Data Protection Caps: %s%s%s%s%s%s\n", 96 (nsdata->dpc == 0) ? "Not Supported" : "", 97 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_END_SHIFT) & 98 NVME_NS_DATA_DPC_MD_END_MASK) ? "Last Bytes, " : "", 99 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_START_SHIFT) & 100 NVME_NS_DATA_DPC_MD_START_MASK) ? "First Bytes, " : "", 101 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT3_SHIFT) & 102 NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "", 103 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) & 104 NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "", 105 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT1_SHIFT) & 106 NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : ""); 107 printf("Data Protection Settings: "); 108 ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) & 109 NVME_NS_DATA_DPS_PIT_MASK; 110 if (ptype) { 111 printf("Type %d, %s Bytes\n", ptype, 112 ((nsdata->dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) & 113 NVME_NS_DATA_DPS_MD_START_MASK) ? "First" : "Last"); 114 } else { 115 printf("Not Enabled\n"); 116 } 117 printf("Multi-Path I/O Capabilities: %s%s\n", 118 (nsdata->nmic == 0) ? "Not Supported" : "", 119 ((nsdata->nmic >> NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) & 120 NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK) ? "May be shared" : ""); 121 printf("Reservation Capabilities: %s%s%s%s%s%s%s%s%s\n", 122 (nsdata->rescap == 0) ? "Not Supported" : "", 123 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_IEKEY13_SHIFT) & 124 NVME_NS_DATA_RESCAP_IEKEY13_MASK) ? "IEKEY13, " : "", 125 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT) & 126 NVME_NS_DATA_RESCAP_EX_AC_AR_MASK) ? "EX_AC_AR, " : "", 127 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT) & 128 NVME_NS_DATA_RESCAP_WR_EX_AR_MASK) ? "WR_EX_AR, " : "", 129 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT) & 130 NVME_NS_DATA_RESCAP_EX_AC_RO_MASK) ? "EX_AC_RO, " : "", 131 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT) & 132 NVME_NS_DATA_RESCAP_WR_EX_RO_MASK) ? "WR_EX_RO, " : "", 133 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_SHIFT) & 134 NVME_NS_DATA_RESCAP_EX_AC_MASK) ? "EX_AC, " : "", 135 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_SHIFT) & 136 NVME_NS_DATA_RESCAP_WR_EX_MASK) ? "WR_EX, " : "", 137 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_PTPL_SHIFT) & 138 NVME_NS_DATA_RESCAP_PTPL_MASK) ? "PTPL" : ""); 139 printf("Format Progress Indicator: "); 140 if ((nsdata->fpi >> NVME_NS_DATA_FPI_SUPP_SHIFT) & 141 NVME_NS_DATA_FPI_SUPP_MASK) { 142 printf("%u%% remains\n", 143 (nsdata->fpi >> NVME_NS_DATA_FPI_PERC_SHIFT) & 144 NVME_NS_DATA_FPI_PERC_MASK); 145 } else 146 printf("Not Supported\n"); 147 t = (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_READ_SHIFT) & 148 NVME_NS_DATA_DLFEAT_READ_MASK; 149 printf("Deallocate Logical Block: Read %s%s%s\n", 150 (t == NVME_NS_DATA_DLFEAT_READ_NR) ? "Not Reported" : 151 (t == NVME_NS_DATA_DLFEAT_READ_00) ? "00h" : 152 (t == NVME_NS_DATA_DLFEAT_READ_FF) ? "FFh" : "Unknown", 153 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_DWZ_SHIFT) & 154 NVME_NS_DATA_DLFEAT_DWZ_MASK ? ", Write Zero" : "", 155 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_GCRC_SHIFT) & 156 NVME_NS_DATA_DLFEAT_GCRC_MASK ? ", Guard CRC" : ""); 157 printf("Optimal I/O Boundary: %u blocks\n", nsdata->noiob); 158 printf("NVM Capacity: %s bytes\n", 159 uint128_to_str(to128(nsdata->nvmcap), cbuf, sizeof(cbuf))); 160 if ((nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) & 161 NVME_NS_DATA_NSFEAT_NPVALID_MASK) { 162 printf("Preferred Write Granularity: %u blocks\n", 163 nsdata->npwg + 1); 164 printf("Preferred Write Alignment: %u blocks\n", 165 nsdata->npwa + 1); 166 printf("Preferred Deallocate Granul: %u blocks\n", 167 nsdata->npdg + 1); 168 printf("Preferred Deallocate Align: %u blocks\n", 169 nsdata->npda + 1); 170 printf("Optimal Write Size: %u blocks\n", 171 nsdata->nows + 1); 172 } 173 printf("Globally Unique Identifier: "); 174 for (i = 0; i < sizeof(nsdata->nguid); i++) 175 printf("%02x", nsdata->nguid[i]); 176 printf("\n"); 177 printf("IEEE EUI64: "); 178 for (i = 0; i < sizeof(nsdata->eui64); i++) 179 printf("%02x", nsdata->eui64[i]); 180 printf("\n"); 181 for (i = 0; i <= nsdata->nlbaf; i++) { 182 lbaf = nsdata->lbaf[i]; 183 lbads = (lbaf >> NVME_NS_DATA_LBAF_LBADS_SHIFT) & 184 NVME_NS_DATA_LBAF_LBADS_MASK; 185 if (lbads == 0) 186 continue; 187 ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) & 188 NVME_NS_DATA_LBAF_MS_MASK; 189 rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) & 190 NVME_NS_DATA_LBAF_RP_MASK; 191 printf("LBA Format #%02d: Data Size: %5d Metadata Size: %5d" 192 " Performance: %s\n", 193 i, 1 << lbads, ms, (rp == 0) ? "Best" : 194 (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded"); 195 } 196 } 197 198 static void 199 identify_ctrlr(int fd) 200 { 201 struct nvme_controller_data cdata; 202 int hexlength; 203 204 if (read_controller_data(fd, &cdata)) 205 errx(EX_IOERR, "Identify request failed"); 206 close(fd); 207 208 if (opt.hex) { 209 if (opt.verbose) 210 hexlength = sizeof(struct nvme_controller_data); 211 else 212 hexlength = offsetof(struct nvme_controller_data, 213 reserved8); 214 print_hex(&cdata, hexlength); 215 exit(0); 216 } 217 218 nvme_print_controller(&cdata); 219 exit(0); 220 } 221 222 static void 223 identify_ns(int fd, uint32_t nsid) 224 { 225 struct nvme_namespace_data nsdata; 226 int hexlength; 227 228 if (read_namespace_data(fd, nsid, &nsdata)) 229 errx(EX_IOERR, "Identify request failed"); 230 close(fd); 231 232 if (opt.hex) { 233 if (opt.verbose) 234 hexlength = sizeof(struct nvme_namespace_data); 235 else 236 hexlength = offsetof(struct nvme_namespace_data, 237 reserved6); 238 print_hex(&nsdata, hexlength); 239 exit(0); 240 } 241 242 print_namespace(&nsdata); 243 exit(0); 244 } 245 246 static void 247 identify(const struct cmd *f, int argc, char *argv[]) 248 { 249 char *path; 250 int fd; 251 uint32_t nsid; 252 253 if (arg_parse(argc, argv, f)) 254 return; 255 256 open_dev(opt.dev, &fd, 0, 1); 257 get_nsid(fd, &path, &nsid); 258 if (nsid != 0) { 259 /* 260 * We got namespace device, but we need to send IDENTIFY 261 * commands to the controller, not the namespace, since it 262 * is an admin cmd. The namespace ID will be specified in 263 * the IDENTIFY command itself. 264 */ 265 close(fd); 266 open_dev(path, &fd, 0, 1); 267 } 268 free(path); 269 if (opt.nsid != NONE) 270 nsid = opt.nsid; 271 272 if (nsid == 0) 273 identify_ctrlr(fd); 274 else 275 identify_ns(fd, nsid); 276 } 277 278 static const struct opts identify_opts[] = { 279 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc } 280 OPT("hex", 'x', arg_none, opt, hex, 281 "Print identiy information in hex"), 282 OPT("verbose", 'v', arg_none, opt, verbose, 283 "More verbosity: print entire identify table"), 284 OPT("nsid", 'n', arg_uint32, opt, nsid, 285 "Namespace ID to use if not in device name"), 286 { NULL, 0, arg_none, NULL, NULL } 287 }; 288 #undef OPT 289 290 static const struct args identify_args[] = { 291 { arg_string, &opt.dev, "controller-id|namespace-id" }, 292 { arg_none, NULL, NULL }, 293 }; 294 295 static struct cmd identify_cmd = { 296 .name = "identify", 297 .fn = identify, 298 .descr = "Print summary of the IDENTIFY information", 299 .ctx_size = sizeof(opt), 300 .opts = identify_opts, 301 .args = identify_args, 302 }; 303 304 CMD_COMMAND(identify_cmd); 305