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