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.
39 #include <rte_eal_memconfig.h>
40 #include <rte_malloc.h>
41 #include <rte_devargs.h>
42 #include <rte_memcpy.h>
44 #include "eal_filesystem.h"
45 #include "eal_private.h"
46 #include "eal_pci_init.h"
50 * PCI probing under linux
52 * This code is used to simulate a PCI probe by parsing information in sysfs.
53 * When a registered device matches a driver, it is then initialized with
54 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
57 /* unbind kernel driver for this device */
59 pci_unbind_kernel_driver(struct rte_pci_device *dev)
63 char filename[PATH_MAX];
65 struct rte_pci_addr *loc = &dev->addr;
67 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
68 snprintf(filename, sizeof(filename),
69 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
70 loc->domain, loc->bus, loc->devid, loc->function);
72 f = fopen(filename, "w");
73 if (f == NULL) /* device was not bound */
76 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
77 loc->domain, loc->bus, loc->devid, loc->function);
78 if ((n < 0) || (n >= (int)sizeof(buf))) {
79 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
82 if (fwrite(buf, n, 1, f) == 0) {
83 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
97 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
103 if (!filename || !dri_name)
106 count = readlink(filename, path, PATH_MAX);
107 if (count >= PATH_MAX)
110 /* For device does not have a driver */
116 name = strrchr(path, '/');
118 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
127 pci_map_device(struct rte_pci_device *dev)
131 /* try mapping the NIC resources using VFIO if it exists */
135 if (pci_vfio_is_enabled())
136 ret = pci_vfio_map_resource(dev);
139 case RTE_KDRV_IGB_UIO:
140 case RTE_KDRV_UIO_GENERIC:
141 /* map resources for devices that use uio */
142 ret = pci_uio_map_resource(dev);
146 " Not managed by a supported kernel driver, skipped\n");
154 /* Unmap pci device */
156 pci_unmap_device(struct rte_pci_device *dev)
158 /* try unmapping the NIC resources using VFIO if it exists */
161 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
163 case RTE_KDRV_IGB_UIO:
164 case RTE_KDRV_UIO_GENERIC:
165 /* unmap resources for devices that use uio */
166 pci_uio_unmap_resource(dev);
170 " Not managed by a supported kernel driver, skipped\n");
176 pci_find_max_end_va(void)
178 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
179 const struct rte_memseg *last = seg;
182 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
183 if (seg->addr == NULL)
186 if (seg->addr > last->addr)
190 return RTE_PTR_ADD(last->addr, last->len);
193 /* parse the "resource" sysfs file */
195 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
199 union pci_resource_info {
205 char *ptrs[PCI_RESOURCE_FMT_NVAL];
208 uint64_t phys_addr, end_addr, flags;
210 f = fopen(filename, "r");
212 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
216 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
218 if (fgets(buf, sizeof(buf), f) == NULL) {
220 "%s(): cannot read resource\n", __func__);
224 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
226 "%s(): bad resource format\n", __func__);
230 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
231 end_addr = strtoull(res_info.end_addr, NULL, 16);
232 flags = strtoull(res_info.flags, NULL, 16);
235 "%s(): bad resource format\n", __func__);
239 if (flags & IORESOURCE_MEM) {
240 dev->mem_resource[i].phys_addr = phys_addr;
241 dev->mem_resource[i].len = end_addr - phys_addr + 1;
242 /* not mapped for now */
243 dev->mem_resource[i].addr = NULL;
254 /* Scan one pci sysfs entry, and fill the devices list from it. */
256 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
257 uint8_t devid, uint8_t function)
259 char filename[PATH_MAX];
261 struct rte_pci_device *dev;
262 char driver[PATH_MAX];
265 dev = malloc(sizeof(*dev));
269 memset(dev, 0, sizeof(*dev));
270 dev->addr.domain = domain;
272 dev->addr.devid = devid;
273 dev->addr.function = function;
276 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
277 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
281 dev->id.vendor_id = (uint16_t)tmp;
284 snprintf(filename, sizeof(filename), "%s/device", dirname);
285 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
289 dev->id.device_id = (uint16_t)tmp;
291 /* get subsystem_vendor id */
292 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
294 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
298 dev->id.subsystem_vendor_id = (uint16_t)tmp;
300 /* get subsystem_device id */
301 snprintf(filename, sizeof(filename), "%s/subsystem_device",
303 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
307 dev->id.subsystem_device_id = (uint16_t)tmp;
311 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
312 if (!access(filename, F_OK) &&
313 eal_parse_sysfs_value(filename, &tmp) == 0)
314 dev->max_vfs = (uint16_t)tmp;
316 /* for non igb_uio driver, need kernel version >= 3.8 */
317 snprintf(filename, sizeof(filename),
318 "%s/sriov_numvfs", dirname);
319 if (!access(filename, F_OK) &&
320 eal_parse_sysfs_value(filename, &tmp) == 0)
321 dev->max_vfs = (uint16_t)tmp;
325 snprintf(filename, sizeof(filename), "%s/numa_node",
327 if (access(filename, R_OK) != 0) {
328 /* if no NUMA support, set default to 0 */
331 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
335 dev->numa_node = tmp;
338 /* parse resources */
339 snprintf(filename, sizeof(filename), "%s/resource", dirname);
340 if (pci_parse_sysfs_resource(filename, dev) < 0) {
341 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
347 snprintf(filename, sizeof(filename), "%s/driver", dirname);
348 ret = pci_get_kernel_driver_by_path(filename, driver);
350 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
356 if (!strcmp(driver, "vfio-pci"))
357 dev->kdrv = RTE_KDRV_VFIO;
358 else if (!strcmp(driver, "igb_uio"))
359 dev->kdrv = RTE_KDRV_IGB_UIO;
360 else if (!strcmp(driver, "uio_pci_generic"))
361 dev->kdrv = RTE_KDRV_UIO_GENERIC;
363 dev->kdrv = RTE_KDRV_UNKNOWN;
365 dev->kdrv = RTE_KDRV_UNKNOWN;
367 /* device is valid, add in list (sorted) */
368 if (TAILQ_EMPTY(&pci_device_list)) {
369 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
371 struct rte_pci_device *dev2;
374 TAILQ_FOREACH(dev2, &pci_device_list, next) {
375 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
380 TAILQ_INSERT_BEFORE(dev2, dev, next);
381 } else { /* already registered */
382 dev2->kdrv = dev->kdrv;
383 dev2->max_vfs = dev->max_vfs;
384 memmove(dev2->mem_resource, dev->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);
574 /* Read PCI config space. */
575 int rte_eal_pci_read_config(const struct rte_pci_device *device,
576 void *buf, size_t len, off_t offset)
578 const struct rte_intr_handle *intr_handle = &device->intr_handle;
580 switch (intr_handle->type) {
581 case RTE_INTR_HANDLE_UIO:
582 case RTE_INTR_HANDLE_UIO_INTX:
583 return pci_uio_read_config(intr_handle, buf, len, offset);
586 case RTE_INTR_HANDLE_VFIO_MSIX:
587 case RTE_INTR_HANDLE_VFIO_MSI:
588 case RTE_INTR_HANDLE_VFIO_LEGACY:
589 return pci_vfio_read_config(intr_handle, buf, len, offset);
593 "Unknown handle type of fd %d\n",
599 /* Write PCI config space. */
600 int rte_eal_pci_write_config(const struct rte_pci_device *device,
601 const void *buf, size_t len, off_t offset)
603 const struct rte_intr_handle *intr_handle = &device->intr_handle;
605 switch (intr_handle->type) {
606 case RTE_INTR_HANDLE_UIO:
607 case RTE_INTR_HANDLE_UIO_INTX:
608 return pci_uio_write_config(intr_handle, buf, len, offset);
611 case RTE_INTR_HANDLE_VFIO_MSIX:
612 case RTE_INTR_HANDLE_VFIO_MSI:
613 case RTE_INTR_HANDLE_VFIO_LEGACY:
614 return pci_vfio_write_config(intr_handle, buf, len, offset);
618 "Unknown handle type of fd %d\n",
624 /* Init the PCI EAL subsystem */
626 rte_eal_pci_init(void)
628 TAILQ_INIT(&pci_driver_list);
629 TAILQ_INIT(&pci_device_list);
631 /* for debug purposes, PCI can be disabled */
632 if (internal_config.no_pci)
635 if (rte_eal_pci_scan() < 0) {
636 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
642 if (pci_vfio_is_enabled()) {
644 /* if we are primary process, create a thread to communicate with
645 * secondary processes. the thread will use a socket to wait for
646 * requests from secondary process to send open file descriptors,
647 * because VFIO does not allow multiple open descriptors on a group or
650 if (internal_config.process_type == RTE_PROC_PRIMARY &&
651 pci_vfio_mp_sync_setup() < 0)