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