1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
11 #include <sys/queue.h>
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
19 #include <rte_bus_pci.h>
20 #include <rte_per_lcore.h>
21 #include <rte_memory.h>
23 #include <rte_string_fns.h>
24 #include <rte_common.h>
25 #include <rte_devargs.h>
29 extern struct rte_pci_bus rte_pci_bus;
31 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
33 const char *rte_pci_get_sysfs_path(void)
35 const char *path = NULL;
37 path = getenv("SYSFS_PCI_DEVICES");
39 return SYSFS_PCI_DEVICES;
44 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
46 struct rte_devargs *devargs;
47 struct rte_pci_addr addr;
50 pbus = rte_bus_find_by_name("pci");
51 TAILQ_FOREACH(devargs, &devargs_list, next) {
52 if (devargs->bus != pbus)
54 devargs->bus->parse(devargs->name, &addr);
55 if (!rte_pci_addr_cmp(&dev->addr, &addr))
62 pci_name_set(struct rte_pci_device *dev)
64 struct rte_devargs *devargs;
66 /* Each device has its internal, canonical name set. */
67 rte_pci_device_name(&dev->addr,
68 dev->name, sizeof(dev->name));
69 devargs = pci_devargs_lookup(dev);
70 dev->device.devargs = devargs;
71 /* In blacklist mode, if the device is not blacklisted, no
72 * rte_devargs exists for it.
75 /* If an rte_devargs exists, the generic rte_device uses the
76 * given name as its namea
78 dev->device.name = dev->device.devargs->name;
80 /* Otherwise, it uses the internal, canonical form. */
81 dev->device.name = dev->name;
85 * Match the PCI Driver and Device using the ID Table
88 rte_pci_match(const struct rte_pci_driver *pci_drv,
89 const struct rte_pci_device *pci_dev)
91 const struct rte_pci_id *id_table;
93 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
95 /* check if device's identifiers match the driver's ones */
96 if (id_table->vendor_id != pci_dev->id.vendor_id &&
97 id_table->vendor_id != PCI_ANY_ID)
99 if (id_table->device_id != pci_dev->id.device_id &&
100 id_table->device_id != PCI_ANY_ID)
102 if (id_table->subsystem_vendor_id !=
103 pci_dev->id.subsystem_vendor_id &&
104 id_table->subsystem_vendor_id != PCI_ANY_ID)
106 if (id_table->subsystem_device_id !=
107 pci_dev->id.subsystem_device_id &&
108 id_table->subsystem_device_id != PCI_ANY_ID)
110 if (id_table->class_id != pci_dev->id.class_id &&
111 id_table->class_id != RTE_CLASS_ANY_ID)
121 * If vendor/device ID match, call the probe() function of the
125 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
126 struct rte_pci_device *dev)
129 struct rte_pci_addr *loc;
131 if ((dr == NULL) || (dev == NULL))
136 /* The device is not blacklisted; Check if driver supports it */
137 if (!rte_pci_match(dr, dev))
138 /* Match of device and driver failed */
141 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
142 loc->domain, loc->bus, loc->devid, loc->function,
143 dev->device.numa_node);
145 /* no initialization when blacklisted, return without error */
146 if (dev->device.devargs != NULL &&
147 dev->device.devargs->policy ==
148 RTE_DEV_BLACKLISTED) {
149 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
154 if (dev->device.numa_node < 0) {
155 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
156 dev->device.numa_node = 0;
159 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
160 dev->id.device_id, dr->driver.name);
162 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
163 /* map resources for devices that use igb_uio */
164 ret = rte_pci_map_device(dev);
169 /* reference driver structure */
171 dev->device.driver = &dr->driver;
173 /* call the driver probe() function */
174 ret = dr->probe(dr, dev);
177 dev->device.driver = NULL;
178 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
179 /* Don't unmap if device is unsupported and
180 * driver needs mapped resources.
183 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
184 rte_pci_unmap_device(dev);
191 * If vendor/device ID match, call the remove() function of the
195 rte_pci_detach_dev(struct rte_pci_device *dev)
197 struct rte_pci_addr *loc;
198 struct rte_pci_driver *dr;
207 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
208 loc->domain, loc->bus, loc->devid,
209 loc->function, dev->device.numa_node);
211 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
212 dev->id.device_id, dr->driver.name);
215 ret = dr->remove(dev);
220 /* clear driver structure */
223 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
224 /* unmap resources for devices that use igb_uio */
225 rte_pci_unmap_device(dev);
231 * If vendor/device ID match, call the probe() function of all
232 * registered driver for the given device. Return -1 if initialization
233 * failed, return 1 if no driver is found for this device.
236 pci_probe_all_drivers(struct rte_pci_device *dev)
238 struct rte_pci_driver *dr = NULL;
244 /* Check if a driver is already loaded */
245 if (dev->driver != NULL)
248 FOREACH_DRIVER_ON_PCIBUS(dr) {
249 rc = rte_pci_probe_one_driver(dr, dev);
251 /* negative value is an error */
254 /* positive value means driver doesn't support it */
262 * Find the pci device specified by pci address, then invoke probe function of
263 * the driver of the device.
266 rte_pci_probe_one(const struct rte_pci_addr *addr)
268 struct rte_pci_device *dev = NULL;
275 /* update current pci device in global list, kernel bindings might have
276 * changed since last time we looked at it.
278 if (pci_update_device(addr) < 0)
281 FOREACH_DEVICE_ON_PCIBUS(dev) {
282 if (rte_pci_addr_cmp(&dev->addr, addr))
285 ret = pci_probe_all_drivers(dev);
293 RTE_LOG(WARNING, EAL,
294 "Requested device " PCI_PRI_FMT " cannot be used\n",
295 addr->domain, addr->bus, addr->devid, addr->function);
300 * Detach device specified by its pci address.
303 rte_pci_detach(const struct rte_pci_addr *addr)
305 struct rte_pci_device *dev = NULL;
311 FOREACH_DEVICE_ON_PCIBUS(dev) {
312 if (rte_pci_addr_cmp(&dev->addr, addr))
315 ret = rte_pci_detach_dev(dev);
317 /* negative value is an error */
320 /* positive value means driver doesn't support it */
323 rte_pci_remove_device(dev);
330 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
331 " cannot be used\n", dev->addr.domain, dev->addr.bus,
332 dev->addr.devid, dev->addr.function);
337 * Scan the content of the PCI bus, and call the probe() function for
338 * all registered drivers that have a matching entry in its id_table
339 * for discovered devices.
344 struct rte_pci_device *dev = NULL;
345 size_t probed = 0, failed = 0;
346 struct rte_devargs *devargs;
350 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
353 FOREACH_DEVICE_ON_PCIBUS(dev) {
356 devargs = dev->device.devargs;
357 /* probe all or only whitelisted devices */
359 ret = pci_probe_all_drivers(dev);
360 else if (devargs != NULL &&
361 devargs->policy == RTE_DEV_WHITELISTED)
362 ret = pci_probe_all_drivers(dev);
364 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
365 " cannot be used\n", dev->addr.domain, dev->addr.bus,
366 dev->addr.devid, dev->addr.function);
373 return (probed && probed == failed) ? -1 : 0;
376 /* dump one device */
378 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
382 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
383 dev->addr.devid, dev->addr.function);
384 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
387 for (i = 0; i != sizeof(dev->mem_resource) /
388 sizeof(dev->mem_resource[0]); i++) {
389 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
390 dev->mem_resource[i].phys_addr,
391 dev->mem_resource[i].len);
396 /* dump devices on the bus */
398 rte_pci_dump(FILE *f)
400 struct rte_pci_device *dev = NULL;
402 FOREACH_DEVICE_ON_PCIBUS(dev) {
403 pci_dump_one_device(f, dev);
408 pci_parse(const char *name, void *addr)
410 struct rte_pci_addr *out = addr;
411 struct rte_pci_addr pci_addr;
414 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
415 if (parse && addr != NULL)
417 return parse == false;
420 /* register a driver */
422 rte_pci_register(struct rte_pci_driver *driver)
424 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
425 driver->bus = &rte_pci_bus;
428 /* unregister a driver */
430 rte_pci_unregister(struct rte_pci_driver *driver)
432 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
436 /* Add a device to PCI bus */
438 rte_pci_add_device(struct rte_pci_device *pci_dev)
440 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
443 /* Insert a device into a predefined position in PCI bus */
445 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
446 struct rte_pci_device *new_pci_dev)
448 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
451 /* Remove a device from PCI bus */
453 rte_pci_remove_device(struct rte_pci_device *pci_dev)
455 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
458 static struct rte_device *
459 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
462 struct rte_pci_device *dev;
464 FOREACH_DEVICE_ON_PCIBUS(dev) {
465 if (start && &dev->device == start) {
466 start = NULL; /* starting point found */
469 if (cmp(&dev->device, data) == 0)
477 pci_plug(struct rte_device *dev)
479 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
483 pci_unplug(struct rte_device *dev)
485 struct rte_pci_device *pdev;
488 pdev = RTE_DEV_TO_PCI(dev);
489 ret = rte_pci_detach_dev(pdev);
491 rte_pci_remove_device(pdev);
497 struct rte_pci_bus rte_pci_bus = {
499 .scan = rte_pci_scan,
500 .probe = rte_pci_probe,
501 .find_device = pci_find_device,
503 .unplug = pci_unplug,
505 .get_iommu_class = rte_pci_get_iommu_class,
507 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
508 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
511 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);