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.
39 #include <rte_eal_memconfig.h>
40 #include <rte_malloc.h>
41 #include <rte_devargs.h>
42 #include <rte_memcpy.h>
44 #include "eal_filesystem.h"
45 #include "eal_private.h"
46 #include "eal_pci_init.h"
50 * PCI probing under linux
52 * This code is used to simulate a PCI probe by parsing information in sysfs.
53 * When a registered device matches a driver, it is then initialized with
54 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
58 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
64 if (!filename || !dri_name)
67 count = readlink(filename, path, PATH_MAX);
68 if (count >= PATH_MAX)
71 /* For device does not have a driver */
77 name = strrchr(path, '/');
79 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
88 rte_eal_pci_map_device(struct rte_pci_device *dev)
92 /* try mapping the NIC resources using VFIO if it exists */
96 if (pci_vfio_is_enabled())
97 ret = pci_vfio_map_resource(dev);
100 case RTE_KDRV_IGB_UIO:
101 case RTE_KDRV_UIO_GENERIC:
102 if (rte_eal_using_phys_addrs()) {
103 /* map resources for devices that use uio */
104 ret = pci_uio_map_resource(dev);
109 " Not managed by a supported kernel driver, skipped\n");
117 /* Unmap pci device */
119 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
121 /* try unmapping the NIC resources using VFIO if it exists */
125 if (pci_vfio_is_enabled())
126 pci_vfio_unmap_resource(dev);
129 case RTE_KDRV_IGB_UIO:
130 case RTE_KDRV_UIO_GENERIC:
131 /* unmap resources for devices that use uio */
132 pci_uio_unmap_resource(dev);
136 " Not managed by a supported kernel driver, skipped\n");
142 pci_find_max_end_va(void)
144 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
145 const struct rte_memseg *last = seg;
148 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
149 if (seg->addr == NULL)
152 if (seg->addr > last->addr)
156 return RTE_PTR_ADD(last->addr, last->len);
159 /* parse one line of the "resource" sysfs file (note that the 'line'
160 * string is modified)
163 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
164 uint64_t *end_addr, uint64_t *flags)
166 union pci_resource_info {
172 char *ptrs[PCI_RESOURCE_FMT_NVAL];
175 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
177 "%s(): bad resource format\n", __func__);
181 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
182 *end_addr = strtoull(res_info.end_addr, NULL, 16);
183 *flags = strtoull(res_info.flags, NULL, 16);
186 "%s(): bad resource format\n", __func__);
193 /* parse the "resource" sysfs file */
195 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
200 uint64_t phys_addr, end_addr, flags;
202 f = fopen(filename, "r");
204 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
208 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
210 if (fgets(buf, sizeof(buf), f) == NULL) {
212 "%s(): cannot read resource\n", __func__);
215 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
216 &end_addr, &flags) < 0)
219 if (flags & IORESOURCE_MEM) {
220 dev->mem_resource[i].phys_addr = phys_addr;
221 dev->mem_resource[i].len = end_addr - phys_addr + 1;
222 /* not mapped for now */
223 dev->mem_resource[i].addr = NULL;
234 /* Scan one pci sysfs entry, and fill the devices list from it. */
236 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
238 char filename[PATH_MAX];
240 struct rte_pci_device *dev;
241 char driver[PATH_MAX];
244 dev = malloc(sizeof(*dev));
248 memset(dev, 0, sizeof(*dev));
252 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
253 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
257 dev->id.vendor_id = (uint16_t)tmp;
260 snprintf(filename, sizeof(filename), "%s/device", dirname);
261 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
265 dev->id.device_id = (uint16_t)tmp;
267 /* get subsystem_vendor id */
268 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
270 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
274 dev->id.subsystem_vendor_id = (uint16_t)tmp;
276 /* get subsystem_device id */
277 snprintf(filename, sizeof(filename), "%s/subsystem_device",
279 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
283 dev->id.subsystem_device_id = (uint16_t)tmp;
286 snprintf(filename, sizeof(filename), "%s/class",
288 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
292 /* the least 24 bits are valid: class, subclass, program interface */
293 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
297 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
298 if (!access(filename, F_OK) &&
299 eal_parse_sysfs_value(filename, &tmp) == 0)
300 dev->max_vfs = (uint16_t)tmp;
302 /* for non igb_uio driver, need kernel version >= 3.8 */
303 snprintf(filename, sizeof(filename),
304 "%s/sriov_numvfs", dirname);
305 if (!access(filename, F_OK) &&
306 eal_parse_sysfs_value(filename, &tmp) == 0)
307 dev->max_vfs = (uint16_t)tmp;
311 snprintf(filename, sizeof(filename), "%s/numa_node",
313 if (access(filename, R_OK) != 0) {
314 /* if no NUMA support, set default to 0 */
315 dev->device.numa_node = 0;
317 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
321 dev->device.numa_node = tmp;
324 /* parse resources */
325 snprintf(filename, sizeof(filename), "%s/resource", dirname);
326 if (pci_parse_sysfs_resource(filename, dev) < 0) {
327 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
333 snprintf(filename, sizeof(filename), "%s/driver", dirname);
334 ret = pci_get_kernel_driver_by_path(filename, driver);
336 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
342 if (!strcmp(driver, "vfio-pci"))
343 dev->kdrv = RTE_KDRV_VFIO;
344 else if (!strcmp(driver, "igb_uio"))
345 dev->kdrv = RTE_KDRV_IGB_UIO;
346 else if (!strcmp(driver, "uio_pci_generic"))
347 dev->kdrv = RTE_KDRV_UIO_GENERIC;
349 dev->kdrv = RTE_KDRV_UNKNOWN;
351 dev->kdrv = RTE_KDRV_NONE;
353 /* device is valid, add in list (sorted) */
354 if (TAILQ_EMPTY(&pci_device_list)) {
355 rte_eal_device_insert(&dev->device);
356 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
358 struct rte_pci_device *dev2;
361 TAILQ_FOREACH(dev2, &pci_device_list, next) {
362 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
367 TAILQ_INSERT_BEFORE(dev2, dev, next);
368 rte_eal_device_insert(&dev->device);
369 } else { /* already registered */
370 dev2->kdrv = dev->kdrv;
371 dev2->max_vfs = dev->max_vfs;
372 memmove(dev2->mem_resource, dev->mem_resource,
373 sizeof(dev->mem_resource));
378 rte_eal_device_insert(&dev->device);
379 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
386 pci_update_device(const struct rte_pci_addr *addr)
388 char filename[PATH_MAX];
390 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
391 pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
394 return pci_scan_one(filename, addr);
398 * split up a pci address into its constituent parts.
401 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
403 /* first split on ':' */
411 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
414 char *buf_copy = strndup(buf, bufsize);
415 if (buf_copy == NULL)
418 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
421 /* final split is on '.' between devid and function */
422 splitaddr.function = strchr(splitaddr.devid,'.');
423 if (splitaddr.function == NULL)
425 *splitaddr.function++ = '\0';
427 /* now convert to int values */
429 addr->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
430 addr->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
431 addr->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
432 addr->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
436 free(buf_copy); /* free the copy made with strdup */
444 * Scan the content of the PCI bus, and the devices in the devices
448 rte_eal_pci_scan(void)
452 char dirname[PATH_MAX];
453 struct rte_pci_addr addr;
455 dir = opendir(pci_get_sysfs_path());
457 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
458 __func__, strerror(errno));
462 while ((e = readdir(dir)) != NULL) {
463 if (e->d_name[0] == '.')
466 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
469 snprintf(dirname, sizeof(dirname), "%s/%s",
470 pci_get_sysfs_path(), e->d_name);
471 if (pci_scan_one(dirname, &addr) < 0)
482 /* Read PCI config space. */
483 int rte_eal_pci_read_config(const struct rte_pci_device *device,
484 void *buf, size_t len, off_t offset)
486 const struct rte_intr_handle *intr_handle = &device->intr_handle;
488 switch (intr_handle->type) {
489 case RTE_INTR_HANDLE_UIO:
490 case RTE_INTR_HANDLE_UIO_INTX:
491 return pci_uio_read_config(intr_handle, buf, len, offset);
494 case RTE_INTR_HANDLE_VFIO_MSIX:
495 case RTE_INTR_HANDLE_VFIO_MSI:
496 case RTE_INTR_HANDLE_VFIO_LEGACY:
497 return pci_vfio_read_config(intr_handle, buf, len, offset);
501 "Unknown handle type of fd %d\n",
507 /* Write PCI config space. */
508 int rte_eal_pci_write_config(const struct rte_pci_device *device,
509 const void *buf, size_t len, off_t offset)
511 const struct rte_intr_handle *intr_handle = &device->intr_handle;
513 switch (intr_handle->type) {
514 case RTE_INTR_HANDLE_UIO:
515 case RTE_INTR_HANDLE_UIO_INTX:
516 return pci_uio_write_config(intr_handle, buf, len, offset);
519 case RTE_INTR_HANDLE_VFIO_MSIX:
520 case RTE_INTR_HANDLE_VFIO_MSI:
521 case RTE_INTR_HANDLE_VFIO_LEGACY:
522 return pci_vfio_write_config(intr_handle, buf, len, offset);
526 "Unknown handle type of fd %d\n",
532 #if defined(RTE_ARCH_X86)
534 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
535 struct rte_pci_ioport *p)
544 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
545 dev->addr.domain, dev->addr.bus,
546 dev->addr.devid, dev->addr.function);
548 fp = fopen("/proc/ioports", "r");
550 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
554 while (getdelim(&line, &linesz, '\n', fp) > 0) {
559 n = strcspn(ptr, ":");
563 while (*left && isspace(*left))
566 if (!strncmp(left, pci_id, strlen(pci_id))) {
569 while (*ptr && isspace(*ptr))
572 sscanf(ptr, "%04hx-%04hx", &start, &end);
584 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
586 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
593 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
594 struct rte_pci_ioport *p)
601 if (pci_vfio_is_enabled())
602 ret = pci_vfio_ioport_map(dev, bar, p);
605 case RTE_KDRV_IGB_UIO:
606 ret = pci_uio_ioport_map(dev, bar, p);
608 case RTE_KDRV_UIO_GENERIC:
609 #if defined(RTE_ARCH_X86)
610 ret = pci_ioport_map(dev, bar, p);
612 ret = pci_uio_ioport_map(dev, bar, p);
616 #if defined(RTE_ARCH_X86)
617 ret = pci_ioport_map(dev, bar, p);
631 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
632 void *data, size_t len, off_t offset)
634 switch (p->dev->kdrv) {
637 pci_vfio_ioport_read(p, data, len, offset);
640 case RTE_KDRV_IGB_UIO:
641 pci_uio_ioport_read(p, data, len, offset);
643 case RTE_KDRV_UIO_GENERIC:
644 pci_uio_ioport_read(p, data, len, offset);
647 #if defined(RTE_ARCH_X86)
648 pci_uio_ioport_read(p, data, len, offset);
657 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
658 const void *data, size_t len, off_t offset)
660 switch (p->dev->kdrv) {
663 pci_vfio_ioport_write(p, data, len, offset);
666 case RTE_KDRV_IGB_UIO:
667 pci_uio_ioport_write(p, data, len, offset);
669 case RTE_KDRV_UIO_GENERIC:
670 pci_uio_ioport_write(p, data, len, offset);
673 #if defined(RTE_ARCH_X86)
674 pci_uio_ioport_write(p, data, len, offset);
683 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
687 switch (p->dev->kdrv) {
690 if (pci_vfio_is_enabled())
691 ret = pci_vfio_ioport_unmap(p);
694 case RTE_KDRV_IGB_UIO:
695 ret = pci_uio_ioport_unmap(p);
697 case RTE_KDRV_UIO_GENERIC:
698 #if defined(RTE_ARCH_X86)
701 ret = pci_uio_ioport_unmap(p);
705 #if defined(RTE_ARCH_X86)
716 /* Init the PCI EAL subsystem */
718 rte_eal_pci_init(void)
720 /* for debug purposes, PCI can be disabled */
721 if (internal_config.no_pci)
724 if (rte_eal_pci_scan() < 0) {
725 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);