blob: 1a92739f4318f620bff6a25781e30a9b95fe2efa [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
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>
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -040016#include <linux/atomic.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040017#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"
23
24static char *pci_devs_to_hide;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040025wait_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
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040028*/
29static DECLARE_RWSEM(pcistub_sem);
30module_param_named(hide, pci_devs_to_hide, charp, 0444);
31
32struct pcistub_device_id {
33 struct list_head slot_list;
34 int domain;
35 unsigned char bus;
36 unsigned int devfn;
37};
38static LIST_HEAD(pcistub_device_ids);
39static DEFINE_SPINLOCK(device_ids_lock);
40
41struct pcistub_device {
42 struct kref kref;
43 struct list_head dev_list;
44 spinlock_t lock;
45
46 struct pci_dev *dev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040047 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040048};
49
50/* Access to pcistub_devices & seized_devices lists and the initialize_devices
51 * flag must be locked with pcistub_devices_lock
52 */
53static DEFINE_SPINLOCK(pcistub_devices_lock);
54static LIST_HEAD(pcistub_devices);
55
56/* wait for device_initcall before initializing our devices
57 * (see pcistub_init_devices_late)
58 */
59static int initialize_devices;
60static LIST_HEAD(seized_devices);
61
62static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
63{
64 struct pcistub_device *psdev;
65
66 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
67
68 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
69 if (!psdev)
70 return NULL;
71
72 psdev->dev = pci_dev_get(dev);
73 if (!psdev->dev) {
74 kfree(psdev);
75 return NULL;
76 }
77
78 kref_init(&psdev->kref);
79 spin_lock_init(&psdev->lock);
80
81 return psdev;
82}
83
84/* Don't call this directly as it's called by pcistub_device_put */
85static void pcistub_device_release(struct kref *kref)
86{
87 struct pcistub_device *psdev;
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050088 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040089
90 psdev = container_of(kref, struct pcistub_device, kref);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050091 dev_data = pci_get_drvdata(psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040092
93 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
94
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -050095 xen_unregister_device_domain_owner(psdev->dev);
96
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050097 /* Call the reset function which does not take lock as this
98 * is called from "unbind" which takes a device_lock mutex.
99 */
100 __pci_reset_function_locked(psdev->dev);
101 if (pci_load_and_free_saved_state(psdev->dev,
102 &dev_data->pci_saved_state)) {
103 dev_dbg(&psdev->dev->dev, "Could not reload PCI state\n");
104 } else
105 pci_restore_state(psdev->dev);
106
107 /* Disable the device */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400108 xen_pcibk_reset_device(psdev->dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500109
110 kfree(dev_data);
111 pci_set_drvdata(psdev->dev, NULL);
112
113 /* Clean-up the device */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400114 xen_pcibk_config_free_dyn_fields(psdev->dev);
115 xen_pcibk_config_free_dev(psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400116
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500117 psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400118 pci_dev_put(psdev->dev);
119
120 kfree(psdev);
121}
122
123static inline void pcistub_device_get(struct pcistub_device *psdev)
124{
125 kref_get(&psdev->kref);
126}
127
128static inline void pcistub_device_put(struct pcistub_device *psdev)
129{
130 kref_put(&psdev->kref, pcistub_device_release);
131}
132
133static struct pcistub_device *pcistub_device_find(int domain, int bus,
134 int slot, int func)
135{
136 struct pcistub_device *psdev = NULL;
137 unsigned long flags;
138
139 spin_lock_irqsave(&pcistub_devices_lock, flags);
140
141 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
142 if (psdev->dev != NULL
143 && domain == pci_domain_nr(psdev->dev->bus)
144 && bus == psdev->dev->bus->number
145 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
146 pcistub_device_get(psdev);
147 goto out;
148 }
149 }
150
151 /* didn't find it */
152 psdev = NULL;
153
154out:
155 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
156 return psdev;
157}
158
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400159static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400160 struct pcistub_device *psdev)
161{
162 struct pci_dev *pci_dev = NULL;
163 unsigned long flags;
164
165 pcistub_device_get(psdev);
166
167 spin_lock_irqsave(&psdev->lock, flags);
168 if (!psdev->pdev) {
169 psdev->pdev = pdev;
170 pci_dev = psdev->dev;
171 }
172 spin_unlock_irqrestore(&psdev->lock, flags);
173
174 if (!pci_dev)
175 pcistub_device_put(psdev);
176
177 return pci_dev;
178}
179
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400180struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400181 int domain, int bus,
182 int slot, int func)
183{
184 struct pcistub_device *psdev;
185 struct pci_dev *found_dev = NULL;
186 unsigned long flags;
187
188 spin_lock_irqsave(&pcistub_devices_lock, flags);
189
190 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
191 if (psdev->dev != NULL
192 && domain == pci_domain_nr(psdev->dev->bus)
193 && bus == psdev->dev->bus->number
194 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
195 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
196 break;
197 }
198 }
199
200 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
201 return found_dev;
202}
203
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400204struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400205 struct pci_dev *dev)
206{
207 struct pcistub_device *psdev;
208 struct pci_dev *found_dev = NULL;
209 unsigned long flags;
210
211 spin_lock_irqsave(&pcistub_devices_lock, flags);
212
213 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
214 if (psdev->dev == dev) {
215 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
216 break;
217 }
218 }
219
220 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
221 return found_dev;
222}
223
224void pcistub_put_pci_dev(struct pci_dev *dev)
225{
226 struct pcistub_device *psdev, *found_psdev = NULL;
227 unsigned long flags;
228
229 spin_lock_irqsave(&pcistub_devices_lock, flags);
230
231 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
232 if (psdev->dev == dev) {
233 found_psdev = psdev;
234 break;
235 }
236 }
237
238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400239 if (WARN_ON(!found_psdev))
240 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400241
242 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400243 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400244 */
245 down_write(&pcistub_sem);
246 /* Cleanup our device
247 * (so it's ready for the next domain)
248 */
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500249
250 /* This is OK - we are running from workqueue context
251 * and want to inhibit the user from fiddling with 'reset'
252 */
253 pci_reset_function(dev);
254 pci_restore_state(psdev->dev);
255
256 /* This disables the device. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400257 xen_pcibk_reset_device(found_psdev->dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500258
259 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400260 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
261 xen_pcibk_config_reset_dev(found_psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400262
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500263 xen_unregister_device_domain_owner(found_psdev->dev);
264
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400265 spin_lock_irqsave(&found_psdev->lock, flags);
266 found_psdev->pdev = NULL;
267 spin_unlock_irqrestore(&found_psdev->lock, flags);
268
269 pcistub_device_put(found_psdev);
270 up_write(&pcistub_sem);
271}
272
273static int __devinit pcistub_match_one(struct pci_dev *dev,
274 struct pcistub_device_id *pdev_id)
275{
276 /* Match the specified device by domain, bus, slot, func and also if
277 * any of the device's parent bridges match.
278 */
279 for (; dev != NULL; dev = dev->bus->self) {
280 if (pci_domain_nr(dev->bus) == pdev_id->domain
281 && dev->bus->number == pdev_id->bus
282 && dev->devfn == pdev_id->devfn)
283 return 1;
284
285 /* Sometimes topmost bridge links to itself. */
286 if (dev == dev->bus->self)
287 break;
288 }
289
290 return 0;
291}
292
293static int __devinit pcistub_match(struct pci_dev *dev)
294{
295 struct pcistub_device_id *pdev_id;
296 unsigned long flags;
297 int found = 0;
298
299 spin_lock_irqsave(&device_ids_lock, flags);
300 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
301 if (pcistub_match_one(dev, pdev_id)) {
302 found = 1;
303 break;
304 }
305 }
306 spin_unlock_irqrestore(&device_ids_lock, flags);
307
308 return found;
309}
310
311static int __devinit pcistub_init_device(struct pci_dev *dev)
312{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400313 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400314 int err = 0;
315
316 dev_dbg(&dev->dev, "initializing...\n");
317
318 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400319 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400320 * would need to be called somewhere to free the memory allocated
321 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
322 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400323 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
324 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400325 if (!dev_data) {
326 err = -ENOMEM;
327 goto out;
328 }
329 pci_set_drvdata(dev, dev_data);
330
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400331 /*
332 * Setup name for fake IRQ handler. It will only be enabled
333 * once the device is turned on by the guest.
334 */
335 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
336
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400337 dev_dbg(&dev->dev, "initializing config\n");
338
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400339 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
340 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400341 if (err)
342 goto out;
343
344 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
345 * must do this here because pcibios_enable_device may specify
346 * the pci device's true irq (and possibly its other resources)
347 * if they differ from what's in the configuration space.
348 * This makes the assumption that the device's resources won't
349 * change after this point (otherwise this code may break!)
350 */
351 dev_dbg(&dev->dev, "enabling device\n");
352 err = pci_enable_device(dev);
353 if (err)
354 goto config_release;
355
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500356 /* We need the device active to save the state. */
357 dev_dbg(&dev->dev, "save state of device\n");
358 pci_save_state(dev);
359 dev_data->pci_saved_state = pci_store_saved_state(dev);
360 if (!dev_data->pci_saved_state)
361 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400362 else {
363 dev_dbg(&dev->dev, "reseting (FLR, D3, etc) the device\n");
364 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400365 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400366 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400367 /* Now disable the device (this also ensures some private device
368 * data is setup before we export)
369 */
370 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400371 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400372
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500373 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400374 return 0;
375
376config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400377 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400378
379out:
380 pci_set_drvdata(dev, NULL);
381 kfree(dev_data);
382 return err;
383}
384
385/*
386 * Because some initialization still happens on
387 * devices during fs_initcall, we need to defer
388 * full initialization of our devices until
389 * device_initcall.
390 */
391static int __init pcistub_init_devices_late(void)
392{
393 struct pcistub_device *psdev;
394 unsigned long flags;
395 int err = 0;
396
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400397 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400398
399 spin_lock_irqsave(&pcistub_devices_lock, flags);
400
401 while (!list_empty(&seized_devices)) {
402 psdev = container_of(seized_devices.next,
403 struct pcistub_device, dev_list);
404 list_del(&psdev->dev_list);
405
406 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
407
408 err = pcistub_init_device(psdev->dev);
409 if (err) {
410 dev_err(&psdev->dev->dev,
411 "error %d initializing device\n", err);
412 kfree(psdev);
413 psdev = NULL;
414 }
415
416 spin_lock_irqsave(&pcistub_devices_lock, flags);
417
418 if (psdev)
419 list_add_tail(&psdev->dev_list, &pcistub_devices);
420 }
421
422 initialize_devices = 1;
423
424 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
425
426 return 0;
427}
428
429static int __devinit pcistub_seize(struct pci_dev *dev)
430{
431 struct pcistub_device *psdev;
432 unsigned long flags;
433 int err = 0;
434
435 psdev = pcistub_device_alloc(dev);
436 if (!psdev)
437 return -ENOMEM;
438
439 spin_lock_irqsave(&pcistub_devices_lock, flags);
440
441 if (initialize_devices) {
442 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
443
444 /* don't want irqs disabled when calling pcistub_init_device */
445 err = pcistub_init_device(psdev->dev);
446
447 spin_lock_irqsave(&pcistub_devices_lock, flags);
448
449 if (!err)
450 list_add(&psdev->dev_list, &pcistub_devices);
451 } else {
452 dev_dbg(&dev->dev, "deferring initialization\n");
453 list_add(&psdev->dev_list, &seized_devices);
454 }
455
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
457
458 if (err)
459 pcistub_device_put(psdev);
460
461 return err;
462}
463
464static int __devinit pcistub_probe(struct pci_dev *dev,
465 const struct pci_device_id *id)
466{
467 int err = 0;
468
469 dev_dbg(&dev->dev, "probing...\n");
470
471 if (pcistub_match(dev)) {
472
473 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
474 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
475 dev_err(&dev->dev, "can't export pci devices that "
476 "don't have a normal (0) or bridge (1) "
477 "header type!\n");
478 err = -ENODEV;
479 goto out;
480 }
481
482 dev_info(&dev->dev, "seizing device\n");
483 err = pcistub_seize(dev);
484 } else
485 /* Didn't find the device */
486 err = -ENODEV;
487
488out:
489 return err;
490}
491
492static void pcistub_remove(struct pci_dev *dev)
493{
494 struct pcistub_device *psdev, *found_psdev = NULL;
495 unsigned long flags;
496
497 dev_dbg(&dev->dev, "removing\n");
498
499 spin_lock_irqsave(&pcistub_devices_lock, flags);
500
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400501 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400502
503 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
504 if (psdev->dev == dev) {
505 found_psdev = psdev;
506 break;
507 }
508 }
509
510 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
511
512 if (found_psdev) {
513 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
514 found_psdev->pdev);
515
516 if (found_psdev->pdev) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400517 printk(KERN_WARNING DRV_NAME ": ****** removing device "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400518 "%s while still in-use! ******\n",
519 pci_name(found_psdev->dev));
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400520 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
521 " still access this device's i/o resources!\n");
522 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400523 "domain before binding device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400524 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400525 "or domains\n");
526
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400527 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400528 found_psdev->dev);
529 }
530
531 spin_lock_irqsave(&pcistub_devices_lock, flags);
532 list_del(&found_psdev->dev_list);
533 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
534
535 /* the final put for releasing from the list */
536 pcistub_device_put(found_psdev);
537 }
538}
539
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400540static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400541 {
542 .vendor = PCI_ANY_ID,
543 .device = PCI_ANY_ID,
544 .subvendor = PCI_ANY_ID,
545 .subdevice = PCI_ANY_ID,
546 },
547 {0,},
548};
549
550#define PCI_NODENAME_MAX 40
551static void kill_domain_by_device(struct pcistub_device *psdev)
552{
553 struct xenbus_transaction xbt;
554 int err;
555 char nodename[PCI_NODENAME_MAX];
556
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400557 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400558 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
559 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400560
561again:
562 err = xenbus_transaction_start(&xbt);
563 if (err) {
564 dev_err(&psdev->dev->dev,
565 "error %d when start xenbus transaction\n", err);
566 return;
567 }
568 /*PV AER handlers will set this flag*/
569 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
570 err = xenbus_transaction_end(xbt, 0);
571 if (err) {
572 if (err == -EAGAIN)
573 goto again;
574 dev_err(&psdev->dev->dev,
575 "error %d when end xenbus transaction\n", err);
576 return;
577 }
578}
579
580/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400581 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400582 * jobs: send service request and waiting for front_end response.
583*/
584static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400585 pci_channel_state_t state, int aer_cmd,
586 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400587{
588 pci_ers_result_t res = result;
589 struct xen_pcie_aer_op *aer_op;
590 int ret;
591
592 /*with PV AER drivers*/
593 aer_op = &(psdev->pdev->sh_info->aer_op);
594 aer_op->cmd = aer_cmd ;
595 /*useful for error_detected callback*/
596 aer_op->err = state;
597 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400598 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400599 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
600 if (!ret) {
601 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400602 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400603 return PCI_ERS_RESULT_NONE;
604 }
605 wmb();
606
607 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400608 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400609 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400610 /*local flag to mark there's aer request, xen_pcibk callback will use
611 * this flag to judge whether we need to check pci-front give aer
612 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400613 */
614 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
615
616 /*It is possible that a pcifront conf_read_write ops request invokes
617 * the callback which cause the spurious execution of wake_up.
618 * Yet it is harmless and better than a spinlock here
619 */
620 set_bit(_XEN_PCIB_active,
621 (unsigned long *)&psdev->pdev->sh_info->flags);
622 wmb();
623 notify_remote_via_irq(psdev->pdev->evtchn_irq);
624
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400625 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
626 !(test_bit(_XEN_PCIB_active, (unsigned long *)
627 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400628
629 if (!ret) {
630 if (test_bit(_XEN_PCIB_active,
631 (unsigned long *)&psdev->pdev->sh_info->flags)) {
632 dev_err(&psdev->dev->dev,
633 "pcifront aer process not responding!\n");
634 clear_bit(_XEN_PCIB_active,
635 (unsigned long *)&psdev->pdev->sh_info->flags);
636 aer_op->err = PCI_ERS_RESULT_NONE;
637 return res;
638 }
639 }
640 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
641
642 if (test_bit(_XEN_PCIF_active,
643 (unsigned long *)&psdev->pdev->sh_info->flags)) {
644 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400645 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400646 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400647 }
648
649 res = (pci_ers_result_t)aer_op->err;
650 return res;
651}
652
653/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400654* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400655* of the device driver could provide this service, and then wait for pcifront
656* ack.
657* @dev: pointer to PCI devices
658* return value is used by aer_core do_recovery policy
659*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400660static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400661{
662 struct pcistub_device *psdev;
663 pci_ers_result_t result;
664
665 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400666 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400667 dev->bus->number, dev->devfn);
668
669 down_write(&pcistub_sem);
670 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
671 dev->bus->number,
672 PCI_SLOT(dev->devfn),
673 PCI_FUNC(dev->devfn));
674
675 if (!psdev || !psdev->pdev) {
676 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400677 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400678 goto end;
679 }
680
681 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400682 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400683 " by HVM, kill it\n");
684 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100685 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400686 }
687
688 if (!test_bit(_XEN_PCIB_AERHANDLER,
689 (unsigned long *)&psdev->pdev->sh_info->flags)) {
690 dev_err(&dev->dev,
691 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100692 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400693 }
694 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
695
696 if (result == PCI_ERS_RESULT_NONE ||
697 result == PCI_ERS_RESULT_DISCONNECT) {
698 dev_dbg(&dev->dev,
699 "No AER slot_reset service or disconnected!\n");
700 kill_domain_by_device(psdev);
701 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400702end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100703 if (psdev)
704 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400705 up_write(&pcistub_sem);
706 return result;
707
708}
709
710
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400711/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400712* in case of the device driver could provide this service, and then wait
713* for pcifront ack
714* @dev: pointer to PCI devices
715* return value is used by aer_core do_recovery policy
716*/
717
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400718static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400719{
720 struct pcistub_device *psdev;
721 pci_ers_result_t result;
722
723 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400724 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400725 dev->bus->number, dev->devfn);
726
727 down_write(&pcistub_sem);
728 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
729 dev->bus->number,
730 PCI_SLOT(dev->devfn),
731 PCI_FUNC(dev->devfn));
732
733 if (!psdev || !psdev->pdev) {
734 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400735 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400736 goto end;
737 }
738
739 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400740 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400741 " by HVM, kill it\n");
742 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100743 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400744 }
745
746 if (!test_bit(_XEN_PCIB_AERHANDLER,
747 (unsigned long *)&psdev->pdev->sh_info->flags)) {
748 dev_err(&dev->dev,
749 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100750 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400751 }
752 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
753
754 if (result == PCI_ERS_RESULT_NONE ||
755 result == PCI_ERS_RESULT_DISCONNECT) {
756 dev_dbg(&dev->dev,
757 "No AER mmio_enabled service or disconnected!\n");
758 kill_domain_by_device(psdev);
759 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400760end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100761 if (psdev)
762 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400763 up_write(&pcistub_sem);
764 return result;
765}
766
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400767/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400768* in case of the device driver could provide this service, and then wait
769* for pcifront ack.
770* @dev: pointer to PCI devices
771* @error: the current PCI connection state
772* return value is used by aer_core do_recovery policy
773*/
774
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400775static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400776 pci_channel_state_t error)
777{
778 struct pcistub_device *psdev;
779 pci_ers_result_t result;
780
781 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400782 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400783 dev->bus->number, dev->devfn);
784
785 down_write(&pcistub_sem);
786 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
787 dev->bus->number,
788 PCI_SLOT(dev->devfn),
789 PCI_FUNC(dev->devfn));
790
791 if (!psdev || !psdev->pdev) {
792 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400793 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400794 goto end;
795 }
796
797 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400798 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400799 " by HVM, kill it\n");
800 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100801 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400802 }
803
804 /*Guest owns the device yet no aer handler regiested, kill guest*/
805 if (!test_bit(_XEN_PCIB_AERHANDLER,
806 (unsigned long *)&psdev->pdev->sh_info->flags)) {
807 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
808 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100809 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400810 }
811 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
812
813 if (result == PCI_ERS_RESULT_NONE ||
814 result == PCI_ERS_RESULT_DISCONNECT) {
815 dev_dbg(&dev->dev,
816 "No AER error_detected service or disconnected!\n");
817 kill_domain_by_device(psdev);
818 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400819end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100820 if (psdev)
821 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400822 up_write(&pcistub_sem);
823 return result;
824}
825
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400826/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400827* in case of the device driver could provide this service, and then wait
828* for pcifront ack.
829* @dev: pointer to PCI devices
830*/
831
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400832static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400833{
834 struct pcistub_device *psdev;
835
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400836 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400837 dev->bus->number, dev->devfn);
838
839 down_write(&pcistub_sem);
840 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
841 dev->bus->number,
842 PCI_SLOT(dev->devfn),
843 PCI_FUNC(dev->devfn));
844
845 if (!psdev || !psdev->pdev) {
846 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400847 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400848 goto end;
849 }
850
851 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400852 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400853 " by HVM, kill it\n");
854 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100855 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400856 }
857
858 if (!test_bit(_XEN_PCIB_AERHANDLER,
859 (unsigned long *)&psdev->pdev->sh_info->flags)) {
860 dev_err(&dev->dev,
861 "guest with no AER driver should have been killed\n");
862 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100863 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400864 }
865 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
866 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400867end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100868 if (psdev)
869 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400870 up_write(&pcistub_sem);
871 return;
872}
873
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400874/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700875static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400876 .error_detected = xen_pcibk_error_detected,
877 .mmio_enabled = xen_pcibk_mmio_enabled,
878 .slot_reset = xen_pcibk_slot_reset,
879 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400880};
881
882/*
883 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
884 * for a normal device. I don't want it to be loaded automatically.
885 */
886
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400887static struct pci_driver xen_pcibk_pci_driver = {
888 /* The name should be xen_pciback, but until the tools are updated
889 * we will keep it as pciback. */
890 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400891 .id_table = pcistub_ids,
892 .probe = pcistub_probe,
893 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400894 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400895};
896
897static inline int str_to_slot(const char *buf, int *domain, int *bus,
898 int *slot, int *func)
899{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000900 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400901
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000902 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
903 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100904 case 3:
905 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000906 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100907 break;
908 case 2:
909 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000910 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100911 break;
912 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000913 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400914 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400915
916 /* try again without domain */
917 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000918 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100919 case 2:
920 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000921 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100922 break;
923 case 1:
924 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000925 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100926 break;
927 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000928 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400929 return 0;
930
931 return -EINVAL;
932}
933
934static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
935 *slot, int *func, int *reg, int *size, int *mask)
936{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000937 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400938
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000939 sscanf(buf, " %4x:%2x:%2x.%d-%8x:%1x:%8x %n", domain, bus, slot, func,
940 reg, size, mask, &parsed);
941 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400942 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000943
944 /* try again without domain */
945 *domain = 0;
946 sscanf(buf, " %2x:%2x.%d-%8x:%1x:%8x %n", bus, slot, func, reg, size,
947 mask, &parsed);
948 if (parsed && !buf[parsed])
949 return 0;
950
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400951 return -EINVAL;
952}
953
954static int pcistub_device_id_add(int domain, int bus, int slot, int func)
955{
956 struct pcistub_device_id *pci_dev_id;
957 unsigned long flags;
Jan Beulichc3cb4702012-09-18 12:29:03 +0100958 int rc = 0;
959
960 if (slot < 0) {
961 for (slot = 0; !rc && slot < 32; ++slot)
962 rc = pcistub_device_id_add(domain, bus, slot, func);
963 return rc;
964 }
965
966 if (func < 0) {
967 for (func = 0; !rc && func < 8; ++func)
968 rc = pcistub_device_id_add(domain, bus, slot, func);
969 return rc;
970 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400971
972 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
973 if (!pci_dev_id)
974 return -ENOMEM;
975
976 pci_dev_id->domain = domain;
977 pci_dev_id->bus = bus;
978 pci_dev_id->devfn = PCI_DEVFN(slot, func);
979
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500980 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400981 domain, bus, slot, func);
982
983 spin_lock_irqsave(&device_ids_lock, flags);
984 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
985 spin_unlock_irqrestore(&device_ids_lock, flags);
986
987 return 0;
988}
989
990static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
991{
992 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400993 int err = -ENOENT;
994 unsigned long flags;
995
996 spin_lock_irqsave(&device_ids_lock, flags);
997 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
998 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100999 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1000 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1001 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001002 /* Don't break; here because it's possible the same
1003 * slot could be in the list more than once
1004 */
1005 list_del(&pci_dev_id->slot_list);
1006 kfree(pci_dev_id);
1007
1008 err = 0;
1009
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001010 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001011 "seize list\n", domain, bus, slot, func);
1012 }
1013 }
1014 spin_unlock_irqrestore(&device_ids_lock, flags);
1015
1016 return err;
1017}
1018
1019static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
1020 int size, int mask)
1021{
1022 int err = 0;
1023 struct pcistub_device *psdev;
1024 struct pci_dev *dev;
1025 struct config_field *field;
1026
1027 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001028 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001029 err = -ENODEV;
1030 goto out;
1031 }
1032 dev = psdev->dev;
1033
1034 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1035 if (!field) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039
1040 field->offset = reg;
1041 field->size = size;
1042 field->mask = mask;
1043 field->init = NULL;
1044 field->reset = NULL;
1045 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001046 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001047
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001048 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001049 if (err)
1050 kfree(field);
1051out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001052 if (psdev)
1053 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001054 return err;
1055}
1056
1057static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1058 size_t count)
1059{
1060 int domain, bus, slot, func;
1061 int err;
1062
1063 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1064 if (err)
1065 goto out;
1066
1067 err = pcistub_device_id_add(domain, bus, slot, func);
1068
1069out:
1070 if (!err)
1071 err = count;
1072 return err;
1073}
Jan Beulich402c5e12011-09-21 16:22:11 -04001074static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001075
1076static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1077 size_t count)
1078{
1079 int domain, bus, slot, func;
1080 int err;
1081
1082 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1083 if (err)
1084 goto out;
1085
1086 err = pcistub_device_id_remove(domain, bus, slot, func);
1087
1088out:
1089 if (!err)
1090 err = count;
1091 return err;
1092}
Jan Beulich402c5e12011-09-21 16:22:11 -04001093static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001094
1095static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1096{
1097 struct pcistub_device_id *pci_dev_id;
1098 size_t count = 0;
1099 unsigned long flags;
1100
1101 spin_lock_irqsave(&device_ids_lock, flags);
1102 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1103 if (count >= PAGE_SIZE)
1104 break;
1105
1106 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001107 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001108 pci_dev_id->domain, pci_dev_id->bus,
1109 PCI_SLOT(pci_dev_id->devfn),
1110 PCI_FUNC(pci_dev_id->devfn));
1111 }
1112 spin_unlock_irqrestore(&device_ids_lock, flags);
1113
1114 return count;
1115}
Jan Beulich402c5e12011-09-21 16:22:11 -04001116static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001117
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001118static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1119{
1120 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001121 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001122 size_t count = 0;
1123 unsigned long flags;
1124
1125 spin_lock_irqsave(&pcistub_devices_lock, flags);
1126 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1127 if (count >= PAGE_SIZE)
1128 break;
1129 if (!psdev->dev)
1130 continue;
1131 dev_data = pci_get_drvdata(psdev->dev);
1132 if (!dev_data)
1133 continue;
1134 count +=
1135 scnprintf(buf + count, PAGE_SIZE - count,
1136 "%s:%s:%sing:%ld\n",
1137 pci_name(psdev->dev),
1138 dev_data->isr_on ? "on" : "off",
1139 dev_data->ack_intr ? "ack" : "not ack",
1140 dev_data->handled);
1141 }
1142 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1143 return count;
1144}
Jan Beulich402c5e12011-09-21 16:22:11 -04001145static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001146
1147static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1148 const char *buf,
1149 size_t count)
1150{
1151 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001152 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001153 int domain, bus, slot, func;
1154 int err = -ENOENT;
1155
1156 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1157 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001158 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001159
1160 psdev = pcistub_device_find(domain, bus, slot, func);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001161 if (!psdev)
1162 goto out;
1163
1164 dev_data = pci_get_drvdata(psdev->dev);
1165 if (!dev_data)
1166 goto out;
1167
1168 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1169 dev_data->irq_name, dev_data->isr_on,
1170 !dev_data->isr_on);
1171
1172 dev_data->isr_on = !(dev_data->isr_on);
1173 if (dev_data->isr_on)
1174 dev_data->ack_intr = 1;
1175out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001176 if (psdev)
1177 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001178 if (!err)
1179 err = count;
1180 return err;
1181}
Jan Beulich402c5e12011-09-21 16:22:11 -04001182static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1183 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001184
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001185static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1186 size_t count)
1187{
1188 int domain, bus, slot, func, reg, size, mask;
1189 int err;
1190
1191 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1192 &mask);
1193 if (err)
1194 goto out;
1195
1196 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1197
1198out:
1199 if (!err)
1200 err = count;
1201 return err;
1202}
1203
1204static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1205{
1206 int count = 0;
1207 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001208 struct xen_pcibk_config_quirk *quirk;
1209 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001210 const struct config_field *field;
1211 const struct config_field_entry *cfg_entry;
1212
1213 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001214 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001215 if (count >= PAGE_SIZE)
1216 goto out;
1217
1218 count += scnprintf(buf + count, PAGE_SIZE - count,
1219 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1220 quirk->pdev->bus->number,
1221 PCI_SLOT(quirk->pdev->devfn),
1222 PCI_FUNC(quirk->pdev->devfn),
1223 quirk->devid.vendor, quirk->devid.device,
1224 quirk->devid.subvendor,
1225 quirk->devid.subdevice);
1226
1227 dev_data = pci_get_drvdata(quirk->pdev);
1228
1229 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1230 field = cfg_entry->field;
1231 if (count >= PAGE_SIZE)
1232 goto out;
1233
1234 count += scnprintf(buf + count, PAGE_SIZE - count,
1235 "\t\t%08x:%01x:%08x\n",
1236 cfg_entry->base_offset +
1237 field->offset, field->size,
1238 field->mask);
1239 }
1240 }
1241
1242out:
1243 spin_unlock_irqrestore(&device_ids_lock, flags);
1244
1245 return count;
1246}
Jan Beulich402c5e12011-09-21 16:22:11 -04001247static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1248 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001249
1250static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1251 size_t count)
1252{
1253 int domain, bus, slot, func;
1254 int err;
1255 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001256 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001257 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1258 if (err)
1259 goto out;
Jan Beulichc3cb4702012-09-18 12:29:03 +01001260 if (slot < 0 || func < 0) {
1261 err = -EINVAL;
1262 goto out;
1263 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001264 psdev = pcistub_device_find(domain, bus, slot, func);
1265 if (!psdev) {
1266 err = -ENODEV;
1267 goto out;
1268 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001269
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001270 dev_data = pci_get_drvdata(psdev->dev);
1271 /* the driver data for a device should never be null at this point */
1272 if (!dev_data) {
1273 err = -ENXIO;
1274 goto release;
1275 }
1276 if (!dev_data->permissive) {
1277 dev_data->permissive = 1;
1278 /* Let user know that what they're doing could be unsafe */
1279 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1280 "configuration space accesses!\n");
1281 dev_warn(&psdev->dev->dev,
1282 "permissive mode is potentially unsafe!\n");
1283 }
1284release:
1285 pcistub_device_put(psdev);
1286out:
1287 if (!err)
1288 err = count;
1289 return err;
1290}
1291
1292static ssize_t permissive_show(struct device_driver *drv, char *buf)
1293{
1294 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001295 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001296 size_t count = 0;
1297 unsigned long flags;
1298 spin_lock_irqsave(&pcistub_devices_lock, flags);
1299 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1300 if (count >= PAGE_SIZE)
1301 break;
1302 if (!psdev->dev)
1303 continue;
1304 dev_data = pci_get_drvdata(psdev->dev);
1305 if (!dev_data || !dev_data->permissive)
1306 continue;
1307 count +=
1308 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1309 pci_name(psdev->dev));
1310 }
1311 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1312 return count;
1313}
Jan Beulich402c5e12011-09-21 16:22:11 -04001314static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1315 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001316
1317static void pcistub_exit(void)
1318{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001319 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1320 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001321 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001322 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1323 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1324 driver_remove_file(&xen_pcibk_pci_driver.driver,
1325 &driver_attr_permissive);
1326 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001327 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001328 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001329 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001330 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001331}
1332
1333static int __init pcistub_init(void)
1334{
1335 int pos = 0;
1336 int err = 0;
1337 int domain, bus, slot, func;
1338 int parsed;
1339
1340 if (pci_devs_to_hide && *pci_devs_to_hide) {
1341 do {
1342 parsed = 0;
1343
1344 err = sscanf(pci_devs_to_hide + pos,
1345 " (%x:%x:%x.%x) %n",
1346 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001347 switch (err) {
1348 case 3:
1349 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001350 sscanf(pci_devs_to_hide + pos,
1351 " (%x:%x:%x.*) %n",
1352 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001353 break;
1354 case 2:
1355 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001356 sscanf(pci_devs_to_hide + pos,
1357 " (%x:%x:*.*) %n",
1358 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001359 break;
1360 }
1361
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001362 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001363 domain = 0;
1364 err = sscanf(pci_devs_to_hide + pos,
1365 " (%x:%x.%x) %n",
1366 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001367 switch (err) {
1368 case 2:
1369 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001370 sscanf(pci_devs_to_hide + pos,
1371 " (%x:%x.*) %n",
1372 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001373 break;
1374 case 1:
1375 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001376 sscanf(pci_devs_to_hide + pos,
1377 " (%x:*.*) %n",
1378 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001379 break;
1380 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001381 }
1382
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001383 if (parsed <= 0)
1384 goto parse_error;
1385
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001386 err = pcistub_device_id_add(domain, bus, slot, func);
1387 if (err)
1388 goto out;
1389
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001390 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001391 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001392 }
1393
1394 /* If we're the first PCI Device Driver to register, we're the
1395 * first one to get offered PCI devices as they become
1396 * available (and thus we can be the first to grab them)
1397 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001398 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001399 if (err < 0)
1400 goto out;
1401
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001402 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001403 &driver_attr_new_slot);
1404 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001405 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001406 &driver_attr_remove_slot);
1407 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001408 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001409 &driver_attr_slots);
1410 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001411 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001412 &driver_attr_quirks);
1413 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001414 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001415 &driver_attr_permissive);
1416
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001417 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001418 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001419 &driver_attr_irq_handlers);
1420 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001421 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001422 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001423 if (err)
1424 pcistub_exit();
1425
1426out:
1427 return err;
1428
1429parse_error:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001430 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001431 pci_devs_to_hide + pos);
1432 return -EINVAL;
1433}
1434
1435#ifndef MODULE
1436/*
1437 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001438 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001439 * want to suck up any device before other drivers
1440 * get a chance by being the first pci device
1441 * driver to register)
1442 */
1443fs_initcall(pcistub_init);
1444#endif
1445
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001446static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001447{
1448 int err;
1449
1450 if (!xen_initial_domain())
1451 return -ENODEV;
1452
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001453 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001454 if (err)
1455 return err;
1456
1457#ifdef MODULE
1458 err = pcistub_init();
1459 if (err < 0)
1460 return err;
1461#endif
1462
1463 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001464 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001465 if (err)
1466 pcistub_exit();
1467
1468 return err;
1469}
1470
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001471static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001472{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001473 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001474 pcistub_exit();
1475}
1476
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001477module_init(xen_pcibk_init);
1478module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001479
1480MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001481MODULE_ALIAS("xen-backend:pci");