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 static void rte_pci_remove_device(struct rte_pci_device *pci_device);
32 extern struct rte_pci_bus rte_pci_bus;
34 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
36 const char *rte_pci_get_sysfs_path(void)
38 const char *path = NULL;
40 path = getenv("SYSFS_PCI_DEVICES");
42 return SYSFS_PCI_DEVICES;
47 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
49 struct rte_devargs *devargs;
50 struct rte_pci_addr addr;
52 RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
53 devargs->bus->parse(devargs->name, &addr);
54 if (!rte_pci_addr_cmp(&dev->addr, &addr))
61 pci_name_set(struct rte_pci_device *dev)
63 struct rte_devargs *devargs;
65 /* Each device has its internal, canonical name set. */
66 rte_pci_device_name(&dev->addr,
67 dev->name, sizeof(dev->name));
68 devargs = pci_devargs_lookup(dev);
69 dev->device.devargs = devargs;
70 /* In blacklist mode, if the device is not blacklisted, no
71 * rte_devargs exists for it.
74 /* If an rte_devargs exists, the generic rte_device uses the
75 * given name as its namea
77 dev->device.name = dev->device.devargs->name;
79 /* Otherwise, it uses the internal, canonical form. */
80 dev->device.name = dev->name;
84 * Match the PCI Driver and Device using the ID Table
87 rte_pci_match(const struct rte_pci_driver *pci_drv,
88 const struct rte_pci_device *pci_dev)
90 const struct rte_pci_id *id_table;
92 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
94 /* check if device's identifiers match the driver's ones */
95 if (id_table->vendor_id != pci_dev->id.vendor_id &&
96 id_table->vendor_id != PCI_ANY_ID)
98 if (id_table->device_id != pci_dev->id.device_id &&
99 id_table->device_id != PCI_ANY_ID)
101 if (id_table->subsystem_vendor_id !=
102 pci_dev->id.subsystem_vendor_id &&
103 id_table->subsystem_vendor_id != PCI_ANY_ID)
105 if (id_table->subsystem_device_id !=
106 pci_dev->id.subsystem_device_id &&
107 id_table->subsystem_device_id != PCI_ANY_ID)
109 if (id_table->class_id != pci_dev->id.class_id &&
110 id_table->class_id != RTE_CLASS_ANY_ID)
120 * If vendor/device ID match, call the probe() function of the
124 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
125 struct rte_pci_device *dev)
128 struct rte_pci_addr *loc;
130 if ((dr == NULL) || (dev == NULL))
135 /* The device is not blacklisted; Check if driver supports it */
136 if (!rte_pci_match(dr, dev))
137 /* Match of device and driver failed */
140 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
141 loc->domain, loc->bus, loc->devid, loc->function,
142 dev->device.numa_node);
144 /* no initialization when blacklisted, return without error */
145 if (dev->device.devargs != NULL &&
146 dev->device.devargs->policy ==
147 RTE_DEV_BLACKLISTED) {
148 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
153 if (dev->device.numa_node < 0) {
154 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
155 dev->device.numa_node = 0;
158 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
159 dev->id.device_id, dr->driver.name);
161 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
162 /* map resources for devices that use igb_uio */
163 ret = rte_pci_map_device(dev);
168 /* reference driver structure */
170 dev->device.driver = &dr->driver;
172 /* call the driver probe() function */
173 ret = dr->probe(dr, dev);
176 dev->device.driver = NULL;
177 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
178 /* Don't unmap if device is unsupported and
179 * driver needs mapped resources.
182 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
183 rte_pci_unmap_device(dev);
190 * If vendor/device ID match, call the remove() function of the
194 rte_pci_detach_dev(struct rte_pci_device *dev)
196 struct rte_pci_addr *loc;
197 struct rte_pci_driver *dr;
206 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
207 loc->domain, loc->bus, loc->devid,
208 loc->function, dev->device.numa_node);
210 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
211 dev->id.device_id, dr->driver.name);
214 ret = dr->remove(dev);
219 /* clear driver structure */
222 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
223 /* unmap resources for devices that use igb_uio */
224 rte_pci_unmap_device(dev);
230 * If vendor/device ID match, call the probe() function of all
231 * registered driver for the given device. Return -1 if initialization
232 * failed, return 1 if no driver is found for this device.
235 pci_probe_all_drivers(struct rte_pci_device *dev)
237 struct rte_pci_driver *dr = NULL;
243 /* Check if a driver is already loaded */
244 if (dev->driver != NULL)
247 FOREACH_DRIVER_ON_PCIBUS(dr) {
248 rc = rte_pci_probe_one_driver(dr, dev);
250 /* negative value is an error */
253 /* positive value means driver doesn't support it */
261 * Find the pci device specified by pci address, then invoke probe function of
262 * the driver of the device.
265 rte_pci_probe_one(const struct rte_pci_addr *addr)
267 struct rte_pci_device *dev = NULL;
274 /* update current pci device in global list, kernel bindings might have
275 * changed since last time we looked at it.
277 if (pci_update_device(addr) < 0)
280 FOREACH_DEVICE_ON_PCIBUS(dev) {
281 if (rte_pci_addr_cmp(&dev->addr, addr))
284 ret = pci_probe_all_drivers(dev);
292 RTE_LOG(WARNING, EAL,
293 "Requested device " PCI_PRI_FMT " cannot be used\n",
294 addr->domain, addr->bus, addr->devid, addr->function);
299 * Detach device specified by its pci address.
302 rte_pci_detach(const struct rte_pci_addr *addr)
304 struct rte_pci_device *dev = NULL;
310 FOREACH_DEVICE_ON_PCIBUS(dev) {
311 if (rte_pci_addr_cmp(&dev->addr, addr))
314 ret = rte_pci_detach_dev(dev);
316 /* negative value is an error */
319 /* positive value means driver doesn't support it */
322 rte_pci_remove_device(dev);
329 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
330 " cannot be used\n", dev->addr.domain, dev->addr.bus,
331 dev->addr.devid, dev->addr.function);
336 * Scan the content of the PCI bus, and call the probe() function for
337 * all registered drivers that have a matching entry in its id_table
338 * for discovered devices.
343 struct rte_pci_device *dev = NULL;
344 size_t probed = 0, failed = 0;
345 struct rte_devargs *devargs;
349 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
352 FOREACH_DEVICE_ON_PCIBUS(dev) {
355 devargs = dev->device.devargs;
356 /* probe all or only whitelisted devices */
358 ret = pci_probe_all_drivers(dev);
359 else if (devargs != NULL &&
360 devargs->policy == RTE_DEV_WHITELISTED)
361 ret = pci_probe_all_drivers(dev);
363 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
364 " cannot be used\n", dev->addr.domain, dev->addr.bus,
365 dev->addr.devid, dev->addr.function);
372 return (probed && probed == failed) ? -1 : 0;
375 /* dump one device */
377 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
381 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
382 dev->addr.devid, dev->addr.function);
383 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
386 for (i = 0; i != sizeof(dev->mem_resource) /
387 sizeof(dev->mem_resource[0]); i++) {
388 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
389 dev->mem_resource[i].phys_addr,
390 dev->mem_resource[i].len);
395 /* dump devices on the bus */
397 rte_pci_dump(FILE *f)
399 struct rte_pci_device *dev = NULL;
401 FOREACH_DEVICE_ON_PCIBUS(dev) {
402 pci_dump_one_device(f, dev);
407 pci_parse(const char *name, void *addr)
409 struct rte_pci_addr *out = addr;
410 struct rte_pci_addr pci_addr;
413 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
414 if (parse && addr != NULL)
416 return parse == false;
419 /* register a driver */
421 rte_pci_register(struct rte_pci_driver *driver)
423 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
424 driver->bus = &rte_pci_bus;
427 /* unregister a driver */
429 rte_pci_unregister(struct rte_pci_driver *driver)
431 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
435 /* Add a device to PCI bus */
437 rte_pci_add_device(struct rte_pci_device *pci_dev)
439 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
442 /* Insert a device into a predefined position in PCI bus */
444 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
445 struct rte_pci_device *new_pci_dev)
447 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
450 /* Remove a device from PCI bus */
452 rte_pci_remove_device(struct rte_pci_device *pci_dev)
454 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
457 static struct rte_device *
458 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
461 const struct rte_pci_device *pstart;
462 struct rte_pci_device *pdev;
465 pstart = RTE_DEV_TO_PCI_CONST(start);
466 pdev = TAILQ_NEXT(pstart, next);
468 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
470 while (pdev != NULL) {
471 if (cmp(&pdev->device, data) == 0)
472 return &pdev->device;
473 pdev = TAILQ_NEXT(pdev, next);
479 pci_plug(struct rte_device *dev)
481 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
485 pci_unplug(struct rte_device *dev)
487 struct rte_pci_device *pdev;
490 pdev = RTE_DEV_TO_PCI(dev);
491 ret = rte_pci_detach_dev(pdev);
493 rte_pci_remove_device(pdev);
499 struct rte_pci_bus rte_pci_bus = {
501 .scan = rte_pci_scan,
502 .probe = rte_pci_probe,
503 .find_device = pci_find_device,
505 .unplug = pci_unplug,
507 .get_iommu_class = rte_pci_get_iommu_class,
509 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
510 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
513 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);