xref: /freebsd/sbin/nvmecontrol/devlist.c (revision 858280e60f0505a2fcf26920ef0b8c9e20cd2d49)
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)120 print_controller_info(const char *name, int fd)
121 {
122 	static struct timespec		now;
123 	struct nvme_controller_data	cdata;
124 	struct timespec			last_disconnect, delta;
125 	uint8_t				mn[64];
126 	nvlist_t			*nvl;
127 	const nvlist_t			*nvl_ts;
128 	bool				connected;
129 
130 	/*
131 	 * If the controller doesn't support connection status, assume
132 	 * it is connected.
133 	 */
134 	if (nvmf_connection_status(fd, &nvl) != 0) {
135 		connected = true;
136 		nvl = NULL;
137 	} else {
138 		connected = nvlist_get_bool(nvl, "connected");
139 	}
140 
141 	if (connected) {
142 		if (read_controller_data(fd, &cdata) != 0) {
143 			nvlist_destroy(nvl);
144 			return (false);
145 		}
146 	} else {
147 		if (ioctl(fd, NVME_GET_CONTROLLER_DATA, &cdata) == -1) {
148 			nvlist_destroy(nvl);
149 			return (false);
150 		}
151 	}
152 
153 	nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH);
154 	printf("%6s: %s", name, mn);
155 	if (connected) {
156 		const struct nvme_discovery_log_entry *dle;
157 		size_t len;
158 
159 		nvlist_destroy(nvl);
160 		if (nvmf_reconnect_params(fd, &nvl) == 0) {
161 			dle = nvlist_get_binary(nvl, "dle", &len);
162 			if (len == sizeof(*dle)) {
163 				printf(" (connected via %s %.*s:%.*s)",
164 				    nvmf_transport_type(dle->trtype),
165 				    (int)sizeof(dle->traddr), dle->traddr,
166 				    (int)sizeof(dle->trsvcid), dle->trsvcid);
167 			}
168 		} else {
169 			nvl = NULL;
170 		}
171 	} else {
172 		if (now.tv_sec == 0)
173 			clock_gettime(CLOCK_REALTIME, &now);
174 
175 		nvl_ts = nvlist_get_nvlist(nvl, "last_disconnect");
176 		last_disconnect.tv_sec = nvlist_get_number(nvl_ts, "tv_sec");
177 		last_disconnect.tv_nsec = nvlist_get_number(nvl_ts, "tv_nsec");
178 		timespecsub(&now, &last_disconnect, &delta);
179 		printf(" (disconnected for %ju seconds)",
180 		    (uintmax_t)delta.tv_sec);
181 	}
182 	printf("\n");
183 	nvlist_destroy(nvl);
184 	return (connected);
185 }
186 
187 static bool
scan_controller(int ctrlr)188 scan_controller(int ctrlr)
189 {
190 	struct nvme_ns_list		nslist;
191 	char				name[64];
192 	uint32_t			nsid;
193 	int				fd, ret;
194 
195 	snprintf(name, sizeof(name), "%s%d", NVME_CTRLR_PREFIX, ctrlr);
196 
197 	ret = open_dev(name, &fd, 0, 0);
198 
199 	if (ret == EACCES) {
200 		warnx("could not open "_PATH_DEV"%s\n", name);
201 		return (false);
202 	} else if (ret != 0)
203 		return (false);
204 
205 	if (!print_controller_info(name, fd)) {
206 		close(fd);
207 		return (true);
208 	}
209 
210 	nsid = 0;
211 	for (;;) {
212 		if (read_active_namespaces(fd, nsid, &nslist) != 0)
213 			break;
214 		for (u_int i = 0; i < nitems(nslist.ns); i++) {
215 			nsid = nslist.ns[i];
216 			if (nsid == 0) {
217 				break;
218 			}
219 
220 			scan_namespace(fd, ctrlr, nsid);
221 		}
222 		if (nsid == 0 || nsid >= NVME_GLOBAL_NAMESPACE_TAG - 1)
223 			break;
224 	}
225 
226 	close(fd);
227 	return (true);
228 }
229 
230 static void
devlist(const struct cmd * f,int argc,char * argv[])231 devlist(const struct cmd *f, int argc, char *argv[])
232 {
233 	int				ctrlr, found;
234 
235 	if (arg_parse(argc, argv, f))
236 		return;
237 
238 	ctrlr = -1;
239 	found = 0;
240 
241 	while (ctrlr < NVME_MAX_UNIT) {
242 		ctrlr++;
243 		if (scan_controller(ctrlr))
244 			found++;
245 	}
246 
247 	if (found == 0) {
248 		printf("No NVMe controllers found.\n");
249 		exit(EX_UNAVAILABLE);
250 	}
251 
252 	exit(0);
253 }
254