xen_wdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "wdt"
  12. #define DRV_VERSION "0.01"
  13. #define PFX DRV_NAME ": "
  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. if (expect_release)
  110. xen_wdt_stop();
  111. else {
  112. printk(KERN_CRIT PFX
  113. "unexpected close, not stopping watchdog!\n");
  114. xen_wdt_kick();
  115. }
  116. is_active = false;
  117. expect_release = false;
  118. return 0;
  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 __devinit 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. printk(KERN_INFO PFX
  213. "timeout value invalid, using %d\n", timeout);
  214. }
  215. ret = misc_register(&xen_wdt_miscdev);
  216. if (ret) {
  217. printk(KERN_ERR PFX
  218. "cannot register miscdev on minor=%d (%d)\n",
  219. WATCHDOG_MINOR, ret);
  220. break;
  221. }
  222. printk(KERN_INFO PFX
  223. "initialized (timeout=%ds, nowayout=%d)\n",
  224. timeout, nowayout);
  225. break;
  226. case -ENOSYS:
  227. printk(KERN_INFO PFX "not supported\n");
  228. ret = -ENODEV;
  229. break;
  230. default:
  231. printk(KERN_INFO PFX "bogus return value %d\n", ret);
  232. break;
  233. }
  234. return ret;
  235. }
  236. static int __devexit xen_wdt_remove(struct platform_device *dev)
  237. {
  238. /* Stop the timer before we leave */
  239. if (!nowayout)
  240. xen_wdt_stop();
  241. misc_deregister(&xen_wdt_miscdev);
  242. return 0;
  243. }
  244. static void xen_wdt_shutdown(struct platform_device *dev)
  245. {
  246. xen_wdt_stop();
  247. }
  248. static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
  249. {
  250. return xen_wdt_stop();
  251. }
  252. static int xen_wdt_resume(struct platform_device *dev)
  253. {
  254. return xen_wdt_start();
  255. }
  256. static struct platform_driver xen_wdt_driver = {
  257. .probe = xen_wdt_probe,
  258. .remove = __devexit_p(xen_wdt_remove),
  259. .shutdown = xen_wdt_shutdown,
  260. .suspend = xen_wdt_suspend,
  261. .resume = xen_wdt_resume,
  262. .driver = {
  263. .owner = THIS_MODULE,
  264. .name = DRV_NAME,
  265. },
  266. };
  267. static int __init xen_wdt_init_module(void)
  268. {
  269. int err;
  270. if (!xen_domain())
  271. return -ENODEV;
  272. printk(KERN_INFO PFX "Xen WatchDog Timer Driver v%s\n", DRV_VERSION);
  273. err = platform_driver_register(&xen_wdt_driver);
  274. if (err)
  275. return err;
  276. platform_device = platform_device_register_simple(DRV_NAME,
  277. -1, NULL, 0);
  278. if (IS_ERR(platform_device)) {
  279. err = PTR_ERR(platform_device);
  280. platform_driver_unregister(&xen_wdt_driver);
  281. }
  282. return err;
  283. }
  284. static void __exit xen_wdt_cleanup_module(void)
  285. {
  286. platform_device_unregister(platform_device);
  287. platform_driver_unregister(&xen_wdt_driver);
  288. printk(KERN_INFO PFX "module unloaded\n");
  289. }
  290. module_init(xen_wdt_init_module);
  291. module_exit(xen_wdt_cleanup_module);
  292. MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
  293. MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
  294. MODULE_VERSION(DRV_VERSION);
  295. MODULE_LICENSE("GPL");
  296. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);