- int i, j;
- struct dirent *e;
- DIR *dir;
- char dirname[PATH_MAX];
- char dirname2[PATH_MAX];
- char devname[PATH_MAX]; /* contains the /dev/uioX */
- void *mapaddr;
- unsigned uio_num;
- uint64_t phaddr;
- uint64_t offset;
- uint64_t pagesz;
- ssize_t nb_maps;
- struct rte_pci_addr *loc = &dev->addr;
- struct uio_resource *uio_res;
- struct uio_map *maps;
-
- RTE_LOG(DEBUG, EAL, "map PCI resource for device "PCI_PRI_FMT"\n",
- loc->domain, loc->bus, loc->devid, loc->function);
-
- /* secondary processes - use already recorded details */
- if (rte_eal_process_type() != RTE_PROC_PRIMARY)
- return pci_uio_map_restore(dev);
-
- /* depending on kernel version, uio can be located in uio/uioX
- * or uio:uioX */
-
- rte_snprintf(dirname, sizeof(dirname),
- "/sys/bus/pci/devices/" PCI_PRI_FMT "/uio",
- loc->domain, loc->bus, loc->devid, loc->function);
-
- dir = opendir(dirname);
- if (dir == NULL) {
- /* retry with the parent directory */
- rte_snprintf(dirname, sizeof(dirname),
- "/sys/bus/pci/devices/" PCI_PRI_FMT,
- loc->domain, loc->bus, loc->devid, loc->function);
- dir = opendir(dirname);
-
- if (dir == NULL) {
- RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
- return -1;
- }
- }
-
- /* take the first file starting with "uio" */
- while ((e = readdir(dir)) != NULL) {
- int shortprefix_len = sizeof("uio") - 1; /* format could be uio%d ...*/
- int longprefix_len = sizeof("uio:uio") - 1; /* ... or uio:uio%d */
- char *endptr;
-
- if (strncmp(e->d_name, "uio", 3) != 0)
- continue;
-
- /* first try uio%d */
- errno = 0;
- uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
- if (errno == 0 && endptr != e->d_name) {
- rte_snprintf(dirname2, sizeof(dirname2),
- "%s/uio%u", dirname, uio_num);
- break;
- }
-
- /* then try uio:uio%d */
- errno = 0;
- uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
- if (errno == 0 && endptr != e->d_name) {
- rte_snprintf(dirname2, sizeof(dirname2),
- "%s/uio:uio%u", dirname, uio_num);
- break;
- }
- }
- closedir(dir);
-
- /* No uio resource found */
- if (e == NULL) {
- RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
- "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
- return -1;
- }
-
- /* allocate the mapping details for secondary processes*/
- if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
- RTE_LOG(ERR, EAL,
- "%s(): cannot store uio mmap details\n", __func__);
- return (-1);
- }
-
- rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
- rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
- memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
-
- /* collect info about device mappings */
- if ((nb_maps = pci_uio_get_mappings(dirname2, uio_res->maps,
- sizeof (uio_res->maps) / sizeof (uio_res->maps[0])))
- < 0)
- return (nb_maps);
-
- uio_res->nb_maps = nb_maps;
-
- /* Map all BARs */
- pagesz = sysconf(_SC_PAGESIZE);
-
- maps = uio_res->maps;
- for (i = 0; i != PCI_MAX_RESOURCE; i++) {
-
- /* skip empty BAR */
- if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
- continue;
-
- for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
- dev->mem_resource[i].len != maps[j].size);
- j++)
- ;
-
- /* if matching map is found, then use it */
- if (j != nb_maps) {
- offset = j * pagesz;
- if (maps[j].addr != NULL ||
- (mapaddr = pci_map_resource(dev,
- NULL, devname, (off_t)offset,
- (size_t)maps[j].size)) == NULL) {
- return (-1);
- }
-
- maps[j].addr = mapaddr;
- maps[j].offset = offset;
- dev->mem_resource[i].addr = mapaddr;
- }
- }
-
- TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);