xref: /titanic_54/usr/src/uts/common/io/zcons.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
527e6fb21Sdp  * Common Development and Distribution License (the "License").
627e6fb21Sdp  * 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  * Zone Console Driver.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * This driver, derived from the pts/ptm drivers, is the pseudo console driver
317c478bd9Sstevel@tonic-gate  * for system zones.  Its implementation is straightforward.  Each instance
327c478bd9Sstevel@tonic-gate  * of the driver represents a global-zone/local-zone pair (this maps in a
337c478bd9Sstevel@tonic-gate  * straightforward way to the commonly used terminal notion of "master side"
347c478bd9Sstevel@tonic-gate  * and "slave side", and we use that terminology throughout).
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Instances of zcons are onlined as children of /pseudo/zconsnex@1/
377c478bd9Sstevel@tonic-gate  * by zoneadmd in userland, using the devctl framework; thus the driver
387c478bd9Sstevel@tonic-gate  * does not need to maintain any sort of "admin" node.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The driver shuttles I/O from master side to slave side and back.  In a break
417c478bd9Sstevel@tonic-gate  * from the pts/ptm semantics, if one side is not open, I/O directed towards
427c478bd9Sstevel@tonic-gate  * it will simply be discarded.  This is so that if zoneadmd is not holding
437c478bd9Sstevel@tonic-gate  * the master side console open (i.e. it has died somehow), processes in
447c478bd9Sstevel@tonic-gate  * the zone do not experience any errors and I/O to the console does not
457c478bd9Sstevel@tonic-gate  * hang.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * TODO: we may want to revisit the other direction; i.e. we may want
487c478bd9Sstevel@tonic-gate  * zoneadmd to be able to detect whether no zone processes are holding the
497c478bd9Sstevel@tonic-gate  * console open, an unusual situation.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
557c478bd9Sstevel@tonic-gate #include <sys/cred.h>
567c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
577c478bd9Sstevel@tonic-gate #include <sys/debug.h>
587c478bd9Sstevel@tonic-gate #include <sys/devops.h>
597c478bd9Sstevel@tonic-gate #include <sys/errno.h>
607c478bd9Sstevel@tonic-gate #include <sys/file.h>
617c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/param.h>
637c478bd9Sstevel@tonic-gate #include <sys/stat.h>
647c478bd9Sstevel@tonic-gate #include <sys/stream.h>
657c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
667c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
677c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
687c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
697c478bd9Sstevel@tonic-gate #include <sys/systm.h>
707c478bd9Sstevel@tonic-gate #include <sys/types.h>
717c478bd9Sstevel@tonic-gate #include <sys/zcons.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
747c478bd9Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
757c478bd9Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
787c478bd9Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *);
797c478bd9Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *);
807c478bd9Sstevel@tonic-gate static void zc_rsrv(queue_t *);
817c478bd9Sstevel@tonic-gate static void zc_wsrv(queue_t *);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * The instance number is encoded in the dev_t in the minor number; the lowest
857c478bd9Sstevel@tonic-gate  * bit of the minor number is used to track the master vs. slave side of the
867c478bd9Sstevel@tonic-gate  * virtual console.  The rest of the bits in the minor number are the instance.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #define	ZC_MASTER_MINOR	0
897c478bd9Sstevel@tonic-gate #define	ZC_SLAVE_MINOR	1
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	ZC_INSTANCE(x)	(getminor((x)) >> 1)
927c478bd9Sstevel@tonic-gate #define	ZC_NODE(x)	(getminor((x)) & 0x01)
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int zcons_debug = 0;
957c478bd9Sstevel@tonic-gate #define	DBG(a)   if (zcons_debug) cmn_err(CE_NOTE, a)
967c478bd9Sstevel@tonic-gate #define	DBG1(a, b)   if (zcons_debug) cmn_err(CE_NOTE, a, b)
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Zone Console Pseudo Terminal Module: stream data structure definitions
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static struct module_info zc_info = {
1037c478bd9Sstevel@tonic-gate 	31337,	/* c0z we r hAx0rs */
1047c478bd9Sstevel@tonic-gate 	"zcons",
1057c478bd9Sstevel@tonic-gate 	0,
1067c478bd9Sstevel@tonic-gate 	INFPSZ,
1077c478bd9Sstevel@tonic-gate 	2048,
1087c478bd9Sstevel@tonic-gate 	128
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct qinit zc_rinit = {
1127c478bd9Sstevel@tonic-gate 	NULL,
1137c478bd9Sstevel@tonic-gate 	(int (*)()) zc_rsrv,
1147c478bd9Sstevel@tonic-gate 	zc_open,
1157c478bd9Sstevel@tonic-gate 	zc_close,
1167c478bd9Sstevel@tonic-gate 	NULL,
1177c478bd9Sstevel@tonic-gate 	&zc_info,
1187c478bd9Sstevel@tonic-gate 	NULL
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static struct qinit zc_winit = {
1227c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wput,
1237c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wsrv,
1247c478bd9Sstevel@tonic-gate 	NULL,
1257c478bd9Sstevel@tonic-gate 	NULL,
1267c478bd9Sstevel@tonic-gate 	NULL,
1277c478bd9Sstevel@tonic-gate 	&zc_info,
1287c478bd9Sstevel@tonic-gate 	NULL
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static struct streamtab zc_tab_info = {
1327c478bd9Sstevel@tonic-gate 	&zc_rinit,
1337c478bd9Sstevel@tonic-gate 	&zc_winit,
1347c478bd9Sstevel@tonic-gate 	NULL,
1357c478bd9Sstevel@tonic-gate 	NULL
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	ZC_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev,	zc_attach, zc_detach, nodev, \
144*19397407SSherry Moore 	zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
151c6f42f0eSedp 	&mod_driverops, 	/* Type of module (this is a pseudo driver) */
152c6f42f0eSedp 	"Zone console driver",	/* description of module */
1537c478bd9Sstevel@tonic-gate 	&zc_ops			/* driver ops */
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1577c478bd9Sstevel@tonic-gate 	MODREV_1,
1587c478bd9Sstevel@tonic-gate 	&modldrv,
1597c478bd9Sstevel@tonic-gate 	NULL
1607c478bd9Sstevel@tonic-gate };
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate typedef struct zc_state {
1637c478bd9Sstevel@tonic-gate 	dev_info_t *zc_devinfo;
1647c478bd9Sstevel@tonic-gate 	queue_t *zc_master_rdq;
1657c478bd9Sstevel@tonic-gate 	queue_t *zc_slave_rdq;
1667c478bd9Sstevel@tonic-gate 	int zc_state;
1677c478bd9Sstevel@tonic-gate } zc_state_t;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #define	ZC_STATE_MOPEN	0x01
1707c478bd9Sstevel@tonic-gate #define	ZC_STATE_SOPEN	0x02
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static void *zc_soft_state;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate int
1757c478bd9Sstevel@tonic-gate _init(void)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int err;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&zc_soft_state,
1807c478bd9Sstevel@tonic-gate 	    sizeof (zc_state_t), 0)) != 0) {
1817c478bd9Sstevel@tonic-gate 		return (err);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0)
1857c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(zc_soft_state);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (err);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate _fini(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	int err;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0) {
1977c478bd9Sstevel@tonic-gate 		return (err);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&zc_soft_state);
2017c478bd9Sstevel@tonic-gate 	return (0);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate int
2057c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static int
2117c478bd9Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
2147c478bd9Sstevel@tonic-gate 	int instance;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2207c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
2217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR,
2247c478bd9Sstevel@tonic-gate 	    instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
2257c478bd9Sstevel@tonic-gate 	    (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR,
2267c478bd9Sstevel@tonic-gate 	    instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
2277c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2287c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(zc_soft_state, instance);
2297c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL) {
2337c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2347c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(zc_soft_state, instance);
2357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	zcs->zc_devinfo = dip;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static int
2437c478bd9Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
2467c478bd9Sstevel@tonic-gate 	int instance;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2527c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
2537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) ||
2567c478bd9Sstevel@tonic-gate 	    (zcs->zc_state & ZC_STATE_SOPEN)) {
2577c478bd9Sstevel@tonic-gate 		DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
2587c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2627c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(zc_soft_state, instance);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * zc_getinfo()
2697c478bd9Sstevel@tonic-gate  *	getinfo(9e) entrypoint.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2727c478bd9Sstevel@tonic-gate static int
2737c478bd9Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
2767c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE((dev_t)arg);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2797c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2807c478bd9Sstevel@tonic-gate 		if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
2817c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2827c478bd9Sstevel@tonic-gate 		*result = zcs->zc_devinfo;
2837c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2847c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2857c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
2867c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * Return the equivalent queue from the other side of the relationship.
2937c478bd9Sstevel@tonic-gate  * e.g.: given the slave's write queue, return the master's write queue.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate static queue_t *
2967c478bd9Sstevel@tonic-gate zc_switch(queue_t *qp)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
2997c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq)
3027c478bd9Sstevel@tonic-gate 		return (zcs->zc_slave_rdq);
3037c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL)
3047c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_slave_rdq));
3057c478bd9Sstevel@tonic-gate 	else if (qp == zcs->zc_slave_rdq)
3067c478bd9Sstevel@tonic-gate 		return (zcs->zc_master_rdq);
3077c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL)
3087c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_master_rdq));
3097c478bd9Sstevel@tonic-gate 	else
3107c478bd9Sstevel@tonic-gate 		return (NULL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * For debugging and outputting messages.  Returns the name of the side of
3157c478bd9Sstevel@tonic-gate  * the relationship associated with this queue.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static const char *
3187c478bd9Sstevel@tonic-gate zc_side(queue_t *qp)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
3217c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq ||
3247c478bd9Sstevel@tonic-gate 	    OTHERQ(qp) == zcs->zc_master_rdq) {
3257c478bd9Sstevel@tonic-gate 		return ("master");
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq);
3287c478bd9Sstevel@tonic-gate 	return ("slave");
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3327c478bd9Sstevel@tonic-gate static int
3337c478bd9Sstevel@tonic-gate zc_master_open(zc_state_t *zcs,
3347c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
3357c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
3367c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
3377c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
3387c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	mblk_t *mop;
3417c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Enforce exclusivity on the master side; the only consumer should
3457c478bd9Sstevel@tonic-gate 	 * be the zoneadmd for the zone.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
3487c478bd9Sstevel@tonic-gate 		return (EBUSY);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
3517c478bd9Sstevel@tonic-gate 		DBG("zc_master_open(): mop allocation failed\n");
3527c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_MOPEN;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
3597c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
3627c478bd9Sstevel@tonic-gate 	qprocson(rqp);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Following qprocson(), the master side is fully plumbed into the
3667c478bd9Sstevel@tonic-gate 	 * STREAM and may send/receive messages.  Setting zcs->zc_master_rdq
3677c478bd9Sstevel@tonic-gate 	 * will allow the slave to send messages to us (the master).
3687c478bd9Sstevel@tonic-gate 	 * This cannot occur before qprocson() because the master is not
3697c478bd9Sstevel@tonic-gate 	 * ready to process them until that point.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	zcs->zc_master_rdq = rqp;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
3757c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
3787c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
37927e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
3807c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
3817c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
3827c478bd9Sstevel@tonic-gate 	else
3837c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
3847c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
3857c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
3867c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	return (0);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3927c478bd9Sstevel@tonic-gate static int
3937c478bd9Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs,
3947c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
3957c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
3967c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
3977c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
3987c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	mblk_t *mop;
4017c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * The slave side can be opened as many times as needed.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
4077c478bd9Sstevel@tonic-gate 		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
4087c478bd9Sstevel@tonic-gate 		return (0);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
4127c478bd9Sstevel@tonic-gate 		DBG("zc_slave_open(): mop allocation failed\n");
4137c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_SOPEN;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
4207c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	qprocson(rqp);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Must follow qprocson(), since we aren't ready to process until then.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	zcs->zc_slave_rdq = rqp;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
4337c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4367c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
43727e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
4387c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
4397c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
4407c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
4417c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	return (0);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate static int
4507c478bd9Sstevel@tonic-gate zc_open(queue_t *rqp,		/* pointer to the read side queue */
4517c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
4527c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
4537c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
4547c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE(*devp);
4577c478bd9Sstevel@tonic-gate 	int ret;
4587c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (sflag != 0)
4617c478bd9Sstevel@tonic-gate 		return (EINVAL);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4647c478bd9Sstevel@tonic-gate 		return (ENXIO);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	switch (ZC_NODE(*devp)) {
4677c478bd9Sstevel@tonic-gate 	case ZC_MASTER_MINOR:
4687c478bd9Sstevel@tonic-gate 		ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp);
4697c478bd9Sstevel@tonic-gate 		break;
4707c478bd9Sstevel@tonic-gate 	case ZC_SLAVE_MINOR:
4717c478bd9Sstevel@tonic-gate 		ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp);
4727c478bd9Sstevel@tonic-gate 		break;
4737c478bd9Sstevel@tonic-gate 	default:
4747c478bd9Sstevel@tonic-gate 		ret = ENXIO;
4757c478bd9Sstevel@tonic-gate 		break;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (ret);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate  * close(9e) entrypoint.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4857c478bd9Sstevel@tonic-gate static int
4867c478bd9Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	queue_t *wqp;
4897c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
4907c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)rqp->q_ptr;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (rqp == zcs->zc_master_rdq) {
4957c478bd9Sstevel@tonic-gate 		DBG("Closing master side");
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		zcs->zc_master_rdq = NULL;
4987c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_MOPEN;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		/*
5017c478bd9Sstevel@tonic-gate 		 * qenable slave side write queue so that it can flush
5027c478bd9Sstevel@tonic-gate 		 * its messages as master's read queue is going away
5037c478bd9Sstevel@tonic-gate 		 */
5047c478bd9Sstevel@tonic-gate 		if (zcs->zc_slave_rdq != NULL) {
5057c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_slave_rdq));
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
5097c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	} else if (rqp == zcs->zc_slave_rdq) {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		DBG("Closing slave side");
5147c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_SOPEN;
5157c478bd9Sstevel@tonic-gate 		zcs->zc_slave_rdq = NULL;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		wqp = WR(rqp);
5187c478bd9Sstevel@tonic-gate 		while ((bp = getq(wqp)) != NULL) {
5197c478bd9Sstevel@tonic-gate 			if (zcs->zc_master_rdq != NULL)
5207c478bd9Sstevel@tonic-gate 				putnext(zcs->zc_master_rdq, bp);
5217c478bd9Sstevel@tonic-gate 			else if (bp->b_datap->db_type == M_IOCTL)
5227c478bd9Sstevel@tonic-gate 				miocnak(wqp, bp, 0, 0);
5237c478bd9Sstevel@tonic-gate 			else
5247c478bd9Sstevel@tonic-gate 				freemsg(bp);
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		/*
5287c478bd9Sstevel@tonic-gate 		 * Qenable master side write queue so that it can flush its
5297c478bd9Sstevel@tonic-gate 		 * messages as slaves's read queue is going away.
5307c478bd9Sstevel@tonic-gate 		 */
5317c478bd9Sstevel@tonic-gate 		if (zcs->zc_master_rdq != NULL)
5327c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_master_rdq));
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
5357c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	return (0);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate static void
5427c478bd9Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
5457c478bd9Sstevel@tonic-gate 	DBG1("M_FLUSH on %s side", zc_side(qp));
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHW) {
5487c478bd9Sstevel@tonic-gate 		DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
5497c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHDATA);
5507c478bd9Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
5517c478bd9Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHR) == 0) {
5527c478bd9Sstevel@tonic-gate 			/*
5537c478bd9Sstevel@tonic-gate 			 * FLUSHW only. Change to FLUSHR and putnext other side,
5547c478bd9Sstevel@tonic-gate 			 * then we are done.
5557c478bd9Sstevel@tonic-gate 			 */
5567c478bd9Sstevel@tonic-gate 			*mp->b_rptr |= FLUSHR;
5577c478bd9Sstevel@tonic-gate 			if (zc_switch(RD(qp)) != NULL) {
5587c478bd9Sstevel@tonic-gate 				putnext(zc_switch(RD(qp)), mp);
5597c478bd9Sstevel@tonic-gate 				return;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 		} else if ((zc_switch(RD(qp)) != NULL) &&
5627c478bd9Sstevel@tonic-gate 		    (nmp = copyb(mp)) != NULL) {
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * It is a FLUSHRW; we copy the mblk and send
5657c478bd9Sstevel@tonic-gate 			 * it to the other side, since we still need to use
5667c478bd9Sstevel@tonic-gate 			 * the mblk in FLUSHR processing, below.
5677c478bd9Sstevel@tonic-gate 			 */
5687c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), nmp);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHR) {
5737c478bd9Sstevel@tonic-gate 		DBG("qreply(qp) turning FLUSHR around\n");
5747c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
5757c478bd9Sstevel@tonic-gate 		return;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	freemsg(mp);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * wput(9E) is symmetric for master and slave sides, so this handles both
5827c478bd9Sstevel@tonic-gate  * without splitting the codepath.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * zc_wput() looks at the other side; if there is no process holding that
5857c478bd9Sstevel@tonic-gate  * side open, it frees the message.  This prevents processes from hanging
5867c478bd9Sstevel@tonic-gate  * if no one is holding open the console.  Otherwise, it putnext's high
5877c478bd9Sstevel@tonic-gate  * priority messages, putnext's normal messages if possible, and otherwise
5887c478bd9Sstevel@tonic-gate  * enqueues the messages; in the case that something is enqueued, wsrv(9E)
5897c478bd9Sstevel@tonic-gate  * will take care of eventually shuttling I/O to the other side.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate static void
5927c478bd9Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	DBG1("entering zc_wput, %s side", zc_side(qp));
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
6017c478bd9Sstevel@tonic-gate 		DBG1("wput to %s side (no one listening)", zc_side(qp));
6027c478bd9Sstevel@tonic-gate 		switch (type) {
6037c478bd9Sstevel@tonic-gate 		case M_FLUSH:
6047c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
6057c478bd9Sstevel@tonic-gate 			break;
6067c478bd9Sstevel@tonic-gate 		case M_IOCTL:
6077c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, 0);
6087c478bd9Sstevel@tonic-gate 			break;
6097c478bd9Sstevel@tonic-gate 		default:
6107c478bd9Sstevel@tonic-gate 			freemsg(mp);
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 		return;
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
6177c478bd9Sstevel@tonic-gate 		DBG1("(hipri) wput, %s side", zc_side(qp));
6187c478bd9Sstevel@tonic-gate 		switch (type) {
6197c478bd9Sstevel@tonic-gate 		case M_READ:		/* supposedly from ldterm? */
6207c478bd9Sstevel@tonic-gate 			DBG("zc_wput: tossing M_READ\n");
6217c478bd9Sstevel@tonic-gate 			freemsg(mp);
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 		case M_FLUSH:
6247c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
6257c478bd9Sstevel@tonic-gate 			break;
6267c478bd9Sstevel@tonic-gate 		default:
6277c478bd9Sstevel@tonic-gate 			/*
6287c478bd9Sstevel@tonic-gate 			 * Put this to the other side.
6297c478bd9Sstevel@tonic-gate 			 */
6307c478bd9Sstevel@tonic-gate 			ASSERT(zc_switch(RD(qp)) != NULL);
6317c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), mp);
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		DBG1("done (hipri) wput, %s side", zc_side(qp));
6357c478bd9Sstevel@tonic-gate 		return;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Only putnext if there isn't already something in the queue.
6407c478bd9Sstevel@tonic-gate 	 * otherwise things would wind up out of order.
6417c478bd9Sstevel@tonic-gate 	 */
6427c478bd9Sstevel@tonic-gate 	if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
6437c478bd9Sstevel@tonic-gate 		DBG("wput: putting message to other side\n");
6447c478bd9Sstevel@tonic-gate 		putnext(RD(zc_switch(qp)), mp);
6457c478bd9Sstevel@tonic-gate 	} else {
6467c478bd9Sstevel@tonic-gate 		DBG("wput: putting msg onto queue\n");
6477c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	DBG1("done wput, %s side", zc_side(qp));
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
6547c478bd9Sstevel@tonic-gate  * without splitting up the codepath.
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * Enable the write side of the partner.  This triggers the partner to send
6577c478bd9Sstevel@tonic-gate  * messages queued on its write side to this queue's read side.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static void
6607c478bd9Sstevel@tonic-gate zc_rsrv(queue_t *qp)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
6637c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)qp->q_ptr;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/*
6667c478bd9Sstevel@tonic-gate 	 * Care must be taken here, as either of the master or slave side
6677c478bd9Sstevel@tonic-gate 	 * qptr could be NULL.
6687c478bd9Sstevel@tonic-gate 	 */
6697c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq);
6707c478bd9Sstevel@tonic-gate 	if (zc_switch(qp) == NULL) {
6717c478bd9Sstevel@tonic-gate 		DBG("zc_rsrv: other side isn't listening\n");
6727c478bd9Sstevel@tonic-gate 		return;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	qenable(WR(zc_switch(qp)));
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * This routine is symmetric for master and slave, so it handles both without
6797c478bd9Sstevel@tonic-gate  * splitting up the codepath.
6807c478bd9Sstevel@tonic-gate  *
6817c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to the other, send
6827c478bd9Sstevel@tonic-gate  * them via putnext(). Else, if queued messages cannot be sent, leave them
6837c478bd9Sstevel@tonic-gate  * on this queue.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate static void
6867c478bd9Sstevel@tonic-gate zc_wsrv(queue_t *qp)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	DBG1("zc_wsrv master (%s) side", zc_side(qp));
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/*
6937c478bd9Sstevel@tonic-gate 	 * Partner has no read queue, so take the data, and throw it away.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
6967c478bd9Sstevel@tonic-gate 		DBG("zc_wsrv: other side isn't listening");
6977c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
6987c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
6997c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, 0);
7007c478bd9Sstevel@tonic-gate 			else
7017c478bd9Sstevel@tonic-gate 				freemsg(mp);
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
7047c478bd9Sstevel@tonic-gate 		return;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Due to the way zc_wput is implemented, we should never
7137c478bd9Sstevel@tonic-gate 		 * see a control message here.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type < QPCTL);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
7187c478bd9Sstevel@tonic-gate 			DBG("wsrv: send message to other side\n");
7197c478bd9Sstevel@tonic-gate 			putnext(RD(zc_switch(qp)), mp);
7207c478bd9Sstevel@tonic-gate 		} else {
7217c478bd9Sstevel@tonic-gate 			DBG("wsrv: putting msg back on queue\n");
7227c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
7237c478bd9Sstevel@tonic-gate 			break;
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate }
727