blob: c4796c81c4aff8702baed4bd2394177f65373f45 [file] [log] [blame]
Ryan Wilson956a9202010-08-02 21:31:05 -04001/*
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>
Ryan Wilson956a9202010-08-02 21:31:05 -040016#include <xen/interface/io/pciif.h>
17#include <asm/xen/pci.h>
18#include <linux/interrupt.h>
Arun Sharma600634972011-07-26 16:09:06 -070019#include <linux/atomic.h>
Ryan Wilson956a9202010-08-02 21:31:05 -040020#include <linux/workqueue.h>
21#include <linux/bitops.h>
22#include <linux/time.h>
Tina Ruchandanie1d5bbc2015-05-19 11:38:09 +053023#include <linux/ktime.h>
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -050024#include <xen/platform_pci.h>
Ryan Wilson956a9202010-08-02 21:31:05 -040025
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -040026#include <asm/xen/swiotlb-xen.h>
Ryan Wilson956a9202010-08-02 21:31:05 -040027#define INVALID_GRANT_REF (0)
28#define INVALID_EVTCHN (-1)
29
30struct pci_bus_entry {
31 struct list_head list;
32 struct pci_bus *bus;
33};
34
35#define _PDEVB_op_active (0)
36#define PDEVB_op_active (1 << (_PDEVB_op_active))
37
38struct pcifront_device {
39 struct xenbus_device *xdev;
40 struct list_head root_buses;
41
42 int evtchn;
43 int gnt_ref;
44
45 int irq;
46
47 /* Lock this when doing any operations in sh_info */
48 spinlock_t sh_info_lock;
49 struct xen_pci_sharedinfo *sh_info;
50 struct work_struct op_work;
51 unsigned long flags;
52
53};
54
55struct pcifront_sd {
56 int domain;
57 struct pcifront_device *pdev;
58};
59
60static inline struct pcifront_device *
61pcifront_get_pdev(struct pcifront_sd *sd)
62{
63 return sd->pdev;
64}
65
66static inline void pcifront_init_sd(struct pcifront_sd *sd,
67 unsigned int domain, unsigned int bus,
68 struct pcifront_device *pdev)
69{
70 sd->domain = domain;
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;
Ryan Wilson956a9202010-08-02 21:31:05 -0400119
120 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
121
122 memcpy(active_op, op, sizeof(struct xen_pci_op));
123
124 /* Go */
125 wmb();
126 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
127 notify_remote_via_evtchn(port);
128
129 /*
130 * We set a poll timeout of 3 seconds but give up on return after
131 * 2 seconds. It is better to time out too late rather than too early
132 * (in the latter case we end up continually re-executing poll() with a
133 * timeout in the past). 1s difference gives plenty of slack for error.
134 */
Tina Ruchandanie1d5bbc2015-05-19 11:38:09 +0530135 ns_timeout = ktime_get_ns() + 2 * (s64)NSEC_PER_SEC;
Ryan Wilson956a9202010-08-02 21:31:05 -0400136
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);
Tina Ruchandanie1d5bbc2015-05-19 11:38:09 +0530143 ns = ktime_get_ns();
Ryan Wilson956a9202010-08-02 21:31:05 -0400144 if (ns > ns_timeout) {
145 dev_err(&pdev->xdev->dev,
146 "pciback not responding!!!\n");
147 clear_bit(_XEN_PCIF_active,
148 (unsigned long *)&pdev->sh_info->flags);
149 err = XEN_PCI_ERR_dev_not_found;
150 goto out;
151 }
152 }
153
154 /*
155 * We might lose backend service request since we
156 * reuse same evtchn with pci_conf backend response. So re-schedule
157 * aer pcifront service.
158 */
159 if (test_bit(_XEN_PCIB_active,
160 (unsigned long *)&pdev->sh_info->flags)) {
161 dev_err(&pdev->xdev->dev,
162 "schedule aer pcifront service\n");
163 schedule_pcifront_aer_op(pdev);
164 }
165
166 memcpy(op, active_op, sizeof(struct xen_pci_op));
167
168 err = op->err;
169out:
170 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
171 return err;
172}
173
174/* Access to this function is spinlocked in drivers/pci/access.c */
175static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
176 int where, int size, u32 *val)
177{
178 int err = 0;
179 struct xen_pci_op op = {
180 .cmd = XEN_PCI_OP_conf_read,
181 .domain = pci_domain_nr(bus),
182 .bus = bus->number,
183 .devfn = devfn,
184 .offset = where,
185 .size = size,
186 };
187 struct pcifront_sd *sd = bus->sysdata;
188 struct pcifront_device *pdev = pcifront_get_pdev(sd);
189
190 if (verbose_request)
191 dev_info(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500192 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
Ryan Wilson956a9202010-08-02 21:31:05 -0400193 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
194 PCI_FUNC(devfn), where, size);
195
196 err = do_pci_op(pdev, &op);
197
198 if (likely(!err)) {
199 if (verbose_request)
200 dev_info(&pdev->xdev->dev, "read got back value %x\n",
201 op.value);
202
203 *val = op.value;
204 } else if (err == -ENODEV) {
205 /* No device here, pretend that it just returned 0 */
206 err = 0;
207 *val = 0;
208 }
209
210 return errno_to_pcibios_err(err);
211}
212
213/* Access to this function is spinlocked in drivers/pci/access.c */
214static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
215 int where, int size, u32 val)
216{
217 struct xen_pci_op op = {
218 .cmd = XEN_PCI_OP_conf_write,
219 .domain = pci_domain_nr(bus),
220 .bus = bus->number,
221 .devfn = devfn,
222 .offset = where,
223 .size = size,
224 .value = val,
225 };
226 struct pcifront_sd *sd = bus->sysdata;
227 struct pcifront_device *pdev = pcifront_get_pdev(sd);
228
229 if (verbose_request)
230 dev_info(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500231 "write dev=%04x:%02x:%02x.%d - "
Ryan Wilson956a9202010-08-02 21:31:05 -0400232 "offset %x size %d val %x\n",
233 pci_domain_nr(bus), bus->number,
234 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
235
236 return errno_to_pcibios_err(do_pci_op(pdev, &op));
237}
238
Konrad Rzeszutek Wilkb8b0f552012-08-21 14:49:34 -0400239static struct pci_ops pcifront_bus_ops = {
Ryan Wilson956a9202010-08-02 21:31:05 -0400240 .read = pcifront_bus_read,
241 .write = pcifront_bus_write,
242};
243
244#ifdef CONFIG_PCI_MSI
245static int pci_frontend_enable_msix(struct pci_dev *dev,
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500246 int vector[], int nvec)
Ryan Wilson956a9202010-08-02 21:31:05 -0400247{
248 int err;
249 int i;
250 struct xen_pci_op op = {
251 .cmd = XEN_PCI_OP_enable_msix,
252 .domain = pci_domain_nr(dev->bus),
253 .bus = dev->bus->number,
254 .devfn = dev->devfn,
255 .value = nvec,
256 };
257 struct pcifront_sd *sd = dev->bus->sysdata;
258 struct pcifront_device *pdev = pcifront_get_pdev(sd);
259 struct msi_desc *entry;
260
261 if (nvec > SH_INFO_MAX_VEC) {
262 dev_err(&dev->dev, "too much vector for pci frontend: %x."
263 " Increase SH_INFO_MAX_VEC.\n", nvec);
264 return -EINVAL;
265 }
266
267 i = 0;
268 list_for_each_entry(entry, &dev->msi_list, list) {
269 op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
270 /* Vector is useless at this point. */
271 op.msix_entries[i].vector = -1;
272 i++;
273 }
274
275 err = do_pci_op(pdev, &op);
276
277 if (likely(!err)) {
278 if (likely(!op.value)) {
279 /* we get the result */
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500280 for (i = 0; i < nvec; i++) {
281 if (op.msix_entries[i].vector <= 0) {
282 dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
283 i, op.msix_entries[i].vector);
284 err = -EINVAL;
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500285 vector[i] = -1;
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500286 continue;
287 }
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500288 vector[i] = op.msix_entries[i].vector;
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500289 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400290 } else {
291 printk(KERN_DEBUG "enable msix get value %x\n",
292 op.value);
Jan Beulichf09d8432012-04-02 15:22:39 +0100293 err = op.value;
Ryan Wilson956a9202010-08-02 21:31:05 -0400294 }
295 } else {
296 dev_err(&dev->dev, "enable msix get err %x\n", err);
Ryan Wilson956a9202010-08-02 21:31:05 -0400297 }
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500298 return err;
Ryan Wilson956a9202010-08-02 21:31:05 -0400299}
300
301static void pci_frontend_disable_msix(struct pci_dev *dev)
302{
303 int err;
304 struct xen_pci_op op = {
305 .cmd = XEN_PCI_OP_disable_msix,
306 .domain = pci_domain_nr(dev->bus),
307 .bus = dev->bus->number,
308 .devfn = dev->devfn,
309 };
310 struct pcifront_sd *sd = dev->bus->sysdata;
311 struct pcifront_device *pdev = pcifront_get_pdev(sd);
312
313 err = do_pci_op(pdev, &op);
314
315 /* What should do for error ? */
316 if (err)
317 dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
318}
319
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500320static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
Ryan Wilson956a9202010-08-02 21:31:05 -0400321{
322 int err;
323 struct xen_pci_op op = {
324 .cmd = XEN_PCI_OP_enable_msi,
325 .domain = pci_domain_nr(dev->bus),
326 .bus = dev->bus->number,
327 .devfn = dev->devfn,
328 };
329 struct pcifront_sd *sd = dev->bus->sysdata;
330 struct pcifront_device *pdev = pcifront_get_pdev(sd);
331
332 err = do_pci_op(pdev, &op);
333 if (likely(!err)) {
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500334 vector[0] = op.value;
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500335 if (op.value <= 0) {
336 dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
337 op.value);
338 err = -EINVAL;
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500339 vector[0] = -1;
Konrad Rzeszutek Wilk1d461052011-02-16 13:43:22 -0500340 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400341 } else {
342 dev_err(&dev->dev, "pci frontend enable msi failed for dev "
343 "%x:%x\n", op.bus, op.devfn);
344 err = -EINVAL;
345 }
346 return err;
347}
348
349static void pci_frontend_disable_msi(struct pci_dev *dev)
350{
351 int err;
352 struct xen_pci_op op = {
353 .cmd = XEN_PCI_OP_disable_msi,
354 .domain = pci_domain_nr(dev->bus),
355 .bus = dev->bus->number,
356 .devfn = dev->devfn,
357 };
358 struct pcifront_sd *sd = dev->bus->sysdata;
359 struct pcifront_device *pdev = pcifront_get_pdev(sd);
360
361 err = do_pci_op(pdev, &op);
362 if (err == XEN_PCI_ERR_dev_not_found) {
363 /* XXX No response from backend, what shall we do? */
364 printk(KERN_DEBUG "get no response from backend for disable MSI\n");
365 return;
366 }
367 if (err)
368 /* how can pciback notify us fail? */
369 printk(KERN_DEBUG "get fake response frombackend\n");
370}
371
372static struct xen_pci_frontend_ops pci_frontend_ops = {
373 .enable_msi = pci_frontend_enable_msi,
374 .disable_msi = pci_frontend_disable_msi,
375 .enable_msix = pci_frontend_enable_msix,
376 .disable_msix = pci_frontend_disable_msix,
377};
378
379static void pci_frontend_registrar(int enable)
380{
381 if (enable)
382 xen_pci_frontend = &pci_frontend_ops;
383 else
384 xen_pci_frontend = NULL;
385};
386#else
387static inline void pci_frontend_registrar(int enable) { };
388#endif /* CONFIG_PCI_MSI */
389
390/* Claim resources for the PCI frontend as-is, backend won't allow changes */
391static int pcifront_claim_resource(struct pci_dev *dev, void *data)
392{
393 struct pcifront_device *pdev = data;
394 int i;
395 struct resource *r;
396
397 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
398 r = &dev->resource[i];
399
400 if (!r->parent && r->start && r->flags) {
401 dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
402 pci_name(dev), i);
403 if (pci_claim_resource(dev, i)) {
Konrad Rzeszutek Wilk917e3e62011-07-22 12:18:43 -0400404 dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
405 "Device offline. Try using e820_host=1 in the guest config.\n",
Ryan Wilson956a9202010-08-02 21:31:05 -0400406 pci_name(dev), i);
407 }
408 }
409 }
410
411 return 0;
412}
413
Bill Pemberton15856ad2012-11-21 15:35:00 -0500414static int pcifront_scan_bus(struct pcifront_device *pdev,
Ryan Wilson956a9202010-08-02 21:31:05 -0400415 unsigned int domain, unsigned int bus,
416 struct pci_bus *b)
417{
418 struct pci_dev *d;
419 unsigned int devfn;
420
421 /* Scan the bus for functions and add.
422 * We omit handling of PCI bridge attachment because pciback prevents
423 * bridges from being exported.
424 */
425 for (devfn = 0; devfn < 0x100; devfn++) {
426 d = pci_get_slot(b, devfn);
427 if (d) {
428 /* Device is already known. */
429 pci_dev_put(d);
430 continue;
431 }
432
433 d = pci_scan_single_device(b, devfn);
434 if (d)
435 dev_info(&pdev->xdev->dev, "New device on "
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500436 "%04x:%02x:%02x.%d found.\n", domain, bus,
Ryan Wilson956a9202010-08-02 21:31:05 -0400437 PCI_SLOT(devfn), PCI_FUNC(devfn));
438 }
439
440 return 0;
441}
442
Bill Pemberton15856ad2012-11-21 15:35:00 -0500443static int pcifront_scan_root(struct pcifront_device *pdev,
Ryan Wilson956a9202010-08-02 21:31:05 -0400444 unsigned int domain, unsigned int bus)
445{
446 struct pci_bus *b;
447 struct pcifront_sd *sd = NULL;
448 struct pci_bus_entry *bus_entry = NULL;
449 int err = 0;
450
451#ifndef CONFIG_PCI_DOMAINS
452 if (domain != 0) {
453 dev_err(&pdev->xdev->dev,
454 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
455 dev_err(&pdev->xdev->dev,
456 "Please compile with CONFIG_PCI_DOMAINS\n");
457 err = -EINVAL;
458 goto err_out;
459 }
460#endif
461
462 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
463 domain, bus);
464
465 bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
466 sd = kmalloc(sizeof(*sd), GFP_KERNEL);
467 if (!bus_entry || !sd) {
468 err = -ENOMEM;
469 goto err_out;
470 }
471 pcifront_init_sd(sd, domain, bus, pdev);
472
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100473 pci_lock_rescan_remove();
474
Ryan Wilson956a9202010-08-02 21:31:05 -0400475 b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
476 &pcifront_bus_ops, sd);
477 if (!b) {
478 dev_err(&pdev->xdev->dev,
479 "Error creating PCI Frontend Bus!\n");
480 err = -ENOMEM;
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100481 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400482 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
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100499 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400500 return err;
501
502err_out:
503 kfree(bus_entry);
504 kfree(sd);
505
506 return err;
507}
508
Bill Pemberton15856ad2012-11-21 15:35:00 -0500509static int pcifront_rescan_root(struct pcifront_device *pdev,
Ryan Wilson956a9202010-08-02 21:31:05 -0400510 unsigned int domain, unsigned int bus)
511{
512 int err;
513 struct pci_bus *b;
514
515#ifndef CONFIG_PCI_DOMAINS
516 if (domain != 0) {
517 dev_err(&pdev->xdev->dev,
518 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
519 dev_err(&pdev->xdev->dev,
520 "Please compile with CONFIG_PCI_DOMAINS\n");
521 return -EINVAL;
522 }
523#endif
524
525 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
526 domain, bus);
527
528 b = pci_find_bus(domain, bus);
529 if (!b)
530 /* If the bus is unknown, create it. */
531 return pcifront_scan_root(pdev, domain, bus);
532
533 err = pcifront_scan_bus(pdev, domain, bus, b);
534
535 /* Claim resources before going "live" with our devices */
536 pci_walk_bus(b, pcifront_claim_resource, pdev);
537
538 /* Create SysFS and notify udev of the devices. Aka: "going live" */
539 pci_bus_add_devices(b);
540
541 return err;
542}
543
544static void free_root_bus_devs(struct pci_bus *bus)
545{
546 struct pci_dev *dev;
547
548 while (!list_empty(&bus->devices)) {
549 dev = container_of(bus->devices.next, struct pci_dev,
550 bus_list);
551 dev_dbg(&dev->dev, "removing device\n");
Yinghai Lu210647a2012-02-25 13:54:20 -0800552 pci_stop_and_remove_bus_device(dev);
Ryan Wilson956a9202010-08-02 21:31:05 -0400553 }
554}
555
556static void pcifront_free_roots(struct pcifront_device *pdev)
557{
558 struct pci_bus_entry *bus_entry, *t;
559
560 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
561
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100562 pci_lock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400563 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
564 list_del(&bus_entry->list);
565
566 free_root_bus_devs(bus_entry->bus);
567
568 kfree(bus_entry->bus->sysdata);
569
570 device_unregister(bus_entry->bus->bridge);
571 pci_remove_bus(bus_entry->bus);
572
573 kfree(bus_entry);
574 }
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100575 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400576}
577
578static pci_ers_result_t pcifront_common_process(int cmd,
579 struct pcifront_device *pdev,
580 pci_channel_state_t state)
581{
582 pci_ers_result_t result;
583 struct pci_driver *pdrv;
584 int bus = pdev->sh_info->aer_op.bus;
585 int devfn = pdev->sh_info->aer_op.devfn;
586 struct pci_dev *pcidev;
587 int flag = 0;
588
589 dev_dbg(&pdev->xdev->dev,
590 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
591 cmd, bus, devfn);
592 result = PCI_ERS_RESULT_NONE;
593
594 pcidev = pci_get_bus_and_slot(bus, devfn);
595 if (!pcidev || !pcidev->driver) {
Jiri Slaby2a63dd72010-11-04 15:31:30 +0100596 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
Markus Elfringff0387c2014-11-10 21:02:17 -0700597 pci_dev_put(pcidev);
Ryan Wilson956a9202010-08-02 21:31:05 -0400598 return result;
599 }
600 pdrv = pcidev->driver;
601
Alan Stern07d25142012-01-27 10:24:40 -0500602 if (pdrv) {
Ryan Wilson956a9202010-08-02 21:31:05 -0400603 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
604 dev_dbg(&pcidev->dev,
605 "trying to call AER service\n");
606 if (pcidev) {
607 flag = 1;
608 switch (cmd) {
609 case XEN_PCI_OP_aer_detected:
610 result = pdrv->err_handler->
611 error_detected(pcidev, state);
612 break;
613 case XEN_PCI_OP_aer_mmio:
614 result = pdrv->err_handler->
615 mmio_enabled(pcidev);
616 break;
617 case XEN_PCI_OP_aer_slotreset:
618 result = pdrv->err_handler->
619 slot_reset(pcidev);
620 break;
621 case XEN_PCI_OP_aer_resume:
622 pdrv->err_handler->resume(pcidev);
623 break;
624 default:
625 dev_err(&pdev->xdev->dev,
626 "bad request in aer recovery "
627 "operation!\n");
628
629 }
630 }
631 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400632 }
633 if (!flag)
634 result = PCI_ERS_RESULT_NONE;
635
636 return result;
637}
638
639
640static void pcifront_do_aer(struct work_struct *data)
641{
642 struct pcifront_device *pdev =
643 container_of(data, struct pcifront_device, op_work);
644 int cmd = pdev->sh_info->aer_op.cmd;
645 pci_channel_state_t state =
646 (pci_channel_state_t)pdev->sh_info->aer_op.err;
647
648 /*If a pci_conf op is in progress,
649 we have to wait until it is done before service aer op*/
650 dev_dbg(&pdev->xdev->dev,
651 "pcifront service aer bus %x devfn %x\n",
652 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
653
654 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
655
656 /* Post the operation to the guest. */
657 wmb();
658 clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
659 notify_remote_via_evtchn(pdev->evtchn);
660
661 /*in case of we lost an aer request in four lines time_window*/
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100662 smp_mb__before_atomic();
Ryan Wilson956a9202010-08-02 21:31:05 -0400663 clear_bit(_PDEVB_op_active, &pdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100664 smp_mb__after_atomic();
Ryan Wilson956a9202010-08-02 21:31:05 -0400665
666 schedule_pcifront_aer_op(pdev);
667
668}
669
670static irqreturn_t pcifront_handler_aer(int irq, void *dev)
671{
672 struct pcifront_device *pdev = dev;
673 schedule_pcifront_aer_op(pdev);
674 return IRQ_HANDLED;
675}
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400676static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400677{
678 int err = 0;
679
680 spin_lock(&pcifront_dev_lock);
681
682 if (!pcifront_dev) {
683 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
684 pcifront_dev = pdev;
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400685 } else
Ryan Wilson956a9202010-08-02 21:31:05 -0400686 err = -EEXIST;
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400687
Ryan Wilson956a9202010-08-02 21:31:05 -0400688 spin_unlock(&pcifront_dev_lock);
689
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400690 if (!err && !swiotlb_nr_tbl()) {
691 err = pci_xen_swiotlb_init_late();
692 if (err)
693 dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
694 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400695 return err;
696}
697
698static void pcifront_disconnect(struct pcifront_device *pdev)
699{
700 spin_lock(&pcifront_dev_lock);
701
702 if (pdev == pcifront_dev) {
703 dev_info(&pdev->xdev->dev,
704 "Disconnecting PCI Frontend Buses\n");
705 pcifront_dev = NULL;
706 }
707
708 spin_unlock(&pcifront_dev_lock);
709}
710static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
711{
712 struct pcifront_device *pdev;
713
714 pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
715 if (pdev == NULL)
716 goto out;
717
718 pdev->sh_info =
719 (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
720 if (pdev->sh_info == NULL) {
721 kfree(pdev);
722 pdev = NULL;
723 goto out;
724 }
725 pdev->sh_info->flags = 0;
726
727 /*Flag for registering PV AER handler*/
728 set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
729
730 dev_set_drvdata(&xdev->dev, pdev);
731 pdev->xdev = xdev;
732
733 INIT_LIST_HEAD(&pdev->root_buses);
734
735 spin_lock_init(&pdev->sh_info_lock);
736
737 pdev->evtchn = INVALID_EVTCHN;
738 pdev->gnt_ref = INVALID_GRANT_REF;
739 pdev->irq = -1;
740
741 INIT_WORK(&pdev->op_work, pcifront_do_aer);
742
743 dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
744 pdev, pdev->sh_info);
745out:
746 return pdev;
747}
748
749static void free_pdev(struct pcifront_device *pdev)
750{
751 dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
752
753 pcifront_free_roots(pdev);
754
Tejun Heodb2e2e62011-01-24 15:43:03 +0100755 cancel_work_sync(&pdev->op_work);
Ryan Wilson956a9202010-08-02 21:31:05 -0400756
757 if (pdev->irq >= 0)
758 unbind_from_irqhandler(pdev->irq, pdev);
759
760 if (pdev->evtchn != INVALID_EVTCHN)
761 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
762
763 if (pdev->gnt_ref != INVALID_GRANT_REF)
764 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
765 (unsigned long)pdev->sh_info);
766 else
767 free_page((unsigned long)pdev->sh_info);
768
769 dev_set_drvdata(&pdev->xdev->dev, NULL);
770
771 kfree(pdev);
772}
773
774static int pcifront_publish_info(struct pcifront_device *pdev)
775{
776 int err = 0;
777 struct xenbus_transaction trans;
Wei Liuccc9d902015-04-03 14:44:59 +0800778 grant_ref_t gref;
Ryan Wilson956a9202010-08-02 21:31:05 -0400779
Wei Liuccc9d902015-04-03 14:44:59 +0800780 err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
Ryan Wilson956a9202010-08-02 21:31:05 -0400781 if (err < 0)
782 goto out;
783
Wei Liuccc9d902015-04-03 14:44:59 +0800784 pdev->gnt_ref = gref;
Ryan Wilson956a9202010-08-02 21:31:05 -0400785
786 err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
787 if (err)
788 goto out;
789
790 err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
791 0, "pcifront", pdev);
792
793 if (err < 0)
794 return err;
795
796 pdev->irq = err;
797
798do_publish:
799 err = xenbus_transaction_start(&trans);
800 if (err) {
801 xenbus_dev_fatal(pdev->xdev, err,
802 "Error writing configuration for backend "
803 "(start transaction)");
804 goto out;
805 }
806
807 err = xenbus_printf(trans, pdev->xdev->nodename,
808 "pci-op-ref", "%u", pdev->gnt_ref);
809 if (!err)
810 err = xenbus_printf(trans, pdev->xdev->nodename,
811 "event-channel", "%u", pdev->evtchn);
812 if (!err)
813 err = xenbus_printf(trans, pdev->xdev->nodename,
814 "magic", XEN_PCI_MAGIC);
815
816 if (err) {
817 xenbus_transaction_end(trans, 1);
818 xenbus_dev_fatal(pdev->xdev, err,
819 "Error writing configuration for backend");
820 goto out;
821 } else {
822 err = xenbus_transaction_end(trans, 0);
823 if (err == -EAGAIN)
824 goto do_publish;
825 else if (err) {
826 xenbus_dev_fatal(pdev->xdev, err,
827 "Error completing transaction "
828 "for backend");
829 goto out;
830 }
831 }
832
833 xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
834
835 dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
836
837out:
838 return err;
839}
840
Bill Pemberton15856ad2012-11-21 15:35:00 -0500841static int pcifront_try_connect(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400842{
843 int err = -EFAULT;
844 int i, num_roots, len;
845 char str[64];
846 unsigned int domain, bus;
847
848
849 /* Only connect once */
850 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
851 XenbusStateInitialised)
852 goto out;
853
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400854 err = pcifront_connect_and_init_dma(pdev);
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400855 if (err && err != -EEXIST) {
Ryan Wilson956a9202010-08-02 21:31:05 -0400856 xenbus_dev_fatal(pdev->xdev, err,
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400857 "Error setting up PCI Frontend");
Ryan Wilson956a9202010-08-02 21:31:05 -0400858 goto out;
859 }
860
861 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
862 "root_num", "%d", &num_roots);
863 if (err == -ENOENT) {
864 xenbus_dev_error(pdev->xdev, err,
865 "No PCI Roots found, trying 0000:00");
866 err = pcifront_scan_root(pdev, 0, 0);
Chen Gang23cf1d02014-10-06 11:04:45 +0800867 if (err) {
868 xenbus_dev_fatal(pdev->xdev, err,
869 "Error scanning PCI root 0000:00");
870 goto out;
871 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400872 num_roots = 0;
873 } else if (err != 1) {
874 if (err == 0)
875 err = -EINVAL;
876 xenbus_dev_fatal(pdev->xdev, err,
877 "Error reading number of PCI roots");
878 goto out;
879 }
880
881 for (i = 0; i < num_roots; i++) {
882 len = snprintf(str, sizeof(str), "root-%d", i);
883 if (unlikely(len >= (sizeof(str) - 1))) {
884 err = -ENOMEM;
885 goto out;
886 }
887
888 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
889 "%x:%x", &domain, &bus);
890 if (err != 2) {
891 if (err >= 0)
892 err = -EINVAL;
893 xenbus_dev_fatal(pdev->xdev, err,
894 "Error reading PCI root %d", i);
895 goto out;
896 }
897
898 err = pcifront_scan_root(pdev, domain, bus);
899 if (err) {
900 xenbus_dev_fatal(pdev->xdev, err,
901 "Error scanning PCI root %04x:%02x",
902 domain, bus);
903 goto out;
904 }
905 }
906
907 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
908
909out:
910 return err;
911}
912
913static int pcifront_try_disconnect(struct pcifront_device *pdev)
914{
915 int err = 0;
916 enum xenbus_state prev_state;
917
918
919 prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
920
921 if (prev_state >= XenbusStateClosing)
922 goto out;
923
924 if (prev_state == XenbusStateConnected) {
925 pcifront_free_roots(pdev);
926 pcifront_disconnect(pdev);
927 }
928
929 err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
930
931out:
932
933 return err;
934}
935
Bill Pemberton15856ad2012-11-21 15:35:00 -0500936static int pcifront_attach_devices(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400937{
938 int err = -EFAULT;
939 int i, num_roots, len;
940 unsigned int domain, bus;
941 char str[64];
942
943 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
944 XenbusStateReconfiguring)
945 goto out;
946
947 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
948 "root_num", "%d", &num_roots);
949 if (err == -ENOENT) {
950 xenbus_dev_error(pdev->xdev, err,
951 "No PCI Roots found, trying 0000:00");
952 err = pcifront_rescan_root(pdev, 0, 0);
Chen Gang23cf1d02014-10-06 11:04:45 +0800953 if (err) {
954 xenbus_dev_fatal(pdev->xdev, err,
955 "Error scanning PCI root 0000:00");
956 goto out;
957 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400958 num_roots = 0;
959 } else if (err != 1) {
960 if (err == 0)
961 err = -EINVAL;
962 xenbus_dev_fatal(pdev->xdev, err,
963 "Error reading number of PCI roots");
964 goto out;
965 }
966
967 for (i = 0; i < num_roots; i++) {
968 len = snprintf(str, sizeof(str), "root-%d", i);
969 if (unlikely(len >= (sizeof(str) - 1))) {
970 err = -ENOMEM;
971 goto out;
972 }
973
974 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
975 "%x:%x", &domain, &bus);
976 if (err != 2) {
977 if (err >= 0)
978 err = -EINVAL;
979 xenbus_dev_fatal(pdev->xdev, err,
980 "Error reading PCI root %d", i);
981 goto out;
982 }
983
984 err = pcifront_rescan_root(pdev, domain, bus);
985 if (err) {
986 xenbus_dev_fatal(pdev->xdev, err,
987 "Error scanning PCI root %04x:%02x",
988 domain, bus);
989 goto out;
990 }
991 }
992
993 xenbus_switch_state(pdev->xdev, XenbusStateConnected);
994
995out:
996 return err;
997}
998
999static int pcifront_detach_devices(struct pcifront_device *pdev)
1000{
1001 int err = 0;
1002 int i, num_devs;
1003 unsigned int domain, bus, slot, func;
Ryan Wilson956a9202010-08-02 21:31:05 -04001004 struct pci_dev *pci_dev;
1005 char str[64];
1006
1007 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1008 XenbusStateConnected)
1009 goto out;
1010
1011 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1012 &num_devs);
1013 if (err != 1) {
1014 if (err >= 0)
1015 err = -EINVAL;
1016 xenbus_dev_fatal(pdev->xdev, err,
1017 "Error reading number of PCI devices");
1018 goto out;
1019 }
1020
1021 /* Find devices being detached and remove them. */
1022 for (i = 0; i < num_devs; i++) {
1023 int l, state;
1024 l = snprintf(str, sizeof(str), "state-%d", i);
1025 if (unlikely(l >= (sizeof(str) - 1))) {
1026 err = -ENOMEM;
1027 goto out;
1028 }
1029 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1030 &state);
1031 if (err != 1)
1032 state = XenbusStateUnknown;
1033
1034 if (state != XenbusStateClosing)
1035 continue;
1036
1037 /* Remove device. */
1038 l = snprintf(str, sizeof(str), "vdev-%d", i);
1039 if (unlikely(l >= (sizeof(str) - 1))) {
1040 err = -ENOMEM;
1041 goto out;
1042 }
1043 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1044 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1045 if (err != 4) {
1046 if (err >= 0)
1047 err = -EINVAL;
1048 xenbus_dev_fatal(pdev->xdev, err,
1049 "Error reading PCI device %d", i);
1050 goto out;
1051 }
1052
Jiang Liu2ccc2462012-08-28 23:43:58 +08001053 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1054 PCI_DEVFN(slot, func));
Ryan Wilson956a9202010-08-02 21:31:05 -04001055 if (!pci_dev) {
1056 dev_dbg(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001057 "Cannot get PCI device %04x:%02x:%02x.%d\n",
Ryan Wilson956a9202010-08-02 21:31:05 -04001058 domain, bus, slot, func);
1059 continue;
1060 }
Rafael J. Wysockia83919e2014-01-10 15:29:19 +01001061 pci_lock_rescan_remove();
Yinghai Lu210647a2012-02-25 13:54:20 -08001062 pci_stop_and_remove_bus_device(pci_dev);
Ryan Wilson956a9202010-08-02 21:31:05 -04001063 pci_dev_put(pci_dev);
Rafael J. Wysockia83919e2014-01-10 15:29:19 +01001064 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -04001065
1066 dev_dbg(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001067 "PCI device %04x:%02x:%02x.%d removed.\n",
Ryan Wilson956a9202010-08-02 21:31:05 -04001068 domain, bus, slot, func);
1069 }
1070
1071 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1072
1073out:
1074 return err;
1075}
1076
1077static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1078 enum xenbus_state be_state)
1079{
1080 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1081
1082 switch (be_state) {
1083 case XenbusStateUnknown:
1084 case XenbusStateInitialising:
1085 case XenbusStateInitWait:
1086 case XenbusStateInitialised:
Ryan Wilson956a9202010-08-02 21:31:05 -04001087 break;
1088
1089 case XenbusStateConnected:
1090 pcifront_try_connect(pdev);
1091 break;
1092
David Vrabeld5af64d2012-10-18 11:03:36 +01001093 case XenbusStateClosed:
1094 if (xdev->state == XenbusStateClosed)
1095 break;
1096 /* Missed the backend's CLOSING state -- fallthrough */
Ryan Wilson956a9202010-08-02 21:31:05 -04001097 case XenbusStateClosing:
1098 dev_warn(&xdev->dev, "backend going away!\n");
1099 pcifront_try_disconnect(pdev);
1100 break;
1101
1102 case XenbusStateReconfiguring:
1103 pcifront_detach_devices(pdev);
1104 break;
1105
1106 case XenbusStateReconfigured:
1107 pcifront_attach_devices(pdev);
1108 break;
1109 }
1110}
1111
1112static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1113 const struct xenbus_device_id *id)
1114{
1115 int err = 0;
1116 struct pcifront_device *pdev = alloc_pdev(xdev);
1117
1118 if (pdev == NULL) {
1119 err = -ENOMEM;
1120 xenbus_dev_fatal(xdev, err,
1121 "Error allocating pcifront_device struct");
1122 goto out;
1123 }
1124
1125 err = pcifront_publish_info(pdev);
1126 if (err)
1127 free_pdev(pdev);
1128
1129out:
1130 return err;
1131}
1132
1133static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1134{
1135 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1136 if (pdev)
1137 free_pdev(pdev);
1138
1139 return 0;
1140}
1141
1142static const struct xenbus_device_id xenpci_ids[] = {
1143 {"pci"},
1144 {""},
1145};
1146
David Vrabel95afae42014-09-08 17:30:41 +01001147static struct xenbus_driver xenpci_driver = {
1148 .name = "pcifront",
1149 .ids = xenpci_ids,
Ryan Wilson956a9202010-08-02 21:31:05 -04001150 .probe = pcifront_xenbus_probe,
1151 .remove = pcifront_xenbus_remove,
1152 .otherend_changed = pcifront_backend_changed,
David Vrabel95afae42014-09-08 17:30:41 +01001153};
Ryan Wilson956a9202010-08-02 21:31:05 -04001154
1155static int __init pcifront_init(void)
1156{
1157 if (!xen_pv_domain() || xen_initial_domain())
1158 return -ENODEV;
1159
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001160 if (!xen_has_pv_devices())
1161 return -ENODEV;
1162
Ryan Wilson956a9202010-08-02 21:31:05 -04001163 pci_frontend_registrar(1 /* enable */);
1164
Jan Beulich73db1442011-12-22 09:08:13 +00001165 return xenbus_register_frontend(&xenpci_driver);
Ryan Wilson956a9202010-08-02 21:31:05 -04001166}
1167
1168static void __exit pcifront_cleanup(void)
1169{
Jan Beulich73db1442011-12-22 09:08:13 +00001170 xenbus_unregister_driver(&xenpci_driver);
Ryan Wilson956a9202010-08-02 21:31:05 -04001171 pci_frontend_registrar(0 /* disable */);
1172}
1173module_init(pcifront_init);
1174module_exit(pcifront_cleanup);
1175
1176MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1177MODULE_LICENSE("GPL");
1178MODULE_ALIAS("xen:pci");