xref: /netbsd-src/sys/dev/sysmon/swwdog.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: swwdog.c,v 1.7 2007/07/09 21:01:23 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 2004, 2005 Steven M. Bellovin
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Steven M. Bellovin
18  * 4. The name of the author contributors may not be used to endorse or
19  *    promote products derived from this software without specific prior
20  *    written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.7 2007/07/09 21:01:23 ad Exp $");
37 
38 /*
39  *
40  * Software watchdog timer
41  *
42  */
43 #include <sys/param.h>
44 #include <sys/callout.h>
45 #include <sys/device.h>
46 #include <sys/kernel.h>
47 #include <sys/reboot.h>
48 #include <sys/systm.h>
49 #include <sys/wdog.h>
50 #include <dev/sysmon/sysmonvar.h>
51 
52 #include "swwdog.h"
53 
54 struct swwdog_softc {
55 	struct sysmon_wdog sc_smw;
56 	struct callout sc_c;
57 	char sc_name[20];
58 	int sc_wdog_armed;
59 } sc_wdog[NSWWDOG];
60 
61 void	swwdogattach(int);
62 
63 static int swwdog_setmode(struct sysmon_wdog *);
64 static int swwdog_tickle(struct sysmon_wdog *);
65 
66 static int swwdog_arm(struct swwdog_softc *);
67 static int swwdog_disarm(struct swwdog_softc *);
68 
69 static void swwdog_panic(void *);
70 
71 int swwdog_reboot = 0;		/* set for panic instead of reboot */
72 
73 #define	SWDOG_DEFAULT	60		/* 60-second default period */
74 
75 void
76 swwdogattach(int count __unused)
77 {
78 	int i;
79 
80 	for (i = 0; i < NSWWDOG; i++) {
81 		struct swwdog_softc *sc = &sc_wdog[i];
82 
83 		snprintf(sc->sc_name, sizeof sc->sc_name, "swwdog%d", i);
84 		printf("%s: ", sc->sc_name);
85 		sc->sc_smw.smw_name = sc->sc_name;
86 		sc->sc_smw.smw_cookie = sc;
87 		sc->sc_smw.smw_setmode = swwdog_setmode;
88 		sc->sc_smw.smw_tickle = swwdog_tickle;
89 		sc->sc_smw.smw_period = SWDOG_DEFAULT;
90 		callout_init(&sc->sc_c, 0);
91 		callout_setfunc(&sc->sc_c, swwdog_panic, sc);
92 
93 		if (sysmon_wdog_register(&sc->sc_smw) == 0)
94 			printf("software watchdog initialized\n");
95 		else
96 			printf("unable to register software watchdog "
97 			    "with sysmon\n");
98 	}
99 }
100 
101 static int
102 swwdog_setmode(struct sysmon_wdog *smw)
103 {
104 	struct swwdog_softc *sc = smw->smw_cookie;
105 	int error = 0;
106 
107 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
108 		error = swwdog_disarm(sc);
109 	} else {
110 		if (smw->smw_period == 0)
111 			return EINVAL;
112 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
113 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
114 		else
115 			sc->sc_smw.smw_period = smw->smw_period;
116 		error = swwdog_arm(sc);
117 	}
118 	return error;
119 }
120 
121 static int
122 swwdog_tickle(struct sysmon_wdog *smw)
123 {
124 
125 	return swwdog_arm(smw->smw_cookie);
126 }
127 
128 static int
129 swwdog_arm(struct swwdog_softc *sc)
130 {
131 
132 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
133 	return 0;
134 }
135 
136 static int
137 swwdog_disarm(struct swwdog_softc *sc)
138 {
139 
140 	callout_stop(&sc->sc_c);
141 	return 0;
142 }
143 
144 static void
145 swwdog_panic(void *vsc)
146 {
147 	struct swwdog_softc *sc = vsc;
148 	int do_panic;
149 
150 	do_panic = swwdog_reboot;
151 	swwdog_reboot = 1;
152 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
153 
154 	printf("%s: %d second timer expired", sc->sc_name,
155 	    sc->sc_smw.smw_period);
156 
157 	if (do_panic)
158 		panic("watchdog timer expired");
159 	else
160 		cpu_reboot(0, NULL);
161 }
162