2 * PCI Stub Driver - Grabs devices in backend to be exported later
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
28 #define PCISTUB_DRIVER_NAME "pciback"
30 static char *pci_devs_to_hide
;
31 wait_queue_head_t xen_pcibk_aer_wait_queue
;
32 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
35 static DECLARE_RWSEM(pcistub_sem
);
36 module_param_named(hide
, pci_devs_to_hide
, charp
, 0444);
38 struct pcistub_device_id
{
39 struct list_head slot_list
;
44 static LIST_HEAD(pcistub_device_ids
);
45 static DEFINE_SPINLOCK(device_ids_lock
);
47 struct pcistub_device
{
49 struct list_head dev_list
;
53 struct xen_pcibk_device
*pdev
;/* non-NULL if struct pci_dev is in use */
56 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
57 * flag must be locked with pcistub_devices_lock
59 static DEFINE_SPINLOCK(pcistub_devices_lock
);
60 static LIST_HEAD(pcistub_devices
);
62 /* wait for device_initcall before initializing our devices
63 * (see pcistub_init_devices_late)
65 static int initialize_devices
;
66 static LIST_HEAD(seized_devices
);
68 static struct pcistub_device
*pcistub_device_alloc(struct pci_dev
*dev
)
70 struct pcistub_device
*psdev
;
72 dev_dbg(&dev
->dev
, "pcistub_device_alloc\n");
74 psdev
= kzalloc(sizeof(*psdev
), GFP_ATOMIC
);
78 psdev
->dev
= pci_dev_get(dev
);
84 kref_init(&psdev
->kref
);
85 spin_lock_init(&psdev
->lock
);
90 /* Don't call this directly as it's called by pcistub_device_put */
91 static void pcistub_device_release(struct kref
*kref
)
93 struct pcistub_device
*psdev
;
95 struct xen_pcibk_dev_data
*dev_data
;
97 psdev
= container_of(kref
, struct pcistub_device
, kref
);
99 dev_data
= pci_get_drvdata(dev
);
101 dev_dbg(&dev
->dev
, "pcistub_device_release\n");
103 xen_unregister_device_domain_owner(dev
);
105 /* Call the reset function which does not take lock as this
106 * is called from "unbind" which takes a device_lock mutex.
108 __pci_reset_function_locked(dev
);
109 if (pci_load_and_free_saved_state(dev
, &dev_data
->pci_saved_state
))
110 dev_info(&dev
->dev
, "Could not reload PCI state\n");
112 pci_restore_state(dev
);
115 struct physdev_pci_device ppdev
= {
116 .seg
= pci_domain_nr(dev
->bus
),
117 .bus
= dev
->bus
->number
,
120 int err
= HYPERVISOR_physdev_op(PHYSDEVOP_release_msix
,
123 if (err
&& err
!= -ENOSYS
)
124 dev_warn(&dev
->dev
, "MSI-X release failed (%d)\n",
128 /* Disable the device */
129 xen_pcibk_reset_device(dev
);
132 pci_set_drvdata(dev
, NULL
);
134 /* Clean-up the device */
135 xen_pcibk_config_free_dyn_fields(dev
);
136 xen_pcibk_config_free_dev(dev
);
138 pci_clear_dev_assigned(dev
);
144 static inline void pcistub_device_get(struct pcistub_device
*psdev
)
146 kref_get(&psdev
->kref
);
149 static inline void pcistub_device_put(struct pcistub_device
*psdev
)
151 kref_put(&psdev
->kref
, pcistub_device_release
);
154 static struct pcistub_device
*pcistub_device_find_locked(int domain
, int bus
,
157 struct pcistub_device
*psdev
;
159 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
160 if (psdev
->dev
!= NULL
161 && domain
== pci_domain_nr(psdev
->dev
->bus
)
162 && bus
== psdev
->dev
->bus
->number
163 && slot
== PCI_SLOT(psdev
->dev
->devfn
)
164 && func
== PCI_FUNC(psdev
->dev
->devfn
)) {
172 static struct pcistub_device
*pcistub_device_find(int domain
, int bus
,
175 struct pcistub_device
*psdev
;
178 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
180 psdev
= pcistub_device_find_locked(domain
, bus
, slot
, func
);
182 pcistub_device_get(psdev
);
184 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
188 static struct pci_dev
*pcistub_device_get_pci_dev(struct xen_pcibk_device
*pdev
,
189 struct pcistub_device
*psdev
)
191 struct pci_dev
*pci_dev
= NULL
;
194 pcistub_device_get(psdev
);
196 spin_lock_irqsave(&psdev
->lock
, flags
);
199 pci_dev
= psdev
->dev
;
201 spin_unlock_irqrestore(&psdev
->lock
, flags
);
204 pcistub_device_put(psdev
);
209 struct pci_dev
*pcistub_get_pci_dev_by_slot(struct xen_pcibk_device
*pdev
,
213 struct pcistub_device
*psdev
;
214 struct pci_dev
*found_dev
= NULL
;
217 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
219 psdev
= pcistub_device_find_locked(domain
, bus
, slot
, func
);
221 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
223 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
227 struct pci_dev
*pcistub_get_pci_dev(struct xen_pcibk_device
*pdev
,
230 struct pcistub_device
*psdev
;
231 struct pci_dev
*found_dev
= NULL
;
234 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
236 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
237 if (psdev
->dev
== dev
) {
238 found_dev
= pcistub_device_get_pci_dev(pdev
, psdev
);
243 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
249 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
250 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
251 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
252 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
254 * As such we have to be careful.
256 * To make this easier, the caller has to hold the device lock.
258 void pcistub_put_pci_dev(struct pci_dev
*dev
)
260 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
262 struct xen_pcibk_dev_data
*dev_data
;
265 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
267 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
268 if (psdev
->dev
== dev
) {
274 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
275 if (WARN_ON(!found_psdev
))
278 /*hold this lock for avoiding breaking link between
279 * pcistub and xen_pcibk when AER is in processing
281 down_write(&pcistub_sem
);
282 /* Cleanup our device
283 * (so it's ready for the next domain)
285 device_lock_assert(&dev
->dev
);
286 __pci_reset_function_locked(dev
);
288 dev_data
= pci_get_drvdata(dev
);
289 ret
= pci_load_saved_state(dev
, dev_data
->pci_saved_state
);
292 * The usual sequence is pci_save_state & pci_restore_state
293 * but the guest might have messed the configuration space up.
294 * Use the initial version (when device was bound to us).
296 pci_restore_state(dev
);
298 dev_info(&dev
->dev
, "Could not reload PCI state\n");
299 /* This disables the device. */
300 xen_pcibk_reset_device(dev
);
302 /* And cleanup up our emulated fields. */
303 xen_pcibk_config_reset_dev(dev
);
304 xen_pcibk_config_free_dyn_fields(dev
);
306 xen_unregister_device_domain_owner(dev
);
308 spin_lock_irqsave(&found_psdev
->lock
, flags
);
309 found_psdev
->pdev
= NULL
;
310 spin_unlock_irqrestore(&found_psdev
->lock
, flags
);
312 pcistub_device_put(found_psdev
);
313 up_write(&pcistub_sem
);
316 static int pcistub_match_one(struct pci_dev
*dev
,
317 struct pcistub_device_id
*pdev_id
)
319 /* Match the specified device by domain, bus, slot, func and also if
320 * any of the device's parent bridges match.
322 for (; dev
!= NULL
; dev
= dev
->bus
->self
) {
323 if (pci_domain_nr(dev
->bus
) == pdev_id
->domain
324 && dev
->bus
->number
== pdev_id
->bus
325 && dev
->devfn
== pdev_id
->devfn
)
328 /* Sometimes topmost bridge links to itself. */
329 if (dev
== dev
->bus
->self
)
336 static int pcistub_match(struct pci_dev
*dev
)
338 struct pcistub_device_id
*pdev_id
;
342 spin_lock_irqsave(&device_ids_lock
, flags
);
343 list_for_each_entry(pdev_id
, &pcistub_device_ids
, slot_list
) {
344 if (pcistub_match_one(dev
, pdev_id
)) {
349 spin_unlock_irqrestore(&device_ids_lock
, flags
);
354 static int pcistub_init_device(struct pci_dev
*dev
)
356 struct xen_pcibk_dev_data
*dev_data
;
359 dev_dbg(&dev
->dev
, "initializing...\n");
361 /* The PCI backend is not intended to be a module (or to work with
362 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
363 * would need to be called somewhere to free the memory allocated
364 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
366 dev_data
= kzalloc(sizeof(*dev_data
) + strlen(DRV_NAME
"[]")
367 + strlen(pci_name(dev
)) + 1, GFP_ATOMIC
);
372 pci_set_drvdata(dev
, dev_data
);
375 * Setup name for fake IRQ handler. It will only be enabled
376 * once the device is turned on by the guest.
378 sprintf(dev_data
->irq_name
, DRV_NAME
"[%s]", pci_name(dev
));
380 dev_dbg(&dev
->dev
, "initializing config\n");
382 init_waitqueue_head(&xen_pcibk_aer_wait_queue
);
383 err
= xen_pcibk_config_init_dev(dev
);
387 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
388 * must do this here because pcibios_enable_device may specify
389 * the pci device's true irq (and possibly its other resources)
390 * if they differ from what's in the configuration space.
391 * This makes the assumption that the device's resources won't
392 * change after this point (otherwise this code may break!)
394 dev_dbg(&dev
->dev
, "enabling device\n");
395 err
= pci_enable_device(dev
);
400 struct physdev_pci_device ppdev
= {
401 .seg
= pci_domain_nr(dev
->bus
),
402 .bus
= dev
->bus
->number
,
406 err
= HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix
, &ppdev
);
407 if (err
&& err
!= -ENOSYS
)
408 dev_err(&dev
->dev
, "MSI-X preparation failed (%d)\n",
412 /* We need the device active to save the state. */
413 dev_dbg(&dev
->dev
, "save state of device\n");
415 dev_data
->pci_saved_state
= pci_store_saved_state(dev
);
416 if (!dev_data
->pci_saved_state
)
417 dev_err(&dev
->dev
, "Could not store PCI conf saved state!\n");
419 dev_dbg(&dev
->dev
, "resetting (FLR, D3, etc) the device\n");
420 __pci_reset_function_locked(dev
);
421 pci_restore_state(dev
);
423 /* Now disable the device (this also ensures some private device
424 * data is setup before we export)
426 dev_dbg(&dev
->dev
, "reset device\n");
427 xen_pcibk_reset_device(dev
);
429 pci_set_dev_assigned(dev
);
433 xen_pcibk_config_free_dev(dev
);
436 pci_set_drvdata(dev
, NULL
);
442 * Because some initialization still happens on
443 * devices during fs_initcall, we need to defer
444 * full initialization of our devices until
447 static int __init
pcistub_init_devices_late(void)
449 struct pcistub_device
*psdev
;
453 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
455 while (!list_empty(&seized_devices
)) {
456 psdev
= container_of(seized_devices
.next
,
457 struct pcistub_device
, dev_list
);
458 list_del(&psdev
->dev_list
);
460 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
462 err
= pcistub_init_device(psdev
->dev
);
464 dev_err(&psdev
->dev
->dev
,
465 "error %d initializing device\n", err
);
470 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
473 list_add_tail(&psdev
->dev_list
, &pcistub_devices
);
476 initialize_devices
= 1;
478 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
483 static void pcistub_device_id_add_list(struct pcistub_device_id
*new,
484 int domain
, int bus
, unsigned int devfn
)
486 struct pcistub_device_id
*pci_dev_id
;
490 spin_lock_irqsave(&device_ids_lock
, flags
);
492 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
493 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
&&
494 pci_dev_id
->devfn
== devfn
) {
501 new->domain
= domain
;
504 list_add_tail(&new->slot_list
, &pcistub_device_ids
);
507 spin_unlock_irqrestore(&device_ids_lock
, flags
);
513 static int pcistub_seize(struct pci_dev
*dev
,
514 struct pcistub_device_id
*pci_dev_id
)
516 struct pcistub_device
*psdev
;
520 psdev
= pcistub_device_alloc(dev
);
526 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
528 if (initialize_devices
) {
529 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
531 /* don't want irqs disabled when calling pcistub_init_device */
532 err
= pcistub_init_device(psdev
->dev
);
534 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
537 list_add(&psdev
->dev_list
, &pcistub_devices
);
539 dev_dbg(&dev
->dev
, "deferring initialization\n");
540 list_add(&psdev
->dev_list
, &seized_devices
);
543 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
547 pcistub_device_put(psdev
);
548 } else if (pci_dev_id
)
549 pcistub_device_id_add_list(pci_dev_id
, pci_domain_nr(dev
->bus
),
550 dev
->bus
->number
, dev
->devfn
);
555 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
556 * other functions that take the sysfs lock. */
557 static int pcistub_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
560 struct pcistub_device_id
*pci_dev_id
= NULL
;
562 dev_dbg(&dev
->dev
, "probing...\n");
564 match
= pcistub_match(dev
);
566 if ((dev
->driver_override
&&
567 !strcmp(dev
->driver_override
, PCISTUB_DRIVER_NAME
)) ||
570 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
571 && dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
572 dev_err(&dev
->dev
, "can't export pci devices that "
573 "don't have a normal (0) or bridge (1) "
580 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_ATOMIC
);
587 dev_info(&dev
->dev
, "seizing device\n");
588 err
= pcistub_seize(dev
, pci_dev_id
);
590 /* Didn't find the device */
597 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
598 * other functions that take the sysfs lock. */
599 static void pcistub_remove(struct pci_dev
*dev
)
601 struct pcistub_device
*psdev
, *found_psdev
= NULL
;
604 dev_dbg(&dev
->dev
, "removing\n");
606 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
608 xen_pcibk_config_quirk_release(dev
);
610 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
611 if (psdev
->dev
== dev
) {
617 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
620 dev_dbg(&dev
->dev
, "found device to remove %s\n",
621 found_psdev
->pdev
? "- in-use" : "");
623 if (found_psdev
->pdev
) {
624 int domid
= xen_find_device_domain_owner(dev
);
626 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
627 pci_name(found_psdev
->dev
), domid
);
628 pr_warn("****** driver domain may still access this device's i/o resources!\n");
629 pr_warn("****** shutdown driver domain before binding device\n");
630 pr_warn("****** to other drivers or domains\n");
632 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
634 xen_pcibk_release_pci_dev(found_psdev
->pdev
,
636 false /* caller holds the lock. */);
639 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
640 list_del(&found_psdev
->dev_list
);
641 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
643 /* the final put for releasing from the list */
644 pcistub_device_put(found_psdev
);
648 static const struct pci_device_id pcistub_ids
[] = {
650 .vendor
= PCI_ANY_ID
,
651 .device
= PCI_ANY_ID
,
652 .subvendor
= PCI_ANY_ID
,
653 .subdevice
= PCI_ANY_ID
,
658 #define PCI_NODENAME_MAX 40
659 static void kill_domain_by_device(struct pcistub_device
*psdev
)
661 struct xenbus_transaction xbt
;
663 char nodename
[PCI_NODENAME_MAX
];
666 snprintf(nodename
, PCI_NODENAME_MAX
, "/local/domain/0/backend/pci/%d/0",
667 psdev
->pdev
->xdev
->otherend_id
);
670 err
= xenbus_transaction_start(&xbt
);
672 dev_err(&psdev
->dev
->dev
,
673 "error %d when start xenbus transaction\n", err
);
676 /*PV AER handlers will set this flag*/
677 xenbus_printf(xbt
, nodename
, "aerState" , "aerfail");
678 err
= xenbus_transaction_end(xbt
, 0);
682 dev_err(&psdev
->dev
->dev
,
683 "error %d when end xenbus transaction\n", err
);
688 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
689 * backend need to have cooperation. In xen_pcibk, those steps will do similar
690 * jobs: send service request and waiting for front_end response.
692 static pci_ers_result_t
common_process(struct pcistub_device
*psdev
,
693 pci_channel_state_t state
, int aer_cmd
,
694 pci_ers_result_t result
)
696 pci_ers_result_t res
= result
;
697 struct xen_pcie_aer_op
*aer_op
;
698 struct xen_pcibk_device
*pdev
= psdev
->pdev
;
699 struct xen_pci_sharedinfo
*sh_info
= pdev
->sh_info
;
702 /*with PV AER drivers*/
703 aer_op
= &(sh_info
->aer_op
);
704 aer_op
->cmd
= aer_cmd
;
705 /*useful for error_detected callback*/
708 ret
= xen_pcibk_get_pcifront_dev(psdev
->dev
, psdev
->pdev
,
709 &aer_op
->domain
, &aer_op
->bus
, &aer_op
->devfn
);
711 dev_err(&psdev
->dev
->dev
,
712 DRV_NAME
": failed to get pcifront device\n");
713 return PCI_ERS_RESULT_NONE
;
717 dev_dbg(&psdev
->dev
->dev
,
718 DRV_NAME
": aer_op %x dom %x bus %x devfn %x\n",
719 aer_cmd
, aer_op
->domain
, aer_op
->bus
, aer_op
->devfn
);
720 /*local flag to mark there's aer request, xen_pcibk callback will use
721 * this flag to judge whether we need to check pci-front give aer
724 set_bit(_PCIB_op_pending
, (unsigned long *)&pdev
->flags
);
726 /*It is possible that a pcifront conf_read_write ops request invokes
727 * the callback which cause the spurious execution of wake_up.
728 * Yet it is harmless and better than a spinlock here
730 set_bit(_XEN_PCIB_active
,
731 (unsigned long *)&sh_info
->flags
);
733 notify_remote_via_irq(pdev
->evtchn_irq
);
735 ret
= wait_event_timeout(xen_pcibk_aer_wait_queue
,
736 !(test_bit(_XEN_PCIB_active
, (unsigned long *)
737 &sh_info
->flags
)), 300*HZ
);
740 if (test_bit(_XEN_PCIB_active
,
741 (unsigned long *)&sh_info
->flags
)) {
742 dev_err(&psdev
->dev
->dev
,
743 "pcifront aer process not responding!\n");
744 clear_bit(_XEN_PCIB_active
,
745 (unsigned long *)&sh_info
->flags
);
746 aer_op
->err
= PCI_ERS_RESULT_NONE
;
750 clear_bit(_PCIB_op_pending
, (unsigned long *)&pdev
->flags
);
752 if (test_bit(_XEN_PCIF_active
,
753 (unsigned long *)&sh_info
->flags
)) {
754 dev_dbg(&psdev
->dev
->dev
,
755 "schedule pci_conf service in " DRV_NAME
"\n");
756 xen_pcibk_test_and_schedule_op(psdev
->pdev
);
759 res
= (pci_ers_result_t
)aer_op
->err
;
764 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
765 * of the device driver could provide this service, and then wait for pcifront
767 * @dev: pointer to PCI devices
768 * return value is used by aer_core do_recovery policy
770 static pci_ers_result_t
xen_pcibk_slot_reset(struct pci_dev
*dev
)
772 struct pcistub_device
*psdev
;
773 pci_ers_result_t result
;
775 result
= PCI_ERS_RESULT_RECOVERED
;
776 dev_dbg(&dev
->dev
, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
777 dev
->bus
->number
, dev
->devfn
);
779 down_write(&pcistub_sem
);
780 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
782 PCI_SLOT(dev
->devfn
),
783 PCI_FUNC(dev
->devfn
));
785 if (!psdev
|| !psdev
->pdev
) {
787 DRV_NAME
" device is not found/assigned\n");
791 if (!psdev
->pdev
->sh_info
) {
792 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
793 " by HVM, kill it\n");
794 kill_domain_by_device(psdev
);
798 if (!test_bit(_XEN_PCIB_AERHANDLER
,
799 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
801 "guest with no AER driver should have been killed\n");
804 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_slotreset
, result
);
806 if (result
== PCI_ERS_RESULT_NONE
||
807 result
== PCI_ERS_RESULT_DISCONNECT
) {
809 "No AER slot_reset service or disconnected!\n");
810 kill_domain_by_device(psdev
);
814 pcistub_device_put(psdev
);
815 up_write(&pcistub_sem
);
821 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
822 * in case of the device driver could provide this service, and then wait
824 * @dev: pointer to PCI devices
825 * return value is used by aer_core do_recovery policy
828 static pci_ers_result_t
xen_pcibk_mmio_enabled(struct pci_dev
*dev
)
830 struct pcistub_device
*psdev
;
831 pci_ers_result_t result
;
833 result
= PCI_ERS_RESULT_RECOVERED
;
834 dev_dbg(&dev
->dev
, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
835 dev
->bus
->number
, dev
->devfn
);
837 down_write(&pcistub_sem
);
838 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
840 PCI_SLOT(dev
->devfn
),
841 PCI_FUNC(dev
->devfn
));
843 if (!psdev
|| !psdev
->pdev
) {
845 DRV_NAME
" device is not found/assigned\n");
849 if (!psdev
->pdev
->sh_info
) {
850 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
851 " by HVM, kill it\n");
852 kill_domain_by_device(psdev
);
856 if (!test_bit(_XEN_PCIB_AERHANDLER
,
857 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
859 "guest with no AER driver should have been killed\n");
862 result
= common_process(psdev
, 1, XEN_PCI_OP_aer_mmio
, result
);
864 if (result
== PCI_ERS_RESULT_NONE
||
865 result
== PCI_ERS_RESULT_DISCONNECT
) {
867 "No AER mmio_enabled service or disconnected!\n");
868 kill_domain_by_device(psdev
);
872 pcistub_device_put(psdev
);
873 up_write(&pcistub_sem
);
877 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
878 * in case of the device driver could provide this service, and then wait
880 * @dev: pointer to PCI devices
881 * @error: the current PCI connection state
882 * return value is used by aer_core do_recovery policy
885 static pci_ers_result_t
xen_pcibk_error_detected(struct pci_dev
*dev
,
886 pci_channel_state_t error
)
888 struct pcistub_device
*psdev
;
889 pci_ers_result_t result
;
891 result
= PCI_ERS_RESULT_CAN_RECOVER
;
892 dev_dbg(&dev
->dev
, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
893 dev
->bus
->number
, dev
->devfn
);
895 down_write(&pcistub_sem
);
896 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
898 PCI_SLOT(dev
->devfn
),
899 PCI_FUNC(dev
->devfn
));
901 if (!psdev
|| !psdev
->pdev
) {
903 DRV_NAME
" device is not found/assigned\n");
907 if (!psdev
->pdev
->sh_info
) {
908 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
909 " by HVM, kill it\n");
910 kill_domain_by_device(psdev
);
914 /*Guest owns the device yet no aer handler regiested, kill guest*/
915 if (!test_bit(_XEN_PCIB_AERHANDLER
,
916 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
917 dev_dbg(&dev
->dev
, "guest may have no aer driver, kill it\n");
918 kill_domain_by_device(psdev
);
921 result
= common_process(psdev
, error
, XEN_PCI_OP_aer_detected
, result
);
923 if (result
== PCI_ERS_RESULT_NONE
||
924 result
== PCI_ERS_RESULT_DISCONNECT
) {
926 "No AER error_detected service or disconnected!\n");
927 kill_domain_by_device(psdev
);
931 pcistub_device_put(psdev
);
932 up_write(&pcistub_sem
);
936 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
937 * in case of the device driver could provide this service, and then wait
939 * @dev: pointer to PCI devices
942 static void xen_pcibk_error_resume(struct pci_dev
*dev
)
944 struct pcistub_device
*psdev
;
946 dev_dbg(&dev
->dev
, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
947 dev
->bus
->number
, dev
->devfn
);
949 down_write(&pcistub_sem
);
950 psdev
= pcistub_device_find(pci_domain_nr(dev
->bus
),
952 PCI_SLOT(dev
->devfn
),
953 PCI_FUNC(dev
->devfn
));
955 if (!psdev
|| !psdev
->pdev
) {
957 DRV_NAME
" device is not found/assigned\n");
961 if (!psdev
->pdev
->sh_info
) {
962 dev_err(&dev
->dev
, DRV_NAME
" device is not connected or owned"
963 " by HVM, kill it\n");
964 kill_domain_by_device(psdev
);
968 if (!test_bit(_XEN_PCIB_AERHANDLER
,
969 (unsigned long *)&psdev
->pdev
->sh_info
->flags
)) {
971 "guest with no AER driver should have been killed\n");
972 kill_domain_by_device(psdev
);
975 common_process(psdev
, 1, XEN_PCI_OP_aer_resume
,
976 PCI_ERS_RESULT_RECOVERED
);
979 pcistub_device_put(psdev
);
980 up_write(&pcistub_sem
);
984 /*add xen_pcibk AER handling*/
985 static const struct pci_error_handlers xen_pcibk_error_handler
= {
986 .error_detected
= xen_pcibk_error_detected
,
987 .mmio_enabled
= xen_pcibk_mmio_enabled
,
988 .slot_reset
= xen_pcibk_slot_reset
,
989 .resume
= xen_pcibk_error_resume
,
993 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
994 * for a normal device. I don't want it to be loaded automatically.
997 static struct pci_driver xen_pcibk_pci_driver
= {
998 /* The name should be xen_pciback, but until the tools are updated
999 * we will keep it as pciback. */
1000 .name
= PCISTUB_DRIVER_NAME
,
1001 .id_table
= pcistub_ids
,
1002 .probe
= pcistub_probe
,
1003 .remove
= pcistub_remove
,
1004 .err_handler
= &xen_pcibk_error_handler
,
1007 static inline int str_to_slot(const char *buf
, int *domain
, int *bus
,
1008 int *slot
, int *func
)
1012 switch (sscanf(buf
, " %x:%x:%x.%x %n", domain
, bus
, slot
, func
,
1016 sscanf(buf
, " %x:%x:%x.* %n", domain
, bus
, slot
, &parsed
);
1020 sscanf(buf
, " %x:%x:*.* %n", domain
, bus
, &parsed
);
1023 if (parsed
&& !buf
[parsed
])
1026 /* try again without domain */
1028 switch (sscanf(buf
, " %x:%x.%x %n", bus
, slot
, func
, &parsed
)) {
1031 sscanf(buf
, " %x:%x.* %n", bus
, slot
, &parsed
);
1035 sscanf(buf
, " %x:*.* %n", bus
, &parsed
);
1038 if (parsed
&& !buf
[parsed
])
1044 static inline int str_to_quirk(const char *buf
, int *domain
, int *bus
, int
1045 *slot
, int *func
, int *reg
, int *size
, int *mask
)
1049 sscanf(buf
, " %x:%x:%x.%x-%x:%x:%x %n", domain
, bus
, slot
, func
,
1050 reg
, size
, mask
, &parsed
);
1051 if (parsed
&& !buf
[parsed
])
1054 /* try again without domain */
1056 sscanf(buf
, " %x:%x.%x-%x:%x:%x %n", bus
, slot
, func
, reg
, size
,
1058 if (parsed
&& !buf
[parsed
])
1064 static int pcistub_device_id_add(int domain
, int bus
, int slot
, int func
)
1066 struct pcistub_device_id
*pci_dev_id
;
1067 int rc
= 0, devfn
= PCI_DEVFN(slot
, func
);
1070 for (slot
= 0; !rc
&& slot
< 32; ++slot
)
1071 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1076 for (func
= 0; !rc
&& func
< 8; ++func
)
1077 rc
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1082 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1083 || !defined(CONFIG_PCI_DOMAINS)
1084 !pci_domains_supported
? domain
:
1086 domain
< 0 || domain
> 0xffff)
1087 || bus
< 0 || bus
> 0xff
1088 || PCI_SLOT(devfn
) != slot
1089 || PCI_FUNC(devfn
) != func
)
1092 pci_dev_id
= kmalloc(sizeof(*pci_dev_id
), GFP_KERNEL
);
1096 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1097 domain
, bus
, slot
, func
);
1099 pcistub_device_id_add_list(pci_dev_id
, domain
, bus
, devfn
);
1104 static int pcistub_device_id_remove(int domain
, int bus
, int slot
, int func
)
1106 struct pcistub_device_id
*pci_dev_id
, *t
;
1108 unsigned long flags
;
1110 spin_lock_irqsave(&device_ids_lock
, flags
);
1111 list_for_each_entry_safe(pci_dev_id
, t
, &pcistub_device_ids
,
1113 if (pci_dev_id
->domain
== domain
&& pci_dev_id
->bus
== bus
1114 && (slot
< 0 || PCI_SLOT(pci_dev_id
->devfn
) == slot
)
1115 && (func
< 0 || PCI_FUNC(pci_dev_id
->devfn
) == func
)) {
1116 /* Don't break; here because it's possible the same
1117 * slot could be in the list more than once
1119 list_del(&pci_dev_id
->slot_list
);
1124 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1125 domain
, bus
, slot
, func
);
1128 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1133 static int pcistub_reg_add(int domain
, int bus
, int slot
, int func
,
1134 unsigned int reg
, unsigned int size
,
1138 struct pcistub_device
*psdev
;
1139 struct pci_dev
*dev
;
1140 struct config_field
*field
;
1142 if (reg
> 0xfff || (size
< 4 && (mask
>> (size
* 8))))
1145 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1152 field
= kzalloc(sizeof(*field
), GFP_ATOMIC
);
1158 field
->offset
= reg
;
1162 field
->reset
= NULL
;
1163 field
->release
= NULL
;
1164 field
->clean
= xen_pcibk_config_field_free
;
1166 err
= xen_pcibk_config_quirks_add_field(dev
, field
);
1171 pcistub_device_put(psdev
);
1175 static ssize_t
new_slot_store(struct device_driver
*drv
, const char *buf
,
1178 int domain
, bus
, slot
, func
;
1181 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1185 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1192 static DRIVER_ATTR_WO(new_slot
);
1194 static ssize_t
remove_slot_store(struct device_driver
*drv
, const char *buf
,
1197 int domain
, bus
, slot
, func
;
1200 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1204 err
= pcistub_device_id_remove(domain
, bus
, slot
, func
);
1211 static DRIVER_ATTR_WO(remove_slot
);
1213 static ssize_t
slots_show(struct device_driver
*drv
, char *buf
)
1215 struct pcistub_device_id
*pci_dev_id
;
1217 unsigned long flags
;
1219 spin_lock_irqsave(&device_ids_lock
, flags
);
1220 list_for_each_entry(pci_dev_id
, &pcistub_device_ids
, slot_list
) {
1221 if (count
>= PAGE_SIZE
)
1224 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1225 "%04x:%02x:%02x.%d\n",
1226 pci_dev_id
->domain
, pci_dev_id
->bus
,
1227 PCI_SLOT(pci_dev_id
->devfn
),
1228 PCI_FUNC(pci_dev_id
->devfn
));
1230 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1234 static DRIVER_ATTR_RO(slots
);
1236 static ssize_t
irq_handlers_show(struct device_driver
*drv
, char *buf
)
1238 struct pcistub_device
*psdev
;
1239 struct xen_pcibk_dev_data
*dev_data
;
1241 unsigned long flags
;
1243 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1244 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1245 if (count
>= PAGE_SIZE
)
1249 dev_data
= pci_get_drvdata(psdev
->dev
);
1253 scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1254 "%s:%s:%sing:%ld\n",
1255 pci_name(psdev
->dev
),
1256 dev_data
->isr_on
? "on" : "off",
1257 dev_data
->ack_intr
? "ack" : "not ack",
1260 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1263 static DRIVER_ATTR_RO(irq_handlers
);
1265 static ssize_t
irq_handler_state_store(struct device_driver
*drv
,
1266 const char *buf
, size_t count
)
1268 struct pcistub_device
*psdev
;
1269 struct xen_pcibk_dev_data
*dev_data
;
1270 int domain
, bus
, slot
, func
;
1273 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1277 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1283 dev_data
= pci_get_drvdata(psdev
->dev
);
1289 dev_dbg(&psdev
->dev
->dev
, "%s fake irq handler: %d->%d\n",
1290 dev_data
->irq_name
, dev_data
->isr_on
,
1293 dev_data
->isr_on
= !(dev_data
->isr_on
);
1294 if (dev_data
->isr_on
)
1295 dev_data
->ack_intr
= 1;
1298 pcistub_device_put(psdev
);
1303 static DRIVER_ATTR_WO(irq_handler_state
);
1305 static ssize_t
quirks_store(struct device_driver
*drv
, const char *buf
,
1308 int domain
, bus
, slot
, func
, reg
, size
, mask
;
1311 err
= str_to_quirk(buf
, &domain
, &bus
, &slot
, &func
, ®
, &size
,
1316 err
= pcistub_reg_add(domain
, bus
, slot
, func
, reg
, size
, mask
);
1324 static ssize_t
quirks_show(struct device_driver
*drv
, char *buf
)
1327 unsigned long flags
;
1328 struct xen_pcibk_config_quirk
*quirk
;
1329 struct xen_pcibk_dev_data
*dev_data
;
1330 const struct config_field
*field
;
1331 const struct config_field_entry
*cfg_entry
;
1333 spin_lock_irqsave(&device_ids_lock
, flags
);
1334 list_for_each_entry(quirk
, &xen_pcibk_quirks
, quirks_list
) {
1335 if (count
>= PAGE_SIZE
)
1338 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1339 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1340 quirk
->pdev
->bus
->number
,
1341 PCI_SLOT(quirk
->pdev
->devfn
),
1342 PCI_FUNC(quirk
->pdev
->devfn
),
1343 quirk
->devid
.vendor
, quirk
->devid
.device
,
1344 quirk
->devid
.subvendor
,
1345 quirk
->devid
.subdevice
);
1347 dev_data
= pci_get_drvdata(quirk
->pdev
);
1349 list_for_each_entry(cfg_entry
, &dev_data
->config_fields
, list
) {
1350 field
= cfg_entry
->field
;
1351 if (count
>= PAGE_SIZE
)
1354 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1355 "\t\t%08x:%01x:%08x\n",
1356 cfg_entry
->base_offset
+
1357 field
->offset
, field
->size
,
1363 spin_unlock_irqrestore(&device_ids_lock
, flags
);
1367 static DRIVER_ATTR_RW(quirks
);
1369 static ssize_t
permissive_store(struct device_driver
*drv
, const char *buf
,
1372 int domain
, bus
, slot
, func
;
1374 struct pcistub_device
*psdev
;
1375 struct xen_pcibk_dev_data
*dev_data
;
1377 err
= str_to_slot(buf
, &domain
, &bus
, &slot
, &func
);
1381 psdev
= pcistub_device_find(domain
, bus
, slot
, func
);
1387 dev_data
= pci_get_drvdata(psdev
->dev
);
1388 /* the driver data for a device should never be null at this point */
1393 if (!dev_data
->permissive
) {
1394 dev_data
->permissive
= 1;
1395 /* Let user know that what they're doing could be unsafe */
1396 dev_warn(&psdev
->dev
->dev
, "enabling permissive mode "
1397 "configuration space accesses!\n");
1398 dev_warn(&psdev
->dev
->dev
,
1399 "permissive mode is potentially unsafe!\n");
1402 pcistub_device_put(psdev
);
1409 static ssize_t
permissive_show(struct device_driver
*drv
, char *buf
)
1411 struct pcistub_device
*psdev
;
1412 struct xen_pcibk_dev_data
*dev_data
;
1414 unsigned long flags
;
1415 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1416 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1417 if (count
>= PAGE_SIZE
)
1421 dev_data
= pci_get_drvdata(psdev
->dev
);
1422 if (!dev_data
|| !dev_data
->permissive
)
1425 scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s\n",
1426 pci_name(psdev
->dev
));
1428 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1431 static DRIVER_ATTR_RW(permissive
);
1433 static void pcistub_exit(void)
1435 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_new_slot
);
1436 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1437 &driver_attr_remove_slot
);
1438 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_slots
);
1439 driver_remove_file(&xen_pcibk_pci_driver
.driver
, &driver_attr_quirks
);
1440 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1441 &driver_attr_permissive
);
1442 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1443 &driver_attr_irq_handlers
);
1444 driver_remove_file(&xen_pcibk_pci_driver
.driver
,
1445 &driver_attr_irq_handler_state
);
1446 pci_unregister_driver(&xen_pcibk_pci_driver
);
1449 static int __init
pcistub_init(void)
1453 int domain
, bus
, slot
, func
;
1456 if (pci_devs_to_hide
&& *pci_devs_to_hide
) {
1460 err
= sscanf(pci_devs_to_hide
+ pos
,
1461 " (%x:%x:%x.%x) %n",
1462 &domain
, &bus
, &slot
, &func
, &parsed
);
1466 sscanf(pci_devs_to_hide
+ pos
,
1468 &domain
, &bus
, &slot
, &parsed
);
1472 sscanf(pci_devs_to_hide
+ pos
,
1474 &domain
, &bus
, &parsed
);
1480 err
= sscanf(pci_devs_to_hide
+ pos
,
1482 &bus
, &slot
, &func
, &parsed
);
1486 sscanf(pci_devs_to_hide
+ pos
,
1488 &bus
, &slot
, &parsed
);
1492 sscanf(pci_devs_to_hide
+ pos
,
1502 err
= pcistub_device_id_add(domain
, bus
, slot
, func
);
1507 } while (pci_devs_to_hide
[pos
]);
1510 /* If we're the first PCI Device Driver to register, we're the
1511 * first one to get offered PCI devices as they become
1512 * available (and thus we can be the first to grab them)
1514 err
= pci_register_driver(&xen_pcibk_pci_driver
);
1518 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1519 &driver_attr_new_slot
);
1521 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1522 &driver_attr_remove_slot
);
1524 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1525 &driver_attr_slots
);
1527 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1528 &driver_attr_quirks
);
1530 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1531 &driver_attr_permissive
);
1534 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1535 &driver_attr_irq_handlers
);
1537 err
= driver_create_file(&xen_pcibk_pci_driver
.driver
,
1538 &driver_attr_irq_handler_state
);
1546 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1547 pci_devs_to_hide
+ pos
);
1553 * fs_initcall happens before device_initcall
1554 * so xen_pcibk *should* get called first (b/c we
1555 * want to suck up any device before other drivers
1556 * get a chance by being the first pci device
1557 * driver to register)
1559 fs_initcall(pcistub_init
);
1562 #ifdef CONFIG_PCI_IOV
1563 static struct pcistub_device
*find_vfs(const struct pci_dev
*pdev
)
1565 struct pcistub_device
*psdev
= NULL
;
1566 unsigned long flags
;
1569 spin_lock_irqsave(&pcistub_devices_lock
, flags
);
1570 list_for_each_entry(psdev
, &pcistub_devices
, dev_list
) {
1571 if (!psdev
->pdev
&& psdev
->dev
!= pdev
1572 && pci_physfn(psdev
->dev
) == pdev
) {
1577 spin_unlock_irqrestore(&pcistub_devices_lock
, flags
);
1583 static int pci_stub_notifier(struct notifier_block
*nb
,
1584 unsigned long action
, void *data
)
1586 struct device
*dev
= data
;
1587 const struct pci_dev
*pdev
= to_pci_dev(dev
);
1589 if (action
!= BUS_NOTIFY_UNBIND_DRIVER
)
1592 if (!pdev
->is_physfn
)
1596 struct pcistub_device
*psdev
= find_vfs(pdev
);
1599 device_release_driver(&psdev
->dev
->dev
);
1604 static struct notifier_block pci_stub_nb
= {
1605 .notifier_call
= pci_stub_notifier
,
1609 static int __init
xen_pcibk_init(void)
1613 if (!xen_initial_domain())
1616 err
= xen_pcibk_config_init();
1621 err
= pcistub_init();
1626 pcistub_init_devices_late();
1627 err
= xen_pcibk_xenbus_register();
1630 #ifdef CONFIG_PCI_IOV
1632 bus_register_notifier(&pci_bus_type
, &pci_stub_nb
);
1638 static void __exit
xen_pcibk_cleanup(void)
1640 #ifdef CONFIG_PCI_IOV
1641 bus_unregister_notifier(&pci_bus_type
, &pci_stub_nb
);
1643 xen_pcibk_xenbus_unregister();
1647 module_init(xen_pcibk_init
);
1648 module_exit(xen_pcibk_cleanup
);
1650 MODULE_LICENSE("Dual BSD/GPL");
1651 MODULE_ALIAS("xen-backend:pci");