Linux Audio

Check our new training course

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