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 #ifdef CONFIG_XEN_ACPI
27 #include <asm/xen/hypervisor.h>
28 #include <xen/interface/physdev.h>
30 #include "conf_space.h"
31 #include "conf_space_quirks.h"
33 #define PCISTUB_DRIVER_NAME "pciback"
35 static char *pci_devs_to_hide
;
36 wait_queue_head_t xen_pcibk_aer_wait_queue
;
37 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
38 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
40 static DECLARE_RWSEM(pcistub_sem
);
41 module_param_named(hide
, pci_devs_to_hide
, charp
, 0444);
43 struct pcistub_device_id
{
44 struct list_head slot_list
;
49 static LIST_HEAD(pcistub_device_ids
);
50 static DEFINE_SPINLOCK(device_ids_lock
);
52 struct pcistub_device
{
54 struct list_head dev_list
;
58 struct xen_pcibk_device
*pdev
;/* non-NULL if struct pci_dev is in use */
59 #ifdef CONFIG_XEN_ACPI
64 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
65 * flag must be locked with pcistub_devices_lock
67 static DEFINE_SPINLOCK(pcistub_devices_lock
);
68 static LIST_HEAD(pcistub_devices
);
70 /* wait for device_initcall before initializing our devices
71 * (see pcistub_init_devices_late)
73 static int initialize_devices
;
74 static LIST_HEAD(seized_devices
);
76 static struct pcistub_device
*pcistub_device_alloc(struct pci_dev
*dev
)
78 struct pcistub_device
*psdev
;
80 dev_dbg(&dev
->dev
, "pcistub_device_alloc\n");
82 psdev
= kzalloc(sizeof(*psdev
), GFP_KERNEL
);
86 psdev
->dev
= pci_dev_get(dev
);
92 kref_init(&psdev
->kref
);
93 spin_lock_init(&psdev
->lock
);
94 #ifdef CONFIG_XEN_ACPI
101 static int pcistub_reset_device_state(struct pci_dev
*dev
)
103 __pci_reset_function_locked(dev
);
105 if (!xen_pv_domain())
106 return xen_reset_device(dev
);
111 /* Don't call this directly as it's called by pcistub_device_put */
112 static void pcistub_device_release(struct kref
*kref
)
114 struct pcistub_device
*psdev
;
116 struct xen_pcibk_dev_data
*dev_data
;
118 psdev
= container_of(kref
, struct pcistub_device
, kref
);
120 dev_data
= pci_get_drvdata(dev
);
122 dev_dbg(&dev
->dev
, "pcistub_device_release\n");
124 xen_unregister_device_domain_owner(dev
);
126 /* Call the reset function which does not take lock as this
127 * is called from "unbind" which takes a device_lock mutex.
129 pcistub_reset_device_state(dev
);
131 pci_load_and_free_saved_state(dev
, &dev_data
->pci_saved_state
))
132 dev_info(&dev
->dev
, "Could not reload PCI state\n");
134 pci_restore_state(dev
);
137 struct physdev_pci_device ppdev
= {
138 .seg
= pci_domain_nr(dev
->bus
),
139 .bus
= dev
->bus
->number
,
142 int err
= HYPERVISOR_physdev_op(PHYSDEVOP_release_msix
,
145 if (err
&& err
!= -ENOSYS
)
146 dev_warn(&dev
->dev
, "MSI-X release failed (%d)\n",
150 /* Disable the device */
151 xen_pcibk_reset_device(dev
);
154 pci_set_drvdata(dev
, NULL
);
156 /* Clean-up the device */
157 xen_pcibk_config_free_dyn_fields(dev
);
158 xen_pcibk_config_free_dev(dev
);
160 pci_clear_dev_assigned(dev
);
166 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
168 kref_get(&psdev
->kref
);
171 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
173 kref_put(&psdev
->kref
, pcistub_device_release
);
176 static struct pcistub_device
*pcistub_device_find_locked(int domain
, int bus
,
179 struct pcistub_device
*psdev
;
181 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
182 if (psdev
->dev
!= NULL
183 && domain
== pci_domain_nr(psdev
->dev
->bus
)
184 && bus
== psdev
->dev
->bus
->number
185 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
186 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
194 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
197 struct pcistub_device
*psdev
;
200 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
202 psdev
= pcistub_device_find_locked(domain
, bus
, slot
, func
);
204 pcistub_device_get(psdev
);
206 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
210 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
211 struct pcistub_device
*psdev
)
213 struct pci_dev
*pci_dev
= NULL
;
216 spin_lock_irqsave(&psdev
->lock
, flags
);
219 pci_dev
= psdev
->dev
;
221 spin_unlock_irqrestore(&psdev
->lock
, flags
);
224 pcistub_device_get(psdev
);
229 #ifdef CONFIG_XEN_ACPI
230 static int pcistub_get_gsi_from_sbdf(unsigned int sbdf
)
232 struct pcistub_device
*psdev
;
233 int domain
= (sbdf
>> 16) & 0xffff;
234 int bus
= PCI_BUS_NUM(sbdf
);
235 int slot
= PCI_SLOT(sbdf
);
236 int func
= PCI_FUNC(sbdf
);
238 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
247 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
251 struct pcistub_device
*psdev
;
252 struct pci_dev
*found_dev
= NULL
;
255 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
257 psdev
= pcistub_device_find_locked(domain
, bus
, slot
, func
);
259 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
261 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
265 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
268 struct pcistub_device
*psdev
;
269 struct pci_dev
*found_dev
= NULL
;
272 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
274 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
275 if (psdev
->dev
== dev
) {
276 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
281 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
287 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
288 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
289 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
290 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
292 * As such we have to be careful.
294 * To make this easier, the caller has to hold the device lock.
296 void pcistub_put_pci_dev(struct pci_dev
*dev
)
298 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
300 struct xen_pcibk_dev_data
*dev_data
;
303 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
305 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
306 if (psdev
->dev
== dev
) {
312 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
313 if (WARN_ON(!found_psdev
))
316 /*hold this lock for avoiding breaking link between
317 * pcistub and xen_pcibk when AER is in processing
319 down_write(&pcistub_sem
);
320 /* Cleanup our device
321 * (so it's ready for the next domain)
323 device_lock_assert(&dev
->dev
);
324 pcistub_reset_device_state(dev
);
326 dev_data
= pci_get_drvdata(dev
);
327 ret
= pci_load_saved_state(dev
, dev_data
->pci_saved_state
);
330 * The usual sequence is pci_save_state & pci_restore_state
331 * but the guest might have messed the configuration space up.
332 * Use the initial version (when device was bound to us).
334 pci_restore_state(dev
);
336 dev_info(&dev
->dev
, "Could not reload PCI state\n");
337 /* This disables the device. */
338 xen_pcibk_reset_device(dev
);
340 /* And cleanup up our emulated fields. */
341 xen_pcibk_config_reset_dev(dev
);
342 xen_pcibk_config_free_dyn_fields(dev
);
344 dev_data
->allow_interrupt_control
= 0;
346 xen_unregister_device_domain_owner(dev
);
348 spin_lock_irqsave(&found_psdev
->lock
, flags
);
349 found_psdev
->pdev
= NULL
;
350 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
352 pcistub_device_put(found_psdev
);
353 up_write(&pcistub_sem
);
356 static int pcistub_match_one(struct pci_dev
*dev
,
357 struct pcistub_device_id
*pdev_id
)
359 /* Match the specified device by domain, bus, slot, func and also if
360 * any of the device's parent bridges match.
362 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
363 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
364 && dev
->bus
->number
== pdev_id
->bus
365 && dev
->devfn
== pdev_id
->devfn
)
368 /* Sometimes topmost bridge links to itself. */
369 if (dev
== dev
->bus
->self
)
376 static int pcistub_match(struct pci_dev
*dev
)
378 struct pcistub_device_id
*pdev_id
;
382 spin_lock_irqsave(&device_ids_lock
, flags
);
383 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
384 if (pcistub_match_one(dev
, pdev_id
)) {
389 spin_unlock_irqrestore(&device_ids_lock
, flags
);
394 static int pcistub_init_device(struct pcistub_device
*psdev
)
396 struct xen_pcibk_dev_data
*dev_data
;
398 #ifdef CONFIG_XEN_ACPI
399 int gsi
, trigger
, polarity
;
408 dev_dbg(&dev
->dev
, "initializing...\n");
410 /* The PCI backend is not intended to be a module (or to work with
411 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
412 * would need to be called somewhere to free the memory allocated
413 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
415 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
416 + strlen(pci_name(dev
)) + 1, GFP_KERNEL
);
421 pci_set_drvdata(dev
, dev_data
);
424 * Setup name for fake IRQ handler. It will only be enabled
425 * once the device is turned on by the guest.
427 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
429 dev_dbg(&dev
->dev
, "initializing config\n");
431 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
432 err
= xen_pcibk_config_init_dev(dev
);
436 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
437 * must do this here because pcibios_enable_device may specify
438 * the pci device's true irq (and possibly its other resources)
439 * if they differ from what's in the configuration space.
440 * This makes the assumption that the device's resources won't
441 * change after this point (otherwise this code may break!)
443 dev_dbg(&dev
->dev
, "enabling device\n");
444 err
= pci_enable_device(dev
);
449 struct physdev_pci_device ppdev
= {
450 .seg
= pci_domain_nr(dev
->bus
),
451 .bus
= dev
->bus
->number
,
455 err
= HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix
, &ppdev
);
456 if (err
&& err
!= -ENOSYS
)
457 dev_err(&dev
->dev
, "MSI-X preparation failed (%d)\n",
461 /* We need the device active to save the state. */
462 dev_dbg(&dev
->dev
, "save state of device\n");
464 dev_data
->pci_saved_state
= pci_store_saved_state(dev
);
465 if (!dev_data
->pci_saved_state
)
466 dev_err(&dev
->dev
, "Could not store PCI conf saved state!\n");
468 dev_dbg(&dev
->dev
, "resetting (FLR, D3, etc) the device\n");
469 err
= pcistub_reset_device_state(dev
);
472 pci_restore_state(dev
);
475 #ifdef CONFIG_XEN_ACPI
476 if (xen_initial_domain() && xen_pvh_domain()) {
477 err
= xen_acpi_get_gsi_info(dev
, &gsi
, &trigger
, &polarity
);
479 dev_err(&dev
->dev
, "Fail to get gsi info!\n");
482 err
= xen_pvh_setup_gsi(gsi
, trigger
, polarity
);
489 /* Now disable the device (this also ensures some private device
490 * data is setup before we export)
492 dev_dbg(&dev
->dev
, "reset device\n");
493 xen_pcibk_reset_device(dev
);
495 pci_set_dev_assigned(dev
);
499 xen_pcibk_config_free_dev(dev
);
502 pci_set_drvdata(dev
, NULL
);
508 * Because some initialization still happens on
509 * devices during fs_initcall, we need to defer
510 * full initialization of our devices until
513 static int __init
pcistub_init_devices_late(void)
515 struct pcistub_device
*psdev
;
519 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
521 while (!list_empty(&seized_devices
)) {
522 psdev
= container_of(seized_devices
.next
,
523 struct pcistub_device
, dev_list
);
524 list_del(&psdev
->dev_list
);
526 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
528 err
= pcistub_init_device(psdev
);
530 dev_err(&psdev
->dev
->dev
,
531 "error %d initializing device\n", err
);
536 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
539 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
542 initialize_devices
= 1;
544 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
549 static void pcistub_device_id_add_list(struct pcistub_device_id
*new,
550 int domain
, int bus
, unsigned int devfn
)
552 struct pcistub_device_id
*pci_dev_id
;
556 spin_lock_irqsave(&device_ids_lock
, flags
);
558 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
559 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
&&
560 pci_dev_id
->devfn
== devfn
) {
567 new->domain
= domain
;
570 list_add_tail(&new->slot_list
, &pcistub_device_ids
);
573 spin_unlock_irqrestore(&device_ids_lock
, flags
);
579 static int pcistub_seize(struct pci_dev
*dev
,
580 struct pcistub_device_id
*pci_dev_id
)
582 struct pcistub_device
*psdev
;
586 psdev
= pcistub_device_alloc(dev
);
592 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
594 if (initialize_devices
) {
595 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
597 /* don't want irqs disabled when calling pcistub_init_device */
598 err
= pcistub_init_device(psdev
);
600 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
603 list_add(&psdev
->dev_list
, &pcistub_devices
);
605 dev_dbg(&dev
->dev
, "deferring initialization\n");
606 list_add(&psdev
->dev_list
, &seized_devices
);
609 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
613 pcistub_device_put(psdev
);
614 } else if (pci_dev_id
)
615 pcistub_device_id_add_list(pci_dev_id
, pci_domain_nr(dev
->bus
),
616 dev
->bus
->number
, dev
->devfn
);
621 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
622 * other functions that take the sysfs lock. */
623 static int pcistub_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
626 struct pcistub_device_id
*pci_dev_id
= NULL
;
628 dev_dbg(&dev
->dev
, "probing...\n");
630 match
= pcistub_match(dev
);
632 if ((dev
->driver_override
&&
633 !strcmp(dev
->driver_override
, PCISTUB_DRIVER_NAME
)) ||
636 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
637 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
638 dev_err(&dev
->dev
, "can't export pci devices that "
639 "don't have a normal (0) or bridge (1) "
646 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
653 dev_info(&dev
->dev
, "seizing device\n");
654 err
= pcistub_seize(dev
, pci_dev_id
);
656 /* Didn't find the device */
663 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
664 * other functions that take the sysfs lock. */
665 static void pcistub_remove(struct pci_dev
*dev
)
667 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
670 dev_dbg(&dev
->dev
, "removing\n");
672 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
674 xen_pcibk_config_quirk_release(dev
);
676 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
677 if (psdev
->dev
== dev
) {
683 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
686 dev_dbg(&dev
->dev
, "found device to remove %s\n",
687 found_psdev
->pdev
? "- in-use" : "");
689 if (found_psdev
->pdev
) {
690 int domid
= xen_find_device_domain_owner(dev
);
692 dev_warn(&dev
->dev
, "****** removing device %s while still in-use by domain %d! ******\n",
693 pci_name(found_psdev
->dev
), domid
);
694 dev_warn(&dev
->dev
, "****** driver domain may still access this device's i/o resources!\n");
695 dev_warn(&dev
->dev
, "****** shutdown driver domain before binding device\n");
696 dev_warn(&dev
->dev
, "****** to other drivers or domains\n");
698 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
700 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
702 false /* caller holds the lock. */);
705 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
706 list_del(&found_psdev
->dev_list
);
707 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
709 /* the final put for releasing from the list */
710 pcistub_device_put(found_psdev
);
714 static const struct pci_device_id pcistub_ids
[] = {
716 .vendor
= PCI_ANY_ID
,
717 .device
= PCI_ANY_ID
,
718 .subvendor
= PCI_ANY_ID
,
719 .subdevice
= PCI_ANY_ID
,
724 #define PCI_NODENAME_MAX 40
725 static void kill_domain_by_device(struct pcistub_device
*psdev
)
727 struct xenbus_transaction xbt
;
729 char nodename
[PCI_NODENAME_MAX
];
732 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
733 psdev
->pdev
->xdev
->otherend_id
);
736 err
= xenbus_transaction_start(&xbt
);
738 dev_err(&psdev
->dev
->dev
,
739 "error %d when start xenbus transaction\n", err
);
742 /*PV AER handlers will set this flag*/
743 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
744 err
= xenbus_transaction_end(xbt
, 0);
748 dev_err(&psdev
->dev
->dev
,
749 "error %d when end xenbus transaction\n", err
);
754 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
755 * backend need to have cooperation. In xen_pcibk, those steps will do similar
756 * jobs: send service request and waiting for front_end response.
758 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
759 pci_channel_state_t state
, int aer_cmd
,
760 pci_ers_result_t result
)
762 pci_ers_result_t res
= result
;
763 struct xen_pcie_aer_op
*aer_op
;
764 struct xen_pcibk_device
*pdev
= psdev
->pdev
;
765 struct xen_pci_sharedinfo
*sh_info
= pdev
->sh_info
;
768 /*with PV AER drivers*/
769 aer_op
= &(sh_info
->aer_op
);
770 aer_op
->cmd
= aer_cmd
;
771 /*useful for error_detected callback*/
774 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
775 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
777 dev_err(&psdev
->dev
->dev
, "failed to get pcifront device\n");
778 return PCI_ERS_RESULT_NONE
;
782 dev_dbg(&psdev
->dev
->dev
, "aer_op %x dom %x bus %x devfn %x\n",
783 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
784 /*local flag to mark there's aer request, xen_pcibk callback will use
785 * this flag to judge whether we need to check pci-front give aer
788 set_bit(_PCIB_op_pending
, (unsigned long *)&pdev
->flags
);
790 /*It is possible that a pcifront conf_read_write ops request invokes
791 * the callback which cause the spurious execution of wake_up.
792 * Yet it is harmless and better than a spinlock here
794 set_bit(_XEN_PCIB_active
,
795 (unsigned long *)&sh_info
->flags
);
797 notify_remote_via_irq(pdev
->evtchn_irq
);
799 /* Enable IRQ to signal "request done". */
800 xen_pcibk_lateeoi(pdev
, 0);
802 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
803 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
804 &sh_info
->flags
)), 300*HZ
);
806 /* Enable IRQ for pcifront request if not already active. */
807 if (!test_bit(_PDEVF_op_active
, &pdev
->flags
))
808 xen_pcibk_lateeoi(pdev
, 0);
811 if (test_bit(_XEN_PCIB_active
,
812 (unsigned long *)&sh_info
->flags
)) {
813 dev_err(&psdev
->dev
->dev
,
814 "pcifront aer process not responding!\n");
815 clear_bit(_XEN_PCIB_active
,
816 (unsigned long *)&sh_info
->flags
);
817 aer_op
->err
= PCI_ERS_RESULT_NONE
;
821 clear_bit(_PCIB_op_pending
, (unsigned long *)&pdev
->flags
);
823 res
= (__force pci_ers_result_t
)aer_op
->err
;
828 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
829 * of the device driver could provide this service, and then wait for pcifront
831 * @dev: pointer to PCI devices
832 * return value is used by aer_core do_recovery policy
834 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
836 struct pcistub_device
*psdev
;
837 pci_ers_result_t result
;
839 result
= PCI_ERS_RESULT_RECOVERED
;
840 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
841 dev
->bus
->number
, dev
->devfn
);
843 down_write(&pcistub_sem
);
844 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
846 PCI_SLOT(dev
->devfn
),
847 PCI_FUNC(dev
->devfn
));
849 if (!psdev
|| !psdev
->pdev
) {
850 dev_err(&dev
->dev
, "device is not found/assigned\n");
854 if (!psdev
->pdev
->sh_info
) {
855 dev_err(&dev
->dev
, "device is not connected or owned"
856 " by HVM, kill it\n");
857 kill_domain_by_device(psdev
);
861 if (!test_bit(_XEN_PCIB_AERHANDLER
,
862 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
864 "guest with no AER driver should have been killed\n");
867 result
= common_process(psdev
, pci_channel_io_normal
, XEN_PCI_OP_aer_slotreset
, result
);
869 if (result
== PCI_ERS_RESULT_NONE
||
870 result
== PCI_ERS_RESULT_DISCONNECT
) {
872 "No AER slot_reset service or disconnected!\n");
873 kill_domain_by_device(psdev
);
877 pcistub_device_put(psdev
);
878 up_write(&pcistub_sem
);
884 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
885 * in case of the device driver could provide this service, and then wait
887 * @dev: pointer to PCI devices
888 * return value is used by aer_core do_recovery policy
891 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
893 struct pcistub_device
*psdev
;
894 pci_ers_result_t result
;
896 result
= PCI_ERS_RESULT_RECOVERED
;
897 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
898 dev
->bus
->number
, dev
->devfn
);
900 down_write(&pcistub_sem
);
901 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
903 PCI_SLOT(dev
->devfn
),
904 PCI_FUNC(dev
->devfn
));
906 if (!psdev
|| !psdev
->pdev
) {
907 dev_err(&dev
->dev
, "device is not found/assigned\n");
911 if (!psdev
->pdev
->sh_info
) {
912 dev_err(&dev
->dev
, "device is not connected or owned"
913 " by HVM, kill it\n");
914 kill_domain_by_device(psdev
);
918 if (!test_bit(_XEN_PCIB_AERHANDLER
,
919 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
921 "guest with no AER driver should have been killed\n");
924 result
= common_process(psdev
, pci_channel_io_normal
, XEN_PCI_OP_aer_mmio
, result
);
926 if (result
== PCI_ERS_RESULT_NONE
||
927 result
== PCI_ERS_RESULT_DISCONNECT
) {
929 "No AER mmio_enabled service or disconnected!\n");
930 kill_domain_by_device(psdev
);
934 pcistub_device_put(psdev
);
935 up_write(&pcistub_sem
);
939 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
940 * in case of the device driver could provide this service, and then wait
942 * @dev: pointer to PCI devices
943 * @error: the current PCI connection state
944 * return value is used by aer_core do_recovery policy
947 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
948 pci_channel_state_t error
)
950 struct pcistub_device
*psdev
;
951 pci_ers_result_t result
;
953 result
= PCI_ERS_RESULT_CAN_RECOVER
;
954 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
955 dev
->bus
->number
, dev
->devfn
);
957 down_write(&pcistub_sem
);
958 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
960 PCI_SLOT(dev
->devfn
),
961 PCI_FUNC(dev
->devfn
));
963 if (!psdev
|| !psdev
->pdev
) {
964 dev_err(&dev
->dev
, "device is not found/assigned\n");
968 if (!psdev
->pdev
->sh_info
) {
969 dev_err(&dev
->dev
, "device is not connected or owned"
970 " by HVM, kill it\n");
971 kill_domain_by_device(psdev
);
975 /*Guest owns the device yet no aer handler regiested, kill guest*/
976 if (!test_bit(_XEN_PCIB_AERHANDLER
,
977 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
978 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
979 kill_domain_by_device(psdev
);
982 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
984 if (result
== PCI_ERS_RESULT_NONE
||
985 result
== PCI_ERS_RESULT_DISCONNECT
) {
987 "No AER error_detected service or disconnected!\n");
988 kill_domain_by_device(psdev
);
992 pcistub_device_put(psdev
);
993 up_write(&pcistub_sem
);
997 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
998 * in case of the device driver could provide this service, and then wait
1000 * @dev: pointer to PCI devices
1003 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
1005 struct pcistub_device
*psdev
;
1007 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
1008 dev
->bus
->number
, dev
->devfn
);
1010 down_write(&pcistub_sem
);
1011 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
1013 PCI_SLOT(dev
->devfn
),
1014 PCI_FUNC(dev
->devfn
));
1016 if (!psdev
|| !psdev
->pdev
) {
1017 dev_err(&dev
->dev
, "device is not found/assigned\n");
1021 if (!psdev
->pdev
->sh_info
) {
1022 dev_err(&dev
->dev
, "device is not connected or owned"
1023 " by HVM, kill it\n");
1024 kill_domain_by_device(psdev
);
1028 if (!test_bit(_XEN_PCIB_AERHANDLER
,
1029 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
1031 "guest with no AER driver should have been killed\n");
1032 kill_domain_by_device(psdev
);
1035 common_process(psdev
, pci_channel_io_normal
, XEN_PCI_OP_aer_resume
,
1036 PCI_ERS_RESULT_RECOVERED
);
1039 pcistub_device_put(psdev
);
1040 up_write(&pcistub_sem
);
1044 /*add xen_pcibk AER handling*/
1045 static const struct pci_error_handlers xen_pcibk_error_handler
= {
1046 .error_detected
= xen_pcibk_error_detected
,
1047 .mmio_enabled
= xen_pcibk_mmio_enabled
,
1048 .slot_reset
= xen_pcibk_slot_reset
,
1049 .resume
= xen_pcibk_error_resume
,
1053 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
1054 * for a normal device. I don't want it to be loaded automatically.
1057 static struct pci_driver xen_pcibk_pci_driver
= {
1058 /* The name should be xen_pciback, but until the tools are updated
1059 * we will keep it as pciback. */
1060 .name
= PCISTUB_DRIVER_NAME
,
1061 .id_table
= pcistub_ids
,
1062 .probe
= pcistub_probe
,
1063 .remove
= pcistub_remove
,
1064 .err_handler
= &xen_pcibk_error_handler
,
1067 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
1068 int *slot
, int *func
)
1072 switch (sscanf(buf
, " %x:%x:%x.%x %n", domain
, bus
, slot
, func
,
1076 sscanf(buf
, " %x:%x:%x.* %n", domain
, bus
, slot
, &parsed
);
1080 sscanf(buf
, " %x:%x:*.* %n", domain
, bus
, &parsed
);
1083 if (parsed
&& !buf
[parsed
])
1086 /* try again without domain */
1088 switch (sscanf(buf
, " %x:%x.%x %n", bus
, slot
, func
, &parsed
)) {
1091 sscanf(buf
, " %x:%x.* %n", bus
, slot
, &parsed
);
1095 sscanf(buf
, " %x:*.* %n", bus
, &parsed
);
1098 if (parsed
&& !buf
[parsed
])
1104 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
1105 *slot
, int *func
, int *reg
, int *size
, int *mask
)
1109 sscanf(buf
, " %x:%x:%x.%x-%x:%x:%x %n", domain
, bus
, slot
, func
,
1110 reg
, size
, mask
, &parsed
);
1111 if (parsed
&& !buf
[parsed
])
1114 /* try again without domain */
1116 sscanf(buf
, " %x:%x.%x-%x:%x:%x %n", bus
, slot
, func
, reg
, size
,
1118 if (parsed
&& !buf
[parsed
])
1124 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
1126 struct pcistub_device_id
*pci_dev_id
;
1127 int rc
= 0, devfn
= PCI_DEVFN(slot
, func
);
1130 for (slot
= 0; !rc
&& slot
< 32; ++slot
)
1131 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1136 for (func
= 0; !rc
&& func
< 8; ++func
)
1137 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1142 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1143 || !defined(CONFIG_PCI_DOMAINS)
1144 !pci_domains_supported
? domain
:
1146 domain
< 0 || domain
> 0xffff)
1147 || bus
< 0 || bus
> 0xff
1148 || PCI_SLOT(devfn
) != slot
1149 || PCI_FUNC(devfn
) != func
)
1152 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
1156 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1157 domain
, bus
, slot
, func
);
1159 pcistub_device_id_add_list(pci_dev_id
, domain
, bus
, devfn
);
1164 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
1166 struct pcistub_device_id
*pci_dev_id
, *t
;
1168 unsigned long flags
;
1170 spin_lock_irqsave(&device_ids_lock
, flags
);
1171 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
1173 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
1174 && (slot
< 0 || PCI_SLOT(pci_dev_id
->devfn
) == slot
)
1175 && (func
< 0 || PCI_FUNC(pci_dev_id
->devfn
) == func
)) {
1176 /* Don't break; here because it's possible the same
1177 * slot could be in the list more than once
1179 list_del(&pci_dev_id
->slot_list
);
1184 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1185 domain
, bus
, slot
, func
);
1188 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1193 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
,
1194 unsigned int reg
, unsigned int size
,
1198 struct pcistub_device
*psdev
;
1199 struct pci_dev
*dev
;
1200 struct config_field
*field
;
1202 if (reg
> 0xfff || (size
< 4 && (mask
>> (size
* 8))))
1205 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1212 field
= kzalloc(sizeof(*field
), GFP_KERNEL
);
1218 field
->offset
= reg
;
1222 field
->reset
= NULL
;
1223 field
->release
= NULL
;
1224 field
->clean
= xen_pcibk_config_field_free
;
1226 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
1231 pcistub_device_put(psdev
);
1235 static ssize_t
new_slot_store(struct device_driver
*drv
, const char *buf
,
1238 int domain
, bus
, slot
, func
;
1241 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1245 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1252 static DRIVER_ATTR_WO(new_slot
);
1254 static ssize_t
remove_slot_store(struct device_driver
*drv
, const char *buf
,
1257 int domain
, bus
, slot
, func
;
1260 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1264 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1271 static DRIVER_ATTR_WO(remove_slot
);
1273 static ssize_t
slots_show(struct device_driver
*drv
, char *buf
)
1275 struct pcistub_device_id
*pci_dev_id
;
1277 unsigned long flags
;
1279 spin_lock_irqsave(&device_ids_lock
, flags
);
1280 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1281 if (count
>= PAGE_SIZE
)
1284 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1285 "%04x:%02x:%02x.%d\n",
1286 pci_dev_id
->domain
, pci_dev_id
->bus
,
1287 PCI_SLOT(pci_dev_id
->devfn
),
1288 PCI_FUNC(pci_dev_id
->devfn
));
1290 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1294 static DRIVER_ATTR_RO(slots
);
1296 static ssize_t
irq_handlers_show(struct device_driver
*drv
, char *buf
)
1298 struct pcistub_device
*psdev
;
1299 struct xen_pcibk_dev_data
*dev_data
;
1301 unsigned long flags
;
1303 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1304 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1305 if (count
>= PAGE_SIZE
)
1309 dev_data
= pci_get_drvdata(psdev
->dev
);
1313 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1314 "%s:%s:%sing:%ld\n",
1315 pci_name(psdev
->dev
),
1316 dev_data
->isr_on
? "on" : "off",
1317 dev_data
->ack_intr
? "ack" : "not ack",
1320 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1323 static DRIVER_ATTR_RO(irq_handlers
);
1325 static ssize_t
irq_handler_state_store(struct device_driver
*drv
,
1326 const char *buf
, size_t count
)
1328 struct pcistub_device
*psdev
;
1329 struct xen_pcibk_dev_data
*dev_data
;
1330 int domain
, bus
, slot
, func
;
1333 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1337 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1343 dev_data
= pci_get_drvdata(psdev
->dev
);
1349 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1350 dev_data
->irq_name
, dev_data
->isr_on
,
1353 dev_data
->isr_on
= !(dev_data
->isr_on
);
1354 if (dev_data
->isr_on
)
1355 dev_data
->ack_intr
= 1;
1358 pcistub_device_put(psdev
);
1363 static DRIVER_ATTR_WO(irq_handler_state
);
1365 static ssize_t
quirks_store(struct device_driver
*drv
, const char *buf
,
1368 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1371 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1376 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1384 static ssize_t
quirks_show(struct device_driver
*drv
, char *buf
)
1387 unsigned long flags
;
1388 struct xen_pcibk_config_quirk
*quirk
;
1389 struct xen_pcibk_dev_data
*dev_data
;
1390 const struct config_field
*field
;
1391 const struct config_field_entry
*cfg_entry
;
1393 spin_lock_irqsave(&device_ids_lock
, flags
);
1394 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1395 if (count
>= PAGE_SIZE
)
1398 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1399 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1400 quirk
->pdev
->bus
->number
,
1401 PCI_SLOT(quirk
->pdev
->devfn
),
1402 PCI_FUNC(quirk
->pdev
->devfn
),
1403 quirk
->devid
.vendor
, quirk
->devid
.device
,
1404 quirk
->devid
.subvendor
,
1405 quirk
->devid
.subdevice
);
1407 dev_data
= pci_get_drvdata(quirk
->pdev
);
1409 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1410 field
= cfg_entry
->field
;
1411 if (count
>= PAGE_SIZE
)
1414 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1415 "\t\t%08x:%01x:%08x\n",
1416 cfg_entry
->base_offset
+
1417 field
->offset
, field
->size
,
1423 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1427 static DRIVER_ATTR_RW(quirks
);
1429 static ssize_t
permissive_store(struct device_driver
*drv
, const char *buf
,
1432 int domain
, bus
, slot
, func
;
1434 struct pcistub_device
*psdev
;
1435 struct xen_pcibk_dev_data
*dev_data
;
1437 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1441 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1447 dev_data
= pci_get_drvdata(psdev
->dev
);
1448 /* the driver data for a device should never be null at this point */
1453 if (!dev_data
->permissive
) {
1454 dev_data
->permissive
= 1;
1455 /* Let user know that what they're doing could be unsafe */
1456 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1457 "configuration space accesses!\n");
1458 dev_warn(&psdev
->dev
->dev
,
1459 "permissive mode is potentially unsafe!\n");
1462 pcistub_device_put(psdev
);
1469 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1471 struct pcistub_device
*psdev
;
1472 struct xen_pcibk_dev_data
*dev_data
;
1474 unsigned long flags
;
1475 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1476 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1477 if (count
>= PAGE_SIZE
)
1481 dev_data
= pci_get_drvdata(psdev
->dev
);
1482 if (!dev_data
|| !dev_data
->permissive
)
1485 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1486 pci_name(psdev
->dev
));
1488 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1491 static DRIVER_ATTR_RW(permissive
);
1493 static ssize_t
allow_interrupt_control_store(struct device_driver
*drv
,
1494 const char *buf
, size_t count
)
1496 int domain
, bus
, slot
, func
;
1498 struct pcistub_device
*psdev
;
1499 struct xen_pcibk_dev_data
*dev_data
;
1501 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1505 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1511 dev_data
= pci_get_drvdata(psdev
->dev
);
1512 /* the driver data for a device should never be null at this point */
1517 dev_data
->allow_interrupt_control
= 1;
1519 pcistub_device_put(psdev
);
1526 static ssize_t
allow_interrupt_control_show(struct device_driver
*drv
,
1529 struct pcistub_device
*psdev
;
1530 struct xen_pcibk_dev_data
*dev_data
;
1532 unsigned long flags
;
1534 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1535 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1536 if (count
>= PAGE_SIZE
)
1540 dev_data
= pci_get_drvdata(psdev
->dev
);
1541 if (!dev_data
|| !dev_data
->allow_interrupt_control
)
1544 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1545 pci_name(psdev
->dev
));
1547 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1550 static DRIVER_ATTR_RW(allow_interrupt_control
);
1552 static void pcistub_exit(void)
1554 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1555 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1556 &driver_attr_remove_slot
);
1557 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1558 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1559 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1560 &driver_attr_permissive
);
1561 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1562 &driver_attr_allow_interrupt_control
);
1563 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1564 &driver_attr_irq_handlers
);
1565 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1566 &driver_attr_irq_handler_state
);
1567 pci_unregister_driver(&xen_pcibk_pci_driver
);
1570 static int __init
pcistub_init(void)
1574 int domain
, bus
, slot
, func
;
1577 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1581 err
= sscanf(pci_devs_to_hide
+ pos
,
1582 " (%x:%x:%x.%x) %n",
1583 &domain
, &bus
, &slot
, &func
, &parsed
);
1587 sscanf(pci_devs_to_hide
+ pos
,
1589 &domain
, &bus
, &slot
, &parsed
);
1593 sscanf(pci_devs_to_hide
+ pos
,
1595 &domain
, &bus
, &parsed
);
1601 err
= sscanf(pci_devs_to_hide
+ pos
,
1603 &bus
, &slot
, &func
, &parsed
);
1607 sscanf(pci_devs_to_hide
+ pos
,
1609 &bus
, &slot
, &parsed
);
1613 sscanf(pci_devs_to_hide
+ pos
,
1623 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1628 } while (pci_devs_to_hide
[pos
]);
1631 /* If we're the first PCI Device Driver to register, we're the
1632 * first one to get offered PCI devices as they become
1633 * available (and thus we can be the first to grab them)
1635 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1639 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1640 &driver_attr_new_slot
);
1642 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1643 &driver_attr_remove_slot
);
1645 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1646 &driver_attr_slots
);
1648 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1649 &driver_attr_quirks
);
1651 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1652 &driver_attr_permissive
);
1654 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1655 &driver_attr_allow_interrupt_control
);
1658 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1659 &driver_attr_irq_handlers
);
1661 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1662 &driver_attr_irq_handler_state
);
1670 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1671 pci_devs_to_hide
+ pos
);
1677 * fs_initcall happens before device_initcall
1678 * so xen_pcibk *should* get called first (b/c we
1679 * want to suck up any device before other drivers
1680 * get a chance by being the first pci device
1681 * driver to register)
1683 fs_initcall(pcistub_init
);
1686 #ifdef CONFIG_PCI_IOV
1687 static struct pcistub_device
*find_vfs(const struct pci_dev
*pdev
)
1689 struct pcistub_device
*psdev
= NULL
;
1690 unsigned long flags
;
1693 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1694 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1695 if (!psdev
->pdev
&& psdev
->dev
!= pdev
1696 && pci_physfn(psdev
->dev
) == pdev
) {
1701 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1707 static int pci_stub_notifier(struct notifier_block
*nb
,
1708 unsigned long action
, void *data
)
1710 struct device
*dev
= data
;
1711 const struct pci_dev
*pdev
= to_pci_dev(dev
);
1713 if (action
!= BUS_NOTIFY_UNBIND_DRIVER
)
1716 if (!pdev
->is_physfn
)
1720 struct pcistub_device
*psdev
= find_vfs(pdev
);
1723 device_release_driver(&psdev
->dev
->dev
);
1728 static struct notifier_block pci_stub_nb
= {
1729 .notifier_call
= pci_stub_notifier
,
1733 static int __init
xen_pcibk_init(void)
1737 if (!xen_initial_domain())
1740 err
= xen_pcibk_config_init();
1745 err
= pcistub_init();
1750 pcistub_init_devices_late();
1751 err
= xen_pcibk_xenbus_register();
1754 #ifdef CONFIG_PCI_IOV
1756 bus_register_notifier(&pci_bus_type
, &pci_stub_nb
);
1759 #ifdef CONFIG_XEN_ACPI
1760 xen_acpi_register_get_gsi_func(pcistub_get_gsi_from_sbdf
);
1766 static void __exit
xen_pcibk_cleanup(void)
1768 #ifdef CONFIG_XEN_ACPI
1769 xen_acpi_register_get_gsi_func(NULL
);
1772 #ifdef CONFIG_PCI_IOV
1773 bus_unregister_notifier(&pci_bus_type
, &pci_stub_nb
);
1775 xen_pcibk_xenbus_unregister();
1779 module_init(xen_pcibk_init
);
1780 module_exit(xen_pcibk_cleanup
);
1782 MODULE_DESCRIPTION("Xen PCI-device stub driver");
1783 MODULE_LICENSE("Dual BSD/GPL");
1784 MODULE_ALIAS("xen-backend:pci");