direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 11402:e036feb6a4ff

Add xenbus_strstate() helper function for more readable debug output.

From: Gerd Hoffmann <kraxel@suse.de>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Sep 01 00:20:42 2006 +0100 (2006-09-01)
parents 1818ea221bfe
children 90d3ca978b40
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/maddr.h>
49 #include <asm/pgtable.h>
50 #include <asm/hypervisor.h>
51 #include <xen/xenbus.h>
52 #include <xen/xen_proc.h>
53 #include <xen/evtchn.h>
54 #include <xen/features.h>
55 #include <xen/hvm.h>
57 #include "xenbus_comms.h"
59 int xen_store_evtchn;
60 struct xenstore_domain_interface *xen_store_interface;
61 static unsigned long xen_store_mfn;
63 extern struct mutex xenwatch_mutex;
65 static struct notifier_block *xenstore_chain;
67 static void wait_for_devices(struct xenbus_driver *xendrv);
69 static int xenbus_probe_frontend(const char *type, const char *name);
70 static int xenbus_uevent_backend(struct device *dev, char **envp,
71 int num_envp, char *buffer, int buffer_size);
72 static int xenbus_probe_backend(const char *type, const char *domid);
74 static int xenbus_dev_probe(struct device *_dev);
75 static int xenbus_dev_remove(struct device *_dev);
77 /* If something in array of ids matches this device, return it. */
78 static const struct xenbus_device_id *
79 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
80 {
81 for (; *arr->devicetype != '\0'; arr++) {
82 if (!strcmp(arr->devicetype, dev->devicetype))
83 return arr;
84 }
85 return NULL;
86 }
88 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
89 {
90 struct xenbus_driver *drv = to_xenbus_driver(_drv);
92 if (!drv->ids)
93 return 0;
95 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
96 }
98 struct xen_bus_type
99 {
100 char *root;
101 unsigned int levels;
102 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
103 int (*probe)(const char *type, const char *dir);
104 struct bus_type bus;
105 struct device dev;
106 };
109 /* device/<type>/<id> => <type>-<id> */
110 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
111 {
112 nodename = strchr(nodename, '/');
113 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
114 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
115 return -EINVAL;
116 }
118 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
119 if (!strchr(bus_id, '/')) {
120 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
121 return -EINVAL;
122 }
123 *strchr(bus_id, '/') = '-';
124 return 0;
125 }
128 static void free_otherend_details(struct xenbus_device *dev)
129 {
130 kfree(dev->otherend);
131 dev->otherend = NULL;
132 }
135 static void free_otherend_watch(struct xenbus_device *dev)
136 {
137 if (dev->otherend_watch.node) {
138 unregister_xenbus_watch(&dev->otherend_watch);
139 kfree(dev->otherend_watch.node);
140 dev->otherend_watch.node = NULL;
141 }
142 }
145 static int read_otherend_details(struct xenbus_device *xendev,
146 char *id_node, char *path_node)
147 {
148 int err = xenbus_gather(XBT_NIL, xendev->nodename,
149 id_node, "%i", &xendev->otherend_id,
150 path_node, NULL, &xendev->otherend,
151 NULL);
152 if (err) {
153 xenbus_dev_fatal(xendev, err,
154 "reading other end details from %s",
155 xendev->nodename);
156 return err;
157 }
158 if (strlen(xendev->otherend) == 0 ||
159 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
160 xenbus_dev_fatal(xendev, -ENOENT,
161 "unable to read other end from %s. "
162 "missing or inaccessible.",
163 xendev->nodename);
164 free_otherend_details(xendev);
165 return -ENOENT;
166 }
168 return 0;
169 }
172 static int read_backend_details(struct xenbus_device *xendev)
173 {
174 return read_otherend_details(xendev, "backend-id", "backend");
175 }
178 static int read_frontend_details(struct xenbus_device *xendev)
179 {
180 return read_otherend_details(xendev, "frontend-id", "frontend");
181 }
184 /* Bus type for frontend drivers. */
185 static struct xen_bus_type xenbus_frontend = {
186 .root = "device",
187 .levels = 2, /* device/type/<id> */
188 .get_bus_id = frontend_bus_id,
189 .probe = xenbus_probe_frontend,
190 .bus = {
191 .name = "xen",
192 .match = xenbus_match,
193 .probe = xenbus_dev_probe,
194 .remove = xenbus_dev_remove,
195 },
196 .dev = {
197 .bus_id = "xen",
198 },
199 };
201 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
202 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
203 {
204 int domid, err;
205 const char *devid, *type, *frontend;
206 unsigned int typelen;
208 type = strchr(nodename, '/');
209 if (!type)
210 return -EINVAL;
211 type++;
212 typelen = strcspn(type, "/");
213 if (!typelen || type[typelen] != '/')
214 return -EINVAL;
216 devid = strrchr(nodename, '/') + 1;
218 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
219 "frontend", NULL, &frontend,
220 NULL);
221 if (err)
222 return err;
223 if (strlen(frontend) == 0)
224 err = -ERANGE;
225 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
226 err = -ENOENT;
228 kfree(frontend);
230 if (err)
231 return err;
233 if (snprintf(bus_id, BUS_ID_SIZE,
234 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
235 return -ENOSPC;
236 return 0;
237 }
239 static struct xen_bus_type xenbus_backend = {
240 .root = "backend",
241 .levels = 3, /* backend/type/<frontend>/<id> */
242 .get_bus_id = backend_bus_id,
243 .probe = xenbus_probe_backend,
244 .bus = {
245 .name = "xen-backend",
246 .match = xenbus_match,
247 .probe = xenbus_dev_probe,
248 .remove = xenbus_dev_remove,
249 .uevent = xenbus_uevent_backend,
250 },
251 .dev = {
252 .bus_id = "xen-backend",
253 },
254 };
256 static int xenbus_uevent_backend(struct device *dev, char **envp,
257 int num_envp, char *buffer, int buffer_size)
258 {
259 struct xenbus_device *xdev;
260 struct xenbus_driver *drv;
261 int i = 0;
262 int length = 0;
264 DPRINTK("");
266 if (dev == NULL)
267 return -ENODEV;
269 xdev = to_xenbus_device(dev);
270 if (xdev == NULL)
271 return -ENODEV;
273 /* stuff we want to pass to /sbin/hotplug */
274 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
275 "XENBUS_TYPE=%s", xdev->devicetype);
277 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
278 "XENBUS_PATH=%s", xdev->nodename);
280 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
281 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
283 /* terminate, set to next free slot, shrink available space */
284 envp[i] = NULL;
285 envp = &envp[i];
286 num_envp -= i;
287 buffer = &buffer[length];
288 buffer_size -= length;
290 if (dev->driver) {
291 drv = to_xenbus_driver(dev->driver);
292 if (drv && drv->uevent)
293 return drv->uevent(xdev, envp, num_envp, buffer,
294 buffer_size);
295 }
297 return 0;
298 }
300 static void otherend_changed(struct xenbus_watch *watch,
301 const char **vec, unsigned int len)
302 {
303 struct xenbus_device *dev =
304 container_of(watch, struct xenbus_device, otherend_watch);
305 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
306 enum xenbus_state state;
308 /* Protect us against watches firing on old details when the otherend
309 details change, say immediately after a resume. */
310 if (!dev->otherend ||
311 strncmp(dev->otherend, vec[XS_WATCH_PATH],
312 strlen(dev->otherend))) {
313 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
314 return;
315 }
317 state = xenbus_read_driver_state(dev->otherend);
319 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
320 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
322 if (drv->otherend_changed)
323 drv->otherend_changed(dev, state);
324 }
327 static int talk_to_otherend(struct xenbus_device *dev)
328 {
329 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
331 free_otherend_watch(dev);
332 free_otherend_details(dev);
334 return drv->read_otherend_details(dev);
335 }
338 static int watch_otherend(struct xenbus_device *dev)
339 {
340 return xenbus_watch_path2(dev, dev->otherend, "state",
341 &dev->otherend_watch, otherend_changed);
342 }
345 static int xenbus_dev_probe(struct device *_dev)
346 {
347 struct xenbus_device *dev = to_xenbus_device(_dev);
348 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
349 const struct xenbus_device_id *id;
350 int err;
352 DPRINTK("");
354 if (!drv->probe) {
355 err = -ENODEV;
356 goto fail;
357 }
359 id = match_device(drv->ids, dev);
360 if (!id) {
361 err = -ENODEV;
362 goto fail;
363 }
365 err = talk_to_otherend(dev);
366 if (err) {
367 printk(KERN_WARNING
368 "xenbus_probe: talk_to_otherend on %s failed.\n",
369 dev->nodename);
370 return err;
371 }
373 err = drv->probe(dev, id);
374 if (err)
375 goto fail;
377 err = watch_otherend(dev);
378 if (err) {
379 printk(KERN_WARNING
380 "xenbus_probe: watch_otherend on %s failed.\n",
381 dev->nodename);
382 return err;
383 }
385 return 0;
386 fail:
387 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
388 xenbus_switch_state(dev, XenbusStateClosed);
389 return -ENODEV;
390 }
392 static int xenbus_dev_remove(struct device *_dev)
393 {
394 struct xenbus_device *dev = to_xenbus_device(_dev);
395 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
397 DPRINTK("");
399 free_otherend_watch(dev);
400 free_otherend_details(dev);
402 if (drv->remove)
403 drv->remove(dev);
405 xenbus_switch_state(dev, XenbusStateClosed);
406 return 0;
407 }
409 static int xenbus_register_driver_common(struct xenbus_driver *drv,
410 struct xen_bus_type *bus)
411 {
412 int ret;
414 drv->driver.name = drv->name;
415 drv->driver.bus = &bus->bus;
416 drv->driver.owner = drv->owner;
418 mutex_lock(&xenwatch_mutex);
419 ret = driver_register(&drv->driver);
420 mutex_unlock(&xenwatch_mutex);
421 return ret;
422 }
424 int xenbus_register_frontend(struct xenbus_driver *drv)
425 {
426 int ret;
428 drv->read_otherend_details = read_backend_details;
430 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
431 if (ret)
432 return ret;
434 /* If this driver is loaded as a module wait for devices to attach. */
435 wait_for_devices(drv);
437 return 0;
438 }
439 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
441 int xenbus_register_backend(struct xenbus_driver *drv)
442 {
443 drv->read_otherend_details = read_frontend_details;
445 return xenbus_register_driver_common(drv, &xenbus_backend);
446 }
447 EXPORT_SYMBOL_GPL(xenbus_register_backend);
449 void xenbus_unregister_driver(struct xenbus_driver *drv)
450 {
451 driver_unregister(&drv->driver);
452 }
453 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
455 struct xb_find_info
456 {
457 struct xenbus_device *dev;
458 const char *nodename;
459 };
461 static int cmp_dev(struct device *dev, void *data)
462 {
463 struct xenbus_device *xendev = to_xenbus_device(dev);
464 struct xb_find_info *info = data;
466 if (!strcmp(xendev->nodename, info->nodename)) {
467 info->dev = xendev;
468 get_device(dev);
469 return 1;
470 }
471 return 0;
472 }
474 struct xenbus_device *xenbus_device_find(const char *nodename,
475 struct bus_type *bus)
476 {
477 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
479 bus_for_each_dev(bus, NULL, &info, cmp_dev);
480 return info.dev;
481 }
483 static int cleanup_dev(struct device *dev, void *data)
484 {
485 struct xenbus_device *xendev = to_xenbus_device(dev);
486 struct xb_find_info *info = data;
487 int len = strlen(info->nodename);
489 DPRINTK("%s", info->nodename);
491 /* Match the info->nodename path, or any subdirectory of that path. */
492 if (strncmp(xendev->nodename, info->nodename, len))
493 return 0;
495 /* If the node name is longer, ensure it really is a subdirectory. */
496 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
497 return 0;
499 info->dev = xendev;
500 get_device(dev);
501 return 1;
502 }
504 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
505 {
506 struct xb_find_info info = { .nodename = path };
508 do {
509 info.dev = NULL;
510 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
511 if (info.dev) {
512 device_unregister(&info.dev->dev);
513 put_device(&info.dev->dev);
514 }
515 } while (info.dev);
516 }
518 static void xenbus_dev_release(struct device *dev)
519 {
520 if (dev)
521 kfree(to_xenbus_device(dev));
522 }
524 /* Simplified asprintf. */
525 char *kasprintf(const char *fmt, ...)
526 {
527 va_list ap;
528 unsigned int len;
529 char *p, dummy[1];
531 va_start(ap, fmt);
532 /* FIXME: vsnprintf has a bug, NULL should work */
533 len = vsnprintf(dummy, 0, fmt, ap);
534 va_end(ap);
536 p = kmalloc(len + 1, GFP_KERNEL);
537 if (!p)
538 return NULL;
539 va_start(ap, fmt);
540 vsprintf(p, fmt, ap);
541 va_end(ap);
542 return p;
543 }
545 static ssize_t xendev_show_nodename(struct device *dev,
546 struct device_attribute *attr, char *buf)
547 {
548 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
549 }
550 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
552 static ssize_t xendev_show_devtype(struct device *dev,
553 struct device_attribute *attr, char *buf)
554 {
555 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
556 }
557 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
560 static int xenbus_probe_node(struct xen_bus_type *bus,
561 const char *type,
562 const char *nodename)
563 {
564 int err;
565 struct xenbus_device *xendev;
566 size_t stringlen;
567 char *tmpstring;
569 enum xenbus_state state = xenbus_read_driver_state(nodename);
571 if (state != XenbusStateInitialising) {
572 /* Device is not new, so ignore it. This can happen if a
573 device is going away after switching to Closed. */
574 return 0;
575 }
577 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
578 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
579 if (!xendev)
580 return -ENOMEM;
582 /* Copy the strings into the extra space. */
584 tmpstring = (char *)(xendev + 1);
585 strcpy(tmpstring, nodename);
586 xendev->nodename = tmpstring;
588 tmpstring += strlen(tmpstring) + 1;
589 strcpy(tmpstring, type);
590 xendev->devicetype = tmpstring;
592 xendev->dev.parent = &bus->dev;
593 xendev->dev.bus = &bus->bus;
594 xendev->dev.release = xenbus_dev_release;
596 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
597 if (err)
598 goto fail;
600 /* Register with generic device framework. */
601 err = device_register(&xendev->dev);
602 if (err)
603 goto fail;
605 device_create_file(&xendev->dev, &dev_attr_nodename);
606 device_create_file(&xendev->dev, &dev_attr_devtype);
608 return 0;
609 fail:
610 kfree(xendev);
611 return err;
612 }
614 /* device/<typename>/<name> */
615 static int xenbus_probe_frontend(const char *type, const char *name)
616 {
617 char *nodename;
618 int err;
620 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
621 if (!nodename)
622 return -ENOMEM;
624 DPRINTK("%s", nodename);
626 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
627 kfree(nodename);
628 return err;
629 }
631 /* backend/<typename>/<frontend-uuid>/<name> */
632 static int xenbus_probe_backend_unit(const char *dir,
633 const char *type,
634 const char *name)
635 {
636 char *nodename;
637 int err;
639 nodename = kasprintf("%s/%s", dir, name);
640 if (!nodename)
641 return -ENOMEM;
643 DPRINTK("%s\n", nodename);
645 err = xenbus_probe_node(&xenbus_backend, type, nodename);
646 kfree(nodename);
647 return err;
648 }
650 /* backend/<typename>/<frontend-domid> */
651 static int xenbus_probe_backend(const char *type, const char *domid)
652 {
653 char *nodename;
654 int err = 0;
655 char **dir;
656 unsigned int i, dir_n = 0;
658 DPRINTK("");
660 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
661 if (!nodename)
662 return -ENOMEM;
664 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
665 if (IS_ERR(dir)) {
666 kfree(nodename);
667 return PTR_ERR(dir);
668 }
670 for (i = 0; i < dir_n; i++) {
671 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
672 if (err)
673 break;
674 }
675 kfree(dir);
676 kfree(nodename);
677 return err;
678 }
680 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
681 {
682 int err = 0;
683 char **dir;
684 unsigned int dir_n = 0;
685 int i;
687 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
688 if (IS_ERR(dir))
689 return PTR_ERR(dir);
691 for (i = 0; i < dir_n; i++) {
692 err = bus->probe(type, dir[i]);
693 if (err)
694 break;
695 }
696 kfree(dir);
697 return err;
698 }
700 static int xenbus_probe_devices(struct xen_bus_type *bus)
701 {
702 int err = 0;
703 char **dir;
704 unsigned int i, dir_n;
706 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
707 if (IS_ERR(dir))
708 return PTR_ERR(dir);
710 for (i = 0; i < dir_n; i++) {
711 err = xenbus_probe_device_type(bus, dir[i]);
712 if (err)
713 break;
714 }
715 kfree(dir);
716 return err;
717 }
719 static unsigned int char_count(const char *str, char c)
720 {
721 unsigned int i, ret = 0;
723 for (i = 0; str[i]; i++)
724 if (str[i] == c)
725 ret++;
726 return ret;
727 }
729 static int strsep_len(const char *str, char c, unsigned int len)
730 {
731 unsigned int i;
733 for (i = 0; str[i]; i++)
734 if (str[i] == c) {
735 if (len == 0)
736 return i;
737 len--;
738 }
739 return (len == 0) ? i : -ERANGE;
740 }
742 static void dev_changed(const char *node, struct xen_bus_type *bus)
743 {
744 int exists, rootlen;
745 struct xenbus_device *dev;
746 char type[BUS_ID_SIZE];
747 const char *p, *root;
749 if (char_count(node, '/') < 2)
750 return;
752 exists = xenbus_exists(XBT_NIL, node, "");
753 if (!exists) {
754 xenbus_cleanup_devices(node, &bus->bus);
755 return;
756 }
758 /* backend/<type>/... or device/<type>/... */
759 p = strchr(node, '/') + 1;
760 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
761 type[BUS_ID_SIZE-1] = '\0';
763 rootlen = strsep_len(node, '/', bus->levels);
764 if (rootlen < 0)
765 return;
766 root = kasprintf("%.*s", rootlen, node);
767 if (!root)
768 return;
770 dev = xenbus_device_find(root, &bus->bus);
771 if (!dev)
772 xenbus_probe_node(bus, type, root);
773 else
774 put_device(&dev->dev);
776 kfree(root);
777 }
779 static void frontend_changed(struct xenbus_watch *watch,
780 const char **vec, unsigned int len)
781 {
782 DPRINTK("");
784 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
785 }
787 static void backend_changed(struct xenbus_watch *watch,
788 const char **vec, unsigned int len)
789 {
790 DPRINTK("");
792 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
793 }
795 /* We watch for devices appearing and vanishing. */
796 static struct xenbus_watch fe_watch = {
797 .node = "device",
798 .callback = frontend_changed,
799 };
801 static struct xenbus_watch be_watch = {
802 .node = "backend",
803 .callback = backend_changed,
804 };
806 static int suspend_dev(struct device *dev, void *data)
807 {
808 int err = 0;
809 struct xenbus_driver *drv;
810 struct xenbus_device *xdev;
812 DPRINTK("");
814 if (dev->driver == NULL)
815 return 0;
816 drv = to_xenbus_driver(dev->driver);
817 xdev = container_of(dev, struct xenbus_device, dev);
818 if (drv->suspend)
819 err = drv->suspend(xdev);
820 if (err)
821 printk(KERN_WARNING
822 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
823 return 0;
824 }
826 static int resume_dev(struct device *dev, void *data)
827 {
828 int err;
829 struct xenbus_driver *drv;
830 struct xenbus_device *xdev;
832 DPRINTK("");
834 if (dev->driver == NULL)
835 return 0;
837 drv = to_xenbus_driver(dev->driver);
838 xdev = container_of(dev, struct xenbus_device, dev);
840 err = talk_to_otherend(xdev);
841 if (err) {
842 printk(KERN_WARNING
843 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
844 dev->bus_id, err);
845 return err;
846 }
848 xdev->state = XenbusStateInitialising;
850 if (drv->resume) {
851 err = drv->resume(xdev);
852 if (err) {
853 printk(KERN_WARNING
854 "xenbus: resume %s failed: %i\n",
855 dev->bus_id, err);
856 return err;
857 }
858 }
860 err = watch_otherend(xdev);
861 if (err) {
862 printk(KERN_WARNING
863 "xenbus_probe: resume (watch_otherend) %s failed: "
864 "%d.\n", dev->bus_id, err);
865 return err;
866 }
868 return 0;
869 }
871 void xenbus_suspend(void)
872 {
873 DPRINTK("");
875 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
876 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
877 xs_suspend();
878 }
879 EXPORT_SYMBOL_GPL(xenbus_suspend);
881 void xenbus_resume(void)
882 {
883 xb_init_comms();
884 xs_resume();
885 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
886 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
887 }
888 EXPORT_SYMBOL_GPL(xenbus_resume);
891 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
892 int xenstored_ready = 0;
895 int register_xenstore_notifier(struct notifier_block *nb)
896 {
897 int ret = 0;
899 if (xenstored_ready > 0)
900 ret = nb->notifier_call(nb, 0, NULL);
901 else
902 notifier_chain_register(&xenstore_chain, nb);
904 return ret;
905 }
906 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
908 void unregister_xenstore_notifier(struct notifier_block *nb)
909 {
910 notifier_chain_unregister(&xenstore_chain, nb);
911 }
912 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
915 void xenbus_probe(void *unused)
916 {
917 BUG_ON((xenstored_ready <= 0));
919 /* Enumerate devices in xenstore. */
920 xenbus_probe_devices(&xenbus_frontend);
921 xenbus_probe_devices(&xenbus_backend);
923 /* Watch for changes. */
924 register_xenbus_watch(&fe_watch);
925 register_xenbus_watch(&be_watch);
927 /* Notify others that xenstore is up */
928 notifier_call_chain(&xenstore_chain, 0, NULL);
929 }
932 #ifdef CONFIG_PROC_FS
933 static struct file_operations xsd_kva_fops;
934 static struct proc_dir_entry *xsd_kva_intf;
935 static struct proc_dir_entry *xsd_port_intf;
937 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
938 {
939 size_t size = vma->vm_end - vma->vm_start;
941 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
942 return -EINVAL;
944 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
945 size, vma->vm_page_prot))
946 return -EAGAIN;
948 return 0;
949 }
951 static int xsd_kva_read(char *page, char **start, off_t off,
952 int count, int *eof, void *data)
953 {
954 int len;
956 len = sprintf(page, "0x%p", xen_store_interface);
957 *eof = 1;
958 return len;
959 }
961 static int xsd_port_read(char *page, char **start, off_t off,
962 int count, int *eof, void *data)
963 {
964 int len;
966 len = sprintf(page, "%d", xen_store_evtchn);
967 *eof = 1;
968 return len;
969 }
970 #endif
972 static int __init xenbus_probe_init(void)
973 {
974 int err = 0;
975 unsigned long page = 0;
977 DPRINTK("");
979 if (!is_running_on_xen())
980 return -ENODEV;
982 /* Register ourselves with the kernel bus subsystem */
983 bus_register(&xenbus_frontend.bus);
984 bus_register(&xenbus_backend.bus);
986 /*
987 * Domain0 doesn't have a store_evtchn or store_mfn yet.
988 */
989 if (is_initial_xendomain()) {
990 struct evtchn_alloc_unbound alloc_unbound;
992 /* Allocate page. */
993 page = get_zeroed_page(GFP_KERNEL);
994 if (!page)
995 return -ENOMEM;
997 xen_store_mfn = xen_start_info->store_mfn =
998 pfn_to_mfn(virt_to_phys((void *)page) >>
999 PAGE_SHIFT);
1001 /* Next allocate a local port which xenstored can bind to */
1002 alloc_unbound.dom = DOMID_SELF;
1003 alloc_unbound.remote_dom = 0;
1005 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1006 &alloc_unbound);
1007 if (err == -ENOSYS)
1008 goto err;
1009 BUG_ON(err);
1010 xen_store_evtchn = xen_start_info->store_evtchn =
1011 alloc_unbound.port;
1013 #ifdef CONFIG_PROC_FS
1014 /* And finally publish the above info in /proc/xen */
1015 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1016 if (xsd_kva_intf) {
1017 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1018 sizeof(xsd_kva_fops));
1019 xsd_kva_fops.mmap = xsd_kva_mmap;
1020 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1021 xsd_kva_intf->read_proc = xsd_kva_read;
1023 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1024 if (xsd_port_intf)
1025 xsd_port_intf->read_proc = xsd_port_read;
1026 #endif
1027 xen_store_interface = mfn_to_virt(xen_store_mfn);
1028 } else {
1029 xenstored_ready = 1;
1030 #ifdef CONFIG_XEN
1031 xen_store_evtchn = xen_start_info->store_evtchn;
1032 xen_store_mfn = xen_start_info->store_mfn;
1033 xen_store_interface = mfn_to_virt(xen_store_mfn);
1034 #else
1035 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1036 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1037 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1038 PAGE_SIZE);
1039 #endif
1043 xenbus_dev_init();
1045 /* Initialize the interface to xenstore. */
1046 err = xs_init();
1047 if (err) {
1048 printk(KERN_WARNING
1049 "XENBUS: Error initializing xenstore comms: %i\n", err);
1050 goto err;
1053 /* Register ourselves with the kernel device subsystem */
1054 device_register(&xenbus_frontend.dev);
1055 device_register(&xenbus_backend.dev);
1057 if (!is_initial_xendomain())
1058 xenbus_probe(NULL);
1060 return 0;
1062 err:
1063 if (page)
1064 free_page(page);
1066 /*
1067 * Do not unregister the xenbus front/backend buses here. The buses
1068 * must exist because front/backend drivers will use them when they are
1069 * registered.
1070 */
1072 return err;
1075 postcore_initcall(xenbus_probe_init);
1077 MODULE_LICENSE("Dual BSD/GPL");
1080 static int is_disconnected_device(struct device *dev, void *data)
1082 struct xenbus_device *xendev = to_xenbus_device(dev);
1083 struct device_driver *drv = data;
1085 /*
1086 * A device with no driver will never connect. We care only about
1087 * devices which should currently be in the process of connecting.
1088 */
1089 if (!dev->driver)
1090 return 0;
1092 /* Is this search limited to a particular driver? */
1093 if (drv && (dev->driver != drv))
1094 return 0;
1096 return (xendev->state != XenbusStateConnected);
1099 static int exists_disconnected_device(struct device_driver *drv)
1101 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1102 is_disconnected_device);
1105 static int print_device_status(struct device *dev, void *data)
1107 struct xenbus_device *xendev = to_xenbus_device(dev);
1108 struct device_driver *drv = data;
1110 /* Is this operation limited to a particular driver? */
1111 if (drv && (dev->driver != drv))
1112 return 0;
1114 if (!dev->driver) {
1115 /* Information only: is this too noisy? */
1116 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1117 xendev->nodename);
1118 } else if (xendev->state != XenbusStateConnected) {
1119 printk(KERN_WARNING "XENBUS: Timeout connecting "
1120 "to device: %s (state %d)\n",
1121 xendev->nodename, xendev->state);
1124 return 0;
1127 /* We only wait for device setup after most initcalls have run. */
1128 static int ready_to_wait_for_devices;
1130 /*
1131 * On a 10 second timeout, wait for all devices currently configured. We need
1132 * to do this to guarantee that the filesystems and / or network devices
1133 * needed for boot are available, before we can allow the boot to proceed.
1135 * This needs to be on a late_initcall, to happen after the frontend device
1136 * drivers have been initialised, but before the root fs is mounted.
1138 * A possible improvement here would be to have the tools add a per-device
1139 * flag to the store entry, indicating whether it is needed at boot time.
1140 * This would allow people who knew what they were doing to accelerate their
1141 * boot slightly, but of course needs tools or manual intervention to set up
1142 * those flags correctly.
1143 */
1144 static void wait_for_devices(struct xenbus_driver *xendrv)
1146 unsigned long timeout = jiffies + 10*HZ;
1147 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1149 if (!ready_to_wait_for_devices || !is_running_on_xen())
1150 return;
1152 while (exists_disconnected_device(drv)) {
1153 if (time_after(jiffies, timeout))
1154 break;
1155 schedule_timeout_interruptible(HZ/10);
1158 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1159 print_device_status);
1162 #ifndef MODULE
1163 static int __init boot_wait_for_devices(void)
1165 ready_to_wait_for_devices = 1;
1166 wait_for_devices(NULL);
1167 return 0;
1170 late_initcall(boot_wait_for_devices);
1171 #endif