blob: 0020899182604cdf72548e66c41c2582c76b3bd9 [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>
Jan Beulich909b3fd2013-03-12 15:06:23 +000020#include <xen/interface/physdev.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040021#include "pciback.h"
22#include "conf_space.h"
23#include "conf_space_quirks.h"
24
25static char *pci_devs_to_hide;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040026wait_queue_head_t xen_pcibk_aer_wait_queue;
27/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
28* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040029*/
30static DECLARE_RWSEM(pcistub_sem);
31module_param_named(hide, pci_devs_to_hide, charp, 0444);
32
33struct pcistub_device_id {
34 struct list_head slot_list;
35 int domain;
36 unsigned char bus;
37 unsigned int devfn;
38};
39static LIST_HEAD(pcistub_device_ids);
40static DEFINE_SPINLOCK(device_ids_lock);
41
42struct pcistub_device {
43 struct kref kref;
44 struct list_head dev_list;
45 spinlock_t lock;
46
47 struct pci_dev *dev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040048 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040049};
50
51/* Access to pcistub_devices & seized_devices lists and the initialize_devices
52 * flag must be locked with pcistub_devices_lock
53 */
54static DEFINE_SPINLOCK(pcistub_devices_lock);
55static LIST_HEAD(pcistub_devices);
56
57/* wait for device_initcall before initializing our devices
58 * (see pcistub_init_devices_late)
59 */
60static int initialize_devices;
61static LIST_HEAD(seized_devices);
62
63static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
64{
65 struct pcistub_device *psdev;
66
67 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
68
69 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
70 if (!psdev)
71 return NULL;
72
73 psdev->dev = pci_dev_get(dev);
74 if (!psdev->dev) {
75 kfree(psdev);
76 return NULL;
77 }
78
79 kref_init(&psdev->kref);
80 spin_lock_init(&psdev->lock);
81
82 return psdev;
83}
84
85/* Don't call this directly as it's called by pcistub_device_put */
86static void pcistub_device_release(struct kref *kref)
87{
88 struct pcistub_device *psdev;
Jan Beulich909b3fd2013-03-12 15:06:23 +000089 struct pci_dev *dev;
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050090 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040091
92 psdev = container_of(kref, struct pcistub_device, kref);
Jan Beulich909b3fd2013-03-12 15:06:23 +000093 dev = psdev->dev;
94 dev_data = pci_get_drvdata(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040095
Jan Beulich909b3fd2013-03-12 15:06:23 +000096 dev_dbg(&dev->dev, "pcistub_device_release\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040097
Jan Beulich909b3fd2013-03-12 15:06:23 +000098 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -050099
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500100 /* Call the reset function which does not take lock as this
101 * is called from "unbind" which takes a device_lock mutex.
102 */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000103 __pci_reset_function_locked(dev);
104 if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
105 dev_dbg(&dev->dev, "Could not reload PCI state\n");
106 else
107 pci_restore_state(dev);
108
Jan Beulichd69c0e32013-05-29 13:31:15 +0100109 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000110 struct physdev_pci_device ppdev = {
111 .seg = pci_domain_nr(dev->bus),
112 .bus = dev->bus->number,
113 .devfn = dev->devfn
114 };
115 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
116 &ppdev);
117
118 if (err)
119 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
120 err);
121 }
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500122
123 /* Disable the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000124 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500125
126 kfree(dev_data);
Jan Beulich909b3fd2013-03-12 15:06:23 +0000127 pci_set_drvdata(dev, NULL);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500128
129 /* Clean-up the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000130 xen_pcibk_config_free_dyn_fields(dev);
131 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400132
Jan Beulich909b3fd2013-03-12 15:06:23 +0000133 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
134 pci_dev_put(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400135
136 kfree(psdev);
137}
138
139static inline void pcistub_device_get(struct pcistub_device *psdev)
140{
141 kref_get(&psdev->kref);
142}
143
144static inline void pcistub_device_put(struct pcistub_device *psdev)
145{
146 kref_put(&psdev->kref, pcistub_device_release);
147}
148
149static struct pcistub_device *pcistub_device_find(int domain, int bus,
150 int slot, int func)
151{
152 struct pcistub_device *psdev = NULL;
153 unsigned long flags;
154
155 spin_lock_irqsave(&pcistub_devices_lock, flags);
156
157 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
158 if (psdev->dev != NULL
159 && domain == pci_domain_nr(psdev->dev->bus)
160 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000161 && slot == PCI_SLOT(psdev->dev->devfn)
162 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400163 pcistub_device_get(psdev);
164 goto out;
165 }
166 }
167
168 /* didn't find it */
169 psdev = NULL;
170
171out:
172 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
173 return psdev;
174}
175
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400176static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400177 struct pcistub_device *psdev)
178{
179 struct pci_dev *pci_dev = NULL;
180 unsigned long flags;
181
182 pcistub_device_get(psdev);
183
184 spin_lock_irqsave(&psdev->lock, flags);
185 if (!psdev->pdev) {
186 psdev->pdev = pdev;
187 pci_dev = psdev->dev;
188 }
189 spin_unlock_irqrestore(&psdev->lock, flags);
190
191 if (!pci_dev)
192 pcistub_device_put(psdev);
193
194 return pci_dev;
195}
196
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400197struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400198 int domain, int bus,
199 int slot, int func)
200{
201 struct pcistub_device *psdev;
202 struct pci_dev *found_dev = NULL;
203 unsigned long flags;
204
205 spin_lock_irqsave(&pcistub_devices_lock, flags);
206
207 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
208 if (psdev->dev != NULL
209 && domain == pci_domain_nr(psdev->dev->bus)
210 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000211 && slot == PCI_SLOT(psdev->dev->devfn)
212 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400213 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
214 break;
215 }
216 }
217
218 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
219 return found_dev;
220}
221
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400222struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400223 struct pci_dev *dev)
224{
225 struct pcistub_device *psdev;
226 struct pci_dev *found_dev = 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_dev = pcistub_device_get_pci_dev(pdev, psdev);
234 break;
235 }
236 }
237
238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
239 return found_dev;
240}
241
242void pcistub_put_pci_dev(struct pci_dev *dev)
243{
244 struct pcistub_device *psdev, *found_psdev = NULL;
245 unsigned long flags;
246
247 spin_lock_irqsave(&pcistub_devices_lock, flags);
248
249 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
250 if (psdev->dev == dev) {
251 found_psdev = psdev;
252 break;
253 }
254 }
255
256 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400257 if (WARN_ON(!found_psdev))
258 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400259
260 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400261 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400262 */
263 down_write(&pcistub_sem);
264 /* Cleanup our device
265 * (so it's ready for the next domain)
266 */
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500267
268 /* This is OK - we are running from workqueue context
269 * and want to inhibit the user from fiddling with 'reset'
270 */
271 pci_reset_function(dev);
272 pci_restore_state(psdev->dev);
273
274 /* This disables the device. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400275 xen_pcibk_reset_device(found_psdev->dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500276
277 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400278 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
279 xen_pcibk_config_reset_dev(found_psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400280
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500281 xen_unregister_device_domain_owner(found_psdev->dev);
282
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400283 spin_lock_irqsave(&found_psdev->lock, flags);
284 found_psdev->pdev = NULL;
285 spin_unlock_irqrestore(&found_psdev->lock, flags);
286
287 pcistub_device_put(found_psdev);
288 up_write(&pcistub_sem);
289}
290
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800291static int pcistub_match_one(struct pci_dev *dev,
292 struct pcistub_device_id *pdev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400293{
294 /* Match the specified device by domain, bus, slot, func and also if
295 * any of the device's parent bridges match.
296 */
297 for (; dev != NULL; dev = dev->bus->self) {
298 if (pci_domain_nr(dev->bus) == pdev_id->domain
299 && dev->bus->number == pdev_id->bus
300 && dev->devfn == pdev_id->devfn)
301 return 1;
302
303 /* Sometimes topmost bridge links to itself. */
304 if (dev == dev->bus->self)
305 break;
306 }
307
308 return 0;
309}
310
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800311static int pcistub_match(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400312{
313 struct pcistub_device_id *pdev_id;
314 unsigned long flags;
315 int found = 0;
316
317 spin_lock_irqsave(&device_ids_lock, flags);
318 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
319 if (pcistub_match_one(dev, pdev_id)) {
320 found = 1;
321 break;
322 }
323 }
324 spin_unlock_irqrestore(&device_ids_lock, flags);
325
326 return found;
327}
328
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800329static int pcistub_init_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400330{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400331 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400332 int err = 0;
333
334 dev_dbg(&dev->dev, "initializing...\n");
335
336 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400337 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400338 * would need to be called somewhere to free the memory allocated
339 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
340 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400341 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
342 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400343 if (!dev_data) {
344 err = -ENOMEM;
345 goto out;
346 }
347 pci_set_drvdata(dev, dev_data);
348
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400349 /*
350 * Setup name for fake IRQ handler. It will only be enabled
351 * once the device is turned on by the guest.
352 */
353 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
354
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400355 dev_dbg(&dev->dev, "initializing config\n");
356
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400357 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
358 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400359 if (err)
360 goto out;
361
362 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
363 * must do this here because pcibios_enable_device may specify
364 * the pci device's true irq (and possibly its other resources)
365 * if they differ from what's in the configuration space.
366 * This makes the assumption that the device's resources won't
367 * change after this point (otherwise this code may break!)
368 */
369 dev_dbg(&dev->dev, "enabling device\n");
370 err = pci_enable_device(dev);
371 if (err)
372 goto config_release;
373
Jan Beulichd69c0e32013-05-29 13:31:15 +0100374 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000375 struct physdev_pci_device ppdev = {
376 .seg = pci_domain_nr(dev->bus),
377 .bus = dev->bus->number,
378 .devfn = dev->devfn
379 };
380
381 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
382 if (err)
383 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
384 err);
385 }
386
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500387 /* We need the device active to save the state. */
388 dev_dbg(&dev->dev, "save state of device\n");
389 pci_save_state(dev);
390 dev_data->pci_saved_state = pci_store_saved_state(dev);
391 if (!dev_data->pci_saved_state)
392 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400393 else {
Masanari Iida744627e92012-11-05 23:30:40 +0900394 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400395 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400396 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400397 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400398 /* Now disable the device (this also ensures some private device
399 * data is setup before we export)
400 */
401 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400402 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400403
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500404 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400405 return 0;
406
407config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400408 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400409
410out:
411 pci_set_drvdata(dev, NULL);
412 kfree(dev_data);
413 return err;
414}
415
416/*
417 * Because some initialization still happens on
418 * devices during fs_initcall, we need to defer
419 * full initialization of our devices until
420 * device_initcall.
421 */
422static int __init pcistub_init_devices_late(void)
423{
424 struct pcistub_device *psdev;
425 unsigned long flags;
426 int err = 0;
427
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400428 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400429
430 spin_lock_irqsave(&pcistub_devices_lock, flags);
431
432 while (!list_empty(&seized_devices)) {
433 psdev = container_of(seized_devices.next,
434 struct pcistub_device, dev_list);
435 list_del(&psdev->dev_list);
436
437 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
438
439 err = pcistub_init_device(psdev->dev);
440 if (err) {
441 dev_err(&psdev->dev->dev,
442 "error %d initializing device\n", err);
443 kfree(psdev);
444 psdev = NULL;
445 }
446
447 spin_lock_irqsave(&pcistub_devices_lock, flags);
448
449 if (psdev)
450 list_add_tail(&psdev->dev_list, &pcistub_devices);
451 }
452
453 initialize_devices = 1;
454
455 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
456
457 return 0;
458}
459
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800460static int pcistub_seize(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400461{
462 struct pcistub_device *psdev;
463 unsigned long flags;
464 int err = 0;
465
466 psdev = pcistub_device_alloc(dev);
467 if (!psdev)
468 return -ENOMEM;
469
470 spin_lock_irqsave(&pcistub_devices_lock, flags);
471
472 if (initialize_devices) {
473 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
474
475 /* don't want irqs disabled when calling pcistub_init_device */
476 err = pcistub_init_device(psdev->dev);
477
478 spin_lock_irqsave(&pcistub_devices_lock, flags);
479
480 if (!err)
481 list_add(&psdev->dev_list, &pcistub_devices);
482 } else {
483 dev_dbg(&dev->dev, "deferring initialization\n");
484 list_add(&psdev->dev_list, &seized_devices);
485 }
486
487 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
488
489 if (err)
490 pcistub_device_put(psdev);
491
492 return err;
493}
494
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800495static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400496{
497 int err = 0;
498
499 dev_dbg(&dev->dev, "probing...\n");
500
501 if (pcistub_match(dev)) {
502
503 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
504 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
505 dev_err(&dev->dev, "can't export pci devices that "
506 "don't have a normal (0) or bridge (1) "
507 "header type!\n");
508 err = -ENODEV;
509 goto out;
510 }
511
512 dev_info(&dev->dev, "seizing device\n");
513 err = pcistub_seize(dev);
514 } else
515 /* Didn't find the device */
516 err = -ENODEV;
517
518out:
519 return err;
520}
521
522static void pcistub_remove(struct pci_dev *dev)
523{
524 struct pcistub_device *psdev, *found_psdev = NULL;
525 unsigned long flags;
526
527 dev_dbg(&dev->dev, "removing\n");
528
529 spin_lock_irqsave(&pcistub_devices_lock, flags);
530
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400531 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400532
533 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
534 if (psdev->dev == dev) {
535 found_psdev = psdev;
536 break;
537 }
538 }
539
540 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
541
542 if (found_psdev) {
543 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
544 found_psdev->pdev);
545
546 if (found_psdev->pdev) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400547 printk(KERN_WARNING DRV_NAME ": ****** removing device "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400548 "%s while still in-use! ******\n",
549 pci_name(found_psdev->dev));
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400550 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
551 " still access this device's i/o resources!\n");
552 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400553 "domain before binding device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400554 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400555 "or domains\n");
556
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400557 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400558 found_psdev->dev);
559 }
560
561 spin_lock_irqsave(&pcistub_devices_lock, flags);
562 list_del(&found_psdev->dev_list);
563 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
564
565 /* the final put for releasing from the list */
566 pcistub_device_put(found_psdev);
567 }
568}
569
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400570static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400571 {
572 .vendor = PCI_ANY_ID,
573 .device = PCI_ANY_ID,
574 .subvendor = PCI_ANY_ID,
575 .subdevice = PCI_ANY_ID,
576 },
577 {0,},
578};
579
580#define PCI_NODENAME_MAX 40
581static void kill_domain_by_device(struct pcistub_device *psdev)
582{
583 struct xenbus_transaction xbt;
584 int err;
585 char nodename[PCI_NODENAME_MAX];
586
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400587 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400588 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
589 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400590
591again:
592 err = xenbus_transaction_start(&xbt);
593 if (err) {
594 dev_err(&psdev->dev->dev,
595 "error %d when start xenbus transaction\n", err);
596 return;
597 }
598 /*PV AER handlers will set this flag*/
599 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
600 err = xenbus_transaction_end(xbt, 0);
601 if (err) {
602 if (err == -EAGAIN)
603 goto again;
604 dev_err(&psdev->dev->dev,
605 "error %d when end xenbus transaction\n", err);
606 return;
607 }
608}
609
610/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400611 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400612 * jobs: send service request and waiting for front_end response.
613*/
614static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400615 pci_channel_state_t state, int aer_cmd,
616 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400617{
618 pci_ers_result_t res = result;
619 struct xen_pcie_aer_op *aer_op;
620 int ret;
621
622 /*with PV AER drivers*/
623 aer_op = &(psdev->pdev->sh_info->aer_op);
624 aer_op->cmd = aer_cmd ;
625 /*useful for error_detected callback*/
626 aer_op->err = state;
627 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400628 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400629 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
630 if (!ret) {
631 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400632 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400633 return PCI_ERS_RESULT_NONE;
634 }
635 wmb();
636
637 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400638 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400639 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400640 /*local flag to mark there's aer request, xen_pcibk callback will use
641 * this flag to judge whether we need to check pci-front give aer
642 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400643 */
644 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
645
646 /*It is possible that a pcifront conf_read_write ops request invokes
647 * the callback which cause the spurious execution of wake_up.
648 * Yet it is harmless and better than a spinlock here
649 */
650 set_bit(_XEN_PCIB_active,
651 (unsigned long *)&psdev->pdev->sh_info->flags);
652 wmb();
653 notify_remote_via_irq(psdev->pdev->evtchn_irq);
654
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400655 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
656 !(test_bit(_XEN_PCIB_active, (unsigned long *)
657 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400658
659 if (!ret) {
660 if (test_bit(_XEN_PCIB_active,
661 (unsigned long *)&psdev->pdev->sh_info->flags)) {
662 dev_err(&psdev->dev->dev,
663 "pcifront aer process not responding!\n");
664 clear_bit(_XEN_PCIB_active,
665 (unsigned long *)&psdev->pdev->sh_info->flags);
666 aer_op->err = PCI_ERS_RESULT_NONE;
667 return res;
668 }
669 }
670 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
671
672 if (test_bit(_XEN_PCIF_active,
673 (unsigned long *)&psdev->pdev->sh_info->flags)) {
674 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400675 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400676 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400677 }
678
679 res = (pci_ers_result_t)aer_op->err;
680 return res;
681}
682
683/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400684* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400685* of the device driver could provide this service, and then wait for pcifront
686* ack.
687* @dev: pointer to PCI devices
688* return value is used by aer_core do_recovery policy
689*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400690static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400691{
692 struct pcistub_device *psdev;
693 pci_ers_result_t result;
694
695 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400696 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400697 dev->bus->number, dev->devfn);
698
699 down_write(&pcistub_sem);
700 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
701 dev->bus->number,
702 PCI_SLOT(dev->devfn),
703 PCI_FUNC(dev->devfn));
704
705 if (!psdev || !psdev->pdev) {
706 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400707 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400708 goto end;
709 }
710
711 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400712 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400713 " by HVM, kill it\n");
714 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100715 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400716 }
717
718 if (!test_bit(_XEN_PCIB_AERHANDLER,
719 (unsigned long *)&psdev->pdev->sh_info->flags)) {
720 dev_err(&dev->dev,
721 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100722 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400723 }
724 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
725
726 if (result == PCI_ERS_RESULT_NONE ||
727 result == PCI_ERS_RESULT_DISCONNECT) {
728 dev_dbg(&dev->dev,
729 "No AER slot_reset service or disconnected!\n");
730 kill_domain_by_device(psdev);
731 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400732end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100733 if (psdev)
734 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400735 up_write(&pcistub_sem);
736 return result;
737
738}
739
740
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400741/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400742* in case of the device driver could provide this service, and then wait
743* for pcifront ack
744* @dev: pointer to PCI devices
745* return value is used by aer_core do_recovery policy
746*/
747
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400748static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400749{
750 struct pcistub_device *psdev;
751 pci_ers_result_t result;
752
753 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400754 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400755 dev->bus->number, dev->devfn);
756
757 down_write(&pcistub_sem);
758 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
759 dev->bus->number,
760 PCI_SLOT(dev->devfn),
761 PCI_FUNC(dev->devfn));
762
763 if (!psdev || !psdev->pdev) {
764 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400765 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400766 goto end;
767 }
768
769 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400770 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400771 " by HVM, kill it\n");
772 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100773 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400774 }
775
776 if (!test_bit(_XEN_PCIB_AERHANDLER,
777 (unsigned long *)&psdev->pdev->sh_info->flags)) {
778 dev_err(&dev->dev,
779 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100780 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400781 }
782 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
783
784 if (result == PCI_ERS_RESULT_NONE ||
785 result == PCI_ERS_RESULT_DISCONNECT) {
786 dev_dbg(&dev->dev,
787 "No AER mmio_enabled service or disconnected!\n");
788 kill_domain_by_device(psdev);
789 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400790end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100791 if (psdev)
792 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400793 up_write(&pcistub_sem);
794 return result;
795}
796
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400797/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400798* in case of the device driver could provide this service, and then wait
799* for pcifront ack.
800* @dev: pointer to PCI devices
801* @error: the current PCI connection state
802* return value is used by aer_core do_recovery policy
803*/
804
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400805static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400806 pci_channel_state_t error)
807{
808 struct pcistub_device *psdev;
809 pci_ers_result_t result;
810
811 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400812 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400813 dev->bus->number, dev->devfn);
814
815 down_write(&pcistub_sem);
816 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
817 dev->bus->number,
818 PCI_SLOT(dev->devfn),
819 PCI_FUNC(dev->devfn));
820
821 if (!psdev || !psdev->pdev) {
822 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400823 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400824 goto end;
825 }
826
827 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400828 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400829 " by HVM, kill it\n");
830 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100831 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400832 }
833
834 /*Guest owns the device yet no aer handler regiested, kill guest*/
835 if (!test_bit(_XEN_PCIB_AERHANDLER,
836 (unsigned long *)&psdev->pdev->sh_info->flags)) {
837 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
838 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100839 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400840 }
841 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
842
843 if (result == PCI_ERS_RESULT_NONE ||
844 result == PCI_ERS_RESULT_DISCONNECT) {
845 dev_dbg(&dev->dev,
846 "No AER error_detected service or disconnected!\n");
847 kill_domain_by_device(psdev);
848 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400849end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100850 if (psdev)
851 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400852 up_write(&pcistub_sem);
853 return result;
854}
855
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400856/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400857* in case of the device driver could provide this service, and then wait
858* for pcifront ack.
859* @dev: pointer to PCI devices
860*/
861
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400862static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400863{
864 struct pcistub_device *psdev;
865
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400866 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400867 dev->bus->number, dev->devfn);
868
869 down_write(&pcistub_sem);
870 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
871 dev->bus->number,
872 PCI_SLOT(dev->devfn),
873 PCI_FUNC(dev->devfn));
874
875 if (!psdev || !psdev->pdev) {
876 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400877 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400878 goto end;
879 }
880
881 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400882 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400883 " by HVM, kill it\n");
884 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100885 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400886 }
887
888 if (!test_bit(_XEN_PCIB_AERHANDLER,
889 (unsigned long *)&psdev->pdev->sh_info->flags)) {
890 dev_err(&dev->dev,
891 "guest with no AER driver should have been killed\n");
892 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100893 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400894 }
895 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
896 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400897end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100898 if (psdev)
899 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400900 up_write(&pcistub_sem);
901 return;
902}
903
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400904/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700905static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400906 .error_detected = xen_pcibk_error_detected,
907 .mmio_enabled = xen_pcibk_mmio_enabled,
908 .slot_reset = xen_pcibk_slot_reset,
909 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400910};
911
912/*
913 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
914 * for a normal device. I don't want it to be loaded automatically.
915 */
916
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400917static struct pci_driver xen_pcibk_pci_driver = {
918 /* The name should be xen_pciback, but until the tools are updated
919 * we will keep it as pciback. */
920 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400921 .id_table = pcistub_ids,
922 .probe = pcistub_probe,
923 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400924 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400925};
926
927static inline int str_to_slot(const char *buf, int *domain, int *bus,
928 int *slot, int *func)
929{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000930 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400931
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000932 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
933 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100934 case 3:
935 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000936 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100937 break;
938 case 2:
939 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000940 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100941 break;
942 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000943 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400944 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400945
946 /* try again without domain */
947 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000948 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100949 case 2:
950 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000951 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100952 break;
953 case 1:
954 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000955 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100956 break;
957 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000958 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400959 return 0;
960
961 return -EINVAL;
962}
963
964static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
965 *slot, int *func, int *reg, int *size, int *mask)
966{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000967 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400968
Jan Beulichb3e40b72012-11-02 14:37:13 +0000969 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000970 reg, size, mask, &parsed);
971 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400972 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000973
974 /* try again without domain */
975 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000976 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000977 mask, &parsed);
978 if (parsed && !buf[parsed])
979 return 0;
980
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400981 return -EINVAL;
982}
983
984static int pcistub_device_id_add(int domain, int bus, int slot, int func)
985{
986 struct pcistub_device_id *pci_dev_id;
987 unsigned long flags;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000988 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100989
990 if (slot < 0) {
991 for (slot = 0; !rc && slot < 32; ++slot)
992 rc = pcistub_device_id_add(domain, bus, slot, func);
993 return rc;
994 }
995
996 if (func < 0) {
997 for (func = 0; !rc && func < 8; ++func)
998 rc = pcistub_device_id_add(domain, bus, slot, func);
999 return rc;
1000 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001001
Jan Beulichb3e40b72012-11-02 14:37:13 +00001002 if ((
1003#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1004 || !defined(CONFIG_PCI_DOMAINS)
1005 !pci_domains_supported ? domain :
1006#endif
1007 domain < 0 || domain > 0xffff)
1008 || bus < 0 || bus > 0xff
1009 || PCI_SLOT(devfn) != slot
1010 || PCI_FUNC(devfn) != func)
1011 return -EINVAL;
1012
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001013 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1014 if (!pci_dev_id)
1015 return -ENOMEM;
1016
1017 pci_dev_id->domain = domain;
1018 pci_dev_id->bus = bus;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001019 pci_dev_id->devfn = devfn;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001020
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001021 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001022 domain, bus, slot, func);
1023
1024 spin_lock_irqsave(&device_ids_lock, flags);
1025 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1026 spin_unlock_irqrestore(&device_ids_lock, flags);
1027
1028 return 0;
1029}
1030
1031static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1032{
1033 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001034 int err = -ENOENT;
1035 unsigned long flags;
1036
1037 spin_lock_irqsave(&device_ids_lock, flags);
1038 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1039 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001040 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1041 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1042 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001043 /* Don't break; here because it's possible the same
1044 * slot could be in the list more than once
1045 */
1046 list_del(&pci_dev_id->slot_list);
1047 kfree(pci_dev_id);
1048
1049 err = 0;
1050
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001051 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001052 "seize list\n", domain, bus, slot, func);
1053 }
1054 }
1055 spin_unlock_irqrestore(&device_ids_lock, flags);
1056
1057 return err;
1058}
1059
Jan Beulichb3e40b72012-11-02 14:37:13 +00001060static int pcistub_reg_add(int domain, int bus, int slot, int func,
1061 unsigned int reg, unsigned int size,
1062 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001063{
1064 int err = 0;
1065 struct pcistub_device *psdev;
1066 struct pci_dev *dev;
1067 struct config_field *field;
1068
Jan Beulichb3e40b72012-11-02 14:37:13 +00001069 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1070 return -EINVAL;
1071
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001072 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001073 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001074 err = -ENODEV;
1075 goto out;
1076 }
1077 dev = psdev->dev;
1078
1079 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1080 if (!field) {
1081 err = -ENOMEM;
1082 goto out;
1083 }
1084
1085 field->offset = reg;
1086 field->size = size;
1087 field->mask = mask;
1088 field->init = NULL;
1089 field->reset = NULL;
1090 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001091 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001092
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001093 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001094 if (err)
1095 kfree(field);
1096out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001097 if (psdev)
1098 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001099 return err;
1100}
1101
1102static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1103 size_t count)
1104{
1105 int domain, bus, slot, func;
1106 int err;
1107
1108 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1109 if (err)
1110 goto out;
1111
1112 err = pcistub_device_id_add(domain, bus, slot, func);
1113
1114out:
1115 if (!err)
1116 err = count;
1117 return err;
1118}
Jan Beulich402c5e12011-09-21 16:22:11 -04001119static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001120
1121static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1122 size_t count)
1123{
1124 int domain, bus, slot, func;
1125 int err;
1126
1127 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1128 if (err)
1129 goto out;
1130
1131 err = pcistub_device_id_remove(domain, bus, slot, func);
1132
1133out:
1134 if (!err)
1135 err = count;
1136 return err;
1137}
Jan Beulich402c5e12011-09-21 16:22:11 -04001138static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001139
1140static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1141{
1142 struct pcistub_device_id *pci_dev_id;
1143 size_t count = 0;
1144 unsigned long flags;
1145
1146 spin_lock_irqsave(&device_ids_lock, flags);
1147 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1148 if (count >= PAGE_SIZE)
1149 break;
1150
1151 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001152 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001153 pci_dev_id->domain, pci_dev_id->bus,
1154 PCI_SLOT(pci_dev_id->devfn),
1155 PCI_FUNC(pci_dev_id->devfn));
1156 }
1157 spin_unlock_irqrestore(&device_ids_lock, flags);
1158
1159 return count;
1160}
Jan Beulich402c5e12011-09-21 16:22:11 -04001161static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001162
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001163static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1164{
1165 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001166 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001167 size_t count = 0;
1168 unsigned long flags;
1169
1170 spin_lock_irqsave(&pcistub_devices_lock, flags);
1171 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1172 if (count >= PAGE_SIZE)
1173 break;
1174 if (!psdev->dev)
1175 continue;
1176 dev_data = pci_get_drvdata(psdev->dev);
1177 if (!dev_data)
1178 continue;
1179 count +=
1180 scnprintf(buf + count, PAGE_SIZE - count,
1181 "%s:%s:%sing:%ld\n",
1182 pci_name(psdev->dev),
1183 dev_data->isr_on ? "on" : "off",
1184 dev_data->ack_intr ? "ack" : "not ack",
1185 dev_data->handled);
1186 }
1187 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1188 return count;
1189}
Jan Beulich402c5e12011-09-21 16:22:11 -04001190static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001191
1192static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1193 const char *buf,
1194 size_t count)
1195{
1196 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001197 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001198 int domain, bus, slot, func;
Wei Yongjun405010d2013-05-31 19:59:20 +08001199 int err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001200
1201 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1202 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001203 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001204
1205 psdev = pcistub_device_find(domain, bus, slot, func);
Wei Yongjun405010d2013-05-31 19:59:20 +08001206 if (!psdev) {
1207 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001208 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001209 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001210
1211 dev_data = pci_get_drvdata(psdev->dev);
Wei Yongjun405010d2013-05-31 19:59:20 +08001212 if (!dev_data) {
1213 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001214 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001215 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001216
1217 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1218 dev_data->irq_name, dev_data->isr_on,
1219 !dev_data->isr_on);
1220
1221 dev_data->isr_on = !(dev_data->isr_on);
1222 if (dev_data->isr_on)
1223 dev_data->ack_intr = 1;
1224out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001225 if (psdev)
1226 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001227 if (!err)
1228 err = count;
1229 return err;
1230}
Jan Beulich402c5e12011-09-21 16:22:11 -04001231static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1232 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001233
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001234static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1235 size_t count)
1236{
1237 int domain, bus, slot, func, reg, size, mask;
1238 int err;
1239
1240 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1241 &mask);
1242 if (err)
1243 goto out;
1244
1245 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1246
1247out:
1248 if (!err)
1249 err = count;
1250 return err;
1251}
1252
1253static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1254{
1255 int count = 0;
1256 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001257 struct xen_pcibk_config_quirk *quirk;
1258 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001259 const struct config_field *field;
1260 const struct config_field_entry *cfg_entry;
1261
1262 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001263 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001264 if (count >= PAGE_SIZE)
1265 goto out;
1266
1267 count += scnprintf(buf + count, PAGE_SIZE - count,
1268 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1269 quirk->pdev->bus->number,
1270 PCI_SLOT(quirk->pdev->devfn),
1271 PCI_FUNC(quirk->pdev->devfn),
1272 quirk->devid.vendor, quirk->devid.device,
1273 quirk->devid.subvendor,
1274 quirk->devid.subdevice);
1275
1276 dev_data = pci_get_drvdata(quirk->pdev);
1277
1278 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1279 field = cfg_entry->field;
1280 if (count >= PAGE_SIZE)
1281 goto out;
1282
1283 count += scnprintf(buf + count, PAGE_SIZE - count,
1284 "\t\t%08x:%01x:%08x\n",
1285 cfg_entry->base_offset +
1286 field->offset, field->size,
1287 field->mask);
1288 }
1289 }
1290
1291out:
1292 spin_unlock_irqrestore(&device_ids_lock, flags);
1293
1294 return count;
1295}
Jan Beulich402c5e12011-09-21 16:22:11 -04001296static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1297 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001298
1299static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1300 size_t count)
1301{
1302 int domain, bus, slot, func;
1303 int err;
1304 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001305 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001306
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001307 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1308 if (err)
1309 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001310
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001311 psdev = pcistub_device_find(domain, bus, slot, func);
1312 if (!psdev) {
1313 err = -ENODEV;
1314 goto out;
1315 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001316
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001317 dev_data = pci_get_drvdata(psdev->dev);
1318 /* the driver data for a device should never be null at this point */
1319 if (!dev_data) {
1320 err = -ENXIO;
1321 goto release;
1322 }
1323 if (!dev_data->permissive) {
1324 dev_data->permissive = 1;
1325 /* Let user know that what they're doing could be unsafe */
1326 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1327 "configuration space accesses!\n");
1328 dev_warn(&psdev->dev->dev,
1329 "permissive mode is potentially unsafe!\n");
1330 }
1331release:
1332 pcistub_device_put(psdev);
1333out:
1334 if (!err)
1335 err = count;
1336 return err;
1337}
1338
1339static ssize_t permissive_show(struct device_driver *drv, char *buf)
1340{
1341 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001342 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001343 size_t count = 0;
1344 unsigned long flags;
1345 spin_lock_irqsave(&pcistub_devices_lock, flags);
1346 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1347 if (count >= PAGE_SIZE)
1348 break;
1349 if (!psdev->dev)
1350 continue;
1351 dev_data = pci_get_drvdata(psdev->dev);
1352 if (!dev_data || !dev_data->permissive)
1353 continue;
1354 count +=
1355 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1356 pci_name(psdev->dev));
1357 }
1358 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1359 return count;
1360}
Jan Beulich402c5e12011-09-21 16:22:11 -04001361static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1362 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001363
1364static void pcistub_exit(void)
1365{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001366 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1367 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001368 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001369 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1370 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1371 driver_remove_file(&xen_pcibk_pci_driver.driver,
1372 &driver_attr_permissive);
1373 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001374 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001375 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001376 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001377 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001378}
1379
1380static int __init pcistub_init(void)
1381{
1382 int pos = 0;
1383 int err = 0;
1384 int domain, bus, slot, func;
1385 int parsed;
1386
1387 if (pci_devs_to_hide && *pci_devs_to_hide) {
1388 do {
1389 parsed = 0;
1390
1391 err = sscanf(pci_devs_to_hide + pos,
1392 " (%x:%x:%x.%x) %n",
1393 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001394 switch (err) {
1395 case 3:
1396 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001397 sscanf(pci_devs_to_hide + pos,
1398 " (%x:%x:%x.*) %n",
1399 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001400 break;
1401 case 2:
1402 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001403 sscanf(pci_devs_to_hide + pos,
1404 " (%x:%x:*.*) %n",
1405 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001406 break;
1407 }
1408
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001409 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001410 domain = 0;
1411 err = sscanf(pci_devs_to_hide + pos,
1412 " (%x:%x.%x) %n",
1413 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001414 switch (err) {
1415 case 2:
1416 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001417 sscanf(pci_devs_to_hide + pos,
1418 " (%x:%x.*) %n",
1419 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001420 break;
1421 case 1:
1422 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001423 sscanf(pci_devs_to_hide + pos,
1424 " (%x:*.*) %n",
1425 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001426 break;
1427 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001428 }
1429
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001430 if (parsed <= 0)
1431 goto parse_error;
1432
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001433 err = pcistub_device_id_add(domain, bus, slot, func);
1434 if (err)
1435 goto out;
1436
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001437 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001438 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001439 }
1440
1441 /* If we're the first PCI Device Driver to register, we're the
1442 * first one to get offered PCI devices as they become
1443 * available (and thus we can be the first to grab them)
1444 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001445 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001446 if (err < 0)
1447 goto out;
1448
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001449 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001450 &driver_attr_new_slot);
1451 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001452 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001453 &driver_attr_remove_slot);
1454 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001455 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001456 &driver_attr_slots);
1457 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001458 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001459 &driver_attr_quirks);
1460 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001461 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001462 &driver_attr_permissive);
1463
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001464 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001465 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001466 &driver_attr_irq_handlers);
1467 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001468 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001469 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001470 if (err)
1471 pcistub_exit();
1472
1473out:
1474 return err;
1475
1476parse_error:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001477 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001478 pci_devs_to_hide + pos);
1479 return -EINVAL;
1480}
1481
1482#ifndef MODULE
1483/*
1484 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001485 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001486 * want to suck up any device before other drivers
1487 * get a chance by being the first pci device
1488 * driver to register)
1489 */
1490fs_initcall(pcistub_init);
1491#endif
1492
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001493static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001494{
1495 int err;
1496
1497 if (!xen_initial_domain())
1498 return -ENODEV;
1499
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001500 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001501 if (err)
1502 return err;
1503
1504#ifdef MODULE
1505 err = pcistub_init();
1506 if (err < 0)
1507 return err;
1508#endif
1509
1510 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001511 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001512 if (err)
1513 pcistub_exit();
1514
1515 return err;
1516}
1517
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001518static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001519{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001520 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001521 pcistub_exit();
1522}
1523
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001524module_init(xen_pcibk_init);
1525module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001526
1527MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001528MODULE_ALIAS("xen-backend:pci");