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 "rte_pci_dev_ids.h"
45 #include "eal_filesystem.h"
46 #include "eal_private.h"
47 #include "eal_pci_init.h"
51 * PCI probing under linux
53 * This code is used to simulate a PCI probe by parsing information in sysfs.
54 * When a registered device matches a driver, it is then initialized with
55 * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
58 /* unbind kernel driver for this device */
60 pci_unbind_kernel_driver(struct rte_pci_device *dev)
64 char filename[PATH_MAX];
66 struct rte_pci_addr *loc = &dev->addr;
68 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
69 snprintf(filename, sizeof(filename),
70 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
71 loc->domain, loc->bus, loc->devid, loc->function);
73 f = fopen(filename, "w");
74 if (f == NULL) /* device was not bound */
77 n = snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
78 loc->domain, loc->bus, loc->devid, loc->function);
79 if ((n < 0) || (n >= (int)sizeof(buf))) {
80 RTE_LOG(ERR, EAL, "%s(): snprintf failed\n", __func__);
83 if (fwrite(buf, n, 1, f) == 0) {
84 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
98 pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
104 if (!filename || !dri_name)
107 count = readlink(filename, path, PATH_MAX);
108 if (count >= PATH_MAX)
111 /* For device does not have a driver */
117 name = strrchr(path, '/');
119 strncpy(dri_name, name + 1, strlen(name + 1) + 1);
128 pci_map_device(struct rte_pci_device *dev)
132 /* try mapping the NIC resources using VFIO if it exists */
136 if (pci_vfio_is_enabled())
137 ret = pci_vfio_map_resource(dev);
140 case RTE_KDRV_IGB_UIO:
141 case RTE_KDRV_UIO_GENERIC:
142 /* map resources for devices that use uio */
143 ret = pci_uio_map_resource(dev);
147 " Not managed by a supported kernel driver, skipped\n");
155 /* Unmap pci device */
157 pci_unmap_device(struct rte_pci_device *dev)
159 /* try unmapping the NIC resources using VFIO if it exists */
162 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
164 case RTE_KDRV_IGB_UIO:
165 case RTE_KDRV_UIO_GENERIC:
166 /* unmap resources for devices that use uio */
167 pci_uio_unmap_resource(dev);
171 " Not managed by a supported kernel driver, skipped\n");
177 pci_find_max_end_va(void)
179 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
180 const struct rte_memseg *last = seg;
183 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
184 if (seg->addr == NULL)
187 if (seg->addr > last->addr)
191 return RTE_PTR_ADD(last->addr, last->len);
194 /* parse the "resource" sysfs file */
196 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
200 union pci_resource_info {
206 char *ptrs[PCI_RESOURCE_FMT_NVAL];
209 uint64_t phys_addr, end_addr, flags;
211 f = fopen(filename, "r");
213 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
217 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
219 if (fgets(buf, sizeof(buf), f) == NULL) {
221 "%s(): cannot read resource\n", __func__);
225 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
227 "%s(): bad resource format\n", __func__);
231 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
232 end_addr = strtoull(res_info.end_addr, NULL, 16);
233 flags = strtoull(res_info.flags, NULL, 16);
236 "%s(): bad resource format\n", __func__);
240 if (flags & IORESOURCE_MEM) {
241 dev->mem_resource[i].phys_addr = phys_addr;
242 dev->mem_resource[i].len = end_addr - phys_addr + 1;
243 /* not mapped for now */
244 dev->mem_resource[i].addr = NULL;
255 /* Scan one pci sysfs entry, and fill the devices list from it. */
257 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
258 uint8_t devid, uint8_t function)
260 char filename[PATH_MAX];
262 struct rte_pci_device *dev;
263 char driver[PATH_MAX];
266 dev = malloc(sizeof(*dev));
270 memset(dev, 0, sizeof(*dev));
271 dev->addr.domain = domain;
273 dev->addr.devid = devid;
274 dev->addr.function = function;
277 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
278 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
282 dev->id.vendor_id = (uint16_t)tmp;
285 snprintf(filename, sizeof(filename), "%s/device", dirname);
286 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
290 dev->id.device_id = (uint16_t)tmp;
292 /* get subsystem_vendor id */
293 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
295 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
299 dev->id.subsystem_vendor_id = (uint16_t)tmp;
301 /* get subsystem_device id */
302 snprintf(filename, sizeof(filename), "%s/subsystem_device",
304 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
308 dev->id.subsystem_device_id = (uint16_t)tmp;
312 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
313 if (!access(filename, F_OK) &&
314 eal_parse_sysfs_value(filename, &tmp) == 0)
315 dev->max_vfs = (uint16_t)tmp;
317 /* for non igb_uio driver, need kernel version >= 3.8 */
318 snprintf(filename, sizeof(filename),
319 "%s/sriov_numvfs", dirname);
320 if (!access(filename, F_OK) &&
321 eal_parse_sysfs_value(filename, &tmp) == 0)
322 dev->max_vfs = (uint16_t)tmp;
326 snprintf(filename, sizeof(filename), "%s/numa_node",
328 if (access(filename, R_OK) != 0) {
329 /* if no NUMA support just set node to -1 */
332 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
336 dev->numa_node = tmp;
339 /* parse resources */
340 snprintf(filename, sizeof(filename), "%s/resource", dirname);
341 if (pci_parse_sysfs_resource(filename, dev) < 0) {
342 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
348 snprintf(filename, sizeof(filename), "%s/driver", dirname);
349 ret = pci_get_kernel_driver_by_path(filename, driver);
351 if (!strcmp(driver, "vfio-pci"))
352 dev->kdrv = RTE_KDRV_VFIO;
353 else if (!strcmp(driver, "igb_uio"))
354 dev->kdrv = RTE_KDRV_IGB_UIO;
355 else if (!strcmp(driver, "uio_pci_generic"))
356 dev->kdrv = RTE_KDRV_UIO_GENERIC;
358 dev->kdrv = RTE_KDRV_UNKNOWN;
359 } else if (ret < 0) {
360 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
364 dev->kdrv = RTE_KDRV_UNKNOWN;
366 /* device is valid, add in list (sorted) */
367 if (TAILQ_EMPTY(&pci_device_list)) {
368 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
371 struct rte_pci_device *dev2 = NULL;
374 TAILQ_FOREACH(dev2, &pci_device_list, next) {
375 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
379 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,
386 sizeof(dev->mem_resource));
391 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
398 * split up a pci address into its constituent parts.
401 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
402 uint8_t *bus, uint8_t *devid, uint8_t *function)
404 /* first split on ':' */
412 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
415 char *buf_copy = strndup(buf, bufsize);
416 if (buf_copy == NULL)
419 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
422 /* final split is on '.' between devid and function */
423 splitaddr.function = strchr(splitaddr.devid,'.');
424 if (splitaddr.function == NULL)
426 *splitaddr.function++ = '\0';
428 /* now convert to int values */
430 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
431 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
432 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
433 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
437 free(buf_copy); /* free the copy made with strdup */
445 * Scan the content of the PCI bus, and the devices in the devices
449 rte_eal_pci_scan(void)
453 char dirname[PATH_MAX];
455 uint8_t bus, devid, function;
457 dir = opendir(SYSFS_PCI_DEVICES);
459 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
460 __func__, strerror(errno));
464 while ((e = readdir(dir)) != NULL) {
465 if (e->d_name[0] == '.')
468 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
469 &bus, &devid, &function) != 0)
472 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
474 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
485 #ifdef RTE_PCI_CONFIG
487 pci_config_extended_tag(struct rte_pci_device *dev)
489 struct rte_pci_addr *loc = &dev->addr;
490 char filename[PATH_MAX];
494 /* not configured, let it as is */
495 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
496 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
499 snprintf(filename, sizeof(filename),
500 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
501 loc->domain, loc->bus, loc->devid, loc->function);
502 f = fopen(filename, "rw+");
506 fgets(buf, sizeof(buf), f);
507 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
508 /* enable Extended Tag*/
509 if (strncmp(buf, "on", 2) != 0) {
510 fseek(f, 0, SEEK_SET);
514 /* disable Extended Tag */
515 if (strncmp(buf, "off", 3) != 0) {
516 fseek(f, 0, SEEK_SET);
526 pci_config_max_read_request_size(struct rte_pci_device *dev)
528 struct rte_pci_addr *loc = &dev->addr;
529 char filename[PATH_MAX];
530 char buf[BUFSIZ], param[BUFSIZ];
532 /* size can be 128, 256, 512, 1024, 2048, 4096 */
533 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
535 /* not configured, let it as is */
539 snprintf(filename, sizeof(filename),
540 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
541 loc->domain, loc->bus, loc->devid, loc->function);
542 f = fopen(filename, "rw+");
546 fgets(buf, sizeof(buf), f);
547 snprintf(param, sizeof(param), "%d", max_size);
549 /* check if the size to be set is the same as current */
550 if (strcmp(buf, param) == 0) {
554 fseek(f, 0, SEEK_SET);
562 pci_config_space_set(struct rte_pci_device *dev)
564 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
567 /* configure extended tag */
568 pci_config_extended_tag(dev);
570 /* configure max read request size */
571 pci_config_max_read_request_size(dev);
575 /* Init the PCI EAL subsystem */
577 rte_eal_pci_init(void)
579 TAILQ_INIT(&pci_driver_list);
580 TAILQ_INIT(&pci_device_list);
582 /* for debug purposes, PCI can be disabled */
583 if (internal_config.no_pci)
586 if (rte_eal_pci_scan() < 0) {
587 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
593 if (pci_vfio_is_enabled()) {
595 /* if we are primary process, create a thread to communicate with
596 * secondary processes. the thread will use a socket to wait for
597 * requests from secondary process to send open file descriptors,
598 * because VFIO does not allow multiple open descriptors on a group or
601 if (internal_config.process_type == RTE_PROC_PRIMARY &&
602 pci_vfio_mp_sync_setup() < 0)