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>
46 #include "nvmecontrol.h"
47 #include "nvmecontrol_ext.h"
49 #define NONE 0xfffffffeu
51 static struct options {
64 print_namespace(struct nvme_namespace_data *nsdata)
66 char cbuf[UINT128_DIG + 1];
68 uint32_t lbaf, lbads, ms, rp;
69 uint8_t thin_prov, ptype;
72 thin_prov = (nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT) &
73 NVME_NS_DATA_NSFEAT_THIN_PROV_MASK;
75 flbas_fmt = (nsdata->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
76 NVME_NS_DATA_FLBAS_FORMAT_MASK;
78 printf("Size: %lld blocks\n",
79 (long long)nsdata->nsze);
80 printf("Capacity: %lld blocks\n",
81 (long long)nsdata->ncap);
82 printf("Utilization: %lld blocks\n",
83 (long long)nsdata->nuse);
84 printf("Thin Provisioning: %s\n",
85 thin_prov ? "Supported" : "Not Supported");
86 printf("Number of LBA Formats: %d\n", nsdata->nlbaf+1);
87 printf("Current LBA Format: LBA Format #%02d\n", flbas_fmt);
88 printf("Data Protection Caps: %s%s%s%s%s%s\n",
89 (nsdata->dpc == 0) ? "Not Supported" : "",
90 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_END_SHIFT) &
91 NVME_NS_DATA_DPC_MD_END_MASK) ? "Last Bytes, " : "",
92 ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_START_SHIFT) &
93 NVME_NS_DATA_DPC_MD_START_MASK) ? "First Bytes, " : "",
94 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT3_SHIFT) &
95 NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "",
96 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) &
97 NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "",
98 ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT1_SHIFT) &
99 NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : "");
100 printf("Data Protection Settings: ");
101 ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) &
102 NVME_NS_DATA_DPS_PIT_MASK;
104 printf("Type %d, %s Bytes\n", ptype,
105 ((nsdata->dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) &
106 NVME_NS_DATA_DPS_MD_START_MASK) ? "First" : "Last");
108 printf("Not Enabled\n");
110 printf("Multi-Path I/O Capabilities: %s%s\n",
111 (nsdata->nmic == 0) ? "Not Supported" : "",
112 ((nsdata->nmic >> NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) &
113 NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK) ? "May be shared" : "");
114 printf("Reservation Capabilities: %s%s%s%s%s%s%s%s%s\n",
115 (nsdata->rescap == 0) ? "Not Supported" : "",
116 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_IEKEY13_SHIFT) &
117 NVME_NS_DATA_RESCAP_IEKEY13_MASK) ? "IEKEY13, " : "",
118 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT) &
119 NVME_NS_DATA_RESCAP_EX_AC_AR_MASK) ? "EX_AC_AR, " : "",
120 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT) &
121 NVME_NS_DATA_RESCAP_WR_EX_AR_MASK) ? "WR_EX_AR, " : "",
122 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT) &
123 NVME_NS_DATA_RESCAP_EX_AC_RO_MASK) ? "EX_AC_RO, " : "",
124 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT) &
125 NVME_NS_DATA_RESCAP_WR_EX_RO_MASK) ? "WR_EX_RO, " : "",
126 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_SHIFT) &
127 NVME_NS_DATA_RESCAP_EX_AC_MASK) ? "EX_AC, " : "",
128 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_SHIFT) &
129 NVME_NS_DATA_RESCAP_WR_EX_MASK) ? "WR_EX, " : "",
130 ((nsdata->rescap >> NVME_NS_DATA_RESCAP_PTPL_SHIFT) &
131 NVME_NS_DATA_RESCAP_PTPL_MASK) ? "PTPL" : "");
132 printf("Format Progress Indicator: ");
133 if ((nsdata->fpi >> NVME_NS_DATA_FPI_SUPP_SHIFT) &
134 NVME_NS_DATA_FPI_SUPP_MASK) {
135 printf("%u%% remains\n",
136 (nsdata->fpi >> NVME_NS_DATA_FPI_PERC_SHIFT) &
137 NVME_NS_DATA_FPI_PERC_MASK);
139 printf("Not Supported\n");
140 t = (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_READ_SHIFT) &
141 NVME_NS_DATA_DLFEAT_READ_MASK;
142 printf("Deallocate Logical Block: Read %s%s%s\n",
143 (t == NVME_NS_DATA_DLFEAT_READ_NR) ? "Not Reported" :
144 (t == NVME_NS_DATA_DLFEAT_READ_00) ? "00h" :
145 (t == NVME_NS_DATA_DLFEAT_READ_FF) ? "FFh" : "Unknown",
146 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_DWZ_SHIFT) &
147 NVME_NS_DATA_DLFEAT_DWZ_MASK ? ", Write Zero" : "",
148 (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_GCRC_SHIFT) &
149 NVME_NS_DATA_DLFEAT_GCRC_MASK ? ", Guard CRC" : "");
150 printf("Optimal I/O Boundary: %u blocks\n", nsdata->noiob);
151 printf("NVM Capacity: %s bytes\n",
152 uint128_to_str(to128(nsdata->nvmcap), cbuf, sizeof(cbuf)));
153 if ((nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) &
154 NVME_NS_DATA_NSFEAT_NPVALID_MASK) {
155 printf("Preferred Write Granularity: %u blocks\n",
157 printf("Preferred Write Alignment: %u blocks\n",
159 printf("Preferred Deallocate Granul: %u blocks\n",
161 printf("Preferred Deallocate Align: %u blocks\n",
163 printf("Optimal Write Size: %u blocks\n",
166 printf("Globally Unique Identifier: ");
167 for (i = 0; i < sizeof(nsdata->nguid); i++)
168 printf("%02x", nsdata->nguid[i]);
170 printf("IEEE EUI64: ");
171 for (i = 0; i < sizeof(nsdata->eui64); i++)
172 printf("%02x", nsdata->eui64[i]);
174 for (i = 0; i <= nsdata->nlbaf; i++) {
175 lbaf = nsdata->lbaf[i];
176 lbads = (lbaf >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
177 NVME_NS_DATA_LBAF_LBADS_MASK;
178 ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) &
179 NVME_NS_DATA_LBAF_MS_MASK;
180 rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) &
181 NVME_NS_DATA_LBAF_RP_MASK;
182 printf("LBA Format #%02d: Data Size: %5d Metadata Size: %5d"
183 " Performance: %s\n",
184 i, 1 << lbads, ms, (rp == 0) ? "Best" :
185 (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded");
190 identify_ctrlr(int fd)
192 struct nvme_controller_data cdata;
195 if (read_controller_data(fd, &cdata))
196 errx(EX_IOERR, "Identify request failed");
201 hexlength = sizeof(struct nvme_controller_data);
203 hexlength = offsetof(struct nvme_controller_data,
205 print_hex(&cdata, hexlength);
209 nvme_print_controller(&cdata);
214 identify_ns(int fd, uint32_t nsid)
216 struct nvme_namespace_data nsdata;
219 if (read_namespace_data(fd, nsid, &nsdata))
220 errx(EX_IOERR, "Identify request failed");
225 hexlength = sizeof(struct nvme_namespace_data);
227 hexlength = offsetof(struct nvme_namespace_data,
229 print_hex(&nsdata, hexlength);
233 print_namespace(&nsdata);
238 identify(const struct cmd *f, int argc, char *argv[])
244 if (arg_parse(argc, argv, f))
247 open_dev(opt.dev, &fd, 0, 1);
248 get_nsid(fd, &path, &nsid);
251 * We got namespace device, but we need to send IDENTIFY
252 * commands to the controller, not the namespace, since it
253 * is an admin cmd. The namespace ID will be specified in
254 * the IDENTIFY command itself.
257 open_dev(path, &fd, 0, 1);
260 if (opt.nsid != NONE)
266 identify_ns(fd, nsid);
269 static const struct opts identify_opts[] = {
270 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
271 OPT("hex", 'x', arg_none, opt, hex,
272 "Print identiy information in hex"),
273 OPT("verbose", 'v', arg_none, opt, verbose,
274 "More verbosity: print entire identify table"),
275 OPT("nsid", 'n', arg_uint32, opt, nsid,
276 "Namespace ID to use if not in device name"),
277 { NULL, 0, arg_none, NULL, NULL }
281 static const struct args identify_args[] = {
282 { arg_string, &opt.dev, "controller-id|namespace-id" },
283 { arg_none, NULL, NULL },
286 static struct cmd identify_cmd = {
289 .descr = "Print summary of the IDENTIFY information",
290 .ctx_size = sizeof(opt),
291 .opts = identify_opts,
292 .args = identify_args,
295 CMD_COMMAND(identify_cmd);