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 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
90 struct rte_devargs *devargs;
92 TAILQ_FOREACH(devargs, &devargs_list, next) {
93 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
94 devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
96 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
102 /* map a particular resource from a file */
104 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
105 int additional_flags)
109 /* Map the PCI memory resource of device */
110 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
111 MAP_SHARED | additional_flags, fd, offset);
112 if (mapaddr == MAP_FAILED) {
113 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
114 __func__, fd, requested_addr,
115 (unsigned long)size, (unsigned long)offset,
116 strerror(errno), mapaddr);
118 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
123 /* unmap a particular resource */
125 pci_unmap_resource(void *requested_addr, size_t size)
127 if (requested_addr == NULL)
130 /* Unmap the PCI memory resource of device */
131 if (munmap(requested_addr, size)) {
132 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
133 __func__, requested_addr, (unsigned long)size,
136 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
142 pci_map_device(struct rte_pci_device *dev)
146 /* try mapping the NIC resources using VFIO if it exists */
150 if (pci_vfio_is_enabled())
151 ret = pci_vfio_map_resource(dev);
154 case RTE_KDRV_IGB_UIO:
155 case RTE_KDRV_UIO_GENERIC:
156 case RTE_KDRV_NIC_UIO:
157 /* map resources for devices that use uio */
158 ret = pci_uio_map_resource(dev);
161 RTE_LOG(DEBUG, EAL, " Not managed by a supported kernel driver,"
170 /* Unmap pci device */
172 pci_unmap_device(struct rte_pci_device *dev)
177 /* try unmapping the NIC resources using VFIO if it exists */
180 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
182 case RTE_KDRV_IGB_UIO:
183 case RTE_KDRV_UIO_GENERIC:
184 case RTE_KDRV_NIC_UIO:
185 /* unmap resources for devices that use uio */
186 pci_uio_unmap_resource(dev);
189 RTE_LOG(DEBUG, EAL, " Not managed by a supported kernel driver,"
196 * If vendor/device ID match, call the devinit() function of the
200 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
203 const struct rte_pci_id *id_table;
205 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
207 /* check if device's identifiers match the driver's ones */
208 if (id_table->vendor_id != dev->id.vendor_id &&
209 id_table->vendor_id != PCI_ANY_ID)
211 if (id_table->device_id != dev->id.device_id &&
212 id_table->device_id != PCI_ANY_ID)
214 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
215 id_table->subsystem_vendor_id != PCI_ANY_ID)
217 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
218 id_table->subsystem_device_id != PCI_ANY_ID)
221 struct rte_pci_addr *loc = &dev->addr;
223 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
224 loc->domain, loc->bus, loc->devid, loc->function,
227 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
228 dev->id.device_id, dr->name);
230 /* no initialization when blacklisted, return without error */
231 if (dev->devargs != NULL &&
232 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
233 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
237 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
238 #ifdef RTE_PCI_CONFIG
240 * Set PCIe config space for high performance.
241 * Return value can be ignored.
243 pci_config_space_set(dev);
245 /* map resources for devices that use igb_uio */
246 ret = pci_map_device(dev);
249 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
250 rte_eal_process_type() == RTE_PROC_PRIMARY) {
251 /* unbind current driver */
252 if (pci_unbind_kernel_driver(dev) < 0)
256 /* reference driver structure */
259 /* call the driver devinit() function */
260 return dr->devinit(dr, dev);
262 /* return positive value if driver is not found */
267 * If vendor/device ID match, call the devuninit() function of the
271 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
272 struct rte_pci_device *dev)
274 const struct rte_pci_id *id_table;
276 if ((dr == NULL) || (dev == NULL))
279 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
281 /* check if device's identifiers match the driver's ones */
282 if (id_table->vendor_id != dev->id.vendor_id &&
283 id_table->vendor_id != PCI_ANY_ID)
285 if (id_table->device_id != dev->id.device_id &&
286 id_table->device_id != PCI_ANY_ID)
288 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
289 id_table->subsystem_vendor_id != PCI_ANY_ID)
291 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
292 id_table->subsystem_device_id != PCI_ANY_ID)
295 struct rte_pci_addr *loc = &dev->addr;
297 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
298 loc->domain, loc->bus, loc->devid,
299 loc->function, dev->numa_node);
301 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
302 dev->id.device_id, dr->name);
304 /* call the driver devuninit() function */
305 if (dr->devuninit && (dr->devuninit(dev) < 0))
306 return -1; /* negative value is an error */
308 /* clear driver structure */
311 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
312 /* unmap resources for devices that use igb_uio */
313 pci_unmap_device(dev);
318 /* return positive value if driver is not found */
323 * If vendor/device ID match, call the devinit() function of all
324 * registered driver for the given device. Return -1 if initialization
325 * failed, return 1 if no driver is found for this device.
328 pci_probe_all_drivers(struct rte_pci_device *dev)
330 struct rte_pci_driver *dr = NULL;
336 TAILQ_FOREACH(dr, &pci_driver_list, next) {
337 rc = rte_eal_pci_probe_one_driver(dr, dev);
339 /* negative value is an error */
342 /* positive value means driver not found */
350 * If vendor/device ID match, call the devuninit() function of all
351 * registered driver for the given device. Return -1 if initialization
352 * failed, return 1 if no driver is found for this device.
355 pci_close_all_drivers(struct rte_pci_device *dev)
357 struct rte_pci_driver *dr = NULL;
363 TAILQ_FOREACH(dr, &pci_driver_list, next) {
364 rc = rte_eal_pci_close_one_driver(dr, dev);
366 /* negative value is an error */
369 /* positive value means driver not found */
377 * Find the pci device specified by pci address, then invoke probe function of
378 * the driver of the devive.
381 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
383 struct rte_pci_device *dev = NULL;
389 TAILQ_FOREACH(dev, &pci_device_list, next) {
390 if (rte_eal_compare_pci_addr(&dev->addr, addr))
393 ret = pci_probe_all_drivers(dev);
401 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
402 " cannot be used\n", dev->addr.domain, dev->addr.bus,
403 dev->addr.devid, dev->addr.function);
408 * Find the pci device specified by pci address, then invoke close function of
409 * the driver of the devive.
412 rte_eal_pci_close_one(const struct rte_pci_addr *addr)
414 struct rte_pci_device *dev = NULL;
420 TAILQ_FOREACH(dev, &pci_device_list, next) {
421 if (rte_eal_compare_pci_addr(&dev->addr, addr))
424 ret = pci_close_all_drivers(dev);
428 TAILQ_REMOVE(&pci_device_list, dev, next);
434 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
435 " cannot be used\n", dev->addr.domain, dev->addr.bus,
436 dev->addr.devid, dev->addr.function);
441 * Scan the content of the PCI bus, and call the devinit() function for
442 * all registered drivers that have a matching entry in its id_table
443 * for discovered devices.
446 rte_eal_pci_probe(void)
448 struct rte_pci_device *dev = NULL;
449 struct rte_devargs *devargs;
453 if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
456 TAILQ_FOREACH(dev, &pci_device_list, next) {
458 /* set devargs in PCI structure */
459 devargs = pci_devargs_lookup(dev);
461 dev->devargs = devargs;
463 /* probe all or only whitelisted devices */
465 ret = pci_probe_all_drivers(dev);
466 else if (devargs != NULL &&
467 devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
468 ret = pci_probe_all_drivers(dev);
470 rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
471 " cannot be used\n", dev->addr.domain, dev->addr.bus,
472 dev->addr.devid, dev->addr.function);
478 /* dump one device */
480 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
484 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
485 dev->addr.devid, dev->addr.function);
486 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
489 for (i = 0; i != sizeof(dev->mem_resource) /
490 sizeof(dev->mem_resource[0]); i++) {
491 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
492 dev->mem_resource[i].phys_addr,
493 dev->mem_resource[i].len);
498 /* dump devices on the bus */
500 rte_eal_pci_dump(FILE *f)
502 struct rte_pci_device *dev = NULL;
504 TAILQ_FOREACH(dev, &pci_device_list, next) {
505 pci_dump_one_device(f, dev);
509 /* register a driver */
511 rte_eal_pci_register(struct rte_pci_driver *driver)
513 TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
516 /* unregister a driver */
518 rte_eal_pci_unregister(struct rte_pci_driver *driver)
520 TAILQ_REMOVE(&pci_driver_list, driver, next);