]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pci/xen-pcifront.c
Merge remote-tracking branch 'sound/for-next'
[karo-tx-linux.git] / drivers / pci / xen-pcifront.c
1 /*
2  * Xen PCI Frontend.
3  *
4  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5  */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <xen/xenbus.h>
10 #include <xen/events.h>
11 #include <xen/grant_table.h>
12 #include <xen/page.h>
13 #include <linux/spinlock.h>
14 #include <linux/pci.h>
15 #include <linux/msi.h>
16 #include <xen/interface/io/pciif.h>
17 #include <asm/xen/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/atomic.h>
20 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/time.h>
23
24 #include <asm/xen/swiotlb-xen.h>
25 #define INVALID_GRANT_REF (0)
26 #define INVALID_EVTCHN    (-1)
27
28 struct pci_bus_entry {
29         struct list_head list;
30         struct pci_bus *bus;
31 };
32
33 #define _PDEVB_op_active                (0)
34 #define PDEVB_op_active                 (1 << (_PDEVB_op_active))
35
36 struct pcifront_device {
37         struct xenbus_device *xdev;
38         struct list_head root_buses;
39
40         int evtchn;
41         int gnt_ref;
42
43         int irq;
44
45         /* Lock this when doing any operations in sh_info */
46         spinlock_t sh_info_lock;
47         struct xen_pci_sharedinfo *sh_info;
48         struct work_struct op_work;
49         unsigned long flags;
50
51 };
52
53 struct pcifront_sd {
54         int domain;
55         struct pcifront_device *pdev;
56 };
57
58 static inline struct pcifront_device *
59 pcifront_get_pdev(struct pcifront_sd *sd)
60 {
61         return sd->pdev;
62 }
63
64 static inline void pcifront_init_sd(struct pcifront_sd *sd,
65                                     unsigned int domain, unsigned int bus,
66                                     struct pcifront_device *pdev)
67 {
68         sd->domain = domain;
69         sd->pdev = pdev;
70 }
71
72 static DEFINE_SPINLOCK(pcifront_dev_lock);
73 static struct pcifront_device *pcifront_dev;
74
75 static int verbose_request;
76 module_param(verbose_request, int, 0644);
77
78 static int errno_to_pcibios_err(int errno)
79 {
80         switch (errno) {
81         case XEN_PCI_ERR_success:
82                 return PCIBIOS_SUCCESSFUL;
83
84         case XEN_PCI_ERR_dev_not_found:
85                 return PCIBIOS_DEVICE_NOT_FOUND;
86
87         case XEN_PCI_ERR_invalid_offset:
88         case XEN_PCI_ERR_op_failed:
89                 return PCIBIOS_BAD_REGISTER_NUMBER;
90
91         case XEN_PCI_ERR_not_implemented:
92                 return PCIBIOS_FUNC_NOT_SUPPORTED;
93
94         case XEN_PCI_ERR_access_denied:
95                 return PCIBIOS_SET_FAILED;
96         }
97         return errno;
98 }
99
100 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
101 {
102         if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
103                 && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
104                 dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
105                 schedule_work(&pdev->op_work);
106         }
107 }
108
109 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
110 {
111         int err = 0;
112         struct xen_pci_op *active_op = &pdev->sh_info->op;
113         unsigned long irq_flags;
114         evtchn_port_t port = pdev->evtchn;
115         unsigned irq = pdev->irq;
116         s64 ns, ns_timeout;
117         struct timeval tv;
118
119         spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
120
121         memcpy(active_op, op, sizeof(struct xen_pci_op));
122
123         /* Go */
124         wmb();
125         set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
126         notify_remote_via_evtchn(port);
127
128         /*
129          * We set a poll timeout of 3 seconds but give up on return after
130          * 2 seconds. It is better to time out too late rather than too early
131          * (in the latter case we end up continually re-executing poll() with a
132          * timeout in the past). 1s difference gives plenty of slack for error.
133          */
134         do_gettimeofday(&tv);
135         ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
136
137         xen_clear_irq_pending(irq);
138
139         while (test_bit(_XEN_PCIF_active,
140                         (unsigned long *)&pdev->sh_info->flags)) {
141                 xen_poll_irq_timeout(irq, jiffies + 3*HZ);
142                 xen_clear_irq_pending(irq);
143                 do_gettimeofday(&tv);
144                 ns = timeval_to_ns(&tv);
145                 if (ns > ns_timeout) {
146                         dev_err(&pdev->xdev->dev,
147                                 "pciback not responding!!!\n");
148                         clear_bit(_XEN_PCIF_active,
149                                   (unsigned long *)&pdev->sh_info->flags);
150                         err = XEN_PCI_ERR_dev_not_found;
151                         goto out;
152                 }
153         }
154
155         /*
156         * We might lose backend service request since we
157         * reuse same evtchn with pci_conf backend response. So re-schedule
158         * aer pcifront service.
159         */
160         if (test_bit(_XEN_PCIB_active,
161                         (unsigned long *)&pdev->sh_info->flags)) {
162                 dev_err(&pdev->xdev->dev,
163                         "schedule aer pcifront service\n");
164                 schedule_pcifront_aer_op(pdev);
165         }
166
167         memcpy(op, active_op, sizeof(struct xen_pci_op));
168
169         err = op->err;
170 out:
171         spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
172         return err;
173 }
174
175 /* Access to this function is spinlocked in drivers/pci/access.c */
176 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
177                              int where, int size, u32 *val)
178 {
179         int err = 0;
180         struct xen_pci_op op = {
181                 .cmd    = XEN_PCI_OP_conf_read,
182                 .domain = pci_domain_nr(bus),
183                 .bus    = bus->number,
184                 .devfn  = devfn,
185                 .offset = where,
186                 .size   = size,
187         };
188         struct pcifront_sd *sd = bus->sysdata;
189         struct pcifront_device *pdev = pcifront_get_pdev(sd);
190
191         if (verbose_request)
192                 dev_info(&pdev->xdev->dev,
193                          "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
194                          pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
195                          PCI_FUNC(devfn), where, size);
196
197         err = do_pci_op(pdev, &op);
198
199         if (likely(!err)) {
200                 if (verbose_request)
201                         dev_info(&pdev->xdev->dev, "read got back value %x\n",
202                                  op.value);
203
204                 *val = op.value;
205         } else if (err == -ENODEV) {
206                 /* No device here, pretend that it just returned 0 */
207                 err = 0;
208                 *val = 0;
209         }
210
211         return errno_to_pcibios_err(err);
212 }
213
214 /* Access to this function is spinlocked in drivers/pci/access.c */
215 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
216                               int where, int size, u32 val)
217 {
218         struct xen_pci_op op = {
219                 .cmd    = XEN_PCI_OP_conf_write,
220                 .domain = pci_domain_nr(bus),
221                 .bus    = bus->number,
222                 .devfn  = devfn,
223                 .offset = where,
224                 .size   = size,
225                 .value  = val,
226         };
227         struct pcifront_sd *sd = bus->sysdata;
228         struct pcifront_device *pdev = pcifront_get_pdev(sd);
229
230         if (verbose_request)
231                 dev_info(&pdev->xdev->dev,
232                          "write dev=%04x:%02x:%02x.%d - "
233                          "offset %x size %d val %x\n",
234                          pci_domain_nr(bus), bus->number,
235                          PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
236
237         return errno_to_pcibios_err(do_pci_op(pdev, &op));
238 }
239
240 static struct pci_ops pcifront_bus_ops = {
241         .read = pcifront_bus_read,
242         .write = pcifront_bus_write,
243 };
244
245 #ifdef CONFIG_PCI_MSI
246 static int pci_frontend_enable_msix(struct pci_dev *dev,
247                                     int vector[], int nvec)
248 {
249         int err;
250         int i;
251         struct xen_pci_op op = {
252                 .cmd    = XEN_PCI_OP_enable_msix,
253                 .domain = pci_domain_nr(dev->bus),
254                 .bus = dev->bus->number,
255                 .devfn = dev->devfn,
256                 .value = nvec,
257         };
258         struct pcifront_sd *sd = dev->bus->sysdata;
259         struct pcifront_device *pdev = pcifront_get_pdev(sd);
260         struct msi_desc *entry;
261
262         if (nvec > SH_INFO_MAX_VEC) {
263                 dev_err(&dev->dev, "too much vector for pci frontend: %x."
264                                    " Increase SH_INFO_MAX_VEC.\n", nvec);
265                 return -EINVAL;
266         }
267
268         i = 0;
269         list_for_each_entry(entry, &dev->msi_list, list) {
270                 op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
271                 /* Vector is useless at this point. */
272                 op.msix_entries[i].vector = -1;
273                 i++;
274         }
275
276         err = do_pci_op(pdev, &op);
277
278         if (likely(!err)) {
279                 if (likely(!op.value)) {
280                         /* we get the result */
281                         for (i = 0; i < nvec; i++) {
282                                 if (op.msix_entries[i].vector <= 0) {
283                                         dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
284                                                 i, op.msix_entries[i].vector);
285                                         err = -EINVAL;
286                                         vector[i] = -1;
287                                         continue;
288                                 }
289                                 vector[i] = op.msix_entries[i].vector;
290                         }
291                 } else {
292                         printk(KERN_DEBUG "enable msix get value %x\n",
293                                 op.value);
294                         err = op.value;
295                 }
296         } else {
297                 dev_err(&dev->dev, "enable msix get err %x\n", err);
298         }
299         return err;
300 }
301
302 static void pci_frontend_disable_msix(struct pci_dev *dev)
303 {
304         int err;
305         struct xen_pci_op op = {
306                 .cmd    = XEN_PCI_OP_disable_msix,
307                 .domain = pci_domain_nr(dev->bus),
308                 .bus = dev->bus->number,
309                 .devfn = dev->devfn,
310         };
311         struct pcifront_sd *sd = dev->bus->sysdata;
312         struct pcifront_device *pdev = pcifront_get_pdev(sd);
313
314         err = do_pci_op(pdev, &op);
315
316         /* What should do for error ? */
317         if (err)
318                 dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
319 }
320
321 static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
322 {
323         int err;
324         struct xen_pci_op op = {
325                 .cmd    = XEN_PCI_OP_enable_msi,
326                 .domain = pci_domain_nr(dev->bus),
327                 .bus = dev->bus->number,
328                 .devfn = dev->devfn,
329         };
330         struct pcifront_sd *sd = dev->bus->sysdata;
331         struct pcifront_device *pdev = pcifront_get_pdev(sd);
332
333         err = do_pci_op(pdev, &op);
334         if (likely(!err)) {
335                 vector[0] = op.value;
336                 if (op.value <= 0) {
337                         dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
338                                 op.value);
339                         err = -EINVAL;
340                         vector[0] = -1;
341                 }
342         } else {
343                 dev_err(&dev->dev, "pci frontend enable msi failed for dev "
344                                     "%x:%x\n", op.bus, op.devfn);
345                 err = -EINVAL;
346         }
347         return err;
348 }
349
350 static void pci_frontend_disable_msi(struct pci_dev *dev)
351 {
352         int err;
353         struct xen_pci_op op = {
354                 .cmd    = XEN_PCI_OP_disable_msi,
355                 .domain = pci_domain_nr(dev->bus),
356                 .bus = dev->bus->number,
357                 .devfn = dev->devfn,
358         };
359         struct pcifront_sd *sd = dev->bus->sysdata;
360         struct pcifront_device *pdev = pcifront_get_pdev(sd);
361
362         err = do_pci_op(pdev, &op);
363         if (err == XEN_PCI_ERR_dev_not_found) {
364                 /* XXX No response from backend, what shall we do? */
365                 printk(KERN_DEBUG "get no response from backend for disable MSI\n");
366                 return;
367         }
368         if (err)
369                 /* how can pciback notify us fail? */
370                 printk(KERN_DEBUG "get fake response frombackend\n");
371 }
372
373 static struct xen_pci_frontend_ops pci_frontend_ops = {
374         .enable_msi = pci_frontend_enable_msi,
375         .disable_msi = pci_frontend_disable_msi,
376         .enable_msix = pci_frontend_enable_msix,
377         .disable_msix = pci_frontend_disable_msix,
378 };
379
380 static void pci_frontend_registrar(int enable)
381 {
382         if (enable)
383                 xen_pci_frontend = &pci_frontend_ops;
384         else
385                 xen_pci_frontend = NULL;
386 };
387 #else
388 static inline void pci_frontend_registrar(int enable) { };
389 #endif /* CONFIG_PCI_MSI */
390
391 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
392 static int pcifront_claim_resource(struct pci_dev *dev, void *data)
393 {
394         struct pcifront_device *pdev = data;
395         int i;
396         struct resource *r;
397
398         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
399                 r = &dev->resource[i];
400
401                 if (!r->parent && r->start && r->flags) {
402                         dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
403                                 pci_name(dev), i);
404                         if (pci_claim_resource(dev, i)) {
405                                 dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
406                                         "Device offline. Try using e820_host=1 in the guest config.\n",
407                                         pci_name(dev), i);
408                         }
409                 }
410         }
411
412         return 0;
413 }
414
415 static int pcifront_scan_bus(struct pcifront_device *pdev,
416                                 unsigned int domain, unsigned int bus,
417                                 struct pci_bus *b)
418 {
419         struct pci_dev *d;
420         unsigned int devfn;
421
422         /* Scan the bus for functions and add.
423          * We omit handling of PCI bridge attachment because pciback prevents
424          * bridges from being exported.
425          */
426         for (devfn = 0; devfn < 0x100; devfn++) {
427                 d = pci_get_slot(b, devfn);
428                 if (d) {
429                         /* Device is already known. */
430                         pci_dev_put(d);
431                         continue;
432                 }
433
434                 d = pci_scan_single_device(b, devfn);
435                 if (d)
436                         dev_info(&pdev->xdev->dev, "New device on "
437                                  "%04x:%02x:%02x.%d found.\n", domain, bus,
438                                  PCI_SLOT(devfn), PCI_FUNC(devfn));
439         }
440
441         return 0;
442 }
443
444 static int pcifront_scan_root(struct pcifront_device *pdev,
445                                  unsigned int domain, unsigned int bus)
446 {
447         struct pci_bus *b;
448         struct pcifront_sd *sd = NULL;
449         struct pci_bus_entry *bus_entry = NULL;
450         int err = 0;
451
452 #ifndef CONFIG_PCI_DOMAINS
453         if (domain != 0) {
454                 dev_err(&pdev->xdev->dev,
455                         "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
456                 dev_err(&pdev->xdev->dev,
457                         "Please compile with CONFIG_PCI_DOMAINS\n");
458                 err = -EINVAL;
459                 goto err_out;
460         }
461 #endif
462
463         dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
464                  domain, bus);
465
466         bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
467         sd = kmalloc(sizeof(*sd), GFP_KERNEL);
468         if (!bus_entry || !sd) {
469                 err = -ENOMEM;
470                 goto err_out;
471         }
472         pcifront_init_sd(sd, domain, bus, pdev);
473
474         pci_lock_rescan_remove();
475
476         b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
477                                   &pcifront_bus_ops, sd);
478         if (!b) {
479                 dev_err(&pdev->xdev->dev,
480                         "Error creating PCI Frontend Bus!\n");
481                 err = -ENOMEM;
482                 pci_unlock_rescan_remove();
483                 goto err_out;
484         }
485
486         bus_entry->bus = b;
487
488         list_add(&bus_entry->list, &pdev->root_buses);
489
490         /* pci_scan_bus_parented skips devices which do not have a have
491         * devfn==0. The pcifront_scan_bus enumerates all devfn. */
492         err = pcifront_scan_bus(pdev, domain, bus, b);
493
494         /* Claim resources before going "live" with our devices */
495         pci_walk_bus(b, pcifront_claim_resource, pdev);
496
497         /* Create SysFS and notify udev of the devices. Aka: "going live" */
498         pci_bus_add_devices(b);
499
500         pci_unlock_rescan_remove();
501         return err;
502
503 err_out:
504         kfree(bus_entry);
505         kfree(sd);
506
507         return err;
508 }
509
510 static int pcifront_rescan_root(struct pcifront_device *pdev,
511                                    unsigned int domain, unsigned int bus)
512 {
513         int err;
514         struct pci_bus *b;
515
516 #ifndef CONFIG_PCI_DOMAINS
517         if (domain != 0) {
518                 dev_err(&pdev->xdev->dev,
519                         "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
520                 dev_err(&pdev->xdev->dev,
521                         "Please compile with CONFIG_PCI_DOMAINS\n");
522                 return -EINVAL;
523         }
524 #endif
525
526         dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
527                  domain, bus);
528
529         b = pci_find_bus(domain, bus);
530         if (!b)
531                 /* If the bus is unknown, create it. */
532                 return pcifront_scan_root(pdev, domain, bus);
533
534         err = pcifront_scan_bus(pdev, domain, bus, b);
535
536         /* Claim resources before going "live" with our devices */
537         pci_walk_bus(b, pcifront_claim_resource, pdev);
538
539         /* Create SysFS and notify udev of the devices. Aka: "going live" */
540         pci_bus_add_devices(b);
541
542         return err;
543 }
544
545 static void free_root_bus_devs(struct pci_bus *bus)
546 {
547         struct pci_dev *dev;
548
549         while (!list_empty(&bus->devices)) {
550                 dev = container_of(bus->devices.next, struct pci_dev,
551                                    bus_list);
552                 dev_dbg(&dev->dev, "removing device\n");
553                 pci_stop_and_remove_bus_device(dev);
554         }
555 }
556
557 static void pcifront_free_roots(struct pcifront_device *pdev)
558 {
559         struct pci_bus_entry *bus_entry, *t;
560
561         dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
562
563         pci_lock_rescan_remove();
564         list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
565                 list_del(&bus_entry->list);
566
567                 free_root_bus_devs(bus_entry->bus);
568
569                 kfree(bus_entry->bus->sysdata);
570
571                 device_unregister(bus_entry->bus->bridge);
572                 pci_remove_bus(bus_entry->bus);
573
574                 kfree(bus_entry);
575         }
576         pci_unlock_rescan_remove();
577 }
578
579 static pci_ers_result_t pcifront_common_process(int cmd,
580                                                 struct pcifront_device *pdev,
581                                                 pci_channel_state_t state)
582 {
583         pci_ers_result_t result;
584         struct pci_driver *pdrv;
585         int bus = pdev->sh_info->aer_op.bus;
586         int devfn = pdev->sh_info->aer_op.devfn;
587         struct pci_dev *pcidev;
588         int flag = 0;
589
590         dev_dbg(&pdev->xdev->dev,
591                 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
592                 cmd, bus, devfn);
593         result = PCI_ERS_RESULT_NONE;
594
595         pcidev = pci_get_bus_and_slot(bus, devfn);
596         if (!pcidev || !pcidev->driver) {
597                 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
598                 if (pcidev)
599                         pci_dev_put(pcidev);
600                 return result;
601         }
602         pdrv = pcidev->driver;
603
604         if (pdrv) {
605                 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
606                         dev_dbg(&pcidev->dev,
607                                 "trying to call AER service\n");
608                         if (pcidev) {
609                                 flag = 1;
610                                 switch (cmd) {
611                                 case XEN_PCI_OP_aer_detected:
612                                         result = pdrv->err_handler->
613                                                  error_detected(pcidev, state);
614                                         break;
615                                 case XEN_PCI_OP_aer_mmio:
616                                         result = pdrv->err_handler->
617                                                  mmio_enabled(pcidev);
618                                         break;
619                                 case XEN_PCI_OP_aer_slotreset:
620                                         result = pdrv->err_handler->
621                                                  slot_reset(pcidev);
622                                         break;
623                                 case XEN_PCI_OP_aer_resume:
624                                         pdrv->err_handler->resume(pcidev);
625                                         break;
626                                 default:
627                                         dev_err(&pdev->xdev->dev,
628                                                 "bad request in aer recovery "
629                                                 "operation!\n");
630
631                                 }
632                         }
633                 }
634         }
635         if (!flag)
636                 result = PCI_ERS_RESULT_NONE;
637
638         return result;
639 }
640
641
642 static void pcifront_do_aer(struct work_struct *data)
643 {
644         struct pcifront_device *pdev =
645                 container_of(data, struct pcifront_device, op_work);
646         int cmd = pdev->sh_info->aer_op.cmd;
647         pci_channel_state_t state =
648                 (pci_channel_state_t)pdev->sh_info->aer_op.err;
649
650         /*If a pci_conf op is in progress,
651                 we have to wait until it is done before service aer op*/
652         dev_dbg(&pdev->xdev->dev,
653                 "pcifront service aer bus %x devfn %x\n",
654                 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
655
656         pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
657
658         /* Post the operation to the guest. */
659         wmb();
660         clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
661         notify_remote_via_evtchn(pdev->evtchn);
662
663         /*in case of we lost an aer request in four lines time_window*/
664         smp_mb__before_clear_bit();
665         clear_bit(_PDEVB_op_active, &pdev->flags);
666         smp_mb__after_clear_bit();
667
668         schedule_pcifront_aer_op(pdev);
669
670 }
671
672 static irqreturn_t pcifront_handler_aer(int irq, void *dev)
673 {
674         struct pcifront_device *pdev = dev;
675         schedule_pcifront_aer_op(pdev);
676         return IRQ_HANDLED;
677 }
678 static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
679 {
680         int err = 0;
681
682         spin_lock(&pcifront_dev_lock);
683
684         if (!pcifront_dev) {
685                 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
686                 pcifront_dev = pdev;
687         } else
688                 err = -EEXIST;
689
690         spin_unlock(&pcifront_dev_lock);
691
692         if (!err && !swiotlb_nr_tbl()) {
693                 err = pci_xen_swiotlb_init_late();
694                 if (err)
695                         dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
696         }
697         return err;
698 }
699
700 static void pcifront_disconnect(struct pcifront_device *pdev)
701 {
702         spin_lock(&pcifront_dev_lock);
703
704         if (pdev == pcifront_dev) {
705                 dev_info(&pdev->xdev->dev,
706                          "Disconnecting PCI Frontend Buses\n");
707                 pcifront_dev = NULL;
708         }
709
710         spin_unlock(&pcifront_dev_lock);
711 }
712 static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
713 {
714         struct pcifront_device *pdev;
715
716         pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
717         if (pdev == NULL)
718                 goto out;
719
720         pdev->sh_info =
721             (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
722         if (pdev->sh_info == NULL) {
723                 kfree(pdev);
724                 pdev = NULL;
725                 goto out;
726         }
727         pdev->sh_info->flags = 0;
728
729         /*Flag for registering PV AER handler*/
730         set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
731
732         dev_set_drvdata(&xdev->dev, pdev);
733         pdev->xdev = xdev;
734
735         INIT_LIST_HEAD(&pdev->root_buses);
736
737         spin_lock_init(&pdev->sh_info_lock);
738
739         pdev->evtchn = INVALID_EVTCHN;
740         pdev->gnt_ref = INVALID_GRANT_REF;
741         pdev->irq = -1;
742
743         INIT_WORK(&pdev->op_work, pcifront_do_aer);
744
745         dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
746                 pdev, pdev->sh_info);
747 out:
748         return pdev;
749 }
750
751 static void free_pdev(struct pcifront_device *pdev)
752 {
753         dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
754
755         pcifront_free_roots(pdev);
756
757         cancel_work_sync(&pdev->op_work);
758
759         if (pdev->irq >= 0)
760                 unbind_from_irqhandler(pdev->irq, pdev);
761
762         if (pdev->evtchn != INVALID_EVTCHN)
763                 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
764
765         if (pdev->gnt_ref != INVALID_GRANT_REF)
766                 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
767                                           (unsigned long)pdev->sh_info);
768         else
769                 free_page((unsigned long)pdev->sh_info);
770
771         dev_set_drvdata(&pdev->xdev->dev, NULL);
772
773         kfree(pdev);
774 }
775
776 static int pcifront_publish_info(struct pcifront_device *pdev)
777 {
778         int err = 0;
779         struct xenbus_transaction trans;
780
781         err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info));
782         if (err < 0)
783                 goto out;
784
785         pdev->gnt_ref = err;
786
787         err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
788         if (err)
789                 goto out;
790
791         err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
792                 0, "pcifront", pdev);
793
794         if (err < 0)
795                 return err;
796
797         pdev->irq = err;
798
799 do_publish:
800         err = xenbus_transaction_start(&trans);
801         if (err) {
802                 xenbus_dev_fatal(pdev->xdev, err,
803                                  "Error writing configuration for backend "
804                                  "(start transaction)");
805                 goto out;
806         }
807
808         err = xenbus_printf(trans, pdev->xdev->nodename,
809                             "pci-op-ref", "%u", pdev->gnt_ref);
810         if (!err)
811                 err = xenbus_printf(trans, pdev->xdev->nodename,
812                                     "event-channel", "%u", pdev->evtchn);
813         if (!err)
814                 err = xenbus_printf(trans, pdev->xdev->nodename,
815                                     "magic", XEN_PCI_MAGIC);
816
817         if (err) {
818                 xenbus_transaction_end(trans, 1);
819                 xenbus_dev_fatal(pdev->xdev, err,
820                                  "Error writing configuration for backend");
821                 goto out;
822         } else {
823                 err = xenbus_transaction_end(trans, 0);
824                 if (err == -EAGAIN)
825                         goto do_publish;
826                 else if (err) {
827                         xenbus_dev_fatal(pdev->xdev, err,
828                                          "Error completing transaction "
829                                          "for backend");
830                         goto out;
831                 }
832         }
833
834         xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
835
836         dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
837
838 out:
839         return err;
840 }
841
842 static int pcifront_try_connect(struct pcifront_device *pdev)
843 {
844         int err = -EFAULT;
845         int i, num_roots, len;
846         char str[64];
847         unsigned int domain, bus;
848
849
850         /* Only connect once */
851         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
852             XenbusStateInitialised)
853                 goto out;
854
855         err = pcifront_connect_and_init_dma(pdev);
856         if (err && err != -EEXIST) {
857                 xenbus_dev_fatal(pdev->xdev, err,
858                                  "Error setting up PCI Frontend");
859                 goto out;
860         }
861
862         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
863                            "root_num", "%d", &num_roots);
864         if (err == -ENOENT) {
865                 xenbus_dev_error(pdev->xdev, err,
866                                  "No PCI Roots found, trying 0000:00");
867                 err = pcifront_scan_root(pdev, 0, 0);
868                 num_roots = 0;
869         } else if (err != 1) {
870                 if (err == 0)
871                         err = -EINVAL;
872                 xenbus_dev_fatal(pdev->xdev, err,
873                                  "Error reading number of PCI roots");
874                 goto out;
875         }
876
877         for (i = 0; i < num_roots; i++) {
878                 len = snprintf(str, sizeof(str), "root-%d", i);
879                 if (unlikely(len >= (sizeof(str) - 1))) {
880                         err = -ENOMEM;
881                         goto out;
882                 }
883
884                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
885                                    "%x:%x", &domain, &bus);
886                 if (err != 2) {
887                         if (err >= 0)
888                                 err = -EINVAL;
889                         xenbus_dev_fatal(pdev->xdev, err,
890                                          "Error reading PCI root %d", i);
891                         goto out;
892                 }
893
894                 err = pcifront_scan_root(pdev, domain, bus);
895                 if (err) {
896                         xenbus_dev_fatal(pdev->xdev, err,
897                                          "Error scanning PCI root %04x:%02x",
898                                          domain, bus);
899                         goto out;
900                 }
901         }
902
903         err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
904
905 out:
906         return err;
907 }
908
909 static int pcifront_try_disconnect(struct pcifront_device *pdev)
910 {
911         int err = 0;
912         enum xenbus_state prev_state;
913
914
915         prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
916
917         if (prev_state >= XenbusStateClosing)
918                 goto out;
919
920         if (prev_state == XenbusStateConnected) {
921                 pcifront_free_roots(pdev);
922                 pcifront_disconnect(pdev);
923         }
924
925         err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
926
927 out:
928
929         return err;
930 }
931
932 static int pcifront_attach_devices(struct pcifront_device *pdev)
933 {
934         int err = -EFAULT;
935         int i, num_roots, len;
936         unsigned int domain, bus;
937         char str[64];
938
939         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
940             XenbusStateReconfiguring)
941                 goto out;
942
943         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
944                            "root_num", "%d", &num_roots);
945         if (err == -ENOENT) {
946                 xenbus_dev_error(pdev->xdev, err,
947                                  "No PCI Roots found, trying 0000:00");
948                 err = pcifront_rescan_root(pdev, 0, 0);
949                 num_roots = 0;
950         } else if (err != 1) {
951                 if (err == 0)
952                         err = -EINVAL;
953                 xenbus_dev_fatal(pdev->xdev, err,
954                                  "Error reading number of PCI roots");
955                 goto out;
956         }
957
958         for (i = 0; i < num_roots; i++) {
959                 len = snprintf(str, sizeof(str), "root-%d", i);
960                 if (unlikely(len >= (sizeof(str) - 1))) {
961                         err = -ENOMEM;
962                         goto out;
963                 }
964
965                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
966                                    "%x:%x", &domain, &bus);
967                 if (err != 2) {
968                         if (err >= 0)
969                                 err = -EINVAL;
970                         xenbus_dev_fatal(pdev->xdev, err,
971                                          "Error reading PCI root %d", i);
972                         goto out;
973                 }
974
975                 err = pcifront_rescan_root(pdev, domain, bus);
976                 if (err) {
977                         xenbus_dev_fatal(pdev->xdev, err,
978                                          "Error scanning PCI root %04x:%02x",
979                                          domain, bus);
980                         goto out;
981                 }
982         }
983
984         xenbus_switch_state(pdev->xdev, XenbusStateConnected);
985
986 out:
987         return err;
988 }
989
990 static int pcifront_detach_devices(struct pcifront_device *pdev)
991 {
992         int err = 0;
993         int i, num_devs;
994         unsigned int domain, bus, slot, func;
995         struct pci_dev *pci_dev;
996         char str[64];
997
998         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
999             XenbusStateConnected)
1000                 goto out;
1001
1002         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1003                            &num_devs);
1004         if (err != 1) {
1005                 if (err >= 0)
1006                         err = -EINVAL;
1007                 xenbus_dev_fatal(pdev->xdev, err,
1008                                  "Error reading number of PCI devices");
1009                 goto out;
1010         }
1011
1012         /* Find devices being detached and remove them. */
1013         for (i = 0; i < num_devs; i++) {
1014                 int l, state;
1015                 l = snprintf(str, sizeof(str), "state-%d", i);
1016                 if (unlikely(l >= (sizeof(str) - 1))) {
1017                         err = -ENOMEM;
1018                         goto out;
1019                 }
1020                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1021                                    &state);
1022                 if (err != 1)
1023                         state = XenbusStateUnknown;
1024
1025                 if (state != XenbusStateClosing)
1026                         continue;
1027
1028                 /* Remove device. */
1029                 l = snprintf(str, sizeof(str), "vdev-%d", i);
1030                 if (unlikely(l >= (sizeof(str) - 1))) {
1031                         err = -ENOMEM;
1032                         goto out;
1033                 }
1034                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1035                                    "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1036                 if (err != 4) {
1037                         if (err >= 0)
1038                                 err = -EINVAL;
1039                         xenbus_dev_fatal(pdev->xdev, err,
1040                                          "Error reading PCI device %d", i);
1041                         goto out;
1042                 }
1043
1044                 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1045                                 PCI_DEVFN(slot, func));
1046                 if (!pci_dev) {
1047                         dev_dbg(&pdev->xdev->dev,
1048                                 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1049                                 domain, bus, slot, func);
1050                         continue;
1051                 }
1052                 pci_lock_rescan_remove();
1053                 pci_stop_and_remove_bus_device(pci_dev);
1054                 pci_dev_put(pci_dev);
1055                 pci_unlock_rescan_remove();
1056
1057                 dev_dbg(&pdev->xdev->dev,
1058                         "PCI device %04x:%02x:%02x.%d removed.\n",
1059                         domain, bus, slot, func);
1060         }
1061
1062         err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1063
1064 out:
1065         return err;
1066 }
1067
1068 static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1069                                                   enum xenbus_state be_state)
1070 {
1071         struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1072
1073         switch (be_state) {
1074         case XenbusStateUnknown:
1075         case XenbusStateInitialising:
1076         case XenbusStateInitWait:
1077         case XenbusStateInitialised:
1078                 break;
1079
1080         case XenbusStateConnected:
1081                 pcifront_try_connect(pdev);
1082                 break;
1083
1084         case XenbusStateClosed:
1085                 if (xdev->state == XenbusStateClosed)
1086                         break;
1087                 /* Missed the backend's CLOSING state -- fallthrough */
1088         case XenbusStateClosing:
1089                 dev_warn(&xdev->dev, "backend going away!\n");
1090                 pcifront_try_disconnect(pdev);
1091                 break;
1092
1093         case XenbusStateReconfiguring:
1094                 pcifront_detach_devices(pdev);
1095                 break;
1096
1097         case XenbusStateReconfigured:
1098                 pcifront_attach_devices(pdev);
1099                 break;
1100         }
1101 }
1102
1103 static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1104                                  const struct xenbus_device_id *id)
1105 {
1106         int err = 0;
1107         struct pcifront_device *pdev = alloc_pdev(xdev);
1108
1109         if (pdev == NULL) {
1110                 err = -ENOMEM;
1111                 xenbus_dev_fatal(xdev, err,
1112                                  "Error allocating pcifront_device struct");
1113                 goto out;
1114         }
1115
1116         err = pcifront_publish_info(pdev);
1117         if (err)
1118                 free_pdev(pdev);
1119
1120 out:
1121         return err;
1122 }
1123
1124 static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1125 {
1126         struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1127         if (pdev)
1128                 free_pdev(pdev);
1129
1130         return 0;
1131 }
1132
1133 static const struct xenbus_device_id xenpci_ids[] = {
1134         {"pci"},
1135         {""},
1136 };
1137
1138 static DEFINE_XENBUS_DRIVER(xenpci, "pcifront",
1139         .probe                  = pcifront_xenbus_probe,
1140         .remove                 = pcifront_xenbus_remove,
1141         .otherend_changed       = pcifront_backend_changed,
1142 );
1143
1144 static int __init pcifront_init(void)
1145 {
1146         if (!xen_pv_domain() || xen_initial_domain())
1147                 return -ENODEV;
1148
1149         pci_frontend_registrar(1 /* enable */);
1150
1151         return xenbus_register_frontend(&xenpci_driver);
1152 }
1153
1154 static void __exit pcifront_cleanup(void)
1155 {
1156         xenbus_unregister_driver(&xenpci_driver);
1157         pci_frontend_registrar(0 /* disable */);
1158 }
1159 module_init(pcifront_init);
1160 module_exit(pcifront_cleanup);
1161
1162 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1163 MODULE_LICENSE("GPL");
1164 MODULE_ALIAS("xen:pci");