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