xref: /onnv-gate/usr/src/uts/common/io/zcons.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
53813Sdp  * Common Development and Distribution License (the "License").
63813Sdp  * 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  * Zone Console Driver.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * This driver, derived from the pts/ptm drivers, is the pseudo console driver
310Sstevel@tonic-gate  * for system zones.  Its implementation is straightforward.  Each instance
320Sstevel@tonic-gate  * of the driver represents a global-zone/local-zone pair (this maps in a
330Sstevel@tonic-gate  * straightforward way to the commonly used terminal notion of "master side"
340Sstevel@tonic-gate  * and "slave side", and we use that terminology throughout).
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * Instances of zcons are onlined as children of /pseudo/zconsnex@1/
370Sstevel@tonic-gate  * by zoneadmd in userland, using the devctl framework; thus the driver
380Sstevel@tonic-gate  * does not need to maintain any sort of "admin" node.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * The driver shuttles I/O from master side to slave side and back.  In a break
410Sstevel@tonic-gate  * from the pts/ptm semantics, if one side is not open, I/O directed towards
420Sstevel@tonic-gate  * it will simply be discarded.  This is so that if zoneadmd is not holding
430Sstevel@tonic-gate  * the master side console open (i.e. it has died somehow), processes in
440Sstevel@tonic-gate  * the zone do not experience any errors and I/O to the console does not
450Sstevel@tonic-gate  * hang.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * TODO: we may want to revisit the other direction; i.e. we may want
480Sstevel@tonic-gate  * zoneadmd to be able to detect whether no zone processes are holding the
490Sstevel@tonic-gate  * console open, an unusual situation.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
540Sstevel@tonic-gate #include <sys/conf.h>
550Sstevel@tonic-gate #include <sys/cred.h>
560Sstevel@tonic-gate #include <sys/ddi.h>
570Sstevel@tonic-gate #include <sys/debug.h>
580Sstevel@tonic-gate #include <sys/devops.h>
590Sstevel@tonic-gate #include <sys/errno.h>
600Sstevel@tonic-gate #include <sys/file.h>
610Sstevel@tonic-gate #include <sys/modctl.h>
620Sstevel@tonic-gate #include <sys/param.h>
630Sstevel@tonic-gate #include <sys/stat.h>
640Sstevel@tonic-gate #include <sys/stream.h>
650Sstevel@tonic-gate #include <sys/stropts.h>
660Sstevel@tonic-gate #include <sys/strsun.h>
670Sstevel@tonic-gate #include <sys/sunddi.h>
680Sstevel@tonic-gate #include <sys/sysmacros.h>
690Sstevel@tonic-gate #include <sys/systm.h>
700Sstevel@tonic-gate #include <sys/types.h>
710Sstevel@tonic-gate #include <sys/zcons.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
740Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
750Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
780Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *);
790Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *);
800Sstevel@tonic-gate static void zc_rsrv(queue_t *);
810Sstevel@tonic-gate static void zc_wsrv(queue_t *);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * The instance number is encoded in the dev_t in the minor number; the lowest
850Sstevel@tonic-gate  * bit of the minor number is used to track the master vs. slave side of the
860Sstevel@tonic-gate  * virtual console.  The rest of the bits in the minor number are the instance.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate #define	ZC_MASTER_MINOR	0
890Sstevel@tonic-gate #define	ZC_SLAVE_MINOR	1
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	ZC_INSTANCE(x)	(getminor((x)) >> 1)
920Sstevel@tonic-gate #define	ZC_NODE(x)	(getminor((x)) & 0x01)
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int zcons_debug = 0;
950Sstevel@tonic-gate #define	DBG(a)   if (zcons_debug) cmn_err(CE_NOTE, a)
960Sstevel@tonic-gate #define	DBG1(a, b)   if (zcons_debug) cmn_err(CE_NOTE, a, b)
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * Zone Console Pseudo Terminal Module: stream data structure definitions
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate static struct module_info zc_info = {
1030Sstevel@tonic-gate 	31337,	/* c0z we r hAx0rs */
1040Sstevel@tonic-gate 	"zcons",
1050Sstevel@tonic-gate 	0,
1060Sstevel@tonic-gate 	INFPSZ,
1070Sstevel@tonic-gate 	2048,
1080Sstevel@tonic-gate 	128
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static struct qinit zc_rinit = {
1120Sstevel@tonic-gate 	NULL,
1130Sstevel@tonic-gate 	(int (*)()) zc_rsrv,
1140Sstevel@tonic-gate 	zc_open,
1150Sstevel@tonic-gate 	zc_close,
1160Sstevel@tonic-gate 	NULL,
1170Sstevel@tonic-gate 	&zc_info,
1180Sstevel@tonic-gate 	NULL
1190Sstevel@tonic-gate };
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static struct qinit zc_winit = {
1220Sstevel@tonic-gate 	(int (*)()) zc_wput,
1230Sstevel@tonic-gate 	(int (*)()) zc_wsrv,
1240Sstevel@tonic-gate 	NULL,
1250Sstevel@tonic-gate 	NULL,
1260Sstevel@tonic-gate 	NULL,
1270Sstevel@tonic-gate 	&zc_info,
1280Sstevel@tonic-gate 	NULL
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static struct streamtab zc_tab_info = {
1320Sstevel@tonic-gate 	&zc_rinit,
1330Sstevel@tonic-gate 	&zc_winit,
1340Sstevel@tonic-gate 	NULL,
1350Sstevel@tonic-gate 	NULL
1360Sstevel@tonic-gate };
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define	ZC_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev,	zc_attach, zc_detach, nodev, \
144*7656SSherry.Moore@Sun.COM 	zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * Module linkage information for the kernel.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static struct modldrv modldrv = {
1514959Sedp 	&mod_driverops, 	/* Type of module (this is a pseudo driver) */
1524959Sedp 	"Zone console driver",	/* description of module */
1534959Sedp 	&zc_ops			/* driver ops */
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct modlinkage modlinkage = {
1570Sstevel@tonic-gate 	MODREV_1,
1580Sstevel@tonic-gate 	&modldrv,
1590Sstevel@tonic-gate 	NULL
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate typedef struct zc_state {
1630Sstevel@tonic-gate 	dev_info_t *zc_devinfo;
1640Sstevel@tonic-gate 	queue_t *zc_master_rdq;
1650Sstevel@tonic-gate 	queue_t *zc_slave_rdq;
1660Sstevel@tonic-gate 	int zc_state;
1670Sstevel@tonic-gate } zc_state_t;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate #define	ZC_STATE_MOPEN	0x01
1700Sstevel@tonic-gate #define	ZC_STATE_SOPEN	0x02
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static void *zc_soft_state;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate int
1750Sstevel@tonic-gate _init(void)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	int err;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&zc_soft_state,
1800Sstevel@tonic-gate 	    sizeof (zc_state_t), 0)) != 0) {
1810Sstevel@tonic-gate 		return (err);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0)
1850Sstevel@tonic-gate 		ddi_soft_state_fini(zc_soft_state);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	return (err);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate int
1920Sstevel@tonic-gate _fini(void)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	int err;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0) {
1970Sstevel@tonic-gate 		return (err);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	ddi_soft_state_fini(&zc_soft_state);
2010Sstevel@tonic-gate 	return (0);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate int
2050Sstevel@tonic-gate _info(struct modinfo *modinfop)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static int
2110Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	zc_state_t *zcs;
2140Sstevel@tonic-gate 	int instance;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2170Sstevel@tonic-gate 		return (DDI_FAILURE);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2200Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
2210Sstevel@tonic-gate 		return (DDI_FAILURE);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR,
2240Sstevel@tonic-gate 	    instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
2250Sstevel@tonic-gate 	    (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR,
2260Sstevel@tonic-gate 	    instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
2270Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2280Sstevel@tonic-gate 		ddi_soft_state_free(zc_soft_state, instance);
2290Sstevel@tonic-gate 		return (DDI_FAILURE);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) {
2330Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2340Sstevel@tonic-gate 		ddi_soft_state_free(zc_soft_state, instance);
2350Sstevel@tonic-gate 		return (DDI_FAILURE);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	zcs->zc_devinfo = dip;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	return (DDI_SUCCESS);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate static int
2430Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	zc_state_t *zcs;
2460Sstevel@tonic-gate 	int instance;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2490Sstevel@tonic-gate 		return (DDI_FAILURE);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2520Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
2530Sstevel@tonic-gate 		return (DDI_FAILURE);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) ||
2560Sstevel@tonic-gate 	    (zcs->zc_state & ZC_STATE_SOPEN)) {
2570Sstevel@tonic-gate 		DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
2580Sstevel@tonic-gate 		return (DDI_FAILURE);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2620Sstevel@tonic-gate 	ddi_soft_state_free(zc_soft_state, instance);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	return (DDI_SUCCESS);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * zc_getinfo()
2690Sstevel@tonic-gate  *	getinfo(9e) entrypoint.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate /*ARGSUSED*/
2720Sstevel@tonic-gate static int
2730Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	zc_state_t *zcs;
2760Sstevel@tonic-gate 	int instance = ZC_INSTANCE((dev_t)arg);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	switch (infocmd) {
2790Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2800Sstevel@tonic-gate 		if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
2810Sstevel@tonic-gate 			return (DDI_FAILURE);
2820Sstevel@tonic-gate 		*result = zcs->zc_devinfo;
2830Sstevel@tonic-gate 		return (DDI_SUCCESS);
2840Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2850Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
2860Sstevel@tonic-gate 		return (DDI_SUCCESS);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	return (DDI_FAILURE);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Return the equivalent queue from the other side of the relationship.
2930Sstevel@tonic-gate  * e.g.: given the slave's write queue, return the master's write queue.
2940Sstevel@tonic-gate  */
2950Sstevel@tonic-gate static queue_t *
2960Sstevel@tonic-gate zc_switch(queue_t *qp)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
2990Sstevel@tonic-gate 	ASSERT(zcs != NULL);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq)
3020Sstevel@tonic-gate 		return (zcs->zc_slave_rdq);
3030Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL)
3040Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_slave_rdq));
3050Sstevel@tonic-gate 	else if (qp == zcs->zc_slave_rdq)
3060Sstevel@tonic-gate 		return (zcs->zc_master_rdq);
3070Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL)
3080Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_master_rdq));
3090Sstevel@tonic-gate 	else
3100Sstevel@tonic-gate 		return (NULL);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * For debugging and outputting messages.  Returns the name of the side of
3150Sstevel@tonic-gate  * the relationship associated with this queue.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static const char *
3180Sstevel@tonic-gate zc_side(queue_t *qp)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
3210Sstevel@tonic-gate 	ASSERT(zcs != NULL);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq ||
3240Sstevel@tonic-gate 	    OTHERQ(qp) == zcs->zc_master_rdq) {
3250Sstevel@tonic-gate 		return ("master");
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq);
3280Sstevel@tonic-gate 	return ("slave");
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*ARGSUSED*/
3320Sstevel@tonic-gate static int
3330Sstevel@tonic-gate zc_master_open(zc_state_t *zcs,
3340Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
3350Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
3360Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
3370Sstevel@tonic-gate     int		sflag,	/* open state flag */
3380Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	mblk_t *mop;
3410Sstevel@tonic-gate 	struct stroptions *sop;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Enforce exclusivity on the master side; the only consumer should
3450Sstevel@tonic-gate 	 * be the zoneadmd for the zone.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
3480Sstevel@tonic-gate 		return (EBUSY);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
3510Sstevel@tonic-gate 		DBG("zc_master_open(): mop allocation failed\n");
3520Sstevel@tonic-gate 		return (ENOMEM);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_MOPEN;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
3590Sstevel@tonic-gate 	 * read and write sides of the queue.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
3620Sstevel@tonic-gate 	qprocson(rqp);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * Following qprocson(), the master side is fully plumbed into the
3660Sstevel@tonic-gate 	 * STREAM and may send/receive messages.  Setting zcs->zc_master_rdq
3670Sstevel@tonic-gate 	 * will allow the slave to send messages to us (the master).
3680Sstevel@tonic-gate 	 * This cannot occur before qprocson() because the master is not
3690Sstevel@tonic-gate 	 * ready to process them until that point.
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	zcs->zc_master_rdq = rqp;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
3750Sstevel@tonic-gate 	 * controlling tty as needed.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
3780Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
3793813Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
3800Sstevel@tonic-gate 	if (oflag & FNOCTTY)
3810Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
3820Sstevel@tonic-gate 	else
3830Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
3840Sstevel@tonic-gate 	sop->so_hiwat = 512;
3850Sstevel@tonic-gate 	sop->so_lowat = 256;
3860Sstevel@tonic-gate 	putnext(rqp, mop);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*ARGSUSED*/
3920Sstevel@tonic-gate static int
3930Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs,
3940Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
3950Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
3960Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
3970Sstevel@tonic-gate     int		sflag,	/* open state flag */
3980Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	mblk_t *mop;
4010Sstevel@tonic-gate 	struct stroptions *sop;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * The slave side can be opened as many times as needed.
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
4070Sstevel@tonic-gate 		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
4080Sstevel@tonic-gate 		return (0);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
4120Sstevel@tonic-gate 		DBG("zc_slave_open(): mop allocation failed\n");
4130Sstevel@tonic-gate 		return (ENOMEM);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_SOPEN;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/*
4190Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
4200Sstevel@tonic-gate 	 * read and write sides of the queue.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	qprocson(rqp);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/*
4270Sstevel@tonic-gate 	 * Must follow qprocson(), since we aren't ready to process until then.
4280Sstevel@tonic-gate 	 */
4290Sstevel@tonic-gate 	zcs->zc_slave_rdq = rqp;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
4330Sstevel@tonic-gate 	 * controlling tty as needed.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4360Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
4373813Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
4380Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
4390Sstevel@tonic-gate 	sop->so_hiwat = 512;
4400Sstevel@tonic-gate 	sop->so_lowat = 256;
4410Sstevel@tonic-gate 	putnext(rqp, mop);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	return (0);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate static int
4500Sstevel@tonic-gate zc_open(queue_t *rqp,		/* pointer to the read side queue */
4510Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
4520Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
4530Sstevel@tonic-gate 	int	sflag,		/* open state flag */
4540Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	int instance = ZC_INSTANCE(*devp);
4570Sstevel@tonic-gate 	int ret;
4580Sstevel@tonic-gate 	zc_state_t *zcs;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (sflag != 0)
4610Sstevel@tonic-gate 		return (EINVAL);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4640Sstevel@tonic-gate 		return (ENXIO);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	switch (ZC_NODE(*devp)) {
4670Sstevel@tonic-gate 	case ZC_MASTER_MINOR:
4680Sstevel@tonic-gate 		ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp);
4690Sstevel@tonic-gate 		break;
4700Sstevel@tonic-gate 	case ZC_SLAVE_MINOR:
4710Sstevel@tonic-gate 		ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp);
4720Sstevel@tonic-gate 		break;
4730Sstevel@tonic-gate 	default:
4740Sstevel@tonic-gate 		ret = ENXIO;
4750Sstevel@tonic-gate 		break;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	return (ret);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * close(9e) entrypoint.
4830Sstevel@tonic-gate  */
4840Sstevel@tonic-gate /*ARGSUSED1*/
4850Sstevel@tonic-gate static int
4860Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	queue_t *wqp;
4890Sstevel@tonic-gate 	mblk_t	*bp;
4900Sstevel@tonic-gate 	zc_state_t *zcs;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	zcs = (zc_state_t *)rqp->q_ptr;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (rqp == zcs->zc_master_rdq) {
4950Sstevel@tonic-gate 		DBG("Closing master side");
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		zcs->zc_master_rdq = NULL;
4980Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_MOPEN;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		/*
5010Sstevel@tonic-gate 		 * qenable slave side write queue so that it can flush
5020Sstevel@tonic-gate 		 * its messages as master's read queue is going away
5030Sstevel@tonic-gate 		 */
5040Sstevel@tonic-gate 		if (zcs->zc_slave_rdq != NULL) {
5050Sstevel@tonic-gate 			qenable(WR(zcs->zc_slave_rdq));
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		qprocsoff(rqp);
5090Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	} else if (rqp == zcs->zc_slave_rdq) {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		DBG("Closing slave side");
5140Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_SOPEN;
5150Sstevel@tonic-gate 		zcs->zc_slave_rdq = NULL;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		wqp = WR(rqp);
5180Sstevel@tonic-gate 		while ((bp = getq(wqp)) != NULL) {
5190Sstevel@tonic-gate 			if (zcs->zc_master_rdq != NULL)
5200Sstevel@tonic-gate 				putnext(zcs->zc_master_rdq, bp);
5210Sstevel@tonic-gate 			else if (bp->b_datap->db_type == M_IOCTL)
5220Sstevel@tonic-gate 				miocnak(wqp, bp, 0, 0);
5230Sstevel@tonic-gate 			else
5240Sstevel@tonic-gate 				freemsg(bp);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		/*
5280Sstevel@tonic-gate 		 * Qenable master side write queue so that it can flush its
5290Sstevel@tonic-gate 		 * messages as slaves's read queue is going away.
5300Sstevel@tonic-gate 		 */
5310Sstevel@tonic-gate 		if (zcs->zc_master_rdq != NULL)
5320Sstevel@tonic-gate 			qenable(WR(zcs->zc_master_rdq));
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		qprocsoff(rqp);
5350Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	return (0);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate static void
5420Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	mblk_t *nmp;
5450Sstevel@tonic-gate 	DBG1("M_FLUSH on %s side", zc_side(qp));
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHW) {
5480Sstevel@tonic-gate 		DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
5490Sstevel@tonic-gate 		flushq(qp, FLUSHDATA);
5500Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
5510Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHR) == 0) {
5520Sstevel@tonic-gate 			/*
5530Sstevel@tonic-gate 			 * FLUSHW only. Change to FLUSHR and putnext other side,
5540Sstevel@tonic-gate 			 * then we are done.
5550Sstevel@tonic-gate 			 */
5560Sstevel@tonic-gate 			*mp->b_rptr |= FLUSHR;
5570Sstevel@tonic-gate 			if (zc_switch(RD(qp)) != NULL) {
5580Sstevel@tonic-gate 				putnext(zc_switch(RD(qp)), mp);
5590Sstevel@tonic-gate 				return;
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 		} else if ((zc_switch(RD(qp)) != NULL) &&
5620Sstevel@tonic-gate 		    (nmp = copyb(mp)) != NULL) {
5630Sstevel@tonic-gate 			/*
5640Sstevel@tonic-gate 			 * It is a FLUSHRW; we copy the mblk and send
5650Sstevel@tonic-gate 			 * it to the other side, since we still need to use
5660Sstevel@tonic-gate 			 * the mblk in FLUSHR processing, below.
5670Sstevel@tonic-gate 			 */
5680Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), nmp);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHR) {
5730Sstevel@tonic-gate 		DBG("qreply(qp) turning FLUSHR around\n");
5740Sstevel@tonic-gate 		qreply(qp, mp);
5750Sstevel@tonic-gate 		return;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	freemsg(mp);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * wput(9E) is symmetric for master and slave sides, so this handles both
5820Sstevel@tonic-gate  * without splitting the codepath.
5830Sstevel@tonic-gate  *
5840Sstevel@tonic-gate  * zc_wput() looks at the other side; if there is no process holding that
5850Sstevel@tonic-gate  * side open, it frees the message.  This prevents processes from hanging
5860Sstevel@tonic-gate  * if no one is holding open the console.  Otherwise, it putnext's high
5870Sstevel@tonic-gate  * priority messages, putnext's normal messages if possible, and otherwise
5880Sstevel@tonic-gate  * enqueues the messages; in the case that something is enqueued, wsrv(9E)
5890Sstevel@tonic-gate  * will take care of eventually shuttling I/O to the other side.
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate static void
5920Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	DBG1("entering zc_wput, %s side", zc_side(qp));
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
6010Sstevel@tonic-gate 		DBG1("wput to %s side (no one listening)", zc_side(qp));
6020Sstevel@tonic-gate 		switch (type) {
6030Sstevel@tonic-gate 		case M_FLUSH:
6040Sstevel@tonic-gate 			handle_mflush(qp, mp);
6050Sstevel@tonic-gate 			break;
6060Sstevel@tonic-gate 		case M_IOCTL:
6070Sstevel@tonic-gate 			miocnak(qp, mp, 0, 0);
6080Sstevel@tonic-gate 			break;
6090Sstevel@tonic-gate 		default:
6100Sstevel@tonic-gate 			freemsg(mp);
6110Sstevel@tonic-gate 			break;
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 		return;
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (type >= QPCTL) {
6170Sstevel@tonic-gate 		DBG1("(hipri) wput, %s side", zc_side(qp));
6180Sstevel@tonic-gate 		switch (type) {
6190Sstevel@tonic-gate 		case M_READ:		/* supposedly from ldterm? */
6200Sstevel@tonic-gate 			DBG("zc_wput: tossing M_READ\n");
6210Sstevel@tonic-gate 			freemsg(mp);
6220Sstevel@tonic-gate 			break;
6230Sstevel@tonic-gate 		case M_FLUSH:
6240Sstevel@tonic-gate 			handle_mflush(qp, mp);
6250Sstevel@tonic-gate 			break;
6260Sstevel@tonic-gate 		default:
6270Sstevel@tonic-gate 			/*
6280Sstevel@tonic-gate 			 * Put this to the other side.
6290Sstevel@tonic-gate 			 */
6300Sstevel@tonic-gate 			ASSERT(zc_switch(RD(qp)) != NULL);
6310Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), mp);
6320Sstevel@tonic-gate 			break;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		DBG1("done (hipri) wput, %s side", zc_side(qp));
6350Sstevel@tonic-gate 		return;
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Only putnext if there isn't already something in the queue.
6400Sstevel@tonic-gate 	 * otherwise things would wind up out of order.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
6430Sstevel@tonic-gate 		DBG("wput: putting message to other side\n");
6440Sstevel@tonic-gate 		putnext(RD(zc_switch(qp)), mp);
6450Sstevel@tonic-gate 	} else {
6460Sstevel@tonic-gate 		DBG("wput: putting msg onto queue\n");
6470Sstevel@tonic-gate 		(void) putq(qp, mp);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 	DBG1("done wput, %s side", zc_side(qp));
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate  * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
6540Sstevel@tonic-gate  * without splitting up the codepath.
6550Sstevel@tonic-gate  *
6560Sstevel@tonic-gate  * Enable the write side of the partner.  This triggers the partner to send
6570Sstevel@tonic-gate  * messages queued on its write side to this queue's read side.
6580Sstevel@tonic-gate  */
6590Sstevel@tonic-gate static void
6600Sstevel@tonic-gate zc_rsrv(queue_t *qp)
6610Sstevel@tonic-gate {
6620Sstevel@tonic-gate 	zc_state_t *zcs;
6630Sstevel@tonic-gate 	zcs = (zc_state_t *)qp->q_ptr;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * Care must be taken here, as either of the master or slave side
6670Sstevel@tonic-gate 	 * qptr could be NULL.
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq);
6700Sstevel@tonic-gate 	if (zc_switch(qp) == NULL) {
6710Sstevel@tonic-gate 		DBG("zc_rsrv: other side isn't listening\n");
6720Sstevel@tonic-gate 		return;
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 	qenable(WR(zc_switch(qp)));
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate  * This routine is symmetric for master and slave, so it handles both without
6790Sstevel@tonic-gate  * splitting up the codepath.
6800Sstevel@tonic-gate  *
6810Sstevel@tonic-gate  * If there are messages on this queue that can be sent to the other, send
6820Sstevel@tonic-gate  * them via putnext(). Else, if queued messages cannot be sent, leave them
6830Sstevel@tonic-gate  * on this queue.
6840Sstevel@tonic-gate  */
6850Sstevel@tonic-gate static void
6860Sstevel@tonic-gate zc_wsrv(queue_t *qp)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	mblk_t *mp;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	DBG1("zc_wsrv master (%s) side", zc_side(qp));
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Partner has no read queue, so take the data, and throw it away.
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
6960Sstevel@tonic-gate 		DBG("zc_wsrv: other side isn't listening");
6970Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
6980Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
6990Sstevel@tonic-gate 				miocnak(qp, mp, 0, 0);
7000Sstevel@tonic-gate 			else
7010Sstevel@tonic-gate 				freemsg(mp);
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
7040Sstevel@tonic-gate 		return;
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	/*
7080Sstevel@tonic-gate 	 * while there are messages on this write queue...
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * Due to the way zc_wput is implemented, we should never
7130Sstevel@tonic-gate 		 * see a control message here.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type < QPCTL);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
7180Sstevel@tonic-gate 			DBG("wsrv: send message to other side\n");
7190Sstevel@tonic-gate 			putnext(RD(zc_switch(qp)), mp);
7200Sstevel@tonic-gate 		} else {
7210Sstevel@tonic-gate 			DBG("wsrv: putting msg back on queue\n");
7220Sstevel@tonic-gate 			(void) putbq(qp, mp);
7230Sstevel@tonic-gate 			break;
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate }
727