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>
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 /* parse the "resource" sysfs file */
172 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
176 union pci_resource_info {
182 char *ptrs[PCI_RESOURCE_FMT_NVAL];
185 uint64_t phys_addr, end_addr, flags;
187 f = fopen(filename, "r");
189 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
193 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
195 if (fgets(buf, sizeof(buf), f) == NULL) {
197 "%s(): cannot read resource\n", __func__);
201 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
203 "%s(): bad resource format\n", __func__);
207 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
208 end_addr = strtoull(res_info.end_addr, NULL, 16);
209 flags = strtoull(res_info.flags, NULL, 16);
212 "%s(): bad resource format\n", __func__);
216 if (flags & IORESOURCE_MEM) {
217 dev->mem_resource[i].phys_addr = phys_addr;
218 dev->mem_resource[i].len = end_addr - phys_addr + 1;
219 /* not mapped for now */
220 dev->mem_resource[i].addr = NULL;
231 /* Compare two PCI device addresses. */
233 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
235 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
236 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
238 if (dev_addr > dev_addr2)
245 /* Scan one pci sysfs entry, and fill the devices list from it. */
247 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
248 uint8_t devid, uint8_t function)
250 char filename[PATH_MAX];
252 struct rte_pci_device *dev;
253 char driver[PATH_MAX];
256 dev = malloc(sizeof(*dev));
260 memset(dev, 0, sizeof(*dev));
261 dev->addr.domain = domain;
263 dev->addr.devid = devid;
264 dev->addr.function = function;
267 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
268 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
272 dev->id.vendor_id = (uint16_t)tmp;
275 snprintf(filename, sizeof(filename), "%s/device", dirname);
276 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
280 dev->id.device_id = (uint16_t)tmp;
282 /* get subsystem_vendor id */
283 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
285 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
289 dev->id.subsystem_vendor_id = (uint16_t)tmp;
291 /* get subsystem_device id */
292 snprintf(filename, sizeof(filename), "%s/subsystem_device",
294 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
298 dev->id.subsystem_device_id = (uint16_t)tmp;
302 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
303 if (!access(filename, F_OK) &&
304 eal_parse_sysfs_value(filename, &tmp) == 0)
305 dev->max_vfs = (uint16_t)tmp;
307 /* for non igb_uio driver, need kernel version >= 3.8 */
308 snprintf(filename, sizeof(filename),
309 "%s/sriov_numvfs", dirname);
310 if (!access(filename, F_OK) &&
311 eal_parse_sysfs_value(filename, &tmp) == 0)
312 dev->max_vfs = (uint16_t)tmp;
316 snprintf(filename, sizeof(filename), "%s/numa_node",
318 if (access(filename, R_OK) != 0) {
319 /* if no NUMA support just set node to -1 */
322 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
326 dev->numa_node = tmp;
329 /* parse resources */
330 snprintf(filename, sizeof(filename), "%s/resource", dirname);
331 if (pci_parse_sysfs_resource(filename, dev) < 0) {
332 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
338 snprintf(filename, sizeof(filename), "%s/driver", dirname);
339 ret = pci_get_kernel_driver_by_path(filename, driver);
341 if (!strcmp(driver, "vfio-pci"))
342 dev->pt_driver = RTE_PT_VFIO;
343 else if (!strcmp(driver, "igb_uio"))
344 dev->pt_driver = RTE_PT_IGB_UIO;
345 else if (!strcmp(driver, "uio_pci_generic"))
346 dev->pt_driver = RTE_PT_UIO_GENERIC;
348 dev->pt_driver = RTE_PT_UNKNOWN;
349 } else if (ret < 0) {
350 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
354 dev->pt_driver = RTE_PT_UNKNOWN;
356 /* device is valid, add in list (sorted) */
357 if (TAILQ_EMPTY(&pci_device_list)) {
358 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
361 struct rte_pci_device *dev2 = NULL;
363 TAILQ_FOREACH(dev2, &pci_device_list, next) {
364 if (pci_addr_comparison(&dev->addr, &dev2->addr))
367 TAILQ_INSERT_BEFORE(dev2, dev, next);
371 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
378 * split up a pci address into its constituent parts.
381 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
382 uint8_t *bus, uint8_t *devid, uint8_t *function)
384 /* first split on ':' */
392 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
395 char *buf_copy = strndup(buf, bufsize);
396 if (buf_copy == NULL)
399 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
402 /* final split is on '.' between devid and function */
403 splitaddr.function = strchr(splitaddr.devid,'.');
404 if (splitaddr.function == NULL)
406 *splitaddr.function++ = '\0';
408 /* now convert to int values */
410 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
411 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
412 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
413 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
417 free(buf_copy); /* free the copy made with strdup */
425 * Scan the content of the PCI bus, and the devices in the devices
433 char dirname[PATH_MAX];
435 uint8_t bus, devid, function;
437 dir = opendir(SYSFS_PCI_DEVICES);
439 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
440 __func__, strerror(errno));
444 while ((e = readdir(dir)) != NULL) {
445 if (e->d_name[0] == '.')
448 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
449 &bus, &devid, &function) != 0)
452 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
454 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
465 #ifdef RTE_PCI_CONFIG
467 pci_config_extended_tag(struct rte_pci_device *dev)
469 struct rte_pci_addr *loc = &dev->addr;
470 char filename[PATH_MAX];
474 /* not configured, let it as is */
475 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
476 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
479 snprintf(filename, sizeof(filename),
480 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
481 loc->domain, loc->bus, loc->devid, loc->function);
482 f = fopen(filename, "rw+");
486 fgets(buf, sizeof(buf), f);
487 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
488 /* enable Extended Tag*/
489 if (strncmp(buf, "on", 2) != 0) {
490 fseek(f, 0, SEEK_SET);
494 /* disable Extended Tag */
495 if (strncmp(buf, "off", 3) != 0) {
496 fseek(f, 0, SEEK_SET);
506 pci_config_max_read_request_size(struct rte_pci_device *dev)
508 struct rte_pci_addr *loc = &dev->addr;
509 char filename[PATH_MAX];
510 char buf[BUFSIZ], param[BUFSIZ];
512 /* size can be 128, 256, 512, 1024, 2048, 4096 */
513 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
515 /* not configured, let it as is */
519 snprintf(filename, sizeof(filename),
520 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
521 loc->domain, loc->bus, loc->devid, loc->function);
522 f = fopen(filename, "rw+");
526 fgets(buf, sizeof(buf), f);
527 snprintf(param, sizeof(param), "%d", max_size);
529 /* check if the size to be set is the same as current */
530 if (strcmp(buf, param) == 0) {
534 fseek(f, 0, SEEK_SET);
542 pci_config_space_set(struct rte_pci_device *dev)
544 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
547 /* configure extended tag */
548 pci_config_extended_tag(dev);
550 /* configure max read request size */
551 pci_config_max_read_request_size(dev);
556 pci_map_device(struct rte_pci_device *dev)
560 /* try mapping the NIC resources using VFIO if it exists */
561 switch (dev->pt_driver) {
564 if (pci_vfio_is_enabled())
565 ret = pci_vfio_map_resource(dev);
569 case RTE_PT_UIO_GENERIC:
570 /* map resources for devices that use uio */
571 ret = pci_uio_map_resource(dev);
574 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
584 * If vendor/device ID match, call the devinit() function of the
588 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
591 struct rte_pci_id *id_table;
593 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
595 /* check if device's identifiers match the driver's ones */
596 if (id_table->vendor_id != dev->id.vendor_id &&
597 id_table->vendor_id != PCI_ANY_ID)
599 if (id_table->device_id != dev->id.device_id &&
600 id_table->device_id != PCI_ANY_ID)
602 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
603 id_table->subsystem_vendor_id != PCI_ANY_ID)
605 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
606 id_table->subsystem_device_id != PCI_ANY_ID)
609 struct rte_pci_addr *loc = &dev->addr;
611 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
612 loc->domain, loc->bus, loc->devid, loc->function,
615 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
616 dev->id.device_id, dr->name);
618 /* no initialization when blacklisted, return without error */
619 if (dev->devargs != NULL &&
620 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
621 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
625 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
626 #ifdef RTE_PCI_CONFIG
628 * Set PCIe config space for high performance.
629 * Return value can be ignored.
631 pci_config_space_set(dev);
633 /* map resources for devices that use igb_uio */
634 ret = pci_map_device(dev);
637 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
638 rte_eal_process_type() == RTE_PROC_PRIMARY) {
639 /* unbind current driver */
640 if (pci_unbind_kernel_driver(dev) < 0)
644 /* reference driver structure */
647 /* call the driver devinit() function */
648 return dr->devinit(dr, dev);
650 /* return positive value if driver is not found */
654 /* Init the PCI EAL subsystem */
656 rte_eal_pci_init(void)
658 TAILQ_INIT(&pci_driver_list);
659 TAILQ_INIT(&pci_device_list);
660 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
661 mapped_pci_res_list);
663 /* for debug purposes, PCI can be disabled */
664 if (internal_config.no_pci)
667 if (pci_scan() < 0) {
668 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
674 if (pci_vfio_is_enabled()) {
676 /* if we are primary process, create a thread to communicate with
677 * secondary processes. the thread will use a socket to wait for
678 * requests from secondary process to send open file descriptors,
679 * because VFIO does not allow multiple open descriptors on a group or
682 if (internal_config.process_type == RTE_PROC_PRIMARY &&
683 pci_vfio_mp_sync_setup() < 0)