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