xref: /freebsd/sbin/nvmecontrol/identify.c (revision c1d255d3)
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\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;
103 	if (ptype) {
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");
107 	} else {
108 		printf("Not Enabled\n");
109 	}
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);
138 	} else
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",
156 		    nsdata->npwg + 1);
157 		printf("Preferred Write Alignment:   %u blocks\n",
158 		    nsdata->npwa + 1);
159 		printf("Preferred Deallocate Granul: %u blocks\n",
160 		    nsdata->npdg + 1);
161 		printf("Preferred Deallocate Align:  %u blocks\n",
162 		    nsdata->npda + 1);
163 		printf("Optimal Write Size:          %u blocks\n",
164 		    nsdata->nows + 1);
165 	}
166 	printf("Globally Unique Identifier:  ");
167 	for (i = 0; i < sizeof(nsdata->nguid); i++)
168 		printf("%02x", nsdata->nguid[i]);
169 	printf("\n");
170 	printf("IEEE EUI64:                  ");
171 	for (i = 0; i < sizeof(nsdata->eui64); i++)
172 		printf("%02x", nsdata->eui64[i]);
173 	printf("\n");
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 		if (lbads == 0)
179 			continue;
180 		ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) &
181 			NVME_NS_DATA_LBAF_MS_MASK;
182 		rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) &
183 			NVME_NS_DATA_LBAF_RP_MASK;
184 		printf("LBA Format #%02d: Data Size: %5d  Metadata Size: %5d"
185 		    "  Performance: %s\n",
186 		    i, 1 << lbads, ms, (rp == 0) ? "Best" :
187 		    (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded");
188 	}
189 }
190 
191 static void
192 identify_ctrlr(int fd)
193 {
194 	struct nvme_controller_data	cdata;
195 	int				hexlength;
196 
197 	if (read_controller_data(fd, &cdata))
198 		errx(EX_IOERR, "Identify request failed");
199 	close(fd);
200 
201 	if (opt.hex) {
202 		if (opt.verbose)
203 			hexlength = sizeof(struct nvme_controller_data);
204 		else
205 			hexlength = offsetof(struct nvme_controller_data,
206 			    reserved8);
207 		print_hex(&cdata, hexlength);
208 		exit(0);
209 	}
210 
211 	nvme_print_controller(&cdata);
212 	exit(0);
213 }
214 
215 static void
216 identify_ns(int fd, uint32_t nsid)
217 {
218 	struct nvme_namespace_data	nsdata;
219 	int				hexlength;
220 
221 	if (read_namespace_data(fd, nsid, &nsdata))
222 		errx(EX_IOERR, "Identify request failed");
223 	close(fd);
224 
225 	if (opt.hex) {
226 		if (opt.verbose)
227 			hexlength = sizeof(struct nvme_namespace_data);
228 		else
229 			hexlength = offsetof(struct nvme_namespace_data,
230 			    reserved6);
231 		print_hex(&nsdata, hexlength);
232 		exit(0);
233 	}
234 
235 	print_namespace(&nsdata);
236 	exit(0);
237 }
238 
239 static void
240 identify(const struct cmd *f, int argc, char *argv[])
241 {
242 	char		*path;
243 	int		fd;
244 	uint32_t	nsid;
245 
246 	if (arg_parse(argc, argv, f))
247 		return;
248 
249 	open_dev(opt.dev, &fd, 0, 1);
250 	get_nsid(fd, &path, &nsid);
251 	if (nsid != 0) {
252 		/*
253 		 * We got namespace device, but we need to send IDENTIFY
254 		 * commands to the controller, not the namespace, since it
255 		 * is an admin cmd.  The namespace ID will be specified in
256 		 * the IDENTIFY command itself.
257 		 */
258 		close(fd);
259 		open_dev(path, &fd, 0, 1);
260 	}
261 	free(path);
262 	if (opt.nsid != NONE)
263 		nsid = opt.nsid;
264 
265 	if (nsid == 0)
266 		identify_ctrlr(fd);
267 	else
268 		identify_ns(fd, nsid);
269 }
270 
271 static const struct opts identify_opts[] = {
272 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
273 	OPT("hex", 'x', arg_none, opt, hex,
274 	    "Print identiy information in hex"),
275 	OPT("verbose", 'v', arg_none, opt, verbose,
276 	    "More verbosity: print entire identify table"),
277 	OPT("nsid", 'n', arg_uint32, opt, nsid,
278 	    "Namespace ID to use if not in device name"),
279 	{ NULL, 0, arg_none, NULL, NULL }
280 };
281 #undef OPT
282 
283 static const struct args identify_args[] = {
284 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
285 	{ arg_none, NULL, NULL },
286 };
287 
288 static struct cmd identify_cmd = {
289 	.name = "identify",
290 	.fn = identify,
291 	.descr = "Print summary of the IDENTIFY information",
292 	.ctx_size = sizeof(opt),
293 	.opts = identify_opts,
294 	.args = identify_args,
295 };
296 
297 CMD_COMMAND(identify_cmd);
298