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 /* parse the "resource" sysfs file */
173 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
177 union pci_resource_info {
183 char *ptrs[PCI_RESOURCE_FMT_NVAL];
186 uint64_t phys_addr, end_addr, flags;
188 f = fopen(filename, "r");
190 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
194 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
196 if (fgets(buf, sizeof(buf), f) == NULL) {
198 "%s(): cannot read resource\n", __func__);
202 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
204 "%s(): bad resource format\n", __func__);
208 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
209 end_addr = strtoull(res_info.end_addr, NULL, 16);
210 flags = strtoull(res_info.flags, NULL, 16);
213 "%s(): bad resource format\n", __func__);
217 if (flags & IORESOURCE_MEM) {
218 dev->mem_resource[i].phys_addr = phys_addr;
219 dev->mem_resource[i].len = end_addr - phys_addr + 1;
220 /* not mapped for now */
221 dev->mem_resource[i].addr = NULL;
232 /* Scan one pci sysfs entry, and fill the devices list from it. */
234 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
235 uint8_t devid, uint8_t function)
237 char filename[PATH_MAX];
239 struct rte_pci_device *dev;
240 char driver[PATH_MAX];
243 dev = malloc(sizeof(*dev));
247 memset(dev, 0, sizeof(*dev));
248 dev->addr.domain = domain;
250 dev->addr.devid = devid;
251 dev->addr.function = function;
254 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
255 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
259 dev->id.vendor_id = (uint16_t)tmp;
262 snprintf(filename, sizeof(filename), "%s/device", dirname);
263 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
267 dev->id.device_id = (uint16_t)tmp;
269 /* get subsystem_vendor id */
270 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
272 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
276 dev->id.subsystem_vendor_id = (uint16_t)tmp;
278 /* get subsystem_device id */
279 snprintf(filename, sizeof(filename), "%s/subsystem_device",
281 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
285 dev->id.subsystem_device_id = (uint16_t)tmp;
289 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
290 if (!access(filename, F_OK) &&
291 eal_parse_sysfs_value(filename, &tmp) == 0)
292 dev->max_vfs = (uint16_t)tmp;
294 /* for non igb_uio driver, need kernel version >= 3.8 */
295 snprintf(filename, sizeof(filename),
296 "%s/sriov_numvfs", dirname);
297 if (!access(filename, F_OK) &&
298 eal_parse_sysfs_value(filename, &tmp) == 0)
299 dev->max_vfs = (uint16_t)tmp;
303 snprintf(filename, sizeof(filename), "%s/numa_node",
305 if (access(filename, R_OK) != 0) {
306 /* if no NUMA support just set node to -1 */
309 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
313 dev->numa_node = tmp;
316 /* parse resources */
317 snprintf(filename, sizeof(filename), "%s/resource", dirname);
318 if (pci_parse_sysfs_resource(filename, dev) < 0) {
319 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
325 snprintf(filename, sizeof(filename), "%s/driver", dirname);
326 ret = pci_get_kernel_driver_by_path(filename, driver);
328 if (!strcmp(driver, "vfio-pci"))
329 dev->pt_driver = RTE_PT_VFIO;
330 else if (!strcmp(driver, "igb_uio"))
331 dev->pt_driver = RTE_PT_IGB_UIO;
332 else if (!strcmp(driver, "uio_pci_generic"))
333 dev->pt_driver = RTE_PT_UIO_GENERIC;
335 dev->pt_driver = RTE_PT_UNKNOWN;
336 } else if (ret < 0) {
337 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
341 dev->pt_driver = RTE_PT_UNKNOWN;
343 /* device is valid, add in list (sorted) */
344 if (TAILQ_EMPTY(&pci_device_list)) {
345 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
348 struct rte_pci_device *dev2 = NULL;
351 TAILQ_FOREACH(dev2, &pci_device_list, next) {
352 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
356 TAILQ_INSERT_BEFORE(dev2, dev, next);
358 } else { /* already registered */
359 /* update pt_driver */
360 dev2->pt_driver = dev->pt_driver;
361 dev2->max_vfs = dev->max_vfs;
362 memmove(dev2->mem_resource,
364 sizeof(dev->mem_resource));
369 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
376 * split up a pci address into its constituent parts.
379 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
380 uint8_t *bus, uint8_t *devid, uint8_t *function)
382 /* first split on ':' */
390 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
393 char *buf_copy = strndup(buf, bufsize);
394 if (buf_copy == NULL)
397 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
400 /* final split is on '.' between devid and function */
401 splitaddr.function = strchr(splitaddr.devid,'.');
402 if (splitaddr.function == NULL)
404 *splitaddr.function++ = '\0';
406 /* now convert to int values */
408 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
409 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
410 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
411 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
415 free(buf_copy); /* free the copy made with strdup */
423 * Scan the content of the PCI bus, and the devices in the devices
431 char dirname[PATH_MAX];
433 uint8_t bus, devid, function;
435 dir = opendir(SYSFS_PCI_DEVICES);
437 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
438 __func__, strerror(errno));
442 while ((e = readdir(dir)) != NULL) {
443 if (e->d_name[0] == '.')
446 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
447 &bus, &devid, &function) != 0)
450 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
452 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
463 #ifdef RTE_PCI_CONFIG
465 pci_config_extended_tag(struct rte_pci_device *dev)
467 struct rte_pci_addr *loc = &dev->addr;
468 char filename[PATH_MAX];
472 /* not configured, let it as is */
473 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
474 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
477 snprintf(filename, sizeof(filename),
478 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
479 loc->domain, loc->bus, loc->devid, loc->function);
480 f = fopen(filename, "rw+");
484 fgets(buf, sizeof(buf), f);
485 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
486 /* enable Extended Tag*/
487 if (strncmp(buf, "on", 2) != 0) {
488 fseek(f, 0, SEEK_SET);
492 /* disable Extended Tag */
493 if (strncmp(buf, "off", 3) != 0) {
494 fseek(f, 0, SEEK_SET);
504 pci_config_max_read_request_size(struct rte_pci_device *dev)
506 struct rte_pci_addr *loc = &dev->addr;
507 char filename[PATH_MAX];
508 char buf[BUFSIZ], param[BUFSIZ];
510 /* size can be 128, 256, 512, 1024, 2048, 4096 */
511 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
513 /* not configured, let it as is */
517 snprintf(filename, sizeof(filename),
518 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
519 loc->domain, loc->bus, loc->devid, loc->function);
520 f = fopen(filename, "rw+");
524 fgets(buf, sizeof(buf), f);
525 snprintf(param, sizeof(param), "%d", max_size);
527 /* check if the size to be set is the same as current */
528 if (strcmp(buf, param) == 0) {
532 fseek(f, 0, SEEK_SET);
540 pci_config_space_set(struct rte_pci_device *dev)
542 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
545 /* configure extended tag */
546 pci_config_extended_tag(dev);
548 /* configure max read request size */
549 pci_config_max_read_request_size(dev);
554 pci_map_device(struct rte_pci_device *dev)
558 /* try mapping the NIC resources using VFIO if it exists */
559 switch (dev->pt_driver) {
562 if (pci_vfio_is_enabled())
563 ret = pci_vfio_map_resource(dev);
567 case RTE_PT_UIO_GENERIC:
568 /* map resources for devices that use uio */
569 ret = pci_uio_map_resource(dev);
572 RTE_LOG(DEBUG, EAL, " Not managed by known pt driver,"
582 * If vendor/device ID match, call the devinit() function of the
586 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
589 struct rte_pci_id *id_table;
591 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
593 /* check if device's identifiers match the driver's ones */
594 if (id_table->vendor_id != dev->id.vendor_id &&
595 id_table->vendor_id != PCI_ANY_ID)
597 if (id_table->device_id != dev->id.device_id &&
598 id_table->device_id != PCI_ANY_ID)
600 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
601 id_table->subsystem_vendor_id != PCI_ANY_ID)
603 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
604 id_table->subsystem_device_id != PCI_ANY_ID)
607 struct rte_pci_addr *loc = &dev->addr;
609 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
610 loc->domain, loc->bus, loc->devid, loc->function,
613 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
614 dev->id.device_id, dr->name);
616 /* no initialization when blacklisted, return without error */
617 if (dev->devargs != NULL &&
618 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
619 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
623 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
624 #ifdef RTE_PCI_CONFIG
626 * Set PCIe config space for high performance.
627 * Return value can be ignored.
629 pci_config_space_set(dev);
631 /* map resources for devices that use igb_uio */
632 ret = pci_map_device(dev);
635 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
636 rte_eal_process_type() == RTE_PROC_PRIMARY) {
637 /* unbind current driver */
638 if (pci_unbind_kernel_driver(dev) < 0)
642 /* reference driver structure */
645 /* call the driver devinit() function */
646 return dr->devinit(dr, dev);
648 /* return positive value if driver is not found */
652 /* Init the PCI EAL subsystem */
654 rte_eal_pci_init(void)
656 TAILQ_INIT(&pci_driver_list);
657 TAILQ_INIT(&pci_device_list);
658 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
659 mapped_pci_res_list);
661 /* for debug purposes, PCI can be disabled */
662 if (internal_config.no_pci)
665 if (pci_scan() < 0) {
666 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
672 if (pci_vfio_is_enabled()) {
674 /* if we are primary process, create a thread to communicate with
675 * secondary processes. the thread will use a socket to wait for
676 * requests from secondary process to send open file descriptors,
677 * because VFIO does not allow multiple open descriptors on a group or
680 if (internal_config.process_type == RTE_PROC_PRIMARY &&
681 pci_vfio_mp_sync_setup() < 0)