xenbus_probe_frontend.c 12 KB

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