2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 2013 iXsystems.com,
4 * author: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <sys/syslog.h>
44 #include <sys/watchdog.h>
46 #include <machine/bus.h>
48 #include <sys/syscallsubr.h> /* kern_clock_gettime() */
50 static int wd_set_pretimeout(int newtimeout, int disableiftoolong);
51 static void wd_timeout_cb(void *arg);
53 static struct callout wd_pretimeo_handle;
54 static int wd_pretimeout;
55 static int wd_pretimeout_act = WD_SOFT_LOG;
57 static struct callout wd_softtimeo_handle;
58 static int wd_softtimer; /* true = use softtimer instead of hardware
60 static int wd_softtimeout_act = WD_SOFT_LOG; /* action for the software timeout */
62 static struct cdev *wd_dev;
63 static volatile u_int wd_last_u; /* last timeout value set by kern_do_pat */
64 static u_int wd_last_u_sysctl; /* last timeout value set by kern_do_pat */
65 static u_int wd_last_u_sysctl_secs; /* wd_last_u in seconds */
67 SYSCTL_NODE(_hw, OID_AUTO, watchdog, CTLFLAG_RD, 0, "Main watchdog device");
68 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u, CTLFLAG_RD,
69 &wd_last_u_sysctl, 0, "Watchdog last update time");
70 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u_secs, CTLFLAG_RD,
71 &wd_last_u_sysctl_secs, 0, "Watchdog last update time");
73 static int wd_lastpat_valid = 0;
74 static time_t wd_lastpat = 0; /* when the watchdog was last patted */
77 pow2ns_to_ts(int pow2ns, struct timespec *ts)
82 ts->tv_sec = ns / 1000000000ULL;
83 ts->tv_nsec = ns % 1000000000ULL;
87 pow2ns_to_ticks(int pow2ns)
92 pow2ns_to_ts(pow2ns, &ts);
93 TIMESPEC_TO_TIMEVAL(&tv, &ts);
98 seconds_to_pow2ns(int seconds)
104 ns = ((uint64_t)seconds) * 1000000000ULL;
106 shifted = 1ULL << power;
115 wdog_kern_pat(u_int utim)
119 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
122 if ((utim & WD_LASTVAL) != 0) {
124 * if WD_LASTVAL is set, fill in the bits for timeout
125 * from the saved value in wd_last_u.
127 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
132 * Otherwise save the new interval.
133 * This can be zero (to disable the watchdog)
135 wd_last_u = (utim & WD_INTERVAL);
136 wd_last_u_sysctl = wd_last_u;
137 wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
139 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
142 /* Assume all is well; watchdog signals failure. */
145 /* Assume no watchdog available; watchdog flags success */
150 callout_stop(&wd_softtimeo_handle);
152 (void) callout_reset(&wd_softtimeo_handle,
153 pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
157 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
159 wd_set_pretimeout(wd_pretimeout, true);
161 * If we were able to arm/strobe the watchdog, then
162 * update the last time it was strobed for WDIOC_GETTIMELEFT
167 error = kern_clock_gettime(curthread /* XXX */,
168 CLOCK_MONOTONIC_FAST, &ts);
170 wd_lastpat = ts.tv_sec;
171 wd_lastpat_valid = 1;
178 wd_valid_act(int act)
181 if ((act & ~(WD_SOFT_MASK)) != 0)
187 wd_ioctl_patpat(caddr_t data)
192 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
194 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
196 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
197 (u & WD_LASTVAL) != 0))
200 return (ENOSYS); /* XXX Not implemented yet */
201 u &= ~(WD_ACTIVE | WD_PASSIVE);
203 return (wdog_kern_pat(u));
207 wd_get_time_left(struct thread *td, time_t *remainp)
212 error = kern_clock_gettime(td, CLOCK_MONOTONIC_FAST, &ts);
215 if (!wd_lastpat_valid)
217 *remainp = ts.tv_sec - wd_lastpat;
222 wd_timeout_cb(void *arg)
224 const char *type = arg;
227 if ((wd_pretimeout_act & WD_SOFT_DDB)) {
229 snprintf(kdb_why, sizeof(buf), "watchdog %s timeout", type);
231 kdb_enter(KDB_WHY_WATCHDOG, kdb_why);
234 if ((wd_pretimeout_act & WD_SOFT_LOG))
235 log(LOG_EMERG, "watchdog %s-timeout, WD_SOFT_LOG", type);
236 if ((wd_pretimeout_act & WD_SOFT_PRINTF))
237 printf("watchdog %s-timeout, WD_SOFT_PRINTF\n", type);
238 if ((wd_pretimeout_act & WD_SOFT_PANIC))
239 panic("watchdog %s-timeout, WD_SOFT_PANIC set", type);
243 * Called to manage timeouts.
244 * newtimeout needs to be in the range of 0 to actual watchdog timeout.
245 * if 0, we disable the pre-timeout.
246 * otherwise we set the pre-timeout provided it's not greater than the
247 * current actual watchdog timeout.
250 wd_set_pretimeout(int newtimeout, int disableiftoolong)
253 struct timespec utime_ts;
256 utime = wdog_kern_last_timeout();
257 pow2ns_to_ts(utime, &utime_ts);
258 /* do not permit a pre-timeout >= than the timeout. */
259 if (newtimeout >= utime_ts.tv_sec) {
261 * If 'disableiftoolong' then just fall through
262 * so as to disable the pre-watchdog
264 if (disableiftoolong)
270 /* disable the pre-timeout */
271 if (newtimeout == 0) {
273 callout_stop(&wd_pretimeo_handle);
277 timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
279 printf("wd_set_pretimeout: "
281 "utime: %d -> utime_ticks: %d, "
282 "hz*newtimeout: %d, "
283 "timeout_ticks: %d -> sec: %d\n",
285 utime, pow2ns_to_ticks(utime),
287 timeout_ticks, timeout_ticks / hz);
290 /* We determined the value is sane, so reset the callout */
291 (void) callout_reset(&wd_pretimeo_handle,
293 wd_timeout_cb, "pre-timeout");
294 wd_pretimeout = newtimeout;
299 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
300 int flags __unused, struct thread *td)
312 if (u == wd_softtimer)
314 /* If there is a pending timeout disallow this ioctl */
315 if (wd_last_u != 0) {
321 case WDIOC_SETSOFTTIMEOUTACT:
323 if (wd_valid_act(u)) {
324 wd_softtimeout_act = u;
329 case WDIOC_SETPRETIMEOUTACT:
331 if (wd_valid_act(u)) {
332 wd_pretimeout_act = u;
337 case WDIOC_GETPRETIMEOUT:
338 *(int *)data = (int)wd_pretimeout;
340 case WDIOC_SETPRETIMEOUT:
341 error = wd_set_pretimeout(*(int *)data, false);
343 case WDIOC_GETTIMELEFT:
344 error = wd_get_time_left(td, &timeleft);
347 *(int *)data = (int)timeleft;
349 case WDIOC_SETTIMEOUT:
351 error = wdog_kern_pat(seconds_to_pow2ns(u));
353 case WDIOC_GETTIMEOUT:
354 u = wdog_kern_last_timeout();
358 error = wd_ioctl_patpat(data);
368 * Return the last timeout set, this is NOT the seconds from NOW until timeout,
369 * rather it is the amount of seconds passed to WDIOCPATPAT/WDIOC_SETTIMEOUT.
372 wdog_kern_last_timeout(void)
378 static struct cdevsw wd_cdevsw = {
379 .d_version = D_VERSION,
381 .d_name = "watchdog",
385 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
389 callout_init(&wd_pretimeo_handle, true);
390 callout_init(&wd_softtimeo_handle, true);
391 wd_dev = make_dev(&wd_cdevsw, 0,
392 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
395 callout_stop(&wd_pretimeo_handle);
396 callout_stop(&wd_softtimeo_handle);
397 callout_drain(&wd_pretimeo_handle);
398 callout_drain(&wd_softtimeo_handle);
408 DEV_MODULE(watchdog, watchdog_modevent, NULL);