xref: /titanic_50/usr/src/uts/common/io/ptm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * Pseudo Terminal Master Driver.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * The pseudo-tty subsystem simulates a terminal connection, where the master
36*7c478bd9Sstevel@tonic-gate  * side represents the terminal and the slave represents the user process's
37*7c478bd9Sstevel@tonic-gate  * special device end point. The master device is set up as a cloned device
38*7c478bd9Sstevel@tonic-gate  * where its major device number is the major for the clone device and its minor
39*7c478bd9Sstevel@tonic-gate  * device number is the major for the ptm driver. There are no nodes in the file
40*7c478bd9Sstevel@tonic-gate  * system for master devices. The master pseudo driver is opened using the
41*7c478bd9Sstevel@tonic-gate  * open(2) system call with /dev/ptmx as the device parameter.  The clone open
42*7c478bd9Sstevel@tonic-gate  * finds the next available minor device for the ptm major device.
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  * A master device is available only if it and its corresponding slave device
45*7c478bd9Sstevel@tonic-gate  * are not already open. When the master device is opened, the corresponding
46*7c478bd9Sstevel@tonic-gate  * slave device is automatically locked out. Only one open is allowed on a
47*7c478bd9Sstevel@tonic-gate  * master device.  Multiple opens are allowed on the slave device.  After both
48*7c478bd9Sstevel@tonic-gate  * the master and slave have been opened, the user has two file descriptors
49*7c478bd9Sstevel@tonic-gate  * which are the end points of a full duplex connection composed of two streams
50*7c478bd9Sstevel@tonic-gate  * which are automatically connected at the master and slave drivers. The user
51*7c478bd9Sstevel@tonic-gate  * may then push modules onto either side of the stream pair.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * The master and slave drivers pass all messages to their adjacent queues.
54*7c478bd9Sstevel@tonic-gate  * Only the M_FLUSH needs some processing.  Because the read queue of one side
55*7c478bd9Sstevel@tonic-gate  * is connected to the write queue of the other, the FLUSHR flag is changed to
56*7c478bd9Sstevel@tonic-gate  * the FLUSHW flag and vice versa. When the master device is closed an M_HANGUP
57*7c478bd9Sstevel@tonic-gate  * message is sent to the slave device which will render the device
58*7c478bd9Sstevel@tonic-gate  * unusable. The process on the slave side gets the EIO when attempting to write
59*7c478bd9Sstevel@tonic-gate  * on that stream but it will be able to read any data remaining on the stream
60*7c478bd9Sstevel@tonic-gate  * head read queue.  When all the data has been read, read() returns 0
61*7c478bd9Sstevel@tonic-gate  * indicating that the stream can no longer be used.  On the last close of the
62*7c478bd9Sstevel@tonic-gate  * slave device, a 0-length message is sent to the master device. When the
63*7c478bd9Sstevel@tonic-gate  * application on the master side issues a read() or getmsg() and 0 is returned,
64*7c478bd9Sstevel@tonic-gate  * the user of the master device decides whether to issue a close() that
65*7c478bd9Sstevel@tonic-gate  * dismantles the pseudo-terminal subsystem. If the master device is not closed,
66*7c478bd9Sstevel@tonic-gate  * the pseudo-tty subsystem will be available to another user to open the slave
67*7c478bd9Sstevel@tonic-gate  * device.
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * If O_NONBLOCK or O_NDELAY is set, read on the master side returns -1 with
70*7c478bd9Sstevel@tonic-gate  * errno set to EAGAIN if no data is available, and write returns -1 with errno
71*7c478bd9Sstevel@tonic-gate  * set to EAGAIN if there is internal flow control.
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  * IOCTLS:
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  *  ISPTM: determines whether the file descriptor is that of an open master
76*7c478bd9Sstevel@tonic-gate  *	   device. Return code of zero indicates that the file descriptor
77*7c478bd9Sstevel@tonic-gate  *	   represents master device.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  *  UNLKPT: unlocks the master and slave devices.  It returns 0 on success. On
80*7c478bd9Sstevel@tonic-gate  *	    failure, the errno is set to EINVAL indicating that the master
81*7c478bd9Sstevel@tonic-gate  *	    device is not open.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  *  ZONEPT: sets the zone membership of ths associated pts device.
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  * Synchronization:
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  *   All global data synchronization between ptm/pts is done via global
88*7c478bd9Sstevel@tonic-gate  *   ptms_lock mutex which is initialized at system boot time from
89*7c478bd9Sstevel@tonic-gate  *   ptms_initspace (called from space.c).
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  *   Individual fields of pt_ttys structure (except ptm_rdq, pts_rdq and
92*7c478bd9Sstevel@tonic-gate  *   pt_nullmsg) are protected by pt_ttys.pt_lock mutex.
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_ENTER_WRITE are reference counter based read-write locks
95*7c478bd9Sstevel@tonic-gate  *   which allow reader locks to be reacquired by the same thread (usual
96*7c478bd9Sstevel@tonic-gate  *   reader/writer locks can't be used for that purpose since it is illegal for
97*7c478bd9Sstevel@tonic-gate  *   a thread to acquire a lock it already holds, even as a reader). The sole
98*7c478bd9Sstevel@tonic-gate  *   purpose of these macros is to guarantee that the peer queue will not
99*7c478bd9Sstevel@tonic-gate  *   disappear (due to closing peer) while it is used. It is safe to use
100*7c478bd9Sstevel@tonic-gate  *   PT_ENTER_READ/PT_EXIT_READ brackets across calls like putq/putnext (since
101*7c478bd9Sstevel@tonic-gate  *   they are not real locks but reference counts).
102*7c478bd9Sstevel@tonic-gate  *
103*7c478bd9Sstevel@tonic-gate  *   PT_ENTER_WRITE/PT_EXIT_WRITE brackets are used ONLY in master/slave
104*7c478bd9Sstevel@tonic-gate  *   open/close paths to modify ptm_rdq and pts_rdq fields. These fields should
105*7c478bd9Sstevel@tonic-gate  *   be set to appropriate queues *after* qprocson() is called during open (to
106*7c478bd9Sstevel@tonic-gate  *   prevent peer from accessing the queue with incomplete plumbing) and set to
107*7c478bd9Sstevel@tonic-gate  *   NULL before qprocsoff() is called during close.
108*7c478bd9Sstevel@tonic-gate  *
109*7c478bd9Sstevel@tonic-gate  *   The pt_nullmsg field is only used in open/close routines and it is also
110*7c478bd9Sstevel@tonic-gate  *   protected by PT_ENTER_WRITE/PT_EXIT_WRITE brackets to avoid extra mutex
111*7c478bd9Sstevel@tonic-gate  *   holds.
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  * Lock Ordering:
114*7c478bd9Sstevel@tonic-gate  *
115*7c478bd9Sstevel@tonic-gate  *   If both ptms_lock and per-pty lock should be held, ptms_lock should always
116*7c478bd9Sstevel@tonic-gate  *   be entered first, followed by per-pty lock.
117*7c478bd9Sstevel@tonic-gate  *
118*7c478bd9Sstevel@tonic-gate  * See ptms.h, pts.c and ptms_conf.c for more information.
119*7c478bd9Sstevel@tonic-gate  */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
122*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
123*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
124*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
125*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
126*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
127*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
128*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
129*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
130*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
131*7c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
132*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
133*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
134*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
135*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
136*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
137*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
138*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
139*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
142*7c478bd9Sstevel@tonic-gate int ptm_debug = 0;
143*7c478bd9Sstevel@tonic-gate #define	DBG(a)	 if (ptm_debug) cmn_err(CE_NOTE, a)
144*7c478bd9Sstevel@tonic-gate #else
145*7c478bd9Sstevel@tonic-gate #define	DBG(a)
146*7c478bd9Sstevel@tonic-gate #endif
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate static int ptmopen(queue_t *, dev_t *, int, int, cred_t *);
149*7c478bd9Sstevel@tonic-gate static int ptmclose(queue_t *, int, cred_t *);
150*7c478bd9Sstevel@tonic-gate static void ptmwput(queue_t *, mblk_t *);
151*7c478bd9Sstevel@tonic-gate static void ptmrsrv(queue_t *);
152*7c478bd9Sstevel@tonic-gate static void ptmwsrv(queue_t *);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * Master Stream Pseudo Terminal Module: stream data structure definitions
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate static struct module_info ptm_info = {
159*7c478bd9Sstevel@tonic-gate 	0xdead,
160*7c478bd9Sstevel@tonic-gate 	"ptm",
161*7c478bd9Sstevel@tonic-gate 	0,
162*7c478bd9Sstevel@tonic-gate 	512,
163*7c478bd9Sstevel@tonic-gate 	512,
164*7c478bd9Sstevel@tonic-gate 	128
165*7c478bd9Sstevel@tonic-gate };
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate static struct qinit ptmrint = {
168*7c478bd9Sstevel@tonic-gate 	NULL,
169*7c478bd9Sstevel@tonic-gate 	(int (*)()) ptmrsrv,
170*7c478bd9Sstevel@tonic-gate 	ptmopen,
171*7c478bd9Sstevel@tonic-gate 	ptmclose,
172*7c478bd9Sstevel@tonic-gate 	NULL,
173*7c478bd9Sstevel@tonic-gate 	&ptm_info,
174*7c478bd9Sstevel@tonic-gate 	NULL
175*7c478bd9Sstevel@tonic-gate };
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate static struct qinit ptmwint = {
178*7c478bd9Sstevel@tonic-gate 	(int (*)()) ptmwput,
179*7c478bd9Sstevel@tonic-gate 	(int (*)()) ptmwsrv,
180*7c478bd9Sstevel@tonic-gate 	NULL,
181*7c478bd9Sstevel@tonic-gate 	NULL,
182*7c478bd9Sstevel@tonic-gate 	NULL,
183*7c478bd9Sstevel@tonic-gate 	&ptm_info,
184*7c478bd9Sstevel@tonic-gate 	NULL
185*7c478bd9Sstevel@tonic-gate };
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate static struct streamtab ptminfo = {
188*7c478bd9Sstevel@tonic-gate 	&ptmrint,
189*7c478bd9Sstevel@tonic-gate 	&ptmwint,
190*7c478bd9Sstevel@tonic-gate 	NULL,
191*7c478bd9Sstevel@tonic-gate 	NULL
192*7c478bd9Sstevel@tonic-gate };
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate static int ptm_attach(dev_info_t *, ddi_attach_cmd_t);
195*7c478bd9Sstevel@tonic-gate static int ptm_detach(dev_info_t *, ddi_detach_cmd_t);
196*7c478bd9Sstevel@tonic-gate static int ptm_devinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static dev_info_t	*ptm_dip;		/* private devinfo pointer */
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate /*
201*7c478bd9Sstevel@tonic-gate  * this will define (struct cb_ops cb_ptm_ops) and (struct dev_ops ptm_ops)
202*7c478bd9Sstevel@tonic-gate  */
203*7c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(ptm_ops, nulldev, nulldev, ptm_attach, ptm_detach,
204*7c478bd9Sstevel@tonic-gate     nodev, ptm_devinfo, D_MP, &ptminfo);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate /*
207*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
211*7c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
212*7c478bd9Sstevel@tonic-gate 	"Master streams driver 'ptm' %I%",
213*7c478bd9Sstevel@tonic-gate 	&ptm_ops,	/* driver ops */
214*7c478bd9Sstevel@tonic-gate };
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
217*7c478bd9Sstevel@tonic-gate 	MODREV_1,
218*7c478bd9Sstevel@tonic-gate 	&modldrv,
219*7c478bd9Sstevel@tonic-gate 	NULL
220*7c478bd9Sstevel@tonic-gate };
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate int
223*7c478bd9Sstevel@tonic-gate _init(void)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	int rc;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if ((rc = mod_install(&modlinkage)) == 0)
228*7c478bd9Sstevel@tonic-gate 		ptms_init();
229*7c478bd9Sstevel@tonic-gate 	return (rc);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate int
233*7c478bd9Sstevel@tonic-gate _fini(void)
234*7c478bd9Sstevel@tonic-gate {
235*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate int
239*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static int
245*7c478bd9Sstevel@tonic-gate ptm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
248*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmajor", S_IFCHR,
251*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
252*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
253*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "ptmx", S_IFCHR,
256*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, CLONE_DEV) == DDI_FAILURE) {
257*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
258*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	ptm_dip = devi;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate static int
266*7c478bd9Sstevel@tonic-gate ptm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
269*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
272*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
276*7c478bd9Sstevel@tonic-gate static int
277*7c478bd9Sstevel@tonic-gate ptm_devinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
278*7c478bd9Sstevel@tonic-gate     void **result)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	int error;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
283*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
284*7c478bd9Sstevel@tonic-gate 		if (ptm_dip == NULL) {
285*7c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
286*7c478bd9Sstevel@tonic-gate 		} else {
287*7c478bd9Sstevel@tonic-gate 			*result = (void *)ptm_dip;
288*7c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 		break;
291*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
292*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
293*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
294*7c478bd9Sstevel@tonic-gate 		break;
295*7c478bd9Sstevel@tonic-gate 	default:
296*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate 	return (error);
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * Open a minor of the master device. Store the write queue pointer and set the
305*7c478bd9Sstevel@tonic-gate  * pt_state field to (PTMOPEN | PTLOCK).
306*7c478bd9Sstevel@tonic-gate  * This code will work properly with both clone opens and direct opens of the
307*7c478bd9Sstevel@tonic-gate  * master device.
308*7c478bd9Sstevel@tonic-gate  */
309*7c478bd9Sstevel@tonic-gate static int
310*7c478bd9Sstevel@tonic-gate ptmopen(
311*7c478bd9Sstevel@tonic-gate 	queue_t *rqp,		/* pointer to the read side queue */
312*7c478bd9Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
313*7c478bd9Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
314*7c478bd9Sstevel@tonic-gate 	int	sflag,		/* open state flag */
315*7c478bd9Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	extern dev_info_t *pts_dip;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
320*7c478bd9Sstevel@tonic-gate 	mblk_t		*mop;		/* ptr to a setopts message block */
321*7c478bd9Sstevel@tonic-gate 	struct stroptions *sop;
322*7c478bd9Sstevel@tonic-gate 	minor_t		dminor = getminor(*devp);
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	/* Allow reopen */
325*7c478bd9Sstevel@tonic-gate 	if (rqp->q_ptr != NULL)
326*7c478bd9Sstevel@tonic-gate 		return (0);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	if (sflag & MODOPEN)
329*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	if (!(sflag & CLONEOPEN) && dminor != 0) {
332*7c478bd9Sstevel@tonic-gate 		/*
333*7c478bd9Sstevel@tonic-gate 		 * This is a direct open to specific master device through an
334*7c478bd9Sstevel@tonic-gate 		 * artificially created entry with specific minor in
335*7c478bd9Sstevel@tonic-gate 		 * /dev/directory. Such behavior is not supported.
336*7c478bd9Sstevel@tonic-gate 		 */
337*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/*
341*7c478bd9Sstevel@tonic-gate 	 * pts dependency: pt_ttys_alloc(), used below, really needs the pts
342*7c478bd9Sstevel@tonic-gate 	 * driver (and pts_dip variable) to be initialized to successfully
343*7c478bd9Sstevel@tonic-gate 	 * create device nodes.
344*7c478bd9Sstevel@tonic-gate 	 */
345*7c478bd9Sstevel@tonic-gate 	if (pts_dip == NULL)
346*7c478bd9Sstevel@tonic-gate 		(void) i_ddi_attach_pseudo_node("pts");
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	mop = allocb(sizeof (struct stroptions), BPRI_MED);
349*7c478bd9Sstevel@tonic-gate 	if (mop == NULL) {
350*7c478bd9Sstevel@tonic-gate 		DDBG("ptmopen(): mop allocation failed\n", 0);
351*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if ((ptmp = pt_ttys_alloc()) == NULL) {
355*7c478bd9Sstevel@tonic-gate 		DDBG("ptmopen(): pty allocation failed\n", 0);
356*7c478bd9Sstevel@tonic-gate 		freemsg(mop);
357*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	dminor = ptmp->pt_minor;
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	DDBGP("ptmopen(): allocated ptmp %p\n", (uintptr_t)ptmp);
363*7c478bd9Sstevel@tonic-gate 	DDBG("ptmopen(): allocated minor %d\n", dminor);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = rqp->q_ptr = ptmp;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	qprocson(rqp);
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	/* Allow slave to send messages to master */
370*7c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
371*7c478bd9Sstevel@tonic-gate 	ptmp->ptm_rdq = rqp;
372*7c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	/*
375*7c478bd9Sstevel@tonic-gate 	 * set up hi/lo water marks on stream head read queue
376*7c478bd9Sstevel@tonic-gate 	 * and add controlling tty if not set
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
379*7c478bd9Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
380*7c478bd9Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
381*7c478bd9Sstevel@tonic-gate 	if (oflag & FNOCTTY)
382*7c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT;
383*7c478bd9Sstevel@tonic-gate 	else
384*7c478bd9Sstevel@tonic-gate 		sop->so_flags = SO_HIWAT | SO_LOWAT | SO_ISTTY;
385*7c478bd9Sstevel@tonic-gate 	sop->so_hiwat = 512;
386*7c478bd9Sstevel@tonic-gate 	sop->so_lowat = 256;
387*7c478bd9Sstevel@tonic-gate 	putnext(rqp, mop);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	/*
390*7c478bd9Sstevel@tonic-gate 	 * The input, devp, is a major device number, the output is put
391*7c478bd9Sstevel@tonic-gate 	 * into the same parm as a major,minor pair.
392*7c478bd9Sstevel@tonic-gate 	 */
393*7c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), dminor);
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	return (0);
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate /*
400*7c478bd9Sstevel@tonic-gate  * Find the address to private data identifying the slave's write queue.
401*7c478bd9Sstevel@tonic-gate  * Send a hang-up message up the slave's read queue to designate the
402*7c478bd9Sstevel@tonic-gate  * master/slave pair is tearing down. Uattach the master and slave by
403*7c478bd9Sstevel@tonic-gate  * nulling out the write queue fields in the private data structure.
404*7c478bd9Sstevel@tonic-gate  * Finally, unlock the master/slave pair and mark the master as closed.
405*7c478bd9Sstevel@tonic-gate  */
406*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
407*7c478bd9Sstevel@tonic-gate static int
408*7c478bd9Sstevel@tonic-gate ptmclose(queue_t *rqp, int flag, cred_t *credp)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
411*7c478bd9Sstevel@tonic-gate 	queue_t *pts_rdq;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	ASSERT(rqp->q_ptr);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)rqp->q_ptr;
416*7c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
417*7c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
418*7c478bd9Sstevel@tonic-gate 		pts_rdq = ptmp->pts_rdq;
419*7c478bd9Sstevel@tonic-gate 		if (pts_rdq->q_next) {
420*7c478bd9Sstevel@tonic-gate 			DBG(("send hangup message to slave\n"));
421*7c478bd9Sstevel@tonic-gate 			(void) putnextctl(pts_rdq, M_HANGUP);
422*7c478bd9Sstevel@tonic-gate 		}
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
425*7c478bd9Sstevel@tonic-gate 	/*
426*7c478bd9Sstevel@tonic-gate 	 * ptm_rdq should be cleared before call to qprocsoff() to prevent pts
427*7c478bd9Sstevel@tonic-gate 	 * write procedure to attempt using ptm_rdq after qprocsoff.
428*7c478bd9Sstevel@tonic-gate 	 */
429*7c478bd9Sstevel@tonic-gate 	PT_ENTER_WRITE(ptmp);
430*7c478bd9Sstevel@tonic-gate 	ptmp->ptm_rdq = NULL;
431*7c478bd9Sstevel@tonic-gate 	freemsg(ptmp->pt_nullmsg);
432*7c478bd9Sstevel@tonic-gate 	ptmp->pt_nullmsg = NULL;
433*7c478bd9Sstevel@tonic-gate 	/*
434*7c478bd9Sstevel@tonic-gate 	 * qenable slave side write queue so that it can flush
435*7c478bd9Sstevel@tonic-gate 	 * its messages as master's read queue is going away
436*7c478bd9Sstevel@tonic-gate 	 */
437*7c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq)
438*7c478bd9Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
439*7c478bd9Sstevel@tonic-gate 	PT_EXIT_WRITE(ptmp);
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	qprocsoff(rqp);
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	/* Finish the close */
444*7c478bd9Sstevel@tonic-gate 	rqp->q_ptr = NULL;
445*7c478bd9Sstevel@tonic-gate 	WR(rqp)->q_ptr = NULL;
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	ptms_close(ptmp, PTMOPEN | PTLOCK);
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	return (0);
450*7c478bd9Sstevel@tonic-gate }
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate /*
453*7c478bd9Sstevel@tonic-gate  * The wput procedure will only handle ioctl and flush messages.
454*7c478bd9Sstevel@tonic-gate  */
455*7c478bd9Sstevel@tonic-gate static void
456*7c478bd9Sstevel@tonic-gate ptmwput(queue_t *qp, mblk_t *mp)
457*7c478bd9Sstevel@tonic-gate {
458*7c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
459*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	DBG(("entering ptmwput\n"));
462*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
465*7c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
468*7c478bd9Sstevel@tonic-gate 	/*
469*7c478bd9Sstevel@tonic-gate 	 * if write queue request, flush master's write
470*7c478bd9Sstevel@tonic-gate 	 * queue and send FLUSHR up slave side. If read
471*7c478bd9Sstevel@tonic-gate 	 * queue request, convert to FLUSHW and putnext().
472*7c478bd9Sstevel@tonic-gate 	 */
473*7c478bd9Sstevel@tonic-gate 	case M_FLUSH:
474*7c478bd9Sstevel@tonic-gate 		{
475*7c478bd9Sstevel@tonic-gate 			unsigned char flush_flg = 0;
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 			DBG(("ptm got flush request\n"));
478*7c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
479*7c478bd9Sstevel@tonic-gate 				DBG(("got FLUSHW, flush ptm write Q\n"));
480*7c478bd9Sstevel@tonic-gate 				if (*mp->b_rptr & FLUSHBAND)
481*7c478bd9Sstevel@tonic-gate 					/*
482*7c478bd9Sstevel@tonic-gate 					 * if it is a FLUSHBAND, do flushband.
483*7c478bd9Sstevel@tonic-gate 					 */
484*7c478bd9Sstevel@tonic-gate 					flushband(qp, *(mp->b_rptr + 1),
485*7c478bd9Sstevel@tonic-gate 					    FLUSHDATA);
486*7c478bd9Sstevel@tonic-gate 				else
487*7c478bd9Sstevel@tonic-gate 					flushq(qp, FLUSHDATA);
488*7c478bd9Sstevel@tonic-gate 				flush_flg = (*mp->b_rptr & ~FLUSHW) | FLUSHR;
489*7c478bd9Sstevel@tonic-gate 			}
490*7c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHR) {
491*7c478bd9Sstevel@tonic-gate 				DBG(("got FLUSHR, set FLUSHW\n"));
492*7c478bd9Sstevel@tonic-gate 				flush_flg |= (*mp->b_rptr & ~FLUSHR) | FLUSHW;
493*7c478bd9Sstevel@tonic-gate 			}
494*7c478bd9Sstevel@tonic-gate 			if (flush_flg != 0 && ptmp->pts_rdq &&
495*7c478bd9Sstevel@tonic-gate 			    !(ptmp->pt_state & PTLOCK)) {
496*7c478bd9Sstevel@tonic-gate 				DBG(("putnext to pts\n"));
497*7c478bd9Sstevel@tonic-gate 				*mp->b_rptr = flush_flg;
498*7c478bd9Sstevel@tonic-gate 				putnext(ptmp->pts_rdq, mp);
499*7c478bd9Sstevel@tonic-gate 			} else
500*7c478bd9Sstevel@tonic-gate 				freemsg(mp);
501*7c478bd9Sstevel@tonic-gate 			break;
502*7c478bd9Sstevel@tonic-gate 		}
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	case M_IOCTL:
505*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
506*7c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_cmd) {
507*7c478bd9Sstevel@tonic-gate 		default:
508*7c478bd9Sstevel@tonic-gate 			if ((ptmp->pt_state & PTLOCK) ||
509*7c478bd9Sstevel@tonic-gate 			    (ptmp->pts_rdq == NULL)) {
510*7c478bd9Sstevel@tonic-gate 				DBG(("got M_IOCTL but no slave\n"));
511*7c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
512*7c478bd9Sstevel@tonic-gate 				PT_EXIT_READ(ptmp);
513*7c478bd9Sstevel@tonic-gate 				return;
514*7c478bd9Sstevel@tonic-gate 			}
515*7c478bd9Sstevel@tonic-gate 			(void) putq(qp, mp);
516*7c478bd9Sstevel@tonic-gate 			break;
517*7c478bd9Sstevel@tonic-gate 		case UNLKPT:
518*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
519*7c478bd9Sstevel@tonic-gate 			ptmp->pt_state &= ~PTLOCK;
520*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
521*7c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
522*7c478bd9Sstevel@tonic-gate 		case ISPTM:
523*7c478bd9Sstevel@tonic-gate 			DBG(("ack the UNLKPT/ISPTM\n"));
524*7c478bd9Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
525*7c478bd9Sstevel@tonic-gate 			break;
526*7c478bd9Sstevel@tonic-gate 		case ZONEPT:
527*7c478bd9Sstevel@tonic-gate 		{
528*7c478bd9Sstevel@tonic-gate 			zoneid_t z;
529*7c478bd9Sstevel@tonic-gate 			int error;
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 			if ((error = drv_priv(iocp->ioc_cr)) != 0) {
532*7c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
533*7c478bd9Sstevel@tonic-gate 				break;
534*7c478bd9Sstevel@tonic-gate 			}
535*7c478bd9Sstevel@tonic-gate 			if ((error = miocpullup(mp, sizeof (zoneid_t))) != 0) {
536*7c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, error);
537*7c478bd9Sstevel@tonic-gate 				break;
538*7c478bd9Sstevel@tonic-gate 			}
539*7c478bd9Sstevel@tonic-gate 			z = *((zoneid_t *)mp->b_cont->b_rptr);
540*7c478bd9Sstevel@tonic-gate 			if (z < MIN_ZONEID || z > MAX_ZONEID) {
541*7c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
542*7c478bd9Sstevel@tonic-gate 				break;
543*7c478bd9Sstevel@tonic-gate 			}
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ptmp->pt_lock);
546*7c478bd9Sstevel@tonic-gate 			ptmp->pt_zoneid = z;
547*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ptmp->pt_lock);
548*7c478bd9Sstevel@tonic-gate 			miocack(qp, mp, 0, 0);
549*7c478bd9Sstevel@tonic-gate 			break;
550*7c478bd9Sstevel@tonic-gate 		}
551*7c478bd9Sstevel@tonic-gate 		}
552*7c478bd9Sstevel@tonic-gate 		break;
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	case M_READ:
555*7c478bd9Sstevel@tonic-gate 		/* Caused by ldterm - can not pass to slave */
556*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
557*7c478bd9Sstevel@tonic-gate 		break;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	/*
560*7c478bd9Sstevel@tonic-gate 	 * send other messages to slave
561*7c478bd9Sstevel@tonic-gate 	 */
562*7c478bd9Sstevel@tonic-gate 	default:
563*7c478bd9Sstevel@tonic-gate 		if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
564*7c478bd9Sstevel@tonic-gate 			DBG(("got msg. but no slave\n"));
565*7c478bd9Sstevel@tonic-gate 			mp = mexchange(NULL, mp, 2, M_ERROR, -1);
566*7c478bd9Sstevel@tonic-gate 			if (mp != NULL) {
567*7c478bd9Sstevel@tonic-gate 				mp->b_rptr[0] = NOERROR;
568*7c478bd9Sstevel@tonic-gate 				mp->b_rptr[1] = EINVAL;
569*7c478bd9Sstevel@tonic-gate 				qreply(qp, mp);
570*7c478bd9Sstevel@tonic-gate 			}
571*7c478bd9Sstevel@tonic-gate 			PT_EXIT_READ(ptmp);
572*7c478bd9Sstevel@tonic-gate 			return;
573*7c478bd9Sstevel@tonic-gate 		}
574*7c478bd9Sstevel@tonic-gate 		DBG(("put msg on master's write queue\n"));
575*7c478bd9Sstevel@tonic-gate 		(void) putq(qp, mp);
576*7c478bd9Sstevel@tonic-gate 		break;
577*7c478bd9Sstevel@tonic-gate 	}
578*7c478bd9Sstevel@tonic-gate 	DBG(("return from ptmwput()\n"));
579*7c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
580*7c478bd9Sstevel@tonic-gate }
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate /*
584*7c478bd9Sstevel@tonic-gate  * enable the write side of the slave. This triggers the
585*7c478bd9Sstevel@tonic-gate  * slave to send any messages queued on its write side to
586*7c478bd9Sstevel@tonic-gate  * the read side of this master.
587*7c478bd9Sstevel@tonic-gate  */
588*7c478bd9Sstevel@tonic-gate static void
589*7c478bd9Sstevel@tonic-gate ptmrsrv(queue_t *qp)
590*7c478bd9Sstevel@tonic-gate {
591*7c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	DBG(("entering ptmrsrv\n"));
594*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
597*7c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
598*7c478bd9Sstevel@tonic-gate 	if (ptmp->pts_rdq) {
599*7c478bd9Sstevel@tonic-gate 		qenable(WR(ptmp->pts_rdq));
600*7c478bd9Sstevel@tonic-gate 	}
601*7c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
602*7c478bd9Sstevel@tonic-gate 	DBG(("leaving ptmrsrv\n"));
603*7c478bd9Sstevel@tonic-gate }
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate /*
607*7c478bd9Sstevel@tonic-gate  * If there are messages on this queue that can be sent to
608*7c478bd9Sstevel@tonic-gate  * slave, send them via putnext(). Else, if queued messages
609*7c478bd9Sstevel@tonic-gate  * cannot be sent, leave them on this queue. If priority
610*7c478bd9Sstevel@tonic-gate  * messages on this queue, send them to slave no matter what.
611*7c478bd9Sstevel@tonic-gate  */
612*7c478bd9Sstevel@tonic-gate static void
613*7c478bd9Sstevel@tonic-gate ptmwsrv(queue_t *qp)
614*7c478bd9Sstevel@tonic-gate {
615*7c478bd9Sstevel@tonic-gate 	struct pt_ttys	*ptmp;
616*7c478bd9Sstevel@tonic-gate 	mblk_t 		*mp;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	DBG(("entering ptmwsrv\n"));
619*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_ptr);
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	ptmp = (struct pt_ttys *)qp->q_ptr;
622*7c478bd9Sstevel@tonic-gate 	PT_ENTER_READ(ptmp);
623*7c478bd9Sstevel@tonic-gate 	if ((ptmp->pt_state  & PTLOCK) || (ptmp->pts_rdq == NULL)) {
624*7c478bd9Sstevel@tonic-gate 		DBG(("in master write srv proc but no slave\n"));
625*7c478bd9Sstevel@tonic-gate 		/*
626*7c478bd9Sstevel@tonic-gate 		 * Free messages on the write queue and send
627*7c478bd9Sstevel@tonic-gate 		 * NAK for any M_IOCTL type messages to wakeup
628*7c478bd9Sstevel@tonic-gate 		 * the user process waiting for ACK/NAK from
629*7c478bd9Sstevel@tonic-gate 		 * the ioctl invocation
630*7c478bd9Sstevel@tonic-gate 		 */
631*7c478bd9Sstevel@tonic-gate 		while ((mp = getq(qp)) != NULL) {
632*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCTL)
633*7c478bd9Sstevel@tonic-gate 				miocnak(qp, mp, 0, EINVAL);
634*7c478bd9Sstevel@tonic-gate 			else
635*7c478bd9Sstevel@tonic-gate 				freemsg(mp);
636*7c478bd9Sstevel@tonic-gate 		}
637*7c478bd9Sstevel@tonic-gate 		flushq(qp, FLUSHALL);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 		mp = mexchange(NULL, NULL, 2, M_ERROR, -1);
640*7c478bd9Sstevel@tonic-gate 		if (mp != NULL) {
641*7c478bd9Sstevel@tonic-gate 			mp->b_rptr[0] = NOERROR;
642*7c478bd9Sstevel@tonic-gate 			mp->b_rptr[1] = EINVAL;
643*7c478bd9Sstevel@tonic-gate 			qreply(qp, mp);
644*7c478bd9Sstevel@tonic-gate 		}
645*7c478bd9Sstevel@tonic-gate 		PT_EXIT_READ(ptmp);
646*7c478bd9Sstevel@tonic-gate 		return;
647*7c478bd9Sstevel@tonic-gate 	}
648*7c478bd9Sstevel@tonic-gate 	/*
649*7c478bd9Sstevel@tonic-gate 	 * while there are messages on this write queue...
650*7c478bd9Sstevel@tonic-gate 	 */
651*7c478bd9Sstevel@tonic-gate 	while ((mp = getq(qp)) != NULL) {
652*7c478bd9Sstevel@tonic-gate 		/*
653*7c478bd9Sstevel@tonic-gate 		 * if don't have control message and cannot put
654*7c478bd9Sstevel@tonic-gate 		 * msg. on slave's read queue, put it back on
655*7c478bd9Sstevel@tonic-gate 		 * this queue.
656*7c478bd9Sstevel@tonic-gate 		 */
657*7c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type <= QPCTL &&
658*7c478bd9Sstevel@tonic-gate 		    !bcanputnext(ptmp->pts_rdq, mp->b_band)) {
659*7c478bd9Sstevel@tonic-gate 			DBG(("put msg. back on queue\n"));
660*7c478bd9Sstevel@tonic-gate 			(void) putbq(qp, mp);
661*7c478bd9Sstevel@tonic-gate 			break;
662*7c478bd9Sstevel@tonic-gate 		}
663*7c478bd9Sstevel@tonic-gate 		/*
664*7c478bd9Sstevel@tonic-gate 		 * else send the message up slave's stream
665*7c478bd9Sstevel@tonic-gate 		 */
666*7c478bd9Sstevel@tonic-gate 		DBG(("send message to slave\n"));
667*7c478bd9Sstevel@tonic-gate 		putnext(ptmp->pts_rdq, mp);
668*7c478bd9Sstevel@tonic-gate 	}
669*7c478bd9Sstevel@tonic-gate 	DBG(("leaving ptmwsrv\n"));
670*7c478bd9Sstevel@tonic-gate 	PT_EXIT_READ(ptmp);
671*7c478bd9Sstevel@tonic-gate }
672