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>
20 #include <xen/interface/physdev.h>
22 #include "conf_space.h"
23 #include "conf_space_quirks.h"
25 static char *pci_devs_to_hide
;
26 wait_queue_head_t xen_pcibk_aer_wait_queue
;
27 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
28 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
30 static DECLARE_RWSEM(pcistub_sem
);
31 module_param_named(hide
, pci_devs_to_hide
, charp
, 0444);
33 struct pcistub_device_id
{
34 struct list_head slot_list
;
39 static LIST_HEAD(pcistub_device_ids
);
40 static DEFINE_SPINLOCK(device_ids_lock
);
42 struct pcistub_device
{
44 struct list_head dev_list
;
48 struct xen_pcibk_device
*pdev
;/* non-NULL if struct pci_dev is in use */
51 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
52 * flag must be locked with pcistub_devices_lock
54 static DEFINE_SPINLOCK(pcistub_devices_lock
);
55 static LIST_HEAD(pcistub_devices
);
57 /* wait for device_initcall before initializing our devices
58 * (see pcistub_init_devices_late)
60 static int initialize_devices
;
61 static LIST_HEAD(seized_devices
);
63 static struct pcistub_device
*pcistub_device_alloc(struct pci_dev
*dev
)
65 struct pcistub_device
*psdev
;
67 dev_dbg(&dev
->dev
, "pcistub_device_alloc\n");
69 psdev
= kzalloc(sizeof(*psdev
), GFP_ATOMIC
);
73 psdev
->dev
= pci_dev_get(dev
);
79 kref_init(&psdev
->kref
);
80 spin_lock_init(&psdev
->lock
);
85 /* Don't call this directly as it's called by pcistub_device_put */
86 static void pcistub_device_release(struct kref
*kref
)
88 struct pcistub_device
*psdev
;
90 struct xen_pcibk_dev_data
*dev_data
;
92 psdev
= container_of(kref
, struct pcistub_device
, kref
);
94 dev_data
= pci_get_drvdata(dev
);
96 dev_dbg(&dev
->dev
, "pcistub_device_release\n");
98 xen_unregister_device_domain_owner(dev
);
100 /* Call the reset function which does not take lock as this
101 * is called from "unbind" which takes a device_lock mutex.
103 __pci_reset_function_locked(dev
);
104 if (pci_load_and_free_saved_state(dev
, &dev_data
->pci_saved_state
))
105 dev_dbg(&dev
->dev
, "Could not reload PCI state\n");
107 pci_restore_state(dev
);
109 if (pci_find_capability(dev
, PCI_CAP_ID_MSIX
)) {
110 struct physdev_pci_device ppdev
= {
111 .seg
= pci_domain_nr(dev
->bus
),
112 .bus
= dev
->bus
->number
,
115 int err
= HYPERVISOR_physdev_op(PHYSDEVOP_release_msix
,
119 dev_warn(&dev
->dev
, "MSI-X release failed (%d)\n",
123 /* Disable the device */
124 xen_pcibk_reset_device(dev
);
127 pci_set_drvdata(dev
, NULL
);
129 /* Clean-up the device */
130 xen_pcibk_config_free_dyn_fields(dev
);
131 xen_pcibk_config_free_dev(dev
);
133 dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
139 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
141 kref_get(&psdev
->kref
);
144 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
146 kref_put(&psdev
->kref
, pcistub_device_release
);
149 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
152 struct pcistub_device
*psdev
= NULL
;
155 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
157 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
158 if (psdev
->dev
!= NULL
159 && domain
== pci_domain_nr(psdev
->dev
->bus
)
160 && bus
== psdev
->dev
->bus
->number
161 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
162 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
163 pcistub_device_get(psdev
);
172 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
176 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
177 struct pcistub_device
*psdev
)
179 struct pci_dev
*pci_dev
= NULL
;
182 pcistub_device_get(psdev
);
184 spin_lock_irqsave(&psdev
->lock
, flags
);
187 pci_dev
= psdev
->dev
;
189 spin_unlock_irqrestore(&psdev
->lock
, flags
);
192 pcistub_device_put(psdev
);
197 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
201 struct pcistub_device
*psdev
;
202 struct pci_dev
*found_dev
= NULL
;
205 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
207 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
208 if (psdev
->dev
!= NULL
209 && domain
== pci_domain_nr(psdev
->dev
->bus
)
210 && bus
== psdev
->dev
->bus
->number
211 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
212 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
213 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
218 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
222 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
225 struct pcistub_device
*psdev
;
226 struct pci_dev
*found_dev
= NULL
;
229 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
231 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
232 if (psdev
->dev
== dev
) {
233 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
238 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
242 void pcistub_put_pci_dev(struct pci_dev
*dev
)
244 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
247 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
249 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
250 if (psdev
->dev
== dev
) {
256 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
257 if (WARN_ON(!found_psdev
))
260 /*hold this lock for avoiding breaking link between
261 * pcistub and xen_pcibk when AER is in processing
263 down_write(&pcistub_sem
);
264 /* Cleanup our device
265 * (so it's ready for the next domain)
268 /* This is OK - we are running from workqueue context
269 * and want to inhibit the user from fiddling with 'reset'
271 pci_reset_function(dev
);
272 pci_restore_state(psdev
->dev
);
274 /* This disables the device. */
275 xen_pcibk_reset_device(found_psdev
->dev
);
277 /* And cleanup up our emulated fields. */
278 xen_pcibk_config_free_dyn_fields(found_psdev
->dev
);
279 xen_pcibk_config_reset_dev(found_psdev
->dev
);
281 xen_unregister_device_domain_owner(found_psdev
->dev
);
283 spin_lock_irqsave(&found_psdev
->lock
, flags
);
284 found_psdev
->pdev
= NULL
;
285 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
287 pcistub_device_put(found_psdev
);
288 up_write(&pcistub_sem
);
291 static int pcistub_match_one(struct pci_dev
*dev
,
292 struct pcistub_device_id
*pdev_id
)
294 /* Match the specified device by domain, bus, slot, func and also if
295 * any of the device's parent bridges match.
297 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
298 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
299 && dev
->bus
->number
== pdev_id
->bus
300 && dev
->devfn
== pdev_id
->devfn
)
303 /* Sometimes topmost bridge links to itself. */
304 if (dev
== dev
->bus
->self
)
311 static int pcistub_match(struct pci_dev
*dev
)
313 struct pcistub_device_id
*pdev_id
;
317 spin_lock_irqsave(&device_ids_lock
, flags
);
318 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
319 if (pcistub_match_one(dev
, pdev_id
)) {
324 spin_unlock_irqrestore(&device_ids_lock
, flags
);
329 static int pcistub_init_device(struct pci_dev
*dev
)
331 struct xen_pcibk_dev_data
*dev_data
;
334 dev_dbg(&dev
->dev
, "initializing...\n");
336 /* The PCI backend is not intended to be a module (or to work with
337 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
338 * would need to be called somewhere to free the memory allocated
339 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
341 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
342 + strlen(pci_name(dev
)) + 1, GFP_ATOMIC
);
347 pci_set_drvdata(dev
, dev_data
);
350 * Setup name for fake IRQ handler. It will only be enabled
351 * once the device is turned on by the guest.
353 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
355 dev_dbg(&dev
->dev
, "initializing config\n");
357 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
358 err
= xen_pcibk_config_init_dev(dev
);
362 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
363 * must do this here because pcibios_enable_device may specify
364 * the pci device's true irq (and possibly its other resources)
365 * if they differ from what's in the configuration space.
366 * This makes the assumption that the device's resources won't
367 * change after this point (otherwise this code may break!)
369 dev_dbg(&dev
->dev
, "enabling device\n");
370 err
= pci_enable_device(dev
);
374 if (pci_find_capability(dev
, PCI_CAP_ID_MSIX
)) {
375 struct physdev_pci_device ppdev
= {
376 .seg
= pci_domain_nr(dev
->bus
),
377 .bus
= dev
->bus
->number
,
381 err
= HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix
, &ppdev
);
383 dev_err(&dev
->dev
, "MSI-X preparation failed (%d)\n",
387 /* We need the device active to save the state. */
388 dev_dbg(&dev
->dev
, "save state of device\n");
390 dev_data
->pci_saved_state
= pci_store_saved_state(dev
);
391 if (!dev_data
->pci_saved_state
)
392 dev_err(&dev
->dev
, "Could not store PCI conf saved state!\n");
394 dev_dbg(&dev
->dev
, "resetting (FLR, D3, etc) the device\n");
395 __pci_reset_function_locked(dev
);
396 pci_restore_state(dev
);
398 /* Now disable the device (this also ensures some private device
399 * data is setup before we export)
401 dev_dbg(&dev
->dev
, "reset device\n");
402 xen_pcibk_reset_device(dev
);
404 dev
->dev_flags
|= PCI_DEV_FLAGS_ASSIGNED
;
408 xen_pcibk_config_free_dev(dev
);
411 pci_set_drvdata(dev
, NULL
);
417 * Because some initialization still happens on
418 * devices during fs_initcall, we need to defer
419 * full initialization of our devices until
422 static int __init
pcistub_init_devices_late(void)
424 struct pcistub_device
*psdev
;
428 pr_debug(DRV_NAME
": pcistub_init_devices_late\n");
430 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
432 while (!list_empty(&seized_devices
)) {
433 psdev
= container_of(seized_devices
.next
,
434 struct pcistub_device
, dev_list
);
435 list_del(&psdev
->dev_list
);
437 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
439 err
= pcistub_init_device(psdev
->dev
);
441 dev_err(&psdev
->dev
->dev
,
442 "error %d initializing device\n", err
);
447 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
450 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
453 initialize_devices
= 1;
455 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
460 static int pcistub_seize(struct pci_dev
*dev
)
462 struct pcistub_device
*psdev
;
466 psdev
= pcistub_device_alloc(dev
);
470 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
472 if (initialize_devices
) {
473 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
475 /* don't want irqs disabled when calling pcistub_init_device */
476 err
= pcistub_init_device(psdev
->dev
);
478 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
481 list_add(&psdev
->dev_list
, &pcistub_devices
);
483 dev_dbg(&dev
->dev
, "deferring initialization\n");
484 list_add(&psdev
->dev_list
, &seized_devices
);
487 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
490 pcistub_device_put(psdev
);
495 static int pcistub_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
499 dev_dbg(&dev
->dev
, "probing...\n");
501 if (pcistub_match(dev
)) {
503 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
504 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
505 dev_err(&dev
->dev
, "can't export pci devices that "
506 "don't have a normal (0) or bridge (1) "
512 dev_info(&dev
->dev
, "seizing device\n");
513 err
= pcistub_seize(dev
);
515 /* Didn't find the device */
522 static void pcistub_remove(struct pci_dev
*dev
)
524 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
527 dev_dbg(&dev
->dev
, "removing\n");
529 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
531 xen_pcibk_config_quirk_release(dev
);
533 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
534 if (psdev
->dev
== dev
) {
540 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
543 dev_dbg(&dev
->dev
, "found device to remove - in use? %p\n",
546 if (found_psdev
->pdev
) {
547 printk(KERN_WARNING DRV_NAME
": ****** removing device "
548 "%s while still in-use! ******\n",
549 pci_name(found_psdev
->dev
));
550 printk(KERN_WARNING DRV_NAME
": ****** driver domain may"
551 " still access this device's i/o resources!\n");
552 printk(KERN_WARNING DRV_NAME
": ****** shutdown driver "
553 "domain before binding device\n");
554 printk(KERN_WARNING DRV_NAME
": ****** to other drivers "
557 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
561 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
562 list_del(&found_psdev
->dev_list
);
563 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
565 /* the final put for releasing from the list */
566 pcistub_device_put(found_psdev
);
570 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids
) = {
572 .vendor
= PCI_ANY_ID
,
573 .device
= PCI_ANY_ID
,
574 .subvendor
= PCI_ANY_ID
,
575 .subdevice
= PCI_ANY_ID
,
580 #define PCI_NODENAME_MAX 40
581 static void kill_domain_by_device(struct pcistub_device
*psdev
)
583 struct xenbus_transaction xbt
;
585 char nodename
[PCI_NODENAME_MAX
];
588 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
589 psdev
->pdev
->xdev
->otherend_id
);
592 err
= xenbus_transaction_start(&xbt
);
594 dev_err(&psdev
->dev
->dev
,
595 "error %d when start xenbus transaction\n", err
);
598 /*PV AER handlers will set this flag*/
599 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
600 err
= xenbus_transaction_end(xbt
, 0);
604 dev_err(&psdev
->dev
->dev
,
605 "error %d when end xenbus transaction\n", err
);
610 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
611 * backend need to have cooperation. In xen_pcibk, those steps will do similar
612 * jobs: send service request and waiting for front_end response.
614 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
615 pci_channel_state_t state
, int aer_cmd
,
616 pci_ers_result_t result
)
618 pci_ers_result_t res
= result
;
619 struct xen_pcie_aer_op
*aer_op
;
622 /*with PV AER drivers*/
623 aer_op
= &(psdev
->pdev
->sh_info
->aer_op
);
624 aer_op
->cmd
= aer_cmd
;
625 /*useful for error_detected callback*/
628 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
629 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
631 dev_err(&psdev
->dev
->dev
,
632 DRV_NAME
": failed to get pcifront device\n");
633 return PCI_ERS_RESULT_NONE
;
637 dev_dbg(&psdev
->dev
->dev
,
638 DRV_NAME
": aer_op %x dom %x bus %x devfn %x\n",
639 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
640 /*local flag to mark there's aer request, xen_pcibk callback will use
641 * this flag to judge whether we need to check pci-front give aer
644 set_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
646 /*It is possible that a pcifront conf_read_write ops request invokes
647 * the callback which cause the spurious execution of wake_up.
648 * Yet it is harmless and better than a spinlock here
650 set_bit(_XEN_PCIB_active
,
651 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
653 notify_remote_via_irq(psdev
->pdev
->evtchn_irq
);
655 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
656 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
657 &psdev
->pdev
->sh_info
->flags
)), 300*HZ
);
660 if (test_bit(_XEN_PCIB_active
,
661 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
662 dev_err(&psdev
->dev
->dev
,
663 "pcifront aer process not responding!\n");
664 clear_bit(_XEN_PCIB_active
,
665 (unsigned long *)&psdev
->pdev
->sh_info
->flags
);
666 aer_op
->err
= PCI_ERS_RESULT_NONE
;
670 clear_bit(_PCIB_op_pending
, (unsigned long *)&psdev
->pdev
->flags
);
672 if (test_bit(_XEN_PCIF_active
,
673 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
674 dev_dbg(&psdev
->dev
->dev
,
675 "schedule pci_conf service in " DRV_NAME
"\n");
676 xen_pcibk_test_and_schedule_op(psdev
->pdev
);
679 res
= (pci_ers_result_t
)aer_op
->err
;
684 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
685 * of the device driver could provide this service, and then wait for pcifront
687 * @dev: pointer to PCI devices
688 * return value is used by aer_core do_recovery policy
690 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
692 struct pcistub_device
*psdev
;
693 pci_ers_result_t result
;
695 result
= PCI_ERS_RESULT_RECOVERED
;
696 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
697 dev
->bus
->number
, dev
->devfn
);
699 down_write(&pcistub_sem
);
700 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
702 PCI_SLOT(dev
->devfn
),
703 PCI_FUNC(dev
->devfn
));
705 if (!psdev
|| !psdev
->pdev
) {
707 DRV_NAME
" device is not found/assigned\n");
711 if (!psdev
->pdev
->sh_info
) {
712 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
713 " by HVM, kill it\n");
714 kill_domain_by_device(psdev
);
718 if (!test_bit(_XEN_PCIB_AERHANDLER
,
719 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
721 "guest with no AER driver should have been killed\n");
724 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_slotreset
, result
);
726 if (result
== PCI_ERS_RESULT_NONE
||
727 result
== PCI_ERS_RESULT_DISCONNECT
) {
729 "No AER slot_reset service or disconnected!\n");
730 kill_domain_by_device(psdev
);
734 pcistub_device_put(psdev
);
735 up_write(&pcistub_sem
);
741 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
742 * in case of the device driver could provide this service, and then wait
744 * @dev: pointer to PCI devices
745 * return value is used by aer_core do_recovery policy
748 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
750 struct pcistub_device
*psdev
;
751 pci_ers_result_t result
;
753 result
= PCI_ERS_RESULT_RECOVERED
;
754 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
755 dev
->bus
->number
, dev
->devfn
);
757 down_write(&pcistub_sem
);
758 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
760 PCI_SLOT(dev
->devfn
),
761 PCI_FUNC(dev
->devfn
));
763 if (!psdev
|| !psdev
->pdev
) {
765 DRV_NAME
" device is not found/assigned\n");
769 if (!psdev
->pdev
->sh_info
) {
770 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
771 " by HVM, kill it\n");
772 kill_domain_by_device(psdev
);
776 if (!test_bit(_XEN_PCIB_AERHANDLER
,
777 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
779 "guest with no AER driver should have been killed\n");
782 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_mmio
, result
);
784 if (result
== PCI_ERS_RESULT_NONE
||
785 result
== PCI_ERS_RESULT_DISCONNECT
) {
787 "No AER mmio_enabled service or disconnected!\n");
788 kill_domain_by_device(psdev
);
792 pcistub_device_put(psdev
);
793 up_write(&pcistub_sem
);
797 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
798 * in case of the device driver could provide this service, and then wait
800 * @dev: pointer to PCI devices
801 * @error: the current PCI connection state
802 * return value is used by aer_core do_recovery policy
805 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
806 pci_channel_state_t error
)
808 struct pcistub_device
*psdev
;
809 pci_ers_result_t result
;
811 result
= PCI_ERS_RESULT_CAN_RECOVER
;
812 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
813 dev
->bus
->number
, dev
->devfn
);
815 down_write(&pcistub_sem
);
816 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
818 PCI_SLOT(dev
->devfn
),
819 PCI_FUNC(dev
->devfn
));
821 if (!psdev
|| !psdev
->pdev
) {
823 DRV_NAME
" device is not found/assigned\n");
827 if (!psdev
->pdev
->sh_info
) {
828 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
829 " by HVM, kill it\n");
830 kill_domain_by_device(psdev
);
834 /*Guest owns the device yet no aer handler regiested, kill guest*/
835 if (!test_bit(_XEN_PCIB_AERHANDLER
,
836 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
837 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
838 kill_domain_by_device(psdev
);
841 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
843 if (result
== PCI_ERS_RESULT_NONE
||
844 result
== PCI_ERS_RESULT_DISCONNECT
) {
846 "No AER error_detected service or disconnected!\n");
847 kill_domain_by_device(psdev
);
851 pcistub_device_put(psdev
);
852 up_write(&pcistub_sem
);
856 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
857 * in case of the device driver could provide this service, and then wait
859 * @dev: pointer to PCI devices
862 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
864 struct pcistub_device
*psdev
;
866 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
867 dev
->bus
->number
, dev
->devfn
);
869 down_write(&pcistub_sem
);
870 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
872 PCI_SLOT(dev
->devfn
),
873 PCI_FUNC(dev
->devfn
));
875 if (!psdev
|| !psdev
->pdev
) {
877 DRV_NAME
" device is not found/assigned\n");
881 if (!psdev
->pdev
->sh_info
) {
882 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
883 " by HVM, kill it\n");
884 kill_domain_by_device(psdev
);
888 if (!test_bit(_XEN_PCIB_AERHANDLER
,
889 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
891 "guest with no AER driver should have been killed\n");
892 kill_domain_by_device(psdev
);
895 common_process(psdev
, 1, XEN_PCI_OP_aer_resume
,
896 PCI_ERS_RESULT_RECOVERED
);
899 pcistub_device_put(psdev
);
900 up_write(&pcistub_sem
);
904 /*add xen_pcibk AER handling*/
905 static const struct pci_error_handlers xen_pcibk_error_handler
= {
906 .error_detected
= xen_pcibk_error_detected
,
907 .mmio_enabled
= xen_pcibk_mmio_enabled
,
908 .slot_reset
= xen_pcibk_slot_reset
,
909 .resume
= xen_pcibk_error_resume
,
913 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
914 * for a normal device. I don't want it to be loaded automatically.
917 static struct pci_driver xen_pcibk_pci_driver
= {
918 /* The name should be xen_pciback, but until the tools are updated
919 * we will keep it as pciback. */
921 .id_table
= pcistub_ids
,
922 .probe
= pcistub_probe
,
923 .remove
= pcistub_remove
,
924 .err_handler
= &xen_pcibk_error_handler
,
927 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
928 int *slot
, int *func
)
932 switch (sscanf(buf
, " %x:%x:%x.%x %n", domain
, bus
, slot
, func
,
936 sscanf(buf
, " %x:%x:%x.* %n", domain
, bus
, slot
, &parsed
);
940 sscanf(buf
, " %x:%x:*.* %n", domain
, bus
, &parsed
);
943 if (parsed
&& !buf
[parsed
])
946 /* try again without domain */
948 switch (sscanf(buf
, " %x:%x.%x %n", bus
, slot
, func
, &parsed
)) {
951 sscanf(buf
, " %x:%x.* %n", bus
, slot
, &parsed
);
955 sscanf(buf
, " %x:*.* %n", bus
, &parsed
);
958 if (parsed
&& !buf
[parsed
])
964 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
965 *slot
, int *func
, int *reg
, int *size
, int *mask
)
969 sscanf(buf
, " %x:%x:%x.%x-%x:%x:%x %n", domain
, bus
, slot
, func
,
970 reg
, size
, mask
, &parsed
);
971 if (parsed
&& !buf
[parsed
])
974 /* try again without domain */
976 sscanf(buf
, " %x:%x.%x-%x:%x:%x %n", bus
, slot
, func
, reg
, size
,
978 if (parsed
&& !buf
[parsed
])
984 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
986 struct pcistub_device_id
*pci_dev_id
;
988 int rc
= 0, devfn
= PCI_DEVFN(slot
, func
);
991 for (slot
= 0; !rc
&& slot
< 32; ++slot
)
992 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
997 for (func
= 0; !rc
&& func
< 8; ++func
)
998 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1003 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1004 || !defined(CONFIG_PCI_DOMAINS)
1005 !pci_domains_supported
? domain
:
1007 domain
< 0 || domain
> 0xffff)
1008 || bus
< 0 || bus
> 0xff
1009 || PCI_SLOT(devfn
) != slot
1010 || PCI_FUNC(devfn
) != func
)
1013 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
1017 pci_dev_id
->domain
= domain
;
1018 pci_dev_id
->bus
= bus
;
1019 pci_dev_id
->devfn
= devfn
;
1021 pr_debug(DRV_NAME
": wants to seize %04x:%02x:%02x.%d\n",
1022 domain
, bus
, slot
, func
);
1024 spin_lock_irqsave(&device_ids_lock
, flags
);
1025 list_add_tail(&pci_dev_id
->slot_list
, &pcistub_device_ids
);
1026 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1031 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
1033 struct pcistub_device_id
*pci_dev_id
, *t
;
1035 unsigned long flags
;
1037 spin_lock_irqsave(&device_ids_lock
, flags
);
1038 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
1040 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
1041 && (slot
< 0 || PCI_SLOT(pci_dev_id
->devfn
) == slot
)
1042 && (func
< 0 || PCI_FUNC(pci_dev_id
->devfn
) == func
)) {
1043 /* Don't break; here because it's possible the same
1044 * slot could be in the list more than once
1046 list_del(&pci_dev_id
->slot_list
);
1051 pr_debug(DRV_NAME
": removed %04x:%02x:%02x.%d from "
1052 "seize list\n", domain
, bus
, slot
, func
);
1055 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1060 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
,
1061 unsigned int reg
, unsigned int size
,
1065 struct pcistub_device
*psdev
;
1066 struct pci_dev
*dev
;
1067 struct config_field
*field
;
1069 if (reg
> 0xfff || (size
< 4 && (mask
>> (size
* 8))))
1072 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1079 field
= kzalloc(sizeof(*field
), GFP_ATOMIC
);
1085 field
->offset
= reg
;
1089 field
->reset
= NULL
;
1090 field
->release
= NULL
;
1091 field
->clean
= xen_pcibk_config_field_free
;
1093 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
1098 pcistub_device_put(psdev
);
1102 static ssize_t
pcistub_slot_add(struct device_driver
*drv
, const char *buf
,
1105 int domain
, bus
, slot
, func
;
1108 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1112 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1119 static DRIVER_ATTR(new_slot
, S_IWUSR
, NULL
, pcistub_slot_add
);
1121 static ssize_t
pcistub_slot_remove(struct device_driver
*drv
, const char *buf
,
1124 int domain
, bus
, slot
, func
;
1127 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1131 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1138 static DRIVER_ATTR(remove_slot
, S_IWUSR
, NULL
, pcistub_slot_remove
);
1140 static ssize_t
pcistub_slot_show(struct device_driver
*drv
, char *buf
)
1142 struct pcistub_device_id
*pci_dev_id
;
1144 unsigned long flags
;
1146 spin_lock_irqsave(&device_ids_lock
, flags
);
1147 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1148 if (count
>= PAGE_SIZE
)
1151 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1152 "%04x:%02x:%02x.%d\n",
1153 pci_dev_id
->domain
, pci_dev_id
->bus
,
1154 PCI_SLOT(pci_dev_id
->devfn
),
1155 PCI_FUNC(pci_dev_id
->devfn
));
1157 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1161 static DRIVER_ATTR(slots
, S_IRUSR
, pcistub_slot_show
, NULL
);
1163 static ssize_t
pcistub_irq_handler_show(struct device_driver
*drv
, char *buf
)
1165 struct pcistub_device
*psdev
;
1166 struct xen_pcibk_dev_data
*dev_data
;
1168 unsigned long flags
;
1170 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1171 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1172 if (count
>= PAGE_SIZE
)
1176 dev_data
= pci_get_drvdata(psdev
->dev
);
1180 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1181 "%s:%s:%sing:%ld\n",
1182 pci_name(psdev
->dev
),
1183 dev_data
->isr_on
? "on" : "off",
1184 dev_data
->ack_intr
? "ack" : "not ack",
1187 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1190 static DRIVER_ATTR(irq_handlers
, S_IRUSR
, pcistub_irq_handler_show
, NULL
);
1192 static ssize_t
pcistub_irq_handler_switch(struct device_driver
*drv
,
1196 struct pcistub_device
*psdev
;
1197 struct xen_pcibk_dev_data
*dev_data
;
1198 int domain
, bus
, slot
, func
;
1201 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1205 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1209 dev_data
= pci_get_drvdata(psdev
->dev
);
1213 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1214 dev_data
->irq_name
, dev_data
->isr_on
,
1217 dev_data
->isr_on
= !(dev_data
->isr_on
);
1218 if (dev_data
->isr_on
)
1219 dev_data
->ack_intr
= 1;
1222 pcistub_device_put(psdev
);
1227 static DRIVER_ATTR(irq_handler_state
, S_IWUSR
, NULL
,
1228 pcistub_irq_handler_switch
);
1230 static ssize_t
pcistub_quirk_add(struct device_driver
*drv
, const char *buf
,
1233 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1236 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1241 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1249 static ssize_t
pcistub_quirk_show(struct device_driver
*drv
, char *buf
)
1252 unsigned long flags
;
1253 struct xen_pcibk_config_quirk
*quirk
;
1254 struct xen_pcibk_dev_data
*dev_data
;
1255 const struct config_field
*field
;
1256 const struct config_field_entry
*cfg_entry
;
1258 spin_lock_irqsave(&device_ids_lock
, flags
);
1259 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1260 if (count
>= PAGE_SIZE
)
1263 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1264 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1265 quirk
->pdev
->bus
->number
,
1266 PCI_SLOT(quirk
->pdev
->devfn
),
1267 PCI_FUNC(quirk
->pdev
->devfn
),
1268 quirk
->devid
.vendor
, quirk
->devid
.device
,
1269 quirk
->devid
.subvendor
,
1270 quirk
->devid
.subdevice
);
1272 dev_data
= pci_get_drvdata(quirk
->pdev
);
1274 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1275 field
= cfg_entry
->field
;
1276 if (count
>= PAGE_SIZE
)
1279 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1280 "\t\t%08x:%01x:%08x\n",
1281 cfg_entry
->base_offset
+
1282 field
->offset
, field
->size
,
1288 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1292 static DRIVER_ATTR(quirks
, S_IRUSR
| S_IWUSR
, pcistub_quirk_show
,
1295 static ssize_t
permissive_add(struct device_driver
*drv
, const char *buf
,
1298 int domain
, bus
, slot
, func
;
1300 struct pcistub_device
*psdev
;
1301 struct xen_pcibk_dev_data
*dev_data
;
1303 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1307 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1313 dev_data
= pci_get_drvdata(psdev
->dev
);
1314 /* the driver data for a device should never be null at this point */
1319 if (!dev_data
->permissive
) {
1320 dev_data
->permissive
= 1;
1321 /* Let user know that what they're doing could be unsafe */
1322 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1323 "configuration space accesses!\n");
1324 dev_warn(&psdev
->dev
->dev
,
1325 "permissive mode is potentially unsafe!\n");
1328 pcistub_device_put(psdev
);
1335 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1337 struct pcistub_device
*psdev
;
1338 struct xen_pcibk_dev_data
*dev_data
;
1340 unsigned long flags
;
1341 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1342 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1343 if (count
>= PAGE_SIZE
)
1347 dev_data
= pci_get_drvdata(psdev
->dev
);
1348 if (!dev_data
|| !dev_data
->permissive
)
1351 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1352 pci_name(psdev
->dev
));
1354 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1357 static DRIVER_ATTR(permissive
, S_IRUSR
| S_IWUSR
, permissive_show
,
1360 static void pcistub_exit(void)
1362 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1363 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1364 &driver_attr_remove_slot
);
1365 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1366 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1367 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1368 &driver_attr_permissive
);
1369 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1370 &driver_attr_irq_handlers
);
1371 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1372 &driver_attr_irq_handler_state
);
1373 pci_unregister_driver(&xen_pcibk_pci_driver
);
1376 static int __init
pcistub_init(void)
1380 int domain
, bus
, slot
, func
;
1383 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1387 err
= sscanf(pci_devs_to_hide
+ pos
,
1388 " (%x:%x:%x.%x) %n",
1389 &domain
, &bus
, &slot
, &func
, &parsed
);
1393 sscanf(pci_devs_to_hide
+ pos
,
1395 &domain
, &bus
, &slot
, &parsed
);
1399 sscanf(pci_devs_to_hide
+ pos
,
1401 &domain
, &bus
, &parsed
);
1407 err
= sscanf(pci_devs_to_hide
+ pos
,
1409 &bus
, &slot
, &func
, &parsed
);
1413 sscanf(pci_devs_to_hide
+ pos
,
1415 &bus
, &slot
, &parsed
);
1419 sscanf(pci_devs_to_hide
+ pos
,
1429 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1434 } while (pci_devs_to_hide
[pos
]);
1437 /* If we're the first PCI Device Driver to register, we're the
1438 * first one to get offered PCI devices as they become
1439 * available (and thus we can be the first to grab them)
1441 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1445 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1446 &driver_attr_new_slot
);
1448 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1449 &driver_attr_remove_slot
);
1451 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1452 &driver_attr_slots
);
1454 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1455 &driver_attr_quirks
);
1457 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1458 &driver_attr_permissive
);
1461 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1462 &driver_attr_irq_handlers
);
1464 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1465 &driver_attr_irq_handler_state
);
1473 printk(KERN_ERR DRV_NAME
": Error parsing pci_devs_to_hide at \"%s\"\n",
1474 pci_devs_to_hide
+ pos
);
1480 * fs_initcall happens before device_initcall
1481 * so xen_pcibk *should* get called first (b/c we
1482 * want to suck up any device before other drivers
1483 * get a chance by being the first pci device
1484 * driver to register)
1486 fs_initcall(pcistub_init
);
1489 static int __init
xen_pcibk_init(void)
1493 if (!xen_initial_domain())
1496 err
= xen_pcibk_config_init();
1501 err
= pcistub_init();
1506 pcistub_init_devices_late();
1507 err
= xen_pcibk_xenbus_register();
1514 static void __exit
xen_pcibk_cleanup(void)
1516 xen_pcibk_xenbus_unregister();
1520 module_init(xen_pcibk_init
);
1521 module_exit(xen_pcibk_cleanup
);
1523 MODULE_LICENSE("Dual BSD/GPL");
1524 MODULE_ALIAS("xen-backend:pci");