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