vmwatchdog.c 6.6 KB

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