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 */
124 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
126 case RTE_KDRV_IGB_UIO:
127 case RTE_KDRV_UIO_GENERIC:
128 /* unmap resources for devices that use uio */
129 pci_uio_unmap_resource(dev);
133 " Not managed by a supported kernel driver, skipped\n");
139 pci_find_max_end_va(void)
141 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
142 const struct rte_memseg *last = seg;
145 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
146 if (seg->addr == NULL)
149 if (seg->addr > last->addr)
153 return RTE_PTR_ADD(last->addr, last->len);
156 /* parse one line of the "resource" sysfs file (note that the 'line'
157 * string is modified)
160 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
161 uint64_t *end_addr, uint64_t *flags)
163 union pci_resource_info {
169 char *ptrs[PCI_RESOURCE_FMT_NVAL];
172 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
174 "%s(): bad resource format\n", __func__);
178 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
179 *end_addr = strtoull(res_info.end_addr, NULL, 16);
180 *flags = strtoull(res_info.flags, NULL, 16);
183 "%s(): bad resource format\n", __func__);
190 /* parse the "resource" sysfs file */
192 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
197 uint64_t phys_addr, end_addr, flags;
199 f = fopen(filename, "r");
201 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
205 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
207 if (fgets(buf, sizeof(buf), f) == NULL) {
209 "%s(): cannot read resource\n", __func__);
212 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
213 &end_addr, &flags) < 0)
216 if (flags & IORESOURCE_MEM) {
217 dev->mem_resource[i].phys_addr = phys_addr;
218 dev->mem_resource[i].len = end_addr - phys_addr + 1;
219 /* not mapped for now */
220 dev->mem_resource[i].addr = NULL;
231 /* Scan one pci sysfs entry, and fill the devices list from it. */
233 pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
235 char filename[PATH_MAX];
237 struct rte_pci_device *dev;
238 char driver[PATH_MAX];
241 dev = malloc(sizeof(*dev));
245 memset(dev, 0, sizeof(*dev));
249 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
250 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
254 dev->id.vendor_id = (uint16_t)tmp;
257 snprintf(filename, sizeof(filename), "%s/device", dirname);
258 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
262 dev->id.device_id = (uint16_t)tmp;
264 /* get subsystem_vendor id */
265 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
267 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
271 dev->id.subsystem_vendor_id = (uint16_t)tmp;
273 /* get subsystem_device id */
274 snprintf(filename, sizeof(filename), "%s/subsystem_device",
276 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
280 dev->id.subsystem_device_id = (uint16_t)tmp;
283 snprintf(filename, sizeof(filename), "%s/class",
285 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
289 /* the least 24 bits are valid: class, subclass, program interface */
290 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
294 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
295 if (!access(filename, F_OK) &&
296 eal_parse_sysfs_value(filename, &tmp) == 0)
297 dev->max_vfs = (uint16_t)tmp;
299 /* for non igb_uio driver, need kernel version >= 3.8 */
300 snprintf(filename, sizeof(filename),
301 "%s/sriov_numvfs", dirname);
302 if (!access(filename, F_OK) &&
303 eal_parse_sysfs_value(filename, &tmp) == 0)
304 dev->max_vfs = (uint16_t)tmp;
308 snprintf(filename, sizeof(filename), "%s/numa_node",
310 if (access(filename, R_OK) != 0) {
311 /* if no NUMA support, set default to 0 */
312 dev->device.numa_node = 0;
314 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
318 dev->device.numa_node = tmp;
321 /* parse resources */
322 snprintf(filename, sizeof(filename), "%s/resource", dirname);
323 if (pci_parse_sysfs_resource(filename, dev) < 0) {
324 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
330 snprintf(filename, sizeof(filename), "%s/driver", dirname);
331 ret = pci_get_kernel_driver_by_path(filename, driver);
333 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
339 if (!strcmp(driver, "vfio-pci"))
340 dev->kdrv = RTE_KDRV_VFIO;
341 else if (!strcmp(driver, "igb_uio"))
342 dev->kdrv = RTE_KDRV_IGB_UIO;
343 else if (!strcmp(driver, "uio_pci_generic"))
344 dev->kdrv = RTE_KDRV_UIO_GENERIC;
346 dev->kdrv = RTE_KDRV_UNKNOWN;
348 dev->kdrv = RTE_KDRV_NONE;
350 /* device is valid, add in list (sorted) */
351 if (TAILQ_EMPTY(&pci_device_list)) {
352 rte_eal_device_insert(&dev->device);
353 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
355 struct rte_pci_device *dev2;
358 TAILQ_FOREACH(dev2, &pci_device_list, next) {
359 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
364 TAILQ_INSERT_BEFORE(dev2, dev, next);
365 rte_eal_device_insert(&dev->device);
366 } else { /* already registered */
367 dev2->kdrv = dev->kdrv;
368 dev2->max_vfs = dev->max_vfs;
369 memmove(dev2->mem_resource, dev->mem_resource,
370 sizeof(dev->mem_resource));
375 rte_eal_device_insert(&dev->device);
376 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
383 pci_update_device(const struct rte_pci_addr *addr)
385 char filename[PATH_MAX];
387 snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
388 pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
391 return pci_scan_one(filename, addr);
395 * split up a pci address into its constituent parts.
398 parse_pci_addr_format(const char *buf, int bufsize, struct rte_pci_addr *addr)
400 /* first split on ':' */
408 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
411 char *buf_copy = strndup(buf, bufsize);
412 if (buf_copy == NULL)
415 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
418 /* final split is on '.' between devid and function */
419 splitaddr.function = strchr(splitaddr.devid,'.');
420 if (splitaddr.function == NULL)
422 *splitaddr.function++ = '\0';
424 /* now convert to int values */
426 addr->domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
427 addr->bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
428 addr->devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
429 addr->function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
433 free(buf_copy); /* free the copy made with strdup */
441 * Scan the content of the PCI bus, and the devices in the devices
445 rte_eal_pci_scan(void)
449 char dirname[PATH_MAX];
450 struct rte_pci_addr addr;
452 dir = opendir(pci_get_sysfs_path());
454 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
455 __func__, strerror(errno));
459 while ((e = readdir(dir)) != NULL) {
460 if (e->d_name[0] == '.')
463 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
466 snprintf(dirname, sizeof(dirname), "%s/%s",
467 pci_get_sysfs_path(), e->d_name);
468 if (pci_scan_one(dirname, &addr) < 0)
479 /* Read PCI config space. */
480 int rte_eal_pci_read_config(const struct rte_pci_device *device,
481 void *buf, size_t len, off_t offset)
483 const struct rte_intr_handle *intr_handle = &device->intr_handle;
485 switch (intr_handle->type) {
486 case RTE_INTR_HANDLE_UIO:
487 case RTE_INTR_HANDLE_UIO_INTX:
488 return pci_uio_read_config(intr_handle, buf, len, offset);
491 case RTE_INTR_HANDLE_VFIO_MSIX:
492 case RTE_INTR_HANDLE_VFIO_MSI:
493 case RTE_INTR_HANDLE_VFIO_LEGACY:
494 return pci_vfio_read_config(intr_handle, buf, len, offset);
498 "Unknown handle type of fd %d\n",
504 /* Write PCI config space. */
505 int rte_eal_pci_write_config(const struct rte_pci_device *device,
506 const void *buf, size_t len, off_t offset)
508 const struct rte_intr_handle *intr_handle = &device->intr_handle;
510 switch (intr_handle->type) {
511 case RTE_INTR_HANDLE_UIO:
512 case RTE_INTR_HANDLE_UIO_INTX:
513 return pci_uio_write_config(intr_handle, buf, len, offset);
516 case RTE_INTR_HANDLE_VFIO_MSIX:
517 case RTE_INTR_HANDLE_VFIO_MSI:
518 case RTE_INTR_HANDLE_VFIO_LEGACY:
519 return pci_vfio_write_config(intr_handle, buf, len, offset);
523 "Unknown handle type of fd %d\n",
529 #if defined(RTE_ARCH_X86)
531 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
532 struct rte_pci_ioport *p)
541 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
542 dev->addr.domain, dev->addr.bus,
543 dev->addr.devid, dev->addr.function);
545 fp = fopen("/proc/ioports", "r");
547 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
551 while (getdelim(&line, &linesz, '\n', fp) > 0) {
556 n = strcspn(ptr, ":");
560 while (*left && isspace(*left))
563 if (!strncmp(left, pci_id, strlen(pci_id))) {
566 while (*ptr && isspace(*ptr))
569 sscanf(ptr, "%04hx-%04hx", &start, &end);
581 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
583 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
590 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
591 struct rte_pci_ioport *p)
598 if (pci_vfio_is_enabled())
599 ret = pci_vfio_ioport_map(dev, bar, p);
602 case RTE_KDRV_IGB_UIO:
603 ret = pci_uio_ioport_map(dev, bar, p);
605 case RTE_KDRV_UIO_GENERIC:
606 #if defined(RTE_ARCH_X86)
607 ret = pci_ioport_map(dev, bar, p);
609 ret = pci_uio_ioport_map(dev, bar, p);
613 #if defined(RTE_ARCH_X86)
614 ret = pci_ioport_map(dev, bar, p);
628 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
629 void *data, size_t len, off_t offset)
631 switch (p->dev->kdrv) {
634 pci_vfio_ioport_read(p, data, len, offset);
637 case RTE_KDRV_IGB_UIO:
638 pci_uio_ioport_read(p, data, len, offset);
640 case RTE_KDRV_UIO_GENERIC:
641 pci_uio_ioport_read(p, data, len, offset);
644 #if defined(RTE_ARCH_X86)
645 pci_uio_ioport_read(p, data, len, offset);
654 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
655 const void *data, size_t len, off_t offset)
657 switch (p->dev->kdrv) {
660 pci_vfio_ioport_write(p, data, len, offset);
663 case RTE_KDRV_IGB_UIO:
664 pci_uio_ioport_write(p, data, len, offset);
666 case RTE_KDRV_UIO_GENERIC:
667 pci_uio_ioport_write(p, data, len, offset);
670 #if defined(RTE_ARCH_X86)
671 pci_uio_ioport_write(p, data, len, offset);
680 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
684 switch (p->dev->kdrv) {
687 if (pci_vfio_is_enabled())
688 ret = pci_vfio_ioport_unmap(p);
691 case RTE_KDRV_IGB_UIO:
692 ret = pci_uio_ioport_unmap(p);
694 case RTE_KDRV_UIO_GENERIC:
695 #if defined(RTE_ARCH_X86)
698 ret = pci_uio_ioport_unmap(p);
702 #if defined(RTE_ARCH_X86)
713 /* Init the PCI EAL subsystem */
715 rte_eal_pci_init(void)
717 /* for debug purposes, PCI can be disabled */
718 if (internal_config.no_pci)
721 if (rte_eal_pci_scan() < 0) {
722 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);