blob: b521ce43d32555171d70842a41f1e8b4bf017c16 [file] [log] [blame]
Ian Campbell2de06cc2009-02-09 12:05:51 -08001#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> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000049static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
50 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080051{
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
Ian Campbell6bac7f92010-12-10 14:39:15 +000066static int xenbus_uevent_frontend(struct device *_dev,
67 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080068{
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
Ian Campbell2de06cc2009-02-09 12:05:51 -080078static 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
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080088static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -080089 .suspend = xenbus_dev_suspend,
90 .resume = xenbus_dev_resume,
91 .freeze = xenbus_dev_suspend,
92 .thaw = xenbus_dev_cancel,
93 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080094};
95
Ian Campbell2de06cc2009-02-09 12:05:51 -080096static struct xen_bus_type xenbus_frontend = {
97 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +000098 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -080099 .get_bus_id = frontend_bus_id,
100 .probe = xenbus_probe_frontend,
101 .otherend_changed = backend_changed,
102 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000103 .name = "xen",
104 .match = xenbus_match,
105 .uevent = xenbus_uevent_frontend,
106 .probe = xenbus_dev_probe,
107 .remove = xenbus_dev_remove,
108 .shutdown = xenbus_dev_shutdown,
109 .dev_attrs = xenbus_frontend_dev_attrs,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800110
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800111 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800112 },
113};
114
115static void frontend_changed(struct xenbus_watch *watch,
116 const char **vec, unsigned int len)
117{
118 DPRINTK("");
119
120 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
121}
122
123
124/* We watch for devices appearing and vanishing. */
125static struct xenbus_watch fe_watch = {
126 .node = "device",
127 .callback = frontend_changed,
128};
129
130static int read_backend_details(struct xenbus_device *xendev)
131{
132 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
133}
134
135static int is_device_connecting(struct device *dev, void *data)
136{
137 struct xenbus_device *xendev = to_xenbus_device(dev);
138 struct device_driver *drv = data;
139 struct xenbus_driver *xendrv;
140
141 /*
142 * A device with no driver will never connect. We care only about
143 * devices which should currently be in the process of connecting.
144 */
145 if (!dev->driver)
146 return 0;
147
148 /* Is this search limited to a particular driver? */
149 if (drv && (dev->driver != drv))
150 return 0;
151
152 xendrv = to_xenbus_driver(dev->driver);
153 return (xendev->state < XenbusStateConnected ||
154 (xendev->state == XenbusStateConnected &&
155 xendrv->is_ready && !xendrv->is_ready(xendev)));
156}
157
158static int exists_connecting_device(struct device_driver *drv)
159{
160 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
161 is_device_connecting);
162}
163
164static int print_device_status(struct device *dev, void *data)
165{
166 struct xenbus_device *xendev = to_xenbus_device(dev);
167 struct device_driver *drv = data;
168
169 /* Is this operation limited to a particular driver? */
170 if (drv && (dev->driver != drv))
171 return 0;
172
173 if (!dev->driver) {
174 /* Information only: is this too noisy? */
175 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
176 xendev->nodename);
177 } else if (xendev->state < XenbusStateConnected) {
178 enum xenbus_state rstate = XenbusStateUnknown;
179 if (xendev->otherend)
180 rstate = xenbus_read_driver_state(xendev->otherend);
181 printk(KERN_WARNING "XENBUS: Timeout connecting "
182 "to device: %s (local state %d, remote state %d)\n",
183 xendev->nodename, xendev->state, rstate);
184 }
185
186 return 0;
187}
188
189/* We only wait for device setup after most initcalls have run. */
190static int ready_to_wait_for_devices;
191
192/*
193 * On a 5-minute timeout, wait for all devices currently configured. We need
194 * to do this to guarantee that the filesystems and / or network devices
195 * needed for boot are available, before we can allow the boot to proceed.
196 *
197 * This needs to be on a late_initcall, to happen after the frontend device
198 * drivers have been initialised, but before the root fs is mounted.
199 *
200 * A possible improvement here would be to have the tools add a per-device
201 * flag to the store entry, indicating whether it is needed at boot time.
202 * This would allow people who knew what they were doing to accelerate their
203 * boot slightly, but of course needs tools or manual intervention to set up
204 * those flags correctly.
205 */
206static void wait_for_devices(struct xenbus_driver *xendrv)
207{
208 unsigned long start = jiffies;
209 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
210 unsigned int seconds_waited = 0;
211
212 if (!ready_to_wait_for_devices || !xen_domain())
213 return;
214
215 while (exists_connecting_device(drv)) {
216 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
217 if (!seconds_waited)
218 printk(KERN_WARNING "XENBUS: Waiting for "
219 "devices to initialise: ");
220 seconds_waited += 5;
221 printk("%us...", 300 - seconds_waited);
222 if (seconds_waited == 300)
223 break;
224 }
225
226 schedule_timeout_interruptible(HZ/10);
227 }
228
229 if (seconds_waited)
230 printk("\n");
231
232 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
233 print_device_status);
234}
235
236int __xenbus_register_frontend(struct xenbus_driver *drv,
237 struct module *owner, const char *mod_name)
238{
239 int ret;
240
241 drv->read_otherend_details = read_backend_details;
242
243 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
244 owner, mod_name);
245 if (ret)
246 return ret;
247
248 /* If this driver is loaded as a module wait for devices to attach. */
249 wait_for_devices(drv);
250
251 return 0;
252}
253EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
254
Olaf Hering116df6f2011-08-25 18:34:45 +0200255static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
256static int backend_state;
257
258static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
259 const char **v, unsigned int l)
260{
261 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
262 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
263 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
264 wake_up(&backend_state_wq);
265}
266
267static void xenbus_reset_wait_for_backend(char *be, int expected)
268{
269 long timeout;
270 timeout = wait_event_interruptible_timeout(backend_state_wq,
271 backend_state == expected, 5 * HZ);
272 if (timeout <= 0)
273 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
274}
275
276/*
277 * Reset frontend if it is in Connected or Closed state.
278 * Wait for backend to catch up.
279 * State Connected happens during kdump, Closed after kexec.
280 */
281static void xenbus_reset_frontend(char *fe, char *be, int be_state)
282{
283 struct xenbus_watch be_watch;
284
285 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
286 be, xenbus_strstate(be_state));
287
288 memset(&be_watch, 0, sizeof(be_watch));
289 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
290 if (!be_watch.node)
291 return;
292
293 be_watch.callback = xenbus_reset_backend_state_changed;
294 backend_state = XenbusStateUnknown;
295
296 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
297 register_xenbus_watch(&be_watch);
298
299 /* fall through to forward backend to state XenbusStateInitialising */
300 switch (be_state) {
301 case XenbusStateConnected:
302 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
303 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
304
305 case XenbusStateClosing:
306 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
307 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
308
309 case XenbusStateClosed:
310 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
311 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
312 }
313
314 unregister_xenbus_watch(&be_watch);
315 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
316 kfree(be_watch.node);
317}
318
319static void xenbus_check_frontend(char *class, char *dev)
320{
321 int be_state, fe_state, err;
322 char *backend, *frontend;
323
324 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
325 if (!frontend)
326 return;
327
328 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
329 if (err != 1)
330 goto out;
331
332 switch (fe_state) {
333 case XenbusStateConnected:
334 case XenbusStateClosed:
335 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
336 frontend, xenbus_strstate(fe_state));
337 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
338 if (!backend || IS_ERR(backend))
339 goto out;
340 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
341 if (err == 1)
342 xenbus_reset_frontend(frontend, backend, be_state);
343 kfree(backend);
344 break;
345 default:
346 break;
347 }
348out:
349 kfree(frontend);
350}
351
352static void xenbus_reset_state(void)
353{
354 char **devclass, **dev;
355 int devclass_n, dev_n;
356 int i, j;
357
358 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
359 if (IS_ERR(devclass))
360 return;
361
362 for (i = 0; i < devclass_n; i++) {
363 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
364 if (IS_ERR(dev))
365 continue;
366 for (j = 0; j < dev_n; j++)
367 xenbus_check_frontend(devclass[i], dev[j]);
368 kfree(dev);
369 }
370 kfree(devclass);
371}
372
Ian Campbelldf660252009-02-09 12:05:51 -0800373static int frontend_probe_and_watch(struct notifier_block *notifier,
374 unsigned long event,
375 void *data)
376{
Olaf Hering116df6f2011-08-25 18:34:45 +0200377 /* reset devices in Connected or Closed state */
378 if (xen_hvm_domain())
379 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800380 /* Enumerate devices in xenstore and watch for changes. */
381 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800382 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700383
Ian Campbelldf660252009-02-09 12:05:51 -0800384 return NOTIFY_DONE;
385}
386
387
Ian Campbell2de06cc2009-02-09 12:05:51 -0800388static int __init xenbus_probe_frontend_init(void)
389{
Ian Campbelldf660252009-02-09 12:05:51 -0800390 static struct notifier_block xenstore_notifier = {
391 .notifier_call = frontend_probe_and_watch
392 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800393 int err;
394
395 DPRINTK("");
396
397 /* Register ourselves with the kernel bus subsystem */
398 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700399 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800400 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800401
Ian Campbelldf660252009-02-09 12:05:51 -0800402 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800403
404 return 0;
405}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800406subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800407
408#ifndef MODULE
409static int __init boot_wait_for_devices(void)
410{
411 if (xen_hvm_domain() && !xen_platform_pci_unplug)
412 return -ENODEV;
413
414 ready_to_wait_for_devices = 1;
415 wait_for_devices(NULL);
416 return 0;
417}
418
419late_initcall(boot_wait_for_devices);
420#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700421
422MODULE_LICENSE("GPL");