blob: 129e1674f4aa95085fa7b8b20b4079604e1c0eb0 [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
Jan Beulichb3e40b72012-11-02 14:37:13 +0000145 && slot == PCI_SLOT(psdev->dev->devfn)
146 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400147 pcistub_device_get(psdev);
148 goto out;
149 }
150 }
151
152 /* didn't find it */
153 psdev = NULL;
154
155out:
156 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
157 return psdev;
158}
159
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400160static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400161 struct pcistub_device *psdev)
162{
163 struct pci_dev *pci_dev = NULL;
164 unsigned long flags;
165
166 pcistub_device_get(psdev);
167
168 spin_lock_irqsave(&psdev->lock, flags);
169 if (!psdev->pdev) {
170 psdev->pdev = pdev;
171 pci_dev = psdev->dev;
172 }
173 spin_unlock_irqrestore(&psdev->lock, flags);
174
175 if (!pci_dev)
176 pcistub_device_put(psdev);
177
178 return pci_dev;
179}
180
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400181struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400182 int domain, int bus,
183 int slot, int func)
184{
185 struct pcistub_device *psdev;
186 struct pci_dev *found_dev = NULL;
187 unsigned long flags;
188
189 spin_lock_irqsave(&pcistub_devices_lock, flags);
190
191 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
192 if (psdev->dev != NULL
193 && domain == pci_domain_nr(psdev->dev->bus)
194 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000195 && slot == PCI_SLOT(psdev->dev->devfn)
196 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400197 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
198 break;
199 }
200 }
201
202 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
203 return found_dev;
204}
205
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400206struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400207 struct pci_dev *dev)
208{
209 struct pcistub_device *psdev;
210 struct pci_dev *found_dev = NULL;
211 unsigned long flags;
212
213 spin_lock_irqsave(&pcistub_devices_lock, flags);
214
215 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
216 if (psdev->dev == dev) {
217 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
218 break;
219 }
220 }
221
222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
223 return found_dev;
224}
225
226void pcistub_put_pci_dev(struct pci_dev *dev)
227{
228 struct pcistub_device *psdev, *found_psdev = NULL;
229 unsigned long flags;
230
231 spin_lock_irqsave(&pcistub_devices_lock, flags);
232
233 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
234 if (psdev->dev == dev) {
235 found_psdev = psdev;
236 break;
237 }
238 }
239
240 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400241 if (WARN_ON(!found_psdev))
242 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400243
244 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400245 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400246 */
247 down_write(&pcistub_sem);
248 /* Cleanup our device
249 * (so it's ready for the next domain)
250 */
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500251
252 /* This is OK - we are running from workqueue context
253 * and want to inhibit the user from fiddling with 'reset'
254 */
255 pci_reset_function(dev);
256 pci_restore_state(psdev->dev);
257
258 /* This disables the device. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400259 xen_pcibk_reset_device(found_psdev->dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500260
261 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400262 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
263 xen_pcibk_config_reset_dev(found_psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400264
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500265 xen_unregister_device_domain_owner(found_psdev->dev);
266
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400267 spin_lock_irqsave(&found_psdev->lock, flags);
268 found_psdev->pdev = NULL;
269 spin_unlock_irqrestore(&found_psdev->lock, flags);
270
271 pcistub_device_put(found_psdev);
272 up_write(&pcistub_sem);
273}
274
275static int __devinit pcistub_match_one(struct pci_dev *dev,
276 struct pcistub_device_id *pdev_id)
277{
278 /* Match the specified device by domain, bus, slot, func and also if
279 * any of the device's parent bridges match.
280 */
281 for (; dev != NULL; dev = dev->bus->self) {
282 if (pci_domain_nr(dev->bus) == pdev_id->domain
283 && dev->bus->number == pdev_id->bus
284 && dev->devfn == pdev_id->devfn)
285 return 1;
286
287 /* Sometimes topmost bridge links to itself. */
288 if (dev == dev->bus->self)
289 break;
290 }
291
292 return 0;
293}
294
295static int __devinit pcistub_match(struct pci_dev *dev)
296{
297 struct pcistub_device_id *pdev_id;
298 unsigned long flags;
299 int found = 0;
300
301 spin_lock_irqsave(&device_ids_lock, flags);
302 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
303 if (pcistub_match_one(dev, pdev_id)) {
304 found = 1;
305 break;
306 }
307 }
308 spin_unlock_irqrestore(&device_ids_lock, flags);
309
310 return found;
311}
312
313static int __devinit pcistub_init_device(struct pci_dev *dev)
314{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400315 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400316 int err = 0;
317
318 dev_dbg(&dev->dev, "initializing...\n");
319
320 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400321 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400322 * would need to be called somewhere to free the memory allocated
323 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
324 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400325 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
326 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400327 if (!dev_data) {
328 err = -ENOMEM;
329 goto out;
330 }
331 pci_set_drvdata(dev, dev_data);
332
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400333 /*
334 * Setup name for fake IRQ handler. It will only be enabled
335 * once the device is turned on by the guest.
336 */
337 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
338
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400339 dev_dbg(&dev->dev, "initializing config\n");
340
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400341 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
342 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400343 if (err)
344 goto out;
345
346 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
347 * must do this here because pcibios_enable_device may specify
348 * the pci device's true irq (and possibly its other resources)
349 * if they differ from what's in the configuration space.
350 * This makes the assumption that the device's resources won't
351 * change after this point (otherwise this code may break!)
352 */
353 dev_dbg(&dev->dev, "enabling device\n");
354 err = pci_enable_device(dev);
355 if (err)
356 goto config_release;
357
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500358 /* We need the device active to save the state. */
359 dev_dbg(&dev->dev, "save state of device\n");
360 pci_save_state(dev);
361 dev_data->pci_saved_state = pci_store_saved_state(dev);
362 if (!dev_data->pci_saved_state)
363 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400364 else {
365 dev_dbg(&dev->dev, "reseting (FLR, D3, etc) the device\n");
366 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400367 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400368 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400369 /* Now disable the device (this also ensures some private device
370 * data is setup before we export)
371 */
372 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400373 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400374
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500375 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400376 return 0;
377
378config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400379 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400380
381out:
382 pci_set_drvdata(dev, NULL);
383 kfree(dev_data);
384 return err;
385}
386
387/*
388 * Because some initialization still happens on
389 * devices during fs_initcall, we need to defer
390 * full initialization of our devices until
391 * device_initcall.
392 */
393static int __init pcistub_init_devices_late(void)
394{
395 struct pcistub_device *psdev;
396 unsigned long flags;
397 int err = 0;
398
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400399 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400400
401 spin_lock_irqsave(&pcistub_devices_lock, flags);
402
403 while (!list_empty(&seized_devices)) {
404 psdev = container_of(seized_devices.next,
405 struct pcistub_device, dev_list);
406 list_del(&psdev->dev_list);
407
408 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
409
410 err = pcistub_init_device(psdev->dev);
411 if (err) {
412 dev_err(&psdev->dev->dev,
413 "error %d initializing device\n", err);
414 kfree(psdev);
415 psdev = NULL;
416 }
417
418 spin_lock_irqsave(&pcistub_devices_lock, flags);
419
420 if (psdev)
421 list_add_tail(&psdev->dev_list, &pcistub_devices);
422 }
423
424 initialize_devices = 1;
425
426 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
427
428 return 0;
429}
430
431static int __devinit pcistub_seize(struct pci_dev *dev)
432{
433 struct pcistub_device *psdev;
434 unsigned long flags;
435 int err = 0;
436
437 psdev = pcistub_device_alloc(dev);
438 if (!psdev)
439 return -ENOMEM;
440
441 spin_lock_irqsave(&pcistub_devices_lock, flags);
442
443 if (initialize_devices) {
444 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
445
446 /* don't want irqs disabled when calling pcistub_init_device */
447 err = pcistub_init_device(psdev->dev);
448
449 spin_lock_irqsave(&pcistub_devices_lock, flags);
450
451 if (!err)
452 list_add(&psdev->dev_list, &pcistub_devices);
453 } else {
454 dev_dbg(&dev->dev, "deferring initialization\n");
455 list_add(&psdev->dev_list, &seized_devices);
456 }
457
458 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
459
460 if (err)
461 pcistub_device_put(psdev);
462
463 return err;
464}
465
466static int __devinit pcistub_probe(struct pci_dev *dev,
467 const struct pci_device_id *id)
468{
469 int err = 0;
470
471 dev_dbg(&dev->dev, "probing...\n");
472
473 if (pcistub_match(dev)) {
474
475 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
476 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
477 dev_err(&dev->dev, "can't export pci devices that "
478 "don't have a normal (0) or bridge (1) "
479 "header type!\n");
480 err = -ENODEV;
481 goto out;
482 }
483
484 dev_info(&dev->dev, "seizing device\n");
485 err = pcistub_seize(dev);
486 } else
487 /* Didn't find the device */
488 err = -ENODEV;
489
490out:
491 return err;
492}
493
494static void pcistub_remove(struct pci_dev *dev)
495{
496 struct pcistub_device *psdev, *found_psdev = NULL;
497 unsigned long flags;
498
499 dev_dbg(&dev->dev, "removing\n");
500
501 spin_lock_irqsave(&pcistub_devices_lock, flags);
502
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400503 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400504
505 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
506 if (psdev->dev == dev) {
507 found_psdev = psdev;
508 break;
509 }
510 }
511
512 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
513
514 if (found_psdev) {
515 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
516 found_psdev->pdev);
517
518 if (found_psdev->pdev) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400519 printk(KERN_WARNING DRV_NAME ": ****** removing device "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400520 "%s while still in-use! ******\n",
521 pci_name(found_psdev->dev));
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400522 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
523 " still access this device's i/o resources!\n");
524 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400525 "domain before binding device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400526 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400527 "or domains\n");
528
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400529 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400530 found_psdev->dev);
531 }
532
533 spin_lock_irqsave(&pcistub_devices_lock, flags);
534 list_del(&found_psdev->dev_list);
535 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
536
537 /* the final put for releasing from the list */
538 pcistub_device_put(found_psdev);
539 }
540}
541
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400542static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400543 {
544 .vendor = PCI_ANY_ID,
545 .device = PCI_ANY_ID,
546 .subvendor = PCI_ANY_ID,
547 .subdevice = PCI_ANY_ID,
548 },
549 {0,},
550};
551
552#define PCI_NODENAME_MAX 40
553static void kill_domain_by_device(struct pcistub_device *psdev)
554{
555 struct xenbus_transaction xbt;
556 int err;
557 char nodename[PCI_NODENAME_MAX];
558
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400559 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400560 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
561 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400562
563again:
564 err = xenbus_transaction_start(&xbt);
565 if (err) {
566 dev_err(&psdev->dev->dev,
567 "error %d when start xenbus transaction\n", err);
568 return;
569 }
570 /*PV AER handlers will set this flag*/
571 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
572 err = xenbus_transaction_end(xbt, 0);
573 if (err) {
574 if (err == -EAGAIN)
575 goto again;
576 dev_err(&psdev->dev->dev,
577 "error %d when end xenbus transaction\n", err);
578 return;
579 }
580}
581
582/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400583 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400584 * jobs: send service request and waiting for front_end response.
585*/
586static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400587 pci_channel_state_t state, int aer_cmd,
588 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400589{
590 pci_ers_result_t res = result;
591 struct xen_pcie_aer_op *aer_op;
592 int ret;
593
594 /*with PV AER drivers*/
595 aer_op = &(psdev->pdev->sh_info->aer_op);
596 aer_op->cmd = aer_cmd ;
597 /*useful for error_detected callback*/
598 aer_op->err = state;
599 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400600 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400601 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
602 if (!ret) {
603 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400604 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400605 return PCI_ERS_RESULT_NONE;
606 }
607 wmb();
608
609 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400610 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400611 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400612 /*local flag to mark there's aer request, xen_pcibk callback will use
613 * this flag to judge whether we need to check pci-front give aer
614 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400615 */
616 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
617
618 /*It is possible that a pcifront conf_read_write ops request invokes
619 * the callback which cause the spurious execution of wake_up.
620 * Yet it is harmless and better than a spinlock here
621 */
622 set_bit(_XEN_PCIB_active,
623 (unsigned long *)&psdev->pdev->sh_info->flags);
624 wmb();
625 notify_remote_via_irq(psdev->pdev->evtchn_irq);
626
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400627 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
628 !(test_bit(_XEN_PCIB_active, (unsigned long *)
629 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400630
631 if (!ret) {
632 if (test_bit(_XEN_PCIB_active,
633 (unsigned long *)&psdev->pdev->sh_info->flags)) {
634 dev_err(&psdev->dev->dev,
635 "pcifront aer process not responding!\n");
636 clear_bit(_XEN_PCIB_active,
637 (unsigned long *)&psdev->pdev->sh_info->flags);
638 aer_op->err = PCI_ERS_RESULT_NONE;
639 return res;
640 }
641 }
642 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
643
644 if (test_bit(_XEN_PCIF_active,
645 (unsigned long *)&psdev->pdev->sh_info->flags)) {
646 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400647 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400648 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400649 }
650
651 res = (pci_ers_result_t)aer_op->err;
652 return res;
653}
654
655/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400656* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400657* of the device driver could provide this service, and then wait for pcifront
658* ack.
659* @dev: pointer to PCI devices
660* return value is used by aer_core do_recovery policy
661*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400662static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400663{
664 struct pcistub_device *psdev;
665 pci_ers_result_t result;
666
667 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400668 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400669 dev->bus->number, dev->devfn);
670
671 down_write(&pcistub_sem);
672 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
673 dev->bus->number,
674 PCI_SLOT(dev->devfn),
675 PCI_FUNC(dev->devfn));
676
677 if (!psdev || !psdev->pdev) {
678 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400679 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400680 goto end;
681 }
682
683 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400684 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400685 " by HVM, kill it\n");
686 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100687 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400688 }
689
690 if (!test_bit(_XEN_PCIB_AERHANDLER,
691 (unsigned long *)&psdev->pdev->sh_info->flags)) {
692 dev_err(&dev->dev,
693 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100694 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400695 }
696 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
697
698 if (result == PCI_ERS_RESULT_NONE ||
699 result == PCI_ERS_RESULT_DISCONNECT) {
700 dev_dbg(&dev->dev,
701 "No AER slot_reset service or disconnected!\n");
702 kill_domain_by_device(psdev);
703 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400704end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100705 if (psdev)
706 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400707 up_write(&pcistub_sem);
708 return result;
709
710}
711
712
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400713/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400714* in case of the device driver could provide this service, and then wait
715* for pcifront ack
716* @dev: pointer to PCI devices
717* return value is used by aer_core do_recovery policy
718*/
719
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400720static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400721{
722 struct pcistub_device *psdev;
723 pci_ers_result_t result;
724
725 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400726 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400727 dev->bus->number, dev->devfn);
728
729 down_write(&pcistub_sem);
730 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
731 dev->bus->number,
732 PCI_SLOT(dev->devfn),
733 PCI_FUNC(dev->devfn));
734
735 if (!psdev || !psdev->pdev) {
736 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400737 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400738 goto end;
739 }
740
741 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400742 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400743 " by HVM, kill it\n");
744 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100745 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400746 }
747
748 if (!test_bit(_XEN_PCIB_AERHANDLER,
749 (unsigned long *)&psdev->pdev->sh_info->flags)) {
750 dev_err(&dev->dev,
751 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100752 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400753 }
754 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
755
756 if (result == PCI_ERS_RESULT_NONE ||
757 result == PCI_ERS_RESULT_DISCONNECT) {
758 dev_dbg(&dev->dev,
759 "No AER mmio_enabled service or disconnected!\n");
760 kill_domain_by_device(psdev);
761 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400762end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100763 if (psdev)
764 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400765 up_write(&pcistub_sem);
766 return result;
767}
768
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400769/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400770* in case of the device driver could provide this service, and then wait
771* for pcifront ack.
772* @dev: pointer to PCI devices
773* @error: the current PCI connection state
774* return value is used by aer_core do_recovery policy
775*/
776
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400777static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400778 pci_channel_state_t error)
779{
780 struct pcistub_device *psdev;
781 pci_ers_result_t result;
782
783 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400784 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400785 dev->bus->number, dev->devfn);
786
787 down_write(&pcistub_sem);
788 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
789 dev->bus->number,
790 PCI_SLOT(dev->devfn),
791 PCI_FUNC(dev->devfn));
792
793 if (!psdev || !psdev->pdev) {
794 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400795 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400796 goto end;
797 }
798
799 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400800 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400801 " by HVM, kill it\n");
802 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100803 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400804 }
805
806 /*Guest owns the device yet no aer handler regiested, kill guest*/
807 if (!test_bit(_XEN_PCIB_AERHANDLER,
808 (unsigned long *)&psdev->pdev->sh_info->flags)) {
809 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
810 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100811 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400812 }
813 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
814
815 if (result == PCI_ERS_RESULT_NONE ||
816 result == PCI_ERS_RESULT_DISCONNECT) {
817 dev_dbg(&dev->dev,
818 "No AER error_detected service or disconnected!\n");
819 kill_domain_by_device(psdev);
820 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400821end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100822 if (psdev)
823 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400824 up_write(&pcistub_sem);
825 return result;
826}
827
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400828/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400829* in case of the device driver could provide this service, and then wait
830* for pcifront ack.
831* @dev: pointer to PCI devices
832*/
833
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400834static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400835{
836 struct pcistub_device *psdev;
837
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400838 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400839 dev->bus->number, dev->devfn);
840
841 down_write(&pcistub_sem);
842 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
843 dev->bus->number,
844 PCI_SLOT(dev->devfn),
845 PCI_FUNC(dev->devfn));
846
847 if (!psdev || !psdev->pdev) {
848 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400849 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400850 goto end;
851 }
852
853 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400854 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400855 " by HVM, kill it\n");
856 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100857 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400858 }
859
860 if (!test_bit(_XEN_PCIB_AERHANDLER,
861 (unsigned long *)&psdev->pdev->sh_info->flags)) {
862 dev_err(&dev->dev,
863 "guest with no AER driver should have been killed\n");
864 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100865 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400866 }
867 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
868 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400869end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100870 if (psdev)
871 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400872 up_write(&pcistub_sem);
873 return;
874}
875
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400876/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700877static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400878 .error_detected = xen_pcibk_error_detected,
879 .mmio_enabled = xen_pcibk_mmio_enabled,
880 .slot_reset = xen_pcibk_slot_reset,
881 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400882};
883
884/*
885 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
886 * for a normal device. I don't want it to be loaded automatically.
887 */
888
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400889static struct pci_driver xen_pcibk_pci_driver = {
890 /* The name should be xen_pciback, but until the tools are updated
891 * we will keep it as pciback. */
892 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400893 .id_table = pcistub_ids,
894 .probe = pcistub_probe,
895 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400896 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400897};
898
899static inline int str_to_slot(const char *buf, int *domain, int *bus,
900 int *slot, int *func)
901{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000902 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400903
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000904 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
905 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100906 case 3:
907 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000908 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100909 break;
910 case 2:
911 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000912 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100913 break;
914 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000915 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400916 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400917
918 /* try again without domain */
919 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000920 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100921 case 2:
922 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000923 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100924 break;
925 case 1:
926 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000927 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100928 break;
929 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000930 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400931 return 0;
932
933 return -EINVAL;
934}
935
936static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
937 *slot, int *func, int *reg, int *size, int *mask)
938{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000939 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400940
Jan Beulichb3e40b72012-11-02 14:37:13 +0000941 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000942 reg, size, mask, &parsed);
943 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400944 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000945
946 /* try again without domain */
947 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000948 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000949 mask, &parsed);
950 if (parsed && !buf[parsed])
951 return 0;
952
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400953 return -EINVAL;
954}
955
956static int pcistub_device_id_add(int domain, int bus, int slot, int func)
957{
958 struct pcistub_device_id *pci_dev_id;
959 unsigned long flags;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000960 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100961
962 if (slot < 0) {
963 for (slot = 0; !rc && slot < 32; ++slot)
964 rc = pcistub_device_id_add(domain, bus, slot, func);
965 return rc;
966 }
967
968 if (func < 0) {
969 for (func = 0; !rc && func < 8; ++func)
970 rc = pcistub_device_id_add(domain, bus, slot, func);
971 return rc;
972 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400973
Jan Beulichb3e40b72012-11-02 14:37:13 +0000974 if ((
975#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
976 || !defined(CONFIG_PCI_DOMAINS)
977 !pci_domains_supported ? domain :
978#endif
979 domain < 0 || domain > 0xffff)
980 || bus < 0 || bus > 0xff
981 || PCI_SLOT(devfn) != slot
982 || PCI_FUNC(devfn) != func)
983 return -EINVAL;
984
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400985 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
986 if (!pci_dev_id)
987 return -ENOMEM;
988
989 pci_dev_id->domain = domain;
990 pci_dev_id->bus = bus;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000991 pci_dev_id->devfn = devfn;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400992
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500993 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400994 domain, bus, slot, func);
995
996 spin_lock_irqsave(&device_ids_lock, flags);
997 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
998 spin_unlock_irqrestore(&device_ids_lock, flags);
999
1000 return 0;
1001}
1002
1003static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1004{
1005 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001006 int err = -ENOENT;
1007 unsigned long flags;
1008
1009 spin_lock_irqsave(&device_ids_lock, flags);
1010 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1011 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001012 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1013 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1014 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001015 /* Don't break; here because it's possible the same
1016 * slot could be in the list more than once
1017 */
1018 list_del(&pci_dev_id->slot_list);
1019 kfree(pci_dev_id);
1020
1021 err = 0;
1022
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001023 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001024 "seize list\n", domain, bus, slot, func);
1025 }
1026 }
1027 spin_unlock_irqrestore(&device_ids_lock, flags);
1028
1029 return err;
1030}
1031
Jan Beulichb3e40b72012-11-02 14:37:13 +00001032static int pcistub_reg_add(int domain, int bus, int slot, int func,
1033 unsigned int reg, unsigned int size,
1034 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001035{
1036 int err = 0;
1037 struct pcistub_device *psdev;
1038 struct pci_dev *dev;
1039 struct config_field *field;
1040
Jan Beulichb3e40b72012-11-02 14:37:13 +00001041 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1042 return -EINVAL;
1043
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001044 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001045 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001046 err = -ENODEV;
1047 goto out;
1048 }
1049 dev = psdev->dev;
1050
1051 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1052 if (!field) {
1053 err = -ENOMEM;
1054 goto out;
1055 }
1056
1057 field->offset = reg;
1058 field->size = size;
1059 field->mask = mask;
1060 field->init = NULL;
1061 field->reset = NULL;
1062 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001063 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001064
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001065 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001066 if (err)
1067 kfree(field);
1068out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001069 if (psdev)
1070 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001071 return err;
1072}
1073
1074static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1075 size_t count)
1076{
1077 int domain, bus, slot, func;
1078 int err;
1079
1080 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1081 if (err)
1082 goto out;
1083
1084 err = pcistub_device_id_add(domain, bus, slot, func);
1085
1086out:
1087 if (!err)
1088 err = count;
1089 return err;
1090}
Jan Beulich402c5e12011-09-21 16:22:11 -04001091static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001092
1093static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1094 size_t count)
1095{
1096 int domain, bus, slot, func;
1097 int err;
1098
1099 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1100 if (err)
1101 goto out;
1102
1103 err = pcistub_device_id_remove(domain, bus, slot, func);
1104
1105out:
1106 if (!err)
1107 err = count;
1108 return err;
1109}
Jan Beulich402c5e12011-09-21 16:22:11 -04001110static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001111
1112static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1113{
1114 struct pcistub_device_id *pci_dev_id;
1115 size_t count = 0;
1116 unsigned long flags;
1117
1118 spin_lock_irqsave(&device_ids_lock, flags);
1119 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1120 if (count >= PAGE_SIZE)
1121 break;
1122
1123 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001124 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001125 pci_dev_id->domain, pci_dev_id->bus,
1126 PCI_SLOT(pci_dev_id->devfn),
1127 PCI_FUNC(pci_dev_id->devfn));
1128 }
1129 spin_unlock_irqrestore(&device_ids_lock, flags);
1130
1131 return count;
1132}
Jan Beulich402c5e12011-09-21 16:22:11 -04001133static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001134
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001135static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1136{
1137 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001138 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001139 size_t count = 0;
1140 unsigned long flags;
1141
1142 spin_lock_irqsave(&pcistub_devices_lock, flags);
1143 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1144 if (count >= PAGE_SIZE)
1145 break;
1146 if (!psdev->dev)
1147 continue;
1148 dev_data = pci_get_drvdata(psdev->dev);
1149 if (!dev_data)
1150 continue;
1151 count +=
1152 scnprintf(buf + count, PAGE_SIZE - count,
1153 "%s:%s:%sing:%ld\n",
1154 pci_name(psdev->dev),
1155 dev_data->isr_on ? "on" : "off",
1156 dev_data->ack_intr ? "ack" : "not ack",
1157 dev_data->handled);
1158 }
1159 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1160 return count;
1161}
Jan Beulich402c5e12011-09-21 16:22:11 -04001162static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001163
1164static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1165 const char *buf,
1166 size_t count)
1167{
1168 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001169 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001170 int domain, bus, slot, func;
1171 int err = -ENOENT;
1172
1173 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1174 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001175 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001176
1177 psdev = pcistub_device_find(domain, bus, slot, func);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001178 if (!psdev)
1179 goto out;
1180
1181 dev_data = pci_get_drvdata(psdev->dev);
1182 if (!dev_data)
1183 goto out;
1184
1185 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1186 dev_data->irq_name, dev_data->isr_on,
1187 !dev_data->isr_on);
1188
1189 dev_data->isr_on = !(dev_data->isr_on);
1190 if (dev_data->isr_on)
1191 dev_data->ack_intr = 1;
1192out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001193 if (psdev)
1194 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001195 if (!err)
1196 err = count;
1197 return err;
1198}
Jan Beulich402c5e12011-09-21 16:22:11 -04001199static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1200 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001201
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001202static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1203 size_t count)
1204{
1205 int domain, bus, slot, func, reg, size, mask;
1206 int err;
1207
1208 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1209 &mask);
1210 if (err)
1211 goto out;
1212
1213 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1214
1215out:
1216 if (!err)
1217 err = count;
1218 return err;
1219}
1220
1221static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1222{
1223 int count = 0;
1224 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001225 struct xen_pcibk_config_quirk *quirk;
1226 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001227 const struct config_field *field;
1228 const struct config_field_entry *cfg_entry;
1229
1230 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001231 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001232 if (count >= PAGE_SIZE)
1233 goto out;
1234
1235 count += scnprintf(buf + count, PAGE_SIZE - count,
1236 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1237 quirk->pdev->bus->number,
1238 PCI_SLOT(quirk->pdev->devfn),
1239 PCI_FUNC(quirk->pdev->devfn),
1240 quirk->devid.vendor, quirk->devid.device,
1241 quirk->devid.subvendor,
1242 quirk->devid.subdevice);
1243
1244 dev_data = pci_get_drvdata(quirk->pdev);
1245
1246 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1247 field = cfg_entry->field;
1248 if (count >= PAGE_SIZE)
1249 goto out;
1250
1251 count += scnprintf(buf + count, PAGE_SIZE - count,
1252 "\t\t%08x:%01x:%08x\n",
1253 cfg_entry->base_offset +
1254 field->offset, field->size,
1255 field->mask);
1256 }
1257 }
1258
1259out:
1260 spin_unlock_irqrestore(&device_ids_lock, flags);
1261
1262 return count;
1263}
Jan Beulich402c5e12011-09-21 16:22:11 -04001264static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1265 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001266
1267static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1268 size_t count)
1269{
1270 int domain, bus, slot, func;
1271 int err;
1272 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001273 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001274
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001275 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1276 if (err)
1277 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001278
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001279 psdev = pcistub_device_find(domain, bus, slot, func);
1280 if (!psdev) {
1281 err = -ENODEV;
1282 goto out;
1283 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001284
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001285 dev_data = pci_get_drvdata(psdev->dev);
1286 /* the driver data for a device should never be null at this point */
1287 if (!dev_data) {
1288 err = -ENXIO;
1289 goto release;
1290 }
1291 if (!dev_data->permissive) {
1292 dev_data->permissive = 1;
1293 /* Let user know that what they're doing could be unsafe */
1294 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1295 "configuration space accesses!\n");
1296 dev_warn(&psdev->dev->dev,
1297 "permissive mode is potentially unsafe!\n");
1298 }
1299release:
1300 pcistub_device_put(psdev);
1301out:
1302 if (!err)
1303 err = count;
1304 return err;
1305}
1306
1307static ssize_t permissive_show(struct device_driver *drv, char *buf)
1308{
1309 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001310 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001311 size_t count = 0;
1312 unsigned long flags;
1313 spin_lock_irqsave(&pcistub_devices_lock, flags);
1314 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1315 if (count >= PAGE_SIZE)
1316 break;
1317 if (!psdev->dev)
1318 continue;
1319 dev_data = pci_get_drvdata(psdev->dev);
1320 if (!dev_data || !dev_data->permissive)
1321 continue;
1322 count +=
1323 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1324 pci_name(psdev->dev));
1325 }
1326 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1327 return count;
1328}
Jan Beulich402c5e12011-09-21 16:22:11 -04001329static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1330 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001331
1332static void pcistub_exit(void)
1333{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001334 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1335 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001336 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001337 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1338 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1339 driver_remove_file(&xen_pcibk_pci_driver.driver,
1340 &driver_attr_permissive);
1341 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001342 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001343 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001344 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001345 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001346}
1347
1348static int __init pcistub_init(void)
1349{
1350 int pos = 0;
1351 int err = 0;
1352 int domain, bus, slot, func;
1353 int parsed;
1354
1355 if (pci_devs_to_hide && *pci_devs_to_hide) {
1356 do {
1357 parsed = 0;
1358
1359 err = sscanf(pci_devs_to_hide + pos,
1360 " (%x:%x:%x.%x) %n",
1361 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001362 switch (err) {
1363 case 3:
1364 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001365 sscanf(pci_devs_to_hide + pos,
1366 " (%x:%x:%x.*) %n",
1367 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001368 break;
1369 case 2:
1370 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001371 sscanf(pci_devs_to_hide + pos,
1372 " (%x:%x:*.*) %n",
1373 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001374 break;
1375 }
1376
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001377 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001378 domain = 0;
1379 err = sscanf(pci_devs_to_hide + pos,
1380 " (%x:%x.%x) %n",
1381 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001382 switch (err) {
1383 case 2:
1384 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001385 sscanf(pci_devs_to_hide + pos,
1386 " (%x:%x.*) %n",
1387 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001388 break;
1389 case 1:
1390 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001391 sscanf(pci_devs_to_hide + pos,
1392 " (%x:*.*) %n",
1393 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001394 break;
1395 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001396 }
1397
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001398 if (parsed <= 0)
1399 goto parse_error;
1400
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001401 err = pcistub_device_id_add(domain, bus, slot, func);
1402 if (err)
1403 goto out;
1404
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001405 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001406 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001407 }
1408
1409 /* If we're the first PCI Device Driver to register, we're the
1410 * first one to get offered PCI devices as they become
1411 * available (and thus we can be the first to grab them)
1412 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001413 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001414 if (err < 0)
1415 goto out;
1416
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001417 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001418 &driver_attr_new_slot);
1419 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001420 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001421 &driver_attr_remove_slot);
1422 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001423 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001424 &driver_attr_slots);
1425 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001426 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001427 &driver_attr_quirks);
1428 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001429 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001430 &driver_attr_permissive);
1431
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001432 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001433 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001434 &driver_attr_irq_handlers);
1435 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001436 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001437 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001438 if (err)
1439 pcistub_exit();
1440
1441out:
1442 return err;
1443
1444parse_error:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001445 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001446 pci_devs_to_hide + pos);
1447 return -EINVAL;
1448}
1449
1450#ifndef MODULE
1451/*
1452 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001453 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001454 * want to suck up any device before other drivers
1455 * get a chance by being the first pci device
1456 * driver to register)
1457 */
1458fs_initcall(pcistub_init);
1459#endif
1460
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001461static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001462{
1463 int err;
1464
1465 if (!xen_initial_domain())
1466 return -ENODEV;
1467
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001468 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001469 if (err)
1470 return err;
1471
1472#ifdef MODULE
1473 err = pcistub_init();
1474 if (err < 0)
1475 return err;
1476#endif
1477
1478 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001479 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001480 if (err)
1481 pcistub_exit();
1482
1483 return err;
1484}
1485
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001486static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001487{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001488 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001489 pcistub_exit();
1490}
1491
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001492module_init(xen_pcibk_init);
1493module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001494
1495MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001496MODULE_ALIAS("xen-backend:pci");