ia64/linux-2.6.18-xen.hg

view drivers/xen/xenbus/xenbus_probe.c @ 522:bb12908b737a

xenbus: Don't try to talk to xenstore from dom0 when xenstore has
already been killed during reboot/shutdown.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Cc: Christian Limpach <christian.limpach@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Apr 15 15:17:41 2008 +0100 (2008-04-15)
parents 1468ebbc3c5d
children e17af34d4d90
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>
48 #include <asm/io.h>
49 #include <asm/page.h>
50 #include <asm/maddr.h>
51 #include <asm/pgtable.h>
52 #include <asm/hypervisor.h>
53 #include <xen/xenbus.h>
54 #include <xen/xen_proc.h>
55 #include <xen/evtchn.h>
56 #include <xen/features.h>
57 #ifdef MODULE
58 #include <xen/hvm.h>
59 #endif
61 #include "xenbus_comms.h"
62 #include "xenbus_probe.h"
64 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
65 #include <xen/platform-compat.h>
66 #endif
68 int xen_store_evtchn;
69 struct xenstore_domain_interface *xen_store_interface;
70 static unsigned long xen_store_mfn;
72 extern struct mutex xenwatch_mutex;
74 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
76 static void wait_for_devices(struct xenbus_driver *xendrv);
78 static int xenbus_probe_frontend(const char *type, const char *name);
80 static void xenbus_dev_shutdown(struct device *_dev);
82 /* If something in array of ids matches this device, return it. */
83 static const struct xenbus_device_id *
84 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
85 {
86 for (; *arr->devicetype != '\0'; arr++) {
87 if (!strcmp(arr->devicetype, dev->devicetype))
88 return arr;
89 }
90 return NULL;
91 }
93 int xenbus_match(struct device *_dev, struct device_driver *_drv)
94 {
95 struct xenbus_driver *drv = to_xenbus_driver(_drv);
97 if (!drv->ids)
98 return 0;
100 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
101 }
103 /* device/<type>/<id> => <type>-<id> */
104 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
105 {
106 nodename = strchr(nodename, '/');
107 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
108 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
109 return -EINVAL;
110 }
112 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
113 if (!strchr(bus_id, '/')) {
114 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
115 return -EINVAL;
116 }
117 *strchr(bus_id, '/') = '-';
118 return 0;
119 }
122 static void free_otherend_details(struct xenbus_device *dev)
123 {
124 kfree(dev->otherend);
125 dev->otherend = NULL;
126 }
129 static void free_otherend_watch(struct xenbus_device *dev)
130 {
131 if (dev->otherend_watch.node) {
132 unregister_xenbus_watch(&dev->otherend_watch);
133 kfree(dev->otherend_watch.node);
134 dev->otherend_watch.node = NULL;
135 }
136 }
139 int read_otherend_details(struct xenbus_device *xendev,
140 char *id_node, char *path_node)
141 {
142 int err = xenbus_gather(XBT_NIL, xendev->nodename,
143 id_node, "%i", &xendev->otherend_id,
144 path_node, NULL, &xendev->otherend,
145 NULL);
146 if (err) {
147 xenbus_dev_fatal(xendev, err,
148 "reading other end details from %s",
149 xendev->nodename);
150 return err;
151 }
152 if (strlen(xendev->otherend) == 0 ||
153 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
154 xenbus_dev_fatal(xendev, -ENOENT,
155 "unable to read other end from %s. "
156 "missing or inaccessible.",
157 xendev->nodename);
158 free_otherend_details(xendev);
159 return -ENOENT;
160 }
162 return 0;
163 }
166 static int read_backend_details(struct xenbus_device *xendev)
167 {
168 return read_otherend_details(xendev, "backend-id", "backend");
169 }
171 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
172 static int xenbus_uevent_frontend(struct device *dev, char **envp,
173 int num_envp, char *buffer, int buffer_size)
174 {
175 struct xenbus_device *xdev;
176 int length = 0, i = 0;
178 if (dev == NULL)
179 return -ENODEV;
180 xdev = to_xenbus_device(dev);
181 if (xdev == NULL)
182 return -ENODEV;
184 /* stuff we want to pass to /sbin/hotplug */
185 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
186 "XENBUS_TYPE=%s", xdev->devicetype);
187 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
188 "XENBUS_PATH=%s", xdev->nodename);
189 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
190 "MODALIAS=xen:%s", xdev->devicetype);
192 return 0;
193 }
194 #endif
196 /* Bus type for frontend drivers. */
197 static struct xen_bus_type xenbus_frontend = {
198 .root = "device",
199 .levels = 2, /* device/type/<id> */
200 .get_bus_id = frontend_bus_id,
201 .probe = xenbus_probe_frontend,
202 .error = -ENODEV,
203 .bus = {
204 .name = "xen",
205 .match = xenbus_match,
206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
207 .probe = xenbus_dev_probe,
208 .remove = xenbus_dev_remove,
209 .shutdown = xenbus_dev_shutdown,
210 .uevent = xenbus_uevent_frontend,
211 #endif
212 },
213 .dev = {
214 .bus_id = "xen",
215 },
216 };
218 static void otherend_changed(struct xenbus_watch *watch,
219 const char **vec, unsigned int len)
220 {
221 struct xenbus_device *dev =
222 container_of(watch, struct xenbus_device, otherend_watch);
223 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
224 enum xenbus_state state;
226 /* Protect us against watches firing on old details when the otherend
227 details change, say immediately after a resume. */
228 if (!dev->otherend ||
229 strncmp(dev->otherend, vec[XS_WATCH_PATH],
230 strlen(dev->otherend))) {
231 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
232 return;
233 }
235 state = xenbus_read_driver_state(dev->otherend);
237 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
238 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
241 /*
242 * Ignore xenbus transitions during shutdown. This prevents us doing
243 * work that can fail e.g., when the rootfs is gone.
244 */
245 if (system_state > SYSTEM_RUNNING) {
246 struct xen_bus_type *bus = bus;
247 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
248 /* If we're frontend, drive the state machine to Closed. */
249 /* This should cause the backend to release our resources. */
250 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
251 xenbus_frontend_closed(dev);
252 return;
253 }
254 #endif
256 if (drv->otherend_changed)
257 drv->otherend_changed(dev, state);
258 }
261 static int talk_to_otherend(struct xenbus_device *dev)
262 {
263 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
265 free_otherend_watch(dev);
266 free_otherend_details(dev);
268 return drv->read_otherend_details(dev);
269 }
272 static int watch_otherend(struct xenbus_device *dev)
273 {
274 return xenbus_watch_path2(dev, dev->otherend, "state",
275 &dev->otherend_watch, otherend_changed);
276 }
279 int xenbus_dev_probe(struct device *_dev)
280 {
281 struct xenbus_device *dev = to_xenbus_device(_dev);
282 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
283 const struct xenbus_device_id *id;
284 int err;
286 DPRINTK("%s", dev->nodename);
288 if (!drv->probe) {
289 err = -ENODEV;
290 goto fail;
291 }
293 id = match_device(drv->ids, dev);
294 if (!id) {
295 err = -ENODEV;
296 goto fail;
297 }
299 err = talk_to_otherend(dev);
300 if (err) {
301 printk(KERN_WARNING
302 "xenbus_probe: talk_to_otherend on %s failed.\n",
303 dev->nodename);
304 return err;
305 }
307 err = drv->probe(dev, id);
308 if (err)
309 goto fail;
311 err = watch_otherend(dev);
312 if (err) {
313 printk(KERN_WARNING
314 "xenbus_probe: watch_otherend on %s failed.\n",
315 dev->nodename);
316 return err;
317 }
319 return 0;
320 fail:
321 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
322 xenbus_switch_state(dev, XenbusStateClosed);
323 return -ENODEV;
324 }
326 int xenbus_dev_remove(struct device *_dev)
327 {
328 struct xenbus_device *dev = to_xenbus_device(_dev);
329 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
331 DPRINTK("%s", dev->nodename);
333 free_otherend_watch(dev);
334 free_otherend_details(dev);
336 if (drv->remove)
337 drv->remove(dev);
339 xenbus_switch_state(dev, XenbusStateClosed);
340 return 0;
341 }
343 static void xenbus_dev_shutdown(struct device *_dev)
344 {
345 struct xenbus_device *dev = to_xenbus_device(_dev);
346 unsigned long timeout = 5*HZ;
348 DPRINTK("%s", dev->nodename);
350 if (is_initial_xendomain())
351 return;
353 get_device(&dev->dev);
354 if (dev->state != XenbusStateConnected) {
355 printk("%s: %s: %s != Connected, skipping\n", __FUNCTION__,
356 dev->nodename, xenbus_strstate(dev->state));
357 goto out;
358 }
359 xenbus_switch_state(dev, XenbusStateClosing);
360 timeout = wait_for_completion_timeout(&dev->down, timeout);
361 if (!timeout)
362 printk("%s: %s timeout closing device\n", __FUNCTION__, dev->nodename);
363 out:
364 put_device(&dev->dev);
365 }
367 int xenbus_register_driver_common(struct xenbus_driver *drv,
368 struct xen_bus_type *bus)
369 {
370 int ret;
372 if (bus->error)
373 return bus->error;
375 drv->driver.name = drv->name;
376 drv->driver.bus = &bus->bus;
377 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
378 drv->driver.owner = drv->owner;
379 #endif
380 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
381 drv->driver.probe = xenbus_dev_probe;
382 drv->driver.remove = xenbus_dev_remove;
383 drv->driver.shutdown = xenbus_dev_shutdown;
384 #endif
386 mutex_lock(&xenwatch_mutex);
387 ret = driver_register(&drv->driver);
388 mutex_unlock(&xenwatch_mutex);
389 return ret;
390 }
392 int xenbus_register_frontend(struct xenbus_driver *drv)
393 {
394 int ret;
396 drv->read_otherend_details = read_backend_details;
398 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
399 if (ret)
400 return ret;
402 /* If this driver is loaded as a module wait for devices to attach. */
403 wait_for_devices(drv);
405 return 0;
406 }
407 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
409 void xenbus_unregister_driver(struct xenbus_driver *drv)
410 {
411 driver_unregister(&drv->driver);
412 }
413 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
415 struct xb_find_info
416 {
417 struct xenbus_device *dev;
418 const char *nodename;
419 };
421 static int cmp_dev(struct device *dev, void *data)
422 {
423 struct xenbus_device *xendev = to_xenbus_device(dev);
424 struct xb_find_info *info = data;
426 if (!strcmp(xendev->nodename, info->nodename)) {
427 info->dev = xendev;
428 get_device(dev);
429 return 1;
430 }
431 return 0;
432 }
434 struct xenbus_device *xenbus_device_find(const char *nodename,
435 struct bus_type *bus)
436 {
437 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
439 bus_for_each_dev(bus, NULL, &info, cmp_dev);
440 return info.dev;
441 }
443 static int cleanup_dev(struct device *dev, void *data)
444 {
445 struct xenbus_device *xendev = to_xenbus_device(dev);
446 struct xb_find_info *info = data;
447 int len = strlen(info->nodename);
449 DPRINTK("%s", info->nodename);
451 /* Match the info->nodename path, or any subdirectory of that path. */
452 if (strncmp(xendev->nodename, info->nodename, len))
453 return 0;
455 /* If the node name is longer, ensure it really is a subdirectory. */
456 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
457 return 0;
459 info->dev = xendev;
460 get_device(dev);
461 return 1;
462 }
464 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
465 {
466 struct xb_find_info info = { .nodename = path };
468 do {
469 info.dev = NULL;
470 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
471 if (info.dev) {
472 device_unregister(&info.dev->dev);
473 put_device(&info.dev->dev);
474 }
475 } while (info.dev);
476 }
478 static void xenbus_dev_release(struct device *dev)
479 {
480 if (dev)
481 kfree(to_xenbus_device(dev));
482 }
484 static ssize_t xendev_show_nodename(struct device *dev,
485 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
486 struct device_attribute *attr,
487 #endif
488 char *buf)
489 {
490 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
491 }
492 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
494 static ssize_t xendev_show_devtype(struct device *dev,
495 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
496 struct device_attribute *attr,
497 #endif
498 char *buf)
499 {
500 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
501 }
502 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
505 int xenbus_probe_node(struct xen_bus_type *bus,
506 const char *type,
507 const char *nodename)
508 {
509 int err;
510 struct xenbus_device *xendev;
511 size_t stringlen;
512 char *tmpstring;
514 enum xenbus_state state = xenbus_read_driver_state(nodename);
516 if (bus->error)
517 return bus->error;
519 if (state != XenbusStateInitialising) {
520 /* Device is not new, so ignore it. This can happen if a
521 device is going away after switching to Closed. */
522 return 0;
523 }
525 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
526 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
527 if (!xendev)
528 return -ENOMEM;
530 xendev->state = XenbusStateInitialising;
532 /* Copy the strings into the extra space. */
534 tmpstring = (char *)(xendev + 1);
535 strcpy(tmpstring, nodename);
536 xendev->nodename = tmpstring;
538 tmpstring += strlen(tmpstring) + 1;
539 strcpy(tmpstring, type);
540 xendev->devicetype = tmpstring;
541 init_completion(&xendev->down);
543 xendev->dev.parent = &bus->dev;
544 xendev->dev.bus = &bus->bus;
545 xendev->dev.release = xenbus_dev_release;
547 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
548 if (err)
549 goto fail;
551 /* Register with generic device framework. */
552 err = device_register(&xendev->dev);
553 if (err)
554 goto fail;
556 err = device_create_file(&xendev->dev, &dev_attr_nodename);
557 if (err)
558 goto unregister;
559 err = device_create_file(&xendev->dev, &dev_attr_devtype);
560 if (err)
561 goto unregister;
563 return 0;
564 unregister:
565 device_remove_file(&xendev->dev, &dev_attr_nodename);
566 device_remove_file(&xendev->dev, &dev_attr_devtype);
567 device_unregister(&xendev->dev);
568 fail:
569 kfree(xendev);
570 return err;
571 }
573 /* device/<typename>/<name> */
574 static int xenbus_probe_frontend(const char *type, const char *name)
575 {
576 char *nodename;
577 int err;
579 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name);
580 if (!nodename)
581 return -ENOMEM;
583 DPRINTK("%s", nodename);
585 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
586 kfree(nodename);
587 return err;
588 }
590 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
591 {
592 int err = 0;
593 char **dir;
594 unsigned int dir_n = 0;
595 int i;
597 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
598 if (IS_ERR(dir))
599 return PTR_ERR(dir);
601 for (i = 0; i < dir_n; i++) {
602 err = bus->probe(type, dir[i]);
603 if (err)
604 break;
605 }
606 kfree(dir);
607 return err;
608 }
610 int xenbus_probe_devices(struct xen_bus_type *bus)
611 {
612 int err = 0;
613 char **dir;
614 unsigned int i, dir_n;
616 if (bus->error)
617 return bus->error;
619 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
620 if (IS_ERR(dir))
621 return PTR_ERR(dir);
623 for (i = 0; i < dir_n; i++) {
624 err = xenbus_probe_device_type(bus, dir[i]);
625 if (err)
626 break;
627 }
628 kfree(dir);
629 return err;
630 }
632 static unsigned int char_count(const char *str, char c)
633 {
634 unsigned int i, ret = 0;
636 for (i = 0; str[i]; i++)
637 if (str[i] == c)
638 ret++;
639 return ret;
640 }
642 static int strsep_len(const char *str, char c, unsigned int len)
643 {
644 unsigned int i;
646 for (i = 0; str[i]; i++)
647 if (str[i] == c) {
648 if (len == 0)
649 return i;
650 len--;
651 }
652 return (len == 0) ? i : -ERANGE;
653 }
655 void dev_changed(const char *node, struct xen_bus_type *bus)
656 {
657 int exists, rootlen;
658 struct xenbus_device *dev;
659 char type[BUS_ID_SIZE];
660 const char *p, *root;
662 if (bus->error || char_count(node, '/') < 2)
663 return;
665 exists = xenbus_exists(XBT_NIL, node, "");
666 if (!exists) {
667 xenbus_cleanup_devices(node, &bus->bus);
668 return;
669 }
671 /* backend/<type>/... or device/<type>/... */
672 p = strchr(node, '/') + 1;
673 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
674 type[BUS_ID_SIZE-1] = '\0';
676 rootlen = strsep_len(node, '/', bus->levels);
677 if (rootlen < 0)
678 return;
679 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
680 if (!root)
681 return;
683 dev = xenbus_device_find(root, &bus->bus);
684 if (!dev)
685 xenbus_probe_node(bus, type, root);
686 else
687 put_device(&dev->dev);
689 kfree(root);
690 }
692 static void frontend_changed(struct xenbus_watch *watch,
693 const char **vec, unsigned int len)
694 {
695 DPRINTK("");
697 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
698 }
700 /* We watch for devices appearing and vanishing. */
701 static struct xenbus_watch fe_watch = {
702 .node = "device",
703 .callback = frontend_changed,
704 };
706 static int suspend_dev(struct device *dev, void *data)
707 {
708 int err = 0;
709 struct xenbus_driver *drv;
710 struct xenbus_device *xdev;
712 DPRINTK("");
714 if (dev->driver == NULL)
715 return 0;
716 drv = to_xenbus_driver(dev->driver);
717 xdev = container_of(dev, struct xenbus_device, dev);
718 if (drv->suspend)
719 err = drv->suspend(xdev);
720 if (err)
721 printk(KERN_WARNING
722 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
723 return 0;
724 }
726 static int suspend_cancel_dev(struct device *dev, void *data)
727 {
728 int err = 0;
729 struct xenbus_driver *drv;
730 struct xenbus_device *xdev;
732 DPRINTK("");
734 if (dev->driver == NULL)
735 return 0;
736 drv = to_xenbus_driver(dev->driver);
737 xdev = container_of(dev, struct xenbus_device, dev);
738 if (drv->suspend_cancel)
739 err = drv->suspend_cancel(xdev);
740 if (err)
741 printk(KERN_WARNING
742 "xenbus: suspend_cancel %s failed: %i\n",
743 dev->bus_id, err);
744 return 0;
745 }
747 static int resume_dev(struct device *dev, void *data)
748 {
749 int err;
750 struct xenbus_driver *drv;
751 struct xenbus_device *xdev;
753 DPRINTK("");
755 if (dev->driver == NULL)
756 return 0;
758 drv = to_xenbus_driver(dev->driver);
759 xdev = container_of(dev, struct xenbus_device, dev);
761 err = talk_to_otherend(xdev);
762 if (err) {
763 printk(KERN_WARNING
764 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
765 dev->bus_id, err);
766 return err;
767 }
769 xdev->state = XenbusStateInitialising;
771 if (drv->resume) {
772 err = drv->resume(xdev);
773 if (err) {
774 printk(KERN_WARNING
775 "xenbus: resume %s failed: %i\n",
776 dev->bus_id, err);
777 return err;
778 }
779 }
781 err = watch_otherend(xdev);
782 if (err) {
783 printk(KERN_WARNING
784 "xenbus_probe: resume (watch_otherend) %s failed: "
785 "%d.\n", dev->bus_id, err);
786 return err;
787 }
789 return 0;
790 }
792 void xenbus_suspend(void)
793 {
794 DPRINTK("");
796 if (!xenbus_frontend.error)
797 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
798 xenbus_backend_suspend(suspend_dev);
799 xs_suspend();
800 }
801 EXPORT_SYMBOL_GPL(xenbus_suspend);
803 void xenbus_resume(void)
804 {
805 xb_init_comms();
806 xs_resume();
807 if (!xenbus_frontend.error)
808 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
809 xenbus_backend_resume(resume_dev);
810 }
811 EXPORT_SYMBOL_GPL(xenbus_resume);
813 void xenbus_suspend_cancel(void)
814 {
815 xs_suspend_cancel();
816 if (!xenbus_frontend.error)
817 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
818 xenbus_backend_resume(suspend_cancel_dev);
819 }
820 EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
822 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
823 int xenstored_ready = 0;
826 int register_xenstore_notifier(struct notifier_block *nb)
827 {
828 int ret = 0;
830 if (xenstored_ready > 0)
831 ret = nb->notifier_call(nb, 0, NULL);
832 else
833 blocking_notifier_chain_register(&xenstore_chain, nb);
835 return ret;
836 }
837 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
839 void unregister_xenstore_notifier(struct notifier_block *nb)
840 {
841 blocking_notifier_chain_unregister(&xenstore_chain, nb);
842 }
843 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
846 void xenbus_probe(void *unused)
847 {
848 BUG_ON((xenstored_ready <= 0));
850 /* Enumerate devices in xenstore and watch for changes. */
851 xenbus_probe_devices(&xenbus_frontend);
852 register_xenbus_watch(&fe_watch);
853 xenbus_backend_probe_and_watch();
855 /* Notify others that xenstore is up */
856 blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
857 }
860 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
861 static struct file_operations xsd_kva_fops;
862 static struct proc_dir_entry *xsd_kva_intf;
863 static struct proc_dir_entry *xsd_port_intf;
865 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
866 {
867 size_t size = vma->vm_end - vma->vm_start;
869 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
870 return -EINVAL;
872 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
873 size, vma->vm_page_prot))
874 return -EAGAIN;
876 return 0;
877 }
879 static int xsd_kva_read(char *page, char **start, off_t off,
880 int count, int *eof, void *data)
881 {
882 int len;
884 len = sprintf(page, "0x%p", xen_store_interface);
885 *eof = 1;
886 return len;
887 }
889 static int xsd_port_read(char *page, char **start, off_t off,
890 int count, int *eof, void *data)
891 {
892 int len;
894 len = sprintf(page, "%d", xen_store_evtchn);
895 *eof = 1;
896 return len;
897 }
898 #endif
900 static int xenbus_probe_init(void)
901 {
902 int err = 0;
903 unsigned long page = 0;
905 DPRINTK("");
907 if (!is_running_on_xen())
908 return -ENODEV;
910 /* Register ourselves with the kernel bus subsystem */
911 xenbus_frontend.error = bus_register(&xenbus_frontend.bus);
912 if (xenbus_frontend.error)
913 printk(KERN_WARNING
914 "XENBUS: Error registering frontend bus: %i\n",
915 xenbus_frontend.error);
916 xenbus_backend_bus_register();
918 /*
919 * Domain0 doesn't have a store_evtchn or store_mfn yet.
920 */
921 if (is_initial_xendomain()) {
922 struct evtchn_alloc_unbound alloc_unbound;
924 /* Allocate page. */
925 page = get_zeroed_page(GFP_KERNEL);
926 if (!page)
927 return -ENOMEM;
929 xen_store_mfn = xen_start_info->store_mfn =
930 pfn_to_mfn(virt_to_phys((void *)page) >>
931 PAGE_SHIFT);
933 /* Next allocate a local port which xenstored can bind to */
934 alloc_unbound.dom = DOMID_SELF;
935 alloc_unbound.remote_dom = 0;
937 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
938 &alloc_unbound);
939 if (err == -ENOSYS)
940 goto err;
941 BUG_ON(err);
942 xen_store_evtchn = xen_start_info->store_evtchn =
943 alloc_unbound.port;
945 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
946 /* And finally publish the above info in /proc/xen */
947 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
948 if (xsd_kva_intf) {
949 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
950 sizeof(xsd_kva_fops));
951 xsd_kva_fops.mmap = xsd_kva_mmap;
952 xsd_kva_intf->proc_fops = &xsd_kva_fops;
953 xsd_kva_intf->read_proc = xsd_kva_read;
954 }
955 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
956 if (xsd_port_intf)
957 xsd_port_intf->read_proc = xsd_port_read;
958 #endif
959 xen_store_interface = mfn_to_virt(xen_store_mfn);
960 } else {
961 xenstored_ready = 1;
962 #ifdef CONFIG_XEN
963 xen_store_evtchn = xen_start_info->store_evtchn;
964 xen_store_mfn = xen_start_info->store_mfn;
965 xen_store_interface = mfn_to_virt(xen_store_mfn);
966 #else
967 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
968 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
969 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
970 PAGE_SIZE);
971 #endif
972 }
975 xenbus_dev_init();
977 /* Initialize the interface to xenstore. */
978 err = xs_init();
979 if (err) {
980 printk(KERN_WARNING
981 "XENBUS: Error initializing xenstore comms: %i\n", err);
982 goto err;
983 }
985 /* Register ourselves with the kernel device subsystem */
986 if (!xenbus_frontend.error) {
987 xenbus_frontend.error = device_register(&xenbus_frontend.dev);
988 if (xenbus_frontend.error) {
989 bus_unregister(&xenbus_frontend.bus);
990 printk(KERN_WARNING
991 "XENBUS: Error registering frontend device: %i\n",
992 xenbus_frontend.error);
993 }
994 }
995 xenbus_backend_device_register();
997 if (!is_initial_xendomain())
998 xenbus_probe(NULL);
1000 return 0;
1002 err:
1003 if (page)
1004 free_page(page);
1006 /*
1007 * Do not unregister the xenbus front/backend buses here. The buses
1008 * must exist because front/backend drivers will use them when they are
1009 * registered.
1010 */
1012 return err;
1015 #ifdef CONFIG_XEN
1016 postcore_initcall(xenbus_probe_init);
1017 MODULE_LICENSE("Dual BSD/GPL");
1018 #else
1019 int xenbus_init(void)
1021 return xenbus_probe_init();
1023 #endif
1025 static int is_disconnected_device(struct device *dev, void *data)
1027 struct xenbus_device *xendev = to_xenbus_device(dev);
1028 struct device_driver *drv = data;
1029 struct xenbus_driver *xendrv;
1031 /*
1032 * A device with no driver will never connect. We care only about
1033 * devices which should currently be in the process of connecting.
1034 */
1035 if (!dev->driver)
1036 return 0;
1038 /* Is this search limited to a particular driver? */
1039 if (drv && (dev->driver != drv))
1040 return 0;
1042 xendrv = to_xenbus_driver(dev->driver);
1043 return (xendev->state < XenbusStateConnected ||
1044 (xendrv->is_ready && !xendrv->is_ready(xendev)));
1047 static int exists_disconnected_device(struct device_driver *drv)
1049 if (xenbus_frontend.error)
1050 return xenbus_frontend.error;
1051 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1052 is_disconnected_device);
1055 static int print_device_status(struct device *dev, void *data)
1057 struct xenbus_device *xendev = to_xenbus_device(dev);
1058 struct device_driver *drv = data;
1059 struct xenbus_driver *xendrv;
1061 /* Is this operation limited to a particular driver? */
1062 if (drv && (dev->driver != drv))
1063 return 0;
1065 if (!dev->driver) {
1066 /* Information only: is this too noisy? */
1067 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1068 xendev->nodename);
1069 return 0;
1072 if (xendev->state < XenbusStateConnected) {
1073 enum xenbus_state rstate = XenbusStateUnknown;
1074 if (xendev->otherend)
1075 rstate = xenbus_read_driver_state(xendev->otherend);
1076 printk(KERN_WARNING "XENBUS: Timeout connecting "
1077 "to device: %s (local state %d, remote state %d)\n",
1078 xendev->nodename, xendev->state, rstate);
1081 xendrv = to_xenbus_driver(dev->driver);
1082 if (xendrv->is_ready && !xendrv->is_ready(xendev))
1083 printk(KERN_WARNING "XENBUS: Device not ready: %s\n",
1084 xendev->nodename);
1086 return 0;
1089 /* We only wait for device setup after most initcalls have run. */
1090 static int ready_to_wait_for_devices;
1092 /*
1093 * On a 5-minute timeout, wait for all devices currently configured. We need
1094 * to do this to guarantee that the filesystems and / or network devices
1095 * needed for boot are available, before we can allow the boot to proceed.
1097 * This needs to be on a late_initcall, to happen after the frontend device
1098 * drivers have been initialised, but before the root fs is mounted.
1100 * A possible improvement here would be to have the tools add a per-device
1101 * flag to the store entry, indicating whether it is needed at boot time.
1102 * This would allow people who knew what they were doing to accelerate their
1103 * boot slightly, but of course needs tools or manual intervention to set up
1104 * those flags correctly.
1105 */
1106 static void wait_for_devices(struct xenbus_driver *xendrv)
1108 unsigned long start = jiffies;
1109 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1110 unsigned int seconds_waited = 0;
1112 if (!ready_to_wait_for_devices || !is_running_on_xen())
1113 return;
1115 while (exists_disconnected_device(drv)) {
1116 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
1117 if (!seconds_waited)
1118 printk(KERN_WARNING "XENBUS: Waiting for "
1119 "devices to initialise: ");
1120 seconds_waited += 5;
1121 printk("%us...", 300 - seconds_waited);
1122 if (seconds_waited == 300)
1123 break;
1126 schedule_timeout_interruptible(HZ/10);
1129 if (seconds_waited)
1130 printk("\n");
1132 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1133 print_device_status);
1136 #ifndef MODULE
1137 static int __init boot_wait_for_devices(void)
1139 if (!xenbus_frontend.error) {
1140 ready_to_wait_for_devices = 1;
1141 wait_for_devices(NULL);
1143 return 0;
1146 late_initcall(boot_wait_for_devices);
1147 #endif
1149 int xenbus_for_each_frontend(void *arg, int (*fn)(struct device *, void *))
1151 return bus_for_each_dev(&xenbus_frontend.bus, NULL, arg, fn);
1153 EXPORT_SYMBOL_GPL(xenbus_for_each_frontend);