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