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