ntwdt.c revision d3d50737e566cade9a08d73d2af95105ac7cd960
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * CDDL HEADER START
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The contents of this file are subject to the terms of the
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Common Development and Distribution License (the "License").
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * You may not use this file except in compliance with the License.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * See the License for the specific language governing permissions
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * and limitations under the License.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * When distributing Covered Code, include this CDDL HEADER in each
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * If applicable, add the following below this CDDL HEADER, with the
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * fields enclosed by brackets "[]" replaced with your own identifying
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * information: Portions Copyright [yyyy] [name of copyright owner]
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * CDDL HEADER END
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Use is subject to license terms.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * sun4v application watchdog driver
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoyint ntwdt_debug = 0; /* ntwdt debug flag, dbg all for now. */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Flags to set in ntwdt_debug.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#define NTWDT_DBG_ENTRY 0x00000001 /* drv entry points */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#define NTWDT_DBG_NTWDT 0x00000004 /* other ntwdt debug */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#else /* DEBUG */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#endif /* DEBUG */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ((ntwdt_state_t *)ddi_get_soft_state(ntwdt_statep, (minor)))
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The ntwdt cyclic interval in nanosecond unit as cyclic subsystem supports
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * nanosecond resolution.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#define NTWDT_CYCLIC_INTERVAL NANOSEC /* 1 seconds */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The ntwdt decrement interval in 1 second resolution.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * ntwdt_watchdog_flags and macros to set/clear one bit in it.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#define NTWDT_FLAG_SKIP_CYCLIC 0x1 /* skip next cyclic */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy#define NTWDT_MAX_TIMEOUT (3 * 60 * 60) /* 3 hours */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Application watchdog state.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoytypedef struct ntwdt_runstate {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy uint32_t ntwdt_time_remaining; /* expiration timer */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy uint32_t ntwdt_watchdog_timeout; /* timeout in seconds */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy hrtime_t ntwdt_cyclic_interval; /* cyclic interval */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * softstate of NTWDT
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoytypedef struct {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic int ntwdt_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic int ntwdt_attach(dev_info_t *, ddi_attach_cmd_t);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic int ntwdt_detach(dev_info_t *, ddi_detach_cmd_t);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic int ntwdt_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic void ntwdt_arm_watchdog(ntwdt_runstate_t *ntwdt_state);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic void ntwdt_cyclic_pat(void);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoystatic void ntwdt_start_timer(ntwdt_state_t *ntwdt_ptr);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy 0, /* devo_refcnt */
193974072f41a843678abf5f61979c748687e66bSherry Moore "Application Watchdog Driver",
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* Initialize the soft state structures */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* Install the loadable module */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state_t *ntwdt_ptr = NULL; /* pointer to ntwdt_runstatep */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (ddi_soft_state_zalloc(ntwdt_statep, instance) != DDI_SUCCESS) {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_ptr = ddi_get_soft_state(ntwdt_statep, instance);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Initialize the watchdog structure
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy &ntwdt_runstatep->ntwdt_runstate_mtx_cookie) != DDI_SUCCESS) {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy "for ntwdt_runstate_mutex");
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy (void *)ntwdt_runstatep->ntwdt_runstate_mtx_cookie);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* Cyclic fires once per second: */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_runstatep->ntwdt_cyclic_interval = NTWDT_CYCLIC_INTERVAL;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* init the Cyclic that drives the NTWDT */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* Softint that will be triggered by Cyclic that drives NTWDT */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &ntwdt_cyclic_softint_id,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NULL, NULL, ntwdt_cyclic_softint, (caddr_t)ntwdt_ptr)
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Create Minor Node as last activity. This prevents
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * application from accessing our implementation until it
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * is initialized.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (ddi_create_minor_node(dip, NTWDT_MINOR_NODE, S_IFCHR, 0,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* Display our driver info in the banner */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy mutex_destroy(&ntwdt_runstatep->ntwdt_runstate_mutex);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /* clean up the driver stuff here */
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy kmem_free(ntwdt_runstatep, sizeof (ntwdt_runstate_t));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy/*ARGSUSED*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoyntwdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy/*ARGSUSED*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_ptr = ddi_get_soft_state(ntwdt_statep, instance);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * release resources in opposite (LIFO) order as
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * were allocated in attach.
193974072f41a843678abf5f61979c748687e66bSherry Moore &ntwdt_ptr->ntwdt_run_state->ntwdt_runstate_mutex);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy/*ARGSUSED*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoyntwdt_open(dev_t *devp, int flag, int otyp, cred_t *credp)
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * ensure caller is a priviledged process.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy/*ARGSUSED*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoyntwdt_close(dev_t dev, int flag, int otyp, cred_t *credp)
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy return (0);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy/*ARGSUSED*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoyntwdt_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy lom_dogstate.reset_enable = ntwdt_state->ntwdt_reset_enabled;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy lom_dogstate.dog_enable = ntwdt_state->ntwdt_watchdog_enabled;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy lom_dogstate.dog_timeout = ntwdt_state->ntwdt_watchdog_timeout;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (ddi_copyout((caddr_t)&lom_dogstate, (caddr_t)arg,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_IOCTL, ("reset_enable: %d, and dog_enable: "
193974072f41a843678abf5f61979c748687e66bSherry Moore "%d, watchdog_timeout %d", lom_dogctl.reset_enable,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * ignore request to enable reset while disabling watchdog.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if (!lom_dogctl.dog_enable && lom_dogctl.reset_enable) {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_IOCTL, ("invalid combination of "
193974072f41a843678abf5f61979c748687e66bSherry Moore "reset_enable: %d, and dog_enable: %d",
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * the LOMIOCDOGTIME has never been used to setup
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * a valid timeout.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_IOCTL, ("timeout has not been set"
193974072f41a843678abf5f61979c748687e66bSherry Moore "watchdog_timeout: %d",
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Store the user specified state in the softstate.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_reset_enabled = lom_dogctl.reset_enable;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_watchdog_enabled = lom_dogctl.dog_enable;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The user wants to enable the watchdog.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Arm the watchdog and start the cyclic.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The user wants to disable the watchdog.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Ensure specified timeout is valid.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if ((lom_dogtime == 0) ||
193974072f41a843678abf5f61979c748687e66bSherry Moore "timeout: %d", (int)TICK_TO_MSEC(lom_dogtime)));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * If awdt is currently running, re-arm it with the
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * newly-specified timeout value.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Allow user to pat the watchdog timer.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * If awdt is not enabled or underlying cyclic is not
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * running, exit.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_IOCTL, ("PAT: AWDT not enabled"));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * re-arm the awdt.
193974072f41a843678abf5f61979c748687e66bSherry Moore "remainning seconds: %d",
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy /*LINTED E_BAD_PTR_CAST_ALIGN*/
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy if ((ntwdt_state->ntwdt_watchdog_flags & NTWDT_FLAG_SKIP_CYCLIC) != 0) {
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_watchdog_flags &= ~NTWDT_FLAG_SKIP_CYCLIC;
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni "ddi_get_lbolt64(): %d\n", ntwdt_state->ntwdt_watchdog_timeout,
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Decrement the virtual watchdog timer and check if it has expired.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_time_remaining -= NTWDT_DECREMENT_INTERVAL;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * The user wants to reset the system.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("recovery being done"));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("no recovery being done"));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Schedule Callout to stop the cyclic.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy (void) timeout(ntwdt_stop_timer_lock, ntwdt_ptr, 0);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("time remaining in AWDT: %d secs",
193974072f41a843678abf5f61979c748687e66bSherry Moore (int)TICK_TO_MSEC(ntwdt_state->ntwdt_time_remaining)));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_time_remaining = ntwdt_state->ntwdt_watchdog_timeout;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_watchdog_flags |= NTWDT_FLAG_SKIP_CYCLIC;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_state->ntwdt_watchdog_flags &= ~NTWDT_FLAG_SKIP_CYCLIC;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_runstate_t *ntwdt_state = ntwdt_ptr->ntwdt_run_state;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy cyc_handler_t *hdlr = &ntwdt_state->ntwdt_cycl_hdlr;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * Init the cyclic.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy when->cyt_interval = ntwdt_state->ntwdt_cyclic_interval;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("cyclic-driven timer is started"));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy ntwdt_runstate_t *ntwdt_state = ntwdt_ptr->ntwdt_run_state;
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("cyclic-driven timer is stopped"));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * This is a wrapper function for ntwdt_stop_timer as some callers
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy * will already have the appropriate mutex locked, and others not.
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy mutex_enter(&ntwdt_ptr->ntwdt_run_state->ntwdt_runstate_mutex);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy mutex_exit(&ntwdt_ptr->ntwdt_run_state->ntwdt_runstate_mutex);
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy cmn_err(CE_NOTE, "Appication watchdog timer expired, "
193974072f41a843678abf5f61979c748687e66bSherry Moore "taking no action");
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy NTWDT_DBG(NTWDT_DBG_NTWDT, ("dump cores and rebooting ..."));
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy cmn_err(CE_PANIC, "kadmin(A_DUMP, AD_BOOT) failed");
927a453e165c072d45bd6aa2945b3db0fce17c56wentaoy "application watchdog is not available.");