gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / xen / xen-pciback / pci_stub.c
blob7af93d65ed51ffbae0c8e45c0fe8e5d04c3883ce
1 /*
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>
6 */
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>
24 #include "pciback.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;
40 int domain;
41 unsigned char bus;
42 unsigned int devfn;
44 static LIST_HEAD(pcistub_device_ids);
45 static DEFINE_SPINLOCK(device_ids_lock);
47 struct pcistub_device {
48 struct kref kref;
49 struct list_head dev_list;
50 spinlock_t lock;
52 struct pci_dev *dev;
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_KERNEL);
75 if (!psdev)
76 return NULL;
78 psdev->dev = pci_dev_get(dev);
79 if (!psdev->dev) {
80 kfree(psdev);
81 return NULL;
84 kref_init(&psdev->kref);
85 spin_lock_init(&psdev->lock);
87 return psdev;
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;
94 struct pci_dev *dev;
95 struct xen_pcibk_dev_data *dev_data;
97 psdev = container_of(kref, struct pcistub_device, kref);
98 dev = psdev->dev;
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 (dev_data &&
110 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
111 dev_info(&dev->dev, "Could not reload PCI state\n");
112 else
113 pci_restore_state(dev);
115 if (dev->msix_cap) {
116 struct physdev_pci_device ppdev = {
117 .seg = pci_domain_nr(dev->bus),
118 .bus = dev->bus->number,
119 .devfn = dev->devfn
121 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
122 &ppdev);
124 if (err && err != -ENOSYS)
125 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
126 err);
129 /* Disable the device */
130 xen_pcibk_reset_device(dev);
132 kfree(dev_data);
133 pci_set_drvdata(dev, NULL);
135 /* Clean-up the device */
136 xen_pcibk_config_free_dyn_fields(dev);
137 xen_pcibk_config_free_dev(dev);
139 pci_clear_dev_assigned(dev);
140 pci_dev_put(dev);
142 kfree(psdev);
145 static inline void pcistub_device_get(struct pcistub_device *psdev)
147 kref_get(&psdev->kref);
150 static inline void pcistub_device_put(struct pcistub_device *psdev)
152 kref_put(&psdev->kref, pcistub_device_release);
155 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
156 int slot, int func)
158 struct pcistub_device *psdev;
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
166 return psdev;
170 return NULL;
173 static struct pcistub_device *pcistub_device_find(int domain, int bus,
174 int slot, int func)
176 struct pcistub_device *psdev;
177 unsigned long flags;
179 spin_lock_irqsave(&pcistub_devices_lock, flags);
181 psdev = pcistub_device_find_locked(domain, bus, slot, func);
182 if (psdev)
183 pcistub_device_get(psdev);
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 return psdev;
189 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
190 struct pcistub_device *psdev)
192 struct pci_dev *pci_dev = NULL;
193 unsigned long flags;
195 pcistub_device_get(psdev);
197 spin_lock_irqsave(&psdev->lock, flags);
198 if (!psdev->pdev) {
199 psdev->pdev = pdev;
200 pci_dev = psdev->dev;
202 spin_unlock_irqrestore(&psdev->lock, flags);
204 if (!pci_dev)
205 pcistub_device_put(psdev);
207 return pci_dev;
210 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
211 int domain, int bus,
212 int slot, int func)
214 struct pcistub_device *psdev;
215 struct pci_dev *found_dev = NULL;
216 unsigned long flags;
218 spin_lock_irqsave(&pcistub_devices_lock, flags);
220 psdev = pcistub_device_find_locked(domain, bus, slot, func);
221 if (psdev)
222 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 return found_dev;
228 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
229 struct pci_dev *dev)
231 struct pcistub_device *psdev;
232 struct pci_dev *found_dev = NULL;
233 unsigned long flags;
235 spin_lock_irqsave(&pcistub_devices_lock, flags);
237 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 if (psdev->dev == dev) {
239 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
240 break;
244 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
245 return found_dev;
249 * Called when:
250 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
255 * As such we have to be careful.
257 * To make this easier, the caller has to hold the device lock.
259 void pcistub_put_pci_dev(struct pci_dev *dev)
261 struct pcistub_device *psdev, *found_psdev = NULL;
262 unsigned long flags;
263 struct xen_pcibk_dev_data *dev_data;
264 int ret;
266 spin_lock_irqsave(&pcistub_devices_lock, flags);
268 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 if (psdev->dev == dev) {
270 found_psdev = psdev;
271 break;
275 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
276 if (WARN_ON(!found_psdev))
277 return;
279 /*hold this lock for avoiding breaking link between
280 * pcistub and xen_pcibk when AER is in processing
282 down_write(&pcistub_sem);
283 /* Cleanup our device
284 * (so it's ready for the next domain)
286 device_lock_assert(&dev->dev);
287 __pci_reset_function_locked(dev);
289 dev_data = pci_get_drvdata(dev);
290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 if (!ret) {
293 * The usual sequence is pci_save_state & pci_restore_state
294 * but the guest might have messed the configuration space up.
295 * Use the initial version (when device was bound to us).
297 pci_restore_state(dev);
298 } else
299 dev_info(&dev->dev, "Could not reload PCI state\n");
300 /* This disables the device. */
301 xen_pcibk_reset_device(dev);
303 /* And cleanup up our emulated fields. */
304 xen_pcibk_config_reset_dev(dev);
305 xen_pcibk_config_free_dyn_fields(dev);
307 dev_data->allow_interrupt_control = 0;
309 xen_unregister_device_domain_owner(dev);
311 spin_lock_irqsave(&found_psdev->lock, flags);
312 found_psdev->pdev = NULL;
313 spin_unlock_irqrestore(&found_psdev->lock, flags);
315 pcistub_device_put(found_psdev);
316 up_write(&pcistub_sem);
319 static int pcistub_match_one(struct pci_dev *dev,
320 struct pcistub_device_id *pdev_id)
322 /* Match the specified device by domain, bus, slot, func and also if
323 * any of the device's parent bridges match.
325 for (; dev != NULL; dev = dev->bus->self) {
326 if (pci_domain_nr(dev->bus) == pdev_id->domain
327 && dev->bus->number == pdev_id->bus
328 && dev->devfn == pdev_id->devfn)
329 return 1;
331 /* Sometimes topmost bridge links to itself. */
332 if (dev == dev->bus->self)
333 break;
336 return 0;
339 static int pcistub_match(struct pci_dev *dev)
341 struct pcistub_device_id *pdev_id;
342 unsigned long flags;
343 int found = 0;
345 spin_lock_irqsave(&device_ids_lock, flags);
346 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
347 if (pcistub_match_one(dev, pdev_id)) {
348 found = 1;
349 break;
352 spin_unlock_irqrestore(&device_ids_lock, flags);
354 return found;
357 static int pcistub_init_device(struct pci_dev *dev)
359 struct xen_pcibk_dev_data *dev_data;
360 int err = 0;
362 dev_dbg(&dev->dev, "initializing...\n");
364 /* The PCI backend is not intended to be a module (or to work with
365 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
366 * would need to be called somewhere to free the memory allocated
367 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
369 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
370 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
371 if (!dev_data) {
372 err = -ENOMEM;
373 goto out;
375 pci_set_drvdata(dev, dev_data);
378 * Setup name for fake IRQ handler. It will only be enabled
379 * once the device is turned on by the guest.
381 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
383 dev_dbg(&dev->dev, "initializing config\n");
385 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
386 err = xen_pcibk_config_init_dev(dev);
387 if (err)
388 goto out;
390 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
391 * must do this here because pcibios_enable_device may specify
392 * the pci device's true irq (and possibly its other resources)
393 * if they differ from what's in the configuration space.
394 * This makes the assumption that the device's resources won't
395 * change after this point (otherwise this code may break!)
397 dev_dbg(&dev->dev, "enabling device\n");
398 err = pci_enable_device(dev);
399 if (err)
400 goto config_release;
402 if (dev->msix_cap) {
403 struct physdev_pci_device ppdev = {
404 .seg = pci_domain_nr(dev->bus),
405 .bus = dev->bus->number,
406 .devfn = dev->devfn
409 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
410 if (err && err != -ENOSYS)
411 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
412 err);
415 /* We need the device active to save the state. */
416 dev_dbg(&dev->dev, "save state of device\n");
417 pci_save_state(dev);
418 dev_data->pci_saved_state = pci_store_saved_state(dev);
419 if (!dev_data->pci_saved_state)
420 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
421 else {
422 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
423 __pci_reset_function_locked(dev);
424 pci_restore_state(dev);
426 /* Now disable the device (this also ensures some private device
427 * data is setup before we export)
429 dev_dbg(&dev->dev, "reset device\n");
430 xen_pcibk_reset_device(dev);
432 pci_set_dev_assigned(dev);
433 return 0;
435 config_release:
436 xen_pcibk_config_free_dev(dev);
438 out:
439 pci_set_drvdata(dev, NULL);
440 kfree(dev_data);
441 return err;
445 * Because some initialization still happens on
446 * devices during fs_initcall, we need to defer
447 * full initialization of our devices until
448 * device_initcall.
450 static int __init pcistub_init_devices_late(void)
452 struct pcistub_device *psdev;
453 unsigned long flags;
454 int err = 0;
456 spin_lock_irqsave(&pcistub_devices_lock, flags);
458 while (!list_empty(&seized_devices)) {
459 psdev = container_of(seized_devices.next,
460 struct pcistub_device, dev_list);
461 list_del(&psdev->dev_list);
463 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
465 err = pcistub_init_device(psdev->dev);
466 if (err) {
467 dev_err(&psdev->dev->dev,
468 "error %d initializing device\n", err);
469 kfree(psdev);
470 psdev = NULL;
473 spin_lock_irqsave(&pcistub_devices_lock, flags);
475 if (psdev)
476 list_add_tail(&psdev->dev_list, &pcistub_devices);
479 initialize_devices = 1;
481 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
483 return 0;
486 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
487 int domain, int bus, unsigned int devfn)
489 struct pcistub_device_id *pci_dev_id;
490 unsigned long flags;
491 int found = 0;
493 spin_lock_irqsave(&device_ids_lock, flags);
495 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
496 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
497 pci_dev_id->devfn == devfn) {
498 found = 1;
499 break;
503 if (!found) {
504 new->domain = domain;
505 new->bus = bus;
506 new->devfn = devfn;
507 list_add_tail(&new->slot_list, &pcistub_device_ids);
510 spin_unlock_irqrestore(&device_ids_lock, flags);
512 if (found)
513 kfree(new);
516 static int pcistub_seize(struct pci_dev *dev,
517 struct pcistub_device_id *pci_dev_id)
519 struct pcistub_device *psdev;
520 unsigned long flags;
521 int err = 0;
523 psdev = pcistub_device_alloc(dev);
524 if (!psdev) {
525 kfree(pci_dev_id);
526 return -ENOMEM;
529 spin_lock_irqsave(&pcistub_devices_lock, flags);
531 if (initialize_devices) {
532 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
534 /* don't want irqs disabled when calling pcistub_init_device */
535 err = pcistub_init_device(psdev->dev);
537 spin_lock_irqsave(&pcistub_devices_lock, flags);
539 if (!err)
540 list_add(&psdev->dev_list, &pcistub_devices);
541 } else {
542 dev_dbg(&dev->dev, "deferring initialization\n");
543 list_add(&psdev->dev_list, &seized_devices);
546 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
548 if (err) {
549 kfree(pci_dev_id);
550 pcistub_device_put(psdev);
551 } else if (pci_dev_id)
552 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
553 dev->bus->number, dev->devfn);
555 return err;
558 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
559 * other functions that take the sysfs lock. */
560 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
562 int err = 0, match;
563 struct pcistub_device_id *pci_dev_id = NULL;
565 dev_dbg(&dev->dev, "probing...\n");
567 match = pcistub_match(dev);
569 if ((dev->driver_override &&
570 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
571 match) {
573 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
574 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
575 dev_err(&dev->dev, "can't export pci devices that "
576 "don't have a normal (0) or bridge (1) "
577 "header type!\n");
578 err = -ENODEV;
579 goto out;
582 if (!match) {
583 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
584 if (!pci_dev_id) {
585 err = -ENOMEM;
586 goto out;
590 dev_info(&dev->dev, "seizing device\n");
591 err = pcistub_seize(dev, pci_dev_id);
592 } else
593 /* Didn't find the device */
594 err = -ENODEV;
596 out:
597 return err;
600 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
601 * other functions that take the sysfs lock. */
602 static void pcistub_remove(struct pci_dev *dev)
604 struct pcistub_device *psdev, *found_psdev = NULL;
605 unsigned long flags;
607 dev_dbg(&dev->dev, "removing\n");
609 spin_lock_irqsave(&pcistub_devices_lock, flags);
611 xen_pcibk_config_quirk_release(dev);
613 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
614 if (psdev->dev == dev) {
615 found_psdev = psdev;
616 break;
620 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
622 if (found_psdev) {
623 dev_dbg(&dev->dev, "found device to remove %s\n",
624 found_psdev->pdev ? "- in-use" : "");
626 if (found_psdev->pdev) {
627 int domid = xen_find_device_domain_owner(dev);
629 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
630 pci_name(found_psdev->dev), domid);
631 pr_warn("****** driver domain may still access this device's i/o resources!\n");
632 pr_warn("****** shutdown driver domain before binding device\n");
633 pr_warn("****** to other drivers or domains\n");
635 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
636 * doing the FLR. */
637 xen_pcibk_release_pci_dev(found_psdev->pdev,
638 found_psdev->dev,
639 false /* caller holds the lock. */);
642 spin_lock_irqsave(&pcistub_devices_lock, flags);
643 list_del(&found_psdev->dev_list);
644 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
646 /* the final put for releasing from the list */
647 pcistub_device_put(found_psdev);
651 static const struct pci_device_id pcistub_ids[] = {
653 .vendor = PCI_ANY_ID,
654 .device = PCI_ANY_ID,
655 .subvendor = PCI_ANY_ID,
656 .subdevice = PCI_ANY_ID,
658 {0,},
661 #define PCI_NODENAME_MAX 40
662 static void kill_domain_by_device(struct pcistub_device *psdev)
664 struct xenbus_transaction xbt;
665 int err;
666 char nodename[PCI_NODENAME_MAX];
668 BUG_ON(!psdev);
669 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
670 psdev->pdev->xdev->otherend_id);
672 again:
673 err = xenbus_transaction_start(&xbt);
674 if (err) {
675 dev_err(&psdev->dev->dev,
676 "error %d when start xenbus transaction\n", err);
677 return;
679 /*PV AER handlers will set this flag*/
680 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
681 err = xenbus_transaction_end(xbt, 0);
682 if (err) {
683 if (err == -EAGAIN)
684 goto again;
685 dev_err(&psdev->dev->dev,
686 "error %d when end xenbus transaction\n", err);
687 return;
691 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
692 * backend need to have cooperation. In xen_pcibk, those steps will do similar
693 * jobs: send service request and waiting for front_end response.
695 static pci_ers_result_t common_process(struct pcistub_device *psdev,
696 pci_channel_state_t state, int aer_cmd,
697 pci_ers_result_t result)
699 pci_ers_result_t res = result;
700 struct xen_pcie_aer_op *aer_op;
701 struct xen_pcibk_device *pdev = psdev->pdev;
702 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
703 int ret;
705 /*with PV AER drivers*/
706 aer_op = &(sh_info->aer_op);
707 aer_op->cmd = aer_cmd ;
708 /*useful for error_detected callback*/
709 aer_op->err = state;
710 /*pcifront_end BDF*/
711 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
712 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
713 if (!ret) {
714 dev_err(&psdev->dev->dev,
715 DRV_NAME ": failed to get pcifront device\n");
716 return PCI_ERS_RESULT_NONE;
718 wmb();
720 dev_dbg(&psdev->dev->dev,
721 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
722 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
723 /*local flag to mark there's aer request, xen_pcibk callback will use
724 * this flag to judge whether we need to check pci-front give aer
725 * service ack signal
727 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
729 /*It is possible that a pcifront conf_read_write ops request invokes
730 * the callback which cause the spurious execution of wake_up.
731 * Yet it is harmless and better than a spinlock here
733 set_bit(_XEN_PCIB_active,
734 (unsigned long *)&sh_info->flags);
735 wmb();
736 notify_remote_via_irq(pdev->evtchn_irq);
738 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
739 !(test_bit(_XEN_PCIB_active, (unsigned long *)
740 &sh_info->flags)), 300*HZ);
742 if (!ret) {
743 if (test_bit(_XEN_PCIB_active,
744 (unsigned long *)&sh_info->flags)) {
745 dev_err(&psdev->dev->dev,
746 "pcifront aer process not responding!\n");
747 clear_bit(_XEN_PCIB_active,
748 (unsigned long *)&sh_info->flags);
749 aer_op->err = PCI_ERS_RESULT_NONE;
750 return res;
753 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
755 if (test_bit(_XEN_PCIF_active,
756 (unsigned long *)&sh_info->flags)) {
757 dev_dbg(&psdev->dev->dev,
758 "schedule pci_conf service in " DRV_NAME "\n");
759 xen_pcibk_test_and_schedule_op(psdev->pdev);
762 res = (pci_ers_result_t)aer_op->err;
763 return res;
767 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
768 * of the device driver could provide this service, and then wait for pcifront
769 * ack.
770 * @dev: pointer to PCI devices
771 * return value is used by aer_core do_recovery policy
773 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
775 struct pcistub_device *psdev;
776 pci_ers_result_t result;
778 result = PCI_ERS_RESULT_RECOVERED;
779 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
780 dev->bus->number, dev->devfn);
782 down_write(&pcistub_sem);
783 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
784 dev->bus->number,
785 PCI_SLOT(dev->devfn),
786 PCI_FUNC(dev->devfn));
788 if (!psdev || !psdev->pdev) {
789 dev_err(&dev->dev,
790 DRV_NAME " device is not found/assigned\n");
791 goto end;
794 if (!psdev->pdev->sh_info) {
795 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
796 " by HVM, kill it\n");
797 kill_domain_by_device(psdev);
798 goto end;
801 if (!test_bit(_XEN_PCIB_AERHANDLER,
802 (unsigned long *)&psdev->pdev->sh_info->flags)) {
803 dev_err(&dev->dev,
804 "guest with no AER driver should have been killed\n");
805 goto end;
807 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
809 if (result == PCI_ERS_RESULT_NONE ||
810 result == PCI_ERS_RESULT_DISCONNECT) {
811 dev_dbg(&dev->dev,
812 "No AER slot_reset service or disconnected!\n");
813 kill_domain_by_device(psdev);
815 end:
816 if (psdev)
817 pcistub_device_put(psdev);
818 up_write(&pcistub_sem);
819 return result;
824 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
825 * in case of the device driver could provide this service, and then wait
826 * for pcifront ack
827 * @dev: pointer to PCI devices
828 * return value is used by aer_core do_recovery policy
831 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
833 struct pcistub_device *psdev;
834 pci_ers_result_t result;
836 result = PCI_ERS_RESULT_RECOVERED;
837 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
838 dev->bus->number, dev->devfn);
840 down_write(&pcistub_sem);
841 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
842 dev->bus->number,
843 PCI_SLOT(dev->devfn),
844 PCI_FUNC(dev->devfn));
846 if (!psdev || !psdev->pdev) {
847 dev_err(&dev->dev,
848 DRV_NAME " device is not found/assigned\n");
849 goto end;
852 if (!psdev->pdev->sh_info) {
853 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
854 " by HVM, kill it\n");
855 kill_domain_by_device(psdev);
856 goto end;
859 if (!test_bit(_XEN_PCIB_AERHANDLER,
860 (unsigned long *)&psdev->pdev->sh_info->flags)) {
861 dev_err(&dev->dev,
862 "guest with no AER driver should have been killed\n");
863 goto end;
865 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
867 if (result == PCI_ERS_RESULT_NONE ||
868 result == PCI_ERS_RESULT_DISCONNECT) {
869 dev_dbg(&dev->dev,
870 "No AER mmio_enabled service or disconnected!\n");
871 kill_domain_by_device(psdev);
873 end:
874 if (psdev)
875 pcistub_device_put(psdev);
876 up_write(&pcistub_sem);
877 return result;
880 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
881 * in case of the device driver could provide this service, and then wait
882 * for pcifront ack.
883 * @dev: pointer to PCI devices
884 * @error: the current PCI connection state
885 * return value is used by aer_core do_recovery policy
888 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
889 pci_channel_state_t error)
891 struct pcistub_device *psdev;
892 pci_ers_result_t result;
894 result = PCI_ERS_RESULT_CAN_RECOVER;
895 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
896 dev->bus->number, dev->devfn);
898 down_write(&pcistub_sem);
899 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
900 dev->bus->number,
901 PCI_SLOT(dev->devfn),
902 PCI_FUNC(dev->devfn));
904 if (!psdev || !psdev->pdev) {
905 dev_err(&dev->dev,
906 DRV_NAME " device is not found/assigned\n");
907 goto end;
910 if (!psdev->pdev->sh_info) {
911 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
912 " by HVM, kill it\n");
913 kill_domain_by_device(psdev);
914 goto end;
917 /*Guest owns the device yet no aer handler regiested, kill guest*/
918 if (!test_bit(_XEN_PCIB_AERHANDLER,
919 (unsigned long *)&psdev->pdev->sh_info->flags)) {
920 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
921 kill_domain_by_device(psdev);
922 goto end;
924 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
926 if (result == PCI_ERS_RESULT_NONE ||
927 result == PCI_ERS_RESULT_DISCONNECT) {
928 dev_dbg(&dev->dev,
929 "No AER error_detected service or disconnected!\n");
930 kill_domain_by_device(psdev);
932 end:
933 if (psdev)
934 pcistub_device_put(psdev);
935 up_write(&pcistub_sem);
936 return result;
939 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
940 * in case of the device driver could provide this service, and then wait
941 * for pcifront ack.
942 * @dev: pointer to PCI devices
945 static void xen_pcibk_error_resume(struct pci_dev *dev)
947 struct pcistub_device *psdev;
949 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
950 dev->bus->number, dev->devfn);
952 down_write(&pcistub_sem);
953 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
954 dev->bus->number,
955 PCI_SLOT(dev->devfn),
956 PCI_FUNC(dev->devfn));
958 if (!psdev || !psdev->pdev) {
959 dev_err(&dev->dev,
960 DRV_NAME " device is not found/assigned\n");
961 goto end;
964 if (!psdev->pdev->sh_info) {
965 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
966 " by HVM, kill it\n");
967 kill_domain_by_device(psdev);
968 goto end;
971 if (!test_bit(_XEN_PCIB_AERHANDLER,
972 (unsigned long *)&psdev->pdev->sh_info->flags)) {
973 dev_err(&dev->dev,
974 "guest with no AER driver should have been killed\n");
975 kill_domain_by_device(psdev);
976 goto end;
978 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
979 PCI_ERS_RESULT_RECOVERED);
980 end:
981 if (psdev)
982 pcistub_device_put(psdev);
983 up_write(&pcistub_sem);
984 return;
987 /*add xen_pcibk AER handling*/
988 static const struct pci_error_handlers xen_pcibk_error_handler = {
989 .error_detected = xen_pcibk_error_detected,
990 .mmio_enabled = xen_pcibk_mmio_enabled,
991 .slot_reset = xen_pcibk_slot_reset,
992 .resume = xen_pcibk_error_resume,
996 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
997 * for a normal device. I don't want it to be loaded automatically.
1000 static struct pci_driver xen_pcibk_pci_driver = {
1001 /* The name should be xen_pciback, but until the tools are updated
1002 * we will keep it as pciback. */
1003 .name = PCISTUB_DRIVER_NAME,
1004 .id_table = pcistub_ids,
1005 .probe = pcistub_probe,
1006 .remove = pcistub_remove,
1007 .err_handler = &xen_pcibk_error_handler,
1010 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1011 int *slot, int *func)
1013 int parsed = 0;
1015 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1016 &parsed)) {
1017 case 3:
1018 *func = -1;
1019 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1020 break;
1021 case 2:
1022 *slot = *func = -1;
1023 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1024 break;
1026 if (parsed && !buf[parsed])
1027 return 0;
1029 /* try again without domain */
1030 *domain = 0;
1031 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1032 case 2:
1033 *func = -1;
1034 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1035 break;
1036 case 1:
1037 *slot = *func = -1;
1038 sscanf(buf, " %x:*.* %n", bus, &parsed);
1039 break;
1041 if (parsed && !buf[parsed])
1042 return 0;
1044 return -EINVAL;
1047 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1048 *slot, int *func, int *reg, int *size, int *mask)
1050 int parsed = 0;
1052 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1053 reg, size, mask, &parsed);
1054 if (parsed && !buf[parsed])
1055 return 0;
1057 /* try again without domain */
1058 *domain = 0;
1059 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1060 mask, &parsed);
1061 if (parsed && !buf[parsed])
1062 return 0;
1064 return -EINVAL;
1067 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1069 struct pcistub_device_id *pci_dev_id;
1070 int rc = 0, devfn = PCI_DEVFN(slot, func);
1072 if (slot < 0) {
1073 for (slot = 0; !rc && slot < 32; ++slot)
1074 rc = pcistub_device_id_add(domain, bus, slot, func);
1075 return rc;
1078 if (func < 0) {
1079 for (func = 0; !rc && func < 8; ++func)
1080 rc = pcistub_device_id_add(domain, bus, slot, func);
1081 return rc;
1084 if ((
1085 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1086 || !defined(CONFIG_PCI_DOMAINS)
1087 !pci_domains_supported ? domain :
1088 #endif
1089 domain < 0 || domain > 0xffff)
1090 || bus < 0 || bus > 0xff
1091 || PCI_SLOT(devfn) != slot
1092 || PCI_FUNC(devfn) != func)
1093 return -EINVAL;
1095 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1096 if (!pci_dev_id)
1097 return -ENOMEM;
1099 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1100 domain, bus, slot, func);
1102 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1104 return 0;
1107 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1109 struct pcistub_device_id *pci_dev_id, *t;
1110 int err = -ENOENT;
1111 unsigned long flags;
1113 spin_lock_irqsave(&device_ids_lock, flags);
1114 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1115 slot_list) {
1116 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1117 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1118 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1119 /* Don't break; here because it's possible the same
1120 * slot could be in the list more than once
1122 list_del(&pci_dev_id->slot_list);
1123 kfree(pci_dev_id);
1125 err = 0;
1127 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1128 domain, bus, slot, func);
1131 spin_unlock_irqrestore(&device_ids_lock, flags);
1133 return err;
1136 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1137 unsigned int reg, unsigned int size,
1138 unsigned int mask)
1140 int err = 0;
1141 struct pcistub_device *psdev;
1142 struct pci_dev *dev;
1143 struct config_field *field;
1145 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1146 return -EINVAL;
1148 psdev = pcistub_device_find(domain, bus, slot, func);
1149 if (!psdev) {
1150 err = -ENODEV;
1151 goto out;
1153 dev = psdev->dev;
1155 field = kzalloc(sizeof(*field), GFP_KERNEL);
1156 if (!field) {
1157 err = -ENOMEM;
1158 goto out;
1161 field->offset = reg;
1162 field->size = size;
1163 field->mask = mask;
1164 field->init = NULL;
1165 field->reset = NULL;
1166 field->release = NULL;
1167 field->clean = xen_pcibk_config_field_free;
1169 err = xen_pcibk_config_quirks_add_field(dev, field);
1170 if (err)
1171 kfree(field);
1172 out:
1173 if (psdev)
1174 pcistub_device_put(psdev);
1175 return err;
1178 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1179 size_t count)
1181 int domain, bus, slot, func;
1182 int err;
1184 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1185 if (err)
1186 goto out;
1188 err = pcistub_device_id_add(domain, bus, slot, func);
1190 out:
1191 if (!err)
1192 err = count;
1193 return err;
1195 static DRIVER_ATTR_WO(new_slot);
1197 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1198 size_t count)
1200 int domain, bus, slot, func;
1201 int err;
1203 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1204 if (err)
1205 goto out;
1207 err = pcistub_device_id_remove(domain, bus, slot, func);
1209 out:
1210 if (!err)
1211 err = count;
1212 return err;
1214 static DRIVER_ATTR_WO(remove_slot);
1216 static ssize_t slots_show(struct device_driver *drv, char *buf)
1218 struct pcistub_device_id *pci_dev_id;
1219 size_t count = 0;
1220 unsigned long flags;
1222 spin_lock_irqsave(&device_ids_lock, flags);
1223 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1224 if (count >= PAGE_SIZE)
1225 break;
1227 count += scnprintf(buf + count, PAGE_SIZE - count,
1228 "%04x:%02x:%02x.%d\n",
1229 pci_dev_id->domain, pci_dev_id->bus,
1230 PCI_SLOT(pci_dev_id->devfn),
1231 PCI_FUNC(pci_dev_id->devfn));
1233 spin_unlock_irqrestore(&device_ids_lock, flags);
1235 return count;
1237 static DRIVER_ATTR_RO(slots);
1239 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1241 struct pcistub_device *psdev;
1242 struct xen_pcibk_dev_data *dev_data;
1243 size_t count = 0;
1244 unsigned long flags;
1246 spin_lock_irqsave(&pcistub_devices_lock, flags);
1247 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1248 if (count >= PAGE_SIZE)
1249 break;
1250 if (!psdev->dev)
1251 continue;
1252 dev_data = pci_get_drvdata(psdev->dev);
1253 if (!dev_data)
1254 continue;
1255 count +=
1256 scnprintf(buf + count, PAGE_SIZE - count,
1257 "%s:%s:%sing:%ld\n",
1258 pci_name(psdev->dev),
1259 dev_data->isr_on ? "on" : "off",
1260 dev_data->ack_intr ? "ack" : "not ack",
1261 dev_data->handled);
1263 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1264 return count;
1266 static DRIVER_ATTR_RO(irq_handlers);
1268 static ssize_t irq_handler_state_store(struct device_driver *drv,
1269 const char *buf, size_t count)
1271 struct pcistub_device *psdev;
1272 struct xen_pcibk_dev_data *dev_data;
1273 int domain, bus, slot, func;
1274 int err;
1276 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1277 if (err)
1278 return err;
1280 psdev = pcistub_device_find(domain, bus, slot, func);
1281 if (!psdev) {
1282 err = -ENOENT;
1283 goto out;
1286 dev_data = pci_get_drvdata(psdev->dev);
1287 if (!dev_data) {
1288 err = -ENOENT;
1289 goto out;
1292 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1293 dev_data->irq_name, dev_data->isr_on,
1294 !dev_data->isr_on);
1296 dev_data->isr_on = !(dev_data->isr_on);
1297 if (dev_data->isr_on)
1298 dev_data->ack_intr = 1;
1299 out:
1300 if (psdev)
1301 pcistub_device_put(psdev);
1302 if (!err)
1303 err = count;
1304 return err;
1306 static DRIVER_ATTR_WO(irq_handler_state);
1308 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1309 size_t count)
1311 int domain, bus, slot, func, reg, size, mask;
1312 int err;
1314 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1315 &mask);
1316 if (err)
1317 goto out;
1319 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1321 out:
1322 if (!err)
1323 err = count;
1324 return err;
1327 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1329 int count = 0;
1330 unsigned long flags;
1331 struct xen_pcibk_config_quirk *quirk;
1332 struct xen_pcibk_dev_data *dev_data;
1333 const struct config_field *field;
1334 const struct config_field_entry *cfg_entry;
1336 spin_lock_irqsave(&device_ids_lock, flags);
1337 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1338 if (count >= PAGE_SIZE)
1339 goto out;
1341 count += scnprintf(buf + count, PAGE_SIZE - count,
1342 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1343 quirk->pdev->bus->number,
1344 PCI_SLOT(quirk->pdev->devfn),
1345 PCI_FUNC(quirk->pdev->devfn),
1346 quirk->devid.vendor, quirk->devid.device,
1347 quirk->devid.subvendor,
1348 quirk->devid.subdevice);
1350 dev_data = pci_get_drvdata(quirk->pdev);
1352 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1353 field = cfg_entry->field;
1354 if (count >= PAGE_SIZE)
1355 goto out;
1357 count += scnprintf(buf + count, PAGE_SIZE - count,
1358 "\t\t%08x:%01x:%08x\n",
1359 cfg_entry->base_offset +
1360 field->offset, field->size,
1361 field->mask);
1365 out:
1366 spin_unlock_irqrestore(&device_ids_lock, flags);
1368 return count;
1370 static DRIVER_ATTR_RW(quirks);
1372 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1373 size_t count)
1375 int domain, bus, slot, func;
1376 int err;
1377 struct pcistub_device *psdev;
1378 struct xen_pcibk_dev_data *dev_data;
1380 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1381 if (err)
1382 goto out;
1384 psdev = pcistub_device_find(domain, bus, slot, func);
1385 if (!psdev) {
1386 err = -ENODEV;
1387 goto out;
1390 dev_data = pci_get_drvdata(psdev->dev);
1391 /* the driver data for a device should never be null at this point */
1392 if (!dev_data) {
1393 err = -ENXIO;
1394 goto release;
1396 if (!dev_data->permissive) {
1397 dev_data->permissive = 1;
1398 /* Let user know that what they're doing could be unsafe */
1399 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1400 "configuration space accesses!\n");
1401 dev_warn(&psdev->dev->dev,
1402 "permissive mode is potentially unsafe!\n");
1404 release:
1405 pcistub_device_put(psdev);
1406 out:
1407 if (!err)
1408 err = count;
1409 return err;
1412 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1414 struct pcistub_device *psdev;
1415 struct xen_pcibk_dev_data *dev_data;
1416 size_t count = 0;
1417 unsigned long flags;
1418 spin_lock_irqsave(&pcistub_devices_lock, flags);
1419 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1420 if (count >= PAGE_SIZE)
1421 break;
1422 if (!psdev->dev)
1423 continue;
1424 dev_data = pci_get_drvdata(psdev->dev);
1425 if (!dev_data || !dev_data->permissive)
1426 continue;
1427 count +=
1428 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1429 pci_name(psdev->dev));
1431 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1432 return count;
1434 static DRIVER_ATTR_RW(permissive);
1436 static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1437 const char *buf, size_t count)
1439 int domain, bus, slot, func;
1440 int err;
1441 struct pcistub_device *psdev;
1442 struct xen_pcibk_dev_data *dev_data;
1444 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1445 if (err)
1446 goto out;
1448 psdev = pcistub_device_find(domain, bus, slot, func);
1449 if (!psdev) {
1450 err = -ENODEV;
1451 goto out;
1454 dev_data = pci_get_drvdata(psdev->dev);
1455 /* the driver data for a device should never be null at this point */
1456 if (!dev_data) {
1457 err = -ENXIO;
1458 goto release;
1460 dev_data->allow_interrupt_control = 1;
1461 release:
1462 pcistub_device_put(psdev);
1463 out:
1464 if (!err)
1465 err = count;
1466 return err;
1469 static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1470 char *buf)
1472 struct pcistub_device *psdev;
1473 struct xen_pcibk_dev_data *dev_data;
1474 size_t count = 0;
1475 unsigned long flags;
1477 spin_lock_irqsave(&pcistub_devices_lock, flags);
1478 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1479 if (count >= PAGE_SIZE)
1480 break;
1481 if (!psdev->dev)
1482 continue;
1483 dev_data = pci_get_drvdata(psdev->dev);
1484 if (!dev_data || !dev_data->allow_interrupt_control)
1485 continue;
1486 count +=
1487 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1488 pci_name(psdev->dev));
1490 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1491 return count;
1493 static DRIVER_ATTR_RW(allow_interrupt_control);
1495 static void pcistub_exit(void)
1497 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1498 driver_remove_file(&xen_pcibk_pci_driver.driver,
1499 &driver_attr_remove_slot);
1500 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1501 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1502 driver_remove_file(&xen_pcibk_pci_driver.driver,
1503 &driver_attr_permissive);
1504 driver_remove_file(&xen_pcibk_pci_driver.driver,
1505 &driver_attr_allow_interrupt_control);
1506 driver_remove_file(&xen_pcibk_pci_driver.driver,
1507 &driver_attr_irq_handlers);
1508 driver_remove_file(&xen_pcibk_pci_driver.driver,
1509 &driver_attr_irq_handler_state);
1510 pci_unregister_driver(&xen_pcibk_pci_driver);
1513 static int __init pcistub_init(void)
1515 int pos = 0;
1516 int err = 0;
1517 int domain, bus, slot, func;
1518 int parsed;
1520 if (pci_devs_to_hide && *pci_devs_to_hide) {
1521 do {
1522 parsed = 0;
1524 err = sscanf(pci_devs_to_hide + pos,
1525 " (%x:%x:%x.%x) %n",
1526 &domain, &bus, &slot, &func, &parsed);
1527 switch (err) {
1528 case 3:
1529 func = -1;
1530 sscanf(pci_devs_to_hide + pos,
1531 " (%x:%x:%x.*) %n",
1532 &domain, &bus, &slot, &parsed);
1533 break;
1534 case 2:
1535 slot = func = -1;
1536 sscanf(pci_devs_to_hide + pos,
1537 " (%x:%x:*.*) %n",
1538 &domain, &bus, &parsed);
1539 break;
1542 if (!parsed) {
1543 domain = 0;
1544 err = sscanf(pci_devs_to_hide + pos,
1545 " (%x:%x.%x) %n",
1546 &bus, &slot, &func, &parsed);
1547 switch (err) {
1548 case 2:
1549 func = -1;
1550 sscanf(pci_devs_to_hide + pos,
1551 " (%x:%x.*) %n",
1552 &bus, &slot, &parsed);
1553 break;
1554 case 1:
1555 slot = func = -1;
1556 sscanf(pci_devs_to_hide + pos,
1557 " (%x:*.*) %n",
1558 &bus, &parsed);
1559 break;
1563 if (parsed <= 0)
1564 goto parse_error;
1566 err = pcistub_device_id_add(domain, bus, slot, func);
1567 if (err)
1568 goto out;
1570 pos += parsed;
1571 } while (pci_devs_to_hide[pos]);
1574 /* If we're the first PCI Device Driver to register, we're the
1575 * first one to get offered PCI devices as they become
1576 * available (and thus we can be the first to grab them)
1578 err = pci_register_driver(&xen_pcibk_pci_driver);
1579 if (err < 0)
1580 goto out;
1582 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1583 &driver_attr_new_slot);
1584 if (!err)
1585 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1586 &driver_attr_remove_slot);
1587 if (!err)
1588 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1589 &driver_attr_slots);
1590 if (!err)
1591 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1592 &driver_attr_quirks);
1593 if (!err)
1594 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1595 &driver_attr_permissive);
1596 if (!err)
1597 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1598 &driver_attr_allow_interrupt_control);
1600 if (!err)
1601 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1602 &driver_attr_irq_handlers);
1603 if (!err)
1604 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1605 &driver_attr_irq_handler_state);
1606 if (err)
1607 pcistub_exit();
1609 out:
1610 return err;
1612 parse_error:
1613 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1614 pci_devs_to_hide + pos);
1615 return -EINVAL;
1618 #ifndef MODULE
1620 * fs_initcall happens before device_initcall
1621 * so xen_pcibk *should* get called first (b/c we
1622 * want to suck up any device before other drivers
1623 * get a chance by being the first pci device
1624 * driver to register)
1626 fs_initcall(pcistub_init);
1627 #endif
1629 #ifdef CONFIG_PCI_IOV
1630 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1632 struct pcistub_device *psdev = NULL;
1633 unsigned long flags;
1634 bool found = false;
1636 spin_lock_irqsave(&pcistub_devices_lock, flags);
1637 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1638 if (!psdev->pdev && psdev->dev != pdev
1639 && pci_physfn(psdev->dev) == pdev) {
1640 found = true;
1641 break;
1644 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1645 if (found)
1646 return psdev;
1647 return NULL;
1650 static int pci_stub_notifier(struct notifier_block *nb,
1651 unsigned long action, void *data)
1653 struct device *dev = data;
1654 const struct pci_dev *pdev = to_pci_dev(dev);
1656 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1657 return NOTIFY_DONE;
1659 if (!pdev->is_physfn)
1660 return NOTIFY_DONE;
1662 for (;;) {
1663 struct pcistub_device *psdev = find_vfs(pdev);
1664 if (!psdev)
1665 break;
1666 device_release_driver(&psdev->dev->dev);
1668 return NOTIFY_DONE;
1671 static struct notifier_block pci_stub_nb = {
1672 .notifier_call = pci_stub_notifier,
1674 #endif
1676 static int __init xen_pcibk_init(void)
1678 int err;
1680 if (!xen_initial_domain())
1681 return -ENODEV;
1683 err = xen_pcibk_config_init();
1684 if (err)
1685 return err;
1687 #ifdef MODULE
1688 err = pcistub_init();
1689 if (err < 0)
1690 return err;
1691 #endif
1693 pcistub_init_devices_late();
1694 err = xen_pcibk_xenbus_register();
1695 if (err)
1696 pcistub_exit();
1697 #ifdef CONFIG_PCI_IOV
1698 else
1699 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1700 #endif
1702 return err;
1705 static void __exit xen_pcibk_cleanup(void)
1707 #ifdef CONFIG_PCI_IOV
1708 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1709 #endif
1710 xen_pcibk_xenbus_unregister();
1711 pcistub_exit();
1714 module_init(xen_pcibk_init);
1715 module_exit(xen_pcibk_cleanup);
1717 MODULE_LICENSE("Dual BSD/GPL");
1718 MODULE_ALIAS("xen-backend:pci");