Loading...
1/*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#define dev_fmt pr_fmt
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/rwsem.h>
14#include <linux/list.h>
15#include <linux/spinlock.h>
16#include <linux/kref.h>
17#include <linux/pci.h>
18#include <linux/wait.h>
19#include <linux/sched.h>
20#include <linux/atomic.h>
21#include <xen/events.h>
22#include <xen/pci.h>
23#include <xen/xen.h>
24#include <asm/xen/hypervisor.h>
25#include <xen/interface/physdev.h>
26#include "pciback.h"
27#include "conf_space.h"
28#include "conf_space_quirks.h"
29
30#define PCISTUB_DRIVER_NAME "pciback"
31
32static char *pci_devs_to_hide;
33wait_queue_head_t xen_pcibk_aer_wait_queue;
34/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
35* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
36*/
37static DECLARE_RWSEM(pcistub_sem);
38module_param_named(hide, pci_devs_to_hide, charp, 0444);
39
40struct pcistub_device_id {
41 struct list_head slot_list;
42 int domain;
43 unsigned char bus;
44 unsigned int devfn;
45};
46static LIST_HEAD(pcistub_device_ids);
47static DEFINE_SPINLOCK(device_ids_lock);
48
49struct pcistub_device {
50 struct kref kref;
51 struct list_head dev_list;
52 spinlock_t lock;
53
54 struct pci_dev *dev;
55 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
56};
57
58/* Access to pcistub_devices & seized_devices lists and the initialize_devices
59 * flag must be locked with pcistub_devices_lock
60 */
61static DEFINE_SPINLOCK(pcistub_devices_lock);
62static LIST_HEAD(pcistub_devices);
63
64/* wait for device_initcall before initializing our devices
65 * (see pcistub_init_devices_late)
66 */
67static int initialize_devices;
68static LIST_HEAD(seized_devices);
69
70static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
71{
72 struct pcistub_device *psdev;
73
74 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
75
76 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
77 if (!psdev)
78 return NULL;
79
80 psdev->dev = pci_dev_get(dev);
81 if (!psdev->dev) {
82 kfree(psdev);
83 return NULL;
84 }
85
86 kref_init(&psdev->kref);
87 spin_lock_init(&psdev->lock);
88
89 return psdev;
90}
91
92/* Don't call this directly as it's called by pcistub_device_put */
93static void pcistub_device_release(struct kref *kref)
94{
95 struct pcistub_device *psdev;
96 struct pci_dev *dev;
97 struct xen_pcibk_dev_data *dev_data;
98
99 psdev = container_of(kref, struct pcistub_device, kref);
100 dev = psdev->dev;
101 dev_data = pci_get_drvdata(dev);
102
103 dev_dbg(&dev->dev, "pcistub_device_release\n");
104
105 xen_unregister_device_domain_owner(dev);
106
107 /* Call the reset function which does not take lock as this
108 * is called from "unbind" which takes a device_lock mutex.
109 */
110 __pci_reset_function_locked(dev);
111 if (dev_data &&
112 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
113 dev_info(&dev->dev, "Could not reload PCI state\n");
114 else
115 pci_restore_state(dev);
116
117 if (dev->msix_cap) {
118 struct physdev_pci_device ppdev = {
119 .seg = pci_domain_nr(dev->bus),
120 .bus = dev->bus->number,
121 .devfn = dev->devfn
122 };
123 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
124 &ppdev);
125
126 if (err && err != -ENOSYS)
127 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
128 err);
129 }
130
131 /* Disable the device */
132 xen_pcibk_reset_device(dev);
133
134 kfree(dev_data);
135 pci_set_drvdata(dev, NULL);
136
137 /* Clean-up the device */
138 xen_pcibk_config_free_dyn_fields(dev);
139 xen_pcibk_config_free_dev(dev);
140
141 pci_clear_dev_assigned(dev);
142 pci_dev_put(dev);
143
144 kfree(psdev);
145}
146
147static inline void pcistub_device_get(struct pcistub_device *psdev)
148{
149 kref_get(&psdev->kref);
150}
151
152static inline void pcistub_device_put(struct pcistub_device *psdev)
153{
154 kref_put(&psdev->kref, pcistub_device_release);
155}
156
157static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
158 int slot, int func)
159{
160 struct pcistub_device *psdev;
161
162 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
163 if (psdev->dev != NULL
164 && domain == pci_domain_nr(psdev->dev->bus)
165 && bus == psdev->dev->bus->number
166 && slot == PCI_SLOT(psdev->dev->devfn)
167 && func == PCI_FUNC(psdev->dev->devfn)) {
168 return psdev;
169 }
170 }
171
172 return NULL;
173}
174
175static struct pcistub_device *pcistub_device_find(int domain, int bus,
176 int slot, int func)
177{
178 struct pcistub_device *psdev;
179 unsigned long flags;
180
181 spin_lock_irqsave(&pcistub_devices_lock, flags);
182
183 psdev = pcistub_device_find_locked(domain, bus, slot, func);
184 if (psdev)
185 pcistub_device_get(psdev);
186
187 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
188 return psdev;
189}
190
191static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
192 struct pcistub_device *psdev)
193{
194 struct pci_dev *pci_dev = NULL;
195 unsigned long flags;
196
197 spin_lock_irqsave(&psdev->lock, flags);
198 if (!psdev->pdev) {
199 psdev->pdev = pdev;
200 pci_dev = psdev->dev;
201 }
202 spin_unlock_irqrestore(&psdev->lock, flags);
203
204 if (pci_dev)
205 pcistub_device_get(psdev);
206
207 return pci_dev;
208}
209
210struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
211 int domain, int bus,
212 int slot, int func)
213{
214 struct pcistub_device *psdev;
215 struct pci_dev *found_dev = NULL;
216 unsigned long flags;
217
218 spin_lock_irqsave(&pcistub_devices_lock, flags);
219
220 psdev = pcistub_device_find_locked(domain, bus, slot, func);
221 if (psdev)
222 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
223
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 return found_dev;
226}
227
228struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
229 struct pci_dev *dev)
230{
231 struct pcistub_device *psdev;
232 struct pci_dev *found_dev = NULL;
233 unsigned long flags;
234
235 spin_lock_irqsave(&pcistub_devices_lock, flags);
236
237 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 if (psdev->dev == dev) {
239 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
240 break;
241 }
242 }
243
244 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
245 return found_dev;
246}
247
248/*
249 * Called when:
250 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
254 *
255 * As such we have to be careful.
256 *
257 * To make this easier, the caller has to hold the device lock.
258 */
259void pcistub_put_pci_dev(struct pci_dev *dev)
260{
261 struct pcistub_device *psdev, *found_psdev = NULL;
262 unsigned long flags;
263 struct xen_pcibk_dev_data *dev_data;
264 int ret;
265
266 spin_lock_irqsave(&pcistub_devices_lock, flags);
267
268 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 if (psdev->dev == dev) {
270 found_psdev = psdev;
271 break;
272 }
273 }
274
275 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
276 if (WARN_ON(!found_psdev))
277 return;
278
279 /*hold this lock for avoiding breaking link between
280 * pcistub and xen_pcibk when AER is in processing
281 */
282 down_write(&pcistub_sem);
283 /* Cleanup our device
284 * (so it's ready for the next domain)
285 */
286 device_lock_assert(&dev->dev);
287 __pci_reset_function_locked(dev);
288
289 dev_data = pci_get_drvdata(dev);
290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 if (!ret) {
292 /*
293 * The usual sequence is pci_save_state & pci_restore_state
294 * but the guest might have messed the configuration space up.
295 * Use the initial version (when device was bound to us).
296 */
297 pci_restore_state(dev);
298 } else
299 dev_info(&dev->dev, "Could not reload PCI state\n");
300 /* This disables the device. */
301 xen_pcibk_reset_device(dev);
302
303 /* And cleanup up our emulated fields. */
304 xen_pcibk_config_reset_dev(dev);
305 xen_pcibk_config_free_dyn_fields(dev);
306
307 dev_data->allow_interrupt_control = 0;
308
309 xen_unregister_device_domain_owner(dev);
310
311 spin_lock_irqsave(&found_psdev->lock, flags);
312 found_psdev->pdev = NULL;
313 spin_unlock_irqrestore(&found_psdev->lock, flags);
314
315 pcistub_device_put(found_psdev);
316 up_write(&pcistub_sem);
317}
318
319static int pcistub_match_one(struct pci_dev *dev,
320 struct pcistub_device_id *pdev_id)
321{
322 /* Match the specified device by domain, bus, slot, func and also if
323 * any of the device's parent bridges match.
324 */
325 for (; dev != NULL; dev = dev->bus->self) {
326 if (pci_domain_nr(dev->bus) == pdev_id->domain
327 && dev->bus->number == pdev_id->bus
328 && dev->devfn == pdev_id->devfn)
329 return 1;
330
331 /* Sometimes topmost bridge links to itself. */
332 if (dev == dev->bus->self)
333 break;
334 }
335
336 return 0;
337}
338
339static int pcistub_match(struct pci_dev *dev)
340{
341 struct pcistub_device_id *pdev_id;
342 unsigned long flags;
343 int found = 0;
344
345 spin_lock_irqsave(&device_ids_lock, flags);
346 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
347 if (pcistub_match_one(dev, pdev_id)) {
348 found = 1;
349 break;
350 }
351 }
352 spin_unlock_irqrestore(&device_ids_lock, flags);
353
354 return found;
355}
356
357static int pcistub_init_device(struct pci_dev *dev)
358{
359 struct xen_pcibk_dev_data *dev_data;
360 int err = 0;
361
362 dev_dbg(&dev->dev, "initializing...\n");
363
364 /* The PCI backend is not intended to be a module (or to work with
365 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
366 * would need to be called somewhere to free the memory allocated
367 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
368 */
369 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
370 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
371 if (!dev_data) {
372 err = -ENOMEM;
373 goto out;
374 }
375 pci_set_drvdata(dev, dev_data);
376
377 /*
378 * Setup name for fake IRQ handler. It will only be enabled
379 * once the device is turned on by the guest.
380 */
381 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
382
383 dev_dbg(&dev->dev, "initializing config\n");
384
385 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
386 err = xen_pcibk_config_init_dev(dev);
387 if (err)
388 goto out;
389
390 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
391 * must do this here because pcibios_enable_device may specify
392 * the pci device's true irq (and possibly its other resources)
393 * if they differ from what's in the configuration space.
394 * This makes the assumption that the device's resources won't
395 * change after this point (otherwise this code may break!)
396 */
397 dev_dbg(&dev->dev, "enabling device\n");
398 err = pci_enable_device(dev);
399 if (err)
400 goto config_release;
401
402 if (dev->msix_cap) {
403 struct physdev_pci_device ppdev = {
404 .seg = pci_domain_nr(dev->bus),
405 .bus = dev->bus->number,
406 .devfn = dev->devfn
407 };
408
409 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
410 if (err && err != -ENOSYS)
411 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
412 err);
413 }
414
415 /* We need the device active to save the state. */
416 dev_dbg(&dev->dev, "save state of device\n");
417 pci_save_state(dev);
418 dev_data->pci_saved_state = pci_store_saved_state(dev);
419 if (!dev_data->pci_saved_state)
420 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
421 else {
422 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
423 __pci_reset_function_locked(dev);
424 pci_restore_state(dev);
425 }
426 /* Now disable the device (this also ensures some private device
427 * data is setup before we export)
428 */
429 dev_dbg(&dev->dev, "reset device\n");
430 xen_pcibk_reset_device(dev);
431
432 pci_set_dev_assigned(dev);
433 return 0;
434
435config_release:
436 xen_pcibk_config_free_dev(dev);
437
438out:
439 pci_set_drvdata(dev, NULL);
440 kfree(dev_data);
441 return err;
442}
443
444/*
445 * Because some initialization still happens on
446 * devices during fs_initcall, we need to defer
447 * full initialization of our devices until
448 * device_initcall.
449 */
450static int __init pcistub_init_devices_late(void)
451{
452 struct pcistub_device *psdev;
453 unsigned long flags;
454 int err = 0;
455
456 spin_lock_irqsave(&pcistub_devices_lock, flags);
457
458 while (!list_empty(&seized_devices)) {
459 psdev = container_of(seized_devices.next,
460 struct pcistub_device, dev_list);
461 list_del(&psdev->dev_list);
462
463 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
464
465 err = pcistub_init_device(psdev->dev);
466 if (err) {
467 dev_err(&psdev->dev->dev,
468 "error %d initializing device\n", err);
469 kfree(psdev);
470 psdev = NULL;
471 }
472
473 spin_lock_irqsave(&pcistub_devices_lock, flags);
474
475 if (psdev)
476 list_add_tail(&psdev->dev_list, &pcistub_devices);
477 }
478
479 initialize_devices = 1;
480
481 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
482
483 return 0;
484}
485
486static void pcistub_device_id_add_list(struct pcistub_device_id *new,
487 int domain, int bus, unsigned int devfn)
488{
489 struct pcistub_device_id *pci_dev_id;
490 unsigned long flags;
491 int found = 0;
492
493 spin_lock_irqsave(&device_ids_lock, flags);
494
495 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
496 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
497 pci_dev_id->devfn == devfn) {
498 found = 1;
499 break;
500 }
501 }
502
503 if (!found) {
504 new->domain = domain;
505 new->bus = bus;
506 new->devfn = devfn;
507 list_add_tail(&new->slot_list, &pcistub_device_ids);
508 }
509
510 spin_unlock_irqrestore(&device_ids_lock, flags);
511
512 if (found)
513 kfree(new);
514}
515
516static int pcistub_seize(struct pci_dev *dev,
517 struct pcistub_device_id *pci_dev_id)
518{
519 struct pcistub_device *psdev;
520 unsigned long flags;
521 int err = 0;
522
523 psdev = pcistub_device_alloc(dev);
524 if (!psdev) {
525 kfree(pci_dev_id);
526 return -ENOMEM;
527 }
528
529 spin_lock_irqsave(&pcistub_devices_lock, flags);
530
531 if (initialize_devices) {
532 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
533
534 /* don't want irqs disabled when calling pcistub_init_device */
535 err = pcistub_init_device(psdev->dev);
536
537 spin_lock_irqsave(&pcistub_devices_lock, flags);
538
539 if (!err)
540 list_add(&psdev->dev_list, &pcistub_devices);
541 } else {
542 dev_dbg(&dev->dev, "deferring initialization\n");
543 list_add(&psdev->dev_list, &seized_devices);
544 }
545
546 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
547
548 if (err) {
549 kfree(pci_dev_id);
550 pcistub_device_put(psdev);
551 } else if (pci_dev_id)
552 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
553 dev->bus->number, dev->devfn);
554
555 return err;
556}
557
558/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
559 * other functions that take the sysfs lock. */
560static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
561{
562 int err = 0, match;
563 struct pcistub_device_id *pci_dev_id = NULL;
564
565 dev_dbg(&dev->dev, "probing...\n");
566
567 match = pcistub_match(dev);
568
569 if ((dev->driver_override &&
570 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
571 match) {
572
573 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
574 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
575 dev_err(&dev->dev, "can't export pci devices that "
576 "don't have a normal (0) or bridge (1) "
577 "header type!\n");
578 err = -ENODEV;
579 goto out;
580 }
581
582 if (!match) {
583 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
584 if (!pci_dev_id) {
585 err = -ENOMEM;
586 goto out;
587 }
588 }
589
590 dev_info(&dev->dev, "seizing device\n");
591 err = pcistub_seize(dev, pci_dev_id);
592 } else
593 /* Didn't find the device */
594 err = -ENODEV;
595
596out:
597 return err;
598}
599
600/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
601 * other functions that take the sysfs lock. */
602static void pcistub_remove(struct pci_dev *dev)
603{
604 struct pcistub_device *psdev, *found_psdev = NULL;
605 unsigned long flags;
606
607 dev_dbg(&dev->dev, "removing\n");
608
609 spin_lock_irqsave(&pcistub_devices_lock, flags);
610
611 xen_pcibk_config_quirk_release(dev);
612
613 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
614 if (psdev->dev == dev) {
615 found_psdev = psdev;
616 break;
617 }
618 }
619
620 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
621
622 if (found_psdev) {
623 dev_dbg(&dev->dev, "found device to remove %s\n",
624 found_psdev->pdev ? "- in-use" : "");
625
626 if (found_psdev->pdev) {
627 int domid = xen_find_device_domain_owner(dev);
628
629 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
630 pci_name(found_psdev->dev), domid);
631 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
632 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
633 dev_warn(&dev->dev, "****** to other drivers or domains\n");
634
635 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
636 * doing the FLR. */
637 xen_pcibk_release_pci_dev(found_psdev->pdev,
638 found_psdev->dev,
639 false /* caller holds the lock. */);
640 }
641
642 spin_lock_irqsave(&pcistub_devices_lock, flags);
643 list_del(&found_psdev->dev_list);
644 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
645
646 /* the final put for releasing from the list */
647 pcistub_device_put(found_psdev);
648 }
649}
650
651static const struct pci_device_id pcistub_ids[] = {
652 {
653 .vendor = PCI_ANY_ID,
654 .device = PCI_ANY_ID,
655 .subvendor = PCI_ANY_ID,
656 .subdevice = PCI_ANY_ID,
657 },
658 {0,},
659};
660
661#define PCI_NODENAME_MAX 40
662static void kill_domain_by_device(struct pcistub_device *psdev)
663{
664 struct xenbus_transaction xbt;
665 int err;
666 char nodename[PCI_NODENAME_MAX];
667
668 BUG_ON(!psdev);
669 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
670 psdev->pdev->xdev->otherend_id);
671
672again:
673 err = xenbus_transaction_start(&xbt);
674 if (err) {
675 dev_err(&psdev->dev->dev,
676 "error %d when start xenbus transaction\n", err);
677 return;
678 }
679 /*PV AER handlers will set this flag*/
680 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
681 err = xenbus_transaction_end(xbt, 0);
682 if (err) {
683 if (err == -EAGAIN)
684 goto again;
685 dev_err(&psdev->dev->dev,
686 "error %d when end xenbus transaction\n", err);
687 return;
688 }
689}
690
691/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
692 * backend need to have cooperation. In xen_pcibk, those steps will do similar
693 * jobs: send service request and waiting for front_end response.
694*/
695static pci_ers_result_t common_process(struct pcistub_device *psdev,
696 pci_channel_state_t state, int aer_cmd,
697 pci_ers_result_t result)
698{
699 pci_ers_result_t res = result;
700 struct xen_pcie_aer_op *aer_op;
701 struct xen_pcibk_device *pdev = psdev->pdev;
702 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
703 int ret;
704
705 /*with PV AER drivers*/
706 aer_op = &(sh_info->aer_op);
707 aer_op->cmd = aer_cmd ;
708 /*useful for error_detected callback*/
709 aer_op->err = state;
710 /*pcifront_end BDF*/
711 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
712 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
713 if (!ret) {
714 dev_err(&psdev->dev->dev, "failed to get pcifront device\n");
715 return PCI_ERS_RESULT_NONE;
716 }
717 wmb();
718
719 dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n",
720 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
721 /*local flag to mark there's aer request, xen_pcibk callback will use
722 * this flag to judge whether we need to check pci-front give aer
723 * service ack signal
724 */
725 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
726
727 /*It is possible that a pcifront conf_read_write ops request invokes
728 * the callback which cause the spurious execution of wake_up.
729 * Yet it is harmless and better than a spinlock here
730 */
731 set_bit(_XEN_PCIB_active,
732 (unsigned long *)&sh_info->flags);
733 wmb();
734 notify_remote_via_irq(pdev->evtchn_irq);
735
736 /* Enable IRQ to signal "request done". */
737 xen_pcibk_lateeoi(pdev, 0);
738
739 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
740 !(test_bit(_XEN_PCIB_active, (unsigned long *)
741 &sh_info->flags)), 300*HZ);
742
743 /* Enable IRQ for pcifront request if not already active. */
744 if (!test_bit(_PDEVF_op_active, &pdev->flags))
745 xen_pcibk_lateeoi(pdev, 0);
746
747 if (!ret) {
748 if (test_bit(_XEN_PCIB_active,
749 (unsigned long *)&sh_info->flags)) {
750 dev_err(&psdev->dev->dev,
751 "pcifront aer process not responding!\n");
752 clear_bit(_XEN_PCIB_active,
753 (unsigned long *)&sh_info->flags);
754 aer_op->err = PCI_ERS_RESULT_NONE;
755 return res;
756 }
757 }
758 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
759
760 res = (pci_ers_result_t)aer_op->err;
761 return res;
762}
763
764/*
765* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
766* of the device driver could provide this service, and then wait for pcifront
767* ack.
768* @dev: pointer to PCI devices
769* return value is used by aer_core do_recovery policy
770*/
771static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
772{
773 struct pcistub_device *psdev;
774 pci_ers_result_t result;
775
776 result = PCI_ERS_RESULT_RECOVERED;
777 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
778 dev->bus->number, dev->devfn);
779
780 down_write(&pcistub_sem);
781 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
782 dev->bus->number,
783 PCI_SLOT(dev->devfn),
784 PCI_FUNC(dev->devfn));
785
786 if (!psdev || !psdev->pdev) {
787 dev_err(&dev->dev, "device is not found/assigned\n");
788 goto end;
789 }
790
791 if (!psdev->pdev->sh_info) {
792 dev_err(&dev->dev, "device is not connected or owned"
793 " by HVM, kill it\n");
794 kill_domain_by_device(psdev);
795 goto end;
796 }
797
798 if (!test_bit(_XEN_PCIB_AERHANDLER,
799 (unsigned long *)&psdev->pdev->sh_info->flags)) {
800 dev_err(&dev->dev,
801 "guest with no AER driver should have been killed\n");
802 goto end;
803 }
804 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
805
806 if (result == PCI_ERS_RESULT_NONE ||
807 result == PCI_ERS_RESULT_DISCONNECT) {
808 dev_dbg(&dev->dev,
809 "No AER slot_reset service or disconnected!\n");
810 kill_domain_by_device(psdev);
811 }
812end:
813 if (psdev)
814 pcistub_device_put(psdev);
815 up_write(&pcistub_sem);
816 return result;
817
818}
819
820
821/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
822* in case of the device driver could provide this service, and then wait
823* for pcifront ack
824* @dev: pointer to PCI devices
825* return value is used by aer_core do_recovery policy
826*/
827
828static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
829{
830 struct pcistub_device *psdev;
831 pci_ers_result_t result;
832
833 result = PCI_ERS_RESULT_RECOVERED;
834 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
835 dev->bus->number, dev->devfn);
836
837 down_write(&pcistub_sem);
838 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
839 dev->bus->number,
840 PCI_SLOT(dev->devfn),
841 PCI_FUNC(dev->devfn));
842
843 if (!psdev || !psdev->pdev) {
844 dev_err(&dev->dev, "device is not found/assigned\n");
845 goto end;
846 }
847
848 if (!psdev->pdev->sh_info) {
849 dev_err(&dev->dev, "device is not connected or owned"
850 " by HVM, kill it\n");
851 kill_domain_by_device(psdev);
852 goto end;
853 }
854
855 if (!test_bit(_XEN_PCIB_AERHANDLER,
856 (unsigned long *)&psdev->pdev->sh_info->flags)) {
857 dev_err(&dev->dev,
858 "guest with no AER driver should have been killed\n");
859 goto end;
860 }
861 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
862
863 if (result == PCI_ERS_RESULT_NONE ||
864 result == PCI_ERS_RESULT_DISCONNECT) {
865 dev_dbg(&dev->dev,
866 "No AER mmio_enabled service or disconnected!\n");
867 kill_domain_by_device(psdev);
868 }
869end:
870 if (psdev)
871 pcistub_device_put(psdev);
872 up_write(&pcistub_sem);
873 return result;
874}
875
876/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
877* in case of the device driver could provide this service, and then wait
878* for pcifront ack.
879* @dev: pointer to PCI devices
880* @error: the current PCI connection state
881* return value is used by aer_core do_recovery policy
882*/
883
884static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
885 pci_channel_state_t error)
886{
887 struct pcistub_device *psdev;
888 pci_ers_result_t result;
889
890 result = PCI_ERS_RESULT_CAN_RECOVER;
891 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
892 dev->bus->number, dev->devfn);
893
894 down_write(&pcistub_sem);
895 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
896 dev->bus->number,
897 PCI_SLOT(dev->devfn),
898 PCI_FUNC(dev->devfn));
899
900 if (!psdev || !psdev->pdev) {
901 dev_err(&dev->dev, "device is not found/assigned\n");
902 goto end;
903 }
904
905 if (!psdev->pdev->sh_info) {
906 dev_err(&dev->dev, "device is not connected or owned"
907 " by HVM, kill it\n");
908 kill_domain_by_device(psdev);
909 goto end;
910 }
911
912 /*Guest owns the device yet no aer handler regiested, kill guest*/
913 if (!test_bit(_XEN_PCIB_AERHANDLER,
914 (unsigned long *)&psdev->pdev->sh_info->flags)) {
915 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
916 kill_domain_by_device(psdev);
917 goto end;
918 }
919 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
920
921 if (result == PCI_ERS_RESULT_NONE ||
922 result == PCI_ERS_RESULT_DISCONNECT) {
923 dev_dbg(&dev->dev,
924 "No AER error_detected service or disconnected!\n");
925 kill_domain_by_device(psdev);
926 }
927end:
928 if (psdev)
929 pcistub_device_put(psdev);
930 up_write(&pcistub_sem);
931 return result;
932}
933
934/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
935* in case of the device driver could provide this service, and then wait
936* for pcifront ack.
937* @dev: pointer to PCI devices
938*/
939
940static void xen_pcibk_error_resume(struct pci_dev *dev)
941{
942 struct pcistub_device *psdev;
943
944 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
945 dev->bus->number, dev->devfn);
946
947 down_write(&pcistub_sem);
948 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
949 dev->bus->number,
950 PCI_SLOT(dev->devfn),
951 PCI_FUNC(dev->devfn));
952
953 if (!psdev || !psdev->pdev) {
954 dev_err(&dev->dev, "device is not found/assigned\n");
955 goto end;
956 }
957
958 if (!psdev->pdev->sh_info) {
959 dev_err(&dev->dev, "device is not connected or owned"
960 " by HVM, kill it\n");
961 kill_domain_by_device(psdev);
962 goto end;
963 }
964
965 if (!test_bit(_XEN_PCIB_AERHANDLER,
966 (unsigned long *)&psdev->pdev->sh_info->flags)) {
967 dev_err(&dev->dev,
968 "guest with no AER driver should have been killed\n");
969 kill_domain_by_device(psdev);
970 goto end;
971 }
972 common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
973 PCI_ERS_RESULT_RECOVERED);
974end:
975 if (psdev)
976 pcistub_device_put(psdev);
977 up_write(&pcistub_sem);
978 return;
979}
980
981/*add xen_pcibk AER handling*/
982static const struct pci_error_handlers xen_pcibk_error_handler = {
983 .error_detected = xen_pcibk_error_detected,
984 .mmio_enabled = xen_pcibk_mmio_enabled,
985 .slot_reset = xen_pcibk_slot_reset,
986 .resume = xen_pcibk_error_resume,
987};
988
989/*
990 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
991 * for a normal device. I don't want it to be loaded automatically.
992 */
993
994static struct pci_driver xen_pcibk_pci_driver = {
995 /* The name should be xen_pciback, but until the tools are updated
996 * we will keep it as pciback. */
997 .name = PCISTUB_DRIVER_NAME,
998 .id_table = pcistub_ids,
999 .probe = pcistub_probe,
1000 .remove = pcistub_remove,
1001 .err_handler = &xen_pcibk_error_handler,
1002};
1003
1004static inline int str_to_slot(const char *buf, int *domain, int *bus,
1005 int *slot, int *func)
1006{
1007 int parsed = 0;
1008
1009 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1010 &parsed)) {
1011 case 3:
1012 *func = -1;
1013 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1014 break;
1015 case 2:
1016 *slot = *func = -1;
1017 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1018 break;
1019 }
1020 if (parsed && !buf[parsed])
1021 return 0;
1022
1023 /* try again without domain */
1024 *domain = 0;
1025 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1026 case 2:
1027 *func = -1;
1028 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1029 break;
1030 case 1:
1031 *slot = *func = -1;
1032 sscanf(buf, " %x:*.* %n", bus, &parsed);
1033 break;
1034 }
1035 if (parsed && !buf[parsed])
1036 return 0;
1037
1038 return -EINVAL;
1039}
1040
1041static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1042 *slot, int *func, int *reg, int *size, int *mask)
1043{
1044 int parsed = 0;
1045
1046 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1047 reg, size, mask, &parsed);
1048 if (parsed && !buf[parsed])
1049 return 0;
1050
1051 /* try again without domain */
1052 *domain = 0;
1053 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1054 mask, &parsed);
1055 if (parsed && !buf[parsed])
1056 return 0;
1057
1058 return -EINVAL;
1059}
1060
1061static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1062{
1063 struct pcistub_device_id *pci_dev_id;
1064 int rc = 0, devfn = PCI_DEVFN(slot, func);
1065
1066 if (slot < 0) {
1067 for (slot = 0; !rc && slot < 32; ++slot)
1068 rc = pcistub_device_id_add(domain, bus, slot, func);
1069 return rc;
1070 }
1071
1072 if (func < 0) {
1073 for (func = 0; !rc && func < 8; ++func)
1074 rc = pcistub_device_id_add(domain, bus, slot, func);
1075 return rc;
1076 }
1077
1078 if ((
1079#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1080 || !defined(CONFIG_PCI_DOMAINS)
1081 !pci_domains_supported ? domain :
1082#endif
1083 domain < 0 || domain > 0xffff)
1084 || bus < 0 || bus > 0xff
1085 || PCI_SLOT(devfn) != slot
1086 || PCI_FUNC(devfn) != func)
1087 return -EINVAL;
1088
1089 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1090 if (!pci_dev_id)
1091 return -ENOMEM;
1092
1093 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1094 domain, bus, slot, func);
1095
1096 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1097
1098 return 0;
1099}
1100
1101static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1102{
1103 struct pcistub_device_id *pci_dev_id, *t;
1104 int err = -ENOENT;
1105 unsigned long flags;
1106
1107 spin_lock_irqsave(&device_ids_lock, flags);
1108 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1109 slot_list) {
1110 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1111 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1112 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1113 /* Don't break; here because it's possible the same
1114 * slot could be in the list more than once
1115 */
1116 list_del(&pci_dev_id->slot_list);
1117 kfree(pci_dev_id);
1118
1119 err = 0;
1120
1121 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1122 domain, bus, slot, func);
1123 }
1124 }
1125 spin_unlock_irqrestore(&device_ids_lock, flags);
1126
1127 return err;
1128}
1129
1130static int pcistub_reg_add(int domain, int bus, int slot, int func,
1131 unsigned int reg, unsigned int size,
1132 unsigned int mask)
1133{
1134 int err = 0;
1135 struct pcistub_device *psdev;
1136 struct pci_dev *dev;
1137 struct config_field *field;
1138
1139 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1140 return -EINVAL;
1141
1142 psdev = pcistub_device_find(domain, bus, slot, func);
1143 if (!psdev) {
1144 err = -ENODEV;
1145 goto out;
1146 }
1147 dev = psdev->dev;
1148
1149 field = kzalloc(sizeof(*field), GFP_KERNEL);
1150 if (!field) {
1151 err = -ENOMEM;
1152 goto out;
1153 }
1154
1155 field->offset = reg;
1156 field->size = size;
1157 field->mask = mask;
1158 field->init = NULL;
1159 field->reset = NULL;
1160 field->release = NULL;
1161 field->clean = xen_pcibk_config_field_free;
1162
1163 err = xen_pcibk_config_quirks_add_field(dev, field);
1164 if (err)
1165 kfree(field);
1166out:
1167 if (psdev)
1168 pcistub_device_put(psdev);
1169 return err;
1170}
1171
1172static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1173 size_t count)
1174{
1175 int domain, bus, slot, func;
1176 int err;
1177
1178 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1179 if (err)
1180 goto out;
1181
1182 err = pcistub_device_id_add(domain, bus, slot, func);
1183
1184out:
1185 if (!err)
1186 err = count;
1187 return err;
1188}
1189static DRIVER_ATTR_WO(new_slot);
1190
1191static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1192 size_t count)
1193{
1194 int domain, bus, slot, func;
1195 int err;
1196
1197 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1198 if (err)
1199 goto out;
1200
1201 err = pcistub_device_id_remove(domain, bus, slot, func);
1202
1203out:
1204 if (!err)
1205 err = count;
1206 return err;
1207}
1208static DRIVER_ATTR_WO(remove_slot);
1209
1210static ssize_t slots_show(struct device_driver *drv, char *buf)
1211{
1212 struct pcistub_device_id *pci_dev_id;
1213 size_t count = 0;
1214 unsigned long flags;
1215
1216 spin_lock_irqsave(&device_ids_lock, flags);
1217 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1218 if (count >= PAGE_SIZE)
1219 break;
1220
1221 count += scnprintf(buf + count, PAGE_SIZE - count,
1222 "%04x:%02x:%02x.%d\n",
1223 pci_dev_id->domain, pci_dev_id->bus,
1224 PCI_SLOT(pci_dev_id->devfn),
1225 PCI_FUNC(pci_dev_id->devfn));
1226 }
1227 spin_unlock_irqrestore(&device_ids_lock, flags);
1228
1229 return count;
1230}
1231static DRIVER_ATTR_RO(slots);
1232
1233static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1234{
1235 struct pcistub_device *psdev;
1236 struct xen_pcibk_dev_data *dev_data;
1237 size_t count = 0;
1238 unsigned long flags;
1239
1240 spin_lock_irqsave(&pcistub_devices_lock, flags);
1241 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1242 if (count >= PAGE_SIZE)
1243 break;
1244 if (!psdev->dev)
1245 continue;
1246 dev_data = pci_get_drvdata(psdev->dev);
1247 if (!dev_data)
1248 continue;
1249 count +=
1250 scnprintf(buf + count, PAGE_SIZE - count,
1251 "%s:%s:%sing:%ld\n",
1252 pci_name(psdev->dev),
1253 dev_data->isr_on ? "on" : "off",
1254 dev_data->ack_intr ? "ack" : "not ack",
1255 dev_data->handled);
1256 }
1257 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1258 return count;
1259}
1260static DRIVER_ATTR_RO(irq_handlers);
1261
1262static ssize_t irq_handler_state_store(struct device_driver *drv,
1263 const char *buf, size_t count)
1264{
1265 struct pcistub_device *psdev;
1266 struct xen_pcibk_dev_data *dev_data;
1267 int domain, bus, slot, func;
1268 int err;
1269
1270 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1271 if (err)
1272 return err;
1273
1274 psdev = pcistub_device_find(domain, bus, slot, func);
1275 if (!psdev) {
1276 err = -ENOENT;
1277 goto out;
1278 }
1279
1280 dev_data = pci_get_drvdata(psdev->dev);
1281 if (!dev_data) {
1282 err = -ENOENT;
1283 goto out;
1284 }
1285
1286 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1287 dev_data->irq_name, dev_data->isr_on,
1288 !dev_data->isr_on);
1289
1290 dev_data->isr_on = !(dev_data->isr_on);
1291 if (dev_data->isr_on)
1292 dev_data->ack_intr = 1;
1293out:
1294 if (psdev)
1295 pcistub_device_put(psdev);
1296 if (!err)
1297 err = count;
1298 return err;
1299}
1300static DRIVER_ATTR_WO(irq_handler_state);
1301
1302static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1303 size_t count)
1304{
1305 int domain, bus, slot, func, reg, size, mask;
1306 int err;
1307
1308 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1309 &mask);
1310 if (err)
1311 goto out;
1312
1313 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1314
1315out:
1316 if (!err)
1317 err = count;
1318 return err;
1319}
1320
1321static ssize_t quirks_show(struct device_driver *drv, char *buf)
1322{
1323 int count = 0;
1324 unsigned long flags;
1325 struct xen_pcibk_config_quirk *quirk;
1326 struct xen_pcibk_dev_data *dev_data;
1327 const struct config_field *field;
1328 const struct config_field_entry *cfg_entry;
1329
1330 spin_lock_irqsave(&device_ids_lock, flags);
1331 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1332 if (count >= PAGE_SIZE)
1333 goto out;
1334
1335 count += scnprintf(buf + count, PAGE_SIZE - count,
1336 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1337 quirk->pdev->bus->number,
1338 PCI_SLOT(quirk->pdev->devfn),
1339 PCI_FUNC(quirk->pdev->devfn),
1340 quirk->devid.vendor, quirk->devid.device,
1341 quirk->devid.subvendor,
1342 quirk->devid.subdevice);
1343
1344 dev_data = pci_get_drvdata(quirk->pdev);
1345
1346 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1347 field = cfg_entry->field;
1348 if (count >= PAGE_SIZE)
1349 goto out;
1350
1351 count += scnprintf(buf + count, PAGE_SIZE - count,
1352 "\t\t%08x:%01x:%08x\n",
1353 cfg_entry->base_offset +
1354 field->offset, field->size,
1355 field->mask);
1356 }
1357 }
1358
1359out:
1360 spin_unlock_irqrestore(&device_ids_lock, flags);
1361
1362 return count;
1363}
1364static DRIVER_ATTR_RW(quirks);
1365
1366static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1367 size_t count)
1368{
1369 int domain, bus, slot, func;
1370 int err;
1371 struct pcistub_device *psdev;
1372 struct xen_pcibk_dev_data *dev_data;
1373
1374 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1375 if (err)
1376 goto out;
1377
1378 psdev = pcistub_device_find(domain, bus, slot, func);
1379 if (!psdev) {
1380 err = -ENODEV;
1381 goto out;
1382 }
1383
1384 dev_data = pci_get_drvdata(psdev->dev);
1385 /* the driver data for a device should never be null at this point */
1386 if (!dev_data) {
1387 err = -ENXIO;
1388 goto release;
1389 }
1390 if (!dev_data->permissive) {
1391 dev_data->permissive = 1;
1392 /* Let user know that what they're doing could be unsafe */
1393 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1394 "configuration space accesses!\n");
1395 dev_warn(&psdev->dev->dev,
1396 "permissive mode is potentially unsafe!\n");
1397 }
1398release:
1399 pcistub_device_put(psdev);
1400out:
1401 if (!err)
1402 err = count;
1403 return err;
1404}
1405
1406static ssize_t permissive_show(struct device_driver *drv, char *buf)
1407{
1408 struct pcistub_device *psdev;
1409 struct xen_pcibk_dev_data *dev_data;
1410 size_t count = 0;
1411 unsigned long flags;
1412 spin_lock_irqsave(&pcistub_devices_lock, flags);
1413 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1414 if (count >= PAGE_SIZE)
1415 break;
1416 if (!psdev->dev)
1417 continue;
1418 dev_data = pci_get_drvdata(psdev->dev);
1419 if (!dev_data || !dev_data->permissive)
1420 continue;
1421 count +=
1422 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1423 pci_name(psdev->dev));
1424 }
1425 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1426 return count;
1427}
1428static DRIVER_ATTR_RW(permissive);
1429
1430static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1431 const char *buf, size_t count)
1432{
1433 int domain, bus, slot, func;
1434 int err;
1435 struct pcistub_device *psdev;
1436 struct xen_pcibk_dev_data *dev_data;
1437
1438 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1439 if (err)
1440 goto out;
1441
1442 psdev = pcistub_device_find(domain, bus, slot, func);
1443 if (!psdev) {
1444 err = -ENODEV;
1445 goto out;
1446 }
1447
1448 dev_data = pci_get_drvdata(psdev->dev);
1449 /* the driver data for a device should never be null at this point */
1450 if (!dev_data) {
1451 err = -ENXIO;
1452 goto release;
1453 }
1454 dev_data->allow_interrupt_control = 1;
1455release:
1456 pcistub_device_put(psdev);
1457out:
1458 if (!err)
1459 err = count;
1460 return err;
1461}
1462
1463static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1464 char *buf)
1465{
1466 struct pcistub_device *psdev;
1467 struct xen_pcibk_dev_data *dev_data;
1468 size_t count = 0;
1469 unsigned long flags;
1470
1471 spin_lock_irqsave(&pcistub_devices_lock, flags);
1472 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1473 if (count >= PAGE_SIZE)
1474 break;
1475 if (!psdev->dev)
1476 continue;
1477 dev_data = pci_get_drvdata(psdev->dev);
1478 if (!dev_data || !dev_data->allow_interrupt_control)
1479 continue;
1480 count +=
1481 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1482 pci_name(psdev->dev));
1483 }
1484 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1485 return count;
1486}
1487static DRIVER_ATTR_RW(allow_interrupt_control);
1488
1489static void pcistub_exit(void)
1490{
1491 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1492 driver_remove_file(&xen_pcibk_pci_driver.driver,
1493 &driver_attr_remove_slot);
1494 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1495 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1496 driver_remove_file(&xen_pcibk_pci_driver.driver,
1497 &driver_attr_permissive);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_allow_interrupt_control);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver,
1501 &driver_attr_irq_handlers);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_irq_handler_state);
1504 pci_unregister_driver(&xen_pcibk_pci_driver);
1505}
1506
1507static int __init pcistub_init(void)
1508{
1509 int pos = 0;
1510 int err = 0;
1511 int domain, bus, slot, func;
1512 int parsed;
1513
1514 if (pci_devs_to_hide && *pci_devs_to_hide) {
1515 do {
1516 parsed = 0;
1517
1518 err = sscanf(pci_devs_to_hide + pos,
1519 " (%x:%x:%x.%x) %n",
1520 &domain, &bus, &slot, &func, &parsed);
1521 switch (err) {
1522 case 3:
1523 func = -1;
1524 sscanf(pci_devs_to_hide + pos,
1525 " (%x:%x:%x.*) %n",
1526 &domain, &bus, &slot, &parsed);
1527 break;
1528 case 2:
1529 slot = func = -1;
1530 sscanf(pci_devs_to_hide + pos,
1531 " (%x:%x:*.*) %n",
1532 &domain, &bus, &parsed);
1533 break;
1534 }
1535
1536 if (!parsed) {
1537 domain = 0;
1538 err = sscanf(pci_devs_to_hide + pos,
1539 " (%x:%x.%x) %n",
1540 &bus, &slot, &func, &parsed);
1541 switch (err) {
1542 case 2:
1543 func = -1;
1544 sscanf(pci_devs_to_hide + pos,
1545 " (%x:%x.*) %n",
1546 &bus, &slot, &parsed);
1547 break;
1548 case 1:
1549 slot = func = -1;
1550 sscanf(pci_devs_to_hide + pos,
1551 " (%x:*.*) %n",
1552 &bus, &parsed);
1553 break;
1554 }
1555 }
1556
1557 if (parsed <= 0)
1558 goto parse_error;
1559
1560 err = pcistub_device_id_add(domain, bus, slot, func);
1561 if (err)
1562 goto out;
1563
1564 pos += parsed;
1565 } while (pci_devs_to_hide[pos]);
1566 }
1567
1568 /* If we're the first PCI Device Driver to register, we're the
1569 * first one to get offered PCI devices as they become
1570 * available (and thus we can be the first to grab them)
1571 */
1572 err = pci_register_driver(&xen_pcibk_pci_driver);
1573 if (err < 0)
1574 goto out;
1575
1576 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1577 &driver_attr_new_slot);
1578 if (!err)
1579 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1580 &driver_attr_remove_slot);
1581 if (!err)
1582 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1583 &driver_attr_slots);
1584 if (!err)
1585 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1586 &driver_attr_quirks);
1587 if (!err)
1588 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1589 &driver_attr_permissive);
1590 if (!err)
1591 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1592 &driver_attr_allow_interrupt_control);
1593
1594 if (!err)
1595 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1596 &driver_attr_irq_handlers);
1597 if (!err)
1598 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1599 &driver_attr_irq_handler_state);
1600 if (err)
1601 pcistub_exit();
1602
1603out:
1604 return err;
1605
1606parse_error:
1607 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1608 pci_devs_to_hide + pos);
1609 return -EINVAL;
1610}
1611
1612#ifndef MODULE
1613/*
1614 * fs_initcall happens before device_initcall
1615 * so xen_pcibk *should* get called first (b/c we
1616 * want to suck up any device before other drivers
1617 * get a chance by being the first pci device
1618 * driver to register)
1619 */
1620fs_initcall(pcistub_init);
1621#endif
1622
1623#ifdef CONFIG_PCI_IOV
1624static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1625{
1626 struct pcistub_device *psdev = NULL;
1627 unsigned long flags;
1628 bool found = false;
1629
1630 spin_lock_irqsave(&pcistub_devices_lock, flags);
1631 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1632 if (!psdev->pdev && psdev->dev != pdev
1633 && pci_physfn(psdev->dev) == pdev) {
1634 found = true;
1635 break;
1636 }
1637 }
1638 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1639 if (found)
1640 return psdev;
1641 return NULL;
1642}
1643
1644static int pci_stub_notifier(struct notifier_block *nb,
1645 unsigned long action, void *data)
1646{
1647 struct device *dev = data;
1648 const struct pci_dev *pdev = to_pci_dev(dev);
1649
1650 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1651 return NOTIFY_DONE;
1652
1653 if (!pdev->is_physfn)
1654 return NOTIFY_DONE;
1655
1656 for (;;) {
1657 struct pcistub_device *psdev = find_vfs(pdev);
1658 if (!psdev)
1659 break;
1660 device_release_driver(&psdev->dev->dev);
1661 }
1662 return NOTIFY_DONE;
1663}
1664
1665static struct notifier_block pci_stub_nb = {
1666 .notifier_call = pci_stub_notifier,
1667};
1668#endif
1669
1670static int __init xen_pcibk_init(void)
1671{
1672 int err;
1673
1674 if (!xen_initial_domain())
1675 return -ENODEV;
1676
1677 err = xen_pcibk_config_init();
1678 if (err)
1679 return err;
1680
1681#ifdef MODULE
1682 err = pcistub_init();
1683 if (err < 0)
1684 return err;
1685#endif
1686
1687 pcistub_init_devices_late();
1688 err = xen_pcibk_xenbus_register();
1689 if (err)
1690 pcistub_exit();
1691#ifdef CONFIG_PCI_IOV
1692 else
1693 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1694#endif
1695
1696 return err;
1697}
1698
1699static void __exit xen_pcibk_cleanup(void)
1700{
1701#ifdef CONFIG_PCI_IOV
1702 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1703#endif
1704 xen_pcibk_xenbus_unregister();
1705 pcistub_exit();
1706}
1707
1708module_init(xen_pcibk_init);
1709module_exit(xen_pcibk_cleanup);
1710
1711MODULE_LICENSE("Dual BSD/GPL");
1712MODULE_ALIAS("xen-backend:pci");
1/*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#define dev_fmt pr_fmt
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/rwsem.h>
14#include <linux/list.h>
15#include <linux/spinlock.h>
16#include <linux/kref.h>
17#include <linux/pci.h>
18#include <linux/wait.h>
19#include <linux/sched.h>
20#include <linux/atomic.h>
21#include <xen/events.h>
22#include <xen/pci.h>
23#include <xen/xen.h>
24#ifdef CONFIG_XEN_ACPI
25#include <xen/acpi.h>
26#endif
27#include <asm/xen/hypervisor.h>
28#include <xen/interface/physdev.h>
29#include "pciback.h"
30#include "conf_space.h"
31#include "conf_space_quirks.h"
32
33#define PCISTUB_DRIVER_NAME "pciback"
34
35static char *pci_devs_to_hide;
36wait_queue_head_t xen_pcibk_aer_wait_queue;
37/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
38* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
39*/
40static DECLARE_RWSEM(pcistub_sem);
41module_param_named(hide, pci_devs_to_hide, charp, 0444);
42
43struct pcistub_device_id {
44 struct list_head slot_list;
45 int domain;
46 unsigned char bus;
47 unsigned int devfn;
48};
49static LIST_HEAD(pcistub_device_ids);
50static DEFINE_SPINLOCK(device_ids_lock);
51
52struct pcistub_device {
53 struct kref kref;
54 struct list_head dev_list;
55 spinlock_t lock;
56
57 struct pci_dev *dev;
58 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
59#ifdef CONFIG_XEN_ACPI
60 int gsi;
61#endif
62};
63
64/* Access to pcistub_devices & seized_devices lists and the initialize_devices
65 * flag must be locked with pcistub_devices_lock
66 */
67static DEFINE_SPINLOCK(pcistub_devices_lock);
68static LIST_HEAD(pcistub_devices);
69
70/* wait for device_initcall before initializing our devices
71 * (see pcistub_init_devices_late)
72 */
73static int initialize_devices;
74static LIST_HEAD(seized_devices);
75
76static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
77{
78 struct pcistub_device *psdev;
79
80 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
81
82 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
83 if (!psdev)
84 return NULL;
85
86 psdev->dev = pci_dev_get(dev);
87 if (!psdev->dev) {
88 kfree(psdev);
89 return NULL;
90 }
91
92 kref_init(&psdev->kref);
93 spin_lock_init(&psdev->lock);
94#ifdef CONFIG_XEN_ACPI
95 psdev->gsi = -1;
96#endif
97
98 return psdev;
99}
100
101static int pcistub_reset_device_state(struct pci_dev *dev)
102{
103 __pci_reset_function_locked(dev);
104
105 if (!xen_pv_domain())
106 return xen_reset_device(dev);
107 else
108 return 0;
109}
110
111/* Don't call this directly as it's called by pcistub_device_put */
112static void pcistub_device_release(struct kref *kref)
113{
114 struct pcistub_device *psdev;
115 struct pci_dev *dev;
116 struct xen_pcibk_dev_data *dev_data;
117
118 psdev = container_of(kref, struct pcistub_device, kref);
119 dev = psdev->dev;
120 dev_data = pci_get_drvdata(dev);
121
122 dev_dbg(&dev->dev, "pcistub_device_release\n");
123
124 xen_unregister_device_domain_owner(dev);
125
126 /* Call the reset function which does not take lock as this
127 * is called from "unbind" which takes a device_lock mutex.
128 */
129 pcistub_reset_device_state(dev);
130 if (dev_data &&
131 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
132 dev_info(&dev->dev, "Could not reload PCI state\n");
133 else
134 pci_restore_state(dev);
135
136 if (dev->msix_cap) {
137 struct physdev_pci_device ppdev = {
138 .seg = pci_domain_nr(dev->bus),
139 .bus = dev->bus->number,
140 .devfn = dev->devfn
141 };
142 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
143 &ppdev);
144
145 if (err && err != -ENOSYS)
146 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
147 err);
148 }
149
150 /* Disable the device */
151 xen_pcibk_reset_device(dev);
152
153 kfree(dev_data);
154 pci_set_drvdata(dev, NULL);
155
156 /* Clean-up the device */
157 xen_pcibk_config_free_dyn_fields(dev);
158 xen_pcibk_config_free_dev(dev);
159
160 pci_clear_dev_assigned(dev);
161 pci_dev_put(dev);
162
163 kfree(psdev);
164}
165
166static inline void pcistub_device_get(struct pcistub_device *psdev)
167{
168 kref_get(&psdev->kref);
169}
170
171static inline void pcistub_device_put(struct pcistub_device *psdev)
172{
173 kref_put(&psdev->kref, pcistub_device_release);
174}
175
176static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
177 int slot, int func)
178{
179 struct pcistub_device *psdev;
180
181 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
182 if (psdev->dev != NULL
183 && domain == pci_domain_nr(psdev->dev->bus)
184 && bus == psdev->dev->bus->number
185 && slot == PCI_SLOT(psdev->dev->devfn)
186 && func == PCI_FUNC(psdev->dev->devfn)) {
187 return psdev;
188 }
189 }
190
191 return NULL;
192}
193
194static struct pcistub_device *pcistub_device_find(int domain, int bus,
195 int slot, int func)
196{
197 struct pcistub_device *psdev;
198 unsigned long flags;
199
200 spin_lock_irqsave(&pcistub_devices_lock, flags);
201
202 psdev = pcistub_device_find_locked(domain, bus, slot, func);
203 if (psdev)
204 pcistub_device_get(psdev);
205
206 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
207 return psdev;
208}
209
210static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
211 struct pcistub_device *psdev)
212{
213 struct pci_dev *pci_dev = NULL;
214 unsigned long flags;
215
216 spin_lock_irqsave(&psdev->lock, flags);
217 if (!psdev->pdev) {
218 psdev->pdev = pdev;
219 pci_dev = psdev->dev;
220 }
221 spin_unlock_irqrestore(&psdev->lock, flags);
222
223 if (pci_dev)
224 pcistub_device_get(psdev);
225
226 return pci_dev;
227}
228
229#ifdef CONFIG_XEN_ACPI
230static int pcistub_get_gsi_from_sbdf(unsigned int sbdf)
231{
232 struct pcistub_device *psdev;
233 int domain = (sbdf >> 16) & 0xffff;
234 int bus = PCI_BUS_NUM(sbdf);
235 int slot = PCI_SLOT(sbdf);
236 int func = PCI_FUNC(sbdf);
237
238 psdev = pcistub_device_find(domain, bus, slot, func);
239
240 if (!psdev)
241 return -ENODEV;
242
243 return psdev->gsi;
244}
245#endif
246
247struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
248 int domain, int bus,
249 int slot, int func)
250{
251 struct pcistub_device *psdev;
252 struct pci_dev *found_dev = NULL;
253 unsigned long flags;
254
255 spin_lock_irqsave(&pcistub_devices_lock, flags);
256
257 psdev = pcistub_device_find_locked(domain, bus, slot, func);
258 if (psdev)
259 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
260
261 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
262 return found_dev;
263}
264
265struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
266 struct pci_dev *dev)
267{
268 struct pcistub_device *psdev;
269 struct pci_dev *found_dev = NULL;
270 unsigned long flags;
271
272 spin_lock_irqsave(&pcistub_devices_lock, flags);
273
274 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
275 if (psdev->dev == dev) {
276 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
277 break;
278 }
279 }
280
281 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
282 return found_dev;
283}
284
285/*
286 * Called when:
287 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
288 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
289 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
290 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
291 *
292 * As such we have to be careful.
293 *
294 * To make this easier, the caller has to hold the device lock.
295 */
296void pcistub_put_pci_dev(struct pci_dev *dev)
297{
298 struct pcistub_device *psdev, *found_psdev = NULL;
299 unsigned long flags;
300 struct xen_pcibk_dev_data *dev_data;
301 int ret;
302
303 spin_lock_irqsave(&pcistub_devices_lock, flags);
304
305 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
306 if (psdev->dev == dev) {
307 found_psdev = psdev;
308 break;
309 }
310 }
311
312 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
313 if (WARN_ON(!found_psdev))
314 return;
315
316 /*hold this lock for avoiding breaking link between
317 * pcistub and xen_pcibk when AER is in processing
318 */
319 down_write(&pcistub_sem);
320 /* Cleanup our device
321 * (so it's ready for the next domain)
322 */
323 device_lock_assert(&dev->dev);
324 pcistub_reset_device_state(dev);
325
326 dev_data = pci_get_drvdata(dev);
327 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
328 if (!ret) {
329 /*
330 * The usual sequence is pci_save_state & pci_restore_state
331 * but the guest might have messed the configuration space up.
332 * Use the initial version (when device was bound to us).
333 */
334 pci_restore_state(dev);
335 } else
336 dev_info(&dev->dev, "Could not reload PCI state\n");
337 /* This disables the device. */
338 xen_pcibk_reset_device(dev);
339
340 /* And cleanup up our emulated fields. */
341 xen_pcibk_config_reset_dev(dev);
342 xen_pcibk_config_free_dyn_fields(dev);
343
344 dev_data->allow_interrupt_control = 0;
345
346 xen_unregister_device_domain_owner(dev);
347
348 spin_lock_irqsave(&found_psdev->lock, flags);
349 found_psdev->pdev = NULL;
350 spin_unlock_irqrestore(&found_psdev->lock, flags);
351
352 pcistub_device_put(found_psdev);
353 up_write(&pcistub_sem);
354}
355
356static int pcistub_match_one(struct pci_dev *dev,
357 struct pcistub_device_id *pdev_id)
358{
359 /* Match the specified device by domain, bus, slot, func and also if
360 * any of the device's parent bridges match.
361 */
362 for (; dev != NULL; dev = dev->bus->self) {
363 if (pci_domain_nr(dev->bus) == pdev_id->domain
364 && dev->bus->number == pdev_id->bus
365 && dev->devfn == pdev_id->devfn)
366 return 1;
367
368 /* Sometimes topmost bridge links to itself. */
369 if (dev == dev->bus->self)
370 break;
371 }
372
373 return 0;
374}
375
376static int pcistub_match(struct pci_dev *dev)
377{
378 struct pcistub_device_id *pdev_id;
379 unsigned long flags;
380 int found = 0;
381
382 spin_lock_irqsave(&device_ids_lock, flags);
383 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
384 if (pcistub_match_one(dev, pdev_id)) {
385 found = 1;
386 break;
387 }
388 }
389 spin_unlock_irqrestore(&device_ids_lock, flags);
390
391 return found;
392}
393
394static int pcistub_init_device(struct pcistub_device *psdev)
395{
396 struct xen_pcibk_dev_data *dev_data;
397 struct pci_dev *dev;
398#ifdef CONFIG_XEN_ACPI
399 int gsi, trigger, polarity;
400#endif
401 int err = 0;
402
403 if (!psdev)
404 return -EINVAL;
405
406 dev = psdev->dev;
407
408 dev_dbg(&dev->dev, "initializing...\n");
409
410 /* The PCI backend is not intended to be a module (or to work with
411 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
412 * would need to be called somewhere to free the memory allocated
413 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
414 */
415 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
416 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
417 if (!dev_data) {
418 err = -ENOMEM;
419 goto out;
420 }
421 pci_set_drvdata(dev, dev_data);
422
423 /*
424 * Setup name for fake IRQ handler. It will only be enabled
425 * once the device is turned on by the guest.
426 */
427 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
428
429 dev_dbg(&dev->dev, "initializing config\n");
430
431 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
432 err = xen_pcibk_config_init_dev(dev);
433 if (err)
434 goto out;
435
436 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
437 * must do this here because pcibios_enable_device may specify
438 * the pci device's true irq (and possibly its other resources)
439 * if they differ from what's in the configuration space.
440 * This makes the assumption that the device's resources won't
441 * change after this point (otherwise this code may break!)
442 */
443 dev_dbg(&dev->dev, "enabling device\n");
444 err = pci_enable_device(dev);
445 if (err)
446 goto config_release;
447
448 if (dev->msix_cap) {
449 struct physdev_pci_device ppdev = {
450 .seg = pci_domain_nr(dev->bus),
451 .bus = dev->bus->number,
452 .devfn = dev->devfn
453 };
454
455 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
456 if (err && err != -ENOSYS)
457 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
458 err);
459 }
460
461 /* We need the device active to save the state. */
462 dev_dbg(&dev->dev, "save state of device\n");
463 pci_save_state(dev);
464 dev_data->pci_saved_state = pci_store_saved_state(dev);
465 if (!dev_data->pci_saved_state)
466 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
467 else {
468 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
469 err = pcistub_reset_device_state(dev);
470 if (err)
471 goto config_release;
472 pci_restore_state(dev);
473 }
474
475#ifdef CONFIG_XEN_ACPI
476 if (xen_initial_domain() && xen_pvh_domain()) {
477 err = xen_acpi_get_gsi_info(dev, &gsi, &trigger, &polarity);
478 if (err) {
479 dev_err(&dev->dev, "Fail to get gsi info!\n");
480 goto config_release;
481 }
482 err = xen_pvh_setup_gsi(gsi, trigger, polarity);
483 if (err)
484 goto config_release;
485 psdev->gsi = gsi;
486 }
487#endif
488
489 /* Now disable the device (this also ensures some private device
490 * data is setup before we export)
491 */
492 dev_dbg(&dev->dev, "reset device\n");
493 xen_pcibk_reset_device(dev);
494
495 pci_set_dev_assigned(dev);
496 return 0;
497
498config_release:
499 xen_pcibk_config_free_dev(dev);
500
501out:
502 pci_set_drvdata(dev, NULL);
503 kfree(dev_data);
504 return err;
505}
506
507/*
508 * Because some initialization still happens on
509 * devices during fs_initcall, we need to defer
510 * full initialization of our devices until
511 * device_initcall.
512 */
513static int __init pcistub_init_devices_late(void)
514{
515 struct pcistub_device *psdev;
516 unsigned long flags;
517 int err = 0;
518
519 spin_lock_irqsave(&pcistub_devices_lock, flags);
520
521 while (!list_empty(&seized_devices)) {
522 psdev = container_of(seized_devices.next,
523 struct pcistub_device, dev_list);
524 list_del(&psdev->dev_list);
525
526 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
527
528 err = pcistub_init_device(psdev);
529 if (err) {
530 dev_err(&psdev->dev->dev,
531 "error %d initializing device\n", err);
532 kfree(psdev);
533 psdev = NULL;
534 }
535
536 spin_lock_irqsave(&pcistub_devices_lock, flags);
537
538 if (psdev)
539 list_add_tail(&psdev->dev_list, &pcistub_devices);
540 }
541
542 initialize_devices = 1;
543
544 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
545
546 return 0;
547}
548
549static void pcistub_device_id_add_list(struct pcistub_device_id *new,
550 int domain, int bus, unsigned int devfn)
551{
552 struct pcistub_device_id *pci_dev_id;
553 unsigned long flags;
554 int found = 0;
555
556 spin_lock_irqsave(&device_ids_lock, flags);
557
558 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
559 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
560 pci_dev_id->devfn == devfn) {
561 found = 1;
562 break;
563 }
564 }
565
566 if (!found) {
567 new->domain = domain;
568 new->bus = bus;
569 new->devfn = devfn;
570 list_add_tail(&new->slot_list, &pcistub_device_ids);
571 }
572
573 spin_unlock_irqrestore(&device_ids_lock, flags);
574
575 if (found)
576 kfree(new);
577}
578
579static int pcistub_seize(struct pci_dev *dev,
580 struct pcistub_device_id *pci_dev_id)
581{
582 struct pcistub_device *psdev;
583 unsigned long flags;
584 int err = 0;
585
586 psdev = pcistub_device_alloc(dev);
587 if (!psdev) {
588 kfree(pci_dev_id);
589 return -ENOMEM;
590 }
591
592 spin_lock_irqsave(&pcistub_devices_lock, flags);
593
594 if (initialize_devices) {
595 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
596
597 /* don't want irqs disabled when calling pcistub_init_device */
598 err = pcistub_init_device(psdev);
599
600 spin_lock_irqsave(&pcistub_devices_lock, flags);
601
602 if (!err)
603 list_add(&psdev->dev_list, &pcistub_devices);
604 } else {
605 dev_dbg(&dev->dev, "deferring initialization\n");
606 list_add(&psdev->dev_list, &seized_devices);
607 }
608
609 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
610
611 if (err) {
612 kfree(pci_dev_id);
613 pcistub_device_put(psdev);
614 } else if (pci_dev_id)
615 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
616 dev->bus->number, dev->devfn);
617
618 return err;
619}
620
621/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
622 * other functions that take the sysfs lock. */
623static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
624{
625 int err = 0, match;
626 struct pcistub_device_id *pci_dev_id = NULL;
627
628 dev_dbg(&dev->dev, "probing...\n");
629
630 match = pcistub_match(dev);
631
632 if ((dev->driver_override &&
633 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
634 match) {
635
636 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
637 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
638 dev_err(&dev->dev, "can't export pci devices that "
639 "don't have a normal (0) or bridge (1) "
640 "header type!\n");
641 err = -ENODEV;
642 goto out;
643 }
644
645 if (!match) {
646 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
647 if (!pci_dev_id) {
648 err = -ENOMEM;
649 goto out;
650 }
651 }
652
653 dev_info(&dev->dev, "seizing device\n");
654 err = pcistub_seize(dev, pci_dev_id);
655 } else
656 /* Didn't find the device */
657 err = -ENODEV;
658
659out:
660 return err;
661}
662
663/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
664 * other functions that take the sysfs lock. */
665static void pcistub_remove(struct pci_dev *dev)
666{
667 struct pcistub_device *psdev, *found_psdev = NULL;
668 unsigned long flags;
669
670 dev_dbg(&dev->dev, "removing\n");
671
672 spin_lock_irqsave(&pcistub_devices_lock, flags);
673
674 xen_pcibk_config_quirk_release(dev);
675
676 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
677 if (psdev->dev == dev) {
678 found_psdev = psdev;
679 break;
680 }
681 }
682
683 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
684
685 if (found_psdev) {
686 dev_dbg(&dev->dev, "found device to remove %s\n",
687 found_psdev->pdev ? "- in-use" : "");
688
689 if (found_psdev->pdev) {
690 int domid = xen_find_device_domain_owner(dev);
691
692 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
693 pci_name(found_psdev->dev), domid);
694 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
695 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
696 dev_warn(&dev->dev, "****** to other drivers or domains\n");
697
698 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
699 * doing the FLR. */
700 xen_pcibk_release_pci_dev(found_psdev->pdev,
701 found_psdev->dev,
702 false /* caller holds the lock. */);
703 }
704
705 spin_lock_irqsave(&pcistub_devices_lock, flags);
706 list_del(&found_psdev->dev_list);
707 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
708
709 /* the final put for releasing from the list */
710 pcistub_device_put(found_psdev);
711 }
712}
713
714static const struct pci_device_id pcistub_ids[] = {
715 {
716 .vendor = PCI_ANY_ID,
717 .device = PCI_ANY_ID,
718 .subvendor = PCI_ANY_ID,
719 .subdevice = PCI_ANY_ID,
720 },
721 {0,},
722};
723
724#define PCI_NODENAME_MAX 40
725static void kill_domain_by_device(struct pcistub_device *psdev)
726{
727 struct xenbus_transaction xbt;
728 int err;
729 char nodename[PCI_NODENAME_MAX];
730
731 BUG_ON(!psdev);
732 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
733 psdev->pdev->xdev->otherend_id);
734
735again:
736 err = xenbus_transaction_start(&xbt);
737 if (err) {
738 dev_err(&psdev->dev->dev,
739 "error %d when start xenbus transaction\n", err);
740 return;
741 }
742 /*PV AER handlers will set this flag*/
743 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
744 err = xenbus_transaction_end(xbt, 0);
745 if (err) {
746 if (err == -EAGAIN)
747 goto again;
748 dev_err(&psdev->dev->dev,
749 "error %d when end xenbus transaction\n", err);
750 return;
751 }
752}
753
754/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
755 * backend need to have cooperation. In xen_pcibk, those steps will do similar
756 * jobs: send service request and waiting for front_end response.
757*/
758static pci_ers_result_t common_process(struct pcistub_device *psdev,
759 pci_channel_state_t state, int aer_cmd,
760 pci_ers_result_t result)
761{
762 pci_ers_result_t res = result;
763 struct xen_pcie_aer_op *aer_op;
764 struct xen_pcibk_device *pdev = psdev->pdev;
765 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
766 int ret;
767
768 /*with PV AER drivers*/
769 aer_op = &(sh_info->aer_op);
770 aer_op->cmd = aer_cmd ;
771 /*useful for error_detected callback*/
772 aer_op->err = state;
773 /*pcifront_end BDF*/
774 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
775 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
776 if (!ret) {
777 dev_err(&psdev->dev->dev, "failed to get pcifront device\n");
778 return PCI_ERS_RESULT_NONE;
779 }
780 wmb();
781
782 dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n",
783 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
784 /*local flag to mark there's aer request, xen_pcibk callback will use
785 * this flag to judge whether we need to check pci-front give aer
786 * service ack signal
787 */
788 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
789
790 /*It is possible that a pcifront conf_read_write ops request invokes
791 * the callback which cause the spurious execution of wake_up.
792 * Yet it is harmless and better than a spinlock here
793 */
794 set_bit(_XEN_PCIB_active,
795 (unsigned long *)&sh_info->flags);
796 wmb();
797 notify_remote_via_irq(pdev->evtchn_irq);
798
799 /* Enable IRQ to signal "request done". */
800 xen_pcibk_lateeoi(pdev, 0);
801
802 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
803 !(test_bit(_XEN_PCIB_active, (unsigned long *)
804 &sh_info->flags)), 300*HZ);
805
806 /* Enable IRQ for pcifront request if not already active. */
807 if (!test_bit(_PDEVF_op_active, &pdev->flags))
808 xen_pcibk_lateeoi(pdev, 0);
809
810 if (!ret) {
811 if (test_bit(_XEN_PCIB_active,
812 (unsigned long *)&sh_info->flags)) {
813 dev_err(&psdev->dev->dev,
814 "pcifront aer process not responding!\n");
815 clear_bit(_XEN_PCIB_active,
816 (unsigned long *)&sh_info->flags);
817 aer_op->err = PCI_ERS_RESULT_NONE;
818 return res;
819 }
820 }
821 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
822
823 res = (__force pci_ers_result_t)aer_op->err;
824 return res;
825}
826
827/*
828* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
829* of the device driver could provide this service, and then wait for pcifront
830* ack.
831* @dev: pointer to PCI devices
832* return value is used by aer_core do_recovery policy
833*/
834static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
835{
836 struct pcistub_device *psdev;
837 pci_ers_result_t result;
838
839 result = PCI_ERS_RESULT_RECOVERED;
840 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
841 dev->bus->number, dev->devfn);
842
843 down_write(&pcistub_sem);
844 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
845 dev->bus->number,
846 PCI_SLOT(dev->devfn),
847 PCI_FUNC(dev->devfn));
848
849 if (!psdev || !psdev->pdev) {
850 dev_err(&dev->dev, "device is not found/assigned\n");
851 goto end;
852 }
853
854 if (!psdev->pdev->sh_info) {
855 dev_err(&dev->dev, "device is not connected or owned"
856 " by HVM, kill it\n");
857 kill_domain_by_device(psdev);
858 goto end;
859 }
860
861 if (!test_bit(_XEN_PCIB_AERHANDLER,
862 (unsigned long *)&psdev->pdev->sh_info->flags)) {
863 dev_err(&dev->dev,
864 "guest with no AER driver should have been killed\n");
865 goto end;
866 }
867 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
868
869 if (result == PCI_ERS_RESULT_NONE ||
870 result == PCI_ERS_RESULT_DISCONNECT) {
871 dev_dbg(&dev->dev,
872 "No AER slot_reset service or disconnected!\n");
873 kill_domain_by_device(psdev);
874 }
875end:
876 if (psdev)
877 pcistub_device_put(psdev);
878 up_write(&pcistub_sem);
879 return result;
880
881}
882
883
884/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
885* in case of the device driver could provide this service, and then wait
886* for pcifront ack
887* @dev: pointer to PCI devices
888* return value is used by aer_core do_recovery policy
889*/
890
891static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
892{
893 struct pcistub_device *psdev;
894 pci_ers_result_t result;
895
896 result = PCI_ERS_RESULT_RECOVERED;
897 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
898 dev->bus->number, dev->devfn);
899
900 down_write(&pcistub_sem);
901 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
902 dev->bus->number,
903 PCI_SLOT(dev->devfn),
904 PCI_FUNC(dev->devfn));
905
906 if (!psdev || !psdev->pdev) {
907 dev_err(&dev->dev, "device is not found/assigned\n");
908 goto end;
909 }
910
911 if (!psdev->pdev->sh_info) {
912 dev_err(&dev->dev, "device is not connected or owned"
913 " by HVM, kill it\n");
914 kill_domain_by_device(psdev);
915 goto end;
916 }
917
918 if (!test_bit(_XEN_PCIB_AERHANDLER,
919 (unsigned long *)&psdev->pdev->sh_info->flags)) {
920 dev_err(&dev->dev,
921 "guest with no AER driver should have been killed\n");
922 goto end;
923 }
924 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
925
926 if (result == PCI_ERS_RESULT_NONE ||
927 result == PCI_ERS_RESULT_DISCONNECT) {
928 dev_dbg(&dev->dev,
929 "No AER mmio_enabled service or disconnected!\n");
930 kill_domain_by_device(psdev);
931 }
932end:
933 if (psdev)
934 pcistub_device_put(psdev);
935 up_write(&pcistub_sem);
936 return result;
937}
938
939/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
940* in case of the device driver could provide this service, and then wait
941* for pcifront ack.
942* @dev: pointer to PCI devices
943* @error: the current PCI connection state
944* return value is used by aer_core do_recovery policy
945*/
946
947static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
948 pci_channel_state_t error)
949{
950 struct pcistub_device *psdev;
951 pci_ers_result_t result;
952
953 result = PCI_ERS_RESULT_CAN_RECOVER;
954 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
955 dev->bus->number, dev->devfn);
956
957 down_write(&pcistub_sem);
958 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
959 dev->bus->number,
960 PCI_SLOT(dev->devfn),
961 PCI_FUNC(dev->devfn));
962
963 if (!psdev || !psdev->pdev) {
964 dev_err(&dev->dev, "device is not found/assigned\n");
965 goto end;
966 }
967
968 if (!psdev->pdev->sh_info) {
969 dev_err(&dev->dev, "device is not connected or owned"
970 " by HVM, kill it\n");
971 kill_domain_by_device(psdev);
972 goto end;
973 }
974
975 /*Guest owns the device yet no aer handler regiested, kill guest*/
976 if (!test_bit(_XEN_PCIB_AERHANDLER,
977 (unsigned long *)&psdev->pdev->sh_info->flags)) {
978 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
979 kill_domain_by_device(psdev);
980 goto end;
981 }
982 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
983
984 if (result == PCI_ERS_RESULT_NONE ||
985 result == PCI_ERS_RESULT_DISCONNECT) {
986 dev_dbg(&dev->dev,
987 "No AER error_detected service or disconnected!\n");
988 kill_domain_by_device(psdev);
989 }
990end:
991 if (psdev)
992 pcistub_device_put(psdev);
993 up_write(&pcistub_sem);
994 return result;
995}
996
997/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
998* in case of the device driver could provide this service, and then wait
999* for pcifront ack.
1000* @dev: pointer to PCI devices
1001*/
1002
1003static void xen_pcibk_error_resume(struct pci_dev *dev)
1004{
1005 struct pcistub_device *psdev;
1006
1007 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
1008 dev->bus->number, dev->devfn);
1009
1010 down_write(&pcistub_sem);
1011 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
1012 dev->bus->number,
1013 PCI_SLOT(dev->devfn),
1014 PCI_FUNC(dev->devfn));
1015
1016 if (!psdev || !psdev->pdev) {
1017 dev_err(&dev->dev, "device is not found/assigned\n");
1018 goto end;
1019 }
1020
1021 if (!psdev->pdev->sh_info) {
1022 dev_err(&dev->dev, "device is not connected or owned"
1023 " by HVM, kill it\n");
1024 kill_domain_by_device(psdev);
1025 goto end;
1026 }
1027
1028 if (!test_bit(_XEN_PCIB_AERHANDLER,
1029 (unsigned long *)&psdev->pdev->sh_info->flags)) {
1030 dev_err(&dev->dev,
1031 "guest with no AER driver should have been killed\n");
1032 kill_domain_by_device(psdev);
1033 goto end;
1034 }
1035 common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
1036 PCI_ERS_RESULT_RECOVERED);
1037end:
1038 if (psdev)
1039 pcistub_device_put(psdev);
1040 up_write(&pcistub_sem);
1041 return;
1042}
1043
1044/*add xen_pcibk AER handling*/
1045static const struct pci_error_handlers xen_pcibk_error_handler = {
1046 .error_detected = xen_pcibk_error_detected,
1047 .mmio_enabled = xen_pcibk_mmio_enabled,
1048 .slot_reset = xen_pcibk_slot_reset,
1049 .resume = xen_pcibk_error_resume,
1050};
1051
1052/*
1053 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
1054 * for a normal device. I don't want it to be loaded automatically.
1055 */
1056
1057static struct pci_driver xen_pcibk_pci_driver = {
1058 /* The name should be xen_pciback, but until the tools are updated
1059 * we will keep it as pciback. */
1060 .name = PCISTUB_DRIVER_NAME,
1061 .id_table = pcistub_ids,
1062 .probe = pcistub_probe,
1063 .remove = pcistub_remove,
1064 .err_handler = &xen_pcibk_error_handler,
1065};
1066
1067static inline int str_to_slot(const char *buf, int *domain, int *bus,
1068 int *slot, int *func)
1069{
1070 int parsed = 0;
1071
1072 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1073 &parsed)) {
1074 case 3:
1075 *func = -1;
1076 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1077 break;
1078 case 2:
1079 *slot = *func = -1;
1080 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1081 break;
1082 }
1083 if (parsed && !buf[parsed])
1084 return 0;
1085
1086 /* try again without domain */
1087 *domain = 0;
1088 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1089 case 2:
1090 *func = -1;
1091 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1092 break;
1093 case 1:
1094 *slot = *func = -1;
1095 sscanf(buf, " %x:*.* %n", bus, &parsed);
1096 break;
1097 }
1098 if (parsed && !buf[parsed])
1099 return 0;
1100
1101 return -EINVAL;
1102}
1103
1104static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1105 *slot, int *func, int *reg, int *size, int *mask)
1106{
1107 int parsed = 0;
1108
1109 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1110 reg, size, mask, &parsed);
1111 if (parsed && !buf[parsed])
1112 return 0;
1113
1114 /* try again without domain */
1115 *domain = 0;
1116 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1117 mask, &parsed);
1118 if (parsed && !buf[parsed])
1119 return 0;
1120
1121 return -EINVAL;
1122}
1123
1124static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1125{
1126 struct pcistub_device_id *pci_dev_id;
1127 int rc = 0, devfn = PCI_DEVFN(slot, func);
1128
1129 if (slot < 0) {
1130 for (slot = 0; !rc && slot < 32; ++slot)
1131 rc = pcistub_device_id_add(domain, bus, slot, func);
1132 return rc;
1133 }
1134
1135 if (func < 0) {
1136 for (func = 0; !rc && func < 8; ++func)
1137 rc = pcistub_device_id_add(domain, bus, slot, func);
1138 return rc;
1139 }
1140
1141 if ((
1142#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1143 || !defined(CONFIG_PCI_DOMAINS)
1144 !pci_domains_supported ? domain :
1145#endif
1146 domain < 0 || domain > 0xffff)
1147 || bus < 0 || bus > 0xff
1148 || PCI_SLOT(devfn) != slot
1149 || PCI_FUNC(devfn) != func)
1150 return -EINVAL;
1151
1152 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1153 if (!pci_dev_id)
1154 return -ENOMEM;
1155
1156 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1157 domain, bus, slot, func);
1158
1159 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1160
1161 return 0;
1162}
1163
1164static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1165{
1166 struct pcistub_device_id *pci_dev_id, *t;
1167 int err = -ENOENT;
1168 unsigned long flags;
1169
1170 spin_lock_irqsave(&device_ids_lock, flags);
1171 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1172 slot_list) {
1173 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1174 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1175 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1176 /* Don't break; here because it's possible the same
1177 * slot could be in the list more than once
1178 */
1179 list_del(&pci_dev_id->slot_list);
1180 kfree(pci_dev_id);
1181
1182 err = 0;
1183
1184 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1185 domain, bus, slot, func);
1186 }
1187 }
1188 spin_unlock_irqrestore(&device_ids_lock, flags);
1189
1190 return err;
1191}
1192
1193static int pcistub_reg_add(int domain, int bus, int slot, int func,
1194 unsigned int reg, unsigned int size,
1195 unsigned int mask)
1196{
1197 int err = 0;
1198 struct pcistub_device *psdev;
1199 struct pci_dev *dev;
1200 struct config_field *field;
1201
1202 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1203 return -EINVAL;
1204
1205 psdev = pcistub_device_find(domain, bus, slot, func);
1206 if (!psdev) {
1207 err = -ENODEV;
1208 goto out;
1209 }
1210 dev = psdev->dev;
1211
1212 field = kzalloc(sizeof(*field), GFP_KERNEL);
1213 if (!field) {
1214 err = -ENOMEM;
1215 goto out;
1216 }
1217
1218 field->offset = reg;
1219 field->size = size;
1220 field->mask = mask;
1221 field->init = NULL;
1222 field->reset = NULL;
1223 field->release = NULL;
1224 field->clean = xen_pcibk_config_field_free;
1225
1226 err = xen_pcibk_config_quirks_add_field(dev, field);
1227 if (err)
1228 kfree(field);
1229out:
1230 if (psdev)
1231 pcistub_device_put(psdev);
1232 return err;
1233}
1234
1235static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1236 size_t count)
1237{
1238 int domain, bus, slot, func;
1239 int err;
1240
1241 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1242 if (err)
1243 goto out;
1244
1245 err = pcistub_device_id_add(domain, bus, slot, func);
1246
1247out:
1248 if (!err)
1249 err = count;
1250 return err;
1251}
1252static DRIVER_ATTR_WO(new_slot);
1253
1254static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1255 size_t count)
1256{
1257 int domain, bus, slot, func;
1258 int err;
1259
1260 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1261 if (err)
1262 goto out;
1263
1264 err = pcistub_device_id_remove(domain, bus, slot, func);
1265
1266out:
1267 if (!err)
1268 err = count;
1269 return err;
1270}
1271static DRIVER_ATTR_WO(remove_slot);
1272
1273static ssize_t slots_show(struct device_driver *drv, char *buf)
1274{
1275 struct pcistub_device_id *pci_dev_id;
1276 size_t count = 0;
1277 unsigned long flags;
1278
1279 spin_lock_irqsave(&device_ids_lock, flags);
1280 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1281 if (count >= PAGE_SIZE)
1282 break;
1283
1284 count += scnprintf(buf + count, PAGE_SIZE - count,
1285 "%04x:%02x:%02x.%d\n",
1286 pci_dev_id->domain, pci_dev_id->bus,
1287 PCI_SLOT(pci_dev_id->devfn),
1288 PCI_FUNC(pci_dev_id->devfn));
1289 }
1290 spin_unlock_irqrestore(&device_ids_lock, flags);
1291
1292 return count;
1293}
1294static DRIVER_ATTR_RO(slots);
1295
1296static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1297{
1298 struct pcistub_device *psdev;
1299 struct xen_pcibk_dev_data *dev_data;
1300 size_t count = 0;
1301 unsigned long flags;
1302
1303 spin_lock_irqsave(&pcistub_devices_lock, flags);
1304 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1305 if (count >= PAGE_SIZE)
1306 break;
1307 if (!psdev->dev)
1308 continue;
1309 dev_data = pci_get_drvdata(psdev->dev);
1310 if (!dev_data)
1311 continue;
1312 count +=
1313 scnprintf(buf + count, PAGE_SIZE - count,
1314 "%s:%s:%sing:%ld\n",
1315 pci_name(psdev->dev),
1316 dev_data->isr_on ? "on" : "off",
1317 dev_data->ack_intr ? "ack" : "not ack",
1318 dev_data->handled);
1319 }
1320 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1321 return count;
1322}
1323static DRIVER_ATTR_RO(irq_handlers);
1324
1325static ssize_t irq_handler_state_store(struct device_driver *drv,
1326 const char *buf, size_t count)
1327{
1328 struct pcistub_device *psdev;
1329 struct xen_pcibk_dev_data *dev_data;
1330 int domain, bus, slot, func;
1331 int err;
1332
1333 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1334 if (err)
1335 return err;
1336
1337 psdev = pcistub_device_find(domain, bus, slot, func);
1338 if (!psdev) {
1339 err = -ENOENT;
1340 goto out;
1341 }
1342
1343 dev_data = pci_get_drvdata(psdev->dev);
1344 if (!dev_data) {
1345 err = -ENOENT;
1346 goto out;
1347 }
1348
1349 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1350 dev_data->irq_name, dev_data->isr_on,
1351 !dev_data->isr_on);
1352
1353 dev_data->isr_on = !(dev_data->isr_on);
1354 if (dev_data->isr_on)
1355 dev_data->ack_intr = 1;
1356out:
1357 if (psdev)
1358 pcistub_device_put(psdev);
1359 if (!err)
1360 err = count;
1361 return err;
1362}
1363static DRIVER_ATTR_WO(irq_handler_state);
1364
1365static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1366 size_t count)
1367{
1368 int domain, bus, slot, func, reg, size, mask;
1369 int err;
1370
1371 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1372 &mask);
1373 if (err)
1374 goto out;
1375
1376 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1377
1378out:
1379 if (!err)
1380 err = count;
1381 return err;
1382}
1383
1384static ssize_t quirks_show(struct device_driver *drv, char *buf)
1385{
1386 int count = 0;
1387 unsigned long flags;
1388 struct xen_pcibk_config_quirk *quirk;
1389 struct xen_pcibk_dev_data *dev_data;
1390 const struct config_field *field;
1391 const struct config_field_entry *cfg_entry;
1392
1393 spin_lock_irqsave(&device_ids_lock, flags);
1394 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1395 if (count >= PAGE_SIZE)
1396 goto out;
1397
1398 count += scnprintf(buf + count, PAGE_SIZE - count,
1399 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1400 quirk->pdev->bus->number,
1401 PCI_SLOT(quirk->pdev->devfn),
1402 PCI_FUNC(quirk->pdev->devfn),
1403 quirk->devid.vendor, quirk->devid.device,
1404 quirk->devid.subvendor,
1405 quirk->devid.subdevice);
1406
1407 dev_data = pci_get_drvdata(quirk->pdev);
1408
1409 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1410 field = cfg_entry->field;
1411 if (count >= PAGE_SIZE)
1412 goto out;
1413
1414 count += scnprintf(buf + count, PAGE_SIZE - count,
1415 "\t\t%08x:%01x:%08x\n",
1416 cfg_entry->base_offset +
1417 field->offset, field->size,
1418 field->mask);
1419 }
1420 }
1421
1422out:
1423 spin_unlock_irqrestore(&device_ids_lock, flags);
1424
1425 return count;
1426}
1427static DRIVER_ATTR_RW(quirks);
1428
1429static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1430 size_t count)
1431{
1432 int domain, bus, slot, func;
1433 int err;
1434 struct pcistub_device *psdev;
1435 struct xen_pcibk_dev_data *dev_data;
1436
1437 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1438 if (err)
1439 goto out;
1440
1441 psdev = pcistub_device_find(domain, bus, slot, func);
1442 if (!psdev) {
1443 err = -ENODEV;
1444 goto out;
1445 }
1446
1447 dev_data = pci_get_drvdata(psdev->dev);
1448 /* the driver data for a device should never be null at this point */
1449 if (!dev_data) {
1450 err = -ENXIO;
1451 goto release;
1452 }
1453 if (!dev_data->permissive) {
1454 dev_data->permissive = 1;
1455 /* Let user know that what they're doing could be unsafe */
1456 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1457 "configuration space accesses!\n");
1458 dev_warn(&psdev->dev->dev,
1459 "permissive mode is potentially unsafe!\n");
1460 }
1461release:
1462 pcistub_device_put(psdev);
1463out:
1464 if (!err)
1465 err = count;
1466 return err;
1467}
1468
1469static ssize_t permissive_show(struct device_driver *drv, char *buf)
1470{
1471 struct pcistub_device *psdev;
1472 struct xen_pcibk_dev_data *dev_data;
1473 size_t count = 0;
1474 unsigned long flags;
1475 spin_lock_irqsave(&pcistub_devices_lock, flags);
1476 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1477 if (count >= PAGE_SIZE)
1478 break;
1479 if (!psdev->dev)
1480 continue;
1481 dev_data = pci_get_drvdata(psdev->dev);
1482 if (!dev_data || !dev_data->permissive)
1483 continue;
1484 count +=
1485 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1486 pci_name(psdev->dev));
1487 }
1488 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1489 return count;
1490}
1491static DRIVER_ATTR_RW(permissive);
1492
1493static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1494 const char *buf, size_t count)
1495{
1496 int domain, bus, slot, func;
1497 int err;
1498 struct pcistub_device *psdev;
1499 struct xen_pcibk_dev_data *dev_data;
1500
1501 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1502 if (err)
1503 goto out;
1504
1505 psdev = pcistub_device_find(domain, bus, slot, func);
1506 if (!psdev) {
1507 err = -ENODEV;
1508 goto out;
1509 }
1510
1511 dev_data = pci_get_drvdata(psdev->dev);
1512 /* the driver data for a device should never be null at this point */
1513 if (!dev_data) {
1514 err = -ENXIO;
1515 goto release;
1516 }
1517 dev_data->allow_interrupt_control = 1;
1518release:
1519 pcistub_device_put(psdev);
1520out:
1521 if (!err)
1522 err = count;
1523 return err;
1524}
1525
1526static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1527 char *buf)
1528{
1529 struct pcistub_device *psdev;
1530 struct xen_pcibk_dev_data *dev_data;
1531 size_t count = 0;
1532 unsigned long flags;
1533
1534 spin_lock_irqsave(&pcistub_devices_lock, flags);
1535 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1536 if (count >= PAGE_SIZE)
1537 break;
1538 if (!psdev->dev)
1539 continue;
1540 dev_data = pci_get_drvdata(psdev->dev);
1541 if (!dev_data || !dev_data->allow_interrupt_control)
1542 continue;
1543 count +=
1544 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1545 pci_name(psdev->dev));
1546 }
1547 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1548 return count;
1549}
1550static DRIVER_ATTR_RW(allow_interrupt_control);
1551
1552static void pcistub_exit(void)
1553{
1554 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1555 driver_remove_file(&xen_pcibk_pci_driver.driver,
1556 &driver_attr_remove_slot);
1557 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1558 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1559 driver_remove_file(&xen_pcibk_pci_driver.driver,
1560 &driver_attr_permissive);
1561 driver_remove_file(&xen_pcibk_pci_driver.driver,
1562 &driver_attr_allow_interrupt_control);
1563 driver_remove_file(&xen_pcibk_pci_driver.driver,
1564 &driver_attr_irq_handlers);
1565 driver_remove_file(&xen_pcibk_pci_driver.driver,
1566 &driver_attr_irq_handler_state);
1567 pci_unregister_driver(&xen_pcibk_pci_driver);
1568}
1569
1570static int __init pcistub_init(void)
1571{
1572 int pos = 0;
1573 int err = 0;
1574 int domain, bus, slot, func;
1575 int parsed;
1576
1577 if (pci_devs_to_hide && *pci_devs_to_hide) {
1578 do {
1579 parsed = 0;
1580
1581 err = sscanf(pci_devs_to_hide + pos,
1582 " (%x:%x:%x.%x) %n",
1583 &domain, &bus, &slot, &func, &parsed);
1584 switch (err) {
1585 case 3:
1586 func = -1;
1587 sscanf(pci_devs_to_hide + pos,
1588 " (%x:%x:%x.*) %n",
1589 &domain, &bus, &slot, &parsed);
1590 break;
1591 case 2:
1592 slot = func = -1;
1593 sscanf(pci_devs_to_hide + pos,
1594 " (%x:%x:*.*) %n",
1595 &domain, &bus, &parsed);
1596 break;
1597 }
1598
1599 if (!parsed) {
1600 domain = 0;
1601 err = sscanf(pci_devs_to_hide + pos,
1602 " (%x:%x.%x) %n",
1603 &bus, &slot, &func, &parsed);
1604 switch (err) {
1605 case 2:
1606 func = -1;
1607 sscanf(pci_devs_to_hide + pos,
1608 " (%x:%x.*) %n",
1609 &bus, &slot, &parsed);
1610 break;
1611 case 1:
1612 slot = func = -1;
1613 sscanf(pci_devs_to_hide + pos,
1614 " (%x:*.*) %n",
1615 &bus, &parsed);
1616 break;
1617 }
1618 }
1619
1620 if (parsed <= 0)
1621 goto parse_error;
1622
1623 err = pcistub_device_id_add(domain, bus, slot, func);
1624 if (err)
1625 goto out;
1626
1627 pos += parsed;
1628 } while (pci_devs_to_hide[pos]);
1629 }
1630
1631 /* If we're the first PCI Device Driver to register, we're the
1632 * first one to get offered PCI devices as they become
1633 * available (and thus we can be the first to grab them)
1634 */
1635 err = pci_register_driver(&xen_pcibk_pci_driver);
1636 if (err < 0)
1637 goto out;
1638
1639 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1640 &driver_attr_new_slot);
1641 if (!err)
1642 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1643 &driver_attr_remove_slot);
1644 if (!err)
1645 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1646 &driver_attr_slots);
1647 if (!err)
1648 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1649 &driver_attr_quirks);
1650 if (!err)
1651 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1652 &driver_attr_permissive);
1653 if (!err)
1654 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1655 &driver_attr_allow_interrupt_control);
1656
1657 if (!err)
1658 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1659 &driver_attr_irq_handlers);
1660 if (!err)
1661 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1662 &driver_attr_irq_handler_state);
1663 if (err)
1664 pcistub_exit();
1665
1666out:
1667 return err;
1668
1669parse_error:
1670 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1671 pci_devs_to_hide + pos);
1672 return -EINVAL;
1673}
1674
1675#ifndef MODULE
1676/*
1677 * fs_initcall happens before device_initcall
1678 * so xen_pcibk *should* get called first (b/c we
1679 * want to suck up any device before other drivers
1680 * get a chance by being the first pci device
1681 * driver to register)
1682 */
1683fs_initcall(pcistub_init);
1684#endif
1685
1686#ifdef CONFIG_PCI_IOV
1687static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1688{
1689 struct pcistub_device *psdev = NULL;
1690 unsigned long flags;
1691 bool found = false;
1692
1693 spin_lock_irqsave(&pcistub_devices_lock, flags);
1694 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1695 if (!psdev->pdev && psdev->dev != pdev
1696 && pci_physfn(psdev->dev) == pdev) {
1697 found = true;
1698 break;
1699 }
1700 }
1701 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1702 if (found)
1703 return psdev;
1704 return NULL;
1705}
1706
1707static int pci_stub_notifier(struct notifier_block *nb,
1708 unsigned long action, void *data)
1709{
1710 struct device *dev = data;
1711 const struct pci_dev *pdev = to_pci_dev(dev);
1712
1713 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1714 return NOTIFY_DONE;
1715
1716 if (!pdev->is_physfn)
1717 return NOTIFY_DONE;
1718
1719 for (;;) {
1720 struct pcistub_device *psdev = find_vfs(pdev);
1721 if (!psdev)
1722 break;
1723 device_release_driver(&psdev->dev->dev);
1724 }
1725 return NOTIFY_DONE;
1726}
1727
1728static struct notifier_block pci_stub_nb = {
1729 .notifier_call = pci_stub_notifier,
1730};
1731#endif
1732
1733static int __init xen_pcibk_init(void)
1734{
1735 int err;
1736
1737 if (!xen_initial_domain())
1738 return -ENODEV;
1739
1740 err = xen_pcibk_config_init();
1741 if (err)
1742 return err;
1743
1744#ifdef MODULE
1745 err = pcistub_init();
1746 if (err < 0)
1747 return err;
1748#endif
1749
1750 pcistub_init_devices_late();
1751 err = xen_pcibk_xenbus_register();
1752 if (err)
1753 pcistub_exit();
1754#ifdef CONFIG_PCI_IOV
1755 else
1756 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1757#endif
1758
1759#ifdef CONFIG_XEN_ACPI
1760 xen_acpi_register_get_gsi_func(pcistub_get_gsi_from_sbdf);
1761#endif
1762
1763 return err;
1764}
1765
1766static void __exit xen_pcibk_cleanup(void)
1767{
1768#ifdef CONFIG_XEN_ACPI
1769 xen_acpi_register_get_gsi_func(NULL);
1770#endif
1771
1772#ifdef CONFIG_PCI_IOV
1773 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1774#endif
1775 xen_pcibk_xenbus_unregister();
1776 pcistub_exit();
1777}
1778
1779module_init(xen_pcibk_init);
1780module_exit(xen_pcibk_cleanup);
1781
1782MODULE_DESCRIPTION("Xen PCI-device stub driver");
1783MODULE_LICENSE("Dual BSD/GPL");
1784MODULE_ALIAS("xen-backend:pci");