Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	Xen Watchdog Driver
  4 *
  5 *	(c) Copyright 2010 Novell, Inc.
 
 
 
 
 
  6 */
  7
  8#define DRV_NAME	"xen_wdt"
  9
 10#include <linux/bug.h>
 11#include <linux/errno.h>
 12#include <linux/fs.h>
 13#include <linux/hrtimer.h>
 14#include <linux/kernel.h>
 15#include <linux/ktime.h>
 16#include <linux/init.h>
 17#include <linux/module.h>
 18#include <linux/moduleparam.h>
 19#include <linux/platform_device.h>
 20#include <linux/watchdog.h>
 21#include <xen/xen.h>
 22#include <asm/xen/hypercall.h>
 23#include <xen/interface/sched.h>
 24
 25static struct platform_device *platform_device;
 26static struct sched_watchdog wdt;
 27static time64_t wdt_expires;
 28
 29#define WATCHDOG_TIMEOUT 60 /* in seconds */
 30static unsigned int timeout;
 31module_param(timeout, uint, S_IRUGO);
 32MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
 33	"(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
 34
 35static bool nowayout = WATCHDOG_NOWAYOUT;
 36module_param(nowayout, bool, S_IRUGO);
 37MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
 38	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 39
 40static inline time64_t set_timeout(struct watchdog_device *wdd)
 41{
 42	wdt.timeout = wdd->timeout;
 43	return ktime_get_seconds() + wdd->timeout;
 44}
 45
 46static int xen_wdt_start(struct watchdog_device *wdd)
 47{
 48	time64_t expires;
 49	int err;
 50
 51	expires = set_timeout(wdd);
 52	if (!wdt.id)
 53		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 54	else
 55		err = -EBUSY;
 56	if (err > 0) {
 57		wdt.id = err;
 58		wdt_expires = expires;
 59		err = 0;
 60	} else
 61		BUG_ON(!err);
 62
 63	return err;
 64}
 65
 66static int xen_wdt_stop(struct watchdog_device *wdd)
 67{
 68	int err = 0;
 69
 70	wdt.timeout = 0;
 71	if (wdt.id)
 72		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 73	if (!err)
 74		wdt.id = 0;
 75
 76	return err;
 77}
 78
 79static int xen_wdt_kick(struct watchdog_device *wdd)
 80{
 81	time64_t expires;
 82	int err;
 83
 84	expires = set_timeout(wdd);
 85	if (wdt.id)
 86		err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
 87	else
 88		err = -ENXIO;
 89	if (!err)
 90		wdt_expires = expires;
 91
 92	return err;
 93}
 94
 95static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
 96{
 97	return wdt_expires - ktime_get_seconds();
 98}
 99
100static struct watchdog_info xen_wdt_info = {
101	.identity = DRV_NAME,
102	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
103};
104
105static const struct watchdog_ops xen_wdt_ops = {
106	.owner = THIS_MODULE,
107	.start = xen_wdt_start,
108	.stop = xen_wdt_stop,
109	.ping = xen_wdt_kick,
110	.get_timeleft = xen_wdt_get_timeleft,
111};
112
113static struct watchdog_device xen_wdt_dev = {
114	.info = &xen_wdt_info,
115	.ops = &xen_wdt_ops,
116	.timeout = WATCHDOG_TIMEOUT,
117};
118
119static int xen_wdt_probe(struct platform_device *pdev)
120{
121	struct device *dev = &pdev->dev;
122	struct sched_watchdog wd = { .id = ~0 };
123	int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
124
125	if (ret == -ENOSYS) {
126		dev_err(dev, "watchdog not supported by hypervisor\n");
127		return -ENODEV;
128	}
129
130	if (ret != -EINVAL) {
131		dev_err(dev, "unexpected hypervisor error (%d)\n", ret);
132		return -ENODEV;
133	}
134
135	watchdog_init_timeout(&xen_wdt_dev, timeout, NULL);
 
 
136	watchdog_set_nowayout(&xen_wdt_dev, nowayout);
137	watchdog_stop_on_reboot(&xen_wdt_dev);
138	watchdog_stop_on_unregister(&xen_wdt_dev);
139
140	ret = devm_watchdog_register_device(dev, &xen_wdt_dev);
141	if (ret)
 
 
142		return ret;
 
143
144	dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n",
145		 xen_wdt_dev.timeout, nowayout);
146
147	return 0;
148}
149
150static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
151{
152	typeof(wdt.id) id = wdt.id;
153	int rc = xen_wdt_stop(&xen_wdt_dev);
154
155	wdt.id = id;
156	return rc;
157}
158
159static int xen_wdt_resume(struct platform_device *dev)
160{
161	if (!wdt.id)
162		return 0;
163	wdt.id = 0;
164	return xen_wdt_start(&xen_wdt_dev);
165}
166
167static struct platform_driver xen_wdt_driver = {
168	.probe          = xen_wdt_probe,
169	.suspend        = xen_wdt_suspend,
170	.resume         = xen_wdt_resume,
171	.driver         = {
172		.name   = DRV_NAME,
173	},
174};
175
176static int __init xen_wdt_init_module(void)
177{
178	int err;
179
180	if (!xen_domain())
181		return -ENODEV;
182
183	err = platform_driver_register(&xen_wdt_driver);
184	if (err)
185		return err;
186
187	platform_device = platform_device_register_simple(DRV_NAME,
188								  -1, NULL, 0);
189	if (IS_ERR(platform_device)) {
190		err = PTR_ERR(platform_device);
191		platform_driver_unregister(&xen_wdt_driver);
192	}
193
194	return err;
195}
196
197static void __exit xen_wdt_cleanup_module(void)
198{
199	platform_device_unregister(platform_device);
200	platform_driver_unregister(&xen_wdt_driver);
201}
202
203module_init(xen_wdt_init_module);
204module_exit(xen_wdt_cleanup_module);
205
206MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
207MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
208MODULE_LICENSE("GPL");
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");