xref: /onnv-gate/usr/src/uts/sun4u/starcat/io/cvcredir.c (revision 11311:639e7bc0b42f)
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
57656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
67656SSherry.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*11311SSurya.Prakki@Sun.COM  * Copyright 2009 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  * MT STREAMS Virtual Console Redirection Device Driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/stream.h>
370Sstevel@tonic-gate #include <sys/stropts.h>
380Sstevel@tonic-gate #include <sys/strsun.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/thread.h>
410Sstevel@tonic-gate #include <sys/conf.h>
420Sstevel@tonic-gate #include <sys/ddi.h>
430Sstevel@tonic-gate #include <sys/sunddi.h>
440Sstevel@tonic-gate #include <sys/tty.h>
450Sstevel@tonic-gate #include <sys/sc_cvcio.h>
460Sstevel@tonic-gate #include <sys/conf.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Routine to to register/unregister our queue for console output and pass
520Sstevel@tonic-gate  * redirected data to the console.  The cvc driver will do a putnext using
530Sstevel@tonic-gate  * our queue, so we will not see the redirected console data.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate extern int	cvc_redir(mblk_t *);
560Sstevel@tonic-gate extern int	cvc_register(queue_t *);
570Sstevel@tonic-gate extern int	cvc_unregister(queue_t *);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int	cvcr_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
600Sstevel@tonic-gate static int	cvcr_attach(dev_info_t *, ddi_attach_cmd_t);
610Sstevel@tonic-gate static int	cvcr_detach(dev_info_t *, ddi_detach_cmd_t);
620Sstevel@tonic-gate static int	cvcr_wput(queue_t *, mblk_t *);
630Sstevel@tonic-gate static int	cvcr_open(queue_t *, dev_t *, int, int, cred_t *);
640Sstevel@tonic-gate static int	cvcr_close(queue_t *, int, cred_t *);
650Sstevel@tonic-gate static void	cvcr_ioctl(queue_t *, mblk_t *);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static dev_info_t	*cvcr_dip;
680Sstevel@tonic-gate static int		cvcr_suspend = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static struct module_info minfo = {
710Sstevel@tonic-gate 	1314,		/* mi_idnum Bad luck number +1  ;-) */
720Sstevel@tonic-gate 	"cvcredir",	/* mi_idname */
730Sstevel@tonic-gate 	0,		/* mi_minpsz */
740Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz */
750Sstevel@tonic-gate 	2048,		/* mi_hiwat */
760Sstevel@tonic-gate 	2048		/* mi_lowat */
770Sstevel@tonic-gate };
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static struct qinit	cvcr_rinit = {
800Sstevel@tonic-gate 	NULL,		/* qi_putp */
810Sstevel@tonic-gate 	NULL,		/* qi_srvp */
820Sstevel@tonic-gate 	cvcr_open,	/* qi_qopen */
830Sstevel@tonic-gate 	cvcr_close,	/* qi_qclose */
840Sstevel@tonic-gate 	NULL,		/* qi_qadmin */
850Sstevel@tonic-gate 	&minfo,		/* qi_minfo */
860Sstevel@tonic-gate 	NULL		/* qi_mstat */
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct qinit	cvcr_winit = {
900Sstevel@tonic-gate 	cvcr_wput,	/* qi_putp */
910Sstevel@tonic-gate 	NULL,		/* qi_srvp */
920Sstevel@tonic-gate 	cvcr_open,	/* qi_qopen */
930Sstevel@tonic-gate 	cvcr_close,	/* qi_qclose */
940Sstevel@tonic-gate 	NULL,		/* qi_qadmin */
950Sstevel@tonic-gate 	&minfo,		/* qi_minfo */
960Sstevel@tonic-gate 	NULL		/* qi_mstat */
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate struct streamtab	cvcrinfo = {
1000Sstevel@tonic-gate 	&cvcr_rinit,	/* st_rdinit */
1010Sstevel@tonic-gate 	&cvcr_winit,	/* st_wrinit */
1020Sstevel@tonic-gate 	NULL,		/* st_muxrinit */
1030Sstevel@tonic-gate 	NULL		/* st_muxwrinit */
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(cvcrops, nulldev, nulldev, cvcr_attach,
1077656SSherry.Moore@Sun.COM     cvcr_detach, nodev, cvcr_info, (D_MTPERQ|D_MP), &cvcrinfo,
1087656SSherry.Moore@Sun.COM     ddi_quiesce_not_supported);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static struct modldrv modldrv = {
1130Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
1147656SSherry.Moore@Sun.COM 	"CVC redirect driver 'cvcredir'",
1150Sstevel@tonic-gate 	&cvcrops,	/* driver ops */
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static struct modlinkage modlinkage = {
1190Sstevel@tonic-gate 	MODREV_1,
1200Sstevel@tonic-gate 	&modldrv,
1210Sstevel@tonic-gate 	NULL
1220Sstevel@tonic-gate };
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
_init()1260Sstevel@tonic-gate _init()
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate int
_fini()1320Sstevel@tonic-gate _fini()
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
_info(modinfop)1380Sstevel@tonic-gate _info(modinfop)
1390Sstevel@tonic-gate 	struct modinfo *modinfop;
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*ARGSUSED*/
1450Sstevel@tonic-gate static int
cvcr_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)1460Sstevel@tonic-gate cvcr_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	if (cmd == DDI_RESUME) {
1490Sstevel@tonic-gate 		if (cvcr_suspend) {
1500Sstevel@tonic-gate 			cvcr_suspend = 0;
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 	} else {
1530Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, "cvcredir", S_IFCHR,
1540Sstevel@tonic-gate 		    0, NULL, NULL) == DDI_FAILURE) {
1550Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
1560Sstevel@tonic-gate 			return (-1);
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 		cvcr_dip = devi;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	return (DDI_SUCCESS);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static int
cvcr_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1640Sstevel@tonic-gate cvcr_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	if (cmd == DDI_SUSPEND) {
1670Sstevel@tonic-gate 		if (!cvcr_suspend) {
1680Sstevel@tonic-gate 			cvcr_suspend = 1;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 	} else {
1710Sstevel@tonic-gate 		if (cmd != DDI_DETACH) {
1720Sstevel@tonic-gate 			return (DDI_FAILURE);
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 	return (DDI_SUCCESS);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /* ARGSUSED */
1800Sstevel@tonic-gate static int
cvcr_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1810Sstevel@tonic-gate cvcr_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	register int error;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	switch (infocmd) {
1860Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1870Sstevel@tonic-gate 		if (cvcr_dip == NULL) {
1880Sstevel@tonic-gate 			error = DDI_FAILURE;
1890Sstevel@tonic-gate 		} else {
1900Sstevel@tonic-gate 			*result = (void *)cvcr_dip;
1910Sstevel@tonic-gate 			error = DDI_SUCCESS;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1950Sstevel@tonic-gate 		*result = (void *)0;
1960Sstevel@tonic-gate 		error = DDI_SUCCESS;
1970Sstevel@tonic-gate 		break;
1980Sstevel@tonic-gate 	default:
1990Sstevel@tonic-gate 		error = DDI_FAILURE;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	return (error);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /* ARGSUSED */
2050Sstevel@tonic-gate static int
cvcr_open(queue_t * q,dev_t * dev,int flag,int sflag,cred_t * cred)2060Sstevel@tonic-gate cvcr_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	WR(q)->q_ptr = q->q_ptr = (char *)2;
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * call into the cvc driver to register our queue.  cvc will use
2110Sstevel@tonic-gate 	 * our queue to send console output data upstream (our stream)to
2120Sstevel@tonic-gate 	 * cvcd which has us open and is reading console data.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	if (cvc_register(RD(q)) == -1) {
215930Smathue 		cmn_err(CE_WARN, "cvcr_open: cvc_register failed for q = 0x%p",
216*11311SSurya.Prakki@Sun.COM 		    (void *)q);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 	return (0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /* ARGSUSED */
2220Sstevel@tonic-gate static int
cvcr_close(queue_t * q,int flag,cred_t * cred)2230Sstevel@tonic-gate cvcr_close(queue_t *q, int flag, cred_t *cred)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * call into the cvc driver to un-register our queue.  cvc will
2270Sstevel@tonic-gate 	 * no longer use our queue to send console output data upstream.
2280Sstevel@tonic-gate 	 */
229*11311SSurya.Prakki@Sun.COM 	(void) cvc_unregister(RD(q));
2300Sstevel@tonic-gate 	WR(q)->q_ptr = q->q_ptr = NULL;
2310Sstevel@tonic-gate 	return (0);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static int
cvcr_wput(queue_t * q,mblk_t * mp)2350Sstevel@tonic-gate cvcr_wput(queue_t *q, mblk_t *mp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Handle network-transmitted control messages
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_IOCTL) {
2410Sstevel@tonic-gate 		cvcr_ioctl(q, mp);
2420Sstevel@tonic-gate 		return (0);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Call into the cvc driver to put console input data on
2460Sstevel@tonic-gate 	 * its upstream queue to be picked up by the console driver.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	if (!cvc_redir(mp))
2490Sstevel@tonic-gate 		freemsg(mp);
2500Sstevel@tonic-gate 	return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static void
cvcr_ioctl(queue_t * q,mblk_t * mp)2540Sstevel@tonic-gate cvcr_ioctl(queue_t *q, mblk_t *mp)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * If this is a CVC_BREAK, drop to OBP/kmdb from here rather than
2600Sstevel@tonic-gate 	 * passing the mblk on to cvc.  If it is a disconnect, pass it on to
2610Sstevel@tonic-gate 	 * cvc.  Nothing else is currently supported, so NAK all others.
2620Sstevel@tonic-gate 	 * Note that cvc_redir doesn't free the mblk passed in, so we can
2630Sstevel@tonic-gate 	 * reuse it for ACKing.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (iocp->ioc_cmd == CVC_BREAK) {
2660Sstevel@tonic-gate 		abort_sequence_enter((char *)NULL);
2670Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
2680Sstevel@tonic-gate 	} else if (iocp->ioc_cmd == CVC_DISCONNECT) {
2690Sstevel@tonic-gate 		(void) cvc_redir(mp);
2700Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
2710Sstevel@tonic-gate 	} else {
2720Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate }
275