igb_uio: various updates
[dpdk.git] / lib / librte_eal / linuxapp / igb_uio / igb_uio.c
1 /*-
2  * GPL LICENSE SUMMARY
3  * 
4  *   Copyright(c) 2010-2012 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
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/uio_driver.h>
30 #include <linux/io.h>
31 #include <linux/msi.h>
32 #include <linux/version.h>
33
34 /**
35  * MSI-X related macros, copy from linux/pci_regs.h in kernel 2.6.39,
36  * but none of them in kernel 2.6.35.
37  */
38 #ifndef PCI_MSIX_ENTRY_SIZE
39 #define PCI_MSIX_ENTRY_SIZE             16
40 #define PCI_MSIX_ENTRY_LOWER_ADDR       0
41 #define PCI_MSIX_ENTRY_UPPER_ADDR       4
42 #define PCI_MSIX_ENTRY_DATA             8
43 #define PCI_MSIX_ENTRY_VECTOR_CTRL      12
44 #define PCI_MSIX_ENTRY_CTRL_MASKBIT     1
45 #endif
46
47 #define IGBUIO_NUM_MSI_VECTORS 1
48
49 /* interrupt mode */
50 enum igbuio_intr_mode {
51         IGBUIO_LEGACY_INTR_MODE = 0,
52         IGBUIO_MSI_INTR_MODE,
53         IGBUIO_MSIX_INTR_MODE,
54         IGBUIO_INTR_MODE_MAX
55 };
56
57 /**
58  * A structure describing the private information for a uio device.
59  */
60 struct rte_uio_pci_dev {
61         struct uio_info info;
62         struct pci_dev *pdev;
63         spinlock_t lock; /* spinlock for accessing PCI config space or msix data in multi tasks/isr */
64         enum igbuio_intr_mode mode;
65         struct msix_entry \
66                 msix_entries[IGBUIO_NUM_MSI_VECTORS]; /* pointer to the msix vectors to be allocated later */
67 };
68
69 static const 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 static inline int
91 pci_lock(struct pci_dev * pdev)
92 {
93         /* Some function names changes between 3.2.0 and 3.3.0... */
94 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
95         pci_block_user_cfg_access(pdev);
96         return 1;
97 #else
98         return pci_cfg_access_trylock(pdev);
99 #endif
100 }
101
102 static inline void
103 pci_unlock(struct pci_dev * pdev)
104 {
105         /* Some function names changes between 3.2.0 and 3.3.0... */
106 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
107         pci_unblock_user_cfg_access(pdev);
108 #else
109         pci_cfg_access_unlock(pdev);
110 #endif
111 }
112
113 /**
114  * It masks the msix on/off of generating MSI-X messages.
115  */
116 static int
117 igbuio_msix_mask_irq(struct msi_desc *desc, int32_t state)
118 {
119         uint32_t mask_bits = desc->masked;
120         unsigned offset = desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
121                                                 PCI_MSIX_ENTRY_VECTOR_CTRL;
122
123         if (state != 0)
124                 mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT;
125         else
126                 mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT;
127
128         if (mask_bits != desc->masked) {
129                 writel(mask_bits, desc->mask_base + offset);
130                 readl(desc->mask_base);
131                 desc->masked = mask_bits;
132         }
133
134         return 0;
135 }
136
137 /**
138  * This function sets/clears the masks for generating LSC interrupts.
139  *
140  * @param info
141  *   The pointer to struct uio_info.
142  * @param on
143  *   The on/off flag of masking LSC.
144  * @return
145  *   -On success, zero value.
146  *   -On failure, a negative value.
147  */
148 static int
149 igbuio_set_interrupt_mask(struct rte_uio_pci_dev *udev, int32_t state)
150 {
151         struct pci_dev *pdev = udev->pdev;
152
153         if (udev->mode == IGBUIO_MSIX_INTR_MODE) {
154                 struct msi_desc *desc;
155
156                 list_for_each_entry(desc, &pdev->msi_list, list) {
157                         igbuio_msix_mask_irq(desc, state);
158                 }
159         }
160         else if (udev->mode == IGBUIO_LEGACY_INTR_MODE) {
161                 uint32_t status;
162                 uint16_t old, new;
163
164                 pci_read_config_dword(pdev, PCI_COMMAND, &status);
165                 old = status;
166                 if (state != 0)
167                         new = old & (~PCI_COMMAND_INTX_DISABLE);
168                 else
169                         new = old | PCI_COMMAND_INTX_DISABLE;
170
171                 if (old != new)
172                         pci_write_config_word(pdev, PCI_COMMAND, new);
173         }
174
175         return 0;
176 }
177
178 /**
179  * This is the irqcontrol callback to be registered to uio_info.
180  * It can be used to disable/enable interrupt from user space processes.
181  *
182  * @param info
183  *  pointer to uio_info.
184  * @param irq_state
185  *  state value. 1 to enable interrupt, 0 to disable interrupt.
186  *
187  * @return
188  *  - On success, 0.
189  *  - On failure, a negative value.
190  */
191 static int
192 igbuio_pci_irqcontrol(struct uio_info *info, s32 irq_state)
193 {
194         unsigned long flags;
195         struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
196         struct pci_dev *pdev = udev->pdev;
197
198         spin_lock_irqsave(&udev->lock, flags);
199         if (!pci_lock(pdev)) {
200                 spin_unlock_irqrestore(&udev->lock, flags);
201                 return -1;
202         }
203
204         igbuio_set_interrupt_mask(udev, irq_state);
205
206         pci_unlock(pdev);
207         spin_unlock_irqrestore(&udev->lock, flags);
208
209         return 0;
210 }
211
212 /**
213  * This is interrupt handler which will check if the interrupt is for the right device.
214  * If yes, disable it here and will be enable later.
215  */
216 static irqreturn_t
217 igbuio_pci_irqhandler(int irq, struct uio_info *info)
218 {
219         irqreturn_t ret = IRQ_NONE;
220         unsigned long flags;
221         struct rte_uio_pci_dev *udev = igbuio_get_uio_pci_dev(info);
222         struct pci_dev *pdev = udev->pdev;
223         uint32_t cmd_status_dword;
224         uint16_t status;
225
226         spin_lock_irqsave(&udev->lock, flags);
227         /* block userspace PCI config reads/writes */
228         if (!pci_lock(pdev))
229                 goto spin_unlock;
230
231         /* for legacy mode, interrupt maybe shared */
232         if (udev->mode == IGBUIO_LEGACY_INTR_MODE) {
233                 pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
234                 status = cmd_status_dword >> 16;
235                 /* interrupt is not ours, goes to out */
236                 if (!(status & PCI_STATUS_INTERRUPT))
237                         goto done;
238         }
239
240         igbuio_set_interrupt_mask(udev, 0);
241         ret = IRQ_HANDLED;
242 done:
243         /* unblock userspace PCI config reads/writes */
244         pci_unlock(pdev);
245 spin_unlock:
246         spin_unlock_irqrestore(&udev->lock, flags);
247         printk(KERN_INFO "irq 0x%x %s\n", irq, (ret == IRQ_HANDLED) ? "handled" : "not handled");
248
249         return ret;
250 }
251
252 /* Remap pci resources described by bar #pci_bar in uio resource n. */
253 static int
254 igbuio_pci_setup_iomem(struct pci_dev *dev, struct uio_info *info,
255                        int n, int pci_bar, const char *name)
256 {
257         unsigned long addr, len;
258         void *internal_addr;
259
260         addr = pci_resource_start(dev, pci_bar);
261         len = pci_resource_len(dev, pci_bar);
262         if (addr == 0 || len == 0)
263                 return -1;
264         internal_addr = ioremap(addr, len);
265         if (internal_addr == NULL)
266                 return -1;
267         info->mem[n].name = name;
268         info->mem[n].addr = addr;
269         info->mem[n].internal_addr = internal_addr;
270         info->mem[n].size = len;
271         info->mem[n].memtype = UIO_MEM_PHYS;
272         return 0;
273 }
274
275 /* Unmap previously ioremap'd resources */
276 static void
277 igbuio_pci_release_iomem(struct uio_info *info)
278 {
279         int i;
280         for (i = 0; i < MAX_UIO_MAPS; i++) {
281                 if (info->mem[i].internal_addr)
282                         iounmap(info->mem[i].internal_addr);
283         }
284 }
285
286 static int __devinit
287 igbuio_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
288 {
289         struct rte_uio_pci_dev *udev;
290
291         udev = kzalloc(sizeof(struct rte_uio_pci_dev), GFP_KERNEL);
292         if (!udev)
293                 return -ENOMEM;
294
295         /*
296          * enable device: ask low-level code to enable I/O and
297          * memory
298          */
299         if (pci_enable_device(dev)) {
300                 printk(KERN_ERR "Cannot enable PCI device\n");
301                 goto fail_free;
302         }
303
304         /* XXX should we use 64 bits ? */
305         /* set 32-bit DMA mask */
306         if (pci_set_dma_mask(dev,(uint64_t)0xffffffff)) {
307                 printk(KERN_ERR "Cannot set DMA mask\n");
308                 goto fail_disable;
309         }
310
311         /*
312          * reserve device's PCI memory regions for use by this
313          * module
314          */
315         if (pci_request_regions(dev, "igb_uio")) {
316                 printk(KERN_ERR "Cannot request regions\n");
317                 goto fail_disable;
318         }
319
320         /* enable bus mastering on the device */
321         pci_set_master(dev);
322
323         /* remap IO memory */
324         if (igbuio_pci_setup_iomem(dev, &udev->info, 0, 0, "config"))
325                 goto fail_release_regions;
326
327         /* fill uio infos */
328         udev->info.name = "Intel IGB UIO";
329         udev->info.version = "0.1";
330         udev->info.handler = igbuio_pci_irqhandler;
331         udev->info.irqcontrol = igbuio_pci_irqcontrol;
332         udev->info.priv = udev;
333         udev->pdev = dev;
334         udev->mode = 0; /* set the default value for interrupt mode */
335         spin_lock_init(&udev->lock);
336
337         /* check if it need to try msix first */
338         if (igbuio_intr_mode_preferred == IGBUIO_MSIX_INTR_MODE) {
339                 int vector;
340
341                 for (vector = 0; vector < IGBUIO_NUM_MSI_VECTORS; vector ++)
342                         udev->msix_entries[vector].entry = vector;
343
344                 if (pci_enable_msix(udev->pdev, udev->msix_entries, IGBUIO_NUM_MSI_VECTORS) == 0) {
345                         udev->mode = IGBUIO_MSIX_INTR_MODE;
346                 }
347                 else {
348                         pci_disable_msix(udev->pdev);
349                         printk(KERN_INFO "fail to enable pci msix, or not enough msix entries\n");
350                 }
351         }
352         switch (udev->mode) {
353         case IGBUIO_MSIX_INTR_MODE:
354                 udev->info.irq_flags = 0;
355                 udev->info.irq = udev->msix_entries[0].vector;
356                 break;
357         case IGBUIO_MSI_INTR_MODE:
358                 break;
359         case IGBUIO_LEGACY_INTR_MODE:
360                 udev->info.irq_flags = IRQF_SHARED;
361                 udev->info.irq = dev->irq;
362                 break;
363         default:
364                 break;
365         }
366
367         pci_set_drvdata(dev, udev);
368         igbuio_pci_irqcontrol(&udev->info, 0);
369
370         /* register uio driver */
371         if (uio_register_device(&dev->dev, &udev->info))
372                 goto fail_release_iomem;
373
374         printk(KERN_INFO "uio device registered with irq %lx\n", udev->info.irq);
375
376         return 0;
377
378 fail_release_iomem:
379         igbuio_pci_release_iomem(&udev->info);
380         if (udev->mode == IGBUIO_MSIX_INTR_MODE)
381                 pci_disable_msix(udev->pdev);
382 fail_release_regions:
383         pci_release_regions(dev);
384 fail_disable:
385         pci_disable_device(dev);
386 fail_free:
387         kfree(udev);
388
389         return -ENODEV;
390 }
391
392 static void
393 igbuio_pci_remove(struct pci_dev *dev)
394 {
395         struct uio_info *info = pci_get_drvdata(dev);
396
397         if (info->priv == NULL) {
398                 printk(KERN_DEBUG "Not igbuio device\n");
399                 return;
400         }
401
402         uio_unregister_device(info);
403         igbuio_pci_release_iomem(info);
404         if (((struct rte_uio_pci_dev *)info->priv)->mode == IGBUIO_MSIX_INTR_MODE)
405                 pci_disable_msix(dev);
406         pci_release_regions(dev);
407         pci_disable_device(dev);
408         pci_set_drvdata(dev, NULL);
409         kfree(info);
410 }
411
412 static struct pci_driver igbuio_pci_driver = {
413         .name = "igb_uio",
414         .id_table = igbuio_pci_ids,
415         .probe = igbuio_pci_probe,
416         .remove = igbuio_pci_remove,
417 };
418
419 static int __init
420 igbuio_pci_init_module(void)
421 {
422         return pci_register_driver(&igbuio_pci_driver);
423 }
424
425 static void __exit
426 igbuio_pci_exit_module(void)
427 {
428         pci_unregister_driver(&igbuio_pci_driver);
429 }
430
431 module_init(igbuio_pci_init_module);
432 module_exit(igbuio_pci_exit_module);
433
434 MODULE_DESCRIPTION("UIO driver for Intel IGB PCI cards");
435 MODULE_LICENSE("GPL");
436 MODULE_AUTHOR("Intel Corporation");