1 /* $NetBSD: swwdog.c,v 1.19 2015/05/12 10:20:14 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.19 2015/05/12 10:20:14 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 if (workqueue_create(&wq, "swwreboot", doreboot, NULL, 164 PRI_NONE, IPL_NONE, 0) != 0) { 165 aprint_error_dev(self, "failed to create reboot workqueue"); 166 } 167 168 sc->sc_dev = self; 169 sc->sc_smw.smw_name = device_xname(self); 170 sc->sc_smw.smw_cookie = sc; 171 sc->sc_smw.smw_setmode = swwdog_setmode; 172 sc->sc_smw.smw_tickle = swwdog_tickle; 173 sc->sc_smw.smw_period = SWDOG_DEFAULT; 174 175 callout_init(&sc->sc_c, 0); 176 callout_setfunc(&sc->sc_c, swwdog_panic, sc); 177 178 if (sysmon_wdog_register(&sc->sc_smw) == 0) 179 aprint_normal_dev(self, "software watchdog initialized\n"); 180 else { 181 aprint_error_dev(self, "unable to register software " 182 "watchdog with sysmon\n"); 183 callout_destroy(&sc->sc_c); 184 workqueue_destroy(wq); 185 return; 186 } 187 188 if (!pmf_device_register(self, swwdog_suspend, NULL)) 189 aprint_error_dev(self, "couldn't establish power handler\n"); 190 191 swwdog_sysctl_setup(); 192 } 193 194 static int 195 swwdog_detach(device_t self, int flags) 196 { 197 struct swwdog_softc *sc = device_private(self); 198 199 pmf_device_deregister(self); 200 swwdog_disarm(sc); 201 sysctl_teardown(&swwdog_sysctllog); 202 sysmon_wdog_unregister(&sc->sc_smw); 203 callout_destroy(&sc->sc_c); 204 workqueue_destroy(wq); 205 206 return 0; 207 } 208 209 static bool 210 swwdog_suspend(device_t dev, const pmf_qual_t *qual) 211 { 212 struct swwdog_softc *sc = device_private(dev); 213 214 /* Don't allow suspend if watchdog is armed */ 215 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED) 216 return false; 217 return true; 218 } 219 220 static int 221 swwdog_setmode(struct sysmon_wdog *smw) 222 { 223 struct swwdog_softc *sc = smw->smw_cookie; 224 int error = 0; 225 226 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 227 error = swwdog_disarm(sc); 228 } else { 229 if (smw->smw_period == 0) 230 return EINVAL; 231 else if (smw->smw_period == WDOG_PERIOD_DEFAULT) 232 sc->sc_smw.smw_period = SWDOG_DEFAULT; 233 else 234 sc->sc_smw.smw_period = smw->smw_period; 235 error = swwdog_arm(sc); 236 } 237 return error; 238 } 239 240 static int 241 swwdog_tickle(struct sysmon_wdog *smw) 242 { 243 244 return swwdog_arm(smw->smw_cookie); 245 } 246 247 static int 248 swwdog_arm(struct swwdog_softc *sc) 249 { 250 251 callout_schedule(&sc->sc_c, sc->sc_smw.smw_period * hz); 252 return 0; 253 } 254 255 static int 256 swwdog_disarm(struct swwdog_softc *sc) 257 { 258 259 callout_stop(&sc->sc_c); 260 return 0; 261 } 262 263 static void 264 swwdog_panic(void *vsc) 265 { 266 struct swwdog_softc *sc = vsc; 267 static struct work wk; /* we'll need it max once */ 268 bool do_panic; 269 270 do_panic = !swwdog_reboot; 271 swwdog_reboot = false; 272 callout_schedule(&sc->sc_c, 60 * hz); /* deliberate double-panic */ 273 274 printf("%s: %d second timer expired\n", "swwdog", 275 sc->sc_smw.smw_period); 276 277 if (do_panic) 278 panic("watchdog timer expired"); 279 else 280 workqueue_enqueue(wq, &wk, NULL); 281 } 282 283 static void 284 swwdog_sysctl_setup(void) 285 { 286 const struct sysctlnode *me; 287 288 KASSERT(swwdog_sysctllog == NULL); 289 290 sysctl_createv(&swwdog_sysctllog, 0, NULL, &me, CTLFLAG_READWRITE, 291 CTLTYPE_NODE, "swwdog", NULL, 292 NULL, 0, NULL, 0, 293 CTL_HW, CTL_CREATE, CTL_EOL); 294 sysctl_createv(&swwdog_sysctllog, 0, NULL, NULL, CTLFLAG_READWRITE, 295 CTLTYPE_BOOL, "reboot", "reboot if timer expires", 296 NULL, 0, &swwdog_reboot, sizeof(bool), 297 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL); 298 } 299 300 /* 301 * Module management 302 */ 303 304 static 305 int 306 swwdog_init(void *arg) 307 { 308 /* 309 * Merge the driver info into the kernel tables and attach the 310 * pseudo-device 311 */ 312 int error = 0; 313 314 315 #ifdef _MODULE 316 error = config_cfdriver_attach(&swwdog_cd); 317 if (error) { 318 aprint_error("%s: unable to attach cfdriver\n", 319 swwdog_cd.cd_name); 320 return error; 321 } 322 error = swwdogattach(1); 323 if (error) { 324 aprint_error("%s: device attach failed\n", swwdog_cd.cd_name); 325 config_cfdriver_detach(&swwdog_cd); 326 } 327 #endif 328 329 return error; 330 } 331 332 static 333 int 334 swwdog_fini(void *arg) 335 { 336 int error; 337 338 error = config_detach(swwdog_dev, 0); 339 340 #ifdef _MODULE 341 error = config_cfattach_detach(swwdog_cd.cd_name, &swwdog_ca); 342 if (error) 343 aprint_error("%s: error detaching cfattach: %d\n", 344 swwdog_cd.cd_name, error); 345 346 error = config_cfdriver_detach(&swwdog_cd); 347 if (error) 348 aprint_error("%s: error detaching cfdriver: %d\n", 349 swwdog_cd.cd_name, error); 350 #endif 351 352 return error; 353 } 354 355 static 356 int 357 swwdog_modcmd(modcmd_t cmd, void *arg) 358 { 359 int ret; 360 361 switch (cmd) { 362 case MODULE_CMD_INIT: 363 ret = swwdog_init(arg); 364 break; 365 366 case MODULE_CMD_FINI: 367 ret = swwdog_fini(arg); 368 break; 369 370 case MODULE_CMD_STAT: 371 default: 372 ret = ENOTTY; 373 } 374 375 return ret; 376 } 377 378