blob: 21a2a829bf4eb5baf20cfdfd9e71489c5987b77a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Watchdog implementation based on z/VM Watchdog Timer API
3 *
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.
8 */
9#include <linux/init.h>
10#include <linux/fs.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>
Arnd Bergmann3e0420f2008-05-20 19:17:01 +020016#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/ebcdic.h>
19#include <asm/io.h>
20#include <asm/uaccess.h>
21
22#define MAX_CMDLEN 240
23#define MIN_INTERVAL 15
24static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
25static int vmwdt_conceal;
26
Andrey Panin4bfdf372005-07-27 11:43:58 -070027static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
31MODULE_DESCRIPTION("z/VM Watchdog Timer");
32module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
33MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
34module_param_named(conceal, vmwdt_conceal, bool, 0644);
35MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
36 " is active");
37module_param_named(nowayout, vmwdt_nowayout, bool, 0);
38MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
39 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
40MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
41
42static unsigned int vmwdt_interval = 60;
43static unsigned long vmwdt_is_open;
44static int vmwdt_expect_close;
45
46enum vmwdt_func {
47 /* function codes */
48 wdt_init = 0,
49 wdt_change = 1,
50 wdt_cancel = 2,
51 /* flags */
52 wdt_conceal = 0x80000000,
53};
54
55static int __diag288(enum vmwdt_func func, unsigned int timeout,
56 char *cmd, size_t len)
57{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020058 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int err;
63
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020064 err = -EINVAL;
65 asm volatile(
66 " diag %1,%3,0x288\n"
67 "0: la %0,0\n"
68 "1:\n"
69 EX_TABLE(0b,1b)
Heiko Carstens2b12f992007-10-12 16:11:48 +020070 : "+d" (err) : "d"(__func), "d"(__timeout),
71 "d"(__cmdp), "d"(__cmdl) : "1", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return err;
73}
74
75static int vmwdt_keepalive(void)
76{
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 */
81 static char *ebc_cmd;
82 size_t len;
83 int ret;
84 unsigned int func;
85
86 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
87 if (!ebc_cmd)
88 return -ENOMEM;
89
90 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
91 ASCEBC(ebc_cmd, MAX_CMDLEN);
92 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
93
94 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
95 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
Martin Schwidefsky0d130062008-07-14 09:59:40 +020096 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kfree(ebc_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return ret;
99}
100
101static int vmwdt_disable(void)
102{
103 int ret = __diag288(wdt_cancel, 0, "", 0);
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200104 WARN_ON(ret != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return ret;
106}
107
108static int __init vmwdt_probe(void)
109{
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
116 };
Martin Schwidefsky0d130062008-07-14 09:59:40 +0200117 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return vmwdt_disable();
120}
121
122static int vmwdt_open(struct inode *i, struct file *f)
123{
124 int ret;
Arnd Bergmann3e0420f2008-05-20 19:17:01 +0200125 lock_kernel();
126 if (test_and_set_bit(0, &vmwdt_is_open)) {
127 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EBUSY;
Arnd Bergmann3e0420f2008-05-20 19:17:01 +0200129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 ret = vmwdt_keepalive();
131 if (ret)
132 clear_bit(0, &vmwdt_is_open);
Arnd Bergmann3e0420f2008-05-20 19:17:01 +0200133 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return ret ? ret : nonseekable_open(i, f);
135}
136
137static int vmwdt_close(struct inode *i, struct file *f)
138{
139 if (vmwdt_expect_close == 42)
140 vmwdt_disable();
141 vmwdt_expect_close = 0;
142 clear_bit(0, &vmwdt_is_open);
143 return 0;
144}
145
146static struct watchdog_info vmwdt_info = {
147 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
148 .firmware_version = 0,
149 .identity = "z/VM Watchdog Timer",
150};
151
152static int vmwdt_ioctl(struct inode *i, struct file *f,
153 unsigned int cmd, unsigned long arg)
154{
155 switch (cmd) {
156 case WDIOC_GETSUPPORT:
157 if (copy_to_user((void __user *)arg, &vmwdt_info,
158 sizeof(vmwdt_info)))
159 return -EFAULT;
160 return 0;
161 case WDIOC_GETSTATUS:
162 case WDIOC_GETBOOTSTATUS:
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200163 return put_user(0, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 case WDIOC_GETTEMP:
165 return -EINVAL;
166 case WDIOC_SETOPTIONS:
167 {
168 int options, ret;
169 if (get_user(options, (int __user *)arg))
170 return -EFAULT;
171 ret = -EINVAL;
172 if (options & WDIOS_DISABLECARD) {
173 ret = vmwdt_disable();
174 if (ret)
175 return ret;
176 }
177 if (options & WDIOS_ENABLECARD) {
178 ret = vmwdt_keepalive();
179 }
180 return ret;
181 }
182 case WDIOC_GETTIMEOUT:
183 return put_user(vmwdt_interval, (int __user *)arg);
184 case WDIOC_SETTIMEOUT:
185 {
186 int interval;
187 if (get_user(interval, (int __user *)arg))
188 return -EFAULT;
189 if (interval < MIN_INTERVAL)
190 return -EINVAL;
191 vmwdt_interval = interval;
192 }
193 return vmwdt_keepalive();
194 case WDIOC_KEEPALIVE:
195 return vmwdt_keepalive();
196 }
197
198 return -EINVAL;
199}
200
201static ssize_t vmwdt_write(struct file *f, const char __user *buf,
202 size_t count, loff_t *ppos)
203{
204 if(count) {
205 if (!vmwdt_nowayout) {
206 size_t i;
207
208 /* note: just in case someone wrote the magic character
209 * five months ago... */
210 vmwdt_expect_close = 0;
211
212 for (i = 0; i != count; i++) {
213 char c;
214 if (get_user(c, buf+i))
215 return -EFAULT;
216 if (c == 'V')
217 vmwdt_expect_close = 42;
218 }
219 }
220 /* someone wrote to us, we should restart timer */
221 vmwdt_keepalive();
222 }
223 return count;
224}
225
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800226static const struct file_operations vmwdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .open = &vmwdt_open,
228 .release = &vmwdt_close,
229 .ioctl = &vmwdt_ioctl,
230 .write = &vmwdt_write,
231 .owner = THIS_MODULE,
232};
233
234static struct miscdevice vmwdt_dev = {
235 .minor = WATCHDOG_MINOR,
236 .name = "watchdog",
237 .fops = &vmwdt_fops,
238};
239
240static int __init vmwdt_init(void)
241{
242 int ret;
243
244 ret = vmwdt_probe();
245 if (ret)
246 return ret;
247 return misc_register(&vmwdt_dev);
248}
249module_init(vmwdt_init);
250
251static void __exit vmwdt_exit(void)
252{
253 WARN_ON(misc_deregister(&vmwdt_dev) != 0);
254}
255module_exit(vmwdt_exit);