diag288_wdt.c 7.5 KB

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