17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*19397407SSherry Moore * Common Development and Distribution License (the "License"). 6*19397407SSherry Moore * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*19397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * Excalibur fans watchdog module 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <sys/conf.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 347c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 387c478bd9Sstevel@tonic-gate #include <sys/sunndi.h> 397c478bd9Sstevel@tonic-gate #include <sys/ksynch.h> 407c478bd9Sstevel@tonic-gate #include <sys/file.h> 417c478bd9Sstevel@tonic-gate #include <sys/errno.h> 427c478bd9Sstevel@tonic-gate #include <sys/open.h> 437c478bd9Sstevel@tonic-gate #include <sys/cred.h> 447c478bd9Sstevel@tonic-gate #include <sys/xcalwd.h> 457c478bd9Sstevel@tonic-gate #include <sys/policy.h> 467c478bd9Sstevel@tonic-gate #include <sys/platform_module.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #define MINOR_DEVICE_NAME "xcalwd" 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * Define your per instance state data 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate typedef struct xcalwd_state { 567c478bd9Sstevel@tonic-gate kmutex_t lock; 577c478bd9Sstevel@tonic-gate boolean_t started; 587c478bd9Sstevel@tonic-gate int intvl; 597c478bd9Sstevel@tonic-gate timeout_id_t tid; 607c478bd9Sstevel@tonic-gate dev_info_t *dip; 617c478bd9Sstevel@tonic-gate } xcalwd_state_t; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * Pointer to soft states 657c478bd9Sstevel@tonic-gate */ 667c478bd9Sstevel@tonic-gate static void *xcalwd_statep; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * dev_ops 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate static int xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 727c478bd9Sstevel@tonic-gate void *arg, void **resultp); 737c478bd9Sstevel@tonic-gate static int xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 747c478bd9Sstevel@tonic-gate static int xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * cb_ops 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate static int xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp); 807c478bd9Sstevel@tonic-gate static int xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp); 817c478bd9Sstevel@tonic-gate static int xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 827c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp); 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * timeout handler 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate static void xcalwd_timeout(void *arg); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * cb_ops 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate static struct cb_ops xcalwd_cb_ops = { 927c478bd9Sstevel@tonic-gate xcalwd_open, /* open */ 937c478bd9Sstevel@tonic-gate xcalwd_close, /* close */ 947c478bd9Sstevel@tonic-gate nodev, /* strategy */ 957c478bd9Sstevel@tonic-gate nodev, /* print */ 967c478bd9Sstevel@tonic-gate nodev, /* dump */ 977c478bd9Sstevel@tonic-gate nodev, /* read */ 987c478bd9Sstevel@tonic-gate nodev, /* write */ 997c478bd9Sstevel@tonic-gate xcalwd_ioctl, /* ioctl */ 1007c478bd9Sstevel@tonic-gate nodev, /* devmap */ 1017c478bd9Sstevel@tonic-gate nodev, /* mmap */ 1027c478bd9Sstevel@tonic-gate nodev, /* segmap */ 1037c478bd9Sstevel@tonic-gate nochpoll, /* chpoll */ 1047c478bd9Sstevel@tonic-gate ddi_prop_op, /* prop_op */ 1057c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 1067c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_64BIT, /* cb_flag */ 1077c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 1087c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 1097c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 1107c478bd9Sstevel@tonic-gate }; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * dev_ops 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate static struct dev_ops xcalwd_dev_ops = { 1167c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 1177c478bd9Sstevel@tonic-gate 0, /* devo_refcnt */ 1187c478bd9Sstevel@tonic-gate xcalwd_getinfo, /* getinfo */ 1197c478bd9Sstevel@tonic-gate nulldev, /* identify */ 1207c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1217c478bd9Sstevel@tonic-gate xcalwd_attach, /* attach */ 1227c478bd9Sstevel@tonic-gate xcalwd_detach, /* detach */ 1237c478bd9Sstevel@tonic-gate nodev, /* devo_reset */ 1247c478bd9Sstevel@tonic-gate &xcalwd_cb_ops, /* devo_cb_ops */ 1257c478bd9Sstevel@tonic-gate NULL, /* devo_bus_ops */ 126*19397407SSherry Moore NULL, /* devo_power */ 127*19397407SSherry Moore ddi_quiesce_not_needed, /* devo_quiesce */ 1287c478bd9Sstevel@tonic-gate }; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * modldrv 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate static struct modldrv xcalwd_modldrv = { 1347c478bd9Sstevel@tonic-gate &mod_driverops, /* drv_modops */ 135*19397407SSherry Moore "Excalibur watchdog timer v1.7 ", /* drv_linkinfo */ 1367c478bd9Sstevel@tonic-gate &xcalwd_dev_ops /* drv_dev_ops */ 1377c478bd9Sstevel@tonic-gate }; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * modlinkage 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate static struct modlinkage xcalwd_modlinkage = { 1437c478bd9Sstevel@tonic-gate MODREV_1, 1447c478bd9Sstevel@tonic-gate &xcalwd_modldrv, 1457c478bd9Sstevel@tonic-gate NULL 1467c478bd9Sstevel@tonic-gate }; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate int 1497c478bd9Sstevel@tonic-gate _init(void) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate int error; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * Initialize the module state structure 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate error = ddi_soft_state_init(&xcalwd_statep, 1577c478bd9Sstevel@tonic-gate sizeof (xcalwd_state_t), 0); 1587c478bd9Sstevel@tonic-gate if (error) { 1597c478bd9Sstevel@tonic-gate return (error); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * Link the driver into the system 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate error = mod_install(&xcalwd_modlinkage); 1667c478bd9Sstevel@tonic-gate if (error) { 1677c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&xcalwd_statep); 1687c478bd9Sstevel@tonic-gate return (error); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate return (0); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate int 1747c478bd9Sstevel@tonic-gate _fini(void) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate int error; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate error = mod_remove(&xcalwd_modlinkage); 1797c478bd9Sstevel@tonic-gate if (error != 0) { 1807c478bd9Sstevel@tonic-gate return (error); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * Cleanup resources allocated in _init 1857c478bd9Sstevel@tonic-gate */ 1867c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&xcalwd_statep); 1877c478bd9Sstevel@tonic-gate return (0); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate int 1917c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate return (mod_info(&xcalwd_modlinkage, modinfop)); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1977c478bd9Sstevel@tonic-gate static int 1987c478bd9Sstevel@tonic-gate xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, 1997c478bd9Sstevel@tonic-gate void *arg, void **resultp) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate int retval; 2027c478bd9Sstevel@tonic-gate dev_t dev = (dev_t)arg; 2037c478bd9Sstevel@tonic-gate int instance; 2047c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate retval = DDI_FAILURE; 2077c478bd9Sstevel@tonic-gate switch (cmd) { 2087c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2097c478bd9Sstevel@tonic-gate instance = getminor(dev); 2107c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 2117c478bd9Sstevel@tonic-gate if (tsp == NULL) 2127c478bd9Sstevel@tonic-gate *resultp = NULL; 2137c478bd9Sstevel@tonic-gate else { 2147c478bd9Sstevel@tonic-gate *resultp = tsp->dip; 2157c478bd9Sstevel@tonic-gate retval = DDI_SUCCESS; 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate break; 2187c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 219360e6f5eSmathue *resultp = (void *)(uintptr_t)getminor(dev); 2207c478bd9Sstevel@tonic-gate retval = DDI_SUCCESS; 2217c478bd9Sstevel@tonic-gate break; 2227c478bd9Sstevel@tonic-gate default: 2237c478bd9Sstevel@tonic-gate break; 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate return (retval); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static int 2297c478bd9Sstevel@tonic-gate xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate int instance; 2327c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate switch (cmd) { 2357c478bd9Sstevel@tonic-gate case DDI_ATTACH: 2367c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (&plat_fan_blast == NULL) { 2397c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "missing plat_fan_blast function"); 2407c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(xcalwd_statep, instance) != 2447c478bd9Sstevel@tonic-gate DDI_SUCCESS) { 2457c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "attach could not alloc" 2467c478bd9Sstevel@tonic-gate "%d state structure", instance); 2477c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 2517c478bd9Sstevel@tonic-gate if (tsp == NULL) { 2527c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "get state failed %d", 2537c478bd9Sstevel@tonic-gate instance); 2547c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, MINOR_DEVICE_NAME, 2587c478bd9Sstevel@tonic-gate S_IFCHR, instance, DDI_PSEUDO, NULL) == DDI_FAILURE) { 2597c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "create minor node failed\n"); 2607c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate mutex_init(&tsp->lock, NULL, MUTEX_DRIVER, NULL); 2647c478bd9Sstevel@tonic-gate tsp->started = B_FALSE; 2657c478bd9Sstevel@tonic-gate tsp->intvl = 0; 2667c478bd9Sstevel@tonic-gate tsp->tid = 0; 2677c478bd9Sstevel@tonic-gate tsp->dip = dip; 2687c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 2697c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate case DDI_RESUME: 2727c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2737c478bd9Sstevel@tonic-gate default: 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate static int 2807c478bd9Sstevel@tonic-gate xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 2837c478bd9Sstevel@tonic-gate int instance; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate switch (cmd) { 2867c478bd9Sstevel@tonic-gate case DDI_DETACH: 2877c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2887c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 2897c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2907c478bd9Sstevel@tonic-gate mutex_destroy(&tsp->lock); 2917c478bd9Sstevel@tonic-gate ddi_soft_state_free(xcalwd_statep, instance); 2927c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2937c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 2947c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2957c478bd9Sstevel@tonic-gate default: 2967c478bd9Sstevel@tonic-gate break; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* 3027c478bd9Sstevel@tonic-gate * Watchdog timeout handler that calls plat_fan_blast to take 3037c478bd9Sstevel@tonic-gate * the failsafe action. 3047c478bd9Sstevel@tonic-gate */ 3057c478bd9Sstevel@tonic-gate static void 3067c478bd9Sstevel@tonic-gate xcalwd_timeout(void *arg) 3077c478bd9Sstevel@tonic-gate { 308360e6f5eSmathue int instance = (int)(uintptr_t)arg; 3097c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (instance < 0) 3127c478bd9Sstevel@tonic-gate return; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 3157c478bd9Sstevel@tonic-gate if (tsp == NULL) 3167c478bd9Sstevel@tonic-gate return; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 3197c478bd9Sstevel@tonic-gate if (tsp->started == B_FALSE || tsp->tid == 0) { 3207c478bd9Sstevel@tonic-gate tsp->tid = 0; 3217c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 3227c478bd9Sstevel@tonic-gate return; 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate plat_fan_blast(); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3307c478bd9Sstevel@tonic-gate static int 3317c478bd9Sstevel@tonic-gate xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate int instance; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (secpolicy_sys_config(credp, B_FALSE) != 0) 3367c478bd9Sstevel@tonic-gate return (EPERM); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 3397c478bd9Sstevel@tonic-gate return (EINVAL); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate instance = getminor(*devp); 3427c478bd9Sstevel@tonic-gate if (instance < 0) 3437c478bd9Sstevel@tonic-gate return (ENXIO); 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate if (ddi_get_soft_state(xcalwd_statep, instance) == NULL) { 3467c478bd9Sstevel@tonic-gate return (ENXIO); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate return (0); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3537c478bd9Sstevel@tonic-gate static int 3547c478bd9Sstevel@tonic-gate xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp) 3557c478bd9Sstevel@tonic-gate { 3567c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 3577c478bd9Sstevel@tonic-gate int instance; 3587c478bd9Sstevel@tonic-gate timeout_id_t tid; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate instance = getminor(dev); 3617c478bd9Sstevel@tonic-gate if (instance < 0) 3627c478bd9Sstevel@tonic-gate return (ENXIO); 3637c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 3647c478bd9Sstevel@tonic-gate if (tsp == NULL) 3657c478bd9Sstevel@tonic-gate return (ENXIO); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 3687c478bd9Sstevel@tonic-gate if (tsp->started == B_FALSE) { 3697c478bd9Sstevel@tonic-gate tsp->tid = 0; 3707c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 3717c478bd9Sstevel@tonic-gate return (0); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * The watchdog is enabled. Cancel the pending timer 3757c478bd9Sstevel@tonic-gate * and call plat_fan_blast. 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate tsp->started = B_FALSE; 3787c478bd9Sstevel@tonic-gate tid = tsp->tid; 3797c478bd9Sstevel@tonic-gate tsp->tid = 0; 3807c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 3817c478bd9Sstevel@tonic-gate if (tid != 0) 3827c478bd9Sstevel@tonic-gate (void) untimeout(tid); 3837c478bd9Sstevel@tonic-gate plat_fan_blast(); 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate return (0); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * These are private ioctls for PICL environmental control plug-in 3907c478bd9Sstevel@tonic-gate * to use. The plug-in enables the watchdog before performing 3917c478bd9Sstevel@tonic-gate * altering fan speeds. It also periodically issues a keepalive 3927c478bd9Sstevel@tonic-gate * to the watchdog to cancel and reinstate the watchdog timer. 3937c478bd9Sstevel@tonic-gate * The watchdog timeout handler when executed with the watchdog 3947c478bd9Sstevel@tonic-gate * enabled sets fans to full blast by calling plat_fan_blast. 3957c478bd9Sstevel@tonic-gate */ 3967c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3977c478bd9Sstevel@tonic-gate static int 3987c478bd9Sstevel@tonic-gate xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, 3997c478bd9Sstevel@tonic-gate cred_t *cred_p, int *rvalp) 4007c478bd9Sstevel@tonic-gate { 4017c478bd9Sstevel@tonic-gate int instance; 4027c478bd9Sstevel@tonic-gate xcalwd_state_t *tsp; 4037c478bd9Sstevel@tonic-gate int intvl; 4047c478bd9Sstevel@tonic-gate int o_intvl; 4057c478bd9Sstevel@tonic-gate boolean_t curstate; 4067c478bd9Sstevel@tonic-gate timeout_id_t tid; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (secpolicy_sys_config(cred_p, B_FALSE) != 0) 4097c478bd9Sstevel@tonic-gate return (EPERM); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate instance = getminor(dev); 4127c478bd9Sstevel@tonic-gate if (instance < 0) 4137c478bd9Sstevel@tonic-gate return (ENXIO); 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate tsp = ddi_get_soft_state(xcalwd_statep, instance); 4167c478bd9Sstevel@tonic-gate if (tsp == NULL) 4177c478bd9Sstevel@tonic-gate return (ENXIO); 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate switch (cmd) { 4207c478bd9Sstevel@tonic-gate case XCALWD_STOPWATCHDOG: 4217c478bd9Sstevel@tonic-gate /* 4227c478bd9Sstevel@tonic-gate * cancels any pending timer and disables the timer. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate tid = 0; 4257c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4267c478bd9Sstevel@tonic-gate if (tsp->started == B_FALSE) { 4277c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4287c478bd9Sstevel@tonic-gate return (0); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate tid = tsp->tid; 4317c478bd9Sstevel@tonic-gate tsp->started = B_FALSE; 4327c478bd9Sstevel@tonic-gate tsp->tid = 0; 4337c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4347c478bd9Sstevel@tonic-gate if (tid != 0) 4357c478bd9Sstevel@tonic-gate (void) untimeout(tid); 4367c478bd9Sstevel@tonic-gate return (0); 4377c478bd9Sstevel@tonic-gate case XCALWD_STARTWATCHDOG: 4387c478bd9Sstevel@tonic-gate if (ddi_copyin((void *)arg, &intvl, sizeof (intvl), flag)) 4397c478bd9Sstevel@tonic-gate return (EFAULT); 4407c478bd9Sstevel@tonic-gate if (intvl == 0) 4417c478bd9Sstevel@tonic-gate return (EINVAL); 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4447c478bd9Sstevel@tonic-gate o_intvl = tsp->intvl; 4457c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (ddi_copyout((const void *)&o_intvl, (void *)arg, 4487c478bd9Sstevel@tonic-gate sizeof (o_intvl), flag)) 4497c478bd9Sstevel@tonic-gate return (EFAULT); 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4527c478bd9Sstevel@tonic-gate if (tsp->started == B_TRUE) { 4537c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4547c478bd9Sstevel@tonic-gate return (EINVAL); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate tsp->intvl = intvl; 4577c478bd9Sstevel@tonic-gate tsp->tid = realtime_timeout(xcalwd_timeout, 458360e6f5eSmathue (void *)(uintptr_t)instance, 4597c478bd9Sstevel@tonic-gate drv_usectohz(1000000) * tsp->intvl); 4607c478bd9Sstevel@tonic-gate tsp->started = B_TRUE; 4617c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4627c478bd9Sstevel@tonic-gate return (0); 4637c478bd9Sstevel@tonic-gate case XCALWD_KEEPALIVE: 4647c478bd9Sstevel@tonic-gate tid = 0; 4657c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4667c478bd9Sstevel@tonic-gate tid = tsp->tid; 4677c478bd9Sstevel@tonic-gate tsp->tid = 0; 4687c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4697c478bd9Sstevel@tonic-gate if (tid != 0) 4707c478bd9Sstevel@tonic-gate (void) untimeout(tid); /* cancel */ 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4737c478bd9Sstevel@tonic-gate if (tsp->started == B_TRUE) /* reinstate */ 4747c478bd9Sstevel@tonic-gate tsp->tid = realtime_timeout(xcalwd_timeout, 475360e6f5eSmathue (void *)(uintptr_t)instance, 4767c478bd9Sstevel@tonic-gate drv_usectohz(1000000) * tsp->intvl); 4777c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4787c478bd9Sstevel@tonic-gate return (0); 4797c478bd9Sstevel@tonic-gate case XCALWD_GETSTATE: 4807c478bd9Sstevel@tonic-gate mutex_enter(&tsp->lock); 4817c478bd9Sstevel@tonic-gate curstate = tsp->started; 4827c478bd9Sstevel@tonic-gate mutex_exit(&tsp->lock); 4837c478bd9Sstevel@tonic-gate if (ddi_copyout((const void *)&curstate, (void *)arg, 4847c478bd9Sstevel@tonic-gate sizeof (curstate), flag)) 4857c478bd9Sstevel@tonic-gate return (EFAULT); 4867c478bd9Sstevel@tonic-gate return (0); 4877c478bd9Sstevel@tonic-gate default: 4887c478bd9Sstevel@tonic-gate return (EINVAL); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 4917c478bd9Sstevel@tonic-gate } 492