xenbus_probe_frontend.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <asm/page.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/xen/hypervisor.h>
  18. #include <xen/xenbus.h>
  19. #include <xen/events.h>
  20. #include <xen/page.h>
  21. #include <xen/platform_pci.h>
  22. #include "xenbus_comms.h"
  23. #include "xenbus_probe.h"
  24. /* device/<type>/<id> => <type>-<id> */
  25. static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
  26. {
  27. nodename = strchr(nodename, '/');
  28. if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
  29. printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
  30. return -EINVAL;
  31. }
  32. strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
  33. if (!strchr(bus_id, '/')) {
  34. printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
  35. return -EINVAL;
  36. }
  37. *strchr(bus_id, '/') = '-';
  38. return 0;
  39. }
  40. /* device/<typename>/<name> */
  41. static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, const char *name)
  42. {
  43. char *nodename;
  44. int err;
  45. nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
  46. if (!nodename)
  47. return -ENOMEM;
  48. DPRINTK("%s", nodename);
  49. err = xenbus_probe_node(bus, type, nodename);
  50. kfree(nodename);
  51. return err;
  52. }
  53. static int xenbus_uevent_frontend(struct device *_dev, struct kobj_uevent_env *env)
  54. {
  55. struct xenbus_device *dev = to_xenbus_device(_dev);
  56. if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
  57. return -ENOMEM;
  58. return 0;
  59. }
  60. static void backend_changed(struct xenbus_watch *watch,
  61. const char **vec, unsigned int len)
  62. {
  63. xenbus_otherend_changed(watch, vec, len, 1);
  64. }
  65. static struct device_attribute xenbus_frontend_dev_attrs[] = {
  66. __ATTR_NULL
  67. };
  68. static struct xen_bus_type xenbus_frontend = {
  69. .root = "device",
  70. .levels = 2, /* device/type/<id> */
  71. .get_bus_id = frontend_bus_id,
  72. .probe = xenbus_probe_frontend,
  73. .otherend_changed = backend_changed,
  74. .bus = {
  75. .name = "xen",
  76. .match = xenbus_match,
  77. .uevent = xenbus_uevent_frontend,
  78. .probe = xenbus_dev_probe,
  79. .remove = xenbus_dev_remove,
  80. .shutdown = xenbus_dev_shutdown,
  81. .dev_attrs= xenbus_frontend_dev_attrs,
  82. .suspend = xenbus_dev_suspend,
  83. .resume = xenbus_dev_resume,
  84. },
  85. };
  86. static void frontend_changed(struct xenbus_watch *watch,
  87. const char **vec, unsigned int len)
  88. {
  89. DPRINTK("");
  90. xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
  91. }
  92. /* We watch for devices appearing and vanishing. */
  93. static struct xenbus_watch fe_watch = {
  94. .node = "device",
  95. .callback = frontend_changed,
  96. };
  97. static int read_backend_details(struct xenbus_device *xendev)
  98. {
  99. return xenbus_read_otherend_details(xendev, "backend-id", "backend");
  100. }
  101. static int is_device_connecting(struct device *dev, void *data)
  102. {
  103. struct xenbus_device *xendev = to_xenbus_device(dev);
  104. struct device_driver *drv = data;
  105. struct xenbus_driver *xendrv;
  106. /*
  107. * A device with no driver will never connect. We care only about
  108. * devices which should currently be in the process of connecting.
  109. */
  110. if (!dev->driver)
  111. return 0;
  112. /* Is this search limited to a particular driver? */
  113. if (drv && (dev->driver != drv))
  114. return 0;
  115. xendrv = to_xenbus_driver(dev->driver);
  116. return (xendev->state < XenbusStateConnected ||
  117. (xendev->state == XenbusStateConnected &&
  118. xendrv->is_ready && !xendrv->is_ready(xendev)));
  119. }
  120. static int exists_connecting_device(struct device_driver *drv)
  121. {
  122. return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  123. is_device_connecting);
  124. }
  125. static int print_device_status(struct device *dev, void *data)
  126. {
  127. struct xenbus_device *xendev = to_xenbus_device(dev);
  128. struct device_driver *drv = data;
  129. /* Is this operation limited to a particular driver? */
  130. if (drv && (dev->driver != drv))
  131. return 0;
  132. if (!dev->driver) {
  133. /* Information only: is this too noisy? */
  134. printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
  135. xendev->nodename);
  136. } else if (xendev->state < XenbusStateConnected) {
  137. enum xenbus_state rstate = XenbusStateUnknown;
  138. if (xendev->otherend)
  139. rstate = xenbus_read_driver_state(xendev->otherend);
  140. printk(KERN_WARNING "XENBUS: Timeout connecting "
  141. "to device: %s (local state %d, remote state %d)\n",
  142. xendev->nodename, xendev->state, rstate);
  143. }
  144. return 0;
  145. }
  146. /* We only wait for device setup after most initcalls have run. */
  147. static int ready_to_wait_for_devices;
  148. /*
  149. * On a 5-minute timeout, wait for all devices currently configured. We need
  150. * to do this to guarantee that the filesystems and / or network devices
  151. * needed for boot are available, before we can allow the boot to proceed.
  152. *
  153. * This needs to be on a late_initcall, to happen after the frontend device
  154. * drivers have been initialised, but before the root fs is mounted.
  155. *
  156. * A possible improvement here would be to have the tools add a per-device
  157. * flag to the store entry, indicating whether it is needed at boot time.
  158. * This would allow people who knew what they were doing to accelerate their
  159. * boot slightly, but of course needs tools or manual intervention to set up
  160. * those flags correctly.
  161. */
  162. static void wait_for_devices(struct xenbus_driver *xendrv)
  163. {
  164. unsigned long start = jiffies;
  165. struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
  166. unsigned int seconds_waited = 0;
  167. if (!ready_to_wait_for_devices || !xen_domain())
  168. return;
  169. while (exists_connecting_device(drv)) {
  170. if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
  171. if (!seconds_waited)
  172. printk(KERN_WARNING "XENBUS: Waiting for "
  173. "devices to initialise: ");
  174. seconds_waited += 5;
  175. printk("%us...", 300 - seconds_waited);
  176. if (seconds_waited == 300)
  177. break;
  178. }
  179. schedule_timeout_interruptible(HZ/10);
  180. }
  181. if (seconds_waited)
  182. printk("\n");
  183. bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  184. print_device_status);
  185. }
  186. int __xenbus_register_frontend(struct xenbus_driver *drv,
  187. struct module *owner, const char *mod_name)
  188. {
  189. int ret;
  190. drv->read_otherend_details = read_backend_details;
  191. ret = xenbus_register_driver_common(drv, &xenbus_frontend,
  192. owner, mod_name);
  193. if (ret)
  194. return ret;
  195. /* If this driver is loaded as a module wait for devices to attach. */
  196. wait_for_devices(drv);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
  200. static int frontend_probe_and_watch(struct notifier_block *notifier,
  201. unsigned long event,
  202. void *data)
  203. {
  204. /* Enumerate devices in xenstore and watch for changes. */
  205. xenbus_probe_devices(&xenbus_frontend);
  206. printk(KERN_CRIT "%s devices probed ok\n", __func__);
  207. register_xenbus_watch(&fe_watch);
  208. printk(KERN_CRIT "%s watch add ok ok\n", __func__);
  209. printk(KERN_CRIT "%s all done\n", __func__);
  210. return NOTIFY_DONE;
  211. }
  212. static int __init xenbus_probe_frontend_init(void)
  213. {
  214. static struct notifier_block xenstore_notifier = {
  215. .notifier_call = frontend_probe_and_watch
  216. };
  217. int err;
  218. DPRINTK("");
  219. /* Register ourselves with the kernel bus subsystem */
  220. err = bus_register(&xenbus_frontend.bus);
  221. if (err) {
  222. printk(KERN_CRIT "%s didn't register bus!\n", __func__);
  223. return err;
  224. }
  225. printk(KERN_CRIT "%s bus registered ok\n", __func__);
  226. register_xenstore_notifier(&xenstore_notifier);
  227. return 0;
  228. }
  229. subsys_initcall(xenbus_probe_frontend_init);
  230. #ifndef MODULE
  231. static int __init boot_wait_for_devices(void)
  232. {
  233. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  234. return -ENODEV;
  235. ready_to_wait_for_devices = 1;
  236. wait_for_devices(NULL);
  237. return 0;
  238. }
  239. late_initcall(boot_wait_for_devices);
  240. #endif
  241. MODULE_LICENSE("GPL");