xref: /freebsd/sbin/nvmecontrol/identify.c (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.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 <sysexits.h>
44 #include <unistd.h>
45 
46 #include "nvmecontrol.h"
47 #include "nvmecontrol_ext.h"
48 
49 #define NONE 0xfffffffeu
50 
51 static struct options {
52 	bool		hex;
53 	bool		verbose;
54 	const char	*dev;
55 	uint32_t	nsid;
56 } opt = {
57 	.hex = false,
58 	.verbose = false,
59 	.dev = NULL,
60 	.nsid = NONE,
61 };
62 
63 void
64 print_namespace(struct nvme_namespace_data *nsdata)
65 {
66 	char cbuf[UINT128_DIG + 1];
67 	uint32_t	i;
68 	uint32_t	lbaf, lbads, ms, rp;
69 	uint8_t		thin_prov, ptype;
70 	uint8_t		flbas_fmt, t;
71 
72 	thin_prov = (nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT) &
73 		NVME_NS_DATA_NSFEAT_THIN_PROV_MASK;
74 
75 	flbas_fmt = (nsdata->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
76 		NVME_NS_DATA_FLBAS_FORMAT_MASK;
77 
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", flbas_fmt);
88 	if (nsdata->lbaf[flbas_fmt] >> NVME_NS_DATA_LBAF_MS_SHIFT & NVME_NS_DATA_LBAF_MS_MASK)
89 		printf(" %s metadata\n", nsdata->flbas >> NVME_NS_DATA_FLBAS_EXTENDED_SHIFT &
90 		    NVME_NS_DATA_FLBAS_EXTENDED_MASK ? "Extended" : "Separate");
91 	else
92 		printf("\n");
93 	printf("Metadata Capabilities\n");
94 	printf("  Extended:                  %s\n",
95 		nsdata->mc >> NVME_NS_DATA_MC_EXTENDED_SHIFT & NVME_NS_DATA_MC_EXTENDED_MASK ? "Supported" : "Not Supported");
96 	printf("  Separate:                  %s\n",
97 		nsdata->mc >> NVME_NS_DATA_MC_POINTER_SHIFT & NVME_NS_DATA_MC_POINTER_MASK ? "Supported" : "Not Supported");
98 	printf("Data Protection Caps:        %s%s%s%s%s%s\n",
99 	    (nsdata->dpc == 0) ? "Not Supported" : "",
100 	    ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_END_SHIFT) &
101 	     NVME_NS_DATA_DPC_MD_END_MASK) ? "Last Bytes, " : "",
102 	    ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_START_SHIFT) &
103 	     NVME_NS_DATA_DPC_MD_START_MASK) ? "First Bytes, " : "",
104 	    ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT3_SHIFT) &
105 	     NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "",
106 	    ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) &
107 	     NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "",
108 	    ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT1_SHIFT) &
109 	     NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : "");
110 	printf("Data Protection Settings:    ");
111 	ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) &
112 	    NVME_NS_DATA_DPS_PIT_MASK;
113 	if (ptype) {
114 		printf("Type %d, %s Bytes\n", ptype,
115 		    ((nsdata->dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) &
116 		     NVME_NS_DATA_DPS_MD_START_MASK) ? "First" : "Last");
117 	} else {
118 		printf("Not Enabled\n");
119 	}
120 	printf("Multi-Path I/O Capabilities: %s%s\n",
121 	    (nsdata->nmic == 0) ? "Not Supported" : "",
122 	    ((nsdata->nmic >> NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) &
123 	     NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK) ? "May be shared" : "");
124 	printf("Reservation Capabilities:    %s%s%s%s%s%s%s%s%s\n",
125 	    (nsdata->rescap == 0) ? "Not Supported" : "",
126 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_IEKEY13_SHIFT) &
127 	     NVME_NS_DATA_RESCAP_IEKEY13_MASK) ? "IEKEY13, " : "",
128 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT) &
129 	     NVME_NS_DATA_RESCAP_EX_AC_AR_MASK) ? "EX_AC_AR, " : "",
130 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT) &
131 	     NVME_NS_DATA_RESCAP_WR_EX_AR_MASK) ? "WR_EX_AR, " : "",
132 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT) &
133 	     NVME_NS_DATA_RESCAP_EX_AC_RO_MASK) ? "EX_AC_RO, " : "",
134 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT) &
135 	     NVME_NS_DATA_RESCAP_WR_EX_RO_MASK) ? "WR_EX_RO, " : "",
136 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_SHIFT) &
137 	     NVME_NS_DATA_RESCAP_EX_AC_MASK) ? "EX_AC, " : "",
138 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_SHIFT) &
139 	     NVME_NS_DATA_RESCAP_WR_EX_MASK) ? "WR_EX, " : "",
140 	    ((nsdata->rescap >> NVME_NS_DATA_RESCAP_PTPL_SHIFT) &
141 	     NVME_NS_DATA_RESCAP_PTPL_MASK) ? "PTPL" : "");
142 	printf("Format Progress Indicator:   ");
143 	if ((nsdata->fpi >> NVME_NS_DATA_FPI_SUPP_SHIFT) &
144 	    NVME_NS_DATA_FPI_SUPP_MASK) {
145 		printf("%u%% remains\n",
146 		    (nsdata->fpi >> NVME_NS_DATA_FPI_PERC_SHIFT) &
147 		    NVME_NS_DATA_FPI_PERC_MASK);
148 	} else
149 		printf("Not Supported\n");
150 	t = (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_READ_SHIFT) &
151 	    NVME_NS_DATA_DLFEAT_READ_MASK;
152 	printf("Deallocate Logical Block:    Read %s%s%s\n",
153 	    (t == NVME_NS_DATA_DLFEAT_READ_NR) ? "Not Reported" :
154 	    (t == NVME_NS_DATA_DLFEAT_READ_00) ? "00h" :
155 	    (t == NVME_NS_DATA_DLFEAT_READ_FF) ? "FFh" : "Unknown",
156 	    (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_DWZ_SHIFT) &
157 	     NVME_NS_DATA_DLFEAT_DWZ_MASK ? ", Write Zero" : "",
158 	    (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_GCRC_SHIFT) &
159 	     NVME_NS_DATA_DLFEAT_GCRC_MASK ? ", Guard CRC" : "");
160 	printf("Optimal I/O Boundary:        %u blocks\n", nsdata->noiob);
161 	printf("NVM Capacity:                %s bytes\n",
162 	   uint128_to_str(to128(nsdata->nvmcap), cbuf, sizeof(cbuf)));
163 	if ((nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) &
164 	    NVME_NS_DATA_NSFEAT_NPVALID_MASK) {
165 		printf("Preferred Write Granularity: %u blocks\n",
166 		    nsdata->npwg + 1);
167 		printf("Preferred Write Alignment:   %u blocks\n",
168 		    nsdata->npwa + 1);
169 		printf("Preferred Deallocate Granul: %u blocks\n",
170 		    nsdata->npdg + 1);
171 		printf("Preferred Deallocate Align:  %u blocks\n",
172 		    nsdata->npda + 1);
173 		printf("Optimal Write Size:          %u blocks\n",
174 		    nsdata->nows + 1);
175 	}
176 	printf("Globally Unique Identifier:  ");
177 	for (i = 0; i < sizeof(nsdata->nguid); i++)
178 		printf("%02x", nsdata->nguid[i]);
179 	printf("\n");
180 	printf("IEEE EUI64:                  ");
181 	for (i = 0; i < sizeof(nsdata->eui64); i++)
182 		printf("%02x", nsdata->eui64[i]);
183 	printf("\n");
184 	for (i = 0; i <= nsdata->nlbaf; i++) {
185 		lbaf = nsdata->lbaf[i];
186 		lbads = (lbaf >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
187 			NVME_NS_DATA_LBAF_LBADS_MASK;
188 		if (lbads == 0)
189 			continue;
190 		ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) &
191 			NVME_NS_DATA_LBAF_MS_MASK;
192 		rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) &
193 			NVME_NS_DATA_LBAF_RP_MASK;
194 		printf("LBA Format #%02d: Data Size: %5d  Metadata Size: %5d"
195 		    "  Performance: %s\n",
196 		    i, 1 << lbads, ms, (rp == 0) ? "Best" :
197 		    (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded");
198 	}
199 }
200 
201 static void
202 identify_ctrlr(int fd)
203 {
204 	struct nvme_controller_data	cdata;
205 	int				hexlength;
206 
207 	if (read_controller_data(fd, &cdata))
208 		errx(EX_IOERR, "Identify request failed");
209 	close(fd);
210 
211 	if (opt.hex) {
212 		if (opt.verbose)
213 			hexlength = sizeof(struct nvme_controller_data);
214 		else
215 			hexlength = offsetof(struct nvme_controller_data,
216 			    reserved8);
217 		print_hex(&cdata, hexlength);
218 		exit(0);
219 	}
220 
221 	nvme_print_controller(&cdata);
222 	exit(0);
223 }
224 
225 static void
226 identify_ns(int fd, uint32_t nsid)
227 {
228 	struct nvme_namespace_data	nsdata;
229 	int				hexlength;
230 
231 	if (read_namespace_data(fd, nsid, &nsdata))
232 		errx(EX_IOERR, "Identify request failed");
233 	close(fd);
234 
235 	if (opt.hex) {
236 		if (opt.verbose)
237 			hexlength = sizeof(struct nvme_namespace_data);
238 		else
239 			hexlength = offsetof(struct nvme_namespace_data,
240 			    reserved6);
241 		print_hex(&nsdata, hexlength);
242 		exit(0);
243 	}
244 
245 	print_namespace(&nsdata);
246 	exit(0);
247 }
248 
249 static void
250 identify(const struct cmd *f, int argc, char *argv[])
251 {
252 	char		*path;
253 	int		fd;
254 	uint32_t	nsid;
255 
256 	if (arg_parse(argc, argv, f))
257 		return;
258 
259 	open_dev(opt.dev, &fd, 0, 1);
260 	get_nsid(fd, &path, &nsid);
261 	if (nsid != 0) {
262 		/*
263 		 * We got namespace device, but we need to send IDENTIFY
264 		 * commands to the controller, not the namespace, since it
265 		 * is an admin cmd.  The namespace ID will be specified in
266 		 * the IDENTIFY command itself.
267 		 */
268 		close(fd);
269 		open_dev(path, &fd, 0, 1);
270 	}
271 	free(path);
272 	if (opt.nsid != NONE)
273 		nsid = opt.nsid;
274 
275 	if (nsid == 0)
276 		identify_ctrlr(fd);
277 	else
278 		identify_ns(fd, nsid);
279 }
280 
281 static const struct opts identify_opts[] = {
282 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
283 	OPT("hex", 'x', arg_none, opt, hex,
284 	    "Print identiy information in hex"),
285 	OPT("verbose", 'v', arg_none, opt, verbose,
286 	    "More verbosity: print entire identify table"),
287 	OPT("nsid", 'n', arg_uint32, opt, nsid,
288 	    "Namespace ID to use if not in device name"),
289 	{ NULL, 0, arg_none, NULL, NULL }
290 };
291 #undef OPT
292 
293 static const struct args identify_args[] = {
294 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
295 	{ arg_none, NULL, NULL },
296 };
297 
298 static struct cmd identify_cmd = {
299 	.name = "identify",
300 	.fn = identify,
301 	.descr = "Print summary of the IDENTIFY information",
302 	.ctx_size = sizeof(opt),
303 	.opts = identify_opts,
304 	.args = identify_args,
305 };
306 
307 CMD_COMMAND(identify_cmd);
308