]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/xen/xen-pciback/pci_stub.c
xen/pciback: Check dev_data before using it
[mirror_ubuntu-bionic-kernel.git] / drivers / xen / xen-pciback / pci_stub.c
CommitLineData
30edc14b
KRW
1/*
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 */
283c0972
JP
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
30edc14b
KRW
10#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>
8bfd4e02 19#include <linux/atomic.h>
30edc14b
KRW
20#include <xen/events.h>
21#include <asm/xen/pci.h>
22#include <asm/xen/hypervisor.h>
909b3fdb 23#include <xen/interface/physdev.h>
30edc14b
KRW
24#include "pciback.h"
25#include "conf_space.h"
26#include "conf_space_quirks.h"
27
b057878b
JG
28#define PCISTUB_DRIVER_NAME "pciback"
29
30edc14b 30static char *pci_devs_to_hide;
a92336a1
KRW
31wait_queue_head_t xen_pcibk_aer_wait_queue;
32/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
30edc14b
KRW
34*/
35static DECLARE_RWSEM(pcistub_sem);
36module_param_named(hide, pci_devs_to_hide, charp, 0444);
37
38struct pcistub_device_id {
39 struct list_head slot_list;
40 int domain;
41 unsigned char bus;
42 unsigned int devfn;
43};
44static LIST_HEAD(pcistub_device_ids);
45static DEFINE_SPINLOCK(device_ids_lock);
46
47struct pcistub_device {
48 struct kref kref;
49 struct list_head dev_list;
50 spinlock_t lock;
51
52 struct pci_dev *dev;
a92336a1 53 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
30edc14b
KRW
54};
55
56/* Access to pcistub_devices & seized_devices lists and the initialize_devices
57 * flag must be locked with pcistub_devices_lock
58 */
59static DEFINE_SPINLOCK(pcistub_devices_lock);
60static LIST_HEAD(pcistub_devices);
61
62/* wait for device_initcall before initializing our devices
63 * (see pcistub_init_devices_late)
64 */
65static int initialize_devices;
66static LIST_HEAD(seized_devices);
67
68static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
69{
70 struct pcistub_device *psdev;
71
72 dev_dbg(&dev->dev, "pcistub_device_alloc\n");
73
74 psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
75 if (!psdev)
76 return NULL;
77
78 psdev->dev = pci_dev_get(dev);
79 if (!psdev->dev) {
80 kfree(psdev);
81 return NULL;
82 }
83
84 kref_init(&psdev->kref);
85 spin_lock_init(&psdev->lock);
86
87 return psdev;
88}
89
90/* Don't call this directly as it's called by pcistub_device_put */
91static void pcistub_device_release(struct kref *kref)
92{
93 struct pcistub_device *psdev;
909b3fdb 94 struct pci_dev *dev;
cd9db80e 95 struct xen_pcibk_dev_data *dev_data;
30edc14b
KRW
96
97 psdev = container_of(kref, struct pcistub_device, kref);
909b3fdb
JB
98 dev = psdev->dev;
99 dev_data = pci_get_drvdata(dev);
30edc14b 100
909b3fdb 101 dev_dbg(&dev->dev, "pcistub_device_release\n");
30edc14b 102
909b3fdb 103 xen_unregister_device_domain_owner(dev);
6221a9b2 104
cd9db80e
KRW
105 /* Call the reset function which does not take lock as this
106 * is called from "unbind" which takes a device_lock mutex.
107 */
909b3fdb 108 __pci_reset_function_locked(dev);
e6622b64
RL
109 if (dev_data &&
110 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
b1df4a56 111 dev_info(&dev->dev, "Could not reload PCI state\n");
909b3fdb
JB
112 else
113 pci_restore_state(dev);
114
d69c0e39 115 if (dev->msix_cap) {
909b3fdb
JB
116 struct physdev_pci_device ppdev = {
117 .seg = pci_domain_nr(dev->bus),
118 .bus = dev->bus->number,
119 .devfn = dev->devfn
120 };
121 int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
122 &ppdev);
123
74beaf62 124 if (err && err != -ENOSYS)
909b3fdb
JB
125 dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
126 err);
127 }
cd9db80e
KRW
128
129 /* Disable the device */
909b3fdb 130 xen_pcibk_reset_device(dev);
cd9db80e
KRW
131
132 kfree(dev_data);
909b3fdb 133 pci_set_drvdata(dev, NULL);
cd9db80e
KRW
134
135 /* Clean-up the device */
909b3fdb
JB
136 xen_pcibk_config_free_dyn_fields(dev);
137 xen_pcibk_config_free_dev(dev);
30edc14b 138
be507fd0 139 pci_clear_dev_assigned(dev);
909b3fdb 140 pci_dev_put(dev);
30edc14b
KRW
141
142 kfree(psdev);
143}
144
145static inline void pcistub_device_get(struct pcistub_device *psdev)
146{
147 kref_get(&psdev->kref);
148}
149
150static inline void pcistub_device_put(struct pcistub_device *psdev)
151{
152 kref_put(&psdev->kref, pcistub_device_release);
153}
154
1af916b7
JG
155static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
156 int slot, int func)
30edc14b 157{
1af916b7 158 struct pcistub_device *psdev;
30edc14b
KRW
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
b3e40b72
JB
164 && slot == PCI_SLOT(psdev->dev->devfn)
165 && func == PCI_FUNC(psdev->dev->devfn)) {
1af916b7 166 return psdev;
30edc14b
KRW
167 }
168 }
169
1af916b7
JG
170 return NULL;
171}
172
173static struct pcistub_device *pcistub_device_find(int domain, int bus,
174 int slot, int func)
175{
176 struct pcistub_device *psdev;
177 unsigned long flags;
178
179 spin_lock_irqsave(&pcistub_devices_lock, flags);
180
181 psdev = pcistub_device_find_locked(domain, bus, slot, func);
182 if (psdev)
183 pcistub_device_get(psdev);
30edc14b 184
30edc14b
KRW
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 return psdev;
187}
188
a92336a1 189static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
30edc14b
KRW
190 struct pcistub_device *psdev)
191{
192 struct pci_dev *pci_dev = NULL;
193 unsigned long flags;
194
195 pcistub_device_get(psdev);
196
197 spin_lock_irqsave(&psdev->lock, flags);
198 if (!psdev->pdev) {
199 psdev->pdev = pdev;
200 pci_dev = psdev->dev;
201 }
202 spin_unlock_irqrestore(&psdev->lock, flags);
203
204 if (!pci_dev)
205 pcistub_device_put(psdev);
206
207 return pci_dev;
208}
209
a92336a1 210struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
30edc14b
KRW
211 int domain, int bus,
212 int slot, int func)
213{
214 struct pcistub_device *psdev;
215 struct pci_dev *found_dev = NULL;
216 unsigned long flags;
217
218 spin_lock_irqsave(&pcistub_devices_lock, flags);
219
1af916b7
JG
220 psdev = pcistub_device_find_locked(domain, bus, slot, func);
221 if (psdev)
222 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
30edc14b
KRW
223
224 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 return found_dev;
226}
227
a92336a1 228struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
30edc14b
KRW
229 struct pci_dev *dev)
230{
231 struct pcistub_device *psdev;
232 struct pci_dev *found_dev = NULL;
233 unsigned long flags;
234
235 spin_lock_irqsave(&pcistub_devices_lock, flags);
236
237 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 if (psdev->dev == dev) {
239 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
240 break;
241 }
242 }
243
244 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
245 return found_dev;
246}
247
0a9fd015
KRW
248/*
249 * Called when:
250 * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251 * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252 * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253 * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
254 *
255 * As such we have to be careful.
e8801a74
KRW
256 *
257 * To make this easier, the caller has to hold the device lock.
0a9fd015 258 */
30edc14b
KRW
259void pcistub_put_pci_dev(struct pci_dev *dev)
260{
261 struct pcistub_device *psdev, *found_psdev = NULL;
262 unsigned long flags;
b1df4a56
KRW
263 struct xen_pcibk_dev_data *dev_data;
264 int ret;
30edc14b
KRW
265
266 spin_lock_irqsave(&pcistub_devices_lock, flags);
267
268 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 if (psdev->dev == dev) {
270 found_psdev = psdev;
271 break;
272 }
273 }
274
275 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
4645bf30
KRW
276 if (WARN_ON(!found_psdev))
277 return;
30edc14b
KRW
278
279 /*hold this lock for avoiding breaking link between
a92336a1 280 * pcistub and xen_pcibk when AER is in processing
30edc14b
KRW
281 */
282 down_write(&pcistub_sem);
283 /* Cleanup our device
284 * (so it's ready for the next domain)
285 */
ac801022 286 device_lock_assert(&dev->dev);
e8801a74 287 __pci_reset_function_locked(dev);
cd9db80e 288
b1df4a56
KRW
289 dev_data = pci_get_drvdata(dev);
290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 if (!ret) {
292 /*
293 * The usual sequence is pci_save_state & pci_restore_state
294 * but the guest might have messed the configuration space up.
295 * Use the initial version (when device was bound to us).
296 */
297 pci_restore_state(dev);
298 } else
299 dev_info(&dev->dev, "Could not reload PCI state\n");
cd9db80e 300 /* This disables the device. */
8899035e 301 xen_pcibk_reset_device(dev);
cd9db80e
KRW
302
303 /* And cleanup up our emulated fields. */
8899035e 304 xen_pcibk_config_reset_dev(dev);
fcb8ce96 305 xen_pcibk_config_free_dyn_fields(dev);
30edc14b 306
8899035e 307 xen_unregister_device_domain_owner(dev);
31673558 308
30edc14b
KRW
309 spin_lock_irqsave(&found_psdev->lock, flags);
310 found_psdev->pdev = NULL;
311 spin_unlock_irqrestore(&found_psdev->lock, flags);
312
313 pcistub_device_put(found_psdev);
314 up_write(&pcistub_sem);
315}
316
345a5255
GKH
317static int pcistub_match_one(struct pci_dev *dev,
318 struct pcistub_device_id *pdev_id)
30edc14b
KRW
319{
320 /* Match the specified device by domain, bus, slot, func and also if
321 * any of the device's parent bridges match.
322 */
323 for (; dev != NULL; dev = dev->bus->self) {
324 if (pci_domain_nr(dev->bus) == pdev_id->domain
325 && dev->bus->number == pdev_id->bus
326 && dev->devfn == pdev_id->devfn)
327 return 1;
328
329 /* Sometimes topmost bridge links to itself. */
330 if (dev == dev->bus->self)
331 break;
332 }
333
334 return 0;
335}
336
345a5255 337static int pcistub_match(struct pci_dev *dev)
30edc14b
KRW
338{
339 struct pcistub_device_id *pdev_id;
340 unsigned long flags;
341 int found = 0;
342
343 spin_lock_irqsave(&device_ids_lock, flags);
344 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
345 if (pcistub_match_one(dev, pdev_id)) {
346 found = 1;
347 break;
348 }
349 }
350 spin_unlock_irqrestore(&device_ids_lock, flags);
351
352 return found;
353}
354
345a5255 355static int pcistub_init_device(struct pci_dev *dev)
30edc14b 356{
a92336a1 357 struct xen_pcibk_dev_data *dev_data;
30edc14b
KRW
358 int err = 0;
359
360 dev_dbg(&dev->dev, "initializing...\n");
361
362 /* The PCI backend is not intended to be a module (or to work with
a92336a1 363 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
30edc14b
KRW
364 * would need to be called somewhere to free the memory allocated
365 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
366 */
0513fe9e
KRW
367 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
368 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
30edc14b
KRW
369 if (!dev_data) {
370 err = -ENOMEM;
371 goto out;
372 }
373 pci_set_drvdata(dev, dev_data);
374
0513fe9e
KRW
375 /*
376 * Setup name for fake IRQ handler. It will only be enabled
377 * once the device is turned on by the guest.
378 */
379 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
380
30edc14b
KRW
381 dev_dbg(&dev->dev, "initializing config\n");
382
a92336a1
KRW
383 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
384 err = xen_pcibk_config_init_dev(dev);
30edc14b
KRW
385 if (err)
386 goto out;
387
388 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
389 * must do this here because pcibios_enable_device may specify
390 * the pci device's true irq (and possibly its other resources)
391 * if they differ from what's in the configuration space.
392 * This makes the assumption that the device's resources won't
393 * change after this point (otherwise this code may break!)
394 */
395 dev_dbg(&dev->dev, "enabling device\n");
396 err = pci_enable_device(dev);
397 if (err)
398 goto config_release;
399
d69c0e39 400 if (dev->msix_cap) {
909b3fdb
JB
401 struct physdev_pci_device ppdev = {
402 .seg = pci_domain_nr(dev->bus),
403 .bus = dev->bus->number,
404 .devfn = dev->devfn
405 };
406
407 err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
74beaf62 408 if (err && err != -ENOSYS)
909b3fdb
JB
409 dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
410 err);
411 }
412
cd9db80e
KRW
413 /* We need the device active to save the state. */
414 dev_dbg(&dev->dev, "save state of device\n");
415 pci_save_state(dev);
416 dev_data->pci_saved_state = pci_store_saved_state(dev);
417 if (!dev_data->pci_saved_state)
418 dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
80ba77df 419 else {
744627e9 420 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
80ba77df 421 __pci_reset_function_locked(dev);
c341ca45 422 pci_restore_state(dev);
80ba77df 423 }
30edc14b
KRW
424 /* Now disable the device (this also ensures some private device
425 * data is setup before we export)
426 */
427 dev_dbg(&dev->dev, "reset device\n");
a92336a1 428 xen_pcibk_reset_device(dev);
30edc14b 429
be507fd0 430 pci_set_dev_assigned(dev);
30edc14b
KRW
431 return 0;
432
433config_release:
a92336a1 434 xen_pcibk_config_free_dev(dev);
30edc14b
KRW
435
436out:
437 pci_set_drvdata(dev, NULL);
438 kfree(dev_data);
439 return err;
440}
441
442/*
443 * Because some initialization still happens on
444 * devices during fs_initcall, we need to defer
445 * full initialization of our devices until
446 * device_initcall.
447 */
448static int __init pcistub_init_devices_late(void)
449{
450 struct pcistub_device *psdev;
451 unsigned long flags;
452 int err = 0;
453
30edc14b
KRW
454 spin_lock_irqsave(&pcistub_devices_lock, flags);
455
456 while (!list_empty(&seized_devices)) {
457 psdev = container_of(seized_devices.next,
458 struct pcistub_device, dev_list);
459 list_del(&psdev->dev_list);
460
461 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
462
463 err = pcistub_init_device(psdev->dev);
464 if (err) {
465 dev_err(&psdev->dev->dev,
466 "error %d initializing device\n", err);
467 kfree(psdev);
468 psdev = NULL;
469 }
470
471 spin_lock_irqsave(&pcistub_devices_lock, flags);
472
473 if (psdev)
474 list_add_tail(&psdev->dev_list, &pcistub_devices);
475 }
476
477 initialize_devices = 1;
478
479 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
480
481 return 0;
482}
483
9f8bee9c
JG
484static void pcistub_device_id_add_list(struct pcistub_device_id *new,
485 int domain, int bus, unsigned int devfn)
486{
487 struct pcistub_device_id *pci_dev_id;
488 unsigned long flags;
489 int found = 0;
490
491 spin_lock_irqsave(&device_ids_lock, flags);
492
493 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
494 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
495 pci_dev_id->devfn == devfn) {
496 found = 1;
497 break;
498 }
499 }
500
501 if (!found) {
502 new->domain = domain;
503 new->bus = bus;
504 new->devfn = devfn;
505 list_add_tail(&new->slot_list, &pcistub_device_ids);
506 }
507
508 spin_unlock_irqrestore(&device_ids_lock, flags);
509
510 if (found)
511 kfree(new);
512}
513
b057878b
JG
514static int pcistub_seize(struct pci_dev *dev,
515 struct pcistub_device_id *pci_dev_id)
30edc14b
KRW
516{
517 struct pcistub_device *psdev;
518 unsigned long flags;
519 int err = 0;
520
521 psdev = pcistub_device_alloc(dev);
b057878b
JG
522 if (!psdev) {
523 kfree(pci_dev_id);
30edc14b 524 return -ENOMEM;
b057878b 525 }
30edc14b
KRW
526
527 spin_lock_irqsave(&pcistub_devices_lock, flags);
528
529 if (initialize_devices) {
530 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
531
532 /* don't want irqs disabled when calling pcistub_init_device */
533 err = pcistub_init_device(psdev->dev);
534
535 spin_lock_irqsave(&pcistub_devices_lock, flags);
536
537 if (!err)
538 list_add(&psdev->dev_list, &pcistub_devices);
539 } else {
540 dev_dbg(&dev->dev, "deferring initialization\n");
541 list_add(&psdev->dev_list, &seized_devices);
542 }
543
544 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
545
b057878b
JG
546 if (err) {
547 kfree(pci_dev_id);
30edc14b 548 pcistub_device_put(psdev);
b057878b
JG
549 } else if (pci_dev_id)
550 pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
551 dev->bus->number, dev->devfn);
30edc14b
KRW
552
553 return err;
554}
555
24d8bf1b
KRW
556/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
557 * other functions that take the sysfs lock. */
345a5255 558static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
30edc14b 559{
b057878b
JG
560 int err = 0, match;
561 struct pcistub_device_id *pci_dev_id = NULL;
30edc14b
KRW
562
563 dev_dbg(&dev->dev, "probing...\n");
564
b057878b
JG
565 match = pcistub_match(dev);
566
567 if ((dev->driver_override &&
568 !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
569 match) {
30edc14b
KRW
570
571 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
572 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
573 dev_err(&dev->dev, "can't export pci devices that "
574 "don't have a normal (0) or bridge (1) "
575 "header type!\n");
576 err = -ENODEV;
577 goto out;
578 }
579
b057878b
JG
580 if (!match) {
581 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_ATOMIC);
582 if (!pci_dev_id) {
583 err = -ENOMEM;
584 goto out;
585 }
586 }
587
30edc14b 588 dev_info(&dev->dev, "seizing device\n");
b057878b 589 err = pcistub_seize(dev, pci_dev_id);
30edc14b
KRW
590 } else
591 /* Didn't find the device */
592 err = -ENODEV;
593
594out:
595 return err;
596}
597
24d8bf1b
KRW
598/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
599 * other functions that take the sysfs lock. */
30edc14b
KRW
600static void pcistub_remove(struct pci_dev *dev)
601{
602 struct pcistub_device *psdev, *found_psdev = NULL;
603 unsigned long flags;
604
605 dev_dbg(&dev->dev, "removing\n");
606
607 spin_lock_irqsave(&pcistub_devices_lock, flags);
608
a92336a1 609 xen_pcibk_config_quirk_release(dev);
30edc14b
KRW
610
611 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
612 if (psdev->dev == dev) {
613 found_psdev = psdev;
614 break;
615 }
616 }
617
618 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
619
620 if (found_psdev) {
b5d51214
KRW
621 dev_dbg(&dev->dev, "found device to remove %s\n",
622 found_psdev->pdev ? "- in-use" : "");
30edc14b
KRW
623
624 if (found_psdev->pdev) {
b5d51214
KRW
625 int domid = xen_find_device_domain_owner(dev);
626
627 pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
628 pci_name(found_psdev->dev), domid);
283c0972
JP
629 pr_warn("****** driver domain may still access this device's i/o resources!\n");
630 pr_warn("****** shutdown driver domain before binding device\n");
631 pr_warn("****** to other drivers or domains\n");
30edc14b 632
8be9df6d
KRW
633 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
634 * doing the FLR. */
a92336a1 635 xen_pcibk_release_pci_dev(found_psdev->pdev,
e8801a74
KRW
636 found_psdev->dev,
637 false /* caller holds the lock. */);
30edc14b
KRW
638 }
639
640 spin_lock_irqsave(&pcistub_devices_lock, flags);
641 list_del(&found_psdev->dev_list);
642 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
643
644 /* the final put for releasing from the list */
645 pcistub_device_put(found_psdev);
646 }
647}
648
9baa3c34 649static const struct pci_device_id pcistub_ids[] = {
30edc14b
KRW
650 {
651 .vendor = PCI_ANY_ID,
652 .device = PCI_ANY_ID,
653 .subvendor = PCI_ANY_ID,
654 .subdevice = PCI_ANY_ID,
655 },
656 {0,},
657};
658
659#define PCI_NODENAME_MAX 40
660static void kill_domain_by_device(struct pcistub_device *psdev)
661{
662 struct xenbus_transaction xbt;
663 int err;
664 char nodename[PCI_NODENAME_MAX];
665
72bf809a 666 BUG_ON(!psdev);
30edc14b
KRW
667 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
668 psdev->pdev->xdev->otherend_id);
30edc14b
KRW
669
670again:
671 err = xenbus_transaction_start(&xbt);
672 if (err) {
673 dev_err(&psdev->dev->dev,
674 "error %d when start xenbus transaction\n", err);
675 return;
676 }
677 /*PV AER handlers will set this flag*/
678 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
679 err = xenbus_transaction_end(xbt, 0);
680 if (err) {
681 if (err == -EAGAIN)
682 goto again;
683 dev_err(&psdev->dev->dev,
684 "error %d when end xenbus transaction\n", err);
685 return;
686 }
687}
688
689/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
a92336a1 690 * backend need to have cooperation. In xen_pcibk, those steps will do similar
30edc14b
KRW
691 * jobs: send service request and waiting for front_end response.
692*/
693static pci_ers_result_t common_process(struct pcistub_device *psdev,
a92336a1
KRW
694 pci_channel_state_t state, int aer_cmd,
695 pci_ers_result_t result)
30edc14b
KRW
696{
697 pci_ers_result_t res = result;
698 struct xen_pcie_aer_op *aer_op;
c1a04339
KRW
699 struct xen_pcibk_device *pdev = psdev->pdev;
700 struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
30edc14b
KRW
701 int ret;
702
703 /*with PV AER drivers*/
c1a04339 704 aer_op = &(sh_info->aer_op);
30edc14b
KRW
705 aer_op->cmd = aer_cmd ;
706 /*useful for error_detected callback*/
707 aer_op->err = state;
708 /*pcifront_end BDF*/
a92336a1 709 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
30edc14b
KRW
710 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
711 if (!ret) {
712 dev_err(&psdev->dev->dev,
a92336a1 713 DRV_NAME ": failed to get pcifront device\n");
30edc14b
KRW
714 return PCI_ERS_RESULT_NONE;
715 }
716 wmb();
717
718 dev_dbg(&psdev->dev->dev,
a92336a1 719 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
30edc14b 720 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
a92336a1
KRW
721 /*local flag to mark there's aer request, xen_pcibk callback will use
722 * this flag to judge whether we need to check pci-front give aer
723 * service ack signal
30edc14b 724 */
c1a04339 725 set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
30edc14b
KRW
726
727 /*It is possible that a pcifront conf_read_write ops request invokes
728 * the callback which cause the spurious execution of wake_up.
729 * Yet it is harmless and better than a spinlock here
730 */
731 set_bit(_XEN_PCIB_active,
c1a04339 732 (unsigned long *)&sh_info->flags);
30edc14b 733 wmb();
c1a04339 734 notify_remote_via_irq(pdev->evtchn_irq);
30edc14b 735
a92336a1
KRW
736 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
737 !(test_bit(_XEN_PCIB_active, (unsigned long *)
c1a04339 738 &sh_info->flags)), 300*HZ);
30edc14b
KRW
739
740 if (!ret) {
741 if (test_bit(_XEN_PCIB_active,
c1a04339 742 (unsigned long *)&sh_info->flags)) {
30edc14b
KRW
743 dev_err(&psdev->dev->dev,
744 "pcifront aer process not responding!\n");
745 clear_bit(_XEN_PCIB_active,
c1a04339 746 (unsigned long *)&sh_info->flags);
30edc14b
KRW
747 aer_op->err = PCI_ERS_RESULT_NONE;
748 return res;
749 }
750 }
c1a04339 751 clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
30edc14b
KRW
752
753 if (test_bit(_XEN_PCIF_active,
c1a04339 754 (unsigned long *)&sh_info->flags)) {
30edc14b 755 dev_dbg(&psdev->dev->dev,
402c5e15 756 "schedule pci_conf service in " DRV_NAME "\n");
a92336a1 757 xen_pcibk_test_and_schedule_op(psdev->pdev);
30edc14b
KRW
758 }
759
760 res = (pci_ers_result_t)aer_op->err;
761 return res;
762}
763
764/*
a92336a1 765* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
30edc14b
KRW
766* of the device driver could provide this service, and then wait for pcifront
767* ack.
768* @dev: pointer to PCI devices
769* return value is used by aer_core do_recovery policy
770*/
a92336a1 771static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
30edc14b
KRW
772{
773 struct pcistub_device *psdev;
774 pci_ers_result_t result;
775
776 result = PCI_ERS_RESULT_RECOVERED;
a92336a1 777 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
30edc14b
KRW
778 dev->bus->number, dev->devfn);
779
780 down_write(&pcistub_sem);
781 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
782 dev->bus->number,
783 PCI_SLOT(dev->devfn),
784 PCI_FUNC(dev->devfn));
785
786 if (!psdev || !psdev->pdev) {
787 dev_err(&dev->dev,
a92336a1 788 DRV_NAME " device is not found/assigned\n");
30edc14b
KRW
789 goto end;
790 }
791
792 if (!psdev->pdev->sh_info) {
a92336a1 793 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
30edc14b
KRW
794 " by HVM, kill it\n");
795 kill_domain_by_device(psdev);
e6aa70a0 796 goto end;
30edc14b
KRW
797 }
798
799 if (!test_bit(_XEN_PCIB_AERHANDLER,
800 (unsigned long *)&psdev->pdev->sh_info->flags)) {
801 dev_err(&dev->dev,
802 "guest with no AER driver should have been killed\n");
e6aa70a0 803 goto end;
30edc14b
KRW
804 }
805 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
806
807 if (result == PCI_ERS_RESULT_NONE ||
808 result == PCI_ERS_RESULT_DISCONNECT) {
809 dev_dbg(&dev->dev,
810 "No AER slot_reset service or disconnected!\n");
811 kill_domain_by_device(psdev);
812 }
30edc14b 813end:
e6aa70a0
JB
814 if (psdev)
815 pcistub_device_put(psdev);
30edc14b
KRW
816 up_write(&pcistub_sem);
817 return result;
818
819}
820
821
a92336a1 822/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
30edc14b
KRW
823* in case of the device driver could provide this service, and then wait
824* for pcifront ack
825* @dev: pointer to PCI devices
826* return value is used by aer_core do_recovery policy
827*/
828
a92336a1 829static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
30edc14b
KRW
830{
831 struct pcistub_device *psdev;
832 pci_ers_result_t result;
833
834 result = PCI_ERS_RESULT_RECOVERED;
a92336a1 835 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
30edc14b
KRW
836 dev->bus->number, dev->devfn);
837
838 down_write(&pcistub_sem);
839 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
840 dev->bus->number,
841 PCI_SLOT(dev->devfn),
842 PCI_FUNC(dev->devfn));
843
844 if (!psdev || !psdev->pdev) {
845 dev_err(&dev->dev,
a92336a1 846 DRV_NAME " device is not found/assigned\n");
30edc14b
KRW
847 goto end;
848 }
849
850 if (!psdev->pdev->sh_info) {
a92336a1 851 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
30edc14b
KRW
852 " by HVM, kill it\n");
853 kill_domain_by_device(psdev);
e6aa70a0 854 goto end;
30edc14b
KRW
855 }
856
857 if (!test_bit(_XEN_PCIB_AERHANDLER,
858 (unsigned long *)&psdev->pdev->sh_info->flags)) {
859 dev_err(&dev->dev,
860 "guest with no AER driver should have been killed\n");
e6aa70a0 861 goto end;
30edc14b
KRW
862 }
863 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
864
865 if (result == PCI_ERS_RESULT_NONE ||
866 result == PCI_ERS_RESULT_DISCONNECT) {
867 dev_dbg(&dev->dev,
868 "No AER mmio_enabled service or disconnected!\n");
869 kill_domain_by_device(psdev);
870 }
30edc14b 871end:
e6aa70a0
JB
872 if (psdev)
873 pcistub_device_put(psdev);
30edc14b
KRW
874 up_write(&pcistub_sem);
875 return result;
876}
877
a92336a1 878/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
30edc14b
KRW
879* in case of the device driver could provide this service, and then wait
880* for pcifront ack.
881* @dev: pointer to PCI devices
882* @error: the current PCI connection state
883* return value is used by aer_core do_recovery policy
884*/
885
a92336a1 886static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
30edc14b
KRW
887 pci_channel_state_t error)
888{
889 struct pcistub_device *psdev;
890 pci_ers_result_t result;
891
892 result = PCI_ERS_RESULT_CAN_RECOVER;
a92336a1 893 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
30edc14b
KRW
894 dev->bus->number, dev->devfn);
895
896 down_write(&pcistub_sem);
897 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
898 dev->bus->number,
899 PCI_SLOT(dev->devfn),
900 PCI_FUNC(dev->devfn));
901
902 if (!psdev || !psdev->pdev) {
903 dev_err(&dev->dev,
a92336a1 904 DRV_NAME " device is not found/assigned\n");
30edc14b
KRW
905 goto end;
906 }
907
908 if (!psdev->pdev->sh_info) {
a92336a1 909 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
30edc14b
KRW
910 " by HVM, kill it\n");
911 kill_domain_by_device(psdev);
e6aa70a0 912 goto end;
30edc14b
KRW
913 }
914
915 /*Guest owns the device yet no aer handler regiested, kill guest*/
916 if (!test_bit(_XEN_PCIB_AERHANDLER,
917 (unsigned long *)&psdev->pdev->sh_info->flags)) {
918 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
919 kill_domain_by_device(psdev);
e6aa70a0 920 goto end;
30edc14b
KRW
921 }
922 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
923
924 if (result == PCI_ERS_RESULT_NONE ||
925 result == PCI_ERS_RESULT_DISCONNECT) {
926 dev_dbg(&dev->dev,
927 "No AER error_detected service or disconnected!\n");
928 kill_domain_by_device(psdev);
929 }
30edc14b 930end:
e6aa70a0
JB
931 if (psdev)
932 pcistub_device_put(psdev);
30edc14b
KRW
933 up_write(&pcistub_sem);
934 return result;
935}
936
a92336a1 937/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
30edc14b
KRW
938* in case of the device driver could provide this service, and then wait
939* for pcifront ack.
940* @dev: pointer to PCI devices
941*/
942
a92336a1 943static void xen_pcibk_error_resume(struct pci_dev *dev)
30edc14b
KRW
944{
945 struct pcistub_device *psdev;
946
a92336a1 947 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
30edc14b
KRW
948 dev->bus->number, dev->devfn);
949
950 down_write(&pcistub_sem);
951 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
952 dev->bus->number,
953 PCI_SLOT(dev->devfn),
954 PCI_FUNC(dev->devfn));
955
956 if (!psdev || !psdev->pdev) {
957 dev_err(&dev->dev,
a92336a1 958 DRV_NAME " device is not found/assigned\n");
30edc14b
KRW
959 goto end;
960 }
961
962 if (!psdev->pdev->sh_info) {
a92336a1 963 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
30edc14b
KRW
964 " by HVM, kill it\n");
965 kill_domain_by_device(psdev);
e6aa70a0 966 goto end;
30edc14b
KRW
967 }
968
969 if (!test_bit(_XEN_PCIB_AERHANDLER,
970 (unsigned long *)&psdev->pdev->sh_info->flags)) {
971 dev_err(&dev->dev,
972 "guest with no AER driver should have been killed\n");
973 kill_domain_by_device(psdev);
e6aa70a0 974 goto end;
30edc14b
KRW
975 }
976 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
977 PCI_ERS_RESULT_RECOVERED);
30edc14b 978end:
e6aa70a0
JB
979 if (psdev)
980 pcistub_device_put(psdev);
30edc14b
KRW
981 up_write(&pcistub_sem);
982 return;
983}
984
a92336a1 985/*add xen_pcibk AER handling*/
1d352035 986static const struct pci_error_handlers xen_pcibk_error_handler = {
a92336a1
KRW
987 .error_detected = xen_pcibk_error_detected,
988 .mmio_enabled = xen_pcibk_mmio_enabled,
989 .slot_reset = xen_pcibk_slot_reset,
990 .resume = xen_pcibk_error_resume,
30edc14b
KRW
991};
992
993/*
994 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
995 * for a normal device. I don't want it to be loaded automatically.
996 */
997
a92336a1
KRW
998static struct pci_driver xen_pcibk_pci_driver = {
999 /* The name should be xen_pciback, but until the tools are updated
1000 * we will keep it as pciback. */
b057878b 1001 .name = PCISTUB_DRIVER_NAME,
30edc14b
KRW
1002 .id_table = pcistub_ids,
1003 .probe = pcistub_probe,
1004 .remove = pcistub_remove,
a92336a1 1005 .err_handler = &xen_pcibk_error_handler,
30edc14b
KRW
1006};
1007
1008static inline int str_to_slot(const char *buf, int *domain, int *bus,
1009 int *slot, int *func)
1010{
5b71fbdc 1011 int parsed = 0;
30edc14b 1012
5b71fbdc
JB
1013 switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1014 &parsed)) {
c3cb4709
JB
1015 case 3:
1016 *func = -1;
5b71fbdc 1017 sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
c3cb4709
JB
1018 break;
1019 case 2:
1020 *slot = *func = -1;
5b71fbdc 1021 sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
c3cb4709
JB
1022 break;
1023 }
5b71fbdc 1024 if (parsed && !buf[parsed])
30edc14b 1025 return 0;
30edc14b
KRW
1026
1027 /* try again without domain */
1028 *domain = 0;
5b71fbdc 1029 switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
c3cb4709
JB
1030 case 2:
1031 *func = -1;
5b71fbdc 1032 sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
c3cb4709
JB
1033 break;
1034 case 1:
1035 *slot = *func = -1;
5b71fbdc 1036 sscanf(buf, " %x:*.* %n", bus, &parsed);
c3cb4709
JB
1037 break;
1038 }
5b71fbdc 1039 if (parsed && !buf[parsed])
30edc14b
KRW
1040 return 0;
1041
1042 return -EINVAL;
1043}
1044
1045static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1046 *slot, int *func, int *reg, int *size, int *mask)
1047{
5b71fbdc
JB
1048 int parsed = 0;
1049
b3e40b72 1050 sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
5b71fbdc
JB
1051 reg, size, mask, &parsed);
1052 if (parsed && !buf[parsed])
1053 return 0;
30edc14b 1054
5b71fbdc
JB
1055 /* try again without domain */
1056 *domain = 0;
b3e40b72 1057 sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
5b71fbdc
JB
1058 mask, &parsed);
1059 if (parsed && !buf[parsed])
30edc14b 1060 return 0;
5b71fbdc 1061
30edc14b
KRW
1062 return -EINVAL;
1063}
1064
1065static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1066{
1067 struct pcistub_device_id *pci_dev_id;
b3e40b72 1068 int rc = 0, devfn = PCI_DEVFN(slot, func);
c3cb4709
JB
1069
1070 if (slot < 0) {
1071 for (slot = 0; !rc && slot < 32; ++slot)
1072 rc = pcistub_device_id_add(domain, bus, slot, func);
1073 return rc;
1074 }
1075
1076 if (func < 0) {
1077 for (func = 0; !rc && func < 8; ++func)
1078 rc = pcistub_device_id_add(domain, bus, slot, func);
1079 return rc;
1080 }
30edc14b 1081
b3e40b72
JB
1082 if ((
1083#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1084 || !defined(CONFIG_PCI_DOMAINS)
1085 !pci_domains_supported ? domain :
1086#endif
1087 domain < 0 || domain > 0xffff)
1088 || bus < 0 || bus > 0xff
1089 || PCI_SLOT(devfn) != slot
1090 || PCI_FUNC(devfn) != func)
1091 return -EINVAL;
1092
30edc14b
KRW
1093 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1094 if (!pci_dev_id)
1095 return -ENOMEM;
1096
283c0972 1097 pr_debug("wants to seize %04x:%02x:%02x.%d\n",
30edc14b
KRW
1098 domain, bus, slot, func);
1099
9f8bee9c 1100 pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
30edc14b
KRW
1101
1102 return 0;
1103}
1104
1105static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1106{
1107 struct pcistub_device_id *pci_dev_id, *t;
30edc14b
KRW
1108 int err = -ENOENT;
1109 unsigned long flags;
1110
1111 spin_lock_irqsave(&device_ids_lock, flags);
1112 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1113 slot_list) {
c3cb4709
JB
1114 if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1115 && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1116 && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
30edc14b
KRW
1117 /* Don't break; here because it's possible the same
1118 * slot could be in the list more than once
1119 */
1120 list_del(&pci_dev_id->slot_list);
1121 kfree(pci_dev_id);
1122
1123 err = 0;
1124
283c0972
JP
1125 pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1126 domain, bus, slot, func);
30edc14b
KRW
1127 }
1128 }
1129 spin_unlock_irqrestore(&device_ids_lock, flags);
1130
1131 return err;
1132}
1133
b3e40b72
JB
1134static int pcistub_reg_add(int domain, int bus, int slot, int func,
1135 unsigned int reg, unsigned int size,
1136 unsigned int mask)
30edc14b
KRW
1137{
1138 int err = 0;
1139 struct pcistub_device *psdev;
1140 struct pci_dev *dev;
1141 struct config_field *field;
1142
b3e40b72
JB
1143 if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1144 return -EINVAL;
1145
30edc14b 1146 psdev = pcistub_device_find(domain, bus, slot, func);
e6aa70a0 1147 if (!psdev) {
30edc14b
KRW
1148 err = -ENODEV;
1149 goto out;
1150 }
1151 dev = psdev->dev;
1152
1153 field = kzalloc(sizeof(*field), GFP_ATOMIC);
1154 if (!field) {
1155 err = -ENOMEM;
1156 goto out;
1157 }
1158
1159 field->offset = reg;
1160 field->size = size;
1161 field->mask = mask;
1162 field->init = NULL;
1163 field->reset = NULL;
1164 field->release = NULL;
a92336a1 1165 field->clean = xen_pcibk_config_field_free;
30edc14b 1166
a92336a1 1167 err = xen_pcibk_config_quirks_add_field(dev, field);
30edc14b
KRW
1168 if (err)
1169 kfree(field);
1170out:
e6aa70a0
JB
1171 if (psdev)
1172 pcistub_device_put(psdev);
30edc14b
KRW
1173 return err;
1174}
1175
0538dcb0
GKH
1176static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1177 size_t count)
30edc14b
KRW
1178{
1179 int domain, bus, slot, func;
1180 int err;
1181
1182 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1183 if (err)
1184 goto out;
1185
1186 err = pcistub_device_id_add(domain, bus, slot, func);
1187
1188out:
1189 if (!err)
1190 err = count;
1191 return err;
1192}
0538dcb0 1193static DRIVER_ATTR_WO(new_slot);
30edc14b 1194
0538dcb0
GKH
1195static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1196 size_t count)
30edc14b
KRW
1197{
1198 int domain, bus, slot, func;
1199 int err;
1200
1201 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1202 if (err)
1203 goto out;
1204
1205 err = pcistub_device_id_remove(domain, bus, slot, func);
1206
1207out:
1208 if (!err)
1209 err = count;
1210 return err;
1211}
0538dcb0 1212static DRIVER_ATTR_WO(remove_slot);
30edc14b 1213
0538dcb0 1214static ssize_t slots_show(struct device_driver *drv, char *buf)
30edc14b
KRW
1215{
1216 struct pcistub_device_id *pci_dev_id;
1217 size_t count = 0;
1218 unsigned long flags;
1219
1220 spin_lock_irqsave(&device_ids_lock, flags);
1221 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1222 if (count >= PAGE_SIZE)
1223 break;
1224
1225 count += scnprintf(buf + count, PAGE_SIZE - count,
e4de866a 1226 "%04x:%02x:%02x.%d\n",
30edc14b
KRW
1227 pci_dev_id->domain, pci_dev_id->bus,
1228 PCI_SLOT(pci_dev_id->devfn),
1229 PCI_FUNC(pci_dev_id->devfn));
1230 }
1231 spin_unlock_irqrestore(&device_ids_lock, flags);
1232
1233 return count;
1234}
0538dcb0 1235static DRIVER_ATTR_RO(slots);
30edc14b 1236
0538dcb0 1237static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
0513fe9e
KRW
1238{
1239 struct pcistub_device *psdev;
a92336a1 1240 struct xen_pcibk_dev_data *dev_data;
0513fe9e
KRW
1241 size_t count = 0;
1242 unsigned long flags;
1243
1244 spin_lock_irqsave(&pcistub_devices_lock, flags);
1245 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1246 if (count >= PAGE_SIZE)
1247 break;
1248 if (!psdev->dev)
1249 continue;
1250 dev_data = pci_get_drvdata(psdev->dev);
1251 if (!dev_data)
1252 continue;
1253 count +=
1254 scnprintf(buf + count, PAGE_SIZE - count,
1255 "%s:%s:%sing:%ld\n",
1256 pci_name(psdev->dev),
1257 dev_data->isr_on ? "on" : "off",
1258 dev_data->ack_intr ? "ack" : "not ack",
1259 dev_data->handled);
1260 }
1261 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1262 return count;
1263}
0538dcb0 1264static DRIVER_ATTR_RO(irq_handlers);
0513fe9e 1265
0538dcb0
GKH
1266static ssize_t irq_handler_state_store(struct device_driver *drv,
1267 const char *buf, size_t count)
0513fe9e
KRW
1268{
1269 struct pcistub_device *psdev;
a92336a1 1270 struct xen_pcibk_dev_data *dev_data;
0513fe9e 1271 int domain, bus, slot, func;
405010df 1272 int err;
0513fe9e
KRW
1273
1274 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1275 if (err)
e6aa70a0 1276 return err;
0513fe9e
KRW
1277
1278 psdev = pcistub_device_find(domain, bus, slot, func);
405010df
WY
1279 if (!psdev) {
1280 err = -ENOENT;
0513fe9e 1281 goto out;
405010df 1282 }
0513fe9e
KRW
1283
1284 dev_data = pci_get_drvdata(psdev->dev);
405010df
WY
1285 if (!dev_data) {
1286 err = -ENOENT;
0513fe9e 1287 goto out;
405010df 1288 }
0513fe9e
KRW
1289
1290 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1291 dev_data->irq_name, dev_data->isr_on,
1292 !dev_data->isr_on);
1293
1294 dev_data->isr_on = !(dev_data->isr_on);
1295 if (dev_data->isr_on)
1296 dev_data->ack_intr = 1;
1297out:
e6aa70a0
JB
1298 if (psdev)
1299 pcistub_device_put(psdev);
0513fe9e
KRW
1300 if (!err)
1301 err = count;
1302 return err;
1303}
0538dcb0 1304static DRIVER_ATTR_WO(irq_handler_state);
0513fe9e 1305
0538dcb0
GKH
1306static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1307 size_t count)
30edc14b
KRW
1308{
1309 int domain, bus, slot, func, reg, size, mask;
1310 int err;
1311
1312 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1313 &mask);
1314 if (err)
1315 goto out;
1316
1317 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1318
1319out:
1320 if (!err)
1321 err = count;
1322 return err;
1323}
1324
0538dcb0 1325static ssize_t quirks_show(struct device_driver *drv, char *buf)
30edc14b
KRW
1326{
1327 int count = 0;
1328 unsigned long flags;
a92336a1
KRW
1329 struct xen_pcibk_config_quirk *quirk;
1330 struct xen_pcibk_dev_data *dev_data;
30edc14b
KRW
1331 const struct config_field *field;
1332 const struct config_field_entry *cfg_entry;
1333
1334 spin_lock_irqsave(&device_ids_lock, flags);
a92336a1 1335 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
30edc14b
KRW
1336 if (count >= PAGE_SIZE)
1337 goto out;
1338
1339 count += scnprintf(buf + count, PAGE_SIZE - count,
1340 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1341 quirk->pdev->bus->number,
1342 PCI_SLOT(quirk->pdev->devfn),
1343 PCI_FUNC(quirk->pdev->devfn),
1344 quirk->devid.vendor, quirk->devid.device,
1345 quirk->devid.subvendor,
1346 quirk->devid.subdevice);
1347
1348 dev_data = pci_get_drvdata(quirk->pdev);
1349
1350 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1351 field = cfg_entry->field;
1352 if (count >= PAGE_SIZE)
1353 goto out;
1354
1355 count += scnprintf(buf + count, PAGE_SIZE - count,
1356 "\t\t%08x:%01x:%08x\n",
1357 cfg_entry->base_offset +
1358 field->offset, field->size,
1359 field->mask);
1360 }
1361 }
1362
1363out:
1364 spin_unlock_irqrestore(&device_ids_lock, flags);
1365
1366 return count;
1367}
0538dcb0 1368static DRIVER_ATTR_RW(quirks);
30edc14b 1369
0538dcb0
GKH
1370static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1371 size_t count)
30edc14b
KRW
1372{
1373 int domain, bus, slot, func;
1374 int err;
1375 struct pcistub_device *psdev;
a92336a1 1376 struct xen_pcibk_dev_data *dev_data;
b3e40b72 1377
30edc14b
KRW
1378 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1379 if (err)
1380 goto out;
b3e40b72 1381
30edc14b
KRW
1382 psdev = pcistub_device_find(domain, bus, slot, func);
1383 if (!psdev) {
1384 err = -ENODEV;
1385 goto out;
1386 }
e6aa70a0 1387
30edc14b
KRW
1388 dev_data = pci_get_drvdata(psdev->dev);
1389 /* the driver data for a device should never be null at this point */
1390 if (!dev_data) {
1391 err = -ENXIO;
1392 goto release;
1393 }
1394 if (!dev_data->permissive) {
1395 dev_data->permissive = 1;
1396 /* Let user know that what they're doing could be unsafe */
1397 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1398 "configuration space accesses!\n");
1399 dev_warn(&psdev->dev->dev,
1400 "permissive mode is potentially unsafe!\n");
1401 }
1402release:
1403 pcistub_device_put(psdev);
1404out:
1405 if (!err)
1406 err = count;
1407 return err;
1408}
1409
1410static ssize_t permissive_show(struct device_driver *drv, char *buf)
1411{
1412 struct pcistub_device *psdev;
a92336a1 1413 struct xen_pcibk_dev_data *dev_data;
30edc14b
KRW
1414 size_t count = 0;
1415 unsigned long flags;
1416 spin_lock_irqsave(&pcistub_devices_lock, flags);
1417 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1418 if (count >= PAGE_SIZE)
1419 break;
1420 if (!psdev->dev)
1421 continue;
1422 dev_data = pci_get_drvdata(psdev->dev);
1423 if (!dev_data || !dev_data->permissive)
1424 continue;
1425 count +=
1426 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1427 pci_name(psdev->dev));
1428 }
1429 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1430 return count;
1431}
0538dcb0 1432static DRIVER_ATTR_RW(permissive);
30edc14b
KRW
1433
1434static void pcistub_exit(void)
1435{
a92336a1
KRW
1436 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1437 driver_remove_file(&xen_pcibk_pci_driver.driver,
30edc14b 1438 &driver_attr_remove_slot);
a92336a1
KRW
1439 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1440 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1441 driver_remove_file(&xen_pcibk_pci_driver.driver,
1442 &driver_attr_permissive);
1443 driver_remove_file(&xen_pcibk_pci_driver.driver,
0513fe9e 1444 &driver_attr_irq_handlers);
a92336a1 1445 driver_remove_file(&xen_pcibk_pci_driver.driver,
0513fe9e 1446 &driver_attr_irq_handler_state);
a92336a1 1447 pci_unregister_driver(&xen_pcibk_pci_driver);
30edc14b
KRW
1448}
1449
1450static int __init pcistub_init(void)
1451{
1452 int pos = 0;
1453 int err = 0;
1454 int domain, bus, slot, func;
1455 int parsed;
1456
1457 if (pci_devs_to_hide && *pci_devs_to_hide) {
1458 do {
1459 parsed = 0;
1460
1461 err = sscanf(pci_devs_to_hide + pos,
1462 " (%x:%x:%x.%x) %n",
1463 &domain, &bus, &slot, &func, &parsed);
c3cb4709
JB
1464 switch (err) {
1465 case 3:
1466 func = -1;
5b71fbdc
JB
1467 sscanf(pci_devs_to_hide + pos,
1468 " (%x:%x:%x.*) %n",
1469 &domain, &bus, &slot, &parsed);
c3cb4709
JB
1470 break;
1471 case 2:
1472 slot = func = -1;
5b71fbdc
JB
1473 sscanf(pci_devs_to_hide + pos,
1474 " (%x:%x:*.*) %n",
1475 &domain, &bus, &parsed);
c3cb4709
JB
1476 break;
1477 }
1478
5b71fbdc 1479 if (!parsed) {
30edc14b
KRW
1480 domain = 0;
1481 err = sscanf(pci_devs_to_hide + pos,
1482 " (%x:%x.%x) %n",
1483 &bus, &slot, &func, &parsed);
c3cb4709
JB
1484 switch (err) {
1485 case 2:
1486 func = -1;
5b71fbdc
JB
1487 sscanf(pci_devs_to_hide + pos,
1488 " (%x:%x.*) %n",
1489 &bus, &slot, &parsed);
c3cb4709
JB
1490 break;
1491 case 1:
1492 slot = func = -1;
5b71fbdc
JB
1493 sscanf(pci_devs_to_hide + pos,
1494 " (%x:*.*) %n",
1495 &bus, &parsed);
c3cb4709
JB
1496 break;
1497 }
30edc14b
KRW
1498 }
1499
5b71fbdc
JB
1500 if (parsed <= 0)
1501 goto parse_error;
1502
30edc14b
KRW
1503 err = pcistub_device_id_add(domain, bus, slot, func);
1504 if (err)
1505 goto out;
1506
30edc14b 1507 pos += parsed;
5b71fbdc 1508 } while (pci_devs_to_hide[pos]);
30edc14b
KRW
1509 }
1510
1511 /* If we're the first PCI Device Driver to register, we're the
1512 * first one to get offered PCI devices as they become
1513 * available (and thus we can be the first to grab them)
1514 */
a92336a1 1515 err = pci_register_driver(&xen_pcibk_pci_driver);
30edc14b
KRW
1516 if (err < 0)
1517 goto out;
1518
a92336a1 1519 err = driver_create_file(&xen_pcibk_pci_driver.driver,
30edc14b
KRW
1520 &driver_attr_new_slot);
1521 if (!err)
a92336a1 1522 err = driver_create_file(&xen_pcibk_pci_driver.driver,
30edc14b
KRW
1523 &driver_attr_remove_slot);
1524 if (!err)
a92336a1 1525 err = driver_create_file(&xen_pcibk_pci_driver.driver,
30edc14b
KRW
1526 &driver_attr_slots);
1527 if (!err)
a92336a1 1528 err = driver_create_file(&xen_pcibk_pci_driver.driver,
30edc14b
KRW
1529 &driver_attr_quirks);
1530 if (!err)
a92336a1 1531 err = driver_create_file(&xen_pcibk_pci_driver.driver,
30edc14b
KRW
1532 &driver_attr_permissive);
1533
0513fe9e 1534 if (!err)
a92336a1 1535 err = driver_create_file(&xen_pcibk_pci_driver.driver,
0513fe9e
KRW
1536 &driver_attr_irq_handlers);
1537 if (!err)
a92336a1 1538 err = driver_create_file(&xen_pcibk_pci_driver.driver,
0513fe9e 1539 &driver_attr_irq_handler_state);
30edc14b
KRW
1540 if (err)
1541 pcistub_exit();
1542
1543out:
1544 return err;
1545
1546parse_error:
283c0972 1547 pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
30edc14b
KRW
1548 pci_devs_to_hide + pos);
1549 return -EINVAL;
1550}
1551
1552#ifndef MODULE
1553/*
1554 * fs_initcall happens before device_initcall
a92336a1 1555 * so xen_pcibk *should* get called first (b/c we
30edc14b
KRW
1556 * want to suck up any device before other drivers
1557 * get a chance by being the first pci device
1558 * driver to register)
1559 */
1560fs_initcall(pcistub_init);
1561#endif
1562
6945c59c
JB
1563#ifdef CONFIG_PCI_IOV
1564static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1565{
1566 struct pcistub_device *psdev = NULL;
1567 unsigned long flags;
1568 bool found = false;
1569
1570 spin_lock_irqsave(&pcistub_devices_lock, flags);
1571 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1572 if (!psdev->pdev && psdev->dev != pdev
1573 && pci_physfn(psdev->dev) == pdev) {
1574 found = true;
1575 break;
1576 }
1577 }
1578 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1579 if (found)
1580 return psdev;
1581 return NULL;
1582}
1583
1584static int pci_stub_notifier(struct notifier_block *nb,
1585 unsigned long action, void *data)
1586{
1587 struct device *dev = data;
1588 const struct pci_dev *pdev = to_pci_dev(dev);
1589
1590 if (action != BUS_NOTIFY_UNBIND_DRIVER)
1591 return NOTIFY_DONE;
1592
1593 if (!pdev->is_physfn)
1594 return NOTIFY_DONE;
1595
1596 for (;;) {
1597 struct pcistub_device *psdev = find_vfs(pdev);
1598 if (!psdev)
1599 break;
1600 device_release_driver(&psdev->dev->dev);
1601 }
1602 return NOTIFY_DONE;
1603}
1604
1605static struct notifier_block pci_stub_nb = {
1606 .notifier_call = pci_stub_notifier,
1607};
1608#endif
1609
a92336a1 1610static int __init xen_pcibk_init(void)
30edc14b
KRW
1611{
1612 int err;
1613
1614 if (!xen_initial_domain())
1615 return -ENODEV;
1616
a92336a1 1617 err = xen_pcibk_config_init();
30edc14b
KRW
1618 if (err)
1619 return err;
1620
1621#ifdef MODULE
1622 err = pcistub_init();
1623 if (err < 0)
1624 return err;
1625#endif
1626
1627 pcistub_init_devices_late();
a92336a1 1628 err = xen_pcibk_xenbus_register();
30edc14b
KRW
1629 if (err)
1630 pcistub_exit();
6945c59c
JB
1631#ifdef CONFIG_PCI_IOV
1632 else
1633 bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1634#endif
30edc14b
KRW
1635
1636 return err;
1637}
1638
a92336a1 1639static void __exit xen_pcibk_cleanup(void)
30edc14b 1640{
6945c59c
JB
1641#ifdef CONFIG_PCI_IOV
1642 bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1643#endif
a92336a1 1644 xen_pcibk_xenbus_unregister();
30edc14b
KRW
1645 pcistub_exit();
1646}
1647
a92336a1
KRW
1648module_init(xen_pcibk_init);
1649module_exit(xen_pcibk_cleanup);
30edc14b
KRW
1650
1651MODULE_LICENSE("Dual BSD/GPL");
402c5e15 1652MODULE_ALIAS("xen-backend:pci");