1 /*
2  * PCI Stub Driver - Grabs devices in backend to be exported later
3  *
4  * Ryan Wilson <hap9@epoch.ncsc.mil>
5  * Chris Bookholt <hap10@epoch.ncsc.mil>
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/rwsem.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/kref.h>
16 #include <linux/pci.h>
17 #include <linux/wait.h>
18 #include <linux/sched.h>
19 #include <linux/atomic.h>
20 #include <xen/events.h>
21 #include <asm/xen/pci.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/interface/physdev.h>
24 #include "pciback.h"
25 #include "conf_space.h"
26 #include "conf_space_quirks.h"
27 
28 #define PCISTUB_DRIVER_NAME "pciback"
29 
30 static char *pci_devs_to_hide;
31 wait_queue_head_t xen_pcibk_aer_wait_queue;
32 /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
33 * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
34 */
35 static DECLARE_RWSEM(pcistub_sem);
36 module_param_named(hide, pci_devs_to_hide, charp, 0444);
37 
38 struct pcistub_device_id {
39 	struct list_head slot_list;
40 	int domain;
41 	unsigned char bus;
42 	unsigned int devfn;
43 };
44 static LIST_HEAD(pcistub_device_ids);
45 static DEFINE_SPINLOCK(device_ids_lock);
46 
47 struct pcistub_device {
48 	struct kref kref;
49 	struct list_head dev_list;
50 	spinlock_t lock;
51 
52 	struct pci_dev *dev;
53 	struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
54 };
55 
56 /* Access to pcistub_devices & seized_devices lists and the initialize_devices
57  * flag must be locked with pcistub_devices_lock
58  */
59 static DEFINE_SPINLOCK(pcistub_devices_lock);
60 static LIST_HEAD(pcistub_devices);
61 
62 /* wait for device_initcall before initializing our devices
63  * (see pcistub_init_devices_late)
64  */
65 static int initialize_devices;
66 static LIST_HEAD(seized_devices);
67 
68 static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
69 {
70 	struct pcistub_device *psdev;
71 
72 	dev_dbg(&dev->dev, "pcistub_device_alloc\n");
73 
74 	psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
75 	if (!psdev)
76 		return NULL;
77 
78 	psdev->dev = pci_dev_get(dev);
79 	if (!psdev->dev) {
80 		kfree(psdev);
81 		return NULL;
82 	}
83 
84 	kref_init(&psdev->kref);
85 	spin_lock_init(&psdev->lock);
86 
87 	return psdev;
88 }
89 
90 /* Don't call this directly as it's called by pcistub_device_put */
91 static void pcistub_device_release(struct kref *kref)
92 {
93 	struct pcistub_device *psdev;
94 	struct pci_dev *dev;
95 	struct xen_pcibk_dev_data *dev_data;
96 
97 	psdev = container_of(kref, struct pcistub_device, kref);
98 	dev = psdev->dev;
99 	dev_data = pci_get_drvdata(dev);
100 
101 	dev_dbg(&dev->dev, "pcistub_device_release\n");
102 
103 	xen_unregister_device_domain_owner(dev);
104 
105 	/* Call the reset function which does not take lock as this
106 	 * is called from "unbind" which takes a device_lock mutex.
107 	 */
108 	__pci_reset_function_locked(dev);
109 	if (dev_data &&
110 	    pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
111 		dev_info(&dev->dev, "Could not reload PCI state\n");
112 	else
113 		pci_restore_state(dev);
114 
115 	if (dev->msix_cap) {
116 		struct physdev_pci_device ppdev = {
117 			.seg = pci_domain_nr(dev->bus),
118 			.bus = dev->bus->number,
119 			.devfn = dev->devfn
120 		};
121 		int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
122 						&ppdev);
123 
124 		if (err && err != -ENOSYS)
125 			dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
126 				 err);
127 	}
128 
129 	/* Disable the device */
130 	xen_pcibk_reset_device(dev);
131 
132 	kfree(dev_data);
133 	pci_set_drvdata(dev, NULL);
134 
135 	/* Clean-up the device */
136 	xen_pcibk_config_free_dyn_fields(dev);
137 	xen_pcibk_config_free_dev(dev);
138 
139 	pci_clear_dev_assigned(dev);
140 	pci_dev_put(dev);
141 
142 	kfree(psdev);
143 }
144 
145 static inline void pcistub_device_get(struct pcistub_device *psdev)
146 {
147 	kref_get(&psdev->kref);
148 }
149 
150 static inline void pcistub_device_put(struct pcistub_device *psdev)
151 {
152 	kref_put(&psdev->kref, pcistub_device_release);
153 }
154 
155 static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
156 							 int slot, int func)
157 {
158 	struct pcistub_device *psdev;
159 
160 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
161 		if (psdev->dev != NULL
162 		    && domain == pci_domain_nr(psdev->dev->bus)
163 		    && bus == psdev->dev->bus->number
164 		    && slot == PCI_SLOT(psdev->dev->devfn)
165 		    && func == PCI_FUNC(psdev->dev->devfn)) {
166 			return psdev;
167 		}
168 	}
169 
170 	return NULL;
171 }
172 
173 static struct pcistub_device *pcistub_device_find(int domain, int bus,
174 						  int slot, int func)
175 {
176 	struct pcistub_device *psdev;
177 	unsigned long flags;
178 
179 	spin_lock_irqsave(&pcistub_devices_lock, flags);
180 
181 	psdev = pcistub_device_find_locked(domain, bus, slot, func);
182 	if (psdev)
183 		pcistub_device_get(psdev);
184 
185 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 	return psdev;
187 }
188 
189 static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
190 						  struct pcistub_device *psdev)
191 {
192 	struct pci_dev *pci_dev = NULL;
193 	unsigned long flags;
194 
195 	pcistub_device_get(psdev);
196 
197 	spin_lock_irqsave(&psdev->lock, flags);
198 	if (!psdev->pdev) {
199 		psdev->pdev = pdev;
200 		pci_dev = psdev->dev;
201 	}
202 	spin_unlock_irqrestore(&psdev->lock, flags);
203 
204 	if (!pci_dev)
205 		pcistub_device_put(psdev);
206 
207 	return pci_dev;
208 }
209 
210 struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
211 					    int domain, int bus,
212 					    int slot, int func)
213 {
214 	struct pcistub_device *psdev;
215 	struct pci_dev *found_dev = NULL;
216 	unsigned long flags;
217 
218 	spin_lock_irqsave(&pcistub_devices_lock, flags);
219 
220 	psdev = pcistub_device_find_locked(domain, bus, slot, func);
221 	if (psdev)
222 		found_dev = pcistub_device_get_pci_dev(pdev, psdev);
223 
224 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
225 	return found_dev;
226 }
227 
228 struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
229 				    struct pci_dev *dev)
230 {
231 	struct pcistub_device *psdev;
232 	struct pci_dev *found_dev = NULL;
233 	unsigned long flags;
234 
235 	spin_lock_irqsave(&pcistub_devices_lock, flags);
236 
237 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
238 		if (psdev->dev == dev) {
239 			found_dev = pcistub_device_get_pci_dev(pdev, psdev);
240 			break;
241 		}
242 	}
243 
244 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
245 	return found_dev;
246 }
247 
248 /*
249  * Called when:
250  *  - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
251  *  - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
252  *  - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
253  *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
254  *
255  *  As such we have to be careful.
256  *
257  *  To make this easier, the caller has to hold the device lock.
258  */
259 void pcistub_put_pci_dev(struct pci_dev *dev)
260 {
261 	struct pcistub_device *psdev, *found_psdev = NULL;
262 	unsigned long flags;
263 	struct xen_pcibk_dev_data *dev_data;
264 	int ret;
265 
266 	spin_lock_irqsave(&pcistub_devices_lock, flags);
267 
268 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
269 		if (psdev->dev == dev) {
270 			found_psdev = psdev;
271 			break;
272 		}
273 	}
274 
275 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
276 	if (WARN_ON(!found_psdev))
277 		return;
278 
279 	/*hold this lock for avoiding breaking link between
280 	* pcistub and xen_pcibk when AER is in processing
281 	*/
282 	down_write(&pcistub_sem);
283 	/* Cleanup our device
284 	 * (so it's ready for the next domain)
285 	 */
286 	device_lock_assert(&dev->dev);
287 	__pci_reset_function_locked(dev);
288 
289 	dev_data = pci_get_drvdata(dev);
290 	ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
291 	if (!ret) {
292 		/*
293 		 * The usual sequence is pci_save_state & pci_restore_state
294 		 * but the guest might have messed the configuration space up.
295 		 * Use the initial version (when device was bound to us).
296 		 */
297 		pci_restore_state(dev);
298 	} else
299 		dev_info(&dev->dev, "Could not reload PCI state\n");
300 	/* This disables the device. */
301 	xen_pcibk_reset_device(dev);
302 
303 	/* And cleanup up our emulated fields. */
304 	xen_pcibk_config_reset_dev(dev);
305 	xen_pcibk_config_free_dyn_fields(dev);
306 
307 	xen_unregister_device_domain_owner(dev);
308 
309 	spin_lock_irqsave(&found_psdev->lock, flags);
310 	found_psdev->pdev = NULL;
311 	spin_unlock_irqrestore(&found_psdev->lock, flags);
312 
313 	pcistub_device_put(found_psdev);
314 	up_write(&pcistub_sem);
315 }
316 
317 static int pcistub_match_one(struct pci_dev *dev,
318 			     struct pcistub_device_id *pdev_id)
319 {
320 	/* Match the specified device by domain, bus, slot, func and also if
321 	 * any of the device's parent bridges match.
322 	 */
323 	for (; dev != NULL; dev = dev->bus->self) {
324 		if (pci_domain_nr(dev->bus) == pdev_id->domain
325 		    && dev->bus->number == pdev_id->bus
326 		    && dev->devfn == pdev_id->devfn)
327 			return 1;
328 
329 		/* Sometimes topmost bridge links to itself. */
330 		if (dev == dev->bus->self)
331 			break;
332 	}
333 
334 	return 0;
335 }
336 
337 static int pcistub_match(struct pci_dev *dev)
338 {
339 	struct pcistub_device_id *pdev_id;
340 	unsigned long flags;
341 	int found = 0;
342 
343 	spin_lock_irqsave(&device_ids_lock, flags);
344 	list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
345 		if (pcistub_match_one(dev, pdev_id)) {
346 			found = 1;
347 			break;
348 		}
349 	}
350 	spin_unlock_irqrestore(&device_ids_lock, flags);
351 
352 	return found;
353 }
354 
355 static int pcistub_init_device(struct pci_dev *dev)
356 {
357 	struct xen_pcibk_dev_data *dev_data;
358 	int err = 0;
359 
360 	dev_dbg(&dev->dev, "initializing...\n");
361 
362 	/* The PCI backend is not intended to be a module (or to work with
363 	 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
364 	 * would need to be called somewhere to free the memory allocated
365 	 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
366 	 */
367 	dev_data = kzalloc(sizeof(*dev_data) +  strlen(DRV_NAME "[]")
368 				+ strlen(pci_name(dev)) + 1, GFP_KERNEL);
369 	if (!dev_data) {
370 		err = -ENOMEM;
371 		goto out;
372 	}
373 	pci_set_drvdata(dev, dev_data);
374 
375 	/*
376 	 * Setup name for fake IRQ handler. It will only be enabled
377 	 * once the device is turned on by the guest.
378 	 */
379 	sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
380 
381 	dev_dbg(&dev->dev, "initializing config\n");
382 
383 	init_waitqueue_head(&xen_pcibk_aer_wait_queue);
384 	err = xen_pcibk_config_init_dev(dev);
385 	if (err)
386 		goto out;
387 
388 	/* HACK: Force device (& ACPI) to determine what IRQ it's on - we
389 	 * must do this here because pcibios_enable_device may specify
390 	 * the pci device's true irq (and possibly its other resources)
391 	 * if they differ from what's in the configuration space.
392 	 * This makes the assumption that the device's resources won't
393 	 * change after this point (otherwise this code may break!)
394 	 */
395 	dev_dbg(&dev->dev, "enabling device\n");
396 	err = pci_enable_device(dev);
397 	if (err)
398 		goto config_release;
399 
400 	if (dev->msix_cap) {
401 		struct physdev_pci_device ppdev = {
402 			.seg = pci_domain_nr(dev->bus),
403 			.bus = dev->bus->number,
404 			.devfn = dev->devfn
405 		};
406 
407 		err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
408 		if (err && err != -ENOSYS)
409 			dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
410 				err);
411 	}
412 
413 	/* We need the device active to save the state. */
414 	dev_dbg(&dev->dev, "save state of device\n");
415 	pci_save_state(dev);
416 	dev_data->pci_saved_state = pci_store_saved_state(dev);
417 	if (!dev_data->pci_saved_state)
418 		dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
419 	else {
420 		dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
421 		__pci_reset_function_locked(dev);
422 		pci_restore_state(dev);
423 	}
424 	/* Now disable the device (this also ensures some private device
425 	 * data is setup before we export)
426 	 */
427 	dev_dbg(&dev->dev, "reset device\n");
428 	xen_pcibk_reset_device(dev);
429 
430 	pci_set_dev_assigned(dev);
431 	return 0;
432 
433 config_release:
434 	xen_pcibk_config_free_dev(dev);
435 
436 out:
437 	pci_set_drvdata(dev, NULL);
438 	kfree(dev_data);
439 	return err;
440 }
441 
442 /*
443  * Because some initialization still happens on
444  * devices during fs_initcall, we need to defer
445  * full initialization of our devices until
446  * device_initcall.
447  */
448 static int __init pcistub_init_devices_late(void)
449 {
450 	struct pcistub_device *psdev;
451 	unsigned long flags;
452 	int err = 0;
453 
454 	spin_lock_irqsave(&pcistub_devices_lock, flags);
455 
456 	while (!list_empty(&seized_devices)) {
457 		psdev = container_of(seized_devices.next,
458 				     struct pcistub_device, dev_list);
459 		list_del(&psdev->dev_list);
460 
461 		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
462 
463 		err = pcistub_init_device(psdev->dev);
464 		if (err) {
465 			dev_err(&psdev->dev->dev,
466 				"error %d initializing device\n", err);
467 			kfree(psdev);
468 			psdev = NULL;
469 		}
470 
471 		spin_lock_irqsave(&pcistub_devices_lock, flags);
472 
473 		if (psdev)
474 			list_add_tail(&psdev->dev_list, &pcistub_devices);
475 	}
476 
477 	initialize_devices = 1;
478 
479 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
480 
481 	return 0;
482 }
483 
484 static void pcistub_device_id_add_list(struct pcistub_device_id *new,
485 				       int domain, int bus, unsigned int devfn)
486 {
487 	struct pcistub_device_id *pci_dev_id;
488 	unsigned long flags;
489 	int found = 0;
490 
491 	spin_lock_irqsave(&device_ids_lock, flags);
492 
493 	list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
494 		if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
495 		    pci_dev_id->devfn == devfn) {
496 			found = 1;
497 			break;
498 		}
499 	}
500 
501 	if (!found) {
502 		new->domain = domain;
503 		new->bus = bus;
504 		new->devfn = devfn;
505 		list_add_tail(&new->slot_list, &pcistub_device_ids);
506 	}
507 
508 	spin_unlock_irqrestore(&device_ids_lock, flags);
509 
510 	if (found)
511 		kfree(new);
512 }
513 
514 static int pcistub_seize(struct pci_dev *dev,
515 			 struct pcistub_device_id *pci_dev_id)
516 {
517 	struct pcistub_device *psdev;
518 	unsigned long flags;
519 	int err = 0;
520 
521 	psdev = pcistub_device_alloc(dev);
522 	if (!psdev) {
523 		kfree(pci_dev_id);
524 		return -ENOMEM;
525 	}
526 
527 	spin_lock_irqsave(&pcistub_devices_lock, flags);
528 
529 	if (initialize_devices) {
530 		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
531 
532 		/* don't want irqs disabled when calling pcistub_init_device */
533 		err = pcistub_init_device(psdev->dev);
534 
535 		spin_lock_irqsave(&pcistub_devices_lock, flags);
536 
537 		if (!err)
538 			list_add(&psdev->dev_list, &pcistub_devices);
539 	} else {
540 		dev_dbg(&dev->dev, "deferring initialization\n");
541 		list_add(&psdev->dev_list, &seized_devices);
542 	}
543 
544 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
545 
546 	if (err) {
547 		kfree(pci_dev_id);
548 		pcistub_device_put(psdev);
549 	} else if (pci_dev_id)
550 		pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
551 					   dev->bus->number, dev->devfn);
552 
553 	return err;
554 }
555 
556 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
557  * other functions that take the sysfs lock. */
558 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
559 {
560 	int err = 0, match;
561 	struct pcistub_device_id *pci_dev_id = NULL;
562 
563 	dev_dbg(&dev->dev, "probing...\n");
564 
565 	match = pcistub_match(dev);
566 
567 	if ((dev->driver_override &&
568 	     !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
569 	    match) {
570 
571 		if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
572 		    && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
573 			dev_err(&dev->dev, "can't export pci devices that "
574 				"don't have a normal (0) or bridge (1) "
575 				"header type!\n");
576 			err = -ENODEV;
577 			goto out;
578 		}
579 
580 		if (!match) {
581 			pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
582 			if (!pci_dev_id) {
583 				err = -ENOMEM;
584 				goto out;
585 			}
586 		}
587 
588 		dev_info(&dev->dev, "seizing device\n");
589 		err = pcistub_seize(dev, pci_dev_id);
590 	} else
591 		/* Didn't find the device */
592 		err = -ENODEV;
593 
594 out:
595 	return err;
596 }
597 
598 /* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
599  * other functions that take the sysfs lock. */
600 static void pcistub_remove(struct pci_dev *dev)
601 {
602 	struct pcistub_device *psdev, *found_psdev = NULL;
603 	unsigned long flags;
604 
605 	dev_dbg(&dev->dev, "removing\n");
606 
607 	spin_lock_irqsave(&pcistub_devices_lock, flags);
608 
609 	xen_pcibk_config_quirk_release(dev);
610 
611 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
612 		if (psdev->dev == dev) {
613 			found_psdev = psdev;
614 			break;
615 		}
616 	}
617 
618 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
619 
620 	if (found_psdev) {
621 		dev_dbg(&dev->dev, "found device to remove %s\n",
622 			found_psdev->pdev ? "- in-use" : "");
623 
624 		if (found_psdev->pdev) {
625 			int domid = xen_find_device_domain_owner(dev);
626 
627 			pr_warn("****** removing device %s while still in-use by domain %d! ******\n",
628 			       pci_name(found_psdev->dev), domid);
629 			pr_warn("****** driver domain may still access this device's i/o resources!\n");
630 			pr_warn("****** shutdown driver domain before binding device\n");
631 			pr_warn("****** to other drivers or domains\n");
632 
633 			/* N.B. This ends up calling pcistub_put_pci_dev which ends up
634 			 * doing the FLR. */
635 			xen_pcibk_release_pci_dev(found_psdev->pdev,
636 						found_psdev->dev,
637 						false /* caller holds the lock. */);
638 		}
639 
640 		spin_lock_irqsave(&pcistub_devices_lock, flags);
641 		list_del(&found_psdev->dev_list);
642 		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
643 
644 		/* the final put for releasing from the list */
645 		pcistub_device_put(found_psdev);
646 	}
647 }
648 
649 static const struct pci_device_id pcistub_ids[] = {
650 	{
651 	 .vendor = PCI_ANY_ID,
652 	 .device = PCI_ANY_ID,
653 	 .subvendor = PCI_ANY_ID,
654 	 .subdevice = PCI_ANY_ID,
655 	 },
656 	{0,},
657 };
658 
659 #define PCI_NODENAME_MAX 40
660 static void kill_domain_by_device(struct pcistub_device *psdev)
661 {
662 	struct xenbus_transaction xbt;
663 	int err;
664 	char nodename[PCI_NODENAME_MAX];
665 
666 	BUG_ON(!psdev);
667 	snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
668 		psdev->pdev->xdev->otherend_id);
669 
670 again:
671 	err = xenbus_transaction_start(&xbt);
672 	if (err) {
673 		dev_err(&psdev->dev->dev,
674 			"error %d when start xenbus transaction\n", err);
675 		return;
676 	}
677 	/*PV AER handlers will set this flag*/
678 	xenbus_printf(xbt, nodename, "aerState" , "aerfail");
679 	err = xenbus_transaction_end(xbt, 0);
680 	if (err) {
681 		if (err == -EAGAIN)
682 			goto again;
683 		dev_err(&psdev->dev->dev,
684 			"error %d when end xenbus transaction\n", err);
685 		return;
686 	}
687 }
688 
689 /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
690  * backend need to have cooperation. In xen_pcibk, those steps will do similar
691  * jobs: send service request and waiting for front_end response.
692 */
693 static pci_ers_result_t common_process(struct pcistub_device *psdev,
694 				       pci_channel_state_t state, int aer_cmd,
695 				       pci_ers_result_t result)
696 {
697 	pci_ers_result_t res = result;
698 	struct xen_pcie_aer_op *aer_op;
699 	struct xen_pcibk_device *pdev = psdev->pdev;
700 	struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
701 	int ret;
702 
703 	/*with PV AER drivers*/
704 	aer_op = &(sh_info->aer_op);
705 	aer_op->cmd = aer_cmd ;
706 	/*useful for error_detected callback*/
707 	aer_op->err = state;
708 	/*pcifront_end BDF*/
709 	ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
710 		&aer_op->domain, &aer_op->bus, &aer_op->devfn);
711 	if (!ret) {
712 		dev_err(&psdev->dev->dev,
713 			DRV_NAME ": failed to get pcifront device\n");
714 		return PCI_ERS_RESULT_NONE;
715 	}
716 	wmb();
717 
718 	dev_dbg(&psdev->dev->dev,
719 			DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
720 			aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
721 	/*local flag to mark there's aer request, xen_pcibk callback will use
722 	* this flag to judge whether we need to check pci-front give aer
723 	* service ack signal
724 	*/
725 	set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
726 
727 	/*It is possible that a pcifront conf_read_write ops request invokes
728 	* the callback which cause the spurious execution of wake_up.
729 	* Yet it is harmless and better than a spinlock here
730 	*/
731 	set_bit(_XEN_PCIB_active,
732 		(unsigned long *)&sh_info->flags);
733 	wmb();
734 	notify_remote_via_irq(pdev->evtchn_irq);
735 
736 	ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
737 				 !(test_bit(_XEN_PCIB_active, (unsigned long *)
738 				 &sh_info->flags)), 300*HZ);
739 
740 	if (!ret) {
741 		if (test_bit(_XEN_PCIB_active,
742 			(unsigned long *)&sh_info->flags)) {
743 			dev_err(&psdev->dev->dev,
744 				"pcifront aer process not responding!\n");
745 			clear_bit(_XEN_PCIB_active,
746 			  (unsigned long *)&sh_info->flags);
747 			aer_op->err = PCI_ERS_RESULT_NONE;
748 			return res;
749 		}
750 	}
751 	clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
752 
753 	if (test_bit(_XEN_PCIF_active,
754 		(unsigned long *)&sh_info->flags)) {
755 		dev_dbg(&psdev->dev->dev,
756 			"schedule pci_conf service in " DRV_NAME "\n");
757 		xen_pcibk_test_and_schedule_op(psdev->pdev);
758 	}
759 
760 	res = (pci_ers_result_t)aer_op->err;
761 	return res;
762 }
763 
764 /*
765 * xen_pcibk_slot_reset: it will send the slot_reset request to  pcifront in case
766 * of the device driver could provide this service, and then wait for pcifront
767 * ack.
768 * @dev: pointer to PCI devices
769 * return value is used by aer_core do_recovery policy
770 */
771 static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
772 {
773 	struct pcistub_device *psdev;
774 	pci_ers_result_t result;
775 
776 	result = PCI_ERS_RESULT_RECOVERED;
777 	dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
778 		dev->bus->number, dev->devfn);
779 
780 	down_write(&pcistub_sem);
781 	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
782 				dev->bus->number,
783 				PCI_SLOT(dev->devfn),
784 				PCI_FUNC(dev->devfn));
785 
786 	if (!psdev || !psdev->pdev) {
787 		dev_err(&dev->dev,
788 			DRV_NAME " device is not found/assigned\n");
789 		goto end;
790 	}
791 
792 	if (!psdev->pdev->sh_info) {
793 		dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
794 			" by HVM, kill it\n");
795 		kill_domain_by_device(psdev);
796 		goto end;
797 	}
798 
799 	if (!test_bit(_XEN_PCIB_AERHANDLER,
800 		(unsigned long *)&psdev->pdev->sh_info->flags)) {
801 		dev_err(&dev->dev,
802 			"guest with no AER driver should have been killed\n");
803 		goto end;
804 	}
805 	result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
806 
807 	if (result == PCI_ERS_RESULT_NONE ||
808 		result == PCI_ERS_RESULT_DISCONNECT) {
809 		dev_dbg(&dev->dev,
810 			"No AER slot_reset service or disconnected!\n");
811 		kill_domain_by_device(psdev);
812 	}
813 end:
814 	if (psdev)
815 		pcistub_device_put(psdev);
816 	up_write(&pcistub_sem);
817 	return result;
818 
819 }
820 
821 
822 /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to  pcifront
823 * in case of the device driver could provide this service, and then wait
824 * for pcifront ack
825 * @dev: pointer to PCI devices
826 * return value is used by aer_core do_recovery policy
827 */
828 
829 static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
830 {
831 	struct pcistub_device *psdev;
832 	pci_ers_result_t result;
833 
834 	result = PCI_ERS_RESULT_RECOVERED;
835 	dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
836 		dev->bus->number, dev->devfn);
837 
838 	down_write(&pcistub_sem);
839 	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
840 				dev->bus->number,
841 				PCI_SLOT(dev->devfn),
842 				PCI_FUNC(dev->devfn));
843 
844 	if (!psdev || !psdev->pdev) {
845 		dev_err(&dev->dev,
846 			DRV_NAME " device is not found/assigned\n");
847 		goto end;
848 	}
849 
850 	if (!psdev->pdev->sh_info) {
851 		dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
852 			" by HVM, kill it\n");
853 		kill_domain_by_device(psdev);
854 		goto end;
855 	}
856 
857 	if (!test_bit(_XEN_PCIB_AERHANDLER,
858 		(unsigned long *)&psdev->pdev->sh_info->flags)) {
859 		dev_err(&dev->dev,
860 			"guest with no AER driver should have been killed\n");
861 		goto end;
862 	}
863 	result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
864 
865 	if (result == PCI_ERS_RESULT_NONE ||
866 		result == PCI_ERS_RESULT_DISCONNECT) {
867 		dev_dbg(&dev->dev,
868 			"No AER mmio_enabled service or disconnected!\n");
869 		kill_domain_by_device(psdev);
870 	}
871 end:
872 	if (psdev)
873 		pcistub_device_put(psdev);
874 	up_write(&pcistub_sem);
875 	return result;
876 }
877 
878 /*xen_pcibk_error_detected: it will send the error_detected request to  pcifront
879 * in case of the device driver could provide this service, and then wait
880 * for pcifront ack.
881 * @dev: pointer to PCI devices
882 * @error: the current PCI connection state
883 * return value is used by aer_core do_recovery policy
884 */
885 
886 static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
887 	pci_channel_state_t error)
888 {
889 	struct pcistub_device *psdev;
890 	pci_ers_result_t result;
891 
892 	result = PCI_ERS_RESULT_CAN_RECOVER;
893 	dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
894 		dev->bus->number, dev->devfn);
895 
896 	down_write(&pcistub_sem);
897 	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
898 				dev->bus->number,
899 				PCI_SLOT(dev->devfn),
900 				PCI_FUNC(dev->devfn));
901 
902 	if (!psdev || !psdev->pdev) {
903 		dev_err(&dev->dev,
904 			DRV_NAME " device is not found/assigned\n");
905 		goto end;
906 	}
907 
908 	if (!psdev->pdev->sh_info) {
909 		dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
910 			" by HVM, kill it\n");
911 		kill_domain_by_device(psdev);
912 		goto end;
913 	}
914 
915 	/*Guest owns the device yet no aer handler regiested, kill guest*/
916 	if (!test_bit(_XEN_PCIB_AERHANDLER,
917 		(unsigned long *)&psdev->pdev->sh_info->flags)) {
918 		dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
919 		kill_domain_by_device(psdev);
920 		goto end;
921 	}
922 	result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
923 
924 	if (result == PCI_ERS_RESULT_NONE ||
925 		result == PCI_ERS_RESULT_DISCONNECT) {
926 		dev_dbg(&dev->dev,
927 			"No AER error_detected service or disconnected!\n");
928 		kill_domain_by_device(psdev);
929 	}
930 end:
931 	if (psdev)
932 		pcistub_device_put(psdev);
933 	up_write(&pcistub_sem);
934 	return result;
935 }
936 
937 /*xen_pcibk_error_resume: it will send the error_resume request to  pcifront
938 * in case of the device driver could provide this service, and then wait
939 * for pcifront ack.
940 * @dev: pointer to PCI devices
941 */
942 
943 static void xen_pcibk_error_resume(struct pci_dev *dev)
944 {
945 	struct pcistub_device *psdev;
946 
947 	dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
948 		dev->bus->number, dev->devfn);
949 
950 	down_write(&pcistub_sem);
951 	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
952 				dev->bus->number,
953 				PCI_SLOT(dev->devfn),
954 				PCI_FUNC(dev->devfn));
955 
956 	if (!psdev || !psdev->pdev) {
957 		dev_err(&dev->dev,
958 			DRV_NAME " device is not found/assigned\n");
959 		goto end;
960 	}
961 
962 	if (!psdev->pdev->sh_info) {
963 		dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
964 			" by HVM, kill it\n");
965 		kill_domain_by_device(psdev);
966 		goto end;
967 	}
968 
969 	if (!test_bit(_XEN_PCIB_AERHANDLER,
970 		(unsigned long *)&psdev->pdev->sh_info->flags)) {
971 		dev_err(&dev->dev,
972 			"guest with no AER driver should have been killed\n");
973 		kill_domain_by_device(psdev);
974 		goto end;
975 	}
976 	common_process(psdev, 1, XEN_PCI_OP_aer_resume,
977 		       PCI_ERS_RESULT_RECOVERED);
978 end:
979 	if (psdev)
980 		pcistub_device_put(psdev);
981 	up_write(&pcistub_sem);
982 	return;
983 }
984 
985 /*add xen_pcibk AER handling*/
986 static const struct pci_error_handlers xen_pcibk_error_handler = {
987 	.error_detected = xen_pcibk_error_detected,
988 	.mmio_enabled = xen_pcibk_mmio_enabled,
989 	.slot_reset = xen_pcibk_slot_reset,
990 	.resume = xen_pcibk_error_resume,
991 };
992 
993 /*
994  * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
995  * for a normal device. I don't want it to be loaded automatically.
996  */
997 
998 static struct pci_driver xen_pcibk_pci_driver = {
999 	/* The name should be xen_pciback, but until the tools are updated
1000 	 * we will keep it as pciback. */
1001 	.name = PCISTUB_DRIVER_NAME,
1002 	.id_table = pcistub_ids,
1003 	.probe = pcistub_probe,
1004 	.remove = pcistub_remove,
1005 	.err_handler = &xen_pcibk_error_handler,
1006 };
1007 
1008 static inline int str_to_slot(const char *buf, int *domain, int *bus,
1009 			      int *slot, int *func)
1010 {
1011 	int parsed = 0;
1012 
1013 	switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1014 		       &parsed)) {
1015 	case 3:
1016 		*func = -1;
1017 		sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1018 		break;
1019 	case 2:
1020 		*slot = *func = -1;
1021 		sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1022 		break;
1023 	}
1024 	if (parsed && !buf[parsed])
1025 		return 0;
1026 
1027 	/* try again without domain */
1028 	*domain = 0;
1029 	switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1030 	case 2:
1031 		*func = -1;
1032 		sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1033 		break;
1034 	case 1:
1035 		*slot = *func = -1;
1036 		sscanf(buf, " %x:*.* %n", bus, &parsed);
1037 		break;
1038 	}
1039 	if (parsed && !buf[parsed])
1040 		return 0;
1041 
1042 	return -EINVAL;
1043 }
1044 
1045 static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1046 			       *slot, int *func, int *reg, int *size, int *mask)
1047 {
1048 	int parsed = 0;
1049 
1050 	sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1051 	       reg, size, mask, &parsed);
1052 	if (parsed && !buf[parsed])
1053 		return 0;
1054 
1055 	/* try again without domain */
1056 	*domain = 0;
1057 	sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1058 	       mask, &parsed);
1059 	if (parsed && !buf[parsed])
1060 		return 0;
1061 
1062 	return -EINVAL;
1063 }
1064 
1065 static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1066 {
1067 	struct pcistub_device_id *pci_dev_id;
1068 	int rc = 0, devfn = PCI_DEVFN(slot, func);
1069 
1070 	if (slot < 0) {
1071 		for (slot = 0; !rc && slot < 32; ++slot)
1072 			rc = pcistub_device_id_add(domain, bus, slot, func);
1073 		return rc;
1074 	}
1075 
1076 	if (func < 0) {
1077 		for (func = 0; !rc && func < 8; ++func)
1078 			rc = pcistub_device_id_add(domain, bus, slot, func);
1079 		return rc;
1080 	}
1081 
1082 	if ((
1083 #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1084     || !defined(CONFIG_PCI_DOMAINS)
1085 	     !pci_domains_supported ? domain :
1086 #endif
1087 	     domain < 0 || domain > 0xffff)
1088 	    || bus < 0 || bus > 0xff
1089 	    || PCI_SLOT(devfn) != slot
1090 	    || PCI_FUNC(devfn) != func)
1091 		return -EINVAL;
1092 
1093 	pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1094 	if (!pci_dev_id)
1095 		return -ENOMEM;
1096 
1097 	pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1098 		 domain, bus, slot, func);
1099 
1100 	pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1101 
1102 	return 0;
1103 }
1104 
1105 static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1106 {
1107 	struct pcistub_device_id *pci_dev_id, *t;
1108 	int err = -ENOENT;
1109 	unsigned long flags;
1110 
1111 	spin_lock_irqsave(&device_ids_lock, flags);
1112 	list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1113 				 slot_list) {
1114 		if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1115 		    && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1116 		    && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1117 			/* Don't break; here because it's possible the same
1118 			 * slot could be in the list more than once
1119 			 */
1120 			list_del(&pci_dev_id->slot_list);
1121 			kfree(pci_dev_id);
1122 
1123 			err = 0;
1124 
1125 			pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1126 				 domain, bus, slot, func);
1127 		}
1128 	}
1129 	spin_unlock_irqrestore(&device_ids_lock, flags);
1130 
1131 	return err;
1132 }
1133 
1134 static int pcistub_reg_add(int domain, int bus, int slot, int func,
1135 			   unsigned int reg, unsigned int size,
1136 			   unsigned int mask)
1137 {
1138 	int err = 0;
1139 	struct pcistub_device *psdev;
1140 	struct pci_dev *dev;
1141 	struct config_field *field;
1142 
1143 	if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1144 		return -EINVAL;
1145 
1146 	psdev = pcistub_device_find(domain, bus, slot, func);
1147 	if (!psdev) {
1148 		err = -ENODEV;
1149 		goto out;
1150 	}
1151 	dev = psdev->dev;
1152 
1153 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1154 	if (!field) {
1155 		err = -ENOMEM;
1156 		goto out;
1157 	}
1158 
1159 	field->offset = reg;
1160 	field->size = size;
1161 	field->mask = mask;
1162 	field->init = NULL;
1163 	field->reset = NULL;
1164 	field->release = NULL;
1165 	field->clean = xen_pcibk_config_field_free;
1166 
1167 	err = xen_pcibk_config_quirks_add_field(dev, field);
1168 	if (err)
1169 		kfree(field);
1170 out:
1171 	if (psdev)
1172 		pcistub_device_put(psdev);
1173 	return err;
1174 }
1175 
1176 static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1177 			      size_t count)
1178 {
1179 	int domain, bus, slot, func;
1180 	int err;
1181 
1182 	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1183 	if (err)
1184 		goto out;
1185 
1186 	err = pcistub_device_id_add(domain, bus, slot, func);
1187 
1188 out:
1189 	if (!err)
1190 		err = count;
1191 	return err;
1192 }
1193 static DRIVER_ATTR_WO(new_slot);
1194 
1195 static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1196 				 size_t count)
1197 {
1198 	int domain, bus, slot, func;
1199 	int err;
1200 
1201 	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1202 	if (err)
1203 		goto out;
1204 
1205 	err = pcistub_device_id_remove(domain, bus, slot, func);
1206 
1207 out:
1208 	if (!err)
1209 		err = count;
1210 	return err;
1211 }
1212 static DRIVER_ATTR_WO(remove_slot);
1213 
1214 static ssize_t slots_show(struct device_driver *drv, char *buf)
1215 {
1216 	struct pcistub_device_id *pci_dev_id;
1217 	size_t count = 0;
1218 	unsigned long flags;
1219 
1220 	spin_lock_irqsave(&device_ids_lock, flags);
1221 	list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1222 		if (count >= PAGE_SIZE)
1223 			break;
1224 
1225 		count += scnprintf(buf + count, PAGE_SIZE - count,
1226 				   "%04x:%02x:%02x.%d\n",
1227 				   pci_dev_id->domain, pci_dev_id->bus,
1228 				   PCI_SLOT(pci_dev_id->devfn),
1229 				   PCI_FUNC(pci_dev_id->devfn));
1230 	}
1231 	spin_unlock_irqrestore(&device_ids_lock, flags);
1232 
1233 	return count;
1234 }
1235 static DRIVER_ATTR_RO(slots);
1236 
1237 static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1238 {
1239 	struct pcistub_device *psdev;
1240 	struct xen_pcibk_dev_data *dev_data;
1241 	size_t count = 0;
1242 	unsigned long flags;
1243 
1244 	spin_lock_irqsave(&pcistub_devices_lock, flags);
1245 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1246 		if (count >= PAGE_SIZE)
1247 			break;
1248 		if (!psdev->dev)
1249 			continue;
1250 		dev_data = pci_get_drvdata(psdev->dev);
1251 		if (!dev_data)
1252 			continue;
1253 		count +=
1254 		    scnprintf(buf + count, PAGE_SIZE - count,
1255 			      "%s:%s:%sing:%ld\n",
1256 			      pci_name(psdev->dev),
1257 			      dev_data->isr_on ? "on" : "off",
1258 			      dev_data->ack_intr ? "ack" : "not ack",
1259 			      dev_data->handled);
1260 	}
1261 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1262 	return count;
1263 }
1264 static DRIVER_ATTR_RO(irq_handlers);
1265 
1266 static ssize_t irq_handler_state_store(struct device_driver *drv,
1267 				       const char *buf, size_t count)
1268 {
1269 	struct pcistub_device *psdev;
1270 	struct xen_pcibk_dev_data *dev_data;
1271 	int domain, bus, slot, func;
1272 	int err;
1273 
1274 	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1275 	if (err)
1276 		return err;
1277 
1278 	psdev = pcistub_device_find(domain, bus, slot, func);
1279 	if (!psdev) {
1280 		err = -ENOENT;
1281 		goto out;
1282 	}
1283 
1284 	dev_data = pci_get_drvdata(psdev->dev);
1285 	if (!dev_data) {
1286 		err = -ENOENT;
1287 		goto out;
1288 	}
1289 
1290 	dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1291 		dev_data->irq_name, dev_data->isr_on,
1292 		!dev_data->isr_on);
1293 
1294 	dev_data->isr_on = !(dev_data->isr_on);
1295 	if (dev_data->isr_on)
1296 		dev_data->ack_intr = 1;
1297 out:
1298 	if (psdev)
1299 		pcistub_device_put(psdev);
1300 	if (!err)
1301 		err = count;
1302 	return err;
1303 }
1304 static DRIVER_ATTR_WO(irq_handler_state);
1305 
1306 static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1307 			    size_t count)
1308 {
1309 	int domain, bus, slot, func, reg, size, mask;
1310 	int err;
1311 
1312 	err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1313 			   &mask);
1314 	if (err)
1315 		goto out;
1316 
1317 	err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1318 
1319 out:
1320 	if (!err)
1321 		err = count;
1322 	return err;
1323 }
1324 
1325 static ssize_t quirks_show(struct device_driver *drv, char *buf)
1326 {
1327 	int count = 0;
1328 	unsigned long flags;
1329 	struct xen_pcibk_config_quirk *quirk;
1330 	struct xen_pcibk_dev_data *dev_data;
1331 	const struct config_field *field;
1332 	const struct config_field_entry *cfg_entry;
1333 
1334 	spin_lock_irqsave(&device_ids_lock, flags);
1335 	list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1336 		if (count >= PAGE_SIZE)
1337 			goto out;
1338 
1339 		count += scnprintf(buf + count, PAGE_SIZE - count,
1340 				   "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1341 				   quirk->pdev->bus->number,
1342 				   PCI_SLOT(quirk->pdev->devfn),
1343 				   PCI_FUNC(quirk->pdev->devfn),
1344 				   quirk->devid.vendor, quirk->devid.device,
1345 				   quirk->devid.subvendor,
1346 				   quirk->devid.subdevice);
1347 
1348 		dev_data = pci_get_drvdata(quirk->pdev);
1349 
1350 		list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1351 			field = cfg_entry->field;
1352 			if (count >= PAGE_SIZE)
1353 				goto out;
1354 
1355 			count += scnprintf(buf + count, PAGE_SIZE - count,
1356 					   "\t\t%08x:%01x:%08x\n",
1357 					   cfg_entry->base_offset +
1358 					   field->offset, field->size,
1359 					   field->mask);
1360 		}
1361 	}
1362 
1363 out:
1364 	spin_unlock_irqrestore(&device_ids_lock, flags);
1365 
1366 	return count;
1367 }
1368 static DRIVER_ATTR_RW(quirks);
1369 
1370 static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1371 				size_t count)
1372 {
1373 	int domain, bus, slot, func;
1374 	int err;
1375 	struct pcistub_device *psdev;
1376 	struct xen_pcibk_dev_data *dev_data;
1377 
1378 	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1379 	if (err)
1380 		goto out;
1381 
1382 	psdev = pcistub_device_find(domain, bus, slot, func);
1383 	if (!psdev) {
1384 		err = -ENODEV;
1385 		goto out;
1386 	}
1387 
1388 	dev_data = pci_get_drvdata(psdev->dev);
1389 	/* the driver data for a device should never be null at this point */
1390 	if (!dev_data) {
1391 		err = -ENXIO;
1392 		goto release;
1393 	}
1394 	if (!dev_data->permissive) {
1395 		dev_data->permissive = 1;
1396 		/* Let user know that what they're doing could be unsafe */
1397 		dev_warn(&psdev->dev->dev, "enabling permissive mode "
1398 			 "configuration space accesses!\n");
1399 		dev_warn(&psdev->dev->dev,
1400 			 "permissive mode is potentially unsafe!\n");
1401 	}
1402 release:
1403 	pcistub_device_put(psdev);
1404 out:
1405 	if (!err)
1406 		err = count;
1407 	return err;
1408 }
1409 
1410 static ssize_t permissive_show(struct device_driver *drv, char *buf)
1411 {
1412 	struct pcistub_device *psdev;
1413 	struct xen_pcibk_dev_data *dev_data;
1414 	size_t count = 0;
1415 	unsigned long flags;
1416 	spin_lock_irqsave(&pcistub_devices_lock, flags);
1417 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1418 		if (count >= PAGE_SIZE)
1419 			break;
1420 		if (!psdev->dev)
1421 			continue;
1422 		dev_data = pci_get_drvdata(psdev->dev);
1423 		if (!dev_data || !dev_data->permissive)
1424 			continue;
1425 		count +=
1426 		    scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1427 			      pci_name(psdev->dev));
1428 	}
1429 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1430 	return count;
1431 }
1432 static DRIVER_ATTR_RW(permissive);
1433 
1434 static void pcistub_exit(void)
1435 {
1436 	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1437 	driver_remove_file(&xen_pcibk_pci_driver.driver,
1438 			   &driver_attr_remove_slot);
1439 	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1440 	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1441 	driver_remove_file(&xen_pcibk_pci_driver.driver,
1442 			   &driver_attr_permissive);
1443 	driver_remove_file(&xen_pcibk_pci_driver.driver,
1444 			   &driver_attr_irq_handlers);
1445 	driver_remove_file(&xen_pcibk_pci_driver.driver,
1446 			   &driver_attr_irq_handler_state);
1447 	pci_unregister_driver(&xen_pcibk_pci_driver);
1448 }
1449 
1450 static int __init pcistub_init(void)
1451 {
1452 	int pos = 0;
1453 	int err = 0;
1454 	int domain, bus, slot, func;
1455 	int parsed;
1456 
1457 	if (pci_devs_to_hide && *pci_devs_to_hide) {
1458 		do {
1459 			parsed = 0;
1460 
1461 			err = sscanf(pci_devs_to_hide + pos,
1462 				     " (%x:%x:%x.%x) %n",
1463 				     &domain, &bus, &slot, &func, &parsed);
1464 			switch (err) {
1465 			case 3:
1466 				func = -1;
1467 				sscanf(pci_devs_to_hide + pos,
1468 				       " (%x:%x:%x.*) %n",
1469 				       &domain, &bus, &slot, &parsed);
1470 				break;
1471 			case 2:
1472 				slot = func = -1;
1473 				sscanf(pci_devs_to_hide + pos,
1474 				       " (%x:%x:*.*) %n",
1475 				       &domain, &bus, &parsed);
1476 				break;
1477 			}
1478 
1479 			if (!parsed) {
1480 				domain = 0;
1481 				err = sscanf(pci_devs_to_hide + pos,
1482 					     " (%x:%x.%x) %n",
1483 					     &bus, &slot, &func, &parsed);
1484 				switch (err) {
1485 				case 2:
1486 					func = -1;
1487 					sscanf(pci_devs_to_hide + pos,
1488 					       " (%x:%x.*) %n",
1489 					       &bus, &slot, &parsed);
1490 					break;
1491 				case 1:
1492 					slot = func = -1;
1493 					sscanf(pci_devs_to_hide + pos,
1494 					       " (%x:*.*) %n",
1495 					       &bus, &parsed);
1496 					break;
1497 				}
1498 			}
1499 
1500 			if (parsed <= 0)
1501 				goto parse_error;
1502 
1503 			err = pcistub_device_id_add(domain, bus, slot, func);
1504 			if (err)
1505 				goto out;
1506 
1507 			pos += parsed;
1508 		} while (pci_devs_to_hide[pos]);
1509 	}
1510 
1511 	/* If we're the first PCI Device Driver to register, we're the
1512 	 * first one to get offered PCI devices as they become
1513 	 * available (and thus we can be the first to grab them)
1514 	 */
1515 	err = pci_register_driver(&xen_pcibk_pci_driver);
1516 	if (err < 0)
1517 		goto out;
1518 
1519 	err = driver_create_file(&xen_pcibk_pci_driver.driver,
1520 				 &driver_attr_new_slot);
1521 	if (!err)
1522 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1523 					 &driver_attr_remove_slot);
1524 	if (!err)
1525 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1526 					 &driver_attr_slots);
1527 	if (!err)
1528 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1529 					 &driver_attr_quirks);
1530 	if (!err)
1531 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1532 					 &driver_attr_permissive);
1533 
1534 	if (!err)
1535 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1536 					 &driver_attr_irq_handlers);
1537 	if (!err)
1538 		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1539 					&driver_attr_irq_handler_state);
1540 	if (err)
1541 		pcistub_exit();
1542 
1543 out:
1544 	return err;
1545 
1546 parse_error:
1547 	pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1548 	       pci_devs_to_hide + pos);
1549 	return -EINVAL;
1550 }
1551 
1552 #ifndef MODULE
1553 /*
1554  * fs_initcall happens before device_initcall
1555  * so xen_pcibk *should* get called first (b/c we
1556  * want to suck up any device before other drivers
1557  * get a chance by being the first pci device
1558  * driver to register)
1559  */
1560 fs_initcall(pcistub_init);
1561 #endif
1562 
1563 #ifdef CONFIG_PCI_IOV
1564 static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1565 {
1566 	struct pcistub_device *psdev = NULL;
1567 	unsigned long flags;
1568 	bool found = false;
1569 
1570 	spin_lock_irqsave(&pcistub_devices_lock, flags);
1571 	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1572 		if (!psdev->pdev && psdev->dev != pdev
1573 		    && pci_physfn(psdev->dev) == pdev) {
1574 			found = true;
1575 			break;
1576 		}
1577 	}
1578 	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1579 	if (found)
1580 		return psdev;
1581 	return NULL;
1582 }
1583 
1584 static int pci_stub_notifier(struct notifier_block *nb,
1585 			     unsigned long action, void *data)
1586 {
1587 	struct device *dev = data;
1588 	const struct pci_dev *pdev = to_pci_dev(dev);
1589 
1590 	if (action != BUS_NOTIFY_UNBIND_DRIVER)
1591 		return NOTIFY_DONE;
1592 
1593 	if (!pdev->is_physfn)
1594 		return NOTIFY_DONE;
1595 
1596 	for (;;) {
1597 		struct pcistub_device *psdev = find_vfs(pdev);
1598 		if (!psdev)
1599 			break;
1600 		device_release_driver(&psdev->dev->dev);
1601 	}
1602 	return NOTIFY_DONE;
1603 }
1604 
1605 static struct notifier_block pci_stub_nb = {
1606 	.notifier_call = pci_stub_notifier,
1607 };
1608 #endif
1609 
1610 static int __init xen_pcibk_init(void)
1611 {
1612 	int err;
1613 
1614 	if (!xen_initial_domain())
1615 		return -ENODEV;
1616 
1617 	err = xen_pcibk_config_init();
1618 	if (err)
1619 		return err;
1620 
1621 #ifdef MODULE
1622 	err = pcistub_init();
1623 	if (err < 0)
1624 		return err;
1625 #endif
1626 
1627 	pcistub_init_devices_late();
1628 	err = xen_pcibk_xenbus_register();
1629 	if (err)
1630 		pcistub_exit();
1631 #ifdef CONFIG_PCI_IOV
1632 	else
1633 		bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1634 #endif
1635 
1636 	return err;
1637 }
1638 
1639 static void __exit xen_pcibk_cleanup(void)
1640 {
1641 #ifdef CONFIG_PCI_IOV
1642 	bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1643 #endif
1644 	xen_pcibk_xenbus_unregister();
1645 	pcistub_exit();
1646 }
1647 
1648 module_init(xen_pcibk_init);
1649 module_exit(xen_pcibk_cleanup);
1650 
1651 MODULE_LICENSE("Dual BSD/GPL");
1652 MODULE_ALIAS("xen-backend:pci");
1653