direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 10575:a377304fc4e6

[XENBUS] Another simplification -- bus_for_each_dev() better than bus_find_device().
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Jun 30 22:02:18 2006 +0100 (2006-06-30)
parents 4d7565ba9ff3
children ee6014279103
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", \
35 __FUNCTION__, __LINE__, ##args)
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>
46 #include <asm/io.h>
47 #include <asm/page.h>
48 #include <asm/pgtable.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <xen/evtchn.h>
53 #include <xen/features.h>
55 #include "xenbus_comms.h"
57 extern struct mutex xenwatch_mutex;
59 static struct notifier_block *xenstore_chain;
61 /* If something in array of ids matches this device, return it. */
62 static const struct xenbus_device_id *
63 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
64 {
65 for (; *arr->devicetype != '\0'; arr++) {
66 if (!strcmp(arr->devicetype, dev->devicetype))
67 return arr;
68 }
69 return NULL;
70 }
72 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
73 {
74 struct xenbus_driver *drv = to_xenbus_driver(_drv);
76 if (!drv->ids)
77 return 0;
79 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
80 }
82 struct xen_bus_type
83 {
84 char *root;
85 unsigned int levels;
86 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
87 int (*probe)(const char *type, const char *dir);
88 struct bus_type bus;
89 struct device dev;
90 };
93 /* device/<type>/<id> => <type>-<id> */
94 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
95 {
96 nodename = strchr(nodename, '/');
97 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
98 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
99 return -EINVAL;
100 }
102 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
103 if (!strchr(bus_id, '/')) {
104 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
105 return -EINVAL;
106 }
107 *strchr(bus_id, '/') = '-';
108 return 0;
109 }
112 static void free_otherend_details(struct xenbus_device *dev)
113 {
114 kfree(dev->otherend);
115 dev->otherend = NULL;
116 }
119 static void free_otherend_watch(struct xenbus_device *dev)
120 {
121 if (dev->otherend_watch.node) {
122 unregister_xenbus_watch(&dev->otherend_watch);
123 kfree(dev->otherend_watch.node);
124 dev->otherend_watch.node = NULL;
125 }
126 }
129 static int read_otherend_details(struct xenbus_device *xendev,
130 char *id_node, char *path_node)
131 {
132 int err = xenbus_gather(XBT_NIL, xendev->nodename,
133 id_node, "%i", &xendev->otherend_id,
134 path_node, NULL, &xendev->otherend,
135 NULL);
136 if (err) {
137 xenbus_dev_fatal(xendev, err,
138 "reading other end details from %s",
139 xendev->nodename);
140 return err;
141 }
142 if (strlen(xendev->otherend) == 0 ||
143 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
144 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
145 xendev->nodename);
146 free_otherend_details(xendev);
147 return -ENOENT;
148 }
150 return 0;
151 }
154 static int read_backend_details(struct xenbus_device *xendev)
155 {
156 return read_otherend_details(xendev, "backend-id", "backend");
157 }
160 static int read_frontend_details(struct xenbus_device *xendev)
161 {
162 return read_otherend_details(xendev, "frontend-id", "frontend");
163 }
166 /* Bus type for frontend drivers. */
167 static int xenbus_probe_frontend(const char *type, const char *name);
168 static struct xen_bus_type xenbus_frontend = {
169 .root = "device",
170 .levels = 2, /* device/type/<id> */
171 .get_bus_id = frontend_bus_id,
172 .probe = xenbus_probe_frontend,
173 .bus = {
174 .name = "xen",
175 .match = xenbus_match,
176 },
177 .dev = {
178 .bus_id = "xen",
179 },
180 };
182 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
183 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
184 {
185 int domid, err;
186 const char *devid, *type, *frontend;
187 unsigned int typelen;
189 type = strchr(nodename, '/');
190 if (!type)
191 return -EINVAL;
192 type++;
193 typelen = strcspn(type, "/");
194 if (!typelen || type[typelen] != '/')
195 return -EINVAL;
197 devid = strrchr(nodename, '/') + 1;
199 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
200 "frontend", NULL, &frontend,
201 NULL);
202 if (err)
203 return err;
204 if (strlen(frontend) == 0)
205 err = -ERANGE;
206 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
207 err = -ENOENT;
209 kfree(frontend);
211 if (err)
212 return err;
214 if (snprintf(bus_id, BUS_ID_SIZE,
215 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
216 return -ENOSPC;
217 return 0;
218 }
220 static int xenbus_uevent_backend(struct device *dev, char **envp,
221 int num_envp, char *buffer, int buffer_size);
222 static int xenbus_probe_backend(const char *type, const char *domid);
223 static struct xen_bus_type xenbus_backend = {
224 .root = "backend",
225 .levels = 3, /* backend/type/<frontend>/<id> */
226 .get_bus_id = backend_bus_id,
227 .probe = xenbus_probe_backend,
228 .bus = {
229 .name = "xen-backend",
230 .match = xenbus_match,
231 .uevent = xenbus_uevent_backend,
232 },
233 .dev = {
234 .bus_id = "xen-backend",
235 },
236 };
238 static int xenbus_uevent_backend(struct device *dev, char **envp,
239 int num_envp, char *buffer, int buffer_size)
240 {
241 struct xenbus_device *xdev;
242 struct xenbus_driver *drv;
243 int i = 0;
244 int length = 0;
246 DPRINTK("");
248 if (dev == NULL)
249 return -ENODEV;
251 xdev = to_xenbus_device(dev);
252 if (xdev == NULL)
253 return -ENODEV;
255 /* stuff we want to pass to /sbin/hotplug */
256 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
257 "XENBUS_TYPE=%s", xdev->devicetype);
259 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
260 "XENBUS_PATH=%s", xdev->nodename);
262 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
263 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
265 /* terminate, set to next free slot, shrink available space */
266 envp[i] = NULL;
267 envp = &envp[i];
268 num_envp -= i;
269 buffer = &buffer[length];
270 buffer_size -= length;
272 if (dev->driver) {
273 drv = to_xenbus_driver(dev->driver);
274 if (drv && drv->uevent)
275 return drv->uevent(xdev, envp, num_envp, buffer,
276 buffer_size);
277 }
279 return 0;
280 }
282 static void otherend_changed(struct xenbus_watch *watch,
283 const char **vec, unsigned int len)
284 {
285 struct xenbus_device *dev =
286 container_of(watch, struct xenbus_device, otherend_watch);
287 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
288 enum xenbus_state state;
290 /* Protect us against watches firing on old details when the otherend
291 details change, say immediately after a resume. */
292 if (!dev->otherend ||
293 strncmp(dev->otherend, vec[XS_WATCH_PATH],
294 strlen(dev->otherend))) {
295 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
296 return;
297 }
299 state = xenbus_read_driver_state(dev->otherend);
301 DPRINTK("state is %d, %s, %s",
302 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
303 if (drv->otherend_changed)
304 drv->otherend_changed(dev, state);
305 }
308 static int talk_to_otherend(struct xenbus_device *dev)
309 {
310 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
312 free_otherend_watch(dev);
313 free_otherend_details(dev);
315 return drv->read_otherend_details(dev);
316 }
319 static int watch_otherend(struct xenbus_device *dev)
320 {
321 return xenbus_watch_path2(dev, dev->otherend, "state",
322 &dev->otherend_watch, otherend_changed);
323 }
326 static int xenbus_dev_probe(struct device *_dev)
327 {
328 struct xenbus_device *dev = to_xenbus_device(_dev);
329 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
330 const struct xenbus_device_id *id;
331 int err;
333 DPRINTK("");
335 if (!drv->probe) {
336 err = -ENODEV;
337 goto fail;
338 }
340 id = match_device(drv->ids, dev);
341 if (!id) {
342 err = -ENODEV;
343 goto fail;
344 }
346 err = talk_to_otherend(dev);
347 if (err) {
348 printk(KERN_WARNING
349 "xenbus_probe: talk_to_otherend on %s failed.\n",
350 dev->nodename);
351 return err;
352 }
354 err = drv->probe(dev, id);
355 if (err)
356 goto fail;
358 err = watch_otherend(dev);
359 if (err) {
360 printk(KERN_WARNING
361 "xenbus_probe: watch_otherend on %s failed.\n",
362 dev->nodename);
363 return err;
364 }
366 return 0;
367 fail:
368 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
369 xenbus_switch_state(dev, XenbusStateClosed);
370 return -ENODEV;
371 }
373 static int xenbus_dev_remove(struct device *_dev)
374 {
375 struct xenbus_device *dev = to_xenbus_device(_dev);
376 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
378 DPRINTK("");
380 free_otherend_watch(dev);
381 free_otherend_details(dev);
383 if (drv->remove)
384 drv->remove(dev);
386 xenbus_switch_state(dev, XenbusStateClosed);
387 return 0;
388 }
390 static int xenbus_register_driver_common(struct xenbus_driver *drv,
391 struct xen_bus_type *bus)
392 {
393 int ret;
395 drv->driver.name = drv->name;
396 drv->driver.bus = &bus->bus;
397 drv->driver.owner = drv->owner;
398 drv->driver.probe = xenbus_dev_probe;
399 drv->driver.remove = xenbus_dev_remove;
401 mutex_lock(&xenwatch_mutex);
402 ret = driver_register(&drv->driver);
403 mutex_unlock(&xenwatch_mutex);
404 return ret;
405 }
407 int xenbus_register_frontend(struct xenbus_driver *drv)
408 {
409 drv->read_otherend_details = read_backend_details;
411 return xenbus_register_driver_common(drv, &xenbus_frontend);
412 }
413 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
415 int xenbus_register_backend(struct xenbus_driver *drv)
416 {
417 drv->read_otherend_details = read_frontend_details;
419 return xenbus_register_driver_common(drv, &xenbus_backend);
420 }
421 EXPORT_SYMBOL_GPL(xenbus_register_backend);
423 void xenbus_unregister_driver(struct xenbus_driver *drv)
424 {
425 driver_unregister(&drv->driver);
426 }
427 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
429 struct xb_find_info
430 {
431 struct xenbus_device *dev;
432 const char *nodename;
433 };
435 static int cmp_dev(struct device *dev, void *data)
436 {
437 struct xenbus_device *xendev = to_xenbus_device(dev);
438 struct xb_find_info *info = data;
440 if (!strcmp(xendev->nodename, info->nodename)) {
441 info->dev = xendev;
442 get_device(dev);
443 return 1;
444 }
445 return 0;
446 }
448 struct xenbus_device *xenbus_device_find(const char *nodename,
449 struct bus_type *bus)
450 {
451 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
453 bus_for_each_dev(bus, NULL, &info, cmp_dev);
454 return info.dev;
455 }
457 static int cleanup_dev(struct device *dev, void *data)
458 {
459 struct xenbus_device *xendev = to_xenbus_device(dev);
460 struct xb_find_info *info = data;
461 int len = strlen(info->nodename);
463 DPRINTK("%s", info->nodename);
465 /* Match the info->nodename path, or any subdirectory of that path. */
466 if (strncmp(xendev->nodename, info->nodename, len))
467 return 0;
469 /* If the node name is longer, ensure it really is a subdirectory. */
470 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
471 return 0;
473 info->dev = xendev;
474 get_device(dev);
475 return 1;
476 }
478 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
479 {
480 struct xb_find_info info = { .nodename = path };
482 do {
483 info.dev = NULL;
484 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
485 if (info.dev) {
486 device_unregister(&info.dev->dev);
487 put_device(&info.dev->dev);
488 }
489 } while (info.dev);
490 }
492 static void xenbus_dev_release(struct device *dev)
493 {
494 if (dev)
495 kfree(to_xenbus_device(dev));
496 }
498 /* Simplified asprintf. */
499 char *kasprintf(const char *fmt, ...)
500 {
501 va_list ap;
502 unsigned int len;
503 char *p, dummy[1];
505 va_start(ap, fmt);
506 /* FIXME: vsnprintf has a bug, NULL should work */
507 len = vsnprintf(dummy, 0, fmt, ap);
508 va_end(ap);
510 p = kmalloc(len + 1, GFP_KERNEL);
511 if (!p)
512 return NULL;
513 va_start(ap, fmt);
514 vsprintf(p, fmt, ap);
515 va_end(ap);
516 return p;
517 }
519 static ssize_t xendev_show_nodename(struct device *dev,
520 struct device_attribute *attr, char *buf)
521 {
522 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
523 }
524 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
526 static ssize_t xendev_show_devtype(struct device *dev,
527 struct device_attribute *attr, char *buf)
528 {
529 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
530 }
531 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
534 static int xenbus_probe_node(struct xen_bus_type *bus,
535 const char *type,
536 const char *nodename)
537 {
538 int err;
539 struct xenbus_device *xendev;
540 size_t stringlen;
541 char *tmpstring;
543 enum xenbus_state state = xenbus_read_driver_state(nodename);
545 if (state != XenbusStateInitialising) {
546 /* Device is not new, so ignore it. This can happen if a
547 device is going away after switching to Closed. */
548 return 0;
549 }
551 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
552 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
553 if (!xendev)
554 return -ENOMEM;
556 /* Copy the strings into the extra space. */
558 tmpstring = (char *)(xendev + 1);
559 strcpy(tmpstring, nodename);
560 xendev->nodename = tmpstring;
562 tmpstring += strlen(tmpstring) + 1;
563 strcpy(tmpstring, type);
564 xendev->devicetype = tmpstring;
566 xendev->dev.parent = &bus->dev;
567 xendev->dev.bus = &bus->bus;
568 xendev->dev.release = xenbus_dev_release;
570 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
571 if (err)
572 goto fail;
574 /* Register with generic device framework. */
575 err = device_register(&xendev->dev);
576 if (err)
577 goto fail;
579 device_create_file(&xendev->dev, &dev_attr_nodename);
580 device_create_file(&xendev->dev, &dev_attr_devtype);
582 return 0;
583 fail:
584 kfree(xendev);
585 return err;
586 }
588 /* device/<typename>/<name> */
589 static int xenbus_probe_frontend(const char *type, const char *name)
590 {
591 char *nodename;
592 int err;
594 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
595 if (!nodename)
596 return -ENOMEM;
598 DPRINTK("%s", nodename);
600 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
601 kfree(nodename);
602 return err;
603 }
605 /* backend/<typename>/<frontend-uuid>/<name> */
606 static int xenbus_probe_backend_unit(const char *dir,
607 const char *type,
608 const char *name)
609 {
610 char *nodename;
611 int err;
613 nodename = kasprintf("%s/%s", dir, name);
614 if (!nodename)
615 return -ENOMEM;
617 DPRINTK("%s\n", nodename);
619 err = xenbus_probe_node(&xenbus_backend, type, nodename);
620 kfree(nodename);
621 return err;
622 }
624 /* backend/<typename>/<frontend-domid> */
625 static int xenbus_probe_backend(const char *type, const char *domid)
626 {
627 char *nodename;
628 int err = 0;
629 char **dir;
630 unsigned int i, dir_n = 0;
632 DPRINTK("");
634 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
635 if (!nodename)
636 return -ENOMEM;
638 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
639 if (IS_ERR(dir)) {
640 kfree(nodename);
641 return PTR_ERR(dir);
642 }
644 for (i = 0; i < dir_n; i++) {
645 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
646 if (err)
647 break;
648 }
649 kfree(dir);
650 kfree(nodename);
651 return err;
652 }
654 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
655 {
656 int err = 0;
657 char **dir;
658 unsigned int dir_n = 0;
659 int i;
661 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
662 if (IS_ERR(dir))
663 return PTR_ERR(dir);
665 for (i = 0; i < dir_n; i++) {
666 err = bus->probe(type, dir[i]);
667 if (err)
668 break;
669 }
670 kfree(dir);
671 return err;
672 }
674 static int xenbus_probe_devices(struct xen_bus_type *bus)
675 {
676 int err = 0;
677 char **dir;
678 unsigned int i, dir_n;
680 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
681 if (IS_ERR(dir))
682 return PTR_ERR(dir);
684 for (i = 0; i < dir_n; i++) {
685 err = xenbus_probe_device_type(bus, dir[i]);
686 if (err)
687 break;
688 }
689 kfree(dir);
690 return err;
691 }
693 static unsigned int char_count(const char *str, char c)
694 {
695 unsigned int i, ret = 0;
697 for (i = 0; str[i]; i++)
698 if (str[i] == c)
699 ret++;
700 return ret;
701 }
703 static int strsep_len(const char *str, char c, unsigned int len)
704 {
705 unsigned int i;
707 for (i = 0; str[i]; i++)
708 if (str[i] == c) {
709 if (len == 0)
710 return i;
711 len--;
712 }
713 return (len == 0) ? i : -ERANGE;
714 }
716 static void dev_changed(const char *node, struct xen_bus_type *bus)
717 {
718 int exists, rootlen;
719 struct xenbus_device *dev;
720 char type[BUS_ID_SIZE];
721 const char *p, *root;
723 if (char_count(node, '/') < 2)
724 return;
726 exists = xenbus_exists(XBT_NIL, node, "");
727 if (!exists) {
728 xenbus_cleanup_devices(node, &bus->bus);
729 return;
730 }
732 /* backend/<type>/... or device/<type>/... */
733 p = strchr(node, '/') + 1;
734 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
735 type[BUS_ID_SIZE-1] = '\0';
737 rootlen = strsep_len(node, '/', bus->levels);
738 if (rootlen < 0)
739 return;
740 root = kasprintf("%.*s", rootlen, node);
741 if (!root)
742 return;
744 dev = xenbus_device_find(root, &bus->bus);
745 if (!dev)
746 xenbus_probe_node(bus, type, root);
747 else
748 put_device(&dev->dev);
750 kfree(root);
751 }
753 static void frontend_changed(struct xenbus_watch *watch,
754 const char **vec, unsigned int len)
755 {
756 DPRINTK("");
758 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
759 }
761 static void backend_changed(struct xenbus_watch *watch,
762 const char **vec, unsigned int len)
763 {
764 DPRINTK("");
766 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
767 }
769 /* We watch for devices appearing and vanishing. */
770 static struct xenbus_watch fe_watch = {
771 .node = "device",
772 .callback = frontend_changed,
773 };
775 static struct xenbus_watch be_watch = {
776 .node = "backend",
777 .callback = backend_changed,
778 };
780 static int suspend_dev(struct device *dev, void *data)
781 {
782 int err = 0;
783 struct xenbus_driver *drv;
784 struct xenbus_device *xdev;
786 DPRINTK("");
788 if (dev->driver == NULL)
789 return 0;
790 drv = to_xenbus_driver(dev->driver);
791 xdev = container_of(dev, struct xenbus_device, dev);
792 if (drv->suspend)
793 err = drv->suspend(xdev);
794 if (err)
795 printk(KERN_WARNING
796 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
797 return 0;
798 }
800 static int resume_dev(struct device *dev, void *data)
801 {
802 int err;
803 struct xenbus_driver *drv;
804 struct xenbus_device *xdev;
806 DPRINTK("");
808 if (dev->driver == NULL)
809 return 0;
811 drv = to_xenbus_driver(dev->driver);
812 xdev = container_of(dev, struct xenbus_device, dev);
814 err = talk_to_otherend(xdev);
815 if (err) {
816 printk(KERN_WARNING
817 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
818 dev->bus_id, err);
819 return err;
820 }
822 xdev->state = XenbusStateInitialising;
824 if (drv->resume) {
825 err = drv->resume(xdev);
826 if (err) {
827 printk(KERN_WARNING
828 "xenbus: resume %s failed: %i\n",
829 dev->bus_id, err);
830 return err;
831 }
832 }
834 err = watch_otherend(xdev);
835 if (err) {
836 printk(KERN_WARNING
837 "xenbus_probe: resume (watch_otherend) %s failed: "
838 "%d.\n", dev->bus_id, err);
839 return err;
840 }
842 return 0;
843 }
845 void xenbus_suspend(void)
846 {
847 DPRINTK("");
849 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
850 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
851 xs_suspend();
852 }
853 EXPORT_SYMBOL_GPL(xenbus_suspend);
855 void xenbus_resume(void)
856 {
857 xb_init_comms();
858 xs_resume();
859 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
860 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
861 }
862 EXPORT_SYMBOL_GPL(xenbus_resume);
865 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
866 int xenstored_ready = 0;
869 int register_xenstore_notifier(struct notifier_block *nb)
870 {
871 int ret = 0;
873 if (xenstored_ready > 0)
874 ret = nb->notifier_call(nb, 0, NULL);
875 else
876 notifier_chain_register(&xenstore_chain, nb);
878 return ret;
879 }
880 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
882 void unregister_xenstore_notifier(struct notifier_block *nb)
883 {
884 notifier_chain_unregister(&xenstore_chain, nb);
885 }
886 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
889 void xenbus_probe(void *unused)
890 {
891 BUG_ON((xenstored_ready <= 0));
893 /* Enumerate devices in xenstore. */
894 xenbus_probe_devices(&xenbus_frontend);
895 xenbus_probe_devices(&xenbus_backend);
897 /* Watch for changes. */
898 register_xenbus_watch(&fe_watch);
899 register_xenbus_watch(&be_watch);
901 /* Notify others that xenstore is up */
902 notifier_call_chain(&xenstore_chain, 0, NULL);
903 }
906 #ifdef CONFIG_PROC_FS
907 static struct file_operations xsd_kva_fops;
908 static struct proc_dir_entry *xsd_kva_intf;
909 static struct proc_dir_entry *xsd_port_intf;
911 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
912 {
913 size_t size = vma->vm_end - vma->vm_start;
915 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
916 return -EINVAL;
918 if (remap_pfn_range(vma, vma->vm_start,
919 mfn_to_pfn(xen_start_info->store_mfn),
920 size, vma->vm_page_prot))
921 return -EAGAIN;
923 return 0;
924 }
926 static int xsd_kva_read(char *page, char **start, off_t off,
927 int count, int *eof, void *data)
928 {
929 int len;
931 len = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
932 *eof = 1;
933 return len;
934 }
936 static int xsd_port_read(char *page, char **start, off_t off,
937 int count, int *eof, void *data)
938 {
939 int len;
941 len = sprintf(page, "%d", xen_start_info->store_evtchn);
942 *eof = 1;
943 return len;
944 }
945 #endif
948 static int __init xenbus_probe_init(void)
949 {
950 int err = 0, dom0;
951 unsigned long page = 0;
953 DPRINTK("");
955 if (!is_running_on_xen())
956 return -ENODEV;
958 /* Register ourselves with the kernel bus subsystem */
959 bus_register(&xenbus_frontend.bus);
960 bus_register(&xenbus_backend.bus);
962 /*
963 * Domain0 doesn't have a store_evtchn or store_mfn yet.
964 */
965 dom0 = (xen_start_info->store_evtchn == 0);
967 if (dom0) {
968 struct evtchn_alloc_unbound alloc_unbound;
970 /* Allocate page. */
971 page = get_zeroed_page(GFP_KERNEL);
972 if (!page)
973 return -ENOMEM;
975 xen_start_info->store_mfn =
976 pfn_to_mfn(virt_to_phys((void *)page) >>
977 PAGE_SHIFT);
979 /* Next allocate a local port which xenstored can bind to */
980 alloc_unbound.dom = DOMID_SELF;
981 alloc_unbound.remote_dom = 0;
983 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
984 &alloc_unbound);
985 if (err == -ENOSYS)
986 goto err;
987 BUG_ON(err);
988 xen_start_info->store_evtchn = alloc_unbound.port;
990 #ifdef CONFIG_PROC_FS
991 /* And finally publish the above info in /proc/xen */
992 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
993 if (xsd_kva_intf) {
994 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
995 sizeof(xsd_kva_fops));
996 xsd_kva_fops.mmap = xsd_kva_mmap;
997 xsd_kva_intf->proc_fops = &xsd_kva_fops;
998 xsd_kva_intf->read_proc = xsd_kva_read;
999 }
1000 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1001 if (xsd_port_intf)
1002 xsd_port_intf->read_proc = xsd_port_read;
1003 #endif
1004 } else
1005 xenstored_ready = 1;
1007 /* Initialize the interface to xenstore. */
1008 err = xs_init();
1009 if (err) {
1010 printk(KERN_WARNING
1011 "XENBUS: Error initializing xenstore comms: %i\n", err);
1012 goto err;
1015 /* Register ourselves with the kernel device subsystem */
1016 device_register(&xenbus_frontend.dev);
1017 device_register(&xenbus_backend.dev);
1019 if (!dom0)
1020 xenbus_probe(NULL);
1022 return 0;
1024 err:
1025 if (page)
1026 free_page(page);
1028 /*
1029 * Do not unregister the xenbus front/backend buses here. The buses
1030 * must exist because front/backend drivers will use them when they are
1031 * registered.
1032 */
1034 return err;
1037 postcore_initcall(xenbus_probe_init);
1040 static int is_disconnected_device(struct device *dev, void *data)
1042 struct xenbus_device *xendev = to_xenbus_device(dev);
1044 /*
1045 * A device with no driver will never connect. We care only about
1046 * devices which should currently be in the process of connecting.
1047 */
1048 if (!dev->driver)
1049 return 0;
1051 return (xendev->state != XenbusStateConnected);
1054 static int exists_disconnected_device(void)
1056 return bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL,
1057 is_disconnected_device);
1060 static int print_device_status(struct device *dev, void *data)
1062 struct xenbus_device *xendev = to_xenbus_device(dev);
1064 if (!dev->driver) {
1065 /* Information only: is this too noisy? */
1066 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1067 xendev->nodename);
1068 } else if (xendev->state != XenbusStateConnected) {
1069 printk(KERN_WARNING "XENBUS: Timeout connecting "
1070 "to device: %s (state %d)\n",
1071 xendev->nodename, xendev->state);
1074 return 0;
1077 /*
1078 * On a 10 second timeout, wait for all devices currently configured. We need
1079 * to do this to guarantee that the filesystems and / or network devices
1080 * needed for boot are available, before we can allow the boot to proceed.
1082 * This needs to be on a late_initcall, to happen after the frontend device
1083 * drivers have been initialised, but before the root fs is mounted.
1085 * A possible improvement here would be to have the tools add a per-device
1086 * flag to the store entry, indicating whether it is needed at boot time.
1087 * This would allow people who knew what they were doing to accelerate their
1088 * boot slightly, but of course needs tools or manual intervention to set up
1089 * those flags correctly.
1090 */
1091 static int __init wait_for_devices(void)
1093 unsigned long timeout = jiffies + 10*HZ;
1095 if (!is_running_on_xen())
1096 return -ENODEV;
1098 while (time_before(jiffies, timeout) && exists_disconnected_device())
1099 schedule_timeout_interruptible(HZ/10);
1101 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL,
1102 print_device_status);
1104 return 0;
1107 late_initcall(wait_for_devices);