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/slab.h>
21 #include <linux/suspend.h>
22 #include <linux/watchdog.h>
24 #include <asm/ebcdic.h>
26 #include <asm/uaccess.h>
28 #define MAX_CMDLEN 240
29 #define MIN_INTERVAL 15
30 static char vmwdt_cmd
[MAX_CMDLEN
] = "IPL";
31 static bool vmwdt_conceal
;
33 static bool vmwdt_nowayout
= WATCHDOG_NOWAYOUT
;
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
37 MODULE_DESCRIPTION("z/VM Watchdog Timer");
38 module_param_string(cmd
, vmwdt_cmd
, MAX_CMDLEN
, 0644);
39 MODULE_PARM_DESC(cmd
, "CP command that is run when the watchdog triggers");
40 module_param_named(conceal
, vmwdt_conceal
, bool, 0644);
41 MODULE_PARM_DESC(conceal
, "Enable the CONCEAL CP option while the watchdog "
43 module_param_named(nowayout
, vmwdt_nowayout
, bool, 0);
44 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started"
45 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
46 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
48 static unsigned int vmwdt_interval
= 60;
49 static unsigned long vmwdt_is_open
;
50 static int vmwdt_expect_close
;
52 static DEFINE_MUTEX(vmwdt_mutex
);
54 #define VMWDT_OPEN 0 /* devnode is open or suspend in progress */
55 #define VMWDT_RUNNING 1 /* The watchdog is armed */
63 wdt_conceal
= 0x80000000,
66 static int __diag288(enum vmwdt_func func
, unsigned int timeout
,
67 char *cmd
, size_t len
)
69 register unsigned long __func
asm("2") = func
;
70 register unsigned long __timeout
asm("3") = timeout
;
71 register unsigned long __cmdp
asm("4") = virt_to_phys(cmd
);
72 register unsigned long __cmdl
asm("5") = len
;
81 : "+d" (err
) : "d"(__func
), "d"(__timeout
),
82 "d"(__cmdp
), "d"(__cmdl
) : "1", "cc");
86 static int vmwdt_keepalive(void)
88 /* we allocate new memory every time to avoid having
89 * to track the state. static allocation is not an
90 * option since that might not be contiguous in real
91 * storage in case of a modular build */
97 ebc_cmd
= kmalloc(MAX_CMDLEN
, GFP_KERNEL
);
101 len
= strlcpy(ebc_cmd
, vmwdt_cmd
, MAX_CMDLEN
);
102 ASCEBC(ebc_cmd
, MAX_CMDLEN
);
103 EBC_TOUPPER(ebc_cmd
, MAX_CMDLEN
);
105 func
= vmwdt_conceal
? (wdt_init
| wdt_conceal
) : wdt_init
;
106 set_bit(VMWDT_RUNNING
, &vmwdt_is_open
);
107 ret
= __diag288(func
, vmwdt_interval
, ebc_cmd
, len
);
113 static int vmwdt_disable(void)
116 int ret
= __diag288(wdt_cancel
, 0, cmd
, 0);
118 clear_bit(VMWDT_RUNNING
, &vmwdt_is_open
);
122 static int __init
vmwdt_probe(void)
124 /* there is no real way to see if the watchdog is supported,
125 * so we try initializing it with a NOP command ("BEGIN")
126 * that won't cause any harm even if the following disable
127 * fails for some reason */
129 194, 197, 199, 201, 213
131 if (__diag288(wdt_init
, 15, ebc_begin
, sizeof(ebc_begin
)) != 0)
133 return vmwdt_disable();
136 static int vmwdt_open(struct inode
*i
, struct file
*f
)
139 if (test_and_set_bit(VMWDT_OPEN
, &vmwdt_is_open
))
141 ret
= vmwdt_keepalive();
143 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
144 return ret
? ret
: nonseekable_open(i
, f
);
147 static int vmwdt_close(struct inode
*i
, struct file
*f
)
149 if (vmwdt_expect_close
== 42)
151 vmwdt_expect_close
= 0;
152 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
156 static struct watchdog_info vmwdt_info
= {
157 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
158 .firmware_version
= 0,
159 .identity
= "z/VM Watchdog Timer",
162 static int __vmwdt_ioctl(unsigned int cmd
, unsigned long arg
)
165 case WDIOC_GETSUPPORT
:
166 if (copy_to_user((void __user
*)arg
, &vmwdt_info
,
170 case WDIOC_GETSTATUS
:
171 case WDIOC_GETBOOTSTATUS
:
172 return put_user(0, (int __user
*)arg
);
175 case WDIOC_SETOPTIONS
:
178 if (get_user(options
, (int __user
*)arg
))
181 if (options
& WDIOS_DISABLECARD
) {
182 ret
= vmwdt_disable();
186 if (options
& WDIOS_ENABLECARD
) {
187 ret
= vmwdt_keepalive();
191 case WDIOC_GETTIMEOUT
:
192 return put_user(vmwdt_interval
, (int __user
*)arg
);
193 case WDIOC_SETTIMEOUT
:
196 if (get_user(interval
, (int __user
*)arg
))
198 if (interval
< MIN_INTERVAL
)
200 vmwdt_interval
= interval
;
202 return vmwdt_keepalive();
203 case WDIOC_KEEPALIVE
:
204 return vmwdt_keepalive();
209 static long vmwdt_ioctl(struct file
*f
, unsigned int cmd
, unsigned long arg
)
213 mutex_lock(&vmwdt_mutex
);
214 rc
= __vmwdt_ioctl(cmd
, arg
);
215 mutex_unlock(&vmwdt_mutex
);
219 static ssize_t
vmwdt_write(struct file
*f
, const char __user
*buf
,
220 size_t count
, loff_t
*ppos
)
223 if (!vmwdt_nowayout
) {
226 /* note: just in case someone wrote the magic character
227 * five months ago... */
228 vmwdt_expect_close
= 0;
230 for (i
= 0; i
!= count
; i
++) {
232 if (get_user(c
, buf
+i
))
235 vmwdt_expect_close
= 42;
238 /* someone wrote to us, we should restart timer */
244 static int vmwdt_resume(void)
246 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
251 * It makes no sense to go into suspend while the watchdog is running.
252 * Depending on the memory size, the watchdog might trigger, while we
253 * are still saving the memory.
254 * We reuse the open flag to ensure that suspend and watchdog open are
255 * exclusive operations
257 static int vmwdt_suspend(void)
259 if (test_and_set_bit(VMWDT_OPEN
, &vmwdt_is_open
)) {
260 pr_err("The system cannot be suspended while the watchdog"
262 return notifier_from_errno(-EBUSY
);
264 if (test_bit(VMWDT_RUNNING
, &vmwdt_is_open
)) {
265 clear_bit(VMWDT_OPEN
, &vmwdt_is_open
);
266 pr_err("The system cannot be suspended while the watchdog"
268 return notifier_from_errno(-EBUSY
);
274 * This function is called for suspend and resume.
276 static int vmwdt_power_event(struct notifier_block
*this, unsigned long event
,
280 case PM_POST_HIBERNATION
:
281 case PM_POST_SUSPEND
:
282 return vmwdt_resume();
283 case PM_HIBERNATION_PREPARE
:
284 case PM_SUSPEND_PREPARE
:
285 return vmwdt_suspend();
291 static struct notifier_block vmwdt_power_notifier
= {
292 .notifier_call
= vmwdt_power_event
,
295 static const struct file_operations vmwdt_fops
= {
297 .release
= &vmwdt_close
,
298 .unlocked_ioctl
= &vmwdt_ioctl
,
299 .write
= &vmwdt_write
,
300 .owner
= THIS_MODULE
,
301 .llseek
= noop_llseek
,
304 static struct miscdevice vmwdt_dev
= {
305 .minor
= WATCHDOG_MINOR
,
310 static int __init
vmwdt_init(void)
317 ret
= register_pm_notifier(&vmwdt_power_notifier
);
321 * misc_register() has to be the last action in module_init(), because
322 * file operations will be available right after this.
324 ret
= misc_register(&vmwdt_dev
);
326 unregister_pm_notifier(&vmwdt_power_notifier
);
331 module_init(vmwdt_init
);
333 static void __exit
vmwdt_exit(void)
335 unregister_pm_notifier(&vmwdt_power_notifier
);
336 misc_deregister(&vmwdt_dev
);
338 module_exit(vmwdt_exit
);