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.
41 #include <sys/types.h>
48 #include <sys/queue.h>
50 #include <sys/ioctl.h>
52 #include <rte_interrupts.h>
55 #include <rte_common.h>
56 #include <rte_launch.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_tailq.h>
61 #include <rte_eal_memconfig.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>
67 #include <rte_devargs.h>
69 #include "rte_pci_dev_ids.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.
93 * For multi-process we need to reproduce all PCI mappings in secondary
94 * processes, so save them in a tailq.
97 TAILQ_ENTRY(uio_resource) next;
99 struct rte_pci_addr pci_addr;
102 struct uio_map maps[PCI_MAX_RESOURCE];
105 TAILQ_HEAD(uio_res_list, uio_resource);
107 static struct uio_res_list *uio_res_list = NULL;
108 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
110 /* unbind kernel driver for this device */
112 pci_unbind_kernel_driver(struct rte_pci_device *dev)
116 char filename[PATH_MAX];
118 struct rte_pci_addr *loc = &dev->addr;
120 /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
121 rte_snprintf(filename, sizeof(filename),
122 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
123 loc->domain, loc->bus, loc->devid, loc->function);
125 f = fopen(filename, "w");
126 if (f == NULL) /* device was not bound */
129 n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
130 loc->domain, loc->bus, loc->devid, loc->function);
131 if ((n < 0) || (n >= (int)sizeof(buf))) {
132 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
135 if (fwrite(buf, n, 1, f) == 0) {
136 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
149 /* map a particular resource from a file */
151 pci_map_resource(void *requested_addr, const char *devname, off_t offset,
158 * open devname, to mmap it
160 fd = open(devname, O_RDWR);
162 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
163 devname, strerror(errno));
167 /* Map the PCI memory resource of device */
168 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
169 MAP_SHARED, fd, offset);
171 if (mapaddr == MAP_FAILED ||
172 (requested_addr != NULL && mapaddr != requested_addr)) {
173 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%s(%d), %p, 0x%lx, 0x%lx):"
174 " %s (%p)\n", __func__, devname, fd, requested_addr,
175 (unsigned long)size, (unsigned long)offset,
176 strerror(errno), mapaddr);
180 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
188 #define OFF_MAX ((uint64_t)(off_t)-1)
190 pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps)
193 char dirname[PATH_MAX];
194 char filename[PATH_MAX];
195 uint64_t offset, size;
197 for (i = 0; i != nb_maps; i++) {
199 /* check if map directory exists */
200 rte_snprintf(dirname, sizeof(dirname),
201 "%s/maps/map%u", devname, i);
203 if (access(dirname, F_OK) != 0)
206 /* get mapping offset */
207 rte_snprintf(filename, sizeof(filename),
208 "%s/offset", dirname);
209 if (pci_parse_sysfs_value(filename, &offset) < 0) {
211 "%s(): cannot parse offset of %s\n",
216 /* get mapping size */
217 rte_snprintf(filename, sizeof(filename),
219 if (pci_parse_sysfs_value(filename, &size) < 0) {
221 "%s(): cannot parse size of %s\n",
226 /* get mapping physical address */
227 rte_snprintf(filename, sizeof(filename),
229 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
231 "%s(): cannot parse addr of %s\n",
236 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
238 "%s(): offset/size exceed system max value\n",
243 maps[i].offset = offset;
250 pci_uio_map_secondary(struct rte_pci_device *dev)
253 struct uio_resource *uio_res;
255 TAILQ_FOREACH(uio_res, uio_res_list, next) {
257 /* skip this element if it doesn't match our PCI address */
258 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
261 for (i = 0; i != uio_res->nb_maps; i++) {
262 if (pci_map_resource(uio_res->maps[i].addr,
264 (off_t)uio_res->maps[i].offset,
265 (size_t)uio_res->maps[i].size)
266 != uio_res->maps[i].addr) {
268 "Cannot mmap device resource\n");
275 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
279 static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
282 char filename[PATH_MAX];
284 unsigned major, minor;
287 /* get the name of the sysfs file that contains the major and minor
288 * of the uio device and read its content */
289 rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
291 f = fopen(filename, "r");
293 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
298 ret = fscanf(f, "%d:%d", &major, &minor);
300 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
307 /* create the char device "mknod /dev/uioX c major minor" */
308 rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
309 dev = makedev(major, minor);
310 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
312 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
313 __func__, strerror(errno));
321 * Return the uioX char device used for a pci device. On success, return
322 * the UIO number and fill dstbuf string with the path of the device in
323 * sysfs. On error, return a negative value. In this case dstbuf is
326 static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
329 struct rte_pci_addr *loc = &dev->addr;
330 unsigned int uio_num;
333 char dirname[PATH_MAX];
335 /* depending on kernel version, uio can be located in uio/uioX
338 rte_snprintf(dirname, sizeof(dirname),
339 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
340 loc->domain, loc->bus, loc->devid, loc->function);
342 dir = opendir(dirname);
344 /* retry with the parent directory */
345 rte_snprintf(dirname, sizeof(dirname),
346 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
347 loc->domain, loc->bus, loc->devid, loc->function);
348 dir = opendir(dirname);
351 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
356 /* take the first file starting with "uio" */
357 while ((e = readdir(dir)) != NULL) {
358 /* format could be uio%d ...*/
359 int shortprefix_len = sizeof("uio") - 1;
360 /* ... or uio:uio%d */
361 int longprefix_len = sizeof("uio:uio") - 1;
364 if (strncmp(e->d_name, "uio", 3) != 0)
367 /* first try uio%d */
369 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
370 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
371 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
375 /* then try uio:uio%d */
377 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
378 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
379 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
385 /* No uio resource found */
389 /* create uio device if we've been asked to */
390 if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
391 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
396 /* map the PCI resource of a PCI device in virtual memory */
398 pci_uio_map_resource(struct rte_pci_device *dev)
401 char dirname[PATH_MAX];
402 char devname[PATH_MAX]; /* contains the /dev/uioX */
409 struct rte_pci_addr *loc = &dev->addr;
410 struct uio_resource *uio_res;
411 struct uio_map *maps;
413 dev->intr_handle.fd = -1;
414 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
416 /* secondary processes - use already recorded details */
417 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
418 return (pci_uio_map_secondary(dev));
420 /* find uio resource */
421 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
423 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
424 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
427 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
429 /* save fd if in primary process */
430 dev->intr_handle.fd = open(devname, O_RDWR);
431 if (dev->intr_handle.fd < 0) {
432 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
433 devname, strerror(errno));
436 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
438 /* allocate the mapping details for secondary processes*/
439 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
441 "%s(): cannot store uio mmap details\n", __func__);
445 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
446 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
448 /* collect info about device mappings */
449 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
450 RTE_DIM(uio_res->maps));
456 uio_res->nb_maps = nb_maps;
459 pagesz = sysconf(_SC_PAGESIZE);
461 maps = uio_res->maps;
462 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
465 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
468 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
469 dev->mem_resource[i].len != maps[j].size);
473 /* if matching map is found, then use it */
476 if (maps[j].addr != NULL ||
477 (mapaddr = pci_map_resource(NULL, devname,
479 (size_t)maps[j].size)
485 maps[j].addr = mapaddr;
486 maps[j].offset = offset;
487 dev->mem_resource[i].addr = mapaddr;
491 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
496 /* parse the "resource" sysfs file */
497 #define IORESOURCE_MEM 0x00000200
500 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
504 union pci_resource_info {
510 char *ptrs[PCI_RESOURCE_FMT_NVAL];
513 uint64_t phys_addr, end_addr, flags;
515 f = fopen(filename, "r");
517 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
521 for (i = 0; i<PCI_MAX_RESOURCE; i++) {
523 if (fgets(buf, sizeof(buf), f) == NULL) {
525 "%s(): cannot read resource\n", __func__);
529 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
531 "%s(): bad resource format\n", __func__);
535 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
536 end_addr = strtoull(res_info.end_addr, NULL, 16);
537 flags = strtoull(res_info.flags, NULL, 16);
540 "%s(): bad resource format\n", __func__);
544 if (flags & IORESOURCE_MEM) {
545 dev->mem_resource[i].phys_addr = phys_addr;
546 dev->mem_resource[i].len = end_addr - phys_addr + 1;
547 /* not mapped for now */
548 dev->mem_resource[i].addr = NULL;
560 * parse a sysfs file containing one integer value
561 * different to the eal version, as it needs to work with 64-bit values
564 pci_parse_sysfs_value(const char *filename, uint64_t *val)
570 f = fopen(filename, "r");
572 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
577 if (fgets(buf, sizeof(buf), f) == NULL) {
578 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
583 *val = strtoull(buf, &end, 0);
584 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
585 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
594 /* Compare two PCI device addresses. */
596 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
598 uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
599 uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
601 if (dev_addr > dev_addr2)
608 /* Scan one pci sysfs entry, and fill the devices list from it. */
610 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
611 uint8_t devid, uint8_t function)
613 char filename[PATH_MAX];
615 struct rte_pci_device *dev;
617 dev = malloc(sizeof(*dev));
622 memset(dev, 0, sizeof(*dev));
623 dev->addr.domain = domain;
625 dev->addr.devid = devid;
626 dev->addr.function = function;
629 rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
630 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
634 dev->id.vendor_id = (uint16_t)tmp;
637 rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
638 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
642 dev->id.device_id = (uint16_t)tmp;
644 /* get subsystem_vendor id */
645 rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
647 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
651 dev->id.subsystem_vendor_id = (uint16_t)tmp;
653 /* get subsystem_device id */
654 rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
656 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
660 dev->id.subsystem_device_id = (uint16_t)tmp;
664 rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
665 if (!access(filename, F_OK) &&
666 eal_parse_sysfs_value(filename, &tmp) == 0) {
667 dev->max_vfs = (uint16_t)tmp;
671 rte_snprintf(filename, sizeof(filename), "%s/numa_node",
673 if (access(filename, R_OK) != 0) {
674 /* if no NUMA support just set node to 0 */
677 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
681 dev->numa_node = tmp;
684 /* parse resources */
685 rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
686 if (pci_parse_sysfs_resource(filename, dev) < 0) {
687 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
692 /* device is valid, add in list (sorted) */
693 if (TAILQ_EMPTY(&pci_device_list)) {
694 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
697 struct rte_pci_device *dev2 = NULL;
699 TAILQ_FOREACH(dev2, &pci_device_list, next) {
700 if (pci_addr_comparison(&dev->addr, &dev2->addr))
703 TAILQ_INSERT_BEFORE(dev2, dev, next);
707 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
714 * split up a pci address into its constituent parts.
717 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
718 uint8_t *bus, uint8_t *devid, uint8_t *function)
720 /* first split on ':' */
728 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
731 char *buf_copy = strndup(buf, bufsize);
732 if (buf_copy == NULL)
735 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
738 /* final split is on '.' between devid and function */
739 splitaddr.function = strchr(splitaddr.devid,'.');
740 if (splitaddr.function == NULL)
742 *splitaddr.function++ = '\0';
744 /* now convert to int values */
746 *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
747 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
748 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
749 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
753 free(buf_copy); /* free the copy made with strdup */
761 * Scan the content of the PCI bus, and the devices in the devices
769 char dirname[PATH_MAX];
771 uint8_t bus, devid, function;
773 dir = opendir(SYSFS_PCI_DEVICES);
775 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
776 __func__, strerror(errno));
780 while ((e = readdir(dir)) != NULL) {
781 if (e->d_name[0] == '.')
784 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
785 &bus, &devid, &function) != 0)
788 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
790 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
802 * If vendor/device ID match, call the devinit() function of the
806 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
808 struct rte_pci_id *id_table;
810 for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
812 /* check if device's identifiers match the driver's ones */
813 if (id_table->vendor_id != dev->id.vendor_id &&
814 id_table->vendor_id != PCI_ANY_ID)
816 if (id_table->device_id != dev->id.device_id &&
817 id_table->device_id != PCI_ANY_ID)
819 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
820 id_table->subsystem_vendor_id != PCI_ANY_ID)
822 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
823 id_table->subsystem_device_id != PCI_ANY_ID)
826 struct rte_pci_addr *loc = &dev->addr;
828 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
829 loc->domain, loc->bus, loc->devid, loc->function,
832 RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
833 dev->id.device_id, dr->name);
835 /* no initialization when blacklisted, return without error */
836 if (dev->devargs != NULL &&
837 dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
838 RTE_LOG(DEBUG, EAL, " Device is blacklisted, not initializing\n");
842 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
843 /* map resources for devices that use igb_uio */
844 if (pci_uio_map_resource(dev) < 0)
846 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
847 rte_eal_process_type() == RTE_PROC_PRIMARY) {
848 /* unbind current driver */
849 if (pci_unbind_kernel_driver(dev) < 0)
853 /* reference driver structure */
856 /* call the driver devinit() function */
857 return dr->devinit(dr, dev);
859 /* return positive value if driver is not found */
863 /* Init the PCI EAL subsystem */
865 rte_eal_pci_init(void)
867 TAILQ_INIT(&pci_driver_list);
868 TAILQ_INIT(&pci_device_list);
869 uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
871 /* for debug purposes, PCI can be disabled */
872 if (internal_config.no_pci)
875 if (pci_scan() < 0) {
876 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);