Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / s390 / char / vmwatchdog.c
blob22cf4fec8da9a64034d63296217152fc8cd61943
1 /*
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.
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>
17 #include <asm/ebcdic.h>
18 #include <asm/io.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 #ifdef CONFIG_WATCHDOG_NOWAYOUT
27 static int vmwdt_nowayout = 1;
28 #else
29 static int vmwdt_nowayout = 0;
30 #endif
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
34 MODULE_DESCRIPTION("z/VM Watchdog Timer");
35 module_param_string(cmd, vmwdt_cmd, MAX_CMDLEN, 0644);
36 MODULE_PARM_DESC(cmd, "CP command that is run when the watchdog triggers");
37 module_param_named(conceal, vmwdt_conceal, bool, 0644);
38 MODULE_PARM_DESC(conceal, "Enable the CONCEAL CP option while the watchdog "
39 " is active");
40 module_param_named(nowayout, vmwdt_nowayout, bool, 0);
41 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
42 " (default=CONFIG_WATCHDOG_NOWAYOUT)");
43 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
45 static unsigned int vmwdt_interval = 60;
46 static unsigned long vmwdt_is_open;
47 static int vmwdt_expect_close;
49 enum vmwdt_func {
50 /* function codes */
51 wdt_init = 0,
52 wdt_change = 1,
53 wdt_cancel = 2,
54 /* flags */
55 wdt_conceal = 0x80000000,
58 static int __diag288(enum vmwdt_func func, unsigned int timeout,
59 char *cmd, size_t len)
61 register unsigned long __func asm("2");
62 register unsigned long __timeout asm("3");
63 register unsigned long __cmdp asm("4");
64 register unsigned long __cmdl asm("5");
65 int err;
67 __func = func;
68 __timeout = timeout;
69 __cmdp = virt_to_phys(cmd);
70 __cmdl = len;
71 err = 0;
72 asm volatile (
73 #ifdef __s390x__
74 "diag %2,%4,0x288\n"
75 "1: \n"
76 ".section .fixup,\"ax\"\n"
77 "2: lghi %0,%1\n"
78 " jg 1b\n"
79 ".previous\n"
80 ".section __ex_table,\"a\"\n"
81 " .align 8\n"
82 " .quad 1b,2b\n"
83 ".previous\n"
84 #else
85 "diag %2,%4,0x288\n"
86 "1: \n"
87 ".section .fixup,\"ax\"\n"
88 "2: lhi %0,%1\n"
89 " bras 1,3f\n"
90 " .long 1b\n"
91 "3: l 1,0(1)\n"
92 " br 1\n"
93 ".previous\n"
94 ".section __ex_table,\"a\"\n"
95 " .align 4\n"
96 " .long 1b,2b\n"
97 ".previous\n"
98 #endif
99 : "+&d"(err)
100 : "i"(-EINVAL), "d"(__func), "d"(__timeout),
101 "d"(__cmdp), "d"(__cmdl)
102 : "1", "cc");
103 return err;
106 static int vmwdt_keepalive(void)
108 /* we allocate new memory every time to avoid having
109 * to track the state. static allocation is not an
110 * option since that might not be contiguous in real
111 * storage in case of a modular build */
112 static char *ebc_cmd;
113 size_t len;
114 int ret;
115 unsigned int func;
117 ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
118 if (!ebc_cmd)
119 return -ENOMEM;
121 len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
122 ASCEBC(ebc_cmd, MAX_CMDLEN);
123 EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
125 func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
126 ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
127 kfree(ebc_cmd);
129 if (ret) {
130 printk(KERN_WARNING "%s: problem setting interval %d, "
131 "cmd %s\n", __FUNCTION__, vmwdt_interval,
132 vmwdt_cmd);
134 return ret;
137 static int vmwdt_disable(void)
139 int ret = __diag288(wdt_cancel, 0, "", 0);
140 if (ret) {
141 printk(KERN_WARNING "%s: problem disabling watchdog\n",
142 __FUNCTION__);
144 return ret;
147 static int __init vmwdt_probe(void)
149 /* there is no real way to see if the watchdog is supported,
150 * so we try initializing it with a NOP command ("BEGIN")
151 * that won't cause any harm even if the following disable
152 * fails for some reason */
153 static char __initdata ebc_begin[] = {
154 194, 197, 199, 201, 213
156 if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0) {
157 printk(KERN_INFO "z/VM watchdog not available\n");
158 return -EINVAL;
160 return vmwdt_disable();
163 static int vmwdt_open(struct inode *i, struct file *f)
165 int ret;
166 if (test_and_set_bit(0, &vmwdt_is_open))
167 return -EBUSY;
168 ret = vmwdt_keepalive();
169 if (ret)
170 clear_bit(0, &vmwdt_is_open);
171 return ret ? ret : nonseekable_open(i, f);
174 static int vmwdt_close(struct inode *i, struct file *f)
176 if (vmwdt_expect_close == 42)
177 vmwdt_disable();
178 vmwdt_expect_close = 0;
179 clear_bit(0, &vmwdt_is_open);
180 return 0;
183 static struct watchdog_info vmwdt_info = {
184 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
185 .firmware_version = 0,
186 .identity = "z/VM Watchdog Timer",
189 static int vmwdt_ioctl(struct inode *i, struct file *f,
190 unsigned int cmd, unsigned long arg)
192 switch (cmd) {
193 case WDIOC_GETSUPPORT:
194 if (copy_to_user((void __user *)arg, &vmwdt_info,
195 sizeof(vmwdt_info)))
196 return -EFAULT;
197 return 0;
198 case WDIOC_GETSTATUS:
199 case WDIOC_GETBOOTSTATUS:
200 return put_user(0, (int *)arg);
201 case WDIOC_GETTEMP:
202 return -EINVAL;
203 case WDIOC_SETOPTIONS:
205 int options, ret;
206 if (get_user(options, (int __user *)arg))
207 return -EFAULT;
208 ret = -EINVAL;
209 if (options & WDIOS_DISABLECARD) {
210 ret = vmwdt_disable();
211 if (ret)
212 return ret;
214 if (options & WDIOS_ENABLECARD) {
215 ret = vmwdt_keepalive();
217 return ret;
219 case WDIOC_GETTIMEOUT:
220 return put_user(vmwdt_interval, (int __user *)arg);
221 case WDIOC_SETTIMEOUT:
223 int interval;
224 if (get_user(interval, (int __user *)arg))
225 return -EFAULT;
226 if (interval < MIN_INTERVAL)
227 return -EINVAL;
228 vmwdt_interval = interval;
230 return vmwdt_keepalive();
231 case WDIOC_KEEPALIVE:
232 return vmwdt_keepalive();
235 return -EINVAL;
238 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
239 size_t count, loff_t *ppos)
241 if(count) {
242 if (!vmwdt_nowayout) {
243 size_t i;
245 /* note: just in case someone wrote the magic character
246 * five months ago... */
247 vmwdt_expect_close = 0;
249 for (i = 0; i != count; i++) {
250 char c;
251 if (get_user(c, buf+i))
252 return -EFAULT;
253 if (c == 'V')
254 vmwdt_expect_close = 42;
257 /* someone wrote to us, we should restart timer */
258 vmwdt_keepalive();
260 return count;
263 static struct file_operations vmwdt_fops = {
264 .open = &vmwdt_open,
265 .release = &vmwdt_close,
266 .ioctl = &vmwdt_ioctl,
267 .write = &vmwdt_write,
268 .owner = THIS_MODULE,
271 static struct miscdevice vmwdt_dev = {
272 .minor = WATCHDOG_MINOR,
273 .name = "watchdog",
274 .fops = &vmwdt_fops,
277 static int __init vmwdt_init(void)
279 int ret;
281 ret = vmwdt_probe();
282 if (ret)
283 return ret;
284 return misc_register(&vmwdt_dev);
286 module_init(vmwdt_init);
288 static void __exit vmwdt_exit(void)
290 WARN_ON(misc_deregister(&vmwdt_dev) != 0);
292 module_exit(vmwdt_exit);