1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend Operations - respond to PCI requests from Frontend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
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>
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
;
32 dev_data
= pci_get_drvdata(dev
);
36 /* We don't deal with bridges */
37 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
)
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
)
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.
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)
64 dev_dbg(&dev
->dev
, "%s: #%d %s %s%s %s-> %s\n",
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");
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
)
81 rc
= request_irq(dev_data
->irq
,
82 xen_pcibk_guest_interrupt
, IRQF_SHARED
,
83 dev_data
->irq_name
, dev
);
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
);
91 free_irq(dev_data
->irq
, dev
);
94 dev_data
->isr_on
= enable
;
95 dev_data
->ack_intr
= enable
;
97 dev_dbg(&dev
->dev
, "%s: #%d %s %s%s %s\n",
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
)
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
);
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;
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
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
;
152 if (unlikely(verbose_request
))
153 printk(KERN_DEBUG DRV_NAME
": %s: enable MSI\n", pci_name(dev
));
155 if (dev
->msi_enabled
)
157 else if (dev
->msix_enabled
)
160 status
= pci_enable_msi(dev
);
163 pr_warn_ratelimited("%s: error enabling MSI for guest %u: err %d\n",
164 pci_name(dev
), pdev
->xdev
->otherend_id
,
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
),
178 dev_data
= pci_get_drvdata(dev
);
180 dev_data
->ack_intr
= 0;
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",
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
);
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
),
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
;
215 struct msix_entry
*entries
;
218 if (unlikely(verbose_request
))
219 printk(KERN_DEBUG DRV_NAME
": %s: enable MSI-X\n",
222 if (op
->value
> SH_INFO_MAX_VEC
)
225 if (dev
->msix_enabled
)
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
))
237 entries
= kmalloc_array(op
->value
, sizeof(*entries
), GFP_KERNEL
);
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
);
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: " \
257 op
->msix_entries
[i
].vector
);
261 pr_warn_ratelimited("%s: error enabling MSI-X for guest %u: err %d!\n",
262 pci_name(dev
), pdev
->xdev
->otherend_id
,
267 dev_data
= pci_get_drvdata(dev
);
269 dev_data
->ack_intr
= 0;
271 return result
> 0 ? 0 : result
;
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",
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
);
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
);
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
);
334 struct xen_pcibk_dev_data
*dev_data
= NULL
;
335 struct xen_pci_op
*op
= &pdev
->op
;
337 #ifdef CONFIG_PCI_MSI
341 *op
= pdev
->sh_info
->op
;
343 dev
= xen_pcibk_get_pci_dev(pdev
, op
->domain
, op
->bus
, op
->devfn
);
346 op
->err
= XEN_PCI_ERR_dev_not_found
;
348 dev_data
= pci_get_drvdata(dev
);
350 test_intx
= dev_data
->enable_intx
;
352 case XEN_PCI_OP_conf_read
:
353 op
->err
= xen_pcibk_config_read(dev
,
354 op
->offset
, op
->size
, &op
->value
);
356 case XEN_PCI_OP_conf_write
:
357 op
->err
= xen_pcibk_config_write(dev
,
358 op
->offset
, op
->size
, op
->value
);
360 #ifdef CONFIG_PCI_MSI
361 case XEN_PCI_OP_enable_msi
:
362 op
->err
= xen_pcibk_enable_msi(pdev
, dev
, op
);
364 case XEN_PCI_OP_disable_msi
:
365 op
->err
= xen_pcibk_disable_msi(pdev
, dev
, op
);
367 case XEN_PCI_OP_enable_msix
:
369 op
->err
= xen_pcibk_enable_msix(pdev
, dev
, op
);
371 case XEN_PCI_OP_disable_msix
:
372 op
->err
= xen_pcibk_disable_msix(pdev
, dev
, op
);
376 op
->err
= XEN_PCI_ERR_not_implemented
;
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) {
391 for (i
= 0; i
< nr
; i
++)
392 pdev
->sh_info
->op
.msix_entries
[i
].vector
=
393 op
->msix_entries
[i
].vector
;
396 /* Tell the driver domain that we're done. */
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
);
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
) {
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",
432 dev_data
->ack_intr
= 0;