4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R4 1.4 */
34 * Transport Interface Library read/write module - issue 1
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/stream.h>
40 #include <sys/stropts.h>
41 #include <sys/tihdr.h>
42 #include <sys/debug.h>
43 #include <sys/errno.h>
45 #include <sys/tirdwr.h>
47 #include <sys/modctl.h>
49 #include <sys/sunddi.h>
58 * Per-Stream private data structure.
66 * stream data structure definitions
68 static int tirdwropen(queue_t
*q
, dev_t
*dev
,
69 int flag
, int sflag
, cred_t
*cr
);
71 static int tirdwrclose(queue_t
*q
, int flag
, cred_t
*cr
);
73 static int check_strhead(queue_t
*q
);
76 * To save instructions, since STREAMS ignores the return value
77 * from these functions, they are defined as void here. Kind of icky, but...
79 static void tirdwrrput(queue_t
*q
, mblk_t
*mp
);
80 static void tirdwrwput(queue_t
*q
, mblk_t
*mp
);
82 static struct module_info tirdwr_info
= {
91 static struct qinit tirdwrrinit
= {
92 (int (*)())tirdwrrput
,
101 static struct qinit tirdwrwinit
= {
102 (int (*)())tirdwrwput
,
111 static struct streamtab trwinfo
= {
118 static struct fmodsw fsw
= {
124 static struct modlstrmod modlstrmod
= {
125 &mod_strmodops
, "xport interface rd/wr str mod", &fsw
128 static struct modlinkage modlinkage
= {
129 MODREV_1
, &modlstrmod
, NULL
135 return (mod_install(&modlinkage
));
141 return (mod_remove(&modlinkage
));
145 _info(struct modinfo
*modinfop
)
147 return (mod_info(&modlinkage
, modinfop
));
150 static void send_fatal(queue_t
*q
, mblk_t
*mp
);
151 static void strip_strhead(queue_t
*q
);
155 * tirdwropen - open routine gets called when the
156 * module gets pushed onto the stream.
167 struct trw_trw
*trwptr
;
169 /* check if already open */
175 * Allocate a new trw_trw struct.
177 trwptr
= kmem_alloc(sizeof (struct trw_trw
), KM_SLEEP
);
179 /* initialize data structure */
180 trwptr
->trw_flags
= 0;
182 q
->q_ptr
= (caddr_t
)trwptr
;
183 WR(q
)->q_ptr
= (caddr_t
)trwptr
;
188 (void) strqset(WR(q
), QMAXPSZ
, 0, (uintptr_t)WR(q
)->q_next
->q_maxpsz
);
189 (void) strqset(q
, QMAXPSZ
, 0, (uintptr_t)q
->q_next
->q_maxpsz
);
191 if (!check_strhead(q
)) {
194 kmem_free(trwptr
, sizeof (struct trw_trw
));
204 * tirdwrclose - This routine gets called when the module
205 * gets popped off of the stream.
210 tirdwrclose(queue_t
*q
, int flag
, cred_t
*cr
)
212 struct trw_trw
*trwptr
;
214 union T_primitives
*pptr
;
217 trwptr
= (struct trw_trw
*)q
->q_ptr
;
219 ASSERT(trwptr
!= NULL
);
222 * Send up a T_DISCON_IND if necessary.
224 if ((trwptr
->trw_flags
& ORDREL
) && !(trwptr
->trw_flags
& FATAL
))
225 if (mp
= allocb(sizeof (struct T_discon_req
), BPRI_LO
)) {
226 pptr
= (union T_primitives
*)mp
->b_rptr
;
227 mp
->b_wptr
= mp
->b_rptr
+ sizeof (struct T_ordrel_req
);
228 pptr
->type
= T_ORDREL_REQ
;
229 mp
->b_datap
->db_type
= M_PROTO
;
233 kmem_free(trwptr
, sizeof (struct trw_trw
));
239 * tirdwrrput - Module read queue put procedure.
240 * This is called from the module or
245 tirdwrrput(queue_t
*q
, mblk_t
*mp
)
247 union T_primitives
*pptr
;
248 struct trw_trw
*trwptr
;
251 trwptr
= (struct trw_trw
*)q
->q_ptr
;
253 ASSERT(trwptr
!= NULL
);
255 if ((trwptr
->trw_flags
& FATAL
) && !(trwptr
->trw_flags
& WAITACK
)) {
260 switch (mp
->b_datap
->db_type
) {
272 /* is there enough data to check type */
273 if ((mp
->b_wptr
- mp
->b_rptr
) < sizeof (t_scalar_t
)) {
274 /* malformed message */
278 pptr
= (union T_primitives
*)mp
->b_rptr
;
280 switch (pptr
->type
) {
286 if (msgdsize(mp
) == 0) {
291 tmp
= (mblk_t
*)unlinkb(mp
);
297 trwptr
->trw_flags
|= ORDREL
;
298 mp
->b_datap
->db_type
= M_DATA
;
299 mp
->b_wptr
= mp
->b_rptr
;
304 trwptr
->trw_flags
|= DISCON
;
305 trwptr
->trw_flags
&= ~ORDREL
;
306 if (msgdsize(mp
) != 0) {
307 tmp
= (mblk_t
*)unlinkb(mp
);
310 mp
->b_datap
->db_type
= M_HANGUP
;
311 mp
->b_wptr
= mp
->b_rptr
;
324 * tirdwrwput - Module write queue put procedure.
325 * This is called from the module or
326 * stream head upstream.
329 tirdwrwput(queue_t
*q
, mblk_t
*mp
)
331 struct trw_trw
*trwptr
;
333 trwptr
= (struct trw_trw
*)q
->q_ptr
;
335 ASSERT(trwptr
!= NULL
);
337 if (trwptr
->trw_flags
& FATAL
) {
342 switch (mp
->b_datap
->db_type
) {
360 send_fatal(queue_t
*q
, mblk_t
*mp
)
362 struct trw_trw
*trwptr
;
364 trwptr
= (struct trw_trw
*)q
->q_ptr
;
366 trwptr
->trw_flags
|= FATAL
;
367 mp
->b_datap
->db_type
= M_ERROR
;
368 *mp
->b_datap
->db_base
= EPROTO
;
369 mp
->b_rptr
= mp
->b_datap
->db_base
;
370 mp
->b_wptr
= mp
->b_datap
->db_base
+ sizeof (char);
371 freemsg(unlinkb(mp
));
372 if (q
->q_flag
&QREADR
)
379 check_strhead(queue_t
*q
)
382 union T_primitives
*pptr
;
384 for (mp
= q
->q_next
->q_first
; mp
!= NULL
; mp
= mp
->b_next
) {
386 switch (mp
->b_datap
->db_type
) {
388 pptr
= (union T_primitives
*)mp
->b_rptr
;
389 if ((mp
->b_wptr
- mp
->b_rptr
) < sizeof (t_scalar_t
))
391 switch (pptr
->type
) {
397 (mp
->b_cont
->b_datap
->db_type
!= M_DATA
))
419 strip_strhead(queue_t
*q
)
424 union T_primitives
*pptr
;
428 for (mp
= q
->q_first
; mp
!= NULL
; ) {
430 switch (mp
->b_datap
->db_type
) {
432 pptr
= (union T_primitives
*)mp
->b_rptr
;
433 switch (pptr
->type
) {
436 if (msgdsize(mp
) == 0) {
446 tmp
= (mblk_t
*)unlinkb(mp
);
448 (void) insq(q
, emp
, tmp
);
455 if (msgdsize(mp
) == 0)