pci: remove eal prefix
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_pci.c
index 761c2f3..6294b7e 100644 (file)
 #include <sys/pciio.h>
 #include <dev/pci/pcireg.h>
 
+#if defined(RTE_ARCH_X86)
+#include <sys/types.h>
+#include <machine/cpufunc.h>
+#endif
+
 #include <rte_interrupts.h>
 #include <rte_log.h>
 #include <rte_pci.h>
@@ -67,7 +72,6 @@
 #include <rte_debug.h>
 #include <rte_devargs.h>
 
-#include "rte_pci_dev_ids.h"
 #include "eal_filesystem.h"
 #include "eal_private.h"
 
  * enabling bus master.
  */
 
-/* unbind kernel driver for this device */
+extern struct rte_pci_bus rte_pci_bus;
+
+/* Map pci device */
 int
-pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
+rte_pci_map_device(struct rte_pci_device *dev)
+{
+       int ret = -1;
+
+       /* try mapping the NIC resources */
+       switch (dev->kdrv) {
+       case RTE_KDRV_NIC_UIO:
+               /* map resources for devices that use uio */
+               ret = pci_uio_map_resource(dev);
+               break;
+       default:
+               RTE_LOG(DEBUG, EAL,
+                       "  Not managed by a supported kernel driver, skipped\n");
+               ret = 1;
+               break;
+       }
+
+       return ret;
+}
+
+/* Unmap pci device */
+void
+rte_pci_unmap_device(struct rte_pci_device *dev)
 {
-       RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
-               "for BSD\n");
-       return -ENOTSUP;
+       /* try unmapping the NIC resources */
+       switch (dev->kdrv) {
+       case RTE_KDRV_NIC_UIO:
+               /* unmap resources for devices that use uio */
+               pci_uio_unmap_resource(dev);
+               break;
+       default:
+               RTE_LOG(DEBUG, EAL,
+                       "  Not managed by a supported kernel driver, skipped\n");
+               break;
+       }
 }
 
 void
@@ -235,11 +271,19 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
        /* get subsystem_device id */
        dev->id.subsystem_device_id = conf->pc_subdevice;
 
+       /* get class id */
+       dev->id.class_id = (conf->pc_class << 16) |
+                          (conf->pc_subclass << 8) |
+                          (conf->pc_progif);
+
        /* TODO: get max_vfs */
        dev->max_vfs = 0;
 
        /* FreeBSD has no NUMA support (yet) */
-       dev->numa_node = 0;
+       dev->device.numa_node = 0;
+
+       rte_pci_device_name(&dev->addr, dev->name, sizeof(dev->name));
+       dev->device.name = dev->name;
 
        /* FreeBSD has only one pass through driver */
        dev->kdrv = RTE_KDRV_NIC_UIO;
@@ -274,20 +318,19 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
        }
 
        /* device is valid, add in list (sorted) */
-       if (TAILQ_EMPTY(&pci_device_list)) {
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+       if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
+               rte_pci_add_device(dev);
        }
        else {
                struct rte_pci_device *dev2 = NULL;
                int ret;
 
-               TAILQ_FOREACH(dev2, &pci_device_list, next) {
+               TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
                        ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
                        if (ret > 0)
                                continue;
                        else if (ret < 0) {
-                               TAILQ_INSERT_BEFORE(dev2, dev, next);
-                               return 0;
+                               rte_pci_insert_device(dev2, dev);
                        } else { /* already registered */
                                dev2->kdrv = dev->kdrv;
                                dev2->max_vfs = dev->max_vfs;
@@ -295,10 +338,10 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
                                        dev->mem_resource,
                                        sizeof(dev->mem_resource));
                                free(dev);
-                               return 0;
                        }
+                       return 0;
                }
-               TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+               rte_pci_add_device(dev);
        }
 
        return 0;
@@ -312,8 +355,8 @@ skipdev:
  * Scan the content of the PCI bus, and add the devices in the devices
  * list. Call pci_scan_one() for each pci entry found.
  */
-static int
-pci_scan(void)
+int
+rte_pci_scan(void)
 {
        int fd;
        unsigned dev_count = 0;
@@ -326,6 +369,10 @@ pci_scan(void)
                        .matches = &matches[0],
        };
 
+       /* for debug purposes, PCI can be disabled */
+       if (internal_config.no_pci)
+               return 0;
+
        fd = open("/dev/pci", O_RDONLY);
        if (fd < 0) {
                RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
@@ -358,20 +405,264 @@ error:
        return -1;
 }
 
-/* Init the PCI EAL subsystem */
 int
-rte_eal_pci_init(void)
+pci_update_device(const struct rte_pci_addr *addr)
 {
-       TAILQ_INIT(&pci_driver_list);
-       TAILQ_INIT(&pci_device_list);
+       int fd;
+       struct pci_conf matches[2];
+       struct pci_match_conf match = {
+               .pc_sel = {
+                       .pc_domain = addr->domain,
+                       .pc_bus = addr->bus,
+                       .pc_dev = addr->devid,
+                       .pc_func = addr->function,
+               },
+       };
+       struct pci_conf_io conf_io = {
+               .pat_buf_len = 0,
+               .num_patterns = 1,
+               .patterns = &match,
+               .match_buf_len = sizeof(matches),
+               .matches = &matches[0],
+       };
 
-       /* for debug purposes, PCI can be disabled */
-       if (internal_config.no_pci)
-               return 0;
+       fd = open("/dev/pci", O_RDONLY);
+       if (fd < 0) {
+               RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
+               goto error;
+       }
 
-       if (pci_scan() < 0) {
-               RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
-               return -1;
+       if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
+               RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
+                               __func__, strerror(errno));
+               goto error;
+       }
+
+       if (conf_io.num_matches != 1)
+               goto error;
+
+       if (pci_scan_one(fd, &matches[0]) < 0)
+               goto error;
+
+       close(fd);
+
+       return 0;
+
+error:
+       if (fd >= 0)
+               close(fd);
+       return -1;
+}
+
+/* Read PCI config space. */
+int rte_pci_read_config(const struct rte_pci_device *dev,
+               void *buf, size_t len, off_t offset)
+{
+       int fd = -1;
+       struct pci_io pi = {
+               .pi_sel = {
+                       .pc_domain = dev->addr.domain,
+                       .pc_bus = dev->addr.bus,
+                       .pc_dev = dev->addr.devid,
+                       .pc_func = dev->addr.function,
+               },
+               .pi_reg = offset,
+               .pi_width = len,
+       };
+
+       if (len == 3 || len > sizeof(pi.pi_data)) {
+               RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
+               goto error;
+       }
+
+       fd = open("/dev/pci", O_RDWR);
+       if (fd < 0) {
+               RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
+               goto error;
+       }
+
+       if (ioctl(fd, PCIOCREAD, &pi) < 0)
+               goto error;
+       close(fd);
+
+       memcpy(buf, &pi.pi_data, len);
+       return 0;
+
+ error:
+       if (fd >= 0)
+               close(fd);
+       return -1;
+}
+
+/* Write PCI config space. */
+int rte_pci_write_config(const struct rte_pci_device *dev,
+               const void *buf, size_t len, off_t offset)
+{
+       int fd = -1;
+
+       struct pci_io pi = {
+               .pi_sel = {
+                       .pc_domain = dev->addr.domain,
+                       .pc_bus = dev->addr.bus,
+                       .pc_dev = dev->addr.devid,
+                       .pc_func = dev->addr.function,
+               },
+               .pi_reg = offset,
+               .pi_data = *(const uint32_t *)buf,
+               .pi_width = len,
+       };
+
+       if (len == 3 || len > sizeof(pi.pi_data)) {
+               RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
+               goto error;
        }
+
+       memcpy(&pi.pi_data, buf, len);
+
+       fd = open("/dev/pci", O_RDWR);
+       if (fd < 0) {
+               RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
+               goto error;
+       }
+
+       if (ioctl(fd, PCIOCWRITE, &pi) < 0)
+               goto error;
+
+       close(fd);
        return 0;
+
+ error:
+       if (fd >= 0)
+               close(fd);
+       return -1;
+}
+
+int
+rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
+               struct rte_pci_ioport *p)
+{
+       int ret;
+
+       switch (dev->kdrv) {
+#if defined(RTE_ARCH_X86)
+       case RTE_KDRV_NIC_UIO:
+               if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
+                       p->base = (uintptr_t)dev->mem_resource[bar].addr;
+                       ret = 0;
+               } else
+                       ret = -1;
+               break;
+#endif
+       default:
+               ret = -1;
+               break;
+       }
+
+       if (!ret)
+               p->dev = dev;
+
+       return ret;
+}
+
+static void
+pci_uio_ioport_read(struct rte_pci_ioport *p,
+               void *data, size_t len, off_t offset)
+{
+#if defined(RTE_ARCH_X86)
+       uint8_t *d;
+       int size;
+       unsigned short reg = p->base + offset;
+
+       for (d = data; len > 0; d += size, reg += size, len -= size) {
+               if (len >= 4) {
+                       size = 4;
+                       *(uint32_t *)d = inl(reg);
+               } else if (len >= 2) {
+                       size = 2;
+                       *(uint16_t *)d = inw(reg);
+               } else {
+                       size = 1;
+                       *d = inb(reg);
+               }
+       }
+#else
+       RTE_SET_USED(p);
+       RTE_SET_USED(data);
+       RTE_SET_USED(len);
+       RTE_SET_USED(offset);
+#endif
+}
+
+void
+rte_pci_ioport_read(struct rte_pci_ioport *p,
+               void *data, size_t len, off_t offset)
+{
+       switch (p->dev->kdrv) {
+       case RTE_KDRV_NIC_UIO:
+               pci_uio_ioport_read(p, data, len, offset);
+               break;
+       default:
+               break;
+       }
+}
+
+static void
+pci_uio_ioport_write(struct rte_pci_ioport *p,
+               const void *data, size_t len, off_t offset)
+{
+#if defined(RTE_ARCH_X86)
+       const uint8_t *s;
+       int size;
+       unsigned short reg = p->base + offset;
+
+       for (s = data; len > 0; s += size, reg += size, len -= size) {
+               if (len >= 4) {
+                       size = 4;
+                       outl(*(const uint32_t *)s, reg);
+               } else if (len >= 2) {
+                       size = 2;
+                       outw(*(const uint16_t *)s, reg);
+               } else {
+                       size = 1;
+                       outb(*s, reg);
+               }
+       }
+#else
+       RTE_SET_USED(p);
+       RTE_SET_USED(data);
+       RTE_SET_USED(len);
+       RTE_SET_USED(offset);
+#endif
+}
+
+void
+rte_pci_ioport_write(struct rte_pci_ioport *p,
+               const void *data, size_t len, off_t offset)
+{
+       switch (p->dev->kdrv) {
+       case RTE_KDRV_NIC_UIO:
+               pci_uio_ioport_write(p, data, len, offset);
+               break;
+       default:
+               break;
+       }
+}
+
+int
+rte_pci_ioport_unmap(struct rte_pci_ioport *p)
+{
+       int ret;
+
+       switch (p->dev->kdrv) {
+#if defined(RTE_ARCH_X86)
+       case RTE_KDRV_NIC_UIO:
+               ret = 0;
+               break;
+#endif
+       default:
+               ret = -1;
+               break;
+       }
+
+       return ret;
 }