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