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