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 /* unbind kernel driver for this device */
63 pci_unbind_kernel_driver(struct rte_pci_device *dev)
67 char filename[PATH_MAX];
69 struct rte_pci_addr *loc = &dev->addr;
71 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
72 snprintf(filename, sizeof(filename),
73 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
74 loc->domain, loc->bus, loc->devid, loc->function);
76 f = fopen(filename, "w");
77 if (f == NULL) /* device was not bound */
80 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
81 loc->domain, loc->bus, loc->devid, loc->function);
82 if ((n < 0) || (n >= (int)sizeof(buf))) {
83 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
86 if (fwrite(buf, n, 1, f) == 0) {
87 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
101 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
107 if (!filename || !dri_name)
110 count = readlink(filename, path, PATH_MAX);
111 if (count >= PATH_MAX)
114 /* For device does not have a driver */
120 name = strrchr(path, '/');
122 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
130 pci_find_max_end_va(void)
132 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
133 const struct rte_memseg *last = seg;
136 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
137 if (seg->addr == NULL)
140 if (seg->addr > last->addr)
144 return RTE_PTR_ADD(last->addr, last->len);
148 /* map a particular resource from a file */
150 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
151 int additional_flags)
155 /* Map the PCI memory resource of device */
156 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
157 MAP_SHARED | additional_flags, fd, offset);
158 if (mapaddr == MAP_FAILED) {
159 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
160 __func__, fd, requested_addr,
161 (unsigned long)size, (unsigned long)offset,
162 strerror(errno), mapaddr);
164 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
170 /* unmap a particular resource */
172 pci_unmap_resource(void *requested_addr, size_t size)
174 if (requested_addr == NULL)
177 /* Unmap the PCI memory resource of device */
178 if (munmap(requested_addr, size)) {
179 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
180 __func__, requested_addr, (unsigned long)size,
183 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
187 /* parse the "resource" sysfs file */
189 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
193 union pci_resource_info {
199 char *ptrs[PCI_RESOURCE_FMT_NVAL];
202 uint64_t phys_addr, end_addr, flags;
204 f = fopen(filename, "r");
206 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
210 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
212 if (fgets(buf, sizeof(buf), f) == NULL) {
214 "%s(): cannot read resource\n", __func__);
218 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
220 "%s(): bad resource format\n", __func__);
224 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
225 end_addr = strtoull(res_info.end_addr, NULL, 16);
226 flags = strtoull(res_info.flags, NULL, 16);
229 "%s(): bad resource format\n", __func__);
233 if (flags & IORESOURCE_MEM) {
234 dev->mem_resource[i].phys_addr = phys_addr;
235 dev->mem_resource[i].len = end_addr - phys_addr + 1;
236 /* not mapped for now */
237 dev->mem_resource[i].addr = NULL;
248 /* Scan one pci sysfs entry, and fill the devices list from it. */
250 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
251 uint8_t devid, uint8_t function)
253 char filename[PATH_MAX];
255 struct rte_pci_device *dev;
256 char driver[PATH_MAX];
259 dev = malloc(sizeof(*dev));
263 memset(dev, 0, sizeof(*dev));
264 dev->addr.domain = domain;
266 dev->addr.devid = devid;
267 dev->addr.function = function;
270 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
271 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
275 dev->id.vendor_id = (uint16_t)tmp;
278 snprintf(filename, sizeof(filename), "%s/device", dirname);
279 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
283 dev->id.device_id = (uint16_t)tmp;
285 /* get subsystem_vendor id */
286 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
288 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
292 dev->id.subsystem_vendor_id = (uint16_t)tmp;
294 /* get subsystem_device id */
295 snprintf(filename, sizeof(filename), "%s/subsystem_device",
297 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
301 dev->id.subsystem_device_id = (uint16_t)tmp;
305 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
306 if (!access(filename, F_OK) &&
307 eal_parse_sysfs_value(filename, &tmp) == 0)
308 dev->max_vfs = (uint16_t)tmp;
310 /* for non igb_uio driver, need kernel version >= 3.8 */
311 snprintf(filename, sizeof(filename),
312 "%s/sriov_numvfs", dirname);
313 if (!access(filename, F_OK) &&
314 eal_parse_sysfs_value(filename, &tmp) == 0)
315 dev->max_vfs = (uint16_t)tmp;
319 snprintf(filename, sizeof(filename), "%s/numa_node",
321 if (access(filename, R_OK) != 0) {
322 /* if no NUMA support just set node to -1 */
325 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
329 dev->numa_node = tmp;
332 /* parse resources */
333 snprintf(filename, sizeof(filename), "%s/resource", dirname);
334 if (pci_parse_sysfs_resource(filename, dev) < 0) {
335 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
341 snprintf(filename, sizeof(filename), "%s/driver", dirname);
342 ret = pci_get_kernel_driver_by_path(filename, driver);
344 if (!strcmp(driver, "vfio-pci"))
345 dev->pt_driver = RTE_PT_VFIO;
346 else if (!strcmp(driver, "igb_uio"))
347 dev->pt_driver = RTE_PT_IGB_UIO;
348 else if (!strcmp(driver, "uio_pci_generic"))
349 dev->pt_driver = RTE_PT_UIO_GENERIC;
351 dev->pt_driver = RTE_PT_UNKNOWN;
352 } else if (ret < 0) {
353 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
357 dev->pt_driver = RTE_PT_UNKNOWN;
359 /* device is valid, add in list (sorted) */
360 if (TAILQ_EMPTY(&pci_device_list)) {
361 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
364 struct rte_pci_device *dev2 = NULL;
367 TAILQ_FOREACH(dev2, &pci_device_list, next) {
368 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
372 TAILQ_INSERT_BEFORE(dev2, dev, next);
374 } else { /* already registered */
375 /* update pt_driver */
376 dev2->pt_driver = dev->pt_driver;
377 dev2->max_vfs = dev->max_vfs;
378 memmove(dev2->mem_resource,
380 sizeof(dev->mem_resource));
385 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
392 * split up a pci address into its constituent parts.
395 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
396 uint8_t *bus, uint8_t *devid, uint8_t *function)
398 /* first split on ':' */
406 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
409 char *buf_copy = strndup(buf, bufsize);
410 if (buf_copy == NULL)
413 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
416 /* final split is on '.' between devid and function */
417 splitaddr.function = strchr(splitaddr.devid,'.');
418 if (splitaddr.function == NULL)
420 *splitaddr.function++ = '\0';
422 /* now convert to int values */
424 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
425 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
426 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
427 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
431 free(buf_copy); /* free the copy made with strdup */
439 * Scan the content of the PCI bus, and the devices in the devices
443 rte_eal_pci_scan(void)
447 char dirname[PATH_MAX];
449 uint8_t bus, devid, function;
451 dir = opendir(SYSFS_PCI_DEVICES);
453 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
454 __func__, strerror(errno));
458 while ((e = readdir(dir)) != NULL) {
459 if (e->d_name[0] == '.')
462 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
463 &bus, &devid, &function) != 0)
466 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
468 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
479 #ifdef RTE_PCI_CONFIG
481 pci_config_extended_tag(struct rte_pci_device *dev)
483 struct rte_pci_addr *loc = &dev->addr;
484 char filename[PATH_MAX];
488 /* not configured, let it as is */
489 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
490 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
493 snprintf(filename, sizeof(filename),
494 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
495 loc->domain, loc->bus, loc->devid, loc->function);
496 f = fopen(filename, "rw+");
500 fgets(buf, sizeof(buf), f);
501 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
502 /* enable Extended Tag*/
503 if (strncmp(buf, "on", 2) != 0) {
504 fseek(f, 0, SEEK_SET);
508 /* disable Extended Tag */
509 if (strncmp(buf, "off", 3) != 0) {
510 fseek(f, 0, SEEK_SET);
520 pci_config_max_read_request_size(struct rte_pci_device *dev)
522 struct rte_pci_addr *loc = &dev->addr;
523 char filename[PATH_MAX];
524 char buf[BUFSIZ], param[BUFSIZ];
526 /* size can be 128, 256, 512, 1024, 2048, 4096 */
527 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
529 /* not configured, let it as is */
533 snprintf(filename, sizeof(filename),
534 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
535 loc->domain, loc->bus, loc->devid, loc->function);
536 f = fopen(filename, "rw+");
540 fgets(buf, sizeof(buf), f);
541 snprintf(param, sizeof(param), "%d", max_size);
543 /* check if the size to be set is the same as current */
544 if (strcmp(buf, param) == 0) {
548 fseek(f, 0, SEEK_SET);
556 pci_config_space_set(struct rte_pci_device *dev)
558 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
561 /* configure extended tag */
562 pci_config_extended_tag(dev);
564 /* configure max read request size */
565 pci_config_max_read_request_size(dev);
570 pci_map_device(struct rte_pci_device *dev)
574 /* try mapping the NIC resources using VFIO if it exists */
575 switch (dev->pt_driver) {
578 if (pci_vfio_is_enabled())
579 ret = pci_vfio_map_resource(dev);
583 case RTE_PT_UIO_GENERIC:
584 /* map resources for devices that use uio */
585 ret = pci_uio_map_resource(dev);
588 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
597 #ifdef RTE_LIBRTE_EAL_HOTPLUG
599 pci_unmap_device(struct rte_pci_device *dev)
604 /* try unmapping the NIC resources using VFIO if it exists */
605 switch (dev->pt_driver) {
607 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
610 case RTE_PT_UIO_GENERIC:
611 /* unmap resources for devices that use uio */
612 pci_uio_unmap_resource(dev);
615 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
620 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
623 * If vendor/device ID match, call the devinit() function of the
627 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
630 struct rte_pci_id *id_table;
632 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
634 /* check if device's identifiers match the driver's ones */
635 if (id_table->vendor_id != dev->id.vendor_id &&
636 id_table->vendor_id != PCI_ANY_ID)
638 if (id_table->device_id != dev->id.device_id &&
639 id_table->device_id != PCI_ANY_ID)
641 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
642 id_table->subsystem_vendor_id != PCI_ANY_ID)
644 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
645 id_table->subsystem_device_id != PCI_ANY_ID)
648 struct rte_pci_addr *loc = &dev->addr;
650 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
651 loc->domain, loc->bus, loc->devid, loc->function,
654 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
655 dev->id.device_id, dr->name);
657 /* no initialization when blacklisted, return without error */
658 if (dev->devargs != NULL &&
659 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
660 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
664 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
665 #ifdef RTE_PCI_CONFIG
667 * Set PCIe config space for high performance.
668 * Return value can be ignored.
670 pci_config_space_set(dev);
672 /* map resources for devices that use igb_uio */
673 ret = pci_map_device(dev);
676 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
677 rte_eal_process_type() == RTE_PROC_PRIMARY) {
678 /* unbind current driver */
679 if (pci_unbind_kernel_driver(dev) < 0)
683 /* reference driver structure */
686 /* call the driver devinit() function */
687 return dr->devinit(dr, dev);
689 /* return positive value if driver is not found */
693 #ifdef RTE_LIBRTE_EAL_HOTPLUG
695 * If vendor/device ID match, call the devuninit() function of the
699 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
700 struct rte_pci_device *dev)
702 struct rte_pci_id *id_table;
704 if ((dr == NULL) || (dev == NULL))
707 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
709 /* check if device's identifiers match the driver's ones */
710 if (id_table->vendor_id != dev->id.vendor_id &&
711 id_table->vendor_id != PCI_ANY_ID)
713 if (id_table->device_id != dev->id.device_id &&
714 id_table->device_id != PCI_ANY_ID)
716 if (id_table->subsystem_vendor_id !=
717 dev->id.subsystem_vendor_id &&
718 id_table->subsystem_vendor_id != PCI_ANY_ID)
720 if (id_table->subsystem_device_id !=
721 dev->id.subsystem_device_id &&
722 id_table->subsystem_device_id != PCI_ANY_ID)
725 struct rte_pci_addr *loc = &dev->addr;
728 "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
729 loc->domain, loc->bus, loc->devid,
730 loc->function, dev->numa_node);
732 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n",
733 dev->id.vendor_id, dev->id.device_id,
736 /* call the driver devuninit() function */
737 if (dr->devuninit && (dr->devuninit(dev) < 0))
738 return -1; /* negative value is an error */
740 /* clear driver structure */
743 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
744 /* unmap resources for devices that use igb_uio */
745 pci_unmap_device(dev);
749 /* return positive value if driver is not found */
752 #else /* RTE_LIBRTE_EAL_HOTPLUG */
754 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr __rte_unused,
755 struct rte_pci_device *dev __rte_unused)
757 RTE_LOG(ERR, EAL, "Hotplug support isn't enabled\n");
760 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
762 /* Init the PCI EAL subsystem */
764 rte_eal_pci_init(void)
766 TAILQ_INIT(&pci_driver_list);
767 TAILQ_INIT(&pci_device_list);
768 pci_res_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_PCI,
769 mapped_pci_res_list);
771 /* for debug purposes, PCI can be disabled */
772 if (internal_config.no_pci)
775 if (rte_eal_pci_scan() < 0) {
776 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
782 if (pci_vfio_is_enabled()) {
784 /* if we are primary process, create a thread to communicate with
785 * secondary processes. the thread will use a socket to wait for
786 * requests from secondary process to send open file descriptors,
787 * because VFIO does not allow multiple open descriptors on a group or
790 if (internal_config.process_type == RTE_PROC_PRIMARY &&
791 pci_vfio_mp_sync_setup() < 0)