- unsigned n;
- int fd;
- void *mapaddr;
-
- /*
- * open devname, and mmap it: it can take some time to
- * appear, so we wait some time before returning an error
- */
- for (n=0; n<UIO_DEV_WAIT_TIMEOUT*10; n++) {
- fd = open(devname, O_RDWR);
- if (fd >= 0)
- break;
- if (errno != ENOENT)
- break;
- usleep(100000);
- }
- if (fd < 0) {
- RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", devname, strerror(errno));
- goto fail;
- }
-
- /* Map the PCI memory resource of device */
- mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, offset);
- if (mapaddr == MAP_FAILED ||
- (requested_addr != NULL && mapaddr != requested_addr)) {
- RTE_LOG(ERR, EAL, "%s(): cannot mmap %s: %s\n", __func__,
- devname, strerror(errno));
- close(fd);
- goto fail;
- }
- if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
- /* save fd if in primary process */
- dev->intr_handle.fd = fd;
- dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
- } else {
- /* fd is not needed in slave process, close it */
- dev->intr_handle.fd = -1;
- dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
- close(fd);
- }
-
- RTE_LOG(DEBUG, EAL, "PCI memory mapped at %p\n", mapaddr);
-
- return mapaddr;
-
-fail:
- dev->intr_handle.fd = -1;
- dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
-
- return NULL;
-}
-/* map the PCI resource of a PCI device in virtual memory */
-static int
-pci_uio_map_resource(struct rte_pci_device *dev)
-{
- struct dirent *e;
- DIR *dir;
- char dirname[PATH_MAX];
- char dirname2[PATH_MAX];
- char filename[PATH_MAX];
- char devname[PATH_MAX]; /* contains the /dev/uioX */
- void *mapaddr;
- unsigned uio_num;
- unsigned long size, offset;
- struct rte_pci_addr *loc = &dev->addr;
- struct uio_resource *uio_res;
-
- 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) {
-
- TAILQ_FOREACH(uio_res, uio_res_list, next) {
- /* skip this element if it doesn't match our PCI address */
- if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
- continue;
-
- if (pci_map_resource(dev, uio_res->addr, uio_res->path, \
- uio_res->offset, uio_res->size) == uio_res->addr)
- return 0;
- else {
- RTE_LOG(ERR, EAL, "Cannot mmap device resource\n");
- return -1;
- }
- }
- RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
- return -1;
- }
-
- /* 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;