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