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. int ret = __diag288(wdt_cancel, 0, "", 0);
  100. WARN_ON(ret != 0);
  101. clear_bit(VMWDT_RUNNING, &vmwdt_is_open);
  102. return ret;
  103. }
  104. static int __init vmwdt_probe(void)
  105. {
  106. /* there is no real way to see if the watchdog is supported,
  107. * so we try initializing it with a NOP command ("BEGIN")
  108. * that won't cause any harm even if the following disable
  109. * fails for some reason */
  110. static char __initdata ebc_begin[] = {
  111. 194, 197, 199, 201, 213
  112. };
  113. if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
  114. return -EINVAL;
  115. return vmwdt_disable();
  116. }
  117. static int vmwdt_open(struct inode *i, struct file *f)
  118. {
  119. int ret;
  120. if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open))
  121. return -EBUSY;
  122. ret = vmwdt_keepalive();
  123. if (ret)
  124. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  125. return ret ? ret : nonseekable_open(i, f);
  126. }
  127. static int vmwdt_close(struct inode *i, struct file *f)
  128. {
  129. if (vmwdt_expect_close == 42)
  130. vmwdt_disable();
  131. vmwdt_expect_close = 0;
  132. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  133. return 0;
  134. }
  135. static struct watchdog_info vmwdt_info = {
  136. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  137. .firmware_version = 0,
  138. .identity = "z/VM Watchdog Timer",
  139. };
  140. static int __vmwdt_ioctl(unsigned int cmd, unsigned long arg)
  141. {
  142. switch (cmd) {
  143. case WDIOC_GETSUPPORT:
  144. if (copy_to_user((void __user *)arg, &vmwdt_info,
  145. sizeof(vmwdt_info)))
  146. return -EFAULT;
  147. return 0;
  148. case WDIOC_GETSTATUS:
  149. case WDIOC_GETBOOTSTATUS:
  150. return put_user(0, (int __user *)arg);
  151. case WDIOC_GETTEMP:
  152. return -EINVAL;
  153. case WDIOC_SETOPTIONS:
  154. {
  155. int options, ret;
  156. if (get_user(options, (int __user *)arg))
  157. return -EFAULT;
  158. ret = -EINVAL;
  159. if (options & WDIOS_DISABLECARD) {
  160. ret = vmwdt_disable();
  161. if (ret)
  162. return ret;
  163. }
  164. if (options & WDIOS_ENABLECARD) {
  165. ret = vmwdt_keepalive();
  166. }
  167. return ret;
  168. }
  169. case WDIOC_GETTIMEOUT:
  170. return put_user(vmwdt_interval, (int __user *)arg);
  171. case WDIOC_SETTIMEOUT:
  172. {
  173. int interval;
  174. if (get_user(interval, (int __user *)arg))
  175. return -EFAULT;
  176. if (interval < MIN_INTERVAL)
  177. return -EINVAL;
  178. vmwdt_interval = interval;
  179. }
  180. return vmwdt_keepalive();
  181. case WDIOC_KEEPALIVE:
  182. return vmwdt_keepalive();
  183. }
  184. return -EINVAL;
  185. }
  186. static long vmwdt_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  187. {
  188. int rc;
  189. mutex_lock(&vmwdt_mutex);
  190. rc = __vmwdt_ioctl(cmd, arg);
  191. mutex_unlock(&vmwdt_mutex);
  192. return (long) rc;
  193. }
  194. static ssize_t vmwdt_write(struct file *f, const char __user *buf,
  195. size_t count, loff_t *ppos)
  196. {
  197. if(count) {
  198. if (!vmwdt_nowayout) {
  199. size_t i;
  200. /* note: just in case someone wrote the magic character
  201. * five months ago... */
  202. vmwdt_expect_close = 0;
  203. for (i = 0; i != count; i++) {
  204. char c;
  205. if (get_user(c, buf+i))
  206. return -EFAULT;
  207. if (c == 'V')
  208. vmwdt_expect_close = 42;
  209. }
  210. }
  211. /* someone wrote to us, we should restart timer */
  212. vmwdt_keepalive();
  213. }
  214. return count;
  215. }
  216. static int vmwdt_resume(void)
  217. {
  218. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  219. return NOTIFY_DONE;
  220. }
  221. /*
  222. * It makes no sense to go into suspend while the watchdog is running.
  223. * Depending on the memory size, the watchdog might trigger, while we
  224. * are still saving the memory.
  225. * We reuse the open flag to ensure that suspend and watchdog open are
  226. * exclusive operations
  227. */
  228. static int vmwdt_suspend(void)
  229. {
  230. if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
  231. pr_err("The system cannot be suspended while the watchdog"
  232. " is in use\n");
  233. return notifier_from_errno(-EBUSY);
  234. }
  235. if (test_bit(VMWDT_RUNNING, &vmwdt_is_open)) {
  236. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  237. pr_err("The system cannot be suspended while the watchdog"
  238. " is running\n");
  239. return notifier_from_errno(-EBUSY);
  240. }
  241. return NOTIFY_DONE;
  242. }
  243. /*
  244. * This function is called for suspend and resume.
  245. */
  246. static int vmwdt_power_event(struct notifier_block *this, unsigned long event,
  247. void *ptr)
  248. {
  249. switch (event) {
  250. case PM_POST_HIBERNATION:
  251. case PM_POST_SUSPEND:
  252. return vmwdt_resume();
  253. case PM_HIBERNATION_PREPARE:
  254. case PM_SUSPEND_PREPARE:
  255. return vmwdt_suspend();
  256. default:
  257. return NOTIFY_DONE;
  258. }
  259. }
  260. static struct notifier_block vmwdt_power_notifier = {
  261. .notifier_call = vmwdt_power_event,
  262. };
  263. static const struct file_operations vmwdt_fops = {
  264. .open = &vmwdt_open,
  265. .release = &vmwdt_close,
  266. .unlocked_ioctl = &vmwdt_ioctl,
  267. .write = &vmwdt_write,
  268. .owner = THIS_MODULE,
  269. .llseek = noop_llseek,
  270. };
  271. static struct miscdevice vmwdt_dev = {
  272. .minor = WATCHDOG_MINOR,
  273. .name = "watchdog",
  274. .fops = &vmwdt_fops,
  275. };
  276. static int __init vmwdt_init(void)
  277. {
  278. int ret;
  279. ret = vmwdt_probe();
  280. if (ret)
  281. return ret;
  282. ret = register_pm_notifier(&vmwdt_power_notifier);
  283. if (ret)
  284. return ret;
  285. /*
  286. * misc_register() has to be the last action in module_init(), because
  287. * file operations will be available right after this.
  288. */
  289. ret = misc_register(&vmwdt_dev);
  290. if (ret) {
  291. unregister_pm_notifier(&vmwdt_power_notifier);
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. module_init(vmwdt_init);
  297. static void __exit vmwdt_exit(void)
  298. {
  299. unregister_pm_notifier(&vmwdt_power_notifier);
  300. misc_deregister(&vmwdt_dev);
  301. }
  302. module_exit(vmwdt_exit);