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 spin_lock_irqsave(&psdev->lock, flags);
200 pci_dev = psdev->dev;
202 spin_unlock_irqrestore(&psdev->lock, flags);
205 pcistub_device_get(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 dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
630 pci_name(found_psdev->dev), domid);
631 dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
632 dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
633 dev_warn(&dev->dev, "****** 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, "failed to get pcifront device\n");
715 return PCI_ERS_RESULT_NONE;
719 dev_dbg(&psdev->dev->dev, "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 /* Enable IRQ to signal "request done". */
737 xen_pcibk_lateeoi(pdev, 0);
739 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
740 !(test_bit(_XEN_PCIB_active, (unsigned long *)
741 &sh_info->flags)), 300*HZ);
743 /* Enable IRQ for pcifront request if not already active. */
744 if (!test_bit(_PDEVF_op_active, &pdev->flags))
745 xen_pcibk_lateeoi(pdev, 0);
748 if (test_bit(_XEN_PCIB_active,
749 (unsigned long *)&sh_info->flags)) {
750 dev_err(&psdev->dev->dev,
751 "pcifront aer process not responding!\n");
752 clear_bit(_XEN_PCIB_active,
753 (unsigned long *)&sh_info->flags);
754 aer_op->err = PCI_ERS_RESULT_NONE;
758 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
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) {
787 dev_err(&dev->dev, "device is not found/assigned\n");
791 if (!psdev->pdev->sh_info) {
792 dev_err(&dev->dev, "device is not connected or owned"
793 " by HVM, kill it\n");
794 kill_domain_by_device(psdev);
798 if (!test_bit(_XEN_PCIB_AERHANDLER,
799 (unsigned long *)&psdev->pdev->sh_info->flags)) {
801 "guest with no AER driver should have been killed\n");
804 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
806 if (result == PCI_ERS_RESULT_NONE ||
807 result == PCI_ERS_RESULT_DISCONNECT) {
809 "No AER slot_reset service or disconnected!\n");
810 kill_domain_by_device(psdev);
814 pcistub_device_put(psdev);
815 up_write(&pcistub_sem);
821 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
822 * in case of the device driver could provide this service, and then wait
824 * @dev: pointer to PCI devices
825 * return value is used by aer_core do_recovery policy
828 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
830 struct pcistub_device *psdev;
831 pci_ers_result_t result;
833 result = PCI_ERS_RESULT_RECOVERED;
834 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
835 dev->bus->number, dev->devfn);
837 down_write(&pcistub_sem);
838 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
840 PCI_SLOT(dev->devfn),
841 PCI_FUNC(dev->devfn));
843 if (!psdev || !psdev->pdev) {
844 dev_err(&dev->dev, "device is not found/assigned\n");
848 if (!psdev->pdev->sh_info) {
849 dev_err(&dev->dev, "device is not connected or owned"
850 " by HVM, kill it\n");
851 kill_domain_by_device(psdev);
855 if (!test_bit(_XEN_PCIB_AERHANDLER,
856 (unsigned long *)&psdev->pdev->sh_info->flags)) {
858 "guest with no AER driver should have been killed\n");
861 result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
863 if (result == PCI_ERS_RESULT_NONE ||
864 result == PCI_ERS_RESULT_DISCONNECT) {
866 "No AER mmio_enabled service or disconnected!\n");
867 kill_domain_by_device(psdev);
871 pcistub_device_put(psdev);
872 up_write(&pcistub_sem);
876 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
877 * in case of the device driver could provide this service, and then wait
879 * @dev: pointer to PCI devices
880 * @error: the current PCI connection state
881 * return value is used by aer_core do_recovery policy
884 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
885 pci_channel_state_t error)
887 struct pcistub_device *psdev;
888 pci_ers_result_t result;
890 result = PCI_ERS_RESULT_CAN_RECOVER;
891 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
892 dev->bus->number, dev->devfn);
894 down_write(&pcistub_sem);
895 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
897 PCI_SLOT(dev->devfn),
898 PCI_FUNC(dev->devfn));
900 if (!psdev || !psdev->pdev) {
901 dev_err(&dev->dev, "device is not found/assigned\n");
905 if (!psdev->pdev->sh_info) {
906 dev_err(&dev->dev, "device is not connected or owned"
907 " by HVM, kill it\n");
908 kill_domain_by_device(psdev);
912 /*Guest owns the device yet no aer handler regiested, kill guest*/
913 if (!test_bit(_XEN_PCIB_AERHANDLER,
914 (unsigned long *)&psdev->pdev->sh_info->flags)) {
915 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
916 kill_domain_by_device(psdev);
919 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
921 if (result == PCI_ERS_RESULT_NONE ||
922 result == PCI_ERS_RESULT_DISCONNECT) {
924 "No AER error_detected service or disconnected!\n");
925 kill_domain_by_device(psdev);
929 pcistub_device_put(psdev);
930 up_write(&pcistub_sem);
934 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
935 * in case of the device driver could provide this service, and then wait
937 * @dev: pointer to PCI devices
940 static void xen_pcibk_error_resume(struct pci_dev *dev)
942 struct pcistub_device *psdev;
944 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
945 dev->bus->number, dev->devfn);
947 down_write(&pcistub_sem);
948 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
950 PCI_SLOT(dev->devfn),
951 PCI_FUNC(dev->devfn));
953 if (!psdev || !psdev->pdev) {
954 dev_err(&dev->dev, "device is not found/assigned\n");
958 if (!psdev->pdev->sh_info) {
959 dev_err(&dev->dev, "device is not connected or owned"
960 " by HVM, kill it\n");
961 kill_domain_by_device(psdev);
965 if (!test_bit(_XEN_PCIB_AERHANDLER,
966 (unsigned long *)&psdev->pdev->sh_info->flags)) {
968 "guest with no AER driver should have been killed\n");
969 kill_domain_by_device(psdev);
972 common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
973 PCI_ERS_RESULT_RECOVERED);
976 pcistub_device_put(psdev);
977 up_write(&pcistub_sem);
981 /*add xen_pcibk AER handling*/
982 static const struct pci_error_handlers xen_pcibk_error_handler = {
983 .error_detected = xen_pcibk_error_detected,
984 .mmio_enabled = xen_pcibk_mmio_enabled,
985 .slot_reset = xen_pcibk_slot_reset,
986 .resume = xen_pcibk_error_resume,
990 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
991 * for a normal device. I don't want it to be loaded automatically.
994 static struct pci_driver xen_pcibk_pci_driver = {
995 /* The name should be xen_pciback, but until the tools are updated
996 * we will keep it as pciback. */
997 .name = PCISTUB_DRIVER_NAME,
998 .id_table = pcistub_ids,
999 .probe = pcistub_probe,
1000 .remove = pcistub_remove,
1001 .err_handler = &xen_pcibk_error_handler,
1004 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1005 int *slot, int *func)
1009 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1013 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1017 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1020 if (parsed && !buf[parsed])
1023 /* try again without domain */
1025 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1028 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1032 sscanf(buf, " %x:*.* %n", bus, &parsed);
1035 if (parsed && !buf[parsed])
1041 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1042 *slot, int *func, int *reg, int *size, int *mask)
1046 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1047 reg, size, mask, &parsed);
1048 if (parsed && !buf[parsed])
1051 /* try again without domain */
1053 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1055 if (parsed && !buf[parsed])
1061 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1063 struct pcistub_device_id *pci_dev_id;
1064 int rc = 0, devfn = PCI_DEVFN(slot, func);
1067 for (slot = 0; !rc && slot < 32; ++slot)
1068 rc = pcistub_device_id_add(domain, bus, slot, func);
1073 for (func = 0; !rc && func < 8; ++func)
1074 rc = pcistub_device_id_add(domain, bus, slot, func);
1079 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1080 || !defined(CONFIG_PCI_DOMAINS)
1081 !pci_domains_supported ? domain :
1083 domain < 0 || domain > 0xffff)
1084 || bus < 0 || bus > 0xff
1085 || PCI_SLOT(devfn) != slot
1086 || PCI_FUNC(devfn) != func)
1089 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1093 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1094 domain, bus, slot, func);
1096 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1101 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1103 struct pcistub_device_id *pci_dev_id, *t;
1105 unsigned long flags;
1107 spin_lock_irqsave(&device_ids_lock, flags);
1108 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1110 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1111 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1112 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1113 /* Don't break; here because it's possible the same
1114 * slot could be in the list more than once
1116 list_del(&pci_dev_id->slot_list);
1121 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1122 domain, bus, slot, func);
1125 spin_unlock_irqrestore(&device_ids_lock, flags);
1130 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1131 unsigned int reg, unsigned int size,
1135 struct pcistub_device *psdev;
1136 struct pci_dev *dev;
1137 struct config_field *field;
1139 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1142 psdev = pcistub_device_find(domain, bus, slot, func);
1149 field = kzalloc(sizeof(*field), GFP_KERNEL);
1155 field->offset = reg;
1159 field->reset = NULL;
1160 field->release = NULL;
1161 field->clean = xen_pcibk_config_field_free;
1163 err = xen_pcibk_config_quirks_add_field(dev, field);
1168 pcistub_device_put(psdev);
1172 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1175 int domain, bus, slot, func;
1178 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1182 err = pcistub_device_id_add(domain, bus, slot, func);
1189 static DRIVER_ATTR_WO(new_slot);
1191 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1194 int domain, bus, slot, func;
1197 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1201 err = pcistub_device_id_remove(domain, bus, slot, func);
1208 static DRIVER_ATTR_WO(remove_slot);
1210 static ssize_t slots_show(struct device_driver *drv, char *buf)
1212 struct pcistub_device_id *pci_dev_id;
1214 unsigned long flags;
1216 spin_lock_irqsave(&device_ids_lock, flags);
1217 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1218 if (count >= PAGE_SIZE)
1221 count += scnprintf(buf + count, PAGE_SIZE - count,
1222 "%04x:%02x:%02x.%d\n",
1223 pci_dev_id->domain, pci_dev_id->bus,
1224 PCI_SLOT(pci_dev_id->devfn),
1225 PCI_FUNC(pci_dev_id->devfn));
1227 spin_unlock_irqrestore(&device_ids_lock, flags);
1231 static DRIVER_ATTR_RO(slots);
1233 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1235 struct pcistub_device *psdev;
1236 struct xen_pcibk_dev_data *dev_data;
1238 unsigned long flags;
1240 spin_lock_irqsave(&pcistub_devices_lock, flags);
1241 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1242 if (count >= PAGE_SIZE)
1246 dev_data = pci_get_drvdata(psdev->dev);
1250 scnprintf(buf + count, PAGE_SIZE - count,
1251 "%s:%s:%sing:%ld\n",
1252 pci_name(psdev->dev),
1253 dev_data->isr_on ? "on" : "off",
1254 dev_data->ack_intr ? "ack" : "not ack",
1257 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1260 static DRIVER_ATTR_RO(irq_handlers);
1262 static ssize_t irq_handler_state_store(struct device_driver *drv,
1263 const char *buf, size_t count)
1265 struct pcistub_device *psdev;
1266 struct xen_pcibk_dev_data *dev_data;
1267 int domain, bus, slot, func;
1270 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1274 psdev = pcistub_device_find(domain, bus, slot, func);
1280 dev_data = pci_get_drvdata(psdev->dev);
1286 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1287 dev_data->irq_name, dev_data->isr_on,
1290 dev_data->isr_on = !(dev_data->isr_on);
1291 if (dev_data->isr_on)
1292 dev_data->ack_intr = 1;
1295 pcistub_device_put(psdev);
1300 static DRIVER_ATTR_WO(irq_handler_state);
1302 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1305 int domain, bus, slot, func, reg, size, mask;
1308 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1313 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1321 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1324 unsigned long flags;
1325 struct xen_pcibk_config_quirk *quirk;
1326 struct xen_pcibk_dev_data *dev_data;
1327 const struct config_field *field;
1328 const struct config_field_entry *cfg_entry;
1330 spin_lock_irqsave(&device_ids_lock, flags);
1331 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1332 if (count >= PAGE_SIZE)
1335 count += scnprintf(buf + count, PAGE_SIZE - count,
1336 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1337 quirk->pdev->bus->number,
1338 PCI_SLOT(quirk->pdev->devfn),
1339 PCI_FUNC(quirk->pdev->devfn),
1340 quirk->devid.vendor, quirk->devid.device,
1341 quirk->devid.subvendor,
1342 quirk->devid.subdevice);
1344 dev_data = pci_get_drvdata(quirk->pdev);
1346 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1347 field = cfg_entry->field;
1348 if (count >= PAGE_SIZE)
1351 count += scnprintf(buf + count, PAGE_SIZE - count,
1352 "\t\t%08x:%01x:%08x\n",
1353 cfg_entry->base_offset +
1354 field->offset, field->size,
1360 spin_unlock_irqrestore(&device_ids_lock, flags);
1364 static DRIVER_ATTR_RW(quirks);
1366 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1369 int domain, bus, slot, func;
1371 struct pcistub_device *psdev;
1372 struct xen_pcibk_dev_data *dev_data;
1374 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1378 psdev = pcistub_device_find(domain, bus, slot, func);
1384 dev_data = pci_get_drvdata(psdev->dev);
1385 /* the driver data for a device should never be null at this point */
1390 if (!dev_data->permissive) {
1391 dev_data->permissive = 1;
1392 /* Let user know that what they're doing could be unsafe */
1393 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1394 "configuration space accesses!\n");
1395 dev_warn(&psdev->dev->dev,
1396 "permissive mode is potentially unsafe!\n");
1399 pcistub_device_put(psdev);
1406 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1408 struct pcistub_device *psdev;
1409 struct xen_pcibk_dev_data *dev_data;
1411 unsigned long flags;
1412 spin_lock_irqsave(&pcistub_devices_lock, flags);
1413 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1414 if (count >= PAGE_SIZE)
1418 dev_data = pci_get_drvdata(psdev->dev);
1419 if (!dev_data || !dev_data->permissive)
1422 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1423 pci_name(psdev->dev));
1425 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1428 static DRIVER_ATTR_RW(permissive);
1430 static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1431 const char *buf, size_t count)
1433 int domain, bus, slot, func;
1435 struct pcistub_device *psdev;
1436 struct xen_pcibk_dev_data *dev_data;
1438 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1442 psdev = pcistub_device_find(domain, bus, slot, func);
1448 dev_data = pci_get_drvdata(psdev->dev);
1449 /* the driver data for a device should never be null at this point */
1454 dev_data->allow_interrupt_control = 1;
1456 pcistub_device_put(psdev);
1463 static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1466 struct pcistub_device *psdev;
1467 struct xen_pcibk_dev_data *dev_data;
1469 unsigned long flags;
1471 spin_lock_irqsave(&pcistub_devices_lock, flags);
1472 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1473 if (count >= PAGE_SIZE)
1477 dev_data = pci_get_drvdata(psdev->dev);
1478 if (!dev_data || !dev_data->allow_interrupt_control)
1481 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1482 pci_name(psdev->dev));
1484 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1487 static DRIVER_ATTR_RW(allow_interrupt_control);
1489 static void pcistub_exit(void)
1491 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1492 driver_remove_file(&xen_pcibk_pci_driver.driver,
1493 &driver_attr_remove_slot);
1494 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1495 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1496 driver_remove_file(&xen_pcibk_pci_driver.driver,
1497 &driver_attr_permissive);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_allow_interrupt_control);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver,
1501 &driver_attr_irq_handlers);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_irq_handler_state);
1504 pci_unregister_driver(&xen_pcibk_pci_driver);
1507 static int __init pcistub_init(void)
1511 int domain, bus, slot, func;
1514 if (pci_devs_to_hide && *pci_devs_to_hide) {
1518 err = sscanf(pci_devs_to_hide + pos,
1519 " (%x:%x:%x.%x) %n",
1520 &domain, &bus, &slot, &func, &parsed);
1524 sscanf(pci_devs_to_hide + pos,
1526 &domain, &bus, &slot, &parsed);
1530 sscanf(pci_devs_to_hide + pos,
1532 &domain, &bus, &parsed);
1538 err = sscanf(pci_devs_to_hide + pos,
1540 &bus, &slot, &func, &parsed);
1544 sscanf(pci_devs_to_hide + pos,
1546 &bus, &slot, &parsed);
1550 sscanf(pci_devs_to_hide + pos,
1560 err = pcistub_device_id_add(domain, bus, slot, func);
1565 } while (pci_devs_to_hide[pos]);
1568 /* If we're the first PCI Device Driver to register, we're the
1569 * first one to get offered PCI devices as they become
1570 * available (and thus we can be the first to grab them)
1572 err = pci_register_driver(&xen_pcibk_pci_driver);
1576 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1577 &driver_attr_new_slot);
1579 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1580 &driver_attr_remove_slot);
1582 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1583 &driver_attr_slots);
1585 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1586 &driver_attr_quirks);
1588 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1589 &driver_attr_permissive);
1591 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1592 &driver_attr_allow_interrupt_control);
1595 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1596 &driver_attr_irq_handlers);
1598 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1599 &driver_attr_irq_handler_state);
1607 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1608 pci_devs_to_hide + pos);
1614 * fs_initcall happens before device_initcall
1615 * so xen_pcibk *should* get called first (b/c we
1616 * want to suck up any device before other drivers
1617 * get a chance by being the first pci device
1618 * driver to register)
1620 fs_initcall(pcistub_init);
1623 #ifdef CONFIG_PCI_IOV
1624 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1626 struct pcistub_device *psdev = NULL;
1627 unsigned long flags;
1630 spin_lock_irqsave(&pcistub_devices_lock, flags);
1631 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1632 if (!psdev->pdev && psdev->dev != pdev
1633 && pci_physfn(psdev->dev) == pdev) {
1638 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1644 static int pci_stub_notifier(struct notifier_block *nb,
1645 unsigned long action, void *data)
1647 struct device *dev = data;
1648 const struct pci_dev *pdev = to_pci_dev(dev);
1650 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1653 if (!pdev->is_physfn)
1657 struct pcistub_device *psdev = find_vfs(pdev);
1660 device_release_driver(&psdev->dev->dev);
1665 static struct notifier_block pci_stub_nb = {
1666 .notifier_call = pci_stub_notifier,
1670 static int __init xen_pcibk_init(void)
1674 if (!xen_initial_domain())
1677 err = xen_pcibk_config_init();
1682 err = pcistub_init();
1687 pcistub_init_devices_late();
1688 err = xen_pcibk_xenbus_register();
1691 #ifdef CONFIG_PCI_IOV
1693 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1699 static void __exit xen_pcibk_cleanup(void)
1701 #ifdef CONFIG_PCI_IOV
1702 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1704 xen_pcibk_xenbus_unregister();
1708 module_init(xen_pcibk_init);
1709 module_exit(xen_pcibk_cleanup);
1711 MODULE_LICENSE("Dual BSD/GPL");
1712 MODULE_ALIAS("xen-backend:pci");