vmwatchdog.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Watchdog implementation based on z/VM Watchdog Timer API
  3. *
  4. * The user space watchdog daemon can use this driver as
  5. * /dev/vmwatchdog to have z/VM execute the specified CP
  6. * command when the timeout expires. The default command is
  7. * "IPL", which which cause an immediate reboot.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/watchdog.h>
  16. #include <asm/ebcdic.h>
  17. #include <asm/io.h>
  18. #include <asm/uaccess.h>
  19. #define MAX_CMDLEN 240
  20. #define MIN_INTERVAL 15
  21. static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
  22. static int vmwdt_conceal;
  23. static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
  24. MODULE_LICENSE("GPL");
  25. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
  26. MODULE_DESCRIPTION("z/VM Watchdog Timer");
  27. module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
  28. MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
  29. module_param_named(conceal, vmwdt_conceal, bool, 0644);
  30. MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
  31. " is active");
  32. module_param_named(nowayout, vmwdt_nowayout, bool, 0);
  33. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
  34. " (default=CONFIG_WATCHDOG_NOWAYOUT)");
  35. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  36. static unsigned int vmwdt_interval = 60;
  37. static unsigned long vmwdt_is_open;
  38. static int vmwdt_expect_close;
  39. enum vmwdt_func {
  40. /* function codes */
  41. wdt_init = 0,
  42. wdt_change = 1,
  43. wdt_cancel = 2,
  44. /* flags */
  45. wdt_conceal = 0x80000000,
  46. };
  47. static int __diag288(enum vmwdt_func func, unsigned int timeout,
  48. char *cmd, size_t len)
  49. {
  50. register unsigned long __func asm("2") = func;
  51. register unsigned long __timeout asm("3") = timeout;
  52. register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
  53. register unsigned long __cmdl asm("5") = len;
  54. int err;
  55. err = -EINVAL;
  56. asm volatile(
  57. " diag %1,%3,0x288\n"
  58. "0: la %0,0\n"
  59. "1:\n"
  60. EX_TABLE(0b,1b)
  61. : "=d" (err) : "d"(__func), "d"(__timeout),
  62. "d"(__cmdp), "d"(__cmdl), "0" (-EINVAL) : "1", "cc");
  63. return err;
  64. }
  65. static int vmwdt_keepalive(void)
  66. {
  67. /* we allocate new memory every time to avoid having
  68. * to track the state. static allocation is not an
  69. * option since that might not be contiguous in real
  70. * storage in case of a modular build */
  71. static char *ebc_cmd;
  72. size_t len;
  73. int ret;
  74. unsigned int func;
  75. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  76. if (!ebc_cmd)
  77. return -ENOMEM;
  78. len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
  79. ASCEBC(ebc_cmd, MAX_CMDLEN);
  80. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  81. func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
  82. ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
  83. kfree(ebc_cmd);
  84. if (ret) {
  85. printk(KERN_WARNING "%s: problem setting interval %d, "
  86. "cmd %s\n", __FUNCTION__, vmwdt_interval,
  87. vmwdt_cmd);
  88. }
  89. return ret;
  90. }
  91. static int vmwdt_disable(void)
  92. {
  93. int ret = __diag288(wdt_cancel, 0, "", 0);
  94. if (ret) {
  95. printk(KERN_WARNING "%s: problem disabling watchdog\n",
  96. __FUNCTION__);
  97. }
  98. return ret;
  99. }
  100. static int __init vmwdt_probe(void)
  101. {
  102. /* there is no real way to see if the watchdog is supported,
  103. * so we try initializing it with a NOP command ("BEGIN")
  104. * that won't cause any harm even if the following disable
  105. * fails for some reason */
  106. static char __initdata ebc_begin[] = {
  107. 194, 197, 199, 201, 213
  108. };
  109. if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0) {
  110. printk(KERN_INFO "z/VM watchdog not available\n");
  111. return -EINVAL;
  112. }
  113. return vmwdt_disable();
  114. }
  115. static int vmwdt_open(struct inode *i, struct file *f)
  116. {
  117. int ret;
  118. if (test_and_set_bit(0, &vmwdt_is_open))
  119. return -EBUSY;
  120. ret = vmwdt_keepalive();
  121. if (ret)
  122. clear_bit(0, &vmwdt_is_open);
  123. return ret ? ret : nonseekable_open(i, f);
  124. }
  125. static int vmwdt_close(struct inode *i, struct file *f)
  126. {
  127. if (vmwdt_expect_close == 42)
  128. vmwdt_disable();
  129. vmwdt_expect_close = 0;
  130. clear_bit(0, &vmwdt_is_open);
  131. return 0;
  132. }
  133. static struct watchdog_info vmwdt_info = {
  134. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  135. .firmware_version = 0,
  136. .identity = "z/VM Watchdog Timer",
  137. };
  138. static int vmwdt_ioctl(struct inode *i, struct file *f,
  139. unsigned int cmd, unsigned long arg)
  140. {
  141. switch (cmd) {
  142. case WDIOC_GETSUPPORT:
  143. if (copy_to_user((void __user *)arg, &vmwdt_info,
  144. sizeof(vmwdt_info)))
  145. return -EFAULT;
  146. return 0;
  147. case WDIOC_GETSTATUS:
  148. case WDIOC_GETBOOTSTATUS:
  149. return put_user(0, (int __user *)arg);
  150. case WDIOC_GETTEMP:
  151. return -EINVAL;
  152. case WDIOC_SETOPTIONS:
  153. {
  154. int options, ret;
  155. if (get_user(options, (int __user *)arg))
  156. return -EFAULT;
  157. ret = -EINVAL;
  158. if (options & WDIOS_DISABLECARD) {
  159. ret = vmwdt_disable();
  160. if (ret)
  161. return ret;
  162. }
  163. if (options & WDIOS_ENABLECARD) {
  164. ret = vmwdt_keepalive();
  165. }
  166. return ret;
  167. }
  168. case WDIOC_GETTIMEOUT:
  169. return put_user(vmwdt_interval, (int __user *)arg);
  170. case WDIOC_SETTIMEOUT:
  171. {
  172. int interval;
  173. if (get_user(interval, (int __user *)arg))
  174. return -EFAULT;
  175. if (interval < MIN_INTERVAL)
  176. return -EINVAL;
  177. vmwdt_interval = interval;
  178. }
  179. return vmwdt_keepalive();
  180. case WDIOC_KEEPALIVE:
  181. return vmwdt_keepalive();
  182. }
  183. return -EINVAL;
  184. }
  185. static ssize_t vmwdt_write(struct file *f, const char __user *buf,
  186. size_t count, loff_t *ppos)
  187. {
  188. if(count) {
  189. if (!vmwdt_nowayout) {
  190. size_t i;
  191. /* note: just in case someone wrote the magic character
  192. * five months ago... */
  193. vmwdt_expect_close = 0;
  194. for (i = 0; i != count; i++) {
  195. char c;
  196. if (get_user(c, buf+i))
  197. return -EFAULT;
  198. if (c == 'V')
  199. vmwdt_expect_close = 42;
  200. }
  201. }
  202. /* someone wrote to us, we should restart timer */
  203. vmwdt_keepalive();
  204. }
  205. return count;
  206. }
  207. static struct file_operations vmwdt_fops = {
  208. .open = &vmwdt_open,
  209. .release = &vmwdt_close,
  210. .ioctl = &vmwdt_ioctl,
  211. .write = &vmwdt_write,
  212. .owner = THIS_MODULE,
  213. };
  214. static struct miscdevice vmwdt_dev = {
  215. .minor = WATCHDOG_MINOR,
  216. .name = "watchdog",
  217. .fops = &vmwdt_fops,
  218. };
  219. static int __init vmwdt_init(void)
  220. {
  221. int ret;
  222. ret = vmwdt_probe();
  223. if (ret)
  224. return ret;
  225. return misc_register(&vmwdt_dev);
  226. }
  227. module_init(vmwdt_init);
  228. static void __exit vmwdt_exit(void)
  229. {
  230. WARN_ON(misc_deregister(&vmwdt_dev) != 0);
  231. }
  232. module_exit(vmwdt_exit);