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_dbg(&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,
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 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
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);
245 void pcistub_put_pci_dev(struct pci_dev *dev)
247 struct pcistub_device *psdev, *found_psdev = NULL;
250 spin_lock_irqsave(&pcistub_devices_lock, flags);
252 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
253 if (psdev->dev == dev) {
259 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
260 if (WARN_ON(!found_psdev))
263 /*hold this lock for avoiding breaking link between
264 * pcistub and xen_pcibk when AER is in processing
266 down_write(&pcistub_sem);
267 /* Cleanup our device
268 * (so it's ready for the next domain)
271 /* This is OK - we are running from workqueue context
272 * and want to inhibit the user from fiddling with 'reset'
274 pci_reset_function(dev);
275 pci_restore_state(dev);
277 /* This disables the device. */
278 xen_pcibk_reset_device(dev);
280 /* And cleanup up our emulated fields. */
281 xen_pcibk_config_reset_dev(dev);
282 xen_pcibk_config_free_dyn_fields(dev);
284 xen_unregister_device_domain_owner(dev);
286 spin_lock_irqsave(&found_psdev->lock, flags);
287 found_psdev->pdev = NULL;
288 spin_unlock_irqrestore(&found_psdev->lock, flags);
290 pcistub_device_put(found_psdev);
291 up_write(&pcistub_sem);
294 static int pcistub_match_one(struct pci_dev *dev,
295 struct pcistub_device_id *pdev_id)
297 /* Match the specified device by domain, bus, slot, func and also if
298 * any of the device's parent bridges match.
300 for (; dev != NULL; dev = dev->bus->self) {
301 if (pci_domain_nr(dev->bus) == pdev_id->domain
302 && dev->bus->number == pdev_id->bus
303 && dev->devfn == pdev_id->devfn)
306 /* Sometimes topmost bridge links to itself. */
307 if (dev == dev->bus->self)
314 static int pcistub_match(struct pci_dev *dev)
316 struct pcistub_device_id *pdev_id;
320 spin_lock_irqsave(&device_ids_lock, flags);
321 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
322 if (pcistub_match_one(dev, pdev_id)) {
327 spin_unlock_irqrestore(&device_ids_lock, flags);
332 static int pcistub_init_device(struct pci_dev *dev)
334 struct xen_pcibk_dev_data *dev_data;
337 dev_dbg(&dev->dev, "initializing...\n");
339 /* The PCI backend is not intended to be a module (or to work with
340 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
341 * would need to be called somewhere to free the memory allocated
342 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
344 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
345 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
350 pci_set_drvdata(dev, dev_data);
353 * Setup name for fake IRQ handler. It will only be enabled
354 * once the device is turned on by the guest.
356 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
358 dev_dbg(&dev->dev, "initializing config\n");
360 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
361 err = xen_pcibk_config_init_dev(dev);
365 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
366 * must do this here because pcibios_enable_device may specify
367 * the pci device's true irq (and possibly its other resources)
368 * if they differ from what's in the configuration space.
369 * This makes the assumption that the device's resources won't
370 * change after this point (otherwise this code may break!)
372 dev_dbg(&dev->dev, "enabling device\n");
373 err = pci_enable_device(dev);
378 struct physdev_pci_device ppdev = {
379 .seg = pci_domain_nr(dev->bus),
380 .bus = dev->bus->number,
384 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
386 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
390 /* We need the device active to save the state. */
391 dev_dbg(&dev->dev, "save state of device\n");
393 dev_data->pci_saved_state = pci_store_saved_state(dev);
394 if (!dev_data->pci_saved_state)
395 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
397 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
398 __pci_reset_function_locked(dev);
399 pci_restore_state(dev);
401 /* Now disable the device (this also ensures some private device
402 * data is setup before we export)
404 dev_dbg(&dev->dev, "reset device\n");
405 xen_pcibk_reset_device(dev);
407 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
411 xen_pcibk_config_free_dev(dev);
414 pci_set_drvdata(dev, NULL);
420 * Because some initialization still happens on
421 * devices during fs_initcall, we need to defer
422 * full initialization of our devices until
425 static int __init pcistub_init_devices_late(void)
427 struct pcistub_device *psdev;
431 spin_lock_irqsave(&pcistub_devices_lock, flags);
433 while (!list_empty(&seized_devices)) {
434 psdev = container_of(seized_devices.next,
435 struct pcistub_device, dev_list);
436 list_del(&psdev->dev_list);
438 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
440 err = pcistub_init_device(psdev->dev);
442 dev_err(&psdev->dev->dev,
443 "error %d initializing device\n", err);
448 spin_lock_irqsave(&pcistub_devices_lock, flags);
451 list_add_tail(&psdev->dev_list, &pcistub_devices);
454 initialize_devices = 1;
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
461 static int pcistub_seize(struct pci_dev *dev)
463 struct pcistub_device *psdev;
467 psdev = pcistub_device_alloc(dev);
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
473 if (initialize_devices) {
474 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
476 /* don't want irqs disabled when calling pcistub_init_device */
477 err = pcistub_init_device(psdev->dev);
479 spin_lock_irqsave(&pcistub_devices_lock, flags);
482 list_add(&psdev->dev_list, &pcistub_devices);
484 dev_dbg(&dev->dev, "deferring initialization\n");
485 list_add(&psdev->dev_list, &seized_devices);
488 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
491 pcistub_device_put(psdev);
496 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
500 dev_dbg(&dev->dev, "probing...\n");
502 if (pcistub_match(dev)) {
504 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
505 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
506 dev_err(&dev->dev, "can't export pci devices that "
507 "don't have a normal (0) or bridge (1) "
513 dev_info(&dev->dev, "seizing device\n");
514 err = pcistub_seize(dev);
516 /* Didn't find the device */
523 static void pcistub_remove(struct pci_dev *dev)
525 struct pcistub_device *psdev, *found_psdev = NULL;
528 dev_dbg(&dev->dev, "removing\n");
530 spin_lock_irqsave(&pcistub_devices_lock, flags);
532 xen_pcibk_config_quirk_release(dev);
534 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
535 if (psdev->dev == dev) {
541 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
544 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
547 if (found_psdev->pdev) {
548 pr_warn("****** removing device %s while still in-use! ******\n",
549 pci_name(found_psdev->dev));
550 pr_warn("****** driver domain may still access this device's i/o resources!\n");
551 pr_warn("****** shutdown driver domain before binding device\n");
552 pr_warn("****** to other drivers or domains\n");
554 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
556 xen_pcibk_release_pci_dev(found_psdev->pdev,
560 spin_lock_irqsave(&pcistub_devices_lock, flags);
561 list_del(&found_psdev->dev_list);
562 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
564 /* the final put for releasing from the list */
565 pcistub_device_put(found_psdev);
569 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
571 .vendor = PCI_ANY_ID,
572 .device = PCI_ANY_ID,
573 .subvendor = PCI_ANY_ID,
574 .subdevice = PCI_ANY_ID,
579 #define PCI_NODENAME_MAX 40
580 static void kill_domain_by_device(struct pcistub_device *psdev)
582 struct xenbus_transaction xbt;
584 char nodename[PCI_NODENAME_MAX];
587 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
588 psdev->pdev->xdev->otherend_id);
591 err = xenbus_transaction_start(&xbt);
593 dev_err(&psdev->dev->dev,
594 "error %d when start xenbus transaction\n", err);
597 /*PV AER handlers will set this flag*/
598 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
599 err = xenbus_transaction_end(xbt, 0);
603 dev_err(&psdev->dev->dev,
604 "error %d when end xenbus transaction\n", err);
609 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
610 * backend need to have cooperation. In xen_pcibk, those steps will do similar
611 * jobs: send service request and waiting for front_end response.
613 static pci_ers_result_t common_process(struct pcistub_device *psdev,
614 pci_channel_state_t state, int aer_cmd,
615 pci_ers_result_t result)
617 pci_ers_result_t res = result;
618 struct xen_pcie_aer_op *aer_op;
621 /*with PV AER drivers*/
622 aer_op = &(psdev->pdev->sh_info->aer_op);
623 aer_op->cmd = aer_cmd ;
624 /*useful for error_detected callback*/
627 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
628 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
630 dev_err(&psdev->dev->dev,
631 DRV_NAME ": failed to get pcifront device\n");
632 return PCI_ERS_RESULT_NONE;
636 dev_dbg(&psdev->dev->dev,
637 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
638 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
639 /*local flag to mark there's aer request, xen_pcibk callback will use
640 * this flag to judge whether we need to check pci-front give aer
643 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
645 /*It is possible that a pcifront conf_read_write ops request invokes
646 * the callback which cause the spurious execution of wake_up.
647 * Yet it is harmless and better than a spinlock here
649 set_bit(_XEN_PCIB_active,
650 (unsigned long *)&psdev->pdev->sh_info->flags);
652 notify_remote_via_irq(psdev->pdev->evtchn_irq);
654 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
655 !(test_bit(_XEN_PCIB_active, (unsigned long *)
656 &psdev->pdev->sh_info->flags)), 300*HZ);
659 if (test_bit(_XEN_PCIB_active,
660 (unsigned long *)&psdev->pdev->sh_info->flags)) {
661 dev_err(&psdev->dev->dev,
662 "pcifront aer process not responding!\n");
663 clear_bit(_XEN_PCIB_active,
664 (unsigned long *)&psdev->pdev->sh_info->flags);
665 aer_op->err = PCI_ERS_RESULT_NONE;
669 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
671 if (test_bit(_XEN_PCIF_active,
672 (unsigned long *)&psdev->pdev->sh_info->flags)) {
673 dev_dbg(&psdev->dev->dev,
674 "schedule pci_conf service in " DRV_NAME "\n");
675 xen_pcibk_test_and_schedule_op(psdev->pdev);
678 res = (pci_ers_result_t)aer_op->err;
683 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
684 * of the device driver could provide this service, and then wait for pcifront
686 * @dev: pointer to PCI devices
687 * return value is used by aer_core do_recovery policy
689 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
691 struct pcistub_device *psdev;
692 pci_ers_result_t result;
694 result = PCI_ERS_RESULT_RECOVERED;
695 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
696 dev->bus->number, dev->devfn);
698 down_write(&pcistub_sem);
699 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
701 PCI_SLOT(dev->devfn),
702 PCI_FUNC(dev->devfn));
704 if (!psdev || !psdev->pdev) {
706 DRV_NAME " device is not found/assigned\n");
710 if (!psdev->pdev->sh_info) {
711 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
712 " by HVM, kill it\n");
713 kill_domain_by_device(psdev);
717 if (!test_bit(_XEN_PCIB_AERHANDLER,
718 (unsigned long *)&psdev->pdev->sh_info->flags)) {
720 "guest with no AER driver should have been killed\n");
723 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
725 if (result == PCI_ERS_RESULT_NONE ||
726 result == PCI_ERS_RESULT_DISCONNECT) {
728 "No AER slot_reset service or disconnected!\n");
729 kill_domain_by_device(psdev);
733 pcistub_device_put(psdev);
734 up_write(&pcistub_sem);
740 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
741 * in case of the device driver could provide this service, and then wait
743 * @dev: pointer to PCI devices
744 * return value is used by aer_core do_recovery policy
747 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
749 struct pcistub_device *psdev;
750 pci_ers_result_t result;
752 result = PCI_ERS_RESULT_RECOVERED;
753 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
754 dev->bus->number, dev->devfn);
756 down_write(&pcistub_sem);
757 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
759 PCI_SLOT(dev->devfn),
760 PCI_FUNC(dev->devfn));
762 if (!psdev || !psdev->pdev) {
764 DRV_NAME " device is not found/assigned\n");
768 if (!psdev->pdev->sh_info) {
769 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
770 " by HVM, kill it\n");
771 kill_domain_by_device(psdev);
775 if (!test_bit(_XEN_PCIB_AERHANDLER,
776 (unsigned long *)&psdev->pdev->sh_info->flags)) {
778 "guest with no AER driver should have been killed\n");
781 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
783 if (result == PCI_ERS_RESULT_NONE ||
784 result == PCI_ERS_RESULT_DISCONNECT) {
786 "No AER mmio_enabled service or disconnected!\n");
787 kill_domain_by_device(psdev);
791 pcistub_device_put(psdev);
792 up_write(&pcistub_sem);
796 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
797 * in case of the device driver could provide this service, and then wait
799 * @dev: pointer to PCI devices
800 * @error: the current PCI connection state
801 * return value is used by aer_core do_recovery policy
804 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
805 pci_channel_state_t error)
807 struct pcistub_device *psdev;
808 pci_ers_result_t result;
810 result = PCI_ERS_RESULT_CAN_RECOVER;
811 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
812 dev->bus->number, dev->devfn);
814 down_write(&pcistub_sem);
815 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
817 PCI_SLOT(dev->devfn),
818 PCI_FUNC(dev->devfn));
820 if (!psdev || !psdev->pdev) {
822 DRV_NAME " device is not found/assigned\n");
826 if (!psdev->pdev->sh_info) {
827 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
828 " by HVM, kill it\n");
829 kill_domain_by_device(psdev);
833 /*Guest owns the device yet no aer handler regiested, kill guest*/
834 if (!test_bit(_XEN_PCIB_AERHANDLER,
835 (unsigned long *)&psdev->pdev->sh_info->flags)) {
836 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
837 kill_domain_by_device(psdev);
840 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
842 if (result == PCI_ERS_RESULT_NONE ||
843 result == PCI_ERS_RESULT_DISCONNECT) {
845 "No AER error_detected service or disconnected!\n");
846 kill_domain_by_device(psdev);
850 pcistub_device_put(psdev);
851 up_write(&pcistub_sem);
855 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
856 * in case of the device driver could provide this service, and then wait
858 * @dev: pointer to PCI devices
861 static void xen_pcibk_error_resume(struct pci_dev *dev)
863 struct pcistub_device *psdev;
865 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
866 dev->bus->number, dev->devfn);
868 down_write(&pcistub_sem);
869 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
871 PCI_SLOT(dev->devfn),
872 PCI_FUNC(dev->devfn));
874 if (!psdev || !psdev->pdev) {
876 DRV_NAME " device is not found/assigned\n");
880 if (!psdev->pdev->sh_info) {
881 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
882 " by HVM, kill it\n");
883 kill_domain_by_device(psdev);
887 if (!test_bit(_XEN_PCIB_AERHANDLER,
888 (unsigned long *)&psdev->pdev->sh_info->flags)) {
890 "guest with no AER driver should have been killed\n");
891 kill_domain_by_device(psdev);
894 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
895 PCI_ERS_RESULT_RECOVERED);
898 pcistub_device_put(psdev);
899 up_write(&pcistub_sem);
903 /*add xen_pcibk AER handling*/
904 static const struct pci_error_handlers xen_pcibk_error_handler = {
905 .error_detected = xen_pcibk_error_detected,
906 .mmio_enabled = xen_pcibk_mmio_enabled,
907 .slot_reset = xen_pcibk_slot_reset,
908 .resume = xen_pcibk_error_resume,
912 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
913 * for a normal device. I don't want it to be loaded automatically.
916 static struct pci_driver xen_pcibk_pci_driver = {
917 /* The name should be xen_pciback, but until the tools are updated
918 * we will keep it as pciback. */
920 .id_table = pcistub_ids,
921 .probe = pcistub_probe,
922 .remove = pcistub_remove,
923 .err_handler = &xen_pcibk_error_handler,
926 static inline int str_to_slot(const char *buf, int *domain, int *bus,
927 int *slot, int *func)
931 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
935 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
939 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
942 if (parsed && !buf[parsed])
945 /* try again without domain */
947 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
950 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
954 sscanf(buf, " %x:*.* %n", bus, &parsed);
957 if (parsed && !buf[parsed])
963 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
964 *slot, int *func, int *reg, int *size, int *mask)
968 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
969 reg, size, mask, &parsed);
970 if (parsed && !buf[parsed])
973 /* try again without domain */
975 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
977 if (parsed && !buf[parsed])
983 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
985 struct pcistub_device_id *pci_dev_id;
987 int rc = 0, devfn = PCI_DEVFN(slot, func);
990 for (slot = 0; !rc && slot < 32; ++slot)
991 rc = pcistub_device_id_add(domain, bus, slot, func);
996 for (func = 0; !rc && func < 8; ++func)
997 rc = pcistub_device_id_add(domain, bus, slot, func);
1002 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1003 || !defined(CONFIG_PCI_DOMAINS)
1004 !pci_domains_supported ? domain :
1006 domain < 0 || domain > 0xffff)
1007 || bus < 0 || bus > 0xff
1008 || PCI_SLOT(devfn) != slot
1009 || PCI_FUNC(devfn) != func)
1012 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1016 pci_dev_id->domain = domain;
1017 pci_dev_id->bus = bus;
1018 pci_dev_id->devfn = devfn;
1020 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1021 domain, bus, slot, func);
1023 spin_lock_irqsave(&device_ids_lock, flags);
1024 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1025 spin_unlock_irqrestore(&device_ids_lock, flags);
1030 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1032 struct pcistub_device_id *pci_dev_id, *t;
1034 unsigned long flags;
1036 spin_lock_irqsave(&device_ids_lock, flags);
1037 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1039 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1040 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1041 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1042 /* Don't break; here because it's possible the same
1043 * slot could be in the list more than once
1045 list_del(&pci_dev_id->slot_list);
1050 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1051 domain, bus, slot, func);
1054 spin_unlock_irqrestore(&device_ids_lock, flags);
1059 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1060 unsigned int reg, unsigned int size,
1064 struct pcistub_device *psdev;
1065 struct pci_dev *dev;
1066 struct config_field *field;
1068 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1071 psdev = pcistub_device_find(domain, bus, slot, func);
1078 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1084 field->offset = reg;
1088 field->reset = NULL;
1089 field->release = NULL;
1090 field->clean = xen_pcibk_config_field_free;
1092 err = xen_pcibk_config_quirks_add_field(dev, field);
1097 pcistub_device_put(psdev);
1101 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1104 int domain, bus, slot, func;
1107 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1111 err = pcistub_device_id_add(domain, bus, slot, func);
1118 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1120 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1123 int domain, bus, slot, func;
1126 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1130 err = pcistub_device_id_remove(domain, bus, slot, func);
1137 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1139 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1141 struct pcistub_device_id *pci_dev_id;
1143 unsigned long flags;
1145 spin_lock_irqsave(&device_ids_lock, flags);
1146 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1147 if (count >= PAGE_SIZE)
1150 count += scnprintf(buf + count, PAGE_SIZE - count,
1151 "%04x:%02x:%02x.%d\n",
1152 pci_dev_id->domain, pci_dev_id->bus,
1153 PCI_SLOT(pci_dev_id->devfn),
1154 PCI_FUNC(pci_dev_id->devfn));
1156 spin_unlock_irqrestore(&device_ids_lock, flags);
1160 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1162 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1164 struct pcistub_device *psdev;
1165 struct xen_pcibk_dev_data *dev_data;
1167 unsigned long flags;
1169 spin_lock_irqsave(&pcistub_devices_lock, flags);
1170 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1171 if (count >= PAGE_SIZE)
1175 dev_data = pci_get_drvdata(psdev->dev);
1179 scnprintf(buf + count, PAGE_SIZE - count,
1180 "%s:%s:%sing:%ld\n",
1181 pci_name(psdev->dev),
1182 dev_data->isr_on ? "on" : "off",
1183 dev_data->ack_intr ? "ack" : "not ack",
1186 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1189 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1191 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1195 struct pcistub_device *psdev;
1196 struct xen_pcibk_dev_data *dev_data;
1197 int domain, bus, slot, func;
1200 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1204 psdev = pcistub_device_find(domain, bus, slot, func);
1210 dev_data = pci_get_drvdata(psdev->dev);
1216 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1217 dev_data->irq_name, dev_data->isr_on,
1220 dev_data->isr_on = !(dev_data->isr_on);
1221 if (dev_data->isr_on)
1222 dev_data->ack_intr = 1;
1225 pcistub_device_put(psdev);
1230 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1231 pcistub_irq_handler_switch);
1233 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1236 int domain, bus, slot, func, reg, size, mask;
1239 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1244 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1252 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1255 unsigned long flags;
1256 struct xen_pcibk_config_quirk *quirk;
1257 struct xen_pcibk_dev_data *dev_data;
1258 const struct config_field *field;
1259 const struct config_field_entry *cfg_entry;
1261 spin_lock_irqsave(&device_ids_lock, flags);
1262 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1263 if (count >= PAGE_SIZE)
1266 count += scnprintf(buf + count, PAGE_SIZE - count,
1267 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1268 quirk->pdev->bus->number,
1269 PCI_SLOT(quirk->pdev->devfn),
1270 PCI_FUNC(quirk->pdev->devfn),
1271 quirk->devid.vendor, quirk->devid.device,
1272 quirk->devid.subvendor,
1273 quirk->devid.subdevice);
1275 dev_data = pci_get_drvdata(quirk->pdev);
1277 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1278 field = cfg_entry->field;
1279 if (count >= PAGE_SIZE)
1282 count += scnprintf(buf + count, PAGE_SIZE - count,
1283 "\t\t%08x:%01x:%08x\n",
1284 cfg_entry->base_offset +
1285 field->offset, field->size,
1291 spin_unlock_irqrestore(&device_ids_lock, flags);
1295 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1298 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1301 int domain, bus, slot, func;
1303 struct pcistub_device *psdev;
1304 struct xen_pcibk_dev_data *dev_data;
1306 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1310 psdev = pcistub_device_find(domain, bus, slot, func);
1316 dev_data = pci_get_drvdata(psdev->dev);
1317 /* the driver data for a device should never be null at this point */
1322 if (!dev_data->permissive) {
1323 dev_data->permissive = 1;
1324 /* Let user know that what they're doing could be unsafe */
1325 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1326 "configuration space accesses!\n");
1327 dev_warn(&psdev->dev->dev,
1328 "permissive mode is potentially unsafe!\n");
1331 pcistub_device_put(psdev);
1338 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1340 struct pcistub_device *psdev;
1341 struct xen_pcibk_dev_data *dev_data;
1343 unsigned long flags;
1344 spin_lock_irqsave(&pcistub_devices_lock, flags);
1345 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1346 if (count >= PAGE_SIZE)
1350 dev_data = pci_get_drvdata(psdev->dev);
1351 if (!dev_data || !dev_data->permissive)
1354 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1355 pci_name(psdev->dev));
1357 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1360 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1363 static void pcistub_exit(void)
1365 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1366 driver_remove_file(&xen_pcibk_pci_driver.driver,
1367 &driver_attr_remove_slot);
1368 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1369 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1370 driver_remove_file(&xen_pcibk_pci_driver.driver,
1371 &driver_attr_permissive);
1372 driver_remove_file(&xen_pcibk_pci_driver.driver,
1373 &driver_attr_irq_handlers);
1374 driver_remove_file(&xen_pcibk_pci_driver.driver,
1375 &driver_attr_irq_handler_state);
1376 pci_unregister_driver(&xen_pcibk_pci_driver);
1379 static int __init pcistub_init(void)
1383 int domain, bus, slot, func;
1386 if (pci_devs_to_hide && *pci_devs_to_hide) {
1390 err = sscanf(pci_devs_to_hide + pos,
1391 " (%x:%x:%x.%x) %n",
1392 &domain, &bus, &slot, &func, &parsed);
1396 sscanf(pci_devs_to_hide + pos,
1398 &domain, &bus, &slot, &parsed);
1402 sscanf(pci_devs_to_hide + pos,
1404 &domain, &bus, &parsed);
1410 err = sscanf(pci_devs_to_hide + pos,
1412 &bus, &slot, &func, &parsed);
1416 sscanf(pci_devs_to_hide + pos,
1418 &bus, &slot, &parsed);
1422 sscanf(pci_devs_to_hide + pos,
1432 err = pcistub_device_id_add(domain, bus, slot, func);
1437 } while (pci_devs_to_hide[pos]);
1440 /* If we're the first PCI Device Driver to register, we're the
1441 * first one to get offered PCI devices as they become
1442 * available (and thus we can be the first to grab them)
1444 err = pci_register_driver(&xen_pcibk_pci_driver);
1448 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1449 &driver_attr_new_slot);
1451 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1452 &driver_attr_remove_slot);
1454 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1455 &driver_attr_slots);
1457 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1458 &driver_attr_quirks);
1460 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1461 &driver_attr_permissive);
1464 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1465 &driver_attr_irq_handlers);
1467 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1468 &driver_attr_irq_handler_state);
1476 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1477 pci_devs_to_hide + pos);
1483 * fs_initcall happens before device_initcall
1484 * so xen_pcibk *should* get called first (b/c we
1485 * want to suck up any device before other drivers
1486 * get a chance by being the first pci device
1487 * driver to register)
1489 fs_initcall(pcistub_init);
1492 static int __init xen_pcibk_init(void)
1496 if (!xen_initial_domain())
1499 err = xen_pcibk_config_init();
1504 err = pcistub_init();
1509 pcistub_init_devices_late();
1510 err = xen_pcibk_xenbus_register();
1517 static void __exit xen_pcibk_cleanup(void)
1519 xen_pcibk_xenbus_unregister();
1523 module_init(xen_pcibk_init);
1524 module_exit(xen_pcibk_cleanup);
1526 MODULE_LICENSE("Dual BSD/GPL");
1527 MODULE_ALIAS("xen-backend:pci");