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>
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;
49 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
50 devargs->bus->parse(devargs->name, &addr);
51 if (!rte_pci_addr_cmp(&dev->addr, &addr))
58 pci_name_set(struct rte_pci_device *dev)
60 struct rte_devargs *devargs;
62 /* Each device has its internal, canonical name set. */
63 rte_pci_device_name(&dev->addr,
64 dev->name, sizeof(dev->name));
65 devargs = pci_devargs_lookup(dev);
66 dev->device.devargs = devargs;
67 /* In blacklist mode, if the device is not blacklisted, no
68 * rte_devargs exists for it.
71 /* If an rte_devargs exists, the generic rte_device uses the
72 * given name as its name.
74 dev->device.name = dev->device.devargs->name;
76 /* Otherwise, it uses the internal, canonical form. */
77 dev->device.name = dev->name;
81 * Match the PCI Driver and Device using the ID Table
84 rte_pci_match(const struct rte_pci_driver *pci_drv,
85 const struct rte_pci_device *pci_dev)
87 const struct rte_pci_id *id_table;
89 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
91 /* check if device's identifiers match the driver's ones */
92 if (id_table->vendor_id != pci_dev->id.vendor_id &&
93 id_table->vendor_id != PCI_ANY_ID)
95 if (id_table->device_id != pci_dev->id.device_id &&
96 id_table->device_id != PCI_ANY_ID)
98 if (id_table->subsystem_vendor_id !=
99 pci_dev->id.subsystem_vendor_id &&
100 id_table->subsystem_vendor_id != PCI_ANY_ID)
102 if (id_table->subsystem_device_id !=
103 pci_dev->id.subsystem_device_id &&
104 id_table->subsystem_device_id != PCI_ANY_ID)
106 if (id_table->class_id != pci_dev->id.class_id &&
107 id_table->class_id != RTE_CLASS_ANY_ID)
117 * If vendor/device ID match, call the probe() function of the
121 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
122 struct rte_pci_device *dev)
125 struct rte_pci_addr *loc;
127 if ((dr == NULL) || (dev == NULL))
132 /* The device is not blacklisted; Check if driver supports it */
133 if (!rte_pci_match(dr, dev))
134 /* Match of device and driver failed */
137 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
138 loc->domain, loc->bus, loc->devid, loc->function,
139 dev->device.numa_node);
141 /* no initialization when blacklisted, return without error */
142 if (dev->device.devargs != NULL &&
143 dev->device.devargs->policy ==
144 RTE_DEV_BLACKLISTED) {
145 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
150 if (dev->device.numa_node < 0) {
151 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
152 dev->device.numa_node = 0;
155 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
156 dev->id.device_id, dr->driver.name);
159 * reference driver structure
160 * This needs to be before rte_pci_map_device(), as it enables to use
161 * driver flags for adjusting configuration.
165 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
166 /* map resources for devices that use igb_uio */
167 ret = rte_pci_map_device(dev);
174 /* call the driver probe() function */
175 ret = dr->probe(dr, dev);
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);
186 dev->device.driver = &dr->driver;
193 * If vendor/device ID match, call the remove() function of the
197 rte_pci_detach_dev(struct rte_pci_device *dev)
199 struct rte_pci_addr *loc;
200 struct rte_pci_driver *dr;
209 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
210 loc->domain, loc->bus, loc->devid,
211 loc->function, dev->device.numa_node);
213 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
214 dev->id.device_id, dr->driver.name);
217 ret = dr->remove(dev);
222 /* clear driver structure */
225 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
226 /* unmap resources for devices that use igb_uio */
227 rte_pci_unmap_device(dev);
233 * If vendor/device ID match, call the probe() function of all
234 * registered driver for the given device. Return -1 if initialization
235 * failed, return 1 if no driver is found for this device.
238 pci_probe_all_drivers(struct rte_pci_device *dev)
240 struct rte_pci_driver *dr = NULL;
246 /* Check if a driver is already loaded */
247 if (dev->device.driver != NULL)
250 FOREACH_DRIVER_ON_PCIBUS(dr) {
251 rc = rte_pci_probe_one_driver(dr, dev);
253 /* negative value is an error */
256 /* positive value means driver doesn't support it */
264 * Scan the content of the PCI bus, and call the probe() function for
265 * all registered drivers that have a matching entry in its id_table
266 * for discovered devices.
271 struct rte_pci_device *dev = NULL;
272 size_t probed = 0, failed = 0;
273 struct rte_devargs *devargs;
277 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
280 FOREACH_DEVICE_ON_PCIBUS(dev) {
283 devargs = dev->device.devargs;
284 /* probe all or only whitelisted devices */
286 ret = pci_probe_all_drivers(dev);
287 else if (devargs != NULL &&
288 devargs->policy == RTE_DEV_WHITELISTED)
289 ret = pci_probe_all_drivers(dev);
291 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
292 " cannot be used\n", dev->addr.domain, dev->addr.bus,
293 dev->addr.devid, dev->addr.function);
300 return (probed && probed == failed) ? -1 : 0;
303 /* dump one device */
305 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
309 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
310 dev->addr.devid, dev->addr.function);
311 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
314 for (i = 0; i != sizeof(dev->mem_resource) /
315 sizeof(dev->mem_resource[0]); i++) {
316 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
317 dev->mem_resource[i].phys_addr,
318 dev->mem_resource[i].len);
323 /* dump devices on the bus */
325 rte_pci_dump(FILE *f)
327 struct rte_pci_device *dev = NULL;
329 FOREACH_DEVICE_ON_PCIBUS(dev) {
330 pci_dump_one_device(f, dev);
335 pci_parse(const char *name, void *addr)
337 struct rte_pci_addr *out = addr;
338 struct rte_pci_addr pci_addr;
341 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
342 if (parse && addr != NULL)
344 return parse == false;
347 /* register a driver */
349 rte_pci_register(struct rte_pci_driver *driver)
351 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
352 driver->bus = &rte_pci_bus;
355 /* unregister a driver */
357 rte_pci_unregister(struct rte_pci_driver *driver)
359 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
363 /* Add a device to PCI bus */
365 rte_pci_add_device(struct rte_pci_device *pci_dev)
367 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
370 /* Insert a device into a predefined position in PCI bus */
372 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
373 struct rte_pci_device *new_pci_dev)
375 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
378 /* Remove a device from PCI bus */
380 rte_pci_remove_device(struct rte_pci_device *pci_dev)
382 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
385 static struct rte_device *
386 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
389 const struct rte_pci_device *pstart;
390 struct rte_pci_device *pdev;
393 pstart = RTE_DEV_TO_PCI_CONST(start);
394 pdev = TAILQ_NEXT(pstart, next);
396 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
398 while (pdev != NULL) {
399 if (cmp(&pdev->device, data) == 0)
400 return &pdev->device;
401 pdev = TAILQ_NEXT(pdev, next);
407 * find the device which encounter the failure, by iterate over all device on
408 * PCI bus to check if the memory failure address is located in the range
409 * of the BARs of the device.
411 static struct rte_pci_device *
412 pci_find_device_by_addr(const void *failure_addr)
414 struct rte_pci_device *pdev = NULL;
415 uint64_t check_point, start, end, len;
418 check_point = (uint64_t)(uintptr_t)failure_addr;
420 FOREACH_DEVICE_ON_PCIBUS(pdev) {
421 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
422 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
423 len = pdev->mem_resource[i].len;
425 if (check_point >= start && check_point < end) {
426 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
427 PRIx64" belongs to device %s!\n",
428 check_point, pdev->device.name);
437 pci_hot_unplug_handler(struct rte_device *dev)
439 struct rte_pci_device *pdev = NULL;
442 pdev = RTE_DEV_TO_PCI(dev);
446 switch (pdev->kdrv) {
447 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
450 * vfio kernel module guaranty the pci device would not be
451 * deleted until the user space release the resource, so no
452 * need to remap BARs resource here, just directly notify
453 * the req event to the user space to handle it.
455 rte_dev_event_callback_process(dev->name,
456 RTE_DEV_EVENT_REMOVE);
459 case RTE_KDRV_IGB_UIO:
460 case RTE_KDRV_UIO_GENERIC:
461 case RTE_KDRV_NIC_UIO:
462 /* BARs resource is invalid, remap it to be safe. */
463 ret = pci_uio_remap_resource(pdev);
467 "Not managed by a supported kernel driver, skipped\n");
476 pci_sigbus_handler(const void *failure_addr)
478 struct rte_pci_device *pdev = NULL;
481 pdev = pci_find_device_by_addr(failure_addr);
483 /* It is a generic sigbus error, no bus would handle it. */
486 /* The sigbus error is caused of hot-unplug. */
487 ret = pci_hot_unplug_handler(&pdev->device);
490 "Failed to handle hot-unplug for device %s",
499 pci_plug(struct rte_device *dev)
501 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
505 pci_unplug(struct rte_device *dev)
507 struct rte_pci_device *pdev;
510 pdev = RTE_DEV_TO_PCI(dev);
511 ret = rte_pci_detach_dev(pdev);
513 rte_pci_remove_device(pdev);
519 struct rte_pci_bus rte_pci_bus = {
521 .scan = rte_pci_scan,
522 .probe = rte_pci_probe,
523 .find_device = pci_find_device,
525 .unplug = pci_unplug,
527 .get_iommu_class = rte_pci_get_iommu_class,
528 .dev_iterate = rte_pci_dev_iterate,
529 .hot_unplug_handler = pci_hot_unplug_handler,
530 .sigbus_handler = pci_sigbus_handler,
532 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
533 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
536 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);