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_per_lcore.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54 #include <rte_devargs.h>
56 #include "eal_private.h"
58 extern struct rte_pci_bus rte_pci_bus;
60 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
62 const char *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;
77 TAILQ_FOREACH(devargs, &devargs_list, next) {
78 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
79 devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
81 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
87 /* map a particular resource from a file */
89 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
94 /* Map the PCI memory resource of device */
95 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
96 MAP_SHARED | additional_flags, fd, offset);
97 if (mapaddr == MAP_FAILED) {
98 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
99 __func__, fd, requested_addr,
100 (unsigned long)size, (unsigned long)offset,
101 strerror(errno), mapaddr);
103 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
108 /* unmap a particular resource */
110 pci_unmap_resource(void *requested_addr, size_t size)
112 if (requested_addr == NULL)
115 /* Unmap the PCI memory resource of device */
116 if (munmap(requested_addr, size)) {
117 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
118 __func__, requested_addr, (unsigned long)size,
121 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
126 * Match the PCI Driver and Device using the ID Table
129 * PCI driver from which ID table would be extracted
131 * PCI device to match against the driver
133 * 1 for successful match
134 * 0 for unsuccessful match
137 rte_pci_match(const struct rte_pci_driver *pci_drv,
138 const struct rte_pci_device *pci_dev)
140 const struct rte_pci_id *id_table;
142 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
144 /* check if device's identifiers match the driver's ones */
145 if (id_table->vendor_id != pci_dev->id.vendor_id &&
146 id_table->vendor_id != PCI_ANY_ID)
148 if (id_table->device_id != pci_dev->id.device_id &&
149 id_table->device_id != PCI_ANY_ID)
151 if (id_table->subsystem_vendor_id !=
152 pci_dev->id.subsystem_vendor_id &&
153 id_table->subsystem_vendor_id != PCI_ANY_ID)
155 if (id_table->subsystem_device_id !=
156 pci_dev->id.subsystem_device_id &&
157 id_table->subsystem_device_id != PCI_ANY_ID)
159 if (id_table->class_id != pci_dev->id.class_id &&
160 id_table->class_id != RTE_CLASS_ANY_ID)
170 * If vendor/device ID match, call the probe() function of the
174 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
175 struct rte_pci_device *dev)
178 struct rte_pci_addr *loc;
180 if ((dr == NULL) || (dev == NULL))
185 /* The device is not blacklisted; Check if driver supports it */
186 if (!rte_pci_match(dr, dev))
187 /* Match of device and driver failed */
190 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
191 loc->domain, loc->bus, loc->devid, loc->function,
192 dev->device.numa_node);
194 /* no initialization when blacklisted, return without error */
195 if (dev->device.devargs != NULL &&
196 dev->device.devargs->type ==
197 RTE_DEVTYPE_BLACKLISTED_PCI) {
198 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
203 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
204 dev->id.device_id, dr->driver.name);
206 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
207 /* map resources for devices that use igb_uio */
208 ret = rte_pci_map_device(dev);
213 /* reference driver structure */
215 dev->device.driver = &dr->driver;
217 /* call the driver probe() function */
218 ret = dr->probe(dr, dev);
221 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
222 /* Don't unmap if device is unsupported and
223 * driver needs mapped resources.
226 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
227 rte_pci_unmap_device(dev);
234 * If vendor/device ID match, call the remove() function of the
238 rte_pci_detach_dev(struct rte_pci_device *dev)
240 struct rte_pci_addr *loc;
241 struct rte_pci_driver *dr;
249 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
250 loc->domain, loc->bus, loc->devid,
251 loc->function, dev->device.numa_node);
253 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
254 dev->id.device_id, dr->driver.name);
256 if (dr->remove && (dr->remove(dev) < 0))
257 return -1; /* negative value is an error */
259 /* clear driver structure */
262 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
263 /* unmap resources for devices that use igb_uio */
264 rte_pci_unmap_device(dev);
270 * If vendor/device ID match, call the probe() function of all
271 * registered driver for the given device. Return -1 if initialization
272 * failed, return 1 if no driver is found for this device.
275 pci_probe_all_drivers(struct rte_pci_device *dev)
277 struct rte_pci_driver *dr = NULL;
283 /* Check if a driver is already loaded */
284 if (dev->driver != NULL)
287 FOREACH_DRIVER_ON_PCIBUS(dr) {
288 rc = rte_pci_probe_one_driver(dr, dev);
290 /* negative value is an error */
293 /* positive value means driver doesn't support it */
301 * Find the pci device specified by pci address, then invoke probe function of
302 * the driver of the devive.
305 rte_pci_probe_one(const struct rte_pci_addr *addr)
307 struct rte_pci_device *dev = NULL;
314 /* update current pci device in global list, kernel bindings might have
315 * changed since last time we looked at it.
317 if (pci_update_device(addr) < 0)
320 FOREACH_DEVICE_ON_PCIBUS(dev) {
321 if (rte_eal_compare_pci_addr(&dev->addr, addr))
324 ret = pci_probe_all_drivers(dev);
332 RTE_LOG(WARNING, EAL,
333 "Requested device " PCI_PRI_FMT " cannot be used\n",
334 addr->domain, addr->bus, addr->devid, addr->function);
339 * Detach device specified by its pci address.
342 rte_pci_detach(const struct rte_pci_addr *addr)
344 struct rte_pci_device *dev = NULL;
350 FOREACH_DEVICE_ON_PCIBUS(dev) {
351 if (rte_eal_compare_pci_addr(&dev->addr, addr))
354 ret = rte_pci_detach_dev(dev);
356 /* negative value is an error */
359 /* positive value means driver doesn't support it */
362 rte_pci_remove_device(dev);
369 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
370 " cannot be used\n", dev->addr.domain, dev->addr.bus,
371 dev->addr.devid, dev->addr.function);
376 * Scan the content of the PCI bus, and call the probe() function for
377 * all registered drivers that have a matching entry in its id_table
378 * for discovered devices.
383 struct rte_pci_device *dev = NULL;
384 size_t probed = 0, failed = 0;
385 struct rte_devargs *devargs;
389 if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
392 FOREACH_DEVICE_ON_PCIBUS(dev) {
395 /* set devargs in PCI structure */
396 devargs = pci_devargs_lookup(dev);
398 dev->device.devargs = devargs;
400 /* probe all or only whitelisted devices */
402 ret = pci_probe_all_drivers(dev);
403 else if (devargs != NULL &&
404 devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
405 ret = pci_probe_all_drivers(dev);
407 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
408 " cannot be used\n", dev->addr.domain, dev->addr.bus,
409 dev->addr.devid, dev->addr.function);
416 return (probed && probed == failed) ? -1 : 0;
419 /* dump one device */
421 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
425 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
426 dev->addr.devid, dev->addr.function);
427 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
430 for (i = 0; i != sizeof(dev->mem_resource) /
431 sizeof(dev->mem_resource[0]); i++) {
432 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
433 dev->mem_resource[i].phys_addr,
434 dev->mem_resource[i].len);
439 /* dump devices on the bus */
441 rte_pci_dump(FILE *f)
443 struct rte_pci_device *dev = NULL;
445 FOREACH_DEVICE_ON_PCIBUS(dev) {
446 pci_dump_one_device(f, dev);
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;
493 bool start_found = !start;
495 FOREACH_DEVICE_ON_PCIBUS(dev) {
497 if (&dev->device == start)
501 if (cmp(&dev->device, data) == 0)
508 struct rte_pci_bus rte_pci_bus = {
510 .scan = rte_pci_scan,
511 .probe = rte_pci_probe,
512 .find_device = pci_find_device,
514 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
515 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
518 RTE_REGISTER_BUS(PCI_BUS_NAME, rte_pci_bus.bus);