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.
41 #include <sys/types.h>
48 #include <sys/queue.h>
50 #include <sys/ioctl.h>
51 #include <sys/pciio.h>
52 #include <dev/pci/pcireg.h>
54 #include <rte_interrupts.h>
57 #include <rte_common.h>
58 #include <rte_launch.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_debug.h>
68 #include <rte_devargs.h>
70 #include "rte_pci_dev_ids.h"
71 #include "eal_filesystem.h"
72 #include "eal_private.h"
76 * PCI probing under linux
78 * This code is used to simulate a PCI probe by parsing information in
79 * sysfs. Moreover, when a registered driver matches a device, the
80 * kernel driver currently using it is unloaded and replaced by
81 * igb_uio module, which is a very minimal userland driver for Intel
82 * network card, only providing access to PCI BAR to applications, and
83 * enabling bus master.
85 static struct rte_tailq_elem rte_uio_tailq = {
86 .name = "UIO_RESOURCE_LIST",
88 EAL_REGISTER_TAILQ(rte_uio_tailq)
90 /* unbind kernel driver for this device */
92 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
94 RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
99 /* map a particular resource from a file */
101 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
102 int additional_flags)
106 /* Map the PCI memory resource of device */
107 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
108 MAP_SHARED | additional_flags, fd, offset);
109 if (mapaddr == MAP_FAILED) {
111 "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
112 __func__, fd, requested_addr,
113 (unsigned long)size, (unsigned long)offset,
114 strerror(errno), mapaddr);
116 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
122 pci_uio_map_secondary(struct rte_pci_device *dev)
125 struct mapped_pci_resource *uio_res;
126 struct mapped_pci_res_list *uio_res_list =
127 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
129 TAILQ_FOREACH(uio_res, uio_res_list, next) {
131 /* skip this element if it doesn't match our PCI address */
132 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
135 for (i = 0; i != uio_res->nb_maps; i++) {
137 * open devname, to mmap it
139 fd = open(uio_res->maps[i].path, O_RDWR);
141 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
142 uio_res->maps[i].path, strerror(errno));
146 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
147 fd, (off_t)uio_res->maps[i].offset,
148 (size_t)uio_res->maps[i].size, 0);
149 /* fd is not needed in slave process, close it */
151 if (mapaddr != uio_res->maps[i].addr) {
153 "Cannot mmap device resource file %s to address: %p\n",
154 uio_res->maps[i].path,
155 uio_res->maps[i].addr);
162 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
167 pci_uio_free_resource(struct rte_pci_device *dev,
168 struct mapped_pci_resource *uio_res)
172 if (dev->intr_handle.fd) {
173 close(dev->intr_handle.fd);
174 dev->intr_handle.fd = -1;
175 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
180 pci_uio_alloc_resource(struct rte_pci_device *dev,
181 struct mapped_pci_resource **uio_res)
183 char devname[PATH_MAX]; /* contains the /dev/uioX */
184 struct rte_pci_addr *loc;
188 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
189 dev->addr.bus, dev->addr.devid, dev->addr.function);
191 if (access(devname, O_RDWR) < 0) {
192 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
193 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
197 /* save fd if in primary process */
198 dev->intr_handle.fd = open(devname, O_RDWR);
199 if (dev->intr_handle.fd < 0) {
200 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
201 devname, strerror(errno));
204 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
206 /* allocate the mapping details for secondary processes*/
207 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
208 if (*uio_res == NULL) {
210 "%s(): cannot store uio mmap details\n", __func__);
214 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
215 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
220 pci_uio_free_resource(dev, *uio_res);
225 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
226 struct mapped_pci_resource *uio_res, int map_idx)
233 struct pci_map *maps;
235 maps = uio_res->maps;
236 devname = uio_res->path;
237 pagesz = sysconf(_SC_PAGESIZE);
239 /* allocate memory to keep path */
240 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
241 if (maps[map_idx].path == NULL) {
242 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
248 * open resource file, to mmap it
250 fd = open(devname, O_RDWR);
252 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
253 devname, strerror(errno));
257 /* if matching map is found, then use it */
258 offset = res_idx * pagesz;
259 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
260 (size_t)dev->mem_resource[res_idx].len, 0);
262 if (mapaddr == MAP_FAILED)
265 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
266 maps[map_idx].size = dev->mem_resource[res_idx].len;
267 maps[map_idx].addr = mapaddr;
268 maps[map_idx].offset = offset;
269 strcpy(maps[map_idx].path, devname);
270 dev->mem_resource[res_idx].addr = mapaddr;
275 rte_free(maps[map_idx].path);
279 /* map the PCI resource of a PCI device in virtual memory */
281 pci_uio_map_resource(struct rte_pci_device *dev)
283 int i, map_idx = 0, ret;
285 struct mapped_pci_resource *uio_res = NULL;
286 struct mapped_pci_res_list *uio_res_list =
287 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
289 dev->intr_handle.fd = -1;
290 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
292 /* secondary processes - use already recorded details */
293 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
294 return pci_uio_map_secondary(dev);
296 /* allocate uio resource */
297 ret = pci_uio_alloc_resource(dev, &uio_res);
302 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
304 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
307 ret = pci_uio_map_resource_by_index(dev, i,
315 uio_res->nb_maps = map_idx;
317 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
321 for (i = 0; i < map_idx; i++)
322 rte_free(uio_res->maps[i].path);
323 pci_uio_free_resource(dev, uio_res);
327 /* Scan one pci sysfs entry, and fill the devices list from it. */
329 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
331 struct rte_pci_device *dev;
332 struct pci_bar_io bar;
335 dev = malloc(sizeof(*dev));
340 memset(dev, 0, sizeof(*dev));
341 dev->addr.domain = conf->pc_sel.pc_domain;
342 dev->addr.bus = conf->pc_sel.pc_bus;
343 dev->addr.devid = conf->pc_sel.pc_dev;
344 dev->addr.function = conf->pc_sel.pc_func;
347 dev->id.vendor_id = conf->pc_vendor;
350 dev->id.device_id = conf->pc_device;
352 /* get subsystem_vendor id */
353 dev->id.subsystem_vendor_id = conf->pc_subvendor;
355 /* get subsystem_device id */
356 dev->id.subsystem_device_id = conf->pc_subdevice;
358 /* TODO: get max_vfs */
361 /* FreeBSD has no NUMA support (yet) */
364 /* parse resources */
365 switch (conf->pc_hdr & PCIM_HDRTYPE) {
366 case PCIM_HDRTYPE_NORMAL:
367 max = PCIR_MAX_BAR_0;
369 case PCIM_HDRTYPE_BRIDGE:
370 max = PCIR_MAX_BAR_1;
372 case PCIM_HDRTYPE_CARDBUS:
373 max = PCIR_MAX_BAR_2;
379 for (i = 0; i <= max; i++) {
380 bar.pbi_sel = conf->pc_sel;
381 bar.pbi_reg = PCIR_BAR(i);
382 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
385 dev->mem_resource[i].len = bar.pbi_length;
386 if (PCI_BAR_IO(bar.pbi_base)) {
387 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
390 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
393 /* device is valid, add in list (sorted) */
394 if (TAILQ_EMPTY(&pci_device_list)) {
395 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
398 struct rte_pci_device *dev2 = NULL;
401 TAILQ_FOREACH(dev2, &pci_device_list, next) {
402 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
406 TAILQ_INSERT_BEFORE(dev2, dev, next);
408 } else { /* already registered */
409 dev2->kdrv = dev->kdrv;
410 dev2->max_vfs = dev->max_vfs;
411 memmove(dev2->mem_resource,
413 sizeof(dev->mem_resource));
418 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
429 * Scan the content of the PCI bus, and add the devices in the devices
430 * list. Call pci_scan_one() for each pci entry found.
436 unsigned dev_count = 0;
437 struct pci_conf matches[16];
438 struct pci_conf_io conf_io = {
442 .match_buf_len = sizeof(matches),
443 .matches = &matches[0],
446 fd = open("/dev/pci", O_RDONLY);
448 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
454 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
455 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
456 __func__, strerror(errno));
460 for (i = 0; i < conf_io.num_matches; i++)
461 if (pci_scan_one(fd, &matches[i]) < 0)
464 dev_count += conf_io.num_matches;
465 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
469 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
479 * If vendor/device ID match, call the devinit() function of the
483 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
485 const struct rte_pci_id *id_table;
488 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
490 /* check if device's identifiers match the driver's ones */
491 if (id_table->vendor_id != dev->id.vendor_id &&
492 id_table->vendor_id != PCI_ANY_ID)
494 if (id_table->device_id != dev->id.device_id &&
495 id_table->device_id != PCI_ANY_ID)
497 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
498 id_table->subsystem_vendor_id != PCI_ANY_ID)
500 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
501 id_table->subsystem_device_id != PCI_ANY_ID)
504 struct rte_pci_addr *loc = &dev->addr;
506 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
507 loc->domain, loc->bus, loc->devid, loc->function,
510 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
511 dev->id.device_id, dr->name);
513 /* no initialization when blacklisted, return without error */
514 if (dev->devargs != NULL &&
515 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
517 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
521 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
522 /* map resources for devices that use igb_uio */
523 ret = pci_uio_map_resource(dev);
526 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
527 rte_eal_process_type() == RTE_PROC_PRIMARY) {
528 /* unbind current driver */
529 if (pci_unbind_kernel_driver(dev) < 0)
533 /* reference driver structure */
536 /* call the driver devinit() function */
537 return dr->devinit(dr, dev);
539 /* return positive value if driver is not found */
543 /* Init the PCI EAL subsystem */
545 rte_eal_pci_init(void)
547 TAILQ_INIT(&pci_driver_list);
548 TAILQ_INIT(&pci_device_list);
550 /* for debug purposes, PCI can be disabled */
551 if (internal_config.no_pci)
554 if (pci_scan() < 0) {
555 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);