xenbus_probe_frontend.c 13 KB

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