4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * The full GNU General Public License is included in this distribution
19 * in the file called LICENSE.GPL.
21 * Contact Information:
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/uio_driver.h>
32 #include <linux/msi.h>
33 #include <linux/version.h>
35 #ifdef CONFIG_XEN_DOM0
38 #include <rte_pci_dev_features.h>
41 * MSI-X related macros, copy from linux/pci_regs.h in kernel 2.6.39,
42 * but none of them in kernel 2.6.35.
44 #ifndef PCI_MSIX_ENTRY_SIZE
45 #define PCI_MSIX_ENTRY_SIZE 16
46 #define PCI_MSIX_ENTRY_LOWER_ADDR 0
47 #define PCI_MSIX_ENTRY_UPPER_ADDR 4
48 #define PCI_MSIX_ENTRY_DATA 8
49 #define PCI_MSIX_ENTRY_VECTOR_CTRL 12
50 #define PCI_MSIX_ENTRY_CTRL_MASKBIT 1
54 #define PCI_SYS_FILE_BUF_SIZE 10
55 #define PCI_DEV_CAP_REG 0xA4
56 #define PCI_DEV_CTRL_REG 0xA8
57 #define PCI_DEV_CAP_EXT_TAG_MASK 0x20
58 #define PCI_DEV_CTRL_EXT_TAG_SHIFT 8
59 #define PCI_DEV_CTRL_EXT_TAG_MASK (1 << PCI_DEV_CTRL_EXT_TAG_SHIFT)
62 #define IGBUIO_NUM_MSI_VECTORS 1
65 * A structure describing the private information for a uio device.
67 struct rte_uio_pci_dev {
70 spinlock_t lock; /* spinlock for accessing PCI config space or msix data in multi tasks/isr */
71 enum rte_intr_mode mode;
73 msix_entries[IGBUIO_NUM_MSI_VECTORS]; /* pointer to the msix vectors to be allocated later */
76 static char *intr_mode = NULL;
77 static enum rte_intr_mode igbuio_intr_mode_preferred = RTE_INTR_MODE_MSIX;
79 static inline struct rte_uio_pci_dev *
80 igbuio_get_uio_pci_dev(struct uio_info *info)
82 return container_of(info, struct rte_uio_pci_dev, info);
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
87 static int pci_num_vf(struct pci_dev *dev)
96 } *iov = (struct iov *)dev->sriov;
101 return iov->nr_virtfn;
106 show_max_vfs(struct device *dev, struct device_attribute *attr,
109 return snprintf(buf, 10, "%u\n",
110 pci_num_vf(container_of(dev, struct pci_dev, dev)));
114 store_max_vfs(struct device *dev, struct device_attribute *attr,
115 const char *buf, size_t count)
118 unsigned long max_vfs;
119 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
121 if (0 != strict_strtoul(buf, 0, &max_vfs))
125 pci_disable_sriov(pdev);
126 else if (0 == pci_num_vf(pdev))
127 err = pci_enable_sriov(pdev, max_vfs);
128 else /* do nothing if change max_vfs number */
131 return err ? err : count;
134 #ifdef RTE_PCI_CONFIG
136 show_extended_tag(struct device *dev, struct device_attribute *attr, char *buf)
138 struct pci_dev *pci_dev = container_of(dev, struct pci_dev, dev);
141 pci_read_config_dword(pci_dev, PCI_DEV_CAP_REG, &val);
142 if (!(val & PCI_DEV_CAP_EXT_TAG_MASK)) /* Not supported */
143 return snprintf(buf, PCI_SYS_FILE_BUF_SIZE, "%s\n", "invalid");
146 pci_bus_read_config_dword(pci_dev->bus, pci_dev->devfn,
147 PCI_DEV_CTRL_REG, &val);
149 return snprintf(buf, PCI_SYS_FILE_BUF_SIZE, "%s\n",
150 (val & PCI_DEV_CTRL_EXT_TAG_MASK) ? "on" : "off");
154 store_extended_tag(struct device *dev,
155 struct device_attribute *attr,
159 struct pci_dev *pci_dev = container_of(dev, struct pci_dev, dev);
160 uint32_t val = 0, enable;
162 if (strncmp(buf, "on", 2) == 0)
164 else if (strncmp(buf, "off", 3) == 0)
169 pci_bus_read_config_dword(pci_dev->bus, pci_dev->devfn,
170 PCI_DEV_CAP_REG, &val);
171 if (!(val & PCI_DEV_CAP_EXT_TAG_MASK)) /* Not supported */
175 pci_bus_read_config_dword(pci_dev->bus, pci_dev->devfn,
176 PCI_DEV_CTRL_REG, &val);
178 val |= PCI_DEV_CTRL_EXT_TAG_MASK;
180 val &= ~PCI_DEV_CTRL_EXT_TAG_MASK;
181 pci_bus_write_config_dword(pci_dev->bus, pci_dev->devfn,
182 PCI_DEV_CTRL_REG, val);
188 show_max_read_request_size(struct device *dev,
189 struct device_attribute *attr,
192 struct pci_dev *pci_dev = container_of(dev, struct pci_dev, dev);
193 int val = pcie_get_readrq(pci_dev);
195 return snprintf(buf, PCI_SYS_FILE_BUF_SIZE, "%d\n", val);
199 store_max_read_request_size(struct device *dev,
200 struct device_attribute *attr,
204 struct pci_dev *pci_dev = container_of(dev, struct pci_dev, dev);
205 unsigned long size = 0;
208 if (strict_strtoul(buf, 0, &size) != 0)
211 ret = pcie_set_readrq(pci_dev, (int)size);
219 static DEVICE_ATTR(max_vfs, S_IRUGO | S_IWUSR, show_max_vfs, store_max_vfs);
220 #ifdef RTE_PCI_CONFIG
221 static DEVICE_ATTR(extended_tag, S_IRUGO | S_IWUSR, show_extended_tag,
223 static DEVICE_ATTR(max_read_request_size, S_IRUGO | S_IWUSR,
224 show_max_read_request_size, store_max_read_request_size);
227 static struct attribute *dev_attrs[] = {
228 &dev_attr_max_vfs.attr,
229 #ifdef RTE_PCI_CONFIG
230 &dev_attr_extended_tag.attr,
231 &dev_attr_max_read_request_size.attr,
236 static const struct attribute_group dev_attr_grp = {
241 pci_lock(struct pci_dev * pdev)
243 /* Some function names changes between 3.2.0 and 3.3.0... */
244 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
245 pci_block_user_cfg_access(pdev);
248 return pci_cfg_access_trylock(pdev);
253 pci_unlock(struct pci_dev * pdev)
255 /* Some function names changes between 3.2.0 and 3.3.0... */
256 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
257 pci_unblock_user_cfg_access(pdev);
259 pci_cfg_access_unlock(pdev);
264 * It masks the msix on/off of generating MSI-X messages.
267 igbuio_msix_mask_irq(struct msi_desc *desc, int32_t state)
269 uint32_t mask_bits = desc->masked;
270 unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
271 PCI_MSIX_ENTRY_VECTOR_CTRL;
274 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
276 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
278 if (mask_bits != desc->masked) {
279 writel(mask_bits, desc->mask_base + offset);
280 readl(desc->mask_base);
281 desc->masked = mask_bits;
288 * This function sets/clears the masks for generating LSC interrupts.
291 * The pointer to struct uio_info.
293 * The on/off flag of masking LSC.
295 * -On success, zero value.
296 * -On failure, a negative value.
299 igbuio_set_interrupt_mask(struct rte_uio_pci_dev *udev, int32_t state)
301 struct pci_dev *pdev = udev->pdev;
303 if (udev->mode == RTE_INTR_MODE_MSIX) {
304 struct msi_desc *desc;
306 list_for_each_entry(desc, &pdev->msi_list, list) {
307 igbuio_msix_mask_irq(desc, state);
309 } else if (udev->mode == RTE_INTR_MODE_LEGACY) {
313 pci_read_config_dword(pdev, PCI_COMMAND, &status);
316 new = old & (~PCI_COMMAND_INTX_DISABLE);
318 new = old | PCI_COMMAND_INTX_DISABLE;
321 pci_write_config_word(pdev, PCI_COMMAND, new);
328 * This is the irqcontrol callback to be registered to uio_info.
329 * It can be used to disable/enable interrupt from user space processes.
332 * pointer to uio_info.
334 * state value. 1 to enable interrupt, 0 to disable interrupt.
338 * - On failure, a negative value.
341 igbuio_pci_irqcontrol(struct uio_info *info, s32 irq_state)
344 struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
345 struct pci_dev *pdev = udev->pdev;
347 spin_lock_irqsave(&udev->lock, flags);
348 if (!pci_lock(pdev)) {
349 spin_unlock_irqrestore(&udev->lock, flags);
353 igbuio_set_interrupt_mask(udev, irq_state);
356 spin_unlock_irqrestore(&udev->lock, flags);
362 * This is interrupt handler which will check if the interrupt is for the right device.
363 * If yes, disable it here and will be enable later.
366 igbuio_pci_irqhandler(int irq, struct uio_info *info)
368 irqreturn_t ret = IRQ_NONE;
370 struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
371 struct pci_dev *pdev = udev->pdev;
372 uint32_t cmd_status_dword;
375 spin_lock_irqsave(&udev->lock, flags);
376 /* block userspace PCI config reads/writes */
380 /* for legacy mode, interrupt maybe shared */
381 if (udev->mode == RTE_INTR_MODE_LEGACY) {
382 pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
383 status = cmd_status_dword >> 16;
384 /* interrupt is not ours, goes to out */
385 if (!(status & PCI_STATUS_INTERRUPT))
389 igbuio_set_interrupt_mask(udev, 0);
392 /* unblock userspace PCI config reads/writes */
395 spin_unlock_irqrestore(&udev->lock, flags);
396 pr_info("irq 0x%x %s\n", irq, (ret == IRQ_HANDLED) ? "handled" : "not handled");
401 #ifdef CONFIG_XEN_DOM0
403 igbuio_dom0_mmap_phys(struct uio_info *info, struct vm_area_struct *vma)
407 idx = (int)vma->vm_pgoff;
408 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
409 vma->vm_page_prot.pgprot |= _PAGE_IOMAP;
411 return remap_pfn_range(vma,
413 info->mem[idx].addr >> PAGE_SHIFT,
414 vma->vm_end - vma->vm_start,
419 * This is uio device mmap method which will use igbuio mmap for Xen
423 igbuio_dom0_pci_mmap(struct uio_info *info, struct vm_area_struct *vma)
427 if (vma->vm_pgoff >= MAX_UIO_MAPS)
430 if (info->mem[vma->vm_pgoff].size == 0)
433 idx = (int)vma->vm_pgoff;
434 switch (info->mem[idx].memtype) {
436 return igbuio_dom0_mmap_phys(info, vma);
437 case UIO_MEM_LOGICAL:
438 case UIO_MEM_VIRTUAL:
445 /* Remap pci resources described by bar #pci_bar in uio resource n. */
447 igbuio_pci_setup_iomem(struct pci_dev *dev, struct uio_info *info,
448 int n, int pci_bar, const char *name)
450 unsigned long addr, len;
453 if (sizeof(info->mem) / sizeof(info->mem[0]) <= n)
456 addr = pci_resource_start(dev, pci_bar);
457 len = pci_resource_len(dev, pci_bar);
458 if (addr == 0 || len == 0)
460 internal_addr = ioremap(addr, len);
461 if (internal_addr == NULL)
463 info->mem[n].name = name;
464 info->mem[n].addr = addr;
465 info->mem[n].internal_addr = internal_addr;
466 info->mem[n].size = len;
467 info->mem[n].memtype = UIO_MEM_PHYS;
471 /* Get pci port io resources described by bar #pci_bar in uio resource n. */
473 igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,
474 int n, int pci_bar, const char *name)
476 unsigned long addr, len;
478 if (sizeof(info->port) / sizeof(info->port[0]) <= n)
481 addr = pci_resource_start(dev, pci_bar);
482 len = pci_resource_len(dev, pci_bar);
483 if (addr == 0 || len == 0)
486 info->port[n].name = name;
487 info->port[n].start = addr;
488 info->port[n].size = len;
489 info->port[n].porttype = UIO_PORT_X86;
494 /* Unmap previously ioremap'd resources */
496 igbuio_pci_release_iomem(struct uio_info *info)
500 for (i = 0; i < MAX_UIO_MAPS; i++) {
501 if (info->mem[i].internal_addr)
502 iounmap(info->mem[i].internal_addr);
507 igbuio_setup_bars(struct pci_dev *dev, struct uio_info *info)
509 int i, iom, iop, ret;
511 static const char *bar_names[PCI_STD_RESOURCE_END + 1] = {
523 for (i = 0; i != sizeof(bar_names) / sizeof(bar_names[0]); i++) {
524 if (pci_resource_len(dev, i) != 0 &&
525 pci_resource_start(dev, i) != 0) {
526 flags = pci_resource_flags(dev, i);
527 if (flags & IORESOURCE_MEM) {
528 ret = igbuio_pci_setup_iomem(dev, info, iom,
533 } else if (flags & IORESOURCE_IO) {
534 ret = igbuio_pci_setup_ioport(dev, info, iop,
543 return ((iom != 0) ? ret : ENOENT);
546 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
551 igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
553 struct rte_uio_pci_dev *udev;
555 udev = kzalloc(sizeof(struct rte_uio_pci_dev), GFP_KERNEL);
560 * enable device: ask low-level code to enable I/O and
563 if (pci_enable_device(dev)) {
564 dev_err(&dev->dev, "Cannot enable PCI device\n");
569 * reserve device's PCI memory regions for use by this
572 if (pci_request_regions(dev, "igb_uio")) {
573 dev_err(&dev->dev, "Cannot request regions\n");
577 /* enable bus mastering on the device */
580 /* remap IO memory */
581 if (igbuio_setup_bars(dev, &udev->info))
582 goto fail_release_iomem;
584 /* set 64-bit DMA mask */
585 if (pci_set_dma_mask(dev, DMA_BIT_MASK(64))) {
586 dev_err(&dev->dev, "Cannot set DMA mask\n");
587 goto fail_release_iomem;
588 } else if (pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(64))) {
589 dev_err(&dev->dev, "Cannot set consistent DMA mask\n");
590 goto fail_release_iomem;
594 udev->info.name = "igb_uio";
595 udev->info.version = "0.1";
596 udev->info.handler = igbuio_pci_irqhandler;
597 udev->info.irqcontrol = igbuio_pci_irqcontrol;
598 #ifdef CONFIG_XEN_DOM0
599 /* check if the driver run on Xen Dom0 */
600 if (xen_initial_domain())
601 udev->info.mmap = igbuio_dom0_pci_mmap;
603 udev->info.priv = udev;
605 udev->mode = RTE_INTR_MODE_LEGACY;
606 spin_lock_init(&udev->lock);
608 /* check if it need to try msix first */
609 if (igbuio_intr_mode_preferred == RTE_INTR_MODE_MSIX) {
612 for (vector = 0; vector < IGBUIO_NUM_MSI_VECTORS; vector ++)
613 udev->msix_entries[vector].entry = vector;
615 if (pci_enable_msix(udev->pdev, udev->msix_entries, IGBUIO_NUM_MSI_VECTORS) == 0) {
616 udev->mode = RTE_INTR_MODE_MSIX;
619 pci_disable_msix(udev->pdev);
620 pr_info("fail to enable pci msix, or not enough msix entries\n");
623 switch (udev->mode) {
624 case RTE_INTR_MODE_MSIX:
625 udev->info.irq_flags = 0;
626 udev->info.irq = udev->msix_entries[0].vector;
628 case RTE_INTR_MODE_MSI:
630 case RTE_INTR_MODE_LEGACY:
631 udev->info.irq_flags = IRQF_SHARED;
632 udev->info.irq = dev->irq;
638 pci_set_drvdata(dev, udev);
639 igbuio_pci_irqcontrol(&udev->info, 0);
641 if (sysfs_create_group(&dev->dev.kobj, &dev_attr_grp))
642 goto fail_release_iomem;
644 /* register uio driver */
645 if (uio_register_device(&dev->dev, &udev->info))
646 goto fail_release_iomem;
648 pr_info("uio device registered with irq %lx\n", udev->info.irq);
653 sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
654 igbuio_pci_release_iomem(&udev->info);
655 if (udev->mode == RTE_INTR_MODE_MSIX)
656 pci_disable_msix(udev->pdev);
657 pci_release_regions(dev);
659 pci_disable_device(dev);
667 igbuio_pci_remove(struct pci_dev *dev)
669 struct uio_info *info = pci_get_drvdata(dev);
671 if (info->priv == NULL) {
672 pr_notice("Not igbuio device\n");
676 sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
677 uio_unregister_device(info);
678 igbuio_pci_release_iomem(info);
679 if (((struct rte_uio_pci_dev *)info->priv)->mode ==
681 pci_disable_msix(dev);
682 pci_release_regions(dev);
683 pci_disable_device(dev);
684 pci_set_drvdata(dev, NULL);
689 igbuio_config_intr_mode(char *intr_str)
692 pr_info("Use MSIX interrupt by default\n");
696 if (!strcmp(intr_str, RTE_INTR_MODE_MSIX_NAME)) {
697 igbuio_intr_mode_preferred = RTE_INTR_MODE_MSIX;
698 pr_info("Use MSIX interrupt\n");
699 } else if (!strcmp(intr_str, RTE_INTR_MODE_LEGACY_NAME)) {
700 igbuio_intr_mode_preferred = RTE_INTR_MODE_LEGACY;
701 pr_info("Use legacy interrupt\n");
703 pr_info("Error: bad parameter - %s\n", intr_str);
710 static struct pci_driver igbuio_pci_driver = {
713 .probe = igbuio_pci_probe,
714 .remove = igbuio_pci_remove,
718 igbuio_pci_init_module(void)
722 ret = igbuio_config_intr_mode(intr_mode);
726 return pci_register_driver(&igbuio_pci_driver);
730 igbuio_pci_exit_module(void)
732 pci_unregister_driver(&igbuio_pci_driver);
735 module_init(igbuio_pci_init_module);
736 module_exit(igbuio_pci_exit_module);
738 module_param(intr_mode, charp, S_IRUGO | S_IWUSR);
739 MODULE_PARM_DESC(intr_mode,
740 "igb_uio interrupt mode (default=msix):\n"
741 " " RTE_INTR_MODE_MSIX_NAME " Use MSIX interrupt\n"
742 " " RTE_INTR_MODE_LEGACY_NAME " Use Legacy interrupt\n"
745 MODULE_DESCRIPTION("UIO driver for Intel IGB PCI cards");
746 MODULE_LICENSE("GPL");
747 MODULE_AUTHOR("Intel Corporation");