xenbus_probe_frontend.c 11 KB

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