xen/pciback: Return error on XEN_PCI_OP_enable_msi when device has MSI or MSI-X enabled
[pandora-kernel.git] / drivers / xen / xen-pciback / pciback_ops.c
1 /*
2  * PCI Backend Operations - respond to PCI requests from Frontend
3  *
4  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5  */
6 #include <linux/module.h>
7 #include <linux/wait.h>
8 #include <linux/bitops.h>
9 #include <xen/events.h>
10 #include <linux/sched.h>
11 #include <linux/ratelimit.h>
12 #include "pciback.h"
13
14 int verbose_request;
15 module_param(verbose_request, int, 0644);
16
17 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
18
19 /* Ensure a device is has the fake IRQ handler "turned on/off" and is
20  * ready to be exported. This MUST be run after xen_pcibk_reset_device
21  * which does the actual PCI device enable/disable.
22  */
23 static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
24 {
25         struct xen_pcibk_dev_data *dev_data;
26         int rc;
27         int enable = 0;
28
29         dev_data = pci_get_drvdata(dev);
30         if (!dev_data)
31                 return;
32
33         /* We don't deal with bridges */
34         if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
35                 return;
36
37         if (reset) {
38                 dev_data->enable_intx = 0;
39                 dev_data->ack_intr = 0;
40         }
41         enable =  dev_data->enable_intx;
42
43         /* Asked to disable, but ISR isn't runnig */
44         if (!enable && !dev_data->isr_on)
45                 return;
46
47         /* Squirrel away the IRQs in the dev_data. We need this
48          * b/c when device transitions to MSI, the dev->irq is
49          * overwritten with the MSI vector.
50          */
51         if (enable)
52                 dev_data->irq = dev->irq;
53
54         /*
55          * SR-IOV devices in all use MSI-X and have no legacy
56          * interrupts, so inhibit creating a fake IRQ handler for them.
57          */
58         if (dev_data->irq == 0)
59                 goto out;
60
61         dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
62                 dev_data->irq_name,
63                 dev_data->irq,
64                 pci_is_enabled(dev) ? "on" : "off",
65                 dev->msi_enabled ? "MSI" : "",
66                 dev->msix_enabled ? "MSI/X" : "",
67                 dev_data->isr_on ? "enable" : "disable",
68                 enable ? "enable" : "disable");
69
70         if (enable) {
71                 rc = request_irq(dev_data->irq,
72                                 xen_pcibk_guest_interrupt, IRQF_SHARED,
73                                 dev_data->irq_name, dev);
74                 if (rc) {
75                         dev_err(&dev->dev, "%s: failed to install fake IRQ " \
76                                 "handler for IRQ %d! (rc:%d)\n",
77                                 dev_data->irq_name, dev_data->irq, rc);
78                         goto out;
79                 }
80         } else {
81                 free_irq(dev_data->irq, dev);
82                 dev_data->irq = 0;
83         }
84         dev_data->isr_on = enable;
85         dev_data->ack_intr = enable;
86 out:
87         dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
88                 dev_data->irq_name,
89                 dev_data->irq,
90                 pci_is_enabled(dev) ? "on" : "off",
91                 dev->msi_enabled ? "MSI" : "",
92                 dev->msix_enabled ? "MSI/X" : "",
93                 enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
94                         (dev_data->isr_on ? "failed to disable" : "disabled"));
95 }
96
97 /* Ensure a device is "turned off" and ready to be exported.
98  * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
99  * ready to be re-exported)
100  */
101 void xen_pcibk_reset_device(struct pci_dev *dev)
102 {
103         u16 cmd;
104
105         xen_pcibk_control_isr(dev, 1 /* reset device */);
106
107         /* Disable devices (but not bridges) */
108         if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
109 #ifdef CONFIG_PCI_MSI
110                 /* The guest could have been abruptly killed without
111                  * disabling MSI/MSI-X interrupts.*/
112                 if (dev->msix_enabled)
113                         pci_disable_msix(dev);
114                 if (dev->msi_enabled)
115                         pci_disable_msi(dev);
116 #endif
117                 if (pci_is_enabled(dev))
118                         pci_disable_device(dev);
119
120                 pci_write_config_word(dev, PCI_COMMAND, 0);
121
122                 dev->is_busmaster = 0;
123         } else {
124                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
125                 if (cmd & (PCI_COMMAND_INVALIDATE)) {
126                         cmd &= ~(PCI_COMMAND_INVALIDATE);
127                         pci_write_config_word(dev, PCI_COMMAND, cmd);
128
129                         dev->is_busmaster = 0;
130                 }
131         }
132 }
133
134 #ifdef CONFIG_PCI_MSI
135 static
136 int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
137                          struct pci_dev *dev, struct xen_pci_op *op)
138 {
139         struct xen_pcibk_dev_data *dev_data;
140         int status;
141
142         if (unlikely(verbose_request))
143                 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI\n", pci_name(dev));
144
145         if (dev->msi_enabled)
146                 status = -EALREADY;
147         else if (dev->msix_enabled)
148                 status = -ENXIO;
149         else
150                 status = pci_enable_msi(dev);
151
152         if (status) {
153                 pr_warn_ratelimited(DRV_NAME ": %s: error enabling MSI for guest %u: err %d\n",
154                                     pci_name(dev), pdev->xdev->otherend_id,
155                                     status);
156                 op->value = 0;
157                 return XEN_PCI_ERR_op_failed;
158         }
159
160         /* The value the guest needs is actually the IDT vector, not the
161          * the local domain's IRQ number. */
162
163         op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
164         if (unlikely(verbose_request))
165                 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
166                         op->value);
167
168         dev_data = pci_get_drvdata(dev);
169         if (dev_data)
170                 dev_data->ack_intr = 0;
171
172         return 0;
173 }
174
175 static
176 int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
177                           struct pci_dev *dev, struct xen_pci_op *op)
178 {
179         struct xen_pcibk_dev_data *dev_data;
180
181         if (unlikely(verbose_request))
182                 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI\n",
183                        pci_name(dev));
184         pci_disable_msi(dev);
185
186         op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
187         if (unlikely(verbose_request))
188                 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
189                         op->value);
190         dev_data = pci_get_drvdata(dev);
191         if (dev_data)
192                 dev_data->ack_intr = 1;
193         return 0;
194 }
195
196 static
197 int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
198                           struct pci_dev *dev, struct xen_pci_op *op)
199 {
200         struct xen_pcibk_dev_data *dev_data;
201         int i, result;
202         struct msix_entry *entries;
203
204         if (unlikely(verbose_request))
205                 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI-X\n",
206                        pci_name(dev));
207         if (op->value > SH_INFO_MAX_VEC)
208                 return -EINVAL;
209
210         entries = kmalloc(op->value * sizeof(*entries), GFP_KERNEL);
211         if (entries == NULL)
212                 return -ENOMEM;
213
214         for (i = 0; i < op->value; i++) {
215                 entries[i].entry = op->msix_entries[i].entry;
216                 entries[i].vector = op->msix_entries[i].vector;
217         }
218
219         result = pci_enable_msix(dev, entries, op->value);
220
221         if (result == 0) {
222                 for (i = 0; i < op->value; i++) {
223                         op->msix_entries[i].entry = entries[i].entry;
224                         if (entries[i].vector)
225                                 op->msix_entries[i].vector =
226                                         xen_pirq_from_irq(entries[i].vector);
227                                 if (unlikely(verbose_request))
228                                         printk(KERN_DEBUG DRV_NAME ": %s: " \
229                                                 "MSI-X[%d]: %d\n",
230                                                 pci_name(dev), i,
231                                                 op->msix_entries[i].vector);
232                 }
233         } else
234                 pr_warn_ratelimited(DRV_NAME ": %s: error enabling MSI-X for guest %u: err %d!\n",
235                                     pci_name(dev), pdev->xdev->otherend_id,
236                                     result);
237         kfree(entries);
238
239         op->value = result;
240         dev_data = pci_get_drvdata(dev);
241         if (dev_data)
242                 dev_data->ack_intr = 0;
243
244         return result;
245 }
246
247 static
248 int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
249                            struct pci_dev *dev, struct xen_pci_op *op)
250 {
251         struct xen_pcibk_dev_data *dev_data;
252         if (unlikely(verbose_request))
253                 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
254                         pci_name(dev));
255         pci_disable_msix(dev);
256
257         /*
258          * SR-IOV devices (which don't have any legacy IRQ) have
259          * an undefined IRQ value of zero.
260          */
261         op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
262         if (unlikely(verbose_request))
263                 printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n", pci_name(dev),
264                         op->value);
265         dev_data = pci_get_drvdata(dev);
266         if (dev_data)
267                 dev_data->ack_intr = 1;
268         return 0;
269 }
270 #endif
271 /*
272 * Now the same evtchn is used for both pcifront conf_read_write request
273 * as well as pcie aer front end ack. We use a new work_queue to schedule
274 * xen_pcibk conf_read_write service for avoiding confict with aer_core
275 * do_recovery job which also use the system default work_queue
276 */
277 void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
278 {
279         /* Check that frontend is requesting an operation and that we are not
280          * already processing a request */
281         if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
282             && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
283                 queue_work(xen_pcibk_wq, &pdev->op_work);
284         }
285         /*_XEN_PCIB_active should have been cleared by pcifront. And also make
286         sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
287         if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
288             && test_bit(_PCIB_op_pending, &pdev->flags)) {
289                 wake_up(&xen_pcibk_aer_wait_queue);
290         }
291 }
292
293 /* Performing the configuration space reads/writes must not be done in atomic
294  * context because some of the pci_* functions can sleep (mostly due to ACPI
295  * use of semaphores). This function is intended to be called from a work
296  * queue in process context taking a struct xen_pcibk_device as a parameter */
297
298 void xen_pcibk_do_op(struct work_struct *data)
299 {
300         struct xen_pcibk_device *pdev =
301                 container_of(data, struct xen_pcibk_device, op_work);
302         struct pci_dev *dev;
303         struct xen_pcibk_dev_data *dev_data = NULL;
304         struct xen_pci_op *op = &pdev->op;
305         int test_intx = 0;
306
307         *op = pdev->sh_info->op;
308         barrier();
309         dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
310
311         if (dev == NULL)
312                 op->err = XEN_PCI_ERR_dev_not_found;
313         else {
314                 dev_data = pci_get_drvdata(dev);
315                 if (dev_data)
316                         test_intx = dev_data->enable_intx;
317                 switch (op->cmd) {
318                 case XEN_PCI_OP_conf_read:
319                         op->err = xen_pcibk_config_read(dev,
320                                   op->offset, op->size, &op->value);
321                         break;
322                 case XEN_PCI_OP_conf_write:
323                         op->err = xen_pcibk_config_write(dev,
324                                   op->offset, op->size, op->value);
325                         break;
326 #ifdef CONFIG_PCI_MSI
327                 case XEN_PCI_OP_enable_msi:
328                         op->err = xen_pcibk_enable_msi(pdev, dev, op);
329                         break;
330                 case XEN_PCI_OP_disable_msi:
331                         op->err = xen_pcibk_disable_msi(pdev, dev, op);
332                         break;
333                 case XEN_PCI_OP_enable_msix:
334                         op->err = xen_pcibk_enable_msix(pdev, dev, op);
335                         break;
336                 case XEN_PCI_OP_disable_msix:
337                         op->err = xen_pcibk_disable_msix(pdev, dev, op);
338                         break;
339 #endif
340                 default:
341                         op->err = XEN_PCI_ERR_not_implemented;
342                         break;
343                 }
344         }
345         if (!op->err && dev && dev_data) {
346                 /* Transition detected */
347                 if ((dev_data->enable_intx != test_intx))
348                         xen_pcibk_control_isr(dev, 0 /* no reset */);
349         }
350         pdev->sh_info->op.err = op->err;
351         pdev->sh_info->op.value = op->value;
352 #ifdef CONFIG_PCI_MSI
353         if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
354                 unsigned int i;
355
356                 for (i = 0; i < op->value; i++)
357                         pdev->sh_info->op.msix_entries[i].vector =
358                                 op->msix_entries[i].vector;
359         }
360 #endif
361         /* Tell the driver domain that we're done. */
362         wmb();
363         clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
364         notify_remote_via_irq(pdev->evtchn_irq);
365
366         /* Mark that we're done. */
367         smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */
368         clear_bit(_PDEVF_op_active, &pdev->flags);
369         smp_mb__after_clear_bit(); /* /before/ final check for work */
370
371         /* Check to see if the driver domain tried to start another request in
372          * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
373         */
374         xen_pcibk_test_and_schedule_op(pdev);
375 }
376
377 irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
378 {
379         struct xen_pcibk_device *pdev = dev_id;
380
381         xen_pcibk_test_and_schedule_op(pdev);
382
383         return IRQ_HANDLED;
384 }
385 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
386 {
387         struct pci_dev *dev = (struct pci_dev *)dev_id;
388         struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
389
390         if (dev_data->isr_on && dev_data->ack_intr) {
391                 dev_data->handled++;
392                 if ((dev_data->handled % 1000) == 0) {
393                         if (xen_test_irq_shared(irq)) {
394                                 printk(KERN_INFO "%s IRQ line is not shared "
395                                         "with other domains. Turning ISR off\n",
396                                          dev_data->irq_name);
397                                 dev_data->ack_intr = 0;
398                         }
399                 }
400                 return IRQ_HANDLED;
401         }
402         return IRQ_NONE;
403 }