1 /******************************************************************************
2  * Talks to Xen Store to figure out what devices we have.
3  *
4  * Copyright (C) 2005 Rusty Russell, IBM Corporation
5  * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6  * Copyright (C) 2005, 2006 XenSource Ltd
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32 
33 #define DPRINTK(fmt, args...)				\
34 	pr_debug("xenbus_probe (%s:%d) " fmt ".\n",	\
35 		 __func__, __LINE__, ##args)
36 
37 #include <linux/kernel.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/ctype.h>
41 #include <linux/fcntl.h>
42 #include <linux/mm.h>
43 #include <linux/proc_fs.h>
44 #include <linux/notifier.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
47 #include <linux/io.h>
48 
49 #include <asm/page.h>
50 #include <asm/pgtable.h>
51 #include <asm/xen/hypervisor.h>
52 
53 #include <xen/xen.h>
54 #include <xen/xenbus.h>
55 #include <xen/events.h>
56 #include <xen/page.h>
57 
58 #include "xenbus_comms.h"
59 #include "xenbus_probe.h"
60 
61 
62 int xen_store_evtchn;
63 EXPORT_SYMBOL(xen_store_evtchn);
64 
65 struct xenstore_domain_interface *xen_store_interface;
66 static unsigned long xen_store_mfn;
67 
68 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
69 
70 static void wait_for_devices(struct xenbus_driver *xendrv);
71 
72 static int xenbus_probe_frontend(const char *type, const char *name);
73 
74 static void xenbus_dev_shutdown(struct device *_dev);
75 
76 static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
77 static int xenbus_dev_resume(struct device *dev);
78 
79 /* If something in array of ids matches this device, return it. */
80 static const struct xenbus_device_id *
81 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
82 {
83 	for (; *arr->devicetype != '\0'; arr++) {
84 		if (!strcmp(arr->devicetype, dev->devicetype))
85 			return arr;
86 	}
87 	return NULL;
88 }
89 
90 int xenbus_match(struct device *_dev, struct device_driver *_drv)
91 {
92 	struct xenbus_driver *drv = to_xenbus_driver(_drv);
93 
94 	if (!drv->ids)
95 		return 0;
96 
97 	return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
98 }
99 
100 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
101 {
102 	struct xenbus_device *dev = to_xenbus_device(_dev);
103 
104 	if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
105 		return -ENOMEM;
106 
107 	return 0;
108 }
109 
110 /* device/<type>/<id> => <type>-<id> */
111 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
112 {
113 	nodename = strchr(nodename, '/');
114 	if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
115 		printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
116 		return -EINVAL;
117 	}
118 
119 	strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
120 	if (!strchr(bus_id, '/')) {
121 		printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
122 		return -EINVAL;
123 	}
124 	*strchr(bus_id, '/') = '-';
125 	return 0;
126 }
127 
128 
129 static void free_otherend_details(struct xenbus_device *dev)
130 {
131 	kfree(dev->otherend);
132 	dev->otherend = NULL;
133 }
134 
135 
136 static void free_otherend_watch(struct xenbus_device *dev)
137 {
138 	if (dev->otherend_watch.node) {
139 		unregister_xenbus_watch(&dev->otherend_watch);
140 		kfree(dev->otherend_watch.node);
141 		dev->otherend_watch.node = NULL;
142 	}
143 }
144 
145 
146 int read_otherend_details(struct xenbus_device *xendev,
147 				 char *id_node, char *path_node)
148 {
149 	int err = xenbus_gather(XBT_NIL, xendev->nodename,
150 				id_node, "%i", &xendev->otherend_id,
151 				path_node, NULL, &xendev->otherend,
152 				NULL);
153 	if (err) {
154 		xenbus_dev_fatal(xendev, err,
155 				 "reading other end details from %s",
156 				 xendev->nodename);
157 		return err;
158 	}
159 	if (strlen(xendev->otherend) == 0 ||
160 	    !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
161 		xenbus_dev_fatal(xendev, -ENOENT,
162 				 "unable to read other end from %s.  "
163 				 "missing or inaccessible.",
164 				 xendev->nodename);
165 		free_otherend_details(xendev);
166 		return -ENOENT;
167 	}
168 
169 	return 0;
170 }
171 
172 
173 static int read_backend_details(struct xenbus_device *xendev)
174 {
175 	return read_otherend_details(xendev, "backend-id", "backend");
176 }
177 
178 static struct device_attribute xenbus_dev_attrs[] = {
179 	__ATTR_NULL
180 };
181 
182 /* Bus type for frontend drivers. */
183 static struct xen_bus_type xenbus_frontend = {
184 	.root = "device",
185 	.levels = 2, 		/* device/type/<id> */
186 	.get_bus_id = frontend_bus_id,
187 	.probe = xenbus_probe_frontend,
188 	.bus = {
189 		.name      = "xen",
190 		.match     = xenbus_match,
191 		.uevent    = xenbus_uevent,
192 		.probe     = xenbus_dev_probe,
193 		.remove    = xenbus_dev_remove,
194 		.shutdown  = xenbus_dev_shutdown,
195 		.dev_attrs = xenbus_dev_attrs,
196 
197 		.suspend   = xenbus_dev_suspend,
198 		.resume    = xenbus_dev_resume,
199 	},
200 };
201 
202 static void otherend_changed(struct xenbus_watch *watch,
203 			     const char **vec, unsigned int len)
204 {
205 	struct xenbus_device *dev =
206 		container_of(watch, struct xenbus_device, otherend_watch);
207 	struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
208 	enum xenbus_state state;
209 
210 	/* Protect us against watches firing on old details when the otherend
211 	   details change, say immediately after a resume. */
212 	if (!dev->otherend ||
213 	    strncmp(dev->otherend, vec[XS_WATCH_PATH],
214 		    strlen(dev->otherend))) {
215 		dev_dbg(&dev->dev, "Ignoring watch at %s\n",
216 			vec[XS_WATCH_PATH]);
217 		return;
218 	}
219 
220 	state = xenbus_read_driver_state(dev->otherend);
221 
222 	dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
223 		state, xenbus_strstate(state), dev->otherend_watch.node,
224 		vec[XS_WATCH_PATH]);
225 
226 	/*
227 	 * Ignore xenbus transitions during shutdown. This prevents us doing
228 	 * work that can fail e.g., when the rootfs is gone.
229 	 */
230 	if (system_state > SYSTEM_RUNNING) {
231 		struct xen_bus_type *bus = bus;
232 		bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
233 		/* If we're frontend, drive the state machine to Closed. */
234 		/* This should cause the backend to release our resources. */
235 		if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
236 			xenbus_frontend_closed(dev);
237 		return;
238 	}
239 
240 	if (drv->otherend_changed)
241 		drv->otherend_changed(dev, state);
242 }
243 
244 
245 static int talk_to_otherend(struct xenbus_device *dev)
246 {
247 	struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
248 
249 	free_otherend_watch(dev);
250 	free_otherend_details(dev);
251 
252 	return drv->read_otherend_details(dev);
253 }
254 
255 
256 static int watch_otherend(struct xenbus_device *dev)
257 {
258 	return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
259 				    "%s/%s", dev->otherend, "state");
260 }
261 
262 
263 int xenbus_dev_probe(struct device *_dev)
264 {
265 	struct xenbus_device *dev = to_xenbus_device(_dev);
266 	struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
267 	const struct xenbus_device_id *id;
268 	int err;
269 
270 	DPRINTK("%s", dev->nodename);
271 
272 	if (!drv->probe) {
273 		err = -ENODEV;
274 		goto fail;
275 	}
276 
277 	id = match_device(drv->ids, dev);
278 	if (!id) {
279 		err = -ENODEV;
280 		goto fail;
281 	}
282 
283 	err = talk_to_otherend(dev);
284 	if (err) {
285 		dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
286 			 dev->nodename);
287 		return err;
288 	}
289 
290 	err = drv->probe(dev, id);
291 	if (err)
292 		goto fail;
293 
294 	err = watch_otherend(dev);
295 	if (err) {
296 		dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
297 		       dev->nodename);
298 		return err;
299 	}
300 
301 	return 0;
302 fail:
303 	xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
304 	xenbus_switch_state(dev, XenbusStateClosed);
305 	return -ENODEV;
306 }
307 
308 int xenbus_dev_remove(struct device *_dev)
309 {
310 	struct xenbus_device *dev = to_xenbus_device(_dev);
311 	struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
312 
313 	DPRINTK("%s", dev->nodename);
314 
315 	free_otherend_watch(dev);
316 	free_otherend_details(dev);
317 
318 	if (drv->remove)
319 		drv->remove(dev);
320 
321 	xenbus_switch_state(dev, XenbusStateClosed);
322 	return 0;
323 }
324 
325 static void xenbus_dev_shutdown(struct device *_dev)
326 {
327 	struct xenbus_device *dev = to_xenbus_device(_dev);
328 	unsigned long timeout = 5*HZ;
329 
330 	DPRINTK("%s", dev->nodename);
331 
332 	get_device(&dev->dev);
333 	if (dev->state != XenbusStateConnected) {
334 		printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
335 		       dev->nodename, xenbus_strstate(dev->state));
336 		goto out;
337 	}
338 	xenbus_switch_state(dev, XenbusStateClosing);
339 	timeout = wait_for_completion_timeout(&dev->down, timeout);
340 	if (!timeout)
341 		printk(KERN_INFO "%s: %s timeout closing device\n",
342 		       __func__, dev->nodename);
343  out:
344 	put_device(&dev->dev);
345 }
346 
347 int xenbus_register_driver_common(struct xenbus_driver *drv,
348 				  struct xen_bus_type *bus,
349 				  struct module *owner,
350 				  const char *mod_name)
351 {
352 	drv->driver.name = drv->name;
353 	drv->driver.bus = &bus->bus;
354 	drv->driver.owner = owner;
355 	drv->driver.mod_name = mod_name;
356 
357 	return driver_register(&drv->driver);
358 }
359 
360 int __xenbus_register_frontend(struct xenbus_driver *drv,
361 			       struct module *owner, const char *mod_name)
362 {
363 	int ret;
364 
365 	drv->read_otherend_details = read_backend_details;
366 
367 	ret = xenbus_register_driver_common(drv, &xenbus_frontend,
368 					    owner, mod_name);
369 	if (ret)
370 		return ret;
371 
372 	/* If this driver is loaded as a module wait for devices to attach. */
373 	wait_for_devices(drv);
374 
375 	return 0;
376 }
377 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
378 
379 void xenbus_unregister_driver(struct xenbus_driver *drv)
380 {
381 	driver_unregister(&drv->driver);
382 }
383 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
384 
385 struct xb_find_info
386 {
387 	struct xenbus_device *dev;
388 	const char *nodename;
389 };
390 
391 static int cmp_dev(struct device *dev, void *data)
392 {
393 	struct xenbus_device *xendev = to_xenbus_device(dev);
394 	struct xb_find_info *info = data;
395 
396 	if (!strcmp(xendev->nodename, info->nodename)) {
397 		info->dev = xendev;
398 		get_device(dev);
399 		return 1;
400 	}
401 	return 0;
402 }
403 
404 struct xenbus_device *xenbus_device_find(const char *nodename,
405 					 struct bus_type *bus)
406 {
407 	struct xb_find_info info = { .dev = NULL, .nodename = nodename };
408 
409 	bus_for_each_dev(bus, NULL, &info, cmp_dev);
410 	return info.dev;
411 }
412 
413 static int cleanup_dev(struct device *dev, void *data)
414 {
415 	struct xenbus_device *xendev = to_xenbus_device(dev);
416 	struct xb_find_info *info = data;
417 	int len = strlen(info->nodename);
418 
419 	DPRINTK("%s", info->nodename);
420 
421 	/* Match the info->nodename path, or any subdirectory of that path. */
422 	if (strncmp(xendev->nodename, info->nodename, len))
423 		return 0;
424 
425 	/* If the node name is longer, ensure it really is a subdirectory. */
426 	if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
427 		return 0;
428 
429 	info->dev = xendev;
430 	get_device(dev);
431 	return 1;
432 }
433 
434 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
435 {
436 	struct xb_find_info info = { .nodename = path };
437 
438 	do {
439 		info.dev = NULL;
440 		bus_for_each_dev(bus, NULL, &info, cleanup_dev);
441 		if (info.dev) {
442 			device_unregister(&info.dev->dev);
443 			put_device(&info.dev->dev);
444 		}
445 	} while (info.dev);
446 }
447 
448 static void xenbus_dev_release(struct device *dev)
449 {
450 	if (dev)
451 		kfree(to_xenbus_device(dev));
452 }
453 
454 static ssize_t xendev_show_nodename(struct device *dev,
455 				    struct device_attribute *attr, char *buf)
456 {
457 	return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
458 }
459 static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
460 
461 static ssize_t xendev_show_devtype(struct device *dev,
462 				   struct device_attribute *attr, char *buf)
463 {
464 	return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
465 }
466 static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
467 
468 static ssize_t xendev_show_modalias(struct device *dev,
469 				    struct device_attribute *attr, char *buf)
470 {
471 	return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
472 }
473 static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
474 
475 int xenbus_probe_node(struct xen_bus_type *bus,
476 		      const char *type,
477 		      const char *nodename)
478 {
479 	char devname[XEN_BUS_ID_SIZE];
480 	int err;
481 	struct xenbus_device *xendev;
482 	size_t stringlen;
483 	char *tmpstring;
484 
485 	enum xenbus_state state = xenbus_read_driver_state(nodename);
486 
487 	if (state != XenbusStateInitialising) {
488 		/* Device is not new, so ignore it.  This can happen if a
489 		   device is going away after switching to Closed.  */
490 		return 0;
491 	}
492 
493 	stringlen = strlen(nodename) + 1 + strlen(type) + 1;
494 	xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
495 	if (!xendev)
496 		return -ENOMEM;
497 
498 	xendev->state = XenbusStateInitialising;
499 
500 	/* Copy the strings into the extra space. */
501 
502 	tmpstring = (char *)(xendev + 1);
503 	strcpy(tmpstring, nodename);
504 	xendev->nodename = tmpstring;
505 
506 	tmpstring += strlen(tmpstring) + 1;
507 	strcpy(tmpstring, type);
508 	xendev->devicetype = tmpstring;
509 	init_completion(&xendev->down);
510 
511 	xendev->dev.bus = &bus->bus;
512 	xendev->dev.release = xenbus_dev_release;
513 
514 	err = bus->get_bus_id(devname, xendev->nodename);
515 	if (err)
516 		goto fail;
517 
518 	dev_set_name(&xendev->dev, devname);
519 
520 	/* Register with generic device framework. */
521 	err = device_register(&xendev->dev);
522 	if (err)
523 		goto fail;
524 
525 	err = device_create_file(&xendev->dev, &dev_attr_nodename);
526 	if (err)
527 		goto fail_unregister;
528 
529 	err = device_create_file(&xendev->dev, &dev_attr_devtype);
530 	if (err)
531 		goto fail_remove_nodename;
532 
533 	err = device_create_file(&xendev->dev, &dev_attr_modalias);
534 	if (err)
535 		goto fail_remove_devtype;
536 
537 	return 0;
538 fail_remove_devtype:
539 	device_remove_file(&xendev->dev, &dev_attr_devtype);
540 fail_remove_nodename:
541 	device_remove_file(&xendev->dev, &dev_attr_nodename);
542 fail_unregister:
543 	device_unregister(&xendev->dev);
544 fail:
545 	kfree(xendev);
546 	return err;
547 }
548 
549 /* device/<typename>/<name> */
550 static int xenbus_probe_frontend(const char *type, const char *name)
551 {
552 	char *nodename;
553 	int err;
554 
555 	nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
556 			     xenbus_frontend.root, type, name);
557 	if (!nodename)
558 		return -ENOMEM;
559 
560 	DPRINTK("%s", nodename);
561 
562 	err = xenbus_probe_node(&xenbus_frontend, type, nodename);
563 	kfree(nodename);
564 	return err;
565 }
566 
567 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
568 {
569 	int err = 0;
570 	char **dir;
571 	unsigned int dir_n = 0;
572 	int i;
573 
574 	dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
575 	if (IS_ERR(dir))
576 		return PTR_ERR(dir);
577 
578 	for (i = 0; i < dir_n; i++) {
579 		err = bus->probe(type, dir[i]);
580 		if (err)
581 			break;
582 	}
583 	kfree(dir);
584 	return err;
585 }
586 
587 int xenbus_probe_devices(struct xen_bus_type *bus)
588 {
589 	int err = 0;
590 	char **dir;
591 	unsigned int i, dir_n;
592 
593 	dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
594 	if (IS_ERR(dir))
595 		return PTR_ERR(dir);
596 
597 	for (i = 0; i < dir_n; i++) {
598 		err = xenbus_probe_device_type(bus, dir[i]);
599 		if (err)
600 			break;
601 	}
602 	kfree(dir);
603 	return err;
604 }
605 
606 static unsigned int char_count(const char *str, char c)
607 {
608 	unsigned int i, ret = 0;
609 
610 	for (i = 0; str[i]; i++)
611 		if (str[i] == c)
612 			ret++;
613 	return ret;
614 }
615 
616 static int strsep_len(const char *str, char c, unsigned int len)
617 {
618 	unsigned int i;
619 
620 	for (i = 0; str[i]; i++)
621 		if (str[i] == c) {
622 			if (len == 0)
623 				return i;
624 			len--;
625 		}
626 	return (len == 0) ? i : -ERANGE;
627 }
628 
629 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
630 {
631 	int exists, rootlen;
632 	struct xenbus_device *dev;
633 	char type[XEN_BUS_ID_SIZE];
634 	const char *p, *root;
635 
636 	if (char_count(node, '/') < 2)
637 		return;
638 
639 	exists = xenbus_exists(XBT_NIL, node, "");
640 	if (!exists) {
641 		xenbus_cleanup_devices(node, &bus->bus);
642 		return;
643 	}
644 
645 	/* backend/<type>/... or device/<type>/... */
646 	p = strchr(node, '/') + 1;
647 	snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
648 	type[XEN_BUS_ID_SIZE-1] = '\0';
649 
650 	rootlen = strsep_len(node, '/', bus->levels);
651 	if (rootlen < 0)
652 		return;
653 	root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
654 	if (!root)
655 		return;
656 
657 	dev = xenbus_device_find(root, &bus->bus);
658 	if (!dev)
659 		xenbus_probe_node(bus, type, root);
660 	else
661 		put_device(&dev->dev);
662 
663 	kfree(root);
664 }
665 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
666 
667 static void frontend_changed(struct xenbus_watch *watch,
668 			     const char **vec, unsigned int len)
669 {
670 	DPRINTK("");
671 
672 	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
673 }
674 
675 /* We watch for devices appearing and vanishing. */
676 static struct xenbus_watch fe_watch = {
677 	.node = "device",
678 	.callback = frontend_changed,
679 };
680 
681 static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
682 {
683 	int err = 0;
684 	struct xenbus_driver *drv;
685 	struct xenbus_device *xdev;
686 
687 	DPRINTK("");
688 
689 	if (dev->driver == NULL)
690 		return 0;
691 	drv = to_xenbus_driver(dev->driver);
692 	xdev = container_of(dev, struct xenbus_device, dev);
693 	if (drv->suspend)
694 		err = drv->suspend(xdev, state);
695 	if (err)
696 		printk(KERN_WARNING
697 		       "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
698 	return 0;
699 }
700 
701 static int xenbus_dev_resume(struct device *dev)
702 {
703 	int err;
704 	struct xenbus_driver *drv;
705 	struct xenbus_device *xdev;
706 
707 	DPRINTK("");
708 
709 	if (dev->driver == NULL)
710 		return 0;
711 
712 	drv = to_xenbus_driver(dev->driver);
713 	xdev = container_of(dev, struct xenbus_device, dev);
714 
715 	err = talk_to_otherend(xdev);
716 	if (err) {
717 		printk(KERN_WARNING
718 		       "xenbus: resume (talk_to_otherend) %s failed: %i\n",
719 		       dev_name(dev), err);
720 		return err;
721 	}
722 
723 	xdev->state = XenbusStateInitialising;
724 
725 	if (drv->resume) {
726 		err = drv->resume(xdev);
727 		if (err) {
728 			printk(KERN_WARNING
729 			       "xenbus: resume %s failed: %i\n",
730 			       dev_name(dev), err);
731 			return err;
732 		}
733 	}
734 
735 	err = watch_otherend(xdev);
736 	if (err) {
737 		printk(KERN_WARNING
738 		       "xenbus_probe: resume (watch_otherend) %s failed: "
739 		       "%d.\n", dev_name(dev), err);
740 		return err;
741 	}
742 
743 	return 0;
744 }
745 
746 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
747 int xenstored_ready = 0;
748 
749 
750 int register_xenstore_notifier(struct notifier_block *nb)
751 {
752 	int ret = 0;
753 
754 	if (xenstored_ready > 0)
755 		ret = nb->notifier_call(nb, 0, NULL);
756 	else
757 		blocking_notifier_chain_register(&xenstore_chain, nb);
758 
759 	return ret;
760 }
761 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
762 
763 void unregister_xenstore_notifier(struct notifier_block *nb)
764 {
765 	blocking_notifier_chain_unregister(&xenstore_chain, nb);
766 }
767 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
768 
769 void xenbus_probe(struct work_struct *unused)
770 {
771 	BUG_ON((xenstored_ready <= 0));
772 
773 	/* Enumerate devices in xenstore and watch for changes. */
774 	xenbus_probe_devices(&xenbus_frontend);
775 	register_xenbus_watch(&fe_watch);
776 	xenbus_backend_probe_and_watch();
777 
778 	/* Notify others that xenstore is up */
779 	blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
780 }
781 
782 static int __init xenbus_probe_init(void)
783 {
784 	int err = 0;
785 
786 	DPRINTK("");
787 
788 	err = -ENODEV;
789 	if (!xen_domain())
790 		goto out_error;
791 
792 	/* Register ourselves with the kernel bus subsystem */
793 	err = bus_register(&xenbus_frontend.bus);
794 	if (err)
795 		goto out_error;
796 
797 	err = xenbus_backend_bus_register();
798 	if (err)
799 		goto out_unreg_front;
800 
801 	/*
802 	 * Domain0 doesn't have a store_evtchn or store_mfn yet.
803 	 */
804 	if (xen_initial_domain()) {
805 		/* dom0 not yet supported */
806 	} else {
807 		xenstored_ready = 1;
808 		xen_store_evtchn = xen_start_info->store_evtchn;
809 		xen_store_mfn = xen_start_info->store_mfn;
810 	}
811 	xen_store_interface = mfn_to_virt(xen_store_mfn);
812 
813 	/* Initialize the interface to xenstore. */
814 	err = xs_init();
815 	if (err) {
816 		printk(KERN_WARNING
817 		       "XENBUS: Error initializing xenstore comms: %i\n", err);
818 		goto out_unreg_back;
819 	}
820 
821 	if (!xen_initial_domain())
822 		xenbus_probe(NULL);
823 
824 #ifdef CONFIG_XEN_COMPAT_XENFS
825 	/*
826 	 * Create xenfs mountpoint in /proc for compatibility with
827 	 * utilities that expect to find "xenbus" under "/proc/xen".
828 	 */
829 	proc_mkdir("xen", NULL);
830 #endif
831 
832 	return 0;
833 
834   out_unreg_back:
835 	xenbus_backend_bus_unregister();
836 
837   out_unreg_front:
838 	bus_unregister(&xenbus_frontend.bus);
839 
840   out_error:
841 	return err;
842 }
843 
844 postcore_initcall(xenbus_probe_init);
845 
846 MODULE_LICENSE("GPL");
847 
848 static int is_device_connecting(struct device *dev, void *data)
849 {
850 	struct xenbus_device *xendev = to_xenbus_device(dev);
851 	struct device_driver *drv = data;
852 	struct xenbus_driver *xendrv;
853 
854 	/*
855 	 * A device with no driver will never connect. We care only about
856 	 * devices which should currently be in the process of connecting.
857 	 */
858 	if (!dev->driver)
859 		return 0;
860 
861 	/* Is this search limited to a particular driver? */
862 	if (drv && (dev->driver != drv))
863 		return 0;
864 
865 	xendrv = to_xenbus_driver(dev->driver);
866 	return (xendev->state < XenbusStateConnected ||
867 		(xendev->state == XenbusStateConnected &&
868 		 xendrv->is_ready && !xendrv->is_ready(xendev)));
869 }
870 
871 static int exists_connecting_device(struct device_driver *drv)
872 {
873 	return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
874 				is_device_connecting);
875 }
876 
877 static int print_device_status(struct device *dev, void *data)
878 {
879 	struct xenbus_device *xendev = to_xenbus_device(dev);
880 	struct device_driver *drv = data;
881 
882 	/* Is this operation limited to a particular driver? */
883 	if (drv && (dev->driver != drv))
884 		return 0;
885 
886 	if (!dev->driver) {
887 		/* Information only: is this too noisy? */
888 		printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
889 		       xendev->nodename);
890 	} else if (xendev->state < XenbusStateConnected) {
891 		enum xenbus_state rstate = XenbusStateUnknown;
892 		if (xendev->otherend)
893 			rstate = xenbus_read_driver_state(xendev->otherend);
894 		printk(KERN_WARNING "XENBUS: Timeout connecting "
895 		       "to device: %s (local state %d, remote state %d)\n",
896 		       xendev->nodename, xendev->state, rstate);
897 	}
898 
899 	return 0;
900 }
901 
902 /* We only wait for device setup after most initcalls have run. */
903 static int ready_to_wait_for_devices;
904 
905 /*
906  * On a 5-minute timeout, wait for all devices currently configured.  We need
907  * to do this to guarantee that the filesystems and / or network devices
908  * needed for boot are available, before we can allow the boot to proceed.
909  *
910  * This needs to be on a late_initcall, to happen after the frontend device
911  * drivers have been initialised, but before the root fs is mounted.
912  *
913  * A possible improvement here would be to have the tools add a per-device
914  * flag to the store entry, indicating whether it is needed at boot time.
915  * This would allow people who knew what they were doing to accelerate their
916  * boot slightly, but of course needs tools or manual intervention to set up
917  * those flags correctly.
918  */
919 static void wait_for_devices(struct xenbus_driver *xendrv)
920 {
921 	unsigned long start = jiffies;
922 	struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
923 	unsigned int seconds_waited = 0;
924 
925 	if (!ready_to_wait_for_devices || !xen_domain())
926 		return;
927 
928 	while (exists_connecting_device(drv)) {
929 		if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
930 			if (!seconds_waited)
931 				printk(KERN_WARNING "XENBUS: Waiting for "
932 				       "devices to initialise: ");
933 			seconds_waited += 5;
934 			printk("%us...", 300 - seconds_waited);
935 			if (seconds_waited == 300)
936 				break;
937 		}
938 
939 		schedule_timeout_interruptible(HZ/10);
940 	}
941 
942 	if (seconds_waited)
943 		printk("\n");
944 
945 	bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
946 			 print_device_status);
947 }
948 
949 #ifndef MODULE
950 static int __init boot_wait_for_devices(void)
951 {
952 	ready_to_wait_for_devices = 1;
953 	wait_for_devices(NULL);
954 	return 0;
955 }
956 
957 late_initcall(boot_wait_for_devices);
958 #endif
959