PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/xen/xenbus/xenbus_probe_frontend.c

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