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_tailq.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_malloc.h>
43 #include <rte_devargs.h>
44 #include <rte_memcpy.h>
46 #include "rte_pci_dev_ids.h"
47 #include "eal_filesystem.h"
48 #include "eal_private.h"
49 #include "eal_pci_init.h"
53 * PCI probing under linux
55 * This code is used to simulate a PCI probe by parsing information in sysfs.
56 * When a registered device matches a driver, it is then initialized with
57 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
60 struct mapped_pci_res_list *pci_res_list = NULL;
62 /* unbind kernel driver for this device */
64 pci_unbind_kernel_driver(struct rte_pci_device *dev)
68 char filename[PATH_MAX];
70 struct rte_pci_addr *loc = &dev->addr;
72 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
73 snprintf(filename, sizeof(filename),
74 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
75 loc->domain, loc->bus, loc->devid, loc->function);
77 f = fopen(filename, "w");
78 if (f == NULL) /* device was not bound */
81 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
82 loc->domain, loc->bus, loc->devid, loc->function);
83 if ((n < 0) || (n >= (int)sizeof(buf))) {
84 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
87 if (fwrite(buf, n, 1, f) == 0) {
88 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
102 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
108 if (!filename || !dri_name)
111 count = readlink(filename, path, PATH_MAX);
112 if (count >= PATH_MAX)
115 /* For device does not have a driver */
121 name = strrchr(path, '/');
123 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
131 pci_find_max_end_va(void)
133 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
134 const struct rte_memseg *last = seg;
137 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
138 if (seg->addr == NULL)
141 if (seg->addr > last->addr)
145 return RTE_PTR_ADD(last->addr, last->len);
149 /* map a particular resource from a file */
151 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
152 int additional_flags)
156 /* Map the PCI memory resource of device */
157 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
158 MAP_SHARED | additional_flags, fd, offset);
159 if (mapaddr == MAP_FAILED) {
160 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
161 __func__, fd, requested_addr,
162 (unsigned long)size, (unsigned long)offset,
163 strerror(errno), mapaddr);
165 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
171 /* unmap a particular resource */
173 pci_unmap_resource(void *requested_addr, size_t size)
175 if (requested_addr == NULL)
178 /* Unmap the PCI memory resource of device */
179 if (munmap(requested_addr, size)) {
180 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
181 __func__, requested_addr, (unsigned long)size,
184 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
188 /* parse the "resource" sysfs file */
190 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
194 union pci_resource_info {
200 char *ptrs[PCI_RESOURCE_FMT_NVAL];
203 uint64_t phys_addr, end_addr, flags;
205 f = fopen(filename, "r");
207 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
211 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
213 if (fgets(buf, sizeof(buf), f) == NULL) {
215 "%s(): cannot read resource\n", __func__);
219 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
221 "%s(): bad resource format\n", __func__);
225 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
226 end_addr = strtoull(res_info.end_addr, NULL, 16);
227 flags = strtoull(res_info.flags, NULL, 16);
230 "%s(): bad resource format\n", __func__);
234 if (flags & IORESOURCE_MEM) {
235 dev->mem_resource[i].phys_addr = phys_addr;
236 dev->mem_resource[i].len = end_addr - phys_addr + 1;
237 /* not mapped for now */
238 dev->mem_resource[i].addr = NULL;
249 /* Scan one pci sysfs entry, and fill the devices list from it. */
251 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
252 uint8_t devid, uint8_t function)
254 char filename[PATH_MAX];
256 struct rte_pci_device *dev;
257 char driver[PATH_MAX];
260 dev = malloc(sizeof(*dev));
264 memset(dev, 0, sizeof(*dev));
265 dev->addr.domain = domain;
267 dev->addr.devid = devid;
268 dev->addr.function = function;
271 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
272 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
276 dev->id.vendor_id = (uint16_t)tmp;
279 snprintf(filename, sizeof(filename), "%s/device", dirname);
280 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
284 dev->id.device_id = (uint16_t)tmp;
286 /* get subsystem_vendor id */
287 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
289 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
293 dev->id.subsystem_vendor_id = (uint16_t)tmp;
295 /* get subsystem_device id */
296 snprintf(filename, sizeof(filename), "%s/subsystem_device",
298 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
302 dev->id.subsystem_device_id = (uint16_t)tmp;
306 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
307 if (!access(filename, F_OK) &&
308 eal_parse_sysfs_value(filename, &tmp) == 0)
309 dev->max_vfs = (uint16_t)tmp;
311 /* for non igb_uio driver, need kernel version >= 3.8 */
312 snprintf(filename, sizeof(filename),
313 "%s/sriov_numvfs", dirname);
314 if (!access(filename, F_OK) &&
315 eal_parse_sysfs_value(filename, &tmp) == 0)
316 dev->max_vfs = (uint16_t)tmp;
320 snprintf(filename, sizeof(filename), "%s/numa_node",
322 if (access(filename, R_OK) != 0) {
323 /* if no NUMA support just set node to -1 */
326 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
330 dev->numa_node = tmp;
333 /* parse resources */
334 snprintf(filename, sizeof(filename), "%s/resource", dirname);
335 if (pci_parse_sysfs_resource(filename, dev) < 0) {
336 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
342 snprintf(filename, sizeof(filename), "%s/driver", dirname);
343 ret = pci_get_kernel_driver_by_path(filename, driver);
345 if (!strcmp(driver, "vfio-pci"))
346 dev->pt_driver = RTE_PT_VFIO;
347 else if (!strcmp(driver, "igb_uio"))
348 dev->pt_driver = RTE_PT_IGB_UIO;
349 else if (!strcmp(driver, "uio_pci_generic"))
350 dev->pt_driver = RTE_PT_UIO_GENERIC;
352 dev->pt_driver = RTE_PT_UNKNOWN;
353 } else if (ret < 0) {
354 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
358 dev->pt_driver = RTE_PT_UNKNOWN;
360 /* device is valid, add in list (sorted) */
361 if (TAILQ_EMPTY(&pci_device_list)) {
362 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
365 struct rte_pci_device *dev2 = NULL;
368 TAILQ_FOREACH(dev2, &pci_device_list, next) {
369 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
373 TAILQ_INSERT_BEFORE(dev2, dev, next);
375 } else { /* already registered */
376 /* update pt_driver */
377 dev2->pt_driver = dev->pt_driver;
378 dev2->max_vfs = dev->max_vfs;
379 memmove(dev2->mem_resource,
381 sizeof(dev->mem_resource));
386 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
393 * split up a pci address into its constituent parts.
396 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
397 uint8_t *bus, uint8_t *devid, uint8_t *function)
399 /* first split on ':' */
407 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
410 char *buf_copy = strndup(buf, bufsize);
411 if (buf_copy == NULL)
414 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
417 /* final split is on '.' between devid and function */
418 splitaddr.function = strchr(splitaddr.devid,'.');
419 if (splitaddr.function == NULL)
421 *splitaddr.function++ = '\0';
423 /* now convert to int values */
425 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
426 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
427 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
428 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
432 free(buf_copy); /* free the copy made with strdup */
440 * Scan the content of the PCI bus, and the devices in the devices
444 rte_eal_pci_scan(void)
448 char dirname[PATH_MAX];
450 uint8_t bus, devid, function;
452 dir = opendir(SYSFS_PCI_DEVICES);
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), &domain,
464 &bus, &devid, &function) != 0)
467 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
469 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
480 #ifdef RTE_PCI_CONFIG
482 pci_config_extended_tag(struct rte_pci_device *dev)
484 struct rte_pci_addr *loc = &dev->addr;
485 char filename[PATH_MAX];
489 /* not configured, let it as is */
490 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
491 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
494 snprintf(filename, sizeof(filename),
495 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
496 loc->domain, loc->bus, loc->devid, loc->function);
497 f = fopen(filename, "rw+");
501 fgets(buf, sizeof(buf), f);
502 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
503 /* enable Extended Tag*/
504 if (strncmp(buf, "on", 2) != 0) {
505 fseek(f, 0, SEEK_SET);
509 /* disable Extended Tag */
510 if (strncmp(buf, "off", 3) != 0) {
511 fseek(f, 0, SEEK_SET);
521 pci_config_max_read_request_size(struct rte_pci_device *dev)
523 struct rte_pci_addr *loc = &dev->addr;
524 char filename[PATH_MAX];
525 char buf[BUFSIZ], param[BUFSIZ];
527 /* size can be 128, 256, 512, 1024, 2048, 4096 */
528 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
530 /* not configured, let it as is */
534 snprintf(filename, sizeof(filename),
535 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
536 loc->domain, loc->bus, loc->devid, loc->function);
537 f = fopen(filename, "rw+");
541 fgets(buf, sizeof(buf), f);
542 snprintf(param, sizeof(param), "%d", max_size);
544 /* check if the size to be set is the same as current */
545 if (strcmp(buf, param) == 0) {
549 fseek(f, 0, SEEK_SET);
557 pci_config_space_set(struct rte_pci_device *dev)
559 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
562 /* configure extended tag */
563 pci_config_extended_tag(dev);
565 /* configure max read request size */
566 pci_config_max_read_request_size(dev);
571 pci_map_device(struct rte_pci_device *dev)
575 /* try mapping the NIC resources using VFIO if it exists */
576 switch (dev->pt_driver) {
579 if (pci_vfio_is_enabled())
580 ret = pci_vfio_map_resource(dev);
584 case RTE_PT_UIO_GENERIC:
585 /* map resources for devices that use uio */
586 ret = pci_uio_map_resource(dev);
589 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
598 #ifdef RTE_LIBRTE_EAL_HOTPLUG
600 pci_unmap_device(struct rte_pci_device *dev)
605 /* try unmapping the NIC resources using VFIO if it exists */
606 switch (dev->pt_driver) {
608 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
611 case RTE_PT_UIO_GENERIC:
612 /* unmap resources for devices that use uio */
613 pci_uio_unmap_resource(dev);
616 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
621 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
624 * If vendor/device ID match, call the devinit() function of the
628 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
631 struct rte_pci_id *id_table;
633 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
635 /* check if device's identifiers match the driver's ones */
636 if (id_table->vendor_id != dev->id.vendor_id &&
637 id_table->vendor_id != PCI_ANY_ID)
639 if (id_table->device_id != dev->id.device_id &&
640 id_table->device_id != PCI_ANY_ID)
642 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
643 id_table->subsystem_vendor_id != PCI_ANY_ID)
645 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
646 id_table->subsystem_device_id != PCI_ANY_ID)
649 struct rte_pci_addr *loc = &dev->addr;
651 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
652 loc->domain, loc->bus, loc->devid, loc->function,
655 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
656 dev->id.device_id, dr->name);
658 /* no initialization when blacklisted, return without error */
659 if (dev->devargs != NULL &&
660 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
661 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
665 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
666 #ifdef RTE_PCI_CONFIG
668 * Set PCIe config space for high performance.
669 * Return value can be ignored.
671 pci_config_space_set(dev);
673 /* map resources for devices that use igb_uio */
674 ret = pci_map_device(dev);
677 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
678 rte_eal_process_type() == RTE_PROC_PRIMARY) {
679 /* unbind current driver */
680 if (pci_unbind_kernel_driver(dev) < 0)
684 /* reference driver structure */
687 /* call the driver devinit() function */
688 return dr->devinit(dr, dev);
690 /* return positive value if driver is not found */
694 #ifdef RTE_LIBRTE_EAL_HOTPLUG
696 * If vendor/device ID match, call the devuninit() function of the
700 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
701 struct rte_pci_device *dev)
703 struct rte_pci_id *id_table;
705 if ((dr == NULL) || (dev == NULL))
708 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
710 /* check if device's identifiers match the driver's ones */
711 if (id_table->vendor_id != dev->id.vendor_id &&
712 id_table->vendor_id != PCI_ANY_ID)
714 if (id_table->device_id != dev->id.device_id &&
715 id_table->device_id != PCI_ANY_ID)
717 if (id_table->subsystem_vendor_id !=
718 dev->id.subsystem_vendor_id &&
719 id_table->subsystem_vendor_id != PCI_ANY_ID)
721 if (id_table->subsystem_device_id !=
722 dev->id.subsystem_device_id &&
723 id_table->subsystem_device_id != PCI_ANY_ID)
726 struct rte_pci_addr *loc = &dev->addr;
729 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
730 loc->domain, loc->bus, loc->devid,
731 loc->function, dev->numa_node);
733 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n",
734 dev->id.vendor_id, dev->id.device_id,
737 /* call the driver devuninit() function */
738 if (dr->devuninit && (dr->devuninit(dev) < 0))
739 return -1; /* negative value is an error */
741 /* clear driver structure */
744 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
745 /* unmap resources for devices that use igb_uio */
746 pci_unmap_device(dev);
750 /* return positive value if driver is not found */
753 #else /* RTE_LIBRTE_EAL_HOTPLUG */
755 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
756 struct rte_pci_device *dev __rte_unused)
758 RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
761 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
763 /* Init the PCI EAL subsystem */
765 rte_eal_pci_init(void)
767 TAILQ_INIT(&pci_driver_list);
768 TAILQ_INIT(&pci_device_list);
769 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
770 mapped_pci_res_list);
772 /* for debug purposes, PCI can be disabled */
773 if (internal_config.no_pci)
776 if (rte_eal_pci_scan() < 0) {
777 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
783 if (pci_vfio_is_enabled()) {
785 /* if we are primary process, create a thread to communicate with
786 * secondary processes. the thread will use a socket to wait for
787 * requests from secondary process to send open file descriptors,
788 * because VFIO does not allow multiple open descriptors on a group or
791 if (internal_config.process_type == RTE_PROC_PRIMARY &&
792 pci_vfio_mp_sync_setup() < 0)