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