1 // SPDX-License-Identifier: GPL-2.0-only
3 * Watchdog driver for z/VM and LPAR using the diag 288 interface.
5 * Under z/VM, expiration of the watchdog will send a "system restart" command
8 * 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.
11 * On LPAR, the watchdog will always trigger a system restart. the module
12 * paramter cmd is meaningless here.
15 * Copyright IBM Corp. 2004, 2013
16 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
17 * Philipp Hachtmann (phacht@de.ibm.com)
21 #define KMSG_COMPONENT "diag288_wdt"
22 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
29 #include <linux/watchdog.h>
30 #include <asm/ebcdic.h>
34 #define MAX_CMDLEN 240
35 #define DEFAULT_CMD "SYSTEM RESTART"
37 #define MIN_INTERVAL 15 /* Minimal time supported by diag88 */
38 #define MAX_INTERVAL 3600 /* One hour should be enough - pure estimation */
40 #define WDT_DEFAULT_TIMEOUT 30
42 /* Function codes - init, change, cancel */
43 #define WDT_FUNC_INIT 0
44 #define WDT_FUNC_CHANGE 1
45 #define WDT_FUNC_CANCEL 2
46 #define WDT_FUNC_CONCEAL 0x80000000
48 /* Action codes for LPAR watchdog */
49 #define LPARWDT_RESTART 0
51 static char wdt_cmd
[MAX_CMDLEN
] = DEFAULT_CMD
;
52 static bool conceal_on
;
53 static bool nowayout_info
= WATCHDOG_NOWAYOUT
;
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
57 MODULE_AUTHOR("Philipp Hachtmann <phacht@de.ibm.com>");
59 MODULE_DESCRIPTION("System z diag288 Watchdog Timer");
61 module_param_string(cmd
, wdt_cmd
, MAX_CMDLEN
, 0644);
62 MODULE_PARM_DESC(cmd
, "CP command that is run when the watchdog triggers (z/VM only)");
64 module_param_named(conceal
, conceal_on
, bool, 0644);
65 MODULE_PARM_DESC(conceal
, "Enable the CONCEAL CP option while the watchdog is active (z/VM only)");
67 module_param_named(nowayout
, nowayout_info
, bool, 0444);
68 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default = CONFIG_WATCHDOG_NOWAYOUT)");
70 MODULE_ALIAS("vmwatchdog");
74 static int diag288(unsigned int func
, unsigned int timeout
,
75 unsigned long action
, unsigned int len
)
77 union register_pair r1
= { .even
= func
, .odd
= timeout
, };
78 union register_pair r3
= { .even
= action
, .odd
= len
, };
81 diag_stat_inc(DIAG_STAT_X288
);
85 " diag %[r1],%[r3],0x288\n"
90 : [r1
] "d" (r1
.pair
), [r3
] "d" (r3
.pair
)
95 static int diag288_str(unsigned int func
, unsigned int timeout
, char *cmd
)
99 len
= strscpy(cmd_buf
, cmd
, MAX_CMDLEN
);
102 ASCEBC(cmd_buf
, MAX_CMDLEN
);
103 EBC_TOUPPER(cmd_buf
, MAX_CMDLEN
);
105 return diag288(func
, timeout
, virt_to_phys(cmd_buf
), len
);
108 static int wdt_start(struct watchdog_device
*dev
)
114 func
= conceal_on
? (WDT_FUNC_INIT
| WDT_FUNC_CONCEAL
)
116 ret
= diag288_str(func
, dev
->timeout
, wdt_cmd
);
119 ret
= diag288(WDT_FUNC_INIT
, dev
->timeout
, LPARWDT_RESTART
, 0);
123 pr_err("The watchdog cannot be activated\n");
129 static int wdt_stop(struct watchdog_device
*dev
)
131 return diag288(WDT_FUNC_CANCEL
, 0, 0, 0);
134 static int wdt_ping(struct watchdog_device
*dev
)
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.
145 func
= conceal_on
? (WDT_FUNC_INIT
| WDT_FUNC_CONCEAL
)
148 ret
= diag288_str(func
, dev
->timeout
, wdt_cmd
);
151 ret
= diag288(WDT_FUNC_CHANGE
, dev
->timeout
, 0, 0);
155 pr_err("The watchdog timer cannot be started or reset\n");
159 static int wdt_set_timeout(struct watchdog_device
* dev
, unsigned int new_to
)
161 dev
->timeout
= new_to
;
162 return wdt_ping(dev
);
165 static const struct watchdog_ops wdt_ops
= {
166 .owner
= THIS_MODULE
,
170 .set_timeout
= wdt_set_timeout
,
173 static const struct watchdog_info wdt_info
= {
174 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
175 .firmware_version
= 0,
176 .identity
= "z Watchdog",
179 static struct watchdog_device wdt_dev
= {
184 .timeout
= WDT_DEFAULT_TIMEOUT
,
185 .min_timeout
= MIN_INTERVAL
,
186 .max_timeout
= MAX_INTERVAL
,
189 static int __init
diag288_init(void)
193 watchdog_set_nowayout(&wdt_dev
, nowayout_info
);
196 cmd_buf
= kmalloc(MAX_CMDLEN
, GFP_KERNEL
);
198 pr_err("The watchdog cannot be initialized\n");
202 ret
= diag288_str(WDT_FUNC_INIT
, MIN_INTERVAL
, "BEGIN");
204 pr_err("The watchdog cannot be initialized\n");
209 if (diag288(WDT_FUNC_INIT
, WDT_DEFAULT_TIMEOUT
,
210 LPARWDT_RESTART
, 0)) {
211 pr_err("The watchdog cannot be initialized\n");
216 if (diag288(WDT_FUNC_CANCEL
, 0, 0, 0)) {
217 pr_err("The watchdog cannot be deactivated\n");
221 return watchdog_register_device(&wdt_dev
);
224 static void __exit
diag288_exit(void)
226 watchdog_unregister_device(&wdt_dev
);
230 module_init(diag288_init
);
231 module_exit(diag288_exit
);