]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/xen/xenbus/xenbus_probe_frontend.c
xen: xenbus PM events support
[mirror_ubuntu-artful-kernel.git] / drivers / xen / xenbus / xenbus_probe_frontend.c
CommitLineData
2de06cc1
IC
1#define DPRINTK(fmt, args...) \
2 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3 __func__, __LINE__, ##args)
4
5#include <linux/kernel.h>
6#include <linux/err.h>
7#include <linux/string.h>
8#include <linux/ctype.h>
9#include <linux/fcntl.h>
10#include <linux/mm.h>
11#include <linux/proc_fs.h>
12#include <linux/notifier.h>
13#include <linux/kthread.h>
14#include <linux/mutex.h>
15#include <linux/io.h>
16
17#include <asm/page.h>
18#include <asm/pgtable.h>
19#include <asm/xen/hypervisor.h>
20#include <xen/xenbus.h>
21#include <xen/events.h>
22#include <xen/page.h>
23
24#include <xen/platform_pci.h>
25
26#include "xenbus_comms.h"
27#include "xenbus_probe.h"
28
29
30/* device/<type>/<id> => <type>-<id> */
31static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
32{
33 nodename = strchr(nodename, '/');
34 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
35 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
36 return -EINVAL;
37 }
38
39 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
40 if (!strchr(bus_id, '/')) {
41 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
42 return -EINVAL;
43 }
44 *strchr(bus_id, '/') = '-';
45 return 0;
46}
47
48/* device/<typename>/<name> */
6bac7f9f
IC
49static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
50 const char *name)
2de06cc1
IC
51{
52 char *nodename;
53 int err;
54
55 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
56 if (!nodename)
57 return -ENOMEM;
58
59 DPRINTK("%s", nodename);
60
61 err = xenbus_probe_node(bus, type, nodename);
62 kfree(nodename);
63 return err;
64}
65
6bac7f9f
IC
66static int xenbus_uevent_frontend(struct device *_dev,
67 struct kobj_uevent_env *env)
df660251
IC
68{
69 struct xenbus_device *dev = to_xenbus_device(_dev);
70
71 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
72 return -ENOMEM;
73
74 return 0;
75}
76
77
2de06cc1
IC
78static void backend_changed(struct xenbus_watch *watch,
79 const char **vec, unsigned int len)
80{
81 xenbus_otherend_changed(watch, vec, len, 1);
82}
83
84static struct device_attribute xenbus_frontend_dev_attrs[] = {
85 __ATTR_NULL
86};
87
c7853aea
KS
88static const struct dev_pm_ops xenbus_pm_ops = {
89 .suspend = xenbus_dev_suspend,
90 .resume = xenbus_dev_resume,
91 .thaw = xenbus_dev_cancel,
92};
93
2de06cc1
IC
94static struct xen_bus_type xenbus_frontend = {
95 .root = "device",
6bac7f9f 96 .levels = 2, /* device/type/<id> */
2de06cc1
IC
97 .get_bus_id = frontend_bus_id,
98 .probe = xenbus_probe_frontend,
99 .otherend_changed = backend_changed,
100 .bus = {
6bac7f9f
IC
101 .name = "xen",
102 .match = xenbus_match,
103 .uevent = xenbus_uevent_frontend,
104 .probe = xenbus_dev_probe,
105 .remove = xenbus_dev_remove,
106 .shutdown = xenbus_dev_shutdown,
107 .dev_attrs = xenbus_frontend_dev_attrs,
108
c7853aea 109 .pm = &xenbus_pm_ops,
2de06cc1
IC
110 },
111};
112
113static void frontend_changed(struct xenbus_watch *watch,
114 const char **vec, unsigned int len)
115{
116 DPRINTK("");
117
118 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
119}
120
121
122/* We watch for devices appearing and vanishing. */
123static struct xenbus_watch fe_watch = {
124 .node = "device",
125 .callback = frontend_changed,
126};
127
128static int read_backend_details(struct xenbus_device *xendev)
129{
130 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
131}
132
133static int is_device_connecting(struct device *dev, void *data)
134{
135 struct xenbus_device *xendev = to_xenbus_device(dev);
136 struct device_driver *drv = data;
137 struct xenbus_driver *xendrv;
138
139 /*
140 * A device with no driver will never connect. We care only about
141 * devices which should currently be in the process of connecting.
142 */
143 if (!dev->driver)
144 return 0;
145
146 /* Is this search limited to a particular driver? */
147 if (drv && (dev->driver != drv))
148 return 0;
149
150 xendrv = to_xenbus_driver(dev->driver);
151 return (xendev->state < XenbusStateConnected ||
152 (xendev->state == XenbusStateConnected &&
153 xendrv->is_ready && !xendrv->is_ready(xendev)));
154}
155
156static int exists_connecting_device(struct device_driver *drv)
157{
158 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
159 is_device_connecting);
160}
161
162static int print_device_status(struct device *dev, void *data)
163{
164 struct xenbus_device *xendev = to_xenbus_device(dev);
165 struct device_driver *drv = data;
166
167 /* Is this operation limited to a particular driver? */
168 if (drv && (dev->driver != drv))
169 return 0;
170
171 if (!dev->driver) {
172 /* Information only: is this too noisy? */
173 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
174 xendev->nodename);
175 } else if (xendev->state < XenbusStateConnected) {
176 enum xenbus_state rstate = XenbusStateUnknown;
177 if (xendev->otherend)
178 rstate = xenbus_read_driver_state(xendev->otherend);
179 printk(KERN_WARNING "XENBUS: Timeout connecting "
180 "to device: %s (local state %d, remote state %d)\n",
181 xendev->nodename, xendev->state, rstate);
182 }
183
184 return 0;
185}
186
187/* We only wait for device setup after most initcalls have run. */
188static int ready_to_wait_for_devices;
189
190/*
191 * On a 5-minute timeout, wait for all devices currently configured. We need
192 * to do this to guarantee that the filesystems and / or network devices
193 * needed for boot are available, before we can allow the boot to proceed.
194 *
195 * This needs to be on a late_initcall, to happen after the frontend device
196 * drivers have been initialised, but before the root fs is mounted.
197 *
198 * A possible improvement here would be to have the tools add a per-device
199 * flag to the store entry, indicating whether it is needed at boot time.
200 * This would allow people who knew what they were doing to accelerate their
201 * boot slightly, but of course needs tools or manual intervention to set up
202 * those flags correctly.
203 */
204static void wait_for_devices(struct xenbus_driver *xendrv)
205{
206 unsigned long start = jiffies;
207 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
208 unsigned int seconds_waited = 0;
209
210 if (!ready_to_wait_for_devices || !xen_domain())
211 return;
212
213 while (exists_connecting_device(drv)) {
214 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
215 if (!seconds_waited)
216 printk(KERN_WARNING "XENBUS: Waiting for "
217 "devices to initialise: ");
218 seconds_waited += 5;
219 printk("%us...", 300 - seconds_waited);
220 if (seconds_waited == 300)
221 break;
222 }
223
224 schedule_timeout_interruptible(HZ/10);
225 }
226
227 if (seconds_waited)
228 printk("\n");
229
230 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
231 print_device_status);
232}
233
234int __xenbus_register_frontend(struct xenbus_driver *drv,
235 struct module *owner, const char *mod_name)
236{
237 int ret;
238
239 drv->read_otherend_details = read_backend_details;
240
241 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
242 owner, mod_name);
243 if (ret)
244 return ret;
245
246 /* If this driver is loaded as a module wait for devices to attach. */
247 wait_for_devices(drv);
248
249 return 0;
250}
251EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
252
df660251
IC
253static int frontend_probe_and_watch(struct notifier_block *notifier,
254 unsigned long event,
255 void *data)
256{
257 /* Enumerate devices in xenstore and watch for changes. */
258 xenbus_probe_devices(&xenbus_frontend);
df660251 259 register_xenbus_watch(&fe_watch);
0ff4fdf0 260
df660251
IC
261 return NOTIFY_DONE;
262}
263
264
2de06cc1
IC
265static int __init xenbus_probe_frontend_init(void)
266{
df660251
IC
267 static struct notifier_block xenstore_notifier = {
268 .notifier_call = frontend_probe_and_watch
269 };
2de06cc1
IC
270 int err;
271
272 DPRINTK("");
273
274 /* Register ourselves with the kernel bus subsystem */
275 err = bus_register(&xenbus_frontend.bus);
0ff4fdf0 276 if (err)
2de06cc1 277 return err;
2de06cc1 278
df660251 279 register_xenstore_notifier(&xenstore_notifier);
2de06cc1
IC
280
281 return 0;
282}
806f5463 283subsys_initcall(xenbus_probe_frontend_init);
2de06cc1
IC
284
285#ifndef MODULE
286static int __init boot_wait_for_devices(void)
287{
288 if (xen_hvm_domain() && !xen_platform_pci_unplug)
289 return -ENODEV;
290
291 ready_to_wait_for_devices = 1;
292 wait_for_devices(NULL);
293 return 0;
294}
295
296late_initcall(boot_wait_for_devices);
297#endif
1b31a143
JF
298
299MODULE_LICENSE("GPL");