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->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;
350 } else if (ret < 0) {
351 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
355 dev->kdrv = RTE_KDRV_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 dev2->kdrv = dev->kdrv;
374 dev2->max_vfs = dev->max_vfs;
375 memmove(dev2->mem_resource,
377 sizeof(dev->mem_resource));
382 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
389 * split up a pci address into its constituent parts.
392 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
393 uint8_t *bus, uint8_t *devid, uint8_t *function)
395 /* first split on ':' */
403 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
406 char *buf_copy = strndup(buf, bufsize);
407 if (buf_copy == NULL)
410 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
413 /* final split is on '.' between devid and function */
414 splitaddr.function = strchr(splitaddr.devid,'.');
415 if (splitaddr.function == NULL)
417 *splitaddr.function++ = '\0';
419 /* now convert to int values */
421 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
422 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
423 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
424 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
428 free(buf_copy); /* free the copy made with strdup */
436 * Scan the content of the PCI bus, and the devices in the devices
440 rte_eal_pci_scan(void)
444 char dirname[PATH_MAX];
446 uint8_t bus, devid, function;
448 dir = opendir(SYSFS_PCI_DEVICES);
450 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
451 __func__, strerror(errno));
455 while ((e = readdir(dir)) != NULL) {
456 if (e->d_name[0] == '.')
459 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
460 &bus, &devid, &function) != 0)
463 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
465 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
476 #ifdef RTE_PCI_CONFIG
478 pci_config_extended_tag(struct rte_pci_device *dev)
480 struct rte_pci_addr *loc = &dev->addr;
481 char filename[PATH_MAX];
485 /* not configured, let it as is */
486 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
487 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
490 snprintf(filename, sizeof(filename),
491 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
492 loc->domain, loc->bus, loc->devid, loc->function);
493 f = fopen(filename, "rw+");
497 fgets(buf, sizeof(buf), f);
498 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
499 /* enable Extended Tag*/
500 if (strncmp(buf, "on", 2) != 0) {
501 fseek(f, 0, SEEK_SET);
505 /* disable Extended Tag */
506 if (strncmp(buf, "off", 3) != 0) {
507 fseek(f, 0, SEEK_SET);
517 pci_config_max_read_request_size(struct rte_pci_device *dev)
519 struct rte_pci_addr *loc = &dev->addr;
520 char filename[PATH_MAX];
521 char buf[BUFSIZ], param[BUFSIZ];
523 /* size can be 128, 256, 512, 1024, 2048, 4096 */
524 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
526 /* not configured, let it as is */
530 snprintf(filename, sizeof(filename),
531 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
532 loc->domain, loc->bus, loc->devid, loc->function);
533 f = fopen(filename, "rw+");
537 fgets(buf, sizeof(buf), f);
538 snprintf(param, sizeof(param), "%d", max_size);
540 /* check if the size to be set is the same as current */
541 if (strcmp(buf, param) == 0) {
545 fseek(f, 0, SEEK_SET);
553 pci_config_space_set(struct rte_pci_device *dev)
555 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
558 /* configure extended tag */
559 pci_config_extended_tag(dev);
561 /* configure max read request size */
562 pci_config_max_read_request_size(dev);
567 pci_map_device(struct rte_pci_device *dev)
571 /* try mapping the NIC resources using VFIO if it exists */
575 if (pci_vfio_is_enabled())
576 ret = pci_vfio_map_resource(dev);
579 case RTE_KDRV_IGB_UIO:
580 case RTE_KDRV_UIO_GENERIC:
581 /* map resources for devices that use uio */
582 ret = pci_uio_map_resource(dev);
585 RTE_LOG(DEBUG, EAL, " Not managed by a supported kernel driver,"
594 #ifdef RTE_LIBRTE_EAL_HOTPLUG
596 pci_unmap_device(struct rte_pci_device *dev)
601 /* try unmapping the NIC resources using VFIO if it exists */
604 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
606 case RTE_KDRV_IGB_UIO:
607 case RTE_KDRV_UIO_GENERIC:
608 /* unmap resources for devices that use uio */
609 pci_uio_unmap_resource(dev);
612 RTE_LOG(DEBUG, EAL, " Not managed by a supported kernel driver,"
617 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
620 * If vendor/device ID match, call the devinit() function of the
624 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
627 const struct rte_pci_id *id_table;
629 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
631 /* check if device's identifiers match the driver's ones */
632 if (id_table->vendor_id != dev->id.vendor_id &&
633 id_table->vendor_id != PCI_ANY_ID)
635 if (id_table->device_id != dev->id.device_id &&
636 id_table->device_id != PCI_ANY_ID)
638 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
639 id_table->subsystem_vendor_id != PCI_ANY_ID)
641 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
642 id_table->subsystem_device_id != PCI_ANY_ID)
645 struct rte_pci_addr *loc = &dev->addr;
647 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
648 loc->domain, loc->bus, loc->devid, loc->function,
651 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
652 dev->id.device_id, dr->name);
654 /* no initialization when blacklisted, return without error */
655 if (dev->devargs != NULL &&
656 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
657 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
661 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
662 #ifdef RTE_PCI_CONFIG
664 * Set PCIe config space for high performance.
665 * Return value can be ignored.
667 pci_config_space_set(dev);
669 /* map resources for devices that use igb_uio */
670 ret = pci_map_device(dev);
673 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
674 rte_eal_process_type() == RTE_PROC_PRIMARY) {
675 /* unbind current driver */
676 if (pci_unbind_kernel_driver(dev) < 0)
680 /* reference driver structure */
683 /* call the driver devinit() function */
684 return dr->devinit(dr, dev);
686 /* return positive value if driver is not found */
690 #ifdef RTE_LIBRTE_EAL_HOTPLUG
692 * If vendor/device ID match, call the devuninit() function of the
696 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
697 struct rte_pci_device *dev)
699 const struct rte_pci_id *id_table;
701 if ((dr == NULL) || (dev == NULL))
704 for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
706 /* check if device's identifiers match the driver's ones */
707 if (id_table->vendor_id != dev->id.vendor_id &&
708 id_table->vendor_id != PCI_ANY_ID)
710 if (id_table->device_id != dev->id.device_id &&
711 id_table->device_id != PCI_ANY_ID)
713 if (id_table->subsystem_vendor_id !=
714 dev->id.subsystem_vendor_id &&
715 id_table->subsystem_vendor_id != PCI_ANY_ID)
717 if (id_table->subsystem_device_id !=
718 dev->id.subsystem_device_id &&
719 id_table->subsystem_device_id != PCI_ANY_ID)
722 struct rte_pci_addr *loc = &dev->addr;
725 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
726 loc->domain, loc->bus, loc->devid,
727 loc->function, dev->numa_node);
729 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n",
730 dev->id.vendor_id, dev->id.device_id,
733 /* call the driver devuninit() function */
734 if (dr->devuninit && (dr->devuninit(dev) < 0))
735 return -1; /* negative value is an error */
737 /* clear driver structure */
740 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
741 /* unmap resources for devices that use igb_uio */
742 pci_unmap_device(dev);
746 /* return positive value if driver is not found */
749 #else /* RTE_LIBRTE_EAL_HOTPLUG */
751 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
752 struct rte_pci_device *dev __rte_unused)
754 RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
757 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
759 /* Init the PCI EAL subsystem */
761 rte_eal_pci_init(void)
763 TAILQ_INIT(&pci_driver_list);
764 TAILQ_INIT(&pci_device_list);
766 /* for debug purposes, PCI can be disabled */
767 if (internal_config.no_pci)
770 if (rte_eal_pci_scan() < 0) {
771 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
777 if (pci_vfio_is_enabled()) {
779 /* if we are primary process, create a thread to communicate with
780 * secondary processes. the thread will use a socket to wait for
781 * requests from secondary process to send open file descriptors,
782 * because VFIO does not allow multiple open descriptors on a group or
785 if (internal_config.process_type == RTE_PROC_PRIMARY &&
786 pci_vfio_mp_sync_setup() < 0)