10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * Excalibur fans watchdog module
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/mkdev.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/ksynch.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/cred.h>
440Sstevel@tonic-gate #include <sys/xcalwd.h>
450Sstevel@tonic-gate #include <sys/policy.h>
460Sstevel@tonic-gate #include <sys/platform_module.h>
470Sstevel@tonic-gate
480Sstevel@tonic-gate extern struct mod_ops mod_driverops;
490Sstevel@tonic-gate
500Sstevel@tonic-gate #define MINOR_DEVICE_NAME "xcalwd"
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * Define your per instance state data
540Sstevel@tonic-gate */
550Sstevel@tonic-gate typedef struct xcalwd_state {
560Sstevel@tonic-gate kmutex_t lock;
570Sstevel@tonic-gate boolean_t started;
580Sstevel@tonic-gate int intvl;
590Sstevel@tonic-gate timeout_id_t tid;
600Sstevel@tonic-gate dev_info_t *dip;
610Sstevel@tonic-gate } xcalwd_state_t;
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Pointer to soft states
650Sstevel@tonic-gate */
660Sstevel@tonic-gate static void *xcalwd_statep;
670Sstevel@tonic-gate
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate * dev_ops
700Sstevel@tonic-gate */
710Sstevel@tonic-gate static int xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
720Sstevel@tonic-gate void *arg, void **resultp);
730Sstevel@tonic-gate static int xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
740Sstevel@tonic-gate static int xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * cb_ops
780Sstevel@tonic-gate */
790Sstevel@tonic-gate static int xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp);
800Sstevel@tonic-gate static int xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp);
810Sstevel@tonic-gate static int xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
820Sstevel@tonic-gate cred_t *credp, int *rvalp);
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate * timeout handler
850Sstevel@tonic-gate */
860Sstevel@tonic-gate static void xcalwd_timeout(void *arg);
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * cb_ops
900Sstevel@tonic-gate */
910Sstevel@tonic-gate static struct cb_ops xcalwd_cb_ops = {
920Sstevel@tonic-gate xcalwd_open, /* open */
930Sstevel@tonic-gate xcalwd_close, /* close */
940Sstevel@tonic-gate nodev, /* strategy */
950Sstevel@tonic-gate nodev, /* print */
960Sstevel@tonic-gate nodev, /* dump */
970Sstevel@tonic-gate nodev, /* read */
980Sstevel@tonic-gate nodev, /* write */
990Sstevel@tonic-gate xcalwd_ioctl, /* ioctl */
1000Sstevel@tonic-gate nodev, /* devmap */
1010Sstevel@tonic-gate nodev, /* mmap */
1020Sstevel@tonic-gate nodev, /* segmap */
1030Sstevel@tonic-gate nochpoll, /* chpoll */
1040Sstevel@tonic-gate ddi_prop_op, /* prop_op */
1050Sstevel@tonic-gate NULL, /* streamtab */
1060Sstevel@tonic-gate D_NEW | D_MP | D_64BIT, /* cb_flag */
1070Sstevel@tonic-gate CB_REV, /* rev */
1080Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
1090Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * dev_ops
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate static struct dev_ops xcalwd_dev_ops = {
1160Sstevel@tonic-gate DEVO_REV, /* devo_rev */
1170Sstevel@tonic-gate 0, /* devo_refcnt */
118*7656SSherry.Moore@Sun.COM xcalwd_getinfo, /* getinfo */
119*7656SSherry.Moore@Sun.COM nulldev, /* identify */
1200Sstevel@tonic-gate nulldev, /* probe */
121*7656SSherry.Moore@Sun.COM xcalwd_attach, /* attach */
122*7656SSherry.Moore@Sun.COM xcalwd_detach, /* detach */
1230Sstevel@tonic-gate nodev, /* devo_reset */
124*7656SSherry.Moore@Sun.COM &xcalwd_cb_ops, /* devo_cb_ops */
1250Sstevel@tonic-gate NULL, /* devo_bus_ops */
126*7656SSherry.Moore@Sun.COM NULL, /* devo_power */
127*7656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * modldrv
1320Sstevel@tonic-gate */
1330Sstevel@tonic-gate static struct modldrv xcalwd_modldrv = {
1340Sstevel@tonic-gate &mod_driverops, /* drv_modops */
135*7656SSherry.Moore@Sun.COM "Excalibur watchdog timer v1.7 ", /* drv_linkinfo */
1360Sstevel@tonic-gate &xcalwd_dev_ops /* drv_dev_ops */
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * modlinkage
1410Sstevel@tonic-gate */
1420Sstevel@tonic-gate static struct modlinkage xcalwd_modlinkage = {
1430Sstevel@tonic-gate MODREV_1,
1440Sstevel@tonic-gate &xcalwd_modldrv,
1450Sstevel@tonic-gate NULL
1460Sstevel@tonic-gate };
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate int
_init(void)1490Sstevel@tonic-gate _init(void)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate int error;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Initialize the module state structure
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate error = ddi_soft_state_init(&xcalwd_statep,
1570Sstevel@tonic-gate sizeof (xcalwd_state_t), 0);
1580Sstevel@tonic-gate if (error) {
1590Sstevel@tonic-gate return (error);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * Link the driver into the system
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate error = mod_install(&xcalwd_modlinkage);
1660Sstevel@tonic-gate if (error) {
1670Sstevel@tonic-gate ddi_soft_state_fini(&xcalwd_statep);
1680Sstevel@tonic-gate return (error);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate return (0);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate int
_fini(void)1740Sstevel@tonic-gate _fini(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate int error;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate error = mod_remove(&xcalwd_modlinkage);
1790Sstevel@tonic-gate if (error != 0) {
1800Sstevel@tonic-gate return (error);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * Cleanup resources allocated in _init
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate ddi_soft_state_fini(&xcalwd_statep);
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1910Sstevel@tonic-gate _info(struct modinfo *modinfop)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate return (mod_info(&xcalwd_modlinkage, modinfop));
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*ARGSUSED*/
1970Sstevel@tonic-gate static int
xcalwd_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** resultp)1980Sstevel@tonic-gate xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
1990Sstevel@tonic-gate void *arg, void **resultp)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate int retval;
2020Sstevel@tonic-gate dev_t dev = (dev_t)arg;
2030Sstevel@tonic-gate int instance;
2040Sstevel@tonic-gate xcalwd_state_t *tsp;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate retval = DDI_FAILURE;
2070Sstevel@tonic-gate switch (cmd) {
2080Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2090Sstevel@tonic-gate instance = getminor(dev);
2100Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
2110Sstevel@tonic-gate if (tsp == NULL)
2120Sstevel@tonic-gate *resultp = NULL;
2130Sstevel@tonic-gate else {
2140Sstevel@tonic-gate *resultp = tsp->dip;
2150Sstevel@tonic-gate retval = DDI_SUCCESS;
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate break;
2180Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
219796Smathue *resultp = (void *)(uintptr_t)getminor(dev);
2200Sstevel@tonic-gate retval = DDI_SUCCESS;
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate default:
2230Sstevel@tonic-gate break;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate return (retval);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate static int
xcalwd_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2290Sstevel@tonic-gate xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate int instance;
2320Sstevel@tonic-gate xcalwd_state_t *tsp;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate switch (cmd) {
2350Sstevel@tonic-gate case DDI_ATTACH:
2360Sstevel@tonic-gate instance = ddi_get_instance(dip);
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (&plat_fan_blast == NULL) {
2390Sstevel@tonic-gate cmn_err(CE_WARN, "missing plat_fan_blast function");
2400Sstevel@tonic-gate return (DDI_FAILURE);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate if (ddi_soft_state_zalloc(xcalwd_statep, instance) !=
2440Sstevel@tonic-gate DDI_SUCCESS) {
2450Sstevel@tonic-gate cmn_err(CE_WARN, "attach could not alloc"
246*7656SSherry.Moore@Sun.COM "%d state structure", instance);
2470Sstevel@tonic-gate return (DDI_FAILURE);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
2510Sstevel@tonic-gate if (tsp == NULL) {
2520Sstevel@tonic-gate cmn_err(CE_WARN, "get state failed %d",
253*7656SSherry.Moore@Sun.COM instance);
2540Sstevel@tonic-gate return (DDI_FAILURE);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (ddi_create_minor_node(dip, MINOR_DEVICE_NAME,
2580Sstevel@tonic-gate S_IFCHR, instance, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2590Sstevel@tonic-gate cmn_err(CE_WARN, "create minor node failed\n");
2600Sstevel@tonic-gate return (DDI_FAILURE);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate mutex_init(&tsp->lock, NULL, MUTEX_DRIVER, NULL);
2640Sstevel@tonic-gate tsp->started = B_FALSE;
2650Sstevel@tonic-gate tsp->intvl = 0;
2660Sstevel@tonic-gate tsp->tid = 0;
2670Sstevel@tonic-gate tsp->dip = dip;
2680Sstevel@tonic-gate ddi_report_dev(dip);
2690Sstevel@tonic-gate return (DDI_SUCCESS);
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate case DDI_RESUME:
2720Sstevel@tonic-gate return (DDI_SUCCESS);
2730Sstevel@tonic-gate default:
2740Sstevel@tonic-gate break;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate return (DDI_FAILURE);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate static int
xcalwd_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2800Sstevel@tonic-gate xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate xcalwd_state_t *tsp;
2830Sstevel@tonic-gate int instance;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate switch (cmd) {
2860Sstevel@tonic-gate case DDI_DETACH:
2870Sstevel@tonic-gate instance = ddi_get_instance(dip);
2880Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
2890Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
2900Sstevel@tonic-gate mutex_destroy(&tsp->lock);
2910Sstevel@tonic-gate ddi_soft_state_free(xcalwd_statep, instance);
2920Sstevel@tonic-gate return (DDI_SUCCESS);
2930Sstevel@tonic-gate case DDI_SUSPEND:
2940Sstevel@tonic-gate return (DDI_SUCCESS);
2950Sstevel@tonic-gate default:
2960Sstevel@tonic-gate break;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate return (DDI_FAILURE);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Watchdog timeout handler that calls plat_fan_blast to take
3030Sstevel@tonic-gate * the failsafe action.
3040Sstevel@tonic-gate */
3050Sstevel@tonic-gate static void
xcalwd_timeout(void * arg)3060Sstevel@tonic-gate xcalwd_timeout(void *arg)
3070Sstevel@tonic-gate {
308796Smathue int instance = (int)(uintptr_t)arg;
3090Sstevel@tonic-gate xcalwd_state_t *tsp;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (instance < 0)
3120Sstevel@tonic-gate return;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
3150Sstevel@tonic-gate if (tsp == NULL)
3160Sstevel@tonic-gate return;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate mutex_enter(&tsp->lock);
3190Sstevel@tonic-gate if (tsp->started == B_FALSE || tsp->tid == 0) {
3200Sstevel@tonic-gate tsp->tid = 0;
3210Sstevel@tonic-gate mutex_exit(&tsp->lock);
3220Sstevel@tonic-gate return;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate mutex_exit(&tsp->lock);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate plat_fan_blast();
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate /*ARGSUSED*/
3300Sstevel@tonic-gate static int
xcalwd_open(dev_t * devp,int flag,int otyp,cred_t * credp)3310Sstevel@tonic-gate xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate int instance;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate if (secpolicy_sys_config(credp, B_FALSE) != 0)
3360Sstevel@tonic-gate return (EPERM);
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate if (otyp != OTYP_CHR)
3390Sstevel@tonic-gate return (EINVAL);
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate instance = getminor(*devp);
3420Sstevel@tonic-gate if (instance < 0)
3430Sstevel@tonic-gate return (ENXIO);
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if (ddi_get_soft_state(xcalwd_statep, instance) == NULL) {
3460Sstevel@tonic-gate return (ENXIO);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate return (0);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /*ARGSUSED*/
3530Sstevel@tonic-gate static int
xcalwd_close(dev_t dev,int flag,int otyp,cred_t * credp)3540Sstevel@tonic-gate xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate xcalwd_state_t *tsp;
3570Sstevel@tonic-gate int instance;
3580Sstevel@tonic-gate timeout_id_t tid;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate instance = getminor(dev);
3610Sstevel@tonic-gate if (instance < 0)
3620Sstevel@tonic-gate return (ENXIO);
3630Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
3640Sstevel@tonic-gate if (tsp == NULL)
3650Sstevel@tonic-gate return (ENXIO);
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate mutex_enter(&tsp->lock);
3680Sstevel@tonic-gate if (tsp->started == B_FALSE) {
3690Sstevel@tonic-gate tsp->tid = 0;
3700Sstevel@tonic-gate mutex_exit(&tsp->lock);
3710Sstevel@tonic-gate return (0);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate /*
3740Sstevel@tonic-gate * The watchdog is enabled. Cancel the pending timer
3750Sstevel@tonic-gate * and call plat_fan_blast.
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate tsp->started = B_FALSE;
3780Sstevel@tonic-gate tid = tsp->tid;
3790Sstevel@tonic-gate tsp->tid = 0;
3800Sstevel@tonic-gate mutex_exit(&tsp->lock);
3810Sstevel@tonic-gate if (tid != 0)
3820Sstevel@tonic-gate (void) untimeout(tid);
3830Sstevel@tonic-gate plat_fan_blast();
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate return (0);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * These are private ioctls for PICL environmental control plug-in
3900Sstevel@tonic-gate * to use. The plug-in enables the watchdog before performing
3910Sstevel@tonic-gate * altering fan speeds. It also periodically issues a keepalive
3920Sstevel@tonic-gate * to the watchdog to cancel and reinstate the watchdog timer.
3930Sstevel@tonic-gate * The watchdog timeout handler when executed with the watchdog
3940Sstevel@tonic-gate * enabled sets fans to full blast by calling plat_fan_blast.
3950Sstevel@tonic-gate */
3960Sstevel@tonic-gate /*ARGSUSED*/
3970Sstevel@tonic-gate static int
xcalwd_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cred_p,int * rvalp)3980Sstevel@tonic-gate xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
3990Sstevel@tonic-gate cred_t *cred_p, int *rvalp)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate int instance;
4020Sstevel@tonic-gate xcalwd_state_t *tsp;
4030Sstevel@tonic-gate int intvl;
4040Sstevel@tonic-gate int o_intvl;
4050Sstevel@tonic-gate boolean_t curstate;
4060Sstevel@tonic-gate timeout_id_t tid;
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate if (secpolicy_sys_config(cred_p, B_FALSE) != 0)
4090Sstevel@tonic-gate return (EPERM);
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate instance = getminor(dev);
4120Sstevel@tonic-gate if (instance < 0)
4130Sstevel@tonic-gate return (ENXIO);
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance);
4160Sstevel@tonic-gate if (tsp == NULL)
4170Sstevel@tonic-gate return (ENXIO);
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate switch (cmd) {
4200Sstevel@tonic-gate case XCALWD_STOPWATCHDOG:
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate * cancels any pending timer and disables the timer.
4230Sstevel@tonic-gate */
4240Sstevel@tonic-gate tid = 0;
4250Sstevel@tonic-gate mutex_enter(&tsp->lock);
4260Sstevel@tonic-gate if (tsp->started == B_FALSE) {
4270Sstevel@tonic-gate mutex_exit(&tsp->lock);
4280Sstevel@tonic-gate return (0);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate tid = tsp->tid;
4310Sstevel@tonic-gate tsp->started = B_FALSE;
4320Sstevel@tonic-gate tsp->tid = 0;
4330Sstevel@tonic-gate mutex_exit(&tsp->lock);
4340Sstevel@tonic-gate if (tid != 0)
4350Sstevel@tonic-gate (void) untimeout(tid);
4360Sstevel@tonic-gate return (0);
4370Sstevel@tonic-gate case XCALWD_STARTWATCHDOG:
4380Sstevel@tonic-gate if (ddi_copyin((void *)arg, &intvl, sizeof (intvl), flag))
4390Sstevel@tonic-gate return (EFAULT);
4400Sstevel@tonic-gate if (intvl == 0)
4410Sstevel@tonic-gate return (EINVAL);
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate mutex_enter(&tsp->lock);
4440Sstevel@tonic-gate o_intvl = tsp->intvl;
4450Sstevel@tonic-gate mutex_exit(&tsp->lock);
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate if (ddi_copyout((const void *)&o_intvl, (void *)arg,
4480Sstevel@tonic-gate sizeof (o_intvl), flag))
4490Sstevel@tonic-gate return (EFAULT);
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate mutex_enter(&tsp->lock);
4520Sstevel@tonic-gate if (tsp->started == B_TRUE) {
4530Sstevel@tonic-gate mutex_exit(&tsp->lock);
4540Sstevel@tonic-gate return (EINVAL);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate tsp->intvl = intvl;
4570Sstevel@tonic-gate tsp->tid = realtime_timeout(xcalwd_timeout,
458796Smathue (void *)(uintptr_t)instance,
4590Sstevel@tonic-gate drv_usectohz(1000000) * tsp->intvl);
4600Sstevel@tonic-gate tsp->started = B_TRUE;
4610Sstevel@tonic-gate mutex_exit(&tsp->lock);
4620Sstevel@tonic-gate return (0);
4630Sstevel@tonic-gate case XCALWD_KEEPALIVE:
4640Sstevel@tonic-gate tid = 0;
4650Sstevel@tonic-gate mutex_enter(&tsp->lock);
4660Sstevel@tonic-gate tid = tsp->tid;
4670Sstevel@tonic-gate tsp->tid = 0;
4680Sstevel@tonic-gate mutex_exit(&tsp->lock);
4690Sstevel@tonic-gate if (tid != 0)
4700Sstevel@tonic-gate (void) untimeout(tid); /* cancel */
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate mutex_enter(&tsp->lock);
4730Sstevel@tonic-gate if (tsp->started == B_TRUE) /* reinstate */
4740Sstevel@tonic-gate tsp->tid = realtime_timeout(xcalwd_timeout,
475796Smathue (void *)(uintptr_t)instance,
4760Sstevel@tonic-gate drv_usectohz(1000000) * tsp->intvl);
4770Sstevel@tonic-gate mutex_exit(&tsp->lock);
4780Sstevel@tonic-gate return (0);
4790Sstevel@tonic-gate case XCALWD_GETSTATE:
4800Sstevel@tonic-gate mutex_enter(&tsp->lock);
4810Sstevel@tonic-gate curstate = tsp->started;
4820Sstevel@tonic-gate mutex_exit(&tsp->lock);
4830Sstevel@tonic-gate if (ddi_copyout((const void *)&curstate, (void *)arg,
4840Sstevel@tonic-gate sizeof (curstate), flag))
4850Sstevel@tonic-gate return (EFAULT);
4860Sstevel@tonic-gate return (0);
4870Sstevel@tonic-gate default:
4880Sstevel@tonic-gate return (EINVAL);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate /*NOTREACHED*/
4910Sstevel@tonic-gate }
492