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>
17 #include <asm/ebcdic.h>
19 #include <asm/uaccess.h>
21 #define MAX_CMDLEN 240
22 #define MIN_INTERVAL 15
23 static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
24 static int vmwdt_conceal;
26 static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
30 MODULE_DESCRIPTION("z/VM Watchdog Timer");
31 module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
32 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
33 module_param_named(conceal, vmwdt_conceal, bool, 0644);
34 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
36 module_param_named(nowayout, vmwdt_nowayout, bool, 0);
37 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
38 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
39 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
41 static unsigned int vmwdt_interval = 60;
42 static unsigned long vmwdt_is_open;
43 static int vmwdt_expect_close;
51 wdt_conceal = 0x80000000,
54 static int __diag288(enum vmwdt_func func, unsigned int timeout,
55 char *cmd, size_t len)
57 register unsigned long __func asm("2");
58 register unsigned long __timeout asm("3");
59 register unsigned long __cmdp asm("4");
60 register unsigned long __cmdl asm("5");
65 __cmdp = virt_to_phys(cmd);
72 ".section .fixup,\"ax\"\n"
76 ".section __ex_table,\"a\"\n"
83 ".section .fixup,\"ax\"\n"
90 ".section __ex_table,\"a\"\n"
96 : "i"(-EINVAL), "d"(__func), "d"(__timeout),
97 "d"(__cmdp), "d"(__cmdl)
102 static int vmwdt_keepalive(void)
104 /* we allocate new memory every time to avoid having
105 * to track the state. static allocation is not an
106 * option since that might not be contiguous in real
107 * storage in case of a modular build */
108 static char *ebc_cmd;
113 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
117 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
118 ASCEBC(ebc_cmd, MAX_CMDLEN);
119 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
121 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
122 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
126 printk(KERN_WARNING "%s: problem setting interval %d, "
127 "cmd %s\n", __FUNCTION__, vmwdt_interval,
133 static int vmwdt_disable(void)
135 int ret = __diag288(wdt_cancel, 0, "", 0);
137 printk(KERN_WARNING "%s: problem disabling watchdog\n",
143 static int __init vmwdt_probe(void)
145 /* there is no real way to see if the watchdog is supported,
146 * so we try initializing it with a NOP command ("BEGIN")
147 * that won't cause any harm even if the following disable
148 * fails for some reason */
149 static char __initdata ebc_begin[] = {
150 194, 197, 199, 201, 213
152 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0) {
153 printk(KERN_INFO "z/VM watchdog not available\n");
156 return vmwdt_disable();
159 static int vmwdt_open(struct inode *i, struct file *f)
162 if (test_and_set_bit(0, &vmwdt_is_open))
164 ret = vmwdt_keepalive();
166 clear_bit(0, &vmwdt_is_open);
167 return ret ? ret : nonseekable_open(i, f);
170 static int vmwdt_close(struct inode *i, struct file *f)
172 if (vmwdt_expect_close == 42)
174 vmwdt_expect_close = 0;
175 clear_bit(0, &vmwdt_is_open);
179 static struct watchdog_info vmwdt_info = {
180 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
181 .firmware_version = 0,
182 .identity = "z/VM Watchdog Timer",
185 static int vmwdt_ioctl(struct inode *i, struct file *f,
186 unsigned int cmd, unsigned long arg)
189 case WDIOC_GETSUPPORT:
190 if (copy_to_user((void __user *)arg, &vmwdt_info,
194 case WDIOC_GETSTATUS:
195 case WDIOC_GETBOOTSTATUS:
196 return put_user(0, (int __user *)arg);
199 case WDIOC_SETOPTIONS:
202 if (get_user(options, (int __user *)arg))
205 if (options & WDIOS_DISABLECARD) {
206 ret = vmwdt_disable();
210 if (options & WDIOS_ENABLECARD) {
211 ret = vmwdt_keepalive();
215 case WDIOC_GETTIMEOUT:
216 return put_user(vmwdt_interval, (int __user *)arg);
217 case WDIOC_SETTIMEOUT:
220 if (get_user(interval, (int __user *)arg))
222 if (interval < MIN_INTERVAL)
224 vmwdt_interval = interval;
226 return vmwdt_keepalive();
227 case WDIOC_KEEPALIVE:
228 return vmwdt_keepalive();
234 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
235 size_t count, loff_t *ppos)
238 if (!vmwdt_nowayout) {
241 /* note: just in case someone wrote the magic character
242 * five months ago... */
243 vmwdt_expect_close = 0;
245 for (i = 0; i != count; i++) {
247 if (get_user(c, buf+i))
250 vmwdt_expect_close = 42;
253 /* someone wrote to us, we should restart timer */
259 static struct file_operations vmwdt_fops = {
261 .release = &vmwdt_close,
262 .ioctl = &vmwdt_ioctl,
263 .write = &vmwdt_write,
264 .owner = THIS_MODULE,
267 static struct miscdevice vmwdt_dev = {
268 .minor = WATCHDOG_MINOR,
273 static int __init vmwdt_init(void)
280 return misc_register(&vmwdt_dev);
282 module_init(vmwdt_init);
284 static void __exit vmwdt_exit(void)
286 WARN_ON(misc_deregister(&vmwdt_dev) != 0);
288 module_exit(vmwdt_exit);