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>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/rwsem.h>
10 #include <linux/list.h>
11 #include <linux/spinlock.h>
12 #include <linux/kref.h>
13 #include <linux/pci.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/atomic.h>
17 #include <xen/events.h>
18 #include <asm/xen/pci.h>
19 #include <asm/xen/hypervisor.h>
21 #include "conf_space.h"
22 #include "conf_space_quirks.h"
24 static char *pci_devs_to_hide
;
25 wait_queue_head_t xen_pcibk_aer_wait_queue
;
26 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
27 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
29 static DECLARE_RWSEM(pcistub_sem
);
30 module_param_named(hide
, pci_devs_to_hide
, charp
, 0444);
32 struct pcistub_device_id
{
33 struct list_head slot_list
;
38 static LIST_HEAD(pcistub_device_ids
);
39 static DEFINE_SPINLOCK(device_ids_lock
);
41 struct pcistub_device
{
43 struct list_head dev_list
;
47 struct xen_pcibk_device
*pdev
;/* non-NULL if struct pci_dev is in use */
50 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
51 * flag must be locked with pcistub_devices_lock
53 static DEFINE_SPINLOCK(pcistub_devices_lock
);
54 static LIST_HEAD(pcistub_devices
);
56 /* wait for device_initcall before initializing our devices
57 * (see pcistub_init_devices_late)
59 static int initialize_devices
;
60 static LIST_HEAD(seized_devices
);
62 static struct pcistub_device
*pcistub_device_alloc(struct pci_dev
*dev
)
64 struct pcistub_device
*psdev
;
66 dev_dbg(&dev
->dev
, "pcistub_device_alloc\n");
68 psdev
= kzalloc(sizeof(*psdev
), GFP_ATOMIC
);
72 psdev
->dev
= pci_dev_get(dev
);
78 kref_init(&psdev
->kref
);
79 spin_lock_init(&psdev
->lock
);
84 /* Don't call this directly as it's called by pcistub_device_put */
85 static void pcistub_device_release(struct kref
*kref
)
87 struct pcistub_device
*psdev
;
88 struct xen_pcibk_dev_data
*dev_data
;
90 psdev
= container_of(kref
, struct pcistub_device
, kref
);
91 dev_data
= pci_get_drvdata(psdev
->dev
);
93 dev_dbg(&psdev
->dev
->dev
, "pcistub_device_release\n");
95 xen_unregister_device_domain_owner(psdev
->dev
);
97 /* Call the reset function which does not take lock as this
98 * is called from "unbind" which takes a device_lock mutex.
100 __pci_reset_function_locked(psdev
->dev
);
101 if (pci_load_and_free_saved_state(psdev
->dev
,
102 &dev_data
->pci_saved_state
)) {
103 dev_dbg(&psdev
->dev
->dev
, "Could not reload PCI state\n");
105 pci_restore_state(psdev
->dev
);
107 /* Disable the device */
108 xen_pcibk_reset_device(psdev
->dev
);
111 pci_set_drvdata(psdev
->dev
, NULL
);
113 /* Clean-up the device */
114 xen_pcibk_config_free_dyn_fields(psdev
->dev
);
115 xen_pcibk_config_free_dev(psdev
->dev
);
117 psdev
->dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
118 pci_dev_put(psdev
->dev
);
123 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
125 kref_get(&psdev
->kref
);
128 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
130 kref_put(&psdev
->kref
, pcistub_device_release
);
133 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
136 struct pcistub_device
*psdev
= NULL
;
139 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
141 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
142 if (psdev
->dev
!= NULL
143 && domain
== pci_domain_nr(psdev
->dev
->bus
)
144 && bus
== psdev
->dev
->bus
->number
145 && PCI_DEVFN(slot
, func
) == psdev
->dev
->devfn
) {
146 pcistub_device_get(psdev
);
155 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
159 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
160 struct pcistub_device
*psdev
)
162 struct pci_dev
*pci_dev
= NULL
;
165 pcistub_device_get(psdev
);
167 spin_lock_irqsave(&psdev
->lock
, flags
);
170 pci_dev
= psdev
->dev
;
172 spin_unlock_irqrestore(&psdev
->lock
, flags
);
175 pcistub_device_put(psdev
);
180 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
184 struct pcistub_device
*psdev
;
185 struct pci_dev
*found_dev
= NULL
;
188 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
190 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
191 if (psdev
->dev
!= NULL
192 && domain
== pci_domain_nr(psdev
->dev
->bus
)
193 && bus
== psdev
->dev
->bus
->number
194 && PCI_DEVFN(slot
, func
) == psdev
->dev
->devfn
) {
195 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
200 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
204 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
207 struct pcistub_device
*psdev
;
208 struct pci_dev
*found_dev
= NULL
;
211 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
213 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
214 if (psdev
->dev
== dev
) {
215 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
220 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
224 void pcistub_put_pci_dev(struct pci_dev
*dev
)
226 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
229 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
231 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
232 if (psdev
->dev
== dev
) {
238 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
239 if (WARN_ON(!found_psdev
))
242 /*hold this lock for avoiding breaking link between
243 * pcistub and xen_pcibk when AER is in processing
245 down_write(&pcistub_sem
);
246 /* Cleanup our device
247 * (so it's ready for the next domain)
250 /* This is OK - we are running from workqueue context
251 * and want to inhibit the user from fiddling with 'reset'
253 pci_reset_function(dev
);
254 pci_restore_state(psdev
->dev
);
256 /* This disables the device. */
257 xen_pcibk_reset_device(found_psdev
->dev
);
259 /* And cleanup up our emulated fields. */
260 xen_pcibk_config_free_dyn_fields(found_psdev
->dev
);
261 xen_pcibk_config_reset_dev(found_psdev
->dev
);
263 xen_unregister_device_domain_owner(found_psdev
->dev
);
265 spin_lock_irqsave(&found_psdev
->lock
, flags
);
266 found_psdev
->pdev
= NULL
;
267 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
269 pcistub_device_put(found_psdev
);
270 up_write(&pcistub_sem
);
273 static int __devinit
pcistub_match_one(struct pci_dev
*dev
,
274 struct pcistub_device_id
*pdev_id
)
276 /* Match the specified device by domain, bus, slot, func and also if
277 * any of the device's parent bridges match.
279 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
280 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
281 && dev
->bus
->number
== pdev_id
->bus
282 && dev
->devfn
== pdev_id
->devfn
)
285 /* Sometimes topmost bridge links to itself. */
286 if (dev
== dev
->bus
->self
)
293 static int __devinit
pcistub_match(struct pci_dev
*dev
)
295 struct pcistub_device_id
*pdev_id
;
299 spin_lock_irqsave(&device_ids_lock
, flags
);
300 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
301 if (pcistub_match_one(dev
, pdev_id
)) {
306 spin_unlock_irqrestore(&device_ids_lock
, flags
);
311 static int __devinit
pcistub_init_device(struct pci_dev
*dev
)
313 struct xen_pcibk_dev_data
*dev_data
;
316 dev_dbg(&dev
->dev
, "initializing...\n");
318 /* The PCI backend is not intended to be a module (or to work with
319 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
320 * would need to be called somewhere to free the memory allocated
321 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
323 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
324 + strlen(pci_name(dev
)) + 1, GFP_ATOMIC
);
329 pci_set_drvdata(dev
, dev_data
);
332 * Setup name for fake IRQ handler. It will only be enabled
333 * once the device is turned on by the guest.
335 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
337 dev_dbg(&dev
->dev
, "initializing config\n");
339 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
340 err
= xen_pcibk_config_init_dev(dev
);
344 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
345 * must do this here because pcibios_enable_device may specify
346 * the pci device's true irq (and possibly its other resources)
347 * if they differ from what's in the configuration space.
348 * This makes the assumption that the device's resources won't
349 * change after this point (otherwise this code may break!)
351 dev_dbg(&dev
->dev
, "enabling device\n");
352 err
= pci_enable_device(dev
);
356 /* We need the device active to save the state. */
357 dev_dbg(&dev
->dev
, "save state of device\n");
359 dev_data
->pci_saved_state
= pci_store_saved_state(dev
);
360 if (!dev_data
->pci_saved_state
)
361 dev_err(&dev
->dev
, "Could not store PCI conf saved state!\n");
363 dev_dbg(&dev
->dev
, "reseting (FLR, D3, etc) the device\n");
364 __pci_reset_function_locked(dev
);
365 pci_restore_state(dev
);
367 /* Now disable the device (this also ensures some private device
368 * data is setup before we export)
370 dev_dbg(&dev
->dev
, "reset device\n");
371 xen_pcibk_reset_device(dev
);
373 dev
->dev_flags
|= PCI_DEV_FLAGS_ASSIGNED
;
377 xen_pcibk_config_free_dev(dev
);
380 pci_set_drvdata(dev
, NULL
);
386 * Because some initialization still happens on
387 * devices during fs_initcall, we need to defer
388 * full initialization of our devices until
391 static int __init
pcistub_init_devices_late(void)
393 struct pcistub_device
*psdev
;
397 pr_debug(DRV_NAME
": pcistub_init_devices_late\n");
399 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
401 while (!list_empty(&seized_devices
)) {
402 psdev
= container_of(seized_devices
.next
,
403 struct pcistub_device
, dev_list
);
404 list_del(&psdev
->dev_list
);
406 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
408 err
= pcistub_init_device(psdev
->dev
);
410 dev_err(&psdev
->dev
->dev
,
411 "error %d initializing device\n", err
);
416 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
419 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
422 initialize_devices
= 1;
424 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
429 static int __devinit
pcistub_seize(struct pci_dev
*dev
)
431 struct pcistub_device
*psdev
;
435 psdev
= pcistub_device_alloc(dev
);
439 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
441 if (initialize_devices
) {
442 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
444 /* don't want irqs disabled when calling pcistub_init_device */
445 err
= pcistub_init_device(psdev
->dev
);
447 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
450 list_add(&psdev
->dev_list
, &pcistub_devices
);
452 dev_dbg(&dev
->dev
, "deferring initialization\n");
453 list_add(&psdev
->dev_list
, &seized_devices
);
456 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
459 pcistub_device_put(psdev
);
464 static int __devinit
pcistub_probe(struct pci_dev
*dev
,
465 const struct pci_device_id
*id
)
469 dev_dbg(&dev
->dev
, "probing...\n");
471 if (pcistub_match(dev
)) {
473 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
474 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
475 dev_err(&dev
->dev
, "can't export pci devices that "
476 "don't have a normal (0) or bridge (1) "
482 dev_info(&dev
->dev
, "seizing device\n");
483 err
= pcistub_seize(dev
);
485 /* Didn't find the device */
492 static void pcistub_remove(struct pci_dev
*dev
)
494 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
497 dev_dbg(&dev
->dev
, "removing\n");
499 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
501 xen_pcibk_config_quirk_release(dev
);
503 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
504 if (psdev
->dev
== dev
) {
510 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
513 dev_dbg(&dev
->dev
, "found device to remove - in use? %p\n",
516 if (found_psdev
->pdev
) {
517 printk(KERN_WARNING DRV_NAME
": ****** removing device "
518 "%s while still in-use! ******\n",
519 pci_name(found_psdev
->dev
));
520 printk(KERN_WARNING DRV_NAME
": ****** driver domain may"
521 " still access this device's i/o resources!\n");
522 printk(KERN_WARNING DRV_NAME
": ****** shutdown driver "
523 "domain before binding device\n");
524 printk(KERN_WARNING DRV_NAME
": ****** to other drivers "
527 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
531 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
532 list_del(&found_psdev
->dev_list
);
533 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
535 /* the final put for releasing from the list */
536 pcistub_device_put(found_psdev
);
540 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids
) = {
542 .vendor
= PCI_ANY_ID
,
543 .device
= PCI_ANY_ID
,
544 .subvendor
= PCI_ANY_ID
,
545 .subdevice
= PCI_ANY_ID
,
550 #define PCI_NODENAME_MAX 40
551 static void kill_domain_by_device(struct pcistub_device
*psdev
)
553 struct xenbus_transaction xbt
;
555 char nodename
[PCI_NODENAME_MAX
];
558 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
559 psdev
->pdev
->xdev
->otherend_id
);
562 err
= xenbus_transaction_start(&xbt
);
564 dev_err(&psdev
->dev
->dev
,
565 "error %d when start xenbus transaction\n", err
);
568 /*PV AER handlers will set this flag*/
569 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
570 err
= xenbus_transaction_end(xbt
, 0);
574 dev_err(&psdev
->dev
->dev
,
575 "error %d when end xenbus transaction\n", err
);
580 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
581 * backend need to have cooperation. In xen_pcibk, those steps will do similar
582 * jobs: send service request and waiting for front_end response.
584 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
585 pci_channel_state_t state
, int aer_cmd
,
586 pci_ers_result_t result
)
588 pci_ers_result_t res
= result
;
589 struct xen_pcie_aer_op
*aer_op
;
592 /*with PV AER drivers*/
593 aer_op
= &(psdev
->pdev
->sh_info
->aer_op
);
594 aer_op
->cmd
= aer_cmd
;
595 /*useful for error_detected callback*/
598 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
599 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
601 dev_err(&psdev
->dev
->dev
,
602 DRV_NAME
": failed to get pcifront device\n");
603 return PCI_ERS_RESULT_NONE
;
607 dev_dbg(&psdev
->dev
->dev
,
608 DRV_NAME
": aer_op %x dom %x bus %x devfn %x\n",
609 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
610 /*local flag to mark there's aer request, xen_pcibk callback will use
611 * this flag to judge whether we need to check pci-front give aer
614 set_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
616 /*It is possible that a pcifront conf_read_write ops request invokes
617 * the callback which cause the spurious execution of wake_up.
618 * Yet it is harmless and better than a spinlock here
620 set_bit(_XEN_PCIB_active
,
621 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
623 notify_remote_via_irq(psdev
->pdev
->evtchn_irq
);
625 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
626 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
627 &psdev
->pdev
->sh_info
->flags
)), 300*HZ
);
630 if (test_bit(_XEN_PCIB_active
,
631 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
632 dev_err(&psdev
->dev
->dev
,
633 "pcifront aer process not responding!\n");
634 clear_bit(_XEN_PCIB_active
,
635 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
636 aer_op
->err
= PCI_ERS_RESULT_NONE
;
640 clear_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
642 if (test_bit(_XEN_PCIF_active
,
643 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
644 dev_dbg(&psdev
->dev
->dev
,
645 "schedule pci_conf service in " DRV_NAME
"\n");
646 xen_pcibk_test_and_schedule_op(psdev
->pdev
);
649 res
= (pci_ers_result_t
)aer_op
->err
;
654 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
655 * of the device driver could provide this service, and then wait for pcifront
657 * @dev: pointer to PCI devices
658 * return value is used by aer_core do_recovery policy
660 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
662 struct pcistub_device
*psdev
;
663 pci_ers_result_t result
;
665 result
= PCI_ERS_RESULT_RECOVERED
;
666 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
667 dev
->bus
->number
, dev
->devfn
);
669 down_write(&pcistub_sem
);
670 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
672 PCI_SLOT(dev
->devfn
),
673 PCI_FUNC(dev
->devfn
));
675 if (!psdev
|| !psdev
->pdev
) {
677 DRV_NAME
" device is not found/assigned\n");
681 if (!psdev
->pdev
->sh_info
) {
682 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
683 " by HVM, kill it\n");
684 kill_domain_by_device(psdev
);
688 if (!test_bit(_XEN_PCIB_AERHANDLER
,
689 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
691 "guest with no AER driver should have been killed\n");
694 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_slotreset
, result
);
696 if (result
== PCI_ERS_RESULT_NONE
||
697 result
== PCI_ERS_RESULT_DISCONNECT
) {
699 "No AER slot_reset service or disconnected!\n");
700 kill_domain_by_device(psdev
);
703 pcistub_device_put(psdev
);
705 up_write(&pcistub_sem
);
711 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
712 * in case of the device driver could provide this service, and then wait
714 * @dev: pointer to PCI devices
715 * return value is used by aer_core do_recovery policy
718 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
720 struct pcistub_device
*psdev
;
721 pci_ers_result_t result
;
723 result
= PCI_ERS_RESULT_RECOVERED
;
724 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
725 dev
->bus
->number
, dev
->devfn
);
727 down_write(&pcistub_sem
);
728 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
730 PCI_SLOT(dev
->devfn
),
731 PCI_FUNC(dev
->devfn
));
733 if (!psdev
|| !psdev
->pdev
) {
735 DRV_NAME
" device is not found/assigned\n");
739 if (!psdev
->pdev
->sh_info
) {
740 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
741 " by HVM, kill it\n");
742 kill_domain_by_device(psdev
);
746 if (!test_bit(_XEN_PCIB_AERHANDLER
,
747 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
749 "guest with no AER driver should have been killed\n");
752 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_mmio
, result
);
754 if (result
== PCI_ERS_RESULT_NONE
||
755 result
== PCI_ERS_RESULT_DISCONNECT
) {
757 "No AER mmio_enabled service or disconnected!\n");
758 kill_domain_by_device(psdev
);
761 pcistub_device_put(psdev
);
763 up_write(&pcistub_sem
);
767 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
768 * in case of the device driver could provide this service, and then wait
770 * @dev: pointer to PCI devices
771 * @error: the current PCI connection state
772 * return value is used by aer_core do_recovery policy
775 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
776 pci_channel_state_t error
)
778 struct pcistub_device
*psdev
;
779 pci_ers_result_t result
;
781 result
= PCI_ERS_RESULT_CAN_RECOVER
;
782 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
783 dev
->bus
->number
, dev
->devfn
);
785 down_write(&pcistub_sem
);
786 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
788 PCI_SLOT(dev
->devfn
),
789 PCI_FUNC(dev
->devfn
));
791 if (!psdev
|| !psdev
->pdev
) {
793 DRV_NAME
" device is not found/assigned\n");
797 if (!psdev
->pdev
->sh_info
) {
798 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
799 " by HVM, kill it\n");
800 kill_domain_by_device(psdev
);
804 /*Guest owns the device yet no aer handler regiested, kill guest*/
805 if (!test_bit(_XEN_PCIB_AERHANDLER
,
806 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
807 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
808 kill_domain_by_device(psdev
);
811 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
813 if (result
== PCI_ERS_RESULT_NONE
||
814 result
== PCI_ERS_RESULT_DISCONNECT
) {
816 "No AER error_detected service or disconnected!\n");
817 kill_domain_by_device(psdev
);
820 pcistub_device_put(psdev
);
822 up_write(&pcistub_sem
);
826 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
827 * in case of the device driver could provide this service, and then wait
829 * @dev: pointer to PCI devices
832 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
834 struct pcistub_device
*psdev
;
836 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
837 dev
->bus
->number
, dev
->devfn
);
839 down_write(&pcistub_sem
);
840 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
842 PCI_SLOT(dev
->devfn
),
843 PCI_FUNC(dev
->devfn
));
845 if (!psdev
|| !psdev
->pdev
) {
847 DRV_NAME
" device is not found/assigned\n");
851 if (!psdev
->pdev
->sh_info
) {
852 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
853 " by HVM, kill it\n");
854 kill_domain_by_device(psdev
);
858 if (!test_bit(_XEN_PCIB_AERHANDLER
,
859 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
861 "guest with no AER driver should have been killed\n");
862 kill_domain_by_device(psdev
);
865 common_process(psdev
, 1, XEN_PCI_OP_aer_resume
,
866 PCI_ERS_RESULT_RECOVERED
);
868 pcistub_device_put(psdev
);
870 up_write(&pcistub_sem
);
874 /*add xen_pcibk AER handling*/
875 static struct pci_error_handlers xen_pcibk_error_handler
= {
876 .error_detected
= xen_pcibk_error_detected
,
877 .mmio_enabled
= xen_pcibk_mmio_enabled
,
878 .slot_reset
= xen_pcibk_slot_reset
,
879 .resume
= xen_pcibk_error_resume
,
883 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
884 * for a normal device. I don't want it to be loaded automatically.
887 static struct pci_driver xen_pcibk_pci_driver
= {
888 /* The name should be xen_pciback, but until the tools are updated
889 * we will keep it as pciback. */
891 .id_table
= pcistub_ids
,
892 .probe
= pcistub_probe
,
893 .remove
= pcistub_remove
,
894 .err_handler
= &xen_pcibk_error_handler
,
897 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
898 int *slot
, int *func
)
902 err
= sscanf(buf
, " %x:%x:%x.%x", domain
, bus
, slot
, func
);
908 /* try again without domain */
910 err
= sscanf(buf
, " %x:%x.%x", bus
, slot
, func
);
917 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
918 *slot
, int *func
, int *reg
, int *size
, int *mask
)
923 sscanf(buf
, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain
, bus
, slot
,
924 func
, reg
, size
, mask
);
930 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
932 struct pcistub_device_id
*pci_dev_id
;
935 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
939 pci_dev_id
->domain
= domain
;
940 pci_dev_id
->bus
= bus
;
941 pci_dev_id
->devfn
= PCI_DEVFN(slot
, func
);
943 pr_debug(DRV_NAME
": wants to seize %04x:%02x:%02x.%d\n",
944 domain
, bus
, slot
, func
);
946 spin_lock_irqsave(&device_ids_lock
, flags
);
947 list_add_tail(&pci_dev_id
->slot_list
, &pcistub_device_ids
);
948 spin_unlock_irqrestore(&device_ids_lock
, flags
);
953 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
955 struct pcistub_device_id
*pci_dev_id
, *t
;
956 int devfn
= PCI_DEVFN(slot
, func
);
960 spin_lock_irqsave(&device_ids_lock
, flags
);
961 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
963 if (pci_dev_id
->domain
== domain
964 && pci_dev_id
->bus
== bus
&& pci_dev_id
->devfn
== devfn
) {
965 /* Don't break; here because it's possible the same
966 * slot could be in the list more than once
968 list_del(&pci_dev_id
->slot_list
);
973 pr_debug(DRV_NAME
": removed %04x:%02x:%02x.%d from "
974 "seize list\n", domain
, bus
, slot
, func
);
977 spin_unlock_irqrestore(&device_ids_lock
, flags
);
982 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
, int reg
,
986 struct pcistub_device
*psdev
;
988 struct config_field
*field
;
990 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
991 if (!psdev
|| !psdev
->dev
) {
997 field
= kzalloc(sizeof(*field
), GFP_ATOMIC
);
1003 field
->offset
= reg
;
1007 field
->reset
= NULL
;
1008 field
->release
= NULL
;
1009 field
->clean
= xen_pcibk_config_field_free
;
1011 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
1018 static ssize_t
pcistub_slot_add(struct device_driver
*drv
, const char *buf
,
1021 int domain
, bus
, slot
, func
;
1024 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1028 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1035 static DRIVER_ATTR(new_slot
, S_IWUSR
, NULL
, pcistub_slot_add
);
1037 static ssize_t
pcistub_slot_remove(struct device_driver
*drv
, const char *buf
,
1040 int domain
, bus
, slot
, func
;
1043 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1047 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1054 static DRIVER_ATTR(remove_slot
, S_IWUSR
, NULL
, pcistub_slot_remove
);
1056 static ssize_t
pcistub_slot_show(struct device_driver
*drv
, char *buf
)
1058 struct pcistub_device_id
*pci_dev_id
;
1060 unsigned long flags
;
1062 spin_lock_irqsave(&device_ids_lock
, flags
);
1063 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1064 if (count
>= PAGE_SIZE
)
1067 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1068 "%04x:%02x:%02x.%d\n",
1069 pci_dev_id
->domain
, pci_dev_id
->bus
,
1070 PCI_SLOT(pci_dev_id
->devfn
),
1071 PCI_FUNC(pci_dev_id
->devfn
));
1073 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1077 static DRIVER_ATTR(slots
, S_IRUSR
, pcistub_slot_show
, NULL
);
1079 static ssize_t
pcistub_irq_handler_show(struct device_driver
*drv
, char *buf
)
1081 struct pcistub_device
*psdev
;
1082 struct xen_pcibk_dev_data
*dev_data
;
1084 unsigned long flags
;
1086 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1087 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1088 if (count
>= PAGE_SIZE
)
1092 dev_data
= pci_get_drvdata(psdev
->dev
);
1096 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1097 "%s:%s:%sing:%ld\n",
1098 pci_name(psdev
->dev
),
1099 dev_data
->isr_on
? "on" : "off",
1100 dev_data
->ack_intr
? "ack" : "not ack",
1103 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1106 static DRIVER_ATTR(irq_handlers
, S_IRUSR
, pcistub_irq_handler_show
, NULL
);
1108 static ssize_t
pcistub_irq_handler_switch(struct device_driver
*drv
,
1112 struct pcistub_device
*psdev
;
1113 struct xen_pcibk_dev_data
*dev_data
;
1114 int domain
, bus
, slot
, func
;
1117 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1121 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1126 dev_data
= pci_get_drvdata(psdev
->dev
);
1130 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1131 dev_data
->irq_name
, dev_data
->isr_on
,
1134 dev_data
->isr_on
= !(dev_data
->isr_on
);
1135 if (dev_data
->isr_on
)
1136 dev_data
->ack_intr
= 1;
1142 static DRIVER_ATTR(irq_handler_state
, S_IWUSR
, NULL
,
1143 pcistub_irq_handler_switch
);
1145 static ssize_t
pcistub_quirk_add(struct device_driver
*drv
, const char *buf
,
1148 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1151 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1156 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1164 static ssize_t
pcistub_quirk_show(struct device_driver
*drv
, char *buf
)
1167 unsigned long flags
;
1168 struct xen_pcibk_config_quirk
*quirk
;
1169 struct xen_pcibk_dev_data
*dev_data
;
1170 const struct config_field
*field
;
1171 const struct config_field_entry
*cfg_entry
;
1173 spin_lock_irqsave(&device_ids_lock
, flags
);
1174 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1175 if (count
>= PAGE_SIZE
)
1178 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1179 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1180 quirk
->pdev
->bus
->number
,
1181 PCI_SLOT(quirk
->pdev
->devfn
),
1182 PCI_FUNC(quirk
->pdev
->devfn
),
1183 quirk
->devid
.vendor
, quirk
->devid
.device
,
1184 quirk
->devid
.subvendor
,
1185 quirk
->devid
.subdevice
);
1187 dev_data
= pci_get_drvdata(quirk
->pdev
);
1189 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1190 field
= cfg_entry
->field
;
1191 if (count
>= PAGE_SIZE
)
1194 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1195 "\t\t%08x:%01x:%08x\n",
1196 cfg_entry
->base_offset
+
1197 field
->offset
, field
->size
,
1203 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1207 static DRIVER_ATTR(quirks
, S_IRUSR
| S_IWUSR
, pcistub_quirk_show
,
1210 static ssize_t
permissive_add(struct device_driver
*drv
, const char *buf
,
1213 int domain
, bus
, slot
, func
;
1215 struct pcistub_device
*psdev
;
1216 struct xen_pcibk_dev_data
*dev_data
;
1217 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1220 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1229 dev_data
= pci_get_drvdata(psdev
->dev
);
1230 /* the driver data for a device should never be null at this point */
1235 if (!dev_data
->permissive
) {
1236 dev_data
->permissive
= 1;
1237 /* Let user know that what they're doing could be unsafe */
1238 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1239 "configuration space accesses!\n");
1240 dev_warn(&psdev
->dev
->dev
,
1241 "permissive mode is potentially unsafe!\n");
1244 pcistub_device_put(psdev
);
1251 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1253 struct pcistub_device
*psdev
;
1254 struct xen_pcibk_dev_data
*dev_data
;
1256 unsigned long flags
;
1257 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1258 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1259 if (count
>= PAGE_SIZE
)
1263 dev_data
= pci_get_drvdata(psdev
->dev
);
1264 if (!dev_data
|| !dev_data
->permissive
)
1267 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1268 pci_name(psdev
->dev
));
1270 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1273 static DRIVER_ATTR(permissive
, S_IRUSR
| S_IWUSR
, permissive_show
,
1276 static void pcistub_exit(void)
1278 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1279 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1280 &driver_attr_remove_slot
);
1281 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1282 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1283 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1284 &driver_attr_permissive
);
1285 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1286 &driver_attr_irq_handlers
);
1287 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1288 &driver_attr_irq_handler_state
);
1289 pci_unregister_driver(&xen_pcibk_pci_driver
);
1292 static int __init
pcistub_init(void)
1296 int domain
, bus
, slot
, func
;
1299 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1303 err
= sscanf(pci_devs_to_hide
+ pos
,
1304 " (%x:%x:%x.%x) %n",
1305 &domain
, &bus
, &slot
, &func
, &parsed
);
1308 err
= sscanf(pci_devs_to_hide
+ pos
,
1310 &bus
, &slot
, &func
, &parsed
);
1315 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1319 /* if parsed<=0, we've reached the end of the string */
1321 } while (parsed
> 0 && pci_devs_to_hide
[pos
]);
1324 /* If we're the first PCI Device Driver to register, we're the
1325 * first one to get offered PCI devices as they become
1326 * available (and thus we can be the first to grab them)
1328 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1332 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1333 &driver_attr_new_slot
);
1335 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1336 &driver_attr_remove_slot
);
1338 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1339 &driver_attr_slots
);
1341 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1342 &driver_attr_quirks
);
1344 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1345 &driver_attr_permissive
);
1348 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1349 &driver_attr_irq_handlers
);
1351 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1352 &driver_attr_irq_handler_state
);
1360 printk(KERN_ERR DRV_NAME
": Error parsing pci_devs_to_hide at \"%s\"\n",
1361 pci_devs_to_hide
+ pos
);
1367 * fs_initcall happens before device_initcall
1368 * so xen_pcibk *should* get called first (b/c we
1369 * want to suck up any device before other drivers
1370 * get a chance by being the first pci device
1371 * driver to register)
1373 fs_initcall(pcistub_init
);
1376 static int __init
xen_pcibk_init(void)
1380 if (!xen_initial_domain())
1383 err
= xen_pcibk_config_init();
1388 err
= pcistub_init();
1393 pcistub_init_devices_late();
1394 err
= xen_pcibk_xenbus_register();
1401 static void __exit
xen_pcibk_cleanup(void)
1403 xen_pcibk_xenbus_unregister();
1407 module_init(xen_pcibk_init
);
1408 module_exit(xen_pcibk_cleanup
);
1410 MODULE_LICENSE("Dual BSD/GPL");
1411 MODULE_ALIAS("xen-backend:pci");