]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/xen/xenbus/xenbus_probe.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-2.6-nommu
[linux-2.6-omap-h63xx.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  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #define DPRINTK(fmt, args...)                           \
34         pr_debug("xenbus_probe (%s:%d) " fmt ".\n",     \
35                  __func__, __LINE__, ##args)
36
37 #include <linux/kernel.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/ctype.h>
41 #include <linux/fcntl.h>
42 #include <linux/mm.h>
43 #include <linux/proc_fs.h>
44 #include <linux/notifier.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
47 #include <linux/io.h>
48
49 #include <asm/page.h>
50 #include <asm/pgtable.h>
51 #include <asm/xen/hypervisor.h>
52 #include <xen/xenbus.h>
53 #include <xen/events.h>
54 #include <xen/page.h>
55
56 #include "xenbus_comms.h"
57 #include "xenbus_probe.h"
58
59
60 int xen_store_evtchn;
61 EXPORT_SYMBOL(xen_store_evtchn);
62
63 struct xenstore_domain_interface *xen_store_interface;
64 static unsigned long xen_store_mfn;
65
66 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
67
68 static void wait_for_devices(struct xenbus_driver *xendrv);
69
70 static int xenbus_probe_frontend(const char *type, const char *name);
71
72 static void xenbus_dev_shutdown(struct device *_dev);
73
74 /* If something in array of ids matches this device, return it. */
75 static const struct xenbus_device_id *
76 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
77 {
78         for (; *arr->devicetype != '\0'; arr++) {
79                 if (!strcmp(arr->devicetype, dev->devicetype))
80                         return arr;
81         }
82         return NULL;
83 }
84
85 int xenbus_match(struct device *_dev, struct device_driver *_drv)
86 {
87         struct xenbus_driver *drv = to_xenbus_driver(_drv);
88
89         if (!drv->ids)
90                 return 0;
91
92         return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
93 }
94
95 static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env)
96 {
97         struct xenbus_device *dev = to_xenbus_device(_dev);
98
99         if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
100                 return -ENOMEM;
101
102         return 0;
103 }
104
105 /* device/<type>/<id> => <type>-<id> */
106 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
107 {
108         nodename = strchr(nodename, '/');
109         if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
110                 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
111                 return -EINVAL;
112         }
113
114         strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
115         if (!strchr(bus_id, '/')) {
116                 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
117                 return -EINVAL;
118         }
119         *strchr(bus_id, '/') = '-';
120         return 0;
121 }
122
123
124 static void free_otherend_details(struct xenbus_device *dev)
125 {
126         kfree(dev->otherend);
127         dev->otherend = NULL;
128 }
129
130
131 static void free_otherend_watch(struct xenbus_device *dev)
132 {
133         if (dev->otherend_watch.node) {
134                 unregister_xenbus_watch(&dev->otherend_watch);
135                 kfree(dev->otherend_watch.node);
136                 dev->otherend_watch.node = NULL;
137         }
138 }
139
140
141 int read_otherend_details(struct xenbus_device *xendev,
142                                  char *id_node, char *path_node)
143 {
144         int err = xenbus_gather(XBT_NIL, xendev->nodename,
145                                 id_node, "%i", &xendev->otherend_id,
146                                 path_node, NULL, &xendev->otherend,
147                                 NULL);
148         if (err) {
149                 xenbus_dev_fatal(xendev, err,
150                                  "reading other end details from %s",
151                                  xendev->nodename);
152                 return err;
153         }
154         if (strlen(xendev->otherend) == 0 ||
155             !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
156                 xenbus_dev_fatal(xendev, -ENOENT,
157                                  "unable to read other end from %s.  "
158                                  "missing or inaccessible.",
159                                  xendev->nodename);
160                 free_otherend_details(xendev);
161                 return -ENOENT;
162         }
163
164         return 0;
165 }
166
167
168 static int read_backend_details(struct xenbus_device *xendev)
169 {
170         return read_otherend_details(xendev, "backend-id", "backend");
171 }
172
173 static struct device_attribute xenbus_dev_attrs[] = {
174         __ATTR_NULL
175 };
176
177 /* Bus type for frontend drivers. */
178 static struct xen_bus_type xenbus_frontend = {
179         .root = "device",
180         .levels = 2,            /* device/type/<id> */
181         .get_bus_id = frontend_bus_id,
182         .probe = xenbus_probe_frontend,
183         .bus = {
184                 .name      = "xen",
185                 .match     = xenbus_match,
186                 .uevent    = xenbus_uevent,
187                 .probe     = xenbus_dev_probe,
188                 .remove    = xenbus_dev_remove,
189                 .shutdown  = xenbus_dev_shutdown,
190                 .dev_attrs = xenbus_dev_attrs,
191         },
192 };
193
194 static void otherend_changed(struct xenbus_watch *watch,
195                              const char **vec, unsigned int len)
196 {
197         struct xenbus_device *dev =
198                 container_of(watch, struct xenbus_device, otherend_watch);
199         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
200         enum xenbus_state state;
201
202         /* Protect us against watches firing on old details when the otherend
203            details change, say immediately after a resume. */
204         if (!dev->otherend ||
205             strncmp(dev->otherend, vec[XS_WATCH_PATH],
206                     strlen(dev->otherend))) {
207                 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
208                         vec[XS_WATCH_PATH]);
209                 return;
210         }
211
212         state = xenbus_read_driver_state(dev->otherend);
213
214         dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
215                 state, xenbus_strstate(state), dev->otherend_watch.node,
216                 vec[XS_WATCH_PATH]);
217
218         /*
219          * Ignore xenbus transitions during shutdown. This prevents us doing
220          * work that can fail e.g., when the rootfs is gone.
221          */
222         if (system_state > SYSTEM_RUNNING) {
223                 struct xen_bus_type *bus = bus;
224                 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
225                 /* If we're frontend, drive the state machine to Closed. */
226                 /* This should cause the backend to release our resources. */
227                 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
228                         xenbus_frontend_closed(dev);
229                 return;
230         }
231
232         if (drv->otherend_changed)
233                 drv->otherend_changed(dev, state);
234 }
235
236
237 static int talk_to_otherend(struct xenbus_device *dev)
238 {
239         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
240
241         free_otherend_watch(dev);
242         free_otherend_details(dev);
243
244         return drv->read_otherend_details(dev);
245 }
246
247
248 static int watch_otherend(struct xenbus_device *dev)
249 {
250         return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
251                                     "%s/%s", dev->otherend, "state");
252 }
253
254
255 int xenbus_dev_probe(struct device *_dev)
256 {
257         struct xenbus_device *dev = to_xenbus_device(_dev);
258         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
259         const struct xenbus_device_id *id;
260         int err;
261
262         DPRINTK("%s", dev->nodename);
263
264         if (!drv->probe) {
265                 err = -ENODEV;
266                 goto fail;
267         }
268
269         id = match_device(drv->ids, dev);
270         if (!id) {
271                 err = -ENODEV;
272                 goto fail;
273         }
274
275         err = talk_to_otherend(dev);
276         if (err) {
277                 dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
278                          dev->nodename);
279                 return err;
280         }
281
282         err = drv->probe(dev, id);
283         if (err)
284                 goto fail;
285
286         err = watch_otherend(dev);
287         if (err) {
288                 dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
289                        dev->nodename);
290                 return err;
291         }
292
293         return 0;
294 fail:
295         xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
296         xenbus_switch_state(dev, XenbusStateClosed);
297         return -ENODEV;
298 }
299
300 int xenbus_dev_remove(struct device *_dev)
301 {
302         struct xenbus_device *dev = to_xenbus_device(_dev);
303         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
304
305         DPRINTK("%s", dev->nodename);
306
307         free_otherend_watch(dev);
308         free_otherend_details(dev);
309
310         if (drv->remove)
311                 drv->remove(dev);
312
313         xenbus_switch_state(dev, XenbusStateClosed);
314         return 0;
315 }
316
317 static void xenbus_dev_shutdown(struct device *_dev)
318 {
319         struct xenbus_device *dev = to_xenbus_device(_dev);
320         unsigned long timeout = 5*HZ;
321
322         DPRINTK("%s", dev->nodename);
323
324         get_device(&dev->dev);
325         if (dev->state != XenbusStateConnected) {
326                 printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
327                        dev->nodename, xenbus_strstate(dev->state));
328                 goto out;
329         }
330         xenbus_switch_state(dev, XenbusStateClosing);
331         timeout = wait_for_completion_timeout(&dev->down, timeout);
332         if (!timeout)
333                 printk(KERN_INFO "%s: %s timeout closing device\n",
334                        __func__, dev->nodename);
335  out:
336         put_device(&dev->dev);
337 }
338
339 int xenbus_register_driver_common(struct xenbus_driver *drv,
340                                   struct xen_bus_type *bus,
341                                   struct module *owner,
342                                   const char *mod_name)
343 {
344         drv->driver.name = drv->name;
345         drv->driver.bus = &bus->bus;
346         drv->driver.owner = owner;
347         drv->driver.mod_name = mod_name;
348
349         return driver_register(&drv->driver);
350 }
351
352 int __xenbus_register_frontend(struct xenbus_driver *drv,
353                                struct module *owner, const char *mod_name)
354 {
355         int ret;
356
357         drv->read_otherend_details = read_backend_details;
358
359         ret = xenbus_register_driver_common(drv, &xenbus_frontend,
360                                             owner, mod_name);
361         if (ret)
362                 return ret;
363
364         /* If this driver is loaded as a module wait for devices to attach. */
365         wait_for_devices(drv);
366
367         return 0;
368 }
369 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
370
371 void xenbus_unregister_driver(struct xenbus_driver *drv)
372 {
373         driver_unregister(&drv->driver);
374 }
375 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
376
377 struct xb_find_info
378 {
379         struct xenbus_device *dev;
380         const char *nodename;
381 };
382
383 static int cmp_dev(struct device *dev, void *data)
384 {
385         struct xenbus_device *xendev = to_xenbus_device(dev);
386         struct xb_find_info *info = data;
387
388         if (!strcmp(xendev->nodename, info->nodename)) {
389                 info->dev = xendev;
390                 get_device(dev);
391                 return 1;
392         }
393         return 0;
394 }
395
396 struct xenbus_device *xenbus_device_find(const char *nodename,
397                                          struct bus_type *bus)
398 {
399         struct xb_find_info info = { .dev = NULL, .nodename = nodename };
400
401         bus_for_each_dev(bus, NULL, &info, cmp_dev);
402         return info.dev;
403 }
404
405 static int cleanup_dev(struct device *dev, void *data)
406 {
407         struct xenbus_device *xendev = to_xenbus_device(dev);
408         struct xb_find_info *info = data;
409         int len = strlen(info->nodename);
410
411         DPRINTK("%s", info->nodename);
412
413         /* Match the info->nodename path, or any subdirectory of that path. */
414         if (strncmp(xendev->nodename, info->nodename, len))
415                 return 0;
416
417         /* If the node name is longer, ensure it really is a subdirectory. */
418         if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
419                 return 0;
420
421         info->dev = xendev;
422         get_device(dev);
423         return 1;
424 }
425
426 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
427 {
428         struct xb_find_info info = { .nodename = path };
429
430         do {
431                 info.dev = NULL;
432                 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
433                 if (info.dev) {
434                         device_unregister(&info.dev->dev);
435                         put_device(&info.dev->dev);
436                 }
437         } while (info.dev);
438 }
439
440 static void xenbus_dev_release(struct device *dev)
441 {
442         if (dev)
443                 kfree(to_xenbus_device(dev));
444 }
445
446 static ssize_t xendev_show_nodename(struct device *dev,
447                                     struct device_attribute *attr, char *buf)
448 {
449         return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
450 }
451 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
452
453 static ssize_t xendev_show_devtype(struct device *dev,
454                                    struct device_attribute *attr, char *buf)
455 {
456         return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
457 }
458 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
459
460 static ssize_t xendev_show_modalias(struct device *dev,
461                                     struct device_attribute *attr, char *buf)
462 {
463         return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype);
464 }
465 DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
466
467 int xenbus_probe_node(struct xen_bus_type *bus,
468                       const char *type,
469                       const char *nodename)
470 {
471         char devname[XEN_BUS_ID_SIZE];
472         int err;
473         struct xenbus_device *xendev;
474         size_t stringlen;
475         char *tmpstring;
476
477         enum xenbus_state state = xenbus_read_driver_state(nodename);
478
479         if (state != XenbusStateInitialising) {
480                 /* Device is not new, so ignore it.  This can happen if a
481                    device is going away after switching to Closed.  */
482                 return 0;
483         }
484
485         stringlen = strlen(nodename) + 1 + strlen(type) + 1;
486         xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
487         if (!xendev)
488                 return -ENOMEM;
489
490         xendev->state = XenbusStateInitialising;
491
492         /* Copy the strings into the extra space. */
493
494         tmpstring = (char *)(xendev + 1);
495         strcpy(tmpstring, nodename);
496         xendev->nodename = tmpstring;
497
498         tmpstring += strlen(tmpstring) + 1;
499         strcpy(tmpstring, type);
500         xendev->devicetype = tmpstring;
501         init_completion(&xendev->down);
502
503         xendev->dev.bus = &bus->bus;
504         xendev->dev.release = xenbus_dev_release;
505
506         err = bus->get_bus_id(devname, xendev->nodename);
507         if (err)
508                 goto fail;
509
510         dev_set_name(&xendev->dev, devname);
511
512         /* Register with generic device framework. */
513         err = device_register(&xendev->dev);
514         if (err)
515                 goto fail;
516
517         err = device_create_file(&xendev->dev, &dev_attr_nodename);
518         if (err)
519                 goto fail_unregister;
520
521         err = device_create_file(&xendev->dev, &dev_attr_devtype);
522         if (err)
523                 goto fail_remove_nodename;
524
525         err = device_create_file(&xendev->dev, &dev_attr_modalias);
526         if (err)
527                 goto fail_remove_devtype;
528
529         return 0;
530 fail_remove_devtype:
531         device_remove_file(&xendev->dev, &dev_attr_devtype);
532 fail_remove_nodename:
533         device_remove_file(&xendev->dev, &dev_attr_nodename);
534 fail_unregister:
535         device_unregister(&xendev->dev);
536 fail:
537         kfree(xendev);
538         return err;
539 }
540
541 /* device/<typename>/<name> */
542 static int xenbus_probe_frontend(const char *type, const char *name)
543 {
544         char *nodename;
545         int err;
546
547         nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
548                              xenbus_frontend.root, type, name);
549         if (!nodename)
550                 return -ENOMEM;
551
552         DPRINTK("%s", nodename);
553
554         err = xenbus_probe_node(&xenbus_frontend, type, nodename);
555         kfree(nodename);
556         return err;
557 }
558
559 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
560 {
561         int err = 0;
562         char **dir;
563         unsigned int dir_n = 0;
564         int i;
565
566         dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
567         if (IS_ERR(dir))
568                 return PTR_ERR(dir);
569
570         for (i = 0; i < dir_n; i++) {
571                 err = bus->probe(type, dir[i]);
572                 if (err)
573                         break;
574         }
575         kfree(dir);
576         return err;
577 }
578
579 int xenbus_probe_devices(struct xen_bus_type *bus)
580 {
581         int err = 0;
582         char **dir;
583         unsigned int i, dir_n;
584
585         dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
586         if (IS_ERR(dir))
587                 return PTR_ERR(dir);
588
589         for (i = 0; i < dir_n; i++) {
590                 err = xenbus_probe_device_type(bus, dir[i]);
591                 if (err)
592                         break;
593         }
594         kfree(dir);
595         return err;
596 }
597
598 static unsigned int char_count(const char *str, char c)
599 {
600         unsigned int i, ret = 0;
601
602         for (i = 0; str[i]; i++)
603                 if (str[i] == c)
604                         ret++;
605         return ret;
606 }
607
608 static int strsep_len(const char *str, char c, unsigned int len)
609 {
610         unsigned int i;
611
612         for (i = 0; str[i]; i++)
613                 if (str[i] == c) {
614                         if (len == 0)
615                                 return i;
616                         len--;
617                 }
618         return (len == 0) ? i : -ERANGE;
619 }
620
621 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
622 {
623         int exists, rootlen;
624         struct xenbus_device *dev;
625         char type[XEN_BUS_ID_SIZE];
626         const char *p, *root;
627
628         if (char_count(node, '/') < 2)
629                 return;
630
631         exists = xenbus_exists(XBT_NIL, node, "");
632         if (!exists) {
633                 xenbus_cleanup_devices(node, &bus->bus);
634                 return;
635         }
636
637         /* backend/<type>/... or device/<type>/... */
638         p = strchr(node, '/') + 1;
639         snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
640         type[XEN_BUS_ID_SIZE-1] = '\0';
641
642         rootlen = strsep_len(node, '/', bus->levels);
643         if (rootlen < 0)
644                 return;
645         root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
646         if (!root)
647                 return;
648
649         dev = xenbus_device_find(root, &bus->bus);
650         if (!dev)
651                 xenbus_probe_node(bus, type, root);
652         else
653                 put_device(&dev->dev);
654
655         kfree(root);
656 }
657
658 static void frontend_changed(struct xenbus_watch *watch,
659                              const char **vec, unsigned int len)
660 {
661         DPRINTK("");
662
663         xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
664 }
665
666 /* We watch for devices appearing and vanishing. */
667 static struct xenbus_watch fe_watch = {
668         .node = "device",
669         .callback = frontend_changed,
670 };
671
672 static int suspend_dev(struct device *dev, void *data)
673 {
674         int err = 0;
675         struct xenbus_driver *drv;
676         struct xenbus_device *xdev;
677
678         DPRINTK("");
679
680         if (dev->driver == NULL)
681                 return 0;
682         drv = to_xenbus_driver(dev->driver);
683         xdev = container_of(dev, struct xenbus_device, dev);
684         if (drv->suspend)
685                 err = drv->suspend(xdev);
686         if (err)
687                 printk(KERN_WARNING
688                        "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
689         return 0;
690 }
691
692 static int suspend_cancel_dev(struct device *dev, void *data)
693 {
694         int err = 0;
695         struct xenbus_driver *drv;
696         struct xenbus_device *xdev;
697
698         DPRINTK("");
699
700         if (dev->driver == NULL)
701                 return 0;
702         drv = to_xenbus_driver(dev->driver);
703         xdev = container_of(dev, struct xenbus_device, dev);
704         if (drv->suspend_cancel)
705                 err = drv->suspend_cancel(xdev);
706         if (err)
707                 printk(KERN_WARNING
708                        "xenbus: suspend_cancel %s failed: %i\n",
709                        dev_name(dev), err);
710         return 0;
711 }
712
713 static int resume_dev(struct device *dev, void *data)
714 {
715         int err;
716         struct xenbus_driver *drv;
717         struct xenbus_device *xdev;
718
719         DPRINTK("");
720
721         if (dev->driver == NULL)
722                 return 0;
723
724         drv = to_xenbus_driver(dev->driver);
725         xdev = container_of(dev, struct xenbus_device, dev);
726
727         err = talk_to_otherend(xdev);
728         if (err) {
729                 printk(KERN_WARNING
730                        "xenbus: resume (talk_to_otherend) %s failed: %i\n",
731                        dev_name(dev), err);
732                 return err;
733         }
734
735         xdev->state = XenbusStateInitialising;
736
737         if (drv->resume) {
738                 err = drv->resume(xdev);
739                 if (err) {
740                         printk(KERN_WARNING
741                                "xenbus: resume %s failed: %i\n",
742                                dev_name(dev), err);
743                         return err;
744                 }
745         }
746
747         err = watch_otherend(xdev);
748         if (err) {
749                 printk(KERN_WARNING
750                        "xenbus_probe: resume (watch_otherend) %s failed: "
751                        "%d.\n", dev_name(dev), err);
752                 return err;
753         }
754
755         return 0;
756 }
757
758 void xenbus_suspend(void)
759 {
760         DPRINTK("");
761
762         bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
763         xenbus_backend_suspend(suspend_dev);
764         xs_suspend();
765 }
766 EXPORT_SYMBOL_GPL(xenbus_suspend);
767
768 void xenbus_resume(void)
769 {
770         xb_init_comms();
771         xs_resume();
772         bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
773         xenbus_backend_resume(resume_dev);
774 }
775 EXPORT_SYMBOL_GPL(xenbus_resume);
776
777 void xenbus_suspend_cancel(void)
778 {
779         xs_suspend_cancel();
780         bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
781         xenbus_backend_resume(suspend_cancel_dev);
782 }
783 EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
784
785 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
786 int xenstored_ready = 0;
787
788
789 int register_xenstore_notifier(struct notifier_block *nb)
790 {
791         int ret = 0;
792
793         if (xenstored_ready > 0)
794                 ret = nb->notifier_call(nb, 0, NULL);
795         else
796                 blocking_notifier_chain_register(&xenstore_chain, nb);
797
798         return ret;
799 }
800 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
801
802 void unregister_xenstore_notifier(struct notifier_block *nb)
803 {
804         blocking_notifier_chain_unregister(&xenstore_chain, nb);
805 }
806 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
807
808 void xenbus_probe(struct work_struct *unused)
809 {
810         BUG_ON((xenstored_ready <= 0));
811
812         /* Enumerate devices in xenstore and watch for changes. */
813         xenbus_probe_devices(&xenbus_frontend);
814         register_xenbus_watch(&fe_watch);
815         xenbus_backend_probe_and_watch();
816
817         /* Notify others that xenstore is up */
818         blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
819 }
820
821 static int __init xenbus_probe_init(void)
822 {
823         int err = 0;
824
825         DPRINTK("");
826
827         err = -ENODEV;
828         if (!xen_domain())
829                 goto out_error;
830
831         /* Register ourselves with the kernel bus subsystem */
832         err = bus_register(&xenbus_frontend.bus);
833         if (err)
834                 goto out_error;
835
836         err = xenbus_backend_bus_register();
837         if (err)
838                 goto out_unreg_front;
839
840         /*
841          * Domain0 doesn't have a store_evtchn or store_mfn yet.
842          */
843         if (xen_initial_domain()) {
844                 /* dom0 not yet supported */
845         } else {
846                 xenstored_ready = 1;
847                 xen_store_evtchn = xen_start_info->store_evtchn;
848                 xen_store_mfn = xen_start_info->store_mfn;
849         }
850         xen_store_interface = mfn_to_virt(xen_store_mfn);
851
852         /* Initialize the interface to xenstore. */
853         err = xs_init();
854         if (err) {
855                 printk(KERN_WARNING
856                        "XENBUS: Error initializing xenstore comms: %i\n", err);
857                 goto out_unreg_back;
858         }
859
860         if (!xen_initial_domain())
861                 xenbus_probe(NULL);
862
863 #ifdef CONFIG_XEN_COMPAT_XENFS
864         /*
865          * Create xenfs mountpoint in /proc for compatibility with
866          * utilities that expect to find "xenbus" under "/proc/xen".
867          */
868         proc_mkdir("xen", NULL);
869 #endif
870
871         return 0;
872
873   out_unreg_back:
874         xenbus_backend_bus_unregister();
875
876   out_unreg_front:
877         bus_unregister(&xenbus_frontend.bus);
878
879   out_error:
880         return err;
881 }
882
883 postcore_initcall(xenbus_probe_init);
884
885 MODULE_LICENSE("GPL");
886
887 static int is_disconnected_device(struct device *dev, void *data)
888 {
889         struct xenbus_device *xendev = to_xenbus_device(dev);
890         struct device_driver *drv = data;
891         struct xenbus_driver *xendrv;
892
893         /*
894          * A device with no driver will never connect. We care only about
895          * devices which should currently be in the process of connecting.
896          */
897         if (!dev->driver)
898                 return 0;
899
900         /* Is this search limited to a particular driver? */
901         if (drv && (dev->driver != drv))
902                 return 0;
903
904         xendrv = to_xenbus_driver(dev->driver);
905         return (xendev->state != XenbusStateConnected ||
906                 (xendrv->is_ready && !xendrv->is_ready(xendev)));
907 }
908
909 static int exists_disconnected_device(struct device_driver *drv)
910 {
911         return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
912                                 is_disconnected_device);
913 }
914
915 static int print_device_status(struct device *dev, void *data)
916 {
917         struct xenbus_device *xendev = to_xenbus_device(dev);
918         struct device_driver *drv = data;
919
920         /* Is this operation limited to a particular driver? */
921         if (drv && (dev->driver != drv))
922                 return 0;
923
924         if (!dev->driver) {
925                 /* Information only: is this too noisy? */
926                 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
927                        xendev->nodename);
928         } else if (xendev->state != XenbusStateConnected) {
929                 printk(KERN_WARNING "XENBUS: Timeout connecting "
930                        "to device: %s (state %d)\n",
931                        xendev->nodename, xendev->state);
932         }
933
934         return 0;
935 }
936
937 /* We only wait for device setup after most initcalls have run. */
938 static int ready_to_wait_for_devices;
939
940 /*
941  * On a 10 second timeout, wait for all devices currently configured.  We need
942  * to do this to guarantee that the filesystems and / or network devices
943  * needed for boot are available, before we can allow the boot to proceed.
944  *
945  * This needs to be on a late_initcall, to happen after the frontend device
946  * drivers have been initialised, but before the root fs is mounted.
947  *
948  * A possible improvement here would be to have the tools add a per-device
949  * flag to the store entry, indicating whether it is needed at boot time.
950  * This would allow people who knew what they were doing to accelerate their
951  * boot slightly, but of course needs tools or manual intervention to set up
952  * those flags correctly.
953  */
954 static void wait_for_devices(struct xenbus_driver *xendrv)
955 {
956         unsigned long timeout = jiffies + 10*HZ;
957         struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
958
959         if (!ready_to_wait_for_devices || !xen_domain())
960                 return;
961
962         while (exists_disconnected_device(drv)) {
963                 if (time_after(jiffies, timeout))
964                         break;
965                 schedule_timeout_interruptible(HZ/10);
966         }
967
968         bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
969                          print_device_status);
970 }
971
972 #ifndef MODULE
973 static int __init boot_wait_for_devices(void)
974 {
975         ready_to_wait_for_devices = 1;
976         wait_for_devices(NULL);
977         return 0;
978 }
979
980 late_initcall(boot_wait_for_devices);
981 #endif