blob: 8b7a900cd28b25e5a8d52a10357768df1d6513aa [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;
Arnd Bergmann515d4252015-04-28 17:32:33 +0800447 LIST_HEAD(resources);
Ryan Wilson956a9202010-08-02 21:31:05 -0400448 struct pcifront_sd *sd = NULL;
449 struct pci_bus_entry *bus_entry = NULL;
450 int err = 0;
Arnd Bergmann515d4252015-04-28 17:32:33 +0800451 static struct resource busn_res = {
452 .start = 0,
453 .end = 255,
454 .flags = IORESOURCE_BUS,
455 };
Ryan Wilson956a9202010-08-02 21:31:05 -0400456
457#ifndef CONFIG_PCI_DOMAINS
458 if (domain != 0) {
459 dev_err(&pdev->xdev->dev,
460 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
461 dev_err(&pdev->xdev->dev,
462 "Please compile with CONFIG_PCI_DOMAINS\n");
463 err = -EINVAL;
464 goto err_out;
465 }
466#endif
467
468 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
469 domain, bus);
470
471 bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
472 sd = kmalloc(sizeof(*sd), GFP_KERNEL);
473 if (!bus_entry || !sd) {
474 err = -ENOMEM;
475 goto err_out;
476 }
Arnd Bergmann515d4252015-04-28 17:32:33 +0800477 pci_add_resource(&resources, &ioport_resource);
478 pci_add_resource(&resources, &iomem_resource);
479 pci_add_resource(&resources, &busn_res);
Ryan Wilson956a9202010-08-02 21:31:05 -0400480 pcifront_init_sd(sd, domain, bus, pdev);
481
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100482 pci_lock_rescan_remove();
483
Arnd Bergmann515d4252015-04-28 17:32:33 +0800484 b = pci_scan_root_bus(&pdev->xdev->dev, bus,
485 &pcifront_bus_ops, sd, &resources);
Ryan Wilson956a9202010-08-02 21:31:05 -0400486 if (!b) {
487 dev_err(&pdev->xdev->dev,
488 "Error creating PCI Frontend Bus!\n");
489 err = -ENOMEM;
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100490 pci_unlock_rescan_remove();
Arnd Bergmann515d4252015-04-28 17:32:33 +0800491 pci_free_resource_list(&resources);
Ryan Wilson956a9202010-08-02 21:31:05 -0400492 goto err_out;
493 }
494
495 bus_entry->bus = b;
496
497 list_add(&bus_entry->list, &pdev->root_buses);
498
Arnd Bergmann515d4252015-04-28 17:32:33 +0800499 /* pci_scan_root_bus skips devices which do not have a
Ryan Wilson956a9202010-08-02 21:31:05 -0400500 * devfn==0. The pcifront_scan_bus enumerates all devfn. */
501 err = pcifront_scan_bus(pdev, domain, bus, b);
502
503 /* Claim resources before going "live" with our devices */
504 pci_walk_bus(b, pcifront_claim_resource, pdev);
505
506 /* Create SysFS and notify udev of the devices. Aka: "going live" */
507 pci_bus_add_devices(b);
508
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100509 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400510 return err;
511
512err_out:
513 kfree(bus_entry);
514 kfree(sd);
515
516 return err;
517}
518
Bill Pemberton15856ad2012-11-21 15:35:00 -0500519static int pcifront_rescan_root(struct pcifront_device *pdev,
Ryan Wilson956a9202010-08-02 21:31:05 -0400520 unsigned int domain, unsigned int bus)
521{
522 int err;
523 struct pci_bus *b;
524
525#ifndef CONFIG_PCI_DOMAINS
526 if (domain != 0) {
527 dev_err(&pdev->xdev->dev,
528 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
529 dev_err(&pdev->xdev->dev,
530 "Please compile with CONFIG_PCI_DOMAINS\n");
531 return -EINVAL;
532 }
533#endif
534
535 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
536 domain, bus);
537
538 b = pci_find_bus(domain, bus);
539 if (!b)
540 /* If the bus is unknown, create it. */
541 return pcifront_scan_root(pdev, domain, bus);
542
543 err = pcifront_scan_bus(pdev, domain, bus, b);
544
545 /* Claim resources before going "live" with our devices */
546 pci_walk_bus(b, pcifront_claim_resource, pdev);
547
548 /* Create SysFS and notify udev of the devices. Aka: "going live" */
549 pci_bus_add_devices(b);
550
551 return err;
552}
553
554static void free_root_bus_devs(struct pci_bus *bus)
555{
556 struct pci_dev *dev;
557
558 while (!list_empty(&bus->devices)) {
559 dev = container_of(bus->devices.next, struct pci_dev,
560 bus_list);
561 dev_dbg(&dev->dev, "removing device\n");
Yinghai Lu210647a2012-02-25 13:54:20 -0800562 pci_stop_and_remove_bus_device(dev);
Ryan Wilson956a9202010-08-02 21:31:05 -0400563 }
564}
565
566static void pcifront_free_roots(struct pcifront_device *pdev)
567{
568 struct pci_bus_entry *bus_entry, *t;
569
570 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
571
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100572 pci_lock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400573 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
574 list_del(&bus_entry->list);
575
576 free_root_bus_devs(bus_entry->bus);
577
578 kfree(bus_entry->bus->sysdata);
579
580 device_unregister(bus_entry->bus->bridge);
581 pci_remove_bus(bus_entry->bus);
582
583 kfree(bus_entry);
584 }
Rafael J. Wysockia83919e2014-01-10 15:29:19 +0100585 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -0400586}
587
588static pci_ers_result_t pcifront_common_process(int cmd,
589 struct pcifront_device *pdev,
590 pci_channel_state_t state)
591{
592 pci_ers_result_t result;
593 struct pci_driver *pdrv;
594 int bus = pdev->sh_info->aer_op.bus;
595 int devfn = pdev->sh_info->aer_op.devfn;
596 struct pci_dev *pcidev;
597 int flag = 0;
598
599 dev_dbg(&pdev->xdev->dev,
600 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
601 cmd, bus, devfn);
602 result = PCI_ERS_RESULT_NONE;
603
604 pcidev = pci_get_bus_and_slot(bus, devfn);
605 if (!pcidev || !pcidev->driver) {
Jiri Slaby2a63dd72010-11-04 15:31:30 +0100606 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
Markus Elfringff0387c2014-11-10 21:02:17 -0700607 pci_dev_put(pcidev);
Ryan Wilson956a9202010-08-02 21:31:05 -0400608 return result;
609 }
610 pdrv = pcidev->driver;
611
Alan Stern07d25142012-01-27 10:24:40 -0500612 if (pdrv) {
Ryan Wilson956a9202010-08-02 21:31:05 -0400613 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
614 dev_dbg(&pcidev->dev,
615 "trying to call AER service\n");
616 if (pcidev) {
617 flag = 1;
618 switch (cmd) {
619 case XEN_PCI_OP_aer_detected:
620 result = pdrv->err_handler->
621 error_detected(pcidev, state);
622 break;
623 case XEN_PCI_OP_aer_mmio:
624 result = pdrv->err_handler->
625 mmio_enabled(pcidev);
626 break;
627 case XEN_PCI_OP_aer_slotreset:
628 result = pdrv->err_handler->
629 slot_reset(pcidev);
630 break;
631 case XEN_PCI_OP_aer_resume:
632 pdrv->err_handler->resume(pcidev);
633 break;
634 default:
635 dev_err(&pdev->xdev->dev,
636 "bad request in aer recovery "
637 "operation!\n");
638
639 }
640 }
641 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400642 }
643 if (!flag)
644 result = PCI_ERS_RESULT_NONE;
645
646 return result;
647}
648
649
650static void pcifront_do_aer(struct work_struct *data)
651{
652 struct pcifront_device *pdev =
653 container_of(data, struct pcifront_device, op_work);
654 int cmd = pdev->sh_info->aer_op.cmd;
655 pci_channel_state_t state =
656 (pci_channel_state_t)pdev->sh_info->aer_op.err;
657
658 /*If a pci_conf op is in progress,
659 we have to wait until it is done before service aer op*/
660 dev_dbg(&pdev->xdev->dev,
661 "pcifront service aer bus %x devfn %x\n",
662 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
663
664 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
665
666 /* Post the operation to the guest. */
667 wmb();
668 clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
669 notify_remote_via_evtchn(pdev->evtchn);
670
671 /*in case of we lost an aer request in four lines time_window*/
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100672 smp_mb__before_atomic();
Ryan Wilson956a9202010-08-02 21:31:05 -0400673 clear_bit(_PDEVB_op_active, &pdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100674 smp_mb__after_atomic();
Ryan Wilson956a9202010-08-02 21:31:05 -0400675
676 schedule_pcifront_aer_op(pdev);
677
678}
679
680static irqreturn_t pcifront_handler_aer(int irq, void *dev)
681{
682 struct pcifront_device *pdev = dev;
683 schedule_pcifront_aer_op(pdev);
684 return IRQ_HANDLED;
685}
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400686static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400687{
688 int err = 0;
689
690 spin_lock(&pcifront_dev_lock);
691
692 if (!pcifront_dev) {
693 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
694 pcifront_dev = pdev;
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400695 } else
Ryan Wilson956a9202010-08-02 21:31:05 -0400696 err = -EEXIST;
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400697
Ryan Wilson956a9202010-08-02 21:31:05 -0400698 spin_unlock(&pcifront_dev_lock);
699
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400700 if (!err && !swiotlb_nr_tbl()) {
701 err = pci_xen_swiotlb_init_late();
702 if (err)
703 dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
704 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400705 return err;
706}
707
708static void pcifront_disconnect(struct pcifront_device *pdev)
709{
710 spin_lock(&pcifront_dev_lock);
711
712 if (pdev == pcifront_dev) {
713 dev_info(&pdev->xdev->dev,
714 "Disconnecting PCI Frontend Buses\n");
715 pcifront_dev = NULL;
716 }
717
718 spin_unlock(&pcifront_dev_lock);
719}
720static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
721{
722 struct pcifront_device *pdev;
723
724 pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
725 if (pdev == NULL)
726 goto out;
727
728 pdev->sh_info =
729 (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
730 if (pdev->sh_info == NULL) {
731 kfree(pdev);
732 pdev = NULL;
733 goto out;
734 }
735 pdev->sh_info->flags = 0;
736
737 /*Flag for registering PV AER handler*/
738 set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
739
740 dev_set_drvdata(&xdev->dev, pdev);
741 pdev->xdev = xdev;
742
743 INIT_LIST_HEAD(&pdev->root_buses);
744
745 spin_lock_init(&pdev->sh_info_lock);
746
747 pdev->evtchn = INVALID_EVTCHN;
748 pdev->gnt_ref = INVALID_GRANT_REF;
749 pdev->irq = -1;
750
751 INIT_WORK(&pdev->op_work, pcifront_do_aer);
752
753 dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
754 pdev, pdev->sh_info);
755out:
756 return pdev;
757}
758
759static void free_pdev(struct pcifront_device *pdev)
760{
761 dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
762
763 pcifront_free_roots(pdev);
764
Tejun Heodb2e2e62011-01-24 15:43:03 +0100765 cancel_work_sync(&pdev->op_work);
Ryan Wilson956a9202010-08-02 21:31:05 -0400766
767 if (pdev->irq >= 0)
768 unbind_from_irqhandler(pdev->irq, pdev);
769
770 if (pdev->evtchn != INVALID_EVTCHN)
771 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
772
773 if (pdev->gnt_ref != INVALID_GRANT_REF)
774 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
775 (unsigned long)pdev->sh_info);
776 else
777 free_page((unsigned long)pdev->sh_info);
778
779 dev_set_drvdata(&pdev->xdev->dev, NULL);
780
781 kfree(pdev);
782}
783
784static int pcifront_publish_info(struct pcifront_device *pdev)
785{
786 int err = 0;
787 struct xenbus_transaction trans;
Wei Liuccc9d902015-04-03 14:44:59 +0800788 grant_ref_t gref;
Ryan Wilson956a9202010-08-02 21:31:05 -0400789
Wei Liuccc9d902015-04-03 14:44:59 +0800790 err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
Ryan Wilson956a9202010-08-02 21:31:05 -0400791 if (err < 0)
792 goto out;
793
Wei Liuccc9d902015-04-03 14:44:59 +0800794 pdev->gnt_ref = gref;
Ryan Wilson956a9202010-08-02 21:31:05 -0400795
796 err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
797 if (err)
798 goto out;
799
800 err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
801 0, "pcifront", pdev);
802
803 if (err < 0)
804 return err;
805
806 pdev->irq = err;
807
808do_publish:
809 err = xenbus_transaction_start(&trans);
810 if (err) {
811 xenbus_dev_fatal(pdev->xdev, err,
812 "Error writing configuration for backend "
813 "(start transaction)");
814 goto out;
815 }
816
817 err = xenbus_printf(trans, pdev->xdev->nodename,
818 "pci-op-ref", "%u", pdev->gnt_ref);
819 if (!err)
820 err = xenbus_printf(trans, pdev->xdev->nodename,
821 "event-channel", "%u", pdev->evtchn);
822 if (!err)
823 err = xenbus_printf(trans, pdev->xdev->nodename,
824 "magic", XEN_PCI_MAGIC);
825
826 if (err) {
827 xenbus_transaction_end(trans, 1);
828 xenbus_dev_fatal(pdev->xdev, err,
829 "Error writing configuration for backend");
830 goto out;
831 } else {
832 err = xenbus_transaction_end(trans, 0);
833 if (err == -EAGAIN)
834 goto do_publish;
835 else if (err) {
836 xenbus_dev_fatal(pdev->xdev, err,
837 "Error completing transaction "
838 "for backend");
839 goto out;
840 }
841 }
842
843 xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
844
845 dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
846
847out:
848 return err;
849}
850
Bill Pemberton15856ad2012-11-21 15:35:00 -0500851static int pcifront_try_connect(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400852{
853 int err = -EFAULT;
854 int i, num_roots, len;
855 char str[64];
856 unsigned int domain, bus;
857
858
859 /* Only connect once */
860 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
861 XenbusStateInitialised)
862 goto out;
863
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400864 err = pcifront_connect_and_init_dma(pdev);
Konrad Rzeszutek Wilk098b1ae2013-06-10 16:48:09 -0400865 if (err && err != -EEXIST) {
Ryan Wilson956a9202010-08-02 21:31:05 -0400866 xenbus_dev_fatal(pdev->xdev, err,
Konrad Rzeszutek Wilk3d925322012-07-31 06:26:59 -0400867 "Error setting up PCI Frontend");
Ryan Wilson956a9202010-08-02 21:31:05 -0400868 goto out;
869 }
870
871 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
872 "root_num", "%d", &num_roots);
873 if (err == -ENOENT) {
874 xenbus_dev_error(pdev->xdev, err,
875 "No PCI Roots found, trying 0000:00");
876 err = pcifront_scan_root(pdev, 0, 0);
Chen Gang23cf1d02014-10-06 11:04:45 +0800877 if (err) {
878 xenbus_dev_fatal(pdev->xdev, err,
879 "Error scanning PCI root 0000:00");
880 goto out;
881 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400882 num_roots = 0;
883 } else if (err != 1) {
884 if (err == 0)
885 err = -EINVAL;
886 xenbus_dev_fatal(pdev->xdev, err,
887 "Error reading number of PCI roots");
888 goto out;
889 }
890
891 for (i = 0; i < num_roots; i++) {
892 len = snprintf(str, sizeof(str), "root-%d", i);
893 if (unlikely(len >= (sizeof(str) - 1))) {
894 err = -ENOMEM;
895 goto out;
896 }
897
898 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
899 "%x:%x", &domain, &bus);
900 if (err != 2) {
901 if (err >= 0)
902 err = -EINVAL;
903 xenbus_dev_fatal(pdev->xdev, err,
904 "Error reading PCI root %d", i);
905 goto out;
906 }
907
908 err = pcifront_scan_root(pdev, domain, bus);
909 if (err) {
910 xenbus_dev_fatal(pdev->xdev, err,
911 "Error scanning PCI root %04x:%02x",
912 domain, bus);
913 goto out;
914 }
915 }
916
917 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
918
919out:
920 return err;
921}
922
923static int pcifront_try_disconnect(struct pcifront_device *pdev)
924{
925 int err = 0;
926 enum xenbus_state prev_state;
927
928
929 prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
930
931 if (prev_state >= XenbusStateClosing)
932 goto out;
933
934 if (prev_state == XenbusStateConnected) {
935 pcifront_free_roots(pdev);
936 pcifront_disconnect(pdev);
937 }
938
939 err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
940
941out:
942
943 return err;
944}
945
Bill Pemberton15856ad2012-11-21 15:35:00 -0500946static int pcifront_attach_devices(struct pcifront_device *pdev)
Ryan Wilson956a9202010-08-02 21:31:05 -0400947{
948 int err = -EFAULT;
949 int i, num_roots, len;
950 unsigned int domain, bus;
951 char str[64];
952
953 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
954 XenbusStateReconfiguring)
955 goto out;
956
957 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
958 "root_num", "%d", &num_roots);
959 if (err == -ENOENT) {
960 xenbus_dev_error(pdev->xdev, err,
961 "No PCI Roots found, trying 0000:00");
962 err = pcifront_rescan_root(pdev, 0, 0);
Chen Gang23cf1d02014-10-06 11:04:45 +0800963 if (err) {
964 xenbus_dev_fatal(pdev->xdev, err,
965 "Error scanning PCI root 0000:00");
966 goto out;
967 }
Ryan Wilson956a9202010-08-02 21:31:05 -0400968 num_roots = 0;
969 } else if (err != 1) {
970 if (err == 0)
971 err = -EINVAL;
972 xenbus_dev_fatal(pdev->xdev, err,
973 "Error reading number of PCI roots");
974 goto out;
975 }
976
977 for (i = 0; i < num_roots; i++) {
978 len = snprintf(str, sizeof(str), "root-%d", i);
979 if (unlikely(len >= (sizeof(str) - 1))) {
980 err = -ENOMEM;
981 goto out;
982 }
983
984 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
985 "%x:%x", &domain, &bus);
986 if (err != 2) {
987 if (err >= 0)
988 err = -EINVAL;
989 xenbus_dev_fatal(pdev->xdev, err,
990 "Error reading PCI root %d", i);
991 goto out;
992 }
993
994 err = pcifront_rescan_root(pdev, domain, bus);
995 if (err) {
996 xenbus_dev_fatal(pdev->xdev, err,
997 "Error scanning PCI root %04x:%02x",
998 domain, bus);
999 goto out;
1000 }
1001 }
1002
1003 xenbus_switch_state(pdev->xdev, XenbusStateConnected);
1004
1005out:
1006 return err;
1007}
1008
1009static int pcifront_detach_devices(struct pcifront_device *pdev)
1010{
1011 int err = 0;
1012 int i, num_devs;
1013 unsigned int domain, bus, slot, func;
Ryan Wilson956a9202010-08-02 21:31:05 -04001014 struct pci_dev *pci_dev;
1015 char str[64];
1016
1017 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1018 XenbusStateConnected)
1019 goto out;
1020
1021 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1022 &num_devs);
1023 if (err != 1) {
1024 if (err >= 0)
1025 err = -EINVAL;
1026 xenbus_dev_fatal(pdev->xdev, err,
1027 "Error reading number of PCI devices");
1028 goto out;
1029 }
1030
1031 /* Find devices being detached and remove them. */
1032 for (i = 0; i < num_devs; i++) {
1033 int l, state;
1034 l = snprintf(str, sizeof(str), "state-%d", i);
1035 if (unlikely(l >= (sizeof(str) - 1))) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1040 &state);
1041 if (err != 1)
1042 state = XenbusStateUnknown;
1043
1044 if (state != XenbusStateClosing)
1045 continue;
1046
1047 /* Remove device. */
1048 l = snprintf(str, sizeof(str), "vdev-%d", i);
1049 if (unlikely(l >= (sizeof(str) - 1))) {
1050 err = -ENOMEM;
1051 goto out;
1052 }
1053 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1054 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1055 if (err != 4) {
1056 if (err >= 0)
1057 err = -EINVAL;
1058 xenbus_dev_fatal(pdev->xdev, err,
1059 "Error reading PCI device %d", i);
1060 goto out;
1061 }
1062
Jiang Liu2ccc2462012-08-28 23:43:58 +08001063 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1064 PCI_DEVFN(slot, func));
Ryan Wilson956a9202010-08-02 21:31:05 -04001065 if (!pci_dev) {
1066 dev_dbg(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001067 "Cannot get PCI device %04x:%02x:%02x.%d\n",
Ryan Wilson956a9202010-08-02 21:31:05 -04001068 domain, bus, slot, func);
1069 continue;
1070 }
Rafael J. Wysockia83919e2014-01-10 15:29:19 +01001071 pci_lock_rescan_remove();
Yinghai Lu210647a2012-02-25 13:54:20 -08001072 pci_stop_and_remove_bus_device(pci_dev);
Ryan Wilson956a9202010-08-02 21:31:05 -04001073 pci_dev_put(pci_dev);
Rafael J. Wysockia83919e2014-01-10 15:29:19 +01001074 pci_unlock_rescan_remove();
Ryan Wilson956a9202010-08-02 21:31:05 -04001075
1076 dev_dbg(&pdev->xdev->dev,
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -05001077 "PCI device %04x:%02x:%02x.%d removed.\n",
Ryan Wilson956a9202010-08-02 21:31:05 -04001078 domain, bus, slot, func);
1079 }
1080
1081 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1082
1083out:
1084 return err;
1085}
1086
1087static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1088 enum xenbus_state be_state)
1089{
1090 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1091
1092 switch (be_state) {
1093 case XenbusStateUnknown:
1094 case XenbusStateInitialising:
1095 case XenbusStateInitWait:
1096 case XenbusStateInitialised:
Ryan Wilson956a9202010-08-02 21:31:05 -04001097 break;
1098
1099 case XenbusStateConnected:
1100 pcifront_try_connect(pdev);
1101 break;
1102
David Vrabeld5af64d2012-10-18 11:03:36 +01001103 case XenbusStateClosed:
1104 if (xdev->state == XenbusStateClosed)
1105 break;
1106 /* Missed the backend's CLOSING state -- fallthrough */
Ryan Wilson956a9202010-08-02 21:31:05 -04001107 case XenbusStateClosing:
1108 dev_warn(&xdev->dev, "backend going away!\n");
1109 pcifront_try_disconnect(pdev);
1110 break;
1111
1112 case XenbusStateReconfiguring:
1113 pcifront_detach_devices(pdev);
1114 break;
1115
1116 case XenbusStateReconfigured:
1117 pcifront_attach_devices(pdev);
1118 break;
1119 }
1120}
1121
1122static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1123 const struct xenbus_device_id *id)
1124{
1125 int err = 0;
1126 struct pcifront_device *pdev = alloc_pdev(xdev);
1127
1128 if (pdev == NULL) {
1129 err = -ENOMEM;
1130 xenbus_dev_fatal(xdev, err,
1131 "Error allocating pcifront_device struct");
1132 goto out;
1133 }
1134
1135 err = pcifront_publish_info(pdev);
1136 if (err)
1137 free_pdev(pdev);
1138
1139out:
1140 return err;
1141}
1142
1143static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1144{
1145 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1146 if (pdev)
1147 free_pdev(pdev);
1148
1149 return 0;
1150}
1151
1152static const struct xenbus_device_id xenpci_ids[] = {
1153 {"pci"},
1154 {""},
1155};
1156
David Vrabel95afae42014-09-08 17:30:41 +01001157static struct xenbus_driver xenpci_driver = {
1158 .name = "pcifront",
1159 .ids = xenpci_ids,
Ryan Wilson956a9202010-08-02 21:31:05 -04001160 .probe = pcifront_xenbus_probe,
1161 .remove = pcifront_xenbus_remove,
1162 .otherend_changed = pcifront_backend_changed,
David Vrabel95afae42014-09-08 17:30:41 +01001163};
Ryan Wilson956a9202010-08-02 21:31:05 -04001164
1165static int __init pcifront_init(void)
1166{
1167 if (!xen_pv_domain() || xen_initial_domain())
1168 return -ENODEV;
1169
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001170 if (!xen_has_pv_devices())
1171 return -ENODEV;
1172
Ryan Wilson956a9202010-08-02 21:31:05 -04001173 pci_frontend_registrar(1 /* enable */);
1174
Jan Beulich73db1442011-12-22 09:08:13 +00001175 return xenbus_register_frontend(&xenpci_driver);
Ryan Wilson956a9202010-08-02 21:31:05 -04001176}
1177
1178static void __exit pcifront_cleanup(void)
1179{
Jan Beulich73db1442011-12-22 09:08:13 +00001180 xenbus_unregister_driver(&xenpci_driver);
Ryan Wilson956a9202010-08-02 21:31:05 -04001181 pci_frontend_registrar(0 /* disable */);
1182}
1183module_init(pcifront_init);
1184module_exit(pcifront_cleanup);
1185
1186MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1187MODULE_LICENSE("GPL");
1188MODULE_ALIAS("xen:pci");