1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005, 2006 XenSource Ltd
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:
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __func__, __LINE__, ##args)
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>
43 #include <linux/proc_fs.h>
44 #include <linux/notifier.h>
45 #include <linux/kthread.h>
46 #include <linux/mutex.h>
48 #include <linux/slab.h>
51 #include <asm/pgtable.h>
52 #include <asm/xen/hypervisor.h>
55 #include <xen/xenbus.h>
56 #include <xen/events.h>
61 #include "xenbus_comms.h"
62 #include "xenbus_probe.h"
66 EXPORT_SYMBOL_GPL(xen_store_evtchn
);
68 struct xenstore_domain_interface
*xen_store_interface
;
69 EXPORT_SYMBOL_GPL(xen_store_interface
);
71 static unsigned long xen_store_mfn
;
73 static BLOCKING_NOTIFIER_HEAD(xenstore_chain
);
75 /* If something in array of ids matches this device, return it. */
76 static const struct xenbus_device_id
*
77 match_device(const struct xenbus_device_id
*arr
, struct xenbus_device
*dev
)
79 for (; *arr
->devicetype
!= '\0'; arr
++) {
80 if (!strcmp(arr
->devicetype
, dev
->devicetype
))
86 int xenbus_match(struct device
*_dev
, struct device_driver
*_drv
)
88 struct xenbus_driver
*drv
= to_xenbus_driver(_drv
);
93 return match_device(drv
->ids
, to_xenbus_device(_dev
)) != NULL
;
95 EXPORT_SYMBOL_GPL(xenbus_match
);
98 static void free_otherend_details(struct xenbus_device
*dev
)
100 kfree(dev
->otherend
);
101 dev
->otherend
= NULL
;
105 static void free_otherend_watch(struct xenbus_device
*dev
)
107 if (dev
->otherend_watch
.node
) {
108 unregister_xenbus_watch(&dev
->otherend_watch
);
109 kfree(dev
->otherend_watch
.node
);
110 dev
->otherend_watch
.node
= NULL
;
115 static int talk_to_otherend(struct xenbus_device
*dev
)
117 struct xenbus_driver
*drv
= to_xenbus_driver(dev
->dev
.driver
);
119 free_otherend_watch(dev
);
120 free_otherend_details(dev
);
122 return drv
->read_otherend_details(dev
);
127 static int watch_otherend(struct xenbus_device
*dev
)
129 struct xen_bus_type
*bus
= container_of(dev
->dev
.bus
, struct xen_bus_type
, bus
);
131 return xenbus_watch_pathfmt(dev
, &dev
->otherend_watch
, bus
->otherend_changed
,
132 "%s/%s", dev
->otherend
, "state");
136 int xenbus_read_otherend_details(struct xenbus_device
*xendev
,
137 char *id_node
, char *path_node
)
139 int err
= xenbus_gather(XBT_NIL
, xendev
->nodename
,
140 id_node
, "%i", &xendev
->otherend_id
,
141 path_node
, NULL
, &xendev
->otherend
,
144 xenbus_dev_fatal(xendev
, err
,
145 "reading other end details from %s",
149 if (strlen(xendev
->otherend
) == 0 ||
150 !xenbus_exists(XBT_NIL
, xendev
->otherend
, "")) {
151 xenbus_dev_fatal(xendev
, -ENOENT
,
152 "unable to read other end from %s. "
153 "missing or inaccessible.",
155 free_otherend_details(xendev
);
161 EXPORT_SYMBOL_GPL(xenbus_read_otherend_details
);
163 void xenbus_otherend_changed(struct xenbus_watch
*watch
,
164 const char **vec
, unsigned int len
,
165 int ignore_on_shutdown
)
167 struct xenbus_device
*dev
=
168 container_of(watch
, struct xenbus_device
, otherend_watch
);
169 struct xenbus_driver
*drv
= to_xenbus_driver(dev
->dev
.driver
);
170 enum xenbus_state state
;
172 /* Protect us against watches firing on old details when the otherend
173 details change, say immediately after a resume. */
174 if (!dev
->otherend
||
175 strncmp(dev
->otherend
, vec
[XS_WATCH_PATH
],
176 strlen(dev
->otherend
))) {
177 dev_dbg(&dev
->dev
, "Ignoring watch at %s\n",
182 state
= xenbus_read_driver_state(dev
->otherend
);
184 dev_dbg(&dev
->dev
, "state is %d, (%s), %s, %s\n",
185 state
, xenbus_strstate(state
), dev
->otherend_watch
.node
,
189 * Ignore xenbus transitions during shutdown. This prevents us doing
190 * work that can fail e.g., when the rootfs is gone.
192 if (system_state
> SYSTEM_RUNNING
) {
193 if (ignore_on_shutdown
&& (state
== XenbusStateClosing
))
194 xenbus_frontend_closed(dev
);
198 if (drv
->otherend_changed
)
199 drv
->otherend_changed(dev
, state
);
201 EXPORT_SYMBOL_GPL(xenbus_otherend_changed
);
203 int xenbus_dev_probe(struct device
*_dev
)
205 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
206 struct xenbus_driver
*drv
= to_xenbus_driver(_dev
->driver
);
207 const struct xenbus_device_id
*id
;
210 DPRINTK("%s", dev
->nodename
);
217 id
= match_device(drv
->ids
, dev
);
223 err
= talk_to_otherend(dev
);
225 dev_warn(&dev
->dev
, "talk_to_otherend on %s failed.\n",
230 err
= drv
->probe(dev
, id
);
234 err
= watch_otherend(dev
);
236 dev_warn(&dev
->dev
, "watch_otherend on %s failed.\n",
243 xenbus_dev_error(dev
, err
, "xenbus_dev_probe on %s", dev
->nodename
);
244 xenbus_switch_state(dev
, XenbusStateClosed
);
247 EXPORT_SYMBOL_GPL(xenbus_dev_probe
);
249 int xenbus_dev_remove(struct device
*_dev
)
251 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
252 struct xenbus_driver
*drv
= to_xenbus_driver(_dev
->driver
);
254 DPRINTK("%s", dev
->nodename
);
256 free_otherend_watch(dev
);
257 free_otherend_details(dev
);
262 xenbus_switch_state(dev
, XenbusStateClosed
);
265 EXPORT_SYMBOL_GPL(xenbus_dev_remove
);
267 void xenbus_dev_shutdown(struct device
*_dev
)
269 struct xenbus_device
*dev
= to_xenbus_device(_dev
);
270 unsigned long timeout
= 5*HZ
;
272 DPRINTK("%s", dev
->nodename
);
274 get_device(&dev
->dev
);
275 if (dev
->state
!= XenbusStateConnected
) {
276 printk(KERN_INFO
"%s: %s: %s != Connected, skipping\n", __func__
,
277 dev
->nodename
, xenbus_strstate(dev
->state
));
280 xenbus_switch_state(dev
, XenbusStateClosing
);
281 timeout
= wait_for_completion_timeout(&dev
->down
, timeout
);
283 printk(KERN_INFO
"%s: %s timeout closing device\n",
284 __func__
, dev
->nodename
);
286 put_device(&dev
->dev
);
288 EXPORT_SYMBOL_GPL(xenbus_dev_shutdown
);
290 int xenbus_register_driver_common(struct xenbus_driver
*drv
,
291 struct xen_bus_type
*bus
,
292 struct module
*owner
,
293 const char *mod_name
)
295 drv
->driver
.name
= drv
->name
;
296 drv
->driver
.bus
= &bus
->bus
;
297 drv
->driver
.owner
= owner
;
298 drv
->driver
.mod_name
= mod_name
;
300 return driver_register(&drv
->driver
);
302 EXPORT_SYMBOL_GPL(xenbus_register_driver_common
);
304 void xenbus_unregister_driver(struct xenbus_driver
*drv
)
306 driver_unregister(&drv
->driver
);
308 EXPORT_SYMBOL_GPL(xenbus_unregister_driver
);
312 struct xenbus_device
*dev
;
313 const char *nodename
;
316 static int cmp_dev(struct device
*dev
, void *data
)
318 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
319 struct xb_find_info
*info
= data
;
321 if (!strcmp(xendev
->nodename
, info
->nodename
)) {
329 struct xenbus_device
*xenbus_device_find(const char *nodename
,
330 struct bus_type
*bus
)
332 struct xb_find_info info
= { .dev
= NULL
, .nodename
= nodename
};
334 bus_for_each_dev(bus
, NULL
, &info
, cmp_dev
);
338 static int cleanup_dev(struct device
*dev
, void *data
)
340 struct xenbus_device
*xendev
= to_xenbus_device(dev
);
341 struct xb_find_info
*info
= data
;
342 int len
= strlen(info
->nodename
);
344 DPRINTK("%s", info
->nodename
);
346 /* Match the info->nodename path, or any subdirectory of that path. */
347 if (strncmp(xendev
->nodename
, info
->nodename
, len
))
350 /* If the node name is longer, ensure it really is a subdirectory. */
351 if ((strlen(xendev
->nodename
) > len
) && (xendev
->nodename
[len
] != '/'))
359 static void xenbus_cleanup_devices(const char *path
, struct bus_type
*bus
)
361 struct xb_find_info info
= { .nodename
= path
};
365 bus_for_each_dev(bus
, NULL
, &info
, cleanup_dev
);
367 device_unregister(&info
.dev
->dev
);
368 put_device(&info
.dev
->dev
);
373 static void xenbus_dev_release(struct device
*dev
)
376 kfree(to_xenbus_device(dev
));
379 static ssize_t
xendev_show_nodename(struct device
*dev
,
380 struct device_attribute
*attr
, char *buf
)
382 return sprintf(buf
, "%s\n", to_xenbus_device(dev
)->nodename
);
384 static DEVICE_ATTR(nodename
, S_IRUSR
| S_IRGRP
| S_IROTH
, xendev_show_nodename
, NULL
);
386 static ssize_t
xendev_show_devtype(struct device
*dev
,
387 struct device_attribute
*attr
, char *buf
)
389 return sprintf(buf
, "%s\n", to_xenbus_device(dev
)->devicetype
);
391 static DEVICE_ATTR(devtype
, S_IRUSR
| S_IRGRP
| S_IROTH
, xendev_show_devtype
, NULL
);
393 static ssize_t
xendev_show_modalias(struct device
*dev
,
394 struct device_attribute
*attr
, char *buf
)
396 return sprintf(buf
, "xen:%s\n", to_xenbus_device(dev
)->devicetype
);
398 static DEVICE_ATTR(modalias
, S_IRUSR
| S_IRGRP
| S_IROTH
, xendev_show_modalias
, NULL
);
400 int xenbus_probe_node(struct xen_bus_type
*bus
,
402 const char *nodename
)
404 char devname
[XEN_BUS_ID_SIZE
];
406 struct xenbus_device
*xendev
;
410 enum xenbus_state state
= xenbus_read_driver_state(nodename
);
412 if (state
!= XenbusStateInitialising
) {
413 /* Device is not new, so ignore it. This can happen if a
414 device is going away after switching to Closed. */
418 stringlen
= strlen(nodename
) + 1 + strlen(type
) + 1;
419 xendev
= kzalloc(sizeof(*xendev
) + stringlen
, GFP_KERNEL
);
423 xendev
->state
= XenbusStateInitialising
;
425 /* Copy the strings into the extra space. */
427 tmpstring
= (char *)(xendev
+ 1);
428 strcpy(tmpstring
, nodename
);
429 xendev
->nodename
= tmpstring
;
431 tmpstring
+= strlen(tmpstring
) + 1;
432 strcpy(tmpstring
, type
);
433 xendev
->devicetype
= tmpstring
;
434 init_completion(&xendev
->down
);
436 xendev
->dev
.bus
= &bus
->bus
;
437 xendev
->dev
.release
= xenbus_dev_release
;
439 err
= bus
->get_bus_id(devname
, xendev
->nodename
);
443 dev_set_name(&xendev
->dev
, devname
);
445 /* Register with generic device framework. */
446 err
= device_register(&xendev
->dev
);
450 err
= device_create_file(&xendev
->dev
, &dev_attr_nodename
);
452 goto fail_unregister
;
454 err
= device_create_file(&xendev
->dev
, &dev_attr_devtype
);
456 goto fail_remove_nodename
;
458 err
= device_create_file(&xendev
->dev
, &dev_attr_modalias
);
460 goto fail_remove_devtype
;
464 device_remove_file(&xendev
->dev
, &dev_attr_devtype
);
465 fail_remove_nodename
:
466 device_remove_file(&xendev
->dev
, &dev_attr_nodename
);
468 device_unregister(&xendev
->dev
);
473 EXPORT_SYMBOL_GPL(xenbus_probe_node
);
475 static int xenbus_probe_device_type(struct xen_bus_type
*bus
, const char *type
)
479 unsigned int dir_n
= 0;
482 printk(KERN_CRIT
"%s type %s\n", __func__
, type
);
484 dir
= xenbus_directory(XBT_NIL
, bus
->root
, type
, &dir_n
);
486 printk(KERN_CRIT
"%s failed xenbus_directory\n", __func__
);
490 for (i
= 0; i
< dir_n
; i
++) {
491 printk(KERN_CRIT
"%s %d/%d %s\n", __func__
, i
+1,dir_n
, dir
[i
]);
492 err
= bus
->probe(bus
, type
, dir
[i
]);
494 printk(KERN_CRIT
"%s failed\n", __func__
);
498 printk("%s done\n", __func__
);
503 int xenbus_probe_devices(struct xen_bus_type
*bus
)
507 unsigned int i
, dir_n
;
509 printk(KERN_CRIT
"%s %s\n", __func__
, bus
->root
);
511 dir
= xenbus_directory(XBT_NIL
, bus
->root
, "", &dir_n
);
513 printk(KERN_CRIT
"%s failed xenbus_directory\n", __func__
);
517 for (i
= 0; i
< dir_n
; i
++) {
518 printk(KERN_CRIT
"%s %d/%d %s\n", __func__
, i
+1,dir_n
, dir
[i
]);
519 err
= xenbus_probe_device_type(bus
, dir
[i
]);
521 printk(KERN_CRIT
"%s failed\n", __func__
);
525 printk("%s done\n", __func__
);
529 EXPORT_SYMBOL_GPL(xenbus_probe_devices
);
531 static unsigned int char_count(const char *str
, char c
)
533 unsigned int i
, ret
= 0;
535 for (i
= 0; str
[i
]; i
++)
541 static int strsep_len(const char *str
, char c
, unsigned int len
)
545 for (i
= 0; str
[i
]; i
++)
551 return (len
== 0) ? i
: -ERANGE
;
554 void xenbus_dev_changed(const char *node
, struct xen_bus_type
*bus
)
557 struct xenbus_device
*dev
;
558 char type
[XEN_BUS_ID_SIZE
];
559 const char *p
, *root
;
561 if (char_count(node
, '/') < 2)
564 exists
= xenbus_exists(XBT_NIL
, node
, "");
566 xenbus_cleanup_devices(node
, &bus
->bus
);
570 /* backend/<type>/... or device/<type>/... */
571 p
= strchr(node
, '/') + 1;
572 snprintf(type
, XEN_BUS_ID_SIZE
, "%.*s", (int)strcspn(p
, "/"), p
);
573 type
[XEN_BUS_ID_SIZE
-1] = '\0';
575 rootlen
= strsep_len(node
, '/', bus
->levels
);
578 root
= kasprintf(GFP_KERNEL
, "%.*s", rootlen
, node
);
582 dev
= xenbus_device_find(root
, &bus
->bus
);
584 xenbus_probe_node(bus
, type
, root
);
586 put_device(&dev
->dev
);
590 EXPORT_SYMBOL_GPL(xenbus_dev_changed
);
592 int xenbus_dev_suspend(struct device
*dev
, pm_message_t state
)
595 struct xenbus_driver
*drv
;
596 struct xenbus_device
*xdev
= container_of(dev
, struct xenbus_device
, dev
);
598 DPRINTK("%s", xdev
->nodename
);
600 if (dev
->driver
== NULL
)
602 drv
= to_xenbus_driver(dev
->driver
);
604 err
= drv
->suspend(xdev
, state
);
607 "xenbus: suspend %s failed: %i\n", dev_name(dev
), err
);
610 EXPORT_SYMBOL_GPL(xenbus_dev_suspend
);
612 int xenbus_dev_resume(struct device
*dev
)
615 struct xenbus_driver
*drv
;
616 struct xenbus_device
*xdev
= container_of(dev
, struct xenbus_device
, dev
);
618 DPRINTK("%s", xdev
->nodename
);
620 if (dev
->driver
== NULL
)
622 drv
= to_xenbus_driver(dev
->driver
);
623 err
= talk_to_otherend(xdev
);
626 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
631 xdev
->state
= XenbusStateInitialising
;
634 err
= drv
->resume(xdev
);
637 "xenbus: resume %s failed: %i\n",
643 err
= watch_otherend(xdev
);
646 "xenbus_probe: resume (watch_otherend) %s failed: "
647 "%d.\n", dev_name(dev
), err
);
653 EXPORT_SYMBOL_GPL(xenbus_dev_resume
);
655 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
656 int xenstored_ready
= 0;
659 int register_xenstore_notifier(struct notifier_block
*nb
)
663 if (xenstored_ready
> 0)
664 ret
= nb
->notifier_call(nb
, 0, NULL
);
666 blocking_notifier_chain_register(&xenstore_chain
, nb
);
670 EXPORT_SYMBOL_GPL(register_xenstore_notifier
);
672 void unregister_xenstore_notifier(struct notifier_block
*nb
)
674 blocking_notifier_chain_unregister(&xenstore_chain
, nb
);
676 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier
);
678 void xenbus_probe(struct work_struct
*unused
)
682 printk(KERN_CRIT
"xenbus_probe wake_waiting\n");
684 /* Notify others that xenstore is up */
685 blocking_notifier_call_chain(&xenstore_chain
, 0, NULL
);
687 EXPORT_SYMBOL_GPL(xenbus_probe
);
689 static int __init
xenbus_probe_initcall(void)
694 if (xen_initial_domain() || xen_hvm_domain())
701 device_initcall(xenbus_probe_initcall
);
703 static int __init
xenbus_init(void)
706 unsigned long page
= 0;
715 * Domain0 doesn't have a store_evtchn or store_mfn yet.
717 if (xen_initial_domain()) {
718 struct evtchn_alloc_unbound alloc_unbound
;
720 /* Allocate Xenstore page */
721 page
= get_zeroed_page(GFP_KERNEL
);
725 xen_store_mfn
= xen_start_info
->store_mfn
=
726 pfn_to_mfn(virt_to_phys((void *)page
) >>
729 /* Next allocate a local port which xenstored can bind to */
730 alloc_unbound
.dom
= DOMID_SELF
;
731 alloc_unbound
.remote_dom
= 0;
733 err
= HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound
,
739 xen_store_evtchn
= xen_start_info
->store_evtchn
=
742 xen_store_interface
= mfn_to_virt(xen_store_mfn
);
744 if (xen_hvm_domain()) {
746 err
= hvm_get_parameter(HVM_PARAM_STORE_EVTCHN
, &v
);
749 xen_store_evtchn
= (int)v
;
750 err
= hvm_get_parameter(HVM_PARAM_STORE_PFN
, &v
);
753 xen_store_mfn
= (unsigned long)v
;
754 xen_store_interface
= ioremap(xen_store_mfn
<< PAGE_SHIFT
, PAGE_SIZE
);
756 xen_store_evtchn
= xen_start_info
->store_evtchn
;
757 xen_store_mfn
= xen_start_info
->store_mfn
;
758 xen_store_interface
= mfn_to_virt(xen_store_mfn
);
763 /* Initialize the interface to xenstore. */
767 "XENBUS: Error initializing xenstore comms: %i\n", err
);
771 #ifdef CONFIG_XEN_COMPAT_XENFS
773 * Create xenfs mountpoint in /proc for compatibility with
774 * utilities that expect to find "xenbus" under "/proc/xen".
776 proc_mkdir("xen", NULL
);
779 printk(KERN_CRIT
"%s ok\n", __func__
);
789 postcore_initcall(xenbus_init
);
791 MODULE_LICENSE("GPL");