ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_probe.c @ 876:bd7e30b58d12

xenbus: Allow lazy init in case xenstored runs in a separate minios domain.

Here's an explanation of the states:

It starts out in XENBUS_XSD_UNCOMMITTED.

As the master xenbus (the one local to xenstored), it will receive an
mmap from xenstore, putting it in XENBUS_XSD_LOCAL_INIT. This enables
the wake_waiting IRQ, which will put it in XENBUS_XSD_LOCAL_READY.

Alternatively, as a slave xenbus, it will receive an ioctl from the
xenstore domain builder, putting it in XENBUS_XSD_FOREIGN_INIT. This
enables the wake_waiting IRQ, which will put it in
XENBUS_XSD_FOREIGN_READY.

DomU's are immediately initialized to XENBUS_XSD_FOREIGN_READY.

Signed-off-by: Diego Ongaro <diego.ongaro@citrix.com>
Signed-off-by: Alex Zeffertt <alex.zeffertt@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 19 14:45:50 2009 +0100 (2009-05-19)
parents e17af34d4d90
children 865707f40531
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 * Copyright (C) 2007 Solarflare Communications, Inc.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
32 */
34 #define DPRINTK(fmt, args...) \
35 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
36 __FUNCTION__, __LINE__, ##args)
38 #include <linux/kernel.h>
39 #include <linux/err.h>
40 #include <linux/string.h>
41 #include <linux/ctype.h>
42 #include <linux/fcntl.h>
43 #include <linux/mm.h>
44 #include <linux/notifier.h>
45 #include <linux/mutex.h>
46 #include <linux/module.h>
47 #include <xen/gnttab.h>
49 #include <asm/io.h>
50 #include <asm/page.h>
51 #include <asm/maddr.h>
52 #include <asm/pgtable.h>
53 #include <asm/hypervisor.h>
54 #include <xen/xenbus.h>
55 #include <xen/xen_proc.h>
56 #include <xen/evtchn.h>
57 #include <xen/features.h>
58 #ifdef MODULE
59 #include <xen/hvm.h>
60 #endif
62 #include "xenbus_comms.h"
63 #include "xenbus_probe.h"
65 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
66 #include <xen/platform-compat.h>
67 #endif
69 int xen_store_evtchn;
70 struct xenstore_domain_interface *xen_store_interface;
71 static unsigned long xen_store_mfn;
73 extern struct mutex xenwatch_mutex;
75 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
77 static void wait_for_devices(struct xenbus_driver *xendrv);
79 static int xenbus_probe_frontend(const char *type, const char *name);
81 static void xenbus_dev_shutdown(struct device *_dev);
83 /* If something in array of ids matches this device, return it. */
84 static const struct xenbus_device_id *
85 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
86 {
87 for (; *arr->devicetype != '\0'; arr++) {
88 if (!strcmp(arr->devicetype, dev->devicetype))
89 return arr;
90 }
91 return NULL;
92 }
94 int xenbus_match(struct device *_dev, struct device_driver *_drv)
95 {
96 struct xenbus_driver *drv = to_xenbus_driver(_drv);
98 if (!drv->ids)
99 return 0;
101 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
102 }
104 /* device/<type>/<id> => <type>-<id> */
105 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
106 {
107 nodename = strchr(nodename, '/');
108 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
109 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
110 return -EINVAL;
111 }
113 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
114 if (!strchr(bus_id, '/')) {
115 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
116 return -EINVAL;
117 }
118 *strchr(bus_id, '/') = '-';
119 return 0;
120 }
123 static void free_otherend_details(struct xenbus_device *dev)
124 {
125 kfree(dev->otherend);
126 dev->otherend = NULL;
127 }
130 static void free_otherend_watch(struct xenbus_device *dev)
131 {
132 if (dev->otherend_watch.node) {
133 unregister_xenbus_watch(&dev->otherend_watch);
134 kfree(dev->otherend_watch.node);
135 dev->otherend_watch.node = NULL;
136 }
137 }
140 int read_otherend_details(struct xenbus_device *xendev,
141 char *id_node, char *path_node)
142 {
143 int err = xenbus_gather(XBT_NIL, xendev->nodename,
144 id_node, "%i", &xendev->otherend_id,
145 path_node, NULL, &xendev->otherend,
146 NULL);
147 if (err) {
148 xenbus_dev_fatal(xendev, err,
149 "reading other end details from %s",
150 xendev->nodename);
151 return err;
152 }
153 if (strlen(xendev->otherend) == 0 ||
154 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
155 xenbus_dev_fatal(xendev, -ENOENT,
156 "unable to read other end from %s. "
157 "missing or inaccessible.",
158 xendev->nodename);
159 free_otherend_details(xendev);
160 return -ENOENT;
161 }
163 return 0;
164 }
167 static int read_backend_details(struct xenbus_device *xendev)
168 {
169 return read_otherend_details(xendev, "backend-id", "backend");
170 }
172 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
173 static int xenbus_uevent_frontend(struct device *dev, char **envp,
174 int num_envp, char *buffer, int buffer_size)
175 {
176 struct xenbus_device *xdev;
177 int length = 0, i = 0;
179 if (dev == NULL)
180 return -ENODEV;
181 xdev = to_xenbus_device(dev);
182 if (xdev == NULL)
183 return -ENODEV;
185 /* stuff we want to pass to /sbin/hotplug */
186 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
187 "XENBUS_TYPE=%s", xdev->devicetype);
188 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
189 "XENBUS_PATH=%s", xdev->nodename);
190 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
191 "MODALIAS=xen:%s", xdev->devicetype);
193 return 0;
194 }
195 #endif
197 /* Bus type for frontend drivers. */
198 static struct xen_bus_type xenbus_frontend = {
199 .root = "device",
200 .levels = 2, /* device/type/<id> */
201 .get_bus_id = frontend_bus_id,
202 .probe = xenbus_probe_frontend,
203 .error = -ENODEV,
204 .bus = {
205 .name = "xen",
206 .match = xenbus_match,
207 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
208 .probe = xenbus_dev_probe,
209 .remove = xenbus_dev_remove,
210 .shutdown = xenbus_dev_shutdown,
211 .uevent = xenbus_uevent_frontend,
212 #endif
213 },
214 .dev = {
215 .bus_id = "xen",
216 },
217 };
219 static void otherend_changed(struct xenbus_watch *watch,
220 const char **vec, unsigned int len)
221 {
222 struct xenbus_device *dev =
223 container_of(watch, struct xenbus_device, otherend_watch);
224 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
225 enum xenbus_state state;
227 /* Protect us against watches firing on old details when the otherend
228 details change, say immediately after a resume. */
229 if (!dev->otherend ||
230 strncmp(dev->otherend, vec[XS_WATCH_PATH],
231 strlen(dev->otherend))) {
232 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
233 return;
234 }
236 state = xenbus_read_driver_state(dev->otherend);
238 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
239 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
242 /*
243 * Ignore xenbus transitions during shutdown. This prevents us doing
244 * work that can fail e.g., when the rootfs is gone.
245 */
246 if (system_state > SYSTEM_RUNNING) {
247 struct xen_bus_type *bus = bus;
248 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
249 /* If we're frontend, drive the state machine to Closed. */
250 /* This should cause the backend to release our resources. */
251 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
252 xenbus_frontend_closed(dev);
253 return;
254 }
255 #endif
257 if (drv->otherend_changed)
258 drv->otherend_changed(dev, state);
259 }
262 static int talk_to_otherend(struct xenbus_device *dev)
263 {
264 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
266 free_otherend_watch(dev);
267 free_otherend_details(dev);
269 return drv->read_otherend_details(dev);
270 }
273 static int watch_otherend(struct xenbus_device *dev)
274 {
275 return xenbus_watch_path2(dev, dev->otherend, "state",
276 &dev->otherend_watch, otherend_changed);
277 }
280 int xenbus_dev_probe(struct device *_dev)
281 {
282 struct xenbus_device *dev = to_xenbus_device(_dev);
283 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
284 const struct xenbus_device_id *id;
285 int err;
287 DPRINTK("%s", dev->nodename);
289 if (!drv->probe) {
290 err = -ENODEV;
291 goto fail;
292 }
294 id = match_device(drv->ids, dev);
295 if (!id) {
296 err = -ENODEV;
297 goto fail;
298 }
300 err = talk_to_otherend(dev);
301 if (err) {
302 printk(KERN_WARNING
303 "xenbus_probe: talk_to_otherend on %s failed.\n",
304 dev->nodename);
305 return err;
306 }
308 err = drv->probe(dev, id);
309 if (err)
310 goto fail;
312 err = watch_otherend(dev);
313 if (err) {
314 printk(KERN_WARNING
315 "xenbus_probe: watch_otherend on %s failed.\n",
316 dev->nodename);
317 return err;
318 }
320 return 0;
321 fail:
322 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
323 xenbus_switch_state(dev, XenbusStateClosed);
324 return -ENODEV;
325 }
327 int xenbus_dev_remove(struct device *_dev)
328 {
329 struct xenbus_device *dev = to_xenbus_device(_dev);
330 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
332 DPRINTK("%s", dev->nodename);
334 free_otherend_watch(dev);
335 free_otherend_details(dev);
337 if (drv->remove)
338 drv->remove(dev);
340 xenbus_switch_state(dev, XenbusStateClosed);
341 return 0;
342 }
344 static void xenbus_dev_shutdown(struct device *_dev)
345 {
346 struct xenbus_device *dev = to_xenbus_device(_dev);
347 unsigned long timeout = 5*HZ;
349 DPRINTK("%s", dev->nodename);
351 /* Commented out since xenstored stubdom is now minios based not linux based
352 #define XENSTORE_DOMAIN_SHARES_THIS_KERNEL
353 */
354 #ifndef XENSTORE_DOMAIN_SHARES_THIS_KERNEL
355 if (is_initial_xendomain())
356 #endif
357 return;
359 get_device(&dev->dev);
360 if (dev->state != XenbusStateConnected) {
361 printk("%s: %s: %s != Connected, skipping\n", __FUNCTION__,
362 dev->nodename, xenbus_strstate(dev->state));
363 goto out;
364 }
365 xenbus_switch_state(dev, XenbusStateClosing);
366 timeout = wait_for_completion_timeout(&dev->down, timeout);
367 if (!timeout)
368 printk("%s: %s timeout closing device\n", __FUNCTION__, dev->nodename);
369 out:
370 put_device(&dev->dev);
371 }
373 int xenbus_register_driver_common(struct xenbus_driver *drv,
374 struct xen_bus_type *bus)
375 {
376 int ret;
378 if (bus->error)
379 return bus->error;
381 drv->driver.name = drv->name;
382 drv->driver.bus = &bus->bus;
383 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
384 drv->driver.owner = drv->owner;
385 #endif
386 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
387 drv->driver.probe = xenbus_dev_probe;
388 drv->driver.remove = xenbus_dev_remove;
389 drv->driver.shutdown = xenbus_dev_shutdown;
390 #endif
392 mutex_lock(&xenwatch_mutex);
393 ret = driver_register(&drv->driver);
394 mutex_unlock(&xenwatch_mutex);
395 return ret;
396 }
398 int xenbus_register_frontend(struct xenbus_driver *drv)
399 {
400 int ret;
402 drv->read_otherend_details = read_backend_details;
404 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
405 if (ret)
406 return ret;
408 /* If this driver is loaded as a module wait for devices to attach. */
409 wait_for_devices(drv);
411 return 0;
412 }
413 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
415 void xenbus_unregister_driver(struct xenbus_driver *drv)
416 {
417 driver_unregister(&drv->driver);
418 }
419 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
421 struct xb_find_info
422 {
423 struct xenbus_device *dev;
424 const char *nodename;
425 };
427 static int cmp_dev(struct device *dev, void *data)
428 {
429 struct xenbus_device *xendev = to_xenbus_device(dev);
430 struct xb_find_info *info = data;
432 if (!strcmp(xendev->nodename, info->nodename)) {
433 info->dev = xendev;
434 get_device(dev);
435 return 1;
436 }
437 return 0;
438 }
440 struct xenbus_device *xenbus_device_find(const char *nodename,
441 struct bus_type *bus)
442 {
443 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
445 bus_for_each_dev(bus, NULL, &info, cmp_dev);
446 return info.dev;
447 }
449 static int cleanup_dev(struct device *dev, void *data)
450 {
451 struct xenbus_device *xendev = to_xenbus_device(dev);
452 struct xb_find_info *info = data;
453 int len = strlen(info->nodename);
455 DPRINTK("%s", info->nodename);
457 /* Match the info->nodename path, or any subdirectory of that path. */
458 if (strncmp(xendev->nodename, info->nodename, len))
459 return 0;
461 /* If the node name is longer, ensure it really is a subdirectory. */
462 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
463 return 0;
465 info->dev = xendev;
466 get_device(dev);
467 return 1;
468 }
470 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
471 {
472 struct xb_find_info info = { .nodename = path };
474 do {
475 info.dev = NULL;
476 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
477 if (info.dev) {
478 device_unregister(&info.dev->dev);
479 put_device(&info.dev->dev);
480 }
481 } while (info.dev);
482 }
484 static void xenbus_dev_release(struct device *dev)
485 {
486 if (dev)
487 kfree(to_xenbus_device(dev));
488 }
490 static ssize_t xendev_show_nodename(struct device *dev,
491 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
492 struct device_attribute *attr,
493 #endif
494 char *buf)
495 {
496 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
497 }
498 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
500 static ssize_t xendev_show_devtype(struct device *dev,
501 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
502 struct device_attribute *attr,
503 #endif
504 char *buf)
505 {
506 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
507 }
508 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
511 int xenbus_probe_node(struct xen_bus_type *bus,
512 const char *type,
513 const char *nodename)
514 {
515 int err;
516 struct xenbus_device *xendev;
517 size_t stringlen;
518 char *tmpstring;
520 enum xenbus_state state = xenbus_read_driver_state(nodename);
522 if (bus->error)
523 return bus->error;
525 if (state != XenbusStateInitialising) {
526 /* Device is not new, so ignore it. This can happen if a
527 device is going away after switching to Closed. */
528 return 0;
529 }
531 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
532 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
533 if (!xendev)
534 return -ENOMEM;
536 xendev->state = XenbusStateInitialising;
538 /* Copy the strings into the extra space. */
540 tmpstring = (char *)(xendev + 1);
541 strcpy(tmpstring, nodename);
542 xendev->nodename = tmpstring;
544 tmpstring += strlen(tmpstring) + 1;
545 strcpy(tmpstring, type);
546 xendev->devicetype = tmpstring;
547 init_completion(&xendev->down);
549 xendev->dev.parent = &bus->dev;
550 xendev->dev.bus = &bus->bus;
551 xendev->dev.release = xenbus_dev_release;
553 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
554 if (err)
555 goto fail;
557 /* Register with generic device framework. */
558 err = device_register(&xendev->dev);
559 if (err)
560 goto fail;
562 err = device_create_file(&xendev->dev, &dev_attr_nodename);
563 if (err)
564 goto unregister;
565 err = device_create_file(&xendev->dev, &dev_attr_devtype);
566 if (err)
567 goto unregister;
569 return 0;
570 unregister:
571 device_remove_file(&xendev->dev, &dev_attr_nodename);
572 device_remove_file(&xendev->dev, &dev_attr_devtype);
573 device_unregister(&xendev->dev);
574 fail:
575 kfree(xendev);
576 return err;
577 }
579 /* device/<typename>/<name> */
580 static int xenbus_probe_frontend(const char *type, const char *name)
581 {
582 char *nodename;
583 int err;
585 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name);
586 if (!nodename)
587 return -ENOMEM;
589 DPRINTK("%s", nodename);
591 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
592 kfree(nodename);
593 return err;
594 }
596 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
597 {
598 int err = 0;
599 char **dir;
600 unsigned int dir_n = 0;
601 int i;
603 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
604 if (IS_ERR(dir))
605 return PTR_ERR(dir);
607 for (i = 0; i < dir_n; i++) {
608 err = bus->probe(type, dir[i]);
609 if (err)
610 break;
611 }
612 kfree(dir);
613 return err;
614 }
616 int xenbus_probe_devices(struct xen_bus_type *bus)
617 {
618 int err = 0;
619 char **dir;
620 unsigned int i, dir_n;
622 if (bus->error)
623 return bus->error;
625 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
626 if (IS_ERR(dir))
627 return PTR_ERR(dir);
629 for (i = 0; i < dir_n; i++) {
630 err = xenbus_probe_device_type(bus, dir[i]);
631 if (err)
632 break;
633 }
634 kfree(dir);
635 return err;
636 }
638 static unsigned int char_count(const char *str, char c)
639 {
640 unsigned int i, ret = 0;
642 for (i = 0; str[i]; i++)
643 if (str[i] == c)
644 ret++;
645 return ret;
646 }
648 static int strsep_len(const char *str, char c, unsigned int len)
649 {
650 unsigned int i;
652 for (i = 0; str[i]; i++)
653 if (str[i] == c) {
654 if (len == 0)
655 return i;
656 len--;
657 }
658 return (len == 0) ? i : -ERANGE;
659 }
661 void dev_changed(const char *node, struct xen_bus_type *bus)
662 {
663 int exists, rootlen;
664 struct xenbus_device *dev;
665 char type[BUS_ID_SIZE];
666 const char *p, *root;
668 if (bus->error || char_count(node, '/') < 2)
669 return;
671 exists = xenbus_exists(XBT_NIL, node, "");
672 if (!exists) {
673 xenbus_cleanup_devices(node, &bus->bus);
674 return;
675 }
677 /* backend/<type>/... or device/<type>/... */
678 p = strchr(node, '/') + 1;
679 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
680 type[BUS_ID_SIZE-1] = '\0';
682 rootlen = strsep_len(node, '/', bus->levels);
683 if (rootlen < 0)
684 return;
685 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
686 if (!root)
687 return;
689 dev = xenbus_device_find(root, &bus->bus);
690 if (!dev)
691 xenbus_probe_node(bus, type, root);
692 else
693 put_device(&dev->dev);
695 kfree(root);
696 }
698 static void frontend_changed(struct xenbus_watch *watch,
699 const char **vec, unsigned int len)
700 {
701 DPRINTK("");
703 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
704 }
706 /* We watch for devices appearing and vanishing. */
707 static struct xenbus_watch fe_watch = {
708 .node = "device",
709 .callback = frontend_changed,
710 };
712 static int suspend_dev(struct device *dev, void *data)
713 {
714 int err = 0;
715 struct xenbus_driver *drv;
716 struct xenbus_device *xdev;
718 DPRINTK("");
720 if (dev->driver == NULL)
721 return 0;
722 drv = to_xenbus_driver(dev->driver);
723 xdev = container_of(dev, struct xenbus_device, dev);
724 if (drv->suspend)
725 err = drv->suspend(xdev);
726 if (err)
727 printk(KERN_WARNING
728 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
729 return 0;
730 }
732 static int suspend_cancel_dev(struct device *dev, void *data)
733 {
734 int err = 0;
735 struct xenbus_driver *drv;
736 struct xenbus_device *xdev;
738 DPRINTK("");
740 if (dev->driver == NULL)
741 return 0;
742 drv = to_xenbus_driver(dev->driver);
743 xdev = container_of(dev, struct xenbus_device, dev);
744 if (drv->suspend_cancel)
745 err = drv->suspend_cancel(xdev);
746 if (err)
747 printk(KERN_WARNING
748 "xenbus: suspend_cancel %s failed: %i\n",
749 dev->bus_id, err);
750 return 0;
751 }
753 static int resume_dev(struct device *dev, void *data)
754 {
755 int err;
756 struct xenbus_driver *drv;
757 struct xenbus_device *xdev;
759 DPRINTK("");
761 if (dev->driver == NULL)
762 return 0;
764 drv = to_xenbus_driver(dev->driver);
765 xdev = container_of(dev, struct xenbus_device, dev);
767 err = talk_to_otherend(xdev);
768 if (err) {
769 printk(KERN_WARNING
770 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
771 dev->bus_id, err);
772 return err;
773 }
775 xdev->state = XenbusStateInitialising;
777 if (drv->resume) {
778 err = drv->resume(xdev);
779 if (err) {
780 printk(KERN_WARNING
781 "xenbus: resume %s failed: %i\n",
782 dev->bus_id, err);
783 return err;
784 }
785 }
787 err = watch_otherend(xdev);
788 if (err) {
789 printk(KERN_WARNING
790 "xenbus_probe: resume (watch_otherend) %s failed: "
791 "%d.\n", dev->bus_id, err);
792 return err;
793 }
795 return 0;
796 }
798 void xenbus_suspend(void)
799 {
800 DPRINTK("");
802 if (!xenbus_frontend.error)
803 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
804 xenbus_backend_suspend(suspend_dev);
805 xs_suspend();
806 }
807 EXPORT_SYMBOL_GPL(xenbus_suspend);
809 void xenbus_resume(void)
810 {
811 xb_init_comms();
812 xs_resume();
813 if (!xenbus_frontend.error)
814 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
815 xenbus_backend_resume(resume_dev);
816 }
817 EXPORT_SYMBOL_GPL(xenbus_resume);
819 void xenbus_suspend_cancel(void)
820 {
821 xs_suspend_cancel();
822 if (!xenbus_frontend.error)
823 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
824 xenbus_backend_resume(suspend_cancel_dev);
825 }
826 EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
828 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
829 atomic_t xenbus_xsd_state = ATOMIC_INIT(XENBUS_XSD_UNCOMMITTED);
831 int register_xenstore_notifier(struct notifier_block *nb)
832 {
833 int ret = 0;
835 if (is_xenstored_ready())
836 ret = nb->notifier_call(nb, 0, NULL);
837 else
838 blocking_notifier_chain_register(&xenstore_chain, nb);
840 return ret;
841 }
842 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
844 void unregister_xenstore_notifier(struct notifier_block *nb)
845 {
846 blocking_notifier_chain_unregister(&xenstore_chain, nb);
847 }
848 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
851 void xenbus_probe(void *unused)
852 {
853 BUG_ON(!is_xenstored_ready());
855 /* Enumerate devices in xenstore and watch for changes. */
856 xenbus_probe_devices(&xenbus_frontend);
857 register_xenbus_watch(&fe_watch);
858 xenbus_backend_probe_and_watch();
860 /* Notify others that xenstore is up */
861 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
862 }
865 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
866 static struct file_operations xsd_kva_fops;
867 static struct proc_dir_entry *xsd_kva_intf;
868 static struct proc_dir_entry *xsd_port_intf;
870 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
871 {
872 size_t size = vma->vm_end - vma->vm_start;
873 int old;
874 int rc;
876 old = atomic_cmpxchg(&xenbus_xsd_state,
877 XENBUS_XSD_UNCOMMITTED,
878 XENBUS_XSD_LOCAL_INIT);
879 switch (old) {
880 case XENBUS_XSD_UNCOMMITTED:
881 rc = xb_init_comms();
882 if (rc != 0)
883 return rc;
884 break;
886 case XENBUS_XSD_FOREIGN_INIT:
887 case XENBUS_XSD_FOREIGN_READY:
888 return -EBUSY;
890 case XENBUS_XSD_LOCAL_INIT:
891 case XENBUS_XSD_LOCAL_READY:
892 default:
893 break;
894 }
896 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
897 return -EINVAL;
899 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
900 size, vma->vm_page_prot))
901 return -EAGAIN;
903 return 0;
904 }
906 static int xsd_kva_read(char *page, char **start, off_t off,
907 int count, int *eof, void *data)
908 {
909 int len;
911 len = sprintf(page, "0x%p", xen_store_interface);
912 *eof = 1;
913 return len;
914 }
916 static int xsd_port_read(char *page, char **start, off_t off,
917 int count, int *eof, void *data)
918 {
919 int len;
921 len = sprintf(page, "%d", xen_store_evtchn);
922 *eof = 1;
923 return len;
924 }
925 #endif
927 static int xb_free_port(evtchn_port_t port)
928 {
929 struct evtchn_close close;
930 close.port = port;
931 return HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
932 }
934 int xenbus_conn(domid_t remote_dom, unsigned long *grant_ref, evtchn_port_t *local_port)
935 {
936 struct evtchn_alloc_unbound alloc_unbound;
937 int rc, rc2;
939 BUG_ON(atomic_read(&xenbus_xsd_state) != XENBUS_XSD_FOREIGN_INIT);
940 BUG_ON(!is_initial_xendomain());
942 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
943 remove_xen_proc_entry("xsd_kva");
944 remove_xen_proc_entry("xsd_port");
945 #endif
947 rc = xb_free_port(xen_store_evtchn);
948 if (rc != 0)
949 goto fail0;
951 alloc_unbound.dom = DOMID_SELF;
952 alloc_unbound.remote_dom = remote_dom;
953 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
954 &alloc_unbound);
955 if (rc != 0)
956 goto fail0;
957 *local_port = xen_store_evtchn = alloc_unbound.port;
959 /* keep the old page (xen_store_mfn, xen_store_interface) */
960 rc = gnttab_grant_foreign_access(remote_dom, xen_store_mfn,
961 GTF_permit_access);
962 if (rc < 0)
963 goto fail1;
964 *grant_ref = rc;
966 rc = xb_init_comms();
967 if (rc != 0)
968 goto fail1;
970 return 0;
972 fail1:
973 rc2 = xb_free_port(xen_store_evtchn);
974 if (rc2 != 0)
975 printk(KERN_WARNING
976 "XENBUS: Error freeing xenstore event channel: %d\n",
977 rc2);
978 fail0:
979 xen_store_evtchn = -1;
980 return rc;
981 }
983 static int xenbus_probe_init(void)
984 {
985 int err = 0;
986 unsigned long page = 0;
988 DPRINTK("");
990 if (!is_running_on_xen())
991 return -ENODEV;
993 /* Register ourselves with the kernel bus subsystem */
994 xenbus_frontend.error = bus_register(&xenbus_frontend.bus);
995 if (xenbus_frontend.error)
996 printk(KERN_WARNING
997 "XENBUS: Error registering frontend bus: %i\n",
998 xenbus_frontend.error);
999 xenbus_backend_bus_register();
1001 /*
1002 * Domain0 doesn't have a store_evtchn or store_mfn yet.
1003 */
1004 if (is_initial_xendomain()) {
1005 struct evtchn_alloc_unbound alloc_unbound;
1007 /* Allocate page. */
1008 page = get_zeroed_page(GFP_KERNEL);
1009 if (!page)
1010 return -ENOMEM;
1012 xen_store_mfn = xen_start_info->store_mfn =
1013 pfn_to_mfn(virt_to_phys((void *)page) >>
1014 PAGE_SHIFT);
1016 /* Next allocate a local port which xenstored can bind to */
1017 alloc_unbound.dom = DOMID_SELF;
1018 alloc_unbound.remote_dom = DOMID_SELF;
1020 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1021 &alloc_unbound);
1022 if (err == -ENOSYS)
1023 goto err;
1024 BUG_ON(err);
1025 xen_store_evtchn = xen_start_info->store_evtchn =
1026 alloc_unbound.port;
1028 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
1029 /* And finally publish the above info in /proc/xen */
1030 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1031 if (xsd_kva_intf) {
1032 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1033 sizeof(xsd_kva_fops));
1034 xsd_kva_fops.mmap = xsd_kva_mmap;
1035 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1036 xsd_kva_intf->read_proc = xsd_kva_read;
1038 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1039 if (xsd_port_intf)
1040 xsd_port_intf->read_proc = xsd_port_read;
1041 #endif
1042 xen_store_interface = mfn_to_virt(xen_store_mfn);
1043 } else {
1044 atomic_set(&xenbus_xsd_state, XENBUS_XSD_FOREIGN_READY);
1045 #ifdef CONFIG_XEN
1046 xen_store_evtchn = xen_start_info->store_evtchn;
1047 xen_store_mfn = xen_start_info->store_mfn;
1048 xen_store_interface = mfn_to_virt(xen_store_mfn);
1049 #else
1050 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
1051 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
1052 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1053 PAGE_SIZE);
1054 #endif
1055 /* Initialize the shared memory rings to talk to xenstored */
1056 err = xb_init_comms();
1057 if (err)
1058 goto err;
1061 xenbus_dev_init();
1063 /* Initialize the interface to xenstore. */
1064 err = xs_init();
1065 if (err) {
1066 printk(KERN_WARNING
1067 "XENBUS: Error initializing xenstore comms: %i\n", err);
1068 goto err;
1071 /* Register ourselves with the kernel device subsystem */
1072 if (!xenbus_frontend.error) {
1073 xenbus_frontend.error = device_register(&xenbus_frontend.dev);
1074 if (xenbus_frontend.error) {
1075 bus_unregister(&xenbus_frontend.bus);
1076 printk(KERN_WARNING
1077 "XENBUS: Error registering frontend device: %i\n",
1078 xenbus_frontend.error);
1081 xenbus_backend_device_register();
1083 if (!is_initial_xendomain())
1084 xenbus_probe(NULL);
1086 return 0;
1088 err:
1089 if (page)
1090 free_page(page);
1092 /*
1093 * Do not unregister the xenbus front/backend buses here. The buses
1094 * must exist because front/backend drivers will use them when they are
1095 * registered.
1096 */
1098 return err;
1101 #ifdef CONFIG_XEN
1102 postcore_initcall(xenbus_probe_init);
1103 MODULE_LICENSE("Dual BSD/GPL");
1104 #else
1105 int xenbus_init(void)
1107 return xenbus_probe_init();
1109 #endif
1111 static int is_disconnected_device(struct device *dev, void *data)
1113 struct xenbus_device *xendev = to_xenbus_device(dev);
1114 struct device_driver *drv = data;
1115 struct xenbus_driver *xendrv;
1117 /*
1118 * A device with no driver will never connect. We care only about
1119 * devices which should currently be in the process of connecting.
1120 */
1121 if (!dev->driver)
1122 return 0;
1124 /* Is this search limited to a particular driver? */
1125 if (drv && (dev->driver != drv))
1126 return 0;
1128 xendrv = to_xenbus_driver(dev->driver);
1129 return (xendev->state < XenbusStateConnected ||
1130 (xendrv->is_ready && !xendrv->is_ready(xendev)));
1133 static int exists_disconnected_device(struct device_driver *drv)
1135 if (xenbus_frontend.error)
1136 return xenbus_frontend.error;
1137 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1138 is_disconnected_device);
1141 static int print_device_status(struct device *dev, void *data)
1143 struct xenbus_device *xendev = to_xenbus_device(dev);
1144 struct device_driver *drv = data;
1145 struct xenbus_driver *xendrv;
1147 /* Is this operation limited to a particular driver? */
1148 if (drv && (dev->driver != drv))
1149 return 0;
1151 if (!dev->driver) {
1152 /* Information only: is this too noisy? */
1153 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1154 xendev->nodename);
1155 return 0;
1158 if (xendev->state < XenbusStateConnected) {
1159 enum xenbus_state rstate = XenbusStateUnknown;
1160 if (xendev->otherend)
1161 rstate = xenbus_read_driver_state(xendev->otherend);
1162 printk(KERN_WARNING "XENBUS: Timeout connecting "
1163 "to device: %s (local state %d, remote state %d)\n",
1164 xendev->nodename, xendev->state, rstate);
1167 xendrv = to_xenbus_driver(dev->driver);
1168 if (xendrv->is_ready && !xendrv->is_ready(xendev))
1169 printk(KERN_WARNING "XENBUS: Device not ready: %s\n",
1170 xendev->nodename);
1172 return 0;
1175 /* We only wait for device setup after most initcalls have run. */
1176 static int ready_to_wait_for_devices;
1178 /*
1179 * On a 5-minute timeout, wait for all devices currently configured. We need
1180 * to do this to guarantee that the filesystems and / or network devices
1181 * needed for boot are available, before we can allow the boot to proceed.
1183 * This needs to be on a late_initcall, to happen after the frontend device
1184 * drivers have been initialised, but before the root fs is mounted.
1186 * A possible improvement here would be to have the tools add a per-device
1187 * flag to the store entry, indicating whether it is needed at boot time.
1188 * This would allow people who knew what they were doing to accelerate their
1189 * boot slightly, but of course needs tools or manual intervention to set up
1190 * those flags correctly.
1191 */
1192 static void wait_for_devices(struct xenbus_driver *xendrv)
1194 unsigned long start = jiffies;
1195 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1196 unsigned int seconds_waited = 0;
1198 if (!ready_to_wait_for_devices || !is_running_on_xen())
1199 return;
1201 while (exists_disconnected_device(drv)) {
1202 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
1203 if (!seconds_waited)
1204 printk(KERN_WARNING "XENBUS: Waiting for "
1205 "devices to initialise: ");
1206 seconds_waited += 5;
1207 printk("%us...", 300 - seconds_waited);
1208 if (seconds_waited == 300)
1209 break;
1212 schedule_timeout_interruptible(HZ/10);
1215 if (seconds_waited)
1216 printk("\n");
1218 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1219 print_device_status);
1222 #ifndef MODULE
1223 static int __init boot_wait_for_devices(void)
1225 if (!xenbus_frontend.error) {
1226 ready_to_wait_for_devices = 1;
1227 wait_for_devices(NULL);
1229 return 0;
1232 late_initcall(boot_wait_for_devices);
1233 #endif
1235 int xenbus_for_each_frontend(void *arg, int (*fn)(struct device *, void *))
1237 return bus_for_each_dev(&xenbus_frontend.bus, NULL, arg, fn);
1239 EXPORT_SYMBOL_GPL(xenbus_for_each_frontend);