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