xref: /titanic_53/usr/src/uts/common/io/zcons.c (revision 9d5056ea37dd689d393cb1715ae29551bc0eba2e)
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*9d5056eaSjv227347  * 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.
50*9d5056eaSjv227347  *
51*9d5056eaSjv227347  *
52*9d5056eaSjv227347  *
53*9d5056eaSjv227347  * MASTER SIDE IOCTLS
54*9d5056eaSjv227347  *
55*9d5056eaSjv227347  * The ZC_HOLDSLAVE and ZC_RELEASESLAVE ioctls instruct the master side of the
56*9d5056eaSjv227347  * console to hold and release a reference to the slave side's vnode.  They are
57*9d5056eaSjv227347  * meant to be issued by zoneadmd after the console device node is created and
58*9d5056eaSjv227347  * before it is destroyed so that the slave's STREAMS anchor, ptem, is
59*9d5056eaSjv227347  * preserved when ttymon starts popping STREAMS modules from within the
60*9d5056eaSjv227347  * associated zone.  This guarantees that the zone console will always have
61*9d5056eaSjv227347  * terminal semantics while the zone is running.
62*9d5056eaSjv227347  *
63*9d5056eaSjv227347  * Here is the issue: the ptem module is anchored in the zone console
64*9d5056eaSjv227347  * (slave side) so that processes within the associated non-global zone will
65*9d5056eaSjv227347  * fail to pop it off, thus ensuring that the slave will retain terminal
66*9d5056eaSjv227347  * semantics.  When a process attempts to pop the anchor off of a stream, the
67*9d5056eaSjv227347  * STREAMS subsystem checks whether the calling process' zone is the same as
68*9d5056eaSjv227347  * that of the process that pushed the anchor onto the stream and cancels the
69*9d5056eaSjv227347  * pop if they differ.  zoneadmd used to hold an open file descriptor for the
70*9d5056eaSjv227347  * slave while the associated non-global zone ran, thus ensuring that the
71*9d5056eaSjv227347  * slave's STREAMS anchor would never be popped from within the non-global zone
72*9d5056eaSjv227347  * (because zoneadmd runs in the global zone).  However, this file descriptor
73*9d5056eaSjv227347  * was removed to make zone console management more robust.  sad(7D) is now
74*9d5056eaSjv227347  * used to automatically set up the slave's STREAMS modules when the zone
75*9d5056eaSjv227347  * console is freshly opened within the associated non-global zone.  However,
76*9d5056eaSjv227347  * when a process within the non-global zone freshly opens the zone console, the
77*9d5056eaSjv227347  * anchor is pushed from within the non-global zone, making it possible for
78*9d5056eaSjv227347  * processes within the non-global zone (e.g., ttymon) to pop the anchor and
79*9d5056eaSjv227347  * destroy the zone console's terminal semantics.
80*9d5056eaSjv227347  *
81*9d5056eaSjv227347  * One solution is to make the zcons device hold the slave open while the
82*9d5056eaSjv227347  * associated non-global zone runs so that the STREAMS anchor will always be
83*9d5056eaSjv227347  * associated with the global zone.  Unfortunately, the slave cannot be opened
84*9d5056eaSjv227347  * from within the zcons driver because the driver is not reentrant: it has
85*9d5056eaSjv227347  * an outer STREAMS perimeter.  Therefore, the next best option is for zcons to
86*9d5056eaSjv227347  * provide an ioctl interface to zoneadmd to manage holding and releasing
87*9d5056eaSjv227347  * the slave side of the console.  It is sufficient to hold the slave side's
88*9d5056eaSjv227347  * vnode and bump the associated snode's reference count to preserve the slave's
89*9d5056eaSjv227347  * STREAMS configuration while the associated zone runs, so that's what the
90*9d5056eaSjv227347  * ioctls do.
91*9d5056eaSjv227347  *
92*9d5056eaSjv227347  *
93*9d5056eaSjv227347  * ZC_HOLDSLAVE
94*9d5056eaSjv227347  *
95*9d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively gets a
96*9d5056eaSjv227347  * reference to the slave side's minor node's vnode and bumps the associated
97*9d5056eaSjv227347  * snode's reference count.  The vnode reference is stored in the zcons device
98*9d5056eaSjv227347  * node's soft state.  This ioctl succeeds if the given file descriptor refers
99*9d5056eaSjv227347  * to the slave side's minor node or if there is already a reference to the
100*9d5056eaSjv227347  * slave side's minor node's vnode in the device's soft state.
101*9d5056eaSjv227347  *
102*9d5056eaSjv227347  *
103*9d5056eaSjv227347  * ZC_RELEASESLAVE
104*9d5056eaSjv227347  *
105*9d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively releases
106*9d5056eaSjv227347  * the vnode reference stored in the zcons device node's soft state (which was
107*9d5056eaSjv227347  * previously acquired via ZC_HOLDSLAVE) and decrements the reference count of
108*9d5056eaSjv227347  * the snode associated with the vnode.  This ioctl succeeds if the given file
109*9d5056eaSjv227347  * descriptor refers to the slave side's minor node or if no reference to the
110*9d5056eaSjv227347  * slave side's minor node's vnode is stored in the device's soft state.
111*9d5056eaSjv227347  *
112*9d5056eaSjv227347  *
113*9d5056eaSjv227347  * Note that the file descriptor arguments for both ioctls must be cast to
114*9d5056eaSjv227347  * integers of pointer width.
115*9d5056eaSjv227347  *
116*9d5056eaSjv227347  * Here's how the dance between zcons and zoneadmd works:
117*9d5056eaSjv227347  *
118*9d5056eaSjv227347  *     Zone boot:
119*9d5056eaSjv227347  *     1.  While booting the zone, zoneadmd creates an instance of zcons.
120*9d5056eaSjv227347  *     2.  zoneadmd opens the master and slave sides of the new zone console
121*9d5056eaSjv227347  *         and issues the ZC_HOLDSLAVE ioctl on the master side, passing its
122*9d5056eaSjv227347  *         file descriptor for the slave side as the ioctl argument.
123*9d5056eaSjv227347  *     3.  zcons holds the slave side's vnode, bumps the snode's reference
124*9d5056eaSjv227347  *         count, and stores a pointer to the vnode in the device's soft
125*9d5056eaSjv227347  *         state.
126*9d5056eaSjv227347  *     4.  zoneadmd closes the master and slave sides and continues to boot
127*9d5056eaSjv227347  *         the zone.
128*9d5056eaSjv227347  *
129*9d5056eaSjv227347  *     Zone halt:
130*9d5056eaSjv227347  *     1.  While halting the zone, zoneadmd opens the master and slave sides
131*9d5056eaSjv227347  *         of the zone's console and issues the ZC_RELEASESLAVE ioctl on the
132*9d5056eaSjv227347  *         master side, passing its file descriptor for the slave side as the
133*9d5056eaSjv227347  *         ioctl argument.
134*9d5056eaSjv227347  *     2.  zcons decrements the slave side's snode's reference count, releases
135*9d5056eaSjv227347  *         the slave's vnode, and eliminates its reference to the vnode in the
136*9d5056eaSjv227347  *         device's soft state.
137*9d5056eaSjv227347  *     3.  zoneadmd closes the master and slave sides.
138*9d5056eaSjv227347  *     4.  zoneadmd destroys the zcons device and continues to halt the zone.
139*9d5056eaSjv227347  *
140*9d5056eaSjv227347  * It is necessary for zoneadmd to hold the slave open while issuing
141*9d5056eaSjv227347  * ZC_RELEASESLAVE because zcons might otherwise release the last reference to
142*9d5056eaSjv227347  * the slave's vnode.  If it does, then specfs will panic because it will expect
143*9d5056eaSjv227347  * that the STREAMS configuration for the vnode was destroyed, which VN_RELE
144*9d5056eaSjv227347  * doesn't do.  Forcing zoneadmd to hold the slave open guarantees that zcons
145*9d5056eaSjv227347  * won't release the vnode's last reference.  zoneadmd will properly destroy the
146*9d5056eaSjv227347  * vnode and the snode when it closes the file descriptor.
147*9d5056eaSjv227347  *
148*9d5056eaSjv227347  * Technically, any process that can access the master side can issue these
149*9d5056eaSjv227347  * 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>
161*9d5056eaSjv227347 #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>
173*9d5056eaSjv227347 #include <sys/vnode.h>
174*9d5056eaSjv227347 #include <sys/fs/snode.h>
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1777c478bd9Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
1787c478bd9Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
1817c478bd9Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *);
1827c478bd9Sstevel@tonic-gate static void zc_wput(queue_t *, mblk_t *);
1837c478bd9Sstevel@tonic-gate static void zc_rsrv(queue_t *);
1847c478bd9Sstevel@tonic-gate static void zc_wsrv(queue_t *);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * The instance number is encoded in the dev_t in the minor number; the lowest
1887c478bd9Sstevel@tonic-gate  * bit of the minor number is used to track the master vs. slave side of the
1897c478bd9Sstevel@tonic-gate  * virtual console.  The rest of the bits in the minor number are the instance.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate #define	ZC_MASTER_MINOR		0
1927c478bd9Sstevel@tonic-gate #define	ZC_SLAVE_MINOR		1
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #define	ZC_INSTANCE(x)		(getminor((x)) >> 1)
1957c478bd9Sstevel@tonic-gate #define	ZC_NODE(x)		(getminor((x)) & 0x01)
1967c478bd9Sstevel@tonic-gate 
197*9d5056eaSjv227347 /*
198*9d5056eaSjv227347  * This macro converts a zc_state_t pointer to the associated slave minor node's
199*9d5056eaSjv227347  * dev_t.
200*9d5056eaSjv227347  */
201*9d5056eaSjv227347 #define	ZC_STATE_TO_SLAVEDEV(x)	(makedevice(ddi_driver_major((x)->zc_devinfo), \
202*9d5056eaSjv227347 	(minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SLAVE_MINOR)))
203*9d5056eaSjv227347 
2047c478bd9Sstevel@tonic-gate int zcons_debug = 0;
2057c478bd9Sstevel@tonic-gate #define	DBG(a)   if (zcons_debug) cmn_err(CE_NOTE, a)
2067c478bd9Sstevel@tonic-gate #define	DBG1(a, b)   if (zcons_debug) cmn_err(CE_NOTE, a, b)
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Zone Console Pseudo Terminal Module: stream data structure definitions
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static struct module_info zc_info = {
2137c478bd9Sstevel@tonic-gate 	31337,	/* c0z we r hAx0rs */
2147c478bd9Sstevel@tonic-gate 	"zcons",
2157c478bd9Sstevel@tonic-gate 	0,
2167c478bd9Sstevel@tonic-gate 	INFPSZ,
2177c478bd9Sstevel@tonic-gate 	2048,
2187c478bd9Sstevel@tonic-gate 	128
2197c478bd9Sstevel@tonic-gate };
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static struct qinit zc_rinit = {
2227c478bd9Sstevel@tonic-gate 	NULL,
2237c478bd9Sstevel@tonic-gate 	(int (*)()) zc_rsrv,
2247c478bd9Sstevel@tonic-gate 	zc_open,
2257c478bd9Sstevel@tonic-gate 	zc_close,
2267c478bd9Sstevel@tonic-gate 	NULL,
2277c478bd9Sstevel@tonic-gate 	&zc_info,
2287c478bd9Sstevel@tonic-gate 	NULL
2297c478bd9Sstevel@tonic-gate };
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static struct qinit zc_winit = {
2327c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wput,
2337c478bd9Sstevel@tonic-gate 	(int (*)()) zc_wsrv,
2347c478bd9Sstevel@tonic-gate 	NULL,
2357c478bd9Sstevel@tonic-gate 	NULL,
2367c478bd9Sstevel@tonic-gate 	NULL,
2377c478bd9Sstevel@tonic-gate 	&zc_info,
2387c478bd9Sstevel@tonic-gate 	NULL
2397c478bd9Sstevel@tonic-gate };
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static struct streamtab zc_tab_info = {
2427c478bd9Sstevel@tonic-gate 	&zc_rinit,
2437c478bd9Sstevel@tonic-gate 	&zc_winit,
2447c478bd9Sstevel@tonic-gate 	NULL,
2457c478bd9Sstevel@tonic-gate 	NULL
2467c478bd9Sstevel@tonic-gate };
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #define	ZC_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev,	zc_attach, zc_detach, nodev, \
25419397407SSherry Moore 	zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
261c6f42f0eSedp 	&mod_driverops, 	/* Type of module (this is a pseudo driver) */
262c6f42f0eSedp 	"Zone console driver",	/* description of module */
2637c478bd9Sstevel@tonic-gate 	&zc_ops			/* driver ops */
2647c478bd9Sstevel@tonic-gate };
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2677c478bd9Sstevel@tonic-gate 	MODREV_1,
2687c478bd9Sstevel@tonic-gate 	&modldrv,
2697c478bd9Sstevel@tonic-gate 	NULL
2707c478bd9Sstevel@tonic-gate };
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate typedef struct zc_state {
2737c478bd9Sstevel@tonic-gate 	dev_info_t *zc_devinfo;
2747c478bd9Sstevel@tonic-gate 	queue_t *zc_master_rdq;
2757c478bd9Sstevel@tonic-gate 	queue_t *zc_slave_rdq;
276*9d5056eaSjv227347 	vnode_t *zc_slave_vnode;
2777c478bd9Sstevel@tonic-gate 	int zc_state;
2787c478bd9Sstevel@tonic-gate } zc_state_t;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #define	ZC_STATE_MOPEN	0x01
2817c478bd9Sstevel@tonic-gate #define	ZC_STATE_SOPEN	0x02
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static void *zc_soft_state;
2847c478bd9Sstevel@tonic-gate 
285*9d5056eaSjv227347 /*
286*9d5056eaSjv227347  * List of STREAMS modules that should be pushed onto every slave instance.
287*9d5056eaSjv227347  */
288*9d5056eaSjv227347 static char *zcons_mods[] = {
289*9d5056eaSjv227347 	"ptem",
290*9d5056eaSjv227347 	"ldterm",
291*9d5056eaSjv227347 	"ttcompat",
292*9d5056eaSjv227347 	NULL
293*9d5056eaSjv227347 };
294*9d5056eaSjv227347 
2957c478bd9Sstevel@tonic-gate int
2967c478bd9Sstevel@tonic-gate _init(void)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int err;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&zc_soft_state,
3017c478bd9Sstevel@tonic-gate 	    sizeof (zc_state_t), 0)) != 0) {
3027c478bd9Sstevel@tonic-gate 		return (err);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0)
3067c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(zc_soft_state);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (err);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate int
3137c478bd9Sstevel@tonic-gate _fini(void)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	int err;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0) {
3187c478bd9Sstevel@tonic-gate 		return (err);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&zc_soft_state);
3227c478bd9Sstevel@tonic-gate 	return (0);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate int
3267c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
331*9d5056eaSjv227347 /*
332*9d5056eaSjv227347  * This is a convenience function that clears a device's autopush configuration.
333*9d5056eaSjv227347  * It is meant to be used on the slave side of the console.  Unlike calling
334*9d5056eaSjv227347  * kstr_autopush() directly, this function outputs a warning via cmn_err() if
335*9d5056eaSjv227347  * kstr_autopush() fails.  'dip' must be non-NULL in debug builds.  Both
336*9d5056eaSjv227347  * 'major' and 'minor' must be valid.
337*9d5056eaSjv227347  */
338*9d5056eaSjv227347 static void
339*9d5056eaSjv227347 zc_clearautopush(dev_info_t *dip, major_t major, minor_t minor)
340*9d5056eaSjv227347 {
341*9d5056eaSjv227347 	char *devicepathp;
342*9d5056eaSjv227347 
343*9d5056eaSjv227347 	if (kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL, NULL) !=
344*9d5056eaSjv227347 	    0 && zcons_debug != 0) {
345*9d5056eaSjv227347 		devicepathp = (char *)kmem_alloc(MAXPATHLEN * sizeof (char),
346*9d5056eaSjv227347 		    KM_SLEEP);
347*9d5056eaSjv227347 		(void) ddi_pathname(dip, devicepathp);
348*9d5056eaSjv227347 		cmn_err(CE_NOTE, "zc_detach: could not clear sad configuration "
349*9d5056eaSjv227347 		    "for device %s\n", devicepathp);
350*9d5056eaSjv227347 		kmem_free(devicepathp, MAXPATHLEN * sizeof (char));
351*9d5056eaSjv227347 	}
352*9d5056eaSjv227347 }
353*9d5056eaSjv227347 
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3587c478bd9Sstevel@tonic-gate 	int instance;
359*9d5056eaSjv227347 	major_t major;
360*9d5056eaSjv227347 	minor_t minor;
361*9d5056eaSjv227347 	minor_t lastminor;
362*9d5056eaSjv227347 	uint_t anchorindex;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3657c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3687c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
3697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3707c478bd9Sstevel@tonic-gate 
371*9d5056eaSjv227347 	/*
372*9d5056eaSjv227347 	 * Set up sad(7D) so that the necessary STREAMS modules will be in place
373*9d5056eaSjv227347 	 * when the slave is opened.  A wrinkle is that 'ptem' must be anchored
374*9d5056eaSjv227347 	 * in place (see streamio(7i)) because we always want the console to
375*9d5056eaSjv227347 	 * have terminal semantics.
376*9d5056eaSjv227347 	 */
377*9d5056eaSjv227347 	minor = instance << 1 | ZC_SLAVE_MINOR;
378*9d5056eaSjv227347 	lastminor = 0;
379*9d5056eaSjv227347 	major = ddi_driver_major(dip);
380*9d5056eaSjv227347 	anchorindex = 1;
381*9d5056eaSjv227347 	if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor,
382*9d5056eaSjv227347 	    &anchorindex, zcons_mods) != 0)
383*9d5056eaSjv227347 		goto commonfail;
384*9d5056eaSjv227347 
385*9d5056eaSjv227347 	/*
386*9d5056eaSjv227347 	 * Create the master and slave minor nodes.
387*9d5056eaSjv227347 	 */
388*9d5056eaSjv227347 	if ((ddi_create_minor_node(dip, ZCONS_SLAVE_NAME, S_IFCHR, minor,
389*9d5056eaSjv227347 	    DDI_PSEUDO, 0) == DDI_FAILURE) ||
3907c478bd9Sstevel@tonic-gate 	    (ddi_create_minor_node(dip, ZCONS_MASTER_NAME, S_IFCHR,
391*9d5056eaSjv227347 	    instance << 1 | ZC_MASTER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE))
392*9d5056eaSjv227347 		goto failwithautopush;
3937c478bd9Sstevel@tonic-gate 
394*9d5056eaSjv227347 	VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL);
3957c478bd9Sstevel@tonic-gate 	zcs->zc_devinfo = dip;
3967c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
397*9d5056eaSjv227347 
398*9d5056eaSjv227347 failwithautopush:
399*9d5056eaSjv227347 	zc_clearautopush(dip, major, minor);
400*9d5056eaSjv227347 	ddi_remove_minor_node(dip, NULL);
401*9d5056eaSjv227347 commonfail:
402*9d5056eaSjv227347 	ddi_soft_state_free(zc_soft_state, instance);
403*9d5056eaSjv227347 	return (DDI_FAILURE);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static int
4077c478bd9Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
4107c478bd9Sstevel@tonic-gate 	int instance;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
4137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4167c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) ||
4207c478bd9Sstevel@tonic-gate 	    (zcs->zc_state & ZC_STATE_SOPEN)) {
4217c478bd9Sstevel@tonic-gate 		DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
4227c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
425*9d5056eaSjv227347 	/*
426*9d5056eaSjv227347 	 * Clear the sad configuration so that reattaching doesn't fail to
427*9d5056eaSjv227347 	 * set up sad configuration.
428*9d5056eaSjv227347 	 */
429*9d5056eaSjv227347 	zc_clearautopush(dip, ddi_driver_major(dip), instance << 1 |
430*9d5056eaSjv227347 	    ZC_SLAVE_MINOR);
431*9d5056eaSjv227347 
4327c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
4337c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(zc_soft_state, instance);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * zc_getinfo()
4407c478bd9Sstevel@tonic-gate  *	getinfo(9e) entrypoint.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4437c478bd9Sstevel@tonic-gate static int
4447c478bd9Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
4477c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE((dev_t)arg);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4507c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4517c478bd9Sstevel@tonic-gate 		if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4527c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4537c478bd9Sstevel@tonic-gate 		*result = zcs->zc_devinfo;
4547c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4557c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4567c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
4577c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Return the equivalent queue from the other side of the relationship.
4647c478bd9Sstevel@tonic-gate  * e.g.: given the slave's write queue, return the master's write queue.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate static queue_t *
4677c478bd9Sstevel@tonic-gate zc_switch(queue_t *qp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4707c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq)
4737c478bd9Sstevel@tonic-gate 		return (zcs->zc_slave_rdq);
4747c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_master_rdq && zcs->zc_slave_rdq != NULL)
4757c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_slave_rdq));
4767c478bd9Sstevel@tonic-gate 	else if (qp == zcs->zc_slave_rdq)
4777c478bd9Sstevel@tonic-gate 		return (zcs->zc_master_rdq);
4787c478bd9Sstevel@tonic-gate 	else if (OTHERQ(qp) == zcs->zc_slave_rdq && zcs->zc_master_rdq != NULL)
4797c478bd9Sstevel@tonic-gate 		return (OTHERQ(zcs->zc_master_rdq));
4807c478bd9Sstevel@tonic-gate 	else
4817c478bd9Sstevel@tonic-gate 		return (NULL);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * For debugging and outputting messages.  Returns the name of the side of
4867c478bd9Sstevel@tonic-gate  * the relationship associated with this queue.
4877c478bd9Sstevel@tonic-gate  */
4887c478bd9Sstevel@tonic-gate static const char *
4897c478bd9Sstevel@tonic-gate zc_side(queue_t *qp)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4927c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (qp == zcs->zc_master_rdq ||
4957c478bd9Sstevel@tonic-gate 	    OTHERQ(qp) == zcs->zc_master_rdq) {
4967c478bd9Sstevel@tonic-gate 		return ("master");
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_slave_rdq || OTHERQ(qp) == zcs->zc_slave_rdq);
4997c478bd9Sstevel@tonic-gate 	return ("slave");
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5037c478bd9Sstevel@tonic-gate static int
5047c478bd9Sstevel@tonic-gate zc_master_open(zc_state_t *zcs,
5057c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
5067c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
5077c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
5087c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
5097c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	mblk_t *mop;
5127c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/*
5157c478bd9Sstevel@tonic-gate 	 * Enforce exclusivity on the master side; the only consumer should
5167c478bd9Sstevel@tonic-gate 	 * be the zoneadmd for the zone.
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
5197c478bd9Sstevel@tonic-gate 		return (EBUSY);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
5227c478bd9Sstevel@tonic-gate 		DBG("zc_master_open(): mop allocation failed\n");
5237c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_MOPEN;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
5307c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
5337c478bd9Sstevel@tonic-gate 	qprocson(rqp);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * Following qprocson(), the master side is fully plumbed into the
5377c478bd9Sstevel@tonic-gate 	 * STREAM and may send/receive messages.  Setting zcs->zc_master_rdq
5387c478bd9Sstevel@tonic-gate 	 * will allow the slave to send messages to us (the master).
5397c478bd9Sstevel@tonic-gate 	 * This cannot occur before qprocson() because the master is not
5407c478bd9Sstevel@tonic-gate 	 * ready to process them until that point.
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	zcs->zc_master_rdq = rqp;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
5467c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
5497c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
55027e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
5517c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
5527c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
5537c478bd9Sstevel@tonic-gate 	else
5547c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
5557c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
5567c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
5577c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (0);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5637c478bd9Sstevel@tonic-gate static int
5647c478bd9Sstevel@tonic-gate zc_slave_open(zc_state_t *zcs,
5657c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
5667c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
5677c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
5687c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
5697c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	mblk_t *mop;
5727c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * The slave side can be opened as many times as needed.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
5787c478bd9Sstevel@tonic-gate 		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
5797c478bd9Sstevel@tonic-gate 		return (0);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
5837c478bd9Sstevel@tonic-gate 		DBG("zc_slave_open(): mop allocation failed\n");
5847c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_SOPEN;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
5917c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
5927c478bd9Sstevel@tonic-gate 	 */
5937c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	qprocson(rqp);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * Must follow qprocson(), since we aren't ready to process until then.
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	zcs->zc_slave_rdq = rqp;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/*
6037c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
6047c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
6077c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
60827e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
6097c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
6107c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
6117c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
6127c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	return (0);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate  * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate static int
6217c478bd9Sstevel@tonic-gate zc_open(queue_t *rqp,		/* pointer to the read side queue */
6227c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
6237c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
6247c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
6257c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE(*devp);
6287c478bd9Sstevel@tonic-gate 	int ret;
6297c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (sflag != 0)
6327c478bd9Sstevel@tonic-gate 		return (EINVAL);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
6357c478bd9Sstevel@tonic-gate 		return (ENXIO);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	switch (ZC_NODE(*devp)) {
6387c478bd9Sstevel@tonic-gate 	case ZC_MASTER_MINOR:
6397c478bd9Sstevel@tonic-gate 		ret = zc_master_open(zcs, rqp, devp, oflag, sflag, credp);
6407c478bd9Sstevel@tonic-gate 		break;
6417c478bd9Sstevel@tonic-gate 	case ZC_SLAVE_MINOR:
6427c478bd9Sstevel@tonic-gate 		ret = zc_slave_open(zcs, rqp, devp, oflag, sflag, credp);
6437c478bd9Sstevel@tonic-gate 		break;
6447c478bd9Sstevel@tonic-gate 	default:
6457c478bd9Sstevel@tonic-gate 		ret = ENXIO;
6467c478bd9Sstevel@tonic-gate 		break;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (ret);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * close(9e) entrypoint.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
6567c478bd9Sstevel@tonic-gate static int
6577c478bd9Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	queue_t *wqp;
6607c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
6617c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)rqp->q_ptr;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (rqp == zcs->zc_master_rdq) {
6667c478bd9Sstevel@tonic-gate 		DBG("Closing master side");
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		zcs->zc_master_rdq = NULL;
6697c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_MOPEN;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		/*
6727c478bd9Sstevel@tonic-gate 		 * qenable slave side write queue so that it can flush
6737c478bd9Sstevel@tonic-gate 		 * its messages as master's read queue is going away
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		if (zcs->zc_slave_rdq != NULL) {
6767c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_slave_rdq));
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
6807c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	} else if (rqp == zcs->zc_slave_rdq) {
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		DBG("Closing slave side");
6857c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_SOPEN;
6867c478bd9Sstevel@tonic-gate 		zcs->zc_slave_rdq = NULL;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		wqp = WR(rqp);
6897c478bd9Sstevel@tonic-gate 		while ((bp = getq(wqp)) != NULL) {
6907c478bd9Sstevel@tonic-gate 			if (zcs->zc_master_rdq != NULL)
6917c478bd9Sstevel@tonic-gate 				putnext(zcs->zc_master_rdq, bp);
6927c478bd9Sstevel@tonic-gate 			else if (bp->b_datap->db_type == M_IOCTL)
6937c478bd9Sstevel@tonic-gate 				miocnak(wqp, bp, 0, 0);
6947c478bd9Sstevel@tonic-gate 			else
6957c478bd9Sstevel@tonic-gate 				freemsg(bp);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		/*
6997c478bd9Sstevel@tonic-gate 		 * Qenable master side write queue so that it can flush its
7007c478bd9Sstevel@tonic-gate 		 * messages as slaves's read queue is going away.
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		if (zcs->zc_master_rdq != NULL)
7037c478bd9Sstevel@tonic-gate 			qenable(WR(zcs->zc_master_rdq));
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
7067c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	return (0);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate static void
7137c478bd9Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
7167c478bd9Sstevel@tonic-gate 	DBG1("M_FLUSH on %s side", zc_side(qp));
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHW) {
7197c478bd9Sstevel@tonic-gate 		DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
7207c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHDATA);
7217c478bd9Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
7227c478bd9Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHR) == 0) {
7237c478bd9Sstevel@tonic-gate 			/*
7247c478bd9Sstevel@tonic-gate 			 * FLUSHW only. Change to FLUSHR and putnext other side,
7257c478bd9Sstevel@tonic-gate 			 * then we are done.
7267c478bd9Sstevel@tonic-gate 			 */
7277c478bd9Sstevel@tonic-gate 			*mp->b_rptr |= FLUSHR;
7287c478bd9Sstevel@tonic-gate 			if (zc_switch(RD(qp)) != NULL) {
7297c478bd9Sstevel@tonic-gate 				putnext(zc_switch(RD(qp)), mp);
7307c478bd9Sstevel@tonic-gate 				return;
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 		} else if ((zc_switch(RD(qp)) != NULL) &&
7337c478bd9Sstevel@tonic-gate 		    (nmp = copyb(mp)) != NULL) {
7347c478bd9Sstevel@tonic-gate 			/*
7357c478bd9Sstevel@tonic-gate 			 * It is a FLUSHRW; we copy the mblk and send
7367c478bd9Sstevel@tonic-gate 			 * it to the other side, since we still need to use
7377c478bd9Sstevel@tonic-gate 			 * the mblk in FLUSHR processing, below.
7387c478bd9Sstevel@tonic-gate 			 */
7397c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), nmp);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHR) {
7447c478bd9Sstevel@tonic-gate 		DBG("qreply(qp) turning FLUSHR around\n");
7457c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
7467c478bd9Sstevel@tonic-gate 		return;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	freemsg(mp);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * wput(9E) is symmetric for master and slave sides, so this handles both
753*9d5056eaSjv227347  * without splitting the codepath.  (The only exception to this is the
754*9d5056eaSjv227347  * processing of zcons ioctls, which is restricted to the master side.)
7557c478bd9Sstevel@tonic-gate  *
7567c478bd9Sstevel@tonic-gate  * zc_wput() looks at the other side; if there is no process holding that
7577c478bd9Sstevel@tonic-gate  * side open, it frees the message.  This prevents processes from hanging
7587c478bd9Sstevel@tonic-gate  * if no one is holding open the console.  Otherwise, it putnext's high
7597c478bd9Sstevel@tonic-gate  * priority messages, putnext's normal messages if possible, and otherwise
7607c478bd9Sstevel@tonic-gate  * enqueues the messages; in the case that something is enqueued, wsrv(9E)
7617c478bd9Sstevel@tonic-gate  * will take care of eventually shuttling I/O to the other side.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate static void
7647c478bd9Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
767*9d5056eaSjv227347 	zc_state_t *zcs;
768*9d5056eaSjv227347 	struct iocblk *iocbp;
769*9d5056eaSjv227347 	file_t *slave_filep;
770*9d5056eaSjv227347 	struct snode *slave_snodep;
771*9d5056eaSjv227347 	int slave_fd;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	DBG1("entering zc_wput, %s side", zc_side(qp));
7767c478bd9Sstevel@tonic-gate 
777*9d5056eaSjv227347 	/*
778*9d5056eaSjv227347 	 * Process zcons ioctl messages if qp is the master console's write
779*9d5056eaSjv227347 	 * queue.
780*9d5056eaSjv227347 	 */
781*9d5056eaSjv227347 	zcs = (zc_state_t *)qp->q_ptr;
782*9d5056eaSjv227347 	if (zcs->zc_master_rdq != NULL && qp == WR(zcs->zc_master_rdq) &&
783*9d5056eaSjv227347 	    type == M_IOCTL) {
784*9d5056eaSjv227347 		iocbp = (struct iocblk *)(void *)mp->b_rptr;
785*9d5056eaSjv227347 		switch (iocbp->ioc_cmd) {
786*9d5056eaSjv227347 		case ZC_HOLDSLAVE:
787*9d5056eaSjv227347 			/*
788*9d5056eaSjv227347 			 * Hold the slave's vnode and increment the refcount
789*9d5056eaSjv227347 			 * of the snode.  If the vnode is already held, then
790*9d5056eaSjv227347 			 * indicate success.
791*9d5056eaSjv227347 			 */
792*9d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
793*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
794*9d5056eaSjv227347 				return;
795*9d5056eaSjv227347 			}
796*9d5056eaSjv227347 			if (zcs->zc_slave_vnode != NULL) {
797*9d5056eaSjv227347 				miocack(qp, mp, 0, 0);
798*9d5056eaSjv227347 				return;
799*9d5056eaSjv227347 			}
800*9d5056eaSjv227347 
801*9d5056eaSjv227347 			/*
802*9d5056eaSjv227347 			 * The calling process must pass a file descriptor for
803*9d5056eaSjv227347 			 * the slave device.
804*9d5056eaSjv227347 			 */
805*9d5056eaSjv227347 			slave_fd =
806*9d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
807*9d5056eaSjv227347 			    b_rptr;
808*9d5056eaSjv227347 			slave_filep = getf(slave_fd);
809*9d5056eaSjv227347 			if (slave_filep == NULL) {
810*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
811*9d5056eaSjv227347 				return;
812*9d5056eaSjv227347 			}
813*9d5056eaSjv227347 			if (ZC_STATE_TO_SLAVEDEV(zcs) !=
814*9d5056eaSjv227347 			    slave_filep->f_vnode->v_rdev) {
815*9d5056eaSjv227347 				releasef(slave_fd);
816*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
817*9d5056eaSjv227347 				return;
818*9d5056eaSjv227347 			}
819*9d5056eaSjv227347 
820*9d5056eaSjv227347 			/*
821*9d5056eaSjv227347 			 * Get a reference to the slave's vnode.  Also bump the
822*9d5056eaSjv227347 			 * reference count on the associated snode.
823*9d5056eaSjv227347 			 */
824*9d5056eaSjv227347 			ASSERT(vn_matchops(slave_filep->f_vnode,
825*9d5056eaSjv227347 			    spec_getvnodeops()));
826*9d5056eaSjv227347 			zcs->zc_slave_vnode = slave_filep->f_vnode;
827*9d5056eaSjv227347 			VN_HOLD(zcs->zc_slave_vnode);
828*9d5056eaSjv227347 			slave_snodep = VTOCS(zcs->zc_slave_vnode);
829*9d5056eaSjv227347 			mutex_enter(&slave_snodep->s_lock);
830*9d5056eaSjv227347 			++slave_snodep->s_count;
831*9d5056eaSjv227347 			mutex_exit(&slave_snodep->s_lock);
832*9d5056eaSjv227347 			releasef(slave_fd);
833*9d5056eaSjv227347 			miocack(qp, mp, 0, 0);
834*9d5056eaSjv227347 			return;
835*9d5056eaSjv227347 		case ZC_RELEASESLAVE:
836*9d5056eaSjv227347 			/*
837*9d5056eaSjv227347 			 * Release the master's handle on the slave's vnode.
838*9d5056eaSjv227347 			 * If there isn't a handle for the vnode, then indicate
839*9d5056eaSjv227347 			 * success.
840*9d5056eaSjv227347 			 */
841*9d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
842*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
843*9d5056eaSjv227347 				return;
844*9d5056eaSjv227347 			}
845*9d5056eaSjv227347 			if (zcs->zc_slave_vnode == NULL) {
846*9d5056eaSjv227347 				miocack(qp, mp, 0, 0);
847*9d5056eaSjv227347 				return;
848*9d5056eaSjv227347 			}
849*9d5056eaSjv227347 
850*9d5056eaSjv227347 			/*
851*9d5056eaSjv227347 			 * The process that passed the ioctl must have provided
852*9d5056eaSjv227347 			 * a file descriptor for the slave device.  Make sure
853*9d5056eaSjv227347 			 * this is correct.
854*9d5056eaSjv227347 			 */
855*9d5056eaSjv227347 			slave_fd =
856*9d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
857*9d5056eaSjv227347 			    b_rptr;
858*9d5056eaSjv227347 			slave_filep = getf(slave_fd);
859*9d5056eaSjv227347 			if (slave_filep == NULL) {
860*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
861*9d5056eaSjv227347 				return;
862*9d5056eaSjv227347 			}
863*9d5056eaSjv227347 			if (zcs->zc_slave_vnode->v_rdev !=
864*9d5056eaSjv227347 			    slave_filep->f_vnode->v_rdev) {
865*9d5056eaSjv227347 				releasef(slave_fd);
866*9d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
867*9d5056eaSjv227347 				return;
868*9d5056eaSjv227347 			}
869*9d5056eaSjv227347 
870*9d5056eaSjv227347 			/*
871*9d5056eaSjv227347 			 * Decrement the snode's reference count and release the
872*9d5056eaSjv227347 			 * vnode.
873*9d5056eaSjv227347 			 */
874*9d5056eaSjv227347 			ASSERT(vn_matchops(slave_filep->f_vnode,
875*9d5056eaSjv227347 			    spec_getvnodeops()));
876*9d5056eaSjv227347 			slave_snodep = VTOCS(zcs->zc_slave_vnode);
877*9d5056eaSjv227347 			mutex_enter(&slave_snodep->s_lock);
878*9d5056eaSjv227347 			--slave_snodep->s_count;
879*9d5056eaSjv227347 			mutex_exit(&slave_snodep->s_lock);
880*9d5056eaSjv227347 			VN_RELE(zcs->zc_slave_vnode);
881*9d5056eaSjv227347 			zcs->zc_slave_vnode = NULL;
882*9d5056eaSjv227347 			releasef(slave_fd);
883*9d5056eaSjv227347 			miocack(qp, mp, 0, 0);
884*9d5056eaSjv227347 			return;
885*9d5056eaSjv227347 		default:
886*9d5056eaSjv227347 			break;
887*9d5056eaSjv227347 		}
888*9d5056eaSjv227347 	}
889*9d5056eaSjv227347 
8907c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
8917c478bd9Sstevel@tonic-gate 		DBG1("wput to %s side (no one listening)", zc_side(qp));
8927c478bd9Sstevel@tonic-gate 		switch (type) {
8937c478bd9Sstevel@tonic-gate 		case M_FLUSH:
8947c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 		case M_IOCTL:
8977c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, 0);
8987c478bd9Sstevel@tonic-gate 			break;
8997c478bd9Sstevel@tonic-gate 		default:
9007c478bd9Sstevel@tonic-gate 			freemsg(mp);
9017c478bd9Sstevel@tonic-gate 			break;
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 		return;
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
9077c478bd9Sstevel@tonic-gate 		DBG1("(hipri) wput, %s side", zc_side(qp));
9087c478bd9Sstevel@tonic-gate 		switch (type) {
9097c478bd9Sstevel@tonic-gate 		case M_READ:		/* supposedly from ldterm? */
9107c478bd9Sstevel@tonic-gate 			DBG("zc_wput: tossing M_READ\n");
9117c478bd9Sstevel@tonic-gate 			freemsg(mp);
9127c478bd9Sstevel@tonic-gate 			break;
9137c478bd9Sstevel@tonic-gate 		case M_FLUSH:
9147c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
9157c478bd9Sstevel@tonic-gate 			break;
9167c478bd9Sstevel@tonic-gate 		default:
9177c478bd9Sstevel@tonic-gate 			/*
9187c478bd9Sstevel@tonic-gate 			 * Put this to the other side.
9197c478bd9Sstevel@tonic-gate 			 */
9207c478bd9Sstevel@tonic-gate 			ASSERT(zc_switch(RD(qp)) != NULL);
9217c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), mp);
9227c478bd9Sstevel@tonic-gate 			break;
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 		DBG1("done (hipri) wput, %s side", zc_side(qp));
9257c478bd9Sstevel@tonic-gate 		return;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Only putnext if there isn't already something in the queue.
9307c478bd9Sstevel@tonic-gate 	 * otherwise things would wind up out of order.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
9337c478bd9Sstevel@tonic-gate 		DBG("wput: putting message to other side\n");
9347c478bd9Sstevel@tonic-gate 		putnext(RD(zc_switch(qp)), mp);
9357c478bd9Sstevel@tonic-gate 	} else {
9367c478bd9Sstevel@tonic-gate 		DBG("wput: putting msg onto queue\n");
9377c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 	DBG1("done wput, %s side", zc_side(qp));
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * rsrv(9E) is symmetric for master and slave, so zc_rsrv() handles both
9447c478bd9Sstevel@tonic-gate  * without splitting up the codepath.
9457c478bd9Sstevel@tonic-gate  *
9467c478bd9Sstevel@tonic-gate  * Enable the write side of the partner.  This triggers the partner to send
9477c478bd9Sstevel@tonic-gate  * messages queued on its write side to this queue's read side.
9487c478bd9Sstevel@tonic-gate  */
9497c478bd9Sstevel@tonic-gate static void
9507c478bd9Sstevel@tonic-gate zc_rsrv(queue_t *qp)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
9537c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)qp->q_ptr;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/*
9567c478bd9Sstevel@tonic-gate 	 * Care must be taken here, as either of the master or slave side
9577c478bd9Sstevel@tonic-gate 	 * qptr could be NULL.
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate 	ASSERT(qp == zcs->zc_master_rdq || qp == zcs->zc_slave_rdq);
9607c478bd9Sstevel@tonic-gate 	if (zc_switch(qp) == NULL) {
9617c478bd9Sstevel@tonic-gate 		DBG("zc_rsrv: other side isn't listening\n");
9627c478bd9Sstevel@tonic-gate 		return;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	qenable(WR(zc_switch(qp)));
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate  * This routine is symmetric for master and slave, so it handles both without
9697c478bd9Sstevel@tonic-gate  * splitting up the codepath.
9707c478bd9Sstevel@tonic-gate  *
9717c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to the other, send
9727c478bd9Sstevel@tonic-gate  * them via putnext(). Else, if queued messages cannot be sent, leave them
9737c478bd9Sstevel@tonic-gate  * on this queue.
9747c478bd9Sstevel@tonic-gate  */
9757c478bd9Sstevel@tonic-gate static void
9767c478bd9Sstevel@tonic-gate zc_wsrv(queue_t *qp)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	DBG1("zc_wsrv master (%s) side", zc_side(qp));
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * Partner has no read queue, so take the data, and throw it away.
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
9867c478bd9Sstevel@tonic-gate 		DBG("zc_wsrv: other side isn't listening");
9877c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
9887c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
9897c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, 0);
9907c478bd9Sstevel@tonic-gate 			else
9917c478bd9Sstevel@tonic-gate 				freemsg(mp);
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
9947c478bd9Sstevel@tonic-gate 		return;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
9987c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
10017c478bd9Sstevel@tonic-gate 		/*
10027c478bd9Sstevel@tonic-gate 		 * Due to the way zc_wput is implemented, we should never
10037c478bd9Sstevel@tonic-gate 		 * see a control message here.
10047c478bd9Sstevel@tonic-gate 		 */
10057c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type < QPCTL);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 		if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
10087c478bd9Sstevel@tonic-gate 			DBG("wsrv: send message to other side\n");
10097c478bd9Sstevel@tonic-gate 			putnext(RD(zc_switch(qp)), mp);
10107c478bd9Sstevel@tonic-gate 		} else {
10117c478bd9Sstevel@tonic-gate 			DBG("wsrv: putting msg back on queue\n");
10127c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
10137c478bd9Sstevel@tonic-gate 			break;
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate }
1017