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