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