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.
40 #include <rte_eal_memconfig.h>
41 #include <rte_malloc.h>
42 #include <rte_devargs.h>
43 #include <rte_memcpy.h>
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_private.h"
48 #include "eal_pci_init.h"
52 * PCI probing under linux
54 * This code is used to simulate a PCI probe by parsing information in sysfs.
55 * When a registered device matches a driver, it is then initialized with
56 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
59 /* unbind kernel driver for this device */
61 pci_unbind_kernel_driver(struct rte_pci_device *dev)
65 char filename[PATH_MAX];
67 struct rte_pci_addr *loc = &dev->addr;
69 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
70 snprintf(filename, sizeof(filename),
71 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
72 loc->domain, loc->bus, loc->devid, loc->function);
74 f = fopen(filename, "w");
75 if (f == NULL) /* device was not bound */
78 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
79 loc->domain, loc->bus, loc->devid, loc->function);
80 if ((n < 0) || (n >= (int)sizeof(buf))) {
81 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
84 if (fwrite(buf, n, 1, f) == 0) {
85 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
99 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
105 if (!filename || !dri_name)
108 count = readlink(filename, path, PATH_MAX);
109 if (count >= PATH_MAX)
112 /* For device does not have a driver */
118 name = strrchr(path, '/');
120 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
128 pci_find_max_end_va(void)
130 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
131 const struct rte_memseg *last = seg;
134 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
135 if (seg->addr == NULL)
138 if (seg->addr > last->addr)
142 return RTE_PTR_ADD(last->addr, last->len);
146 /* map a particular resource from a file */
148 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
149 int additional_flags)
153 /* Map the PCI memory resource of device */
154 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
155 MAP_SHARED | additional_flags, fd, offset);
156 if (mapaddr == MAP_FAILED) {
157 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
158 __func__, fd, requested_addr,
159 (unsigned long)size, (unsigned long)offset,
160 strerror(errno), mapaddr);
162 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
168 /* unmap a particular resource */
170 pci_unmap_resource(void *requested_addr, size_t size)
172 if (requested_addr == NULL)
175 /* Unmap the PCI memory resource of device */
176 if (munmap(requested_addr, size)) {
177 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
178 __func__, requested_addr, (unsigned long)size,
181 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
185 /* parse the "resource" sysfs file */
187 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
191 union pci_resource_info {
197 char *ptrs[PCI_RESOURCE_FMT_NVAL];
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__);
216 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
218 "%s(): bad resource format\n", __func__);
222 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
223 end_addr = strtoull(res_info.end_addr, NULL, 16);
224 flags = strtoull(res_info.flags, NULL, 16);
227 "%s(): bad resource format\n", __func__);
231 if (flags & IORESOURCE_MEM) {
232 dev->mem_resource[i].phys_addr = phys_addr;
233 dev->mem_resource[i].len = end_addr - phys_addr + 1;
234 /* not mapped for now */
235 dev->mem_resource[i].addr = NULL;
246 /* Scan one pci sysfs entry, and fill the devices list from it. */
248 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
249 uint8_t devid, uint8_t function)
251 char filename[PATH_MAX];
253 struct rte_pci_device *dev;
254 char driver[PATH_MAX];
257 dev = malloc(sizeof(*dev));
261 memset(dev, 0, sizeof(*dev));
262 dev->addr.domain = domain;
264 dev->addr.devid = devid;
265 dev->addr.function = function;
268 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
269 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
273 dev->id.vendor_id = (uint16_t)tmp;
276 snprintf(filename, sizeof(filename), "%s/device", dirname);
277 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
281 dev->id.device_id = (uint16_t)tmp;
283 /* get subsystem_vendor id */
284 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
286 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
290 dev->id.subsystem_vendor_id = (uint16_t)tmp;
292 /* get subsystem_device id */
293 snprintf(filename, sizeof(filename), "%s/subsystem_device",
295 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
299 dev->id.subsystem_device_id = (uint16_t)tmp;
303 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
304 if (!access(filename, F_OK) &&
305 eal_parse_sysfs_value(filename, &tmp) == 0)
306 dev->max_vfs = (uint16_t)tmp;
308 /* for non igb_uio driver, need kernel version >= 3.8 */
309 snprintf(filename, sizeof(filename),
310 "%s/sriov_numvfs", dirname);
311 if (!access(filename, F_OK) &&
312 eal_parse_sysfs_value(filename, &tmp) == 0)
313 dev->max_vfs = (uint16_t)tmp;
317 snprintf(filename, sizeof(filename), "%s/numa_node",
319 if (access(filename, R_OK) != 0) {
320 /* if no NUMA support just set node to -1 */
323 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
327 dev->numa_node = tmp;
330 /* parse resources */
331 snprintf(filename, sizeof(filename), "%s/resource", dirname);
332 if (pci_parse_sysfs_resource(filename, dev) < 0) {
333 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
339 snprintf(filename, sizeof(filename), "%s/driver", dirname);
340 ret = pci_get_kernel_driver_by_path(filename, driver);
342 if (!strcmp(driver, "vfio-pci"))
343 dev->pt_driver = RTE_PT_VFIO;
344 else if (!strcmp(driver, "igb_uio"))
345 dev->pt_driver = RTE_PT_IGB_UIO;
346 else if (!strcmp(driver, "uio_pci_generic"))
347 dev->pt_driver = RTE_PT_UIO_GENERIC;
349 dev->pt_driver = RTE_PT_UNKNOWN;
350 } else if (ret < 0) {
351 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
355 dev->pt_driver = RTE_PT_UNKNOWN;
357 /* device is valid, add in list (sorted) */
358 if (TAILQ_EMPTY(&pci_device_list)) {
359 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
362 struct rte_pci_device *dev2 = NULL;
365 TAILQ_FOREACH(dev2, &pci_device_list, next) {
366 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
370 TAILQ_INSERT_BEFORE(dev2, dev, next);
372 } else { /* already registered */
373 /* update pt_driver */
374 dev2->pt_driver = dev->pt_driver;
375 dev2->max_vfs = dev->max_vfs;
376 memmove(dev2->mem_resource,
378 sizeof(dev->mem_resource));
383 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
390 * split up a pci address into its constituent parts.
393 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
394 uint8_t *bus, uint8_t *devid, uint8_t *function)
396 /* first split on ':' */
404 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
407 char *buf_copy = strndup(buf, bufsize);
408 if (buf_copy == NULL)
411 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
414 /* final split is on '.' between devid and function */
415 splitaddr.function = strchr(splitaddr.devid,'.');
416 if (splitaddr.function == NULL)
418 *splitaddr.function++ = '\0';
420 /* now convert to int values */
422 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
423 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
424 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
425 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
429 free(buf_copy); /* free the copy made with strdup */
437 * Scan the content of the PCI bus, and the devices in the devices
441 rte_eal_pci_scan(void)
445 char dirname[PATH_MAX];
447 uint8_t bus, devid, function;
449 dir = opendir(SYSFS_PCI_DEVICES);
451 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
452 __func__, strerror(errno));
456 while ((e = readdir(dir)) != NULL) {
457 if (e->d_name[0] == '.')
460 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
461 &bus, &devid, &function) != 0)
464 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
466 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
477 #ifdef RTE_PCI_CONFIG
479 pci_config_extended_tag(struct rte_pci_device *dev)
481 struct rte_pci_addr *loc = &dev->addr;
482 char filename[PATH_MAX];
486 /* not configured, let it as is */
487 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
488 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
491 snprintf(filename, sizeof(filename),
492 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
493 loc->domain, loc->bus, loc->devid, loc->function);
494 f = fopen(filename, "rw+");
498 fgets(buf, sizeof(buf), f);
499 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
500 /* enable Extended Tag*/
501 if (strncmp(buf, "on", 2) != 0) {
502 fseek(f, 0, SEEK_SET);
506 /* disable Extended Tag */
507 if (strncmp(buf, "off", 3) != 0) {
508 fseek(f, 0, SEEK_SET);
518 pci_config_max_read_request_size(struct rte_pci_device *dev)
520 struct rte_pci_addr *loc = &dev->addr;
521 char filename[PATH_MAX];
522 char buf[BUFSIZ], param[BUFSIZ];
524 /* size can be 128, 256, 512, 1024, 2048, 4096 */
525 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
527 /* not configured, let it as is */
531 snprintf(filename, sizeof(filename),
532 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
533 loc->domain, loc->bus, loc->devid, loc->function);
534 f = fopen(filename, "rw+");
538 fgets(buf, sizeof(buf), f);
539 snprintf(param, sizeof(param), "%d", max_size);
541 /* check if the size to be set is the same as current */
542 if (strcmp(buf, param) == 0) {
546 fseek(f, 0, SEEK_SET);
554 pci_config_space_set(struct rte_pci_device *dev)
556 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
559 /* configure extended tag */
560 pci_config_extended_tag(dev);
562 /* configure max read request size */
563 pci_config_max_read_request_size(dev);
568 pci_map_device(struct rte_pci_device *dev)
572 /* try mapping the NIC resources using VFIO if it exists */
573 switch (dev->pt_driver) {
576 if (pci_vfio_is_enabled())
577 ret = pci_vfio_map_resource(dev);
581 case RTE_PT_UIO_GENERIC:
582 /* map resources for devices that use uio */
583 ret = pci_uio_map_resource(dev);
586 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
595 #ifdef RTE_LIBRTE_EAL_HOTPLUG
597 pci_unmap_device(struct rte_pci_device *dev)
602 /* try unmapping the NIC resources using VFIO if it exists */
603 switch (dev->pt_driver) {
605 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
608 case RTE_PT_UIO_GENERIC:
609 /* unmap resources for devices that use uio */
610 pci_uio_unmap_resource(dev);
613 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
618 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
621 * If vendor/device ID match, call the devinit() function of the
625 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
628 struct rte_pci_id *id_table;
630 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
632 /* check if device's identifiers match the driver's ones */
633 if (id_table->vendor_id != dev->id.vendor_id &&
634 id_table->vendor_id != PCI_ANY_ID)
636 if (id_table->device_id != dev->id.device_id &&
637 id_table->device_id != PCI_ANY_ID)
639 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
640 id_table->subsystem_vendor_id != PCI_ANY_ID)
642 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
643 id_table->subsystem_device_id != PCI_ANY_ID)
646 struct rte_pci_addr *loc = &dev->addr;
648 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
649 loc->domain, loc->bus, loc->devid, loc->function,
652 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
653 dev->id.device_id, dr->name);
655 /* no initialization when blacklisted, return without error */
656 if (dev->devargs != NULL &&
657 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
658 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
662 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
663 #ifdef RTE_PCI_CONFIG
665 * Set PCIe config space for high performance.
666 * Return value can be ignored.
668 pci_config_space_set(dev);
670 /* map resources for devices that use igb_uio */
671 ret = pci_map_device(dev);
674 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
675 rte_eal_process_type() == RTE_PROC_PRIMARY) {
676 /* unbind current driver */
677 if (pci_unbind_kernel_driver(dev) < 0)
681 /* reference driver structure */
684 /* call the driver devinit() function */
685 return dr->devinit(dr, dev);
687 /* return positive value if driver is not found */
691 #ifdef RTE_LIBRTE_EAL_HOTPLUG
693 * If vendor/device ID match, call the devuninit() function of the
697 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
698 struct rte_pci_device *dev)
700 struct rte_pci_id *id_table;
702 if ((dr == NULL) || (dev == NULL))
705 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
707 /* check if device's identifiers match the driver's ones */
708 if (id_table->vendor_id != dev->id.vendor_id &&
709 id_table->vendor_id != PCI_ANY_ID)
711 if (id_table->device_id != dev->id.device_id &&
712 id_table->device_id != PCI_ANY_ID)
714 if (id_table->subsystem_vendor_id !=
715 dev->id.subsystem_vendor_id &&
716 id_table->subsystem_vendor_id != PCI_ANY_ID)
718 if (id_table->subsystem_device_id !=
719 dev->id.subsystem_device_id &&
720 id_table->subsystem_device_id != PCI_ANY_ID)
723 struct rte_pci_addr *loc = &dev->addr;
726 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
727 loc->domain, loc->bus, loc->devid,
728 loc->function, dev->numa_node);
730 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n",
731 dev->id.vendor_id, dev->id.device_id,
734 /* call the driver devuninit() function */
735 if (dr->devuninit && (dr->devuninit(dev) < 0))
736 return -1; /* negative value is an error */
738 /* clear driver structure */
741 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
742 /* unmap resources for devices that use igb_uio */
743 pci_unmap_device(dev);
747 /* return positive value if driver is not found */
750 #else /* RTE_LIBRTE_EAL_HOTPLUG */
752 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
753 struct rte_pci_device *dev __rte_unused)
755 RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
758 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
760 /* Init the PCI EAL subsystem */
762 rte_eal_pci_init(void)
764 TAILQ_INIT(&pci_driver_list);
765 TAILQ_INIT(&pci_device_list);
767 /* for debug purposes, PCI can be disabled */
768 if (internal_config.no_pci)
771 if (rte_eal_pci_scan() < 0) {
772 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
778 if (pci_vfio_is_enabled()) {
780 /* if we are primary process, create a thread to communicate with
781 * secondary processes. the thread will use a socket to wait for
782 * requests from secondary process to send open file descriptors,
783 * because VFIO does not allow multiple open descriptors on a group or
786 if (internal_config.process_type == RTE_PROC_PRIMARY &&
787 pci_vfio_mp_sync_setup() < 0)