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 rte_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 = rte_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(): rte_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__,
100 /* map a particular resource from a file */
102 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size)
106 /* Map the PCI memory resource of device */
107 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
108 MAP_SHARED, fd, offset);
109 if (mapaddr == MAP_FAILED ||
110 (requested_addr != NULL && mapaddr != requested_addr)) {
111 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
112 __func__, fd, requested_addr,
113 (unsigned long)size, (unsigned long)offset,
114 strerror(errno), mapaddr);
118 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
126 /* parse the "resource" sysfs file */
127 #define IORESOURCE_MEM 0x00000200
130 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
134 union pci_resource_info {
140 char *ptrs[PCI_RESOURCE_FMT_NVAL];
143 uint64_t phys_addr, end_addr, flags;
145 f = fopen(filename, "r");
147 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
151 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
153 if (fgets(buf, sizeof(buf), f) == NULL) {
155 "%s(): cannot read resource\n", __func__);
159 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
161 "%s(): bad resource format\n", __func__);
165 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
166 end_addr = strtoull(res_info.end_addr, NULL, 16);
167 flags = strtoull(res_info.flags, NULL, 16);
170 "%s(): bad resource format\n", __func__);
174 if (flags & IORESOURCE_MEM) {
175 dev->mem_resource[i].phys_addr = phys_addr;
176 dev->mem_resource[i].len = end_addr - phys_addr + 1;
177 /* not mapped for now */
178 dev->mem_resource[i].addr = NULL;
189 /* Compare two PCI device addresses. */
191 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
193 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
194 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
196 if (dev_addr > dev_addr2)
203 /* Scan one pci sysfs entry, and fill the devices list from it. */
205 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
206 uint8_t devid, uint8_t function)
208 char filename[PATH_MAX];
210 struct rte_pci_device *dev;
212 dev = malloc(sizeof(*dev));
217 memset(dev, 0, sizeof(*dev));
218 dev->addr.domain = domain;
220 dev->addr.devid = devid;
221 dev->addr.function = function;
224 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
225 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
229 dev->id.vendor_id = (uint16_t)tmp;
232 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
233 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
237 dev->id.device_id = (uint16_t)tmp;
239 /* get subsystem_vendor id */
240 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
242 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
246 dev->id.subsystem_vendor_id = (uint16_t)tmp;
248 /* get subsystem_device id */
249 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
251 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
255 dev->id.subsystem_device_id = (uint16_t)tmp;
259 rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
260 if (!access(filename, F_OK) &&
261 eal_parse_sysfs_value(filename, &tmp) == 0) {
262 dev->max_vfs = (uint16_t)tmp;
266 rte_snprintf(filename, sizeof(filename), "%s/numa_node",
268 if (access(filename, R_OK) != 0) {
269 /* if no NUMA support just set node to 0 */
272 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
276 dev->numa_node = tmp;
279 /* parse resources */
280 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
281 if (pci_parse_sysfs_resource(filename, dev) < 0) {
282 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
287 /* device is valid, add in list (sorted) */
288 if (TAILQ_EMPTY(&pci_device_list)) {
289 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
292 struct rte_pci_device *dev2 = NULL;
294 TAILQ_FOREACH(dev2, &pci_device_list, next) {
295 if (pci_addr_comparison(&dev->addr, &dev2->addr))
298 TAILQ_INSERT_BEFORE(dev2, dev, next);
302 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
309 * split up a pci address into its constituent parts.
312 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
313 uint8_t *bus, uint8_t *devid, uint8_t *function)
315 /* first split on ':' */
323 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
326 char *buf_copy = strndup(buf, bufsize);
327 if (buf_copy == NULL)
330 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
333 /* final split is on '.' between devid and function */
334 splitaddr.function = strchr(splitaddr.devid,'.');
335 if (splitaddr.function == NULL)
337 *splitaddr.function++ = '\0';
339 /* now convert to int values */
341 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
342 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
343 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
344 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
348 free(buf_copy); /* free the copy made with strdup */
356 * Scan the content of the PCI bus, and the devices in the devices
364 char dirname[PATH_MAX];
366 uint8_t bus, devid, function;
368 dir = opendir(SYSFS_PCI_DEVICES);
370 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
371 __func__, strerror(errno));
375 while ((e = readdir(dir)) != NULL) {
376 if (e->d_name[0] == '.')
379 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
380 &bus, &devid, &function) != 0)
383 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
385 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
397 pci_map_device(struct rte_pci_device *dev)
401 /* try mapping the NIC resources using VFIO if it exists */
403 if (pci_vfio_is_enabled()) {
404 ret = pci_vfio_map_resource(dev);
411 /* map resources for devices that use igb_uio */
413 ret = pci_uio_map_resource(dev);
421 * If vendor/device ID match, call the devinit() function of the
425 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
428 struct rte_pci_id *id_table;
430 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
432 /* check if device's identifiers match the driver's ones */
433 if (id_table->vendor_id != dev->id.vendor_id &&
434 id_table->vendor_id != PCI_ANY_ID)
436 if (id_table->device_id != dev->id.device_id &&
437 id_table->device_id != PCI_ANY_ID)
439 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
440 id_table->subsystem_vendor_id != PCI_ANY_ID)
442 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
443 id_table->subsystem_device_id != PCI_ANY_ID)
446 struct rte_pci_addr *loc = &dev->addr;
448 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
449 loc->domain, loc->bus, loc->devid, loc->function,
452 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
453 dev->id.device_id, dr->name);
455 /* no initialization when blacklisted, return without error */
456 if (dev->devargs != NULL &&
457 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
458 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
462 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
463 /* map resources for devices that use igb_uio */
464 ret = pci_map_device(dev);
467 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
468 rte_eal_process_type() == RTE_PROC_PRIMARY) {
469 /* unbind current driver */
470 if (pci_unbind_kernel_driver(dev) < 0)
474 /* reference driver structure */
477 /* call the driver devinit() function */
478 return dr->devinit(dr, dev);
480 /* return positive value if driver is not found */
484 /* Init the PCI EAL subsystem */
486 rte_eal_pci_init(void)
488 TAILQ_INIT(&pci_driver_list);
489 TAILQ_INIT(&pci_device_list);
490 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
491 mapped_pci_res_list);
493 /* for debug purposes, PCI can be disabled */
494 if (internal_config.no_pci)
497 if (pci_scan() < 0) {
498 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
504 if (pci_vfio_is_enabled()) {
506 /* if we are primary process, create a thread to communicate with
507 * secondary processes. the thread will use a socket to wait for
508 * requests from secondary process to send open file descriptors,
509 * because VFIO does not allow multiple open descriptors on a group or
512 if (internal_config.process_type == RTE_PROC_PRIMARY &&
513 pci_vfio_mp_sync_setup() < 0)