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
;
89 psdev
= container_of(kref
, struct pcistub_device
, kref
);
91 dev_dbg(&psdev
->dev
->dev
, "pcistub_device_release\n");
93 xen_unregister_device_domain_owner(psdev
->dev
);
95 /* Clean-up the device */
96 xen_pcibk_reset_device(psdev
->dev
);
97 xen_pcibk_config_free_dyn_fields(psdev
->dev
);
98 xen_pcibk_config_free_dev(psdev
->dev
);
99 kfree(pci_get_drvdata(psdev
->dev
));
100 pci_set_drvdata(psdev
->dev
, NULL
);
102 psdev
->dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
103 pci_dev_put(psdev
->dev
);
108 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
110 kref_get(&psdev
->kref
);
113 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
115 kref_put(&psdev
->kref
, pcistub_device_release
);
118 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
121 struct pcistub_device
*psdev
= NULL
;
124 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
126 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
127 if (psdev
->dev
!= NULL
128 && domain
== pci_domain_nr(psdev
->dev
->bus
)
129 && bus
== psdev
->dev
->bus
->number
130 && PCI_DEVFN(slot
, func
) == psdev
->dev
->devfn
) {
131 pcistub_device_get(psdev
);
140 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
144 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
145 struct pcistub_device
*psdev
)
147 struct pci_dev
*pci_dev
= NULL
;
150 pcistub_device_get(psdev
);
152 spin_lock_irqsave(&psdev
->lock
, flags
);
155 pci_dev
= psdev
->dev
;
157 spin_unlock_irqrestore(&psdev
->lock
, flags
);
160 pcistub_device_put(psdev
);
165 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
169 struct pcistub_device
*psdev
;
170 struct pci_dev
*found_dev
= NULL
;
173 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
175 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
176 if (psdev
->dev
!= NULL
177 && domain
== pci_domain_nr(psdev
->dev
->bus
)
178 && bus
== psdev
->dev
->bus
->number
179 && PCI_DEVFN(slot
, func
) == psdev
->dev
->devfn
) {
180 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
185 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
189 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
192 struct pcistub_device
*psdev
;
193 struct pci_dev
*found_dev
= NULL
;
196 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
198 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
199 if (psdev
->dev
== dev
) {
200 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
205 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
209 void pcistub_put_pci_dev(struct pci_dev
*dev
)
211 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
214 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
216 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
217 if (psdev
->dev
== dev
) {
223 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
224 if (WARN_ON(!found_psdev
))
227 /*hold this lock for avoiding breaking link between
228 * pcistub and xen_pcibk when AER is in processing
230 down_write(&pcistub_sem
);
231 /* Cleanup our device
232 * (so it's ready for the next domain)
234 xen_pcibk_reset_device(found_psdev
->dev
);
235 xen_pcibk_config_free_dyn_fields(found_psdev
->dev
);
236 xen_pcibk_config_reset_dev(found_psdev
->dev
);
238 xen_unregister_device_domain_owner(found_psdev
->dev
);
240 spin_lock_irqsave(&found_psdev
->lock
, flags
);
241 found_psdev
->pdev
= NULL
;
242 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
244 pcistub_device_put(found_psdev
);
245 up_write(&pcistub_sem
);
248 static int __devinit
pcistub_match_one(struct pci_dev
*dev
,
249 struct pcistub_device_id
*pdev_id
)
251 /* Match the specified device by domain, bus, slot, func and also if
252 * any of the device's parent bridges match.
254 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
255 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
256 && dev
->bus
->number
== pdev_id
->bus
257 && dev
->devfn
== pdev_id
->devfn
)
260 /* Sometimes topmost bridge links to itself. */
261 if (dev
== dev
->bus
->self
)
268 static int __devinit
pcistub_match(struct pci_dev
*dev
)
270 struct pcistub_device_id
*pdev_id
;
274 spin_lock_irqsave(&device_ids_lock
, flags
);
275 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
276 if (pcistub_match_one(dev
, pdev_id
)) {
281 spin_unlock_irqrestore(&device_ids_lock
, flags
);
286 static int __devinit
pcistub_init_device(struct pci_dev
*dev
)
288 struct xen_pcibk_dev_data
*dev_data
;
291 dev_dbg(&dev
->dev
, "initializing...\n");
293 /* The PCI backend is not intended to be a module (or to work with
294 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
295 * would need to be called somewhere to free the memory allocated
296 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
298 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
299 + strlen(pci_name(dev
)) + 1, GFP_ATOMIC
);
304 pci_set_drvdata(dev
, dev_data
);
307 * Setup name for fake IRQ handler. It will only be enabled
308 * once the device is turned on by the guest.
310 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
312 dev_dbg(&dev
->dev
, "initializing config\n");
314 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
315 err
= xen_pcibk_config_init_dev(dev
);
319 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
320 * must do this here because pcibios_enable_device may specify
321 * the pci device's true irq (and possibly its other resources)
322 * if they differ from what's in the configuration space.
323 * This makes the assumption that the device's resources won't
324 * change after this point (otherwise this code may break!)
326 dev_dbg(&dev
->dev
, "enabling device\n");
327 err
= pci_enable_device(dev
);
331 /* Now disable the device (this also ensures some private device
332 * data is setup before we export)
334 dev_dbg(&dev
->dev
, "reset device\n");
335 xen_pcibk_reset_device(dev
);
337 dev
->dev_flags
|= PCI_DEV_FLAGS_ASSIGNED
;
341 xen_pcibk_config_free_dev(dev
);
344 pci_set_drvdata(dev
, NULL
);
350 * Because some initialization still happens on
351 * devices during fs_initcall, we need to defer
352 * full initialization of our devices until
355 static int __init
pcistub_init_devices_late(void)
357 struct pcistub_device
*psdev
;
361 pr_debug(DRV_NAME
": pcistub_init_devices_late\n");
363 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
365 while (!list_empty(&seized_devices
)) {
366 psdev
= container_of(seized_devices
.next
,
367 struct pcistub_device
, dev_list
);
368 list_del(&psdev
->dev_list
);
370 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
372 err
= pcistub_init_device(psdev
->dev
);
374 dev_err(&psdev
->dev
->dev
,
375 "error %d initializing device\n", err
);
380 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
383 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
386 initialize_devices
= 1;
388 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
393 static int __devinit
pcistub_seize(struct pci_dev
*dev
)
395 struct pcistub_device
*psdev
;
399 psdev
= pcistub_device_alloc(dev
);
403 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
405 if (initialize_devices
) {
406 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
408 /* don't want irqs disabled when calling pcistub_init_device */
409 err
= pcistub_init_device(psdev
->dev
);
411 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
414 list_add(&psdev
->dev_list
, &pcistub_devices
);
416 dev_dbg(&dev
->dev
, "deferring initialization\n");
417 list_add(&psdev
->dev_list
, &seized_devices
);
420 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
423 pcistub_device_put(psdev
);
428 static int __devinit
pcistub_probe(struct pci_dev
*dev
,
429 const struct pci_device_id
*id
)
433 dev_dbg(&dev
->dev
, "probing...\n");
435 if (pcistub_match(dev
)) {
437 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
438 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
439 dev_err(&dev
->dev
, "can't export pci devices that "
440 "don't have a normal (0) or bridge (1) "
446 dev_info(&dev
->dev
, "seizing device\n");
447 err
= pcistub_seize(dev
);
449 /* Didn't find the device */
456 static void pcistub_remove(struct pci_dev
*dev
)
458 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
461 dev_dbg(&dev
->dev
, "removing\n");
463 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
465 xen_pcibk_config_quirk_release(dev
);
467 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
468 if (psdev
->dev
== dev
) {
474 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
477 dev_dbg(&dev
->dev
, "found device to remove - in use? %p\n",
480 if (found_psdev
->pdev
) {
481 printk(KERN_WARNING DRV_NAME
": ****** removing device "
482 "%s while still in-use! ******\n",
483 pci_name(found_psdev
->dev
));
484 printk(KERN_WARNING DRV_NAME
": ****** driver domain may"
485 " still access this device's i/o resources!\n");
486 printk(KERN_WARNING DRV_NAME
": ****** shutdown driver "
487 "domain before binding device\n");
488 printk(KERN_WARNING DRV_NAME
": ****** to other drivers "
491 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
495 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
496 list_del(&found_psdev
->dev_list
);
497 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
499 /* the final put for releasing from the list */
500 pcistub_device_put(found_psdev
);
504 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids
) = {
506 .vendor
= PCI_ANY_ID
,
507 .device
= PCI_ANY_ID
,
508 .subvendor
= PCI_ANY_ID
,
509 .subdevice
= PCI_ANY_ID
,
514 #define PCI_NODENAME_MAX 40
515 static void kill_domain_by_device(struct pcistub_device
*psdev
)
517 struct xenbus_transaction xbt
;
519 char nodename
[PCI_NODENAME_MAX
];
522 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
523 psdev
->pdev
->xdev
->otherend_id
);
526 err
= xenbus_transaction_start(&xbt
);
528 dev_err(&psdev
->dev
->dev
,
529 "error %d when start xenbus transaction\n", err
);
532 /*PV AER handlers will set this flag*/
533 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
534 err
= xenbus_transaction_end(xbt
, 0);
538 dev_err(&psdev
->dev
->dev
,
539 "error %d when end xenbus transaction\n", err
);
544 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
545 * backend need to have cooperation. In xen_pcibk, those steps will do similar
546 * jobs: send service request and waiting for front_end response.
548 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
549 pci_channel_state_t state
, int aer_cmd
,
550 pci_ers_result_t result
)
552 pci_ers_result_t res
= result
;
553 struct xen_pcie_aer_op
*aer_op
;
556 /*with PV AER drivers*/
557 aer_op
= &(psdev
->pdev
->sh_info
->aer_op
);
558 aer_op
->cmd
= aer_cmd
;
559 /*useful for error_detected callback*/
562 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
563 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
565 dev_err(&psdev
->dev
->dev
,
566 DRV_NAME
": failed to get pcifront device\n");
567 return PCI_ERS_RESULT_NONE
;
571 dev_dbg(&psdev
->dev
->dev
,
572 DRV_NAME
": aer_op %x dom %x bus %x devfn %x\n",
573 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
574 /*local flag to mark there's aer request, xen_pcibk callback will use
575 * this flag to judge whether we need to check pci-front give aer
578 set_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
580 /*It is possible that a pcifront conf_read_write ops request invokes
581 * the callback which cause the spurious execution of wake_up.
582 * Yet it is harmless and better than a spinlock here
584 set_bit(_XEN_PCIB_active
,
585 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
587 notify_remote_via_irq(psdev
->pdev
->evtchn_irq
);
589 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
590 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
591 &psdev
->pdev
->sh_info
->flags
)), 300*HZ
);
594 if (test_bit(_XEN_PCIB_active
,
595 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
596 dev_err(&psdev
->dev
->dev
,
597 "pcifront aer process not responding!\n");
598 clear_bit(_XEN_PCIB_active
,
599 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
600 aer_op
->err
= PCI_ERS_RESULT_NONE
;
604 clear_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
606 if (test_bit(_XEN_PCIF_active
,
607 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
608 dev_dbg(&psdev
->dev
->dev
,
609 "schedule pci_conf service in " DRV_NAME
"\n");
610 xen_pcibk_test_and_schedule_op(psdev
->pdev
);
613 res
= (pci_ers_result_t
)aer_op
->err
;
618 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
619 * of the device driver could provide this service, and then wait for pcifront
621 * @dev: pointer to PCI devices
622 * return value is used by aer_core do_recovery policy
624 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
626 struct pcistub_device
*psdev
;
627 pci_ers_result_t result
;
629 result
= PCI_ERS_RESULT_RECOVERED
;
630 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
631 dev
->bus
->number
, dev
->devfn
);
633 down_write(&pcistub_sem
);
634 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
636 PCI_SLOT(dev
->devfn
),
637 PCI_FUNC(dev
->devfn
));
639 if (!psdev
|| !psdev
->pdev
) {
641 DRV_NAME
" device is not found/assigned\n");
645 if (!psdev
->pdev
->sh_info
) {
646 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
647 " by HVM, kill it\n");
648 kill_domain_by_device(psdev
);
652 if (!test_bit(_XEN_PCIB_AERHANDLER
,
653 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
655 "guest with no AER driver should have been killed\n");
658 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_slotreset
, result
);
660 if (result
== PCI_ERS_RESULT_NONE
||
661 result
== PCI_ERS_RESULT_DISCONNECT
) {
663 "No AER slot_reset service or disconnected!\n");
664 kill_domain_by_device(psdev
);
667 pcistub_device_put(psdev
);
669 up_write(&pcistub_sem
);
675 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
676 * in case of the device driver could provide this service, and then wait
678 * @dev: pointer to PCI devices
679 * return value is used by aer_core do_recovery policy
682 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
684 struct pcistub_device
*psdev
;
685 pci_ers_result_t result
;
687 result
= PCI_ERS_RESULT_RECOVERED
;
688 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
689 dev
->bus
->number
, dev
->devfn
);
691 down_write(&pcistub_sem
);
692 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
694 PCI_SLOT(dev
->devfn
),
695 PCI_FUNC(dev
->devfn
));
697 if (!psdev
|| !psdev
->pdev
) {
699 DRV_NAME
" device is not found/assigned\n");
703 if (!psdev
->pdev
->sh_info
) {
704 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
705 " by HVM, kill it\n");
706 kill_domain_by_device(psdev
);
710 if (!test_bit(_XEN_PCIB_AERHANDLER
,
711 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
713 "guest with no AER driver should have been killed\n");
716 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_mmio
, result
);
718 if (result
== PCI_ERS_RESULT_NONE
||
719 result
== PCI_ERS_RESULT_DISCONNECT
) {
721 "No AER mmio_enabled service or disconnected!\n");
722 kill_domain_by_device(psdev
);
725 pcistub_device_put(psdev
);
727 up_write(&pcistub_sem
);
731 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
732 * in case of the device driver could provide this service, and then wait
734 * @dev: pointer to PCI devices
735 * @error: the current PCI connection state
736 * return value is used by aer_core do_recovery policy
739 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
740 pci_channel_state_t error
)
742 struct pcistub_device
*psdev
;
743 pci_ers_result_t result
;
745 result
= PCI_ERS_RESULT_CAN_RECOVER
;
746 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
747 dev
->bus
->number
, dev
->devfn
);
749 down_write(&pcistub_sem
);
750 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
752 PCI_SLOT(dev
->devfn
),
753 PCI_FUNC(dev
->devfn
));
755 if (!psdev
|| !psdev
->pdev
) {
757 DRV_NAME
" device is not found/assigned\n");
761 if (!psdev
->pdev
->sh_info
) {
762 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
763 " by HVM, kill it\n");
764 kill_domain_by_device(psdev
);
768 /*Guest owns the device yet no aer handler regiested, kill guest*/
769 if (!test_bit(_XEN_PCIB_AERHANDLER
,
770 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
771 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
772 kill_domain_by_device(psdev
);
775 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
777 if (result
== PCI_ERS_RESULT_NONE
||
778 result
== PCI_ERS_RESULT_DISCONNECT
) {
780 "No AER error_detected service or disconnected!\n");
781 kill_domain_by_device(psdev
);
784 pcistub_device_put(psdev
);
786 up_write(&pcistub_sem
);
790 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
791 * in case of the device driver could provide this service, and then wait
793 * @dev: pointer to PCI devices
796 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
798 struct pcistub_device
*psdev
;
800 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
801 dev
->bus
->number
, dev
->devfn
);
803 down_write(&pcistub_sem
);
804 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
806 PCI_SLOT(dev
->devfn
),
807 PCI_FUNC(dev
->devfn
));
809 if (!psdev
|| !psdev
->pdev
) {
811 DRV_NAME
" device is not found/assigned\n");
815 if (!psdev
->pdev
->sh_info
) {
816 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
817 " by HVM, kill it\n");
818 kill_domain_by_device(psdev
);
822 if (!test_bit(_XEN_PCIB_AERHANDLER
,
823 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
825 "guest with no AER driver should have been killed\n");
826 kill_domain_by_device(psdev
);
829 common_process(psdev
, 1, XEN_PCI_OP_aer_resume
,
830 PCI_ERS_RESULT_RECOVERED
);
832 pcistub_device_put(psdev
);
834 up_write(&pcistub_sem
);
838 /*add xen_pcibk AER handling*/
839 static struct pci_error_handlers xen_pcibk_error_handler
= {
840 .error_detected
= xen_pcibk_error_detected
,
841 .mmio_enabled
= xen_pcibk_mmio_enabled
,
842 .slot_reset
= xen_pcibk_slot_reset
,
843 .resume
= xen_pcibk_error_resume
,
847 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
848 * for a normal device. I don't want it to be loaded automatically.
851 static struct pci_driver xen_pcibk_pci_driver
= {
852 /* The name should be xen_pciback, but until the tools are updated
853 * we will keep it as pciback. */
855 .id_table
= pcistub_ids
,
856 .probe
= pcistub_probe
,
857 .remove
= pcistub_remove
,
858 .err_handler
= &xen_pcibk_error_handler
,
861 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
862 int *slot
, int *func
)
866 err
= sscanf(buf
, " %x:%x:%x.%x", domain
, bus
, slot
, func
);
872 /* try again without domain */
874 err
= sscanf(buf
, " %x:%x.%x", bus
, slot
, func
);
881 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
882 *slot
, int *func
, int *reg
, int *size
, int *mask
)
887 sscanf(buf
, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain
, bus
, slot
,
888 func
, reg
, size
, mask
);
894 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
896 struct pcistub_device_id
*pci_dev_id
;
899 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
903 pci_dev_id
->domain
= domain
;
904 pci_dev_id
->bus
= bus
;
905 pci_dev_id
->devfn
= PCI_DEVFN(slot
, func
);
907 pr_debug(DRV_NAME
": wants to seize %04x:%02x:%02x.%d\n",
908 domain
, bus
, slot
, func
);
910 spin_lock_irqsave(&device_ids_lock
, flags
);
911 list_add_tail(&pci_dev_id
->slot_list
, &pcistub_device_ids
);
912 spin_unlock_irqrestore(&device_ids_lock
, flags
);
917 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
919 struct pcistub_device_id
*pci_dev_id
, *t
;
920 int devfn
= PCI_DEVFN(slot
, func
);
924 spin_lock_irqsave(&device_ids_lock
, flags
);
925 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
927 if (pci_dev_id
->domain
== domain
928 && pci_dev_id
->bus
== bus
&& pci_dev_id
->devfn
== devfn
) {
929 /* Don't break; here because it's possible the same
930 * slot could be in the list more than once
932 list_del(&pci_dev_id
->slot_list
);
937 pr_debug(DRV_NAME
": removed %04x:%02x:%02x.%d from "
938 "seize list\n", domain
, bus
, slot
, func
);
941 spin_unlock_irqrestore(&device_ids_lock
, flags
);
946 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
, int reg
,
950 struct pcistub_device
*psdev
;
952 struct config_field
*field
;
954 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
955 if (!psdev
|| !psdev
->dev
) {
961 field
= kzalloc(sizeof(*field
), GFP_ATOMIC
);
972 field
->release
= NULL
;
973 field
->clean
= xen_pcibk_config_field_free
;
975 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
982 static ssize_t
pcistub_slot_add(struct device_driver
*drv
, const char *buf
,
985 int domain
, bus
, slot
, func
;
988 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
992 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
999 static DRIVER_ATTR(new_slot
, S_IWUSR
, NULL
, pcistub_slot_add
);
1001 static ssize_t
pcistub_slot_remove(struct device_driver
*drv
, const char *buf
,
1004 int domain
, bus
, slot
, func
;
1007 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1011 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1018 static DRIVER_ATTR(remove_slot
, S_IWUSR
, NULL
, pcistub_slot_remove
);
1020 static ssize_t
pcistub_slot_show(struct device_driver
*drv
, char *buf
)
1022 struct pcistub_device_id
*pci_dev_id
;
1024 unsigned long flags
;
1026 spin_lock_irqsave(&device_ids_lock
, flags
);
1027 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1028 if (count
>= PAGE_SIZE
)
1031 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1032 "%04x:%02x:%02x.%d\n",
1033 pci_dev_id
->domain
, pci_dev_id
->bus
,
1034 PCI_SLOT(pci_dev_id
->devfn
),
1035 PCI_FUNC(pci_dev_id
->devfn
));
1037 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1041 static DRIVER_ATTR(slots
, S_IRUSR
, pcistub_slot_show
, NULL
);
1043 static ssize_t
pcistub_irq_handler_show(struct device_driver
*drv
, char *buf
)
1045 struct pcistub_device
*psdev
;
1046 struct xen_pcibk_dev_data
*dev_data
;
1048 unsigned long flags
;
1050 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1051 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1052 if (count
>= PAGE_SIZE
)
1056 dev_data
= pci_get_drvdata(psdev
->dev
);
1060 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1061 "%s:%s:%sing:%ld\n",
1062 pci_name(psdev
->dev
),
1063 dev_data
->isr_on
? "on" : "off",
1064 dev_data
->ack_intr
? "ack" : "not ack",
1067 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1070 static DRIVER_ATTR(irq_handlers
, S_IRUSR
, pcistub_irq_handler_show
, NULL
);
1072 static ssize_t
pcistub_irq_handler_switch(struct device_driver
*drv
,
1076 struct pcistub_device
*psdev
;
1077 struct xen_pcibk_dev_data
*dev_data
;
1078 int domain
, bus
, slot
, func
;
1081 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1085 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1090 dev_data
= pci_get_drvdata(psdev
->dev
);
1094 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1095 dev_data
->irq_name
, dev_data
->isr_on
,
1098 dev_data
->isr_on
= !(dev_data
->isr_on
);
1099 if (dev_data
->isr_on
)
1100 dev_data
->ack_intr
= 1;
1106 static DRIVER_ATTR(irq_handler_state
, S_IWUSR
, NULL
,
1107 pcistub_irq_handler_switch
);
1109 static ssize_t
pcistub_quirk_add(struct device_driver
*drv
, const char *buf
,
1112 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1115 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1120 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1128 static ssize_t
pcistub_quirk_show(struct device_driver
*drv
, char *buf
)
1131 unsigned long flags
;
1132 struct xen_pcibk_config_quirk
*quirk
;
1133 struct xen_pcibk_dev_data
*dev_data
;
1134 const struct config_field
*field
;
1135 const struct config_field_entry
*cfg_entry
;
1137 spin_lock_irqsave(&device_ids_lock
, flags
);
1138 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1139 if (count
>= PAGE_SIZE
)
1142 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1143 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1144 quirk
->pdev
->bus
->number
,
1145 PCI_SLOT(quirk
->pdev
->devfn
),
1146 PCI_FUNC(quirk
->pdev
->devfn
),
1147 quirk
->devid
.vendor
, quirk
->devid
.device
,
1148 quirk
->devid
.subvendor
,
1149 quirk
->devid
.subdevice
);
1151 dev_data
= pci_get_drvdata(quirk
->pdev
);
1153 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1154 field
= cfg_entry
->field
;
1155 if (count
>= PAGE_SIZE
)
1158 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1159 "\t\t%08x:%01x:%08x\n",
1160 cfg_entry
->base_offset
+
1161 field
->offset
, field
->size
,
1167 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1171 static DRIVER_ATTR(quirks
, S_IRUSR
| S_IWUSR
, pcistub_quirk_show
,
1174 static ssize_t
permissive_add(struct device_driver
*drv
, const char *buf
,
1177 int domain
, bus
, slot
, func
;
1179 struct pcistub_device
*psdev
;
1180 struct xen_pcibk_dev_data
*dev_data
;
1181 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1184 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1193 dev_data
= pci_get_drvdata(psdev
->dev
);
1194 /* the driver data for a device should never be null at this point */
1199 if (!dev_data
->permissive
) {
1200 dev_data
->permissive
= 1;
1201 /* Let user know that what they're doing could be unsafe */
1202 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1203 "configuration space accesses!\n");
1204 dev_warn(&psdev
->dev
->dev
,
1205 "permissive mode is potentially unsafe!\n");
1208 pcistub_device_put(psdev
);
1215 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1217 struct pcistub_device
*psdev
;
1218 struct xen_pcibk_dev_data
*dev_data
;
1220 unsigned long flags
;
1221 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1222 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1223 if (count
>= PAGE_SIZE
)
1227 dev_data
= pci_get_drvdata(psdev
->dev
);
1228 if (!dev_data
|| !dev_data
->permissive
)
1231 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1232 pci_name(psdev
->dev
));
1234 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1237 static DRIVER_ATTR(permissive
, S_IRUSR
| S_IWUSR
, permissive_show
,
1240 static void pcistub_exit(void)
1242 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1243 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1244 &driver_attr_remove_slot
);
1245 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1246 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1247 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1248 &driver_attr_permissive
);
1249 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1250 &driver_attr_irq_handlers
);
1251 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1252 &driver_attr_irq_handler_state
);
1253 pci_unregister_driver(&xen_pcibk_pci_driver
);
1256 static int __init
pcistub_init(void)
1260 int domain
, bus
, slot
, func
;
1263 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1267 err
= sscanf(pci_devs_to_hide
+ pos
,
1268 " (%x:%x:%x.%x) %n",
1269 &domain
, &bus
, &slot
, &func
, &parsed
);
1272 err
= sscanf(pci_devs_to_hide
+ pos
,
1274 &bus
, &slot
, &func
, &parsed
);
1279 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1283 /* if parsed<=0, we've reached the end of the string */
1285 } while (parsed
> 0 && pci_devs_to_hide
[pos
]);
1288 /* If we're the first PCI Device Driver to register, we're the
1289 * first one to get offered PCI devices as they become
1290 * available (and thus we can be the first to grab them)
1292 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1296 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1297 &driver_attr_new_slot
);
1299 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1300 &driver_attr_remove_slot
);
1302 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1303 &driver_attr_slots
);
1305 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1306 &driver_attr_quirks
);
1308 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1309 &driver_attr_permissive
);
1312 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1313 &driver_attr_irq_handlers
);
1315 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1316 &driver_attr_irq_handler_state
);
1324 printk(KERN_ERR DRV_NAME
": Error parsing pci_devs_to_hide at \"%s\"\n",
1325 pci_devs_to_hide
+ pos
);
1331 * fs_initcall happens before device_initcall
1332 * so xen_pcibk *should* get called first (b/c we
1333 * want to suck up any device before other drivers
1334 * get a chance by being the first pci device
1335 * driver to register)
1337 fs_initcall(pcistub_init
);
1340 static int __init
xen_pcibk_init(void)
1344 if (!xen_initial_domain())
1347 err
= xen_pcibk_config_init();
1352 err
= pcistub_init();
1357 pcistub_init_devices_late();
1358 err
= xen_pcibk_xenbus_register();
1365 static void __exit
xen_pcibk_cleanup(void)
1367 xen_pcibk_xenbus_unregister();
1371 module_init(xen_pcibk_init
);
1372 module_exit(xen_pcibk_cleanup
);
1374 MODULE_LICENSE("Dual BSD/GPL");
1375 MODULE_ALIAS("xen-backend:pci");