xref: /freebsd/sbin/nvmecontrol/identify.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
1 /*-
2  * Copyright (C) 2012-2013 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "nvmecontrol.h"
42 
43 static void
44 print_controller(struct nvme_controller_data *cdata)
45 {
46 	uint8_t str[128];
47 	char cbuf[UINT128_DIG + 1];
48 
49 	printf("Controller Capabilities/Features\n");
50 	printf("================================\n");
51 	printf("Vendor ID:                  %04x\n", cdata->vid);
52 	printf("Subsystem Vendor ID:        %04x\n", cdata->ssvid);
53 	nvme_strvis(str, cdata->sn, sizeof(str), NVME_SERIAL_NUMBER_LENGTH);
54 	printf("Serial Number:              %s\n", str);
55 	nvme_strvis(str, cdata->mn, sizeof(str), NVME_MODEL_NUMBER_LENGTH);
56 	printf("Model Number:               %s\n", str);
57 	nvme_strvis(str, cdata->fr, sizeof(str), NVME_FIRMWARE_REVISION_LENGTH);
58 	printf("Firmware Version:           %s\n", str);
59 	printf("Recommended Arb Burst:      %d\n", cdata->rab);
60 	printf("IEEE OUI Identifier:        %02x %02x %02x\n",
61 		cdata->ieee[0], cdata->ieee[1], cdata->ieee[2]);
62 	printf("Multi-Interface Cap:        %02x\n", cdata->mic);
63 	/* TODO: Use CAP.MPSMIN to determine true memory page size. */
64 	printf("Max Data Transfer Size:     ");
65 	if (cdata->mdts == 0)
66 		printf("Unlimited\n");
67 	else
68 		printf("%d\n", PAGE_SIZE * (1 << cdata->mdts));
69 
70 	printf("\n");
71 	printf("Admin Command Set Attributes\n");
72 	printf("============================\n");
73 	printf("Security Send/Receive:       %s\n",
74 		cdata->oacs.security ? "Supported" : "Not Supported");
75 	printf("Format NVM:                  %s\n",
76 		cdata->oacs.format ? "Supported" : "Not Supported");
77 	printf("Firmware Activate/Download:  %s\n",
78 		cdata->oacs.firmware ? "Supported" : "Not Supported");
79 	printf("Namespace Managment:         %s\n",
80 		   cdata->oacs.nsmgmt ? "Supported" : "Not Supported");
81 	printf("Abort Command Limit:         %d\n", cdata->acl+1);
82 	printf("Async Event Request Limit:   %d\n", cdata->aerl+1);
83 	printf("Number of Firmware Slots:    ");
84 	if (cdata->oacs.firmware != 0)
85 		printf("%d\n", cdata->frmw.num_slots);
86 	else
87 		printf("N/A\n");
88 	printf("Firmware Slot 1 Read-Only:   ");
89 	if (cdata->oacs.firmware != 0)
90 		printf("%s\n", cdata->frmw.slot1_ro ? "Yes" : "No");
91 	else
92 		printf("N/A\n");
93 	printf("Per-Namespace SMART Log:     %s\n",
94 		cdata->lpa.ns_smart ? "Yes" : "No");
95 	printf("Error Log Page Entries:      %d\n", cdata->elpe+1);
96 	printf("Number of Power States:      %d\n", cdata->npss+1);
97 
98 	printf("\n");
99 	printf("NVM Command Set Attributes\n");
100 	printf("==========================\n");
101 	printf("Submission Queue Entry Size\n");
102 	printf("  Max:                       %d\n", 1 << cdata->sqes.max);
103 	printf("  Min:                       %d\n", 1 << cdata->sqes.min);
104 	printf("Completion Queue Entry Size\n");
105 	printf("  Max:                       %d\n", 1 << cdata->cqes.max);
106 	printf("  Min:                       %d\n", 1 << cdata->cqes.min);
107 	printf("Number of Namespaces:        %d\n", cdata->nn);
108 	printf("Compare Command:             %s\n",
109 		cdata->oncs.compare ? "Supported" : "Not Supported");
110 	printf("Write Uncorrectable Command: %s\n",
111 		cdata->oncs.write_unc ? "Supported" : "Not Supported");
112 	printf("Dataset Management Command:  %s\n",
113 		cdata->oncs.dsm ? "Supported" : "Not Supported");
114 	printf("Volatile Write Cache:        %s\n",
115 		cdata->vwc.present ? "Present" : "Not Present");
116 
117 	if (cdata->oacs.nsmgmt) {
118 		printf("\n");
119 		printf("Namespace Drive Attributes\n");
120 		printf("==========================\n");
121 		printf("NVM total cap:               %s\n",
122 			   uint128_to_str(to128(cdata->untncap.tnvmcap), cbuf, sizeof(cbuf)));
123 		printf("NVM unallocated cap:         %s\n",
124 			   uint128_to_str(to128(cdata->untncap.unvmcap), cbuf, sizeof(cbuf)));
125 	}
126 }
127 
128 static void
129 print_namespace(struct nvme_namespace_data *nsdata)
130 {
131 	uint32_t	i;
132 
133 	printf("Size (in LBAs):              %lld (%lldM)\n",
134 		(long long)nsdata->nsze,
135 		(long long)nsdata->nsze / 1024 / 1024);
136 	printf("Capacity (in LBAs):          %lld (%lldM)\n",
137 		(long long)nsdata->ncap,
138 		(long long)nsdata->ncap / 1024 / 1024);
139 	printf("Utilization (in LBAs):       %lld (%lldM)\n",
140 		(long long)nsdata->nuse,
141 		(long long)nsdata->nuse / 1024 / 1024);
142 	printf("Thin Provisioning:           %s\n",
143 		nsdata->nsfeat.thin_prov ? "Supported" : "Not Supported");
144 	printf("Number of LBA Formats:       %d\n", nsdata->nlbaf+1);
145 	printf("Current LBA Format:          LBA Format #%02d\n",
146 		nsdata->flbas.format);
147 	for (i = 0; i <= nsdata->nlbaf; i++)
148 		printf("LBA Format #%02d: Data Size: %5d  Metadata Size: %5d\n",
149 		    i, 1 << nsdata->lbaf[i].lbads, nsdata->lbaf[i].ms);
150 }
151 
152 static void
153 identify_usage(void)
154 {
155 	fprintf(stderr, "usage:\n");
156 	fprintf(stderr, IDENTIFY_USAGE);
157 	exit(1);
158 }
159 
160 static void
161 identify_ctrlr(int argc, char *argv[])
162 {
163 	struct nvme_controller_data	cdata;
164 	int				ch, fd, hexflag = 0, hexlength;
165 	int				verboseflag = 0;
166 
167 	while ((ch = getopt(argc, argv, "vx")) != -1) {
168 		switch ((char)ch) {
169 		case 'v':
170 			verboseflag = 1;
171 			break;
172 		case 'x':
173 			hexflag = 1;
174 			break;
175 		default:
176 			identify_usage();
177 		}
178 	}
179 
180 	/* Check that a controller was specified. */
181 	if (optind >= argc)
182 		identify_usage();
183 
184 	open_dev(argv[optind], &fd, 1, 1);
185 	read_controller_data(fd, &cdata);
186 	close(fd);
187 
188 	if (hexflag == 1) {
189 		if (verboseflag == 1)
190 			hexlength = sizeof(struct nvme_controller_data);
191 		else
192 			hexlength = offsetof(struct nvme_controller_data,
193 			    reserved5);
194 		print_hex(&cdata, hexlength);
195 		exit(0);
196 	}
197 
198 	if (verboseflag == 1) {
199 		fprintf(stderr, "-v not currently supported without -x\n");
200 		identify_usage();
201 	}
202 
203 	print_controller(&cdata);
204 	exit(0);
205 }
206 
207 static void
208 identify_ns(int argc, char *argv[])
209 {
210 	struct nvme_namespace_data	nsdata;
211 	char				path[64];
212 	int				ch, fd, hexflag = 0, hexlength, nsid;
213 	int				verboseflag = 0;
214 
215 	while ((ch = getopt(argc, argv, "vx")) != -1) {
216 		switch ((char)ch) {
217 		case 'v':
218 			verboseflag = 1;
219 			break;
220 		case 'x':
221 			hexflag = 1;
222 			break;
223 		default:
224 			identify_usage();
225 		}
226 	}
227 
228 	/* Check that a namespace was specified. */
229 	if (optind >= argc)
230 		identify_usage();
231 
232 	/*
233 	 * Check if the specified device node exists before continuing.
234 	 *  This is a cleaner check for cases where the correct controller
235 	 *  is specified, but an invalid namespace on that controller.
236 	 */
237 	open_dev(argv[optind], &fd, 1, 1);
238 	close(fd);
239 
240 	/*
241 	 * We send IDENTIFY commands to the controller, not the namespace,
242 	 *  since it is an admin cmd.  The namespace ID will be specified in
243 	 *  the IDENTIFY command itself.  So parse the namespace's device node
244 	 *  string to get the controller substring and namespace ID.
245 	 */
246 	parse_ns_str(argv[optind], path, &nsid);
247 	open_dev(path, &fd, 1, 1);
248 	read_namespace_data(fd, nsid, &nsdata);
249 	close(fd);
250 
251 	if (hexflag == 1) {
252 		if (verboseflag == 1)
253 			hexlength = sizeof(struct nvme_namespace_data);
254 		else
255 			hexlength = offsetof(struct nvme_namespace_data,
256 			    reserved6);
257 		print_hex(&nsdata, hexlength);
258 		exit(0);
259 	}
260 
261 	if (verboseflag == 1) {
262 		fprintf(stderr, "-v not currently supported without -x\n");
263 		identify_usage();
264 	}
265 
266 	print_namespace(&nsdata);
267 	exit(0);
268 }
269 
270 void
271 identify(int argc, char *argv[])
272 {
273 	char	*target;
274 
275 	if (argc < 2)
276 		identify_usage();
277 
278 	while (getopt(argc, argv, "vx") != -1) ;
279 
280 	/* Check that a controller or namespace was specified. */
281 	if (optind >= argc)
282 		identify_usage();
283 
284 	target = argv[optind];
285 
286 	optreset = 1;
287 	optind = 1;
288 
289 	/*
290 	 * If device node contains "ns", we consider it a namespace,
291 	 *  otherwise, consider it a controller.
292 	 */
293 	if (strstr(target, NVME_NS_PREFIX) == NULL)
294 		identify_ctrlr(argc, argv);
295 	else
296 		identify_ns(argc, argv);
297 }
298