USB: serial: fix whitespace issues
[zen-stable.git] / drivers / xen / xen-pciback / pci_stub.c
blob8f06e1ed028c9ab737ca61136754ae2c707d43e2
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 pci_dev_put(psdev->dev);
104 kfree(psdev);
107 static inline void pcistub_device_get(struct pcistub_device *psdev)
109 kref_get(&psdev->kref);
112 static inline void pcistub_device_put(struct pcistub_device *psdev)
114 kref_put(&psdev->kref, pcistub_device_release);
117 static struct pcistub_device *pcistub_device_find(int domain, int bus,
118 int slot, int func)
120 struct pcistub_device *psdev = NULL;
121 unsigned long flags;
123 spin_lock_irqsave(&pcistub_devices_lock, flags);
125 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
126 if (psdev->dev != NULL
127 && domain == pci_domain_nr(psdev->dev->bus)
128 && bus == psdev->dev->bus->number
129 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
130 pcistub_device_get(psdev);
131 goto out;
135 /* didn't find it */
136 psdev = NULL;
138 out:
139 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
140 return psdev;
143 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
144 struct pcistub_device *psdev)
146 struct pci_dev *pci_dev = NULL;
147 unsigned long flags;
149 pcistub_device_get(psdev);
151 spin_lock_irqsave(&psdev->lock, flags);
152 if (!psdev->pdev) {
153 psdev->pdev = pdev;
154 pci_dev = psdev->dev;
156 spin_unlock_irqrestore(&psdev->lock, flags);
158 if (!pci_dev)
159 pcistub_device_put(psdev);
161 return pci_dev;
164 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
165 int domain, int bus,
166 int slot, int func)
168 struct pcistub_device *psdev;
169 struct pci_dev *found_dev = NULL;
170 unsigned long flags;
172 spin_lock_irqsave(&pcistub_devices_lock, flags);
174 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
175 if (psdev->dev != NULL
176 && domain == pci_domain_nr(psdev->dev->bus)
177 && bus == psdev->dev->bus->number
178 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
179 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
180 break;
184 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
185 return found_dev;
188 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
189 struct pci_dev *dev)
191 struct pcistub_device *psdev;
192 struct pci_dev *found_dev = NULL;
193 unsigned long flags;
195 spin_lock_irqsave(&pcistub_devices_lock, flags);
197 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
198 if (psdev->dev == dev) {
199 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
200 break;
204 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
205 return found_dev;
208 void pcistub_put_pci_dev(struct pci_dev *dev)
210 struct pcistub_device *psdev, *found_psdev = NULL;
211 unsigned long flags;
213 spin_lock_irqsave(&pcistub_devices_lock, flags);
215 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
216 if (psdev->dev == dev) {
217 found_psdev = psdev;
218 break;
222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
223 if (WARN_ON(!found_psdev))
224 return;
226 /*hold this lock for avoiding breaking link between
227 * pcistub and xen_pcibk when AER is in processing
229 down_write(&pcistub_sem);
230 /* Cleanup our device
231 * (so it's ready for the next domain)
233 xen_pcibk_reset_device(found_psdev->dev);
234 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
235 xen_pcibk_config_reset_dev(found_psdev->dev);
237 spin_lock_irqsave(&found_psdev->lock, flags);
238 found_psdev->pdev = NULL;
239 spin_unlock_irqrestore(&found_psdev->lock, flags);
241 pcistub_device_put(found_psdev);
242 up_write(&pcistub_sem);
245 static int __devinit pcistub_match_one(struct pci_dev *dev,
246 struct pcistub_device_id *pdev_id)
248 /* Match the specified device by domain, bus, slot, func and also if
249 * any of the device's parent bridges match.
251 for (; dev != NULL; dev = dev->bus->self) {
252 if (pci_domain_nr(dev->bus) == pdev_id->domain
253 && dev->bus->number == pdev_id->bus
254 && dev->devfn == pdev_id->devfn)
255 return 1;
257 /* Sometimes topmost bridge links to itself. */
258 if (dev == dev->bus->self)
259 break;
262 return 0;
265 static int __devinit pcistub_match(struct pci_dev *dev)
267 struct pcistub_device_id *pdev_id;
268 unsigned long flags;
269 int found = 0;
271 spin_lock_irqsave(&device_ids_lock, flags);
272 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
273 if (pcistub_match_one(dev, pdev_id)) {
274 found = 1;
275 break;
278 spin_unlock_irqrestore(&device_ids_lock, flags);
280 return found;
283 static int __devinit pcistub_init_device(struct pci_dev *dev)
285 struct xen_pcibk_dev_data *dev_data;
286 int err = 0;
288 dev_dbg(&dev->dev, "initializing...\n");
290 /* The PCI backend is not intended to be a module (or to work with
291 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
292 * would need to be called somewhere to free the memory allocated
293 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
295 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
296 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
297 if (!dev_data) {
298 err = -ENOMEM;
299 goto out;
301 pci_set_drvdata(dev, dev_data);
304 * Setup name for fake IRQ handler. It will only be enabled
305 * once the device is turned on by the guest.
307 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
309 dev_dbg(&dev->dev, "initializing config\n");
311 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
312 err = xen_pcibk_config_init_dev(dev);
313 if (err)
314 goto out;
316 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
317 * must do this here because pcibios_enable_device may specify
318 * the pci device's true irq (and possibly its other resources)
319 * if they differ from what's in the configuration space.
320 * This makes the assumption that the device's resources won't
321 * change after this point (otherwise this code may break!)
323 dev_dbg(&dev->dev, "enabling device\n");
324 err = pci_enable_device(dev);
325 if (err)
326 goto config_release;
328 /* Now disable the device (this also ensures some private device
329 * data is setup before we export)
331 dev_dbg(&dev->dev, "reset device\n");
332 xen_pcibk_reset_device(dev);
334 return 0;
336 config_release:
337 xen_pcibk_config_free_dev(dev);
339 out:
340 pci_set_drvdata(dev, NULL);
341 kfree(dev_data);
342 return err;
346 * Because some initialization still happens on
347 * devices during fs_initcall, we need to defer
348 * full initialization of our devices until
349 * device_initcall.
351 static int __init pcistub_init_devices_late(void)
353 struct pcistub_device *psdev;
354 unsigned long flags;
355 int err = 0;
357 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
359 spin_lock_irqsave(&pcistub_devices_lock, flags);
361 while (!list_empty(&seized_devices)) {
362 psdev = container_of(seized_devices.next,
363 struct pcistub_device, dev_list);
364 list_del(&psdev->dev_list);
366 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
368 err = pcistub_init_device(psdev->dev);
369 if (err) {
370 dev_err(&psdev->dev->dev,
371 "error %d initializing device\n", err);
372 kfree(psdev);
373 psdev = NULL;
376 spin_lock_irqsave(&pcistub_devices_lock, flags);
378 if (psdev)
379 list_add_tail(&psdev->dev_list, &pcistub_devices);
382 initialize_devices = 1;
384 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
386 return 0;
389 static int __devinit pcistub_seize(struct pci_dev *dev)
391 struct pcistub_device *psdev;
392 unsigned long flags;
393 int err = 0;
395 psdev = pcistub_device_alloc(dev);
396 if (!psdev)
397 return -ENOMEM;
399 spin_lock_irqsave(&pcistub_devices_lock, flags);
401 if (initialize_devices) {
402 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
404 /* don't want irqs disabled when calling pcistub_init_device */
405 err = pcistub_init_device(psdev->dev);
407 spin_lock_irqsave(&pcistub_devices_lock, flags);
409 if (!err)
410 list_add(&psdev->dev_list, &pcistub_devices);
411 } else {
412 dev_dbg(&dev->dev, "deferring initialization\n");
413 list_add(&psdev->dev_list, &seized_devices);
416 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
418 if (err)
419 pcistub_device_put(psdev);
421 return err;
424 static int __devinit pcistub_probe(struct pci_dev *dev,
425 const struct pci_device_id *id)
427 int err = 0;
429 dev_dbg(&dev->dev, "probing...\n");
431 if (pcistub_match(dev)) {
433 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
434 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
435 dev_err(&dev->dev, "can't export pci devices that "
436 "don't have a normal (0) or bridge (1) "
437 "header type!\n");
438 err = -ENODEV;
439 goto out;
442 dev_info(&dev->dev, "seizing device\n");
443 err = pcistub_seize(dev);
444 } else
445 /* Didn't find the device */
446 err = -ENODEV;
448 out:
449 return err;
452 static void pcistub_remove(struct pci_dev *dev)
454 struct pcistub_device *psdev, *found_psdev = NULL;
455 unsigned long flags;
457 dev_dbg(&dev->dev, "removing\n");
459 spin_lock_irqsave(&pcistub_devices_lock, flags);
461 xen_pcibk_config_quirk_release(dev);
463 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
464 if (psdev->dev == dev) {
465 found_psdev = psdev;
466 break;
470 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
472 if (found_psdev) {
473 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
474 found_psdev->pdev);
476 if (found_psdev->pdev) {
477 printk(KERN_WARNING DRV_NAME ": ****** removing device "
478 "%s while still in-use! ******\n",
479 pci_name(found_psdev->dev));
480 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
481 " still access this device's i/o resources!\n");
482 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
483 "domain before binding device\n");
484 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
485 "or domains\n");
487 xen_pcibk_release_pci_dev(found_psdev->pdev,
488 found_psdev->dev);
491 spin_lock_irqsave(&pcistub_devices_lock, flags);
492 list_del(&found_psdev->dev_list);
493 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
495 /* the final put for releasing from the list */
496 pcistub_device_put(found_psdev);
500 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
502 .vendor = PCI_ANY_ID,
503 .device = PCI_ANY_ID,
504 .subvendor = PCI_ANY_ID,
505 .subdevice = PCI_ANY_ID,
507 {0,},
510 #define PCI_NODENAME_MAX 40
511 static void kill_domain_by_device(struct pcistub_device *psdev)
513 struct xenbus_transaction xbt;
514 int err;
515 char nodename[PCI_NODENAME_MAX];
517 BUG_ON(!psdev);
518 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
519 psdev->pdev->xdev->otherend_id);
521 again:
522 err = xenbus_transaction_start(&xbt);
523 if (err) {
524 dev_err(&psdev->dev->dev,
525 "error %d when start xenbus transaction\n", err);
526 return;
528 /*PV AER handlers will set this flag*/
529 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
530 err = xenbus_transaction_end(xbt, 0);
531 if (err) {
532 if (err == -EAGAIN)
533 goto again;
534 dev_err(&psdev->dev->dev,
535 "error %d when end xenbus transaction\n", err);
536 return;
540 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
541 * backend need to have cooperation. In xen_pcibk, those steps will do similar
542 * jobs: send service request and waiting for front_end response.
544 static pci_ers_result_t common_process(struct pcistub_device *psdev,
545 pci_channel_state_t state, int aer_cmd,
546 pci_ers_result_t result)
548 pci_ers_result_t res = result;
549 struct xen_pcie_aer_op *aer_op;
550 int ret;
552 /*with PV AER drivers*/
553 aer_op = &(psdev->pdev->sh_info->aer_op);
554 aer_op->cmd = aer_cmd ;
555 /*useful for error_detected callback*/
556 aer_op->err = state;
557 /*pcifront_end BDF*/
558 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
559 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
560 if (!ret) {
561 dev_err(&psdev->dev->dev,
562 DRV_NAME ": failed to get pcifront device\n");
563 return PCI_ERS_RESULT_NONE;
565 wmb();
567 dev_dbg(&psdev->dev->dev,
568 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
569 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
570 /*local flag to mark there's aer request, xen_pcibk callback will use
571 * this flag to judge whether we need to check pci-front give aer
572 * service ack signal
574 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
576 /*It is possible that a pcifront conf_read_write ops request invokes
577 * the callback which cause the spurious execution of wake_up.
578 * Yet it is harmless and better than a spinlock here
580 set_bit(_XEN_PCIB_active,
581 (unsigned long *)&psdev->pdev->sh_info->flags);
582 wmb();
583 notify_remote_via_irq(psdev->pdev->evtchn_irq);
585 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
586 !(test_bit(_XEN_PCIB_active, (unsigned long *)
587 &psdev->pdev->sh_info->flags)), 300*HZ);
589 if (!ret) {
590 if (test_bit(_XEN_PCIB_active,
591 (unsigned long *)&psdev->pdev->sh_info->flags)) {
592 dev_err(&psdev->dev->dev,
593 "pcifront aer process not responding!\n");
594 clear_bit(_XEN_PCIB_active,
595 (unsigned long *)&psdev->pdev->sh_info->flags);
596 aer_op->err = PCI_ERS_RESULT_NONE;
597 return res;
600 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
602 if (test_bit(_XEN_PCIF_active,
603 (unsigned long *)&psdev->pdev->sh_info->flags)) {
604 dev_dbg(&psdev->dev->dev,
605 "schedule pci_conf service in " DRV_NAME "\n");
606 xen_pcibk_test_and_schedule_op(psdev->pdev);
609 res = (pci_ers_result_t)aer_op->err;
610 return res;
614 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
615 * of the device driver could provide this service, and then wait for pcifront
616 * ack.
617 * @dev: pointer to PCI devices
618 * return value is used by aer_core do_recovery policy
620 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
622 struct pcistub_device *psdev;
623 pci_ers_result_t result;
625 result = PCI_ERS_RESULT_RECOVERED;
626 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
627 dev->bus->number, dev->devfn);
629 down_write(&pcistub_sem);
630 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
631 dev->bus->number,
632 PCI_SLOT(dev->devfn),
633 PCI_FUNC(dev->devfn));
635 if (!psdev || !psdev->pdev) {
636 dev_err(&dev->dev,
637 DRV_NAME " device is not found/assigned\n");
638 goto end;
641 if (!psdev->pdev->sh_info) {
642 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
643 " by HVM, kill it\n");
644 kill_domain_by_device(psdev);
645 goto release;
648 if (!test_bit(_XEN_PCIB_AERHANDLER,
649 (unsigned long *)&psdev->pdev->sh_info->flags)) {
650 dev_err(&dev->dev,
651 "guest with no AER driver should have been killed\n");
652 goto release;
654 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
656 if (result == PCI_ERS_RESULT_NONE ||
657 result == PCI_ERS_RESULT_DISCONNECT) {
658 dev_dbg(&dev->dev,
659 "No AER slot_reset service or disconnected!\n");
660 kill_domain_by_device(psdev);
662 release:
663 pcistub_device_put(psdev);
664 end:
665 up_write(&pcistub_sem);
666 return result;
671 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
672 * in case of the device driver could provide this service, and then wait
673 * for pcifront ack
674 * @dev: pointer to PCI devices
675 * return value is used by aer_core do_recovery policy
678 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
680 struct pcistub_device *psdev;
681 pci_ers_result_t result;
683 result = PCI_ERS_RESULT_RECOVERED;
684 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
685 dev->bus->number, dev->devfn);
687 down_write(&pcistub_sem);
688 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
689 dev->bus->number,
690 PCI_SLOT(dev->devfn),
691 PCI_FUNC(dev->devfn));
693 if (!psdev || !psdev->pdev) {
694 dev_err(&dev->dev,
695 DRV_NAME " device is not found/assigned\n");
696 goto end;
699 if (!psdev->pdev->sh_info) {
700 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
701 " by HVM, kill it\n");
702 kill_domain_by_device(psdev);
703 goto release;
706 if (!test_bit(_XEN_PCIB_AERHANDLER,
707 (unsigned long *)&psdev->pdev->sh_info->flags)) {
708 dev_err(&dev->dev,
709 "guest with no AER driver should have been killed\n");
710 goto release;
712 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
714 if (result == PCI_ERS_RESULT_NONE ||
715 result == PCI_ERS_RESULT_DISCONNECT) {
716 dev_dbg(&dev->dev,
717 "No AER mmio_enabled service or disconnected!\n");
718 kill_domain_by_device(psdev);
720 release:
721 pcistub_device_put(psdev);
722 end:
723 up_write(&pcistub_sem);
724 return result;
727 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
728 * in case of the device driver could provide this service, and then wait
729 * for pcifront ack.
730 * @dev: pointer to PCI devices
731 * @error: the current PCI connection state
732 * return value is used by aer_core do_recovery policy
735 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
736 pci_channel_state_t error)
738 struct pcistub_device *psdev;
739 pci_ers_result_t result;
741 result = PCI_ERS_RESULT_CAN_RECOVER;
742 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
743 dev->bus->number, dev->devfn);
745 down_write(&pcistub_sem);
746 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
747 dev->bus->number,
748 PCI_SLOT(dev->devfn),
749 PCI_FUNC(dev->devfn));
751 if (!psdev || !psdev->pdev) {
752 dev_err(&dev->dev,
753 DRV_NAME " device is not found/assigned\n");
754 goto end;
757 if (!psdev->pdev->sh_info) {
758 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
759 " by HVM, kill it\n");
760 kill_domain_by_device(psdev);
761 goto release;
764 /*Guest owns the device yet no aer handler regiested, kill guest*/
765 if (!test_bit(_XEN_PCIB_AERHANDLER,
766 (unsigned long *)&psdev->pdev->sh_info->flags)) {
767 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
768 kill_domain_by_device(psdev);
769 goto release;
771 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
773 if (result == PCI_ERS_RESULT_NONE ||
774 result == PCI_ERS_RESULT_DISCONNECT) {
775 dev_dbg(&dev->dev,
776 "No AER error_detected service or disconnected!\n");
777 kill_domain_by_device(psdev);
779 release:
780 pcistub_device_put(psdev);
781 end:
782 up_write(&pcistub_sem);
783 return result;
786 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
787 * in case of the device driver could provide this service, and then wait
788 * for pcifront ack.
789 * @dev: pointer to PCI devices
792 static void xen_pcibk_error_resume(struct pci_dev *dev)
794 struct pcistub_device *psdev;
796 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
797 dev->bus->number, dev->devfn);
799 down_write(&pcistub_sem);
800 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
801 dev->bus->number,
802 PCI_SLOT(dev->devfn),
803 PCI_FUNC(dev->devfn));
805 if (!psdev || !psdev->pdev) {
806 dev_err(&dev->dev,
807 DRV_NAME " device is not found/assigned\n");
808 goto end;
811 if (!psdev->pdev->sh_info) {
812 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
813 " by HVM, kill it\n");
814 kill_domain_by_device(psdev);
815 goto release;
818 if (!test_bit(_XEN_PCIB_AERHANDLER,
819 (unsigned long *)&psdev->pdev->sh_info->flags)) {
820 dev_err(&dev->dev,
821 "guest with no AER driver should have been killed\n");
822 kill_domain_by_device(psdev);
823 goto release;
825 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
826 PCI_ERS_RESULT_RECOVERED);
827 release:
828 pcistub_device_put(psdev);
829 end:
830 up_write(&pcistub_sem);
831 return;
834 /*add xen_pcibk AER handling*/
835 static struct pci_error_handlers xen_pcibk_error_handler = {
836 .error_detected = xen_pcibk_error_detected,
837 .mmio_enabled = xen_pcibk_mmio_enabled,
838 .slot_reset = xen_pcibk_slot_reset,
839 .resume = xen_pcibk_error_resume,
843 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
844 * for a normal device. I don't want it to be loaded automatically.
847 static struct pci_driver xen_pcibk_pci_driver = {
848 /* The name should be xen_pciback, but until the tools are updated
849 * we will keep it as pciback. */
850 .name = "pciback",
851 .id_table = pcistub_ids,
852 .probe = pcistub_probe,
853 .remove = pcistub_remove,
854 .err_handler = &xen_pcibk_error_handler,
857 static inline int str_to_slot(const char *buf, int *domain, int *bus,
858 int *slot, int *func)
860 int err;
862 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
863 if (err == 4)
864 return 0;
865 else if (err < 0)
866 return -EINVAL;
868 /* try again without domain */
869 *domain = 0;
870 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
871 if (err == 3)
872 return 0;
874 return -EINVAL;
877 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
878 *slot, int *func, int *reg, int *size, int *mask)
880 int err;
882 err =
883 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
884 func, reg, size, mask);
885 if (err == 7)
886 return 0;
887 return -EINVAL;
890 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
892 struct pcistub_device_id *pci_dev_id;
893 unsigned long flags;
895 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
896 if (!pci_dev_id)
897 return -ENOMEM;
899 pci_dev_id->domain = domain;
900 pci_dev_id->bus = bus;
901 pci_dev_id->devfn = PCI_DEVFN(slot, func);
903 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%01x\n",
904 domain, bus, slot, func);
906 spin_lock_irqsave(&device_ids_lock, flags);
907 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
908 spin_unlock_irqrestore(&device_ids_lock, flags);
910 return 0;
913 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
915 struct pcistub_device_id *pci_dev_id, *t;
916 int devfn = PCI_DEVFN(slot, func);
917 int err = -ENOENT;
918 unsigned long flags;
920 spin_lock_irqsave(&device_ids_lock, flags);
921 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
922 slot_list) {
923 if (pci_dev_id->domain == domain
924 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
925 /* Don't break; here because it's possible the same
926 * slot could be in the list more than once
928 list_del(&pci_dev_id->slot_list);
929 kfree(pci_dev_id);
931 err = 0;
933 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%01x from "
934 "seize list\n", domain, bus, slot, func);
937 spin_unlock_irqrestore(&device_ids_lock, flags);
939 return err;
942 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
943 int size, int mask)
945 int err = 0;
946 struct pcistub_device *psdev;
947 struct pci_dev *dev;
948 struct config_field *field;
950 psdev = pcistub_device_find(domain, bus, slot, func);
951 if (!psdev || !psdev->dev) {
952 err = -ENODEV;
953 goto out;
955 dev = psdev->dev;
957 field = kzalloc(sizeof(*field), GFP_ATOMIC);
958 if (!field) {
959 err = -ENOMEM;
960 goto out;
963 field->offset = reg;
964 field->size = size;
965 field->mask = mask;
966 field->init = NULL;
967 field->reset = NULL;
968 field->release = NULL;
969 field->clean = xen_pcibk_config_field_free;
971 err = xen_pcibk_config_quirks_add_field(dev, field);
972 if (err)
973 kfree(field);
974 out:
975 return err;
978 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
979 size_t count)
981 int domain, bus, slot, func;
982 int err;
984 err = str_to_slot(buf, &domain, &bus, &slot, &func);
985 if (err)
986 goto out;
988 err = pcistub_device_id_add(domain, bus, slot, func);
990 out:
991 if (!err)
992 err = count;
993 return err;
995 static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
997 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
998 size_t count)
1000 int domain, bus, slot, func;
1001 int err;
1003 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1004 if (err)
1005 goto out;
1007 err = pcistub_device_id_remove(domain, bus, slot, func);
1009 out:
1010 if (!err)
1011 err = count;
1012 return err;
1014 static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1016 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1018 struct pcistub_device_id *pci_dev_id;
1019 size_t count = 0;
1020 unsigned long flags;
1022 spin_lock_irqsave(&device_ids_lock, flags);
1023 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1024 if (count >= PAGE_SIZE)
1025 break;
1027 count += scnprintf(buf + count, PAGE_SIZE - count,
1028 "%04x:%02x:%02x.%01x\n",
1029 pci_dev_id->domain, pci_dev_id->bus,
1030 PCI_SLOT(pci_dev_id->devfn),
1031 PCI_FUNC(pci_dev_id->devfn));
1033 spin_unlock_irqrestore(&device_ids_lock, flags);
1035 return count;
1037 static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1039 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1041 struct pcistub_device *psdev;
1042 struct xen_pcibk_dev_data *dev_data;
1043 size_t count = 0;
1044 unsigned long flags;
1046 spin_lock_irqsave(&pcistub_devices_lock, flags);
1047 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1048 if (count >= PAGE_SIZE)
1049 break;
1050 if (!psdev->dev)
1051 continue;
1052 dev_data = pci_get_drvdata(psdev->dev);
1053 if (!dev_data)
1054 continue;
1055 count +=
1056 scnprintf(buf + count, PAGE_SIZE - count,
1057 "%s:%s:%sing:%ld\n",
1058 pci_name(psdev->dev),
1059 dev_data->isr_on ? "on" : "off",
1060 dev_data->ack_intr ? "ack" : "not ack",
1061 dev_data->handled);
1063 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1064 return count;
1066 static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1068 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1069 const char *buf,
1070 size_t count)
1072 struct pcistub_device *psdev;
1073 struct xen_pcibk_dev_data *dev_data;
1074 int domain, bus, slot, func;
1075 int err = -ENOENT;
1077 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1078 if (err)
1079 goto out;
1081 psdev = pcistub_device_find(domain, bus, slot, func);
1083 if (!psdev)
1084 goto out;
1086 dev_data = pci_get_drvdata(psdev->dev);
1087 if (!dev_data)
1088 goto out;
1090 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1091 dev_data->irq_name, dev_data->isr_on,
1092 !dev_data->isr_on);
1094 dev_data->isr_on = !(dev_data->isr_on);
1095 if (dev_data->isr_on)
1096 dev_data->ack_intr = 1;
1097 out:
1098 if (!err)
1099 err = count;
1100 return err;
1102 static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1103 pcistub_irq_handler_switch);
1105 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1106 size_t count)
1108 int domain, bus, slot, func, reg, size, mask;
1109 int err;
1111 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1112 &mask);
1113 if (err)
1114 goto out;
1116 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1118 out:
1119 if (!err)
1120 err = count;
1121 return err;
1124 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1126 int count = 0;
1127 unsigned long flags;
1128 struct xen_pcibk_config_quirk *quirk;
1129 struct xen_pcibk_dev_data *dev_data;
1130 const struct config_field *field;
1131 const struct config_field_entry *cfg_entry;
1133 spin_lock_irqsave(&device_ids_lock, flags);
1134 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1135 if (count >= PAGE_SIZE)
1136 goto out;
1138 count += scnprintf(buf + count, PAGE_SIZE - count,
1139 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1140 quirk->pdev->bus->number,
1141 PCI_SLOT(quirk->pdev->devfn),
1142 PCI_FUNC(quirk->pdev->devfn),
1143 quirk->devid.vendor, quirk->devid.device,
1144 quirk->devid.subvendor,
1145 quirk->devid.subdevice);
1147 dev_data = pci_get_drvdata(quirk->pdev);
1149 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1150 field = cfg_entry->field;
1151 if (count >= PAGE_SIZE)
1152 goto out;
1154 count += scnprintf(buf + count, PAGE_SIZE - count,
1155 "\t\t%08x:%01x:%08x\n",
1156 cfg_entry->base_offset +
1157 field->offset, field->size,
1158 field->mask);
1162 out:
1163 spin_unlock_irqrestore(&device_ids_lock, flags);
1165 return count;
1167 static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1168 pcistub_quirk_add);
1170 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1171 size_t count)
1173 int domain, bus, slot, func;
1174 int err;
1175 struct pcistub_device *psdev;
1176 struct xen_pcibk_dev_data *dev_data;
1177 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1178 if (err)
1179 goto out;
1180 psdev = pcistub_device_find(domain, bus, slot, func);
1181 if (!psdev) {
1182 err = -ENODEV;
1183 goto out;
1185 if (!psdev->dev) {
1186 err = -ENODEV;
1187 goto release;
1189 dev_data = pci_get_drvdata(psdev->dev);
1190 /* the driver data for a device should never be null at this point */
1191 if (!dev_data) {
1192 err = -ENXIO;
1193 goto release;
1195 if (!dev_data->permissive) {
1196 dev_data->permissive = 1;
1197 /* Let user know that what they're doing could be unsafe */
1198 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1199 "configuration space accesses!\n");
1200 dev_warn(&psdev->dev->dev,
1201 "permissive mode is potentially unsafe!\n");
1203 release:
1204 pcistub_device_put(psdev);
1205 out:
1206 if (!err)
1207 err = count;
1208 return err;
1211 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1213 struct pcistub_device *psdev;
1214 struct xen_pcibk_dev_data *dev_data;
1215 size_t count = 0;
1216 unsigned long flags;
1217 spin_lock_irqsave(&pcistub_devices_lock, flags);
1218 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1219 if (count >= PAGE_SIZE)
1220 break;
1221 if (!psdev->dev)
1222 continue;
1223 dev_data = pci_get_drvdata(psdev->dev);
1224 if (!dev_data || !dev_data->permissive)
1225 continue;
1226 count +=
1227 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1228 pci_name(psdev->dev));
1230 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1231 return count;
1233 static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1234 permissive_add);
1236 static void pcistub_exit(void)
1238 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1239 driver_remove_file(&xen_pcibk_pci_driver.driver,
1240 &driver_attr_remove_slot);
1241 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1242 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1243 driver_remove_file(&xen_pcibk_pci_driver.driver,
1244 &driver_attr_permissive);
1245 driver_remove_file(&xen_pcibk_pci_driver.driver,
1246 &driver_attr_irq_handlers);
1247 driver_remove_file(&xen_pcibk_pci_driver.driver,
1248 &driver_attr_irq_handler_state);
1249 pci_unregister_driver(&xen_pcibk_pci_driver);
1252 static int __init pcistub_init(void)
1254 int pos = 0;
1255 int err = 0;
1256 int domain, bus, slot, func;
1257 int parsed;
1259 if (pci_devs_to_hide && *pci_devs_to_hide) {
1260 do {
1261 parsed = 0;
1263 err = sscanf(pci_devs_to_hide + pos,
1264 " (%x:%x:%x.%x) %n",
1265 &domain, &bus, &slot, &func, &parsed);
1266 if (err != 4) {
1267 domain = 0;
1268 err = sscanf(pci_devs_to_hide + pos,
1269 " (%x:%x.%x) %n",
1270 &bus, &slot, &func, &parsed);
1271 if (err != 3)
1272 goto parse_error;
1275 err = pcistub_device_id_add(domain, bus, slot, func);
1276 if (err)
1277 goto out;
1279 /* if parsed<=0, we've reached the end of the string */
1280 pos += parsed;
1281 } while (parsed > 0 && pci_devs_to_hide[pos]);
1284 /* If we're the first PCI Device Driver to register, we're the
1285 * first one to get offered PCI devices as they become
1286 * available (and thus we can be the first to grab them)
1288 err = pci_register_driver(&xen_pcibk_pci_driver);
1289 if (err < 0)
1290 goto out;
1292 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1293 &driver_attr_new_slot);
1294 if (!err)
1295 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1296 &driver_attr_remove_slot);
1297 if (!err)
1298 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1299 &driver_attr_slots);
1300 if (!err)
1301 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1302 &driver_attr_quirks);
1303 if (!err)
1304 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1305 &driver_attr_permissive);
1307 if (!err)
1308 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1309 &driver_attr_irq_handlers);
1310 if (!err)
1311 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1312 &driver_attr_irq_handler_state);
1313 if (err)
1314 pcistub_exit();
1316 out:
1317 return err;
1319 parse_error:
1320 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
1321 pci_devs_to_hide + pos);
1322 return -EINVAL;
1325 #ifndef MODULE
1327 * fs_initcall happens before device_initcall
1328 * so xen_pcibk *should* get called first (b/c we
1329 * want to suck up any device before other drivers
1330 * get a chance by being the first pci device
1331 * driver to register)
1333 fs_initcall(pcistub_init);
1334 #endif
1336 static int __init xen_pcibk_init(void)
1338 int err;
1340 if (!xen_initial_domain())
1341 return -ENODEV;
1343 err = xen_pcibk_config_init();
1344 if (err)
1345 return err;
1347 #ifdef MODULE
1348 err = pcistub_init();
1349 if (err < 0)
1350 return err;
1351 #endif
1353 pcistub_init_devices_late();
1354 err = xen_pcibk_xenbus_register();
1355 if (err)
1356 pcistub_exit();
1358 return err;
1361 static void __exit xen_pcibk_cleanup(void)
1363 xen_pcibk_xenbus_unregister();
1364 pcistub_exit();
1367 module_init(xen_pcibk_init);
1368 module_exit(xen_pcibk_cleanup);
1370 MODULE_LICENSE("Dual BSD/GPL");
1371 MODULE_ALIAS("xen-backend:pci");