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.
164 dev->device.driver = &dr->driver;
166 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
167 /* map resources for devices that use igb_uio */
168 ret = rte_pci_map_device(dev);
171 dev->device.driver = NULL;
176 /* call the driver probe() function */
177 ret = dr->probe(dr, dev);
180 dev->device.driver = NULL;
181 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
182 /* Don't unmap if device is unsupported and
183 * driver needs mapped resources.
186 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
187 rte_pci_unmap_device(dev);
194 * If vendor/device ID match, call the remove() function of the
198 rte_pci_detach_dev(struct rte_pci_device *dev)
200 struct rte_pci_addr *loc;
201 struct rte_pci_driver *dr;
210 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
211 loc->domain, loc->bus, loc->devid,
212 loc->function, dev->device.numa_node);
214 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
215 dev->id.device_id, dr->driver.name);
218 ret = dr->remove(dev);
223 /* clear driver structure */
226 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
227 /* unmap resources for devices that use igb_uio */
228 rte_pci_unmap_device(dev);
234 * If vendor/device ID match, call the probe() function of all
235 * registered driver for the given device. Return -1 if initialization
236 * failed, return 1 if no driver is found for this device.
239 pci_probe_all_drivers(struct rte_pci_device *dev)
241 struct rte_pci_driver *dr = NULL;
247 /* Check if a driver is already loaded */
248 if (dev->driver != NULL)
251 FOREACH_DRIVER_ON_PCIBUS(dr) {
252 rc = rte_pci_probe_one_driver(dr, dev);
254 /* negative value is an error */
257 /* positive value means driver doesn't support it */
265 * Scan the content of the PCI bus, and call the probe() function for
266 * all registered drivers that have a matching entry in its id_table
267 * for discovered devices.
272 struct rte_pci_device *dev = NULL;
273 size_t probed = 0, failed = 0;
274 struct rte_devargs *devargs;
278 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
281 FOREACH_DEVICE_ON_PCIBUS(dev) {
284 devargs = dev->device.devargs;
285 /* probe all or only whitelisted devices */
287 ret = pci_probe_all_drivers(dev);
288 else if (devargs != NULL &&
289 devargs->policy == RTE_DEV_WHITELISTED)
290 ret = pci_probe_all_drivers(dev);
292 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
293 " cannot be used\n", dev->addr.domain, dev->addr.bus,
294 dev->addr.devid, dev->addr.function);
301 return (probed && probed == failed) ? -1 : 0;
304 /* dump one device */
306 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
310 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
311 dev->addr.devid, dev->addr.function);
312 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
315 for (i = 0; i != sizeof(dev->mem_resource) /
316 sizeof(dev->mem_resource[0]); i++) {
317 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
318 dev->mem_resource[i].phys_addr,
319 dev->mem_resource[i].len);
324 /* dump devices on the bus */
326 rte_pci_dump(FILE *f)
328 struct rte_pci_device *dev = NULL;
330 FOREACH_DEVICE_ON_PCIBUS(dev) {
331 pci_dump_one_device(f, dev);
336 pci_parse(const char *name, void *addr)
338 struct rte_pci_addr *out = addr;
339 struct rte_pci_addr pci_addr;
342 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
343 if (parse && addr != NULL)
345 return parse == false;
348 /* register a driver */
350 rte_pci_register(struct rte_pci_driver *driver)
352 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
353 driver->bus = &rte_pci_bus;
356 /* unregister a driver */
358 rte_pci_unregister(struct rte_pci_driver *driver)
360 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
364 /* Add a device to PCI bus */
366 rte_pci_add_device(struct rte_pci_device *pci_dev)
368 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
371 /* Insert a device into a predefined position in PCI bus */
373 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
374 struct rte_pci_device *new_pci_dev)
376 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
379 /* Remove a device from PCI bus */
381 rte_pci_remove_device(struct rte_pci_device *pci_dev)
383 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
386 static struct rte_device *
387 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
390 const struct rte_pci_device *pstart;
391 struct rte_pci_device *pdev;
394 pstart = RTE_DEV_TO_PCI_CONST(start);
395 pdev = TAILQ_NEXT(pstart, next);
397 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
399 while (pdev != NULL) {
400 if (cmp(&pdev->device, data) == 0)
401 return &pdev->device;
402 pdev = TAILQ_NEXT(pdev, next);
408 * find the device which encounter the failure, by iterate over all device on
409 * PCI bus to check if the memory failure address is located in the range
410 * of the BARs of the device.
412 static struct rte_pci_device *
413 pci_find_device_by_addr(const void *failure_addr)
415 struct rte_pci_device *pdev = NULL;
416 uint64_t check_point, start, end, len;
419 check_point = (uint64_t)(uintptr_t)failure_addr;
421 FOREACH_DEVICE_ON_PCIBUS(pdev) {
422 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
423 start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
424 len = pdev->mem_resource[i].len;
426 if (check_point >= start && check_point < end) {
427 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
428 PRIx64" belongs to device %s!\n",
429 check_point, pdev->device.name);
438 pci_hot_unplug_handler(struct rte_device *dev)
440 struct rte_pci_device *pdev = NULL;
443 pdev = RTE_DEV_TO_PCI(dev);
447 switch (pdev->kdrv) {
448 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
451 * vfio kernel module guaranty the pci device would not be
452 * deleted until the user space release the resource, so no
453 * need to remap BARs resource here, just directly notify
454 * the req event to the user space to handle it.
456 rte_dev_event_callback_process(dev->name,
457 RTE_DEV_EVENT_REMOVE);
460 case RTE_KDRV_IGB_UIO:
461 case RTE_KDRV_UIO_GENERIC:
462 case RTE_KDRV_NIC_UIO:
463 /* BARs resource is invalid, remap it to be safe. */
464 ret = pci_uio_remap_resource(pdev);
468 "Not managed by a supported kernel driver, skipped\n");
477 pci_sigbus_handler(const void *failure_addr)
479 struct rte_pci_device *pdev = NULL;
482 pdev = pci_find_device_by_addr(failure_addr);
484 /* It is a generic sigbus error, no bus would handle it. */
487 /* The sigbus error is caused of hot-unplug. */
488 ret = pci_hot_unplug_handler(&pdev->device);
491 "Failed to handle hot-unplug for device %s",
500 pci_plug(struct rte_device *dev)
502 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
506 pci_unplug(struct rte_device *dev)
508 struct rte_pci_device *pdev;
511 pdev = RTE_DEV_TO_PCI(dev);
512 ret = rte_pci_detach_dev(pdev);
514 rte_pci_remove_device(pdev);
520 struct rte_pci_bus rte_pci_bus = {
522 .scan = rte_pci_scan,
523 .probe = rte_pci_probe,
524 .find_device = pci_find_device,
526 .unplug = pci_unplug,
528 .get_iommu_class = rte_pci_get_iommu_class,
529 .dev_iterate = rte_pci_dev_iterate,
530 .hot_unplug_handler = pci_hot_unplug_handler,
531 .sigbus_handler = pci_sigbus_handler,
533 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
534 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
537 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);