diag288_wdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 ([email protected])
  17. * Philipp Hachtmann ([email protected])
  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 <[email protected]>");
  49. MODULE_AUTHOR("Philipp Hachtmann <[email protected]>");
  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", "memory");
  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. if (MACHINE_IS_VM) {
  99. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  100. if (!ebc_cmd) {
  101. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  102. return -ENOMEM;
  103. }
  104. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  105. ASCEBC(ebc_cmd, MAX_CMDLEN);
  106. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  107. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  108. : WDT_FUNC_INIT;
  109. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  110. WARN_ON(ret != 0);
  111. kfree(ebc_cmd);
  112. } else {
  113. ret = __diag288_lpar(WDT_FUNC_INIT,
  114. dev->timeout, LPARWDT_RESTART);
  115. }
  116. if (ret) {
  117. pr_err("The watchdog cannot be activated\n");
  118. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  119. return ret;
  120. }
  121. return 0;
  122. }
  123. static int wdt_stop(struct watchdog_device *dev)
  124. {
  125. int ret;
  126. diag_stat_inc(DIAG_STAT_X288);
  127. ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
  128. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  129. return ret;
  130. }
  131. static int wdt_ping(struct watchdog_device *dev)
  132. {
  133. char *ebc_cmd;
  134. size_t len;
  135. int ret;
  136. unsigned int func;
  137. if (MACHINE_IS_VM) {
  138. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  139. if (!ebc_cmd)
  140. return -ENOMEM;
  141. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  142. ASCEBC(ebc_cmd, MAX_CMDLEN);
  143. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  144. /*
  145. * It seems to be ok to z/VM to use the init function to
  146. * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
  147. * be used when the watchdog is running.
  148. */
  149. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  150. : WDT_FUNC_INIT;
  151. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  152. WARN_ON(ret != 0);
  153. kfree(ebc_cmd);
  154. } else {
  155. ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
  156. }
  157. if (ret)
  158. pr_err("The watchdog timer cannot be started or reset\n");
  159. return ret;
  160. }
  161. static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
  162. {
  163. dev->timeout = new_to;
  164. return wdt_ping(dev);
  165. }
  166. static const struct watchdog_ops wdt_ops = {
  167. .owner = THIS_MODULE,
  168. .start = wdt_start,
  169. .stop = wdt_stop,
  170. .ping = wdt_ping,
  171. .set_timeout = wdt_set_timeout,
  172. };
  173. static const struct watchdog_info wdt_info = {
  174. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  175. .firmware_version = 0,
  176. .identity = "z Watchdog",
  177. };
  178. static struct watchdog_device wdt_dev = {
  179. .parent = NULL,
  180. .info = &wdt_info,
  181. .ops = &wdt_ops,
  182. .bootstatus = 0,
  183. .timeout = WDT_DEFAULT_TIMEOUT,
  184. .min_timeout = MIN_INTERVAL,
  185. .max_timeout = MAX_INTERVAL,
  186. };
  187. /*
  188. * It makes no sense to go into suspend while the watchdog is running.
  189. * Depending on the memory size, the watchdog might trigger, while we
  190. * are still saving the memory.
  191. */
  192. static int wdt_suspend(void)
  193. {
  194. if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
  195. pr_err("Linux cannot be suspended while the watchdog is in use\n");
  196. return notifier_from_errno(-EBUSY);
  197. }
  198. return NOTIFY_DONE;
  199. }
  200. static int wdt_resume(void)
  201. {
  202. clear_bit(DIAG_WDOG_BUSY, &wdt_status);
  203. return NOTIFY_DONE;
  204. }
  205. static int wdt_power_event(struct notifier_block *this, unsigned long event,
  206. void *ptr)
  207. {
  208. switch (event) {
  209. case PM_POST_HIBERNATION:
  210. case PM_POST_SUSPEND:
  211. return wdt_resume();
  212. case PM_HIBERNATION_PREPARE:
  213. case PM_SUSPEND_PREPARE:
  214. return wdt_suspend();
  215. default:
  216. return NOTIFY_DONE;
  217. }
  218. }
  219. static struct notifier_block wdt_power_notifier = {
  220. .notifier_call = wdt_power_event,
  221. };
  222. static int __init diag288_init(void)
  223. {
  224. int ret;
  225. char ebc_begin[] = {
  226. 194, 197, 199, 201, 213
  227. };
  228. char *ebc_cmd;
  229. watchdog_set_nowayout(&wdt_dev, nowayout_info);
  230. if (MACHINE_IS_VM) {
  231. ebc_cmd = kmalloc(sizeof(ebc_begin), GFP_KERNEL);
  232. if (!ebc_cmd) {
  233. pr_err("The watchdog cannot be initialized\n");
  234. return -ENOMEM;
  235. }
  236. memcpy(ebc_cmd, ebc_begin, sizeof(ebc_begin));
  237. ret = __diag288_vm(WDT_FUNC_INIT, 15,
  238. ebc_cmd, sizeof(ebc_begin));
  239. kfree(ebc_cmd);
  240. if (ret != 0) {
  241. pr_err("The watchdog cannot be initialized\n");
  242. return -EINVAL;
  243. }
  244. } else {
  245. if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
  246. pr_err("The watchdog cannot be initialized\n");
  247. return -EINVAL;
  248. }
  249. }
  250. if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
  251. pr_err("The watchdog cannot be deactivated\n");
  252. return -EINVAL;
  253. }
  254. ret = register_pm_notifier(&wdt_power_notifier);
  255. if (ret)
  256. return ret;
  257. ret = watchdog_register_device(&wdt_dev);
  258. if (ret)
  259. unregister_pm_notifier(&wdt_power_notifier);
  260. return ret;
  261. }
  262. static void __exit diag288_exit(void)
  263. {
  264. watchdog_unregister_device(&wdt_dev);
  265. unregister_pm_notifier(&wdt_power_notifier);
  266. }
  267. module_init(diag288_init);
  268. module_exit(diag288_exit);