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);
108 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
109 dev_info(&dev->dev, "Could not reload PCI state\n");
111 pci_restore_state(dev);
114 struct physdev_pci_device ppdev = {
115 .seg = pci_domain_nr(dev->bus),
116 .bus = dev->bus->number,
119 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
122 if (err && err != -ENOSYS)
123 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
127 /* Disable the device */
128 xen_pcibk_reset_device(dev);
131 pci_set_drvdata(dev, NULL);
133 /* Clean-up the device */
134 xen_pcibk_config_free_dyn_fields(dev);
135 xen_pcibk_config_free_dev(dev);
137 pci_clear_dev_assigned(dev);
143 static inline void pcistub_device_get(struct pcistub_device *psdev)
145 kref_get(&psdev->kref);
148 static inline void pcistub_device_put(struct pcistub_device *psdev)
150 kref_put(&psdev->kref, pcistub_device_release);
153 static struct pcistub_device *pcistub_device_find(int domain, int bus,
156 struct pcistub_device *psdev = NULL;
159 spin_lock_irqsave(&pcistub_devices_lock, flags);
161 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
162 if (psdev->dev != NULL
163 && domain == pci_domain_nr(psdev->dev->bus)
164 && bus == psdev->dev->bus->number
165 && slot == PCI_SLOT(psdev->dev->devfn)
166 && func == PCI_FUNC(psdev->dev->devfn)) {
167 pcistub_device_get(psdev);
176 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
180 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
181 struct pcistub_device *psdev)
183 struct pci_dev *pci_dev = NULL;
186 pcistub_device_get(psdev);
188 spin_lock_irqsave(&psdev->lock, flags);
191 pci_dev = psdev->dev;
193 spin_unlock_irqrestore(&psdev->lock, flags);
196 pcistub_device_put(psdev);
201 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
205 struct pcistub_device *psdev;
206 struct pci_dev *found_dev = NULL;
209 spin_lock_irqsave(&pcistub_devices_lock, flags);
211 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
212 if (psdev->dev != NULL
213 && domain == pci_domain_nr(psdev->dev->bus)
214 && bus == psdev->dev->bus->number
215 && slot == PCI_SLOT(psdev->dev->devfn)
216 && func == PCI_FUNC(psdev->dev->devfn)) {
217 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
226 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
229 struct pcistub_device *psdev;
230 struct pci_dev *found_dev = NULL;
233 spin_lock_irqsave(&pcistub_devices_lock, flags);
235 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
236 if (psdev->dev == dev) {
237 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
242 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
248 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
249 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
250 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
251 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
253 * As such we have to be careful.
255 * To make this easier, the caller has to hold the device lock.
257 void pcistub_put_pci_dev(struct pci_dev *dev)
259 struct pcistub_device *psdev, *found_psdev = NULL;
261 struct xen_pcibk_dev_data *dev_data;
264 spin_lock_irqsave(&pcistub_devices_lock, flags);
266 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
267 if (psdev->dev == dev) {
273 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
274 if (WARN_ON(!found_psdev))
277 /*hold this lock for avoiding breaking link between
278 * pcistub and xen_pcibk when AER is in processing
280 down_write(&pcistub_sem);
281 /* Cleanup our device
282 * (so it's ready for the next domain)
284 device_lock_assert(&dev->dev);
285 __pci_reset_function_locked(dev);
287 dev_data = pci_get_drvdata(dev);
288 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 * The usual sequence is pci_save_state & pci_restore_state
292 * but the guest might have messed the configuration space up.
293 * Use the initial version (when device was bound to us).
295 pci_restore_state(dev);
297 dev_info(&dev->dev, "Could not reload PCI state\n");
298 /* This disables the device. */
299 xen_pcibk_reset_device(dev);
301 /* And cleanup up our emulated fields. */
302 xen_pcibk_config_reset_dev(dev);
303 xen_pcibk_config_free_dyn_fields(dev);
305 xen_unregister_device_domain_owner(dev);
307 spin_lock_irqsave(&found_psdev->lock, flags);
308 found_psdev->pdev = NULL;
309 spin_unlock_irqrestore(&found_psdev->lock, flags);
311 pcistub_device_put(found_psdev);
312 up_write(&pcistub_sem);
315 static int pcistub_match_one(struct pci_dev *dev,
316 struct pcistub_device_id *pdev_id)
318 /* Match the specified device by domain, bus, slot, func and also if
319 * any of the device's parent bridges match.
321 for (; dev != NULL; dev = dev->bus->self) {
322 if (pci_domain_nr(dev->bus) == pdev_id->domain
323 && dev->bus->number == pdev_id->bus
324 && dev->devfn == pdev_id->devfn)
327 /* Sometimes topmost bridge links to itself. */
328 if (dev == dev->bus->self)
335 static int pcistub_match(struct pci_dev *dev)
337 struct pcistub_device_id *pdev_id;
341 spin_lock_irqsave(&device_ids_lock, flags);
342 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
343 if (pcistub_match_one(dev, pdev_id)) {
348 spin_unlock_irqrestore(&device_ids_lock, flags);
353 static int pcistub_init_device(struct pci_dev *dev)
355 struct xen_pcibk_dev_data *dev_data;
358 dev_dbg(&dev->dev, "initializing...\n");
360 /* The PCI backend is not intended to be a module (or to work with
361 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
362 * would need to be called somewhere to free the memory allocated
363 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
365 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
366 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
371 pci_set_drvdata(dev, dev_data);
374 * Setup name for fake IRQ handler. It will only be enabled
375 * once the device is turned on by the guest.
377 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
379 dev_dbg(&dev->dev, "initializing config\n");
381 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
382 err = xen_pcibk_config_init_dev(dev);
386 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
387 * must do this here because pcibios_enable_device may specify
388 * the pci device's true irq (and possibly its other resources)
389 * if they differ from what's in the configuration space.
390 * This makes the assumption that the device's resources won't
391 * change after this point (otherwise this code may break!)
393 dev_dbg(&dev->dev, "enabling device\n");
394 err = pci_enable_device(dev);
399 struct physdev_pci_device ppdev = {
400 .seg = pci_domain_nr(dev->bus),
401 .bus = dev->bus->number,
405 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
406 if (err && err != -ENOSYS)
407 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
411 /* We need the device active to save the state. */
412 dev_dbg(&dev->dev, "save state of device\n");
414 dev_data->pci_saved_state = pci_store_saved_state(dev);
415 if (!dev_data->pci_saved_state)
416 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
418 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
419 __pci_reset_function_locked(dev);
420 pci_restore_state(dev);
422 /* Now disable the device (this also ensures some private device
423 * data is setup before we export)
425 dev_dbg(&dev->dev, "reset device\n");
426 xen_pcibk_reset_device(dev);
428 pci_set_dev_assigned(dev);
432 xen_pcibk_config_free_dev(dev);
435 pci_set_drvdata(dev, NULL);
441 * Because some initialization still happens on
442 * devices during fs_initcall, we need to defer
443 * full initialization of our devices until
446 static int __init pcistub_init_devices_late(void)
448 struct pcistub_device *psdev;
452 spin_lock_irqsave(&pcistub_devices_lock, flags);
454 while (!list_empty(&seized_devices)) {
455 psdev = container_of(seized_devices.next,
456 struct pcistub_device, dev_list);
457 list_del(&psdev->dev_list);
459 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
461 err = pcistub_init_device(psdev->dev);
463 dev_err(&psdev->dev->dev,
464 "error %d initializing device\n", err);
469 spin_lock_irqsave(&pcistub_devices_lock, flags);
472 list_add_tail(&psdev->dev_list, &pcistub_devices);
475 initialize_devices = 1;
477 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
482 static int pcistub_seize(struct pci_dev *dev)
484 struct pcistub_device *psdev;
488 psdev = pcistub_device_alloc(dev);
492 spin_lock_irqsave(&pcistub_devices_lock, flags);
494 if (initialize_devices) {
495 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
497 /* don't want irqs disabled when calling pcistub_init_device */
498 err = pcistub_init_device(psdev->dev);
500 spin_lock_irqsave(&pcistub_devices_lock, flags);
503 list_add(&psdev->dev_list, &pcistub_devices);
505 dev_dbg(&dev->dev, "deferring initialization\n");
506 list_add(&psdev->dev_list, &seized_devices);
509 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
512 pcistub_device_put(psdev);
517 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
518 * other functions that take the sysfs lock. */
519 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
523 dev_dbg(&dev->dev, "probing...\n");
525 if (pcistub_match(dev)) {
527 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
528 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
529 dev_err(&dev->dev, "can't export pci devices that "
530 "don't have a normal (0) or bridge (1) "
536 dev_info(&dev->dev, "seizing device\n");
537 err = pcistub_seize(dev);
539 /* Didn't find the device */
546 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
547 * other functions that take the sysfs lock. */
548 static void pcistub_remove(struct pci_dev *dev)
550 struct pcistub_device *psdev, *found_psdev = NULL;
553 dev_dbg(&dev->dev, "removing\n");
555 spin_lock_irqsave(&pcistub_devices_lock, flags);
557 xen_pcibk_config_quirk_release(dev);
559 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
560 if (psdev->dev == dev) {
566 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
569 dev_dbg(&dev->dev, "found device to remove %s\n",
570 found_psdev->pdev ? "- in-use" : "");
572 if (found_psdev->pdev) {
573 int domid = xen_find_device_domain_owner(dev);
575 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
576 pci_name(found_psdev->dev), domid);
577 pr_warn("****** driver domain may still access this device's i/o resources!\n");
578 pr_warn("****** shutdown driver domain before binding device\n");
579 pr_warn("****** to other drivers or domains\n");
581 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
583 xen_pcibk_release_pci_dev(found_psdev->pdev,
585 false /* caller holds the lock. */);
588 spin_lock_irqsave(&pcistub_devices_lock, flags);
589 list_del(&found_psdev->dev_list);
590 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
592 /* the final put for releasing from the list */
593 pcistub_device_put(found_psdev);
597 static const struct pci_device_id pcistub_ids[] = {
599 .vendor = PCI_ANY_ID,
600 .device = PCI_ANY_ID,
601 .subvendor = PCI_ANY_ID,
602 .subdevice = PCI_ANY_ID,
607 #define PCI_NODENAME_MAX 40
608 static void kill_domain_by_device(struct pcistub_device *psdev)
610 struct xenbus_transaction xbt;
612 char nodename[PCI_NODENAME_MAX];
615 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
616 psdev->pdev->xdev->otherend_id);
619 err = xenbus_transaction_start(&xbt);
621 dev_err(&psdev->dev->dev,
622 "error %d when start xenbus transaction\n", err);
625 /*PV AER handlers will set this flag*/
626 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
627 err = xenbus_transaction_end(xbt, 0);
631 dev_err(&psdev->dev->dev,
632 "error %d when end xenbus transaction\n", err);
637 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
638 * backend need to have cooperation. In xen_pcibk, those steps will do similar
639 * jobs: send service request and waiting for front_end response.
641 static pci_ers_result_t common_process(struct pcistub_device *psdev,
642 pci_channel_state_t state, int aer_cmd,
643 pci_ers_result_t result)
645 pci_ers_result_t res = result;
646 struct xen_pcie_aer_op *aer_op;
647 struct xen_pcibk_device *pdev = psdev->pdev;
648 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
651 /*with PV AER drivers*/
652 aer_op = &(sh_info->aer_op);
653 aer_op->cmd = aer_cmd ;
654 /*useful for error_detected callback*/
657 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
658 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
660 dev_err(&psdev->dev->dev,
661 DRV_NAME ": failed to get pcifront device\n");
662 return PCI_ERS_RESULT_NONE;
666 dev_dbg(&psdev->dev->dev,
667 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
668 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
669 /*local flag to mark there's aer request, xen_pcibk callback will use
670 * this flag to judge whether we need to check pci-front give aer
673 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
675 /*It is possible that a pcifront conf_read_write ops request invokes
676 * the callback which cause the spurious execution of wake_up.
677 * Yet it is harmless and better than a spinlock here
679 set_bit(_XEN_PCIB_active,
680 (unsigned long *)&sh_info->flags);
682 notify_remote_via_irq(pdev->evtchn_irq);
684 /* Enable IRQ to signal "request done". */
685 xen_pcibk_lateeoi(pdev, 0);
687 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
688 !(test_bit(_XEN_PCIB_active, (unsigned long *)
689 &sh_info->flags)), 300*HZ);
691 /* Enable IRQ for pcifront request if not already active. */
692 if (!test_bit(_PDEVF_op_active, &pdev->flags))
693 xen_pcibk_lateeoi(pdev, 0);
696 if (test_bit(_XEN_PCIB_active,
697 (unsigned long *)&sh_info->flags)) {
698 dev_err(&psdev->dev->dev,
699 "pcifront aer process not responding!\n");
700 clear_bit(_XEN_PCIB_active,
701 (unsigned long *)&sh_info->flags);
702 aer_op->err = PCI_ERS_RESULT_NONE;
706 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
708 res = (pci_ers_result_t)aer_op->err;
713 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
714 * of the device driver could provide this service, and then wait for pcifront
716 * @dev: pointer to PCI devices
717 * return value is used by aer_core do_recovery policy
719 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
721 struct pcistub_device *psdev;
722 pci_ers_result_t result;
724 result = PCI_ERS_RESULT_RECOVERED;
725 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
726 dev->bus->number, dev->devfn);
728 down_write(&pcistub_sem);
729 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
731 PCI_SLOT(dev->devfn),
732 PCI_FUNC(dev->devfn));
734 if (!psdev || !psdev->pdev) {
736 DRV_NAME " device is not found/assigned\n");
740 if (!psdev->pdev->sh_info) {
741 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
742 " by HVM, kill it\n");
743 kill_domain_by_device(psdev);
747 if (!test_bit(_XEN_PCIB_AERHANDLER,
748 (unsigned long *)&psdev->pdev->sh_info->flags)) {
750 "guest with no AER driver should have been killed\n");
753 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
755 if (result == PCI_ERS_RESULT_NONE ||
756 result == PCI_ERS_RESULT_DISCONNECT) {
758 "No AER slot_reset service or disconnected!\n");
759 kill_domain_by_device(psdev);
763 pcistub_device_put(psdev);
764 up_write(&pcistub_sem);
770 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
771 * in case of the device driver could provide this service, and then wait
773 * @dev: pointer to PCI devices
774 * return value is used by aer_core do_recovery policy
777 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
779 struct pcistub_device *psdev;
780 pci_ers_result_t result;
782 result = PCI_ERS_RESULT_RECOVERED;
783 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
784 dev->bus->number, dev->devfn);
786 down_write(&pcistub_sem);
787 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
789 PCI_SLOT(dev->devfn),
790 PCI_FUNC(dev->devfn));
792 if (!psdev || !psdev->pdev) {
794 DRV_NAME " device is not found/assigned\n");
798 if (!psdev->pdev->sh_info) {
799 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
800 " by HVM, kill it\n");
801 kill_domain_by_device(psdev);
805 if (!test_bit(_XEN_PCIB_AERHANDLER,
806 (unsigned long *)&psdev->pdev->sh_info->flags)) {
808 "guest with no AER driver should have been killed\n");
811 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
813 if (result == PCI_ERS_RESULT_NONE ||
814 result == PCI_ERS_RESULT_DISCONNECT) {
816 "No AER mmio_enabled service or disconnected!\n");
817 kill_domain_by_device(psdev);
821 pcistub_device_put(psdev);
822 up_write(&pcistub_sem);
826 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
827 * in case of the device driver could provide this service, and then wait
829 * @dev: pointer to PCI devices
830 * @error: the current PCI connection state
831 * return value is used by aer_core do_recovery policy
834 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
835 pci_channel_state_t error)
837 struct pcistub_device *psdev;
838 pci_ers_result_t result;
840 result = PCI_ERS_RESULT_CAN_RECOVER;
841 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
842 dev->bus->number, dev->devfn);
844 down_write(&pcistub_sem);
845 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
847 PCI_SLOT(dev->devfn),
848 PCI_FUNC(dev->devfn));
850 if (!psdev || !psdev->pdev) {
852 DRV_NAME " device is not found/assigned\n");
856 if (!psdev->pdev->sh_info) {
857 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
858 " by HVM, kill it\n");
859 kill_domain_by_device(psdev);
863 /*Guest owns the device yet no aer handler regiested, kill guest*/
864 if (!test_bit(_XEN_PCIB_AERHANDLER,
865 (unsigned long *)&psdev->pdev->sh_info->flags)) {
866 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
867 kill_domain_by_device(psdev);
870 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
872 if (result == PCI_ERS_RESULT_NONE ||
873 result == PCI_ERS_RESULT_DISCONNECT) {
875 "No AER error_detected service or disconnected!\n");
876 kill_domain_by_device(psdev);
880 pcistub_device_put(psdev);
881 up_write(&pcistub_sem);
885 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
886 * in case of the device driver could provide this service, and then wait
888 * @dev: pointer to PCI devices
891 static void xen_pcibk_error_resume(struct pci_dev *dev)
893 struct pcistub_device *psdev;
895 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
896 dev->bus->number, dev->devfn);
898 down_write(&pcistub_sem);
899 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
901 PCI_SLOT(dev->devfn),
902 PCI_FUNC(dev->devfn));
904 if (!psdev || !psdev->pdev) {
906 DRV_NAME " device is not found/assigned\n");
910 if (!psdev->pdev->sh_info) {
911 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
912 " by HVM, kill it\n");
913 kill_domain_by_device(psdev);
917 if (!test_bit(_XEN_PCIB_AERHANDLER,
918 (unsigned long *)&psdev->pdev->sh_info->flags)) {
920 "guest with no AER driver should have been killed\n");
921 kill_domain_by_device(psdev);
924 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
925 PCI_ERS_RESULT_RECOVERED);
928 pcistub_device_put(psdev);
929 up_write(&pcistub_sem);
933 /*add xen_pcibk AER handling*/
934 static const struct pci_error_handlers xen_pcibk_error_handler = {
935 .error_detected = xen_pcibk_error_detected,
936 .mmio_enabled = xen_pcibk_mmio_enabled,
937 .slot_reset = xen_pcibk_slot_reset,
938 .resume = xen_pcibk_error_resume,
942 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
943 * for a normal device. I don't want it to be loaded automatically.
946 static struct pci_driver xen_pcibk_pci_driver = {
947 /* The name should be xen_pciback, but until the tools are updated
948 * we will keep it as pciback. */
950 .id_table = pcistub_ids,
951 .probe = pcistub_probe,
952 .remove = pcistub_remove,
953 .err_handler = &xen_pcibk_error_handler,
956 static inline int str_to_slot(const char *buf, int *domain, int *bus,
957 int *slot, int *func)
961 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
965 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
969 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
972 if (parsed && !buf[parsed])
975 /* try again without domain */
977 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
980 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
984 sscanf(buf, " %x:*.* %n", bus, &parsed);
987 if (parsed && !buf[parsed])
993 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
994 *slot, int *func, int *reg, int *size, int *mask)
998 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
999 reg, size, mask, &parsed);
1000 if (parsed && !buf[parsed])
1003 /* try again without domain */
1005 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1007 if (parsed && !buf[parsed])
1013 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1015 struct pcistub_device_id *pci_dev_id;
1016 unsigned long flags;
1017 int rc = 0, devfn = PCI_DEVFN(slot, func);
1020 for (slot = 0; !rc && slot < 32; ++slot)
1021 rc = pcistub_device_id_add(domain, bus, slot, func);
1026 for (func = 0; !rc && func < 8; ++func)
1027 rc = pcistub_device_id_add(domain, bus, slot, func);
1032 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1033 || !defined(CONFIG_PCI_DOMAINS)
1034 !pci_domains_supported ? domain :
1036 domain < 0 || domain > 0xffff)
1037 || bus < 0 || bus > 0xff
1038 || PCI_SLOT(devfn) != slot
1039 || PCI_FUNC(devfn) != func)
1042 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1046 pci_dev_id->domain = domain;
1047 pci_dev_id->bus = bus;
1048 pci_dev_id->devfn = devfn;
1050 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1051 domain, bus, slot, func);
1053 spin_lock_irqsave(&device_ids_lock, flags);
1054 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1055 spin_unlock_irqrestore(&device_ids_lock, flags);
1060 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1062 struct pcistub_device_id *pci_dev_id, *t;
1064 unsigned long flags;
1066 spin_lock_irqsave(&device_ids_lock, flags);
1067 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1069 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1070 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1071 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1072 /* Don't break; here because it's possible the same
1073 * slot could be in the list more than once
1075 list_del(&pci_dev_id->slot_list);
1080 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1081 domain, bus, slot, func);
1084 spin_unlock_irqrestore(&device_ids_lock, flags);
1089 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1090 unsigned int reg, unsigned int size,
1094 struct pcistub_device *psdev;
1095 struct pci_dev *dev;
1096 struct config_field *field;
1098 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1101 psdev = pcistub_device_find(domain, bus, slot, func);
1108 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1114 field->offset = reg;
1118 field->reset = NULL;
1119 field->release = NULL;
1120 field->clean = xen_pcibk_config_field_free;
1122 err = xen_pcibk_config_quirks_add_field(dev, field);
1127 pcistub_device_put(psdev);
1131 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1134 int domain, bus, slot, func;
1137 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1141 err = pcistub_device_id_add(domain, bus, slot, func);
1148 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1150 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1153 int domain, bus, slot, func;
1156 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1160 err = pcistub_device_id_remove(domain, bus, slot, func);
1167 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1169 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1171 struct pcistub_device_id *pci_dev_id;
1173 unsigned long flags;
1175 spin_lock_irqsave(&device_ids_lock, flags);
1176 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1177 if (count >= PAGE_SIZE)
1180 count += scnprintf(buf + count, PAGE_SIZE - count,
1181 "%04x:%02x:%02x.%d\n",
1182 pci_dev_id->domain, pci_dev_id->bus,
1183 PCI_SLOT(pci_dev_id->devfn),
1184 PCI_FUNC(pci_dev_id->devfn));
1186 spin_unlock_irqrestore(&device_ids_lock, flags);
1190 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1192 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1194 struct pcistub_device *psdev;
1195 struct xen_pcibk_dev_data *dev_data;
1197 unsigned long flags;
1199 spin_lock_irqsave(&pcistub_devices_lock, flags);
1200 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1201 if (count >= PAGE_SIZE)
1205 dev_data = pci_get_drvdata(psdev->dev);
1209 scnprintf(buf + count, PAGE_SIZE - count,
1210 "%s:%s:%sing:%ld\n",
1211 pci_name(psdev->dev),
1212 dev_data->isr_on ? "on" : "off",
1213 dev_data->ack_intr ? "ack" : "not ack",
1216 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1219 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1221 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1225 struct pcistub_device *psdev;
1226 struct xen_pcibk_dev_data *dev_data;
1227 int domain, bus, slot, func;
1230 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1234 psdev = pcistub_device_find(domain, bus, slot, func);
1240 dev_data = pci_get_drvdata(psdev->dev);
1246 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1247 dev_data->irq_name, dev_data->isr_on,
1250 dev_data->isr_on = !(dev_data->isr_on);
1251 if (dev_data->isr_on)
1252 dev_data->ack_intr = 1;
1255 pcistub_device_put(psdev);
1260 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1261 pcistub_irq_handler_switch);
1263 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1266 int domain, bus, slot, func, reg, size, mask;
1269 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1274 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1282 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1285 unsigned long flags;
1286 struct xen_pcibk_config_quirk *quirk;
1287 struct xen_pcibk_dev_data *dev_data;
1288 const struct config_field *field;
1289 const struct config_field_entry *cfg_entry;
1291 spin_lock_irqsave(&device_ids_lock, flags);
1292 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1293 if (count >= PAGE_SIZE)
1296 count += scnprintf(buf + count, PAGE_SIZE - count,
1297 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1298 quirk->pdev->bus->number,
1299 PCI_SLOT(quirk->pdev->devfn),
1300 PCI_FUNC(quirk->pdev->devfn),
1301 quirk->devid.vendor, quirk->devid.device,
1302 quirk->devid.subvendor,
1303 quirk->devid.subdevice);
1305 dev_data = pci_get_drvdata(quirk->pdev);
1307 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1308 field = cfg_entry->field;
1309 if (count >= PAGE_SIZE)
1312 count += scnprintf(buf + count, PAGE_SIZE - count,
1313 "\t\t%08x:%01x:%08x\n",
1314 cfg_entry->base_offset +
1315 field->offset, field->size,
1321 spin_unlock_irqrestore(&device_ids_lock, flags);
1325 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1328 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1331 int domain, bus, slot, func;
1333 struct pcistub_device *psdev;
1334 struct xen_pcibk_dev_data *dev_data;
1336 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1340 psdev = pcistub_device_find(domain, bus, slot, func);
1346 dev_data = pci_get_drvdata(psdev->dev);
1347 /* the driver data for a device should never be null at this point */
1352 if (!dev_data->permissive) {
1353 dev_data->permissive = 1;
1354 /* Let user know that what they're doing could be unsafe */
1355 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1356 "configuration space accesses!\n");
1357 dev_warn(&psdev->dev->dev,
1358 "permissive mode is potentially unsafe!\n");
1361 pcistub_device_put(psdev);
1368 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1370 struct pcistub_device *psdev;
1371 struct xen_pcibk_dev_data *dev_data;
1373 unsigned long flags;
1374 spin_lock_irqsave(&pcistub_devices_lock, flags);
1375 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1376 if (count >= PAGE_SIZE)
1380 dev_data = pci_get_drvdata(psdev->dev);
1381 if (!dev_data || !dev_data->permissive)
1384 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1385 pci_name(psdev->dev));
1387 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1390 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1393 static void pcistub_exit(void)
1395 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1396 driver_remove_file(&xen_pcibk_pci_driver.driver,
1397 &driver_attr_remove_slot);
1398 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1399 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1400 driver_remove_file(&xen_pcibk_pci_driver.driver,
1401 &driver_attr_permissive);
1402 driver_remove_file(&xen_pcibk_pci_driver.driver,
1403 &driver_attr_irq_handlers);
1404 driver_remove_file(&xen_pcibk_pci_driver.driver,
1405 &driver_attr_irq_handler_state);
1406 pci_unregister_driver(&xen_pcibk_pci_driver);
1409 static int __init pcistub_init(void)
1413 int domain, bus, slot, func;
1416 if (pci_devs_to_hide && *pci_devs_to_hide) {
1420 err = sscanf(pci_devs_to_hide + pos,
1421 " (%x:%x:%x.%x) %n",
1422 &domain, &bus, &slot, &func, &parsed);
1426 sscanf(pci_devs_to_hide + pos,
1428 &domain, &bus, &slot, &parsed);
1432 sscanf(pci_devs_to_hide + pos,
1434 &domain, &bus, &parsed);
1440 err = sscanf(pci_devs_to_hide + pos,
1442 &bus, &slot, &func, &parsed);
1446 sscanf(pci_devs_to_hide + pos,
1448 &bus, &slot, &parsed);
1452 sscanf(pci_devs_to_hide + pos,
1462 err = pcistub_device_id_add(domain, bus, slot, func);
1467 } while (pci_devs_to_hide[pos]);
1470 /* If we're the first PCI Device Driver to register, we're the
1471 * first one to get offered PCI devices as they become
1472 * available (and thus we can be the first to grab them)
1474 err = pci_register_driver(&xen_pcibk_pci_driver);
1478 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1479 &driver_attr_new_slot);
1481 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1482 &driver_attr_remove_slot);
1484 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1485 &driver_attr_slots);
1487 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1488 &driver_attr_quirks);
1490 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1491 &driver_attr_permissive);
1494 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1495 &driver_attr_irq_handlers);
1497 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1498 &driver_attr_irq_handler_state);
1506 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1507 pci_devs_to_hide + pos);
1513 * fs_initcall happens before device_initcall
1514 * so xen_pcibk *should* get called first (b/c we
1515 * want to suck up any device before other drivers
1516 * get a chance by being the first pci device
1517 * driver to register)
1519 fs_initcall(pcistub_init);
1522 #ifdef CONFIG_PCI_IOV
1523 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1525 struct pcistub_device *psdev = NULL;
1526 unsigned long flags;
1529 spin_lock_irqsave(&pcistub_devices_lock, flags);
1530 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1531 if (!psdev->pdev && psdev->dev != pdev
1532 && pci_physfn(psdev->dev) == pdev) {
1537 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1543 static int pci_stub_notifier(struct notifier_block *nb,
1544 unsigned long action, void *data)
1546 struct device *dev = data;
1547 const struct pci_dev *pdev = to_pci_dev(dev);
1549 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1552 if (!pdev->is_physfn)
1556 struct pcistub_device *psdev = find_vfs(pdev);
1559 device_release_driver(&psdev->dev->dev);
1564 static struct notifier_block pci_stub_nb = {
1565 .notifier_call = pci_stub_notifier,
1569 static int __init xen_pcibk_init(void)
1573 if (!xen_initial_domain())
1576 err = xen_pcibk_config_init();
1581 err = pcistub_init();
1586 pcistub_init_devices_late();
1587 err = xen_pcibk_xenbus_register();
1590 #ifdef CONFIG_PCI_IOV
1592 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1598 static void __exit xen_pcibk_cleanup(void)
1600 #ifdef CONFIG_PCI_IOV
1601 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1603 xen_pcibk_xenbus_unregister();
1607 module_init(xen_pcibk_init);
1608 module_exit(xen_pcibk_cleanup);
1610 MODULE_LICENSE("Dual BSD/GPL");
1611 MODULE_ALIAS("xen-backend:pci");