ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 9665:bb97f5120d03

Fix the test inside all_devices_ready, and move it from xenbus_probe (a
postcore_initcall) to a new late_initcall, so that it happens after the
drivers have initialised.

Fixes the reopened bug #549 (I hope).

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Apr 10 18:17:54 2006 +0100 (2006-04-10)
parents d0d3fef37685
children b32bc0c5648d
line source
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 */
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
36 #include <linux/kernel.h>
37 #include <linux/err.h>
38 #include <linux/string.h>
39 #include <linux/ctype.h>
40 #include <linux/fcntl.h>
41 #include <linux/mm.h>
42 #include <linux/notifier.h>
43 #include <linux/kthread.h>
45 #include <asm/io.h>
46 #include <asm/page.h>
47 #include <asm/pgtable.h>
48 #include <asm/hypervisor.h>
49 #include <xen/xenbus.h>
50 #include <xen/xen_proc.h>
51 #include <xen/evtchn.h>
52 #include <xen/features.h>
54 #include "xenbus_comms.h"
56 extern struct mutex xenwatch_mutex;
58 static struct notifier_block *xenstore_chain;
60 /* If something in array of ids matches this device, return it. */
61 static const struct xenbus_device_id *
62 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
63 {
64 for (; *arr->devicetype != '\0'; arr++) {
65 if (!strcmp(arr->devicetype, dev->devicetype))
66 return arr;
67 }
68 return NULL;
69 }
71 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
72 {
73 struct xenbus_driver *drv = to_xenbus_driver(_drv);
75 if (!drv->ids)
76 return 0;
78 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
79 }
81 struct xen_bus_type
82 {
83 char *root;
84 unsigned int levels;
85 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
86 int (*probe)(const char *type, const char *dir);
87 struct bus_type bus;
88 struct device dev;
89 };
92 /* device/<type>/<id> => <type>-<id> */
93 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
94 {
95 nodename = strchr(nodename, '/');
96 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
97 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
98 return -EINVAL;
99 }
101 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
102 if (!strchr(bus_id, '/')) {
103 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
104 return -EINVAL;
105 }
106 *strchr(bus_id, '/') = '-';
107 return 0;
108 }
111 static void free_otherend_details(struct xenbus_device *dev)
112 {
113 kfree(dev->otherend);
114 dev->otherend = NULL;
115 }
118 static void free_otherend_watch(struct xenbus_device *dev)
119 {
120 if (dev->otherend_watch.node) {
121 unregister_xenbus_watch(&dev->otherend_watch);
122 kfree(dev->otherend_watch.node);
123 dev->otherend_watch.node = NULL;
124 }
125 }
128 static int read_otherend_details(struct xenbus_device *xendev,
129 char *id_node, char *path_node)
130 {
131 int err = xenbus_gather(XBT_NULL, xendev->nodename,
132 id_node, "%i", &xendev->otherend_id,
133 path_node, NULL, &xendev->otherend,
134 NULL);
135 if (err) {
136 xenbus_dev_fatal(xendev, err,
137 "reading other end details from %s",
138 xendev->nodename);
139 return err;
140 }
141 if (strlen(xendev->otherend) == 0 ||
142 !xenbus_exists(XBT_NULL, xendev->otherend, "")) {
143 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
144 xendev->nodename);
145 free_otherend_details(xendev);
146 return -ENOENT;
147 }
149 return 0;
150 }
153 static int read_backend_details(struct xenbus_device *xendev)
154 {
155 return read_otherend_details(xendev, "backend-id", "backend");
156 }
159 static int read_frontend_details(struct xenbus_device *xendev)
160 {
161 return read_otherend_details(xendev, "frontend-id", "frontend");
162 }
165 /* Bus type for frontend drivers. */
166 static int xenbus_probe_frontend(const char *type, const char *name);
167 static struct xen_bus_type xenbus_frontend = {
168 .root = "device",
169 .levels = 2, /* device/type/<id> */
170 .get_bus_id = frontend_bus_id,
171 .probe = xenbus_probe_frontend,
172 .bus = {
173 .name = "xen",
174 .match = xenbus_match,
175 },
176 .dev = {
177 .bus_id = "xen",
178 },
179 };
181 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
182 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
183 {
184 int domid, err;
185 const char *devid, *type, *frontend;
186 unsigned int typelen;
188 type = strchr(nodename, '/');
189 if (!type)
190 return -EINVAL;
191 type++;
192 typelen = strcspn(type, "/");
193 if (!typelen || type[typelen] != '/')
194 return -EINVAL;
196 devid = strrchr(nodename, '/') + 1;
198 err = xenbus_gather(XBT_NULL, nodename, "frontend-id", "%i", &domid,
199 "frontend", NULL, &frontend,
200 NULL);
201 if (err)
202 return err;
203 if (strlen(frontend) == 0)
204 err = -ERANGE;
205 if (!err && !xenbus_exists(XBT_NULL, frontend, ""))
206 err = -ENOENT;
208 kfree(frontend);
210 if (err)
211 return err;
213 if (snprintf(bus_id, BUS_ID_SIZE,
214 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
215 return -ENOSPC;
216 return 0;
217 }
219 static int xenbus_uevent_backend(struct device *dev, char **envp,
220 int num_envp, char *buffer, int buffer_size);
221 static int xenbus_probe_backend(const char *type, const char *domid);
222 static struct xen_bus_type xenbus_backend = {
223 .root = "backend",
224 .levels = 3, /* backend/type/<frontend>/<id> */
225 .get_bus_id = backend_bus_id,
226 .probe = xenbus_probe_backend,
227 .bus = {
228 .name = "xen-backend",
229 .match = xenbus_match,
230 .uevent = xenbus_uevent_backend,
231 },
232 .dev = {
233 .bus_id = "xen-backend",
234 },
235 };
237 static int xenbus_uevent_backend(struct device *dev, char **envp,
238 int num_envp, char *buffer, int buffer_size)
239 {
240 struct xenbus_device *xdev;
241 struct xenbus_driver *drv;
242 int i = 0;
243 int length = 0;
245 DPRINTK("");
247 if (dev == NULL)
248 return -ENODEV;
250 xdev = to_xenbus_device(dev);
251 if (xdev == NULL)
252 return -ENODEV;
254 /* stuff we want to pass to /sbin/hotplug */
255 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
256 "XENBUS_TYPE=%s", xdev->devicetype);
258 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
259 "XENBUS_PATH=%s", xdev->nodename);
261 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
262 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
264 /* terminate, set to next free slot, shrink available space */
265 envp[i] = NULL;
266 envp = &envp[i];
267 num_envp -= i;
268 buffer = &buffer[length];
269 buffer_size -= length;
271 if (dev->driver) {
272 drv = to_xenbus_driver(dev->driver);
273 if (drv && drv->uevent)
274 return drv->uevent(xdev, envp, num_envp, buffer,
275 buffer_size);
276 }
278 return 0;
279 }
281 static void otherend_changed(struct xenbus_watch *watch,
282 const char **vec, unsigned int len)
283 {
284 struct xenbus_device *dev =
285 container_of(watch, struct xenbus_device, otherend_watch);
286 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
287 XenbusState state;
289 /* Protect us against watches firing on old details when the otherend
290 details change, say immediately after a resume. */
291 if (!dev->otherend ||
292 strncmp(dev->otherend, vec[XS_WATCH_PATH],
293 strlen(dev->otherend))) {
294 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
295 return;
296 }
298 state = xenbus_read_driver_state(dev->otherend);
300 DPRINTK("state is %d, %s, %s",
301 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
302 if (drv->otherend_changed)
303 drv->otherend_changed(dev, state);
304 }
307 static int talk_to_otherend(struct xenbus_device *dev)
308 {
309 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
311 free_otherend_watch(dev);
312 free_otherend_details(dev);
314 return drv->read_otherend_details(dev);
315 }
318 static int watch_otherend(struct xenbus_device *dev)
319 {
320 return xenbus_watch_path2(dev, dev->otherend, "state",
321 &dev->otherend_watch, otherend_changed);
322 }
325 static int xenbus_dev_probe(struct device *_dev)
326 {
327 struct xenbus_device *dev = to_xenbus_device(_dev);
328 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
329 const struct xenbus_device_id *id;
330 int err;
332 DPRINTK("");
334 if (!drv->probe) {
335 err = -ENODEV;
336 goto fail;
337 }
339 id = match_device(drv->ids, dev);
340 if (!id) {
341 err = -ENODEV;
342 goto fail;
343 }
345 err = talk_to_otherend(dev);
346 if (err) {
347 printk(KERN_WARNING
348 "xenbus_probe: talk_to_otherend on %s failed.\n",
349 dev->nodename);
350 return err;
351 }
353 err = drv->probe(dev, id);
354 if (err)
355 goto fail;
357 err = watch_otherend(dev);
358 if (err) {
359 printk(KERN_WARNING
360 "xenbus_probe: watch_otherend on %s failed.\n",
361 dev->nodename);
362 return err;
363 }
365 return 0;
366 fail:
367 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
368 xenbus_switch_state(dev, XenbusStateClosed);
369 return -ENODEV;
370 }
372 static int xenbus_dev_remove(struct device *_dev)
373 {
374 struct xenbus_device *dev = to_xenbus_device(_dev);
375 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
377 DPRINTK("");
379 free_otherend_watch(dev);
380 free_otherend_details(dev);
382 if (drv->remove)
383 drv->remove(dev);
385 xenbus_switch_state(dev, XenbusStateClosed);
386 return 0;
387 }
389 static int xenbus_register_driver_common(struct xenbus_driver *drv,
390 struct xen_bus_type *bus)
391 {
392 int ret;
394 drv->driver.name = drv->name;
395 drv->driver.bus = &bus->bus;
396 drv->driver.owner = drv->owner;
397 drv->driver.probe = xenbus_dev_probe;
398 drv->driver.remove = xenbus_dev_remove;
400 mutex_lock(&xenwatch_mutex);
401 ret = driver_register(&drv->driver);
402 mutex_unlock(&xenwatch_mutex);
403 return ret;
404 }
406 int xenbus_register_frontend(struct xenbus_driver *drv)
407 {
408 drv->read_otherend_details = read_backend_details;
410 return xenbus_register_driver_common(drv, &xenbus_frontend);
411 }
412 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
414 int xenbus_register_backend(struct xenbus_driver *drv)
415 {
416 drv->read_otherend_details = read_frontend_details;
418 return xenbus_register_driver_common(drv, &xenbus_backend);
419 }
420 EXPORT_SYMBOL_GPL(xenbus_register_backend);
422 void xenbus_unregister_driver(struct xenbus_driver *drv)
423 {
424 driver_unregister(&drv->driver);
425 }
426 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
428 struct xb_find_info
429 {
430 struct xenbus_device *dev;
431 const char *nodename;
432 };
434 static int cmp_dev(struct device *dev, void *data)
435 {
436 struct xenbus_device *xendev = to_xenbus_device(dev);
437 struct xb_find_info *info = data;
439 if (!strcmp(xendev->nodename, info->nodename)) {
440 info->dev = xendev;
441 get_device(dev);
442 return 1;
443 }
444 return 0;
445 }
447 struct xenbus_device *xenbus_device_find(const char *nodename,
448 struct bus_type *bus)
449 {
450 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
452 bus_for_each_dev(bus, NULL, &info, cmp_dev);
453 return info.dev;
454 }
456 static int cleanup_dev(struct device *dev, void *data)
457 {
458 struct xenbus_device *xendev = to_xenbus_device(dev);
459 struct xb_find_info *info = data;
460 int len = strlen(info->nodename);
462 DPRINTK("%s", info->nodename);
464 /* Match the info->nodename path, or any subdirectory of that path. */
465 if (strncmp(xendev->nodename, info->nodename, len))
466 return 0;
468 /* If the node name is longer, ensure it really is a subdirectory. */
469 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
470 return 0;
472 info->dev = xendev;
473 get_device(dev);
474 return 1;
475 }
477 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
478 {
479 struct xb_find_info info = { .nodename = path };
481 do {
482 info.dev = NULL;
483 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
484 if (info.dev) {
485 device_unregister(&info.dev->dev);
486 put_device(&info.dev->dev);
487 }
488 } while (info.dev);
489 }
491 static void xenbus_dev_release(struct device *dev)
492 {
493 if (dev)
494 kfree(to_xenbus_device(dev));
495 }
497 /* Simplified asprintf. */
498 char *kasprintf(const char *fmt, ...)
499 {
500 va_list ap;
501 unsigned int len;
502 char *p, dummy[1];
504 va_start(ap, fmt);
505 /* FIXME: vsnprintf has a bug, NULL should work */
506 len = vsnprintf(dummy, 0, fmt, ap);
507 va_end(ap);
509 p = kmalloc(len + 1, GFP_KERNEL);
510 if (!p)
511 return NULL;
512 va_start(ap, fmt);
513 vsprintf(p, fmt, ap);
514 va_end(ap);
515 return p;
516 }
518 static ssize_t xendev_show_nodename(struct device *dev,
519 struct device_attribute *attr, char *buf)
520 {
521 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
522 }
523 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
525 static ssize_t xendev_show_devtype(struct device *dev,
526 struct device_attribute *attr, char *buf)
527 {
528 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
529 }
530 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
533 static int xenbus_probe_node(struct xen_bus_type *bus,
534 const char *type,
535 const char *nodename)
536 {
537 int err;
538 struct xenbus_device *xendev;
539 size_t stringlen;
540 char *tmpstring;
542 XenbusState state = xenbus_read_driver_state(nodename);
544 if (state != XenbusStateInitialising) {
545 /* Device is not new, so ignore it. This can happen if a
546 device is going away after switching to Closed. */
547 return 0;
548 }
550 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
551 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
552 if (!xendev)
553 return -ENOMEM;
555 /* Copy the strings into the extra space. */
557 tmpstring = (char *)(xendev + 1);
558 strcpy(tmpstring, nodename);
559 xendev->nodename = tmpstring;
561 tmpstring += strlen(tmpstring) + 1;
562 strcpy(tmpstring, type);
563 xendev->devicetype = tmpstring;
565 xendev->dev.parent = &bus->dev;
566 xendev->dev.bus = &bus->bus;
567 xendev->dev.release = xenbus_dev_release;
569 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
570 if (err)
571 goto fail;
573 /* Register with generic device framework. */
574 err = device_register(&xendev->dev);
575 if (err)
576 goto fail;
578 device_create_file(&xendev->dev, &dev_attr_nodename);
579 device_create_file(&xendev->dev, &dev_attr_devtype);
581 return 0;
582 fail:
583 kfree(xendev);
584 return err;
585 }
587 /* device/<typename>/<name> */
588 static int xenbus_probe_frontend(const char *type, const char *name)
589 {
590 char *nodename;
591 int err;
593 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
594 if (!nodename)
595 return -ENOMEM;
597 DPRINTK("%s", nodename);
599 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
600 kfree(nodename);
601 return err;
602 }
604 /* backend/<typename>/<frontend-uuid>/<name> */
605 static int xenbus_probe_backend_unit(const char *dir,
606 const char *type,
607 const char *name)
608 {
609 char *nodename;
610 int err;
612 nodename = kasprintf("%s/%s", dir, name);
613 if (!nodename)
614 return -ENOMEM;
616 DPRINTK("%s\n", nodename);
618 err = xenbus_probe_node(&xenbus_backend, type, nodename);
619 kfree(nodename);
620 return err;
621 }
623 /* backend/<typename>/<frontend-domid> */
624 static int xenbus_probe_backend(const char *type, const char *domid)
625 {
626 char *nodename;
627 int err = 0;
628 char **dir;
629 unsigned int i, dir_n = 0;
631 DPRINTK("");
633 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
634 if (!nodename)
635 return -ENOMEM;
637 dir = xenbus_directory(XBT_NULL, nodename, "", &dir_n);
638 if (IS_ERR(dir)) {
639 kfree(nodename);
640 return PTR_ERR(dir);
641 }
643 for (i = 0; i < dir_n; i++) {
644 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
645 if (err)
646 break;
647 }
648 kfree(dir);
649 kfree(nodename);
650 return err;
651 }
653 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
654 {
655 int err = 0;
656 char **dir;
657 unsigned int dir_n = 0;
658 int i;
660 dir = xenbus_directory(XBT_NULL, bus->root, type, &dir_n);
661 if (IS_ERR(dir))
662 return PTR_ERR(dir);
664 for (i = 0; i < dir_n; i++) {
665 err = bus->probe(type, dir[i]);
666 if (err)
667 break;
668 }
669 kfree(dir);
670 return err;
671 }
673 static int xenbus_probe_devices(struct xen_bus_type *bus)
674 {
675 int err = 0;
676 char **dir;
677 unsigned int i, dir_n;
679 dir = xenbus_directory(XBT_NULL, bus->root, "", &dir_n);
680 if (IS_ERR(dir))
681 return PTR_ERR(dir);
683 for (i = 0; i < dir_n; i++) {
684 err = xenbus_probe_device_type(bus, dir[i]);
685 if (err)
686 break;
687 }
688 kfree(dir);
689 return err;
690 }
692 static unsigned int char_count(const char *str, char c)
693 {
694 unsigned int i, ret = 0;
696 for (i = 0; str[i]; i++)
697 if (str[i] == c)
698 ret++;
699 return ret;
700 }
702 static int strsep_len(const char *str, char c, unsigned int len)
703 {
704 unsigned int i;
706 for (i = 0; str[i]; i++)
707 if (str[i] == c) {
708 if (len == 0)
709 return i;
710 len--;
711 }
712 return (len == 0) ? i : -ERANGE;
713 }
715 static void dev_changed(const char *node, struct xen_bus_type *bus)
716 {
717 int exists, rootlen;
718 struct xenbus_device *dev;
719 char type[BUS_ID_SIZE];
720 const char *p, *root;
722 if (char_count(node, '/') < 2)
723 return;
725 exists = xenbus_exists(XBT_NULL, node, "");
726 if (!exists) {
727 xenbus_cleanup_devices(node, &bus->bus);
728 return;
729 }
731 /* backend/<type>/... or device/<type>/... */
732 p = strchr(node, '/') + 1;
733 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
734 type[BUS_ID_SIZE-1] = '\0';
736 rootlen = strsep_len(node, '/', bus->levels);
737 if (rootlen < 0)
738 return;
739 root = kasprintf("%.*s", rootlen, node);
740 if (!root)
741 return;
743 dev = xenbus_device_find(root, &bus->bus);
744 if (!dev)
745 xenbus_probe_node(bus, type, root);
746 else
747 put_device(&dev->dev);
749 kfree(root);
750 }
752 static void frontend_changed(struct xenbus_watch *watch,
753 const char **vec, unsigned int len)
754 {
755 DPRINTK("");
757 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
758 }
760 static void backend_changed(struct xenbus_watch *watch,
761 const char **vec, unsigned int len)
762 {
763 DPRINTK("");
765 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
766 }
768 /* We watch for devices appearing and vanishing. */
769 static struct xenbus_watch fe_watch = {
770 .node = "device",
771 .callback = frontend_changed,
772 };
774 static struct xenbus_watch be_watch = {
775 .node = "backend",
776 .callback = backend_changed,
777 };
779 static int suspend_dev(struct device *dev, void *data)
780 {
781 int err = 0;
782 struct xenbus_driver *drv;
783 struct xenbus_device *xdev;
785 DPRINTK("");
787 if (dev->driver == NULL)
788 return 0;
789 drv = to_xenbus_driver(dev->driver);
790 xdev = container_of(dev, struct xenbus_device, dev);
791 if (drv->suspend)
792 err = drv->suspend(xdev);
793 if (err)
794 printk(KERN_WARNING
795 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
796 return 0;
797 }
799 static int resume_dev(struct device *dev, void *data)
800 {
801 int err;
802 struct xenbus_driver *drv;
803 struct xenbus_device *xdev;
805 DPRINTK("");
807 if (dev->driver == NULL)
808 return 0;
809 drv = to_xenbus_driver(dev->driver);
810 xdev = container_of(dev, struct xenbus_device, dev);
812 err = talk_to_otherend(xdev);
813 if (err) {
814 printk(KERN_WARNING
815 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
816 dev->bus_id, err);
817 return err;
818 }
820 err = watch_otherend(xdev);
821 if (err) {
822 printk(KERN_WARNING
823 "xenbus_probe: resume (watch_otherend) %s failed: "
824 "%d.\n", dev->bus_id, err);
825 return err;
826 }
828 if (drv->resume)
829 err = drv->resume(xdev);
830 if (err)
831 printk(KERN_WARNING
832 "xenbus: resume %s failed: %i\n", dev->bus_id, err);
833 return err;
834 }
836 void xenbus_suspend(void)
837 {
838 DPRINTK("");
840 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
841 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
842 xs_suspend();
843 }
844 EXPORT_SYMBOL_GPL(xenbus_suspend);
846 void xenbus_resume(void)
847 {
848 xb_init_comms();
849 xs_resume();
850 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
851 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
852 }
853 EXPORT_SYMBOL_GPL(xenbus_resume);
856 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
857 int xenstored_ready = 0;
860 int register_xenstore_notifier(struct notifier_block *nb)
861 {
862 int ret = 0;
864 if (xenstored_ready > 0)
865 ret = nb->notifier_call(nb, 0, NULL);
866 else
867 notifier_chain_register(&xenstore_chain, nb);
869 return ret;
870 }
871 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
873 void unregister_xenstore_notifier(struct notifier_block *nb)
874 {
875 notifier_chain_unregister(&xenstore_chain, nb);
876 }
877 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
880 static int all_devices_ready_(struct device *dev, void *data)
881 {
882 struct xenbus_device *xendev = to_xenbus_device(dev);
883 int *result = data;
885 if (xendev->state != XenbusStateConnected) {
886 *result = 0;
887 return 1;
888 }
890 return 0;
891 }
894 static int all_devices_ready(void)
895 {
896 int ready = 1;
897 bus_for_each_dev(&xenbus_frontend.bus, NULL, &ready,
898 all_devices_ready_);
899 return ready;
900 }
903 void xenbus_probe(void *unused)
904 {
905 BUG_ON((xenstored_ready <= 0));
907 /* Enumerate devices in xenstore. */
908 xenbus_probe_devices(&xenbus_frontend);
909 xenbus_probe_devices(&xenbus_backend);
911 /* Watch for changes. */
912 register_xenbus_watch(&fe_watch);
913 register_xenbus_watch(&be_watch);
915 /* Notify others that xenstore is up */
916 notifier_call_chain(&xenstore_chain, 0, NULL);
917 }
920 static struct file_operations xsd_kva_fops;
921 static struct proc_dir_entry *xsd_kva_intf;
922 static struct proc_dir_entry *xsd_port_intf;
924 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
925 {
926 size_t size = vma->vm_end - vma->vm_start;
928 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
929 return -EINVAL;
931 if (remap_pfn_range(vma, vma->vm_start,
932 mfn_to_pfn(xen_start_info->store_mfn),
933 size, vma->vm_page_prot))
934 return -EAGAIN;
936 return 0;
937 }
939 static int xsd_kva_read(char *page, char **start, off_t off,
940 int count, int *eof, void *data)
941 {
942 int len;
944 len = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
945 *eof = 1;
946 return len;
947 }
949 static int xsd_port_read(char *page, char **start, off_t off,
950 int count, int *eof, void *data)
951 {
952 int len;
954 len = sprintf(page, "%d", xen_start_info->store_evtchn);
955 *eof = 1;
956 return len;
957 }
960 static int __init xenbus_probe_init(void)
961 {
962 int err = 0, dom0;
963 unsigned long page = 0;
965 DPRINTK("");
967 if (xen_init() < 0) {
968 DPRINTK("failed");
969 return -ENODEV;
970 }
972 /* Register ourselves with the kernel bus subsystem */
973 bus_register(&xenbus_frontend.bus);
974 bus_register(&xenbus_backend.bus);
976 /*
977 * Domain0 doesn't have a store_evtchn or store_mfn yet.
978 */
979 dom0 = (xen_start_info->store_evtchn == 0);
981 if (dom0) {
982 evtchn_op_t op = { 0 };
984 /* Allocate page. */
985 page = get_zeroed_page(GFP_KERNEL);
986 if (!page)
987 return -ENOMEM;
989 xen_start_info->store_mfn =
990 pfn_to_mfn(virt_to_phys((void *)page) >>
991 PAGE_SHIFT);
993 /* Next allocate a local port which xenstored can bind to */
994 op.cmd = EVTCHNOP_alloc_unbound;
995 op.u.alloc_unbound.dom = DOMID_SELF;
996 op.u.alloc_unbound.remote_dom = 0;
998 err = HYPERVISOR_event_channel_op(&op);
999 if (err == -ENOSYS)
1000 goto err;
1001 BUG_ON(err);
1002 xen_start_info->store_evtchn = op.u.alloc_unbound.port;
1004 /* And finally publish the above info in /proc/xen */
1005 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1006 if (xsd_kva_intf) {
1007 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1008 sizeof(xsd_kva_fops));
1009 xsd_kva_fops.mmap = xsd_kva_mmap;
1010 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1011 xsd_kva_intf->read_proc = xsd_kva_read;
1013 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1014 if (xsd_port_intf)
1015 xsd_port_intf->read_proc = xsd_port_read;
1016 } else
1017 xenstored_ready = 1;
1019 /* Initialize the interface to xenstore. */
1020 err = xs_init();
1021 if (err) {
1022 printk(KERN_WARNING
1023 "XENBUS: Error initializing xenstore comms: %i\n", err);
1024 goto err;
1027 /* Register ourselves with the kernel device subsystem */
1028 device_register(&xenbus_frontend.dev);
1029 device_register(&xenbus_backend.dev);
1031 if (!dom0)
1032 xenbus_probe(NULL);
1034 return 0;
1036 err:
1037 if (page)
1038 free_page(page);
1040 /*
1041 * Do not unregister the xenbus front/backend buses here. The
1042 * buses must exist because front/backend drivers will use
1043 * them when they are registered.
1044 */
1046 return err;
1049 postcore_initcall(xenbus_probe_init);
1052 /*
1053 * On a 10 second timeout, wait for all devices currently configured. We need
1054 * to do this to guarantee that the filesystems and / or network devices
1055 * needed for boot are available, before we can allow the boot to proceed.
1057 * This needs to be on a late_initcall, to happen after the frontend device
1058 * drivers have been initialised, but before the root fs is mounted.
1060 * A possible improvement here would be to have the tools add a per-device
1061 * flag to the store entry, indicating whether it is needed at boot time.
1062 * This would allow people who knew what they were doing to accelerate their
1063 * boot slightly, but of course needs tools or manual intervention to set up
1064 * those flags correctly.
1065 */
1066 static int __init wait_for_devices(void)
1068 int i;
1070 for (i = 0; i < 10 * HZ; i++) {
1071 if (all_devices_ready()) {
1072 return;
1075 set_current_state(TASK_INTERRUPTIBLE);
1076 schedule_timeout(1);
1079 printk(KERN_WARNING
1080 "XENBUS: Timeout connecting to devices!\n");
1083 late_initcall(wait_for_devices);
1086 /*
1087 * Local variables:
1088 * c-file-style: "linux"
1089 * indent-tabs-mode: t
1090 * c-indent-level: 8
1091 * c-basic-offset: 8
1092 * tab-width: 8
1093 * End:
1094 */