vmwatchdog.c 7.8 KB

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