xref: /freebsd/sbin/nvmecontrol/identify.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 		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");
186 	}
187 }
188 
189 static void
190 identify_ctrlr(int fd)
191 {
192 	struct nvme_controller_data	cdata;
193 	int				hexlength;
194 
195 	if (read_controller_data(fd, &cdata))
196 		errx(EX_IOERR, "Identify request failed");
197 	close(fd);
198 
199 	if (opt.hex) {
200 		if (opt.verbose)
201 			hexlength = sizeof(struct nvme_controller_data);
202 		else
203 			hexlength = offsetof(struct nvme_controller_data,
204 			    reserved8);
205 		print_hex(&cdata, hexlength);
206 		exit(0);
207 	}
208 
209 	nvme_print_controller(&cdata);
210 	exit(0);
211 }
212 
213 static void
214 identify_ns(int fd, uint32_t nsid)
215 {
216 	struct nvme_namespace_data	nsdata;
217 	int				hexlength;
218 
219 	if (read_namespace_data(fd, nsid, &nsdata))
220 		errx(EX_IOERR, "Identify request failed");
221 	close(fd);
222 
223 	if (opt.hex) {
224 		if (opt.verbose)
225 			hexlength = sizeof(struct nvme_namespace_data);
226 		else
227 			hexlength = offsetof(struct nvme_namespace_data,
228 			    reserved6);
229 		print_hex(&nsdata, hexlength);
230 		exit(0);
231 	}
232 
233 	print_namespace(&nsdata);
234 	exit(0);
235 }
236 
237 static void
238 identify(const struct cmd *f, int argc, char *argv[])
239 {
240 	char		*path;
241 	int		fd;
242 	uint32_t	nsid;
243 
244 	if (arg_parse(argc, argv, f))
245 		return;
246 
247 	open_dev(opt.dev, &fd, 0, 1);
248 	get_nsid(fd, &path, &nsid);
249 	if (nsid != 0) {
250 		/*
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.
255 		 */
256 		close(fd);
257 		open_dev(path, &fd, 0, 1);
258 	}
259 	free(path);
260 	if (opt.nsid != NONE)
261 		nsid = opt.nsid;
262 
263 	if (nsid == 0)
264 		identify_ctrlr(fd);
265 	else
266 		identify_ns(fd, nsid);
267 }
268 
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 }
278 };
279 #undef OPT
280 
281 static const struct args identify_args[] = {
282 	{ arg_string, &opt.dev, "controller-id|namespace-id" },
283 	{ arg_none, NULL, NULL },
284 };
285 
286 static struct cmd identify_cmd = {
287 	.name = "identify",
288 	.fn = identify,
289 	.descr = "Print summary of the IDENTIFY information",
290 	.ctx_size = sizeof(opt),
291 	.opts = identify_opts,
292 	.args = identify_args,
293 };
294 
295 CMD_COMMAND(identify_cmd);
296