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