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