xref: /linux/drivers/pci/xen-pcifront.c (revision df62ab5e0f75608919df7442654b0fab78246b7b)
17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
2956a9202SRyan Wilson /*
3*df62ab5eSBjorn Helgaas  * Xen PCI Frontend
4956a9202SRyan Wilson  *
5956a9202SRyan Wilson  * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6956a9202SRyan Wilson  */
7956a9202SRyan Wilson #include <linux/module.h>
8956a9202SRyan Wilson #include <linux/init.h>
9956a9202SRyan Wilson #include <linux/mm.h>
10956a9202SRyan Wilson #include <xen/xenbus.h>
11956a9202SRyan Wilson #include <xen/events.h>
12956a9202SRyan Wilson #include <xen/grant_table.h>
13956a9202SRyan Wilson #include <xen/page.h>
14956a9202SRyan Wilson #include <linux/spinlock.h>
15956a9202SRyan Wilson #include <linux/pci.h>
16956a9202SRyan Wilson #include <linux/msi.h>
17956a9202SRyan Wilson #include <xen/interface/io/pciif.h>
18956a9202SRyan Wilson #include <asm/xen/pci.h>
19956a9202SRyan Wilson #include <linux/interrupt.h>
2060063497SArun Sharma #include <linux/atomic.h>
21956a9202SRyan Wilson #include <linux/workqueue.h>
22956a9202SRyan Wilson #include <linux/bitops.h>
23956a9202SRyan Wilson #include <linux/time.h>
24e1d5bbcdSTina Ruchandani #include <linux/ktime.h>
2551c71a3bSKonrad Rzeszutek Wilk #include <xen/platform_pci.h>
26956a9202SRyan Wilson 
273d925320SKonrad Rzeszutek Wilk #include <asm/xen/swiotlb-xen.h>
28956a9202SRyan Wilson #define INVALID_GRANT_REF (0)
29956a9202SRyan Wilson #define INVALID_EVTCHN    (-1)
30956a9202SRyan Wilson 
31956a9202SRyan Wilson struct pci_bus_entry {
32956a9202SRyan Wilson 	struct list_head list;
33956a9202SRyan Wilson 	struct pci_bus *bus;
34956a9202SRyan Wilson };
35956a9202SRyan Wilson 
36956a9202SRyan Wilson #define _PDEVB_op_active		(0)
37956a9202SRyan Wilson #define PDEVB_op_active			(1 << (_PDEVB_op_active))
38956a9202SRyan Wilson 
39956a9202SRyan Wilson struct pcifront_device {
40956a9202SRyan Wilson 	struct xenbus_device *xdev;
41956a9202SRyan Wilson 	struct list_head root_buses;
42956a9202SRyan Wilson 
43956a9202SRyan Wilson 	int evtchn;
44956a9202SRyan Wilson 	int gnt_ref;
45956a9202SRyan Wilson 
46956a9202SRyan Wilson 	int irq;
47956a9202SRyan Wilson 
48956a9202SRyan Wilson 	/* Lock this when doing any operations in sh_info */
49956a9202SRyan Wilson 	spinlock_t sh_info_lock;
50956a9202SRyan Wilson 	struct xen_pci_sharedinfo *sh_info;
51956a9202SRyan Wilson 	struct work_struct op_work;
52956a9202SRyan Wilson 	unsigned long flags;
53956a9202SRyan Wilson 
54956a9202SRyan Wilson };
55956a9202SRyan Wilson 
56956a9202SRyan Wilson struct pcifront_sd {
574d8c8bd6SKonrad Rzeszutek Wilk 	struct pci_sysdata sd;
58956a9202SRyan Wilson 	struct pcifront_device *pdev;
59956a9202SRyan Wilson };
60956a9202SRyan Wilson 
61956a9202SRyan Wilson static inline struct pcifront_device *
62956a9202SRyan Wilson pcifront_get_pdev(struct pcifront_sd *sd)
63956a9202SRyan Wilson {
64956a9202SRyan Wilson 	return sd->pdev;
65956a9202SRyan Wilson }
66956a9202SRyan Wilson 
67956a9202SRyan Wilson static inline void pcifront_init_sd(struct pcifront_sd *sd,
68956a9202SRyan Wilson 				    unsigned int domain, unsigned int bus,
69956a9202SRyan Wilson 				    struct pcifront_device *pdev)
70956a9202SRyan Wilson {
714d8c8bd6SKonrad Rzeszutek Wilk 	/* Because we do not expose that information via XenBus. */
724d8c8bd6SKonrad Rzeszutek Wilk 	sd->sd.node = first_online_node;
734d8c8bd6SKonrad Rzeszutek Wilk 	sd->sd.domain = domain;
74956a9202SRyan Wilson 	sd->pdev = pdev;
75956a9202SRyan Wilson }
76956a9202SRyan Wilson 
77956a9202SRyan Wilson static DEFINE_SPINLOCK(pcifront_dev_lock);
78956a9202SRyan Wilson static struct pcifront_device *pcifront_dev;
79956a9202SRyan Wilson 
80956a9202SRyan Wilson static int verbose_request;
81956a9202SRyan Wilson module_param(verbose_request, int, 0644);
82956a9202SRyan Wilson 
83956a9202SRyan Wilson static int errno_to_pcibios_err(int errno)
84956a9202SRyan Wilson {
85956a9202SRyan Wilson 	switch (errno) {
86956a9202SRyan Wilson 	case XEN_PCI_ERR_success:
87956a9202SRyan Wilson 		return PCIBIOS_SUCCESSFUL;
88956a9202SRyan Wilson 
89956a9202SRyan Wilson 	case XEN_PCI_ERR_dev_not_found:
90956a9202SRyan Wilson 		return PCIBIOS_DEVICE_NOT_FOUND;
91956a9202SRyan Wilson 
92956a9202SRyan Wilson 	case XEN_PCI_ERR_invalid_offset:
93956a9202SRyan Wilson 	case XEN_PCI_ERR_op_failed:
94956a9202SRyan Wilson 		return PCIBIOS_BAD_REGISTER_NUMBER;
95956a9202SRyan Wilson 
96956a9202SRyan Wilson 	case XEN_PCI_ERR_not_implemented:
97956a9202SRyan Wilson 		return PCIBIOS_FUNC_NOT_SUPPORTED;
98956a9202SRyan Wilson 
99956a9202SRyan Wilson 	case XEN_PCI_ERR_access_denied:
100956a9202SRyan Wilson 		return PCIBIOS_SET_FAILED;
101956a9202SRyan Wilson 	}
102956a9202SRyan Wilson 	return errno;
103956a9202SRyan Wilson }
104956a9202SRyan Wilson 
105956a9202SRyan Wilson static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
106956a9202SRyan Wilson {
107956a9202SRyan Wilson 	if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
108956a9202SRyan Wilson 		&& !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
109956a9202SRyan Wilson 		dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
110956a9202SRyan Wilson 		schedule_work(&pdev->op_work);
111956a9202SRyan Wilson 	}
112956a9202SRyan Wilson }
113956a9202SRyan Wilson 
114956a9202SRyan Wilson static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
115956a9202SRyan Wilson {
116956a9202SRyan Wilson 	int err = 0;
117956a9202SRyan Wilson 	struct xen_pci_op *active_op = &pdev->sh_info->op;
118956a9202SRyan Wilson 	unsigned long irq_flags;
119956a9202SRyan Wilson 	evtchn_port_t port = pdev->evtchn;
120956a9202SRyan Wilson 	unsigned irq = pdev->irq;
121956a9202SRyan Wilson 	s64 ns, ns_timeout;
122956a9202SRyan Wilson 
123956a9202SRyan Wilson 	spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
124956a9202SRyan Wilson 
125956a9202SRyan Wilson 	memcpy(active_op, op, sizeof(struct xen_pci_op));
126956a9202SRyan Wilson 
127956a9202SRyan Wilson 	/* Go */
128956a9202SRyan Wilson 	wmb();
129956a9202SRyan Wilson 	set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
130956a9202SRyan Wilson 	notify_remote_via_evtchn(port);
131956a9202SRyan Wilson 
132956a9202SRyan Wilson 	/*
133956a9202SRyan Wilson 	 * We set a poll timeout of 3 seconds but give up on return after
134956a9202SRyan Wilson 	 * 2 seconds. It is better to time out too late rather than too early
135956a9202SRyan Wilson 	 * (in the latter case we end up continually re-executing poll() with a
136956a9202SRyan Wilson 	 * timeout in the past). 1s difference gives plenty of slack for error.
137956a9202SRyan Wilson 	 */
138e1d5bbcdSTina Ruchandani 	ns_timeout = ktime_get_ns() + 2 * (s64)NSEC_PER_SEC;
139956a9202SRyan Wilson 
140956a9202SRyan Wilson 	xen_clear_irq_pending(irq);
141956a9202SRyan Wilson 
142956a9202SRyan Wilson 	while (test_bit(_XEN_PCIF_active,
143956a9202SRyan Wilson 			(unsigned long *)&pdev->sh_info->flags)) {
144956a9202SRyan Wilson 		xen_poll_irq_timeout(irq, jiffies + 3*HZ);
145956a9202SRyan Wilson 		xen_clear_irq_pending(irq);
146e1d5bbcdSTina Ruchandani 		ns = ktime_get_ns();
147956a9202SRyan Wilson 		if (ns > ns_timeout) {
148956a9202SRyan Wilson 			dev_err(&pdev->xdev->dev,
149956a9202SRyan Wilson 				"pciback not responding!!!\n");
150956a9202SRyan Wilson 			clear_bit(_XEN_PCIF_active,
151956a9202SRyan Wilson 				  (unsigned long *)&pdev->sh_info->flags);
152956a9202SRyan Wilson 			err = XEN_PCI_ERR_dev_not_found;
153956a9202SRyan Wilson 			goto out;
154956a9202SRyan Wilson 		}
155956a9202SRyan Wilson 	}
156956a9202SRyan Wilson 
157956a9202SRyan Wilson 	/*
158956a9202SRyan Wilson 	* We might lose backend service request since we
159956a9202SRyan Wilson 	* reuse same evtchn with pci_conf backend response. So re-schedule
160956a9202SRyan Wilson 	* aer pcifront service.
161956a9202SRyan Wilson 	*/
162956a9202SRyan Wilson 	if (test_bit(_XEN_PCIB_active,
163956a9202SRyan Wilson 			(unsigned long *)&pdev->sh_info->flags)) {
164956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
165956a9202SRyan Wilson 			"schedule aer pcifront service\n");
166956a9202SRyan Wilson 		schedule_pcifront_aer_op(pdev);
167956a9202SRyan Wilson 	}
168956a9202SRyan Wilson 
169956a9202SRyan Wilson 	memcpy(op, active_op, sizeof(struct xen_pci_op));
170956a9202SRyan Wilson 
171956a9202SRyan Wilson 	err = op->err;
172956a9202SRyan Wilson out:
173956a9202SRyan Wilson 	spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
174956a9202SRyan Wilson 	return err;
175956a9202SRyan Wilson }
176956a9202SRyan Wilson 
177956a9202SRyan Wilson /* Access to this function is spinlocked in drivers/pci/access.c */
178956a9202SRyan Wilson static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
179956a9202SRyan Wilson 			     int where, int size, u32 *val)
180956a9202SRyan Wilson {
181956a9202SRyan Wilson 	int err = 0;
182956a9202SRyan Wilson 	struct xen_pci_op op = {
183956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_conf_read,
184956a9202SRyan Wilson 		.domain = pci_domain_nr(bus),
185956a9202SRyan Wilson 		.bus    = bus->number,
186956a9202SRyan Wilson 		.devfn  = devfn,
187956a9202SRyan Wilson 		.offset = where,
188956a9202SRyan Wilson 		.size   = size,
189956a9202SRyan Wilson 	};
190956a9202SRyan Wilson 	struct pcifront_sd *sd = bus->sysdata;
191956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
192956a9202SRyan Wilson 
193956a9202SRyan Wilson 	if (verbose_request)
194956a9202SRyan Wilson 		dev_info(&pdev->xdev->dev,
195e4de866aSKonrad Rzeszutek Wilk 			 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
196956a9202SRyan Wilson 			 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
197956a9202SRyan Wilson 			 PCI_FUNC(devfn), where, size);
198956a9202SRyan Wilson 
199956a9202SRyan Wilson 	err = do_pci_op(pdev, &op);
200956a9202SRyan Wilson 
201956a9202SRyan Wilson 	if (likely(!err)) {
202956a9202SRyan Wilson 		if (verbose_request)
203956a9202SRyan Wilson 			dev_info(&pdev->xdev->dev, "read got back value %x\n",
204956a9202SRyan Wilson 				 op.value);
205956a9202SRyan Wilson 
206956a9202SRyan Wilson 		*val = op.value;
207956a9202SRyan Wilson 	} else if (err == -ENODEV) {
208956a9202SRyan Wilson 		/* No device here, pretend that it just returned 0 */
209956a9202SRyan Wilson 		err = 0;
210956a9202SRyan Wilson 		*val = 0;
211956a9202SRyan Wilson 	}
212956a9202SRyan Wilson 
213956a9202SRyan Wilson 	return errno_to_pcibios_err(err);
214956a9202SRyan Wilson }
215956a9202SRyan Wilson 
216956a9202SRyan Wilson /* Access to this function is spinlocked in drivers/pci/access.c */
217956a9202SRyan Wilson static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
218956a9202SRyan Wilson 			      int where, int size, u32 val)
219956a9202SRyan Wilson {
220956a9202SRyan Wilson 	struct xen_pci_op op = {
221956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_conf_write,
222956a9202SRyan Wilson 		.domain = pci_domain_nr(bus),
223956a9202SRyan Wilson 		.bus    = bus->number,
224956a9202SRyan Wilson 		.devfn  = devfn,
225956a9202SRyan Wilson 		.offset = where,
226956a9202SRyan Wilson 		.size   = size,
227956a9202SRyan Wilson 		.value  = val,
228956a9202SRyan Wilson 	};
229956a9202SRyan Wilson 	struct pcifront_sd *sd = bus->sysdata;
230956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
231956a9202SRyan Wilson 
232956a9202SRyan Wilson 	if (verbose_request)
233956a9202SRyan Wilson 		dev_info(&pdev->xdev->dev,
234e4de866aSKonrad Rzeszutek Wilk 			 "write dev=%04x:%02x:%02x.%d - "
235956a9202SRyan Wilson 			 "offset %x size %d val %x\n",
236956a9202SRyan Wilson 			 pci_domain_nr(bus), bus->number,
237956a9202SRyan Wilson 			 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
238956a9202SRyan Wilson 
239956a9202SRyan Wilson 	return errno_to_pcibios_err(do_pci_op(pdev, &op));
240956a9202SRyan Wilson }
241956a9202SRyan Wilson 
242b8b0f559SKonrad Rzeszutek Wilk static struct pci_ops pcifront_bus_ops = {
243956a9202SRyan Wilson 	.read = pcifront_bus_read,
244956a9202SRyan Wilson 	.write = pcifront_bus_write,
245956a9202SRyan Wilson };
246956a9202SRyan Wilson 
247956a9202SRyan Wilson #ifdef CONFIG_PCI_MSI
248956a9202SRyan Wilson static int pci_frontend_enable_msix(struct pci_dev *dev,
249cc0f89c4SKonrad Rzeszutek Wilk 				    int vector[], int nvec)
250956a9202SRyan Wilson {
251956a9202SRyan Wilson 	int err;
252956a9202SRyan Wilson 	int i;
253956a9202SRyan Wilson 	struct xen_pci_op op = {
254956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_enable_msix,
255956a9202SRyan Wilson 		.domain = pci_domain_nr(dev->bus),
256956a9202SRyan Wilson 		.bus = dev->bus->number,
257956a9202SRyan Wilson 		.devfn = dev->devfn,
258956a9202SRyan Wilson 		.value = nvec,
259956a9202SRyan Wilson 	};
260956a9202SRyan Wilson 	struct pcifront_sd *sd = dev->bus->sysdata;
261956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
262956a9202SRyan Wilson 	struct msi_desc *entry;
263956a9202SRyan Wilson 
264956a9202SRyan Wilson 	if (nvec > SH_INFO_MAX_VEC) {
2657506dc79SFrederick Lawler 		pci_err(dev, "too many vectors (0x%x) for PCI frontend:"
2667506dc79SFrederick Lawler 				   " Increase SH_INFO_MAX_VEC\n", nvec);
267956a9202SRyan Wilson 		return -EINVAL;
268956a9202SRyan Wilson 	}
269956a9202SRyan Wilson 
270956a9202SRyan Wilson 	i = 0;
2715004e98aSJiang Liu 	for_each_pci_msi_entry(entry, dev) {
272956a9202SRyan Wilson 		op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
273956a9202SRyan Wilson 		/* Vector is useless at this point. */
274956a9202SRyan Wilson 		op.msix_entries[i].vector = -1;
275956a9202SRyan Wilson 		i++;
276956a9202SRyan Wilson 	}
277956a9202SRyan Wilson 
278956a9202SRyan Wilson 	err = do_pci_op(pdev, &op);
279956a9202SRyan Wilson 
280956a9202SRyan Wilson 	if (likely(!err)) {
281956a9202SRyan Wilson 		if (likely(!op.value)) {
282956a9202SRyan Wilson 			/* we get the result */
2831d461052SKonrad Rzeszutek Wilk 			for (i = 0; i < nvec; i++) {
2841d461052SKonrad Rzeszutek Wilk 				if (op.msix_entries[i].vector <= 0) {
2857506dc79SFrederick Lawler 					pci_warn(dev, "MSI-X entry %d is invalid: %d!\n",
2861d461052SKonrad Rzeszutek Wilk 						i, op.msix_entries[i].vector);
2871d461052SKonrad Rzeszutek Wilk 					err = -EINVAL;
288cc0f89c4SKonrad Rzeszutek Wilk 					vector[i] = -1;
2891d461052SKonrad Rzeszutek Wilk 					continue;
2901d461052SKonrad Rzeszutek Wilk 				}
291cc0f89c4SKonrad Rzeszutek Wilk 				vector[i] = op.msix_entries[i].vector;
2921d461052SKonrad Rzeszutek Wilk 			}
293956a9202SRyan Wilson 		} else {
294956a9202SRyan Wilson 			printk(KERN_DEBUG "enable msix get value %x\n",
295956a9202SRyan Wilson 				op.value);
296f09d8432SJan Beulich 			err = op.value;
297956a9202SRyan Wilson 		}
298956a9202SRyan Wilson 	} else {
2997506dc79SFrederick Lawler 		pci_err(dev, "enable msix get err %x\n", err);
300956a9202SRyan Wilson 	}
3011d461052SKonrad Rzeszutek Wilk 	return err;
302956a9202SRyan Wilson }
303956a9202SRyan Wilson 
304956a9202SRyan Wilson static void pci_frontend_disable_msix(struct pci_dev *dev)
305956a9202SRyan Wilson {
306956a9202SRyan Wilson 	int err;
307956a9202SRyan Wilson 	struct xen_pci_op op = {
308956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_disable_msix,
309956a9202SRyan Wilson 		.domain = pci_domain_nr(dev->bus),
310956a9202SRyan Wilson 		.bus = dev->bus->number,
311956a9202SRyan Wilson 		.devfn = dev->devfn,
312956a9202SRyan Wilson 	};
313956a9202SRyan Wilson 	struct pcifront_sd *sd = dev->bus->sysdata;
314956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
315956a9202SRyan Wilson 
316956a9202SRyan Wilson 	err = do_pci_op(pdev, &op);
317956a9202SRyan Wilson 
318956a9202SRyan Wilson 	/* What should do for error ? */
319956a9202SRyan Wilson 	if (err)
3207506dc79SFrederick Lawler 		pci_err(dev, "pci_disable_msix get err %x\n", err);
321956a9202SRyan Wilson }
322956a9202SRyan Wilson 
323cc0f89c4SKonrad Rzeszutek Wilk static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
324956a9202SRyan Wilson {
325956a9202SRyan Wilson 	int err;
326956a9202SRyan Wilson 	struct xen_pci_op op = {
327956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_enable_msi,
328956a9202SRyan Wilson 		.domain = pci_domain_nr(dev->bus),
329956a9202SRyan Wilson 		.bus = dev->bus->number,
330956a9202SRyan Wilson 		.devfn = dev->devfn,
331956a9202SRyan Wilson 	};
332956a9202SRyan Wilson 	struct pcifront_sd *sd = dev->bus->sysdata;
333956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
334956a9202SRyan Wilson 
335956a9202SRyan Wilson 	err = do_pci_op(pdev, &op);
336956a9202SRyan Wilson 	if (likely(!err)) {
337cc0f89c4SKonrad Rzeszutek Wilk 		vector[0] = op.value;
3381d461052SKonrad Rzeszutek Wilk 		if (op.value <= 0) {
3397506dc79SFrederick Lawler 			pci_warn(dev, "MSI entry is invalid: %d!\n",
3401d461052SKonrad Rzeszutek Wilk 				op.value);
3411d461052SKonrad Rzeszutek Wilk 			err = -EINVAL;
342cc0f89c4SKonrad Rzeszutek Wilk 			vector[0] = -1;
3431d461052SKonrad Rzeszutek Wilk 		}
344956a9202SRyan Wilson 	} else {
3457506dc79SFrederick Lawler 		pci_err(dev, "pci frontend enable msi failed for dev "
346956a9202SRyan Wilson 				    "%x:%x\n", op.bus, op.devfn);
347956a9202SRyan Wilson 		err = -EINVAL;
348956a9202SRyan Wilson 	}
349956a9202SRyan Wilson 	return err;
350956a9202SRyan Wilson }
351956a9202SRyan Wilson 
352956a9202SRyan Wilson static void pci_frontend_disable_msi(struct pci_dev *dev)
353956a9202SRyan Wilson {
354956a9202SRyan Wilson 	int err;
355956a9202SRyan Wilson 	struct xen_pci_op op = {
356956a9202SRyan Wilson 		.cmd    = XEN_PCI_OP_disable_msi,
357956a9202SRyan Wilson 		.domain = pci_domain_nr(dev->bus),
358956a9202SRyan Wilson 		.bus = dev->bus->number,
359956a9202SRyan Wilson 		.devfn = dev->devfn,
360956a9202SRyan Wilson 	};
361956a9202SRyan Wilson 	struct pcifront_sd *sd = dev->bus->sysdata;
362956a9202SRyan Wilson 	struct pcifront_device *pdev = pcifront_get_pdev(sd);
363956a9202SRyan Wilson 
364956a9202SRyan Wilson 	err = do_pci_op(pdev, &op);
365956a9202SRyan Wilson 	if (err == XEN_PCI_ERR_dev_not_found) {
366956a9202SRyan Wilson 		/* XXX No response from backend, what shall we do? */
367956a9202SRyan Wilson 		printk(KERN_DEBUG "get no response from backend for disable MSI\n");
368956a9202SRyan Wilson 		return;
369956a9202SRyan Wilson 	}
370956a9202SRyan Wilson 	if (err)
371956a9202SRyan Wilson 		/* how can pciback notify us fail? */
372956a9202SRyan Wilson 		printk(KERN_DEBUG "get fake response frombackend\n");
373956a9202SRyan Wilson }
374956a9202SRyan Wilson 
375956a9202SRyan Wilson static struct xen_pci_frontend_ops pci_frontend_ops = {
376956a9202SRyan Wilson 	.enable_msi = pci_frontend_enable_msi,
377956a9202SRyan Wilson 	.disable_msi = pci_frontend_disable_msi,
378956a9202SRyan Wilson 	.enable_msix = pci_frontend_enable_msix,
379956a9202SRyan Wilson 	.disable_msix = pci_frontend_disable_msix,
380956a9202SRyan Wilson };
381956a9202SRyan Wilson 
382956a9202SRyan Wilson static void pci_frontend_registrar(int enable)
383956a9202SRyan Wilson {
384956a9202SRyan Wilson 	if (enable)
385956a9202SRyan Wilson 		xen_pci_frontend = &pci_frontend_ops;
386956a9202SRyan Wilson 	else
387956a9202SRyan Wilson 		xen_pci_frontend = NULL;
388956a9202SRyan Wilson };
389956a9202SRyan Wilson #else
390956a9202SRyan Wilson static inline void pci_frontend_registrar(int enable) { };
391956a9202SRyan Wilson #endif /* CONFIG_PCI_MSI */
392956a9202SRyan Wilson 
393956a9202SRyan Wilson /* Claim resources for the PCI frontend as-is, backend won't allow changes */
394956a9202SRyan Wilson static int pcifront_claim_resource(struct pci_dev *dev, void *data)
395956a9202SRyan Wilson {
396956a9202SRyan Wilson 	struct pcifront_device *pdev = data;
397956a9202SRyan Wilson 	int i;
398956a9202SRyan Wilson 	struct resource *r;
399956a9202SRyan Wilson 
400956a9202SRyan Wilson 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
401956a9202SRyan Wilson 		r = &dev->resource[i];
402956a9202SRyan Wilson 
403956a9202SRyan Wilson 		if (!r->parent && r->start && r->flags) {
404956a9202SRyan Wilson 			dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
405956a9202SRyan Wilson 				pci_name(dev), i);
406956a9202SRyan Wilson 			if (pci_claim_resource(dev, i)) {
407917e3e65SKonrad Rzeszutek Wilk 				dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
408917e3e65SKonrad Rzeszutek Wilk 					"Device offline. Try using e820_host=1 in the guest config.\n",
409956a9202SRyan Wilson 					pci_name(dev), i);
410956a9202SRyan Wilson 			}
411956a9202SRyan Wilson 		}
412956a9202SRyan Wilson 	}
413956a9202SRyan Wilson 
414956a9202SRyan Wilson 	return 0;
415956a9202SRyan Wilson }
416956a9202SRyan Wilson 
41715856ad5SBill Pemberton static int pcifront_scan_bus(struct pcifront_device *pdev,
418956a9202SRyan Wilson 				unsigned int domain, unsigned int bus,
419956a9202SRyan Wilson 				struct pci_bus *b)
420956a9202SRyan Wilson {
421956a9202SRyan Wilson 	struct pci_dev *d;
422956a9202SRyan Wilson 	unsigned int devfn;
423956a9202SRyan Wilson 
424956a9202SRyan Wilson 	/* Scan the bus for functions and add.
425956a9202SRyan Wilson 	 * We omit handling of PCI bridge attachment because pciback prevents
426956a9202SRyan Wilson 	 * bridges from being exported.
427956a9202SRyan Wilson 	 */
428956a9202SRyan Wilson 	for (devfn = 0; devfn < 0x100; devfn++) {
429956a9202SRyan Wilson 		d = pci_get_slot(b, devfn);
430956a9202SRyan Wilson 		if (d) {
431956a9202SRyan Wilson 			/* Device is already known. */
432956a9202SRyan Wilson 			pci_dev_put(d);
433956a9202SRyan Wilson 			continue;
434956a9202SRyan Wilson 		}
435956a9202SRyan Wilson 
436956a9202SRyan Wilson 		d = pci_scan_single_device(b, devfn);
437956a9202SRyan Wilson 		if (d)
438956a9202SRyan Wilson 			dev_info(&pdev->xdev->dev, "New device on "
439e4de866aSKonrad Rzeszutek Wilk 				 "%04x:%02x:%02x.%d found.\n", domain, bus,
440956a9202SRyan Wilson 				 PCI_SLOT(devfn), PCI_FUNC(devfn));
441956a9202SRyan Wilson 	}
442956a9202SRyan Wilson 
443956a9202SRyan Wilson 	return 0;
444956a9202SRyan Wilson }
445956a9202SRyan Wilson 
44615856ad5SBill Pemberton static int pcifront_scan_root(struct pcifront_device *pdev,
447956a9202SRyan Wilson 				 unsigned int domain, unsigned int bus)
448956a9202SRyan Wilson {
449956a9202SRyan Wilson 	struct pci_bus *b;
450515d425bSArnd Bergmann 	LIST_HEAD(resources);
451956a9202SRyan Wilson 	struct pcifront_sd *sd = NULL;
452956a9202SRyan Wilson 	struct pci_bus_entry *bus_entry = NULL;
453956a9202SRyan Wilson 	int err = 0;
454515d425bSArnd Bergmann 	static struct resource busn_res = {
455515d425bSArnd Bergmann 		.start = 0,
456515d425bSArnd Bergmann 		.end = 255,
457515d425bSArnd Bergmann 		.flags = IORESOURCE_BUS,
458515d425bSArnd Bergmann 	};
459956a9202SRyan Wilson 
460956a9202SRyan Wilson #ifndef CONFIG_PCI_DOMAINS
461956a9202SRyan Wilson 	if (domain != 0) {
462956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
463956a9202SRyan Wilson 			"PCI Root in non-zero PCI Domain! domain=%d\n", domain);
464956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
465956a9202SRyan Wilson 			"Please compile with CONFIG_PCI_DOMAINS\n");
466956a9202SRyan Wilson 		err = -EINVAL;
467956a9202SRyan Wilson 		goto err_out;
468956a9202SRyan Wilson 	}
469956a9202SRyan Wilson #endif
470956a9202SRyan Wilson 
471956a9202SRyan Wilson 	dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
472956a9202SRyan Wilson 		 domain, bus);
473956a9202SRyan Wilson 
4744d8c8bd6SKonrad Rzeszutek Wilk 	bus_entry = kzalloc(sizeof(*bus_entry), GFP_KERNEL);
4754d8c8bd6SKonrad Rzeszutek Wilk 	sd = kzalloc(sizeof(*sd), GFP_KERNEL);
476956a9202SRyan Wilson 	if (!bus_entry || !sd) {
477956a9202SRyan Wilson 		err = -ENOMEM;
478956a9202SRyan Wilson 		goto err_out;
479956a9202SRyan Wilson 	}
480515d425bSArnd Bergmann 	pci_add_resource(&resources, &ioport_resource);
481515d425bSArnd Bergmann 	pci_add_resource(&resources, &iomem_resource);
482515d425bSArnd Bergmann 	pci_add_resource(&resources, &busn_res);
483956a9202SRyan Wilson 	pcifront_init_sd(sd, domain, bus, pdev);
484956a9202SRyan Wilson 
485a83919e0SRafael J. Wysocki 	pci_lock_rescan_remove();
486a83919e0SRafael J. Wysocki 
487515d425bSArnd Bergmann 	b = pci_scan_root_bus(&pdev->xdev->dev, bus,
488515d425bSArnd Bergmann 				  &pcifront_bus_ops, sd, &resources);
489956a9202SRyan Wilson 	if (!b) {
490956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
491956a9202SRyan Wilson 			"Error creating PCI Frontend Bus!\n");
492956a9202SRyan Wilson 		err = -ENOMEM;
493a83919e0SRafael J. Wysocki 		pci_unlock_rescan_remove();
494515d425bSArnd Bergmann 		pci_free_resource_list(&resources);
495956a9202SRyan Wilson 		goto err_out;
496956a9202SRyan Wilson 	}
497956a9202SRyan Wilson 
498956a9202SRyan Wilson 	bus_entry->bus = b;
499956a9202SRyan Wilson 
500956a9202SRyan Wilson 	list_add(&bus_entry->list, &pdev->root_buses);
501956a9202SRyan Wilson 
502515d425bSArnd Bergmann 	/* pci_scan_root_bus skips devices which do not have a
503956a9202SRyan Wilson 	* devfn==0. The pcifront_scan_bus enumerates all devfn. */
504956a9202SRyan Wilson 	err = pcifront_scan_bus(pdev, domain, bus, b);
505956a9202SRyan Wilson 
506956a9202SRyan Wilson 	/* Claim resources before going "live" with our devices */
507956a9202SRyan Wilson 	pci_walk_bus(b, pcifront_claim_resource, pdev);
508956a9202SRyan Wilson 
509956a9202SRyan Wilson 	/* Create SysFS and notify udev of the devices. Aka: "going live" */
510956a9202SRyan Wilson 	pci_bus_add_devices(b);
511956a9202SRyan Wilson 
512a83919e0SRafael J. Wysocki 	pci_unlock_rescan_remove();
513956a9202SRyan Wilson 	return err;
514956a9202SRyan Wilson 
515956a9202SRyan Wilson err_out:
516956a9202SRyan Wilson 	kfree(bus_entry);
517956a9202SRyan Wilson 	kfree(sd);
518956a9202SRyan Wilson 
519956a9202SRyan Wilson 	return err;
520956a9202SRyan Wilson }
521956a9202SRyan Wilson 
52215856ad5SBill Pemberton static int pcifront_rescan_root(struct pcifront_device *pdev,
523956a9202SRyan Wilson 				   unsigned int domain, unsigned int bus)
524956a9202SRyan Wilson {
525956a9202SRyan Wilson 	int err;
526956a9202SRyan Wilson 	struct pci_bus *b;
527956a9202SRyan Wilson 
528956a9202SRyan Wilson #ifndef CONFIG_PCI_DOMAINS
529956a9202SRyan Wilson 	if (domain != 0) {
530956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
531956a9202SRyan Wilson 			"PCI Root in non-zero PCI Domain! domain=%d\n", domain);
532956a9202SRyan Wilson 		dev_err(&pdev->xdev->dev,
533956a9202SRyan Wilson 			"Please compile with CONFIG_PCI_DOMAINS\n");
534956a9202SRyan Wilson 		return -EINVAL;
535956a9202SRyan Wilson 	}
536956a9202SRyan Wilson #endif
537956a9202SRyan Wilson 
538956a9202SRyan Wilson 	dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
539956a9202SRyan Wilson 		 domain, bus);
540956a9202SRyan Wilson 
541956a9202SRyan Wilson 	b = pci_find_bus(domain, bus);
542956a9202SRyan Wilson 	if (!b)
543956a9202SRyan Wilson 		/* If the bus is unknown, create it. */
544956a9202SRyan Wilson 		return pcifront_scan_root(pdev, domain, bus);
545956a9202SRyan Wilson 
546956a9202SRyan Wilson 	err = pcifront_scan_bus(pdev, domain, bus, b);
547956a9202SRyan Wilson 
548956a9202SRyan Wilson 	/* Claim resources before going "live" with our devices */
549956a9202SRyan Wilson 	pci_walk_bus(b, pcifront_claim_resource, pdev);
550956a9202SRyan Wilson 
551956a9202SRyan Wilson 	/* Create SysFS and notify udev of the devices. Aka: "going live" */
552956a9202SRyan Wilson 	pci_bus_add_devices(b);
553956a9202SRyan Wilson 
554956a9202SRyan Wilson 	return err;
555956a9202SRyan Wilson }
556956a9202SRyan Wilson 
557956a9202SRyan Wilson static void free_root_bus_devs(struct pci_bus *bus)
558956a9202SRyan Wilson {
559956a9202SRyan Wilson 	struct pci_dev *dev;
560956a9202SRyan Wilson 
561956a9202SRyan Wilson 	while (!list_empty(&bus->devices)) {
562956a9202SRyan Wilson 		dev = container_of(bus->devices.next, struct pci_dev,
563956a9202SRyan Wilson 				   bus_list);
5647506dc79SFrederick Lawler 		pci_dbg(dev, "removing device\n");
565210647afSYinghai Lu 		pci_stop_and_remove_bus_device(dev);
566956a9202SRyan Wilson 	}
567956a9202SRyan Wilson }
568956a9202SRyan Wilson 
569956a9202SRyan Wilson static void pcifront_free_roots(struct pcifront_device *pdev)
570956a9202SRyan Wilson {
571956a9202SRyan Wilson 	struct pci_bus_entry *bus_entry, *t;
572956a9202SRyan Wilson 
573956a9202SRyan Wilson 	dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
574956a9202SRyan Wilson 
575a83919e0SRafael J. Wysocki 	pci_lock_rescan_remove();
576956a9202SRyan Wilson 	list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
577956a9202SRyan Wilson 		list_del(&bus_entry->list);
578956a9202SRyan Wilson 
579956a9202SRyan Wilson 		free_root_bus_devs(bus_entry->bus);
580956a9202SRyan Wilson 
581956a9202SRyan Wilson 		kfree(bus_entry->bus->sysdata);
582956a9202SRyan Wilson 
583956a9202SRyan Wilson 		device_unregister(bus_entry->bus->bridge);
584956a9202SRyan Wilson 		pci_remove_bus(bus_entry->bus);
585956a9202SRyan Wilson 
586956a9202SRyan Wilson 		kfree(bus_entry);
587956a9202SRyan Wilson 	}
588a83919e0SRafael J. Wysocki 	pci_unlock_rescan_remove();
589956a9202SRyan Wilson }
590956a9202SRyan Wilson 
591956a9202SRyan Wilson static pci_ers_result_t pcifront_common_process(int cmd,
592956a9202SRyan Wilson 						struct pcifront_device *pdev,
593956a9202SRyan Wilson 						pci_channel_state_t state)
594956a9202SRyan Wilson {
595956a9202SRyan Wilson 	pci_ers_result_t result;
596956a9202SRyan Wilson 	struct pci_driver *pdrv;
597956a9202SRyan Wilson 	int bus = pdev->sh_info->aer_op.bus;
598956a9202SRyan Wilson 	int devfn = pdev->sh_info->aer_op.devfn;
599dd34bb40SSinan Kaya 	int domain = pdev->sh_info->aer_op.domain;
600956a9202SRyan Wilson 	struct pci_dev *pcidev;
601956a9202SRyan Wilson 	int flag = 0;
602956a9202SRyan Wilson 
603956a9202SRyan Wilson 	dev_dbg(&pdev->xdev->dev,
604956a9202SRyan Wilson 		"pcifront AER process: cmd %x (bus:%x, devfn%x)",
605956a9202SRyan Wilson 		cmd, bus, devfn);
606956a9202SRyan Wilson 	result = PCI_ERS_RESULT_NONE;
607956a9202SRyan Wilson 
608dd34bb40SSinan Kaya 	pcidev = pci_get_domain_bus_and_slot(domain, bus, devfn);
609956a9202SRyan Wilson 	if (!pcidev || !pcidev->driver) {
6102a63dd72SJiri Slaby 		dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
6112a63dd72SJiri Slaby 		pci_dev_put(pcidev);
612956a9202SRyan Wilson 		return result;
613956a9202SRyan Wilson 	}
614956a9202SRyan Wilson 	pdrv = pcidev->driver;
615956a9202SRyan Wilson 
61607d25146SAlan Stern 	if (pdrv) {
617956a9202SRyan Wilson 		if (pdrv->err_handler && pdrv->err_handler->error_detected) {
6187506dc79SFrederick Lawler 			pci_dbg(pcidev, "trying to call AER service\n");
619956a9202SRyan Wilson 			if (pcidev) {
620956a9202SRyan Wilson 				flag = 1;
621956a9202SRyan Wilson 				switch (cmd) {
622956a9202SRyan Wilson 				case XEN_PCI_OP_aer_detected:
623956a9202SRyan Wilson 					result = pdrv->err_handler->
624956a9202SRyan Wilson 						 error_detected(pcidev, state);
625956a9202SRyan Wilson 					break;
626956a9202SRyan Wilson 				case XEN_PCI_OP_aer_mmio:
627956a9202SRyan Wilson 					result = pdrv->err_handler->
628956a9202SRyan Wilson 						 mmio_enabled(pcidev);
629956a9202SRyan Wilson 					break;
630956a9202SRyan Wilson 				case XEN_PCI_OP_aer_slotreset:
631956a9202SRyan Wilson 					result = pdrv->err_handler->
632956a9202SRyan Wilson 						 slot_reset(pcidev);
633956a9202SRyan Wilson 					break;
634956a9202SRyan Wilson 				case XEN_PCI_OP_aer_resume:
635956a9202SRyan Wilson 					pdrv->err_handler->resume(pcidev);
636956a9202SRyan Wilson 					break;
637956a9202SRyan Wilson 				default:
638956a9202SRyan Wilson 					dev_err(&pdev->xdev->dev,
639956a9202SRyan Wilson 						"bad request in aer recovery "
640956a9202SRyan Wilson 						"operation!\n");
641956a9202SRyan Wilson 
642956a9202SRyan Wilson 				}
643956a9202SRyan Wilson 			}
644956a9202SRyan Wilson 		}
645956a9202SRyan Wilson 	}
646956a9202SRyan Wilson 	if (!flag)
647956a9202SRyan Wilson 		result = PCI_ERS_RESULT_NONE;
648956a9202SRyan Wilson 
649956a9202SRyan Wilson 	return result;
650956a9202SRyan Wilson }
651956a9202SRyan Wilson 
652956a9202SRyan Wilson 
653956a9202SRyan Wilson static void pcifront_do_aer(struct work_struct *data)
654956a9202SRyan Wilson {
655956a9202SRyan Wilson 	struct pcifront_device *pdev =
656956a9202SRyan Wilson 		container_of(data, struct pcifront_device, op_work);
657956a9202SRyan Wilson 	int cmd = pdev->sh_info->aer_op.cmd;
658956a9202SRyan Wilson 	pci_channel_state_t state =
659956a9202SRyan Wilson 		(pci_channel_state_t)pdev->sh_info->aer_op.err;
660956a9202SRyan Wilson 
661956a9202SRyan Wilson 	/*If a pci_conf op is in progress,
662956a9202SRyan Wilson 		we have to wait until it is done before service aer op*/
663956a9202SRyan Wilson 	dev_dbg(&pdev->xdev->dev,
664956a9202SRyan Wilson 		"pcifront service aer bus %x devfn %x\n",
665956a9202SRyan Wilson 		pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
666956a9202SRyan Wilson 
667956a9202SRyan Wilson 	pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
668956a9202SRyan Wilson 
669956a9202SRyan Wilson 	/* Post the operation to the guest. */
670956a9202SRyan Wilson 	wmb();
671956a9202SRyan Wilson 	clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
672956a9202SRyan Wilson 	notify_remote_via_evtchn(pdev->evtchn);
673956a9202SRyan Wilson 
674956a9202SRyan Wilson 	/*in case of we lost an aer request in four lines time_window*/
6754e857c58SPeter Zijlstra 	smp_mb__before_atomic();
676956a9202SRyan Wilson 	clear_bit(_PDEVB_op_active, &pdev->flags);
6774e857c58SPeter Zijlstra 	smp_mb__after_atomic();
678956a9202SRyan Wilson 
679956a9202SRyan Wilson 	schedule_pcifront_aer_op(pdev);
680956a9202SRyan Wilson 
681956a9202SRyan Wilson }
682956a9202SRyan Wilson 
683956a9202SRyan Wilson static irqreturn_t pcifront_handler_aer(int irq, void *dev)
684956a9202SRyan Wilson {
685956a9202SRyan Wilson 	struct pcifront_device *pdev = dev;
686956a9202SRyan Wilson 	schedule_pcifront_aer_op(pdev);
687956a9202SRyan Wilson 	return IRQ_HANDLED;
688956a9202SRyan Wilson }
6893d925320SKonrad Rzeszutek Wilk static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
690956a9202SRyan Wilson {
691956a9202SRyan Wilson 	int err = 0;
692956a9202SRyan Wilson 
693956a9202SRyan Wilson 	spin_lock(&pcifront_dev_lock);
694956a9202SRyan Wilson 
695956a9202SRyan Wilson 	if (!pcifront_dev) {
696956a9202SRyan Wilson 		dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
697956a9202SRyan Wilson 		pcifront_dev = pdev;
698098b1aeaSKonrad Rzeszutek Wilk 	} else
699956a9202SRyan Wilson 		err = -EEXIST;
700098b1aeaSKonrad Rzeszutek Wilk 
701956a9202SRyan Wilson 	spin_unlock(&pcifront_dev_lock);
702956a9202SRyan Wilson 
7033d925320SKonrad Rzeszutek Wilk 	if (!err && !swiotlb_nr_tbl()) {
7043d925320SKonrad Rzeszutek Wilk 		err = pci_xen_swiotlb_init_late();
7053d925320SKonrad Rzeszutek Wilk 		if (err)
7063d925320SKonrad Rzeszutek Wilk 			dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
7073d925320SKonrad Rzeszutek Wilk 	}
708956a9202SRyan Wilson 	return err;
709956a9202SRyan Wilson }
710956a9202SRyan Wilson 
711956a9202SRyan Wilson static void pcifront_disconnect(struct pcifront_device *pdev)
712956a9202SRyan Wilson {
713956a9202SRyan Wilson 	spin_lock(&pcifront_dev_lock);
714956a9202SRyan Wilson 
715956a9202SRyan Wilson 	if (pdev == pcifront_dev) {
716956a9202SRyan Wilson 		dev_info(&pdev->xdev->dev,
717956a9202SRyan Wilson 			 "Disconnecting PCI Frontend Buses\n");
718956a9202SRyan Wilson 		pcifront_dev = NULL;
719956a9202SRyan Wilson 	}
720956a9202SRyan Wilson 
721956a9202SRyan Wilson 	spin_unlock(&pcifront_dev_lock);
722956a9202SRyan Wilson }
723956a9202SRyan Wilson static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
724956a9202SRyan Wilson {
725956a9202SRyan Wilson 	struct pcifront_device *pdev;
726956a9202SRyan Wilson 
727956a9202SRyan Wilson 	pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
728956a9202SRyan Wilson 	if (pdev == NULL)
729956a9202SRyan Wilson 		goto out;
730956a9202SRyan Wilson 
731956a9202SRyan Wilson 	pdev->sh_info =
732956a9202SRyan Wilson 	    (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
733956a9202SRyan Wilson 	if (pdev->sh_info == NULL) {
734956a9202SRyan Wilson 		kfree(pdev);
735956a9202SRyan Wilson 		pdev = NULL;
736956a9202SRyan Wilson 		goto out;
737956a9202SRyan Wilson 	}
738956a9202SRyan Wilson 	pdev->sh_info->flags = 0;
739956a9202SRyan Wilson 
740956a9202SRyan Wilson 	/*Flag for registering PV AER handler*/
741956a9202SRyan Wilson 	set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
742956a9202SRyan Wilson 
743956a9202SRyan Wilson 	dev_set_drvdata(&xdev->dev, pdev);
744956a9202SRyan Wilson 	pdev->xdev = xdev;
745956a9202SRyan Wilson 
746956a9202SRyan Wilson 	INIT_LIST_HEAD(&pdev->root_buses);
747956a9202SRyan Wilson 
748956a9202SRyan Wilson 	spin_lock_init(&pdev->sh_info_lock);
749956a9202SRyan Wilson 
750956a9202SRyan Wilson 	pdev->evtchn = INVALID_EVTCHN;
751956a9202SRyan Wilson 	pdev->gnt_ref = INVALID_GRANT_REF;
752956a9202SRyan Wilson 	pdev->irq = -1;
753956a9202SRyan Wilson 
754956a9202SRyan Wilson 	INIT_WORK(&pdev->op_work, pcifront_do_aer);
755956a9202SRyan Wilson 
756956a9202SRyan Wilson 	dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
757956a9202SRyan Wilson 		pdev, pdev->sh_info);
758956a9202SRyan Wilson out:
759956a9202SRyan Wilson 	return pdev;
760956a9202SRyan Wilson }
761956a9202SRyan Wilson 
762956a9202SRyan Wilson static void free_pdev(struct pcifront_device *pdev)
763956a9202SRyan Wilson {
764956a9202SRyan Wilson 	dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
765956a9202SRyan Wilson 
766956a9202SRyan Wilson 	pcifront_free_roots(pdev);
767956a9202SRyan Wilson 
768db2e2e6eSTejun Heo 	cancel_work_sync(&pdev->op_work);
769956a9202SRyan Wilson 
770956a9202SRyan Wilson 	if (pdev->irq >= 0)
771956a9202SRyan Wilson 		unbind_from_irqhandler(pdev->irq, pdev);
772956a9202SRyan Wilson 
773956a9202SRyan Wilson 	if (pdev->evtchn != INVALID_EVTCHN)
774956a9202SRyan Wilson 		xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
775956a9202SRyan Wilson 
776956a9202SRyan Wilson 	if (pdev->gnt_ref != INVALID_GRANT_REF)
777956a9202SRyan Wilson 		gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
778956a9202SRyan Wilson 					  (unsigned long)pdev->sh_info);
779956a9202SRyan Wilson 	else
780956a9202SRyan Wilson 		free_page((unsigned long)pdev->sh_info);
781956a9202SRyan Wilson 
782956a9202SRyan Wilson 	dev_set_drvdata(&pdev->xdev->dev, NULL);
783956a9202SRyan Wilson 
784956a9202SRyan Wilson 	kfree(pdev);
785956a9202SRyan Wilson }
786956a9202SRyan Wilson 
787956a9202SRyan Wilson static int pcifront_publish_info(struct pcifront_device *pdev)
788956a9202SRyan Wilson {
789956a9202SRyan Wilson 	int err = 0;
790956a9202SRyan Wilson 	struct xenbus_transaction trans;
791ccc9d90aSWei Liu 	grant_ref_t gref;
792956a9202SRyan Wilson 
793ccc9d90aSWei Liu 	err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
794956a9202SRyan Wilson 	if (err < 0)
795956a9202SRyan Wilson 		goto out;
796956a9202SRyan Wilson 
797ccc9d90aSWei Liu 	pdev->gnt_ref = gref;
798956a9202SRyan Wilson 
799956a9202SRyan Wilson 	err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
800956a9202SRyan Wilson 	if (err)
801956a9202SRyan Wilson 		goto out;
802956a9202SRyan Wilson 
803956a9202SRyan Wilson 	err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
804956a9202SRyan Wilson 		0, "pcifront", pdev);
805956a9202SRyan Wilson 
806956a9202SRyan Wilson 	if (err < 0)
807956a9202SRyan Wilson 		return err;
808956a9202SRyan Wilson 
809956a9202SRyan Wilson 	pdev->irq = err;
810956a9202SRyan Wilson 
811956a9202SRyan Wilson do_publish:
812956a9202SRyan Wilson 	err = xenbus_transaction_start(&trans);
813956a9202SRyan Wilson 	if (err) {
814956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
815956a9202SRyan Wilson 				 "Error writing configuration for backend "
816956a9202SRyan Wilson 				 "(start transaction)");
817956a9202SRyan Wilson 		goto out;
818956a9202SRyan Wilson 	}
819956a9202SRyan Wilson 
820956a9202SRyan Wilson 	err = xenbus_printf(trans, pdev->xdev->nodename,
821956a9202SRyan Wilson 			    "pci-op-ref", "%u", pdev->gnt_ref);
822956a9202SRyan Wilson 	if (!err)
823956a9202SRyan Wilson 		err = xenbus_printf(trans, pdev->xdev->nodename,
824956a9202SRyan Wilson 				    "event-channel", "%u", pdev->evtchn);
825956a9202SRyan Wilson 	if (!err)
826956a9202SRyan Wilson 		err = xenbus_printf(trans, pdev->xdev->nodename,
827956a9202SRyan Wilson 				    "magic", XEN_PCI_MAGIC);
828956a9202SRyan Wilson 
829956a9202SRyan Wilson 	if (err) {
830956a9202SRyan Wilson 		xenbus_transaction_end(trans, 1);
831956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
832956a9202SRyan Wilson 				 "Error writing configuration for backend");
833956a9202SRyan Wilson 		goto out;
834956a9202SRyan Wilson 	} else {
835956a9202SRyan Wilson 		err = xenbus_transaction_end(trans, 0);
836956a9202SRyan Wilson 		if (err == -EAGAIN)
837956a9202SRyan Wilson 			goto do_publish;
838956a9202SRyan Wilson 		else if (err) {
839956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
840956a9202SRyan Wilson 					 "Error completing transaction "
841956a9202SRyan Wilson 					 "for backend");
842956a9202SRyan Wilson 			goto out;
843956a9202SRyan Wilson 		}
844956a9202SRyan Wilson 	}
845956a9202SRyan Wilson 
846956a9202SRyan Wilson 	xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
847956a9202SRyan Wilson 
848956a9202SRyan Wilson 	dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
849956a9202SRyan Wilson 
850956a9202SRyan Wilson out:
851956a9202SRyan Wilson 	return err;
852956a9202SRyan Wilson }
853956a9202SRyan Wilson 
85415856ad5SBill Pemberton static int pcifront_try_connect(struct pcifront_device *pdev)
855956a9202SRyan Wilson {
856956a9202SRyan Wilson 	int err = -EFAULT;
857956a9202SRyan Wilson 	int i, num_roots, len;
858956a9202SRyan Wilson 	char str[64];
859956a9202SRyan Wilson 	unsigned int domain, bus;
860956a9202SRyan Wilson 
861956a9202SRyan Wilson 
862956a9202SRyan Wilson 	/* Only connect once */
863956a9202SRyan Wilson 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
864956a9202SRyan Wilson 	    XenbusStateInitialised)
865956a9202SRyan Wilson 		goto out;
866956a9202SRyan Wilson 
8673d925320SKonrad Rzeszutek Wilk 	err = pcifront_connect_and_init_dma(pdev);
868098b1aeaSKonrad Rzeszutek Wilk 	if (err && err != -EEXIST) {
869956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
8703d925320SKonrad Rzeszutek Wilk 				 "Error setting up PCI Frontend");
871956a9202SRyan Wilson 		goto out;
872956a9202SRyan Wilson 	}
873956a9202SRyan Wilson 
874956a9202SRyan Wilson 	err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
875956a9202SRyan Wilson 			   "root_num", "%d", &num_roots);
876956a9202SRyan Wilson 	if (err == -ENOENT) {
877956a9202SRyan Wilson 		xenbus_dev_error(pdev->xdev, err,
878956a9202SRyan Wilson 				 "No PCI Roots found, trying 0000:00");
879956a9202SRyan Wilson 		err = pcifront_scan_root(pdev, 0, 0);
88023cf1d00SChen Gang 		if (err) {
88123cf1d00SChen Gang 			xenbus_dev_fatal(pdev->xdev, err,
88223cf1d00SChen Gang 					 "Error scanning PCI root 0000:00");
88323cf1d00SChen Gang 			goto out;
88423cf1d00SChen Gang 		}
885956a9202SRyan Wilson 		num_roots = 0;
886956a9202SRyan Wilson 	} else if (err != 1) {
887956a9202SRyan Wilson 		if (err == 0)
888956a9202SRyan Wilson 			err = -EINVAL;
889956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
890956a9202SRyan Wilson 				 "Error reading number of PCI roots");
891956a9202SRyan Wilson 		goto out;
892956a9202SRyan Wilson 	}
893956a9202SRyan Wilson 
894956a9202SRyan Wilson 	for (i = 0; i < num_roots; i++) {
895956a9202SRyan Wilson 		len = snprintf(str, sizeof(str), "root-%d", i);
896956a9202SRyan Wilson 		if (unlikely(len >= (sizeof(str) - 1))) {
897956a9202SRyan Wilson 			err = -ENOMEM;
898956a9202SRyan Wilson 			goto out;
899956a9202SRyan Wilson 		}
900956a9202SRyan Wilson 
901956a9202SRyan Wilson 		err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
902956a9202SRyan Wilson 				   "%x:%x", &domain, &bus);
903956a9202SRyan Wilson 		if (err != 2) {
904956a9202SRyan Wilson 			if (err >= 0)
905956a9202SRyan Wilson 				err = -EINVAL;
906956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
907956a9202SRyan Wilson 					 "Error reading PCI root %d", i);
908956a9202SRyan Wilson 			goto out;
909956a9202SRyan Wilson 		}
910956a9202SRyan Wilson 
911956a9202SRyan Wilson 		err = pcifront_scan_root(pdev, domain, bus);
912956a9202SRyan Wilson 		if (err) {
913956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
914956a9202SRyan Wilson 					 "Error scanning PCI root %04x:%02x",
915956a9202SRyan Wilson 					 domain, bus);
916956a9202SRyan Wilson 			goto out;
917956a9202SRyan Wilson 		}
918956a9202SRyan Wilson 	}
919956a9202SRyan Wilson 
920956a9202SRyan Wilson 	err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
921956a9202SRyan Wilson 
922956a9202SRyan Wilson out:
923956a9202SRyan Wilson 	return err;
924956a9202SRyan Wilson }
925956a9202SRyan Wilson 
926956a9202SRyan Wilson static int pcifront_try_disconnect(struct pcifront_device *pdev)
927956a9202SRyan Wilson {
928956a9202SRyan Wilson 	int err = 0;
929956a9202SRyan Wilson 	enum xenbus_state prev_state;
930956a9202SRyan Wilson 
931956a9202SRyan Wilson 
932956a9202SRyan Wilson 	prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
933956a9202SRyan Wilson 
934956a9202SRyan Wilson 	if (prev_state >= XenbusStateClosing)
935956a9202SRyan Wilson 		goto out;
936956a9202SRyan Wilson 
937956a9202SRyan Wilson 	if (prev_state == XenbusStateConnected) {
938956a9202SRyan Wilson 		pcifront_free_roots(pdev);
939956a9202SRyan Wilson 		pcifront_disconnect(pdev);
940956a9202SRyan Wilson 	}
941956a9202SRyan Wilson 
942956a9202SRyan Wilson 	err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
943956a9202SRyan Wilson 
944956a9202SRyan Wilson out:
945956a9202SRyan Wilson 
946956a9202SRyan Wilson 	return err;
947956a9202SRyan Wilson }
948956a9202SRyan Wilson 
94915856ad5SBill Pemberton static int pcifront_attach_devices(struct pcifront_device *pdev)
950956a9202SRyan Wilson {
951956a9202SRyan Wilson 	int err = -EFAULT;
952956a9202SRyan Wilson 	int i, num_roots, len;
953956a9202SRyan Wilson 	unsigned int domain, bus;
954956a9202SRyan Wilson 	char str[64];
955956a9202SRyan Wilson 
956956a9202SRyan Wilson 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
957956a9202SRyan Wilson 	    XenbusStateReconfiguring)
958956a9202SRyan Wilson 		goto out;
959956a9202SRyan Wilson 
960956a9202SRyan Wilson 	err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
961956a9202SRyan Wilson 			   "root_num", "%d", &num_roots);
962956a9202SRyan Wilson 	if (err == -ENOENT) {
963956a9202SRyan Wilson 		xenbus_dev_error(pdev->xdev, err,
964956a9202SRyan Wilson 				 "No PCI Roots found, trying 0000:00");
965956a9202SRyan Wilson 		err = pcifront_rescan_root(pdev, 0, 0);
96623cf1d00SChen Gang 		if (err) {
96723cf1d00SChen Gang 			xenbus_dev_fatal(pdev->xdev, err,
96823cf1d00SChen Gang 					 "Error scanning PCI root 0000:00");
96923cf1d00SChen Gang 			goto out;
97023cf1d00SChen Gang 		}
971956a9202SRyan Wilson 		num_roots = 0;
972956a9202SRyan Wilson 	} else if (err != 1) {
973956a9202SRyan Wilson 		if (err == 0)
974956a9202SRyan Wilson 			err = -EINVAL;
975956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
976956a9202SRyan Wilson 				 "Error reading number of PCI roots");
977956a9202SRyan Wilson 		goto out;
978956a9202SRyan Wilson 	}
979956a9202SRyan Wilson 
980956a9202SRyan Wilson 	for (i = 0; i < num_roots; i++) {
981956a9202SRyan Wilson 		len = snprintf(str, sizeof(str), "root-%d", i);
982956a9202SRyan Wilson 		if (unlikely(len >= (sizeof(str) - 1))) {
983956a9202SRyan Wilson 			err = -ENOMEM;
984956a9202SRyan Wilson 			goto out;
985956a9202SRyan Wilson 		}
986956a9202SRyan Wilson 
987956a9202SRyan Wilson 		err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
988956a9202SRyan Wilson 				   "%x:%x", &domain, &bus);
989956a9202SRyan Wilson 		if (err != 2) {
990956a9202SRyan Wilson 			if (err >= 0)
991956a9202SRyan Wilson 				err = -EINVAL;
992956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
993956a9202SRyan Wilson 					 "Error reading PCI root %d", i);
994956a9202SRyan Wilson 			goto out;
995956a9202SRyan Wilson 		}
996956a9202SRyan Wilson 
997956a9202SRyan Wilson 		err = pcifront_rescan_root(pdev, domain, bus);
998956a9202SRyan Wilson 		if (err) {
999956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
1000956a9202SRyan Wilson 					 "Error scanning PCI root %04x:%02x",
1001956a9202SRyan Wilson 					 domain, bus);
1002956a9202SRyan Wilson 			goto out;
1003956a9202SRyan Wilson 		}
1004956a9202SRyan Wilson 	}
1005956a9202SRyan Wilson 
1006956a9202SRyan Wilson 	xenbus_switch_state(pdev->xdev, XenbusStateConnected);
1007956a9202SRyan Wilson 
1008956a9202SRyan Wilson out:
1009956a9202SRyan Wilson 	return err;
1010956a9202SRyan Wilson }
1011956a9202SRyan Wilson 
1012956a9202SRyan Wilson static int pcifront_detach_devices(struct pcifront_device *pdev)
1013956a9202SRyan Wilson {
1014956a9202SRyan Wilson 	int err = 0;
1015956a9202SRyan Wilson 	int i, num_devs;
1016956a9202SRyan Wilson 	unsigned int domain, bus, slot, func;
1017956a9202SRyan Wilson 	struct pci_dev *pci_dev;
1018956a9202SRyan Wilson 	char str[64];
1019956a9202SRyan Wilson 
1020956a9202SRyan Wilson 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1021956a9202SRyan Wilson 	    XenbusStateConnected)
1022956a9202SRyan Wilson 		goto out;
1023956a9202SRyan Wilson 
1024956a9202SRyan Wilson 	err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1025956a9202SRyan Wilson 			   &num_devs);
1026956a9202SRyan Wilson 	if (err != 1) {
1027956a9202SRyan Wilson 		if (err >= 0)
1028956a9202SRyan Wilson 			err = -EINVAL;
1029956a9202SRyan Wilson 		xenbus_dev_fatal(pdev->xdev, err,
1030956a9202SRyan Wilson 				 "Error reading number of PCI devices");
1031956a9202SRyan Wilson 		goto out;
1032956a9202SRyan Wilson 	}
1033956a9202SRyan Wilson 
1034956a9202SRyan Wilson 	/* Find devices being detached and remove them. */
1035956a9202SRyan Wilson 	for (i = 0; i < num_devs; i++) {
1036956a9202SRyan Wilson 		int l, state;
1037956a9202SRyan Wilson 		l = snprintf(str, sizeof(str), "state-%d", i);
1038956a9202SRyan Wilson 		if (unlikely(l >= (sizeof(str) - 1))) {
1039956a9202SRyan Wilson 			err = -ENOMEM;
1040956a9202SRyan Wilson 			goto out;
1041956a9202SRyan Wilson 		}
104258faf07bSJuergen Gross 		state = xenbus_read_unsigned(pdev->xdev->otherend, str,
104358faf07bSJuergen Gross 					     XenbusStateUnknown);
1044956a9202SRyan Wilson 
1045956a9202SRyan Wilson 		if (state != XenbusStateClosing)
1046956a9202SRyan Wilson 			continue;
1047956a9202SRyan Wilson 
1048956a9202SRyan Wilson 		/* Remove device. */
1049956a9202SRyan Wilson 		l = snprintf(str, sizeof(str), "vdev-%d", i);
1050956a9202SRyan Wilson 		if (unlikely(l >= (sizeof(str) - 1))) {
1051956a9202SRyan Wilson 			err = -ENOMEM;
1052956a9202SRyan Wilson 			goto out;
1053956a9202SRyan Wilson 		}
1054956a9202SRyan Wilson 		err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1055956a9202SRyan Wilson 				   "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1056956a9202SRyan Wilson 		if (err != 4) {
1057956a9202SRyan Wilson 			if (err >= 0)
1058956a9202SRyan Wilson 				err = -EINVAL;
1059956a9202SRyan Wilson 			xenbus_dev_fatal(pdev->xdev, err,
1060956a9202SRyan Wilson 					 "Error reading PCI device %d", i);
1061956a9202SRyan Wilson 			goto out;
1062956a9202SRyan Wilson 		}
1063956a9202SRyan Wilson 
10642ccc246dSJiang Liu 		pci_dev = pci_get_domain_bus_and_slot(domain, bus,
10652ccc246dSJiang Liu 				PCI_DEVFN(slot, func));
1066956a9202SRyan Wilson 		if (!pci_dev) {
1067956a9202SRyan Wilson 			dev_dbg(&pdev->xdev->dev,
1068e4de866aSKonrad Rzeszutek Wilk 				"Cannot get PCI device %04x:%02x:%02x.%d\n",
1069956a9202SRyan Wilson 				domain, bus, slot, func);
1070956a9202SRyan Wilson 			continue;
1071956a9202SRyan Wilson 		}
1072a83919e0SRafael J. Wysocki 		pci_lock_rescan_remove();
1073210647afSYinghai Lu 		pci_stop_and_remove_bus_device(pci_dev);
1074956a9202SRyan Wilson 		pci_dev_put(pci_dev);
1075a83919e0SRafael J. Wysocki 		pci_unlock_rescan_remove();
1076956a9202SRyan Wilson 
1077956a9202SRyan Wilson 		dev_dbg(&pdev->xdev->dev,
1078e4de866aSKonrad Rzeszutek Wilk 			"PCI device %04x:%02x:%02x.%d removed.\n",
1079956a9202SRyan Wilson 			domain, bus, slot, func);
1080956a9202SRyan Wilson 	}
1081956a9202SRyan Wilson 
1082956a9202SRyan Wilson 	err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1083956a9202SRyan Wilson 
1084956a9202SRyan Wilson out:
1085956a9202SRyan Wilson 	return err;
1086956a9202SRyan Wilson }
1087956a9202SRyan Wilson 
1088bd721ea7SFabian Frederick static void __ref pcifront_backend_changed(struct xenbus_device *xdev,
1089956a9202SRyan Wilson 						  enum xenbus_state be_state)
1090956a9202SRyan Wilson {
1091956a9202SRyan Wilson 	struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1092956a9202SRyan Wilson 
1093956a9202SRyan Wilson 	switch (be_state) {
1094956a9202SRyan Wilson 	case XenbusStateUnknown:
1095956a9202SRyan Wilson 	case XenbusStateInitialising:
1096956a9202SRyan Wilson 	case XenbusStateInitWait:
1097956a9202SRyan Wilson 	case XenbusStateInitialised:
1098956a9202SRyan Wilson 		break;
1099956a9202SRyan Wilson 
1100956a9202SRyan Wilson 	case XenbusStateConnected:
1101956a9202SRyan Wilson 		pcifront_try_connect(pdev);
1102956a9202SRyan Wilson 		break;
1103956a9202SRyan Wilson 
1104d5af64deSDavid Vrabel 	case XenbusStateClosed:
1105d5af64deSDavid Vrabel 		if (xdev->state == XenbusStateClosed)
1106d5af64deSDavid Vrabel 			break;
1107d5af64deSDavid Vrabel 		/* Missed the backend's CLOSING state -- fallthrough */
1108956a9202SRyan Wilson 	case XenbusStateClosing:
1109956a9202SRyan Wilson 		dev_warn(&xdev->dev, "backend going away!\n");
1110956a9202SRyan Wilson 		pcifront_try_disconnect(pdev);
1111956a9202SRyan Wilson 		break;
1112956a9202SRyan Wilson 
1113956a9202SRyan Wilson 	case XenbusStateReconfiguring:
1114956a9202SRyan Wilson 		pcifront_detach_devices(pdev);
1115956a9202SRyan Wilson 		break;
1116956a9202SRyan Wilson 
1117956a9202SRyan Wilson 	case XenbusStateReconfigured:
1118956a9202SRyan Wilson 		pcifront_attach_devices(pdev);
1119956a9202SRyan Wilson 		break;
1120956a9202SRyan Wilson 	}
1121956a9202SRyan Wilson }
1122956a9202SRyan Wilson 
1123956a9202SRyan Wilson static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1124956a9202SRyan Wilson 				 const struct xenbus_device_id *id)
1125956a9202SRyan Wilson {
1126956a9202SRyan Wilson 	int err = 0;
1127956a9202SRyan Wilson 	struct pcifront_device *pdev = alloc_pdev(xdev);
1128956a9202SRyan Wilson 
1129956a9202SRyan Wilson 	if (pdev == NULL) {
1130956a9202SRyan Wilson 		err = -ENOMEM;
1131956a9202SRyan Wilson 		xenbus_dev_fatal(xdev, err,
1132956a9202SRyan Wilson 				 "Error allocating pcifront_device struct");
1133956a9202SRyan Wilson 		goto out;
1134956a9202SRyan Wilson 	}
1135956a9202SRyan Wilson 
1136956a9202SRyan Wilson 	err = pcifront_publish_info(pdev);
1137956a9202SRyan Wilson 	if (err)
1138956a9202SRyan Wilson 		free_pdev(pdev);
1139956a9202SRyan Wilson 
1140956a9202SRyan Wilson out:
1141956a9202SRyan Wilson 	return err;
1142956a9202SRyan Wilson }
1143956a9202SRyan Wilson 
1144956a9202SRyan Wilson static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1145956a9202SRyan Wilson {
1146956a9202SRyan Wilson 	struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1147956a9202SRyan Wilson 	if (pdev)
1148956a9202SRyan Wilson 		free_pdev(pdev);
1149956a9202SRyan Wilson 
1150956a9202SRyan Wilson 	return 0;
1151956a9202SRyan Wilson }
1152956a9202SRyan Wilson 
1153956a9202SRyan Wilson static const struct xenbus_device_id xenpci_ids[] = {
1154956a9202SRyan Wilson 	{"pci"},
1155956a9202SRyan Wilson 	{""},
1156956a9202SRyan Wilson };
1157956a9202SRyan Wilson 
115895afae48SDavid Vrabel static struct xenbus_driver xenpci_driver = {
115995afae48SDavid Vrabel 	.name			= "pcifront",
116095afae48SDavid Vrabel 	.ids			= xenpci_ids,
1161956a9202SRyan Wilson 	.probe			= pcifront_xenbus_probe,
1162956a9202SRyan Wilson 	.remove			= pcifront_xenbus_remove,
1163956a9202SRyan Wilson 	.otherend_changed	= pcifront_backend_changed,
116495afae48SDavid Vrabel };
1165956a9202SRyan Wilson 
1166956a9202SRyan Wilson static int __init pcifront_init(void)
1167956a9202SRyan Wilson {
1168956a9202SRyan Wilson 	if (!xen_pv_domain() || xen_initial_domain())
1169956a9202SRyan Wilson 		return -ENODEV;
1170956a9202SRyan Wilson 
117151c71a3bSKonrad Rzeszutek Wilk 	if (!xen_has_pv_devices())
117251c71a3bSKonrad Rzeszutek Wilk 		return -ENODEV;
117351c71a3bSKonrad Rzeszutek Wilk 
1174956a9202SRyan Wilson 	pci_frontend_registrar(1 /* enable */);
1175956a9202SRyan Wilson 
117673db144bSJan Beulich 	return xenbus_register_frontend(&xenpci_driver);
1177956a9202SRyan Wilson }
1178956a9202SRyan Wilson 
1179956a9202SRyan Wilson static void __exit pcifront_cleanup(void)
1180956a9202SRyan Wilson {
118173db144bSJan Beulich 	xenbus_unregister_driver(&xenpci_driver);
1182956a9202SRyan Wilson 	pci_frontend_registrar(0 /* disable */);
1183956a9202SRyan Wilson }
1184956a9202SRyan Wilson module_init(pcifront_init);
1185956a9202SRyan Wilson module_exit(pcifront_cleanup);
1186956a9202SRyan Wilson 
1187956a9202SRyan Wilson MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1188956a9202SRyan Wilson MODULE_LICENSE("GPL");
1189956a9202SRyan Wilson MODULE_ALIAS("xen:pci");
1190