1 /*
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 #include <linux/ratelimit.h>
13 #include <linux/printk.h>
14
15 int verbose_request;
16 module_param(verbose_request, int, 0644);
17
18 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
19
20 /* Ensure a device is has the fake IRQ handler "turned on/off" and is
21 * ready to be exported. This MUST be run after xen_pcibk_reset_device
22 * which does the actual PCI device enable/disable.
23 */
xen_pcibk_control_isr(struct pci_dev * dev,int reset)24 static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
25 {
26 struct xen_pcibk_dev_data *dev_data;
27 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
55 /*
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
62 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,
73 xen_pcibk_guest_interrupt, IRQF_SHARED,
74 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;
87 out:
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
98 /* Ensure a device is "turned off" and ready to be exported.
99 * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
100 * ready to be re-exported)
101 */
xen_pcibk_reset_device(struct pci_dev * dev)102 void xen_pcibk_reset_device(struct pci_dev *dev)
103 {
104 u16 cmd;
105
106 xen_pcibk_control_isr(dev, 1 /* reset device */);
107
108 /* Disable devices (but not bridges) */
109 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
110 #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
118 if (pci_is_enabled(dev))
119 pci_disable_device(dev);
120
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 }
134
135 #ifdef CONFIG_PCI_MSI
136 static
xen_pcibk_enable_msi(struct xen_pcibk_device * pdev,struct pci_dev * dev,struct xen_pci_op * op)137 int 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;
141 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) {
149 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);
152 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
171 static
xen_pcibk_disable_msi(struct xen_pcibk_device * pdev,struct pci_dev * dev,struct xen_pci_op * op)172 int 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
192 static
xen_pcibk_enable_msix(struct xen_pcibk_device * pdev,struct pci_dev * dev,struct xen_pci_op * op)193 int 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 }
229 } 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);
233 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
240 return result > 0 ? 0 : result;
241 }
242
243 static
xen_pcibk_disable_msix(struct xen_pcibk_device * pdev,struct pci_dev * dev,struct xen_pci_op * op)244 int 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
267 /*
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
270 * xen_pcibk conf_read_write service for avoiding confict with aer_core
271 * do_recovery job which also use the system default work_queue
272 */
xen_pcibk_test_and_schedule_op(struct xen_pcibk_device * pdev)273 void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
274 {
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)) {
279 queue_work(xen_pcibk_wq, &pdev->op_work);
280 }
281 /*_XEN_PCIB_active should have been cleared by pcifront. And also make
282 sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
283 if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
284 && test_bit(_PCIB_op_pending, &pdev->flags)) {
285 wake_up(&xen_pcibk_aer_wait_queue);
286 }
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
292 * queue in process context taking a struct xen_pcibk_device as a parameter */
293
xen_pcibk_do_op(struct work_struct * data)294 void xen_pcibk_do_op(struct work_struct *data)
295 {
296 struct xen_pcibk_device *pdev =
297 container_of(data, struct xen_pcibk_device, op_work);
298 struct pci_dev *dev;
299 struct xen_pcibk_dev_data *dev_data = NULL;
300 struct xen_pci_op *op = &pdev->sh_info->op;
301 int test_intx = 0;
302
303 dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
304
305 if (dev == NULL)
306 op->err = XEN_PCI_ERR_dev_not_found;
307 else {
308 dev_data = pci_get_drvdata(dev);
309 if (dev_data)
310 test_intx = dev_data->enable_intx;
311 switch (op->cmd) {
312 case XEN_PCI_OP_conf_read:
313 op->err = xen_pcibk_config_read(dev,
314 op->offset, op->size, &op->value);
315 break;
316 case XEN_PCI_OP_conf_write:
317 op->err = xen_pcibk_config_write(dev,
318 op->offset, op->size, op->value);
319 break;
320 #ifdef CONFIG_PCI_MSI
321 case XEN_PCI_OP_enable_msi:
322 op->err = xen_pcibk_enable_msi(pdev, dev, op);
323 break;
324 case XEN_PCI_OP_disable_msi:
325 op->err = xen_pcibk_disable_msi(pdev, dev, op);
326 break;
327 case XEN_PCI_OP_enable_msix:
328 op->err = xen_pcibk_enable_msix(pdev, dev, op);
329 break;
330 case XEN_PCI_OP_disable_msix:
331 op->err = xen_pcibk_disable_msix(pdev, dev, op);
332 break;
333 #endif
334 default:
335 op->err = XEN_PCI_ERR_not_implemented;
336 break;
337 }
338 }
339 if (!op->err && dev && dev_data) {
340 /* Transition detected */
341 if ((dev_data->enable_intx != test_intx))
342 xen_pcibk_control_isr(dev, 0 /* no reset */);
343 }
344 /* 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 */
357 xen_pcibk_test_and_schedule_op(pdev);
358 }
359
xen_pcibk_handle_event(int irq,void * dev_id)360 irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
361 {
362 struct xen_pcibk_device *pdev = dev_id;
363
364 xen_pcibk_test_and_schedule_op(pdev);
365
366 return IRQ_HANDLED;
367 }
xen_pcibk_guest_interrupt(int irq,void * dev_id)368 static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
369 {
370 struct pci_dev *dev = (struct pci_dev *)dev_id;
371 struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
372
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 }
387