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).
57 /* unbind kernel driver for this device */
59 pci_unbind_kernel_driver(struct rte_pci_device *dev)
63 char filename[PATH_MAX];
65 struct rte_pci_addr *loc = &dev->addr;
67 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
68 snprintf(filename, sizeof(filename),
69 "%s/" PCI_PRI_FMT "/driver/unbind", pci_get_sysfs_path(),
70 loc->domain, loc->bus, loc->devid, loc->function);
72 f = fopen(filename, "w");
73 if (f == NULL) /* device was not bound */
76 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
77 loc->domain, loc->bus, loc->devid, loc->function);
78 if ((n < 0) || (n >= (int)sizeof(buf))) {
79 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
82 if (fwrite(buf, n, 1, f) == 0) {
83 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
97 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
103 if (!filename || !dri_name)
106 count = readlink(filename, path, PATH_MAX);
107 if (count >= PATH_MAX)
110 /* For device does not have a driver */
116 name = strrchr(path, '/');
118 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
127 rte_eal_pci_map_device(struct rte_pci_device *dev)
131 /* try mapping the NIC resources using VFIO if it exists */
135 if (pci_vfio_is_enabled())
136 ret = pci_vfio_map_resource(dev);
139 case RTE_KDRV_IGB_UIO:
140 case RTE_KDRV_UIO_GENERIC:
141 /* map resources for devices that use uio */
142 ret = pci_uio_map_resource(dev);
146 " Not managed by a supported kernel driver, skipped\n");
154 /* Unmap pci device */
156 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
158 /* try unmapping the NIC resources using VFIO if it exists */
161 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
163 case RTE_KDRV_IGB_UIO:
164 case RTE_KDRV_UIO_GENERIC:
165 /* unmap resources for devices that use uio */
166 pci_uio_unmap_resource(dev);
170 " Not managed by a supported kernel driver, skipped\n");
176 pci_find_max_end_va(void)
178 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
179 const struct rte_memseg *last = seg;
182 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
183 if (seg->addr == NULL)
186 if (seg->addr > last->addr)
190 return RTE_PTR_ADD(last->addr, last->len);
193 /* parse one line of the "resource" sysfs file (note that the 'line'
194 * string is modified)
197 pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
198 uint64_t *end_addr, uint64_t *flags)
200 union pci_resource_info {
206 char *ptrs[PCI_RESOURCE_FMT_NVAL];
209 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3) {
211 "%s(): bad resource format\n", __func__);
215 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
216 *end_addr = strtoull(res_info.end_addr, NULL, 16);
217 *flags = strtoull(res_info.flags, NULL, 16);
220 "%s(): bad resource format\n", __func__);
227 /* parse the "resource" sysfs file */
229 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
234 uint64_t phys_addr, end_addr, flags;
236 f = fopen(filename, "r");
238 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
242 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
244 if (fgets(buf, sizeof(buf), f) == NULL) {
246 "%s(): cannot read resource\n", __func__);
249 if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
250 &end_addr, &flags) < 0)
253 if (flags & IORESOURCE_MEM) {
254 dev->mem_resource[i].phys_addr = phys_addr;
255 dev->mem_resource[i].len = end_addr - phys_addr + 1;
256 /* not mapped for now */
257 dev->mem_resource[i].addr = NULL;
268 /* Scan one pci sysfs entry, and fill the devices list from it. */
270 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
271 uint8_t devid, uint8_t function)
273 char filename[PATH_MAX];
275 struct rte_pci_device *dev;
276 char driver[PATH_MAX];
279 dev = malloc(sizeof(*dev));
283 memset(dev, 0, sizeof(*dev));
284 dev->addr.domain = domain;
286 dev->addr.devid = devid;
287 dev->addr.function = function;
290 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
291 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
295 dev->id.vendor_id = (uint16_t)tmp;
298 snprintf(filename, sizeof(filename), "%s/device", dirname);
299 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
303 dev->id.device_id = (uint16_t)tmp;
305 /* get subsystem_vendor id */
306 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
308 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
312 dev->id.subsystem_vendor_id = (uint16_t)tmp;
314 /* get subsystem_device id */
315 snprintf(filename, sizeof(filename), "%s/subsystem_device",
317 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
321 dev->id.subsystem_device_id = (uint16_t)tmp;
324 snprintf(filename, sizeof(filename), "%s/class",
326 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
330 /* the least 24 bits are valid: class, subclass, program interface */
331 dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;
335 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
336 if (!access(filename, F_OK) &&
337 eal_parse_sysfs_value(filename, &tmp) == 0)
338 dev->max_vfs = (uint16_t)tmp;
340 /* for non igb_uio driver, need kernel version >= 3.8 */
341 snprintf(filename, sizeof(filename),
342 "%s/sriov_numvfs", dirname);
343 if (!access(filename, F_OK) &&
344 eal_parse_sysfs_value(filename, &tmp) == 0)
345 dev->max_vfs = (uint16_t)tmp;
349 snprintf(filename, sizeof(filename), "%s/numa_node",
351 if (access(filename, R_OK) != 0) {
352 /* if no NUMA support, set default to 0 */
355 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
359 dev->numa_node = tmp;
362 /* parse resources */
363 snprintf(filename, sizeof(filename), "%s/resource", dirname);
364 if (pci_parse_sysfs_resource(filename, dev) < 0) {
365 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
371 snprintf(filename, sizeof(filename), "%s/driver", dirname);
372 ret = pci_get_kernel_driver_by_path(filename, driver);
374 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
380 if (!strcmp(driver, "vfio-pci"))
381 dev->kdrv = RTE_KDRV_VFIO;
382 else if (!strcmp(driver, "igb_uio"))
383 dev->kdrv = RTE_KDRV_IGB_UIO;
384 else if (!strcmp(driver, "uio_pci_generic"))
385 dev->kdrv = RTE_KDRV_UIO_GENERIC;
387 dev->kdrv = RTE_KDRV_UNKNOWN;
389 dev->kdrv = RTE_KDRV_NONE;
391 /* device is valid, add in list (sorted) */
392 if (TAILQ_EMPTY(&pci_device_list)) {
393 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
395 struct rte_pci_device *dev2;
398 TAILQ_FOREACH(dev2, &pci_device_list, next) {
399 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
404 TAILQ_INSERT_BEFORE(dev2, dev, next);
405 } else { /* already registered */
406 dev2->kdrv = dev->kdrv;
407 dev2->max_vfs = dev->max_vfs;
408 memmove(dev2->mem_resource, dev->mem_resource,
409 sizeof(dev->mem_resource));
414 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
421 * split up a pci address into its constituent parts.
424 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
425 uint8_t *bus, uint8_t *devid, uint8_t *function)
427 /* first split on ':' */
435 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
438 char *buf_copy = strndup(buf, bufsize);
439 if (buf_copy == NULL)
442 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
445 /* final split is on '.' between devid and function */
446 splitaddr.function = strchr(splitaddr.devid,'.');
447 if (splitaddr.function == NULL)
449 *splitaddr.function++ = '\0';
451 /* now convert to int values */
453 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
454 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
455 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
456 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
460 free(buf_copy); /* free the copy made with strdup */
468 * Scan the content of the PCI bus, and the devices in the devices
472 rte_eal_pci_scan(void)
476 char dirname[PATH_MAX];
478 uint8_t bus, devid, function;
480 dir = opendir(pci_get_sysfs_path());
482 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
483 __func__, strerror(errno));
487 while ((e = readdir(dir)) != NULL) {
488 if (e->d_name[0] == '.')
491 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
492 &bus, &devid, &function) != 0)
495 snprintf(dirname, sizeof(dirname), "%s/%s",
496 pci_get_sysfs_path(), e->d_name);
497 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
508 /* Read PCI config space. */
509 int rte_eal_pci_read_config(const struct rte_pci_device *device,
510 void *buf, size_t len, off_t offset)
512 const struct rte_intr_handle *intr_handle = &device->intr_handle;
514 switch (intr_handle->type) {
515 case RTE_INTR_HANDLE_UIO:
516 case RTE_INTR_HANDLE_UIO_INTX:
517 return pci_uio_read_config(intr_handle, buf, len, offset);
520 case RTE_INTR_HANDLE_VFIO_MSIX:
521 case RTE_INTR_HANDLE_VFIO_MSI:
522 case RTE_INTR_HANDLE_VFIO_LEGACY:
523 return pci_vfio_read_config(intr_handle, buf, len, offset);
527 "Unknown handle type of fd %d\n",
533 /* Write PCI config space. */
534 int rte_eal_pci_write_config(const struct rte_pci_device *device,
535 const void *buf, size_t len, off_t offset)
537 const struct rte_intr_handle *intr_handle = &device->intr_handle;
539 switch (intr_handle->type) {
540 case RTE_INTR_HANDLE_UIO:
541 case RTE_INTR_HANDLE_UIO_INTX:
542 return pci_uio_write_config(intr_handle, buf, len, offset);
545 case RTE_INTR_HANDLE_VFIO_MSIX:
546 case RTE_INTR_HANDLE_VFIO_MSI:
547 case RTE_INTR_HANDLE_VFIO_LEGACY:
548 return pci_vfio_write_config(intr_handle, buf, len, offset);
552 "Unknown handle type of fd %d\n",
558 #if defined(RTE_ARCH_X86)
560 pci_ioport_map(struct rte_pci_device *dev, int bar __rte_unused,
561 struct rte_pci_ioport *p)
570 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
571 dev->addr.domain, dev->addr.bus,
572 dev->addr.devid, dev->addr.function);
574 fp = fopen("/proc/ioports", "r");
576 RTE_LOG(ERR, EAL, "%s(): can't open ioports\n", __func__);
580 while (getdelim(&line, &linesz, '\n', fp) > 0) {
585 n = strcspn(ptr, ":");
589 while (*left && isspace(*left))
592 if (!strncmp(left, pci_id, strlen(pci_id))) {
595 while (*ptr && isspace(*ptr))
598 sscanf(ptr, "%04hx-%04hx", &start, &end);
610 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
612 RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%x\n", start);
619 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
620 struct rte_pci_ioport *p)
627 if (pci_vfio_is_enabled())
628 ret = pci_vfio_ioport_map(dev, bar, p);
631 case RTE_KDRV_IGB_UIO:
632 ret = pci_uio_ioport_map(dev, bar, p);
634 case RTE_KDRV_UIO_GENERIC:
635 #if defined(RTE_ARCH_X86)
636 ret = pci_ioport_map(dev, bar, p);
638 ret = pci_uio_ioport_map(dev, bar, p);
642 #if defined(RTE_ARCH_X86)
643 ret = pci_ioport_map(dev, bar, p);
657 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
658 void *data, size_t len, off_t offset)
660 switch (p->dev->kdrv) {
663 pci_vfio_ioport_read(p, data, len, offset);
666 case RTE_KDRV_IGB_UIO:
667 pci_uio_ioport_read(p, data, len, offset);
669 case RTE_KDRV_UIO_GENERIC:
670 pci_uio_ioport_read(p, data, len, offset);
673 #if defined(RTE_ARCH_X86)
674 pci_uio_ioport_read(p, data, len, offset);
683 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
684 const void *data, size_t len, off_t offset)
686 switch (p->dev->kdrv) {
689 pci_vfio_ioport_write(p, data, len, offset);
692 case RTE_KDRV_IGB_UIO:
693 pci_uio_ioport_write(p, data, len, offset);
695 case RTE_KDRV_UIO_GENERIC:
696 pci_uio_ioport_write(p, data, len, offset);
699 #if defined(RTE_ARCH_X86)
700 pci_uio_ioport_write(p, data, len, offset);
709 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
713 switch (p->dev->kdrv) {
716 if (pci_vfio_is_enabled())
717 ret = pci_vfio_ioport_unmap(p);
720 case RTE_KDRV_IGB_UIO:
721 ret = pci_uio_ioport_unmap(p);
723 case RTE_KDRV_UIO_GENERIC:
724 #if defined(RTE_ARCH_X86)
727 ret = pci_uio_ioport_unmap(p);
731 #if defined(RTE_ARCH_X86)
742 /* Init the PCI EAL subsystem */
744 rte_eal_pci_init(void)
746 TAILQ_INIT(&pci_driver_list);
747 TAILQ_INIT(&pci_device_list);
749 /* for debug purposes, PCI can be disabled */
750 if (internal_config.no_pci)
753 if (rte_eal_pci_scan() < 0) {
754 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);