xref: /netbsd-src/sys/dev/sysmon/swwdog.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: swwdog.c,v 1.20 2019/03/27 09:52:16 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.20 2019/03/27 09:52:16 pgoyette Exp $");
37 
38 /*
39  *
40  * Software watchdog timer
41  *
42  */
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/callout.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 <sys/module.h>
54 #include <dev/sysmon/sysmonvar.h>
55 
56 #ifndef _MODULE
57 #include "opt_modular.h"
58 #endif
59 
60 struct swwdog_softc {
61 	device_t		sc_dev;
62 	struct sysmon_wdog	sc_smw;
63 	struct callout		sc_c;
64 	int			sc_armed;
65 };
66 
67 bool	swwdog_reboot = false;	/* false --> panic , true  --> reboot */
68 
69 static struct workqueue *wq;
70 static device_t		swwdog_dev;
71 
72 MODULE(MODULE_CLASS_DRIVER, swwdog, "sysmon_wdog");
73 
74 #ifdef _MODULE
75 CFDRIVER_DECL(swwdog, DV_DULL, NULL);
76 #endif
77 
78 int swwdogattach(int);
79 
80 static int	swwdog_setmode(struct sysmon_wdog *);
81 static int	swwdog_tickle(struct sysmon_wdog *);
82 static bool	swwdog_suspend(device_t, const pmf_qual_t *);
83 static int	swwdog_arm(struct swwdog_softc *);
84 static int	swwdog_disarm(struct swwdog_softc *);
85 
86 static void	swwdog_panic(void *);
87 
88 static void	swwdog_sysctl_setup(void);
89 static struct sysctllog *swwdog_sysctllog = NULL;
90 
91 static int	swwdog_match(device_t, cfdata_t, void *);
92 static void	swwdog_attach(device_t, device_t, void *);
93 static int	swwdog_detach(device_t, int);
94 static bool	swwdog_suspend(device_t, const pmf_qual_t *);
95 
96 static int	swwdog_init(void *);
97 static int	swwdog_fini(void *);
98 static int	swwdog_modcmd(modcmd_t, void *);
99 
100 CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
101 	swwdog_match, swwdog_attach, swwdog_detach, NULL);
102 extern struct cfdriver swwdog_cd;
103 
104 #define	SWDOG_DEFAULT	60		/* 60-second default period */
105 
106 static void
107 doreboot(struct work *wrkwrkwrk, void *p)
108 {
109 
110 	cpu_reboot(0, NULL);
111 }
112 
113 int
114 swwdogattach(int n __unused)
115 {
116 	int error;
117 	static struct cfdata cf;
118 
119 	if (workqueue_create(&wq, "swwreboot", doreboot, NULL,
120 	    PRI_NONE, IPL_NONE, 0) != 0) {
121 		aprint_error("failed to create swwdog reboot wq");
122 		return 1;
123 	}
124 
125 	error = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
126 	if (error) {
127 		aprint_error("%s: unable to attach cfattach\n",
128 		    swwdog_cd.cd_name);
129 		workqueue_destroy(wq);
130 		return error;
131 	}
132 
133 	cf.cf_name = swwdog_cd.cd_name;
134 	cf.cf_atname = swwdog_cd.cd_name;
135 	cf.cf_unit = 0;
136 	cf.cf_fstate = FSTATE_STAR;
137 	cf.cf_pspec = NULL;
138 	cf.cf_loc = NULL;
139 	cf.cf_flags = 0;
140 
141 	swwdog_dev = config_attach_pseudo(&cf);
142 
143 	if (swwdog_dev == NULL) {
144 		config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
145 		workqueue_destroy(wq);
146 		return 1;
147 	}
148 	return 0;
149 }
150 
151 static int
152 swwdog_match(device_t parent, cfdata_t data, void *aux)
153 {
154 
155 	return 1;
156 }
157 
158 static void
159 swwdog_attach(device_t parent, device_t self, void *aux)
160 {
161 	struct swwdog_softc *sc = device_private(self);
162 
163 	sc->sc_dev = self;
164 	sc->sc_smw.smw_name = device_xname(self);
165 	sc->sc_smw.smw_cookie = sc;
166 	sc->sc_smw.smw_setmode = swwdog_setmode;
167 	sc->sc_smw.smw_tickle = swwdog_tickle;
168 	sc->sc_smw.smw_period = SWDOG_DEFAULT;
169 
170 	callout_init(&sc->sc_c, 0);
171 	callout_setfunc(&sc->sc_c, swwdog_panic, sc);
172 
173 	if (sysmon_wdog_register(&sc->sc_smw) == 0)
174 		aprint_normal_dev(self, "software watchdog initialized\n");
175 	else {
176 		aprint_error_dev(self, "unable to register software "
177 		    "watchdog with sysmon\n");
178 		callout_destroy(&sc->sc_c);
179 		return;
180 	}
181 
182 	if (!pmf_device_register(self, swwdog_suspend, NULL))
183 		aprint_error_dev(self, "couldn't establish power handler\n");
184 
185 	swwdog_sysctl_setup();
186 }
187 
188 static int
189 swwdog_detach(device_t self, int flags)
190 {
191 	struct swwdog_softc *sc = device_private(self);
192 
193 	pmf_device_deregister(self);
194 	swwdog_disarm(sc);
195 	sysctl_teardown(&swwdog_sysctllog);
196 	sysmon_wdog_unregister(&sc->sc_smw);
197 	callout_destroy(&sc->sc_c);
198 	workqueue_destroy(wq);
199 
200 	return 0;
201 }
202 
203 static bool
204 swwdog_suspend(device_t dev, const pmf_qual_t *qual)
205 {
206 	struct swwdog_softc *sc = device_private(dev);
207 
208 	/* Don't allow suspend if watchdog is armed */
209 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
210 		return false;
211 	return true;
212 }
213 
214 static int
215 swwdog_setmode(struct sysmon_wdog *smw)
216 {
217 	struct swwdog_softc *sc = smw->smw_cookie;
218 	int error = 0;
219 
220 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
221 		error = swwdog_disarm(sc);
222 	} else {
223 		if (smw->smw_period == 0)
224 			return EINVAL;
225 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
226 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
227 		else
228 			sc->sc_smw.smw_period = smw->smw_period;
229 		error = swwdog_arm(sc);
230 	}
231 	return error;
232 }
233 
234 static int
235 swwdog_tickle(struct sysmon_wdog *smw)
236 {
237 
238 	return swwdog_arm(smw->smw_cookie);
239 }
240 
241 static int
242 swwdog_arm(struct swwdog_softc *sc)
243 {
244 
245 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
246 	return 0;
247 }
248 
249 static int
250 swwdog_disarm(struct swwdog_softc *sc)
251 {
252 
253 	callout_stop(&sc->sc_c);
254 	return 0;
255 }
256 
257 static void
258 swwdog_panic(void *vsc)
259 {
260 	struct swwdog_softc *sc = vsc;
261 	static struct work wk; /* we'll need it max once */
262 	bool do_panic;
263 
264 	do_panic = !swwdog_reboot;
265 	swwdog_reboot = false;
266 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
267 
268 	printf("%s: %d second timer expired\n", "swwdog",
269 	    sc->sc_smw.smw_period);
270 
271 	if (do_panic)
272 		panic("watchdog timer expired");
273 	else
274 		workqueue_enqueue(wq, &wk, NULL);
275 }
276 
277 static void
278 swwdog_sysctl_setup(void)
279 {
280 	const struct sysctlnode *me;
281 
282 	KASSERT(swwdog_sysctllog == NULL);
283 
284 	sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE,
285 	    CTLTYPE_NODE, "swwdog", NULL,
286 	    NULL, 0, NULL, 0,
287 	    CTL_HW, CTL_CREATE, CTL_EOL);
288 	sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE,
289 	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
290 	    NULL, 0, &swwdog_reboot, sizeof(bool),
291 	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
292 }
293 
294 /*
295  * Module management
296  */
297 
298 static
299 int
300 swwdog_init(void *arg)
301 {
302 	/*
303 	 * Merge the driver info into the kernel tables and attach the
304 	 * pseudo-device
305 	 */
306 	int error = 0;
307 
308 
309 #ifdef _MODULE
310 	error = config_cfdriver_attach(&swwdog_cd);
311 	if (error) {
312 		aprint_error("%s: unable to attach cfdriver\n",
313 		    swwdog_cd.cd_name);
314 		return error;
315 	}
316 	error = swwdogattach(1);
317 	if (error) {
318 		aprint_error("%s: device attach failed\n", swwdog_cd.cd_name);
319 		config_cfdriver_detach(&swwdog_cd);
320 	}
321 #endif
322 
323 	return error;
324 }
325 
326 static
327 int
328 swwdog_fini(void *arg)
329 {
330 	int error;
331 
332 	error = config_detach(swwdog_dev, 0);
333 
334 #ifdef _MODULE
335 	error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca);
336 	if (error)
337 		aprint_error("%s: error detaching cfattach: %d\n",
338 		    swwdog_cd.cd_name, error);
339 
340 	error = config_cfdriver_detach(&swwdog_cd);
341 	if (error)
342 		aprint_error("%s: error detaching cfdriver: %d\n",
343 		    swwdog_cd.cd_name, error);
344 #endif
345 
346         return error;
347 }
348 
349 static
350 int
351 swwdog_modcmd(modcmd_t cmd, void *arg)
352 {
353 	int ret;
354 
355 	switch (cmd) {
356 	case MODULE_CMD_INIT:
357 		ret = swwdog_init(arg);
358 		break;
359 
360 	case MODULE_CMD_FINI:
361 		ret = swwdog_fini(arg);
362 		break;
363 
364 	case MODULE_CMD_STAT:
365 	default:
366 		ret = ENOTTY;
367 	}
368 
369 	return ret;
370 }
371 
372