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