]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/xen/xen-pciback/pci_stub.c
xen/pciback: Allocate IRQ handler for device that is shared with guest.
[mv-sheeva.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 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/rwsem.h>
10 #include <linux/list.h>
11 #include <linux/spinlock.h>
12 #include <linux/kref.h>
13 #include <linux/pci.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/atomic.h>
17 #include <xen/events.h>
18 #include <asm/xen/pci.h>
19 #include <asm/xen/hypervisor.h>
20 #include "pciback.h"
21 #include "conf_space.h"
22 #include "conf_space_quirks.h"
23
24 #define DRV_NAME        "pciback"
25
26 static char *pci_devs_to_hide;
27 wait_queue_head_t aer_wait_queue;
28 /*Add sem for sync AER handling and pciback remove/reconfigue ops,
29 * We want to avoid in middle of AER ops, pciback devices is being removed
30 */
31 static DECLARE_RWSEM(pcistub_sem);
32 module_param_named(hide, pci_devs_to_hide, charp, 0444);
33
34 struct pcistub_device_id {
35         struct list_head slot_list;
36         int domain;
37         unsigned char bus;
38         unsigned int devfn;
39 };
40 static LIST_HEAD(pcistub_device_ids);
41 static DEFINE_SPINLOCK(device_ids_lock);
42
43 struct pcistub_device {
44         struct kref kref;
45         struct list_head dev_list;
46         spinlock_t lock;
47
48         struct pci_dev *dev;
49         struct pciback_device *pdev;/* non-NULL if struct pci_dev is in use */
50 };
51
52 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
53  * flag must be locked with pcistub_devices_lock
54  */
55 static DEFINE_SPINLOCK(pcistub_devices_lock);
56 static LIST_HEAD(pcistub_devices);
57
58 /* wait for device_initcall before initializing our devices
59  * (see pcistub_init_devices_late)
60  */
61 static int initialize_devices;
62 static LIST_HEAD(seized_devices);
63
64 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
65 {
66         struct pcistub_device *psdev;
67
68         dev_dbg(&dev->dev, "pcistub_device_alloc\n");
69
70         psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
71         if (!psdev)
72                 return NULL;
73
74         psdev->dev = pci_dev_get(dev);
75         if (!psdev->dev) {
76                 kfree(psdev);
77                 return NULL;
78         }
79
80         kref_init(&psdev->kref);
81         spin_lock_init(&psdev->lock);
82
83         return psdev;
84 }
85
86 /* Don't call this directly as it's called by pcistub_device_put */
87 static void pcistub_device_release(struct kref *kref)
88 {
89         struct pcistub_device *psdev;
90
91         psdev = container_of(kref, struct pcistub_device, kref);
92
93         dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
94
95         xen_unregister_device_domain_owner(psdev->dev);
96
97         /* Clean-up the device */
98         pciback_reset_device(psdev->dev);
99         pciback_config_free_dyn_fields(psdev->dev);
100         pciback_config_free_dev(psdev->dev);
101         kfree(pci_get_drvdata(psdev->dev));
102         pci_set_drvdata(psdev->dev, NULL);
103
104         pci_dev_put(psdev->dev);
105
106         kfree(psdev);
107 }
108
109 static inline void pcistub_device_get(struct pcistub_device *psdev)
110 {
111         kref_get(&psdev->kref);
112 }
113
114 static inline void pcistub_device_put(struct pcistub_device *psdev)
115 {
116         kref_put(&psdev->kref, pcistub_device_release);
117 }
118
119 static struct pcistub_device *pcistub_device_find(int domain, int bus,
120                                                   int slot, int func)
121 {
122         struct pcistub_device *psdev = NULL;
123         unsigned long flags;
124
125         spin_lock_irqsave(&pcistub_devices_lock, flags);
126
127         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
128                 if (psdev->dev != NULL
129                     && domain == pci_domain_nr(psdev->dev->bus)
130                     && bus == psdev->dev->bus->number
131                     && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
132                         pcistub_device_get(psdev);
133                         goto out;
134                 }
135         }
136
137         /* didn't find it */
138         psdev = NULL;
139
140 out:
141         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
142         return psdev;
143 }
144
145 static struct pci_dev *pcistub_device_get_pci_dev(struct pciback_device *pdev,
146                                                   struct pcistub_device *psdev)
147 {
148         struct pci_dev *pci_dev = NULL;
149         unsigned long flags;
150
151         pcistub_device_get(psdev);
152
153         spin_lock_irqsave(&psdev->lock, flags);
154         if (!psdev->pdev) {
155                 psdev->pdev = pdev;
156                 pci_dev = psdev->dev;
157         }
158         spin_unlock_irqrestore(&psdev->lock, flags);
159
160         if (!pci_dev)
161                 pcistub_device_put(psdev);
162
163         return pci_dev;
164 }
165
166 struct pci_dev *pcistub_get_pci_dev_by_slot(struct pciback_device *pdev,
167                                             int domain, int bus,
168                                             int slot, int func)
169 {
170         struct pcistub_device *psdev;
171         struct pci_dev *found_dev = NULL;
172         unsigned long flags;
173
174         spin_lock_irqsave(&pcistub_devices_lock, flags);
175
176         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
177                 if (psdev->dev != NULL
178                     && domain == pci_domain_nr(psdev->dev->bus)
179                     && bus == psdev->dev->bus->number
180                     && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
181                         found_dev = pcistub_device_get_pci_dev(pdev, psdev);
182                         break;
183                 }
184         }
185
186         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
187         return found_dev;
188 }
189
190 struct pci_dev *pcistub_get_pci_dev(struct pciback_device *pdev,
191                                     struct pci_dev *dev)
192 {
193         struct pcistub_device *psdev;
194         struct pci_dev *found_dev = NULL;
195         unsigned long flags;
196
197         spin_lock_irqsave(&pcistub_devices_lock, flags);
198
199         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
200                 if (psdev->dev == dev) {
201                         found_dev = pcistub_device_get_pci_dev(pdev, psdev);
202                         break;
203                 }
204         }
205
206         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
207         return found_dev;
208 }
209
210 void pcistub_put_pci_dev(struct pci_dev *dev)
211 {
212         struct pcistub_device *psdev, *found_psdev = NULL;
213         unsigned long flags;
214
215         spin_lock_irqsave(&pcistub_devices_lock, flags);
216
217         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
218                 if (psdev->dev == dev) {
219                         found_psdev = psdev;
220                         break;
221                 }
222         }
223
224         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225
226         /*hold this lock for avoiding breaking link between
227         * pcistub and pciback when AER is in processing
228         */
229         down_write(&pcistub_sem);
230         /* Cleanup our device
231          * (so it's ready for the next domain)
232          */
233         pciback_reset_device(found_psdev->dev);
234         pciback_config_free_dyn_fields(found_psdev->dev);
235         pciback_config_reset_dev(found_psdev->dev);
236
237         spin_lock_irqsave(&found_psdev->lock, flags);
238         found_psdev->pdev = NULL;
239         spin_unlock_irqrestore(&found_psdev->lock, flags);
240
241         pcistub_device_put(found_psdev);
242         up_write(&pcistub_sem);
243 }
244
245 static int __devinit pcistub_match_one(struct pci_dev *dev,
246                                        struct pcistub_device_id *pdev_id)
247 {
248         /* Match the specified device by domain, bus, slot, func and also if
249          * any of the device's parent bridges match.
250          */
251         for (; dev != NULL; dev = dev->bus->self) {
252                 if (pci_domain_nr(dev->bus) == pdev_id->domain
253                     && dev->bus->number == pdev_id->bus
254                     && dev->devfn == pdev_id->devfn)
255                         return 1;
256
257                 /* Sometimes topmost bridge links to itself. */
258                 if (dev == dev->bus->self)
259                         break;
260         }
261
262         return 0;
263 }
264
265 static int __devinit pcistub_match(struct pci_dev *dev)
266 {
267         struct pcistub_device_id *pdev_id;
268         unsigned long flags;
269         int found = 0;
270
271         spin_lock_irqsave(&device_ids_lock, flags);
272         list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
273                 if (pcistub_match_one(dev, pdev_id)) {
274                         found = 1;
275                         break;
276                 }
277         }
278         spin_unlock_irqrestore(&device_ids_lock, flags);
279
280         return found;
281 }
282
283 static int __devinit pcistub_init_device(struct pci_dev *dev)
284 {
285         struct pciback_dev_data *dev_data;
286         int err = 0;
287
288         dev_dbg(&dev->dev, "initializing...\n");
289
290         /* The PCI backend is not intended to be a module (or to work with
291          * removable PCI devices (yet). If it were, pciback_config_free()
292          * would need to be called somewhere to free the memory allocated
293          * here and then to call kfree(pci_get_drvdata(psdev->dev)).
294          */
295         dev_data = kzalloc(sizeof(*dev_data) +  strlen(DRV_NAME "[]")
296                                 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
297         if (!dev_data) {
298                 err = -ENOMEM;
299                 goto out;
300         }
301         pci_set_drvdata(dev, dev_data);
302
303         /*
304          * Setup name for fake IRQ handler. It will only be enabled
305          * once the device is turned on by the guest.
306          */
307         sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
308
309         dev_dbg(&dev->dev, "initializing config\n");
310
311         init_waitqueue_head(&aer_wait_queue);
312         err = pciback_config_init_dev(dev);
313         if (err)
314                 goto out;
315
316         /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
317          * must do this here because pcibios_enable_device may specify
318          * the pci device's true irq (and possibly its other resources)
319          * if they differ from what's in the configuration space.
320          * This makes the assumption that the device's resources won't
321          * change after this point (otherwise this code may break!)
322          */
323         dev_dbg(&dev->dev, "enabling device\n");
324         err = pci_enable_device(dev);
325         if (err)
326                 goto config_release;
327
328         /* Now disable the device (this also ensures some private device
329          * data is setup before we export)
330          */
331         dev_dbg(&dev->dev, "reset device\n");
332         pciback_reset_device(dev);
333
334         return 0;
335
336 config_release:
337         pciback_config_free_dev(dev);
338
339 out:
340         pci_set_drvdata(dev, NULL);
341         kfree(dev_data);
342         return err;
343 }
344
345 /*
346  * Because some initialization still happens on
347  * devices during fs_initcall, we need to defer
348  * full initialization of our devices until
349  * device_initcall.
350  */
351 static int __init pcistub_init_devices_late(void)
352 {
353         struct pcistub_device *psdev;
354         unsigned long flags;
355         int err = 0;
356
357         pr_debug("pciback: pcistub_init_devices_late\n");
358
359         spin_lock_irqsave(&pcistub_devices_lock, flags);
360
361         while (!list_empty(&seized_devices)) {
362                 psdev = container_of(seized_devices.next,
363                                      struct pcistub_device, dev_list);
364                 list_del(&psdev->dev_list);
365
366                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
367
368                 err = pcistub_init_device(psdev->dev);
369                 if (err) {
370                         dev_err(&psdev->dev->dev,
371                                 "error %d initializing device\n", err);
372                         kfree(psdev);
373                         psdev = NULL;
374                 }
375
376                 spin_lock_irqsave(&pcistub_devices_lock, flags);
377
378                 if (psdev)
379                         list_add_tail(&psdev->dev_list, &pcistub_devices);
380         }
381
382         initialize_devices = 1;
383
384         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
385
386         return 0;
387 }
388
389 static int __devinit pcistub_seize(struct pci_dev *dev)
390 {
391         struct pcistub_device *psdev;
392         unsigned long flags;
393         int err = 0;
394
395         psdev = pcistub_device_alloc(dev);
396         if (!psdev)
397                 return -ENOMEM;
398
399         spin_lock_irqsave(&pcistub_devices_lock, flags);
400
401         if (initialize_devices) {
402                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
403
404                 /* don't want irqs disabled when calling pcistub_init_device */
405                 err = pcistub_init_device(psdev->dev);
406
407                 spin_lock_irqsave(&pcistub_devices_lock, flags);
408
409                 if (!err)
410                         list_add(&psdev->dev_list, &pcistub_devices);
411         } else {
412                 dev_dbg(&dev->dev, "deferring initialization\n");
413                 list_add(&psdev->dev_list, &seized_devices);
414         }
415
416         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
417
418         if (err)
419                 pcistub_device_put(psdev);
420
421         return err;
422 }
423
424 static int __devinit pcistub_probe(struct pci_dev *dev,
425                                    const struct pci_device_id *id)
426 {
427         int err = 0;
428
429         dev_dbg(&dev->dev, "probing...\n");
430
431         if (pcistub_match(dev)) {
432
433                 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
434                     && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
435                         dev_err(&dev->dev, "can't export pci devices that "
436                                 "don't have a normal (0) or bridge (1) "
437                                 "header type!\n");
438                         err = -ENODEV;
439                         goto out;
440                 }
441
442                 dev_info(&dev->dev, "seizing device\n");
443                 err = pcistub_seize(dev);
444         } else
445                 /* Didn't find the device */
446                 err = -ENODEV;
447
448 out:
449         return err;
450 }
451
452 static void pcistub_remove(struct pci_dev *dev)
453 {
454         struct pcistub_device *psdev, *found_psdev = NULL;
455         unsigned long flags;
456
457         dev_dbg(&dev->dev, "removing\n");
458
459         spin_lock_irqsave(&pcistub_devices_lock, flags);
460
461         pciback_config_quirk_release(dev);
462
463         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
464                 if (psdev->dev == dev) {
465                         found_psdev = psdev;
466                         break;
467                 }
468         }
469
470         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
471
472         if (found_psdev) {
473                 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
474                         found_psdev->pdev);
475
476                 if (found_psdev->pdev) {
477                         printk(KERN_WARNING "pciback: ****** removing device "
478                                "%s while still in-use! ******\n",
479                                pci_name(found_psdev->dev));
480                         printk(KERN_WARNING "pciback: ****** driver domain may "
481                                "still access this device's i/o resources!\n");
482                         printk(KERN_WARNING "pciback: ****** shutdown driver "
483                                "domain before binding device\n");
484                         printk(KERN_WARNING "pciback: ****** to other drivers "
485                                "or domains\n");
486
487                         pciback_release_pci_dev(found_psdev->pdev,
488                                                 found_psdev->dev);
489                 }
490
491                 spin_lock_irqsave(&pcistub_devices_lock, flags);
492                 list_del(&found_psdev->dev_list);
493                 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
494
495                 /* the final put for releasing from the list */
496                 pcistub_device_put(found_psdev);
497         }
498 }
499
500 static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
501         {
502          .vendor = PCI_ANY_ID,
503          .device = PCI_ANY_ID,
504          .subvendor = PCI_ANY_ID,
505          .subdevice = PCI_ANY_ID,
506          },
507         {0,},
508 };
509
510 #define PCI_NODENAME_MAX 40
511 static void kill_domain_by_device(struct pcistub_device *psdev)
512 {
513         struct xenbus_transaction xbt;
514         int err;
515         char nodename[PCI_NODENAME_MAX];
516
517         if (!psdev)
518                 dev_err(&psdev->dev->dev,
519                         "device is NULL when do AER recovery/kill_domain\n");
520         snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
521                 psdev->pdev->xdev->otherend_id);
522         nodename[strlen(nodename)] = '\0';
523
524 again:
525         err = xenbus_transaction_start(&xbt);
526         if (err) {
527                 dev_err(&psdev->dev->dev,
528                         "error %d when start xenbus transaction\n", err);
529                 return;
530         }
531         /*PV AER handlers will set this flag*/
532         xenbus_printf(xbt, nodename, "aerState" , "aerfail");
533         err = xenbus_transaction_end(xbt, 0);
534         if (err) {
535                 if (err == -EAGAIN)
536                         goto again;
537                 dev_err(&psdev->dev->dev,
538                         "error %d when end xenbus transaction\n", err);
539                 return;
540         }
541 }
542
543 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
544  * backend need to have cooperation. In pciback, those steps will do similar
545  * jobs: send service request and waiting for front_end response.
546 */
547 static pci_ers_result_t common_process(struct pcistub_device *psdev,
548                 pci_channel_state_t state, int aer_cmd, pci_ers_result_t result)
549 {
550         pci_ers_result_t res = result;
551         struct xen_pcie_aer_op *aer_op;
552         int ret;
553
554         /*with PV AER drivers*/
555         aer_op = &(psdev->pdev->sh_info->aer_op);
556         aer_op->cmd = aer_cmd ;
557         /*useful for error_detected callback*/
558         aer_op->err = state;
559         /*pcifront_end BDF*/
560         ret = pciback_get_pcifront_dev(psdev->dev, psdev->pdev,
561                 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
562         if (!ret) {
563                 dev_err(&psdev->dev->dev,
564                         "pciback: failed to get pcifront device\n");
565                 return PCI_ERS_RESULT_NONE;
566         }
567         wmb();
568
569         dev_dbg(&psdev->dev->dev,
570                         "pciback: aer_op %x dom %x bus %x devfn %x\n",
571                         aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
572         /*local flag to mark there's aer request, pciback callback will use this
573         * flag to judge whether we need to check pci-front give aer service
574         * ack signal
575         */
576         set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
577
578         /*It is possible that a pcifront conf_read_write ops request invokes
579         * the callback which cause the spurious execution of wake_up.
580         * Yet it is harmless and better than a spinlock here
581         */
582         set_bit(_XEN_PCIB_active,
583                 (unsigned long *)&psdev->pdev->sh_info->flags);
584         wmb();
585         notify_remote_via_irq(psdev->pdev->evtchn_irq);
586
587         ret = wait_event_timeout(aer_wait_queue, !(test_bit(_XEN_PCIB_active,
588                 (unsigned long *)&psdev->pdev->sh_info->flags)), 300*HZ);
589
590         if (!ret) {
591                 if (test_bit(_XEN_PCIB_active,
592                         (unsigned long *)&psdev->pdev->sh_info->flags)) {
593                         dev_err(&psdev->dev->dev,
594                                 "pcifront aer process not responding!\n");
595                         clear_bit(_XEN_PCIB_active,
596                           (unsigned long *)&psdev->pdev->sh_info->flags);
597                         aer_op->err = PCI_ERS_RESULT_NONE;
598                         return res;
599                 }
600         }
601         clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
602
603         if (test_bit(_XEN_PCIF_active,
604                 (unsigned long *)&psdev->pdev->sh_info->flags)) {
605                 dev_dbg(&psdev->dev->dev,
606                         "schedule pci_conf service in pciback\n");
607                 test_and_schedule_op(psdev->pdev);
608         }
609
610         res = (pci_ers_result_t)aer_op->err;
611         return res;
612 }
613
614 /*
615 * pciback_slot_reset: it will send the slot_reset request to  pcifront in case
616 * of the device driver could provide this service, and then wait for pcifront
617 * ack.
618 * @dev: pointer to PCI devices
619 * return value is used by aer_core do_recovery policy
620 */
621 static pci_ers_result_t pciback_slot_reset(struct pci_dev *dev)
622 {
623         struct pcistub_device *psdev;
624         pci_ers_result_t result;
625
626         result = PCI_ERS_RESULT_RECOVERED;
627         dev_dbg(&dev->dev, "pciback_slot_reset(bus:%x,devfn:%x)\n",
628                 dev->bus->number, dev->devfn);
629
630         down_write(&pcistub_sem);
631         psdev = pcistub_device_find(pci_domain_nr(dev->bus),
632                                 dev->bus->number,
633                                 PCI_SLOT(dev->devfn),
634                                 PCI_FUNC(dev->devfn));
635
636         if (!psdev || !psdev->pdev) {
637                 dev_err(&dev->dev,
638                         "pciback device is not found/assigned\n");
639                 goto end;
640         }
641
642         if (!psdev->pdev->sh_info) {
643                 dev_err(&dev->dev, "pciback device is not connected or owned"
644                         " by HVM, kill it\n");
645                 kill_domain_by_device(psdev);
646                 goto release;
647         }
648
649         if (!test_bit(_XEN_PCIB_AERHANDLER,
650                 (unsigned long *)&psdev->pdev->sh_info->flags)) {
651                 dev_err(&dev->dev,
652                         "guest with no AER driver should have been killed\n");
653                 goto release;
654         }
655         result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
656
657         if (result == PCI_ERS_RESULT_NONE ||
658                 result == PCI_ERS_RESULT_DISCONNECT) {
659                 dev_dbg(&dev->dev,
660                         "No AER slot_reset service or disconnected!\n");
661                 kill_domain_by_device(psdev);
662         }
663 release:
664         pcistub_device_put(psdev);
665 end:
666         up_write(&pcistub_sem);
667         return result;
668
669 }
670
671
672 /*pciback_mmio_enabled: it will send the mmio_enabled request to  pcifront
673 * in case of the device driver could provide this service, and then wait
674 * for pcifront ack
675 * @dev: pointer to PCI devices
676 * return value is used by aer_core do_recovery policy
677 */
678
679 static pci_ers_result_t pciback_mmio_enabled(struct pci_dev *dev)
680 {
681         struct pcistub_device *psdev;
682         pci_ers_result_t result;
683
684         result = PCI_ERS_RESULT_RECOVERED;
685         dev_dbg(&dev->dev, "pciback_mmio_enabled(bus:%x,devfn:%x)\n",
686                 dev->bus->number, dev->devfn);
687
688         down_write(&pcistub_sem);
689         psdev = pcistub_device_find(pci_domain_nr(dev->bus),
690                                 dev->bus->number,
691                                 PCI_SLOT(dev->devfn),
692                                 PCI_FUNC(dev->devfn));
693
694         if (!psdev || !psdev->pdev) {
695                 dev_err(&dev->dev,
696                         "pciback device is not found/assigned\n");
697                 goto end;
698         }
699
700         if (!psdev->pdev->sh_info) {
701                 dev_err(&dev->dev, "pciback device is not connected or owned"
702                         " by HVM, kill it\n");
703                 kill_domain_by_device(psdev);
704                 goto release;
705         }
706
707         if (!test_bit(_XEN_PCIB_AERHANDLER,
708                 (unsigned long *)&psdev->pdev->sh_info->flags)) {
709                 dev_err(&dev->dev,
710                         "guest with no AER driver should have been killed\n");
711                 goto release;
712         }
713         result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
714
715         if (result == PCI_ERS_RESULT_NONE ||
716                 result == PCI_ERS_RESULT_DISCONNECT) {
717                 dev_dbg(&dev->dev,
718                         "No AER mmio_enabled service or disconnected!\n");
719                 kill_domain_by_device(psdev);
720         }
721 release:
722         pcistub_device_put(psdev);
723 end:
724         up_write(&pcistub_sem);
725         return result;
726 }
727
728 /*pciback_error_detected: it will send the error_detected request to  pcifront
729 * in case of the device driver could provide this service, and then wait
730 * for pcifront ack.
731 * @dev: pointer to PCI devices
732 * @error: the current PCI connection state
733 * return value is used by aer_core do_recovery policy
734 */
735
736 static pci_ers_result_t pciback_error_detected(struct pci_dev *dev,
737         pci_channel_state_t error)
738 {
739         struct pcistub_device *psdev;
740         pci_ers_result_t result;
741
742         result = PCI_ERS_RESULT_CAN_RECOVER;
743         dev_dbg(&dev->dev, "pciback_error_detected(bus:%x,devfn:%x)\n",
744                 dev->bus->number, dev->devfn);
745
746         down_write(&pcistub_sem);
747         psdev = pcistub_device_find(pci_domain_nr(dev->bus),
748                                 dev->bus->number,
749                                 PCI_SLOT(dev->devfn),
750                                 PCI_FUNC(dev->devfn));
751
752         if (!psdev || !psdev->pdev) {
753                 dev_err(&dev->dev,
754                         "pciback device is not found/assigned\n");
755                 goto end;
756         }
757
758         if (!psdev->pdev->sh_info) {
759                 dev_err(&dev->dev, "pciback device is not connected or owned"
760                         " by HVM, kill it\n");
761                 kill_domain_by_device(psdev);
762                 goto release;
763         }
764
765         /*Guest owns the device yet no aer handler regiested, kill guest*/
766         if (!test_bit(_XEN_PCIB_AERHANDLER,
767                 (unsigned long *)&psdev->pdev->sh_info->flags)) {
768                 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
769                 kill_domain_by_device(psdev);
770                 goto release;
771         }
772         result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
773
774         if (result == PCI_ERS_RESULT_NONE ||
775                 result == PCI_ERS_RESULT_DISCONNECT) {
776                 dev_dbg(&dev->dev,
777                         "No AER error_detected service or disconnected!\n");
778                 kill_domain_by_device(psdev);
779         }
780 release:
781         pcistub_device_put(psdev);
782 end:
783         up_write(&pcistub_sem);
784         return result;
785 }
786
787 /*pciback_error_resume: it will send the error_resume request to  pcifront
788 * in case of the device driver could provide this service, and then wait
789 * for pcifront ack.
790 * @dev: pointer to PCI devices
791 */
792
793 static void pciback_error_resume(struct pci_dev *dev)
794 {
795         struct pcistub_device *psdev;
796
797         dev_dbg(&dev->dev, "pciback_error_resume(bus:%x,devfn:%x)\n",
798                 dev->bus->number, dev->devfn);
799
800         down_write(&pcistub_sem);
801         psdev = pcistub_device_find(pci_domain_nr(dev->bus),
802                                 dev->bus->number,
803                                 PCI_SLOT(dev->devfn),
804                                 PCI_FUNC(dev->devfn));
805
806         if (!psdev || !psdev->pdev) {
807                 dev_err(&dev->dev,
808                         "pciback device is not found/assigned\n");
809                 goto end;
810         }
811
812         if (!psdev->pdev->sh_info) {
813                 dev_err(&dev->dev, "pciback device is not connected or owned"
814                         " by HVM, kill it\n");
815                 kill_domain_by_device(psdev);
816                 goto release;
817         }
818
819         if (!test_bit(_XEN_PCIB_AERHANDLER,
820                 (unsigned long *)&psdev->pdev->sh_info->flags)) {
821                 dev_err(&dev->dev,
822                         "guest with no AER driver should have been killed\n");
823                 kill_domain_by_device(psdev);
824                 goto release;
825         }
826         common_process(psdev, 1, XEN_PCI_OP_aer_resume,
827                        PCI_ERS_RESULT_RECOVERED);
828 release:
829         pcistub_device_put(psdev);
830 end:
831         up_write(&pcistub_sem);
832         return;
833 }
834
835 /*add pciback AER handling*/
836 static struct pci_error_handlers pciback_error_handler = {
837         .error_detected = pciback_error_detected,
838         .mmio_enabled = pciback_mmio_enabled,
839         .slot_reset = pciback_slot_reset,
840         .resume = pciback_error_resume,
841 };
842
843 /*
844  * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
845  * for a normal device. I don't want it to be loaded automatically.
846  */
847
848 static struct pci_driver pciback_pci_driver = {
849         .name = DRV_NAME,
850         .id_table = pcistub_ids,
851         .probe = pcistub_probe,
852         .remove = pcistub_remove,
853         .err_handler = &pciback_error_handler,
854 };
855
856 static inline int str_to_slot(const char *buf, int *domain, int *bus,
857                               int *slot, int *func)
858 {
859         int err;
860
861         err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
862         if (err == 4)
863                 return 0;
864         else if (err < 0)
865                 return -EINVAL;
866
867         /* try again without domain */
868         *domain = 0;
869         err = sscanf(buf, " %x:%x.%x", bus, slot, func);
870         if (err == 3)
871                 return 0;
872
873         return -EINVAL;
874 }
875
876 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
877                                *slot, int *func, int *reg, int *size, int *mask)
878 {
879         int err;
880
881         err =
882             sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
883                    func, reg, size, mask);
884         if (err == 7)
885                 return 0;
886         return -EINVAL;
887 }
888
889 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
890 {
891         struct pcistub_device_id *pci_dev_id;
892         unsigned long flags;
893
894         pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
895         if (!pci_dev_id)
896                 return -ENOMEM;
897
898         pci_dev_id->domain = domain;
899         pci_dev_id->bus = bus;
900         pci_dev_id->devfn = PCI_DEVFN(slot, func);
901
902         pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
903                  domain, bus, slot, func);
904
905         spin_lock_irqsave(&device_ids_lock, flags);
906         list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
907         spin_unlock_irqrestore(&device_ids_lock, flags);
908
909         return 0;
910 }
911
912 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
913 {
914         struct pcistub_device_id *pci_dev_id, *t;
915         int devfn = PCI_DEVFN(slot, func);
916         int err = -ENOENT;
917         unsigned long flags;
918
919         spin_lock_irqsave(&device_ids_lock, flags);
920         list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
921                                  slot_list) {
922                 if (pci_dev_id->domain == domain
923                     && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
924                         /* Don't break; here because it's possible the same
925                          * slot could be in the list more than once
926                          */
927                         list_del(&pci_dev_id->slot_list);
928                         kfree(pci_dev_id);
929
930                         err = 0;
931
932                         pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
933                                  "seize list\n", domain, bus, slot, func);
934                 }
935         }
936         spin_unlock_irqrestore(&device_ids_lock, flags);
937
938         return err;
939 }
940
941 static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
942                            int size, int mask)
943 {
944         int err = 0;
945         struct pcistub_device *psdev;
946         struct pci_dev *dev;
947         struct config_field *field;
948
949         psdev = pcistub_device_find(domain, bus, slot, func);
950         if (!psdev || !psdev->dev) {
951                 err = -ENODEV;
952                 goto out;
953         }
954         dev = psdev->dev;
955
956         field = kzalloc(sizeof(*field), GFP_ATOMIC);
957         if (!field) {
958                 err = -ENOMEM;
959                 goto out;
960         }
961
962         field->offset = reg;
963         field->size = size;
964         field->mask = mask;
965         field->init = NULL;
966         field->reset = NULL;
967         field->release = NULL;
968         field->clean = pciback_config_field_free;
969
970         err = pciback_config_quirks_add_field(dev, field);
971         if (err)
972                 kfree(field);
973 out:
974         return err;
975 }
976
977 static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
978                                 size_t count)
979 {
980         int domain, bus, slot, func;
981         int err;
982
983         err = str_to_slot(buf, &domain, &bus, &slot, &func);
984         if (err)
985                 goto out;
986
987         err = pcistub_device_id_add(domain, bus, slot, func);
988
989 out:
990         if (!err)
991                 err = count;
992         return err;
993 }
994
995 DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
996
997 static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
998                                    size_t count)
999 {
1000         int domain, bus, slot, func;
1001         int err;
1002
1003         err = str_to_slot(buf, &domain, &bus, &slot, &func);
1004         if (err)
1005                 goto out;
1006
1007         err = pcistub_device_id_remove(domain, bus, slot, func);
1008
1009 out:
1010         if (!err)
1011                 err = count;
1012         return err;
1013 }
1014
1015 DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
1016
1017 static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1018 {
1019         struct pcistub_device_id *pci_dev_id;
1020         size_t count = 0;
1021         unsigned long flags;
1022
1023         spin_lock_irqsave(&device_ids_lock, flags);
1024         list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1025                 if (count >= PAGE_SIZE)
1026                         break;
1027
1028                 count += scnprintf(buf + count, PAGE_SIZE - count,
1029                                    "%04x:%02x:%02x.%01x\n",
1030                                    pci_dev_id->domain, pci_dev_id->bus,
1031                                    PCI_SLOT(pci_dev_id->devfn),
1032                                    PCI_FUNC(pci_dev_id->devfn));
1033         }
1034         spin_unlock_irqrestore(&device_ids_lock, flags);
1035
1036         return count;
1037 }
1038
1039 DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
1040
1041 static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1042 {
1043         struct pcistub_device *psdev;
1044         struct pciback_dev_data *dev_data;
1045         size_t count = 0;
1046         unsigned long flags;
1047
1048         spin_lock_irqsave(&pcistub_devices_lock, flags);
1049         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1050                 if (count >= PAGE_SIZE)
1051                         break;
1052                 if (!psdev->dev)
1053                         continue;
1054                 dev_data = pci_get_drvdata(psdev->dev);
1055                 if (!dev_data)
1056                         continue;
1057                 count +=
1058                     scnprintf(buf + count, PAGE_SIZE - count,
1059                               "%s:%s:%sing:%ld\n",
1060                               pci_name(psdev->dev),
1061                               dev_data->isr_on ? "on" : "off",
1062                               dev_data->ack_intr ? "ack" : "not ack",
1063                               dev_data->handled);
1064         }
1065         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1066         return count;
1067 }
1068
1069 DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
1070
1071 static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1072                                           const char *buf,
1073                                           size_t count)
1074 {
1075         struct pcistub_device *psdev;
1076         struct pciback_dev_data *dev_data;
1077         int domain, bus, slot, func;
1078         int err = -ENOENT;
1079
1080         err = str_to_slot(buf, &domain, &bus, &slot, &func);
1081         if (err)
1082                 goto out;
1083
1084         psdev = pcistub_device_find(domain, bus, slot, func);
1085
1086         if (!psdev)
1087                 goto out;
1088
1089         dev_data = pci_get_drvdata(psdev->dev);
1090         if (!dev_data)
1091                 goto out;
1092
1093         dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1094                 dev_data->irq_name, dev_data->isr_on,
1095                 !dev_data->isr_on);
1096
1097         dev_data->isr_on = !(dev_data->isr_on);
1098         if (dev_data->isr_on)
1099                 dev_data->ack_intr = 1;
1100 out:
1101         if (!err)
1102                 err = count;
1103         return err;
1104 }
1105 DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL, pcistub_irq_handler_switch);
1106
1107 static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1108                                  size_t count)
1109 {
1110         int domain, bus, slot, func, reg, size, mask;
1111         int err;
1112
1113         err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1114                            &mask);
1115         if (err)
1116                 goto out;
1117
1118         err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1119
1120 out:
1121         if (!err)
1122                 err = count;
1123         return err;
1124 }
1125
1126 static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1127 {
1128         int count = 0;
1129         unsigned long flags;
1130         struct pciback_config_quirk *quirk;
1131         struct pciback_dev_data *dev_data;
1132         const struct config_field *field;
1133         const struct config_field_entry *cfg_entry;
1134
1135         spin_lock_irqsave(&device_ids_lock, flags);
1136         list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
1137                 if (count >= PAGE_SIZE)
1138                         goto out;
1139
1140                 count += scnprintf(buf + count, PAGE_SIZE - count,
1141                                    "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1142                                    quirk->pdev->bus->number,
1143                                    PCI_SLOT(quirk->pdev->devfn),
1144                                    PCI_FUNC(quirk->pdev->devfn),
1145                                    quirk->devid.vendor, quirk->devid.device,
1146                                    quirk->devid.subvendor,
1147                                    quirk->devid.subdevice);
1148
1149                 dev_data = pci_get_drvdata(quirk->pdev);
1150
1151                 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1152                         field = cfg_entry->field;
1153                         if (count >= PAGE_SIZE)
1154                                 goto out;
1155
1156                         count += scnprintf(buf + count, PAGE_SIZE - count,
1157                                            "\t\t%08x:%01x:%08x\n",
1158                                            cfg_entry->base_offset +
1159                                            field->offset, field->size,
1160                                            field->mask);
1161                 }
1162         }
1163
1164 out:
1165         spin_unlock_irqrestore(&device_ids_lock, flags);
1166
1167         return count;
1168 }
1169
1170 DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
1171
1172 static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1173                               size_t count)
1174 {
1175         int domain, bus, slot, func;
1176         int err;
1177         struct pcistub_device *psdev;
1178         struct pciback_dev_data *dev_data;
1179         err = str_to_slot(buf, &domain, &bus, &slot, &func);
1180         if (err)
1181                 goto out;
1182         psdev = pcistub_device_find(domain, bus, slot, func);
1183         if (!psdev) {
1184                 err = -ENODEV;
1185                 goto out;
1186         }
1187         if (!psdev->dev) {
1188                 err = -ENODEV;
1189                 goto release;
1190         }
1191         dev_data = pci_get_drvdata(psdev->dev);
1192         /* the driver data for a device should never be null at this point */
1193         if (!dev_data) {
1194                 err = -ENXIO;
1195                 goto release;
1196         }
1197         if (!dev_data->permissive) {
1198                 dev_data->permissive = 1;
1199                 /* Let user know that what they're doing could be unsafe */
1200                 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1201                          "configuration space accesses!\n");
1202                 dev_warn(&psdev->dev->dev,
1203                          "permissive mode is potentially unsafe!\n");
1204         }
1205 release:
1206         pcistub_device_put(psdev);
1207 out:
1208         if (!err)
1209                 err = count;
1210         return err;
1211 }
1212
1213 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1214 {
1215         struct pcistub_device *psdev;
1216         struct pciback_dev_data *dev_data;
1217         size_t count = 0;
1218         unsigned long flags;
1219         spin_lock_irqsave(&pcistub_devices_lock, flags);
1220         list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1221                 if (count >= PAGE_SIZE)
1222                         break;
1223                 if (!psdev->dev)
1224                         continue;
1225                 dev_data = pci_get_drvdata(psdev->dev);
1226                 if (!dev_data || !dev_data->permissive)
1227                         continue;
1228                 count +=
1229                     scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1230                               pci_name(psdev->dev));
1231         }
1232         spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1233         return count;
1234 }
1235
1236 DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
1237
1238 static void pcistub_exit(void)
1239 {
1240         driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
1241         driver_remove_file(&pciback_pci_driver.driver,
1242                            &driver_attr_remove_slot);
1243         driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
1244         driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
1245         driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
1246         driver_remove_file(&pciback_pci_driver.driver,
1247                            &driver_attr_irq_handlers);
1248         driver_remove_file(&pciback_pci_driver.driver,
1249                            &driver_attr_irq_handler_state);
1250         pci_unregister_driver(&pciback_pci_driver);
1251 }
1252
1253 static int __init pcistub_init(void)
1254 {
1255         int pos = 0;
1256         int err = 0;
1257         int domain, bus, slot, func;
1258         int parsed;
1259
1260         if (pci_devs_to_hide && *pci_devs_to_hide) {
1261                 do {
1262                         parsed = 0;
1263
1264                         err = sscanf(pci_devs_to_hide + pos,
1265                                      " (%x:%x:%x.%x) %n",
1266                                      &domain, &bus, &slot, &func, &parsed);
1267                         if (err != 4) {
1268                                 domain = 0;
1269                                 err = sscanf(pci_devs_to_hide + pos,
1270                                              " (%x:%x.%x) %n",
1271                                              &bus, &slot, &func, &parsed);
1272                                 if (err != 3)
1273                                         goto parse_error;
1274                         }
1275
1276                         err = pcistub_device_id_add(domain, bus, slot, func);
1277                         if (err)
1278                                 goto out;
1279
1280                         /* if parsed<=0, we've reached the end of the string */
1281                         pos += parsed;
1282                 } while (parsed > 0 && pci_devs_to_hide[pos]);
1283         }
1284
1285         /* If we're the first PCI Device Driver to register, we're the
1286          * first one to get offered PCI devices as they become
1287          * available (and thus we can be the first to grab them)
1288          */
1289         err = pci_register_driver(&pciback_pci_driver);
1290         if (err < 0)
1291                 goto out;
1292
1293         err = driver_create_file(&pciback_pci_driver.driver,
1294                                  &driver_attr_new_slot);
1295         if (!err)
1296                 err = driver_create_file(&pciback_pci_driver.driver,
1297                                          &driver_attr_remove_slot);
1298         if (!err)
1299                 err = driver_create_file(&pciback_pci_driver.driver,
1300                                          &driver_attr_slots);
1301         if (!err)
1302                 err = driver_create_file(&pciback_pci_driver.driver,
1303                                          &driver_attr_quirks);
1304         if (!err)
1305                 err = driver_create_file(&pciback_pci_driver.driver,
1306                                          &driver_attr_permissive);
1307
1308         if (!err)
1309                 err = driver_create_file(&pciback_pci_driver.driver,
1310                                          &driver_attr_irq_handlers);
1311         if (!err)
1312                 err = driver_create_file(&pciback_pci_driver.driver,
1313                                         &driver_attr_irq_handler_state);
1314         if (err)
1315                 pcistub_exit();
1316
1317 out:
1318         return err;
1319
1320 parse_error:
1321         printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
1322                pci_devs_to_hide + pos);
1323         return -EINVAL;
1324 }
1325
1326 #ifndef MODULE
1327 /*
1328  * fs_initcall happens before device_initcall
1329  * so pciback *should* get called first (b/c we
1330  * want to suck up any device before other drivers
1331  * get a chance by being the first pci device
1332  * driver to register)
1333  */
1334 fs_initcall(pcistub_init);
1335 #endif
1336
1337 static int __init pciback_init(void)
1338 {
1339         int err;
1340
1341         if (!xen_initial_domain())
1342                 return -ENODEV;
1343
1344         err = pciback_config_init();
1345         if (err)
1346                 return err;
1347
1348 #ifdef MODULE
1349         err = pcistub_init();
1350         if (err < 0)
1351                 return err;
1352 #endif
1353
1354         pcistub_init_devices_late();
1355         err = pciback_xenbus_register();
1356         if (err)
1357                 pcistub_exit();
1358
1359         return err;
1360 }
1361
1362 static void __exit pciback_cleanup(void)
1363 {
1364         pciback_xenbus_unregister();
1365         pcistub_exit();
1366 }
1367
1368 module_init(pciback_init);
1369 module_exit(pciback_cleanup);
1370
1371 MODULE_LICENSE("Dual BSD/GPL");