Merge commit 'v2.6.26' into bkl-removal
[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 #include <linux/smp_lock.h>
17
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
24 static char vmwdt_cmd[MAX_CMDLEN] = "IPL";
25 static int vmwdt_conceal;
26
27 static int vmwdt_nowayout = WATCHDOG_NOWAYOUT;
28
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 "
36                 " is active");
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);
41
42 static unsigned int vmwdt_interval = 60;
43 static unsigned long vmwdt_is_open;
44 static int vmwdt_expect_close;
45
46 enum 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
55 static int __diag288(enum vmwdt_func func, unsigned int timeout,
56                             char *cmd, size_t len)
57 {
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;
62         int err;
63
64         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)
70                 : "+d" (err) : "d"(__func), "d"(__timeout),
71                   "d"(__cmdp), "d"(__cmdl) : "1", "cc");
72         return err;
73 }
74
75 static 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);
96         kfree(ebc_cmd);
97
98         if (ret) {
99                 printk(KERN_WARNING "%s: problem setting interval %d, "
100                         "cmd %s\n", __func__, vmwdt_interval,
101                         vmwdt_cmd);
102         }
103         return ret;
104 }
105
106 static int vmwdt_disable(void)
107 {
108         int ret = __diag288(wdt_cancel, 0, "", 0);
109         if (ret) {
110                 printk(KERN_WARNING "%s: problem disabling watchdog\n",
111                         __func__);
112         }
113         return ret;
114 }
115
116 static int __init vmwdt_probe(void)
117 {
118         /* there is no real way to see if the watchdog is supported,
119          * so we try initializing it with a NOP command ("BEGIN")
120          * that won't cause any harm even if the following disable
121          * fails for some reason */
122         static char __initdata ebc_begin[] = {
123                 194, 197, 199, 201, 213
124         };
125         if (__diag288(wdt_init, 15, ebc_begin, sizeof(ebc_begin)) != 0) {
126                 printk(KERN_INFO "z/VM watchdog not available\n");
127                 return -EINVAL;
128         }
129         return vmwdt_disable();
130 }
131
132 static int vmwdt_open(struct inode *i, struct file *f)
133 {
134         int ret;
135         lock_kernel();
136         if (test_and_set_bit(0, &vmwdt_is_open)) {
137                 unlock_kernel();
138                 return -EBUSY;
139         }
140         ret = vmwdt_keepalive();
141         if (ret)
142                 clear_bit(0, &vmwdt_is_open);
143         unlock_kernel();
144         return ret ? ret : nonseekable_open(i, f);
145 }
146
147 static int vmwdt_close(struct inode *i, struct file *f)
148 {
149         if (vmwdt_expect_close == 42)
150                 vmwdt_disable();
151         vmwdt_expect_close = 0;
152         clear_bit(0, &vmwdt_is_open);
153         return 0;
154 }
155
156 static struct watchdog_info vmwdt_info = {
157         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
158         .firmware_version = 0,
159         .identity = "z/VM Watchdog Timer",
160 };
161
162 static int vmwdt_ioctl(struct inode *i, struct file *f,
163                           unsigned int cmd, unsigned long arg)
164 {
165         switch (cmd) {
166         case WDIOC_GETSUPPORT:
167                 if (copy_to_user((void __user *)arg, &vmwdt_info,
168                                         sizeof(vmwdt_info)))
169                         return -EFAULT;
170                 return 0;
171         case WDIOC_GETSTATUS:
172         case WDIOC_GETBOOTSTATUS:
173                 return put_user(0, (int __user *)arg);
174         case WDIOC_GETTEMP:
175                 return -EINVAL;
176         case WDIOC_SETOPTIONS:
177                 {
178                         int options, ret;
179                         if (get_user(options, (int __user *)arg))
180                                 return -EFAULT;
181                         ret = -EINVAL;
182                         if (options & WDIOS_DISABLECARD) {
183                                 ret = vmwdt_disable();
184                                 if (ret)
185                                         return ret;
186                         }
187                         if (options & WDIOS_ENABLECARD) {
188                                 ret = vmwdt_keepalive();
189                         }
190                         return ret;
191                 }
192         case WDIOC_GETTIMEOUT:
193                 return put_user(vmwdt_interval, (int __user *)arg);
194         case WDIOC_SETTIMEOUT:
195                 {
196                         int interval;
197                         if (get_user(interval, (int __user *)arg))
198                                 return -EFAULT;
199                         if (interval < MIN_INTERVAL)
200                                 return -EINVAL;
201                         vmwdt_interval = interval;
202                 }
203                 return vmwdt_keepalive();
204         case WDIOC_KEEPALIVE:
205                 return vmwdt_keepalive();
206         }
207
208         return -EINVAL;
209 }
210
211 static ssize_t vmwdt_write(struct file *f, const char __user *buf,
212                                 size_t count, loff_t *ppos)
213 {
214         if(count) {
215                 if (!vmwdt_nowayout) {
216                         size_t i;
217
218                         /* note: just in case someone wrote the magic character
219                          * five months ago... */
220                         vmwdt_expect_close = 0;
221
222                         for (i = 0; i != count; i++) {
223                                 char c;
224                                 if (get_user(c, buf+i))
225                                         return -EFAULT;
226                                 if (c == 'V')
227                                         vmwdt_expect_close = 42;
228                         }
229                 }
230                 /* someone wrote to us, we should restart timer */
231                 vmwdt_keepalive();
232         }
233         return count;
234 }
235
236 static const struct file_operations vmwdt_fops = {
237         .open    = &vmwdt_open,
238         .release = &vmwdt_close,
239         .ioctl   = &vmwdt_ioctl,
240         .write   = &vmwdt_write,
241         .owner   = THIS_MODULE,
242 };
243
244 static struct miscdevice vmwdt_dev = {
245         .minor      = WATCHDOG_MINOR,
246         .name       = "watchdog",
247         .fops       = &vmwdt_fops,
248 };
249
250 static int __init vmwdt_init(void)
251 {
252         int ret;
253
254         ret = vmwdt_probe();
255         if (ret)
256                 return ret;
257         return misc_register(&vmwdt_dev);
258 }
259 module_init(vmwdt_init);
260
261 static void __exit vmwdt_exit(void)
262 {
263         WARN_ON(misc_deregister(&vmwdt_dev) != 0);
264 }
265 module_exit(vmwdt_exit);