diag288_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <linux/io.h>
  31. #include <linux/uaccess.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_MISCDEV(WATCHDOG_MINOR);
  58. MODULE_ALIAS("vmwatchdog");
  59. static int __diag288(unsigned int func, unsigned int timeout,
  60. unsigned long action, unsigned int len)
  61. {
  62. register unsigned long __func asm("2") = func;
  63. register unsigned long __timeout asm("3") = timeout;
  64. register unsigned long __action asm("4") = action;
  65. register unsigned long __len asm("5") = len;
  66. int err;
  67. err = -EINVAL;
  68. asm volatile(
  69. " diag %1, %3, 0x288\n"
  70. "0: la %0, 0\n"
  71. "1:\n"
  72. EX_TABLE(0b, 1b)
  73. : "+d" (err) : "d"(__func), "d"(__timeout),
  74. "d"(__action), "d"(__len) : "1", "cc");
  75. return err;
  76. }
  77. static int __diag288_vm(unsigned int func, unsigned int timeout,
  78. char *cmd, size_t len)
  79. {
  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. return __diag288(func, timeout, action, 0);
  86. }
  87. static int wdt_start(struct watchdog_device *dev)
  88. {
  89. char *ebc_cmd;
  90. size_t len;
  91. int ret;
  92. unsigned int func;
  93. ret = -ENODEV;
  94. if (MACHINE_IS_VM) {
  95. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  96. if (!ebc_cmd)
  97. return -ENOMEM;
  98. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  99. ASCEBC(ebc_cmd, MAX_CMDLEN);
  100. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  101. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  102. : WDT_FUNC_INIT;
  103. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  104. WARN_ON(ret != 0);
  105. kfree(ebc_cmd);
  106. }
  107. if (MACHINE_IS_LPAR) {
  108. ret = __diag288_lpar(WDT_FUNC_INIT,
  109. dev->timeout, LPARWDT_RESTART);
  110. }
  111. if (ret) {
  112. pr_err("The watchdog cannot be activated\n");
  113. return ret;
  114. }
  115. pr_info("The watchdog was activated\n");
  116. return 0;
  117. }
  118. static int wdt_stop(struct watchdog_device *dev)
  119. {
  120. int ret;
  121. ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
  122. pr_info("The watchdog was deactivated\n");
  123. return ret;
  124. }
  125. static int wdt_ping(struct watchdog_device *dev)
  126. {
  127. char *ebc_cmd;
  128. size_t len;
  129. int ret;
  130. unsigned int func;
  131. ret = -ENODEV;
  132. if (MACHINE_IS_VM) {
  133. ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
  134. if (!ebc_cmd)
  135. return -ENOMEM;
  136. len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
  137. ASCEBC(ebc_cmd, MAX_CMDLEN);
  138. EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
  139. /*
  140. * It seems to be ok to z/VM to use the init function to
  141. * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
  142. * be used when the watchdog is running.
  143. */
  144. func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
  145. : WDT_FUNC_INIT;
  146. ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
  147. WARN_ON(ret != 0);
  148. kfree(ebc_cmd);
  149. }
  150. if (MACHINE_IS_LPAR)
  151. ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
  152. if (ret)
  153. pr_err("The watchdog timer cannot be started or reset\n");
  154. return ret;
  155. }
  156. static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
  157. {
  158. dev->timeout = new_to;
  159. return wdt_ping(dev);
  160. }
  161. static struct watchdog_ops wdt_ops = {
  162. .owner = THIS_MODULE,
  163. .start = wdt_start,
  164. .stop = wdt_stop,
  165. .ping = wdt_ping,
  166. .set_timeout = wdt_set_timeout,
  167. };
  168. static struct watchdog_info wdt_info = {
  169. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  170. .firmware_version = 0,
  171. .identity = "z Watchdog",
  172. };
  173. static struct watchdog_device wdt_dev = {
  174. .parent = NULL,
  175. .info = &wdt_info,
  176. .ops = &wdt_ops,
  177. .bootstatus = 0,
  178. .timeout = WDT_DEFAULT_TIMEOUT,
  179. .min_timeout = MIN_INTERVAL,
  180. .max_timeout = MAX_INTERVAL,
  181. };
  182. /*
  183. * It makes no sense to go into suspend while the watchdog is running.
  184. * Depending on the memory size, the watchdog might trigger, while we
  185. * are still saving the memory.
  186. * We reuse the open flag to ensure that suspend and watchdog open are
  187. * exclusive operations
  188. */
  189. static int wdt_suspend(void)
  190. {
  191. if (test_and_set_bit(WDOG_DEV_OPEN, &wdt_dev.status)) {
  192. pr_err("Linux cannot be suspended while the watchdog is in use\n");
  193. return notifier_from_errno(-EBUSY);
  194. }
  195. if (test_bit(WDOG_ACTIVE, &wdt_dev.status)) {
  196. clear_bit(WDOG_DEV_OPEN, &wdt_dev.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(WDOG_DEV_OPEN, &wdt_dev.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. pr_info("The watchdog device driver detected a z/VM environment\n");
  233. if (__diag288_vm(WDT_FUNC_INIT, 15,
  234. ebc_begin, sizeof(ebc_begin)) != 0) {
  235. pr_err("The watchdog cannot be initialized\n");
  236. return -EINVAL;
  237. }
  238. } else if (MACHINE_IS_LPAR) {
  239. pr_info("The watchdog device driver detected an LPAR environment\n");
  240. if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
  241. pr_err("The watchdog cannot be initialized\n");
  242. return -EINVAL;
  243. }
  244. } else {
  245. pr_err("Linux runs in an environment that does not support the diag288 watchdog\n");
  246. return -ENODEV;
  247. }
  248. if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
  249. pr_err("The watchdog cannot be deactivated\n");
  250. return -EINVAL;
  251. }
  252. ret = register_pm_notifier(&wdt_power_notifier);
  253. if (ret)
  254. return ret;
  255. ret = watchdog_register_device(&wdt_dev);
  256. if (ret)
  257. unregister_pm_notifier(&wdt_power_notifier);
  258. return ret;
  259. }
  260. static void __exit diag288_exit(void)
  261. {
  262. watchdog_unregister_device(&wdt_dev);
  263. unregister_pm_notifier(&wdt_power_notifier);
  264. }
  265. module_init(diag288_init);
  266. module_exit(diag288_exit);