1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #ifndef _SYS_IB_MGT_IBCM_IBCM_IMPL_H
26 #define	_SYS_IB_MGT_IBCM_IBCM_IMPL_H
27 
28 /*
29  * ibcm_impl.h
30  *
31  * This file contains all of the internal data structures and
32  * definitions for IBCM.
33  *
34  * The general state transition processing of CM is achieved by the
35  * following callgraph:
36  *
37  * CM INIT : Register for hca attach and detach callbacks, and other asyncs
38  *
39  * On new HCA attach:	Register with IBMF on all ports of upcoming HCA
40  *			Specify CM callback and callback "per HCA arg"
41  *			Register with SA, allocate AVL trees etc.
42  *
43  * IBMF Callback
44  *  	Validate combination of method and attribute Id in the generic MAD hdr
45  *	-> Call CM Connection state transition function based on attribute ID
46  *	    Create/lookup/delete CM state structure and save it into avl tree
47  *	    Handle duplicate messages and MRA to adjust timers etc.
48  *	    Handle stale connections
49  *	    Allocate reply MADs
50  *		-> Call CM QP/EEC state transition function based on CM message
51  *		     Change QP/EEC state  (to enable recvQ posting by client)
52  *		     Call Client/Server handler callback function
53  *		     Modify QP/EEC attributes
54  *		     Optionally fill up some fields of response MAD
55  *	    Post reply MADs
56  *	    Store reply MADs and reply MAD address, if necessary
57  *	    Initialize timeouts for the message
58  *	    Change CM state
59  *	    Deallocate reply MADs
60  *
61  * NOTES:
62  * 	o There are *NO* explicit CM allocation and deallocation routines for
63  *	CM MADs and state data structures
64  *	o CM timeouts are scheduled using timeout(9f), and cancelled using
65  *	untimeout(9f)
66  *	o svc_id allocation scheme
67  *	A new counter for svcid is maintained in ibcm_hca_info_t
68  *	which is used to allocate svcid. The svcids are incremented
69  *	sequentially and allocated (with wrap around on overflow) with
70  *	these considerations:
71  *		The WellKnown service id's and locally allocated svcid's
72  *		could be maintained in separate lists, thus allowing the
73  *		lists to be kept apart and sorted easily.
74  *		The insertions are done at the end of the list
75  *	o reqid allocation scheme
76  *	The list is a sorted one (as reqid's are allocated sequentially).
77  *	If there is a code required for wrap around, it would search for
78  *	a reqid from the head of the list.
79  *	The insertions are always done at the end of the lists
80  *	o XXX svc_id allocation scheme and req_id allocation scheme will
81  *	be revisited.
82  */
83 
84 #include <sys/sysmacros.h>
85 #include <sys/systm.h>
86 #include <sys/kmem.h>
87 #include <sys/modctl.h>
88 #include <sys/avl.h>
89 #include <sys/taskq.h>
90 #include <sys/vmem.h>
91 #include <sys/note.h>
92 #include <sys/t_lock.h>
93 
94 #include <sys/ib/ibtl/ibvti.h>
95 #include <sys/ib/ibtl/impl/ibtl_cm.h>
96 #include <sys/ib/ibtl/impl/ibtl_util.h>
97 #include <sys/ib/mgt/ibmf/ibmf.h>
98 #include <sys/ib/mgt/ibcm/ibcm_trace.h>
99 #include <inet/ip.h>
100 
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 
105 _NOTE(SCHEME_PROTECTS_DATA("Private", sa_service_record_s))
106 _NOTE(SCHEME_PROTECTS_DATA("Exclusive access to ibmf msg buf based on state",
107 ib_mad_hdr_t))
108 _NOTE(SCHEME_PROTECTS_DATA("Exclusive access to ibmf msg buf based on state",
109 _ibmf_msg))
110 
111 /*
112  * Defines for all CM state machine states, as defined in
113  * section 12.9.7. IBCM_REJ_SENT is a state not defined in
114  * the spec and is added for implementation purposes.
115  */
116 typedef enum ibcm_conn_state_e {
117 	/* Initial states */
118 	IBCM_STATE_IDLE			= 0,
119 	IBCM_STATE_LISTEN,
120 
121 	/* States during connection establishment */
122 	IBCM_STATE_REQ_SENT,
123 	IBCM_STATE_REQ_RCVD,
124 	IBCM_STATE_REP_SENT,
125 	IBCM_STATE_REP_RCVD,
126 	IBCM_STATE_REP_WAIT,
127 	IBCM_STATE_MRA_SENT,
128 	IBCM_STATE_MRA_REP_SENT,
129 	IBCM_STATE_MRA_REP_RCVD,
130 
131 	/* States during connection establishment failures */
132 	IBCM_STATE_TIMED_OUT,
133 	IBCM_STATE_ABORTED,
134 	IBCM_STATE_REJ_SENT,
135 
136 	/* Established state */
137 	IBCM_STATE_TRANSIENT_ESTABLISHED,
138 	IBCM_STATE_ESTABLISHED,
139 
140 	/* States during connection teardown */
141 	IBCM_STATE_TRANSIENT_DREQ_SENT,
142 	IBCM_STATE_DREQ_SENT,
143 	IBCM_STATE_DREQ_RCVD,
144 	IBCM_STATE_DREP_RCVD,
145 	IBCM_STATE_TIMEWAIT,
146 
147 	/* states for UD side of things */
148 	IBCM_STATE_SIDR_REQ_SENT,
149 	IBCM_STATE_SIDR_REQ_RCVD,
150 	IBCM_STATE_SIDR_REP_SENT,
151 	IBCM_STATE_SIDR_REP_RCVD,
152 
153 	/* states common to RC and UD, during state resource deletion */
154 	IBCM_STATE_DELETE
155 } ibcm_conn_state_t;
156 
157 /* Defines the AP states for LAP/APR */
158 typedef enum ibcm_ap_state_e {
159 	IBCM_AP_STATE_IDLE	= 0x0,
160 	IBCM_AP_STATE_LAP_SENT,
161 	IBCM_AP_STATE_LAP_RCVD,
162 	IBCM_AP_STATE_APR_RCVD,
163 	IBCM_AP_STATE_MRA_LAP_RCVD,
164 	IBCM_AP_STATE_MRA_LAP_SENT,
165 	IBCM_AP_STATE_TIMED_OUT
166 } ibcm_ap_state_t;
167 
168 /*
169  * Defines for the CM event types/MAD attribute IDs
170  */
171 typedef enum ibcm_event_type_e {
172 	IBCM_INCOMING_REQ	= 0x0,
173 	IBCM_INCOMING_MRA	= 0x1,
174 	IBCM_INCOMING_REJ	= 0x2,
175 	IBCM_INCOMING_REP	= 0x3,
176 	IBCM_INCOMING_RTU	= 0x4,
177 	IBCM_INCOMING_DREQ	= 0x5,
178 	IBCM_INCOMING_DREP	= 0x6,
179 	IBCM_INCOMING_SIDR_REQ	= 0x7,
180 	IBCM_INCOMING_SIDR_REP	= 0x8,
181 	IBCM_INCOMING_LAP	= 0x9,
182 	IBCM_INCOMING_APR	= 0xA,
183 	IBCM_OUTGOING_REQ	= 0xB,	/* REQ Sent on active CM side */
184 	IBCM_INCOMING_REQ_STALE	= 0xC,	/* lookup by remote HCA and */
185 					/* remote comid */
186 	IBCM_INCOMING_REP_STALE	= 0xD,	/* lookup by passive HCA and QPN */
187 	IBCM_INCOMING_REJ_RCOMID = 0xE	/* lookup by remote com id */
188 } ibcm_event_type_t;
189 
190 /*
191  * IBMF calls back into CM on only the first 11 events defined in
192  * ibcm_event_type_t. CM has pre-defined functions for these 11 events
193  *
194  */
195 #define	IBCM_MAX_EVENTS		11
196 
197 /*
198  * CM message attribute IDs begin at this "base ID". The first 11 event types
199  * in ibcm_event_type_t are CM protocol messages that are posted to IBMF by
200  * adding the "base_id" to the respective event type value. By subtracting
201  * the "base_id" in IBMF callback in CM MAD, the message type is gotten back
202  */
203 #define	IBCM_ATTR_BASE_ID		0x10
204 
205 #define	IBCM_MAX_RETRY_CNT		15
206 #define	IBCM_ATTRID_FIELD_SIZE		4
207 #define	IBCM_TRANID_PRIV_FIELD_SIZE	28
208 
209 #define	IBCM_RNR_RETRY_CNT_MASK		0x7	/* 3 bits */
210 #define	IBCM_MAX_RNR_RETRY_CNT		7
211 
212 #define	IBCM_INITIAL_COMID		1
213 #define	IBCM_INITIAL_REQID		1
214 #define	IBCM_INITIAL_SID		1
215 
216 /*
217  * Maximum number of com ids / req ids that can be active at any given time
218  * MUST ENSURE THAT (INITIAL ID + MAX IDS -1), for any of the IDs does not
219  * exceed the max 32 bit
220  */
221 
222 /* An hca can have max of 2^24 -2  RC connections */
223 #define	IBCM_MAX_COMIDS		(0x01000000 - 2)
224 #define	IBCM_MAX_REQIDS		0xFFFFFFFF
225 #define	IBCM_MAX_LOCAL_SIDS	0xFFFFFFFF
226 #define	IBCM_MAX_IP_SIDS	0xFFFF
227 
228 typedef uint32_t ib_com_id_t;	/* CM Communication ID */
229 
230 /*
231  * Defines the CM Mode of operation for a connection
232  */
233 typedef enum ibcm_mode_e {
234 	IBCM_ACTIVE_MODE	= 1,	/* Active side CM */
235 	IBCM_PASSIVE_MODE	= 2	/* Passive side CM */
236 } ibcm_mode_t;
237 
238 
239 /* different IBCM return values */
240 typedef enum ibcm_status_e {
241 	IBCM_SUCCESS  		= 0,	/* good status */
242 	IBCM_LOOKUP_EXISTS,		/* statep lookup found existing entry */
243 	IBCM_LOOKUP_NEW,		/* lookup created new statep entry */
244 	IBCM_LOOKUP_FAIL,		/* lookup found no statep entry */
245 	IBCM_SEND_REJ,			/* CM QP state change sent REJ msg */
246 	IBCM_SEND_REP,			/* CM QP state change sent REP msg */
247 	IBCM_SEND_RTU,			/* CM QP state change sent RTU msg */
248 	IBCM_SEND_APR,			/* CM to send APR MAD as response */
249 	IBCM_SEND_SIDR_REP, 		/* client's UD handler returned this */
250 	IBCM_DEFER,			/* client's handler returned this */
251 	IBCM_FAILURE			/* generic IBCM failure */
252 } ibcm_status_t;
253 
254 /*
255  * Struct definition for addressing information that CM maintains for
256  * each of the incoming MADs
257  */
258 typedef	struct	ibcm_mad_addr {
259 	ibmf_global_addr_info_t	grh_hdr;	/* GRH related fields of MAD */
260 	ibmf_addr_info_t	rcvd_addr;	/* Outgoing/Incoming MAD addr */
261 	ibmf_handle_t		ibmf_hdl;	/* IBMF handle */
262 	boolean_t		grh_exists;	/* TRUE if grh exists */
263 	uint8_t			port_num;
264 	struct ibcm_qp_list_s	*cm_qp_entry;	/* IBMF hdl on which MAD rcvd */
265 						/* or on which MAD shall be */
266 						/* sent out */
267 } ibcm_mad_addr_t;
268 
269 _NOTE(READ_ONLY_DATA(ibcm_mad_addr))
270 
271 #define	IBCM_MAD_SIZE		0x100			/* size of MAD */
272 #define	IBCM_MAD_HDR_SIZE	sizeof (ib_mad_hdr_t)	/* size of MAD HDR */
273 #define	IBCM_MSG_SIZE		IBCM_MAD_SIZE-IBCM_MAD_HDR_SIZE
274 
275 typedef enum ibcm_abort_flag_e {
276 	IBCM_ABORT_INIT		= 0,	/* no abort flag is set */
277 	IBCM_ABORT_CLIENT	= 1,	/* client requested connection abort */
278 	IBCM_ABORT_REJ		= 2	/* REJ received with timeout reason */
279 } ibcm_abort_flag_t;
280 
281 typedef	enum ibcm_isync_e {
282 	IBCM_BLOCK	= 0,	/* Block cm operation */
283 	IBCM_UNBLOCK	= 1,	/* Unblock cm operation */
284 	IBCM_FAIL	= 2	/* fail cm operation */
285 } ibcm_isync_t;
286 
287 /*
288  * Define a connection state structure, used by the IBTF CM
289  * to maintain state about connected QPs.
290  *
291  * mode			: CM connection mode active/passive
292  * state		: CM connection state
293  * ap_state		: CM AP Internal state to manage LAP/APR state machine
294  * state_mutex		: lock for this structure
295  * channel		: Channel associated with this RC state structure
296  * ref_cnt		: Number of active threads that may reference this
297  *			  state structure
298  * svcid		: Service ID
299  * cm_handler		: Client handler callback address
300  * stored_reply_addr	: Address for replying using the stored mad
301  * hcap			: A pointer to the HCA's entry
302  * stored_msg		: Stores the response REP/REJ/RTU MAD
303  * mra_msg		: Stores the response MRA MAD
304  * dreq_msg		: Stores the DREQ MAD
305  * drep_msg		: Stores the DREP MAD
306  * lapr_msg		: Stores the LAP/APR MAD
307  *			  detect duplicate LAP messages
308  * local_comid  	: Local communication id
309  * local_hca_guid	: Local HCA GUID
310  * local_qpn		: Local QPN
311  *
312  * remote_comid 	: Remote communication id
313  * remote_hca_guid	: Remote HCA GUID
314  * remote_qpn		: Remote QPN
315  *
316  * timerid		: Timer id for the timeout either for re-sending the
317  *			  stored mad or deleting the stored mad
318  *			  Ex: A REJ/RTU response for an incoming REP
319  *			      A REP response to an incoming REQ
320  *			      An outgoing REQ on active connection side
321  * timer_value		: Time for any of the above timers in HZ
322  * pkt_life_time	: pkt life time from source to destination
323  * remote_ack_delay	: Remote hca's ack delay in clock_t
324  * rc_alt_pkt_lt	: Life time for new ALT path specified in LAP
325  * stale_clock		: clock used to detect stale vs duplicate REQs
326  * timer_stored_state	: state of connection for timeout() validation
327  * timer_stored_ap_state: CM ap_state for timeout validation
328  * remaining_retry_count: Remaining count for retries ie., posting stored MADs
329  * max_cm_retries	: Max retry count for sending a REQ/REP/DREQ
330  * delete_mra_msg	: Set to TRUE for deletion, if MRA re-send in progress
331  * resend_mad		: B_TRUE, if REQ/REP/RTU/REJ MAD re-send is in progress
332  * resend_mra_mad	: B_TRUE, if a MRA mad re-sens is in progress
333  * cep_retry_cnt	: Retry count for CEP.
334  * stale		: B_TRUE, if connection has become stale
335  * blocking_done	: B_TRUE, if cv_signal been issued to block_client_cv
336  * clnt_hdl		: Clnt_hdl passed in ibt_open_channel
337  * return_data		: RC return args, valid for blocking
338  *			  ibt_open_channel
339  * drep_priv_data;	: The pointer to client specified outgoing private
340  *			  data, from close channel API call
341  * drep_priv_data_len   : The length of DREP private data that client would
342  *			  like to be returned from close channel API call
343  * delete_state_data	: B_TRUE, if CM decides to delete state data, but
344  *			  there is some thread that could access state data
345  *
346  * avl_active_link	: For inserting this state-data into active AVL tree
347  * avl_passive_link	: For inserting this state-data into passive AVL tree
348  * Note : All timer values that are of type "clock_t" below are in usecs
349  */
350 typedef struct ibcm_state_data_s {
351 	/* for AVL tree */
352 	avl_node_t		avl_active_link;
353 	avl_node_t		avl_passive_link;
354 	avl_node_t		avl_passive_comid_link;
355 
356 	/* remote stuff */
357 	ib_guid_t		remote_hca_guid;
358 	ib_com_id_t		remote_comid;
359 	ib_qpn_t		remote_qpn;
360 
361 	/* local stuff */
362 	ib_com_id_t		local_comid;
363 	ib_qpn_t		local_qpn;
364 	ib_guid_t		local_hca_guid;
365 
366 	ibcm_mode_t		mode;
367 	ibcm_conn_state_t	state;
368 	ibcm_ap_state_t		ap_state;
369 	kmutex_t		state_mutex;
370 	ibt_channel_hdl_t	channel;	/* save a copy */
371 
372 	/* ref_cnt so others cannot delete a statep that may be referenced */
373 	int			ref_cnt;
374 
375 	ib_svc_id_t		svcid;
376 	ibt_cm_handler_t	cm_handler;
377 
378 	ibcm_mad_addr_t		stored_reply_addr;
379 
380 	struct ibcm_hca_info_s *hcap;
381 
382 	ibmf_msg_t		*stored_msg;
383 	ibmf_msg_t		*mra_msg;
384 	ibmf_msg_t		*dreq_msg;
385 	ibmf_msg_t		*drep_msg;
386 	ibmf_msg_t		*lapr_msg;
387 
388 	void			*defer_cm_msg;
389 
390 	/* timeout related stuff */
391 	timeout_id_t		timerid;
392 	clock_t			timer_value;
393 	clock_t			pkt_life_time;
394 	clock_t			remote_ack_delay;
395 	clock_t			rc_alt_pkt_lt;
396 
397 	hrtime_t		stale_clock;
398 	hrtime_t		post_time;
399 	hrtime_t		mra_time;
400 
401 	ibcm_conn_state_t	timer_stored_state;
402 	ibcm_ap_state_t		timer_stored_ap_state;
403 	uint8_t			remaining_retry_cnt;
404 	uint8_t			max_cm_retries;
405 	uint8_t			cm_retries;
406 
407 	uint8_t			drep_in_progress;
408 
409 	/* some cep stuff, stored here temporarily during connection est  */
410 	uint8_t			cep_retry_cnt:3;
411 	ibt_srate_t		local_srate;
412 	ibt_srate_t		local_alt_srate;
413 	ib_pkey_t		pkey;
414 	uint8_t			prim_port;
415 	uint8_t			alt_port;
416 	uint32_t		starting_psn;
417 	ib_path_bits_t		prim_src_path_bits;
418 	ib_path_bits_t		alt_src_path_bits;
419 
420 	boolean_t		delete_mra_msg;
421 	boolean_t		stale;
422 	boolean_t		delete_state_data;
423 	boolean_t		is_this_ofuv_chan;
424 
425 	boolean_t		open_done;
426 	boolean_t		close_done;
427 	boolean_t		ap_done;
428 
429 	uint8_t			send_mad_flags;
430 	uint8_t			close_flow;
431 	uint8_t			open_flow;
432 	ibcm_abort_flag_t	abort_flag;
433 
434 	struct ibcm_state_data_s	*timeout_next;
435 
436 	ibcm_conn_state_t	timedout_state;
437 
438 	ibcm_isync_t		cep_in_rts;
439 	ibcm_isync_t		clnt_proceed;
440 	ibcm_isync_t		close_nocb_state;
441 
442 	/* Clients' information */
443 	void			*state_cm_private;
444 
445 	/* pointer to service info */
446 	struct ibcm_svc_info_s  *state_svc_infop;
447 
448 	kcondvar_t		block_client_cv;
449 	kcondvar_t		block_mad_cv;
450 
451 	/* Data for recycle function */
452 	struct ibcm_taskq_recycle_arg_s	*recycle_arg;
453 
454 	/* Return data pointers in various cm api calls */
455 	ibt_rc_returns_t	*open_return_data;
456 	ibt_ap_returns_t	*ap_return_data;
457 	uint8_t			*close_ret_priv_data;
458 	ibt_priv_data_len_t	*close_ret_priv_data_len;
459 	uint8_t			*close_ret_status;
460 
461 	/* for queuing of open_rc_channel requests */
462 	struct ibcm_state_data_s	*open_link;
463 	/* for queuing of non-blocking close_rc_channel requests */
464 	struct ibcm_state_data_s	*close_link;
465 
466 	struct ibcm_conn_trace_s	*conn_trace;
467 
468 	/* For ibt_ofuvcm_get_req_data() */
469 	void			*req_msgp;
470 
471 	/* Stored RNR retry count from incoming REQ or REP */
472 	ibt_rnr_retry_cnt_t	local_qp_rnr_cnt;
473 
474 } ibcm_state_data_t;
475 
476 _NOTE(MUTEX_PROTECTS_DATA(ibcm_state_data_s::state_mutex,
477     ibcm_state_data_s::{state ref_cnt timer_stored_state timer_value
478     timer_stored_ap_state remaining_retry_cnt clnt_proceed cep_in_rts
479     close_nocb_state block_client_cv block_mad_cv timedout_state cm_handler
480     abort_flag mra_msg}))
481 
482 _NOTE(READ_ONLY_DATA(ibcm_state_data_s::{mode channel svcid hcap
483     local_comid local_hca_guid local_qpn remote_comid remote_hca_guid
484     remote_qpn pkt_life_time remote_ack_delay rc_alt_pkt_lt stored_reply_addr
485     max_cm_retries cep_retry_cnt local_srate local_alt_srate pkey
486     prim_port alt_port starting_psn state_svc_infop avl_active_link
487     avl_passive_link avl_passive_comid_link defer_cm_msg recycle_arg
488     conn_trace}))
489 
490 _NOTE(SCHEME_PROTECTS_DATA("Serailized access by block_client_cv",
491     ibcm_state_data_s::{open_return_data ap_return_data close_ret_priv_data
492     close_ret_priv_data_len close_ret_status}))
493 
494 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibcm_state_data_s::{timedout_state
495     cm_handler mra_msg abort_flag local_qp_rnr_cnt}))
496 
497 /*
498  * Definitions for send mad flags. Respective bits in send_mad_flags or
499  * ud_send_mad_flags are set to 1, during MAD transmission, and reset in
500  * ibmf send completion callback or on completion of a blocking ibmf mad post.
501  */
502 #define	IBCM_REP_POST_BUSY	1	/* REP post in progress */
503 #define	IBCM_REJ_POST_BUSY	2	/* REJ post in progress */
504 #define	IBCM_RTU_POST_BUSY	4	/* RTU post in progress */
505 #define	IBCM_MRA_POST_BUSY	8	/* MRA post in progress */
506 #define	IBCM_DREP_POST_BUSY	16	/* DREQ post in progress */
507 #define	IBCM_SREP_POST_BUSY	32	/* SIDR REP post in progress */
508 
509 /* MADs that are retransmitted only because of a timeout */
510 #define	IBCM_REQ_POST_BUSY	64	/* REQ post in progress */
511 
512 
513 /* Incr/Decr ref_cnt by 1 */
514 #define	IBCM_REF_CNT_INCR(s)	(s->ref_cnt++)
515 #define	IBCM_REF_CNT_DECR(s)	\
516 	if ((--(s->ref_cnt) == 0) && (s->delete_state_data == B_TRUE)) { \
517 		ibcm_add_tlist(s);\
518 	} \
519 	ASSERT(s->ref_cnt >= 0);
520 
521 /*
522  * This macro checks if ch_qp/ch_eec handles are both not set for a channel
523  */
524 #define	IBCM_INVALID_CHANNEL(chan)	(chan == NULL)
525 
526 /*
527  * The next macros are used to get/set the statep from the QP
528  * handles, using the CM private data. These call into IBTL.
529  * The WAIT and RELEASE macros deal with related issues that
530  * require use of the same lock within IBTL.
531  */
532 #define	IBCM_GET_CHAN_PRIVATE(ch, s) \
533 	if ((ch) != NULL) { \
534 		s = ibtl_cm_get_chan_private(ch); \
535 	} else \
536 		s = NULL;
537 
538 #define	IBCM_SET_CHAN_PRIVATE(ch, s) \
539 	if ((ch) != NULL) { \
540 		ibtl_cm_set_chan_private(ch, (void *)(s)); \
541 	}
542 
543 #define	IBCM_RELEASE_CHAN_PRIVATE(ch) \
544 	if ((ch) != NULL) { \
545 		ibtl_cm_release_chan_private(ch); \
546 	}
547 
548 #define	IBCM_WAIT_CHAN_PRIVATE(ch) \
549 	ibtl_cm_wait_chan_private(ch);
550 
551 /* In future, if we intend to change it to realtime_timeout, it's easy */
552 #define	IBCM_TIMEOUT(arg1, arg2)	timeout(ibcm_timeout_cb, arg1,\
553 						drv_usectohz(arg2))
554 #define	IBCM_UD_TIMEOUT(arg1, arg2)	timeout(ibcm_sidr_timeout_cb, arg1,\
555 						drv_usectohz(arg2))
556 
557 extern void ibcm_open_enqueue(ibcm_state_data_t *statep);
558 extern void ibcm_open_done(ibcm_state_data_t *statep);
559 extern void ibcm_close_enqueue(ibcm_state_data_t *statep);
560 extern void ibcm_close_done(ibcm_state_data_t *statep, int send_done);
561 extern void ibcm_close_enter(void);
562 extern void ibcm_close_exit(void);
563 extern void ibcm_lapr_enter(void);
564 extern void ibcm_lapr_exit(void);
565 extern void ibcm_check_for_opens(void);
566 extern void ibcm_check_for_async_close(void);
567 extern void ibcm_close_start(ibcm_state_data_t *statep);
568 extern void ibcm_run_tlist_thread(void);
569 
570 /*
571  * Structures & defines for SIDR
572  */
573 
574 /*
575  * Define a connection state structure, used for SIDR REQ and REP
576  * (ibcm_ud_state_data_t - struct for SIDR connection)
577  *
578  * ud_state: 		CM connection state (See ibcm_conn_state_t)
579  * ud_req_id:		Request ID
580  * ud_svcid:		Service ID
581  * ud_state_mutex:	CM connection state
582  *
583  * ud_max_cm_retries:	Max retry count for sending a SIDR REQ
584  * ud_ref_cnt:		State ref count for not deleting accidentally
585  * ud_remaining_retry_count: Remaining count for retries ie., posting
586  *			stored MADs
587  * ud_cm_handler:	Server's handler callback address
588  *
589  * ud_nextp:		CM link for IBTF list
590  * ud_hcap:		A pointer to the HCA's entry
591  *
592  * ud_timerid:		Timer id for the timeout either for re-sending the
593  *			stored mad or deleting the stored mad
594  *			Ex: A SIDR REP response for an incoming SIDR REQ
595  *			An outgoing SIDR REQ on active connection side
596  * ud_timer_value:	Time for any of the above timers in HZ
597  * ud_pkt_life_time:	pkt life time from source to destination
598  * ud_stored_reply_addr: Address for replying using the stored mad
599  *
600  * ud_sidr_req_lid:	SIDR REQ sender's port LID
601  * ud_sidr_req_gid:	SIDR REQ sender's port GID
602  * ud_grh_exists:	TRUE if GRH exists in the incoming SIDR REQ
603  *
604  * ud_passive_qpn:	QPN allocated by server for a SIDR REQ
605  * ud_passive_qpn_qkey:	QPN's QKEY allocated by server
606  *
607  * ud_block_client_cv:	CV condition variable on which ibt_ud_get_dqpn() waits,
608  *			if called in blocking mode.
609  * ud_return_data:	UD return args, valid for blocking ibt_ud_get_dqpn
610  * ud_timer_stored_state: State stored for timeout handling
611  * ud_blocking_done	: Tells if cv_wait is needed or not. To handle the
612  *			  case where a cv_signal is received prior to its
613  *			  cv_wait().
614  * Note : All timer values that are of type "clock_t" below are in usec
615  */
616 typedef struct ibcm_ud_state_data_s {
617 	kmutex_t		ud_state_mutex;
618 	ibcm_conn_state_t	ud_state;
619 	ibcm_mode_t		ud_mode;
620 
621 	int			ud_ref_cnt;
622 
623 	uint32_t		ud_req_id;
624 	ib_svc_id_t		ud_svc_id;
625 
626 	uint8_t			ud_max_cm_retries;
627 	uint8_t			ud_remaining_retry_cnt;
628 	ibt_cm_ud_handler_t	ud_cm_handler;
629 
630 	struct ibcm_ud_state_data_s	*ud_nextp;
631 	struct ibcm_hca_info_s *ud_hcap;
632 
633 	/* timeout related stuff */
634 	timeout_id_t		ud_timerid;
635 	clock_t			ud_timer_value;
636 	clock_t			ud_pkt_life_time;
637 	ibcm_mad_addr_t		ud_stored_reply_addr;
638 	ibmf_msg_t		*ud_stored_msg;
639 
640 
641 	/* SIDR REQ side related */
642 	ib_lid_t		ud_sidr_req_lid;
643 	ib_gid_t		ud_sidr_req_gid;
644 	boolean_t		ud_grh_exists;
645 
646 	/* Stored values on server/SIDR REP side for re-transmits */
647 	ib_qpn_t		ud_passive_qpn;
648 	ib_qkey_t		ud_passive_qp_qkey;
649 
650 	/* Clients' information */
651 	void			*ud_state_cm_private;
652 
653 	struct ibcm_ud_state_data_s	*ud_timeout_next;
654 	boolean_t		ud_delete_state_data;
655 	boolean_t		ud_blocking_done;
656 
657 	uint8_t			ud_send_mad_flags;
658 
659 	ibcm_isync_t		ud_clnt_proceed;
660 
661 	/* The following fields are not used by server side connection */
662 	kcondvar_t		ud_block_client_cv;
663 	ibt_ud_returns_t	*ud_return_data;
664 	ibcm_conn_state_t	ud_timer_stored_state;
665 } ibcm_ud_state_data_t;
666 
667 _NOTE(MUTEX_PROTECTS_DATA(ibcm_ud_state_data_s::ud_state_mutex,
668     ibcm_ud_state_data_s::{ud_state ud_ref_cnt ud_timerid
669     ud_delete_state_data ud_blocking_done ud_send_mad_flags ud_clnt_proceed
670     ud_timer_stored_state ud_send_mad_flags ud_clnt_proceed
671     ud_block_client_cv ud_timer_value ud_remaining_retry_cnt}))
672 
673 _NOTE(READ_ONLY_DATA(ibcm_ud_state_data_s::{ud_mode ud_req_id ud_svc_id
674     ud_max_cm_retries ud_pkt_life_time ud_stored_reply_addr ud_stored_msg
675     ud_sidr_req_lid ud_sidr_req_gid ud_grh_exists ud_passive_qpn
676     ud_passive_qp_qkey ud_state_cm_private ud_stored_reply_addr ud_stored_msg}))
677 
678 _NOTE(SCHEME_PROTECTS_DATA("Serailized access by ud_block_client_cv",
679     ibcm_ud_state_data_s::{ud_return_data}))
680 
681 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibcm_ud_state_data_s::{ud_cm_handler}))
682 
683 /*
684  * Structure used to specify the SIDR search parameters
685  */
686 typedef struct ibcm_sidr_srch_s {
687 	ib_lid_t		srch_lid;
688 	ib_gid_t		srch_gid;
689 	boolean_t		srch_grh_exists;
690 	uint32_t		srch_req_id;
691 	ibcm_mode_t		srch_mode;
692 } ibcm_sidr_srch_t;
693 
694 _NOTE(READ_ONLY_DATA(ibcm_sidr_srch_s))
695 
696 /*
697  * Incr/Decr ud_ref_cnt by 1
698  */
699 #define	IBCM_UD_REF_CNT_INCR(s)	((s)->ud_ref_cnt++)
700 #define	IBCM_UD_REF_CNT_DECR(s)	\
701 	if ((--(s->ud_ref_cnt) == 0) && (s->ud_delete_state_data == B_TRUE)) { \
702 		ibcm_add_ud_tlist(s);\
703 	} \
704 	ASSERT(s->ud_ref_cnt >= 0);
705 
706 /*
707  * Structure to store the Service Registration and Service Bind entries.
708  *
709  * Well known service id's are unique on a given HCA, but can be registered
710  * only at some GID's. Hence can be multiple GID's per Service ID. For each
711  * such GID and PKEY combination registered, there will be an ibcm_svc_info_t
712  * entry in the CM global service list.
713  *
714  * Annex A of the spec constrains that there shall be one service provider per
715  * service id, which implies same svc_rc_handler for all such entries
716  * There can be multiple transport types (svc_tran_type) per Service ID. For
717  * each such transport type, there will be an ibcm_svc_info_t entry in the
718  * CM global service list and cm handler can be different
719  *
720  * For locally allocated service id's (maintained by OS), there can be only
721  * one GID, where the service can be registered
722  *
723  * svc_id:		Service ID
724  * svc_num_sids:	Number (Range) of service-ids supported
725  * svc_flags:		Service flags specified at registration time
726  * svc_link:		Global AVL tree of ibcm_svc_info_t structs
727  * svc_rc_handler:	Server handler for RC (only one is valid at a time)
728  * svc_ud_handler:	Server handler for UD (only one is valid at a time)
729  * svc_ref_cnt:		Reference count
730  * svc_to_delete:	If 1, then the entry is marked to be deleted
731  *
732  * sbind_gid:		GID
733  * sbind_pkey:		P_Key
734  * sbind_lease:		Service Lease
735  * sbind_name:		Service Name
736  */
737 typedef struct ibcm_svc_info_s {
738 	avl_node_t		svc_link;
739 	struct ibcm_svc_bind_s	*svc_bind_list;
740 	ibt_cm_handler_t	svc_rc_handler;
741 	ibt_cm_ud_handler_t	svc_ud_handler;
742 	int			svc_ref_cnt;
743 	int			svc_to_delete;
744 	ib_svc_id_t		svc_id;
745 	int			svc_num_sids;
746 	ibt_service_flags_t	svc_flags;
747 } ibcm_svc_info_t;
748 
749 typedef struct ibcm_svc_bind_s {
750 	struct ibcm_svc_bind_s	*sbind_link;
751 	void			*sbind_cm_private;
752 	ib_gid_t		sbind_gid;
753 	ib_guid_t		sbind_hcaguid;
754 	uint64_t		sbind_key[2];
755 				/* sbind_data is assumed to be 8-byte aligned */
756 	uint8_t			sbind_data[IB_SVC_DATA_LEN]; /* ServiceData */
757 	uint32_t		sbind_lease;
758 	ib_pkey_t		sbind_pkey;
759 	uint8_t			sbind_port;
760 	uint8_t			sbind_rewrite_state;
761 	char			sbind_name[IB_SVC_NAME_LEN];
762 } ibcm_svc_bind_t;
763 
764 /*
765  * Service records may be lost by the SM/SA (reboot, change in who
766  * is the master, etc.).  When any of the above occurs, a PORT_UP
767  * async event is supposed to occur, at which point we mark all of
768  * our service record information as stale (REWRITE_NEEDED), and
769  * subsequently make the necessary sa_update calls to get the
770  * SM/SA in sync with all the service records we previously wrote.
771  *
772  * Values for sbind_rewrite_state follow.  This field is protected by
773  * ibcm_svc_info_lock.  ibt_unbind_service has to wait until a service
774  * binding is either idle or needed, sleeping on ibcm_svc_info_cv if
775  * busy (rewrite in progress).
776  */
777 #define	IBCM_REWRITE_IDLE	0
778 #define	IBCM_REWRITE_NEEDED	1
779 #define	IBCM_REWRITE_BUSY	2
780 
781 typedef struct ibcm_port_up_s {
782 	ib_guid_t	pup_hca_guid;
783 	uint8_t		pup_port;
784 } ibcm_port_up_t;
785 
786 /* arg is a pointer to ibcm_port_up_t */
787 extern void ibcm_service_record_rewrite_task(void *);
788 
789 #define	IBCM_SVC_INCR(svcinfop) (svcinfop)->svc_ref_cnt++
790 #define	IBCM_SVC_DECR(svcinfop) \
791 	if (--((svcinfop)->svc_ref_cnt) == 0 && \
792 	    (svcinfop)->svc_to_delete) \
793 		cv_broadcast(&ibcm_svc_info_cv); \
794 	ASSERT(svcinfop->svc_ref_cnt >= 0);
795 
796 _NOTE(READ_ONLY_DATA(ibcm_svc_info_s::{svc_rc_handler svc_ud_handler svc_id
797     svc_num_sids svc_flags}))
798 
799 _NOTE(READ_ONLY_DATA(ibcm_svc_bind_s::{sbind_cm_private sbind_gid sbind_hcaguid
800     sbind_key sbind_data sbind_lease sbind_pkey sbind_port sbind_name}))
801 
802 /* for avl tree search */
803 typedef struct ibcm_svc_lookup_s {
804 	ib_svc_id_t	sid;
805 	int		num_sids;
806 } ibcm_svc_lookup_t;
807 
808 typedef struct ibcm_ar_ref_s {
809 	struct ibcm_ar_ref_s	*ar_ref_link;
810 	ibt_clnt_hdl_t		ar_ibt_hdl;
811 } ibcm_ar_ref_t;
812 
813 typedef struct ibcm_ar_s {
814 	ibt_ar_t		ar;
815 	int			ar_flags;	/* 1 = INITING, 2 = FAILED */
816 	int			ar_waiters;	/* # of waiters */
817 	kcondvar_t		ar_cv;
818 	uint8_t			ar_port;
819 	uint8_t			ar_rewrite_state; /* see sbind_rewrite_state */
820 	ibcm_ar_ref_t		*ar_ibt_hdl_list;
821 	struct ibcm_ar_s	*ar_link;
822 	sa_service_record_t	*ar_srv_recp;
823 	ibmf_saa_handle_t	ar_saa_handle;
824 	struct ibcm_hca_info_s	*ar_hcap;
825 } ibcm_ar_t;
826 
827 /* ar_flags */
828 #define	IBCM_AR_SUCCESS		0
829 #define	IBCM_AR_FAILED		1
830 #define	IBCM_AR_INITING		2
831 
832 
833 /*
834  * These flags are used for adding (if an entry does not exist) or
835  * for just looking one up
836  */
837 typedef enum ibcm_lookup_flag_e {
838 	IBCM_FLAG_LOOKUP		= 0,	/* just lookup */
839 	IBCM_FLAG_ADD			= 1,	/* just add */
840 	IBCM_FLAG_LOOKUP_AND_ADD	= 2	/* lookup first. add if  */
841 						/* lookup failed */
842 } ibcm_lookup_flag_t;
843 
844 typedef enum ibcm_finit_state_e {
845 	IBCM_FINIT_INIT,		/* CM's init is not yet completed */
846 	IBCM_FINIT_IDLE,		/* CM not in either init or fini */
847 	IBCM_FINIT_BUSY,		/* CM busy either in init or fini */
848 	IBCM_FINIT_FAIL,		/* Init failed */
849 	IBCM_FINIT_SUCCESS		/* Fini has succeeded */
850 } ibcm_finit_state_t;
851 
852 /*
853  * Identifies HCA's state. Used in the definition of ibcm_hca_info_t
854  * If HCA is in ACTIVE state only does CM allow any MAD processing.
855  */
856 typedef enum ibcm_hca_state_e {
857 	IBCM_HCA_INIT,
858 	IBCM_HCA_ACTIVE,
859 	IBCM_HCA_NOT_ACTIVE
860 } ibcm_hca_state_t;
861 
862 /* QP information per pkey, stored in port information */
863 typedef struct ibcm_qp_list_s {
864 	ib_pkey_t		qp_pkey;
865 	ibmf_qp_handle_t	qp_cm;
866 	uint32_t		qp_ref_cnt;
867 	struct ibcm_port_info_s *qp_port;
868 	struct ibcm_qp_list_s	*qp_next;
869 } ibcm_qp_list_t;
870 
871 _NOTE(READ_ONLY_DATA(ibcm_qp_list_s::{qp_pkey qp_cm qp_port qp_next}))
872 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibcm_qp_list_s))
873 
874 /*
875  * port information per HCA
876  * port_ibmf_hdl	- contains IBMF handle for that port if valid
877  *			  otherwise is NULL
878  * port_ibmf_saa_hdl	- contains SA Access handle for that port if valid
879  *			  otherwise is NULL
880  */
881 typedef struct ibcm_port_info_s {
882 	ibmf_handle_t		port_ibmf_hdl;
883 	ibmf_saa_handle_t	port_ibmf_saa_hdl;
884 	ib_gid_t		port_sgid0;
885 	uint8_t			port_event_status;
886 	uint8_t			port_saa_open_in_progress;
887 	uint8_t			port_num;
888 	ibmf_register_info_t	port_ibmf_reg;
889 	ibmf_impl_caps_t	port_ibmf_caps;
890 	ibcm_qp_list_t		port_qp1;
891 	ibcm_qp_list_t		*port_qplist;
892 	struct ibcm_hca_info_s	*port_hcap;
893 } ibcm_port_info_t;
894 
895 _NOTE(READ_ONLY_DATA(ibcm_port_info_s::{port_num port_ibmf_caps port_qp1
896     port_hcap}))
897 
898 /* Value to indicate to exit the timeout list processing thread */
899 #define	IBCM_TIMEOUT_THREAD_EXIT	01
900 
901 /*
902  * IBCM code relies on AVL routines already in kernel for faster lookups.
903  * AVL was chosen over mod hashing mechanism based on the its internal
904  * limitations in the kernel (no support for over 100,000 keys).
905  *
906  * IBCM uses two AVL trees on the passive side and one on active side per HCA.
907  * The two trees are need on the passive side because the tree lookup criteria
908  * changes based on the type of message being processed. On passive side it is
909  * based on remote_qpn and remote_hca_guid for only incoming REQ message and for
910  * for all other messages the search criteria is based upon remote_comid.
911  * On active side the lookup criteria remains static based upon local_comid.
912  *
913  * AVL tree insertions are done by grabbing the writer lock (hca_state_rwlock)
914  * and lookups are done by grabbing the reader lock.
915  */
916 
917 /*
918  * CM's per HCA data structure.
919  *
920  * One such entry is added/removed on hca attach/detach notifications to CM
921  * respectively.
922  *
923  * Comids are used for all connections. Req ids are used for SIDR REQ and
924  * SIDR REP messages.  These are  simple counters that wrap around INT_MAX.
925  * NOTE: The starting value for comid, per HCA, is 2.
926  *
927  * hca_state:		HCA's current state (ibcm_hca_state_t) - whether
928  *				IBT_HCA_ACTIVE, IBT_HCA_NOT_ACTIVE,
929  * hca_guid:            Active HCA guid
930  * hca_caps:		HCA capability mask
931  * hca_ack_delay:	HCA ack delay
932  * hca_max_rdma_rd	Max RDMA in Reads
933  * hca_max_rdma_dpt	Max RDMA out Reads
934  * hca_active_tree:	This tree is used for lookups on Active/Passive side
935  *			CM based on communication id ONLY.
936  * hca_passive_tree:	This tree is used to lookup/create ibcm_state_data_t on
937  *			Passive Side CM based on remote_qpn and remote_hca_guid.
938  * hca_passive_comid_tree:
939  *			This tree is used to lookup/create ibcm_state_data_t on
940  *			Passive Side CM based on remote_comid and
941  *			remote_hca_guid.
942  * hca_state_rwlock:	reader/writer Lock for the hca entry
943  *				for hca_active_tree
944  *				for hca_passive_tree
945  *				for hca_next_comid
946  * hca_sidr_list:	List for UD side
947  * hca_sidr_list_lock:	List lock for UD side
948  *				for hca_sidr_list
949  *				for hca_next_reqid
950  * hca_next_reqid:	Next active ReqId
951  * hca_next_comid:	Next active ComID
952  * hca_next:		Pointer to the next HCA
953  * hca_svc_cnt:		A count of services registered on this hca
954  * hca_acc_cnt:		A count of active references to this ibcm_hca_info_t
955  * hca_res_cnt:		A count of client's active resources on this hca
956  * hca_num_ports:	Number of ports that this HCA has
957  * hca_port_info:	Per port information (IBMA/SA access handles etc.)
958  *
959  * Note : The global mutex ibcm_global_hca_mutex declared in CM is used for
960  * accesses to the following fields :
961  * hca_acc_cnt, hca_res_cnt, hca_svc_cnt, hca_state
962  */
963 typedef struct ibcm_hca_info_s {
964 	ibcm_hca_state_t	hca_state;		/* Is HCA attached? */
965 	ib_guid_t		hca_guid;		/* HCA's guid value */
966 	ibt_hca_flags_t		hca_caps;		/* HCA capabilities */
967 	uint32_t		hca_vendor_id:24;
968 	uint16_t		hca_device_id;
969 	ib_time_t		hca_ack_delay;		/* HCA ack delay */
970 	uint8_t			hca_max_rdma_in_qp;	/* Max RDMA in Reads */
971 	uint8_t			hca_max_rdma_out_qp;	/* Max RDMA out Reads */
972 	vmem_t			*hca_comid_arena;	/* arena for com ids */
973 	vmem_t			*hca_reqid_arena;	/* arena for req ids */
974 	avl_tree_t		hca_active_tree;	/* active node tree */
975 	avl_tree_t		hca_passive_tree;	/* passive node tree */
976 	avl_tree_t		hca_passive_comid_tree;	/* passive comid tree */
977 	krwlock_t		hca_state_rwlock;	/* per HCA lock */
978 	ibcm_ud_state_data_t	*hca_sidr_list;		/* SIDR state list */
979 	krwlock_t		hca_sidr_list_lock;
980 
981 	struct ibcm_hca_info_s	*hca_next;		/* Next HCA entry */
982 
983 	int			hca_svc_cnt;		/* # of */
984 							/* services allocated */
985 	int			hca_acc_cnt;		/* active references */
986 	int			hca_res_cnt;		/* total resources */
987 	uint8_t			hca_num_ports;		/* #ports on this HCA */
988 	ibcm_port_info_t	hca_port_info[1];	/* Per portinfo array */
989 } ibcm_hca_info_t;
990 
991 _NOTE(RWLOCK_PROTECTS_DATA(ibcm_hca_info_s::hca_state_rwlock,
992     ibcm_hca_info_s::{hca_active_tree hca_passive_tree hca_passive_comid_tree}))
993 
994 _NOTE(SCHEME_PROTECTS_DATA("hca_sidr_list_lock protects hca_sidr_list",
995     ibcm_hca_info_s::{hca_sidr_list}))
996 
997 _NOTE(READ_ONLY_DATA(ibcm_hca_info_s::{hca_guid hca_caps hca_ack_delay
998     hca_max_rdma_in_qp hca_max_rdma_out_qp hca_comid_arena hca_reqid_arena
999     hca_passive_tree hca_active_tree hca_passive_comid_tree hca_num_ports }))
1000 
1001 /* Are we on Tavor HCA */
1002 #define	IBCM_IS_HCA_TAVOR(hcap)	\
1003 	(((hcap)->hca_device_id == 0x5a44) && ((hcap)->hca_vendor_id == 0x15b3))
1004 
1005 /*
1006  * called to ensure that HCA is in "attached" state and is willing to
1007  * process connections etc.
1008  */
1009 #define	IBCM_ACCESS_HCA_OK(s)	((s)->hca_state == IBCM_HCA_ACTIVE)
1010 
1011 /*
1012  * Passive AVL tree lookup info  (for hca_passive_tree)
1013  * CM needs this structure as passive tree lookups are based on
1014  * QPN and HCA GUID.
1015  */
1016 typedef	struct ibcm_passive_node_info_s {
1017 	ib_qpn_t	info_qpn;
1018 	ib_guid_t	info_hca_guid;
1019 } ibcm_passive_node_info_t;
1020 
1021 /*
1022  * Passive Com ID AVL tree lookup info  (for hca_passive_comid_tree)
1023  * CM needs this structure as passive comid tree lookups are based on
1024  * Remote Com ID and Remote HCA GUID.
1025  */
1026 typedef struct ibcm_passive_comid_node_info_s {
1027 	ib_com_id_t	info_comid;
1028 	ib_guid_t	info_hca_guid;
1029 } ibcm_passive_comid_node_info_t;
1030 
1031 /* CM proceed task args structure definition */
1032 typedef struct ibcm_proceed_targs_s {
1033 	ibt_cm_event_type_t	event;
1034 	ibt_cm_status_t		status;
1035 	union tst_t {
1036 		struct rc_s {
1037 			ibcm_state_data_t	*statep;
1038 			ibt_cm_proceed_reply_t	rc_cm_event_data;
1039 		} rc;
1040 		struct ud_s {
1041 			ibcm_ud_state_data_t	*ud_statep;
1042 			ib_qpn_t		ud_qpn;
1043 			ib_qkey_t		ud_qkey;
1044 			ibt_redirect_info_t	ud_redirect_info;
1045 		} ud;
1046 	} tst;
1047 	ibt_priv_data_len_t	priv_data_len;
1048 	/* keep priv_data as the last field */
1049 	uint8_t			priv_data[IBT_MAX_PRIV_DATA_SZ];
1050 } ibcm_proceed_targs_t;
1051 
1052 _NOTE(READ_ONLY_DATA(ibcm_proceed_targs_s))
1053 
1054 
1055 /*
1056  * function prototypes for AVL tree compares
1057  */
1058 int	ibcm_active_node_compare(const void *, const void *);
1059 int	ibcm_passive_node_compare(const void *, const void *);
1060 int	ibcm_passive_comid_node_compare(const void *, const void *);
1061 
1062 /*
1063  * function prototypes to allocate IBMF/SA_ACCESS handles
1064  */
1065 ibt_status_t	ibcm_hca_reinit_port(ibcm_hca_info_t *hca_p,
1066 		    uint8_t port_index);
1067 
1068 /* function prototypes to Manage CM's IBMF QP's */
1069 
1070 ibcm_qp_list_t *ibcm_find_qp(ibcm_hca_info_t *hcap, int port_no,
1071 		    ib_pkey_t pkey);
1072 
1073 void		ibcm_release_qp(ibcm_qp_list_t *cm_qp_entry);
1074 
1075 ibcm_status_t	ibcm_free_qp(ibcm_qp_list_t *cm_qp_entry);
1076 
1077 ibcm_status_t	ibcm_free_allqps(ibcm_hca_info_t *hcap, int port_no);
1078 
1079 /*
1080  * function prototypes to allocate and free outgoing CM messages
1081  */
1082 ibt_status_t
1083 ibcm_alloc_out_msg(ibmf_handle_t ibmf_handle, ibmf_msg_t **ibmf_msgpp,
1084     uint8_t method);
1085 ibcm_status_t
1086 ibcm_free_out_msg(ibmf_handle_t ibmf_handle, ibmf_msg_t **ibmf_msgpp);
1087 
1088 /*
1089  * Definition for CM state transition processing function
1090  */
1091 typedef void (*ibcm_state_handler_t)(ibcm_hca_info_t *hcap,
1092 		uint8_t *cm_input_mad, ibcm_mad_addr_t *cm_mad_addr);
1093 
1094 /*
1095  * CM REQ Message structure
1096  *
1097  * Request for communication.
1098  *
1099  * Things of interest are:-
1100  * ib_qpn_t cannot be used - it is typecast to uint32_t but is 24 bits
1101  * ib_eecn_t cannot be used - it is typecast to uint32_t but is 24 bits
1102  *
1103  * (See Table 85 REQ Message Contents - chapter 12 in IB Spec v1.0a)
1104  *
1105  */
1106 typedef struct ibcm_req_msg_s {
1107 	ib_com_id_t	req_local_comm_id;	/* Local communication id */
1108 						/* 32 bits */
1109 	uint32_t	req_rsvd1;		/* Reserved1 - 32 bits */
1110 	ib_svc_id_t	req_svc_id;		/* Service Id - 64 bits */
1111 	ib_guid_t	req_local_ca_guid;	/* Local CA GUID - 64 bits */
1112 	uint32_t	req_rsvd1p;		/* Reserved1+ - 32 bits */
1113 	ib_qkey_t	req_local_qkey;		/* Local Q_KEY - 32 bits */
1114 	uint32_t	req_local_qpn_plus;	/* QPN_24 RESP_RSRC_8 */
1115 						/* local side QPN - 24 bits */
1116 						/* Offered responder */
1117 						/* resources - 8 bits */
1118 	uint32_t	req_local_eec_no_plus;	/* LOCAL_EECN_24 INIT_DEPTH_8 */
1119 						/* Local side EECN - 24 bits */
1120 						/* Offered initiator */
1121 						/* depth - 8 bits */
1122 	uint32_t	req_remote_eecn_plus;	/* REM_EECN_24 TO_5 TT_2 EE_1 */
1123 						/* Remote side EECN - 24 bits */
1124 						/* Remote CM timeout - 5 bits */
1125 						/* Transport srvtype - 2 bits */
1126 						/* End-to-End flow - 1 bit */
1127 	uint32_t	req_starting_psn_plus;	/* START_PSN_24 TO_5 RETRY_3 */
1128 						/* Starting PSN - 24 bits */
1129 						/* Local CM timeout - 5 bits */
1130 						/* Retry count - 3 bits */
1131 	ib_pkey_t	req_part_key;		/* Partition key - 16 bits */
1132 	uint8_t		req_mtu_plus;		/* PATH_MTU_4 RDC_1 RNR_3 */
1133 						/* Path Pkt MTU - 4 bits */
1134 						/* Does RDC exist? - 1 bits */
1135 						/* RNR retry count - 3 bits */
1136 	uint8_t		req_max_cm_retries_plus; /* MAX_CM_RET_4 SRQ_1 RSV_3 */
1137 						/* Max CM retries - 4 bits */
1138 						/* SRQ Exists - 1 bit */
1139 						/* Reserved2 - 3 bits */
1140 	ib_lid_t	req_primary_l_port_lid;	/* Primary local port LID */
1141 	ib_lid_t	req_primary_r_port_lid;	/* Primary Remote port LID */
1142 	ib_gid_t	req_primary_l_port_gid;	/* Primary local port GID */
1143 	ib_gid_t	req_primary_r_port_gid;	/* Primary remote port GID */
1144 	uint32_t	req_primary_flow_label_plus; /* FLOW_20 RSV_4 SRATE_6 */
1145 						/* Prim. flow label - 20 bits */
1146 						/* Reserved3 - 6 bits */
1147 						/* Primary rate - 6 bits */
1148 	uint8_t		req_primary_traffic_class;
1149 						/* Primary Traffic class */
1150 	uint8_t		req_primary_hop_limit;	/* Prim Hop Limit */
1151 	uint8_t		req_primary_sl_plus;	/* PRIMARY_SL_4 LOCAL_1 RSV_3 */
1152 						/* Primary SL - 4 bits */
1153 						/* Prim. subnet local - 1 bit */
1154 						/* Reserved4 - 3 bits */
1155 	uint8_t		req_primary_localtime_plus; /* LOCAL_TO_5 RSV_3 */
1156 						/* Primary local */
1157 						/* timeout - 5 bits */
1158 						/* Reserved5 - 3 bits */
1159 	ib_lid_t	req_alt_l_port_lid;	/* Alt local port LID */
1160 	ib_lid_t	req_alt_r_port_lid;	/* Alt Remote port LID */
1161 	/* Note: req_alt_l_port_gid/req_alt_r_port_gid are not 8-byte aligned */
1162 	uint8_t		req_alt_l_port_gid[16];	/* Alt local port GID */
1163 	uint8_t		req_alt_r_port_gid[16];	/* Alt remote port GID */
1164 	uint32_t	req_alt_flow_label_plus; /* ALT_FLOW_20 RSV_6 ARATE_6 */
1165 						/* Alt flow label - 20 bits */
1166 						/* Reserved6 - 6 bits */
1167 						/* Alternate rate - 6 bits */
1168 	uint8_t		req_alt_traffic_class;	/* Alt traffic class */
1169 	uint8_t		req_alt_hop_limit;	/* Alt hop limit */
1170 	uint8_t		req_alt_sl_plus;	/* ALT_SL_4 A_LOCAL_1 RSV_3 */
1171 						/* Alternate SL - 4 bits */
1172 						/* Alt subnet local - 1 bit */
1173 						/* Reserved7 - 3 bits */
1174 	uint8_t		req_alt_localtime_plus;	/* ALT_LOCAL_ACK_TO_5 RSV_3 */
1175 						/* Alt Local ACK */
1176 						/* timeout - 5 bits */
1177 						/* Reserved8 - 3 bits */
1178 	uint8_t		req_private_data[IBT_REQ_PRIV_DATA_SZ];
1179 						/* Private data */
1180 } ibcm_req_msg_t;
1181 
1182 
1183 /*
1184  * The following set of defines are short-cuts to CEP_PATH or GRH info
1185  */
1186 #define	IBCM_PRIM_CEP_PATH(s)	(s)->oc_path->pi_prim_cep_path
1187 #define	IBCM_PRIM_ADDS_VECT(s)	(s)->oc_path->pi_prim_cep_path.cep_adds_vect
1188 
1189 #define	IBCM_ALT_CEP_PATH(s)	(s)->oc_path->pi_alt_cep_path
1190 #define	IBCM_ALT_ADDS_VECT(s)	(s)->oc_path->pi_alt_cep_path.cep_adds_vect
1191 
1192 #define	IBCM_UD_CEP_PATH(s)	(s)->us_path_info->ai_cep_path
1193 #define	IBCM_UD_ADDS_VECT(s)	(s)->us_path_info->ai_cep_path.cep_adds_vect
1194 
1195 /*
1196  * The following set of defines are short-cuts to ibt_cm_event_t
1197  */
1198 #define	IBCM_EVT_REQ(e)		(e).cm_event.req
1199 #define	IBCM_EVT_REP(e)		(e).cm_event.rep
1200 
1201 /*
1202  * The following set of defines are short-cuts to qp_attrs or qp_info
1203  */
1204 #define	IBCM_QP_RC(q)		(q).qp_info.qp_transport.rc
1205 #define	IBCM_QP_UD(q)		(q).qp_info.qp_transport.ud
1206 #define	IBCM_QP_UC(q)		(q).qp_info.qp_transport.uc
1207 
1208 #define	IBCM_QPINFO(q)		(q).qp_transport
1209 #define	IBCM_QPINFO_RC(q)	(q).qp_transport.rc
1210 #define	IBCM_QPINFO_RC_PATH(q)	(q).qp_transport.rc.rc_path
1211 #define	IBCM_QPINFO_UC(q)	(q).qp_transport.uc
1212 #define	IBCM_QPINFO_UC_PATH(q)	(q).qp_transport.uc.uc_path
1213 #define	IBCM_QPINFO_UD(q)	(q).qp_transport.ud
1214 
1215 
1216 /* The following set of defines are short-cuts to RC and SIDR MAD HDRs */
1217 
1218 #define	IBCM_OUT_MADP(msgp)	(msgp->im_msgbufs_send.im_bufs_mad_hdr)
1219 #define	IBCM_OUT_HDRP(msgp)	((ib_mad_hdr_t *)IBCM_OUT_MADP(msgp))
1220 #define	IBCM_OUT_MSGP(msgp)	(msgp->im_msgbufs_send.im_bufs_cl_data)
1221 
1222 #define	IBCM_IN_MADP(msgp)	(msgp->im_msgbufs_recv.im_bufs_mad_hdr)
1223 #define	IBCM_IN_HDRP(msgp)	((ib_mad_hdr_t *)IBCM_IN_MADP(msgp))
1224 #define	IBCM_IN_MSGP(msgp)	(msgp->im_msgbufs_recv.im_bufs_cl_data)
1225 
1226 #define	IBCM_REJ_PRIV(msgp)  &(((ibcm_rej_msg_t *) \
1227 	IBCM_OUT_MSGP(statep->stored_msg))->rej_private_data[0])
1228 /*
1229  * CM MRA Message structure
1230  *
1231  * Message Receipt Acknowledgement (MRA).
1232  *
1233  * NOTE: IB hosts and targets are required to be able to receive and
1234  * act upon an MRA, but the ability to send an MRA is optional.
1235  */
1236 typedef struct ibcm_mra_msg_s {
1237 	ib_com_id_t	mra_local_comm_id;	/* Local communication id */
1238 	ib_com_id_t	mra_remote_comm_id;	/* Remote communication id */
1239 	uint8_t		mra_message_type_plus;	/* Message Type - 2 bits */
1240 						/* Reserved1 - 6 bits */
1241 	uint8_t		mra_service_timeout_plus; /* SVC_TO_5 RSV_3 */
1242 						/* Service timeout - 5 bits */
1243 						/* Reserved2 - 3 bits */
1244 	uint8_t		mra_private_data[IBT_MRA_PRIV_DATA_SZ];
1245 						/* Private data */
1246 } ibcm_mra_msg_t;
1247 
1248 /*
1249  * CM REJ Message structure
1250  * REJ indicates that the sender will not continue through the communication
1251  * establishment sequence and the reason why it will not.
1252  *
1253  * NOTE: See ibt_cm_reason_t in common/sys/ib/ib_cm.h for complete list
1254  * of rejection reasons supported.
1255  */
1256 typedef struct ibcm_rej_msg_s {
1257 	ib_com_id_t	rej_local_comm_id;	/* Local communication id */
1258 	ib_com_id_t	rej_remote_comm_id;	/* Remote communication id */
1259 	uint8_t		rej_msg_type_plus;	/* REJ_MSG_TYPE_2 RSV_6 */
1260 						/* Msg being REJed - 2 bits */
1261 						/* Reserved1 - 6 bits */
1262 	uint8_t		rej_reject_info_len_plus; /* REJ_INFO_LEN_7 RSV_1 */
1263 						/* Rej. Info Length - 7 bits */
1264 						/* Reserved2 - 1 bit */
1265 	uint16_t	rej_rejection_reason;	/* Reject err code - 16 bits */
1266 	uint8_t		rej_addl_rej_info[IBT_CM_ADDL_REJ_LEN];
1267 						/* Additional Reject Info */
1268 	uint8_t		rej_private_data[IBT_REJ_PRIV_DATA_SZ];
1269 						/* Private data */
1270 } ibcm_rej_msg_t;
1271 
1272 /*
1273  * CM REP Message structure
1274  *
1275  * REP is returned in response to REQ, indicating that the respondent
1276  * accepts the Service-ID, proposed primary port, and any parameters
1277  * specified in the PrivateData of the REQ.
1278  */
1279 typedef struct ibcm_rep_msg_s {
1280 	ib_com_id_t	rep_local_comm_id;	/* Local communication id */
1281 	ib_com_id_t	rep_remote_comm_id;	/* Remote communication id */
1282 	ib_qkey_t	rep_local_qkey;		/* Local Q_KEY */
1283 	uint32_t	rep_local_qpn_plus;	/* LOCAL_QPN_24 RSV_8 */
1284 						/* Local side QPN - 24 bits */
1285 						/* Reserved1 - 8 bits */
1286 	uint32_t	rep_local_eecn_plus;	/* LOCAL_EECN_24 RSV_8 */
1287 						/* Local side EECN - 24 bits */
1288 						/* Reserved2 - 8 bits */
1289 	uint32_t	rep_starting_psn_plus;	/* STARTING_PSN_24 RSV_8 */
1290 						/* Starting PSN - 24 bits */
1291 						/* Reserved3 - 8 bits */
1292 	uint8_t		rep_resp_resources;	/* Responder resources 8 bits */
1293 	uint8_t		rep_initiator_depth;	/* Initiator depth - 8 bits */
1294 	uint8_t		rep_target_delay_plus;	/* TGT_ACK_DLY_5 FAIL_2 EE_1 */
1295 						/* Target ACK delay - 5 bits */
1296 						/* Failover accepted - 2 bits */
1297 						/* End-to-End flow control - */
1298 						/* 1 bit */
1299 	uint8_t		rep_rnr_retry_cnt_plus;	/* RNR_COUNT_3 SRQ_1 RSV_4 */
1300 						/* RNR retry count - 3 bits */
1301 						/* SRQ Exists - 1 bit */
1302 						/* Reserved4 - 4 bits */
1303 	uint8_t		rep_local_ca_guid[8];	/* Local CA GUID - 64 bits */
1304 	uint8_t		rep_private_data[IBT_REP_PRIV_DATA_SZ];
1305 						/* Private data */
1306 } ibcm_rep_msg_t;
1307 
1308 
1309 /*
1310  * CM RTU Message structure
1311  *
1312  * RTU indicates that the connection is established, and that the
1313  * recipient may begin transmitting.
1314  */
1315 typedef struct ibcm_rtu_msg_s {
1316 	ib_com_id_t	rtu_local_comm_id;	/* Local communication id */
1317 	ib_com_id_t	rtu_remote_comm_id;	/* Remote communication id */
1318 	uint8_t		rtu_private_data[IBT_RTU_PRIV_DATA_SZ];
1319 						/* Private data */
1320 } ibcm_rtu_msg_t;
1321 
1322 
1323 /*
1324  * CM DREQ Message structure
1325  *
1326  * DREQ is sent to initiate the connection release sequence.
1327  */
1328 typedef struct ibcm_dreq_msg_s {
1329 	ib_com_id_t	dreq_local_comm_id;	/* Local communication id */
1330 	ib_com_id_t	dreq_remote_comm_id;	/* Remote communication id */
1331 	uint32_t	dreq_remote_qpn_eecn_plus; /* REM_EECN_24 RSV_8 */
1332 						/* Remote QPN/EECN - 24 bits */
1333 						/* reserved - 8 bits */
1334 	uint8_t		dreq_private_data[IBT_DREQ_PRIV_DATA_SZ];
1335 						/* Private data */
1336 } ibcm_dreq_msg_t;
1337 
1338 
1339 /*
1340  * CM DREP Message structure
1341  *
1342  * DREP is sent in response to DREQ, and signifies that the sender has
1343  * received DREQ.
1344  */
1345 typedef struct ibcm_drep_msg_s {
1346 	ib_com_id_t	drep_local_comm_id;	/* Local communication id */
1347 	ib_com_id_t	drep_remote_comm_id;	/* Remote communication id */
1348 	uint8_t		drep_private_data[IBT_DREP_PRIV_DATA_SZ];
1349 						/* Private Data */
1350 } ibcm_drep_msg_t;
1351 
1352 
1353 /*
1354  * CM LAP Message structure
1355  *
1356  * NOTE: LAP and APR messages are optional. These are needed if CM
1357  * accepts REQ messages and agrees to perform Automatic Path Migration.
1358  *
1359  * This message is used to change the alternate path information for a
1360  * specific connection.
1361  */
1362 typedef struct ibcm_lap_msg_s {
1363 	ib_com_id_t	lap_local_comm_id;	/* Local communication id */
1364 	ib_com_id_t	lap_remote_comm_id;	/* Remote communication id */
1365 	uint32_t	lap_rsvd1;		/* Reserved - 32 bits */
1366 	uint32_t	lap_remote_qpn_eecn_plus; /* REM_EECN_24 TO_5 RSV_3 */
1367 						/* Remote QPN/EECN - 24 bits */
1368 						/* Remote CM response */
1369 						/* timeout - 5 bits */
1370 						/* Reserved1 - 3 bits */
1371 	uint32_t	lap_rsvd2;		/* Reserved2 - 32 bits */
1372 	ib_lid_t	lap_alt_l_port_lid;	/* Alt local port LID */
1373 	ib_lid_t	lap_alt_r_port_lid;	/* Alt Remote port LID */
1374 	ib_gid_t	lap_alt_l_port_gid;	/* Alt local port GID */
1375 	ib_gid_t	lap_alt_r_port_gid;	/* Alt remote port GID */
1376 	uint32_t	lap_alt_flow_label_plus; /* ALT_FLOW_20 RSV_4 TCL_8 */
1377 						/* Alt flow label - 20 bits */
1378 						/* Reserved3 - 4 bits */
1379 						/* Alt traffic class - 8 bits */
1380 	uint8_t		lap_alt_hop_limit;	/* Alt hop limit */
1381 	uint8_t		lap_alt_srate_plus;	/* Reserved4 - 2 bits */
1382 						/* Alt. static rate - 6 bits */
1383 	uint8_t		lap_alt_sl_plus;	/* ALT_SL_4 A_LOCAL_1 RSV_3 */
1384 						/* Alternate SL - 4 bits */
1385 						/* Alt subnet local - 1 bit */
1386 						/* Reserved5 - 3 bits */
1387 	uint8_t		lap_alt_local_acktime_plus; /* ALT_TO_5 RSV_3 */
1388 						/* Alt Local ACK */
1389 						/* timeout - 5 bits */
1390 						/* Reserved6 - 3 bits */
1391 	uint8_t		lap_private_data[IBT_LAP_PRIV_DATA_SZ];
1392 						/* Private data */
1393 } ibcm_lap_msg_t;
1394 
1395 
1396 /*
1397  * CM APR Message structure
1398  *
1399  * APR is sent in response to a LAP request. MRA may be sent to allow
1400  * processing of the LAP.
1401  */
1402 typedef struct ibcm_apr_msg_s {
1403 	ib_com_id_t	apr_local_comm_id;	/* Local communication id */
1404 	ib_com_id_t	apr_remote_comm_id;	/* Remote communication id */
1405 	uint8_t		apr_addl_info_len;	/* Add'l Info Len - 8 bits */
1406 	uint8_t		apr_ap_status;		/* AP status - 8 bits */
1407 	uint16_t	apr_rsvd1;		/* Reserved1 - 16 bits */
1408 	uint8_t		apr_addl_info[IBT_CM_APR_ADDL_LEN];
1409 						/* Additional Information */
1410 	uint8_t		apr_private_data[IBT_APR_PRIV_DATA_SZ];
1411 						/* Private data */
1412 } ibcm_apr_msg_t;
1413 
1414 
1415 /*
1416  * CM SIDR_REQ Message structure
1417  *
1418  * NOTE: SIDR_REQ and SIDR_REP messages are conditionally required.
1419  * These are needed if non-management services are provided on the Channel
1420  * Adapter other than fixed QPNs. Management services include those
1421  * provided thru Subnet Manager Packets or thru General Management Packets.
1422  *
1423  * SIDR_REQ requests that the recipient return the information necessary
1424  * to communicate via UD messages with the entity specified by
1425  * SIDR_REQ:ServiceID
1426  */
1427 typedef struct ibcm_sidr_req_msg_s {
1428 	uint32_t	sidr_req_request_id;		/* Request id */
1429 	ib_pkey_t	sidr_req_pkey;			/* P_Key */
1430 	uint8_t		sidr_req_reserved[2];		/* Reserved */
1431 	ib_svc_id_t	sidr_req_service_id;		/* Service Id */
1432 	uint8_t		sidr_req_private_data[IBT_SIDR_REQ_PRIV_DATA_SZ];
1433 							/* Private Data */
1434 } ibcm_sidr_req_msg_t;
1435 
1436 
1437 /*
1438  * CM SIDR_REP Message structure
1439  *
1440  * SIDR_REP returns the information necessary to communicate via UD
1441  * messages with the entity specified by SIDR_REQ:ServiceID
1442  */
1443 typedef struct ibcm_sidr_rep_msg_s {
1444 	uint32_t	sidr_rep_request_id;		/* Request id */
1445 	uint8_t		sidr_rep_rep_status;		/* Status */
1446 	uint8_t		sidr_rep_add_info_len;		/* Length of Add Info */
1447 	uint8_t		sidr_rep_reserved1[2];		/* Reserved */
1448 	uint32_t	sidr_rep_qpn_plus;		/* QPN_24 RSV_8 */
1449 	/* since the 64-bit SID is not aligned, treat it as a byte array */
1450 	uint8_t		sidr_rep_service_id[8];		/* Service Id */
1451 	ib_qkey_t	sidr_rep_qkey;			/* Q_KEY */
1452 	uint8_t		sidr_rep_class_port_info[IBT_CM_SIDR_CP_LEN];
1453 							/* Class Port Info */
1454 							/* aka., add'l info */
1455 	uint8_t		sidr_rep_private_data[IBT_SIDR_REP_PRIV_DATA_SZ];
1456 							/* Private data */
1457 } ibcm_sidr_rep_msg_t;
1458 
1459 typedef struct ibcm_classportinfo_msg_s {
1460 	uint8_t		BaseVersion;		/* ver. of MAD base format */
1461 	uint8_t		ClassVersion;		/* ver. of MAD class format */
1462 	uint16_t	CapabilityMask;		/* capabilities of this class */
1463 	uint32_t	RespTimeValue_plus;	/* reserved : 27 bits */
1464 						/* resptime value : 5 bits */
1465 	uint64_t	RedirectGID_hi;		/* dest gid of redirect msgs */
1466 	uint64_t	RedirectGID_lo;		/* dest gid of redirect msgs */
1467 	uint32_t	RedirectTC_plus;	/* traffic class: 8 bits */
1468 						/* SL: 4 bits */
1469 						/* Flow label: 20 bits */
1470 	ib_lid_t	RedirectLID;		/* dlid for class services */
1471 	ib_pkey_t	RedirectP_Key;		/* p_key for class services */
1472 	uint32_t	RedirectQP_plus;	/* Reserved: 8 bits */
1473 						/* QPN: 24 bits */
1474 	ib_qkey_t	RedirectQ_Key;		/* q_key for class services */
1475 	uint64_t	TrapGID_hi;		/* dest gid of trap msgs */
1476 	uint64_t	TrapGID_lo;		/* dest gid of trap msgs */
1477 	uint32_t	TrapTC_plus;		/* Trap traffic class, etc., */
1478 	ib_lid_t	TrapLID;		/* dlid for traps */
1479 	ib_pkey_t	TrapP_Key;		/* p_key for traps */
1480 	uint32_t	TrapHL_plus;		/* Trap hop limit,etc., */
1481 	ib_qkey_t	TrapQ_Key;		/* q_key for traps */
1482 } ibcm_classportinfo_msg_t;
1483 
1484 /* All msgs are readonly on receiving side */
1485 _NOTE(READ_ONLY_DATA(ibcm_req_msg_s))
1486 _NOTE(READ_ONLY_DATA(ibcm_rep_msg_s))
1487 _NOTE(READ_ONLY_DATA(ibcm_mra_msg_s))
1488 _NOTE(READ_ONLY_DATA(ibcm_rej_msg_s))
1489 _NOTE(READ_ONLY_DATA(ibcm_lap_msg_s))
1490 _NOTE(READ_ONLY_DATA(ibcm_apr_msg_s))
1491 _NOTE(READ_ONLY_DATA(ibcm_sidr_req_msg_s))
1492 _NOTE(READ_ONLY_DATA(ibcm_sidr_rep_msg_s))
1493 _NOTE(READ_ONLY_DATA(ibcm_rtu_msg_s))
1494 _NOTE(READ_ONLY_DATA(ibcm_dreq_msg_s))
1495 _NOTE(READ_ONLY_DATA(ibcm_drep_msg_s))
1496 _NOTE(READ_ONLY_DATA(ibcm_classportinfo_msg_s))
1497 
1498 /* Prototype definitions for CM implementation functions */
1499 
1500 /*
1501  * The callback from IBMF to CM. This routines calls one of the CM
1502  * state processing functions depending upon mesg/attribute id
1503  *
1504  * ibmf_handle	: IBMF handle on which CM MAD was received
1505  * pktp		: MAD packet
1506  * args		: IBMF receive mad callback arg
1507  */
1508 void	ibcm_recv_cb(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp, void *args);
1509 
1510 /*
1511  * Prototypes for CM state transition handling functions
1512  */
1513 
1514 /*
1515  * The following are the CM state processing functions called on an
1516  * incoming REQ/REP/RTU/MRA/REJ/DREQ/DREP on active/passive sides
1517  * (Also handled are SIDR_REP and SIDR_REQ)
1518  * The brief description of these functions
1519  *	Search based on CM message fields in CM's HCA entry.
1520  *	Create/Delete state structures based on incoming message
1521  *	Handle duplicate messages and state transitions
1522  *	Set and Cancel timeouts
1523  *	Handle stale connections
1524  *	Change CM connection state
1525  *	Call CM CEP state transition functions to update CEP state
1526  *	and set CEP attributes
1527  *
1528  * INPUTS:
1529  *	hcap:		- IBMF callback argument
1530  *	cm_input_mad:	- ibmf message pointer of incoming MAD
1531  *	cm_mad_addr	- CM MAD address
1532  *
1533  * The state transition processing is specified in different functions based
1534  * on incoming message type rather than as one function because, the CM
1535  * processing is different for each of them.
1536  *
1537  * A global call table is initialized with these function addresses
1538  * (is defined in ibcm_impl.c), and invoked from ibcm_recv_cb
1539  * (IBMF's recv callback to CM) based on mesg/attribute id.
1540  */
1541 void	ibcm_process_req_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1542 	    ibcm_mad_addr_t *cm_mad_addr);
1543 void	ibcm_process_rep_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1544 	    ibcm_mad_addr_t *cm_mad_addr);
1545 void	ibcm_process_rtu_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1546 	    ibcm_mad_addr_t *cm_mad_addr);
1547 void	ibcm_process_dreq_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1548 	    ibcm_mad_addr_t *cm_mad_addr);
1549 void	ibcm_process_drep_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1550 	    ibcm_mad_addr_t *cm_mad_addr);
1551 void	ibcm_process_rej_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1552 	    ibcm_mad_addr_t *cm_mad_addr);
1553 void	ibcm_process_mra_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1554 	    ibcm_mad_addr_t *cm_mad_addr);
1555 void	ibcm_process_apr_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1556 	    ibcm_mad_addr_t *cm_mad_addr);
1557 void	ibcm_process_lap_msg(ibcm_hca_info_t *hcap, uint8_t *cm_input_mad,
1558 	    ibcm_mad_addr_t *cm_mad_addr);
1559 void	ibcm_process_sidr_req_msg(ibcm_hca_info_t *hcap,
1560 	    uint8_t *cm_input_mad, ibcm_mad_addr_t *cm_mad_addr);
1561 void	ibcm_process_sidr_rep_msg(ibcm_hca_info_t *hcap,
1562 	    uint8_t *cm_input_mad, ibcm_mad_addr_t *cm_mad_addr);
1563 
1564 typedef enum ibcm_proceed_error_e {
1565 	IBCM_PROCEED_INVALID_NONE	= 0,
1566 	IBCM_PROCEED_INVALID_EVENT,
1567 	IBCM_PROCEED_INVALID_EVENT_STATE,
1568 	IBCM_PROCEED_INVALID_PRIV_SZ,
1569 	IBCM_PROCEED_INVALID_LAP
1570 } ibcm_proceed_error_t;
1571 
1572 /* Encapsulates the information that client returns back from CM callback */
1573 typedef struct ibcm_clnt_reply_info_s {
1574 	ibt_cm_proceed_reply_t	*reply_event;
1575 	void			*priv_data;
1576 	ibt_priv_data_len_t	priv_data_len;
1577 } ibcm_clnt_reply_info_t;
1578 
1579 /* Encapsulates the information that UD client returns back from CM callback */
1580 typedef struct ibcm_ud_clnt_reply_info_s {
1581 	ib_qpn_t		ud_qpn;
1582 	ib_qkey_t		ud_qkey;
1583 	ibt_redirect_info_t	*redirect_infop;
1584 	void			*priv_data;
1585 	ibt_priv_data_len_t	priv_data_len;
1586 } ibcm_ud_clnt_reply_info_t;
1587 
1588 /*
1589  * Prototypes for CM CEP state transition handling functions. These are
1590  * called from CM connection state transition handling functions.
1591  *
1592  * The brief description of these functions :
1593  *	Validate CEP related attributes in the messages
1594  *	Change CEP state
1595  *	Set CEP attributes (modify CEP)
1596  *	Call client/server callback handlers
1597  *	Fill up the response MADs
1598  *
1599  * The arguments are :
1600  *	statep:		Connection state structure
1601  *	cm_req/rep/rtu/rej msg : Received CM message
1602  *	cm_output_mad	: The response CM MAD with some of the fields filled in
1603  *			  The cm output mad is allocated by CM state transition
1604  *			  functions and has generic MAD header
1605  *			  Certain fields like com id, etc., are filled by CM
1606  *			  connection state transition functions that are above
1607  */
1608 
1609 /* QP state transition function called for an incoming REQ on passive side */
1610 ibcm_status_t	ibcm_cep_state_req(ibcm_state_data_t *statep,
1611 		    ibcm_req_msg_t *cm_req_msg, ibt_cm_reason_t *reason,
1612 		    uint8_t *arej_info_len);
1613 
1614 /* Processes QP state machine based on return values from cm handler */
1615 ibcm_status_t	ibcm_process_cep_req_cm_hdlr(ibcm_state_data_t *statep,
1616 		    ibt_cm_status_t cb_status,
1617 		    ibcm_clnt_reply_info_t *clnt_info,
1618 		    ibt_cm_reason_t *reject_reason, uint8_t *arej_len,
1619 		    ibcm_req_msg_t *cm_req_msgp);
1620 
1621 /* Processes CM state machine based on return values from ibcm_cep_state_req */
1622 void		ibcm_handle_cep_req_response(ibcm_state_data_t *statep,
1623 		    ibcm_status_t response, ibt_cm_reason_t reject_reason,
1624 		    uint8_t arej_info_len);
1625 
1626 /* QP state transition function called for an incoming REP on active side */
1627 ibcm_status_t	ibcm_cep_state_rep(ibcm_state_data_t *statep,
1628 		    ibcm_rep_msg_t *cm_rep_msg, ibt_cm_reason_t *reason,
1629 		    uint8_t *arej_info_len);
1630 
1631 /* Processes QP state machine based on return values from cm handler */
1632 ibcm_status_t	ibcm_process_cep_rep_cm_hdlr(ibcm_state_data_t *statep,
1633 		    ibt_cm_status_t cb_status,
1634 		    ibcm_clnt_reply_info_t *clnt_info,
1635 		    ibt_cm_reason_t *reject_reason, uint8_t *arej_len,
1636 		    ibcm_rep_msg_t *cm_rep_msgp);
1637 
1638 /* Processes CM state machine based on return values from ibcm_cep_state_rep */
1639 void		ibcm_handle_cep_rep_response(ibcm_state_data_t *statep,
1640 		    ibcm_status_t response, ibt_cm_reason_t reject_reason,
1641 		    uint8_t arej_info_len, ibcm_rep_msg_t *rep_msgp);
1642 
1643 /* QP state transition function called for an incoming RTU on passive side */
1644 void	ibcm_cep_state_rtu(ibcm_state_data_t *statep,
1645 	    ibcm_rtu_msg_t *cm_rtu_msg);
1646 
1647 /* QP state transition func called for an incoming REJ on active/passive side */
1648 void	ibcm_cep_state_rej(ibcm_state_data_t *statep,
1649 	    ibcm_rej_msg_t *cm_rej_msg, ibcm_conn_state_t rej_state);
1650 
1651 /* QP state transition func for an incoming REJ on active side in est state */
1652 void	ibcm_cep_state_rej_est(ibcm_state_data_t *statep);
1653 
1654 /*
1655  * QP state transition function called for an outgoing RTU on active side,
1656  * after setting CEP to RTS state active/passive side
1657  */
1658 void	ibcm_cep_send_rtu(ibcm_state_data_t *statep);
1659 
1660 
1661 /* QP state transition function called for an incoming LAP */
1662 ibcm_status_t	ibcm_cep_state_lap(ibcm_state_data_t *statep,
1663 		    ibcm_lap_msg_t *lap_msg, ibcm_apr_msg_t *apr_msg);
1664 
1665 /* Processes QP state machine based on return value from cm handler for LAP */
1666 void		ibcm_process_cep_lap_cm_hdlr(ibcm_state_data_t *statep,
1667 		    ibt_cm_status_t cb_status,
1668 		    ibcm_clnt_reply_info_t *clnt_info,
1669 		    ibcm_lap_msg_t *lap_msg, ibcm_apr_msg_t *apr_msg);
1670 
1671 void		ibcm_post_apr_mad(ibcm_state_data_t *statep);
1672 
1673 void		ibcm_cep_state_apr(ibcm_state_data_t *statep,
1674 		    ibcm_lap_msg_t *lap_msg, ibcm_apr_msg_t *apr_msg);
1675 
1676 /* Processes CM state machine based on return value from cm handler */
1677 void		ibcm_handle_cep_dreq_response(ibcm_state_data_t *statep,
1678 		    void *priv_data, ibt_priv_data_len_t  priv_data_len);
1679 
1680 /* Processes CM UD state machine based on return values from cm handler */
1681 void		ibcm_process_sidr_req_cm_hdlr(ibcm_ud_state_data_t *ud_statep,
1682 		    ibt_cm_status_t cb_status,
1683 		    ibcm_ud_clnt_reply_info_t *ud_clnt_info,
1684 		    ibt_sidr_status_t *sidr_status,
1685 		    ibcm_sidr_rep_msg_t *sidr_repp);
1686 
1687 void		ibcm_proceed_via_taskq(void *targs);
1688 void		ibcm_ud_proceed_via_taskq(void *targs);
1689 
1690 /*
1691  * Builds the reply MAD address based on "incoming mad addr" that is
1692  * supplied to it as an arg.
1693  *	Swaps the source and destination lids in ibmf_addr_info_t
1694  *	Swaps the source and destination gids in ib_grh_t
1695  *
1696  * INPUTS:
1697  *	incoming_cm_mad_addr	- Address information in the incoming MAD
1698  *	reply_cm_mad_addr	- Derived address for the reply MAD
1699  *				  The reply MAD address is derived based
1700  *				  address information of incoming CM MAD
1701  */
1702 void	ibcm_build_reply_mad_addr(ibcm_mad_addr_t *incoming_cm_mad_addr,
1703 	    ibcm_mad_addr_t *reply_cm_mad_addr);
1704 
1705 /*  Posts RC CM MAD using IBMF */
1706 void	ibcm_post_rc_mad(ibcm_state_data_t *statep, ibmf_msg_t *msgp,
1707 	    ibmf_msg_cb_t post_cb, void *args);
1708 
1709 /*  Posts UD CM MAD using IBMF */
1710 void	ibcm_post_ud_mad(ibcm_ud_state_data_t *ud_statep, ibmf_msg_t *msgp,
1711 	    ibmf_msg_cb_t ud_post_cb, void *args);
1712 
1713 /*  Posts CM MAD using IBMF */
1714 ibt_status_t	ibcm_post_mad(ibmf_msg_t *msgp, ibcm_mad_addr_t *cm_mad_addr,
1715 	    ibmf_msg_cb_t post_cb, void *args);
1716 
1717 /* Post REJ MAD */
1718 void	ibcm_post_rej_mad(ibcm_state_data_t *statep, ibt_cm_reason_t reason,
1719 	    int who, void *addl_rej_info, uint8_t arej_info_len);
1720 
1721 /* Post REP MAD */
1722 void	ibcm_post_rep_mad(ibcm_state_data_t *statep);
1723 
1724 /* Post RTU MAD */
1725 ibcm_status_t	ibcm_post_rtu_mad(ibcm_state_data_t *statep);
1726 
1727 /* Post DREQ MAD */
1728 void	ibcm_post_dreq_mad(void *statep);
1729 
1730 /* Post LAP MAD */
1731 void	ibcm_post_lap_mad(ibcm_state_data_t *statep);
1732 
1733 
1734 /*
1735  * Posts CM SIDR MAD using IBMF in blocking mode
1736  *
1737  * INPUTS:
1738  *	ud_statep:	UD statep which is posting the mad
1739  *	cm_mad_addr:	Address information for the MAD to be posted
1740  *	status:		SIDR status
1741  */
1742 void	ibcm_post_sidr_rep_mad(ibcm_ud_state_data_t *ud_statep,
1743 	    ibt_sidr_status_t status);
1744 
1745 /* prototypes to resend RC mad and UD MAD */
1746 void	ibcm_resend_rep_mad(ibcm_state_data_t *statep);
1747 void	ibcm_resend_rtu_mad(ibcm_state_data_t *statep);
1748 void	ibcm_resend_rej_mad(ibcm_state_data_t *statep);
1749 void	ibcm_resend_mra_mad(ibcm_state_data_t *statep);
1750 void	ibcm_resend_srep_mad(ibcm_ud_state_data_t *statep);
1751 
1752 
1753 /* Helper function used in connection abort processing */
1754 void	ibcm_process_abort(ibcm_state_data_t	*statep);
1755 
1756 /*
1757  * Prototypes for CM functions that lookup for a connection state structure
1758  */
1759 
1760 /*
1761  * ibcm_lookup_msg:
1762  *
1763  * Retrieves an existing state structure or creates a new one if none found.
1764  * This function is used during passive side of connection establishment for
1765  * INCOMING REQ/REJ/RTU/MRA
1766  * This function is used during active side of connection establishment for
1767  * INCOMING REP/REJ/MRA
1768  * This function is used during active side of connection establishment for
1769  * an outgoing REQ.
1770  *
1771  * NOTE: IBCM_LOOKP_FAIL is only returned if a new entry wasn't created and
1772  * a match wasn't found.
1773  *
1774  * Arguments are:-
1775  *	ibcm_event_type_t	- what type of message
1776  *				  incoming REQ, REP, REJ, MRA, RTU, DREQ, DREP
1777  *	local_comid		- ONLY *NOT* valid for incoming REQ.
1778  *					needed for others
1779  *	remote_qpn		- Remote CM's QP number
1780  *	remote_hca_guid		- ONLY VALID FOR incoming REQ.
1781  *				  Ignored for others
1782  *	hcap			- HCA entry table pointer
1783  *	statep			- "return"ed state pointer
1784  *
1785  * Return Values:
1786  *	IBCM_LOOKUP_NEW		- new statep allocated
1787  *	IBCM_LOOKUP_EXISTS	- found an existing entry
1788  *	IBCM_LOOKUP_FAIL	- failed to find an entry
1789  *	IBCM_MEMORY_FAILURE	- failed to get memory
1790  *					iff flags != IBT_CHAN_BLOCKING
1791  */
1792 ibcm_status_t	ibcm_lookup_msg(ibcm_event_type_t event_type,
1793 		    ib_com_id_t local_comid, ib_qpn_t remote_qpn,
1794 		    ib_guid_t remote_hca_guid, ibcm_hca_info_t *hcap,
1795 		    ibcm_state_data_t **statep);
1796 
1797 
1798 /*
1799  * Routines for CM SIDR state structure list manipulation
1800  * Wherever possible, the list routines of ibtl are used
1801  * for list manipulation
1802  */
1803 
1804 /*
1805  * Finds an entry based on lid, gid and grh exists fields
1806  * lid:		LID of incoming SIDR REQ
1807  * gid:		GID of incoming SIDR REQ
1808  * grh_exists:		TRUE if GRH exists in the incoming SIDR REQ
1809  * hcap:	CM State HCA entry ptr to search for SIDR state structure
1810  * statep:	Returns a valid state structure, if one exists based
1811  *		on lid, gid and grh_exists fields
1812  * flag:	whether to just look OR to look and add if it doesn't exist.
1813  */
1814 ibcm_status_t		ibcm_find_sidr_entry(ibcm_sidr_srch_t *srch_param,
1815 			    ibcm_hca_info_t *hcap,
1816 			    ibcm_ud_state_data_t **statep,
1817 			    ibcm_lookup_flag_t flag);
1818 
1819 ibcm_ud_state_data_t	*ibcm_add_sidr_entry(ibcm_sidr_srch_t *srch_param,
1820 			    ibcm_hca_info_t *hcap);
1821 
1822 /*
1823  * Deletes a given state structure, from both hca state and passive trees
1824  * If ref cnt is zero, deallocates all buffers and memory of state data
1825  */
1826 void	ibcm_delete_state_data(ibcm_state_data_t *statep);
1827 
1828 /*
1829  * Deallocates all the buffers and memory of state data.
1830  * This function must be called, only when ref_cnt is zero.
1831  */
1832 void	ibcm_dealloc_state_data(ibcm_state_data_t *statep);
1833 
1834 /*
1835  * Deletes a given UD state structure, from SIDR list.
1836  * The routine acquires and releases the SIDR list lock.
1837  */
1838 void	ibcm_delete_ud_state_data(ibcm_ud_state_data_t *statep);
1839 void	ibcm_dealloc_ud_state_data(ibcm_ud_state_data_t *statep);
1840 
1841 /*
1842  * Service ID entry create and lookup functions
1843  */
1844 
1845 /*
1846  * Adds/looks-up an ibcm_svc_info_t entry in the CM's global table.
1847  * This global table is defined in ibcm_impl.c.
1848  *
1849  * svc_info_list_lock must be held for RW_READER by caller of
1850  * ibcm_find_svc_entry().
1851  *
1852  * Arguments are:-
1853  *	sid		- service id
1854  *	num_sids	- Number (Range) of service-ids
1855  *
1856  * Return values:
1857  *	Pointer to ibcm_svc_info_t on success, otherwise NULL.
1858  */
1859 int ibcm_svc_compare(const void *p1, const void *p2);
1860 ibcm_svc_info_t *ibcm_create_svc_entry(ib_svc_id_t sid, int num_sids);
1861 ibcm_svc_info_t *ibcm_find_svc_entry(ib_svc_id_t sid);
1862 
1863 /*
1864  * The following are the function prototypes for various id initialization,
1865  * allocation, free and destroy operations. The cm id allocations are based
1866  * on vmem operations
1867  * The service id's are maintained globally per host
1868  * The com id and req id's are maintained per hca
1869  * To maintain compatibility with intel, service ids are allocated on a 32 bit
1870  * range, though spec has 64 bit range for service id's
1871  */
1872 ibcm_status_t	ibcm_init_ids();
1873 void		ibcm_fini_ids();
1874 
1875 ibcm_status_t	ibcm_init_hca_ids(ibcm_hca_info_t *hcap);
1876 void		ibcm_fini_hca_ids(ibcm_hca_info_t *hcap);
1877 
1878 ibcm_status_t	ibcm_alloc_comid(ibcm_hca_info_t *hcap, ib_com_id_t *comid);
1879 void		ibcm_free_comid(ibcm_hca_info_t *hcap, ib_com_id_t comid);
1880 
1881 ibcm_status_t	ibcm_alloc_reqid(ibcm_hca_info_t *hcap, uint32_t *reqid);
1882 void		ibcm_free_reqid(ibcm_hca_info_t *hcap, uint32_t reqid);
1883 
1884 ib_svc_id_t	ibcm_alloc_local_sids(int num_sids);
1885 void		ibcm_free_local_sids(ib_svc_id_t service_id, int num_sids);
1886 
1887 ib_svc_id_t	ibcm_alloc_ip_sid();
1888 void		ibcm_free_ip_sid(ib_svc_id_t sid);
1889 
1890 uint64_t	ibcm_generate_tranid(uint8_t event, uint32_t id,
1891 		    uint32_t cm_tran_priv);
1892 
1893 void		ibcm_decode_tranid(uint64_t tran_id, uint32_t *cm_tran_priv);
1894 
1895 ibcm_status_t	ibcm_ar_init(void);
1896 ibcm_status_t	ibcm_ar_fini(void);
1897 
1898 /* IP Addressing API debugging */
1899 extern int ibcm_printip;	/* set to 1 to enable IBTF DPRINTFs */
1900 extern void ibcm_ip_print(char *label, ibt_ip_addr_t *ipa);
1901 
1902 #define	IBCM_PRINT_IP(LABEL, IP_ADDR)			\
1903 	if (ibcm_printip) {			\
1904 		ibcm_ip_print(LABEL, IP_ADDR);	\
1905 	}
1906 /*
1907  * These functions are called to do timeout processing from CM connection
1908  * state transitions. (Also for SIDR REQ and SIDR REP processing)
1909  *
1910  * Brief description :
1911  *	If retry count is below max retry value, then post the stored response
1912  *	MAD using IBMF in blocking mode, adjusts remaining retry counters.
1913  *	If retry counter reaches max value, then retry failure handling is
1914  *	done here
1915  *
1916  *	CM will ensure that the state data structure of the associated
1917  *	timeout is valid when this timeout function is called.
1918  *	(See timer_stored_state in ibcm_state_data_t and
1919  *	ud_timer_stored_state in ibcm_ud_state_data_t)
1920  */
1921 void	ibcm_timeout_cb(void *arg);
1922 void	ibcm_sidr_timeout_cb(void *arg);
1923 
1924 /*
1925  * function prototypes for IBMF send completion callbacks on non-blocking
1926  * MAD posts
1927  */
1928 void	ibcm_post_req_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1929 	    void *args);
1930 void	ibcm_post_rep_wait_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1931 	    void *args);	/* MRA Rcvd on active side */
1932 void	ibcm_post_rep_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1933 	    void *args);
1934 void	ibcm_resend_post_rep_complete(ibmf_handle_t ibmf_handle,
1935 	    ibmf_msg_t *msgp, void *args);
1936 void	ibcm_post_mra_rep_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1937 	    void *args);	/* MRA Rcvd on passive side */
1938 void	ibcm_post_rej_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1939 	    void *args);
1940 void	ibcm_post_dreq_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1941 	    void *args);
1942 void	ibcm_post_drep_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1943 	    void *args);
1944 void	ibcm_post_lap_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1945 	    void *args);
1946 void	ibcm_post_apr_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1947 	    void *args);
1948 void	ibcm_post_stored_apr_complete(ibmf_handle_t ibmf_handle,
1949 	    ibmf_msg_t *msgp, void *args);
1950 void	ibcm_post_mra_lap_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1951 	    void *args);	/* MRA Rcvd for LAP on active side */
1952 void	ibcm_post_mra_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1953 	    void *args);	/* for MRA sender */
1954 void	ibcm_post_rtu_complete(ibmf_handle_t ibmf_handle, ibmf_msg_t *msgp,
1955 	    void *args);
1956 
1957 void	ibcm_post_sidr_req_complete(ibmf_handle_t ibmf_handle,
1958 	    ibmf_msg_t *msgp, void *args);
1959 
1960 /*
1961  * ibcm_find_hca_entry:
1962  *	Given a HCA's GUID find out ibcm_hca_info_t entry for that HCA
1963  *	This entry can be then used to access AVL tree/SIDR list etc.
1964  *
1965  *	NOTE: This entry is not removed from the "ibcm_hca_listp".
1966  *	And this function is called with ibcm_hca_list_mutex mutex held.
1967  *
1968  * INPUTS:
1969  *	hca_guid	- HCA's guid
1970  *
1971  * RETURN VALUE:
1972  *	hcap		- if a match is found, else NULL
1973  */
1974 ibcm_hca_info_t	*ibcm_find_hca_entry(ib_guid_t hca_guid);
1975 ibcm_hca_info_t	*ibcm_find_hcap_entry(ib_guid_t hca_guid);
1976 void ibcm_delete_hca_entry(ibcm_hca_info_t *hcap);
1977 
1978 /* Routines that manage the hca's temporary access count */
1979 ibcm_status_t ibcm_inc_hca_acc_cnt(ibcm_hca_info_t *hca);
1980 void ibcm_dec_hca_acc_cnt(ibcm_hca_info_t *hca);
1981 
1982 /* Routines that manage the hca's resource count */
1983 void ibcm_inc_hca_res_cnt(ibcm_hca_info_t *hca);
1984 void ibcm_dec_hca_res_cnt(ibcm_hca_info_t *hca);
1985 
1986 /* Routines that manage the hca's service count */
1987 void ibcm_inc_hca_svc_cnt(ibcm_hca_info_t *hca);
1988 void ibcm_dec_hca_svc_cnt(ibcm_hca_info_t *hca);
1989 
1990 /* Routine to fetch the saa_handle */
1991 ibmf_saa_handle_t ibcm_get_saa_handle(ibcm_hca_info_t *hcap, uint8_t port);
1992 
1993 /* Allow some flow control of RC connection initiations */
1994 void ibcm_flow_inc(void);
1995 void ibcm_flow_dec(hrtime_t delta, char *mad_type);
1996 
1997 /* Allow some flow control of SA requests */
1998 void ibcm_sa_access_enter(void);
1999 void ibcm_sa_access_exit(void);
2000 
2001 /*
2002  * ibcm_cep_to_error_state:
2003  *	Helper function to transition a CEP to ERROR state
2004  *
2005  *	NOTE: This function checks if ch_qp is valid or ch_eec and calls
2006  *	into IBTL to transition the CEP.
2007  *
2008  * INPUTS:
2009  *	statep	- Connection state pointer
2010  *
2011  * RETURN VALUE:
2012  *	IBT_SUCCESS	- if CEP transition succeeded; else error
2013  */
2014 ibt_status_t	ibcm_cep_to_error_state(ibcm_state_data_t *statep);
2015 
2016 /*
2017  * Processes the pending stateps in a linked list. The operations are to
2018  * invoke a cm handler or delete statep
2019  * When the above operations are required on statep from a timeout handler,
2020  * they are linked for later processing by an independent thread
2021  */
2022 void	ibcm_process_tlist();
2023 /* Links RC stateps to an RC timeout processing list */
2024 void	ibcm_add_tlist(ibcm_state_data_t *statep);
2025 
2026 /* Links SIDR/UD stateps to an SIDR/UD timeout processing list */
2027 void	ibcm_add_ud_tlist(ibcm_ud_state_data_t *ud_statep);
2028 
2029 /*
2030  * This call either aborts a pending or completes a in-progress LAP/APR
2031  * operation
2032  */
2033 void	ibcm_sync_lapr_idle(ibcm_state_data_t	*statep);
2034 
2035 void	ibcm_process_rc_recycle(void *recycle_arg);
2036 
2037 /*
2038  * Helper function to handle endianess in case of Service Data.
2039  * Used by ibt_bind_service() and ibt_get_paths().
2040  */
2041 void ibcm_swizzle_from_srv(ibt_srv_data_t *sb_data, uint8_t *service_bytes);
2042 void ibcm_swizzle_to_srv(uint8_t *service_bytes, ibt_srv_data_t *sb_data);
2043 
2044 /* Misc ibcm global variables */
2045 extern char			cmlog[];
2046 extern ibt_clnt_hdl_t		ibcm_ibt_handle;
2047 extern taskq_t			*ibcm_taskq;
2048 extern ibcm_state_handler_t	ibcm_sm_funcs_tbl[];
2049 extern uint8_t			ibcm_timeout_list_flags;
2050 extern ibcm_classportinfo_msg_t	ibcm_clpinfo;
2051 
2052 /* Global lists */
2053 extern avl_tree_t	ibcm_svc_avl_tree;	/* global service id tree */
2054 extern ibcm_state_data_t	*ibcm_timeout_list_hdr, *ibcm_timeout_list_tail;
2055 extern ibcm_ud_state_data_t	*ibcm_ud_timeout_list_hdr,
2056 				*ibcm_ud_timeout_list_tail;
2057 /* Default global retry counts */
2058 extern uint8_t		ibcm_max_retries;
2059 extern uint32_t		ibcm_max_sa_retries;
2060 extern int		ibcm_sa_timeout_delay;	/* in ticks */
2061 
2062 /* Various default global timers */
2063 extern ibt_rnr_nak_time_t	ibcm_default_rnr_nak_time;
2064 
2065 extern clock_t		ibcm_local_processing_time;	/* usecs */
2066 extern clock_t		ibcm_remote_response_time;
2067 extern ib_time_t	ibcm_max_sidr_rep_proctime;
2068 extern ib_time_t	ibcm_max_sidr_rep_store_time;
2069 extern uint32_t		ibcm_adj_btime;
2070 extern uint32_t		ibcm_sw_delay;
2071 
2072 extern ib_time_t	ibcm_max_ib_pkt_lt;
2073 extern ib_time_t	ibcm_max_ib_mad_pkt_lt;
2074 
2075 /* Global locks */
2076 extern kmutex_t		ibcm_svc_info_lock;
2077 extern kmutex_t		ibcm_mcglist_lock;
2078 extern kmutex_t		ibcm_global_hca_lock;
2079 extern kmutex_t		ibcm_qp_list_lock;
2080 extern kmutex_t		ibcm_timeout_list_lock;
2081 extern kmutex_t		ibcm_recv_mutex;
2082 
2083 /* Global cond variables */
2084 extern kcondvar_t	ibcm_global_hca_cv;
2085 extern kcondvar_t	ibcm_svc_info_cv;
2086 extern kcondvar_t	ibcm_timeout_list_cv;
2087 extern kcondvar_t	ibcm_timeout_thread_done_cv;
2088 
2089 _NOTE(LOCK_ORDER(ibcm_state_data_s::state_mutex ibcm_timeout_list_lock))
2090 _NOTE(LOCK_ORDER(ibcm_ud_state_data_s::ud_state_mutex ibcm_timeout_list_lock))
2091 _NOTE(LOCK_ORDER(ibcm_hca_info_s::hca_state_rwlock
2092     ibcm_state_data_s::state_mutex))
2093 _NOTE(LOCK_ORDER(ibcm_hca_info_s::hca_sidr_list_lock
2094     ibcm_ud_state_data_s::ud_state_mutex))
2095 
2096 _NOTE(READ_ONLY_DATA(ibcm_local_processing_time ibcm_remote_response_time
2097     ibcm_max_sidr_rep_proctime ibcm_max_sidr_rep_store_time ibcm_adj_btime
2098     ibcm_sw_delay ibcm_max_retries ibcm_max_sa_retries))
2099 
2100 /*
2101  * miscellaneous defines for retries, times etc.
2102  */
2103 #define	IBCM_MAX_RETRIES		11	/* Max CM retries for a msg */
2104 #define	IBCM_LOCAL_RESPONSE_TIME	300000	/* Local CM processing time */
2105 						/* in usecs */
2106 #define	IBCM_REMOTE_RESPONSE_TIME	300000	/* Remote CM response time  */
2107 						/* in usecs */
2108 #define	IBCM_MAX_SIDR_PROCESS_TIME	16	/* Time to process SIDR REP */
2109 #define	IBCM_MAX_SIDR_PKT_LIFE_TIME	9	/* Approx pkt lt for UD srver */
2110 
2111 #define	IBCM_MAX_IB_PKT_LT		20	/* 4 second */
2112 #define	IBCM_MAX_IB_MAD_PKT_LT		18	/* 1 second */
2113 
2114 #define	IBCM_MAX_SA_RETRIES		0	/* Max CM retry for SA update */
2115 
2116 /* versions for CM MADs */
2117 #define	IBCM_MAD_BASE_VERSION		1
2118 #define	IBCM_MAD_CLASS_VERSION		2
2119 
2120 /* for Class_Port_Info stuff - see section 16.7.3.1 in Vol1 IB Spec */
2121 #define	IBCM_CPINFO_CAP_RC		0x0200	/* RC is supported */
2122 #define	IBCM_CPINFO_CAP_RD		0x0400	/* RD is supported */
2123 #define	IBCM_CPINFO_CAP_RAW		0x0800	/* Raw Datagrams supported */
2124 #define	IBCM_CPINFO_CAP_UC		0x1000	/* UC supported */
2125 #define	IBCM_CPINFO_CAP_SIDR		0x2000	/* SIDR supported */
2126 
2127 #define	IBCM_V4_PART_OF_V6(v6)	v6.s6_addr32[3]
2128 /* RDMA CM IP Service's Private Data Format. */
2129 #ifdef _BIG_ENDIAN
2130 typedef struct ibcm_ip_pvtdata_s {
2131 	uint8_t		ip_MajV:4,
2132 			ip_MinV:4;
2133 	uint8_t		ip_ipv:4,
2134 			ip_rsvd:4;	/* 0-3: rsvd, 4-7: ipv */
2135 	uint16_t	ip_srcport;	/* Source Port */
2136 	in6_addr_t	ip_srcip;	/* Source IP address. */
2137 	in6_addr_t	ip_dstip;	/* Remote IP address. */
2138 #define	ip_srcv4	IBCM_V4_PART_OF_V6(ip_srcip)
2139 #define	ip_dstv4	IBCM_V4_PART_OF_V6(ip_dstip)
2140 #define	ip_srcv6	ip_srcip
2141 #define	ip_dstv6	ip_dstip
2142 } ibcm_ip_pvtdata_t;
2143 #else
2144 typedef struct ibcm_ip_pvtdata_s {
2145 	uint8_t		ip_MinV:4,
2146 			ip_MajV:4;
2147 	uint8_t		ip_rsvd:4,
2148 			ip_ipv:4;	/* 0-3: rsvd, 4-7: ipv */
2149 	uint16_t	ip_srcport;	/* Source Port */
2150 	in6_addr_t	ip_srcip;	/* Source IP address. */
2151 	in6_addr_t	ip_dstip;	/* Remote IP address. */
2152 #define	ip_srcv4	IBCM_V4_PART_OF_V6(ip_srcip)
2153 #define	ip_dstv4	IBCM_V4_PART_OF_V6(ip_dstip)
2154 #define	ip_srcv6	ip_srcip
2155 #define	ip_dstv6	ip_dstip
2156 } ibcm_ip_pvtdata_t;
2157 #endif
2158 
2159 /*
2160  * for debug purposes
2161  */
2162 #ifdef	DEBUG
2163 extern	int ibcm_test_mode;
2164 
2165 void	ibcm_query_qp(ibmf_handle_t ibmf_hdl, ibmf_qp_handle_t ibmf_qp);
2166 void	ibcm_dump_raw_message(uchar_t *);
2167 void	ibcm_dump_srvrec(sa_service_record_t *);
2168 void	ibcm_dump_pathrec(sa_path_record_t *);
2169 void	ibcm_dump_noderec(sa_node_record_t *);
2170 
2171 void	ibcm_query_classport_info(ibt_channel_hdl_t channel);
2172 
2173 #define	IBCM_DUMP_RAW_MSG(x)		ibcm_dump_raw_message(x)
2174 #define	IBCM_DUMP_SERVICE_REC(x)	ibcm_dump_srvrec(x)
2175 #define	IBCM_DUMP_PATH_REC(x)		ibcm_dump_pathrec(x)
2176 #define	IBCM_DUMP_NODE_REC(x)		ibcm_dump_noderec(x)
2177 #else
2178 #define	IBCM_DUMP_RAW_MSG(x)
2179 #define	IBCM_DUMP_SERVICE_REC(x)
2180 #define	IBCM_DUMP_PATH_REC(x)
2181 #define	IBCM_DUMP_NODE_REC(x)
2182 #endif
2183 
2184 ibt_status_t ibcm_ibmf_analyze_error(int ibmf_status);
2185 
2186 ibt_status_t ibcm_contact_sa_access(ibmf_saa_handle_t saa_handle,
2187     ibmf_saa_access_args_t *access_args, size_t *length, void **results_p);
2188 
2189 ibt_status_t	ibcm_ibtl_node_info(ib_guid_t, uint8_t, ib_lid_t,
2190     ibt_node_info_t *node_info);
2191 
2192 void ibcm_path_cache_init(void);
2193 void ibcm_path_cache_fini(void);
2194 void ibcm_path_cache_purge(void);
2195 
2196 #ifdef	__cplusplus
2197 }
2198 #endif
2199 
2200 
2201 #endif /* _SYS_IB_MGT_IBCM_IBCM_IMPL_H */
2202