xref: /onnv-gate/usr/src/uts/common/io/ib/mgt/ibmf/ibmf_msg.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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file implements the IBMF message related functions.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/ib/mgt/ibmf/ibmf_impl.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate extern int ibmf_trace_level;
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * ibmf_i_client_add_msg():
39*0Sstevel@tonic-gate  *	Add the message to the client message list
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate void
ibmf_i_client_add_msg(ibmf_client_t * clientp,ibmf_msg_impl_t * msgimplp)42*0Sstevel@tonic-gate ibmf_i_client_add_msg(ibmf_client_t *clientp, ibmf_msg_impl_t *msgimplp)
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L4,
45*0Sstevel@tonic-gate 	    ibmf_i_client_add_msg_start, IBMF_TNF_TRACE, "",
46*0Sstevel@tonic-gate 	    "ibmf_i_client_add_msg(): clientp = 0x%p, msgp = 0x%p\n",
47*0Sstevel@tonic-gate 	    tnf_opaque, clientp, clientp, tnf_opaque, msg, msgimplp);
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&msgimplp->im_mutex));
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	mutex_enter(&clientp->ic_msg_mutex);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	/*
54*0Sstevel@tonic-gate 	 * If this is a termination message, add the message to
55*0Sstevel@tonic-gate 	 * the termination message list else add the message
56*0Sstevel@tonic-gate 	 * to the regular message list.
57*0Sstevel@tonic-gate 	 */
58*0Sstevel@tonic-gate 	mutex_enter(&msgimplp->im_mutex);
59*0Sstevel@tonic-gate 	if (msgimplp->im_flags & IBMF_MSG_FLAGS_TERMINATION) {
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		mutex_exit(&msgimplp->im_mutex);
62*0Sstevel@tonic-gate 		/* Put the message on the list */
63*0Sstevel@tonic-gate 		if (clientp->ic_term_msg_list == NULL) {
64*0Sstevel@tonic-gate 			clientp->ic_term_msg_list = clientp->ic_term_msg_last =
65*0Sstevel@tonic-gate 			    msgimplp;
66*0Sstevel@tonic-gate 		} else {
67*0Sstevel@tonic-gate 			msgimplp->im_msg_prev = clientp->ic_term_msg_last;
68*0Sstevel@tonic-gate 			clientp->ic_term_msg_last->im_msg_next = msgimplp;
69*0Sstevel@tonic-gate 			clientp->ic_term_msg_last = msgimplp;
70*0Sstevel@tonic-gate 		}
71*0Sstevel@tonic-gate 	} else {
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 		mutex_exit(&msgimplp->im_mutex);
74*0Sstevel@tonic-gate 		/*
75*0Sstevel@tonic-gate 		 * Increment the counter and kstats for active messages
76*0Sstevel@tonic-gate 		 */
77*0Sstevel@tonic-gate 		clientp->ic_msgs_active++;
78*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
79*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, msgs_active, 1);
80*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 		/* Put the message on the list */
83*0Sstevel@tonic-gate 		if (clientp->ic_msg_list == NULL) {
84*0Sstevel@tonic-gate 			clientp->ic_msg_list = clientp->ic_msg_last = msgimplp;
85*0Sstevel@tonic-gate 		} else {
86*0Sstevel@tonic-gate 			msgimplp->im_msg_prev = clientp->ic_msg_last;
87*0Sstevel@tonic-gate 			clientp->ic_msg_last->im_msg_next = msgimplp;
88*0Sstevel@tonic-gate 			clientp->ic_msg_last = msgimplp;
89*0Sstevel@tonic-gate 		}
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	msgimplp->im_msg_next = NULL;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	/* Set the message flags to indicate the message is on the list */
95*0Sstevel@tonic-gate 	mutex_enter(&msgimplp->im_mutex);
96*0Sstevel@tonic-gate 	msgimplp->im_flags |= IBMF_MSG_FLAGS_ON_LIST;
97*0Sstevel@tonic-gate 	mutex_exit(&msgimplp->im_mutex);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	mutex_exit(&clientp->ic_msg_mutex);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
102*0Sstevel@tonic-gate 	    ibmf_i_client_add_msg_end, IBMF_TNF_TRACE, "",
103*0Sstevel@tonic-gate 	    "ibmf_i_client_add_msg() exit\n");
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * ibmf_i_client_rem_msg():
108*0Sstevel@tonic-gate  *	Remove the message from the client's message list
109*0Sstevel@tonic-gate  *	The refcnt will hold the message reference count at the time
110*0Sstevel@tonic-gate  *	the message was removed from the message list. Any packets
111*0Sstevel@tonic-gate  *	arriving after this point for the message will be dropped.
112*0Sstevel@tonic-gate  *	The message reference count is used by the threads processing
113*0Sstevel@tonic-gate  *	the message to decide which one should notify the client
114*0Sstevel@tonic-gate  *	(the one that decrements the reference count to zero).
115*0Sstevel@tonic-gate  */
116*0Sstevel@tonic-gate void
ibmf_i_client_rem_msg(ibmf_client_t * clientp,ibmf_msg_impl_t * msgimplp,uint_t * refcnt)117*0Sstevel@tonic-gate ibmf_i_client_rem_msg(ibmf_client_t *clientp, ibmf_msg_impl_t *msgimplp,
118*0Sstevel@tonic-gate     uint_t *refcnt)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	ibmf_msg_impl_t *tmpmsg, *prevmsg = NULL;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&msgimplp->im_mutex));
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L4,
125*0Sstevel@tonic-gate 	    ibmf_i_client_rem_msg_start, IBMF_TNF_TRACE, "",
126*0Sstevel@tonic-gate 	    "ibmf_i_client_rem_msg(): clientp = 0x%p, msgp = 0x%p\n",
127*0Sstevel@tonic-gate 	    tnf_opaque, clientp, clientp, tnf_opaque, msg, msgimplp);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	mutex_enter(&clientp->ic_msg_mutex);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	/*
132*0Sstevel@tonic-gate 	 * If this is a termination message, remove the message from
133*0Sstevel@tonic-gate 	 * the termination message list else remove the message
134*0Sstevel@tonic-gate 	 * from the regular message list.
135*0Sstevel@tonic-gate 	 */
136*0Sstevel@tonic-gate 	mutex_enter(&msgimplp->im_mutex);
137*0Sstevel@tonic-gate 	if (msgimplp->im_flags & IBMF_MSG_FLAGS_TERMINATION) {
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 		mutex_exit(&msgimplp->im_mutex);
140*0Sstevel@tonic-gate 		tmpmsg = clientp->ic_term_msg_list;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		while (tmpmsg != NULL) {
143*0Sstevel@tonic-gate 			if (tmpmsg == msgimplp)
144*0Sstevel@tonic-gate 				break;
145*0Sstevel@tonic-gate 			prevmsg = tmpmsg;
146*0Sstevel@tonic-gate 			tmpmsg = tmpmsg->im_msg_next;
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		ASSERT(tmpmsg != NULL);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 		if (tmpmsg->im_msg_next == NULL)
152*0Sstevel@tonic-gate 			clientp->ic_term_msg_last = prevmsg;
153*0Sstevel@tonic-gate 		else
154*0Sstevel@tonic-gate 			tmpmsg->im_msg_next->im_msg_prev = prevmsg;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 		if (prevmsg != NULL)
157*0Sstevel@tonic-gate 			prevmsg->im_msg_next = tmpmsg->im_msg_next;
158*0Sstevel@tonic-gate 		else
159*0Sstevel@tonic-gate 			clientp->ic_term_msg_list = tmpmsg->im_msg_next;
160*0Sstevel@tonic-gate 	} else {
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		mutex_exit(&msgimplp->im_mutex);
163*0Sstevel@tonic-gate 		/*
164*0Sstevel@tonic-gate 		 * Decrement the counter and kstats for active messages
165*0Sstevel@tonic-gate 		 */
166*0Sstevel@tonic-gate 		ASSERT(clientp->ic_msgs_active != 0);
167*0Sstevel@tonic-gate 		clientp->ic_msgs_active--;
168*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
169*0Sstevel@tonic-gate 		IBMF_SUB32_KSTATS(clientp, msgs_active, 1);
170*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		tmpmsg = clientp->ic_msg_list;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		while (tmpmsg != NULL) {
175*0Sstevel@tonic-gate 			if (tmpmsg == msgimplp)
176*0Sstevel@tonic-gate 				break;
177*0Sstevel@tonic-gate 			prevmsg = tmpmsg;
178*0Sstevel@tonic-gate 			tmpmsg = tmpmsg->im_msg_next;
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		ASSERT(tmpmsg != NULL);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 		if (tmpmsg->im_msg_next == NULL)
184*0Sstevel@tonic-gate 			clientp->ic_msg_last = prevmsg;
185*0Sstevel@tonic-gate 		else
186*0Sstevel@tonic-gate 			tmpmsg->im_msg_next->im_msg_prev = prevmsg;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if (prevmsg != NULL)
189*0Sstevel@tonic-gate 			prevmsg->im_msg_next = tmpmsg->im_msg_next;
190*0Sstevel@tonic-gate 		else
191*0Sstevel@tonic-gate 			clientp->ic_msg_list = tmpmsg->im_msg_next;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/* Save away the message reference count and clear the list flag */
195*0Sstevel@tonic-gate 	mutex_enter(&msgimplp->im_mutex);
196*0Sstevel@tonic-gate 	*refcnt = msgimplp->im_ref_count;
197*0Sstevel@tonic-gate 	msgimplp->im_flags &= ~IBMF_MSG_FLAGS_ON_LIST;
198*0Sstevel@tonic-gate 	mutex_exit(&msgimplp->im_mutex);
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	mutex_exit(&clientp->ic_msg_mutex);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
203*0Sstevel@tonic-gate 	    ibmf_i_client_rem_msg_end, IBMF_TNF_TRACE, "",
204*0Sstevel@tonic-gate 	    "ibmf_i_client_rem_msg() exit\n");
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate /*
208*0Sstevel@tonic-gate  * ibmf_i_find_msg():
209*0Sstevel@tonic-gate  *	Walk the client message list for the message corresponding to
210*0Sstevel@tonic-gate  *	the parameters specified
211*0Sstevel@tonic-gate  *	The msg_list parameter should be either IBMF_REG_MSG_LIST
212*0Sstevel@tonic-gate  *	or IBMF_TERM_MSG_LIST for the termination message list.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate ibmf_msg_impl_t *
ibmf_i_find_msg(ibmf_client_t * clientp,uint64_t tid,uint8_t mgt_class,uint8_t r_method,ib_lid_t lid,ib_gid_t * gid,boolean_t gid_pr,ibmf_rmpp_hdr_t * rmpp_hdr,boolean_t msg_list)215*0Sstevel@tonic-gate ibmf_i_find_msg(ibmf_client_t *clientp, uint64_t tid, uint8_t mgt_class,
216*0Sstevel@tonic-gate     uint8_t r_method, ib_lid_t lid, ib_gid_t *gid, boolean_t gid_pr,
217*0Sstevel@tonic-gate     ibmf_rmpp_hdr_t *rmpp_hdr, boolean_t msg_list)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	ibmf_msg_impl_t *msgimplp;
220*0Sstevel@tonic-gate 	ib_gid_t	*ctx_gidp;
221*0Sstevel@tonic-gate 	int		msg_found;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	IBMF_TRACE_5(IBMF_TNF_DEBUG, DPRINT_L4,
224*0Sstevel@tonic-gate 	    ibmf_i_find_msg_start, IBMF_TNF_TRACE, "",
225*0Sstevel@tonic-gate 	    "ibmf_i_find_msg(): clientp = 0x%p, tid = 0x%p, mgmt_class = 0x%x, "
226*0Sstevel@tonic-gate 	    "lid = 0x%x, gidp = 0x%p\n", tnf_opaque, clientp, clientp,
227*0Sstevel@tonic-gate 	    tnf_opaque, tid, tid, tnf_opaque, mgt_class, mgt_class,
228*0Sstevel@tonic-gate 	    tnf_opaque, lid, lid, tnf_opaque, gid, gid);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	msg_found = B_FALSE;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	mutex_enter(&clientp->ic_msg_mutex);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (msg_list == IBMF_REG_MSG_LIST)
235*0Sstevel@tonic-gate 		msgimplp = clientp->ic_msg_list;
236*0Sstevel@tonic-gate 	else
237*0Sstevel@tonic-gate 		msgimplp = clientp->ic_term_msg_list;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/*
240*0Sstevel@tonic-gate 	 * Look for a transaction (message) context that matches the
241*0Sstevel@tonic-gate 	 * transaction ID, gid or lid, and management class of the
242*0Sstevel@tonic-gate 	 * incoming packet.
243*0Sstevel@tonic-gate 	 *
244*0Sstevel@tonic-gate 	 * If the client decides to do a non-rmpp or rmpp send only,
245*0Sstevel@tonic-gate 	 * despite expecting a response, then the response should check
246*0Sstevel@tonic-gate 	 * if the message context for the send still exists.
247*0Sstevel@tonic-gate 	 * If it does, it should be skipped.
248*0Sstevel@tonic-gate 	 */
249*0Sstevel@tonic-gate 	while (msgimplp != NULL) {
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 		if (gid_pr == B_TRUE) {
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 			ctx_gidp = &msgimplp->im_global_addr.ig_sender_gid;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 			/* first match gid */
256*0Sstevel@tonic-gate 			if ((ctx_gidp->gid_prefix != gid->gid_prefix) ||
257*0Sstevel@tonic-gate 			    (ctx_gidp->gid_guid != gid->gid_guid)) {
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 				msgimplp = msgimplp->im_msg_next;
260*0Sstevel@tonic-gate 				continue;
261*0Sstevel@tonic-gate 			}
262*0Sstevel@tonic-gate 		} else  {
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 			IBMF_TRACE_5(IBMF_TNF_DEBUG, DPRINT_L3,
265*0Sstevel@tonic-gate 			    ibmf_i_find_msg, IBMF_TNF_TRACE, "",
266*0Sstevel@tonic-gate 			    "ibmf_i_find_msg(): %s, msgp = 0x%p, tid = 0x%p, "
267*0Sstevel@tonic-gate 			    "remote_lid = 0x%x, mgmt_class = 0x%x\n",
268*0Sstevel@tonic-gate 			    tnf_string, msg, "Comparing to msg",
269*0Sstevel@tonic-gate 			    tnf_opaque, msg, msgimplp,
270*0Sstevel@tonic-gate 			    tnf_opaque, tid, msgimplp->im_tid,
271*0Sstevel@tonic-gate 			    tnf_opaque, remote_lid,
272*0Sstevel@tonic-gate 			    msgimplp->im_local_addr.ia_remote_lid,
273*0Sstevel@tonic-gate 			    tnf_opaque, class, msgimplp->im_mgt_class);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 			/* first match lid */
276*0Sstevel@tonic-gate 			if (msgimplp->im_local_addr.ia_remote_lid != lid) {
277*0Sstevel@tonic-gate 				msgimplp = msgimplp->im_msg_next;
278*0Sstevel@tonic-gate 				continue;
279*0Sstevel@tonic-gate 			}
280*0Sstevel@tonic-gate 		}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 		/* next match tid and class */
283*0Sstevel@tonic-gate 		if ((msgimplp->im_tid != tid) ||
284*0Sstevel@tonic-gate 		    (msgimplp->im_mgt_class != mgt_class)) {
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 			msgimplp = msgimplp->im_msg_next;
287*0Sstevel@tonic-gate 			continue;
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 		/*
291*0Sstevel@tonic-gate 		 * For unsolicited transactions, the message is found
292*0Sstevel@tonic-gate 		 * if the method matches, but,
293*0Sstevel@tonic-gate 		 * If the response is an ACK, and the transaction is
294*0Sstevel@tonic-gate 		 * in RMPP receiver mode, then skip this message.
295*0Sstevel@tonic-gate 		 */
296*0Sstevel@tonic-gate 		if (msgimplp->im_unsolicited == B_TRUE) {
297*0Sstevel@tonic-gate 			ibmf_rmpp_ctx_t *rmpp_ctx;
298*0Sstevel@tonic-gate 			ibmf_msg_bufs_t *msgbufp;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 			mutex_enter(&msgimplp->im_mutex);
301*0Sstevel@tonic-gate 			rmpp_ctx = &msgimplp->im_rmpp_ctx;
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 			if ((msgimplp->im_flags & IBMF_MSG_FLAGS_RECV_RMPP) &&
304*0Sstevel@tonic-gate 			    ((rmpp_ctx->rmpp_state ==
305*0Sstevel@tonic-gate 			    IBMF_RMPP_STATE_RECEVR_ACTIVE) ||
306*0Sstevel@tonic-gate 			    (rmpp_ctx->rmpp_state ==
307*0Sstevel@tonic-gate 			    IBMF_RMPP_STATE_RECEVR_TERMINATE))) {
308*0Sstevel@tonic-gate 				/* Continue if ACK packet */
309*0Sstevel@tonic-gate 				if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ACK) {
310*0Sstevel@tonic-gate 					mutex_exit(&msgimplp->im_mutex);
311*0Sstevel@tonic-gate 					msgimplp = msgimplp->im_msg_next;
312*0Sstevel@tonic-gate 					continue;
313*0Sstevel@tonic-gate 				}
314*0Sstevel@tonic-gate 			}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 			if (msgimplp->im_trans_state_flags ==
317*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_RECV_ACTIVE) {
318*0Sstevel@tonic-gate 				msgbufp = &msgimplp->im_msgbufs_recv;
319*0Sstevel@tonic-gate 				if (msgbufp->im_bufs_mad_hdr->R_Method ==
320*0Sstevel@tonic-gate 				    r_method) {
321*0Sstevel@tonic-gate 					mutex_exit(&msgimplp->im_mutex);
322*0Sstevel@tonic-gate 					msg_found = B_TRUE;
323*0Sstevel@tonic-gate 					break;
324*0Sstevel@tonic-gate 				}
325*0Sstevel@tonic-gate 			}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 			mutex_exit(&msgimplp->im_mutex);
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 		/*
331*0Sstevel@tonic-gate 		 * if this was an unsequenced, non-RMPP transaction there should
332*0Sstevel@tonic-gate 		 * be no incoming packets
333*0Sstevel@tonic-gate 		 */
334*0Sstevel@tonic-gate 		if ((!(msgimplp->im_transp_op_flags &
335*0Sstevel@tonic-gate 		    IBMF_MSG_TRANS_FLAG_RMPP)) &&
336*0Sstevel@tonic-gate 		    (!(msgimplp->im_transp_op_flags &
337*0Sstevel@tonic-gate 		    IBMF_MSG_TRANS_FLAG_SEQ))) {
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 			msgimplp = msgimplp->im_msg_next;
340*0Sstevel@tonic-gate 			continue;
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 		/*
345*0Sstevel@tonic-gate 		 * if this is a sequenced transaction,
346*0Sstevel@tonic-gate 		 * (the send and response may or may not be RMPP)
347*0Sstevel@tonic-gate 		 * and the method of the incoming MAD is the same as the
348*0Sstevel@tonic-gate 		 * method in the send message context with the response bit
349*0Sstevel@tonic-gate 		 * set then this message matches.
350*0Sstevel@tonic-gate 		 */
351*0Sstevel@tonic-gate 		if (msgimplp->im_transp_op_flags & IBMF_MSG_TRANS_FLAG_SEQ) {
352*0Sstevel@tonic-gate 			ibmf_msg_bufs_t *msgbufp;
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 			mutex_enter(&msgimplp->im_mutex);
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 			msgbufp = &msgimplp->im_msgbufs_send;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 			if ((msgbufp->im_bufs_mad_hdr->R_Method |
359*0Sstevel@tonic-gate 			    IBMF_RMPP_METHOD_RESP_BIT) == r_method) {
360*0Sstevel@tonic-gate 				mutex_exit(&msgimplp->im_mutex);
361*0Sstevel@tonic-gate 				msg_found = B_TRUE;
362*0Sstevel@tonic-gate 				break;
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 			mutex_exit(&msgimplp->im_mutex);
366*0Sstevel@tonic-gate 		}
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 		/*
369*0Sstevel@tonic-gate 		 * if this is an RMPP SEND transaction there should only
370*0Sstevel@tonic-gate 		 * be ACK, STOP, and ABORTS RMPP packets.
371*0Sstevel@tonic-gate 		 * The response data packets would have been detected in
372*0Sstevel@tonic-gate 		 * the check above.
373*0Sstevel@tonic-gate 		 */
374*0Sstevel@tonic-gate 		if (msgimplp->im_transp_op_flags & IBMF_MSG_TRANS_FLAG_RMPP) {
375*0Sstevel@tonic-gate 			ibmf_rmpp_ctx_t *rmpp_ctx = &msgimplp->im_rmpp_ctx;
376*0Sstevel@tonic-gate 			ibmf_msg_bufs_t *msgbufp;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 			_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*rmpp_ctx))
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 			if ((rmpp_hdr != NULL) &&
381*0Sstevel@tonic-gate 			    (rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_ACTIVE)) {
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 				/*
384*0Sstevel@tonic-gate 				 * If non-sequenced, then there should be
385*0Sstevel@tonic-gate 				 * no DATA packets incoming for this transaction
386*0Sstevel@tonic-gate 				 */
387*0Sstevel@tonic-gate 				if (!(msgimplp->im_transp_op_flags &
388*0Sstevel@tonic-gate 				    IBMF_MSG_TRANS_FLAG_SEQ)) {
389*0Sstevel@tonic-gate 					/* Continue if DATA packet */
390*0Sstevel@tonic-gate 					if (rmpp_hdr->rmpp_type ==
391*0Sstevel@tonic-gate 					    IBMF_RMPP_TYPE_DATA) {
392*0Sstevel@tonic-gate 						msgimplp =
393*0Sstevel@tonic-gate 						    msgimplp->im_msg_next;
394*0Sstevel@tonic-gate 						continue;
395*0Sstevel@tonic-gate 					}
396*0Sstevel@tonic-gate 				}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 				/* Skip if R_Method does not match */
400*0Sstevel@tonic-gate 				if ((rmpp_ctx->rmpp_state ==
401*0Sstevel@tonic-gate 				    IBMF_RMPP_STATE_SENDER_ACTIVE) ||
402*0Sstevel@tonic-gate 				    (rmpp_ctx->rmpp_state ==
403*0Sstevel@tonic-gate 				    IBMF_RMPP_STATE_SENDER_SWITCH)) {
404*0Sstevel@tonic-gate 					/* Continue if DATA packet */
405*0Sstevel@tonic-gate 					if (rmpp_hdr->rmpp_type ==
406*0Sstevel@tonic-gate 					    IBMF_RMPP_TYPE_DATA) {
407*0Sstevel@tonic-gate 						msgimplp =
408*0Sstevel@tonic-gate 						    msgimplp->im_msg_next;
409*0Sstevel@tonic-gate 						continue;
410*0Sstevel@tonic-gate 					}
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 					/*
413*0Sstevel@tonic-gate 					 * Continue if method does not match
414*0Sstevel@tonic-gate 					 * Ignore response bit during match.
415*0Sstevel@tonic-gate 					 */
416*0Sstevel@tonic-gate 					msgbufp = &msgimplp->im_msgbufs_send;
417*0Sstevel@tonic-gate 					if ((msgbufp->im_bufs_mad_hdr->
418*0Sstevel@tonic-gate 					    R_Method & MAD_METHOD_MASK) !=
419*0Sstevel@tonic-gate 					    (r_method & MAD_METHOD_MASK)) {
420*0Sstevel@tonic-gate 						msgimplp = msgimplp->
421*0Sstevel@tonic-gate 						    im_msg_next;
422*0Sstevel@tonic-gate 						continue;
423*0Sstevel@tonic-gate 					}
424*0Sstevel@tonic-gate 				}
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 				/* Skip if R_Method does not match */
427*0Sstevel@tonic-gate 				if ((rmpp_ctx->rmpp_state ==
428*0Sstevel@tonic-gate 				    IBMF_RMPP_STATE_RECEVR_ACTIVE) ||
429*0Sstevel@tonic-gate 				    (rmpp_ctx->rmpp_state ==
430*0Sstevel@tonic-gate 				    IBMF_RMPP_STATE_RECEVR_TERMINATE)) {
431*0Sstevel@tonic-gate 					/* Continue if ACK packet */
432*0Sstevel@tonic-gate 					if (rmpp_hdr->rmpp_type ==
433*0Sstevel@tonic-gate 					    IBMF_RMPP_TYPE_ACK) {
434*0Sstevel@tonic-gate 						msgimplp =
435*0Sstevel@tonic-gate 						    msgimplp->im_msg_next;
436*0Sstevel@tonic-gate 						continue;
437*0Sstevel@tonic-gate 					}
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 					/* Continue if method does not match */
440*0Sstevel@tonic-gate 					msgbufp = &msgimplp->im_msgbufs_recv;
441*0Sstevel@tonic-gate 					if (msgbufp->im_bufs_mad_hdr->
442*0Sstevel@tonic-gate 					    R_Method != r_method) {
443*0Sstevel@tonic-gate 						msgimplp = msgimplp->
444*0Sstevel@tonic-gate 						    im_msg_next;
445*0Sstevel@tonic-gate 						continue;
446*0Sstevel@tonic-gate 					}
447*0Sstevel@tonic-gate 				}
448*0Sstevel@tonic-gate 			}
449*0Sstevel@tonic-gate 		}
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		/*
452*0Sstevel@tonic-gate 		 * For a sequenced non-RMPP transaction, if the
453*0Sstevel@tonic-gate 		 * TID/LID/MgtClass are the same, and if the method
454*0Sstevel@tonic-gate 		 * of the incoming MAD and the message context are the
455*0Sstevel@tonic-gate 		 * same, then the MAD is likely to be a new request from
456*0Sstevel@tonic-gate 		 * the remote entity, so skip this message.
457*0Sstevel@tonic-gate 		 */
458*0Sstevel@tonic-gate 		if ((msgimplp->im_transp_op_flags & IBMF_MSG_TRANS_FLAG_SEQ) &&
459*0Sstevel@tonic-gate 		    !(msgimplp->im_transp_op_flags &
460*0Sstevel@tonic-gate 		    IBMF_MSG_TRANS_FLAG_RMPP)) {
461*0Sstevel@tonic-gate 			ibmf_msg_bufs_t *msgbufp;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 			mutex_enter(&msgimplp->im_mutex);
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 			msgbufp = &msgimplp->im_msgbufs_send;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 			mutex_exit(&msgimplp->im_mutex);
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 			/* Continue if method is the same */
470*0Sstevel@tonic-gate 			if (msgbufp->im_bufs_mad_hdr->
471*0Sstevel@tonic-gate 			    R_Method == r_method) {
472*0Sstevel@tonic-gate 				msgimplp = msgimplp-> im_msg_next;
473*0Sstevel@tonic-gate 				continue;
474*0Sstevel@tonic-gate 			}
475*0Sstevel@tonic-gate 		}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		/* everything matches, found the correct message */
478*0Sstevel@tonic-gate 		msg_found = B_TRUE;
479*0Sstevel@tonic-gate 		break;
480*0Sstevel@tonic-gate 	}
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	if (msg_found == B_TRUE) {
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 		mutex_enter(&msgimplp->im_mutex);
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		IBMF_MSG_INCR_REFCNT(msgimplp);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3,
489*0Sstevel@tonic-gate 		    ibmf_i_find_msg, IBMF_TNF_TRACE, "",
490*0Sstevel@tonic-gate 		    "ibmf_i_find_msg(): %s, msgp = 0x%p, ref_cnt = 0x%d\n",
491*0Sstevel@tonic-gate 		    tnf_string, msg, "Found message. Inc ref count",
492*0Sstevel@tonic-gate 		    tnf_opaque, msgimplp, msgimplp,
493*0Sstevel@tonic-gate 		    tnf_uint, ref_count, msgimplp->im_ref_count);
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		mutex_exit(&msgimplp->im_mutex);
496*0Sstevel@tonic-gate 	}
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	mutex_exit(&clientp->ic_msg_mutex);
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L4,
501*0Sstevel@tonic-gate 	    ibmf_i_find_msg_end, IBMF_TNF_TRACE, "",
502*0Sstevel@tonic-gate 	    "ibmf_i_find_msg() exit, msgp = 0x%p\n", tnf_opaque, msg, msgimplp);
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	return (msgimplp);
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate /*
508*0Sstevel@tonic-gate  * ibmf_i_find_msg_client():
509*0Sstevel@tonic-gate  *	Walk the client message list to find the specified message
510*0Sstevel@tonic-gate  */
511*0Sstevel@tonic-gate boolean_t
ibmf_i_find_msg_client(ibmf_client_t * clp,ibmf_msg_impl_t * msgimplp,boolean_t inc_refcnt)512*0Sstevel@tonic-gate ibmf_i_find_msg_client(ibmf_client_t *clp, ibmf_msg_impl_t *msgimplp,
513*0Sstevel@tonic-gate     boolean_t inc_refcnt)
514*0Sstevel@tonic-gate {
515*0Sstevel@tonic-gate 	ibmf_msg_impl_t	*msgp;
516*0Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L4,
519*0Sstevel@tonic-gate 	    ibmf_i_find_msg_client_start, IBMF_TNF_TRACE, "",
520*0Sstevel@tonic-gate 	    "ibmf_i_find_msg_client(): clientp = 0x%p, msgp = 0x%p\n",
521*0Sstevel@tonic-gate 	    tnf_opaque, clientp, clp, tnf_opaque, msg, msgimplp);
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	mutex_enter(&clp->ic_msg_mutex);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	msgp = clp->ic_msg_list;
526*0Sstevel@tonic-gate 	while (msgp != NULL) {
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 		if (msgp == msgimplp) {
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 			/* grab the mutex */
531*0Sstevel@tonic-gate 			mutex_enter(&msgimplp->im_mutex);
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 			if (inc_refcnt == B_TRUE)
534*0Sstevel@tonic-gate 				IBMF_MSG_INCR_REFCNT(msgimplp);
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 			IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3,
537*0Sstevel@tonic-gate 			    ibmf_i_find_msg_client, IBMF_TNF_TRACE, "",
538*0Sstevel@tonic-gate 			    "ibmf_i_find_msg_client(): %s, msgp = 0x%p, "
539*0Sstevel@tonic-gate 			    "ref_cnt = 0x%d\n",
540*0Sstevel@tonic-gate 			    tnf_string, msg, "Found message. Inc ref count",
541*0Sstevel@tonic-gate 			    tnf_opaque, msgimplp, msgimplp,
542*0Sstevel@tonic-gate 			    tnf_uint, ref_count, msgimplp->im_ref_count);
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 			mutex_exit(&msgimplp->im_mutex);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 			found = B_TRUE;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 			break;
549*0Sstevel@tonic-gate 		}
550*0Sstevel@tonic-gate 		msgp = msgp->im_msg_next;
551*0Sstevel@tonic-gate 	}
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	/*
554*0Sstevel@tonic-gate 	 * If not found on the regular message list,
555*0Sstevel@tonic-gate 	 * look in the termination list.
556*0Sstevel@tonic-gate 	 */
557*0Sstevel@tonic-gate 	if (found == B_FALSE) {
558*0Sstevel@tonic-gate 		msgp = clp->ic_term_msg_list;
559*0Sstevel@tonic-gate 		while (msgp != NULL) {
560*0Sstevel@tonic-gate 			if (msgp == msgimplp) {
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 				/* grab the mutex */
563*0Sstevel@tonic-gate 				mutex_enter(&msgimplp->im_mutex);
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 				if (inc_refcnt == B_TRUE)
566*0Sstevel@tonic-gate 					IBMF_MSG_INCR_REFCNT(msgimplp);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 				IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3,
569*0Sstevel@tonic-gate 				    ibmf_i_find_msg_client, IBMF_TNF_TRACE, "",
570*0Sstevel@tonic-gate 				    "ibmf_i_find_msg_client(): %s, "
571*0Sstevel@tonic-gate 				    "msgp = 0x%p, ref_cnt = 0x%d\n", tnf_string,
572*0Sstevel@tonic-gate 				    msg, "Found message. Inc ref count",
573*0Sstevel@tonic-gate 				    tnf_opaque, msgimplp, msgimplp, tnf_uint,
574*0Sstevel@tonic-gate 				    ref_count, msgimplp->im_ref_count);
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 				mutex_exit(&msgimplp->im_mutex);
577*0Sstevel@tonic-gate 				found = B_TRUE;
578*0Sstevel@tonic-gate 				break;
579*0Sstevel@tonic-gate 			}
580*0Sstevel@tonic-gate 			msgp = msgp->im_msg_next;
581*0Sstevel@tonic-gate 		}
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	mutex_exit(&clp->ic_msg_mutex);
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
587*0Sstevel@tonic-gate 	    ibmf_i_find_msg_client_end, IBMF_TNF_TRACE, "",
588*0Sstevel@tonic-gate 	    "ibmf_i_find_msg_client() exit\n");
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	return (found);
591*0Sstevel@tonic-gate }
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate /*
594*0Sstevel@tonic-gate  * ibmf_setup_recvbuf_on_error():
595*0Sstevel@tonic-gate  *
596*0Sstevel@tonic-gate  * This function is used to set up the receive buffers to provide
597*0Sstevel@tonic-gate  * a context for sending ABORT MADs in cases where the protocol
598*0Sstevel@tonic-gate  * fails before the receive buffers have been setup. This can happen
599*0Sstevel@tonic-gate  * if the initial receive MAD has a bad version, or an unexpected
600*0Sstevel@tonic-gate  * segment number, for example.
601*0Sstevel@tonic-gate  * We allocate IBMF_MAD_SIZE memory as we only need the information
602*0Sstevel@tonic-gate  * stored in the MAD header and the class header to be able to send
603*0Sstevel@tonic-gate  * the ABORT.
604*0Sstevel@tonic-gate  */
605*0Sstevel@tonic-gate int
ibmf_setup_recvbuf_on_error(ibmf_msg_impl_t * msgimplp,uchar_t * mad)606*0Sstevel@tonic-gate ibmf_setup_recvbuf_on_error(ibmf_msg_impl_t *msgimplp, uchar_t *mad)
607*0Sstevel@tonic-gate {
608*0Sstevel@tonic-gate 	size_t		offset;
609*0Sstevel@tonic-gate 	uint32_t	cl_hdr_sz, cl_hdr_off;
610*0Sstevel@tonic-gate 	ib_mad_hdr_t	*mad_hdr;
611*0Sstevel@tonic-gate 	uchar_t		*msgbufp;
612*0Sstevel@tonic-gate 	ibmf_client_t	*clientp = (ibmf_client_t *)msgimplp->im_client;
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	ASSERT(msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL);
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 	/*
617*0Sstevel@tonic-gate 	 * Allocate enough memory for the MAD headers only.
618*0Sstevel@tonic-gate 	 */
619*0Sstevel@tonic-gate 	msgimplp->im_msgbufs_recv.im_bufs_mad_hdr =
620*0Sstevel@tonic-gate 	    (ib_mad_hdr_t *)kmem_zalloc(IBMF_MAD_SIZE, KM_NOSLEEP);
621*0Sstevel@tonic-gate 	if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
622*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L1,
623*0Sstevel@tonic-gate 		    ibmf_setup_recvbuf_on_error, IBMF_TNF_ERROR, "",
624*0Sstevel@tonic-gate 		    "ibmf_setup_recvbuf_on_error(): %s\n", tnf_string, msg,
625*0Sstevel@tonic-gate 		    "recv buf mem allocation failure");
626*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
627*0Sstevel@tonic-gate 		    ibmf_setup_recvbuf_on_error_end, IBMF_TNF_TRACE, "",
628*0Sstevel@tonic-gate 		    "ibmf_setup_recvbuf_on_error() exit\n");
629*0Sstevel@tonic-gate 		return (IBMF_NO_RESOURCES);
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	mutex_enter(&clientp->ic_kstat_mutex);
633*0Sstevel@tonic-gate 	IBMF_ADD32_KSTATS(clientp, recv_bufs_alloced, 1);
634*0Sstevel@tonic-gate 	mutex_exit(&clientp->ic_kstat_mutex);
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	mad_hdr = (ib_mad_hdr_t *)mad;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	/* Get the class header size and offset */
639*0Sstevel@tonic-gate 	ibmf_i_mgt_class_to_hdr_sz_off(mad_hdr->MgmtClass, &cl_hdr_sz,
640*0Sstevel@tonic-gate 	    &cl_hdr_off);
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	msgbufp = (uchar_t *)msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	/* copy the MAD and class header */
645*0Sstevel@tonic-gate 	bcopy((const void *)mad, (void *)msgbufp,
646*0Sstevel@tonic-gate 	    sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz);
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	/* offset of the class header */
649*0Sstevel@tonic-gate 	offset = sizeof (ib_mad_hdr_t) + cl_hdr_off;
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	/* initialize class header pointer */
652*0Sstevel@tonic-gate 	if (cl_hdr_sz == 0) {
653*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_cl_hdr = NULL;
654*0Sstevel@tonic-gate 	} else {
655*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_cl_hdr =
656*0Sstevel@tonic-gate 		    (void *)(msgbufp + offset);
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	/* Set the class header length */
660*0Sstevel@tonic-gate 	msgimplp->im_msgbufs_recv.im_bufs_cl_hdr_len = cl_hdr_sz;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	/* offset of the class data */
663*0Sstevel@tonic-gate 	offset += cl_hdr_sz;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	/* initialize data area pointer */
666*0Sstevel@tonic-gate 	msgimplp->im_msgbufs_recv.im_bufs_cl_data = (void *)(msgbufp + offset);
667*0Sstevel@tonic-gate 	msgimplp->im_msgbufs_recv.im_bufs_cl_data_len = IBMF_MAD_SIZE -
668*0Sstevel@tonic-gate 	    sizeof (ib_mad_hdr_t) - cl_hdr_off - cl_hdr_sz;
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	return (IBMF_SUCCESS);
671*0Sstevel@tonic-gate }
672