xen_wdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #define DRV_NAME "wdt"
  13. #define DRV_VERSION "0.01"
  14. #include <linux/bug.h>
  15. #include <linux/errno.h>
  16. #include <linux/fs.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/kernel.h>
  19. #include <linux/ktime.h>
  20. #include <linux/init.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/watchdog.h>
  28. #include <xen/xen.h>
  29. #include <asm/xen/hypercall.h>
  30. #include <xen/interface/sched.h>
  31. static struct platform_device *platform_device;
  32. static DEFINE_SPINLOCK(wdt_lock);
  33. static struct sched_watchdog wdt;
  34. static __kernel_time_t wdt_expires;
  35. static bool is_active, expect_release;
  36. #define WATCHDOG_TIMEOUT 60 /* in seconds */
  37. static unsigned int timeout = WATCHDOG_TIMEOUT;
  38. module_param(timeout, uint, S_IRUGO);
  39. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
  40. "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  41. static bool nowayout = WATCHDOG_NOWAYOUT;
  42. module_param(nowayout, bool, S_IRUGO);
  43. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  44. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  45. static inline __kernel_time_t set_timeout(void)
  46. {
  47. wdt.timeout = timeout;
  48. return ktime_to_timespec(ktime_get()).tv_sec + timeout;
  49. }
  50. static int xen_wdt_start(void)
  51. {
  52. __kernel_time_t expires;
  53. int err;
  54. spin_lock(&wdt_lock);
  55. expires = set_timeout();
  56. if (!wdt.id)
  57. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  58. else
  59. err = -EBUSY;
  60. if (err > 0) {
  61. wdt.id = err;
  62. wdt_expires = expires;
  63. err = 0;
  64. } else
  65. BUG_ON(!err);
  66. spin_unlock(&wdt_lock);
  67. return err;
  68. }
  69. static int xen_wdt_stop(void)
  70. {
  71. int err = 0;
  72. spin_lock(&wdt_lock);
  73. wdt.timeout = 0;
  74. if (wdt.id)
  75. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  76. if (!err)
  77. wdt.id = 0;
  78. spin_unlock(&wdt_lock);
  79. return err;
  80. }
  81. static int xen_wdt_kick(void)
  82. {
  83. __kernel_time_t expires;
  84. int err;
  85. spin_lock(&wdt_lock);
  86. expires = set_timeout();
  87. if (wdt.id)
  88. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  89. else
  90. err = -ENXIO;
  91. if (!err)
  92. wdt_expires = expires;
  93. spin_unlock(&wdt_lock);
  94. return err;
  95. }
  96. static int xen_wdt_open(struct inode *inode, struct file *file)
  97. {
  98. int err;
  99. /* /dev/watchdog can only be opened once */
  100. if (xchg(&is_active, true))
  101. return -EBUSY;
  102. err = xen_wdt_start();
  103. if (err == -EBUSY)
  104. err = xen_wdt_kick();
  105. return err ?: nonseekable_open(inode, file);
  106. }
  107. static int xen_wdt_release(struct inode *inode, struct file *file)
  108. {
  109. int err = 0;
  110. if (expect_release)
  111. err = xen_wdt_stop();
  112. else {
  113. pr_crit("unexpected close, not stopping watchdog!\n");
  114. xen_wdt_kick();
  115. }
  116. is_active = err;
  117. expect_release = false;
  118. return err;
  119. }
  120. static ssize_t xen_wdt_write(struct file *file, const char __user *data,
  121. size_t len, loff_t *ppos)
  122. {
  123. /* See if we got the magic character 'V' and reload the timer */
  124. if (len) {
  125. if (!nowayout) {
  126. size_t i;
  127. /* in case it was set long ago */
  128. expect_release = false;
  129. /* scan to see whether or not we got the magic
  130. character */
  131. for (i = 0; i != len; i++) {
  132. char c;
  133. if (get_user(c, data + i))
  134. return -EFAULT;
  135. if (c == 'V')
  136. expect_release = true;
  137. }
  138. }
  139. /* someone wrote to us, we should reload the timer */
  140. xen_wdt_kick();
  141. }
  142. return len;
  143. }
  144. static long xen_wdt_ioctl(struct file *file, unsigned int cmd,
  145. unsigned long arg)
  146. {
  147. int new_options, retval = -EINVAL;
  148. int new_timeout;
  149. int __user *argp = (void __user *)arg;
  150. static const struct watchdog_info ident = {
  151. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  152. .firmware_version = 0,
  153. .identity = DRV_NAME,
  154. };
  155. switch (cmd) {
  156. case WDIOC_GETSUPPORT:
  157. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  158. case WDIOC_GETSTATUS:
  159. case WDIOC_GETBOOTSTATUS:
  160. return put_user(0, argp);
  161. case WDIOC_SETOPTIONS:
  162. if (get_user(new_options, argp))
  163. return -EFAULT;
  164. if (new_options & WDIOS_DISABLECARD)
  165. retval = xen_wdt_stop();
  166. if (new_options & WDIOS_ENABLECARD) {
  167. retval = xen_wdt_start();
  168. if (retval == -EBUSY)
  169. retval = xen_wdt_kick();
  170. }
  171. return retval;
  172. case WDIOC_KEEPALIVE:
  173. xen_wdt_kick();
  174. return 0;
  175. case WDIOC_SETTIMEOUT:
  176. if (get_user(new_timeout, argp))
  177. return -EFAULT;
  178. if (!new_timeout)
  179. return -EINVAL;
  180. timeout = new_timeout;
  181. xen_wdt_kick();
  182. /* fall through */
  183. case WDIOC_GETTIMEOUT:
  184. return put_user(timeout, argp);
  185. case WDIOC_GETTIMELEFT:
  186. retval = wdt_expires - ktime_to_timespec(ktime_get()).tv_sec;
  187. return put_user(retval, argp);
  188. }
  189. return -ENOTTY;
  190. }
  191. static const struct file_operations xen_wdt_fops = {
  192. .owner = THIS_MODULE,
  193. .llseek = no_llseek,
  194. .write = xen_wdt_write,
  195. .unlocked_ioctl = xen_wdt_ioctl,
  196. .open = xen_wdt_open,
  197. .release = xen_wdt_release,
  198. };
  199. static struct miscdevice xen_wdt_miscdev = {
  200. .minor = WATCHDOG_MINOR,
  201. .name = "watchdog",
  202. .fops = &xen_wdt_fops,
  203. };
  204. static int xen_wdt_probe(struct platform_device *dev)
  205. {
  206. struct sched_watchdog wd = { .id = ~0 };
  207. int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
  208. switch (ret) {
  209. case -EINVAL:
  210. if (!timeout) {
  211. timeout = WATCHDOG_TIMEOUT;
  212. pr_info("timeout value invalid, using %d\n", timeout);
  213. }
  214. ret = misc_register(&xen_wdt_miscdev);
  215. if (ret) {
  216. pr_err("cannot register miscdev on minor=%d (%d)\n",
  217. WATCHDOG_MINOR, ret);
  218. break;
  219. }
  220. pr_info("initialized (timeout=%ds, nowayout=%d)\n",
  221. timeout, nowayout);
  222. break;
  223. case -ENOSYS:
  224. pr_info("not supported\n");
  225. ret = -ENODEV;
  226. break;
  227. default:
  228. pr_info("bogus return value %d\n", ret);
  229. break;
  230. }
  231. return ret;
  232. }
  233. static int xen_wdt_remove(struct platform_device *dev)
  234. {
  235. /* Stop the timer before we leave */
  236. if (!nowayout)
  237. xen_wdt_stop();
  238. misc_deregister(&xen_wdt_miscdev);
  239. return 0;
  240. }
  241. static void xen_wdt_shutdown(struct platform_device *dev)
  242. {
  243. xen_wdt_stop();
  244. }
  245. static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
  246. {
  247. typeof(wdt.id) id = wdt.id;
  248. int rc = xen_wdt_stop();
  249. wdt.id = id;
  250. return rc;
  251. }
  252. static int xen_wdt_resume(struct platform_device *dev)
  253. {
  254. if (!wdt.id)
  255. return 0;
  256. wdt.id = 0;
  257. return xen_wdt_start();
  258. }
  259. static struct platform_driver xen_wdt_driver = {
  260. .probe = xen_wdt_probe,
  261. .remove = xen_wdt_remove,
  262. .shutdown = xen_wdt_shutdown,
  263. .suspend = xen_wdt_suspend,
  264. .resume = xen_wdt_resume,
  265. .driver = {
  266. .name = DRV_NAME,
  267. },
  268. };
  269. static int __init xen_wdt_init_module(void)
  270. {
  271. int err;
  272. if (!xen_domain())
  273. return -ENODEV;
  274. pr_info("Xen WatchDog Timer Driver v%s\n", DRV_VERSION);
  275. err = platform_driver_register(&xen_wdt_driver);
  276. if (err)
  277. return err;
  278. platform_device = platform_device_register_simple(DRV_NAME,
  279. -1, NULL, 0);
  280. if (IS_ERR(platform_device)) {
  281. err = PTR_ERR(platform_device);
  282. platform_driver_unregister(&xen_wdt_driver);
  283. }
  284. return err;
  285. }
  286. static void __exit xen_wdt_cleanup_module(void)
  287. {
  288. platform_device_unregister(platform_device);
  289. platform_driver_unregister(&xen_wdt_driver);
  290. pr_info("module unloaded\n");
  291. }
  292. module_init(xen_wdt_init_module);
  293. module_exit(xen_wdt_cleanup_module);
  294. MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
  295. MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
  296. MODULE_VERSION(DRV_VERSION);
  297. MODULE_LICENSE("GPL");