2 * Watchdog implementation based on z/VM Watchdog Timer API
4 * Copyright IBM Corp. 2004,2009
6 * The user space watchdog daemon can use this driver as
7 * /dev/vmwatchdog to have z/VM execute the specified CP
8 * command when the timeout expires. The default command is
9 * "IPL", which which cause an immediate reboot.
11 #define KMSG_COMPONENT "vmwatchdog"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/suspend.h>
21 #include <linux/watchdog.h>
23 #include <asm/ebcdic.h>
25 #include <asm/uaccess.h>
27 #define MAX_CMDLEN 240
28 #define MIN_INTERVAL 15
29 static char vmwdt_cmd
[MAX_CMDLEN
] = "IPL";
30 static int vmwdt_conceal
;
32 static int vmwdt_nowayout
= WATCHDOG_NOWAYOUT
;
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
36 MODULE_DESCRIPTION("z/VM Watchdog Timer");
37 module_param_string(cmd
, vmwdt_cmd
, MAX_CMDLEN
, 0644);
38 MODULE_PARM_DESC(cmd
, "CP command that is run when the watchdog triggers");
39 module_param_named(conceal
, vmwdt_conceal
, bool, 0644);
40 MODULE_PARM_DESC(conceal
, "Enable the CONCEAL CP option while the watchdog "
42 module_param_named(nowayout
, vmwdt_nowayout
, bool, 0);
43 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started"
44 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
45 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
47 static unsigned int vmwdt_interval
= 60;
48 static unsigned long vmwdt_is_open
;
49 static int vmwdt_expect_close
;
51 static DEFINE_MUTEX(vmwdt_mutex
);
53 #define VMWDT_OPEN 0 /* devnode is open or suspend in progress */
54 #define VMWDT_RUNNING 1 /* The watchdog is armed */
62 wdt_conceal
= 0x80000000,
65 static int __diag288(enum vmwdt_func func
, unsigned int timeout
,
66 char *cmd
, size_t len
)
68 register unsigned long __func
asm("2") = func
;
69 register unsigned long __timeout
asm("3") = timeout
;
70 register unsigned long __cmdp
asm("4") = virt_to_phys(cmd
);
71 register unsigned long __cmdl
asm("5") = len
;
80 : "+d" (err
) : "d"(__func
), "d"(__timeout
),
81 "d"(__cmdp
), "d"(__cmdl
) : "1", "cc");
85 static int vmwdt_keepalive(void)
87 /* we allocate new memory every time to avoid having
88 * to track the state. static allocation is not an
89 * option since that might not be contiguous in real
90 * storage in case of a modular build */
96 ebc_cmd
= kmalloc(MAX_CMDLEN
, GFP_KERNEL
);
100 len
= strlcpy(ebc_cmd
, vmwdt_cmd
, MAX_CMDLEN
);
101 ASCEBC(ebc_cmd
, MAX_CMDLEN
);
102 EBC_TOUPPER(ebc_cmd
, MAX_CMDLEN
);
104 func
= vmwdt_conceal
? (wdt_init
| wdt_conceal
) : wdt_init
;
105 set_bit(VMWDT_RUNNING
, &vmwdt_is_open
);
106 ret
= __diag288(func
, vmwdt_interval
, ebc_cmd
, len
);
112 static int vmwdt_disable(void)
114 int ret
= __diag288(wdt_cancel
, 0, "", 0);
116 clear_bit(VMWDT_RUNNING
, &vmwdt_is_open
);
120 static int __init
vmwdt_probe(void)
122 /* there is no real way to see if the watchdog is supported,
123 * so we try initializing it with a NOP command ("BEGIN")
124 * that won't cause any harm even if the following disable
125 * fails for some reason */
126 static char __initdata ebc_begin
[] = {
127 194, 197, 199, 201, 213
129 if (__diag288(wdt_init
, 15, ebc_begin
, sizeof(ebc_begin
)) != 0)
131 return vmwdt_disable();
134 static int vmwdt_open(struct inode
*i
, struct file
*f
)
137 if (test_and_set_bit(VMWDT_OPEN
, &vmwdt_is_open
))
139 ret
= vmwdt_keepalive();
141 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
142 return ret
? ret
: nonseekable_open(i
, f
);
145 static int vmwdt_close(struct inode
*i
, struct file
*f
)
147 if (vmwdt_expect_close
== 42)
149 vmwdt_expect_close
= 0;
150 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
154 static struct watchdog_info vmwdt_info
= {
155 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
156 .firmware_version
= 0,
157 .identity
= "z/VM Watchdog Timer",
160 static int __vmwdt_ioctl(unsigned int cmd
, unsigned long arg
)
163 case WDIOC_GETSUPPORT
:
164 if (copy_to_user((void __user
*)arg
, &vmwdt_info
,
168 case WDIOC_GETSTATUS
:
169 case WDIOC_GETBOOTSTATUS
:
170 return put_user(0, (int __user
*)arg
);
173 case WDIOC_SETOPTIONS
:
176 if (get_user(options
, (int __user
*)arg
))
179 if (options
& WDIOS_DISABLECARD
) {
180 ret
= vmwdt_disable();
184 if (options
& WDIOS_ENABLECARD
) {
185 ret
= vmwdt_keepalive();
189 case WDIOC_GETTIMEOUT
:
190 return put_user(vmwdt_interval
, (int __user
*)arg
);
191 case WDIOC_SETTIMEOUT
:
194 if (get_user(interval
, (int __user
*)arg
))
196 if (interval
< MIN_INTERVAL
)
198 vmwdt_interval
= interval
;
200 return vmwdt_keepalive();
201 case WDIOC_KEEPALIVE
:
202 return vmwdt_keepalive();
207 static long vmwdt_ioctl(struct file
*f
, unsigned int cmd
, unsigned long arg
)
211 mutex_lock(&vmwdt_mutex
);
212 rc
= __vmwdt_ioctl(cmd
, arg
);
213 mutex_unlock(&vmwdt_mutex
);
217 static ssize_t
vmwdt_write(struct file
*f
, const char __user
*buf
,
218 size_t count
, loff_t
*ppos
)
221 if (!vmwdt_nowayout
) {
224 /* note: just in case someone wrote the magic character
225 * five months ago... */
226 vmwdt_expect_close
= 0;
228 for (i
= 0; i
!= count
; i
++) {
230 if (get_user(c
, buf
+i
))
233 vmwdt_expect_close
= 42;
236 /* someone wrote to us, we should restart timer */
242 static int vmwdt_resume(void)
244 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
249 * It makes no sense to go into suspend while the watchdog is running.
250 * Depending on the memory size, the watchdog might trigger, while we
251 * are still saving the memory.
252 * We reuse the open flag to ensure that suspend and watchdog open are
253 * exclusive operations
255 static int vmwdt_suspend(void)
257 if (test_and_set_bit(VMWDT_OPEN
, &vmwdt_is_open
)) {
258 pr_err("The system cannot be suspended while the watchdog"
262 if (test_bit(VMWDT_RUNNING
, &vmwdt_is_open
)) {
263 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
264 pr_err("The system cannot be suspended while the watchdog"
272 * This function is called for suspend and resume.
274 static int vmwdt_power_event(struct notifier_block
*this, unsigned long event
,
278 case PM_POST_HIBERNATION
:
279 case PM_POST_SUSPEND
:
280 return vmwdt_resume();
281 case PM_HIBERNATION_PREPARE
:
282 case PM_SUSPEND_PREPARE
:
283 return vmwdt_suspend();
289 static struct notifier_block vmwdt_power_notifier
= {
290 .notifier_call
= vmwdt_power_event
,
293 static const struct file_operations vmwdt_fops
= {
295 .release
= &vmwdt_close
,
296 .unlocked_ioctl
= &vmwdt_ioctl
,
297 .write
= &vmwdt_write
,
298 .owner
= THIS_MODULE
,
301 static struct miscdevice vmwdt_dev
= {
302 .minor
= WATCHDOG_MINOR
,
307 static int __init
vmwdt_init(void)
314 ret
= register_pm_notifier(&vmwdt_power_notifier
);
318 * misc_register() has to be the last action in module_init(), because
319 * file operations will be available right after this.
321 ret
= misc_register(&vmwdt_dev
);
323 unregister_pm_notifier(&vmwdt_power_notifier
);
328 module_init(vmwdt_init
);
330 static void __exit
vmwdt_exit(void)
332 unregister_pm_notifier(&vmwdt_power_notifier
);
333 misc_deregister(&vmwdt_dev
);
335 module_exit(vmwdt_exit
);