update copyright date to 2013
[dpdk.git] / lib / librte_eal / linuxapp / igb_uio / igb_uio.c
index 51733f6..4774c7f 100644 (file)
@@ -1,23 +1,26 @@
 /*-
- *
- * Copyright (c) 2010-2012, Intel Corporation
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * 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
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- * GNU GPL V2: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- *
+ * GPL LICENSE SUMMARY
+ * 
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
+ * 
+ *   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 
+ *   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 
+ *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *   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.
@@ -72,21 +66,51 @@ 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)},
 #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);
 }
 
+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.
  */
@@ -173,11 +197,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;
@@ -199,7 +226,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) {
@@ -214,7 +242,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");
 
@@ -255,7 +284,11 @@ igbuio_pci_release_iomem(struct uio_info *info)
        }
 }
 
+#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;
@@ -366,8 +399,15 @@ 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;
+       }
+
        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);
@@ -375,6 +415,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,
@@ -385,6 +447,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);
 }
 
@@ -397,6 +465,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");