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