xref: /onnv-gate/usr/src/uts/common/io/ib/mgt/ibmf/ibmf_rmpp.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 2005 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 contains the routines to implement the RMPP protocol.
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 ibmf_state_t *ibmf_statep;
36*0Sstevel@tonic-gate extern int ibmf_trace_level;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #define	IBMF_BUF_PKTS	10
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static void ibmf_i_rmpp_sender_active_flow(ibmf_client_t *clientp,
41*0Sstevel@tonic-gate     ibmf_qp_handle_t qp_hdl, ibmf_msg_impl_t *msgimplp, uchar_t *mad);
42*0Sstevel@tonic-gate static void ibmf_i_rmpp_sender_switch_flow(ibmf_client_t *clientp,
43*0Sstevel@tonic-gate     ibmf_qp_handle_t qp_hdl, ibmf_msg_impl_t *msgimplp, uchar_t *mad);
44*0Sstevel@tonic-gate static void ibmf_i_rmpp_recvr_flow_main(ibmf_client_t *clientp,
45*0Sstevel@tonic-gate     ibmf_qp_handle_t qp_hdl, ibmf_msg_impl_t *msgimplp, uchar_t *mad);
46*0Sstevel@tonic-gate static void ibmf_i_rmpp_recvr_active_flow(ibmf_client_t *clientp,
47*0Sstevel@tonic-gate     ibmf_qp_handle_t qp_hdl, ibmf_msg_impl_t *msgimplp, uchar_t *mad);
48*0Sstevel@tonic-gate static void ibmf_i_rmpp_recvr_term_flow(ibmf_client_t *clientp,
49*0Sstevel@tonic-gate     ibmf_qp_handle_t qp_hdl, ibmf_msg_impl_t *msgimplp, uchar_t *mad);
50*0Sstevel@tonic-gate static boolean_t ibmf_i_is_valid_rmpp_status(ibmf_rmpp_hdr_t *rmpp_hdr);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * ibmf_i_is_rmpp():
54*0Sstevel@tonic-gate  *	Check if the client and QP context supports RMPP transfers
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate boolean_t
ibmf_i_is_rmpp(ibmf_client_t * clientp,ibmf_qp_handle_t ibmf_qp_handle)57*0Sstevel@tonic-gate ibmf_i_is_rmpp(ibmf_client_t *clientp, ibmf_qp_handle_t ibmf_qp_handle)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	ibmf_alt_qp_t	*qpp = (ibmf_alt_qp_t *)ibmf_qp_handle;
60*0Sstevel@tonic-gate 	boolean_t	is_rmpp;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L4, ibmf_i_is_rmpp_start,
63*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_is_rmpp(): clientp = %p, "
64*0Sstevel@tonic-gate 	    "ibmf_qp_handle = 0x%p\n", tnf_opaque, clientp, clientp,
65*0Sstevel@tonic-gate 	    tnf_opaque, ibmf_qp_handle, ibmf_qp_handle);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	if ((clientp->ic_reg_flags & IBMF_REG_FLAG_RMPP) == 0) {
68*0Sstevel@tonic-gate 		is_rmpp = B_FALSE;
69*0Sstevel@tonic-gate 	} else if ((ibmf_qp_handle != IBMF_QP_HANDLE_DEFAULT) &&
70*0Sstevel@tonic-gate 	    (qpp->isq_supports_rmpp == B_FALSE)) {
71*0Sstevel@tonic-gate 		is_rmpp = B_FALSE;
72*0Sstevel@tonic-gate 	} else {
73*0Sstevel@tonic-gate 		is_rmpp = B_TRUE;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L4, ibmf_i_is_rmpp_end,
77*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_is_rmpp() exit, is_rmpp = %d\n",
78*0Sstevel@tonic-gate 	    tnf_uint, is_rmpp, is_rmpp);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	return (is_rmpp);
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * ibmf_i_rmpp_sender_active_flow():
85*0Sstevel@tonic-gate  *	Perform RMPP processing for the sender side transaction.
86*0Sstevel@tonic-gate  *	Refer to figure 178 "RMPP Sender Main Flow Diagram" of
87*0Sstevel@tonic-gate  *	the InfiniBand Architecture Specification Volume 1, Release 1.1
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate static void
ibmf_i_rmpp_sender_active_flow(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * mad)90*0Sstevel@tonic-gate ibmf_i_rmpp_sender_active_flow(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
91*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *mad)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
94*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
95*0Sstevel@tonic-gate 	uint32_t	abort_status;
96*0Sstevel@tonic-gate 	int		status;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
99*0Sstevel@tonic-gate 	    ibmf_i_rmpp_sender_active_flow_start, IBMF_TNF_TRACE, "",
100*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_sender_active_flow(): clientp = 0x%p, qp_hdl = 0x%p, "
101*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
102*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
103*0Sstevel@tonic-gate 	    tnf_opaque, mad, mad);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/*
106*0Sstevel@tonic-gate 	 * RMPP header is located just after the MAD header for SA MADs
107*0Sstevel@tonic-gate 	 * If this changes for Vendor MADs, we will need some way for
108*0Sstevel@tonic-gate 	 * the client to specify the byte offset of the RMPP header
109*0Sstevel@tonic-gate 	 * within the MAD.
110*0Sstevel@tonic-gate 	 */
111*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(mad + sizeof (ib_mad_hdr_t));
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_DATA) {
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
116*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
117*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow(): %s\n", tnf_string, msg,
118*0Sstevel@tonic-gate 		    "Data packet received, discarding it");
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		/*
121*0Sstevel@tonic-gate 		 * According to the IB spec, we discard the packet and resend
122*0Sstevel@tonic-gate 		 * packets next_seg->window_last.  However, next_seg is equal to
123*0Sstevel@tonic-gate 		 * window_last so send_rmpp_window() will just reset the timer.
124*0Sstevel@tonic-gate 		 */
125*0Sstevel@tonic-gate 		ibmf_i_send_rmpp_window(msgimplp, IBMF_NO_BLOCK);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
128*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
129*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		return;
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type != IBMF_RMPP_TYPE_ACK) {
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		if ((rmpp_hdr->rmpp_type != IBMF_RMPP_TYPE_STOP) &&
137*0Sstevel@tonic-gate 		    (rmpp_hdr->rmpp_type != IBMF_RMPP_TYPE_ABORT)) {
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
140*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
141*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow(): %s\n",
142*0Sstevel@tonic-gate 			    tnf_string, msg,
143*0Sstevel@tonic-gate 			    "Unrecognized packet received, sending ABORT");
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 			/* abort with status BadT */
146*0Sstevel@tonic-gate 			status = ibmf_i_send_rmpp(msgimplp,
147*0Sstevel@tonic-gate 			    IBMF_RMPP_TYPE_ABORT, IBMF_RMPP_STATUS_BADT,
148*0Sstevel@tonic-gate 			    0, 0, IBMF_NO_BLOCK);
149*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
150*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
151*0Sstevel@tonic-gate 				    ibmf_i_rmpp_sender_active_flow,
152*0Sstevel@tonic-gate 				    IBMF_TNF_TRACE, "",
153*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_sender_active_flow(): %s\n",
154*0Sstevel@tonic-gate 				    tnf_string, msg, "RMPP ABORT send failed");
155*0Sstevel@tonic-gate 				msgimplp->im_trans_state_flags |=
156*0Sstevel@tonic-gate 				    IBMF_TRANS_STATE_FLAG_SEND_DONE;
157*0Sstevel@tonic-gate 			}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 			mutex_enter(&clientp->ic_kstat_mutex);
160*0Sstevel@tonic-gate 			IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
161*0Sstevel@tonic-gate 			mutex_exit(&clientp->ic_kstat_mutex);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		} else {
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 			abort_status = rmpp_hdr->rmpp_status;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L2,
168*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
169*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow(): %s, "
170*0Sstevel@tonic-gate 			    "status = %d\n", tnf_string, msg,
171*0Sstevel@tonic-gate 			    "STOP or ABORT packet received, terminating",
172*0Sstevel@tonic-gate 			    tnf_uint, abort_status, abort_status);
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
176*0Sstevel@tonic-gate 		msgimplp->im_trans_state_flags |=
177*0Sstevel@tonic-gate 		    IBMF_TRANS_STATE_FLAG_SEND_DONE;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout, msgimplp,
180*0Sstevel@tonic-gate 		    IBMF_RESP_TIMER);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
183*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
184*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		return;
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	IBMF_TRACE_5(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_rmpp_sender_active_flow,
190*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_rmpp_sender_active_flow(): %s, "
191*0Sstevel@tonic-gate 	    "msgp = 0x%p, recvd seg = %d wl = %d wf = %d\n",
192*0Sstevel@tonic-gate 	    tnf_string, msg, "ACK packet received",
193*0Sstevel@tonic-gate 	    tnf_opaque, msgp, msgimplp, tnf_uint, recvd_seg,
194*0Sstevel@tonic-gate 	    b2h32(rmpp_hdr->rmpp_segnum), tnf_uint, wl, rmpp_ctx->rmpp_wl,
195*0Sstevel@tonic-gate 	    tnf_uint, wf, rmpp_ctx->rmpp_wf);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/* only ACK packets get here */
199*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_segnum) > rmpp_ctx->rmpp_wl) {
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		/* abort with status S2B */
202*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
203*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_S2B, 0, 0, IBMF_NO_BLOCK);
204*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
205*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
206*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
207*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow(): %s\n",
208*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ABORT send failed");
209*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
210*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
214*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
215*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
218*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
219*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow(): %s\n",
220*0Sstevel@tonic-gate 		    tnf_string, msg, "Segnum > WL");
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
225*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
228*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
229*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		return;
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_segnum) < rmpp_ctx->rmpp_wf) {
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
237*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
238*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow(): %s\n",
239*0Sstevel@tonic-gate 		    tnf_string, msg, "Segnum < WF");
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 		/* discard the packet by not processing it here */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 		/* send the window */
244*0Sstevel@tonic-gate 		ibmf_i_send_rmpp_window(msgimplp, IBMF_NO_BLOCK);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
247*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
248*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		return;
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	/* only ACK packets with valid segnum get here */
254*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_pyldlen_nwl) < rmpp_ctx->rmpp_wl) {
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
257*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
258*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow(): %s\n",
259*0Sstevel@tonic-gate 		    tnf_string, msg, "NWL < WL");
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 		/* abort with status W2S */
262*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
263*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_W2S, 0, 0, IBMF_NO_BLOCK);
264*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
265*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
266*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
267*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow(): %s\n",
268*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ABORT send failed");
269*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
270*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
274*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
275*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
280*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
283*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
284*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 		return;
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	/* is ACK of last packet */
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_segnum) == rmpp_ctx->rmpp_num_pkts) {
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3,
294*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
295*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow(): %s, msgp = 0x%p, "
296*0Sstevel@tonic-gate 		    "double-sided = %d\n", tnf_string, msg, "Last packet",
297*0Sstevel@tonic-gate 		    tnf_opaque, msgimplp, msgimplp,
298*0Sstevel@tonic-gate 		    tnf_opaque, double_sided, rmpp_ctx->rmpp_is_ds);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_is_ds) {
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
303*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow, IBMF_TNF_TRACE, "",
304*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow(): %s, "
305*0Sstevel@tonic-gate 			    "msgp = 0x%p\n", tnf_string, msg,
306*0Sstevel@tonic-gate 			    "Doublesided,sending ACK and switching to receiver",
307*0Sstevel@tonic-gate 			    tnf_opaque, msgimplp, msgimplp);
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_is_ds = B_FALSE;
310*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_SENDER_SWITCH;
311*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_wf = 1;
312*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_wl = 1;
313*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_es = 1;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 			(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
316*0Sstevel@tonic-gate 			    IBMF_RMPP_STATUS_NORMAL, 0, 1, IBMF_NO_BLOCK);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 			/* set the response timer */
319*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_send_timeout,
320*0Sstevel@tonic-gate 			    msgimplp, IBMF_RESP_TIMER);
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 			/* proceed with sender switch to receiver */
323*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
324*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_active_flow_end,
325*0Sstevel@tonic-gate 			    IBMF_TNF_TRACE, "",
326*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_active_flow() exit\n");
327*0Sstevel@tonic-gate 			return;
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 		/* successful termination */
331*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_DONE;
332*0Sstevel@tonic-gate 		ibmf_i_terminate_transaction(clientp, msgimplp, IBMF_SUCCESS);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
335*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
336*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_active_flow() exit\n");
337*0Sstevel@tonic-gate 		return;
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/* update RMPP context and send the next window */
341*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_wf = b2h32(rmpp_hdr->rmpp_segnum) + 1;
342*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_ns = b2h32(rmpp_hdr->rmpp_segnum) + 1;
343*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_wl =
344*0Sstevel@tonic-gate 	    (rmpp_ctx->rmpp_num_pkts < b2h32(rmpp_hdr->rmpp_pyldlen_nwl)) ?
345*0Sstevel@tonic-gate 	    rmpp_ctx->rmpp_num_pkts : b2h32(rmpp_hdr->rmpp_pyldlen_nwl);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_rmpp_sender_active_flow,
348*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_rmpp_sender_active_flow(): %s, "
349*0Sstevel@tonic-gate 	    "wf = %d, wl = %d, ns = %d\n",
350*0Sstevel@tonic-gate 	    tnf_string, msg, "sending next window",
351*0Sstevel@tonic-gate 	    tnf_uint, wf, rmpp_ctx->rmpp_wf, tnf_uint, wl, rmpp_ctx->rmpp_wl,
352*0Sstevel@tonic-gate 	    tnf_uint, ns, rmpp_ctx->rmpp_ns);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	/* send the window */
355*0Sstevel@tonic-gate 	ibmf_i_send_rmpp_window(msgimplp, IBMF_NO_BLOCK);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/* carry on with the protocol */
358*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
359*0Sstevel@tonic-gate 	    ibmf_i_rmpp_sender_active_flow_end, IBMF_TNF_TRACE, "",
360*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_sender_active_flow() exit\n");
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate  * ibmf_i_rmpp_sender_switch_flow():
365*0Sstevel@tonic-gate  *	Perform sender to receiver flow processing switch.
366*0Sstevel@tonic-gate  *	Refer to figure 179 "RMPP Sender Direction Switch Flow Diagram" of
367*0Sstevel@tonic-gate  *	the InfiniBand Architecture Specification Volume 1, Release 1.1
368*0Sstevel@tonic-gate  */
369*0Sstevel@tonic-gate static void
ibmf_i_rmpp_sender_switch_flow(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * mad)370*0Sstevel@tonic-gate ibmf_i_rmpp_sender_switch_flow(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
371*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *mad)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
374*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
375*0Sstevel@tonic-gate 	int		status;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
378*0Sstevel@tonic-gate 	    ibmf_i_rmpp_sender_switch_flow_start, IBMF_TNF_TRACE, "",
379*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_sender_switch_flow(): clientp = 0x%p, qp_hdl = 0x%p, "
380*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
381*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
382*0Sstevel@tonic-gate 	    tnf_opaque, mad, mad);
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(mad + sizeof (ib_mad_hdr_t));
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ACK) {
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
389*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_switch_flow, IBMF_TNF_TRACE, "",
390*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_switch_flow(): %s\n", tnf_string, msg,
391*0Sstevel@tonic-gate 		    "ACK packet received, sending ACK");
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
394*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_NORMAL, 0, 1, IBMF_NO_BLOCK);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		/* set the response timer */
397*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
398*0Sstevel@tonic-gate 		    IBMF_RESP_TIMER);
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	} else if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_DATA) {
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
403*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_switch_flow, IBMF_TNF_TRACE, "",
404*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_switch_flow(): %s\n", tnf_string, msg,
405*0Sstevel@tonic-gate 		    "DATA packet received, processing packet");
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 		msgimplp->im_flags |= IBMF_MSG_FLAGS_RECV_RMPP;
408*0Sstevel@tonic-gate 		ibmf_i_rmpp_recvr_flow_main(clientp, qp_hdl, msgimplp, mad);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	} else {
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
413*0Sstevel@tonic-gate 		    ibmf_i_rmpp_sender_switch_flow, IBMF_TNF_TRACE, "",
414*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_sender_switch_flow(): %s\n", tnf_string, msg,
415*0Sstevel@tonic-gate 		    "Unexpected packet received, sending ABORT BADT");
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		/* abort with status BadT */
418*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
419*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_BADT, 0, 0, IBMF_NO_BLOCK);
420*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
421*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
422*0Sstevel@tonic-gate 			    ibmf_i_rmpp_sender_switch_flow, IBMF_TNF_TRACE, "",
423*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_sender_switch_flow(): %s\n",
424*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ABORT send failed");
425*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
426*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
430*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
431*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
436*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
440*0Sstevel@tonic-gate 	    ibmf_i_rmpp_sender_switch_flow_end, IBMF_TNF_TRACE, "",
441*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_sender_switch_flow() exit\n");
442*0Sstevel@tonic-gate }
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate /*
445*0Sstevel@tonic-gate  * ibmf_i_rmpp_recvr_flow_main():
446*0Sstevel@tonic-gate  *	Perform RMPP receiver flow processing.
447*0Sstevel@tonic-gate  *	Refer to figure 176 "RMPP Receiver Main Flow Diagram" of
448*0Sstevel@tonic-gate  *	the InfiniBand Architecture Specification Volume 1, Release 1.1
449*0Sstevel@tonic-gate  */
450*0Sstevel@tonic-gate static void
ibmf_i_rmpp_recvr_flow_main(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * mad)451*0Sstevel@tonic-gate ibmf_i_rmpp_recvr_flow_main(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
452*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *mad)
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
455*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
456*0Sstevel@tonic-gate 	ib_mad_hdr_t	*mad_hdr;
457*0Sstevel@tonic-gate 	uchar_t		*msgbufp;
458*0Sstevel@tonic-gate 	uchar_t		*datap;
459*0Sstevel@tonic-gate 	uint32_t	data_sz, offset, num_pkts;
460*0Sstevel@tonic-gate 	uint32_t	cl_hdr_sz, cl_data_sz, cl_hdr_off, cl_hdrdata_sz;
461*0Sstevel@tonic-gate 	size_t		buf_sz;
462*0Sstevel@tonic-gate 	int		status;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
465*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_flow_main_start, IBMF_TNF_TRACE, "",
466*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_flow_main(): clientp = 0x%p, qp_hdl = 0x%p, "
467*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
468*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
469*0Sstevel@tonic-gate 	    tnf_opaque, mad, mad);
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(mad + sizeof (ib_mad_hdr_t));
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_rmpp_recvr_flow_main,
474*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_rmpp_recvr_flow_main(): "
475*0Sstevel@tonic-gate 	    "segnum = %d, es = %d, wl = %d\n", tnf_uint, segnum,
476*0Sstevel@tonic-gate 	    b2h32(rmpp_hdr->rmpp_segnum), tnf_uint, es, rmpp_ctx->rmpp_es,
477*0Sstevel@tonic-gate 	    tnf_uint, wl, rmpp_ctx->rmpp_wl);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	/*
480*0Sstevel@tonic-gate 	 * check that this is the segment we expected;
481*0Sstevel@tonic-gate 	 * assume this check will succeed for the first segment since we cannot
482*0Sstevel@tonic-gate 	 * send an ACK if we haven't allocated the rmpp context yet
483*0Sstevel@tonic-gate 	 */
484*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_segnum) != rmpp_ctx->rmpp_es) {
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
487*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
488*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s\n", tnf_string, msg,
489*0Sstevel@tonic-gate 		    "Unexpected segment number, discarding packet");
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 		/* discard this packet by not processing it here */
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		/*
494*0Sstevel@tonic-gate 		 * If the receive buffer is not yet allocated, this is
495*0Sstevel@tonic-gate 		 * probably the first MAD received for the receive context.
496*0Sstevel@tonic-gate 		 * We need to set up the receive buffer before calling
497*0Sstevel@tonic-gate 		 * ibmf_i_send_rmpp() to send an ACK packet.
498*0Sstevel@tonic-gate 		 */
499*0Sstevel@tonic-gate 		if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
500*0Sstevel@tonic-gate 			status = ibmf_setup_recvbuf_on_error(msgimplp, mad);
501*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
502*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L2,
503*0Sstevel@tonic-gate 				    ibmf_i_rmpp_recvr_flow_main_err,
504*0Sstevel@tonic-gate 				    IBMF_TNF_ERROR, "",
505*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
506*0Sstevel@tonic-gate 				    tnf_string, msg,
507*0Sstevel@tonic-gate 				    "ibmf_setup_recvbuf_on_error() failed");
508*0Sstevel@tonic-gate 				return;
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 		/* send an ACK of ES - 1 if ES is greater than 1 */
513*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_es > 1) {
514*0Sstevel@tonic-gate 			(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
515*0Sstevel@tonic-gate 			    IBMF_RMPP_STATUS_NORMAL, rmpp_ctx->rmpp_es - 1,
516*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_es - 1 + IBMF_RMPP_DEFAULT_WIN_SZ,
517*0Sstevel@tonic-gate 			    IBMF_NO_BLOCK);
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		/*
521*0Sstevel@tonic-gate 		 * reset the timer if we're still waiting for the first seg;
522*0Sstevel@tonic-gate 		 * this is the same timer that is normally set in send_compl
523*0Sstevel@tonic-gate 		 * NOTE: this should be in the IB spec's flowchart but isn't
524*0Sstevel@tonic-gate 		 */
525*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_es == 1) {
526*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
527*0Sstevel@tonic-gate 			    IBMF_RESP_TIMER);
528*0Sstevel@tonic-gate 		}
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
531*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE, "",
532*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main() exit\n");
533*0Sstevel@tonic-gate 		return;
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	mad_hdr = (ib_mad_hdr_t *)mad;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	ibmf_i_mgt_class_to_hdr_sz_off(mad_hdr->MgmtClass, &cl_hdr_sz,
539*0Sstevel@tonic-gate 	    &cl_hdr_off);
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	if ((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_FIRST_PKT) ||
542*0Sstevel@tonic-gate 	    (b2h32(rmpp_hdr->rmpp_segnum) == 1)) {
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 		/* first packet flag should be set and seg num should be 1 */
545*0Sstevel@tonic-gate 		if (((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_FIRST_PKT) == 0) ||
546*0Sstevel@tonic-gate 		    (b2h32(rmpp_hdr->rmpp_segnum) != 1)) {
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 			/*
549*0Sstevel@tonic-gate 			 * If the receive buffer is not yet allocated, this is
550*0Sstevel@tonic-gate 			 * probably the first MAD received for the receive ctx.
551*0Sstevel@tonic-gate 			 * We need to set up the receive buffer before calling
552*0Sstevel@tonic-gate 			 * ibmf_i_send_rmpp() to send an ABORT packet.
553*0Sstevel@tonic-gate 			 */
554*0Sstevel@tonic-gate 			if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
555*0Sstevel@tonic-gate 				status = ibmf_setup_recvbuf_on_error(msgimplp,
556*0Sstevel@tonic-gate 				    mad);
557*0Sstevel@tonic-gate 				if (status != IBMF_SUCCESS) {
558*0Sstevel@tonic-gate 					IBMF_TRACE_1(IBMF_TNF_NODEBUG,
559*0Sstevel@tonic-gate 					    DPRINT_L2,
560*0Sstevel@tonic-gate 					    ibmf_i_rmpp_recvr_flow_main_err,
561*0Sstevel@tonic-gate 					    IBMF_TNF_ERROR, "",
562*0Sstevel@tonic-gate 					    "ibmf_i_rmpp_recvr_flow_main(): "
563*0Sstevel@tonic-gate 					    "%s\n", tnf_string, msg,
564*0Sstevel@tonic-gate 					    "ibmf_setup_recvbuf_on_error() "
565*0Sstevel@tonic-gate 					    "failed");
566*0Sstevel@tonic-gate 					return;
567*0Sstevel@tonic-gate 				}
568*0Sstevel@tonic-gate 			}
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 			/* abort with status BadT */
571*0Sstevel@tonic-gate 			status = ibmf_i_send_rmpp(msgimplp,
572*0Sstevel@tonic-gate 			    IBMF_RMPP_TYPE_ABORT, IBMF_RMPP_STATUS_IFSN,
573*0Sstevel@tonic-gate 			    0, 0, IBMF_NO_BLOCK);
574*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
575*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
576*0Sstevel@tonic-gate 				    ibmf_i_rmpp_recvr_flow_main,
577*0Sstevel@tonic-gate 				    IBMF_TNF_TRACE, "",
578*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
579*0Sstevel@tonic-gate 				    tnf_string, msg, "RMPP ABORT send failed");
580*0Sstevel@tonic-gate 				msgimplp->im_trans_state_flags |=
581*0Sstevel@tonic-gate 				    IBMF_TRANS_STATE_FLAG_SEND_DONE;
582*0Sstevel@tonic-gate 			}
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 			mutex_enter(&clientp->ic_kstat_mutex);
585*0Sstevel@tonic-gate 			IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
586*0Sstevel@tonic-gate 			mutex_exit(&clientp->ic_kstat_mutex);
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
589*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
590*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
591*0Sstevel@tonic-gate 			    tnf_string, msg, "Inconsistent first and segment "
592*0Sstevel@tonic-gate 			    "number detected, sending ABORT IFSN");
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 			ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
599*0Sstevel@tonic-gate 			    msgimplp, IBMF_RESP_TIMER);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
602*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE,
603*0Sstevel@tonic-gate 			    "", "ibmf_i_rmpp_recvr_flow_main() exit\n");
604*0Sstevel@tonic-gate 			return;
605*0Sstevel@tonic-gate 		}
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
608*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
609*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s\n", tnf_string, msg,
610*0Sstevel@tonic-gate 		    "Segment number 1 received:", tnf_opaque, msgp, msgimplp);
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		cl_data_sz = MAD_SIZE_IN_BYTES -
613*0Sstevel@tonic-gate 		    sizeof (ib_mad_hdr_t) - cl_hdr_off - cl_hdr_sz;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		cl_hdrdata_sz = MAD_SIZE_IN_BYTES -
616*0Sstevel@tonic-gate 		    sizeof (ib_mad_hdr_t) - cl_hdr_off;
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 		/*
619*0Sstevel@tonic-gate 		 * Calculate the number of packets by dividing the payload
620*0Sstevel@tonic-gate 		 * length in the RMPP header by the payload size for
621*0Sstevel@tonic-gate 		 * a single packet of that management class (including the
622*0Sstevel@tonic-gate 		 * class header).
623*0Sstevel@tonic-gate 		 */
624*0Sstevel@tonic-gate 		buf_sz = b2h32(rmpp_hdr->rmpp_pyldlen_nwl);
625*0Sstevel@tonic-gate 		if ((buf_sz % cl_hdrdata_sz) != 0)
626*0Sstevel@tonic-gate 			num_pkts = (buf_sz / cl_hdrdata_sz) + 1;
627*0Sstevel@tonic-gate 		else {
628*0Sstevel@tonic-gate 			if (buf_sz > 0)
629*0Sstevel@tonic-gate 				num_pkts = buf_sz / cl_hdrdata_sz;
630*0Sstevel@tonic-gate 			else
631*0Sstevel@tonic-gate 				num_pkts = 1;
632*0Sstevel@tonic-gate 		}
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 		/*
635*0Sstevel@tonic-gate 		 * If the payload length of the message is not specified
636*0Sstevel@tonic-gate 		 * in the first packet's RMPP header, we create a
637*0Sstevel@tonic-gate 		 * temporary receive buffer with space for data payloads
638*0Sstevel@tonic-gate 		 * of IBMF_BUF_PKTS packets. If the number of packets
639*0Sstevel@tonic-gate 		 * received exceeds the capacity in the receive buffer,
640*0Sstevel@tonic-gate 		 * the temporary receive buffer will be freed up, and
641*0Sstevel@tonic-gate 		 * a larger temporary receive buffer will be allocated.
642*0Sstevel@tonic-gate 		 * When the last packet is received, the final receive
643*0Sstevel@tonic-gate 		 * buffer will be allocated with the real size of the message.
644*0Sstevel@tonic-gate 		 * The data will be copied from the old buffer to the new
645*0Sstevel@tonic-gate 		 * buffer.
646*0Sstevel@tonic-gate 		 */
647*0Sstevel@tonic-gate 		if (b2h32(rmpp_hdr->rmpp_pyldlen_nwl) != 0) {
648*0Sstevel@tonic-gate 			/*
649*0Sstevel@tonic-gate 			 * rmpp_pyld_len is the total length of just the
650*0Sstevel@tonic-gate 			 * class data. Class headers from each packet are
651*0Sstevel@tonic-gate 			 * not included in this calculation.
652*0Sstevel@tonic-gate 			 */
653*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_data_len =
654*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len =
655*0Sstevel@tonic-gate 			    b2h32(rmpp_hdr->rmpp_pyldlen_nwl) -
656*0Sstevel@tonic-gate 			    (num_pkts * cl_hdr_sz);
657*0Sstevel@tonic-gate 		} else {
658*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_data_len =
659*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len =
660*0Sstevel@tonic-gate 			    IBMF_BUF_PKTS * cl_data_sz;
661*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_flags |= IBMF_CTX_RMPP_FLAGS_DYN_PYLD;
662*0Sstevel@tonic-gate 		}
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 		ASSERT(msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL);
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 		/* allocate memory for the message data */
667*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_mad_hdr =
668*0Sstevel@tonic-gate 		    (ib_mad_hdr_t *)kmem_zalloc(sizeof (ib_mad_hdr_t) +
669*0Sstevel@tonic-gate 		    cl_hdr_off + cl_hdr_sz + rmpp_ctx->rmpp_pyld_len,
670*0Sstevel@tonic-gate 		    KM_NOSLEEP);
671*0Sstevel@tonic-gate 		if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
674*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
675*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
676*0Sstevel@tonic-gate 			    tnf_string, msg,
677*0Sstevel@tonic-gate 			    "mem allocation failure (known rmpp payload)");
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 			ibmf_i_terminate_transaction(
680*0Sstevel@tonic-gate 			    msgimplp->im_client, msgimplp,
681*0Sstevel@tonic-gate 			    IBMF_NO_MEMORY);
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
684*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE,
685*0Sstevel@tonic-gate 			    "", "ibmf_i_rmpp_recvr_flow_main() exit\n");
686*0Sstevel@tonic-gate 			return;
687*0Sstevel@tonic-gate 		}
688*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
689*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, recv_bufs_alloced, 1);
690*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 		msgbufp = (uchar_t *)msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 		/* copy the MAD and class header */
695*0Sstevel@tonic-gate 		bcopy((const void *)mad, (void *)msgbufp,
696*0Sstevel@tonic-gate 		    sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz);
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 		offset = sizeof (ib_mad_hdr_t) + cl_hdr_off;
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 		/* initialize class header pointer */
701*0Sstevel@tonic-gate 		if (cl_hdr_sz == 0) {
702*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_hdr = NULL;
703*0Sstevel@tonic-gate 		} else {
704*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_hdr =
705*0Sstevel@tonic-gate 			    (void *)(msgbufp + offset);
706*0Sstevel@tonic-gate 		}
707*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_cl_hdr_len = cl_hdr_sz;
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		offset += cl_hdr_sz;
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 		/* initialize data area pointer */
712*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_cl_data =
713*0Sstevel@tonic-gate 		    (void *)(msgbufp + offset);
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_data_offset = 0;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 		cl_data_sz = MAD_SIZE_IN_BYTES -
718*0Sstevel@tonic-gate 		    sizeof (ib_mad_hdr_t) - cl_hdr_off - cl_hdr_sz;
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_pkt_data_sz = cl_data_sz;
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		/*
723*0Sstevel@tonic-gate 		 * calculate number of expected packets for transaction
724*0Sstevel@tonic-gate 		 * timeout calculation
725*0Sstevel@tonic-gate 		 */
726*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_flags & IBMF_CTX_RMPP_FLAGS_DYN_PYLD) {
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 			/*
729*0Sstevel@tonic-gate 			 * if the payload length is not specified in
730*0Sstevel@tonic-gate 			 * the first packet, just guess how many packets
731*0Sstevel@tonic-gate 			 * might arrive
732*0Sstevel@tonic-gate 			 */
733*0Sstevel@tonic-gate 			msgimplp->im_rmpp_ctx.rmpp_num_pkts = 100;
734*0Sstevel@tonic-gate 		} else {
735*0Sstevel@tonic-gate 			msgimplp->im_rmpp_ctx.rmpp_num_pkts =
736*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len / cl_data_sz;
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 			/* round up */
739*0Sstevel@tonic-gate 			if ((rmpp_ctx->rmpp_pyld_len % cl_data_sz) != 0)
740*0Sstevel@tonic-gate 				msgimplp->im_rmpp_ctx.rmpp_num_pkts++;
741*0Sstevel@tonic-gate 		}
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 		/* set the transaction timer if there are more packets */
744*0Sstevel@tonic-gate 		if ((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_LAST_PKT) == 0) {
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
747*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
748*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
749*0Sstevel@tonic-gate 			    tnf_string, msg,
750*0Sstevel@tonic-gate 			    "First pkt recvd; setting trans timer: ",
751*0Sstevel@tonic-gate 			    tnf_opaque, msg, msgimplp);
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
754*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
755*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): setting trans"
756*0Sstevel@tonic-gate 			    " timer %p %d\n", tnf_opaque, msg, msgimplp,
757*0Sstevel@tonic-gate 			    tnf_opaque, timeout_id, msgimplp->im_rp_timeout_id);
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_recv_timeout, msgimplp,
760*0Sstevel@tonic-gate 			    IBMF_TRANS_TIMER);
761*0Sstevel@tonic-gate 		}
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	offset = sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	/*
767*0Sstevel@tonic-gate 	 * copy the data from the packet into the data buffer in
768*0Sstevel@tonic-gate 	 * the message.
769*0Sstevel@tonic-gate 	 */
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_LAST_PKT)
772*0Sstevel@tonic-gate 		data_sz = b2h32(rmpp_hdr->rmpp_pyldlen_nwl) - cl_hdr_sz;
773*0Sstevel@tonic-gate 	else
774*0Sstevel@tonic-gate 		data_sz = rmpp_ctx->rmpp_pkt_data_sz;
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 	/* if a payload length was specified and we've met or exceeded it */
777*0Sstevel@tonic-gate 	if (((data_sz + rmpp_ctx->rmpp_data_offset) >=
778*0Sstevel@tonic-gate 	    rmpp_ctx->rmpp_pyld_len) &&
779*0Sstevel@tonic-gate 	    ((rmpp_ctx->rmpp_flags & IBMF_CTX_RMPP_FLAGS_DYN_PYLD) == 0)) {
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 		/* last packet flag should be set */
782*0Sstevel@tonic-gate 		if ((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_LAST_PKT) == 0) {
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 			/* abort with status Incon. last and payload length */
785*0Sstevel@tonic-gate 			status = ibmf_i_send_rmpp(msgimplp,
786*0Sstevel@tonic-gate 			    IBMF_RMPP_TYPE_ABORT, IBMF_RMPP_STATUS_ILPL,
787*0Sstevel@tonic-gate 			    0, 0, IBMF_NO_BLOCK);
788*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
789*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
790*0Sstevel@tonic-gate 				    ibmf_i_rmpp_recvr_flow_main,
791*0Sstevel@tonic-gate 				    IBMF_TNF_TRACE, "",
792*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
793*0Sstevel@tonic-gate 				    tnf_string, msg, "RMPP ABORT send failed");
794*0Sstevel@tonic-gate 				msgimplp->im_trans_state_flags |=
795*0Sstevel@tonic-gate 				    IBMF_TRANS_STATE_FLAG_SEND_DONE;
796*0Sstevel@tonic-gate 			}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 			mutex_enter(&clientp->ic_kstat_mutex);
799*0Sstevel@tonic-gate 			IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
800*0Sstevel@tonic-gate 			mutex_exit(&clientp->ic_kstat_mutex);
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
803*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
804*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
805*0Sstevel@tonic-gate 			    tnf_string, msg,
806*0Sstevel@tonic-gate 			    "Inconsistent last and payload length detected,"
807*0Sstevel@tonic-gate 			    " sending ABORT ILPL, unsetting trans timer");
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 			ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
814*0Sstevel@tonic-gate 			    msgimplp, IBMF_RESP_TIMER);
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
817*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE,
818*0Sstevel@tonic-gate 			    "", "ibmf_i_rmpp_recvr_flow_main() exit\n");
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 			return;
821*0Sstevel@tonic-gate 		}
822*0Sstevel@tonic-gate 	} else if (((data_sz + rmpp_ctx->rmpp_data_offset) >=
823*0Sstevel@tonic-gate 	    rmpp_ctx->rmpp_pyld_len) &&
824*0Sstevel@tonic-gate 	    ((rmpp_ctx->rmpp_flags & IBMF_CTX_RMPP_FLAGS_DYN_PYLD) != 0) &&
825*0Sstevel@tonic-gate 	    ((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_LAST_PKT) == 0)) {
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		/*
828*0Sstevel@tonic-gate 		 * If the payload length was not specified in the first
829*0Sstevel@tonic-gate 		 * packet's RMPP header, we have a temporary receive buffer
830*0Sstevel@tonic-gate 		 * the size of which will be exceeded with this incoming
831*0Sstevel@tonic-gate 		 * packet. We need to allocate a new temporary receive buffer
832*0Sstevel@tonic-gate 		 * with an additional IBMF_BUF_PKTS data payloads.
833*0Sstevel@tonic-gate 		 */
834*0Sstevel@tonic-gate 		ib_mad_hdr_t	*old_buf;
835*0Sstevel@tonic-gate 		size_t		prev_pyld_len;
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate 		old_buf = msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
838*0Sstevel@tonic-gate 		prev_pyld_len = rmpp_ctx->rmpp_pyld_len;
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_pyld_len +=
841*0Sstevel@tonic-gate 		    IBMF_BUF_PKTS * rmpp_ctx->rmpp_pkt_data_sz;
842*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_cl_data_len =
843*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len;
844*0Sstevel@tonic-gate 		msgimplp->im_msgbufs_recv.im_bufs_mad_hdr =
845*0Sstevel@tonic-gate 		    (ib_mad_hdr_t *)kmem_zalloc(sizeof (ib_mad_hdr_t) +
846*0Sstevel@tonic-gate 		    cl_hdr_off + cl_hdr_sz + rmpp_ctx->rmpp_pyld_len,
847*0Sstevel@tonic-gate 		    KM_NOSLEEP);
848*0Sstevel@tonic-gate 		if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
851*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
852*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s, allocsz = %d\n",
853*0Sstevel@tonic-gate 			    tnf_string, msg,
854*0Sstevel@tonic-gate 			    "mem allocation failure (unknown rmpp payload)",
855*0Sstevel@tonic-gate 			    tnf_uint, alloc_size,
856*0Sstevel@tonic-gate 			    sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz +
857*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len);
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 			ibmf_i_terminate_transaction(
860*0Sstevel@tonic-gate 			    msgimplp->im_client, msgimplp,
861*0Sstevel@tonic-gate 			    IBMF_NO_MEMORY);
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
864*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE,
865*0Sstevel@tonic-gate 			    "", "ibmf_i_rmpp_recvr_flow_main() exit\n");
866*0Sstevel@tonic-gate 			return;
867*0Sstevel@tonic-gate 		}
868*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
869*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, recv_bufs_alloced, 1);
870*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		msgbufp = (uchar_t *)msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 		/* copy the MAD and class header */
875*0Sstevel@tonic-gate 		bcopy((const void *)old_buf, (void *)msgbufp,
876*0Sstevel@tonic-gate 		    sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz +
877*0Sstevel@tonic-gate 		    prev_pyld_len);
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 		kmem_free(old_buf, sizeof (ib_mad_hdr_t) + cl_hdr_off +
880*0Sstevel@tonic-gate 		    cl_hdr_sz + prev_pyld_len);
881*0Sstevel@tonic-gate 	}
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	/* don't overflow buffer */
884*0Sstevel@tonic-gate 	if (rmpp_ctx->rmpp_data_offset + data_sz >
885*0Sstevel@tonic-gate 	    rmpp_ctx->rmpp_pyld_len) {
886*0Sstevel@tonic-gate 		data_sz = rmpp_ctx->rmpp_pyld_len -
887*0Sstevel@tonic-gate 		    rmpp_ctx->rmpp_data_offset;
888*0Sstevel@tonic-gate 	}
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 	datap = (uchar_t *)msgimplp->im_msgbufs_recv.im_bufs_cl_data;
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	bcopy((void *)&mad[offset],
893*0Sstevel@tonic-gate 	    (void *)(datap + rmpp_ctx->rmpp_data_offset), data_sz);
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_data_offset += data_sz;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_es++;
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_rmpp_recvr_flow_main,
900*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_rmpp_recvr_flow_main(): es = %d\n",
901*0Sstevel@tonic-gate 	    tnf_uint, es, rmpp_ctx->rmpp_es);
902*0Sstevel@tonic-gate 
903*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_LAST_PKT) {
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 		/*
906*0Sstevel@tonic-gate 		 * Since this is the last packet, we finally know the
907*0Sstevel@tonic-gate 		 * size of the receive buffer we need to allocate.
908*0Sstevel@tonic-gate 		 * Allocate the needed size and free the temporary receive
909*0Sstevel@tonic-gate 		 * buffer.
910*0Sstevel@tonic-gate 		 */
911*0Sstevel@tonic-gate 		if ((rmpp_ctx->rmpp_flags & IBMF_CTX_RMPP_FLAGS_DYN_PYLD) !=
912*0Sstevel@tonic-gate 		    0) {
913*0Sstevel@tonic-gate 			ib_mad_hdr_t	*old_buf;
914*0Sstevel@tonic-gate 			size_t		prev_pyld_len;
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_flags &= ~IBMF_CTX_RMPP_FLAGS_DYN_PYLD;
917*0Sstevel@tonic-gate 			old_buf = msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
918*0Sstevel@tonic-gate 			prev_pyld_len = rmpp_ctx->rmpp_pyld_len;
919*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_pyld_len = rmpp_ctx->rmpp_data_offset;
920*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_data_len =
921*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len;
922*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_mad_hdr =
923*0Sstevel@tonic-gate 			    (ib_mad_hdr_t *)kmem_zalloc(sizeof (ib_mad_hdr_t) +
924*0Sstevel@tonic-gate 			    cl_hdr_off + cl_hdr_sz + rmpp_ctx->rmpp_pyld_len,
925*0Sstevel@tonic-gate 			    KM_NOSLEEP);
926*0Sstevel@tonic-gate 			if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
927*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
928*0Sstevel@tonic-gate 				    ibmf_i_rmpp_recvr_flow_main,
929*0Sstevel@tonic-gate 				    IBMF_TNF_TRACE, "",
930*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
931*0Sstevel@tonic-gate 				    tnf_string, msg,
932*0Sstevel@tonic-gate 				    "mem allocation failure (final payload)");
933*0Sstevel@tonic-gate 				ibmf_i_terminate_transaction(
934*0Sstevel@tonic-gate 				    msgimplp->im_client, msgimplp,
935*0Sstevel@tonic-gate 				    IBMF_NO_MEMORY);
936*0Sstevel@tonic-gate 				IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
937*0Sstevel@tonic-gate 				    ibmf_i_rmpp_recvr_flow_main_end,
938*0Sstevel@tonic-gate 				    IBMF_TNF_TRACE, "",
939*0Sstevel@tonic-gate 				    "ibmf_i_rmpp_recvr_flow_main() exit\n");
940*0Sstevel@tonic-gate 				return;
941*0Sstevel@tonic-gate 			}
942*0Sstevel@tonic-gate 			mutex_enter(&clientp->ic_kstat_mutex);
943*0Sstevel@tonic-gate 			IBMF_ADD32_KSTATS(clientp, recv_bufs_alloced, 1);
944*0Sstevel@tonic-gate 			mutex_exit(&clientp->ic_kstat_mutex);
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 			msgbufp = (uchar_t *)
947*0Sstevel@tonic-gate 			    msgimplp->im_msgbufs_recv.im_bufs_mad_hdr;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 			/* copy the data to the new buffer */
950*0Sstevel@tonic-gate 			bcopy((const void *)old_buf, (void *)msgbufp,
951*0Sstevel@tonic-gate 			    sizeof (ib_mad_hdr_t) + cl_hdr_off + cl_hdr_sz +
952*0Sstevel@tonic-gate 			    rmpp_ctx->rmpp_pyld_len);
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 			offset = sizeof (ib_mad_hdr_t) + cl_hdr_off;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 			/* initialize class header pointer */
957*0Sstevel@tonic-gate 			if (cl_hdr_sz == 0) {
958*0Sstevel@tonic-gate 				msgimplp->im_msgbufs_recv.im_bufs_cl_hdr = NULL;
959*0Sstevel@tonic-gate 			} else {
960*0Sstevel@tonic-gate 				msgimplp->im_msgbufs_recv.im_bufs_cl_hdr =
961*0Sstevel@tonic-gate 				    (void *)(msgbufp + offset);
962*0Sstevel@tonic-gate 			}
963*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_hdr_len =
964*0Sstevel@tonic-gate 			    cl_hdr_sz;
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 			offset += cl_hdr_sz;
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 			/* initialize data area pointer */
969*0Sstevel@tonic-gate 			msgimplp->im_msgbufs_recv.im_bufs_cl_data =
970*0Sstevel@tonic-gate 			    (void *)(msgbufp + offset);
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate 			kmem_free(old_buf, sizeof (ib_mad_hdr_t) + cl_hdr_off +
973*0Sstevel@tonic-gate 			    cl_hdr_sz + prev_pyld_len);
974*0Sstevel@tonic-gate 		}
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
977*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
978*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s, msgp = 0x%p\n",
979*0Sstevel@tonic-gate 		    tnf_string, msg,
980*0Sstevel@tonic-gate 		    "Last pkt rcvd; state to recv_term, sending ack",
981*0Sstevel@tonic-gate 		    tnf_opaque, msgp, msgimplp);
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_RECEVR_TERMINATE;
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
986*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_NORMAL, rmpp_ctx->rmpp_es - 1,
987*0Sstevel@tonic-gate 		    rmpp_ctx->rmpp_es - 1 + IBMF_RMPP_DEFAULT_WIN_SZ,
988*0Sstevel@tonic-gate 		    IBMF_NO_BLOCK);
989*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
990*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
991*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
992*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
993*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ACK send failed");
994*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
995*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
996*0Sstevel@tonic-gate 		}
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate 		/* unset the transaction timer if it's not the first segment */
999*0Sstevel@tonic-gate 		if ((rmpp_hdr->rmpp_flags & IBMF_RMPP_FLAGS_FIRST_PKT) == 0) {
1000*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1001*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1002*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): %s, msgp = 0x%p\n",
1003*0Sstevel@tonic-gate 			    tnf_string, msg, "Last, but not first segment",
1004*0Sstevel@tonic-gate 			    tnf_opaque, msgp, msgimplp);
1005*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1006*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1007*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_flow_main(): unsetting timer "
1008*0Sstevel@tonic-gate 			    "%p %d\n", tnf_opaque, msgp, msgimplp,
1009*0Sstevel@tonic-gate 			    tnf_opaque, timeout_id, msgimplp->im_rp_timeout_id);
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate 			ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1012*0Sstevel@tonic-gate 		}
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1015*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1016*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s, msgp = 0x%p\n",
1017*0Sstevel@tonic-gate 		    tnf_string, msg,
1018*0Sstevel@tonic-gate 		    "Last pkt rcvd; setting resp timer",
1019*0Sstevel@tonic-gate 		    tnf_opaque, msgp, msgimplp);
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 		/*
1022*0Sstevel@tonic-gate 		 * The RMPP receive transaction has been broken
1023*0Sstevel@tonic-gate 		 * up into two parts. At this point in the
1024*0Sstevel@tonic-gate 		 * transaction, all the data has been received.
1025*0Sstevel@tonic-gate 		 * From the perspective of the client, the transaction
1026*0Sstevel@tonic-gate 		 * is complete. So, control is returned to the client
1027*0Sstevel@tonic-gate 		 * at this point. However, the RMPP protocol requires
1028*0Sstevel@tonic-gate 		 * a wait after receiving the last data packet, so that,
1029*0Sstevel@tonic-gate 		 * duplicate packets may be absorbed. This wait is
1030*0Sstevel@tonic-gate 		 * implemented in the second part of the transaction under
1031*0Sstevel@tonic-gate 		 * a duplicate message context.
1032*0Sstevel@tonic-gate 		 * The regular message context is marked as done in
1033*0Sstevel@tonic-gate 		 * ibmf_i_terminate_transaction().
1034*0Sstevel@tonic-gate 		 * The IBMF_MSG_FLAGS_SET_TERMINATION flag indicates
1035*0Sstevel@tonic-gate 		 * that the duplicate message context needs to be created
1036*0Sstevel@tonic-gate 		 * to handle the termination loop.
1037*0Sstevel@tonic-gate 		 */
1038*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1039*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1040*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): last packet, "
1041*0Sstevel@tonic-gate 		    " returning data to client for message %p\n",
1042*0Sstevel@tonic-gate 		    tnf_opaque, msgp, msgimplp);
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 		ibmf_i_terminate_transaction(clientp, msgimplp, IBMF_SUCCESS);
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 		/* Mark this message for early termination */
1047*0Sstevel@tonic-gate 		msgimplp->im_flags |= IBMF_MSG_FLAGS_SET_TERMINATION;
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 		return;
1050*0Sstevel@tonic-gate 	}
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 	if (b2h32(rmpp_hdr->rmpp_segnum) == rmpp_ctx->rmpp_wl) {
1053*0Sstevel@tonic-gate 		IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L3,
1054*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1055*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s, msgp = 0x%p"
1056*0Sstevel@tonic-gate 		    "segnum = %d, wl = %d\n", tnf_string, msg,
1057*0Sstevel@tonic-gate 		    "Last packet in window received", tnf_opaque, msgimplp,
1058*0Sstevel@tonic-gate 		    msgimplp, tnf_opaque, seg, b2h32(rmpp_hdr->rmpp_segnum),
1059*0Sstevel@tonic-gate 		    tnf_opaque, wl, rmpp_ctx->rmpp_wl);
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 		(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
1062*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_NORMAL,
1063*0Sstevel@tonic-gate 		    rmpp_ctx->rmpp_es - 1,
1064*0Sstevel@tonic-gate 		    rmpp_ctx->rmpp_es - 1 +
1065*0Sstevel@tonic-gate 		    IBMF_RMPP_DEFAULT_WIN_SZ, IBMF_NO_BLOCK);
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 		/* update the window */
1068*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_wl += IBMF_RMPP_DEFAULT_WIN_SZ;
1069*0Sstevel@tonic-gate 
1070*0Sstevel@tonic-gate 	} else {
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1073*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_flow_main, IBMF_TNF_TRACE, "",
1074*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_flow_main(): %s\n",
1075*0Sstevel@tonic-gate 		    tnf_string, msg, "Packet in window received");
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 	}
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1080*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_flow_main_end, IBMF_TNF_TRACE, "",
1081*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_flow_main() exit\n");
1082*0Sstevel@tonic-gate }
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate /*
1085*0Sstevel@tonic-gate  * ibmf_i_rmpp_recvr_active_flow():
1086*0Sstevel@tonic-gate  *	Perform RMPP receiver flow initiation processing.
1087*0Sstevel@tonic-gate  *	Refer to figure 176 "RMPP Receiver Main Flow Diagram" of
1088*0Sstevel@tonic-gate  *	the InfiniBand Architecture Specification Volume 1, Release 1.1
1089*0Sstevel@tonic-gate  */
1090*0Sstevel@tonic-gate static void
ibmf_i_rmpp_recvr_active_flow(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * mad)1091*0Sstevel@tonic-gate ibmf_i_rmpp_recvr_active_flow(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
1092*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *mad)
1093*0Sstevel@tonic-gate {
1094*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1095*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
1096*0Sstevel@tonic-gate 	uint32_t	abort_status;
1097*0Sstevel@tonic-gate 	int		status;
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
1100*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_active_flow_start, IBMF_TNF_TRACE, "",
1101*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_active_flow(): clientp = 0x%p, qp_hdl = 0x%p, "
1102*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
1103*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
1104*0Sstevel@tonic-gate 	    tnf_opaque, mad, mad);
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(mad + sizeof (ib_mad_hdr_t));
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ACK) {
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate 		/* discard this packet by not processing it here */
1111*0Sstevel@tonic-gate 
1112*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1113*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_active_flow, IBMF_TNF_TRACE, "",
1114*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_active_flow(): %s\n", tnf_string, msg,
1115*0Sstevel@tonic-gate 		    "ACK packet received, discarding packet");
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 		/*
1118*0Sstevel@tonic-gate 		 * reset the timer if we're still waiting for the first seg;
1119*0Sstevel@tonic-gate 		 * this is the same timer that is normally set in send_compl
1120*0Sstevel@tonic-gate 		 * NOTE: this should be in the IB spec's flowchart but isn't
1121*0Sstevel@tonic-gate 		 */
1122*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_es == 1) {
1123*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
1124*0Sstevel@tonic-gate 			    IBMF_RESP_TIMER);
1125*0Sstevel@tonic-gate 		}
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 		return;
1128*0Sstevel@tonic-gate 	}
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_DATA) {
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1133*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_active_flow, IBMF_TNF_TRACE, "",
1134*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_active_flow(): %s\n", tnf_string, msg,
1135*0Sstevel@tonic-gate 		    "DATA packet received, processing packet");
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate 		ibmf_i_rmpp_recvr_flow_main(clientp, qp_hdl, msgimplp, mad);
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 	} else if ((rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_STOP) ||
1140*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ABORT)) {
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate 		abort_status = rmpp_hdr->rmpp_status;
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L2,
1145*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_active_flow, IBMF_TNF_TRACE, "",
1146*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_active_flow(): %s, status = %d\n",
1147*0Sstevel@tonic-gate 		    tnf_string, msg,
1148*0Sstevel@tonic-gate 		    "STOP/ABORT packet received, terminating transaction",
1149*0Sstevel@tonic-gate 		    tnf_uint, abort_status, abort_status);
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 		/* discard the packet and terminate the transaction */
1152*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1153*0Sstevel@tonic-gate 		msgimplp->im_trans_state_flags |=
1154*0Sstevel@tonic-gate 		    IBMF_TRANS_STATE_FLAG_SEND_DONE;
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 		ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1159*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 	} else {
1162*0Sstevel@tonic-gate 
1163*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1164*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_active_flow, IBMF_TNF_TRACE, "",
1165*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_active_flow(): %s\n", tnf_string, msg,
1166*0Sstevel@tonic-gate 		    "Unrecognized packet received, terminating transaction");
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 		/* abort with status BadT */
1169*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
1170*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_BADT, 0, 0, IBMF_NO_BLOCK);
1171*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
1172*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1173*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_active_flow, IBMF_TNF_TRACE, "",
1174*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_active_flow(): %s\n",
1175*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ABORT send failed");
1176*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
1177*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
1178*0Sstevel@tonic-gate 		}
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
1183*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
1184*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate 		ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1187*0Sstevel@tonic-gate 
1188*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1189*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
1190*0Sstevel@tonic-gate 	}
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1193*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_active_flow_end, IBMF_TNF_TRACE, "",
1194*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_active_flow() exit\n");
1195*0Sstevel@tonic-gate }
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate /*
1198*0Sstevel@tonic-gate  * ibmf_i_rmpp_recvr_term_flow():
1199*0Sstevel@tonic-gate  *	Perform RMPP receiver termination flow processing.
1200*0Sstevel@tonic-gate  *	Refer to figure 177 "RMPP Receiver Termination Flow Diagram" of
1201*0Sstevel@tonic-gate  *	the InfiniBand Architecture Specification Volume 1, Release 1.1
1202*0Sstevel@tonic-gate  */
1203*0Sstevel@tonic-gate static void
ibmf_i_rmpp_recvr_term_flow(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * mad)1204*0Sstevel@tonic-gate ibmf_i_rmpp_recvr_term_flow(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
1205*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *mad)
1206*0Sstevel@tonic-gate {
1207*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1208*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
1209*0Sstevel@tonic-gate 	int		status;
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
1212*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_term_flow_start, IBMF_TNF_TRACE, "",
1213*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_term_flow(): clientp = 0x%p, qp_hdl = 0x%p, "
1214*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
1215*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
1216*0Sstevel@tonic-gate 	    tnf_opaque, mad, mad);
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(mad + sizeof (ib_mad_hdr_t));
1219*0Sstevel@tonic-gate 
1220*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_DATA) {
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1223*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1224*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_term_flow(): %s\n", tnf_string, msg,
1225*0Sstevel@tonic-gate 		    "Data packet received, resending ACK");
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 		(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ACK,
1228*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_NORMAL, rmpp_ctx->rmpp_es - 1,
1229*0Sstevel@tonic-gate 		    rmpp_ctx->rmpp_es - 1 + IBMF_RMPP_DEFAULT_WIN_SZ,
1230*0Sstevel@tonic-gate 		    IBMF_NO_BLOCK);
1231*0Sstevel@tonic-gate 
1232*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1233*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1234*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_term_flow(): setting resp timer %d %p\n",
1235*0Sstevel@tonic-gate 		    tnf_opaque, msgimplp, msgimplp, tnf_opaque,
1236*0Sstevel@tonic-gate 		    timeout_id, msgimplp->im_rp_timeout_id);
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 		/* set the response timer */
1239*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_recv_timeout, msgimplp,
1240*0Sstevel@tonic-gate 		    IBMF_RESP_TIMER);
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	} else if (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ACK) {
1243*0Sstevel@tonic-gate 
1244*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1245*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1246*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_term_flow(): %s, msgp = 0x%p\n",
1247*0Sstevel@tonic-gate 		    tnf_string, msg, "ACK packet received",
1248*0Sstevel@tonic-gate 		    tnf_opaque, msgimplp, msgimplp);
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_is_ds) {
1251*0Sstevel@tonic-gate 			/*
1252*0Sstevel@tonic-gate 			 * received ACK from sender which is indication that
1253*0Sstevel@tonic-gate 			 * we can send response; notify client that data has
1254*0Sstevel@tonic-gate 			 * arrived; it will call msg_transport to send response
1255*0Sstevel@tonic-gate 			 */
1256*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3,
1257*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1258*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_term_flow(): %s, msgp = 0x%p\n",
1259*0Sstevel@tonic-gate 			    tnf_string, msg,
1260*0Sstevel@tonic-gate 			    "Received final ack for double-sided trans",
1261*0Sstevel@tonic-gate 			    tnf_opaque, msgimplp, msgimplp);
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 			/*
1264*0Sstevel@tonic-gate 			 * successful termination
1265*0Sstevel@tonic-gate 			 */
1266*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_DONE;
1267*0Sstevel@tonic-gate 			ibmf_i_terminate_transaction(clientp, msgimplp,
1268*0Sstevel@tonic-gate 			    IBMF_SUCCESS);
1269*0Sstevel@tonic-gate 
1270*0Sstevel@tonic-gate 		} else {
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate 			IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L2,
1273*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1274*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_term_flow(): %s, msgp = 0x%p\n",
1275*0Sstevel@tonic-gate 			    tnf_string, msg, "Received ACK while in recv_term "
1276*0Sstevel@tonic-gate 			    "state for single sided trans",
1277*0Sstevel@tonic-gate 			    tnf_opaque, msgimplp, msgimplp);
1278*0Sstevel@tonic-gate 
1279*0Sstevel@tonic-gate 			/* abort with status BadT */
1280*0Sstevel@tonic-gate 			(void) ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
1281*0Sstevel@tonic-gate 			    IBMF_RMPP_STATUS_BADT, 0, 0, IBMF_NO_BLOCK);
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate 			rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1284*0Sstevel@tonic-gate 
1285*0Sstevel@tonic-gate 			ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1288*0Sstevel@tonic-gate 			    msgimplp, IBMF_RESP_TIMER);
1289*0Sstevel@tonic-gate 		}
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 	} else {
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1294*0Sstevel@tonic-gate 		    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1295*0Sstevel@tonic-gate 		    "ibmf_i_rmpp_recvr_term_flow(): %s\n", tnf_string, msg,
1296*0Sstevel@tonic-gate 		    "Unexpected packet received, sending ABORT BADT");
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 		/* abort with status BadT */
1299*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
1300*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_BADT, 0, 0, IBMF_NO_BLOCK);
1301*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
1302*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1303*0Sstevel@tonic-gate 			    ibmf_i_rmpp_recvr_term_flow, IBMF_TNF_TRACE, "",
1304*0Sstevel@tonic-gate 			    "ibmf_i_rmpp_recvr_term_flow(): %s\n",
1305*0Sstevel@tonic-gate 			    tnf_string, msg, "RMPP ABORT send failed");
1306*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
1307*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
1308*0Sstevel@tonic-gate 		}
1309*0Sstevel@tonic-gate 
1310*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
1313*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
1314*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate 		ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1319*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
1320*0Sstevel@tonic-gate 
1321*0Sstevel@tonic-gate 	}
1322*0Sstevel@tonic-gate 
1323*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1324*0Sstevel@tonic-gate 	    ibmf_i_rmpp_recvr_term_flow_end, IBMF_TNF_TRACE, "",
1325*0Sstevel@tonic-gate 	    "ibmf_i_rmpp_recvr_term_flow() exit\n");
1326*0Sstevel@tonic-gate }
1327*0Sstevel@tonic-gate 
1328*0Sstevel@tonic-gate /*
1329*0Sstevel@tonic-gate  * ibmf_i_is_valid_rmpp_status():
1330*0Sstevel@tonic-gate  *	Check for a valid RMPP status
1331*0Sstevel@tonic-gate  */
1332*0Sstevel@tonic-gate static boolean_t
ibmf_i_is_valid_rmpp_status(ibmf_rmpp_hdr_t * rmpp_hdr)1333*0Sstevel@tonic-gate ibmf_i_is_valid_rmpp_status(ibmf_rmpp_hdr_t *rmpp_hdr)
1334*0Sstevel@tonic-gate {
1335*0Sstevel@tonic-gate 	boolean_t	found = B_TRUE;
1336*0Sstevel@tonic-gate 
1337*0Sstevel@tonic-gate 	IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L4,
1338*0Sstevel@tonic-gate 	    ibmf_i_is_valid_rmpp_status_start, IBMF_TNF_TRACE, "",
1339*0Sstevel@tonic-gate 	    "ibmf_i_is_valid_rmpp_status(): rmpp_hdr = 0x%p\n",
1340*0Sstevel@tonic-gate 	    tnf_opaque, rmpp_hdr, rmpp_hdr);
1341*0Sstevel@tonic-gate 
1342*0Sstevel@tonic-gate 	if (((rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_DATA) ||
1343*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_type == IBMF_RMPP_TYPE_ACK)) &&
1344*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status != IBMF_RMPP_STATUS_NORMAL))
1345*0Sstevel@tonic-gate 		found = B_FALSE;
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 	if ((rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_RESX) &&
1348*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_type != IBMF_RMPP_TYPE_STOP))
1349*0Sstevel@tonic-gate 		found = B_FALSE;
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate 	if (((rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_T2L) ||
1352*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_ILPL) ||
1353*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_IFSN) ||
1354*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_BADT) ||
1355*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_W2S) ||
1356*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_S2B) ||
1357*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_IS) ||
1358*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_UNV) ||
1359*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_TMR) ||
1360*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_status == IBMF_RMPP_STATUS_USP)) &&
1361*0Sstevel@tonic-gate 	    (rmpp_hdr->rmpp_type != IBMF_RMPP_TYPE_ABORT))
1362*0Sstevel@tonic-gate 		found = B_FALSE;
1363*0Sstevel@tonic-gate 
1364*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1365*0Sstevel@tonic-gate 	    ibmf_i_is_valid_rmpp_status_end, IBMF_TNF_TRACE, "",
1366*0Sstevel@tonic-gate 	    "ibmf_i_is_valid_rmpp_status_flow() exit\n");
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 	return (found);
1369*0Sstevel@tonic-gate }
1370*0Sstevel@tonic-gate 
1371*0Sstevel@tonic-gate /*
1372*0Sstevel@tonic-gate  * ibmf_i_handle_rmpp():
1373*0Sstevel@tonic-gate  *	Handle RMPP processing of an incoming IB packet
1374*0Sstevel@tonic-gate  */
1375*0Sstevel@tonic-gate void
ibmf_i_handle_rmpp(ibmf_client_t * clientp,ibmf_qp_handle_t qp_hdl,ibmf_msg_impl_t * msgimplp,uchar_t * madp)1376*0Sstevel@tonic-gate ibmf_i_handle_rmpp(ibmf_client_t *clientp, ibmf_qp_handle_t qp_hdl,
1377*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, uchar_t *madp)
1378*0Sstevel@tonic-gate {
1379*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1380*0Sstevel@tonic-gate 	ibmf_rmpp_hdr_t *rmpp_hdr;
1381*0Sstevel@tonic-gate 	int		status;
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4,
1384*0Sstevel@tonic-gate 	    ibmf_i_handle_rmpp_start, IBMF_TNF_TRACE, "",
1385*0Sstevel@tonic-gate 	    "ibmf_i_handle_rmpp(): clientp = 0x%p, qp_hdl = 0x%p, "
1386*0Sstevel@tonic-gate 	    "msgp = 0x%p, madp = 0x%p\n", tnf_opaque, clientp, clientp,
1387*0Sstevel@tonic-gate 	    tnf_opaque, qp_hdl, qp_hdl, tnf_opaque, msg, msgimplp,
1388*0Sstevel@tonic-gate 	    tnf_opaque, mad, madp);
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&msgimplp->im_mutex));
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate 	rmpp_hdr = (ibmf_rmpp_hdr_t *)(madp + sizeof (ib_mad_hdr_t));
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 	/*
1395*0Sstevel@tonic-gate 	 * Check the version in the RMPP header
1396*0Sstevel@tonic-gate 	 */
1397*0Sstevel@tonic-gate 	if (rmpp_hdr->rmpp_version != IBMF_RMPP_VERSION) {
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 		/*
1400*0Sstevel@tonic-gate 		 * If the receive buffer is not yet allocated, this is
1401*0Sstevel@tonic-gate 		 * probably the first MAD received for the receive context.
1402*0Sstevel@tonic-gate 		 * We need to set up the receive buffer before calling
1403*0Sstevel@tonic-gate 		 * ibmf_i_send_rmpp() to send an ABORT packet.
1404*0Sstevel@tonic-gate 		 */
1405*0Sstevel@tonic-gate 		if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
1406*0Sstevel@tonic-gate 			status = ibmf_setup_recvbuf_on_error(msgimplp, madp);
1407*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
1408*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L2,
1409*0Sstevel@tonic-gate 				    ibmf_i_handle_rmpp_err, IBMF_TNF_ERROR, "",
1410*0Sstevel@tonic-gate 				    "ibmf_i_handle_rmpp(): %s\n", tnf_string,
1411*0Sstevel@tonic-gate 				    msg,
1412*0Sstevel@tonic-gate 				    "ibmf_setup_recvbuf_on_error() failed");
1413*0Sstevel@tonic-gate 				return;
1414*0Sstevel@tonic-gate 			}
1415*0Sstevel@tonic-gate 		}
1416*0Sstevel@tonic-gate 
1417*0Sstevel@tonic-gate 		/*
1418*0Sstevel@tonic-gate 		 * Drop the message if the transaction has not yet
1419*0Sstevel@tonic-gate 		 * been identified as a send or receive RMPP transaction.
1420*0Sstevel@tonic-gate 		 * This is because the send completion of an abort packet
1421*0Sstevel@tonic-gate 		 * will hit the non-rmpp code which attempts to reset the
1422*0Sstevel@tonic-gate 		 * RESP timer set after sending the abort packet, causing
1423*0Sstevel@tonic-gate 		 * an assert.
1424*0Sstevel@tonic-gate 		 */
1425*0Sstevel@tonic-gate 		if (((msgimplp->im_flags & IBMF_MSG_FLAGS_RECV_RMPP) == 0) &&
1426*0Sstevel@tonic-gate 		    (msgimplp->im_flags & IBMF_MSG_FLAGS_SEND_RMPP) == 0) {
1427*0Sstevel@tonic-gate 			/*
1428*0Sstevel@tonic-gate 			 * Reset the response timer since we're still
1429*0Sstevel@tonic-gate 			 * waiting for the first response MAD, provided
1430*0Sstevel@tonic-gate 			 * that the send completion has occured
1431*0Sstevel@tonic-gate 			 */
1432*0Sstevel@tonic-gate 			if (msgimplp->im_trans_state_flags &
1433*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE) {
1434*0Sstevel@tonic-gate 				ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
1435*0Sstevel@tonic-gate 				    IBMF_RESP_TIMER);
1436*0Sstevel@tonic-gate 			}
1437*0Sstevel@tonic-gate 
1438*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1439*0Sstevel@tonic-gate 			    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1440*0Sstevel@tonic-gate 			    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1441*0Sstevel@tonic-gate 			    "BAD version detected, dropping MAD");
1442*0Sstevel@tonic-gate 
1443*0Sstevel@tonic-gate 			return;
1444*0Sstevel@tonic-gate 		}
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 		/* abort with status BadT */
1447*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
1448*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_UNV, 0, 0, IBMF_NO_BLOCK);
1449*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
1450*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L2,
1451*0Sstevel@tonic-gate 			    ibmf_i_handle_rmpp_err, IBMF_TNF_ERROR, "",
1452*0Sstevel@tonic-gate 			    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1453*0Sstevel@tonic-gate 			    "RMPP ABORT send failed");
1454*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
1455*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
1456*0Sstevel@tonic-gate 		}
1457*0Sstevel@tonic-gate 
1458*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
1459*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
1460*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
1461*0Sstevel@tonic-gate 
1462*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1463*0Sstevel@tonic-gate 		    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1464*0Sstevel@tonic-gate 		    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1465*0Sstevel@tonic-gate 		    "Unsupported RMPP version detected, sending ABORT UNV");
1466*0Sstevel@tonic-gate 
1467*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1468*0Sstevel@tonic-gate 		    ibmf_i_handle_rmpp_end, IBMF_TNF_TRACE, "",
1469*0Sstevel@tonic-gate 		    "ibmf_i_handle_rmpp() exit\n");
1470*0Sstevel@tonic-gate 
1471*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate 		ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1476*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
1477*0Sstevel@tonic-gate 
1478*0Sstevel@tonic-gate 		return;
1479*0Sstevel@tonic-gate 	}
1480*0Sstevel@tonic-gate 
1481*0Sstevel@tonic-gate 	/*
1482*0Sstevel@tonic-gate 	 * Check for a valid status in the RMPP header
1483*0Sstevel@tonic-gate 	 */
1484*0Sstevel@tonic-gate 	if (ibmf_i_is_valid_rmpp_status(rmpp_hdr) != B_TRUE) {
1485*0Sstevel@tonic-gate 
1486*0Sstevel@tonic-gate 		/*
1487*0Sstevel@tonic-gate 		 * If the receive buffer is not yet allocated, this is
1488*0Sstevel@tonic-gate 		 * probably the first MAD received for the receive context.
1489*0Sstevel@tonic-gate 		 * We need to set up the receive buffer before calling
1490*0Sstevel@tonic-gate 		 * ibmf_i_send_rmpp() to send an ABORT packet.
1491*0Sstevel@tonic-gate 		 */
1492*0Sstevel@tonic-gate 		if (msgimplp->im_msgbufs_recv.im_bufs_mad_hdr == NULL) {
1493*0Sstevel@tonic-gate 			status = ibmf_setup_recvbuf_on_error(msgimplp, madp);
1494*0Sstevel@tonic-gate 			if (status != IBMF_SUCCESS) {
1495*0Sstevel@tonic-gate 				IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L2,
1496*0Sstevel@tonic-gate 				    ibmf_i_handle_rmpp_err, IBMF_TNF_ERROR, "",
1497*0Sstevel@tonic-gate 				    "ibmf_i_handle_rmpp(): %s\n", tnf_string,
1498*0Sstevel@tonic-gate 				    msg,
1499*0Sstevel@tonic-gate 				    "ibmf_setup_recvbuf_on_error() failed");
1500*0Sstevel@tonic-gate 				return;
1501*0Sstevel@tonic-gate 			}
1502*0Sstevel@tonic-gate 		}
1503*0Sstevel@tonic-gate 
1504*0Sstevel@tonic-gate 		/*
1505*0Sstevel@tonic-gate 		 * Drop the message if the transaction has not yet
1506*0Sstevel@tonic-gate 		 * been identified as a send or receive RMPP transaction.
1507*0Sstevel@tonic-gate 		 * This is because the send completion of an abort packet
1508*0Sstevel@tonic-gate 		 * will hit the non-rmpp code which attempts to reset the
1509*0Sstevel@tonic-gate 		 * RESP timer set after sending the abort packet, causing
1510*0Sstevel@tonic-gate 		 * an assert.
1511*0Sstevel@tonic-gate 		 */
1512*0Sstevel@tonic-gate 		if (((msgimplp->im_flags & IBMF_MSG_FLAGS_RECV_RMPP) == 0) &&
1513*0Sstevel@tonic-gate 		    (msgimplp->im_flags & IBMF_MSG_FLAGS_SEND_RMPP) == 0) {
1514*0Sstevel@tonic-gate 			/*
1515*0Sstevel@tonic-gate 			 * Reset the response timer since we're still
1516*0Sstevel@tonic-gate 			 * waiting for the first response MAD, provided
1517*0Sstevel@tonic-gate 			 * that the send completion has occured
1518*0Sstevel@tonic-gate 			 */
1519*0Sstevel@tonic-gate 			if (msgimplp->im_trans_state_flags &
1520*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE) {
1521*0Sstevel@tonic-gate 				ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
1522*0Sstevel@tonic-gate 				    IBMF_RESP_TIMER);
1523*0Sstevel@tonic-gate 			}
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1526*0Sstevel@tonic-gate 			    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1527*0Sstevel@tonic-gate 			    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1528*0Sstevel@tonic-gate 			    "Invalid RMPP status detected, dropping MAD");
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate 			return;
1531*0Sstevel@tonic-gate 		}
1532*0Sstevel@tonic-gate 
1533*0Sstevel@tonic-gate 		/* abort with status BadT */
1534*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_ABORT,
1535*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_IS, 0, 0, IBMF_NO_BLOCK);
1536*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
1537*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L2,
1538*0Sstevel@tonic-gate 			    ibmf_i_handle_rmpp_err, IBMF_TNF_ERROR, "",
1539*0Sstevel@tonic-gate 			    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1540*0Sstevel@tonic-gate 			    "RMPP ABORT send failed");
1541*0Sstevel@tonic-gate 			msgimplp->im_trans_state_flags |=
1542*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE;
1543*0Sstevel@tonic-gate 		}
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate 		mutex_enter(&clientp->ic_kstat_mutex);
1546*0Sstevel@tonic-gate 		IBMF_ADD32_KSTATS(clientp, rmpp_errors, 1);
1547*0Sstevel@tonic-gate 		mutex_exit(&clientp->ic_kstat_mutex);
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 		IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L2,
1550*0Sstevel@tonic-gate 		    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1551*0Sstevel@tonic-gate 		    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1552*0Sstevel@tonic-gate 		    "Invalid RMPP status detected, sending ABORT IS");
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1555*0Sstevel@tonic-gate 		    ibmf_i_handle_rmpp_end, IBMF_TNF_TRACE, "",
1556*0Sstevel@tonic-gate 		    "ibmf_i_handle_rmpp() exit\n");
1557*0Sstevel@tonic-gate 
1558*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_ABORT;
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 		ibmf_i_unset_timer(msgimplp, IBMF_TRANS_TIMER);
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate 		ibmf_i_set_timer(ibmf_i_err_terminate_timeout,
1563*0Sstevel@tonic-gate 		    msgimplp, IBMF_RESP_TIMER);
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate 		return;
1566*0Sstevel@tonic-gate 	}
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 	/*
1569*0Sstevel@tonic-gate 	 * We could check the MAD here and do an optional abort.
1570*0Sstevel@tonic-gate 	 * This abort if the MAD header is bad is not required by the spec.
1571*0Sstevel@tonic-gate 	 * Also, we should account for RRespTime here.
1572*0Sstevel@tonic-gate 	 */
1573*0Sstevel@tonic-gate 
1574*0Sstevel@tonic-gate 	/*
1575*0Sstevel@tonic-gate 	 * The RMPP engine has four execution flow paths corresponding
1576*0Sstevel@tonic-gate 	 * to the four states the RMPP state machine can be in at any
1577*0Sstevel@tonic-gate 	 * given time.  The packet will be dropped if the context is not in any
1578*0Sstevel@tonic-gate 	 * of these four states.
1579*0Sstevel@tonic-gate 	 */
1580*0Sstevel@tonic-gate 	switch (rmpp_ctx->rmpp_state) {
1581*0Sstevel@tonic-gate 	case IBMF_RMPP_STATE_SENDER_ACTIVE :
1582*0Sstevel@tonic-gate 		ibmf_i_rmpp_sender_active_flow(clientp, qp_hdl, msgimplp, madp);
1583*0Sstevel@tonic-gate 		break;
1584*0Sstevel@tonic-gate 	case IBMF_RMPP_STATE_SENDER_SWITCH :
1585*0Sstevel@tonic-gate 		ibmf_i_rmpp_sender_switch_flow(clientp, qp_hdl, msgimplp, madp);
1586*0Sstevel@tonic-gate 		break;
1587*0Sstevel@tonic-gate 	case IBMF_RMPP_STATE_RECEVR_ACTIVE :
1588*0Sstevel@tonic-gate 		ibmf_i_rmpp_recvr_active_flow(clientp, qp_hdl, msgimplp, madp);
1589*0Sstevel@tonic-gate 		break;
1590*0Sstevel@tonic-gate 	case IBMF_RMPP_STATE_RECEVR_TERMINATE :
1591*0Sstevel@tonic-gate 		ibmf_i_rmpp_recvr_term_flow(clientp, qp_hdl, msgimplp, madp);
1592*0Sstevel@tonic-gate 		break;
1593*0Sstevel@tonic-gate 	default:
1594*0Sstevel@tonic-gate 		/* Including IBMF_RMPP_STATE_ABORT */
1595*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L2,
1596*0Sstevel@tonic-gate 		    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1597*0Sstevel@tonic-gate 		    "ibmf_i_handle_rmpp(): %s, rmpp_state = 0x%x\n",
1598*0Sstevel@tonic-gate 		    tnf_string, msg, "Dropping packet",
1599*0Sstevel@tonic-gate 		    tnf_opaque, rmpp_state, rmpp_ctx->rmpp_state);
1600*0Sstevel@tonic-gate 
1601*0Sstevel@tonic-gate 		/* Reinitiate the resp timer if the state is ABORT */
1602*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_state == IBMF_RMPP_STATE_ABORT) {
1603*0Sstevel@tonic-gate 			ibmf_i_set_timer(ibmf_i_err_terminate_timeout, msgimplp,
1604*0Sstevel@tonic-gate 			    IBMF_RESP_TIMER);
1605*0Sstevel@tonic-gate 
1606*0Sstevel@tonic-gate 			return;
1607*0Sstevel@tonic-gate 		}
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate 		/*
1610*0Sstevel@tonic-gate 		 * Drop the message if the transaction has not yet
1611*0Sstevel@tonic-gate 		 * been identified as a send or receive RMPP transaction.
1612*0Sstevel@tonic-gate 		 */
1613*0Sstevel@tonic-gate 		if (((msgimplp->im_flags & IBMF_MSG_FLAGS_RECV_RMPP) == 0) &&
1614*0Sstevel@tonic-gate 		    (msgimplp->im_flags & IBMF_MSG_FLAGS_SEND_RMPP) == 0) {
1615*0Sstevel@tonic-gate 			/*
1616*0Sstevel@tonic-gate 			 * Reset the response timer since we're still
1617*0Sstevel@tonic-gate 			 * waiting for the first response MAD, provided
1618*0Sstevel@tonic-gate 			 * that the send completion has occured
1619*0Sstevel@tonic-gate 			 */
1620*0Sstevel@tonic-gate 			if (msgimplp->im_trans_state_flags &
1621*0Sstevel@tonic-gate 			    IBMF_TRANS_STATE_FLAG_SEND_DONE) {
1622*0Sstevel@tonic-gate 				ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp,
1623*0Sstevel@tonic-gate 				    IBMF_RESP_TIMER);
1624*0Sstevel@tonic-gate 			}
1625*0Sstevel@tonic-gate 
1626*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L3,
1627*0Sstevel@tonic-gate 			    ibmf_i_handle_rmpp, IBMF_TNF_TRACE, "",
1628*0Sstevel@tonic-gate 			    "ibmf_i_handle_rmpp(): %s\n", tnf_string, msg,
1629*0Sstevel@tonic-gate 			    "BAD 1st RMPP packet, dropping MAD");
1630*0Sstevel@tonic-gate 
1631*0Sstevel@tonic-gate 			return;
1632*0Sstevel@tonic-gate 		}
1633*0Sstevel@tonic-gate 	}
1634*0Sstevel@tonic-gate 
1635*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1636*0Sstevel@tonic-gate 	    ibmf_i_handle_rmpp_end, IBMF_TNF_TRACE, "",
1637*0Sstevel@tonic-gate 	    "ibmf_i_handle_rmpp() exit\n");
1638*0Sstevel@tonic-gate }
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate /*
1641*0Sstevel@tonic-gate  * ibmf_i_send_rmpp():
1642*0Sstevel@tonic-gate  * ibmf_i_send_rmpp() is called to send any
1643*0Sstevel@tonic-gate  * type RMPP packet. The RMPP status is passed in as an argument.
1644*0Sstevel@tonic-gate  * In addition, the segment field and the payload length / new window last
1645*0Sstevel@tonic-gate  * field are passed in as arguments.
1646*0Sstevel@tonic-gate  */
1647*0Sstevel@tonic-gate int
ibmf_i_send_rmpp(ibmf_msg_impl_t * msgimplp,uint8_t rmpp_type,uint8_t rmpp_status,uint32_t segno,uint32_t nwl,int block)1648*0Sstevel@tonic-gate ibmf_i_send_rmpp(ibmf_msg_impl_t *msgimplp, uint8_t rmpp_type,
1649*0Sstevel@tonic-gate     uint8_t rmpp_status, uint32_t segno, uint32_t nwl, int block)
1650*0Sstevel@tonic-gate {
1651*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1652*0Sstevel@tonic-gate 	int		status;
1653*0Sstevel@tonic-gate 
1654*0Sstevel@tonic-gate 	IBMF_TRACE_5(IBMF_TNF_DEBUG, DPRINT_L4, ibmf_i_send_rmpp_start,
1655*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "",
1656*0Sstevel@tonic-gate 	    "ibmf_i_send_rmpp(): msgp = 0x%p, rmpp_type = 0x%x, "
1657*0Sstevel@tonic-gate 	    "rmpp_status = %d, segno = %d, nwl = %d\n",
1658*0Sstevel@tonic-gate 	    tnf_opaque, msg, msgimplp,
1659*0Sstevel@tonic-gate 	    tnf_uint, rmpp_type, rmpp_type,
1660*0Sstevel@tonic-gate 	    tnf_uint, rmpp_status, rmpp_status,
1661*0Sstevel@tonic-gate 	    tnf_uint, segno, segno,
1662*0Sstevel@tonic-gate 	    tnf_uint, nwl, nwl);
1663*0Sstevel@tonic-gate 
1664*0Sstevel@tonic-gate 	IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_send_rmpp,
1665*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp(): msgp = 0x%p, "
1666*0Sstevel@tonic-gate 	    "next_seg = 0x%x, num_pkts = %d\n",
1667*0Sstevel@tonic-gate 	    tnf_opaque, msg, msgimplp,
1668*0Sstevel@tonic-gate 	    tnf_uint, next_seg, msgimplp->im_rmpp_ctx.rmpp_ns,
1669*0Sstevel@tonic-gate 	    tnf_uint, num_pkts, msgimplp->im_rmpp_ctx.rmpp_num_pkts);
1670*0Sstevel@tonic-gate 
1671*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&msgimplp->im_mutex));
1672*0Sstevel@tonic-gate 
1673*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_type = rmpp_type;
1674*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_status = rmpp_status;
1675*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_word3 = segno;
1676*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_word4 = nwl;
1677*0Sstevel@tonic-gate 
1678*0Sstevel@tonic-gate 	/*
1679*0Sstevel@tonic-gate 	 * send packet without blocking
1680*0Sstevel@tonic-gate 	 */
1681*0Sstevel@tonic-gate 	status = ibmf_i_send_pkt(msgimplp->im_client, msgimplp->im_qp_hdl,
1682*0Sstevel@tonic-gate 	    msgimplp, block);
1683*0Sstevel@tonic-gate 	if (status != IBMF_SUCCESS) {
1684*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_NODEBUG, DPRINT_L1,
1685*0Sstevel@tonic-gate 		    ibmf_i_send_rmpp_err, IBMF_TNF_ERROR, "",
1686*0Sstevel@tonic-gate 		    "ibmf_i_send_rmpp(): %s, status = %d\n", tnf_string, msg,
1687*0Sstevel@tonic-gate 		    "unable to send packet", tnf_uint, status, status);
1688*0Sstevel@tonic-gate 		IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,	ibmf_i_send_rmpp_end,
1689*0Sstevel@tonic-gate 		    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp() exit\n");
1690*0Sstevel@tonic-gate 		return (status);
1691*0Sstevel@tonic-gate 	}
1692*0Sstevel@tonic-gate 
1693*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,	ibmf_i_send_rmpp_end,
1694*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp() exit\n");
1695*0Sstevel@tonic-gate 
1696*0Sstevel@tonic-gate 	return (IBMF_SUCCESS);
1697*0Sstevel@tonic-gate }
1698*0Sstevel@tonic-gate 
1699*0Sstevel@tonic-gate /*
1700*0Sstevel@tonic-gate  * ibmf_i_send_rmpp_window():
1701*0Sstevel@tonic-gate  *	Send an RMPP protocol window of packets
1702*0Sstevel@tonic-gate  */
1703*0Sstevel@tonic-gate void
ibmf_i_send_rmpp_window(ibmf_msg_impl_t * msgimplp,int block)1704*0Sstevel@tonic-gate ibmf_i_send_rmpp_window(ibmf_msg_impl_t *msgimplp, int block)
1705*0Sstevel@tonic-gate {
1706*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1707*0Sstevel@tonic-gate 	int status, i, numpkts = rmpp_ctx->rmpp_wl - rmpp_ctx->rmpp_ns + 1;
1708*0Sstevel@tonic-gate 	uint32_t	payload_length, cl_hdr_sz, cl_hdr_off;
1709*0Sstevel@tonic-gate 
1710*0Sstevel@tonic-gate 	IBMF_TRACE_1(IBMF_TNF_DEBUG, DPRINT_L4, ibmf_i_send_rmpp_window_start,
1711*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp_window(): msgp = 0x%p\n",
1712*0Sstevel@tonic-gate 	    tnf_opaque, msg, msgimplp);
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 	IBMF_TRACE_3(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_send_rmpp_window,
1715*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_handle_rmpp(): wl = %d "
1716*0Sstevel@tonic-gate 	    "ns = %d, num_pkts = %d\n", tnf_uint, wl, rmpp_ctx->rmpp_wl,
1717*0Sstevel@tonic-gate 	    tnf_uint, ns, rmpp_ctx->rmpp_ns, tnf_uint, num_pkts,
1718*0Sstevel@tonic-gate 	    rmpp_ctx->rmpp_num_pkts);
1719*0Sstevel@tonic-gate 
1720*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&msgimplp->im_mutex));
1721*0Sstevel@tonic-gate 
1722*0Sstevel@tonic-gate 	ibmf_i_mgt_class_to_hdr_sz_off(
1723*0Sstevel@tonic-gate 	    msgimplp->im_msgbufs_send.im_bufs_mad_hdr->MgmtClass,
1724*0Sstevel@tonic-gate 	    &cl_hdr_sz, &cl_hdr_off);
1725*0Sstevel@tonic-gate 
1726*0Sstevel@tonic-gate 	for (i = 1; i <= numpkts; i++) {
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate 		if (rmpp_ctx->rmpp_ns == 1)
1729*0Sstevel@tonic-gate 			payload_length = rmpp_ctx->rmpp_pyld_len +
1730*0Sstevel@tonic-gate 			    (rmpp_ctx->rmpp_num_pkts * cl_hdr_sz);
1731*0Sstevel@tonic-gate 		else if (rmpp_ctx->rmpp_ns == rmpp_ctx->rmpp_num_pkts)
1732*0Sstevel@tonic-gate 			payload_length = rmpp_ctx->rmpp_last_pkt_sz + cl_hdr_sz;
1733*0Sstevel@tonic-gate 		else
1734*0Sstevel@tonic-gate 			payload_length = rmpp_ctx->rmpp_pkt_data_sz;
1735*0Sstevel@tonic-gate 
1736*0Sstevel@tonic-gate 		IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_send_rmpp_window,
1737*0Sstevel@tonic-gate 		    IBMF_TNF_TRACE, "", "ibmf_i_handle_rmpp(): ns = %d, "
1738*0Sstevel@tonic-gate 		    "pl = %d\n", tnf_uint, ns, rmpp_ctx->rmpp_ns,
1739*0Sstevel@tonic-gate 		    tnf_uint, pl, payload_length);
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 		status = ibmf_i_send_rmpp(msgimplp, IBMF_RMPP_TYPE_DATA,
1742*0Sstevel@tonic-gate 		    IBMF_RMPP_STATUS_NORMAL, rmpp_ctx->rmpp_ns, payload_length,
1743*0Sstevel@tonic-gate 		    block);
1744*0Sstevel@tonic-gate 		if (status != IBMF_SUCCESS) {
1745*0Sstevel@tonic-gate 
1746*0Sstevel@tonic-gate 			IBMF_TRACE_1(IBMF_TNF_NODEBUG, DPRINT_L1,
1747*0Sstevel@tonic-gate 			    ibmf_i_send_rmpp_window_err, IBMF_TNF_ERROR, "",
1748*0Sstevel@tonic-gate 			    "ibmf_i_send_rmpp_window(): %s\n", tnf_string, msg,
1749*0Sstevel@tonic-gate 			    "Send rmpp window failed");
1750*0Sstevel@tonic-gate 			IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,
1751*0Sstevel@tonic-gate 			    ibmf_i_send_rmpp_window_end, IBMF_TNF_TRACE, "",
1752*0Sstevel@tonic-gate 			    "ibmf_i_send_rmpp_window() exit\n");
1753*0Sstevel@tonic-gate 			return;
1754*0Sstevel@tonic-gate 		}
1755*0Sstevel@tonic-gate 
1756*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_ns++;
1757*0Sstevel@tonic-gate 
1758*0Sstevel@tonic-gate 		rmpp_ctx->rmpp_data_offset += rmpp_ctx->rmpp_pkt_data_sz;
1759*0Sstevel@tonic-gate 	}
1760*0Sstevel@tonic-gate 
1761*0Sstevel@tonic-gate 	/* Set the response timer */
1762*0Sstevel@tonic-gate 	IBMF_TRACE_2(IBMF_TNF_DEBUG, DPRINT_L3, ibmf_i_send_rmpp_window,
1763*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_handle_rmpp(): setting timer %p %d\n",
1764*0Sstevel@tonic-gate 	    tnf_opaque, msgimplp, msgimplp, tnf_opaque, timeout_id,
1765*0Sstevel@tonic-gate 	    msgimplp->im_rp_timeout_id);
1766*0Sstevel@tonic-gate 
1767*0Sstevel@tonic-gate 	ibmf_i_set_timer(ibmf_i_send_timeout, msgimplp, IBMF_RESP_TIMER);
1768*0Sstevel@tonic-gate 
1769*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,	ibmf_i_send_rmpp_window_end,
1770*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp_window() exit\n");
1771*0Sstevel@tonic-gate }
1772*0Sstevel@tonic-gate 
1773*0Sstevel@tonic-gate /*
1774*0Sstevel@tonic-gate  * ibmf_i_send_rmpp_pkts():
1775*0Sstevel@tonic-gate  *	Send a message using the RMPP protocol
1776*0Sstevel@tonic-gate  */
1777*0Sstevel@tonic-gate int
ibmf_i_send_rmpp_pkts(ibmf_client_t * clientp,ibmf_qp_handle_t ibmf_qp_handle,ibmf_msg_impl_t * msgimplp,boolean_t isDS,int block)1778*0Sstevel@tonic-gate ibmf_i_send_rmpp_pkts(ibmf_client_t *clientp, ibmf_qp_handle_t ibmf_qp_handle,
1779*0Sstevel@tonic-gate     ibmf_msg_impl_t *msgimplp, boolean_t isDS, int block)
1780*0Sstevel@tonic-gate {
1781*0Sstevel@tonic-gate 	ibmf_rmpp_ctx_t	*rmpp_ctx = &msgimplp->im_rmpp_ctx;
1782*0Sstevel@tonic-gate 	size_t		buf_sz = msgimplp->im_msgbufs_send.im_bufs_cl_data_len;
1783*0Sstevel@tonic-gate 	uint32_t	num_pkts, resid;
1784*0Sstevel@tonic-gate 	uint32_t	cl_hdr_sz, cl_data_sz, cl_hdr_off;
1785*0Sstevel@tonic-gate 
1786*0Sstevel@tonic-gate 	IBMF_TRACE_4(IBMF_TNF_DEBUG, DPRINT_L4, ibmf_i_send_rmpp_pkts_start,
1787*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp_pkts(): clientp = 0x%p, "
1788*0Sstevel@tonic-gate 	    "qphdl = 0x%p, msgp = 0x%p, block = %d\n",
1789*0Sstevel@tonic-gate 	    tnf_opaque, clientp, clientp, tnf_opaque, qphdl, ibmf_qp_handle,
1790*0Sstevel@tonic-gate 	    tnf_opaque, msg, msgimplp, tnf_uint, block, block);
1791*0Sstevel@tonic-gate 
1792*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&msgimplp->im_mutex));
1793*0Sstevel@tonic-gate 
1794*0Sstevel@tonic-gate 	ibmf_i_mgt_class_to_hdr_sz_off(
1795*0Sstevel@tonic-gate 	    msgimplp->im_msgbufs_send.im_bufs_mad_hdr->MgmtClass,
1796*0Sstevel@tonic-gate 	    &cl_hdr_sz, &cl_hdr_off);
1797*0Sstevel@tonic-gate 
1798*0Sstevel@tonic-gate 	cl_data_sz = MAD_SIZE_IN_BYTES - sizeof (ib_mad_hdr_t) - cl_hdr_off -
1799*0Sstevel@tonic-gate 	    cl_hdr_sz;
1800*0Sstevel@tonic-gate 
1801*0Sstevel@tonic-gate 	if ((resid = (buf_sz % cl_data_sz)) != 0)
1802*0Sstevel@tonic-gate 		num_pkts = (buf_sz / cl_data_sz) + 1;
1803*0Sstevel@tonic-gate 	else {
1804*0Sstevel@tonic-gate 		if (buf_sz > 0)
1805*0Sstevel@tonic-gate 			num_pkts = buf_sz / cl_data_sz;
1806*0Sstevel@tonic-gate 		else
1807*0Sstevel@tonic-gate 			num_pkts = 1;
1808*0Sstevel@tonic-gate 	}
1809*0Sstevel@tonic-gate 
1810*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_wf = 1;
1811*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_wl = 1;
1812*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_ns = 1;
1813*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_is_ds = isDS;
1814*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_pyld_len = buf_sz;
1815*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_state = IBMF_RMPP_STATE_SENDER_ACTIVE;
1816*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_type = IBMF_RMPP_TYPE_DATA;
1817*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_respt = IBMF_RMPP_TERM_RRESPT;
1818*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_status = IBMF_RMPP_STATUS_NORMAL;
1819*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_num_pkts = num_pkts;
1820*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_pkt_data_sz =
1821*0Sstevel@tonic-gate 	    (buf_sz < cl_data_sz) ? buf_sz : cl_data_sz;
1822*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_last_pkt_sz =
1823*0Sstevel@tonic-gate 	    (resid == 0) ? ((buf_sz == 0) ? 0 : cl_data_sz) : resid;
1824*0Sstevel@tonic-gate 	rmpp_ctx->rmpp_data_offset = 0;
1825*0Sstevel@tonic-gate 
1826*0Sstevel@tonic-gate 	ibmf_i_send_rmpp_window(msgimplp, block);
1827*0Sstevel@tonic-gate 
1828*0Sstevel@tonic-gate 	IBMF_TRACE_0(IBMF_TNF_DEBUG, DPRINT_L4,	ibmf_i_send_rmpp_pkts_end,
1829*0Sstevel@tonic-gate 	    IBMF_TNF_TRACE, "", "ibmf_i_send_rmpp_pkts() exit\n");
1830*0Sstevel@tonic-gate 
1831*0Sstevel@tonic-gate 	return (IBMF_SUCCESS);
1832*0Sstevel@tonic-gate }
1833