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_find_max_end_va(void)
103 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
104 const struct rte_memseg *last = seg;
107 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
108 if (seg->addr == NULL)
111 if (seg->addr > last->addr)
115 return RTE_PTR_ADD(last->addr, last->len);
119 /* map a particular resource from a file */
121 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size)
125 /* Map the PCI memory resource of device */
126 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
127 MAP_SHARED, fd, offset);
128 if (mapaddr == MAP_FAILED) {
129 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
130 __func__, fd, requested_addr,
131 (unsigned long)size, (unsigned long)offset,
132 strerror(errno), mapaddr);
134 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
140 /* parse the "resource" sysfs file */
142 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
146 union pci_resource_info {
152 char *ptrs[PCI_RESOURCE_FMT_NVAL];
155 uint64_t phys_addr, end_addr, flags;
157 f = fopen(filename, "r");
159 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
163 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
165 if (fgets(buf, sizeof(buf), f) == NULL) {
167 "%s(): cannot read resource\n", __func__);
171 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
173 "%s(): bad resource format\n", __func__);
177 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
178 end_addr = strtoull(res_info.end_addr, NULL, 16);
179 flags = strtoull(res_info.flags, NULL, 16);
182 "%s(): bad resource format\n", __func__);
186 if (flags & IORESOURCE_MEM) {
187 dev->mem_resource[i].phys_addr = phys_addr;
188 dev->mem_resource[i].len = end_addr - phys_addr + 1;
189 /* not mapped for now */
190 dev->mem_resource[i].addr = NULL;
201 /* Compare two PCI device addresses. */
203 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
205 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
206 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
208 if (dev_addr > dev_addr2)
215 /* Scan one pci sysfs entry, and fill the devices list from it. */
217 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
218 uint8_t devid, uint8_t function)
220 char filename[PATH_MAX];
222 struct rte_pci_device *dev;
224 dev = malloc(sizeof(*dev));
229 memset(dev, 0, sizeof(*dev));
230 dev->addr.domain = domain;
232 dev->addr.devid = devid;
233 dev->addr.function = function;
236 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
237 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
241 dev->id.vendor_id = (uint16_t)tmp;
244 snprintf(filename, sizeof(filename), "%s/device", dirname);
245 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
249 dev->id.device_id = (uint16_t)tmp;
251 /* get subsystem_vendor id */
252 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
254 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
258 dev->id.subsystem_vendor_id = (uint16_t)tmp;
260 /* get subsystem_device id */
261 snprintf(filename, sizeof(filename), "%s/subsystem_device",
263 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
267 dev->id.subsystem_device_id = (uint16_t)tmp;
271 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
272 if (!access(filename, F_OK) &&
273 eal_parse_sysfs_value(filename, &tmp) == 0)
274 dev->max_vfs = (uint16_t)tmp;
276 /* for non igb_uio driver, need kernel version >= 3.8 */
277 snprintf(filename, sizeof(filename),
278 "%s/sriov_numvfs", dirname);
279 if (!access(filename, F_OK) &&
280 eal_parse_sysfs_value(filename, &tmp) == 0)
281 dev->max_vfs = (uint16_t)tmp;
285 snprintf(filename, sizeof(filename), "%s/numa_node",
287 if (access(filename, R_OK) != 0) {
288 /* if no NUMA support just set node to -1 */
291 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
295 dev->numa_node = tmp;
298 /* parse resources */
299 snprintf(filename, sizeof(filename), "%s/resource", dirname);
300 if (pci_parse_sysfs_resource(filename, dev) < 0) {
301 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
306 /* device is valid, add in list (sorted) */
307 if (TAILQ_EMPTY(&pci_device_list)) {
308 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
311 struct rte_pci_device *dev2 = NULL;
313 TAILQ_FOREACH(dev2, &pci_device_list, next) {
314 if (pci_addr_comparison(&dev->addr, &dev2->addr))
317 TAILQ_INSERT_BEFORE(dev2, dev, next);
321 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
328 * split up a pci address into its constituent parts.
331 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
332 uint8_t *bus, uint8_t *devid, uint8_t *function)
334 /* first split on ':' */
342 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
345 char *buf_copy = strndup(buf, bufsize);
346 if (buf_copy == NULL)
349 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
352 /* final split is on '.' between devid and function */
353 splitaddr.function = strchr(splitaddr.devid,'.');
354 if (splitaddr.function == NULL)
356 *splitaddr.function++ = '\0';
358 /* now convert to int values */
360 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
361 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
362 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
363 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
367 free(buf_copy); /* free the copy made with strdup */
375 * Scan the content of the PCI bus, and the devices in the devices
383 char dirname[PATH_MAX];
385 uint8_t bus, devid, function;
387 dir = opendir(SYSFS_PCI_DEVICES);
389 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
390 __func__, strerror(errno));
394 while ((e = readdir(dir)) != NULL) {
395 if (e->d_name[0] == '.')
398 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
399 &bus, &devid, &function) != 0)
402 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
404 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
415 #ifdef RTE_PCI_CONFIG
417 pci_config_extended_tag(struct rte_pci_device *dev)
419 struct rte_pci_addr *loc = &dev->addr;
420 char filename[PATH_MAX];
424 /* not configured, let it as is */
425 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
426 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
429 snprintf(filename, sizeof(filename),
430 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
431 loc->domain, loc->bus, loc->devid, loc->function);
432 f = fopen(filename, "rw+");
436 fgets(buf, sizeof(buf), f);
437 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
438 /* enable Extended Tag*/
439 if (strncmp(buf, "on", 2) != 0) {
440 fseek(f, 0, SEEK_SET);
444 /* disable Extended Tag */
445 if (strncmp(buf, "off", 3) != 0) {
446 fseek(f, 0, SEEK_SET);
456 pci_config_max_read_request_size(struct rte_pci_device *dev)
458 struct rte_pci_addr *loc = &dev->addr;
459 char filename[PATH_MAX];
460 char buf[BUFSIZ], param[BUFSIZ];
462 /* size can be 128, 256, 512, 1024, 2048, 4096 */
463 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
465 /* not configured, let it as is */
469 snprintf(filename, sizeof(filename),
470 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
471 loc->domain, loc->bus, loc->devid, loc->function);
472 f = fopen(filename, "rw+");
476 fgets(buf, sizeof(buf), f);
477 snprintf(param, sizeof(param), "%d", max_size);
479 /* check if the size to be set is the same as current */
480 if (strcmp(buf, param) == 0) {
484 fseek(f, 0, SEEK_SET);
492 pci_config_space_set(struct rte_pci_device *dev)
494 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
497 /* configure extended tag */
498 pci_config_extended_tag(dev);
500 /* configure max read request size */
501 pci_config_max_read_request_size(dev);
506 pci_map_device(struct rte_pci_device *dev)
510 /* try mapping the NIC resources using VFIO if it exists */
512 if (pci_vfio_is_enabled()) {
513 ret = pci_vfio_map_resource(dev);
520 /* map resources for devices that use uio_pci_generic or igb_uio */
522 ret = pci_uio_map_resource(dev);
530 * If vendor/device ID match, call the devinit() function of the
534 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
537 struct rte_pci_id *id_table;
539 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
541 /* check if device's identifiers match the driver's ones */
542 if (id_table->vendor_id != dev->id.vendor_id &&
543 id_table->vendor_id != PCI_ANY_ID)
545 if (id_table->device_id != dev->id.device_id &&
546 id_table->device_id != PCI_ANY_ID)
548 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
549 id_table->subsystem_vendor_id != PCI_ANY_ID)
551 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
552 id_table->subsystem_device_id != PCI_ANY_ID)
555 struct rte_pci_addr *loc = &dev->addr;
557 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
558 loc->domain, loc->bus, loc->devid, loc->function,
561 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
562 dev->id.device_id, dr->name);
564 /* no initialization when blacklisted, return without error */
565 if (dev->devargs != NULL &&
566 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
567 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
571 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
572 #ifdef RTE_PCI_CONFIG
574 * Set PCIe config space for high performance.
575 * Return value can be ignored.
577 pci_config_space_set(dev);
579 /* map resources for devices that use igb_uio */
580 ret = pci_map_device(dev);
583 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
584 rte_eal_process_type() == RTE_PROC_PRIMARY) {
585 /* unbind current driver */
586 if (pci_unbind_kernel_driver(dev) < 0)
590 /* reference driver structure */
593 /* call the driver devinit() function */
594 return dr->devinit(dr, dev);
596 /* return positive value if driver is not found */
600 /* Init the PCI EAL subsystem */
602 rte_eal_pci_init(void)
604 TAILQ_INIT(&pci_driver_list);
605 TAILQ_INIT(&pci_device_list);
606 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
607 mapped_pci_res_list);
609 /* for debug purposes, PCI can be disabled */
610 if (internal_config.no_pci)
613 if (pci_scan() < 0) {
614 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
620 if (pci_vfio_is_enabled()) {
622 /* if we are primary process, create a thread to communicate with
623 * secondary processes. the thread will use a socket to wait for
624 * requests from secondary process to send open file descriptors,
625 * because VFIO does not allow multiple open descriptors on a group or
628 if (internal_config.process_type == RTE_PROC_PRIMARY &&
629 pci_vfio_mp_sync_setup() < 0)