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