2 * PCI Stub Driver - Grabs devices in backend to be exported later
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
28 static char *pci_devs_to_hide
;
29 wait_queue_head_t xen_pcibk_aer_wait_queue
;
30 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
31 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
33 static DECLARE_RWSEM(pcistub_sem
);
34 module_param_named(hide
, pci_devs_to_hide
, charp
, 0444);
36 struct pcistub_device_id
{
37 struct list_head slot_list
;
42 static LIST_HEAD(pcistub_device_ids
);
43 static DEFINE_SPINLOCK(device_ids_lock
);
45 struct pcistub_device
{
47 struct list_head dev_list
;
51 struct xen_pcibk_device
*pdev
;/* non-NULL if struct pci_dev is in use */
54 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
55 * flag must be locked with pcistub_devices_lock
57 static DEFINE_SPINLOCK(pcistub_devices_lock
);
58 static LIST_HEAD(pcistub_devices
);
60 /* wait for device_initcall before initializing our devices
61 * (see pcistub_init_devices_late)
63 static int initialize_devices
;
64 static LIST_HEAD(seized_devices
);
66 static struct pcistub_device
*pcistub_device_alloc(struct pci_dev
*dev
)
68 struct pcistub_device
*psdev
;
70 dev_dbg(&dev
->dev
, "pcistub_device_alloc\n");
72 psdev
= kzalloc(sizeof(*psdev
), GFP_ATOMIC
);
76 psdev
->dev
= pci_dev_get(dev
);
82 kref_init(&psdev
->kref
);
83 spin_lock_init(&psdev
->lock
);
88 /* Don't call this directly as it's called by pcistub_device_put */
89 static void pcistub_device_release(struct kref
*kref
)
91 struct pcistub_device
*psdev
;
93 struct xen_pcibk_dev_data
*dev_data
;
95 psdev
= container_of(kref
, struct pcistub_device
, kref
);
97 dev_data
= pci_get_drvdata(dev
);
99 dev_dbg(&dev
->dev
, "pcistub_device_release\n");
101 xen_unregister_device_domain_owner(dev
);
103 /* Call the reset function which does not take lock as this
104 * is called from "unbind" which takes a device_lock mutex.
106 __pci_reset_function_locked(dev
);
107 if (pci_load_and_free_saved_state(dev
, &dev_data
->pci_saved_state
))
108 dev_dbg(&dev
->dev
, "Could not reload PCI state\n");
110 pci_restore_state(dev
);
113 struct physdev_pci_device ppdev
= {
114 .seg
= pci_domain_nr(dev
->bus
),
115 .bus
= dev
->bus
->number
,
118 int err
= HYPERVISOR_physdev_op(PHYSDEVOP_release_msix
,
122 dev_warn(&dev
->dev
, "MSI-X release failed (%d)\n",
126 /* Disable the device */
127 xen_pcibk_reset_device(dev
);
130 pci_set_drvdata(dev
, NULL
);
132 /* Clean-up the device */
133 xen_pcibk_config_free_dyn_fields(dev
);
134 xen_pcibk_config_free_dev(dev
);
136 dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
142 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
144 kref_get(&psdev
->kref
);
147 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
149 kref_put(&psdev
->kref
, pcistub_device_release
);
152 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
155 struct pcistub_device
*psdev
= NULL
;
158 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
160 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
161 if (psdev
->dev
!= NULL
162 && domain
== pci_domain_nr(psdev
->dev
->bus
)
163 && bus
== psdev
->dev
->bus
->number
164 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
165 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
166 pcistub_device_get(psdev
);
175 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
179 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
180 struct pcistub_device
*psdev
)
182 struct pci_dev
*pci_dev
= NULL
;
185 pcistub_device_get(psdev
);
187 spin_lock_irqsave(&psdev
->lock
, flags
);
190 pci_dev
= psdev
->dev
;
192 spin_unlock_irqrestore(&psdev
->lock
, flags
);
195 pcistub_device_put(psdev
);
200 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
204 struct pcistub_device
*psdev
;
205 struct pci_dev
*found_dev
= NULL
;
208 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
210 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
211 if (psdev
->dev
!= NULL
212 && domain
== pci_domain_nr(psdev
->dev
->bus
)
213 && bus
== psdev
->dev
->bus
->number
214 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
215 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
216 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
221 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
225 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
228 struct pcistub_device
*psdev
;
229 struct pci_dev
*found_dev
= NULL
;
232 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
234 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
235 if (psdev
->dev
== dev
) {
236 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
241 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
247 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
248 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
249 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
250 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
252 * As such we have to be careful.
254 void pcistub_put_pci_dev(struct pci_dev
*dev
)
256 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
259 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
261 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
262 if (psdev
->dev
== dev
) {
268 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
269 if (WARN_ON(!found_psdev
))
272 /*hold this lock for avoiding breaking link between
273 * pcistub and xen_pcibk when AER is in processing
275 down_write(&pcistub_sem
);
276 /* Cleanup our device
277 * (so it's ready for the next domain)
280 /* This is OK - we are running from workqueue context
281 * and want to inhibit the user from fiddling with 'reset'
283 pci_reset_function(dev
);
284 pci_restore_state(dev
);
286 /* This disables the device. */
287 xen_pcibk_reset_device(dev
);
289 /* And cleanup up our emulated fields. */
290 xen_pcibk_config_reset_dev(dev
);
291 xen_pcibk_config_free_dyn_fields(dev
);
293 xen_unregister_device_domain_owner(dev
);
295 spin_lock_irqsave(&found_psdev
->lock
, flags
);
296 found_psdev
->pdev
= NULL
;
297 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
299 pcistub_device_put(found_psdev
);
300 up_write(&pcistub_sem
);
303 static int pcistub_match_one(struct pci_dev
*dev
,
304 struct pcistub_device_id
*pdev_id
)
306 /* Match the specified device by domain, bus, slot, func and also if
307 * any of the device's parent bridges match.
309 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
310 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
311 && dev
->bus
->number
== pdev_id
->bus
312 && dev
->devfn
== pdev_id
->devfn
)
315 /* Sometimes topmost bridge links to itself. */
316 if (dev
== dev
->bus
->self
)
323 static int pcistub_match(struct pci_dev
*dev
)
325 struct pcistub_device_id
*pdev_id
;
329 spin_lock_irqsave(&device_ids_lock
, flags
);
330 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
331 if (pcistub_match_one(dev
, pdev_id
)) {
336 spin_unlock_irqrestore(&device_ids_lock
, flags
);
341 static int pcistub_init_device(struct pci_dev
*dev
)
343 struct xen_pcibk_dev_data
*dev_data
;
346 dev_dbg(&dev
->dev
, "initializing...\n");
348 /* The PCI backend is not intended to be a module (or to work with
349 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
350 * would need to be called somewhere to free the memory allocated
351 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
353 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
354 + strlen(pci_name(dev
)) + 1, GFP_ATOMIC
);
359 pci_set_drvdata(dev
, dev_data
);
362 * Setup name for fake IRQ handler. It will only be enabled
363 * once the device is turned on by the guest.
365 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
367 dev_dbg(&dev
->dev
, "initializing config\n");
369 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
370 err
= xen_pcibk_config_init_dev(dev
);
374 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
375 * must do this here because pcibios_enable_device may specify
376 * the pci device's true irq (and possibly its other resources)
377 * if they differ from what's in the configuration space.
378 * This makes the assumption that the device's resources won't
379 * change after this point (otherwise this code may break!)
381 dev_dbg(&dev
->dev
, "enabling device\n");
382 err
= pci_enable_device(dev
);
387 struct physdev_pci_device ppdev
= {
388 .seg
= pci_domain_nr(dev
->bus
),
389 .bus
= dev
->bus
->number
,
393 err
= HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix
, &ppdev
);
395 dev_err(&dev
->dev
, "MSI-X preparation failed (%d)\n",
399 /* We need the device active to save the state. */
400 dev_dbg(&dev
->dev
, "save state of device\n");
402 dev_data
->pci_saved_state
= pci_store_saved_state(dev
);
403 if (!dev_data
->pci_saved_state
)
404 dev_err(&dev
->dev
, "Could not store PCI conf saved state!\n");
406 dev_dbg(&dev
->dev
, "resetting (FLR, D3, etc) the device\n");
407 __pci_reset_function_locked(dev
);
408 pci_restore_state(dev
);
410 /* Now disable the device (this also ensures some private device
411 * data is setup before we export)
413 dev_dbg(&dev
->dev
, "reset device\n");
414 xen_pcibk_reset_device(dev
);
416 dev
->dev_flags
|= PCI_DEV_FLAGS_ASSIGNED
;
420 xen_pcibk_config_free_dev(dev
);
423 pci_set_drvdata(dev
, NULL
);
429 * Because some initialization still happens on
430 * devices during fs_initcall, we need to defer
431 * full initialization of our devices until
434 static int __init
pcistub_init_devices_late(void)
436 struct pcistub_device
*psdev
;
440 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
442 while (!list_empty(&seized_devices
)) {
443 psdev
= container_of(seized_devices
.next
,
444 struct pcistub_device
, dev_list
);
445 list_del(&psdev
->dev_list
);
447 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
449 err
= pcistub_init_device(psdev
->dev
);
451 dev_err(&psdev
->dev
->dev
,
452 "error %d initializing device\n", err
);
457 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
460 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
463 initialize_devices
= 1;
465 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
470 static int pcistub_seize(struct pci_dev
*dev
)
472 struct pcistub_device
*psdev
;
476 psdev
= pcistub_device_alloc(dev
);
480 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
482 if (initialize_devices
) {
483 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
485 /* don't want irqs disabled when calling pcistub_init_device */
486 err
= pcistub_init_device(psdev
->dev
);
488 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
491 list_add(&psdev
->dev_list
, &pcistub_devices
);
493 dev_dbg(&dev
->dev
, "deferring initialization\n");
494 list_add(&psdev
->dev_list
, &seized_devices
);
497 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
500 pcistub_device_put(psdev
);
505 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
506 * other functions that take the sysfs lock. */
507 static int pcistub_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
511 dev_dbg(&dev
->dev
, "probing...\n");
513 if (pcistub_match(dev
)) {
515 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
516 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
517 dev_err(&dev
->dev
, "can't export pci devices that "
518 "don't have a normal (0) or bridge (1) "
524 dev_info(&dev
->dev
, "seizing device\n");
525 err
= pcistub_seize(dev
);
527 /* Didn't find the device */
534 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
535 * other functions that take the sysfs lock. */
536 static void pcistub_remove(struct pci_dev
*dev
)
538 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
541 dev_dbg(&dev
->dev
, "removing\n");
543 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
545 xen_pcibk_config_quirk_release(dev
);
547 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
548 if (psdev
->dev
== dev
) {
554 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
557 dev_dbg(&dev
->dev
, "found device to remove - in use? %p\n",
560 if (found_psdev
->pdev
) {
561 pr_warn("****** removing device %s while still in-use! ******\n",
562 pci_name(found_psdev
->dev
));
563 pr_warn("****** driver domain may still access this device's i/o resources!\n");
564 pr_warn("****** shutdown driver domain before binding device\n");
565 pr_warn("****** to other drivers or domains\n");
567 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
569 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
573 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
574 list_del(&found_psdev
->dev_list
);
575 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
577 /* the final put for releasing from the list */
578 pcistub_device_put(found_psdev
);
582 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids
) = {
584 .vendor
= PCI_ANY_ID
,
585 .device
= PCI_ANY_ID
,
586 .subvendor
= PCI_ANY_ID
,
587 .subdevice
= PCI_ANY_ID
,
592 #define PCI_NODENAME_MAX 40
593 static void kill_domain_by_device(struct pcistub_device
*psdev
)
595 struct xenbus_transaction xbt
;
597 char nodename
[PCI_NODENAME_MAX
];
600 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
601 psdev
->pdev
->xdev
->otherend_id
);
604 err
= xenbus_transaction_start(&xbt
);
606 dev_err(&psdev
->dev
->dev
,
607 "error %d when start xenbus transaction\n", err
);
610 /*PV AER handlers will set this flag*/
611 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
612 err
= xenbus_transaction_end(xbt
, 0);
616 dev_err(&psdev
->dev
->dev
,
617 "error %d when end xenbus transaction\n", err
);
622 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
623 * backend need to have cooperation. In xen_pcibk, those steps will do similar
624 * jobs: send service request and waiting for front_end response.
626 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
627 pci_channel_state_t state
, int aer_cmd
,
628 pci_ers_result_t result
)
630 pci_ers_result_t res
= result
;
631 struct xen_pcie_aer_op
*aer_op
;
634 /*with PV AER drivers*/
635 aer_op
= &(psdev
->pdev
->sh_info
->aer_op
);
636 aer_op
->cmd
= aer_cmd
;
637 /*useful for error_detected callback*/
640 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
641 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
643 dev_err(&psdev
->dev
->dev
,
644 DRV_NAME
": failed to get pcifront device\n");
645 return PCI_ERS_RESULT_NONE
;
649 dev_dbg(&psdev
->dev
->dev
,
650 DRV_NAME
": aer_op %x dom %x bus %x devfn %x\n",
651 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
652 /*local flag to mark there's aer request, xen_pcibk callback will use
653 * this flag to judge whether we need to check pci-front give aer
656 set_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
658 /*It is possible that a pcifront conf_read_write ops request invokes
659 * the callback which cause the spurious execution of wake_up.
660 * Yet it is harmless and better than a spinlock here
662 set_bit(_XEN_PCIB_active
,
663 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
665 notify_remote_via_irq(psdev
->pdev
->evtchn_irq
);
667 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
668 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
669 &psdev
->pdev
->sh_info
->flags
)), 300*HZ
);
672 if (test_bit(_XEN_PCIB_active
,
673 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
674 dev_err(&psdev
->dev
->dev
,
675 "pcifront aer process not responding!\n");
676 clear_bit(_XEN_PCIB_active
,
677 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
678 aer_op
->err
= PCI_ERS_RESULT_NONE
;
682 clear_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
684 if (test_bit(_XEN_PCIF_active
,
685 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
686 dev_dbg(&psdev
->dev
->dev
,
687 "schedule pci_conf service in " DRV_NAME
"\n");
688 xen_pcibk_test_and_schedule_op(psdev
->pdev
);
691 res
= (pci_ers_result_t
)aer_op
->err
;
696 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
697 * of the device driver could provide this service, and then wait for pcifront
699 * @dev: pointer to PCI devices
700 * return value is used by aer_core do_recovery policy
702 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
704 struct pcistub_device
*psdev
;
705 pci_ers_result_t result
;
707 result
= PCI_ERS_RESULT_RECOVERED
;
708 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
709 dev
->bus
->number
, dev
->devfn
);
711 down_write(&pcistub_sem
);
712 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
714 PCI_SLOT(dev
->devfn
),
715 PCI_FUNC(dev
->devfn
));
717 if (!psdev
|| !psdev
->pdev
) {
719 DRV_NAME
" device is not found/assigned\n");
723 if (!psdev
->pdev
->sh_info
) {
724 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
725 " by HVM, kill it\n");
726 kill_domain_by_device(psdev
);
730 if (!test_bit(_XEN_PCIB_AERHANDLER
,
731 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
733 "guest with no AER driver should have been killed\n");
736 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_slotreset
, result
);
738 if (result
== PCI_ERS_RESULT_NONE
||
739 result
== PCI_ERS_RESULT_DISCONNECT
) {
741 "No AER slot_reset service or disconnected!\n");
742 kill_domain_by_device(psdev
);
746 pcistub_device_put(psdev
);
747 up_write(&pcistub_sem
);
753 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
754 * in case of the device driver could provide this service, and then wait
756 * @dev: pointer to PCI devices
757 * return value is used by aer_core do_recovery policy
760 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
762 struct pcistub_device
*psdev
;
763 pci_ers_result_t result
;
765 result
= PCI_ERS_RESULT_RECOVERED
;
766 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
767 dev
->bus
->number
, dev
->devfn
);
769 down_write(&pcistub_sem
);
770 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
772 PCI_SLOT(dev
->devfn
),
773 PCI_FUNC(dev
->devfn
));
775 if (!psdev
|| !psdev
->pdev
) {
777 DRV_NAME
" device is not found/assigned\n");
781 if (!psdev
->pdev
->sh_info
) {
782 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
783 " by HVM, kill it\n");
784 kill_domain_by_device(psdev
);
788 if (!test_bit(_XEN_PCIB_AERHANDLER
,
789 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
791 "guest with no AER driver should have been killed\n");
794 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_mmio
, result
);
796 if (result
== PCI_ERS_RESULT_NONE
||
797 result
== PCI_ERS_RESULT_DISCONNECT
) {
799 "No AER mmio_enabled service or disconnected!\n");
800 kill_domain_by_device(psdev
);
804 pcistub_device_put(psdev
);
805 up_write(&pcistub_sem
);
809 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
810 * in case of the device driver could provide this service, and then wait
812 * @dev: pointer to PCI devices
813 * @error: the current PCI connection state
814 * return value is used by aer_core do_recovery policy
817 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
818 pci_channel_state_t error
)
820 struct pcistub_device
*psdev
;
821 pci_ers_result_t result
;
823 result
= PCI_ERS_RESULT_CAN_RECOVER
;
824 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
825 dev
->bus
->number
, dev
->devfn
);
827 down_write(&pcistub_sem
);
828 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
830 PCI_SLOT(dev
->devfn
),
831 PCI_FUNC(dev
->devfn
));
833 if (!psdev
|| !psdev
->pdev
) {
835 DRV_NAME
" device is not found/assigned\n");
839 if (!psdev
->pdev
->sh_info
) {
840 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
841 " by HVM, kill it\n");
842 kill_domain_by_device(psdev
);
846 /*Guest owns the device yet no aer handler regiested, kill guest*/
847 if (!test_bit(_XEN_PCIB_AERHANDLER
,
848 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
849 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
850 kill_domain_by_device(psdev
);
853 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
855 if (result
== PCI_ERS_RESULT_NONE
||
856 result
== PCI_ERS_RESULT_DISCONNECT
) {
858 "No AER error_detected service or disconnected!\n");
859 kill_domain_by_device(psdev
);
863 pcistub_device_put(psdev
);
864 up_write(&pcistub_sem
);
868 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
869 * in case of the device driver could provide this service, and then wait
871 * @dev: pointer to PCI devices
874 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
876 struct pcistub_device
*psdev
;
878 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
879 dev
->bus
->number
, dev
->devfn
);
881 down_write(&pcistub_sem
);
882 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
884 PCI_SLOT(dev
->devfn
),
885 PCI_FUNC(dev
->devfn
));
887 if (!psdev
|| !psdev
->pdev
) {
889 DRV_NAME
" device is not found/assigned\n");
893 if (!psdev
->pdev
->sh_info
) {
894 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
895 " by HVM, kill it\n");
896 kill_domain_by_device(psdev
);
900 if (!test_bit(_XEN_PCIB_AERHANDLER
,
901 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
903 "guest with no AER driver should have been killed\n");
904 kill_domain_by_device(psdev
);
907 common_process(psdev
, 1, XEN_PCI_OP_aer_resume
,
908 PCI_ERS_RESULT_RECOVERED
);
911 pcistub_device_put(psdev
);
912 up_write(&pcistub_sem
);
916 /*add xen_pcibk AER handling*/
917 static const struct pci_error_handlers xen_pcibk_error_handler
= {
918 .error_detected
= xen_pcibk_error_detected
,
919 .mmio_enabled
= xen_pcibk_mmio_enabled
,
920 .slot_reset
= xen_pcibk_slot_reset
,
921 .resume
= xen_pcibk_error_resume
,
925 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
926 * for a normal device. I don't want it to be loaded automatically.
929 static struct pci_driver xen_pcibk_pci_driver
= {
930 /* The name should be xen_pciback, but until the tools are updated
931 * we will keep it as pciback. */
933 .id_table
= pcistub_ids
,
934 .probe
= pcistub_probe
,
935 .remove
= pcistub_remove
,
936 .err_handler
= &xen_pcibk_error_handler
,
939 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
940 int *slot
, int *func
)
944 switch (sscanf(buf
, " %x:%x:%x.%x %n", domain
, bus
, slot
, func
,
948 sscanf(buf
, " %x:%x:%x.* %n", domain
, bus
, slot
, &parsed
);
952 sscanf(buf
, " %x:%x:*.* %n", domain
, bus
, &parsed
);
955 if (parsed
&& !buf
[parsed
])
958 /* try again without domain */
960 switch (sscanf(buf
, " %x:%x.%x %n", bus
, slot
, func
, &parsed
)) {
963 sscanf(buf
, " %x:%x.* %n", bus
, slot
, &parsed
);
967 sscanf(buf
, " %x:*.* %n", bus
, &parsed
);
970 if (parsed
&& !buf
[parsed
])
976 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
977 *slot
, int *func
, int *reg
, int *size
, int *mask
)
981 sscanf(buf
, " %x:%x:%x.%x-%x:%x:%x %n", domain
, bus
, slot
, func
,
982 reg
, size
, mask
, &parsed
);
983 if (parsed
&& !buf
[parsed
])
986 /* try again without domain */
988 sscanf(buf
, " %x:%x.%x-%x:%x:%x %n", bus
, slot
, func
, reg
, size
,
990 if (parsed
&& !buf
[parsed
])
996 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
998 struct pcistub_device_id
*pci_dev_id
;
1000 int rc
= 0, devfn
= PCI_DEVFN(slot
, func
);
1003 for (slot
= 0; !rc
&& slot
< 32; ++slot
)
1004 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1009 for (func
= 0; !rc
&& func
< 8; ++func
)
1010 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1015 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1016 || !defined(CONFIG_PCI_DOMAINS)
1017 !pci_domains_supported
? domain
:
1019 domain
< 0 || domain
> 0xffff)
1020 || bus
< 0 || bus
> 0xff
1021 || PCI_SLOT(devfn
) != slot
1022 || PCI_FUNC(devfn
) != func
)
1025 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
1029 pci_dev_id
->domain
= domain
;
1030 pci_dev_id
->bus
= bus
;
1031 pci_dev_id
->devfn
= devfn
;
1033 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1034 domain
, bus
, slot
, func
);
1036 spin_lock_irqsave(&device_ids_lock
, flags
);
1037 list_add_tail(&pci_dev_id
->slot_list
, &pcistub_device_ids
);
1038 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1043 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
1045 struct pcistub_device_id
*pci_dev_id
, *t
;
1047 unsigned long flags
;
1049 spin_lock_irqsave(&device_ids_lock
, flags
);
1050 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
1052 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
1053 && (slot
< 0 || PCI_SLOT(pci_dev_id
->devfn
) == slot
)
1054 && (func
< 0 || PCI_FUNC(pci_dev_id
->devfn
) == func
)) {
1055 /* Don't break; here because it's possible the same
1056 * slot could be in the list more than once
1058 list_del(&pci_dev_id
->slot_list
);
1063 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1064 domain
, bus
, slot
, func
);
1067 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1072 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
,
1073 unsigned int reg
, unsigned int size
,
1077 struct pcistub_device
*psdev
;
1078 struct pci_dev
*dev
;
1079 struct config_field
*field
;
1081 if (reg
> 0xfff || (size
< 4 && (mask
>> (size
* 8))))
1084 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1091 field
= kzalloc(sizeof(*field
), GFP_ATOMIC
);
1097 field
->offset
= reg
;
1101 field
->reset
= NULL
;
1102 field
->release
= NULL
;
1103 field
->clean
= xen_pcibk_config_field_free
;
1105 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
1110 pcistub_device_put(psdev
);
1114 static ssize_t
pcistub_slot_add(struct device_driver
*drv
, const char *buf
,
1117 int domain
, bus
, slot
, func
;
1120 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1124 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1131 static DRIVER_ATTR(new_slot
, S_IWUSR
, NULL
, pcistub_slot_add
);
1133 static ssize_t
pcistub_slot_remove(struct device_driver
*drv
, const char *buf
,
1136 int domain
, bus
, slot
, func
;
1139 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1143 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1150 static DRIVER_ATTR(remove_slot
, S_IWUSR
, NULL
, pcistub_slot_remove
);
1152 static ssize_t
pcistub_slot_show(struct device_driver
*drv
, char *buf
)
1154 struct pcistub_device_id
*pci_dev_id
;
1156 unsigned long flags
;
1158 spin_lock_irqsave(&device_ids_lock
, flags
);
1159 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1160 if (count
>= PAGE_SIZE
)
1163 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1164 "%04x:%02x:%02x.%d\n",
1165 pci_dev_id
->domain
, pci_dev_id
->bus
,
1166 PCI_SLOT(pci_dev_id
->devfn
),
1167 PCI_FUNC(pci_dev_id
->devfn
));
1169 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1173 static DRIVER_ATTR(slots
, S_IRUSR
, pcistub_slot_show
, NULL
);
1175 static ssize_t
pcistub_irq_handler_show(struct device_driver
*drv
, char *buf
)
1177 struct pcistub_device
*psdev
;
1178 struct xen_pcibk_dev_data
*dev_data
;
1180 unsigned long flags
;
1182 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1183 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1184 if (count
>= PAGE_SIZE
)
1188 dev_data
= pci_get_drvdata(psdev
->dev
);
1192 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1193 "%s:%s:%sing:%ld\n",
1194 pci_name(psdev
->dev
),
1195 dev_data
->isr_on
? "on" : "off",
1196 dev_data
->ack_intr
? "ack" : "not ack",
1199 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1202 static DRIVER_ATTR(irq_handlers
, S_IRUSR
, pcistub_irq_handler_show
, NULL
);
1204 static ssize_t
pcistub_irq_handler_switch(struct device_driver
*drv
,
1208 struct pcistub_device
*psdev
;
1209 struct xen_pcibk_dev_data
*dev_data
;
1210 int domain
, bus
, slot
, func
;
1213 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1217 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1223 dev_data
= pci_get_drvdata(psdev
->dev
);
1229 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1230 dev_data
->irq_name
, dev_data
->isr_on
,
1233 dev_data
->isr_on
= !(dev_data
->isr_on
);
1234 if (dev_data
->isr_on
)
1235 dev_data
->ack_intr
= 1;
1238 pcistub_device_put(psdev
);
1243 static DRIVER_ATTR(irq_handler_state
, S_IWUSR
, NULL
,
1244 pcistub_irq_handler_switch
);
1246 static ssize_t
pcistub_quirk_add(struct device_driver
*drv
, const char *buf
,
1249 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1252 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1257 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1265 static ssize_t
pcistub_quirk_show(struct device_driver
*drv
, char *buf
)
1268 unsigned long flags
;
1269 struct xen_pcibk_config_quirk
*quirk
;
1270 struct xen_pcibk_dev_data
*dev_data
;
1271 const struct config_field
*field
;
1272 const struct config_field_entry
*cfg_entry
;
1274 spin_lock_irqsave(&device_ids_lock
, flags
);
1275 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1276 if (count
>= PAGE_SIZE
)
1279 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1280 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1281 quirk
->pdev
->bus
->number
,
1282 PCI_SLOT(quirk
->pdev
->devfn
),
1283 PCI_FUNC(quirk
->pdev
->devfn
),
1284 quirk
->devid
.vendor
, quirk
->devid
.device
,
1285 quirk
->devid
.subvendor
,
1286 quirk
->devid
.subdevice
);
1288 dev_data
= pci_get_drvdata(quirk
->pdev
);
1290 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1291 field
= cfg_entry
->field
;
1292 if (count
>= PAGE_SIZE
)
1295 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1296 "\t\t%08x:%01x:%08x\n",
1297 cfg_entry
->base_offset
+
1298 field
->offset
, field
->size
,
1304 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1308 static DRIVER_ATTR(quirks
, S_IRUSR
| S_IWUSR
, pcistub_quirk_show
,
1311 static ssize_t
permissive_add(struct device_driver
*drv
, const char *buf
,
1314 int domain
, bus
, slot
, func
;
1316 struct pcistub_device
*psdev
;
1317 struct xen_pcibk_dev_data
*dev_data
;
1319 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1323 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1329 dev_data
= pci_get_drvdata(psdev
->dev
);
1330 /* the driver data for a device should never be null at this point */
1335 if (!dev_data
->permissive
) {
1336 dev_data
->permissive
= 1;
1337 /* Let user know that what they're doing could be unsafe */
1338 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1339 "configuration space accesses!\n");
1340 dev_warn(&psdev
->dev
->dev
,
1341 "permissive mode is potentially unsafe!\n");
1344 pcistub_device_put(psdev
);
1351 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1353 struct pcistub_device
*psdev
;
1354 struct xen_pcibk_dev_data
*dev_data
;
1356 unsigned long flags
;
1357 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1358 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1359 if (count
>= PAGE_SIZE
)
1363 dev_data
= pci_get_drvdata(psdev
->dev
);
1364 if (!dev_data
|| !dev_data
->permissive
)
1367 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1368 pci_name(psdev
->dev
));
1370 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1373 static DRIVER_ATTR(permissive
, S_IRUSR
| S_IWUSR
, permissive_show
,
1376 static void pcistub_exit(void)
1378 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1379 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1380 &driver_attr_remove_slot
);
1381 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1382 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1383 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1384 &driver_attr_permissive
);
1385 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1386 &driver_attr_irq_handlers
);
1387 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1388 &driver_attr_irq_handler_state
);
1389 pci_unregister_driver(&xen_pcibk_pci_driver
);
1392 static int __init
pcistub_init(void)
1396 int domain
, bus
, slot
, func
;
1399 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1403 err
= sscanf(pci_devs_to_hide
+ pos
,
1404 " (%x:%x:%x.%x) %n",
1405 &domain
, &bus
, &slot
, &func
, &parsed
);
1409 sscanf(pci_devs_to_hide
+ pos
,
1411 &domain
, &bus
, &slot
, &parsed
);
1415 sscanf(pci_devs_to_hide
+ pos
,
1417 &domain
, &bus
, &parsed
);
1423 err
= sscanf(pci_devs_to_hide
+ pos
,
1425 &bus
, &slot
, &func
, &parsed
);
1429 sscanf(pci_devs_to_hide
+ pos
,
1431 &bus
, &slot
, &parsed
);
1435 sscanf(pci_devs_to_hide
+ pos
,
1445 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1450 } while (pci_devs_to_hide
[pos
]);
1453 /* If we're the first PCI Device Driver to register, we're the
1454 * first one to get offered PCI devices as they become
1455 * available (and thus we can be the first to grab them)
1457 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1461 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1462 &driver_attr_new_slot
);
1464 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1465 &driver_attr_remove_slot
);
1467 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1468 &driver_attr_slots
);
1470 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1471 &driver_attr_quirks
);
1473 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1474 &driver_attr_permissive
);
1477 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1478 &driver_attr_irq_handlers
);
1480 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1481 &driver_attr_irq_handler_state
);
1489 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1490 pci_devs_to_hide
+ pos
);
1496 * fs_initcall happens before device_initcall
1497 * so xen_pcibk *should* get called first (b/c we
1498 * want to suck up any device before other drivers
1499 * get a chance by being the first pci device
1500 * driver to register)
1502 fs_initcall(pcistub_init
);
1505 static int __init
xen_pcibk_init(void)
1509 if (!xen_initial_domain())
1512 err
= xen_pcibk_config_init();
1517 err
= pcistub_init();
1522 pcistub_init_devices_late();
1523 err
= xen_pcibk_xenbus_register();
1530 static void __exit
xen_pcibk_cleanup(void)
1532 xen_pcibk_xenbus_unregister();
1536 module_init(xen_pcibk_init
);
1537 module_exit(xen_pcibk_cleanup
);
1539 MODULE_LICENSE("Dual BSD/GPL");
1540 MODULE_ALIAS("xen-backend:pci");