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>
30 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
32 const char *rte_pci_get_sysfs_path(void)
34 const char *path = NULL;
36 path = getenv("SYSFS_PCI_DEVICES");
38 return SYSFS_PCI_DEVICES;
43 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
45 struct rte_devargs *devargs;
46 struct rte_pci_addr addr;
48 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
49 devargs->bus->parse(devargs->name, &addr);
50 if (!rte_pci_addr_cmp(&dev->addr, &addr))
57 pci_name_set(struct rte_pci_device *dev)
59 struct rte_devargs *devargs;
61 /* Each device has its internal, canonical name set. */
62 rte_pci_device_name(&dev->addr,
63 dev->name, sizeof(dev->name));
64 devargs = pci_devargs_lookup(dev);
65 dev->device.devargs = devargs;
66 /* In blacklist mode, if the device is not blacklisted, no
67 * rte_devargs exists for it.
70 /* If an rte_devargs exists, the generic rte_device uses the
71 * given name as its name.
73 dev->device.name = dev->device.devargs->name;
75 /* Otherwise, it uses the internal, canonical form. */
76 dev->device.name = dev->name;
80 * Match the PCI Driver and Device using the ID Table
83 rte_pci_match(const struct rte_pci_driver *pci_drv,
84 const struct rte_pci_device *pci_dev)
86 const struct rte_pci_id *id_table;
88 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
90 /* check if device's identifiers match the driver's ones */
91 if (id_table->vendor_id != pci_dev->id.vendor_id &&
92 id_table->vendor_id != PCI_ANY_ID)
94 if (id_table->device_id != pci_dev->id.device_id &&
95 id_table->device_id != PCI_ANY_ID)
97 if (id_table->subsystem_vendor_id !=
98 pci_dev->id.subsystem_vendor_id &&
99 id_table->subsystem_vendor_id != PCI_ANY_ID)
101 if (id_table->subsystem_device_id !=
102 pci_dev->id.subsystem_device_id &&
103 id_table->subsystem_device_id != PCI_ANY_ID)
105 if (id_table->class_id != pci_dev->id.class_id &&
106 id_table->class_id != RTE_CLASS_ANY_ID)
116 * If vendor/device ID match, call the probe() function of the
120 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
121 struct rte_pci_device *dev)
124 struct rte_pci_addr *loc;
126 if ((dr == NULL) || (dev == NULL))
131 /* The device is not blacklisted; Check if driver supports it */
132 if (!rte_pci_match(dr, dev))
133 /* Match of device and driver failed */
136 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
137 loc->domain, loc->bus, loc->devid, loc->function,
138 dev->device.numa_node);
140 /* no initialization when blacklisted, return without error */
141 if (dev->device.devargs != NULL &&
142 dev->device.devargs->policy ==
143 RTE_DEV_BLACKLISTED) {
144 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
149 if (dev->device.numa_node < 0) {
150 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
151 dev->device.numa_node = 0;
154 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
155 dev->id.device_id, dr->driver.name);
158 * reference driver structure
159 * This needs to be before rte_pci_map_device(), as it enables to use
160 * driver flags for adjusting configuration.
163 dev->device.driver = &dr->driver;
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);
170 dev->device.driver = NULL;
175 /* call the driver probe() function */
176 ret = dr->probe(dr, dev);
179 dev->device.driver = NULL;
180 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
181 /* Don't unmap if device is unsupported and
182 * driver needs mapped resources.
185 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
186 rte_pci_unmap_device(dev);
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->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 pci_hot_unplug_handler(struct rte_device *dev)
409 struct rte_pci_device *pdev = NULL;
412 pdev = RTE_DEV_TO_PCI(dev);
416 switch (pdev->kdrv) {
417 case RTE_KDRV_IGB_UIO:
418 case RTE_KDRV_UIO_GENERIC:
419 case RTE_KDRV_NIC_UIO:
420 /* BARs resource is invalid, remap it to be safe. */
421 ret = pci_uio_remap_resource(pdev);
425 "Not managed by a supported kernel driver, skipped\n");
434 pci_plug(struct rte_device *dev)
436 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
440 pci_unplug(struct rte_device *dev)
442 struct rte_pci_device *pdev;
445 pdev = RTE_DEV_TO_PCI(dev);
446 ret = rte_pci_detach_dev(pdev);
448 rte_pci_remove_device(pdev);
454 struct rte_pci_bus rte_pci_bus = {
456 .scan = rte_pci_scan,
457 .probe = rte_pci_probe,
458 .find_device = pci_find_device,
460 .unplug = pci_unplug,
462 .get_iommu_class = rte_pci_get_iommu_class,
463 .dev_iterate = rte_pci_dev_iterate,
464 .hot_unplug_handler = pci_hot_unplug_handler,
466 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
467 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
470 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);