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