0001
0002
0003
0004
0005
0006
0007
0008 #define DRV_NAME "xen_wdt"
0009
0010 #include <linux/bug.h>
0011 #include <linux/errno.h>
0012 #include <linux/fs.h>
0013 #include <linux/hrtimer.h>
0014 #include <linux/kernel.h>
0015 #include <linux/ktime.h>
0016 #include <linux/init.h>
0017 #include <linux/module.h>
0018 #include <linux/moduleparam.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/watchdog.h>
0021 #include <xen/xen.h>
0022 #include <asm/xen/hypercall.h>
0023 #include <xen/interface/sched.h>
0024
0025 static struct platform_device *platform_device;
0026 static struct sched_watchdog wdt;
0027 static time64_t wdt_expires;
0028
0029 #define WATCHDOG_TIMEOUT 60
0030 static unsigned int timeout;
0031 module_param(timeout, uint, S_IRUGO);
0032 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
0033 "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
0034
0035 static bool nowayout = WATCHDOG_NOWAYOUT;
0036 module_param(nowayout, bool, S_IRUGO);
0037 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
0038 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0039
0040 static inline time64_t set_timeout(struct watchdog_device *wdd)
0041 {
0042 wdt.timeout = wdd->timeout;
0043 return ktime_get_seconds() + wdd->timeout;
0044 }
0045
0046 static int xen_wdt_start(struct watchdog_device *wdd)
0047 {
0048 time64_t expires;
0049 int err;
0050
0051 expires = set_timeout(wdd);
0052 if (!wdt.id)
0053 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
0054 else
0055 err = -EBUSY;
0056 if (err > 0) {
0057 wdt.id = err;
0058 wdt_expires = expires;
0059 err = 0;
0060 } else
0061 BUG_ON(!err);
0062
0063 return err;
0064 }
0065
0066 static int xen_wdt_stop(struct watchdog_device *wdd)
0067 {
0068 int err = 0;
0069
0070 wdt.timeout = 0;
0071 if (wdt.id)
0072 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
0073 if (!err)
0074 wdt.id = 0;
0075
0076 return err;
0077 }
0078
0079 static int xen_wdt_kick(struct watchdog_device *wdd)
0080 {
0081 time64_t expires;
0082 int err;
0083
0084 expires = set_timeout(wdd);
0085 if (wdt.id)
0086 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
0087 else
0088 err = -ENXIO;
0089 if (!err)
0090 wdt_expires = expires;
0091
0092 return err;
0093 }
0094
0095 static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
0096 {
0097 return wdt_expires - ktime_get_seconds();
0098 }
0099
0100 static struct watchdog_info xen_wdt_info = {
0101 .identity = DRV_NAME,
0102 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
0103 };
0104
0105 static const struct watchdog_ops xen_wdt_ops = {
0106 .owner = THIS_MODULE,
0107 .start = xen_wdt_start,
0108 .stop = xen_wdt_stop,
0109 .ping = xen_wdt_kick,
0110 .get_timeleft = xen_wdt_get_timeleft,
0111 };
0112
0113 static struct watchdog_device xen_wdt_dev = {
0114 .info = &xen_wdt_info,
0115 .ops = &xen_wdt_ops,
0116 .timeout = WATCHDOG_TIMEOUT,
0117 };
0118
0119 static int xen_wdt_probe(struct platform_device *pdev)
0120 {
0121 struct device *dev = &pdev->dev;
0122 struct sched_watchdog wd = { .id = ~0 };
0123 int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
0124
0125 if (ret == -ENOSYS) {
0126 dev_err(dev, "watchdog not supported by hypervisor\n");
0127 return -ENODEV;
0128 }
0129
0130 if (ret != -EINVAL) {
0131 dev_err(dev, "unexpected hypervisor error (%d)\n", ret);
0132 return -ENODEV;
0133 }
0134
0135 watchdog_init_timeout(&xen_wdt_dev, timeout, NULL);
0136 watchdog_set_nowayout(&xen_wdt_dev, nowayout);
0137 watchdog_stop_on_reboot(&xen_wdt_dev);
0138 watchdog_stop_on_unregister(&xen_wdt_dev);
0139
0140 ret = devm_watchdog_register_device(dev, &xen_wdt_dev);
0141 if (ret)
0142 return ret;
0143
0144 dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n",
0145 xen_wdt_dev.timeout, nowayout);
0146
0147 return 0;
0148 }
0149
0150 static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
0151 {
0152 typeof(wdt.id) id = wdt.id;
0153 int rc = xen_wdt_stop(&xen_wdt_dev);
0154
0155 wdt.id = id;
0156 return rc;
0157 }
0158
0159 static int xen_wdt_resume(struct platform_device *dev)
0160 {
0161 if (!wdt.id)
0162 return 0;
0163 wdt.id = 0;
0164 return xen_wdt_start(&xen_wdt_dev);
0165 }
0166
0167 static struct platform_driver xen_wdt_driver = {
0168 .probe = xen_wdt_probe,
0169 .suspend = xen_wdt_suspend,
0170 .resume = xen_wdt_resume,
0171 .driver = {
0172 .name = DRV_NAME,
0173 },
0174 };
0175
0176 static int __init xen_wdt_init_module(void)
0177 {
0178 int err;
0179
0180 if (!xen_domain())
0181 return -ENODEV;
0182
0183 err = platform_driver_register(&xen_wdt_driver);
0184 if (err)
0185 return err;
0186
0187 platform_device = platform_device_register_simple(DRV_NAME,
0188 -1, NULL, 0);
0189 if (IS_ERR(platform_device)) {
0190 err = PTR_ERR(platform_device);
0191 platform_driver_unregister(&xen_wdt_driver);
0192 }
0193
0194 return err;
0195 }
0196
0197 static void __exit xen_wdt_cleanup_module(void)
0198 {
0199 platform_device_unregister(platform_device);
0200 platform_driver_unregister(&xen_wdt_driver);
0201 }
0202
0203 module_init(xen_wdt_init_module);
0204 module_exit(xen_wdt_cleanup_module);
0205
0206 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
0207 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
0208 MODULE_LICENSE("GPL");