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>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/rwsem.h>
10 #include <linux/list.h>
11 #include <linux/spinlock.h>
12 #include <linux/kref.h>
13 #include <linux/pci.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/atomic.h>
17 #include <xen/events.h>
18 #include <asm/xen/pci.h>
19 #include <asm/xen/hypervisor.h>
21 #include "conf_space.h"
22 #include "conf_space_quirks.h"
24 static char *pci_devs_to_hide;
25 wait_queue_head_t xen_pcibk_aer_wait_queue;
26 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
27 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
29 static DECLARE_RWSEM(pcistub_sem);
30 module_param_named(hide, pci_devs_to_hide, charp, 0444);
32 struct pcistub_device_id {
33 struct list_head slot_list;
38 static LIST_HEAD(pcistub_device_ids);
39 static DEFINE_SPINLOCK(device_ids_lock);
41 struct pcistub_device {
43 struct list_head dev_list;
47 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
50 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
51 * flag must be locked with pcistub_devices_lock
53 static DEFINE_SPINLOCK(pcistub_devices_lock);
54 static LIST_HEAD(pcistub_devices);
56 /* wait for device_initcall before initializing our devices
57 * (see pcistub_init_devices_late)
59 static int initialize_devices;
60 static LIST_HEAD(seized_devices);
62 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
64 struct pcistub_device *psdev;
66 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
68 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
72 psdev->dev = pci_dev_get(dev);
78 kref_init(&psdev->kref);
79 spin_lock_init(&psdev->lock);
84 /* Don't call this directly as it's called by pcistub_device_put */
85 static void pcistub_device_release(struct kref *kref)
87 struct pcistub_device *psdev;
88 struct xen_pcibk_dev_data *dev_data;
90 psdev = container_of(kref, struct pcistub_device, kref);
91 dev_data = pci_get_drvdata(psdev->dev);
93 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
95 xen_unregister_device_domain_owner(psdev->dev);
97 /* Call the reset function which does not take lock as this
98 * is called from "unbind" which takes a device_lock mutex.
100 __pci_reset_function_locked(psdev->dev);
101 if (pci_load_and_free_saved_state(psdev->dev,
102 &dev_data->pci_saved_state)) {
103 dev_dbg(&psdev->dev->dev, "Could not reload PCI state\n");
105 pci_restore_state(psdev->dev);
107 /* Disable the device */
108 xen_pcibk_reset_device(psdev->dev);
111 pci_set_drvdata(psdev->dev, NULL);
113 /* Clean-up the device */
114 xen_pcibk_config_free_dyn_fields(psdev->dev);
115 xen_pcibk_config_free_dev(psdev->dev);
117 psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
118 pci_dev_put(psdev->dev);
123 static inline void pcistub_device_get(struct pcistub_device *psdev)
125 kref_get(&psdev->kref);
128 static inline void pcistub_device_put(struct pcistub_device *psdev)
130 kref_put(&psdev->kref, pcistub_device_release);
133 static struct pcistub_device *pcistub_device_find(int domain, int bus,
136 struct pcistub_device *psdev = NULL;
139 spin_lock_irqsave(&pcistub_devices_lock, flags);
141 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
142 if (psdev->dev != NULL
143 && domain == pci_domain_nr(psdev->dev->bus)
144 && bus == psdev->dev->bus->number
145 && slot == PCI_SLOT(psdev->dev->devfn)
146 && func == PCI_FUNC(psdev->dev->devfn)) {
147 pcistub_device_get(psdev);
156 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
160 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
161 struct pcistub_device *psdev)
163 struct pci_dev *pci_dev = NULL;
166 pcistub_device_get(psdev);
168 spin_lock_irqsave(&psdev->lock, flags);
171 pci_dev = psdev->dev;
173 spin_unlock_irqrestore(&psdev->lock, flags);
176 pcistub_device_put(psdev);
181 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
185 struct pcistub_device *psdev;
186 struct pci_dev *found_dev = NULL;
189 spin_lock_irqsave(&pcistub_devices_lock, flags);
191 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
192 if (psdev->dev != NULL
193 && domain == pci_domain_nr(psdev->dev->bus)
194 && bus == psdev->dev->bus->number
195 && slot == PCI_SLOT(psdev->dev->devfn)
196 && func == PCI_FUNC(psdev->dev->devfn)) {
197 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
202 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
206 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
209 struct pcistub_device *psdev;
210 struct pci_dev *found_dev = NULL;
213 spin_lock_irqsave(&pcistub_devices_lock, flags);
215 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
216 if (psdev->dev == dev) {
217 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
226 void pcistub_put_pci_dev(struct pci_dev *dev)
228 struct pcistub_device *psdev, *found_psdev = NULL;
231 spin_lock_irqsave(&pcistub_devices_lock, flags);
233 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
234 if (psdev->dev == dev) {
240 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
241 if (WARN_ON(!found_psdev))
244 /*hold this lock for avoiding breaking link between
245 * pcistub and xen_pcibk when AER is in processing
247 down_write(&pcistub_sem);
248 /* Cleanup our device
249 * (so it's ready for the next domain)
252 /* This is OK - we are running from workqueue context
253 * and want to inhibit the user from fiddling with 'reset'
255 pci_reset_function(dev);
256 pci_restore_state(psdev->dev);
258 /* This disables the device. */
259 xen_pcibk_reset_device(found_psdev->dev);
261 /* And cleanup up our emulated fields. */
262 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
263 xen_pcibk_config_reset_dev(found_psdev->dev);
265 xen_unregister_device_domain_owner(found_psdev->dev);
267 spin_lock_irqsave(&found_psdev->lock, flags);
268 found_psdev->pdev = NULL;
269 spin_unlock_irqrestore(&found_psdev->lock, flags);
271 pcistub_device_put(found_psdev);
272 up_write(&pcistub_sem);
275 static int __devinit pcistub_match_one(struct pci_dev *dev,
276 struct pcistub_device_id *pdev_id)
278 /* Match the specified device by domain, bus, slot, func and also if
279 * any of the device's parent bridges match.
281 for (; dev != NULL; dev = dev->bus->self) {
282 if (pci_domain_nr(dev->bus) == pdev_id->domain
283 && dev->bus->number == pdev_id->bus
284 && dev->devfn == pdev_id->devfn)
287 /* Sometimes topmost bridge links to itself. */
288 if (dev == dev->bus->self)
295 static int __devinit pcistub_match(struct pci_dev *dev)
297 struct pcistub_device_id *pdev_id;
301 spin_lock_irqsave(&device_ids_lock, flags);
302 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
303 if (pcistub_match_one(dev, pdev_id)) {
308 spin_unlock_irqrestore(&device_ids_lock, flags);
313 static int __devinit pcistub_init_device(struct pci_dev *dev)
315 struct xen_pcibk_dev_data *dev_data;
318 dev_dbg(&dev->dev, "initializing...\n");
320 /* The PCI backend is not intended to be a module (or to work with
321 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
322 * would need to be called somewhere to free the memory allocated
323 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
325 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
326 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
331 pci_set_drvdata(dev, dev_data);
334 * Setup name for fake IRQ handler. It will only be enabled
335 * once the device is turned on by the guest.
337 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
339 dev_dbg(&dev->dev, "initializing config\n");
341 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
342 err = xen_pcibk_config_init_dev(dev);
346 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
347 * must do this here because pcibios_enable_device may specify
348 * the pci device's true irq (and possibly its other resources)
349 * if they differ from what's in the configuration space.
350 * This makes the assumption that the device's resources won't
351 * change after this point (otherwise this code may break!)
353 dev_dbg(&dev->dev, "enabling device\n");
354 err = pci_enable_device(dev);
358 /* We need the device active to save the state. */
359 dev_dbg(&dev->dev, "save state of device\n");
361 dev_data->pci_saved_state = pci_store_saved_state(dev);
362 if (!dev_data->pci_saved_state)
363 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
365 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
366 __pci_reset_function_locked(dev);
367 pci_restore_state(dev);
369 /* Now disable the device (this also ensures some private device
370 * data is setup before we export)
372 dev_dbg(&dev->dev, "reset device\n");
373 xen_pcibk_reset_device(dev);
375 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
379 xen_pcibk_config_free_dev(dev);
382 pci_set_drvdata(dev, NULL);
388 * Because some initialization still happens on
389 * devices during fs_initcall, we need to defer
390 * full initialization of our devices until
393 static int __init pcistub_init_devices_late(void)
395 struct pcistub_device *psdev;
399 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
401 spin_lock_irqsave(&pcistub_devices_lock, flags);
403 while (!list_empty(&seized_devices)) {
404 psdev = container_of(seized_devices.next,
405 struct pcistub_device, dev_list);
406 list_del(&psdev->dev_list);
408 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
410 err = pcistub_init_device(psdev->dev);
412 dev_err(&psdev->dev->dev,
413 "error %d initializing device\n", err);
418 spin_lock_irqsave(&pcistub_devices_lock, flags);
421 list_add_tail(&psdev->dev_list, &pcistub_devices);
424 initialize_devices = 1;
426 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
431 static int __devinit pcistub_seize(struct pci_dev *dev)
433 struct pcistub_device *psdev;
437 psdev = pcistub_device_alloc(dev);
441 spin_lock_irqsave(&pcistub_devices_lock, flags);
443 if (initialize_devices) {
444 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
446 /* don't want irqs disabled when calling pcistub_init_device */
447 err = pcistub_init_device(psdev->dev);
449 spin_lock_irqsave(&pcistub_devices_lock, flags);
452 list_add(&psdev->dev_list, &pcistub_devices);
454 dev_dbg(&dev->dev, "deferring initialization\n");
455 list_add(&psdev->dev_list, &seized_devices);
458 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
461 pcistub_device_put(psdev);
466 static int __devinit pcistub_probe(struct pci_dev *dev,
467 const struct pci_device_id *id)
471 dev_dbg(&dev->dev, "probing...\n");
473 if (pcistub_match(dev)) {
475 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
476 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
477 dev_err(&dev->dev, "can't export pci devices that "
478 "don't have a normal (0) or bridge (1) "
484 dev_info(&dev->dev, "seizing device\n");
485 err = pcistub_seize(dev);
487 /* Didn't find the device */
494 static void pcistub_remove(struct pci_dev *dev)
496 struct pcistub_device *psdev, *found_psdev = NULL;
499 dev_dbg(&dev->dev, "removing\n");
501 spin_lock_irqsave(&pcistub_devices_lock, flags);
503 xen_pcibk_config_quirk_release(dev);
505 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
506 if (psdev->dev == dev) {
512 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
515 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
518 if (found_psdev->pdev) {
519 printk(KERN_WARNING DRV_NAME ": ****** removing device "
520 "%s while still in-use! ******\n",
521 pci_name(found_psdev->dev));
522 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
523 " still access this device's i/o resources!\n");
524 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
525 "domain before binding device\n");
526 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
529 xen_pcibk_release_pci_dev(found_psdev->pdev,
533 spin_lock_irqsave(&pcistub_devices_lock, flags);
534 list_del(&found_psdev->dev_list);
535 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
537 /* the final put for releasing from the list */
538 pcistub_device_put(found_psdev);
542 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
544 .vendor = PCI_ANY_ID,
545 .device = PCI_ANY_ID,
546 .subvendor = PCI_ANY_ID,
547 .subdevice = PCI_ANY_ID,
552 #define PCI_NODENAME_MAX 40
553 static void kill_domain_by_device(struct pcistub_device *psdev)
555 struct xenbus_transaction xbt;
557 char nodename[PCI_NODENAME_MAX];
560 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
561 psdev->pdev->xdev->otherend_id);
564 err = xenbus_transaction_start(&xbt);
566 dev_err(&psdev->dev->dev,
567 "error %d when start xenbus transaction\n", err);
570 /*PV AER handlers will set this flag*/
571 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
572 err = xenbus_transaction_end(xbt, 0);
576 dev_err(&psdev->dev->dev,
577 "error %d when end xenbus transaction\n", err);
582 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
583 * backend need to have cooperation. In xen_pcibk, those steps will do similar
584 * jobs: send service request and waiting for front_end response.
586 static pci_ers_result_t common_process(struct pcistub_device *psdev,
587 pci_channel_state_t state, int aer_cmd,
588 pci_ers_result_t result)
590 pci_ers_result_t res = result;
591 struct xen_pcie_aer_op *aer_op;
594 /*with PV AER drivers*/
595 aer_op = &(psdev->pdev->sh_info->aer_op);
596 aer_op->cmd = aer_cmd ;
597 /*useful for error_detected callback*/
600 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
601 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
603 dev_err(&psdev->dev->dev,
604 DRV_NAME ": failed to get pcifront device\n");
605 return PCI_ERS_RESULT_NONE;
609 dev_dbg(&psdev->dev->dev,
610 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
611 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
612 /*local flag to mark there's aer request, xen_pcibk callback will use
613 * this flag to judge whether we need to check pci-front give aer
616 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
618 /*It is possible that a pcifront conf_read_write ops request invokes
619 * the callback which cause the spurious execution of wake_up.
620 * Yet it is harmless and better than a spinlock here
622 set_bit(_XEN_PCIB_active,
623 (unsigned long *)&psdev->pdev->sh_info->flags);
625 notify_remote_via_irq(psdev->pdev->evtchn_irq);
627 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
628 !(test_bit(_XEN_PCIB_active, (unsigned long *)
629 &psdev->pdev->sh_info->flags)), 300*HZ);
632 if (test_bit(_XEN_PCIB_active,
633 (unsigned long *)&psdev->pdev->sh_info->flags)) {
634 dev_err(&psdev->dev->dev,
635 "pcifront aer process not responding!\n");
636 clear_bit(_XEN_PCIB_active,
637 (unsigned long *)&psdev->pdev->sh_info->flags);
638 aer_op->err = PCI_ERS_RESULT_NONE;
642 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
644 if (test_bit(_XEN_PCIF_active,
645 (unsigned long *)&psdev->pdev->sh_info->flags)) {
646 dev_dbg(&psdev->dev->dev,
647 "schedule pci_conf service in " DRV_NAME "\n");
648 xen_pcibk_test_and_schedule_op(psdev->pdev);
651 res = (pci_ers_result_t)aer_op->err;
656 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
657 * of the device driver could provide this service, and then wait for pcifront
659 * @dev: pointer to PCI devices
660 * return value is used by aer_core do_recovery policy
662 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
664 struct pcistub_device *psdev;
665 pci_ers_result_t result;
667 result = PCI_ERS_RESULT_RECOVERED;
668 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
669 dev->bus->number, dev->devfn);
671 down_write(&pcistub_sem);
672 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
674 PCI_SLOT(dev->devfn),
675 PCI_FUNC(dev->devfn));
677 if (!psdev || !psdev->pdev) {
679 DRV_NAME " device is not found/assigned\n");
683 if (!psdev->pdev->sh_info) {
684 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
685 " by HVM, kill it\n");
686 kill_domain_by_device(psdev);
690 if (!test_bit(_XEN_PCIB_AERHANDLER,
691 (unsigned long *)&psdev->pdev->sh_info->flags)) {
693 "guest with no AER driver should have been killed\n");
696 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
698 if (result == PCI_ERS_RESULT_NONE ||
699 result == PCI_ERS_RESULT_DISCONNECT) {
701 "No AER slot_reset service or disconnected!\n");
702 kill_domain_by_device(psdev);
706 pcistub_device_put(psdev);
707 up_write(&pcistub_sem);
713 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
714 * in case of the device driver could provide this service, and then wait
716 * @dev: pointer to PCI devices
717 * return value is used by aer_core do_recovery policy
720 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
722 struct pcistub_device *psdev;
723 pci_ers_result_t result;
725 result = PCI_ERS_RESULT_RECOVERED;
726 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
727 dev->bus->number, dev->devfn);
729 down_write(&pcistub_sem);
730 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
732 PCI_SLOT(dev->devfn),
733 PCI_FUNC(dev->devfn));
735 if (!psdev || !psdev->pdev) {
737 DRV_NAME " device is not found/assigned\n");
741 if (!psdev->pdev->sh_info) {
742 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
743 " by HVM, kill it\n");
744 kill_domain_by_device(psdev);
748 if (!test_bit(_XEN_PCIB_AERHANDLER,
749 (unsigned long *)&psdev->pdev->sh_info->flags)) {
751 "guest with no AER driver should have been killed\n");
754 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
756 if (result == PCI_ERS_RESULT_NONE ||
757 result == PCI_ERS_RESULT_DISCONNECT) {
759 "No AER mmio_enabled service or disconnected!\n");
760 kill_domain_by_device(psdev);
764 pcistub_device_put(psdev);
765 up_write(&pcistub_sem);
769 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
770 * in case of the device driver could provide this service, and then wait
772 * @dev: pointer to PCI devices
773 * @error: the current PCI connection state
774 * return value is used by aer_core do_recovery policy
777 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
778 pci_channel_state_t error)
780 struct pcistub_device *psdev;
781 pci_ers_result_t result;
783 result = PCI_ERS_RESULT_CAN_RECOVER;
784 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
785 dev->bus->number, dev->devfn);
787 down_write(&pcistub_sem);
788 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
790 PCI_SLOT(dev->devfn),
791 PCI_FUNC(dev->devfn));
793 if (!psdev || !psdev->pdev) {
795 DRV_NAME " device is not found/assigned\n");
799 if (!psdev->pdev->sh_info) {
800 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
801 " by HVM, kill it\n");
802 kill_domain_by_device(psdev);
806 /*Guest owns the device yet no aer handler regiested, kill guest*/
807 if (!test_bit(_XEN_PCIB_AERHANDLER,
808 (unsigned long *)&psdev->pdev->sh_info->flags)) {
809 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
810 kill_domain_by_device(psdev);
813 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
815 if (result == PCI_ERS_RESULT_NONE ||
816 result == PCI_ERS_RESULT_DISCONNECT) {
818 "No AER error_detected service or disconnected!\n");
819 kill_domain_by_device(psdev);
823 pcistub_device_put(psdev);
824 up_write(&pcistub_sem);
828 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
829 * in case of the device driver could provide this service, and then wait
831 * @dev: pointer to PCI devices
834 static void xen_pcibk_error_resume(struct pci_dev *dev)
836 struct pcistub_device *psdev;
838 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
839 dev->bus->number, dev->devfn);
841 down_write(&pcistub_sem);
842 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
844 PCI_SLOT(dev->devfn),
845 PCI_FUNC(dev->devfn));
847 if (!psdev || !psdev->pdev) {
849 DRV_NAME " device is not found/assigned\n");
853 if (!psdev->pdev->sh_info) {
854 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
855 " by HVM, kill it\n");
856 kill_domain_by_device(psdev);
860 if (!test_bit(_XEN_PCIB_AERHANDLER,
861 (unsigned long *)&psdev->pdev->sh_info->flags)) {
863 "guest with no AER driver should have been killed\n");
864 kill_domain_by_device(psdev);
867 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
868 PCI_ERS_RESULT_RECOVERED);
871 pcistub_device_put(psdev);
872 up_write(&pcistub_sem);
876 /*add xen_pcibk AER handling*/
877 static const struct pci_error_handlers xen_pcibk_error_handler = {
878 .error_detected = xen_pcibk_error_detected,
879 .mmio_enabled = xen_pcibk_mmio_enabled,
880 .slot_reset = xen_pcibk_slot_reset,
881 .resume = xen_pcibk_error_resume,
885 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
886 * for a normal device. I don't want it to be loaded automatically.
889 static struct pci_driver xen_pcibk_pci_driver = {
890 /* The name should be xen_pciback, but until the tools are updated
891 * we will keep it as pciback. */
893 .id_table = pcistub_ids,
894 .probe = pcistub_probe,
895 .remove = pcistub_remove,
896 .err_handler = &xen_pcibk_error_handler,
899 static inline int str_to_slot(const char *buf, int *domain, int *bus,
900 int *slot, int *func)
904 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
908 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
912 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
915 if (parsed && !buf[parsed])
918 /* try again without domain */
920 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
923 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
927 sscanf(buf, " %x:*.* %n", bus, &parsed);
930 if (parsed && !buf[parsed])
936 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
937 *slot, int *func, int *reg, int *size, int *mask)
941 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
942 reg, size, mask, &parsed);
943 if (parsed && !buf[parsed])
946 /* try again without domain */
948 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
950 if (parsed && !buf[parsed])
956 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
958 struct pcistub_device_id *pci_dev_id;
960 int rc = 0, devfn = PCI_DEVFN(slot, func);
963 for (slot = 0; !rc && slot < 32; ++slot)
964 rc = pcistub_device_id_add(domain, bus, slot, func);
969 for (func = 0; !rc && func < 8; ++func)
970 rc = pcistub_device_id_add(domain, bus, slot, func);
975 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
976 || !defined(CONFIG_PCI_DOMAINS)
977 !pci_domains_supported ? domain :
979 domain < 0 || domain > 0xffff)
980 || bus < 0 || bus > 0xff
981 || PCI_SLOT(devfn) != slot
982 || PCI_FUNC(devfn) != func)
985 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
989 pci_dev_id->domain = domain;
990 pci_dev_id->bus = bus;
991 pci_dev_id->devfn = devfn;
993 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
994 domain, bus, slot, func);
996 spin_lock_irqsave(&device_ids_lock, flags);
997 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
998 spin_unlock_irqrestore(&device_ids_lock, flags);
1003 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1005 struct pcistub_device_id *pci_dev_id, *t;
1007 unsigned long flags;
1009 spin_lock_irqsave(&device_ids_lock, flags);
1010 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1012 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1013 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1014 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1015 /* Don't break; here because it's possible the same
1016 * slot could be in the list more than once
1018 list_del(&pci_dev_id->slot_list);
1023 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
1024 "seize list\n", domain, bus, slot, func);
1027 spin_unlock_irqrestore(&device_ids_lock, flags);
1032 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1033 unsigned int reg, unsigned int size,
1037 struct pcistub_device *psdev;
1038 struct pci_dev *dev;
1039 struct config_field *field;
1041 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1044 psdev = pcistub_device_find(domain, bus, slot, func);
1051 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1057 field->offset = reg;
1061 field->reset = NULL;
1062 field->release = NULL;
1063 field->clean = xen_pcibk_config_field_free;
1065 err = xen_pcibk_config_quirks_add_field(dev, field);
1070 pcistub_device_put(psdev);
1074 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1077 int domain, bus, slot, func;
1080 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1084 err = pcistub_device_id_add(domain, bus, slot, func);
1091 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1093 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1096 int domain, bus, slot, func;
1099 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1103 err = pcistub_device_id_remove(domain, bus, slot, func);
1110 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1112 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1114 struct pcistub_device_id *pci_dev_id;
1116 unsigned long flags;
1118 spin_lock_irqsave(&device_ids_lock, flags);
1119 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1120 if (count >= PAGE_SIZE)
1123 count += scnprintf(buf + count, PAGE_SIZE - count,
1124 "%04x:%02x:%02x.%d\n",
1125 pci_dev_id->domain, pci_dev_id->bus,
1126 PCI_SLOT(pci_dev_id->devfn),
1127 PCI_FUNC(pci_dev_id->devfn));
1129 spin_unlock_irqrestore(&device_ids_lock, flags);
1133 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1135 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1137 struct pcistub_device *psdev;
1138 struct xen_pcibk_dev_data *dev_data;
1140 unsigned long flags;
1142 spin_lock_irqsave(&pcistub_devices_lock, flags);
1143 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1144 if (count >= PAGE_SIZE)
1148 dev_data = pci_get_drvdata(psdev->dev);
1152 scnprintf(buf + count, PAGE_SIZE - count,
1153 "%s:%s:%sing:%ld\n",
1154 pci_name(psdev->dev),
1155 dev_data->isr_on ? "on" : "off",
1156 dev_data->ack_intr ? "ack" : "not ack",
1159 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1162 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1164 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1168 struct pcistub_device *psdev;
1169 struct xen_pcibk_dev_data *dev_data;
1170 int domain, bus, slot, func;
1173 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1177 psdev = pcistub_device_find(domain, bus, slot, func);
1181 dev_data = pci_get_drvdata(psdev->dev);
1185 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1186 dev_data->irq_name, dev_data->isr_on,
1189 dev_data->isr_on = !(dev_data->isr_on);
1190 if (dev_data->isr_on)
1191 dev_data->ack_intr = 1;
1194 pcistub_device_put(psdev);
1199 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1200 pcistub_irq_handler_switch);
1202 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1205 int domain, bus, slot, func, reg, size, mask;
1208 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1213 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1221 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1224 unsigned long flags;
1225 struct xen_pcibk_config_quirk *quirk;
1226 struct xen_pcibk_dev_data *dev_data;
1227 const struct config_field *field;
1228 const struct config_field_entry *cfg_entry;
1230 spin_lock_irqsave(&device_ids_lock, flags);
1231 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1232 if (count >= PAGE_SIZE)
1235 count += scnprintf(buf + count, PAGE_SIZE - count,
1236 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1237 quirk->pdev->bus->number,
1238 PCI_SLOT(quirk->pdev->devfn),
1239 PCI_FUNC(quirk->pdev->devfn),
1240 quirk->devid.vendor, quirk->devid.device,
1241 quirk->devid.subvendor,
1242 quirk->devid.subdevice);
1244 dev_data = pci_get_drvdata(quirk->pdev);
1246 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1247 field = cfg_entry->field;
1248 if (count >= PAGE_SIZE)
1251 count += scnprintf(buf + count, PAGE_SIZE - count,
1252 "\t\t%08x:%01x:%08x\n",
1253 cfg_entry->base_offset +
1254 field->offset, field->size,
1260 spin_unlock_irqrestore(&device_ids_lock, flags);
1264 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1267 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1270 int domain, bus, slot, func;
1272 struct pcistub_device *psdev;
1273 struct xen_pcibk_dev_data *dev_data;
1275 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1279 psdev = pcistub_device_find(domain, bus, slot, func);
1285 dev_data = pci_get_drvdata(psdev->dev);
1286 /* the driver data for a device should never be null at this point */
1291 if (!dev_data->permissive) {
1292 dev_data->permissive = 1;
1293 /* Let user know that what they're doing could be unsafe */
1294 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1295 "configuration space accesses!\n");
1296 dev_warn(&psdev->dev->dev,
1297 "permissive mode is potentially unsafe!\n");
1300 pcistub_device_put(psdev);
1307 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1309 struct pcistub_device *psdev;
1310 struct xen_pcibk_dev_data *dev_data;
1312 unsigned long flags;
1313 spin_lock_irqsave(&pcistub_devices_lock, flags);
1314 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1315 if (count >= PAGE_SIZE)
1319 dev_data = pci_get_drvdata(psdev->dev);
1320 if (!dev_data || !dev_data->permissive)
1323 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1324 pci_name(psdev->dev));
1326 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1329 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1332 static void pcistub_exit(void)
1334 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1335 driver_remove_file(&xen_pcibk_pci_driver.driver,
1336 &driver_attr_remove_slot);
1337 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1338 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1339 driver_remove_file(&xen_pcibk_pci_driver.driver,
1340 &driver_attr_permissive);
1341 driver_remove_file(&xen_pcibk_pci_driver.driver,
1342 &driver_attr_irq_handlers);
1343 driver_remove_file(&xen_pcibk_pci_driver.driver,
1344 &driver_attr_irq_handler_state);
1345 pci_unregister_driver(&xen_pcibk_pci_driver);
1348 static int __init pcistub_init(void)
1352 int domain, bus, slot, func;
1355 if (pci_devs_to_hide && *pci_devs_to_hide) {
1359 err = sscanf(pci_devs_to_hide + pos,
1360 " (%x:%x:%x.%x) %n",
1361 &domain, &bus, &slot, &func, &parsed);
1365 sscanf(pci_devs_to_hide + pos,
1367 &domain, &bus, &slot, &parsed);
1371 sscanf(pci_devs_to_hide + pos,
1373 &domain, &bus, &parsed);
1379 err = sscanf(pci_devs_to_hide + pos,
1381 &bus, &slot, &func, &parsed);
1385 sscanf(pci_devs_to_hide + pos,
1387 &bus, &slot, &parsed);
1391 sscanf(pci_devs_to_hide + pos,
1401 err = pcistub_device_id_add(domain, bus, slot, func);
1406 } while (pci_devs_to_hide[pos]);
1409 /* If we're the first PCI Device Driver to register, we're the
1410 * first one to get offered PCI devices as they become
1411 * available (and thus we can be the first to grab them)
1413 err = pci_register_driver(&xen_pcibk_pci_driver);
1417 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1418 &driver_attr_new_slot);
1420 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1421 &driver_attr_remove_slot);
1423 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1424 &driver_attr_slots);
1426 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1427 &driver_attr_quirks);
1429 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1430 &driver_attr_permissive);
1433 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1434 &driver_attr_irq_handlers);
1436 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1437 &driver_attr_irq_handler_state);
1445 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
1446 pci_devs_to_hide + pos);
1452 * fs_initcall happens before device_initcall
1453 * so xen_pcibk *should* get called first (b/c we
1454 * want to suck up any device before other drivers
1455 * get a chance by being the first pci device
1456 * driver to register)
1458 fs_initcall(pcistub_init);
1461 static int __init xen_pcibk_init(void)
1465 if (!xen_initial_domain())
1468 err = xen_pcibk_config_init();
1473 err = pcistub_init();
1478 pcistub_init_devices_late();
1479 err = xen_pcibk_xenbus_register();
1486 static void __exit xen_pcibk_cleanup(void)
1488 xen_pcibk_xenbus_unregister();
1492 module_init(xen_pcibk_init);
1493 module_exit(xen_pcibk_cleanup);
1495 MODULE_LICENSE("Dual BSD/GPL");
1496 MODULE_ALIAS("xen-backend:pci");