vmwatchdog.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <asm/ebcdic.h>
  22. #include <asm/io.h>
  23. #include <asm/uaccess.h>
  24. #define MAX_CMDLEN 240
  25. #define MIN_INTERVAL 15
  26. static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
  27. static int vmwdt_conceal;
  28. static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
  29. MODULE_LICENSE("GPL");
  30. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
  31. MODULE_DESCRIPTION("z/VM Watchdog Timer");
  32. module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
  33. MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
  34. module_param_named(conceal, vmwdt_conceal, bool, 0644);
  35. MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
  36. " is active");
  37. module_param_named(nowayout, vmwdt_nowayout, bool, 0);
  38. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
  39. " (default=CONFIG_WATCHDOG_NOWAYOUT)");
  40. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  41. static unsigned int vmwdt_interval = 60;
  42. static unsigned long vmwdt_is_open;
  43. static int vmwdt_expect_close;
  44. static DEFINE_MUTEX(vmwdt_mutex);
  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. if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open))
  120. return -EBUSY;
  121. ret = vmwdt_keepalive();
  122. if (ret)
  123. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  124. return ret ? ret : nonseekable_open(i, f);
  125. }
  126. static int vmwdt_close(struct inode *i, struct file *f)
  127. {
  128. if (vmwdt_expect_close == 42)
  129. vmwdt_disable();
  130. vmwdt_expect_close = 0;
  131. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  132. return 0;
  133. }
  134. static struct watchdog_info vmwdt_info = {
  135. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  136. .firmware_version = 0,
  137. .identity = "z/VM Watchdog Timer",
  138. };
  139. static int __vmwdt_ioctl(unsigned int cmd, unsigned long arg)
  140. {
  141. switch (cmd) {
  142. case WDIOC_GETSUPPORT:
  143. if (copy_to_user((void __user *)arg, &vmwdt_info,
  144. sizeof(vmwdt_info)))
  145. return -EFAULT;
  146. return 0;
  147. case WDIOC_GETSTATUS:
  148. case WDIOC_GETBOOTSTATUS:
  149. return put_user(0, (int __user *)arg);
  150. case WDIOC_GETTEMP:
  151. return -EINVAL;
  152. case WDIOC_SETOPTIONS:
  153. {
  154. int options, ret;
  155. if (get_user(options, (int __user *)arg))
  156. return -EFAULT;
  157. ret = -EINVAL;
  158. if (options & WDIOS_DISABLECARD) {
  159. ret = vmwdt_disable();
  160. if (ret)
  161. return ret;
  162. }
  163. if (options & WDIOS_ENABLECARD) {
  164. ret = vmwdt_keepalive();
  165. }
  166. return ret;
  167. }
  168. case WDIOC_GETTIMEOUT:
  169. return put_user(vmwdt_interval, (int __user *)arg);
  170. case WDIOC_SETTIMEOUT:
  171. {
  172. int interval;
  173. if (get_user(interval, (int __user *)arg))
  174. return -EFAULT;
  175. if (interval < MIN_INTERVAL)
  176. return -EINVAL;
  177. vmwdt_interval = interval;
  178. }
  179. return vmwdt_keepalive();
  180. case WDIOC_KEEPALIVE:
  181. return vmwdt_keepalive();
  182. }
  183. return -EINVAL;
  184. }
  185. static long vmwdt_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  186. {
  187. int rc;
  188. mutex_lock(&vmwdt_mutex);
  189. rc = __vmwdt_ioctl(cmd, arg);
  190. mutex_unlock(&vmwdt_mutex);
  191. return (long) rc;
  192. }
  193. static ssize_t vmwdt_write(struct file *f, const char __user *buf,
  194. size_t count, loff_t *ppos)
  195. {
  196. if(count) {
  197. if (!vmwdt_nowayout) {
  198. size_t i;
  199. /* note: just in case someone wrote the magic character
  200. * five months ago... */
  201. vmwdt_expect_close = 0;
  202. for (i = 0; i != count; i++) {
  203. char c;
  204. if (get_user(c, buf+i))
  205. return -EFAULT;
  206. if (c == 'V')
  207. vmwdt_expect_close = 42;
  208. }
  209. }
  210. /* someone wrote to us, we should restart timer */
  211. vmwdt_keepalive();
  212. }
  213. return count;
  214. }
  215. static int vmwdt_resume(void)
  216. {
  217. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  218. return NOTIFY_DONE;
  219. }
  220. /*
  221. * It makes no sense to go into suspend while the watchdog is running.
  222. * Depending on the memory size, the watchdog might trigger, while we
  223. * are still saving the memory.
  224. * We reuse the open flag to ensure that suspend and watchdog open are
  225. * exclusive operations
  226. */
  227. static int vmwdt_suspend(void)
  228. {
  229. if (test_and_set_bit(VMWDT_OPEN, &vmwdt_is_open)) {
  230. pr_err("The system cannot be suspended while the watchdog"
  231. " is in use\n");
  232. return NOTIFY_BAD;
  233. }
  234. if (test_bit(VMWDT_RUNNING, &vmwdt_is_open)) {
  235. clear_bit(VMWDT_OPEN, &vmwdt_is_open);
  236. pr_err("The system cannot be suspended while the watchdog"
  237. " is running\n");
  238. return NOTIFY_BAD;
  239. }
  240. return NOTIFY_DONE;
  241. }
  242. /*
  243. * This function is called for suspend and resume.
  244. */
  245. static int vmwdt_power_event(struct notifier_block *this, unsigned long event,
  246. void *ptr)
  247. {
  248. switch (event) {
  249. case PM_POST_HIBERNATION:
  250. case PM_POST_SUSPEND:
  251. return vmwdt_resume();
  252. case PM_HIBERNATION_PREPARE:
  253. case PM_SUSPEND_PREPARE:
  254. return vmwdt_suspend();
  255. default:
  256. return NOTIFY_DONE;
  257. }
  258. }
  259. static struct notifier_block vmwdt_power_notifier = {
  260. .notifier_call = vmwdt_power_event,
  261. };
  262. static const struct file_operations vmwdt_fops = {
  263. .open = &vmwdt_open,
  264. .release = &vmwdt_close,
  265. .unlocked_ioctl = &vmwdt_ioctl,
  266. .write = &vmwdt_write,
  267. .owner = THIS_MODULE,
  268. };
  269. static struct miscdevice vmwdt_dev = {
  270. .minor = WATCHDOG_MINOR,
  271. .name = "watchdog",
  272. .fops = &vmwdt_fops,
  273. };
  274. static int __init vmwdt_init(void)
  275. {
  276. int ret;
  277. ret = vmwdt_probe();
  278. if (ret)
  279. return ret;
  280. ret = register_pm_notifier(&vmwdt_power_notifier);
  281. if (ret)
  282. return ret;
  283. /*
  284. * misc_register() has to be the last action in module_init(), because
  285. * file operations will be available right after this.
  286. */
  287. ret = misc_register(&vmwdt_dev);
  288. if (ret) {
  289. unregister_pm_notifier(&vmwdt_power_notifier);
  290. return ret;
  291. }
  292. return 0;
  293. }
  294. module_init(vmwdt_init);
  295. static void __exit vmwdt_exit(void)
  296. {
  297. unregister_pm_notifier(&vmwdt_power_notifier);
  298. misc_deregister(&vmwdt_dev);
  299. }
  300. module_exit(vmwdt_exit);