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