drbd: merge_bvec_fn: properly remap bvm->bi_bdev
[pandora-kernel.git] / drivers / uio / uio_pci_generic.c
1 /* uio_pci_generic - generic UIO driver for PCI 2.3 devices
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  * Author: Michael S. Tsirkin <mst@redhat.com>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  *
8  * Since the driver does not declare any device ids, you must allocate
9  * id and bind the device to the driver yourself.  For example:
10  *
11  * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
12  * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
13  * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
14  * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
15  * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
16  *
17  * Driver won't bind to devices which do not support the Interrupt Disable Bit
18  * in the command register. All devices compliant to PCI 2.3 (circa 2002) and
19  * all compliant PCI Express devices should support this bit.
20  */
21
22 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/slab.h>
26 #include <linux/uio_driver.h>
27
28 #define DRIVER_VERSION  "0.01.0"
29 #define DRIVER_AUTHOR   "Michael S. Tsirkin <mst@redhat.com>"
30 #define DRIVER_DESC     "Generic UIO driver for PCI 2.3 devices"
31
32 struct uio_pci_generic_dev {
33         struct uio_info info;
34         struct pci_dev *pdev;
35 };
36
37 static inline struct uio_pci_generic_dev *
38 to_uio_pci_generic_dev(struct uio_info *info)
39 {
40         return container_of(info, struct uio_pci_generic_dev, info);
41 }
42
43 /* Interrupt handler. Read/modify/write the command register to disable
44  * the interrupt. */
45 static irqreturn_t irqhandler(int irq, struct uio_info *info)
46 {
47         struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
48         struct pci_dev *pdev = gdev->pdev;
49         irqreturn_t ret = IRQ_NONE;
50         u32 cmd_status_dword;
51         u16 origcmd, newcmd, status;
52
53         /* We do a single dword read to retrieve both command and status.
54          * Document assumptions that make this possible. */
55         BUILD_BUG_ON(PCI_COMMAND % 4);
56         BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
57
58         pci_block_user_cfg_access(pdev);
59
60         /* Read both command and status registers in a single 32-bit operation.
61          * Note: we could cache the value for command and move the status read
62          * out of the lock if there was a way to get notified of user changes
63          * to command register through sysfs. Should be good for shared irqs. */
64         pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
65         origcmd = cmd_status_dword;
66         status = cmd_status_dword >> 16;
67
68         /* Check interrupt status register to see whether our device
69          * triggered the interrupt. */
70         if (!(status & PCI_STATUS_INTERRUPT))
71                 goto done;
72
73         /* We triggered the interrupt, disable it. */
74         newcmd = origcmd | PCI_COMMAND_INTX_DISABLE;
75         if (newcmd != origcmd)
76                 pci_write_config_word(pdev, PCI_COMMAND, newcmd);
77
78         /* UIO core will signal the user process. */
79         ret = IRQ_HANDLED;
80 done:
81
82         pci_unblock_user_cfg_access(pdev);
83         return ret;
84 }
85
86 /* Verify that the device supports Interrupt Disable bit in command register,
87  * per PCI 2.3, by flipping this bit and reading it back: this bit was readonly
88  * in PCI 2.2. */
89 static int __devinit verify_pci_2_3(struct pci_dev *pdev)
90 {
91         u16 orig, new;
92         int err = 0;
93
94         pci_block_user_cfg_access(pdev);
95         pci_read_config_word(pdev, PCI_COMMAND, &orig);
96         pci_write_config_word(pdev, PCI_COMMAND,
97                               orig ^ PCI_COMMAND_INTX_DISABLE);
98         pci_read_config_word(pdev, PCI_COMMAND, &new);
99         /* There's no way to protect against
100          * hardware bugs or detect them reliably, but as long as we know
101          * what the value should be, let's go ahead and check it. */
102         if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
103                 err = -EBUSY;
104                 dev_err(&pdev->dev, "Command changed from 0x%x to 0x%x: "
105                         "driver or HW bug?\n", orig, new);
106                 goto err;
107         }
108         if (!((new ^ orig) & PCI_COMMAND_INTX_DISABLE)) {
109                 dev_warn(&pdev->dev, "Device does not support "
110                          "disabling interrupts: unable to bind.\n");
111                 err = -ENODEV;
112                 goto err;
113         }
114         /* Now restore the original value. */
115         pci_write_config_word(pdev, PCI_COMMAND, orig);
116 err:
117         pci_unblock_user_cfg_access(pdev);
118         return err;
119 }
120
121 static int __devinit probe(struct pci_dev *pdev,
122                            const struct pci_device_id *id)
123 {
124         struct uio_pci_generic_dev *gdev;
125         int err;
126
127         err = pci_enable_device(pdev);
128         if (err) {
129                 dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
130                         __func__, err);
131                 return err;
132         }
133
134         if (!pdev->irq) {
135                 dev_warn(&pdev->dev, "No IRQ assigned to device: "
136                          "no support for interrupts?\n");
137                 pci_disable_device(pdev);
138                 return -ENODEV;
139         }
140
141         err = verify_pci_2_3(pdev);
142         if (err)
143                 goto err_verify;
144
145         gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
146         if (!gdev) {
147                 err = -ENOMEM;
148                 goto err_alloc;
149         }
150
151         gdev->info.name = "uio_pci_generic";
152         gdev->info.version = DRIVER_VERSION;
153         gdev->info.irq = pdev->irq;
154         gdev->info.irq_flags = IRQF_SHARED;
155         gdev->info.handler = irqhandler;
156         gdev->pdev = pdev;
157
158         if (uio_register_device(&pdev->dev, &gdev->info))
159                 goto err_register;
160         pci_set_drvdata(pdev, gdev);
161
162         return 0;
163 err_register:
164         kfree(gdev);
165 err_alloc:
166 err_verify:
167         pci_disable_device(pdev);
168         return err;
169 }
170
171 static void remove(struct pci_dev *pdev)
172 {
173         struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
174
175         uio_unregister_device(&gdev->info);
176         pci_disable_device(pdev);
177         kfree(gdev);
178 }
179
180 static struct pci_driver driver = {
181         .name = "uio_pci_generic",
182         .id_table = NULL, /* only dynamic id's */
183         .probe = probe,
184         .remove = remove,
185 };
186
187 static int __init init(void)
188 {
189         pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
190         return pci_register_driver(&driver);
191 }
192
193 static void __exit cleanup(void)
194 {
195         pci_unregister_driver(&driver);
196 }
197
198 module_init(init);
199 module_exit(cleanup);
200
201 MODULE_VERSION(DRIVER_VERSION);
202 MODULE_LICENSE("GPL v2");
203 MODULE_AUTHOR(DRIVER_AUTHOR);
204 MODULE_DESCRIPTION(DRIVER_DESC);