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.
95 * For multi-process we need to reproduce all PCI mappings in secondary
96 * processes, so save them in a tailq.
98 struct mapped_pci_resource {
99 TAILQ_ENTRY(mapped_pci_resource) next;
101 struct rte_pci_addr pci_addr;
104 struct pci_map maps[PCI_MAX_RESOURCE];
107 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
109 static struct rte_tailq_elem rte_uio_tailq = {
110 .name = "UIO_RESOURCE_LIST",
112 EAL_REGISTER_TAILQ(rte_uio_tailq)
114 /* unbind kernel driver for this device */
116 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
118 RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
123 /* map a particular resource from a file */
125 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
126 int additional_flags)
130 /* Map the PCI memory resource of device */
131 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
132 MAP_SHARED | additional_flags, fd, offset);
133 if (mapaddr == MAP_FAILED) {
135 "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
136 __func__, fd, requested_addr,
137 (unsigned long)size, (unsigned long)offset,
138 strerror(errno), mapaddr);
140 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
146 pci_uio_map_secondary(struct rte_pci_device *dev)
149 struct mapped_pci_resource *uio_res;
150 struct mapped_pci_res_list *uio_res_list =
151 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
153 TAILQ_FOREACH(uio_res, uio_res_list, next) {
155 /* skip this element if it doesn't match our PCI address */
156 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
159 for (i = 0; i != uio_res->nb_maps; i++) {
161 * open devname, to mmap it
163 fd = open(uio_res->maps[i].path, O_RDWR);
165 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
166 uio_res->maps[i].path, strerror(errno));
170 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
171 fd, (off_t)uio_res->maps[i].offset,
172 (size_t)uio_res->maps[i].size, 0);
173 /* fd is not needed in slave process, close it */
175 if (mapaddr != uio_res->maps[i].addr) {
177 "Cannot mmap device resource file %s to address: %p\n",
178 uio_res->maps[i].path,
179 uio_res->maps[i].addr);
186 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
191 pci_uio_free_resource(struct rte_pci_device *dev,
192 struct mapped_pci_resource *uio_res)
196 if (dev->intr_handle.fd) {
197 close(dev->intr_handle.fd);
198 dev->intr_handle.fd = -1;
199 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
204 pci_uio_alloc_resource(struct rte_pci_device *dev,
205 struct mapped_pci_resource **uio_res)
207 char devname[PATH_MAX]; /* contains the /dev/uioX */
208 struct rte_pci_addr *loc;
212 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
213 dev->addr.bus, dev->addr.devid, dev->addr.function);
215 if (access(devname, O_RDWR) < 0) {
216 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
217 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
221 /* save fd if in primary process */
222 dev->intr_handle.fd = open(devname, O_RDWR);
223 if (dev->intr_handle.fd < 0) {
224 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
225 devname, strerror(errno));
228 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
230 /* allocate the mapping details for secondary processes*/
231 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
232 if (*uio_res == NULL) {
234 "%s(): cannot store uio mmap details\n", __func__);
238 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
239 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
244 pci_uio_free_resource(dev, *uio_res);
248 /* map the PCI resource of a PCI device in virtual memory */
250 pci_uio_map_resource(struct rte_pci_device *dev)
252 int i, map_idx = 0, ret;
258 struct mapped_pci_resource *uio_res = NULL;
259 struct mapped_pci_res_list *uio_res_list =
260 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
261 struct pci_map *maps;
263 dev->intr_handle.fd = -1;
264 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
266 /* secondary processes - use already recorded details */
267 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
268 return pci_uio_map_secondary(dev);
270 /* allocate uio resource */
271 ret = pci_uio_alloc_resource(dev, &uio_res);
276 pagesz = sysconf(_SC_PAGESIZE);
277 devname = uio_res->path;
279 maps = uio_res->maps;
280 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
284 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
287 /* allocate memory to keep path */
288 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
289 if (maps[map_idx].path == NULL) {
290 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
296 * open resource file, to mmap it
298 fd = open(devname, O_RDWR);
300 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
301 devname, strerror(errno));
302 rte_free(maps[map_idx].path);
306 /* if matching map is found, then use it */
308 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
309 (size_t)dev->mem_resource[i].len, 0);
311 if (mapaddr == MAP_FAILED) {
312 rte_free(maps[map_idx].path);
316 maps[map_idx].phaddr = dev->mem_resource[i].phys_addr;
317 maps[map_idx].size = dev->mem_resource[i].len;
318 maps[map_idx].addr = mapaddr;
319 maps[map_idx].offset = offset;
320 strcpy(maps[map_idx].path, devname);
322 dev->mem_resource[i].addr = mapaddr;
325 uio_res->nb_maps = map_idx;
327 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
331 for (i = 0; i < map_idx; i++)
332 rte_free(maps[i].path);
333 pci_uio_free_resource(dev, uio_res);
337 /* Scan one pci sysfs entry, and fill the devices list from it. */
339 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
341 struct rte_pci_device *dev;
342 struct pci_bar_io bar;
345 dev = malloc(sizeof(*dev));
350 memset(dev, 0, sizeof(*dev));
351 dev->addr.domain = conf->pc_sel.pc_domain;
352 dev->addr.bus = conf->pc_sel.pc_bus;
353 dev->addr.devid = conf->pc_sel.pc_dev;
354 dev->addr.function = conf->pc_sel.pc_func;
357 dev->id.vendor_id = conf->pc_vendor;
360 dev->id.device_id = conf->pc_device;
362 /* get subsystem_vendor id */
363 dev->id.subsystem_vendor_id = conf->pc_subvendor;
365 /* get subsystem_device id */
366 dev->id.subsystem_device_id = conf->pc_subdevice;
368 /* TODO: get max_vfs */
371 /* FreeBSD has no NUMA support (yet) */
374 /* parse resources */
375 switch (conf->pc_hdr & PCIM_HDRTYPE) {
376 case PCIM_HDRTYPE_NORMAL:
377 max = PCIR_MAX_BAR_0;
379 case PCIM_HDRTYPE_BRIDGE:
380 max = PCIR_MAX_BAR_1;
382 case PCIM_HDRTYPE_CARDBUS:
383 max = PCIR_MAX_BAR_2;
389 for (i = 0; i <= max; i++) {
390 bar.pbi_sel = conf->pc_sel;
391 bar.pbi_reg = PCIR_BAR(i);
392 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
395 dev->mem_resource[i].len = bar.pbi_length;
396 if (PCI_BAR_IO(bar.pbi_base)) {
397 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
400 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
403 /* device is valid, add in list (sorted) */
404 if (TAILQ_EMPTY(&pci_device_list)) {
405 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
408 struct rte_pci_device *dev2 = NULL;
411 TAILQ_FOREACH(dev2, &pci_device_list, next) {
412 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
416 TAILQ_INSERT_BEFORE(dev2, dev, next);
418 } else { /* already registered */
419 dev2->kdrv = dev->kdrv;
420 dev2->max_vfs = dev->max_vfs;
421 memmove(dev2->mem_resource,
423 sizeof(dev->mem_resource));
428 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
439 * Scan the content of the PCI bus, and add the devices in the devices
440 * list. Call pci_scan_one() for each pci entry found.
446 unsigned dev_count = 0;
447 struct pci_conf matches[16];
448 struct pci_conf_io conf_io = {
452 .match_buf_len = sizeof(matches),
453 .matches = &matches[0],
456 fd = open("/dev/pci", O_RDONLY);
458 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
464 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
465 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
466 __func__, strerror(errno));
470 for (i = 0; i < conf_io.num_matches; i++)
471 if (pci_scan_one(fd, &matches[i]) < 0)
474 dev_count += conf_io.num_matches;
475 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
479 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
489 * If vendor/device ID match, call the devinit() function of the
493 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
495 const struct rte_pci_id *id_table;
498 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
500 /* check if device's identifiers match the driver's ones */
501 if (id_table->vendor_id != dev->id.vendor_id &&
502 id_table->vendor_id != PCI_ANY_ID)
504 if (id_table->device_id != dev->id.device_id &&
505 id_table->device_id != PCI_ANY_ID)
507 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
508 id_table->subsystem_vendor_id != PCI_ANY_ID)
510 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
511 id_table->subsystem_device_id != PCI_ANY_ID)
514 struct rte_pci_addr *loc = &dev->addr;
516 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
517 loc->domain, loc->bus, loc->devid, loc->function,
520 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
521 dev->id.device_id, dr->name);
523 /* no initialization when blacklisted, return without error */
524 if (dev->devargs != NULL &&
525 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
527 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
531 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
532 /* map resources for devices that use igb_uio */
533 ret = pci_uio_map_resource(dev);
536 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
537 rte_eal_process_type() == RTE_PROC_PRIMARY) {
538 /* unbind current driver */
539 if (pci_unbind_kernel_driver(dev) < 0)
543 /* reference driver structure */
546 /* call the driver devinit() function */
547 return dr->devinit(dr, dev);
549 /* return positive value if driver is not found */
553 /* Init the PCI EAL subsystem */
555 rte_eal_pci_init(void)
557 TAILQ_INIT(&pci_driver_list);
558 TAILQ_INIT(&pci_device_list);
560 /* for debug purposes, PCI can be disabled */
561 if (internal_config.no_pci)
564 if (pci_scan() < 0) {
565 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);