xref: /spdk/examples/vmd/lsvmd/lsvmd.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 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  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/log.h"
36 #include "spdk/env.h"
37 #include "spdk/vmd.h"
38 
39 struct spdk_pci_addr g_probe_addr;
40 
41 static int
42 parse_args(int argc, char **argv)
43 {
44 	int op;
45 
46 	while ((op = getopt(argc, argv, "r:d")) != -1) {
47 		switch (op) {
48 		case 'r':
49 			if (spdk_pci_addr_parse(&g_probe_addr, optarg)) {
50 				SPDK_ERRLOG("Error parsing PCI address\n");
51 				return 1;
52 			}
53 
54 			break;
55 
56 		case 'd':
57 			spdk_log_set_print_level(SPDK_LOG_DEBUG);
58 			spdk_log_set_flag("vmd");
59 			break;
60 
61 		default:
62 			return 1;
63 		}
64 	}
65 
66 	return 0;
67 }
68 
69 int main(int argc, char **argv)
70 {
71 	struct spdk_env_opts opts;
72 	struct spdk_pci_device *pci_device;
73 	char addr_buf[128];
74 	int rc;
75 
76 	rc = parse_args(argc, argv);
77 	if (rc != 0) {
78 		return rc;
79 	}
80 
81 	spdk_env_opts_init(&opts);
82 	opts.name = "lsvmd";
83 
84 	if (spdk_env_init(&opts) < 0) {
85 		SPDK_ERRLOG("Unable to initialize SPDK env\n");
86 		return 1;
87 	}
88 
89 	rc = spdk_vmd_init();
90 	if (rc) {
91 		SPDK_ERRLOG("No VMD Controllers found\n");
92 	}
93 
94 	for (pci_device = spdk_pci_get_first_device(); pci_device != NULL;
95 	     pci_device = spdk_pci_get_next_device(pci_device)) {
96 		if (strcmp(spdk_pci_device_get_type(pci_device), "vmd") == 0) {
97 			rc = spdk_pci_addr_fmt(addr_buf, sizeof(addr_buf), &pci_device->addr);
98 			if (rc != 0) {
99 				fprintf(stderr, "Failed to format VMD's PCI address\n");
100 				continue;
101 			}
102 
103 			printf("%s\n", addr_buf);
104 		}
105 	}
106 
107 	return rc;
108 }
109