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 struct mapped_pci_res_list *pci_res_list = NULL;
61 static struct rte_tailq_elem rte_pci_tailq = {
62 .name = "PCI_RESOURCE_LIST",
64 EAL_REGISTER_TAILQ(rte_pci_tailq)
66 /* unbind kernel driver for this device */
68 pci_unbind_kernel_driver(struct rte_pci_device *dev)
72 char filename[PATH_MAX];
74 struct rte_pci_addr *loc = &dev->addr;
76 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
77 snprintf(filename, sizeof(filename),
78 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
79 loc->domain, loc->bus, loc->devid, loc->function);
81 f = fopen(filename, "w");
82 if (f == NULL) /* device was not bound */
85 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
86 loc->domain, loc->bus, loc->devid, loc->function);
87 if ((n < 0) || (n >= (int)sizeof(buf))) {
88 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
91 if (fwrite(buf, n, 1, f) == 0) {
92 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
106 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
112 if (!filename || !dri_name)
115 count = readlink(filename, path, PATH_MAX);
116 if (count >= PATH_MAX)
119 /* For device does not have a driver */
125 name = strrchr(path, '/');
127 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
135 pci_find_max_end_va(void)
137 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
138 const struct rte_memseg *last = seg;
141 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
142 if (seg->addr == NULL)
145 if (seg->addr > last->addr)
149 return RTE_PTR_ADD(last->addr, last->len);
153 /* map a particular resource from a file */
155 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
156 int additional_flags)
160 /* Map the PCI memory resource of device */
161 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
162 MAP_SHARED | additional_flags, fd, offset);
163 if (mapaddr == MAP_FAILED) {
164 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
165 __func__, fd, requested_addr,
166 (unsigned long)size, (unsigned long)offset,
167 strerror(errno), mapaddr);
169 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
175 /* unmap a particular resource */
177 pci_unmap_resource(void *requested_addr, size_t size)
179 if (requested_addr == NULL)
182 /* Unmap the PCI memory resource of device */
183 if (munmap(requested_addr, size)) {
184 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
185 __func__, requested_addr, (unsigned long)size,
188 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
192 /* parse the "resource" sysfs file */
194 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
198 union pci_resource_info {
204 char *ptrs[PCI_RESOURCE_FMT_NVAL];
207 uint64_t phys_addr, end_addr, flags;
209 f = fopen(filename, "r");
211 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
215 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
217 if (fgets(buf, sizeof(buf), f) == NULL) {
219 "%s(): cannot read resource\n", __func__);
223 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
225 "%s(): bad resource format\n", __func__);
229 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
230 end_addr = strtoull(res_info.end_addr, NULL, 16);
231 flags = strtoull(res_info.flags, NULL, 16);
234 "%s(): bad resource format\n", __func__);
238 if (flags & IORESOURCE_MEM) {
239 dev->mem_resource[i].phys_addr = phys_addr;
240 dev->mem_resource[i].len = end_addr - phys_addr + 1;
241 /* not mapped for now */
242 dev->mem_resource[i].addr = NULL;
253 /* Scan one pci sysfs entry, and fill the devices list from it. */
255 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
256 uint8_t devid, uint8_t function)
258 char filename[PATH_MAX];
260 struct rte_pci_device *dev;
261 char driver[PATH_MAX];
264 dev = malloc(sizeof(*dev));
268 memset(dev, 0, sizeof(*dev));
269 dev->addr.domain = domain;
271 dev->addr.devid = devid;
272 dev->addr.function = function;
275 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
276 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
280 dev->id.vendor_id = (uint16_t)tmp;
283 snprintf(filename, sizeof(filename), "%s/device", dirname);
284 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
288 dev->id.device_id = (uint16_t)tmp;
290 /* get subsystem_vendor id */
291 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
293 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
297 dev->id.subsystem_vendor_id = (uint16_t)tmp;
299 /* get subsystem_device id */
300 snprintf(filename, sizeof(filename), "%s/subsystem_device",
302 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
306 dev->id.subsystem_device_id = (uint16_t)tmp;
310 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
311 if (!access(filename, F_OK) &&
312 eal_parse_sysfs_value(filename, &tmp) == 0)
313 dev->max_vfs = (uint16_t)tmp;
315 /* for non igb_uio driver, need kernel version >= 3.8 */
316 snprintf(filename, sizeof(filename),
317 "%s/sriov_numvfs", dirname);
318 if (!access(filename, F_OK) &&
319 eal_parse_sysfs_value(filename, &tmp) == 0)
320 dev->max_vfs = (uint16_t)tmp;
324 snprintf(filename, sizeof(filename), "%s/numa_node",
326 if (access(filename, R_OK) != 0) {
327 /* if no NUMA support just set node to -1 */
330 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
334 dev->numa_node = tmp;
337 /* parse resources */
338 snprintf(filename, sizeof(filename), "%s/resource", dirname);
339 if (pci_parse_sysfs_resource(filename, dev) < 0) {
340 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
346 snprintf(filename, sizeof(filename), "%s/driver", dirname);
347 ret = pci_get_kernel_driver_by_path(filename, driver);
349 if (!strcmp(driver, "vfio-pci"))
350 dev->pt_driver = RTE_PT_VFIO;
351 else if (!strcmp(driver, "igb_uio"))
352 dev->pt_driver = RTE_PT_IGB_UIO;
353 else if (!strcmp(driver, "uio_pci_generic"))
354 dev->pt_driver = RTE_PT_UIO_GENERIC;
356 dev->pt_driver = RTE_PT_UNKNOWN;
357 } else if (ret < 0) {
358 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
362 dev->pt_driver = RTE_PT_UNKNOWN;
364 /* device is valid, add in list (sorted) */
365 if (TAILQ_EMPTY(&pci_device_list)) {
366 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
369 struct rte_pci_device *dev2 = NULL;
372 TAILQ_FOREACH(dev2, &pci_device_list, next) {
373 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
377 TAILQ_INSERT_BEFORE(dev2, dev, next);
379 } else { /* already registered */
380 /* update pt_driver */
381 dev2->pt_driver = dev->pt_driver;
382 dev2->max_vfs = dev->max_vfs;
383 memmove(dev2->mem_resource,
385 sizeof(dev->mem_resource));
390 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
397 * split up a pci address into its constituent parts.
400 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
401 uint8_t *bus, uint8_t *devid, uint8_t *function)
403 /* first split on ':' */
411 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
414 char *buf_copy = strndup(buf, bufsize);
415 if (buf_copy == NULL)
418 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
421 /* final split is on '.' between devid and function */
422 splitaddr.function = strchr(splitaddr.devid,'.');
423 if (splitaddr.function == NULL)
425 *splitaddr.function++ = '\0';
427 /* now convert to int values */
429 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
430 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
431 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
432 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
436 free(buf_copy); /* free the copy made with strdup */
444 * Scan the content of the PCI bus, and the devices in the devices
448 rte_eal_pci_scan(void)
452 char dirname[PATH_MAX];
454 uint8_t bus, devid, function;
456 dir = opendir(SYSFS_PCI_DEVICES);
458 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
459 __func__, strerror(errno));
463 while ((e = readdir(dir)) != NULL) {
464 if (e->d_name[0] == '.')
467 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
468 &bus, &devid, &function) != 0)
471 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
473 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
484 #ifdef RTE_PCI_CONFIG
486 pci_config_extended_tag(struct rte_pci_device *dev)
488 struct rte_pci_addr *loc = &dev->addr;
489 char filename[PATH_MAX];
493 /* not configured, let it as is */
494 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
495 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
498 snprintf(filename, sizeof(filename),
499 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
500 loc->domain, loc->bus, loc->devid, loc->function);
501 f = fopen(filename, "rw+");
505 fgets(buf, sizeof(buf), f);
506 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
507 /* enable Extended Tag*/
508 if (strncmp(buf, "on", 2) != 0) {
509 fseek(f, 0, SEEK_SET);
513 /* disable Extended Tag */
514 if (strncmp(buf, "off", 3) != 0) {
515 fseek(f, 0, SEEK_SET);
525 pci_config_max_read_request_size(struct rte_pci_device *dev)
527 struct rte_pci_addr *loc = &dev->addr;
528 char filename[PATH_MAX];
529 char buf[BUFSIZ], param[BUFSIZ];
531 /* size can be 128, 256, 512, 1024, 2048, 4096 */
532 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
534 /* not configured, let it as is */
538 snprintf(filename, sizeof(filename),
539 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
540 loc->domain, loc->bus, loc->devid, loc->function);
541 f = fopen(filename, "rw+");
545 fgets(buf, sizeof(buf), f);
546 snprintf(param, sizeof(param), "%d", max_size);
548 /* check if the size to be set is the same as current */
549 if (strcmp(buf, param) == 0) {
553 fseek(f, 0, SEEK_SET);
561 pci_config_space_set(struct rte_pci_device *dev)
563 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
566 /* configure extended tag */
567 pci_config_extended_tag(dev);
569 /* configure max read request size */
570 pci_config_max_read_request_size(dev);
575 pci_map_device(struct rte_pci_device *dev)
579 /* try mapping the NIC resources using VFIO if it exists */
580 switch (dev->pt_driver) {
583 if (pci_vfio_is_enabled())
584 ret = pci_vfio_map_resource(dev);
588 case RTE_PT_UIO_GENERIC:
589 /* map resources for devices that use uio */
590 ret = pci_uio_map_resource(dev);
593 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
602 #ifdef RTE_LIBRTE_EAL_HOTPLUG
604 pci_unmap_device(struct rte_pci_device *dev)
609 /* try unmapping the NIC resources using VFIO if it exists */
610 switch (dev->pt_driver) {
612 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
615 case RTE_PT_UIO_GENERIC:
616 /* unmap resources for devices that use uio */
617 pci_uio_unmap_resource(dev);
620 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
625 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
628 * If vendor/device ID match, call the devinit() function of the
632 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
635 struct rte_pci_id *id_table;
637 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
639 /* check if device's identifiers match the driver's ones */
640 if (id_table->vendor_id != dev->id.vendor_id &&
641 id_table->vendor_id != PCI_ANY_ID)
643 if (id_table->device_id != dev->id.device_id &&
644 id_table->device_id != PCI_ANY_ID)
646 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
647 id_table->subsystem_vendor_id != PCI_ANY_ID)
649 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
650 id_table->subsystem_device_id != PCI_ANY_ID)
653 struct rte_pci_addr *loc = &dev->addr;
655 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
656 loc->domain, loc->bus, loc->devid, loc->function,
659 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
660 dev->id.device_id, dr->name);
662 /* no initialization when blacklisted, return without error */
663 if (dev->devargs != NULL &&
664 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
665 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
669 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
670 #ifdef RTE_PCI_CONFIG
672 * Set PCIe config space for high performance.
673 * Return value can be ignored.
675 pci_config_space_set(dev);
677 /* map resources for devices that use igb_uio */
678 ret = pci_map_device(dev);
681 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
682 rte_eal_process_type() == RTE_PROC_PRIMARY) {
683 /* unbind current driver */
684 if (pci_unbind_kernel_driver(dev) < 0)
688 /* reference driver structure */
691 /* call the driver devinit() function */
692 return dr->devinit(dr, dev);
694 /* return positive value if driver is not found */
698 #ifdef RTE_LIBRTE_EAL_HOTPLUG
700 * If vendor/device ID match, call the devuninit() function of the
704 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
705 struct rte_pci_device *dev)
707 struct rte_pci_id *id_table;
709 if ((dr == NULL) || (dev == NULL))
712 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
714 /* check if device's identifiers match the driver's ones */
715 if (id_table->vendor_id != dev->id.vendor_id &&
716 id_table->vendor_id != PCI_ANY_ID)
718 if (id_table->device_id != dev->id.device_id &&
719 id_table->device_id != PCI_ANY_ID)
721 if (id_table->subsystem_vendor_id !=
722 dev->id.subsystem_vendor_id &&
723 id_table->subsystem_vendor_id != PCI_ANY_ID)
725 if (id_table->subsystem_device_id !=
726 dev->id.subsystem_device_id &&
727 id_table->subsystem_device_id != PCI_ANY_ID)
730 struct rte_pci_addr *loc = &dev->addr;
733 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
734 loc->domain, loc->bus, loc->devid,
735 loc->function, dev->numa_node);
737 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n",
738 dev->id.vendor_id, dev->id.device_id,
741 /* call the driver devuninit() function */
742 if (dr->devuninit && (dr->devuninit(dev) < 0))
743 return -1; /* negative value is an error */
745 /* clear driver structure */
748 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
749 /* unmap resources for devices that use igb_uio */
750 pci_unmap_device(dev);
754 /* return positive value if driver is not found */
757 #else /* RTE_LIBRTE_EAL_HOTPLUG */
759 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
760 struct rte_pci_device *dev __rte_unused)
762 RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
765 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
767 /* Init the PCI EAL subsystem */
769 rte_eal_pci_init(void)
771 TAILQ_INIT(&pci_driver_list);
772 TAILQ_INIT(&pci_device_list);
773 pci_res_list = RTE_TAILQ_CAST(rte_pci_tailq.head, mapped_pci_res_list);
775 /* for debug purposes, PCI can be disabled */
776 if (internal_config.no_pci)
779 if (rte_eal_pci_scan() < 0) {
780 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
786 if (pci_vfio_is_enabled()) {
788 /* if we are primary process, create a thread to communicate with
789 * secondary processes. the thread will use a socket to wait for
790 * requests from secondary process to send open file descriptors,
791 * because VFIO does not allow multiple open descriptors on a group or
794 if (internal_config.process_type == RTE_PROC_PRIMARY &&
795 pci_vfio_mp_sync_setup() < 0)