diag288_wdt.c 7.4 KB

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