devlist.c revision 256281
1176774Sraj/*-
2209908Sraj * Copyright (C) 2012-2013 Intel Corporation
3209908Sraj * All rights reserved.
4176774Sraj *
5176774Sraj * Redistribution and use in source and binary forms, with or without
6176774Sraj * modification, are permitted provided that the following conditions
7176774Sraj * are met:
8176774Sraj * 1. Redistributions of source code must retain the above copyright
9176774Sraj *    notice, this list of conditions and the following disclaimer.
10176774Sraj * 2. Redistributions in binary form must reproduce the above copyright
11176774Sraj *    notice, this list of conditions and the following disclaimer in the
12176774Sraj *    documentation and/or other materials provided with the distribution.
13176774Sraj *
14176774Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15176774Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16176774Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17176774Sraj * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18176774Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19176774Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20176774Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21176774Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22176774Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23176774Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24176774Sraj * SUCH DAMAGE.
25176774Sraj */
26176774Sraj
27176774Sraj#include <sys/cdefs.h>
28176774Sraj__FBSDID("$FreeBSD: stable/10/sbin/nvmecontrol/devlist.c 253476 2013-07-19 21:40:57Z jimharris $");
29176774Sraj
30176774Sraj#include <sys/param.h>
31176774Sraj
32176774Sraj#include <err.h>
33188711Sraj#include <errno.h>
34188711Sraj#include <fcntl.h>
35188711Sraj#include <paths.h>
36188711Sraj#include <stddef.h>
37176774Sraj#include <stdio.h>
38176774Sraj#include <stdlib.h>
39182189Sraj#include <string.h>
40176774Sraj#include <unistd.h>
41176774Sraj
42176774Sraj#include "nvmecontrol.h"
43176774Sraj
44176774Srajstatic void
45182189Srajdevlist_usage(void)
46176774Sraj{
47176774Sraj	fprintf(stderr, "usage:\n");
48182189Sraj	fprintf(stderr, DEVLIST_USAGE);
49182189Sraj	exit(1);
50176774Sraj}
51182189Sraj
52176774Srajstatic inline uint32_t
53176774Srajns_get_sector_size(struct nvme_namespace_data *nsdata)
54182189Sraj{
55182189Sraj
56176774Sraj	return (1 << nsdata->lbaf[nsdata->flbas.format].lbads);
57188711Sraj}
58188711Sraj
59188711Srajvoid
60188711Srajdevlist(int argc, char *argv[])
61176774Sraj{
62176774Sraj	struct nvme_controller_data	cdata;
63176774Sraj	struct nvme_namespace_data	nsdata;
64176774Sraj	char				name[64];
65176774Sraj	uint8_t				mn[64];
66176774Sraj	uint32_t			i;
67176774Sraj	int				ch, ctrlr, fd, found, ret;
68176774Sraj
69182189Sraj	while ((ch = getopt(argc, argv, "")) != -1) {
70182189Sraj		switch ((char)ch) {
71182189Sraj		default:
72182189Sraj			devlist_usage();
73188711Sraj		}
74188711Sraj	}
75176774Sraj
76182189Sraj	ctrlr = -1;
77176774Sraj	found = 0;
78176774Sraj
79176774Sraj	while (1) {
80176774Sraj		ctrlr++;
81176774Sraj		sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr);
82176774Sraj
83182189Sraj		ret = open_dev(name, &fd, 0, 0);
84182189Sraj
85182189Sraj		if (ret != 0) {
86182189Sraj			if (ret == EACCES) {
87182189Sraj				warnx("could not open "_PATH_DEV"%s\n", name);
88182189Sraj				continue;
89182189Sraj			} else
90182189Sraj				break;
91182189Sraj		}
92176774Sraj
93182189Sraj		found++;
94182189Sraj		read_controller_data(fd, &cdata);
95188711Sraj		nvme_strvis(mn, cdata.mn, sizeof(mn), NVME_MODEL_NUMBER_LENGTH);
96188711Sraj		printf("%6s: %s\n", name, mn);
97188711Sraj
98188711Sraj		for (i = 0; i < cdata.nn; i++) {
99188711Sraj			sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr,
100193096Sattilio			    NVME_NS_PREFIX, i+1);
101188711Sraj			read_namespace_data(fd, i+1, &nsdata);
102188711Sraj			printf("  %10s (%lldGB)\n",
103188711Sraj				name,
104188711Sraj				nsdata.nsze *
105188711Sraj				(long long)ns_get_sector_size(&nsdata) /
106188711Sraj				1024 / 1024 / 1024);
107188711Sraj		}
108176774Sraj
109182189Sraj		close(fd);
110182189Sraj	}
111182189Sraj
112182189Sraj	if (found == 0)
113176774Sraj		printf("No NVMe controllers found.\n");
114259235Sandreast
115259235Sandreast	exit(1);
116182189Sraj}
117182189Sraj