]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sbin/nvmecontrol/identify.c
MFC r253109, r253279:
[FreeBSD/stable/9.git] / sbin / nvmecontrol / identify.c
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 <errno.h>
35 #include <fcntl.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include "nvmecontrol.h"
43
44 static void
45 print_controller(struct nvme_controller_data *cdata)
46 {
47         printf("Controller Capabilities/Features\n");
48         printf("================================\n");
49         printf("Vendor ID:                  %04x\n", cdata->vid);
50         printf("Subsystem Vendor ID:        %04x\n", cdata->ssvid);
51         printf("Serial Number:              %s\n", cdata->sn);
52         printf("Model Number:               %s\n", cdata->mn);
53         printf("Firmware Version:           %s\n", cdata->fr);
54         printf("Recommended Arb Burst:      %d\n", cdata->rab);
55         printf("IEEE OUI Identifier:        %02x %02x %02x\n",
56                 cdata->ieee[0], cdata->ieee[1], cdata->ieee[2]);
57         printf("Multi-Interface Cap:        %02x\n", cdata->mic);
58         /* TODO: Use CAP.MPSMIN to determine true memory page size. */
59         printf("Max Data Transfer Size:     ");
60         if (cdata->mdts == 0)
61                 printf("Unlimited\n");
62         else
63                 printf("%d\n", PAGE_SIZE * (1 << cdata->mdts));
64         printf("\n");
65
66         printf("Admin Command Set Attributes\n");
67         printf("============================\n");
68         printf("Security Send/Receive:       %s\n",
69                 cdata->oacs.security ? "Supported" : "Not Supported");
70         printf("Format NVM:                  %s\n",
71                 cdata->oacs.format ? "Supported" : "Not Supported");
72         printf("Firmware Activate/Download:  %s\n",
73                 cdata->oacs.firmware ? "Supported" : "Not Supported");
74         printf("Abort Command Limit:         %d\n", cdata->acl+1);
75         printf("Async Event Request Limit:   %d\n", cdata->aerl+1);
76         printf("Number of Firmware Slots:    ");
77         if (cdata->oacs.firmware != 0)
78                 printf("%d\n", cdata->frmw.num_slots);
79         else
80                 printf("N/A\n");
81         printf("Firmware Slot 1 Read-Only:   ");
82         if (cdata->oacs.firmware != 0)
83                 printf("%s\n", cdata->frmw.slot1_ro ? "Yes" : "No");
84         else
85                 printf("N/A\n");
86         printf("Per-Namespace SMART Log:     %s\n",
87                 cdata->lpa.ns_smart ? "Yes" : "No");
88         printf("Error Log Page Entries:      %d\n", cdata->elpe+1);
89         printf("Number of Power States:      %d\n", cdata->npss+1);
90         printf("\n");
91
92         printf("NVM Command Set Attributes\n");
93         printf("==========================\n");
94         printf("Submission Queue Entry Size\n");
95         printf("  Max:                       %d\n", 1 << cdata->sqes.max);
96         printf("  Min:                       %d\n", 1 << cdata->sqes.min);
97         printf("Completion Queue Entry Size\n");
98         printf("  Max:                       %d\n", 1 << cdata->cqes.max);
99         printf("  Min:                       %d\n", 1 << cdata->cqes.min);
100         printf("Number of Namespaces:        %d\n", cdata->nn);
101         printf("Compare Command:             %s\n",
102                 cdata->oncs.compare ? "Supported" : "Not Supported");
103         printf("Write Uncorrectable Command: %s\n",
104                 cdata->oncs.write_unc ? "Supported" : "Not Supported");
105         printf("Dataset Management Command:  %s\n",
106                 cdata->oncs.dsm ? "Supported" : "Not Supported");
107         printf("Volatile Write Cache:        %s\n",
108                 cdata->vwc.present ? "Present" : "Not Present");
109 }
110
111 static void
112 print_namespace(struct nvme_namespace_data *nsdata)
113 {
114         uint32_t        i;
115
116         printf("Size (in LBAs):              %lld (%lldM)\n",
117                 (long long)nsdata->nsze,
118                 (long long)nsdata->nsze / 1024 / 1024);
119         printf("Capacity (in LBAs):          %lld (%lldM)\n",
120                 (long long)nsdata->ncap,
121                 (long long)nsdata->ncap / 1024 / 1024);
122         printf("Utilization (in LBAs):       %lld (%lldM)\n",
123                 (long long)nsdata->nuse,
124                 (long long)nsdata->nuse / 1024 / 1024);
125         printf("Thin Provisioning:           %s\n",
126                 nsdata->nsfeat.thin_prov ? "Supported" : "Not Supported");
127         printf("Number of LBA Formats:       %d\n", nsdata->nlbaf+1);
128         printf("Current LBA Format:          LBA Format #%d\n",
129                 nsdata->flbas.format);
130         for (i = 0; i <= nsdata->nlbaf; i++) {
131                 printf("LBA Format #%d:\n", i);
132                 printf("  LBA Data Size:             %d\n",
133                         1 << nsdata->lbaf[i].lbads);
134         }
135 }
136
137 static void
138 identify_usage(void)
139 {
140         fprintf(stderr, "usage:\n");
141         fprintf(stderr, IDENTIFY_USAGE);
142         exit(1);
143 }
144
145 static void
146 identify_ctrlr(int argc, char *argv[])
147 {
148         struct nvme_controller_data     cdata;
149         int                             ch, fd, hexflag = 0, hexlength;
150         int                             verboseflag = 0;
151
152         while ((ch = getopt(argc, argv, "vx")) != -1) {
153                 switch ((char)ch) {
154                 case 'v':
155                         verboseflag = 1;
156                         break;
157                 case 'x':
158                         hexflag = 1;
159                         break;
160                 default:
161                         identify_usage();
162                 }
163         }
164
165         /* Check that a controller was specified. */
166         if (optind >= argc)
167                 identify_usage();
168
169         open_dev(argv[optind], &fd, 1, 1);
170         read_controller_data(fd, &cdata);
171         close(fd);
172
173         if (hexflag == 1) {
174                 if (verboseflag == 1)
175                         hexlength = sizeof(struct nvme_controller_data);
176                 else
177                         hexlength = offsetof(struct nvme_controller_data,
178                             reserved5);
179                 print_hex(&cdata, hexlength);
180                 exit(0);
181         }
182
183         if (verboseflag == 1) {
184                 fprintf(stderr, "-v not currently supported without -x\n");
185                 identify_usage();
186         }
187
188         print_controller(&cdata);
189         exit(0);
190 }
191
192 static void
193 identify_ns(int argc, char *argv[])
194 {
195         struct nvme_namespace_data      nsdata;
196         char                            path[64];
197         char                            *nsloc;
198         int                             ch, fd, hexflag = 0, hexlength, nsid;
199         int                             verboseflag = 0;
200
201         while ((ch = getopt(argc, argv, "vx")) != -1) {
202                 switch ((char)ch) {
203                 case 'v':
204                         verboseflag = 1;
205                         break;
206                 case 'x':
207                         hexflag = 1;
208                         break;
209                 default:
210                         identify_usage();
211                 }
212         }
213
214         /* Check that a namespace was specified. */
215         if (optind >= argc)
216                 identify_usage();
217
218         /*
219          * Check if the specified device node exists before continuing.
220          *  This is a cleaner check for cases where the correct controller
221          *  is specified, but an invalid namespace on that controller.
222          */
223         open_dev(argv[optind], &fd, 1, 1);
224         close(fd);
225
226         /*
227          * Pull the namespace id from the string. +2 skips past the "ns" part
228          *  of the string.  Don't search past 10 characters into the string,
229          *  otherwise we know it is malformed.
230          */
231         nsloc = strnstr(argv[optind], NVME_NS_PREFIX, 10);
232         if (nsloc != NULL)
233                 nsid = strtol(nsloc + 2, NULL, 10);
234         if (nsloc == NULL || (nsid == 0 && errno != 0))
235                 errx(1, "invalid namespace ID '%s'", argv[optind]);
236
237         /*
238          * We send IDENTIFY commands to the controller, not the namespace,
239          *  since it is an admin cmd.  So the path should only include the
240          *  nvmeX part of the nvmeXnsY string.
241          */
242         snprintf(path, nsloc - argv[optind] + 1, "%s", argv[optind]);
243         open_dev(path, &fd, 1, 1);
244         read_namespace_data(fd, nsid, &nsdata);
245         close(fd);
246
247         if (hexflag == 1) {
248                 if (verboseflag == 1)
249                         hexlength = sizeof(struct nvme_namespace_data);
250                 else
251                         hexlength = offsetof(struct nvme_namespace_data,
252                             reserved6);
253                 print_hex(&nsdata, hexlength);
254                 exit(0);
255         }
256
257         if (verboseflag == 1) {
258                 fprintf(stderr, "-v not currently supported without -x\n");
259                 identify_usage();
260         }
261
262         print_namespace(&nsdata);
263         exit(0);
264 }
265
266 void
267 identify(int argc, char *argv[])
268 {
269         char    *target;
270
271         if (argc < 2)
272                 identify_usage();
273
274         while (getopt(argc, argv, "vx") != -1) ;
275
276         target = argv[optind];
277
278         optreset = 1;
279         optind = 1;
280
281         /*
282          * If device node contains "ns", we consider it a namespace,
283          *  otherwise, consider it a controller.
284          */
285         if (strstr(target, NVME_NS_PREFIX) == NULL)
286                 identify_ctrlr(argc, argv);
287         else
288                 identify_ns(argc, argv);
289 }