i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / xen / xen-pciback / pci_stub.c
blob19834d1c7c3679668fd50b3b3514ed0390014dfe
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;
89 psdev = container_of(kref, struct pcistub_device, kref);
91 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
93 xen_unregister_device_domain_owner(psdev->dev);
95 /* Clean-up the device */
96 xen_pcibk_reset_device(psdev->dev);
97 xen_pcibk_config_free_dyn_fields(psdev->dev);
98 xen_pcibk_config_free_dev(psdev->dev);
99 kfree(pci_get_drvdata(psdev->dev));
100 pci_set_drvdata(psdev->dev, NULL);
102 psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
103 pci_dev_put(psdev->dev);
105 kfree(psdev);
108 static inline void pcistub_device_get(struct pcistub_device *psdev)
110 kref_get(&psdev->kref);
113 static inline void pcistub_device_put(struct pcistub_device *psdev)
115 kref_put(&psdev->kref, pcistub_device_release);
118 static struct pcistub_device *pcistub_device_find(int domain, int bus,
119 int slot, int func)
121 struct pcistub_device *psdev = NULL;
122 unsigned long flags;
124 spin_lock_irqsave(&pcistub_devices_lock, flags);
126 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
127 if (psdev->dev != NULL
128 && domain == pci_domain_nr(psdev->dev->bus)
129 && bus == psdev->dev->bus->number
130 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
131 pcistub_device_get(psdev);
132 goto out;
136 /* didn't find it */
137 psdev = NULL;
139 out:
140 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
141 return psdev;
144 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
145 struct pcistub_device *psdev)
147 struct pci_dev *pci_dev = NULL;
148 unsigned long flags;
150 pcistub_device_get(psdev);
152 spin_lock_irqsave(&psdev->lock, flags);
153 if (!psdev->pdev) {
154 psdev->pdev = pdev;
155 pci_dev = psdev->dev;
157 spin_unlock_irqrestore(&psdev->lock, flags);
159 if (!pci_dev)
160 pcistub_device_put(psdev);
162 return pci_dev;
165 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
166 int domain, int bus,
167 int slot, int func)
169 struct pcistub_device *psdev;
170 struct pci_dev *found_dev = NULL;
171 unsigned long flags;
173 spin_lock_irqsave(&pcistub_devices_lock, flags);
175 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
176 if (psdev->dev != NULL
177 && domain == pci_domain_nr(psdev->dev->bus)
178 && bus == psdev->dev->bus->number
179 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
180 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
181 break;
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 return found_dev;
189 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
190 struct pci_dev *dev)
192 struct pcistub_device *psdev;
193 struct pci_dev *found_dev = NULL;
194 unsigned long flags;
196 spin_lock_irqsave(&pcistub_devices_lock, flags);
198 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
199 if (psdev->dev == dev) {
200 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
201 break;
205 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
206 return found_dev;
209 void pcistub_put_pci_dev(struct pci_dev *dev)
211 struct pcistub_device *psdev, *found_psdev = NULL;
212 unsigned long flags;
214 spin_lock_irqsave(&pcistub_devices_lock, flags);
216 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
217 if (psdev->dev == dev) {
218 found_psdev = psdev;
219 break;
223 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
224 if (WARN_ON(!found_psdev))
225 return;
227 /*hold this lock for avoiding breaking link between
228 * pcistub and xen_pcibk when AER is in processing
230 down_write(&pcistub_sem);
231 /* Cleanup our device
232 * (so it's ready for the next domain)
234 xen_pcibk_reset_device(found_psdev->dev);
235 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
236 xen_pcibk_config_reset_dev(found_psdev->dev);
238 xen_unregister_device_domain_owner(found_psdev->dev);
240 spin_lock_irqsave(&found_psdev->lock, flags);
241 found_psdev->pdev = NULL;
242 spin_unlock_irqrestore(&found_psdev->lock, flags);
244 pcistub_device_put(found_psdev);
245 up_write(&pcistub_sem);
248 static int __devinit pcistub_match_one(struct pci_dev *dev,
249 struct pcistub_device_id *pdev_id)
251 /* Match the specified device by domain, bus, slot, func and also if
252 * any of the device's parent bridges match.
254 for (; dev != NULL; dev = dev->bus->self) {
255 if (pci_domain_nr(dev->bus) == pdev_id->domain
256 && dev->bus->number == pdev_id->bus
257 && dev->devfn == pdev_id->devfn)
258 return 1;
260 /* Sometimes topmost bridge links to itself. */
261 if (dev == dev->bus->self)
262 break;
265 return 0;
268 static int __devinit pcistub_match(struct pci_dev *dev)
270 struct pcistub_device_id *pdev_id;
271 unsigned long flags;
272 int found = 0;
274 spin_lock_irqsave(&device_ids_lock, flags);
275 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
276 if (pcistub_match_one(dev, pdev_id)) {
277 found = 1;
278 break;
281 spin_unlock_irqrestore(&device_ids_lock, flags);
283 return found;
286 static int __devinit pcistub_init_device(struct pci_dev *dev)
288 struct xen_pcibk_dev_data *dev_data;
289 int err = 0;
291 dev_dbg(&dev->dev, "initializing...\n");
293 /* The PCI backend is not intended to be a module (or to work with
294 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
295 * would need to be called somewhere to free the memory allocated
296 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
298 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
299 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
300 if (!dev_data) {
301 err = -ENOMEM;
302 goto out;
304 pci_set_drvdata(dev, dev_data);
307 * Setup name for fake IRQ handler. It will only be enabled
308 * once the device is turned on by the guest.
310 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
312 dev_dbg(&dev->dev, "initializing config\n");
314 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
315 err = xen_pcibk_config_init_dev(dev);
316 if (err)
317 goto out;
319 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
320 * must do this here because pcibios_enable_device may specify
321 * the pci device's true irq (and possibly its other resources)
322 * if they differ from what's in the configuration space.
323 * This makes the assumption that the device's resources won't
324 * change after this point (otherwise this code may break!)
326 dev_dbg(&dev->dev, "enabling device\n");
327 err = pci_enable_device(dev);
328 if (err)
329 goto config_release;
331 /* Now disable the device (this also ensures some private device
332 * data is setup before we export)
334 dev_dbg(&dev->dev, "reset device\n");
335 xen_pcibk_reset_device(dev);
337 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
338 return 0;
340 config_release:
341 xen_pcibk_config_free_dev(dev);
343 out:
344 pci_set_drvdata(dev, NULL);
345 kfree(dev_data);
346 return err;
350 * Because some initialization still happens on
351 * devices during fs_initcall, we need to defer
352 * full initialization of our devices until
353 * device_initcall.
355 static int __init pcistub_init_devices_late(void)
357 struct pcistub_device *psdev;
358 unsigned long flags;
359 int err = 0;
361 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
363 spin_lock_irqsave(&pcistub_devices_lock, flags);
365 while (!list_empty(&seized_devices)) {
366 psdev = container_of(seized_devices.next,
367 struct pcistub_device, dev_list);
368 list_del(&psdev->dev_list);
370 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
372 err = pcistub_init_device(psdev->dev);
373 if (err) {
374 dev_err(&psdev->dev->dev,
375 "error %d initializing device\n", err);
376 kfree(psdev);
377 psdev = NULL;
380 spin_lock_irqsave(&pcistub_devices_lock, flags);
382 if (psdev)
383 list_add_tail(&psdev->dev_list, &pcistub_devices);
386 initialize_devices = 1;
388 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
390 return 0;
393 static int __devinit pcistub_seize(struct pci_dev *dev)
395 struct pcistub_device *psdev;
396 unsigned long flags;
397 int err = 0;
399 psdev = pcistub_device_alloc(dev);
400 if (!psdev)
401 return -ENOMEM;
403 spin_lock_irqsave(&pcistub_devices_lock, flags);
405 if (initialize_devices) {
406 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
408 /* don't want irqs disabled when calling pcistub_init_device */
409 err = pcistub_init_device(psdev->dev);
411 spin_lock_irqsave(&pcistub_devices_lock, flags);
413 if (!err)
414 list_add(&psdev->dev_list, &pcistub_devices);
415 } else {
416 dev_dbg(&dev->dev, "deferring initialization\n");
417 list_add(&psdev->dev_list, &seized_devices);
420 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
422 if (err)
423 pcistub_device_put(psdev);
425 return err;
428 static int __devinit pcistub_probe(struct pci_dev *dev,
429 const struct pci_device_id *id)
431 int err = 0;
433 dev_dbg(&dev->dev, "probing...\n");
435 if (pcistub_match(dev)) {
437 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
438 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
439 dev_err(&dev->dev, "can't export pci devices that "
440 "don't have a normal (0) or bridge (1) "
441 "header type!\n");
442 err = -ENODEV;
443 goto out;
446 dev_info(&dev->dev, "seizing device\n");
447 err = pcistub_seize(dev);
448 } else
449 /* Didn't find the device */
450 err = -ENODEV;
452 out:
453 return err;
456 static void pcistub_remove(struct pci_dev *dev)
458 struct pcistub_device *psdev, *found_psdev = NULL;
459 unsigned long flags;
461 dev_dbg(&dev->dev, "removing\n");
463 spin_lock_irqsave(&pcistub_devices_lock, flags);
465 xen_pcibk_config_quirk_release(dev);
467 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
468 if (psdev->dev == dev) {
469 found_psdev = psdev;
470 break;
474 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
476 if (found_psdev) {
477 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
478 found_psdev->pdev);
480 if (found_psdev->pdev) {
481 printk(KERN_WARNING DRV_NAME ": ****** removing device "
482 "%s while still in-use! ******\n",
483 pci_name(found_psdev->dev));
484 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
485 " still access this device's i/o resources!\n");
486 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
487 "domain before binding device\n");
488 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
489 "or domains\n");
491 xen_pcibk_release_pci_dev(found_psdev->pdev,
492 found_psdev->dev);
495 spin_lock_irqsave(&pcistub_devices_lock, flags);
496 list_del(&found_psdev->dev_list);
497 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
499 /* the final put for releasing from the list */
500 pcistub_device_put(found_psdev);
504 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
506 .vendor = PCI_ANY_ID,
507 .device = PCI_ANY_ID,
508 .subvendor = PCI_ANY_ID,
509 .subdevice = PCI_ANY_ID,
511 {0,},
514 #define PCI_NODENAME_MAX 40
515 static void kill_domain_by_device(struct pcistub_device *psdev)
517 struct xenbus_transaction xbt;
518 int err;
519 char nodename[PCI_NODENAME_MAX];
521 BUG_ON(!psdev);
522 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
523 psdev->pdev->xdev->otherend_id);
525 again:
526 err = xenbus_transaction_start(&xbt);
527 if (err) {
528 dev_err(&psdev->dev->dev,
529 "error %d when start xenbus transaction\n", err);
530 return;
532 /*PV AER handlers will set this flag*/
533 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
534 err = xenbus_transaction_end(xbt, 0);
535 if (err) {
536 if (err == -EAGAIN)
537 goto again;
538 dev_err(&psdev->dev->dev,
539 "error %d when end xenbus transaction\n", err);
540 return;
544 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
545 * backend need to have cooperation. In xen_pcibk, those steps will do similar
546 * jobs: send service request and waiting for front_end response.
548 static pci_ers_result_t common_process(struct pcistub_device *psdev,
549 pci_channel_state_t state, int aer_cmd,
550 pci_ers_result_t result)
552 pci_ers_result_t res = result;
553 struct xen_pcie_aer_op *aer_op;
554 int ret;
556 /*with PV AER drivers*/
557 aer_op = &(psdev->pdev->sh_info->aer_op);
558 aer_op->cmd = aer_cmd ;
559 /*useful for error_detected callback*/
560 aer_op->err = state;
561 /*pcifront_end BDF*/
562 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
563 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
564 if (!ret) {
565 dev_err(&psdev->dev->dev,
566 DRV_NAME ": failed to get pcifront device\n");
567 return PCI_ERS_RESULT_NONE;
569 wmb();
571 dev_dbg(&psdev->dev->dev,
572 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
573 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
574 /*local flag to mark there's aer request, xen_pcibk callback will use
575 * this flag to judge whether we need to check pci-front give aer
576 * service ack signal
578 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
580 /*It is possible that a pcifront conf_read_write ops request invokes
581 * the callback which cause the spurious execution of wake_up.
582 * Yet it is harmless and better than a spinlock here
584 set_bit(_XEN_PCIB_active,
585 (unsigned long *)&psdev->pdev->sh_info->flags);
586 wmb();
587 notify_remote_via_irq(psdev->pdev->evtchn_irq);
589 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
590 !(test_bit(_XEN_PCIB_active, (unsigned long *)
591 &psdev->pdev->sh_info->flags)), 300*HZ);
593 if (!ret) {
594 if (test_bit(_XEN_PCIB_active,
595 (unsigned long *)&psdev->pdev->sh_info->flags)) {
596 dev_err(&psdev->dev->dev,
597 "pcifront aer process not responding!\n");
598 clear_bit(_XEN_PCIB_active,
599 (unsigned long *)&psdev->pdev->sh_info->flags);
600 aer_op->err = PCI_ERS_RESULT_NONE;
601 return res;
604 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
606 if (test_bit(_XEN_PCIF_active,
607 (unsigned long *)&psdev->pdev->sh_info->flags)) {
608 dev_dbg(&psdev->dev->dev,
609 "schedule pci_conf service in " DRV_NAME "\n");
610 xen_pcibk_test_and_schedule_op(psdev->pdev);
613 res = (pci_ers_result_t)aer_op->err;
614 return res;
618 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
619 * of the device driver could provide this service, and then wait for pcifront
620 * ack.
621 * @dev: pointer to PCI devices
622 * return value is used by aer_core do_recovery policy
624 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
626 struct pcistub_device *psdev;
627 pci_ers_result_t result;
629 result = PCI_ERS_RESULT_RECOVERED;
630 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
631 dev->bus->number, dev->devfn);
633 down_write(&pcistub_sem);
634 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
635 dev->bus->number,
636 PCI_SLOT(dev->devfn),
637 PCI_FUNC(dev->devfn));
639 if (!psdev || !psdev->pdev) {
640 dev_err(&dev->dev,
641 DRV_NAME " device is not found/assigned\n");
642 goto end;
645 if (!psdev->pdev->sh_info) {
646 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
647 " by HVM, kill it\n");
648 kill_domain_by_device(psdev);
649 goto release;
652 if (!test_bit(_XEN_PCIB_AERHANDLER,
653 (unsigned long *)&psdev->pdev->sh_info->flags)) {
654 dev_err(&dev->dev,
655 "guest with no AER driver should have been killed\n");
656 goto release;
658 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
660 if (result == PCI_ERS_RESULT_NONE ||
661 result == PCI_ERS_RESULT_DISCONNECT) {
662 dev_dbg(&dev->dev,
663 "No AER slot_reset service or disconnected!\n");
664 kill_domain_by_device(psdev);
666 release:
667 pcistub_device_put(psdev);
668 end:
669 up_write(&pcistub_sem);
670 return result;
675 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
676 * in case of the device driver could provide this service, and then wait
677 * for pcifront ack
678 * @dev: pointer to PCI devices
679 * return value is used by aer_core do_recovery policy
682 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
684 struct pcistub_device *psdev;
685 pci_ers_result_t result;
687 result = PCI_ERS_RESULT_RECOVERED;
688 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
689 dev->bus->number, dev->devfn);
691 down_write(&pcistub_sem);
692 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
693 dev->bus->number,
694 PCI_SLOT(dev->devfn),
695 PCI_FUNC(dev->devfn));
697 if (!psdev || !psdev->pdev) {
698 dev_err(&dev->dev,
699 DRV_NAME " device is not found/assigned\n");
700 goto end;
703 if (!psdev->pdev->sh_info) {
704 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
705 " by HVM, kill it\n");
706 kill_domain_by_device(psdev);
707 goto release;
710 if (!test_bit(_XEN_PCIB_AERHANDLER,
711 (unsigned long *)&psdev->pdev->sh_info->flags)) {
712 dev_err(&dev->dev,
713 "guest with no AER driver should have been killed\n");
714 goto release;
716 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
718 if (result == PCI_ERS_RESULT_NONE ||
719 result == PCI_ERS_RESULT_DISCONNECT) {
720 dev_dbg(&dev->dev,
721 "No AER mmio_enabled service or disconnected!\n");
722 kill_domain_by_device(psdev);
724 release:
725 pcistub_device_put(psdev);
726 end:
727 up_write(&pcistub_sem);
728 return result;
731 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
732 * in case of the device driver could provide this service, and then wait
733 * for pcifront ack.
734 * @dev: pointer to PCI devices
735 * @error: the current PCI connection state
736 * return value is used by aer_core do_recovery policy
739 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
740 pci_channel_state_t error)
742 struct pcistub_device *psdev;
743 pci_ers_result_t result;
745 result = PCI_ERS_RESULT_CAN_RECOVER;
746 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
747 dev->bus->number, dev->devfn);
749 down_write(&pcistub_sem);
750 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
751 dev->bus->number,
752 PCI_SLOT(dev->devfn),
753 PCI_FUNC(dev->devfn));
755 if (!psdev || !psdev->pdev) {
756 dev_err(&dev->dev,
757 DRV_NAME " device is not found/assigned\n");
758 goto end;
761 if (!psdev->pdev->sh_info) {
762 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
763 " by HVM, kill it\n");
764 kill_domain_by_device(psdev);
765 goto release;
768 /*Guest owns the device yet no aer handler regiested, kill guest*/
769 if (!test_bit(_XEN_PCIB_AERHANDLER,
770 (unsigned long *)&psdev->pdev->sh_info->flags)) {
771 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
772 kill_domain_by_device(psdev);
773 goto release;
775 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
777 if (result == PCI_ERS_RESULT_NONE ||
778 result == PCI_ERS_RESULT_DISCONNECT) {
779 dev_dbg(&dev->dev,
780 "No AER error_detected service or disconnected!\n");
781 kill_domain_by_device(psdev);
783 release:
784 pcistub_device_put(psdev);
785 end:
786 up_write(&pcistub_sem);
787 return result;
790 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
791 * in case of the device driver could provide this service, and then wait
792 * for pcifront ack.
793 * @dev: pointer to PCI devices
796 static void xen_pcibk_error_resume(struct pci_dev *dev)
798 struct pcistub_device *psdev;
800 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
801 dev->bus->number, dev->devfn);
803 down_write(&pcistub_sem);
804 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
805 dev->bus->number,
806 PCI_SLOT(dev->devfn),
807 PCI_FUNC(dev->devfn));
809 if (!psdev || !psdev->pdev) {
810 dev_err(&dev->dev,
811 DRV_NAME " device is not found/assigned\n");
812 goto end;
815 if (!psdev->pdev->sh_info) {
816 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
817 " by HVM, kill it\n");
818 kill_domain_by_device(psdev);
819 goto release;
822 if (!test_bit(_XEN_PCIB_AERHANDLER,
823 (unsigned long *)&psdev->pdev->sh_info->flags)) {
824 dev_err(&dev->dev,
825 "guest with no AER driver should have been killed\n");
826 kill_domain_by_device(psdev);
827 goto release;
829 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
830 PCI_ERS_RESULT_RECOVERED);
831 release:
832 pcistub_device_put(psdev);
833 end:
834 up_write(&pcistub_sem);
835 return;
838 /*add xen_pcibk AER handling*/
839 static struct pci_error_handlers xen_pcibk_error_handler = {
840 .error_detected = xen_pcibk_error_detected,
841 .mmio_enabled = xen_pcibk_mmio_enabled,
842 .slot_reset = xen_pcibk_slot_reset,
843 .resume = xen_pcibk_error_resume,
847 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
848 * for a normal device. I don't want it to be loaded automatically.
851 static struct pci_driver xen_pcibk_pci_driver = {
852 /* The name should be xen_pciback, but until the tools are updated
853 * we will keep it as pciback. */
854 .name = "pciback",
855 .id_table = pcistub_ids,
856 .probe = pcistub_probe,
857 .remove = pcistub_remove,
858 .err_handler = &xen_pcibk_error_handler,
861 static inline int str_to_slot(const char *buf, int *domain, int *bus,
862 int *slot, int *func)
864 int err;
866 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
867 if (err == 4)
868 return 0;
869 else if (err < 0)
870 return -EINVAL;
872 /* try again without domain */
873 *domain = 0;
874 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
875 if (err == 3)
876 return 0;
878 return -EINVAL;
881 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
882 *slot, int *func, int *reg, int *size, int *mask)
884 int err;
886 err =
887 sscanf(buf, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot,
888 func, reg, size, mask);
889 if (err == 7)
890 return 0;
891 return -EINVAL;
894 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
896 struct pcistub_device_id *pci_dev_id;
897 unsigned long flags;
899 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
900 if (!pci_dev_id)
901 return -ENOMEM;
903 pci_dev_id->domain = domain;
904 pci_dev_id->bus = bus;
905 pci_dev_id->devfn = PCI_DEVFN(slot, func);
907 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
908 domain, bus, slot, func);
910 spin_lock_irqsave(&device_ids_lock, flags);
911 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
912 spin_unlock_irqrestore(&device_ids_lock, flags);
914 return 0;
917 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
919 struct pcistub_device_id *pci_dev_id, *t;
920 int devfn = PCI_DEVFN(slot, func);
921 int err = -ENOENT;
922 unsigned long flags;
924 spin_lock_irqsave(&device_ids_lock, flags);
925 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
926 slot_list) {
927 if (pci_dev_id->domain == domain
928 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
929 /* Don't break; here because it's possible the same
930 * slot could be in the list more than once
932 list_del(&pci_dev_id->slot_list);
933 kfree(pci_dev_id);
935 err = 0;
937 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
938 "seize list\n", domain, bus, slot, func);
941 spin_unlock_irqrestore(&device_ids_lock, flags);
943 return err;
946 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
947 int size, int mask)
949 int err = 0;
950 struct pcistub_device *psdev;
951 struct pci_dev *dev;
952 struct config_field *field;
954 psdev = pcistub_device_find(domain, bus, slot, func);
955 if (!psdev || !psdev->dev) {
956 err = -ENODEV;
957 goto out;
959 dev = psdev->dev;
961 field = kzalloc(sizeof(*field), GFP_ATOMIC);
962 if (!field) {
963 err = -ENOMEM;
964 goto out;
967 field->offset = reg;
968 field->size = size;
969 field->mask = mask;
970 field->init = NULL;
971 field->reset = NULL;
972 field->release = NULL;
973 field->clean = xen_pcibk_config_field_free;
975 err = xen_pcibk_config_quirks_add_field(dev, field);
976 if (err)
977 kfree(field);
978 out:
979 return err;
982 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
983 size_t count)
985 int domain, bus, slot, func;
986 int err;
988 err = str_to_slot(buf, &domain, &bus, &slot, &func);
989 if (err)
990 goto out;
992 err = pcistub_device_id_add(domain, bus, slot, func);
994 out:
995 if (!err)
996 err = count;
997 return err;
999 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
1001 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1002 size_t count)
1004 int domain, bus, slot, func;
1005 int err;
1007 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1008 if (err)
1009 goto out;
1011 err = pcistub_device_id_remove(domain, bus, slot, func);
1013 out:
1014 if (!err)
1015 err = count;
1016 return err;
1018 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1020 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1022 struct pcistub_device_id *pci_dev_id;
1023 size_t count = 0;
1024 unsigned long flags;
1026 spin_lock_irqsave(&device_ids_lock, flags);
1027 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1028 if (count >= PAGE_SIZE)
1029 break;
1031 count += scnprintf(buf + count, PAGE_SIZE - count,
1032 "%04x:%02x:%02x.%d\n",
1033 pci_dev_id->domain, pci_dev_id->bus,
1034 PCI_SLOT(pci_dev_id->devfn),
1035 PCI_FUNC(pci_dev_id->devfn));
1037 spin_unlock_irqrestore(&device_ids_lock, flags);
1039 return count;
1041 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1043 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1045 struct pcistub_device *psdev;
1046 struct xen_pcibk_dev_data *dev_data;
1047 size_t count = 0;
1048 unsigned long flags;
1050 spin_lock_irqsave(&pcistub_devices_lock, flags);
1051 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1052 if (count >= PAGE_SIZE)
1053 break;
1054 if (!psdev->dev)
1055 continue;
1056 dev_data = pci_get_drvdata(psdev->dev);
1057 if (!dev_data)
1058 continue;
1059 count +=
1060 scnprintf(buf + count, PAGE_SIZE - count,
1061 "%s:%s:%sing:%ld\n",
1062 pci_name(psdev->dev),
1063 dev_data->isr_on ? "on" : "off",
1064 dev_data->ack_intr ? "ack" : "not ack",
1065 dev_data->handled);
1067 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1068 return count;
1070 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1072 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1073 const char *buf,
1074 size_t count)
1076 struct pcistub_device *psdev;
1077 struct xen_pcibk_dev_data *dev_data;
1078 int domain, bus, slot, func;
1079 int err = -ENOENT;
1081 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1082 if (err)
1083 goto out;
1085 psdev = pcistub_device_find(domain, bus, slot, func);
1087 if (!psdev)
1088 goto out;
1090 dev_data = pci_get_drvdata(psdev->dev);
1091 if (!dev_data)
1092 goto out;
1094 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1095 dev_data->irq_name, dev_data->isr_on,
1096 !dev_data->isr_on);
1098 dev_data->isr_on = !(dev_data->isr_on);
1099 if (dev_data->isr_on)
1100 dev_data->ack_intr = 1;
1101 out:
1102 if (!err)
1103 err = count;
1104 return err;
1106 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1107 pcistub_irq_handler_switch);
1109 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1110 size_t count)
1112 int domain, bus, slot, func, reg, size, mask;
1113 int err;
1115 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1116 &mask);
1117 if (err)
1118 goto out;
1120 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1122 out:
1123 if (!err)
1124 err = count;
1125 return err;
1128 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1130 int count = 0;
1131 unsigned long flags;
1132 struct xen_pcibk_config_quirk *quirk;
1133 struct xen_pcibk_dev_data *dev_data;
1134 const struct config_field *field;
1135 const struct config_field_entry *cfg_entry;
1137 spin_lock_irqsave(&device_ids_lock, flags);
1138 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1139 if (count >= PAGE_SIZE)
1140 goto out;
1142 count += scnprintf(buf + count, PAGE_SIZE - count,
1143 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1144 quirk->pdev->bus->number,
1145 PCI_SLOT(quirk->pdev->devfn),
1146 PCI_FUNC(quirk->pdev->devfn),
1147 quirk->devid.vendor, quirk->devid.device,
1148 quirk->devid.subvendor,
1149 quirk->devid.subdevice);
1151 dev_data = pci_get_drvdata(quirk->pdev);
1153 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1154 field = cfg_entry->field;
1155 if (count >= PAGE_SIZE)
1156 goto out;
1158 count += scnprintf(buf + count, PAGE_SIZE - count,
1159 "\t\t%08x:%01x:%08x\n",
1160 cfg_entry->base_offset +
1161 field->offset, field->size,
1162 field->mask);
1166 out:
1167 spin_unlock_irqrestore(&device_ids_lock, flags);
1169 return count;
1171 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1172 pcistub_quirk_add);
1174 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1175 size_t count)
1177 int domain, bus, slot, func;
1178 int err;
1179 struct pcistub_device *psdev;
1180 struct xen_pcibk_dev_data *dev_data;
1181 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1182 if (err)
1183 goto out;
1184 psdev = pcistub_device_find(domain, bus, slot, func);
1185 if (!psdev) {
1186 err = -ENODEV;
1187 goto out;
1189 if (!psdev->dev) {
1190 err = -ENODEV;
1191 goto release;
1193 dev_data = pci_get_drvdata(psdev->dev);
1194 /* the driver data for a device should never be null at this point */
1195 if (!dev_data) {
1196 err = -ENXIO;
1197 goto release;
1199 if (!dev_data->permissive) {
1200 dev_data->permissive = 1;
1201 /* Let user know that what they're doing could be unsafe */
1202 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1203 "configuration space accesses!\n");
1204 dev_warn(&psdev->dev->dev,
1205 "permissive mode is potentially unsafe!\n");
1207 release:
1208 pcistub_device_put(psdev);
1209 out:
1210 if (!err)
1211 err = count;
1212 return err;
1215 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1217 struct pcistub_device *psdev;
1218 struct xen_pcibk_dev_data *dev_data;
1219 size_t count = 0;
1220 unsigned long flags;
1221 spin_lock_irqsave(&pcistub_devices_lock, flags);
1222 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1223 if (count >= PAGE_SIZE)
1224 break;
1225 if (!psdev->dev)
1226 continue;
1227 dev_data = pci_get_drvdata(psdev->dev);
1228 if (!dev_data || !dev_data->permissive)
1229 continue;
1230 count +=
1231 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1232 pci_name(psdev->dev));
1234 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1235 return count;
1237 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1238 permissive_add);
1240 static void pcistub_exit(void)
1242 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1243 driver_remove_file(&xen_pcibk_pci_driver.driver,
1244 &driver_attr_remove_slot);
1245 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1246 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1247 driver_remove_file(&xen_pcibk_pci_driver.driver,
1248 &driver_attr_permissive);
1249 driver_remove_file(&xen_pcibk_pci_driver.driver,
1250 &driver_attr_irq_handlers);
1251 driver_remove_file(&xen_pcibk_pci_driver.driver,
1252 &driver_attr_irq_handler_state);
1253 pci_unregister_driver(&xen_pcibk_pci_driver);
1256 static int __init pcistub_init(void)
1258 int pos = 0;
1259 int err = 0;
1260 int domain, bus, slot, func;
1261 int parsed;
1263 if (pci_devs_to_hide && *pci_devs_to_hide) {
1264 do {
1265 parsed = 0;
1267 err = sscanf(pci_devs_to_hide + pos,
1268 " (%x:%x:%x.%x) %n",
1269 &domain, &bus, &slot, &func, &parsed);
1270 if (err != 4) {
1271 domain = 0;
1272 err = sscanf(pci_devs_to_hide + pos,
1273 " (%x:%x.%x) %n",
1274 &bus, &slot, &func, &parsed);
1275 if (err != 3)
1276 goto parse_error;
1279 err = pcistub_device_id_add(domain, bus, slot, func);
1280 if (err)
1281 goto out;
1283 /* if parsed<=0, we've reached the end of the string */
1284 pos += parsed;
1285 } while (parsed > 0 && pci_devs_to_hide[pos]);
1288 /* If we're the first PCI Device Driver to register, we're the
1289 * first one to get offered PCI devices as they become
1290 * available (and thus we can be the first to grab them)
1292 err = pci_register_driver(&xen_pcibk_pci_driver);
1293 if (err < 0)
1294 goto out;
1296 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1297 &driver_attr_new_slot);
1298 if (!err)
1299 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1300 &driver_attr_remove_slot);
1301 if (!err)
1302 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1303 &driver_attr_slots);
1304 if (!err)
1305 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1306 &driver_attr_quirks);
1307 if (!err)
1308 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1309 &driver_attr_permissive);
1311 if (!err)
1312 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1313 &driver_attr_irq_handlers);
1314 if (!err)
1315 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1316 &driver_attr_irq_handler_state);
1317 if (err)
1318 pcistub_exit();
1320 out:
1321 return err;
1323 parse_error:
1324 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
1325 pci_devs_to_hide + pos);
1326 return -EINVAL;
1329 #ifndef MODULE
1331 * fs_initcall happens before device_initcall
1332 * so xen_pcibk *should* get called first (b/c we
1333 * want to suck up any device before other drivers
1334 * get a chance by being the first pci device
1335 * driver to register)
1337 fs_initcall(pcistub_init);
1338 #endif
1340 static int __init xen_pcibk_init(void)
1342 int err;
1344 if (!xen_initial_domain())
1345 return -ENODEV;
1347 err = xen_pcibk_config_init();
1348 if (err)
1349 return err;
1351 #ifdef MODULE
1352 err = pcistub_init();
1353 if (err < 0)
1354 return err;
1355 #endif
1357 pcistub_init_devices_late();
1358 err = xen_pcibk_xenbus_register();
1359 if (err)
1360 pcistub_exit();
1362 return err;
1365 static void __exit xen_pcibk_cleanup(void)
1367 xen_pcibk_xenbus_unregister();
1368 pcistub_exit();
1371 module_init(xen_pcibk_init);
1372 module_exit(xen_pcibk_cleanup);
1374 MODULE_LICENSE("Dual BSD/GPL");
1375 MODULE_ALIAS("xen-backend:pci");