vmwatchdog.c 6.0 KB

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