blob: aafc8d98bf9fd4dbd8a97c038438b5a2e39f608e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +02002/*
Philipp Hachtmann646f9192014-06-05 11:02:36 +02003 * Watchdog driver for z/VM and LPAR using the diag 288 interface.
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +02004 *
5 * Under z/VM, expiration of the watchdog will send a "system restart" command
6 * to CP.
7 *
Philipp Hachtmann646f9192014-06-05 11:02:36 +02008 * 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 *
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020014 *
15 * Copyright IBM Corp. 2004, 2013
16 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
17 * Philipp Hachtmann (phacht@de.ibm.com)
18 *
19 */
20
21#define KMSG_COMPONENT "diag288_wdt"
22#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
23
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/slab.h>
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020029#include <linux/watchdog.h>
30#include <linux/suspend.h>
31#include <asm/ebcdic.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020032#include <asm/diag.h>
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020033#include <linux/io.h>
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020034
35#define MAX_CMDLEN 240
36#define DEFAULT_CMD "SYSTEM RESTART"
37
38#define MIN_INTERVAL 15 /* Minimal time supported by diag88 */
39#define MAX_INTERVAL 3600 /* One hour should be enough - pure estimation */
40
41#define WDT_DEFAULT_TIMEOUT 30
42
43/* Function codes - init, change, cancel */
44#define WDT_FUNC_INIT 0
45#define WDT_FUNC_CHANGE 1
46#define WDT_FUNC_CANCEL 2
47#define WDT_FUNC_CONCEAL 0x80000000
48
Philipp Hachtmann646f9192014-06-05 11:02:36 +020049/* Action codes for LPAR watchdog */
50#define LPARWDT_RESTART 0
51
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020052static char wdt_cmd[MAX_CMDLEN] = DEFAULT_CMD;
53static bool conceal_on;
54static bool nowayout_info = WATCHDOG_NOWAYOUT;
55
56MODULE_LICENSE("GPL");
57MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
58MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
59
60MODULE_DESCRIPTION("System z diag288 Watchdog Timer");
61
62module_param_string(cmd, wdt_cmd, MAX_CMDLEN, 0644);
63MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers (z/VM only)");
64
65module_param_named(conceal, conceal_on, bool, 0644);
66MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
67
68module_param_named(nowayout, nowayout_info, bool, 0444);
69MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
70
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020071MODULE_ALIAS("vmwatchdog");
72
73static int __diag288(unsigned int func, unsigned int timeout,
74 unsigned long action, unsigned int len)
75{
76 register unsigned long __func asm("2") = func;
77 register unsigned long __timeout asm("3") = timeout;
78 register unsigned long __action asm("4") = action;
79 register unsigned long __len asm("5") = len;
80 int err;
81
82 err = -EINVAL;
83 asm volatile(
84 " diag %1, %3, 0x288\n"
85 "0: la %0, 0\n"
86 "1:\n"
87 EX_TABLE(0b, 1b)
88 : "+d" (err) : "d"(__func), "d"(__timeout),
89 "d"(__action), "d"(__len) : "1", "cc");
90 return err;
91}
92
93static int __diag288_vm(unsigned int func, unsigned int timeout,
94 char *cmd, size_t len)
95{
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020096 diag_stat_inc(DIAG_STAT_X288);
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +020097 return __diag288(func, timeout, virt_to_phys(cmd), len);
98}
99
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200100static int __diag288_lpar(unsigned int func, unsigned int timeout,
101 unsigned long action)
102{
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200103 diag_stat_inc(DIAG_STAT_X288);
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200104 return __diag288(func, timeout, action, 0);
105}
106
Guenter Roecke7d162f2015-12-25 16:01:41 -0800107static unsigned long wdt_status;
108
109#define DIAG_WDOG_BUSY 0
110
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200111static int wdt_start(struct watchdog_device *dev)
112{
113 char *ebc_cmd;
114 size_t len;
115 int ret;
116 unsigned int func;
117
Guenter Roecke7d162f2015-12-25 16:01:41 -0800118 if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status))
119 return -EBUSY;
120
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200121 ret = -ENODEV;
122
123 if (MACHINE_IS_VM) {
124 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
Guenter Roecke7d162f2015-12-25 16:01:41 -0800125 if (!ebc_cmd) {
126 clear_bit(DIAG_WDOG_BUSY, &wdt_status);
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200127 return -ENOMEM;
Guenter Roecke7d162f2015-12-25 16:01:41 -0800128 }
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200129 len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
130 ASCEBC(ebc_cmd, MAX_CMDLEN);
131 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
132
133 func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
134 : WDT_FUNC_INIT;
135 ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
136 WARN_ON(ret != 0);
137 kfree(ebc_cmd);
Xu Wangb2527d22015-03-06 16:26:30 +0800138 } else {
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200139 ret = __diag288_lpar(WDT_FUNC_INIT,
140 dev->timeout, LPARWDT_RESTART);
141 }
142
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200143 if (ret) {
144 pr_err("The watchdog cannot be activated\n");
Guenter Roecke7d162f2015-12-25 16:01:41 -0800145 clear_bit(DIAG_WDOG_BUSY, &wdt_status);
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200146 return ret;
147 }
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200148 return 0;
149}
150
151static int wdt_stop(struct watchdog_device *dev)
152{
153 int ret;
154
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200155 diag_stat_inc(DIAG_STAT_X288);
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200156 ret = __diag288(WDT_FUNC_CANCEL, 0, 0, 0);
Guenter Roecke7d162f2015-12-25 16:01:41 -0800157
158 clear_bit(DIAG_WDOG_BUSY, &wdt_status);
159
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200160 return ret;
161}
162
163static int wdt_ping(struct watchdog_device *dev)
164{
165 char *ebc_cmd;
166 size_t len;
167 int ret;
168 unsigned int func;
169
170 ret = -ENODEV;
171
172 if (MACHINE_IS_VM) {
173 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
174 if (!ebc_cmd)
175 return -ENOMEM;
176 len = strlcpy(ebc_cmd, wdt_cmd, MAX_CMDLEN);
177 ASCEBC(ebc_cmd, MAX_CMDLEN);
178 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
179
180 /*
181 * It seems to be ok to z/VM to use the init function to
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200182 * retrigger the watchdog. On LPAR WDT_FUNC_CHANGE must
183 * be used when the watchdog is running.
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200184 */
185 func = conceal_on ? (WDT_FUNC_INIT | WDT_FUNC_CONCEAL)
186 : WDT_FUNC_INIT;
187
188 ret = __diag288_vm(func, dev->timeout, ebc_cmd, len);
189 WARN_ON(ret != 0);
190 kfree(ebc_cmd);
Xu Wangb2527d22015-03-06 16:26:30 +0800191 } else {
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200192 ret = __diag288_lpar(WDT_FUNC_CHANGE, dev->timeout, 0);
Xu Wangb2527d22015-03-06 16:26:30 +0800193 }
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200194
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200195 if (ret)
196 pr_err("The watchdog timer cannot be started or reset\n");
197 return ret;
198}
199
200static int wdt_set_timeout(struct watchdog_device * dev, unsigned int new_to)
201{
202 dev->timeout = new_to;
203 return wdt_ping(dev);
204}
205
Bhumika Goyalb893e342017-01-28 13:11:17 +0530206static const struct watchdog_ops wdt_ops = {
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200207 .owner = THIS_MODULE,
208 .start = wdt_start,
209 .stop = wdt_stop,
210 .ping = wdt_ping,
211 .set_timeout = wdt_set_timeout,
212};
213
Julia Lawall323edb22017-08-03 23:21:31 +0200214static const struct watchdog_info wdt_info = {
Xu Wang9ec6cb82015-03-06 16:26:29 +0800215 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200216 .firmware_version = 0,
217 .identity = "z Watchdog",
218};
219
220static struct watchdog_device wdt_dev = {
221 .parent = NULL,
222 .info = &wdt_info,
223 .ops = &wdt_ops,
224 .bootstatus = 0,
225 .timeout = WDT_DEFAULT_TIMEOUT,
226 .min_timeout = MIN_INTERVAL,
227 .max_timeout = MAX_INTERVAL,
228};
229
230/*
231 * It makes no sense to go into suspend while the watchdog is running.
232 * Depending on the memory size, the watchdog might trigger, while we
233 * are still saving the memory.
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200234 */
235static int wdt_suspend(void)
236{
Guenter Roecke7d162f2015-12-25 16:01:41 -0800237 if (test_and_set_bit(DIAG_WDOG_BUSY, &wdt_status)) {
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200238 pr_err("Linux cannot be suspended while the watchdog is in use\n");
239 return notifier_from_errno(-EBUSY);
240 }
241 return NOTIFY_DONE;
242}
243
244static int wdt_resume(void)
245{
Guenter Roecke7d162f2015-12-25 16:01:41 -0800246 clear_bit(DIAG_WDOG_BUSY, &wdt_status);
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200247 return NOTIFY_DONE;
248}
249
250static int wdt_power_event(struct notifier_block *this, unsigned long event,
251 void *ptr)
252{
253 switch (event) {
254 case PM_POST_HIBERNATION:
255 case PM_POST_SUSPEND:
256 return wdt_resume();
257 case PM_HIBERNATION_PREPARE:
258 case PM_SUSPEND_PREPARE:
259 return wdt_suspend();
260 default:
261 return NOTIFY_DONE;
262 }
263}
264
265static struct notifier_block wdt_power_notifier = {
266 .notifier_call = wdt_power_event,
267};
268
269static int __init diag288_init(void)
270{
271 int ret;
272 char ebc_begin[] = {
273 194, 197, 199, 201, 213
274 };
275
276 watchdog_set_nowayout(&wdt_dev, nowayout_info);
277
278 if (MACHINE_IS_VM) {
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200279 if (__diag288_vm(WDT_FUNC_INIT, 15,
280 ebc_begin, sizeof(ebc_begin)) != 0) {
281 pr_err("The watchdog cannot be initialized\n");
282 return -EINVAL;
283 }
Xu Wangb2527d22015-03-06 16:26:30 +0800284 } else {
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200285 if (__diag288_lpar(WDT_FUNC_INIT, 30, LPARWDT_RESTART)) {
286 pr_err("The watchdog cannot be initialized\n");
287 return -EINVAL;
288 }
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200289 }
290
Philipp Hachtmann646f9192014-06-05 11:02:36 +0200291 if (__diag288_lpar(WDT_FUNC_CANCEL, 0, 0)) {
Philipp Hachtmannf7a94db2014-06-05 11:01:43 +0200292 pr_err("The watchdog cannot be deactivated\n");
293 return -EINVAL;
294 }
295
296 ret = register_pm_notifier(&wdt_power_notifier);
297 if (ret)
298 return ret;
299
300 ret = watchdog_register_device(&wdt_dev);
301 if (ret)
302 unregister_pm_notifier(&wdt_power_notifier);
303
304 return ret;
305}
306
307static void __exit diag288_exit(void)
308{
309 watchdog_unregister_device(&wdt_dev);
310 unregister_pm_notifier(&wdt_power_notifier);
311}
312
313module_init(diag288_init);
314module_exit(diag288_exit);