xref: /illumos-gate/usr/src/uts/common/io/zcons.c (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
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
331fa2a664SJoshua M. Clulow  * straightforward way to the commonly used terminal notion of "manager side"
341fa2a664SJoshua M. Clulow  * and "subsidiary 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  *
401fa2a664SJoshua M. Clulow  * The driver shuttles I/O from manager side to subsidiary side and back.  In a
411fa2a664SJoshua M. Clulow  * break from the pts/ptm semantics, if one side is not open, I/O directed
421fa2a664SJoshua M. Clulow  * towards it will simply be discarded.  This is so that if zoneadmd is not
431fa2a664SJoshua M. Clulow  * holding the manager side console open (i.e. it has died somehow), processes
441fa2a664SJoshua M. Clulow  * in 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  *
531fa2a664SJoshua M. Clulow  * MANAGER SIDE IOCTLS
549d5056eaSjv227347  *
551fa2a664SJoshua M. Clulow  * The ZC_HOLDSUBSID and ZC_RELEASESUBSID ioctls instruct the manager side of
561fa2a664SJoshua M. Clulow  * the console to hold and release a reference to the subsidiary side's vnode.
571fa2a664SJoshua M. Clulow  * They are meant to be issued by zoneadmd after the console device node is
581fa2a664SJoshua M. Clulow  * created and before it is destroyed so that the subsidiary's STREAMS anchor,
591fa2a664SJoshua M. Clulow  * ptem, is preserved when ttymon starts popping STREAMS modules from within
601fa2a664SJoshua M. Clulow  * the 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
641fa2a664SJoshua M. Clulow  * (subsidiary side) so that processes within the associated non-global zone
651fa2a664SJoshua M. Clulow  * will fail to pop it off, thus ensuring that the subsidiary will retain
661fa2a664SJoshua M. Clulow  * terminal semantics.  When a process attempts to pop the anchor off of a
671fa2a664SJoshua M. Clulow  * stream, the STREAMS subsystem checks whether the calling process' zone is
681fa2a664SJoshua M. Clulow  * the same as that of the process that pushed the anchor onto the stream and
691fa2a664SJoshua M. Clulow  * cancels the pop if they differ.  zoneadmd used to hold an open file
701fa2a664SJoshua M. Clulow  * descriptor for the subsidiary while the associated non-global zone ran, thus
711fa2a664SJoshua M. Clulow  * ensuring that the subsidiary's STREAMS anchor would never be popped from
721fa2a664SJoshua M. Clulow  * within the non-global zone (because zoneadmd runs in the global zone).
731fa2a664SJoshua M. Clulow  * However, this file descriptor was removed to make zone console management
74*bbf21555SRichard Lowe  * more robust.  sad(4D) is now used to automatically set up the subsidiary's
751fa2a664SJoshua M. Clulow  * STREAMS modules when the zone console is freshly opened within the
761fa2a664SJoshua M. Clulow  * associated non-global zone.  However, when a process within the non-global
771fa2a664SJoshua M. Clulow  * zone freshly opens the zone console, the anchor is pushed from within the
781fa2a664SJoshua M. Clulow  * non-global zone, making it possible for processes within the non-global zone
791fa2a664SJoshua M. Clulow  * (e.g., ttymon) to pop the anchor and destroy the zone console's terminal
801fa2a664SJoshua M. Clulow  * semantics.
819d5056eaSjv227347  *
821fa2a664SJoshua M. Clulow  * One solution is to make the zcons device hold the subsidiary open while the
839d5056eaSjv227347  * associated non-global zone runs so that the STREAMS anchor will always be
841fa2a664SJoshua M. Clulow  * associated with the global zone.  Unfortunately, the subsidiary cannot be
851fa2a664SJoshua M. Clulow  * opened from within the zcons driver because the driver is not reentrant: it
861fa2a664SJoshua M. Clulow  * has an outer STREAMS perimeter.  Therefore, the next best option is for
871fa2a664SJoshua M. Clulow  * zcons to provide an ioctl interface to zoneadmd to manage holding and
881fa2a664SJoshua M. Clulow  * releasing the subsidiary side of the console.  It is sufficient to hold the
891fa2a664SJoshua M. Clulow  * subsidiary side's vnode and bump the associated snode's reference count to
901fa2a664SJoshua M. Clulow  * preserve the subsidiary's STREAMS configuration while the associated zone
911fa2a664SJoshua M. Clulow  * runs, so that's what the ioctls do.
929d5056eaSjv227347  *
939d5056eaSjv227347  *
941fa2a664SJoshua M. Clulow  * ZC_HOLDSUBSID
959d5056eaSjv227347  *
969d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively gets a
971fa2a664SJoshua M. Clulow  * reference to the subsidiary side's minor node's vnode and bumps the
981fa2a664SJoshua M. Clulow  * associated snode's reference count.  The vnode reference is stored in the
991fa2a664SJoshua M. Clulow  * zcons device node's soft state.  This ioctl succeeds if the given file
1001fa2a664SJoshua M. Clulow  * descriptor refers to the subsidiary side's minor node or if there is already
1011fa2a664SJoshua M. Clulow  * a reference to the subsidiary side's minor node's vnode in the device's soft
1021fa2a664SJoshua M. Clulow  * state.
1039d5056eaSjv227347  *
1049d5056eaSjv227347  *
1051fa2a664SJoshua M. Clulow  * ZC_RELEASESUBSID
1069d5056eaSjv227347  *
1079d5056eaSjv227347  * This ioctl takes a file descriptor as an argument.  It effectively releases
1089d5056eaSjv227347  * the vnode reference stored in the zcons device node's soft state (which was
1091fa2a664SJoshua M. Clulow  * previously acquired via ZC_HOLDSUBSID) and decrements the reference count of
1109d5056eaSjv227347  * the snode associated with the vnode.  This ioctl succeeds if the given file
1111fa2a664SJoshua M. Clulow  * descriptor refers to the subsidiary side's minor node or if no reference to
1121fa2a664SJoshua M. Clulow  * the subsidiary side's minor node's vnode is stored in the device's soft
1131fa2a664SJoshua M. Clulow  * state.
1149d5056eaSjv227347  *
1159d5056eaSjv227347  *
1169d5056eaSjv227347  * Note that the file descriptor arguments for both ioctls must be cast to
1179d5056eaSjv227347  * integers of pointer width.
1189d5056eaSjv227347  *
1199d5056eaSjv227347  * Here's how the dance between zcons and zoneadmd works:
1209d5056eaSjv227347  *
1219d5056eaSjv227347  *     Zone boot:
1229d5056eaSjv227347  *     1.  While booting the zone, zoneadmd creates an instance of zcons.
1231fa2a664SJoshua M. Clulow  *     2.  zoneadmd opens the manager and subsidiary sides of the new zone
1241fa2a664SJoshua M. Clulow  *         console and issues the ZC_HOLDSUBSID ioctl on the manager side,
1251fa2a664SJoshua M. Clulow  *         passing its file descriptor for the subsidiary side as the ioctl
1261fa2a664SJoshua M. Clulow  *         argument.
1271fa2a664SJoshua M. Clulow  *     3.  zcons holds the subsidiary side's vnode, bumps the snode's reference
1289d5056eaSjv227347  *         count, and stores a pointer to the vnode in the device's soft
1299d5056eaSjv227347  *         state.
1301fa2a664SJoshua M. Clulow  *     4.  zoneadmd closes the manager and subsidiary sides and continues to
1311fa2a664SJoshua M. Clulow  *         boot the zone.
1329d5056eaSjv227347  *
1339d5056eaSjv227347  *     Zone halt:
1341fa2a664SJoshua M. Clulow  *     1.  While halting the zone, zoneadmd opens the manager and subsidiary
1351fa2a664SJoshua M. Clulow  *         sides of the zone's console and issues the ZC_RELEASESUBSID ioctl on
1361fa2a664SJoshua M. Clulow  *         the manager side, passing its file descriptor for the subsidiary
1371fa2a664SJoshua M. Clulow  *         side as the ioctl argument.
1381fa2a664SJoshua M. Clulow  *     2.  zcons decrements the subsidiary side's snode's reference count,
1391fa2a664SJoshua M. Clulow  *         releases the subsidiary's vnode, and eliminates its reference to the
1401fa2a664SJoshua M. Clulow  *         vnode in the device's soft state.
1411fa2a664SJoshua M. Clulow  *     3.  zoneadmd closes the manager and subsidiary sides.
1429d5056eaSjv227347  *     4.  zoneadmd destroys the zcons device and continues to halt the zone.
1439d5056eaSjv227347  *
1441fa2a664SJoshua M. Clulow  * It is necessary for zoneadmd to hold the subsidiary open while issuing
1451fa2a664SJoshua M. Clulow  * ZC_RELEASESUBSID because zcons might otherwise release the last reference to
1461fa2a664SJoshua M. Clulow  * the subsidiary's vnode.  If it does, then specfs will panic because it will
1471fa2a664SJoshua M. Clulow  * expect that the STREAMS configuration for the vnode was destroyed, which
1481fa2a664SJoshua M. Clulow  * VN_RELE doesn't do.  Forcing zoneadmd to hold the subsidiary open guarantees
1491fa2a664SJoshua M. Clulow  * that zcons won't release the vnode's last reference.  zoneadmd will properly
1501fa2a664SJoshua M. Clulow  * destroy the vnode and the snode when it closes the file descriptor.
1519d5056eaSjv227347  *
1521fa2a664SJoshua M. Clulow  * Technically, any process that can access the manager side can issue these
1539d5056eaSjv227347  * ioctls, but they should be treated as private interfaces for zoneadmd.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #include <sys/types.h>
1577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
1587c478bd9Sstevel@tonic-gate #include <sys/conf.h>
1597c478bd9Sstevel@tonic-gate #include <sys/cred.h>
1607c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1627c478bd9Sstevel@tonic-gate #include <sys/devops.h>
1637c478bd9Sstevel@tonic-gate #include <sys/errno.h>
1647c478bd9Sstevel@tonic-gate #include <sys/file.h>
1659d5056eaSjv227347 #include <sys/kstr.h>
1667c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1677c478bd9Sstevel@tonic-gate #include <sys/param.h>
1687c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1697c478bd9Sstevel@tonic-gate #include <sys/stream.h>
1707c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
1717c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
1727c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1737c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1747c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1757c478bd9Sstevel@tonic-gate #include <sys/types.h>
1767c478bd9Sstevel@tonic-gate #include <sys/zcons.h>
1779d5056eaSjv227347 #include <sys/vnode.h>
1789d5056eaSjv227347 #include <sys/fs/snode.h>
1796a1b30f3Sjv227347 #include <sys/zone.h>
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static int zc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
1827c478bd9Sstevel@tonic-gate static int zc_attach(dev_info_t *, ddi_attach_cmd_t);
1837c478bd9Sstevel@tonic-gate static int zc_detach(dev_info_t *, ddi_detach_cmd_t);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static int zc_open(queue_t *, dev_t *, int, int, cred_t *);
1867c478bd9Sstevel@tonic-gate static int zc_close(queue_t *, int, cred_t *);
1870e2db3e7SToomas Soome static int zc_wput(queue_t *, mblk_t *);
1880e2db3e7SToomas Soome static int zc_rsrv(queue_t *);
1890e2db3e7SToomas Soome static int zc_wsrv(queue_t *);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * The instance number is encoded in the dev_t in the minor number; the lowest
1931fa2a664SJoshua M. Clulow  * bit of the minor number is used to track the manager vs. subsidiary side of
1941fa2a664SJoshua M. Clulow  * the virtual console.  The rest of the bits in the minor number are the
1951fa2a664SJoshua M. Clulow  * instance.
1967c478bd9Sstevel@tonic-gate  */
1971fa2a664SJoshua M. Clulow #define	ZC_MANAGER_MINOR	0
1981fa2a664SJoshua M. Clulow #define	ZC_SUBSID_MINOR		1
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #define	ZC_INSTANCE(x)		(getminor((x)) >> 1)
2017c478bd9Sstevel@tonic-gate #define	ZC_NODE(x)		(getminor((x)) & 0x01)
2027c478bd9Sstevel@tonic-gate 
2039d5056eaSjv227347 /*
2041fa2a664SJoshua M. Clulow  * This macro converts a zc_state_t pointer to the associated subsidiary minor
2051fa2a664SJoshua M. Clulow  * node's dev_t.
2069d5056eaSjv227347  */
2071fa2a664SJoshua M. Clulow #define	ZC_STATE_TO_SUBDEV(x)	(makedevice(ddi_driver_major((x)->zc_devinfo), \
2081fa2a664SJoshua M. Clulow 	(minor_t)(ddi_get_instance((x)->zc_devinfo) << 1 | ZC_SUBSID_MINOR)))
2099d5056eaSjv227347 
2107c478bd9Sstevel@tonic-gate int zcons_debug = 0;
2117c478bd9Sstevel@tonic-gate #define	DBG(a)   if (zcons_debug) cmn_err(CE_NOTE, a)
2127c478bd9Sstevel@tonic-gate #define	DBG1(a, b)   if (zcons_debug) cmn_err(CE_NOTE, a, b)
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Zone Console Pseudo Terminal Module: stream data structure definitions
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate static struct module_info zc_info = {
2197c478bd9Sstevel@tonic-gate 	31337,	/* c0z we r hAx0rs */
2207c478bd9Sstevel@tonic-gate 	"zcons",
2217c478bd9Sstevel@tonic-gate 	0,
2227c478bd9Sstevel@tonic-gate 	INFPSZ,
2232463e920SRichard Lowe 	_TTY_BUFSIZ,
2247c478bd9Sstevel@tonic-gate 	128
2257c478bd9Sstevel@tonic-gate };
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static struct qinit zc_rinit = {
2287c478bd9Sstevel@tonic-gate 	NULL,
2290e2db3e7SToomas Soome 	zc_rsrv,
2307c478bd9Sstevel@tonic-gate 	zc_open,
2317c478bd9Sstevel@tonic-gate 	zc_close,
2327c478bd9Sstevel@tonic-gate 	NULL,
2337c478bd9Sstevel@tonic-gate 	&zc_info,
2347c478bd9Sstevel@tonic-gate 	NULL
2357c478bd9Sstevel@tonic-gate };
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static struct qinit zc_winit = {
2380e2db3e7SToomas Soome 	zc_wput,
2390e2db3e7SToomas Soome 	zc_wsrv,
2407c478bd9Sstevel@tonic-gate 	NULL,
2417c478bd9Sstevel@tonic-gate 	NULL,
2427c478bd9Sstevel@tonic-gate 	NULL,
2437c478bd9Sstevel@tonic-gate 	&zc_info,
2447c478bd9Sstevel@tonic-gate 	NULL
2457c478bd9Sstevel@tonic-gate };
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static struct streamtab zc_tab_info = {
2487c478bd9Sstevel@tonic-gate 	&zc_rinit,
2497c478bd9Sstevel@tonic-gate 	&zc_winit,
2507c478bd9Sstevel@tonic-gate 	NULL,
2517c478bd9Sstevel@tonic-gate 	NULL
2527c478bd9Sstevel@tonic-gate };
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #define	ZC_CONF_FLAG	(D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL)
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_zc_ops) and (struct dev_ops zc_ops)
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(zc_ops, nulldev, nulldev,	zc_attach, zc_detach, nodev, \
26019397407SSherry Moore     zc_getinfo, ZC_CONF_FLAG, &zc_tab_info, ddi_quiesce_not_needed);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
267c6f42f0eSedp 	&mod_driverops,		/* Type of module (this is a pseudo driver) */
268c6f42f0eSedp 	"Zone console driver",	/* description of module */
2697c478bd9Sstevel@tonic-gate 	&zc_ops			/* driver ops */
2707c478bd9Sstevel@tonic-gate };
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2737c478bd9Sstevel@tonic-gate 	MODREV_1,
2747c478bd9Sstevel@tonic-gate 	&modldrv,
2757c478bd9Sstevel@tonic-gate 	NULL
2767c478bd9Sstevel@tonic-gate };
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate typedef struct zc_state {
2797c478bd9Sstevel@tonic-gate 	dev_info_t *zc_devinfo;
2801fa2a664SJoshua M. Clulow 	queue_t *zc_manager_rdq;
2811fa2a664SJoshua M. Clulow 	queue_t *zc_subsid_rdq;
2821fa2a664SJoshua M. Clulow 	vnode_t *zc_subsid_vnode;
2837c478bd9Sstevel@tonic-gate 	int zc_state;
2847c478bd9Sstevel@tonic-gate } zc_state_t;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #define	ZC_STATE_MOPEN	0x01
2877c478bd9Sstevel@tonic-gate #define	ZC_STATE_SOPEN	0x02
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static void *zc_soft_state;
2907c478bd9Sstevel@tonic-gate 
2919d5056eaSjv227347 /*
2921fa2a664SJoshua M. Clulow  * List of STREAMS modules that should be pushed onto every subsidiary instance.
2939d5056eaSjv227347  */
2949d5056eaSjv227347 static char *zcons_mods[] = {
2959d5056eaSjv227347 	"ptem",
2969d5056eaSjv227347 	"ldterm",
2979d5056eaSjv227347 	"ttcompat",
2989d5056eaSjv227347 	NULL
2999d5056eaSjv227347 };
3009d5056eaSjv227347 
3017c478bd9Sstevel@tonic-gate int
_init(void)3027c478bd9Sstevel@tonic-gate _init(void)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	int err;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ((err = ddi_soft_state_init(&zc_soft_state,
3077c478bd9Sstevel@tonic-gate 	    sizeof (zc_state_t), 0)) != 0) {
3087c478bd9Sstevel@tonic-gate 		return (err);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if ((err = mod_install(&modlinkage)) != 0)
3127c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(zc_soft_state);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	return (err);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate int
_fini(void)3197c478bd9Sstevel@tonic-gate _fini(void)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	int err;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0) {
3247c478bd9Sstevel@tonic-gate 		return (err);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&zc_soft_state);
3287c478bd9Sstevel@tonic-gate 	return (0);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3327c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate static int
zc_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3387c478bd9Sstevel@tonic-gate zc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3417c478bd9Sstevel@tonic-gate 	int instance;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
3447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3477c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(zc_soft_state, instance) != DDI_SUCCESS)
3487c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3497c478bd9Sstevel@tonic-gate 
3509d5056eaSjv227347 	/*
3511fa2a664SJoshua M. Clulow 	 * Create the manager and subsidiary minor nodes.
3529d5056eaSjv227347 	 */
3531fa2a664SJoshua M. Clulow 	if ((ddi_create_minor_node(dip, ZCONS_SUBSIDIARY_NAME, S_IFCHR,
3541fa2a664SJoshua M. Clulow 	    instance << 1 | ZC_SUBSID_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE) ||
3551fa2a664SJoshua M. Clulow 	    (ddi_create_minor_node(dip, ZCONS_MANAGER_NAME, S_IFCHR,
3561fa2a664SJoshua M. Clulow 	    instance << 1 | ZC_MANAGER_MINOR, DDI_PSEUDO, 0) == DDI_FAILURE)) {
3576a1b30f3Sjv227347 		ddi_remove_minor_node(dip, NULL);
3586a1b30f3Sjv227347 		ddi_soft_state_free(zc_soft_state, instance);
3596a1b30f3Sjv227347 		return (DDI_FAILURE);
3606a1b30f3Sjv227347 	}
3617c478bd9Sstevel@tonic-gate 
3629d5056eaSjv227347 	VERIFY((zcs = ddi_get_soft_state(zc_soft_state, instance)) != NULL);
3637c478bd9Sstevel@tonic-gate 	zcs->zc_devinfo = dip;
3647c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int
zc_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3687c478bd9Sstevel@tonic-gate zc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
3717c478bd9Sstevel@tonic-gate 	int instance;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
3747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3777c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
3787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) ||
3817c478bd9Sstevel@tonic-gate 	    (zcs->zc_state & ZC_STATE_SOPEN)) {
3827c478bd9Sstevel@tonic-gate 		DBG1("zc_detach: device (dip=%p) still open\n", (void *)dip);
3837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
3877c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(zc_soft_state, instance);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate  * zc_getinfo()
3947c478bd9Sstevel@tonic-gate  *	getinfo(9e) entrypoint.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate static int
zc_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)3977c478bd9Sstevel@tonic-gate zc_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
4007c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE((dev_t)arg);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4037c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4047c478bd9Sstevel@tonic-gate 		if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
4057c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4067c478bd9Sstevel@tonic-gate 		*result = zcs->zc_devinfo;
4077c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4087c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4097c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
4107c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * Return the equivalent queue from the other side of the relationship.
4171fa2a664SJoshua M. Clulow  * e.g.: given the subsidiary's write queue, return the manager's write queue.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate static queue_t *
zc_switch(queue_t * qp)4207c478bd9Sstevel@tonic-gate zc_switch(queue_t *qp)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4237c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4247c478bd9Sstevel@tonic-gate 
4251fa2a664SJoshua M. Clulow 	if (qp == zcs->zc_manager_rdq) {
4261fa2a664SJoshua M. Clulow 		return (zcs->zc_subsid_rdq);
4271fa2a664SJoshua M. Clulow 	} else if (OTHERQ(qp) == zcs->zc_manager_rdq &&
4281fa2a664SJoshua M. Clulow 	    zcs->zc_subsid_rdq != NULL) {
4291fa2a664SJoshua M. Clulow 		return (OTHERQ(zcs->zc_subsid_rdq));
4301fa2a664SJoshua M. Clulow 	} else if (qp == zcs->zc_subsid_rdq) {
4311fa2a664SJoshua M. Clulow 		return (zcs->zc_manager_rdq);
4321fa2a664SJoshua M. Clulow 	} else if (OTHERQ(qp) == zcs->zc_subsid_rdq &&
4331fa2a664SJoshua M. Clulow 	    zcs->zc_manager_rdq != NULL) {
4341fa2a664SJoshua M. Clulow 		return (OTHERQ(zcs->zc_manager_rdq));
4351fa2a664SJoshua M. Clulow 	} else {
4367c478bd9Sstevel@tonic-gate 		return (NULL);
4377c478bd9Sstevel@tonic-gate 	}
4381fa2a664SJoshua M. Clulow }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * For debugging and outputting messages.  Returns the name of the side of
4427c478bd9Sstevel@tonic-gate  * the relationship associated with this queue.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate static const char *
zc_side(queue_t * qp)4457c478bd9Sstevel@tonic-gate zc_side(queue_t *qp)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	zc_state_t *zcs = qp->q_ptr;
4487c478bd9Sstevel@tonic-gate 	ASSERT(zcs != NULL);
4497c478bd9Sstevel@tonic-gate 
4501fa2a664SJoshua M. Clulow 	if (qp == zcs->zc_manager_rdq ||
4511fa2a664SJoshua M. Clulow 	    OTHERQ(qp) == zcs->zc_manager_rdq) {
4521fa2a664SJoshua M. Clulow 		return ("manager");
4537c478bd9Sstevel@tonic-gate 	}
4541fa2a664SJoshua M. Clulow 	ASSERT(qp == zcs->zc_subsid_rdq || OTHERQ(qp) == zcs->zc_subsid_rdq);
4551fa2a664SJoshua M. Clulow 	return ("subsidiary");
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static int
zc_manager_open(zc_state_t * zcs,queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)4591fa2a664SJoshua M. Clulow zc_manager_open(zc_state_t *zcs,
4607c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
4617c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
4627c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
4637c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
4647c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	mblk_t *mop;
4677c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4701fa2a664SJoshua M. Clulow 	 * Enforce exclusivity on the manager side; the only consumer should
4717c478bd9Sstevel@tonic-gate 	 * be the zoneadmd for the zone.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_MOPEN) != 0)
4747c478bd9Sstevel@tonic-gate 		return (EBUSY);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
4771fa2a664SJoshua M. Clulow 		DBG("zc_manager_open(): mop allocation failed\n");
4787c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_MOPEN;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	/*
4847c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
4857c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
4887c478bd9Sstevel@tonic-gate 	qprocson(rqp);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/*
4911fa2a664SJoshua M. Clulow 	 * Following qprocson(), the manager side is fully plumbed into the
4921fa2a664SJoshua M. Clulow 	 * STREAM and may send/receive messages.  Setting zcs->zc_manager_rdq
4931fa2a664SJoshua M. Clulow 	 * will allow the subsidiary to send messages to us (the manager).
4941fa2a664SJoshua M. Clulow 	 * This cannot occur before qprocson() because the manager is not
4957c478bd9Sstevel@tonic-gate 	 * ready to process them until that point.
4967c478bd9Sstevel@tonic-gate 	 */
4971fa2a664SJoshua M. Clulow 	zcs->zc_manager_rdq = rqp;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
5017c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
5047c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
50527e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
5067c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
5077c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
5087c478bd9Sstevel@tonic-gate 	else
5097c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
5102463e920SRichard Lowe 	sop->so_hiwat = _TTY_BUFSIZ;
5117c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
5127c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	return (0);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate static int
zc_subsidiary_open(zc_state_t * zcs,queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)5181fa2a664SJoshua M. Clulow zc_subsidiary_open(zc_state_t *zcs,
5197c478bd9Sstevel@tonic-gate     queue_t	*rqp,	/* pointer to the read side queue */
5207c478bd9Sstevel@tonic-gate     dev_t	*devp,	/* pointer to stream tail's dev */
5217c478bd9Sstevel@tonic-gate     int		oflag,	/* the user open(2) supplied flags */
5227c478bd9Sstevel@tonic-gate     int		sflag,	/* open state flag */
5237c478bd9Sstevel@tonic-gate     cred_t	*credp)	/* credentials */
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	mblk_t *mop;
5267c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
5276a1b30f3Sjv227347 	major_t major;
5286a1b30f3Sjv227347 	minor_t minor;
5296a1b30f3Sjv227347 	minor_t lastminor;
5306a1b30f3Sjv227347 	uint_t anchorindex;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5331fa2a664SJoshua M. Clulow 	 * The subsidiary side can be opened as many times as needed.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if ((zcs->zc_state & ZC_STATE_SOPEN) != 0) {
5367c478bd9Sstevel@tonic-gate 		ASSERT((rqp != NULL) && (WR(rqp)->q_ptr == zcs));
5377c478bd9Sstevel@tonic-gate 		return (0);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5406a1b30f3Sjv227347 	/*
541*bbf21555SRichard Lowe 	 * Set up sad(4D) so that the necessary STREAMS modules will be in
5426a1b30f3Sjv227347 	 * place.  A wrinkle is that 'ptem' must be anchored
543*bbf21555SRichard Lowe 	 * in place (see streamio(4I)) because we always want the console to
5446a1b30f3Sjv227347 	 * have terminal semantics.
5456a1b30f3Sjv227347 	 */
5461fa2a664SJoshua M. Clulow 	minor = ddi_get_instance(zcs->zc_devinfo) << 1 | ZC_SUBSID_MINOR;
5476a1b30f3Sjv227347 	major = ddi_driver_major(zcs->zc_devinfo);
5486a1b30f3Sjv227347 	lastminor = 0;
5496a1b30f3Sjv227347 	anchorindex = 1;
5506a1b30f3Sjv227347 	if (kstr_autopush(SET_AUTOPUSH, &major, &minor, &lastminor,
5516a1b30f3Sjv227347 	    &anchorindex, zcons_mods) != 0) {
5521fa2a664SJoshua M. Clulow 		DBG("zc_subsidiary_open(): kstr_autopush() failed\n");
5536a1b30f3Sjv227347 		return (EIO);
5546a1b30f3Sjv227347 	}
5556a1b30f3Sjv227347 
5567c478bd9Sstevel@tonic-gate 	if ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
5571fa2a664SJoshua M. Clulow 		DBG("zc_subsidiary_open(): mop allocation failed\n");
5587c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	zcs->zc_state |= ZC_STATE_SOPEN;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/*
5647c478bd9Sstevel@tonic-gate 	 * q_ptr stores driver private data; stash the soft state data on both
5657c478bd9Sstevel@tonic-gate 	 * read and write sides of the queue.
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = zcs;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	qprocson(rqp);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Must follow qprocson(), since we aren't ready to process until then.
5737c478bd9Sstevel@tonic-gate 	 */
5741fa2a664SJoshua M. Clulow 	zcs->zc_subsid_rdq = rqp;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue and add
5787c478bd9Sstevel@tonic-gate 	 * controlling tty as needed.
5797c478bd9Sstevel@tonic-gate 	 */
5807c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
5817c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
58227e6fb21Sdp 	sop = (struct stroptions *)(void *)mop->b_rptr;
5837c478bd9Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
5842463e920SRichard Lowe 	sop->so_hiwat = _TTY_BUFSIZ;
5857c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
5867c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	return (0);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * open(9e) entrypoint; checks sflag, and rejects anything unordinary.
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate static int
zc_open(queue_t * rqp,dev_t * devp,int oflag,int sflag,cred_t * credp)5957c478bd9Sstevel@tonic-gate zc_open(queue_t *rqp,	/* pointer to the read side queue */
5967c478bd9Sstevel@tonic-gate     dev_t   *devp,	/* pointer to stream tail's dev */
5977c478bd9Sstevel@tonic-gate     int	oflag,		/* the user open(2) supplied flags */
5987c478bd9Sstevel@tonic-gate     int	sflag,		/* open state flag */
5997c478bd9Sstevel@tonic-gate     cred_t  *credp)	/* credentials */
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	int instance = ZC_INSTANCE(*devp);
6027c478bd9Sstevel@tonic-gate 	int ret;
6037c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (sflag != 0)
6067c478bd9Sstevel@tonic-gate 		return (EINVAL);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if ((zcs = ddi_get_soft_state(zc_soft_state, instance)) == NULL)
6097c478bd9Sstevel@tonic-gate 		return (ENXIO);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	switch (ZC_NODE(*devp)) {
6121fa2a664SJoshua M. Clulow 	case ZC_MANAGER_MINOR:
6131fa2a664SJoshua M. Clulow 		ret = zc_manager_open(zcs, rqp, devp, oflag, sflag, credp);
6147c478bd9Sstevel@tonic-gate 		break;
6151fa2a664SJoshua M. Clulow 	case ZC_SUBSID_MINOR:
6161fa2a664SJoshua M. Clulow 		ret = zc_subsidiary_open(zcs, rqp, devp, oflag, sflag, credp);
6177c478bd9Sstevel@tonic-gate 		break;
6187c478bd9Sstevel@tonic-gate 	default:
6197c478bd9Sstevel@tonic-gate 		ret = ENXIO;
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (ret);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * close(9e) entrypoint.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate static int
zc_close(queue_t * rqp,int flag,cred_t * credp)6307c478bd9Sstevel@tonic-gate zc_close(queue_t *rqp, int flag, cred_t *credp)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	queue_t *wqp;
6337c478bd9Sstevel@tonic-gate 	mblk_t	*bp;
6347c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
6356a1b30f3Sjv227347 	major_t major;
6366a1b30f3Sjv227347 	minor_t minor;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)rqp->q_ptr;
6397c478bd9Sstevel@tonic-gate 
6401fa2a664SJoshua M. Clulow 	if (rqp == zcs->zc_manager_rdq) {
6411fa2a664SJoshua M. Clulow 		DBG("Closing manager side");
6427c478bd9Sstevel@tonic-gate 
6431fa2a664SJoshua M. Clulow 		zcs->zc_manager_rdq = NULL;
6447c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_MOPEN;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		/*
6471fa2a664SJoshua M. Clulow 		 * qenable subsidiary side write queue so that it can flush
6481fa2a664SJoshua M. Clulow 		 * its messages as manager's read queue is going away
6497c478bd9Sstevel@tonic-gate 		 */
6501fa2a664SJoshua M. Clulow 		if (zcs->zc_subsid_rdq != NULL) {
6511fa2a664SJoshua M. Clulow 			qenable(WR(zcs->zc_subsid_rdq));
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
6557c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
6567c478bd9Sstevel@tonic-gate 
6571fa2a664SJoshua M. Clulow 	} else if (rqp == zcs->zc_subsid_rdq) {
6587c478bd9Sstevel@tonic-gate 
6591fa2a664SJoshua M. Clulow 		DBG("Closing subsidiary side");
6607c478bd9Sstevel@tonic-gate 		zcs->zc_state &= ~ZC_STATE_SOPEN;
6611fa2a664SJoshua M. Clulow 		zcs->zc_subsid_rdq = NULL;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		wqp = WR(rqp);
6647c478bd9Sstevel@tonic-gate 		while ((bp = getq(wqp)) != NULL) {
6651fa2a664SJoshua M. Clulow 			if (zcs->zc_manager_rdq != NULL)
6661fa2a664SJoshua M. Clulow 				putnext(zcs->zc_manager_rdq, bp);
6677c478bd9Sstevel@tonic-gate 			else if (bp->b_datap->db_type == M_IOCTL)
6687c478bd9Sstevel@tonic-gate 				miocnak(wqp, bp, 0, 0);
6697c478bd9Sstevel@tonic-gate 			else
6707c478bd9Sstevel@tonic-gate 				freemsg(bp);
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		/*
6741fa2a664SJoshua M. Clulow 		 * Qenable manager side write queue so that it can flush its
6751fa2a664SJoshua M. Clulow 		 * messages as subsidiarys's read queue is going away.
6767c478bd9Sstevel@tonic-gate 		 */
6771fa2a664SJoshua M. Clulow 		if (zcs->zc_manager_rdq != NULL)
6781fa2a664SJoshua M. Clulow 			qenable(WR(zcs->zc_manager_rdq));
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		qprocsoff(rqp);
6817c478bd9Sstevel@tonic-gate 		WR(rqp)->q_ptr = rqp->q_ptr = NULL;
6826a1b30f3Sjv227347 
6836a1b30f3Sjv227347 		/*
6846a1b30f3Sjv227347 		 * Clear the sad configuration so that reopening doesn't fail
6856a1b30f3Sjv227347 		 * to set up sad configuration.
6866a1b30f3Sjv227347 		 */
6876a1b30f3Sjv227347 		major = ddi_driver_major(zcs->zc_devinfo);
6881fa2a664SJoshua M. Clulow 		minor = ddi_get_instance(zcs->zc_devinfo) << 1 |
6891fa2a664SJoshua M. Clulow 		    ZC_SUBSID_MINOR;
6906a1b30f3Sjv227347 		(void) kstr_autopush(CLR_AUTOPUSH, &major, &minor, NULL, NULL,
6916a1b30f3Sjv227347 		    NULL);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	return (0);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static void
handle_mflush(queue_t * qp,mblk_t * mp)6987c478bd9Sstevel@tonic-gate handle_mflush(queue_t *qp, mblk_t *mp)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
7017c478bd9Sstevel@tonic-gate 	DBG1("M_FLUSH on %s side", zc_side(qp));
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHW) {
7047c478bd9Sstevel@tonic-gate 		DBG1("M_FLUSH, FLUSHW, %s side", zc_side(qp));
7057c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHDATA);
7067c478bd9Sstevel@tonic-gate 		*mp->b_rptr &= ~FLUSHW;
7077c478bd9Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHR) == 0) {
7087c478bd9Sstevel@tonic-gate 			/*
7097c478bd9Sstevel@tonic-gate 			 * FLUSHW only. Change to FLUSHR and putnext other side,
7107c478bd9Sstevel@tonic-gate 			 * then we are done.
7117c478bd9Sstevel@tonic-gate 			 */
7127c478bd9Sstevel@tonic-gate 			*mp->b_rptr |= FLUSHR;
7137c478bd9Sstevel@tonic-gate 			if (zc_switch(RD(qp)) != NULL) {
7147c478bd9Sstevel@tonic-gate 				putnext(zc_switch(RD(qp)), mp);
7157c478bd9Sstevel@tonic-gate 				return;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 		} else if ((zc_switch(RD(qp)) != NULL) &&
7187c478bd9Sstevel@tonic-gate 		    (nmp = copyb(mp)) != NULL) {
7197c478bd9Sstevel@tonic-gate 			/*
7207c478bd9Sstevel@tonic-gate 			 * It is a FLUSHRW; we copy the mblk and send
7217c478bd9Sstevel@tonic-gate 			 * it to the other side, since we still need to use
7227c478bd9Sstevel@tonic-gate 			 * the mblk in FLUSHR processing, below.
7237c478bd9Sstevel@tonic-gate 			 */
7247c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), nmp);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (*mp->b_rptr & FLUSHR) {
7297c478bd9Sstevel@tonic-gate 		DBG("qreply(qp) turning FLUSHR around\n");
7307c478bd9Sstevel@tonic-gate 		qreply(qp, mp);
7317c478bd9Sstevel@tonic-gate 		return;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	freemsg(mp);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*
7371fa2a664SJoshua M. Clulow  * wput(9E) is symmetric for manager and subsidiary sides, so this handles both
7389d5056eaSjv227347  * without splitting the codepath.  (The only exception to this is the
7391fa2a664SJoshua M. Clulow  * processing of zcons ioctls, which is restricted to the manager side.)
7407c478bd9Sstevel@tonic-gate  *
7417c478bd9Sstevel@tonic-gate  * zc_wput() looks at the other side; if there is no process holding that
7427c478bd9Sstevel@tonic-gate  * side open, it frees the message.  This prevents processes from hanging
7437c478bd9Sstevel@tonic-gate  * if no one is holding open the console.  Otherwise, it putnext's high
7447c478bd9Sstevel@tonic-gate  * priority messages, putnext's normal messages if possible, and otherwise
7457c478bd9Sstevel@tonic-gate  * enqueues the messages; in the case that something is enqueued, wsrv(9E)
7467c478bd9Sstevel@tonic-gate  * will take care of eventually shuttling I/O to the other side.
7477c478bd9Sstevel@tonic-gate  */
7480e2db3e7SToomas Soome static int
zc_wput(queue_t * qp,mblk_t * mp)7497c478bd9Sstevel@tonic-gate zc_wput(queue_t *qp, mblk_t *mp)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	unsigned char type = mp->b_datap->db_type;
7529d5056eaSjv227347 	zc_state_t *zcs;
7539d5056eaSjv227347 	struct iocblk *iocbp;
7541fa2a664SJoshua M. Clulow 	file_t *subsidiary_filep;
7551fa2a664SJoshua M. Clulow 	struct snode *subsidiary_snodep;
7561fa2a664SJoshua M. Clulow 	int subsidiary_fd;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	DBG1("entering zc_wput, %s side", zc_side(qp));
7617c478bd9Sstevel@tonic-gate 
7629d5056eaSjv227347 	/*
7631fa2a664SJoshua M. Clulow 	 * Process zcons ioctl messages if qp is the manager console's write
7649d5056eaSjv227347 	 * queue.
7659d5056eaSjv227347 	 */
7669d5056eaSjv227347 	zcs = (zc_state_t *)qp->q_ptr;
7671fa2a664SJoshua M. Clulow 	if (zcs->zc_manager_rdq != NULL && qp == WR(zcs->zc_manager_rdq) &&
7689d5056eaSjv227347 	    type == M_IOCTL) {
7699d5056eaSjv227347 		iocbp = (struct iocblk *)(void *)mp->b_rptr;
7709d5056eaSjv227347 		switch (iocbp->ioc_cmd) {
7711fa2a664SJoshua M. Clulow 		case ZC_HOLDSUBSID:
7729d5056eaSjv227347 			/*
7731fa2a664SJoshua M. Clulow 			 * Hold the subsidiary's vnode and increment the
7741fa2a664SJoshua M. Clulow 			 * refcount of the snode.  If the vnode is already
7751fa2a664SJoshua M. Clulow 			 * held, then indicate success.
7769d5056eaSjv227347 			 */
7779d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
7789d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
7790e2db3e7SToomas Soome 				return (0);
7809d5056eaSjv227347 			}
7811fa2a664SJoshua M. Clulow 			if (zcs->zc_subsid_vnode != NULL) {
7829d5056eaSjv227347 				miocack(qp, mp, 0, 0);
7830e2db3e7SToomas Soome 				return (0);
7849d5056eaSjv227347 			}
7859d5056eaSjv227347 
7869d5056eaSjv227347 			/*
7876a1b30f3Sjv227347 			 * The process that passed the ioctl must be running in
7886a1b30f3Sjv227347 			 * the global zone.
7896a1b30f3Sjv227347 			 */
7906a1b30f3Sjv227347 			if (curzone != global_zone) {
7916a1b30f3Sjv227347 				miocack(qp, mp, 0, EINVAL);
7920e2db3e7SToomas Soome 				return (0);
7936a1b30f3Sjv227347 			}
7946a1b30f3Sjv227347 
7956a1b30f3Sjv227347 			/*
7969d5056eaSjv227347 			 * The calling process must pass a file descriptor for
7971fa2a664SJoshua M. Clulow 			 * the subsidiary device.
7989d5056eaSjv227347 			 */
7991fa2a664SJoshua M. Clulow 			subsidiary_fd =
8009d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
8019d5056eaSjv227347 			    b_rptr;
8021fa2a664SJoshua M. Clulow 			subsidiary_filep = getf(subsidiary_fd);
8031fa2a664SJoshua M. Clulow 			if (subsidiary_filep == NULL) {
8049d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8050e2db3e7SToomas Soome 				return (0);
8069d5056eaSjv227347 			}
8071fa2a664SJoshua M. Clulow 			if (ZC_STATE_TO_SUBDEV(zcs) !=
8081fa2a664SJoshua M. Clulow 			    subsidiary_filep->f_vnode->v_rdev) {
8091fa2a664SJoshua M. Clulow 				releasef(subsidiary_fd);
8109d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8110e2db3e7SToomas Soome 				return (0);
8129d5056eaSjv227347 			}
8139d5056eaSjv227347 
8149d5056eaSjv227347 			/*
8151fa2a664SJoshua M. Clulow 			 * Get a reference to the subsidiary's vnode.  Also
8161fa2a664SJoshua M. Clulow 			 * bump the reference count on the associated snode.
8179d5056eaSjv227347 			 */
8181fa2a664SJoshua M. Clulow 			ASSERT(vn_matchops(subsidiary_filep->f_vnode,
8199d5056eaSjv227347 			    spec_getvnodeops()));
8201fa2a664SJoshua M. Clulow 			zcs->zc_subsid_vnode = subsidiary_filep->f_vnode;
8211fa2a664SJoshua M. Clulow 			VN_HOLD(zcs->zc_subsid_vnode);
8221fa2a664SJoshua M. Clulow 			subsidiary_snodep = VTOCS(zcs->zc_subsid_vnode);
8231fa2a664SJoshua M. Clulow 			mutex_enter(&subsidiary_snodep->s_lock);
8241fa2a664SJoshua M. Clulow 			++subsidiary_snodep->s_count;
8251fa2a664SJoshua M. Clulow 			mutex_exit(&subsidiary_snodep->s_lock);
8261fa2a664SJoshua M. Clulow 			releasef(subsidiary_fd);
8279d5056eaSjv227347 			miocack(qp, mp, 0, 0);
8280e2db3e7SToomas Soome 			return (0);
8291fa2a664SJoshua M. Clulow 		case ZC_RELEASESUBSID:
8309d5056eaSjv227347 			/*
8311fa2a664SJoshua M. Clulow 			 * Release the manager's handle on the subsidiary's
8321fa2a664SJoshua M. Clulow 			 * vnode.  If there isn't a handle for the vnode, then
8331fa2a664SJoshua M. Clulow 			 * indicate success.
8349d5056eaSjv227347 			 */
8359d5056eaSjv227347 			if (iocbp->ioc_count != TRANSPARENT) {
8369d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8370e2db3e7SToomas Soome 				return (0);
8389d5056eaSjv227347 			}
8391fa2a664SJoshua M. Clulow 			if (zcs->zc_subsid_vnode == NULL) {
8409d5056eaSjv227347 				miocack(qp, mp, 0, 0);
8410e2db3e7SToomas Soome 				return (0);
8429d5056eaSjv227347 			}
8439d5056eaSjv227347 
8449d5056eaSjv227347 			/*
8456a1b30f3Sjv227347 			 * The process that passed the ioctl must be running in
8466a1b30f3Sjv227347 			 * the global zone.
8476a1b30f3Sjv227347 			 */
8486a1b30f3Sjv227347 			if (curzone != global_zone) {
8496a1b30f3Sjv227347 				miocack(qp, mp, 0, EINVAL);
8500e2db3e7SToomas Soome 				return (0);
8516a1b30f3Sjv227347 			}
8526a1b30f3Sjv227347 
8536a1b30f3Sjv227347 			/*
8549d5056eaSjv227347 			 * The process that passed the ioctl must have provided
8551fa2a664SJoshua M. Clulow 			 * a file descriptor for the subsidiary device.  Make
8561fa2a664SJoshua M. Clulow 			 * sure this is correct.
8579d5056eaSjv227347 			 */
8581fa2a664SJoshua M. Clulow 			subsidiary_fd =
8599d5056eaSjv227347 			    (int)(intptr_t)*(caddr_t *)(void *)mp->b_cont->
8609d5056eaSjv227347 			    b_rptr;
8611fa2a664SJoshua M. Clulow 			subsidiary_filep = getf(subsidiary_fd);
8621fa2a664SJoshua M. Clulow 			if (subsidiary_filep == NULL) {
8639d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8640e2db3e7SToomas Soome 				return (0);
8659d5056eaSjv227347 			}
8661fa2a664SJoshua M. Clulow 			if (zcs->zc_subsid_vnode->v_rdev !=
8671fa2a664SJoshua M. Clulow 			    subsidiary_filep->f_vnode->v_rdev) {
8681fa2a664SJoshua M. Clulow 				releasef(subsidiary_fd);
8699d5056eaSjv227347 				miocack(qp, mp, 0, EINVAL);
8700e2db3e7SToomas Soome 				return (0);
8719d5056eaSjv227347 			}
8729d5056eaSjv227347 
8739d5056eaSjv227347 			/*
8749d5056eaSjv227347 			 * Decrement the snode's reference count and release the
8759d5056eaSjv227347 			 * vnode.
8769d5056eaSjv227347 			 */
8771fa2a664SJoshua M. Clulow 			ASSERT(vn_matchops(subsidiary_filep->f_vnode,
8789d5056eaSjv227347 			    spec_getvnodeops()));
8791fa2a664SJoshua M. Clulow 			subsidiary_snodep = VTOCS(zcs->zc_subsid_vnode);
8801fa2a664SJoshua M. Clulow 			mutex_enter(&subsidiary_snodep->s_lock);
8811fa2a664SJoshua M. Clulow 			--subsidiary_snodep->s_count;
8821fa2a664SJoshua M. Clulow 			mutex_exit(&subsidiary_snodep->s_lock);
8831fa2a664SJoshua M. Clulow 			VN_RELE(zcs->zc_subsid_vnode);
8841fa2a664SJoshua M. Clulow 			zcs->zc_subsid_vnode = NULL;
8851fa2a664SJoshua M. Clulow 			releasef(subsidiary_fd);
8869d5056eaSjv227347 			miocack(qp, mp, 0, 0);
8870e2db3e7SToomas Soome 			return (0);
8889d5056eaSjv227347 		default:
8899d5056eaSjv227347 			break;
8909d5056eaSjv227347 		}
8919d5056eaSjv227347 	}
8929d5056eaSjv227347 
8937c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
8947c478bd9Sstevel@tonic-gate 		DBG1("wput to %s side (no one listening)", zc_side(qp));
8957c478bd9Sstevel@tonic-gate 		switch (type) {
8967c478bd9Sstevel@tonic-gate 		case M_FLUSH:
8977c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
8987c478bd9Sstevel@tonic-gate 			break;
8997c478bd9Sstevel@tonic-gate 		case M_IOCTL:
9007c478bd9Sstevel@tonic-gate 			miocnak(qp, mp, 0, 0);
9017c478bd9Sstevel@tonic-gate 			break;
9027c478bd9Sstevel@tonic-gate 		default:
9037c478bd9Sstevel@tonic-gate 			freemsg(mp);
9047c478bd9Sstevel@tonic-gate 			break;
9057c478bd9Sstevel@tonic-gate 		}
9060e2db3e7SToomas Soome 		return (0);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (type >= QPCTL) {
9107c478bd9Sstevel@tonic-gate 		DBG1("(hipri) wput, %s side", zc_side(qp));
9117c478bd9Sstevel@tonic-gate 		switch (type) {
9127c478bd9Sstevel@tonic-gate 		case M_READ:		/* supposedly from ldterm? */
9137c478bd9Sstevel@tonic-gate 			DBG("zc_wput: tossing M_READ\n");
9147c478bd9Sstevel@tonic-gate 			freemsg(mp);
9157c478bd9Sstevel@tonic-gate 			break;
9167c478bd9Sstevel@tonic-gate 		case M_FLUSH:
9177c478bd9Sstevel@tonic-gate 			handle_mflush(qp, mp);
9187c478bd9Sstevel@tonic-gate 			break;
9197c478bd9Sstevel@tonic-gate 		default:
9207c478bd9Sstevel@tonic-gate 			/*
9217c478bd9Sstevel@tonic-gate 			 * Put this to the other side.
9227c478bd9Sstevel@tonic-gate 			 */
9237c478bd9Sstevel@tonic-gate 			ASSERT(zc_switch(RD(qp)) != NULL);
9247c478bd9Sstevel@tonic-gate 			putnext(zc_switch(RD(qp)), mp);
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		DBG1("done (hipri) wput, %s side", zc_side(qp));
9280e2db3e7SToomas Soome 		return (0);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * Only putnext if there isn't already something in the queue.
9337c478bd9Sstevel@tonic-gate 	 * otherwise things would wind up out of order.
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	if (qp->q_first == NULL && bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
9367c478bd9Sstevel@tonic-gate 		DBG("wput: putting message to other side\n");
9377c478bd9Sstevel@tonic-gate 		putnext(RD(zc_switch(qp)), mp);
9387c478bd9Sstevel@tonic-gate 	} else {
9397c478bd9Sstevel@tonic-gate 		DBG("wput: putting msg onto queue\n");
9407c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	DBG1("done wput, %s side", zc_side(qp));
9430e2db3e7SToomas Soome 	return (0);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9471fa2a664SJoshua M. Clulow  * rsrv(9E) is symmetric for manager and subsidiary, so zc_rsrv() handles both
9487c478bd9Sstevel@tonic-gate  * without splitting up the codepath.
9497c478bd9Sstevel@tonic-gate  *
9507c478bd9Sstevel@tonic-gate  * Enable the write side of the partner.  This triggers the partner to send
9517c478bd9Sstevel@tonic-gate  * messages queued on its write side to this queue's read side.
9527c478bd9Sstevel@tonic-gate  */
9530e2db3e7SToomas Soome static int
zc_rsrv(queue_t * qp)9547c478bd9Sstevel@tonic-gate zc_rsrv(queue_t *qp)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	zc_state_t *zcs;
9577c478bd9Sstevel@tonic-gate 	zcs = (zc_state_t *)qp->q_ptr;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
9601fa2a664SJoshua M. Clulow 	 * Care must be taken here, as either of the manager or subsidiary side
9617c478bd9Sstevel@tonic-gate 	 * qptr could be NULL.
9627c478bd9Sstevel@tonic-gate 	 */
9631fa2a664SJoshua M. Clulow 	ASSERT(qp == zcs->zc_manager_rdq || qp == zcs->zc_subsid_rdq);
9647c478bd9Sstevel@tonic-gate 	if (zc_switch(qp) == NULL) {
9657c478bd9Sstevel@tonic-gate 		DBG("zc_rsrv: other side isn't listening\n");
9660e2db3e7SToomas Soome 		return (0);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 	qenable(WR(zc_switch(qp)));
9690e2db3e7SToomas Soome 	return (0);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9731fa2a664SJoshua M. Clulow  * This routine is symmetric for manager and subsidiary, so it handles both
9741fa2a664SJoshua M. Clulow  * without splitting up the codepath.
9757c478bd9Sstevel@tonic-gate  *
9767c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to the other, send
9777c478bd9Sstevel@tonic-gate  * them via putnext(). Else, if queued messages cannot be sent, leave them
9787c478bd9Sstevel@tonic-gate  * on this queue.
9797c478bd9Sstevel@tonic-gate  */
9800e2db3e7SToomas Soome static int
zc_wsrv(queue_t * qp)9817c478bd9Sstevel@tonic-gate zc_wsrv(queue_t *qp)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	mblk_t *mp;
9847c478bd9Sstevel@tonic-gate 
9851fa2a664SJoshua M. Clulow 	DBG1("zc_wsrv manager (%s) side", zc_side(qp));
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	/*
9887c478bd9Sstevel@tonic-gate 	 * Partner has no read queue, so take the data, and throw it away.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	if (zc_switch(RD(qp)) == NULL) {
9917c478bd9Sstevel@tonic-gate 		DBG("zc_wsrv: other side isn't listening");
9927c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
9937c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
9947c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, 0);
9957c478bd9Sstevel@tonic-gate 			else
9967c478bd9Sstevel@tonic-gate 				freemsg(mp);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
9990e2db3e7SToomas Soome 		return (0);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/*
10037c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
10047c478bd9Sstevel@tonic-gate 	 */
10057c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
10067c478bd9Sstevel@tonic-gate 		/*
10077c478bd9Sstevel@tonic-gate 		 * Due to the way zc_wput is implemented, we should never
10087c478bd9Sstevel@tonic-gate 		 * see a control message here.
10097c478bd9Sstevel@tonic-gate 		 */
10107c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_type < QPCTL);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		if (bcanputnext(RD(zc_switch(qp)), mp->b_band)) {
10137c478bd9Sstevel@tonic-gate 			DBG("wsrv: send message to other side\n");
10147c478bd9Sstevel@tonic-gate 			putnext(RD(zc_switch(qp)), mp);
10157c478bd9Sstevel@tonic-gate 		} else {
10167c478bd9Sstevel@tonic-gate 			DBG("wsrv: putting msg back on queue\n");
10177c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
10187c478bd9Sstevel@tonic-gate 			break;
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 	}
10210e2db3e7SToomas Soome 	return (0);
10227c478bd9Sstevel@tonic-gate }
1023