2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2013 Intel Corporation
6 * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
45 #include "nvmecontrol.h"
46 #include "nvmecontrol_ext.h"
48 #define NONE 0xfffffffeu
50 static struct options {
63 print_namespace(struct nvme_namespace_data *nsdata)
66 uint32_t lbaf, lbads, ms, rp;
67 uint8_t thin_prov, ptype;
70 thin_prov = (nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT) &
71 NVME_NS_DATA_NSFEAT_THIN_PROV_MASK;
73 flbas_fmt = (nsdata->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
74 NVME_NS_DATA_FLBAS_FORMAT_MASK;
76 printf("Size (in LBAs): %lld (%lldM)\n",
77 (long long)nsdata->nsze,
78 (long long)nsdata->nsze / 1024 / 1024);
79 printf("Capacity (in LBAs): %lld (%lldM)\n",
80 (long long)nsdata->ncap,
81 (long long)nsdata->ncap / 1024 / 1024);
82 printf("Utilization (in LBAs): %lld (%lldM)\n",
83 (long long)nsdata->nuse,
84 (long long)nsdata->nuse / 1024 / 1024);
85 printf("Thin Provisioning: %s\n",
86 thin_prov ? "Supported" : "Not Supported");
87 printf("Number of LBA Formats: %d\n", nsdata->nlbaf+1);
88 printf("Current LBA Format: LBA Format #%02d\n", flbas_fmt);
89 printf("Data Protection Caps: %s%s%s%s%s%s\n",
90 (nsdata->dpc == 0) ? "Not Supported" : "",
91 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_END_SHIFT) &
92 NVME_NS_DATA_DPC_MD_END_MASK) ? "Last Bytes, " : "",
93 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_START_SHIFT) &
94 NVME_NS_DATA_DPC_MD_START_MASK) ? "First Bytes, " : "",
95 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT3_SHIFT) &
96 NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "",
97 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) &
98 NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "",
99 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_MASK) &
100 NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : "");
101 printf("Data Protection Settings: ");
102 ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) &
103 NVME_NS_DATA_DPS_PIT_MASK;
105 printf("Type %d, %s Bytes\n", ptype,
106 ((nsdata->dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) &
107 NVME_NS_DATA_DPS_MD_START_MASK) ? "First" : "Last");
109 printf("Not Enabled\n");
111 printf("Multi-Path I/O Capabilities: %s%s\n",
112 (nsdata->nmic == 0) ? "Not Supported" : "",
113 ((nsdata->nmic >> NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) &
114 NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK) ? "May be shared" : "");
115 printf("Reservation Capabilities: %s%s%s%s%s%s%s%s%s\n",
116 (nsdata->rescap == 0) ? "Not Supported" : "",
117 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_IEKEY13_SHIFT) &
118 NVME_NS_DATA_RESCAP_IEKEY13_MASK) ? "IEKEY13, " : "",
119 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT) &
120 NVME_NS_DATA_RESCAP_EX_AC_AR_MASK) ? "EX_AC_AR, " : "",
121 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT) &
122 NVME_NS_DATA_RESCAP_WR_EX_AR_MASK) ? "WR_EX_AR, " : "",
123 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT) &
124 NVME_NS_DATA_RESCAP_EX_AC_RO_MASK) ? "EX_AC_RO, " : "",
125 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT) &
126 NVME_NS_DATA_RESCAP_WR_EX_RO_MASK) ? "WR_EX_RO, " : "",
127 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_SHIFT) &
128 NVME_NS_DATA_RESCAP_EX_AC_MASK) ? "EX_AC, " : "",
129 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_SHIFT) &
130 NVME_NS_DATA_RESCAP_WR_EX_MASK) ? "WR_EX, " : "",
131 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_PTPL_SHIFT) &
132 NVME_NS_DATA_RESCAP_PTPL_MASK) ? "PTPL" : "");
133 printf("Format Progress Indicator: ");
134 if ((nsdata->fpi >> NVME_NS_DATA_FPI_SUPP_SHIFT) &
135 NVME_NS_DATA_FPI_SUPP_MASK) {
136 printf("%u%% remains\n",
137 (nsdata->fpi >> NVME_NS_DATA_FPI_PERC_SHIFT) &
138 NVME_NS_DATA_FPI_PERC_MASK);
140 printf("Not Supported\n");
141 t = (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_READ_SHIFT) &
142 NVME_NS_DATA_DLFEAT_READ_MASK;
143 printf("Deallocate Logical Block: Read %s%s%s\n",
144 (t == NVME_NS_DATA_DLFEAT_READ_NR) ? "Not Reported" :
145 (t == NVME_NS_DATA_DLFEAT_READ_00) ? "00h" :
146 (t == NVME_NS_DATA_DLFEAT_READ_FF) ? "FFh" : "Unknown",
147 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_DWZ_SHIFT) &
148 NVME_NS_DATA_DLFEAT_DWZ_MASK ? ", Write Zero" : "",
149 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_GCRC_SHIFT) &
150 NVME_NS_DATA_DLFEAT_GCRC_MASK ? ", Guard CRC" : "");
151 printf("Optimal I/O Boundary (LBAs): %u\n", nsdata->noiob);
152 printf("Globally Unique Identifier: ");
153 for (i = 0; i < sizeof(nsdata->nguid); i++)
154 printf("%02x", nsdata->nguid[i]);
156 printf("IEEE EUI64: ");
157 for (i = 0; i < sizeof(nsdata->eui64); i++)
158 printf("%02x", nsdata->eui64[i]);
160 for (i = 0; i <= nsdata->nlbaf; i++) {
161 lbaf = nsdata->lbaf[i];
162 lbads = (lbaf >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
163 NVME_NS_DATA_LBAF_LBADS_MASK;
164 ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) &
165 NVME_NS_DATA_LBAF_MS_MASK;
166 rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) &
167 NVME_NS_DATA_LBAF_RP_MASK;
168 printf("LBA Format #%02d: Data Size: %5d Metadata Size: %5d"
169 " Performance: %s\n",
170 i, 1 << lbads, ms, (rp == 0) ? "Best" :
171 (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded");
176 identify_ctrlr(int fd)
178 struct nvme_controller_data cdata;
181 read_controller_data(fd, &cdata);
186 hexlength = sizeof(struct nvme_controller_data);
188 hexlength = offsetof(struct nvme_controller_data,
190 print_hex(&cdata, hexlength);
194 nvme_print_controller(&cdata);
199 identify_ns(int fd, uint32_t nsid)
201 struct nvme_namespace_data nsdata;
204 read_namespace_data(fd, nsid, &nsdata);
209 hexlength = sizeof(struct nvme_namespace_data);
211 hexlength = offsetof(struct nvme_namespace_data,
213 print_hex(&nsdata, hexlength);
217 print_namespace(&nsdata);
222 identify(const struct cmd *f, int argc, char *argv[])
228 arg_parse(argc, argv, f);
230 open_dev(opt.dev, &fd, 1, 1);
231 get_nsid(fd, &path, &nsid);
234 * We got namespace device, but we need to send IDENTIFY
235 * commands to the controller, not the namespace, since it
236 * is an admin cmd. The namespace ID will be specified in
237 * the IDENTIFY command itself.
240 open_dev(path, &fd, 1, 1);
243 if (opt.nsid != NONE)
249 identify_ns(fd, nsid);
252 static const struct opts identify_opts[] = {
253 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
254 OPT("hex", 'x', arg_none, opt, hex,
255 "Print identiy information in hex"),
256 OPT("verbose", 'v', arg_none, opt, verbose,
257 "More verbosity: print entire identify table"),
258 OPT("nsid", 'n', arg_uint32, opt, nsid,
259 "Namespace ID to use if not in device name"),
260 { NULL, 0, arg_none, NULL, NULL }
264 static const struct args identify_args[] = {
265 { arg_string, &opt.dev, "controller-id|namespace-id" },
266 { arg_none, NULL, NULL },
269 static struct cmd identify_cmd = {
272 .descr = "Print summary of the IDENTIFY information",
273 .ctx_size = sizeof(opt),
274 .opts = identify_opts,
275 .args = identify_args,
278 CMD_COMMAND(identify_cmd);