2 * PCI Stub Driver - Grabs devices in backend to be exported later
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(int domain, int bus,
155 struct pcistub_device *psdev = NULL;
158 spin_lock_irqsave(&pcistub_devices_lock, flags);
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
166 pcistub_device_get(psdev);
175 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
179 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
180 struct pcistub_device *psdev)
182 struct pci_dev *pci_dev = NULL;
185 pcistub_device_get(psdev);
187 spin_lock_irqsave(&psdev->lock, flags);
190 pci_dev = psdev->dev;
192 spin_unlock_irqrestore(&psdev->lock, flags);
195 pcistub_device_put(psdev);
200 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
204 struct pcistub_device *psdev;
205 struct pci_dev *found_dev = NULL;
208 spin_lock_irqsave(&pcistub_devices_lock, flags);
210 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
211 if (psdev->dev != NULL
212 && domain == pci_domain_nr(psdev->dev->bus)
213 && bus == psdev->dev->bus->number
214 && slot == PCI_SLOT(psdev->dev->devfn)
215 && func == PCI_FUNC(psdev->dev->devfn)) {
216 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 int pcistub_seize(struct pci_dev *dev)
483 struct pcistub_device *psdev;
487 psdev = pcistub_device_alloc(dev);
491 spin_lock_irqsave(&pcistub_devices_lock, flags);
493 if (initialize_devices) {
494 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
496 /* don't want irqs disabled when calling pcistub_init_device */
497 err = pcistub_init_device(psdev->dev);
499 spin_lock_irqsave(&pcistub_devices_lock, flags);
502 list_add(&psdev->dev_list, &pcistub_devices);
504 dev_dbg(&dev->dev, "deferring initialization\n");
505 list_add(&psdev->dev_list, &seized_devices);
508 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
511 pcistub_device_put(psdev);
516 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
517 * other functions that take the sysfs lock. */
518 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
522 dev_dbg(&dev->dev, "probing...\n");
524 if (pcistub_match(dev)) {
526 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
527 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
528 dev_err(&dev->dev, "can't export pci devices that "
529 "don't have a normal (0) or bridge (1) "
535 dev_info(&dev->dev, "seizing device\n");
536 err = pcistub_seize(dev);
538 /* Didn't find the device */
545 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
546 * other functions that take the sysfs lock. */
547 static void pcistub_remove(struct pci_dev *dev)
549 struct pcistub_device *psdev, *found_psdev = NULL;
552 dev_dbg(&dev->dev, "removing\n");
554 spin_lock_irqsave(&pcistub_devices_lock, flags);
556 xen_pcibk_config_quirk_release(dev);
558 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
559 if (psdev->dev == dev) {
565 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
568 dev_dbg(&dev->dev, "found device to remove %s\n",
569 found_psdev->pdev ? "- in-use" : "");
571 if (found_psdev->pdev) {
572 int domid = xen_find_device_domain_owner(dev);
574 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
575 pci_name(found_psdev->dev), domid);
576 pr_warn("****** driver domain may still access this device's i/o resources!\n");
577 pr_warn("****** shutdown driver domain before binding device\n");
578 pr_warn("****** to other drivers or domains\n");
580 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
582 xen_pcibk_release_pci_dev(found_psdev->pdev,
584 false /* caller holds the lock. */);
587 spin_lock_irqsave(&pcistub_devices_lock, flags);
588 list_del(&found_psdev->dev_list);
589 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
591 /* the final put for releasing from the list */
592 pcistub_device_put(found_psdev);
596 static const struct pci_device_id pcistub_ids[] = {
598 .vendor = PCI_ANY_ID,
599 .device = PCI_ANY_ID,
600 .subvendor = PCI_ANY_ID,
601 .subdevice = PCI_ANY_ID,
606 #define PCI_NODENAME_MAX 40
607 static void kill_domain_by_device(struct pcistub_device *psdev)
609 struct xenbus_transaction xbt;
611 char nodename[PCI_NODENAME_MAX];
614 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
615 psdev->pdev->xdev->otherend_id);
618 err = xenbus_transaction_start(&xbt);
620 dev_err(&psdev->dev->dev,
621 "error %d when start xenbus transaction\n", err);
624 /*PV AER handlers will set this flag*/
625 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
626 err = xenbus_transaction_end(xbt, 0);
630 dev_err(&psdev->dev->dev,
631 "error %d when end xenbus transaction\n", err);
636 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
637 * backend need to have cooperation. In xen_pcibk, those steps will do similar
638 * jobs: send service request and waiting for front_end response.
640 static pci_ers_result_t common_process(struct pcistub_device *psdev,
641 pci_channel_state_t state, int aer_cmd,
642 pci_ers_result_t result)
644 pci_ers_result_t res = result;
645 struct xen_pcie_aer_op *aer_op;
646 struct xen_pcibk_device *pdev = psdev->pdev;
647 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
650 /*with PV AER drivers*/
651 aer_op = &(sh_info->aer_op);
652 aer_op->cmd = aer_cmd ;
653 /*useful for error_detected callback*/
656 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
657 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
659 dev_err(&psdev->dev->dev,
660 DRV_NAME ": failed to get pcifront device\n");
661 return PCI_ERS_RESULT_NONE;
665 dev_dbg(&psdev->dev->dev,
666 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
667 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
668 /*local flag to mark there's aer request, xen_pcibk callback will use
669 * this flag to judge whether we need to check pci-front give aer
672 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
674 /*It is possible that a pcifront conf_read_write ops request invokes
675 * the callback which cause the spurious execution of wake_up.
676 * Yet it is harmless and better than a spinlock here
678 set_bit(_XEN_PCIB_active,
679 (unsigned long *)&sh_info->flags);
681 notify_remote_via_irq(pdev->evtchn_irq);
683 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
684 !(test_bit(_XEN_PCIB_active, (unsigned long *)
685 &sh_info->flags)), 300*HZ);
688 if (test_bit(_XEN_PCIB_active,
689 (unsigned long *)&sh_info->flags)) {
690 dev_err(&psdev->dev->dev,
691 "pcifront aer process not responding!\n");
692 clear_bit(_XEN_PCIB_active,
693 (unsigned long *)&sh_info->flags);
694 aer_op->err = PCI_ERS_RESULT_NONE;
698 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
700 if (test_bit(_XEN_PCIF_active,
701 (unsigned long *)&sh_info->flags)) {
702 dev_dbg(&psdev->dev->dev,
703 "schedule pci_conf service in " DRV_NAME "\n");
704 xen_pcibk_test_and_schedule_op(psdev->pdev);
707 res = (pci_ers_result_t)aer_op->err;
712 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
713 * of the device driver could provide this service, and then wait for pcifront
715 * @dev: pointer to PCI devices
716 * return value is used by aer_core do_recovery policy
718 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
720 struct pcistub_device *psdev;
721 pci_ers_result_t result;
723 result = PCI_ERS_RESULT_RECOVERED;
724 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
725 dev->bus->number, dev->devfn);
727 down_write(&pcistub_sem);
728 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
730 PCI_SLOT(dev->devfn),
731 PCI_FUNC(dev->devfn));
733 if (!psdev || !psdev->pdev) {
735 DRV_NAME " device is not found/assigned\n");
739 if (!psdev->pdev->sh_info) {
740 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
741 " by HVM, kill it\n");
742 kill_domain_by_device(psdev);
746 if (!test_bit(_XEN_PCIB_AERHANDLER,
747 (unsigned long *)&psdev->pdev->sh_info->flags)) {
749 "guest with no AER driver should have been killed\n");
752 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
754 if (result == PCI_ERS_RESULT_NONE ||
755 result == PCI_ERS_RESULT_DISCONNECT) {
757 "No AER slot_reset service or disconnected!\n");
758 kill_domain_by_device(psdev);
762 pcistub_device_put(psdev);
763 up_write(&pcistub_sem);
769 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
770 * in case of the device driver could provide this service, and then wait
772 * @dev: pointer to PCI devices
773 * return value is used by aer_core do_recovery policy
776 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
778 struct pcistub_device *psdev;
779 pci_ers_result_t result;
781 result = PCI_ERS_RESULT_RECOVERED;
782 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
783 dev->bus->number, dev->devfn);
785 down_write(&pcistub_sem);
786 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
788 PCI_SLOT(dev->devfn),
789 PCI_FUNC(dev->devfn));
791 if (!psdev || !psdev->pdev) {
793 DRV_NAME " device is not found/assigned\n");
797 if (!psdev->pdev->sh_info) {
798 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
799 " by HVM, kill it\n");
800 kill_domain_by_device(psdev);
804 if (!test_bit(_XEN_PCIB_AERHANDLER,
805 (unsigned long *)&psdev->pdev->sh_info->flags)) {
807 "guest with no AER driver should have been killed\n");
810 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
812 if (result == PCI_ERS_RESULT_NONE ||
813 result == PCI_ERS_RESULT_DISCONNECT) {
815 "No AER mmio_enabled service or disconnected!\n");
816 kill_domain_by_device(psdev);
820 pcistub_device_put(psdev);
821 up_write(&pcistub_sem);
825 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
826 * in case of the device driver could provide this service, and then wait
828 * @dev: pointer to PCI devices
829 * @error: the current PCI connection state
830 * return value is used by aer_core do_recovery policy
833 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
834 pci_channel_state_t error)
836 struct pcistub_device *psdev;
837 pci_ers_result_t result;
839 result = PCI_ERS_RESULT_CAN_RECOVER;
840 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
841 dev->bus->number, dev->devfn);
843 down_write(&pcistub_sem);
844 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
846 PCI_SLOT(dev->devfn),
847 PCI_FUNC(dev->devfn));
849 if (!psdev || !psdev->pdev) {
851 DRV_NAME " device is not found/assigned\n");
855 if (!psdev->pdev->sh_info) {
856 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
857 " by HVM, kill it\n");
858 kill_domain_by_device(psdev);
862 /*Guest owns the device yet no aer handler regiested, kill guest*/
863 if (!test_bit(_XEN_PCIB_AERHANDLER,
864 (unsigned long *)&psdev->pdev->sh_info->flags)) {
865 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
866 kill_domain_by_device(psdev);
869 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
871 if (result == PCI_ERS_RESULT_NONE ||
872 result == PCI_ERS_RESULT_DISCONNECT) {
874 "No AER error_detected service or disconnected!\n");
875 kill_domain_by_device(psdev);
879 pcistub_device_put(psdev);
880 up_write(&pcistub_sem);
884 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
885 * in case of the device driver could provide this service, and then wait
887 * @dev: pointer to PCI devices
890 static void xen_pcibk_error_resume(struct pci_dev *dev)
892 struct pcistub_device *psdev;
894 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
895 dev->bus->number, dev->devfn);
897 down_write(&pcistub_sem);
898 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
900 PCI_SLOT(dev->devfn),
901 PCI_FUNC(dev->devfn));
903 if (!psdev || !psdev->pdev) {
905 DRV_NAME " device is not found/assigned\n");
909 if (!psdev->pdev->sh_info) {
910 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
911 " by HVM, kill it\n");
912 kill_domain_by_device(psdev);
916 if (!test_bit(_XEN_PCIB_AERHANDLER,
917 (unsigned long *)&psdev->pdev->sh_info->flags)) {
919 "guest with no AER driver should have been killed\n");
920 kill_domain_by_device(psdev);
923 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
924 PCI_ERS_RESULT_RECOVERED);
927 pcistub_device_put(psdev);
928 up_write(&pcistub_sem);
932 /*add xen_pcibk AER handling*/
933 static const struct pci_error_handlers xen_pcibk_error_handler = {
934 .error_detected = xen_pcibk_error_detected,
935 .mmio_enabled = xen_pcibk_mmio_enabled,
936 .slot_reset = xen_pcibk_slot_reset,
937 .resume = xen_pcibk_error_resume,
941 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
942 * for a normal device. I don't want it to be loaded automatically.
945 static struct pci_driver xen_pcibk_pci_driver = {
946 /* The name should be xen_pciback, but until the tools are updated
947 * we will keep it as pciback. */
949 .id_table = pcistub_ids,
950 .probe = pcistub_probe,
951 .remove = pcistub_remove,
952 .err_handler = &xen_pcibk_error_handler,
955 static inline int str_to_slot(const char *buf, int *domain, int *bus,
956 int *slot, int *func)
960 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
964 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
968 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
971 if (parsed && !buf[parsed])
974 /* try again without domain */
976 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
979 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
983 sscanf(buf, " %x:*.* %n", bus, &parsed);
986 if (parsed && !buf[parsed])
992 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
993 *slot, int *func, int *reg, int *size, int *mask)
997 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
998 reg, size, mask, &parsed);
999 if (parsed && !buf[parsed])
1002 /* try again without domain */
1004 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1006 if (parsed && !buf[parsed])
1012 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1014 struct pcistub_device_id *pci_dev_id;
1015 unsigned long flags;
1016 int rc = 0, devfn = PCI_DEVFN(slot, func);
1019 for (slot = 0; !rc && slot < 32; ++slot)
1020 rc = pcistub_device_id_add(domain, bus, slot, func);
1025 for (func = 0; !rc && func < 8; ++func)
1026 rc = pcistub_device_id_add(domain, bus, slot, func);
1031 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1032 || !defined(CONFIG_PCI_DOMAINS)
1033 !pci_domains_supported ? domain :
1035 domain < 0 || domain > 0xffff)
1036 || bus < 0 || bus > 0xff
1037 || PCI_SLOT(devfn) != slot
1038 || PCI_FUNC(devfn) != func)
1041 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1045 pci_dev_id->domain = domain;
1046 pci_dev_id->bus = bus;
1047 pci_dev_id->devfn = devfn;
1049 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1050 domain, bus, slot, func);
1052 spin_lock_irqsave(&device_ids_lock, flags);
1053 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1054 spin_unlock_irqrestore(&device_ids_lock, flags);
1059 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1061 struct pcistub_device_id *pci_dev_id, *t;
1063 unsigned long flags;
1065 spin_lock_irqsave(&device_ids_lock, flags);
1066 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1068 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1069 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1070 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1071 /* Don't break; here because it's possible the same
1072 * slot could be in the list more than once
1074 list_del(&pci_dev_id->slot_list);
1079 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1080 domain, bus, slot, func);
1083 spin_unlock_irqrestore(&device_ids_lock, flags);
1088 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1089 unsigned int reg, unsigned int size,
1093 struct pcistub_device *psdev;
1094 struct pci_dev *dev;
1095 struct config_field *field;
1097 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1100 psdev = pcistub_device_find(domain, bus, slot, func);
1107 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1113 field->offset = reg;
1117 field->reset = NULL;
1118 field->release = NULL;
1119 field->clean = xen_pcibk_config_field_free;
1121 err = xen_pcibk_config_quirks_add_field(dev, field);
1126 pcistub_device_put(psdev);
1130 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1133 int domain, bus, slot, func;
1136 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1140 err = pcistub_device_id_add(domain, bus, slot, func);
1147 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1149 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1152 int domain, bus, slot, func;
1155 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1159 err = pcistub_device_id_remove(domain, bus, slot, func);
1166 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1168 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1170 struct pcistub_device_id *pci_dev_id;
1172 unsigned long flags;
1174 spin_lock_irqsave(&device_ids_lock, flags);
1175 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1176 if (count >= PAGE_SIZE)
1179 count += scnprintf(buf + count, PAGE_SIZE - count,
1180 "%04x:%02x:%02x.%d\n",
1181 pci_dev_id->domain, pci_dev_id->bus,
1182 PCI_SLOT(pci_dev_id->devfn),
1183 PCI_FUNC(pci_dev_id->devfn));
1185 spin_unlock_irqrestore(&device_ids_lock, flags);
1189 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1191 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1193 struct pcistub_device *psdev;
1194 struct xen_pcibk_dev_data *dev_data;
1196 unsigned long flags;
1198 spin_lock_irqsave(&pcistub_devices_lock, flags);
1199 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1200 if (count >= PAGE_SIZE)
1204 dev_data = pci_get_drvdata(psdev->dev);
1208 scnprintf(buf + count, PAGE_SIZE - count,
1209 "%s:%s:%sing:%ld\n",
1210 pci_name(psdev->dev),
1211 dev_data->isr_on ? "on" : "off",
1212 dev_data->ack_intr ? "ack" : "not ack",
1215 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1218 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1220 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1224 struct pcistub_device *psdev;
1225 struct xen_pcibk_dev_data *dev_data;
1226 int domain, bus, slot, func;
1229 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1233 psdev = pcistub_device_find(domain, bus, slot, func);
1239 dev_data = pci_get_drvdata(psdev->dev);
1245 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1246 dev_data->irq_name, dev_data->isr_on,
1249 dev_data->isr_on = !(dev_data->isr_on);
1250 if (dev_data->isr_on)
1251 dev_data->ack_intr = 1;
1254 pcistub_device_put(psdev);
1259 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1260 pcistub_irq_handler_switch);
1262 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1265 int domain, bus, slot, func, reg, size, mask;
1268 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1273 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1281 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1284 unsigned long flags;
1285 struct xen_pcibk_config_quirk *quirk;
1286 struct xen_pcibk_dev_data *dev_data;
1287 const struct config_field *field;
1288 const struct config_field_entry *cfg_entry;
1290 spin_lock_irqsave(&device_ids_lock, flags);
1291 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1292 if (count >= PAGE_SIZE)
1295 count += scnprintf(buf + count, PAGE_SIZE - count,
1296 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1297 quirk->pdev->bus->number,
1298 PCI_SLOT(quirk->pdev->devfn),
1299 PCI_FUNC(quirk->pdev->devfn),
1300 quirk->devid.vendor, quirk->devid.device,
1301 quirk->devid.subvendor,
1302 quirk->devid.subdevice);
1304 dev_data = pci_get_drvdata(quirk->pdev);
1306 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1307 field = cfg_entry->field;
1308 if (count >= PAGE_SIZE)
1311 count += scnprintf(buf + count, PAGE_SIZE - count,
1312 "\t\t%08x:%01x:%08x\n",
1313 cfg_entry->base_offset +
1314 field->offset, field->size,
1320 spin_unlock_irqrestore(&device_ids_lock, flags);
1324 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1327 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1330 int domain, bus, slot, func;
1332 struct pcistub_device *psdev;
1333 struct xen_pcibk_dev_data *dev_data;
1335 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1339 psdev = pcistub_device_find(domain, bus, slot, func);
1345 dev_data = pci_get_drvdata(psdev->dev);
1346 /* the driver data for a device should never be null at this point */
1351 if (!dev_data->permissive) {
1352 dev_data->permissive = 1;
1353 /* Let user know that what they're doing could be unsafe */
1354 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1355 "configuration space accesses!\n");
1356 dev_warn(&psdev->dev->dev,
1357 "permissive mode is potentially unsafe!\n");
1360 pcistub_device_put(psdev);
1367 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1369 struct pcistub_device *psdev;
1370 struct xen_pcibk_dev_data *dev_data;
1372 unsigned long flags;
1373 spin_lock_irqsave(&pcistub_devices_lock, flags);
1374 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1375 if (count >= PAGE_SIZE)
1379 dev_data = pci_get_drvdata(psdev->dev);
1380 if (!dev_data || !dev_data->permissive)
1383 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1384 pci_name(psdev->dev));
1386 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1389 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1392 static void pcistub_exit(void)
1394 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1395 driver_remove_file(&xen_pcibk_pci_driver.driver,
1396 &driver_attr_remove_slot);
1397 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1398 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1399 driver_remove_file(&xen_pcibk_pci_driver.driver,
1400 &driver_attr_permissive);
1401 driver_remove_file(&xen_pcibk_pci_driver.driver,
1402 &driver_attr_irq_handlers);
1403 driver_remove_file(&xen_pcibk_pci_driver.driver,
1404 &driver_attr_irq_handler_state);
1405 pci_unregister_driver(&xen_pcibk_pci_driver);
1408 static int __init pcistub_init(void)
1412 int domain, bus, slot, func;
1415 if (pci_devs_to_hide && *pci_devs_to_hide) {
1419 err = sscanf(pci_devs_to_hide + pos,
1420 " (%x:%x:%x.%x) %n",
1421 &domain, &bus, &slot, &func, &parsed);
1425 sscanf(pci_devs_to_hide + pos,
1427 &domain, &bus, &slot, &parsed);
1431 sscanf(pci_devs_to_hide + pos,
1433 &domain, &bus, &parsed);
1439 err = sscanf(pci_devs_to_hide + pos,
1441 &bus, &slot, &func, &parsed);
1445 sscanf(pci_devs_to_hide + pos,
1447 &bus, &slot, &parsed);
1451 sscanf(pci_devs_to_hide + pos,
1461 err = pcistub_device_id_add(domain, bus, slot, func);
1466 } while (pci_devs_to_hide[pos]);
1469 /* If we're the first PCI Device Driver to register, we're the
1470 * first one to get offered PCI devices as they become
1471 * available (and thus we can be the first to grab them)
1473 err = pci_register_driver(&xen_pcibk_pci_driver);
1477 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1478 &driver_attr_new_slot);
1480 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1481 &driver_attr_remove_slot);
1483 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1484 &driver_attr_slots);
1486 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1487 &driver_attr_quirks);
1489 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1490 &driver_attr_permissive);
1493 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1494 &driver_attr_irq_handlers);
1496 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1497 &driver_attr_irq_handler_state);
1505 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1506 pci_devs_to_hide + pos);
1512 * fs_initcall happens before device_initcall
1513 * so xen_pcibk *should* get called first (b/c we
1514 * want to suck up any device before other drivers
1515 * get a chance by being the first pci device
1516 * driver to register)
1518 fs_initcall(pcistub_init);
1521 #ifdef CONFIG_PCI_IOV
1522 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1524 struct pcistub_device *psdev = NULL;
1525 unsigned long flags;
1528 spin_lock_irqsave(&pcistub_devices_lock, flags);
1529 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1530 if (!psdev->pdev && psdev->dev != pdev
1531 && pci_physfn(psdev->dev) == pdev) {
1536 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1542 static int pci_stub_notifier(struct notifier_block *nb,
1543 unsigned long action, void *data)
1545 struct device *dev = data;
1546 const struct pci_dev *pdev = to_pci_dev(dev);
1548 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1551 if (!pdev->is_physfn)
1555 struct pcistub_device *psdev = find_vfs(pdev);
1558 device_release_driver(&psdev->dev->dev);
1563 static struct notifier_block pci_stub_nb = {
1564 .notifier_call = pci_stub_notifier,
1568 static int __init xen_pcibk_init(void)
1572 if (!xen_initial_domain())
1575 err = xen_pcibk_config_init();
1580 err = pcistub_init();
1585 pcistub_init_devices_late();
1586 err = xen_pcibk_xenbus_register();
1589 #ifdef CONFIG_PCI_IOV
1591 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1597 static void __exit xen_pcibk_cleanup(void)
1599 #ifdef CONFIG_PCI_IOV
1600 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1602 xen_pcibk_xenbus_unregister();
1606 module_init(xen_pcibk_init);
1607 module_exit(xen_pcibk_cleanup);
1609 MODULE_LICENSE("Dual BSD/GPL");
1610 MODULE_ALIAS("xen-backend:pci");