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 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
146 pcistub_device_get(psdev);
155 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
159 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
160 struct pcistub_device *psdev)
162 struct pci_dev *pci_dev = NULL;
165 pcistub_device_get(psdev);
167 spin_lock_irqsave(&psdev->lock, flags);
170 pci_dev = psdev->dev;
172 spin_unlock_irqrestore(&psdev->lock, flags);
175 pcistub_device_put(psdev);
180 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
184 struct pcistub_device *psdev;
185 struct pci_dev *found_dev = NULL;
188 spin_lock_irqsave(&pcistub_devices_lock, flags);
190 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
191 if (psdev->dev != NULL
192 && domain == pci_domain_nr(psdev->dev->bus)
193 && bus == psdev->dev->bus->number
194 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
195 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
200 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
204 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
207 struct pcistub_device *psdev;
208 struct pci_dev *found_dev = NULL;
211 spin_lock_irqsave(&pcistub_devices_lock, flags);
213 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
214 if (psdev->dev == dev) {
215 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
220 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
224 void pcistub_put_pci_dev(struct pci_dev *dev)
226 struct pcistub_device *psdev, *found_psdev = NULL;
229 spin_lock_irqsave(&pcistub_devices_lock, flags);
231 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
232 if (psdev->dev == dev) {
238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
239 if (WARN_ON(!found_psdev))
242 /*hold this lock for avoiding breaking link between
243 * pcistub and xen_pcibk when AER is in processing
245 down_write(&pcistub_sem);
246 /* Cleanup our device
247 * (so it's ready for the next domain)
250 /* This is OK - we are running from workqueue context
251 * and want to inhibit the user from fiddling with 'reset'
253 pci_reset_function(dev);
254 pci_restore_state(psdev->dev);
256 /* This disables the device. */
257 xen_pcibk_reset_device(found_psdev->dev);
259 /* And cleanup up our emulated fields. */
260 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
261 xen_pcibk_config_reset_dev(found_psdev->dev);
263 xen_unregister_device_domain_owner(found_psdev->dev);
265 spin_lock_irqsave(&found_psdev->lock, flags);
266 found_psdev->pdev = NULL;
267 spin_unlock_irqrestore(&found_psdev->lock, flags);
269 pcistub_device_put(found_psdev);
270 up_write(&pcistub_sem);
273 static int __devinit pcistub_match_one(struct pci_dev *dev,
274 struct pcistub_device_id *pdev_id)
276 /* Match the specified device by domain, bus, slot, func and also if
277 * any of the device's parent bridges match.
279 for (; dev != NULL; dev = dev->bus->self) {
280 if (pci_domain_nr(dev->bus) == pdev_id->domain
281 && dev->bus->number == pdev_id->bus
282 && dev->devfn == pdev_id->devfn)
285 /* Sometimes topmost bridge links to itself. */
286 if (dev == dev->bus->self)
293 static int __devinit pcistub_match(struct pci_dev *dev)
295 struct pcistub_device_id *pdev_id;
299 spin_lock_irqsave(&device_ids_lock, flags);
300 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
301 if (pcistub_match_one(dev, pdev_id)) {
306 spin_unlock_irqrestore(&device_ids_lock, flags);
311 static int __devinit pcistub_init_device(struct pci_dev *dev)
313 struct xen_pcibk_dev_data *dev_data;
316 dev_dbg(&dev->dev, "initializing...\n");
318 /* The PCI backend is not intended to be a module (or to work with
319 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
320 * would need to be called somewhere to free the memory allocated
321 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
323 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
324 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
329 pci_set_drvdata(dev, dev_data);
332 * Setup name for fake IRQ handler. It will only be enabled
333 * once the device is turned on by the guest.
335 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
337 dev_dbg(&dev->dev, "initializing config\n");
339 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
340 err = xen_pcibk_config_init_dev(dev);
344 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
345 * must do this here because pcibios_enable_device may specify
346 * the pci device's true irq (and possibly its other resources)
347 * if they differ from what's in the configuration space.
348 * This makes the assumption that the device's resources won't
349 * change after this point (otherwise this code may break!)
351 dev_dbg(&dev->dev, "enabling device\n");
352 err = pci_enable_device(dev);
356 /* We need the device active to save the state. */
357 dev_dbg(&dev->dev, "save state of device\n");
359 dev_data->pci_saved_state = pci_store_saved_state(dev);
360 if (!dev_data->pci_saved_state)
361 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
363 dev_dbg(&dev->dev, "reseting (FLR, D3, etc) the device\n");
364 __pci_reset_function_locked(dev);
365 pci_restore_state(dev);
367 /* Now disable the device (this also ensures some private device
368 * data is setup before we export)
370 dev_dbg(&dev->dev, "reset device\n");
371 xen_pcibk_reset_device(dev);
373 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
377 xen_pcibk_config_free_dev(dev);
380 pci_set_drvdata(dev, NULL);
386 * Because some initialization still happens on
387 * devices during fs_initcall, we need to defer
388 * full initialization of our devices until
391 static int __init pcistub_init_devices_late(void)
393 struct pcistub_device *psdev;
397 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
399 spin_lock_irqsave(&pcistub_devices_lock, flags);
401 while (!list_empty(&seized_devices)) {
402 psdev = container_of(seized_devices.next,
403 struct pcistub_device, dev_list);
404 list_del(&psdev->dev_list);
406 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
408 err = pcistub_init_device(psdev->dev);
410 dev_err(&psdev->dev->dev,
411 "error %d initializing device\n", err);
416 spin_lock_irqsave(&pcistub_devices_lock, flags);
419 list_add_tail(&psdev->dev_list, &pcistub_devices);
422 initialize_devices = 1;
424 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
429 static int __devinit pcistub_seize(struct pci_dev *dev)
431 struct pcistub_device *psdev;
435 psdev = pcistub_device_alloc(dev);
439 spin_lock_irqsave(&pcistub_devices_lock, flags);
441 if (initialize_devices) {
442 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
444 /* don't want irqs disabled when calling pcistub_init_device */
445 err = pcistub_init_device(psdev->dev);
447 spin_lock_irqsave(&pcistub_devices_lock, flags);
450 list_add(&psdev->dev_list, &pcistub_devices);
452 dev_dbg(&dev->dev, "deferring initialization\n");
453 list_add(&psdev->dev_list, &seized_devices);
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
459 pcistub_device_put(psdev);
464 static int __devinit pcistub_probe(struct pci_dev *dev,
465 const struct pci_device_id *id)
469 dev_dbg(&dev->dev, "probing...\n");
471 if (pcistub_match(dev)) {
473 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
474 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
475 dev_err(&dev->dev, "can't export pci devices that "
476 "don't have a normal (0) or bridge (1) "
482 dev_info(&dev->dev, "seizing device\n");
483 err = pcistub_seize(dev);
485 /* Didn't find the device */
492 static void pcistub_remove(struct pci_dev *dev)
494 struct pcistub_device *psdev, *found_psdev = NULL;
497 dev_dbg(&dev->dev, "removing\n");
499 spin_lock_irqsave(&pcistub_devices_lock, flags);
501 xen_pcibk_config_quirk_release(dev);
503 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
504 if (psdev->dev == dev) {
510 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
513 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
516 if (found_psdev->pdev) {
517 printk(KERN_WARNING DRV_NAME ": ****** removing device "
518 "%s while still in-use! ******\n",
519 pci_name(found_psdev->dev));
520 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
521 " still access this device's i/o resources!\n");
522 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
523 "domain before binding device\n");
524 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
527 xen_pcibk_release_pci_dev(found_psdev->pdev,
531 spin_lock_irqsave(&pcistub_devices_lock, flags);
532 list_del(&found_psdev->dev_list);
533 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
535 /* the final put for releasing from the list */
536 pcistub_device_put(found_psdev);
540 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
542 .vendor = PCI_ANY_ID,
543 .device = PCI_ANY_ID,
544 .subvendor = PCI_ANY_ID,
545 .subdevice = PCI_ANY_ID,
550 #define PCI_NODENAME_MAX 40
551 static void kill_domain_by_device(struct pcistub_device *psdev)
553 struct xenbus_transaction xbt;
555 char nodename[PCI_NODENAME_MAX];
558 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
559 psdev->pdev->xdev->otherend_id);
562 err = xenbus_transaction_start(&xbt);
564 dev_err(&psdev->dev->dev,
565 "error %d when start xenbus transaction\n", err);
568 /*PV AER handlers will set this flag*/
569 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
570 err = xenbus_transaction_end(xbt, 0);
574 dev_err(&psdev->dev->dev,
575 "error %d when end xenbus transaction\n", err);
580 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
581 * backend need to have cooperation. In xen_pcibk, those steps will do similar
582 * jobs: send service request and waiting for front_end response.
584 static pci_ers_result_t common_process(struct pcistub_device *psdev,
585 pci_channel_state_t state, int aer_cmd,
586 pci_ers_result_t result)
588 pci_ers_result_t res = result;
589 struct xen_pcie_aer_op *aer_op;
592 /*with PV AER drivers*/
593 aer_op = &(psdev->pdev->sh_info->aer_op);
594 aer_op->cmd = aer_cmd ;
595 /*useful for error_detected callback*/
598 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
599 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
601 dev_err(&psdev->dev->dev,
602 DRV_NAME ": failed to get pcifront device\n");
603 return PCI_ERS_RESULT_NONE;
607 dev_dbg(&psdev->dev->dev,
608 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
609 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
610 /*local flag to mark there's aer request, xen_pcibk callback will use
611 * this flag to judge whether we need to check pci-front give aer
614 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
616 /*It is possible that a pcifront conf_read_write ops request invokes
617 * the callback which cause the spurious execution of wake_up.
618 * Yet it is harmless and better than a spinlock here
620 set_bit(_XEN_PCIB_active,
621 (unsigned long *)&psdev->pdev->sh_info->flags);
623 notify_remote_via_irq(psdev->pdev->evtchn_irq);
625 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
626 !(test_bit(_XEN_PCIB_active, (unsigned long *)
627 &psdev->pdev->sh_info->flags)), 300*HZ);
630 if (test_bit(_XEN_PCIB_active,
631 (unsigned long *)&psdev->pdev->sh_info->flags)) {
632 dev_err(&psdev->dev->dev,
633 "pcifront aer process not responding!\n");
634 clear_bit(_XEN_PCIB_active,
635 (unsigned long *)&psdev->pdev->sh_info->flags);
636 aer_op->err = PCI_ERS_RESULT_NONE;
640 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
642 if (test_bit(_XEN_PCIF_active,
643 (unsigned long *)&psdev->pdev->sh_info->flags)) {
644 dev_dbg(&psdev->dev->dev,
645 "schedule pci_conf service in " DRV_NAME "\n");
646 xen_pcibk_test_and_schedule_op(psdev->pdev);
649 res = (pci_ers_result_t)aer_op->err;
654 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
655 * of the device driver could provide this service, and then wait for pcifront
657 * @dev: pointer to PCI devices
658 * return value is used by aer_core do_recovery policy
660 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
662 struct pcistub_device *psdev;
663 pci_ers_result_t result;
665 result = PCI_ERS_RESULT_RECOVERED;
666 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
667 dev->bus->number, dev->devfn);
669 down_write(&pcistub_sem);
670 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
672 PCI_SLOT(dev->devfn),
673 PCI_FUNC(dev->devfn));
675 if (!psdev || !psdev->pdev) {
677 DRV_NAME " device is not found/assigned\n");
681 if (!psdev->pdev->sh_info) {
682 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
683 " by HVM, kill it\n");
684 kill_domain_by_device(psdev);
688 if (!test_bit(_XEN_PCIB_AERHANDLER,
689 (unsigned long *)&psdev->pdev->sh_info->flags)) {
691 "guest with no AER driver should have been killed\n");
694 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
696 if (result == PCI_ERS_RESULT_NONE ||
697 result == PCI_ERS_RESULT_DISCONNECT) {
699 "No AER slot_reset service or disconnected!\n");
700 kill_domain_by_device(psdev);
704 pcistub_device_put(psdev);
705 up_write(&pcistub_sem);
711 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
712 * in case of the device driver could provide this service, and then wait
714 * @dev: pointer to PCI devices
715 * return value is used by aer_core do_recovery policy
718 static pci_ers_result_t xen_pcibk_mmio_enabled(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_mmio_enabled(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_mmio, result);
754 if (result == PCI_ERS_RESULT_NONE ||
755 result == PCI_ERS_RESULT_DISCONNECT) {
757 "No AER mmio_enabled service or disconnected!\n");
758 kill_domain_by_device(psdev);
762 pcistub_device_put(psdev);
763 up_write(&pcistub_sem);
767 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
768 * in case of the device driver could provide this service, and then wait
770 * @dev: pointer to PCI devices
771 * @error: the current PCI connection state
772 * return value is used by aer_core do_recovery policy
775 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
776 pci_channel_state_t error)
778 struct pcistub_device *psdev;
779 pci_ers_result_t result;
781 result = PCI_ERS_RESULT_CAN_RECOVER;
782 dev_dbg(&dev->dev, "xen_pcibk_error_detected(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 /*Guest owns the device yet no aer handler regiested, kill guest*/
805 if (!test_bit(_XEN_PCIB_AERHANDLER,
806 (unsigned long *)&psdev->pdev->sh_info->flags)) {
807 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
808 kill_domain_by_device(psdev);
811 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
813 if (result == PCI_ERS_RESULT_NONE ||
814 result == PCI_ERS_RESULT_DISCONNECT) {
816 "No AER error_detected service or disconnected!\n");
817 kill_domain_by_device(psdev);
821 pcistub_device_put(psdev);
822 up_write(&pcistub_sem);
826 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
827 * in case of the device driver could provide this service, and then wait
829 * @dev: pointer to PCI devices
832 static void xen_pcibk_error_resume(struct pci_dev *dev)
834 struct pcistub_device *psdev;
836 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
837 dev->bus->number, dev->devfn);
839 down_write(&pcistub_sem);
840 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
842 PCI_SLOT(dev->devfn),
843 PCI_FUNC(dev->devfn));
845 if (!psdev || !psdev->pdev) {
847 DRV_NAME " device is not found/assigned\n");
851 if (!psdev->pdev->sh_info) {
852 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
853 " by HVM, kill it\n");
854 kill_domain_by_device(psdev);
858 if (!test_bit(_XEN_PCIB_AERHANDLER,
859 (unsigned long *)&psdev->pdev->sh_info->flags)) {
861 "guest with no AER driver should have been killed\n");
862 kill_domain_by_device(psdev);
865 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
866 PCI_ERS_RESULT_RECOVERED);
869 pcistub_device_put(psdev);
870 up_write(&pcistub_sem);
874 /*add xen_pcibk AER handling*/
875 static const struct pci_error_handlers xen_pcibk_error_handler = {
876 .error_detected = xen_pcibk_error_detected,
877 .mmio_enabled = xen_pcibk_mmio_enabled,
878 .slot_reset = xen_pcibk_slot_reset,
879 .resume = xen_pcibk_error_resume,
883 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
884 * for a normal device. I don't want it to be loaded automatically.
887 static struct pci_driver xen_pcibk_pci_driver = {
888 /* The name should be xen_pciback, but until the tools are updated
889 * we will keep it as pciback. */
891 .id_table = pcistub_ids,
892 .probe = pcistub_probe,
893 .remove = pcistub_remove,
894 .err_handler = &xen_pcibk_error_handler,
897 static inline int str_to_slot(const char *buf, int *domain, int *bus,
898 int *slot, int *func)
903 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
907 err = sscanf(buf, " %x:%x:%x.%c", domain, bus, slot, &wc);
911 err = sscanf(buf, " %x:%x:*.%c", domain, bus, &wc);
916 if (err == 4 && wc == '*')
921 /* try again without domain */
924 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
928 err = sscanf(buf, " %x:%x.%c", bus, slot, &wc);
932 err = sscanf(buf, " %x:*.%c", bus, &wc) + 1;
935 if (err == 3 && wc == '*')
941 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
942 *slot, int *func, int *reg, int *size, int *mask)
947 sscanf(buf, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot,
948 func, reg, size, mask);
954 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
956 struct pcistub_device_id *pci_dev_id;
961 for (slot = 0; !rc && slot < 32; ++slot)
962 rc = pcistub_device_id_add(domain, bus, slot, func);
967 for (func = 0; !rc && func < 8; ++func)
968 rc = pcistub_device_id_add(domain, bus, slot, func);
972 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
976 pci_dev_id->domain = domain;
977 pci_dev_id->bus = bus;
978 pci_dev_id->devfn = PCI_DEVFN(slot, func);
980 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
981 domain, bus, slot, func);
983 spin_lock_irqsave(&device_ids_lock, flags);
984 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
985 spin_unlock_irqrestore(&device_ids_lock, flags);
990 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
992 struct pcistub_device_id *pci_dev_id, *t;
996 spin_lock_irqsave(&device_ids_lock, flags);
997 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
999 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1000 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1001 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1002 /* Don't break; here because it's possible the same
1003 * slot could be in the list more than once
1005 list_del(&pci_dev_id->slot_list);
1010 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
1011 "seize list\n", domain, bus, slot, func);
1014 spin_unlock_irqrestore(&device_ids_lock, flags);
1019 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
1023 struct pcistub_device *psdev;
1024 struct pci_dev *dev;
1025 struct config_field *field;
1027 psdev = pcistub_device_find(domain, bus, slot, func);
1034 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1040 field->offset = reg;
1044 field->reset = NULL;
1045 field->release = NULL;
1046 field->clean = xen_pcibk_config_field_free;
1048 err = xen_pcibk_config_quirks_add_field(dev, field);
1053 pcistub_device_put(psdev);
1057 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1060 int domain, bus, slot, func;
1063 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1067 err = pcistub_device_id_add(domain, bus, slot, func);
1074 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1076 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1079 int domain, bus, slot, func;
1082 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1086 err = pcistub_device_id_remove(domain, bus, slot, func);
1093 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1095 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1097 struct pcistub_device_id *pci_dev_id;
1099 unsigned long flags;
1101 spin_lock_irqsave(&device_ids_lock, flags);
1102 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1103 if (count >= PAGE_SIZE)
1106 count += scnprintf(buf + count, PAGE_SIZE - count,
1107 "%04x:%02x:%02x.%d\n",
1108 pci_dev_id->domain, pci_dev_id->bus,
1109 PCI_SLOT(pci_dev_id->devfn),
1110 PCI_FUNC(pci_dev_id->devfn));
1112 spin_unlock_irqrestore(&device_ids_lock, flags);
1116 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1118 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1120 struct pcistub_device *psdev;
1121 struct xen_pcibk_dev_data *dev_data;
1123 unsigned long flags;
1125 spin_lock_irqsave(&pcistub_devices_lock, flags);
1126 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1127 if (count >= PAGE_SIZE)
1131 dev_data = pci_get_drvdata(psdev->dev);
1135 scnprintf(buf + count, PAGE_SIZE - count,
1136 "%s:%s:%sing:%ld\n",
1137 pci_name(psdev->dev),
1138 dev_data->isr_on ? "on" : "off",
1139 dev_data->ack_intr ? "ack" : "not ack",
1142 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1145 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1147 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1151 struct pcistub_device *psdev;
1152 struct xen_pcibk_dev_data *dev_data;
1153 int domain, bus, slot, func;
1156 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1160 psdev = pcistub_device_find(domain, bus, slot, func);
1164 dev_data = pci_get_drvdata(psdev->dev);
1168 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1169 dev_data->irq_name, dev_data->isr_on,
1172 dev_data->isr_on = !(dev_data->isr_on);
1173 if (dev_data->isr_on)
1174 dev_data->ack_intr = 1;
1177 pcistub_device_put(psdev);
1182 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1183 pcistub_irq_handler_switch);
1185 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1188 int domain, bus, slot, func, reg, size, mask;
1191 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1196 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1204 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1207 unsigned long flags;
1208 struct xen_pcibk_config_quirk *quirk;
1209 struct xen_pcibk_dev_data *dev_data;
1210 const struct config_field *field;
1211 const struct config_field_entry *cfg_entry;
1213 spin_lock_irqsave(&device_ids_lock, flags);
1214 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1215 if (count >= PAGE_SIZE)
1218 count += scnprintf(buf + count, PAGE_SIZE - count,
1219 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1220 quirk->pdev->bus->number,
1221 PCI_SLOT(quirk->pdev->devfn),
1222 PCI_FUNC(quirk->pdev->devfn),
1223 quirk->devid.vendor, quirk->devid.device,
1224 quirk->devid.subvendor,
1225 quirk->devid.subdevice);
1227 dev_data = pci_get_drvdata(quirk->pdev);
1229 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1230 field = cfg_entry->field;
1231 if (count >= PAGE_SIZE)
1234 count += scnprintf(buf + count, PAGE_SIZE - count,
1235 "\t\t%08x:%01x:%08x\n",
1236 cfg_entry->base_offset +
1237 field->offset, field->size,
1243 spin_unlock_irqrestore(&device_ids_lock, flags);
1247 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1250 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1253 int domain, bus, slot, func;
1255 struct pcistub_device *psdev;
1256 struct xen_pcibk_dev_data *dev_data;
1257 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1260 if (slot < 0 || func < 0) {
1264 psdev = pcistub_device_find(domain, bus, slot, func);
1270 dev_data = pci_get_drvdata(psdev->dev);
1271 /* the driver data for a device should never be null at this point */
1276 if (!dev_data->permissive) {
1277 dev_data->permissive = 1;
1278 /* Let user know that what they're doing could be unsafe */
1279 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1280 "configuration space accesses!\n");
1281 dev_warn(&psdev->dev->dev,
1282 "permissive mode is potentially unsafe!\n");
1285 pcistub_device_put(psdev);
1292 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1294 struct pcistub_device *psdev;
1295 struct xen_pcibk_dev_data *dev_data;
1297 unsigned long flags;
1298 spin_lock_irqsave(&pcistub_devices_lock, flags);
1299 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1300 if (count >= PAGE_SIZE)
1304 dev_data = pci_get_drvdata(psdev->dev);
1305 if (!dev_data || !dev_data->permissive)
1308 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1309 pci_name(psdev->dev));
1311 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1314 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1317 static void pcistub_exit(void)
1319 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1320 driver_remove_file(&xen_pcibk_pci_driver.driver,
1321 &driver_attr_remove_slot);
1322 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1323 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1324 driver_remove_file(&xen_pcibk_pci_driver.driver,
1325 &driver_attr_permissive);
1326 driver_remove_file(&xen_pcibk_pci_driver.driver,
1327 &driver_attr_irq_handlers);
1328 driver_remove_file(&xen_pcibk_pci_driver.driver,
1329 &driver_attr_irq_handler_state);
1330 pci_unregister_driver(&xen_pcibk_pci_driver);
1333 static int __init pcistub_init(void)
1337 int domain, bus, slot, func;
1340 if (pci_devs_to_hide && *pci_devs_to_hide) {
1346 err = sscanf(pci_devs_to_hide + pos,
1347 " (%x:%x:%x.%x) %n",
1348 &domain, &bus, &slot, &func, &parsed);
1352 err = sscanf(pci_devs_to_hide + pos,
1353 " (%x:%x:%x.%c) %n",
1354 &domain, &bus, &slot, &wc,
1359 err = sscanf(pci_devs_to_hide + pos,
1361 &domain, &bus, &wc, &parsed) + 1;
1365 if (err != 4 || wc != '*') {
1368 err = sscanf(pci_devs_to_hide + pos,
1370 &bus, &slot, &func, &parsed);
1374 err = sscanf(pci_devs_to_hide + pos,
1381 err = sscanf(pci_devs_to_hide + pos,
1383 &bus, &wc, &parsed) + 1;
1386 if (err != 3 || wc != '*')
1390 err = pcistub_device_id_add(domain, bus, slot, func);
1394 /* if parsed<=0, we've reached the end of the string */
1396 } while (parsed > 0 && pci_devs_to_hide[pos]);
1399 /* If we're the first PCI Device Driver to register, we're the
1400 * first one to get offered PCI devices as they become
1401 * available (and thus we can be the first to grab them)
1403 err = pci_register_driver(&xen_pcibk_pci_driver);
1407 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1408 &driver_attr_new_slot);
1410 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1411 &driver_attr_remove_slot);
1413 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1414 &driver_attr_slots);
1416 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1417 &driver_attr_quirks);
1419 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1420 &driver_attr_permissive);
1423 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1424 &driver_attr_irq_handlers);
1426 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1427 &driver_attr_irq_handler_state);
1435 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
1436 pci_devs_to_hide + pos);
1442 * fs_initcall happens before device_initcall
1443 * so xen_pcibk *should* get called first (b/c we
1444 * want to suck up any device before other drivers
1445 * get a chance by being the first pci device
1446 * driver to register)
1448 fs_initcall(pcistub_init);
1451 static int __init xen_pcibk_init(void)
1455 if (!xen_initial_domain())
1458 err = xen_pcibk_config_init();
1463 err = pcistub_init();
1468 pcistub_init_devices_late();
1469 err = xen_pcibk_xenbus_register();
1476 static void __exit xen_pcibk_cleanup(void)
1478 xen_pcibk_xenbus_unregister();
1482 module_init(xen_pcibk_init);
1483 module_exit(xen_pcibk_cleanup);
1485 MODULE_LICENSE("Dual BSD/GPL");
1486 MODULE_ALIAS("xen-backend:pci");