ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 11190:487fe5006064

[LINUX] Allow xenbus to be built as a module.
Signed-off-by: Steven Smith <ssmith@xensource.com>
author kfraser@localhost.localdomain
date Thu Aug 17 16:28:47 2006 +0100 (2006-08-17)
parents 45f3c127069d
children 88e8abee7575
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 int xen_store_evtchn;
58 struct xenstore_domain_interface *xen_store_interface;
59 static unsigned long xen_store_mfn;
61 extern struct mutex xenwatch_mutex;
63 static struct notifier_block *xenstore_chain;
65 static void wait_for_devices(struct xenbus_driver *xendrv);
67 /* If something in array of ids matches this device, return it. */
68 static const struct xenbus_device_id *
69 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
70 {
71 for (; *arr->devicetype != '\0'; arr++) {
72 if (!strcmp(arr->devicetype, dev->devicetype))
73 return arr;
74 }
75 return NULL;
76 }
78 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
79 {
80 struct xenbus_driver *drv = to_xenbus_driver(_drv);
82 if (!drv->ids)
83 return 0;
85 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
86 }
88 struct xen_bus_type
89 {
90 char *root;
91 unsigned int levels;
92 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
93 int (*probe)(const char *type, const char *dir);
94 struct bus_type bus;
95 struct device dev;
96 };
99 /* device/<type>/<id> => <type>-<id> */
100 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
101 {
102 nodename = strchr(nodename, '/');
103 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
104 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
105 return -EINVAL;
106 }
108 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
109 if (!strchr(bus_id, '/')) {
110 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
111 return -EINVAL;
112 }
113 *strchr(bus_id, '/') = '-';
114 return 0;
115 }
118 static void free_otherend_details(struct xenbus_device *dev)
119 {
120 kfree(dev->otherend);
121 dev->otherend = NULL;
122 }
125 static void free_otherend_watch(struct xenbus_device *dev)
126 {
127 if (dev->otherend_watch.node) {
128 unregister_xenbus_watch(&dev->otherend_watch);
129 kfree(dev->otherend_watch.node);
130 dev->otherend_watch.node = NULL;
131 }
132 }
135 static int read_otherend_details(struct xenbus_device *xendev,
136 char *id_node, char *path_node)
137 {
138 int err = xenbus_gather(XBT_NIL, xendev->nodename,
139 id_node, "%i", &xendev->otherend_id,
140 path_node, NULL, &xendev->otherend,
141 NULL);
142 if (err) {
143 xenbus_dev_fatal(xendev, err,
144 "reading other end details from %s",
145 xendev->nodename);
146 return err;
147 }
148 if (strlen(xendev->otherend) == 0 ||
149 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
150 xenbus_dev_fatal(xendev, -ENOENT,
151 "unable to read other end from %s. "
152 "missing or inaccessible.",
153 xendev->nodename);
154 free_otherend_details(xendev);
155 return -ENOENT;
156 }
158 return 0;
159 }
162 static int read_backend_details(struct xenbus_device *xendev)
163 {
164 return read_otherend_details(xendev, "backend-id", "backend");
165 }
168 static int read_frontend_details(struct xenbus_device *xendev)
169 {
170 return read_otherend_details(xendev, "frontend-id", "frontend");
171 }
174 /* Bus type for frontend drivers. */
175 static int xenbus_probe_frontend(const char *type, const char *name);
176 static struct xen_bus_type xenbus_frontend = {
177 .root = "device",
178 .levels = 2, /* device/type/<id> */
179 .get_bus_id = frontend_bus_id,
180 .probe = xenbus_probe_frontend,
181 .bus = {
182 .name = "xen",
183 .match = xenbus_match,
184 },
185 .dev = {
186 .bus_id = "xen",
187 },
188 };
190 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
191 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
192 {
193 int domid, err;
194 const char *devid, *type, *frontend;
195 unsigned int typelen;
197 type = strchr(nodename, '/');
198 if (!type)
199 return -EINVAL;
200 type++;
201 typelen = strcspn(type, "/");
202 if (!typelen || type[typelen] != '/')
203 return -EINVAL;
205 devid = strrchr(nodename, '/') + 1;
207 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
208 "frontend", NULL, &frontend,
209 NULL);
210 if (err)
211 return err;
212 if (strlen(frontend) == 0)
213 err = -ERANGE;
214 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
215 err = -ENOENT;
217 kfree(frontend);
219 if (err)
220 return err;
222 if (snprintf(bus_id, BUS_ID_SIZE,
223 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
224 return -ENOSPC;
225 return 0;
226 }
228 static int xenbus_uevent_backend(struct device *dev, char **envp,
229 int num_envp, char *buffer, int buffer_size);
230 static int xenbus_probe_backend(const char *type, const char *domid);
231 static struct xen_bus_type xenbus_backend = {
232 .root = "backend",
233 .levels = 3, /* backend/type/<frontend>/<id> */
234 .get_bus_id = backend_bus_id,
235 .probe = xenbus_probe_backend,
236 .bus = {
237 .name = "xen-backend",
238 .match = xenbus_match,
239 .uevent = xenbus_uevent_backend,
240 },
241 .dev = {
242 .bus_id = "xen-backend",
243 },
244 };
246 static int xenbus_uevent_backend(struct device *dev, char **envp,
247 int num_envp, char *buffer, int buffer_size)
248 {
249 struct xenbus_device *xdev;
250 struct xenbus_driver *drv;
251 int i = 0;
252 int length = 0;
254 DPRINTK("");
256 if (dev == NULL)
257 return -ENODEV;
259 xdev = to_xenbus_device(dev);
260 if (xdev == NULL)
261 return -ENODEV;
263 /* stuff we want to pass to /sbin/hotplug */
264 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
265 "XENBUS_TYPE=%s", xdev->devicetype);
267 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
268 "XENBUS_PATH=%s", xdev->nodename);
270 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
271 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
273 /* terminate, set to next free slot, shrink available space */
274 envp[i] = NULL;
275 envp = &envp[i];
276 num_envp -= i;
277 buffer = &buffer[length];
278 buffer_size -= length;
280 if (dev->driver) {
281 drv = to_xenbus_driver(dev->driver);
282 if (drv && drv->uevent)
283 return drv->uevent(xdev, envp, num_envp, buffer,
284 buffer_size);
285 }
287 return 0;
288 }
290 static void otherend_changed(struct xenbus_watch *watch,
291 const char **vec, unsigned int len)
292 {
293 struct xenbus_device *dev =
294 container_of(watch, struct xenbus_device, otherend_watch);
295 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
296 enum xenbus_state state;
298 /* Protect us against watches firing on old details when the otherend
299 details change, say immediately after a resume. */
300 if (!dev->otherend ||
301 strncmp(dev->otherend, vec[XS_WATCH_PATH],
302 strlen(dev->otherend))) {
303 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
304 return;
305 }
307 state = xenbus_read_driver_state(dev->otherend);
309 DPRINTK("state is %d, %s, %s",
310 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
311 if (drv->otherend_changed)
312 drv->otherend_changed(dev, state);
313 }
316 static int talk_to_otherend(struct xenbus_device *dev)
317 {
318 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
320 free_otherend_watch(dev);
321 free_otherend_details(dev);
323 return drv->read_otherend_details(dev);
324 }
327 static int watch_otherend(struct xenbus_device *dev)
328 {
329 return xenbus_watch_path2(dev, dev->otherend, "state",
330 &dev->otherend_watch, otherend_changed);
331 }
334 static int xenbus_dev_probe(struct device *_dev)
335 {
336 struct xenbus_device *dev = to_xenbus_device(_dev);
337 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
338 const struct xenbus_device_id *id;
339 int err;
341 DPRINTK("");
343 if (!drv->probe) {
344 err = -ENODEV;
345 goto fail;
346 }
348 id = match_device(drv->ids, dev);
349 if (!id) {
350 err = -ENODEV;
351 goto fail;
352 }
354 err = talk_to_otherend(dev);
355 if (err) {
356 printk(KERN_WARNING
357 "xenbus_probe: talk_to_otherend on %s failed.\n",
358 dev->nodename);
359 return err;
360 }
362 err = drv->probe(dev, id);
363 if (err)
364 goto fail;
366 err = watch_otherend(dev);
367 if (err) {
368 printk(KERN_WARNING
369 "xenbus_probe: watch_otherend on %s failed.\n",
370 dev->nodename);
371 return err;
372 }
374 return 0;
375 fail:
376 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
377 xenbus_switch_state(dev, XenbusStateClosed);
378 return -ENODEV;
379 }
381 static int xenbus_dev_remove(struct device *_dev)
382 {
383 struct xenbus_device *dev = to_xenbus_device(_dev);
384 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
386 DPRINTK("");
388 free_otherend_watch(dev);
389 free_otherend_details(dev);
391 if (drv->remove)
392 drv->remove(dev);
394 xenbus_switch_state(dev, XenbusStateClosed);
395 return 0;
396 }
398 static int xenbus_register_driver_common(struct xenbus_driver *drv,
399 struct xen_bus_type *bus)
400 {
401 int ret;
403 drv->driver.name = drv->name;
404 drv->driver.bus = &bus->bus;
405 drv->driver.owner = drv->owner;
406 drv->driver.probe = xenbus_dev_probe;
407 drv->driver.remove = xenbus_dev_remove;
409 mutex_lock(&xenwatch_mutex);
410 ret = driver_register(&drv->driver);
411 mutex_unlock(&xenwatch_mutex);
412 return ret;
413 }
415 int xenbus_register_frontend(struct xenbus_driver *drv)
416 {
417 int ret;
419 drv->read_otherend_details = read_backend_details;
421 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
422 if (ret)
423 return ret;
425 /* If this driver is loaded as a module wait for devices to attach. */
426 wait_for_devices(drv);
428 return 0;
429 }
430 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
432 int xenbus_register_backend(struct xenbus_driver *drv)
433 {
434 drv->read_otherend_details = read_frontend_details;
436 return xenbus_register_driver_common(drv, &xenbus_backend);
437 }
438 EXPORT_SYMBOL_GPL(xenbus_register_backend);
440 void xenbus_unregister_driver(struct xenbus_driver *drv)
441 {
442 driver_unregister(&drv->driver);
443 }
444 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
446 struct xb_find_info
447 {
448 struct xenbus_device *dev;
449 const char *nodename;
450 };
452 static int cmp_dev(struct device *dev, void *data)
453 {
454 struct xenbus_device *xendev = to_xenbus_device(dev);
455 struct xb_find_info *info = data;
457 if (!strcmp(xendev->nodename, info->nodename)) {
458 info->dev = xendev;
459 get_device(dev);
460 return 1;
461 }
462 return 0;
463 }
465 struct xenbus_device *xenbus_device_find(const char *nodename,
466 struct bus_type *bus)
467 {
468 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
470 bus_for_each_dev(bus, NULL, &info, cmp_dev);
471 return info.dev;
472 }
474 static int cleanup_dev(struct device *dev, void *data)
475 {
476 struct xenbus_device *xendev = to_xenbus_device(dev);
477 struct xb_find_info *info = data;
478 int len = strlen(info->nodename);
480 DPRINTK("%s", info->nodename);
482 /* Match the info->nodename path, or any subdirectory of that path. */
483 if (strncmp(xendev->nodename, info->nodename, len))
484 return 0;
486 /* If the node name is longer, ensure it really is a subdirectory. */
487 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
488 return 0;
490 info->dev = xendev;
491 get_device(dev);
492 return 1;
493 }
495 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
496 {
497 struct xb_find_info info = { .nodename = path };
499 do {
500 info.dev = NULL;
501 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
502 if (info.dev) {
503 device_unregister(&info.dev->dev);
504 put_device(&info.dev->dev);
505 }
506 } while (info.dev);
507 }
509 static void xenbus_dev_release(struct device *dev)
510 {
511 if (dev)
512 kfree(to_xenbus_device(dev));
513 }
515 /* Simplified asprintf. */
516 char *kasprintf(const char *fmt, ...)
517 {
518 va_list ap;
519 unsigned int len;
520 char *p, dummy[1];
522 va_start(ap, fmt);
523 /* FIXME: vsnprintf has a bug, NULL should work */
524 len = vsnprintf(dummy, 0, fmt, ap);
525 va_end(ap);
527 p = kmalloc(len + 1, GFP_KERNEL);
528 if (!p)
529 return NULL;
530 va_start(ap, fmt);
531 vsprintf(p, fmt, ap);
532 va_end(ap);
533 return p;
534 }
536 static ssize_t xendev_show_nodename(struct device *dev,
537 struct device_attribute *attr, char *buf)
538 {
539 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
540 }
541 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
543 static ssize_t xendev_show_devtype(struct device *dev,
544 struct device_attribute *attr, char *buf)
545 {
546 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
547 }
548 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
551 static int xenbus_probe_node(struct xen_bus_type *bus,
552 const char *type,
553 const char *nodename)
554 {
555 int err;
556 struct xenbus_device *xendev;
557 size_t stringlen;
558 char *tmpstring;
560 enum xenbus_state state = xenbus_read_driver_state(nodename);
562 if (state != XenbusStateInitialising) {
563 /* Device is not new, so ignore it. This can happen if a
564 device is going away after switching to Closed. */
565 return 0;
566 }
568 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
569 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
570 if (!xendev)
571 return -ENOMEM;
573 /* Copy the strings into the extra space. */
575 tmpstring = (char *)(xendev + 1);
576 strcpy(tmpstring, nodename);
577 xendev->nodename = tmpstring;
579 tmpstring += strlen(tmpstring) + 1;
580 strcpy(tmpstring, type);
581 xendev->devicetype = tmpstring;
583 xendev->dev.parent = &bus->dev;
584 xendev->dev.bus = &bus->bus;
585 xendev->dev.release = xenbus_dev_release;
587 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
588 if (err)
589 goto fail;
591 /* Register with generic device framework. */
592 err = device_register(&xendev->dev);
593 if (err)
594 goto fail;
596 device_create_file(&xendev->dev, &dev_attr_nodename);
597 device_create_file(&xendev->dev, &dev_attr_devtype);
599 return 0;
600 fail:
601 kfree(xendev);
602 return err;
603 }
605 /* device/<typename>/<name> */
606 static int xenbus_probe_frontend(const char *type, const char *name)
607 {
608 char *nodename;
609 int err;
611 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
612 if (!nodename)
613 return -ENOMEM;
615 DPRINTK("%s", nodename);
617 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
618 kfree(nodename);
619 return err;
620 }
622 /* backend/<typename>/<frontend-uuid>/<name> */
623 static int xenbus_probe_backend_unit(const char *dir,
624 const char *type,
625 const char *name)
626 {
627 char *nodename;
628 int err;
630 nodename = kasprintf("%s/%s", dir, name);
631 if (!nodename)
632 return -ENOMEM;
634 DPRINTK("%s\n", nodename);
636 err = xenbus_probe_node(&xenbus_backend, type, nodename);
637 kfree(nodename);
638 return err;
639 }
641 /* backend/<typename>/<frontend-domid> */
642 static int xenbus_probe_backend(const char *type, const char *domid)
643 {
644 char *nodename;
645 int err = 0;
646 char **dir;
647 unsigned int i, dir_n = 0;
649 DPRINTK("");
651 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
652 if (!nodename)
653 return -ENOMEM;
655 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
656 if (IS_ERR(dir)) {
657 kfree(nodename);
658 return PTR_ERR(dir);
659 }
661 for (i = 0; i < dir_n; i++) {
662 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
663 if (err)
664 break;
665 }
666 kfree(dir);
667 kfree(nodename);
668 return err;
669 }
671 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
672 {
673 int err = 0;
674 char **dir;
675 unsigned int dir_n = 0;
676 int i;
678 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
679 if (IS_ERR(dir))
680 return PTR_ERR(dir);
682 for (i = 0; i < dir_n; i++) {
683 err = bus->probe(type, dir[i]);
684 if (err)
685 break;
686 }
687 kfree(dir);
688 return err;
689 }
691 static int xenbus_probe_devices(struct xen_bus_type *bus)
692 {
693 int err = 0;
694 char **dir;
695 unsigned int i, dir_n;
697 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
698 if (IS_ERR(dir))
699 return PTR_ERR(dir);
701 for (i = 0; i < dir_n; i++) {
702 err = xenbus_probe_device_type(bus, dir[i]);
703 if (err)
704 break;
705 }
706 kfree(dir);
707 return err;
708 }
710 static unsigned int char_count(const char *str, char c)
711 {
712 unsigned int i, ret = 0;
714 for (i = 0; str[i]; i++)
715 if (str[i] == c)
716 ret++;
717 return ret;
718 }
720 static int strsep_len(const char *str, char c, unsigned int len)
721 {
722 unsigned int i;
724 for (i = 0; str[i]; i++)
725 if (str[i] == c) {
726 if (len == 0)
727 return i;
728 len--;
729 }
730 return (len == 0) ? i : -ERANGE;
731 }
733 static void dev_changed(const char *node, struct xen_bus_type *bus)
734 {
735 int exists, rootlen;
736 struct xenbus_device *dev;
737 char type[BUS_ID_SIZE];
738 const char *p, *root;
740 if (char_count(node, '/') < 2)
741 return;
743 exists = xenbus_exists(XBT_NIL, node, "");
744 if (!exists) {
745 xenbus_cleanup_devices(node, &bus->bus);
746 return;
747 }
749 /* backend/<type>/... or device/<type>/... */
750 p = strchr(node, '/') + 1;
751 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
752 type[BUS_ID_SIZE-1] = '\0';
754 rootlen = strsep_len(node, '/', bus->levels);
755 if (rootlen < 0)
756 return;
757 root = kasprintf("%.*s", rootlen, node);
758 if (!root)
759 return;
761 dev = xenbus_device_find(root, &bus->bus);
762 if (!dev)
763 xenbus_probe_node(bus, type, root);
764 else
765 put_device(&dev->dev);
767 kfree(root);
768 }
770 static void frontend_changed(struct xenbus_watch *watch,
771 const char **vec, unsigned int len)
772 {
773 DPRINTK("");
775 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
776 }
778 static void backend_changed(struct xenbus_watch *watch,
779 const char **vec, unsigned int len)
780 {
781 DPRINTK("");
783 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
784 }
786 /* We watch for devices appearing and vanishing. */
787 static struct xenbus_watch fe_watch = {
788 .node = "device",
789 .callback = frontend_changed,
790 };
792 static struct xenbus_watch be_watch = {
793 .node = "backend",
794 .callback = backend_changed,
795 };
797 static int suspend_dev(struct device *dev, void *data)
798 {
799 int err = 0;
800 struct xenbus_driver *drv;
801 struct xenbus_device *xdev;
803 DPRINTK("");
805 if (dev->driver == NULL)
806 return 0;
807 drv = to_xenbus_driver(dev->driver);
808 xdev = container_of(dev, struct xenbus_device, dev);
809 if (drv->suspend)
810 err = drv->suspend(xdev);
811 if (err)
812 printk(KERN_WARNING
813 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
814 return 0;
815 }
817 static int resume_dev(struct device *dev, void *data)
818 {
819 int err;
820 struct xenbus_driver *drv;
821 struct xenbus_device *xdev;
823 DPRINTK("");
825 if (dev->driver == NULL)
826 return 0;
828 drv = to_xenbus_driver(dev->driver);
829 xdev = container_of(dev, struct xenbus_device, dev);
831 err = talk_to_otherend(xdev);
832 if (err) {
833 printk(KERN_WARNING
834 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
835 dev->bus_id, err);
836 return err;
837 }
839 xdev->state = XenbusStateInitialising;
841 if (drv->resume) {
842 err = drv->resume(xdev);
843 if (err) {
844 printk(KERN_WARNING
845 "xenbus: resume %s failed: %i\n",
846 dev->bus_id, err);
847 return err;
848 }
849 }
851 err = watch_otherend(xdev);
852 if (err) {
853 printk(KERN_WARNING
854 "xenbus_probe: resume (watch_otherend) %s failed: "
855 "%d.\n", dev->bus_id, err);
856 return err;
857 }
859 return 0;
860 }
862 void xenbus_suspend(void)
863 {
864 DPRINTK("");
866 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
867 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
868 xs_suspend();
869 }
870 EXPORT_SYMBOL_GPL(xenbus_suspend);
872 void xenbus_resume(void)
873 {
874 xb_init_comms();
875 xs_resume();
876 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
877 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
878 }
879 EXPORT_SYMBOL_GPL(xenbus_resume);
882 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
883 int xenstored_ready = 0;
886 int register_xenstore_notifier(struct notifier_block *nb)
887 {
888 int ret = 0;
890 if (xenstored_ready > 0)
891 ret = nb->notifier_call(nb, 0, NULL);
892 else
893 notifier_chain_register(&xenstore_chain, nb);
895 return ret;
896 }
897 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
899 void unregister_xenstore_notifier(struct notifier_block *nb)
900 {
901 notifier_chain_unregister(&xenstore_chain, nb);
902 }
903 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
906 void xenbus_probe(void *unused)
907 {
908 BUG_ON((xenstored_ready <= 0));
910 /* Enumerate devices in xenstore. */
911 xenbus_probe_devices(&xenbus_frontend);
912 xenbus_probe_devices(&xenbus_backend);
914 /* Watch for changes. */
915 register_xenbus_watch(&fe_watch);
916 register_xenbus_watch(&be_watch);
918 /* Notify others that xenstore is up */
919 notifier_call_chain(&xenstore_chain, 0, NULL);
920 }
923 #ifdef CONFIG_PROC_FS
924 static struct file_operations xsd_kva_fops;
925 static struct proc_dir_entry *xsd_kva_intf;
926 static struct proc_dir_entry *xsd_port_intf;
928 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
929 {
930 size_t size = vma->vm_end - vma->vm_start;
932 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
933 return -EINVAL;
935 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
936 size, vma->vm_page_prot))
937 return -EAGAIN;
939 return 0;
940 }
942 static int xsd_kva_read(char *page, char **start, off_t off,
943 int count, int *eof, void *data)
944 {
945 int len;
947 len = sprintf(page, "0x%p", xen_store_interface);
948 *eof = 1;
949 return len;
950 }
952 static int xsd_port_read(char *page, char **start, off_t off,
953 int count, int *eof, void *data)
954 {
955 int len;
957 len = sprintf(page, "%d", xen_store_evtchn);
958 *eof = 1;
959 return len;
960 }
961 #endif
963 static int __init xenbus_probe_init(void)
964 {
965 int err = 0;
966 unsigned long page = 0;
968 DPRINTK("");
970 if (!is_running_on_xen())
971 return -ENODEV;
973 /* Register ourselves with the kernel bus subsystem */
974 bus_register(&xenbus_frontend.bus);
975 bus_register(&xenbus_backend.bus);
977 /*
978 * Domain0 doesn't have a store_evtchn or store_mfn yet.
979 */
980 if (is_initial_xendomain()) {
981 struct evtchn_alloc_unbound alloc_unbound;
983 /* Allocate page. */
984 page = get_zeroed_page(GFP_KERNEL);
985 if (!page)
986 return -ENOMEM;
988 xen_store_mfn = xen_start_info->store_mfn =
989 pfn_to_mfn(virt_to_phys((void *)page) >>
990 PAGE_SHIFT);
992 /* Next allocate a local port which xenstored can bind to */
993 alloc_unbound.dom = DOMID_SELF;
994 alloc_unbound.remote_dom = 0;
996 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
997 &alloc_unbound);
998 if (err == -ENOSYS)
999 goto err;
1000 BUG_ON(err);
1001 xen_store_evtchn = xen_start_info->store_evtchn =
1002 alloc_unbound.port;
1004 #ifdef CONFIG_PROC_FS
1005 /* And finally publish the above info in /proc/xen */
1006 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1007 if (xsd_kva_intf) {
1008 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1009 sizeof(xsd_kva_fops));
1010 xsd_kva_fops.mmap = xsd_kva_mmap;
1011 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1012 xsd_kva_intf->read_proc = xsd_kva_read;
1014 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1015 if (xsd_port_intf)
1016 xsd_port_intf->read_proc = xsd_port_read;
1017 #endif
1018 } else {
1019 xenstored_ready = 1;
1020 xen_store_evtchn = xen_start_info->store_evtchn;
1021 xen_store_mfn = xen_start_info->store_mfn;
1024 xen_store_interface = mfn_to_virt(xen_store_mfn);
1026 xenbus_dev_init();
1028 /* Initialize the interface to xenstore. */
1029 err = xs_init();
1030 if (err) {
1031 printk(KERN_WARNING
1032 "XENBUS: Error initializing xenstore comms: %i\n", err);
1033 goto err;
1036 /* Register ourselves with the kernel device subsystem */
1037 device_register(&xenbus_frontend.dev);
1038 device_register(&xenbus_backend.dev);
1040 if (!is_initial_xendomain())
1041 xenbus_probe(NULL);
1043 return 0;
1045 err:
1046 if (page)
1047 free_page(page);
1049 /*
1050 * Do not unregister the xenbus front/backend buses here. The buses
1051 * must exist because front/backend drivers will use them when they are
1052 * registered.
1053 */
1055 return err;
1058 postcore_initcall(xenbus_probe_init);
1060 MODULE_LICENSE("Dual BSD/GPL");
1063 static int is_disconnected_device(struct device *dev, void *data)
1065 struct xenbus_device *xendev = to_xenbus_device(dev);
1066 struct device_driver *drv = data;
1068 /*
1069 * A device with no driver will never connect. We care only about
1070 * devices which should currently be in the process of connecting.
1071 */
1072 if (!dev->driver)
1073 return 0;
1075 /* Is this search limited to a particular driver? */
1076 if (drv && (dev->driver != drv))
1077 return 0;
1079 return (xendev->state != XenbusStateConnected);
1082 static int exists_disconnected_device(struct device_driver *drv)
1084 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1085 is_disconnected_device);
1088 static int print_device_status(struct device *dev, void *data)
1090 struct xenbus_device *xendev = to_xenbus_device(dev);
1091 struct device_driver *drv = data;
1093 /* Is this operation limited to a particular driver? */
1094 if (drv && (dev->driver != drv))
1095 return 0;
1097 if (!dev->driver) {
1098 /* Information only: is this too noisy? */
1099 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1100 xendev->nodename);
1101 } else if (xendev->state != XenbusStateConnected) {
1102 printk(KERN_WARNING "XENBUS: Timeout connecting "
1103 "to device: %s (state %d)\n",
1104 xendev->nodename, xendev->state);
1107 return 0;
1110 /* We only wait for device setup after most initcalls have run. */
1111 static int ready_to_wait_for_devices;
1113 /*
1114 * On a 10 second timeout, wait for all devices currently configured. We need
1115 * to do this to guarantee that the filesystems and / or network devices
1116 * needed for boot are available, before we can allow the boot to proceed.
1118 * This needs to be on a late_initcall, to happen after the frontend device
1119 * drivers have been initialised, but before the root fs is mounted.
1121 * A possible improvement here would be to have the tools add a per-device
1122 * flag to the store entry, indicating whether it is needed at boot time.
1123 * This would allow people who knew what they were doing to accelerate their
1124 * boot slightly, but of course needs tools or manual intervention to set up
1125 * those flags correctly.
1126 */
1127 static void wait_for_devices(struct xenbus_driver *xendrv)
1129 unsigned long timeout = jiffies + 10*HZ;
1130 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1132 if (!ready_to_wait_for_devices || !is_running_on_xen())
1133 return;
1135 while (exists_disconnected_device(drv)) {
1136 if (time_after(jiffies, timeout))
1137 break;
1138 schedule_timeout_interruptible(HZ/10);
1141 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1142 print_device_status);
1145 #ifndef MODULE
1146 static int __init boot_wait_for_devices(void)
1148 ready_to_wait_for_devices = 1;
1149 wait_for_devices(NULL);
1150 return 0;
1153 late_initcall(boot_wait_for_devices);
1154 #endif