inet: frags: remove inet_frag_maybe_warn_overflow()
[linux-stable.git] / drivers / xen / xenbus / xenbus_probe_frontend.c
blob19e45ce21f891e19dc82e01e30cc5aedeb79a951
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #define DPRINTK(fmt, ...) \
4 pr_debug("(%s:%d) " fmt "\n", \
5 __func__, __LINE__, ##__VA_ARGS__)
7 #include <linux/kernel.h>
8 #include <linux/err.h>
9 #include <linux/string.h>
10 #include <linux/ctype.h>
11 #include <linux/fcntl.h>
12 #include <linux/mm.h>
13 #include <linux/proc_fs.h>
14 #include <linux/notifier.h>
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
20 #include <asm/page.h>
21 #include <asm/pgtable.h>
22 #include <asm/xen/hypervisor.h>
23 #include <xen/xenbus.h>
24 #include <xen/events.h>
25 #include <xen/page.h>
26 #include <xen/xen.h>
28 #include <xen/platform_pci.h>
30 #include "xenbus.h"
34 /* device/<type>/<id> => <type>-<id> */
35 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
37 nodename = strchr(nodename, '/');
38 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
39 pr_warn("bad frontend %s\n", nodename);
40 return -EINVAL;
43 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
44 if (!strchr(bus_id, '/')) {
45 pr_warn("bus_id %s no slash\n", bus_id);
46 return -EINVAL;
48 *strchr(bus_id, '/') = '-';
49 return 0;
52 /* device/<typename>/<name> */
53 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
54 const char *name)
56 char *nodename;
57 int err;
59 /* ignore console/0 */
60 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
61 DPRINTK("Ignoring buggy device entry console/0");
62 return 0;
65 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
66 if (!nodename)
67 return -ENOMEM;
69 DPRINTK("%s", nodename);
71 err = xenbus_probe_node(bus, type, nodename);
72 kfree(nodename);
73 return err;
76 static int xenbus_uevent_frontend(struct device *_dev,
77 struct kobj_uevent_env *env)
79 struct xenbus_device *dev = to_xenbus_device(_dev);
81 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
82 return -ENOMEM;
84 return 0;
88 static void backend_changed(struct xenbus_watch *watch,
89 const char *path, const char *token)
91 xenbus_otherend_changed(watch, path, token, 1);
94 static void xenbus_frontend_delayed_resume(struct work_struct *w)
96 struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
98 xenbus_dev_resume(&xdev->dev);
101 static int xenbus_frontend_dev_resume(struct device *dev)
104 * If xenstored is running in this domain, we cannot access the backend
105 * state at the moment, so we need to defer xenbus_dev_resume
107 if (xen_store_domain_type == XS_LOCAL) {
108 struct xenbus_device *xdev = to_xenbus_device(dev);
110 schedule_work(&xdev->work);
112 return 0;
115 return xenbus_dev_resume(dev);
118 static int xenbus_frontend_dev_probe(struct device *dev)
120 if (xen_store_domain_type == XS_LOCAL) {
121 struct xenbus_device *xdev = to_xenbus_device(dev);
122 INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
125 return xenbus_dev_probe(dev);
128 static const struct dev_pm_ops xenbus_pm_ops = {
129 .suspend = xenbus_dev_suspend,
130 .resume = xenbus_frontend_dev_resume,
131 .freeze = xenbus_dev_suspend,
132 .thaw = xenbus_dev_cancel,
133 .restore = xenbus_dev_resume,
136 static struct xen_bus_type xenbus_frontend = {
137 .root = "device",
138 .levels = 2, /* device/type/<id> */
139 .get_bus_id = frontend_bus_id,
140 .probe = xenbus_probe_frontend,
141 .otherend_changed = backend_changed,
142 .bus = {
143 .name = "xen",
144 .match = xenbus_match,
145 .uevent = xenbus_uevent_frontend,
146 .probe = xenbus_frontend_dev_probe,
147 .remove = xenbus_dev_remove,
148 .shutdown = xenbus_dev_shutdown,
149 .dev_groups = xenbus_dev_groups,
151 .pm = &xenbus_pm_ops,
155 static void frontend_changed(struct xenbus_watch *watch,
156 const char *path, const char *token)
158 DPRINTK("");
160 xenbus_dev_changed(path, &xenbus_frontend);
164 /* We watch for devices appearing and vanishing. */
165 static struct xenbus_watch fe_watch = {
166 .node = "device",
167 .callback = frontend_changed,
170 static int read_backend_details(struct xenbus_device *xendev)
172 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
175 static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
177 struct xenbus_device *xendev = to_xenbus_device(dev);
178 struct device_driver *drv = data;
179 struct xenbus_driver *xendrv;
182 * A device with no driver will never connect. We care only about
183 * devices which should currently be in the process of connecting.
185 if (!dev->driver)
186 return 0;
188 /* Is this search limited to a particular driver? */
189 if (drv && (dev->driver != drv))
190 return 0;
192 if (ignore_nonessential) {
193 /* With older QEMU, for PVonHVM guests the guest config files
194 * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
195 * which is nonsensical as there is no PV FB (there can be
196 * a PVKB) running as HVM guest. */
198 if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
199 return 0;
201 if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
202 return 0;
204 xendrv = to_xenbus_driver(dev->driver);
205 return (xendev->state < XenbusStateConnected ||
206 (xendev->state == XenbusStateConnected &&
207 xendrv->is_ready && !xendrv->is_ready(xendev)));
209 static int essential_device_connecting(struct device *dev, void *data)
211 return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
213 static int non_essential_device_connecting(struct device *dev, void *data)
215 return is_device_connecting(dev, data, false);
218 static int exists_essential_connecting_device(struct device_driver *drv)
220 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
221 essential_device_connecting);
223 static int exists_non_essential_connecting_device(struct device_driver *drv)
225 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
226 non_essential_device_connecting);
229 static int print_device_status(struct device *dev, void *data)
231 struct xenbus_device *xendev = to_xenbus_device(dev);
232 struct device_driver *drv = data;
234 /* Is this operation limited to a particular driver? */
235 if (drv && (dev->driver != drv))
236 return 0;
238 if (!dev->driver) {
239 /* Information only: is this too noisy? */
240 pr_info("Device with no driver: %s\n", xendev->nodename);
241 } else if (xendev->state < XenbusStateConnected) {
242 enum xenbus_state rstate = XenbusStateUnknown;
243 if (xendev->otherend)
244 rstate = xenbus_read_driver_state(xendev->otherend);
245 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
246 xendev->nodename, xendev->state, rstate);
249 return 0;
252 /* We only wait for device setup after most initcalls have run. */
253 static int ready_to_wait_for_devices;
255 static bool wait_loop(unsigned long start, unsigned int max_delay,
256 unsigned int *seconds_waited)
258 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
259 if (!*seconds_waited)
260 pr_warn("Waiting for devices to initialise: ");
261 *seconds_waited += 5;
262 pr_cont("%us...", max_delay - *seconds_waited);
263 if (*seconds_waited == max_delay) {
264 pr_cont("\n");
265 return true;
269 schedule_timeout_interruptible(HZ/10);
271 return false;
274 * On a 5-minute timeout, wait for all devices currently configured. We need
275 * to do this to guarantee that the filesystems and / or network devices
276 * needed for boot are available, before we can allow the boot to proceed.
278 * This needs to be on a late_initcall, to happen after the frontend device
279 * drivers have been initialised, but before the root fs is mounted.
281 * A possible improvement here would be to have the tools add a per-device
282 * flag to the store entry, indicating whether it is needed at boot time.
283 * This would allow people who knew what they were doing to accelerate their
284 * boot slightly, but of course needs tools or manual intervention to set up
285 * those flags correctly.
287 static void wait_for_devices(struct xenbus_driver *xendrv)
289 unsigned long start = jiffies;
290 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
291 unsigned int seconds_waited = 0;
293 if (!ready_to_wait_for_devices || !xen_domain())
294 return;
296 while (exists_non_essential_connecting_device(drv))
297 if (wait_loop(start, 30, &seconds_waited))
298 break;
300 /* Skips PVKB and PVFB check.*/
301 while (exists_essential_connecting_device(drv))
302 if (wait_loop(start, 270, &seconds_waited))
303 break;
305 if (seconds_waited)
306 printk("\n");
308 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
309 print_device_status);
312 int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
313 const char *mod_name)
315 int ret;
317 drv->read_otherend_details = read_backend_details;
319 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
320 owner, mod_name);
321 if (ret)
322 return ret;
324 /* If this driver is loaded as a module wait for devices to attach. */
325 wait_for_devices(drv);
327 return 0;
329 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
331 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
332 static int backend_state;
334 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
335 const char *path, const char *token)
337 if (xenbus_scanf(XBT_NIL, path, "", "%i",
338 &backend_state) != 1)
339 backend_state = XenbusStateUnknown;
340 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
341 path, xenbus_strstate(backend_state));
342 wake_up(&backend_state_wq);
345 static void xenbus_reset_wait_for_backend(char *be, int expected)
347 long timeout;
348 timeout = wait_event_interruptible_timeout(backend_state_wq,
349 backend_state == expected, 5 * HZ);
350 if (timeout <= 0)
351 pr_info("backend %s timed out\n", be);
355 * Reset frontend if it is in Connected or Closed state.
356 * Wait for backend to catch up.
357 * State Connected happens during kdump, Closed after kexec.
359 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
361 struct xenbus_watch be_watch;
363 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
364 be, xenbus_strstate(be_state));
366 memset(&be_watch, 0, sizeof(be_watch));
367 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
368 if (!be_watch.node)
369 return;
371 be_watch.callback = xenbus_reset_backend_state_changed;
372 backend_state = XenbusStateUnknown;
374 pr_info("triggering reconnect on %s\n", be);
375 register_xenbus_watch(&be_watch);
377 /* fall through to forward backend to state XenbusStateInitialising */
378 switch (be_state) {
379 case XenbusStateConnected:
380 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
381 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
383 case XenbusStateClosing:
384 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
385 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
387 case XenbusStateClosed:
388 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
389 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
392 unregister_xenbus_watch(&be_watch);
393 pr_info("reconnect done on %s\n", be);
394 kfree(be_watch.node);
397 static void xenbus_check_frontend(char *class, char *dev)
399 int be_state, fe_state, err;
400 char *backend, *frontend;
402 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
403 if (!frontend)
404 return;
406 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
407 if (err != 1)
408 goto out;
410 switch (fe_state) {
411 case XenbusStateConnected:
412 case XenbusStateClosed:
413 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
414 frontend, xenbus_strstate(fe_state));
415 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
416 if (!backend || IS_ERR(backend))
417 goto out;
418 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
419 if (err == 1)
420 xenbus_reset_frontend(frontend, backend, be_state);
421 kfree(backend);
422 break;
423 default:
424 break;
426 out:
427 kfree(frontend);
430 static void xenbus_reset_state(void)
432 char **devclass, **dev;
433 int devclass_n, dev_n;
434 int i, j;
436 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
437 if (IS_ERR(devclass))
438 return;
440 for (i = 0; i < devclass_n; i++) {
441 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
442 if (IS_ERR(dev))
443 continue;
444 for (j = 0; j < dev_n; j++)
445 xenbus_check_frontend(devclass[i], dev[j]);
446 kfree(dev);
448 kfree(devclass);
451 static int frontend_probe_and_watch(struct notifier_block *notifier,
452 unsigned long event,
453 void *data)
455 /* reset devices in Connected or Closed state */
456 if (xen_hvm_domain())
457 xenbus_reset_state();
458 /* Enumerate devices in xenstore and watch for changes. */
459 xenbus_probe_devices(&xenbus_frontend);
460 register_xenbus_watch(&fe_watch);
462 return NOTIFY_DONE;
466 static int __init xenbus_probe_frontend_init(void)
468 static struct notifier_block xenstore_notifier = {
469 .notifier_call = frontend_probe_and_watch
471 int err;
473 DPRINTK("");
475 /* Register ourselves with the kernel bus subsystem */
476 err = bus_register(&xenbus_frontend.bus);
477 if (err)
478 return err;
480 register_xenstore_notifier(&xenstore_notifier);
482 return 0;
484 subsys_initcall(xenbus_probe_frontend_init);
486 #ifndef MODULE
487 static int __init boot_wait_for_devices(void)
489 if (!xen_has_pv_devices())
490 return -ENODEV;
492 ready_to_wait_for_devices = 1;
493 wait_for_devices(NULL);
494 return 0;
497 late_initcall(boot_wait_for_devices);
498 #endif
500 MODULE_LICENSE("GPL");