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