ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 11213:1818ea221bfe

[XENBUS] A few cleanups.
* move function prototypes to the top of the file.
* uses the "struct bus_type" function pointers instead
of overwriting the "struct device_driver" ones for
xenbus_dev_probe() and xenbus_dev_remove().

Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
author kaf24@localhost.localdomain
date Sat Aug 19 10:15:01 2006 +0100 (2006-08-19)
parents 88e8abee7575
children e036feb6a4ff
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",
320 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
321 if (drv->otherend_changed)
322 drv->otherend_changed(dev, state);
323 }
326 static int talk_to_otherend(struct xenbus_device *dev)
327 {
328 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
330 free_otherend_watch(dev);
331 free_otherend_details(dev);
333 return drv->read_otherend_details(dev);
334 }
337 static int watch_otherend(struct xenbus_device *dev)
338 {
339 return xenbus_watch_path2(dev, dev->otherend, "state",
340 &dev->otherend_watch, otherend_changed);
341 }
344 static int xenbus_dev_probe(struct device *_dev)
345 {
346 struct xenbus_device *dev = to_xenbus_device(_dev);
347 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
348 const struct xenbus_device_id *id;
349 int err;
351 DPRINTK("");
353 if (!drv->probe) {
354 err = -ENODEV;
355 goto fail;
356 }
358 id = match_device(drv->ids, dev);
359 if (!id) {
360 err = -ENODEV;
361 goto fail;
362 }
364 err = talk_to_otherend(dev);
365 if (err) {
366 printk(KERN_WARNING
367 "xenbus_probe: talk_to_otherend on %s failed.\n",
368 dev->nodename);
369 return err;
370 }
372 err = drv->probe(dev, id);
373 if (err)
374 goto fail;
376 err = watch_otherend(dev);
377 if (err) {
378 printk(KERN_WARNING
379 "xenbus_probe: watch_otherend on %s failed.\n",
380 dev->nodename);
381 return err;
382 }
384 return 0;
385 fail:
386 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
387 xenbus_switch_state(dev, XenbusStateClosed);
388 return -ENODEV;
389 }
391 static int xenbus_dev_remove(struct device *_dev)
392 {
393 struct xenbus_device *dev = to_xenbus_device(_dev);
394 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
396 DPRINTK("");
398 free_otherend_watch(dev);
399 free_otherend_details(dev);
401 if (drv->remove)
402 drv->remove(dev);
404 xenbus_switch_state(dev, XenbusStateClosed);
405 return 0;
406 }
408 static int xenbus_register_driver_common(struct xenbus_driver *drv,
409 struct xen_bus_type *bus)
410 {
411 int ret;
413 drv->driver.name = drv->name;
414 drv->driver.bus = &bus->bus;
415 drv->driver.owner = drv->owner;
417 mutex_lock(&xenwatch_mutex);
418 ret = driver_register(&drv->driver);
419 mutex_unlock(&xenwatch_mutex);
420 return ret;
421 }
423 int xenbus_register_frontend(struct xenbus_driver *drv)
424 {
425 int ret;
427 drv->read_otherend_details = read_backend_details;
429 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
430 if (ret)
431 return ret;
433 /* If this driver is loaded as a module wait for devices to attach. */
434 wait_for_devices(drv);
436 return 0;
437 }
438 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
440 int xenbus_register_backend(struct xenbus_driver *drv)
441 {
442 drv->read_otherend_details = read_frontend_details;
444 return xenbus_register_driver_common(drv, &xenbus_backend);
445 }
446 EXPORT_SYMBOL_GPL(xenbus_register_backend);
448 void xenbus_unregister_driver(struct xenbus_driver *drv)
449 {
450 driver_unregister(&drv->driver);
451 }
452 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
454 struct xb_find_info
455 {
456 struct xenbus_device *dev;
457 const char *nodename;
458 };
460 static int cmp_dev(struct device *dev, void *data)
461 {
462 struct xenbus_device *xendev = to_xenbus_device(dev);
463 struct xb_find_info *info = data;
465 if (!strcmp(xendev->nodename, info->nodename)) {
466 info->dev = xendev;
467 get_device(dev);
468 return 1;
469 }
470 return 0;
471 }
473 struct xenbus_device *xenbus_device_find(const char *nodename,
474 struct bus_type *bus)
475 {
476 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
478 bus_for_each_dev(bus, NULL, &info, cmp_dev);
479 return info.dev;
480 }
482 static int cleanup_dev(struct device *dev, void *data)
483 {
484 struct xenbus_device *xendev = to_xenbus_device(dev);
485 struct xb_find_info *info = data;
486 int len = strlen(info->nodename);
488 DPRINTK("%s", info->nodename);
490 /* Match the info->nodename path, or any subdirectory of that path. */
491 if (strncmp(xendev->nodename, info->nodename, len))
492 return 0;
494 /* If the node name is longer, ensure it really is a subdirectory. */
495 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
496 return 0;
498 info->dev = xendev;
499 get_device(dev);
500 return 1;
501 }
503 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
504 {
505 struct xb_find_info info = { .nodename = path };
507 do {
508 info.dev = NULL;
509 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
510 if (info.dev) {
511 device_unregister(&info.dev->dev);
512 put_device(&info.dev->dev);
513 }
514 } while (info.dev);
515 }
517 static void xenbus_dev_release(struct device *dev)
518 {
519 if (dev)
520 kfree(to_xenbus_device(dev));
521 }
523 /* Simplified asprintf. */
524 char *kasprintf(const char *fmt, ...)
525 {
526 va_list ap;
527 unsigned int len;
528 char *p, dummy[1];
530 va_start(ap, fmt);
531 /* FIXME: vsnprintf has a bug, NULL should work */
532 len = vsnprintf(dummy, 0, fmt, ap);
533 va_end(ap);
535 p = kmalloc(len + 1, GFP_KERNEL);
536 if (!p)
537 return NULL;
538 va_start(ap, fmt);
539 vsprintf(p, fmt, ap);
540 va_end(ap);
541 return p;
542 }
544 static ssize_t xendev_show_nodename(struct device *dev,
545 struct device_attribute *attr, char *buf)
546 {
547 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
548 }
549 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
551 static ssize_t xendev_show_devtype(struct device *dev,
552 struct device_attribute *attr, char *buf)
553 {
554 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
555 }
556 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
559 static int xenbus_probe_node(struct xen_bus_type *bus,
560 const char *type,
561 const char *nodename)
562 {
563 int err;
564 struct xenbus_device *xendev;
565 size_t stringlen;
566 char *tmpstring;
568 enum xenbus_state state = xenbus_read_driver_state(nodename);
570 if (state != XenbusStateInitialising) {
571 /* Device is not new, so ignore it. This can happen if a
572 device is going away after switching to Closed. */
573 return 0;
574 }
576 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
577 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
578 if (!xendev)
579 return -ENOMEM;
581 /* Copy the strings into the extra space. */
583 tmpstring = (char *)(xendev + 1);
584 strcpy(tmpstring, nodename);
585 xendev->nodename = tmpstring;
587 tmpstring += strlen(tmpstring) + 1;
588 strcpy(tmpstring, type);
589 xendev->devicetype = tmpstring;
591 xendev->dev.parent = &bus->dev;
592 xendev->dev.bus = &bus->bus;
593 xendev->dev.release = xenbus_dev_release;
595 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
596 if (err)
597 goto fail;
599 /* Register with generic device framework. */
600 err = device_register(&xendev->dev);
601 if (err)
602 goto fail;
604 device_create_file(&xendev->dev, &dev_attr_nodename);
605 device_create_file(&xendev->dev, &dev_attr_devtype);
607 return 0;
608 fail:
609 kfree(xendev);
610 return err;
611 }
613 /* device/<typename>/<name> */
614 static int xenbus_probe_frontend(const char *type, const char *name)
615 {
616 char *nodename;
617 int err;
619 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
620 if (!nodename)
621 return -ENOMEM;
623 DPRINTK("%s", nodename);
625 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
626 kfree(nodename);
627 return err;
628 }
630 /* backend/<typename>/<frontend-uuid>/<name> */
631 static int xenbus_probe_backend_unit(const char *dir,
632 const char *type,
633 const char *name)
634 {
635 char *nodename;
636 int err;
638 nodename = kasprintf("%s/%s", dir, name);
639 if (!nodename)
640 return -ENOMEM;
642 DPRINTK("%s\n", nodename);
644 err = xenbus_probe_node(&xenbus_backend, type, nodename);
645 kfree(nodename);
646 return err;
647 }
649 /* backend/<typename>/<frontend-domid> */
650 static int xenbus_probe_backend(const char *type, const char *domid)
651 {
652 char *nodename;
653 int err = 0;
654 char **dir;
655 unsigned int i, dir_n = 0;
657 DPRINTK("");
659 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
660 if (!nodename)
661 return -ENOMEM;
663 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
664 if (IS_ERR(dir)) {
665 kfree(nodename);
666 return PTR_ERR(dir);
667 }
669 for (i = 0; i < dir_n; i++) {
670 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
671 if (err)
672 break;
673 }
674 kfree(dir);
675 kfree(nodename);
676 return err;
677 }
679 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
680 {
681 int err = 0;
682 char **dir;
683 unsigned int dir_n = 0;
684 int i;
686 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
687 if (IS_ERR(dir))
688 return PTR_ERR(dir);
690 for (i = 0; i < dir_n; i++) {
691 err = bus->probe(type, dir[i]);
692 if (err)
693 break;
694 }
695 kfree(dir);
696 return err;
697 }
699 static int xenbus_probe_devices(struct xen_bus_type *bus)
700 {
701 int err = 0;
702 char **dir;
703 unsigned int i, dir_n;
705 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
706 if (IS_ERR(dir))
707 return PTR_ERR(dir);
709 for (i = 0; i < dir_n; i++) {
710 err = xenbus_probe_device_type(bus, dir[i]);
711 if (err)
712 break;
713 }
714 kfree(dir);
715 return err;
716 }
718 static unsigned int char_count(const char *str, char c)
719 {
720 unsigned int i, ret = 0;
722 for (i = 0; str[i]; i++)
723 if (str[i] == c)
724 ret++;
725 return ret;
726 }
728 static int strsep_len(const char *str, char c, unsigned int len)
729 {
730 unsigned int i;
732 for (i = 0; str[i]; i++)
733 if (str[i] == c) {
734 if (len == 0)
735 return i;
736 len--;
737 }
738 return (len == 0) ? i : -ERANGE;
739 }
741 static void dev_changed(const char *node, struct xen_bus_type *bus)
742 {
743 int exists, rootlen;
744 struct xenbus_device *dev;
745 char type[BUS_ID_SIZE];
746 const char *p, *root;
748 if (char_count(node, '/') < 2)
749 return;
751 exists = xenbus_exists(XBT_NIL, node, "");
752 if (!exists) {
753 xenbus_cleanup_devices(node, &bus->bus);
754 return;
755 }
757 /* backend/<type>/... or device/<type>/... */
758 p = strchr(node, '/') + 1;
759 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
760 type[BUS_ID_SIZE-1] = '\0';
762 rootlen = strsep_len(node, '/', bus->levels);
763 if (rootlen < 0)
764 return;
765 root = kasprintf("%.*s", rootlen, node);
766 if (!root)
767 return;
769 dev = xenbus_device_find(root, &bus->bus);
770 if (!dev)
771 xenbus_probe_node(bus, type, root);
772 else
773 put_device(&dev->dev);
775 kfree(root);
776 }
778 static void frontend_changed(struct xenbus_watch *watch,
779 const char **vec, unsigned int len)
780 {
781 DPRINTK("");
783 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
784 }
786 static void backend_changed(struct xenbus_watch *watch,
787 const char **vec, unsigned int len)
788 {
789 DPRINTK("");
791 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
792 }
794 /* We watch for devices appearing and vanishing. */
795 static struct xenbus_watch fe_watch = {
796 .node = "device",
797 .callback = frontend_changed,
798 };
800 static struct xenbus_watch be_watch = {
801 .node = "backend",
802 .callback = backend_changed,
803 };
805 static int suspend_dev(struct device *dev, void *data)
806 {
807 int err = 0;
808 struct xenbus_driver *drv;
809 struct xenbus_device *xdev;
811 DPRINTK("");
813 if (dev->driver == NULL)
814 return 0;
815 drv = to_xenbus_driver(dev->driver);
816 xdev = container_of(dev, struct xenbus_device, dev);
817 if (drv->suspend)
818 err = drv->suspend(xdev);
819 if (err)
820 printk(KERN_WARNING
821 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
822 return 0;
823 }
825 static int resume_dev(struct device *dev, void *data)
826 {
827 int err;
828 struct xenbus_driver *drv;
829 struct xenbus_device *xdev;
831 DPRINTK("");
833 if (dev->driver == NULL)
834 return 0;
836 drv = to_xenbus_driver(dev->driver);
837 xdev = container_of(dev, struct xenbus_device, dev);
839 err = talk_to_otherend(xdev);
840 if (err) {
841 printk(KERN_WARNING
842 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
843 dev->bus_id, err);
844 return err;
845 }
847 xdev->state = XenbusStateInitialising;
849 if (drv->resume) {
850 err = drv->resume(xdev);
851 if (err) {
852 printk(KERN_WARNING
853 "xenbus: resume %s failed: %i\n",
854 dev->bus_id, err);
855 return err;
856 }
857 }
859 err = watch_otherend(xdev);
860 if (err) {
861 printk(KERN_WARNING
862 "xenbus_probe: resume (watch_otherend) %s failed: "
863 "%d.\n", dev->bus_id, err);
864 return err;
865 }
867 return 0;
868 }
870 void xenbus_suspend(void)
871 {
872 DPRINTK("");
874 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
875 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
876 xs_suspend();
877 }
878 EXPORT_SYMBOL_GPL(xenbus_suspend);
880 void xenbus_resume(void)
881 {
882 xb_init_comms();
883 xs_resume();
884 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
885 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
886 }
887 EXPORT_SYMBOL_GPL(xenbus_resume);
890 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
891 int xenstored_ready = 0;
894 int register_xenstore_notifier(struct notifier_block *nb)
895 {
896 int ret = 0;
898 if (xenstored_ready > 0)
899 ret = nb->notifier_call(nb, 0, NULL);
900 else
901 notifier_chain_register(&xenstore_chain, nb);
903 return ret;
904 }
905 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
907 void unregister_xenstore_notifier(struct notifier_block *nb)
908 {
909 notifier_chain_unregister(&xenstore_chain, nb);
910 }
911 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
914 void xenbus_probe(void *unused)
915 {
916 BUG_ON((xenstored_ready <= 0));
918 /* Enumerate devices in xenstore. */
919 xenbus_probe_devices(&xenbus_frontend);
920 xenbus_probe_devices(&xenbus_backend);
922 /* Watch for changes. */
923 register_xenbus_watch(&fe_watch);
924 register_xenbus_watch(&be_watch);
926 /* Notify others that xenstore is up */
927 notifier_call_chain(&xenstore_chain, 0, NULL);
928 }
931 #ifdef CONFIG_PROC_FS
932 static struct file_operations xsd_kva_fops;
933 static struct proc_dir_entry *xsd_kva_intf;
934 static struct proc_dir_entry *xsd_port_intf;
936 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
937 {
938 size_t size = vma->vm_end - vma->vm_start;
940 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
941 return -EINVAL;
943 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
944 size, vma->vm_page_prot))
945 return -EAGAIN;
947 return 0;
948 }
950 static int xsd_kva_read(char *page, char **start, off_t off,
951 int count, int *eof, void *data)
952 {
953 int len;
955 len = sprintf(page, "0x%p", xen_store_interface);
956 *eof = 1;
957 return len;
958 }
960 static int xsd_port_read(char *page, char **start, off_t off,
961 int count, int *eof, void *data)
962 {
963 int len;
965 len = sprintf(page, "%d", xen_store_evtchn);
966 *eof = 1;
967 return len;
968 }
969 #endif
971 static int __init xenbus_probe_init(void)
972 {
973 int err = 0;
974 unsigned long page = 0;
976 DPRINTK("");
978 if (!is_running_on_xen())
979 return -ENODEV;
981 /* Register ourselves with the kernel bus subsystem */
982 bus_register(&xenbus_frontend.bus);
983 bus_register(&xenbus_backend.bus);
985 /*
986 * Domain0 doesn't have a store_evtchn or store_mfn yet.
987 */
988 if (is_initial_xendomain()) {
989 struct evtchn_alloc_unbound alloc_unbound;
991 /* Allocate page. */
992 page = get_zeroed_page(GFP_KERNEL);
993 if (!page)
994 return -ENOMEM;
996 xen_store_mfn = xen_start_info->store_mfn =
997 pfn_to_mfn(virt_to_phys((void *)page) >>
998 PAGE_SHIFT);
1000 /* Next allocate a local port which xenstored can bind to */
1001 alloc_unbound.dom = DOMID_SELF;
1002 alloc_unbound.remote_dom = 0;
1004 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1005 &alloc_unbound);
1006 if (err == -ENOSYS)
1007 goto err;
1008 BUG_ON(err);
1009 xen_store_evtchn = xen_start_info->store_evtchn =
1010 alloc_unbound.port;
1012 #ifdef CONFIG_PROC_FS
1013 /* And finally publish the above info in /proc/xen */
1014 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1015 if (xsd_kva_intf) {
1016 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1017 sizeof(xsd_kva_fops));
1018 xsd_kva_fops.mmap = xsd_kva_mmap;
1019 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1020 xsd_kva_intf->read_proc = xsd_kva_read;
1022 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1023 if (xsd_port_intf)
1024 xsd_port_intf->read_proc = xsd_port_read;
1025 #endif
1026 xen_store_interface = mfn_to_virt(xen_store_mfn);
1027 } else {
1028 xenstored_ready = 1;
1029 #ifdef CONFIG_XEN
1030 xen_store_evtchn = xen_start_info->store_evtchn;
1031 xen_store_mfn = xen_start_info->store_mfn;
1032 xen_store_interface = mfn_to_virt(xen_store_mfn);
1033 #else
1034 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1035 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1036 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1037 PAGE_SIZE);
1038 #endif
1042 xenbus_dev_init();
1044 /* Initialize the interface to xenstore. */
1045 err = xs_init();
1046 if (err) {
1047 printk(KERN_WARNING
1048 "XENBUS: Error initializing xenstore comms: %i\n", err);
1049 goto err;
1052 /* Register ourselves with the kernel device subsystem */
1053 device_register(&xenbus_frontend.dev);
1054 device_register(&xenbus_backend.dev);
1056 if (!is_initial_xendomain())
1057 xenbus_probe(NULL);
1059 return 0;
1061 err:
1062 if (page)
1063 free_page(page);
1065 /*
1066 * Do not unregister the xenbus front/backend buses here. The buses
1067 * must exist because front/backend drivers will use them when they are
1068 * registered.
1069 */
1071 return err;
1074 postcore_initcall(xenbus_probe_init);
1076 MODULE_LICENSE("Dual BSD/GPL");
1079 static int is_disconnected_device(struct device *dev, void *data)
1081 struct xenbus_device *xendev = to_xenbus_device(dev);
1082 struct device_driver *drv = data;
1084 /*
1085 * A device with no driver will never connect. We care only about
1086 * devices which should currently be in the process of connecting.
1087 */
1088 if (!dev->driver)
1089 return 0;
1091 /* Is this search limited to a particular driver? */
1092 if (drv && (dev->driver != drv))
1093 return 0;
1095 return (xendev->state != XenbusStateConnected);
1098 static int exists_disconnected_device(struct device_driver *drv)
1100 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1101 is_disconnected_device);
1104 static int print_device_status(struct device *dev, void *data)
1106 struct xenbus_device *xendev = to_xenbus_device(dev);
1107 struct device_driver *drv = data;
1109 /* Is this operation limited to a particular driver? */
1110 if (drv && (dev->driver != drv))
1111 return 0;
1113 if (!dev->driver) {
1114 /* Information only: is this too noisy? */
1115 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1116 xendev->nodename);
1117 } else if (xendev->state != XenbusStateConnected) {
1118 printk(KERN_WARNING "XENBUS: Timeout connecting "
1119 "to device: %s (state %d)\n",
1120 xendev->nodename, xendev->state);
1123 return 0;
1126 /* We only wait for device setup after most initcalls have run. */
1127 static int ready_to_wait_for_devices;
1129 /*
1130 * On a 10 second timeout, wait for all devices currently configured. We need
1131 * to do this to guarantee that the filesystems and / or network devices
1132 * needed for boot are available, before we can allow the boot to proceed.
1134 * This needs to be on a late_initcall, to happen after the frontend device
1135 * drivers have been initialised, but before the root fs is mounted.
1137 * A possible improvement here would be to have the tools add a per-device
1138 * flag to the store entry, indicating whether it is needed at boot time.
1139 * This would allow people who knew what they were doing to accelerate their
1140 * boot slightly, but of course needs tools or manual intervention to set up
1141 * those flags correctly.
1142 */
1143 static void wait_for_devices(struct xenbus_driver *xendrv)
1145 unsigned long timeout = jiffies + 10*HZ;
1146 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1148 if (!ready_to_wait_for_devices || !is_running_on_xen())
1149 return;
1151 while (exists_disconnected_device(drv)) {
1152 if (time_after(jiffies, timeout))
1153 break;
1154 schedule_timeout_interruptible(HZ/10);
1157 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1158 print_device_status);
1161 #ifndef MODULE
1162 static int __init boot_wait_for_devices(void)
1164 ready_to_wait_for_devices = 1;
1165 wait_for_devices(NULL);
1166 return 0;
1169 late_initcall(boot_wait_for_devices);
1170 #endif