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>
61 #include <rte_tailq.h>
63 #include <rte_eal_memconfig.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_malloc.h>
67 #include <rte_string_fns.h>
68 #include <rte_debug.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.
94 * For multi-process we need to reproduce all PCI mappings in secondary
95 * processes, so save them in a tailq.
98 TAILQ_ENTRY(uio_resource) next;
100 struct rte_pci_addr pci_addr;
103 struct uio_map maps[PCI_MAX_RESOURCE];
106 TAILQ_HEAD(uio_res_list, uio_resource);
108 static struct uio_res_list *uio_res_list = NULL;
110 /* forward prototype of function called in pci_switch_module below */
111 static int pci_uio_map_resource(struct rte_pci_device *dev);
113 /* map a particular resource from a file */
115 pci_map_resource(struct rte_pci_device *dev, void *requested_addr,
116 const char *devname, off_t offset, size_t size)
122 * open devname, to mmap it
124 fd = open(devname, O_RDWR);
126 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
127 devname, strerror(errno));
131 /* Map the PCI memory resource of device */
132 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
133 MAP_SHARED, fd, offset);
134 if (mapaddr == MAP_FAILED ||
135 (requested_addr != NULL && mapaddr != requested_addr)) {
136 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
137 " %s (%p)\n", __func__, devname, fd, requested_addr,
138 (unsigned long)size, (unsigned long)offset,
139 strerror(errno), mapaddr);
144 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
145 /* save fd if in primary process */
146 dev->intr_handle.fd = fd;
147 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
149 /* fd is not needed in slave process, close it */
150 dev->intr_handle.fd = -1;
151 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
155 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
160 dev->intr_handle.fd = -1;
161 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
167 #define OFF_MAX ((uint64_t)(off_t)-1)
171 pci_uio_map_secondary(struct rte_pci_device *dev)
174 struct uio_resource *uio_res;
176 TAILQ_FOREACH(uio_res, uio_res_list, next) {
178 /* skip this element if it doesn't match our PCI address */
179 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
182 for (i = 0; i != uio_res->nb_maps; i++) {
183 if (pci_map_resource(dev, uio_res->maps[i].addr,
185 (off_t)uio_res->maps[i].offset,
186 (size_t)uio_res->maps[i].size) !=
187 uio_res->maps[i].addr) {
189 "Cannot mmap device resource\n");
196 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
200 /* map the PCI resource of a PCI device in virtual memory */
202 pci_uio_map_resource(struct rte_pci_device *dev)
205 char devname[PATH_MAX]; /* contains the /dev/uioX */
210 struct rte_pci_addr *loc = &dev->addr;
211 struct uio_resource *uio_res;
212 struct uio_map *maps;
214 dev->intr_handle.fd = -1;
216 rte_snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
217 dev->addr.bus, dev->addr.devid, dev->addr.function);
219 if (access(devname, O_RDWR) < 0) {
220 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
221 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
225 /* secondary processes - use already recorded details */
226 if ((rte_eal_process_type() != RTE_PROC_PRIMARY) &&
227 (dev->id.vendor_id != PCI_VENDOR_ID_QUMRANET))
228 return (pci_uio_map_secondary(dev));
230 if(dev->id.vendor_id == PCI_VENDOR_ID_QUMRANET) {
231 /* I/O port address already assigned */
232 /* rte_virtio_pmd does not need any other bar even if available */
236 /* allocate the mapping details for secondary processes*/
237 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
239 "%s(): cannot store uio mmap details\n", __func__);
243 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
244 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
248 pagesz = sysconf(_SC_PAGESIZE);
250 maps = uio_res->maps;
251 for (i = uio_res->nb_maps = 0; i != PCI_MAX_RESOURCE; i++) {
253 j = uio_res->nb_maps;
255 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
258 /* if matching map is found, then use it */
260 maps[j].offset = offset;
261 maps[j].phaddr = dev->mem_resource[i].phys_addr;
262 maps[j].size = dev->mem_resource[i].len;
263 if (maps[j].addr != NULL ||
264 (mapaddr = pci_map_resource(dev,
265 NULL, devname, (off_t)offset,
266 (size_t)maps[j].size)) == NULL) {
271 maps[j].addr = mapaddr;
273 dev->mem_resource[i].addr = mapaddr;
276 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
281 /* parse the "resource" sysfs file */
282 #define IORESOURCE_MEM 0x00000200
284 /* Compare two PCI device addresses. */
286 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
288 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
289 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
291 if (dev_addr > dev_addr2)
298 /* Scan one pci sysfs entry, and fill the devices list from it. */
300 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
302 struct rte_pci_device *dev;
303 struct pci_bar_io bar;
306 dev = malloc(sizeof(*dev));
311 memset(dev, 0, sizeof(*dev));
312 dev->addr.domain = conf->pc_sel.pc_domain;
313 dev->addr.bus = conf->pc_sel.pc_bus;
314 dev->addr.devid = conf->pc_sel.pc_dev;
315 dev->addr.function = conf->pc_sel.pc_func;
318 dev->id.vendor_id = conf->pc_vendor;
321 dev->id.device_id = conf->pc_device;
323 /* get subsystem_vendor id */
324 dev->id.subsystem_vendor_id = conf->pc_subvendor;
326 /* get subsystem_device id */
327 dev->id.subsystem_device_id = conf->pc_subdevice;
329 /* TODO: get max_vfs */
332 /* FreeBSD has no NUMA support (yet) */
335 /* parse resources */
336 switch (conf->pc_hdr & PCIM_HDRTYPE) {
337 case PCIM_HDRTYPE_NORMAL:
338 max = PCIR_MAX_BAR_0;
340 case PCIM_HDRTYPE_BRIDGE:
341 max = PCIR_MAX_BAR_1;
343 case PCIM_HDRTYPE_CARDBUS:
344 max = PCIR_MAX_BAR_2;
350 for (i = 0; i <= max; i++) {
351 bar.pbi_sel = conf->pc_sel;
352 bar.pbi_reg = PCIR_BAR(i);
353 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
356 dev->mem_resource[i].len = bar.pbi_length;
357 if (PCI_BAR_IO(bar.pbi_base)) {
358 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
361 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
364 /* device is valid, add in list (sorted) */
365 if (TAILQ_EMPTY(&device_list)) {
366 TAILQ_INSERT_TAIL(&device_list, dev, next);
369 struct rte_pci_device *dev2 = NULL;
371 TAILQ_FOREACH(dev2, &device_list, next) {
372 if (pci_addr_comparison(&dev->addr, &dev2->addr))
375 TAILQ_INSERT_BEFORE(dev2, dev, next);
379 TAILQ_INSERT_TAIL(&device_list, dev, next);
390 * Scan the content of the PCI bus, and add the devices in the devices
391 * list. Call pci_scan_one() for each pci entry found.
397 unsigned dev_count = 0;
398 struct pci_conf matches[16];
399 struct pci_conf_io conf_io = {
403 .match_buf_len = sizeof(matches),
404 .matches = &matches[0],
407 fd = open("/dev/pci", O_RDONLY);
409 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
415 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
416 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
417 __func__, strerror(errno));
421 for (i = 0; i < conf_io.num_matches; i++)
422 if (pci_scan_one(fd, &matches[i]) < 0)
425 dev_count += conf_io.num_matches;
426 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
430 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
440 * If vendor/device ID match, call the devinit() function of the
444 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
446 struct rte_pci_id *id_table;
448 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
450 /* check if device's identifiers match the driver's ones */
451 if (id_table->vendor_id != dev->id.vendor_id &&
452 id_table->vendor_id != PCI_ANY_ID)
454 if (id_table->device_id != dev->id.device_id &&
455 id_table->device_id != PCI_ANY_ID)
457 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
458 id_table->subsystem_vendor_id != PCI_ANY_ID)
460 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
461 id_table->subsystem_device_id != PCI_ANY_ID)
464 struct rte_pci_addr *loc = &dev->addr;
466 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
467 loc->domain, loc->bus, loc->devid, loc->function,
470 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
471 dev->id.device_id, dr->name);
473 /* no initialization when blacklisted, return without error */
474 if (dev->blacklisted) {
475 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
479 /* just map the NIC resources */
480 if (pci_uio_map_resource(dev) < 0)
483 /* We always should have BAR0 mapped */
484 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
485 dev->mem_resource[0].addr == NULL) {
487 "%s(): BAR0 is not mapped\n",
492 /* reference driver structure */
495 /* call the driver devinit() function */
496 return dr->devinit(dr, dev);
498 /* return positive value if driver is not found */
502 /* Init the PCI EAL subsystem */
504 rte_eal_pci_init(void)
506 TAILQ_INIT(&driver_list);
507 TAILQ_INIT(&device_list);
508 uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
510 /* for debug purposes, PCI can be disabled */
511 if (internal_config.no_pci)
514 if (pci_scan() < 0) {
515 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);