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