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.
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 rte_tailq_elem rte_uio_tailq = {
109 .name = "UIO_RESOURCE_LIST",
111 EAL_REGISTER_TAILQ(rte_uio_tailq)
113 /* unbind kernel driver for this device */
115 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
117 RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
122 /* map a particular resource from a file */
124 pci_map_resource(void *requested_addr, const char *devname, off_t offset,
131 * open devname, to mmap it
133 fd = open(devname, O_RDWR);
135 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
136 devname, strerror(errno));
140 /* Map the PCI memory resource of device */
141 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
142 MAP_SHARED, fd, offset);
144 if (mapaddr == MAP_FAILED ||
145 (requested_addr != NULL && mapaddr != requested_addr)) {
146 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
147 " %s (%p)\n", __func__, devname, fd, requested_addr,
148 (unsigned long)size, (unsigned long)offset,
149 strerror(errno), mapaddr);
153 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
162 pci_uio_map_secondary(struct rte_pci_device *dev)
165 struct uio_resource *uio_res;
166 struct uio_res_list *uio_res_list =
167 RTE_TAILQ_CAST(rte_uio_tailq.head, uio_res_list);
169 TAILQ_FOREACH(uio_res, uio_res_list, next) {
171 /* skip this element if it doesn't match our PCI address */
172 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
175 for (i = 0; i != uio_res->nb_maps; i++) {
176 if (pci_map_resource(uio_res->maps[i].addr,
178 (off_t)uio_res->maps[i].offset,
179 (size_t)uio_res->maps[i].size)
180 != uio_res->maps[i].addr) {
182 "Cannot mmap device resource\n");
189 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
193 /* map the PCI resource of a PCI device in virtual memory */
195 pci_uio_map_resource(struct rte_pci_device *dev)
198 char devname[PATH_MAX]; /* contains the /dev/uioX */
203 struct rte_pci_addr *loc = &dev->addr;
204 struct uio_resource *uio_res;
205 struct uio_res_list *uio_res_list =
206 RTE_TAILQ_CAST(rte_uio_tailq.head, uio_res_list);
207 struct uio_map *maps;
209 dev->intr_handle.fd = -1;
210 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
212 /* secondary processes - use already recorded details */
213 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
214 return pci_uio_map_secondary(dev);
216 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 /* save fd if in primary process */
226 dev->intr_handle.fd = open(devname, O_RDWR);
227 if (dev->intr_handle.fd < 0) {
228 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
229 devname, strerror(errno));
232 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
234 /* allocate the mapping details for secondary processes*/
235 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
237 "%s(): cannot store uio mmap details\n", __func__);
241 snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
242 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
246 pagesz = sysconf(_SC_PAGESIZE);
248 maps = uio_res->maps;
249 for (i = uio_res->nb_maps = 0; i != PCI_MAX_RESOURCE; i++) {
251 j = uio_res->nb_maps;
253 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
256 /* if matching map is found, then use it */
258 maps[j].offset = offset;
259 maps[j].phaddr = dev->mem_resource[i].phys_addr;
260 maps[j].size = dev->mem_resource[i].len;
261 if (maps[j].addr != NULL ||
262 (mapaddr = pci_map_resource(NULL, devname, (off_t)offset,
263 (size_t)maps[j].size)
269 maps[j].addr = mapaddr;
271 dev->mem_resource[i].addr = mapaddr;
274 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
279 /* Scan one pci sysfs entry, and fill the devices list from it. */
281 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
283 struct rte_pci_device *dev;
284 struct pci_bar_io bar;
287 dev = malloc(sizeof(*dev));
292 memset(dev, 0, sizeof(*dev));
293 dev->addr.domain = conf->pc_sel.pc_domain;
294 dev->addr.bus = conf->pc_sel.pc_bus;
295 dev->addr.devid = conf->pc_sel.pc_dev;
296 dev->addr.function = conf->pc_sel.pc_func;
299 dev->id.vendor_id = conf->pc_vendor;
302 dev->id.device_id = conf->pc_device;
304 /* get subsystem_vendor id */
305 dev->id.subsystem_vendor_id = conf->pc_subvendor;
307 /* get subsystem_device id */
308 dev->id.subsystem_device_id = conf->pc_subdevice;
310 /* TODO: get max_vfs */
313 /* FreeBSD has no NUMA support (yet) */
316 /* parse resources */
317 switch (conf->pc_hdr & PCIM_HDRTYPE) {
318 case PCIM_HDRTYPE_NORMAL:
319 max = PCIR_MAX_BAR_0;
321 case PCIM_HDRTYPE_BRIDGE:
322 max = PCIR_MAX_BAR_1;
324 case PCIM_HDRTYPE_CARDBUS:
325 max = PCIR_MAX_BAR_2;
331 for (i = 0; i <= max; i++) {
332 bar.pbi_sel = conf->pc_sel;
333 bar.pbi_reg = PCIR_BAR(i);
334 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
337 dev->mem_resource[i].len = bar.pbi_length;
338 if (PCI_BAR_IO(bar.pbi_base)) {
339 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
342 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
345 /* device is valid, add in list (sorted) */
346 if (TAILQ_EMPTY(&pci_device_list)) {
347 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
350 struct rte_pci_device *dev2 = NULL;
353 TAILQ_FOREACH(dev2, &pci_device_list, next) {
354 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
358 TAILQ_INSERT_BEFORE(dev2, dev, next);
360 } else { /* already registered */
361 dev2->kdrv = dev->kdrv;
362 dev2->max_vfs = dev->max_vfs;
363 memmove(dev2->mem_resource,
365 sizeof(dev->mem_resource));
370 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
381 * Scan the content of the PCI bus, and add the devices in the devices
382 * list. Call pci_scan_one() for each pci entry found.
388 unsigned dev_count = 0;
389 struct pci_conf matches[16];
390 struct pci_conf_io conf_io = {
394 .match_buf_len = sizeof(matches),
395 .matches = &matches[0],
398 fd = open("/dev/pci", O_RDONLY);
400 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
406 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
407 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
408 __func__, strerror(errno));
412 for (i = 0; i < conf_io.num_matches; i++)
413 if (pci_scan_one(fd, &matches[i]) < 0)
416 dev_count += conf_io.num_matches;
417 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
421 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
431 * If vendor/device ID match, call the devinit() function of the
435 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
437 const struct rte_pci_id *id_table;
440 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
442 /* check if device's identifiers match the driver's ones */
443 if (id_table->vendor_id != dev->id.vendor_id &&
444 id_table->vendor_id != PCI_ANY_ID)
446 if (id_table->device_id != dev->id.device_id &&
447 id_table->device_id != PCI_ANY_ID)
449 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
450 id_table->subsystem_vendor_id != PCI_ANY_ID)
452 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
453 id_table->subsystem_device_id != PCI_ANY_ID)
456 struct rte_pci_addr *loc = &dev->addr;
458 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
459 loc->domain, loc->bus, loc->devid, loc->function,
462 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
463 dev->id.device_id, dr->name);
465 /* no initialization when blacklisted, return without error */
466 if (dev->devargs != NULL &&
467 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
469 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
473 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
474 /* map resources for devices that use igb_uio */
475 ret = pci_uio_map_resource(dev);
478 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
479 rte_eal_process_type() == RTE_PROC_PRIMARY) {
480 /* unbind current driver */
481 if (pci_unbind_kernel_driver(dev) < 0)
485 /* reference driver structure */
488 /* call the driver devinit() function */
489 return dr->devinit(dr, dev);
491 /* return positive value if driver is not found */
495 /* Init the PCI EAL subsystem */
497 rte_eal_pci_init(void)
499 TAILQ_INIT(&pci_driver_list);
500 TAILQ_INIT(&pci_device_list);
502 /* for debug purposes, PCI can be disabled */
503 if (internal_config.no_pci)
506 if (pci_scan() < 0) {
507 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);