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
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/rwsem.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/kref.h>
17 #include <linux/pci.h>
18 #include <linux/wait.h>
19 #include <linux/sched.h>
20 #include <linux/atomic.h>
21 #include <xen/events.h>
24 #include <asm/xen/hypervisor.h>
25 #include <xen/interface/physdev.h>
27 #include "conf_space.h"
28 #include "conf_space_quirks.h"
30 #define PCISTUB_DRIVER_NAME "pciback"
32 static char *pci_devs_to_hide;
33 wait_queue_head_t xen_pcibk_aer_wait_queue;
34 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
35 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
37 static DECLARE_RWSEM(pcistub_sem);
38 module_param_named(hide, pci_devs_to_hide, charp, 0444);
40 struct pcistub_device_id {
41 struct list_head slot_list;
46 static LIST_HEAD(pcistub_device_ids);
47 static DEFINE_SPINLOCK(device_ids_lock);
49 struct pcistub_device {
51 struct list_head dev_list;
55 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
58 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
59 * flag must be locked with pcistub_devices_lock
61 static DEFINE_SPINLOCK(pcistub_devices_lock);
62 static LIST_HEAD(pcistub_devices);
64 /* wait for device_initcall before initializing our devices
65 * (see pcistub_init_devices_late)
67 static int initialize_devices;
68 static LIST_HEAD(seized_devices);
70 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
72 struct pcistub_device *psdev;
74 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
76 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
80 psdev->dev = pci_dev_get(dev);
86 kref_init(&psdev->kref);
87 spin_lock_init(&psdev->lock);
92 /* Don't call this directly as it's called by pcistub_device_put */
93 static void pcistub_device_release(struct kref *kref)
95 struct pcistub_device *psdev;
97 struct xen_pcibk_dev_data *dev_data;
99 psdev = container_of(kref, struct pcistub_device, kref);
101 dev_data = pci_get_drvdata(dev);
103 dev_dbg(&dev->dev, "pcistub_device_release\n");
105 xen_unregister_device_domain_owner(dev);
107 /* Call the reset function which does not take lock as this
108 * is called from "unbind" which takes a device_lock mutex.
110 __pci_reset_function_locked(dev);
112 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
113 dev_info(&dev->dev, "Could not reload PCI state\n");
115 pci_restore_state(dev);
118 struct physdev_pci_device ppdev = {
119 .seg = pci_domain_nr(dev->bus),
120 .bus = dev->bus->number,
123 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
126 if (err && err != -ENOSYS)
127 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
131 /* Disable the device */
132 xen_pcibk_reset_device(dev);
135 pci_set_drvdata(dev, NULL);
137 /* Clean-up the device */
138 xen_pcibk_config_free_dyn_fields(dev);
139 xen_pcibk_config_free_dev(dev);
141 pci_clear_dev_assigned(dev);
147 static inline void pcistub_device_get(struct pcistub_device *psdev)
149 kref_get(&psdev->kref);
152 static inline void pcistub_device_put(struct pcistub_device *psdev)
154 kref_put(&psdev->kref, pcistub_device_release);
157 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
160 struct pcistub_device *psdev;
162 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
163 if (psdev->dev != NULL
164 && domain == pci_domain_nr(psdev->dev->bus)
165 && bus == psdev->dev->bus->number
166 && slot == PCI_SLOT(psdev->dev->devfn)
167 && func == PCI_FUNC(psdev->dev->devfn)) {
175 static struct pcistub_device *pcistub_device_find(int domain, int bus,
178 struct pcistub_device *psdev;
181 spin_lock_irqsave(&pcistub_devices_lock, flags);
183 psdev = pcistub_device_find_locked(domain, bus, slot, func);
185 pcistub_device_get(psdev);
187 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
191 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
192 struct pcistub_device *psdev)
194 struct pci_dev *pci_dev = NULL;
197 pcistub_device_get(psdev);
199 spin_lock_irqsave(&psdev->lock, flags);
202 pci_dev = psdev->dev;
204 spin_unlock_irqrestore(&psdev->lock, flags);
207 pcistub_device_put(psdev);
212 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
216 struct pcistub_device *psdev;
217 struct pci_dev *found_dev = NULL;
220 spin_lock_irqsave(&pcistub_devices_lock, flags);
222 psdev = pcistub_device_find_locked(domain, bus, slot, func);
224 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
226 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
230 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
233 struct pcistub_device *psdev;
234 struct pci_dev *found_dev = NULL;
237 spin_lock_irqsave(&pcistub_devices_lock, flags);
239 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
240 if (psdev->dev == dev) {
241 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
246 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
252 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
253 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
254 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
255 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
257 * As such we have to be careful.
259 * To make this easier, the caller has to hold the device lock.
261 void pcistub_put_pci_dev(struct pci_dev *dev)
263 struct pcistub_device *psdev, *found_psdev = NULL;
265 struct xen_pcibk_dev_data *dev_data;
268 spin_lock_irqsave(&pcistub_devices_lock, flags);
270 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
271 if (psdev->dev == dev) {
277 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
278 if (WARN_ON(!found_psdev))
281 /*hold this lock for avoiding breaking link between
282 * pcistub and xen_pcibk when AER is in processing
284 down_write(&pcistub_sem);
285 /* Cleanup our device
286 * (so it's ready for the next domain)
288 device_lock_assert(&dev->dev);
289 __pci_reset_function_locked(dev);
291 dev_data = pci_get_drvdata(dev);
292 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
295 * The usual sequence is pci_save_state & pci_restore_state
296 * but the guest might have messed the configuration space up.
297 * Use the initial version (when device was bound to us).
299 pci_restore_state(dev);
301 dev_info(&dev->dev, "Could not reload PCI state\n");
302 /* This disables the device. */
303 xen_pcibk_reset_device(dev);
305 /* And cleanup up our emulated fields. */
306 xen_pcibk_config_reset_dev(dev);
307 xen_pcibk_config_free_dyn_fields(dev);
309 dev_data->allow_interrupt_control = 0;
311 xen_unregister_device_domain_owner(dev);
313 spin_lock_irqsave(&found_psdev->lock, flags);
314 found_psdev->pdev = NULL;
315 spin_unlock_irqrestore(&found_psdev->lock, flags);
317 pcistub_device_put(found_psdev);
318 up_write(&pcistub_sem);
321 static int pcistub_match_one(struct pci_dev *dev,
322 struct pcistub_device_id *pdev_id)
324 /* Match the specified device by domain, bus, slot, func and also if
325 * any of the device's parent bridges match.
327 for (; dev != NULL; dev = dev->bus->self) {
328 if (pci_domain_nr(dev->bus) == pdev_id->domain
329 && dev->bus->number == pdev_id->bus
330 && dev->devfn == pdev_id->devfn)
333 /* Sometimes topmost bridge links to itself. */
334 if (dev == dev->bus->self)
341 static int pcistub_match(struct pci_dev *dev)
343 struct pcistub_device_id *pdev_id;
347 spin_lock_irqsave(&device_ids_lock, flags);
348 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
349 if (pcistub_match_one(dev, pdev_id)) {
354 spin_unlock_irqrestore(&device_ids_lock, flags);
359 static int pcistub_init_device(struct pci_dev *dev)
361 struct xen_pcibk_dev_data *dev_data;
364 dev_dbg(&dev->dev, "initializing...\n");
366 /* The PCI backend is not intended to be a module (or to work with
367 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
368 * would need to be called somewhere to free the memory allocated
369 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
371 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
372 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
377 pci_set_drvdata(dev, dev_data);
380 * Setup name for fake IRQ handler. It will only be enabled
381 * once the device is turned on by the guest.
383 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
385 dev_dbg(&dev->dev, "initializing config\n");
387 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
388 err = xen_pcibk_config_init_dev(dev);
392 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
393 * must do this here because pcibios_enable_device may specify
394 * the pci device's true irq (and possibly its other resources)
395 * if they differ from what's in the configuration space.
396 * This makes the assumption that the device's resources won't
397 * change after this point (otherwise this code may break!)
399 dev_dbg(&dev->dev, "enabling device\n");
400 err = pci_enable_device(dev);
405 struct physdev_pci_device ppdev = {
406 .seg = pci_domain_nr(dev->bus),
407 .bus = dev->bus->number,
411 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
412 if (err && err != -ENOSYS)
413 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
417 /* We need the device active to save the state. */
418 dev_dbg(&dev->dev, "save state of device\n");
420 dev_data->pci_saved_state = pci_store_saved_state(dev);
421 if (!dev_data->pci_saved_state)
422 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
424 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
425 __pci_reset_function_locked(dev);
426 pci_restore_state(dev);
428 /* Now disable the device (this also ensures some private device
429 * data is setup before we export)
431 dev_dbg(&dev->dev, "reset device\n");
432 xen_pcibk_reset_device(dev);
434 pci_set_dev_assigned(dev);
438 xen_pcibk_config_free_dev(dev);
441 pci_set_drvdata(dev, NULL);
447 * Because some initialization still happens on
448 * devices during fs_initcall, we need to defer
449 * full initialization of our devices until
452 static int __init pcistub_init_devices_late(void)
454 struct pcistub_device *psdev;
458 spin_lock_irqsave(&pcistub_devices_lock, flags);
460 while (!list_empty(&seized_devices)) {
461 psdev = container_of(seized_devices.next,
462 struct pcistub_device, dev_list);
463 list_del(&psdev->dev_list);
465 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
467 err = pcistub_init_device(psdev->dev);
469 dev_err(&psdev->dev->dev,
470 "error %d initializing device\n", err);
475 spin_lock_irqsave(&pcistub_devices_lock, flags);
478 list_add_tail(&psdev->dev_list, &pcistub_devices);
481 initialize_devices = 1;
483 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
488 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
489 int domain, int bus, unsigned int devfn)
491 struct pcistub_device_id *pci_dev_id;
495 spin_lock_irqsave(&device_ids_lock, flags);
497 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
498 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
499 pci_dev_id->devfn == devfn) {
506 new->domain = domain;
509 list_add_tail(&new->slot_list, &pcistub_device_ids);
512 spin_unlock_irqrestore(&device_ids_lock, flags);
518 static int pcistub_seize(struct pci_dev *dev,
519 struct pcistub_device_id *pci_dev_id)
521 struct pcistub_device *psdev;
525 psdev = pcistub_device_alloc(dev);
531 spin_lock_irqsave(&pcistub_devices_lock, flags);
533 if (initialize_devices) {
534 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
536 /* don't want irqs disabled when calling pcistub_init_device */
537 err = pcistub_init_device(psdev->dev);
539 spin_lock_irqsave(&pcistub_devices_lock, flags);
542 list_add(&psdev->dev_list, &pcistub_devices);
544 dev_dbg(&dev->dev, "deferring initialization\n");
545 list_add(&psdev->dev_list, &seized_devices);
548 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
552 pcistub_device_put(psdev);
553 } else if (pci_dev_id)
554 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
555 dev->bus->number, dev->devfn);
560 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
561 * other functions that take the sysfs lock. */
562 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
565 struct pcistub_device_id *pci_dev_id = NULL;
567 dev_dbg(&dev->dev, "probing...\n");
569 match = pcistub_match(dev);
571 if ((dev->driver_override &&
572 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
575 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
576 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
577 dev_err(&dev->dev, "can't export pci devices that "
578 "don't have a normal (0) or bridge (1) "
585 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
592 dev_info(&dev->dev, "seizing device\n");
593 err = pcistub_seize(dev, pci_dev_id);
595 /* Didn't find the device */
602 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
603 * other functions that take the sysfs lock. */
604 static void pcistub_remove(struct pci_dev *dev)
606 struct pcistub_device *psdev, *found_psdev = NULL;
609 dev_dbg(&dev->dev, "removing\n");
611 spin_lock_irqsave(&pcistub_devices_lock, flags);
613 xen_pcibk_config_quirk_release(dev);
615 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
616 if (psdev->dev == dev) {
622 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
625 dev_dbg(&dev->dev, "found device to remove %s\n",
626 found_psdev->pdev ? "- in-use" : "");
628 if (found_psdev->pdev) {
629 int domid = xen_find_device_domain_owner(dev);
631 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
632 pci_name(found_psdev->dev), domid);
633 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
634 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
635 dev_warn(&dev->dev, "****** to other drivers or domains\n");
637 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
639 xen_pcibk_release_pci_dev(found_psdev->pdev,
641 false /* caller holds the lock. */);
644 spin_lock_irqsave(&pcistub_devices_lock, flags);
645 list_del(&found_psdev->dev_list);
646 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
648 /* the final put for releasing from the list */
649 pcistub_device_put(found_psdev);
653 static const struct pci_device_id pcistub_ids[] = {
655 .vendor = PCI_ANY_ID,
656 .device = PCI_ANY_ID,
657 .subvendor = PCI_ANY_ID,
658 .subdevice = PCI_ANY_ID,
663 #define PCI_NODENAME_MAX 40
664 static void kill_domain_by_device(struct pcistub_device *psdev)
666 struct xenbus_transaction xbt;
668 char nodename[PCI_NODENAME_MAX];
671 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
672 psdev->pdev->xdev->otherend_id);
675 err = xenbus_transaction_start(&xbt);
677 dev_err(&psdev->dev->dev,
678 "error %d when start xenbus transaction\n", err);
681 /*PV AER handlers will set this flag*/
682 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
683 err = xenbus_transaction_end(xbt, 0);
687 dev_err(&psdev->dev->dev,
688 "error %d when end xenbus transaction\n", err);
693 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
694 * backend need to have cooperation. In xen_pcibk, those steps will do similar
695 * jobs: send service request and waiting for front_end response.
697 static pci_ers_result_t common_process(struct pcistub_device *psdev,
698 pci_channel_state_t state, int aer_cmd,
699 pci_ers_result_t result)
701 pci_ers_result_t res = result;
702 struct xen_pcie_aer_op *aer_op;
703 struct xen_pcibk_device *pdev = psdev->pdev;
704 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
707 /*with PV AER drivers*/
708 aer_op = &(sh_info->aer_op);
709 aer_op->cmd = aer_cmd ;
710 /*useful for error_detected callback*/
713 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
714 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
716 dev_err(&psdev->dev->dev, "failed to get pcifront device\n");
717 return PCI_ERS_RESULT_NONE;
721 dev_dbg(&psdev->dev->dev, "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 /* Enable IRQ to signal "request done". */
739 xen_pcibk_lateeoi(pdev, 0);
741 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
742 !(test_bit(_XEN_PCIB_active, (unsigned long *)
743 &sh_info->flags)), 300*HZ);
745 /* Enable IRQ for pcifront request if not already active. */
746 if (!test_bit(_PDEVF_op_active, &pdev->flags))
747 xen_pcibk_lateeoi(pdev, 0);
750 if (test_bit(_XEN_PCIB_active,
751 (unsigned long *)&sh_info->flags)) {
752 dev_err(&psdev->dev->dev,
753 "pcifront aer process not responding!\n");
754 clear_bit(_XEN_PCIB_active,
755 (unsigned long *)&sh_info->flags);
756 aer_op->err = PCI_ERS_RESULT_NONE;
760 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
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) {
789 dev_err(&dev->dev, "device is not found/assigned\n");
793 if (!psdev->pdev->sh_info) {
794 dev_err(&dev->dev, "device is not connected or owned"
795 " by HVM, kill it\n");
796 kill_domain_by_device(psdev);
800 if (!test_bit(_XEN_PCIB_AERHANDLER,
801 (unsigned long *)&psdev->pdev->sh_info->flags)) {
803 "guest with no AER driver should have been killed\n");
806 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
808 if (result == PCI_ERS_RESULT_NONE ||
809 result == PCI_ERS_RESULT_DISCONNECT) {
811 "No AER slot_reset service or disconnected!\n");
812 kill_domain_by_device(psdev);
816 pcistub_device_put(psdev);
817 up_write(&pcistub_sem);
823 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
824 * in case of the device driver could provide this service, and then wait
826 * @dev: pointer to PCI devices
827 * return value is used by aer_core do_recovery policy
830 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
832 struct pcistub_device *psdev;
833 pci_ers_result_t result;
835 result = PCI_ERS_RESULT_RECOVERED;
836 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
837 dev->bus->number, dev->devfn);
839 down_write(&pcistub_sem);
840 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
842 PCI_SLOT(dev->devfn),
843 PCI_FUNC(dev->devfn));
845 if (!psdev || !psdev->pdev) {
846 dev_err(&dev->dev, "device is not found/assigned\n");
850 if (!psdev->pdev->sh_info) {
851 dev_err(&dev->dev, "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, pci_channel_io_normal, 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) {
903 dev_err(&dev->dev, "device is not found/assigned\n");
907 if (!psdev->pdev->sh_info) {
908 dev_err(&dev->dev, "device is not connected or owned"
909 " by HVM, kill it\n");
910 kill_domain_by_device(psdev);
914 /*Guest owns the device yet no aer handler regiested, kill guest*/
915 if (!test_bit(_XEN_PCIB_AERHANDLER,
916 (unsigned long *)&psdev->pdev->sh_info->flags)) {
917 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
918 kill_domain_by_device(psdev);
921 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
923 if (result == PCI_ERS_RESULT_NONE ||
924 result == PCI_ERS_RESULT_DISCONNECT) {
926 "No AER error_detected service or disconnected!\n");
927 kill_domain_by_device(psdev);
931 pcistub_device_put(psdev);
932 up_write(&pcistub_sem);
936 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
937 * in case of the device driver could provide this service, and then wait
939 * @dev: pointer to PCI devices
942 static void xen_pcibk_error_resume(struct pci_dev *dev)
944 struct pcistub_device *psdev;
946 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
947 dev->bus->number, dev->devfn);
949 down_write(&pcistub_sem);
950 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
952 PCI_SLOT(dev->devfn),
953 PCI_FUNC(dev->devfn));
955 if (!psdev || !psdev->pdev) {
956 dev_err(&dev->dev, "device is not found/assigned\n");
960 if (!psdev->pdev->sh_info) {
961 dev_err(&dev->dev, "device is not connected or owned"
962 " by HVM, kill it\n");
963 kill_domain_by_device(psdev);
967 if (!test_bit(_XEN_PCIB_AERHANDLER,
968 (unsigned long *)&psdev->pdev->sh_info->flags)) {
970 "guest with no AER driver should have been killed\n");
971 kill_domain_by_device(psdev);
974 common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
975 PCI_ERS_RESULT_RECOVERED);
978 pcistub_device_put(psdev);
979 up_write(&pcistub_sem);
983 /*add xen_pcibk AER handling*/
984 static const struct pci_error_handlers xen_pcibk_error_handler = {
985 .error_detected = xen_pcibk_error_detected,
986 .mmio_enabled = xen_pcibk_mmio_enabled,
987 .slot_reset = xen_pcibk_slot_reset,
988 .resume = xen_pcibk_error_resume,
992 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
993 * for a normal device. I don't want it to be loaded automatically.
996 static struct pci_driver xen_pcibk_pci_driver = {
997 /* The name should be xen_pciback, but until the tools are updated
998 * we will keep it as pciback. */
999 .name = PCISTUB_DRIVER_NAME,
1000 .id_table = pcistub_ids,
1001 .probe = pcistub_probe,
1002 .remove = pcistub_remove,
1003 .err_handler = &xen_pcibk_error_handler,
1006 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1007 int *slot, int *func)
1011 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1015 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1019 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1022 if (parsed && !buf[parsed])
1025 /* try again without domain */
1027 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1030 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1034 sscanf(buf, " %x:*.* %n", bus, &parsed);
1037 if (parsed && !buf[parsed])
1043 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1044 *slot, int *func, int *reg, int *size, int *mask)
1048 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1049 reg, size, mask, &parsed);
1050 if (parsed && !buf[parsed])
1053 /* try again without domain */
1055 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1057 if (parsed && !buf[parsed])
1063 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1065 struct pcistub_device_id *pci_dev_id;
1066 int rc = 0, devfn = PCI_DEVFN(slot, func);
1069 for (slot = 0; !rc && slot < 32; ++slot)
1070 rc = pcistub_device_id_add(domain, bus, slot, func);
1075 for (func = 0; !rc && func < 8; ++func)
1076 rc = pcistub_device_id_add(domain, bus, slot, func);
1081 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1082 || !defined(CONFIG_PCI_DOMAINS)
1083 !pci_domains_supported ? domain :
1085 domain < 0 || domain > 0xffff)
1086 || bus < 0 || bus > 0xff
1087 || PCI_SLOT(devfn) != slot
1088 || PCI_FUNC(devfn) != func)
1091 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1095 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1096 domain, bus, slot, func);
1098 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1103 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1105 struct pcistub_device_id *pci_dev_id, *t;
1107 unsigned long flags;
1109 spin_lock_irqsave(&device_ids_lock, flags);
1110 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1112 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1113 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1114 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1115 /* Don't break; here because it's possible the same
1116 * slot could be in the list more than once
1118 list_del(&pci_dev_id->slot_list);
1123 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1124 domain, bus, slot, func);
1127 spin_unlock_irqrestore(&device_ids_lock, flags);
1132 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1133 unsigned int reg, unsigned int size,
1137 struct pcistub_device *psdev;
1138 struct pci_dev *dev;
1139 struct config_field *field;
1141 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1144 psdev = pcistub_device_find(domain, bus, slot, func);
1151 field = kzalloc(sizeof(*field), GFP_KERNEL);
1157 field->offset = reg;
1161 field->reset = NULL;
1162 field->release = NULL;
1163 field->clean = xen_pcibk_config_field_free;
1165 err = xen_pcibk_config_quirks_add_field(dev, field);
1170 pcistub_device_put(psdev);
1174 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1177 int domain, bus, slot, func;
1180 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1184 err = pcistub_device_id_add(domain, bus, slot, func);
1191 static DRIVER_ATTR_WO(new_slot);
1193 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1196 int domain, bus, slot, func;
1199 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1203 err = pcistub_device_id_remove(domain, bus, slot, func);
1210 static DRIVER_ATTR_WO(remove_slot);
1212 static ssize_t slots_show(struct device_driver *drv, char *buf)
1214 struct pcistub_device_id *pci_dev_id;
1216 unsigned long flags;
1218 spin_lock_irqsave(&device_ids_lock, flags);
1219 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1220 if (count >= PAGE_SIZE)
1223 count += scnprintf(buf + count, PAGE_SIZE - count,
1224 "%04x:%02x:%02x.%d\n",
1225 pci_dev_id->domain, pci_dev_id->bus,
1226 PCI_SLOT(pci_dev_id->devfn),
1227 PCI_FUNC(pci_dev_id->devfn));
1229 spin_unlock_irqrestore(&device_ids_lock, flags);
1233 static DRIVER_ATTR_RO(slots);
1235 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1237 struct pcistub_device *psdev;
1238 struct xen_pcibk_dev_data *dev_data;
1240 unsigned long flags;
1242 spin_lock_irqsave(&pcistub_devices_lock, flags);
1243 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1244 if (count >= PAGE_SIZE)
1248 dev_data = pci_get_drvdata(psdev->dev);
1252 scnprintf(buf + count, PAGE_SIZE - count,
1253 "%s:%s:%sing:%ld\n",
1254 pci_name(psdev->dev),
1255 dev_data->isr_on ? "on" : "off",
1256 dev_data->ack_intr ? "ack" : "not ack",
1259 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1262 static DRIVER_ATTR_RO(irq_handlers);
1264 static ssize_t irq_handler_state_store(struct device_driver *drv,
1265 const char *buf, size_t count)
1267 struct pcistub_device *psdev;
1268 struct xen_pcibk_dev_data *dev_data;
1269 int domain, bus, slot, func;
1272 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1276 psdev = pcistub_device_find(domain, bus, slot, func);
1282 dev_data = pci_get_drvdata(psdev->dev);
1288 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1289 dev_data->irq_name, dev_data->isr_on,
1292 dev_data->isr_on = !(dev_data->isr_on);
1293 if (dev_data->isr_on)
1294 dev_data->ack_intr = 1;
1297 pcistub_device_put(psdev);
1302 static DRIVER_ATTR_WO(irq_handler_state);
1304 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1307 int domain, bus, slot, func, reg, size, mask;
1310 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1315 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1323 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1326 unsigned long flags;
1327 struct xen_pcibk_config_quirk *quirk;
1328 struct xen_pcibk_dev_data *dev_data;
1329 const struct config_field *field;
1330 const struct config_field_entry *cfg_entry;
1332 spin_lock_irqsave(&device_ids_lock, flags);
1333 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1334 if (count >= PAGE_SIZE)
1337 count += scnprintf(buf + count, PAGE_SIZE - count,
1338 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1339 quirk->pdev->bus->number,
1340 PCI_SLOT(quirk->pdev->devfn),
1341 PCI_FUNC(quirk->pdev->devfn),
1342 quirk->devid.vendor, quirk->devid.device,
1343 quirk->devid.subvendor,
1344 quirk->devid.subdevice);
1346 dev_data = pci_get_drvdata(quirk->pdev);
1348 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1349 field = cfg_entry->field;
1350 if (count >= PAGE_SIZE)
1353 count += scnprintf(buf + count, PAGE_SIZE - count,
1354 "\t\t%08x:%01x:%08x\n",
1355 cfg_entry->base_offset +
1356 field->offset, field->size,
1362 spin_unlock_irqrestore(&device_ids_lock, flags);
1366 static DRIVER_ATTR_RW(quirks);
1368 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1371 int domain, bus, slot, func;
1373 struct pcistub_device *psdev;
1374 struct xen_pcibk_dev_data *dev_data;
1376 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1380 psdev = pcistub_device_find(domain, bus, slot, func);
1386 dev_data = pci_get_drvdata(psdev->dev);
1387 /* the driver data for a device should never be null at this point */
1392 if (!dev_data->permissive) {
1393 dev_data->permissive = 1;
1394 /* Let user know that what they're doing could be unsafe */
1395 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1396 "configuration space accesses!\n");
1397 dev_warn(&psdev->dev->dev,
1398 "permissive mode is potentially unsafe!\n");
1401 pcistub_device_put(psdev);
1408 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1410 struct pcistub_device *psdev;
1411 struct xen_pcibk_dev_data *dev_data;
1413 unsigned long flags;
1414 spin_lock_irqsave(&pcistub_devices_lock, flags);
1415 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1416 if (count >= PAGE_SIZE)
1420 dev_data = pci_get_drvdata(psdev->dev);
1421 if (!dev_data || !dev_data->permissive)
1424 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1425 pci_name(psdev->dev));
1427 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1430 static DRIVER_ATTR_RW(permissive);
1432 static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1433 const char *buf, size_t count)
1435 int domain, bus, slot, func;
1437 struct pcistub_device *psdev;
1438 struct xen_pcibk_dev_data *dev_data;
1440 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1444 psdev = pcistub_device_find(domain, bus, slot, func);
1450 dev_data = pci_get_drvdata(psdev->dev);
1451 /* the driver data for a device should never be null at this point */
1456 dev_data->allow_interrupt_control = 1;
1458 pcistub_device_put(psdev);
1465 static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1468 struct pcistub_device *psdev;
1469 struct xen_pcibk_dev_data *dev_data;
1471 unsigned long flags;
1473 spin_lock_irqsave(&pcistub_devices_lock, flags);
1474 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1475 if (count >= PAGE_SIZE)
1479 dev_data = pci_get_drvdata(psdev->dev);
1480 if (!dev_data || !dev_data->allow_interrupt_control)
1483 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1484 pci_name(psdev->dev));
1486 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1489 static DRIVER_ATTR_RW(allow_interrupt_control);
1491 static void pcistub_exit(void)
1493 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1494 driver_remove_file(&xen_pcibk_pci_driver.driver,
1495 &driver_attr_remove_slot);
1496 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1497 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_permissive);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver,
1501 &driver_attr_allow_interrupt_control);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_irq_handlers);
1504 driver_remove_file(&xen_pcibk_pci_driver.driver,
1505 &driver_attr_irq_handler_state);
1506 pci_unregister_driver(&xen_pcibk_pci_driver);
1509 static int __init pcistub_init(void)
1513 int domain, bus, slot, func;
1516 if (pci_devs_to_hide && *pci_devs_to_hide) {
1520 err = sscanf(pci_devs_to_hide + pos,
1521 " (%x:%x:%x.%x) %n",
1522 &domain, &bus, &slot, &func, &parsed);
1526 sscanf(pci_devs_to_hide + pos,
1528 &domain, &bus, &slot, &parsed);
1532 sscanf(pci_devs_to_hide + pos,
1534 &domain, &bus, &parsed);
1540 err = sscanf(pci_devs_to_hide + pos,
1542 &bus, &slot, &func, &parsed);
1546 sscanf(pci_devs_to_hide + pos,
1548 &bus, &slot, &parsed);
1552 sscanf(pci_devs_to_hide + pos,
1562 err = pcistub_device_id_add(domain, bus, slot, func);
1567 } while (pci_devs_to_hide[pos]);
1570 /* If we're the first PCI Device Driver to register, we're the
1571 * first one to get offered PCI devices as they become
1572 * available (and thus we can be the first to grab them)
1574 err = pci_register_driver(&xen_pcibk_pci_driver);
1578 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1579 &driver_attr_new_slot);
1581 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1582 &driver_attr_remove_slot);
1584 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1585 &driver_attr_slots);
1587 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1588 &driver_attr_quirks);
1590 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1591 &driver_attr_permissive);
1593 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1594 &driver_attr_allow_interrupt_control);
1597 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1598 &driver_attr_irq_handlers);
1600 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1601 &driver_attr_irq_handler_state);
1609 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1610 pci_devs_to_hide + pos);
1616 * fs_initcall happens before device_initcall
1617 * so xen_pcibk *should* get called first (b/c we
1618 * want to suck up any device before other drivers
1619 * get a chance by being the first pci device
1620 * driver to register)
1622 fs_initcall(pcistub_init);
1625 #ifdef CONFIG_PCI_IOV
1626 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1628 struct pcistub_device *psdev = NULL;
1629 unsigned long flags;
1632 spin_lock_irqsave(&pcistub_devices_lock, flags);
1633 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1634 if (!psdev->pdev && psdev->dev != pdev
1635 && pci_physfn(psdev->dev) == pdev) {
1640 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1646 static int pci_stub_notifier(struct notifier_block *nb,
1647 unsigned long action, void *data)
1649 struct device *dev = data;
1650 const struct pci_dev *pdev = to_pci_dev(dev);
1652 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1655 if (!pdev->is_physfn)
1659 struct pcistub_device *psdev = find_vfs(pdev);
1662 device_release_driver(&psdev->dev->dev);
1667 static struct notifier_block pci_stub_nb = {
1668 .notifier_call = pci_stub_notifier,
1672 static int __init xen_pcibk_init(void)
1676 if (!xen_initial_domain())
1679 err = xen_pcibk_config_init();
1684 err = pcistub_init();
1689 pcistub_init_devices_late();
1690 err = xen_pcibk_xenbus_register();
1693 #ifdef CONFIG_PCI_IOV
1695 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1701 static void __exit xen_pcibk_cleanup(void)
1703 #ifdef CONFIG_PCI_IOV
1704 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1706 xen_pcibk_xenbus_unregister();
1710 module_init(xen_pcibk_init);
1711 module_exit(xen_pcibk_cleanup);
1713 MODULE_LICENSE("Dual BSD/GPL");
1714 MODULE_ALIAS("xen-backend:pci");