Merge git://git.kernel.org/pub/scm/linux/kernel/git/hskinnemoen/avr32-2.6
[pandora-kernel.git] / drivers / s390 / char / vmwatchdog.c
1 /*
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>
16
17 #include <asm/ebcdic.h>
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20
21 #define MAX_CMDLEN 240
22 #define MIN_INTERVAL 15
23 static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
24 static int vmwdt_conceal;
25
26 static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
27
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 "
35                 " is active");
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);
40
41 static unsigned int vmwdt_interval = 60;
42 static unsigned long vmwdt_is_open;
43 static int vmwdt_expect_close;
44
45 enum vmwdt_func {
46         /* function codes */
47         wdt_init   = 0,
48         wdt_change = 1,
49         wdt_cancel = 2,
50         /* flags */
51         wdt_conceal = 0x80000000,
52 };
53
54 static int __diag288(enum vmwdt_func func, unsigned int timeout,
55                             char *cmd, size_t len)
56 {
57         register unsigned long __func asm("2") = func;
58         register unsigned long __timeout asm("3") = timeout;
59         register unsigned long __cmdp asm("4") = virt_to_phys(cmd);
60         register unsigned long __cmdl asm("5") = len;
61         int err;
62
63         err = -EINVAL;
64         asm volatile(
65                 "       diag    %1,%3,0x288\n"
66                 "0:     la      %0,0\n"
67                 "1:\n"
68                 EX_TABLE(0b,1b)
69                 : "+d" (err) : "d"(__func), "d"(__timeout),
70                   "d"(__cmdp), "d"(__cmdl) : "1", "cc");
71         return err;
72 }
73
74 static int vmwdt_keepalive(void)
75 {
76         /* we allocate new memory every time to avoid having
77          * to track the state. static allocation is not an
78          * option since that might not be contiguous in real
79          * storage in case of a modular build */
80         static char *ebc_cmd;
81         size_t len;
82         int ret;
83         unsigned int func;
84
85         ebc_cmd = kmalloc(MAX_CMDLEN, GFP_KERNEL);
86         if (!ebc_cmd)
87                 return -ENOMEM;
88
89         len = strlcpy(ebc_cmd, vmwdt_cmd, MAX_CMDLEN);
90         ASCEBC(ebc_cmd, MAX_CMDLEN);
91         EBC_TOUPPER(ebc_cmd, MAX_CMDLEN);
92
93         func = vmwdt_conceal ? (wdt_init | wdt_conceal) : wdt_init;
94         ret = __diag288(func, vmwdt_interval, ebc_cmd, len);
95         WARN_ON(ret != 0);
96         kfree(ebc_cmd);
97         return ret;
98 }
99
100 static int vmwdt_disable(void)
101 {
102         int ret = __diag288(wdt_cancel, 0, "", 0);
103         WARN_ON(ret != 0);
104         return ret;
105 }
106
107 static int __init vmwdt_probe(void)
108 {
109         /* there is no real way to see if the watchdog is supported,
110          * so we try initializing it with a NOP command ("BEGIN")
111          * that won't cause any harm even if the following disable
112          * fails for some reason */
113         static char __initdata ebc_begin[] = {
114                 194, 197, 199, 201, 213
115         };
116         if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0)
117                 return -EINVAL;
118         return vmwdt_disable();
119 }
120
121 static int vmwdt_open(struct inode *i, struct file *f)
122 {
123         int ret;
124         if (test_and_set_bit(0, &vmwdt_is_open))
125                 return -EBUSY;
126         ret = vmwdt_keepalive();
127         if (ret)
128                 clear_bit(0, &vmwdt_is_open);
129         return ret ? ret : nonseekable_open(i, f);
130 }
131
132 static int vmwdt_close(struct inode *i, struct file *f)
133 {
134         if (vmwdt_expect_close == 42)
135                 vmwdt_disable();
136         vmwdt_expect_close = 0;
137         clear_bit(0, &vmwdt_is_open);
138         return 0;
139 }
140
141 static struct watchdog_info vmwdt_info = {
142         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
143         .firmware_version = 0,
144         .identity = "z/VM Watchdog Timer",
145 };
146
147 static int vmwdt_ioctl(struct inode *i, struct file *f,
148                           unsigned int cmd, unsigned long arg)
149 {
150         switch (cmd) {
151         case WDIOC_GETSUPPORT:
152                 if (copy_to_user((void __user *)arg, &vmwdt_info,
153                                         sizeof(vmwdt_info)))
154                         return -EFAULT;
155                 return 0;
156         case WDIOC_GETSTATUS:
157         case WDIOC_GETBOOTSTATUS:
158                 return put_user(0, (int __user *)arg);
159         case WDIOC_GETTEMP:
160                 return -EINVAL;
161         case WDIOC_SETOPTIONS:
162                 {
163                         int options, ret;
164                         if (get_user(options, (int __user *)arg))
165                                 return -EFAULT;
166                         ret = -EINVAL;
167                         if (options & WDIOS_DISABLECARD) {
168                                 ret = vmwdt_disable();
169                                 if (ret)
170                                         return ret;
171                         }
172                         if (options & WDIOS_ENABLECARD) {
173                                 ret = vmwdt_keepalive();
174                         }
175                         return ret;
176                 }
177         case WDIOC_GETTIMEOUT:
178                 return put_user(vmwdt_interval, (int __user *)arg);
179         case WDIOC_SETTIMEOUT:
180                 {
181                         int interval;
182                         if (get_user(interval, (int __user *)arg))
183                                 return -EFAULT;
184                         if (interval < MIN_INTERVAL)
185                                 return -EINVAL;
186                         vmwdt_interval = interval;
187                 }
188                 return vmwdt_keepalive();
189         case WDIOC_KEEPALIVE:
190                 return vmwdt_keepalive();
191         }
192
193         return -EINVAL;
194 }
195
196 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
197                                 size_t count, loff_t *ppos)
198 {
199         if(count) {
200                 if (!vmwdt_nowayout) {
201                         size_t i;
202
203                         /* note: just in case someone wrote the magic character
204                          * five months ago... */
205                         vmwdt_expect_close = 0;
206
207                         for (i = 0; i != count; i++) {
208                                 char c;
209                                 if (get_user(c, buf+i))
210                                         return -EFAULT;
211                                 if (c == 'V')
212                                         vmwdt_expect_close = 42;
213                         }
214                 }
215                 /* someone wrote to us, we should restart timer */
216                 vmwdt_keepalive();
217         }
218         return count;
219 }
220
221 static const struct file_operations vmwdt_fops = {
222         .open    = &vmwdt_open,
223         .release = &vmwdt_close,
224         .ioctl   = &vmwdt_ioctl,
225         .write   = &vmwdt_write,
226         .owner   = THIS_MODULE,
227 };
228
229 static struct miscdevice vmwdt_dev = {
230         .minor      = WATCHDOG_MINOR,
231         .name       = "watchdog",
232         .fops       = &vmwdt_fops,
233 };
234
235 static int __init vmwdt_init(void)
236 {
237         int ret;
238
239         ret = vmwdt_probe();
240         if (ret)
241                 return ret;
242         return misc_register(&vmwdt_dev);
243 }
244 module_init(vmwdt_init);
245
246 static void __exit vmwdt_exit(void)
247 {
248         WARN_ON(misc_deregister(&vmwdt_dev) != 0);
249 }
250 module_exit(vmwdt_exit);