4 * Copyright(c) 2010-2012 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.
42 #include <sys/types.h>
49 #include <sys/queue.h>
51 #include <sys/ioctl.h>
53 #include <rte_interrupts.h>
56 #include <rte_common.h>
57 #include <rte_launch.h>
58 #include <rte_memory.h>
59 #include <rte_memzone.h>
60 #include <rte_tailq.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_debug.h>
69 #include "eal_internal_cfg.h"
70 #include "eal_filesystem.h"
71 #include "eal_private.h"
75 * PCI probing under linux
77 * This code is used to simulate a PCI probe by parsing information in
78 * sysfs. Moreover, when a registered driver matches a device, the
79 * kernel driver currently using it is unloaded and replaced by
80 * igb_uio module, which is a very minimal userland driver for Intel
81 * network card, only providing access to PCI BAR to applications, and
82 * enabling bus master.
86 #define PROC_MODULES "/proc/modules"
88 #define IGB_UIO_NAME "igb_uio"
90 #define UIO_NEWID "/sys/bus/pci/drivers/%s/new_id"
91 #define UIO_BIND "/sys/bus/pci/drivers/%s/bind"
93 /* maximum time to wait that /dev/uioX appears */
94 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
97 * For multi-process we need to reproduce all PCI mappings in secondary
98 * processes, so save them in a tailq.
100 struct uio_resource {
101 TAILQ_ENTRY(uio_resource) next;
103 struct rte_pci_addr pci_addr;
107 unsigned long offset;
110 TAILQ_HEAD(uio_res_list, uio_resource);
112 static struct uio_res_list *uio_res_list = NULL;
115 * Check that a kernel module is loaded. Returns 0 on success, or if the
116 * parameter is NULL, or -1 if the module is not loaded.
119 pci_uio_check_module(const char *module_name)
125 if (module_name == NULL)
128 f = fopen(PROC_MODULES, "r");
130 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES"\n");
134 while(fgets(buf, sizeof(buf), f) != NULL) {
136 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
141 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
147 RTE_LOG(ERR, EAL, "Cannot find %s in "PROC_MODULES"\n", module_name);
151 /* bind a PCI to the kernel module driver */
153 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
158 char uio_newid[PATH_MAX];
159 char uio_bind[PATH_MAX];
160 struct rte_pci_addr *loc = &dev->addr;
162 RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT" to %s driver\n",
163 loc->domain, loc->bus, loc->devid, loc->function, module_name);
165 n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_NEWID, module_name);
166 if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
167 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
170 n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_BIND, module_name);
171 if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
172 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
176 n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
177 dev->id.vendor_id, dev->id.device_id);
178 if ((n < 0) || (n >= (int)sizeof(buf))) {
179 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
183 f = fopen(uio_newid, "w");
185 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
188 if (fwrite(buf, n, 1, f) == 0) {
194 f = fopen(uio_bind, "w");
196 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_bind);
199 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
200 loc->domain, loc->bus, loc->devid, loc->function);
201 if ((n < 0) || (n >= (int)sizeof(buf))) {
202 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
206 if (fwrite(buf, n, 1, f) == 0) {
211 RTE_LOG(DEBUG, EAL, "Device bound\n");
217 /* map a particular resource from a file */
219 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, const char *devname,
220 unsigned long offset, unsigned long size)
227 * open devname, and mmap it: it can take some time to
228 * appear, so we wait some time before returning an error
230 for (n=0; n<UIO_DEV_WAIT_TIMEOUT*10; n++) {
231 fd = open(devname, O_RDWR);
239 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", devname, strerror(errno));
243 /* Map the PCI memory resource of device */
244 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
245 MAP_SHARED, fd, offset);
246 if (mapaddr == MAP_FAILED ||
247 (requested_addr != NULL && mapaddr != requested_addr)) {
248 RTE_LOG(ERR, EAL, "%s(): cannot mmap %s: %s\n", __func__,
249 devname, strerror(errno));
253 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
254 /* save fd if in primary process */
255 dev->intr_handle.fd = fd;
256 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
258 /* fd is not needed in slave process, close it */
259 dev->intr_handle.fd = -1;
260 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
264 RTE_LOG(DEBUG, EAL, "PCI memory mapped at %p\n", mapaddr);
269 dev->intr_handle.fd = -1;
270 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
274 /* map the PCI resource of a PCI device in virtual memory */
276 pci_uio_map_resource(struct rte_pci_device *dev)
280 char dirname[PATH_MAX];
281 char dirname2[PATH_MAX];
282 char filename[PATH_MAX];
283 char devname[PATH_MAX]; /* contains the /dev/uioX */
286 unsigned long size, offset;
287 struct rte_pci_addr *loc = &dev->addr;
288 struct uio_resource *uio_res;
290 RTE_LOG(DEBUG, EAL, "map PCI resource for device "PCI_PRI_FMT"\n",
291 loc->domain, loc->bus, loc->devid, loc->function);
293 /* secondary processes - use already recorded details */
294 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
296 TAILQ_FOREACH(uio_res, uio_res_list, next) {
297 /* skip this element if it doesn't match our PCI address */
298 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
301 if (pci_map_resource(dev, uio_res->addr, uio_res->path, \
302 uio_res->offset, uio_res->size) == uio_res->addr)
305 RTE_LOG(ERR, EAL, "Cannot mmap device resource\n");
309 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
313 /* depending on kernel version, uio can be located in uio/uioX
316 rte_snprintf(dirname, sizeof(dirname),
317 "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
318 loc->domain, loc->bus, loc->devid, loc->function);
320 dir = opendir(dirname);
322 /* retry with the parent directory */
323 rte_snprintf(dirname, sizeof(dirname),
324 "/sys/bus/pci/devices/" PCI_PRI_FMT,
325 loc->domain, loc->bus, loc->devid, loc->function);
326 dir = opendir(dirname);
329 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
334 /* take the first file starting with "uio" */
335 while ((e = readdir(dir)) != NULL) {
336 int shortprefix_len = sizeof("uio") - 1; /* format could be uio%d ...*/
337 int longprefix_len = sizeof("uio:uio") - 1; /* ... or uio:uio%d */
340 if (strncmp(e->d_name, "uio", 3) != 0)
343 /* first try uio%d */
345 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
346 if (errno == 0 && endptr != e->d_name) {
347 rte_snprintf(dirname2, sizeof(dirname2),
348 "%s/uio%u", dirname, uio_num);
352 /* then try uio:uio%d */
354 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
355 if (errno == 0 && endptr != e->d_name) {
356 rte_snprintf(dirname2, sizeof(dirname2),
357 "%s/uio:uio%u", dirname, uio_num);
363 /* No uio resource found */
367 /* get mapping offset */
368 rte_snprintf(filename, sizeof(filename),
369 "%s/maps/map0/offset", dirname2);
370 if (eal_parse_sysfs_value(filename, &offset) < 0) {
371 RTE_LOG(ERR, EAL, "%s(): cannot parse offset\n",
376 /* get mapping size */
377 rte_snprintf(filename, sizeof(filename),
378 "%s/maps/map0/size", dirname2);
379 if (eal_parse_sysfs_value(filename, &size) < 0) {
380 RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
385 /* open and mmap /dev/uioX */
386 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
387 mapaddr = pci_map_resource(dev, NULL, devname, offset, size);
390 dev->mem_resource.addr = mapaddr;
392 /* save the mapping details for secondary processes*/
393 uio_res = rte_malloc("UIO_RES", sizeof(*uio_res), 0);
394 if (uio_res == NULL){
395 RTE_LOG(ERR, EAL, "%s(): cannot store uio mmap details\n", __func__);
398 uio_res->addr = mapaddr;
399 uio_res->offset = offset;
400 uio_res->size = size;
401 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
402 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
404 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
409 /* parse the "resource" sysfs file */
410 #define IORESOURCE_MEM 0x00000200
413 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
417 union pci_resource_info {
423 char *ptrs[PCI_RESOURCE_FMT_NVAL];
426 uint64_t phys_addr, end_addr, flags;
428 f = fopen(filename, "r");
430 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
434 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
436 if (fgets(buf, sizeof(buf), f) == NULL) {
437 RTE_LOG(ERR, EAL, "%s(): cannot read resource\n", __func__);
441 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
442 RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
446 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
447 end_addr = strtoull(res_info.end_addr, NULL, 16);
448 flags = strtoull(res_info.flags, NULL, 16);
450 RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
454 if (flags & IORESOURCE_MEM) {
455 dev->mem_resource.phys_addr = phys_addr;
456 dev->mem_resource.len = end_addr - phys_addr + 1;
457 dev->mem_resource.addr = NULL; /* not mapped for now */
470 /* Scan one pci sysfs entry, and fill the devices list from it. */
472 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
473 uint8_t devid, uint8_t function)
475 char filename[PATH_MAX];
477 struct rte_pci_device *dev;
479 dev = malloc(sizeof(*dev));
484 memset(dev, 0, sizeof(*dev));
485 dev->addr.domain = domain;
487 dev->addr.devid = devid;
488 dev->addr.function = function;
491 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
492 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
496 dev->id.vendor_id = (uint16_t)tmp;
499 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
500 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
504 dev->id.device_id = (uint16_t)tmp;
506 /* get subsystem_vendor id */
507 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
509 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
513 dev->id.subsystem_vendor_id = (uint16_t)tmp;
515 /* get subsystem_device id */
516 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
518 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
522 dev->id.subsystem_device_id = (uint16_t)tmp;
524 /* parse resources */
525 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
526 if (pci_parse_sysfs_resource(filename, dev) < 0) {
527 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
532 /* device is valid, add in list */
533 TAILQ_INSERT_TAIL(&device_list, dev, next);
539 * split up a pci address into its constituent parts.
542 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
543 uint8_t *bus, uint8_t *devid, uint8_t *function)
545 /* first split on ':' */
553 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
556 char *buf_copy = strndup(buf, bufsize);
557 if (buf_copy == NULL)
560 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
563 /* final split is on '.' between devid and function */
564 splitaddr.function = strchr(splitaddr.devid,'.');
565 if (splitaddr.function == NULL)
567 *splitaddr.function++ = '\0';
569 /* now convert to int values */
571 *domain = (uint8_t)strtoul(splitaddr.domain, NULL, 16);
572 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
573 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
574 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
578 free(buf_copy); /* free the copy made with strdup */
586 * Scan the content of the PCI bus, and the devices in the devices
594 char dirname[PATH_MAX];
596 uint8_t bus, devid, function;
598 dir = opendir(SYSFS_PCI_DEVICES);
600 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
601 __func__, strerror(errno));
605 while ((e = readdir(dir)) != NULL) {
606 if (e->d_name[0] == '.')
609 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
610 &bus, &devid, &function) != 0)
613 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
615 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
626 /* unbind kernel driver for this device */
628 pci_unbind_kernel_driver(struct rte_pci_device *dev)
632 char filename[PATH_MAX];
634 struct rte_pci_addr *loc = &dev->addr;
636 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
637 rte_snprintf(filename, sizeof(filename),
638 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
639 loc->domain, loc->bus, loc->devid, loc->function);
641 RTE_LOG(DEBUG, EAL, "unbind kernel driver %s\n", filename);
643 f = fopen(filename, "w");
644 if (f == NULL) /* device was not bound */
647 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
648 loc->domain, loc->bus, loc->devid, loc->function);
649 if ((n < 0) || (n >= (int)sizeof(buf))) {
650 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
653 if (fwrite(buf, n, 1, f) == 0)
665 * If vendor/device ID match, call the devinit() function of the
669 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
671 struct rte_pci_id *id_table;
672 const char *module_name = NULL;
675 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
676 module_name = IGB_UIO_NAME;
678 ret = pci_uio_check_module(module_name);
680 rte_exit(1, "The %s module is required by the %s driver\n",
681 module_name, dr->name);
683 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
685 /* check if device's identifiers match the driver's ones */
686 if (id_table->vendor_id != dev->id.vendor_id &&
687 id_table->vendor_id != PCI_ANY_ID)
689 if (id_table->device_id != dev->id.device_id &&
690 id_table->device_id != PCI_ANY_ID)
692 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
693 id_table->subsystem_vendor_id != PCI_ANY_ID)
695 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
696 id_table->subsystem_device_id != PCI_ANY_ID)
699 RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n",
700 dev->id.vendor_id, dev->id.device_id, dr->name);
702 /* Unbind PCI devices if needed */
703 if ((!dev->blacklisted) &&
704 (module_name != NULL)) {
705 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
706 /* unbind current driver, bind ours */
707 if (pci_unbind_kernel_driver(dev) < 0)
709 if (pci_uio_bind_device(dev, module_name) < 0)
712 /* map the NIC resources */
713 if (pci_uio_map_resource(dev) < 0)
717 /* reference driver structure */
720 /* no initialization when blacklisted */
721 if (dev->blacklisted)
724 /* call the driver devinit() function */
725 return dr->devinit(dr, dev);
731 /* Init the PCI EAL subsystem */
733 rte_eal_pci_init(void)
735 TAILQ_INIT(&driver_list);
736 TAILQ_INIT(&device_list);
737 uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
739 /* for debug purposes, PCI can be disabled */
740 if (internal_config.no_pci)
743 if (pci_scan() < 0) {
744 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);