xref: /linux/drivers/xen/xen-pciback/xenbus.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
1 /*
2  * PCI Backend Xenbus Setup - handles setup with frontend and xend
3  *
4  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5  */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/list.h>
9 #include <linux/vmalloc.h>
10 #include <linux/workqueue.h>
11 #include <xen/xenbus.h>
12 #include <xen/events.h>
13 #include <asm/xen/pci.h>
14 #include "pciback.h"
15 
16 #define INVALID_EVTCHN_IRQ  (-1)
17 struct workqueue_struct *xen_pcibk_wq;
18 
19 static bool __read_mostly passthrough;
20 module_param(passthrough, bool, S_IRUGO);
21 MODULE_PARM_DESC(passthrough,
22 	"Option to specify how to export PCI topology to guest:\n"\
23 	" 0 - (default) Hide the true PCI topology and makes the frontend\n"\
24 	"   there is a single PCI bus with only the exported devices on it.\n"\
25 	"   For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
26 	"   while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
27 	" 1 - Passthrough provides a real view of the PCI topology to the\n"\
28 	"   frontend (for example, a device at 06:01.b will still appear at\n"\
29 	"   06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
30 	"   exposed PCI devices to its driver domains. This may be required\n"\
31 	"   for drivers which depend on finding their hardward in certain\n"\
32 	"   bus/slot locations.");
33 
34 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
35 {
36 	struct xen_pcibk_device *pdev;
37 
38 	pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
39 	if (pdev == NULL)
40 		goto out;
41 	dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
42 
43 	pdev->xdev = xdev;
44 	dev_set_drvdata(&xdev->dev, pdev);
45 
46 	mutex_init(&pdev->dev_lock);
47 
48 	pdev->sh_info = NULL;
49 	pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
50 	pdev->be_watching = 0;
51 
52 	INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
53 
54 	if (xen_pcibk_init_devices(pdev)) {
55 		kfree(pdev);
56 		pdev = NULL;
57 	}
58 out:
59 	return pdev;
60 }
61 
62 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
63 {
64 	mutex_lock(&pdev->dev_lock);
65 	/* Ensure the guest can't trigger our handler before removing devices */
66 	if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
67 		unbind_from_irqhandler(pdev->evtchn_irq, pdev);
68 		pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
69 	}
70 
71 	/* If the driver domain started an op, make sure we complete it
72 	 * before releasing the shared memory */
73 
74 	/* Note, the workqueue does not use spinlocks at all.*/
75 	flush_workqueue(xen_pcibk_wq);
76 
77 	if (pdev->sh_info != NULL) {
78 		xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
79 		pdev->sh_info = NULL;
80 	}
81 	mutex_unlock(&pdev->dev_lock);
82 }
83 
84 static void free_pdev(struct xen_pcibk_device *pdev)
85 {
86 	if (pdev->be_watching) {
87 		unregister_xenbus_watch(&pdev->be_watch);
88 		pdev->be_watching = 0;
89 	}
90 
91 	xen_pcibk_disconnect(pdev);
92 
93 	xen_pcibk_release_devices(pdev);
94 
95 	dev_set_drvdata(&pdev->xdev->dev, NULL);
96 	pdev->xdev = NULL;
97 
98 	kfree(pdev);
99 }
100 
101 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
102 			     int remote_evtchn)
103 {
104 	int err = 0;
105 	void *vaddr;
106 
107 	dev_dbg(&pdev->xdev->dev,
108 		"Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
109 		gnt_ref, remote_evtchn);
110 
111 	err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
112 	if (err < 0) {
113 		xenbus_dev_fatal(pdev->xdev, err,
114 				"Error mapping other domain page in ours.");
115 		goto out;
116 	}
117 
118 	pdev->sh_info = vaddr;
119 
120 	err = bind_interdomain_evtchn_to_irqhandler(
121 		pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
122 		0, DRV_NAME, pdev);
123 	if (err < 0) {
124 		xenbus_dev_fatal(pdev->xdev, err,
125 				 "Error binding event channel to IRQ");
126 		goto out;
127 	}
128 	pdev->evtchn_irq = err;
129 	err = 0;
130 
131 	dev_dbg(&pdev->xdev->dev, "Attached!\n");
132 out:
133 	return err;
134 }
135 
136 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
137 {
138 	int err = 0;
139 	int gnt_ref, remote_evtchn;
140 	char *magic = NULL;
141 
142 
143 	mutex_lock(&pdev->dev_lock);
144 	/* Make sure we only do this setup once */
145 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
146 	    XenbusStateInitialised)
147 		goto out;
148 
149 	/* Wait for frontend to state that it has published the configuration */
150 	if (xenbus_read_driver_state(pdev->xdev->otherend) !=
151 	    XenbusStateInitialised)
152 		goto out;
153 
154 	dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
155 
156 	err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
157 			    "pci-op-ref", "%u", &gnt_ref,
158 			    "event-channel", "%u", &remote_evtchn,
159 			    "magic", NULL, &magic, NULL);
160 	if (err) {
161 		/* If configuration didn't get read correctly, wait longer */
162 		xenbus_dev_fatal(pdev->xdev, err,
163 				 "Error reading configuration from frontend");
164 		goto out;
165 	}
166 
167 	if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
168 		xenbus_dev_fatal(pdev->xdev, -EFAULT,
169 				 "version mismatch (%s/%s) with pcifront - "
170 				 "halting " DRV_NAME,
171 				 magic, XEN_PCI_MAGIC);
172 		goto out;
173 	}
174 
175 	err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
176 	if (err)
177 		goto out;
178 
179 	dev_dbg(&pdev->xdev->dev, "Connecting...\n");
180 
181 	err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
182 	if (err)
183 		xenbus_dev_fatal(pdev->xdev, err,
184 				 "Error switching to connected state!");
185 
186 	dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
187 out:
188 	mutex_unlock(&pdev->dev_lock);
189 
190 	kfree(magic);
191 
192 	return err;
193 }
194 
195 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
196 				   unsigned int domain, unsigned int bus,
197 				   unsigned int devfn, unsigned int devid)
198 {
199 	int err;
200 	int len;
201 	char str[64];
202 
203 	len = snprintf(str, sizeof(str), "vdev-%d", devid);
204 	if (unlikely(len >= (sizeof(str) - 1))) {
205 		err = -ENOMEM;
206 		goto out;
207 	}
208 
209 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
210 			    "%04x:%02x:%02x.%02x", domain, bus,
211 			    PCI_SLOT(devfn), PCI_FUNC(devfn));
212 
213 out:
214 	return err;
215 }
216 
217 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
218 				 int domain, int bus, int slot, int func,
219 				 int devid)
220 {
221 	struct pci_dev *dev;
222 	int err = 0;
223 
224 	dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
225 		domain, bus, slot, func);
226 
227 	dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
228 	if (!dev) {
229 		err = -EINVAL;
230 		xenbus_dev_fatal(pdev->xdev, err,
231 				 "Couldn't locate PCI device "
232 				 "(%04x:%02x:%02x.%01x)! "
233 				 "perhaps already in-use?",
234 				 domain, bus, slot, func);
235 		goto out;
236 	}
237 
238 	err = xen_pcibk_add_pci_dev(pdev, dev, devid,
239 				    xen_pcibk_publish_pci_dev);
240 	if (err)
241 		goto out;
242 
243 	dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
244 	if (xen_register_device_domain_owner(dev,
245 					     pdev->xdev->otherend_id) != 0) {
246 		dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
247 			xen_find_device_domain_owner(dev));
248 		xen_unregister_device_domain_owner(dev);
249 		xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
250 	}
251 
252 	/* TODO: It'd be nice to export a bridge and have all of its children
253 	 * get exported with it. This may be best done in xend (which will
254 	 * have to calculate resource usage anyway) but we probably want to
255 	 * put something in here to ensure that if a bridge gets given to a
256 	 * driver domain, that all devices under that bridge are not given
257 	 * to other driver domains (as he who controls the bridge can disable
258 	 * it and stop the other devices from working).
259 	 */
260 out:
261 	return err;
262 }
263 
264 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
265 				 int domain, int bus, int slot, int func)
266 {
267 	int err = 0;
268 	struct pci_dev *dev;
269 
270 	dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
271 		domain, bus, slot, func);
272 
273 	dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
274 	if (!dev) {
275 		err = -EINVAL;
276 		dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
277 			"(%04x:%02x:%02x.%01x)! not owned by this domain\n",
278 			domain, bus, slot, func);
279 		goto out;
280 	}
281 
282 	dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
283 	xen_unregister_device_domain_owner(dev);
284 
285 	xen_pcibk_release_pci_dev(pdev, dev);
286 
287 out:
288 	return err;
289 }
290 
291 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
292 				    unsigned int domain, unsigned int bus)
293 {
294 	unsigned int d, b;
295 	int i, root_num, len, err;
296 	char str[64];
297 
298 	dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
299 
300 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
301 			   "root_num", "%d", &root_num);
302 	if (err == 0 || err == -ENOENT)
303 		root_num = 0;
304 	else if (err < 0)
305 		goto out;
306 
307 	/* Verify that we haven't already published this pci root */
308 	for (i = 0; i < root_num; i++) {
309 		len = snprintf(str, sizeof(str), "root-%d", i);
310 		if (unlikely(len >= (sizeof(str) - 1))) {
311 			err = -ENOMEM;
312 			goto out;
313 		}
314 
315 		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
316 				   str, "%x:%x", &d, &b);
317 		if (err < 0)
318 			goto out;
319 		if (err != 2) {
320 			err = -EINVAL;
321 			goto out;
322 		}
323 
324 		if (d == domain && b == bus) {
325 			err = 0;
326 			goto out;
327 		}
328 	}
329 
330 	len = snprintf(str, sizeof(str), "root-%d", root_num);
331 	if (unlikely(len >= (sizeof(str) - 1))) {
332 		err = -ENOMEM;
333 		goto out;
334 	}
335 
336 	dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
337 		root_num, domain, bus);
338 
339 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
340 			    "%04x:%02x", domain, bus);
341 	if (err)
342 		goto out;
343 
344 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
345 			    "root_num", "%d", (root_num + 1));
346 
347 out:
348 	return err;
349 }
350 
351 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
352 {
353 	int err = 0;
354 	int num_devs;
355 	int domain, bus, slot, func;
356 	int substate;
357 	int i, len;
358 	char state_str[64];
359 	char dev_str[64];
360 
361 
362 	dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
363 
364 	mutex_lock(&pdev->dev_lock);
365 	/* Make sure we only reconfigure once */
366 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
367 	    XenbusStateReconfiguring)
368 		goto out;
369 
370 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
371 			   &num_devs);
372 	if (err != 1) {
373 		if (err >= 0)
374 			err = -EINVAL;
375 		xenbus_dev_fatal(pdev->xdev, err,
376 				 "Error reading number of devices");
377 		goto out;
378 	}
379 
380 	for (i = 0; i < num_devs; i++) {
381 		len = snprintf(state_str, sizeof(state_str), "state-%d", i);
382 		if (unlikely(len >= (sizeof(state_str) - 1))) {
383 			err = -ENOMEM;
384 			xenbus_dev_fatal(pdev->xdev, err,
385 					 "String overflow while reading "
386 					 "configuration");
387 			goto out;
388 		}
389 		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
390 				   "%d", &substate);
391 		if (err != 1)
392 			substate = XenbusStateUnknown;
393 
394 		switch (substate) {
395 		case XenbusStateInitialising:
396 			dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
397 
398 			len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
399 			if (unlikely(len >= (sizeof(dev_str) - 1))) {
400 				err = -ENOMEM;
401 				xenbus_dev_fatal(pdev->xdev, err,
402 						 "String overflow while "
403 						 "reading configuration");
404 				goto out;
405 			}
406 			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
407 					   dev_str, "%x:%x:%x.%x",
408 					   &domain, &bus, &slot, &func);
409 			if (err < 0) {
410 				xenbus_dev_fatal(pdev->xdev, err,
411 						 "Error reading device "
412 						 "configuration");
413 				goto out;
414 			}
415 			if (err != 4) {
416 				err = -EINVAL;
417 				xenbus_dev_fatal(pdev->xdev, err,
418 						 "Error parsing pci device "
419 						 "configuration");
420 				goto out;
421 			}
422 
423 			err = xen_pcibk_export_device(pdev, domain, bus, slot,
424 						    func, i);
425 			if (err)
426 				goto out;
427 
428 			/* Publish pci roots. */
429 			err = xen_pcibk_publish_pci_roots(pdev,
430 						xen_pcibk_publish_pci_root);
431 			if (err) {
432 				xenbus_dev_fatal(pdev->xdev, err,
433 						 "Error while publish PCI root"
434 						 "buses for frontend");
435 				goto out;
436 			}
437 
438 			err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
439 					    state_str, "%d",
440 					    XenbusStateInitialised);
441 			if (err) {
442 				xenbus_dev_fatal(pdev->xdev, err,
443 						 "Error switching substate of "
444 						 "dev-%d\n", i);
445 				goto out;
446 			}
447 			break;
448 
449 		case XenbusStateClosing:
450 			dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
451 
452 			len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
453 			if (unlikely(len >= (sizeof(dev_str) - 1))) {
454 				err = -ENOMEM;
455 				xenbus_dev_fatal(pdev->xdev, err,
456 						 "String overflow while "
457 						 "reading configuration");
458 				goto out;
459 			}
460 			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
461 					   dev_str, "%x:%x:%x.%x",
462 					   &domain, &bus, &slot, &func);
463 			if (err < 0) {
464 				xenbus_dev_fatal(pdev->xdev, err,
465 						 "Error reading device "
466 						 "configuration");
467 				goto out;
468 			}
469 			if (err != 4) {
470 				err = -EINVAL;
471 				xenbus_dev_fatal(pdev->xdev, err,
472 						 "Error parsing pci device "
473 						 "configuration");
474 				goto out;
475 			}
476 
477 			err = xen_pcibk_remove_device(pdev, domain, bus, slot,
478 						    func);
479 			if (err)
480 				goto out;
481 
482 			/* TODO: If at some point we implement support for pci
483 			 * root hot-remove on pcifront side, we'll need to
484 			 * remove unnecessary xenstore nodes of pci roots here.
485 			 */
486 
487 			break;
488 
489 		default:
490 			break;
491 		}
492 	}
493 
494 	err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
495 	if (err) {
496 		xenbus_dev_fatal(pdev->xdev, err,
497 				 "Error switching to reconfigured state!");
498 		goto out;
499 	}
500 
501 out:
502 	mutex_unlock(&pdev->dev_lock);
503 	return 0;
504 }
505 
506 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
507 				     enum xenbus_state fe_state)
508 {
509 	struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
510 
511 	dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
512 
513 	switch (fe_state) {
514 	case XenbusStateInitialised:
515 		xen_pcibk_attach(pdev);
516 		break;
517 
518 	case XenbusStateReconfiguring:
519 		xen_pcibk_reconfigure(pdev);
520 		break;
521 
522 	case XenbusStateConnected:
523 		/* pcifront switched its state from reconfiguring to connected.
524 		 * Then switch to connected state.
525 		 */
526 		xenbus_switch_state(xdev, XenbusStateConnected);
527 		break;
528 
529 	case XenbusStateClosing:
530 		xen_pcibk_disconnect(pdev);
531 		xenbus_switch_state(xdev, XenbusStateClosing);
532 		break;
533 
534 	case XenbusStateClosed:
535 		xen_pcibk_disconnect(pdev);
536 		xenbus_switch_state(xdev, XenbusStateClosed);
537 		if (xenbus_dev_is_online(xdev))
538 			break;
539 		/* fall through if not online */
540 	case XenbusStateUnknown:
541 		dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
542 		device_unregister(&xdev->dev);
543 		break;
544 
545 	default:
546 		break;
547 	}
548 }
549 
550 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
551 {
552 	/* Get configuration from xend (if available now) */
553 	int domain, bus, slot, func;
554 	int err = 0;
555 	int i, num_devs;
556 	char dev_str[64];
557 	char state_str[64];
558 
559 	mutex_lock(&pdev->dev_lock);
560 	/* It's possible we could get the call to setup twice, so make sure
561 	 * we're not already connected.
562 	 */
563 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
564 	    XenbusStateInitWait)
565 		goto out;
566 
567 	dev_dbg(&pdev->xdev->dev, "getting be setup\n");
568 
569 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
570 			   &num_devs);
571 	if (err != 1) {
572 		if (err >= 0)
573 			err = -EINVAL;
574 		xenbus_dev_fatal(pdev->xdev, err,
575 				 "Error reading number of devices");
576 		goto out;
577 	}
578 
579 	for (i = 0; i < num_devs; i++) {
580 		int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
581 		if (unlikely(l >= (sizeof(dev_str) - 1))) {
582 			err = -ENOMEM;
583 			xenbus_dev_fatal(pdev->xdev, err,
584 					 "String overflow while reading "
585 					 "configuration");
586 			goto out;
587 		}
588 
589 		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
590 				   "%x:%x:%x.%x", &domain, &bus, &slot, &func);
591 		if (err < 0) {
592 			xenbus_dev_fatal(pdev->xdev, err,
593 					 "Error reading device configuration");
594 			goto out;
595 		}
596 		if (err != 4) {
597 			err = -EINVAL;
598 			xenbus_dev_fatal(pdev->xdev, err,
599 					 "Error parsing pci device "
600 					 "configuration");
601 			goto out;
602 		}
603 
604 		err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
605 		if (err)
606 			goto out;
607 
608 		/* Switch substate of this device. */
609 		l = snprintf(state_str, sizeof(state_str), "state-%d", i);
610 		if (unlikely(l >= (sizeof(state_str) - 1))) {
611 			err = -ENOMEM;
612 			xenbus_dev_fatal(pdev->xdev, err,
613 					 "String overflow while reading "
614 					 "configuration");
615 			goto out;
616 		}
617 		err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
618 				    "%d", XenbusStateInitialised);
619 		if (err) {
620 			xenbus_dev_fatal(pdev->xdev, err, "Error switching "
621 					 "substate of dev-%d\n", i);
622 			goto out;
623 		}
624 	}
625 
626 	err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
627 	if (err) {
628 		xenbus_dev_fatal(pdev->xdev, err,
629 				 "Error while publish PCI root buses "
630 				 "for frontend");
631 		goto out;
632 	}
633 
634 	err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
635 	if (err)
636 		xenbus_dev_fatal(pdev->xdev, err,
637 				 "Error switching to initialised state!");
638 
639 out:
640 	mutex_unlock(&pdev->dev_lock);
641 	if (!err)
642 		/* see if pcifront is already configured (if not, we'll wait) */
643 		xen_pcibk_attach(pdev);
644 	return err;
645 }
646 
647 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
648 			     const char **vec, unsigned int len)
649 {
650 	struct xen_pcibk_device *pdev =
651 	    container_of(watch, struct xen_pcibk_device, be_watch);
652 
653 	switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
654 	case XenbusStateInitWait:
655 		xen_pcibk_setup_backend(pdev);
656 		break;
657 
658 	default:
659 		break;
660 	}
661 }
662 
663 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
664 				const struct xenbus_device_id *id)
665 {
666 	int err = 0;
667 	struct xen_pcibk_device *pdev = alloc_pdev(dev);
668 
669 	if (pdev == NULL) {
670 		err = -ENOMEM;
671 		xenbus_dev_fatal(dev, err,
672 				 "Error allocating xen_pcibk_device struct");
673 		goto out;
674 	}
675 
676 	/* wait for xend to configure us */
677 	err = xenbus_switch_state(dev, XenbusStateInitWait);
678 	if (err)
679 		goto out;
680 
681 	/* watch the backend node for backend configuration information */
682 	err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
683 				xen_pcibk_be_watch);
684 	if (err)
685 		goto out;
686 
687 	pdev->be_watching = 1;
688 
689 	/* We need to force a call to our callback here in case
690 	 * xend already configured us!
691 	 */
692 	xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
693 
694 out:
695 	return err;
696 }
697 
698 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
699 {
700 	struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
701 
702 	if (pdev != NULL)
703 		free_pdev(pdev);
704 
705 	return 0;
706 }
707 
708 static const struct xenbus_device_id xen_pcibk_ids[] = {
709 	{"pci"},
710 	{""},
711 };
712 
713 static DEFINE_XENBUS_DRIVER(xen_pcibk, DRV_NAME,
714 	.probe			= xen_pcibk_xenbus_probe,
715 	.remove			= xen_pcibk_xenbus_remove,
716 	.otherend_changed	= xen_pcibk_frontend_changed,
717 );
718 
719 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
720 
721 int __init xen_pcibk_xenbus_register(void)
722 {
723 	xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
724 	if (!xen_pcibk_wq) {
725 		printk(KERN_ERR "%s: create"
726 			"xen_pciback_workqueue failed\n", __func__);
727 		return -EFAULT;
728 	}
729 	xen_pcibk_backend = &xen_pcibk_vpci_backend;
730 	if (passthrough)
731 		xen_pcibk_backend = &xen_pcibk_passthrough_backend;
732 	pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
733 	return xenbus_register_backend(&xen_pcibk_driver);
734 }
735 
736 void __exit xen_pcibk_xenbus_unregister(void)
737 {
738 	destroy_workqueue(xen_pcibk_wq);
739 	xenbus_unregister_driver(&xen_pcibk_driver);
740 }
741