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_interrupts.h>
75 #include <rte_per_lcore.h>
76 #include <rte_memory.h>
77 #include <rte_memzone.h>
79 #include <rte_string_fns.h>
80 #include <rte_common.h>
81 #include <rte_devargs.h>
83 #include "eal_private.h"
85 struct pci_driver_list pci_driver_list;
86 struct pci_device_list pci_device_list;
88 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
90 const char *pci_get_sysfs_path(void)
92 const char *path = NULL;
94 path = getenv("SYSFS_PCI_DEVICES");
96 return SYSFS_PCI_DEVICES;
101 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
103 struct rte_devargs *devargs;
105 TAILQ_FOREACH(devargs, &devargs_list, next) {
106 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
107 devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
109 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
115 /* map a particular resource from a file */
117 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
118 int additional_flags)
122 /* Map the PCI memory resource of device */
123 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
124 MAP_SHARED | additional_flags, fd, offset);
125 if (mapaddr == MAP_FAILED) {
126 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
127 __func__, fd, requested_addr,
128 (unsigned long)size, (unsigned long)offset,
129 strerror(errno), mapaddr);
131 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
136 /* unmap a particular resource */
138 pci_unmap_resource(void *requested_addr, size_t size)
140 if (requested_addr == NULL)
143 /* Unmap the PCI memory resource of device */
144 if (munmap(requested_addr, size)) {
145 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
146 __func__, requested_addr, (unsigned long)size,
149 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
154 * If vendor/device ID match, call the devinit() function of the
158 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
161 const struct rte_pci_id *id_table;
163 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
165 /* check if device's identifiers match the driver's ones */
166 if (id_table->vendor_id != dev->id.vendor_id &&
167 id_table->vendor_id != PCI_ANY_ID)
169 if (id_table->device_id != dev->id.device_id &&
170 id_table->device_id != PCI_ANY_ID)
172 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
173 id_table->subsystem_vendor_id != PCI_ANY_ID)
175 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
176 id_table->subsystem_device_id != PCI_ANY_ID)
179 struct rte_pci_addr *loc = &dev->addr;
181 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
182 loc->domain, loc->bus, loc->devid, loc->function,
185 /* no initialization when blacklisted, return without error */
186 if (dev->devargs != NULL &&
187 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
188 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
192 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
193 dev->id.device_id, dr->name);
195 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
196 /* map resources for devices that use igb_uio */
197 ret = rte_eal_pci_map_device(dev);
200 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
201 rte_eal_process_type() == RTE_PROC_PRIMARY) {
202 /* unbind current driver */
203 if (pci_unbind_kernel_driver(dev) < 0)
207 /* reference driver structure */
210 /* call the driver devinit() function */
211 return dr->devinit(dr, dev);
213 /* return positive value if driver doesn't support this device */
218 * If vendor/device ID match, call the devuninit() function of the
222 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
223 struct rte_pci_device *dev)
225 const struct rte_pci_id *id_table;
227 if ((dr == NULL) || (dev == NULL))
230 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
232 /* check if device's identifiers match the driver's ones */
233 if (id_table->vendor_id != dev->id.vendor_id &&
234 id_table->vendor_id != PCI_ANY_ID)
236 if (id_table->device_id != dev->id.device_id &&
237 id_table->device_id != PCI_ANY_ID)
239 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
240 id_table->subsystem_vendor_id != PCI_ANY_ID)
242 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
243 id_table->subsystem_device_id != PCI_ANY_ID)
246 struct rte_pci_addr *loc = &dev->addr;
248 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
249 loc->domain, loc->bus, loc->devid,
250 loc->function, dev->numa_node);
252 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
253 dev->id.device_id, dr->name);
255 if (dr->devuninit && (dr->devuninit(dev) < 0))
256 return -1; /* negative value is an error */
258 /* clear driver structure */
261 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
262 /* unmap resources for devices that use igb_uio */
263 rte_eal_pci_unmap_device(dev);
268 /* return positive value if driver doesn't support this device */
273 * If vendor/device ID match, call the devinit() function of all
274 * registered driver for the given device. Return -1 if initialization
275 * failed, return 1 if no driver is found for this device.
278 pci_probe_all_drivers(struct rte_pci_device *dev)
280 struct rte_pci_driver *dr = NULL;
286 TAILQ_FOREACH(dr, &pci_driver_list, next) {
287 rc = rte_eal_pci_probe_one_driver(dr, dev);
289 /* negative value is an error */
292 /* positive value means driver doesn't support it */
300 * If vendor/device ID match, call the devuninit() function of all
301 * registered driver for the given device. Return -1 if initialization
302 * failed, return 1 if no driver is found for this device.
305 pci_detach_all_drivers(struct rte_pci_device *dev)
307 struct rte_pci_driver *dr = NULL;
313 TAILQ_FOREACH(dr, &pci_driver_list, next) {
314 rc = rte_eal_pci_detach_dev(dr, dev);
316 /* negative value is an error */
319 /* positive value means driver doesn't support it */
327 * Find the pci device specified by pci address, then invoke probe function of
328 * the driver of the devive.
331 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
333 struct rte_pci_device *dev = NULL;
339 TAILQ_FOREACH(dev, &pci_device_list, next) {
340 if (rte_eal_compare_pci_addr(&dev->addr, addr))
343 ret = pci_probe_all_drivers(dev);
351 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
352 " cannot be used\n", dev->addr.domain, dev->addr.bus,
353 dev->addr.devid, dev->addr.function);
358 * Detach device specified by its pci address.
361 rte_eal_pci_detach(const struct rte_pci_addr *addr)
363 struct rte_pci_device *dev = NULL;
369 TAILQ_FOREACH(dev, &pci_device_list, next) {
370 if (rte_eal_compare_pci_addr(&dev->addr, addr))
373 ret = pci_detach_all_drivers(dev);
377 TAILQ_REMOVE(&pci_device_list, dev, next);
383 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
384 " cannot be used\n", dev->addr.domain, dev->addr.bus,
385 dev->addr.devid, dev->addr.function);
390 * Scan the content of the PCI bus, and call the devinit() function for
391 * all registered drivers that have a matching entry in its id_table
392 * for discovered devices.
395 rte_eal_pci_probe(void)
397 struct rte_pci_device *dev = NULL;
398 struct rte_devargs *devargs;
402 if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
405 TAILQ_FOREACH(dev, &pci_device_list, next) {
407 /* set devargs in PCI structure */
408 devargs = pci_devargs_lookup(dev);
410 dev->devargs = devargs;
412 /* probe all or only whitelisted devices */
414 ret = pci_probe_all_drivers(dev);
415 else if (devargs != NULL &&
416 devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
417 ret = pci_probe_all_drivers(dev);
419 rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
420 " cannot be used\n", dev->addr.domain, dev->addr.bus,
421 dev->addr.devid, dev->addr.function);
427 /* dump one device */
429 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
433 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
434 dev->addr.devid, dev->addr.function);
435 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
438 for (i = 0; i != sizeof(dev->mem_resource) /
439 sizeof(dev->mem_resource[0]); i++) {
440 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
441 dev->mem_resource[i].phys_addr,
442 dev->mem_resource[i].len);
447 /* dump devices on the bus */
449 rte_eal_pci_dump(FILE *f)
451 struct rte_pci_device *dev = NULL;
453 TAILQ_FOREACH(dev, &pci_device_list, next) {
454 pci_dump_one_device(f, dev);
458 /* register a driver */
460 rte_eal_pci_register(struct rte_pci_driver *driver)
462 TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
465 /* unregister a driver */
467 rte_eal_pci_unregister(struct rte_pci_driver *driver)
469 TAILQ_REMOVE(&pci_driver_list, driver, next);