xenbus_probe_frontend.c 13 KB

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