xen_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Xen Watchdog Driver
  3. *
  4. * (c) Copyright 2010 Novell, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define DRV_NAME "xen_wdt"
  12. #include <linux/bug.h>
  13. #include <linux/errno.h>
  14. #include <linux/fs.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/kernel.h>
  17. #include <linux/ktime.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/watchdog.h>
  23. #include <xen/xen.h>
  24. #include <asm/xen/hypercall.h>
  25. #include <xen/interface/sched.h>
  26. static struct platform_device *platform_device;
  27. static struct sched_watchdog wdt;
  28. static time64_t wdt_expires;
  29. #define WATCHDOG_TIMEOUT 60 /* in seconds */
  30. static unsigned int timeout;
  31. module_param(timeout, uint, S_IRUGO);
  32. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
  33. "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, bool, S_IRUGO);
  36. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  37. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  38. static inline time64_t set_timeout(struct watchdog_device *wdd)
  39. {
  40. wdt.timeout = wdd->timeout;
  41. return ktime_get_seconds() + wdd->timeout;
  42. }
  43. static int xen_wdt_start(struct watchdog_device *wdd)
  44. {
  45. time64_t expires;
  46. int err;
  47. expires = set_timeout(wdd);
  48. if (!wdt.id)
  49. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  50. else
  51. err = -EBUSY;
  52. if (err > 0) {
  53. wdt.id = err;
  54. wdt_expires = expires;
  55. err = 0;
  56. } else
  57. BUG_ON(!err);
  58. return err;
  59. }
  60. static int xen_wdt_stop(struct watchdog_device *wdd)
  61. {
  62. int err = 0;
  63. wdt.timeout = 0;
  64. if (wdt.id)
  65. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  66. if (!err)
  67. wdt.id = 0;
  68. return err;
  69. }
  70. static int xen_wdt_kick(struct watchdog_device *wdd)
  71. {
  72. time64_t expires;
  73. int err;
  74. expires = set_timeout(wdd);
  75. if (wdt.id)
  76. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  77. else
  78. err = -ENXIO;
  79. if (!err)
  80. wdt_expires = expires;
  81. return err;
  82. }
  83. static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
  84. {
  85. return wdt_expires - ktime_get_seconds();
  86. }
  87. static struct watchdog_info xen_wdt_info = {
  88. .identity = DRV_NAME,
  89. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  90. };
  91. static const struct watchdog_ops xen_wdt_ops = {
  92. .owner = THIS_MODULE,
  93. .start = xen_wdt_start,
  94. .stop = xen_wdt_stop,
  95. .ping = xen_wdt_kick,
  96. .get_timeleft = xen_wdt_get_timeleft,
  97. };
  98. static struct watchdog_device xen_wdt_dev = {
  99. .info = &xen_wdt_info,
  100. .ops = &xen_wdt_ops,
  101. .timeout = WATCHDOG_TIMEOUT,
  102. };
  103. static int xen_wdt_probe(struct platform_device *pdev)
  104. {
  105. struct sched_watchdog wd = { .id = ~0 };
  106. int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
  107. if (ret == -ENOSYS) {
  108. dev_err(&pdev->dev, "watchdog not supported by hypervisor\n");
  109. return -ENODEV;
  110. }
  111. if (ret != -EINVAL) {
  112. dev_err(&pdev->dev, "unexpected hypervisor error (%d)\n", ret);
  113. return -ENODEV;
  114. }
  115. if (watchdog_init_timeout(&xen_wdt_dev, timeout, NULL))
  116. dev_info(&pdev->dev, "timeout value invalid, using %d\n",
  117. xen_wdt_dev.timeout);
  118. watchdog_set_nowayout(&xen_wdt_dev, nowayout);
  119. watchdog_stop_on_reboot(&xen_wdt_dev);
  120. watchdog_stop_on_unregister(&xen_wdt_dev);
  121. ret = devm_watchdog_register_device(&pdev->dev, &xen_wdt_dev);
  122. if (ret) {
  123. dev_err(&pdev->dev, "cannot register watchdog device (%d)\n",
  124. ret);
  125. return ret;
  126. }
  127. dev_info(&pdev->dev, "initialized (timeout=%ds, nowayout=%d)\n",
  128. xen_wdt_dev.timeout, nowayout);
  129. return 0;
  130. }
  131. static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
  132. {
  133. typeof(wdt.id) id = wdt.id;
  134. int rc = xen_wdt_stop(&xen_wdt_dev);
  135. wdt.id = id;
  136. return rc;
  137. }
  138. static int xen_wdt_resume(struct platform_device *dev)
  139. {
  140. if (!wdt.id)
  141. return 0;
  142. wdt.id = 0;
  143. return xen_wdt_start(&xen_wdt_dev);
  144. }
  145. static struct platform_driver xen_wdt_driver = {
  146. .probe = xen_wdt_probe,
  147. .suspend = xen_wdt_suspend,
  148. .resume = xen_wdt_resume,
  149. .driver = {
  150. .name = DRV_NAME,
  151. },
  152. };
  153. static int __init xen_wdt_init_module(void)
  154. {
  155. int err;
  156. if (!xen_domain())
  157. return -ENODEV;
  158. err = platform_driver_register(&xen_wdt_driver);
  159. if (err)
  160. return err;
  161. platform_device = platform_device_register_simple(DRV_NAME,
  162. -1, NULL, 0);
  163. if (IS_ERR(platform_device)) {
  164. err = PTR_ERR(platform_device);
  165. platform_driver_unregister(&xen_wdt_driver);
  166. }
  167. return err;
  168. }
  169. static void __exit xen_wdt_cleanup_module(void)
  170. {
  171. platform_device_unregister(platform_device);
  172. platform_driver_unregister(&xen_wdt_driver);
  173. }
  174. module_init(xen_wdt_init_module);
  175. module_exit(xen_wdt_cleanup_module);
  176. MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
  177. MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
  178. MODULE_LICENSE("GPL");