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_per_lcore.h>
63 #include <rte_lcore.h>
64 #include <rte_malloc.h>
65 #include <rte_string_fns.h>
66 #include <rte_debug.h>
68 #include "eal_internal_cfg.h"
69 #include "eal_private.h"
73 * PCI probing under linux
75 * This code is used to simulate a PCI probe by parsing information in
76 * sysfs. Moreover, when a registered driver matches a device, the
77 * kernel driver currently using it is unloaded and replaced by
78 * igb_uio module, which is a very minimal userland driver for Intel
79 * network card, only providing access to PCI BAR to applications, and
80 * enabling bus master.
84 #define PROC_MODULES "/proc/modules"
86 #define IGB_UIO_NAME "igb_uio"
88 #define UIO_NEWID "/sys/bus/pci/drivers/%s/new_id"
89 #define UIO_BIND "/sys/bus/pci/drivers/%s/bind"
91 /* maximum time to wait that /dev/uioX appears */
92 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
95 * For multi-process we need to reproduce all PCI mappings in secondary
96 * processes, so save them in a tailq.
99 TAILQ_ENTRY(uio_resource) next;
101 struct rte_pci_addr pci_addr;
105 unsigned long offset;
108 TAILQ_HEAD(uio_res_list, uio_resource);
110 static struct uio_res_list *uio_res_list = NULL;
111 static int pci_parse_sysfs_value(const char *filename, unsigned long *val);
114 * Check that a kernel module is loaded. Returns 0 on success, or if the
115 * parameter is NULL, or -1 if the module is not loaded.
118 pci_uio_check_module(const char *module_name)
124 if (module_name == NULL)
127 f = fopen(PROC_MODULES, "r");
129 RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES"\n");
133 while(fgets(buf, sizeof(buf), f) != NULL) {
135 for (i = 0; i < sizeof(buf) && buf[i] != '\0'; i++) {
140 if (strncmp(buf, module_name, sizeof(buf)) == 0) {
146 RTE_LOG(ERR, EAL, "Cannot find %s in "PROC_MODULES"\n", module_name);
150 /* bind a PCI to the kernel module driver */
152 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
157 char uio_newid[PATH_MAX];
158 char uio_bind[PATH_MAX];
159 struct rte_pci_addr *loc = &dev->addr;
161 RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT" to %s driver\n",
162 loc->domain, loc->bus, loc->devid, loc->function, module_name);
164 n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_NEWID, module_name);
165 if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
166 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
169 n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_BIND, module_name);
170 if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
171 RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
175 n = rte_snprintf(buf, sizeof(buf), "%x %x\n",
176 dev->id.vendor_id, dev->id.device_id);
177 if ((n < 0) || (n >= (int)sizeof(buf))) {
178 RTE_LOG(ERR, EAL, "Cannot rte_snprintf vendor_id/device_id\n");
182 f = fopen(uio_newid, "w");
184 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_newid);
187 if (fwrite(buf, n, 1, f) == 0) {
193 f = fopen(uio_bind, "w");
195 RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_bind);
198 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
199 loc->domain, loc->bus, loc->devid, loc->function);
200 if ((n < 0) || (n >= (int)sizeof(buf))) {
201 RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
205 if (fwrite(buf, n, 1, f) == 0) {
210 RTE_LOG(DEBUG, EAL, "Device bound\n");
216 /* map a particular resource from a file */
218 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, const char *devname,
219 unsigned long offset, unsigned long size)
226 * open devname, and mmap it: it can take some time to
227 * appear, so we wait some time before returning an error
229 for (n=0; n<UIO_DEV_WAIT_TIMEOUT*10; n++) {
230 fd = open(devname, O_RDWR);
238 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", devname, strerror(errno));
242 /* Map the PCI memory resource of device */
243 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
244 MAP_SHARED, fd, offset);
245 if (mapaddr == MAP_FAILED ||
246 (requested_addr != NULL && mapaddr != requested_addr)) {
247 RTE_LOG(ERR, EAL, "%s(): cannot mmap %s: %s\n", __func__,
248 devname, strerror(errno));
252 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
253 /* save fd if in primary process */
254 dev->intr_handle.fd = fd;
255 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
257 /* fd is not needed in slave process, close it */
258 dev->intr_handle.fd = -1;
259 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
263 RTE_LOG(DEBUG, EAL, "PCI memory mapped at %p\n", mapaddr);
268 dev->intr_handle.fd = -1;
269 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
273 /* map the PCI resource of a PCI device in virtual memory */
275 pci_uio_map_resource(struct rte_pci_device *dev)
279 char dirname[PATH_MAX];
280 char dirname2[PATH_MAX];
281 char filename[PATH_MAX];
282 char devname[PATH_MAX]; /* contains the /dev/uioX */
285 unsigned long size, offset;
286 struct rte_pci_addr *loc = &dev->addr;
287 struct uio_resource *uio_res;
289 RTE_LOG(DEBUG, EAL, "map PCI resource for device "PCI_PRI_FMT"\n",
290 loc->domain, loc->bus, loc->devid, loc->function);
292 /* secondary processes - use already recorded details */
293 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
295 TAILQ_FOREACH(uio_res, uio_res_list, next) {
296 /* skip this element if it doesn't match our PCI address */
297 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
300 if (pci_map_resource(dev, uio_res->addr, uio_res->path, \
301 uio_res->offset, uio_res->size) == uio_res->addr)
304 RTE_LOG(ERR, EAL, "Cannot mmap device resource\n");
308 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
312 /* depending on kernel version, uio can be located in uio/uioX
315 rte_snprintf(dirname, sizeof(dirname),
316 "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
317 loc->domain, loc->bus, loc->devid, loc->function);
319 dir = opendir(dirname);
321 /* retry with the parent directory */
322 rte_snprintf(dirname, sizeof(dirname),
323 "/sys/bus/pci/devices/" PCI_PRI_FMT,
324 loc->domain, loc->bus, loc->devid, loc->function);
325 dir = opendir(dirname);
328 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
333 /* take the first file starting with "uio" */
334 while ((e = readdir(dir)) != NULL) {
335 int shortprefix_len = sizeof("uio") - 1; /* format could be uio%d ...*/
336 int longprefix_len = sizeof("uio:uio") - 1; /* ... or uio:uio%d */
339 if (strncmp(e->d_name, "uio", 3) != 0)
342 /* first try uio%d */
344 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
345 if (errno == 0 && endptr != e->d_name) {
346 rte_snprintf(dirname2, sizeof(dirname2),
347 "%s/uio%u", dirname, uio_num);
351 /* then try uio:uio%d */
353 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
354 if (errno == 0 && endptr != e->d_name) {
355 rte_snprintf(dirname2, sizeof(dirname2),
356 "%s/uio:uio%u", dirname, uio_num);
362 /* No uio resource found */
366 /* get mapping offset */
367 rte_snprintf(filename, sizeof(filename),
368 "%s/maps/map0/offset", dirname2);
369 if (pci_parse_sysfs_value(filename, &offset) < 0) {
370 RTE_LOG(ERR, EAL, "%s(): cannot parse offset\n",
375 /* get mapping size */
376 rte_snprintf(filename, sizeof(filename),
377 "%s/maps/map0/size", dirname2);
378 if (pci_parse_sysfs_value(filename, &size) < 0) {
379 RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
384 /* open and mmap /dev/uioX */
385 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
386 mapaddr = pci_map_resource(dev, NULL, devname, offset, size);
389 dev->mem_resource.addr = mapaddr;
391 /* save the mapping details for secondary processes*/
392 uio_res = rte_malloc("UIO_RES", sizeof(*uio_res), 0);
393 if (uio_res == NULL){
394 RTE_LOG(ERR, EAL, "%s(): cannot store uio mmap details\n", __func__);
397 uio_res->addr = mapaddr;
398 uio_res->offset = offset;
399 uio_res->size = size;
400 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
401 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
403 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
408 /* parse the "resource" sysfs file */
409 #define IORESOURCE_MEM 0x00000200
412 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
416 union pci_resource_info {
422 char *ptrs[PCI_RESOURCE_FMT_NVAL];
425 uint64_t phys_addr, end_addr, flags;
427 f = fopen(filename, "r");
429 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
433 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
435 if (fgets(buf, sizeof(buf), f) == NULL) {
436 RTE_LOG(ERR, EAL, "%s(): cannot read resource\n", __func__);
440 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
441 RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
445 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
446 end_addr = strtoull(res_info.end_addr, NULL, 16);
447 flags = strtoull(res_info.flags, NULL, 16);
449 RTE_LOG(ERR, EAL, "%s(): bad resource format\n", __func__);
453 if (flags & IORESOURCE_MEM) {
454 dev->mem_resource.phys_addr = phys_addr;
455 dev->mem_resource.len = end_addr - phys_addr + 1;
456 dev->mem_resource.addr = NULL; /* not mapped for now */
468 /* parse a sysfs file containing one integer value */
470 pci_parse_sysfs_value(const char *filename, unsigned long *val)
476 f = fopen(filename, "r");
478 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
483 if (fgets(buf, sizeof(buf), f) == NULL) {
484 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
489 *val = strtoul(buf, &end, 0);
490 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
491 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
500 /* Scan one pci sysfs entry, and fill the devices list from it. */
502 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
503 uint8_t devid, uint8_t function)
505 char filename[PATH_MAX];
507 struct rte_pci_device *dev;
509 dev = malloc(sizeof(*dev));
514 memset(dev, 0, sizeof(*dev));
515 dev->addr.domain = domain;
517 dev->addr.devid = devid;
518 dev->addr.function = function;
521 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
522 if (pci_parse_sysfs_value(filename, &tmp) < 0) {
526 dev->id.vendor_id = (uint16_t)tmp;
529 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
530 if (pci_parse_sysfs_value(filename, &tmp) < 0) {
534 dev->id.device_id = (uint16_t)tmp;
536 /* get subsystem_vendor id */
537 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
539 if (pci_parse_sysfs_value(filename, &tmp) < 0) {
543 dev->id.subsystem_vendor_id = (uint16_t)tmp;
545 /* get subsystem_device id */
546 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
548 if (pci_parse_sysfs_value(filename, &tmp) < 0) {
552 dev->id.subsystem_device_id = (uint16_t)tmp;
554 /* parse resources */
555 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
556 if (pci_parse_sysfs_resource(filename, dev) < 0) {
557 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
562 /* device is valid, add in list */
563 TAILQ_INSERT_TAIL(&device_list, dev, next);
569 * split up a pci address into its constituent parts.
572 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
573 uint8_t *bus, uint8_t *devid, uint8_t *function)
575 /* first split on ':' */
583 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
586 char *buf_copy = strndup(buf, bufsize);
587 if (buf_copy == NULL)
590 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
593 /* final split is on '.' between devid and function */
594 splitaddr.function = strchr(splitaddr.devid,'.');
595 if (splitaddr.function == NULL)
597 *splitaddr.function++ = '\0';
599 /* now convert to int values */
601 *domain = (uint8_t)strtoul(splitaddr.domain, NULL, 16);
602 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
603 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
604 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
608 free(buf_copy); /* free the copy made with strdup */
616 * Scan the content of the PCI bus, and the devices in the devices
624 char dirname[PATH_MAX];
626 uint8_t bus, devid, function;
628 dir = opendir(SYSFS_PCI_DEVICES);
630 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
631 __func__, strerror(errno));
635 while ((e = readdir(dir)) != NULL) {
636 if (e->d_name[0] == '.')
639 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
640 &bus, &devid, &function) != 0)
643 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
645 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
656 /* unbind kernel driver for this device */
658 pci_unbind_kernel_driver(struct rte_pci_device *dev)
662 char filename[PATH_MAX];
664 struct rte_pci_addr *loc = &dev->addr;
666 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
667 rte_snprintf(filename, sizeof(filename),
668 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
669 loc->domain, loc->bus, loc->devid, loc->function);
671 RTE_LOG(DEBUG, EAL, "unbind kernel driver %s\n", filename);
673 f = fopen(filename, "w");
674 if (f == NULL) /* device was not bound */
677 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
678 loc->domain, loc->bus, loc->devid, loc->function);
679 if ((n < 0) || (n >= (int)sizeof(buf))) {
680 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
683 if (fwrite(buf, n, 1, f) == 0)
695 * If vendor/device ID match, call the devinit() function of the
699 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
701 struct rte_pci_id *id_table;
702 const char *module_name = NULL;
705 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
706 module_name = IGB_UIO_NAME;
708 ret = pci_uio_check_module(module_name);
710 rte_exit(1, "The %s module is required by the %s driver\n",
711 module_name, dr->name);
713 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
715 /* check if device's identifiers match the driver's ones */
716 if (id_table->vendor_id != dev->id.vendor_id &&
717 id_table->vendor_id != PCI_ANY_ID)
719 if (id_table->device_id != dev->id.device_id &&
720 id_table->device_id != PCI_ANY_ID)
722 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
723 id_table->subsystem_vendor_id != PCI_ANY_ID)
725 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
726 id_table->subsystem_device_id != PCI_ANY_ID)
729 RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n",
730 dev->id.vendor_id, dev->id.device_id, dr->name);
732 /* Unbind PCI devices if needed */
733 if (module_name != NULL) {
734 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
735 /* unbind current driver, bind ours */
736 if (pci_unbind_kernel_driver(dev) < 0)
738 if (pci_uio_bind_device(dev, module_name) < 0)
741 /* map the NIC resources */
742 if (pci_uio_map_resource(dev) < 0)
745 /* call the driver devinit() function */
746 return dr->devinit(dr, dev);
752 /* Init the PCI EAL subsystem */
754 rte_eal_pci_init(void)
756 TAILQ_INIT(&driver_list);
757 TAILQ_INIT(&device_list);
758 uio_res_list = RTE_TAILQ_RESERVE("PCI_RESOURCE_LIST", uio_res_list);
760 /* for debug purposes, PCI can be disabled */
761 if (internal_config.no_pci)
764 if (pci_scan() < 0) {
765 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);