4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2013-2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
43 #include <rte_errno.h>
44 #include <rte_interrupts.h>
48 #include <rte_bus_pci.h>
49 #include <rte_per_lcore.h>
50 #include <rte_memory.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54 #include <rte_devargs.h>
58 extern struct rte_pci_bus rte_pci_bus;
60 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
62 const char *rte_pci_get_sysfs_path(void)
64 const char *path = NULL;
66 path = getenv("SYSFS_PCI_DEVICES");
68 return SYSFS_PCI_DEVICES;
73 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
75 struct rte_devargs *devargs;
76 struct rte_pci_addr addr;
79 pbus = rte_bus_find_by_name("pci");
80 TAILQ_FOREACH(devargs, &devargs_list, next) {
81 if (devargs->bus != pbus)
83 devargs->bus->parse(devargs->name, &addr);
84 if (!rte_pci_addr_cmp(&dev->addr, &addr))
91 pci_name_set(struct rte_pci_device *dev)
93 struct rte_devargs *devargs;
95 /* Each device has its internal, canonical name set. */
96 rte_pci_device_name(&dev->addr,
97 dev->name, sizeof(dev->name));
98 devargs = pci_devargs_lookup(dev);
99 dev->device.devargs = devargs;
100 /* In blacklist mode, if the device is not blacklisted, no
101 * rte_devargs exists for it.
104 /* If an rte_devargs exists, the generic rte_device uses the
105 * given name as its namea
107 dev->device.name = dev->device.devargs->name;
109 /* Otherwise, it uses the internal, canonical form. */
110 dev->device.name = dev->name;
114 * Match the PCI Driver and Device using the ID Table
117 rte_pci_match(const struct rte_pci_driver *pci_drv,
118 const struct rte_pci_device *pci_dev)
120 const struct rte_pci_id *id_table;
122 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
124 /* check if device's identifiers match the driver's ones */
125 if (id_table->vendor_id != pci_dev->id.vendor_id &&
126 id_table->vendor_id != PCI_ANY_ID)
128 if (id_table->device_id != pci_dev->id.device_id &&
129 id_table->device_id != PCI_ANY_ID)
131 if (id_table->subsystem_vendor_id !=
132 pci_dev->id.subsystem_vendor_id &&
133 id_table->subsystem_vendor_id != PCI_ANY_ID)
135 if (id_table->subsystem_device_id !=
136 pci_dev->id.subsystem_device_id &&
137 id_table->subsystem_device_id != PCI_ANY_ID)
139 if (id_table->class_id != pci_dev->id.class_id &&
140 id_table->class_id != RTE_CLASS_ANY_ID)
150 * If vendor/device ID match, call the probe() function of the
154 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
155 struct rte_pci_device *dev)
158 struct rte_pci_addr *loc;
160 if ((dr == NULL) || (dev == NULL))
165 /* The device is not blacklisted; Check if driver supports it */
166 if (!rte_pci_match(dr, dev))
167 /* Match of device and driver failed */
170 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
171 loc->domain, loc->bus, loc->devid, loc->function,
172 dev->device.numa_node);
174 /* no initialization when blacklisted, return without error */
175 if (dev->device.devargs != NULL &&
176 dev->device.devargs->policy ==
177 RTE_DEV_BLACKLISTED) {
178 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
183 if (dev->device.numa_node < 0) {
184 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
185 dev->device.numa_node = 0;
188 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
189 dev->id.device_id, dr->driver.name);
191 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
192 /* map resources for devices that use igb_uio */
193 ret = rte_pci_map_device(dev);
198 /* reference driver structure */
200 dev->device.driver = &dr->driver;
202 /* call the driver probe() function */
203 ret = dr->probe(dr, dev);
206 dev->device.driver = NULL;
207 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
208 /* Don't unmap if device is unsupported and
209 * driver needs mapped resources.
212 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
213 rte_pci_unmap_device(dev);
220 * If vendor/device ID match, call the remove() function of the
224 rte_pci_detach_dev(struct rte_pci_device *dev)
226 struct rte_pci_addr *loc;
227 struct rte_pci_driver *dr;
236 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
237 loc->domain, loc->bus, loc->devid,
238 loc->function, dev->device.numa_node);
240 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
241 dev->id.device_id, dr->driver.name);
244 ret = dr->remove(dev);
249 /* clear driver structure */
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 -1 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 /* Check if a driver is already loaded */
274 if (dev->driver != NULL)
277 FOREACH_DRIVER_ON_PCIBUS(dr) {
278 rc = rte_pci_probe_one_driver(dr, dev);
280 /* negative value is an error */
283 /* positive value means driver doesn't support it */
291 * Find the pci device specified by pci address, then invoke probe function of
292 * the driver of the device.
295 rte_pci_probe_one(const struct rte_pci_addr *addr)
297 struct rte_pci_device *dev = NULL;
304 /* update current pci device in global list, kernel bindings might have
305 * changed since last time we looked at it.
307 if (pci_update_device(addr) < 0)
310 FOREACH_DEVICE_ON_PCIBUS(dev) {
311 if (rte_pci_addr_cmp(&dev->addr, addr))
314 ret = pci_probe_all_drivers(dev);
322 RTE_LOG(WARNING, EAL,
323 "Requested device " PCI_PRI_FMT " cannot be used\n",
324 addr->domain, addr->bus, addr->devid, addr->function);
329 * Detach device specified by its pci address.
332 rte_pci_detach(const struct rte_pci_addr *addr)
334 struct rte_pci_device *dev = NULL;
340 FOREACH_DEVICE_ON_PCIBUS(dev) {
341 if (rte_pci_addr_cmp(&dev->addr, addr))
344 ret = rte_pci_detach_dev(dev);
346 /* negative value is an error */
349 /* positive value means driver doesn't support it */
352 rte_pci_remove_device(dev);
359 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
360 " cannot be used\n", dev->addr.domain, dev->addr.bus,
361 dev->addr.devid, dev->addr.function);
366 * Scan the content of the PCI bus, and call the probe() function for
367 * all registered drivers that have a matching entry in its id_table
368 * for discovered devices.
373 struct rte_pci_device *dev = NULL;
374 size_t probed = 0, failed = 0;
375 struct rte_devargs *devargs;
379 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
382 FOREACH_DEVICE_ON_PCIBUS(dev) {
385 devargs = dev->device.devargs;
386 /* probe all or only whitelisted devices */
388 ret = pci_probe_all_drivers(dev);
389 else if (devargs != NULL &&
390 devargs->policy == RTE_DEV_WHITELISTED)
391 ret = pci_probe_all_drivers(dev);
393 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
394 " cannot be used\n", dev->addr.domain, dev->addr.bus,
395 dev->addr.devid, dev->addr.function);
402 return (probed && probed == failed) ? -1 : 0;
405 /* dump one device */
407 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
411 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
412 dev->addr.devid, dev->addr.function);
413 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
416 for (i = 0; i != sizeof(dev->mem_resource) /
417 sizeof(dev->mem_resource[0]); i++) {
418 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
419 dev->mem_resource[i].phys_addr,
420 dev->mem_resource[i].len);
425 /* dump devices on the bus */
427 rte_pci_dump(FILE *f)
429 struct rte_pci_device *dev = NULL;
431 FOREACH_DEVICE_ON_PCIBUS(dev) {
432 pci_dump_one_device(f, dev);
437 pci_parse(const char *name, void *addr)
439 struct rte_pci_addr *out = addr;
440 struct rte_pci_addr pci_addr;
443 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
444 if (parse && addr != NULL)
446 return parse == false;
449 /* register a driver */
451 rte_pci_register(struct rte_pci_driver *driver)
453 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
454 driver->bus = &rte_pci_bus;
457 /* unregister a driver */
459 rte_pci_unregister(struct rte_pci_driver *driver)
461 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
465 /* Add a device to PCI bus */
467 rte_pci_add_device(struct rte_pci_device *pci_dev)
469 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
472 /* Insert a device into a predefined position in PCI bus */
474 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
475 struct rte_pci_device *new_pci_dev)
477 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
480 /* Remove a device from PCI bus */
482 rte_pci_remove_device(struct rte_pci_device *pci_dev)
484 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
487 static struct rte_device *
488 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
491 struct rte_pci_device *dev;
493 FOREACH_DEVICE_ON_PCIBUS(dev) {
494 if (start && &dev->device == start) {
495 start = NULL; /* starting point found */
498 if (cmp(&dev->device, data) == 0)
506 pci_plug(struct rte_device *dev)
508 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
512 pci_unplug(struct rte_device *dev)
514 struct rte_pci_device *pdev;
517 pdev = RTE_DEV_TO_PCI(dev);
518 ret = rte_pci_detach_dev(pdev);
520 rte_pci_remove_device(pdev);
526 struct rte_pci_bus rte_pci_bus = {
528 .scan = rte_pci_scan,
529 .probe = rte_pci_probe,
530 .find_device = pci_find_device,
532 .unplug = pci_unplug,
534 .get_iommu_class = rte_pci_get_iommu_class,
536 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
537 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
540 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);