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