blob: d52703c5821ae6c2355b1e4633de7fb97f39d4f9 [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
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 "pciback.h"
Jan Beulich1bcf5bc2013-02-06 10:30:38 -050012#include <linux/ratelimit.h>
13#include <linux/printk.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040014
15int verbose_request;
16module_param(verbose_request, int, 0644);
17
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040018static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
19
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040020/* Ensure a device is has the fake IRQ handler "turned on/off" and is
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040021 * ready to be exported. This MUST be run after xen_pcibk_reset_device
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040022 * which does the actual PCI device enable/disable.
23 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040024static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040025{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040026 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040027 int rc;
28 int enable = 0;
29
30 dev_data = pci_get_drvdata(dev);
31 if (!dev_data)
32 return;
33
34 /* We don't deal with bridges */
35 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
36 return;
37
38 if (reset) {
39 dev_data->enable_intx = 0;
40 dev_data->ack_intr = 0;
41 }
42 enable = dev_data->enable_intx;
43
44 /* Asked to disable, but ISR isn't runnig */
45 if (!enable && !dev_data->isr_on)
46 return;
47
48 /* Squirrel away the IRQs in the dev_data. We need this
49 * b/c when device transitions to MSI, the dev->irq is
50 * overwritten with the MSI vector.
51 */
52 if (enable)
53 dev_data->irq = dev->irq;
54
Konrad Rzeszutek Wilke17ab352011-02-16 15:43:25 -050055 /*
56 * SR-IOV devices in all use MSI-X and have no legacy
57 * interrupts, so inhibit creating a fake IRQ handler for them.
58 */
59 if (dev_data->irq == 0)
60 goto out;
61
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040062 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
63 dev_data->irq_name,
64 dev_data->irq,
65 pci_is_enabled(dev) ? "on" : "off",
66 dev->msi_enabled ? "MSI" : "",
67 dev->msix_enabled ? "MSI/X" : "",
68 dev_data->isr_on ? "enable" : "disable",
69 enable ? "enable" : "disable");
70
71 if (enable) {
72 rc = request_irq(dev_data->irq,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040073 xen_pcibk_guest_interrupt, IRQF_SHARED,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040074 dev_data->irq_name, dev);
75 if (rc) {
76 dev_err(&dev->dev, "%s: failed to install fake IRQ " \
77 "handler for IRQ %d! (rc:%d)\n",
78 dev_data->irq_name, dev_data->irq, rc);
79 goto out;
80 }
81 } else {
82 free_irq(dev_data->irq, dev);
83 dev_data->irq = 0;
84 }
85 dev_data->isr_on = enable;
86 dev_data->ack_intr = enable;
87out:
88 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
89 dev_data->irq_name,
90 dev_data->irq,
91 pci_is_enabled(dev) ? "on" : "off",
92 dev->msi_enabled ? "MSI" : "",
93 dev->msix_enabled ? "MSI/X" : "",
94 enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
95 (dev_data->isr_on ? "failed to disable" : "disabled"));
96}
97
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040098/* Ensure a device is "turned off" and ready to be exported.
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040099 * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400100 * ready to be re-exported)
101 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400102void xen_pcibk_reset_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400103{
104 u16 cmd;
105
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400106 xen_pcibk_control_isr(dev, 1 /* reset device */);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400107
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400108 /* Disable devices (but not bridges) */
109 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
Konrad Rzeszutek Wilka2be65fd2010-03-03 13:38:43 -0500110#ifdef CONFIG_PCI_MSI
111 /* The guest could have been abruptly killed without
112 * disabling MSI/MSI-X interrupts.*/
113 if (dev->msix_enabled)
114 pci_disable_msix(dev);
115 if (dev->msi_enabled)
116 pci_disable_msi(dev);
117#endif
Konrad Rzeszutek Wilk40e0c1e2013-03-05 13:14:19 -0500118 if (pci_is_enabled(dev))
119 pci_disable_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400120
121 pci_write_config_word(dev, PCI_COMMAND, 0);
122
123 dev->is_busmaster = 0;
124 } else {
125 pci_read_config_word(dev, PCI_COMMAND, &cmd);
126 if (cmd & (PCI_COMMAND_INVALIDATE)) {
127 cmd &= ~(PCI_COMMAND_INVALIDATE);
128 pci_write_config_word(dev, PCI_COMMAND, cmd);
129
130 dev->is_busmaster = 0;
131 }
132 }
133}
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400134
135#ifdef CONFIG_PCI_MSI
136static
137int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
138 struct pci_dev *dev, struct xen_pci_op *op)
139{
140 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400141 int status;
142
143 if (unlikely(verbose_request))
144 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI\n", pci_name(dev));
145
146 status = pci_enable_msi(dev);
147
148 if (status) {
Jan Beulich1bcf5bc2013-02-06 10:30:38 -0500149 pr_warn_ratelimited(DRV_NAME ": %s: error enabling MSI for guest %u: err %d\n",
150 pci_name(dev), pdev->xdev->otherend_id,
151 status);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400152 op->value = 0;
153 return XEN_PCI_ERR_op_failed;
154 }
155
156 /* The value the guest needs is actually the IDT vector, not the
157 * the local domain's IRQ number. */
158
159 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
160 if (unlikely(verbose_request))
161 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
162 op->value);
163
164 dev_data = pci_get_drvdata(dev);
165 if (dev_data)
166 dev_data->ack_intr = 0;
167
168 return 0;
169}
170
171static
172int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
173 struct pci_dev *dev, struct xen_pci_op *op)
174{
175 struct xen_pcibk_dev_data *dev_data;
176
177 if (unlikely(verbose_request))
178 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI\n",
179 pci_name(dev));
180 pci_disable_msi(dev);
181
182 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
183 if (unlikely(verbose_request))
184 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
185 op->value);
186 dev_data = pci_get_drvdata(dev);
187 if (dev_data)
188 dev_data->ack_intr = 1;
189 return 0;
190}
191
192static
193int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
194 struct pci_dev *dev, struct xen_pci_op *op)
195{
196 struct xen_pcibk_dev_data *dev_data;
197 int i, result;
198 struct msix_entry *entries;
199
200 if (unlikely(verbose_request))
201 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI-X\n",
202 pci_name(dev));
203 if (op->value > SH_INFO_MAX_VEC)
204 return -EINVAL;
205
206 entries = kmalloc(op->value * sizeof(*entries), GFP_KERNEL);
207 if (entries == NULL)
208 return -ENOMEM;
209
210 for (i = 0; i < op->value; i++) {
211 entries[i].entry = op->msix_entries[i].entry;
212 entries[i].vector = op->msix_entries[i].vector;
213 }
214
215 result = pci_enable_msix(dev, entries, op->value);
216
217 if (result == 0) {
218 for (i = 0; i < op->value; i++) {
219 op->msix_entries[i].entry = entries[i].entry;
220 if (entries[i].vector)
221 op->msix_entries[i].vector =
222 xen_pirq_from_irq(entries[i].vector);
223 if (unlikely(verbose_request))
224 printk(KERN_DEBUG DRV_NAME ": %s: " \
225 "MSI-X[%d]: %d\n",
226 pci_name(dev), i,
227 op->msix_entries[i].vector);
228 }
Jan Beulich1bcf5bc2013-02-06 10:30:38 -0500229 } else
230 pr_warn_ratelimited(DRV_NAME ": %s: error enabling MSI-X for guest %u: err %d!\n",
231 pci_name(dev), pdev->xdev->otherend_id,
232 result);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400233 kfree(entries);
234
235 op->value = result;
236 dev_data = pci_get_drvdata(dev);
237 if (dev_data)
238 dev_data->ack_intr = 0;
239
Jan Beulich0ee46ec2012-04-02 15:32:22 +0100240 return result > 0 ? 0 : result;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400241}
242
243static
244int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
245 struct pci_dev *dev, struct xen_pci_op *op)
246{
247 struct xen_pcibk_dev_data *dev_data;
248 if (unlikely(verbose_request))
249 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
250 pci_name(dev));
251 pci_disable_msix(dev);
252
253 /*
254 * SR-IOV devices (which don't have any legacy IRQ) have
255 * an undefined IRQ value of zero.
256 */
257 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
258 if (unlikely(verbose_request))
259 printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n", pci_name(dev),
260 op->value);
261 dev_data = pci_get_drvdata(dev);
262 if (dev_data)
263 dev_data->ack_intr = 1;
264 return 0;
265}
266#endif
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400267/*
268* Now the same evtchn is used for both pcifront conf_read_write request
269* as well as pcie aer front end ack. We use a new work_queue to schedule
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400270* xen_pcibk conf_read_write service for avoiding confict with aer_core
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400271* do_recovery job which also use the system default work_queue
272*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400273void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400274{
275 /* Check that frontend is requesting an operation and that we are not
276 * already processing a request */
277 if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
278 && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400279 queue_work(xen_pcibk_wq, &pdev->op_work);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400280 }
281 /*_XEN_PCIB_active should have been cleared by pcifront. And also make
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400282 sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400283 if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
284 && test_bit(_PCIB_op_pending, &pdev->flags)) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400285 wake_up(&xen_pcibk_aer_wait_queue);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400286 }
287}
288
289/* Performing the configuration space reads/writes must not be done in atomic
290 * context because some of the pci_* functions can sleep (mostly due to ACPI
291 * use of semaphores). This function is intended to be called from a work
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400292 * queue in process context taking a struct xen_pcibk_device as a parameter */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400293
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400294void xen_pcibk_do_op(struct work_struct *data)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400295{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400296 struct xen_pcibk_device *pdev =
297 container_of(data, struct xen_pcibk_device, op_work);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400298 struct pci_dev *dev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400299 struct xen_pcibk_dev_data *dev_data = NULL;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400300 struct xen_pci_op *op = &pdev->sh_info->op;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400301 int test_intx = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400302
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400303 dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400304
305 if (dev == NULL)
306 op->err = XEN_PCI_ERR_dev_not_found;
307 else {
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400308 dev_data = pci_get_drvdata(dev);
309 if (dev_data)
310 test_intx = dev_data->enable_intx;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400311 switch (op->cmd) {
312 case XEN_PCI_OP_conf_read:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400313 op->err = xen_pcibk_config_read(dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400314 op->offset, op->size, &op->value);
315 break;
316 case XEN_PCI_OP_conf_write:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400317 op->err = xen_pcibk_config_write(dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400318 op->offset, op->size, op->value);
319 break;
320#ifdef CONFIG_PCI_MSI
321 case XEN_PCI_OP_enable_msi:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400322 op->err = xen_pcibk_enable_msi(pdev, dev, op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400323 break;
324 case XEN_PCI_OP_disable_msi:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400325 op->err = xen_pcibk_disable_msi(pdev, dev, op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400326 break;
327 case XEN_PCI_OP_enable_msix:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400328 op->err = xen_pcibk_enable_msix(pdev, dev, op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400329 break;
330 case XEN_PCI_OP_disable_msix:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400331 op->err = xen_pcibk_disable_msix(pdev, dev, op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400332 break;
333#endif
334 default:
335 op->err = XEN_PCI_ERR_not_implemented;
336 break;
337 }
338 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400339 if (!op->err && dev && dev_data) {
340 /* Transition detected */
341 if ((dev_data->enable_intx != test_intx))
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400342 xen_pcibk_control_isr(dev, 0 /* no reset */);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400343 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400344 /* Tell the driver domain that we're done. */
345 wmb();
346 clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
347 notify_remote_via_irq(pdev->evtchn_irq);
348
349 /* Mark that we're done. */
350 smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */
351 clear_bit(_PDEVF_op_active, &pdev->flags);
352 smp_mb__after_clear_bit(); /* /before/ final check for work */
353
354 /* Check to see if the driver domain tried to start another request in
355 * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
356 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400357 xen_pcibk_test_and_schedule_op(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400358}
359
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400360irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400361{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400362 struct xen_pcibk_device *pdev = dev_id;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400363
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400364 xen_pcibk_test_and_schedule_op(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400365
366 return IRQ_HANDLED;
367}
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400368static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400369{
370 struct pci_dev *dev = (struct pci_dev *)dev_id;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400371 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400372
373 if (dev_data->isr_on && dev_data->ack_intr) {
374 dev_data->handled++;
375 if ((dev_data->handled % 1000) == 0) {
376 if (xen_test_irq_shared(irq)) {
377 printk(KERN_INFO "%s IRQ line is not shared "
378 "with other domains. Turning ISR off\n",
379 dev_data->irq_name);
380 dev_data->ack_intr = 0;
381 }
382 }
383 return IRQ_HANDLED;
384 }
385 return IRQ_NONE;
386}