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 #if defined(RTE_ARCH_X86)
55 #include <sys/types.h>
56 #include <machine/cpufunc.h>
59 #include <rte_interrupts.h>
62 #include <rte_common.h>
63 #include <rte_launch.h>
64 #include <rte_memory.h>
65 #include <rte_memzone.h>
67 #include <rte_eal_memconfig.h>
68 #include <rte_per_lcore.h>
69 #include <rte_lcore.h>
70 #include <rte_malloc.h>
71 #include <rte_string_fns.h>
72 #include <rte_debug.h>
73 #include <rte_devargs.h>
75 #include "eal_filesystem.h"
76 #include "eal_private.h"
80 * PCI probing under linux
82 * This code is used to simulate a PCI probe by parsing information in
83 * sysfs. Moreover, when a registered driver matches a device, the
84 * kernel driver currently using it is unloaded and replaced by
85 * igb_uio module, which is a very minimal userland driver for Intel
86 * network card, only providing access to PCI BAR to applications, and
87 * enabling bus master.
90 extern struct rte_pci_bus rte_pci_bus;
94 rte_pci_map_device(struct rte_pci_device *dev)
98 /* try mapping the NIC resources */
100 case RTE_KDRV_NIC_UIO:
101 /* map resources for devices that use uio */
102 ret = pci_uio_map_resource(dev);
106 " Not managed by a supported kernel driver, skipped\n");
114 /* Unmap pci device */
116 rte_pci_unmap_device(struct rte_pci_device *dev)
118 /* try unmapping the NIC resources */
120 case RTE_KDRV_NIC_UIO:
121 /* unmap resources for devices that use uio */
122 pci_uio_unmap_resource(dev);
126 " Not managed by a supported kernel driver, skipped\n");
132 pci_uio_free_resource(struct rte_pci_device *dev,
133 struct mapped_pci_resource *uio_res)
137 if (dev->intr_handle.fd) {
138 close(dev->intr_handle.fd);
139 dev->intr_handle.fd = -1;
140 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
145 pci_uio_alloc_resource(struct rte_pci_device *dev,
146 struct mapped_pci_resource **uio_res)
148 char devname[PATH_MAX]; /* contains the /dev/uioX */
149 struct rte_pci_addr *loc;
153 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
154 dev->addr.bus, dev->addr.devid, dev->addr.function);
156 if (access(devname, O_RDWR) < 0) {
157 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
158 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
162 /* save fd if in primary process */
163 dev->intr_handle.fd = open(devname, O_RDWR);
164 if (dev->intr_handle.fd < 0) {
165 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
166 devname, strerror(errno));
169 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
171 /* allocate the mapping details for secondary processes*/
172 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
173 if (*uio_res == NULL) {
175 "%s(): cannot store uio mmap details\n", __func__);
179 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
180 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
185 pci_uio_free_resource(dev, *uio_res);
190 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
191 struct mapped_pci_resource *uio_res, int map_idx)
198 struct pci_map *maps;
200 maps = uio_res->maps;
201 devname = uio_res->path;
202 pagesz = sysconf(_SC_PAGESIZE);
204 /* allocate memory to keep path */
205 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
206 if (maps[map_idx].path == NULL) {
207 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
213 * open resource file, to mmap it
215 fd = open(devname, O_RDWR);
217 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
218 devname, strerror(errno));
222 /* if matching map is found, then use it */
223 offset = res_idx * pagesz;
224 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
225 (size_t)dev->mem_resource[res_idx].len, 0);
227 if (mapaddr == MAP_FAILED)
230 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
231 maps[map_idx].size = dev->mem_resource[res_idx].len;
232 maps[map_idx].addr = mapaddr;
233 maps[map_idx].offset = offset;
234 strcpy(maps[map_idx].path, devname);
235 dev->mem_resource[res_idx].addr = mapaddr;
240 rte_free(maps[map_idx].path);
245 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
247 struct rte_pci_device *dev;
248 struct pci_bar_io bar;
251 dev = malloc(sizeof(*dev));
256 memset(dev, 0, sizeof(*dev));
257 dev->addr.domain = conf->pc_sel.pc_domain;
258 dev->addr.bus = conf->pc_sel.pc_bus;
259 dev->addr.devid = conf->pc_sel.pc_dev;
260 dev->addr.function = conf->pc_sel.pc_func;
263 dev->id.vendor_id = conf->pc_vendor;
266 dev->id.device_id = conf->pc_device;
268 /* get subsystem_vendor id */
269 dev->id.subsystem_vendor_id = conf->pc_subvendor;
271 /* get subsystem_device id */
272 dev->id.subsystem_device_id = conf->pc_subdevice;
275 dev->id.class_id = (conf->pc_class << 16) |
276 (conf->pc_subclass << 8) |
279 /* TODO: get max_vfs */
282 /* FreeBSD has no NUMA support (yet) */
283 dev->device.numa_node = 0;
285 rte_pci_device_name(&dev->addr, dev->name, sizeof(dev->name));
286 dev->device.name = dev->name;
288 /* FreeBSD has only one pass through driver */
289 dev->kdrv = RTE_KDRV_NIC_UIO;
291 /* parse resources */
292 switch (conf->pc_hdr & PCIM_HDRTYPE) {
293 case PCIM_HDRTYPE_NORMAL:
294 max = PCIR_MAX_BAR_0;
296 case PCIM_HDRTYPE_BRIDGE:
297 max = PCIR_MAX_BAR_1;
299 case PCIM_HDRTYPE_CARDBUS:
300 max = PCIR_MAX_BAR_2;
306 for (i = 0; i <= max; i++) {
307 bar.pbi_sel = conf->pc_sel;
308 bar.pbi_reg = PCIR_BAR(i);
309 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
312 dev->mem_resource[i].len = bar.pbi_length;
313 if (PCI_BAR_IO(bar.pbi_base)) {
314 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
317 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
320 /* device is valid, add in list (sorted) */
321 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
322 rte_pci_add_device(dev);
325 struct rte_pci_device *dev2 = NULL;
328 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
329 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
333 rte_pci_insert_device(dev2, dev);
334 } else { /* already registered */
335 dev2->kdrv = dev->kdrv;
336 dev2->max_vfs = dev->max_vfs;
337 memmove(dev2->mem_resource,
339 sizeof(dev->mem_resource));
344 rte_pci_add_device(dev);
355 * Scan the content of the PCI bus, and add the devices in the devices
356 * list. Call pci_scan_one() for each pci entry found.
362 unsigned dev_count = 0;
363 struct pci_conf matches[16];
364 struct pci_conf_io conf_io = {
368 .match_buf_len = sizeof(matches),
369 .matches = &matches[0],
372 /* for debug purposes, PCI can be disabled */
373 if (internal_config.no_pci)
376 fd = open("/dev/pci", O_RDONLY);
378 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
384 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
385 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
386 __func__, strerror(errno));
390 for (i = 0; i < conf_io.num_matches; i++)
391 if (pci_scan_one(fd, &matches[i]) < 0)
394 dev_count += conf_io.num_matches;
395 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
399 RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
409 pci_update_device(const struct rte_pci_addr *addr)
412 struct pci_conf matches[2];
413 struct pci_match_conf match = {
415 .pc_domain = addr->domain,
417 .pc_dev = addr->devid,
418 .pc_func = addr->function,
421 struct pci_conf_io conf_io = {
425 .match_buf_len = sizeof(matches),
426 .matches = &matches[0],
429 fd = open("/dev/pci", O_RDONLY);
431 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
435 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
436 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
437 __func__, strerror(errno));
441 if (conf_io.num_matches != 1)
444 if (pci_scan_one(fd, &matches[0]) < 0)
457 /* Read PCI config space. */
458 int rte_pci_read_config(const struct rte_pci_device *dev,
459 void *buf, size_t len, off_t offset)
464 .pc_domain = dev->addr.domain,
465 .pc_bus = dev->addr.bus,
466 .pc_dev = dev->addr.devid,
467 .pc_func = dev->addr.function,
473 if (len == 3 || len > sizeof(pi.pi_data)) {
474 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
478 fd = open("/dev/pci", O_RDWR);
480 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
484 if (ioctl(fd, PCIOCREAD, &pi) < 0)
488 memcpy(buf, &pi.pi_data, len);
497 /* Write PCI config space. */
498 int rte_pci_write_config(const struct rte_pci_device *dev,
499 const void *buf, size_t len, off_t offset)
505 .pc_domain = dev->addr.domain,
506 .pc_bus = dev->addr.bus,
507 .pc_dev = dev->addr.devid,
508 .pc_func = dev->addr.function,
511 .pi_data = *(const uint32_t *)buf,
515 if (len == 3 || len > sizeof(pi.pi_data)) {
516 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
520 memcpy(&pi.pi_data, buf, len);
522 fd = open("/dev/pci", O_RDWR);
524 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
528 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
541 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
542 struct rte_pci_ioport *p)
547 #if defined(RTE_ARCH_X86)
548 case RTE_KDRV_NIC_UIO:
549 if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
550 p->base = (uintptr_t)dev->mem_resource[bar].addr;
568 pci_uio_ioport_read(struct rte_pci_ioport *p,
569 void *data, size_t len, off_t offset)
571 #if defined(RTE_ARCH_X86)
574 unsigned short reg = p->base + offset;
576 for (d = data; len > 0; d += size, reg += size, len -= size) {
579 *(uint32_t *)d = inl(reg);
580 } else if (len >= 2) {
582 *(uint16_t *)d = inw(reg);
592 RTE_SET_USED(offset);
597 rte_pci_ioport_read(struct rte_pci_ioport *p,
598 void *data, size_t len, off_t offset)
600 switch (p->dev->kdrv) {
601 case RTE_KDRV_NIC_UIO:
602 pci_uio_ioport_read(p, data, len, offset);
610 pci_uio_ioport_write(struct rte_pci_ioport *p,
611 const void *data, size_t len, off_t offset)
613 #if defined(RTE_ARCH_X86)
616 unsigned short reg = p->base + offset;
618 for (s = data; len > 0; s += size, reg += size, len -= size) {
621 outl(reg, *(const uint32_t *)s);
622 } else if (len >= 2) {
624 outw(reg, *(const uint16_t *)s);
634 RTE_SET_USED(offset);
639 rte_pci_ioport_write(struct rte_pci_ioport *p,
640 const void *data, size_t len, off_t offset)
642 switch (p->dev->kdrv) {
643 case RTE_KDRV_NIC_UIO:
644 pci_uio_ioport_write(p, data, len, offset);
652 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
656 switch (p->dev->kdrv) {
657 #if defined(RTE_ARCH_X86)
658 case RTE_KDRV_NIC_UIO: