- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / watchdog / xen_wdt.c
1 /*
2  *      Xen Watchdog Driver
3  *
4  *      (c) Copyright 2010,2011 Novell, Inc.
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11
12 #define DRV_NAME        "wdt"
13 #define DRV_VERSION     "0.01"
14 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
15
16 #include <linux/bug.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/hrtimer.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/init.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <linux/uaccess.h>
29 #include <linux/watchdog.h>
30 #ifdef CONFIG_PARAVIRT_XEN
31 #include <xen/xen.h>
32 #include <asm/xen/hypercall.h>
33 #endif
34 #include <xen/interface/sched.h>
35
36 static struct platform_device *platform_device;
37 static DEFINE_SPINLOCK(wdt_lock);
38 static struct sched_watchdog wdt;
39 static __kernel_time_t wdt_expires;
40 static bool is_active, expect_release;
41
42 #define WATCHDOG_TIMEOUT 60 /* in seconds */
43 static unsigned int timeout = WATCHDOG_TIMEOUT;
44 module_param(timeout, uint, S_IRUGO);
45 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
46         "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
47
48 static bool nowayout = WATCHDOG_NOWAYOUT;
49 module_param(nowayout, bool, S_IRUGO);
50 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
51         "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
52
53 static inline __kernel_time_t set_timeout(void)
54 {
55         wdt.timeout = timeout;
56         return ktime_to_timespec(ktime_get()).tv_sec + timeout;
57 }
58
59 static int xen_wdt_start(void)
60 {
61         __kernel_time_t expires;
62         int err;
63
64         spin_lock(&wdt_lock);
65
66         expires = set_timeout();
67         if (!wdt.id)
68                 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
69         else
70                 err = -EBUSY;
71         if (err > 0) {
72                 wdt.id = err;
73                 wdt_expires = expires;
74                 err = 0;
75         } else
76                 BUG_ON(!err);
77
78         spin_unlock(&wdt_lock);
79
80         return err;
81 }
82
83 static int xen_wdt_stop(void)
84 {
85         int err = 0;
86
87         spin_lock(&wdt_lock);
88
89         wdt.timeout = 0;
90         if (wdt.id)
91                 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
92         if (!err)
93                 wdt.id = 0;
94
95         spin_unlock(&wdt_lock);
96
97         return err;
98 }
99
100 static int xen_wdt_kick(void)
101 {
102         __kernel_time_t expires;
103         int err;
104
105         spin_lock(&wdt_lock);
106
107         expires = set_timeout();
108         if (wdt.id)
109                 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
110         else
111                 err = -ENXIO;
112         if (!err)
113                 wdt_expires = expires;
114
115         spin_unlock(&wdt_lock);
116
117         return err;
118 }
119
120 static int xen_wdt_open(struct inode *inode, struct file *file)
121 {
122         int err;
123
124         /* /dev/watchdog can only be opened once */
125         if (xchg(&is_active, true))
126                 return -EBUSY;
127
128         err = xen_wdt_start();
129         if (err == -EBUSY)
130                 err = xen_wdt_kick();
131         return err ?: nonseekable_open(inode, file);
132 }
133
134 static int xen_wdt_release(struct inode *inode, struct file *file)
135 {
136         if (expect_release)
137                 xen_wdt_stop();
138         else {
139                 pr_crit("unexpected close, not stopping watchdog!\n");
140                 xen_wdt_kick();
141         }
142         is_active = false;
143         expect_release = false;
144         return 0;
145 }
146
147 static ssize_t xen_wdt_write(struct file *file, const char __user *data,
148                              size_t len, loff_t *ppos)
149 {
150         /* See if we got the magic character 'V' and reload the timer */
151         if (len) {
152                 if (!nowayout) {
153                         size_t i;
154
155                         /* in case it was set long ago */
156                         expect_release = false;
157
158                         /* scan to see whether or not we got the magic
159                            character */
160                         for (i = 0; i != len; i++) {
161                                 char c;
162                                 if (get_user(c, data + i))
163                                         return -EFAULT;
164                                 if (c == 'V')
165                                         expect_release = true;
166                         }
167                 }
168
169                 /* someone wrote to us, we should reload the timer */
170                 xen_wdt_kick();
171         }
172         return len;
173 }
174
175 static long xen_wdt_ioctl(struct file *file, unsigned int cmd,
176                           unsigned long arg)
177 {
178         int new_options, retval = -EINVAL;
179         int new_timeout;
180         int __user *argp = (void __user *)arg;
181         static const struct watchdog_info ident = {
182                 .options =              WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
183                 .firmware_version =     0,
184                 .identity =             DRV_NAME,
185         };
186
187         switch (cmd) {
188         case WDIOC_GETSUPPORT:
189                 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
190
191         case WDIOC_GETSTATUS:
192         case WDIOC_GETBOOTSTATUS:
193                 return put_user(0, argp);
194
195         case WDIOC_SETOPTIONS:
196                 if (get_user(new_options, argp))
197                         return -EFAULT;
198
199                 if (new_options & WDIOS_DISABLECARD)
200                         retval = xen_wdt_stop();
201                 if (new_options & WDIOS_ENABLECARD) {
202                         retval = xen_wdt_start();
203                         if (retval == -EBUSY)
204                                 retval = xen_wdt_kick();
205                 }
206                 return retval;
207
208         case WDIOC_KEEPALIVE:
209                 xen_wdt_kick();
210                 return 0;
211
212         case WDIOC_SETTIMEOUT:
213                 if (get_user(new_timeout, argp))
214                         return -EFAULT;
215                 if (!new_timeout)
216                         return -EINVAL;
217                 timeout = new_timeout;
218                 xen_wdt_kick();
219                 /* fall through */
220         case WDIOC_GETTIMEOUT:
221                 return put_user(timeout, argp);
222
223         case WDIOC_GETTIMELEFT:
224                 retval = wdt_expires - ktime_to_timespec(ktime_get()).tv_sec;
225                 return put_user(retval, argp);
226         }
227
228         return -ENOTTY;
229 }
230
231 static const struct file_operations xen_wdt_fops = {
232         .owner =                THIS_MODULE,
233         .llseek =               no_llseek,
234         .write =                xen_wdt_write,
235         .unlocked_ioctl =       xen_wdt_ioctl,
236         .open =                 xen_wdt_open,
237         .release =              xen_wdt_release,
238 };
239
240 static struct miscdevice xen_wdt_miscdev = {
241         .minor =        WATCHDOG_MINOR,
242         .name =         "watchdog",
243         .fops =         &xen_wdt_fops,
244 };
245
246 static int __devinit xen_wdt_probe(struct platform_device *dev)
247 {
248         struct sched_watchdog wd = { .id = ~0 };
249         int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
250
251         switch (ret) {
252         case -EINVAL:
253                 if (!timeout) {
254                         timeout = WATCHDOG_TIMEOUT;
255                         pr_info("timeout value invalid, using %d\n", timeout);
256                 }
257
258                 ret = misc_register(&xen_wdt_miscdev);
259                 if (ret) {
260                         pr_err("cannot register miscdev on minor=%d (%d)\n",
261                                WATCHDOG_MINOR, ret);
262                         break;
263                 }
264
265                 pr_info("initialized (timeout=%ds, nowayout=%d)\n",
266                         timeout, nowayout);
267                 break;
268
269         case -ENOSYS:
270                 pr_info("not supported\n");
271                 ret = -ENODEV;
272                 break;
273
274         default:
275                 pr_info("bogus return value %d\n", ret);
276                 break;
277         }
278
279         return ret;
280 }
281
282 static int __devexit xen_wdt_remove(struct platform_device *dev)
283 {
284         /* Stop the timer before we leave */
285         if (!nowayout)
286                 xen_wdt_stop();
287
288         misc_deregister(&xen_wdt_miscdev);
289
290         return 0;
291 }
292
293 static void xen_wdt_shutdown(struct platform_device *dev)
294 {
295         xen_wdt_stop();
296 }
297
298 static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
299 {
300         return xen_wdt_stop();
301 }
302
303 static int xen_wdt_resume(struct platform_device *dev)
304 {
305         return xen_wdt_start();
306 }
307
308 static struct platform_driver xen_wdt_driver = {
309         .probe          = xen_wdt_probe,
310         .remove         = __devexit_p(xen_wdt_remove),
311         .shutdown       = xen_wdt_shutdown,
312         .suspend        = xen_wdt_suspend,
313         .resume         = xen_wdt_resume,
314         .driver         = {
315                 .owner  = THIS_MODULE,
316                 .name   = DRV_NAME,
317         },
318 };
319
320 static int __init xen_wdt_init_module(void)
321 {
322         int err;
323
324 #ifdef CONFIG_PARAVIRT_XEN
325         if (!xen_domain())
326                 return -ENODEV;
327 #endif
328
329         printk(KERN_INFO "Xen WatchDog Timer Driver v%s\n", DRV_VERSION);
330
331         err = platform_driver_register(&xen_wdt_driver);
332         if (err)
333                 return err;
334
335         platform_device = platform_device_register_simple(DRV_NAME,
336                                                           -1, NULL, 0);
337         if (IS_ERR(platform_device)) {
338                 err = PTR_ERR(platform_device);
339                 platform_driver_unregister(&xen_wdt_driver);
340         }
341
342         return err;
343 }
344
345 static void __exit xen_wdt_cleanup_module(void)
346 {
347         platform_device_unregister(platform_device);
348         platform_driver_unregister(&xen_wdt_driver);
349         pr_info("module unloaded\n");
350 }
351
352 module_init(xen_wdt_init_module);
353 module_exit(xen_wdt_cleanup_module);
354
355 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
356 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
357 MODULE_VERSION(DRV_VERSION);
358 MODULE_LICENSE("GPL");
359 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);