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 */
141 #define IORESOURCE_MEM 0x00000200
144 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
148 union pci_resource_info {
154 char *ptrs[PCI_RESOURCE_FMT_NVAL];
157 uint64_t phys_addr, end_addr, flags;
159 f = fopen(filename, "r");
161 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
165 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
167 if (fgets(buf, sizeof(buf), f) == NULL) {
169 "%s(): cannot read resource\n", __func__);
173 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
175 "%s(): bad resource format\n", __func__);
179 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
180 end_addr = strtoull(res_info.end_addr, NULL, 16);
181 flags = strtoull(res_info.flags, NULL, 16);
184 "%s(): bad resource format\n", __func__);
188 if (flags & IORESOURCE_MEM) {
189 dev->mem_resource[i].phys_addr = phys_addr;
190 dev->mem_resource[i].len = end_addr - phys_addr + 1;
191 /* not mapped for now */
192 dev->mem_resource[i].addr = NULL;
203 /* Compare two PCI device addresses. */
205 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
207 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
208 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
210 if (dev_addr > dev_addr2)
217 /* Scan one pci sysfs entry, and fill the devices list from it. */
219 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
220 uint8_t devid, uint8_t function)
222 char filename[PATH_MAX];
224 struct rte_pci_device *dev;
226 dev = malloc(sizeof(*dev));
231 memset(dev, 0, sizeof(*dev));
232 dev->addr.domain = domain;
234 dev->addr.devid = devid;
235 dev->addr.function = function;
238 snprintf(filename, sizeof(filename), "%s/vendor", dirname);
239 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
243 dev->id.vendor_id = (uint16_t)tmp;
246 snprintf(filename, sizeof(filename), "%s/device", dirname);
247 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
251 dev->id.device_id = (uint16_t)tmp;
253 /* get subsystem_vendor id */
254 snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
256 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
260 dev->id.subsystem_vendor_id = (uint16_t)tmp;
262 /* get subsystem_device id */
263 snprintf(filename, sizeof(filename), "%s/subsystem_device",
265 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
269 dev->id.subsystem_device_id = (uint16_t)tmp;
273 snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
274 if (!access(filename, F_OK) &&
275 eal_parse_sysfs_value(filename, &tmp) == 0) {
276 dev->max_vfs = (uint16_t)tmp;
280 snprintf(filename, sizeof(filename), "%s/numa_node",
282 if (access(filename, R_OK) != 0) {
283 /* if no NUMA support just set node to -1 */
286 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
290 dev->numa_node = tmp;
293 /* parse resources */
294 snprintf(filename, sizeof(filename), "%s/resource", dirname);
295 if (pci_parse_sysfs_resource(filename, dev) < 0) {
296 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
301 /* device is valid, add in list (sorted) */
302 if (TAILQ_EMPTY(&pci_device_list)) {
303 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
306 struct rte_pci_device *dev2 = NULL;
308 TAILQ_FOREACH(dev2, &pci_device_list, next) {
309 if (pci_addr_comparison(&dev->addr, &dev2->addr))
312 TAILQ_INSERT_BEFORE(dev2, dev, next);
316 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
323 * split up a pci address into its constituent parts.
326 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
327 uint8_t *bus, uint8_t *devid, uint8_t *function)
329 /* first split on ':' */
337 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
340 char *buf_copy = strndup(buf, bufsize);
341 if (buf_copy == NULL)
344 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
347 /* final split is on '.' between devid and function */
348 splitaddr.function = strchr(splitaddr.devid,'.');
349 if (splitaddr.function == NULL)
351 *splitaddr.function++ = '\0';
353 /* now convert to int values */
355 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
356 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
357 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
358 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
362 free(buf_copy); /* free the copy made with strdup */
370 * Scan the content of the PCI bus, and the devices in the devices
378 char dirname[PATH_MAX];
380 uint8_t bus, devid, function;
382 dir = opendir(SYSFS_PCI_DEVICES);
384 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
385 __func__, strerror(errno));
389 while ((e = readdir(dir)) != NULL) {
390 if (e->d_name[0] == '.')
393 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
394 &bus, &devid, &function) != 0)
397 snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
399 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
410 #ifdef RTE_PCI_CONFIG
412 pci_config_extended_tag(struct rte_pci_device *dev)
414 struct rte_pci_addr *loc = &dev->addr;
415 char filename[PATH_MAX];
419 /* not configured, let it as is */
420 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) != 0 &&
421 strncmp(RTE_PCI_EXTENDED_TAG, "off", 3) != 0)
424 snprintf(filename, sizeof(filename),
425 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "extended_tag",
426 loc->domain, loc->bus, loc->devid, loc->function);
427 f = fopen(filename, "rw+");
431 fgets(buf, sizeof(buf), f);
432 if (strncmp(RTE_PCI_EXTENDED_TAG, "on", 2) == 0) {
433 /* enable Extended Tag*/
434 if (strncmp(buf, "on", 2) != 0) {
435 fseek(f, 0, SEEK_SET);
439 /* disable Extended Tag */
440 if (strncmp(buf, "off", 3) != 0) {
441 fseek(f, 0, SEEK_SET);
451 pci_config_max_read_request_size(struct rte_pci_device *dev)
453 struct rte_pci_addr *loc = &dev->addr;
454 char filename[PATH_MAX];
455 char buf[BUFSIZ], param[BUFSIZ];
457 /* size can be 128, 256, 512, 1024, 2048, 4096 */
458 uint32_t max_size = RTE_PCI_MAX_READ_REQUEST_SIZE;
460 /* not configured, let it as is */
464 snprintf(filename, sizeof(filename),
465 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/" "max_read_request_size",
466 loc->domain, loc->bus, loc->devid, loc->function);
467 f = fopen(filename, "rw+");
471 fgets(buf, sizeof(buf), f);
472 snprintf(param, sizeof(param), "%d", max_size);
474 /* check if the size to be set is the same as current */
475 if (strcmp(buf, param) == 0) {
479 fseek(f, 0, SEEK_SET);
487 pci_config_space_set(struct rte_pci_device *dev)
489 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
492 /* configure extended tag */
493 pci_config_extended_tag(dev);
495 /* configure max read request size */
496 pci_config_max_read_request_size(dev);
501 pci_map_device(struct rte_pci_device *dev)
505 /* try mapping the NIC resources using VFIO if it exists */
507 if (pci_vfio_is_enabled()) {
508 ret = pci_vfio_map_resource(dev);
515 /* map resources for devices that use igb_uio */
517 ret = pci_uio_map_resource(dev);
525 * If vendor/device ID match, call the devinit() function of the
529 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
532 struct rte_pci_id *id_table;
534 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
536 /* check if device's identifiers match the driver's ones */
537 if (id_table->vendor_id != dev->id.vendor_id &&
538 id_table->vendor_id != PCI_ANY_ID)
540 if (id_table->device_id != dev->id.device_id &&
541 id_table->device_id != PCI_ANY_ID)
543 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
544 id_table->subsystem_vendor_id != PCI_ANY_ID)
546 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
547 id_table->subsystem_device_id != PCI_ANY_ID)
550 struct rte_pci_addr *loc = &dev->addr;
552 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
553 loc->domain, loc->bus, loc->devid, loc->function,
556 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
557 dev->id.device_id, dr->name);
559 /* no initialization when blacklisted, return without error */
560 if (dev->devargs != NULL &&
561 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
562 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
566 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
567 #ifdef RTE_PCI_CONFIG
569 * Set PCIe config space for high performance.
570 * Return value can be ignored.
572 pci_config_space_set(dev);
574 /* map resources for devices that use igb_uio */
575 ret = pci_map_device(dev);
578 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
579 rte_eal_process_type() == RTE_PROC_PRIMARY) {
580 /* unbind current driver */
581 if (pci_unbind_kernel_driver(dev) < 0)
585 /* reference driver structure */
588 /* call the driver devinit() function */
589 return dr->devinit(dr, dev);
591 /* return positive value if driver is not found */
595 /* Init the PCI EAL subsystem */
597 rte_eal_pci_init(void)
599 TAILQ_INIT(&pci_driver_list);
600 TAILQ_INIT(&pci_device_list);
601 pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
602 mapped_pci_res_list);
604 /* for debug purposes, PCI can be disabled */
605 if (internal_config.no_pci)
608 if (pci_scan() < 0) {
609 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
615 if (pci_vfio_is_enabled()) {
617 /* if we are primary process, create a thread to communicate with
618 * secondary processes. the thread will use a socket to wait for
619 * requests from secondary process to send open file descriptors,
620 * because VFIO does not allow multiple open descriptors on a group or
623 if (internal_config.process_type == RTE_PROC_PRIMARY &&
624 pci_vfio_mp_sync_setup() < 0)