blob: b84426a7f88df599b504c70dbac1fca078bc9db3 [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 */
Joe Perches283c0972013-06-28 03:21:41 -07007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/rwsem.h>
13#include <linux/list.h>
14#include <linux/spinlock.h>
15#include <linux/kref.h>
16#include <linux/pci.h>
17#include <linux/wait.h>
18#include <linux/sched.h>
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -040019#include <linux/atomic.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040020#include <xen/events.h>
21#include <asm/xen/pci.h>
22#include <asm/xen/hypervisor.h>
Jan Beulich909b3fd2013-03-12 15:06:23 +000023#include <xen/interface/physdev.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040024#include "pciback.h"
25#include "conf_space.h"
26#include "conf_space_quirks.h"
27
28static char *pci_devs_to_hide;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040029wait_queue_head_t xen_pcibk_aer_wait_queue;
30/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
31* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040032*/
33static DECLARE_RWSEM(pcistub_sem);
34module_param_named(hide, pci_devs_to_hide, charp, 0444);
35
36struct pcistub_device_id {
37 struct list_head slot_list;
38 int domain;
39 unsigned char bus;
40 unsigned int devfn;
41};
42static LIST_HEAD(pcistub_device_ids);
43static DEFINE_SPINLOCK(device_ids_lock);
44
45struct pcistub_device {
46 struct kref kref;
47 struct list_head dev_list;
48 spinlock_t lock;
49
50 struct pci_dev *dev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040051 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040052};
53
54/* Access to pcistub_devices & seized_devices lists and the initialize_devices
55 * flag must be locked with pcistub_devices_lock
56 */
57static DEFINE_SPINLOCK(pcistub_devices_lock);
58static LIST_HEAD(pcistub_devices);
59
60/* wait for device_initcall before initializing our devices
61 * (see pcistub_init_devices_late)
62 */
63static int initialize_devices;
64static LIST_HEAD(seized_devices);
65
66static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
67{
68 struct pcistub_device *psdev;
69
70 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
71
72 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
73 if (!psdev)
74 return NULL;
75
76 psdev->dev = pci_dev_get(dev);
77 if (!psdev->dev) {
78 kfree(psdev);
79 return NULL;
80 }
81
82 kref_init(&psdev->kref);
83 spin_lock_init(&psdev->lock);
84
85 return psdev;
86}
87
88/* Don't call this directly as it's called by pcistub_device_put */
89static void pcistub_device_release(struct kref *kref)
90{
91 struct pcistub_device *psdev;
Jan Beulich909b3fd2013-03-12 15:06:23 +000092 struct pci_dev *dev;
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -050093 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040094
95 psdev = container_of(kref, struct pcistub_device, kref);
Jan Beulich909b3fd2013-03-12 15:06:23 +000096 dev = psdev->dev;
97 dev_data = pci_get_drvdata(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040098
Jan Beulich909b3fd2013-03-12 15:06:23 +000099 dev_dbg(&dev->dev, "pcistub_device_release\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400100
Jan Beulich909b3fd2013-03-12 15:06:23 +0000101 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500102
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500103 /* Call the reset function which does not take lock as this
104 * is called from "unbind" which takes a device_lock mutex.
105 */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000106 __pci_reset_function_locked(dev);
107 if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
108 dev_dbg(&dev->dev, "Could not reload PCI state\n");
109 else
110 pci_restore_state(dev);
111
Jan Beulichd69c0e32013-05-29 13:31:15 +0100112 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000113 struct physdev_pci_device ppdev = {
114 .seg = pci_domain_nr(dev->bus),
115 .bus = dev->bus->number,
116 .devfn = dev->devfn
117 };
118 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
119 &ppdev);
120
121 if (err)
122 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
123 err);
124 }
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500125
126 /* Disable the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000127 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500128
129 kfree(dev_data);
Jan Beulich909b3fd2013-03-12 15:06:23 +0000130 pci_set_drvdata(dev, NULL);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500131
132 /* Clean-up the device */
Jan Beulich909b3fd2013-03-12 15:06:23 +0000133 xen_pcibk_config_free_dyn_fields(dev);
134 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400135
Jan Beulich909b3fd2013-03-12 15:06:23 +0000136 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
137 pci_dev_put(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400138
139 kfree(psdev);
140}
141
142static inline void pcistub_device_get(struct pcistub_device *psdev)
143{
144 kref_get(&psdev->kref);
145}
146
147static inline void pcistub_device_put(struct pcistub_device *psdev)
148{
149 kref_put(&psdev->kref, pcistub_device_release);
150}
151
152static struct pcistub_device *pcistub_device_find(int domain, int bus,
153 int slot, int func)
154{
155 struct pcistub_device *psdev = NULL;
156 unsigned long flags;
157
158 spin_lock_irqsave(&pcistub_devices_lock, flags);
159
160 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 if (psdev->dev != NULL
162 && domain == pci_domain_nr(psdev->dev->bus)
163 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400166 pcistub_device_get(psdev);
167 goto out;
168 }
169 }
170
171 /* didn't find it */
172 psdev = NULL;
173
174out:
175 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
176 return psdev;
177}
178
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400179static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400180 struct pcistub_device *psdev)
181{
182 struct pci_dev *pci_dev = NULL;
183 unsigned long flags;
184
185 pcistub_device_get(psdev);
186
187 spin_lock_irqsave(&psdev->lock, flags);
188 if (!psdev->pdev) {
189 psdev->pdev = pdev;
190 pci_dev = psdev->dev;
191 }
192 spin_unlock_irqrestore(&psdev->lock, flags);
193
194 if (!pci_dev)
195 pcistub_device_put(psdev);
196
197 return pci_dev;
198}
199
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400200struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400201 int domain, int bus,
202 int slot, int func)
203{
204 struct pcistub_device *psdev;
205 struct pci_dev *found_dev = NULL;
206 unsigned long flags;
207
208 spin_lock_irqsave(&pcistub_devices_lock, flags);
209
210 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
211 if (psdev->dev != NULL
212 && domain == pci_domain_nr(psdev->dev->bus)
213 && bus == psdev->dev->bus->number
Jan Beulichb3e40b72012-11-02 14:37:13 +0000214 && slot == PCI_SLOT(psdev->dev->devfn)
215 && func == PCI_FUNC(psdev->dev->devfn)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400216 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
217 break;
218 }
219 }
220
221 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
222 return found_dev;
223}
224
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400225struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400226 struct pci_dev *dev)
227{
228 struct pcistub_device *psdev;
229 struct pci_dev *found_dev = NULL;
230 unsigned long flags;
231
232 spin_lock_irqsave(&pcistub_devices_lock, flags);
233
234 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
235 if (psdev->dev == dev) {
236 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
237 break;
238 }
239 }
240
241 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
242 return found_dev;
243}
244
245void pcistub_put_pci_dev(struct pci_dev *dev)
246{
247 struct pcistub_device *psdev, *found_psdev = NULL;
248 unsigned long flags;
249
250 spin_lock_irqsave(&pcistub_devices_lock, flags);
251
252 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
253 if (psdev->dev == dev) {
254 found_psdev = psdev;
255 break;
256 }
257 }
258
259 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400260 if (WARN_ON(!found_psdev))
261 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400262
263 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400264 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400265 */
266 down_write(&pcistub_sem);
267 /* Cleanup our device
268 * (so it's ready for the next domain)
269 */
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500270
271 /* This is OK - we are running from workqueue context
272 * and want to inhibit the user from fiddling with 'reset'
273 */
274 pci_reset_function(dev);
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500275 pci_restore_state(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500276
277 /* This disables the device. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500278 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500279
280 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500281 xen_pcibk_config_reset_dev(dev);
Konrad Rzeszutek Wilkfcb8ce92013-12-03 21:37:24 -0500282 xen_pcibk_config_free_dyn_fields(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400283
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500284 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500285
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400286 spin_lock_irqsave(&found_psdev->lock, flags);
287 found_psdev->pdev = NULL;
288 spin_unlock_irqrestore(&found_psdev->lock, flags);
289
290 pcistub_device_put(found_psdev);
291 up_write(&pcistub_sem);
292}
293
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800294static int pcistub_match_one(struct pci_dev *dev,
295 struct pcistub_device_id *pdev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400296{
297 /* Match the specified device by domain, bus, slot, func and also if
298 * any of the device's parent bridges match.
299 */
300 for (; dev != NULL; dev = dev->bus->self) {
301 if (pci_domain_nr(dev->bus) == pdev_id->domain
302 && dev->bus->number == pdev_id->bus
303 && dev->devfn == pdev_id->devfn)
304 return 1;
305
306 /* Sometimes topmost bridge links to itself. */
307 if (dev == dev->bus->self)
308 break;
309 }
310
311 return 0;
312}
313
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800314static int pcistub_match(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400315{
316 struct pcistub_device_id *pdev_id;
317 unsigned long flags;
318 int found = 0;
319
320 spin_lock_irqsave(&device_ids_lock, flags);
321 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
322 if (pcistub_match_one(dev, pdev_id)) {
323 found = 1;
324 break;
325 }
326 }
327 spin_unlock_irqrestore(&device_ids_lock, flags);
328
329 return found;
330}
331
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800332static int pcistub_init_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400333{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400334 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400335 int err = 0;
336
337 dev_dbg(&dev->dev, "initializing...\n");
338
339 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400340 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400341 * would need to be called somewhere to free the memory allocated
342 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
343 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400344 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
345 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400346 if (!dev_data) {
347 err = -ENOMEM;
348 goto out;
349 }
350 pci_set_drvdata(dev, dev_data);
351
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400352 /*
353 * Setup name for fake IRQ handler. It will only be enabled
354 * once the device is turned on by the guest.
355 */
356 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
357
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400358 dev_dbg(&dev->dev, "initializing config\n");
359
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400360 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
361 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400362 if (err)
363 goto out;
364
365 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
366 * must do this here because pcibios_enable_device may specify
367 * the pci device's true irq (and possibly its other resources)
368 * if they differ from what's in the configuration space.
369 * This makes the assumption that the device's resources won't
370 * change after this point (otherwise this code may break!)
371 */
372 dev_dbg(&dev->dev, "enabling device\n");
373 err = pci_enable_device(dev);
374 if (err)
375 goto config_release;
376
Jan Beulichd69c0e32013-05-29 13:31:15 +0100377 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000378 struct physdev_pci_device ppdev = {
379 .seg = pci_domain_nr(dev->bus),
380 .bus = dev->bus->number,
381 .devfn = dev->devfn
382 };
383
384 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
385 if (err)
386 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
387 err);
388 }
389
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500390 /* We need the device active to save the state. */
391 dev_dbg(&dev->dev, "save state of device\n");
392 pci_save_state(dev);
393 dev_data->pci_saved_state = pci_store_saved_state(dev);
394 if (!dev_data->pci_saved_state)
395 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400396 else {
Masanari Iida744627e92012-11-05 23:30:40 +0900397 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400398 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400399 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400400 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400401 /* Now disable the device (this also ensures some private device
402 * data is setup before we export)
403 */
404 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400405 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400406
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500407 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400408 return 0;
409
410config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400411 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400412
413out:
414 pci_set_drvdata(dev, NULL);
415 kfree(dev_data);
416 return err;
417}
418
419/*
420 * Because some initialization still happens on
421 * devices during fs_initcall, we need to defer
422 * full initialization of our devices until
423 * device_initcall.
424 */
425static int __init pcistub_init_devices_late(void)
426{
427 struct pcistub_device *psdev;
428 unsigned long flags;
429 int err = 0;
430
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400431 spin_lock_irqsave(&pcistub_devices_lock, flags);
432
433 while (!list_empty(&seized_devices)) {
434 psdev = container_of(seized_devices.next,
435 struct pcistub_device, dev_list);
436 list_del(&psdev->dev_list);
437
438 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
439
440 err = pcistub_init_device(psdev->dev);
441 if (err) {
442 dev_err(&psdev->dev->dev,
443 "error %d initializing device\n", err);
444 kfree(psdev);
445 psdev = NULL;
446 }
447
448 spin_lock_irqsave(&pcistub_devices_lock, flags);
449
450 if (psdev)
451 list_add_tail(&psdev->dev_list, &pcistub_devices);
452 }
453
454 initialize_devices = 1;
455
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
457
458 return 0;
459}
460
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800461static int pcistub_seize(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400462{
463 struct pcistub_device *psdev;
464 unsigned long flags;
465 int err = 0;
466
467 psdev = pcistub_device_alloc(dev);
468 if (!psdev)
469 return -ENOMEM;
470
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
472
473 if (initialize_devices) {
474 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
475
476 /* don't want irqs disabled when calling pcistub_init_device */
477 err = pcistub_init_device(psdev->dev);
478
479 spin_lock_irqsave(&pcistub_devices_lock, flags);
480
481 if (!err)
482 list_add(&psdev->dev_list, &pcistub_devices);
483 } else {
484 dev_dbg(&dev->dev, "deferring initialization\n");
485 list_add(&psdev->dev_list, &seized_devices);
486 }
487
488 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
489
490 if (err)
491 pcistub_device_put(psdev);
492
493 return err;
494}
495
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800496static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400497{
498 int err = 0;
499
500 dev_dbg(&dev->dev, "probing...\n");
501
502 if (pcistub_match(dev)) {
503
504 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
505 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
506 dev_err(&dev->dev, "can't export pci devices that "
507 "don't have a normal (0) or bridge (1) "
508 "header type!\n");
509 err = -ENODEV;
510 goto out;
511 }
512
513 dev_info(&dev->dev, "seizing device\n");
514 err = pcistub_seize(dev);
515 } else
516 /* Didn't find the device */
517 err = -ENODEV;
518
519out:
520 return err;
521}
522
523static void pcistub_remove(struct pci_dev *dev)
524{
525 struct pcistub_device *psdev, *found_psdev = NULL;
526 unsigned long flags;
527
528 dev_dbg(&dev->dev, "removing\n");
529
530 spin_lock_irqsave(&pcistub_devices_lock, flags);
531
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400532 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400533
534 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
535 if (psdev->dev == dev) {
536 found_psdev = psdev;
537 break;
538 }
539 }
540
541 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
542
543 if (found_psdev) {
544 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
545 found_psdev->pdev);
546
547 if (found_psdev->pdev) {
Joe Perches283c0972013-06-28 03:21:41 -0700548 pr_warn("****** removing device %s while still in-use! ******\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400549 pci_name(found_psdev->dev));
Joe Perches283c0972013-06-28 03:21:41 -0700550 pr_warn("****** driver domain may still access this device's i/o resources!\n");
551 pr_warn("****** shutdown driver domain before binding device\n");
552 pr_warn("****** to other drivers or domains\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400553
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -0500554 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
555 * doing the FLR. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400556 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400557 found_psdev->dev);
558 }
559
560 spin_lock_irqsave(&pcistub_devices_lock, flags);
561 list_del(&found_psdev->dev_list);
562 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
563
564 /* the final put for releasing from the list */
565 pcistub_device_put(found_psdev);
566 }
567}
568
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400569static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400570 {
571 .vendor = PCI_ANY_ID,
572 .device = PCI_ANY_ID,
573 .subvendor = PCI_ANY_ID,
574 .subdevice = PCI_ANY_ID,
575 },
576 {0,},
577};
578
579#define PCI_NODENAME_MAX 40
580static void kill_domain_by_device(struct pcistub_device *psdev)
581{
582 struct xenbus_transaction xbt;
583 int err;
584 char nodename[PCI_NODENAME_MAX];
585
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400586 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400587 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
588 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400589
590again:
591 err = xenbus_transaction_start(&xbt);
592 if (err) {
593 dev_err(&psdev->dev->dev,
594 "error %d when start xenbus transaction\n", err);
595 return;
596 }
597 /*PV AER handlers will set this flag*/
598 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
599 err = xenbus_transaction_end(xbt, 0);
600 if (err) {
601 if (err == -EAGAIN)
602 goto again;
603 dev_err(&psdev->dev->dev,
604 "error %d when end xenbus transaction\n", err);
605 return;
606 }
607}
608
609/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400610 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400611 * jobs: send service request and waiting for front_end response.
612*/
613static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400614 pci_channel_state_t state, int aer_cmd,
615 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400616{
617 pci_ers_result_t res = result;
618 struct xen_pcie_aer_op *aer_op;
619 int ret;
620
621 /*with PV AER drivers*/
622 aer_op = &(psdev->pdev->sh_info->aer_op);
623 aer_op->cmd = aer_cmd ;
624 /*useful for error_detected callback*/
625 aer_op->err = state;
626 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400627 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400628 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
629 if (!ret) {
630 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400631 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400632 return PCI_ERS_RESULT_NONE;
633 }
634 wmb();
635
636 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400637 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400638 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400639 /*local flag to mark there's aer request, xen_pcibk callback will use
640 * this flag to judge whether we need to check pci-front give aer
641 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400642 */
643 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
644
645 /*It is possible that a pcifront conf_read_write ops request invokes
646 * the callback which cause the spurious execution of wake_up.
647 * Yet it is harmless and better than a spinlock here
648 */
649 set_bit(_XEN_PCIB_active,
650 (unsigned long *)&psdev->pdev->sh_info->flags);
651 wmb();
652 notify_remote_via_irq(psdev->pdev->evtchn_irq);
653
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400654 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
655 !(test_bit(_XEN_PCIB_active, (unsigned long *)
656 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400657
658 if (!ret) {
659 if (test_bit(_XEN_PCIB_active,
660 (unsigned long *)&psdev->pdev->sh_info->flags)) {
661 dev_err(&psdev->dev->dev,
662 "pcifront aer process not responding!\n");
663 clear_bit(_XEN_PCIB_active,
664 (unsigned long *)&psdev->pdev->sh_info->flags);
665 aer_op->err = PCI_ERS_RESULT_NONE;
666 return res;
667 }
668 }
669 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
670
671 if (test_bit(_XEN_PCIF_active,
672 (unsigned long *)&psdev->pdev->sh_info->flags)) {
673 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400674 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400675 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400676 }
677
678 res = (pci_ers_result_t)aer_op->err;
679 return res;
680}
681
682/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400683* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400684* of the device driver could provide this service, and then wait for pcifront
685* ack.
686* @dev: pointer to PCI devices
687* return value is used by aer_core do_recovery policy
688*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400689static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400690{
691 struct pcistub_device *psdev;
692 pci_ers_result_t result;
693
694 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400695 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400696 dev->bus->number, dev->devfn);
697
698 down_write(&pcistub_sem);
699 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
700 dev->bus->number,
701 PCI_SLOT(dev->devfn),
702 PCI_FUNC(dev->devfn));
703
704 if (!psdev || !psdev->pdev) {
705 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400706 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400707 goto end;
708 }
709
710 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400711 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400712 " by HVM, kill it\n");
713 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100714 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400715 }
716
717 if (!test_bit(_XEN_PCIB_AERHANDLER,
718 (unsigned long *)&psdev->pdev->sh_info->flags)) {
719 dev_err(&dev->dev,
720 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100721 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400722 }
723 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
724
725 if (result == PCI_ERS_RESULT_NONE ||
726 result == PCI_ERS_RESULT_DISCONNECT) {
727 dev_dbg(&dev->dev,
728 "No AER slot_reset service or disconnected!\n");
729 kill_domain_by_device(psdev);
730 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400731end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100732 if (psdev)
733 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400734 up_write(&pcistub_sem);
735 return result;
736
737}
738
739
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400740/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400741* in case of the device driver could provide this service, and then wait
742* for pcifront ack
743* @dev: pointer to PCI devices
744* return value is used by aer_core do_recovery policy
745*/
746
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400747static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400748{
749 struct pcistub_device *psdev;
750 pci_ers_result_t result;
751
752 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400753 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400754 dev->bus->number, dev->devfn);
755
756 down_write(&pcistub_sem);
757 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
758 dev->bus->number,
759 PCI_SLOT(dev->devfn),
760 PCI_FUNC(dev->devfn));
761
762 if (!psdev || !psdev->pdev) {
763 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400764 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400765 goto end;
766 }
767
768 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400769 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400770 " by HVM, kill it\n");
771 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100772 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400773 }
774
775 if (!test_bit(_XEN_PCIB_AERHANDLER,
776 (unsigned long *)&psdev->pdev->sh_info->flags)) {
777 dev_err(&dev->dev,
778 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100779 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400780 }
781 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
782
783 if (result == PCI_ERS_RESULT_NONE ||
784 result == PCI_ERS_RESULT_DISCONNECT) {
785 dev_dbg(&dev->dev,
786 "No AER mmio_enabled service or disconnected!\n");
787 kill_domain_by_device(psdev);
788 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400789end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100790 if (psdev)
791 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400792 up_write(&pcistub_sem);
793 return result;
794}
795
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400796/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400797* in case of the device driver could provide this service, and then wait
798* for pcifront ack.
799* @dev: pointer to PCI devices
800* @error: the current PCI connection state
801* return value is used by aer_core do_recovery policy
802*/
803
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400804static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400805 pci_channel_state_t error)
806{
807 struct pcistub_device *psdev;
808 pci_ers_result_t result;
809
810 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400811 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400812 dev->bus->number, dev->devfn);
813
814 down_write(&pcistub_sem);
815 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
816 dev->bus->number,
817 PCI_SLOT(dev->devfn),
818 PCI_FUNC(dev->devfn));
819
820 if (!psdev || !psdev->pdev) {
821 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400822 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400823 goto end;
824 }
825
826 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400827 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400828 " by HVM, kill it\n");
829 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100830 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400831 }
832
833 /*Guest owns the device yet no aer handler regiested, kill guest*/
834 if (!test_bit(_XEN_PCIB_AERHANDLER,
835 (unsigned long *)&psdev->pdev->sh_info->flags)) {
836 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
837 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100838 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400839 }
840 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
841
842 if (result == PCI_ERS_RESULT_NONE ||
843 result == PCI_ERS_RESULT_DISCONNECT) {
844 dev_dbg(&dev->dev,
845 "No AER error_detected service or disconnected!\n");
846 kill_domain_by_device(psdev);
847 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400848end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100849 if (psdev)
850 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400851 up_write(&pcistub_sem);
852 return result;
853}
854
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400855/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400856* in case of the device driver could provide this service, and then wait
857* for pcifront ack.
858* @dev: pointer to PCI devices
859*/
860
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400861static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400862{
863 struct pcistub_device *psdev;
864
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400865 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400866 dev->bus->number, dev->devfn);
867
868 down_write(&pcistub_sem);
869 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
870 dev->bus->number,
871 PCI_SLOT(dev->devfn),
872 PCI_FUNC(dev->devfn));
873
874 if (!psdev || !psdev->pdev) {
875 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400876 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400877 goto end;
878 }
879
880 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400881 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400882 " by HVM, kill it\n");
883 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100884 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400885 }
886
887 if (!test_bit(_XEN_PCIB_AERHANDLER,
888 (unsigned long *)&psdev->pdev->sh_info->flags)) {
889 dev_err(&dev->dev,
890 "guest with no AER driver should have been killed\n");
891 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100892 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400893 }
894 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
895 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400896end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100897 if (psdev)
898 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400899 up_write(&pcistub_sem);
900 return;
901}
902
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400903/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700904static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400905 .error_detected = xen_pcibk_error_detected,
906 .mmio_enabled = xen_pcibk_mmio_enabled,
907 .slot_reset = xen_pcibk_slot_reset,
908 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400909};
910
911/*
912 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
913 * for a normal device. I don't want it to be loaded automatically.
914 */
915
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400916static struct pci_driver xen_pcibk_pci_driver = {
917 /* The name should be xen_pciback, but until the tools are updated
918 * we will keep it as pciback. */
919 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400920 .id_table = pcistub_ids,
921 .probe = pcistub_probe,
922 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400923 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400924};
925
926static inline int str_to_slot(const char *buf, int *domain, int *bus,
927 int *slot, int *func)
928{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000929 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400930
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000931 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
932 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100933 case 3:
934 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000935 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100936 break;
937 case 2:
938 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000939 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100940 break;
941 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000942 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400943 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400944
945 /* try again without domain */
946 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000947 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100948 case 2:
949 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000950 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100951 break;
952 case 1:
953 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000954 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100955 break;
956 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000957 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400958 return 0;
959
960 return -EINVAL;
961}
962
963static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
964 *slot, int *func, int *reg, int *size, int *mask)
965{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000966 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400967
Jan Beulichb3e40b72012-11-02 14:37:13 +0000968 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000969 reg, size, mask, &parsed);
970 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400971 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000972
973 /* try again without domain */
974 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000975 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000976 mask, &parsed);
977 if (parsed && !buf[parsed])
978 return 0;
979
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400980 return -EINVAL;
981}
982
983static int pcistub_device_id_add(int domain, int bus, int slot, int func)
984{
985 struct pcistub_device_id *pci_dev_id;
986 unsigned long flags;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000987 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100988
989 if (slot < 0) {
990 for (slot = 0; !rc && slot < 32; ++slot)
991 rc = pcistub_device_id_add(domain, bus, slot, func);
992 return rc;
993 }
994
995 if (func < 0) {
996 for (func = 0; !rc && func < 8; ++func)
997 rc = pcistub_device_id_add(domain, bus, slot, func);
998 return rc;
999 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001000
Jan Beulichb3e40b72012-11-02 14:37:13 +00001001 if ((
1002#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1003 || !defined(CONFIG_PCI_DOMAINS)
1004 !pci_domains_supported ? domain :
1005#endif
1006 domain < 0 || domain > 0xffff)
1007 || bus < 0 || bus > 0xff
1008 || PCI_SLOT(devfn) != slot
1009 || PCI_FUNC(devfn) != func)
1010 return -EINVAL;
1011
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001012 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1013 if (!pci_dev_id)
1014 return -ENOMEM;
1015
1016 pci_dev_id->domain = domain;
1017 pci_dev_id->bus = bus;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001018 pci_dev_id->devfn = devfn;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001019
Joe Perches283c0972013-06-28 03:21:41 -07001020 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001021 domain, bus, slot, func);
1022
1023 spin_lock_irqsave(&device_ids_lock, flags);
1024 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1025 spin_unlock_irqrestore(&device_ids_lock, flags);
1026
1027 return 0;
1028}
1029
1030static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1031{
1032 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001033 int err = -ENOENT;
1034 unsigned long flags;
1035
1036 spin_lock_irqsave(&device_ids_lock, flags);
1037 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1038 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001039 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1040 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1041 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001042 /* Don't break; here because it's possible the same
1043 * slot could be in the list more than once
1044 */
1045 list_del(&pci_dev_id->slot_list);
1046 kfree(pci_dev_id);
1047
1048 err = 0;
1049
Joe Perches283c0972013-06-28 03:21:41 -07001050 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1051 domain, bus, slot, func);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001052 }
1053 }
1054 spin_unlock_irqrestore(&device_ids_lock, flags);
1055
1056 return err;
1057}
1058
Jan Beulichb3e40b72012-11-02 14:37:13 +00001059static int pcistub_reg_add(int domain, int bus, int slot, int func,
1060 unsigned int reg, unsigned int size,
1061 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001062{
1063 int err = 0;
1064 struct pcistub_device *psdev;
1065 struct pci_dev *dev;
1066 struct config_field *field;
1067
Jan Beulichb3e40b72012-11-02 14:37:13 +00001068 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1069 return -EINVAL;
1070
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001071 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001072 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001073 err = -ENODEV;
1074 goto out;
1075 }
1076 dev = psdev->dev;
1077
1078 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1079 if (!field) {
1080 err = -ENOMEM;
1081 goto out;
1082 }
1083
1084 field->offset = reg;
1085 field->size = size;
1086 field->mask = mask;
1087 field->init = NULL;
1088 field->reset = NULL;
1089 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001090 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001091
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001092 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001093 if (err)
1094 kfree(field);
1095out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001096 if (psdev)
1097 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001098 return err;
1099}
1100
1101static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1102 size_t count)
1103{
1104 int domain, bus, slot, func;
1105 int err;
1106
1107 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1108 if (err)
1109 goto out;
1110
1111 err = pcistub_device_id_add(domain, bus, slot, func);
1112
1113out:
1114 if (!err)
1115 err = count;
1116 return err;
1117}
Jan Beulich402c5e12011-09-21 16:22:11 -04001118static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001119
1120static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1121 size_t count)
1122{
1123 int domain, bus, slot, func;
1124 int err;
1125
1126 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1127 if (err)
1128 goto out;
1129
1130 err = pcistub_device_id_remove(domain, bus, slot, func);
1131
1132out:
1133 if (!err)
1134 err = count;
1135 return err;
1136}
Jan Beulich402c5e12011-09-21 16:22:11 -04001137static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001138
1139static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1140{
1141 struct pcistub_device_id *pci_dev_id;
1142 size_t count = 0;
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&device_ids_lock, flags);
1146 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1147 if (count >= PAGE_SIZE)
1148 break;
1149
1150 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001151 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001152 pci_dev_id->domain, pci_dev_id->bus,
1153 PCI_SLOT(pci_dev_id->devfn),
1154 PCI_FUNC(pci_dev_id->devfn));
1155 }
1156 spin_unlock_irqrestore(&device_ids_lock, flags);
1157
1158 return count;
1159}
Jan Beulich402c5e12011-09-21 16:22:11 -04001160static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001161
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001162static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1163{
1164 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001165 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001166 size_t count = 0;
1167 unsigned long flags;
1168
1169 spin_lock_irqsave(&pcistub_devices_lock, flags);
1170 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1171 if (count >= PAGE_SIZE)
1172 break;
1173 if (!psdev->dev)
1174 continue;
1175 dev_data = pci_get_drvdata(psdev->dev);
1176 if (!dev_data)
1177 continue;
1178 count +=
1179 scnprintf(buf + count, PAGE_SIZE - count,
1180 "%s:%s:%sing:%ld\n",
1181 pci_name(psdev->dev),
1182 dev_data->isr_on ? "on" : "off",
1183 dev_data->ack_intr ? "ack" : "not ack",
1184 dev_data->handled);
1185 }
1186 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1187 return count;
1188}
Jan Beulich402c5e12011-09-21 16:22:11 -04001189static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001190
1191static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1192 const char *buf,
1193 size_t count)
1194{
1195 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001196 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001197 int domain, bus, slot, func;
Wei Yongjun405010d2013-05-31 19:59:20 +08001198 int err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001199
1200 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1201 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001202 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001203
1204 psdev = pcistub_device_find(domain, bus, slot, func);
Wei Yongjun405010d2013-05-31 19:59:20 +08001205 if (!psdev) {
1206 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001207 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001208 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001209
1210 dev_data = pci_get_drvdata(psdev->dev);
Wei Yongjun405010d2013-05-31 19:59:20 +08001211 if (!dev_data) {
1212 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001213 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001214 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001215
1216 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1217 dev_data->irq_name, dev_data->isr_on,
1218 !dev_data->isr_on);
1219
1220 dev_data->isr_on = !(dev_data->isr_on);
1221 if (dev_data->isr_on)
1222 dev_data->ack_intr = 1;
1223out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001224 if (psdev)
1225 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001226 if (!err)
1227 err = count;
1228 return err;
1229}
Jan Beulich402c5e12011-09-21 16:22:11 -04001230static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1231 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001232
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001233static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1234 size_t count)
1235{
1236 int domain, bus, slot, func, reg, size, mask;
1237 int err;
1238
1239 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1240 &mask);
1241 if (err)
1242 goto out;
1243
1244 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1245
1246out:
1247 if (!err)
1248 err = count;
1249 return err;
1250}
1251
1252static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1253{
1254 int count = 0;
1255 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001256 struct xen_pcibk_config_quirk *quirk;
1257 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001258 const struct config_field *field;
1259 const struct config_field_entry *cfg_entry;
1260
1261 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001262 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001263 if (count >= PAGE_SIZE)
1264 goto out;
1265
1266 count += scnprintf(buf + count, PAGE_SIZE - count,
1267 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1268 quirk->pdev->bus->number,
1269 PCI_SLOT(quirk->pdev->devfn),
1270 PCI_FUNC(quirk->pdev->devfn),
1271 quirk->devid.vendor, quirk->devid.device,
1272 quirk->devid.subvendor,
1273 quirk->devid.subdevice);
1274
1275 dev_data = pci_get_drvdata(quirk->pdev);
1276
1277 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1278 field = cfg_entry->field;
1279 if (count >= PAGE_SIZE)
1280 goto out;
1281
1282 count += scnprintf(buf + count, PAGE_SIZE - count,
1283 "\t\t%08x:%01x:%08x\n",
1284 cfg_entry->base_offset +
1285 field->offset, field->size,
1286 field->mask);
1287 }
1288 }
1289
1290out:
1291 spin_unlock_irqrestore(&device_ids_lock, flags);
1292
1293 return count;
1294}
Jan Beulich402c5e12011-09-21 16:22:11 -04001295static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1296 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001297
1298static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1299 size_t count)
1300{
1301 int domain, bus, slot, func;
1302 int err;
1303 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001304 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001305
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001306 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1307 if (err)
1308 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001309
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001310 psdev = pcistub_device_find(domain, bus, slot, func);
1311 if (!psdev) {
1312 err = -ENODEV;
1313 goto out;
1314 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001315
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001316 dev_data = pci_get_drvdata(psdev->dev);
1317 /* the driver data for a device should never be null at this point */
1318 if (!dev_data) {
1319 err = -ENXIO;
1320 goto release;
1321 }
1322 if (!dev_data->permissive) {
1323 dev_data->permissive = 1;
1324 /* Let user know that what they're doing could be unsafe */
1325 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1326 "configuration space accesses!\n");
1327 dev_warn(&psdev->dev->dev,
1328 "permissive mode is potentially unsafe!\n");
1329 }
1330release:
1331 pcistub_device_put(psdev);
1332out:
1333 if (!err)
1334 err = count;
1335 return err;
1336}
1337
1338static ssize_t permissive_show(struct device_driver *drv, char *buf)
1339{
1340 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001341 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001342 size_t count = 0;
1343 unsigned long flags;
1344 spin_lock_irqsave(&pcistub_devices_lock, flags);
1345 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1346 if (count >= PAGE_SIZE)
1347 break;
1348 if (!psdev->dev)
1349 continue;
1350 dev_data = pci_get_drvdata(psdev->dev);
1351 if (!dev_data || !dev_data->permissive)
1352 continue;
1353 count +=
1354 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1355 pci_name(psdev->dev));
1356 }
1357 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1358 return count;
1359}
Jan Beulich402c5e12011-09-21 16:22:11 -04001360static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1361 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001362
1363static void pcistub_exit(void)
1364{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001365 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1366 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001367 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001368 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1369 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1370 driver_remove_file(&xen_pcibk_pci_driver.driver,
1371 &driver_attr_permissive);
1372 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001373 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001374 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001375 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001376 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001377}
1378
1379static int __init pcistub_init(void)
1380{
1381 int pos = 0;
1382 int err = 0;
1383 int domain, bus, slot, func;
1384 int parsed;
1385
1386 if (pci_devs_to_hide && *pci_devs_to_hide) {
1387 do {
1388 parsed = 0;
1389
1390 err = sscanf(pci_devs_to_hide + pos,
1391 " (%x:%x:%x.%x) %n",
1392 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001393 switch (err) {
1394 case 3:
1395 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001396 sscanf(pci_devs_to_hide + pos,
1397 " (%x:%x:%x.*) %n",
1398 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001399 break;
1400 case 2:
1401 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001402 sscanf(pci_devs_to_hide + pos,
1403 " (%x:%x:*.*) %n",
1404 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001405 break;
1406 }
1407
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001408 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001409 domain = 0;
1410 err = sscanf(pci_devs_to_hide + pos,
1411 " (%x:%x.%x) %n",
1412 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001413 switch (err) {
1414 case 2:
1415 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001416 sscanf(pci_devs_to_hide + pos,
1417 " (%x:%x.*) %n",
1418 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001419 break;
1420 case 1:
1421 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001422 sscanf(pci_devs_to_hide + pos,
1423 " (%x:*.*) %n",
1424 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001425 break;
1426 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001427 }
1428
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001429 if (parsed <= 0)
1430 goto parse_error;
1431
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001432 err = pcistub_device_id_add(domain, bus, slot, func);
1433 if (err)
1434 goto out;
1435
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001436 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001437 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001438 }
1439
1440 /* If we're the first PCI Device Driver to register, we're the
1441 * first one to get offered PCI devices as they become
1442 * available (and thus we can be the first to grab them)
1443 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001444 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001445 if (err < 0)
1446 goto out;
1447
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001448 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001449 &driver_attr_new_slot);
1450 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001451 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001452 &driver_attr_remove_slot);
1453 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001454 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001455 &driver_attr_slots);
1456 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001457 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001458 &driver_attr_quirks);
1459 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001460 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001461 &driver_attr_permissive);
1462
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001463 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001464 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001465 &driver_attr_irq_handlers);
1466 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001467 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001468 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001469 if (err)
1470 pcistub_exit();
1471
1472out:
1473 return err;
1474
1475parse_error:
Joe Perches283c0972013-06-28 03:21:41 -07001476 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001477 pci_devs_to_hide + pos);
1478 return -EINVAL;
1479}
1480
1481#ifndef MODULE
1482/*
1483 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001484 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001485 * want to suck up any device before other drivers
1486 * get a chance by being the first pci device
1487 * driver to register)
1488 */
1489fs_initcall(pcistub_init);
1490#endif
1491
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001492static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001493{
1494 int err;
1495
1496 if (!xen_initial_domain())
1497 return -ENODEV;
1498
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001499 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001500 if (err)
1501 return err;
1502
1503#ifdef MODULE
1504 err = pcistub_init();
1505 if (err < 0)
1506 return err;
1507#endif
1508
1509 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001510 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001511 if (err)
1512 pcistub_exit();
1513
1514 return err;
1515}
1516
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001517static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001518{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001519 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001520 pcistub_exit();
1521}
1522
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001523module_init(xen_pcibk_init);
1524module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001525
1526MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001527MODULE_ALIAS("xen-backend:pci");