4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright 2013-2014 6WIND S.A.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name of 6WIND S.A. nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include <sys/queue.h>
72 #include <rte_errno.h>
73 #include <rte_interrupts.h>
76 #include <rte_per_lcore.h>
77 #include <rte_memory.h>
78 #include <rte_memzone.h>
80 #include <rte_string_fns.h>
81 #include <rte_common.h>
82 #include <rte_devargs.h>
84 #include "eal_private.h"
86 struct pci_driver_list pci_driver_list =
87 TAILQ_HEAD_INITIALIZER(pci_driver_list);
88 struct pci_device_list pci_device_list =
89 TAILQ_HEAD_INITIALIZER(pci_device_list);
91 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
93 const char *pci_get_sysfs_path(void)
95 const char *path = NULL;
97 path = getenv("SYSFS_PCI_DEVICES");
99 return SYSFS_PCI_DEVICES;
104 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
106 struct rte_devargs *devargs;
108 TAILQ_FOREACH(devargs, &devargs_list, next) {
109 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
110 devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
112 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
118 /* map a particular resource from a file */
120 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
121 int additional_flags)
125 /* Map the PCI memory resource of device */
126 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
127 MAP_SHARED | additional_flags, fd, offset);
128 if (mapaddr == MAP_FAILED) {
129 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
130 __func__, fd, requested_addr,
131 (unsigned long)size, (unsigned long)offset,
132 strerror(errno), mapaddr);
134 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
139 /* unmap a particular resource */
141 pci_unmap_resource(void *requested_addr, size_t size)
143 if (requested_addr == NULL)
146 /* Unmap the PCI memory resource of device */
147 if (munmap(requested_addr, size)) {
148 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
149 __func__, requested_addr, (unsigned long)size,
152 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
157 * If vendor/device ID match, call the probe() function of the
161 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
164 const struct rte_pci_id *id_table;
166 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
168 /* check if device's identifiers match the driver's ones */
169 if (id_table->vendor_id != dev->id.vendor_id &&
170 id_table->vendor_id != PCI_ANY_ID)
172 if (id_table->device_id != dev->id.device_id &&
173 id_table->device_id != PCI_ANY_ID)
175 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
176 id_table->subsystem_vendor_id != PCI_ANY_ID)
178 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
179 id_table->subsystem_device_id != PCI_ANY_ID)
181 if (id_table->class_id != dev->id.class_id &&
182 id_table->class_id != RTE_CLASS_ANY_ID)
185 struct rte_pci_addr *loc = &dev->addr;
187 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
188 loc->domain, loc->bus, loc->devid, loc->function,
189 dev->device.numa_node);
191 /* no initialization when blacklisted, return without error */
192 if (dev->device.devargs != NULL &&
193 dev->device.devargs->type ==
194 RTE_DEVTYPE_BLACKLISTED_PCI) {
195 RTE_LOG(INFO, EAL, " Device is blacklisted, not initializing\n");
199 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
200 dev->id.device_id, dr->driver.name);
202 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
203 /* map resources for devices that use igb_uio */
204 ret = rte_eal_pci_map_device(dev);
209 /* reference driver structure */
212 /* call the driver probe() function */
213 ret = dr->probe(dr, dev);
216 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
217 rte_eal_pci_unmap_device(dev);
222 /* return positive value if driver doesn't support this device */
227 * If vendor/device ID match, call the remove() function of the
231 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
232 struct rte_pci_device *dev)
234 const struct rte_pci_id *id_table;
236 if ((dr == NULL) || (dev == NULL))
239 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
241 /* check if device's identifiers match the driver's ones */
242 if (id_table->vendor_id != dev->id.vendor_id &&
243 id_table->vendor_id != PCI_ANY_ID)
245 if (id_table->device_id != dev->id.device_id &&
246 id_table->device_id != PCI_ANY_ID)
248 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
249 id_table->subsystem_vendor_id != PCI_ANY_ID)
251 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
252 id_table->subsystem_device_id != PCI_ANY_ID)
255 struct rte_pci_addr *loc = &dev->addr;
257 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
258 loc->domain, loc->bus, loc->devid,
259 loc->function, dev->device.numa_node);
261 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
262 dev->id.device_id, dr->driver.name);
264 if (dr->remove && (dr->remove(dev) < 0))
265 return -1; /* negative value is an error */
267 /* clear driver structure */
270 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
271 /* unmap resources for devices that use igb_uio */
272 rte_eal_pci_unmap_device(dev);
277 /* return positive value if driver doesn't support this device */
282 * If vendor/device ID match, call the probe() function of all
283 * registered driver for the given device. Return -1 if initialization
284 * failed, return 1 if no driver is found for this device.
287 pci_probe_all_drivers(struct rte_pci_device *dev)
289 struct rte_pci_driver *dr = NULL;
295 /* Check if a driver is already loaded */
296 if (dev->driver != NULL)
299 TAILQ_FOREACH(dr, &pci_driver_list, next) {
300 rc = rte_eal_pci_probe_one_driver(dr, dev);
302 /* negative value is an error */
305 /* positive value means driver doesn't support it */
313 * If vendor/device ID match, call the remove() function of all
314 * registered driver for the given device. Return -1 if initialization
315 * failed, return 1 if no driver is found for this device.
318 pci_detach_all_drivers(struct rte_pci_device *dev)
320 struct rte_pci_driver *dr = NULL;
326 TAILQ_FOREACH(dr, &pci_driver_list, next) {
327 rc = rte_eal_pci_detach_dev(dr, dev);
329 /* negative value is an error */
332 /* positive value means driver doesn't support it */
340 * Find the pci device specified by pci address, then invoke probe function of
341 * the driver of the devive.
344 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
346 struct rte_pci_device *dev = NULL;
352 /* update current pci device in global list, kernel bindings might have
353 * changed since last time we looked at it.
355 if (pci_update_device(addr) < 0)
358 TAILQ_FOREACH(dev, &pci_device_list, next) {
359 if (rte_eal_compare_pci_addr(&dev->addr, addr))
362 ret = pci_probe_all_drivers(dev);
370 RTE_LOG(WARNING, EAL,
371 "Requested device " PCI_PRI_FMT " cannot be used\n",
372 addr->domain, addr->bus, addr->devid, addr->function);
377 * Detach device specified by its pci address.
380 rte_eal_pci_detach(const struct rte_pci_addr *addr)
382 struct rte_pci_device *dev = NULL;
388 TAILQ_FOREACH(dev, &pci_device_list, next) {
389 if (rte_eal_compare_pci_addr(&dev->addr, addr))
392 ret = pci_detach_all_drivers(dev);
396 TAILQ_REMOVE(&pci_device_list, dev, next);
403 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
404 " cannot be used\n", dev->addr.domain, dev->addr.bus,
405 dev->addr.devid, dev->addr.function);
410 * Scan the content of the PCI bus, and call the probe() function for
411 * all registered drivers that have a matching entry in its id_table
412 * for discovered devices.
415 rte_eal_pci_probe(void)
417 struct rte_pci_device *dev = NULL;
418 size_t probed = 0, failed = 0;
419 struct rte_devargs *devargs;
423 if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
426 TAILQ_FOREACH(dev, &pci_device_list, next) {
429 /* set devargs in PCI structure */
430 devargs = pci_devargs_lookup(dev);
432 dev->device.devargs = devargs;
434 /* probe all or only whitelisted devices */
436 ret = pci_probe_all_drivers(dev);
437 else if (devargs != NULL &&
438 devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
439 ret = pci_probe_all_drivers(dev);
441 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
442 " cannot be used\n", dev->addr.domain, dev->addr.bus,
443 dev->addr.devid, dev->addr.function);
449 return (probed && probed == failed) ? -1 : 0;
452 /* dump one device */
454 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
458 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
459 dev->addr.devid, dev->addr.function);
460 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
463 for (i = 0; i != sizeof(dev->mem_resource) /
464 sizeof(dev->mem_resource[0]); i++) {
465 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
466 dev->mem_resource[i].phys_addr,
467 dev->mem_resource[i].len);
472 /* dump devices on the bus */
474 rte_eal_pci_dump(FILE *f)
476 struct rte_pci_device *dev = NULL;
478 TAILQ_FOREACH(dev, &pci_device_list, next) {
479 pci_dump_one_device(f, dev);
483 /* register a driver */
485 rte_eal_pci_register(struct rte_pci_driver *driver)
487 TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
488 rte_eal_driver_register(&driver->driver);
491 /* unregister a driver */
493 rte_eal_pci_unregister(struct rte_pci_driver *driver)
495 rte_eal_driver_unregister(&driver->driver);
496 TAILQ_REMOVE(&pci_driver_list, driver, next);