0001
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003
0004 #define DPRINTK(fmt, ...) \
0005 pr_debug("(%s:%d) " fmt "\n", \
0006 __func__, __LINE__, ##__VA_ARGS__)
0007
0008 #include <linux/kernel.h>
0009 #include <linux/err.h>
0010 #include <linux/string.h>
0011 #include <linux/ctype.h>
0012 #include <linux/fcntl.h>
0013 #include <linux/mm.h>
0014 #include <linux/proc_fs.h>
0015 #include <linux/notifier.h>
0016 #include <linux/kthread.h>
0017 #include <linux/mutex.h>
0018 #include <linux/io.h>
0019 #include <linux/module.h>
0020
0021 #include <asm/page.h>
0022 #include <asm/xen/hypervisor.h>
0023 #include <xen/xenbus.h>
0024 #include <xen/events.h>
0025 #include <xen/page.h>
0026 #include <xen/xen.h>
0027
0028 #include <xen/platform_pci.h>
0029
0030 #include "xenbus.h"
0031
0032
0033
0034
0035 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
0036 {
0037 nodename = strchr(nodename, '/');
0038 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
0039 pr_warn("bad frontend %s\n", nodename);
0040 return -EINVAL;
0041 }
0042
0043 strscpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
0044 if (!strchr(bus_id, '/')) {
0045 pr_warn("bus_id %s no slash\n", bus_id);
0046 return -EINVAL;
0047 }
0048 *strchr(bus_id, '/') = '-';
0049 return 0;
0050 }
0051
0052
0053 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
0054 const char *name)
0055 {
0056 char *nodename;
0057 int err;
0058
0059
0060 if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
0061 DPRINTK("Ignoring buggy device entry console/0");
0062 return 0;
0063 }
0064
0065 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
0066 if (!nodename)
0067 return -ENOMEM;
0068
0069 DPRINTK("%s", nodename);
0070
0071 err = xenbus_probe_node(bus, type, nodename);
0072 kfree(nodename);
0073 return err;
0074 }
0075
0076 static int xenbus_uevent_frontend(struct device *_dev,
0077 struct kobj_uevent_env *env)
0078 {
0079 struct xenbus_device *dev = to_xenbus_device(_dev);
0080
0081 if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
0082 return -ENOMEM;
0083
0084 return 0;
0085 }
0086
0087
0088 static void backend_changed(struct xenbus_watch *watch,
0089 const char *path, const char *token)
0090 {
0091 xenbus_otherend_changed(watch, path, token, 1);
0092 }
0093
0094 static void xenbus_frontend_delayed_resume(struct work_struct *w)
0095 {
0096 struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
0097
0098 xenbus_dev_resume(&xdev->dev);
0099 }
0100
0101 static int xenbus_frontend_dev_resume(struct device *dev)
0102 {
0103
0104
0105
0106
0107 if (xen_store_domain_type == XS_LOCAL) {
0108 struct xenbus_device *xdev = to_xenbus_device(dev);
0109
0110 schedule_work(&xdev->work);
0111
0112 return 0;
0113 }
0114
0115 return xenbus_dev_resume(dev);
0116 }
0117
0118 static int xenbus_frontend_dev_probe(struct device *dev)
0119 {
0120 if (xen_store_domain_type == XS_LOCAL) {
0121 struct xenbus_device *xdev = to_xenbus_device(dev);
0122 INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
0123 }
0124
0125 return xenbus_dev_probe(dev);
0126 }
0127
0128 static void xenbus_frontend_dev_shutdown(struct device *_dev)
0129 {
0130 struct xenbus_device *dev = to_xenbus_device(_dev);
0131 unsigned long timeout = 5*HZ;
0132
0133 DPRINTK("%s", dev->nodename);
0134
0135 get_device(&dev->dev);
0136 if (dev->state != XenbusStateConnected) {
0137 pr_info("%s: %s: %s != Connected, skipping\n",
0138 __func__, dev->nodename, xenbus_strstate(dev->state));
0139 goto out;
0140 }
0141 xenbus_switch_state(dev, XenbusStateClosing);
0142 timeout = wait_for_completion_timeout(&dev->down, timeout);
0143 if (!timeout)
0144 pr_info("%s: %s timeout closing device\n",
0145 __func__, dev->nodename);
0146 out:
0147 put_device(&dev->dev);
0148 }
0149
0150 static const struct dev_pm_ops xenbus_pm_ops = {
0151 .suspend = xenbus_dev_suspend,
0152 .resume = xenbus_frontend_dev_resume,
0153 .freeze = xenbus_dev_suspend,
0154 .thaw = xenbus_dev_cancel,
0155 .restore = xenbus_dev_resume,
0156 };
0157
0158 static struct xen_bus_type xenbus_frontend = {
0159 .root = "device",
0160 .levels = 2,
0161 .get_bus_id = frontend_bus_id,
0162 .probe = xenbus_probe_frontend,
0163 .otherend_changed = backend_changed,
0164 .bus = {
0165 .name = "xen",
0166 .match = xenbus_match,
0167 .uevent = xenbus_uevent_frontend,
0168 .probe = xenbus_frontend_dev_probe,
0169 .remove = xenbus_dev_remove,
0170 .shutdown = xenbus_frontend_dev_shutdown,
0171 .dev_groups = xenbus_dev_groups,
0172
0173 .pm = &xenbus_pm_ops,
0174 },
0175 };
0176
0177 static void frontend_changed(struct xenbus_watch *watch,
0178 const char *path, const char *token)
0179 {
0180 DPRINTK("");
0181
0182 xenbus_dev_changed(path, &xenbus_frontend);
0183 }
0184
0185
0186
0187 static struct xenbus_watch fe_watch = {
0188 .node = "device",
0189 .callback = frontend_changed,
0190 };
0191
0192 static int read_backend_details(struct xenbus_device *xendev)
0193 {
0194 return xenbus_read_otherend_details(xendev, "backend-id", "backend");
0195 }
0196
0197 static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
0198 {
0199 struct xenbus_device *xendev = to_xenbus_device(dev);
0200 struct device_driver *drv = data;
0201 struct xenbus_driver *xendrv;
0202
0203
0204
0205
0206
0207 if (!dev->driver)
0208 return 0;
0209
0210
0211 if (drv && (dev->driver != drv))
0212 return 0;
0213
0214 xendrv = to_xenbus_driver(dev->driver);
0215
0216 if (ignore_nonessential && xendrv->not_essential)
0217 return 0;
0218
0219 return (xendev->state < XenbusStateConnected ||
0220 (xendev->state == XenbusStateConnected &&
0221 xendrv->is_ready && !xendrv->is_ready(xendev)));
0222 }
0223 static int essential_device_connecting(struct device *dev, void *data)
0224 {
0225 return is_device_connecting(dev, data, true );
0226 }
0227 static int non_essential_device_connecting(struct device *dev, void *data)
0228 {
0229 return is_device_connecting(dev, data, false);
0230 }
0231
0232 static int exists_essential_connecting_device(struct device_driver *drv)
0233 {
0234 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
0235 essential_device_connecting);
0236 }
0237 static int exists_non_essential_connecting_device(struct device_driver *drv)
0238 {
0239 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
0240 non_essential_device_connecting);
0241 }
0242
0243 static int print_device_status(struct device *dev, void *data)
0244 {
0245 struct xenbus_device *xendev = to_xenbus_device(dev);
0246 struct device_driver *drv = data;
0247
0248
0249 if (drv && (dev->driver != drv))
0250 return 0;
0251
0252 if (!dev->driver) {
0253
0254 pr_info("Device with no driver: %s\n", xendev->nodename);
0255 } else if (xendev->state < XenbusStateConnected) {
0256 enum xenbus_state rstate = XenbusStateUnknown;
0257 if (xendev->otherend)
0258 rstate = xenbus_read_driver_state(xendev->otherend);
0259 pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
0260 xendev->nodename, xendev->state, rstate);
0261 }
0262
0263 return 0;
0264 }
0265
0266
0267 static int ready_to_wait_for_devices;
0268
0269 static bool wait_loop(unsigned long start, unsigned int max_delay,
0270 unsigned int *seconds_waited)
0271 {
0272 if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
0273 if (!*seconds_waited)
0274 pr_warn("Waiting for devices to initialise: ");
0275 *seconds_waited += 5;
0276 pr_cont("%us...", max_delay - *seconds_waited);
0277 if (*seconds_waited == max_delay) {
0278 pr_cont("\n");
0279 return true;
0280 }
0281 }
0282
0283 schedule_timeout_interruptible(HZ/10);
0284
0285 return false;
0286 }
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301 static void wait_for_devices(struct xenbus_driver *xendrv)
0302 {
0303 unsigned long start = jiffies;
0304 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
0305 unsigned int seconds_waited = 0;
0306
0307 if (!ready_to_wait_for_devices || !xen_domain())
0308 return;
0309
0310 while (exists_non_essential_connecting_device(drv))
0311 if (wait_loop(start, 30, &seconds_waited))
0312 break;
0313
0314
0315 while (exists_essential_connecting_device(drv))
0316 if (wait_loop(start, 270, &seconds_waited))
0317 break;
0318
0319 if (seconds_waited)
0320 printk("\n");
0321
0322 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
0323 print_device_status);
0324 }
0325
0326 int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
0327 const char *mod_name)
0328 {
0329 int ret;
0330
0331 drv->read_otherend_details = read_backend_details;
0332
0333 ret = xenbus_register_driver_common(drv, &xenbus_frontend,
0334 owner, mod_name);
0335 if (ret)
0336 return ret;
0337
0338
0339 wait_for_devices(drv);
0340
0341 return 0;
0342 }
0343 EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
0344
0345 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
0346 static int backend_state;
0347
0348 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
0349 const char *path, const char *token)
0350 {
0351 if (xenbus_scanf(XBT_NIL, path, "", "%i",
0352 &backend_state) != 1)
0353 backend_state = XenbusStateUnknown;
0354 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
0355 path, xenbus_strstate(backend_state));
0356 wake_up(&backend_state_wq);
0357 }
0358
0359 static void xenbus_reset_wait_for_backend(char *be, int expected)
0360 {
0361 long timeout;
0362 timeout = wait_event_interruptible_timeout(backend_state_wq,
0363 backend_state == expected, 5 * HZ);
0364 if (timeout <= 0)
0365 pr_info("backend %s timed out\n", be);
0366 }
0367
0368
0369
0370
0371
0372
0373 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
0374 {
0375 struct xenbus_watch be_watch;
0376
0377 printk(KERN_DEBUG "XENBUS: backend %s %s\n",
0378 be, xenbus_strstate(be_state));
0379
0380 memset(&be_watch, 0, sizeof(be_watch));
0381 be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
0382 if (!be_watch.node)
0383 return;
0384
0385 be_watch.callback = xenbus_reset_backend_state_changed;
0386 backend_state = XenbusStateUnknown;
0387
0388 pr_info("triggering reconnect on %s\n", be);
0389 register_xenbus_watch(&be_watch);
0390
0391
0392 switch (be_state) {
0393 case XenbusStateConnected:
0394 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
0395 xenbus_reset_wait_for_backend(be, XenbusStateClosing);
0396 fallthrough;
0397
0398 case XenbusStateClosing:
0399 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
0400 xenbus_reset_wait_for_backend(be, XenbusStateClosed);
0401 fallthrough;
0402
0403 case XenbusStateClosed:
0404 xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
0405 xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
0406 }
0407
0408 unregister_xenbus_watch(&be_watch);
0409 pr_info("reconnect done on %s\n", be);
0410 kfree(be_watch.node);
0411 }
0412
0413 static void xenbus_check_frontend(char *class, char *dev)
0414 {
0415 int be_state, fe_state, err;
0416 char *backend, *frontend;
0417
0418 frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
0419 if (!frontend)
0420 return;
0421
0422 err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
0423 if (err != 1)
0424 goto out;
0425
0426 switch (fe_state) {
0427 case XenbusStateConnected:
0428 case XenbusStateClosed:
0429 printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
0430 frontend, xenbus_strstate(fe_state));
0431 backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
0432 if (!backend || IS_ERR(backend))
0433 goto out;
0434 err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
0435 if (err == 1)
0436 xenbus_reset_frontend(frontend, backend, be_state);
0437 kfree(backend);
0438 break;
0439 default:
0440 break;
0441 }
0442 out:
0443 kfree(frontend);
0444 }
0445
0446 static void xenbus_reset_state(void)
0447 {
0448 char **devclass, **dev;
0449 int devclass_n, dev_n;
0450 int i, j;
0451
0452 devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
0453 if (IS_ERR(devclass))
0454 return;
0455
0456 for (i = 0; i < devclass_n; i++) {
0457 dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
0458 if (IS_ERR(dev))
0459 continue;
0460 for (j = 0; j < dev_n; j++)
0461 xenbus_check_frontend(devclass[i], dev[j]);
0462 kfree(dev);
0463 }
0464 kfree(devclass);
0465 }
0466
0467 static int frontend_probe_and_watch(struct notifier_block *notifier,
0468 unsigned long event,
0469 void *data)
0470 {
0471
0472 if (xen_hvm_domain())
0473 xenbus_reset_state();
0474
0475 xenbus_probe_devices(&xenbus_frontend);
0476 register_xenbus_watch(&fe_watch);
0477
0478 return NOTIFY_DONE;
0479 }
0480
0481
0482 static int __init xenbus_probe_frontend_init(void)
0483 {
0484 static struct notifier_block xenstore_notifier = {
0485 .notifier_call = frontend_probe_and_watch
0486 };
0487 int err;
0488
0489 DPRINTK("");
0490
0491
0492 err = bus_register(&xenbus_frontend.bus);
0493 if (err)
0494 return err;
0495
0496 register_xenstore_notifier(&xenstore_notifier);
0497
0498 return 0;
0499 }
0500 subsys_initcall(xenbus_probe_frontend_init);
0501
0502 #ifndef MODULE
0503 static int __init boot_wait_for_devices(void)
0504 {
0505 if (!xen_has_pv_devices())
0506 return -ENODEV;
0507
0508 ready_to_wait_for_devices = 1;
0509 wait_for_devices(NULL);
0510 return 0;
0511 }
0512
0513 late_initcall(boot_wait_for_devices);
0514 #endif
0515
0516 MODULE_LICENSE("GPL");