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 TAILQ_HEAD_INITIALIZER(pci_driver_list
);
87 struct pci_device_list pci_device_list
=
88 TAILQ_HEAD_INITIALIZER(pci_device_list
);
90 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
92 const char *pci_get_sysfs_path(void)
94 const char *path
= NULL
;
96 path
= getenv("SYSFS_PCI_DEVICES");
98 return SYSFS_PCI_DEVICES
;
103 static struct rte_devargs
*pci_devargs_lookup(struct rte_pci_device
*dev
)
105 struct rte_devargs
*devargs
;
107 TAILQ_FOREACH(devargs
, &devargs_list
, next
) {
108 if (devargs
->type
!= RTE_DEVTYPE_BLACKLISTED_PCI
&&
109 devargs
->type
!= RTE_DEVTYPE_WHITELISTED_PCI
)
111 if (!rte_eal_compare_pci_addr(&dev
->addr
, &devargs
->pci
.addr
))
117 /* map a particular resource from a file */
119 pci_map_resource(void *requested_addr
, int fd
, off_t offset
, size_t size
,
120 int additional_flags
)
124 /* Map the PCI memory resource of device */
125 mapaddr
= mmap(requested_addr
, size
, PROT_READ
| PROT_WRITE
,
126 MAP_SHARED
| additional_flags
, fd
, offset
);
127 if (mapaddr
== MAP_FAILED
) {
128 RTE_LOG(ERR
, EAL
, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
129 __func__
, fd
, requested_addr
,
130 (unsigned long)size
, (unsigned long)offset
,
131 strerror(errno
), mapaddr
);
133 RTE_LOG(DEBUG
, EAL
, " PCI memory mapped at %p\n", mapaddr
);
138 /* unmap a particular resource */
140 pci_unmap_resource(void *requested_addr
, size_t size
)
142 if (requested_addr
== NULL
)
145 /* Unmap the PCI memory resource of device */
146 if (munmap(requested_addr
, size
)) {
147 RTE_LOG(ERR
, EAL
, "%s(): cannot munmap(%p, 0x%lx): %s\n",
148 __func__
, requested_addr
, (unsigned long)size
,
151 RTE_LOG(DEBUG
, EAL
, " PCI memory unmapped at %p\n",
156 * If vendor/device ID match, call the probe() function of the
160 rte_eal_pci_probe_one_driver(struct rte_pci_driver
*dr
, struct rte_pci_device
*dev
)
163 const struct rte_pci_id
*id_table
;
165 for (id_table
= dr
->id_table
; id_table
->vendor_id
!= 0; id_table
++) {
167 /* check if device's identifiers match the driver's ones */
168 if (id_table
->vendor_id
!= dev
->id
.vendor_id
&&
169 id_table
->vendor_id
!= PCI_ANY_ID
)
171 if (id_table
->device_id
!= dev
->id
.device_id
&&
172 id_table
->device_id
!= PCI_ANY_ID
)
174 if (id_table
->subsystem_vendor_id
!= dev
->id
.subsystem_vendor_id
&&
175 id_table
->subsystem_vendor_id
!= PCI_ANY_ID
)
177 if (id_table
->subsystem_device_id
!= dev
->id
.subsystem_device_id
&&
178 id_table
->subsystem_device_id
!= PCI_ANY_ID
)
180 if (id_table
->class_id
!= dev
->id
.class_id
&&
181 id_table
->class_id
!= RTE_CLASS_ANY_ID
)
184 struct rte_pci_addr
*loc
= &dev
->addr
;
186 RTE_LOG(INFO
, EAL
, "PCI device "PCI_PRI_FMT
" on NUMA socket %i\n",
187 loc
->domain
, loc
->bus
, loc
->devid
, loc
->function
,
188 dev
->device
.numa_node
);
190 /* no initialization when blacklisted, return without error */
191 if (dev
->device
.devargs
!= NULL
&&
192 dev
->device
.devargs
->type
==
193 RTE_DEVTYPE_BLACKLISTED_PCI
) {
194 RTE_LOG(INFO
, EAL
, " Device is blacklisted, not initializing\n");
198 RTE_LOG(INFO
, EAL
, " probe driver: %x:%x %s\n", dev
->id
.vendor_id
,
199 dev
->id
.device_id
, dr
->driver
.name
);
201 if (dr
->drv_flags
& RTE_PCI_DRV_NEED_MAPPING
) {
202 /* map resources for devices that use igb_uio */
203 ret
= rte_eal_pci_map_device(dev
);
206 } else if (dr
->drv_flags
& RTE_PCI_DRV_FORCE_UNBIND
&&
207 rte_eal_process_type() == RTE_PROC_PRIMARY
) {
208 /* unbind current driver */
209 if (pci_unbind_kernel_driver(dev
) < 0)
213 /* reference driver structure */
216 /* call the driver probe() function */
217 ret
= dr
->probe(dr
, dev
);
223 /* return positive value if driver doesn't support this device */
228 * If vendor/device ID match, call the remove() function of the
232 rte_eal_pci_detach_dev(struct rte_pci_driver
*dr
,
233 struct rte_pci_device
*dev
)
235 const struct rte_pci_id
*id_table
;
237 if ((dr
== NULL
) || (dev
== NULL
))
240 for (id_table
= dr
->id_table
; id_table
->vendor_id
!= 0; id_table
++) {
242 /* check if device's identifiers match the driver's ones */
243 if (id_table
->vendor_id
!= dev
->id
.vendor_id
&&
244 id_table
->vendor_id
!= PCI_ANY_ID
)
246 if (id_table
->device_id
!= dev
->id
.device_id
&&
247 id_table
->device_id
!= PCI_ANY_ID
)
249 if (id_table
->subsystem_vendor_id
!= dev
->id
.subsystem_vendor_id
&&
250 id_table
->subsystem_vendor_id
!= PCI_ANY_ID
)
252 if (id_table
->subsystem_device_id
!= dev
->id
.subsystem_device_id
&&
253 id_table
->subsystem_device_id
!= PCI_ANY_ID
)
256 struct rte_pci_addr
*loc
= &dev
->addr
;
258 RTE_LOG(DEBUG
, EAL
, "PCI device "PCI_PRI_FMT
" on NUMA socket %i\n",
259 loc
->domain
, loc
->bus
, loc
->devid
,
260 loc
->function
, dev
->device
.numa_node
);
262 RTE_LOG(DEBUG
, EAL
, " remove driver: %x:%x %s\n", dev
->id
.vendor_id
,
263 dev
->id
.device_id
, dr
->driver
.name
);
265 if (dr
->remove
&& (dr
->remove(dev
) < 0))
266 return -1; /* negative value is an error */
268 /* clear driver structure */
271 if (dr
->drv_flags
& RTE_PCI_DRV_NEED_MAPPING
)
272 /* unmap resources for devices that use igb_uio */
273 rte_eal_pci_unmap_device(dev
);
278 /* return positive value if driver doesn't support this device */
283 * If vendor/device ID match, call the probe() function of all
284 * registered driver for the given device. Return -1 if initialization
285 * failed, return 1 if no driver is found for this device.
288 pci_probe_all_drivers(struct rte_pci_device
*dev
)
290 struct rte_pci_driver
*dr
= NULL
;
296 /* Check if a driver is already loaded */
297 if (dev
->driver
!= NULL
)
300 TAILQ_FOREACH(dr
, &pci_driver_list
, next
) {
301 rc
= rte_eal_pci_probe_one_driver(dr
, dev
);
303 /* negative value is an error */
306 /* positive value means driver doesn't support it */
314 * If vendor/device ID match, call the remove() function of all
315 * registered driver for the given device. Return -1 if initialization
316 * failed, return 1 if no driver is found for this device.
319 pci_detach_all_drivers(struct rte_pci_device
*dev
)
321 struct rte_pci_driver
*dr
= NULL
;
327 TAILQ_FOREACH(dr
, &pci_driver_list
, next
) {
328 rc
= rte_eal_pci_detach_dev(dr
, dev
);
330 /* negative value is an error */
333 /* positive value means driver doesn't support it */
341 * Find the pci device specified by pci address, then invoke probe function of
342 * the driver of the devive.
345 rte_eal_pci_probe_one(const struct rte_pci_addr
*addr
)
347 struct rte_pci_device
*dev
= NULL
;
353 /* update current pci device in global list, kernel bindings might have
354 * changed since last time we looked at it.
356 if (pci_update_device(addr
) < 0)
359 TAILQ_FOREACH(dev
, &pci_device_list
, next
) {
360 if (rte_eal_compare_pci_addr(&dev
->addr
, addr
))
363 ret
= pci_probe_all_drivers(dev
);
371 RTE_LOG(WARNING
, EAL
,
372 "Requested device " PCI_PRI_FMT
" cannot be used\n",
373 addr
->domain
, addr
->bus
, addr
->devid
, addr
->function
);
378 * Detach device specified by its pci address.
381 rte_eal_pci_detach(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_detach_all_drivers(dev
);
397 TAILQ_REMOVE(&pci_device_list
, dev
, next
);
404 RTE_LOG(WARNING
, EAL
, "Requested device " PCI_PRI_FMT
405 " cannot be used\n", dev
->addr
.domain
, dev
->addr
.bus
,
406 dev
->addr
.devid
, dev
->addr
.function
);
411 * Scan the content of the PCI bus, and call the probe() function for
412 * all registered drivers that have a matching entry in its id_table
413 * for discovered devices.
416 rte_eal_pci_probe(void)
418 struct rte_pci_device
*dev
= NULL
;
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
) {
428 /* set devargs in PCI structure */
429 devargs
= pci_devargs_lookup(dev
);
431 dev
->device
.devargs
= devargs
;
433 /* probe all or only whitelisted devices */
435 ret
= pci_probe_all_drivers(dev
);
436 else if (devargs
!= NULL
&&
437 devargs
->type
== RTE_DEVTYPE_WHITELISTED_PCI
)
438 ret
= pci_probe_all_drivers(dev
);
440 rte_exit(EXIT_FAILURE
, "Requested device " PCI_PRI_FMT
441 " cannot be used\n", dev
->addr
.domain
, dev
->addr
.bus
,
442 dev
->addr
.devid
, dev
->addr
.function
);
448 /* dump one device */
450 pci_dump_one_device(FILE *f
, struct rte_pci_device
*dev
)
454 fprintf(f
, PCI_PRI_FMT
, dev
->addr
.domain
, dev
->addr
.bus
,
455 dev
->addr
.devid
, dev
->addr
.function
);
456 fprintf(f
, " - vendor:%x device:%x\n", dev
->id
.vendor_id
,
459 for (i
= 0; i
!= sizeof(dev
->mem_resource
) /
460 sizeof(dev
->mem_resource
[0]); i
++) {
461 fprintf(f
, " %16.16"PRIx64
" %16.16"PRIx64
"\n",
462 dev
->mem_resource
[i
].phys_addr
,
463 dev
->mem_resource
[i
].len
);
468 /* dump devices on the bus */
470 rte_eal_pci_dump(FILE *f
)
472 struct rte_pci_device
*dev
= NULL
;
474 TAILQ_FOREACH(dev
, &pci_device_list
, next
) {
475 pci_dump_one_device(f
, dev
);
479 /* register a driver */
481 rte_eal_pci_register(struct rte_pci_driver
*driver
)
483 TAILQ_INSERT_TAIL(&pci_driver_list
, driver
, next
);
484 rte_eal_driver_register(&driver
->driver
);
487 /* unregister a driver */
489 rte_eal_pci_unregister(struct rte_pci_driver
*driver
)
491 rte_eal_driver_unregister(&driver
->driver
);
492 TAILQ_REMOVE(&pci_driver_list
, driver
, next
);