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