eal: move interrupt type out of igb_uio
[dpdk.git] / lib / librte_eal / linuxapp / igb_uio / igb_uio.c
1 /*-
2  * GPL LICENSE SUMMARY
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *
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.
9  *
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.
14  *
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.
20  *
21  *   Contact Information:
22  *   Intel Corporation
23  */
24
25 #include <linux/device.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/uio_driver.h>
29 #include <linux/io.h>
30 #include <linux/msi.h>
31 #include <linux/version.h>
32
33 #ifdef CONFIG_XEN_DOM0
34 #include <xen/xen.h>
35 #endif
36 #include <rte_pci_dev_features.h>
37
38 /**
39  * MSI-X related macros, copy from linux/pci_regs.h in kernel 2.6.39,
40  * but none of them in kernel 2.6.35.
41  */
42 #ifndef PCI_MSIX_ENTRY_SIZE
43 #define PCI_MSIX_ENTRY_SIZE             16
44 #define PCI_MSIX_ENTRY_LOWER_ADDR       0
45 #define PCI_MSIX_ENTRY_UPPER_ADDR       4
46 #define PCI_MSIX_ENTRY_DATA             8
47 #define PCI_MSIX_ENTRY_VECTOR_CTRL      12
48 #define PCI_MSIX_ENTRY_CTRL_MASKBIT     1
49 #endif
50
51 #define IGBUIO_NUM_MSI_VECTORS 1
52
53 /**
54  * A structure describing the private information for a uio device.
55  */
56 struct rte_uio_pci_dev {
57         struct uio_info info;
58         struct pci_dev *pdev;
59         spinlock_t lock; /* spinlock for accessing PCI config space or msix data in multi tasks/isr */
60         enum rte_intr_mode mode;
61         struct msix_entry \
62                 msix_entries[IGBUIO_NUM_MSI_VECTORS]; /* pointer to the msix vectors to be allocated later */
63 };
64
65 static char *intr_mode = NULL;
66 static enum rte_intr_mode igbuio_intr_mode_preferred = RTE_INTR_MODE_MSIX;
67
68 /* PCI device id table */
69 static struct pci_device_id igbuio_pci_ids[] = {
70 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {PCI_DEVICE(vend, dev)},
71 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {PCI_DEVICE(vend, dev)},
72 #define RTE_PCI_DEV_ID_DECL_IGBVF(vend, dev) {PCI_DEVICE(vend, dev)},
73 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {PCI_DEVICE(vend, dev)},
74 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {PCI_DEVICE(vend, dev)},
75 #ifdef RTE_LIBRTE_VIRTIO_PMD
76 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {PCI_DEVICE(vend, dev)},
77 #endif
78 #ifdef RTE_LIBRTE_VMXNET3_PMD
79 #define RTE_PCI_DEV_ID_DECL_VMXNET3(vend, dev) {PCI_DEVICE(vend, dev)},
80 #endif
81 #include <rte_pci_dev_ids.h>
82 { 0, },
83 };
84
85 MODULE_DEVICE_TABLE(pci, igbuio_pci_ids);
86
87 static inline struct rte_uio_pci_dev *
88 igbuio_get_uio_pci_dev(struct uio_info *info)
89 {
90         return container_of(info, struct rte_uio_pci_dev, info);
91 }
92
93 /* sriov sysfs */
94 int local_pci_num_vf(struct pci_dev *dev)
95 {
96 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
97         struct iov {
98                 int pos;
99                 int nres;
100                 u32 cap;
101                 u16 ctrl;
102                 u16 total;
103                 u16 initial;
104                 u16 nr_virtfn;
105         } *iov = (struct iov*)dev->sriov;
106
107         if (!dev->is_physfn)
108                 return 0;
109
110         return iov->nr_virtfn;
111 #else
112         return pci_num_vf(dev);
113 #endif
114 }
115
116 static ssize_t
117 show_max_vfs(struct device *dev, struct device_attribute *attr,
118              char *buf)
119 {
120         return snprintf(buf, 10, "%u\n", local_pci_num_vf(
121                                 container_of(dev, struct pci_dev, dev)));
122 }
123
124 static ssize_t
125 store_max_vfs(struct device *dev, struct device_attribute *attr,
126               const char *buf, size_t count)
127 {
128         int err = 0;
129         unsigned long max_vfs;
130         struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
131
132         if (0 != strict_strtoul(buf, 0, &max_vfs))
133                 return -EINVAL;
134
135         if (0 == max_vfs)
136                 pci_disable_sriov(pdev);
137         else if (0 == local_pci_num_vf(pdev))
138                 err = pci_enable_sriov(pdev, max_vfs);
139         else /* do nothing if change max_vfs number */
140                 err = -EINVAL;
141
142         return err ? err : count;
143 }
144
145 static DEVICE_ATTR(max_vfs, S_IRUGO | S_IWUSR, show_max_vfs, store_max_vfs);
146 static struct attribute *dev_attrs[] = {
147         &dev_attr_max_vfs.attr,
148         NULL,
149 };
150
151 static const struct attribute_group dev_attr_grp = {
152         .attrs = dev_attrs,
153 };
154
155 static inline int
156 pci_lock(struct pci_dev * pdev)
157 {
158         /* Some function names changes between 3.2.0 and 3.3.0... */
159 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
160         pci_block_user_cfg_access(pdev);
161         return 1;
162 #else
163         return pci_cfg_access_trylock(pdev);
164 #endif
165 }
166
167 static inline void
168 pci_unlock(struct pci_dev * pdev)
169 {
170         /* Some function names changes between 3.2.0 and 3.3.0... */
171 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
172         pci_unblock_user_cfg_access(pdev);
173 #else
174         pci_cfg_access_unlock(pdev);
175 #endif
176 }
177
178 /**
179  * It masks the msix on/off of generating MSI-X messages.
180  */
181 static int
182 igbuio_msix_mask_irq(struct msi_desc *desc, int32_t state)
183 {
184         uint32_t mask_bits = desc->masked;
185         unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
186                                                 PCI_MSIX_ENTRY_VECTOR_CTRL;
187
188         if (state != 0)
189                 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
190         else
191                 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
192
193         if (mask_bits != desc->masked) {
194                 writel(mask_bits, desc->mask_base + offset);
195                 readl(desc->mask_base);
196                 desc->masked = mask_bits;
197         }
198
199         return 0;
200 }
201
202 /**
203  * This function sets/clears the masks for generating LSC interrupts.
204  *
205  * @param info
206  *   The pointer to struct uio_info.
207  * @param on
208  *   The on/off flag of masking LSC.
209  * @return
210  *   -On success, zero value.
211  *   -On failure, a negative value.
212  */
213 static int
214 igbuio_set_interrupt_mask(struct rte_uio_pci_dev *udev, int32_t state)
215 {
216         struct pci_dev *pdev = udev->pdev;
217
218         if (udev->mode == RTE_INTR_MODE_MSIX) {
219                 struct msi_desc *desc;
220
221                 list_for_each_entry(desc, &pdev->msi_list, list) {
222                         igbuio_msix_mask_irq(desc, state);
223                 }
224         } else if (udev->mode == RTE_INTR_MODE_LEGACY) {
225                 uint32_t status;
226                 uint16_t old, new;
227
228                 pci_read_config_dword(pdev, PCI_COMMAND, &status);
229                 old = status;
230                 if (state != 0)
231                         new = old & (~PCI_COMMAND_INTX_DISABLE);
232                 else
233                         new = old | PCI_COMMAND_INTX_DISABLE;
234
235                 if (old != new)
236                         pci_write_config_word(pdev, PCI_COMMAND, new);
237         }
238
239         return 0;
240 }
241
242 /**
243  * This is the irqcontrol callback to be registered to uio_info.
244  * It can be used to disable/enable interrupt from user space processes.
245  *
246  * @param info
247  *  pointer to uio_info.
248  * @param irq_state
249  *  state value. 1 to enable interrupt, 0 to disable interrupt.
250  *
251  * @return
252  *  - On success, 0.
253  *  - On failure, a negative value.
254  */
255 static int
256 igbuio_pci_irqcontrol(struct uio_info *info, s32 irq_state)
257 {
258         unsigned long flags;
259         struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
260         struct pci_dev *pdev = udev->pdev;
261
262         spin_lock_irqsave(&udev->lock, flags);
263         if (!pci_lock(pdev)) {
264                 spin_unlock_irqrestore(&udev->lock, flags);
265                 return -1;
266         }
267
268         igbuio_set_interrupt_mask(udev, irq_state);
269
270         pci_unlock(pdev);
271         spin_unlock_irqrestore(&udev->lock, flags);
272
273         return 0;
274 }
275
276 /**
277  * This is interrupt handler which will check if the interrupt is for the right device.
278  * If yes, disable it here and will be enable later.
279  */
280 static irqreturn_t
281 igbuio_pci_irqhandler(int irq, struct uio_info *info)
282 {
283         irqreturn_t ret = IRQ_NONE;
284         unsigned long flags;
285         struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
286         struct pci_dev *pdev = udev->pdev;
287         uint32_t cmd_status_dword;
288         uint16_t status;
289
290         spin_lock_irqsave(&udev->lock, flags);
291         /* block userspace PCI config reads/writes */
292         if (!pci_lock(pdev))
293                 goto spin_unlock;
294
295         /* for legacy mode, interrupt maybe shared */
296         if (udev->mode == RTE_INTR_MODE_LEGACY) {
297                 pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
298                 status = cmd_status_dword >> 16;
299                 /* interrupt is not ours, goes to out */
300                 if (!(status & PCI_STATUS_INTERRUPT))
301                         goto done;
302         }
303
304         igbuio_set_interrupt_mask(udev, 0);
305         ret = IRQ_HANDLED;
306 done:
307         /* unblock userspace PCI config reads/writes */
308         pci_unlock(pdev);
309 spin_unlock:
310         spin_unlock_irqrestore(&udev->lock, flags);
311         printk(KERN_INFO "irq 0x%x %s\n", irq, (ret == IRQ_HANDLED) ? "handled" : "not handled");
312
313         return ret;
314 }
315
316 #ifdef CONFIG_XEN_DOM0
317 static int
318 igbuio_dom0_mmap_phys(struct uio_info *info, struct vm_area_struct *vma)
319 {
320         int idx;
321         idx = (int)vma->vm_pgoff;
322         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
323         vma->vm_page_prot.pgprot |= _PAGE_IOMAP;
324
325         return remap_pfn_range(vma,
326                         vma->vm_start,
327                         info->mem[idx].addr >> PAGE_SHIFT,
328                         vma->vm_end - vma->vm_start,
329                         vma->vm_page_prot);
330 }
331
332 /**
333  * This is uio device mmap method which will use igbuio mmap for Xen
334  * Dom0 environment.
335  */
336 static int
337 igbuio_dom0_pci_mmap(struct uio_info *info, struct vm_area_struct *vma)
338 {
339         int idx;
340
341         if (vma->vm_pgoff >= MAX_UIO_MAPS)
342                 return -EINVAL;
343         if(info->mem[vma->vm_pgoff].size == 0)
344                 return  -EINVAL;
345
346         idx = (int)vma->vm_pgoff;
347         switch (info->mem[idx].memtype) {
348         case UIO_MEM_PHYS:
349                 return igbuio_dom0_mmap_phys(info, vma);
350         case UIO_MEM_LOGICAL:
351         case UIO_MEM_VIRTUAL:
352         default:
353                 return -EINVAL;
354         }
355 }
356 #endif
357
358 /* Remap pci resources described by bar #pci_bar in uio resource n. */
359 static int
360 igbuio_pci_setup_iomem(struct pci_dev *dev, struct uio_info *info,
361                        int n, int pci_bar, const char *name)
362 {
363         unsigned long addr, len;
364         void *internal_addr;
365
366         if (sizeof(info->mem) / sizeof (info->mem[0]) <= n)
367                 return (EINVAL);
368
369         addr = pci_resource_start(dev, pci_bar);
370         len = pci_resource_len(dev, pci_bar);
371         if (addr == 0 || len == 0)
372                 return -1;
373         internal_addr = ioremap(addr, len);
374         if (internal_addr == NULL)
375                 return -1;
376         info->mem[n].name = name;
377         info->mem[n].addr = addr;
378         info->mem[n].internal_addr = internal_addr;
379         info->mem[n].size = len;
380         info->mem[n].memtype = UIO_MEM_PHYS;
381         return 0;
382 }
383
384 /* Get pci port io resources described by bar #pci_bar in uio resource n. */
385 static int
386 igbuio_pci_setup_ioport(struct pci_dev *dev, struct uio_info *info,
387                 int n, int pci_bar, const char *name)
388 {
389         unsigned long addr, len;
390
391         if (sizeof(info->port) / sizeof (info->port[0]) <= n)
392                 return (EINVAL);
393
394         addr = pci_resource_start(dev, pci_bar);
395         len = pci_resource_len(dev, pci_bar);
396         if (addr == 0 || len == 0)
397                 return (-1);
398
399         info->port[n].name = name;
400         info->port[n].start = addr;
401         info->port[n].size = len;
402         info->port[n].porttype = UIO_PORT_X86;
403
404         return (0);
405 }
406
407 /* Unmap previously ioremap'd resources */
408 static void
409 igbuio_pci_release_iomem(struct uio_info *info)
410 {
411         int i;
412         for (i = 0; i < MAX_UIO_MAPS; i++) {
413                 if (info->mem[i].internal_addr)
414                         iounmap(info->mem[i].internal_addr);
415         }
416 }
417
418 static int
419 igbuio_setup_bars(struct pci_dev *dev, struct uio_info *info)
420 {
421         int i, iom, iop, ret;
422         unsigned long flags;
423         static const char *bar_names[PCI_STD_RESOURCE_END + 1]  = {
424                 "BAR0",
425                 "BAR1",
426                 "BAR2",
427                 "BAR3",
428                 "BAR4",
429                 "BAR5",
430         };
431
432         iom = 0;
433         iop = 0;
434
435         for (i = 0; i != sizeof(bar_names) / sizeof(bar_names[0]); i++) {
436                 if (pci_resource_len(dev, i) != 0 &&
437                                 pci_resource_start(dev, i) != 0) {
438                         flags = pci_resource_flags(dev, i);
439                         if (flags & IORESOURCE_MEM) {
440                                 if ((ret = igbuio_pci_setup_iomem(dev, info,
441                                                 iom, i, bar_names[i])) != 0)
442                                         return (ret);
443                                 iom++;
444                         } else if (flags & IORESOURCE_IO) {
445                                 if ((ret = igbuio_pci_setup_ioport(dev, info,
446                                                 iop, i, bar_names[i])) != 0)
447                                         return (ret);
448                                 iop++;
449                         }
450                 }
451         }
452
453         return ((iom != 0) ? ret : ENOENT);
454 }
455
456 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
457 static int __devinit
458 #else
459 static int
460 #endif
461 igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
462 {
463         struct rte_uio_pci_dev *udev;
464
465         udev = kzalloc(sizeof(struct rte_uio_pci_dev), GFP_KERNEL);
466         if (!udev)
467                 return -ENOMEM;
468
469         /*
470          * enable device: ask low-level code to enable I/O and
471          * memory
472          */
473         if (pci_enable_device(dev)) {
474                 printk(KERN_ERR "Cannot enable PCI device\n");
475                 goto fail_free;
476         }
477
478         /*
479          * reserve device's PCI memory regions for use by this
480          * module
481          */
482         if (pci_request_regions(dev, "igb_uio")) {
483                 printk(KERN_ERR "Cannot request regions\n");
484                 goto fail_disable;
485         }
486
487         /* enable bus mastering on the device */
488         pci_set_master(dev);
489
490         /* remap IO memory */
491         if (igbuio_setup_bars(dev, &udev->info))
492                 goto fail_release_iomem;
493
494         /* set 64-bit DMA mask */
495         if (pci_set_dma_mask(dev,  DMA_BIT_MASK(64))) {
496                 printk(KERN_ERR "Cannot set DMA mask\n");
497                 goto fail_release_iomem;
498         } else if (pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(64))) {
499                 printk(KERN_ERR "Cannot set consistent DMA mask\n");
500                 goto fail_release_iomem;
501         }
502
503         /* fill uio infos */
504         udev->info.name = "Intel IGB UIO";
505         udev->info.version = "0.1";
506         udev->info.handler = igbuio_pci_irqhandler;
507         udev->info.irqcontrol = igbuio_pci_irqcontrol;
508 #ifdef CONFIG_XEN_DOM0
509         /* check if the driver run on Xen Dom0 */
510         if (xen_initial_domain())
511                 udev->info.mmap = igbuio_dom0_pci_mmap;
512 #endif
513         udev->info.priv = udev;
514         udev->pdev = dev;
515         udev->mode = RTE_INTR_MODE_LEGACY;
516         spin_lock_init(&udev->lock);
517
518         /* check if it need to try msix first */
519         if (igbuio_intr_mode_preferred == RTE_INTR_MODE_MSIX) {
520                 int vector;
521
522                 for (vector = 0; vector < IGBUIO_NUM_MSI_VECTORS; vector ++)
523                         udev->msix_entries[vector].entry = vector;
524
525                 if (pci_enable_msix(udev->pdev, udev->msix_entries, IGBUIO_NUM_MSI_VECTORS) == 0) {
526                         udev->mode = RTE_INTR_MODE_MSIX;
527                 }
528                 else {
529                         pci_disable_msix(udev->pdev);
530                         printk(KERN_INFO "fail to enable pci msix, or not enough msix entries\n");
531                 }
532         }
533         switch (udev->mode) {
534         case RTE_INTR_MODE_MSIX:
535                 udev->info.irq_flags = 0;
536                 udev->info.irq = udev->msix_entries[0].vector;
537                 break;
538         case RTE_INTR_MODE_MSI:
539                 break;
540         case RTE_INTR_MODE_LEGACY:
541                 udev->info.irq_flags = IRQF_SHARED;
542                 udev->info.irq = dev->irq;
543                 break;
544         default:
545                 break;
546         }
547
548         pci_set_drvdata(dev, udev);
549         igbuio_pci_irqcontrol(&udev->info, 0);
550
551         if (sysfs_create_group(&dev->dev.kobj, &dev_attr_grp))
552                 goto fail_release_iomem;
553
554         /* register uio driver */
555         if (uio_register_device(&dev->dev, &udev->info))
556                 goto fail_release_iomem;
557
558         printk(KERN_INFO "uio device registered with irq %lx\n", udev->info.irq);
559
560         return 0;
561
562 fail_release_iomem:
563         sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
564         igbuio_pci_release_iomem(&udev->info);
565         if (udev->mode == RTE_INTR_MODE_MSIX)
566                 pci_disable_msix(udev->pdev);
567         pci_release_regions(dev);
568 fail_disable:
569         pci_disable_device(dev);
570 fail_free:
571         kfree(udev);
572
573         return -ENODEV;
574 }
575
576 static void
577 igbuio_pci_remove(struct pci_dev *dev)
578 {
579         struct uio_info *info = pci_get_drvdata(dev);
580
581         if (info->priv == NULL) {
582                 printk(KERN_DEBUG "Not igbuio device\n");
583                 return;
584         }
585
586         sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
587         uio_unregister_device(info);
588         igbuio_pci_release_iomem(info);
589         if (((struct rte_uio_pci_dev *)info->priv)->mode ==
590                         RTE_INTR_MODE_MSIX)
591                 pci_disable_msix(dev);
592         pci_release_regions(dev);
593         pci_disable_device(dev);
594         pci_set_drvdata(dev, NULL);
595         kfree(info);
596 }
597
598 static int
599 igbuio_config_intr_mode(char *intr_str)
600 {
601         if (!intr_str) {
602                 printk(KERN_INFO "Use MSIX interrupt by default\n");
603                 return 0;
604         }
605
606         if (!strcmp(intr_str, RTE_INTR_MODE_MSIX_NAME)) {
607                 igbuio_intr_mode_preferred = RTE_INTR_MODE_MSIX;
608                 printk(KERN_INFO "Use MSIX interrupt\n");
609         } else if (!strcmp(intr_str, RTE_INTR_MODE_LEGACY_NAME)) {
610                 igbuio_intr_mode_preferred = RTE_INTR_MODE_LEGACY;
611                 printk(KERN_INFO "Use legacy interrupt\n");
612         } else {
613                 printk(KERN_INFO "Error: bad parameter - %s\n", intr_str);
614                 return -EINVAL;
615         }
616
617         return 0;
618 }
619
620 static struct pci_driver igbuio_pci_driver = {
621         .name = "igb_uio",
622         .id_table = igbuio_pci_ids,
623         .probe = igbuio_pci_probe,
624         .remove = igbuio_pci_remove,
625 };
626
627 static int __init
628 igbuio_pci_init_module(void)
629 {
630         int ret;
631
632         ret = igbuio_config_intr_mode(intr_mode);
633         if (ret < 0)
634                 return ret;
635
636         return pci_register_driver(&igbuio_pci_driver);
637 }
638
639 static void __exit
640 igbuio_pci_exit_module(void)
641 {
642         pci_unregister_driver(&igbuio_pci_driver);
643 }
644
645 module_init(igbuio_pci_init_module);
646 module_exit(igbuio_pci_exit_module);
647
648 module_param(intr_mode, charp, S_IRUGO | S_IWUSR);
649 MODULE_PARM_DESC(intr_mode,
650 "igb_uio interrupt mode (default=msix):\n"
651 "    " RTE_INTR_MODE_MSIX_NAME "       Use MSIX interrupt\n"
652 "    " RTE_INTR_MODE_LEGACY_NAME "     Use Legacy interrupt\n"
653 "\n");
654
655 MODULE_DESCRIPTION("UIO driver for Intel IGB PCI cards");
656 MODULE_LICENSE("GPL");
657 MODULE_AUTHOR("Intel Corporation");