- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / xenbus / xenbus_probe.c
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  */
33
34 #define DPRINTK(fmt, args...)                           \
35         pr_debug("xenbus_probe (%s:%d) " fmt ".\n",     \
36                  __FUNCTION__, __LINE__, ##args)
37
38 #include <linux/kernel.h>
39 #include <linux/version.h>
40 #include <linux/err.h>
41 #include <linux/string.h>
42 #include <linux/ctype.h>
43 #include <linux/fcntl.h>
44 #include <linux/mm.h>
45 #include <linux/sched.h>
46 #include <linux/proc_fs.h>
47 #include <linux/notifier.h>
48 #include <linux/mutex.h>
49 #include <linux/io.h>
50 #include <linux/slab.h>
51 #include <linux/module.h>
52
53 #include <asm/page.h>
54 #include <asm/pgtable.h>
55 #if defined(CONFIG_XEN) || defined(MODULE)
56 #include <asm/hypervisor.h>
57 #include <xen/xenbus.h>
58 #include <xen/xen_proc.h>
59 #include <xen/evtchn.h>
60 #include <xen/features.h>
61 #include <xen/gnttab.h>
62
63 #define PARAVIRT_EXPORT_SYMBOL(sym) __typeof__(sym) sym
64 #else
65 #include <asm/xen/hypervisor.h>
66
67 #include <xen/xen.h>
68 #include <xen/xenbus.h>
69 #include <xen/events.h>
70 #include <xen/page.h>
71
72 #define PARAVIRT_EXPORT_SYMBOL EXPORT_SYMBOL_GPL
73 #endif
74
75 #ifndef CONFIG_XEN
76 #include <xen/hvm.h>
77 #endif
78
79 #include "xenbus_comms.h"
80 #include "xenbus_probe.h"
81
82 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
83 #include <xen/platform-compat.h>
84 #endif
85
86 int xen_store_evtchn;
87 EXPORT_SYMBOL_GPL(xen_store_evtchn);
88
89 struct xenstore_domain_interface *xen_store_interface;
90 EXPORT_SYMBOL_GPL(xen_store_interface);
91
92 static unsigned long xen_store_mfn;
93
94 extern struct mutex xenwatch_mutex;
95
96 static
97 #ifdef CONFIG_XEN_UNPRIVILEGED_GUEST
98 __initdata
99 #endif
100 BLOCKING_NOTIFIER_HEAD(xenstore_chain);
101
102 #if defined(CONFIG_XEN) || defined(MODULE)
103 static void wait_for_devices(struct xenbus_driver *xendrv);
104 #endif
105
106 /* If something in array of ids matches this device, return it. */
107 static const struct xenbus_device_id *
108 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
109 {
110         for (; *arr->devicetype != '\0'; arr++) {
111                 if (!strcmp(arr->devicetype, dev->devicetype))
112                         return arr;
113         }
114         return NULL;
115 }
116
117 int xenbus_match(struct device *_dev, struct device_driver *_drv)
118 {
119         struct xenbus_driver *drv = to_xenbus_driver(_drv);
120
121         if (!drv->ids)
122                 return 0;
123
124         return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
125 }
126 PARAVIRT_EXPORT_SYMBOL(xenbus_match);
127
128
129 static void free_otherend_details(struct xenbus_device *dev)
130 {
131         kfree(dev->otherend);
132         dev->otherend = NULL;
133 }
134
135
136 static void free_otherend_watch(struct xenbus_device *dev)
137 {
138         if (dev->otherend_watch.node) {
139                 unregister_xenbus_watch(&dev->otherend_watch);
140                 kfree(dev->otherend_watch.node);
141                 dev->otherend_watch.node = NULL;
142         }
143 }
144
145
146 int xenbus_read_otherend_details(struct xenbus_device *xendev,
147                                  char *id_node, char *path_node)
148 {
149         int err = xenbus_gather(XBT_NIL, xendev->nodename,
150                                 id_node, "%i", &xendev->otherend_id,
151                                 path_node, NULL, &xendev->otherend,
152                                 NULL);
153         if (err) {
154                 xenbus_dev_fatal(xendev, err,
155                                  "reading other end details from %s",
156                                  xendev->nodename);
157                 return err;
158         }
159         if (strlen(xendev->otherend) == 0 ||
160             !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
161                 xenbus_dev_fatal(xendev, -ENOENT,
162                                  "unable to read other end from %s.  "
163                                  "missing or inaccessible.",
164                                  xendev->nodename);
165                 free_otherend_details(xendev);
166                 return -ENOENT;
167         }
168
169         return 0;
170 }
171 PARAVIRT_EXPORT_SYMBOL(xenbus_read_otherend_details);
172
173 #if defined(CONFIG_XEN) || defined(MODULE)
174
175 static int read_backend_details(struct xenbus_device *xendev)
176 {
177         return xenbus_read_otherend_details(xendev, "backend-id", "backend");
178 }
179
180 static void otherend_changed(struct xenbus_watch *watch,
181                              const char **vec, unsigned int len)
182 #else /* !CONFIG_XEN && !MODULE */
183 void xenbus_otherend_changed(struct xenbus_watch *watch,
184                              const char **vec, unsigned int len,
185                              int ignore_on_shutdown)
186 #endif /* CONFIG_XEN || MODULE */
187 {
188         struct xenbus_device *dev =
189                 container_of(watch, struct xenbus_device, otherend_watch);
190         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
191         enum xenbus_state state;
192
193         /* Protect us against watches firing on old details when the otherend
194            details change, say immediately after a resume. */
195         if (!dev->otherend ||
196             strncmp(dev->otherend, vec[XS_WATCH_PATH],
197                     strlen(dev->otherend))) {
198                 dev_dbg(&dev->dev, "Ignoring watch at %s", vec[XS_WATCH_PATH]);
199                 return;
200         }
201
202         state = xenbus_read_driver_state(dev->otherend);
203
204         dev_dbg(&dev->dev, "state is %d (%s), %s, %s",
205                 state, xenbus_strstate(state), dev->otherend_watch.node,
206                 vec[XS_WATCH_PATH]);
207
208 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
209         /*
210          * Ignore xenbus transitions during shutdown. This prevents us doing
211          * work that can fail e.g., when the rootfs is gone.
212          */
213         if (system_state > SYSTEM_RUNNING) {
214                 /* If we're frontend, drive the state machine to Closed. */
215                 /* This should cause the backend to release our resources. */
216 # if defined(CONFIG_XEN) || defined(MODULE)
217                 const struct xen_bus_type *bus =
218                         container_of(dev->dev.bus, struct xen_bus_type, bus);
219                 int ignore_on_shutdown = (bus->levels == 2);
220 # endif
221
222                 if (ignore_on_shutdown && (state == XenbusStateClosing))
223                         xenbus_frontend_closed(dev);
224                 return;
225         }
226 #endif
227
228         if (drv->otherend_changed)
229                 drv->otherend_changed(dev, state);
230 }
231 PARAVIRT_EXPORT_SYMBOL(xenbus_otherend_changed);
232
233
234 static int talk_to_otherend(struct xenbus_device *dev)
235 {
236         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
237
238         free_otherend_watch(dev);
239         free_otherend_details(dev);
240
241         return drv->read_otherend_details(dev);
242 }
243
244
245
246 static int watch_otherend(struct xenbus_device *dev)
247 {
248 #if defined(CONFIG_XEN) || defined(MODULE)
249         return xenbus_watch_path2(dev, dev->otherend, "state",
250                                   &dev->otherend_watch, otherend_changed);
251 #else
252         struct xen_bus_type *bus =
253                 container_of(dev->dev.bus, struct xen_bus_type, bus);
254
255         return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
256                                     bus->otherend_changed,
257                                     "%s/%s", dev->otherend, "state");
258 #endif
259 }
260
261
262 int xenbus_dev_probe(struct device *_dev)
263 {
264         struct xenbus_device *dev = to_xenbus_device(_dev);
265         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
266         const struct xenbus_device_id *id;
267         int err;
268
269         DPRINTK("%s", dev->nodename);
270
271         if (!drv->probe) {
272                 err = -ENODEV;
273                 goto fail;
274         }
275
276         id = match_device(drv->ids, dev);
277         if (!id) {
278                 err = -ENODEV;
279                 goto fail;
280         }
281
282         err = talk_to_otherend(dev);
283         if (err) {
284                 dev_warn(&dev->dev,
285                          "xenbus_probe: talk_to_otherend on %s failed.\n",
286                          dev->nodename);
287                 return err;
288         }
289
290         err = drv->probe(dev, id);
291         if (err)
292                 goto fail;
293
294         err = watch_otherend(dev);
295         if (err) {
296                 dev_warn(&dev->dev,
297                          "xenbus_probe: watch_otherend on %s failed.\n",
298                          dev->nodename);
299                 return err;
300         }
301
302         return 0;
303 fail:
304         xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
305         xenbus_switch_state(dev, XenbusStateClosed);
306 #if defined(CONFIG_XEN) || defined(MODULE)
307         return -ENODEV;
308 #else
309         return err;
310 #endif
311 }
312 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_probe);
313
314 int xenbus_dev_remove(struct device *_dev)
315 {
316         struct xenbus_device *dev = to_xenbus_device(_dev);
317         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
318
319         DPRINTK("%s", dev->nodename);
320
321         free_otherend_watch(dev);
322         free_otherend_details(dev);
323
324         if (drv->remove)
325                 drv->remove(dev);
326
327         xenbus_switch_state(dev, XenbusStateClosed);
328         return 0;
329 }
330 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_remove);
331
332 void xenbus_dev_shutdown(struct device *_dev)
333 {
334         struct xenbus_device *dev = to_xenbus_device(_dev);
335         unsigned long timeout = 5*HZ;
336
337         DPRINTK("%s", dev->nodename);
338
339 /* Commented out since xenstored stubdom is now minios based not linux based
340 #define XENSTORE_DOMAIN_SHARES_THIS_KERNEL
341 */
342 #ifndef XENSTORE_DOMAIN_SHARES_THIS_KERNEL
343         if (is_initial_xendomain())
344 #endif
345                 return;
346
347         get_device(&dev->dev);
348         if (dev->state != XenbusStateConnected) {
349                 dev_info(&dev->dev, "%s: %s: %s != Connected, skipping\n", __FUNCTION__,
350                          dev->nodename, xenbus_strstate(dev->state));
351                 goto out;
352         }
353         xenbus_switch_state(dev, XenbusStateClosing);
354
355         if (!strcmp(dev->devicetype, "vfb"))
356                 goto out;
357
358         timeout = wait_for_completion_timeout(&dev->down, timeout);
359         if (!timeout)
360                 dev_info(&dev->dev, "%s: %s timeout closing device\n",
361                          __FUNCTION__, dev->nodename);
362  out:
363         put_device(&dev->dev);
364 }
365 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_shutdown);
366
367 int xenbus_register_driver_common(struct xenbus_driver *drv,
368                                   struct xen_bus_type *bus)
369 {
370         int ret;
371
372         if (bus->error)
373                 return bus->error;
374
375         drv->driver.bus = &bus->bus;
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
377         drv->driver.probe = xenbus_dev_probe;
378         drv->driver.remove = xenbus_dev_remove;
379         drv->driver.shutdown = xenbus_dev_shutdown;
380 #endif
381
382         mutex_lock(&xenwatch_mutex);
383         ret = driver_register(&drv->driver);
384         mutex_unlock(&xenwatch_mutex);
385         return ret;
386 }
387 PARAVIRT_EXPORT_SYMBOL(xenbus_register_driver_common);
388
389 void xenbus_unregister_driver(struct xenbus_driver *drv)
390 {
391         driver_unregister(&drv->driver);
392 }
393 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
394
395 struct xb_find_info {
396         struct xenbus_device *dev;
397         const char *nodename;
398 };
399
400 static int cmp_dev(struct device *dev, void *data)
401 {
402         struct xenbus_device *xendev = to_xenbus_device(dev);
403         struct xb_find_info *info = data;
404
405         if (!strcmp(xendev->nodename, info->nodename)) {
406                 info->dev = xendev;
407                 get_device(dev);
408                 return 1;
409         }
410         return 0;
411 }
412
413 struct xenbus_device *xenbus_device_find(const char *nodename,
414                                          struct bus_type *bus)
415 {
416         struct xb_find_info info = { .dev = NULL, .nodename = nodename };
417
418         bus_for_each_dev(bus, NULL, &info, cmp_dev);
419         return info.dev;
420 }
421
422 static int cleanup_dev(struct device *dev, void *data)
423 {
424         struct xenbus_device *xendev = to_xenbus_device(dev);
425         struct xb_find_info *info = data;
426         int len = strlen(info->nodename);
427
428         DPRINTK("%s", info->nodename);
429
430         /* Match the info->nodename path, or any subdirectory of that path. */
431         if (strncmp(xendev->nodename, info->nodename, len))
432                 return 0;
433
434         /* If the node name is longer, ensure it really is a subdirectory. */
435         if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
436                 return 0;
437
438         info->dev = xendev;
439         get_device(dev);
440         return 1;
441 }
442
443 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
444 {
445         struct xb_find_info info = { .nodename = path };
446
447         do {
448                 info.dev = NULL;
449                 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
450                 if (info.dev) {
451                         device_unregister(&info.dev->dev);
452                         put_device(&info.dev->dev);
453                 }
454         } while (info.dev);
455 }
456
457 static void xenbus_dev_release(struct device *dev)
458 {
459         if (dev)
460                 kfree(to_xenbus_device(dev));
461 }
462
463 static ssize_t nodename_show(struct device *dev,
464 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
465                              struct device_attribute *attr,
466 #endif
467                              char *buf)
468 {
469         return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
470 }
471
472 static ssize_t devtype_show(struct device *dev,
473 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
474                             struct device_attribute *attr,
475 #endif
476                             char *buf)
477 {
478         return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
479 }
480
481 static ssize_t modalias_show(struct device *dev,
482 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
483                              struct device_attribute *attr,
484 #endif
485                              char *buf)
486 {
487         return sprintf(buf, "%s:%s\n", dev->bus->name,
488                        to_xenbus_device(dev)->devicetype);
489 }
490
491 struct device_attribute xenbus_dev_attrs[] = {
492         __ATTR_RO(nodename),
493         __ATTR_RO(devtype),
494         __ATTR_RO(modalias),
495         __ATTR_NULL
496 };
497 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_attrs);
498
499 int xenbus_probe_node(struct xen_bus_type *bus,
500                       const char *type,
501                       const char *nodename)
502 {
503         int err;
504         struct xenbus_device *xendev;
505         size_t stringlen;
506         char *tmpstring;
507
508         enum xenbus_state state = xenbus_read_driver_state(nodename);
509
510         if (bus->error)
511                 return bus->error;
512
513         if (state != XenbusStateInitialising) {
514                 /* Device is not new, so ignore it.  This can happen if a
515                    device is going away after switching to Closed.  */
516                 return 0;
517         }
518
519         stringlen = strlen(nodename) + 1 + strlen(type) + 1;
520         xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
521         if (!xendev)
522                 return -ENOMEM;
523
524         xendev->state = XenbusStateInitialising;
525
526         /* Copy the strings into the extra space. */
527
528         tmpstring = (char *)(xendev + 1);
529         strcpy(tmpstring, nodename);
530         xendev->nodename = tmpstring;
531
532         tmpstring += strlen(tmpstring) + 1;
533         strcpy(tmpstring, type);
534         xendev->devicetype = tmpstring;
535         init_completion(&xendev->down);
536
537 #if defined(CONFIG_XEN) || defined(MODULE)
538         xendev->dev.parent = &bus->dev;
539 #endif
540         xendev->dev.bus = &bus->bus;
541         xendev->dev.release = xenbus_dev_release;
542
543 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
544         {
545                 char devname[XEN_BUS_ID_SIZE];
546
547                 err = bus->get_bus_id(devname, xendev->nodename);
548                 if (!err)
549                         dev_set_name(&xendev->dev, devname);
550         }
551 #else
552         err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
553 #endif
554         if (err)
555                 goto fail;
556
557         /* Register with generic device framework. */
558         err = device_register(&xendev->dev);
559         if (err)
560                 goto fail;
561
562         return 0;
563 fail:
564         kfree(xendev);
565         return err;
566 }
567 PARAVIRT_EXPORT_SYMBOL(xenbus_probe_node);
568
569 #if defined(CONFIG_XEN) || defined(MODULE)
570
571 /* device/<type>/<id> => <type>-<id> */
572 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
573 {
574         nodename = strchr(nodename, '/');
575         if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
576                 pr_warning("XENBUS: bad frontend %s\n", nodename);
577                 return -EINVAL;
578         }
579
580         strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
581         if (!strchr(bus_id, '/')) {
582                 pr_warning("XENBUS: bus_id %s no slash\n", bus_id);
583                 return -EINVAL;
584         }
585         *strchr(bus_id, '/') = '-';
586         return 0;
587 }
588
589 /* device/<typename>/<name> */
590 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
591                                  const char *name)
592 {
593         char *nodename;
594         int err;
595
596         if (!strcmp(type, "console"))
597                 return 0;
598
599         nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
600         if (!nodename)
601                 return -ENOMEM;
602
603         DPRINTK("%s", nodename);
604
605         err = xenbus_probe_node(bus, type, nodename);
606         kfree(nodename);
607         return err;
608 }
609
610 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
611 static int xenbus_uevent_frontend(struct device *dev, struct kobj_uevent_env *env)
612 {
613         struct xenbus_device *xdev;
614
615         if (dev == NULL)
616                 return -ENODEV;
617         xdev = to_xenbus_device(dev);
618         if (xdev == NULL)
619                 return -ENODEV;
620
621         /* stuff we want to pass to /sbin/hotplug */
622         if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype) ||
623             add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename) ||
624             add_uevent_var(env, "MODALIAS=xen:%s", xdev->devicetype))
625                 return -ENOMEM;
626
627         return 0;
628 }
629 #endif
630
631 /* Bus type for frontend drivers. */
632 static struct xen_bus_type xenbus_frontend = {
633         .root = "device",
634         .levels = 2,            /* device/type/<id> */
635         .get_bus_id = frontend_bus_id,
636         .probe = xenbus_probe_frontend,
637         .error = -ENODEV,
638         .bus = {
639                 .name      = "xen",
640                 .match     = xenbus_match,
641 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
642                 .probe     = xenbus_dev_probe,
643                 .remove    = xenbus_dev_remove,
644                 .shutdown  = xenbus_dev_shutdown,
645                 .uevent    = xenbus_uevent_frontend,
646 #endif
647 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
648                 .dev_attrs = xenbus_dev_attrs,
649 #endif
650         },
651         .dev = {
652                 .init_name = "xen",
653         },
654 };
655
656 int xenbus_register_frontend(struct xenbus_driver *drv)
657 {
658         int ret;
659
660         drv->read_otherend_details = read_backend_details;
661
662         ret = xenbus_register_driver_common(drv, &xenbus_frontend);
663         if (ret)
664                 return ret;
665
666         /* If this driver is loaded as a module wait for devices to attach. */
667         wait_for_devices(drv);
668
669         return 0;
670 }
671 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
672
673 #endif
674
675 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
676 {
677         int err = 0;
678         char **dir;
679         unsigned int dir_n = 0;
680         int i;
681
682         dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
683         if (IS_ERR(dir))
684                 return PTR_ERR(dir);
685
686         for (i = 0; i < dir_n; i++) {
687                 err = bus->probe(bus, type, dir[i]);
688                 if (err)
689                         break;
690         }
691
692         kfree(dir);
693         return err;
694 }
695
696 int xenbus_probe_devices(struct xen_bus_type *bus)
697 {
698         int err = 0;
699         char **dir;
700         unsigned int i, dir_n;
701
702         if (bus->error)
703                 return bus->error;
704
705         dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
706         if (IS_ERR(dir))
707                 return PTR_ERR(dir);
708
709         for (i = 0; i < dir_n; i++) {
710                 err = xenbus_probe_device_type(bus, dir[i]);
711                 if (err)
712                         break;
713         }
714
715         kfree(dir);
716         return err;
717 }
718 PARAVIRT_EXPORT_SYMBOL(xenbus_probe_devices);
719
720 static unsigned int char_count(const char *str, char c)
721 {
722         unsigned int i, ret = 0;
723
724         for (i = 0; str[i]; i++)
725                 if (str[i] == c)
726                         ret++;
727         return ret;
728 }
729
730 static int strsep_len(const char *str, char c, unsigned int len)
731 {
732         unsigned int i;
733
734         for (i = 0; str[i]; i++)
735                 if (str[i] == c) {
736                         if (len == 0)
737                                 return i;
738                         len--;
739                 }
740         return (len == 0) ? i : -ERANGE;
741 }
742
743 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
744 {
745         int exists, rootlen;
746         struct xenbus_device *dev;
747         char type[XEN_BUS_ID_SIZE];
748         const char *p, *root;
749
750         if (bus->error || char_count(node, '/') < 2)
751                 return;
752
753         exists = xenbus_exists(XBT_NIL, node, "");
754         if (!exists) {
755                 xenbus_cleanup_devices(node, &bus->bus);
756                 return;
757         }
758
759         /* backend/<type>/... or device/<type>/... */
760         p = strchr(node, '/') + 1;
761         snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
762         type[XEN_BUS_ID_SIZE-1] = '\0';
763
764         rootlen = strsep_len(node, '/', bus->levels);
765         if (rootlen < 0)
766                 return;
767         root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
768         if (!root)
769                 return;
770
771         dev = xenbus_device_find(root, &bus->bus);
772         if (!dev)
773                 xenbus_probe_node(bus, type, root);
774         else
775                 put_device(&dev->dev);
776
777         kfree(root);
778 }
779 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_changed);
780
781 #if defined(CONFIG_XEN) || defined(MODULE)
782 static void frontend_changed(struct xenbus_watch *watch,
783                              const char **vec, unsigned int len)
784 {
785         DPRINTK("");
786
787         xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
788 }
789
790 /* We watch for devices appearing and vanishing. */
791 static struct xenbus_watch fe_watch = {
792         .node = "device",
793         .callback = frontend_changed,
794 };
795
796 static int suspend_dev(struct device *dev, void *data)
797 #else
798 int xenbus_dev_suspend(struct device *dev)
799 #endif
800 {
801         int err = 0;
802         struct xenbus_driver *drv;
803         struct xenbus_device *xdev
804                 = container_of(dev, struct xenbus_device, dev);
805
806         DPRINTK("%s", xdev->nodename);
807
808         if (dev->driver == NULL)
809                 return 0;
810         drv = to_xenbus_driver(dev->driver);
811         if (drv->suspend)
812                 err = drv->suspend(xdev);
813         if (err)
814                 pr_warning("xenbus: suspend %s failed: %i\n",
815                            dev_name(dev), err);
816         return 0;
817 }
818 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_suspend);
819
820 #if defined(CONFIG_XEN) || defined(MODULE)
821 static int suspend_cancel_dev(struct device *dev, void *data)
822 {
823         int err = 0;
824         struct xenbus_driver *drv;
825         struct xenbus_device *xdev;
826
827         DPRINTK("");
828
829         if (dev->driver == NULL)
830                 return 0;
831         drv = to_xenbus_driver(dev->driver);
832         xdev = container_of(dev, struct xenbus_device, dev);
833         if (drv->suspend_cancel)
834                 err = drv->suspend_cancel(xdev);
835         if (err)
836                 pr_warning("xenbus: suspend_cancel %s failed: %i\n",
837                            dev_name(dev), err);
838         return 0;
839 }
840
841 static int resume_dev(struct device *dev, void *data)
842 #else
843 int xenbus_dev_resume(struct device *dev)
844 #endif
845 {
846         int err;
847         struct xenbus_driver *drv;
848         struct xenbus_device *xdev
849                 = container_of(dev, struct xenbus_device, dev);
850
851         DPRINTK("%s", xdev->nodename);
852
853         if (dev->driver == NULL)
854                 return 0;
855         drv = to_xenbus_driver(dev->driver);
856         err = talk_to_otherend(xdev);
857         if (err) {
858                 pr_warning("xenbus: resume (talk_to_otherend) %s failed: %i\n",
859                            dev_name(dev), err);
860                 return err;
861         }
862
863         xdev->state = XenbusStateInitialising;
864
865         if (drv->resume) {
866                 err = drv->resume(xdev);
867                 if (err) {
868                         pr_warning("xenbus: resume %s failed: %i\n",
869                                    dev_name(dev), err);
870                         return err;
871                 }
872         }
873
874         err = watch_otherend(xdev);
875         if (err) {
876                 pr_warning("xenbus_probe: resume (watch_otherend) %s failed:"
877                            " %d\n", dev_name(dev), err);
878                 return err;
879         }
880
881         return 0;
882 }
883 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_resume);
884
885 #if !defined(CONFIG_XEN) && !defined(MODULE)
886 int xenbus_dev_cancel(struct device *dev)
887 {
888         /* Do nothing */
889         DPRINTK("cancel");
890         return 0;
891 }
892 PARAVIRT_EXPORT_SYMBOL(xenbus_dev_cancel);
893 #else
894 void xenbus_suspend(void)
895 {
896         DPRINTK("");
897
898         if (!xenbus_frontend.error)
899                 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
900         xenbus_backend_suspend(suspend_dev);
901         xs_suspend();
902 }
903
904 void xenbus_resume(void)
905 {
906         xb_init_comms();
907         xs_resume();
908         if (!xenbus_frontend.error)
909                 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
910         xenbus_backend_resume(resume_dev);
911 }
912
913 void xenbus_suspend_cancel(void)
914 {
915         xs_suspend_cancel();
916         if (!xenbus_frontend.error)
917                 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
918         xenbus_backend_resume(suspend_cancel_dev);
919 }
920 #endif
921
922 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
923 atomic_t xenbus_xsd_state = ATOMIC_INIT(XENBUS_XSD_UNCOMMITTED);
924
925
926 int
927 #ifdef CONFIG_XEN
928 __init
929 #endif
930 register_xenstore_notifier(struct notifier_block *nb)
931 {
932         int ret = 0;
933
934         if (is_xenstored_ready())
935                 ret = nb->notifier_call(nb, 0, NULL);
936         else
937                 blocking_notifier_chain_register(&xenstore_chain, nb);
938
939         return ret;
940 }
941 #ifndef CONFIG_XEN
942 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
943
944 void unregister_xenstore_notifier(struct notifier_block *nb)
945 {
946         blocking_notifier_chain_unregister(&xenstore_chain, nb);
947 }
948 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
949 #endif
950
951 #ifndef CONFIG_XEN
952 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
953 static int backend_state;
954
955 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
956                                         const char **v, unsigned int l)
957 {
958         if (xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state) != 1)
959                 backend_state = XenbusStateUnknown;
960         printk(KERN_DEBUG "XENBUS: backend %s %s\n",
961                         v[XS_WATCH_PATH], xenbus_strstate(backend_state));
962         wake_up(&backend_state_wq);
963 }
964
965 static void xenbus_reset_wait_for_backend(char *be, int expected)
966 {
967         long timeout;
968         timeout = wait_event_interruptible_timeout(backend_state_wq,
969                         backend_state == expected, 5 * HZ);
970         if (timeout <= 0)
971                 pr_info("XENBUS: backend %s timed out.\n", be);
972 }
973
974 /*
975  * Reset frontend if it is in Connected or Closed state.
976  * Wait for backend to catch up.
977  * State Connected happens during kdump, Closed after kexec.
978  */
979 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
980 {
981         struct xenbus_watch be_watch;
982
983         printk(KERN_DEBUG "XENBUS: backend %s %s\n",
984                         be, xenbus_strstate(be_state));
985
986         memset(&be_watch, 0, sizeof(be_watch));
987         be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
988         if (!be_watch.node)
989                 return;
990
991         be_watch.callback = xenbus_reset_backend_state_changed;
992         backend_state = XenbusStateUnknown;
993
994         pr_info("XENBUS: triggering reconnect on %s\n", be);
995         register_xenbus_watch(&be_watch);
996
997         /* fall through to forward backend to state XenbusStateInitialising */
998         switch (be_state) {
999         case XenbusStateConnected:
1000                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
1001                 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
1002
1003         case XenbusStateClosing:
1004                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
1005                 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
1006
1007         case XenbusStateClosed:
1008                 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
1009                 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
1010         }
1011
1012         unregister_xenbus_watch(&be_watch);
1013         pr_info("XENBUS: reconnect done on %s\n", be);
1014         kfree(be_watch.node);
1015 }
1016
1017 static void xenbus_check_frontend(char *class, char *dev)
1018 {
1019         int be_state, fe_state, err;
1020         char *backend, *frontend;
1021
1022         frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
1023         if (!frontend)
1024                 return;
1025
1026         err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
1027         if (err != 1)
1028                 goto out;
1029
1030         switch (fe_state) {
1031         case XenbusStateConnected:
1032         case XenbusStateClosed:
1033                 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
1034                                 frontend, xenbus_strstate(fe_state));
1035                 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
1036                 if (!backend || IS_ERR(backend))
1037                         goto out;
1038                 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
1039                 if (err == 1)
1040                         xenbus_reset_frontend(frontend, backend, be_state);
1041                 kfree(backend);
1042                 break;
1043         default:
1044                 break;
1045         }
1046 out:
1047         kfree(frontend);
1048 }
1049
1050 static void xenbus_reset_state(void)
1051 {
1052         char **devclass, **dev;
1053         int devclass_n, dev_n;
1054         int i, j;
1055
1056         devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
1057         if (IS_ERR(devclass))
1058                 return;
1059
1060         for (i = 0; i < devclass_n; i++) {
1061                 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
1062                 if (IS_ERR(dev))
1063                         continue;
1064                 for (j = 0; j < dev_n; j++)
1065                         xenbus_check_frontend(devclass[i], dev[j]);
1066                 kfree(dev);
1067         }
1068         kfree(devclass);
1069 }
1070 #endif
1071
1072 void
1073 #if defined(CONFIG_XEN_UNPRIVILEGED_GUEST)
1074 __init
1075 #elif defined(MODULE)
1076 __devinit
1077 #endif
1078 xenbus_probe(struct work_struct *unused)
1079 {
1080         BUG_ON(!is_xenstored_ready());
1081
1082 #ifndef CONFIG_XEN
1083         /* reset devices in Connected or Closed state */
1084         xenbus_reset_state();
1085 #endif
1086
1087 #if defined(CONFIG_XEN) || defined(MODULE)
1088         /* Enumerate devices in xenstore and watch for changes. */
1089         xenbus_probe_devices(&xenbus_frontend);
1090         register_xenbus_watch(&fe_watch);
1091         xenbus_backend_probe_and_watch();
1092 #endif
1093
1094         /* Notify others that xenstore is up */
1095         blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
1096 }
1097 PARAVIRT_EXPORT_SYMBOL(xenbus_probe);
1098
1099 #if !defined(CONFIG_XEN) && !defined(MODULE)
1100 static int __init xenbus_probe_initcall(void)
1101 {
1102         if (!xen_domain())
1103                 return -ENODEV;
1104
1105         if (xen_initial_domain() || xen_hvm_domain())
1106                 return 0;
1107
1108         xenbus_probe(NULL);
1109         return 0;
1110 }
1111
1112 device_initcall(xenbus_probe_initcall);
1113 #endif
1114
1115 #ifdef CONFIG_XEN_PRIVILEGED_GUEST
1116 #ifdef CONFIG_PROC_FS
1117 static struct file_operations xsd_kva_fops;
1118 static struct proc_dir_entry *xsd_kva_intf;
1119 static struct proc_dir_entry *xsd_port_intf;
1120
1121 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
1122 {
1123         size_t size = vma->vm_end - vma->vm_start;
1124         int old;
1125         int rc;
1126
1127         old = atomic_cmpxchg(&xenbus_xsd_state,
1128                            XENBUS_XSD_UNCOMMITTED,
1129                            XENBUS_XSD_LOCAL_INIT);
1130         switch (old) {
1131                 case XENBUS_XSD_UNCOMMITTED:
1132                         rc = xb_init_comms();
1133                         if (rc != 0)
1134                                 return rc;
1135                         break;
1136
1137                 case XENBUS_XSD_FOREIGN_INIT:
1138                 case XENBUS_XSD_FOREIGN_READY:
1139                         return -EBUSY;
1140
1141                 case XENBUS_XSD_LOCAL_INIT:
1142                 case XENBUS_XSD_LOCAL_READY:
1143                 default:
1144                         break;
1145         }
1146
1147         if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
1148                 return -EINVAL;
1149
1150         if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
1151                             size, vma->vm_page_prot))
1152                 return -EAGAIN;
1153
1154         return 0;
1155 }
1156
1157 static int xsd_kva_read(char *page, char **start, off_t off,
1158                         int count, int *eof, void *data)
1159 {
1160         int len;
1161
1162         len  = sprintf(page, "0x%p", xen_store_interface);
1163         *eof = 1;
1164         return len;
1165 }
1166
1167 static int xsd_port_read(char *page, char **start, off_t off,
1168                          int count, int *eof, void *data)
1169 {
1170         int len;
1171
1172         len  = sprintf(page, "%d", xen_store_evtchn);
1173         *eof = 1;
1174         return len;
1175 }
1176 #endif
1177
1178 #ifdef CONFIG_XEN_XENBUS_DEV
1179 int xenbus_conn(domid_t remote_dom, grant_ref_t *grant_ref,
1180                 evtchn_port_t *local_port)
1181 {
1182         struct evtchn_alloc_unbound alloc_unbound;
1183         int rc, rc2;
1184
1185         BUG_ON(atomic_read(&xenbus_xsd_state) != XENBUS_XSD_FOREIGN_INIT);
1186         BUG_ON(!is_initial_xendomain());
1187
1188         remove_xen_proc_entry("xsd_kva");
1189         remove_xen_proc_entry("xsd_port");
1190
1191         rc = close_evtchn(xen_store_evtchn);
1192         if (rc != 0)
1193                 goto fail0;
1194
1195         alloc_unbound.dom = DOMID_SELF;
1196         alloc_unbound.remote_dom = remote_dom;
1197         rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1198                                          &alloc_unbound);
1199         if (rc != 0)
1200                 goto fail0;
1201         *local_port = xen_store_evtchn = alloc_unbound.port;
1202
1203         /* keep the old page (xen_store_mfn, xen_store_interface) */
1204         rc = gnttab_grant_foreign_access(remote_dom, xen_store_mfn,
1205                                          GTF_permit_access);
1206         if (rc < 0)
1207                 goto fail1;
1208         *grant_ref = rc;
1209
1210         rc = xb_init_comms();
1211         if (rc != 0)
1212                 goto fail1;
1213
1214         return 0;
1215
1216 fail1:
1217         rc2 = close_evtchn(xen_store_evtchn);
1218         if (rc2 != 0)
1219                 pr_warning("XENBUS: Error freeing xenstore event channel:"
1220                            " %d\n", rc2);
1221 fail0:
1222         xen_store_evtchn = -1;
1223         return rc;
1224 }
1225 #endif
1226 #endif /* CONFIG_XEN_PRIVILEGED_GUEST */
1227
1228 /* Set up event channel for xenstored which is run as a local process
1229  * (this is normally used only in dom0)
1230  */
1231 static int __init xenstored_local_init(void)
1232 {
1233         int err = 0;
1234         unsigned long page = 0;
1235         struct evtchn_alloc_unbound alloc_unbound;
1236
1237         /* Allocate Xenstore page */
1238         page = get_zeroed_page(GFP_KERNEL);
1239         if (!page)
1240                 goto out_err;
1241
1242         xen_store_mfn = xen_start_info->store_mfn =
1243                 pfn_to_mfn(virt_to_phys((void *)page) >>
1244                            PAGE_SHIFT);
1245
1246         /* Next allocate a local port which xenstored can bind to */
1247         alloc_unbound.dom        = DOMID_SELF;
1248         alloc_unbound.remote_dom = DOMID_SELF;
1249
1250         err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
1251                                           &alloc_unbound);
1252         if (err == -ENOSYS)
1253                 goto out_err;
1254
1255         BUG_ON(err);
1256         xen_store_evtchn = xen_start_info->store_evtchn =
1257                 alloc_unbound.port;
1258
1259         return 0;
1260
1261  out_err:
1262         if (page != 0)
1263                 free_page(page);
1264         return err;
1265 }
1266
1267 #ifndef MODULE
1268 static int __init
1269 #else
1270 int __devinit
1271 #endif
1272 xenbus_init(void)
1273 {
1274         int err = 0;
1275
1276         DPRINTK("");
1277
1278         if (!is_running_on_xen())
1279                 return -ENODEV;
1280
1281 #if defined(CONFIG_XEN) || defined(MODULE)
1282         /* Register ourselves with the kernel bus subsystem */
1283         xenbus_frontend.error = bus_register(&xenbus_frontend.bus);
1284         if (xenbus_frontend.error)
1285                 pr_warning("XENBUS: Error registering frontend bus: %i\n",
1286                            xenbus_frontend.error);
1287         xenbus_backend_bus_register();
1288
1289         /*
1290          * Domain0 doesn't have a store_evtchn or store_mfn yet.
1291          */
1292         if (is_initial_xendomain()) {
1293                 err = xenstored_local_init();
1294                 if (err)
1295                         goto out_error;
1296
1297 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
1298                 /* And finally publish the above info in /proc/xen */
1299                 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1300                 if (xsd_kva_intf) {
1301                         memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1302                                sizeof(xsd_kva_fops));
1303                         xsd_kva_fops.mmap = xsd_kva_mmap;
1304                         xsd_kva_intf->proc_fops = &xsd_kva_fops;
1305                         xsd_kva_intf->read_proc = xsd_kva_read;
1306                 }
1307                 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1308                 if (xsd_port_intf)
1309                         xsd_port_intf->read_proc = xsd_port_read;
1310 #endif
1311                 xen_store_interface = mfn_to_virt(xen_store_mfn);
1312         } else {
1313 #ifndef CONFIG_XEN
1314                 uint64_t v = 0;
1315
1316                 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
1317                 if (err)
1318                         goto out_error;
1319                 xen_store_evtchn = (int)v;
1320                 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
1321                 if (err)
1322                         goto out_error;
1323                 xen_store_mfn = (unsigned long)v;
1324                 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
1325                                               PAGE_SIZE);
1326 #endif
1327 #ifndef MODULE
1328                 xen_store_evtchn = xen_start_info->store_evtchn;
1329                 xen_store_mfn = xen_start_info->store_mfn;
1330                 xen_store_interface = mfn_to_virt(xen_store_mfn);
1331 #endif
1332                 atomic_set(&xenbus_xsd_state, XENBUS_XSD_FOREIGN_READY);
1333
1334                 /* Initialize the shared memory rings to talk to xenstored */
1335                 err = xb_init_comms();
1336                 if (err)
1337                         goto out_error;
1338         }
1339
1340         xenbus_dev_init();
1341 #else /* !defined(CONFIG_XEN) && !defined(MODULE) */
1342         xenbus_ring_ops_init();
1343
1344         if (xen_hvm_domain()) {
1345                 uint64_t v = 0;
1346                 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
1347                 if (err)
1348                         goto out_error;
1349                 xen_store_evtchn = (int)v;
1350                 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
1351                 if (err)
1352                         goto out_error;
1353                 xen_store_mfn = (unsigned long)v;
1354                 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
1355         } else {
1356                 xen_store_evtchn = xen_start_info->store_evtchn;
1357                 xen_store_mfn = xen_start_info->store_mfn;
1358                 if (xen_store_evtchn)
1359                         atomic_set(&xenbus_xsd_state, XENBUS_XSD_FOREIGN_READY);
1360                 else {
1361                         err = xenstored_local_init();
1362                         if (err)
1363                                 goto out_error;
1364                 }
1365                 xen_store_interface = mfn_to_virt(xen_store_mfn);
1366         }
1367 #endif
1368
1369         /* Initialize the interface to xenstore. */
1370         err = xs_init();
1371         if (err) {
1372                 pr_warning("XENBUS: Error initializing xenstore comms: %i\n",
1373                            err);
1374                 goto out_error;
1375         }
1376
1377 #if defined(CONFIG_XEN) || defined(MODULE)
1378         /* Register ourselves with the kernel device subsystem */
1379         if (!xenbus_frontend.error) {
1380                 xenbus_frontend.error = device_register(&xenbus_frontend.dev);
1381                 if (xenbus_frontend.error) {
1382                         bus_unregister(&xenbus_frontend.bus);
1383                         pr_warning("XENBUS: Error registering frontend device:"
1384                                    " %d\n", xenbus_frontend.error);
1385                 }
1386         }
1387         xenbus_backend_device_register();
1388
1389         if (!is_initial_xendomain())
1390                 xenbus_probe(NULL);
1391 #endif
1392
1393 #if defined(CONFIG_XEN_COMPAT_XENFS) && !defined(MODULE)
1394         /*
1395          * Create xenfs mountpoint in /proc for compatibility with
1396          * utilities that expect to find "xenbus" under "/proc/xen".
1397          */
1398         proc_mkdir("xen", NULL);
1399 #endif
1400
1401         return 0;
1402
1403 out_error:
1404         /*
1405          * Do not unregister the xenbus front/backend buses here. The buses
1406          * must exist because front/backend drivers will use them when they are
1407          * registered.
1408          */
1409         return err;
1410 }
1411
1412 #ifndef MODULE
1413 postcore_initcall(xenbus_init);
1414 #ifdef CONFIG_XEN
1415 MODULE_LICENSE("Dual BSD/GPL");
1416 #else
1417 MODULE_LICENSE("GPL");
1418 #endif
1419 #endif
1420
1421 #if defined(CONFIG_XEN) || defined(MODULE)
1422
1423 static int is_device_connecting(struct device *dev, void *data)
1424 {
1425         struct xenbus_device *xendev = to_xenbus_device(dev);
1426         struct device_driver *drv = data;
1427         struct xenbus_driver *xendrv;
1428
1429         /*
1430          * A device with no driver will never connect. We care only about
1431          * devices which should currently be in the process of connecting.
1432          */
1433         if (!dev->driver)
1434                 return 0;
1435
1436         /* Is this search limited to a particular driver? */
1437         if (drv && (dev->driver != drv))
1438                 return 0;
1439
1440         xendrv = to_xenbus_driver(dev->driver);
1441         return (xendev->state < XenbusStateConnected ||
1442                 (xendev->state == XenbusStateConnected &&
1443                  xendrv->is_ready && !xendrv->is_ready(xendev)));
1444 }
1445
1446 static int exists_connecting_device(struct device_driver *drv)
1447 {
1448         if (xenbus_frontend.error)
1449                 return xenbus_frontend.error;
1450         return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1451                                 is_device_connecting);
1452 }
1453
1454 static int print_device_status(struct device *dev, void *data)
1455 {
1456         struct xenbus_device *xendev = to_xenbus_device(dev);
1457         struct device_driver *drv = data;
1458         struct xenbus_driver *xendrv;
1459
1460         /* Is this operation limited to a particular driver? */
1461         if (drv && (dev->driver != drv))
1462                 return 0;
1463
1464         if (!dev->driver) {
1465                 /* Information only: is this too noisy? */
1466                 pr_info("XENBUS: Device with no driver: %s\n",
1467                         xendev->nodename);
1468                 return 0;
1469         }
1470
1471         if (xendev->state < XenbusStateConnected) {
1472                 enum xenbus_state rstate = XenbusStateUnknown;
1473                 if (xendev->otherend)
1474                         rstate = xenbus_read_driver_state(xendev->otherend);
1475                 pr_warning("XENBUS: Timeout connecting to device: %s"
1476                            " (local state %d, remote state %d)\n",
1477                            xendev->nodename, xendev->state, rstate);
1478         }
1479
1480         xendrv = to_xenbus_driver(dev->driver);
1481         if (xendrv->is_ready && !xendrv->is_ready(xendev))
1482                 pr_warning("XENBUS: Device not ready: %s\n",
1483                            xendev->nodename);
1484
1485         return 0;
1486 }
1487
1488 /* We only wait for device setup after most initcalls have run. */
1489 static int ready_to_wait_for_devices;
1490
1491 /*
1492  * On a 5-minute timeout, wait for all devices currently configured.  We need
1493  * to do this to guarantee that the filesystems and / or network devices
1494  * needed for boot are available, before we can allow the boot to proceed.
1495  *
1496  * This needs to be on a late_initcall, to happen after the frontend device
1497  * drivers have been initialised, but before the root fs is mounted.
1498  *
1499  * A possible improvement here would be to have the tools add a per-device
1500  * flag to the store entry, indicating whether it is needed at boot time.
1501  * This would allow people who knew what they were doing to accelerate their
1502  * boot slightly, but of course needs tools or manual intervention to set up
1503  * those flags correctly.
1504  */
1505 static void wait_for_devices(struct xenbus_driver *xendrv)
1506 {
1507         unsigned long start = jiffies;
1508         struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1509         unsigned int seconds_waited = 0;
1510
1511         if (!ready_to_wait_for_devices || !is_running_on_xen())
1512                 return;
1513
1514         while (exists_connecting_device(drv)) {
1515                 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
1516                         if (!seconds_waited)
1517                                 pr_warning("XENBUS: Waiting for "
1518                                            "devices to initialise: ");
1519                         seconds_waited += 5;
1520                         printk("%us...", 300 - seconds_waited);
1521                         if (seconds_waited == 300)
1522                                 break;
1523                 }
1524
1525                 schedule_timeout_interruptible(HZ/10);
1526         }
1527
1528         if (seconds_waited)
1529                 printk("\n");
1530
1531         bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1532                          print_device_status);
1533 }
1534
1535 #ifndef MODULE
1536 static int __init boot_wait_for_devices(void)
1537 {
1538 #if !defined(CONFIG_XEN) && !defined(MODULE)
1539         if (xen_hvm_domain() && !xen_platform_pci_unplug)
1540                 return -ENODEV;
1541 #endif
1542
1543         if (!xenbus_frontend.error) {
1544                 ready_to_wait_for_devices = 1;
1545                 wait_for_devices(NULL);
1546         }
1547         return 0;
1548 }
1549
1550 late_initcall(boot_wait_for_devices);
1551 #endif
1552
1553 int xenbus_for_each_frontend(void *arg, int (*fn)(struct device *, void *))
1554 {
1555         return bus_for_each_dev(&xenbus_frontend.bus, NULL, arg, fn);
1556 }
1557 EXPORT_SYMBOL_GPL(xenbus_for_each_frontend);
1558
1559 #endif /* CONFIG_XEN || MODULE */