blob: 9c57819df51ae6fbd10de6c78c5f5916cee6a9f0 [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>
Paul Gortmaker72ee5112011-07-03 16:20:57 -040016#include <linux/module.h>
Ian Campbell2de06cc2009-02-09 12:05:51 -080017
18#include <asm/page.h>
19#include <asm/pgtable.h>
20#include <asm/xen/hypervisor.h>
21#include <xen/xenbus.h>
22#include <xen/events.h>
23#include <xen/page.h>
24
25#include <xen/platform_pci.h>
26
27#include "xenbus_comms.h"
28#include "xenbus_probe.h"
29
30
31/* device/<type>/<id> => <type>-<id> */
32static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
33{
34 nodename = strchr(nodename, '/');
35 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
36 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
37 return -EINVAL;
38 }
39
40 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
41 if (!strchr(bus_id, '/')) {
42 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
43 return -EINVAL;
44 }
45 *strchr(bus_id, '/') = '-';
46 return 0;
47}
48
49/* device/<typename>/<name> */
Ian Campbell6bac7f92010-12-10 14:39:15 +000050static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
51 const char *name)
Ian Campbell2de06cc2009-02-09 12:05:51 -080052{
53 char *nodename;
54 int err;
55
56 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
57 if (!nodename)
58 return -ENOMEM;
59
60 DPRINTK("%s", nodename);
61
62 err = xenbus_probe_node(bus, type, nodename);
63 kfree(nodename);
64 return err;
65}
66
Ian Campbell6bac7f92010-12-10 14:39:15 +000067static int xenbus_uevent_frontend(struct device *_dev,
68 struct kobj_uevent_env *env)
Ian Campbelldf660252009-02-09 12:05:51 -080069{
70 struct xenbus_device *dev = to_xenbus_device(_dev);
71
72 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
73 return -ENOMEM;
74
75 return 0;
76}
77
78
Ian Campbell2de06cc2009-02-09 12:05:51 -080079static void backend_changed(struct xenbus_watch *watch,
80 const char **vec, unsigned int len)
81{
82 xenbus_otherend_changed(watch, vec, len, 1);
83}
84
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080085static const struct dev_pm_ops xenbus_pm_ops = {
Shriram Rajagopalanb3e96c02011-02-22 14:59:06 -080086 .suspend = xenbus_dev_suspend,
87 .resume = xenbus_dev_resume,
88 .freeze = xenbus_dev_suspend,
89 .thaw = xenbus_dev_cancel,
90 .restore = xenbus_dev_resume,
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -080091};
92
Ian Campbell2de06cc2009-02-09 12:05:51 -080093static struct xen_bus_type xenbus_frontend = {
94 .root = "device",
Ian Campbell6bac7f92010-12-10 14:39:15 +000095 .levels = 2, /* device/type/<id> */
Ian Campbell2de06cc2009-02-09 12:05:51 -080096 .get_bus_id = frontend_bus_id,
97 .probe = xenbus_probe_frontend,
98 .otherend_changed = backend_changed,
99 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000100 .name = "xen",
101 .match = xenbus_match,
102 .uevent = xenbus_uevent_frontend,
103 .probe = xenbus_dev_probe,
104 .remove = xenbus_dev_remove,
105 .shutdown = xenbus_dev_shutdown,
Bastian Blankcc85e932011-06-29 14:39:26 +0200106 .dev_attrs = xenbus_dev_attrs,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800107
Kazuhiro SUZUKIc7853ae2011-02-18 14:43:07 -0800108 .pm = &xenbus_pm_ops,
Ian Campbell2de06cc2009-02-09 12:05:51 -0800109 },
110};
111
112static void frontend_changed(struct xenbus_watch *watch,
113 const char **vec, unsigned int len)
114{
115 DPRINTK("");
116
117 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
118}
119
120
121/* We watch for devices appearing and vanishing. */
122static struct xenbus_watch fe_watch = {
123 .node = "device",
124 .callback = frontend_changed,
125};
126
127static int read_backend_details(struct xenbus_device *xendev)
128{
129 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
130}
131
132static int is_device_connecting(struct device *dev, void *data)
133{
134 struct xenbus_device *xendev = to_xenbus_device(dev);
135 struct device_driver *drv = data;
136 struct xenbus_driver *xendrv;
137
138 /*
139 * A device with no driver will never connect. We care only about
140 * devices which should currently be in the process of connecting.
141 */
142 if (!dev->driver)
143 return 0;
144
145 /* Is this search limited to a particular driver? */
146 if (drv && (dev->driver != drv))
147 return 0;
148
149 xendrv = to_xenbus_driver(dev->driver);
150 return (xendev->state < XenbusStateConnected ||
151 (xendev->state == XenbusStateConnected &&
152 xendrv->is_ready && !xendrv->is_ready(xendev)));
153}
154
155static int exists_connecting_device(struct device_driver *drv)
156{
157 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
158 is_device_connecting);
159}
160
161static int print_device_status(struct device *dev, void *data)
162{
163 struct xenbus_device *xendev = to_xenbus_device(dev);
164 struct device_driver *drv = data;
165
166 /* Is this operation limited to a particular driver? */
167 if (drv && (dev->driver != drv))
168 return 0;
169
170 if (!dev->driver) {
171 /* Information only: is this too noisy? */
172 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
173 xendev->nodename);
174 } else if (xendev->state < XenbusStateConnected) {
175 enum xenbus_state rstate = XenbusStateUnknown;
176 if (xendev->otherend)
177 rstate = xenbus_read_driver_state(xendev->otherend);
178 printk(KERN_WARNING "XENBUS: Timeout connecting "
179 "to device: %s (local state %d, remote state %d)\n",
180 xendev->nodename, xendev->state, rstate);
181 }
182
183 return 0;
184}
185
186/* We only wait for device setup after most initcalls have run. */
187static int ready_to_wait_for_devices;
188
189/*
190 * On a 5-minute timeout, wait for all devices currently configured. We need
191 * to do this to guarantee that the filesystems and / or network devices
192 * needed for boot are available, before we can allow the boot to proceed.
193 *
194 * This needs to be on a late_initcall, to happen after the frontend device
195 * drivers have been initialised, but before the root fs is mounted.
196 *
197 * A possible improvement here would be to have the tools add a per-device
198 * flag to the store entry, indicating whether it is needed at boot time.
199 * This would allow people who knew what they were doing to accelerate their
200 * boot slightly, but of course needs tools or manual intervention to set up
201 * those flags correctly.
202 */
203static void wait_for_devices(struct xenbus_driver *xendrv)
204{
205 unsigned long start = jiffies;
206 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
207 unsigned int seconds_waited = 0;
208
209 if (!ready_to_wait_for_devices || !xen_domain())
210 return;
211
212 while (exists_connecting_device(drv)) {
213 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
214 if (!seconds_waited)
215 printk(KERN_WARNING "XENBUS: Waiting for "
216 "devices to initialise: ");
217 seconds_waited += 5;
218 printk("%us...", 300 - seconds_waited);
219 if (seconds_waited == 300)
220 break;
221 }
222
223 schedule_timeout_interruptible(HZ/10);
224 }
225
226 if (seconds_waited)
227 printk("\n");
228
229 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
230 print_device_status);
231}
232
Jan Beulich73db1442011-12-22 09:08:13 +0000233int xenbus_register_frontend(struct xenbus_driver *drv)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800234{
235 int ret;
236
237 drv->read_otherend_details = read_backend_details;
238
Jan Beulich73db1442011-12-22 09:08:13 +0000239 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800240 if (ret)
241 return ret;
242
243 /* If this driver is loaded as a module wait for devices to attach. */
244 wait_for_devices(drv);
245
246 return 0;
247}
Jan Beulich73db1442011-12-22 09:08:13 +0000248EXPORT_SYMBOL_GPL(xenbus_register_frontend);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800249
Olaf Hering116df6f2011-08-25 18:34:45 +0200250static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
251static int backend_state;
252
253static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
254 const char **v, unsigned int l)
255{
256 xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
257 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
258 v[XS_WATCH_PATH], xenbus_strstate(backend_state));
259 wake_up(&backend_state_wq);
260}
261
262static void xenbus_reset_wait_for_backend(char *be, int expected)
263{
264 long timeout;
265 timeout = wait_event_interruptible_timeout(backend_state_wq,
266 backend_state == expected, 5 * HZ);
267 if (timeout <= 0)
268 printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
269}
270
271/*
272 * Reset frontend if it is in Connected or Closed state.
273 * Wait for backend to catch up.
274 * State Connected happens during kdump, Closed after kexec.
275 */
276static void xenbus_reset_frontend(char *fe, char *be, int be_state)
277{
278 struct xenbus_watch be_watch;
279
280 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
281 be, xenbus_strstate(be_state));
282
283 memset(&be_watch, 0, sizeof(be_watch));
284 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
285 if (!be_watch.node)
286 return;
287
288 be_watch.callback = xenbus_reset_backend_state_changed;
289 backend_state = XenbusStateUnknown;
290
291 printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
292 register_xenbus_watch(&be_watch);
293
294 /* fall through to forward backend to state XenbusStateInitialising */
295 switch (be_state) {
296 case XenbusStateConnected:
297 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
298 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
299
300 case XenbusStateClosing:
301 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
302 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
303
304 case XenbusStateClosed:
305 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
306 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
307 }
308
309 unregister_xenbus_watch(&be_watch);
310 printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
311 kfree(be_watch.node);
312}
313
314static void xenbus_check_frontend(char *class, char *dev)
315{
316 int be_state, fe_state, err;
317 char *backend, *frontend;
318
319 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
320 if (!frontend)
321 return;
322
323 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
324 if (err != 1)
325 goto out;
326
327 switch (fe_state) {
328 case XenbusStateConnected:
329 case XenbusStateClosed:
330 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
331 frontend, xenbus_strstate(fe_state));
332 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
333 if (!backend || IS_ERR(backend))
334 goto out;
335 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
336 if (err == 1)
337 xenbus_reset_frontend(frontend, backend, be_state);
338 kfree(backend);
339 break;
340 default:
341 break;
342 }
343out:
344 kfree(frontend);
345}
346
347static void xenbus_reset_state(void)
348{
349 char **devclass, **dev;
350 int devclass_n, dev_n;
351 int i, j;
352
353 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
354 if (IS_ERR(devclass))
355 return;
356
357 for (i = 0; i < devclass_n; i++) {
358 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
359 if (IS_ERR(dev))
360 continue;
361 for (j = 0; j < dev_n; j++)
362 xenbus_check_frontend(devclass[i], dev[j]);
363 kfree(dev);
364 }
365 kfree(devclass);
366}
367
Ian Campbelldf660252009-02-09 12:05:51 -0800368static int frontend_probe_and_watch(struct notifier_block *notifier,
369 unsigned long event,
370 void *data)
371{
Olaf Hering116df6f2011-08-25 18:34:45 +0200372 /* reset devices in Connected or Closed state */
373 if (xen_hvm_domain())
374 xenbus_reset_state();
Ian Campbelldf660252009-02-09 12:05:51 -0800375 /* Enumerate devices in xenstore and watch for changes. */
376 xenbus_probe_devices(&xenbus_frontend);
Ian Campbelldf660252009-02-09 12:05:51 -0800377 register_xenbus_watch(&fe_watch);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700378
Ian Campbelldf660252009-02-09 12:05:51 -0800379 return NOTIFY_DONE;
380}
381
382
Ian Campbell2de06cc2009-02-09 12:05:51 -0800383static int __init xenbus_probe_frontend_init(void)
384{
Ian Campbelldf660252009-02-09 12:05:51 -0800385 static struct notifier_block xenstore_notifier = {
386 .notifier_call = frontend_probe_and_watch
387 };
Ian Campbell2de06cc2009-02-09 12:05:51 -0800388 int err;
389
390 DPRINTK("");
391
392 /* Register ourselves with the kernel bus subsystem */
393 err = bus_register(&xenbus_frontend.bus);
Jeremy Fitzhardinge0ff4fdf2010-03-29 14:38:54 -0700394 if (err)
Ian Campbell2de06cc2009-02-09 12:05:51 -0800395 return err;
Ian Campbell2de06cc2009-02-09 12:05:51 -0800396
Ian Campbelldf660252009-02-09 12:05:51 -0800397 register_xenstore_notifier(&xenstore_notifier);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800398
399 return 0;
400}
Jeremy Fitzhardinge806f5462009-03-04 22:31:45 -0800401subsys_initcall(xenbus_probe_frontend_init);
Ian Campbell2de06cc2009-02-09 12:05:51 -0800402
403#ifndef MODULE
404static int __init boot_wait_for_devices(void)
405{
406 if (xen_hvm_domain() && !xen_platform_pci_unplug)
407 return -ENODEV;
408
409 ready_to_wait_for_devices = 1;
410 wait_for_devices(NULL);
411 return 0;
412}
413
414late_initcall(boot_wait_for_devices);
415#endif
Jeremy Fitzhardinge1b31a142009-03-27 16:29:44 -0700416
417MODULE_LICENSE("GPL");