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