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 #define PCISTUB_DRIVER_NAME "pciback"
30 static char *pci_devs_to_hide;
31 wait_queue_head_t xen_pcibk_aer_wait_queue;
32 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
35 static DECLARE_RWSEM(pcistub_sem);
36 module_param_named(hide, pci_devs_to_hide, charp, 0444);
38 struct pcistub_device_id {
39 struct list_head slot_list;
44 static LIST_HEAD(pcistub_device_ids);
45 static DEFINE_SPINLOCK(device_ids_lock);
47 struct pcistub_device {
49 struct list_head dev_list;
53 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
56 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
57 * flag must be locked with pcistub_devices_lock
59 static DEFINE_SPINLOCK(pcistub_devices_lock);
60 static LIST_HEAD(pcistub_devices);
62 /* wait for device_initcall before initializing our devices
63 * (see pcistub_init_devices_late)
65 static int initialize_devices;
66 static LIST_HEAD(seized_devices);
68 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
70 struct pcistub_device *psdev;
72 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
74 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
78 psdev->dev = pci_dev_get(dev);
84 kref_init(&psdev->kref);
85 spin_lock_init(&psdev->lock);
90 /* Don't call this directly as it's called by pcistub_device_put */
91 static void pcistub_device_release(struct kref *kref)
93 struct pcistub_device *psdev;
95 struct xen_pcibk_dev_data *dev_data;
97 psdev = container_of(kref, struct pcistub_device, kref);
99 dev_data = pci_get_drvdata(dev);
101 dev_dbg(&dev->dev, "pcistub_device_release\n");
103 xen_unregister_device_domain_owner(dev);
105 /* Call the reset function which does not take lock as this
106 * is called from "unbind" which takes a device_lock mutex.
108 __pci_reset_function_locked(dev);
110 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
111 dev_info(&dev->dev, "Could not reload PCI state\n");
113 pci_restore_state(dev);
116 struct physdev_pci_device ppdev = {
117 .seg = pci_domain_nr(dev->bus),
118 .bus = dev->bus->number,
121 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
124 if (err && err != -ENOSYS)
125 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
129 /* Disable the device */
130 xen_pcibk_reset_device(dev);
133 pci_set_drvdata(dev, NULL);
135 /* Clean-up the device */
136 xen_pcibk_config_free_dyn_fields(dev);
137 xen_pcibk_config_free_dev(dev);
139 pci_clear_dev_assigned(dev);
145 static inline void pcistub_device_get(struct pcistub_device *psdev)
147 kref_get(&psdev->kref);
150 static inline void pcistub_device_put(struct pcistub_device *psdev)
152 kref_put(&psdev->kref, pcistub_device_release);
155 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
158 struct pcistub_device *psdev;
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
173 static struct pcistub_device *pcistub_device_find(int domain, int bus,
176 struct pcistub_device *psdev;
179 spin_lock_irqsave(&pcistub_devices_lock, flags);
181 psdev = pcistub_device_find_locked(domain, bus, slot, func);
183 pcistub_device_get(psdev);
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
189 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
190 struct pcistub_device *psdev)
192 struct pci_dev *pci_dev = NULL;
195 pcistub_device_get(psdev);
197 spin_lock_irqsave(&psdev->lock, flags);
200 pci_dev = psdev->dev;
202 spin_unlock_irqrestore(&psdev->lock, flags);
205 pcistub_device_put(psdev);
210 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
214 struct pcistub_device *psdev;
215 struct pci_dev *found_dev = NULL;
218 spin_lock_irqsave(&pcistub_devices_lock, flags);
220 psdev = pcistub_device_find_locked(domain, bus, slot, func);
222 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
228 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
231 struct pcistub_device *psdev;
232 struct pci_dev *found_dev = NULL;
235 spin_lock_irqsave(&pcistub_devices_lock, flags);
237 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 if (psdev->dev == dev) {
239 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
244 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
250 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
255 * As such we have to be careful.
257 * To make this easier, the caller has to hold the device lock.
259 void pcistub_put_pci_dev(struct pci_dev *dev)
261 struct pcistub_device *psdev, *found_psdev = NULL;
263 struct xen_pcibk_dev_data *dev_data;
266 spin_lock_irqsave(&pcistub_devices_lock, flags);
268 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 if (psdev->dev == dev) {
275 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
276 if (WARN_ON(!found_psdev))
279 /*hold this lock for avoiding breaking link between
280 * pcistub and xen_pcibk when AER is in processing
282 down_write(&pcistub_sem);
283 /* Cleanup our device
284 * (so it's ready for the next domain)
286 device_lock_assert(&dev->dev);
287 __pci_reset_function_locked(dev);
289 dev_data = pci_get_drvdata(dev);
290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
293 * The usual sequence is pci_save_state & pci_restore_state
294 * but the guest might have messed the configuration space up.
295 * Use the initial version (when device was bound to us).
297 pci_restore_state(dev);
299 dev_info(&dev->dev, "Could not reload PCI state\n");
300 /* This disables the device. */
301 xen_pcibk_reset_device(dev);
303 /* And cleanup up our emulated fields. */
304 xen_pcibk_config_reset_dev(dev);
305 xen_pcibk_config_free_dyn_fields(dev);
307 dev_data->allow_interrupt_control = 0;
309 xen_unregister_device_domain_owner(dev);
311 spin_lock_irqsave(&found_psdev->lock, flags);
312 found_psdev->pdev = NULL;
313 spin_unlock_irqrestore(&found_psdev->lock, flags);
315 pcistub_device_put(found_psdev);
316 up_write(&pcistub_sem);
319 static int pcistub_match_one(struct pci_dev *dev,
320 struct pcistub_device_id *pdev_id)
322 /* Match the specified device by domain, bus, slot, func and also if
323 * any of the device's parent bridges match.
325 for (; dev != NULL; dev = dev->bus->self) {
326 if (pci_domain_nr(dev->bus) == pdev_id->domain
327 && dev->bus->number == pdev_id->bus
328 && dev->devfn == pdev_id->devfn)
331 /* Sometimes topmost bridge links to itself. */
332 if (dev == dev->bus->self)
339 static int pcistub_match(struct pci_dev *dev)
341 struct pcistub_device_id *pdev_id;
345 spin_lock_irqsave(&device_ids_lock, flags);
346 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
347 if (pcistub_match_one(dev, pdev_id)) {
352 spin_unlock_irqrestore(&device_ids_lock, flags);
357 static int pcistub_init_device(struct pci_dev *dev)
359 struct xen_pcibk_dev_data *dev_data;
362 dev_dbg(&dev->dev, "initializing...\n");
364 /* The PCI backend is not intended to be a module (or to work with
365 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
366 * would need to be called somewhere to free the memory allocated
367 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
369 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
370 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
375 pci_set_drvdata(dev, dev_data);
378 * Setup name for fake IRQ handler. It will only be enabled
379 * once the device is turned on by the guest.
381 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
383 dev_dbg(&dev->dev, "initializing config\n");
385 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
386 err = xen_pcibk_config_init_dev(dev);
390 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
391 * must do this here because pcibios_enable_device may specify
392 * the pci device's true irq (and possibly its other resources)
393 * if they differ from what's in the configuration space.
394 * This makes the assumption that the device's resources won't
395 * change after this point (otherwise this code may break!)
397 dev_dbg(&dev->dev, "enabling device\n");
398 err = pci_enable_device(dev);
403 struct physdev_pci_device ppdev = {
404 .seg = pci_domain_nr(dev->bus),
405 .bus = dev->bus->number,
409 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
410 if (err && err != -ENOSYS)
411 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
415 /* We need the device active to save the state. */
416 dev_dbg(&dev->dev, "save state of device\n");
418 dev_data->pci_saved_state = pci_store_saved_state(dev);
419 if (!dev_data->pci_saved_state)
420 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
422 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
423 __pci_reset_function_locked(dev);
424 pci_restore_state(dev);
426 /* Now disable the device (this also ensures some private device
427 * data is setup before we export)
429 dev_dbg(&dev->dev, "reset device\n");
430 xen_pcibk_reset_device(dev);
432 pci_set_dev_assigned(dev);
436 xen_pcibk_config_free_dev(dev);
439 pci_set_drvdata(dev, NULL);
445 * Because some initialization still happens on
446 * devices during fs_initcall, we need to defer
447 * full initialization of our devices until
450 static int __init pcistub_init_devices_late(void)
452 struct pcistub_device *psdev;
456 spin_lock_irqsave(&pcistub_devices_lock, flags);
458 while (!list_empty(&seized_devices)) {
459 psdev = container_of(seized_devices.next,
460 struct pcistub_device, dev_list);
461 list_del(&psdev->dev_list);
463 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
465 err = pcistub_init_device(psdev->dev);
467 dev_err(&psdev->dev->dev,
468 "error %d initializing device\n", err);
473 spin_lock_irqsave(&pcistub_devices_lock, flags);
476 list_add_tail(&psdev->dev_list, &pcistub_devices);
479 initialize_devices = 1;
481 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
486 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
487 int domain, int bus, unsigned int devfn)
489 struct pcistub_device_id *pci_dev_id;
493 spin_lock_irqsave(&device_ids_lock, flags);
495 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
496 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
497 pci_dev_id->devfn == devfn) {
504 new->domain = domain;
507 list_add_tail(&new->slot_list, &pcistub_device_ids);
510 spin_unlock_irqrestore(&device_ids_lock, flags);
516 static int pcistub_seize(struct pci_dev *dev,
517 struct pcistub_device_id *pci_dev_id)
519 struct pcistub_device *psdev;
523 psdev = pcistub_device_alloc(dev);
529 spin_lock_irqsave(&pcistub_devices_lock, flags);
531 if (initialize_devices) {
532 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
534 /* don't want irqs disabled when calling pcistub_init_device */
535 err = pcistub_init_device(psdev->dev);
537 spin_lock_irqsave(&pcistub_devices_lock, flags);
540 list_add(&psdev->dev_list, &pcistub_devices);
542 dev_dbg(&dev->dev, "deferring initialization\n");
543 list_add(&psdev->dev_list, &seized_devices);
546 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
550 pcistub_device_put(psdev);
551 } else if (pci_dev_id)
552 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
553 dev->bus->number, dev->devfn);
558 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
559 * other functions that take the sysfs lock. */
560 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
563 struct pcistub_device_id *pci_dev_id = NULL;
565 dev_dbg(&dev->dev, "probing...\n");
567 match = pcistub_match(dev);
569 if ((dev->driver_override &&
570 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
573 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
574 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
575 dev_err(&dev->dev, "can't export pci devices that "
576 "don't have a normal (0) or bridge (1) "
583 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
590 dev_info(&dev->dev, "seizing device\n");
591 err = pcistub_seize(dev, pci_dev_id);
593 /* Didn't find the device */
600 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
601 * other functions that take the sysfs lock. */
602 static void pcistub_remove(struct pci_dev *dev)
604 struct pcistub_device *psdev, *found_psdev = NULL;
607 dev_dbg(&dev->dev, "removing\n");
609 spin_lock_irqsave(&pcistub_devices_lock, flags);
611 xen_pcibk_config_quirk_release(dev);
613 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
614 if (psdev->dev == dev) {
620 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
623 dev_dbg(&dev->dev, "found device to remove %s\n",
624 found_psdev->pdev ? "- in-use" : "");
626 if (found_psdev->pdev) {
627 int domid = xen_find_device_domain_owner(dev);
629 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
630 pci_name(found_psdev->dev), domid);
631 pr_warn("****** driver domain may still access this device's i/o resources!\n");
632 pr_warn("****** shutdown driver domain before binding device\n");
633 pr_warn("****** to other drivers or domains\n");
635 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
637 xen_pcibk_release_pci_dev(found_psdev->pdev,
639 false /* caller holds the lock. */);
642 spin_lock_irqsave(&pcistub_devices_lock, flags);
643 list_del(&found_psdev->dev_list);
644 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
646 /* the final put for releasing from the list */
647 pcistub_device_put(found_psdev);
651 static const struct pci_device_id pcistub_ids[] = {
653 .vendor = PCI_ANY_ID,
654 .device = PCI_ANY_ID,
655 .subvendor = PCI_ANY_ID,
656 .subdevice = PCI_ANY_ID,
661 #define PCI_NODENAME_MAX 40
662 static void kill_domain_by_device(struct pcistub_device *psdev)
664 struct xenbus_transaction xbt;
666 char nodename[PCI_NODENAME_MAX];
669 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
670 psdev->pdev->xdev->otherend_id);
673 err = xenbus_transaction_start(&xbt);
675 dev_err(&psdev->dev->dev,
676 "error %d when start xenbus transaction\n", err);
679 /*PV AER handlers will set this flag*/
680 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
681 err = xenbus_transaction_end(xbt, 0);
685 dev_err(&psdev->dev->dev,
686 "error %d when end xenbus transaction\n", err);
691 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
692 * backend need to have cooperation. In xen_pcibk, those steps will do similar
693 * jobs: send service request and waiting for front_end response.
695 static pci_ers_result_t common_process(struct pcistub_device *psdev,
696 pci_channel_state_t state, int aer_cmd,
697 pci_ers_result_t result)
699 pci_ers_result_t res = result;
700 struct xen_pcie_aer_op *aer_op;
701 struct xen_pcibk_device *pdev = psdev->pdev;
702 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
705 /*with PV AER drivers*/
706 aer_op = &(sh_info->aer_op);
707 aer_op->cmd = aer_cmd ;
708 /*useful for error_detected callback*/
711 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
712 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
714 dev_err(&psdev->dev->dev,
715 DRV_NAME ": failed to get pcifront device\n");
716 return PCI_ERS_RESULT_NONE;
720 dev_dbg(&psdev->dev->dev,
721 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
722 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
723 /*local flag to mark there's aer request, xen_pcibk callback will use
724 * this flag to judge whether we need to check pci-front give aer
727 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
729 /*It is possible that a pcifront conf_read_write ops request invokes
730 * the callback which cause the spurious execution of wake_up.
731 * Yet it is harmless and better than a spinlock here
733 set_bit(_XEN_PCIB_active,
734 (unsigned long *)&sh_info->flags);
736 notify_remote_via_irq(pdev->evtchn_irq);
738 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
739 !(test_bit(_XEN_PCIB_active, (unsigned long *)
740 &sh_info->flags)), 300*HZ);
743 if (test_bit(_XEN_PCIB_active,
744 (unsigned long *)&sh_info->flags)) {
745 dev_err(&psdev->dev->dev,
746 "pcifront aer process not responding!\n");
747 clear_bit(_XEN_PCIB_active,
748 (unsigned long *)&sh_info->flags);
749 aer_op->err = PCI_ERS_RESULT_NONE;
753 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
755 if (test_bit(_XEN_PCIF_active,
756 (unsigned long *)&sh_info->flags)) {
757 dev_dbg(&psdev->dev->dev,
758 "schedule pci_conf service in " DRV_NAME "\n");
759 xen_pcibk_test_and_schedule_op(psdev->pdev);
762 res = (pci_ers_result_t)aer_op->err;
767 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
768 * of the device driver could provide this service, and then wait for pcifront
770 * @dev: pointer to PCI devices
771 * return value is used by aer_core do_recovery policy
773 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
775 struct pcistub_device *psdev;
776 pci_ers_result_t result;
778 result = PCI_ERS_RESULT_RECOVERED;
779 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
780 dev->bus->number, dev->devfn);
782 down_write(&pcistub_sem);
783 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
785 PCI_SLOT(dev->devfn),
786 PCI_FUNC(dev->devfn));
788 if (!psdev || !psdev->pdev) {
790 DRV_NAME " device is not found/assigned\n");
794 if (!psdev->pdev->sh_info) {
795 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
796 " by HVM, kill it\n");
797 kill_domain_by_device(psdev);
801 if (!test_bit(_XEN_PCIB_AERHANDLER,
802 (unsigned long *)&psdev->pdev->sh_info->flags)) {
804 "guest with no AER driver should have been killed\n");
807 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
809 if (result == PCI_ERS_RESULT_NONE ||
810 result == PCI_ERS_RESULT_DISCONNECT) {
812 "No AER slot_reset service or disconnected!\n");
813 kill_domain_by_device(psdev);
817 pcistub_device_put(psdev);
818 up_write(&pcistub_sem);
824 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
825 * in case of the device driver could provide this service, and then wait
827 * @dev: pointer to PCI devices
828 * return value is used by aer_core do_recovery policy
831 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
833 struct pcistub_device *psdev;
834 pci_ers_result_t result;
836 result = PCI_ERS_RESULT_RECOVERED;
837 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
838 dev->bus->number, dev->devfn);
840 down_write(&pcistub_sem);
841 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
843 PCI_SLOT(dev->devfn),
844 PCI_FUNC(dev->devfn));
846 if (!psdev || !psdev->pdev) {
848 DRV_NAME " device is not found/assigned\n");
852 if (!psdev->pdev->sh_info) {
853 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
854 " by HVM, kill it\n");
855 kill_domain_by_device(psdev);
859 if (!test_bit(_XEN_PCIB_AERHANDLER,
860 (unsigned long *)&psdev->pdev->sh_info->flags)) {
862 "guest with no AER driver should have been killed\n");
865 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
867 if (result == PCI_ERS_RESULT_NONE ||
868 result == PCI_ERS_RESULT_DISCONNECT) {
870 "No AER mmio_enabled service or disconnected!\n");
871 kill_domain_by_device(psdev);
875 pcistub_device_put(psdev);
876 up_write(&pcistub_sem);
880 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
881 * in case of the device driver could provide this service, and then wait
883 * @dev: pointer to PCI devices
884 * @error: the current PCI connection state
885 * return value is used by aer_core do_recovery policy
888 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
889 pci_channel_state_t error)
891 struct pcistub_device *psdev;
892 pci_ers_result_t result;
894 result = PCI_ERS_RESULT_CAN_RECOVER;
895 dev_dbg(&dev->dev, "xen_pcibk_error_detected(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 /*Guest owns the device yet no aer handler regiested, kill guest*/
918 if (!test_bit(_XEN_PCIB_AERHANDLER,
919 (unsigned long *)&psdev->pdev->sh_info->flags)) {
920 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
921 kill_domain_by_device(psdev);
924 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
926 if (result == PCI_ERS_RESULT_NONE ||
927 result == PCI_ERS_RESULT_DISCONNECT) {
929 "No AER error_detected service or disconnected!\n");
930 kill_domain_by_device(psdev);
934 pcistub_device_put(psdev);
935 up_write(&pcistub_sem);
939 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
940 * in case of the device driver could provide this service, and then wait
942 * @dev: pointer to PCI devices
945 static void xen_pcibk_error_resume(struct pci_dev *dev)
947 struct pcistub_device *psdev;
949 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
950 dev->bus->number, dev->devfn);
952 down_write(&pcistub_sem);
953 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
955 PCI_SLOT(dev->devfn),
956 PCI_FUNC(dev->devfn));
958 if (!psdev || !psdev->pdev) {
960 DRV_NAME " device is not found/assigned\n");
964 if (!psdev->pdev->sh_info) {
965 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
966 " by HVM, kill it\n");
967 kill_domain_by_device(psdev);
971 if (!test_bit(_XEN_PCIB_AERHANDLER,
972 (unsigned long *)&psdev->pdev->sh_info->flags)) {
974 "guest with no AER driver should have been killed\n");
975 kill_domain_by_device(psdev);
978 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
979 PCI_ERS_RESULT_RECOVERED);
982 pcistub_device_put(psdev);
983 up_write(&pcistub_sem);
987 /*add xen_pcibk AER handling*/
988 static const struct pci_error_handlers xen_pcibk_error_handler = {
989 .error_detected = xen_pcibk_error_detected,
990 .mmio_enabled = xen_pcibk_mmio_enabled,
991 .slot_reset = xen_pcibk_slot_reset,
992 .resume = xen_pcibk_error_resume,
996 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
997 * for a normal device. I don't want it to be loaded automatically.
1000 static struct pci_driver xen_pcibk_pci_driver = {
1001 /* The name should be xen_pciback, but until the tools are updated
1002 * we will keep it as pciback. */
1003 .name = PCISTUB_DRIVER_NAME,
1004 .id_table = pcistub_ids,
1005 .probe = pcistub_probe,
1006 .remove = pcistub_remove,
1007 .err_handler = &xen_pcibk_error_handler,
1010 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1011 int *slot, int *func)
1015 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1019 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1023 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1026 if (parsed && !buf[parsed])
1029 /* try again without domain */
1031 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1034 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1038 sscanf(buf, " %x:*.* %n", bus, &parsed);
1041 if (parsed && !buf[parsed])
1047 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1048 *slot, int *func, int *reg, int *size, int *mask)
1052 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1053 reg, size, mask, &parsed);
1054 if (parsed && !buf[parsed])
1057 /* try again without domain */
1059 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1061 if (parsed && !buf[parsed])
1067 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1069 struct pcistub_device_id *pci_dev_id;
1070 int rc = 0, devfn = PCI_DEVFN(slot, func);
1073 for (slot = 0; !rc && slot < 32; ++slot)
1074 rc = pcistub_device_id_add(domain, bus, slot, func);
1079 for (func = 0; !rc && func < 8; ++func)
1080 rc = pcistub_device_id_add(domain, bus, slot, func);
1085 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1086 || !defined(CONFIG_PCI_DOMAINS)
1087 !pci_domains_supported ? domain :
1089 domain < 0 || domain > 0xffff)
1090 || bus < 0 || bus > 0xff
1091 || PCI_SLOT(devfn) != slot
1092 || PCI_FUNC(devfn) != func)
1095 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1099 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1100 domain, bus, slot, func);
1102 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1107 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1109 struct pcistub_device_id *pci_dev_id, *t;
1111 unsigned long flags;
1113 spin_lock_irqsave(&device_ids_lock, flags);
1114 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1116 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1117 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1118 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1119 /* Don't break; here because it's possible the same
1120 * slot could be in the list more than once
1122 list_del(&pci_dev_id->slot_list);
1127 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1128 domain, bus, slot, func);
1131 spin_unlock_irqrestore(&device_ids_lock, flags);
1136 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1137 unsigned int reg, unsigned int size,
1141 struct pcistub_device *psdev;
1142 struct pci_dev *dev;
1143 struct config_field *field;
1145 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1148 psdev = pcistub_device_find(domain, bus, slot, func);
1155 field = kzalloc(sizeof(*field), GFP_KERNEL);
1161 field->offset = reg;
1165 field->reset = NULL;
1166 field->release = NULL;
1167 field->clean = xen_pcibk_config_field_free;
1169 err = xen_pcibk_config_quirks_add_field(dev, field);
1174 pcistub_device_put(psdev);
1178 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1181 int domain, bus, slot, func;
1184 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1188 err = pcistub_device_id_add(domain, bus, slot, func);
1195 static DRIVER_ATTR_WO(new_slot);
1197 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1200 int domain, bus, slot, func;
1203 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1207 err = pcistub_device_id_remove(domain, bus, slot, func);
1214 static DRIVER_ATTR_WO(remove_slot);
1216 static ssize_t slots_show(struct device_driver *drv, char *buf)
1218 struct pcistub_device_id *pci_dev_id;
1220 unsigned long flags;
1222 spin_lock_irqsave(&device_ids_lock, flags);
1223 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1224 if (count >= PAGE_SIZE)
1227 count += scnprintf(buf + count, PAGE_SIZE - count,
1228 "%04x:%02x:%02x.%d\n",
1229 pci_dev_id->domain, pci_dev_id->bus,
1230 PCI_SLOT(pci_dev_id->devfn),
1231 PCI_FUNC(pci_dev_id->devfn));
1233 spin_unlock_irqrestore(&device_ids_lock, flags);
1237 static DRIVER_ATTR_RO(slots);
1239 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1241 struct pcistub_device *psdev;
1242 struct xen_pcibk_dev_data *dev_data;
1244 unsigned long flags;
1246 spin_lock_irqsave(&pcistub_devices_lock, flags);
1247 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1248 if (count >= PAGE_SIZE)
1252 dev_data = pci_get_drvdata(psdev->dev);
1256 scnprintf(buf + count, PAGE_SIZE - count,
1257 "%s:%s:%sing:%ld\n",
1258 pci_name(psdev->dev),
1259 dev_data->isr_on ? "on" : "off",
1260 dev_data->ack_intr ? "ack" : "not ack",
1263 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1266 static DRIVER_ATTR_RO(irq_handlers);
1268 static ssize_t irq_handler_state_store(struct device_driver *drv,
1269 const char *buf, size_t count)
1271 struct pcistub_device *psdev;
1272 struct xen_pcibk_dev_data *dev_data;
1273 int domain, bus, slot, func;
1276 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1280 psdev = pcistub_device_find(domain, bus, slot, func);
1286 dev_data = pci_get_drvdata(psdev->dev);
1292 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1293 dev_data->irq_name, dev_data->isr_on,
1296 dev_data->isr_on = !(dev_data->isr_on);
1297 if (dev_data->isr_on)
1298 dev_data->ack_intr = 1;
1301 pcistub_device_put(psdev);
1306 static DRIVER_ATTR_WO(irq_handler_state);
1308 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1311 int domain, bus, slot, func, reg, size, mask;
1314 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1319 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1327 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1330 unsigned long flags;
1331 struct xen_pcibk_config_quirk *quirk;
1332 struct xen_pcibk_dev_data *dev_data;
1333 const struct config_field *field;
1334 const struct config_field_entry *cfg_entry;
1336 spin_lock_irqsave(&device_ids_lock, flags);
1337 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1338 if (count >= PAGE_SIZE)
1341 count += scnprintf(buf + count, PAGE_SIZE - count,
1342 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1343 quirk->pdev->bus->number,
1344 PCI_SLOT(quirk->pdev->devfn),
1345 PCI_FUNC(quirk->pdev->devfn),
1346 quirk->devid.vendor, quirk->devid.device,
1347 quirk->devid.subvendor,
1348 quirk->devid.subdevice);
1350 dev_data = pci_get_drvdata(quirk->pdev);
1352 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1353 field = cfg_entry->field;
1354 if (count >= PAGE_SIZE)
1357 count += scnprintf(buf + count, PAGE_SIZE - count,
1358 "\t\t%08x:%01x:%08x\n",
1359 cfg_entry->base_offset +
1360 field->offset, field->size,
1366 spin_unlock_irqrestore(&device_ids_lock, flags);
1370 static DRIVER_ATTR_RW(quirks);
1372 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1375 int domain, bus, slot, func;
1377 struct pcistub_device *psdev;
1378 struct xen_pcibk_dev_data *dev_data;
1380 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1384 psdev = pcistub_device_find(domain, bus, slot, func);
1390 dev_data = pci_get_drvdata(psdev->dev);
1391 /* the driver data for a device should never be null at this point */
1396 if (!dev_data->permissive) {
1397 dev_data->permissive = 1;
1398 /* Let user know that what they're doing could be unsafe */
1399 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1400 "configuration space accesses!\n");
1401 dev_warn(&psdev->dev->dev,
1402 "permissive mode is potentially unsafe!\n");
1405 pcistub_device_put(psdev);
1412 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1414 struct pcistub_device *psdev;
1415 struct xen_pcibk_dev_data *dev_data;
1417 unsigned long flags;
1418 spin_lock_irqsave(&pcistub_devices_lock, flags);
1419 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1420 if (count >= PAGE_SIZE)
1424 dev_data = pci_get_drvdata(psdev->dev);
1425 if (!dev_data || !dev_data->permissive)
1428 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1429 pci_name(psdev->dev));
1431 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1434 static DRIVER_ATTR_RW(permissive);
1436 static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1437 const char *buf, size_t count)
1439 int domain, bus, slot, func;
1441 struct pcistub_device *psdev;
1442 struct xen_pcibk_dev_data *dev_data;
1444 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1448 psdev = pcistub_device_find(domain, bus, slot, func);
1454 dev_data = pci_get_drvdata(psdev->dev);
1455 /* the driver data for a device should never be null at this point */
1460 dev_data->allow_interrupt_control = 1;
1462 pcistub_device_put(psdev);
1469 static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1472 struct pcistub_device *psdev;
1473 struct xen_pcibk_dev_data *dev_data;
1475 unsigned long flags;
1477 spin_lock_irqsave(&pcistub_devices_lock, flags);
1478 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1479 if (count >= PAGE_SIZE)
1483 dev_data = pci_get_drvdata(psdev->dev);
1484 if (!dev_data || !dev_data->allow_interrupt_control)
1487 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1488 pci_name(psdev->dev));
1490 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1493 static DRIVER_ATTR_RW(allow_interrupt_control);
1495 static void pcistub_exit(void)
1497 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_remove_slot);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1501 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_permissive);
1504 driver_remove_file(&xen_pcibk_pci_driver.driver,
1505 &driver_attr_allow_interrupt_control);
1506 driver_remove_file(&xen_pcibk_pci_driver.driver,
1507 &driver_attr_irq_handlers);
1508 driver_remove_file(&xen_pcibk_pci_driver.driver,
1509 &driver_attr_irq_handler_state);
1510 pci_unregister_driver(&xen_pcibk_pci_driver);
1513 static int __init pcistub_init(void)
1517 int domain, bus, slot, func;
1520 if (pci_devs_to_hide && *pci_devs_to_hide) {
1524 err = sscanf(pci_devs_to_hide + pos,
1525 " (%x:%x:%x.%x) %n",
1526 &domain, &bus, &slot, &func, &parsed);
1530 sscanf(pci_devs_to_hide + pos,
1532 &domain, &bus, &slot, &parsed);
1536 sscanf(pci_devs_to_hide + pos,
1538 &domain, &bus, &parsed);
1544 err = sscanf(pci_devs_to_hide + pos,
1546 &bus, &slot, &func, &parsed);
1550 sscanf(pci_devs_to_hide + pos,
1552 &bus, &slot, &parsed);
1556 sscanf(pci_devs_to_hide + pos,
1566 err = pcistub_device_id_add(domain, bus, slot, func);
1571 } while (pci_devs_to_hide[pos]);
1574 /* If we're the first PCI Device Driver to register, we're the
1575 * first one to get offered PCI devices as they become
1576 * available (and thus we can be the first to grab them)
1578 err = pci_register_driver(&xen_pcibk_pci_driver);
1582 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1583 &driver_attr_new_slot);
1585 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1586 &driver_attr_remove_slot);
1588 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1589 &driver_attr_slots);
1591 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1592 &driver_attr_quirks);
1594 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1595 &driver_attr_permissive);
1597 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1598 &driver_attr_allow_interrupt_control);
1601 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1602 &driver_attr_irq_handlers);
1604 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1605 &driver_attr_irq_handler_state);
1613 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1614 pci_devs_to_hide + pos);
1620 * fs_initcall happens before device_initcall
1621 * so xen_pcibk *should* get called first (b/c we
1622 * want to suck up any device before other drivers
1623 * get a chance by being the first pci device
1624 * driver to register)
1626 fs_initcall(pcistub_init);
1629 #ifdef CONFIG_PCI_IOV
1630 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1632 struct pcistub_device *psdev = NULL;
1633 unsigned long flags;
1636 spin_lock_irqsave(&pcistub_devices_lock, flags);
1637 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1638 if (!psdev->pdev && psdev->dev != pdev
1639 && pci_physfn(psdev->dev) == pdev) {
1644 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1650 static int pci_stub_notifier(struct notifier_block *nb,
1651 unsigned long action, void *data)
1653 struct device *dev = data;
1654 const struct pci_dev *pdev = to_pci_dev(dev);
1656 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1659 if (!pdev->is_physfn)
1663 struct pcistub_device *psdev = find_vfs(pdev);
1666 device_release_driver(&psdev->dev->dev);
1671 static struct notifier_block pci_stub_nb = {
1672 .notifier_call = pci_stub_notifier,
1676 static int __init xen_pcibk_init(void)
1680 if (!xen_initial_domain())
1683 err = xen_pcibk_config_init();
1688 err = pcistub_init();
1693 pcistub_init_devices_late();
1694 err = xen_pcibk_xenbus_register();
1697 #ifdef CONFIG_PCI_IOV
1699 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1705 static void __exit xen_pcibk_cleanup(void)
1707 #ifdef CONFIG_PCI_IOV
1708 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1710 xen_pcibk_xenbus_unregister();
1714 module_init(xen_pcibk_init);
1715 module_exit(xen_pcibk_cleanup);
1717 MODULE_LICENSE("Dual BSD/GPL");
1718 MODULE_ALIAS("xen-backend:pci");