2 * PCI Backend Operations - respond to PCI requests from Frontend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
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>
14 module_param(verbose_request
, int, 0644);
16 /* Ensure a device is has the fake IRQ handler "turned on/off" and is
17 * ready to be exported. This MUST be run after pciback_reset_device
18 * which does the actual PCI device enable/disable.
20 void pciback_control_isr(struct pci_dev
*dev
, int reset
)
22 struct pciback_dev_data
*dev_data
;
26 dev_data
= pci_get_drvdata(dev
);
30 /* We don't deal with bridges */
31 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
)
35 dev_data
->enable_intx
= 0;
36 dev_data
->ack_intr
= 0;
38 enable
= dev_data
->enable_intx
;
40 /* Asked to disable, but ISR isn't runnig */
41 if (!enable
&& !dev_data
->isr_on
)
44 /* Squirrel away the IRQs in the dev_data. We need this
45 * b/c when device transitions to MSI, the dev->irq is
46 * overwritten with the MSI vector.
49 dev_data
->irq
= dev
->irq
;
52 * SR-IOV devices in all use MSI-X and have no legacy
53 * interrupts, so inhibit creating a fake IRQ handler for them.
55 if (dev_data
->irq
== 0)
58 dev_dbg(&dev
->dev
, "%s: #%d %s %s%s %s-> %s\n",
61 pci_is_enabled(dev
) ? "on" : "off",
62 dev
->msi_enabled
? "MSI" : "",
63 dev
->msix_enabled
? "MSI/X" : "",
64 dev_data
->isr_on
? "enable" : "disable",
65 enable
? "enable" : "disable");
68 rc
= request_irq(dev_data
->irq
,
69 pciback_guest_interrupt
, IRQF_SHARED
,
70 dev_data
->irq_name
, dev
);
72 dev_err(&dev
->dev
, "%s: failed to install fake IRQ " \
73 "handler for IRQ %d! (rc:%d)\n",
74 dev_data
->irq_name
, dev_data
->irq
, rc
);
78 free_irq(dev_data
->irq
, dev
);
81 dev_data
->isr_on
= enable
;
82 dev_data
->ack_intr
= enable
;
84 dev_dbg(&dev
->dev
, "%s: #%d %s %s%s %s\n",
87 pci_is_enabled(dev
) ? "on" : "off",
88 dev
->msi_enabled
? "MSI" : "",
89 dev
->msix_enabled
? "MSI/X" : "",
90 enable
? (dev_data
->isr_on
? "enabled" : "failed to enable") :
91 (dev_data
->isr_on
? "failed to disable" : "disabled"));
94 /* Ensure a device is "turned off" and ready to be exported.
95 * (Also see pciback_config_reset to ensure virtual configuration space is
96 * ready to be re-exported)
98 void pciback_reset_device(struct pci_dev
*dev
)
102 pciback_control_isr(dev
, 1 /* reset device */);
104 /* Disable devices (but not bridges) */
105 if (dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
) {
106 #ifdef CONFIG_PCI_MSI
107 /* The guest could have been abruptly killed without
108 * disabling MSI/MSI-X interrupts.*/
109 if (dev
->msix_enabled
)
110 pci_disable_msix(dev
);
111 if (dev
->msi_enabled
)
112 pci_disable_msi(dev
);
114 pci_disable_device(dev
);
116 pci_write_config_word(dev
, PCI_COMMAND
, 0);
118 dev
->is_busmaster
= 0;
120 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
121 if (cmd
& (PCI_COMMAND_INVALIDATE
)) {
122 cmd
&= ~(PCI_COMMAND_INVALIDATE
);
123 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
125 dev
->is_busmaster
= 0;
130 * Now the same evtchn is used for both pcifront conf_read_write request
131 * as well as pcie aer front end ack. We use a new work_queue to schedule
132 * pciback conf_read_write service for avoiding confict with aer_core
133 * do_recovery job which also use the system default work_queue
135 void test_and_schedule_op(struct pciback_device
*pdev
)
137 /* Check that frontend is requesting an operation and that we are not
138 * already processing a request */
139 if (test_bit(_XEN_PCIF_active
, (unsigned long *)&pdev
->sh_info
->flags
)
140 && !test_and_set_bit(_PDEVF_op_active
, &pdev
->flags
)) {
141 queue_work(pciback_wq
, &pdev
->op_work
);
143 /*_XEN_PCIB_active should have been cleared by pcifront. And also make
144 sure pciback is waiting for ack by checking _PCIB_op_pending*/
145 if (!test_bit(_XEN_PCIB_active
, (unsigned long *)&pdev
->sh_info
->flags
)
146 && test_bit(_PCIB_op_pending
, &pdev
->flags
)) {
147 wake_up(&aer_wait_queue
);
151 /* Performing the configuration space reads/writes must not be done in atomic
152 * context because some of the pci_* functions can sleep (mostly due to ACPI
153 * use of semaphores). This function is intended to be called from a work
154 * queue in process context taking a struct pciback_device as a parameter */
156 void pciback_do_op(struct work_struct
*data
)
158 struct pciback_device
*pdev
=
159 container_of(data
, struct pciback_device
, op_work
);
161 struct pciback_dev_data
*dev_data
= NULL
;
162 struct xen_pci_op
*op
= &pdev
->sh_info
->op
;
165 dev
= pciback_get_pci_dev(pdev
, op
->domain
, op
->bus
, op
->devfn
);
168 op
->err
= XEN_PCI_ERR_dev_not_found
;
170 dev_data
= pci_get_drvdata(dev
);
172 test_intx
= dev_data
->enable_intx
;
174 case XEN_PCI_OP_conf_read
:
175 op
->err
= pciback_config_read(dev
,
176 op
->offset
, op
->size
, &op
->value
);
178 case XEN_PCI_OP_conf_write
:
179 op
->err
= pciback_config_write(dev
,
180 op
->offset
, op
->size
, op
->value
);
182 #ifdef CONFIG_PCI_MSI
183 case XEN_PCI_OP_enable_msi
:
184 op
->err
= pciback_enable_msi(pdev
, dev
, op
);
186 case XEN_PCI_OP_disable_msi
:
187 op
->err
= pciback_disable_msi(pdev
, dev
, op
);
189 case XEN_PCI_OP_enable_msix
:
190 op
->err
= pciback_enable_msix(pdev
, dev
, op
);
192 case XEN_PCI_OP_disable_msix
:
193 op
->err
= pciback_disable_msix(pdev
, dev
, op
);
197 op
->err
= XEN_PCI_ERR_not_implemented
;
201 if (!op
->err
&& dev
&& dev_data
) {
202 /* Transition detected */
203 if ((dev_data
->enable_intx
!= test_intx
))
204 pciback_control_isr(dev
, 0 /* no reset */);
206 /* Tell the driver domain that we're done. */
208 clear_bit(_XEN_PCIF_active
, (unsigned long *)&pdev
->sh_info
->flags
);
209 notify_remote_via_irq(pdev
->evtchn_irq
);
211 /* Mark that we're done. */
212 smp_mb__before_clear_bit(); /* /after/ clearing PCIF_active */
213 clear_bit(_PDEVF_op_active
, &pdev
->flags
);
214 smp_mb__after_clear_bit(); /* /before/ final check for work */
216 /* Check to see if the driver domain tried to start another request in
217 * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
219 test_and_schedule_op(pdev
);
222 irqreturn_t
pciback_handle_event(int irq
, void *dev_id
)
224 struct pciback_device
*pdev
= dev_id
;
226 test_and_schedule_op(pdev
);
230 irqreturn_t
pciback_guest_interrupt(int irq
, void *dev_id
)
232 struct pci_dev
*dev
= (struct pci_dev
*)dev_id
;
233 struct pciback_dev_data
*dev_data
= pci_get_drvdata(dev
);
235 if (dev_data
->isr_on
&& dev_data
->ack_intr
) {
237 if ((dev_data
->handled
% 1000) == 0) {
238 if (xen_test_irq_shared(irq
)) {
239 printk(KERN_INFO
"%s IRQ line is not shared "
240 "with other domains. Turning ISR off\n",
242 dev_data
->ack_intr
= 0;