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