swwdog.c revision 1.2 1 1.2 perry /* $NetBSD: swwdog.c,v 1.2 2005/02/27 00:27:49 perry 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.2 perry __KERNEL_RCSID(0, "$NetBSD: swwdog.c,v 1.2 2005/02/27 00:27:49 perry 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.1 smb #include <sys/cdefs.h>
46 1.2 perry #include <sys/device.h>
47 1.2 perry #include <sys/kernel.h>
48 1.1 smb #include <sys/reboot.h>
49 1.1 smb #include <sys/systm.h>
50 1.1 smb #include <sys/wdog.h>
51 1.1 smb #include <dev/sysmon/sysmonvar.h>
52 1.1 smb
53 1.1 smb #include "swwdog.h"
54 1.1 smb
55 1.1 smb struct swwdog_softc {
56 1.1 smb struct sysmon_wdog sc_smw;
57 1.1 smb struct callout sc_c;
58 1.1 smb char sc_name[20];
59 1.1 smb int sc_wdog_armed;
60 1.1 smb } sc_wdog[NSWWDOG];
61 1.1 smb
62 1.1 smb void swwdogattach(int);
63 1.1 smb
64 1.1 smb static int swwdog_setmode(struct sysmon_wdog *);
65 1.1 smb static int swwdog_tickle(struct sysmon_wdog *);
66 1.1 smb
67 1.1 smb static int swwdog_arm(struct swwdog_softc *);
68 1.1 smb static int swwdog_disarm(struct swwdog_softc *);
69 1.1 smb
70 1.1 smb static void swwdog_panic(void *);
71 1.1 smb
72 1.1 smb int swwdog_reboot = 0; /* set for panic instead of reboot */
73 1.1 smb
74 1.1 smb #define SWDOG_DEFAULT 60 /* 60-second default period */
75 1.1 smb
76 1.1 smb void
77 1.1 smb swwdogattach(int count)
78 1.1 smb {
79 1.1 smb int i;
80 1.1 smb
81 1.1 smb for (i = 0; i < NSWWDOG; i++) {
82 1.1 smb struct swwdog_softc *sc = &sc_wdog[i];
83 1.1 smb
84 1.1 smb snprintf(sc->sc_name, sizeof sc->sc_name, "swwdog%d", i);
85 1.1 smb printf("%s: ", sc->sc_name);
86 1.1 smb sc->sc_smw.smw_name = sc->sc_name;
87 1.1 smb sc->sc_smw.smw_cookie = sc;
88 1.1 smb sc->sc_smw.smw_setmode = swwdog_setmode;
89 1.1 smb sc->sc_smw.smw_tickle = swwdog_tickle;
90 1.1 smb sc->sc_smw.smw_period = SWDOG_DEFAULT;
91 1.1 smb callout_init(&sc->sc_c);
92 1.1 smb callout_setfunc(&sc->sc_c, swwdog_panic, sc);
93 1.1 smb
94 1.1 smb if (sysmon_wdog_register(&sc->sc_smw) == 0)
95 1.1 smb printf("software watchdog initialized\n");
96 1.1 smb else printf("unable to register software watchdog "
97 1.1 smb "with sysmon\n");
98 1.1 smb }
99 1.1 smb }
100 1.1 smb
101 1.1 smb static int
102 1.1 smb swwdog_setmode(struct sysmon_wdog *smw)
103 1.1 smb {
104 1.2 perry struct swwdog_softc *sc = smw->smw_cookie;
105 1.1 smb int error = 0;
106 1.1 smb
107 1.1 smb if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
108 1.1 smb error = swwdog_disarm(sc);
109 1.1 smb } else {
110 1.1 smb if (smw->smw_period == 0)
111 1.1 smb return EINVAL;
112 1.1 smb else if (smw->smw_period == WDOG_PERIOD_DEFAULT)
113 1.1 smb sc->sc_smw.smw_period = SWDOG_DEFAULT;
114 1.1 smb else sc->sc_smw.smw_period = smw->smw_period;
115 1.1 smb error = swwdog_arm(sc);
116 1.1 smb }
117 1.1 smb return error;
118 1.1 smb }
119 1.1 smb
120 1.1 smb static int
121 1.1 smb swwdog_tickle(struct sysmon_wdog *smw)
122 1.1 smb {
123 1.1 smb
124 1.1 smb return swwdog_arm(smw->smw_cookie);
125 1.1 smb }
126 1.1 smb
127 1.1 smb static int
128 1.1 smb swwdog_arm(struct swwdog_softc *sc)
129 1.1 smb {
130 1.1 smb
131 1.1 smb callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz);
132 1.1 smb return 0;
133 1.1 smb }
134 1.1 smb
135 1.1 smb static int
136 1.1 smb swwdog_disarm(struct swwdog_softc *sc)
137 1.1 smb {
138 1.1 smb
139 1.1 smb callout_stop(&sc->sc_c);
140 1.1 smb return 0;
141 1.1 smb }
142 1.1 smb
143 1.1 smb static void
144 1.2 perry swwdog_panic(void *vsc)
145 1.1 smb {
146 1.1 smb struct swwdog_softc *sc = vsc;
147 1.1 smb int do_panic;
148 1.1 smb
149 1.1 smb do_panic = swwdog_reboot;
150 1.1 smb swwdog_reboot = 1;
151 1.1 smb callout_schedule(&sc->sc_c, 60 * hz); /* deliberate double-panic */
152 1.1 smb
153 1.1 smb printf("%s: %d second timer expired", sc->sc_name,
154 1.1 smb sc->sc_smw.smw_period);
155 1.1 smb
156 1.1 smb if (do_panic)
157 1.1 smb panic("watchdog timer expired");
158 1.1 smb else cpu_reboot(0, NULL);
159 1.1 smb }
160