Commit | Line | Data |
---|---|---|
066d6c7f JB |
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 | ||
18cffd68 | 12 | #define DRV_NAME "xen_wdt" |
066d6c7f JB |
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> | |
066d6c7f JB |
21 | #include <linux/module.h> |
22 | #include <linux/moduleparam.h> | |
23 | #include <linux/platform_device.h> | |
066d6c7f JB |
24 | #include <linux/watchdog.h> |
25 | #include <xen/xen.h> | |
26 | #include <asm/xen/hypercall.h> | |
27 | #include <xen/interface/sched.h> | |
28 | ||
29 | static struct platform_device *platform_device; | |
066d6c7f | 30 | static struct sched_watchdog wdt; |
b6c84c9f | 31 | static time64_t wdt_expires; |
066d6c7f JB |
32 | |
33 | #define WATCHDOG_TIMEOUT 60 /* in seconds */ | |
18cffd68 | 34 | static unsigned int timeout; |
066d6c7f JB |
35 | module_param(timeout, uint, S_IRUGO); |
36 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds " | |
37 | "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); | |
38 | ||
39 | static bool nowayout = WATCHDOG_NOWAYOUT; | |
40 | module_param(nowayout, bool, S_IRUGO); | |
41 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " | |
42 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
43 | ||
18cffd68 | 44 | static inline time64_t set_timeout(struct watchdog_device *wdd) |
066d6c7f | 45 | { |
18cffd68 RR |
46 | wdt.timeout = wdd->timeout; |
47 | return ktime_get_seconds() + wdd->timeout; | |
066d6c7f JB |
48 | } |
49 | ||
18cffd68 | 50 | static int xen_wdt_start(struct watchdog_device *wdd) |
066d6c7f | 51 | { |
b6c84c9f | 52 | time64_t expires; |
066d6c7f JB |
53 | int err; |
54 | ||
18cffd68 | 55 | expires = set_timeout(wdd); |
066d6c7f JB |
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 | ||
066d6c7f JB |
67 | return err; |
68 | } | |
69 | ||
18cffd68 | 70 | static int xen_wdt_stop(struct watchdog_device *wdd) |
066d6c7f JB |
71 | { |
72 | int err = 0; | |
73 | ||
066d6c7f JB |
74 | wdt.timeout = 0; |
75 | if (wdt.id) | |
76 | err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt); | |
77 | if (!err) | |
78 | wdt.id = 0; | |
79 | ||
066d6c7f JB |
80 | return err; |
81 | } | |
82 | ||
18cffd68 | 83 | static int xen_wdt_kick(struct watchdog_device *wdd) |
066d6c7f | 84 | { |
b6c84c9f | 85 | time64_t expires; |
066d6c7f JB |
86 | int err; |
87 | ||
18cffd68 | 88 | expires = set_timeout(wdd); |
066d6c7f JB |
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 | ||
066d6c7f JB |
96 | return err; |
97 | } | |
98 | ||
18cffd68 | 99 | static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd) |
066d6c7f | 100 | { |
18cffd68 | 101 | return wdt_expires - ktime_get_seconds(); |
066d6c7f JB |
102 | } |
103 | ||
18cffd68 RR |
104 | static struct watchdog_info xen_wdt_info = { |
105 | .identity = DRV_NAME, | |
106 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, | |
107 | }; | |
066d6c7f | 108 | |
18cffd68 RR |
109 | static 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, | |
066d6c7f JB |
115 | }; |
116 | ||
18cffd68 RR |
117 | static struct watchdog_device xen_wdt_dev = { |
118 | .info = &xen_wdt_info, | |
119 | .ops = &xen_wdt_ops, | |
120 | .timeout = WATCHDOG_TIMEOUT, | |
066d6c7f JB |
121 | }; |
122 | ||
18cffd68 | 123 | static int xen_wdt_probe(struct platform_device *pdev) |
066d6c7f | 124 | { |
b90abaac | 125 | struct device *dev = &pdev->dev; |
066d6c7f JB |
126 | struct sched_watchdog wd = { .id = ~0 }; |
127 | int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd); | |
128 | ||
18cffd68 | 129 | if (ret == -ENOSYS) { |
b90abaac | 130 | dev_err(dev, "watchdog not supported by hypervisor\n"); |
18cffd68 | 131 | return -ENODEV; |
066d6c7f JB |
132 | } |
133 | ||
18cffd68 | 134 | if (ret != -EINVAL) { |
b90abaac | 135 | dev_err(dev, "unexpected hypervisor error (%d)\n", ret); |
18cffd68 RR |
136 | return -ENODEV; |
137 | } | |
066d6c7f | 138 | |
b74d6461 | 139 | watchdog_init_timeout(&xen_wdt_dev, timeout, NULL); |
18cffd68 RR |
140 | watchdog_set_nowayout(&xen_wdt_dev, nowayout); |
141 | watchdog_stop_on_reboot(&xen_wdt_dev); | |
142 | watchdog_stop_on_unregister(&xen_wdt_dev); | |
143 | ||
b90abaac | 144 | ret = devm_watchdog_register_device(dev, &xen_wdt_dev); |
18cffd68 | 145 | if (ret) { |
b90abaac | 146 | dev_err(dev, "cannot register watchdog device (%d)\n", ret); |
18cffd68 RR |
147 | return ret; |
148 | } | |
066d6c7f | 149 | |
b90abaac GR |
150 | dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n", |
151 | xen_wdt_dev.timeout, nowayout); | |
066d6c7f JB |
152 | |
153 | return 0; | |
154 | } | |
155 | ||
066d6c7f JB |
156 | static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state) |
157 | { | |
83448bf7 | 158 | typeof(wdt.id) id = wdt.id; |
18cffd68 | 159 | int rc = xen_wdt_stop(&xen_wdt_dev); |
83448bf7 JB |
160 | |
161 | wdt.id = id; | |
162 | return rc; | |
066d6c7f JB |
163 | } |
164 | ||
165 | static int xen_wdt_resume(struct platform_device *dev) | |
166 | { | |
83448bf7 JB |
167 | if (!wdt.id) |
168 | return 0; | |
169 | wdt.id = 0; | |
18cffd68 | 170 | return xen_wdt_start(&xen_wdt_dev); |
066d6c7f JB |
171 | } |
172 | ||
173 | static struct platform_driver xen_wdt_driver = { | |
174 | .probe = xen_wdt_probe, | |
066d6c7f JB |
175 | .suspend = xen_wdt_suspend, |
176 | .resume = xen_wdt_resume, | |
177 | .driver = { | |
066d6c7f JB |
178 | .name = DRV_NAME, |
179 | }, | |
180 | }; | |
181 | ||
182 | static int __init xen_wdt_init_module(void) | |
183 | { | |
184 | int err; | |
185 | ||
186 | if (!xen_domain()) | |
187 | return -ENODEV; | |
188 | ||
066d6c7f JB |
189 | err = platform_driver_register(&xen_wdt_driver); |
190 | if (err) | |
191 | return err; | |
192 | ||
193 | platform_device = platform_device_register_simple(DRV_NAME, | |
194 | -1, NULL, 0); | |
195 | if (IS_ERR(platform_device)) { | |
196 | err = PTR_ERR(platform_device); | |
197 | platform_driver_unregister(&xen_wdt_driver); | |
198 | } | |
199 | ||
200 | return err; | |
201 | } | |
202 | ||
203 | static void __exit xen_wdt_cleanup_module(void) | |
204 | { | |
205 | platform_device_unregister(platform_device); | |
206 | platform_driver_unregister(&xen_wdt_driver); | |
066d6c7f JB |
207 | } |
208 | ||
209 | module_init(xen_wdt_init_module); | |
210 | module_exit(xen_wdt_cleanup_module); | |
211 | ||
212 | MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>"); | |
213 | MODULE_DESCRIPTION("Xen WatchDog Timer Driver"); | |
066d6c7f | 214 | MODULE_LICENSE("GPL"); |