vmwatchdog.c 6.7 KB

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