vmwatchdog.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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) : "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. WARN_ON(ret != 0);
  84. kfree(ebc_cmd);
  85. return ret;
  86. }
  87. static int vmwdt_disable(void)
  88. {
  89. int ret = __diag288(wdt_cancel, 0, "", 0);
  90. WARN_ON(ret != 0);
  91. return ret;
  92. }
  93. static int __init vmwdt_probe(void)
  94. {
  95. /* there is no real way to see if the watchdog is supported,
  96. * so we try initializing it with a NOP command ("BEGIN")
  97. * that won't cause any harm even if the following disable
  98. * fails for some reason */
  99. static char __initdata ebc_begin[] = {
  100. 194, 197, 199, 201, 213
  101. };
  102. if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
  103. return -EINVAL;
  104. return vmwdt_disable();
  105. }
  106. static int vmwdt_open(struct inode *i, struct file *f)
  107. {
  108. int ret;
  109. if (test_and_set_bit(0, &vmwdt_is_open))
  110. return -EBUSY;
  111. ret = vmwdt_keepalive();
  112. if (ret)
  113. clear_bit(0, &vmwdt_is_open);
  114. return ret ? ret : nonseekable_open(i, f);
  115. }
  116. static int vmwdt_close(struct inode *i, struct file *f)
  117. {
  118. if (vmwdt_expect_close == 42)
  119. vmwdt_disable();
  120. vmwdt_expect_close = 0;
  121. clear_bit(0, &vmwdt_is_open);
  122. return 0;
  123. }
  124. static struct watchdog_info vmwdt_info = {
  125. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  126. .firmware_version = 0,
  127. .identity = "z/VM Watchdog Timer",
  128. };
  129. static int vmwdt_ioctl(struct inode *i, struct file *f,
  130. unsigned int cmd, unsigned long arg)
  131. {
  132. switch (cmd) {
  133. case WDIOC_GETSUPPORT:
  134. if (copy_to_user((void __user *)arg, &vmwdt_info,
  135. sizeof(vmwdt_info)))
  136. return -EFAULT;
  137. return 0;
  138. case WDIOC_GETSTATUS:
  139. case WDIOC_GETBOOTSTATUS:
  140. return put_user(0, (int __user *)arg);
  141. case WDIOC_GETTEMP:
  142. return -EINVAL;
  143. case WDIOC_SETOPTIONS:
  144. {
  145. int options, ret;
  146. if (get_user(options, (int __user *)arg))
  147. return -EFAULT;
  148. ret = -EINVAL;
  149. if (options & WDIOS_DISABLECARD) {
  150. ret = vmwdt_disable();
  151. if (ret)
  152. return ret;
  153. }
  154. if (options & WDIOS_ENABLECARD) {
  155. ret = vmwdt_keepalive();
  156. }
  157. return ret;
  158. }
  159. case WDIOC_GETTIMEOUT:
  160. return put_user(vmwdt_interval, (int __user *)arg);
  161. case WDIOC_SETTIMEOUT:
  162. {
  163. int interval;
  164. if (get_user(interval, (int __user *)arg))
  165. return -EFAULT;
  166. if (interval < MIN_INTERVAL)
  167. return -EINVAL;
  168. vmwdt_interval = interval;
  169. }
  170. return vmwdt_keepalive();
  171. case WDIOC_KEEPALIVE:
  172. return vmwdt_keepalive();
  173. }
  174. return -EINVAL;
  175. }
  176. static ssize_t vmwdt_write(struct file *f, const char __user *buf,
  177. size_t count, loff_t *ppos)
  178. {
  179. if(count) {
  180. if (!vmwdt_nowayout) {
  181. size_t i;
  182. /* note: just in case someone wrote the magic character
  183. * five months ago... */
  184. vmwdt_expect_close = 0;
  185. for (i = 0; i != count; i++) {
  186. char c;
  187. if (get_user(c, buf+i))
  188. return -EFAULT;
  189. if (c == 'V')
  190. vmwdt_expect_close = 42;
  191. }
  192. }
  193. /* someone wrote to us, we should restart timer */
  194. vmwdt_keepalive();
  195. }
  196. return count;
  197. }
  198. static const struct file_operations vmwdt_fops = {
  199. .open = &vmwdt_open,
  200. .release = &vmwdt_close,
  201. .ioctl = &vmwdt_ioctl,
  202. .write = &vmwdt_write,
  203. .owner = THIS_MODULE,
  204. };
  205. static struct miscdevice vmwdt_dev = {
  206. .minor = WATCHDOG_MINOR,
  207. .name = "watchdog",
  208. .fops = &vmwdt_fops,
  209. };
  210. static int __init vmwdt_init(void)
  211. {
  212. int ret;
  213. ret = vmwdt_probe();
  214. if (ret)
  215. return ret;
  216. return misc_register(&vmwdt_dev);
  217. }
  218. module_init(vmwdt_init);
  219. static void __exit vmwdt_exit(void)
  220. {
  221. WARN_ON(misc_deregister(&vmwdt_dev) != 0);
  222. }
  223. module_exit(vmwdt_exit);