xref: /netbsd-src/sys/dev/sysmon/swwdog.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: swwdog.c,v 1.12 2010/11/11 21:55:04 pooka 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.12 2010/11/11 21:55:04 pooka 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/kmem.h>
48 #include <sys/reboot.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/wdog.h>
52 #include <sys/workqueue.h>
53 #include <dev/sysmon/sysmonvar.h>
54 
55 #include "ioconf.h"
56 
57 struct swwdog_softc {
58 	device_t sc_dev;
59 	struct sysmon_wdog sc_smw;
60 	struct callout sc_c;
61 	int sc_wdog_armed;
62 };
63 
64 void		swwdogattach(int);
65 
66 static int	swwdog_match(device_t, cfdata_t, void *);
67 static void	swwdog_attach(device_t, device_t, void *);
68 static int	swwdog_detach(device_t, int);
69 static bool	swwdog_suspend(device_t, const pmf_qual_t *);
70 
71 static int swwdog_setmode(struct sysmon_wdog *);
72 static int swwdog_tickle(struct sysmon_wdog *);
73 
74 static int swwdog_arm(struct swwdog_softc *);
75 static int swwdog_disarm(struct swwdog_softc *);
76 
77 static void swwdog_panic(void *);
78 
79 bool swwdog_reboot = false;		/* set for panic instead of reboot */
80 
81 #define	SWDOG_DEFAULT	60		/* 60-second default period */
82 
83 CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
84 	swwdog_match, swwdog_attach, swwdog_detach, NULL);
85 
86 static void swwdog_sysctl_setup(void);
87 static struct sysctllog *swwdog_sysctllog;
88 
89 static void
90 doreboot(struct work *wrkwrkwrk, void *p)
91 {
92 
93 	cpu_reboot(0, NULL);
94 }
95 
96 static struct workqueue *wq;
97 
98 void
99 swwdogattach(int n __unused)
100 {
101 	int err;
102 	static struct cfdata cf;
103 
104 	if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
105 	    PRI_NONE, IPL_NONE, 0) != 0) {
106 		aprint_error("failed to create swwdog reboot wq");
107 	}
108 
109 	err = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
110 	if (err) {
111 		aprint_error("%s: couldn't register cfattach: %d\n",
112 		    swwdog_cd.cd_name, err);
113 		config_cfdriver_detach(&swwdog_cd);
114 		workqueue_destroy(wq);
115 		return;
116 	}
117 
118 	cf.cf_name = swwdog_cd.cd_name;
119 	cf.cf_atname = swwdog_cd.cd_name;
120 	cf.cf_unit = 0;
121 	cf.cf_fstate = FSTATE_STAR;
122 
123 	(void)config_attach_pseudo(&cf);
124 
125 	return;
126 }
127 
128 static int
129 swwdog_match(device_t parent, cfdata_t data, void *aux)
130 {
131 	return 1;
132 }
133 
134 static void
135 swwdog_attach(device_t parent, device_t self, void *aux)
136 {
137 	struct swwdog_softc *sc = device_private(self);
138 
139 	sc->sc_dev = self;
140 	sc->sc_smw.smw_name = device_xname(self);
141 	sc->sc_smw.smw_cookie = sc;
142 	sc->sc_smw.smw_setmode = swwdog_setmode;
143 	sc->sc_smw.smw_tickle = swwdog_tickle;
144 	sc->sc_smw.smw_period = SWDOG_DEFAULT;
145 	callout_init(&sc->sc_c, 0);
146 	callout_setfunc(&sc->sc_c, swwdog_panic, sc);
147 
148 	if (sysmon_wdog_register(&sc->sc_smw) == 0)
149 		aprint_normal_dev(self, "software watchdog initialized\n");
150 	else
151 		aprint_error_dev(self, "unable to register software "
152 		    "watchdog with sysmon\n");
153 
154 	if (!pmf_device_register(self, swwdog_suspend, NULL))
155 		aprint_error_dev(self, "couldn't establish power handler\n");
156 
157 	swwdog_sysctl_setup();
158 }
159 
160 static int
161 swwdog_detach(device_t self, int flags)
162 {
163 	struct swwdog_softc *sc = device_private(self);
164 
165 	swwdog_disarm(sc);
166 	callout_destroy(&sc->sc_c);
167 	sysctl_teardown(&swwdog_sysctllog);
168 	workqueue_destroy(wq);
169 
170 	return 1;
171 }
172 
173 static bool
174 swwdog_suspend(device_t dev, const pmf_qual_t *qual)
175 {
176 	struct swwdog_softc *sc = device_private(dev);
177 
178 	/* Don't allow suspend if watchdog is armed */
179 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
180 		return false;
181 	return true;
182 }
183 
184 static int
185 swwdog_setmode(struct sysmon_wdog *smw)
186 {
187 	struct swwdog_softc *sc = smw->smw_cookie;
188 	int error = 0;
189 
190 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
191 		error = swwdog_disarm(sc);
192 	} else {
193 		if (smw->smw_period == 0)
194 			return EINVAL;
195 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
196 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
197 		else
198 			sc->sc_smw.smw_period = smw->smw_period;
199 		error = swwdog_arm(sc);
200 	}
201 	return error;
202 }
203 
204 static int
205 swwdog_tickle(struct sysmon_wdog *smw)
206 {
207 
208 	return swwdog_arm(smw->smw_cookie);
209 }
210 
211 static int
212 swwdog_arm(struct swwdog_softc *sc)
213 {
214 
215 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
216 	return 0;
217 }
218 
219 static int
220 swwdog_disarm(struct swwdog_softc *sc)
221 {
222 
223 	callout_stop(&sc->sc_c);
224 	return 0;
225 }
226 
227 static void
228 swwdog_panic(void *vsc)
229 {
230 	struct swwdog_softc *sc = vsc;
231 	static struct work wk; /* we'll need it max once */
232 	bool do_panic;
233 
234 	do_panic = !swwdog_reboot;
235 	swwdog_reboot = false;
236 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
237 
238 	printf("%s: %d second timer expired\n", device_xname(sc->sc_dev),
239 	    sc->sc_smw.smw_period);
240 
241 	if (do_panic)
242 		panic("watchdog timer expired");
243 	else
244 		workqueue_enqueue(wq, &wk, NULL);
245 }
246 
247 static void
248 swwdog_sysctl_setup(void)
249 {
250 	const struct sysctlnode *me;
251 
252 	KASSERT(swwdog_sysctllog == NULL);
253 
254 	sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE,
255 	    CTLTYPE_NODE, "swwdog", NULL,
256 	    NULL, 0, NULL, 0,
257 	    CTL_HW, CTL_CREATE, CTL_EOL);
258 	sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE,
259 	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
260 	    NULL, 0, &swwdog_reboot, sizeof(bool),
261 	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
262 }
263