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