xref: /freebsd/sys/sys/watchdog.h (revision 26d6617f3e5406d9f92d87674f1667dd856828a3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Poul-Henning Kamp
5  * Copyright (c) 2013 iXsystems.com,
6  *                    author: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 #ifndef _SYS_WATCHDOG_H
32 #define	_SYS_WATCHDOG_H
33 
34 #include <sys/ioccom.h>
35 #include <sys/_types.h>
36 
37 #define	_PATH_WATCHDOG	"fido"
38 
39 #define WDIOC_PATPAT	    _IOW('W', 52, sbintime_t)	/* pat the watchdog */
40 #define WDIOC_SETTIMEOUT    _IOW('W', 53, sbintime_t)	/* set/reset the timer */
41 #define WDIOC_GETTIMEOUT    _IOR('W', 54, sbintime_t)	/* get total timeout */
42 #define WDIOC_GETTIMELEFT   _IOR('W', 55, sbintime_t)	/* get time left */
43 #define WDIOC_GETPRETIMEOUT _IOR('W', 56, sbintime_t)	/* get the pre-timeout */
44 #define WDIOC_SETPRETIMEOUT _IOW('W', 57, sbintime_t)	/* set the pre-timeout */
45 /* set the action when a pre-timeout occurs see: WD_SOFT_* */
46 #define WDIOC_SETPRETIMEOUTACT _IOW('W', 48, int)
47 
48 /* use software watchdog instead of hardware */
49 #define WDIOC_SETSOFT	_IOW('W', 49, int)
50 #define WDIOC_SETSOFTTIMEOUTACT	_IOW('W', 50, int)
51 
52 #define	WDIOC_CONTROL		_IOW('W', 51, int)	/* configure watchdog */
53 
54 #define WD_ACTIVE	0x8000000
55 	/*
56 	 * Watchdog reset, timeout set to value in WD_INTERVAL field.
57 	 * The kernel will arm the watchdog and unless the userland
58 	 * program calls WDIOCPATPAT again before the timer expires
59 	 * the system will reinitialize.
60 	 */
61 
62 #define WD_PASSIVE	0x0400000
63 	/*
64 	 * Set the watchdog in passive mode.
65 	 * The kernel will chose an appropriate timeout duration and
66 	 * periodically reset the timer provided everything looks all
67 	 * right to the kernel.
68  	 */
69 
70 #define WD_LASTVAL	0x0200000
71 	/*
72 	 * Use the already last used timeout value.
73 	 * The kernel will use as timeout the last valid timeout provided.
74  	 */
75 
76 #define WD_INTERVAL	0x00000ff
77 	/*
78 	 * Mask for duration bits.
79 	 * The watchdog will have a nominal patience of 2^N * nanoseconds.
80 	 * Example:  N == 30 gives a patience of 2^30 nanoseconds ~= 1 second.
81 	 * NB: Expect variance in the +/- 10-20% range.
82 	 */
83 
84 /* Handy macros for humans not used to power of two nanoseconds */
85 #define WD_TO_NEVER	0
86 #define WD_TO_1MS	20
87 #define WD_TO_125MS	27
88 #define WD_TO_250MS	28
89 #define WD_TO_500MS	29
90 #define WD_TO_1SEC	30
91 #define WD_TO_2SEC	31
92 #define WD_TO_4SEC	32
93 #define WD_TO_8SEC	33
94 #define WD_TO_16SEC	34
95 #define WD_TO_32SEC	35
96 #define WD_TO_64SEC	36
97 #define WD_TO_128SEC	37
98 
99 /* Control options for WDIOC_CONTROL */
100 #define	WD_CTRL_DISABLE	0x00000000
101 #define	WD_CTRL_ENABLE	0x00000001
102 #define	WD_CTRL_RESET	0x00000002
103 
104 /* action on pre-timeout trigger */
105 #define	WD_SOFT_PANIC	0x01	/* panic */
106 #define	WD_SOFT_DDB	0x02	/* enter debugger */
107 #define	WD_SOFT_LOG	0x04	/* log(9) */
108 #define	WD_SOFT_PRINTF	0x08	/* printf(9) */
109 #define WD_SOFT_MASK	0x0f	/* all of the above */
110 
111 #ifdef _KERNEL
112 
113 #include <sys/_eventhandler.h>
114 
115 typedef void (*watchdog_fn)(void *, u_int, int *);
116 typedef void (*watchdog_sbt_fn)(void *, sbintime_t, sbintime_t *, int *);
117 
118 EVENTHANDLER_DECLARE(watchdog_list, watchdog_fn);
119 EVENTHANDLER_DECLARE(watchdog_sbt_list, watchdog_sbt_fn);
120 
121 u_int	wdog_kern_last_timeout(void);
122 int	wdog_kern_pat(u_int utim);
123 sbintime_t	wdog_kern_last_timeout_sbt(void);
124 int		wdog_kern_pat_sbt(sbintime_t utim);
125 int		wdog_control(int ctrl);
126 
127 /*
128  * The following function pointer is used to attach a software watchdog
129  * if no hardware watchdog has been attached, and if the software module
130  * has initialized the function pointer.
131  */
132 
133 extern void (*wdog_software_attach)(void);
134 #endif
135 
136 #endif /* _SYS_WATCHDOG_H */
137