blob: 017069a455d432af73af8939aee93195a5821a61 [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
Ethan Zhaobe507fd2014-09-09 10:21:27 +0800136 pci_clear_dev_assigned(dev);
Jan Beulich909b3fd2013-03-12 15:06:23 +0000137 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
Konrad Rzeszutek Wilk0a9fd012014-04-22 10:48:17 -0400245/*
246 * Called when:
247 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
248 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
249 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
250 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
251 *
252 * As such we have to be careful.
253 */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400254void pcistub_put_pci_dev(struct pci_dev *dev)
255{
256 struct pcistub_device *psdev, *found_psdev = NULL;
257 unsigned long flags;
258
259 spin_lock_irqsave(&pcistub_devices_lock, flags);
260
261 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
262 if (psdev->dev == dev) {
263 found_psdev = psdev;
264 break;
265 }
266 }
267
268 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400269 if (WARN_ON(!found_psdev))
270 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400271
272 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400273 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400274 */
275 down_write(&pcistub_sem);
276 /* Cleanup our device
277 * (so it's ready for the next domain)
278 */
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500279
280 /* This is OK - we are running from workqueue context
281 * and want to inhibit the user from fiddling with 'reset'
282 */
283 pci_reset_function(dev);
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500284 pci_restore_state(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500285
286 /* This disables the device. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500287 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500288
289 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500290 xen_pcibk_config_reset_dev(dev);
Konrad Rzeszutek Wilkfcb8ce92013-12-03 21:37:24 -0500291 xen_pcibk_config_free_dyn_fields(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400292
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500293 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500294
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400295 spin_lock_irqsave(&found_psdev->lock, flags);
296 found_psdev->pdev = NULL;
297 spin_unlock_irqrestore(&found_psdev->lock, flags);
298
299 pcistub_device_put(found_psdev);
300 up_write(&pcistub_sem);
301}
302
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800303static int pcistub_match_one(struct pci_dev *dev,
304 struct pcistub_device_id *pdev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400305{
306 /* Match the specified device by domain, bus, slot, func and also if
307 * any of the device's parent bridges match.
308 */
309 for (; dev != NULL; dev = dev->bus->self) {
310 if (pci_domain_nr(dev->bus) == pdev_id->domain
311 && dev->bus->number == pdev_id->bus
312 && dev->devfn == pdev_id->devfn)
313 return 1;
314
315 /* Sometimes topmost bridge links to itself. */
316 if (dev == dev->bus->self)
317 break;
318 }
319
320 return 0;
321}
322
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800323static int pcistub_match(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400324{
325 struct pcistub_device_id *pdev_id;
326 unsigned long flags;
327 int found = 0;
328
329 spin_lock_irqsave(&device_ids_lock, flags);
330 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
331 if (pcistub_match_one(dev, pdev_id)) {
332 found = 1;
333 break;
334 }
335 }
336 spin_unlock_irqrestore(&device_ids_lock, flags);
337
338 return found;
339}
340
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800341static int pcistub_init_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400342{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400343 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400344 int err = 0;
345
346 dev_dbg(&dev->dev, "initializing...\n");
347
348 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400349 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400350 * would need to be called somewhere to free the memory allocated
351 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
352 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400353 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
354 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400355 if (!dev_data) {
356 err = -ENOMEM;
357 goto out;
358 }
359 pci_set_drvdata(dev, dev_data);
360
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400361 /*
362 * Setup name for fake IRQ handler. It will only be enabled
363 * once the device is turned on by the guest.
364 */
365 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
366
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400367 dev_dbg(&dev->dev, "initializing config\n");
368
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400369 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
370 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400371 if (err)
372 goto out;
373
374 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
375 * must do this here because pcibios_enable_device may specify
376 * the pci device's true irq (and possibly its other resources)
377 * if they differ from what's in the configuration space.
378 * This makes the assumption that the device's resources won't
379 * change after this point (otherwise this code may break!)
380 */
381 dev_dbg(&dev->dev, "enabling device\n");
382 err = pci_enable_device(dev);
383 if (err)
384 goto config_release;
385
Jan Beulichd69c0e32013-05-29 13:31:15 +0100386 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000387 struct physdev_pci_device ppdev = {
388 .seg = pci_domain_nr(dev->bus),
389 .bus = dev->bus->number,
390 .devfn = dev->devfn
391 };
392
393 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
394 if (err)
395 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
396 err);
397 }
398
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500399 /* We need the device active to save the state. */
400 dev_dbg(&dev->dev, "save state of device\n");
401 pci_save_state(dev);
402 dev_data->pci_saved_state = pci_store_saved_state(dev);
403 if (!dev_data->pci_saved_state)
404 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400405 else {
Masanari Iida744627e92012-11-05 23:30:40 +0900406 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400407 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400408 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400409 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400410 /* Now disable the device (this also ensures some private device
411 * data is setup before we export)
412 */
413 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400414 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400415
Ethan Zhaobe507fd2014-09-09 10:21:27 +0800416 pci_set_dev_assigned(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400417 return 0;
418
419config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400420 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400421
422out:
423 pci_set_drvdata(dev, NULL);
424 kfree(dev_data);
425 return err;
426}
427
428/*
429 * Because some initialization still happens on
430 * devices during fs_initcall, we need to defer
431 * full initialization of our devices until
432 * device_initcall.
433 */
434static int __init pcistub_init_devices_late(void)
435{
436 struct pcistub_device *psdev;
437 unsigned long flags;
438 int err = 0;
439
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400440 spin_lock_irqsave(&pcistub_devices_lock, flags);
441
442 while (!list_empty(&seized_devices)) {
443 psdev = container_of(seized_devices.next,
444 struct pcistub_device, dev_list);
445 list_del(&psdev->dev_list);
446
447 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
448
449 err = pcistub_init_device(psdev->dev);
450 if (err) {
451 dev_err(&psdev->dev->dev,
452 "error %d initializing device\n", err);
453 kfree(psdev);
454 psdev = NULL;
455 }
456
457 spin_lock_irqsave(&pcistub_devices_lock, flags);
458
459 if (psdev)
460 list_add_tail(&psdev->dev_list, &pcistub_devices);
461 }
462
463 initialize_devices = 1;
464
465 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
466
467 return 0;
468}
469
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800470static int pcistub_seize(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400471{
472 struct pcistub_device *psdev;
473 unsigned long flags;
474 int err = 0;
475
476 psdev = pcistub_device_alloc(dev);
477 if (!psdev)
478 return -ENOMEM;
479
480 spin_lock_irqsave(&pcistub_devices_lock, flags);
481
482 if (initialize_devices) {
483 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
484
485 /* don't want irqs disabled when calling pcistub_init_device */
486 err = pcistub_init_device(psdev->dev);
487
488 spin_lock_irqsave(&pcistub_devices_lock, flags);
489
490 if (!err)
491 list_add(&psdev->dev_list, &pcistub_devices);
492 } else {
493 dev_dbg(&dev->dev, "deferring initialization\n");
494 list_add(&psdev->dev_list, &seized_devices);
495 }
496
497 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
498
499 if (err)
500 pcistub_device_put(psdev);
501
502 return err;
503}
504
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400505/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
506 * other functions that take the sysfs lock. */
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800507static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400508{
509 int err = 0;
510
511 dev_dbg(&dev->dev, "probing...\n");
512
513 if (pcistub_match(dev)) {
514
515 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
516 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
517 dev_err(&dev->dev, "can't export pci devices that "
518 "don't have a normal (0) or bridge (1) "
519 "header type!\n");
520 err = -ENODEV;
521 goto out;
522 }
523
524 dev_info(&dev->dev, "seizing device\n");
525 err = pcistub_seize(dev);
526 } else
527 /* Didn't find the device */
528 err = -ENODEV;
529
530out:
531 return err;
532}
533
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400534/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
535 * other functions that take the sysfs lock. */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400536static void pcistub_remove(struct pci_dev *dev)
537{
538 struct pcistub_device *psdev, *found_psdev = NULL;
539 unsigned long flags;
540
541 dev_dbg(&dev->dev, "removing\n");
542
543 spin_lock_irqsave(&pcistub_devices_lock, flags);
544
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400545 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400546
547 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
548 if (psdev->dev == dev) {
549 found_psdev = psdev;
550 break;
551 }
552 }
553
554 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
555
556 if (found_psdev) {
557 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
558 found_psdev->pdev);
559
560 if (found_psdev->pdev) {
Joe Perches283c0972013-06-28 03:21:41 -0700561 pr_warn("****** removing device %s while still in-use! ******\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400562 pci_name(found_psdev->dev));
Joe Perches283c0972013-06-28 03:21:41 -0700563 pr_warn("****** driver domain may still access this device's i/o resources!\n");
564 pr_warn("****** shutdown driver domain before binding device\n");
565 pr_warn("****** to other drivers or domains\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400566
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -0500567 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
568 * doing the FLR. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400569 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400570 found_psdev->dev);
571 }
572
573 spin_lock_irqsave(&pcistub_devices_lock, flags);
574 list_del(&found_psdev->dev_list);
575 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
576
577 /* the final put for releasing from the list */
578 pcistub_device_put(found_psdev);
579 }
580}
581
Benoit Taine9baa3c32014-08-08 15:56:03 +0200582static const struct pci_device_id pcistub_ids[] = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400583 {
584 .vendor = PCI_ANY_ID,
585 .device = PCI_ANY_ID,
586 .subvendor = PCI_ANY_ID,
587 .subdevice = PCI_ANY_ID,
588 },
589 {0,},
590};
591
592#define PCI_NODENAME_MAX 40
593static void kill_domain_by_device(struct pcistub_device *psdev)
594{
595 struct xenbus_transaction xbt;
596 int err;
597 char nodename[PCI_NODENAME_MAX];
598
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400599 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400600 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
601 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400602
603again:
604 err = xenbus_transaction_start(&xbt);
605 if (err) {
606 dev_err(&psdev->dev->dev,
607 "error %d when start xenbus transaction\n", err);
608 return;
609 }
610 /*PV AER handlers will set this flag*/
611 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
612 err = xenbus_transaction_end(xbt, 0);
613 if (err) {
614 if (err == -EAGAIN)
615 goto again;
616 dev_err(&psdev->dev->dev,
617 "error %d when end xenbus transaction\n", err);
618 return;
619 }
620}
621
622/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400623 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400624 * jobs: send service request and waiting for front_end response.
625*/
626static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400627 pci_channel_state_t state, int aer_cmd,
628 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400629{
630 pci_ers_result_t res = result;
631 struct xen_pcie_aer_op *aer_op;
632 int ret;
633
634 /*with PV AER drivers*/
635 aer_op = &(psdev->pdev->sh_info->aer_op);
636 aer_op->cmd = aer_cmd ;
637 /*useful for error_detected callback*/
638 aer_op->err = state;
639 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400640 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400641 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
642 if (!ret) {
643 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400644 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400645 return PCI_ERS_RESULT_NONE;
646 }
647 wmb();
648
649 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400650 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400651 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400652 /*local flag to mark there's aer request, xen_pcibk callback will use
653 * this flag to judge whether we need to check pci-front give aer
654 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400655 */
656 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
657
658 /*It is possible that a pcifront conf_read_write ops request invokes
659 * the callback which cause the spurious execution of wake_up.
660 * Yet it is harmless and better than a spinlock here
661 */
662 set_bit(_XEN_PCIB_active,
663 (unsigned long *)&psdev->pdev->sh_info->flags);
664 wmb();
665 notify_remote_via_irq(psdev->pdev->evtchn_irq);
666
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400667 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
668 !(test_bit(_XEN_PCIB_active, (unsigned long *)
669 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400670
671 if (!ret) {
672 if (test_bit(_XEN_PCIB_active,
673 (unsigned long *)&psdev->pdev->sh_info->flags)) {
674 dev_err(&psdev->dev->dev,
675 "pcifront aer process not responding!\n");
676 clear_bit(_XEN_PCIB_active,
677 (unsigned long *)&psdev->pdev->sh_info->flags);
678 aer_op->err = PCI_ERS_RESULT_NONE;
679 return res;
680 }
681 }
682 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
683
684 if (test_bit(_XEN_PCIF_active,
685 (unsigned long *)&psdev->pdev->sh_info->flags)) {
686 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400687 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400688 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400689 }
690
691 res = (pci_ers_result_t)aer_op->err;
692 return res;
693}
694
695/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400696* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400697* of the device driver could provide this service, and then wait for pcifront
698* ack.
699* @dev: pointer to PCI devices
700* return value is used by aer_core do_recovery policy
701*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400702static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400703{
704 struct pcistub_device *psdev;
705 pci_ers_result_t result;
706
707 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400708 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400709 dev->bus->number, dev->devfn);
710
711 down_write(&pcistub_sem);
712 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
713 dev->bus->number,
714 PCI_SLOT(dev->devfn),
715 PCI_FUNC(dev->devfn));
716
717 if (!psdev || !psdev->pdev) {
718 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400719 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400720 goto end;
721 }
722
723 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400724 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400725 " by HVM, kill it\n");
726 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100727 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400728 }
729
730 if (!test_bit(_XEN_PCIB_AERHANDLER,
731 (unsigned long *)&psdev->pdev->sh_info->flags)) {
732 dev_err(&dev->dev,
733 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100734 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400735 }
736 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
737
738 if (result == PCI_ERS_RESULT_NONE ||
739 result == PCI_ERS_RESULT_DISCONNECT) {
740 dev_dbg(&dev->dev,
741 "No AER slot_reset service or disconnected!\n");
742 kill_domain_by_device(psdev);
743 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400744end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100745 if (psdev)
746 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400747 up_write(&pcistub_sem);
748 return result;
749
750}
751
752
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400753/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400754* in case of the device driver could provide this service, and then wait
755* for pcifront ack
756* @dev: pointer to PCI devices
757* return value is used by aer_core do_recovery policy
758*/
759
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400760static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400761{
762 struct pcistub_device *psdev;
763 pci_ers_result_t result;
764
765 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400766 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400767 dev->bus->number, dev->devfn);
768
769 down_write(&pcistub_sem);
770 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
771 dev->bus->number,
772 PCI_SLOT(dev->devfn),
773 PCI_FUNC(dev->devfn));
774
775 if (!psdev || !psdev->pdev) {
776 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400777 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400778 goto end;
779 }
780
781 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400782 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400783 " by HVM, kill it\n");
784 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100785 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400786 }
787
788 if (!test_bit(_XEN_PCIB_AERHANDLER,
789 (unsigned long *)&psdev->pdev->sh_info->flags)) {
790 dev_err(&dev->dev,
791 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100792 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400793 }
794 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
795
796 if (result == PCI_ERS_RESULT_NONE ||
797 result == PCI_ERS_RESULT_DISCONNECT) {
798 dev_dbg(&dev->dev,
799 "No AER mmio_enabled service or disconnected!\n");
800 kill_domain_by_device(psdev);
801 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400802end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100803 if (psdev)
804 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400805 up_write(&pcistub_sem);
806 return result;
807}
808
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400809/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400810* in case of the device driver could provide this service, and then wait
811* for pcifront ack.
812* @dev: pointer to PCI devices
813* @error: the current PCI connection state
814* return value is used by aer_core do_recovery policy
815*/
816
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400817static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400818 pci_channel_state_t error)
819{
820 struct pcistub_device *psdev;
821 pci_ers_result_t result;
822
823 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400824 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400825 dev->bus->number, dev->devfn);
826
827 down_write(&pcistub_sem);
828 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
829 dev->bus->number,
830 PCI_SLOT(dev->devfn),
831 PCI_FUNC(dev->devfn));
832
833 if (!psdev || !psdev->pdev) {
834 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400835 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400836 goto end;
837 }
838
839 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400840 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400841 " by HVM, kill it\n");
842 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100843 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400844 }
845
846 /*Guest owns the device yet no aer handler regiested, kill guest*/
847 if (!test_bit(_XEN_PCIB_AERHANDLER,
848 (unsigned long *)&psdev->pdev->sh_info->flags)) {
849 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
850 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100851 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400852 }
853 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
854
855 if (result == PCI_ERS_RESULT_NONE ||
856 result == PCI_ERS_RESULT_DISCONNECT) {
857 dev_dbg(&dev->dev,
858 "No AER error_detected service or disconnected!\n");
859 kill_domain_by_device(psdev);
860 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400861end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100862 if (psdev)
863 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400864 up_write(&pcistub_sem);
865 return result;
866}
867
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400868/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400869* in case of the device driver could provide this service, and then wait
870* for pcifront ack.
871* @dev: pointer to PCI devices
872*/
873
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400874static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400875{
876 struct pcistub_device *psdev;
877
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400878 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400879 dev->bus->number, dev->devfn);
880
881 down_write(&pcistub_sem);
882 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
883 dev->bus->number,
884 PCI_SLOT(dev->devfn),
885 PCI_FUNC(dev->devfn));
886
887 if (!psdev || !psdev->pdev) {
888 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400889 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400890 goto end;
891 }
892
893 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400894 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400895 " by HVM, kill it\n");
896 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100897 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400898 }
899
900 if (!test_bit(_XEN_PCIB_AERHANDLER,
901 (unsigned long *)&psdev->pdev->sh_info->flags)) {
902 dev_err(&dev->dev,
903 "guest with no AER driver should have been killed\n");
904 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100905 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400906 }
907 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
908 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400909end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100910 if (psdev)
911 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400912 up_write(&pcistub_sem);
913 return;
914}
915
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400916/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700917static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400918 .error_detected = xen_pcibk_error_detected,
919 .mmio_enabled = xen_pcibk_mmio_enabled,
920 .slot_reset = xen_pcibk_slot_reset,
921 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400922};
923
924/*
925 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
926 * for a normal device. I don't want it to be loaded automatically.
927 */
928
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400929static struct pci_driver xen_pcibk_pci_driver = {
930 /* The name should be xen_pciback, but until the tools are updated
931 * we will keep it as pciback. */
932 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400933 .id_table = pcistub_ids,
934 .probe = pcistub_probe,
935 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400936 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400937};
938
939static inline int str_to_slot(const char *buf, int *domain, int *bus,
940 int *slot, int *func)
941{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000942 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400943
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000944 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
945 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100946 case 3:
947 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000948 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100949 break;
950 case 2:
951 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000952 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100953 break;
954 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000955 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400956 return 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400957
958 /* try again without domain */
959 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000960 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100961 case 2:
962 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000963 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100964 break;
965 case 1:
966 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000967 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100968 break;
969 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000970 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400971 return 0;
972
973 return -EINVAL;
974}
975
976static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
977 *slot, int *func, int *reg, int *size, int *mask)
978{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000979 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400980
Jan Beulichb3e40b72012-11-02 14:37:13 +0000981 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000982 reg, size, mask, &parsed);
983 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400984 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000985
986 /* try again without domain */
987 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000988 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000989 mask, &parsed);
990 if (parsed && !buf[parsed])
991 return 0;
992
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400993 return -EINVAL;
994}
995
996static int pcistub_device_id_add(int domain, int bus, int slot, int func)
997{
998 struct pcistub_device_id *pci_dev_id;
999 unsigned long flags;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001000 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001001
1002 if (slot < 0) {
1003 for (slot = 0; !rc && slot < 32; ++slot)
1004 rc = pcistub_device_id_add(domain, bus, slot, func);
1005 return rc;
1006 }
1007
1008 if (func < 0) {
1009 for (func = 0; !rc && func < 8; ++func)
1010 rc = pcistub_device_id_add(domain, bus, slot, func);
1011 return rc;
1012 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001013
Jan Beulichb3e40b72012-11-02 14:37:13 +00001014 if ((
1015#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1016 || !defined(CONFIG_PCI_DOMAINS)
1017 !pci_domains_supported ? domain :
1018#endif
1019 domain < 0 || domain > 0xffff)
1020 || bus < 0 || bus > 0xff
1021 || PCI_SLOT(devfn) != slot
1022 || PCI_FUNC(devfn) != func)
1023 return -EINVAL;
1024
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001025 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1026 if (!pci_dev_id)
1027 return -ENOMEM;
1028
1029 pci_dev_id->domain = domain;
1030 pci_dev_id->bus = bus;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001031 pci_dev_id->devfn = devfn;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001032
Joe Perches283c0972013-06-28 03:21:41 -07001033 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001034 domain, bus, slot, func);
1035
1036 spin_lock_irqsave(&device_ids_lock, flags);
1037 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1038 spin_unlock_irqrestore(&device_ids_lock, flags);
1039
1040 return 0;
1041}
1042
1043static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1044{
1045 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001046 int err = -ENOENT;
1047 unsigned long flags;
1048
1049 spin_lock_irqsave(&device_ids_lock, flags);
1050 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1051 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001052 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1053 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1054 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001055 /* Don't break; here because it's possible the same
1056 * slot could be in the list more than once
1057 */
1058 list_del(&pci_dev_id->slot_list);
1059 kfree(pci_dev_id);
1060
1061 err = 0;
1062
Joe Perches283c0972013-06-28 03:21:41 -07001063 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1064 domain, bus, slot, func);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001065 }
1066 }
1067 spin_unlock_irqrestore(&device_ids_lock, flags);
1068
1069 return err;
1070}
1071
Jan Beulichb3e40b72012-11-02 14:37:13 +00001072static int pcistub_reg_add(int domain, int bus, int slot, int func,
1073 unsigned int reg, unsigned int size,
1074 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001075{
1076 int err = 0;
1077 struct pcistub_device *psdev;
1078 struct pci_dev *dev;
1079 struct config_field *field;
1080
Jan Beulichb3e40b72012-11-02 14:37:13 +00001081 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1082 return -EINVAL;
1083
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001084 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001085 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001086 err = -ENODEV;
1087 goto out;
1088 }
1089 dev = psdev->dev;
1090
1091 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1092 if (!field) {
1093 err = -ENOMEM;
1094 goto out;
1095 }
1096
1097 field->offset = reg;
1098 field->size = size;
1099 field->mask = mask;
1100 field->init = NULL;
1101 field->reset = NULL;
1102 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001103 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001104
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001105 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001106 if (err)
1107 kfree(field);
1108out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001109 if (psdev)
1110 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001111 return err;
1112}
1113
1114static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1115 size_t count)
1116{
1117 int domain, bus, slot, func;
1118 int err;
1119
1120 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1121 if (err)
1122 goto out;
1123
1124 err = pcistub_device_id_add(domain, bus, slot, func);
1125
1126out:
1127 if (!err)
1128 err = count;
1129 return err;
1130}
Jan Beulich402c5e12011-09-21 16:22:11 -04001131static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001132
1133static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1134 size_t count)
1135{
1136 int domain, bus, slot, func;
1137 int err;
1138
1139 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1140 if (err)
1141 goto out;
1142
1143 err = pcistub_device_id_remove(domain, bus, slot, func);
1144
1145out:
1146 if (!err)
1147 err = count;
1148 return err;
1149}
Jan Beulich402c5e12011-09-21 16:22:11 -04001150static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001151
1152static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1153{
1154 struct pcistub_device_id *pci_dev_id;
1155 size_t count = 0;
1156 unsigned long flags;
1157
1158 spin_lock_irqsave(&device_ids_lock, flags);
1159 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1160 if (count >= PAGE_SIZE)
1161 break;
1162
1163 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001164 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001165 pci_dev_id->domain, pci_dev_id->bus,
1166 PCI_SLOT(pci_dev_id->devfn),
1167 PCI_FUNC(pci_dev_id->devfn));
1168 }
1169 spin_unlock_irqrestore(&device_ids_lock, flags);
1170
1171 return count;
1172}
Jan Beulich402c5e12011-09-21 16:22:11 -04001173static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001174
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001175static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1176{
1177 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001178 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001179 size_t count = 0;
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&pcistub_devices_lock, flags);
1183 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1184 if (count >= PAGE_SIZE)
1185 break;
1186 if (!psdev->dev)
1187 continue;
1188 dev_data = pci_get_drvdata(psdev->dev);
1189 if (!dev_data)
1190 continue;
1191 count +=
1192 scnprintf(buf + count, PAGE_SIZE - count,
1193 "%s:%s:%sing:%ld\n",
1194 pci_name(psdev->dev),
1195 dev_data->isr_on ? "on" : "off",
1196 dev_data->ack_intr ? "ack" : "not ack",
1197 dev_data->handled);
1198 }
1199 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1200 return count;
1201}
Jan Beulich402c5e12011-09-21 16:22:11 -04001202static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001203
1204static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1205 const char *buf,
1206 size_t count)
1207{
1208 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001209 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001210 int domain, bus, slot, func;
Wei Yongjun405010d2013-05-31 19:59:20 +08001211 int err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001212
1213 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1214 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001215 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001216
1217 psdev = pcistub_device_find(domain, bus, slot, func);
Wei Yongjun405010d2013-05-31 19:59:20 +08001218 if (!psdev) {
1219 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001220 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001221 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001222
1223 dev_data = pci_get_drvdata(psdev->dev);
Wei Yongjun405010d2013-05-31 19:59:20 +08001224 if (!dev_data) {
1225 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001226 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001227 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001228
1229 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1230 dev_data->irq_name, dev_data->isr_on,
1231 !dev_data->isr_on);
1232
1233 dev_data->isr_on = !(dev_data->isr_on);
1234 if (dev_data->isr_on)
1235 dev_data->ack_intr = 1;
1236out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001237 if (psdev)
1238 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001239 if (!err)
1240 err = count;
1241 return err;
1242}
Jan Beulich402c5e12011-09-21 16:22:11 -04001243static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1244 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001245
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001246static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1247 size_t count)
1248{
1249 int domain, bus, slot, func, reg, size, mask;
1250 int err;
1251
1252 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1253 &mask);
1254 if (err)
1255 goto out;
1256
1257 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1258
1259out:
1260 if (!err)
1261 err = count;
1262 return err;
1263}
1264
1265static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1266{
1267 int count = 0;
1268 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001269 struct xen_pcibk_config_quirk *quirk;
1270 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001271 const struct config_field *field;
1272 const struct config_field_entry *cfg_entry;
1273
1274 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001275 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001276 if (count >= PAGE_SIZE)
1277 goto out;
1278
1279 count += scnprintf(buf + count, PAGE_SIZE - count,
1280 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1281 quirk->pdev->bus->number,
1282 PCI_SLOT(quirk->pdev->devfn),
1283 PCI_FUNC(quirk->pdev->devfn),
1284 quirk->devid.vendor, quirk->devid.device,
1285 quirk->devid.subvendor,
1286 quirk->devid.subdevice);
1287
1288 dev_data = pci_get_drvdata(quirk->pdev);
1289
1290 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1291 field = cfg_entry->field;
1292 if (count >= PAGE_SIZE)
1293 goto out;
1294
1295 count += scnprintf(buf + count, PAGE_SIZE - count,
1296 "\t\t%08x:%01x:%08x\n",
1297 cfg_entry->base_offset +
1298 field->offset, field->size,
1299 field->mask);
1300 }
1301 }
1302
1303out:
1304 spin_unlock_irqrestore(&device_ids_lock, flags);
1305
1306 return count;
1307}
Jan Beulich402c5e12011-09-21 16:22:11 -04001308static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1309 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001310
1311static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1312 size_t count)
1313{
1314 int domain, bus, slot, func;
1315 int err;
1316 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001317 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001318
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001319 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1320 if (err)
1321 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001322
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001323 psdev = pcistub_device_find(domain, bus, slot, func);
1324 if (!psdev) {
1325 err = -ENODEV;
1326 goto out;
1327 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001328
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001329 dev_data = pci_get_drvdata(psdev->dev);
1330 /* the driver data for a device should never be null at this point */
1331 if (!dev_data) {
1332 err = -ENXIO;
1333 goto release;
1334 }
1335 if (!dev_data->permissive) {
1336 dev_data->permissive = 1;
1337 /* Let user know that what they're doing could be unsafe */
1338 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1339 "configuration space accesses!\n");
1340 dev_warn(&psdev->dev->dev,
1341 "permissive mode is potentially unsafe!\n");
1342 }
1343release:
1344 pcistub_device_put(psdev);
1345out:
1346 if (!err)
1347 err = count;
1348 return err;
1349}
1350
1351static ssize_t permissive_show(struct device_driver *drv, char *buf)
1352{
1353 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001354 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001355 size_t count = 0;
1356 unsigned long flags;
1357 spin_lock_irqsave(&pcistub_devices_lock, flags);
1358 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1359 if (count >= PAGE_SIZE)
1360 break;
1361 if (!psdev->dev)
1362 continue;
1363 dev_data = pci_get_drvdata(psdev->dev);
1364 if (!dev_data || !dev_data->permissive)
1365 continue;
1366 count +=
1367 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1368 pci_name(psdev->dev));
1369 }
1370 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1371 return count;
1372}
Jan Beulich402c5e12011-09-21 16:22:11 -04001373static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1374 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001375
1376static void pcistub_exit(void)
1377{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001378 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1379 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001380 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001381 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1382 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1383 driver_remove_file(&xen_pcibk_pci_driver.driver,
1384 &driver_attr_permissive);
1385 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001386 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001387 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001388 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001389 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001390}
1391
1392static int __init pcistub_init(void)
1393{
1394 int pos = 0;
1395 int err = 0;
1396 int domain, bus, slot, func;
1397 int parsed;
1398
1399 if (pci_devs_to_hide && *pci_devs_to_hide) {
1400 do {
1401 parsed = 0;
1402
1403 err = sscanf(pci_devs_to_hide + pos,
1404 " (%x:%x:%x.%x) %n",
1405 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001406 switch (err) {
1407 case 3:
1408 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001409 sscanf(pci_devs_to_hide + pos,
1410 " (%x:%x:%x.*) %n",
1411 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001412 break;
1413 case 2:
1414 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001415 sscanf(pci_devs_to_hide + pos,
1416 " (%x:%x:*.*) %n",
1417 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001418 break;
1419 }
1420
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001421 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001422 domain = 0;
1423 err = sscanf(pci_devs_to_hide + pos,
1424 " (%x:%x.%x) %n",
1425 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001426 switch (err) {
1427 case 2:
1428 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001429 sscanf(pci_devs_to_hide + pos,
1430 " (%x:%x.*) %n",
1431 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001432 break;
1433 case 1:
1434 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001435 sscanf(pci_devs_to_hide + pos,
1436 " (%x:*.*) %n",
1437 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001438 break;
1439 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001440 }
1441
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001442 if (parsed <= 0)
1443 goto parse_error;
1444
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001445 err = pcistub_device_id_add(domain, bus, slot, func);
1446 if (err)
1447 goto out;
1448
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001449 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001450 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001451 }
1452
1453 /* If we're the first PCI Device Driver to register, we're the
1454 * first one to get offered PCI devices as they become
1455 * available (and thus we can be the first to grab them)
1456 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001457 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001458 if (err < 0)
1459 goto out;
1460
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_new_slot);
1463 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001464 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001465 &driver_attr_remove_slot);
1466 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001467 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001468 &driver_attr_slots);
1469 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001470 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001471 &driver_attr_quirks);
1472 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001473 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001474 &driver_attr_permissive);
1475
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001476 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001477 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001478 &driver_attr_irq_handlers);
1479 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001480 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001481 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001482 if (err)
1483 pcistub_exit();
1484
1485out:
1486 return err;
1487
1488parse_error:
Joe Perches283c0972013-06-28 03:21:41 -07001489 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001490 pci_devs_to_hide + pos);
1491 return -EINVAL;
1492}
1493
1494#ifndef MODULE
1495/*
1496 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001497 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001498 * want to suck up any device before other drivers
1499 * get a chance by being the first pci device
1500 * driver to register)
1501 */
1502fs_initcall(pcistub_init);
1503#endif
1504
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001505static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001506{
1507 int err;
1508
1509 if (!xen_initial_domain())
1510 return -ENODEV;
1511
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001512 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001513 if (err)
1514 return err;
1515
1516#ifdef MODULE
1517 err = pcistub_init();
1518 if (err < 0)
1519 return err;
1520#endif
1521
1522 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001523 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001524 if (err)
1525 pcistub_exit();
1526
1527 return err;
1528}
1529
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001530static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001531{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001532 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001533 pcistub_exit();
1534}
1535
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001536module_init(xen_pcibk_init);
1537module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001538
1539MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001540MODULE_ALIAS("xen-backend:pci");