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