xenbus_probe_frontend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #define DPRINTK(fmt, args...) \
  2. pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
  3. __func__, __LINE__, ##args)
  4. #include <linux/kernel.h>
  5. #include <linux/err.h>
  6. #include <linux/string.h>
  7. #include <linux/ctype.h>
  8. #include <linux/fcntl.h>
  9. #include <linux/mm.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/notifier.h>
  12. #include <linux/kthread.h>
  13. #include <linux/mutex.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <asm/page.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/xen/hypervisor.h>
  19. #include <xen/xenbus.h>
  20. #include <xen/events.h>
  21. #include <xen/page.h>
  22. #include <xen/xen.h>
  23. #include <xen/platform_pci.h>
  24. #include "xenbus_comms.h"
  25. #include "xenbus_probe.h"
  26. static struct workqueue_struct *xenbus_frontend_wq;
  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. printk(KERN_WARNING "XENBUS: 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. printk(KERN_WARNING "XENBUS: 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. if (!xenbus_frontend_wq) {
  89. pr_err("%s: no workqueue to process delayed resume\n",
  90. xdev->nodename);
  91. return -EFAULT;
  92. }
  93. INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
  94. queue_work(xenbus_frontend_wq, &xdev->work);
  95. return 0;
  96. }
  97. return xenbus_dev_resume(dev);
  98. }
  99. static const struct dev_pm_ops xenbus_pm_ops = {
  100. .suspend = xenbus_dev_suspend,
  101. .resume = xenbus_frontend_dev_resume,
  102. .freeze = xenbus_dev_suspend,
  103. .thaw = xenbus_dev_cancel,
  104. .restore = xenbus_dev_resume,
  105. };
  106. static struct xen_bus_type xenbus_frontend = {
  107. .root = "device",
  108. .levels = 2, /* device/type/<id> */
  109. .get_bus_id = frontend_bus_id,
  110. .probe = xenbus_probe_frontend,
  111. .otherend_changed = backend_changed,
  112. .bus = {
  113. .name = "xen",
  114. .match = xenbus_match,
  115. .uevent = xenbus_uevent_frontend,
  116. .probe = xenbus_dev_probe,
  117. .remove = xenbus_dev_remove,
  118. .shutdown = xenbus_dev_shutdown,
  119. .dev_attrs = xenbus_dev_attrs,
  120. .pm = &xenbus_pm_ops,
  121. },
  122. };
  123. static void frontend_changed(struct xenbus_watch *watch,
  124. const char **vec, unsigned int len)
  125. {
  126. DPRINTK("");
  127. xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
  128. }
  129. /* We watch for devices appearing and vanishing. */
  130. static struct xenbus_watch fe_watch = {
  131. .node = "device",
  132. .callback = frontend_changed,
  133. };
  134. static int read_backend_details(struct xenbus_device *xendev)
  135. {
  136. return xenbus_read_otherend_details(xendev, "backend-id", "backend");
  137. }
  138. static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
  139. {
  140. struct xenbus_device *xendev = to_xenbus_device(dev);
  141. struct device_driver *drv = data;
  142. struct xenbus_driver *xendrv;
  143. /*
  144. * A device with no driver will never connect. We care only about
  145. * devices which should currently be in the process of connecting.
  146. */
  147. if (!dev->driver)
  148. return 0;
  149. /* Is this search limited to a particular driver? */
  150. if (drv && (dev->driver != drv))
  151. return 0;
  152. if (ignore_nonessential) {
  153. /* With older QEMU, for PVonHVM guests the guest config files
  154. * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
  155. * which is nonsensical as there is no PV FB (there can be
  156. * a PVKB) running as HVM guest. */
  157. if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
  158. return 0;
  159. if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
  160. return 0;
  161. }
  162. xendrv = to_xenbus_driver(dev->driver);
  163. return (xendev->state < XenbusStateConnected ||
  164. (xendev->state == XenbusStateConnected &&
  165. xendrv->is_ready && !xendrv->is_ready(xendev)));
  166. }
  167. static int essential_device_connecting(struct device *dev, void *data)
  168. {
  169. return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
  170. }
  171. static int non_essential_device_connecting(struct device *dev, void *data)
  172. {
  173. return is_device_connecting(dev, data, false);
  174. }
  175. static int exists_essential_connecting_device(struct device_driver *drv)
  176. {
  177. return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  178. essential_device_connecting);
  179. }
  180. static int exists_non_essential_connecting_device(struct device_driver *drv)
  181. {
  182. return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  183. non_essential_device_connecting);
  184. }
  185. static int print_device_status(struct device *dev, void *data)
  186. {
  187. struct xenbus_device *xendev = to_xenbus_device(dev);
  188. struct device_driver *drv = data;
  189. /* Is this operation limited to a particular driver? */
  190. if (drv && (dev->driver != drv))
  191. return 0;
  192. if (!dev->driver) {
  193. /* Information only: is this too noisy? */
  194. printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
  195. 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. printk(KERN_WARNING "XENBUS: Timeout connecting "
  201. "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. printk(KERN_WARNING "XENBUS: Waiting for "
  214. "devices to initialise: ");
  215. *seconds_waited += 5;
  216. printk("%us...", max_delay - *seconds_waited);
  217. if (*seconds_waited == max_delay)
  218. return true;
  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)
  257. {
  258. int ret;
  259. drv->read_otherend_details = read_backend_details;
  260. ret = xenbus_register_driver_common(drv, &xenbus_frontend);
  261. if (ret)
  262. return ret;
  263. /* If this driver is loaded as a module wait for devices to attach. */
  264. wait_for_devices(drv);
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(xenbus_register_frontend);
  268. static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
  269. static int backend_state;
  270. static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
  271. const char **v, unsigned int l)
  272. {
  273. xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
  274. printk(KERN_DEBUG "XENBUS: backend %s %s\n",
  275. v[XS_WATCH_PATH], xenbus_strstate(backend_state));
  276. wake_up(&backend_state_wq);
  277. }
  278. static void xenbus_reset_wait_for_backend(char *be, int expected)
  279. {
  280. long timeout;
  281. timeout = wait_event_interruptible_timeout(backend_state_wq,
  282. backend_state == expected, 5 * HZ);
  283. if (timeout <= 0)
  284. printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
  285. }
  286. /*
  287. * Reset frontend if it is in Connected or Closed state.
  288. * Wait for backend to catch up.
  289. * State Connected happens during kdump, Closed after kexec.
  290. */
  291. static void xenbus_reset_frontend(char *fe, char *be, int be_state)
  292. {
  293. struct xenbus_watch be_watch;
  294. printk(KERN_DEBUG "XENBUS: backend %s %s\n",
  295. be, xenbus_strstate(be_state));
  296. memset(&be_watch, 0, sizeof(be_watch));
  297. be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
  298. if (!be_watch.node)
  299. return;
  300. be_watch.callback = xenbus_reset_backend_state_changed;
  301. backend_state = XenbusStateUnknown;
  302. printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
  303. register_xenbus_watch(&be_watch);
  304. /* fall through to forward backend to state XenbusStateInitialising */
  305. switch (be_state) {
  306. case XenbusStateConnected:
  307. xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
  308. xenbus_reset_wait_for_backend(be, XenbusStateClosing);
  309. case XenbusStateClosing:
  310. xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
  311. xenbus_reset_wait_for_backend(be, XenbusStateClosed);
  312. case XenbusStateClosed:
  313. xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
  314. xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
  315. }
  316. unregister_xenbus_watch(&be_watch);
  317. printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
  318. kfree(be_watch.node);
  319. }
  320. static void xenbus_check_frontend(char *class, char *dev)
  321. {
  322. int be_state, fe_state, err;
  323. char *backend, *frontend;
  324. frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
  325. if (!frontend)
  326. return;
  327. err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
  328. if (err != 1)
  329. goto out;
  330. switch (fe_state) {
  331. case XenbusStateConnected:
  332. case XenbusStateClosed:
  333. printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
  334. frontend, xenbus_strstate(fe_state));
  335. backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
  336. if (!backend || IS_ERR(backend))
  337. goto out;
  338. err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
  339. if (err == 1)
  340. xenbus_reset_frontend(frontend, backend, be_state);
  341. kfree(backend);
  342. break;
  343. default:
  344. break;
  345. }
  346. out:
  347. kfree(frontend);
  348. }
  349. static void xenbus_reset_state(void)
  350. {
  351. char **devclass, **dev;
  352. int devclass_n, dev_n;
  353. int i, j;
  354. devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
  355. if (IS_ERR(devclass))
  356. return;
  357. for (i = 0; i < devclass_n; i++) {
  358. dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
  359. if (IS_ERR(dev))
  360. continue;
  361. for (j = 0; j < dev_n; j++)
  362. xenbus_check_frontend(devclass[i], dev[j]);
  363. kfree(dev);
  364. }
  365. kfree(devclass);
  366. }
  367. static int frontend_probe_and_watch(struct notifier_block *notifier,
  368. unsigned long event,
  369. void *data)
  370. {
  371. /* reset devices in Connected or Closed state */
  372. if (xen_hvm_domain())
  373. xenbus_reset_state();
  374. /* Enumerate devices in xenstore and watch for changes. */
  375. xenbus_probe_devices(&xenbus_frontend);
  376. register_xenbus_watch(&fe_watch);
  377. return NOTIFY_DONE;
  378. }
  379. static int __init xenbus_probe_frontend_init(void)
  380. {
  381. static struct notifier_block xenstore_notifier = {
  382. .notifier_call = frontend_probe_and_watch
  383. };
  384. int err;
  385. DPRINTK("");
  386. /* Register ourselves with the kernel bus subsystem */
  387. err = bus_register(&xenbus_frontend.bus);
  388. if (err)
  389. return err;
  390. register_xenstore_notifier(&xenstore_notifier);
  391. xenbus_frontend_wq = create_workqueue("xenbus_frontend");
  392. return 0;
  393. }
  394. subsys_initcall(xenbus_probe_frontend_init);
  395. #ifndef MODULE
  396. static int __init boot_wait_for_devices(void)
  397. {
  398. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  399. return -ENODEV;
  400. ready_to_wait_for_devices = 1;
  401. wait_for_devices(NULL);
  402. return 0;
  403. }
  404. late_initcall(boot_wait_for_devices);
  405. #endif
  406. MODULE_LICENSE("GPL");