blob: 28a2a558483168dfef4c7dfeb3575ee9ba688151 [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"
12
13int verbose_request;
14module_param(verbose_request, int, 0644);
15
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040016/* 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.
19 */
20void pciback_control_isr(struct pci_dev *dev, int reset)
21{
22 struct pciback_dev_data *dev_data;
23 int rc;
24 int enable = 0;
25
26 dev_data = pci_get_drvdata(dev);
27 if (!dev_data)
28 return;
29
30 /* We don't deal with bridges */
31 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
32 return;
33
34 if (reset) {
35 dev_data->enable_intx = 0;
36 dev_data->ack_intr = 0;
37 }
38 enable = dev_data->enable_intx;
39
40 /* Asked to disable, but ISR isn't runnig */
41 if (!enable && !dev_data->isr_on)
42 return;
43
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.
47 */
48 if (enable)
49 dev_data->irq = dev->irq;
50
Konrad Rzeszutek Wilke17ab352011-02-16 15:43:25 -050051 /*
52 * SR-IOV devices in all use MSI-X and have no legacy
53 * interrupts, so inhibit creating a fake IRQ handler for them.
54 */
55 if (dev_data->irq == 0)
56 goto out;
57
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -040058 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
59 dev_data->irq_name,
60 dev_data->irq,
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");
66
67 if (enable) {
68 rc = request_irq(dev_data->irq,
69 pciback_guest_interrupt, IRQF_SHARED,
70 dev_data->irq_name, dev);
71 if (rc) {
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);
75 goto out;
76 }
77 } else {
78 free_irq(dev_data->irq, dev);
79 dev_data->irq = 0;
80 }
81 dev_data->isr_on = enable;
82 dev_data->ack_intr = enable;
83out:
84 dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
85 dev_data->irq_name,
86 dev_data->irq,
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"));
92}
93
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040094/* 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)
97 */
98void pciback_reset_device(struct pci_dev *dev)
99{
100 u16 cmd;
101
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400102 pciback_control_isr(dev, 1 /* reset device */);
103
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400104 /* Disable devices (but not bridges) */
105 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
Konrad Rzeszutek Wilka2be65fd2010-03-03 13:38:43 -0500106#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);
113#endif
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400114 pci_disable_device(dev);
115
116 pci_write_config_word(dev, PCI_COMMAND, 0);
117
118 dev->is_busmaster = 0;
119 } else {
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);
124
125 dev->is_busmaster = 0;
126 }
127 }
128}
129/*
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
134*/
135void test_and_schedule_op(struct pciback_device *pdev)
136{
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);
142 }
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);
148 }
149}
150
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 */
155
156void pciback_do_op(struct work_struct *data)
157{
158 struct pciback_device *pdev =
159 container_of(data, struct pciback_device, op_work);
160 struct pci_dev *dev;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400161 struct pciback_dev_data *dev_data = NULL;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400162 struct xen_pci_op *op = &pdev->sh_info->op;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400163 int test_intx = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400164
165 dev = pciback_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
166
167 if (dev == NULL)
168 op->err = XEN_PCI_ERR_dev_not_found;
169 else {
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400170 dev_data = pci_get_drvdata(dev);
171 if (dev_data)
172 test_intx = dev_data->enable_intx;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400173 switch (op->cmd) {
174 case XEN_PCI_OP_conf_read:
175 op->err = pciback_config_read(dev,
176 op->offset, op->size, &op->value);
177 break;
178 case XEN_PCI_OP_conf_write:
179 op->err = pciback_config_write(dev,
180 op->offset, op->size, op->value);
181 break;
182#ifdef CONFIG_PCI_MSI
183 case XEN_PCI_OP_enable_msi:
184 op->err = pciback_enable_msi(pdev, dev, op);
185 break;
186 case XEN_PCI_OP_disable_msi:
187 op->err = pciback_disable_msi(pdev, dev, op);
188 break;
189 case XEN_PCI_OP_enable_msix:
190 op->err = pciback_enable_msix(pdev, dev, op);
191 break;
192 case XEN_PCI_OP_disable_msix:
193 op->err = pciback_disable_msix(pdev, dev, op);
194 break;
195#endif
196 default:
197 op->err = XEN_PCI_ERR_not_implemented;
198 break;
199 }
200 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400201 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 */);
205 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400206 /* Tell the driver domain that we're done. */
207 wmb();
208 clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
209 notify_remote_via_irq(pdev->evtchn_irq);
210
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 */
215
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.
218 */
219 test_and_schedule_op(pdev);
220}
221
222irqreturn_t pciback_handle_event(int irq, void *dev_id)
223{
224 struct pciback_device *pdev = dev_id;
225
226 test_and_schedule_op(pdev);
227
228 return IRQ_HANDLED;
229}
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400230irqreturn_t pciback_guest_interrupt(int irq, void *dev_id)
231{
232 struct pci_dev *dev = (struct pci_dev *)dev_id;
233 struct pciback_dev_data *dev_data = pci_get_drvdata(dev);
234
235 if (dev_data->isr_on && dev_data->ack_intr) {
236 dev_data->handled++;
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",
241 dev_data->irq_name);
242 dev_data->ack_intr = 0;
243 }
244 }
245 return IRQ_HANDLED;
246 }
247 return IRQ_NONE;
248}