This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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                  __FUNCTION__, __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
46 #include <asm/io.h>
47 #include <asm/page.h>
48 #include <asm/pgtable.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <xen/evtchn.h>
53 #include <xen/features.h>
54
55 #include "xenbus_comms.h"
56
57 extern struct mutex xenwatch_mutex;
58
59 static BLOCKING_NOTIFIER_HEAD(xenstore_notifier_list);
60
61 static void wait_for_devices(struct xenbus_driver *xendrv);
62
63 /* If something in array of ids matches this device, return it. */
64 static const struct xenbus_device_id *
65 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
66 {
67         for (; *arr->devicetype != '\0'; arr++) {
68                 if (!strcmp(arr->devicetype, dev->devicetype))
69                         return arr;
70         }
71         return NULL;
72 }
73
74 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
75 {
76         struct xenbus_driver *drv = to_xenbus_driver(_drv);
77
78         if (!drv->ids)
79                 return 0;
80
81         return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
82 }
83
84 struct xen_bus_type
85 {
86         char *root;
87         unsigned int levels;
88         int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
89         int (*probe)(const char *type, const char *dir);
90         struct bus_type bus;
91         struct device dev;
92 };
93
94
95 /* device/<type>/<id> => <type>-<id> */
96 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
97 {
98         nodename = strchr(nodename, '/');
99         if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
100                 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
101                 return -EINVAL;
102         }
103
104         strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
105         if (!strchr(bus_id, '/')) {
106                 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
107                 return -EINVAL;
108         }
109         *strchr(bus_id, '/') = '-';
110         return 0;
111 }
112
113
114 static void free_otherend_details(struct xenbus_device *dev)
115 {
116         kfree(dev->otherend);
117         dev->otherend = NULL;
118 }
119
120
121 static void free_otherend_watch(struct xenbus_device *dev)
122 {
123         if (dev->otherend_watch.node) {
124                 unregister_xenbus_watch(&dev->otherend_watch);
125                 kfree(dev->otherend_watch.node);
126                 dev->otherend_watch.node = NULL;
127         }
128 }
129
130
131 static int read_otherend_details(struct xenbus_device *xendev,
132                                  char *id_node, char *path_node)
133 {
134         int err = xenbus_gather(XBT_NIL, xendev->nodename,
135                                 id_node, "%i", &xendev->otherend_id,
136                                 path_node, NULL, &xendev->otherend,
137                                 NULL);
138         if (err) {
139                 xenbus_dev_fatal(xendev, err,
140                                  "reading other end details from %s",
141                                  xendev->nodename);
142                 return err;
143         }
144         if (strlen(xendev->otherend) == 0 ||
145             !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
146                 xenbus_dev_fatal(xendev, -ENOENT,
147                                  "unable to read other end from %s.  "
148                                  "missing or inaccessible.",
149                                  xendev->nodename);
150                 free_otherend_details(xendev);
151                 return -ENOENT;
152         }
153
154         return 0;
155 }
156
157
158 static int read_backend_details(struct xenbus_device *xendev)
159 {
160         return read_otherend_details(xendev, "backend-id", "backend");
161 }
162
163
164 static int read_frontend_details(struct xenbus_device *xendev)
165 {
166         return read_otherend_details(xendev, "frontend-id", "frontend");
167 }
168
169
170 /* Bus type for frontend drivers. */
171 static int xenbus_probe_frontend(const char *type, const char *name);
172 static struct xen_bus_type xenbus_frontend = {
173         .root = "device",
174         .levels = 2,            /* device/type/<id> */
175         .get_bus_id = frontend_bus_id,
176         .probe = xenbus_probe_frontend,
177         .bus = {
178                 .name  = "xen",
179                 .match = xenbus_match,
180         },
181         .dev = {
182                 .bus_id = "xen",
183         },
184 };
185
186 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
187 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
188 {
189         int domid, err;
190         const char *devid, *type, *frontend;
191         unsigned int typelen;
192
193         type = strchr(nodename, '/');
194         if (!type)
195                 return -EINVAL;
196         type++;
197         typelen = strcspn(type, "/");
198         if (!typelen || type[typelen] != '/')
199                 return -EINVAL;
200
201         devid = strrchr(nodename, '/') + 1;
202
203         err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
204                             "frontend", NULL, &frontend,
205                             NULL);
206         if (err)
207                 return err;
208         if (strlen(frontend) == 0)
209                 err = -ERANGE;
210         if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
211                 err = -ENOENT;
212
213         kfree(frontend);
214
215         if (err)
216                 return err;
217
218         if (snprintf(bus_id, BUS_ID_SIZE,
219                      "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
220                 return -ENOSPC;
221         return 0;
222 }
223
224 static int xenbus_uevent_backend(struct device *dev, char **envp,
225                                  int num_envp, char *buffer, int buffer_size);
226 static int xenbus_probe_backend(const char *type, const char *domid);
227 static struct xen_bus_type xenbus_backend = {
228         .root = "backend",
229         .levels = 3,            /* backend/type/<frontend>/<id> */
230         .get_bus_id = backend_bus_id,
231         .probe = xenbus_probe_backend,
232         .bus = {
233                 .name  = "xen-backend",
234                 .match = xenbus_match,
235                 .uevent = xenbus_uevent_backend,
236         },
237         .dev = {
238                 .bus_id = "xen-backend",
239         },
240 };
241
242 static int xenbus_uevent_backend(struct device *dev, char **envp,
243                                  int num_envp, char *buffer, int buffer_size)
244 {
245         struct xenbus_device *xdev;
246         struct xenbus_driver *drv;
247         int i = 0;
248         int length = 0;
249
250         DPRINTK("");
251
252         if (dev == NULL)
253                 return -ENODEV;
254
255         xdev = to_xenbus_device(dev);
256         if (xdev == NULL)
257                 return -ENODEV;
258
259         /* stuff we want to pass to /sbin/hotplug */
260         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
261                        "XENBUS_TYPE=%s", xdev->devicetype);
262
263         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
264                        "XENBUS_PATH=%s", xdev->nodename);
265
266         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
267                        "XENBUS_BASE_PATH=%s", xenbus_backend.root);
268
269         /* terminate, set to next free slot, shrink available space */
270         envp[i] = NULL;
271         envp = &envp[i];
272         num_envp -= i;
273         buffer = &buffer[length];
274         buffer_size -= length;
275
276         if (dev->driver) {
277                 drv = to_xenbus_driver(dev->driver);
278                 if (drv && drv->uevent)
279                         return drv->uevent(xdev, envp, num_envp, buffer,
280                                            buffer_size);
281         }
282
283         return 0;
284 }
285
286 static void otherend_changed(struct xenbus_watch *watch,
287                              const char **vec, unsigned int len)
288 {
289         struct xenbus_device *dev =
290                 container_of(watch, struct xenbus_device, otherend_watch);
291         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
292         enum xenbus_state state;
293
294         /* Protect us against watches firing on old details when the otherend
295            details change, say immediately after a resume. */
296         if (!dev->otherend ||
297             strncmp(dev->otherend, vec[XS_WATCH_PATH],
298                     strlen(dev->otherend))) {
299                 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
300                 return;
301         }
302
303         state = xenbus_read_driver_state(dev->otherend);
304
305         DPRINTK("state is %d, %s, %s",
306                 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
307         if (drv->otherend_changed)
308                 drv->otherend_changed(dev, state);
309 }
310
311
312 static int talk_to_otherend(struct xenbus_device *dev)
313 {
314         struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
315
316         free_otherend_watch(dev);
317         free_otherend_details(dev);
318
319         return drv->read_otherend_details(dev);
320 }
321
322
323 static int watch_otherend(struct xenbus_device *dev)
324 {
325         return xenbus_watch_path2(dev, dev->otherend, "state",
326                                   &dev->otherend_watch, otherend_changed);
327 }
328
329
330 static int xenbus_dev_probe(struct device *_dev)
331 {
332         struct xenbus_device *dev = to_xenbus_device(_dev);
333         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
334         const struct xenbus_device_id *id;
335         int err;
336
337         DPRINTK("");
338
339         if (!drv->probe) {
340                 err = -ENODEV;
341                 goto fail;
342         }
343
344         id = match_device(drv->ids, dev);
345         if (!id) {
346                 err = -ENODEV;
347                 goto fail;
348         }
349
350         err = talk_to_otherend(dev);
351         if (err) {
352                 printk(KERN_WARNING
353                        "xenbus_probe: talk_to_otherend on %s failed.\n",
354                        dev->nodename);
355                 return err;
356         }
357
358         err = drv->probe(dev, id);
359         if (err)
360                 goto fail;
361
362         err = watch_otherend(dev);
363         if (err) {
364                 printk(KERN_WARNING
365                        "xenbus_probe: watch_otherend on %s failed.\n",
366                        dev->nodename);
367                 return err;
368         }
369
370         return 0;
371 fail:
372         xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
373         xenbus_switch_state(dev, XenbusStateClosed);
374         return -ENODEV;
375 }
376
377 static int xenbus_dev_remove(struct device *_dev)
378 {
379         struct xenbus_device *dev = to_xenbus_device(_dev);
380         struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
381
382         DPRINTK("");
383
384         free_otherend_watch(dev);
385         free_otherend_details(dev);
386
387         if (drv->remove)
388                 drv->remove(dev);
389
390         xenbus_switch_state(dev, XenbusStateClosed);
391         return 0;
392 }
393
394 static int xenbus_register_driver_common(struct xenbus_driver *drv,
395                                          struct xen_bus_type *bus)
396 {
397         int ret;
398
399         drv->driver.name = drv->name;
400         drv->driver.bus = &bus->bus;
401         drv->driver.owner = drv->owner;
402         drv->driver.probe = xenbus_dev_probe;
403         drv->driver.remove = xenbus_dev_remove;
404
405         mutex_lock(&xenwatch_mutex);
406         ret = driver_register(&drv->driver);
407         mutex_unlock(&xenwatch_mutex);
408         return ret;
409 }
410
411 int xenbus_register_frontend(struct xenbus_driver *drv)
412 {
413         int ret;
414
415         drv->read_otherend_details = read_backend_details;
416
417         ret = xenbus_register_driver_common(drv, &xenbus_frontend);
418         if (ret)
419                 return ret;
420
421         /* If this driver is loaded as a module wait for devices to attach. */
422         wait_for_devices(drv);
423
424         return 0;
425 }
426 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
427
428 int xenbus_register_backend(struct xenbus_driver *drv)
429 {
430         drv->read_otherend_details = read_frontend_details;
431
432         return xenbus_register_driver_common(drv, &xenbus_backend);
433 }
434 EXPORT_SYMBOL_GPL(xenbus_register_backend);
435
436 void xenbus_unregister_driver(struct xenbus_driver *drv)
437 {
438         driver_unregister(&drv->driver);
439 }
440 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
441
442 struct xb_find_info
443 {
444         struct xenbus_device *dev;
445         const char *nodename;
446 };
447
448 static int cmp_dev(struct device *dev, void *data)
449 {
450         struct xenbus_device *xendev = to_xenbus_device(dev);
451         struct xb_find_info *info = data;
452
453         if (!strcmp(xendev->nodename, info->nodename)) {
454                 info->dev = xendev;
455                 get_device(dev);
456                 return 1;
457         }
458         return 0;
459 }
460
461 struct xenbus_device *xenbus_device_find(const char *nodename,
462                                          struct bus_type *bus)
463 {
464         struct xb_find_info info = { .dev = NULL, .nodename = nodename };
465
466         bus_for_each_dev(bus, NULL, &info, cmp_dev);
467         return info.dev;
468 }
469
470 static int cleanup_dev(struct device *dev, void *data)
471 {
472         struct xenbus_device *xendev = to_xenbus_device(dev);
473         struct xb_find_info *info = data;
474         int len = strlen(info->nodename);
475
476         DPRINTK("%s", info->nodename);
477
478         /* Match the info->nodename path, or any subdirectory of that path. */
479         if (strncmp(xendev->nodename, info->nodename, len))
480                 return 0;
481
482         /* If the node name is longer, ensure it really is a subdirectory. */
483         if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
484                 return 0;
485
486         info->dev = xendev;
487         get_device(dev);
488         return 1;
489 }
490
491 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
492 {
493         struct xb_find_info info = { .nodename = path };
494
495         do {
496                 info.dev = NULL;
497                 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
498                 if (info.dev) {
499                         device_unregister(&info.dev->dev);
500                         put_device(&info.dev->dev);
501                 }
502         } while (info.dev);
503 }
504
505 static void xenbus_dev_release(struct device *dev)
506 {
507         if (dev)
508                 kfree(to_xenbus_device(dev));
509 }
510
511 /* Simplified asprintf. */
512 char *kasprintf(const char *fmt, ...)
513 {
514         va_list ap;
515         unsigned int len;
516         char *p, dummy[1];
517
518         va_start(ap, fmt);
519         /* FIXME: vsnprintf has a bug, NULL should work */
520         len = vsnprintf(dummy, 0, fmt, ap);
521         va_end(ap);
522
523         p = kmalloc(len + 1, GFP_KERNEL);
524         if (!p)
525                 return NULL;
526         va_start(ap, fmt);
527         vsprintf(p, fmt, ap);
528         va_end(ap);
529         return p;
530 }
531
532 static ssize_t xendev_show_nodename(struct device *dev,
533                                     struct device_attribute *attr, char *buf)
534 {
535         return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
536 }
537 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
538
539 static ssize_t xendev_show_devtype(struct device *dev,
540                                    struct device_attribute *attr, char *buf)
541 {
542         return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
543 }
544 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
545
546
547 static int xenbus_probe_node(struct xen_bus_type *bus,
548                              const char *type,
549                              const char *nodename)
550 {
551         int err;
552         struct xenbus_device *xendev;
553         size_t stringlen;
554         char *tmpstring;
555
556         enum xenbus_state state = xenbus_read_driver_state(nodename);
557
558         if (state != XenbusStateInitialising) {
559                 /* Device is not new, so ignore it.  This can happen if a
560                    device is going away after switching to Closed.  */
561                 return 0;
562         }
563
564         stringlen = strlen(nodename) + 1 + strlen(type) + 1;
565         xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
566         if (!xendev)
567                 return -ENOMEM;
568
569         /* Copy the strings into the extra space. */
570
571         tmpstring = (char *)(xendev + 1);
572         strcpy(tmpstring, nodename);
573         xendev->nodename = tmpstring;
574
575         tmpstring += strlen(tmpstring) + 1;
576         strcpy(tmpstring, type);
577         xendev->devicetype = tmpstring;
578
579         xendev->dev.parent = &bus->dev;
580         xendev->dev.bus = &bus->bus;
581         xendev->dev.release = xenbus_dev_release;
582
583         err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
584         if (err)
585                 goto fail;
586
587         /* Register with generic device framework. */
588         err = device_register(&xendev->dev);
589         if (err)
590                 goto fail;
591
592         device_create_file(&xendev->dev, &dev_attr_nodename);
593         device_create_file(&xendev->dev, &dev_attr_devtype);
594
595         return 0;
596 fail:
597         kfree(xendev);
598         return err;
599 }
600
601 /* device/<typename>/<name> */
602 static int xenbus_probe_frontend(const char *type, const char *name)
603 {
604         char *nodename;
605         int err;
606
607         nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
608         if (!nodename)
609                 return -ENOMEM;
610
611         DPRINTK("%s", nodename);
612
613         err = xenbus_probe_node(&xenbus_frontend, type, nodename);
614         kfree(nodename);
615         return err;
616 }
617
618 /* backend/<typename>/<frontend-uuid>/<name> */
619 static int xenbus_probe_backend_unit(const char *dir,
620                                      const char *type,
621                                      const char *name)
622 {
623         char *nodename;
624         int err;
625
626         nodename = kasprintf("%s/%s", dir, name);
627         if (!nodename)
628                 return -ENOMEM;
629
630         DPRINTK("%s\n", nodename);
631
632         err = xenbus_probe_node(&xenbus_backend, type, nodename);
633         kfree(nodename);
634         return err;
635 }
636
637 /* backend/<typename>/<frontend-domid> */
638 static int xenbus_probe_backend(const char *type, const char *domid)
639 {
640         char *nodename;
641         int err = 0;
642         char **dir;
643         unsigned int i, dir_n = 0;
644
645         DPRINTK("");
646
647         nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
648         if (!nodename)
649                 return -ENOMEM;
650
651         dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
652         if (IS_ERR(dir)) {
653                 kfree(nodename);
654                 return PTR_ERR(dir);
655         }
656
657         for (i = 0; i < dir_n; i++) {
658                 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
659                 if (err)
660                         break;
661         }
662         kfree(dir);
663         kfree(nodename);
664         return err;
665 }
666
667 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
668 {
669         int err = 0;
670         char **dir;
671         unsigned int dir_n = 0;
672         int i;
673
674         dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
675         if (IS_ERR(dir))
676                 return PTR_ERR(dir);
677
678         for (i = 0; i < dir_n; i++) {
679                 err = bus->probe(type, dir[i]);
680                 if (err)
681                         break;
682         }
683         kfree(dir);
684         return err;
685 }
686
687 static int xenbus_probe_devices(struct xen_bus_type *bus)
688 {
689         int err = 0;
690         char **dir;
691         unsigned int i, dir_n;
692
693         dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
694         if (IS_ERR(dir))
695                 return PTR_ERR(dir);
696
697         for (i = 0; i < dir_n; i++) {
698                 err = xenbus_probe_device_type(bus, dir[i]);
699                 if (err)
700                         break;
701         }
702         kfree(dir);
703         return err;
704 }
705
706 static unsigned int char_count(const char *str, char c)
707 {
708         unsigned int i, ret = 0;
709
710         for (i = 0; str[i]; i++)
711                 if (str[i] == c)
712                         ret++;
713         return ret;
714 }
715
716 static int strsep_len(const char *str, char c, unsigned int len)
717 {
718         unsigned int i;
719
720         for (i = 0; str[i]; i++)
721                 if (str[i] == c) {
722                         if (len == 0)
723                                 return i;
724                         len--;
725                 }
726         return (len == 0) ? i : -ERANGE;
727 }
728
729 static void dev_changed(const char *node, struct xen_bus_type *bus)
730 {
731         int exists, rootlen;
732         struct xenbus_device *dev;
733         char type[BUS_ID_SIZE];
734         const char *p, *root;
735
736         if (char_count(node, '/') < 2)
737                 return;
738
739         exists = xenbus_exists(XBT_NIL, node, "");
740         if (!exists) {
741                 xenbus_cleanup_devices(node, &bus->bus);
742                 return;
743         }
744
745         /* backend/<type>/... or device/<type>/... */
746         p = strchr(node, '/') + 1;
747         snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
748         type[BUS_ID_SIZE-1] = '\0';
749
750         rootlen = strsep_len(node, '/', bus->levels);
751         if (rootlen < 0)
752                 return;
753         root = kasprintf("%.*s", rootlen, node);
754         if (!root)
755                 return;
756
757         dev = xenbus_device_find(root, &bus->bus);
758         if (!dev)
759                 xenbus_probe_node(bus, type, root);
760         else
761                 put_device(&dev->dev);
762
763         kfree(root);
764 }
765
766 static void frontend_changed(struct xenbus_watch *watch,
767                              const char **vec, unsigned int len)
768 {
769         DPRINTK("");
770
771         dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
772 }
773
774 static void backend_changed(struct xenbus_watch *watch,
775                             const char **vec, unsigned int len)
776 {
777         DPRINTK("");
778
779         dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
780 }
781
782 /* We watch for devices appearing and vanishing. */
783 static struct xenbus_watch fe_watch = {
784         .node = "device",
785         .callback = frontend_changed,
786 };
787
788 static struct xenbus_watch be_watch = {
789         .node = "backend",
790         .callback = backend_changed,
791 };
792
793 static int suspend_dev(struct device *dev, void *data)
794 {
795         int err = 0;
796         struct xenbus_driver *drv;
797         struct xenbus_device *xdev;
798
799         DPRINTK("");
800
801         if (dev->driver == NULL)
802                 return 0;
803         drv = to_xenbus_driver(dev->driver);
804         xdev = container_of(dev, struct xenbus_device, dev);
805         if (drv->suspend)
806                 err = drv->suspend(xdev);
807         if (err)
808                 printk(KERN_WARNING
809                        "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
810         return 0;
811 }
812
813 static int resume_dev(struct device *dev, void *data)
814 {
815         int err;
816         struct xenbus_driver *drv;
817         struct xenbus_device *xdev;
818
819         DPRINTK("");
820
821         if (dev->driver == NULL)
822                 return 0;
823
824         drv = to_xenbus_driver(dev->driver);
825         xdev = container_of(dev, struct xenbus_device, dev);
826
827         err = talk_to_otherend(xdev);
828         if (err) {
829                 printk(KERN_WARNING
830                        "xenbus: resume (talk_to_otherend) %s failed: %i\n",
831                        dev->bus_id, err);
832                 return err;
833         }
834
835         xdev->state = XenbusStateInitialising;
836
837         if (drv->resume) {
838                 err = drv->resume(xdev);
839                 if (err) { 
840                         printk(KERN_WARNING
841                                "xenbus: resume %s failed: %i\n", 
842                                dev->bus_id, err);
843                         return err; 
844                 }
845         }
846
847         err = watch_otherend(xdev);
848         if (err) {
849                 printk(KERN_WARNING
850                        "xenbus_probe: resume (watch_otherend) %s failed: "
851                        "%d.\n", dev->bus_id, err);
852                 return err;
853         }
854
855         return 0; 
856 }
857
858 void xenbus_suspend(void)
859 {
860         DPRINTK("");
861
862         bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
863         bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
864         xs_suspend();
865 }
866 EXPORT_SYMBOL_GPL(xenbus_suspend);
867
868 void xenbus_resume(void)
869 {
870         xb_init_comms();
871         xs_resume();
872         bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
873         bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
874 }
875 EXPORT_SYMBOL_GPL(xenbus_resume);
876
877
878 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
879 int xenstored_ready = 0;
880
881
882 int register_xenstore_notifier(struct notifier_block *nb)
883 {
884         int ret = 0;
885
886         if (xenstored_ready > 0)
887                 ret = nb->notifier_call(nb, 0, NULL);
888         else
889                 blocking_notifier_chain_register(&xenstore_notifier_list, nb);
890
891         return ret;
892 }
893 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
894
895 void unregister_xenstore_notifier(struct notifier_block *nb)
896 {
897         blocking_notifier_chain_unregister(&xenstore_notifier_list, nb);
898 }
899 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
900
901
902 void xenbus_probe(void *unused)
903 {
904         BUG_ON((xenstored_ready <= 0));
905
906         /* Enumerate devices in xenstore. */
907         xenbus_probe_devices(&xenbus_frontend);
908         xenbus_probe_devices(&xenbus_backend);
909
910         /* Watch for changes. */
911         register_xenbus_watch(&fe_watch);
912         register_xenbus_watch(&be_watch);
913
914         /* Notify others that xenstore is up */
915         blocking_notifier_call_chain(&xenstore_notifier_list, 0, NULL);
916 }
917
918
919 #ifdef CONFIG_PROC_FS
920 static struct file_operations xsd_kva_fops;
921 static struct proc_dir_entry *xsd_kva_intf;
922 static struct proc_dir_entry *xsd_port_intf;
923
924 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
925 {
926         size_t size = vma->vm_end - vma->vm_start;
927
928         if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
929                 return -EINVAL;
930
931         if (remap_pfn_range(vma, vma->vm_start,
932                             mfn_to_pfn(xen_start_info->store_mfn),
933                             size, vma->vm_page_prot))
934                 return -EAGAIN;
935
936         return 0;
937 }
938
939 static int xsd_kva_read(char *page, char **start, off_t off,
940                         int count, int *eof, void *data)
941 {
942         int len;
943
944         len  = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
945         *eof = 1;
946         return len;
947 }
948
949 static int xsd_port_read(char *page, char **start, off_t off,
950                          int count, int *eof, void *data)
951 {
952         int len;
953
954         len  = sprintf(page, "%d", xen_start_info->store_evtchn);
955         *eof = 1;
956         return len;
957 }
958 #endif
959
960
961 static int __init xenbus_probe_init(void)
962 {
963         int err = 0, dom0;
964         unsigned long page = 0;
965
966         DPRINTK("");
967
968         if (!is_running_on_xen())
969                 return -ENODEV;
970
971         /* Register ourselves with the kernel bus subsystem */
972         bus_register(&xenbus_frontend.bus);
973         bus_register(&xenbus_backend.bus);
974
975         /*
976          * Domain0 doesn't have a store_evtchn or store_mfn yet.
977          */
978         dom0 = (xen_start_info->store_evtchn == 0);
979
980         if (dom0) {
981                 struct evtchn_alloc_unbound alloc_unbound;
982
983                 /* Allocate page. */
984                 page = get_zeroed_page(GFP_KERNEL);
985                 if (!page)
986                         return -ENOMEM;
987
988                 xen_start_info->store_mfn =
989                         pfn_to_mfn(virt_to_phys((void *)page) >>
990                                    PAGE_SHIFT);
991
992                 /* Next allocate a local port which xenstored can bind to */
993                 alloc_unbound.dom        = DOMID_SELF;
994                 alloc_unbound.remote_dom = 0;
995
996                 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
997                                                   &alloc_unbound);
998                 if (err == -ENOSYS)
999                         goto err;
1000                 BUG_ON(err);
1001                 xen_start_info->store_evtchn = alloc_unbound.port;
1002
1003 #ifdef CONFIG_PROC_FS
1004                 /* And finally publish the above info in /proc/xen */
1005                 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
1006                 if (xsd_kva_intf) {
1007                         memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
1008                                sizeof(xsd_kva_fops));
1009                         xsd_kva_fops.mmap = xsd_kva_mmap;
1010                         xsd_kva_intf->proc_fops = &xsd_kva_fops;
1011                         xsd_kva_intf->read_proc = xsd_kva_read;
1012                 }
1013                 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1014                 if (xsd_port_intf)
1015                         xsd_port_intf->read_proc = xsd_port_read;
1016 #endif
1017         } else
1018                 xenstored_ready = 1;
1019
1020         /* Initialize the interface to xenstore. */
1021         err = xs_init();
1022         if (err) {
1023                 printk(KERN_WARNING
1024                        "XENBUS: Error initializing xenstore comms: %i\n", err);
1025                 goto err;
1026         }
1027
1028         /* Register ourselves with the kernel device subsystem */
1029         device_register(&xenbus_frontend.dev);
1030         device_register(&xenbus_backend.dev);
1031
1032         if (!dom0)
1033                 xenbus_probe(NULL);
1034
1035         return 0;
1036
1037  err:
1038         if (page)
1039                 free_page(page);
1040
1041         /*
1042          * Do not unregister the xenbus front/backend buses here. The buses
1043          * must exist because front/backend drivers will use them when they are
1044          * registered.
1045          */
1046
1047         return err;
1048 }
1049
1050 postcore_initcall(xenbus_probe_init);
1051
1052
1053 static int is_disconnected_device(struct device *dev, void *data)
1054 {
1055         struct xenbus_device *xendev = to_xenbus_device(dev);
1056         struct device_driver *drv = data;
1057
1058         /*
1059          * A device with no driver will never connect. We care only about
1060          * devices which should currently be in the process of connecting.
1061          */
1062         if (!dev->driver)
1063                 return 0;
1064
1065         /* Is this search limited to a particular driver? */
1066         if (drv && (dev->driver != drv))
1067                 return 0;
1068
1069         return (xendev->state != XenbusStateConnected);
1070 }
1071
1072 static int exists_disconnected_device(struct device_driver *drv)
1073 {
1074         return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1075                                 is_disconnected_device);
1076 }
1077
1078 static int print_device_status(struct device *dev, void *data)
1079 {
1080         struct xenbus_device *xendev = to_xenbus_device(dev);
1081         struct device_driver *drv = data;
1082
1083         /* Is this operation limited to a particular driver? */
1084         if (drv && (dev->driver != drv))
1085                 return 0;
1086
1087         if (!dev->driver) {
1088                 /* Information only: is this too noisy? */
1089                 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1090                        xendev->nodename);
1091         } else if (xendev->state != XenbusStateConnected) {
1092                 printk(KERN_WARNING "XENBUS: Timeout connecting "
1093                        "to device: %s (state %d)\n",
1094                        xendev->nodename, xendev->state);
1095         }
1096
1097         return 0;
1098 }
1099
1100 /* We only wait for device setup after most initcalls have run. */
1101 static int ready_to_wait_for_devices;
1102
1103 /*
1104  * On a 10 second timeout, wait for all devices currently configured.  We need
1105  * to do this to guarantee that the filesystems and / or network devices
1106  * needed for boot are available, before we can allow the boot to proceed.
1107  *
1108  * This needs to be on a late_initcall, to happen after the frontend device
1109  * drivers have been initialised, but before the root fs is mounted.
1110  *
1111  * A possible improvement here would be to have the tools add a per-device
1112  * flag to the store entry, indicating whether it is needed at boot time.
1113  * This would allow people who knew what they were doing to accelerate their
1114  * boot slightly, but of course needs tools or manual intervention to set up
1115  * those flags correctly.
1116  */
1117 static void wait_for_devices(struct xenbus_driver *xendrv)
1118 {
1119         unsigned long timeout = jiffies + 10*HZ;
1120         struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
1121
1122         if (!ready_to_wait_for_devices || !is_running_on_xen())
1123                 return;
1124
1125         while (exists_disconnected_device(drv)) {
1126                 if (time_after(jiffies, timeout))
1127                         break;
1128                 schedule_timeout_interruptible(HZ/10);
1129         }
1130
1131         bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1132                          print_device_status);
1133 }
1134
1135 static int __init boot_wait_for_devices(void)
1136 {
1137         ready_to_wait_for_devices = 1;
1138         wait_for_devices(NULL);
1139         return 0;
1140 }
1141
1142 late_initcall(boot_wait_for_devices);