diag288_wdt.c 7.5 KB

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