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>
41 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/systm.h>
51 #include <sys/watchdog.h>
52 #include <machine/bus.h>
54 #include <sys/syscallsubr.h> /* kern_clock_gettime() */
56 static int wd_set_pretimeout(int newtimeout, int disableiftoolong);
57 static void wd_timeout_cb(void *arg);
59 static struct callout wd_pretimeo_handle;
60 static int wd_pretimeout;
61 static int wd_pretimeout_act = WD_SOFT_LOG;
63 static struct callout wd_softtimeo_handle;
64 static int wd_softtimer; /* true = use softtimer instead of hardware
66 static int wd_softtimeout_act = WD_SOFT_LOG; /* action for the software timeout */
68 static struct cdev *wd_dev;
69 static volatile u_int wd_last_u; /* last timeout value set by kern_do_pat */
70 static u_int wd_last_u_sysctl; /* last timeout value set by kern_do_pat */
71 static u_int wd_last_u_sysctl_secs; /* wd_last_u in seconds */
73 SYSCTL_NODE(_hw, OID_AUTO, watchdog, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
74 "Main watchdog device");
75 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u, CTLFLAG_RD,
76 &wd_last_u_sysctl, 0, "Watchdog last update time");
77 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u_secs, CTLFLAG_RD,
78 &wd_last_u_sysctl_secs, 0, "Watchdog last update time");
80 static int wd_lastpat_valid = 0;
81 static time_t wd_lastpat = 0; /* when the watchdog was last patted */
83 /* Hook for external software watchdog to register for use if needed */
84 void (*wdog_software_attach)(void);
87 pow2ns_to_ts(int pow2ns, struct timespec *ts)
92 ts->tv_sec = ns / 1000000000ULL;
93 ts->tv_nsec = ns % 1000000000ULL;
97 pow2ns_to_ticks(int pow2ns)
102 pow2ns_to_ts(pow2ns, &ts);
103 TIMESPEC_TO_TIMEVAL(&tv, &ts);
104 return (tvtohz(&tv));
108 seconds_to_pow2ns(int seconds)
114 ns = ((uint64_t)seconds) * 1000000000ULL;
116 shifted = 1ULL << power;
124 wdog_kern_pat(u_int utim)
127 static int first = 1;
129 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
132 if ((utim & WD_LASTVAL) != 0) {
134 * if WD_LASTVAL is set, fill in the bits for timeout
135 * from the saved value in wd_last_u.
137 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
142 * Otherwise save the new interval.
143 * This can be zero (to disable the watchdog)
145 wd_last_u = (utim & WD_INTERVAL);
146 wd_last_u_sysctl = wd_last_u;
147 wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
149 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
152 /* Assume all is well; watchdog signals failure. */
155 /* Assume no watchdog available; watchdog flags success */
160 callout_stop(&wd_softtimeo_handle);
162 (void) callout_reset(&wd_softtimeo_handle,
163 pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
167 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
170 * If we no hardware watchdog responded, we have not tried to
171 * attach an external software watchdog, and one is available,
172 * attach it now and retry.
174 if (error == EOPNOTSUPP && first && *wdog_software_attach != NULL) {
175 (*wdog_software_attach)();
176 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
180 wd_set_pretimeout(wd_pretimeout, true);
182 * If we were able to arm/strobe the watchdog, then
183 * update the last time it was strobed for WDIOC_GETTIMELEFT
188 error = kern_clock_gettime(curthread /* XXX */,
189 CLOCK_MONOTONIC_FAST, &ts);
191 wd_lastpat = ts.tv_sec;
192 wd_lastpat_valid = 1;
199 wd_valid_act(int act)
202 if ((act & ~(WD_SOFT_MASK)) != 0)
208 wd_ioctl_patpat(caddr_t data)
213 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
215 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
217 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
218 (u & WD_LASTVAL) != 0))
221 return (ENOSYS); /* XXX Not implemented yet */
222 u &= ~(WD_ACTIVE | WD_PASSIVE);
224 return (wdog_kern_pat(u));
228 wd_get_time_left(struct thread *td, time_t *remainp)
233 error = kern_clock_gettime(td, CLOCK_MONOTONIC_FAST, &ts);
236 if (!wd_lastpat_valid)
238 *remainp = ts.tv_sec - wd_lastpat;
243 wd_timeout_cb(void *arg)
245 const char *type = arg;
248 if ((wd_pretimeout_act & WD_SOFT_DDB)) {
250 snprintf(kdb_why, sizeof(kdb_why), "watchdog %s-timeout", type);
252 kdb_enter(KDB_WHY_WATCHDOG, kdb_why);
255 if ((wd_pretimeout_act & WD_SOFT_LOG))
256 log(LOG_EMERG, "watchdog %s-timeout, WD_SOFT_LOG\n", type);
257 if ((wd_pretimeout_act & WD_SOFT_PRINTF))
258 printf("watchdog %s-timeout, WD_SOFT_PRINTF\n", type);
259 if ((wd_pretimeout_act & WD_SOFT_PANIC))
260 panic("watchdog %s-timeout, WD_SOFT_PANIC set", type);
264 * Called to manage timeouts.
265 * newtimeout needs to be in the range of 0 to actual watchdog timeout.
266 * if 0, we disable the pre-timeout.
267 * otherwise we set the pre-timeout provided it's not greater than the
268 * current actual watchdog timeout.
271 wd_set_pretimeout(int newtimeout, int disableiftoolong)
274 struct timespec utime_ts;
277 utime = wdog_kern_last_timeout();
278 pow2ns_to_ts(utime, &utime_ts);
279 /* do not permit a pre-timeout >= than the timeout. */
280 if (newtimeout >= utime_ts.tv_sec) {
282 * If 'disableiftoolong' then just fall through
283 * so as to disable the pre-watchdog
285 if (disableiftoolong)
291 /* disable the pre-timeout */
292 if (newtimeout == 0) {
294 callout_stop(&wd_pretimeo_handle);
298 timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
300 printf("wd_set_pretimeout: "
302 "utime: %d -> utime_ticks: %d, "
303 "hz*newtimeout: %d, "
304 "timeout_ticks: %d -> sec: %d\n",
306 utime, pow2ns_to_ticks(utime),
308 timeout_ticks, timeout_ticks / hz);
311 /* We determined the value is sane, so reset the callout */
312 (void) callout_reset(&wd_pretimeo_handle,
313 timeout_ticks, wd_timeout_cb, "pre");
314 wd_pretimeout = newtimeout;
319 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
320 int flags __unused, struct thread *td)
332 if (u == wd_softtimer)
334 /* If there is a pending timeout disallow this ioctl */
335 if (wd_last_u != 0) {
341 case WDIOC_SETSOFTTIMEOUTACT:
343 if (wd_valid_act(u)) {
344 wd_softtimeout_act = u;
349 case WDIOC_SETPRETIMEOUTACT:
351 if (wd_valid_act(u)) {
352 wd_pretimeout_act = u;
357 case WDIOC_GETPRETIMEOUT:
358 *(int *)data = (int)wd_pretimeout;
360 case WDIOC_SETPRETIMEOUT:
361 error = wd_set_pretimeout(*(int *)data, false);
363 case WDIOC_GETTIMELEFT:
364 error = wd_get_time_left(td, &timeleft);
367 *(int *)data = (int)timeleft;
369 case WDIOC_SETTIMEOUT:
371 error = wdog_kern_pat(seconds_to_pow2ns(u));
373 case WDIOC_GETTIMEOUT:
374 u = wdog_kern_last_timeout();
378 error = wd_ioctl_patpat(data);
388 * Return the last timeout set, this is NOT the seconds from NOW until timeout,
389 * rather it is the amount of seconds passed to WDIOCPATPAT/WDIOC_SETTIMEOUT.
392 wdog_kern_last_timeout(void)
398 static struct cdevsw wd_cdevsw = {
399 .d_version = D_VERSION,
401 .d_name = "watchdog",
405 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
409 callout_init(&wd_pretimeo_handle, 1);
410 callout_init(&wd_softtimeo_handle, 1);
411 wd_dev = make_dev(&wd_cdevsw, 0,
412 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
415 callout_stop(&wd_pretimeo_handle);
416 callout_stop(&wd_softtimeo_handle);
417 callout_drain(&wd_pretimeo_handle);
418 callout_drain(&wd_softtimeo_handle);
428 DEV_MODULE(watchdog, watchdog_modevent, NULL);