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