Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / xen / xen-pciback / pci_stub.c
blob77c9f769daddad971c122d85f4730380d14e68de
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 */
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 "pciback.h"
21 #include "conf_space.h"
22 #include "conf_space_quirks.h"
24 static char *pci_devs_to_hide;
25 wait_queue_head_t xen_pcibk_aer_wait_queue;
26 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
27 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
29 static DECLARE_RWSEM(pcistub_sem);
30 module_param_named(hide, pci_devs_to_hide, charp, 0444);
32 struct pcistub_device_id {
33 struct list_head slot_list;
34 int domain;
35 unsigned char bus;
36 unsigned int devfn;
38 static LIST_HEAD(pcistub_device_ids);
39 static DEFINE_SPINLOCK(device_ids_lock);
41 struct pcistub_device {
42 struct kref kref;
43 struct list_head dev_list;
44 spinlock_t lock;
46 struct pci_dev *dev;
47 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
50 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
51 * flag must be locked with pcistub_devices_lock
53 static DEFINE_SPINLOCK(pcistub_devices_lock);
54 static LIST_HEAD(pcistub_devices);
56 /* wait for device_initcall before initializing our devices
57 * (see pcistub_init_devices_late)
59 static int initialize_devices;
60 static LIST_HEAD(seized_devices);
62 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
64 struct pcistub_device *psdev;
66 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
68 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
69 if (!psdev)
70 return NULL;
72 psdev->dev = pci_dev_get(dev);
73 if (!psdev->dev) {
74 kfree(psdev);
75 return NULL;
78 kref_init(&psdev->kref);
79 spin_lock_init(&psdev->lock);
81 return psdev;
84 /* Don't call this directly as it's called by pcistub_device_put */
85 static void pcistub_device_release(struct kref *kref)
87 struct pcistub_device *psdev;
88 struct xen_pcibk_dev_data *dev_data;
90 psdev = container_of(kref, struct pcistub_device, kref);
91 dev_data = pci_get_drvdata(psdev->dev);
93 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
95 xen_unregister_device_domain_owner(psdev->dev);
97 /* Call the reset function which does not take lock as this
98 * is called from "unbind" which takes a device_lock mutex.
100 __pci_reset_function_locked(psdev->dev);
101 if (pci_load_and_free_saved_state(psdev->dev,
102 &dev_data->pci_saved_state)) {
103 dev_dbg(&psdev->dev->dev, "Could not reload PCI state\n");
104 } else
105 pci_restore_state(psdev->dev);
107 /* Disable the device */
108 xen_pcibk_reset_device(psdev->dev);
110 kfree(dev_data);
111 pci_set_drvdata(psdev->dev, NULL);
113 /* Clean-up the device */
114 xen_pcibk_config_free_dyn_fields(psdev->dev);
115 xen_pcibk_config_free_dev(psdev->dev);
117 psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
118 pci_dev_put(psdev->dev);
120 kfree(psdev);
123 static inline void pcistub_device_get(struct pcistub_device *psdev)
125 kref_get(&psdev->kref);
128 static inline void pcistub_device_put(struct pcistub_device *psdev)
130 kref_put(&psdev->kref, pcistub_device_release);
133 static struct pcistub_device *pcistub_device_find(int domain, int bus,
134 int slot, int func)
136 struct pcistub_device *psdev = NULL;
137 unsigned long flags;
139 spin_lock_irqsave(&pcistub_devices_lock, flags);
141 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
142 if (psdev->dev != NULL
143 && domain == pci_domain_nr(psdev->dev->bus)
144 && bus == psdev->dev->bus->number
145 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
146 pcistub_device_get(psdev);
147 goto out;
151 /* didn't find it */
152 psdev = NULL;
154 out:
155 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
156 return psdev;
159 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
160 struct pcistub_device *psdev)
162 struct pci_dev *pci_dev = NULL;
163 unsigned long flags;
165 pcistub_device_get(psdev);
167 spin_lock_irqsave(&psdev->lock, flags);
168 if (!psdev->pdev) {
169 psdev->pdev = pdev;
170 pci_dev = psdev->dev;
172 spin_unlock_irqrestore(&psdev->lock, flags);
174 if (!pci_dev)
175 pcistub_device_put(psdev);
177 return pci_dev;
180 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
181 int domain, int bus,
182 int slot, int func)
184 struct pcistub_device *psdev;
185 struct pci_dev *found_dev = NULL;
186 unsigned long flags;
188 spin_lock_irqsave(&pcistub_devices_lock, flags);
190 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
191 if (psdev->dev != NULL
192 && domain == pci_domain_nr(psdev->dev->bus)
193 && bus == psdev->dev->bus->number
194 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
195 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
196 break;
200 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
201 return found_dev;
204 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
205 struct pci_dev *dev)
207 struct pcistub_device *psdev;
208 struct pci_dev *found_dev = NULL;
209 unsigned long flags;
211 spin_lock_irqsave(&pcistub_devices_lock, flags);
213 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
214 if (psdev->dev == dev) {
215 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
216 break;
220 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
221 return found_dev;
224 void pcistub_put_pci_dev(struct pci_dev *dev)
226 struct pcistub_device *psdev, *found_psdev = NULL;
227 unsigned long flags;
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_psdev = psdev;
234 break;
238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
239 if (WARN_ON(!found_psdev))
240 return;
242 /*hold this lock for avoiding breaking link between
243 * pcistub and xen_pcibk when AER is in processing
245 down_write(&pcistub_sem);
246 /* Cleanup our device
247 * (so it's ready for the next domain)
250 /* This is OK - we are running from workqueue context
251 * and want to inhibit the user from fiddling with 'reset'
253 pci_reset_function(dev);
254 pci_restore_state(psdev->dev);
256 /* This disables the device. */
257 xen_pcibk_reset_device(found_psdev->dev);
259 /* And cleanup up our emulated fields. */
260 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
261 xen_pcibk_config_reset_dev(found_psdev->dev);
263 xen_unregister_device_domain_owner(found_psdev->dev);
265 spin_lock_irqsave(&found_psdev->lock, flags);
266 found_psdev->pdev = NULL;
267 spin_unlock_irqrestore(&found_psdev->lock, flags);
269 pcistub_device_put(found_psdev);
270 up_write(&pcistub_sem);
273 static int __devinit pcistub_match_one(struct pci_dev *dev,
274 struct pcistub_device_id *pdev_id)
276 /* Match the specified device by domain, bus, slot, func and also if
277 * any of the device's parent bridges match.
279 for (; dev != NULL; dev = dev->bus->self) {
280 if (pci_domain_nr(dev->bus) == pdev_id->domain
281 && dev->bus->number == pdev_id->bus
282 && dev->devfn == pdev_id->devfn)
283 return 1;
285 /* Sometimes topmost bridge links to itself. */
286 if (dev == dev->bus->self)
287 break;
290 return 0;
293 static int __devinit pcistub_match(struct pci_dev *dev)
295 struct pcistub_device_id *pdev_id;
296 unsigned long flags;
297 int found = 0;
299 spin_lock_irqsave(&device_ids_lock, flags);
300 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
301 if (pcistub_match_one(dev, pdev_id)) {
302 found = 1;
303 break;
306 spin_unlock_irqrestore(&device_ids_lock, flags);
308 return found;
311 static int __devinit pcistub_init_device(struct pci_dev *dev)
313 struct xen_pcibk_dev_data *dev_data;
314 int err = 0;
316 dev_dbg(&dev->dev, "initializing...\n");
318 /* The PCI backend is not intended to be a module (or to work with
319 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
320 * would need to be called somewhere to free the memory allocated
321 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
323 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
324 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
325 if (!dev_data) {
326 err = -ENOMEM;
327 goto out;
329 pci_set_drvdata(dev, dev_data);
332 * Setup name for fake IRQ handler. It will only be enabled
333 * once the device is turned on by the guest.
335 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
337 dev_dbg(&dev->dev, "initializing config\n");
339 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
340 err = xen_pcibk_config_init_dev(dev);
341 if (err)
342 goto out;
344 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
345 * must do this here because pcibios_enable_device may specify
346 * the pci device's true irq (and possibly its other resources)
347 * if they differ from what's in the configuration space.
348 * This makes the assumption that the device's resources won't
349 * change after this point (otherwise this code may break!)
351 dev_dbg(&dev->dev, "enabling device\n");
352 err = pci_enable_device(dev);
353 if (err)
354 goto config_release;
356 /* We need the device active to save the state. */
357 dev_dbg(&dev->dev, "save state of device\n");
358 pci_save_state(dev);
359 dev_data->pci_saved_state = pci_store_saved_state(dev);
360 if (!dev_data->pci_saved_state)
361 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
362 else {
363 dev_dbg(&dev->dev, "reseting (FLR, D3, etc) the device\n");
364 __pci_reset_function_locked(dev);
365 pci_restore_state(dev);
367 /* Now disable the device (this also ensures some private device
368 * data is setup before we export)
370 dev_dbg(&dev->dev, "reset device\n");
371 xen_pcibk_reset_device(dev);
373 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
374 return 0;
376 config_release:
377 xen_pcibk_config_free_dev(dev);
379 out:
380 pci_set_drvdata(dev, NULL);
381 kfree(dev_data);
382 return err;
386 * Because some initialization still happens on
387 * devices during fs_initcall, we need to defer
388 * full initialization of our devices until
389 * device_initcall.
391 static int __init pcistub_init_devices_late(void)
393 struct pcistub_device *psdev;
394 unsigned long flags;
395 int err = 0;
397 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
399 spin_lock_irqsave(&pcistub_devices_lock, flags);
401 while (!list_empty(&seized_devices)) {
402 psdev = container_of(seized_devices.next,
403 struct pcistub_device, dev_list);
404 list_del(&psdev->dev_list);
406 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
408 err = pcistub_init_device(psdev->dev);
409 if (err) {
410 dev_err(&psdev->dev->dev,
411 "error %d initializing device\n", err);
412 kfree(psdev);
413 psdev = NULL;
416 spin_lock_irqsave(&pcistub_devices_lock, flags);
418 if (psdev)
419 list_add_tail(&psdev->dev_list, &pcistub_devices);
422 initialize_devices = 1;
424 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
426 return 0;
429 static int __devinit pcistub_seize(struct pci_dev *dev)
431 struct pcistub_device *psdev;
432 unsigned long flags;
433 int err = 0;
435 psdev = pcistub_device_alloc(dev);
436 if (!psdev)
437 return -ENOMEM;
439 spin_lock_irqsave(&pcistub_devices_lock, flags);
441 if (initialize_devices) {
442 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
444 /* don't want irqs disabled when calling pcistub_init_device */
445 err = pcistub_init_device(psdev->dev);
447 spin_lock_irqsave(&pcistub_devices_lock, flags);
449 if (!err)
450 list_add(&psdev->dev_list, &pcistub_devices);
451 } else {
452 dev_dbg(&dev->dev, "deferring initialization\n");
453 list_add(&psdev->dev_list, &seized_devices);
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
458 if (err)
459 pcistub_device_put(psdev);
461 return err;
464 static int __devinit pcistub_probe(struct pci_dev *dev,
465 const struct pci_device_id *id)
467 int err = 0;
469 dev_dbg(&dev->dev, "probing...\n");
471 if (pcistub_match(dev)) {
473 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
474 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
475 dev_err(&dev->dev, "can't export pci devices that "
476 "don't have a normal (0) or bridge (1) "
477 "header type!\n");
478 err = -ENODEV;
479 goto out;
482 dev_info(&dev->dev, "seizing device\n");
483 err = pcistub_seize(dev);
484 } else
485 /* Didn't find the device */
486 err = -ENODEV;
488 out:
489 return err;
492 static void pcistub_remove(struct pci_dev *dev)
494 struct pcistub_device *psdev, *found_psdev = NULL;
495 unsigned long flags;
497 dev_dbg(&dev->dev, "removing\n");
499 spin_lock_irqsave(&pcistub_devices_lock, flags);
501 xen_pcibk_config_quirk_release(dev);
503 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
504 if (psdev->dev == dev) {
505 found_psdev = psdev;
506 break;
510 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
512 if (found_psdev) {
513 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
514 found_psdev->pdev);
516 if (found_psdev->pdev) {
517 printk(KERN_WARNING DRV_NAME ": ****** removing device "
518 "%s while still in-use! ******\n",
519 pci_name(found_psdev->dev));
520 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
521 " still access this device's i/o resources!\n");
522 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
523 "domain before binding device\n");
524 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
525 "or domains\n");
527 xen_pcibk_release_pci_dev(found_psdev->pdev,
528 found_psdev->dev);
531 spin_lock_irqsave(&pcistub_devices_lock, flags);
532 list_del(&found_psdev->dev_list);
533 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
535 /* the final put for releasing from the list */
536 pcistub_device_put(found_psdev);
540 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
542 .vendor = PCI_ANY_ID,
543 .device = PCI_ANY_ID,
544 .subvendor = PCI_ANY_ID,
545 .subdevice = PCI_ANY_ID,
547 {0,},
550 #define PCI_NODENAME_MAX 40
551 static void kill_domain_by_device(struct pcistub_device *psdev)
553 struct xenbus_transaction xbt;
554 int err;
555 char nodename[PCI_NODENAME_MAX];
557 BUG_ON(!psdev);
558 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
559 psdev->pdev->xdev->otherend_id);
561 again:
562 err = xenbus_transaction_start(&xbt);
563 if (err) {
564 dev_err(&psdev->dev->dev,
565 "error %d when start xenbus transaction\n", err);
566 return;
568 /*PV AER handlers will set this flag*/
569 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
570 err = xenbus_transaction_end(xbt, 0);
571 if (err) {
572 if (err == -EAGAIN)
573 goto again;
574 dev_err(&psdev->dev->dev,
575 "error %d when end xenbus transaction\n", err);
576 return;
580 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
581 * backend need to have cooperation. In xen_pcibk, those steps will do similar
582 * jobs: send service request and waiting for front_end response.
584 static pci_ers_result_t common_process(struct pcistub_device *psdev,
585 pci_channel_state_t state, int aer_cmd,
586 pci_ers_result_t result)
588 pci_ers_result_t res = result;
589 struct xen_pcie_aer_op *aer_op;
590 int ret;
592 /*with PV AER drivers*/
593 aer_op = &(psdev->pdev->sh_info->aer_op);
594 aer_op->cmd = aer_cmd ;
595 /*useful for error_detected callback*/
596 aer_op->err = state;
597 /*pcifront_end BDF*/
598 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
599 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
600 if (!ret) {
601 dev_err(&psdev->dev->dev,
602 DRV_NAME ": failed to get pcifront device\n");
603 return PCI_ERS_RESULT_NONE;
605 wmb();
607 dev_dbg(&psdev->dev->dev,
608 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
609 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
610 /*local flag to mark there's aer request, xen_pcibk callback will use
611 * this flag to judge whether we need to check pci-front give aer
612 * service ack signal
614 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
616 /*It is possible that a pcifront conf_read_write ops request invokes
617 * the callback which cause the spurious execution of wake_up.
618 * Yet it is harmless and better than a spinlock here
620 set_bit(_XEN_PCIB_active,
621 (unsigned long *)&psdev->pdev->sh_info->flags);
622 wmb();
623 notify_remote_via_irq(psdev->pdev->evtchn_irq);
625 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
626 !(test_bit(_XEN_PCIB_active, (unsigned long *)
627 &psdev->pdev->sh_info->flags)), 300*HZ);
629 if (!ret) {
630 if (test_bit(_XEN_PCIB_active,
631 (unsigned long *)&psdev->pdev->sh_info->flags)) {
632 dev_err(&psdev->dev->dev,
633 "pcifront aer process not responding!\n");
634 clear_bit(_XEN_PCIB_active,
635 (unsigned long *)&psdev->pdev->sh_info->flags);
636 aer_op->err = PCI_ERS_RESULT_NONE;
637 return res;
640 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
642 if (test_bit(_XEN_PCIF_active,
643 (unsigned long *)&psdev->pdev->sh_info->flags)) {
644 dev_dbg(&psdev->dev->dev,
645 "schedule pci_conf service in " DRV_NAME "\n");
646 xen_pcibk_test_and_schedule_op(psdev->pdev);
649 res = (pci_ers_result_t)aer_op->err;
650 return res;
654 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
655 * of the device driver could provide this service, and then wait for pcifront
656 * ack.
657 * @dev: pointer to PCI devices
658 * return value is used by aer_core do_recovery policy
660 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
662 struct pcistub_device *psdev;
663 pci_ers_result_t result;
665 result = PCI_ERS_RESULT_RECOVERED;
666 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
667 dev->bus->number, dev->devfn);
669 down_write(&pcistub_sem);
670 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
671 dev->bus->number,
672 PCI_SLOT(dev->devfn),
673 PCI_FUNC(dev->devfn));
675 if (!psdev || !psdev->pdev) {
676 dev_err(&dev->dev,
677 DRV_NAME " device is not found/assigned\n");
678 goto end;
681 if (!psdev->pdev->sh_info) {
682 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
683 " by HVM, kill it\n");
684 kill_domain_by_device(psdev);
685 goto release;
688 if (!test_bit(_XEN_PCIB_AERHANDLER,
689 (unsigned long *)&psdev->pdev->sh_info->flags)) {
690 dev_err(&dev->dev,
691 "guest with no AER driver should have been killed\n");
692 goto release;
694 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
696 if (result == PCI_ERS_RESULT_NONE ||
697 result == PCI_ERS_RESULT_DISCONNECT) {
698 dev_dbg(&dev->dev,
699 "No AER slot_reset service or disconnected!\n");
700 kill_domain_by_device(psdev);
702 release:
703 pcistub_device_put(psdev);
704 end:
705 up_write(&pcistub_sem);
706 return result;
711 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
712 * in case of the device driver could provide this service, and then wait
713 * for pcifront ack
714 * @dev: pointer to PCI devices
715 * return value is used by aer_core do_recovery policy
718 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
720 struct pcistub_device *psdev;
721 pci_ers_result_t result;
723 result = PCI_ERS_RESULT_RECOVERED;
724 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
725 dev->bus->number, dev->devfn);
727 down_write(&pcistub_sem);
728 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
729 dev->bus->number,
730 PCI_SLOT(dev->devfn),
731 PCI_FUNC(dev->devfn));
733 if (!psdev || !psdev->pdev) {
734 dev_err(&dev->dev,
735 DRV_NAME " device is not found/assigned\n");
736 goto end;
739 if (!psdev->pdev->sh_info) {
740 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
741 " by HVM, kill it\n");
742 kill_domain_by_device(psdev);
743 goto release;
746 if (!test_bit(_XEN_PCIB_AERHANDLER,
747 (unsigned long *)&psdev->pdev->sh_info->flags)) {
748 dev_err(&dev->dev,
749 "guest with no AER driver should have been killed\n");
750 goto release;
752 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
754 if (result == PCI_ERS_RESULT_NONE ||
755 result == PCI_ERS_RESULT_DISCONNECT) {
756 dev_dbg(&dev->dev,
757 "No AER mmio_enabled service or disconnected!\n");
758 kill_domain_by_device(psdev);
760 release:
761 pcistub_device_put(psdev);
762 end:
763 up_write(&pcistub_sem);
764 return result;
767 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
768 * in case of the device driver could provide this service, and then wait
769 * for pcifront ack.
770 * @dev: pointer to PCI devices
771 * @error: the current PCI connection state
772 * return value is used by aer_core do_recovery policy
775 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
776 pci_channel_state_t error)
778 struct pcistub_device *psdev;
779 pci_ers_result_t result;
781 result = PCI_ERS_RESULT_CAN_RECOVER;
782 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
783 dev->bus->number, dev->devfn);
785 down_write(&pcistub_sem);
786 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
787 dev->bus->number,
788 PCI_SLOT(dev->devfn),
789 PCI_FUNC(dev->devfn));
791 if (!psdev || !psdev->pdev) {
792 dev_err(&dev->dev,
793 DRV_NAME " device is not found/assigned\n");
794 goto end;
797 if (!psdev->pdev->sh_info) {
798 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
799 " by HVM, kill it\n");
800 kill_domain_by_device(psdev);
801 goto release;
804 /*Guest owns the device yet no aer handler regiested, kill guest*/
805 if (!test_bit(_XEN_PCIB_AERHANDLER,
806 (unsigned long *)&psdev->pdev->sh_info->flags)) {
807 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
808 kill_domain_by_device(psdev);
809 goto release;
811 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
813 if (result == PCI_ERS_RESULT_NONE ||
814 result == PCI_ERS_RESULT_DISCONNECT) {
815 dev_dbg(&dev->dev,
816 "No AER error_detected service or disconnected!\n");
817 kill_domain_by_device(psdev);
819 release:
820 pcistub_device_put(psdev);
821 end:
822 up_write(&pcistub_sem);
823 return result;
826 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
827 * in case of the device driver could provide this service, and then wait
828 * for pcifront ack.
829 * @dev: pointer to PCI devices
832 static void xen_pcibk_error_resume(struct pci_dev *dev)
834 struct pcistub_device *psdev;
836 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
837 dev->bus->number, dev->devfn);
839 down_write(&pcistub_sem);
840 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
841 dev->bus->number,
842 PCI_SLOT(dev->devfn),
843 PCI_FUNC(dev->devfn));
845 if (!psdev || !psdev->pdev) {
846 dev_err(&dev->dev,
847 DRV_NAME " device is not found/assigned\n");
848 goto end;
851 if (!psdev->pdev->sh_info) {
852 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
853 " by HVM, kill it\n");
854 kill_domain_by_device(psdev);
855 goto release;
858 if (!test_bit(_XEN_PCIB_AERHANDLER,
859 (unsigned long *)&psdev->pdev->sh_info->flags)) {
860 dev_err(&dev->dev,
861 "guest with no AER driver should have been killed\n");
862 kill_domain_by_device(psdev);
863 goto release;
865 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
866 PCI_ERS_RESULT_RECOVERED);
867 release:
868 pcistub_device_put(psdev);
869 end:
870 up_write(&pcistub_sem);
871 return;
874 /*add xen_pcibk AER handling*/
875 static struct pci_error_handlers xen_pcibk_error_handler = {
876 .error_detected = xen_pcibk_error_detected,
877 .mmio_enabled = xen_pcibk_mmio_enabled,
878 .slot_reset = xen_pcibk_slot_reset,
879 .resume = xen_pcibk_error_resume,
883 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
884 * for a normal device. I don't want it to be loaded automatically.
887 static struct pci_driver xen_pcibk_pci_driver = {
888 /* The name should be xen_pciback, but until the tools are updated
889 * we will keep it as pciback. */
890 .name = "pciback",
891 .id_table = pcistub_ids,
892 .probe = pcistub_probe,
893 .remove = pcistub_remove,
894 .err_handler = &xen_pcibk_error_handler,
897 static inline int str_to_slot(const char *buf, int *domain, int *bus,
898 int *slot, int *func)
900 int err;
902 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
903 if (err == 4)
904 return 0;
905 else if (err < 0)
906 return -EINVAL;
908 /* try again without domain */
909 *domain = 0;
910 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
911 if (err == 3)
912 return 0;
914 return -EINVAL;
917 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
918 *slot, int *func, int *reg, int *size, int *mask)
920 int err;
922 err =
923 sscanf(buf, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot,
924 func, reg, size, mask);
925 if (err == 7)
926 return 0;
927 return -EINVAL;
930 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
932 struct pcistub_device_id *pci_dev_id;
933 unsigned long flags;
935 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
936 if (!pci_dev_id)
937 return -ENOMEM;
939 pci_dev_id->domain = domain;
940 pci_dev_id->bus = bus;
941 pci_dev_id->devfn = PCI_DEVFN(slot, func);
943 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
944 domain, bus, slot, func);
946 spin_lock_irqsave(&device_ids_lock, flags);
947 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
948 spin_unlock_irqrestore(&device_ids_lock, flags);
950 return 0;
953 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
955 struct pcistub_device_id *pci_dev_id, *t;
956 int devfn = PCI_DEVFN(slot, func);
957 int err = -ENOENT;
958 unsigned long flags;
960 spin_lock_irqsave(&device_ids_lock, flags);
961 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
962 slot_list) {
963 if (pci_dev_id->domain == domain
964 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
965 /* Don't break; here because it's possible the same
966 * slot could be in the list more than once
968 list_del(&pci_dev_id->slot_list);
969 kfree(pci_dev_id);
971 err = 0;
973 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
974 "seize list\n", domain, bus, slot, func);
977 spin_unlock_irqrestore(&device_ids_lock, flags);
979 return err;
982 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
983 int size, int mask)
985 int err = 0;
986 struct pcistub_device *psdev;
987 struct pci_dev *dev;
988 struct config_field *field;
990 psdev = pcistub_device_find(domain, bus, slot, func);
991 if (!psdev || !psdev->dev) {
992 err = -ENODEV;
993 goto out;
995 dev = psdev->dev;
997 field = kzalloc(sizeof(*field), GFP_ATOMIC);
998 if (!field) {
999 err = -ENOMEM;
1000 goto out;
1003 field->offset = reg;
1004 field->size = size;
1005 field->mask = mask;
1006 field->init = NULL;
1007 field->reset = NULL;
1008 field->release = NULL;
1009 field->clean = xen_pcibk_config_field_free;
1011 err = xen_pcibk_config_quirks_add_field(dev, field);
1012 if (err)
1013 kfree(field);
1014 out:
1015 return err;
1018 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1019 size_t count)
1021 int domain, bus, slot, func;
1022 int err;
1024 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1025 if (err)
1026 goto out;
1028 err = pcistub_device_id_add(domain, bus, slot, func);
1030 out:
1031 if (!err)
1032 err = count;
1033 return err;
1035 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1037 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1038 size_t count)
1040 int domain, bus, slot, func;
1041 int err;
1043 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1044 if (err)
1045 goto out;
1047 err = pcistub_device_id_remove(domain, bus, slot, func);
1049 out:
1050 if (!err)
1051 err = count;
1052 return err;
1054 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1056 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1058 struct pcistub_device_id *pci_dev_id;
1059 size_t count = 0;
1060 unsigned long flags;
1062 spin_lock_irqsave(&device_ids_lock, flags);
1063 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1064 if (count >= PAGE_SIZE)
1065 break;
1067 count += scnprintf(buf + count, PAGE_SIZE - count,
1068 "%04x:%02x:%02x.%d\n",
1069 pci_dev_id->domain, pci_dev_id->bus,
1070 PCI_SLOT(pci_dev_id->devfn),
1071 PCI_FUNC(pci_dev_id->devfn));
1073 spin_unlock_irqrestore(&device_ids_lock, flags);
1075 return count;
1077 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1079 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1081 struct pcistub_device *psdev;
1082 struct xen_pcibk_dev_data *dev_data;
1083 size_t count = 0;
1084 unsigned long flags;
1086 spin_lock_irqsave(&pcistub_devices_lock, flags);
1087 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1088 if (count >= PAGE_SIZE)
1089 break;
1090 if (!psdev->dev)
1091 continue;
1092 dev_data = pci_get_drvdata(psdev->dev);
1093 if (!dev_data)
1094 continue;
1095 count +=
1096 scnprintf(buf + count, PAGE_SIZE - count,
1097 "%s:%s:%sing:%ld\n",
1098 pci_name(psdev->dev),
1099 dev_data->isr_on ? "on" : "off",
1100 dev_data->ack_intr ? "ack" : "not ack",
1101 dev_data->handled);
1103 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1104 return count;
1106 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1108 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1109 const char *buf,
1110 size_t count)
1112 struct pcistub_device *psdev;
1113 struct xen_pcibk_dev_data *dev_data;
1114 int domain, bus, slot, func;
1115 int err = -ENOENT;
1117 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1118 if (err)
1119 goto out;
1121 psdev = pcistub_device_find(domain, bus, slot, func);
1123 if (!psdev)
1124 goto out;
1126 dev_data = pci_get_drvdata(psdev->dev);
1127 if (!dev_data)
1128 goto out;
1130 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1131 dev_data->irq_name, dev_data->isr_on,
1132 !dev_data->isr_on);
1134 dev_data->isr_on = !(dev_data->isr_on);
1135 if (dev_data->isr_on)
1136 dev_data->ack_intr = 1;
1137 out:
1138 if (!err)
1139 err = count;
1140 return err;
1142 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1143 pcistub_irq_handler_switch);
1145 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1146 size_t count)
1148 int domain, bus, slot, func, reg, size, mask;
1149 int err;
1151 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1152 &mask);
1153 if (err)
1154 goto out;
1156 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1158 out:
1159 if (!err)
1160 err = count;
1161 return err;
1164 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1166 int count = 0;
1167 unsigned long flags;
1168 struct xen_pcibk_config_quirk *quirk;
1169 struct xen_pcibk_dev_data *dev_data;
1170 const struct config_field *field;
1171 const struct config_field_entry *cfg_entry;
1173 spin_lock_irqsave(&device_ids_lock, flags);
1174 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1175 if (count >= PAGE_SIZE)
1176 goto out;
1178 count += scnprintf(buf + count, PAGE_SIZE - count,
1179 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1180 quirk->pdev->bus->number,
1181 PCI_SLOT(quirk->pdev->devfn),
1182 PCI_FUNC(quirk->pdev->devfn),
1183 quirk->devid.vendor, quirk->devid.device,
1184 quirk->devid.subvendor,
1185 quirk->devid.subdevice);
1187 dev_data = pci_get_drvdata(quirk->pdev);
1189 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1190 field = cfg_entry->field;
1191 if (count >= PAGE_SIZE)
1192 goto out;
1194 count += scnprintf(buf + count, PAGE_SIZE - count,
1195 "\t\t%08x:%01x:%08x\n",
1196 cfg_entry->base_offset +
1197 field->offset, field->size,
1198 field->mask);
1202 out:
1203 spin_unlock_irqrestore(&device_ids_lock, flags);
1205 return count;
1207 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1208 pcistub_quirk_add);
1210 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1211 size_t count)
1213 int domain, bus, slot, func;
1214 int err;
1215 struct pcistub_device *psdev;
1216 struct xen_pcibk_dev_data *dev_data;
1217 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1218 if (err)
1219 goto out;
1220 psdev = pcistub_device_find(domain, bus, slot, func);
1221 if (!psdev) {
1222 err = -ENODEV;
1223 goto out;
1225 if (!psdev->dev) {
1226 err = -ENODEV;
1227 goto release;
1229 dev_data = pci_get_drvdata(psdev->dev);
1230 /* the driver data for a device should never be null at this point */
1231 if (!dev_data) {
1232 err = -ENXIO;
1233 goto release;
1235 if (!dev_data->permissive) {
1236 dev_data->permissive = 1;
1237 /* Let user know that what they're doing could be unsafe */
1238 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1239 "configuration space accesses!\n");
1240 dev_warn(&psdev->dev->dev,
1241 "permissive mode is potentially unsafe!\n");
1243 release:
1244 pcistub_device_put(psdev);
1245 out:
1246 if (!err)
1247 err = count;
1248 return err;
1251 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1253 struct pcistub_device *psdev;
1254 struct xen_pcibk_dev_data *dev_data;
1255 size_t count = 0;
1256 unsigned long flags;
1257 spin_lock_irqsave(&pcistub_devices_lock, flags);
1258 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1259 if (count >= PAGE_SIZE)
1260 break;
1261 if (!psdev->dev)
1262 continue;
1263 dev_data = pci_get_drvdata(psdev->dev);
1264 if (!dev_data || !dev_data->permissive)
1265 continue;
1266 count +=
1267 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1268 pci_name(psdev->dev));
1270 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1271 return count;
1273 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1274 permissive_add);
1276 static void pcistub_exit(void)
1278 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1279 driver_remove_file(&xen_pcibk_pci_driver.driver,
1280 &driver_attr_remove_slot);
1281 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1282 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1283 driver_remove_file(&xen_pcibk_pci_driver.driver,
1284 &driver_attr_permissive);
1285 driver_remove_file(&xen_pcibk_pci_driver.driver,
1286 &driver_attr_irq_handlers);
1287 driver_remove_file(&xen_pcibk_pci_driver.driver,
1288 &driver_attr_irq_handler_state);
1289 pci_unregister_driver(&xen_pcibk_pci_driver);
1292 static int __init pcistub_init(void)
1294 int pos = 0;
1295 int err = 0;
1296 int domain, bus, slot, func;
1297 int parsed;
1299 if (pci_devs_to_hide && *pci_devs_to_hide) {
1300 do {
1301 parsed = 0;
1303 err = sscanf(pci_devs_to_hide + pos,
1304 " (%x:%x:%x.%x) %n",
1305 &domain, &bus, &slot, &func, &parsed);
1306 if (err != 4) {
1307 domain = 0;
1308 err = sscanf(pci_devs_to_hide + pos,
1309 " (%x:%x.%x) %n",
1310 &bus, &slot, &func, &parsed);
1311 if (err != 3)
1312 goto parse_error;
1315 err = pcistub_device_id_add(domain, bus, slot, func);
1316 if (err)
1317 goto out;
1319 /* if parsed<=0, we've reached the end of the string */
1320 pos += parsed;
1321 } while (parsed > 0 && pci_devs_to_hide[pos]);
1324 /* If we're the first PCI Device Driver to register, we're the
1325 * first one to get offered PCI devices as they become
1326 * available (and thus we can be the first to grab them)
1328 err = pci_register_driver(&xen_pcibk_pci_driver);
1329 if (err < 0)
1330 goto out;
1332 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1333 &driver_attr_new_slot);
1334 if (!err)
1335 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1336 &driver_attr_remove_slot);
1337 if (!err)
1338 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1339 &driver_attr_slots);
1340 if (!err)
1341 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1342 &driver_attr_quirks);
1343 if (!err)
1344 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1345 &driver_attr_permissive);
1347 if (!err)
1348 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1349 &driver_attr_irq_handlers);
1350 if (!err)
1351 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1352 &driver_attr_irq_handler_state);
1353 if (err)
1354 pcistub_exit();
1356 out:
1357 return err;
1359 parse_error:
1360 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
1361 pci_devs_to_hide + pos);
1362 return -EINVAL;
1365 #ifndef MODULE
1367 * fs_initcall happens before device_initcall
1368 * so xen_pcibk *should* get called first (b/c we
1369 * want to suck up any device before other drivers
1370 * get a chance by being the first pci device
1371 * driver to register)
1373 fs_initcall(pcistub_init);
1374 #endif
1376 static int __init xen_pcibk_init(void)
1378 int err;
1380 if (!xen_initial_domain())
1381 return -ENODEV;
1383 err = xen_pcibk_config_init();
1384 if (err)
1385 return err;
1387 #ifdef MODULE
1388 err = pcistub_init();
1389 if (err < 0)
1390 return err;
1391 #endif
1393 pcistub_init_devices_late();
1394 err = xen_pcibk_xenbus_register();
1395 if (err)
1396 pcistub_exit();
1398 return err;
1401 static void __exit xen_pcibk_cleanup(void)
1403 xen_pcibk_xenbus_unregister();
1404 pcistub_exit();
1407 module_init(xen_pcibk_init);
1408 module_exit(xen_pcibk_cleanup);
1410 MODULE_LICENSE("Dual BSD/GPL");
1411 MODULE_ALIAS("xen-backend:pci");