2 * PCI Stub Driver - Grabs devices in backend to be exported later
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
28 static char *pci_devs_to_hide;
29 wait_queue_head_t xen_pcibk_aer_wait_queue;
30 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
31 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
33 static DECLARE_RWSEM(pcistub_sem);
34 module_param_named(hide, pci_devs_to_hide, charp, 0444);
36 struct pcistub_device_id {
37 struct list_head slot_list;
42 static LIST_HEAD(pcistub_device_ids);
43 static DEFINE_SPINLOCK(device_ids_lock);
45 struct pcistub_device {
47 struct list_head dev_list;
51 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
54 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
55 * flag must be locked with pcistub_devices_lock
57 static DEFINE_SPINLOCK(pcistub_devices_lock);
58 static LIST_HEAD(pcistub_devices);
60 /* wait for device_initcall before initializing our devices
61 * (see pcistub_init_devices_late)
63 static int initialize_devices;
64 static LIST_HEAD(seized_devices);
66 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
68 struct pcistub_device *psdev;
70 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
72 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
76 psdev->dev = pci_dev_get(dev);
82 kref_init(&psdev->kref);
83 spin_lock_init(&psdev->lock);
88 /* Don't call this directly as it's called by pcistub_device_put */
89 static void pcistub_device_release(struct kref *kref)
91 struct pcistub_device *psdev;
93 struct xen_pcibk_dev_data *dev_data;
95 psdev = container_of(kref, struct pcistub_device, kref);
97 dev_data = pci_get_drvdata(dev);
99 dev_dbg(&dev->dev, "pcistub_device_release\n");
101 xen_unregister_device_domain_owner(dev);
103 /* Call the reset function which does not take lock as this
104 * is called from "unbind" which takes a device_lock mutex.
106 __pci_reset_function_locked(dev);
107 if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
108 dev_info(&dev->dev, "Could not reload PCI state\n");
110 pci_restore_state(dev);
113 struct physdev_pci_device ppdev = {
114 .seg = pci_domain_nr(dev->bus),
115 .bus = dev->bus->number,
118 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
121 if (err && err != -ENOSYS)
122 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
126 /* Disable the device */
127 xen_pcibk_reset_device(dev);
130 pci_set_drvdata(dev, NULL);
132 /* Clean-up the device */
133 xen_pcibk_config_free_dyn_fields(dev);
134 xen_pcibk_config_free_dev(dev);
136 pci_clear_dev_assigned(dev);
142 static inline void pcistub_device_get(struct pcistub_device *psdev)
144 kref_get(&psdev->kref);
147 static inline void pcistub_device_put(struct pcistub_device *psdev)
149 kref_put(&psdev->kref, pcistub_device_release);
152 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
155 struct pcistub_device *psdev;
157 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
158 if (psdev->dev != NULL
159 && domain == pci_domain_nr(psdev->dev->bus)
160 && bus == psdev->dev->bus->number
161 && slot == PCI_SLOT(psdev->dev->devfn)
162 && func == PCI_FUNC(psdev->dev->devfn)) {
170 static struct pcistub_device *pcistub_device_find(int domain, int bus,
173 struct pcistub_device *psdev;
176 spin_lock_irqsave(&pcistub_devices_lock, flags);
178 psdev = pcistub_device_find_locked(domain, bus, slot, func);
180 pcistub_device_get(psdev);
182 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
187 struct pcistub_device *psdev)
189 struct pci_dev *pci_dev = NULL;
192 pcistub_device_get(psdev);
194 spin_lock_irqsave(&psdev->lock, flags);
197 pci_dev = psdev->dev;
199 spin_unlock_irqrestore(&psdev->lock, flags);
202 pcistub_device_put(psdev);
207 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
211 struct pcistub_device *psdev;
212 struct pci_dev *found_dev = NULL;
215 spin_lock_irqsave(&pcistub_devices_lock, flags);
217 psdev = pcistub_device_find_locked(domain, bus, slot, func);
219 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
221 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
228 struct pcistub_device *psdev;
229 struct pci_dev *found_dev = NULL;
232 spin_lock_irqsave(&pcistub_devices_lock, flags);
234 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
235 if (psdev->dev == dev) {
236 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
241 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
247 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
248 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
249 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
250 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
252 * As such we have to be careful.
254 * To make this easier, the caller has to hold the device lock.
256 void pcistub_put_pci_dev(struct pci_dev *dev)
258 struct pcistub_device *psdev, *found_psdev = NULL;
260 struct xen_pcibk_dev_data *dev_data;
263 spin_lock_irqsave(&pcistub_devices_lock, flags);
265 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
266 if (psdev->dev == dev) {
272 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
273 if (WARN_ON(!found_psdev))
276 /*hold this lock for avoiding breaking link between
277 * pcistub and xen_pcibk when AER is in processing
279 down_write(&pcistub_sem);
280 /* Cleanup our device
281 * (so it's ready for the next domain)
283 device_lock_assert(&dev->dev);
284 __pci_reset_function_locked(dev);
286 dev_data = pci_get_drvdata(dev);
287 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
290 * The usual sequence is pci_save_state & pci_restore_state
291 * but the guest might have messed the configuration space up.
292 * Use the initial version (when device was bound to us).
294 pci_restore_state(dev);
296 dev_info(&dev->dev, "Could not reload PCI state\n");
297 /* This disables the device. */
298 xen_pcibk_reset_device(dev);
300 /* And cleanup up our emulated fields. */
301 xen_pcibk_config_reset_dev(dev);
302 xen_pcibk_config_free_dyn_fields(dev);
304 xen_unregister_device_domain_owner(dev);
306 spin_lock_irqsave(&found_psdev->lock, flags);
307 found_psdev->pdev = NULL;
308 spin_unlock_irqrestore(&found_psdev->lock, flags);
310 pcistub_device_put(found_psdev);
311 up_write(&pcistub_sem);
314 static int pcistub_match_one(struct pci_dev *dev,
315 struct pcistub_device_id *pdev_id)
317 /* Match the specified device by domain, bus, slot, func and also if
318 * any of the device's parent bridges match.
320 for (; dev != NULL; dev = dev->bus->self) {
321 if (pci_domain_nr(dev->bus) == pdev_id->domain
322 && dev->bus->number == pdev_id->bus
323 && dev->devfn == pdev_id->devfn)
326 /* Sometimes topmost bridge links to itself. */
327 if (dev == dev->bus->self)
334 static int pcistub_match(struct pci_dev *dev)
336 struct pcistub_device_id *pdev_id;
340 spin_lock_irqsave(&device_ids_lock, flags);
341 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
342 if (pcistub_match_one(dev, pdev_id)) {
347 spin_unlock_irqrestore(&device_ids_lock, flags);
352 static int pcistub_init_device(struct pci_dev *dev)
354 struct xen_pcibk_dev_data *dev_data;
357 dev_dbg(&dev->dev, "initializing...\n");
359 /* The PCI backend is not intended to be a module (or to work with
360 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
361 * would need to be called somewhere to free the memory allocated
362 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
364 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
365 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
370 pci_set_drvdata(dev, dev_data);
373 * Setup name for fake IRQ handler. It will only be enabled
374 * once the device is turned on by the guest.
376 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
378 dev_dbg(&dev->dev, "initializing config\n");
380 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
381 err = xen_pcibk_config_init_dev(dev);
385 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
386 * must do this here because pcibios_enable_device may specify
387 * the pci device's true irq (and possibly its other resources)
388 * if they differ from what's in the configuration space.
389 * This makes the assumption that the device's resources won't
390 * change after this point (otherwise this code may break!)
392 dev_dbg(&dev->dev, "enabling device\n");
393 err = pci_enable_device(dev);
398 struct physdev_pci_device ppdev = {
399 .seg = pci_domain_nr(dev->bus),
400 .bus = dev->bus->number,
404 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
405 if (err && err != -ENOSYS)
406 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
410 /* We need the device active to save the state. */
411 dev_dbg(&dev->dev, "save state of device\n");
413 dev_data->pci_saved_state = pci_store_saved_state(dev);
414 if (!dev_data->pci_saved_state)
415 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
417 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
418 __pci_reset_function_locked(dev);
419 pci_restore_state(dev);
421 /* Now disable the device (this also ensures some private device
422 * data is setup before we export)
424 dev_dbg(&dev->dev, "reset device\n");
425 xen_pcibk_reset_device(dev);
427 pci_set_dev_assigned(dev);
431 xen_pcibk_config_free_dev(dev);
434 pci_set_drvdata(dev, NULL);
440 * Because some initialization still happens on
441 * devices during fs_initcall, we need to defer
442 * full initialization of our devices until
445 static int __init pcistub_init_devices_late(void)
447 struct pcistub_device *psdev;
451 spin_lock_irqsave(&pcistub_devices_lock, flags);
453 while (!list_empty(&seized_devices)) {
454 psdev = container_of(seized_devices.next,
455 struct pcistub_device, dev_list);
456 list_del(&psdev->dev_list);
458 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
460 err = pcistub_init_device(psdev->dev);
462 dev_err(&psdev->dev->dev,
463 "error %d initializing device\n", err);
468 spin_lock_irqsave(&pcistub_devices_lock, flags);
471 list_add_tail(&psdev->dev_list, &pcistub_devices);
474 initialize_devices = 1;
476 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
481 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
482 int domain, int bus, unsigned int devfn)
484 struct pcistub_device_id *pci_dev_id;
488 spin_lock_irqsave(&device_ids_lock, flags);
490 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
491 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
492 pci_dev_id->devfn == devfn) {
499 new->domain = domain;
502 list_add_tail(&new->slot_list, &pcistub_device_ids);
505 spin_unlock_irqrestore(&device_ids_lock, flags);
511 static int pcistub_seize(struct pci_dev *dev)
513 struct pcistub_device *psdev;
517 psdev = pcistub_device_alloc(dev);
521 spin_lock_irqsave(&pcistub_devices_lock, flags);
523 if (initialize_devices) {
524 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
526 /* don't want irqs disabled when calling pcistub_init_device */
527 err = pcistub_init_device(psdev->dev);
529 spin_lock_irqsave(&pcistub_devices_lock, flags);
532 list_add(&psdev->dev_list, &pcistub_devices);
534 dev_dbg(&dev->dev, "deferring initialization\n");
535 list_add(&psdev->dev_list, &seized_devices);
538 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
541 pcistub_device_put(psdev);
546 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
547 * other functions that take the sysfs lock. */
548 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
552 dev_dbg(&dev->dev, "probing...\n");
554 if (pcistub_match(dev)) {
556 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
557 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
558 dev_err(&dev->dev, "can't export pci devices that "
559 "don't have a normal (0) or bridge (1) "
565 dev_info(&dev->dev, "seizing device\n");
566 err = pcistub_seize(dev);
568 /* Didn't find the device */
575 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
576 * other functions that take the sysfs lock. */
577 static void pcistub_remove(struct pci_dev *dev)
579 struct pcistub_device *psdev, *found_psdev = NULL;
582 dev_dbg(&dev->dev, "removing\n");
584 spin_lock_irqsave(&pcistub_devices_lock, flags);
586 xen_pcibk_config_quirk_release(dev);
588 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
589 if (psdev->dev == dev) {
595 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
598 dev_dbg(&dev->dev, "found device to remove %s\n",
599 found_psdev->pdev ? "- in-use" : "");
601 if (found_psdev->pdev) {
602 int domid = xen_find_device_domain_owner(dev);
604 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
605 pci_name(found_psdev->dev), domid);
606 pr_warn("****** driver domain may still access this device's i/o resources!\n");
607 pr_warn("****** shutdown driver domain before binding device\n");
608 pr_warn("****** to other drivers or domains\n");
610 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
612 xen_pcibk_release_pci_dev(found_psdev->pdev,
614 false /* caller holds the lock. */);
617 spin_lock_irqsave(&pcistub_devices_lock, flags);
618 list_del(&found_psdev->dev_list);
619 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
621 /* the final put for releasing from the list */
622 pcistub_device_put(found_psdev);
626 static const struct pci_device_id pcistub_ids[] = {
628 .vendor = PCI_ANY_ID,
629 .device = PCI_ANY_ID,
630 .subvendor = PCI_ANY_ID,
631 .subdevice = PCI_ANY_ID,
636 #define PCI_NODENAME_MAX 40
637 static void kill_domain_by_device(struct pcistub_device *psdev)
639 struct xenbus_transaction xbt;
641 char nodename[PCI_NODENAME_MAX];
644 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
645 psdev->pdev->xdev->otherend_id);
648 err = xenbus_transaction_start(&xbt);
650 dev_err(&psdev->dev->dev,
651 "error %d when start xenbus transaction\n", err);
654 /*PV AER handlers will set this flag*/
655 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
656 err = xenbus_transaction_end(xbt, 0);
660 dev_err(&psdev->dev->dev,
661 "error %d when end xenbus transaction\n", err);
666 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
667 * backend need to have cooperation. In xen_pcibk, those steps will do similar
668 * jobs: send service request and waiting for front_end response.
670 static pci_ers_result_t common_process(struct pcistub_device *psdev,
671 pci_channel_state_t state, int aer_cmd,
672 pci_ers_result_t result)
674 pci_ers_result_t res = result;
675 struct xen_pcie_aer_op *aer_op;
676 struct xen_pcibk_device *pdev = psdev->pdev;
677 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
680 /*with PV AER drivers*/
681 aer_op = &(sh_info->aer_op);
682 aer_op->cmd = aer_cmd ;
683 /*useful for error_detected callback*/
686 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
687 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
689 dev_err(&psdev->dev->dev,
690 DRV_NAME ": failed to get pcifront device\n");
691 return PCI_ERS_RESULT_NONE;
695 dev_dbg(&psdev->dev->dev,
696 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
697 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
698 /*local flag to mark there's aer request, xen_pcibk callback will use
699 * this flag to judge whether we need to check pci-front give aer
702 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
704 /*It is possible that a pcifront conf_read_write ops request invokes
705 * the callback which cause the spurious execution of wake_up.
706 * Yet it is harmless and better than a spinlock here
708 set_bit(_XEN_PCIB_active,
709 (unsigned long *)&sh_info->flags);
711 notify_remote_via_irq(pdev->evtchn_irq);
713 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
714 !(test_bit(_XEN_PCIB_active, (unsigned long *)
715 &sh_info->flags)), 300*HZ);
718 if (test_bit(_XEN_PCIB_active,
719 (unsigned long *)&sh_info->flags)) {
720 dev_err(&psdev->dev->dev,
721 "pcifront aer process not responding!\n");
722 clear_bit(_XEN_PCIB_active,
723 (unsigned long *)&sh_info->flags);
724 aer_op->err = PCI_ERS_RESULT_NONE;
728 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
730 if (test_bit(_XEN_PCIF_active,
731 (unsigned long *)&sh_info->flags)) {
732 dev_dbg(&psdev->dev->dev,
733 "schedule pci_conf service in " DRV_NAME "\n");
734 xen_pcibk_test_and_schedule_op(psdev->pdev);
737 res = (pci_ers_result_t)aer_op->err;
742 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
743 * of the device driver could provide this service, and then wait for pcifront
745 * @dev: pointer to PCI devices
746 * return value is used by aer_core do_recovery policy
748 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
750 struct pcistub_device *psdev;
751 pci_ers_result_t result;
753 result = PCI_ERS_RESULT_RECOVERED;
754 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
755 dev->bus->number, dev->devfn);
757 down_write(&pcistub_sem);
758 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
760 PCI_SLOT(dev->devfn),
761 PCI_FUNC(dev->devfn));
763 if (!psdev || !psdev->pdev) {
765 DRV_NAME " device is not found/assigned\n");
769 if (!psdev->pdev->sh_info) {
770 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
771 " by HVM, kill it\n");
772 kill_domain_by_device(psdev);
776 if (!test_bit(_XEN_PCIB_AERHANDLER,
777 (unsigned long *)&psdev->pdev->sh_info->flags)) {
779 "guest with no AER driver should have been killed\n");
782 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
784 if (result == PCI_ERS_RESULT_NONE ||
785 result == PCI_ERS_RESULT_DISCONNECT) {
787 "No AER slot_reset service or disconnected!\n");
788 kill_domain_by_device(psdev);
792 pcistub_device_put(psdev);
793 up_write(&pcistub_sem);
799 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
800 * in case of the device driver could provide this service, and then wait
802 * @dev: pointer to PCI devices
803 * return value is used by aer_core do_recovery policy
806 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
808 struct pcistub_device *psdev;
809 pci_ers_result_t result;
811 result = PCI_ERS_RESULT_RECOVERED;
812 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
813 dev->bus->number, dev->devfn);
815 down_write(&pcistub_sem);
816 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
818 PCI_SLOT(dev->devfn),
819 PCI_FUNC(dev->devfn));
821 if (!psdev || !psdev->pdev) {
823 DRV_NAME " device is not found/assigned\n");
827 if (!psdev->pdev->sh_info) {
828 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
829 " by HVM, kill it\n");
830 kill_domain_by_device(psdev);
834 if (!test_bit(_XEN_PCIB_AERHANDLER,
835 (unsigned long *)&psdev->pdev->sh_info->flags)) {
837 "guest with no AER driver should have been killed\n");
840 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
842 if (result == PCI_ERS_RESULT_NONE ||
843 result == PCI_ERS_RESULT_DISCONNECT) {
845 "No AER mmio_enabled service or disconnected!\n");
846 kill_domain_by_device(psdev);
850 pcistub_device_put(psdev);
851 up_write(&pcistub_sem);
855 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
856 * in case of the device driver could provide this service, and then wait
858 * @dev: pointer to PCI devices
859 * @error: the current PCI connection state
860 * return value is used by aer_core do_recovery policy
863 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
864 pci_channel_state_t error)
866 struct pcistub_device *psdev;
867 pci_ers_result_t result;
869 result = PCI_ERS_RESULT_CAN_RECOVER;
870 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
871 dev->bus->number, dev->devfn);
873 down_write(&pcistub_sem);
874 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
876 PCI_SLOT(dev->devfn),
877 PCI_FUNC(dev->devfn));
879 if (!psdev || !psdev->pdev) {
881 DRV_NAME " device is not found/assigned\n");
885 if (!psdev->pdev->sh_info) {
886 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
887 " by HVM, kill it\n");
888 kill_domain_by_device(psdev);
892 /*Guest owns the device yet no aer handler regiested, kill guest*/
893 if (!test_bit(_XEN_PCIB_AERHANDLER,
894 (unsigned long *)&psdev->pdev->sh_info->flags)) {
895 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
896 kill_domain_by_device(psdev);
899 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
901 if (result == PCI_ERS_RESULT_NONE ||
902 result == PCI_ERS_RESULT_DISCONNECT) {
904 "No AER error_detected service or disconnected!\n");
905 kill_domain_by_device(psdev);
909 pcistub_device_put(psdev);
910 up_write(&pcistub_sem);
914 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
915 * in case of the device driver could provide this service, and then wait
917 * @dev: pointer to PCI devices
920 static void xen_pcibk_error_resume(struct pci_dev *dev)
922 struct pcistub_device *psdev;
924 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
925 dev->bus->number, dev->devfn);
927 down_write(&pcistub_sem);
928 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
930 PCI_SLOT(dev->devfn),
931 PCI_FUNC(dev->devfn));
933 if (!psdev || !psdev->pdev) {
935 DRV_NAME " device is not found/assigned\n");
939 if (!psdev->pdev->sh_info) {
940 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
941 " by HVM, kill it\n");
942 kill_domain_by_device(psdev);
946 if (!test_bit(_XEN_PCIB_AERHANDLER,
947 (unsigned long *)&psdev->pdev->sh_info->flags)) {
949 "guest with no AER driver should have been killed\n");
950 kill_domain_by_device(psdev);
953 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
954 PCI_ERS_RESULT_RECOVERED);
957 pcistub_device_put(psdev);
958 up_write(&pcistub_sem);
962 /*add xen_pcibk AER handling*/
963 static const struct pci_error_handlers xen_pcibk_error_handler = {
964 .error_detected = xen_pcibk_error_detected,
965 .mmio_enabled = xen_pcibk_mmio_enabled,
966 .slot_reset = xen_pcibk_slot_reset,
967 .resume = xen_pcibk_error_resume,
971 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
972 * for a normal device. I don't want it to be loaded automatically.
975 static struct pci_driver xen_pcibk_pci_driver = {
976 /* The name should be xen_pciback, but until the tools are updated
977 * we will keep it as pciback. */
979 .id_table = pcistub_ids,
980 .probe = pcistub_probe,
981 .remove = pcistub_remove,
982 .err_handler = &xen_pcibk_error_handler,
985 static inline int str_to_slot(const char *buf, int *domain, int *bus,
986 int *slot, int *func)
990 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
994 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
998 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1001 if (parsed && !buf[parsed])
1004 /* try again without domain */
1006 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1009 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1013 sscanf(buf, " %x:*.* %n", bus, &parsed);
1016 if (parsed && !buf[parsed])
1022 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1023 *slot, int *func, int *reg, int *size, int *mask)
1027 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1028 reg, size, mask, &parsed);
1029 if (parsed && !buf[parsed])
1032 /* try again without domain */
1034 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1036 if (parsed && !buf[parsed])
1042 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1044 struct pcistub_device_id *pci_dev_id;
1045 int rc = 0, devfn = PCI_DEVFN(slot, func);
1048 for (slot = 0; !rc && slot < 32; ++slot)
1049 rc = pcistub_device_id_add(domain, bus, slot, func);
1054 for (func = 0; !rc && func < 8; ++func)
1055 rc = pcistub_device_id_add(domain, bus, slot, func);
1060 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1061 || !defined(CONFIG_PCI_DOMAINS)
1062 !pci_domains_supported ? domain :
1064 domain < 0 || domain > 0xffff)
1065 || bus < 0 || bus > 0xff
1066 || PCI_SLOT(devfn) != slot
1067 || PCI_FUNC(devfn) != func)
1070 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1074 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1075 domain, bus, slot, func);
1077 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1082 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1084 struct pcistub_device_id *pci_dev_id, *t;
1086 unsigned long flags;
1088 spin_lock_irqsave(&device_ids_lock, flags);
1089 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1091 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1092 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1093 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1094 /* Don't break; here because it's possible the same
1095 * slot could be in the list more than once
1097 list_del(&pci_dev_id->slot_list);
1102 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1103 domain, bus, slot, func);
1106 spin_unlock_irqrestore(&device_ids_lock, flags);
1111 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1112 unsigned int reg, unsigned int size,
1116 struct pcistub_device *psdev;
1117 struct pci_dev *dev;
1118 struct config_field *field;
1120 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1123 psdev = pcistub_device_find(domain, bus, slot, func);
1130 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1136 field->offset = reg;
1140 field->reset = NULL;
1141 field->release = NULL;
1142 field->clean = xen_pcibk_config_field_free;
1144 err = xen_pcibk_config_quirks_add_field(dev, field);
1149 pcistub_device_put(psdev);
1153 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1156 int domain, bus, slot, func;
1159 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1163 err = pcistub_device_id_add(domain, bus, slot, func);
1170 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1172 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1175 int domain, bus, slot, func;
1178 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1182 err = pcistub_device_id_remove(domain, bus, slot, func);
1189 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1191 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1193 struct pcistub_device_id *pci_dev_id;
1195 unsigned long flags;
1197 spin_lock_irqsave(&device_ids_lock, flags);
1198 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1199 if (count >= PAGE_SIZE)
1202 count += scnprintf(buf + count, PAGE_SIZE - count,
1203 "%04x:%02x:%02x.%d\n",
1204 pci_dev_id->domain, pci_dev_id->bus,
1205 PCI_SLOT(pci_dev_id->devfn),
1206 PCI_FUNC(pci_dev_id->devfn));
1208 spin_unlock_irqrestore(&device_ids_lock, flags);
1212 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1214 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1216 struct pcistub_device *psdev;
1217 struct xen_pcibk_dev_data *dev_data;
1219 unsigned long flags;
1221 spin_lock_irqsave(&pcistub_devices_lock, flags);
1222 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1223 if (count >= PAGE_SIZE)
1227 dev_data = pci_get_drvdata(psdev->dev);
1231 scnprintf(buf + count, PAGE_SIZE - count,
1232 "%s:%s:%sing:%ld\n",
1233 pci_name(psdev->dev),
1234 dev_data->isr_on ? "on" : "off",
1235 dev_data->ack_intr ? "ack" : "not ack",
1238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1241 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1243 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1247 struct pcistub_device *psdev;
1248 struct xen_pcibk_dev_data *dev_data;
1249 int domain, bus, slot, func;
1252 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1256 psdev = pcistub_device_find(domain, bus, slot, func);
1262 dev_data = pci_get_drvdata(psdev->dev);
1268 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1269 dev_data->irq_name, dev_data->isr_on,
1272 dev_data->isr_on = !(dev_data->isr_on);
1273 if (dev_data->isr_on)
1274 dev_data->ack_intr = 1;
1277 pcistub_device_put(psdev);
1282 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1283 pcistub_irq_handler_switch);
1285 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1288 int domain, bus, slot, func, reg, size, mask;
1291 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1296 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1304 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1307 unsigned long flags;
1308 struct xen_pcibk_config_quirk *quirk;
1309 struct xen_pcibk_dev_data *dev_data;
1310 const struct config_field *field;
1311 const struct config_field_entry *cfg_entry;
1313 spin_lock_irqsave(&device_ids_lock, flags);
1314 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1315 if (count >= PAGE_SIZE)
1318 count += scnprintf(buf + count, PAGE_SIZE - count,
1319 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1320 quirk->pdev->bus->number,
1321 PCI_SLOT(quirk->pdev->devfn),
1322 PCI_FUNC(quirk->pdev->devfn),
1323 quirk->devid.vendor, quirk->devid.device,
1324 quirk->devid.subvendor,
1325 quirk->devid.subdevice);
1327 dev_data = pci_get_drvdata(quirk->pdev);
1329 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1330 field = cfg_entry->field;
1331 if (count >= PAGE_SIZE)
1334 count += scnprintf(buf + count, PAGE_SIZE - count,
1335 "\t\t%08x:%01x:%08x\n",
1336 cfg_entry->base_offset +
1337 field->offset, field->size,
1343 spin_unlock_irqrestore(&device_ids_lock, flags);
1347 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1350 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1353 int domain, bus, slot, func;
1355 struct pcistub_device *psdev;
1356 struct xen_pcibk_dev_data *dev_data;
1358 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1362 psdev = pcistub_device_find(domain, bus, slot, func);
1368 dev_data = pci_get_drvdata(psdev->dev);
1369 /* the driver data for a device should never be null at this point */
1374 if (!dev_data->permissive) {
1375 dev_data->permissive = 1;
1376 /* Let user know that what they're doing could be unsafe */
1377 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1378 "configuration space accesses!\n");
1379 dev_warn(&psdev->dev->dev,
1380 "permissive mode is potentially unsafe!\n");
1383 pcistub_device_put(psdev);
1390 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1392 struct pcistub_device *psdev;
1393 struct xen_pcibk_dev_data *dev_data;
1395 unsigned long flags;
1396 spin_lock_irqsave(&pcistub_devices_lock, flags);
1397 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1398 if (count >= PAGE_SIZE)
1402 dev_data = pci_get_drvdata(psdev->dev);
1403 if (!dev_data || !dev_data->permissive)
1406 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1407 pci_name(psdev->dev));
1409 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1412 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1415 static void pcistub_exit(void)
1417 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1418 driver_remove_file(&xen_pcibk_pci_driver.driver,
1419 &driver_attr_remove_slot);
1420 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1421 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1422 driver_remove_file(&xen_pcibk_pci_driver.driver,
1423 &driver_attr_permissive);
1424 driver_remove_file(&xen_pcibk_pci_driver.driver,
1425 &driver_attr_irq_handlers);
1426 driver_remove_file(&xen_pcibk_pci_driver.driver,
1427 &driver_attr_irq_handler_state);
1428 pci_unregister_driver(&xen_pcibk_pci_driver);
1431 static int __init pcistub_init(void)
1435 int domain, bus, slot, func;
1438 if (pci_devs_to_hide && *pci_devs_to_hide) {
1442 err = sscanf(pci_devs_to_hide + pos,
1443 " (%x:%x:%x.%x) %n",
1444 &domain, &bus, &slot, &func, &parsed);
1448 sscanf(pci_devs_to_hide + pos,
1450 &domain, &bus, &slot, &parsed);
1454 sscanf(pci_devs_to_hide + pos,
1456 &domain, &bus, &parsed);
1462 err = sscanf(pci_devs_to_hide + pos,
1464 &bus, &slot, &func, &parsed);
1468 sscanf(pci_devs_to_hide + pos,
1470 &bus, &slot, &parsed);
1474 sscanf(pci_devs_to_hide + pos,
1484 err = pcistub_device_id_add(domain, bus, slot, func);
1489 } while (pci_devs_to_hide[pos]);
1492 /* If we're the first PCI Device Driver to register, we're the
1493 * first one to get offered PCI devices as they become
1494 * available (and thus we can be the first to grab them)
1496 err = pci_register_driver(&xen_pcibk_pci_driver);
1500 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1501 &driver_attr_new_slot);
1503 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1504 &driver_attr_remove_slot);
1506 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1507 &driver_attr_slots);
1509 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1510 &driver_attr_quirks);
1512 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1513 &driver_attr_permissive);
1516 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1517 &driver_attr_irq_handlers);
1519 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1520 &driver_attr_irq_handler_state);
1528 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1529 pci_devs_to_hide + pos);
1535 * fs_initcall happens before device_initcall
1536 * so xen_pcibk *should* get called first (b/c we
1537 * want to suck up any device before other drivers
1538 * get a chance by being the first pci device
1539 * driver to register)
1541 fs_initcall(pcistub_init);
1544 #ifdef CONFIG_PCI_IOV
1545 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1547 struct pcistub_device *psdev = NULL;
1548 unsigned long flags;
1551 spin_lock_irqsave(&pcistub_devices_lock, flags);
1552 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1553 if (!psdev->pdev && psdev->dev != pdev
1554 && pci_physfn(psdev->dev) == pdev) {
1559 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1565 static int pci_stub_notifier(struct notifier_block *nb,
1566 unsigned long action, void *data)
1568 struct device *dev = data;
1569 const struct pci_dev *pdev = to_pci_dev(dev);
1571 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1574 if (!pdev->is_physfn)
1578 struct pcistub_device *psdev = find_vfs(pdev);
1581 device_release_driver(&psdev->dev->dev);
1586 static struct notifier_block pci_stub_nb = {
1587 .notifier_call = pci_stub_notifier,
1591 static int __init xen_pcibk_init(void)
1595 if (!xen_initial_domain())
1598 err = xen_pcibk_config_init();
1603 err = pcistub_init();
1608 pcistub_init_devices_late();
1609 err = xen_pcibk_xenbus_register();
1612 #ifdef CONFIG_PCI_IOV
1614 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1620 static void __exit xen_pcibk_cleanup(void)
1622 #ifdef CONFIG_PCI_IOV
1623 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1625 xen_pcibk_xenbus_unregister();
1629 module_init(xen_pcibk_init);
1630 module_exit(xen_pcibk_cleanup);
1632 MODULE_LICENSE("Dual BSD/GPL");
1633 MODULE_ALIAS("xen-backend:pci");