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);
127 pci_find_max_end_va(void)
129 const struct rte_memseg *seg = rte_eal_get_physmem_layout();
130 const struct rte_memseg *last = seg;
133 for (i = 0; i < RTE_MAX_MEMSEG; i++, seg++) {
134 if (seg->addr == NULL)
137 if (seg->addr > last->addr)
141 return RTE_PTR_ADD(last->addr, last->len);
144 /* parse the "resource" sysfs file */
146 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
150 union pci_resource_info {
156 char *ptrs[PCI_RESOURCE_FMT_NVAL];
159 uint64_t phys_addr, end_addr, flags;
161 f = fopen(filename, "r");
163 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
167 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
169 if (fgets(buf, sizeof(buf), f) == NULL) {
171 "%s(): cannot read resource\n", __func__);
175 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
177 "%s(): bad resource format\n", __func__);
181 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
182 end_addr = strtoull(res_info.end_addr, NULL, 16);
183 flags = strtoull(res_info.flags, NULL, 16);
186 "%s(): bad resource format\n", __func__);
190 if (flags & IORESOURCE_MEM) {
191 dev->mem_resource[i].phys_addr = phys_addr;
192 dev->mem_resource[i].len = end_addr - phys_addr + 1;
193 /* not mapped for now */
194 dev->mem_resource[i].addr = NULL;
205 /* Scan one pci sysfs entry, and fill the devices list from it. */
207 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
208 uint8_t devid, uint8_t function)
210 char filename[PATH_MAX];
212 struct rte_pci_device *dev;
213 char driver[PATH_MAX];
216 dev = malloc(sizeof(*dev));
220 memset(dev, 0, sizeof(*dev));
221 dev->addr.domain = domain;
223 dev->addr.devid = devid;
224 dev->addr.function = function;
227 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
228 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
232 dev->id.vendor_id = (uint16_t)tmp;
235 snprintf(filename, sizeof(filename), "%s/device", dirname);
236 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
240 dev->id.device_id = (uint16_t)tmp;
242 /* get subsystem_vendor id */
243 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
245 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
249 dev->id.subsystem_vendor_id = (uint16_t)tmp;
251 /* get subsystem_device id */
252 snprintf(filename, sizeof(filename), "%s/subsystem_device",
254 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
258 dev->id.subsystem_device_id = (uint16_t)tmp;
262 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
263 if (!access(filename, F_OK) &&
264 eal_parse_sysfs_value(filename, &tmp) == 0)
265 dev->max_vfs = (uint16_t)tmp;
267 /* for non igb_uio driver, need kernel version >= 3.8 */
268 snprintf(filename, sizeof(filename),
269 "%s/sriov_numvfs", dirname);
270 if (!access(filename, F_OK) &&
271 eal_parse_sysfs_value(filename, &tmp) == 0)
272 dev->max_vfs = (uint16_t)tmp;
276 snprintf(filename, sizeof(filename), "%s/numa_node",
278 if (access(filename, R_OK) != 0) {
279 /* if no NUMA support just set node to -1 */
282 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
286 dev->numa_node = tmp;
289 /* parse resources */
290 snprintf(filename, sizeof(filename), "%s/resource", dirname);
291 if (pci_parse_sysfs_resource(filename, dev) < 0) {
292 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
298 snprintf(filename, sizeof(filename), "%s/driver", dirname);
299 ret = pci_get_kernel_driver_by_path(filename, driver);
301 if (!strcmp(driver, "vfio-pci"))
302 dev->kdrv = RTE_KDRV_VFIO;
303 else if (!strcmp(driver, "igb_uio"))
304 dev->kdrv = RTE_KDRV_IGB_UIO;
305 else if (!strcmp(driver, "uio_pci_generic"))
306 dev->kdrv = RTE_KDRV_UIO_GENERIC;
308 dev->kdrv = RTE_KDRV_UNKNOWN;
309 } else if (ret < 0) {
310 RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
314 dev->kdrv = RTE_KDRV_UNKNOWN;
316 /* device is valid, add in list (sorted) */
317 if (TAILQ_EMPTY(&pci_device_list)) {
318 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
321 struct rte_pci_device *dev2 = NULL;
324 TAILQ_FOREACH(dev2, &pci_device_list, next) {
325 ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
329 TAILQ_INSERT_BEFORE(dev2, dev, next);
331 } else { /* already registered */
332 dev2->kdrv = dev->kdrv;
333 dev2->max_vfs = dev->max_vfs;
334 memmove(dev2->mem_resource,
336 sizeof(dev->mem_resource));
341 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
348 * split up a pci address into its constituent parts.
351 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
352 uint8_t *bus, uint8_t *devid, uint8_t *function)
354 /* first split on ':' */
362 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
365 char *buf_copy = strndup(buf, bufsize);
366 if (buf_copy == NULL)
369 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
372 /* final split is on '.' between devid and function */
373 splitaddr.function = strchr(splitaddr.devid,'.');
374 if (splitaddr.function == NULL)
376 *splitaddr.function++ = '\0';
378 /* now convert to int values */
380 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
381 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
382 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
383 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
387 free(buf_copy); /* free the copy made with strdup */
395 * Scan the content of the PCI bus, and the devices in the devices
399 rte_eal_pci_scan(void)
403 char dirname[PATH_MAX];
405 uint8_t bus, devid, function;
407 dir = opendir(SYSFS_PCI_DEVICES);
409 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
410 __func__, strerror(errno));
414 while ((e = readdir(dir)) != NULL) {
415 if (e->d_name[0] == '.')
418 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
419 &bus, &devid, &function) != 0)
422 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
424 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
435 #ifdef RTE_PCI_CONFIG
437 pci_config_extended_tag(struct rte_pci_device *dev)
439 struct rte_pci_addr *loc = &dev->addr;
440 char filename[PATH_MAX];
444 /* not configured, let it as is */
445 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
446 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
449 snprintf(filename, sizeof(filename),
450 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
451 loc->domain, loc->bus, loc->devid, loc->function);
452 f = fopen(filename, "rw+");
456 fgets(buf, sizeof(buf), f);
457 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
458 /* enable Extended Tag*/
459 if (strncmp(buf, "on", 2) != 0) {
460 fseek(f, 0, SEEK_SET);
464 /* disable Extended Tag */
465 if (strncmp(buf, "off", 3) != 0) {
466 fseek(f, 0, SEEK_SET);
476 pci_config_max_read_request_size(struct rte_pci_device *dev)
478 struct rte_pci_addr *loc = &dev->addr;
479 char filename[PATH_MAX];
480 char buf[BUFSIZ], param[BUFSIZ];
482 /* size can be 128, 256, 512, 1024, 2048, 4096 */
483 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
485 /* not configured, let it as is */
489 snprintf(filename, sizeof(filename),
490 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
491 loc->domain, loc->bus, loc->devid, loc->function);
492 f = fopen(filename, "rw+");
496 fgets(buf, sizeof(buf), f);
497 snprintf(param, sizeof(param), "%d", max_size);
499 /* check if the size to be set is the same as current */
500 if (strcmp(buf, param) == 0) {
504 fseek(f, 0, SEEK_SET);
512 pci_config_space_set(struct rte_pci_device *dev)
514 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
517 /* configure extended tag */
518 pci_config_extended_tag(dev);
520 /* configure max read request size */
521 pci_config_max_read_request_size(dev);
525 /* Init the PCI EAL subsystem */
527 rte_eal_pci_init(void)
529 TAILQ_INIT(&pci_driver_list);
530 TAILQ_INIT(&pci_device_list);
532 /* for debug purposes, PCI can be disabled */
533 if (internal_config.no_pci)
536 if (rte_eal_pci_scan() < 0) {
537 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
543 if (pci_vfio_is_enabled()) {
545 /* if we are primary process, create a thread to communicate with
546 * secondary processes. the thread will use a socket to wait for
547 * requests from secondary process to send open file descriptors,
548 * because VFIO does not allow multiple open descriptors on a group or
551 if (internal_config.process_type == RTE_PROC_PRIMARY &&
552 pci_vfio_mp_sync_setup() < 0)