Commit | Line | Data |
---|---|---|
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> | |
72ee5112 | 16 | #include <linux/module.h> |
2de06cc1 IC |
17 | |
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> */ | |
32 | static 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> */ | |
6bac7f9f IC |
50 | static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, |
51 | const char *name) | |
2de06cc1 IC |
52 | { |
53 | char *nodename; | |
54 | int err; | |
55 | ||
42c46e6b SS |
56 | /* ignore console/0 */ |
57 | if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) { | |
58 | DPRINTK("Ignoring buggy device entry console/0"); | |
59 | return 0; | |
60 | } | |
61 | ||
2de06cc1 IC |
62 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name); |
63 | if (!nodename) | |
64 | return -ENOMEM; | |
65 | ||
66 | DPRINTK("%s", nodename); | |
67 | ||
68 | err = xenbus_probe_node(bus, type, nodename); | |
69 | kfree(nodename); | |
70 | return err; | |
71 | } | |
72 | ||
6bac7f9f IC |
73 | static int xenbus_uevent_frontend(struct device *_dev, |
74 | struct kobj_uevent_env *env) | |
df660251 IC |
75 | { |
76 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
77 | ||
78 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) | |
79 | return -ENOMEM; | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | ||
2de06cc1 IC |
85 | static void backend_changed(struct xenbus_watch *watch, |
86 | const char **vec, unsigned int len) | |
87 | { | |
88 | xenbus_otherend_changed(watch, vec, len, 1); | |
89 | } | |
90 | ||
c7853aea | 91 | static const struct dev_pm_ops xenbus_pm_ops = { |
b3e96c0c SR |
92 | .suspend = xenbus_dev_suspend, |
93 | .resume = xenbus_dev_resume, | |
94 | .freeze = xenbus_dev_suspend, | |
95 | .thaw = xenbus_dev_cancel, | |
96 | .restore = xenbus_dev_resume, | |
c7853aea KS |
97 | }; |
98 | ||
2de06cc1 IC |
99 | static struct xen_bus_type xenbus_frontend = { |
100 | .root = "device", | |
6bac7f9f | 101 | .levels = 2, /* device/type/<id> */ |
2de06cc1 IC |
102 | .get_bus_id = frontend_bus_id, |
103 | .probe = xenbus_probe_frontend, | |
104 | .otherend_changed = backend_changed, | |
105 | .bus = { | |
6bac7f9f IC |
106 | .name = "xen", |
107 | .match = xenbus_match, | |
108 | .uevent = xenbus_uevent_frontend, | |
109 | .probe = xenbus_dev_probe, | |
110 | .remove = xenbus_dev_remove, | |
111 | .shutdown = xenbus_dev_shutdown, | |
cc85e933 | 112 | .dev_attrs = xenbus_dev_attrs, |
6bac7f9f | 113 | |
c7853aea | 114 | .pm = &xenbus_pm_ops, |
2de06cc1 IC |
115 | }, |
116 | }; | |
117 | ||
118 | static void frontend_changed(struct xenbus_watch *watch, | |
119 | const char **vec, unsigned int len) | |
120 | { | |
121 | DPRINTK(""); | |
122 | ||
123 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | |
124 | } | |
125 | ||
126 | ||
127 | /* We watch for devices appearing and vanishing. */ | |
128 | static struct xenbus_watch fe_watch = { | |
129 | .node = "device", | |
130 | .callback = frontend_changed, | |
131 | }; | |
132 | ||
133 | static int read_backend_details(struct xenbus_device *xendev) | |
134 | { | |
135 | return xenbus_read_otherend_details(xendev, "backend-id", "backend"); | |
136 | } | |
137 | ||
3066616c | 138 | static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential) |
2de06cc1 IC |
139 | { |
140 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
141 | struct device_driver *drv = data; | |
142 | struct xenbus_driver *xendrv; | |
143 | ||
144 | /* | |
145 | * A device with no driver will never connect. We care only about | |
146 | * devices which should currently be in the process of connecting. | |
147 | */ | |
148 | if (!dev->driver) | |
149 | return 0; | |
150 | ||
151 | /* Is this search limited to a particular driver? */ | |
152 | if (drv && (dev->driver != drv)) | |
153 | return 0; | |
154 | ||
3066616c KRW |
155 | if (ignore_nonessential) { |
156 | /* With older QEMU, for PVonHVM guests the guest config files | |
157 | * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0'] | |
158 | * which is nonsensical as there is no PV FB (there can be | |
159 | * a PVKB) running as HVM guest. */ | |
160 | ||
161 | if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0)) | |
162 | return 0; | |
163 | ||
164 | if ((strncmp(xendev->nodename, "device/vfb", 10) == 0)) | |
165 | return 0; | |
166 | } | |
2de06cc1 IC |
167 | xendrv = to_xenbus_driver(dev->driver); |
168 | return (xendev->state < XenbusStateConnected || | |
169 | (xendev->state == XenbusStateConnected && | |
170 | xendrv->is_ready && !xendrv->is_ready(xendev))); | |
171 | } | |
3066616c KRW |
172 | static int essential_device_connecting(struct device *dev, void *data) |
173 | { | |
174 | return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */); | |
175 | } | |
176 | static int non_essential_device_connecting(struct device *dev, void *data) | |
177 | { | |
178 | return is_device_connecting(dev, data, false); | |
179 | } | |
2de06cc1 | 180 | |
3066616c | 181 | static int exists_essential_connecting_device(struct device_driver *drv) |
2de06cc1 IC |
182 | { |
183 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
3066616c KRW |
184 | essential_device_connecting); |
185 | } | |
186 | static int exists_non_essential_connecting_device(struct device_driver *drv) | |
187 | { | |
188 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
189 | non_essential_device_connecting); | |
2de06cc1 IC |
190 | } |
191 | ||
192 | static int print_device_status(struct device *dev, void *data) | |
193 | { | |
194 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
195 | struct device_driver *drv = data; | |
196 | ||
197 | /* Is this operation limited to a particular driver? */ | |
198 | if (drv && (dev->driver != drv)) | |
199 | return 0; | |
200 | ||
201 | if (!dev->driver) { | |
202 | /* Information only: is this too noisy? */ | |
203 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | |
204 | xendev->nodename); | |
205 | } else if (xendev->state < XenbusStateConnected) { | |
206 | enum xenbus_state rstate = XenbusStateUnknown; | |
207 | if (xendev->otherend) | |
208 | rstate = xenbus_read_driver_state(xendev->otherend); | |
209 | printk(KERN_WARNING "XENBUS: Timeout connecting " | |
210 | "to device: %s (local state %d, remote state %d)\n", | |
211 | xendev->nodename, xendev->state, rstate); | |
212 | } | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | /* We only wait for device setup after most initcalls have run. */ | |
218 | static int ready_to_wait_for_devices; | |
219 | ||
3066616c KRW |
220 | static bool wait_loop(unsigned long start, unsigned int max_delay, |
221 | unsigned int *seconds_waited) | |
222 | { | |
223 | if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) { | |
224 | if (!*seconds_waited) | |
225 | printk(KERN_WARNING "XENBUS: Waiting for " | |
226 | "devices to initialise: "); | |
227 | *seconds_waited += 5; | |
228 | printk("%us...", max_delay - *seconds_waited); | |
229 | if (*seconds_waited == max_delay) | |
230 | return true; | |
231 | } | |
232 | ||
233 | schedule_timeout_interruptible(HZ/10); | |
234 | ||
235 | return false; | |
236 | } | |
2de06cc1 IC |
237 | /* |
238 | * On a 5-minute timeout, wait for all devices currently configured. We need | |
239 | * to do this to guarantee that the filesystems and / or network devices | |
240 | * needed for boot are available, before we can allow the boot to proceed. | |
241 | * | |
242 | * This needs to be on a late_initcall, to happen after the frontend device | |
243 | * drivers have been initialised, but before the root fs is mounted. | |
244 | * | |
245 | * A possible improvement here would be to have the tools add a per-device | |
246 | * flag to the store entry, indicating whether it is needed at boot time. | |
247 | * This would allow people who knew what they were doing to accelerate their | |
248 | * boot slightly, but of course needs tools or manual intervention to set up | |
249 | * those flags correctly. | |
250 | */ | |
251 | static void wait_for_devices(struct xenbus_driver *xendrv) | |
252 | { | |
253 | unsigned long start = jiffies; | |
254 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; | |
255 | unsigned int seconds_waited = 0; | |
256 | ||
257 | if (!ready_to_wait_for_devices || !xen_domain()) | |
258 | return; | |
259 | ||
3066616c KRW |
260 | while (exists_non_essential_connecting_device(drv)) |
261 | if (wait_loop(start, 30, &seconds_waited)) | |
262 | break; | |
263 | ||
264 | /* Skips PVKB and PVFB check.*/ | |
265 | while (exists_essential_connecting_device(drv)) | |
266 | if (wait_loop(start, 270, &seconds_waited)) | |
267 | break; | |
2de06cc1 IC |
268 | |
269 | if (seconds_waited) | |
270 | printk("\n"); | |
271 | ||
272 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
273 | print_device_status); | |
274 | } | |
275 | ||
73db144b | 276 | int xenbus_register_frontend(struct xenbus_driver *drv) |
2de06cc1 IC |
277 | { |
278 | int ret; | |
279 | ||
280 | drv->read_otherend_details = read_backend_details; | |
281 | ||
73db144b | 282 | ret = xenbus_register_driver_common(drv, &xenbus_frontend); |
2de06cc1 IC |
283 | if (ret) |
284 | return ret; | |
285 | ||
286 | /* If this driver is loaded as a module wait for devices to attach. */ | |
287 | wait_for_devices(drv); | |
288 | ||
289 | return 0; | |
290 | } | |
73db144b | 291 | EXPORT_SYMBOL_GPL(xenbus_register_frontend); |
2de06cc1 | 292 | |
116df6f0 OH |
293 | static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq); |
294 | static int backend_state; | |
295 | ||
296 | static void xenbus_reset_backend_state_changed(struct xenbus_watch *w, | |
297 | const char **v, unsigned int l) | |
298 | { | |
299 | xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state); | |
300 | printk(KERN_DEBUG "XENBUS: backend %s %s\n", | |
301 | v[XS_WATCH_PATH], xenbus_strstate(backend_state)); | |
302 | wake_up(&backend_state_wq); | |
303 | } | |
304 | ||
305 | static void xenbus_reset_wait_for_backend(char *be, int expected) | |
306 | { | |
307 | long timeout; | |
308 | timeout = wait_event_interruptible_timeout(backend_state_wq, | |
309 | backend_state == expected, 5 * HZ); | |
310 | if (timeout <= 0) | |
311 | printk(KERN_INFO "XENBUS: backend %s timed out.\n", be); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Reset frontend if it is in Connected or Closed state. | |
316 | * Wait for backend to catch up. | |
317 | * State Connected happens during kdump, Closed after kexec. | |
318 | */ | |
319 | static void xenbus_reset_frontend(char *fe, char *be, int be_state) | |
320 | { | |
321 | struct xenbus_watch be_watch; | |
322 | ||
323 | printk(KERN_DEBUG "XENBUS: backend %s %s\n", | |
324 | be, xenbus_strstate(be_state)); | |
325 | ||
326 | memset(&be_watch, 0, sizeof(be_watch)); | |
327 | be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be); | |
328 | if (!be_watch.node) | |
329 | return; | |
330 | ||
331 | be_watch.callback = xenbus_reset_backend_state_changed; | |
332 | backend_state = XenbusStateUnknown; | |
333 | ||
334 | printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be); | |
335 | register_xenbus_watch(&be_watch); | |
336 | ||
337 | /* fall through to forward backend to state XenbusStateInitialising */ | |
338 | switch (be_state) { | |
339 | case XenbusStateConnected: | |
340 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing); | |
341 | xenbus_reset_wait_for_backend(be, XenbusStateClosing); | |
342 | ||
343 | case XenbusStateClosing: | |
344 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed); | |
345 | xenbus_reset_wait_for_backend(be, XenbusStateClosed); | |
346 | ||
347 | case XenbusStateClosed: | |
348 | xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising); | |
349 | xenbus_reset_wait_for_backend(be, XenbusStateInitWait); | |
350 | } | |
351 | ||
352 | unregister_xenbus_watch(&be_watch); | |
353 | printk(KERN_INFO "XENBUS: reconnect done on %s\n", be); | |
354 | kfree(be_watch.node); | |
355 | } | |
356 | ||
357 | static void xenbus_check_frontend(char *class, char *dev) | |
358 | { | |
359 | int be_state, fe_state, err; | |
360 | char *backend, *frontend; | |
361 | ||
362 | frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev); | |
363 | if (!frontend) | |
364 | return; | |
365 | ||
366 | err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state); | |
367 | if (err != 1) | |
368 | goto out; | |
369 | ||
370 | switch (fe_state) { | |
371 | case XenbusStateConnected: | |
372 | case XenbusStateClosed: | |
373 | printk(KERN_DEBUG "XENBUS: frontend %s %s\n", | |
374 | frontend, xenbus_strstate(fe_state)); | |
375 | backend = xenbus_read(XBT_NIL, frontend, "backend", NULL); | |
376 | if (!backend || IS_ERR(backend)) | |
377 | goto out; | |
378 | err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state); | |
379 | if (err == 1) | |
380 | xenbus_reset_frontend(frontend, backend, be_state); | |
381 | kfree(backend); | |
382 | break; | |
383 | default: | |
384 | break; | |
385 | } | |
386 | out: | |
387 | kfree(frontend); | |
388 | } | |
389 | ||
390 | static void xenbus_reset_state(void) | |
391 | { | |
392 | char **devclass, **dev; | |
393 | int devclass_n, dev_n; | |
394 | int i, j; | |
395 | ||
396 | devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n); | |
397 | if (IS_ERR(devclass)) | |
398 | return; | |
399 | ||
400 | for (i = 0; i < devclass_n; i++) { | |
401 | dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n); | |
402 | if (IS_ERR(dev)) | |
403 | continue; | |
404 | for (j = 0; j < dev_n; j++) | |
405 | xenbus_check_frontend(devclass[i], dev[j]); | |
406 | kfree(dev); | |
407 | } | |
408 | kfree(devclass); | |
409 | } | |
410 | ||
df660251 IC |
411 | static int frontend_probe_and_watch(struct notifier_block *notifier, |
412 | unsigned long event, | |
413 | void *data) | |
414 | { | |
116df6f0 OH |
415 | /* reset devices in Connected or Closed state */ |
416 | if (xen_hvm_domain()) | |
417 | xenbus_reset_state(); | |
df660251 IC |
418 | /* Enumerate devices in xenstore and watch for changes. */ |
419 | xenbus_probe_devices(&xenbus_frontend); | |
df660251 | 420 | register_xenbus_watch(&fe_watch); |
0ff4fdf0 | 421 | |
df660251 IC |
422 | return NOTIFY_DONE; |
423 | } | |
424 | ||
425 | ||
2de06cc1 IC |
426 | static int __init xenbus_probe_frontend_init(void) |
427 | { | |
df660251 IC |
428 | static struct notifier_block xenstore_notifier = { |
429 | .notifier_call = frontend_probe_and_watch | |
430 | }; | |
2de06cc1 IC |
431 | int err; |
432 | ||
433 | DPRINTK(""); | |
434 | ||
435 | /* Register ourselves with the kernel bus subsystem */ | |
436 | err = bus_register(&xenbus_frontend.bus); | |
0ff4fdf0 | 437 | if (err) |
2de06cc1 | 438 | return err; |
2de06cc1 | 439 | |
df660251 | 440 | register_xenstore_notifier(&xenstore_notifier); |
2de06cc1 IC |
441 | |
442 | return 0; | |
443 | } | |
806f5463 | 444 | subsys_initcall(xenbus_probe_frontend_init); |
2de06cc1 IC |
445 | |
446 | #ifndef MODULE | |
447 | static int __init boot_wait_for_devices(void) | |
448 | { | |
449 | if (xen_hvm_domain() && !xen_platform_pci_unplug) | |
450 | return -ENODEV; | |
451 | ||
452 | ready_to_wait_for_devices = 1; | |
453 | wait_for_devices(NULL); | |
454 | return 0; | |
455 | } | |
456 | ||
457 | late_initcall(boot_wait_for_devices); | |
458 | #endif | |
1b31a143 JF |
459 | |
460 | MODULE_LICENSE("GPL"); |