2 * Watchdog implementation based on z/VM Watchdog Timer API
4 * The user space watchdog daemon can use this driver as
5 * /dev/vmwatchdog to have z/VM execute the specified CP
6 * command when the timeout expires. The default command is
7 * "IPL", which which cause an immediate reboot.
9 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/watchdog.h>
16 #include <linux/smp_lock.h>
18 #include <asm/ebcdic.h>
20 #include <asm/uaccess.h>
22 #define MAX_CMDLEN 240
23 #define MIN_INTERVAL 15
24 static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
25 static int vmwdt_conceal;
27 static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
31 MODULE_DESCRIPTION("z/VM Watchdog Timer");
32 module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
33 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
34 module_param_named(conceal, vmwdt_conceal, bool, 0644);
35 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
37 module_param_named(nowayout, vmwdt_nowayout, bool, 0);
38 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
39 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
40 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
42 static unsigned int vmwdt_interval = 60;
43 static unsigned long vmwdt_is_open;
44 static int vmwdt_expect_close;
52 wdt_conceal = 0x80000000,
55 static int __diag288(enum vmwdt_func func, unsigned int timeout,
56 char *cmd, size_t len)
58 register unsigned long __func asm("2") = func;
59 register unsigned long __timeout asm("3") = timeout;
60 register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
61 register unsigned long __cmdl asm("5") = len;
70 : "+d" (err) : "d"(__func), "d"(__timeout),
71 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
75 static int vmwdt_keepalive(void)
77 /* we allocate new memory every time to avoid having
78 * to track the state. static allocation is not an
79 * option since that might not be contiguous in real
80 * storage in case of a modular build */
86 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
90 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
91 ASCEBC(ebc_cmd, MAX_CMDLEN);
92 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
94 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
95 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
101 static int vmwdt_disable(void)
103 int ret = __diag288(wdt_cancel, 0, "", 0);
108 static int __init vmwdt_probe(void)
110 /* there is no real way to see if the watchdog is supported,
111 * so we try initializing it with a NOP command ("BEGIN")
112 * that won't cause any harm even if the following disable
113 * fails for some reason */
114 static char __initdata ebc_begin[] = {
115 194, 197, 199, 201, 213
117 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
119 return vmwdt_disable();
122 static int vmwdt_open(struct inode *i, struct file *f)
126 if (test_and_set_bit(0, &vmwdt_is_open)) {
130 ret = vmwdt_keepalive();
132 clear_bit(0, &vmwdt_is_open);
134 return ret ? ret : nonseekable_open(i, f);
137 static int vmwdt_close(struct inode *i, struct file *f)
139 if (vmwdt_expect_close == 42)
141 vmwdt_expect_close = 0;
142 clear_bit(0, &vmwdt_is_open);
146 static struct watchdog_info vmwdt_info = {
147 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
148 .firmware_version = 0,
149 .identity = "z/VM Watchdog Timer",
152 static int vmwdt_ioctl(struct inode *i, struct file *f,
153 unsigned int cmd, unsigned long arg)
156 case WDIOC_GETSUPPORT:
157 if (copy_to_user((void __user *)arg, &vmwdt_info,
161 case WDIOC_GETSTATUS:
162 case WDIOC_GETBOOTSTATUS:
163 return put_user(0, (int __user *)arg);
166 case WDIOC_SETOPTIONS:
169 if (get_user(options, (int __user *)arg))
172 if (options & WDIOS_DISABLECARD) {
173 ret = vmwdt_disable();
177 if (options & WDIOS_ENABLECARD) {
178 ret = vmwdt_keepalive();
182 case WDIOC_GETTIMEOUT:
183 return put_user(vmwdt_interval, (int __user *)arg);
184 case WDIOC_SETTIMEOUT:
187 if (get_user(interval, (int __user *)arg))
189 if (interval < MIN_INTERVAL)
191 vmwdt_interval = interval;
193 return vmwdt_keepalive();
194 case WDIOC_KEEPALIVE:
195 return vmwdt_keepalive();
201 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
202 size_t count, loff_t *ppos)
205 if (!vmwdt_nowayout) {
208 /* note: just in case someone wrote the magic character
209 * five months ago... */
210 vmwdt_expect_close = 0;
212 for (i = 0; i != count; i++) {
214 if (get_user(c, buf+i))
217 vmwdt_expect_close = 42;
220 /* someone wrote to us, we should restart timer */
226 static const struct file_operations vmwdt_fops = {
228 .release = &vmwdt_close,
229 .ioctl = &vmwdt_ioctl,
230 .write = &vmwdt_write,
231 .owner = THIS_MODULE,
234 static struct miscdevice vmwdt_dev = {
235 .minor = WATCHDOG_MINOR,
240 static int __init vmwdt_init(void)
247 return misc_register(&vmwdt_dev);
249 module_init(vmwdt_init);
251 static void __exit vmwdt_exit(void)
253 WARN_ON(misc_deregister(&vmwdt_dev) != 0);
255 module_exit(vmwdt_exit);