ixgbe: Fix race when the VF driver does a reset
[linux/fpc-iii.git] / drivers / xen / xen-pciback / pciback_ops.c
blobea4a08b83fa099ae04ed8486a6cff98a602f1e57
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Backend Operations - respond to PCI requests from Frontend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/moduleparam.h>
11 #include <linux/wait.h>
12 #include <linux/bitops.h>
13 #include <xen/events.h>
14 #include <linux/sched.h>
15 #include "pciback.h"
17 int verbose_request;
18 module_param(verbose_request, int, 0644);
20 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
22 /* Ensure a device is has the fake IRQ handler "turned on/off" and is
23 * ready to be exported. This MUST be run after xen_pcibk_reset_device
24 * which does the actual PCI device enable/disable.
26 static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
28 struct xen_pcibk_dev_data *dev_data;
29 int rc;
30 int enable = 0;
32 dev_data = pci_get_drvdata(dev);
33 if (!dev_data)
34 return;
36 /* We don't deal with bridges */
37 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
38 return;
40 if (reset) {
41 dev_data->enable_intx = 0;
42 dev_data->ack_intr = 0;
44 enable = dev_data->enable_intx;
46 /* Asked to disable, but ISR isn't runnig */
47 if (!enable && !dev_data->isr_on)
48 return;
50 /* Squirrel away the IRQs in the dev_data. We need this
51 * b/c when device transitions to MSI, the dev->irq is
52 * overwritten with the MSI vector.
54 if (enable)
55 dev_data->irq = dev->irq;
58 * SR-IOV devices in all use MSI-X and have no legacy
59 * interrupts, so inhibit creating a fake IRQ handler for them.
61 if (dev_data->irq == 0)
62 goto out;
64 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
65 dev_data->irq_name,
66 dev_data->irq,
67 pci_is_enabled(dev) ? "on" : "off",
68 dev->msi_enabled ? "MSI" : "",
69 dev->msix_enabled ? "MSI/X" : "",
70 dev_data->isr_on ? "enable" : "disable",
71 enable ? "enable" : "disable");
73 if (enable) {
75 * The MSI or MSI-X should not have an IRQ handler. Otherwise
76 * if the guest terminates we BUG_ON in free_msi_irqs.
78 if (dev->msi_enabled || dev->msix_enabled)
79 goto out;
81 rc = request_irq(dev_data->irq,
82 xen_pcibk_guest_interrupt, IRQF_SHARED,
83 dev_data->irq_name, dev);
84 if (rc) {
85 dev_err(&dev->dev, "%s: failed to install fake IRQ " \
86 "handler for IRQ %d! (rc:%d)\n",
87 dev_data->irq_name, dev_data->irq, rc);
88 goto out;
90 } else {
91 free_irq(dev_data->irq, dev);
92 dev_data->irq = 0;
94 dev_data->isr_on = enable;
95 dev_data->ack_intr = enable;
96 out:
97 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
98 dev_data->irq_name,
99 dev_data->irq,
100 pci_is_enabled(dev) ? "on" : "off",
101 dev->msi_enabled ? "MSI" : "",
102 dev->msix_enabled ? "MSI/X" : "",
103 enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
104 (dev_data->isr_on ? "failed to disable" : "disabled"));
107 /* Ensure a device is "turned off" and ready to be exported.
108 * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
109 * ready to be re-exported)
111 void xen_pcibk_reset_device(struct pci_dev *dev)
113 u16 cmd;
115 xen_pcibk_control_isr(dev, 1 /* reset device */);
117 /* Disable devices (but not bridges) */
118 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
119 #ifdef CONFIG_PCI_MSI
120 /* The guest could have been abruptly killed without
121 * disabling MSI/MSI-X interrupts.*/
122 if (dev->msix_enabled)
123 pci_disable_msix(dev);
124 if (dev->msi_enabled)
125 pci_disable_msi(dev);
126 #endif
127 if (pci_is_enabled(dev))
128 pci_disable_device(dev);
130 pci_write_config_word(dev, PCI_COMMAND, 0);
132 dev->is_busmaster = 0;
133 } else {
134 pci_read_config_word(dev, PCI_COMMAND, &cmd);
135 if (cmd & (PCI_COMMAND_INVALIDATE)) {
136 cmd &= ~(PCI_COMMAND_INVALIDATE);
137 pci_write_config_word(dev, PCI_COMMAND, cmd);
139 dev->is_busmaster = 0;
144 #ifdef CONFIG_PCI_MSI
145 static
146 int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
147 struct pci_dev *dev, struct xen_pci_op *op)
149 struct xen_pcibk_dev_data *dev_data;
150 int status;
152 if (unlikely(verbose_request))
153 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI\n", pci_name(dev));
155 if (dev->msi_enabled)
156 status = -EALREADY;
157 else if (dev->msix_enabled)
158 status = -ENXIO;
159 else
160 status = pci_enable_msi(dev);
162 if (status) {
163 pr_warn_ratelimited("%s: error enabling MSI for guest %u: err %d\n",
164 pci_name(dev), pdev->xdev->otherend_id,
165 status);
166 op->value = 0;
167 return XEN_PCI_ERR_op_failed;
170 /* The value the guest needs is actually the IDT vector, not the
171 * the local domain's IRQ number. */
173 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
174 if (unlikely(verbose_request))
175 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
176 op->value);
178 dev_data = pci_get_drvdata(dev);
179 if (dev_data)
180 dev_data->ack_intr = 0;
182 return 0;
185 static
186 int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
187 struct pci_dev *dev, struct xen_pci_op *op)
189 if (unlikely(verbose_request))
190 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI\n",
191 pci_name(dev));
193 if (dev->msi_enabled) {
194 struct xen_pcibk_dev_data *dev_data;
196 pci_disable_msi(dev);
198 dev_data = pci_get_drvdata(dev);
199 if (dev_data)
200 dev_data->ack_intr = 1;
202 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
203 if (unlikely(verbose_request))
204 printk(KERN_DEBUG DRV_NAME ": %s: MSI: %d\n", pci_name(dev),
205 op->value);
206 return 0;
209 static
210 int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
211 struct pci_dev *dev, struct xen_pci_op *op)
213 struct xen_pcibk_dev_data *dev_data;
214 int i, result;
215 struct msix_entry *entries;
216 u16 cmd;
218 if (unlikely(verbose_request))
219 printk(KERN_DEBUG DRV_NAME ": %s: enable MSI-X\n",
220 pci_name(dev));
222 if (op->value > SH_INFO_MAX_VEC)
223 return -EINVAL;
225 if (dev->msix_enabled)
226 return -EALREADY;
229 * PCI_COMMAND_MEMORY must be enabled, otherwise we may not be able
230 * to access the BARs where the MSI-X entries reside.
231 * But VF devices are unique in which the PF needs to be checked.
233 pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
234 if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
235 return -ENXIO;
237 entries = kmalloc_array(op->value, sizeof(*entries), GFP_KERNEL);
238 if (entries == NULL)
239 return -ENOMEM;
241 for (i = 0; i < op->value; i++) {
242 entries[i].entry = op->msix_entries[i].entry;
243 entries[i].vector = op->msix_entries[i].vector;
246 result = pci_enable_msix_exact(dev, entries, op->value);
247 if (result == 0) {
248 for (i = 0; i < op->value; i++) {
249 op->msix_entries[i].entry = entries[i].entry;
250 if (entries[i].vector) {
251 op->msix_entries[i].vector =
252 xen_pirq_from_irq(entries[i].vector);
253 if (unlikely(verbose_request))
254 printk(KERN_DEBUG DRV_NAME ": %s: " \
255 "MSI-X[%d]: %d\n",
256 pci_name(dev), i,
257 op->msix_entries[i].vector);
260 } else
261 pr_warn_ratelimited("%s: error enabling MSI-X for guest %u: err %d!\n",
262 pci_name(dev), pdev->xdev->otherend_id,
263 result);
264 kfree(entries);
266 op->value = result;
267 dev_data = pci_get_drvdata(dev);
268 if (dev_data)
269 dev_data->ack_intr = 0;
271 return result > 0 ? 0 : result;
274 static
275 int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
276 struct pci_dev *dev, struct xen_pci_op *op)
278 if (unlikely(verbose_request))
279 printk(KERN_DEBUG DRV_NAME ": %s: disable MSI-X\n",
280 pci_name(dev));
282 if (dev->msix_enabled) {
283 struct xen_pcibk_dev_data *dev_data;
285 pci_disable_msix(dev);
287 dev_data = pci_get_drvdata(dev);
288 if (dev_data)
289 dev_data->ack_intr = 1;
292 * SR-IOV devices (which don't have any legacy IRQ) have
293 * an undefined IRQ value of zero.
295 op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
296 if (unlikely(verbose_request))
297 printk(KERN_DEBUG DRV_NAME ": %s: MSI-X: %d\n",
298 pci_name(dev), op->value);
299 return 0;
301 #endif
303 * Now the same evtchn is used for both pcifront conf_read_write request
304 * as well as pcie aer front end ack. We use a new work_queue to schedule
305 * xen_pcibk conf_read_write service for avoiding confict with aer_core
306 * do_recovery job which also use the system default work_queue
308 void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
310 /* Check that frontend is requesting an operation and that we are not
311 * already processing a request */
312 if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
313 && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
314 schedule_work(&pdev->op_work);
316 /*_XEN_PCIB_active should have been cleared by pcifront. And also make
317 sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
318 if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
319 && test_bit(_PCIB_op_pending, &pdev->flags)) {
320 wake_up(&xen_pcibk_aer_wait_queue);
324 /* Performing the configuration space reads/writes must not be done in atomic
325 * context because some of the pci_* functions can sleep (mostly due to ACPI
326 * use of semaphores). This function is intended to be called from a work
327 * queue in process context taking a struct xen_pcibk_device as a parameter */
329 void xen_pcibk_do_op(struct work_struct *data)
331 struct xen_pcibk_device *pdev =
332 container_of(data, struct xen_pcibk_device, op_work);
333 struct pci_dev *dev;
334 struct xen_pcibk_dev_data *dev_data = NULL;
335 struct xen_pci_op *op = &pdev->op;
336 int test_intx = 0;
337 #ifdef CONFIG_PCI_MSI
338 unsigned int nr = 0;
339 #endif
341 *op = pdev->sh_info->op;
342 barrier();
343 dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
345 if (dev == NULL)
346 op->err = XEN_PCI_ERR_dev_not_found;
347 else {
348 dev_data = pci_get_drvdata(dev);
349 if (dev_data)
350 test_intx = dev_data->enable_intx;
351 switch (op->cmd) {
352 case XEN_PCI_OP_conf_read:
353 op->err = xen_pcibk_config_read(dev,
354 op->offset, op->size, &op->value);
355 break;
356 case XEN_PCI_OP_conf_write:
357 op->err = xen_pcibk_config_write(dev,
358 op->offset, op->size, op->value);
359 break;
360 #ifdef CONFIG_PCI_MSI
361 case XEN_PCI_OP_enable_msi:
362 op->err = xen_pcibk_enable_msi(pdev, dev, op);
363 break;
364 case XEN_PCI_OP_disable_msi:
365 op->err = xen_pcibk_disable_msi(pdev, dev, op);
366 break;
367 case XEN_PCI_OP_enable_msix:
368 nr = op->value;
369 op->err = xen_pcibk_enable_msix(pdev, dev, op);
370 break;
371 case XEN_PCI_OP_disable_msix:
372 op->err = xen_pcibk_disable_msix(pdev, dev, op);
373 break;
374 #endif
375 default:
376 op->err = XEN_PCI_ERR_not_implemented;
377 break;
380 if (!op->err && dev && dev_data) {
381 /* Transition detected */
382 if ((dev_data->enable_intx != test_intx))
383 xen_pcibk_control_isr(dev, 0 /* no reset */);
385 pdev->sh_info->op.err = op->err;
386 pdev->sh_info->op.value = op->value;
387 #ifdef CONFIG_PCI_MSI
388 if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
389 unsigned int i;
391 for (i = 0; i < nr; i++)
392 pdev->sh_info->op.msix_entries[i].vector =
393 op->msix_entries[i].vector;
395 #endif
396 /* Tell the driver domain that we're done. */
397 wmb();
398 clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
399 notify_remote_via_irq(pdev->evtchn_irq);
401 /* Mark that we're done. */
402 smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
403 clear_bit(_PDEVF_op_active, &pdev->flags);
404 smp_mb__after_atomic(); /* /before/ final check for work */
406 /* Check to see if the driver domain tried to start another request in
407 * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
409 xen_pcibk_test_and_schedule_op(pdev);
412 irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
414 struct xen_pcibk_device *pdev = dev_id;
416 xen_pcibk_test_and_schedule_op(pdev);
418 return IRQ_HANDLED;
420 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
422 struct pci_dev *dev = (struct pci_dev *)dev_id;
423 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
425 if (dev_data->isr_on && dev_data->ack_intr) {
426 dev_data->handled++;
427 if ((dev_data->handled % 1000) == 0) {
428 if (xen_test_irq_shared(irq)) {
429 pr_info("%s IRQ line is not shared "
430 "with other domains. Turning ISR off\n",
431 dev_data->irq_name);
432 dev_data->ack_intr = 0;
435 return IRQ_HANDLED;
437 return IRQ_NONE;