xref: /onnv-gate/usr/src/uts/common/io/pckt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from S5R4 1.10 */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * Description: The pckt module packetizes messages on
35*0Sstevel@tonic-gate  *		its read queue by pre-fixing an M_PROTO
36*0Sstevel@tonic-gate  *		message type to certain incoming messages.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include <sys/stream.h>
42*0Sstevel@tonic-gate #include <sys/stropts.h>
43*0Sstevel@tonic-gate #include <sys/kmem.h>
44*0Sstevel@tonic-gate #include <sys/errno.h>
45*0Sstevel@tonic-gate #include <sys/ddi.h>
46*0Sstevel@tonic-gate #include <sys/sunddi.h>
47*0Sstevel@tonic-gate #include <sys/debug.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * This is the loadable module wrapper.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate #include <sys/conf.h>
53*0Sstevel@tonic-gate #include <sys/modctl.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static struct streamtab pcktinfo;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * Per queue instances are single-threaded since the q_ptr
59*0Sstevel@tonic-gate  * field of queues need to be shared among threads.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate static struct fmodsw fsw = {
62*0Sstevel@tonic-gate 	"pckt",
63*0Sstevel@tonic-gate 	&pcktinfo,
64*0Sstevel@tonic-gate 	D_NEW | D_MTPERQ | D_MP
65*0Sstevel@tonic-gate };
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Module linkage information for the kernel.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
72*0Sstevel@tonic-gate 	&mod_strmodops,
73*0Sstevel@tonic-gate 	"pckt module",
74*0Sstevel@tonic-gate 	&fsw
75*0Sstevel@tonic-gate };
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
78*0Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate int
_init(void)83*0Sstevel@tonic-gate _init(void)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate int
_fini(void)89*0Sstevel@tonic-gate _fini(void)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate int
_info(struct modinfo * modinfop)95*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate static int	pcktopen(queue_t *, dev_t *, int, int, cred_t *);
101*0Sstevel@tonic-gate static int	pcktclose(queue_t *, int, cred_t *);
102*0Sstevel@tonic-gate static void	pcktrput(queue_t *, mblk_t *);
103*0Sstevel@tonic-gate static void	pcktrsrv(queue_t *);
104*0Sstevel@tonic-gate static void	pcktwput(queue_t *, mblk_t *);
105*0Sstevel@tonic-gate static mblk_t	*add_ctl_info(queue_t *, mblk_t *);
106*0Sstevel@tonic-gate static void	add_ctl_wkup(void *);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Stream module data structure definitions.
111*0Sstevel@tonic-gate  * Sits over the ptm module generally.
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  * Read side flow control strategy: Since we may be putting messages on
114*0Sstevel@tonic-gate  * the read q due to allocb failures, these failures must get
115*0Sstevel@tonic-gate  * reflected fairly quickly to the module below us.
116*0Sstevel@tonic-gate  * No sense in piling on messages in times of memory shortage.
117*0Sstevel@tonic-gate  * Further, for the case of upper level flow control, there is no
118*0Sstevel@tonic-gate  * compelling reason to have more buffering in this module.
119*0Sstevel@tonic-gate  * Thus use a hi-water mark of one.
120*0Sstevel@tonic-gate  * This module imposes no max packet size, there is no inherent reason
121*0Sstevel@tonic-gate  * in the code to do so.
122*0Sstevel@tonic-gate  */
123*0Sstevel@tonic-gate static struct module_info pcktiinfo = {
124*0Sstevel@tonic-gate 	0x9898,					/* module id number */
125*0Sstevel@tonic-gate 	"pckt",					/* module name */
126*0Sstevel@tonic-gate 	0,					/* minimum packet size */
127*0Sstevel@tonic-gate 	INFPSZ,					/* maximum packet size */
128*0Sstevel@tonic-gate 	1,					/* hi-water mark */
129*0Sstevel@tonic-gate 	0					/* lo-water mark */
130*0Sstevel@tonic-gate };
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate  * Write side flow control strategy: There is no write service procedure.
134*0Sstevel@tonic-gate  * The write put function is pass thru, thus there is no reason to have any
135*0Sstevel@tonic-gate  * limits on the maximum packet size.
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate static struct module_info pcktoinfo = {
138*0Sstevel@tonic-gate 	0x9898,					/* module id number */
139*0Sstevel@tonic-gate 	"pckt",					/* module name */
140*0Sstevel@tonic-gate 	0,					/* minimum packet size */
141*0Sstevel@tonic-gate 	INFPSZ,					/* maximum packet size */
142*0Sstevel@tonic-gate 	0,					/* hi-water mark */
143*0Sstevel@tonic-gate 	0					/* lo-water mark */
144*0Sstevel@tonic-gate };
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate static struct qinit pcktrinit = {
147*0Sstevel@tonic-gate 	(int (*)())pcktrput,
148*0Sstevel@tonic-gate 	(int (*)())pcktrsrv,
149*0Sstevel@tonic-gate 	pcktopen,
150*0Sstevel@tonic-gate 	pcktclose,
151*0Sstevel@tonic-gate 	NULL,
152*0Sstevel@tonic-gate 	&pcktiinfo,
153*0Sstevel@tonic-gate 	NULL
154*0Sstevel@tonic-gate };
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate static struct qinit pcktwinit = {
157*0Sstevel@tonic-gate 	(int (*)())pcktwput,
158*0Sstevel@tonic-gate 	NULL,
159*0Sstevel@tonic-gate 	NULL,
160*0Sstevel@tonic-gate 	NULL,
161*0Sstevel@tonic-gate 	NULL,
162*0Sstevel@tonic-gate 	&pcktoinfo,
163*0Sstevel@tonic-gate 	NULL
164*0Sstevel@tonic-gate };
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static struct streamtab pcktinfo = {
167*0Sstevel@tonic-gate 	&pcktrinit,
168*0Sstevel@tonic-gate 	&pcktwinit,
169*0Sstevel@tonic-gate 	NULL,
170*0Sstevel@tonic-gate 	NULL
171*0Sstevel@tonic-gate };
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * Per-instance state struct for the pckt module.
176*0Sstevel@tonic-gate  */
177*0Sstevel@tonic-gate struct pckt_info {
178*0Sstevel@tonic-gate 	queue_t		*pi_qptr;		/* back pointer to q */
179*0Sstevel@tonic-gate 	bufcall_id_t	pi_bufcall_id;
180*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
181*0Sstevel@tonic-gate 	model_t		model;
182*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
183*0Sstevel@tonic-gate };
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * Dummy qbufcall callback routine used by open and close.
187*0Sstevel@tonic-gate  * The framework will wake up qwait_sig when we return from
188*0Sstevel@tonic-gate  * this routine (as part of leaving the perimeters.)
189*0Sstevel@tonic-gate  * (The framework enters the perimeters before calling the qbufcall() callback
190*0Sstevel@tonic-gate  * and leaves the perimeters after the callback routine has executed. The
191*0Sstevel@tonic-gate  * framework performs an implicit wakeup of any thread in qwait/qwait_sig
192*0Sstevel@tonic-gate  * when it leaves the perimeter. See qwait(9E).)
193*0Sstevel@tonic-gate  */
194*0Sstevel@tonic-gate /* ARGSUSED */
195*0Sstevel@tonic-gate static void
dummy_callback(void * arg)196*0Sstevel@tonic-gate dummy_callback(void *arg)
197*0Sstevel@tonic-gate {}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate /*
200*0Sstevel@tonic-gate  * pcktopen - open routine gets called when the
201*0Sstevel@tonic-gate  *	    module gets pushed onto the stream.
202*0Sstevel@tonic-gate  */
203*0Sstevel@tonic-gate /*ARGSUSED*/
204*0Sstevel@tonic-gate static int
pcktopen(queue_t * q,dev_t * devp,int oflag,int sflag,cred_t * credp)205*0Sstevel@tonic-gate pcktopen(
206*0Sstevel@tonic-gate 	queue_t *q,		/* pointer to the read side queue */
207*0Sstevel@tonic-gate 	dev_t   *devp,		/* pointer to stream tail's dev */
208*0Sstevel@tonic-gate 	int	oflag,		/* the user open(2) supplied flags */
209*0Sstevel@tonic-gate 	int	sflag,		/* open state flag */
210*0Sstevel@tonic-gate 	cred_t  *credp)		/* credentials */
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	struct pckt_info	*pip;
213*0Sstevel@tonic-gate 	mblk_t			*mop; /* ptr to a setopts msg block */
214*0Sstevel@tonic-gate 	struct stroptions	*sop;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if (sflag != MODOPEN)
217*0Sstevel@tonic-gate 		return (EINVAL);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if (q->q_ptr != NULL) {
220*0Sstevel@tonic-gate 		/* It's already attached. */
221*0Sstevel@tonic-gate 		return (0);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	/*
225*0Sstevel@tonic-gate 	 * Allocate state structure.
226*0Sstevel@tonic-gate 	 */
227*0Sstevel@tonic-gate 	pip = kmem_alloc(sizeof (*pip), KM_SLEEP);
228*0Sstevel@tonic-gate 	bzero(pip, sizeof (*pip));
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
231*0Sstevel@tonic-gate 	pip->model = ddi_model_convert_from(get_udatamodel());
232*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	/*
235*0Sstevel@tonic-gate 	 * Cross-link.
236*0Sstevel@tonic-gate 	 */
237*0Sstevel@tonic-gate 	pip->pi_qptr = q;
238*0Sstevel@tonic-gate 	q->q_ptr = pip;
239*0Sstevel@tonic-gate 	WR(q)->q_ptr = pip;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	qprocson(q);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * Initialize an M_SETOPTS message to set up hi/lo water marks on
245*0Sstevel@tonic-gate 	 * stream head read queue.
246*0Sstevel@tonic-gate 	 */
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	while ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
249*0Sstevel@tonic-gate 		bufcall_id_t id = qbufcall(q, sizeof (struct stroptions),
250*0Sstevel@tonic-gate 		    BPRI_MED, dummy_callback, NULL);
251*0Sstevel@tonic-gate 		if (!qwait_sig(q)) {
252*0Sstevel@tonic-gate 			qunbufcall(q, id);
253*0Sstevel@tonic-gate 			kmem_free(pip, sizeof (*pip));
254*0Sstevel@tonic-gate 			qprocsoff(q);
255*0Sstevel@tonic-gate 			return (EINTR);
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		qunbufcall(q, id);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	/*
262*0Sstevel@tonic-gate 	 * XXX: Should this module really control the hi/low water marks?
263*0Sstevel@tonic-gate 	 * Is there any reason in this code to do so?
264*0Sstevel@tonic-gate 	 */
265*0Sstevel@tonic-gate 	mop->b_datap->db_type = M_SETOPTS;
266*0Sstevel@tonic-gate 	mop->b_wptr += sizeof (struct stroptions);
267*0Sstevel@tonic-gate 	sop = (struct stroptions *)mop->b_rptr;
268*0Sstevel@tonic-gate 	sop->so_flags = SO_HIWAT | SO_LOWAT;
269*0Sstevel@tonic-gate 	sop->so_hiwat = 512;
270*0Sstevel@tonic-gate 	sop->so_lowat = 256;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	/*
273*0Sstevel@tonic-gate 	 * Commit to the open and send the M_SETOPTS off to the stream head.
274*0Sstevel@tonic-gate 	 */
275*0Sstevel@tonic-gate 	putnext(q, mop);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	return (0);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate /*
282*0Sstevel@tonic-gate  * pcktclose - This routine gets called when the module
283*0Sstevel@tonic-gate  *	gets popped off of the stream.
284*0Sstevel@tonic-gate  */
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /*ARGSUSED*/
287*0Sstevel@tonic-gate static int
pcktclose(queue_t * q,int flag,cred_t * credp)288*0Sstevel@tonic-gate pcktclose(
289*0Sstevel@tonic-gate 	queue_t *q,	/* Pointer to the read queue */
290*0Sstevel@tonic-gate 	int	flag,
291*0Sstevel@tonic-gate 	cred_t  *credp)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	qprocsoff(q);
296*0Sstevel@tonic-gate 	/*
297*0Sstevel@tonic-gate 	 * Cancel outstanding qbufcall
298*0Sstevel@tonic-gate 	 */
299*0Sstevel@tonic-gate 	if (pip->pi_bufcall_id) {
300*0Sstevel@tonic-gate 		qunbufcall(q, pip->pi_bufcall_id);
301*0Sstevel@tonic-gate 		pip->pi_bufcall_id = 0;
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 	/*
304*0Sstevel@tonic-gate 	 * Do not worry about msgs queued on the q, the framework
305*0Sstevel@tonic-gate 	 * will free them up.
306*0Sstevel@tonic-gate 	 */
307*0Sstevel@tonic-gate 	kmem_free(q->q_ptr, sizeof (struct pckt_info));
308*0Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
309*0Sstevel@tonic-gate 	return (0);
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate  * pcktrput - Module read queue put procedure.
314*0Sstevel@tonic-gate  *	This is called from the module or
315*0Sstevel@tonic-gate  *	driver downstream.
316*0Sstevel@tonic-gate  */
317*0Sstevel@tonic-gate static void
pcktrput(queue_t * q,mblk_t * mp)318*0Sstevel@tonic-gate pcktrput(
319*0Sstevel@tonic-gate 	queue_t *q,	/* Pointer to the read queue */
320*0Sstevel@tonic-gate 	mblk_t *mp)	/* Pointer to the current message block */
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	mblk_t		*pckt_msgp;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
326*0Sstevel@tonic-gate 	case M_FLUSH:
327*0Sstevel@tonic-gate 		/*
328*0Sstevel@tonic-gate 		 * The PTS driver swaps the FLUSHR and FLUSHW flags
329*0Sstevel@tonic-gate 		 * we need to swap them back to reflect the actual
330*0Sstevel@tonic-gate 		 * slave side FLUSH mode.
331*0Sstevel@tonic-gate 		 */
332*0Sstevel@tonic-gate 		if ((*mp->b_rptr & FLUSHRW) != FLUSHRW)
333*0Sstevel@tonic-gate 			if ((*mp->b_rptr & FLUSHRW) == FLUSHR)
334*0Sstevel@tonic-gate 				*mp->b_rptr = FLUSHW;
335*0Sstevel@tonic-gate 			else if ((*mp->b_rptr & FLUSHRW) == FLUSHW)
336*0Sstevel@tonic-gate 				*mp->b_rptr = FLUSHR;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 		pckt_msgp = copymsg(mp);
339*0Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
340*0Sstevel@tonic-gate 			/*
341*0Sstevel@tonic-gate 			 * In the packet model we are not allowing
342*0Sstevel@tonic-gate 			 * flushes of the master's stream head read
343*0Sstevel@tonic-gate 			 * side queue. This is because all packet
344*0Sstevel@tonic-gate 			 * state information is stored there and
345*0Sstevel@tonic-gate 			 * a flush could destroy this data before
346*0Sstevel@tonic-gate 			 * it is read.
347*0Sstevel@tonic-gate 			 */
348*0Sstevel@tonic-gate 			*mp->b_rptr = FLUSHW;
349*0Sstevel@tonic-gate 			putnext(q, mp);
350*0Sstevel@tonic-gate 		} else {
351*0Sstevel@tonic-gate 			/*
352*0Sstevel@tonic-gate 			 * Free messages that only flush the
353*0Sstevel@tonic-gate 			 * master's read queue.
354*0Sstevel@tonic-gate 			 */
355*0Sstevel@tonic-gate 			freemsg(mp);
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 		if (pckt_msgp == NULL)
359*0Sstevel@tonic-gate 			break;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 		mp = pckt_msgp;
362*0Sstevel@tonic-gate 		/*
363*0Sstevel@tonic-gate 		 * Prefix M_PROTO and putnext.
364*0Sstevel@tonic-gate 		 */
365*0Sstevel@tonic-gate 		goto prefix_head;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	case M_DATA:
368*0Sstevel@tonic-gate 	case M_IOCTL:
369*0Sstevel@tonic-gate 	case M_PROTO:
370*0Sstevel@tonic-gate 		/*
371*0Sstevel@tonic-gate 		 * For non-priority messages, follow flow-control rules.
372*0Sstevel@tonic-gate 		 * Also, if there are messages on the q already, keep
373*0Sstevel@tonic-gate 		 * queueing them since they need to be processed in order.
374*0Sstevel@tonic-gate 		 */
375*0Sstevel@tonic-gate 		if (!canputnext(q) || (qsize(q) > 0)) {
376*0Sstevel@tonic-gate 			(void) putq(q, mp);
377*0Sstevel@tonic-gate 			break;
378*0Sstevel@tonic-gate 		}
379*0Sstevel@tonic-gate 		/* FALLTHROUGH */
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	/*
382*0Sstevel@tonic-gate 	 * For high priority messages, skip flow control checks.
383*0Sstevel@tonic-gate 	 */
384*0Sstevel@tonic-gate 	case M_PCPROTO:
385*0Sstevel@tonic-gate 	case M_READ:
386*0Sstevel@tonic-gate 	case M_STOP:
387*0Sstevel@tonic-gate 	case M_START:
388*0Sstevel@tonic-gate 	case M_STARTI:
389*0Sstevel@tonic-gate 	case M_STOPI:
390*0Sstevel@tonic-gate prefix_head:
391*0Sstevel@tonic-gate 		/*
392*0Sstevel@tonic-gate 		 * Prefix an M_PROTO header to message and pass upstream.
393*0Sstevel@tonic-gate 		 */
394*0Sstevel@tonic-gate 		if ((mp = add_ctl_info(q, mp)) != NULL)
395*0Sstevel@tonic-gate 			putnext(q, mp);
396*0Sstevel@tonic-gate 		break;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	default:
399*0Sstevel@tonic-gate 		/*
400*0Sstevel@tonic-gate 		 * For data messages, queue them back on the queue if
401*0Sstevel@tonic-gate 		 * there are messages on the queue already. This is
402*0Sstevel@tonic-gate 		 * done to preserve the order of messages.
403*0Sstevel@tonic-gate 		 * For high priority messages or for no messages on the
404*0Sstevel@tonic-gate 		 * q, simply putnext() and pass it on.
405*0Sstevel@tonic-gate 		 */
406*0Sstevel@tonic-gate 		if ((datamsg(mp->b_datap->db_type)) && (qsize(q) > 0))
407*0Sstevel@tonic-gate 			(void) putq(q, mp);
408*0Sstevel@tonic-gate 		else
409*0Sstevel@tonic-gate 			putnext(q, mp);
410*0Sstevel@tonic-gate 		break;
411*0Sstevel@tonic-gate 	}
412*0Sstevel@tonic-gate }
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate /*
415*0Sstevel@tonic-gate  * pcktrsrv - module read service procedure
416*0Sstevel@tonic-gate  * This function deals with messages left in the queue due to
417*0Sstevel@tonic-gate  *	(a) not enough memory to allocate the header M_PROTO message
418*0Sstevel@tonic-gate  *	(b) flow control reasons
419*0Sstevel@tonic-gate  * The function will attempt to get the messages off the queue and
420*0Sstevel@tonic-gate  * process them.
421*0Sstevel@tonic-gate  */
422*0Sstevel@tonic-gate static void
pcktrsrv(queue_t * q)423*0Sstevel@tonic-gate pcktrsrv(queue_t *q)
424*0Sstevel@tonic-gate {
425*0Sstevel@tonic-gate 	mblk_t *mp;
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
428*0Sstevel@tonic-gate 		if (!canputnext(q)) {
429*0Sstevel@tonic-gate 			/*
430*0Sstevel@tonic-gate 			 * For high priority messages, make sure there is no
431*0Sstevel@tonic-gate 			 * infinite loop. Disable the queue for this case.
432*0Sstevel@tonic-gate 			 * High priority messages get here only for buffer
433*0Sstevel@tonic-gate 			 * allocation failures. Thus the bufcall callout
434*0Sstevel@tonic-gate 			 * will reenable the q.
435*0Sstevel@tonic-gate 			 * XXX bug alert - nooenable will *not* prevent
436*0Sstevel@tonic-gate 			 * putbq of a hipri messages frm enabling the queue.
437*0Sstevel@tonic-gate 			 */
438*0Sstevel@tonic-gate 			if (!datamsg(mp->b_datap->db_type))
439*0Sstevel@tonic-gate 				noenable(q);
440*0Sstevel@tonic-gate 			(void) putbq(q, mp);
441*0Sstevel@tonic-gate 			return;
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		/*
445*0Sstevel@tonic-gate 		 * M_FLUSH msgs may also be here if there was a memory
446*0Sstevel@tonic-gate 		 * failure.
447*0Sstevel@tonic-gate 		 */
448*0Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
449*0Sstevel@tonic-gate 		case M_FLUSH:
450*0Sstevel@tonic-gate 		case M_PROTO:
451*0Sstevel@tonic-gate 		case M_PCPROTO:
452*0Sstevel@tonic-gate 		case M_STOP:
453*0Sstevel@tonic-gate 		case M_START:
454*0Sstevel@tonic-gate 		case M_IOCTL:
455*0Sstevel@tonic-gate 		case M_DATA:
456*0Sstevel@tonic-gate 		case M_READ:
457*0Sstevel@tonic-gate 		case M_STARTI:
458*0Sstevel@tonic-gate 		case M_STOPI:
459*0Sstevel@tonic-gate 			/*
460*0Sstevel@tonic-gate 			 * Prefix an M_PROTO header to msg and pass upstream.
461*0Sstevel@tonic-gate 			 */
462*0Sstevel@tonic-gate 			if ((mp = add_ctl_info(q, mp)) == NULL) {
463*0Sstevel@tonic-gate 				/*
464*0Sstevel@tonic-gate 				 * Running into memory or flow ctl problems.
465*0Sstevel@tonic-gate 				 */
466*0Sstevel@tonic-gate 				return;
467*0Sstevel@tonic-gate 			}
468*0Sstevel@tonic-gate 			/* FALL THROUGH */
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		default:
471*0Sstevel@tonic-gate 			putnext(q, mp);
472*0Sstevel@tonic-gate 			break;
473*0Sstevel@tonic-gate 		}
474*0Sstevel@tonic-gate 	}
475*0Sstevel@tonic-gate }
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate /*
478*0Sstevel@tonic-gate  * pcktwput - Module write queue put procedure.
479*0Sstevel@tonic-gate  *	All messages are send downstream unchanged
480*0Sstevel@tonic-gate  */
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate static void
pcktwput(queue_t * q,mblk_t * mp)483*0Sstevel@tonic-gate pcktwput(
484*0Sstevel@tonic-gate 	queue_t *q,	/* Pointer to the read queue */
485*0Sstevel@tonic-gate 	mblk_t *mp)	/* Pointer to current message block */
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	putnext(q, mp);
488*0Sstevel@tonic-gate }
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
491*0Sstevel@tonic-gate /*
492*0Sstevel@tonic-gate  * reallocb - copy the data block from the given message block into a new block.
493*0Sstevel@tonic-gate  * This function is used in case data block had another message block
494*0Sstevel@tonic-gate  * pointing to it (and hence we just copy this one data block).
495*0Sstevel@tonic-gate  *
496*0Sstevel@tonic-gate  * Returns new message block if successful. On failure it returns NULL.
497*0Sstevel@tonic-gate  * It also tries to do a qbufcall and if that also fails,
498*0Sstevel@tonic-gate  * it frees the message block.
499*0Sstevel@tonic-gate  */
500*0Sstevel@tonic-gate static mblk_t *
pckt_reallocb(queue_t * q,mblk_t * mp)501*0Sstevel@tonic-gate pckt_reallocb(
502*0Sstevel@tonic-gate 	queue_t *q,	/* Pointer to the read queue */
503*0Sstevel@tonic-gate 	mblk_t *mp	/* Pointer to the message block to be changed */
504*0Sstevel@tonic-gate )
505*0Sstevel@tonic-gate {
506*0Sstevel@tonic-gate 	mblk_t	*nmp;
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_ref >= 1);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	/*
511*0Sstevel@tonic-gate 	 * No reallocation is needed if there is only one reference
512*0Sstevel@tonic-gate 	 * to this data block.
513*0Sstevel@tonic-gate 	 */
514*0Sstevel@tonic-gate 	if (mp->b_datap->db_ref == 1)
515*0Sstevel@tonic-gate 		return (mp);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	if ((nmp = copyb(mp)) == NULL) {
518*0Sstevel@tonic-gate 		struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		noenable(q);
521*0Sstevel@tonic-gate 		if (pip->pi_bufcall_id = qbufcall(q, mp->b_wptr - mp->b_rptr,
522*0Sstevel@tonic-gate 		    BPRI_MED, add_ctl_wkup, q)) {
523*0Sstevel@tonic-gate 			/*
524*0Sstevel@tonic-gate 			 * Put the message back onto the q.
525*0Sstevel@tonic-gate 			 */
526*0Sstevel@tonic-gate 			(void) putq(q, mp);
527*0Sstevel@tonic-gate 		} else {
528*0Sstevel@tonic-gate 			/*
529*0Sstevel@tonic-gate 			 * Things are pretty bad and serious if bufcall fails!
530*0Sstevel@tonic-gate 			 * Drop the message in this case.
531*0Sstevel@tonic-gate 			 */
532*0Sstevel@tonic-gate 			freemsg(mp);
533*0Sstevel@tonic-gate 		}
534*0Sstevel@tonic-gate 		return ((mblk_t *)0);
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	nmp->b_cont = mp->b_cont;
538*0Sstevel@tonic-gate 	freeb(mp);
539*0Sstevel@tonic-gate 	return (nmp);
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate /*
544*0Sstevel@tonic-gate  * add_ctl_info: add message control information to in coming
545*0Sstevel@tonic-gate  * 	message.
546*0Sstevel@tonic-gate  */
547*0Sstevel@tonic-gate static mblk_t *
add_ctl_info(queue_t * q,mblk_t * mp)548*0Sstevel@tonic-gate add_ctl_info(
549*0Sstevel@tonic-gate 	queue_t *q,		/* pointer to the read queue */
550*0Sstevel@tonic-gate 	mblk_t	*mp)		/* pointer to the raw data input message */
551*0Sstevel@tonic-gate {
552*0Sstevel@tonic-gate 	struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;
553*0Sstevel@tonic-gate 	mblk_t	*bp;		/* pointer to the unmodified message block */
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	/*
556*0Sstevel@tonic-gate 	 * Waiting on space for previous message?
557*0Sstevel@tonic-gate 	 */
558*0Sstevel@tonic-gate 	if (pip->pi_bufcall_id) {
559*0Sstevel@tonic-gate 		/*
560*0Sstevel@tonic-gate 		 * Chain this message on to q for later processing.
561*0Sstevel@tonic-gate 		 */
562*0Sstevel@tonic-gate 		(void) putq(q, mp);
563*0Sstevel@tonic-gate 		return (NULL);
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	/*
567*0Sstevel@tonic-gate 	 * Need to add the message block header as
568*0Sstevel@tonic-gate 	 * an M_PROTO type message.
569*0Sstevel@tonic-gate 	 */
570*0Sstevel@tonic-gate 	if ((bp = allocb(sizeof (char), BPRI_MED)) == (mblk_t *)NULL) {
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 		/*
573*0Sstevel@tonic-gate 		 * There are two reasons to disable the q:
574*0Sstevel@tonic-gate 		 * (1) Flow control reasons should not wake up the q.
575*0Sstevel@tonic-gate 		 * (2) High priority messages will wakeup the q
576*0Sstevel@tonic-gate 		 *	immediately. Disallow this.
577*0Sstevel@tonic-gate 		 */
578*0Sstevel@tonic-gate 		noenable(q);
579*0Sstevel@tonic-gate 		if (pip->pi_bufcall_id = qbufcall(q, sizeof (char), BPRI_MED,
580*0Sstevel@tonic-gate 		    add_ctl_wkup, q)) {
581*0Sstevel@tonic-gate 			/*
582*0Sstevel@tonic-gate 			 * Add the message to the q.
583*0Sstevel@tonic-gate 			 */
584*0Sstevel@tonic-gate 			(void) putq(q, mp);
585*0Sstevel@tonic-gate 		} else {
586*0Sstevel@tonic-gate 			/*
587*0Sstevel@tonic-gate 			 * Things are pretty bad and serious if bufcall fails!
588*0Sstevel@tonic-gate 			 * Drop the message in this case.
589*0Sstevel@tonic-gate 			 */
590*0Sstevel@tonic-gate 			freemsg(mp);
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 		return (NULL);
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/*
597*0Sstevel@tonic-gate 	 * Copy the message type information to this message.
598*0Sstevel@tonic-gate 	 */
599*0Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
600*0Sstevel@tonic-gate 	*(unsigned char *)bp->b_rptr = mp->b_datap->db_type;
601*0Sstevel@tonic-gate 	bp->b_wptr++;
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
604*0Sstevel@tonic-gate 	/*
605*0Sstevel@tonic-gate 	 * Check the datamodel and if the calling program is
606*0Sstevel@tonic-gate 	 * an ILP32 application then we covert the M_IOCTLs and M_READs
607*0Sstevel@tonic-gate 	 * into the native ILP32 format before passing them upstream
608*0Sstevel@tonic-gate 	 * to user mode.
609*0Sstevel@tonic-gate 	 */
610*0Sstevel@tonic-gate 	switch (pip->model) {
611*0Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
612*0Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
613*0Sstevel@tonic-gate 			/*
614*0Sstevel@tonic-gate 			 * This structure must have the same shape as
615*0Sstevel@tonic-gate 			 * the * ILP32 compilation of `struct iocblk'
616*0Sstevel@tonic-gate 			 * from <sys/stream.h>.
617*0Sstevel@tonic-gate 			 */
618*0Sstevel@tonic-gate 			struct iocblk32 {
619*0Sstevel@tonic-gate 				int32_t   	ioc_cmd;
620*0Sstevel@tonic-gate 				caddr32_t	ioc_cr;
621*0Sstevel@tonic-gate 				uint32_t	ioc_id;
622*0Sstevel@tonic-gate 				int32_t   	ioc_count;
623*0Sstevel@tonic-gate 				int32_t   	ioc_error;
624*0Sstevel@tonic-gate 				int32_t   	ioc_rval;
625*0Sstevel@tonic-gate 				int32_t   	ioc_fill1;
626*0Sstevel@tonic-gate 				uint32_t	ioc_flag;
627*0Sstevel@tonic-gate 				int32_t   	ioc_filler[2];
628*0Sstevel@tonic-gate 			} niocblk_32;
629*0Sstevel@tonic-gate 			struct iocblk		*iocblk_64;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 		case M_IOCTL:
632*0Sstevel@tonic-gate 			if ((mp = pckt_reallocb(q, mp)) == (mblk_t *)0)
633*0Sstevel@tonic-gate 				return ((mblk_t *)0);
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 			bzero(&niocblk_32, sizeof (niocblk_32));
636*0Sstevel@tonic-gate 			iocblk_64 = (struct iocblk *)mp->b_rptr;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 			/* Leave the pointer to cred_t structure as it is. */
639*0Sstevel@tonic-gate 			niocblk_32.ioc_cmd = iocblk_64->ioc_cmd;
640*0Sstevel@tonic-gate 			niocblk_32.ioc_cr = (caddr32_t)(uintptr_t)
641*0Sstevel@tonic-gate 			    iocblk_64->ioc_cr;
642*0Sstevel@tonic-gate 			niocblk_32.ioc_id = iocblk_64->ioc_id;
643*0Sstevel@tonic-gate 			niocblk_32.ioc_count = iocblk_64->ioc_count;
644*0Sstevel@tonic-gate 			niocblk_32.ioc_error = iocblk_64->ioc_error;
645*0Sstevel@tonic-gate 			niocblk_32.ioc_rval = iocblk_64->ioc_rval;
646*0Sstevel@tonic-gate 			niocblk_32.ioc_flag = iocblk_64->ioc_flag;
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 			/* Copy the iocblk structure for ILP32 back */
649*0Sstevel@tonic-gate 			*(struct iocblk32 *)mp->b_rptr = niocblk_32;
650*0Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (struct iocblk32);
651*0Sstevel@tonic-gate 			break;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 		case M_READ:
654*0Sstevel@tonic-gate 			if ((mp = pckt_reallocb(q, mp)) == (mblk_t *)0)
655*0Sstevel@tonic-gate 				return ((mblk_t *)0);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 			/* change the size_t to size32_t for ILP32 */
658*0Sstevel@tonic-gate 			*(size32_t *)mp->b_rptr = *(size_t *)mp->b_rptr;
659*0Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (size32_t);
660*0Sstevel@tonic-gate 			break;
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 		break;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	case DATAMODEL_NONE:
665*0Sstevel@tonic-gate 		break;
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	/*
670*0Sstevel@tonic-gate 	 * Now change the orginal message type to M_DATA and tie them up.
671*0Sstevel@tonic-gate 	 */
672*0Sstevel@tonic-gate 	mp->b_datap->db_type = M_DATA;
673*0Sstevel@tonic-gate 	bp->b_cont = mp;
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	return (bp);
676*0Sstevel@tonic-gate }
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate static void
add_ctl_wkup(void * arg)679*0Sstevel@tonic-gate add_ctl_wkup(void *arg)
680*0Sstevel@tonic-gate {
681*0Sstevel@tonic-gate 	queue_t *q = arg;	/* ptr to the read queue */
682*0Sstevel@tonic-gate 	struct pckt_info *pip = (struct pckt_info *)q->q_ptr;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	pip->pi_bufcall_id = 0;
685*0Sstevel@tonic-gate 	/*
686*0Sstevel@tonic-gate 	 * Allow enabling of the q to allow the service
687*0Sstevel@tonic-gate 	 * function to do its job.
688*0Sstevel@tonic-gate 	 *
689*0Sstevel@tonic-gate 	 * Also, qenable() to schedule the q immediately.
690*0Sstevel@tonic-gate 	 * This is to ensure timely processing of high priority
691*0Sstevel@tonic-gate 	 * messages if they are on the q.
692*0Sstevel@tonic-gate 	 */
693*0Sstevel@tonic-gate 	enableok(q);
694*0Sstevel@tonic-gate 	qenable(q);
695*0Sstevel@tonic-gate }
696