1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2012-2013 Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/nv.h>
31 #include <sys/time.h>
32
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdbool.h>
37 #include <libnvmf.h>
38 #include <libutil.h>
39 #include <paths.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 #include "nvmecontrol.h"
49 #include "comnd.h"
50
51 /* Tables for command line parsing */
52
53 #define NVME_MAX_UNIT 256
54
55 static cmd_fn_t devlist;
56
57 static struct options {
58 bool human;
59 } opt = {
60 .human = false,
61 };
62
63 static const struct opts devlist_opts[] = {
64 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
65 OPT("human", 'h', arg_none, opt, human,
66 "Show human readable disk size"),
67 { NULL, 0, arg_none, NULL, NULL }
68 };
69 #undef OPT
70
71 static struct cmd devlist_cmd = {
72 .name = "devlist",
73 .fn = devlist,
74 .descr = "List NVMe controllers and namespaces",
75 .ctx_size = sizeof(opt),
76 .opts = devlist_opts,
77 .args = NULL,
78 };
79
80 CMD_COMMAND(devlist_cmd);
81
82 /* End of tables for command line parsing */
83
84 static inline uint32_t
ns_get_sector_size(struct nvme_namespace_data * nsdata)85 ns_get_sector_size(struct nvme_namespace_data *nsdata)
86 {
87 uint8_t flbas_fmt, lbads;
88
89 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsdata->flbas);
90 lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, nsdata->lbaf[flbas_fmt]);
91
92 return (1 << lbads);
93 }
94
95 static void
scan_namespace(int fd,int ctrlr,uint32_t nsid)96 scan_namespace(int fd, int ctrlr, uint32_t nsid)
97 {
98 struct nvme_namespace_data nsdata;
99 char name[64];
100 uint8_t buf[7];
101 uint64_t size;
102
103 if (read_namespace_data(fd, nsid, &nsdata) != 0)
104 return;
105 if (nsdata.nsze == 0)
106 return;
107 snprintf(name, sizeof(name), "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
108 NVME_NS_PREFIX, nsid);
109 size = nsdata.nsze * (uint64_t)ns_get_sector_size(&nsdata);
110 if (opt.human) {
111 humanize_number(buf, sizeof(buf), size, "B",
112 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
113 printf(" %10s (%s)\n", name, buf);
114 } else {
115 printf(" %10s (%juMB)\n", name, (uintmax_t)size / 1024 / 1024);
116 }
117 }
118
119 static bool
print_controller_info(const char * name,int fd,struct nvme_controller_data * cdatap)120 print_controller_info(const char *name, int fd,
121 struct nvme_controller_data *cdatap)
122 {
123 static struct timespec now;
124 struct nvme_controller_data cdata;
125 struct timespec last_disconnect, delta;
126 uint8_t mn[64];
127 nvlist_t *nvl;
128 const nvlist_t *nvl_ts;
129 bool connected;
130
131 /*
132 * If the controller doesn't support connection status, assume
133 * it is connected.
134 */
135 if (nvmf_connection_status(fd, &nvl) != 0) {
136 connected = true;
137 nvl = NULL;
138 } else {
139 connected = nvlist_get_bool(nvl, "connected");
140 }
141
142 if (connected) {
143 if (read_controller_data(fd, &cdata) != 0) {
144 nvlist_destroy(nvl);
145 return (false);
146 }
147 } else {
148 if (ioctl(fd, NVME_GET_CONTROLLER_DATA, &cdata) == -1) {
149 nvlist_destroy(nvl);
150 return (false);
151 }
152 }
153
154 *cdatap = cdata;
155 nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH);
156 printf("%6s: %s", name, mn);
157 if (connected) {
158 const struct nvme_discovery_log_entry *dle;
159 size_t len;
160
161 nvlist_destroy(nvl);
162 if (nvmf_reconnect_params(fd, &nvl) == 0) {
163 dle = nvlist_get_binary(nvl, "dle", &len);
164 if (len == sizeof(*dle)) {
165 printf(" (connected via %s %.*s:%.*s)",
166 nvmf_transport_type(dle->trtype),
167 (int)sizeof(dle->traddr), dle->traddr,
168 (int)sizeof(dle->trsvcid), dle->trsvcid);
169 }
170 } else {
171 nvl = NULL;
172 }
173 } else {
174 if (now.tv_sec == 0)
175 clock_gettime(CLOCK_REALTIME, &now);
176
177 nvl_ts = nvlist_get_nvlist(nvl, "last_disconnect");
178 last_disconnect.tv_sec = nvlist_get_number(nvl_ts, "tv_sec");
179 last_disconnect.tv_nsec = nvlist_get_number(nvl_ts, "tv_nsec");
180 timespecsub(&now, &last_disconnect, &delta);
181 printf(" (disconnected for %ju seconds)",
182 (uintmax_t)delta.tv_sec);
183 }
184 printf("\n");
185 nvlist_destroy(nvl);
186 return (connected);
187 }
188
189 static bool
scan_controller(int ctrlr)190 scan_controller(int ctrlr)
191 {
192 struct nvme_controller_data cdata;
193 struct nvme_ns_list nslist;
194 char name[64];
195 uint32_t nsid;
196 int fd, ret;
197
198 snprintf(name, sizeof(name), "%s%d", NVME_CTRLR_PREFIX, ctrlr);
199
200 ret = open_dev(name, &fd, 0, 0);
201
202 if (ret == EACCES) {
203 warnx("could not open "_PATH_DEV"%s\n", name);
204 return (false);
205 } else if (ret != 0)
206 return (false);
207
208 if (!print_controller_info(name, fd, &cdata)) {
209 close(fd);
210 return (true);
211 }
212
213 /*
214 * Active Namespace ID List (CNS=2) was introduced in NVMe 1.1.
215 * The cdata.ver field was added in NVMe 1.2; for older devices it
216 * is reported as 0. Use CNS=2 only when we know the device is at
217 * least NVMe 1.1, and fall back to iterating cdata.nn otherwise.
218 */
219 if (cdata.ver == 0) {
220 for (nsid = 1; nsid <= cdata.nn; nsid++)
221 scan_namespace(fd, ctrlr, nsid);
222 } else {
223 nsid = 0;
224 for (;;) {
225 if (read_active_namespaces(fd, nsid, &nslist) != 0)
226 break;
227 for (u_int i = 0; i < nitems(nslist.ns); i++) {
228 nsid = nslist.ns[i];
229 if (nsid == 0)
230 break;
231 scan_namespace(fd, ctrlr, nsid);
232 }
233 if (nsid == 0 || nsid >= NVME_GLOBAL_NAMESPACE_TAG - 1)
234 break;
235 }
236 }
237
238 close(fd);
239 return (true);
240 }
241
242 static void
devlist(const struct cmd * f,int argc,char * argv[])243 devlist(const struct cmd *f, int argc, char *argv[])
244 {
245 int ctrlr, found;
246
247 if (arg_parse(argc, argv, f))
248 return;
249
250 ctrlr = -1;
251 found = 0;
252
253 while (ctrlr < NVME_MAX_UNIT) {
254 ctrlr++;
255 if (scan_controller(ctrlr))
256 found++;
257 }
258
259 if (found == 0) {
260 printf("No NVMe controllers found.\n");
261 exit(EX_UNAVAILABLE);
262 }
263
264 exit(0);
265 }
266