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