xenbus_probe_frontend.c 13 KB

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