Home | History | Annotate | Line # | Download | only in sysmon
swwdog.c revision 1.7.32.2
      1  1.7.32.2    yamt /*	$NetBSD: swwdog.c,v 1.7.32.2 2010/08/11 22:54:12 yamt Exp $	*/
      2       1.1     smb 
      3       1.1     smb /*
      4       1.1     smb  * Copyright (c) 2004, 2005 Steven M. Bellovin
      5       1.1     smb  * All rights reserved.
      6       1.1     smb  *
      7       1.1     smb  * Redistribution and use in source and binary forms, with or without
      8       1.1     smb  * modification, are permitted provided that the following conditions
      9       1.1     smb  * are met:
     10       1.1     smb  * 1. Redistributions of source code must retain the above copyright
     11       1.1     smb  *    notice, this list of conditions and the following disclaimer.
     12       1.1     smb  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1     smb  *    notice, this list of conditions and the following disclaimer in the
     14       1.1     smb  *    documentation and/or other materials provided with the distribution.
     15       1.1     smb  * 3. All advertising materials mentioning features or use of this software
     16       1.1     smb  *    must display the following acknowledgement:
     17       1.1     smb  *      This product includes software developed by Steven M. Bellovin
     18       1.1     smb  * 4. The name of the author contributors may not be used to endorse or
     19       1.1     smb  *    promote products derived from this software without specific prior
     20       1.1     smb  *    written permission.
     21       1.1     smb  *
     22       1.1     smb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
     23       1.1     smb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24       1.1     smb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25       1.1     smb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     26       1.1     smb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.1     smb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.1     smb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.1     smb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.1     smb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.1     smb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32       1.1     smb  * POSSIBILITY OF SUCH DAMAGE.
     33       1.1     smb  */
     34       1.1     smb 
     35       1.1     smb #include <sys/cdefs.h>
     36  1.7.32.2    yamt __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.7.32.2 2010/08/11 22:54:12 yamt Exp $");
     37       1.1     smb 
     38       1.1     smb /*
     39       1.1     smb  *
     40       1.1     smb  * Software watchdog timer
     41       1.1     smb  *
     42       1.1     smb  */
     43       1.1     smb #include <sys/param.h>
     44       1.2   perry #include <sys/callout.h>
     45       1.2   perry #include <sys/device.h>
     46       1.2   perry #include <sys/kernel.h>
     47  1.7.32.2    yamt #include <sys/kmem.h>
     48       1.1     smb #include <sys/reboot.h>
     49       1.1     smb #include <sys/systm.h>
     50  1.7.32.2    yamt #include <sys/sysctl.h>
     51       1.1     smb #include <sys/wdog.h>
     52       1.1     smb #include <dev/sysmon/sysmonvar.h>
     53       1.1     smb 
     54  1.7.32.2    yamt #include "ioconf.h"
     55  1.7.32.2    yamt 
     56       1.1     smb struct swwdog_softc {
     57  1.7.32.2    yamt 	device_t sc_dev;
     58       1.1     smb 	struct sysmon_wdog sc_smw;
     59       1.1     smb 	struct callout sc_c;
     60       1.1     smb 	int sc_wdog_armed;
     61  1.7.32.2    yamt };
     62  1.7.32.2    yamt 
     63  1.7.32.2    yamt void		swwdogattach(int);
     64       1.1     smb 
     65  1.7.32.2    yamt static int	swwdog_match(device_t, cfdata_t, void *);
     66  1.7.32.2    yamt static void	swwdog_attach(device_t, device_t, void *);
     67  1.7.32.2    yamt static int	swwdog_detach(device_t, int);
     68  1.7.32.2    yamt static bool	swwdog_suspend(device_t, const pmf_qual_t *);
     69       1.1     smb 
     70       1.1     smb static int swwdog_setmode(struct sysmon_wdog *);
     71       1.1     smb static int swwdog_tickle(struct sysmon_wdog *);
     72       1.1     smb 
     73       1.1     smb static int swwdog_arm(struct swwdog_softc *);
     74       1.1     smb static int swwdog_disarm(struct swwdog_softc *);
     75       1.1     smb 
     76       1.1     smb static void swwdog_panic(void *);
     77       1.1     smb 
     78  1.7.32.2    yamt bool swwdog_reboot = false;		/* set for panic instead of reboot */
     79       1.1     smb 
     80       1.1     smb #define	SWDOG_DEFAULT	60		/* 60-second default period */
     81       1.1     smb 
     82  1.7.32.2    yamt CFATTACH_DECL_NEW(swwdog, sizeof(struct swwdog_softc),
     83  1.7.32.2    yamt 	swwdog_match, swwdog_attach, swwdog_detach, NULL);
     84  1.7.32.2    yamt 
     85  1.7.32.2    yamt static void swwdog_sysctl_setup(void);
     86  1.7.32.2    yamt static struct sysctllog *swwdog_sysctllog;
     87  1.7.32.2    yamt 
     88       1.1     smb void
     89  1.7.32.2    yamt swwdogattach(int n __unused)
     90       1.1     smb {
     91  1.7.32.2    yamt 	int err;
     92  1.7.32.2    yamt 	static struct cfdata cf;
     93  1.7.32.2    yamt 
     94  1.7.32.2    yamt 	err = config_cfattach_attach(swwdog_cd.cd_name, &swwdog_ca);
     95  1.7.32.2    yamt 	if (err) {
     96  1.7.32.2    yamt 		aprint_error("%s: couldn't register cfattach: %d\n",
     97  1.7.32.2    yamt 		    swwdog_cd.cd_name, err);
     98  1.7.32.2    yamt 		config_cfdriver_detach(&swwdog_cd);
     99  1.7.32.2    yamt 		return;
    100  1.7.32.2    yamt 	}
    101       1.1     smb 
    102  1.7.32.2    yamt 	cf.cf_name = swwdog_cd.cd_name;
    103  1.7.32.2    yamt 	cf.cf_atname = swwdog_cd.cd_name;
    104  1.7.32.2    yamt 	cf.cf_unit = 0;
    105  1.7.32.2    yamt 	cf.cf_fstate = FSTATE_STAR;
    106       1.1     smb 
    107  1.7.32.2    yamt 	(void)config_attach_pseudo(&cf);
    108       1.1     smb 
    109  1.7.32.2    yamt 	return;
    110  1.7.32.2    yamt }
    111  1.7.32.2    yamt 
    112  1.7.32.2    yamt static int
    113  1.7.32.2    yamt swwdog_match(device_t parent, cfdata_t data, void *aux)
    114  1.7.32.2    yamt {
    115  1.7.32.2    yamt 	return 1;
    116  1.7.32.2    yamt }
    117  1.7.32.2    yamt 
    118  1.7.32.2    yamt static void
    119  1.7.32.2    yamt swwdog_attach(device_t parent, device_t self, void *aux)
    120  1.7.32.2    yamt {
    121  1.7.32.2    yamt 	struct swwdog_softc *sc = device_private(self);
    122  1.7.32.2    yamt 
    123  1.7.32.2    yamt 	sc->sc_dev = self;
    124  1.7.32.2    yamt 	sc->sc_smw.smw_name = device_xname(self);
    125  1.7.32.2    yamt 	sc->sc_smw.smw_cookie = sc;
    126  1.7.32.2    yamt 	sc->sc_smw.smw_setmode = swwdog_setmode;
    127  1.7.32.2    yamt 	sc->sc_smw.smw_tickle = swwdog_tickle;
    128  1.7.32.2    yamt 	sc->sc_smw.smw_period = SWDOG_DEFAULT;
    129  1.7.32.2    yamt 	callout_init(&sc->sc_c, 0);
    130  1.7.32.2    yamt 	callout_setfunc(&sc->sc_c, swwdog_panic, sc);
    131  1.7.32.2    yamt 
    132  1.7.32.2    yamt 	if (sysmon_wdog_register(&sc->sc_smw) == 0)
    133  1.7.32.2    yamt 		aprint_normal_dev(self, "software watchdog initialized\n");
    134  1.7.32.2    yamt 	else
    135  1.7.32.2    yamt 		aprint_error_dev(self, "unable to register software "
    136  1.7.32.2    yamt 		    "watchdog with sysmon\n");
    137  1.7.32.2    yamt 
    138  1.7.32.2    yamt 	if (!pmf_device_register(self, swwdog_suspend, NULL))
    139  1.7.32.2    yamt 		aprint_error_dev(self, "couldn't establish power handler\n");
    140  1.7.32.2    yamt 
    141  1.7.32.2    yamt 	swwdog_sysctl_setup();
    142  1.7.32.2    yamt }
    143  1.7.32.2    yamt 
    144  1.7.32.2    yamt static int
    145  1.7.32.2    yamt swwdog_detach(device_t self, int flags)
    146  1.7.32.2    yamt {
    147  1.7.32.2    yamt 	struct swwdog_softc *sc = device_private(self);
    148  1.7.32.2    yamt 
    149  1.7.32.2    yamt 	swwdog_disarm(sc);
    150  1.7.32.2    yamt 	callout_destroy(&sc->sc_c);
    151  1.7.32.2    yamt 	sysctl_teardown(&swwdog_sysctllog);
    152  1.7.32.2    yamt 
    153  1.7.32.2    yamt 	return 1;
    154  1.7.32.2    yamt }
    155  1.7.32.2    yamt 
    156  1.7.32.2    yamt static bool
    157  1.7.32.2    yamt swwdog_suspend(device_t dev, const pmf_qual_t *qual)
    158  1.7.32.2    yamt {
    159  1.7.32.2    yamt 	struct swwdog_softc *sc = device_private(dev);
    160  1.7.32.2    yamt 
    161  1.7.32.2    yamt 	/* Don't allow suspend if watchdog is armed */
    162  1.7.32.2    yamt 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
    163  1.7.32.2    yamt 		return false;
    164  1.7.32.2    yamt 	return true;
    165       1.1     smb }
    166       1.1     smb 
    167       1.1     smb static int
    168       1.1     smb swwdog_setmode(struct sysmon_wdog *smw)
    169       1.1     smb {
    170       1.2   perry 	struct swwdog_softc *sc = smw->smw_cookie;
    171       1.1     smb 	int error = 0;
    172       1.1     smb 
    173       1.1     smb 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    174       1.1     smb 		error = swwdog_disarm(sc);
    175       1.1     smb 	} else {
    176       1.1     smb 		if (smw->smw_period == 0)
    177       1.1     smb 			return EINVAL;
    178       1.1     smb 		else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    179       1.1     smb 			sc->sc_smw.smw_period = SWDOG_DEFAULT;
    180       1.3  simonb 		else
    181       1.3  simonb 			sc->sc_smw.smw_period = smw->smw_period;
    182       1.1     smb 		error = swwdog_arm(sc);
    183       1.1     smb 	}
    184       1.1     smb 	return error;
    185       1.1     smb }
    186       1.1     smb 
    187       1.1     smb static int
    188       1.1     smb swwdog_tickle(struct sysmon_wdog *smw)
    189       1.1     smb {
    190       1.1     smb 
    191       1.1     smb 	return swwdog_arm(smw->smw_cookie);
    192       1.1     smb }
    193       1.1     smb 
    194       1.1     smb static int
    195       1.1     smb swwdog_arm(struct swwdog_softc *sc)
    196       1.1     smb {
    197       1.1     smb 
    198       1.1     smb 	callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
    199       1.1     smb 	return 0;
    200       1.1     smb }
    201       1.1     smb 
    202       1.1     smb static int
    203       1.1     smb swwdog_disarm(struct swwdog_softc *sc)
    204       1.1     smb {
    205       1.1     smb 
    206       1.1     smb 	callout_stop(&sc->sc_c);
    207       1.1     smb 	return 0;
    208       1.1     smb }
    209       1.1     smb 
    210       1.1     smb static void
    211       1.2   perry swwdog_panic(void *vsc)
    212       1.1     smb {
    213       1.1     smb 	struct swwdog_softc *sc = vsc;
    214  1.7.32.2    yamt 	bool do_panic;
    215       1.1     smb 
    216  1.7.32.2    yamt 	do_panic = !swwdog_reboot;
    217  1.7.32.2    yamt 	swwdog_reboot = false;
    218       1.1     smb 	callout_schedule(&sc->sc_c, 60 * hz);	/* deliberate double-panic */
    219       1.1     smb 
    220  1.7.32.2    yamt 	printf("%s: %d second timer expired\n", device_xname(sc->sc_dev),
    221       1.1     smb 	    sc->sc_smw.smw_period);
    222       1.1     smb 
    223       1.1     smb 	if (do_panic)
    224       1.1     smb 		panic("watchdog timer expired");
    225       1.3  simonb 	else
    226       1.3  simonb 		cpu_reboot(0, NULL);
    227       1.1     smb }
    228  1.7.32.2    yamt 
    229  1.7.32.2    yamt static void
    230  1.7.32.2    yamt swwdog_sysctl_setup(void)
    231  1.7.32.2    yamt {
    232  1.7.32.2    yamt 	const struct sysctlnode *me;
    233  1.7.32.2    yamt 
    234  1.7.32.2    yamt 	KASSERT(swwdog_sysctllog == NULL);
    235  1.7.32.2    yamt 
    236  1.7.32.2    yamt 	sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE,
    237  1.7.32.2    yamt 	    CTLTYPE_NODE, "swwdog", NULL,
    238  1.7.32.2    yamt 	    NULL, 0, NULL, 0,
    239  1.7.32.2    yamt 	    CTL_HW, CTL_CREATE, CTL_EOL);
    240  1.7.32.2    yamt 	sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE,
    241  1.7.32.2    yamt 	    CTLTYPE_BOOL, "reboot", "reboot if timer expires",
    242  1.7.32.2    yamt 	    NULL, 0, &swwdog_reboot, sizeof(bool),
    243  1.7.32.2    yamt 	    CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
    244  1.7.32.2    yamt }
    245