xref: /netbsd-src/sbin/nvmectl/devlist.c (revision 1145fafc6f8c2b607b406a0d818e14c3e5b00d9d)
1 /*	$NetBSD: devlist.c,v 1.6 2023/03/05 23:24:06 simonb Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (C) 2012-2013 Intel Corporation
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: devlist.c,v 1.6 2023/03/05 23:24:06 simonb Exp $");
34 #if 0
35 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/devlist.c 329824 2018-02-22 13:32:31Z wma $");
36 #endif
37 #endif
38 
39 #include <sys/param.h>
40 
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <paths.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "nvmectl.h"
52 
53 __dead static void
devlist_usage(void)54 devlist_usage(void)
55 {
56 	fprintf(stderr, "usage:\n");
57 	fprintf(stderr, "\t%s " DEVLIST_USAGE, getprogname());
58 	exit(1);
59 }
60 
61 static inline uint32_t
ns_get_sector_size(struct nvm_identify_namespace * nsdata)62 ns_get_sector_size(struct nvm_identify_namespace *nsdata)
63 {
64 
65 	return 1 << nsdata->lbaf[NVME_ID_NS_FLBAS(nsdata->flbas)].lbads;
66 }
67 
68 void
devlist(int argc,char * argv[])69 devlist(int argc, char *argv[])
70 {
71 	struct nvm_identify_controller	cdata;
72 	struct nvm_identify_namespace	nsdata;
73 	char				name[64];
74 	uint8_t				mn[64];
75 	uint32_t			i;
76 	int				ch, ctrlr, fd, found, ret;
77 
78 	while ((ch = getopt(argc, argv, "")) != -1) {
79 		switch (ch) {
80 		default:
81 			devlist_usage();
82 		}
83 	}
84 
85 	ctrlr = -1;
86 	found = 0;
87 
88 	while (1) {
89 		ctrlr++;
90 		sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr);
91 
92 		ret = open_dev(name, &fd, 0, 0);
93 
94 		if (ret != 0) {
95 			if (ret == EACCES) {
96 				warnx("could not open "_PATH_DEV"%s\n", name);
97 				continue;
98 			} else
99 				break;
100 		}
101 
102 		found++;
103 		read_controller_data(fd, &cdata);
104 		nvme_strvis(mn, sizeof(mn), cdata.mn, sizeof(cdata.mn));
105 		printf("%6s: %s\n", name, mn);
106 
107 		for (i = 0; i < cdata.nn; i++) {
108 			sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
109 			    NVME_NS_PREFIX, i+1);
110 			read_namespace_data(fd, i+1, &nsdata);
111 			printf("  %10s (%lldMB)\n",
112 				name,
113 				nsdata.nsze *
114 				(long long)ns_get_sector_size(&nsdata) /
115 				1024 / 1024);
116 		}
117 
118 		close(fd);
119 	}
120 
121 	if (found == 0) {
122 		printf("No NVMe controllers found.\n");
123 		exit(1);
124 	}
125 
126 	exit(0);
127 }
128