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 */
81 /* Hook for external software watchdog to register for use if needed */
82 void (*wdog_software_attach)(void);
85 pow2ns_to_ts(int pow2ns, struct timespec *ts)
90 ts->tv_sec = ns / 1000000000ULL;
91 ts->tv_nsec = ns % 1000000000ULL;
95 pow2ns_to_ticks(int pow2ns)
100 pow2ns_to_ts(pow2ns, &ts);
101 TIMESPEC_TO_TIMEVAL(&tv, &ts);
102 return (tvtohz(&tv));
106 seconds_to_pow2ns(int seconds)
112 ns = ((uint64_t)seconds) * 1000000000ULL;
114 shifted = 1ULL << power;
123 wdog_kern_pat(u_int utim)
126 static int first = 1;
128 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
131 if ((utim & WD_LASTVAL) != 0) {
133 * if WD_LASTVAL is set, fill in the bits for timeout
134 * from the saved value in wd_last_u.
136 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
141 * Otherwise save the new interval.
142 * This can be zero (to disable the watchdog)
144 wd_last_u = (utim & WD_INTERVAL);
145 wd_last_u_sysctl = wd_last_u;
146 wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
148 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
151 /* Assume all is well; watchdog signals failure. */
154 /* Assume no watchdog available; watchdog flags success */
159 callout_stop(&wd_softtimeo_handle);
161 (void) callout_reset(&wd_softtimeo_handle,
162 pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
166 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
169 * If we no hardware watchdog responded, we have not tried to
170 * attach an external software watchdog, and one is available,
171 * attach it now and retry.
173 if (error == EOPNOTSUPP && first && *wdog_software_attach != NULL) {
174 (*wdog_software_attach)();
175 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
179 wd_set_pretimeout(wd_pretimeout, true);
181 * If we were able to arm/strobe the watchdog, then
182 * update the last time it was strobed for WDIOC_GETTIMELEFT
187 error = kern_clock_gettime(curthread /* XXX */,
188 CLOCK_MONOTONIC_FAST, &ts);
190 wd_lastpat = ts.tv_sec;
191 wd_lastpat_valid = 1;
198 wd_valid_act(int act)
201 if ((act & ~(WD_SOFT_MASK)) != 0)
207 wd_ioctl_patpat(caddr_t data)
212 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
214 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
216 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
217 (u & WD_LASTVAL) != 0))
220 return (ENOSYS); /* XXX Not implemented yet */
221 u &= ~(WD_ACTIVE | WD_PASSIVE);
223 return (wdog_kern_pat(u));
227 wd_get_time_left(struct thread *td, time_t *remainp)
232 error = kern_clock_gettime(td, CLOCK_MONOTONIC_FAST, &ts);
235 if (!wd_lastpat_valid)
237 *remainp = ts.tv_sec - wd_lastpat;
242 wd_timeout_cb(void *arg)
244 const char *type = arg;
247 if ((wd_pretimeout_act & WD_SOFT_DDB)) {
249 snprintf(kdb_why, sizeof(kdb_why), "watchdog %s-timeout", type);
251 kdb_enter(KDB_WHY_WATCHDOG, kdb_why);
254 if ((wd_pretimeout_act & WD_SOFT_LOG))
255 log(LOG_EMERG, "watchdog %s-timeout, WD_SOFT_LOG\n", type);
256 if ((wd_pretimeout_act & WD_SOFT_PRINTF))
257 printf("watchdog %s-timeout, WD_SOFT_PRINTF\n", type);
258 if ((wd_pretimeout_act & WD_SOFT_PANIC))
259 panic("watchdog %s-timeout, WD_SOFT_PANIC set", type);
263 * Called to manage timeouts.
264 * newtimeout needs to be in the range of 0 to actual watchdog timeout.
265 * if 0, we disable the pre-timeout.
266 * otherwise we set the pre-timeout provided it's not greater than the
267 * current actual watchdog timeout.
270 wd_set_pretimeout(int newtimeout, int disableiftoolong)
273 struct timespec utime_ts;
276 utime = wdog_kern_last_timeout();
277 pow2ns_to_ts(utime, &utime_ts);
278 /* do not permit a pre-timeout >= than the timeout. */
279 if (newtimeout >= utime_ts.tv_sec) {
281 * If 'disableiftoolong' then just fall through
282 * so as to disable the pre-watchdog
284 if (disableiftoolong)
290 /* disable the pre-timeout */
291 if (newtimeout == 0) {
293 callout_stop(&wd_pretimeo_handle);
297 timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
299 printf("wd_set_pretimeout: "
301 "utime: %d -> utime_ticks: %d, "
302 "hz*newtimeout: %d, "
303 "timeout_ticks: %d -> sec: %d\n",
305 utime, pow2ns_to_ticks(utime),
307 timeout_ticks, timeout_ticks / hz);
310 /* We determined the value is sane, so reset the callout */
311 (void) callout_reset(&wd_pretimeo_handle,
312 timeout_ticks, wd_timeout_cb, "pre");
313 wd_pretimeout = newtimeout;
318 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
319 int flags __unused, struct thread *td)
331 if (u == wd_softtimer)
333 /* If there is a pending timeout disallow this ioctl */
334 if (wd_last_u != 0) {
340 case WDIOC_SETSOFTTIMEOUTACT:
342 if (wd_valid_act(u)) {
343 wd_softtimeout_act = u;
348 case WDIOC_SETPRETIMEOUTACT:
350 if (wd_valid_act(u)) {
351 wd_pretimeout_act = u;
356 case WDIOC_GETPRETIMEOUT:
357 *(int *)data = (int)wd_pretimeout;
359 case WDIOC_SETPRETIMEOUT:
360 error = wd_set_pretimeout(*(int *)data, false);
362 case WDIOC_GETTIMELEFT:
363 error = wd_get_time_left(td, &timeleft);
366 *(int *)data = (int)timeleft;
368 case WDIOC_SETTIMEOUT:
370 error = wdog_kern_pat(seconds_to_pow2ns(u));
372 case WDIOC_GETTIMEOUT:
373 u = wdog_kern_last_timeout();
377 error = wd_ioctl_patpat(data);
387 * Return the last timeout set, this is NOT the seconds from NOW until timeout,
388 * rather it is the amount of seconds passed to WDIOCPATPAT/WDIOC_SETTIMEOUT.
391 wdog_kern_last_timeout(void)
397 static struct cdevsw wd_cdevsw = {
398 .d_version = D_VERSION,
400 .d_name = "watchdog",
404 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
408 callout_init(&wd_pretimeo_handle, 1);
409 callout_init(&wd_softtimeo_handle, 1);
410 wd_dev = make_dev(&wd_cdevsw, 0,
411 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
414 callout_stop(&wd_pretimeo_handle);
415 callout_stop(&wd_softtimeo_handle);
416 callout_drain(&wd_pretimeo_handle);
417 callout_drain(&wd_softtimeo_handle);
427 DEV_MODULE(watchdog, watchdog_modevent, NULL);