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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/watchdog.h>
49 #include <machine/bus.h>
51 #include <sys/syscallsubr.h> /* kern_clock_gettime() */
53 static int wd_set_pretimeout(int newtimeout, int disableiftoolong);
54 static void wd_timeout_cb(void *arg);
56 static struct callout wd_pretimeo_handle;
57 static int wd_pretimeout;
58 static int wd_pretimeout_act = WD_SOFT_LOG;
60 static struct callout wd_softtimeo_handle;
61 static int wd_softtimer; /* true = use softtimer instead of hardware
63 static int wd_softtimeout_act = WD_SOFT_LOG; /* action for the software timeout */
65 static struct cdev *wd_dev;
66 static volatile u_int wd_last_u; /* last timeout value set by kern_do_pat */
67 static u_int wd_last_u_sysctl; /* last timeout value set by kern_do_pat */
68 static u_int wd_last_u_sysctl_secs; /* wd_last_u in seconds */
70 SYSCTL_NODE(_hw, OID_AUTO, watchdog, CTLFLAG_RD, 0, "Main watchdog device");
71 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u, CTLFLAG_RD,
72 &wd_last_u_sysctl, 0, "Watchdog last update time");
73 SYSCTL_UINT(_hw_watchdog, OID_AUTO, wd_last_u_secs, CTLFLAG_RD,
74 &wd_last_u_sysctl_secs, 0, "Watchdog last update time");
76 static int wd_lastpat_valid = 0;
77 static time_t wd_lastpat = 0; /* when the watchdog was last patted */
80 pow2ns_to_ts(int pow2ns, struct timespec *ts)
85 ts->tv_sec = ns / 1000000000ULL;
86 ts->tv_nsec = ns % 1000000000ULL;
90 pow2ns_to_ticks(int pow2ns)
95 pow2ns_to_ts(pow2ns, &ts);
96 TIMESPEC_TO_TIMEVAL(&tv, &ts);
101 seconds_to_pow2ns(int seconds)
107 ns = ((uint64_t)seconds) * 1000000000ULL;
109 shifted = 1ULL << power;
118 wdog_kern_pat(u_int utim)
122 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
125 if ((utim & WD_LASTVAL) != 0) {
127 * if WD_LASTVAL is set, fill in the bits for timeout
128 * from the saved value in wd_last_u.
130 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
135 * Otherwise save the new interval.
136 * This can be zero (to disable the watchdog)
138 wd_last_u = (utim & WD_INTERVAL);
139 wd_last_u_sysctl = wd_last_u;
140 wd_last_u_sysctl_secs = pow2ns_to_ticks(wd_last_u) / hz;
142 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
145 /* Assume all is well; watchdog signals failure. */
148 /* Assume no watchdog available; watchdog flags success */
153 callout_stop(&wd_softtimeo_handle);
155 (void) callout_reset(&wd_softtimeo_handle,
156 pow2ns_to_ticks(utim), wd_timeout_cb, "soft");
160 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
162 wd_set_pretimeout(wd_pretimeout, true);
164 * If we were able to arm/strobe the watchdog, then
165 * update the last time it was strobed for WDIOC_GETTIMELEFT
170 error = kern_clock_gettime(curthread /* XXX */,
171 CLOCK_MONOTONIC_FAST, &ts);
173 wd_lastpat = ts.tv_sec;
174 wd_lastpat_valid = 1;
181 wd_valid_act(int act)
184 if ((act & ~(WD_SOFT_MASK)) != 0)
190 wd_ioctl_patpat(caddr_t data)
195 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
197 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
199 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
200 (u & WD_LASTVAL) != 0))
203 return (ENOSYS); /* XXX Not implemented yet */
204 u &= ~(WD_ACTIVE | WD_PASSIVE);
206 return (wdog_kern_pat(u));
210 wd_get_time_left(struct thread *td, time_t *remainp)
215 error = kern_clock_gettime(td, CLOCK_MONOTONIC_FAST, &ts);
218 if (!wd_lastpat_valid)
220 *remainp = ts.tv_sec - wd_lastpat;
225 wd_timeout_cb(void *arg)
227 const char *type = arg;
230 if ((wd_pretimeout_act & WD_SOFT_DDB)) {
232 snprintf(kdb_why, sizeof(kdb_why), "watchdog %s timeout", type);
234 kdb_enter(KDB_WHY_WATCHDOG, kdb_why);
237 if ((wd_pretimeout_act & WD_SOFT_LOG))
238 log(LOG_EMERG, "watchdog %s-timeout, WD_SOFT_LOG", type);
239 if ((wd_pretimeout_act & WD_SOFT_PRINTF))
240 printf("watchdog %s-timeout, WD_SOFT_PRINTF\n", type);
241 if ((wd_pretimeout_act & WD_SOFT_PANIC))
242 panic("watchdog %s-timeout, WD_SOFT_PANIC set", type);
246 * Called to manage timeouts.
247 * newtimeout needs to be in the range of 0 to actual watchdog timeout.
248 * if 0, we disable the pre-timeout.
249 * otherwise we set the pre-timeout provided it's not greater than the
250 * current actual watchdog timeout.
253 wd_set_pretimeout(int newtimeout, int disableiftoolong)
256 struct timespec utime_ts;
259 utime = wdog_kern_last_timeout();
260 pow2ns_to_ts(utime, &utime_ts);
261 /* do not permit a pre-timeout >= than the timeout. */
262 if (newtimeout >= utime_ts.tv_sec) {
264 * If 'disableiftoolong' then just fall through
265 * so as to disable the pre-watchdog
267 if (disableiftoolong)
273 /* disable the pre-timeout */
274 if (newtimeout == 0) {
276 callout_stop(&wd_pretimeo_handle);
280 timeout_ticks = pow2ns_to_ticks(utime) - (hz*newtimeout);
282 printf("wd_set_pretimeout: "
284 "utime: %d -> utime_ticks: %d, "
285 "hz*newtimeout: %d, "
286 "timeout_ticks: %d -> sec: %d\n",
288 utime, pow2ns_to_ticks(utime),
290 timeout_ticks, timeout_ticks / hz);
293 /* We determined the value is sane, so reset the callout */
294 (void) callout_reset(&wd_pretimeo_handle,
296 wd_timeout_cb, "pre-timeout");
297 wd_pretimeout = newtimeout;
302 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
303 int flags __unused, struct thread *td)
315 if (u == wd_softtimer)
317 /* If there is a pending timeout disallow this ioctl */
318 if (wd_last_u != 0) {
324 case WDIOC_SETSOFTTIMEOUTACT:
326 if (wd_valid_act(u)) {
327 wd_softtimeout_act = u;
332 case WDIOC_SETPRETIMEOUTACT:
334 if (wd_valid_act(u)) {
335 wd_pretimeout_act = u;
340 case WDIOC_GETPRETIMEOUT:
341 *(int *)data = (int)wd_pretimeout;
343 case WDIOC_SETPRETIMEOUT:
344 error = wd_set_pretimeout(*(int *)data, false);
346 case WDIOC_GETTIMELEFT:
347 error = wd_get_time_left(td, &timeleft);
350 *(int *)data = (int)timeleft;
352 case WDIOC_SETTIMEOUT:
354 error = wdog_kern_pat(seconds_to_pow2ns(u));
356 case WDIOC_GETTIMEOUT:
357 u = wdog_kern_last_timeout();
361 error = wd_ioctl_patpat(data);
371 * Return the last timeout set, this is NOT the seconds from NOW until timeout,
372 * rather it is the amount of seconds passed to WDIOCPATPAT/WDIOC_SETTIMEOUT.
375 wdog_kern_last_timeout(void)
381 static struct cdevsw wd_cdevsw = {
382 .d_version = D_VERSION,
384 .d_name = "watchdog",
388 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
392 callout_init(&wd_pretimeo_handle, 1);
393 callout_init(&wd_softtimeo_handle, 1);
394 wd_dev = make_dev(&wd_cdevsw, 0,
395 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
398 callout_stop(&wd_pretimeo_handle);
399 callout_stop(&wd_softtimeo_handle);
400 callout_drain(&wd_pretimeo_handle);
401 callout_drain(&wd_softtimeo_handle);
411 DEV_MODULE(watchdog, watchdog_modevent, NULL);