1 /* SPDX-License-Identifier: BSD-3-Clause
10 #include <rte_debug.h>
11 #include <rte_string_fns.h>
12 #include <rte_errno.h>
14 #include "eal_private.h"
16 static struct rte_bus_list rte_bus_list =
17 TAILQ_HEAD_INITIALIZER(rte_bus_list);
20 rte_bus_register(struct rte_bus *bus)
23 RTE_VERIFY(bus->name && strlen(bus->name));
24 /* A bus should mandatorily have the scan implemented */
25 RTE_VERIFY(bus->scan);
26 RTE_VERIFY(bus->probe);
27 RTE_VERIFY(bus->find_device);
28 /* Buses supporting driver plug also require unplug. */
29 RTE_VERIFY(!bus->plug || bus->unplug);
31 TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
32 RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
36 rte_bus_unregister(struct rte_bus *bus)
38 TAILQ_REMOVE(&rte_bus_list, bus, next);
39 RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
42 /* Scan all the buses for registered devices */
47 struct rte_bus *bus = NULL;
49 TAILQ_FOREACH(bus, &rte_bus_list, next) {
52 RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
59 /* Probe all devices of all buses */
64 struct rte_bus *bus, *vbus = NULL;
66 TAILQ_FOREACH(bus, &rte_bus_list, next) {
67 if (!strcmp(bus->name, "vdev")) {
74 RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
81 RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
88 /* Dump information of a single bus */
90 bus_dump_one(FILE *f, struct rte_bus *bus)
94 /* For now, dump only the bus name */
95 ret = fprintf(f, " %s\n", bus->name);
97 /* Error in case of inability in writing to stream */
105 rte_bus_dump(FILE *f)
110 TAILQ_FOREACH(bus, &rte_bus_list, next) {
111 ret = bus_dump_one(f, bus);
113 RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
121 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
127 bus = TAILQ_NEXT(start, next);
129 bus = TAILQ_FIRST(&rte_bus_list);
130 while (bus != NULL) {
131 if (cmp(bus, data) == 0)
133 bus = TAILQ_NEXT(bus, next);
139 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
141 const struct rte_device *dev2 = _dev2;
147 bus_find_device(const struct rte_bus *bus, const void *_dev)
149 struct rte_device *dev;
151 dev = bus->find_device(NULL, cmp_rte_device, _dev);
156 rte_bus_find_by_device(const struct rte_device *dev)
158 return rte_bus_find(NULL, bus_find_device, (const void *)dev);
162 cmp_bus_name(const struct rte_bus *bus, const void *_name)
164 const char *name = _name;
166 return strcmp(bus->name, name);
170 rte_bus_find_by_name(const char *busname)
172 return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
176 bus_can_parse(const struct rte_bus *bus, const void *_name)
178 const char *name = _name;
180 return !(bus->parse && bus->parse(name, NULL) == 0);
184 rte_bus_find_by_device_name(const char *str)
186 char name[RTE_DEV_NAME_MAX_LEN];
189 strlcpy(name, str, sizeof(name));
190 c = strchr(name, ',');
193 return rte_bus_find(NULL, bus_can_parse, name);
198 * Get iommu class of devices on the bus.
201 rte_bus_get_iommu_class(void)
203 enum rte_iova_mode mode = RTE_IOVA_DC;
204 bool buses_want_va = false;
205 bool buses_want_pa = false;
208 TAILQ_FOREACH(bus, &rte_bus_list, next) {
209 enum rte_iova_mode bus_iova_mode;
211 if (bus->get_iommu_class == NULL)
214 bus_iova_mode = bus->get_iommu_class();
215 RTE_LOG(DEBUG, EAL, "Bus %s wants IOVA as '%s'\n",
217 bus_iova_mode == RTE_IOVA_DC ? "DC" :
218 (bus_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
219 if (bus_iova_mode == RTE_IOVA_PA)
220 buses_want_pa = true;
221 else if (bus_iova_mode == RTE_IOVA_VA)
222 buses_want_va = true;
224 if (buses_want_va && !buses_want_pa) {
226 } else if (buses_want_pa && !buses_want_va) {
231 RTE_LOG(WARNING, EAL, "Some buses want 'VA' but forcing 'DC' because other buses want 'PA'.\n");
232 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all buses may be able to initialize.\n");
240 bus_handle_sigbus(const struct rte_bus *bus,
241 const void *failure_addr)
245 if (!bus->sigbus_handler)
248 ret = bus->sigbus_handler(failure_addr);
250 /* find bus but handle failed, keep the errno be set. */
251 if (ret < 0 && rte_errno == 0)
258 rte_bus_sigbus_handler(const void *failure_addr)
263 int old_errno = rte_errno;
267 bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
268 /* can not find bus. */
271 /* find bus but handle failed, pass on the new errno. */
272 else if (rte_errno != 0)
275 /* restore the old errno. */
276 rte_errno = old_errno;