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;
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 namea
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);
158 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
159 /* map resources for devices that use igb_uio */
160 ret = rte_pci_map_device(dev);
165 /* reference driver structure */
167 dev->device.driver = &dr->driver;
169 /* call the driver probe() function */
170 ret = dr->probe(dr, dev);
173 dev->device.driver = NULL;
174 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
175 /* Don't unmap if device is unsupported and
176 * driver needs mapped resources.
179 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
180 rte_pci_unmap_device(dev);
187 * If vendor/device ID match, call the remove() function of the
191 rte_pci_detach_dev(struct rte_pci_device *dev)
193 struct rte_pci_addr *loc;
194 struct rte_pci_driver *dr;
203 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
204 loc->domain, loc->bus, loc->devid,
205 loc->function, dev->device.numa_node);
207 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
208 dev->id.device_id, dr->driver.name);
211 ret = dr->remove(dev);
216 /* clear driver structure */
219 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
220 /* unmap resources for devices that use igb_uio */
221 rte_pci_unmap_device(dev);
227 * If vendor/device ID match, call the probe() function of all
228 * registered driver for the given device. Return -1 if initialization
229 * failed, return 1 if no driver is found for this device.
232 pci_probe_all_drivers(struct rte_pci_device *dev)
234 struct rte_pci_driver *dr = NULL;
240 /* Check if a driver is already loaded */
241 if (dev->driver != NULL)
244 FOREACH_DRIVER_ON_PCIBUS(dr) {
245 rc = rte_pci_probe_one_driver(dr, dev);
247 /* negative value is an error */
250 /* positive value means driver doesn't support it */
258 * Find the pci device specified by pci address, then invoke probe function of
259 * the driver of the device.
262 rte_pci_probe_one(const struct rte_pci_addr *addr)
264 struct rte_pci_device *dev = NULL;
271 /* update current pci device in global list, kernel bindings might have
272 * changed since last time we looked at it.
274 if (pci_update_device(addr) < 0)
277 FOREACH_DEVICE_ON_PCIBUS(dev) {
278 if (rte_pci_addr_cmp(&dev->addr, addr))
281 ret = pci_probe_all_drivers(dev);
289 RTE_LOG(WARNING, EAL,
290 "Requested device " PCI_PRI_FMT " cannot be used\n",
291 addr->domain, addr->bus, addr->devid, addr->function);
296 * Detach device specified by its pci address.
299 rte_pci_detach(const struct rte_pci_addr *addr)
301 struct rte_pci_device *dev = NULL;
307 FOREACH_DEVICE_ON_PCIBUS(dev) {
308 if (rte_pci_addr_cmp(&dev->addr, addr))
311 ret = rte_pci_detach_dev(dev);
313 /* negative value is an error */
316 /* positive value means driver doesn't support it */
319 rte_pci_remove_device(dev);
326 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
327 " cannot be used\n", dev->addr.domain, dev->addr.bus,
328 dev->addr.devid, dev->addr.function);
333 * Scan the content of the PCI bus, and call the probe() function for
334 * all registered drivers that have a matching entry in its id_table
335 * for discovered devices.
340 struct rte_pci_device *dev = NULL;
341 size_t probed = 0, failed = 0;
342 struct rte_devargs *devargs;
346 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
349 FOREACH_DEVICE_ON_PCIBUS(dev) {
352 devargs = dev->device.devargs;
353 /* probe all or only whitelisted devices */
355 ret = pci_probe_all_drivers(dev);
356 else if (devargs != NULL &&
357 devargs->policy == RTE_DEV_WHITELISTED)
358 ret = pci_probe_all_drivers(dev);
360 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
361 " cannot be used\n", dev->addr.domain, dev->addr.bus,
362 dev->addr.devid, dev->addr.function);
369 return (probed && probed == failed) ? -1 : 0;
372 /* dump one device */
374 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
378 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
379 dev->addr.devid, dev->addr.function);
380 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
383 for (i = 0; i != sizeof(dev->mem_resource) /
384 sizeof(dev->mem_resource[0]); i++) {
385 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
386 dev->mem_resource[i].phys_addr,
387 dev->mem_resource[i].len);
392 /* dump devices on the bus */
394 rte_pci_dump(FILE *f)
396 struct rte_pci_device *dev = NULL;
398 FOREACH_DEVICE_ON_PCIBUS(dev) {
399 pci_dump_one_device(f, dev);
404 pci_parse(const char *name, void *addr)
406 struct rte_pci_addr *out = addr;
407 struct rte_pci_addr pci_addr;
410 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
411 if (parse && addr != NULL)
413 return parse == false;
416 /* register a driver */
418 rte_pci_register(struct rte_pci_driver *driver)
420 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
421 driver->bus = &rte_pci_bus;
424 /* unregister a driver */
426 rte_pci_unregister(struct rte_pci_driver *driver)
428 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
432 /* Add a device to PCI bus */
434 rte_pci_add_device(struct rte_pci_device *pci_dev)
436 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
439 /* Insert a device into a predefined position in PCI bus */
441 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
442 struct rte_pci_device *new_pci_dev)
444 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
447 /* Remove a device from PCI bus */
449 rte_pci_remove_device(struct rte_pci_device *pci_dev)
451 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
454 static struct rte_device *
455 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
458 const struct rte_pci_device *pstart;
459 struct rte_pci_device *pdev;
462 pstart = RTE_DEV_TO_PCI_CONST(start);
463 pdev = TAILQ_NEXT(pstart, next);
465 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
467 while (pdev != NULL) {
468 if (cmp(&pdev->device, data) == 0)
469 return &pdev->device;
470 pdev = TAILQ_NEXT(pdev, next);
476 pci_plug(struct rte_device *dev)
478 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
482 pci_unplug(struct rte_device *dev)
484 struct rte_pci_device *pdev;
487 pdev = RTE_DEV_TO_PCI(dev);
488 ret = rte_pci_detach_dev(pdev);
490 rte_pci_remove_device(pdev);
496 struct rte_pci_bus rte_pci_bus = {
498 .scan = rte_pci_scan,
499 .probe = rte_pci_probe,
500 .find_device = pci_find_device,
502 .unplug = pci_unplug,
504 .get_iommu_class = rte_pci_get_iommu_class,
506 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
507 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
510 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);