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>
51 #include <rte_memzone.h>
53 #include <rte_string_fns.h>
54 #include <rte_common.h>
55 #include <rte_devargs.h>
59 extern struct rte_pci_bus rte_pci_bus;
61 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
63 const char *pci_get_sysfs_path(void)
65 const char *path = NULL;
67 path = getenv("SYSFS_PCI_DEVICES");
69 return SYSFS_PCI_DEVICES;
74 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
76 struct rte_devargs *devargs;
77 struct rte_pci_addr addr;
80 pbus = rte_bus_find_by_name("pci");
81 TAILQ_FOREACH(devargs, &devargs_list, next) {
82 if (devargs->bus != pbus)
84 devargs->bus->parse(devargs->name, &addr);
85 if (!pci_addr_cmp(&dev->addr, &addr))
92 pci_name_set(struct rte_pci_device *dev)
94 struct rte_devargs *devargs;
96 /* Each device has its internal, canonical name set. */
97 rte_pci_device_name(&dev->addr,
98 dev->name, sizeof(dev->name));
99 devargs = pci_devargs_lookup(dev);
100 dev->device.devargs = devargs;
101 /* In blacklist mode, if the device is not blacklisted, no
102 * rte_devargs exists for it.
105 /* If an rte_devargs exists, the generic rte_device uses the
106 * given name as its namea
108 dev->device.name = dev->device.devargs->name;
110 /* Otherwise, it uses the internal, canonical form. */
111 dev->device.name = dev->name;
115 * Match the PCI Driver and Device using the ID Table
118 rte_pci_match(const struct rte_pci_driver *pci_drv,
119 const struct rte_pci_device *pci_dev)
121 const struct rte_pci_id *id_table;
123 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
125 /* check if device's identifiers match the driver's ones */
126 if (id_table->vendor_id != pci_dev->id.vendor_id &&
127 id_table->vendor_id != PCI_ANY_ID)
129 if (id_table->device_id != pci_dev->id.device_id &&
130 id_table->device_id != PCI_ANY_ID)
132 if (id_table->subsystem_vendor_id !=
133 pci_dev->id.subsystem_vendor_id &&
134 id_table->subsystem_vendor_id != PCI_ANY_ID)
136 if (id_table->subsystem_device_id !=
137 pci_dev->id.subsystem_device_id &&
138 id_table->subsystem_device_id != PCI_ANY_ID)
140 if (id_table->class_id != pci_dev->id.class_id &&
141 id_table->class_id != RTE_CLASS_ANY_ID)
151 * If vendor/device ID match, call the probe() function of the
155 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
156 struct rte_pci_device *dev)
159 struct rte_pci_addr *loc;
161 if ((dr == NULL) || (dev == NULL))
166 /* The device is not blacklisted; Check if driver supports it */
167 if (!rte_pci_match(dr, dev))
168 /* Match of device and driver failed */
171 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
172 loc->domain, loc->bus, loc->devid, loc->function,
173 dev->device.numa_node);
175 /* no initialization when blacklisted, return without error */
176 if (dev->device.devargs != NULL &&
177 dev->device.devargs->policy ==
178 RTE_DEV_BLACKLISTED) {
179 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
184 if (dev->device.numa_node < 0) {
185 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
186 dev->device.numa_node = 0;
189 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
190 dev->id.device_id, dr->driver.name);
192 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
193 /* map resources for devices that use igb_uio */
194 ret = rte_pci_map_device(dev);
199 /* reference driver structure */
201 dev->device.driver = &dr->driver;
203 /* call the driver probe() function */
204 ret = dr->probe(dr, dev);
207 dev->device.driver = NULL;
208 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
209 /* Don't unmap if device is unsupported and
210 * driver needs mapped resources.
213 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
214 rte_pci_unmap_device(dev);
221 * If vendor/device ID match, call the remove() function of the
225 rte_pci_detach_dev(struct rte_pci_device *dev)
227 struct rte_pci_addr *loc;
228 struct rte_pci_driver *dr;
237 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
238 loc->domain, loc->bus, loc->devid,
239 loc->function, dev->device.numa_node);
241 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
242 dev->id.device_id, dr->driver.name);
245 ret = dr->remove(dev);
250 /* clear driver structure */
253 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
254 /* unmap resources for devices that use igb_uio */
255 rte_pci_unmap_device(dev);
261 * If vendor/device ID match, call the probe() function of all
262 * registered driver for the given device. Return -1 if initialization
263 * failed, return 1 if no driver is found for this device.
266 pci_probe_all_drivers(struct rte_pci_device *dev)
268 struct rte_pci_driver *dr = NULL;
274 /* Check if a driver is already loaded */
275 if (dev->driver != NULL)
278 FOREACH_DRIVER_ON_PCIBUS(dr) {
279 rc = rte_pci_probe_one_driver(dr, dev);
281 /* negative value is an error */
284 /* positive value means driver doesn't support it */
292 * Find the pci device specified by pci address, then invoke probe function of
293 * the driver of the device.
296 rte_pci_probe_one(const struct rte_pci_addr *addr)
298 struct rte_pci_device *dev = NULL;
305 /* update current pci device in global list, kernel bindings might have
306 * changed since last time we looked at it.
308 if (pci_update_device(addr) < 0)
311 FOREACH_DEVICE_ON_PCIBUS(dev) {
312 if (pci_addr_cmp(&dev->addr, addr))
315 ret = pci_probe_all_drivers(dev);
323 RTE_LOG(WARNING, EAL,
324 "Requested device " PCI_PRI_FMT " cannot be used\n",
325 addr->domain, addr->bus, addr->devid, addr->function);
330 * Detach device specified by its pci address.
333 rte_pci_detach(const struct rte_pci_addr *addr)
335 struct rte_pci_device *dev = NULL;
341 FOREACH_DEVICE_ON_PCIBUS(dev) {
342 if (pci_addr_cmp(&dev->addr, addr))
345 ret = rte_pci_detach_dev(dev);
347 /* negative value is an error */
350 /* positive value means driver doesn't support it */
353 rte_pci_remove_device(dev);
360 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
361 " cannot be used\n", dev->addr.domain, dev->addr.bus,
362 dev->addr.devid, dev->addr.function);
367 * Scan the content of the PCI bus, and call the probe() function for
368 * all registered drivers that have a matching entry in its id_table
369 * for discovered devices.
374 struct rte_pci_device *dev = NULL;
375 size_t probed = 0, failed = 0;
376 struct rte_devargs *devargs;
380 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
383 FOREACH_DEVICE_ON_PCIBUS(dev) {
386 devargs = dev->device.devargs;
387 /* probe all or only whitelisted devices */
389 ret = pci_probe_all_drivers(dev);
390 else if (devargs != NULL &&
391 devargs->policy == RTE_DEV_WHITELISTED)
392 ret = pci_probe_all_drivers(dev);
394 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
395 " cannot be used\n", dev->addr.domain, dev->addr.bus,
396 dev->addr.devid, dev->addr.function);
403 return (probed && probed == failed) ? -1 : 0;
406 /* dump one device */
408 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
412 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
413 dev->addr.devid, dev->addr.function);
414 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
417 for (i = 0; i != sizeof(dev->mem_resource) /
418 sizeof(dev->mem_resource[0]); i++) {
419 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
420 dev->mem_resource[i].phys_addr,
421 dev->mem_resource[i].len);
426 /* dump devices on the bus */
428 rte_pci_dump(FILE *f)
430 struct rte_pci_device *dev = NULL;
432 FOREACH_DEVICE_ON_PCIBUS(dev) {
433 pci_dump_one_device(f, dev);
438 pci_parse(const char *name, void *addr)
440 struct rte_pci_addr *out = addr;
441 struct rte_pci_addr pci_addr;
444 parse = (pci_addr_parse(name, &pci_addr) == 0);
445 if (parse && addr != NULL)
447 return parse == false;
450 /* register a driver */
452 rte_pci_register(struct rte_pci_driver *driver)
454 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
455 driver->bus = &rte_pci_bus;
458 /* unregister a driver */
460 rte_pci_unregister(struct rte_pci_driver *driver)
462 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
466 /* Add a device to PCI bus */
468 rte_pci_add_device(struct rte_pci_device *pci_dev)
470 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
473 /* Insert a device into a predefined position in PCI bus */
475 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
476 struct rte_pci_device *new_pci_dev)
478 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
481 /* Remove a device from PCI bus */
483 rte_pci_remove_device(struct rte_pci_device *pci_dev)
485 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
488 static struct rte_device *
489 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
492 struct rte_pci_device *dev;
494 FOREACH_DEVICE_ON_PCIBUS(dev) {
495 if (start && &dev->device == start) {
496 start = NULL; /* starting point found */
499 if (cmp(&dev->device, data) == 0)
507 pci_plug(struct rte_device *dev)
509 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
513 pci_unplug(struct rte_device *dev)
515 struct rte_pci_device *pdev;
518 pdev = RTE_DEV_TO_PCI(dev);
519 ret = rte_pci_detach_dev(pdev);
521 rte_pci_remove_device(pdev);
527 struct rte_pci_bus rte_pci_bus = {
529 .scan = rte_pci_scan,
530 .probe = rte_pci_probe,
531 .find_device = pci_find_device,
533 .unplug = pci_unplug,
535 .get_iommu_class = rte_pci_get_iommu_class,
537 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
538 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
541 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);