1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
12 #include <sys/queue.h>
15 #include <rte_errno.h>
16 #include <rte_interrupts.h>
20 #include <rte_bus_pci.h>
21 #include <rte_per_lcore.h>
22 #include <rte_memory.h>
24 #include <rte_string_fns.h>
25 #include <rte_common.h>
26 #include <rte_devargs.h>
32 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
34 const char *rte_pci_get_sysfs_path(void)
36 const char *path = NULL;
38 path = getenv("SYSFS_PCI_DEVICES");
40 return SYSFS_PCI_DEVICES;
45 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
47 struct rte_devargs *devargs;
48 struct rte_pci_addr addr;
50 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
51 devargs->bus->parse(devargs->name, &addr);
52 if (!rte_pci_addr_cmp(&dev->addr, &addr))
59 pci_name_set(struct rte_pci_device *dev)
61 struct rte_devargs *devargs;
63 /* Each device has its internal, canonical name set. */
64 rte_pci_device_name(&dev->addr,
65 dev->name, sizeof(dev->name));
66 devargs = pci_devargs_lookup(dev);
67 dev->device.devargs = devargs;
68 /* In blacklist mode, if the device is not blacklisted, no
69 * rte_devargs exists for it.
72 /* If an rte_devargs exists, the generic rte_device uses the
73 * given name as its name.
75 dev->device.name = dev->device.devargs->name;
77 /* Otherwise, it uses the internal, canonical form. */
78 dev->device.name = dev->name;
82 * Match the PCI Driver and Device using the ID Table
85 rte_pci_match(const struct rte_pci_driver *pci_drv,
86 const struct rte_pci_device *pci_dev)
88 const struct rte_pci_id *id_table;
90 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
92 /* check if device's identifiers match the driver's ones */
93 if (id_table->vendor_id != pci_dev->id.vendor_id &&
94 id_table->vendor_id != PCI_ANY_ID)
96 if (id_table->device_id != pci_dev->id.device_id &&
97 id_table->device_id != PCI_ANY_ID)
99 if (id_table->subsystem_vendor_id !=
100 pci_dev->id.subsystem_vendor_id &&
101 id_table->subsystem_vendor_id != PCI_ANY_ID)
103 if (id_table->subsystem_device_id !=
104 pci_dev->id.subsystem_device_id &&
105 id_table->subsystem_device_id != PCI_ANY_ID)
107 if (id_table->class_id != pci_dev->id.class_id &&
108 id_table->class_id != RTE_CLASS_ANY_ID)
118 * If vendor/device ID match, call the probe() function of the
122 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
123 struct rte_pci_device *dev)
127 struct rte_pci_addr *loc;
129 if ((dr == NULL) || (dev == NULL))
134 /* The device is not blacklisted; Check if driver supports it */
135 if (!rte_pci_match(dr, dev))
136 /* Match of device and driver failed */
139 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
140 loc->domain, loc->bus, loc->devid, loc->function,
141 dev->device.numa_node);
143 /* no initialization when blacklisted, return without error */
144 if (dev->device.devargs != NULL &&
145 dev->device.devargs->policy ==
146 RTE_DEV_BLACKLISTED) {
147 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
152 if (dev->device.numa_node < 0) {
153 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
154 dev->device.numa_node = 0;
157 already_probed = rte_dev_is_probed(&dev->device);
158 if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
159 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
164 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
165 dev->id.device_id, dr->driver.name);
168 * reference driver structure
169 * This needs to be before rte_pci_map_device(), as it enables to use
170 * driver flags for adjusting configuration.
172 if (!already_probed) {
173 enum rte_iova_mode dev_iova_mode;
174 enum rte_iova_mode iova_mode;
176 dev_iova_mode = pci_device_iova_mode(dr, dev);
177 iova_mode = rte_eal_iova_mode();
178 if (dev_iova_mode != RTE_IOVA_DC &&
179 dev_iova_mode != iova_mode) {
180 RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
181 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
182 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
189 if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
190 /* map resources for devices that use igb_uio */
191 ret = rte_pci_map_device(dev);
198 /* call the driver probe() function */
199 ret = dr->probe(dr, dev);
201 return ret; /* no rollback if already succeeded earlier */
204 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
205 /* Don't unmap if device is unsupported and
206 * driver needs mapped resources.
209 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
210 rte_pci_unmap_device(dev);
212 dev->device.driver = &dr->driver;
219 * If vendor/device ID match, call the remove() function of the
223 rte_pci_detach_dev(struct rte_pci_device *dev)
225 struct rte_pci_addr *loc;
226 struct rte_pci_driver *dr;
235 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
236 loc->domain, loc->bus, loc->devid,
237 loc->function, dev->device.numa_node);
239 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
240 dev->id.device_id, dr->driver.name);
243 ret = dr->remove(dev);
248 /* clear driver structure */
250 dev->device.driver = NULL;
252 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
253 /* unmap resources for devices that use igb_uio */
254 rte_pci_unmap_device(dev);
260 * If vendor/device ID match, call the probe() function of all
261 * registered driver for the given device. Return < 0 if initialization
262 * failed, return 1 if no driver is found for this device.
265 pci_probe_all_drivers(struct rte_pci_device *dev)
267 struct rte_pci_driver *dr = NULL;
273 FOREACH_DRIVER_ON_PCIBUS(dr) {
274 rc = rte_pci_probe_one_driver(dr, dev);
276 /* negative value is an error */
279 /* positive value means driver doesn't support it */
287 * Scan the content of the PCI bus, and call the probe() function for
288 * all registered drivers that have a matching entry in its id_table
289 * for discovered devices.
294 struct rte_pci_device *dev = NULL;
295 size_t probed = 0, failed = 0;
296 struct rte_devargs *devargs;
300 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
303 FOREACH_DEVICE_ON_PCIBUS(dev) {
306 devargs = dev->device.devargs;
307 /* probe all or only whitelisted devices */
309 ret = pci_probe_all_drivers(dev);
310 else if (devargs != NULL &&
311 devargs->policy == RTE_DEV_WHITELISTED)
312 ret = pci_probe_all_drivers(dev);
314 if (ret != -EEXIST) {
315 RTE_LOG(ERR, EAL, "Requested device "
316 PCI_PRI_FMT " cannot be used\n",
317 dev->addr.domain, dev->addr.bus,
318 dev->addr.devid, dev->addr.function);
326 return (probed && probed == failed) ? -1 : 0;
329 /* dump one device */
331 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
335 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
336 dev->addr.devid, dev->addr.function);
337 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
340 for (i = 0; i != sizeof(dev->mem_resource) /
341 sizeof(dev->mem_resource[0]); i++) {
342 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
343 dev->mem_resource[i].phys_addr,
344 dev->mem_resource[i].len);
349 /* dump devices on the bus */
351 rte_pci_dump(FILE *f)
353 struct rte_pci_device *dev = NULL;
355 FOREACH_DEVICE_ON_PCIBUS(dev) {
356 pci_dump_one_device(f, dev);
361 pci_parse(const char *name, void *addr)
363 struct rte_pci_addr *out = addr;
364 struct rte_pci_addr pci_addr;
367 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
368 if (parse && addr != NULL)
370 return parse == false;
373 /* register a driver */
375 rte_pci_register(struct rte_pci_driver *driver)
377 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
378 driver->bus = &rte_pci_bus;
381 /* unregister a driver */
383 rte_pci_unregister(struct rte_pci_driver *driver)
385 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
389 /* Add a device to PCI bus */
391 rte_pci_add_device(struct rte_pci_device *pci_dev)
393 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
396 /* Insert a device into a predefined position in PCI bus */
398 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
399 struct rte_pci_device *new_pci_dev)
401 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
404 /* Remove a device from PCI bus */
406 rte_pci_remove_device(struct rte_pci_device *pci_dev)
408 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
411 static struct rte_device *
412 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
415 const struct rte_pci_device *pstart;
416 struct rte_pci_device *pdev;
419 pstart = RTE_DEV_TO_PCI_CONST(start);
420 pdev = TAILQ_NEXT(pstart, next);
422 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
424 while (pdev != NULL) {
425 if (cmp(&pdev->device, data) == 0)
426 return &pdev->device;
427 pdev = TAILQ_NEXT(pdev, next);
433 * find the device which encounter the failure, by iterate over all device on
434 * PCI bus to check if the memory failure address is located in the range
435 * of the BARs of the device.
437 static struct rte_pci_device *
438 pci_find_device_by_addr(const void *failure_addr)
440 struct rte_pci_device *pdev = NULL;
441 uint64_t check_point, start, end, len;
444 check_point = (uint64_t)(uintptr_t)failure_addr;
446 FOREACH_DEVICE_ON_PCIBUS(pdev) {
447 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
448 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
449 len = pdev->mem_resource[i].len;
451 if (check_point >= start && check_point < end) {
452 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
453 PRIx64" belongs to device %s!\n",
454 check_point, pdev->device.name);
463 pci_hot_unplug_handler(struct rte_device *dev)
465 struct rte_pci_device *pdev = NULL;
468 pdev = RTE_DEV_TO_PCI(dev);
472 switch (pdev->kdrv) {
473 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
476 * vfio kernel module guaranty the pci device would not be
477 * deleted until the user space release the resource, so no
478 * need to remap BARs resource here, just directly notify
479 * the req event to the user space to handle it.
481 rte_dev_event_callback_process(dev->name,
482 RTE_DEV_EVENT_REMOVE);
485 case RTE_KDRV_IGB_UIO:
486 case RTE_KDRV_UIO_GENERIC:
487 case RTE_KDRV_NIC_UIO:
488 /* BARs resource is invalid, remap it to be safe. */
489 ret = pci_uio_remap_resource(pdev);
493 "Not managed by a supported kernel driver, skipped\n");
502 pci_sigbus_handler(const void *failure_addr)
504 struct rte_pci_device *pdev = NULL;
507 pdev = pci_find_device_by_addr(failure_addr);
509 /* It is a generic sigbus error, no bus would handle it. */
512 /* The sigbus error is caused of hot-unplug. */
513 ret = pci_hot_unplug_handler(&pdev->device);
516 "Failed to handle hot-unplug for device %s",
525 pci_plug(struct rte_device *dev)
527 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
531 pci_unplug(struct rte_device *dev)
533 struct rte_pci_device *pdev;
536 pdev = RTE_DEV_TO_PCI(dev);
537 ret = rte_pci_detach_dev(pdev);
539 rte_pci_remove_device(pdev);
540 rte_devargs_remove(dev->devargs);
547 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
549 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
551 if (!pdev || !pdev->driver) {
555 if (pdev->driver->dma_map)
556 return pdev->driver->dma_map(pdev, addr, iova, len);
558 * In case driver don't provides any specific mapping
559 * try fallback to VFIO.
561 if (pdev->kdrv == RTE_KDRV_VFIO)
562 return rte_vfio_container_dma_map
563 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
570 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
572 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
574 if (!pdev || !pdev->driver) {
578 if (pdev->driver->dma_unmap)
579 return pdev->driver->dma_unmap(pdev, addr, iova, len);
581 * In case driver don't provides any specific mapping
582 * try fallback to VFIO.
584 if (pdev->kdrv == RTE_KDRV_VFIO)
585 return rte_vfio_container_dma_unmap
586 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
593 pci_ignore_device(const struct rte_pci_device *dev)
595 struct rte_devargs *devargs = dev->device.devargs;
597 switch (rte_pci_bus.bus.conf.scan_mode) {
598 case RTE_BUS_SCAN_WHITELIST:
599 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
602 case RTE_BUS_SCAN_UNDEFINED:
603 case RTE_BUS_SCAN_BLACKLIST:
604 if (devargs == NULL ||
605 devargs->policy != RTE_DEV_BLACKLISTED)
613 rte_pci_get_iommu_class(void)
615 enum rte_iova_mode iova_mode = RTE_IOVA_DC;
616 const struct rte_pci_device *dev;
617 const struct rte_pci_driver *drv;
618 bool devices_want_va = false;
619 bool devices_want_pa = false;
620 int iommu_no_va = -1;
622 FOREACH_DEVICE_ON_PCIBUS(dev) {
624 * We can check this only once, because the IOMMU hardware is
625 * the same for all of them.
627 if (iommu_no_va == -1)
628 iommu_no_va = pci_device_iommu_support_va(dev)
630 if (pci_ignore_device(dev))
632 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
633 dev->kdrv == RTE_KDRV_NONE)
635 FOREACH_DRIVER_ON_PCIBUS(drv) {
636 enum rte_iova_mode dev_iova_mode;
638 if (!rte_pci_match(drv, dev))
641 dev_iova_mode = pci_device_iova_mode(drv, dev);
642 RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
643 PCI_PRI_FMT " wants IOVA as '%s'\n",
645 dev->addr.domain, dev->addr.bus,
646 dev->addr.devid, dev->addr.function,
647 dev_iova_mode == RTE_IOVA_DC ? "DC" :
648 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
649 if (dev_iova_mode == RTE_IOVA_PA)
650 devices_want_pa = true;
651 else if (dev_iova_mode == RTE_IOVA_VA)
652 devices_want_va = true;
655 if (iommu_no_va == 1) {
656 iova_mode = RTE_IOVA_PA;
657 if (devices_want_va) {
658 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
659 RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
661 } else if (devices_want_va && !devices_want_pa) {
662 iova_mode = RTE_IOVA_VA;
663 } else if (devices_want_pa && !devices_want_va) {
664 iova_mode = RTE_IOVA_PA;
666 iova_mode = RTE_IOVA_DC;
667 if (devices_want_va) {
668 RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
669 RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
675 struct rte_pci_bus rte_pci_bus = {
677 .scan = rte_pci_scan,
678 .probe = rte_pci_probe,
679 .find_device = pci_find_device,
681 .unplug = pci_unplug,
683 .dma_map = pci_dma_map,
684 .dma_unmap = pci_dma_unmap,
685 .get_iommu_class = rte_pci_get_iommu_class,
686 .dev_iterate = rte_pci_dev_iterate,
687 .hot_unplug_handler = pci_hot_unplug_handler,
688 .sigbus_handler = pci_sigbus_handler,
690 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
691 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
694 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);