blob: e5ff09d8a2422b8135da9763e9e12e9287f53239 [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.
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500253 *
254 * To make this easier, the caller has to hold the device lock.
Konrad Rzeszutek Wilk0a9fd012014-04-22 10:48:17 -0400255 */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400256void pcistub_put_pci_dev(struct pci_dev *dev)
257{
258 struct pcistub_device *psdev, *found_psdev = NULL;
259 unsigned long flags;
260
261 spin_lock_irqsave(&pcistub_devices_lock, flags);
262
263 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
264 if (psdev->dev == dev) {
265 found_psdev = psdev;
266 break;
267 }
268 }
269
270 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400271 if (WARN_ON(!found_psdev))
272 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400273
274 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400275 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400276 */
277 down_write(&pcistub_sem);
278 /* Cleanup our device
279 * (so it's ready for the next domain)
280 */
Konrad Rzeszutek Wilkac801022014-12-03 16:40:27 -0500281 device_lock_assert(&dev->dev);
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500282 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500283 pci_restore_state(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500284
285 /* This disables the device. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500286 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500287
288 /* And cleanup up our emulated fields. */
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500289 xen_pcibk_config_reset_dev(dev);
Konrad Rzeszutek Wilkfcb8ce92013-12-03 21:37:24 -0500290 xen_pcibk_config_free_dyn_fields(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400291
Konrad Rzeszutek Wilk88990352013-12-03 21:34:03 -0500292 xen_unregister_device_domain_owner(dev);
Konrad Rzeszutek Wilk31673552012-01-04 15:11:02 -0500293
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400294 spin_lock_irqsave(&found_psdev->lock, flags);
295 found_psdev->pdev = NULL;
296 spin_unlock_irqrestore(&found_psdev->lock, flags);
297
298 pcistub_device_put(found_psdev);
299 up_write(&pcistub_sem);
300}
301
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800302static int pcistub_match_one(struct pci_dev *dev,
303 struct pcistub_device_id *pdev_id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400304{
305 /* Match the specified device by domain, bus, slot, func and also if
306 * any of the device's parent bridges match.
307 */
308 for (; dev != NULL; dev = dev->bus->self) {
309 if (pci_domain_nr(dev->bus) == pdev_id->domain
310 && dev->bus->number == pdev_id->bus
311 && dev->devfn == pdev_id->devfn)
312 return 1;
313
314 /* Sometimes topmost bridge links to itself. */
315 if (dev == dev->bus->self)
316 break;
317 }
318
319 return 0;
320}
321
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800322static int pcistub_match(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400323{
324 struct pcistub_device_id *pdev_id;
325 unsigned long flags;
326 int found = 0;
327
328 spin_lock_irqsave(&device_ids_lock, flags);
329 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
330 if (pcistub_match_one(dev, pdev_id)) {
331 found = 1;
332 break;
333 }
334 }
335 spin_unlock_irqrestore(&device_ids_lock, flags);
336
337 return found;
338}
339
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800340static int pcistub_init_device(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400341{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400342 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400343 int err = 0;
344
345 dev_dbg(&dev->dev, "initializing...\n");
346
347 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400348 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400349 * would need to be called somewhere to free the memory allocated
350 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
351 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400352 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
353 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400354 if (!dev_data) {
355 err = -ENOMEM;
356 goto out;
357 }
358 pci_set_drvdata(dev, dev_data);
359
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400360 /*
361 * Setup name for fake IRQ handler. It will only be enabled
362 * once the device is turned on by the guest.
363 */
364 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
365
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400366 dev_dbg(&dev->dev, "initializing config\n");
367
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400368 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
369 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400370 if (err)
371 goto out;
372
373 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
374 * must do this here because pcibios_enable_device may specify
375 * the pci device's true irq (and possibly its other resources)
376 * if they differ from what's in the configuration space.
377 * This makes the assumption that the device's resources won't
378 * change after this point (otherwise this code may break!)
379 */
380 dev_dbg(&dev->dev, "enabling device\n");
381 err = pci_enable_device(dev);
382 if (err)
383 goto config_release;
384
Jan Beulichd69c0e32013-05-29 13:31:15 +0100385 if (dev->msix_cap) {
Jan Beulich909b3fd2013-03-12 15:06:23 +0000386 struct physdev_pci_device ppdev = {
387 .seg = pci_domain_nr(dev->bus),
388 .bus = dev->bus->number,
389 .devfn = dev->devfn
390 };
391
392 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
393 if (err)
394 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
395 err);
396 }
397
Konrad Rzeszutek Wilkcd9db802012-01-04 14:30:58 -0500398 /* We need the device active to save the state. */
399 dev_dbg(&dev->dev, "save state of device\n");
400 pci_save_state(dev);
401 dev_data->pci_saved_state = pci_store_saved_state(dev);
402 if (!dev_data->pci_saved_state)
403 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400404 else {
Masanari Iida744627e92012-11-05 23:30:40 +0900405 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400406 __pci_reset_function_locked(dev);
Konrad Rzeszutek Wilkc341ca42012-09-25 16:48:24 -0400407 pci_restore_state(dev);
Konrad Rzeszutek Wilk80ba77d2012-09-05 16:35:20 -0400408 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400409 /* Now disable the device (this also ensures some private device
410 * data is setup before we export)
411 */
412 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400413 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400414
Ethan Zhaobe507fd2014-09-09 10:21:27 +0800415 pci_set_dev_assigned(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400416 return 0;
417
418config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400419 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400420
421out:
422 pci_set_drvdata(dev, NULL);
423 kfree(dev_data);
424 return err;
425}
426
427/*
428 * Because some initialization still happens on
429 * devices during fs_initcall, we need to defer
430 * full initialization of our devices until
431 * device_initcall.
432 */
433static int __init pcistub_init_devices_late(void)
434{
435 struct pcistub_device *psdev;
436 unsigned long flags;
437 int err = 0;
438
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400439 spin_lock_irqsave(&pcistub_devices_lock, flags);
440
441 while (!list_empty(&seized_devices)) {
442 psdev = container_of(seized_devices.next,
443 struct pcistub_device, dev_list);
444 list_del(&psdev->dev_list);
445
446 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
447
448 err = pcistub_init_device(psdev->dev);
449 if (err) {
450 dev_err(&psdev->dev->dev,
451 "error %d initializing device\n", err);
452 kfree(psdev);
453 psdev = NULL;
454 }
455
456 spin_lock_irqsave(&pcistub_devices_lock, flags);
457
458 if (psdev)
459 list_add_tail(&psdev->dev_list, &pcistub_devices);
460 }
461
462 initialize_devices = 1;
463
464 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
465
466 return 0;
467}
468
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800469static int pcistub_seize(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400470{
471 struct pcistub_device *psdev;
472 unsigned long flags;
473 int err = 0;
474
475 psdev = pcistub_device_alloc(dev);
476 if (!psdev)
477 return -ENOMEM;
478
479 spin_lock_irqsave(&pcistub_devices_lock, flags);
480
481 if (initialize_devices) {
482 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
483
484 /* don't want irqs disabled when calling pcistub_init_device */
485 err = pcistub_init_device(psdev->dev);
486
487 spin_lock_irqsave(&pcistub_devices_lock, flags);
488
489 if (!err)
490 list_add(&psdev->dev_list, &pcistub_devices);
491 } else {
492 dev_dbg(&dev->dev, "deferring initialization\n");
493 list_add(&psdev->dev_list, &seized_devices);
494 }
495
496 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
497
498 if (err)
499 pcistub_device_put(psdev);
500
501 return err;
502}
503
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400504/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
505 * other functions that take the sysfs lock. */
Greg Kroah-Hartman345a5252012-12-21 13:00:00 -0800506static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400507{
508 int err = 0;
509
510 dev_dbg(&dev->dev, "probing...\n");
511
512 if (pcistub_match(dev)) {
513
514 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
515 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
516 dev_err(&dev->dev, "can't export pci devices that "
517 "don't have a normal (0) or bridge (1) "
518 "header type!\n");
519 err = -ENODEV;
520 goto out;
521 }
522
523 dev_info(&dev->dev, "seizing device\n");
524 err = pcistub_seize(dev);
525 } else
526 /* Didn't find the device */
527 err = -ENODEV;
528
529out:
530 return err;
531}
532
Konrad Rzeszutek Wilk24d8bf12014-04-21 15:43:08 -0400533/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
534 * other functions that take the sysfs lock. */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400535static void pcistub_remove(struct pci_dev *dev)
536{
537 struct pcistub_device *psdev, *found_psdev = NULL;
538 unsigned long flags;
539
540 dev_dbg(&dev->dev, "removing\n");
541
542 spin_lock_irqsave(&pcistub_devices_lock, flags);
543
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400544 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400545
546 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
547 if (psdev->dev == dev) {
548 found_psdev = psdev;
549 break;
550 }
551 }
552
553 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
554
555 if (found_psdev) {
Konrad Rzeszutek Wilkb5d51212014-12-03 16:40:28 -0500556 dev_dbg(&dev->dev, "found device to remove %s\n",
557 found_psdev->pdev ? "- in-use" : "");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400558
559 if (found_psdev->pdev) {
Konrad Rzeszutek Wilkb5d51212014-12-03 16:40:28 -0500560 int domid = xen_find_device_domain_owner(dev);
561
562 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
563 pci_name(found_psdev->dev), domid);
Joe Perches283c0972013-06-28 03:21:41 -0700564 pr_warn("****** driver domain may still access this device's i/o resources!\n");
565 pr_warn("****** shutdown driver domain before binding device\n");
566 pr_warn("****** to other drivers or domains\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400567
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -0500568 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
569 * doing the FLR. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400570 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500571 found_psdev->dev,
572 false /* caller holds the lock. */);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400573 }
574
575 spin_lock_irqsave(&pcistub_devices_lock, flags);
576 list_del(&found_psdev->dev_list);
577 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
578
579 /* the final put for releasing from the list */
580 pcistub_device_put(found_psdev);
581 }
582}
583
Benoit Taine9baa3c32014-08-08 15:56:03 +0200584static const struct pci_device_id pcistub_ids[] = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400585 {
586 .vendor = PCI_ANY_ID,
587 .device = PCI_ANY_ID,
588 .subvendor = PCI_ANY_ID,
589 .subdevice = PCI_ANY_ID,
590 },
591 {0,},
592};
593
594#define PCI_NODENAME_MAX 40
595static void kill_domain_by_device(struct pcistub_device *psdev)
596{
597 struct xenbus_transaction xbt;
598 int err;
599 char nodename[PCI_NODENAME_MAX];
600
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400601 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400602 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
603 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400604
605again:
606 err = xenbus_transaction_start(&xbt);
607 if (err) {
608 dev_err(&psdev->dev->dev,
609 "error %d when start xenbus transaction\n", err);
610 return;
611 }
612 /*PV AER handlers will set this flag*/
613 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
614 err = xenbus_transaction_end(xbt, 0);
615 if (err) {
616 if (err == -EAGAIN)
617 goto again;
618 dev_err(&psdev->dev->dev,
619 "error %d when end xenbus transaction\n", err);
620 return;
621 }
622}
623
624/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400625 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400626 * jobs: send service request and waiting for front_end response.
627*/
628static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400629 pci_channel_state_t state, int aer_cmd,
630 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400631{
632 pci_ers_result_t res = result;
633 struct xen_pcie_aer_op *aer_op;
634 int ret;
635
636 /*with PV AER drivers*/
637 aer_op = &(psdev->pdev->sh_info->aer_op);
638 aer_op->cmd = aer_cmd ;
639 /*useful for error_detected callback*/
640 aer_op->err = state;
641 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400642 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400643 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
644 if (!ret) {
645 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400646 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400647 return PCI_ERS_RESULT_NONE;
648 }
649 wmb();
650
651 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400652 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400653 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400654 /*local flag to mark there's aer request, xen_pcibk callback will use
655 * this flag to judge whether we need to check pci-front give aer
656 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400657 */
658 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
659
660 /*It is possible that a pcifront conf_read_write ops request invokes
661 * the callback which cause the spurious execution of wake_up.
662 * Yet it is harmless and better than a spinlock here
663 */
664 set_bit(_XEN_PCIB_active,
665 (unsigned long *)&psdev->pdev->sh_info->flags);
666 wmb();
667 notify_remote_via_irq(psdev->pdev->evtchn_irq);
668
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400669 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
670 !(test_bit(_XEN_PCIB_active, (unsigned long *)
671 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400672
673 if (!ret) {
674 if (test_bit(_XEN_PCIB_active,
675 (unsigned long *)&psdev->pdev->sh_info->flags)) {
676 dev_err(&psdev->dev->dev,
677 "pcifront aer process not responding!\n");
678 clear_bit(_XEN_PCIB_active,
679 (unsigned long *)&psdev->pdev->sh_info->flags);
680 aer_op->err = PCI_ERS_RESULT_NONE;
681 return res;
682 }
683 }
684 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
685
686 if (test_bit(_XEN_PCIF_active,
687 (unsigned long *)&psdev->pdev->sh_info->flags)) {
688 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400689 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400690 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400691 }
692
693 res = (pci_ers_result_t)aer_op->err;
694 return res;
695}
696
697/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400698* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400699* of the device driver could provide this service, and then wait for pcifront
700* ack.
701* @dev: pointer to PCI devices
702* return value is used by aer_core do_recovery policy
703*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400704static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400705{
706 struct pcistub_device *psdev;
707 pci_ers_result_t result;
708
709 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400710 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400711 dev->bus->number, dev->devfn);
712
713 down_write(&pcistub_sem);
714 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
715 dev->bus->number,
716 PCI_SLOT(dev->devfn),
717 PCI_FUNC(dev->devfn));
718
719 if (!psdev || !psdev->pdev) {
720 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400721 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400722 goto end;
723 }
724
725 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400726 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400727 " by HVM, kill it\n");
728 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100729 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400730 }
731
732 if (!test_bit(_XEN_PCIB_AERHANDLER,
733 (unsigned long *)&psdev->pdev->sh_info->flags)) {
734 dev_err(&dev->dev,
735 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100736 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400737 }
738 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
739
740 if (result == PCI_ERS_RESULT_NONE ||
741 result == PCI_ERS_RESULT_DISCONNECT) {
742 dev_dbg(&dev->dev,
743 "No AER slot_reset service or disconnected!\n");
744 kill_domain_by_device(psdev);
745 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400746end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100747 if (psdev)
748 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400749 up_write(&pcistub_sem);
750 return result;
751
752}
753
754
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400755/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400756* in case of the device driver could provide this service, and then wait
757* for pcifront ack
758* @dev: pointer to PCI devices
759* return value is used by aer_core do_recovery policy
760*/
761
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400762static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400763{
764 struct pcistub_device *psdev;
765 pci_ers_result_t result;
766
767 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400768 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400769 dev->bus->number, dev->devfn);
770
771 down_write(&pcistub_sem);
772 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
773 dev->bus->number,
774 PCI_SLOT(dev->devfn),
775 PCI_FUNC(dev->devfn));
776
777 if (!psdev || !psdev->pdev) {
778 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400779 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400780 goto end;
781 }
782
783 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400784 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400785 " by HVM, kill it\n");
786 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100787 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400788 }
789
790 if (!test_bit(_XEN_PCIB_AERHANDLER,
791 (unsigned long *)&psdev->pdev->sh_info->flags)) {
792 dev_err(&dev->dev,
793 "guest with no AER driver should have been killed\n");
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100794 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400795 }
796 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
797
798 if (result == PCI_ERS_RESULT_NONE ||
799 result == PCI_ERS_RESULT_DISCONNECT) {
800 dev_dbg(&dev->dev,
801 "No AER mmio_enabled service or disconnected!\n");
802 kill_domain_by_device(psdev);
803 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400804end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100805 if (psdev)
806 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400807 up_write(&pcistub_sem);
808 return result;
809}
810
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400811/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400812* in case of the device driver could provide this service, and then wait
813* for pcifront ack.
814* @dev: pointer to PCI devices
815* @error: the current PCI connection state
816* return value is used by aer_core do_recovery policy
817*/
818
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400819static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400820 pci_channel_state_t error)
821{
822 struct pcistub_device *psdev;
823 pci_ers_result_t result;
824
825 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400826 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400827 dev->bus->number, dev->devfn);
828
829 down_write(&pcistub_sem);
830 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
831 dev->bus->number,
832 PCI_SLOT(dev->devfn),
833 PCI_FUNC(dev->devfn));
834
835 if (!psdev || !psdev->pdev) {
836 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400837 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400838 goto end;
839 }
840
841 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400842 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400843 " by HVM, kill it\n");
844 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100845 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400846 }
847
848 /*Guest owns the device yet no aer handler regiested, kill guest*/
849 if (!test_bit(_XEN_PCIB_AERHANDLER,
850 (unsigned long *)&psdev->pdev->sh_info->flags)) {
851 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
852 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100853 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400854 }
855 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
856
857 if (result == PCI_ERS_RESULT_NONE ||
858 result == PCI_ERS_RESULT_DISCONNECT) {
859 dev_dbg(&dev->dev,
860 "No AER error_detected service or disconnected!\n");
861 kill_domain_by_device(psdev);
862 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400863end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100864 if (psdev)
865 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400866 up_write(&pcistub_sem);
867 return result;
868}
869
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400870/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400871* in case of the device driver could provide this service, and then wait
872* for pcifront ack.
873* @dev: pointer to PCI devices
874*/
875
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400876static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400877{
878 struct pcistub_device *psdev;
879
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400880 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400881 dev->bus->number, dev->devfn);
882
883 down_write(&pcistub_sem);
884 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
885 dev->bus->number,
886 PCI_SLOT(dev->devfn),
887 PCI_FUNC(dev->devfn));
888
889 if (!psdev || !psdev->pdev) {
890 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400891 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400892 goto end;
893 }
894
895 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400896 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400897 " by HVM, kill it\n");
898 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100899 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400900 }
901
902 if (!test_bit(_XEN_PCIB_AERHANDLER,
903 (unsigned long *)&psdev->pdev->sh_info->flags)) {
904 dev_err(&dev->dev,
905 "guest with no AER driver should have been killed\n");
906 kill_domain_by_device(psdev);
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100907 goto end;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400908 }
909 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
910 PCI_ERS_RESULT_RECOVERED);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400911end:
Jan Beuliche6aa70a2012-09-24 15:55:37 +0100912 if (psdev)
913 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400914 up_write(&pcistub_sem);
915 return;
916}
917
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400918/*add xen_pcibk AER handling*/
Stephen Hemminger1d352032012-09-07 09:33:17 -0700919static const struct pci_error_handlers xen_pcibk_error_handler = {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400920 .error_detected = xen_pcibk_error_detected,
921 .mmio_enabled = xen_pcibk_mmio_enabled,
922 .slot_reset = xen_pcibk_slot_reset,
923 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400924};
925
926/*
927 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
928 * for a normal device. I don't want it to be loaded automatically.
929 */
930
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400931static struct pci_driver xen_pcibk_pci_driver = {
932 /* The name should be xen_pciback, but until the tools are updated
933 * we will keep it as pciback. */
934 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400935 .id_table = pcistub_ids,
936 .probe = pcistub_probe,
937 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400938 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400939};
940
941static inline int str_to_slot(const char *buf, int *domain, int *bus,
942 int *slot, int *func)
943{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000944 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400945
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000946 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
947 &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100948 case 3:
949 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000950 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100951 break;
952 case 2:
953 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000954 sscanf(buf, " %x:%x:*.* %n", domain, 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;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400959
960 /* try again without domain */
961 *domain = 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000962 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
Jan Beulichc3cb4702012-09-18 12:29:03 +0100963 case 2:
964 *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000965 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100966 break;
967 case 1:
968 *slot = *func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000969 sscanf(buf, " %x:*.* %n", bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +0100970 break;
971 }
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000972 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400973 return 0;
974
975 return -EINVAL;
976}
977
978static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
979 *slot, int *func, int *reg, int *size, int *mask)
980{
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000981 int parsed = 0;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400982
Jan Beulichb3e40b72012-11-02 14:37:13 +0000983 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000984 reg, size, mask, &parsed);
985 if (parsed && !buf[parsed])
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400986 return 0;
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000987
988 /* try again without domain */
989 *domain = 0;
Jan Beulichb3e40b72012-11-02 14:37:13 +0000990 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
Jan Beulich5b71fbd2012-11-02 14:36:38 +0000991 mask, &parsed);
992 if (parsed && !buf[parsed])
993 return 0;
994
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400995 return -EINVAL;
996}
997
998static int pcistub_device_id_add(int domain, int bus, int slot, int func)
999{
1000 struct pcistub_device_id *pci_dev_id;
1001 unsigned long flags;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001002 int rc = 0, devfn = PCI_DEVFN(slot, func);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001003
1004 if (slot < 0) {
1005 for (slot = 0; !rc && slot < 32; ++slot)
1006 rc = pcistub_device_id_add(domain, bus, slot, func);
1007 return rc;
1008 }
1009
1010 if (func < 0) {
1011 for (func = 0; !rc && func < 8; ++func)
1012 rc = pcistub_device_id_add(domain, bus, slot, func);
1013 return rc;
1014 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001015
Jan Beulichb3e40b72012-11-02 14:37:13 +00001016 if ((
1017#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1018 || !defined(CONFIG_PCI_DOMAINS)
1019 !pci_domains_supported ? domain :
1020#endif
1021 domain < 0 || domain > 0xffff)
1022 || bus < 0 || bus > 0xff
1023 || PCI_SLOT(devfn) != slot
1024 || PCI_FUNC(devfn) != func)
1025 return -EINVAL;
1026
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001027 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1028 if (!pci_dev_id)
1029 return -ENOMEM;
1030
1031 pci_dev_id->domain = domain;
1032 pci_dev_id->bus = bus;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001033 pci_dev_id->devfn = devfn;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001034
Joe Perches283c0972013-06-28 03:21:41 -07001035 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001036 domain, bus, slot, func);
1037
1038 spin_lock_irqsave(&device_ids_lock, flags);
1039 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
1040 spin_unlock_irqrestore(&device_ids_lock, flags);
1041
1042 return 0;
1043}
1044
1045static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1046{
1047 struct pcistub_device_id *pci_dev_id, *t;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001048 int err = -ENOENT;
1049 unsigned long flags;
1050
1051 spin_lock_irqsave(&device_ids_lock, flags);
1052 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1053 slot_list) {
Jan Beulichc3cb4702012-09-18 12:29:03 +01001054 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1055 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1056 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001057 /* Don't break; here because it's possible the same
1058 * slot could be in the list more than once
1059 */
1060 list_del(&pci_dev_id->slot_list);
1061 kfree(pci_dev_id);
1062
1063 err = 0;
1064
Joe Perches283c0972013-06-28 03:21:41 -07001065 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1066 domain, bus, slot, func);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001067 }
1068 }
1069 spin_unlock_irqrestore(&device_ids_lock, flags);
1070
1071 return err;
1072}
1073
Jan Beulichb3e40b72012-11-02 14:37:13 +00001074static int pcistub_reg_add(int domain, int bus, int slot, int func,
1075 unsigned int reg, unsigned int size,
1076 unsigned int mask)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001077{
1078 int err = 0;
1079 struct pcistub_device *psdev;
1080 struct pci_dev *dev;
1081 struct config_field *field;
1082
Jan Beulichb3e40b72012-11-02 14:37:13 +00001083 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1084 return -EINVAL;
1085
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001086 psdev = pcistub_device_find(domain, bus, slot, func);
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001087 if (!psdev) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001088 err = -ENODEV;
1089 goto out;
1090 }
1091 dev = psdev->dev;
1092
1093 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1094 if (!field) {
1095 err = -ENOMEM;
1096 goto out;
1097 }
1098
1099 field->offset = reg;
1100 field->size = size;
1101 field->mask = mask;
1102 field->init = NULL;
1103 field->reset = NULL;
1104 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001105 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001106
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001107 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001108 if (err)
1109 kfree(field);
1110out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001111 if (psdev)
1112 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001113 return err;
1114}
1115
1116static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
1117 size_t count)
1118{
1119 int domain, bus, slot, func;
1120 int err;
1121
1122 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1123 if (err)
1124 goto out;
1125
1126 err = pcistub_device_id_add(domain, bus, slot, func);
1127
1128out:
1129 if (!err)
1130 err = count;
1131 return err;
1132}
Jan Beulich402c5e12011-09-21 16:22:11 -04001133static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001134
1135static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1136 size_t count)
1137{
1138 int domain, bus, slot, func;
1139 int err;
1140
1141 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1142 if (err)
1143 goto out;
1144
1145 err = pcistub_device_id_remove(domain, bus, slot, func);
1146
1147out:
1148 if (!err)
1149 err = count;
1150 return err;
1151}
Jan Beulich402c5e12011-09-21 16:22:11 -04001152static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001153
1154static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1155{
1156 struct pcistub_device_id *pci_dev_id;
1157 size_t count = 0;
1158 unsigned long flags;
1159
1160 spin_lock_irqsave(&device_ids_lock, flags);
1161 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1162 if (count >= PAGE_SIZE)
1163 break;
1164
1165 count += scnprintf(buf + count, PAGE_SIZE - count,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001166 "%04x:%02x:%02x.%d\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001167 pci_dev_id->domain, pci_dev_id->bus,
1168 PCI_SLOT(pci_dev_id->devfn),
1169 PCI_FUNC(pci_dev_id->devfn));
1170 }
1171 spin_unlock_irqrestore(&device_ids_lock, flags);
1172
1173 return count;
1174}
Jan Beulich402c5e12011-09-21 16:22:11 -04001175static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001176
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001177static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1178{
1179 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001180 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001181 size_t count = 0;
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&pcistub_devices_lock, flags);
1185 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1186 if (count >= PAGE_SIZE)
1187 break;
1188 if (!psdev->dev)
1189 continue;
1190 dev_data = pci_get_drvdata(psdev->dev);
1191 if (!dev_data)
1192 continue;
1193 count +=
1194 scnprintf(buf + count, PAGE_SIZE - count,
1195 "%s:%s:%sing:%ld\n",
1196 pci_name(psdev->dev),
1197 dev_data->isr_on ? "on" : "off",
1198 dev_data->ack_intr ? "ack" : "not ack",
1199 dev_data->handled);
1200 }
1201 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1202 return count;
1203}
Jan Beulich402c5e12011-09-21 16:22:11 -04001204static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001205
1206static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1207 const char *buf,
1208 size_t count)
1209{
1210 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001211 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001212 int domain, bus, slot, func;
Wei Yongjun405010d2013-05-31 19:59:20 +08001213 int err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001214
1215 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1216 if (err)
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001217 return err;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001218
1219 psdev = pcistub_device_find(domain, bus, slot, func);
Wei Yongjun405010d2013-05-31 19:59:20 +08001220 if (!psdev) {
1221 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001222 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001223 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001224
1225 dev_data = pci_get_drvdata(psdev->dev);
Wei Yongjun405010d2013-05-31 19:59:20 +08001226 if (!dev_data) {
1227 err = -ENOENT;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001228 goto out;
Wei Yongjun405010d2013-05-31 19:59:20 +08001229 }
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001230
1231 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1232 dev_data->irq_name, dev_data->isr_on,
1233 !dev_data->isr_on);
1234
1235 dev_data->isr_on = !(dev_data->isr_on);
1236 if (dev_data->isr_on)
1237 dev_data->ack_intr = 1;
1238out:
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001239 if (psdev)
1240 pcistub_device_put(psdev);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001241 if (!err)
1242 err = count;
1243 return err;
1244}
Jan Beulich402c5e12011-09-21 16:22:11 -04001245static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1246 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001247
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001248static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1249 size_t count)
1250{
1251 int domain, bus, slot, func, reg, size, mask;
1252 int err;
1253
1254 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1255 &mask);
1256 if (err)
1257 goto out;
1258
1259 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1260
1261out:
1262 if (!err)
1263 err = count;
1264 return err;
1265}
1266
1267static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1268{
1269 int count = 0;
1270 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001271 struct xen_pcibk_config_quirk *quirk;
1272 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001273 const struct config_field *field;
1274 const struct config_field_entry *cfg_entry;
1275
1276 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001277 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001278 if (count >= PAGE_SIZE)
1279 goto out;
1280
1281 count += scnprintf(buf + count, PAGE_SIZE - count,
1282 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1283 quirk->pdev->bus->number,
1284 PCI_SLOT(quirk->pdev->devfn),
1285 PCI_FUNC(quirk->pdev->devfn),
1286 quirk->devid.vendor, quirk->devid.device,
1287 quirk->devid.subvendor,
1288 quirk->devid.subdevice);
1289
1290 dev_data = pci_get_drvdata(quirk->pdev);
1291
1292 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1293 field = cfg_entry->field;
1294 if (count >= PAGE_SIZE)
1295 goto out;
1296
1297 count += scnprintf(buf + count, PAGE_SIZE - count,
1298 "\t\t%08x:%01x:%08x\n",
1299 cfg_entry->base_offset +
1300 field->offset, field->size,
1301 field->mask);
1302 }
1303 }
1304
1305out:
1306 spin_unlock_irqrestore(&device_ids_lock, flags);
1307
1308 return count;
1309}
Jan Beulich402c5e12011-09-21 16:22:11 -04001310static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1311 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001312
1313static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1314 size_t count)
1315{
1316 int domain, bus, slot, func;
1317 int err;
1318 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001319 struct xen_pcibk_dev_data *dev_data;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001320
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001321 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1322 if (err)
1323 goto out;
Jan Beulichb3e40b72012-11-02 14:37:13 +00001324
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001325 psdev = pcistub_device_find(domain, bus, slot, func);
1326 if (!psdev) {
1327 err = -ENODEV;
1328 goto out;
1329 }
Jan Beuliche6aa70a2012-09-24 15:55:37 +01001330
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001331 dev_data = pci_get_drvdata(psdev->dev);
1332 /* the driver data for a device should never be null at this point */
1333 if (!dev_data) {
1334 err = -ENXIO;
1335 goto release;
1336 }
1337 if (!dev_data->permissive) {
1338 dev_data->permissive = 1;
1339 /* Let user know that what they're doing could be unsafe */
1340 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1341 "configuration space accesses!\n");
1342 dev_warn(&psdev->dev->dev,
1343 "permissive mode is potentially unsafe!\n");
1344 }
1345release:
1346 pcistub_device_put(psdev);
1347out:
1348 if (!err)
1349 err = count;
1350 return err;
1351}
1352
1353static ssize_t permissive_show(struct device_driver *drv, char *buf)
1354{
1355 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001356 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001357 size_t count = 0;
1358 unsigned long flags;
1359 spin_lock_irqsave(&pcistub_devices_lock, flags);
1360 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1361 if (count >= PAGE_SIZE)
1362 break;
1363 if (!psdev->dev)
1364 continue;
1365 dev_data = pci_get_drvdata(psdev->dev);
1366 if (!dev_data || !dev_data->permissive)
1367 continue;
1368 count +=
1369 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1370 pci_name(psdev->dev));
1371 }
1372 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1373 return count;
1374}
Jan Beulich402c5e12011-09-21 16:22:11 -04001375static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1376 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001377
1378static void pcistub_exit(void)
1379{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001380 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1381 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001382 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001383 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1384 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1385 driver_remove_file(&xen_pcibk_pci_driver.driver,
1386 &driver_attr_permissive);
1387 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001388 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001389 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001390 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001391 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001392}
1393
1394static int __init pcistub_init(void)
1395{
1396 int pos = 0;
1397 int err = 0;
1398 int domain, bus, slot, func;
1399 int parsed;
1400
1401 if (pci_devs_to_hide && *pci_devs_to_hide) {
1402 do {
1403 parsed = 0;
1404
1405 err = sscanf(pci_devs_to_hide + pos,
1406 " (%x:%x:%x.%x) %n",
1407 &domain, &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001408 switch (err) {
1409 case 3:
1410 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001411 sscanf(pci_devs_to_hide + pos,
1412 " (%x:%x:%x.*) %n",
1413 &domain, &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001414 break;
1415 case 2:
1416 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001417 sscanf(pci_devs_to_hide + pos,
1418 " (%x:%x:*.*) %n",
1419 &domain, &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001420 break;
1421 }
1422
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001423 if (!parsed) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001424 domain = 0;
1425 err = sscanf(pci_devs_to_hide + pos,
1426 " (%x:%x.%x) %n",
1427 &bus, &slot, &func, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001428 switch (err) {
1429 case 2:
1430 func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001431 sscanf(pci_devs_to_hide + pos,
1432 " (%x:%x.*) %n",
1433 &bus, &slot, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001434 break;
1435 case 1:
1436 slot = func = -1;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001437 sscanf(pci_devs_to_hide + pos,
1438 " (%x:*.*) %n",
1439 &bus, &parsed);
Jan Beulichc3cb4702012-09-18 12:29:03 +01001440 break;
1441 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001442 }
1443
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001444 if (parsed <= 0)
1445 goto parse_error;
1446
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001447 err = pcistub_device_id_add(domain, bus, slot, func);
1448 if (err)
1449 goto out;
1450
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001451 pos += parsed;
Jan Beulich5b71fbd2012-11-02 14:36:38 +00001452 } while (pci_devs_to_hide[pos]);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001453 }
1454
1455 /* If we're the first PCI Device Driver to register, we're the
1456 * first one to get offered PCI devices as they become
1457 * available (and thus we can be the first to grab them)
1458 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001459 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001460 if (err < 0)
1461 goto out;
1462
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001463 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001464 &driver_attr_new_slot);
1465 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001466 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001467 &driver_attr_remove_slot);
1468 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001469 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001470 &driver_attr_slots);
1471 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001472 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001473 &driver_attr_quirks);
1474 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001475 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001476 &driver_attr_permissive);
1477
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001478 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001479 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001480 &driver_attr_irq_handlers);
1481 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001482 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001483 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001484 if (err)
1485 pcistub_exit();
1486
1487out:
1488 return err;
1489
1490parse_error:
Joe Perches283c0972013-06-28 03:21:41 -07001491 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001492 pci_devs_to_hide + pos);
1493 return -EINVAL;
1494}
1495
1496#ifndef MODULE
1497/*
1498 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001499 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001500 * want to suck up any device before other drivers
1501 * get a chance by being the first pci device
1502 * driver to register)
1503 */
1504fs_initcall(pcistub_init);
1505#endif
1506
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001507static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001508{
1509 int err;
1510
1511 if (!xen_initial_domain())
1512 return -ENODEV;
1513
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001514 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001515 if (err)
1516 return err;
1517
1518#ifdef MODULE
1519 err = pcistub_init();
1520 if (err < 0)
1521 return err;
1522#endif
1523
1524 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001525 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001526 if (err)
1527 pcistub_exit();
1528
1529 return err;
1530}
1531
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001532static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001533{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001534 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001535 pcistub_exit();
1536}
1537
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001538module_init(xen_pcibk_init);
1539module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001540
1541MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001542MODULE_ALIAS("xen-backend:pci");