xref: /titanic_44/usr/src/uts/common/io/zcons.c (revision 6a1b30f3171ff1867501ed5f85f64f65448299a4)
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 /*
229d5056eaSjv227347  * Copyright 2009 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.
509d5056eaSjv227347  *
519d5056eaSjv227347  *
529d5056eaSjv227347  *
539d5056eaSjv227347  * MASTER SIDE IOCTLS
549d5056eaSjv227347  *
559d5056eaSjv227347  * The ZC_HOLDSLAVE and ZC_RELEASESLAVE ioctls instruct the master side of the
569d5056eaSjv227347  * console to hold and release a reference to the slave side's vnode.  They are
579d5056eaSjv227347  * meant to be issued by zoneadmd after the console device node is created and
589d5056eaSjv227347  * before it is destroyed so that the slave's STREAMS anchor, ptem, is
599d5056eaSjv227347  * preserved when ttymon starts popping STREAMS modules from within the
609d5056eaSjv227347  * associated zone.  This guarantees that the zone console will always have
619d5056eaSjv227347  * terminal semantics while the zone is running.
629d5056eaSjv227347  *
639d5056eaSjv227347  * Here is the issue: the ptem module is anchored in the zone console
649d5056eaSjv227347  * (slave side) so that processes within the associated non-global zone will
659d5056eaSjv227347  * fail to pop it off, thus ensuring that the slave will retain terminal
669d5056eaSjv227347  * semantics.  When a process attempts to pop the anchor off of a stream, the
679d5056eaSjv227347  * STREAMS subsystem checks whether the calling process' zone is the same as
689d5056eaSjv227347  * that of the process that pushed the anchor onto the stream and cancels the
699d5056eaSjv227347  * pop if they differ.  zoneadmd used to hold an open file descriptor for the
709d5056eaSjv227347  * slave while the associated non-global zone ran, thus ensuring that the
719d5056eaSjv227347  * slave's STREAMS anchor would never be popped from within the non-global zone
729d5056eaSjv227347  * (because zoneadmd runs in the global zone).  However, this file descriptor
739d5056eaSjv227347  * was removed to make zone console management more robust.  sad(7D) is now
749d5056eaSjv227347  * used to automatically set up the slave's STREAMS modules when the zone
759d5056eaSjv227347  * console is freshly opened within the associated non-global zone.  However,
769d5056eaSjv227347  * when a process within the non-global zone freshly opens the zone console, the
779d5056eaSjv227347  * anchor is pushed from within the non-global zone, making it possible for
789d5056eaSjv227347  * processes within the non-global zone (e.g., ttymon) to pop the anchor and
799d5056eaSjv227347  * destroy the zone console's terminal semantics.
809d5056eaSjv227347  *
819d5056eaSjv227347  * One solution is to make the zcons device hold the slave open while the
829d5056eaSjv227347  * associated non-global zone runs so that the STREAMS anchor will always be
839d5056eaSjv227347  * associated with the global zone.  Unfortunately, the slave cannot be opened
849d5056eaSjv227347  * from within the zcons driver because the driver is not reentrant: it has
859d5056eaSjv227347  * an outer STREAMS perimeter.  Therefore, the next best option is for zcons to
869d5056eaSjv227347  * provide an ioctl interface to zoneadmd to manage holding and releasing
879d5056eaSjv227347  * the slave side of the console.  It is sufficient to hold the slave side's
889d5056eaSjv227347  * vnode and bump the associated snode's reference count to preserve the slave's
899d5056eaSjv227347  * STREAMS configuration while the associated zone runs, so that's what the
909d5056eaSjv227347  * ioctls do.
919d5056eaSjv227347  *
929d5056eaSjv227347  *
939d5056eaSjv227347  * ZC_HOLDSLAVE
949d5056eaSjv227347  *
959d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively gets a
969d5056eaSjv227347  * reference to the slave side's minor node's vnode and bumps the associated
979d5056eaSjv227347  * snode's reference count.  The vnode reference is stored in the zcons device
989d5056eaSjv227347  * node's soft state.  This ioctl succeeds if the given file descriptor refers
999d5056eaSjv227347  * to the slave side's minor node or if there is already a reference to the
1009d5056eaSjv227347  * slave side's minor node's vnode in the device's soft state.
1019d5056eaSjv227347  *
1029d5056eaSjv227347  *
1039d5056eaSjv227347  * ZC_RELEASESLAVE
1049d5056eaSjv227347  *
1059d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively releases
1069d5056eaSjv227347  * the vnode reference stored in the zcons device node's soft state (which was
1079d5056eaSjv227347  * previously acquired via ZC_HOLDSLAVE) and decrements the reference count of
1089d5056eaSjv227347  * the snode associated with the vnode.  This ioctl succeeds if the given file
1099d5056eaSjv227347  * descriptor refers to the slave side's minor node or if no reference to the
1109d5056eaSjv227347  * slave side's minor node's vnode is stored in the device's soft state.
1119d5056eaSjv227347  *
1129d5056eaSjv227347  *
1139d5056eaSjv227347  * Note that the file descriptor arguments for both ioctls must be cast to
1149d5056eaSjv227347  * integers of pointer width.
1159d5056eaSjv227347  *
1169d5056eaSjv227347  * Here's how the dance between zcons and zoneadmd works:
1179d5056eaSjv227347  *
1189d5056eaSjv227347  *     Zone boot:
1199d5056eaSjv227347  *     1.  While booting the zone, zoneadmd creates an instance of zcons.
1209d5056eaSjv227347  *     2.  zoneadmd opens the master and slave sides of the new zone console
1219d5056eaSjv227347  *         and issues the ZC_HOLDSLAVE ioctl on the master side, passing its
1229d5056eaSjv227347  *         file descriptor for the slave side as the ioctl argument.
1239d5056eaSjv227347  *     3.  zcons holds the slave side's vnode, bumps the snode's reference
1249d5056eaSjv227347  *         count, and stores a pointer to the vnode in the device's soft
1259d5056eaSjv227347  *         state.
1269d5056eaSjv227347  *     4.  zoneadmd closes the master and slave sides and continues to boot
1279d5056eaSjv227347  *         the zone.
1289d5056eaSjv227347  *
1299d5056eaSjv227347  *     Zone halt:
1309d5056eaSjv227347  *     1.  While halting the zone, zoneadmd opens the master and slave sides
1319d5056eaSjv227347  *         of the zone's console and issues the ZC_RELEASESLAVE ioctl on the
1329d5056eaSjv227347  *         master side, passing its file descriptor for the slave side as the
1339d5056eaSjv227347  *         ioctl argument.
1349d5056eaSjv227347  *     2.  zcons decrements the slave side's snode's reference count, releases
1359d5056eaSjv227347  *         the slave's vnode, and eliminates its reference to the vnode in the
1369d5056eaSjv227347  *         device's soft state.
1379d5056eaSjv227347  *     3.  zoneadmd closes the master and slave sides.
1389d5056eaSjv227347  *     4.  zoneadmd destroys the zcons device and continues to halt the zone.
1399d5056eaSjv227347  *
1409d5056eaSjv227347  * It is necessary for zoneadmd to hold the slave open while issuing
1419d5056eaSjv227347  * ZC_RELEASESLAVE because zcons might otherwise release the last reference to
1429d5056eaSjv227347  * the slave's vnode.  If it does, then specfs will panic because it will expect
1439d5056eaSjv227347  * that the STREAMS configuration for the vnode was destroyed, which VN_RELE
1449d5056eaSjv227347  * doesn't do.  Forcing zoneadmd to hold the slave open guarantees that zcons
1459d5056eaSjv227347  * won't release the vnode's last reference.  zoneadmd will properly destroy the
1469d5056eaSjv227347  * vnode and the snode when it closes the file descriptor.
1479d5056eaSjv227347  *
1489d5056eaSjv227347  * Technically, any process that can access the master side can issue these
1499d5056eaSjv227347  * ioctls, but they should be treated as private interfaces for zoneadmd.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #include <sys/types.h>
1537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1557c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1567c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1577c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1587c478bd9Sstevel@tonic-gate #include <sys/devops.h>
1597c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1607c478bd9Sstevel@tonic-gate #include <sys/file.h>
1619d5056eaSjv227347 #include <sys/kstr.h>
1627c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1637c478bd9Sstevel@tonic-gate #include <sys/param.h>
1647c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1657c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1667c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1677c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
1687c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1697c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1707c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1717c478bd9Sstevel@tonic-gate #include <sys/types.h>
1727c478bd9Sstevel@tonic-gate #include <sys/zcons.h>
1739d5056eaSjv227347 #include <sys/vnode.h>
1749d5056eaSjv227347 #include <sys/fs/snode.h>
175*6a1b30f3Sjv227347 #include <sys/zone.h>
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1787c478bd9Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
1797c478bd9Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
1827c478bd9Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *);
1837c478bd9Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *);
1847c478bd9Sstevel@tonic-gate static void zc_rsrv(queue_t *);
1857c478bd9Sstevel@tonic-gate static void zc_wsrv(queue_t *);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * The instance number is encoded in the dev_t in the minor number; the lowest
1897c478bd9Sstevel@tonic-gate  * bit of the minor number is used to track the master vs. slave side of the
1907c478bd9Sstevel@tonic-gate  * virtual console.  The rest of the bits in the minor number are the instance.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate #define	ZC_MASTER_MINOR		0
1937c478bd9Sstevel@tonic-gate #define	ZC_SLAVE_MINOR		1
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate #define	ZC_INSTANCE(x)		(getminor((x)) >> 1)
1967c478bd9Sstevel@tonic-gate #define	ZC_NODE(x)		(getminor((x)) & 0x01)
1977c478bd9Sstevel@tonic-gate 
1989d5056eaSjv227347 /*
1999d5056eaSjv227347  * This macro converts a zc_state_t pointer to the associated slave minor node's
2009d5056eaSjv227347  * dev_t.
2019d5056eaSjv227347  */
2029d5056eaSjv227347 #define	ZC_STATE_TO_SLAVEDEV(x)	(makedevice(ddi_driver_major((x)->zc_devinfo), \
2039d5056eaSjv227347 	(minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SLAVE_MINOR)))
2049d5056eaSjv227347 
2057c478bd9Sstevel@tonic-gate int zcons_debug = 0;
2067c478bd9Sstevel@tonic-gate #define	DBG(a)   if (zcons_debug) cmn_err(CE_NOTE, a)
2077c478bd9Sstevel@tonic-gate #define	DBG1(a, b)   if (zcons_debug) cmn_err(CE_NOTE, a, b)
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Zone Console Pseudo Terminal Module: stream data structure definitions
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate static struct module_info zc_info = {
2147c478bd9Sstevel@tonic-gate 	31337,	/* c0z we r hAx0rs */
2157c478bd9Sstevel@tonic-gate 	"zcons",
2167c478bd9Sstevel@tonic-gate 	0,
2177c478bd9Sstevel@tonic-gate 	INFPSZ,
2187c478bd9Sstevel@tonic-gate 	2048,
2197c478bd9Sstevel@tonic-gate 	128
2207c478bd9Sstevel@tonic-gate };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static struct qinit zc_rinit = {
2237c478bd9Sstevel@tonic-gate 	NULL,
2247c478bd9Sstevel@tonic-gate 	(int (*)()) zc_rsrv,
2257c478bd9Sstevel@tonic-gate 	zc_open,
2267c478bd9Sstevel@tonic-gate 	zc_close,
2277c478bd9Sstevel@tonic-gate 	NULL,
2287c478bd9Sstevel@tonic-gate 	&zc_info,
2297c478bd9Sstevel@tonic-gate 	NULL
2307c478bd9Sstevel@tonic-gate };
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static struct qinit zc_winit = {
2337c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wput,
2347c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wsrv,
2357c478bd9Sstevel@tonic-gate 	NULL,
2367c478bd9Sstevel@tonic-gate 	NULL,
2377c478bd9Sstevel@tonic-gate 	NULL,
2387c478bd9Sstevel@tonic-gate 	&zc_info,
2397c478bd9Sstevel@tonic-gate 	NULL
2407c478bd9Sstevel@tonic-gate };
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static struct streamtab zc_tab_info = {
2437c478bd9Sstevel@tonic-gate 	&zc_rinit,
2447c478bd9Sstevel@tonic-gate 	&zc_winit,
2457c478bd9Sstevel@tonic-gate 	NULL,
2467c478bd9Sstevel@tonic-gate 	NULL
2477c478bd9Sstevel@tonic-gate };
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate #define	ZC_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev,	zc_attach, zc_detach, nodev, \
25519397407SSherry Moore 	zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
262c6f42f0eSedp 	&mod_driverops, 	/* Type of module (this is a pseudo driver) */
263c6f42f0eSedp 	"Zone console driver",	/* description of module */
2647c478bd9Sstevel@tonic-gate 	&zc_ops			/* driver ops */
2657c478bd9Sstevel@tonic-gate };
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2687c478bd9Sstevel@tonic-gate 	MODREV_1,
2697c478bd9Sstevel@tonic-gate 	&modldrv,
2707c478bd9Sstevel@tonic-gate 	NULL
2717c478bd9Sstevel@tonic-gate };
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate typedef struct zc_state {
2747c478bd9Sstevel@tonic-gate 	dev_info_t *zc_devinfo;
2757c478bd9Sstevel@tonic-gate 	queue_t *zc_master_rdq;
2767c478bd9Sstevel@tonic-gate 	queue_t *zc_slave_rdq;
2779d5056eaSjv227347 	vnode_t *zc_slave_vnode;
2787c478bd9Sstevel@tonic-gate 	int zc_state;
2797c478bd9Sstevel@tonic-gate } zc_state_t;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #define	ZC_STATE_MOPEN	0x01
2827c478bd9Sstevel@tonic-gate #define	ZC_STATE_SOPEN	0x02
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static void *zc_soft_state;
2857c478bd9Sstevel@tonic-gate 
2869d5056eaSjv227347 /*
2879d5056eaSjv227347  * List of STREAMS modules that should be pushed onto every slave instance.
2889d5056eaSjv227347  */
2899d5056eaSjv227347 static char *zcons_mods[] = {
2909d5056eaSjv227347 	"ptem",
2919d5056eaSjv227347 	"ldterm",
2929d5056eaSjv227347 	"ttcompat",
2939d5056eaSjv227347 	NULL
2949d5056eaSjv227347 };
2959d5056eaSjv227347 
2967c478bd9Sstevel@tonic-gate int
_init(void)2977c478bd9Sstevel@tonic-gate _init(void)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int err;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&zc_soft_state,
3027c478bd9Sstevel@tonic-gate 	    sizeof (zc_state_t), 0)) != 0) {
3037c478bd9Sstevel@tonic-gate 		return (err);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0)
3077c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(zc_soft_state);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (err);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate int
_fini(void)3147c478bd9Sstevel@tonic-gate _fini(void)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int err;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0) {
3197c478bd9Sstevel@tonic-gate 		return (err);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&zc_soft_state);
3237c478bd9Sstevel@tonic-gate 	return (0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3277c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate static int
zc_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3337c478bd9Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3367c478bd9Sstevel@tonic-gate 	int instance;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3427c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
3437c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3447c478bd9Sstevel@tonic-gate 
3459d5056eaSjv227347 	/*
3469d5056eaSjv227347 	 * Create the master and slave minor nodes.
3479d5056eaSjv227347 	 */
348*6a1b30f3Sjv227347 	if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR,
349*6a1b30f3Sjv227347 	    instance << 1 | ZC_SLAVE_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
3507c478bd9Sstevel@tonic-gate 	    (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR,
351*6a1b30f3Sjv227347 	    instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
352*6a1b30f3Sjv227347 		ddi_remove_minor_node(dip, NULL);
353*6a1b30f3Sjv227347 		ddi_soft_state_free(zc_soft_state, instance);
354*6a1b30f3Sjv227347 		return (DDI_FAILURE);
355*6a1b30f3Sjv227347 	}
3567c478bd9Sstevel@tonic-gate 
3579d5056eaSjv227347 	VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL);
3587c478bd9Sstevel@tonic-gate 	zcs->zc_devinfo = dip;
3597c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate static int
zc_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3637c478bd9Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3667c478bd9Sstevel@tonic-gate 	int instance;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
3697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3727c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
3737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) ||
3767c478bd9Sstevel@tonic-gate 	    (zcs->zc_state & ZC_STATE_SOPEN)) {
3777c478bd9Sstevel@tonic-gate 		DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
3787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3827c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(zc_soft_state, instance);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * zc_getinfo()
3897c478bd9Sstevel@tonic-gate  *	getinfo(9e) entrypoint.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3927c478bd9Sstevel@tonic-gate static int
zc_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)3937c478bd9Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3967c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE((dev_t)arg);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	switch (infocmd) {
3997c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4007c478bd9Sstevel@tonic-gate 		if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4017c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4027c478bd9Sstevel@tonic-gate 		*result = zcs->zc_devinfo;
4037c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4047c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4057c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
4067c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * Return the equivalent queue from the other side of the relationship.
4137c478bd9Sstevel@tonic-gate  * e.g.: given the slave's write queue, return the master's write queue.
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate static queue_t *
zc_switch(queue_t * qp)4167c478bd9Sstevel@tonic-gate zc_switch(queue_t *qp)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4197c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq)
4227c478bd9Sstevel@tonic-gate 		return (zcs->zc_slave_rdq);
4237c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL)
4247c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_slave_rdq));
4257c478bd9Sstevel@tonic-gate 	else if (qp == zcs->zc_slave_rdq)
4267c478bd9Sstevel@tonic-gate 		return (zcs->zc_master_rdq);
4277c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL)
4287c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_master_rdq));
4297c478bd9Sstevel@tonic-gate 	else
4307c478bd9Sstevel@tonic-gate 		return (NULL);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * For debugging and outputting messages.  Returns the name of the side of
4357c478bd9Sstevel@tonic-gate  * the relationship associated with this queue.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static const char *
zc_side(queue_t * qp)4387c478bd9Sstevel@tonic-gate zc_side(queue_t *qp)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4417c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq ||
4447c478bd9Sstevel@tonic-gate 	    OTHERQ(qp) == zcs->zc_master_rdq) {
4457c478bd9Sstevel@tonic-gate 		return ("master");
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq);
4487c478bd9Sstevel@tonic-gate 	return ("slave");
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4527c478bd9Sstevel@tonic-gate static int
zc_master_open(zc_state_t * zcs,queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)4537c478bd9Sstevel@tonic-gate zc_master_open(zc_state_t *zcs,
4547c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
4557c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
4567c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
4577c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
4587c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	mblk_t *mop;
4617c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Enforce exclusivity on the master side; the only consumer should
4657c478bd9Sstevel@tonic-gate 	 * be the zoneadmd for the zone.
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
4687c478bd9Sstevel@tonic-gate 		return (EBUSY);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
4717c478bd9Sstevel@tonic-gate 		DBG("zc_master_open(): mop allocation failed\n");
4727c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_MOPEN;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
4797c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
4827c478bd9Sstevel@tonic-gate 	qprocson(rqp);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Following qprocson(), the master side is fully plumbed into the
4867c478bd9Sstevel@tonic-gate 	 * STREAM and may send/receive messages.  Setting zcs->zc_master_rdq
4877c478bd9Sstevel@tonic-gate 	 * will allow the slave to send messages to us (the master).
4887c478bd9Sstevel@tonic-gate 	 * This cannot occur before qprocson() because the master is not
4897c478bd9Sstevel@tonic-gate 	 * ready to process them until that point.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	zcs->zc_master_rdq = rqp;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
4957c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
4987c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
49927e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
5007c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
5017c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
5027c478bd9Sstevel@tonic-gate 	else
5037c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
5047c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
5057c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
5067c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (0);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5127c478bd9Sstevel@tonic-gate static int
zc_slave_open(zc_state_t * zcs,queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)5137c478bd9Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs,
5147c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
5157c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
5167c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
5177c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
5187c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	mblk_t *mop;
5217c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
522*6a1b30f3Sjv227347 	major_t major;
523*6a1b30f3Sjv227347 	minor_t minor;
524*6a1b30f3Sjv227347 	minor_t lastminor;
525*6a1b30f3Sjv227347 	uint_t anchorindex;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * The slave side can be opened as many times as needed.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
5317c478bd9Sstevel@tonic-gate 		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
5327c478bd9Sstevel@tonic-gate 		return (0);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
535*6a1b30f3Sjv227347 	/*
536*6a1b30f3Sjv227347 	 * Set up sad(7D) so that the necessary STREAMS modules will be in
537*6a1b30f3Sjv227347 	 * place.  A wrinkle is that 'ptem' must be anchored
538*6a1b30f3Sjv227347 	 * in place (see streamio(7i)) because we always want the console to
539*6a1b30f3Sjv227347 	 * have terminal semantics.
540*6a1b30f3Sjv227347 	 */
541*6a1b30f3Sjv227347 	minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR;
542*6a1b30f3Sjv227347 	major = ddi_driver_major(zcs->zc_devinfo);
543*6a1b30f3Sjv227347 	lastminor = 0;
544*6a1b30f3Sjv227347 	anchorindex = 1;
545*6a1b30f3Sjv227347 	if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor,
546*6a1b30f3Sjv227347 	    &anchorindex, zcons_mods) != 0) {
547*6a1b30f3Sjv227347 		DBG("zc_slave_open(): kstr_autopush() failed\n");
548*6a1b30f3Sjv227347 		return (EIO);
549*6a1b30f3Sjv227347 	}
550*6a1b30f3Sjv227347 
5517c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
5527c478bd9Sstevel@tonic-gate 		DBG("zc_slave_open(): mop allocation failed\n");
5537c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_SOPEN;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
5607c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
5617c478bd9Sstevel@tonic-gate 	 */
5627c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	qprocson(rqp);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Must follow qprocson(), since we aren't ready to process until then.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	zcs->zc_slave_rdq = rqp;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
5737c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
5767c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
57727e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
5787c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
5797c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
5807c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
5817c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	return (0);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static int
zc_open(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)5907c478bd9Sstevel@tonic-gate zc_open(queue_t *rqp,		/* pointer to the read side queue */
5917c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
5927c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
5937c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
5947c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE(*devp);
5977c478bd9Sstevel@tonic-gate 	int ret;
5987c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (sflag != 0)
6017c478bd9Sstevel@tonic-gate 		return (EINVAL);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
6047c478bd9Sstevel@tonic-gate 		return (ENXIO);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	switch (ZC_NODE(*devp)) {
6077c478bd9Sstevel@tonic-gate 	case ZC_MASTER_MINOR:
6087c478bd9Sstevel@tonic-gate 		ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp);
6097c478bd9Sstevel@tonic-gate 		break;
6107c478bd9Sstevel@tonic-gate 	case ZC_SLAVE_MINOR:
6117c478bd9Sstevel@tonic-gate 		ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp);
6127c478bd9Sstevel@tonic-gate 		break;
6137c478bd9Sstevel@tonic-gate 	default:
6147c478bd9Sstevel@tonic-gate 		ret = ENXIO;
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	return (ret);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * close(9e) entrypoint.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
6257c478bd9Sstevel@tonic-gate static int
zc_close(queue_t * rqp,int flag,cred_t * credp)6267c478bd9Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	queue_t *wqp;
6297c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
6307c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
631*6a1b30f3Sjv227347 	major_t major;
632*6a1b30f3Sjv227347 	minor_t minor;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)rqp->q_ptr;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (rqp == zcs->zc_master_rdq) {
6377c478bd9Sstevel@tonic-gate 		DBG("Closing master side");
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		zcs->zc_master_rdq = NULL;
6407c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_MOPEN;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * qenable slave side write queue so that it can flush
6447c478bd9Sstevel@tonic-gate 		 * its messages as master's read queue is going away
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate 		if (zcs->zc_slave_rdq != NULL) {
6477c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_slave_rdq));
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
6517c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	} else if (rqp == zcs->zc_slave_rdq) {
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		DBG("Closing slave side");
6567c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_SOPEN;
6577c478bd9Sstevel@tonic-gate 		zcs->zc_slave_rdq = NULL;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		wqp = WR(rqp);
6607c478bd9Sstevel@tonic-gate 		while ((bp = getq(wqp)) != NULL) {
6617c478bd9Sstevel@tonic-gate 			if (zcs->zc_master_rdq != NULL)
6627c478bd9Sstevel@tonic-gate 				putnext(zcs->zc_master_rdq, bp);
6637c478bd9Sstevel@tonic-gate 			else if (bp->b_datap->db_type == M_IOCTL)
6647c478bd9Sstevel@tonic-gate 				miocnak(wqp, bp, 0, 0);
6657c478bd9Sstevel@tonic-gate 			else
6667c478bd9Sstevel@tonic-gate 				freemsg(bp);
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		/*
6707c478bd9Sstevel@tonic-gate 		 * Qenable master side write queue so that it can flush its
6717c478bd9Sstevel@tonic-gate 		 * messages as slaves's read queue is going away.
6727c478bd9Sstevel@tonic-gate 		 */
6737c478bd9Sstevel@tonic-gate 		if (zcs->zc_master_rdq != NULL)
6747c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_master_rdq));
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
6777c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
678*6a1b30f3Sjv227347 
679*6a1b30f3Sjv227347 		/*
680*6a1b30f3Sjv227347 		 * Clear the sad configuration so that reopening doesn't fail
681*6a1b30f3Sjv227347 		 * to set up sad configuration.
682*6a1b30f3Sjv227347 		 */
683*6a1b30f3Sjv227347 		major = ddi_driver_major(zcs->zc_devinfo);
684*6a1b30f3Sjv227347 		minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SLAVE_MINOR;
685*6a1b30f3Sjv227347 		(void) kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL,
686*6a1b30f3Sjv227347 		    NULL);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	return (0);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate static void
handle_mflush(queue_t * qp,mblk_t * mp)6937c478bd9Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
6967c478bd9Sstevel@tonic-gate 	DBG1("M_FLUSH on %s side", zc_side(qp));
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHW) {
6997c478bd9Sstevel@tonic-gate 		DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
7007c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHDATA);
7017c478bd9Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
7027c478bd9Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHR) == 0) {
7037c478bd9Sstevel@tonic-gate 			/*
7047c478bd9Sstevel@tonic-gate 			 * FLUSHW only. Change to FLUSHR and putnext other side,
7057c478bd9Sstevel@tonic-gate 			 * then we are done.
7067c478bd9Sstevel@tonic-gate 			 */
7077c478bd9Sstevel@tonic-gate 			*mp->b_rptr |= FLUSHR;
7087c478bd9Sstevel@tonic-gate 			if (zc_switch(RD(qp)) != NULL) {
7097c478bd9Sstevel@tonic-gate 				putnext(zc_switch(RD(qp)), mp);
7107c478bd9Sstevel@tonic-gate 				return;
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 		} else if ((zc_switch(RD(qp)) != NULL) &&
7137c478bd9Sstevel@tonic-gate 		    (nmp = copyb(mp)) != NULL) {
7147c478bd9Sstevel@tonic-gate 			/*
7157c478bd9Sstevel@tonic-gate 			 * It is a FLUSHRW; we copy the mblk and send
7167c478bd9Sstevel@tonic-gate 			 * it to the other side, since we still need to use
7177c478bd9Sstevel@tonic-gate 			 * the mblk in FLUSHR processing, below.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), nmp);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHR) {
7247c478bd9Sstevel@tonic-gate 		DBG("qreply(qp) turning FLUSHR around\n");
7257c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
7267c478bd9Sstevel@tonic-gate 		return;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	freemsg(mp);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate  * wput(9E) is symmetric for master and slave sides, so this handles both
7339d5056eaSjv227347  * without splitting the codepath.  (The only exception to this is the
7349d5056eaSjv227347  * processing of zcons ioctls, which is restricted to the master side.)
7357c478bd9Sstevel@tonic-gate  *
7367c478bd9Sstevel@tonic-gate  * zc_wput() looks at the other side; if there is no process holding that
7377c478bd9Sstevel@tonic-gate  * side open, it frees the message.  This prevents processes from hanging
7387c478bd9Sstevel@tonic-gate  * if no one is holding open the console.  Otherwise, it putnext's high
7397c478bd9Sstevel@tonic-gate  * priority messages, putnext's normal messages if possible, and otherwise
7407c478bd9Sstevel@tonic-gate  * enqueues the messages; in the case that something is enqueued, wsrv(9E)
7417c478bd9Sstevel@tonic-gate  * will take care of eventually shuttling I/O to the other side.
7427c478bd9Sstevel@tonic-gate  */
7437c478bd9Sstevel@tonic-gate static void
zc_wput(queue_t * qp,mblk_t * mp)7447c478bd9Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
7479d5056eaSjv227347 	zc_state_t *zcs;
7489d5056eaSjv227347 	struct iocblk *iocbp;
7499d5056eaSjv227347 	file_t *slave_filep;
7509d5056eaSjv227347 	struct snode *slave_snodep;
7519d5056eaSjv227347 	int slave_fd;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	DBG1("entering zc_wput, %s side", zc_side(qp));
7567c478bd9Sstevel@tonic-gate 
7579d5056eaSjv227347 	/*
7589d5056eaSjv227347 	 * Process zcons ioctl messages if qp is the master console's write
7599d5056eaSjv227347 	 * queue.
7609d5056eaSjv227347 	 */
7619d5056eaSjv227347 	zcs = (zc_state_t *)qp->q_ptr;
7629d5056eaSjv227347 	if (zcs->zc_master_rdq != NULL && qp == WR(zcs->zc_master_rdq) &&
7639d5056eaSjv227347 	    type == M_IOCTL) {
7649d5056eaSjv227347 		iocbp = (struct iocblk *)(void *)mp->b_rptr;
7659d5056eaSjv227347 		switch (iocbp->ioc_cmd) {
7669d5056eaSjv227347 		case ZC_HOLDSLAVE:
7679d5056eaSjv227347 			/*
7689d5056eaSjv227347 			 * Hold the slave's vnode and increment the refcount
7699d5056eaSjv227347 			 * of the snode.  If the vnode is already held, then
7709d5056eaSjv227347 			 * indicate success.
7719d5056eaSjv227347 			 */
7729d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
7739d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
7749d5056eaSjv227347 				return;
7759d5056eaSjv227347 			}
7769d5056eaSjv227347 			if (zcs->zc_slave_vnode != NULL) {
7779d5056eaSjv227347 				miocack(qp, mp, 0, 0);
7789d5056eaSjv227347 				return;
7799d5056eaSjv227347 			}
7809d5056eaSjv227347 
7819d5056eaSjv227347 			/*
782*6a1b30f3Sjv227347 			 * The process that passed the ioctl must be running in
783*6a1b30f3Sjv227347 			 * the global zone.
784*6a1b30f3Sjv227347 			 */
785*6a1b30f3Sjv227347 			if (curzone != global_zone) {
786*6a1b30f3Sjv227347 				miocack(qp, mp, 0, EINVAL);
787*6a1b30f3Sjv227347 				return;
788*6a1b30f3Sjv227347 			}
789*6a1b30f3Sjv227347 
790*6a1b30f3Sjv227347 			/*
7919d5056eaSjv227347 			 * The calling process must pass a file descriptor for
7929d5056eaSjv227347 			 * the slave device.
7939d5056eaSjv227347 			 */
7949d5056eaSjv227347 			slave_fd =
7959d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
7969d5056eaSjv227347 			    b_rptr;
7979d5056eaSjv227347 			slave_filep = getf(slave_fd);
7989d5056eaSjv227347 			if (slave_filep == NULL) {
7999d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8009d5056eaSjv227347 				return;
8019d5056eaSjv227347 			}
8029d5056eaSjv227347 			if (ZC_STATE_TO_SLAVEDEV(zcs) !=
8039d5056eaSjv227347 			    slave_filep->f_vnode->v_rdev) {
8049d5056eaSjv227347 				releasef(slave_fd);
8059d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8069d5056eaSjv227347 				return;
8079d5056eaSjv227347 			}
8089d5056eaSjv227347 
8099d5056eaSjv227347 			/*
8109d5056eaSjv227347 			 * Get a reference to the slave's vnode.  Also bump the
8119d5056eaSjv227347 			 * reference count on the associated snode.
8129d5056eaSjv227347 			 */
8139d5056eaSjv227347 			ASSERT(vn_matchops(slave_filep->f_vnode,
8149d5056eaSjv227347 			    spec_getvnodeops()));
8159d5056eaSjv227347 			zcs->zc_slave_vnode = slave_filep->f_vnode;
8169d5056eaSjv227347 			VN_HOLD(zcs->zc_slave_vnode);
8179d5056eaSjv227347 			slave_snodep = VTOCS(zcs->zc_slave_vnode);
8189d5056eaSjv227347 			mutex_enter(&slave_snodep->s_lock);
8199d5056eaSjv227347 			++slave_snodep->s_count;
8209d5056eaSjv227347 			mutex_exit(&slave_snodep->s_lock);
8219d5056eaSjv227347 			releasef(slave_fd);
8229d5056eaSjv227347 			miocack(qp, mp, 0, 0);
8239d5056eaSjv227347 			return;
8249d5056eaSjv227347 		case ZC_RELEASESLAVE:
8259d5056eaSjv227347 			/*
8269d5056eaSjv227347 			 * Release the master's handle on the slave's vnode.
8279d5056eaSjv227347 			 * If there isn't a handle for the vnode, then indicate
8289d5056eaSjv227347 			 * success.
8299d5056eaSjv227347 			 */
8309d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
8319d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8329d5056eaSjv227347 				return;
8339d5056eaSjv227347 			}
8349d5056eaSjv227347 			if (zcs->zc_slave_vnode == NULL) {
8359d5056eaSjv227347 				miocack(qp, mp, 0, 0);
8369d5056eaSjv227347 				return;
8379d5056eaSjv227347 			}
8389d5056eaSjv227347 
8399d5056eaSjv227347 			/*
840*6a1b30f3Sjv227347 			 * The process that passed the ioctl must be running in
841*6a1b30f3Sjv227347 			 * the global zone.
842*6a1b30f3Sjv227347 			 */
843*6a1b30f3Sjv227347 			if (curzone != global_zone) {
844*6a1b30f3Sjv227347 				miocack(qp, mp, 0, EINVAL);
845*6a1b30f3Sjv227347 				return;
846*6a1b30f3Sjv227347 			}
847*6a1b30f3Sjv227347 
848*6a1b30f3Sjv227347 			/*
8499d5056eaSjv227347 			 * The process that passed the ioctl must have provided
8509d5056eaSjv227347 			 * a file descriptor for the slave device.  Make sure
8519d5056eaSjv227347 			 * this is correct.
8529d5056eaSjv227347 			 */
8539d5056eaSjv227347 			slave_fd =
8549d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
8559d5056eaSjv227347 			    b_rptr;
8569d5056eaSjv227347 			slave_filep = getf(slave_fd);
8579d5056eaSjv227347 			if (slave_filep == NULL) {
8589d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8599d5056eaSjv227347 				return;
8609d5056eaSjv227347 			}
8619d5056eaSjv227347 			if (zcs->zc_slave_vnode->v_rdev !=
8629d5056eaSjv227347 			    slave_filep->f_vnode->v_rdev) {
8639d5056eaSjv227347 				releasef(slave_fd);
8649d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8659d5056eaSjv227347 				return;
8669d5056eaSjv227347 			}
8679d5056eaSjv227347 
8689d5056eaSjv227347 			/*
8699d5056eaSjv227347 			 * Decrement the snode's reference count and release the
8709d5056eaSjv227347 			 * vnode.
8719d5056eaSjv227347 			 */
8729d5056eaSjv227347 			ASSERT(vn_matchops(slave_filep->f_vnode,
8739d5056eaSjv227347 			    spec_getvnodeops()));
8749d5056eaSjv227347 			slave_snodep = VTOCS(zcs->zc_slave_vnode);
8759d5056eaSjv227347 			mutex_enter(&slave_snodep->s_lock);
8769d5056eaSjv227347 			--slave_snodep->s_count;
8779d5056eaSjv227347 			mutex_exit(&slave_snodep->s_lock);
8789d5056eaSjv227347 			VN_RELE(zcs->zc_slave_vnode);
8799d5056eaSjv227347 			zcs->zc_slave_vnode = NULL;
8809d5056eaSjv227347 			releasef(slave_fd);
8819d5056eaSjv227347 			miocack(qp, mp, 0, 0);
8829d5056eaSjv227347 			return;
8839d5056eaSjv227347 		default:
8849d5056eaSjv227347 			break;
8859d5056eaSjv227347 		}
8869d5056eaSjv227347 	}
8879d5056eaSjv227347 
8887c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
8897c478bd9Sstevel@tonic-gate 		DBG1("wput to %s side (no one listening)", zc_side(qp));
8907c478bd9Sstevel@tonic-gate 		switch (type) {
8917c478bd9Sstevel@tonic-gate 		case M_FLUSH:
8927c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
8937c478bd9Sstevel@tonic-gate 			break;
8947c478bd9Sstevel@tonic-gate 		case M_IOCTL:
8957c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, 0);
8967c478bd9Sstevel@tonic-gate 			break;
8977c478bd9Sstevel@tonic-gate 		default:
8987c478bd9Sstevel@tonic-gate 			freemsg(mp);
8997c478bd9Sstevel@tonic-gate 			break;
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		return;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
9057c478bd9Sstevel@tonic-gate 		DBG1("(hipri) wput, %s side", zc_side(qp));
9067c478bd9Sstevel@tonic-gate 		switch (type) {
9077c478bd9Sstevel@tonic-gate 		case M_READ:		/* supposedly from ldterm? */
9087c478bd9Sstevel@tonic-gate 			DBG("zc_wput: tossing M_READ\n");
9097c478bd9Sstevel@tonic-gate 			freemsg(mp);
9107c478bd9Sstevel@tonic-gate 			break;
9117c478bd9Sstevel@tonic-gate 		case M_FLUSH:
9127c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
9137c478bd9Sstevel@tonic-gate 			break;
9147c478bd9Sstevel@tonic-gate 		default:
9157c478bd9Sstevel@tonic-gate 			/*
9167c478bd9Sstevel@tonic-gate 			 * Put this to the other side.
9177c478bd9Sstevel@tonic-gate 			 */
9187c478bd9Sstevel@tonic-gate 			ASSERT(zc_switch(RD(qp)) != NULL);
9197c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), mp);
9207c478bd9Sstevel@tonic-gate 			break;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		DBG1("done (hipri) wput, %s side", zc_side(qp));
9237c478bd9Sstevel@tonic-gate 		return;
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * Only putnext if there isn't already something in the queue.
9287c478bd9Sstevel@tonic-gate 	 * otherwise things would wind up out of order.
9297c478bd9Sstevel@tonic-gate 	 */
9307c478bd9Sstevel@tonic-gate 	if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
9317c478bd9Sstevel@tonic-gate 		DBG("wput: putting message to other side\n");
9327c478bd9Sstevel@tonic-gate 		putnext(RD(zc_switch(qp)), mp);
9337c478bd9Sstevel@tonic-gate 	} else {
9347c478bd9Sstevel@tonic-gate 		DBG("wput: putting msg onto queue\n");
9357c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 	DBG1("done wput, %s side", zc_side(qp));
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate /*
9417c478bd9Sstevel@tonic-gate  * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
9427c478bd9Sstevel@tonic-gate  * without splitting up the codepath.
9437c478bd9Sstevel@tonic-gate  *
9447c478bd9Sstevel@tonic-gate  * Enable the write side of the partner.  This triggers the partner to send
9457c478bd9Sstevel@tonic-gate  * messages queued on its write side to this queue's read side.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate static void
zc_rsrv(queue_t * qp)9487c478bd9Sstevel@tonic-gate zc_rsrv(queue_t *qp)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
9517c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)qp->q_ptr;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	/*
9547c478bd9Sstevel@tonic-gate 	 * Care must be taken here, as either of the master or slave side
9557c478bd9Sstevel@tonic-gate 	 * qptr could be NULL.
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq);
9587c478bd9Sstevel@tonic-gate 	if (zc_switch(qp) == NULL) {
9597c478bd9Sstevel@tonic-gate 		DBG("zc_rsrv: other side isn't listening\n");
9607c478bd9Sstevel@tonic-gate 		return;
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	qenable(WR(zc_switch(qp)));
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * This routine is symmetric for master and slave, so it handles both without
9677c478bd9Sstevel@tonic-gate  * splitting up the codepath.
9687c478bd9Sstevel@tonic-gate  *
9697c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to the other, send
9707c478bd9Sstevel@tonic-gate  * them via putnext(). Else, if queued messages cannot be sent, leave them
9717c478bd9Sstevel@tonic-gate  * on this queue.
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate static void
zc_wsrv(queue_t * qp)9747c478bd9Sstevel@tonic-gate zc_wsrv(queue_t *qp)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	DBG1("zc_wsrv master (%s) side", zc_side(qp));
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * Partner has no read queue, so take the data, and throw it away.
9827c478bd9Sstevel@tonic-gate 	 */
9837c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
9847c478bd9Sstevel@tonic-gate 		DBG("zc_wsrv: other side isn't listening");
9857c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
9867c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
9877c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, 0);
9887c478bd9Sstevel@tonic-gate 			else
9897c478bd9Sstevel@tonic-gate 				freemsg(mp);
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
9927c478bd9Sstevel@tonic-gate 		return;
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	/*
9967c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
9977c478bd9Sstevel@tonic-gate 	 */
9987c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * Due to the way zc_wput is implemented, we should never
10017c478bd9Sstevel@tonic-gate 		 * see a control message here.
10027c478bd9Sstevel@tonic-gate 		 */
10037c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type < QPCTL);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
10067c478bd9Sstevel@tonic-gate 			DBG("wsrv: send message to other side\n");
10077c478bd9Sstevel@tonic-gate 			putnext(RD(zc_switch(qp)), mp);
10087c478bd9Sstevel@tonic-gate 		} else {
10097c478bd9Sstevel@tonic-gate 			DBG("wsrv: putting msg back on queue\n");
10107c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
10117c478bd9Sstevel@tonic-gate 			break;
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate }
1015