devlist.c revision 1.1 1 /* $NetBSD: devlist.c,v 1.1 2016/06/04 16:29:35 nonaka Exp $ */
2
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/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: devlist.c,v 1.1 2016/06/04 16:29:35 nonaka Exp $");
32 #if 0
33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/devlist.c 260381 2014-01-06 23:48:47Z jimharris $");
34 #endif
35 #endif
36
37 #include <sys/param.h>
38
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <paths.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "nvmectl.h"
50
51 static void
52 devlist_usage(void)
53 {
54 fprintf(stderr, "usage:\n");
55 fprintf(stderr, DEVLIST_USAGE);
56 exit(1);
57 }
58
59 static inline uint32_t
60 ns_get_sector_size(struct nvm_identify_namespace *nsdata)
61 {
62
63 return 1 << nsdata->lbaf[NVME_ID_NS_FLBAS(nsdata->flbas)].lbads;
64 }
65
66 void
67 devlist(int argc, char *argv[])
68 {
69 struct nvm_identify_controller cdata;
70 struct nvm_identify_namespace nsdata;
71 char name[64];
72 uint8_t mn[64];
73 uint32_t i;
74 int ch, ctrlr, fd, found, ret;
75
76 while ((ch = getopt(argc, argv, "")) != -1) {
77 switch (ch) {
78 default:
79 devlist_usage();
80 }
81 }
82
83 ctrlr = -1;
84 found = 0;
85
86 while (1) {
87 ctrlr++;
88 sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr);
89
90 ret = open_dev(name, &fd, 0, 0);
91
92 if (ret != 0) {
93 if (ret == EACCES) {
94 warnx("could not open "_PATH_DEV"%s\n", name);
95 continue;
96 } else
97 break;
98 }
99
100 found++;
101 read_controller_data(fd, &cdata);
102 nvme_strvis(mn, sizeof(mn), cdata.mn, sizeof(cdata.mn));
103 printf("%6s: %s\n", name, mn);
104
105 for (i = 0; i < cdata.nn; i++) {
106 sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
107 NVME_NS_PREFIX, i+1);
108 read_namespace_data(fd, i+1, &nsdata);
109 printf(" %10s (%lldMB)\n",
110 name,
111 nsdata.nsze *
112 (long long)ns_get_sector_size(&nsdata) /
113 1024 / 1024);
114 }
115
116 close(fd);
117 }
118
119 if (found == 0)
120 printf("No NVMe controllers found.\n");
121
122 exit(1);
123 }
124