xref: /linux/drivers/pci/xen-pcifront.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Xen PCI Frontend.
3  *
4  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5  */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <xen/xenbus.h>
10 #include <xen/events.h>
11 #include <xen/grant_table.h>
12 #include <xen/page.h>
13 #include <linux/spinlock.h>
14 #include <linux/pci.h>
15 #include <linux/msi.h>
16 #include <xen/interface/io/pciif.h>
17 #include <asm/xen/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/atomic.h>
20 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/time.h>
23 #include <linux/ktime.h>
24 #include <xen/platform_pci.h>
25 
26 #include <asm/xen/swiotlb-xen.h>
27 #define INVALID_GRANT_REF (0)
28 #define INVALID_EVTCHN    (-1)
29 
30 struct 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 
38 struct 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 
55 struct pcifront_sd {
56 	int domain;
57 	struct pcifront_device *pdev;
58 };
59 
60 static inline struct pcifront_device *
61 pcifront_get_pdev(struct pcifront_sd *sd)
62 {
63 	return sd->pdev;
64 }
65 
66 static 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 
74 static DEFINE_SPINLOCK(pcifront_dev_lock);
75 static struct pcifront_device *pcifront_dev;
76 
77 static int verbose_request;
78 module_param(verbose_request, int, 0644);
79 
80 static 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 
102 static 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 
111 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
112 {
113 	int err = 0;
114 	struct xen_pci_op *active_op = &pdev->sh_info->op;
115 	unsigned long irq_flags;
116 	evtchn_port_t port = pdev->evtchn;
117 	unsigned irq = pdev->irq;
118 	s64 ns, ns_timeout;
119 
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 	 */
135 	ns_timeout = ktime_get_ns() + 2 * (s64)NSEC_PER_SEC;
136 
137 	xen_clear_irq_pending(irq);
138 
139 	while (test_bit(_XEN_PCIF_active,
140 			(unsigned long *)&pdev->sh_info->flags)) {
141 		xen_poll_irq_timeout(irq, jiffies + 3*HZ);
142 		xen_clear_irq_pending(irq);
143 		ns = ktime_get_ns();
144 		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;
169 out:
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 */
175 static 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,
192 			 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
193 			 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 */
214 static 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,
231 			 "write dev=%04x:%02x:%02x.%d - "
232 			 "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 
239 static struct pci_ops pcifront_bus_ops = {
240 	.read = pcifront_bus_read,
241 	.write = pcifront_bus_write,
242 };
243 
244 #ifdef CONFIG_PCI_MSI
245 static int pci_frontend_enable_msix(struct pci_dev *dev,
246 				    int vector[], int nvec)
247 {
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 	for_each_pci_msi_entry(entry, dev) {
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 */
280 			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;
285 					vector[i] = -1;
286 					continue;
287 				}
288 				vector[i] = op.msix_entries[i].vector;
289 			}
290 		} else {
291 			printk(KERN_DEBUG "enable msix get value %x\n",
292 				op.value);
293 			err = op.value;
294 		}
295 	} else {
296 		dev_err(&dev->dev, "enable msix get err %x\n", err);
297 	}
298 	return err;
299 }
300 
301 static 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 
320 static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
321 {
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)) {
334 		vector[0] = op.value;
335 		if (op.value <= 0) {
336 			dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
337 				op.value);
338 			err = -EINVAL;
339 			vector[0] = -1;
340 		}
341 	} 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 
349 static 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 
372 static 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 
379 static 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
387 static 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 */
391 static 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)) {
404 				dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
405 					"Device offline. Try using e820_host=1 in the guest config.\n",
406 					pci_name(dev), i);
407 			}
408 		}
409 	}
410 
411 	return 0;
412 }
413 
414 static int pcifront_scan_bus(struct pcifront_device *pdev,
415 				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 "
436 				 "%04x:%02x:%02x.%d found.\n", domain, bus,
437 				 PCI_SLOT(devfn), PCI_FUNC(devfn));
438 	}
439 
440 	return 0;
441 }
442 
443 static int pcifront_scan_root(struct pcifront_device *pdev,
444 				 unsigned int domain, unsigned int bus)
445 {
446 	struct pci_bus *b;
447 	LIST_HEAD(resources);
448 	struct pcifront_sd *sd = NULL;
449 	struct pci_bus_entry *bus_entry = NULL;
450 	int err = 0;
451 	static struct resource busn_res = {
452 		.start = 0,
453 		.end = 255,
454 		.flags = IORESOURCE_BUS,
455 	};
456 
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 	}
477 	pci_add_resource(&resources, &ioport_resource);
478 	pci_add_resource(&resources, &iomem_resource);
479 	pci_add_resource(&resources, &busn_res);
480 	pcifront_init_sd(sd, domain, bus, pdev);
481 
482 	pci_lock_rescan_remove();
483 
484 	b = pci_scan_root_bus(&pdev->xdev->dev, bus,
485 				  &pcifront_bus_ops, sd, &resources);
486 	if (!b) {
487 		dev_err(&pdev->xdev->dev,
488 			"Error creating PCI Frontend Bus!\n");
489 		err = -ENOMEM;
490 		pci_unlock_rescan_remove();
491 		pci_free_resource_list(&resources);
492 		goto err_out;
493 	}
494 
495 	bus_entry->bus = b;
496 
497 	list_add(&bus_entry->list, &pdev->root_buses);
498 
499 	/* pci_scan_root_bus skips devices which do not have a
500 	* 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 
509 	pci_unlock_rescan_remove();
510 	return err;
511 
512 err_out:
513 	kfree(bus_entry);
514 	kfree(sd);
515 
516 	return err;
517 }
518 
519 static int pcifront_rescan_root(struct pcifront_device *pdev,
520 				   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 
554 static 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");
562 		pci_stop_and_remove_bus_device(dev);
563 	}
564 }
565 
566 static 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 
572 	pci_lock_rescan_remove();
573 	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 	}
585 	pci_unlock_rescan_remove();
586 }
587 
588 static 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) {
606 		dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
607 		pci_dev_put(pcidev);
608 		return result;
609 	}
610 	pdrv = pcidev->driver;
611 
612 	if (pdrv) {
613 		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 		}
642 	}
643 	if (!flag)
644 		result = PCI_ERS_RESULT_NONE;
645 
646 	return result;
647 }
648 
649 
650 static 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*/
672 	smp_mb__before_atomic();
673 	clear_bit(_PDEVB_op_active, &pdev->flags);
674 	smp_mb__after_atomic();
675 
676 	schedule_pcifront_aer_op(pdev);
677 
678 }
679 
680 static 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 }
686 static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
687 {
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;
695 	} else
696 		err = -EEXIST;
697 
698 	spin_unlock(&pcifront_dev_lock);
699 
700 	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 	}
705 	return err;
706 }
707 
708 static 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 }
720 static 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);
755 out:
756 	return pdev;
757 }
758 
759 static 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 
765 	cancel_work_sync(&pdev->op_work);
766 
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 
784 static int pcifront_publish_info(struct pcifront_device *pdev)
785 {
786 	int err = 0;
787 	struct xenbus_transaction trans;
788 	grant_ref_t gref;
789 
790 	err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
791 	if (err < 0)
792 		goto out;
793 
794 	pdev->gnt_ref = gref;
795 
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 
808 do_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 
847 out:
848 	return err;
849 }
850 
851 static int pcifront_try_connect(struct pcifront_device *pdev)
852 {
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 
864 	err = pcifront_connect_and_init_dma(pdev);
865 	if (err && err != -EEXIST) {
866 		xenbus_dev_fatal(pdev->xdev, err,
867 				 "Error setting up PCI Frontend");
868 		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);
877 		if (err) {
878 			xenbus_dev_fatal(pdev->xdev, err,
879 					 "Error scanning PCI root 0000:00");
880 			goto out;
881 		}
882 		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 
919 out:
920 	return err;
921 }
922 
923 static 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 
941 out:
942 
943 	return err;
944 }
945 
946 static int pcifront_attach_devices(struct pcifront_device *pdev)
947 {
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);
963 		if (err) {
964 			xenbus_dev_fatal(pdev->xdev, err,
965 					 "Error scanning PCI root 0000:00");
966 			goto out;
967 		}
968 		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 
1005 out:
1006 	return err;
1007 }
1008 
1009 static 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;
1014 	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 
1063 		pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1064 				PCI_DEVFN(slot, func));
1065 		if (!pci_dev) {
1066 			dev_dbg(&pdev->xdev->dev,
1067 				"Cannot get PCI device %04x:%02x:%02x.%d\n",
1068 				domain, bus, slot, func);
1069 			continue;
1070 		}
1071 		pci_lock_rescan_remove();
1072 		pci_stop_and_remove_bus_device(pci_dev);
1073 		pci_dev_put(pci_dev);
1074 		pci_unlock_rescan_remove();
1075 
1076 		dev_dbg(&pdev->xdev->dev,
1077 			"PCI device %04x:%02x:%02x.%d removed.\n",
1078 			domain, bus, slot, func);
1079 	}
1080 
1081 	err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1082 
1083 out:
1084 	return err;
1085 }
1086 
1087 static 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:
1097 		break;
1098 
1099 	case XenbusStateConnected:
1100 		pcifront_try_connect(pdev);
1101 		break;
1102 
1103 	case XenbusStateClosed:
1104 		if (xdev->state == XenbusStateClosed)
1105 			break;
1106 		/* Missed the backend's CLOSING state -- fallthrough */
1107 	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 
1122 static 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 
1139 out:
1140 	return err;
1141 }
1142 
1143 static 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 
1152 static const struct xenbus_device_id xenpci_ids[] = {
1153 	{"pci"},
1154 	{""},
1155 };
1156 
1157 static struct xenbus_driver xenpci_driver = {
1158 	.name			= "pcifront",
1159 	.ids			= xenpci_ids,
1160 	.probe			= pcifront_xenbus_probe,
1161 	.remove			= pcifront_xenbus_remove,
1162 	.otherend_changed	= pcifront_backend_changed,
1163 };
1164 
1165 static int __init pcifront_init(void)
1166 {
1167 	if (!xen_pv_domain() || xen_initial_domain())
1168 		return -ENODEV;
1169 
1170 	if (!xen_has_pv_devices())
1171 		return -ENODEV;
1172 
1173 	pci_frontend_registrar(1 /* enable */);
1174 
1175 	return xenbus_register_frontend(&xenpci_driver);
1176 }
1177 
1178 static void __exit pcifront_cleanup(void)
1179 {
1180 	xenbus_unregister_driver(&xenpci_driver);
1181 	pci_frontend_registrar(0 /* disable */);
1182 }
1183 module_init(pcifront_init);
1184 module_exit(pcifront_cleanup);
1185 
1186 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1187 MODULE_LICENSE("GPL");
1188 MODULE_ALIAS("xen:pci");
1189