Linux Audio

Check our new training course

Loading...
v3.1
  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 DRV_NAME	"wdt"
 13#define DRV_VERSION	"0.01"
 14#define PFX		DRV_NAME ": "
 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#include <xen/xen.h>
 31#include <asm/xen/hypercall.h>
 32#include <xen/interface/sched.h>
 33
 34static struct platform_device *platform_device;
 35static DEFINE_SPINLOCK(wdt_lock);
 36static struct sched_watchdog wdt;
 37static __kernel_time_t wdt_expires;
 38static bool is_active, expect_release;
 39
 40#define WATCHDOG_TIMEOUT 60 /* in seconds */
 41static unsigned int timeout = WATCHDOG_TIMEOUT;
 42module_param(timeout, uint, S_IRUGO);
 43MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
 44	"(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
 45
 46static bool nowayout = WATCHDOG_NOWAYOUT;
 47module_param(nowayout, bool, S_IRUGO);
 48MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 49	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 50
 51static inline __kernel_time_t set_timeout(void)
 52{
 53	wdt.timeout = timeout;
 54	return ktime_to_timespec(ktime_get()).tv_sec + timeout;
 55}
 56
 57static int xen_wdt_start(void)
 58{
 59	__kernel_time_t expires;
 60	int err;
 61
 62	spin_lock(&wdt_lock);
 63
 64	expires = set_timeout();
 65	if (!wdt.id)
 66		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 67	else
 68		err = -EBUSY;
 69	if (err > 0) {
 70		wdt.id = err;
 71		wdt_expires = expires;
 72		err = 0;
 73	} else
 74		BUG_ON(!err);
 75
 76	spin_unlock(&wdt_lock);
 77
 78	return err;
 79}
 80
 81static int xen_wdt_stop(void)
 82{
 83	int err = 0;
 84
 85	spin_lock(&wdt_lock);
 86
 87	wdt.timeout = 0;
 88	if (wdt.id)
 89		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 90	if (!err)
 91		wdt.id = 0;
 92
 93	spin_unlock(&wdt_lock);
 94
 95	return err;
 96}
 97
 98static int xen_wdt_kick(void)
 99{
100	__kernel_time_t expires;
101	int err;
102
103	spin_lock(&wdt_lock);
104
105	expires = set_timeout();
106	if (wdt.id)
107		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
108	else
109		err = -ENXIO;
110	if (!err)
111		wdt_expires = expires;
112
113	spin_unlock(&wdt_lock);
114
115	return err;
116}
117
118static int xen_wdt_open(struct inode *inode, struct file *file)
119{
120	int err;
121
122	/* /dev/watchdog can only be opened once */
123	if (xchg(&is_active, true))
124		return -EBUSY;
125
126	err = xen_wdt_start();
127	if (err == -EBUSY)
128		err = xen_wdt_kick();
129	return err ?: nonseekable_open(inode, file);
130}
131
132static int xen_wdt_release(struct inode *inode, struct file *file)
133{
134	if (expect_release)
135		xen_wdt_stop();
136	else {
137		printk(KERN_CRIT PFX
138		       "unexpected close, not stopping watchdog!\n");
139		xen_wdt_kick();
140	}
141	is_active = false;
142	expect_release = false;
143	return 0;
144}
145
146static 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
174static 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
230static 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
239static struct miscdevice xen_wdt_miscdev = {
240	.minor =	WATCHDOG_MINOR,
241	.name =		"watchdog",
242	.fops =		&xen_wdt_fops,
243};
244
245static 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			printk(KERN_INFO PFX
255			       "timeout value invalid, using %d\n", timeout);
256		}
257
258		ret = misc_register(&xen_wdt_miscdev);
259		if (ret) {
260			printk(KERN_ERR PFX
261			       "cannot register miscdev on minor=%d (%d)\n",
262			       WATCHDOG_MINOR, ret);
263			break;
264		}
265
266		printk(KERN_INFO PFX
267		       "initialized (timeout=%ds, nowayout=%d)\n",
268		       timeout, nowayout);
269		break;
270
271	case -ENOSYS:
272		printk(KERN_INFO PFX "not supported\n");
273		ret = -ENODEV;
274		break;
275
276	default:
277		printk(KERN_INFO PFX "bogus return value %d\n", ret);
278		break;
279	}
280
281	return ret;
282}
 
 
283
284static int __devexit xen_wdt_remove(struct platform_device *dev)
285{
286	/* Stop the timer before we leave */
287	if (!nowayout)
288		xen_wdt_stop();
 
 
 
 
 
 
 
 
289
290	misc_deregister(&xen_wdt_miscdev);
 
291
292	return 0;
293}
294
295static void xen_wdt_shutdown(struct platform_device *dev)
296{
297	xen_wdt_stop();
298}
299
300static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
301{
302	return xen_wdt_stop();
 
 
 
 
303}
304
305static int xen_wdt_resume(struct platform_device *dev)
306{
307	return xen_wdt_start();
 
 
 
308}
309
310static struct platform_driver xen_wdt_driver = {
311	.probe          = xen_wdt_probe,
312	.remove         = __devexit_p(xen_wdt_remove),
313	.shutdown       = xen_wdt_shutdown,
314	.suspend        = xen_wdt_suspend,
315	.resume         = xen_wdt_resume,
316	.driver         = {
317		.owner  = THIS_MODULE,
318		.name   = DRV_NAME,
319	},
320};
321
322static int __init xen_wdt_init_module(void)
323{
324	int err;
325
326	if (!xen_domain())
327		return -ENODEV;
328
329	printk(KERN_INFO PFX "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
345static void __exit xen_wdt_cleanup_module(void)
346{
347	platform_device_unregister(platform_device);
348	platform_driver_unregister(&xen_wdt_driver);
349	printk(KERN_INFO PFX "module unloaded\n");
350}
351
352module_init(xen_wdt_init_module);
353module_exit(xen_wdt_cleanup_module);
354
355MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
356MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
357MODULE_VERSION(DRV_VERSION);
358MODULE_LICENSE("GPL");
359MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
v4.17
  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 DRV_NAME	"xen_wdt"
 
 
 13
 14#include <linux/bug.h>
 15#include <linux/errno.h>
 16#include <linux/fs.h>
 17#include <linux/hrtimer.h>
 18#include <linux/kernel.h>
 19#include <linux/ktime.h>
 20#include <linux/init.h>
 
 21#include <linux/module.h>
 22#include <linux/moduleparam.h>
 23#include <linux/platform_device.h>
 
 
 24#include <linux/watchdog.h>
 25#include <xen/xen.h>
 26#include <asm/xen/hypercall.h>
 27#include <xen/interface/sched.h>
 28
 29static struct platform_device *platform_device;
 
 30static struct sched_watchdog wdt;
 31static time64_t wdt_expires;
 
 32
 33#define WATCHDOG_TIMEOUT 60 /* in seconds */
 34static unsigned int timeout;
 35module_param(timeout, uint, S_IRUGO);
 36MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
 37	"(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
 38
 39static bool nowayout = WATCHDOG_NOWAYOUT;
 40module_param(nowayout, bool, S_IRUGO);
 41MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 42	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 43
 44static inline time64_t set_timeout(struct watchdog_device *wdd)
 45{
 46	wdt.timeout = wdd->timeout;
 47	return ktime_get_seconds() + wdd->timeout;
 48}
 49
 50static int xen_wdt_start(struct watchdog_device *wdd)
 51{
 52	time64_t expires;
 53	int err;
 54
 55	expires = set_timeout(wdd);
 
 
 56	if (!wdt.id)
 57		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 58	else
 59		err = -EBUSY;
 60	if (err > 0) {
 61		wdt.id = err;
 62		wdt_expires = expires;
 63		err = 0;
 64	} else
 65		BUG_ON(!err);
 66
 
 
 67	return err;
 68}
 69
 70static int xen_wdt_stop(struct watchdog_device *wdd)
 71{
 72	int err = 0;
 73
 
 
 74	wdt.timeout = 0;
 75	if (wdt.id)
 76		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 77	if (!err)
 78		wdt.id = 0;
 79
 
 
 80	return err;
 81}
 82
 83static int xen_wdt_kick(struct watchdog_device *wdd)
 84{
 85	time64_t expires;
 86	int err;
 87
 88	expires = set_timeout(wdd);
 
 
 89	if (wdt.id)
 90		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 91	else
 92		err = -ENXIO;
 93	if (!err)
 94		wdt_expires = expires;
 95
 
 
 96	return err;
 97}
 98
 99static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100{
101	return wdt_expires - ktime_get_seconds();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102}
103
104static struct watchdog_info xen_wdt_info = {
105	.identity = DRV_NAME,
106	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
107};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
109static const struct watchdog_ops xen_wdt_ops = {
110	.owner = THIS_MODULE,
111	.start = xen_wdt_start,
112	.stop = xen_wdt_stop,
113	.ping = xen_wdt_kick,
114	.get_timeleft = xen_wdt_get_timeleft,
 
115};
116
117static struct watchdog_device xen_wdt_dev = {
118	.info = &xen_wdt_info,
119	.ops = &xen_wdt_ops,
120	.timeout = WATCHDOG_TIMEOUT,
121};
122
123static int xen_wdt_probe(struct platform_device *pdev)
124{
125	struct sched_watchdog wd = { .id = ~0 };
126	int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
127
128	if (ret == -ENOSYS) {
129		dev_err(&pdev->dev, "watchdog not supported by hypervisor\n");
130		return -ENODEV;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131	}
132
133	if (ret != -EINVAL) {
134		dev_err(&pdev->dev, "unexpected hypervisor error (%d)\n", ret);
135		return -ENODEV;
136	}
137
138	if (watchdog_init_timeout(&xen_wdt_dev, timeout, NULL))
139		dev_info(&pdev->dev, "timeout value invalid, using %d\n",
140			xen_wdt_dev.timeout);
141	watchdog_set_nowayout(&xen_wdt_dev, nowayout);
142	watchdog_stop_on_reboot(&xen_wdt_dev);
143	watchdog_stop_on_unregister(&xen_wdt_dev);
144
145	ret = devm_watchdog_register_device(&pdev->dev, &xen_wdt_dev);
146	if (ret) {
147		dev_err(&pdev->dev, "cannot register watchdog device (%d)\n",
148			ret);
149		return ret;
150	}
151
152	dev_info(&pdev->dev, "initialized (timeout=%ds, nowayout=%d)\n",
153		xen_wdt_dev.timeout, nowayout);
154
155	return 0;
156}
157
 
 
 
 
 
158static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
159{
160	typeof(wdt.id) id = wdt.id;
161	int rc = xen_wdt_stop(&xen_wdt_dev);
162
163	wdt.id = id;
164	return rc;
165}
166
167static int xen_wdt_resume(struct platform_device *dev)
168{
169	if (!wdt.id)
170		return 0;
171	wdt.id = 0;
172	return xen_wdt_start(&xen_wdt_dev);
173}
174
175static struct platform_driver xen_wdt_driver = {
176	.probe          = xen_wdt_probe,
 
 
177	.suspend        = xen_wdt_suspend,
178	.resume         = xen_wdt_resume,
179	.driver         = {
 
180		.name   = DRV_NAME,
181	},
182};
183
184static int __init xen_wdt_init_module(void)
185{
186	int err;
187
188	if (!xen_domain())
189		return -ENODEV;
190
 
 
191	err = platform_driver_register(&xen_wdt_driver);
192	if (err)
193		return err;
194
195	platform_device = platform_device_register_simple(DRV_NAME,
196								  -1, NULL, 0);
197	if (IS_ERR(platform_device)) {
198		err = PTR_ERR(platform_device);
199		platform_driver_unregister(&xen_wdt_driver);
200	}
201
202	return err;
203}
204
205static void __exit xen_wdt_cleanup_module(void)
206{
207	platform_device_unregister(platform_device);
208	platform_driver_unregister(&xen_wdt_driver);
 
209}
210
211module_init(xen_wdt_init_module);
212module_exit(xen_wdt_cleanup_module);
213
214MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
215MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
 
216MODULE_LICENSE("GPL");