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