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