update Intel copyright years to 2014
[dpdk.git] / lib / librte_eal / linuxapp / igb_uio / igb_uio.c
index 7b5762e..61c004b 100644 (file)
@@ -1,26 +1,25 @@
 /*-
  * GPL LICENSE SUMMARY
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  * 
- *   This program is free software; you can redistribute it and/or modify 
+ *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of version 2 of the GNU General Public License as
  *   published by the Free Software Foundation.
  * 
- *   This program is distributed in the hope that it will be useful, but 
- *   WITHOUT ANY WARRANTY; without even the implied warranty of 
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *   General Public License for more details.
  * 
- *   You should have received a copy of the GNU General Public License 
- *   along with this program; if not, write to the Free Software 
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
- *   The full GNU General Public License is included in this distribution 
+ *   The full GNU General Public License is included in this distribution
  *   in the file called LICENSE.GPL.
  * 
  *   Contact Information:
  *   Intel Corporation
- * 
  */
 
 #include <linux/device.h>
 #include <linux/msi.h>
 #include <linux/version.h>
 
-/* Some function names changes between 3.2.0 and 3.3.0... */
-#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
-#define PCI_LOCK pci_block_user_cfg_access
-#define PCI_UNLOCK pci_unblock_user_cfg_access
-#else
-#define PCI_LOCK pci_cfg_access_lock
-#define PCI_UNLOCK pci_cfg_access_unlock
-#endif
-
 /**
  * MSI-X related macros, copy from linux/pci_regs.h in kernel 2.6.39,
  * but none of them in kernel 2.6.35.
@@ -75,21 +65,114 @@ struct rte_uio_pci_dev {
                msix_entries[IGBUIO_NUM_MSI_VECTORS]; /* pointer to the msix vectors to be allocated later */
 };
 
-static const enum igbuio_intr_mode igbuio_intr_mode_preferred = IGBUIO_MSIX_INTR_MODE;
+static char *intr_mode = NULL;
+static enum igbuio_intr_mode igbuio_intr_mode_preferred = IGBUIO_MSIX_INTR_MODE;
 
 /* PCI device id table */
 static struct pci_device_id igbuio_pci_ids[] = {
-#define RTE_PCI_DEV_ID_DECL(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_IGBVF(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {PCI_DEVICE(vend, dev)},
+#define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {PCI_DEVICE(vend, dev)},
 #include <rte_pci_dev_ids.h>
 { 0, },
 };
 
+MODULE_DEVICE_TABLE(pci, igbuio_pci_ids);
+
 static inline struct rte_uio_pci_dev *
 igbuio_get_uio_pci_dev(struct uio_info *info)
 {
        return container_of(info, struct rte_uio_pci_dev, info);
 }
 
+/* sriov sysfs */
+int local_pci_num_vf(struct pci_dev *dev)
+{
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
+       struct iov {
+               int pos;
+               int nres;
+               u32 cap;
+               u16 ctrl;
+               u16 total;
+               u16 initial;
+               u16 nr_virtfn;
+       } *iov = (struct iov*)dev->sriov;
+
+       if (!dev->is_physfn)
+               return 0;
+       
+       return iov->nr_virtfn;
+#else
+       return pci_num_vf(dev);
+#endif
+}
+
+static ssize_t
+show_max_vfs(struct device *dev, struct device_attribute *attr,
+            char *buf)
+{
+       return snprintf(buf, 10, "%u\n", local_pci_num_vf(
+                               container_of(dev, struct pci_dev, dev)));
+}
+
+static ssize_t
+store_max_vfs(struct device *dev, struct device_attribute *attr,
+             const char *buf, size_t count)
+{
+       int err = 0;
+       unsigned long max_vfs;
+       struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+
+       if (0 != strict_strtoul(buf, 0, &max_vfs))
+               return -EINVAL;
+
+       if (0 == max_vfs)
+               pci_disable_sriov(pdev);
+       else if (0 == local_pci_num_vf(pdev))
+               err = pci_enable_sriov(pdev, max_vfs);
+       else /* do nothing if change max_vfs number */
+               err = -EINVAL;
+
+       return err ? err : count;                                                       
+}
+
+static DEVICE_ATTR(max_vfs, S_IRUGO | S_IWUSR, show_max_vfs, store_max_vfs);
+static struct attribute *dev_attrs[] = {
+       &dev_attr_max_vfs.attr,
+        NULL,
+};
+
+static const struct attribute_group dev_attr_grp = {
+       .attrs = dev_attrs,
+};
+
+static inline int
+pci_lock(struct pci_dev * pdev)
+{
+       /* Some function names changes between 3.2.0 and 3.3.0... */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
+       pci_block_user_cfg_access(pdev);
+       return 1;
+#else
+       return pci_cfg_access_trylock(pdev);
+#endif
+}
+
+static inline void
+pci_unlock(struct pci_dev * pdev)
+{
+       /* Some function names changes between 3.2.0 and 3.3.0... */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
+       pci_unblock_user_cfg_access(pdev);
+#else
+       pci_cfg_access_unlock(pdev);
+#endif
+}
+
 /**
  * It masks the msix on/off of generating MSI-X messages.
  */
@@ -176,11 +259,14 @@ igbuio_pci_irqcontrol(struct uio_info *info, s32 irq_state)
        struct pci_dev *pdev = udev->pdev;
 
        spin_lock_irqsave(&udev->lock, flags);
-       PCI_LOCK(pdev);
+       if (!pci_lock(pdev)) {
+               spin_unlock_irqrestore(&udev->lock, flags);
+               return -1;
+       }
 
        igbuio_set_interrupt_mask(udev, irq_state);
 
-       PCI_UNLOCK(pdev);
+       pci_unlock(pdev);
        spin_unlock_irqrestore(&udev->lock, flags);
 
        return 0;
@@ -202,7 +288,8 @@ igbuio_pci_irqhandler(int irq, struct uio_info *info)
 
        spin_lock_irqsave(&udev->lock, flags);
        /* block userspace PCI config reads/writes */
-       PCI_LOCK(pdev);
+       if (!pci_lock(pdev))
+               goto spin_unlock;
 
        /* for legacy mode, interrupt maybe shared */
        if (udev->mode == IGBUIO_LEGACY_INTR_MODE) {
@@ -217,7 +304,8 @@ igbuio_pci_irqhandler(int irq, struct uio_info *info)
        ret = IRQ_HANDLED;
 done:
        /* unblock userspace PCI config reads/writes */
-       PCI_UNLOCK(pdev);
+       pci_unlock(pdev);
+spin_unlock:
        spin_unlock_irqrestore(&udev->lock, flags);
        printk(KERN_INFO "irq 0x%x %s\n", irq, (ret == IRQ_HANDLED) ? "handled" : "not handled");
 
@@ -232,6 +320,9 @@ igbuio_pci_setup_iomem(struct pci_dev *dev, struct uio_info *info,
        unsigned long addr, len;
        void *internal_addr;
 
+       if (sizeof(info->mem) / sizeof (info->mem[0]) <= n)  
+               return (EINVAL);
+
        addr = pci_resource_start(dev, pci_bar);
        len = pci_resource_len(dev, pci_bar);
        if (addr == 0 || len == 0)
@@ -247,6 +338,29 @@ igbuio_pci_setup_iomem(struct pci_dev *dev, struct uio_info *info,
        return 0;
 }
 
+/* Get pci port io resources described by bar #pci_bar in uio resource n. */
+static int
+igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,
+               int n, int pci_bar, const char *name)
+{
+       unsigned long addr, len;
+
+       if (sizeof(info->port) / sizeof (info->port[0]) <= n)  
+               return (EINVAL);
+
+       addr = pci_resource_start(dev, pci_bar);
+       len = pci_resource_len(dev, pci_bar);
+       if (addr == 0 || len == 0)
+               return (-1);
+
+       info->port[n].name = name;
+       info->port[n].start = addr;
+       info->port[n].size = len;
+       info->port[n].porttype = UIO_PORT_X86;
+
+       return (0);
+}
+
 /* Unmap previously ioremap'd resources */
 static void
 igbuio_pci_release_iomem(struct uio_info *info)
@@ -258,7 +372,49 @@ igbuio_pci_release_iomem(struct uio_info *info)
        }
 }
 
+static int
+igbuio_setup_bars(struct pci_dev *dev, struct uio_info *info)
+{
+       int i, iom, iop, ret;
+       unsigned long flags;
+       static const char *bar_names[PCI_STD_RESOURCE_END + 1]  = {
+               "BAR0",
+               "BAR1",
+               "BAR2",
+               "BAR3",
+               "BAR4",
+               "BAR5",
+       };
+
+       iom = 0;
+       iop = 0;
+
+       for (i = 0; i != sizeof(bar_names) / sizeof(bar_names[0]); i++) {
+               if (pci_resource_len(dev, i) != 0 &&
+                               pci_resource_start(dev, i) != 0) {
+                       flags = pci_resource_flags(dev, i);
+                       if (flags & IORESOURCE_MEM) {
+                               if ((ret = igbuio_pci_setup_iomem(dev, info,
+                                               iom, i, bar_names[i])) != 0)
+                                       return (ret);
+                               iom++;
+                       } else if (flags & IORESOURCE_IO) {
+                               if ((ret = igbuio_pci_setup_ioport(dev, info,
+                                               iop, i, bar_names[i])) != 0)
+                                       return (ret);
+                               iop++;
+                       }
+               }
+       }
+
+       return ((iom != 0) ? ret : ENOENT);
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
 static int __devinit
+#else
+static int
+#endif
 igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
 {
        struct rte_uio_pci_dev *udev;
@@ -276,13 +432,6 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
                goto fail_free;
        }
 
-       /* XXX should we use 64 bits ? */
-       /* set 32-bit DMA mask */
-       if (pci_set_dma_mask(dev,(uint64_t)0xffffffff)) {
-               printk(KERN_ERR "Cannot set DMA mask\n");
-               goto fail_disable;
-       }
-
        /*
         * reserve device's PCI memory regions for use by this
         * module
@@ -296,8 +445,17 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
        pci_set_master(dev);
 
        /* remap IO memory */
-       if (igbuio_pci_setup_iomem(dev, &udev->info, 0, 0, "config"))
-               goto fail_release_regions;
+       if (igbuio_setup_bars(dev, &udev->info))
+               goto fail_release_iomem;
+
+       /* set 64-bit DMA mask */
+       if (pci_set_dma_mask(dev,  DMA_BIT_MASK(64))) {
+               printk(KERN_ERR "Cannot set DMA mask\n");
+               goto fail_release_iomem;
+       } else if (pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(64))) {
+               printk(KERN_ERR "Cannot set consistent DMA mask\n");
+               goto fail_release_iomem;
+       }
 
        /* fill uio infos */
        udev->info.name = "Intel IGB UIO";
@@ -342,6 +500,9 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
        pci_set_drvdata(dev, udev);
        igbuio_pci_irqcontrol(&udev->info, 0);
 
+       if (sysfs_create_group(&dev->dev.kobj, &dev_attr_grp))
+               goto fail_release_iomem;
+
        /* register uio driver */
        if (uio_register_device(&dev->dev, &udev->info))
                goto fail_release_iomem;
@@ -351,10 +512,10 @@ igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
        return 0;
 
 fail_release_iomem:
+       sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
        igbuio_pci_release_iomem(&udev->info);
        if (udev->mode == IGBUIO_MSIX_INTR_MODE)
                pci_disable_msix(udev->pdev);
-fail_release_regions:
        pci_release_regions(dev);
 fail_disable:
        pci_disable_device(dev);
@@ -369,8 +530,16 @@ igbuio_pci_remove(struct pci_dev *dev)
 {
        struct uio_info *info = pci_get_drvdata(dev);
 
+       if (info->priv == NULL) {
+               printk(KERN_DEBUG "Not igbuio device\n");
+               return;
+       }
+
+       sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
        uio_unregister_device(info);
-       if (((struct rte_uio_pci_dev *)info->priv)->mode == IGBUIO_MSIX_INTR_MODE)
+       igbuio_pci_release_iomem(info);
+       if (((struct rte_uio_pci_dev *)info->priv)->mode ==
+                                       IGBUIO_MSIX_INTR_MODE)
                pci_disable_msix(dev);
        pci_release_regions(dev);
        pci_disable_device(dev);
@@ -378,6 +547,28 @@ igbuio_pci_remove(struct pci_dev *dev)
        kfree(info);
 }
 
+static int
+igbuio_config_intr_mode(char *intr_str)
+{
+       if (!intr_str) {
+               printk(KERN_INFO "Use MSIX interrupt by default\n");
+               return 0;
+       }
+
+       if (!strcmp(intr_str, "msix")) {
+               igbuio_intr_mode_preferred = IGBUIO_MSIX_INTR_MODE;
+               printk(KERN_INFO "Use MSIX interrupt\n");
+       } else if (!strcmp(intr_str, "legacy")) {
+               igbuio_intr_mode_preferred = IGBUIO_LEGACY_INTR_MODE;
+               printk(KERN_INFO "Use legacy interrupt\n");
+       } else {
+               printk(KERN_INFO "Error: bad parameter - %s\n", intr_str);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 static struct pci_driver igbuio_pci_driver = {
        .name = "igb_uio",
        .id_table = igbuio_pci_ids,
@@ -388,6 +579,12 @@ static struct pci_driver igbuio_pci_driver = {
 static int __init
 igbuio_pci_init_module(void)
 {
+       int ret;
+
+       ret = igbuio_config_intr_mode(intr_mode);
+       if (ret < 0)
+               return ret;
+
        return pci_register_driver(&igbuio_pci_driver);
 }
 
@@ -400,6 +597,13 @@ igbuio_pci_exit_module(void)
 module_init(igbuio_pci_init_module);
 module_exit(igbuio_pci_exit_module);
 
+module_param(intr_mode, charp, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(intr_mode,
+"igb_uio interrupt mode (default=msix):\n"
+"    msix       Use MSIX interrupt\n"
+"    legacy     Use Legacy interrupt\n"
+"\n");
+
 MODULE_DESCRIPTION("UIO driver for Intel IGB PCI cards");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Intel Corporation");