2 * PCI Stub Driver - Grabs devices in backend to be exported later
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 xen_unregister_device_domain_owner(dev);
309 spin_lock_irqsave(&found_psdev->lock, flags);
310 found_psdev->pdev = NULL;
311 spin_unlock_irqrestore(&found_psdev->lock, flags);
313 pcistub_device_put(found_psdev);
314 up_write(&pcistub_sem);
317 static int pcistub_match_one(struct pci_dev *dev,
318 struct pcistub_device_id *pdev_id)
320 /* Match the specified device by domain, bus, slot, func and also if
321 * any of the device's parent bridges match.
323 for (; dev != NULL; dev = dev->bus->self) {
324 if (pci_domain_nr(dev->bus) == pdev_id->domain
325 && dev->bus->number == pdev_id->bus
326 && dev->devfn == pdev_id->devfn)
329 /* Sometimes topmost bridge links to itself. */
330 if (dev == dev->bus->self)
337 static int pcistub_match(struct pci_dev *dev)
339 struct pcistub_device_id *pdev_id;
343 spin_lock_irqsave(&device_ids_lock, flags);
344 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
345 if (pcistub_match_one(dev, pdev_id)) {
350 spin_unlock_irqrestore(&device_ids_lock, flags);
355 static int pcistub_init_device(struct pci_dev *dev)
357 struct xen_pcibk_dev_data *dev_data;
360 dev_dbg(&dev->dev, "initializing...\n");
362 /* The PCI backend is not intended to be a module (or to work with
363 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
364 * would need to be called somewhere to free the memory allocated
365 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
367 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
368 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
373 pci_set_drvdata(dev, dev_data);
376 * Setup name for fake IRQ handler. It will only be enabled
377 * once the device is turned on by the guest.
379 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
381 dev_dbg(&dev->dev, "initializing config\n");
383 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
384 err = xen_pcibk_config_init_dev(dev);
388 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
389 * must do this here because pcibios_enable_device may specify
390 * the pci device's true irq (and possibly its other resources)
391 * if they differ from what's in the configuration space.
392 * This makes the assumption that the device's resources won't
393 * change after this point (otherwise this code may break!)
395 dev_dbg(&dev->dev, "enabling device\n");
396 err = pci_enable_device(dev);
401 struct physdev_pci_device ppdev = {
402 .seg = pci_domain_nr(dev->bus),
403 .bus = dev->bus->number,
407 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
408 if (err && err != -ENOSYS)
409 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
413 /* We need the device active to save the state. */
414 dev_dbg(&dev->dev, "save state of device\n");
416 dev_data->pci_saved_state = pci_store_saved_state(dev);
417 if (!dev_data->pci_saved_state)
418 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
420 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
421 __pci_reset_function_locked(dev);
422 pci_restore_state(dev);
424 /* Now disable the device (this also ensures some private device
425 * data is setup before we export)
427 dev_dbg(&dev->dev, "reset device\n");
428 xen_pcibk_reset_device(dev);
430 pci_set_dev_assigned(dev);
434 xen_pcibk_config_free_dev(dev);
437 pci_set_drvdata(dev, NULL);
443 * Because some initialization still happens on
444 * devices during fs_initcall, we need to defer
445 * full initialization of our devices until
448 static int __init pcistub_init_devices_late(void)
450 struct pcistub_device *psdev;
454 spin_lock_irqsave(&pcistub_devices_lock, flags);
456 while (!list_empty(&seized_devices)) {
457 psdev = container_of(seized_devices.next,
458 struct pcistub_device, dev_list);
459 list_del(&psdev->dev_list);
461 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
463 err = pcistub_init_device(psdev->dev);
465 dev_err(&psdev->dev->dev,
466 "error %d initializing device\n", err);
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
474 list_add_tail(&psdev->dev_list, &pcistub_devices);
477 initialize_devices = 1;
479 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
484 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
485 int domain, int bus, unsigned int devfn)
487 struct pcistub_device_id *pci_dev_id;
491 spin_lock_irqsave(&device_ids_lock, flags);
493 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
494 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
495 pci_dev_id->devfn == devfn) {
502 new->domain = domain;
505 list_add_tail(&new->slot_list, &pcistub_device_ids);
508 spin_unlock_irqrestore(&device_ids_lock, flags);
514 static int pcistub_seize(struct pci_dev *dev,
515 struct pcistub_device_id *pci_dev_id)
517 struct pcistub_device *psdev;
521 psdev = pcistub_device_alloc(dev);
527 spin_lock_irqsave(&pcistub_devices_lock, flags);
529 if (initialize_devices) {
530 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
532 /* don't want irqs disabled when calling pcistub_init_device */
533 err = pcistub_init_device(psdev->dev);
535 spin_lock_irqsave(&pcistub_devices_lock, flags);
538 list_add(&psdev->dev_list, &pcistub_devices);
540 dev_dbg(&dev->dev, "deferring initialization\n");
541 list_add(&psdev->dev_list, &seized_devices);
544 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
548 pcistub_device_put(psdev);
549 } else if (pci_dev_id)
550 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
551 dev->bus->number, dev->devfn);
556 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
557 * other functions that take the sysfs lock. */
558 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
561 struct pcistub_device_id *pci_dev_id = NULL;
563 dev_dbg(&dev->dev, "probing...\n");
565 match = pcistub_match(dev);
567 if ((dev->driver_override &&
568 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
571 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
572 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
573 dev_err(&dev->dev, "can't export pci devices that "
574 "don't have a normal (0) or bridge (1) "
581 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
588 dev_info(&dev->dev, "seizing device\n");
589 err = pcistub_seize(dev, pci_dev_id);
591 /* Didn't find the device */
598 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
599 * other functions that take the sysfs lock. */
600 static void pcistub_remove(struct pci_dev *dev)
602 struct pcistub_device *psdev, *found_psdev = NULL;
605 dev_dbg(&dev->dev, "removing\n");
607 spin_lock_irqsave(&pcistub_devices_lock, flags);
609 xen_pcibk_config_quirk_release(dev);
611 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
612 if (psdev->dev == dev) {
618 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
621 dev_dbg(&dev->dev, "found device to remove %s\n",
622 found_psdev->pdev ? "- in-use" : "");
624 if (found_psdev->pdev) {
625 int domid = xen_find_device_domain_owner(dev);
627 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
628 pci_name(found_psdev->dev), domid);
629 pr_warn("****** driver domain may still access this device's i/o resources!\n");
630 pr_warn("****** shutdown driver domain before binding device\n");
631 pr_warn("****** to other drivers or domains\n");
633 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
635 xen_pcibk_release_pci_dev(found_psdev->pdev,
637 false /* caller holds the lock. */);
640 spin_lock_irqsave(&pcistub_devices_lock, flags);
641 list_del(&found_psdev->dev_list);
642 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
644 /* the final put for releasing from the list */
645 pcistub_device_put(found_psdev);
649 static const struct pci_device_id pcistub_ids[] = {
651 .vendor = PCI_ANY_ID,
652 .device = PCI_ANY_ID,
653 .subvendor = PCI_ANY_ID,
654 .subdevice = PCI_ANY_ID,
659 #define PCI_NODENAME_MAX 40
660 static void kill_domain_by_device(struct pcistub_device *psdev)
662 struct xenbus_transaction xbt;
664 char nodename[PCI_NODENAME_MAX];
667 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
668 psdev->pdev->xdev->otherend_id);
671 err = xenbus_transaction_start(&xbt);
673 dev_err(&psdev->dev->dev,
674 "error %d when start xenbus transaction\n", err);
677 /*PV AER handlers will set this flag*/
678 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
679 err = xenbus_transaction_end(xbt, 0);
683 dev_err(&psdev->dev->dev,
684 "error %d when end xenbus transaction\n", err);
689 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
690 * backend need to have cooperation. In xen_pcibk, those steps will do similar
691 * jobs: send service request and waiting for front_end response.
693 static pci_ers_result_t common_process(struct pcistub_device *psdev,
694 pci_channel_state_t state, int aer_cmd,
695 pci_ers_result_t result)
697 pci_ers_result_t res = result;
698 struct xen_pcie_aer_op *aer_op;
699 struct xen_pcibk_device *pdev = psdev->pdev;
700 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
703 /*with PV AER drivers*/
704 aer_op = &(sh_info->aer_op);
705 aer_op->cmd = aer_cmd ;
706 /*useful for error_detected callback*/
709 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
710 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
712 dev_err(&psdev->dev->dev,
713 DRV_NAME ": failed to get pcifront device\n");
714 return PCI_ERS_RESULT_NONE;
718 dev_dbg(&psdev->dev->dev,
719 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
720 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
721 /*local flag to mark there's aer request, xen_pcibk callback will use
722 * this flag to judge whether we need to check pci-front give aer
725 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
727 /*It is possible that a pcifront conf_read_write ops request invokes
728 * the callback which cause the spurious execution of wake_up.
729 * Yet it is harmless and better than a spinlock here
731 set_bit(_XEN_PCIB_active,
732 (unsigned long *)&sh_info->flags);
734 notify_remote_via_irq(pdev->evtchn_irq);
736 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
737 !(test_bit(_XEN_PCIB_active, (unsigned long *)
738 &sh_info->flags)), 300*HZ);
741 if (test_bit(_XEN_PCIB_active,
742 (unsigned long *)&sh_info->flags)) {
743 dev_err(&psdev->dev->dev,
744 "pcifront aer process not responding!\n");
745 clear_bit(_XEN_PCIB_active,
746 (unsigned long *)&sh_info->flags);
747 aer_op->err = PCI_ERS_RESULT_NONE;
751 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
753 if (test_bit(_XEN_PCIF_active,
754 (unsigned long *)&sh_info->flags)) {
755 dev_dbg(&psdev->dev->dev,
756 "schedule pci_conf service in " DRV_NAME "\n");
757 xen_pcibk_test_and_schedule_op(psdev->pdev);
760 res = (pci_ers_result_t)aer_op->err;
765 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
766 * of the device driver could provide this service, and then wait for pcifront
768 * @dev: pointer to PCI devices
769 * return value is used by aer_core do_recovery policy
771 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
773 struct pcistub_device *psdev;
774 pci_ers_result_t result;
776 result = PCI_ERS_RESULT_RECOVERED;
777 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
778 dev->bus->number, dev->devfn);
780 down_write(&pcistub_sem);
781 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
783 PCI_SLOT(dev->devfn),
784 PCI_FUNC(dev->devfn));
786 if (!psdev || !psdev->pdev) {
788 DRV_NAME " device is not found/assigned\n");
792 if (!psdev->pdev->sh_info) {
793 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
794 " by HVM, kill it\n");
795 kill_domain_by_device(psdev);
799 if (!test_bit(_XEN_PCIB_AERHANDLER,
800 (unsigned long *)&psdev->pdev->sh_info->flags)) {
802 "guest with no AER driver should have been killed\n");
805 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
807 if (result == PCI_ERS_RESULT_NONE ||
808 result == PCI_ERS_RESULT_DISCONNECT) {
810 "No AER slot_reset service or disconnected!\n");
811 kill_domain_by_device(psdev);
815 pcistub_device_put(psdev);
816 up_write(&pcistub_sem);
822 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
823 * in case of the device driver could provide this service, and then wait
825 * @dev: pointer to PCI devices
826 * return value is used by aer_core do_recovery policy
829 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
831 struct pcistub_device *psdev;
832 pci_ers_result_t result;
834 result = PCI_ERS_RESULT_RECOVERED;
835 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
836 dev->bus->number, dev->devfn);
838 down_write(&pcistub_sem);
839 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
841 PCI_SLOT(dev->devfn),
842 PCI_FUNC(dev->devfn));
844 if (!psdev || !psdev->pdev) {
846 DRV_NAME " device is not found/assigned\n");
850 if (!psdev->pdev->sh_info) {
851 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
852 " by HVM, kill it\n");
853 kill_domain_by_device(psdev);
857 if (!test_bit(_XEN_PCIB_AERHANDLER,
858 (unsigned long *)&psdev->pdev->sh_info->flags)) {
860 "guest with no AER driver should have been killed\n");
863 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
865 if (result == PCI_ERS_RESULT_NONE ||
866 result == PCI_ERS_RESULT_DISCONNECT) {
868 "No AER mmio_enabled service or disconnected!\n");
869 kill_domain_by_device(psdev);
873 pcistub_device_put(psdev);
874 up_write(&pcistub_sem);
878 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
879 * in case of the device driver could provide this service, and then wait
881 * @dev: pointer to PCI devices
882 * @error: the current PCI connection state
883 * return value is used by aer_core do_recovery policy
886 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
887 pci_channel_state_t error)
889 struct pcistub_device *psdev;
890 pci_ers_result_t result;
892 result = PCI_ERS_RESULT_CAN_RECOVER;
893 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
894 dev->bus->number, dev->devfn);
896 down_write(&pcistub_sem);
897 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
899 PCI_SLOT(dev->devfn),
900 PCI_FUNC(dev->devfn));
902 if (!psdev || !psdev->pdev) {
904 DRV_NAME " device is not found/assigned\n");
908 if (!psdev->pdev->sh_info) {
909 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
910 " by HVM, kill it\n");
911 kill_domain_by_device(psdev);
915 /*Guest owns the device yet no aer handler regiested, kill guest*/
916 if (!test_bit(_XEN_PCIB_AERHANDLER,
917 (unsigned long *)&psdev->pdev->sh_info->flags)) {
918 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
919 kill_domain_by_device(psdev);
922 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
924 if (result == PCI_ERS_RESULT_NONE ||
925 result == PCI_ERS_RESULT_DISCONNECT) {
927 "No AER error_detected service or disconnected!\n");
928 kill_domain_by_device(psdev);
932 pcistub_device_put(psdev);
933 up_write(&pcistub_sem);
937 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
938 * in case of the device driver could provide this service, and then wait
940 * @dev: pointer to PCI devices
943 static void xen_pcibk_error_resume(struct pci_dev *dev)
945 struct pcistub_device *psdev;
947 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
948 dev->bus->number, dev->devfn);
950 down_write(&pcistub_sem);
951 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
953 PCI_SLOT(dev->devfn),
954 PCI_FUNC(dev->devfn));
956 if (!psdev || !psdev->pdev) {
958 DRV_NAME " device is not found/assigned\n");
962 if (!psdev->pdev->sh_info) {
963 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
964 " by HVM, kill it\n");
965 kill_domain_by_device(psdev);
969 if (!test_bit(_XEN_PCIB_AERHANDLER,
970 (unsigned long *)&psdev->pdev->sh_info->flags)) {
972 "guest with no AER driver should have been killed\n");
973 kill_domain_by_device(psdev);
976 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
977 PCI_ERS_RESULT_RECOVERED);
980 pcistub_device_put(psdev);
981 up_write(&pcistub_sem);
985 /*add xen_pcibk AER handling*/
986 static const struct pci_error_handlers xen_pcibk_error_handler = {
987 .error_detected = xen_pcibk_error_detected,
988 .mmio_enabled = xen_pcibk_mmio_enabled,
989 .slot_reset = xen_pcibk_slot_reset,
990 .resume = xen_pcibk_error_resume,
994 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
995 * for a normal device. I don't want it to be loaded automatically.
998 static struct pci_driver xen_pcibk_pci_driver = {
999 /* The name should be xen_pciback, but until the tools are updated
1000 * we will keep it as pciback. */
1001 .name = PCISTUB_DRIVER_NAME,
1002 .id_table = pcistub_ids,
1003 .probe = pcistub_probe,
1004 .remove = pcistub_remove,
1005 .err_handler = &xen_pcibk_error_handler,
1008 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1009 int *slot, int *func)
1013 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1017 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1021 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1024 if (parsed && !buf[parsed])
1027 /* try again without domain */
1029 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1032 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1036 sscanf(buf, " %x:*.* %n", bus, &parsed);
1039 if (parsed && !buf[parsed])
1045 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1046 *slot, int *func, int *reg, int *size, int *mask)
1050 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1051 reg, size, mask, &parsed);
1052 if (parsed && !buf[parsed])
1055 /* try again without domain */
1057 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1059 if (parsed && !buf[parsed])
1065 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1067 struct pcistub_device_id *pci_dev_id;
1068 int rc = 0, devfn = PCI_DEVFN(slot, func);
1071 for (slot = 0; !rc && slot < 32; ++slot)
1072 rc = pcistub_device_id_add(domain, bus, slot, func);
1077 for (func = 0; !rc && func < 8; ++func)
1078 rc = pcistub_device_id_add(domain, bus, slot, func);
1083 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1084 || !defined(CONFIG_PCI_DOMAINS)
1085 !pci_domains_supported ? domain :
1087 domain < 0 || domain > 0xffff)
1088 || bus < 0 || bus > 0xff
1089 || PCI_SLOT(devfn) != slot
1090 || PCI_FUNC(devfn) != func)
1093 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1097 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1098 domain, bus, slot, func);
1100 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1105 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1107 struct pcistub_device_id *pci_dev_id, *t;
1109 unsigned long flags;
1111 spin_lock_irqsave(&device_ids_lock, flags);
1112 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1114 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1115 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1116 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1117 /* Don't break; here because it's possible the same
1118 * slot could be in the list more than once
1120 list_del(&pci_dev_id->slot_list);
1125 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1126 domain, bus, slot, func);
1129 spin_unlock_irqrestore(&device_ids_lock, flags);
1134 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1135 unsigned int reg, unsigned int size,
1139 struct pcistub_device *psdev;
1140 struct pci_dev *dev;
1141 struct config_field *field;
1143 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1146 psdev = pcistub_device_find(domain, bus, slot, func);
1153 field = kzalloc(sizeof(*field), GFP_KERNEL);
1159 field->offset = reg;
1163 field->reset = NULL;
1164 field->release = NULL;
1165 field->clean = xen_pcibk_config_field_free;
1167 err = xen_pcibk_config_quirks_add_field(dev, field);
1172 pcistub_device_put(psdev);
1176 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1179 int domain, bus, slot, func;
1182 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1186 err = pcistub_device_id_add(domain, bus, slot, func);
1193 static DRIVER_ATTR_WO(new_slot);
1195 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1198 int domain, bus, slot, func;
1201 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1205 err = pcistub_device_id_remove(domain, bus, slot, func);
1212 static DRIVER_ATTR_WO(remove_slot);
1214 static ssize_t slots_show(struct device_driver *drv, char *buf)
1216 struct pcistub_device_id *pci_dev_id;
1218 unsigned long flags;
1220 spin_lock_irqsave(&device_ids_lock, flags);
1221 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1222 if (count >= PAGE_SIZE)
1225 count += scnprintf(buf + count, PAGE_SIZE - count,
1226 "%04x:%02x:%02x.%d\n",
1227 pci_dev_id->domain, pci_dev_id->bus,
1228 PCI_SLOT(pci_dev_id->devfn),
1229 PCI_FUNC(pci_dev_id->devfn));
1231 spin_unlock_irqrestore(&device_ids_lock, flags);
1235 static DRIVER_ATTR_RO(slots);
1237 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1239 struct pcistub_device *psdev;
1240 struct xen_pcibk_dev_data *dev_data;
1242 unsigned long flags;
1244 spin_lock_irqsave(&pcistub_devices_lock, flags);
1245 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1246 if (count >= PAGE_SIZE)
1250 dev_data = pci_get_drvdata(psdev->dev);
1254 scnprintf(buf + count, PAGE_SIZE - count,
1255 "%s:%s:%sing:%ld\n",
1256 pci_name(psdev->dev),
1257 dev_data->isr_on ? "on" : "off",
1258 dev_data->ack_intr ? "ack" : "not ack",
1261 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1264 static DRIVER_ATTR_RO(irq_handlers);
1266 static ssize_t irq_handler_state_store(struct device_driver *drv,
1267 const char *buf, size_t count)
1269 struct pcistub_device *psdev;
1270 struct xen_pcibk_dev_data *dev_data;
1271 int domain, bus, slot, func;
1274 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1278 psdev = pcistub_device_find(domain, bus, slot, func);
1284 dev_data = pci_get_drvdata(psdev->dev);
1290 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1291 dev_data->irq_name, dev_data->isr_on,
1294 dev_data->isr_on = !(dev_data->isr_on);
1295 if (dev_data->isr_on)
1296 dev_data->ack_intr = 1;
1299 pcistub_device_put(psdev);
1304 static DRIVER_ATTR_WO(irq_handler_state);
1306 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1309 int domain, bus, slot, func, reg, size, mask;
1312 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1317 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1325 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1328 unsigned long flags;
1329 struct xen_pcibk_config_quirk *quirk;
1330 struct xen_pcibk_dev_data *dev_data;
1331 const struct config_field *field;
1332 const struct config_field_entry *cfg_entry;
1334 spin_lock_irqsave(&device_ids_lock, flags);
1335 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1336 if (count >= PAGE_SIZE)
1339 count += scnprintf(buf + count, PAGE_SIZE - count,
1340 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1341 quirk->pdev->bus->number,
1342 PCI_SLOT(quirk->pdev->devfn),
1343 PCI_FUNC(quirk->pdev->devfn),
1344 quirk->devid.vendor, quirk->devid.device,
1345 quirk->devid.subvendor,
1346 quirk->devid.subdevice);
1348 dev_data = pci_get_drvdata(quirk->pdev);
1350 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1351 field = cfg_entry->field;
1352 if (count >= PAGE_SIZE)
1355 count += scnprintf(buf + count, PAGE_SIZE - count,
1356 "\t\t%08x:%01x:%08x\n",
1357 cfg_entry->base_offset +
1358 field->offset, field->size,
1364 spin_unlock_irqrestore(&device_ids_lock, flags);
1368 static DRIVER_ATTR_RW(quirks);
1370 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1373 int domain, bus, slot, func;
1375 struct pcistub_device *psdev;
1376 struct xen_pcibk_dev_data *dev_data;
1378 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1382 psdev = pcistub_device_find(domain, bus, slot, func);
1388 dev_data = pci_get_drvdata(psdev->dev);
1389 /* the driver data for a device should never be null at this point */
1394 if (!dev_data->permissive) {
1395 dev_data->permissive = 1;
1396 /* Let user know that what they're doing could be unsafe */
1397 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1398 "configuration space accesses!\n");
1399 dev_warn(&psdev->dev->dev,
1400 "permissive mode is potentially unsafe!\n");
1403 pcistub_device_put(psdev);
1410 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1412 struct pcistub_device *psdev;
1413 struct xen_pcibk_dev_data *dev_data;
1415 unsigned long flags;
1416 spin_lock_irqsave(&pcistub_devices_lock, flags);
1417 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1418 if (count >= PAGE_SIZE)
1422 dev_data = pci_get_drvdata(psdev->dev);
1423 if (!dev_data || !dev_data->permissive)
1426 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1427 pci_name(psdev->dev));
1429 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1432 static DRIVER_ATTR_RW(permissive);
1434 static void pcistub_exit(void)
1436 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1437 driver_remove_file(&xen_pcibk_pci_driver.driver,
1438 &driver_attr_remove_slot);
1439 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1440 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1441 driver_remove_file(&xen_pcibk_pci_driver.driver,
1442 &driver_attr_permissive);
1443 driver_remove_file(&xen_pcibk_pci_driver.driver,
1444 &driver_attr_irq_handlers);
1445 driver_remove_file(&xen_pcibk_pci_driver.driver,
1446 &driver_attr_irq_handler_state);
1447 pci_unregister_driver(&xen_pcibk_pci_driver);
1450 static int __init pcistub_init(void)
1454 int domain, bus, slot, func;
1457 if (pci_devs_to_hide && *pci_devs_to_hide) {
1461 err = sscanf(pci_devs_to_hide + pos,
1462 " (%x:%x:%x.%x) %n",
1463 &domain, &bus, &slot, &func, &parsed);
1467 sscanf(pci_devs_to_hide + pos,
1469 &domain, &bus, &slot, &parsed);
1473 sscanf(pci_devs_to_hide + pos,
1475 &domain, &bus, &parsed);
1481 err = sscanf(pci_devs_to_hide + pos,
1483 &bus, &slot, &func, &parsed);
1487 sscanf(pci_devs_to_hide + pos,
1489 &bus, &slot, &parsed);
1493 sscanf(pci_devs_to_hide + pos,
1503 err = pcistub_device_id_add(domain, bus, slot, func);
1508 } while (pci_devs_to_hide[pos]);
1511 /* If we're the first PCI Device Driver to register, we're the
1512 * first one to get offered PCI devices as they become
1513 * available (and thus we can be the first to grab them)
1515 err = pci_register_driver(&xen_pcibk_pci_driver);
1519 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1520 &driver_attr_new_slot);
1522 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1523 &driver_attr_remove_slot);
1525 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1526 &driver_attr_slots);
1528 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1529 &driver_attr_quirks);
1531 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1532 &driver_attr_permissive);
1535 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1536 &driver_attr_irq_handlers);
1538 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1539 &driver_attr_irq_handler_state);
1547 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1548 pci_devs_to_hide + pos);
1554 * fs_initcall happens before device_initcall
1555 * so xen_pcibk *should* get called first (b/c we
1556 * want to suck up any device before other drivers
1557 * get a chance by being the first pci device
1558 * driver to register)
1560 fs_initcall(pcistub_init);
1563 #ifdef CONFIG_PCI_IOV
1564 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1566 struct pcistub_device *psdev = NULL;
1567 unsigned long flags;
1570 spin_lock_irqsave(&pcistub_devices_lock, flags);
1571 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1572 if (!psdev->pdev && psdev->dev != pdev
1573 && pci_physfn(psdev->dev) == pdev) {
1578 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1584 static int pci_stub_notifier(struct notifier_block *nb,
1585 unsigned long action, void *data)
1587 struct device *dev = data;
1588 const struct pci_dev *pdev = to_pci_dev(dev);
1590 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1593 if (!pdev->is_physfn)
1597 struct pcistub_device *psdev = find_vfs(pdev);
1600 device_release_driver(&psdev->dev->dev);
1605 static struct notifier_block pci_stub_nb = {
1606 .notifier_call = pci_stub_notifier,
1610 static int __init xen_pcibk_init(void)
1614 if (!xen_initial_domain())
1617 err = xen_pcibk_config_init();
1622 err = pcistub_init();
1627 pcistub_init_devices_late();
1628 err = xen_pcibk_xenbus_register();
1631 #ifdef CONFIG_PCI_IOV
1633 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1639 static void __exit xen_pcibk_cleanup(void)
1641 #ifdef CONFIG_PCI_IOV
1642 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1644 xen_pcibk_xenbus_unregister();
1648 module_init(xen_pcibk_init);
1649 module_exit(xen_pcibk_cleanup);
1651 MODULE_LICENSE("Dual BSD/GPL");
1652 MODULE_ALIAS("xen-backend:pci");