xref: /netbsd-src/sys/dev/sysmon/swwdog.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: swwdog.c,v 1.13 2014/04/13 13:19:50 pgoyette 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.13 2014/04/13 13:19:50 pgoyette 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 	pmf_device_deregister(self);
166 	swwdog_disarm(sc);
167 	callout_destroy(&sc->sc_c);
168 	sysctl_teardown(&swwdog_sysctllog);
169 	workqueue_destroy(wq);
170 
171 	return 1;
172 }
173 
174 static bool
175 swwdog_suspend(device_t dev, const pmf_qual_t *qual)
176 {
177 	struct swwdog_softc *sc = device_private(dev);
178 
179 	/* Don't allow suspend if watchdog is armed */
180 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
181 		return false;
182 	return true;
183 }
184 
185 static int
186 swwdog_setmode(struct sysmon_wdog *smw)
187 {
188 	struct swwdog_softc *sc = smw->smw_cookie;
189 	int error = 0;
190 
191 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
192 		error = swwdog_disarm(sc);
193 	} else {
194 		if (smw->smw_period == 0)
195 			return EINVAL;
196 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
197 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
198 		else
199 			sc->sc_smw.smw_period = smw->smw_period;
200 		error = swwdog_arm(sc);
201 	}
202 	return error;
203 }
204 
205 static int
206 swwdog_tickle(struct sysmon_wdog *smw)
207 {
208 
209 	return swwdog_arm(smw->smw_cookie);
210 }
211 
212 static int
213 swwdog_arm(struct swwdog_softc *sc)
214 {
215 
216 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
217 	return 0;
218 }
219 
220 static int
221 swwdog_disarm(struct swwdog_softc *sc)
222 {
223 
224 	callout_stop(&sc->sc_c);
225 	return 0;
226 }
227 
228 static void
229 swwdog_panic(void *vsc)
230 {
231 	struct swwdog_softc *sc = vsc;
232 	static struct work wk; /* we'll need it max once */
233 	bool do_panic;
234 
235 	do_panic = !swwdog_reboot;
236 	swwdog_reboot = false;
237 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
238 
239 	printf("%s: %d second timer expired\n", device_xname(sc->sc_dev),
240 	    sc->sc_smw.smw_period);
241 
242 	if (do_panic)
243 		panic("watchdog timer expired");
244 	else
245 		workqueue_enqueue(wq, &wk, NULL);
246 }
247 
248 static void
249 swwdog_sysctl_setup(void)
250 {
251 	const struct sysctlnode *me;
252 
253 	KASSERT(swwdog_sysctllog == NULL);
254 
255 	sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE,
256 	    CTLTYPE_NODE, "swwdog", NULL,
257 	    NULL, 0, NULL, 0,
258 	    CTL_HW, CTL_CREATE, CTL_EOL);
259 	sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE,
260 	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
261 	    NULL, 0, &swwdog_reboot, sizeof(bool),
262 	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
263 }
264