diag288_wdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for z/VM and LPAR using the diag 288 interface.
  4. *
  5. * Under z/VM, expiration of the watchdog will send a "system restart" command
  6. * to CP.
  7. *
  8. * The command can be altered using the module parameter "cmd". This is
  9. * not recommended because it's only supported on z/VM but not whith LPAR.
  10. *
  11. * On LPAR, the watchdog will always trigger a system restart. the module
  12. * paramter cmd is meaningless here.
  13. *
  14. *
  15. * Copyright IBM Corp. 2004, 2013
  16. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  17. * Philipp Hachtmann (phacht@de.ibm.com)
  18. *
  19. */
  20. #define KMSG_COMPONENT "diag288_wdt"
  21. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/slab.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/suspend.h>
  29. #include <asm/ebcdic.h>
  30. #include <asm/diag.h>
  31. #include <linux/io.h>
  32. #define MAX_CMDLEN 240
  33. #define DEFAULT_CMD "SYSTEM RESTART"
  34. #define MIN_INTERVAL 15 /* Minimal time supported by diag88 */
  35. #define MAX_INTERVAL 3600 /* One hour should be enough - pure estimation */
  36. #define WDT_DEFAULT_TIMEOUT 30
  37. /* Function codes - init, change, cancel */
  38. #define WDT_FUNC_INIT 0
  39. #define WDT_FUNC_CHANGE 1
  40. #define WDT_FUNC_CANCEL 2
  41. #define WDT_FUNC_CONCEAL 0x80000000
  42. /* Action codes for LPAR watchdog */
  43. #define LPARWDT_RESTART 0
  44. static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD;
  45. static bool conceal_on;
  46. static bool nowayout_info = WATCHDOG_NOWAYOUT;
  47. MODULE_LICENSE("GPL");
  48. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
  49. MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
  50. MODULE_DESCRIPTION("System z diag288 Watchdog Timer");
  51. module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644);
  52. MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)");
  53. module_param_named(conceal, conceal_on, bool, 0644);
  54. MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
  55. module_param_named(nowayout, nowayout_info, bool, 0444);
  56. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
  57. MODULE_ALIAS("vmwatchdog");
  58. static int __diag288(unsigned int func, unsigned int timeout,
  59. unsigned long action, unsigned int len)
  60. {
  61. register unsigned long __func asm("2") = func;
  62. register unsigned long __timeout asm("3") = timeout;
  63. register unsigned long __action asm("4") = action;
  64. register unsigned long __len asm("5") = len;
  65. int err;
  66. err = -EINVAL;
  67. asm volatile(
  68. " diag %1, %3, 0x288\n"
  69. "0: la %0, 0\n"
  70. "1:\n"
  71. EX_TABLE(0b, 1b)
  72. : "+d" (err) : "d"(__func), "d"(__timeout),
  73. "d"(__action), "d"(__len) : "1", "cc");
  74. return err;
  75. }
  76. static int __diag288_vm(unsigned int func, unsigned int timeout,
  77. char *cmd, size_t len)
  78. {
  79. diag_stat_inc(DIAG_STAT_X288);
  80. return __diag288(func, timeout, virt_to_phys(cmd), len);
  81. }
  82. static int __diag288_lpar(unsigned int func, unsigned int timeout,
  83. unsigned long action)
  84. {
  85. diag_stat_inc(DIAG_STAT_X288);
  86. return __diag288(func, timeout, action, 0);
  87. }
  88. static unsigned long wdt_status;
  89. #define DIAG_WDOG_BUSY 0
  90. static int wdt_start(struct watchdog_device *dev)
  91. {
  92. char *ebc_cmd;
  93. size_t len;
  94. int ret;
  95. unsigned int func;
  96. if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status))
  97. return -EBUSY;
  98. ret = -ENODEV;
  99. if (MACHINE_IS_VM) {
  100. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  101. if (!ebc_cmd) {
  102. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  103. return -ENOMEM;
  104. }
  105. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  106. ASCEBC(ebc_cmd, MAX_CMDLEN);
  107. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  108. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  109. : WDT_FUNC_INIT;
  110. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  111. WARN_ON(ret != 0);
  112. kfree(ebc_cmd);
  113. } else {
  114. ret = __diag288_lpar(WDT_FUNC_INIT,
  115. dev->timeout, LPARWDT_RESTART);
  116. }
  117. if (ret) {
  118. pr_err("The watchdog cannot be activated\n");
  119. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static int wdt_stop(struct watchdog_device *dev)
  125. {
  126. int ret;
  127. diag_stat_inc(DIAG_STAT_X288);
  128. ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
  129. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  130. return ret;
  131. }
  132. static int wdt_ping(struct watchdog_device *dev)
  133. {
  134. char *ebc_cmd;
  135. size_t len;
  136. int ret;
  137. unsigned int func;
  138. ret = -ENODEV;
  139. if (MACHINE_IS_VM) {
  140. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  141. if (!ebc_cmd)
  142. return -ENOMEM;
  143. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  144. ASCEBC(ebc_cmd, MAX_CMDLEN);
  145. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  146. /*
  147. * It seems to be ok to z/VM to use the init function to
  148. * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
  149. * be used when the watchdog is running.
  150. */
  151. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  152. : WDT_FUNC_INIT;
  153. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  154. WARN_ON(ret != 0);
  155. kfree(ebc_cmd);
  156. } else {
  157. ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
  158. }
  159. if (ret)
  160. pr_err("The watchdog timer cannot be started or reset\n");
  161. return ret;
  162. }
  163. static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
  164. {
  165. dev->timeout = new_to;
  166. return wdt_ping(dev);
  167. }
  168. static const struct watchdog_ops wdt_ops = {
  169. .owner = THIS_MODULE,
  170. .start = wdt_start,
  171. .stop = wdt_stop,
  172. .ping = wdt_ping,
  173. .set_timeout = wdt_set_timeout,
  174. };
  175. static const struct watchdog_info wdt_info = {
  176. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  177. .firmware_version = 0,
  178. .identity = "z Watchdog",
  179. };
  180. static struct watchdog_device wdt_dev = {
  181. .parent = NULL,
  182. .info = &wdt_info,
  183. .ops = &wdt_ops,
  184. .bootstatus = 0,
  185. .timeout = WDT_DEFAULT_TIMEOUT,
  186. .min_timeout = MIN_INTERVAL,
  187. .max_timeout = MAX_INTERVAL,
  188. };
  189. /*
  190. * It makes no sense to go into suspend while the watchdog is running.
  191. * Depending on the memory size, the watchdog might trigger, while we
  192. * are still saving the memory.
  193. */
  194. static int wdt_suspend(void)
  195. {
  196. if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
  197. pr_err("Linux cannot be suspended while the watchdog is in use\n");
  198. return notifier_from_errno(-EBUSY);
  199. }
  200. return NOTIFY_DONE;
  201. }
  202. static int wdt_resume(void)
  203. {
  204. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  205. return NOTIFY_DONE;
  206. }
  207. static int wdt_power_event(struct notifier_block *this, unsigned long event,
  208. void *ptr)
  209. {
  210. switch (event) {
  211. case PM_POST_HIBERNATION:
  212. case PM_POST_SUSPEND:
  213. return wdt_resume();
  214. case PM_HIBERNATION_PREPARE:
  215. case PM_SUSPEND_PREPARE:
  216. return wdt_suspend();
  217. default:
  218. return NOTIFY_DONE;
  219. }
  220. }
  221. static struct notifier_block wdt_power_notifier = {
  222. .notifier_call = wdt_power_event,
  223. };
  224. static int __init diag288_init(void)
  225. {
  226. int ret;
  227. char ebc_begin[] = {
  228. 194, 197, 199, 201, 213
  229. };
  230. watchdog_set_nowayout(&wdt_dev, nowayout_info);
  231. if (MACHINE_IS_VM) {
  232. if (__diag288_vm(WDT_FUNC_INIT, 15,
  233. ebc_begin, sizeof(ebc_begin)) != 0) {
  234. pr_err("The watchdog cannot be initialized\n");
  235. return -EINVAL;
  236. }
  237. } else {
  238. if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
  239. pr_err("The watchdog cannot be initialized\n");
  240. return -EINVAL;
  241. }
  242. }
  243. if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
  244. pr_err("The watchdog cannot be deactivated\n");
  245. return -EINVAL;
  246. }
  247. ret = register_pm_notifier(&wdt_power_notifier);
  248. if (ret)
  249. return ret;
  250. ret = watchdog_register_device(&wdt_dev);
  251. if (ret)
  252. unregister_pm_notifier(&wdt_power_notifier);
  253. return ret;
  254. }
  255. static void __exit diag288_exit(void)
  256. {
  257. watchdog_unregister_device(&wdt_dev);
  258. unregister_pm_notifier(&wdt_power_notifier);
  259. }
  260. module_init(diag288_init);
  261. module_exit(diag288_exit);