vmwatchdog.c 8.1 KB

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