bus: add helper to find bus by name
[dpdk.git] / lib / librte_eal / common / eal_common_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 NXP
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 NXP 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 <stdio.h>
35 #include <string.h>
36 #include <sys/queue.h>
37
38 #include <rte_bus.h>
39
40 #include "eal_private.h"
41
42 struct rte_bus_list rte_bus_list =
43         TAILQ_HEAD_INITIALIZER(rte_bus_list);
44
45 void
46 rte_bus_register(struct rte_bus *bus)
47 {
48         RTE_VERIFY(bus);
49         RTE_VERIFY(bus->name && strlen(bus->name));
50         /* A bus should mandatorily have the scan implemented */
51         RTE_VERIFY(bus->scan);
52         RTE_VERIFY(bus->probe);
53         RTE_VERIFY(bus->find_device);
54
55         TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
56         RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
57 }
58
59 void
60 rte_bus_unregister(struct rte_bus *bus)
61 {
62         TAILQ_REMOVE(&rte_bus_list, bus, next);
63         RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
64 }
65
66 /* Scan all the buses for registered devices */
67 int
68 rte_bus_scan(void)
69 {
70         int ret;
71         struct rte_bus *bus = NULL;
72
73         TAILQ_FOREACH(bus, &rte_bus_list, next) {
74                 ret = bus->scan();
75                 if (ret) {
76                         RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
77                                 bus->name);
78                         return ret;
79                 }
80         }
81
82         return 0;
83 }
84
85 /* Probe all devices of all buses */
86 int
87 rte_bus_probe(void)
88 {
89         int ret;
90         struct rte_bus *bus, *vbus = NULL;
91
92         TAILQ_FOREACH(bus, &rte_bus_list, next) {
93                 if (!strcmp(bus->name, "virtual")) {
94                         vbus = bus;
95                         continue;
96                 }
97
98                 ret = bus->probe();
99                 if (ret) {
100                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
101                                 bus->name);
102                         return ret;
103                 }
104         }
105
106         if (vbus) {
107                 ret = vbus->probe();
108                 if (ret) {
109                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
110                                 vbus->name);
111                         return ret;
112                 }
113         }
114
115         return 0;
116 }
117
118 /* Dump information of a single bus */
119 static int
120 bus_dump_one(FILE *f, struct rte_bus *bus)
121 {
122         int ret;
123
124         /* For now, dump only the bus name */
125         ret = fprintf(f, " %s\n", bus->name);
126
127         /* Error in case of inability in writing to stream */
128         if (ret < 0)
129                 return ret;
130
131         return 0;
132 }
133
134 void
135 rte_bus_dump(FILE *f)
136 {
137         int ret;
138         struct rte_bus *bus;
139
140         TAILQ_FOREACH(bus, &rte_bus_list, next) {
141                 ret = bus_dump_one(f, bus);
142                 if (ret) {
143                         RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
144                                 ret);
145                         break;
146                 }
147         }
148 }
149
150 struct rte_bus *
151 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
152              const void *data)
153 {
154         struct rte_bus *bus = NULL;
155         bool start_found = !start;
156
157         TAILQ_FOREACH(bus, &rte_bus_list, next) {
158                 if (!start_found) {
159                         if (bus == start)
160                                 start_found = 1;
161                         continue;
162                 }
163                 if (cmp(bus, data) == 0)
164                         break;
165         }
166         return bus;
167 }
168
169 static int
170 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
171 {
172         const struct rte_device *dev2 = _dev2;
173
174         return dev1 != dev2;
175 }
176
177 static int
178 bus_find_device(const struct rte_bus *bus, const void *_dev)
179 {
180         struct rte_device *dev;
181
182         dev = bus->find_device(NULL, cmp_rte_device, _dev);
183         return dev == NULL;
184 }
185
186 struct rte_bus *
187 rte_bus_find_by_device(const struct rte_device *dev)
188 {
189         return rte_bus_find(NULL, bus_find_device, (const void *)dev);
190 }
191
192 static int
193 cmp_bus_name(const struct rte_bus *bus, const void *_name)
194 {
195         const char *name = _name;
196
197         return strcmp(bus->name, name);
198 }
199
200 struct rte_bus *
201 rte_bus_find_by_name(const char *busname)
202 {
203         return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
204 }