2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Poul-Henning Kamp
5 * Copyright (c) 2013 iXsystems.com,
6 * author: Alfred Perlstein <alfred@freebsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/watchdog.h>
51 #include <machine/bus.h>
53 #include <sys/syscallsubr.h> /* kern_clock_gettime() */
55 static int wd_set_pretimeout(int newtimeout, int disableiftoolong);
56 static void wd_timeout_cb(void *arg);
58 static struct callout wd_pretimeo_handle;
59 static int wd_pretimeout;
60 static int wd_pretimeout_act = WD_SOFT_LOG;
62 static struct callout wd_softtimeo_handle;
63 static int wd_softtimer; /* true = use softtimer instead of hardware
65 static int wd_softtimeout_act = WD_SOFT_LOG; /* action for the software timeout */
67 static struct cdev *wd_dev;
68 static volatile u_int wd_last_u; /* last timeout value set by kern_do_pat */
69 static u_int wd_last_u_sysctl; /* last timeout value set by kern_do_pat */
70 static u_int wd_last_u_sysctl_secs; /* wd_last_u in seconds */
72 SYSCTL_NODE(_hw, OID_AUTO, watchdog, CTLFLAG_RD, 0, "Main watchdog device");
73 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u, CTLFLAG_RD,
74 &wd_last_u_sysctl, 0, "Watchdog last update time");
75 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u_secs, CTLFLAG_RD,
76 &wd_last_u_sysctl_secs, 0, "Watchdog last update time");
78 static int wd_lastpat_valid = 0;
79 static time_t wd_lastpat = 0; /* when the watchdog was last patted */
82 pow2ns_to_ts(int pow2ns, struct timespec *ts)
87 ts->tv_sec = ns / 1000000000ULL;
88 ts->tv_nsec = ns % 1000000000ULL;
92 pow2ns_to_ticks(int pow2ns)
97 pow2ns_to_ts(pow2ns, &ts);
98 TIMESPEC_TO_TIMEVAL(&tv, &ts);
103 seconds_to_pow2ns(int seconds)
109 ns = ((uint64_t)seconds) * 1000000000ULL;
111 shifted = 1ULL << power;
120 wdog_kern_pat(u_int utim)
124 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
127 if ((utim & WD_LASTVAL) != 0) {
129 * if WD_LASTVAL is set, fill in the bits for timeout
130 * from the saved value in wd_last_u.
132 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
137 * Otherwise save the new interval.
138 * This can be zero (to disable the watchdog)
140 wd_last_u = (utim & WD_INTERVAL);
141 wd_last_u_sysctl = wd_last_u;
142 wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
144 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
147 /* Assume all is well; watchdog signals failure. */
150 /* Assume no watchdog available; watchdog flags success */
155 callout_stop(&wd_softtimeo_handle);
157 (void) callout_reset(&wd_softtimeo_handle,
158 pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
162 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
164 wd_set_pretimeout(wd_pretimeout, true);
166 * If we were able to arm/strobe the watchdog, then
167 * update the last time it was strobed for WDIOC_GETTIMELEFT
172 error = kern_clock_gettime(curthread /* XXX */,
173 CLOCK_MONOTONIC_FAST, &ts);
175 wd_lastpat = ts.tv_sec;
176 wd_lastpat_valid = 1;
183 wd_valid_act(int act)
186 if ((act & ~(WD_SOFT_MASK)) != 0)
192 wd_ioctl_patpat(caddr_t data)
197 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
199 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
201 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
202 (u & WD_LASTVAL) != 0))
205 return (ENOSYS); /* XXX Not implemented yet */
206 u &= ~(WD_ACTIVE | WD_PASSIVE);
208 return (wdog_kern_pat(u));
212 wd_get_time_left(struct thread *td, time_t *remainp)
217 error = kern_clock_gettime(td, CLOCK_MONOTONIC_FAST, &ts);
220 if (!wd_lastpat_valid)
222 *remainp = ts.tv_sec - wd_lastpat;
227 wd_timeout_cb(void *arg)
229 const char *type = arg;
232 if ((wd_pretimeout_act & WD_SOFT_DDB)) {
234 snprintf(kdb_why, sizeof(kdb_why), "watchdog %s-timeout", type);
236 kdb_enter(KDB_WHY_WATCHDOG, kdb_why);
239 if ((wd_pretimeout_act & WD_SOFT_LOG))
240 log(LOG_EMERG, "watchdog %s-timeout, WD_SOFT_LOG\n", type);
241 if ((wd_pretimeout_act & WD_SOFT_PRINTF))
242 printf("watchdog %s-timeout, WD_SOFT_PRINTF\n", type);
243 if ((wd_pretimeout_act & WD_SOFT_PANIC))
244 panic("watchdog %s-timeout, WD_SOFT_PANIC set", type);
248 * Called to manage timeouts.
249 * newtimeout needs to be in the range of 0 to actual watchdog timeout.
250 * if 0, we disable the pre-timeout.
251 * otherwise we set the pre-timeout provided it's not greater than the
252 * current actual watchdog timeout.
255 wd_set_pretimeout(int newtimeout, int disableiftoolong)
258 struct timespec utime_ts;
261 utime = wdog_kern_last_timeout();
262 pow2ns_to_ts(utime, &utime_ts);
263 /* do not permit a pre-timeout >= than the timeout. */
264 if (newtimeout >= utime_ts.tv_sec) {
266 * If 'disableiftoolong' then just fall through
267 * so as to disable the pre-watchdog
269 if (disableiftoolong)
275 /* disable the pre-timeout */
276 if (newtimeout == 0) {
278 callout_stop(&wd_pretimeo_handle);
282 timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
284 printf("wd_set_pretimeout: "
286 "utime: %d -> utime_ticks: %d, "
287 "hz*newtimeout: %d, "
288 "timeout_ticks: %d -> sec: %d\n",
290 utime, pow2ns_to_ticks(utime),
292 timeout_ticks, timeout_ticks / hz);
295 /* We determined the value is sane, so reset the callout */
296 (void) callout_reset(&wd_pretimeo_handle,
297 timeout_ticks, wd_timeout_cb, "pre");
298 wd_pretimeout = newtimeout;
303 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
304 int flags __unused, struct thread *td)
316 if (u == wd_softtimer)
318 /* If there is a pending timeout disallow this ioctl */
319 if (wd_last_u != 0) {
325 case WDIOC_SETSOFTTIMEOUTACT:
327 if (wd_valid_act(u)) {
328 wd_softtimeout_act = u;
333 case WDIOC_SETPRETIMEOUTACT:
335 if (wd_valid_act(u)) {
336 wd_pretimeout_act = u;
341 case WDIOC_GETPRETIMEOUT:
342 *(int *)data = (int)wd_pretimeout;
344 case WDIOC_SETPRETIMEOUT:
345 error = wd_set_pretimeout(*(int *)data, false);
347 case WDIOC_GETTIMELEFT:
348 error = wd_get_time_left(td, &timeleft);
351 *(int *)data = (int)timeleft;
353 case WDIOC_SETTIMEOUT:
355 error = wdog_kern_pat(seconds_to_pow2ns(u));
357 case WDIOC_GETTIMEOUT:
358 u = wdog_kern_last_timeout();
362 error = wd_ioctl_patpat(data);
372 * Return the last timeout set, this is NOT the seconds from NOW until timeout,
373 * rather it is the amount of seconds passed to WDIOCPATPAT/WDIOC_SETTIMEOUT.
376 wdog_kern_last_timeout(void)
382 static struct cdevsw wd_cdevsw = {
383 .d_version = D_VERSION,
385 .d_name = "watchdog",
389 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
393 callout_init(&wd_pretimeo_handle, 1);
394 callout_init(&wd_softtimeo_handle, 1);
395 wd_dev = make_dev(&wd_cdevsw, 0,
396 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
399 callout_stop(&wd_pretimeo_handle);
400 callout_stop(&wd_softtimeo_handle);
401 callout_drain(&wd_pretimeo_handle);
402 callout_drain(&wd_softtimeo_handle);
412 DEV_MODULE(watchdog, watchdog_modevent, NULL);