xref: /onnv-gate/usr/src/uts/sun4u/mpxu/io/tsalarm.c (revision 7656:2621e50fdf4a)
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 #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/callb.h>
330Sstevel@tonic-gate #include <sys/strlog.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/lom_io.h>
360Sstevel@tonic-gate #include <sys/ddi.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #define	LOMIOCALCTL_OLD		_IOW('a', 4, ts_aldata_t)
400Sstevel@tonic-gate #define	LOMIOCALSTATE_OLD	_IOWR('a', 5, ts_aldata_t)
410Sstevel@tonic-gate 
420Sstevel@tonic-gate struct tsalarm_softc {
430Sstevel@tonic-gate 	dev_info_t *dip;
440Sstevel@tonic-gate 	kmutex_t mutex;
450Sstevel@tonic-gate };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	getsoftc(minor)	\
480Sstevel@tonic-gate 		((struct tsalarm_softc *)ddi_get_soft_state(statep, (minor)))
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Driver entry points
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int	tsalarm_attach(dev_info_t *, ddi_attach_cmd_t);
560Sstevel@tonic-gate static int	tsalarm_detach(dev_info_t *, ddi_detach_cmd_t);
570Sstevel@tonic-gate static int	tsalarm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int	tsalarm_open(dev_t *, int, int, cred_t *);
600Sstevel@tonic-gate static int	tsalarm_close(dev_t, int, int, cred_t *);
610Sstevel@tonic-gate static int	tsalarm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static struct cb_ops tsalarm_cb_ops = {
640Sstevel@tonic-gate 	tsalarm_open,	/* open */
650Sstevel@tonic-gate 	tsalarm_close,	/* close */
660Sstevel@tonic-gate 	nodev,		/* strategy() */
670Sstevel@tonic-gate 	nodev,		/* print() */
680Sstevel@tonic-gate 	nodev,		/* dump() */
690Sstevel@tonic-gate 	nodev,		/* read() */
700Sstevel@tonic-gate 	nodev,		/* write() */
710Sstevel@tonic-gate 	tsalarm_ioctl,	/* ioctl() */
720Sstevel@tonic-gate 	nodev,		/* devmap() */
730Sstevel@tonic-gate 	nodev,		/* mmap() */
740Sstevel@tonic-gate 	ddi_segmap,	/* segmap() */
750Sstevel@tonic-gate 	nochpoll,	/* poll() */
760Sstevel@tonic-gate 	ddi_prop_op,    /* prop_op() */
770Sstevel@tonic-gate 	NULL,		/* cb_str */
780Sstevel@tonic-gate 	D_NEW | D_MP	/* cb_flag */
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static struct dev_ops tsalarm_ops = {
830Sstevel@tonic-gate 	DEVO_REV,
840Sstevel@tonic-gate 	0,			/* ref count */
850Sstevel@tonic-gate 	tsalarm_getinfo,	/* getinfo() */
860Sstevel@tonic-gate 	nulldev,		/* identify() */
870Sstevel@tonic-gate 	nulldev,		/* probe() */
880Sstevel@tonic-gate 	tsalarm_attach,		/* attach() */
890Sstevel@tonic-gate 	tsalarm_detach,		/* detach */
900Sstevel@tonic-gate 	nodev,			/* reset */
910Sstevel@tonic-gate 	&tsalarm_cb_ops,		/* pointer to cb_ops structure */
920Sstevel@tonic-gate 	(struct bus_ops *)NULL,
93*7656SSherry.Moore@Sun.COM 	nulldev,		/* power() */
94*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce() */
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Loadable module support.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1010Sstevel@tonic-gate static void    *statep;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static struct modldrv modldrv = {
1040Sstevel@tonic-gate 	&mod_driverops,			/* Type of module. This is a driver */
105*7656SSherry.Moore@Sun.COM 	"tsalarm control driver",	/* Name of the module */
1060Sstevel@tonic-gate 	&tsalarm_ops			/* pointer to the dev_ops structure */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static struct modlinkage modlinkage = {
1100Sstevel@tonic-gate 	MODREV_1,
1110Sstevel@tonic-gate 	&modldrv,
1120Sstevel@tonic-gate 	NULL
1130Sstevel@tonic-gate };
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate extern int rmclomv_alarm_get(int alarm_type, int *alarm_state);
1160Sstevel@tonic-gate extern int rmclomv_alarm_set(int alarm_type, int new_state);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate int
_init(void)1190Sstevel@tonic-gate _init(void)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	int    e;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (e = ddi_soft_state_init(&statep,
1240Sstevel@tonic-gate 				sizeof (struct tsalarm_softc), 1)) {
1250Sstevel@tonic-gate 		return (e);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0) {
1290Sstevel@tonic-gate 		ddi_soft_state_fini(&statep);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	return (e);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
_fini(void)1370Sstevel@tonic-gate _fini(void)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	int e;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0) {
1420Sstevel@tonic-gate 		return (e);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	ddi_soft_state_fini(&statep);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (DDI_SUCCESS);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1520Sstevel@tonic-gate _info(struct modinfo *modinfop)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /* ARGSUSED */
1590Sstevel@tonic-gate static int
tsalarm_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)1600Sstevel@tonic-gate tsalarm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	int	inst = getminor((dev_t)arg);
1630Sstevel@tonic-gate 	int	retval = DDI_SUCCESS;
1640Sstevel@tonic-gate 	struct tsalarm_softc *softc;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	switch (cmd) {
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1690Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL) {
1700Sstevel@tonic-gate 			*result = (void *)NULL;
1710Sstevel@tonic-gate 			retval = DDI_FAILURE;
1720Sstevel@tonic-gate 		} else {
1730Sstevel@tonic-gate 			*result = (void *)softc->dip;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		break;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
178496Ssc121708 		*result = (void *)(uintptr_t)inst;
1790Sstevel@tonic-gate 		break;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	default:
1820Sstevel@tonic-gate 		retval = DDI_FAILURE;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (retval);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static int
tsalarm_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1890Sstevel@tonic-gate tsalarm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	int inst;
1930Sstevel@tonic-gate 	struct tsalarm_softc *softc = NULL;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	switch (cmd) {
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	case DDI_ATTACH:
1980Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * Allocate a soft state structure for this instance.
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS)
2030Sstevel@tonic-gate 			goto attach_failed;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		softc = getsoftc(inst);
2060Sstevel@tonic-gate 		softc->dip = dip;
2070Sstevel@tonic-gate 		mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL);
2080Sstevel@tonic-gate 		/*
2090Sstevel@tonic-gate 		 * Create minor node.  The minor device number, inst, has no
2100Sstevel@tonic-gate 		 * meaning.  The model number above, which will be added to
2110Sstevel@tonic-gate 		 * the device's softc, is used to direct peculiar behavior.
2120Sstevel@tonic-gate 		 */
2130Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "lom", S_IFCHR, 0,
214*7656SSherry.Moore@Sun.COM 		    DDI_PSEUDO, NULL) == DDI_FAILURE)
2150Sstevel@tonic-gate 			goto attach_failed;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		ddi_report_dev(dip);
2180Sstevel@tonic-gate 		return (DDI_SUCCESS);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	case DDI_RESUME:
2210Sstevel@tonic-gate 		return (DDI_SUCCESS);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	default:
2240Sstevel@tonic-gate 		return (DDI_FAILURE);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate attach_failed:
2280Sstevel@tonic-gate 	/* Free soft state, if allocated. remove minor node if added earlier */
2290Sstevel@tonic-gate 	if (softc) {
2300Sstevel@tonic-gate 		mutex_destroy(&softc->mutex);
2310Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	return (DDI_FAILURE);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static int
tsalarm_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2400Sstevel@tonic-gate tsalarm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	int inst;
2430Sstevel@tonic-gate 	struct tsalarm_softc *softc;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	switch (cmd) {
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	case DDI_DETACH:
2480Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
2490Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL)
2500Sstevel@tonic-gate 			return (DDI_FAILURE);
2510Sstevel@tonic-gate 		/*
2520Sstevel@tonic-gate 		 * Free the soft state and remove minor node added earlier.
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2550Sstevel@tonic-gate 		mutex_destroy(&softc->mutex);
2560Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
2570Sstevel@tonic-gate 		return (DDI_SUCCESS);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	case DDI_SUSPEND:
2600Sstevel@tonic-gate 		return (DDI_SUCCESS);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	default:
2630Sstevel@tonic-gate 		return (DDI_FAILURE);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /* ARGSUSED */
2690Sstevel@tonic-gate static int
tsalarm_open(dev_t * devp,int flag,int otyp,cred_t * credp)2700Sstevel@tonic-gate tsalarm_open(dev_t *devp, int flag, int otyp, cred_t *credp)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	int	inst = getminor(*devp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /* ARGSUSED */
2790Sstevel@tonic-gate static int
tsalarm_close(dev_t dev,int flag,int otyp,cred_t * credp)2800Sstevel@tonic-gate tsalarm_close(dev_t dev, int flag, int otyp, cred_t *credp)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	int	inst = getminor(dev);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /* ARGSUSED */
2890Sstevel@tonic-gate static int
tsalarm_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2900Sstevel@tonic-gate tsalarm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2910Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	int		inst = getminor(dev);
2940Sstevel@tonic-gate 	struct tsalarm_softc *softc;
2950Sstevel@tonic-gate 	int retval = 0;
2960Sstevel@tonic-gate 	ts_aldata_t ts_alinfo;
2970Sstevel@tonic-gate 	int alarm_type, alarm_state = 0;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if ((softc = getsoftc(inst)) == NULL)
3000Sstevel@tonic-gate 		return (ENXIO);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	mutex_enter(&softc->mutex);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	switch (cmd) {
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	case LOMIOCALSTATE:
3070Sstevel@tonic-gate 	case LOMIOCALSTATE_OLD:
3080Sstevel@tonic-gate 		{
3090Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo,
310*7656SSherry.Moore@Sun.COM 			    sizeof (ts_aldata_t), mode) != 0) {
3110Sstevel@tonic-gate 				retval = EFAULT;
3120Sstevel@tonic-gate 				goto end;
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 			alarm_type = ts_alinfo.alarm_no;
3160Sstevel@tonic-gate 			if ((alarm_type < ALARM_CRITICAL) ||
317*7656SSherry.Moore@Sun.COM 			    (alarm_type > ALARM_USER)) {
3180Sstevel@tonic-gate 				retval = EINVAL;
3190Sstevel@tonic-gate 				goto end;
3200Sstevel@tonic-gate 			}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 			retval = rmclomv_alarm_get(alarm_type, &alarm_state);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 			if (retval != 0)
3250Sstevel@tonic-gate 				goto end;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 			if ((alarm_state != 0) && (alarm_state != 1)) {
3280Sstevel@tonic-gate 				retval = EIO;
3290Sstevel@tonic-gate 				goto end;
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 			ts_alinfo.alarm_state = alarm_state;
3330Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&ts_alinfo, (caddr_t)arg,
334*7656SSherry.Moore@Sun.COM 			    sizeof (ts_aldata_t), mode) != 0) {
3350Sstevel@tonic-gate 				retval = EFAULT;
3360Sstevel@tonic-gate 				goto end;
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	case LOMIOCALCTL:
3430Sstevel@tonic-gate 	case LOMIOCALCTL_OLD:
3440Sstevel@tonic-gate 		{
3450Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo,
346*7656SSherry.Moore@Sun.COM 			    sizeof (ts_aldata_t), mode) != 0) {
3470Sstevel@tonic-gate 				retval = EFAULT;
3480Sstevel@tonic-gate 				goto end;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 			alarm_type = ts_alinfo.alarm_no;
3520Sstevel@tonic-gate 			alarm_state = ts_alinfo.alarm_state;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			if ((alarm_type < ALARM_CRITICAL) ||
355*7656SSherry.Moore@Sun.COM 			    (alarm_type > ALARM_USER)) {
3560Sstevel@tonic-gate 				retval = EINVAL;
3570Sstevel@tonic-gate 				goto end;
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 			if ((alarm_state < ALARM_OFF) ||
360*7656SSherry.Moore@Sun.COM 			    (alarm_state > ALARM_ON)) {
3610Sstevel@tonic-gate 				retval = EINVAL;
3620Sstevel@tonic-gate 				goto end;
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 			retval = rmclomv_alarm_set(alarm_type, alarm_state);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	default:
3700Sstevel@tonic-gate 		retval = EINVAL;
3710Sstevel@tonic-gate 		break;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate end:
3750Sstevel@tonic-gate 	mutex_exit(&softc->mutex);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	return (retval);
3780Sstevel@tonic-gate }
379