treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / xen / xen-pciback / pci_stub.c
blob097410a7cdb74d83226a86277eb047a693cbe0bc
1 /*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
24 #include "pciback.h"
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
28 #define PCISTUB_DRIVER_NAME "pciback"
30 static char *pci_devs_to_hide;
31 wait_queue_head_t xen_pcibk_aer_wait_queue;
32 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
35 static DECLARE_RWSEM(pcistub_sem);
36 module_param_named(hide, pci_devs_to_hide, charp, 0444);
38 struct pcistub_device_id {
39 struct list_head slot_list;
40 int domain;
41 unsigned char bus;
42 unsigned int devfn;
44 static LIST_HEAD(pcistub_device_ids);
45 static DEFINE_SPINLOCK(device_ids_lock);
47 struct pcistub_device {
48 struct kref kref;
49 struct list_head dev_list;
50 spinlock_t lock;
52 struct pci_dev *dev;
53 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
56 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
57 * flag must be locked with pcistub_devices_lock
59 static DEFINE_SPINLOCK(pcistub_devices_lock);
60 static LIST_HEAD(pcistub_devices);
62 /* wait for device_initcall before initializing our devices
63 * (see pcistub_init_devices_late)
65 static int initialize_devices;
66 static LIST_HEAD(seized_devices);
68 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
70 struct pcistub_device *psdev;
72 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
74 psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
75 if (!psdev)
76 return NULL;
78 psdev->dev = pci_dev_get(dev);
79 if (!psdev->dev) {
80 kfree(psdev);
81 return NULL;
84 kref_init(&psdev->kref);
85 spin_lock_init(&psdev->lock);
87 return psdev;
90 /* Don't call this directly as it's called by pcistub_device_put */
91 static void pcistub_device_release(struct kref *kref)
93 struct pcistub_device *psdev;
94 struct pci_dev *dev;
95 struct xen_pcibk_dev_data *dev_data;
97 psdev = container_of(kref, struct pcistub_device, kref);
98 dev = psdev->dev;
99 dev_data = pci_get_drvdata(dev);
101 dev_dbg(&dev->dev, "pcistub_device_release\n");
103 xen_unregister_device_domain_owner(dev);
105 /* Call the reset function which does not take lock as this
106 * is called from "unbind" which takes a device_lock mutex.
108 __pci_reset_function_locked(dev);
109 if (dev_data &&
110 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
111 dev_info(&dev->dev, "Could not reload PCI state\n");
112 else
113 pci_restore_state(dev);
115 if (dev->msix_cap) {
116 struct physdev_pci_device ppdev = {
117 .seg = pci_domain_nr(dev->bus),
118 .bus = dev->bus->number,
119 .devfn = dev->devfn
121 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
122 &ppdev);
124 if (err && err != -ENOSYS)
125 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
126 err);
129 /* Disable the device */
130 xen_pcibk_reset_device(dev);
132 kfree(dev_data);
133 pci_set_drvdata(dev, NULL);
135 /* Clean-up the device */
136 xen_pcibk_config_free_dyn_fields(dev);
137 xen_pcibk_config_free_dev(dev);
139 pci_clear_dev_assigned(dev);
140 pci_dev_put(dev);
142 kfree(psdev);
145 static inline void pcistub_device_get(struct pcistub_device *psdev)
147 kref_get(&psdev->kref);
150 static inline void pcistub_device_put(struct pcistub_device *psdev)
152 kref_put(&psdev->kref, pcistub_device_release);
155 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
156 int slot, int func)
158 struct pcistub_device *psdev;
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
166 return psdev;
170 return NULL;
173 static struct pcistub_device *pcistub_device_find(int domain, int bus,
174 int slot, int func)
176 struct pcistub_device *psdev;
177 unsigned long flags;
179 spin_lock_irqsave(&pcistub_devices_lock, flags);
181 psdev = pcistub_device_find_locked(domain, bus, slot, func);
182 if (psdev)
183 pcistub_device_get(psdev);
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 return psdev;
189 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
190 struct pcistub_device *psdev)
192 struct pci_dev *pci_dev = NULL;
193 unsigned long flags;
195 pcistub_device_get(psdev);
197 spin_lock_irqsave(&psdev->lock, flags);
198 if (!psdev->pdev) {
199 psdev->pdev = pdev;
200 pci_dev = psdev->dev;
202 spin_unlock_irqrestore(&psdev->lock, flags);
204 if (!pci_dev)
205 pcistub_device_put(psdev);
207 return pci_dev;
210 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
211 int domain, int bus,
212 int slot, int func)
214 struct pcistub_device *psdev;
215 struct pci_dev *found_dev = NULL;
216 unsigned long flags;
218 spin_lock_irqsave(&pcistub_devices_lock, flags);
220 psdev = pcistub_device_find_locked(domain, bus, slot, func);
221 if (psdev)
222 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 return found_dev;
228 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
229 struct pci_dev *dev)
231 struct pcistub_device *psdev;
232 struct pci_dev *found_dev = NULL;
233 unsigned long flags;
235 spin_lock_irqsave(&pcistub_devices_lock, flags);
237 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 if (psdev->dev == dev) {
239 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
240 break;
244 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
245 return found_dev;
249 * Called when:
250 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
255 * As such we have to be careful.
257 * To make this easier, the caller has to hold the device lock.
259 void pcistub_put_pci_dev(struct pci_dev *dev)
261 struct pcistub_device *psdev, *found_psdev = NULL;
262 unsigned long flags;
263 struct xen_pcibk_dev_data *dev_data;
264 int ret;
266 spin_lock_irqsave(&pcistub_devices_lock, flags);
268 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 if (psdev->dev == dev) {
270 found_psdev = psdev;
271 break;
275 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
276 if (WARN_ON(!found_psdev))
277 return;
279 /*hold this lock for avoiding breaking link between
280 * pcistub and xen_pcibk when AER is in processing
282 down_write(&pcistub_sem);
283 /* Cleanup our device
284 * (so it's ready for the next domain)
286 device_lock_assert(&dev->dev);
287 __pci_reset_function_locked(dev);
289 dev_data = pci_get_drvdata(dev);
290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 if (!ret) {
293 * The usual sequence is pci_save_state & pci_restore_state
294 * but the guest might have messed the configuration space up.
295 * Use the initial version (when device was bound to us).
297 pci_restore_state(dev);
298 } else
299 dev_info(&dev->dev, "Could not reload PCI state\n");
300 /* This disables the device. */
301 xen_pcibk_reset_device(dev);
303 /* And cleanup up our emulated fields. */
304 xen_pcibk_config_reset_dev(dev);
305 xen_pcibk_config_free_dyn_fields(dev);
307 xen_unregister_device_domain_owner(dev);
309 spin_lock_irqsave(&found_psdev->lock, flags);
310 found_psdev->pdev = NULL;
311 spin_unlock_irqrestore(&found_psdev->lock, flags);
313 pcistub_device_put(found_psdev);
314 up_write(&pcistub_sem);
317 static int pcistub_match_one(struct pci_dev *dev,
318 struct pcistub_device_id *pdev_id)
320 /* Match the specified device by domain, bus, slot, func and also if
321 * any of the device's parent bridges match.
323 for (; dev != NULL; dev = dev->bus->self) {
324 if (pci_domain_nr(dev->bus) == pdev_id->domain
325 && dev->bus->number == pdev_id->bus
326 && dev->devfn == pdev_id->devfn)
327 return 1;
329 /* Sometimes topmost bridge links to itself. */
330 if (dev == dev->bus->self)
331 break;
334 return 0;
337 static int pcistub_match(struct pci_dev *dev)
339 struct pcistub_device_id *pdev_id;
340 unsigned long flags;
341 int found = 0;
343 spin_lock_irqsave(&device_ids_lock, flags);
344 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
345 if (pcistub_match_one(dev, pdev_id)) {
346 found = 1;
347 break;
350 spin_unlock_irqrestore(&device_ids_lock, flags);
352 return found;
355 static int pcistub_init_device(struct pci_dev *dev)
357 struct xen_pcibk_dev_data *dev_data;
358 int err = 0;
360 dev_dbg(&dev->dev, "initializing...\n");
362 /* The PCI backend is not intended to be a module (or to work with
363 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
364 * would need to be called somewhere to free the memory allocated
365 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
367 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
368 + strlen(pci_name(dev)) + 1, GFP_KERNEL);
369 if (!dev_data) {
370 err = -ENOMEM;
371 goto out;
373 pci_set_drvdata(dev, dev_data);
376 * Setup name for fake IRQ handler. It will only be enabled
377 * once the device is turned on by the guest.
379 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
381 dev_dbg(&dev->dev, "initializing config\n");
383 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
384 err = xen_pcibk_config_init_dev(dev);
385 if (err)
386 goto out;
388 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
389 * must do this here because pcibios_enable_device may specify
390 * the pci device's true irq (and possibly its other resources)
391 * if they differ from what's in the configuration space.
392 * This makes the assumption that the device's resources won't
393 * change after this point (otherwise this code may break!)
395 dev_dbg(&dev->dev, "enabling device\n");
396 err = pci_enable_device(dev);
397 if (err)
398 goto config_release;
400 if (dev->msix_cap) {
401 struct physdev_pci_device ppdev = {
402 .seg = pci_domain_nr(dev->bus),
403 .bus = dev->bus->number,
404 .devfn = dev->devfn
407 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
408 if (err && err != -ENOSYS)
409 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
410 err);
413 /* We need the device active to save the state. */
414 dev_dbg(&dev->dev, "save state of device\n");
415 pci_save_state(dev);
416 dev_data->pci_saved_state = pci_store_saved_state(dev);
417 if (!dev_data->pci_saved_state)
418 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
419 else {
420 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
421 __pci_reset_function_locked(dev);
422 pci_restore_state(dev);
424 /* Now disable the device (this also ensures some private device
425 * data is setup before we export)
427 dev_dbg(&dev->dev, "reset device\n");
428 xen_pcibk_reset_device(dev);
430 pci_set_dev_assigned(dev);
431 return 0;
433 config_release:
434 xen_pcibk_config_free_dev(dev);
436 out:
437 pci_set_drvdata(dev, NULL);
438 kfree(dev_data);
439 return err;
443 * Because some initialization still happens on
444 * devices during fs_initcall, we need to defer
445 * full initialization of our devices until
446 * device_initcall.
448 static int __init pcistub_init_devices_late(void)
450 struct pcistub_device *psdev;
451 unsigned long flags;
452 int err = 0;
454 spin_lock_irqsave(&pcistub_devices_lock, flags);
456 while (!list_empty(&seized_devices)) {
457 psdev = container_of(seized_devices.next,
458 struct pcistub_device, dev_list);
459 list_del(&psdev->dev_list);
461 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
463 err = pcistub_init_device(psdev->dev);
464 if (err) {
465 dev_err(&psdev->dev->dev,
466 "error %d initializing device\n", err);
467 kfree(psdev);
468 psdev = NULL;
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
473 if (psdev)
474 list_add_tail(&psdev->dev_list, &pcistub_devices);
477 initialize_devices = 1;
479 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
481 return 0;
484 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
485 int domain, int bus, unsigned int devfn)
487 struct pcistub_device_id *pci_dev_id;
488 unsigned long flags;
489 int found = 0;
491 spin_lock_irqsave(&device_ids_lock, flags);
493 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
494 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
495 pci_dev_id->devfn == devfn) {
496 found = 1;
497 break;
501 if (!found) {
502 new->domain = domain;
503 new->bus = bus;
504 new->devfn = devfn;
505 list_add_tail(&new->slot_list, &pcistub_device_ids);
508 spin_unlock_irqrestore(&device_ids_lock, flags);
510 if (found)
511 kfree(new);
514 static int pcistub_seize(struct pci_dev *dev,
515 struct pcistub_device_id *pci_dev_id)
517 struct pcistub_device *psdev;
518 unsigned long flags;
519 int err = 0;
521 psdev = pcistub_device_alloc(dev);
522 if (!psdev) {
523 kfree(pci_dev_id);
524 return -ENOMEM;
527 spin_lock_irqsave(&pcistub_devices_lock, flags);
529 if (initialize_devices) {
530 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
532 /* don't want irqs disabled when calling pcistub_init_device */
533 err = pcistub_init_device(psdev->dev);
535 spin_lock_irqsave(&pcistub_devices_lock, flags);
537 if (!err)
538 list_add(&psdev->dev_list, &pcistub_devices);
539 } else {
540 dev_dbg(&dev->dev, "deferring initialization\n");
541 list_add(&psdev->dev_list, &seized_devices);
544 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
546 if (err) {
547 kfree(pci_dev_id);
548 pcistub_device_put(psdev);
549 } else if (pci_dev_id)
550 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
551 dev->bus->number, dev->devfn);
553 return err;
556 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
557 * other functions that take the sysfs lock. */
558 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
560 int err = 0, match;
561 struct pcistub_device_id *pci_dev_id = NULL;
563 dev_dbg(&dev->dev, "probing...\n");
565 match = pcistub_match(dev);
567 if ((dev->driver_override &&
568 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
569 match) {
571 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
572 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
573 dev_err(&dev->dev, "can't export pci devices that "
574 "don't have a normal (0) or bridge (1) "
575 "header type!\n");
576 err = -ENODEV;
577 goto out;
580 if (!match) {
581 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
582 if (!pci_dev_id) {
583 err = -ENOMEM;
584 goto out;
588 dev_info(&dev->dev, "seizing device\n");
589 err = pcistub_seize(dev, pci_dev_id);
590 } else
591 /* Didn't find the device */
592 err = -ENODEV;
594 out:
595 return err;
598 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
599 * other functions that take the sysfs lock. */
600 static void pcistub_remove(struct pci_dev *dev)
602 struct pcistub_device *psdev, *found_psdev = NULL;
603 unsigned long flags;
605 dev_dbg(&dev->dev, "removing\n");
607 spin_lock_irqsave(&pcistub_devices_lock, flags);
609 xen_pcibk_config_quirk_release(dev);
611 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
612 if (psdev->dev == dev) {
613 found_psdev = psdev;
614 break;
618 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
620 if (found_psdev) {
621 dev_dbg(&dev->dev, "found device to remove %s\n",
622 found_psdev->pdev ? "- in-use" : "");
624 if (found_psdev->pdev) {
625 int domid = xen_find_device_domain_owner(dev);
627 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
628 pci_name(found_psdev->dev), domid);
629 pr_warn("****** driver domain may still access this device's i/o resources!\n");
630 pr_warn("****** shutdown driver domain before binding device\n");
631 pr_warn("****** to other drivers or domains\n");
633 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
634 * doing the FLR. */
635 xen_pcibk_release_pci_dev(found_psdev->pdev,
636 found_psdev->dev,
637 false /* caller holds the lock. */);
640 spin_lock_irqsave(&pcistub_devices_lock, flags);
641 list_del(&found_psdev->dev_list);
642 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
644 /* the final put for releasing from the list */
645 pcistub_device_put(found_psdev);
649 static const struct pci_device_id pcistub_ids[] = {
651 .vendor = PCI_ANY_ID,
652 .device = PCI_ANY_ID,
653 .subvendor = PCI_ANY_ID,
654 .subdevice = PCI_ANY_ID,
656 {0,},
659 #define PCI_NODENAME_MAX 40
660 static void kill_domain_by_device(struct pcistub_device *psdev)
662 struct xenbus_transaction xbt;
663 int err;
664 char nodename[PCI_NODENAME_MAX];
666 BUG_ON(!psdev);
667 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
668 psdev->pdev->xdev->otherend_id);
670 again:
671 err = xenbus_transaction_start(&xbt);
672 if (err) {
673 dev_err(&psdev->dev->dev,
674 "error %d when start xenbus transaction\n", err);
675 return;
677 /*PV AER handlers will set this flag*/
678 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
679 err = xenbus_transaction_end(xbt, 0);
680 if (err) {
681 if (err == -EAGAIN)
682 goto again;
683 dev_err(&psdev->dev->dev,
684 "error %d when end xenbus transaction\n", err);
685 return;
689 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
690 * backend need to have cooperation. In xen_pcibk, those steps will do similar
691 * jobs: send service request and waiting for front_end response.
693 static pci_ers_result_t common_process(struct pcistub_device *psdev,
694 pci_channel_state_t state, int aer_cmd,
695 pci_ers_result_t result)
697 pci_ers_result_t res = result;
698 struct xen_pcie_aer_op *aer_op;
699 struct xen_pcibk_device *pdev = psdev->pdev;
700 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
701 int ret;
703 /*with PV AER drivers*/
704 aer_op = &(sh_info->aer_op);
705 aer_op->cmd = aer_cmd ;
706 /*useful for error_detected callback*/
707 aer_op->err = state;
708 /*pcifront_end BDF*/
709 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
710 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
711 if (!ret) {
712 dev_err(&psdev->dev->dev,
713 DRV_NAME ": failed to get pcifront device\n");
714 return PCI_ERS_RESULT_NONE;
716 wmb();
718 dev_dbg(&psdev->dev->dev,
719 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
720 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
721 /*local flag to mark there's aer request, xen_pcibk callback will use
722 * this flag to judge whether we need to check pci-front give aer
723 * service ack signal
725 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
727 /*It is possible that a pcifront conf_read_write ops request invokes
728 * the callback which cause the spurious execution of wake_up.
729 * Yet it is harmless and better than a spinlock here
731 set_bit(_XEN_PCIB_active,
732 (unsigned long *)&sh_info->flags);
733 wmb();
734 notify_remote_via_irq(pdev->evtchn_irq);
736 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
737 !(test_bit(_XEN_PCIB_active, (unsigned long *)
738 &sh_info->flags)), 300*HZ);
740 if (!ret) {
741 if (test_bit(_XEN_PCIB_active,
742 (unsigned long *)&sh_info->flags)) {
743 dev_err(&psdev->dev->dev,
744 "pcifront aer process not responding!\n");
745 clear_bit(_XEN_PCIB_active,
746 (unsigned long *)&sh_info->flags);
747 aer_op->err = PCI_ERS_RESULT_NONE;
748 return res;
751 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
753 if (test_bit(_XEN_PCIF_active,
754 (unsigned long *)&sh_info->flags)) {
755 dev_dbg(&psdev->dev->dev,
756 "schedule pci_conf service in " DRV_NAME "\n");
757 xen_pcibk_test_and_schedule_op(psdev->pdev);
760 res = (pci_ers_result_t)aer_op->err;
761 return res;
765 * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
766 * of the device driver could provide this service, and then wait for pcifront
767 * ack.
768 * @dev: pointer to PCI devices
769 * return value is used by aer_core do_recovery policy
771 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
773 struct pcistub_device *psdev;
774 pci_ers_result_t result;
776 result = PCI_ERS_RESULT_RECOVERED;
777 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
778 dev->bus->number, dev->devfn);
780 down_write(&pcistub_sem);
781 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
782 dev->bus->number,
783 PCI_SLOT(dev->devfn),
784 PCI_FUNC(dev->devfn));
786 if (!psdev || !psdev->pdev) {
787 dev_err(&dev->dev,
788 DRV_NAME " device is not found/assigned\n");
789 goto end;
792 if (!psdev->pdev->sh_info) {
793 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
794 " by HVM, kill it\n");
795 kill_domain_by_device(psdev);
796 goto end;
799 if (!test_bit(_XEN_PCIB_AERHANDLER,
800 (unsigned long *)&psdev->pdev->sh_info->flags)) {
801 dev_err(&dev->dev,
802 "guest with no AER driver should have been killed\n");
803 goto end;
805 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
807 if (result == PCI_ERS_RESULT_NONE ||
808 result == PCI_ERS_RESULT_DISCONNECT) {
809 dev_dbg(&dev->dev,
810 "No AER slot_reset service or disconnected!\n");
811 kill_domain_by_device(psdev);
813 end:
814 if (psdev)
815 pcistub_device_put(psdev);
816 up_write(&pcistub_sem);
817 return result;
822 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
823 * in case of the device driver could provide this service, and then wait
824 * for pcifront ack
825 * @dev: pointer to PCI devices
826 * return value is used by aer_core do_recovery policy
829 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
831 struct pcistub_device *psdev;
832 pci_ers_result_t result;
834 result = PCI_ERS_RESULT_RECOVERED;
835 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
836 dev->bus->number, dev->devfn);
838 down_write(&pcistub_sem);
839 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
840 dev->bus->number,
841 PCI_SLOT(dev->devfn),
842 PCI_FUNC(dev->devfn));
844 if (!psdev || !psdev->pdev) {
845 dev_err(&dev->dev,
846 DRV_NAME " device is not found/assigned\n");
847 goto end;
850 if (!psdev->pdev->sh_info) {
851 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
852 " by HVM, kill it\n");
853 kill_domain_by_device(psdev);
854 goto end;
857 if (!test_bit(_XEN_PCIB_AERHANDLER,
858 (unsigned long *)&psdev->pdev->sh_info->flags)) {
859 dev_err(&dev->dev,
860 "guest with no AER driver should have been killed\n");
861 goto end;
863 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
865 if (result == PCI_ERS_RESULT_NONE ||
866 result == PCI_ERS_RESULT_DISCONNECT) {
867 dev_dbg(&dev->dev,
868 "No AER mmio_enabled service or disconnected!\n");
869 kill_domain_by_device(psdev);
871 end:
872 if (psdev)
873 pcistub_device_put(psdev);
874 up_write(&pcistub_sem);
875 return result;
878 /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
879 * in case of the device driver could provide this service, and then wait
880 * for pcifront ack.
881 * @dev: pointer to PCI devices
882 * @error: the current PCI connection state
883 * return value is used by aer_core do_recovery policy
886 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
887 pci_channel_state_t error)
889 struct pcistub_device *psdev;
890 pci_ers_result_t result;
892 result = PCI_ERS_RESULT_CAN_RECOVER;
893 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
894 dev->bus->number, dev->devfn);
896 down_write(&pcistub_sem);
897 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
898 dev->bus->number,
899 PCI_SLOT(dev->devfn),
900 PCI_FUNC(dev->devfn));
902 if (!psdev || !psdev->pdev) {
903 dev_err(&dev->dev,
904 DRV_NAME " device is not found/assigned\n");
905 goto end;
908 if (!psdev->pdev->sh_info) {
909 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
910 " by HVM, kill it\n");
911 kill_domain_by_device(psdev);
912 goto end;
915 /*Guest owns the device yet no aer handler regiested, kill guest*/
916 if (!test_bit(_XEN_PCIB_AERHANDLER,
917 (unsigned long *)&psdev->pdev->sh_info->flags)) {
918 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
919 kill_domain_by_device(psdev);
920 goto end;
922 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
924 if (result == PCI_ERS_RESULT_NONE ||
925 result == PCI_ERS_RESULT_DISCONNECT) {
926 dev_dbg(&dev->dev,
927 "No AER error_detected service or disconnected!\n");
928 kill_domain_by_device(psdev);
930 end:
931 if (psdev)
932 pcistub_device_put(psdev);
933 up_write(&pcistub_sem);
934 return result;
937 /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
938 * in case of the device driver could provide this service, and then wait
939 * for pcifront ack.
940 * @dev: pointer to PCI devices
943 static void xen_pcibk_error_resume(struct pci_dev *dev)
945 struct pcistub_device *psdev;
947 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
948 dev->bus->number, dev->devfn);
950 down_write(&pcistub_sem);
951 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
952 dev->bus->number,
953 PCI_SLOT(dev->devfn),
954 PCI_FUNC(dev->devfn));
956 if (!psdev || !psdev->pdev) {
957 dev_err(&dev->dev,
958 DRV_NAME " device is not found/assigned\n");
959 goto end;
962 if (!psdev->pdev->sh_info) {
963 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
964 " by HVM, kill it\n");
965 kill_domain_by_device(psdev);
966 goto end;
969 if (!test_bit(_XEN_PCIB_AERHANDLER,
970 (unsigned long *)&psdev->pdev->sh_info->flags)) {
971 dev_err(&dev->dev,
972 "guest with no AER driver should have been killed\n");
973 kill_domain_by_device(psdev);
974 goto end;
976 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
977 PCI_ERS_RESULT_RECOVERED);
978 end:
979 if (psdev)
980 pcistub_device_put(psdev);
981 up_write(&pcistub_sem);
982 return;
985 /*add xen_pcibk AER handling*/
986 static const struct pci_error_handlers xen_pcibk_error_handler = {
987 .error_detected = xen_pcibk_error_detected,
988 .mmio_enabled = xen_pcibk_mmio_enabled,
989 .slot_reset = xen_pcibk_slot_reset,
990 .resume = xen_pcibk_error_resume,
994 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
995 * for a normal device. I don't want it to be loaded automatically.
998 static struct pci_driver xen_pcibk_pci_driver = {
999 /* The name should be xen_pciback, but until the tools are updated
1000 * we will keep it as pciback. */
1001 .name = PCISTUB_DRIVER_NAME,
1002 .id_table = pcistub_ids,
1003 .probe = pcistub_probe,
1004 .remove = pcistub_remove,
1005 .err_handler = &xen_pcibk_error_handler,
1008 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1009 int *slot, int *func)
1011 int parsed = 0;
1013 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1014 &parsed)) {
1015 case 3:
1016 *func = -1;
1017 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1018 break;
1019 case 2:
1020 *slot = *func = -1;
1021 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1022 break;
1024 if (parsed && !buf[parsed])
1025 return 0;
1027 /* try again without domain */
1028 *domain = 0;
1029 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1030 case 2:
1031 *func = -1;
1032 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1033 break;
1034 case 1:
1035 *slot = *func = -1;
1036 sscanf(buf, " %x:*.* %n", bus, &parsed);
1037 break;
1039 if (parsed && !buf[parsed])
1040 return 0;
1042 return -EINVAL;
1045 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1046 *slot, int *func, int *reg, int *size, int *mask)
1048 int parsed = 0;
1050 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1051 reg, size, mask, &parsed);
1052 if (parsed && !buf[parsed])
1053 return 0;
1055 /* try again without domain */
1056 *domain = 0;
1057 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1058 mask, &parsed);
1059 if (parsed && !buf[parsed])
1060 return 0;
1062 return -EINVAL;
1065 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1067 struct pcistub_device_id *pci_dev_id;
1068 int rc = 0, devfn = PCI_DEVFN(slot, func);
1070 if (slot < 0) {
1071 for (slot = 0; !rc && slot < 32; ++slot)
1072 rc = pcistub_device_id_add(domain, bus, slot, func);
1073 return rc;
1076 if (func < 0) {
1077 for (func = 0; !rc && func < 8; ++func)
1078 rc = pcistub_device_id_add(domain, bus, slot, func);
1079 return rc;
1082 if ((
1083 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1084 || !defined(CONFIG_PCI_DOMAINS)
1085 !pci_domains_supported ? domain :
1086 #endif
1087 domain < 0 || domain > 0xffff)
1088 || bus < 0 || bus > 0xff
1089 || PCI_SLOT(devfn) != slot
1090 || PCI_FUNC(devfn) != func)
1091 return -EINVAL;
1093 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1094 if (!pci_dev_id)
1095 return -ENOMEM;
1097 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1098 domain, bus, slot, func);
1100 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1102 return 0;
1105 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1107 struct pcistub_device_id *pci_dev_id, *t;
1108 int err = -ENOENT;
1109 unsigned long flags;
1111 spin_lock_irqsave(&device_ids_lock, flags);
1112 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1113 slot_list) {
1114 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1115 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1116 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1117 /* Don't break; here because it's possible the same
1118 * slot could be in the list more than once
1120 list_del(&pci_dev_id->slot_list);
1121 kfree(pci_dev_id);
1123 err = 0;
1125 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1126 domain, bus, slot, func);
1129 spin_unlock_irqrestore(&device_ids_lock, flags);
1131 return err;
1134 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1135 unsigned int reg, unsigned int size,
1136 unsigned int mask)
1138 int err = 0;
1139 struct pcistub_device *psdev;
1140 struct pci_dev *dev;
1141 struct config_field *field;
1143 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1144 return -EINVAL;
1146 psdev = pcistub_device_find(domain, bus, slot, func);
1147 if (!psdev) {
1148 err = -ENODEV;
1149 goto out;
1151 dev = psdev->dev;
1153 field = kzalloc(sizeof(*field), GFP_KERNEL);
1154 if (!field) {
1155 err = -ENOMEM;
1156 goto out;
1159 field->offset = reg;
1160 field->size = size;
1161 field->mask = mask;
1162 field->init = NULL;
1163 field->reset = NULL;
1164 field->release = NULL;
1165 field->clean = xen_pcibk_config_field_free;
1167 err = xen_pcibk_config_quirks_add_field(dev, field);
1168 if (err)
1169 kfree(field);
1170 out:
1171 if (psdev)
1172 pcistub_device_put(psdev);
1173 return err;
1176 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1177 size_t count)
1179 int domain, bus, slot, func;
1180 int err;
1182 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1183 if (err)
1184 goto out;
1186 err = pcistub_device_id_add(domain, bus, slot, func);
1188 out:
1189 if (!err)
1190 err = count;
1191 return err;
1193 static DRIVER_ATTR_WO(new_slot);
1195 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1196 size_t count)
1198 int domain, bus, slot, func;
1199 int err;
1201 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1202 if (err)
1203 goto out;
1205 err = pcistub_device_id_remove(domain, bus, slot, func);
1207 out:
1208 if (!err)
1209 err = count;
1210 return err;
1212 static DRIVER_ATTR_WO(remove_slot);
1214 static ssize_t slots_show(struct device_driver *drv, char *buf)
1216 struct pcistub_device_id *pci_dev_id;
1217 size_t count = 0;
1218 unsigned long flags;
1220 spin_lock_irqsave(&device_ids_lock, flags);
1221 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1222 if (count >= PAGE_SIZE)
1223 break;
1225 count += scnprintf(buf + count, PAGE_SIZE - count,
1226 "%04x:%02x:%02x.%d\n",
1227 pci_dev_id->domain, pci_dev_id->bus,
1228 PCI_SLOT(pci_dev_id->devfn),
1229 PCI_FUNC(pci_dev_id->devfn));
1231 spin_unlock_irqrestore(&device_ids_lock, flags);
1233 return count;
1235 static DRIVER_ATTR_RO(slots);
1237 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1239 struct pcistub_device *psdev;
1240 struct xen_pcibk_dev_data *dev_data;
1241 size_t count = 0;
1242 unsigned long flags;
1244 spin_lock_irqsave(&pcistub_devices_lock, flags);
1245 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1246 if (count >= PAGE_SIZE)
1247 break;
1248 if (!psdev->dev)
1249 continue;
1250 dev_data = pci_get_drvdata(psdev->dev);
1251 if (!dev_data)
1252 continue;
1253 count +=
1254 scnprintf(buf + count, PAGE_SIZE - count,
1255 "%s:%s:%sing:%ld\n",
1256 pci_name(psdev->dev),
1257 dev_data->isr_on ? "on" : "off",
1258 dev_data->ack_intr ? "ack" : "not ack",
1259 dev_data->handled);
1261 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1262 return count;
1264 static DRIVER_ATTR_RO(irq_handlers);
1266 static ssize_t irq_handler_state_store(struct device_driver *drv,
1267 const char *buf, size_t count)
1269 struct pcistub_device *psdev;
1270 struct xen_pcibk_dev_data *dev_data;
1271 int domain, bus, slot, func;
1272 int err;
1274 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1275 if (err)
1276 return err;
1278 psdev = pcistub_device_find(domain, bus, slot, func);
1279 if (!psdev) {
1280 err = -ENOENT;
1281 goto out;
1284 dev_data = pci_get_drvdata(psdev->dev);
1285 if (!dev_data) {
1286 err = -ENOENT;
1287 goto out;
1290 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1291 dev_data->irq_name, dev_data->isr_on,
1292 !dev_data->isr_on);
1294 dev_data->isr_on = !(dev_data->isr_on);
1295 if (dev_data->isr_on)
1296 dev_data->ack_intr = 1;
1297 out:
1298 if (psdev)
1299 pcistub_device_put(psdev);
1300 if (!err)
1301 err = count;
1302 return err;
1304 static DRIVER_ATTR_WO(irq_handler_state);
1306 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1307 size_t count)
1309 int domain, bus, slot, func, reg, size, mask;
1310 int err;
1312 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1313 &mask);
1314 if (err)
1315 goto out;
1317 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1319 out:
1320 if (!err)
1321 err = count;
1322 return err;
1325 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1327 int count = 0;
1328 unsigned long flags;
1329 struct xen_pcibk_config_quirk *quirk;
1330 struct xen_pcibk_dev_data *dev_data;
1331 const struct config_field *field;
1332 const struct config_field_entry *cfg_entry;
1334 spin_lock_irqsave(&device_ids_lock, flags);
1335 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1336 if (count >= PAGE_SIZE)
1337 goto out;
1339 count += scnprintf(buf + count, PAGE_SIZE - count,
1340 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1341 quirk->pdev->bus->number,
1342 PCI_SLOT(quirk->pdev->devfn),
1343 PCI_FUNC(quirk->pdev->devfn),
1344 quirk->devid.vendor, quirk->devid.device,
1345 quirk->devid.subvendor,
1346 quirk->devid.subdevice);
1348 dev_data = pci_get_drvdata(quirk->pdev);
1350 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1351 field = cfg_entry->field;
1352 if (count >= PAGE_SIZE)
1353 goto out;
1355 count += scnprintf(buf + count, PAGE_SIZE - count,
1356 "\t\t%08x:%01x:%08x\n",
1357 cfg_entry->base_offset +
1358 field->offset, field->size,
1359 field->mask);
1363 out:
1364 spin_unlock_irqrestore(&device_ids_lock, flags);
1366 return count;
1368 static DRIVER_ATTR_RW(quirks);
1370 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1371 size_t count)
1373 int domain, bus, slot, func;
1374 int err;
1375 struct pcistub_device *psdev;
1376 struct xen_pcibk_dev_data *dev_data;
1378 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1379 if (err)
1380 goto out;
1382 psdev = pcistub_device_find(domain, bus, slot, func);
1383 if (!psdev) {
1384 err = -ENODEV;
1385 goto out;
1388 dev_data = pci_get_drvdata(psdev->dev);
1389 /* the driver data for a device should never be null at this point */
1390 if (!dev_data) {
1391 err = -ENXIO;
1392 goto release;
1394 if (!dev_data->permissive) {
1395 dev_data->permissive = 1;
1396 /* Let user know that what they're doing could be unsafe */
1397 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1398 "configuration space accesses!\n");
1399 dev_warn(&psdev->dev->dev,
1400 "permissive mode is potentially unsafe!\n");
1402 release:
1403 pcistub_device_put(psdev);
1404 out:
1405 if (!err)
1406 err = count;
1407 return err;
1410 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1412 struct pcistub_device *psdev;
1413 struct xen_pcibk_dev_data *dev_data;
1414 size_t count = 0;
1415 unsigned long flags;
1416 spin_lock_irqsave(&pcistub_devices_lock, flags);
1417 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1418 if (count >= PAGE_SIZE)
1419 break;
1420 if (!psdev->dev)
1421 continue;
1422 dev_data = pci_get_drvdata(psdev->dev);
1423 if (!dev_data || !dev_data->permissive)
1424 continue;
1425 count +=
1426 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1427 pci_name(psdev->dev));
1429 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1430 return count;
1432 static DRIVER_ATTR_RW(permissive);
1434 static void pcistub_exit(void)
1436 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1437 driver_remove_file(&xen_pcibk_pci_driver.driver,
1438 &driver_attr_remove_slot);
1439 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1440 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1441 driver_remove_file(&xen_pcibk_pci_driver.driver,
1442 &driver_attr_permissive);
1443 driver_remove_file(&xen_pcibk_pci_driver.driver,
1444 &driver_attr_irq_handlers);
1445 driver_remove_file(&xen_pcibk_pci_driver.driver,
1446 &driver_attr_irq_handler_state);
1447 pci_unregister_driver(&xen_pcibk_pci_driver);
1450 static int __init pcistub_init(void)
1452 int pos = 0;
1453 int err = 0;
1454 int domain, bus, slot, func;
1455 int parsed;
1457 if (pci_devs_to_hide && *pci_devs_to_hide) {
1458 do {
1459 parsed = 0;
1461 err = sscanf(pci_devs_to_hide + pos,
1462 " (%x:%x:%x.%x) %n",
1463 &domain, &bus, &slot, &func, &parsed);
1464 switch (err) {
1465 case 3:
1466 func = -1;
1467 sscanf(pci_devs_to_hide + pos,
1468 " (%x:%x:%x.*) %n",
1469 &domain, &bus, &slot, &parsed);
1470 break;
1471 case 2:
1472 slot = func = -1;
1473 sscanf(pci_devs_to_hide + pos,
1474 " (%x:%x:*.*) %n",
1475 &domain, &bus, &parsed);
1476 break;
1479 if (!parsed) {
1480 domain = 0;
1481 err = sscanf(pci_devs_to_hide + pos,
1482 " (%x:%x.%x) %n",
1483 &bus, &slot, &func, &parsed);
1484 switch (err) {
1485 case 2:
1486 func = -1;
1487 sscanf(pci_devs_to_hide + pos,
1488 " (%x:%x.*) %n",
1489 &bus, &slot, &parsed);
1490 break;
1491 case 1:
1492 slot = func = -1;
1493 sscanf(pci_devs_to_hide + pos,
1494 " (%x:*.*) %n",
1495 &bus, &parsed);
1496 break;
1500 if (parsed <= 0)
1501 goto parse_error;
1503 err = pcistub_device_id_add(domain, bus, slot, func);
1504 if (err)
1505 goto out;
1507 pos += parsed;
1508 } while (pci_devs_to_hide[pos]);
1511 /* If we're the first PCI Device Driver to register, we're the
1512 * first one to get offered PCI devices as they become
1513 * available (and thus we can be the first to grab them)
1515 err = pci_register_driver(&xen_pcibk_pci_driver);
1516 if (err < 0)
1517 goto out;
1519 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1520 &driver_attr_new_slot);
1521 if (!err)
1522 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1523 &driver_attr_remove_slot);
1524 if (!err)
1525 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1526 &driver_attr_slots);
1527 if (!err)
1528 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1529 &driver_attr_quirks);
1530 if (!err)
1531 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1532 &driver_attr_permissive);
1534 if (!err)
1535 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1536 &driver_attr_irq_handlers);
1537 if (!err)
1538 err = driver_create_file(&xen_pcibk_pci_driver.driver,
1539 &driver_attr_irq_handler_state);
1540 if (err)
1541 pcistub_exit();
1543 out:
1544 return err;
1546 parse_error:
1547 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1548 pci_devs_to_hide + pos);
1549 return -EINVAL;
1552 #ifndef MODULE
1554 * fs_initcall happens before device_initcall
1555 * so xen_pcibk *should* get called first (b/c we
1556 * want to suck up any device before other drivers
1557 * get a chance by being the first pci device
1558 * driver to register)
1560 fs_initcall(pcistub_init);
1561 #endif
1563 #ifdef CONFIG_PCI_IOV
1564 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1566 struct pcistub_device *psdev = NULL;
1567 unsigned long flags;
1568 bool found = false;
1570 spin_lock_irqsave(&pcistub_devices_lock, flags);
1571 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1572 if (!psdev->pdev && psdev->dev != pdev
1573 && pci_physfn(psdev->dev) == pdev) {
1574 found = true;
1575 break;
1578 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1579 if (found)
1580 return psdev;
1581 return NULL;
1584 static int pci_stub_notifier(struct notifier_block *nb,
1585 unsigned long action, void *data)
1587 struct device *dev = data;
1588 const struct pci_dev *pdev = to_pci_dev(dev);
1590 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1591 return NOTIFY_DONE;
1593 if (!pdev->is_physfn)
1594 return NOTIFY_DONE;
1596 for (;;) {
1597 struct pcistub_device *psdev = find_vfs(pdev);
1598 if (!psdev)
1599 break;
1600 device_release_driver(&psdev->dev->dev);
1602 return NOTIFY_DONE;
1605 static struct notifier_block pci_stub_nb = {
1606 .notifier_call = pci_stub_notifier,
1608 #endif
1610 static int __init xen_pcibk_init(void)
1612 int err;
1614 if (!xen_initial_domain())
1615 return -ENODEV;
1617 err = xen_pcibk_config_init();
1618 if (err)
1619 return err;
1621 #ifdef MODULE
1622 err = pcistub_init();
1623 if (err < 0)
1624 return err;
1625 #endif
1627 pcistub_init_devices_late();
1628 err = xen_pcibk_xenbus_register();
1629 if (err)
1630 pcistub_exit();
1631 #ifdef CONFIG_PCI_IOV
1632 else
1633 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1634 #endif
1636 return err;
1639 static void __exit xen_pcibk_cleanup(void)
1641 #ifdef CONFIG_PCI_IOV
1642 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1643 #endif
1644 xen_pcibk_xenbus_unregister();
1645 pcistub_exit();
1648 module_init(xen_pcibk_init);
1649 module_exit(xen_pcibk_cleanup);
1651 MODULE_LICENSE("Dual BSD/GPL");
1652 MODULE_ALIAS("xen-backend:pci");