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