]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/identify.c
Import the kyua test framework.
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / identify.c
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 <unistd.h>
44
45 #include "nvmecontrol.h"
46 #include "nvmecontrol_ext.h"
47
48 #define NONE 0xfffffffeu
49
50 static struct options {
51         bool            hex;
52         bool            verbose;
53         const char      *dev;
54         uint32_t        nsid;
55 } opt = {
56         .hex = false,
57         .verbose = false,
58         .dev = NULL,
59         .nsid = NONE,
60 };
61
62 void
63 print_namespace(struct nvme_namespace_data *nsdata)
64 {
65         char cbuf[UINT128_DIG + 1];
66         uint32_t        i;
67         uint32_t        lbaf, lbads, ms, rp;
68         uint8_t         thin_prov, ptype;
69         uint8_t         flbas_fmt, t;
70
71         thin_prov = (nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT) &
72                 NVME_NS_DATA_NSFEAT_THIN_PROV_MASK;
73
74         flbas_fmt = (nsdata->flbas >> NVME_NS_DATA_FLBAS_FORMAT_SHIFT) &
75                 NVME_NS_DATA_FLBAS_FORMAT_MASK;
76
77         printf("Size:                        %lld blocks\n",
78             (long long)nsdata->nsze);
79         printf("Capacity:                    %lld blocks\n",
80             (long long)nsdata->ncap);
81         printf("Utilization:                 %lld blocks\n",
82             (long long)nsdata->nuse);
83         printf("Thin Provisioning:           %s\n",
84                 thin_prov ? "Supported" : "Not Supported");
85         printf("Number of LBA Formats:       %d\n", nsdata->nlbaf+1);
86         printf("Current LBA Format:          LBA Format #%02d\n", flbas_fmt);
87         printf("Data Protection Caps:        %s%s%s%s%s%s\n",
88             (nsdata->dpc == 0) ? "Not Supported" : "",
89             ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_END_SHIFT) &
90              NVME_NS_DATA_DPC_MD_END_MASK) ? "Last Bytes, " : "",
91             ((nsdata->dpc >> NVME_NS_DATA_DPC_MD_START_SHIFT) &
92              NVME_NS_DATA_DPC_MD_START_MASK) ? "First Bytes, " : "",
93             ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT3_SHIFT) &
94              NVME_NS_DATA_DPC_PIT3_MASK) ? "Type 3, " : "",
95             ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_SHIFT) &
96              NVME_NS_DATA_DPC_PIT2_MASK) ? "Type 2, " : "",
97             ((nsdata->dpc >> NVME_NS_DATA_DPC_PIT2_MASK) &
98              NVME_NS_DATA_DPC_PIT1_MASK) ? "Type 1" : "");
99         printf("Data Protection Settings:    ");
100         ptype = (nsdata->dps >> NVME_NS_DATA_DPS_PIT_SHIFT) &
101             NVME_NS_DATA_DPS_PIT_MASK;
102         if (ptype) {
103                 printf("Type %d, %s Bytes\n", ptype,
104                     ((nsdata->dps >> NVME_NS_DATA_DPS_MD_START_SHIFT) &
105                      NVME_NS_DATA_DPS_MD_START_MASK) ? "First" : "Last");
106         } else {
107                 printf("Not Enabled\n");
108         }
109         printf("Multi-Path I/O Capabilities: %s%s\n",
110             (nsdata->nmic == 0) ? "Not Supported" : "",
111             ((nsdata->nmic >> NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) &
112              NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK) ? "May be shared" : "");
113         printf("Reservation Capabilities:    %s%s%s%s%s%s%s%s%s\n",
114             (nsdata->rescap == 0) ? "Not Supported" : "",
115             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_IEKEY13_SHIFT) &
116              NVME_NS_DATA_RESCAP_IEKEY13_MASK) ? "IEKEY13, " : "",
117             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT) &
118              NVME_NS_DATA_RESCAP_EX_AC_AR_MASK) ? "EX_AC_AR, " : "",
119             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT) &
120              NVME_NS_DATA_RESCAP_WR_EX_AR_MASK) ? "WR_EX_AR, " : "",
121             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT) &
122              NVME_NS_DATA_RESCAP_EX_AC_RO_MASK) ? "EX_AC_RO, " : "",
123             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT) &
124              NVME_NS_DATA_RESCAP_WR_EX_RO_MASK) ? "WR_EX_RO, " : "",
125             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_EX_AC_SHIFT) &
126              NVME_NS_DATA_RESCAP_EX_AC_MASK) ? "EX_AC, " : "",
127             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_WR_EX_SHIFT) &
128              NVME_NS_DATA_RESCAP_WR_EX_MASK) ? "WR_EX, " : "",
129             ((nsdata->rescap >> NVME_NS_DATA_RESCAP_PTPL_SHIFT) &
130              NVME_NS_DATA_RESCAP_PTPL_MASK) ? "PTPL" : "");
131         printf("Format Progress Indicator:   ");
132         if ((nsdata->fpi >> NVME_NS_DATA_FPI_SUPP_SHIFT) &
133             NVME_NS_DATA_FPI_SUPP_MASK) {
134                 printf("%u%% remains\n",
135                     (nsdata->fpi >> NVME_NS_DATA_FPI_PERC_SHIFT) &
136                     NVME_NS_DATA_FPI_PERC_MASK);
137         } else
138                 printf("Not Supported\n");
139         t = (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_READ_SHIFT) &
140             NVME_NS_DATA_DLFEAT_READ_MASK;
141         printf("Deallocate Logical Block:    Read %s%s%s\n",
142             (t == NVME_NS_DATA_DLFEAT_READ_NR) ? "Not Reported" :
143             (t == NVME_NS_DATA_DLFEAT_READ_00) ? "00h" :
144             (t == NVME_NS_DATA_DLFEAT_READ_FF) ? "FFh" : "Unknown",
145             (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_DWZ_SHIFT) &
146              NVME_NS_DATA_DLFEAT_DWZ_MASK ? ", Write Zero" : "",
147             (nsdata->dlfeat >> NVME_NS_DATA_DLFEAT_GCRC_SHIFT) &
148              NVME_NS_DATA_DLFEAT_GCRC_MASK ? ", Guard CRC" : "");
149         printf("Optimal I/O Boundary:        %u blocks\n", nsdata->noiob);
150         printf("NVM Capacity:                %s bytes\n",
151            uint128_to_str(to128(nsdata->nvmcap), cbuf, sizeof(cbuf)));
152         if ((nsdata->nsfeat >> NVME_NS_DATA_NSFEAT_NPVALID_SHIFT) &
153             NVME_NS_DATA_NSFEAT_NPVALID_MASK) {
154                 printf("Preferred Write Granularity: %u blocks",
155                     nsdata->npwg + 1);
156                 printf("Preferred Write Alignment:   %u blocks",
157                     nsdata->npwa + 1);
158                 printf("Preferred Deallocate Granul: %u blocks",
159                     nsdata->npdg + 1);
160                 printf("Preferred Deallocate Align:  %u blocks",
161                     nsdata->npda + 1);
162                 printf("Optimal Write Size:          %u blocks",
163                     nsdata->nows + 1);
164         }
165         printf("Globally Unique Identifier:  ");
166         for (i = 0; i < sizeof(nsdata->nguid); i++)
167                 printf("%02x", nsdata->nguid[i]);
168         printf("\n");
169         printf("IEEE EUI64:                  ");
170         for (i = 0; i < sizeof(nsdata->eui64); i++)
171                 printf("%02x", nsdata->eui64[i]);
172         printf("\n");
173         for (i = 0; i <= nsdata->nlbaf; i++) {
174                 lbaf = nsdata->lbaf[i];
175                 lbads = (lbaf >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
176                         NVME_NS_DATA_LBAF_LBADS_MASK;
177                 ms = (lbaf >> NVME_NS_DATA_LBAF_MS_SHIFT) &
178                         NVME_NS_DATA_LBAF_MS_MASK;
179                 rp = (lbaf >> NVME_NS_DATA_LBAF_RP_SHIFT) &
180                         NVME_NS_DATA_LBAF_RP_MASK;
181                 printf("LBA Format #%02d: Data Size: %5d  Metadata Size: %5d"
182                     "  Performance: %s\n",
183                     i, 1 << lbads, ms, (rp == 0) ? "Best" :
184                     (rp == 1) ? "Better" : (rp == 2) ? "Good" : "Degraded");
185         }
186 }
187
188 static void
189 identify_ctrlr(int fd)
190 {
191         struct nvme_controller_data     cdata;
192         int                             hexlength;
193
194         read_controller_data(fd, &cdata);
195         close(fd);
196
197         if (opt.hex) {
198                 if (opt.verbose)
199                         hexlength = sizeof(struct nvme_controller_data);
200                 else
201                         hexlength = offsetof(struct nvme_controller_data,
202                             reserved8);
203                 print_hex(&cdata, hexlength);
204                 exit(0);
205         }
206
207         nvme_print_controller(&cdata);
208         exit(0);
209 }
210
211 static void
212 identify_ns(int fd, uint32_t nsid)
213 {
214         struct nvme_namespace_data      nsdata;
215         int                             hexlength;
216
217         read_namespace_data(fd, nsid, &nsdata);
218         close(fd);
219
220         if (opt.hex) {
221                 if (opt.verbose)
222                         hexlength = sizeof(struct nvme_namespace_data);
223                 else
224                         hexlength = offsetof(struct nvme_namespace_data,
225                             reserved6);
226                 print_hex(&nsdata, hexlength);
227                 exit(0);
228         }
229
230         print_namespace(&nsdata);
231         exit(0);
232 }
233
234 static void
235 identify(const struct cmd *f, int argc, char *argv[])
236 {
237         char            *path;
238         int             fd;
239         uint32_t        nsid;
240
241         arg_parse(argc, argv, f);
242
243         open_dev(opt.dev, &fd, 1, 1);
244         get_nsid(fd, &path, &nsid);
245         if (nsid != 0) {
246                 /*
247                  * We got namespace device, but we need to send IDENTIFY
248                  * commands to the controller, not the namespace, since it
249                  * is an admin cmd.  The namespace ID will be specified in
250                  * the IDENTIFY command itself.
251                  */
252                 close(fd);
253                 open_dev(path, &fd, 1, 1);
254         }
255         free(path);
256         if (opt.nsid != NONE)
257                 nsid = opt.nsid;
258
259         if (nsid == 0)
260                 identify_ctrlr(fd);
261         else
262                 identify_ns(fd, nsid);
263 }
264
265 static const struct opts identify_opts[] = {
266 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
267         OPT("hex", 'x', arg_none, opt, hex,
268             "Print identiy information in hex"),
269         OPT("verbose", 'v', arg_none, opt, verbose,
270             "More verbosity: print entire identify table"),
271         OPT("nsid", 'n', arg_uint32, opt, nsid,
272             "Namespace ID to use if not in device name"),
273         { NULL, 0, arg_none, NULL, NULL }
274 };
275 #undef OPT
276
277 static const struct args identify_args[] = {
278         { arg_string, &opt.dev, "controller-id|namespace-id" },
279         { arg_none, NULL, NULL },
280 };
281
282 static struct cmd identify_cmd = {
283         .name = "identify",
284         .fn = identify,
285         .descr = "Print summary of the IDENTIFY information",
286         .ctx_size = sizeof(opt),
287         .opts = identify_opts,
288         .args = identify_args,
289 };
290
291 CMD_COMMAND(identify_cmd);