net/bonding: use local prefix namespace
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci_uio.c
index db22d31..d17837a 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 #include <dirent.h>
+#include <inttypes.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <sys/sysmacros.h>
 #include <linux/pci_regs.h>
 
-#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
+#if defined(RTE_ARCH_X86)
 #include <sys/io.h>
 #endif
 
@@ -132,7 +134,7 @@ pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
        snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
        dev = makedev(major, minor);
        ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
-       if (f == NULL) {
+       if (ret != 0) {
                RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
                        __func__, strerror(errno));
                return -1;
@@ -152,7 +154,7 @@ pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
                           unsigned int buflen, int create)
 {
        struct rte_pci_addr *loc = &dev->addr;
-       unsigned int uio_num;
+       int uio_num = -1;
        struct dirent *e;
        DIR *dir;
        char dirname[PATH_MAX];
@@ -161,14 +163,14 @@ pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
         * or uio:uioX */
 
        snprintf(dirname, sizeof(dirname),
-                       SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
+                       "%s/" PCI_PRI_FMT "/uio", pci_get_sysfs_path(),
                        loc->domain, loc->bus, loc->devid, loc->function);
 
        dir = opendir(dirname);
        if (dir == NULL) {
                /* retry with the parent directory */
                snprintf(dirname, sizeof(dirname),
-                               SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
+                               "%s/" PCI_PRI_FMT, pci_get_sysfs_path(),
                                loc->domain, loc->bus, loc->devid, loc->function);
                dir = opendir(dirname);
 
@@ -212,7 +214,7 @@ pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
                return -1;
 
        /* create uio device if we've been asked to */
-       if (internal_config.create_uio_dev && create &&
+       if (rte_eal_create_uio_dev() && create &&
                        pci_mknod_uio_dev(dstbuf, uio_num) < 0)
                RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
 
@@ -229,7 +231,7 @@ pci_uio_free_resource(struct rte_pci_device *dev,
                close(dev->intr_handle.uio_cfg_fd);
                dev->intr_handle.uio_cfg_fd = -1;
        }
-       if (dev->intr_handle.fd) {
+       if (dev->intr_handle.fd >= 0) {
                close(dev->intr_handle.fd);
                dev->intr_handle.fd = -1;
                dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
@@ -309,7 +311,7 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
                struct mapped_pci_resource *uio_res, int map_idx)
 {
        int fd;
-       char devname[PATH_MAX]; /* contains the /dev/uioX */
+       char devname[PATH_MAX];
        void *mapaddr;
        struct rte_pci_addr *loc;
        struct pci_map *maps;
@@ -319,7 +321,8 @@ pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
 
        /* update devname for mmap  */
        snprintf(devname, sizeof(devname),
-                       SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
+                       "%s/" PCI_PRI_FMT "/resource%d",
+                       pci_get_sysfs_path(),
                        loc->domain, loc->bus, loc->devid,
                        loc->function, res_idx);
 
@@ -368,11 +371,11 @@ error:
        return -1;
 }
 
+#if defined(RTE_ARCH_X86)
 int
 pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
                   struct rte_pci_ioport *p)
 {
-#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
        char dirname[PATH_MAX];
        char filename[PATH_MAX];
        int uio_num;
@@ -411,81 +414,154 @@ pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
        RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%lx\n", start);
 
        p->base = start;
+       p->len = 0;
        return 0;
+}
 #else
-       RTE_SET_USED(dev);
-       RTE_SET_USED(bar);
-       RTE_SET_USED(p);
+int
+pci_uio_ioport_map(struct rte_pci_device *dev, int bar,
+                  struct rte_pci_ioport *p)
+{
+       FILE *f;
+       char buf[BUFSIZ];
+       char filename[PATH_MAX];
+       uint64_t phys_addr, end_addr, flags;
+       int fd, i;
+       void *addr;
+
+       /* open and read addresses of the corresponding resource in sysfs */
+       snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource",
+               pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
+               dev->addr.devid, dev->addr.function);
+       f = fopen(filename, "r");
+       if (f == NULL) {
+               RTE_LOG(ERR, EAL, "Cannot open sysfs resource: %s\n",
+                       strerror(errno));
+               return -1;
+       }
+       for (i = 0; i < bar + 1; i++) {
+               if (fgets(buf, sizeof(buf), f) == NULL) {
+                       RTE_LOG(ERR, EAL, "Cannot read sysfs resource\n");
+                       goto error;
+               }
+       }
+       if (pci_parse_one_sysfs_resource(buf, sizeof(buf), &phys_addr,
+                       &end_addr, &flags) < 0)
+               goto error;
+       if ((flags & IORESOURCE_IO) == 0) {
+               RTE_LOG(ERR, EAL, "BAR %d is not an IO resource\n", bar);
+               goto error;
+       }
+       snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT "/resource%d",
+               pci_get_sysfs_path(), dev->addr.domain, dev->addr.bus,
+               dev->addr.devid, dev->addr.function, bar);
+
+       /* mmap the pci resource */
+       fd = open(filename, O_RDWR);
+       if (fd < 0) {
+               RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
+                       strerror(errno));
+               goto error;
+       }
+       addr = mmap(NULL, end_addr + 1, PROT_READ | PROT_WRITE,
+               MAP_SHARED, fd, 0);
+       close(fd);
+       if (addr == MAP_FAILED) {
+               RTE_LOG(ERR, EAL, "Cannot mmap IO port resource: %s\n",
+                       strerror(errno));
+               goto error;
+       }
+
+       /* strangely, the base address is mmap addr + phys_addr */
+       p->base = (uintptr_t)addr + phys_addr;
+       p->len = end_addr + 1;
+       RTE_LOG(DEBUG, EAL, "PCI Port IO found start=0x%"PRIx64"\n", p->base);
+       fclose(f);
+
+       return 0;
+
+error:
+       fclose(f);
        return -1;
-#endif
 }
+#endif
 
 void
 pci_uio_ioport_read(struct rte_pci_ioport *p,
                    void *data, size_t len, off_t offset)
 {
-#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
        uint8_t *d;
        int size;
-       unsigned short reg = p->base + offset;
+       uintptr_t reg = p->base + offset;
 
        for (d = data; len > 0; d += size, reg += size, len -= size) {
                if (len >= 4) {
                        size = 4;
+#if defined(RTE_ARCH_X86)
                        *(uint32_t *)d = inl(reg);
+#else
+                       *(uint32_t *)d = *(volatile uint32_t *)reg;
+#endif
                } else if (len >= 2) {
                        size = 2;
+#if defined(RTE_ARCH_X86)
                        *(uint16_t *)d = inw(reg);
+#else
+                       *(uint16_t *)d = *(volatile uint16_t *)reg;
+#endif
                } else {
                        size = 1;
+#if defined(RTE_ARCH_X86)
                        *d = inb(reg);
-               }
-       }
 #else
-       RTE_SET_USED(p);
-       RTE_SET_USED(data);
-       RTE_SET_USED(len);
-       RTE_SET_USED(offset);
+                       *d = *(volatile uint8_t *)reg;
 #endif
+               }
+       }
 }
 
 void
 pci_uio_ioport_write(struct rte_pci_ioport *p,
                     const void *data, size_t len, off_t offset)
 {
-#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
        const uint8_t *s;
        int size;
-       unsigned short reg = p->base + offset;
+       uintptr_t reg = p->base + offset;
 
        for (s = data; len > 0; s += size, reg += size, len -= size) {
                if (len >= 4) {
                        size = 4;
+#if defined(RTE_ARCH_X86)
                        outl_p(*(const uint32_t *)s, reg);
+#else
+                       *(volatile uint32_t *)reg = *(const uint32_t *)s;
+#endif
                } else if (len >= 2) {
                        size = 2;
+#if defined(RTE_ARCH_X86)
                        outw_p(*(const uint16_t *)s, reg);
+#else
+                       *(volatile uint16_t *)reg = *(const uint16_t *)s;
+#endif
                } else {
                        size = 1;
+#if defined(RTE_ARCH_X86)
                        outb_p(*s, reg);
-               }
-       }
 #else
-       RTE_SET_USED(p);
-       RTE_SET_USED(data);
-       RTE_SET_USED(len);
-       RTE_SET_USED(offset);
+                       *(volatile uint8_t *)reg = *s;
 #endif
+               }
+       }
 }
 
 int
 pci_uio_ioport_unmap(struct rte_pci_ioport *p)
 {
+#if defined(RTE_ARCH_X86)
        RTE_SET_USED(p);
-#if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
        /* FIXME close intr fd ? */
        return 0;
 #else
-       return -1;
+       return munmap((void *)(uintptr_t)p->base, p->len);
 #endif
 }