xref: /illumos-gate/usr/src/uts/common/io/ib/mgt/ibcm/ibcm_ti.c (revision e13e346d8734036862432c746042cff8470e8ebd)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/ib/mgt/ibcm/ibcm_impl.h>
29 #include <sys/ib/ibtl/ibti.h>
30 #include <sys/ib/mgt/ibcm/ibcm_arp.h>
31 
32 /*
33  * ibcm_ti.c
34  *	These routines implement the Communication Manager's interfaces to IBTL.
35  */
36 
37 /* CM rc recycle task args structure definition */
38 typedef struct ibcm_taskq_recycle_arg_s {
39 	ibt_channel_hdl_t	rc_chan;
40 	ibt_cep_flags_t		control;
41 	uint8_t			hca_port_num;
42 	ibt_recycle_handler_t	func;
43 	void			*arg;
44 } ibcm_taskq_recycle_arg_t;
45 
46 _NOTE(READ_ONLY_DATA(ibcm_taskq_recycle_arg_s))
47 
48 static ibt_status_t	ibcm_init_reply_addr(ibcm_hca_info_t *hcap,
49     ibcm_mad_addr_t *reply_addr, ibt_chan_open_args_t *chan_args,
50     ibt_chan_open_flags_t flags, ib_time_t *cm_pkt_lt, ib_lid_t prim_slid);
51 static void		ibcm_process_abort_via_taskq(void *args);
52 static ibt_status_t	ibcm_process_rc_recycle_ret(void *recycle_arg);
53 static ibt_status_t	ibcm_process_join_mcg(void *taskq_arg);
54 static void		ibcm_process_async_join_mcg(void *tq_arg);
55 
56 static ibt_status_t ibcm_get_node_rec(ibmf_saa_handle_t, sa_node_record_t *,
57     uint64_t c_mask, void *, size_t *);
58 
59 static ibt_status_t ibcm_close_rc_channel(ibt_channel_hdl_t channel,
60     ibcm_state_data_t *statep, ibt_execution_mode_t mode);
61 
62 /* Address Record management definitions */
63 #define	IBCM_DAPL_ATS_NAME	"DAPL Address Translation Service"
64 #define	IBCM_DAPL_ATS_SID	0x10000CE100415453ULL
65 #define	IBCM_DAPL_ATS_NBYTES	16
66 ibcm_svc_info_t *ibcm_ar_svcinfop;
67 ibcm_ar_t	*ibcm_ar_list;
68 
69 /*
70  * Tunable parameter to turnoff the overriding of pi_path_mtu value.
71  *	1 	By default override the path record's pi_path_mtu value to
72  *		IB_MTU_1K for all RC channels. This is done only for the
73  *		channels established on Tavor HCA and the path's pi_path_mtu
74  *		is greater than IB_MTU_1K.
75  *	0	Do not override, use pi_path_mtu by default.
76  */
77 int	ibcm_override_path_mtu = 1;
78 
79 #ifdef DEBUG
80 static void	ibcm_print_reply_addr(ibt_channel_hdl_t channel,
81 		    ibcm_mad_addr_t *cm_reply_addr);
82 #endif
83 
84 _NOTE(DATA_READABLE_WITHOUT_LOCK(ibcm_port_info_s::{port_ibmf_hdl}))
85 
86 /* access is controlled between ibcm_sm.c and ibcm_ti.c by CVs */
87 _NOTE(SCHEME_PROTECTS_DATA("Serialized access by CV", {ibt_rc_returns_t
88     ibt_ud_returns_t ibt_ap_returns_t ibt_ar_t}))
89 
90 /*
91  * Typically, clients initialize these args in one api call, and use in
92  * another api
93  */
94 _NOTE(SCHEME_PROTECTS_DATA("Expected usage of ibtl api by client",
95     {ibt_path_info_s ibt_cep_path_s ibt_adds_vect_s ibt_mcg_info_s ib_gid_s
96     ibt_ud_dest_attr_s ibt_ud_dest_s ibt_srv_data_s ibt_redirect_info_s}))
97 
98 /*
99  * ibt_open_rc_channel()
100  *	ibt_open_rc_channel opens a communication channel on the specified
101  *	channel to the specified service. For connection service type qp's
102  *	the CM initiates the CEP to establish the connection and transitions
103  *	the QP/EEC to the "Ready to send" State modifying the QP/EEC's
104  *	attributes as necessary.
105  *	The implementation of this function assumes that alt path is different
106  *	from primary path. It is assumed that the Path functions ensure that.
107  *
108  * RETURN VALUES:
109  *	IBT_SUCCESS	on success (or respective failure on error)
110  */
111 ibt_status_t
112 ibt_open_rc_channel(ibt_channel_hdl_t channel, ibt_chan_open_flags_t flags,
113     ibt_execution_mode_t mode, ibt_chan_open_args_t *chan_args,
114     ibt_rc_returns_t *ret_args)
115 {
116 	/* all fields that are related to REQ MAD formation */
117 
118 	ib_pkey_t		prim_pkey;
119 	ib_lid_t		primary_slid, alternate_slid;
120 	ib_qpn_t		local_qpn = 0;
121 	ib_guid_t		hca_guid;
122 	ib_qkey_t		local_qkey = 0;
123 	ib_eecn_t		local_eecn = 0;
124 	ib_eecn_t		remote_eecn = 0;
125 	boolean_t		primary_grh;
126 	boolean_t		alternate_grh = B_FALSE;
127 	ib_lid_t		base_lid;
128 	ib_com_id_t		local_comid;
129 	ibmf_msg_t		*ibmf_msg, *ibmf_msg_dreq;
130 	ibcm_req_msg_t		*req_msgp;
131 
132 	uint8_t			rdma_in, rdma_out;
133 	uint8_t			cm_retries;
134 	uint64_t		local_cm_proc_time;	/* In usec */
135 	uint8_t			local_cm_resp_time;	/* IB time */
136 	uint64_t		remote_cm_resp_time;	/* In usec */
137 	uint32_t		starting_psn = 0;
138 
139 	/* CM path related fields */
140 	ibmf_handle_t		ibmf_hdl;
141 	ibcm_qp_list_t		*cm_qp_entry;
142 	ibcm_mad_addr_t		cm_reply_addr;
143 
144 	uint8_t			cm_pkt_lt;
145 
146 	/* Local args for ibtl/internal CM functions called within */
147 	ibt_status_t		status;
148 	ibcm_status_t		lkup_status;
149 	ibt_qp_query_attr_t	qp_query_attr;
150 
151 	/* Other misc local args */
152 	ibt_priv_data_len_t	len;
153 	ibcm_hca_info_t		*hcap;
154 	ibcm_state_data_t	*statep;
155 	uint8_t			port_no;
156 
157 	IBTF_DPRINTF_L3(cmlog, "ibt_open_rc_channel(chan %p, %X, %x, %p, %p)",
158 	    channel, flags, mode, chan_args, ret_args);
159 
160 	if (IBCM_INVALID_CHANNEL(channel)) {
161 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: invalid channel");
162 		return (IBT_CHAN_HDL_INVALID);
163 	}
164 
165 	/* cm handler should always be specified */
166 	if (chan_args->oc_cm_handler == NULL) {
167 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
168 		    "CM handler is not be specified", channel);
169 		return (IBT_INVALID_PARAM);
170 	}
171 
172 	if (mode == IBT_NONBLOCKING) {
173 		if (ret_args != NULL) {
174 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
175 			    " ret_args should be NULL when called in "
176 			    "non-blocking mode", channel);
177 			return (IBT_INVALID_PARAM);
178 		}
179 	} else if (mode == IBT_BLOCKING) {
180 		if (ret_args == NULL) {
181 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
182 			    " ret_args should be Non-NULL when called in "
183 			    "blocking mode", channel);
184 			return (IBT_INVALID_PARAM);
185 		}
186 		if (ret_args->rc_priv_data_len > IBT_REP_PRIV_DATA_SZ) {
187 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
188 			    " private data length is too large", channel);
189 			return (IBT_INVALID_PARAM);
190 		}
191 		if ((ret_args->rc_priv_data_len > 0) &&
192 		    (ret_args->rc_priv_data == NULL)) {
193 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
194 			    " rc_priv_data_len > 0, but rc_priv_data NULL",
195 			    channel);
196 			return (IBT_INVALID_PARAM);
197 		}
198 	} else { /* any other mode is not valid for ibt_open_rc_channel */
199 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
200 		    "invalid mode %x specified", channel, mode);
201 		return (IBT_INVALID_PARAM);
202 	}
203 
204 	/*
205 	 * XXX: no support yet for ibt_chan_open_flags_t - IBT_OCHAN_DUP
206 	 */
207 	if (flags & IBT_OCHAN_DUP) {
208 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
209 		    "Unsupported Flags specified: 0x%X", channel, flags);
210 		return (IBT_INVALID_PARAM);
211 	}
212 
213 	if ((flags & IBT_OCHAN_REDIRECTED) &&
214 	    (flags & IBT_OCHAN_PORT_REDIRECTED)) {
215 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
216 		    "Illegal to specify IBT_OCHAN_REDIRECTED and "
217 		    "IBT_OCHAN_PORT_REDIRECTED flags together", channel);
218 		return (IBT_INVALID_PARAM);
219 	}
220 
221 	if (((flags & IBT_OCHAN_REDIRECTED) &&
222 	    (chan_args->oc_cm_redirect_info == NULL)) ||
223 	    ((flags & IBT_OCHAN_PORT_REDIRECTED) &&
224 	    (chan_args->oc_cm_cep_path == NULL))) {
225 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
226 		    "Redirect flag specified, but respective arg is NULL",
227 		    channel);
228 		return (IBT_INVALID_PARAM);
229 	}
230 
231 	if ((flags & IBT_OCHAN_REDIRECTED) &&
232 	    (chan_args->oc_cm_redirect_info->rdi_dlid == 0) &&
233 	    (chan_args->oc_cm_redirect_info->rdi_gid.gid_guid == 0)) {
234 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
235 		    "Either rdi_dlid or rdi_gid must be specified for"
236 		    " IBT_OCHAN_REDIRECTED", channel);
237 		return (IBT_INVALID_PARAM);
238 	}
239 
240 	/* primary dlid and hca_port_num should never be zero */
241 	port_no = IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num;
242 
243 	if ((IBCM_PRIM_ADDS_VECT(chan_args).av_dlid == 0) && (port_no == 0)) {
244 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
245 		    "Primary Path's information is not valid", channel);
246 		return (IBT_INVALID_PARAM);
247 	}
248 
249 	/* validate SID */
250 	if (chan_args->oc_path->pi_sid == 0) {
251 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
252 		    "ERROR: Service ID in path information is 0", channel);
253 		return (IBT_INVALID_PARAM);
254 	}
255 
256 	/* validate rnr_retry_cnt (enum has more than 3 bits) */
257 	if ((uint_t)chan_args->oc_path_rnr_retry_cnt > IBT_RNR_INFINITE_RETRY) {
258 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
259 		    "ERROR: oc_path_rnr_retry_cnt(%d) is out of range",
260 		    channel, chan_args->oc_path_rnr_retry_cnt);
261 		return (IBT_INVALID_PARAM);
262 	}
263 
264 	/*
265 	 * Ensure that client is not re-using a QP that is still associated
266 	 * with a statep
267 	 */
268 	IBCM_GET_CHAN_PRIVATE(channel, statep);
269 	if (statep != NULL) {
270 		IBCM_RELEASE_CHAN_PRIVATE(channel);
271 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
272 		    "Channel being re-used on active side", channel);
273 		return (IBT_CHAN_IN_USE);
274 	}
275 
276 	/* Get GUID from Channel */
277 	hca_guid = ibt_channel_to_hca_guid(channel);
278 
279 	/* validate QP's hca guid with that from primary path  */
280 	if (hca_guid != chan_args->oc_path->pi_hca_guid) {
281 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
282 		    "GUID from Channel and primary path don't match", channel);
283 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
284 		    "Channel GUID %llX primary path GUID %llX", channel,
285 		    hca_guid, chan_args->oc_path->pi_hca_guid);
286 		return (IBT_CHAN_HDL_INVALID);
287 	}
288 
289 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
290 	    "Local HCA GUID %llX", channel, hca_guid);
291 
292 	status = ibt_query_qp(channel, &qp_query_attr);
293 	if (status != IBT_SUCCESS) {
294 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
295 		    "ibt_query_qp failed %d", channel, status);
296 		return (status);
297 	}
298 
299 	/* If client specified "no port change on QP" */
300 	if ((qp_query_attr.qp_info.qp_transport.rc.rc_path.cep_hca_port_num !=
301 	    port_no) && (flags & IBT_OCHAN_PORT_FIXED)) {
302 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
303 		    "chan port %d and path port %d does not match", channel,
304 		    qp_query_attr.qp_info.qp_transport.rc.rc_path. \
305 		    cep_hca_port_num, port_no);
306 		return (IBT_INVALID_PARAM);
307 	}
308 
309 	if (qp_query_attr.qp_info.qp_trans != IBT_RC_SRV) {
310 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
311 		    "Invalid Channel type: Applicable only to RC Channel",
312 		    channel);
313 		return (IBT_CHAN_SRV_TYPE_INVALID);
314 	}
315 
316 	/* Check if QP is in INIT state or not */
317 	if (qp_query_attr.qp_info.qp_state != IBT_STATE_INIT) {
318 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
319 		    "QP is not in INIT state %x", channel,
320 		    qp_query_attr.qp_info.qp_state);
321 		return (IBT_CHAN_STATE_INVALID);
322 	}
323 
324 	local_qpn = qp_query_attr.qp_qpn;
325 
326 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p Active QPN 0x%x",
327 	    channel, local_qpn);
328 
329 #ifdef	NO_EEC_SUPPORT_YET
330 
331 	if (flags & IBT_OCHAN_RDC_EXISTS) {
332 		ibt_eec_query_attr_t	eec_query_attr;
333 
334 		local_qkey = qp_query_attr.qp_info.qp_transport.rd_qkey;
335 
336 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: RD");
337 
338 		status = ibt_query_eec(channel, &eec_query_attr);
339 		if (status != IBT_SUCCESS) {
340 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
341 			    " ibt_query_eec failed %d", channel, status);
342 			return (status);
343 		}
344 		local_eecn = eec_query_attr.eec_eecn;
345 	}
346 
347 #endif
348 	if (chan_args->oc_path->pi_prim_pkt_lt > ibcm_max_ib_pkt_lt) {
349 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
350 		    "Huge PktLifeTime %d, Max is %d", channel,
351 		    chan_args->oc_path->pi_prim_pkt_lt, ibcm_max_ib_pkt_lt);
352 		return (IBT_PATH_PKT_LT_TOO_HIGH);
353 	}
354 
355 	/* If no HCA found return failure */
356 	if ((hcap = ibcm_find_hca_entry(hca_guid)) == NULL) {
357 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
358 		    "hcap is NULL. Probably hca is not in active state",
359 		    channel);
360 		return (IBT_CHAN_HDL_INVALID);
361 	}
362 
363 	rdma_out = chan_args->oc_rdma_ra_out;
364 	rdma_in = chan_args->oc_rdma_ra_in;
365 
366 	if ((rdma_in > hcap->hca_max_rdma_in_qp) ||
367 	    (rdma_out > hcap->hca_max_rdma_out_qp)) {
368 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
369 		    "rdma in %d/out %d values exceed hca limits(%d/%d)",
370 		    channel, rdma_in, rdma_out, hcap->hca_max_rdma_in_qp,
371 		    hcap->hca_max_rdma_out_qp);
372 		ibcm_dec_hca_acc_cnt(hcap);
373 		return (IBT_INVALID_PARAM);
374 	}
375 
376 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
377 	    "rdma_in %d rdma_out %d", channel, rdma_in, rdma_out);
378 
379 	status = ibt_get_port_state_byguid(hcap->hca_guid, port_no,
380 	    NULL, &base_lid);
381 	if (status != IBT_SUCCESS) {
382 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
383 		    "primary port_num %d not active", channel, port_no);
384 		ibcm_dec_hca_acc_cnt(hcap);
385 		return (status);
386 	}
387 
388 	/* Validate P_KEY Index */
389 	status = ibt_index2pkey_byguid(hcap->hca_guid, port_no,
390 	    IBCM_PRIM_CEP_PATH(chan_args).cep_pkey_ix, &prim_pkey);
391 	if (status != IBT_SUCCESS) {
392 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
393 		    "Invalid Primary PKeyIx %x", channel,
394 		    IBCM_PRIM_CEP_PATH(chan_args).cep_pkey_ix);
395 		ibcm_dec_hca_acc_cnt(hcap);
396 		return (status);
397 	}
398 
399 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
400 	    "primary_port_num %d primary_pkey 0x%x", channel, port_no,
401 	    prim_pkey);
402 
403 	if ((hcap->hca_port_info[port_no - 1].port_ibmf_hdl == NULL) &&
404 	    ((status = ibcm_hca_reinit_port(hcap, port_no - 1))
405 	    != IBT_SUCCESS)) {
406 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
407 		    "ibmf reg or callback setup failed during re-initialize",
408 		    channel);
409 		ibcm_dec_hca_acc_cnt(hcap);
410 		return (status);
411 	}
412 
413 	ibmf_hdl = hcap->hca_port_info[port_no - 1].port_ibmf_hdl;
414 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
415 	    "primary ibmf_hdl = 0x%p", channel, ibmf_hdl);
416 
417 	primary_slid = base_lid + IBCM_PRIM_ADDS_VECT(chan_args).av_src_path;
418 
419 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: channel 0x%p "
420 	    "primary SLID = %x", channel, primary_slid);
421 
422 	/* check first if alternate path exists or not as it is OPTIONAL */
423 	if (IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num != 0) {
424 		uint8_t	alt_port_no;
425 
426 		alt_port_no = IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num;
427 
428 		if (chan_args->oc_path->pi_alt_pkt_lt > ibcm_max_ib_pkt_lt) {
429 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
430 			    "Huge Alt Pkt lt %d", channel,
431 			    chan_args->oc_path->pi_alt_pkt_lt);
432 			ibcm_dec_hca_acc_cnt(hcap);
433 			return (IBT_PATH_PKT_LT_TOO_HIGH);
434 		}
435 
436 		if (port_no != alt_port_no) {
437 
438 			status = ibt_get_port_state_byguid(hcap->hca_guid,
439 			    alt_port_no, NULL, &base_lid);
440 			if (status != IBT_SUCCESS) {
441 
442 				IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
443 				    "chan 0x%p alt_port_num %d inactive %d",
444 				    channel, alt_port_no, status);
445 				ibcm_dec_hca_acc_cnt(hcap);
446 				return (status);
447 			}
448 
449 		}
450 		alternate_slid =
451 		    base_lid + IBCM_ALT_ADDS_VECT(chan_args).av_src_path;
452 
453 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
454 		    "alternate SLID = %x", channel, alternate_slid);
455 	}
456 
457 	/*
458 	 * only pkey needs to be zero'ed, because all other fields are set in
459 	 * in ibcm_init_reply_addr. But, let's bzero the complete struct for
460 	 * any future modifications.
461 	 */
462 	bzero(&cm_reply_addr, sizeof (cm_reply_addr));
463 
464 	/* Initialize the MAD destination address in stored_reply_addr */
465 	if ((status = ibcm_init_reply_addr(hcap, &cm_reply_addr, chan_args,
466 	    flags, &cm_pkt_lt, primary_slid)) != IBT_SUCCESS) {
467 
468 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
469 		    "ibcm_init_reply_addr failed status %d ", channel, status);
470 		ibcm_dec_hca_acc_cnt(hcap);
471 		return (status);
472 	}
473 
474 
475 	/* Initialize the pkey for CM MAD communication */
476 	if (cm_reply_addr.rcvd_addr.ia_p_key == 0)
477 		cm_reply_addr.rcvd_addr.ia_p_key = prim_pkey;
478 
479 #ifdef DEBUG
480 	ibcm_print_reply_addr(channel, &cm_reply_addr);
481 #endif
482 
483 	/* Retrieve an ibmf qp for sending CM MADs */
484 	if ((cm_qp_entry = ibcm_find_qp(hcap, port_no,
485 	    cm_reply_addr.rcvd_addr.ia_p_key)) == NULL) {
486 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
487 		    "unable to allocate ibmf qp for CM MADs", channel);
488 		ibcm_dec_hca_acc_cnt(hcap);
489 		return (IBT_INSUFF_RESOURCE);
490 	}
491 
492 
493 	if (ibcm_alloc_comid(hcap, &local_comid) != IBCM_SUCCESS) {
494 		ibcm_release_qp(cm_qp_entry);
495 		ibcm_dec_hca_acc_cnt(hcap);
496 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
497 		    " Unable to allocate comid", channel);
498 		return (IBT_INSUFF_KERNEL_RESOURCE);
499 	}
500 
501 	/* allocate an IBMF mad buffer (REQ) */
502 	if ((status = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg,
503 	    MAD_METHOD_SEND)) != IBT_SUCCESS) {
504 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
505 		    "chan 0x%p ibcm_alloc_out_msg failed", channel);
506 		ibcm_release_qp(cm_qp_entry);
507 		ibcm_free_comid(hcap, local_comid);
508 		ibcm_dec_hca_acc_cnt(hcap);
509 		return (status);
510 	}
511 
512 	/* allocate an IBMF mad buffer (DREQ) */
513 	if ((status = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg_dreq,
514 	    MAD_METHOD_SEND)) != IBT_SUCCESS) {
515 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
516 		    "chan 0x%p ibcm_alloc_out_msg failed", channel);
517 		(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg);
518 		ibcm_release_qp(cm_qp_entry);
519 		ibcm_free_comid(hcap, local_comid);
520 		ibcm_dec_hca_acc_cnt(hcap);
521 		return (status);
522 	}
523 
524 	/* Init to Init, if QP's port does not match with path information */
525 	if (qp_query_attr.qp_info.qp_transport.rc.rc_path.cep_hca_port_num !=
526 	    IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num) {
527 
528 		ibt_qp_info_t		qp_info;
529 		ibt_cep_modify_flags_t	cep_flags;
530 
531 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
532 		    "chan 0x%p chan port %d", channel,
533 		    qp_query_attr.qp_info.qp_transport.rc.rc_path.\
534 		    cep_hca_port_num);
535 
536 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
537 		    "chan 0x%p path port %d", channel, port_no);
538 
539 		bzero(&qp_info, sizeof (qp_info));
540 		/* For now, set it to RC type */
541 
542 		qp_info.qp_trans = IBT_RC_SRV;
543 		qp_info.qp_state = IBT_STATE_INIT;
544 		qp_info.qp_transport.rc.rc_path.cep_hca_port_num = port_no;
545 
546 		cep_flags = IBT_CEP_SET_STATE | IBT_CEP_SET_PORT;
547 
548 		status = ibt_modify_qp(channel, cep_flags, &qp_info, NULL);
549 
550 		if (status != IBT_SUCCESS) {
551 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
552 			    "chan 0x%p ibt_modify_qp() = %d", channel, status);
553 			ibcm_release_qp(cm_qp_entry);
554 			ibcm_free_comid(hcap, local_comid);
555 			ibcm_dec_hca_acc_cnt(hcap);
556 			(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg);
557 			(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg_dreq);
558 			return (status);
559 		} else
560 			IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
561 			    "chan 0x%p ibt_modify_qp() = %d", channel, status);
562 	}
563 
564 	/* allocate ibcm_state_data_t before grabbing the WRITER lock */
565 	statep = kmem_zalloc(sizeof (ibcm_state_data_t), KM_SLEEP);
566 	rw_enter(&hcap->hca_state_rwlock, RW_WRITER);
567 	lkup_status = ibcm_lookup_msg(IBCM_OUTGOING_REQ, local_comid, 0, 0,
568 	    hcap, &statep);
569 	rw_exit(&hcap->hca_state_rwlock);
570 
571 	/* CM should be seeing this for the first time */
572 	ASSERT(lkup_status == IBCM_LOOKUP_NEW);
573 
574 	/* Increment the hca's resource count */
575 	ibcm_inc_hca_res_cnt(hcap);
576 
577 	/* Once a resource created on hca, no need to hold the acc cnt */
578 	ibcm_dec_hca_acc_cnt(hcap);
579 
580 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*statep))
581 
582 	statep->timerid = 0;
583 	statep->local_hca_guid = hca_guid;
584 	statep->local_qpn = local_qpn;
585 	statep->stored_reply_addr.cm_qp_entry = cm_qp_entry;
586 	statep->prim_port = IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num;
587 	statep->alt_port = IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num;
588 
589 
590 	/* Save "statep" as channel's CM private data.  */
591 	statep->channel = channel;
592 	IBCM_SET_CHAN_PRIVATE(statep->channel, statep);
593 
594 	statep->stored_msg = ibmf_msg;
595 	statep->dreq_msg = ibmf_msg_dreq;
596 
597 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*req_msgp))
598 
599 	/* Start filling in the REQ MAD */
600 	req_msgp = (ibcm_req_msg_t *)IBCM_OUT_MSGP(statep->stored_msg);
601 	req_msgp->req_local_comm_id = h2b32(local_comid);
602 	req_msgp->req_svc_id = h2b64(chan_args->oc_path->pi_sid);
603 	req_msgp->req_local_ca_guid = h2b64(hca_guid);
604 	req_msgp->req_local_qkey = h2b32(local_qkey);	/* for EEC/RD */
605 
606 	/* Bytes 32-35 are req_local_qpn and req_off_resp_resources */
607 	req_msgp->req_local_qpn_plus = h2b32(local_qpn << 8 | rdma_in);
608 
609 	/* Bytes 36-39 are req_local_eec_no and req_off_initiator_depth */
610 	req_msgp->req_local_eec_no_plus = h2b32(local_eecn << 8 | rdma_out);
611 
612 	if (flags & IBT_OCHAN_REMOTE_CM_TM)
613 		remote_cm_resp_time = chan_args->oc_remote_cm_time;
614 	else
615 		remote_cm_resp_time = ibcm_remote_response_time;
616 
617 	/*
618 	 * Bytes 40-43 - remote_eecn, remote_cm_resp_time, tran_type,
619 	 * IBT_CM_FLOW_CONTROL is always set by default.
620 	 */
621 	req_msgp->req_remote_eecn_plus = h2b32(
622 	    remote_eecn << 8 | (ibt_usec2ib(remote_cm_resp_time) & 0x1f) << 3 |
623 	    IBT_RC_SRV << 1 | IBT_CM_FLOW_CONTROL);
624 
625 	if (flags & IBT_OCHAN_LOCAL_CM_TM)
626 		local_cm_proc_time = chan_args->oc_local_cm_time;
627 	else
628 		local_cm_proc_time = ibcm_local_processing_time;
629 
630 	local_cm_resp_time = ibt_usec2ib(local_cm_proc_time +
631 	    2 * ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt) +
632 	    ibcm_sw_delay);
633 
634 	/* save retry count */
635 	statep->cep_retry_cnt = chan_args->oc_path_retry_cnt;
636 
637 	if (flags & IBT_OCHAN_STARTING_PSN)
638 		starting_psn = chan_args->oc_starting_psn;
639 
640 	if (local_cm_resp_time > 0x1f)
641 		local_cm_resp_time = 0x1f;
642 
643 	/* Bytes 44-47 are req_starting_psn, local_cm_resp_time and retry_cnt */
644 	req_msgp->req_starting_psn_plus = h2b32(starting_psn << 8 |
645 	    local_cm_resp_time << 3 | statep->cep_retry_cnt);
646 
647 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
648 	    "Prim Pkt lt (IB time) 0x%x", channel,
649 	    chan_args->oc_path->pi_prim_pkt_lt);
650 
651 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
652 	    "local_cm_proc_time(usec) %d ", channel, local_cm_proc_time);
653 
654 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
655 	    "local_cm_resp_time(ib_time) %d", channel, local_cm_resp_time);
656 
657 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
658 	    "remote_cm_resp_time (usec) %d", channel, remote_cm_resp_time);
659 
660 	statep->starting_psn = starting_psn;
661 
662 	/* Pkey - bytes 48-49 */
663 	req_msgp->req_part_key = h2b16(prim_pkey);
664 
665 	if (flags & IBT_OCHAN_CM_RETRY)
666 		cm_retries = chan_args->oc_cm_retry_cnt;
667 	else
668 		cm_retries = ibcm_max_retries;
669 
670 	statep->max_cm_retries = statep->remaining_retry_cnt = cm_retries;
671 	req_msgp->req_max_cm_retries_plus = statep->max_cm_retries << 4;
672 
673 	/*
674 	 * Check whether SRQ is associated with this Channel, if yes, then
675 	 * set the SRQ Exists bit in the REQ.
676 	 */
677 	if (qp_query_attr.qp_srq != NULL) {
678 		req_msgp->req_max_cm_retries_plus |= (1 << 3);
679 	}
680 
681 	/*
682 	 * By default on Tavor, we override the PathMTU to 1K.
683 	 * To turn this off, set ibcm_override_path_mtu = 0.
684 	 */
685 	if (ibcm_override_path_mtu && IBCM_IS_HCA_TAVOR(hcap) &&
686 	    (chan_args->oc_path->pi_path_mtu > IB_MTU_1K)) {
687 		req_msgp->req_mtu_plus = IB_MTU_1K << 4 |
688 		    chan_args->oc_path_rnr_retry_cnt;
689 		IBTF_DPRINTF_L3(cmlog, "ibt_open_rc_channel: chan 0x%p PathMTU"
690 		    " overidden to IB_MTU_1K(%d) from %d", channel, IB_MTU_1K,
691 		    chan_args->oc_path->pi_path_mtu);
692 	} else
693 		req_msgp->req_mtu_plus = chan_args->oc_path->pi_path_mtu << 4 |
694 		    chan_args->oc_path_rnr_retry_cnt;
695 
696 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p CM retry cnt %d"
697 	    " staring PSN %x", channel, cm_retries, starting_psn);
698 
699 
700 #ifdef	NO_EEC_SUPPORT_YET
701 	if (flags & IBT_OCHAN_RDC_EXISTS)
702 		req_msgp->req_mtu_plus |= 8;
703 #endif
704 
705 	/* Initialize the "primary" port stuff next - bytes 52-95 */
706 	req_msgp->req_primary_l_port_lid = h2b16(primary_slid);
707 	req_msgp->req_primary_r_port_lid =
708 	    h2b16(IBCM_PRIM_ADDS_VECT(chan_args).av_dlid);
709 	req_msgp->req_primary_l_port_gid.gid_prefix =
710 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_sgid.gid_prefix);
711 	req_msgp->req_primary_l_port_gid.gid_guid =
712 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_sgid.gid_guid);
713 	req_msgp->req_primary_r_port_gid.gid_prefix =
714 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_prefix);
715 	req_msgp->req_primary_r_port_gid.gid_guid =
716 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid);
717 	primary_grh = IBCM_PRIM_ADDS_VECT(chan_args).av_send_grh;
718 
719 	statep->remote_hca_guid = /* not correct, but helpful for debugging */
720 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid;
721 
722 	/* Bytes 88-91 - primary_flowlbl, and primary_srate */
723 	req_msgp->req_primary_flow_label_plus =
724 	    h2b32(((primary_grh == B_TRUE) ?
725 	    (IBCM_PRIM_ADDS_VECT(chan_args).av_flow << 12) : 0) |
726 	    IBCM_PRIM_ADDS_VECT(chan_args).av_srate);
727 	req_msgp->req_primary_traffic_class = (primary_grh == B_TRUE) ?
728 	    IBCM_PRIM_ADDS_VECT(chan_args).av_tclass : 0;
729 	req_msgp->req_primary_hop_limit = (primary_grh == B_TRUE) ?
730 	    IBCM_PRIM_ADDS_VECT(chan_args).av_hop : 1;
731 	req_msgp->req_primary_sl_plus =
732 	    IBCM_PRIM_ADDS_VECT(chan_args).av_srvl << 4 |
733 	    ((primary_grh == B_TRUE) ? 0 : 8);
734 
735 	req_msgp->req_primary_localtime_plus =
736 	    ibt_usec2ib((2 * ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt)) +
737 	    ibt_ib2usec(hcap->hca_ack_delay)) << 3;
738 
739 	IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan %p statep %p",
740 	    channel, statep);
741 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
742 	    "active hca_ack_delay (usec) %d", channel,
743 	    req_msgp->req_primary_localtime_plus);
744 
745 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
746 	    "Sent primary cep timeout (IB Time) %d", channel,
747 	    hcap->hca_ack_delay);
748 
749 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p prim_dlid %x ",
750 	    channel, IBCM_PRIM_ADDS_VECT(chan_args).av_dlid);
751 
752 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
753 	    "prim GID %llX:%llX", channel,
754 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_prefix,
755 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid);
756 
757 	/* Initialize the "alternate" port stuff - optional */
758 	if (chan_args->oc_path->pi_alt_cep_path.cep_hca_port_num != 0) {
759 		ib_gid_t	tmp_gid;
760 
761 		req_msgp->req_alt_l_port_lid = h2b16(alternate_slid);
762 		req_msgp->req_alt_r_port_lid =
763 		    h2b16(IBCM_ALT_ADDS_VECT(chan_args).av_dlid);
764 		/*
765 		 * doing all this as req_alt_r/l_port_gid is at offset
766 		 * 100, 116 which is not divisible by 8
767 		 */
768 
769 		tmp_gid.gid_prefix =
770 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_prefix);
771 		tmp_gid.gid_guid =
772 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_guid);
773 		bcopy(&tmp_gid, &req_msgp->req_alt_r_port_gid[0],
774 		    sizeof (ib_gid_t));
775 		tmp_gid.gid_prefix =
776 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_sgid.gid_prefix);
777 		tmp_gid.gid_guid =
778 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_sgid.gid_guid);
779 
780 		bcopy(&tmp_gid, &req_msgp->req_alt_l_port_gid[0],
781 		    sizeof (ib_gid_t));
782 		alternate_grh = IBCM_ALT_ADDS_VECT(chan_args).av_send_grh;
783 
784 		/* Bytes 132-135 - alternate_flow_label, and alternate srate */
785 		req_msgp->req_alt_flow_label_plus = h2b32(
786 		    (((alternate_grh == B_TRUE) ?
787 		    (IBCM_ALT_ADDS_VECT(chan_args).av_flow << 12) : 0) |
788 		    IBCM_ALT_ADDS_VECT(chan_args).av_srate));
789 		req_msgp->req_alt_traffic_class = (alternate_grh == B_TRUE) ?
790 		    IBCM_ALT_ADDS_VECT(chan_args).av_tclass : 0;
791 		req_msgp->req_alt_hop_limit = (alternate_grh == B_TRUE) ?
792 		    IBCM_ALT_ADDS_VECT(chan_args).av_hop : 1;
793 		req_msgp->req_alt_sl_plus =
794 		    IBCM_ALT_ADDS_VECT(chan_args).av_srvl << 4 |
795 		    ((alternate_grh == B_TRUE) ? 0 : 8);
796 		req_msgp->req_alt_localtime_plus = ibt_usec2ib((2 *
797 		    ibt_ib2usec(chan_args->oc_path->pi_alt_pkt_lt)) +
798 		    ibt_ib2usec(hcap->hca_ack_delay)) << 3;
799 
800 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
801 		    "alt_dlid %x ", channel,
802 		    IBCM_ALT_ADDS_VECT(chan_args).av_dlid);
803 
804 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
805 		    "alt GID %llX:%llX", channel,
806 		    IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_prefix,
807 		    IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_guid);
808 	}
809 
810 	len = min(chan_args->oc_priv_data_len, IBT_REQ_PRIV_DATA_SZ);
811 	if ((len > 0) && chan_args->oc_priv_data)
812 		bcopy(chan_args->oc_priv_data, req_msgp->req_private_data, len);
813 
814 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*req_msgp))
815 
816 	/* return_data is filled up in the state machine code */
817 	if (ret_args != NULL) {
818 		statep->open_return_data = ret_args;
819 	}
820 
821 	/* initialize some statep fields here */
822 	statep->mode = IBCM_ACTIVE_MODE;
823 	statep->hcap = hcap;
824 
825 	statep->cm_handler = chan_args->oc_cm_handler;
826 	statep->state_cm_private = chan_args->oc_cm_clnt_private;
827 
828 	statep->pkt_life_time =
829 	    ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt);
830 
831 	statep->timer_value = ibt_ib2usec(ibt_usec2ib(
832 	    2 * ibt_ib2usec(cm_pkt_lt) + remote_cm_resp_time));
833 
834 	/* Initialize statep->stored_reply_addr */
835 	statep->stored_reply_addr.ibmf_hdl = ibmf_hdl;
836 
837 	/* Initialize stored reply addr fields */
838 	statep->stored_reply_addr.grh_hdr = cm_reply_addr.grh_hdr;
839 	statep->stored_reply_addr.rcvd_addr = cm_reply_addr.rcvd_addr;
840 	statep->stored_reply_addr.grh_exists = cm_reply_addr.grh_exists;
841 	statep->stored_reply_addr.port_num = cm_reply_addr.port_num;
842 
843 	/*
844 	 * The IPD on local/active side is calculated by path functions,
845 	 * hence available in the args of ibt_open_rc_channel
846 	 */
847 	statep->local_srate = IBCM_PRIM_ADDS_VECT(chan_args).av_srate;
848 	statep->local_alt_srate = IBCM_ALT_ADDS_VECT(chan_args).av_srate;
849 
850 	/* Store the source path bits for primary and alt paths */
851 	statep->prim_src_path_bits = IBCM_PRIM_ADDS_VECT(chan_args).av_src_path;
852 	statep->alt_src_path_bits = IBCM_ALT_ADDS_VECT(chan_args).av_src_path;
853 
854 	statep->open_flow = 1;
855 	statep->open_done = B_FALSE;
856 	statep->state = statep->timer_stored_state = IBCM_STATE_REQ_SENT;
857 	IBCM_REF_CNT_INCR(statep);	/* Decremented before return */
858 	IBCM_REF_CNT_INCR(statep);	/* Decremented after REQ is posted */
859 	statep->send_mad_flags |= IBCM_REQ_POST_BUSY;
860 
861 	IBCM_OUT_HDRP(statep->stored_msg)->AttributeID =
862 	    h2b16(IBCM_INCOMING_REQ + IBCM_ATTR_BASE_ID);
863 
864 	IBCM_OUT_HDRP(statep->stored_msg)->TransactionID =
865 	    h2b64(ibcm_generate_tranid(IBCM_INCOMING_REQ, statep->local_comid,
866 	    0));
867 
868 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*statep))
869 
870 	ibcm_open_enqueue(statep);
871 
872 	mutex_enter(&statep->state_mutex);
873 
874 	if (mode == IBT_BLOCKING) {
875 
876 		/* wait for REQ/REP/RTU */
877 		while (statep->open_done != B_TRUE) {
878 			cv_wait(&statep->block_client_cv, &statep->state_mutex);
879 		}
880 
881 		/*
882 		 * In the case that open_channel() fails because of a
883 		 * REJ or timeout, change retval to IBT_CM_FAILURE
884 		 */
885 		if (statep->open_return_data->rc_status != IBT_CM_ACCEPT)
886 			status = IBT_CM_FAILURE;
887 
888 		IBTF_DPRINTF_L3(cmlog, "ibt_open_rc_channel: chan 0x%p "
889 		    "ret status %d cm status %d", channel, status,
890 		    statep->open_return_data->rc_status);
891 	}
892 
893 	/* decrement the ref-count before leaving here */
894 	IBCM_REF_CNT_DECR(statep);
895 
896 	mutex_exit(&statep->state_mutex);
897 
898 	IBTF_DPRINTF_L4(cmlog, "ibt_open_rc_channel: chan 0x%p done", channel);
899 	return (status);
900 }
901 
902 /*
903  * ibcm_init_reply_addr:
904  *
905  * The brief description of functionality below.
906  *
907  * For IBT_OCHAN_PORT_REDIRECTED (ie., port redirected case):
908  *	Build CM path from chan_args->oc_cm_cep_path
909  *	Set CM pkt lt (ie.,life time) to chan_args->oc_cm_pkt_lt
910  *
911  * For IBT_OCHAN_REDIRECTED (ie., port and CM redirected case):
912  *	If Redirect LID is specified,
913  *		If Redirect GID is not specified or specified to be on the same
914  *		    subnet, then
915  *			Build CM path from chan_args->oc_cm_redirect_info
916  *			Set CM pkt lt to subnet timeout
917  *		Else (ie., GID specified, but on a different subnet)
918  *			Do a path lookup to build CM Path and set CM pkt lt
919  *
920  */
921 static ibt_status_t
922 ibcm_init_reply_addr(ibcm_hca_info_t *hcap, ibcm_mad_addr_t *reply_addr,
923     ibt_chan_open_args_t *chan_args, ibt_chan_open_flags_t flags,
924     ib_time_t *cm_pkt_lt, ib_lid_t prim_slid)
925 {
926 	ibt_adds_vect_t	*cm_adds;
927 	ibt_path_info_t	path;
928 	boolean_t	cm_grh;
929 	ibt_status_t	status;
930 
931 	IBTF_DPRINTF_L5(cmlog, "ibcm_init_reply_addr:");
932 
933 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*reply_addr))
934 
935 	/*
936 	 * sending side CM lid/gid/port num are not based on any redirect
937 	 * params. These values are set to primary RC path lid/gid/port num.
938 	 * In the future, these values can be set based on framework policy
939 	 * decisions ensuring reachability.
940 	 */
941 	reply_addr->grh_hdr.ig_sender_gid =
942 	    IBCM_PRIM_ADDS_VECT(chan_args).av_sgid;
943 	reply_addr->rcvd_addr.ia_local_lid = prim_slid;
944 	reply_addr->port_num = IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num;
945 
946 	if (flags & IBT_OCHAN_PORT_REDIRECTED) {
947 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: "
948 		    "IBT_OCHAN_PORT_REDIRECTED specified");
949 
950 		status = ibt_index2pkey_byguid(hcap->hca_guid,
951 		    chan_args->oc_cm_cep_path->cep_hca_port_num,
952 		    chan_args->oc_cm_cep_path->cep_pkey_ix,
953 		    &reply_addr->rcvd_addr.ia_p_key);
954 
955 		if (status != IBT_SUCCESS) {
956 			IBTF_DPRINTF_L2(cmlog, "ibcm_init_rely_addr: Invalid "
957 			    "CM PKeyIx %x port_num %x",
958 			    chan_args->oc_cm_cep_path->cep_pkey_ix,
959 			    chan_args->oc_cm_cep_path->cep_hca_port_num);
960 			return (status);
961 		}
962 
963 		cm_adds = &(chan_args->oc_cm_cep_path->cep_adds_vect);
964 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: dlid = %x",
965 		    cm_adds->av_dlid);
966 
967 		reply_addr->rcvd_addr.ia_q_key = IB_GSI_QKEY;
968 		reply_addr->rcvd_addr.ia_remote_qno = 1;
969 		*cm_pkt_lt = chan_args->oc_cm_pkt_lt;
970 
971 	} else if (flags & IBT_OCHAN_REDIRECTED) {
972 		ibt_redirect_info_t	*redirect_info;
973 		ibt_hca_portinfo_t	*port_infop;
974 		uint_t			psize, nports;
975 
976 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: "
977 		    "IBT_OCHAN_REDIRECTED specified");
978 
979 		redirect_info = chan_args->oc_cm_redirect_info;
980 
981 		if ((redirect_info->rdi_gid.gid_prefix == 0) ||
982 		    (redirect_info->rdi_gid.gid_guid == 0)) {
983 			IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
984 			    "ERROR: Re-direct GID value NOT Provided.");
985 			return (IBT_INVALID_PARAM);
986 		}
987 
988 		/* As per spec definition 1.1, it's always IB_GSI_QKEY */
989 		reply_addr->rcvd_addr.ia_q_key = redirect_info->rdi_qkey;
990 		reply_addr->rcvd_addr.ia_remote_qno = redirect_info->rdi_qpn;
991 		reply_addr->rcvd_addr.ia_p_key = redirect_info->rdi_pkey;
992 
993 		/*
994 		 * if LID is non-zero in classportinfo then use classportinfo
995 		 * fields to form CM MAD destination address.
996 		 */
997 		if (redirect_info->rdi_dlid != 0) {
998 			status = ibtl_cm_query_hca_ports_byguid(hcap->hca_guid,
999 			    reply_addr->port_num, &port_infop, &nports, &psize);
1000 			if ((status != IBT_SUCCESS) || (nports == 0)) {
1001 				IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
1002 				    "Query Ports Failed: %d", status);
1003 				return (status);
1004 			} else if (port_infop->p_subnet_timeout >
1005 			    ibcm_max_ib_pkt_lt) {
1006 				IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
1007 				    "large subnet timeout %x port_no %x",
1008 				    port_infop->p_subnet_timeout,
1009 				    reply_addr->port_num);
1010 				ibt_free_portinfo(port_infop, psize);
1011 				return (IBT_PATH_PKT_LT_TOO_HIGH);
1012 			} else {
1013 				IBTF_DPRINTF_L3(cmlog, "ibcm_init_reply_addr: "
1014 				    "subnet timeout %x port_no %x",
1015 				    port_infop->p_subnet_timeout,
1016 				    reply_addr->port_num);
1017 
1018 				*cm_pkt_lt =
1019 				    ibt_ib2usec(min(ibcm_max_ib_mad_pkt_lt,
1020 				    port_infop->p_subnet_timeout));
1021 
1022 				ibt_free_portinfo(port_infop, psize);
1023 			}
1024 
1025 			reply_addr->rcvd_addr.ia_remote_lid =
1026 			    redirect_info->rdi_dlid;
1027 			reply_addr->rcvd_addr.ia_service_level =
1028 			    redirect_info->rdi_sl;
1029 			reply_addr->grh_exists = B_TRUE;
1030 			reply_addr->grh_hdr.ig_recver_gid =
1031 			    redirect_info->rdi_gid;
1032 			reply_addr->grh_hdr.ig_tclass =
1033 			    redirect_info->rdi_tclass;
1034 			reply_addr->grh_hdr.ig_flow_label =
1035 			    redirect_info->rdi_flow;
1036 
1037 			/* Classportinfo doesn't have hoplimit field */
1038 			reply_addr->grh_hdr.ig_hop_limit = 1;
1039 			return (IBT_SUCCESS);
1040 
1041 		} else {
1042 			ibt_path_attr_t	path_attr;
1043 			ib_gid_t	path_dgid[1];
1044 
1045 			/*
1046 			 * If GID is specified, and LID is zero in classportinfo
1047 			 * do a path lookup using specified GID, Pkey,
1048 			 * in classportinfo
1049 			 */
1050 
1051 			bzero(&path_attr, sizeof (path_attr));
1052 
1053 			path_attr.pa_dgids = &path_dgid[0];
1054 			path_attr.pa_dgids[0] = redirect_info->rdi_gid;
1055 
1056 			/*
1057 			 * use reply_addr below, as sender_gid in reply_addr
1058 			 * may have been set above based on some policy decision
1059 			 * for originating end point for CM MADs above
1060 			 */
1061 			path_attr.pa_sgid = reply_addr->grh_hdr.ig_sender_gid;
1062 			path_attr.pa_num_dgids = 1;
1063 			path_attr.pa_pkey = redirect_info->rdi_pkey;
1064 
1065 			if ((status = ibt_get_paths(ibcm_ibt_handle,
1066 			    IBT_PATH_PKEY, &path_attr, 1, &path, NULL)) !=
1067 			    IBT_SUCCESS)
1068 				return (status);
1069 
1070 			/* Initialize cm_adds */
1071 			cm_adds = &path.pi_prim_cep_path.cep_adds_vect;
1072 			*cm_pkt_lt = path.pi_prim_pkt_lt;
1073 		}
1074 
1075 	} else	{ /* cm_pkey initialized in ibt_open_rc_channel */
1076 		reply_addr->rcvd_addr.ia_q_key = IB_GSI_QKEY;
1077 		reply_addr->rcvd_addr.ia_remote_qno = 1;
1078 		*cm_pkt_lt = chan_args->oc_path->pi_prim_pkt_lt;
1079 		cm_adds = &(IBCM_PRIM_ADDS_VECT(chan_args));
1080 	}
1081 
1082 
1083 	cm_grh = cm_adds->av_send_grh;
1084 	reply_addr->grh_exists = cm_grh;
1085 
1086 	reply_addr->rcvd_addr.ia_remote_lid =
1087 	    cm_adds->av_dlid;
1088 	reply_addr->grh_hdr.ig_recver_gid =
1089 	    cm_adds->av_dgid;
1090 	reply_addr->grh_hdr.ig_flow_label =
1091 	    cm_adds->av_flow & IB_GRH_FLOW_LABEL_MASK;
1092 	reply_addr->grh_hdr.ig_tclass =
1093 	    (cm_grh == B_TRUE) ? cm_adds->av_tclass : 0;
1094 	reply_addr->grh_hdr.ig_hop_limit =
1095 	    (cm_grh == B_TRUE) ? cm_adds->av_hop : 1;
1096 	reply_addr->rcvd_addr.ia_service_level =
1097 	    cm_adds->av_srvl;
1098 
1099 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*reply_addr))
1100 
1101 	return (IBT_SUCCESS);
1102 }
1103 
1104 
1105 /*
1106  * ibt_prime_close_rc_channel()
1107  *	It allocates resources required for close channel operation, so
1108  *	ibt_close_rc_channel can be called from interrupt routine.
1109  *
1110  * INPUTS:
1111  *	channel			The address of an ibt_channel_t struct that
1112  *				specifies the channel to open.
1113  *
1114  * RETURN VALUES:
1115  *	IBT_SUCCESS	on success(or respective failure on error)
1116  *
1117  * Clients are typically expected to call this function in established state
1118  */
1119 ibt_status_t
1120 ibt_prime_close_rc_channel(ibt_channel_hdl_t channel)
1121 {
1122 	ibcm_state_data_t	*statep;
1123 	ibt_status_t		status = IBT_SUCCESS;
1124 
1125 	IBTF_DPRINTF_L3(cmlog, "ibt_prime_close_rc_channel(%p)", channel);
1126 
1127 	/* validate channel, first */
1128 	if (IBCM_INVALID_CHANNEL(channel)) {
1129 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1130 		    "invalid channel", channel);
1131 		return (IBT_CHAN_HDL_INVALID);
1132 	}
1133 
1134 	if (ibtl_cm_get_chan_type(channel) != IBT_RC_SRV) {
1135 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1136 		    "Invalid Channel type: Applicable only to RC Channel",
1137 		    channel);
1138 		return (IBT_CHAN_SRV_TYPE_INVALID);
1139 	}
1140 
1141 	/* get the statep */
1142 	IBCM_GET_CHAN_PRIVATE(channel, statep);
1143 
1144 	/*
1145 	 * This can happen, if the statep is already gone by a DREQ from
1146 	 * the remote side
1147 	 */
1148 
1149 	if (statep == NULL) {
1150 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1151 		    "statep NULL", channel);
1152 		return (IBT_SUCCESS);
1153 	}
1154 
1155 	mutex_enter(&statep->state_mutex);
1156 	IBCM_RELEASE_CHAN_PRIVATE(channel);
1157 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1158 		mutex_exit(&statep->state_mutex);
1159 		return (IBT_CHAN_STATE_INVALID);
1160 	}
1161 	IBCM_REF_CNT_INCR(statep);
1162 	IBTF_DPRINTF_L4(cmlog, "ibt_prime_close_rc_channel: chan 0x%p statep %p"
1163 	    " state %x", channel, statep, statep->state);
1164 	mutex_exit(&statep->state_mutex);
1165 
1166 	/* clients could pre-allocate dreq mad, even before connection est */
1167 	if (statep->dreq_msg == NULL)
1168 		status = ibcm_alloc_out_msg(statep->stored_reply_addr.ibmf_hdl,
1169 		    &statep->dreq_msg, MAD_METHOD_SEND);
1170 
1171 	mutex_enter(&statep->state_mutex);
1172 	IBCM_REF_CNT_DECR(statep);
1173 	mutex_exit(&statep->state_mutex);
1174 
1175 	if (status != IBT_SUCCESS) {
1176 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1177 		    "ibcm_alloc_out_msg failed ", channel);
1178 		return (status);
1179 	}
1180 
1181 	/* If this message isn't seen then ibt_prime_close_rc_channel failed */
1182 	IBTF_DPRINTF_L5(cmlog, "ibt_prime_close_rc_channel: chan 0x%p done",
1183 	    channel);
1184 
1185 	return (IBT_SUCCESS);
1186 }
1187 
1188 /*
1189  * ibt_close_rc_channel()
1190  *	It closes an established channel.
1191  *
1192  * RETURN VALUES:
1193  *	IBT_SUCCESS	on success(or respective failure on error)
1194  */
1195 ibt_status_t
1196 ibt_close_rc_channel(ibt_channel_hdl_t channel, ibt_execution_mode_t mode,
1197     void *priv_data, ibt_priv_data_len_t priv_data_len, uint8_t *ret_status,
1198     void *ret_priv_data, ibt_priv_data_len_t *ret_priv_data_len_p)
1199 {
1200 	ibcm_state_data_t	*statep;
1201 
1202 	IBTF_DPRINTF_L3(cmlog, "ibt_close_rc_channel(%p, %x, %p, %d, %p)",
1203 	    channel, mode, priv_data, priv_data_len,
1204 	    (ret_priv_data_len_p == NULL) ? 0 : *ret_priv_data_len_p);
1205 
1206 	/* validate channel, first */
1207 	if (IBCM_INVALID_CHANNEL(channel)) {
1208 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1209 		    "invalid channel", channel);
1210 		return (IBT_CHAN_HDL_INVALID);
1211 	}
1212 
1213 	if (ibtl_cm_get_chan_type(channel) != IBT_RC_SRV) {
1214 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1215 		    "Invalid Channel type: Applicable only to RC Channel",
1216 		    channel);
1217 		return (IBT_CHAN_SRV_TYPE_INVALID);
1218 	}
1219 
1220 	if (mode == IBT_BLOCKING) {
1221 		/* valid only for BLOCKING MODE */
1222 		if ((ret_priv_data_len_p != NULL) &&
1223 		    (*ret_priv_data_len_p > IBT_DREP_PRIV_DATA_SZ)) {
1224 			IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p"
1225 			    " private data len %d is too large", channel,
1226 			    *ret_priv_data_len_p);
1227 			return (IBT_INVALID_PARAM);
1228 		}
1229 	} else if ((mode != IBT_NONBLOCKING) && (mode != IBT_NOCALLBACKS)) {
1230 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1231 		    "invalid mode %x specified", channel, mode);
1232 		return (IBT_INVALID_PARAM);
1233 	}
1234 
1235 	if (ibtl_cm_is_chan_closing(channel) ||
1236 	    ibtl_cm_is_chan_closed(channel)) {
1237 		if (ret_status)
1238 			*ret_status = IBT_CM_CLOSED_ALREADY;
1239 
1240 		/* No private data to return to the client */
1241 		if (ret_priv_data_len_p != NULL)
1242 			*ret_priv_data_len_p = 0;
1243 
1244 		if ((mode == IBT_BLOCKING) ||
1245 		    (mode == IBT_NOCALLBACKS)) {
1246 			IBCM_GET_CHAN_PRIVATE(channel, statep);
1247 			if (statep == NULL)
1248 				return (IBT_SUCCESS);
1249 			mutex_enter(&statep->state_mutex);
1250 			IBCM_RELEASE_CHAN_PRIVATE(channel);
1251 			IBCM_REF_CNT_INCR(statep);
1252 			while (statep->close_done != B_TRUE)
1253 				cv_wait(&statep->block_client_cv,
1254 				    &statep->state_mutex);
1255 			IBCM_REF_CNT_DECR(statep);
1256 			mutex_exit(&statep->state_mutex);
1257 		}
1258 
1259 		IBTF_DPRINTF_L3(cmlog, "ibt_close_rc_channel: chan 0x%p "
1260 		    "already marked for closing", channel);
1261 
1262 		return (IBT_SUCCESS);
1263 	}
1264 
1265 	/* get the statep */
1266 	IBCM_GET_CHAN_PRIVATE(channel, statep);
1267 	if (statep == NULL) {
1268 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1269 		    "statep NULL", channel);
1270 		return (IBT_CHAN_STATE_INVALID);
1271 	}
1272 
1273 	mutex_enter(&statep->state_mutex);
1274 
1275 	if (statep->dreq_msg == NULL) {
1276 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1277 		    "Fatal Error: dreq_msg is NULL", channel);
1278 		IBCM_RELEASE_CHAN_PRIVATE(channel);
1279 		mutex_exit(&statep->state_mutex);
1280 		return (IBT_CHAN_STATE_INVALID);
1281 	}
1282 
1283 	if ((ret_priv_data == NULL) || (ret_priv_data_len_p == NULL)) {
1284 		statep->close_ret_priv_data = NULL;
1285 		statep->close_ret_priv_data_len = NULL;
1286 	} else {
1287 		statep->close_ret_priv_data = ret_priv_data;
1288 		statep->close_ret_priv_data_len = ret_priv_data_len_p;
1289 	}
1290 
1291 	priv_data_len = min(priv_data_len, IBT_DREQ_PRIV_DATA_SZ);
1292 	if ((priv_data != NULL) && (priv_data_len > 0)) {
1293 		bcopy(priv_data, ((ibcm_dreq_msg_t *)
1294 		    IBCM_OUT_MSGP(statep->dreq_msg))->dreq_private_data,
1295 		    priv_data_len);
1296 	}
1297 	statep->close_ret_status = ret_status;
1298 
1299 	IBCM_RELEASE_CHAN_PRIVATE(channel);
1300 	IBCM_REF_CNT_INCR(statep);
1301 
1302 	if (mode != IBT_NONBLOCKING) {
1303 		return (ibcm_close_rc_channel(channel, statep, mode));
1304 	}
1305 
1306 	/* IBT_NONBLOCKING */
1307 	ibcm_close_enqueue(statep);
1308 	mutex_exit(&statep->state_mutex);
1309 
1310 	return (IBT_SUCCESS);
1311 }
1312 
1313 void
1314 ibcm_close_start(ibcm_state_data_t *statep)
1315 {
1316 	mutex_enter(&statep->state_mutex);
1317 	(void) ibcm_close_rc_channel(statep->channel, statep, IBT_NONBLOCKING);
1318 }
1319 
1320 static
1321 ibt_status_t
1322 ibcm_close_rc_channel(ibt_channel_hdl_t channel, ibcm_state_data_t *statep,
1323     ibt_execution_mode_t mode)
1324 {
1325 	ibcm_hca_info_t		*hcap;
1326 
1327 	_NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&statep->state_mutex));
1328 	ASSERT(MUTEX_HELD(&statep->state_mutex));
1329 
1330 	IBTF_DPRINTF_L3(cmlog, "ibcm_close_rc_channel: chan 0x%p statep %p",
1331 	    channel, statep);
1332 
1333 	hcap = statep->hcap;
1334 
1335 	/* HCA must have been in active state. If not, it's a client bug */
1336 	if (!IBCM_ACCESS_HCA_OK(hcap)) {
1337 		IBTF_DPRINTF_L2(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1338 		    "hcap 0x%p not active", channel, hcap);
1339 		IBCM_REF_CNT_DECR(statep);
1340 		mutex_exit(&statep->state_mutex);
1341 		return (IBT_CHAN_HDL_INVALID);
1342 	}
1343 
1344 	if (statep->state == IBCM_STATE_TRANSIENT_ESTABLISHED) {
1345 		while (statep->cep_in_rts == IBCM_BLOCK)
1346 			cv_wait(&statep->block_mad_cv, &statep->state_mutex);
1347 	}
1348 
1349 	/* Do TRANSIENT_DREQ check after TRANSIENT_ESTABLISHED check */
1350 	while (statep->state == IBCM_STATE_TRANSIENT_DREQ_SENT)
1351 		cv_wait(&statep->block_mad_cv, &statep->state_mutex);
1352 
1353 	IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1354 	    "connection state is %x", channel, statep->state);
1355 
1356 	/* If state is in pre-established states, abort the connection est */
1357 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1358 		statep->cm_retries++;	/* ensure connection trace is dumped */
1359 
1360 		/* No DREP private data possible */
1361 		if (statep->close_ret_priv_data_len != NULL)
1362 			*statep->close_ret_priv_data_len = 0;
1363 
1364 		/*
1365 		 * If waiting for a response mad, then cancel the timer,
1366 		 * and delete the connection
1367 		 */
1368 		if (statep->state == IBCM_STATE_REQ_SENT ||
1369 		    statep->state == IBCM_STATE_REP_SENT ||
1370 		    statep->state == IBCM_STATE_REP_WAIT ||
1371 		    statep->state == IBCM_STATE_MRA_REP_RCVD) {
1372 			timeout_id_t		timer_val = statep->timerid;
1373 			ibcm_conn_state_t	old_state;
1374 
1375 			IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1376 			    "chan 0x%p connection aborted in state %x", channel,
1377 			    statep->state);
1378 
1379 			old_state = statep->state;
1380 			statep->state = IBCM_STATE_DELETE;
1381 
1382 			if (mode == IBT_NONBLOCKING) {
1383 				if (taskq_dispatch(ibcm_taskq,
1384 				    ibcm_process_abort_via_taskq, statep,
1385 				    TQ_NOSLEEP) == 0) {
1386 
1387 					IBCM_REF_CNT_DECR(statep);
1388 					statep->state = old_state;
1389 					mutex_exit(&statep->state_mutex);
1390 					return (IBT_INSUFF_KERNEL_RESOURCE);
1391 				}	/* if taskq_dispatch succeeds */
1392 				/* Cancel the timer */
1393 				statep->timerid = 0;
1394 				mutex_exit(&statep->state_mutex);
1395 			} else {
1396 				/* Cancel the timer */
1397 				statep->timerid = 0;
1398 				mutex_exit(&statep->state_mutex);
1399 				(void) taskq_dispatch(ibcm_taskq,
1400 				    ibcm_process_abort_via_taskq, statep,
1401 				    TQ_SLEEP);
1402 			}
1403 
1404 			/* cancel the currently running timer */
1405 			if (timer_val != 0)
1406 				(void) untimeout(timer_val);
1407 
1408 			/* wait until cm handler returns for BLOCKING cases */
1409 			mutex_enter(&statep->state_mutex);
1410 			if ((mode == IBT_BLOCKING) ||
1411 			    (mode == IBT_NOCALLBACKS)) {
1412 				while (statep->close_done != B_TRUE)
1413 					cv_wait(&statep->block_client_cv,
1414 					    &statep->state_mutex);
1415 			}
1416 
1417 			if (statep->close_ret_status)
1418 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1419 			mutex_exit(&statep->state_mutex);
1420 
1421 			/*
1422 			 * It would ideal to post a REJ MAD, but that would
1423 			 * be non-conformance to spec. Hence, delete the state
1424 			 * data. Assuming that happens quickly, any retransmits
1425 			 * from the remote are replied by CM with reject
1426 			 * reason " no valid com id". That would stop remote
1427 			 * sending any more MADs.
1428 			 */
1429 			ibcm_delete_state_data(statep);
1430 			return (IBT_SUCCESS);
1431 
1432 		/* if CM busy in cm handler, wait until cm handler returns */
1433 		} else if (statep->state == IBCM_STATE_REQ_RCVD ||
1434 		    statep->state == IBCM_STATE_REP_RCVD ||
1435 		    statep->state == IBCM_STATE_MRA_SENT ||
1436 		    statep->state == IBCM_STATE_MRA_REP_SENT) {
1437 
1438 			/* take control of statep */
1439 			statep->abort_flag |= IBCM_ABORT_CLIENT;
1440 
1441 			IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1442 			    "chan 0x%p connection aborted in state = %x",
1443 			    channel, statep->state);
1444 
1445 			/*
1446 			 * wait until state machine modifies qp state to error,
1447 			 * including disassociating statep and QP
1448 			 */
1449 			if ((mode == IBT_BLOCKING) || (mode == IBT_NOCALLBACKS))
1450 				while (statep->close_done != B_TRUE)
1451 					cv_wait(&statep->block_client_cv,
1452 					    &statep->state_mutex);
1453 
1454 			/* a sanity setting */
1455 			if (mode == IBT_NOCALLBACKS)
1456 				statep->cm_handler = NULL;
1457 			IBCM_REF_CNT_DECR(statep);
1458 
1459 			/*
1460 			 * In rare situations, connection attempt could be
1461 			 * terminated for some other reason, before abort is
1462 			 * processed, but CM still returns ret_status as abort
1463 			 */
1464 			if (statep->close_ret_status)
1465 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1466 			mutex_exit(&statep->state_mutex);
1467 
1468 			/*
1469 			 * REJ MAD is posted by the CM state machine for this
1470 			 * case, hence state structure is deleted in the
1471 			 * state machine processing.
1472 			 */
1473 			return (IBT_SUCCESS);
1474 
1475 		} else if ((statep->state == IBCM_STATE_TIMEWAIT) ||
1476 		    (statep->state == IBCM_STATE_DELETE)) {
1477 
1478 			/* State already in timewait, so no return priv data */
1479 			IBCM_REF_CNT_DECR(statep);
1480 
1481 			/* The teardown has already been done */
1482 			if (statep->close_ret_status)
1483 				*statep->close_ret_status =
1484 				    IBT_CM_CLOSED_ALREADY;
1485 			mutex_exit(&statep->state_mutex);
1486 
1487 			return (IBT_SUCCESS);
1488 
1489 		} else if ((statep->state == IBCM_STATE_DREQ_RCVD) ||
1490 		    (statep->state == IBCM_STATE_DREQ_SENT) ||
1491 		    (statep->state == IBCM_STATE_DREP_RCVD) ||
1492 		    ((statep->state == IBCM_STATE_TIMED_OUT) &&
1493 		    (statep->timedout_state == IBCM_STATE_DREQ_SENT))) {
1494 
1495 			/*
1496 			 * Either the remote or local client has already
1497 			 * initiated the teardown.  IBCM_STATE_DREP_RCVD is
1498 			 * possible, if CM initiated teardown without client's
1499 			 * knowledge, for stale handling, etc.,
1500 			 */
1501 			if (mode == IBT_NOCALLBACKS) {
1502 				if (statep->close_nocb_state == IBCM_UNBLOCK) {
1503 					statep->close_nocb_state = IBCM_FAIL;
1504 					/* enable free qp after return */
1505 					ibtl_cm_chan_is_closing(
1506 					    statep->channel);
1507 				} else while (statep->close_nocb_state ==
1508 				    IBCM_BLOCK)
1509 					cv_wait(&statep->block_client_cv,
1510 					    &statep->state_mutex);
1511 				statep->cm_handler = NULL; /* sanity setting */
1512 				if (statep->close_ret_status)
1513 					*statep->close_ret_status =
1514 					    IBT_CM_CLOSED_ALREADY;
1515 			} else if (mode == IBT_BLOCKING) {
1516 				/* wait until state is moved to timewait */
1517 				while (statep->close_done != B_TRUE)
1518 					cv_wait(&statep->block_client_cv,
1519 					    &statep->state_mutex);
1520 			}
1521 
1522 			IBCM_REF_CNT_DECR(statep);
1523 			mutex_exit(&statep->state_mutex);
1524 
1525 			/* ret_status is set in state machine code */
1526 			return (IBT_SUCCESS);
1527 
1528 		} else if (statep->state == IBCM_STATE_TIMED_OUT) {
1529 
1530 			if ((mode == IBT_BLOCKING) ||
1531 			    (mode == IBT_NOCALLBACKS)) {
1532 
1533 				/*
1534 				 * wait until cm handler invocation and
1535 				 * disassociation between statep and channel
1536 				 * is complete
1537 				 */
1538 				while (statep->close_done != B_TRUE)
1539 					cv_wait(&statep->block_client_cv,
1540 					    &statep->state_mutex);
1541 			}
1542 
1543 			if (statep->close_ret_status)
1544 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1545 			IBCM_REF_CNT_DECR(statep);
1546 			mutex_exit(&statep->state_mutex);
1547 
1548 			return (IBT_SUCCESS);
1549 		} else {
1550 			IBCM_REF_CNT_DECR(statep);
1551 			mutex_exit(&statep->state_mutex);
1552 
1553 			return (IBT_CM_FAILURE);
1554 		}
1555 	}
1556 
1557 	ASSERT(statep->close_nocb_state != IBCM_BLOCK);
1558 
1559 	if (mode == IBT_NOCALLBACKS) {
1560 		statep->close_nocb_state = IBCM_FAIL;
1561 		statep->cm_handler = NULL;
1562 		ibtl_cm_chan_is_closing(statep->channel);
1563 		IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1564 		    "NOCALLBACKS on in statep = %p", statep);
1565 	}
1566 
1567 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1568 		goto lost_race;
1569 	}
1570 
1571 	/*
1572 	 * Cancel/wait for any pending ibt_set_alt_path, and
1573 	 * release state mutex
1574 	 */
1575 	ibcm_sync_lapr_idle(statep);
1576 
1577 	ibcm_close_enter();
1578 
1579 	mutex_enter(&statep->state_mutex);
1580 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1581 		ibcm_close_exit();
1582 		goto lost_race;
1583 	}
1584 
1585 	statep->state = IBCM_STATE_TRANSIENT_DREQ_SENT;
1586 	statep->timerid = 0;
1587 	statep->close_done = B_FALSE;
1588 	statep->close_flow = 1;
1589 	mutex_exit(&statep->state_mutex);
1590 
1591 	ibcm_post_dreq_mad(statep);
1592 
1593 	mutex_enter(&statep->state_mutex);
1594 
1595 lost_race:
1596 	if (mode == IBT_BLOCKING) {
1597 
1598 		/* wait for DREP */
1599 		while (statep->close_done != B_TRUE)
1600 			cv_wait(&statep->block_client_cv,
1601 			    &statep->state_mutex);
1602 
1603 		IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1604 		    "done blocking", channel);
1605 	}
1606 
1607 	IBCM_REF_CNT_DECR(statep);
1608 	mutex_exit(&statep->state_mutex);
1609 
1610 	/* If this message isn't seen then ibt_close_rc_channel failed */
1611 	IBTF_DPRINTF_L5(cmlog, "ibcm_close_rc_channel: chan 0x%p done",
1612 	    channel);
1613 
1614 	return (IBT_SUCCESS);
1615 }
1616 
1617 ibt_status_t
1618 ibt_recycle_rc(ibt_channel_hdl_t rc_chan, ibt_cep_flags_t control,
1619     uint8_t hca_port_num, ibt_recycle_handler_t func, void *arg)
1620 {
1621 	ibcm_state_data_t		*statep;
1622 	ibcm_taskq_recycle_arg_t	*ibcm_tq_recycle_arg;
1623 	ibt_qp_query_attr_t		qp_attr;
1624 	ibt_status_t			retval;
1625 
1626 	IBTF_DPRINTF_L3(cmlog, "ibt_recycle_rc (%p, 0x%X, %d, %p, %p)", rc_chan,
1627 	    control, hca_port_num, func, arg);
1628 
1629 	if (IBCM_INVALID_CHANNEL(rc_chan)) {
1630 		IBTF_DPRINTF_L2(cmlog, "ibt_recycle_rc: invalid channel");
1631 		return (IBT_CHAN_HDL_INVALID);
1632 	}
1633 
1634 	/* check qp state */
1635 	retval = ibt_query_qp(rc_chan, &qp_attr);
1636 
1637 	if (retval != IBT_SUCCESS)
1638 		return (retval);
1639 
1640 	if (qp_attr.qp_info.qp_trans != IBT_RC_SRV)
1641 		return (IBT_CHAN_SRV_TYPE_INVALID);
1642 
1643 	if (qp_attr.qp_info.qp_state != IBT_STATE_ERROR)
1644 		return (IBT_CHAN_STATE_INVALID);
1645 
1646 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*ibcm_tq_recycle_arg))
1647 
1648 	ibcm_tq_recycle_arg = kmem_alloc(sizeof (ibcm_taskq_recycle_arg_t),
1649 	    KM_SLEEP);
1650 
1651 	ibcm_tq_recycle_arg->rc_chan		= rc_chan;
1652 	ibcm_tq_recycle_arg->control		= control;
1653 	ibcm_tq_recycle_arg->hca_port_num	= hca_port_num;
1654 	ibcm_tq_recycle_arg->func		= func;
1655 	ibcm_tq_recycle_arg->arg		= arg;
1656 
1657 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*ibcm_tq_recycle_arg))
1658 
1659 	IBCM_GET_CHAN_PRIVATE(rc_chan, statep);
1660 
1661 	/*
1662 	 * If non-blocking ie., func specified and channel has not yet completed
1663 	 * the timewait, then schedule the work for later
1664 	 */
1665 	if ((func != NULL) && (statep != NULL)) {
1666 		IBCM_RELEASE_CHAN_PRIVATE(rc_chan);
1667 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(statep->recycle_arg))
1668 		statep->recycle_arg = ibcm_tq_recycle_arg;
1669 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(statep->recycle_arg))
1670 		return (IBT_SUCCESS);
1671 	}
1672 
1673 	/*
1674 	 * if blocking ie., func specified, and channel has not yet completed
1675 	 * the timewait, then block until the channel completes the timewait
1676 	 */
1677 	if (statep != NULL)
1678 		IBCM_RELEASE_CHAN_PRIVATE(rc_chan);
1679 	IBCM_WAIT_CHAN_PRIVATE(rc_chan);
1680 
1681 	if (func) {	/* NON BLOCKING case. Taskq for QP state change */
1682 		(void) taskq_dispatch(ibcm_taskq, ibcm_process_rc_recycle,
1683 		    ibcm_tq_recycle_arg, TQ_SLEEP);
1684 		return (IBT_SUCCESS);
1685 	} else	/* BLOCKING case */
1686 		return (ibcm_process_rc_recycle_ret(ibcm_tq_recycle_arg));
1687 }
1688 
1689 void
1690 ibcm_process_rc_recycle(void *recycle_arg)
1691 {
1692 	(void) ibcm_process_rc_recycle_ret(recycle_arg);
1693 }
1694 
1695 static ibt_status_t
1696 ibcm_process_rc_recycle_ret(void *recycle_arg)
1697 {
1698 	ibt_qp_info_t			qp_info;
1699 	ibt_status_t			ibt_status = IBT_SUCCESS;
1700 	ibt_cep_modify_flags_t		cep_flags;
1701 	ibt_qp_query_attr_t		qp_attr;
1702 	ibcm_taskq_recycle_arg_t	*ibcm_tq_recycle_arg =
1703 	    (ibcm_taskq_recycle_arg_t *)recycle_arg;
1704 
1705 	/* QP must have been in error state */
1706 	ibt_status = ibt_query_qp(ibcm_tq_recycle_arg->rc_chan, &qp_attr);
1707 	if (ibt_status != IBT_SUCCESS)
1708 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1709 		    "chanp %p ibt_query_qp() = %d",
1710 		    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1711 	else {
1712 		/* perform the QP state change from ERROR to RESET */
1713 		bzero(&qp_info, sizeof (qp_info));
1714 
1715 		qp_info.qp_trans = IBT_RC_SRV;
1716 		qp_info.qp_state = IBT_STATE_RESET;
1717 
1718 		/* Call modify_qp to move to RESET state */
1719 		ibt_status = ibt_modify_qp(ibcm_tq_recycle_arg->rc_chan,
1720 		    IBT_CEP_SET_STATE, &qp_info, NULL);
1721 
1722 		if (ibt_status != IBT_SUCCESS)
1723 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1724 			    "chanp %p ibt_modify_qp() = %d for ERROR to RESET",
1725 			    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1726 	}
1727 
1728 	if (ibt_status == IBT_SUCCESS) {
1729 
1730 		qp_info.qp_state = IBT_STATE_INIT;
1731 
1732 		/* set flags for all mandatory args from RESET to INIT */
1733 		cep_flags = IBT_CEP_SET_STATE | IBT_CEP_SET_PORT;
1734 		cep_flags |= IBT_CEP_SET_RDMA_R | IBT_CEP_SET_RDMA_W;
1735 		cep_flags |= IBT_CEP_SET_ATOMIC;
1736 
1737 		qp_info.qp_transport.rc.rc_path.cep_hca_port_num =
1738 		    ibcm_tq_recycle_arg->hca_port_num;
1739 		qp_info.qp_flags |=
1740 		    ibcm_tq_recycle_arg->control & IBT_CEP_RDMA_RD;
1741 		qp_info.qp_flags |=
1742 		    ibcm_tq_recycle_arg->control & IBT_CEP_RDMA_WR;
1743 		qp_info.qp_flags |=
1744 		    ibcm_tq_recycle_arg->control & IBT_CEP_ATOMIC;
1745 
1746 		/* Always use the existing pkey */
1747 		qp_info.qp_transport.rc.rc_path.cep_pkey_ix =
1748 		    qp_attr. qp_info.qp_transport.rc.rc_path.cep_pkey_ix;
1749 
1750 		/* Call modify_qp to move to INIT state */
1751 		ibt_status = ibt_modify_qp(ibcm_tq_recycle_arg->rc_chan,
1752 		    cep_flags, &qp_info, NULL);
1753 
1754 		if (ibt_status != IBT_SUCCESS)
1755 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1756 			    "chanp %p ibt_modify_qp() = %d for RESET to INIT",
1757 			    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1758 	}
1759 
1760 	/* Change the QP CM state to indicate QP being re-used */
1761 	if (ibt_status == IBT_SUCCESS)
1762 		ibtl_cm_chan_is_reused(ibcm_tq_recycle_arg->rc_chan);
1763 
1764 	/* Call func, if defined */
1765 	if (ibcm_tq_recycle_arg->func)
1766 		(*(ibcm_tq_recycle_arg->func))(ibt_status,
1767 		    ibcm_tq_recycle_arg->arg);
1768 
1769 	kmem_free(ibcm_tq_recycle_arg, sizeof (ibcm_taskq_recycle_arg_t));
1770 
1771 	return (ibt_status);
1772 }
1773 
1774 static void
1775 ibcm_process_abort_via_taskq(void *args)
1776 {
1777 	ibcm_state_data_t	*statep = (ibcm_state_data_t *)args;
1778 
1779 	ibcm_process_abort(statep);
1780 	mutex_enter(&statep->state_mutex);
1781 	IBCM_REF_CNT_DECR(statep);
1782 	mutex_exit(&statep->state_mutex);
1783 }
1784 
1785 /*
1786  * Local UD CM Handler's private data, used during ibt_request_ud_dest() in
1787  * Non-Blocking mode operations.
1788  */
1789 typedef struct ibcm_local_handler_s {
1790 	ibt_cm_ud_handler_t	actual_cm_handler;
1791 	void			*actual_cm_private;
1792 	ibt_ud_dest_t		*dest_hdl;
1793 } ibcm_local_handler_t;
1794 
1795 _NOTE(READ_ONLY_DATA(ibcm_local_handler_s))
1796 
1797 /*
1798  * Local UD CM Handler, used when ibt_alloc_ud_dest() is issued in
1799  * NON-Blocking mode.
1800  *
1801  * Out here, we update the UD Destination handle with
1802  * the obtained DQPN and QKey (from SIDR REP) and invokes actual client
1803  * handler that was specified by the client.
1804  */
1805 static ibt_cm_status_t
1806 ibcm_local_cm_handler(void *priv, ibt_cm_ud_event_t *event,
1807     ibt_cm_ud_return_args_t *ret_args, void *priv_data, ibt_priv_data_len_t len)
1808 {
1809 	ibcm_local_handler_t	*handler_priv = (ibcm_local_handler_t *)priv;
1810 
1811 	IBTF_DPRINTF_L4(cmlog, "ibcm_local_cm_handler: event %d",
1812 	    event->cm_type);
1813 
1814 	ASSERT(handler_priv != NULL);
1815 
1816 	switch (event->cm_type) {
1817 	case IBT_CM_UD_EVENT_SIDR_REP:
1818 		/* Update QPN & QKey from event into destination handle. */
1819 		if (handler_priv->dest_hdl != NULL) {
1820 			handler_priv->dest_hdl->ud_dst_qpn =
1821 			    event->cm_event.sidr_rep.srep_remote_qpn;
1822 			handler_priv->dest_hdl->ud_qkey =
1823 			    event->cm_event.sidr_rep.srep_remote_qkey;
1824 		}
1825 
1826 		/* Invoke the client handler - inform only, so ignore retval */
1827 		(void) handler_priv->actual_cm_handler(
1828 		    handler_priv->actual_cm_private, event, ret_args, priv_data,
1829 		    len);
1830 
1831 		/* Free memory allocated for local handler's private data. */
1832 		if (handler_priv != NULL)
1833 			kmem_free(handler_priv, sizeof (*handler_priv));
1834 
1835 		break;
1836 	default:
1837 		IBTF_DPRINTF_L2(cmlog, "ibcm_local_cm_handler: ERROR");
1838 		break;
1839 	}
1840 
1841 	return (IBT_CM_ACCEPT);
1842 }
1843 
1844 
1845 /* Validate the input UD destination attributes.  */
1846 static ibt_status_t
1847 ibcm_validate_dqpn_data(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
1848     ibt_ud_returns_t *ret_args)
1849 {
1850 	/* cm handler must always be specified */
1851 	if (mode == IBT_NONBLOCKING && attr->ud_cm_handler == NULL) {
1852 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1853 		    "CM handler is not specified ");
1854 		return (IBT_INVALID_PARAM);
1855 	}
1856 
1857 	if (mode == IBT_NONBLOCKING) {
1858 		if (ret_args != NULL) {
1859 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1860 			    "ret_args should be NULL when called in "
1861 			    "non-blocking mode");
1862 			return (IBT_INVALID_PARAM);
1863 		}
1864 	} else if (mode == IBT_BLOCKING) {
1865 		if (ret_args == NULL) {
1866 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1867 			    "ret_args should be Non-NULL when called in "
1868 			    "blocking mode");
1869 			return (IBT_INVALID_PARAM);
1870 		}
1871 	} else {
1872 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1873 		    "invalid mode %x specified ", mode);
1874 		return (IBT_INVALID_PARAM);
1875 	}
1876 
1877 	if (attr->ud_sid == 0) {
1878 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1879 		    "ServiceID must be specified. ");
1880 		return (IBT_INVALID_PARAM);
1881 	}
1882 
1883 	if (attr->ud_addr == NULL) {
1884 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1885 		    "Address Info NULL");
1886 		return (IBT_INVALID_PARAM);
1887 	}
1888 
1889 	/* Validate SGID */
1890 	if ((attr->ud_addr->av_sgid.gid_prefix == 0) ||
1891 	    (attr->ud_addr->av_sgid.gid_guid == 0)) {
1892 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: Invalid SGID");
1893 		return (IBT_INVALID_PARAM);
1894 	}
1895 	IBTF_DPRINTF_L3(cmlog, "ibcm_validate_dqpn_data: SGID<%llX:%llX>",
1896 	    attr->ud_addr->av_sgid.gid_prefix,
1897 	    attr->ud_addr->av_sgid.gid_guid);
1898 
1899 	/* Validate DGID */
1900 	if ((attr->ud_addr->av_dgid.gid_prefix == 0) ||
1901 	    (attr->ud_addr->av_dgid.gid_guid == 0)) {
1902 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: Invalid DGID");
1903 		return (IBT_INVALID_PARAM);
1904 	}
1905 	IBTF_DPRINTF_L3(cmlog, "ibcm_validate_dqpn_data: DGID<%llX:%llX>",
1906 	    attr->ud_addr->av_dgid.gid_prefix,
1907 	    attr->ud_addr->av_dgid.gid_guid);
1908 
1909 	return (IBT_SUCCESS);
1910 }
1911 
1912 
1913 /* Perform SIDR to retrieve DQPN and QKey.  */
1914 static ibt_status_t
1915 ibcm_ud_get_dqpn(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
1916     ibt_ud_returns_t *ret_args)
1917 {
1918 	ibt_status_t		retval;
1919 	ib_pkey_t		ud_pkey;
1920 	ibmf_handle_t		ibmf_hdl;
1921 	ibmf_msg_t		*ibmf_msg;
1922 	ibcm_hca_info_t		*hcap;
1923 	ibcm_sidr_req_msg_t	*sidr_req_msgp;
1924 	ibcm_ud_state_data_t	*ud_statep;
1925 	ibtl_cm_hca_port_t	port;
1926 	ibcm_sidr_srch_t	sidr_entry;
1927 	ibcm_qp_list_t		*cm_qp_entry;
1928 
1929 	/* Retrieve HCA GUID value from the available SGID info. */
1930 	retval = ibtl_cm_get_hca_port(attr->ud_addr->av_sgid, 0, &port);
1931 	if ((retval != IBT_SUCCESS) || (port.hp_port == 0)) {
1932 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1933 		    "ibtl_cm_get_hca_port failed: %d", retval);
1934 		return (retval);
1935 	}
1936 
1937 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: "
1938 	    "HCA GUID:%llX, port_num:%d", port.hp_hca_guid, port.hp_port);
1939 
1940 	/* Lookup the HCA info for this GUID */
1941 	if ((hcap = ibcm_find_hca_entry(port.hp_hca_guid)) == NULL) {
1942 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: hcap is NULL");
1943 		return (IBT_HCA_INVALID);
1944 	}
1945 
1946 	/* Return failure if the HCA device or Port is not operational */
1947 
1948 	if ((retval = ibt_get_port_state_byguid(port.hp_hca_guid, port.hp_port,
1949 	    NULL, NULL)) != IBT_SUCCESS) {
1950 		/* Device Port is not in good state, don't use it. */
1951 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: Invalid "
1952 		    "port specified or port not active");
1953 		ibcm_dec_hca_acc_cnt(hcap);
1954 		return (retval);
1955 	}
1956 
1957 	retval = ibt_index2pkey_byguid(port.hp_hca_guid, port.hp_port,
1958 	    attr->ud_pkey_ix, &ud_pkey);
1959 	if (retval != IBT_SUCCESS) {
1960 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1961 		    "Failed to convert index2pkey: %d", retval);
1962 		ibcm_dec_hca_acc_cnt(hcap);
1963 		return (retval);
1964 	}
1965 
1966 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(sidr_entry))
1967 
1968 	/* Allocate a new request id */
1969 	if (ibcm_alloc_reqid(hcap, &sidr_entry.srch_req_id) == IBCM_FAILURE) {
1970 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1971 		    "no req id available");
1972 		ibcm_dec_hca_acc_cnt(hcap);
1973 		return (IBT_INSUFF_KERNEL_RESOURCE);
1974 	}
1975 
1976 	if ((hcap->hca_port_info[port.hp_port - 1].port_ibmf_hdl == NULL) &&
1977 	    ((retval = ibcm_hca_reinit_port(hcap, port.hp_port - 1))
1978 	    != IBT_SUCCESS)) {
1979 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1980 		    "ibmf reg or callback setup failed during re-initialize");
1981 		return (retval);
1982 	}
1983 
1984 	ibmf_hdl = hcap->hca_port_info[port.hp_port - 1].port_ibmf_hdl;
1985 
1986 	/* find the ibmf QP to post the SIDR REQ */
1987 	if ((cm_qp_entry = ibcm_find_qp(hcap, port.hp_port, ud_pkey)) ==
1988 	    NULL) {
1989 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: IBMF QP allocation"
1990 		    " failed");
1991 		ibcm_dec_hca_acc_cnt(hcap);
1992 		return (IBT_INSUFF_RESOURCE);
1993 	}
1994 
1995 	if ((retval = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg, MAD_METHOD_SEND))
1996 	    != IBT_SUCCESS) {
1997 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: IBMF MSG allocation"
1998 		    " failed");
1999 		ibcm_release_qp(cm_qp_entry);
2000 		ibcm_dec_hca_acc_cnt(hcap);
2001 		return (retval);
2002 	}
2003 
2004 	sidr_entry.srch_lid = port.hp_base_lid;
2005 	sidr_entry.srch_gid = attr->ud_addr->av_sgid;
2006 	sidr_entry.srch_grh_exists = attr->ud_addr->av_send_grh;
2007 	sidr_entry.srch_mode = IBCM_ACTIVE_MODE;
2008 
2009 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(sidr_entry))
2010 
2011 	/* do various allocations needed here */
2012 	rw_enter(&hcap->hca_sidr_list_lock, RW_WRITER);
2013 
2014 	(void) ibcm_find_sidr_entry(&sidr_entry, hcap, &ud_statep,
2015 	    IBCM_FLAG_ADD);
2016 	rw_exit(&hcap->hca_sidr_list_lock);
2017 
2018 	/* Increment hca's resource count */
2019 	ibcm_inc_hca_res_cnt(hcap);
2020 
2021 	/* After a resource created on hca, no need to hold the acc cnt */
2022 	ibcm_dec_hca_acc_cnt(hcap);
2023 
2024 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*ud_statep))
2025 
2026 	/* Initialize some ud_statep fields */
2027 	ud_statep->ud_stored_msg = ibmf_msg;
2028 	ud_statep->ud_svc_id = attr->ud_sid;
2029 	ud_statep->ud_pkt_life_time =
2030 	    ibt_ib2usec(attr->ud_pkt_lt);
2031 	ud_statep->ud_stored_reply_addr.cm_qp_entry = cm_qp_entry;
2032 
2033 	/* set remaining retry cnt */
2034 	ud_statep->ud_remaining_retry_cnt = ud_statep->ud_max_cm_retries;
2035 
2036 	/*
2037 	 * Get UD handler and corresponding args which is pass it back
2038 	 * as first argument for the handler.
2039 	 */
2040 	ud_statep->ud_state_cm_private = attr->ud_cm_private;
2041 
2042 	if (mode == IBT_BLOCKING)
2043 		ud_statep->ud_return_data = ret_args;
2044 	else
2045 		ud_statep->ud_cm_handler = attr->ud_cm_handler;
2046 
2047 	/* Initialize the fields of ud_statep->ud_stored_reply_addr */
2048 	ud_statep->ud_stored_reply_addr.grh_exists = attr->ud_addr->av_send_grh;
2049 	ud_statep->ud_stored_reply_addr.ibmf_hdl = ibmf_hdl;
2050 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_hop_limit =
2051 	    attr->ud_addr->av_hop;
2052 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_sender_gid =
2053 	    attr->ud_addr->av_sgid;
2054 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_recver_gid =
2055 	    attr->ud_addr->av_dgid;
2056 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_tclass =
2057 	    attr->ud_addr->av_tclass;
2058 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_flow_label =
2059 	    attr->ud_addr->av_flow & IB_GRH_FLOW_LABEL_MASK;
2060 
2061 	/* needs to be derived based on the base LID and path bits */
2062 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_local_lid =
2063 	    port.hp_base_lid;
2064 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_remote_lid =
2065 	    attr->ud_addr->av_dlid;
2066 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_p_key = ud_pkey;
2067 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_q_key = IB_GSI_QKEY;
2068 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_service_level =
2069 	    attr->ud_addr->av_srvl;
2070 
2071 	/*
2072 	 * This may be enchanced later, to use a remote qno based on past
2073 	 * redirect rej mad responses. This would be the place to specify
2074 	 * appropriate remote qno
2075 	 */
2076 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_remote_qno = 1;
2077 
2078 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sidr_req_msgp))
2079 
2080 	/* Initialize the SIDR REQ message fields */
2081 	sidr_req_msgp =
2082 	    (ibcm_sidr_req_msg_t *)IBCM_OUT_MSGP(ud_statep->ud_stored_msg);
2083 
2084 	sidr_req_msgp->sidr_req_request_id = h2b32(ud_statep->ud_req_id);
2085 	sidr_req_msgp->sidr_req_service_id = h2b64(attr->ud_sid);
2086 	sidr_req_msgp->sidr_req_pkey = h2b16(ud_pkey);
2087 	IBCM_OUT_HDRP(ud_statep->ud_stored_msg)->AttributeID =
2088 	    h2b16(IBCM_INCOMING_SIDR_REQ + IBCM_ATTR_BASE_ID);
2089 
2090 	if ((attr->ud_priv_data != NULL) && (attr->ud_priv_data_len > 0)) {
2091 		bcopy(attr->ud_priv_data, sidr_req_msgp->sidr_req_private_data,
2092 		    min(attr->ud_priv_data_len, IBT_SIDR_REQ_PRIV_DATA_SZ));
2093 	}
2094 
2095 	/* Send out the SIDR REQ message */
2096 	ud_statep->ud_state = IBCM_STATE_SIDR_REQ_SENT;
2097 	ud_statep->ud_timer_stored_state = IBCM_STATE_SIDR_REQ_SENT;
2098 	IBCM_UD_REF_CNT_INCR(ud_statep); /* for non-blocking SIDR REQ post */
2099 	ud_statep->ud_timer_value = ibt_ib2usec(ibcm_max_sidr_rep_proctime) +
2100 	    (ud_statep->ud_pkt_life_time * 2);
2101 
2102 	IBCM_OUT_HDRP(ud_statep->ud_stored_msg)->TransactionID =
2103 	    h2b64(ibcm_generate_tranid(IBCM_INCOMING_SIDR_REQ,
2104 	    ud_statep->ud_req_id, 0));
2105 
2106 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: timer_value in HZ = %x",
2107 	    ud_statep->ud_timer_value);
2108 
2109 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*ud_statep))
2110 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sidr_req_msgp))
2111 
2112 	ibcm_post_ud_mad(ud_statep, ud_statep->ud_stored_msg,
2113 	    ibcm_post_sidr_req_complete, ud_statep);
2114 
2115 	mutex_enter(&ud_statep->ud_state_mutex);
2116 
2117 	/* Wait for SIDR_REP */
2118 	if (mode == IBT_BLOCKING) {
2119 		IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: blocking");
2120 
2121 		while (ud_statep->ud_blocking_done != B_TRUE) {
2122 			cv_wait(&ud_statep->ud_block_client_cv,
2123 			    &ud_statep->ud_state_mutex);
2124 		}
2125 
2126 		IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: finished blocking");
2127 
2128 		if (ret_args->ud_status == IBT_CM_SREP_QPN_VALID) {
2129 			IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: DQPN = %x, "
2130 			    "status = %x, QKey = %x", ret_args->ud_dqpn,
2131 			    ret_args->ud_status, ret_args->ud_qkey);
2132 
2133 		} else {
2134 			IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: Status<%x>",
2135 			    ret_args->ud_status);
2136 			retval = IBT_CM_FAILURE;
2137 		}
2138 	}
2139 
2140 	IBCM_UD_REF_CNT_DECR(ud_statep);
2141 	mutex_exit(&ud_statep->ud_state_mutex);
2142 
2143 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: done");
2144 
2145 	return (retval);
2146 }
2147 
2148 
2149 /*
2150  * Function:
2151  *	ibt_request_ud_dest
2152  * Input:
2153  *	ud_dest		A previously allocated UD destination handle.
2154  *	mode		This function can execute in blocking or non blocking
2155  *			modes.
2156  *	attr		UD destination attributes to be modified.
2157  * Output:
2158  *	ud_ret_args	If the function is called in blocking mode, ud_ret_args
2159  *			should be a pointer to an ibt_ud_returns_t struct.
2160  * Returns:
2161  *	IBT_SUCCESS
2162  * Description:
2163  *	Modify a previously allocated UD destination handle based on the
2164  *	results of doing the SIDR protocol.
2165  */
2166 ibt_status_t
2167 ibt_request_ud_dest(ibt_ud_dest_hdl_t ud_dest, ibt_execution_mode_t mode,
2168     ibt_ud_dest_attr_t *attr, ibt_ud_returns_t *ud_ret_args)
2169 {
2170 	ibt_status_t		retval;
2171 	ibt_ud_dest_t		*ud_destp;
2172 	ibcm_local_handler_t	*local_handler_priv = NULL;
2173 
2174 	IBTF_DPRINTF_L3(cmlog, "ibt_request_ud_dest(%p, %x, %p, %p)",
2175 	    ud_dest, mode, attr, ud_ret_args);
2176 
2177 	retval = ibcm_validate_dqpn_data(attr, mode, ud_ret_args);
2178 	if (retval != IBT_SUCCESS) {
2179 		return (retval);
2180 	}
2181 
2182 	ud_destp = ud_dest;
2183 
2184 	/* Allocate an Address handle. */
2185 	retval = ibt_modify_ah(ud_destp->ud_dest_hca, ud_destp->ud_ah,
2186 	    attr->ud_addr);
2187 	if (retval != IBT_SUCCESS) {
2188 		IBTF_DPRINTF_L2(cmlog, "ibt_request_ud_dest: "
2189 		    "Address Handle Modification failed: %d", retval);
2190 		return (retval);
2191 	}
2192 
2193 	if (mode == IBT_NONBLOCKING) {
2194 		/*
2195 		 * In NON-BLOCKING mode, and we need to update the destination
2196 		 * handle with the DQPN and QKey that are obtained from
2197 		 * SIDR REP, hook-up our own handler, so that we can catch
2198 		 * the event, and we ourselves call the actual client's
2199 		 * ud_cm_handler, in our handler.
2200 		 */
2201 
2202 		/* Allocate memory for local handler's private data. */
2203 		local_handler_priv =
2204 		    kmem_alloc(sizeof (*local_handler_priv), KM_SLEEP);
2205 
2206 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*local_handler_priv))
2207 
2208 		local_handler_priv->actual_cm_handler = attr->ud_cm_handler;
2209 		local_handler_priv->actual_cm_private = attr->ud_cm_private;
2210 		local_handler_priv->dest_hdl = ud_destp;
2211 
2212 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*local_handler_priv))
2213 
2214 		attr->ud_cm_handler = ibcm_local_cm_handler;
2215 		attr->ud_cm_private = local_handler_priv;
2216 	}
2217 
2218 	/* In order to get DQPN and Destination QKey, perform SIDR */
2219 	retval = ibcm_ud_get_dqpn(attr, mode, ud_ret_args);
2220 	if (retval != IBT_SUCCESS) {
2221 		IBTF_DPRINTF_L2(cmlog, "ibt_request_ud_dest: "
2222 		    "Failed to get DQPN: %d", retval);
2223 
2224 		/* Free memory allocated for local handler's private data. */
2225 		if (local_handler_priv != NULL)
2226 			kmem_free(local_handler_priv,
2227 			    sizeof (*local_handler_priv));
2228 		return (retval);
2229 	}
2230 
2231 	/*
2232 	 * Fill in the dqpn and dqkey as obtained from ud_ret_args,
2233 	 * values will be valid only on BLOCKING mode.
2234 	 */
2235 	if (mode == IBT_BLOCKING) {
2236 		ud_destp->ud_dst_qpn = ud_ret_args->ud_dqpn;
2237 		ud_destp->ud_qkey = ud_ret_args->ud_qkey;
2238 	}
2239 
2240 	return (retval);
2241 }
2242 
2243 /*
2244  * Function:
2245  *	ibt_ud_get_dqpn
2246  * Input:
2247  *	attr		A pointer to an ibt_ud_dest_attr_t struct that are
2248  *			required for SIDR REQ message. Not specified attributes
2249  *			should be set to "NULL" or "0".
2250  *			ud_sid, ud_addr and ud_pkt_lt must be specified.
2251  *	mode		This function can execute in blocking or non blocking
2252  *			modes.
2253  * Output:
2254  *	returns		If the function is called in blocking mode, returns
2255  *			should be a pointer to an ibt_ud_returns_t struct.
2256  * Return:
2257  *	IBT_SUCCESS	on success or respective failure on error.
2258  * Description:
2259  *	Finds the destination QPN at the specified destination that the
2260  *	specified service can be reached on. The IBTF CM initiates the
2261  *	service ID resolution protocol (SIDR) to determine a destination QPN.
2262  *
2263  * NOTE: SIDR_REQ is initiated from active side.
2264  */
2265 ibt_status_t
2266 ibt_ud_get_dqpn(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
2267     ibt_ud_returns_t *returns)
2268 {
2269 	ibt_status_t		retval;
2270 
2271 	IBTF_DPRINTF_L3(cmlog, "ibt_ud_get_dqpn(%p, %x, %p)",
2272 	    attr, mode, returns);
2273 
2274 	retval = ibcm_validate_dqpn_data(attr, mode, returns);
2275 	if (retval != IBT_SUCCESS) {
2276 		return (retval);
2277 	}
2278 
2279 	return (ibcm_ud_get_dqpn(attr, mode, returns));
2280 }
2281 
2282 
2283 /*
2284  * ibt_cm_delay:
2285  *	A client CM handler function can call this function
2286  *	to extend its response time to a CM event.
2287  * INPUTS:
2288  *	flags		Indicates what CM message processing is being delayed
2289  *			by the CM handler, valid values are:
2290  *				IBT_CM_DELAY_REQ
2291  *				IBT_CM_DELAY_REP
2292  *				IBT_CM_DELAY_LAP
2293  *	cm_session_id	The session ID that was passed to client srv_handler
2294  *			by the CM
2295  *	service_time	The extended service time
2296  *	priv_data	Vendor specific data to be sent in the CM generated
2297  *			MRA message. Should be NULL if not specified.
2298  *	len		The number of bytes of data specified by priv_data.
2299  *
2300  * RETURN VALUES:
2301  *	IBT_SUCCESS	on success (or respective failure on error)
2302  */
2303 ibt_status_t
2304 ibt_cm_delay(ibt_cmdelay_flags_t flags, void *cm_session_id,
2305     clock_t service_time, void *priv_data, ibt_priv_data_len_t len)
2306 {
2307 	uint8_t			msg_typ = 0;
2308 	ibcm_mra_msg_t		*mra_msgp;
2309 	ibcm_state_data_t	*statep;
2310 	ibt_status_t		status;
2311 
2312 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_delay(0x%x, %p, 0x%x)",
2313 	    flags, cm_session_id, service_time);
2314 
2315 	/*
2316 	 * Make sure channel is associated with a statep
2317 	 */
2318 	statep = (ibcm_state_data_t *)cm_session_id;
2319 
2320 	if (statep == NULL) {
2321 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: statep NULL");
2322 		return (IBT_INVALID_PARAM);
2323 	}
2324 
2325 	IBTF_DPRINTF_L4(cmlog, "ibt_cm_delay: statep %p", statep);
2326 
2327 	/* Allocate an ibmf msg for mra, if not allocated yet */
2328 	if (statep->mra_msg == NULL) {
2329 		if ((status = ibcm_alloc_out_msg(
2330 		    statep->stored_reply_addr.ibmf_hdl, &statep->mra_msg,
2331 		    MAD_METHOD_SEND)) != IBT_SUCCESS) {
2332 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: chan 0x%p"
2333 			    "IBMF MSG allocation failed", statep->channel);
2334 			return (status);
2335 		}
2336 	}
2337 
2338 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mra_msgp))
2339 
2340 	mra_msgp = (ibcm_mra_msg_t *)IBCM_OUT_MSGP(statep->mra_msg);
2341 	mra_msgp->mra_local_comm_id = h2b32(statep->local_comid);
2342 	mra_msgp->mra_remote_comm_id = h2b32(statep->remote_comid);
2343 
2344 	/* fill in rest of MRA's fields - Message MRAed and Service Timeout */
2345 	if (flags == IBT_CM_DELAY_REQ) {
2346 		msg_typ = IBT_CM_MRA_TYPE_REQ;
2347 	} else if (flags == IBT_CM_DELAY_REP) {
2348 		msg_typ = IBT_CM_MRA_TYPE_REP;
2349 	} else if (flags == IBT_CM_DELAY_LAP) {
2350 		msg_typ = IBT_CM_MRA_TYPE_LAP;
2351 	}
2352 
2353 	mra_msgp->mra_message_type_plus = msg_typ << 6;
2354 	mra_msgp->mra_service_timeout_plus = ibt_usec2ib(service_time) << 3;
2355 
2356 	len = min(len, IBT_MRA_PRIV_DATA_SZ);
2357 	if (priv_data && (len > 0))
2358 		bcopy(priv_data, mra_msgp->mra_private_data, len);
2359 
2360 	IBCM_OUT_HDRP(statep->mra_msg)->AttributeID =
2361 	    h2b16(IBCM_INCOMING_MRA + IBCM_ATTR_BASE_ID);
2362 
2363 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mra_msgp))
2364 
2365 	mutex_enter(&statep->state_mutex);
2366 
2367 	if ((statep->mode == IBCM_ACTIVE_MODE) &&
2368 	    (statep->state == IBCM_STATE_REP_RCVD)) {
2369 		statep->state = IBCM_STATE_MRA_REP_SENT;
2370 	} else if (statep->mode == IBCM_PASSIVE_MODE) {
2371 		if (statep->state == IBCM_STATE_REQ_RCVD) {
2372 			statep->state = IBCM_STATE_MRA_SENT;
2373 		} else if (statep->ap_state == IBCM_AP_STATE_LAP_RCVD) {
2374 			statep->ap_state = IBCM_AP_STATE_MRA_LAP_RCVD;
2375 		} else {
2376 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: invalid state "
2377 			    "/ap_state/mode %x, %x, %x", statep->state,
2378 			    statep->ap_state, statep->mode);
2379 			mutex_exit(&statep->state_mutex);
2380 			return (IBT_CHAN_STATE_INVALID);
2381 		}
2382 	} else {
2383 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: invalid state "
2384 		    "/ap_state/mode %x, %x, %x", statep->state,
2385 		    statep->ap_state, statep->mode);
2386 		mutex_exit(&statep->state_mutex);
2387 
2388 		return (IBT_CHAN_STATE_INVALID);
2389 	}
2390 	/* service time is usecs, stale_clock is nsecs */
2391 	statep->stale_clock = gethrtime() +
2392 	    (hrtime_t)ibt_ib2usec(ibt_usec2ib(service_time)) * (1000 *
2393 	    statep->max_cm_retries);
2394 
2395 	statep->send_mad_flags |= IBCM_MRA_POST_BUSY;
2396 	IBCM_REF_CNT_INCR(statep);	/* for ibcm_post_mra_complete */
2397 	mutex_exit(&statep->state_mutex);
2398 
2399 	IBCM_OUT_HDRP(statep->mra_msg)->TransactionID =
2400 	    IBCM_OUT_HDRP(statep->stored_msg)->TransactionID;
2401 
2402 	/* post the MRA mad in blocking mode, as no timers involved */
2403 	ibcm_post_rc_mad(statep, statep->mra_msg, ibcm_post_mra_complete,
2404 	    statep);
2405 	ibcm_insert_trace(statep, IBCM_TRACE_OUTGOING_MRA);
2406 	/* If this message isn't seen then ibt_cm_delay failed */
2407 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_delay: done !!");
2408 
2409 	return (IBT_SUCCESS);
2410 }
2411 
2412 
2413 /*
2414  * ibt_register_service()
2415  *	Register a service with the IBCM
2416  *
2417  * INPUTS:
2418  *	ibt_hdl		The IBT client handle returned to the client
2419  *			on an ibt_attach() call.
2420  *
2421  *	srv		The address of a ibt_srv_desc_t that describes
2422  *			the service, containing the following:
2423  *
2424  *		sd_ud_handler	The Service CM UD event Handler.
2425  *		sd_handler	The Service CM RC/UC/RD event Handler.
2426  *		sd_flags	Service flags (peer-to-peer, or not).
2427  *
2428  *	sid		This tells CM if the service is local (sid is 0) or
2429  *			wellknown (sid is the starting service id of the range).
2430  *
2431  *	num_sids	The number of contiguous service-ids to reserve.
2432  *
2433  *	srv_hdl		The address of a service identification handle, used
2434  *			to deregister a service, and to bind GIDs to.
2435  *
2436  *	ret_sid		The address to store the Service ID return value.
2437  *			If num_sids > 1, ret_sid is the first Service ID
2438  *			in the range.
2439  *
2440  * ibt_register_service() returns:
2441  *	IBT_SUCCESS		- added a service successfully.
2442  *	IBT_INVALID_PARAM	- invalid input parameter.
2443  *	IBT_CM_FAILURE		- failed to add the service.
2444  *	IBT_CM_SERVICE_EXISTS	- service already exists.
2445  *	IBT_INSUFF_KERNEL_RESOURCE - ran out of local service ids (should
2446  *				     never happen).
2447  */
2448 ibt_status_t
2449 ibt_register_service(ibt_clnt_hdl_t ibt_hdl, ibt_srv_desc_t *srv,
2450     ib_svc_id_t sid, int num_sids, ibt_srv_hdl_t *srv_hdl, ib_svc_id_t *ret_sid)
2451 {
2452 	ibcm_svc_info_t		*svcinfop;
2453 
2454 	IBTF_DPRINTF_L2(cmlog, "ibt_register_service(%p, %p, %llx, %d)",
2455 	    ibt_hdl, srv, (longlong_t)sid, num_sids);
2456 
2457 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*svcinfop))
2458 
2459 	*srv_hdl = NULL;
2460 
2461 	if (num_sids <= 0) {
2462 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2463 		    "Invalid number of service-ids specified (%d)", num_sids);
2464 		return (IBT_INVALID_PARAM);
2465 	}
2466 
2467 	if (sid == 0) {
2468 		if (ret_sid == NULL)
2469 			return (IBT_INVALID_PARAM);
2470 		sid = ibcm_alloc_local_sids(num_sids);
2471 		if (sid == 0)
2472 			return (IBT_INSUFF_KERNEL_RESOURCE);
2473 
2474 	/* Make sure that the ServiceId specified is not of LOCAL AGN type. */
2475 	} else if ((sid & IB_SID_AGN_MASK) == IB_SID_AGN_LOCAL) {
2476 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2477 		    "Invalid non-LOCAL SID specified: 0x%llX",
2478 		    (longlong_t)sid);
2479 		return (IBT_INVALID_PARAM);
2480 	}
2481 
2482 	svcinfop = ibcm_create_svc_entry(sid, num_sids);
2483 
2484 	if (svcinfop == NULL) {
2485 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2486 		    "Service-ID 0x%llx already registered", (longlong_t)sid);
2487 		return (IBT_CM_SERVICE_EXISTS);
2488 	}
2489 
2490 	/*
2491 	 * 'sid' and 'num_sids' are filled in ibcm_create_svc_entry()
2492 	 */
2493 	svcinfop->svc_flags = srv->sd_flags;
2494 	svcinfop->svc_rc_handler = srv->sd_handler;
2495 	svcinfop->svc_ud_handler = srv->sd_ud_handler;
2496 
2497 	if (ret_sid != NULL)
2498 		*ret_sid = sid;
2499 
2500 	*srv_hdl = svcinfop;
2501 
2502 	ibtl_cm_change_service_cnt(ibt_hdl, num_sids);
2503 
2504 	/* If this message isn't seen, then ibt_register_service failed. */
2505 	IBTF_DPRINTF_L2(cmlog, "ibt_register_service: done (%p, %llX)",
2506 	    svcinfop, sid);
2507 
2508 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*svcinfop))
2509 
2510 	return (IBT_SUCCESS);
2511 }
2512 
2513 
2514 static ibt_status_t
2515 ibcm_write_service_record(ibmf_saa_handle_t saa_handle,
2516     sa_service_record_t *srv_recp, ibmf_saa_access_type_t saa_type)
2517 {
2518 	int	rval;
2519 	int	retry;
2520 
2521 	ibcm_sa_access_enter();
2522 	for (retry = 0; retry < ibcm_max_sa_retries; retry++) {
2523 		rval = ibmf_saa_update_service_record(
2524 		    saa_handle, srv_recp, saa_type, 0);
2525 		if (rval != IBMF_TRANS_TIMEOUT) {
2526 			break;
2527 		}
2528 		IBTF_DPRINTF_L2(cmlog, "ibcm_write_service_record: "
2529 		    "ibmf_saa_update_service_record timed out"
2530 		    " SID = %llX, rval = %d, saa_type = %d",
2531 		    (longlong_t)srv_recp->ServiceID, rval, saa_type);
2532 		delay(ibcm_sa_timeout_delay);
2533 	}
2534 	ibcm_sa_access_exit();
2535 
2536 	if (rval != IBMF_SUCCESS) {
2537 		IBTF_DPRINTF_L2(cmlog, "ibcm_write_service_record: "
2538 		    "ibmf_saa_update_service_record() : Failed - %d", rval);
2539 		return (ibcm_ibmf_analyze_error(rval));
2540 	} else
2541 		return (IBT_SUCCESS);
2542 }
2543 
2544 
2545 static void
2546 ibcm_rem_stale_srec(ibmf_saa_handle_t saa_handle, sa_service_record_t *srec)
2547 {
2548 	ibt_status_t		retval;
2549 	uint_t			num_found;
2550 	size_t			length;
2551 	sa_service_record_t	*srv_resp;
2552 	void			*results_p;
2553 	uint_t			i;
2554 	uint64_t		component_mask;
2555 	ibmf_saa_access_args_t	access_args;
2556 
2557 	component_mask =
2558 	    SA_SR_COMPMASK_PKEY | SA_SR_COMPMASK_NAME | SA_SR_COMPMASK_GID;
2559 
2560 	/* Call in SA Access retrieve routine to get Service Records. */
2561 	access_args.sq_attr_id = SA_SERVICERECORD_ATTRID;
2562 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
2563 	access_args.sq_component_mask = component_mask;
2564 	access_args.sq_template = srec;
2565 	access_args.sq_template_length = sizeof (sa_service_record_t);
2566 	access_args.sq_callback = NULL;
2567 	access_args.sq_callback_arg = NULL;
2568 
2569 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
2570 	    &results_p);
2571 	if (retval != IBT_SUCCESS) {
2572 		IBTF_DPRINTF_L2(cmlog, "ibcm_rem_stale_srec: "
2573 		    "SA Access Failure");
2574 		return;
2575 	}
2576 
2577 	num_found = length / sizeof (sa_service_record_t);
2578 
2579 	if (num_found)
2580 		IBTF_DPRINTF_L3(cmlog, "ibcm_rem_stale_srec: "
2581 		    "Found %d matching Service Records.", num_found);
2582 
2583 	/* Validate the returned number of records. */
2584 	if ((results_p != NULL) && (num_found > 0)) {
2585 
2586 		/* Remove all the records. */
2587 		for (i = 0; i < num_found; i++) {
2588 
2589 			srv_resp = (sa_service_record_t *)
2590 			    ((uchar_t *)results_p +
2591 			    i * sizeof (sa_service_record_t));
2592 
2593 			/*
2594 			 * Found some matching records, but check out whether
2595 			 * this Record is really stale or just happens to match
2596 			 * the current session records. If yes, don't remove it.
2597 			 */
2598 			mutex_enter(&ibcm_svc_info_lock);
2599 			if (ibcm_find_svc_entry(srv_resp->ServiceID) != NULL) {
2600 				/* This record is NOT STALE. */
2601 				mutex_exit(&ibcm_svc_info_lock);
2602 				IBTF_DPRINTF_L3(cmlog, "ibcm_rem_stale_srec: "
2603 				    "This is not Stale, it's an active record");
2604 				continue;
2605 			}
2606 			mutex_exit(&ibcm_svc_info_lock);
2607 
2608 			IBTF_DPRINTF_L2(cmlog, "ibcm_rem_stale_srec: "
2609 			    "Removing Stale Rec: %s, %llX",
2610 			    srv_resp->ServiceName, srv_resp->ServiceID);
2611 
2612 			IBCM_DUMP_SERVICE_REC(srv_resp);
2613 
2614 			/*
2615 			 * Remove the Service Record Entry from SA.
2616 			 *
2617 			 * Get ServiceID info from Response Buf, other
2618 			 * attributes are already filled-in.
2619 			 */
2620 
2621 			 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(srec->ServiceID))
2622 
2623 			srec->ServiceID = srv_resp->ServiceID;
2624 
2625 			 _NOTE(NOW_VISIBLE_TO_OTHER_THREADS(srec->ServiceID))
2626 
2627 			(void) ibcm_write_service_record(saa_handle, srec,
2628 			    IBMF_SAA_DELETE);
2629 		}
2630 
2631 		/* Deallocate the memory for results_p. */
2632 		kmem_free(results_p, length);
2633 	}
2634 }
2635 
2636 
2637 
2638 /*
2639  * ibt_bind_service()
2640  *	Register a service with the IBCM
2641  *
2642  * INPUTS:
2643  *	srv_hdl		The service id handle returned to the client
2644  *			on an ibt_service_register() call.
2645  *
2646  *	gid		The GID to which to bind the service.
2647  *
2648  *	srv_bind	The address of a ibt_srv_bind_t that describes
2649  *			the service record.  This should be NULL if there
2650  *			is to be no service record.  This contains:
2651  *
2652  *		sb_lease	Lease period
2653  *		sb_pkey		Partition
2654  *		sb_name		pointer to ASCII string Service Name,
2655  *				NULL terminated.
2656  *		sb_key[]	Key to secure the service record.
2657  *		sb_data		Service Data structure (64-byte)
2658  *
2659  *	cm_private	First argument of Service handler.
2660  *
2661  *	sb_hdl_p	The address of a service bind handle, used
2662  *			to undo the service binding.
2663  *
2664  * ibt_bind_service() returns:
2665  *	IBT_SUCCESS		- added a service successfully.
2666  *	IBT_INVALID_PARAM	- invalid input parameter.
2667  *	IBT_CM_FAILURE		- failed to add the service.
2668  *	IBT_CM_SERVICE_EXISTS	- service already exists.
2669  */
2670 ibt_status_t
2671 ibt_bind_service(ibt_srv_hdl_t srv_hdl, ib_gid_t gid, ibt_srv_bind_t *srv_bind,
2672     void *cm_private, ibt_sbind_hdl_t *sb_hdl_p)
2673 {
2674 	ibt_status_t		status;
2675 	ibtl_cm_hca_port_t	port;
2676 	ibcm_svc_bind_t		*sbindp, *sbp;
2677 	ibcm_hca_info_t		*hcap;
2678 	ib_svc_id_t		sid, start_sid, end_sid;
2679 	ibmf_saa_handle_t	saa_handle;
2680 	sa_service_record_t	srv_rec;
2681 	uint16_t		pkey_ix;
2682 
2683 	if (sb_hdl_p != NULL)
2684 		*sb_hdl_p = NULL;	/* return value for error cases */
2685 
2686 	IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: srv_hdl %p, gid (%llX:%llX)",
2687 	    srv_hdl, (longlong_t)gid.gid_prefix, (longlong_t)gid.gid_guid);
2688 
2689 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sbindp))
2690 
2691 	/* Call ibtl_cm_get_hca_port to get the port number and the HCA GUID. */
2692 	if ((status = ibtl_cm_get_hca_port(gid, 0, &port)) != IBT_SUCCESS) {
2693 		IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2694 		    "ibtl_cm_get_hca_port failed: %d", status);
2695 		return (status);
2696 	}
2697 	IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: Port:%d HCA GUID:%llX",
2698 	    port.hp_port, port.hp_hca_guid);
2699 
2700 	hcap = ibcm_find_hca_entry(port.hp_hca_guid);
2701 	if (hcap == NULL) {
2702 		IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: NO HCA found");
2703 		return (IBT_HCA_BUSY_DETACHING);
2704 	}
2705 	IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: hcap = %p", hcap);
2706 
2707 	if (srv_bind != NULL) {
2708 		saa_handle = ibcm_get_saa_handle(hcap, port.hp_port);
2709 		if (saa_handle == NULL) {
2710 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2711 			    "saa_handle is NULL");
2712 			ibcm_dec_hca_acc_cnt(hcap);
2713 			return (IBT_HCA_PORT_NOT_ACTIVE);
2714 		}
2715 		if (srv_bind->sb_pkey == 0) {
2716 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2717 			    "P_Key must not be 0");
2718 			ibcm_dec_hca_acc_cnt(hcap);
2719 			return (IBT_INVALID_PARAM);
2720 		}
2721 		if (strlen(srv_bind->sb_name) >= IB_SVC_NAME_LEN) {
2722 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2723 			    "Service Name is too long");
2724 			ibcm_dec_hca_acc_cnt(hcap);
2725 			return (IBT_INVALID_PARAM);
2726 		} else
2727 			IBTF_DPRINTF_L3(cmlog, "ibt_bind_service: "
2728 			    "Service Name='%s'", srv_bind->sb_name);
2729 		status = ibt_pkey2index_byguid(port.hp_hca_guid,
2730 		    port.hp_port, srv_bind->sb_pkey, &pkey_ix);
2731 		if (status != IBT_SUCCESS) {
2732 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2733 			    "P_Key 0x%x not found in P_Key_Table",
2734 			    srv_bind->sb_pkey);
2735 			ibcm_dec_hca_acc_cnt(hcap);
2736 			return (status);
2737 		}
2738 	}
2739 
2740 	/* assume success - allocate before locking */
2741 	sbindp = kmem_zalloc(sizeof (*sbindp), KM_SLEEP);
2742 	sbindp->sbind_cm_private = cm_private;
2743 	sbindp->sbind_gid = gid;
2744 	sbindp->sbind_hcaguid = port.hp_hca_guid;
2745 	sbindp->sbind_port = port.hp_port;
2746 
2747 	mutex_enter(&ibcm_svc_info_lock);
2748 
2749 	sbp = srv_hdl->svc_bind_list;
2750 	while (sbp != NULL) {
2751 		if (sbp->sbind_gid.gid_guid == gid.gid_guid &&
2752 		    sbp->sbind_gid.gid_prefix == gid.gid_prefix) {
2753 			if (srv_bind == NULL ||
2754 			    srv_bind->sb_pkey == sbp->sbind_pkey) {
2755 				IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2756 				    "failed: GID %llX:%llX and PKEY %x is "
2757 				    "already bound", gid.gid_prefix,
2758 				    gid.gid_guid, sbp->sbind_pkey);
2759 				mutex_exit(&ibcm_svc_info_lock);
2760 				ibcm_dec_hca_acc_cnt(hcap);
2761 				kmem_free(sbindp, sizeof (*sbindp));
2762 				return (IBT_CM_SERVICE_EXISTS);
2763 			}
2764 		}
2765 		sbp = sbp->sbind_link;
2766 	}
2767 	/* no entry found */
2768 
2769 	sbindp->sbind_link = srv_hdl->svc_bind_list;
2770 	srv_hdl->svc_bind_list = sbindp;
2771 
2772 	mutex_exit(&ibcm_svc_info_lock);
2773 
2774 	if (srv_bind != NULL) {
2775 		bzero(&srv_rec, sizeof (srv_rec));
2776 
2777 		srv_rec.ServiceLease =
2778 		    sbindp->sbind_lease = srv_bind->sb_lease;
2779 		srv_rec.ServiceP_Key =
2780 		    sbindp->sbind_pkey = srv_bind->sb_pkey;
2781 		srv_rec.ServiceKey_hi =
2782 		    sbindp->sbind_key[0] = srv_bind->sb_key[0];
2783 		srv_rec.ServiceKey_lo =
2784 		    sbindp->sbind_key[1] = srv_bind->sb_key[1];
2785 		(void) strcpy(sbindp->sbind_name, srv_bind->sb_name);
2786 		(void) strcpy((char *)srv_rec.ServiceName, srv_bind->sb_name);
2787 		srv_rec.ServiceGID = gid;
2788 
2789 		/*
2790 		 * Find out whether we have any stale Local Service records
2791 		 * matching the current attributes.  If yes, we shall try to
2792 		 * remove them from SA using the current request's ServiceKey.
2793 		 *
2794 		 * We will perform this operation only for Local Services, as
2795 		 * it is handled by SA automatically for WellKnown Services.
2796 		 *
2797 		 * Ofcourse, clients can specify NOT to do this clean-up by
2798 		 * setting IBT_SBIND_NO_CLEANUP flag (srv_bind->sb_flag).
2799 		 */
2800 		if ((srv_hdl->svc_id & IB_SID_AGN_LOCAL) &&
2801 		    (!(srv_bind->sb_flag & IBT_SBIND_NO_CLEANUP))) {
2802 			ibcm_rem_stale_srec(saa_handle, &srv_rec);
2803 		}
2804 
2805 		/* Handle endianess for service data. */
2806 		ibcm_swizzle_from_srv(&srv_bind->sb_data, sbindp->sbind_data);
2807 
2808 		bcopy(sbindp->sbind_data, srv_rec.ServiceData, IB_SVC_DATA_LEN);
2809 
2810 		/* insert srv record into the SA */
2811 		start_sid = srv_hdl->svc_id;
2812 		end_sid = start_sid + srv_hdl->svc_num_sids - 1;
2813 		for (sid = start_sid; sid <= end_sid; sid++) {
2814 
2815 			srv_rec.ServiceID = sid;
2816 
2817 			IBCM_DUMP_SERVICE_REC(&srv_rec);
2818 
2819 			IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: "
2820 			    "ibmf_saa_write_service_record, SvcId = %llX",
2821 			    (longlong_t)sid);
2822 
2823 			status = ibcm_write_service_record(saa_handle, &srv_rec,
2824 			    IBMF_SAA_UPDATE);
2825 			if (status != IBT_SUCCESS) {
2826 				IBTF_DPRINTF_L2(cmlog, "ibt_bind_service:"
2827 				    " ibcm_write_service_record fails %d, "
2828 				    "sid %llX", status, (longlong_t)sid);
2829 
2830 				if (sid != start_sid) {
2831 					/*
2832 					 * Bind failed while bind SID other than
2833 					 * first in the sid_range.  So we need
2834 					 * to unbind those, which are passed.
2835 					 *
2836 					 * Need to increment svc count to
2837 					 * compensate for ibt_unbind_service().
2838 					 */
2839 					ibcm_inc_hca_svc_cnt(hcap);
2840 					ibcm_dec_hca_acc_cnt(hcap);
2841 
2842 					(void) ibt_unbind_service(srv_hdl,
2843 					    sbindp);
2844 				} else {
2845 					ibcm_svc_bind_t		**sbpp;
2846 
2847 					/*
2848 					 * Bind failed for the first SID or the
2849 					 * only SID in question, then no need
2850 					 * to unbind, just free memory and
2851 					 * return error.
2852 					 */
2853 					mutex_enter(&ibcm_svc_info_lock);
2854 
2855 					sbpp = &srv_hdl->svc_bind_list;
2856 					sbp = *sbpp;
2857 					while (sbp != NULL) {
2858 						if (sbp == sbindp) {
2859 							*sbpp = sbp->sbind_link;
2860 							break;
2861 						}
2862 						sbpp = &sbp->sbind_link;
2863 						sbp = *sbpp;
2864 					}
2865 					mutex_exit(&ibcm_svc_info_lock);
2866 					ibcm_dec_hca_acc_cnt(hcap);
2867 
2868 					kmem_free(sbindp, sizeof (*sbindp));
2869 				}
2870 				return (status);
2871 			}
2872 		}
2873 	}
2874 	ibcm_inc_hca_svc_cnt(hcap);
2875 	ibcm_dec_hca_acc_cnt(hcap);
2876 
2877 	/* If this message isn't seen then ibt_bind_service failed */
2878 	IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: DONE (%p, %llX:%llX)",
2879 	    srv_hdl, gid.gid_prefix, gid.gid_guid);
2880 
2881 	if (sb_hdl_p != NULL)
2882 		*sb_hdl_p = sbindp;
2883 
2884 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sbindp))
2885 
2886 	return (IBT_SUCCESS);
2887 }
2888 
2889 ibt_status_t
2890 ibt_unbind_service(ibt_srv_hdl_t srv_hdl, ibt_sbind_hdl_t sbindp)
2891 {
2892 	ib_svc_id_t	sid, end_sid;
2893 	ibt_status_t	rval;
2894 	ibcm_hca_info_t	*hcap;
2895 	ibcm_svc_bind_t	*sbp, **sbpp;
2896 
2897 	IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service(%p, %p)",
2898 	    srv_hdl, sbindp);
2899 
2900 	hcap = ibcm_find_hca_entry(sbindp->sbind_hcaguid);
2901 
2902 	/* If there is a service on hca, respective hcap cannot go away */
2903 	ASSERT(hcap != NULL);
2904 
2905 	mutex_enter(&ibcm_svc_info_lock);
2906 
2907 	sbpp = &srv_hdl->svc_bind_list;
2908 	sbp = *sbpp;
2909 	while (sbp != NULL) {
2910 		if (sbp == sbindp) {
2911 			*sbpp = sbp->sbind_link;
2912 			break;
2913 		}
2914 		sbpp = &sbp->sbind_link;
2915 		sbp = *sbpp;
2916 	}
2917 	sid = srv_hdl->svc_id;
2918 	end_sid = srv_hdl->svc_id + srv_hdl->svc_num_sids - 1;
2919 	if (sbp != NULL)
2920 		while (sbp->sbind_rewrite_state == IBCM_REWRITE_BUSY)
2921 			cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
2922 	mutex_exit(&ibcm_svc_info_lock);
2923 
2924 	if (sbp == NULL) {
2925 		IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2926 		    "service binding not found: srv_hdl %p, srv_bind %p",
2927 		    srv_hdl, sbindp);
2928 		ibcm_dec_hca_acc_cnt(hcap);
2929 		return (IBT_INVALID_PARAM);
2930 	}
2931 
2932 	if (sbindp->sbind_pkey != 0) {	/* Are there service records? */
2933 		ibtl_cm_hca_port_t	port;
2934 		sa_service_record_t	srv_rec;
2935 		ibmf_saa_handle_t	saa_handle;
2936 		ibt_status_t		status;
2937 
2938 		/* get the default SGID of the port */
2939 		if ((status = ibtl_cm_get_hca_port(sbindp->sbind_gid, 0, &port))
2940 		    != IBT_SUCCESS) {
2941 			IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2942 			    "ibtl_cm_get_hca_port failed: %d", status);
2943 			/* we're done, but there may be stale service records */
2944 			goto done;
2945 		}
2946 
2947 		saa_handle = ibcm_get_saa_handle(hcap, port.hp_port);
2948 		if (saa_handle == NULL) {
2949 			IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2950 			    "saa_handle is NULL");
2951 			/* we're done, but there may be stale service records */
2952 			goto done;
2953 		}
2954 
2955 		/* Fill in fields of srv_rec */
2956 		bzero(&srv_rec, sizeof (srv_rec));
2957 
2958 		srv_rec.ServiceP_Key = sbindp->sbind_pkey;
2959 		srv_rec.ServiceKey_hi = sbindp->sbind_key[0];
2960 		srv_rec.ServiceKey_lo = sbindp->sbind_key[1];
2961 		srv_rec.ServiceGID = sbindp->sbind_gid;
2962 		(void) strcpy((char *)srv_rec.ServiceName, sbindp->sbind_name);
2963 
2964 		while (sid <= end_sid) {
2965 
2966 			srv_rec.ServiceID = sid;
2967 			IBCM_DUMP_SERVICE_REC(&srv_rec);
2968 
2969 			rval = ibcm_write_service_record(saa_handle, &srv_rec,
2970 			    IBMF_SAA_DELETE);
2971 
2972 			IBTF_DPRINTF_L4(cmlog, "ibt_unbind_service: "
2973 			    "ibcm_write_service_record rval = %d, SID %llx",
2974 			    rval, sid);
2975 			if (rval != IBT_SUCCESS) {
2976 				/* this is not considered a reason to fail */
2977 				IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2978 				    "ibcm_write_service_record fails %d, "
2979 				    "sid %llx", rval, sid);
2980 			}
2981 			sid++;
2982 		}
2983 	}
2984 done:
2985 	ibcm_dec_hca_svc_cnt(hcap);
2986 	ibcm_dec_hca_acc_cnt(hcap);
2987 	kmem_free(sbindp, sizeof (*sbindp));
2988 
2989 	/* If this message isn't seen then ibt_unbind_service failed */
2990 	IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: done !!");
2991 
2992 	return (IBT_SUCCESS);
2993 }
2994 
2995 /*
2996  * Simply pull off each binding from the list and unbind it.
2997  * If any of the unbind calls fail, we fail.
2998  */
2999 ibt_status_t
3000 ibt_unbind_all_services(ibt_srv_hdl_t srv_hdl)
3001 {
3002 	ibt_status_t	status;
3003 	ibcm_svc_bind_t	*sbp;
3004 
3005 	mutex_enter(&ibcm_svc_info_lock);
3006 	sbp = NULL;
3007 
3008 	/* this compare keeps the loop from being infinite */
3009 	while (sbp != srv_hdl->svc_bind_list) {
3010 		sbp = srv_hdl->svc_bind_list;
3011 		mutex_exit(&ibcm_svc_info_lock);
3012 		status = ibt_unbind_service(srv_hdl, sbp);
3013 		if (status != IBT_SUCCESS)
3014 			return (status);
3015 		mutex_enter(&ibcm_svc_info_lock);
3016 		if (srv_hdl->svc_bind_list == NULL)
3017 			break;
3018 	}
3019 	mutex_exit(&ibcm_svc_info_lock);
3020 	return (IBT_SUCCESS);
3021 }
3022 
3023 /*
3024  * ibt_deregister_service()
3025  *	Deregister a service with the IBCM
3026  *
3027  * INPUTS:
3028  *	ibt_hdl		The IBT client handle returned to the client
3029  *			on an ibt_attach() call.
3030  *
3031  *	srv_hdl		The address of a service identification handle, used
3032  *			to de-register a service.
3033  * RETURN VALUES:
3034  *	IBT_SUCCESS	on success (or respective failure on error)
3035  */
3036 ibt_status_t
3037 ibt_deregister_service(ibt_clnt_hdl_t ibt_hdl, ibt_srv_hdl_t srv_hdl)
3038 {
3039 	ibcm_svc_info_t		*svcp;
3040 	ibcm_svc_lookup_t	svc;
3041 
3042 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service(%p, %p)",
3043 	    ibt_hdl, srv_hdl);
3044 
3045 	mutex_enter(&ibcm_svc_info_lock);
3046 
3047 	if (srv_hdl->svc_bind_list != NULL) {
3048 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service:"
3049 		    " srv_hdl %p still has bindings", srv_hdl);
3050 		mutex_exit(&ibcm_svc_info_lock);
3051 		return (IBT_CM_SERVICE_BUSY);
3052 	}
3053 	svc.sid = srv_hdl->svc_id;
3054 	svc.num_sids = 1;
3055 	IBTF_DPRINTF_L3(cmlog, "ibt_deregister_service: SID 0x%llX, numsids %d",
3056 	    srv_hdl->svc_id, srv_hdl->svc_num_sids);
3057 
3058 #ifdef __lock_lint
3059 	ibcm_svc_compare(NULL, NULL);
3060 #endif
3061 	svcp = avl_find(&ibcm_svc_avl_tree, &svc, NULL);
3062 	if (svcp != srv_hdl) {
3063 		mutex_exit(&ibcm_svc_info_lock);
3064 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service(): "
3065 		    "srv_hdl %p not found", srv_hdl);
3066 		return (IBT_INVALID_PARAM);
3067 	}
3068 	avl_remove(&ibcm_svc_avl_tree, svcp);
3069 
3070 	/* wait for active REQ/SREQ handling to be done */
3071 	svcp->svc_to_delete = 1;
3072 	while (svcp->svc_ref_cnt != 0)
3073 		cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
3074 
3075 	mutex_exit(&ibcm_svc_info_lock);
3076 
3077 	if ((srv_hdl->svc_id & IB_SID_AGN_MASK) == IB_SID_AGN_LOCAL)
3078 		ibcm_free_local_sids(srv_hdl->svc_id, srv_hdl->svc_num_sids);
3079 
3080 	ibtl_cm_change_service_cnt(ibt_hdl, -srv_hdl->svc_num_sids);
3081 	kmem_free(srv_hdl, sizeof (*srv_hdl));
3082 
3083 	/* If this message isn't seen then ibt_deregister_service failed */
3084 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service: done !!");
3085 
3086 	return (IBT_SUCCESS);
3087 }
3088 
3089 ibcm_status_t
3090 ibcm_ar_init(void)
3091 {
3092 	ib_svc_id_t	sid = IBCM_DAPL_ATS_SID;
3093 	ibcm_svc_info_t *tmp_svcp;
3094 
3095 	IBTF_DPRINTF_L3(cmlog, "ibcm_ar_init()");
3096 
3097 	/* remove this special SID from the pool of available SIDs */
3098 	if ((tmp_svcp = ibcm_create_svc_entry(sid, 1)) == NULL) {
3099 		IBTF_DPRINTF_L2(cmlog, "ibcm_ar_init: "
3100 		    "DAPL ATS SID 0x%llx already registered", (longlong_t)sid);
3101 		return (IBCM_FAILURE);
3102 	}
3103 	mutex_enter(&ibcm_svc_info_lock);
3104 	ibcm_ar_svcinfop = tmp_svcp;
3105 	ibcm_ar_list = NULL;	/* no address records registered yet */
3106 	mutex_exit(&ibcm_svc_info_lock);
3107 	return (IBCM_SUCCESS);
3108 }
3109 
3110 ibcm_status_t
3111 ibcm_ar_fini(void)
3112 {
3113 	ibcm_ar_t	*ar_list;
3114 	ibcm_svc_info_t	*tmp_svcp;
3115 
3116 	mutex_enter(&ibcm_svc_info_lock);
3117 	ar_list = ibcm_ar_list;
3118 
3119 	if (ar_list == NULL &&
3120 	    avl_numnodes(&ibcm_svc_avl_tree) == 1 &&
3121 	    avl_first(&ibcm_svc_avl_tree) == ibcm_ar_svcinfop) {
3122 		avl_remove(&ibcm_svc_avl_tree, ibcm_ar_svcinfop);
3123 		tmp_svcp = ibcm_ar_svcinfop;
3124 		mutex_exit(&ibcm_svc_info_lock);
3125 		kmem_free(tmp_svcp, sizeof (*ibcm_ar_svcinfop));
3126 		return (IBCM_SUCCESS);
3127 	}
3128 	mutex_exit(&ibcm_svc_info_lock);
3129 	return (IBCM_FAILURE);
3130 }
3131 
3132 
3133 /*
3134  * Return to the caller:
3135  *	IBT_SUCCESS		Found a perfect match.
3136  *				*arpp is set to the record.
3137  *	IBT_INCONSISTENT_AR	Found a record that's inconsistent.
3138  *	IBT_AR_NOT_REGISTERED	Found no record with same GID/pkey and
3139  *				found no record with same data.
3140  */
3141 static ibt_status_t
3142 ibcm_search_ar(ibt_ar_t *arp, ibcm_ar_t **arpp)
3143 {
3144 	ibcm_ar_t	*tmp;
3145 	int		i;
3146 
3147 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
3148 	tmp = ibcm_ar_list;
3149 	while (tmp != NULL) {
3150 		if (tmp->ar.ar_gid.gid_prefix == arp->ar_gid.gid_prefix &&
3151 		    tmp->ar.ar_gid.gid_guid == arp->ar_gid.gid_guid &&
3152 		    tmp->ar.ar_pkey == arp->ar_pkey) {
3153 			for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++)
3154 				if (tmp->ar.ar_data[i] != arp->ar_data[i])
3155 					return (IBT_INCONSISTENT_AR);
3156 			*arpp = tmp;
3157 			return (IBT_SUCCESS);
3158 		} else {
3159 			/* if all the data bytes match, we have inconsistency */
3160 			for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++)
3161 				if (tmp->ar.ar_data[i] != arp->ar_data[i])
3162 					break;
3163 			if (i == IBCM_DAPL_ATS_NBYTES)
3164 				return (IBT_INCONSISTENT_AR);
3165 			/* try next address record */
3166 		}
3167 		tmp = tmp->ar_link;
3168 	}
3169 	return (IBT_AR_NOT_REGISTERED);
3170 }
3171 
3172 ibt_status_t
3173 ibt_register_ar(ibt_clnt_hdl_t ibt_hdl, ibt_ar_t *arp)
3174 {
3175 	ibcm_ar_t		*found;
3176 	ibcm_ar_t		*tmp;
3177 	ibt_status_t		status;
3178 	ibt_status_t		s1, s2;
3179 	char			*s;
3180 	ibcm_ar_ref_t		*hdlp;
3181 	ibcm_ar_t		*new;
3182 	ibcm_ar_t		**linkp;
3183 	ibtl_cm_hca_port_t	cm_port;
3184 	uint16_t		pkey_ix;
3185 	ibcm_hca_info_t		*hcap;
3186 	ibmf_saa_handle_t	saa_handle;
3187 	sa_service_record_t	*srv_recp;
3188 	uint64_t		gid_ored;
3189 
3190 	IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: PKey 0x%X GID %llX:%llX",
3191 	    arp->ar_pkey, (longlong_t)arp->ar_gid.gid_prefix,
3192 	    (longlong_t)arp->ar_gid.gid_guid);
3193 
3194 	/*
3195 	 * If P_Key is 0, but GID is not, this query is invalid.
3196 	 * If GID is 0, but P_Key is not, this query is invalid.
3197 	 */
3198 	gid_ored = arp->ar_gid.gid_guid | arp->ar_gid.gid_prefix;
3199 	if ((arp->ar_pkey == 0 && gid_ored != 0ULL) ||
3200 	    (arp->ar_pkey != 0 && gid_ored == 0ULL)) {
3201 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3202 		    "GID/P_Key is not valid");
3203 		return (IBT_INVALID_PARAM);
3204 	}
3205 
3206 	/* assume success, so these might be needed */
3207 	hdlp = kmem_alloc(sizeof (*hdlp), KM_SLEEP);
3208 	new = kmem_zalloc(sizeof (*new), KM_SLEEP);
3209 
3210 	mutex_enter(&ibcm_svc_info_lock);
3211 	/* search for existing GID/pkey (there can be at most 1) */
3212 	status = ibcm_search_ar(arp, &found);
3213 	if (status == IBT_INCONSISTENT_AR) {
3214 		mutex_exit(&ibcm_svc_info_lock);
3215 		kmem_free(new, sizeof (*new));
3216 		kmem_free(hdlp, sizeof (*hdlp));
3217 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3218 		    "address record is inconsistent with a known one");
3219 		return (IBT_INCONSISTENT_AR);
3220 	} else if (status == IBT_SUCCESS) {
3221 		if (found->ar_flags == IBCM_AR_INITING) {
3222 			found->ar_waiters++;
3223 			cv_wait(&found->ar_cv, &ibcm_svc_info_lock);
3224 			found->ar_waiters--;
3225 		}
3226 		if (found->ar_flags == IBCM_AR_FAILED) {
3227 			if (found->ar_waiters == 0) {
3228 				cv_destroy(&found->ar_cv);
3229 				kmem_free(found, sizeof (*found));
3230 			}
3231 			mutex_exit(&ibcm_svc_info_lock);
3232 			kmem_free(new, sizeof (*new));
3233 			kmem_free(hdlp, sizeof (*hdlp));
3234 			return (ibt_get_module_failure(IBT_FAILURE_IBCM, 0));
3235 		}
3236 		hdlp->ar_ibt_hdl = ibt_hdl;
3237 		hdlp->ar_ref_link = found->ar_ibt_hdl_list;
3238 		found->ar_ibt_hdl_list = hdlp;
3239 		mutex_exit(&ibcm_svc_info_lock);
3240 		kmem_free(new, sizeof (*new));
3241 		ibtl_cm_change_service_cnt(ibt_hdl, 1);
3242 		return (IBT_SUCCESS);
3243 	} else {
3244 		ASSERT(status == IBT_AR_NOT_REGISTERED);
3245 	}
3246 	hdlp->ar_ref_link = NULL;
3247 	hdlp->ar_ibt_hdl = ibt_hdl;
3248 	new->ar_ibt_hdl_list = hdlp;
3249 	new->ar = *arp;
3250 	new->ar_flags = IBCM_AR_INITING;
3251 	new->ar_waiters = 0;
3252 	cv_init(&new->ar_cv, NULL, CV_DEFAULT, NULL);
3253 	new->ar_link = ibcm_ar_list;
3254 	ibcm_ar_list = new;
3255 
3256 	/* verify GID/pkey is valid for a local port, etc. */
3257 	hcap = NULL;
3258 	if ((s1 = ibtl_cm_get_hca_port(arp->ar_gid, 0, &cm_port))
3259 	    != IBT_SUCCESS ||
3260 	    (s2 = ibt_pkey2index_byguid(cm_port.hp_hca_guid, cm_port.hp_port,
3261 	    arp->ar_pkey, &pkey_ix)) != IBT_SUCCESS ||
3262 	    (hcap = ibcm_find_hca_entry(cm_port.hp_hca_guid)) == NULL) {
3263 		cv_destroy(&new->ar_cv);
3264 		ibcm_ar_list = new->ar_link;
3265 		mutex_exit(&ibcm_svc_info_lock);
3266 		kmem_free(new, sizeof (*new));
3267 		kmem_free(hdlp, sizeof (*hdlp));
3268 		status = IBT_INVALID_PARAM;
3269 		if (s1 == IBT_HCA_PORT_NOT_ACTIVE) {
3270 			s = "PORT DOWN";
3271 			status = IBT_HCA_PORT_NOT_ACTIVE;
3272 		} else if (s1 != IBT_SUCCESS)
3273 			s = "GID not found";
3274 		else if (s2 != IBT_SUCCESS)
3275 			s = "PKEY not found";
3276 		else
3277 			s = "CM could not find its HCA entry";
3278 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: %s, status = %d",
3279 		    s, status);
3280 		return (status);
3281 	}
3282 	mutex_exit(&ibcm_svc_info_lock);
3283 	saa_handle = ibcm_get_saa_handle(hcap, cm_port.hp_port);
3284 
3285 	/* create service record */
3286 	srv_recp = kmem_zalloc(sizeof (*srv_recp), KM_SLEEP);
3287 	srv_recp->ServiceLease = 0xFFFFFFFF;	/* infinite */
3288 	srv_recp->ServiceP_Key = arp->ar_pkey;
3289 	srv_recp->ServiceKey_hi = 0xDA410000ULL;	/* DAPL */
3290 	srv_recp->ServiceKey_lo = 0xA7500000ULL;	/* ATS */
3291 	(void) strcpy((char *)srv_recp->ServiceName, IBCM_DAPL_ATS_NAME);
3292 	srv_recp->ServiceGID = arp->ar_gid;
3293 	bcopy(arp->ar_data, srv_recp->ServiceData, IBCM_DAPL_ATS_NBYTES);
3294 	srv_recp->ServiceID = IBCM_DAPL_ATS_SID;
3295 
3296 	/* insert service record into the SA */
3297 
3298 	IBCM_DUMP_SERVICE_REC(srv_recp);
3299 
3300 	if (saa_handle != NULL)
3301 		status = ibcm_write_service_record(saa_handle, srv_recp,
3302 		    IBMF_SAA_UPDATE);
3303 	else
3304 		status = IBT_HCA_PORT_NOT_ACTIVE;
3305 
3306 	if (status != IBT_SUCCESS) {
3307 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: sa access fails %d, "
3308 		    "sid %llX", status, (longlong_t)srv_recp->ServiceID);
3309 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: FAILED for gid "
3310 		    "%llX:%llX pkey 0x%X", (longlong_t)arp->ar_gid.gid_prefix,
3311 		    (longlong_t)arp->ar_gid.gid_guid, arp->ar_pkey);
3312 
3313 		kmem_free(srv_recp, sizeof (*srv_recp));
3314 		kmem_free(hdlp, sizeof (*hdlp));
3315 
3316 		mutex_enter(&ibcm_svc_info_lock);
3317 		linkp = &ibcm_ar_list;
3318 		tmp = *linkp;
3319 		while (tmp != NULL) {
3320 			if (tmp == new) {
3321 				*linkp = new->ar_link;
3322 				break;
3323 			}
3324 			linkp = &tmp->ar_link;
3325 			tmp = *linkp;
3326 		}
3327 		if (new->ar_waiters > 0) {
3328 			new->ar_flags = IBCM_AR_FAILED;
3329 			cv_broadcast(&new->ar_cv);
3330 			mutex_exit(&ibcm_svc_info_lock);
3331 		} else {
3332 			cv_destroy(&new->ar_cv);
3333 			mutex_exit(&ibcm_svc_info_lock);
3334 			kmem_free(new, sizeof (*new));
3335 		}
3336 		ibcm_dec_hca_acc_cnt(hcap);
3337 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3338 		    "IBMF_SAA failed to write address record");
3339 	} else {					/* SUCCESS */
3340 		uint8_t		*b;
3341 
3342 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: SUCCESS for gid "
3343 		    "%llx:%llx pkey %x", (longlong_t)arp->ar_gid.gid_prefix,
3344 		    (longlong_t)arp->ar_gid.gid_guid, arp->ar_pkey);
3345 		b = arp->ar_data;
3346 
3347 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar:"
3348 		    " data %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
3349 		    b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9],
3350 		    b[10], b[11], b[12], b[13], b[14], b[15]);
3351 		mutex_enter(&ibcm_svc_info_lock);
3352 		new->ar_srv_recp = srv_recp;
3353 		new->ar_saa_handle = saa_handle;
3354 		new->ar_port = cm_port.hp_port;
3355 		new->ar_hcap = hcap;
3356 		new->ar_flags = IBCM_AR_SUCCESS;
3357 		if (new->ar_waiters > 0)
3358 			cv_broadcast(&new->ar_cv);
3359 		mutex_exit(&ibcm_svc_info_lock);
3360 		ibtl_cm_change_service_cnt(ibt_hdl, 1);
3361 		/* do not call ibcm_dec_hca_acc_cnt(hcap) until deregister */
3362 	}
3363 	return (status);
3364 }
3365 
3366 ibt_status_t
3367 ibt_deregister_ar(ibt_clnt_hdl_t ibt_hdl, ibt_ar_t *arp)
3368 {
3369 	ibcm_ar_t		*found;
3370 	ibcm_ar_t		*tmp;
3371 	ibcm_ar_t		**linkp;
3372 	ibcm_ar_ref_t		*hdlp;
3373 	ibcm_ar_ref_t		**hdlpp;
3374 	ibt_status_t		status;
3375 	ibmf_saa_handle_t	saa_handle;
3376 	sa_service_record_t	*srv_recp;
3377 	uint64_t		gid_ored;
3378 
3379 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: pkey %x", arp->ar_pkey);
3380 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: gid %llx:%llx",
3381 	    (longlong_t)arp->ar_gid.gid_prefix,
3382 	    (longlong_t)arp->ar_gid.gid_guid);
3383 
3384 	/*
3385 	 * If P_Key is 0, but GID is not, this query is invalid.
3386 	 * If GID is 0, but P_Key is not, this query is invalid.
3387 	 */
3388 	gid_ored = arp->ar_gid.gid_guid | arp->ar_gid.gid_prefix;
3389 	if ((arp->ar_pkey == 0 && gid_ored != 0ULL) ||
3390 	    (arp->ar_pkey != 0 && gid_ored == 0ULL)) {
3391 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3392 		    "GID/P_Key is not valid");
3393 		return (IBT_INVALID_PARAM);
3394 	}
3395 
3396 	mutex_enter(&ibcm_svc_info_lock);
3397 	/* search for existing GID/pkey (there can be at most 1) */
3398 	status = ibcm_search_ar(arp, &found);
3399 	if (status == IBT_INCONSISTENT_AR || status == IBT_AR_NOT_REGISTERED) {
3400 		mutex_exit(&ibcm_svc_info_lock);
3401 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3402 		    "address record not found");
3403 		return (IBT_AR_NOT_REGISTERED);
3404 	}
3405 	ASSERT(status == IBT_SUCCESS);
3406 
3407 	hdlpp = &found->ar_ibt_hdl_list;
3408 	hdlp = *hdlpp;
3409 	while (hdlp != NULL) {
3410 		if (hdlp->ar_ibt_hdl == ibt_hdl)
3411 			break;
3412 		hdlpp = &hdlp->ar_ref_link;
3413 		hdlp = *hdlpp;
3414 	}
3415 	if (hdlp == NULL) {	/* could not find ibt_hdl on list */
3416 		mutex_exit(&ibcm_svc_info_lock);
3417 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3418 		    "address record found, but not for this client");
3419 		return (IBT_AR_NOT_REGISTERED);
3420 	}
3421 	*hdlpp = hdlp->ar_ref_link;	/* remove ref for this client */
3422 	if (found->ar_ibt_hdl_list == NULL && found->ar_waiters == 0) {
3423 		/* last entry was removed */
3424 		found->ar_flags = IBCM_AR_INITING; /* hold off register_ar */
3425 		saa_handle = found->ar_saa_handle;
3426 		srv_recp = found->ar_srv_recp;
3427 
3428 		/* wait if this service record is being rewritten */
3429 		while (found->ar_rewrite_state == IBCM_REWRITE_BUSY)
3430 			cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
3431 		mutex_exit(&ibcm_svc_info_lock);
3432 
3433 		/* remove service record */
3434 		status = ibcm_write_service_record(saa_handle, srv_recp,
3435 		    IBMF_SAA_DELETE);
3436 		if (status != IBT_SUCCESS)
3437 			IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3438 			    "IBMF_SAA failed to delete address record");
3439 		mutex_enter(&ibcm_svc_info_lock);
3440 		if (found->ar_waiters == 0) {	/* still no waiters */
3441 			linkp = &ibcm_ar_list;
3442 			tmp = *linkp;
3443 			while (tmp != found) {
3444 				linkp = &tmp->ar_link;
3445 				tmp = *linkp;
3446 			}
3447 			*linkp = tmp->ar_link;
3448 			ibcm_dec_hca_acc_cnt(found->ar_hcap);
3449 			kmem_free(srv_recp, sizeof (*srv_recp));
3450 			cv_destroy(&found->ar_cv);
3451 			kmem_free(found, sizeof (*found));
3452 		} else {
3453 			/* add service record back in for the waiters */
3454 			mutex_exit(&ibcm_svc_info_lock);
3455 			status = ibcm_write_service_record(saa_handle, srv_recp,
3456 			    IBMF_SAA_UPDATE);
3457 			mutex_enter(&ibcm_svc_info_lock);
3458 			if (status == IBT_SUCCESS)
3459 				found->ar_flags = IBCM_AR_SUCCESS;
3460 			else {
3461 				found->ar_flags = IBCM_AR_FAILED;
3462 				IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3463 				    "IBMF_SAA failed to write address record");
3464 			}
3465 			cv_broadcast(&found->ar_cv);
3466 		}
3467 	}
3468 	mutex_exit(&ibcm_svc_info_lock);
3469 	kmem_free(hdlp, sizeof (*hdlp));
3470 	ibtl_cm_change_service_cnt(ibt_hdl, -1);
3471 	return (status);
3472 }
3473 
3474 ibt_status_t
3475 ibt_query_ar(ib_gid_t *sgid, ibt_ar_t *queryp, ibt_ar_t *resultp)
3476 {
3477 	sa_service_record_t	svcrec_req;
3478 	sa_service_record_t	*svcrec_resp;
3479 	void			*results_p;
3480 	uint64_t		component_mask = 0;
3481 	uint64_t		gid_ored;
3482 	size_t			length;
3483 	int			num_rec;
3484 	int			i;
3485 	ibmf_saa_access_args_t	access_args;
3486 	ibt_status_t		retval;
3487 	ibtl_cm_hca_port_t	cm_port;
3488 	ibcm_hca_info_t		*hcap;
3489 	ibmf_saa_handle_t	saa_handle;
3490 
3491 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar(%p, %p)", queryp, resultp);
3492 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: sgid %llx:%llx",
3493 	    (longlong_t)sgid->gid_prefix, (longlong_t)sgid->gid_guid);
3494 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: query_pkey %x", queryp->ar_pkey);
3495 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: query_gid %llx:%llx",
3496 	    (longlong_t)queryp->ar_gid.gid_prefix,
3497 	    (longlong_t)queryp->ar_gid.gid_guid);
3498 
3499 	/*
3500 	 * If P_Key is 0, but GID is not, this query is invalid.
3501 	 * If GID is 0, but P_Key is not, this query is invalid.
3502 	 */
3503 	gid_ored = queryp->ar_gid.gid_guid | queryp->ar_gid.gid_prefix;
3504 	if ((queryp->ar_pkey == 0 && gid_ored != 0ULL) ||
3505 	    (queryp->ar_pkey != 0 && gid_ored == 0ULL)) {
3506 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: GID/P_Key is not valid");
3507 		return (IBT_INVALID_PARAM);
3508 	}
3509 
3510 	hcap = NULL;
3511 	if (ibtl_cm_get_hca_port(*sgid, 0, &cm_port) != IBT_SUCCESS ||
3512 	    (hcap = ibcm_find_hca_entry(cm_port.hp_hca_guid)) == NULL ||
3513 	    (saa_handle = ibcm_get_saa_handle(hcap, cm_port.hp_port)) == NULL) {
3514 		if (hcap != NULL)
3515 			ibcm_dec_hca_acc_cnt(hcap);
3516 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: sgid is not valid");
3517 		return (IBT_INVALID_PARAM);
3518 	}
3519 
3520 	bzero(&svcrec_req, sizeof (svcrec_req));
3521 
3522 	/* Is GID/P_Key Specified. */
3523 	if (queryp->ar_pkey != 0) {	/* GID is non-zero from check above */
3524 		svcrec_req.ServiceP_Key = queryp->ar_pkey;
3525 		component_mask |= SA_SR_COMPMASK_PKEY;
3526 		IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: P_Key %X",
3527 		    queryp->ar_pkey);
3528 		svcrec_req.ServiceGID = queryp->ar_gid;
3529 		component_mask |= SA_SR_COMPMASK_GID;
3530 		IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: GID %llX:%llX",
3531 		    (longlong_t)queryp->ar_gid.gid_prefix,
3532 		    (longlong_t)queryp->ar_gid.gid_guid);
3533 	}
3534 
3535 	/* Is ServiceData Specified. */
3536 	for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++) {
3537 		if (queryp->ar_data[i] != 0) {
3538 			bcopy(queryp->ar_data, svcrec_req.ServiceData,
3539 			    IBCM_DAPL_ATS_NBYTES);
3540 			component_mask |= 0xFFFF << 7;	/* all 16 Data8 */
3541 							/* components */
3542 			break;
3543 		}
3544 	}
3545 
3546 	/* Service Name */
3547 	(void) strcpy((char *)svcrec_req.ServiceName, IBCM_DAPL_ATS_NAME);
3548 	component_mask |= SA_SR_COMPMASK_NAME;
3549 
3550 	svcrec_req.ServiceID = IBCM_DAPL_ATS_SID;
3551 	component_mask |= SA_SR_COMPMASK_ID;
3552 
3553 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: "
3554 	    "Perform SA Access: Mask: 0x%X", component_mask);
3555 
3556 	/*
3557 	 * Call in SA Access retrieve routine to get Service Records.
3558 	 *
3559 	 * SA Access framework allocated memory for the "results_p".
3560 	 * Make sure to deallocate once we are done with the results_p.
3561 	 * The size of the buffer allocated will be as returned in
3562 	 * "length" field.
3563 	 */
3564 	access_args.sq_attr_id = SA_SERVICERECORD_ATTRID;
3565 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
3566 	access_args.sq_component_mask = component_mask;
3567 	access_args.sq_template = &svcrec_req;
3568 	access_args.sq_template_length = sizeof (sa_service_record_t);
3569 	access_args.sq_callback = NULL;
3570 	access_args.sq_callback_arg = NULL;
3571 
3572 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
3573 	    &results_p);
3574 
3575 	ibcm_dec_hca_acc_cnt(hcap);
3576 	if (retval != IBT_SUCCESS) {
3577 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: SA Access Failed");
3578 		return (retval);
3579 	}
3580 
3581 	num_rec = length / sizeof (sa_service_record_t);
3582 
3583 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: "
3584 	    "Found %d Service Records.", num_rec);
3585 
3586 	/* Validate the returned number of records. */
3587 	if ((results_p != NULL) && (num_rec > 0)) {
3588 		uint8_t		*b;
3589 
3590 		/* Just return info from the first service record. */
3591 		svcrec_resp = (sa_service_record_t *)results_p;
3592 
3593 		/* The Service GID and Service ID */
3594 		resultp->ar_gid = svcrec_resp->ServiceGID;
3595 		resultp->ar_pkey = svcrec_resp->ServiceP_Key;
3596 		bcopy(svcrec_resp->ServiceData,
3597 		    resultp->ar_data, IBCM_DAPL_ATS_NBYTES);
3598 
3599 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: "
3600 		    "Found: pkey %x dgid %llX:%llX", resultp->ar_pkey,
3601 		    (longlong_t)resultp->ar_gid.gid_prefix,
3602 		    (longlong_t)resultp->ar_gid.gid_guid);
3603 		b = resultp->ar_data;
3604 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar:"
3605 		    " data %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
3606 		    b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9],
3607 		    b[10], b[11], b[12], b[13], b[14], b[15]);
3608 
3609 		/* Deallocate the memory for results_p. */
3610 		kmem_free(results_p, length);
3611 		if (num_rec > 1)
3612 			retval = IBT_MULTIPLE_AR;
3613 		else
3614 			retval = IBT_SUCCESS;
3615 	} else {
3616 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: "
3617 		    "ibmf_sa_access found 0 matching records");
3618 		retval = IBT_AR_NOT_REGISTERED;
3619 	}
3620 	return (retval);
3621 }
3622 
3623 /* mark all ATS service records associated with the port */
3624 static void
3625 ibcm_mark_ar(ib_guid_t hca_guid, uint8_t port)
3626 {
3627 	ibcm_ar_t	*tmp;
3628 
3629 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
3630 	for (tmp = ibcm_ar_list; tmp != NULL; tmp = tmp->ar_link) {
3631 		if (tmp->ar_hcap == NULL)
3632 			continue;
3633 		if (tmp->ar_hcap->hca_guid == hca_guid &&
3634 		    tmp->ar_port == port) {
3635 			/* even if it's busy, we mark it for rewrite */
3636 			tmp->ar_rewrite_state = IBCM_REWRITE_NEEDED;
3637 		}
3638 	}
3639 }
3640 
3641 /* rewrite all ATS service records */
3642 static int
3643 ibcm_rewrite_ar(void)
3644 {
3645 	ibcm_ar_t		*tmp;
3646 	ibmf_saa_handle_t	saa_handle;
3647 	sa_service_record_t	*srv_recp;
3648 	ibt_status_t		rval;
3649 	int			did_something = 0;
3650 
3651 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
3652 check_for_work:
3653 	for (tmp = ibcm_ar_list; tmp != NULL; tmp = tmp->ar_link) {
3654 		if (tmp->ar_rewrite_state == IBCM_REWRITE_NEEDED) {
3655 			tmp->ar_rewrite_state = IBCM_REWRITE_BUSY;
3656 			saa_handle = tmp->ar_saa_handle;
3657 			srv_recp = tmp->ar_srv_recp;
3658 			mutex_exit(&ibcm_svc_info_lock);
3659 			IBTF_DPRINTF_L3(cmlog, "ibcm_rewrite_ar: "
3660 			    "rewriting ar @ %p", tmp);
3661 			did_something = 1;
3662 			rval = ibcm_write_service_record(saa_handle, srv_recp,
3663 			    IBMF_SAA_UPDATE);
3664 			if (rval != IBT_SUCCESS)
3665 				IBTF_DPRINTF_L2(cmlog, "ibcm_rewrite_ar: "
3666 				    "ibcm_write_service_record failed: "
3667 				    "status = %d", rval);
3668 			mutex_enter(&ibcm_svc_info_lock);
3669 			/* if it got marked again, then we want to rewrite */
3670 			if (tmp->ar_rewrite_state == IBCM_REWRITE_BUSY)
3671 				tmp->ar_rewrite_state = IBCM_REWRITE_IDLE;
3672 			/* in case there was a waiter... */
3673 			cv_broadcast(&ibcm_svc_info_cv);
3674 			goto check_for_work;
3675 		}
3676 	}
3677 	return (did_something);
3678 }
3679 
3680 static void
3681 ibcm_rewrite_svc_record(ibcm_svc_info_t *srv_hdl, ibcm_svc_bind_t *sbindp)
3682 {
3683 	ibcm_hca_info_t		*hcap;
3684 	ib_svc_id_t		sid, start_sid, end_sid;
3685 	ibmf_saa_handle_t	saa_handle;
3686 	sa_service_record_t	srv_rec;
3687 	ibt_status_t		rval;
3688 
3689 	hcap = ibcm_find_hca_entry(sbindp->sbind_hcaguid);
3690 	if (hcap == NULL) {
3691 		IBTF_DPRINTF_L2(cmlog, "ibcm_rewrite_svc_record: "
3692 		    "NO HCA found for HCA GUID %llX", sbindp->sbind_hcaguid);
3693 		return;
3694 	}
3695 
3696 	saa_handle = ibcm_get_saa_handle(hcap, sbindp->sbind_port);
3697 	if (saa_handle == NULL) {
3698 		IBTF_DPRINTF_L2(cmlog, "ibcm_rewrite_svc_record: "
3699 		    "saa_handle is NULL");
3700 		ibcm_dec_hca_acc_cnt(hcap);
3701 		return;
3702 	}
3703 
3704 	IBTF_DPRINTF_L3(cmlog, "ibcm_rewrite_svc_record: "
3705 	    "rewriting svc '%s', port_guid = %llX", sbindp->sbind_name,
3706 	    sbindp->sbind_gid.gid_guid);
3707 
3708 	bzero(&srv_rec, sizeof (srv_rec));
3709 
3710 	srv_rec.ServiceLease = sbindp->sbind_lease;
3711 	srv_rec.ServiceP_Key = sbindp->sbind_pkey;
3712 	srv_rec.ServiceKey_hi = sbindp->sbind_key[0];
3713 	srv_rec.ServiceKey_lo = sbindp->sbind_key[1];
3714 	(void) strcpy((char *)srv_rec.ServiceName, sbindp->sbind_name);
3715 	srv_rec.ServiceGID = sbindp->sbind_gid;
3716 
3717 	bcopy(sbindp->sbind_data, srv_rec.ServiceData, IB_SVC_DATA_LEN);
3718 
3719 	/* insert srv record into the SA */
3720 	start_sid = srv_hdl->svc_id;
3721 	end_sid = start_sid + srv_hdl->svc_num_sids - 1;
3722 	for (sid = start_sid; sid <= end_sid; sid++) {
3723 		srv_rec.ServiceID = sid;
3724 
3725 		rval = ibcm_write_service_record(saa_handle, &srv_rec,
3726 		    IBMF_SAA_UPDATE);
3727 
3728 		IBTF_DPRINTF_L4(cmlog, "ibcm_rewrite_svc_record: "
3729 		    "ibcm_write_service_record, SvcId = %llX, "
3730 		    "rval = %d", (longlong_t)sid, rval);
3731 		if (rval != IBT_SUCCESS) {
3732 			IBTF_DPRINTF_L2(cmlog, "ibcm_rewrite_svc_record:"
3733 			    " ibcm_write_service_record fails %d sid %llX",
3734 			    rval, (longlong_t)sid);
3735 		}
3736 	}
3737 	ibcm_dec_hca_acc_cnt(hcap);
3738 }
3739 
3740 /*
3741  * Task to mark all service records as needing to be rewritten to the SM/SA.
3742  * This task does not return until all of them have been rewritten.
3743  */
3744 void
3745 ibcm_service_record_rewrite_task(void *arg)
3746 {
3747 	ibcm_port_up_t	*pup = (ibcm_port_up_t *)arg;
3748 	ib_guid_t	hca_guid = pup->pup_hca_guid;
3749 	uint8_t		port = pup->pup_port;
3750 	ibcm_svc_info_t	*svcp;
3751 	ibcm_svc_bind_t	*sbp;
3752 	avl_tree_t	*avl_tree = &ibcm_svc_avl_tree;
3753 	static int	task_is_running = 0;
3754 
3755 	IBTF_DPRINTF_L2(cmlog, "ibcm_service_record_rewrite_task STARTED "
3756 	    "for hca_guid %llX, port %d", hca_guid, port);
3757 
3758 	mutex_enter(&ibcm_svc_info_lock);
3759 	ibcm_mark_ar(hca_guid, port);
3760 	for (svcp = avl_first(avl_tree); svcp != NULL;
3761 	    svcp = avl_walk(avl_tree, svcp, AVL_AFTER)) {
3762 		sbp = svcp->svc_bind_list;
3763 		while (sbp != NULL) {
3764 			if (sbp->sbind_pkey != 0 &&
3765 			    sbp->sbind_port == port &&
3766 			    sbp->sbind_hcaguid == hca_guid) {
3767 				/* even if it's busy, we mark it for rewrite */
3768 				sbp->sbind_rewrite_state = IBCM_REWRITE_NEEDED;
3769 			}
3770 			sbp = sbp->sbind_link;
3771 		}
3772 	}
3773 	if (task_is_running) {
3774 		/* let the other task thread finish the work */
3775 		mutex_exit(&ibcm_svc_info_lock);
3776 		return;
3777 	}
3778 	task_is_running = 1;
3779 
3780 	(void) ibcm_rewrite_ar();
3781 
3782 check_for_work:
3783 	for (svcp = avl_first(avl_tree); svcp != NULL;
3784 	    svcp = avl_walk(avl_tree, svcp, AVL_AFTER)) {
3785 		sbp = svcp->svc_bind_list;
3786 		while (sbp != NULL) {
3787 			if (sbp->sbind_rewrite_state == IBCM_REWRITE_NEEDED) {
3788 				sbp->sbind_rewrite_state = IBCM_REWRITE_BUSY;
3789 				mutex_exit(&ibcm_svc_info_lock);
3790 				ibcm_rewrite_svc_record(svcp, sbp);
3791 				mutex_enter(&ibcm_svc_info_lock);
3792 				/* if it got marked again, we want to rewrite */
3793 				if (sbp->sbind_rewrite_state ==
3794 				    IBCM_REWRITE_BUSY)
3795 					sbp->sbind_rewrite_state =
3796 					    IBCM_REWRITE_IDLE;
3797 				/* in case there was a waiter... */
3798 				cv_broadcast(&ibcm_svc_info_cv);
3799 				goto check_for_work;
3800 			}
3801 			sbp = sbp->sbind_link;
3802 		}
3803 	}
3804 	/*
3805 	 * If there were no service records to write, and we failed to
3806 	 * have to rewrite any more ATS service records, then we're done.
3807 	 */
3808 	if (ibcm_rewrite_ar() != 0)
3809 		goto check_for_work;
3810 	task_is_running = 0;
3811 	mutex_exit(&ibcm_svc_info_lock);
3812 
3813 	IBTF_DPRINTF_L2(cmlog, "ibcm_service_record_rewrite_task DONE");
3814 	kmem_free(pup, sizeof (ibcm_port_up_t));
3815 }
3816 
3817 
3818 /*
3819  * Function:
3820  * 	ibt_cm_proceed
3821  *
3822  * Verifies the arguments and dispatches the cm state machine processing
3823  * via taskq
3824  */
3825 
3826 ibt_status_t
3827 ibt_cm_proceed(ibt_cm_event_type_t event, void *session_id,
3828     ibt_cm_status_t status, ibt_cm_proceed_reply_t *cm_event_data,
3829     void *priv_data, ibt_priv_data_len_t priv_data_len)
3830 {
3831 	ibcm_state_data_t *statep = (ibcm_state_data_t *)session_id;
3832 	ibcm_proceed_targs_t	*proceed_targs;
3833 	ibcm_proceed_error_t	proceed_error;
3834 
3835 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_proceed chan 0x%p event %x status %x "
3836 	    "session_id %p", statep->channel, event, status, session_id);
3837 
3838 	IBTF_DPRINTF_L5(cmlog, "ibt_cm_proceed chan 0x%p cm_event_data %p, "
3839 	    "priv_data %p priv_data_len %x", statep->channel, cm_event_data,
3840 	    priv_data, priv_data_len);
3841 
3842 	/* validate session_id and status */
3843 	if ((statep == NULL) || (status == IBT_CM_DEFER)) {
3844 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : Invalid Args");
3845 		return (IBT_INVALID_PARAM);
3846 	}
3847 
3848 	/* If priv data len specified, then priv_data cannot be NULL */
3849 	if ((priv_data_len > 0) && (priv_data == NULL))
3850 		return (IBT_INVALID_PARAM);
3851 
3852 	proceed_error = IBCM_PROCEED_INVALID_NONE;
3853 
3854 	mutex_enter(&statep->state_mutex);
3855 	if (event == IBT_CM_EVENT_REQ_RCV) {
3856 
3857 		if ((statep->state != IBCM_STATE_REQ_RCVD) &&
3858 		    (statep->state != IBCM_STATE_MRA_SENT))
3859 			proceed_error = IBCM_PROCEED_INVALID_EVENT_STATE;
3860 		else if (priv_data_len > IBT_REP_PRIV_DATA_SZ)
3861 			proceed_error = IBCM_PROCEED_INVALID_PRIV_SZ;
3862 
3863 	} else if (event == IBT_CM_EVENT_REP_RCV) {
3864 		if ((statep->state != IBCM_STATE_REP_RCVD) &&
3865 		    (statep->state != IBCM_STATE_MRA_REP_SENT))
3866 			proceed_error = IBCM_PROCEED_INVALID_EVENT_STATE;
3867 		else if (priv_data_len > IBT_RTU_PRIV_DATA_SZ)
3868 			proceed_error = IBCM_PROCEED_INVALID_PRIV_SZ;
3869 	} else if (event == IBT_CM_EVENT_LAP_RCV) {
3870 		if ((statep->ap_state != IBCM_AP_STATE_LAP_RCVD) &&
3871 		    (statep->ap_state != IBCM_AP_STATE_MRA_LAP_SENT))
3872 			proceed_error = IBCM_PROCEED_INVALID_EVENT_STATE;
3873 		else if (priv_data_len > IBT_APR_PRIV_DATA_SZ)
3874 			proceed_error = IBCM_PROCEED_INVALID_PRIV_SZ;
3875 	} else if (event == IBT_CM_EVENT_CONN_CLOSED) {
3876 		if (statep->state != IBCM_STATE_DREQ_RCVD)
3877 			proceed_error = IBCM_PROCEED_INVALID_EVENT_STATE;
3878 		else if (priv_data_len > IBT_DREP_PRIV_DATA_SZ)
3879 			proceed_error = IBCM_PROCEED_INVALID_PRIV_SZ;
3880 	} else {
3881 			proceed_error = IBCM_PROCEED_INVALID_EVENT;
3882 	}
3883 
3884 	/* if there is an error, print an error message and return */
3885 	if (proceed_error != IBCM_PROCEED_INVALID_NONE) {
3886 		mutex_exit(&statep->state_mutex);
3887 		if (proceed_error == IBCM_PROCEED_INVALID_EVENT_STATE) {
3888 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : chan 0x%p"
3889 			    "Invalid Event/State combination specified",
3890 			    statep->channel);
3891 			return (IBT_INVALID_PARAM);
3892 		} else if (proceed_error == IBCM_PROCEED_INVALID_PRIV_SZ) {
3893 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : chan 0x%p"
3894 			    "Invalid Event/priv len combination specified",
3895 			    statep->channel);
3896 			return (IBT_INVALID_PARAM);
3897 		} else if (proceed_error == IBCM_PROCEED_INVALID_EVENT) {
3898 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : chan 0x%p"
3899 			    "Invalid Event specified", statep->channel);
3900 			return (IBT_INVALID_PARAM);
3901 		} else {
3902 			ASSERT(proceed_error == IBCM_PROCEED_INVALID_LAP);
3903 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : chan 0x%p"
3904 			    "IBT_CM_EVENT_LAP_RCV not supported",
3905 			    statep->channel);
3906 			/* UNTIL HCA DRIVER ENABLES AP SUPPORT, FAIL THE CALL */
3907 			return (IBT_APM_NOT_SUPPORTED);
3908 		}
3909 	}
3910 
3911 
3912 	/* wait until client's CM handler returns DEFER status back to CM */
3913 
3914 	while (statep->clnt_proceed == IBCM_BLOCK) {
3915 		IBTF_DPRINTF_L5(cmlog, "ibt_cm_proceed : chan 0x%p blocked for "
3916 		    "return of client's cm handler", statep->channel);
3917 		cv_wait(&statep->block_client_cv, &statep->state_mutex);
3918 	}
3919 
3920 	if (statep->clnt_proceed == IBCM_FAIL) {
3921 		mutex_exit(&statep->state_mutex);
3922 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_proceed : chan 0x%p Failed as "
3923 		    "client returned non-DEFER status from cm handler",
3924 		    statep->channel);
3925 		return (IBT_CHAN_STATE_INVALID);
3926 	}
3927 
3928 	ASSERT(statep->clnt_proceed == IBCM_UNBLOCK);
3929 	statep->clnt_proceed = IBCM_FAIL;
3930 	mutex_exit(&statep->state_mutex);
3931 
3932 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*proceed_targs))
3933 
3934 	/* the state machine processing is done in a separate thread */
3935 
3936 	/* proceed_targs is freed in ibcm_proceed_via_taskq */
3937 	proceed_targs = kmem_alloc(sizeof (ibcm_proceed_targs_t),
3938 	    KM_SLEEP);
3939 
3940 	proceed_targs->event  = event;
3941 	proceed_targs->status = status;
3942 	proceed_targs->priv_data_len = priv_data_len;
3943 
3944 	bcopy(priv_data, proceed_targs->priv_data, priv_data_len);
3945 
3946 	proceed_targs->tst.rc.statep = statep;
3947 	bcopy(cm_event_data, &proceed_targs->tst.rc.rc_cm_event_data,
3948 	    sizeof (ibt_cm_proceed_reply_t));
3949 
3950 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*proceed_targs))
3951 
3952 	(void) taskq_dispatch(ibcm_taskq, ibcm_proceed_via_taskq,
3953 	    proceed_targs, TQ_SLEEP);
3954 
3955 	return (IBT_SUCCESS);
3956 }
3957 
3958 /*
3959  * Function:
3960  * 	ibcm_proceed_via_taskq
3961  *
3962  * Called from taskq, dispatched by ibt_cm_proceed
3963  * Completes the cm state processing for ibt_cm_proceed
3964  */
3965 void
3966 ibcm_proceed_via_taskq(void *targs)
3967 {
3968 	ibcm_proceed_targs_t	*proceed_targs = (ibcm_proceed_targs_t *)targs;
3969 	ibcm_state_data_t *statep = proceed_targs->tst.rc.statep;
3970 	ibt_cm_reason_t reject_reason;
3971 	uint8_t arej_len;
3972 	ibcm_status_t response;
3973 	ibcm_clnt_reply_info_t clnt_info;
3974 
3975 	clnt_info.reply_event = &proceed_targs->tst.rc.rc_cm_event_data;
3976 	clnt_info.priv_data = proceed_targs->priv_data;
3977 	clnt_info.priv_data_len = proceed_targs->priv_data_len;
3978 
3979 	IBTF_DPRINTF_L4(cmlog, "ibcm_proceed_via_taskq chan 0x%p targs %x",
3980 	    statep->channel, targs);
3981 
3982 	if (proceed_targs->event == IBT_CM_EVENT_REQ_RCV) {
3983 		response =
3984 		    ibcm_process_cep_req_cm_hdlr(statep, proceed_targs->status,
3985 		    &clnt_info, &reject_reason, &arej_len,
3986 		    (ibcm_req_msg_t *)statep->defer_cm_msg);
3987 
3988 		ibcm_handle_cep_req_response(statep, response, reject_reason,
3989 		    arej_len);
3990 
3991 	} else if (proceed_targs->event == IBT_CM_EVENT_REP_RCV) {
3992 		response =
3993 		    ibcm_process_cep_rep_cm_hdlr(statep, proceed_targs->status,
3994 		    &clnt_info, &reject_reason, &arej_len,
3995 		    (ibcm_rep_msg_t *)statep->defer_cm_msg);
3996 
3997 		ibcm_handle_cep_rep_response(statep, response, reject_reason,
3998 		    arej_len, (ibcm_rep_msg_t *)statep->defer_cm_msg);
3999 
4000 	} else if (proceed_targs->event == IBT_CM_EVENT_LAP_RCV) {
4001 		ibcm_process_cep_lap_cm_hdlr(statep, proceed_targs->status,
4002 		    &clnt_info, (ibcm_lap_msg_t *)statep->defer_cm_msg,
4003 		    (ibcm_apr_msg_t *)IBCM_OUT_MSGP(statep->lapr_msg));
4004 
4005 		ibcm_post_apr_mad(statep);
4006 
4007 	} else {
4008 		ASSERT(proceed_targs->event == IBT_CM_EVENT_CONN_CLOSED);
4009 		ibcm_handle_cep_dreq_response(statep, proceed_targs->priv_data,
4010 		    proceed_targs->priv_data_len);
4011 	}
4012 
4013 	kmem_free(targs, sizeof (ibcm_proceed_targs_t));
4014 }
4015 
4016 /*
4017  * Function:
4018  * 	ibt_cm_ud_proceed
4019  *
4020  * Verifies the arguments and dispatches the cm state machine processing
4021  * via taskq
4022  */
4023 ibt_status_t
4024 ibt_cm_ud_proceed(void *session_id, ibt_channel_hdl_t ud_channel,
4025     ibt_cm_status_t status, ibt_redirect_info_t *redirect_infop,
4026     void *priv_data, ibt_priv_data_len_t priv_data_len)
4027 {
4028 	ibcm_ud_state_data_t *ud_statep = (ibcm_ud_state_data_t *)session_id;
4029 	ibcm_proceed_targs_t	*proceed_targs;
4030 	ibt_qp_query_attr_t	qp_attr;
4031 	ibt_status_t		retval;
4032 
4033 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_ud_proceed session_id %p "
4034 	    "ud_channel %p ", session_id, ud_channel);
4035 
4036 	IBTF_DPRINTF_L4(cmlog, "ibt_cm_ud_proceed status %x priv_data %p "
4037 	    "priv_data_len %x",  status, priv_data, priv_data_len);
4038 
4039 	/* validate session_id and status */
4040 	if ((ud_statep == NULL) || (status == IBT_CM_DEFER)) {
4041 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_ud_proceed : Invalid Args");
4042 		return (IBT_INVALID_PARAM);
4043 	}
4044 
4045 	/* If priv data len specified, then priv_data cannot be NULL */
4046 	if ((priv_data_len > 0) && (priv_data == NULL))
4047 		return (IBT_INVALID_PARAM);
4048 
4049 	if (priv_data_len > IBT_SIDR_REP_PRIV_DATA_SZ)
4050 		return (IBT_INVALID_PARAM);
4051 
4052 	/* retrieve qpn and qkey from ud channel */
4053 
4054 	/* validate event and statep's state */
4055 
4056 	if (status == IBT_CM_ACCEPT) {
4057 		retval = ibt_query_qp(ud_channel, &qp_attr);
4058 		if ((retval != IBT_SUCCESS) ||
4059 		    (qp_attr.qp_info.qp_trans != IBT_UD_SRV)) {
4060 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_ud_proceed: "
4061 			    "Failed to retrieve QPN from the channel: %d",
4062 			    retval);
4063 			return (IBT_INVALID_PARAM);
4064 		}
4065 	}
4066 
4067 
4068 	mutex_enter(&ud_statep->ud_state_mutex);
4069 
4070 	if (ud_statep->ud_state != IBCM_STATE_SIDR_REQ_RCVD) {
4071 		mutex_exit(&ud_statep->ud_state_mutex);
4072 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_ud_proceed : Invalid State "
4073 		    "specified");
4074 		return (IBT_INVALID_PARAM);
4075 	}
4076 
4077 	/* wait until client's CM handler returns DEFER status back to CM */
4078 
4079 	while (ud_statep->ud_clnt_proceed == IBCM_BLOCK) {
4080 		IBTF_DPRINTF_L5(cmlog, "ibt_cm_ud_proceed : Blocked for return"
4081 		    " of client's ud cm handler");
4082 		cv_wait(&ud_statep->ud_block_client_cv,
4083 		    &ud_statep->ud_state_mutex);
4084 	}
4085 
4086 	if (ud_statep->ud_clnt_proceed == IBCM_FAIL) {
4087 		mutex_exit(&ud_statep->ud_state_mutex);
4088 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_ud_proceed : Failed as client "
4089 		    "returned non-DEFER status from cm handler");
4090 		return (IBT_INVALID_PARAM);
4091 	}
4092 
4093 	ASSERT(ud_statep->ud_clnt_proceed == IBCM_UNBLOCK);
4094 	ud_statep->ud_clnt_proceed = IBCM_FAIL;
4095 	mutex_exit(&ud_statep->ud_state_mutex);
4096 
4097 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*proceed_targs))
4098 
4099 	/* the state machine processing is done in a separate thread */
4100 
4101 	/* proceed_targs is freed in ibcm_proceed_via_taskq */
4102 	proceed_targs = kmem_zalloc(sizeof (ibcm_proceed_targs_t),
4103 	    KM_SLEEP);
4104 
4105 	proceed_targs->status = status;
4106 	proceed_targs->priv_data_len = priv_data_len;
4107 
4108 	bcopy(priv_data, proceed_targs->priv_data, priv_data_len);
4109 
4110 	if (status == IBT_CM_ACCEPT) {
4111 		proceed_targs->tst.ud.ud_qkey =
4112 		    qp_attr.qp_info.qp_transport.ud.ud_qkey;
4113 		proceed_targs->tst.ud.ud_qpn = qp_attr.qp_qpn;
4114 	}
4115 
4116 	proceed_targs->tst.ud.ud_statep = ud_statep;
4117 
4118 	/* copy redirect info based on status */
4119 	if (status == IBT_CM_REDIRECT)
4120 		bcopy(redirect_infop, &proceed_targs->tst.ud.ud_redirect_info,
4121 		    sizeof (ibt_redirect_info_t));
4122 
4123 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*proceed_targs))
4124 
4125 	(void) taskq_dispatch(ibcm_taskq, ibcm_ud_proceed_via_taskq,
4126 	    proceed_targs, TQ_SLEEP);
4127 
4128 	return (IBT_SUCCESS);
4129 }
4130 
4131 /*
4132  * Function:
4133  * 	ibcm_ud_proceed_via_taskq
4134  *
4135  * Called from taskq, dispatched by ibt_cm_ud_proceed
4136  * Completes the cm state processing for ibt_cm_ud_proceed
4137  */
4138 void
4139 ibcm_ud_proceed_via_taskq(void *targs)
4140 {
4141 	ibcm_proceed_targs_t	*proceed_targs = (ibcm_proceed_targs_t *)targs;
4142 	ibcm_ud_state_data_t	*ud_statep = proceed_targs->tst.ud.ud_statep;
4143 	ibcm_ud_clnt_reply_info_t ud_clnt_info;
4144 	ibt_sidr_status_t	sidr_status;
4145 
4146 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_proceed_via_taskq(%p)", targs);
4147 
4148 	ud_clnt_info.ud_qpn  = proceed_targs->tst.ud.ud_qpn;
4149 	ud_clnt_info.ud_qkey  = proceed_targs->tst.ud.ud_qkey;
4150 	ud_clnt_info.priv_data = proceed_targs->priv_data;
4151 	ud_clnt_info.priv_data_len = proceed_targs->priv_data_len;
4152 	ud_clnt_info.redirect_infop = &proceed_targs->tst.ud.ud_redirect_info;
4153 
4154 	/* validate event and statep's state */
4155 	ibcm_process_sidr_req_cm_hdlr(ud_statep, proceed_targs->status,
4156 	    &ud_clnt_info, &sidr_status,
4157 	    (ibcm_sidr_rep_msg_t *)IBCM_OUT_MSGP(ud_statep->ud_stored_msg));
4158 
4159 	ibcm_post_sidr_rep_mad(ud_statep, sidr_status);
4160 
4161 	/* decr the statep ref cnt incremented in ibcm_process_sidr_req_msg */
4162 	mutex_enter(&ud_statep->ud_state_mutex);
4163 	IBCM_UD_REF_CNT_DECR(ud_statep);
4164 	mutex_exit(&ud_statep->ud_state_mutex);
4165 
4166 	kmem_free(targs, sizeof (ibcm_proceed_targs_t));
4167 }
4168 
4169 /*
4170  * Function:
4171  *	ibt_set_alt_path
4172  * Input:
4173  *	channel		Channel handle returned from ibt_alloc_rc_channel(9F).
4174  *
4175  *	mode		Execute in blocking or non blocking mode.
4176  *
4177  *	alt_path	A pointer to an ibt_alt_path_info_t as returned from an
4178  *			ibt_get_alt_path(9F) call that specifies the new
4179  *			alternate path.
4180  *
4181  *	priv_data       A pointer to a buffer specified by caller for the
4182  *			private data in the outgoing CM Load Alternate Path
4183  *			(LAP) message sent to the remote host. This can be NULL
4184  *			if no private data is available to communicate to the
4185  *			remote node.
4186  *
4187  *	priv_data_len   Length of valid data in priv_data, this should be less
4188  *			than or equal to IBT_LAP_PRIV_DATA_SZ.
4189  *
4190  * Output:
4191  *	ret_args	If called in blocking mode, points to a return argument
4192  *			structure of type ibt_ap_returns_t.
4193  *
4194  * Returns:
4195  *	IBT_SUCCESS on Success else appropriate error.
4196  * Description:
4197  *	Load the specified alternate path. Causes the CM to send an LAP message
4198  *	to the remote node.
4199  *	Can only be called on a previously opened RC channel.
4200  */
4201 ibt_status_t
4202 ibt_set_alt_path(ibt_channel_hdl_t channel, ibt_execution_mode_t mode,
4203     ibt_alt_path_info_t *alt_path, void *priv_data,
4204     ibt_priv_data_len_t priv_data_len, ibt_ap_returns_t *ret_args)
4205 {
4206 	ibmf_handle_t		ibmf_hdl;
4207 	ibt_status_t		status = IBT_SUCCESS;
4208 	ibcm_lap_msg_t		*lap_msgp;
4209 	ibcm_hca_info_t		*hcap;
4210 	ibcm_state_data_t	*statep;
4211 	uint8_t			port_no;
4212 	ib_lid_t		alternate_slid;
4213 	ibt_priv_data_len_t	len;
4214 	ib_lid_t		base_lid;
4215 	boolean_t		alt_grh;
4216 
4217 	IBTF_DPRINTF_L3(cmlog, "ibt_set_alt_path(%p, %x, %p, %p, %x, %p)",
4218 	    channel, mode, alt_path, priv_data, priv_data_len, ret_args);
4219 
4220 	/* validate channel */
4221 	if (IBCM_INVALID_CHANNEL(channel)) {
4222 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: invalid channel");
4223 		return (IBT_CHAN_HDL_INVALID);
4224 	}
4225 
4226 	if (ibtl_cm_get_chan_type(channel) != IBT_RC_SRV) {
4227 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4228 		    "Invalid Channel type: Applicable only to RC Channel");
4229 		return (IBT_CHAN_SRV_TYPE_INVALID);
4230 	}
4231 
4232 	if (mode == IBT_NONBLOCKING) {
4233 		if (ret_args != NULL) {
4234 			IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4235 			    "ret_args should be NULL when called in "
4236 			    "non-blocking mode");
4237 			return (IBT_INVALID_PARAM);
4238 		}
4239 	} else if (mode == IBT_BLOCKING) {
4240 		if (ret_args == NULL) {
4241 			IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4242 			    "ret_args should be Non-NULL when called in "
4243 			    "blocking mode");
4244 			return (IBT_INVALID_PARAM);
4245 		}
4246 		if (ret_args->ap_priv_data_len > IBT_APR_PRIV_DATA_SZ) {
4247 			IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4248 			    "expected private data length is too large");
4249 			return (IBT_INVALID_PARAM);
4250 		}
4251 		if ((ret_args->ap_priv_data_len > 0) &&
4252 		    (ret_args->ap_priv_data == NULL)) {
4253 			IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4254 			    "apr_priv_data_len > 0, but apr_priv_data NULL");
4255 			return (IBT_INVALID_PARAM);
4256 		}
4257 	} else { /* any other mode is not valid for ibt_set_alt_path */
4258 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4259 		    "invalid mode %x specified", mode);
4260 		return (IBT_INVALID_PARAM);
4261 	}
4262 
4263 	if ((port_no = alt_path->ap_alt_cep_path.cep_hca_port_num) == 0)
4264 		return (IBT_INVALID_PARAM);
4265 
4266 	/* get the statep */
4267 	IBCM_GET_CHAN_PRIVATE(channel, statep);
4268 	if (statep == NULL) {
4269 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: statep NULL");
4270 		return (IBT_CM_FAILURE);
4271 	}
4272 
4273 	mutex_enter(&statep->state_mutex);
4274 	IBCM_RELEASE_CHAN_PRIVATE(channel);
4275 	IBCM_REF_CNT_INCR(statep);
4276 	mutex_exit(&statep->state_mutex);
4277 
4278 	IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: statep %p", statep);
4279 
4280 	hcap = statep->hcap;
4281 
4282 	/* HCA must have been in active state. If not, it's a client bug */
4283 	if (!IBCM_ACCESS_HCA_OK(hcap))
4284 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: hca in error state");
4285 
4286 	ASSERT(statep->cm_handler != NULL);
4287 
4288 	/* Check Alternate port */
4289 	status = ibt_get_port_state_byguid(hcap->hca_guid, port_no, NULL,
4290 	    &base_lid);
4291 	if (status != IBT_SUCCESS) {
4292 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4293 		    "ibt_get_port_state_byguid status %d ", status);
4294 		mutex_enter(&statep->state_mutex);
4295 		IBCM_REF_CNT_DECR(statep);
4296 		mutex_exit(&statep->state_mutex);
4297 		return (status);
4298 	}
4299 
4300 	if ((hcap->hca_port_info[port_no - 1].port_ibmf_hdl == NULL) &&
4301 	    ((status = ibcm_hca_reinit_port(hcap, port_no - 1))
4302 	    != IBT_SUCCESS)) {
4303 		IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4304 		    "ibmf reg or callback setup failed during re-initialize");
4305 		mutex_enter(&statep->state_mutex);
4306 		IBCM_REF_CNT_DECR(statep);
4307 		mutex_exit(&statep->state_mutex);
4308 		return (status);
4309 	}
4310 
4311 	ibmf_hdl = statep->stored_reply_addr.ibmf_hdl;
4312 
4313 	alternate_slid = base_lid +
4314 	    alt_path->ap_alt_cep_path.cep_adds_vect.av_src_path;
4315 
4316 	IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: alternate SLID = %x",
4317 	    h2b16(alternate_slid));
4318 
4319 	ibcm_lapr_enter();	/* limit how many run simultaneously */
4320 
4321 	/* Allocate MAD for LAP */
4322 	if (statep->lapr_msg == NULL)
4323 		if ((status = ibcm_alloc_out_msg(ibmf_hdl, &statep->lapr_msg,
4324 		    MAD_METHOD_SEND)) != IBT_SUCCESS) {
4325 			ibcm_lapr_exit();
4326 			IBTF_DPRINTF_L2(cmlog, "ibt_set_alt_path: "
4327 			    "chan 0x%p ibcm_alloc_out_msg failed", channel);
4328 			mutex_enter(&statep->state_mutex);
4329 			IBCM_REF_CNT_DECR(statep);
4330 			mutex_exit(&statep->state_mutex);
4331 			return (status);
4332 		}
4333 
4334 	mutex_enter(&statep->state_mutex);
4335 
4336 	IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: connection state is"
4337 	    " %x", statep->state);
4338 
4339 	/* Check state */
4340 	if ((statep->state != IBCM_STATE_ESTABLISHED) ||
4341 	    (statep->ap_state != IBCM_AP_STATE_IDLE)) {
4342 		IBCM_REF_CNT_DECR(statep);
4343 		mutex_exit(&statep->state_mutex);
4344 		(void) ibcm_free_out_msg(ibmf_hdl, &statep->lapr_msg);
4345 		ibcm_lapr_exit();
4346 		return (IBT_CHAN_STATE_INVALID);
4347 	} else {
4348 		/* Set to LAP Sent state */
4349 		statep->ap_state = IBCM_AP_STATE_LAP_SENT;
4350 		statep->ap_done = B_FALSE;
4351 		statep->remaining_retry_cnt = statep->max_cm_retries;
4352 		statep->timer_stored_state = statep->state;
4353 		statep->timer_stored_ap_state = statep->ap_state;
4354 		IBCM_REF_CNT_INCR(statep); /* for ibcm_post_lap_complete */
4355 	}
4356 
4357 	mutex_exit(&statep->state_mutex);
4358 
4359 	/* No more failure returns below */
4360 
4361 	/* Allocate MAD for LAP */
4362 	IBTF_DPRINTF_L5(cmlog, "ibt_set_alt_path:"
4363 	    " statep's mad addr = 0x%p", IBCM_OUT_HDRP(statep->lapr_msg));
4364 
4365 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*lap_msgp))
4366 
4367 	lap_msgp = (ibcm_lap_msg_t *)IBCM_OUT_MSGP(statep->lapr_msg);
4368 
4369 	lap_msgp->lap_alt_l_port_lid = h2b16(alternate_slid);
4370 	lap_msgp->lap_alt_r_port_lid =
4371 	    h2b16(alt_path->ap_alt_cep_path.cep_adds_vect.av_dlid);
4372 
4373 	/* Fill in remote port gid */
4374 	lap_msgp->lap_alt_r_port_gid.gid_prefix =
4375 	    h2b64(alt_path->ap_alt_cep_path.cep_adds_vect.av_dgid.gid_prefix);
4376 	lap_msgp->lap_alt_r_port_gid.gid_guid =
4377 	    h2b64(alt_path->ap_alt_cep_path.cep_adds_vect.av_dgid.gid_guid);
4378 
4379 	/* Fill in local port gid */
4380 	lap_msgp->lap_alt_l_port_gid.gid_prefix =
4381 	    h2b64(alt_path->ap_alt_cep_path.cep_adds_vect.av_sgid.gid_prefix);
4382 	lap_msgp->lap_alt_l_port_gid.gid_guid =
4383 	    h2b64(alt_path->ap_alt_cep_path.cep_adds_vect.av_sgid.gid_guid);
4384 
4385 	alt_grh = alt_path->ap_alt_cep_path.cep_adds_vect.av_send_grh;
4386 
4387 	/* alternate_flow_label, and alternate srate, alternate traffic class */
4388 	lap_msgp->lap_alt_srate_plus =
4389 	    alt_path->ap_alt_cep_path.cep_adds_vect.av_srate & 0x3f;
4390 	lap_msgp->lap_alt_flow_label_plus = h2b32(((alt_grh == B_TRUE) ?
4391 	    (alt_path->ap_alt_cep_path.cep_adds_vect.av_flow << 12) : 0) |
4392 	    alt_path->ap_alt_cep_path.cep_adds_vect.av_tclass);
4393 
4394 	/* Alternate hop limit, service level */
4395 	lap_msgp->lap_alt_hop_limit = (alt_grh == B_TRUE) ?
4396 	    alt_path->ap_alt_cep_path.cep_adds_vect.av_hop : 1;
4397 	lap_msgp->lap_alt_sl_plus =
4398 	    alt_path->ap_alt_cep_path.cep_adds_vect.av_srvl << 4 |
4399 	    ((alt_grh == B_FALSE) ? 0x8 : 0);
4400 
4401 	lap_msgp->lap_alt_local_acktime_plus = ibt_usec2ib(
4402 	    (2 * statep->rc_alt_pkt_lt) +
4403 	    ibt_ib2usec(hcap->hca_ack_delay)) << 3;
4404 
4405 	lap_msgp->lap_local_comm_id = h2b32(statep->local_comid);
4406 	lap_msgp->lap_remote_comm_id = h2b32(statep->remote_comid);
4407 
4408 	lap_msgp->lap_remote_qpn_eecn_plus =
4409 	    h2b32((statep->remote_qpn << 8) |
4410 	    ibt_usec2ib(ibcm_remote_response_time) << 3);
4411 
4412 	len = min(priv_data_len, IBT_LAP_PRIV_DATA_SZ);
4413 	if ((len > 0) && priv_data) {
4414 		bcopy(priv_data, lap_msgp->lap_private_data, len);
4415 	}
4416 
4417 	/* only rc_alt_pkt_lt and ap_return_data fields are initialized */
4418 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*statep))
4419 
4420 	statep->rc_alt_pkt_lt = ibt_ib2usec(alt_path->ap_alt_pkt_lt);
4421 
4422 	/* return_data is filled up in the state machine code */
4423 	statep->ap_return_data = ret_args;
4424 
4425 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*statep))
4426 
4427 	IBCM_OUT_HDRP(statep->lapr_msg)->AttributeID =
4428 	    h2b16(IBCM_INCOMING_LAP + IBCM_ATTR_BASE_ID);
4429 
4430 	IBCM_OUT_HDRP(statep->lapr_msg)->TransactionID =
4431 	    h2b64(ibcm_generate_tranid(IBCM_INCOMING_LAP, statep->local_comid,
4432 	    0));
4433 	IBTF_DPRINTF_L3(cmlog, "ibt_set_alt_path: statep %p, tid %llx",
4434 	    statep, IBCM_OUT_HDRP(statep->lapr_msg)->TransactionID);
4435 
4436 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*lap_msgp))
4437 
4438 	/* Send LAP */
4439 	ibcm_post_rc_mad(statep, statep->lapr_msg, ibcm_post_lap_complete,
4440 	    statep);
4441 
4442 	mutex_enter(&statep->state_mutex);
4443 
4444 	if (mode == IBT_BLOCKING) {
4445 		IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: blocking");
4446 
4447 		/* wait for APR */
4448 		while (statep->ap_done != B_TRUE) {
4449 			cv_wait(&statep->block_client_cv,
4450 			    &statep->state_mutex);
4451 		}
4452 
4453 		IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: done blocking");
4454 
4455 		/*
4456 		 * In the case that ibt_set_alt_path fails,
4457 		 * change retval to IBT_CM_FAILURE
4458 		 */
4459 		if (statep->ap_return_data->ap_status != IBT_CM_AP_LOADED)
4460 			status = IBT_CM_FAILURE;
4461 
4462 	}
4463 
4464 	/* decrement the ref-count before leaving here */
4465 	IBCM_REF_CNT_DECR(statep);
4466 
4467 	mutex_exit(&statep->state_mutex);
4468 
4469 	ibcm_lapr_exit();
4470 
4471 	/* If this message isn't seen then ibt_set_alt_path failed */
4472 	IBTF_DPRINTF_L4(cmlog, "ibt_set_alt_path: done");
4473 
4474 	return (status);
4475 }
4476 
4477 
4478 #ifdef DEBUG
4479 
4480 /*
4481  * ibcm_query_classport_info:
4482  *	Query classportinfo
4483  *
4484  * INPUTS:
4485  *	channel		- Channel that is associated with a statep
4486  *
4487  * RETURN VALUE: NONE
4488  * This function is currently used to generate a valid get method classport
4489  * info, and test CM functionality. There is no ibtl client interface to
4490  * generate a classportinfo. It is possible that CM may use classportinfo
4491  * from other nodes in the future, and most of the code below could be re-used.
4492  */
4493 void
4494 ibcm_query_classport_info(ibt_channel_hdl_t channel)
4495 {
4496 	ibcm_state_data_t	*statep;
4497 	ibmf_msg_t		*msgp;
4498 
4499 	IBTF_DPRINTF_L2(cmlog, "ibcm_query_classport_info(%p)", channel);
4500 
4501 	/* validate channel, first */
4502 	if (IBCM_INVALID_CHANNEL(channel)) {
4503 		IBTF_DPRINTF_L2(cmlog, "ibcm_query_classport_info: "
4504 		    "invalid channel (%p)", channel);
4505 		return;
4506 	}
4507 
4508 	/* get the statep */
4509 	IBCM_GET_CHAN_PRIVATE(channel, statep);
4510 
4511 	/*
4512 	 * This can happen, if the statep is already gone by a DREQ from
4513 	 * the remote side
4514 	 */
4515 	if (statep == NULL) {
4516 		IBTF_DPRINTF_L2(cmlog, "ibcm_query_classport_info: "
4517 		    "statep NULL");
4518 		return;
4519 	}
4520 
4521 	mutex_enter(&statep->state_mutex);
4522 	IBCM_RELEASE_CHAN_PRIVATE(channel);
4523 	IBCM_REF_CNT_INCR(statep);
4524 	mutex_exit(&statep->state_mutex);
4525 
4526 	/* Debug/test code, so don't care about return status */
4527 	(void) ibcm_alloc_out_msg(statep->stored_reply_addr.ibmf_hdl, &msgp,
4528 	    MAD_METHOD_GET);
4529 
4530 	IBCM_OUT_HDRP(msgp)->TransactionID = h2b64(ibcm_generate_tranid(
4531 	    MAD_ATTR_ID_CLASSPORTINFO, statep->local_comid, 0));
4532 	IBCM_OUT_HDRP(msgp)->AttributeID = h2b16(MAD_ATTR_ID_CLASSPORTINFO);
4533 
4534 	(void) ibcm_post_mad(msgp, &statep->stored_reply_addr, NULL, NULL);
4535 
4536 	IBTF_DPRINTF_L2(cmlog, "ibcm_query_classport_info(%p) "
4537 	    "Get method MAD posted ", channel);
4538 
4539 	(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl, &msgp);
4540 
4541 	mutex_enter(&statep->state_mutex);
4542 	IBCM_REF_CNT_DECR(statep);
4543 	mutex_exit(&statep->state_mutex);
4544 }
4545 
4546 static void
4547 ibcm_print_reply_addr(ibt_channel_hdl_t channel, ibcm_mad_addr_t *cm_reply_addr)
4548 {
4549 	IBTF_DPRINTF_L4(cmlog, "ibcm_print_reply_addr: chan 0x%p, SLID %x, "
4550 	    "DLID %x", channel, cm_reply_addr->rcvd_addr.ia_local_lid,
4551 	    cm_reply_addr->rcvd_addr.ia_remote_lid);
4552 
4553 	IBTF_DPRINTF_L4(cmlog, "ibcm_print_reply_addr: QKEY %x, PKEY %x, "
4554 	    "RQPN %x SL %x", cm_reply_addr->rcvd_addr.ia_q_key,
4555 	    cm_reply_addr->rcvd_addr.ia_p_key,
4556 	    cm_reply_addr->rcvd_addr.ia_remote_qno,
4557 	    cm_reply_addr->rcvd_addr.ia_service_level);
4558 
4559 	IBTF_DPRINTF_L4(cmlog, "ibcm_print_reply_addr: CM SGID %llX:%llX ",
4560 	    cm_reply_addr->grh_hdr.ig_sender_gid.gid_prefix,
4561 	    cm_reply_addr->grh_hdr.ig_sender_gid.gid_guid);
4562 
4563 	IBTF_DPRINTF_L4(cmlog, "ibcm_print_reply_addr: CM DGID %llX:%llX",
4564 	    cm_reply_addr->grh_hdr.ig_recver_gid.gid_prefix,
4565 	    cm_reply_addr->grh_hdr.ig_recver_gid.gid_guid);
4566 
4567 	IBTF_DPRINTF_L4(cmlog, "ibcm_print_reply_addr: CM FL %x TC %x HL %x",
4568 	    cm_reply_addr->grh_hdr.ig_flow_label,
4569 	    cm_reply_addr->grh_hdr.ig_tclass,
4570 	    cm_reply_addr->grh_hdr.ig_hop_limit);
4571 }
4572 
4573 #endif
4574 
4575 typedef struct ibcm_join_mcg_tqarg_s {
4576 	ib_gid_t		rgid;
4577 	ibt_mcg_attr_t		mcg_attr;
4578 	ibt_mcg_info_t		*mcg_infop;
4579 	ibt_mcg_handler_t	func;
4580 	void			*arg;
4581 } ibcm_join_mcg_tqarg_t;
4582 
4583 _NOTE(READ_ONLY_DATA(ibcm_join_mcg_tqarg_s))
4584 
4585 /*
4586  * Function:
4587  *	ibt_join_mcg
4588  * Input:
4589  *	rgid		The request GID that defines the HCA port from which a
4590  *			contact to SA Access is performed to add the specified
4591  *			endport GID ((mcg_attr->mc_pgid) to a multicast group.
4592  *			If mcg_attr->mc_pgid is null, then this (rgid) will be
4593  *			treated as endport GID that is to be added to the
4594  *			multicast group.
4595  *
4596  *	mcg_attr	A pointer to an ibt_mcg_attr_t structure that defines
4597  *			the attributes of the desired multicast group to be
4598  *			created or joined.
4599  *
4600  *	func		NULL or a pointer to a function to call when
4601  *			ibt_join_mcg() completes. If 'func' is not NULL then
4602  *			ibt_join_mcg() will return as soon as possible after
4603  *			initiating the multicast group join/create process.
4604  *			'func' is then called when the process completes.
4605  *
4606  *	arg		Argument to the 'func'.
4607  *
4608  * Output:
4609  *	mcg_info_p	A pointer to the ibt_mcg_info_t structure, allocated
4610  *			by the caller, where the attributes of the created or
4611  *			joined multicast group are copied.
4612  * Returns:
4613  *	IBT_SUCCESS
4614  *	IBT_INVALID_PARAM
4615  *	IBT_MCG_RECORDS_NOT_FOUND
4616  *	IBT_INSUFF_RESOURCE
4617  * Description:
4618  *	Join a multicast group.  The first full member "join" causes the MCG
4619  *	to be created.
4620  */
4621 ibt_status_t
4622 ibt_join_mcg(ib_gid_t rgid, ibt_mcg_attr_t *mcg_attr,
4623     ibt_mcg_info_t *mcg_info_p, ibt_mcg_handler_t func, void  *arg)
4624 {
4625 	ibcm_join_mcg_tqarg_t	*mcg_tq;
4626 	int			flag = ((func == NULL) ? KM_SLEEP : KM_NOSLEEP);
4627 
4628 	IBTF_DPRINTF_L3(cmlog, "ibt_join_mcg(%llX:%llX, %p)", rgid.gid_prefix,
4629 	    rgid.gid_guid, mcg_attr);
4630 
4631 	if ((rgid.gid_prefix == 0) || (rgid.gid_guid == 0)) {
4632 		IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: Request GID is required");
4633 		return (IBT_INVALID_PARAM);
4634 	}
4635 
4636 	if ((mcg_attr->mc_pkey == IB_PKEY_INVALID_LIMITED) ||
4637 	    (mcg_attr->mc_pkey == IB_PKEY_INVALID_FULL)) {
4638 		IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: Invalid P_Key specified");
4639 		return (IBT_INVALID_PARAM);
4640 	}
4641 
4642 	if (mcg_attr->mc_join_state == 0) {
4643 		IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: JoinState not specified");
4644 		return (IBT_INVALID_PARAM);
4645 	}
4646 
4647 	if (mcg_info_p == NULL) {
4648 		IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: mcg_info_p is NULL");
4649 		return (IBT_INVALID_PARAM);
4650 	}
4651 
4652 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mcg_tq))
4653 
4654 	mcg_tq = kmem_alloc(sizeof (ibcm_join_mcg_tqarg_t), flag);
4655 	if (mcg_tq == NULL) {
4656 		IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: "
4657 		    "Unable to allocate memory for local usage.");
4658 		return (IBT_INSUFF_KERNEL_RESOURCE);
4659 	}
4660 
4661 	mcg_tq->rgid = rgid;
4662 	bcopy(mcg_attr, &mcg_tq->mcg_attr, sizeof (ibt_mcg_attr_t));
4663 	mcg_tq->mcg_infop = mcg_info_p;
4664 	mcg_tq->func = func;
4665 	mcg_tq->arg = arg;
4666 
4667 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mcg_tq))
4668 
4669 	if (func != NULL) {	/* Non-Blocking */
4670 		IBTF_DPRINTF_L3(cmlog, "ibt_join_mcg: Non-Blocking Call");
4671 		if (taskq_dispatch(ibcm_taskq, ibcm_process_async_join_mcg,
4672 		    mcg_tq, TQ_NOSLEEP) == 0) {
4673 			IBTF_DPRINTF_L2(cmlog, "ibt_join_mcg: Failed to "
4674 			    "Dispatch the TaskQ");
4675 			kmem_free(mcg_tq, sizeof (ibcm_join_mcg_tqarg_t));
4676 			return (IBT_INSUFF_KERNEL_RESOURCE);
4677 		} else
4678 			return (IBT_SUCCESS);
4679 	} else {		/* Blocking */
4680 		return (ibcm_process_join_mcg(mcg_tq));
4681 	}
4682 }
4683 
4684 static void
4685 ibcm_process_async_join_mcg(void *tq_arg)
4686 {
4687 	(void) ibcm_process_join_mcg(tq_arg);
4688 }
4689 
4690 static ibt_status_t
4691 ibcm_process_join_mcg(void *taskq_arg)
4692 {
4693 	sa_mcmember_record_t	mcg_req;
4694 	sa_mcmember_record_t	*mcg_resp;
4695 	ibmf_saa_access_args_t	access_args;
4696 	ibmf_saa_handle_t	saa_handle;
4697 	uint64_t		component_mask = 0;
4698 	ibt_status_t		retval;
4699 	ibtl_cm_hca_port_t	hca_port;
4700 	uint_t			num_records;
4701 	size_t			length;
4702 	ibcm_hca_info_t		*hcap;
4703 	ibcm_join_mcg_tqarg_t	*mcg_arg = (ibcm_join_mcg_tqarg_t *)taskq_arg;
4704 	ibt_mcg_info_t		*mcg_info_p = mcg_arg->mcg_infop;
4705 
4706 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_join_mcg(%p)", mcg_arg);
4707 
4708 	retval = ibtl_cm_get_hca_port(mcg_arg->rgid, 0, &hca_port);
4709 	if (retval != IBT_SUCCESS) {
4710 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_join_mcg: Failed to get "
4711 		    "port info from specified RGID: status = %d", retval);
4712 		goto ibcm_join_mcg_exit1;
4713 	}
4714 
4715 	bzero(&mcg_req, sizeof (sa_mcmember_record_t));
4716 
4717 	if ((mcg_arg->mcg_attr.mc_pgid.gid_prefix == 0) ||
4718 	    (mcg_arg->mcg_attr.mc_pgid.gid_guid == 0)) {
4719 		IBTF_DPRINTF_L3(cmlog, "ibcm_process_join_mcg: "
4720 		    "Request GID is Port GID");
4721 		mcg_req.PortGID = mcg_arg->rgid;
4722 	} else {
4723 		mcg_req.PortGID = mcg_arg->mcg_attr.mc_pgid;
4724 	}
4725 	component_mask |= SA_MC_COMPMASK_PORTGID;
4726 
4727 	mcg_req.Q_Key = mcg_arg->mcg_attr.mc_qkey;
4728 	mcg_req.P_Key = mcg_arg->mcg_attr.mc_pkey;
4729 	mcg_req.JoinState = mcg_arg->mcg_attr.mc_join_state;
4730 	mcg_req.TClass = mcg_arg->mcg_attr.mc_tclass;
4731 	mcg_req.FlowLabel = mcg_arg->mcg_attr.mc_flow;
4732 	mcg_req.SL = mcg_arg->mcg_attr.mc_sl;
4733 
4734 	component_mask |= SA_MC_COMPMASK_QKEY | SA_MC_COMPMASK_PKEY |
4735 	    SA_MC_COMPMASK_JOINSTATE | SA_MC_COMPMASK_TCLASS |
4736 	    SA_MC_COMPMASK_FLOWLABEL | SA_MC_COMPMASK_SL;
4737 
4738 	/* If client has specified MGID, use it else SA will assign one. */
4739 	if ((mcg_arg->mcg_attr.mc_mgid.gid_prefix >> 56ULL & 0xFF) == 0xFF) {
4740 		mcg_req.MGID = mcg_arg->mcg_attr.mc_mgid;
4741 		component_mask |= SA_MC_COMPMASK_MGID;
4742 	}
4743 
4744 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_join_mcg: PGID=%llX:%llX, "
4745 	    "MGID=%llX:%llX", mcg_req.PortGID.gid_prefix,
4746 	    mcg_req.PortGID.gid_guid, mcg_req.MGID.gid_prefix,
4747 	    mcg_req.MGID.gid_guid);
4748 
4749 	/* Is MTU specified. */
4750 	if (mcg_arg->mcg_attr.mc_mtu_req.r_mtu) {
4751 		mcg_req.MTU = mcg_arg->mcg_attr.mc_mtu_req.r_mtu;
4752 		mcg_req.MTUSelector = mcg_arg->mcg_attr.mc_mtu_req.r_selector;
4753 
4754 		component_mask |= SA_MC_COMPMASK_MTUSELECTOR |
4755 		    SA_MC_COMPMASK_MTU;
4756 	}
4757 
4758 	/* Is RATE specified. */
4759 	if (mcg_arg->mcg_attr.mc_rate_req.r_srate) {
4760 		mcg_req.Rate = mcg_arg->mcg_attr.mc_rate_req.r_srate;
4761 		mcg_req.RateSelector =
4762 		    mcg_arg->mcg_attr.mc_rate_req.r_selector;
4763 
4764 		component_mask |= SA_MC_COMPMASK_RATESELECTOR |
4765 		    SA_MC_COMPMASK_RATE;
4766 	}
4767 
4768 	/* Is Packet Life Time specified. */
4769 	if (mcg_arg->mcg_attr.mc_pkt_lt_req.p_pkt_lt) {
4770 		mcg_req.Rate = mcg_arg->mcg_attr.mc_pkt_lt_req.p_pkt_lt;
4771 		mcg_req.RateSelector =
4772 		    mcg_arg->mcg_attr.mc_pkt_lt_req.p_selector;
4773 
4774 		component_mask |= SA_MC_COMPMASK_PKTLTSELECTOR |
4775 		    SA_MC_COMPMASK_PKTLT;
4776 	}
4777 
4778 	if (mcg_arg->mcg_attr.mc_hop) {
4779 		mcg_req.HopLimit = mcg_arg->mcg_attr.mc_hop;
4780 		component_mask |= SA_MC_COMPMASK_HOPLIMIT;
4781 	}
4782 
4783 	if (mcg_arg->mcg_attr.mc_scope) {
4784 		mcg_req.Scope = mcg_arg->mcg_attr.mc_scope;
4785 		component_mask |= SA_MC_COMPMASK_SCOPE;
4786 	}
4787 
4788 	if (mcg_arg->mcg_attr.mc_mlid) {
4789 		mcg_req.MLID = mcg_arg->mcg_attr.mc_mlid;
4790 		component_mask |= SA_MC_COMPMASK_MLID;
4791 	}
4792 
4793 	/* Get SA Access Handle. */
4794 	hcap = ibcm_find_hca_entry(hca_port.hp_hca_guid);
4795 	if (hcap == NULL) {
4796 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_join_mcg: NO HCA found");
4797 
4798 		retval = IBT_HCA_BUSY_DETACHING;
4799 		goto ibcm_join_mcg_exit1;
4800 	}
4801 
4802 	saa_handle = ibcm_get_saa_handle(hcap, hca_port.hp_port);
4803 	if (saa_handle == NULL) {
4804 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_join_mcg: SA Handle NULL");
4805 
4806 		retval = IBT_HCA_PORT_NOT_ACTIVE;
4807 		goto ibcm_join_mcg_exit;
4808 	}
4809 
4810 	if ((mcg_arg->mcg_attr.mc_pgid.gid_prefix != 0) &&
4811 	    (mcg_arg->mcg_attr.mc_pgid.gid_guid != 0)) {
4812 		retval = ibtl_cm_get_hca_port(mcg_arg->mcg_attr.mc_pgid, 0,
4813 		    &hca_port);
4814 		if (retval != IBT_SUCCESS) {
4815 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_join_mcg: Failed "
4816 			    "to get PortInfo of specified PGID: status = %d",
4817 			    retval);
4818 			goto ibcm_join_mcg_exit1;
4819 		}
4820 	}
4821 
4822 	/* Contact SA Access */
4823 	access_args.sq_attr_id = SA_MCMEMBERRECORD_ATTRID;
4824 	access_args.sq_access_type = IBMF_SAA_UPDATE;
4825 	access_args.sq_component_mask = component_mask;
4826 	access_args.sq_template = &mcg_req;
4827 	access_args.sq_template_length = sizeof (sa_mcmember_record_t);
4828 	access_args.sq_callback = NULL;
4829 	access_args.sq_callback_arg = NULL;
4830 
4831 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
4832 	    (void **)&mcg_resp);
4833 	if (retval != IBT_SUCCESS) {
4834 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_join_mcg: "
4835 		    "SA Access Failed");
4836 		goto ibcm_join_mcg_exit;
4837 	}
4838 
4839 	num_records = length/sizeof (sa_mcmember_record_t);
4840 
4841 	IBTF_DPRINTF_L4(cmlog, "ibcm_process_join_mcg: "
4842 	    "Found %d MCMember Records", num_records);
4843 
4844 	/* Validate the returned number of records. */
4845 	if ((mcg_resp != NULL) && (num_records > 0)) {
4846 		/* Update the return values. */
4847 		mcg_info_p->mc_adds_vect.av_dgid = mcg_resp->MGID;
4848 		mcg_info_p->mc_adds_vect.av_sgid = mcg_resp->PortGID;
4849 		mcg_info_p->mc_adds_vect.av_srate = mcg_resp->Rate;
4850 		mcg_info_p->mc_adds_vect.av_srvl = mcg_resp->SL;
4851 		mcg_info_p->mc_adds_vect.av_flow = mcg_resp->FlowLabel;
4852 		mcg_info_p->mc_adds_vect.av_tclass = mcg_resp->TClass;
4853 		mcg_info_p->mc_adds_vect.av_hop = mcg_resp->HopLimit;
4854 		mcg_info_p->mc_adds_vect.av_send_grh = B_TRUE;
4855 		mcg_info_p->mc_adds_vect.av_dlid = mcg_resp->MLID;
4856 		mcg_info_p->mc_mtu = mcg_resp->MTU;
4857 		mcg_info_p->mc_qkey = mcg_resp->Q_Key;
4858 
4859 		retval = ibt_pkey2index_byguid(hca_port.hp_hca_guid,
4860 		    hca_port.hp_port, mcg_resp->P_Key, &mcg_info_p->mc_pkey_ix);
4861 		if (retval != IBT_SUCCESS) {
4862 			IBTF_DPRINTF_L3(cmlog, "ibcm_process_join_mcg: "
4863 			    "Pkey2Index Conversion failed<%d>", retval);
4864 			mcg_info_p->mc_pkey_ix = 0;
4865 		}
4866 
4867 		mcg_info_p->mc_scope = mcg_resp->Scope;
4868 		mcg_info_p->mc_pkt_lt = mcg_resp->PacketLifeTime;
4869 
4870 		mcg_info_p->mc_adds_vect.av_port_num = hca_port.hp_port;
4871 		mcg_info_p->mc_adds_vect.av_sgid_ix = hca_port.hp_sgid_ix;
4872 		mcg_info_p->mc_adds_vect.av_src_path = 0;
4873 
4874 		/* Deallocate the memory allocated by SA for mcg_resp. */
4875 		kmem_free(mcg_resp, length);
4876 		retval = IBT_SUCCESS;
4877 	} else {
4878 		retval = IBT_MCG_RECORDS_NOT_FOUND;
4879 		IBTF_DPRINTF_L3(cmlog, "ibcm_process_join_mcg: "
4880 		    "MCG RECORDS NOT FOUND");
4881 	}
4882 
4883 ibcm_join_mcg_exit:
4884 	ibcm_dec_hca_acc_cnt(hcap);
4885 
4886 ibcm_join_mcg_exit1:
4887 	if (mcg_arg->func)
4888 		(*(mcg_arg->func))(mcg_arg->arg, retval, mcg_info_p);
4889 
4890 	kmem_free(mcg_arg, sizeof (ibcm_join_mcg_tqarg_t));
4891 
4892 	return (retval);
4893 }
4894 
4895 
4896 /*
4897  * Function:
4898  *	ibt_leave_mcg
4899  * Input:
4900  *	rgid		The request GID that defines the HCA port upon which
4901  *			to send the request to the Subnet Administrator, to
4902  *			remove the specified port (port_gid) from the multicast
4903  *			group.  If 'port_gid' is the Reserved GID (i.e.
4904  *			port_gid.gid_prefix = 0 and port_gid.gid_guid = 0),
4905  *			then the end-port associated with 'rgid' is removed
4906  *			from the multicast group.
4907  *
4908  *	mc_gid		A multicast group GID as returned from ibt_join_mcg()
4909  *			call.  This is optional, if not specified (i.e.
4910  *			mc_gid.gid_prefix has 0xFF in its upper 8 bits to
4911  *			identify this as being a multicast GID), then the
4912  *			port is removed from all the multicast groups of
4913  *			which it is a member.
4914  *
4915  *	port_gid	This is optional, if not the Reserved GID (gid_prefix
4916  *			and gid_guid not equal to 0), then this specifies the
4917  *			endport GID of the multicast group member being deleted
4918  *			from the group. If it is the Reserved GID (gid_prefix
4919  *			and gid_guid equal to 0) then the member endport GID is
4920  *			determined from 'rgid'.
4921  *
4922  *	mc_join_state	The Join State attribute used when the group was joined
4923  *			using ibt_join_mcg(). This Join State component must
4924  *			contains at least one bit set to 1 in the same position
4925  *			as that used during ibt_join_mcg(). i.e. the logical
4926  *			AND of the two JoinState components is not all zeros.
4927  *			This Join State component must not have some bits set
4928  *			which are not set using ibt_join_mcg().
4929  * Output:
4930  *	None.
4931  * Returns:
4932  *	IBT_SUCCESS
4933  *	IBT_INVALID_PARAM
4934  *	IBT_MC_GROUP_INVALID
4935  *	IBT_INSUFF_RESOURCE
4936  * Description:
4937  *	The port associated with the port GID shall be removed from the
4938  *	multicast group specified by MGID (mc_gid) or from all the multicast
4939  *	groups of which it is a member if the MGID (mc_gid) is not specified.
4940  *
4941  *	The last full member to leave causes the destruction of the Multicast
4942  *	Group.
4943  */
4944 ibt_status_t
4945 ibt_leave_mcg(ib_gid_t rgid, ib_gid_t mc_gid, ib_gid_t port_gid,
4946     uint8_t mc_join_state)
4947 {
4948 	sa_mcmember_record_t	mcg_req;
4949 	ibmf_saa_access_args_t	access_args;
4950 	ibmf_saa_handle_t	saa_handle;
4951 	uint64_t		component_mask = 0;
4952 	int			sa_retval;
4953 	ibt_status_t		retval;
4954 	ibtl_cm_hca_port_t	hca_port;
4955 	size_t			length;
4956 	void			*results_p;
4957 	ibcm_hca_info_t		*hcap;
4958 
4959 	IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg(%llX:%llX, %llX:%llX)",
4960 	    rgid.gid_prefix, rgid.gid_guid, mc_gid.gid_prefix, mc_gid.gid_guid);
4961 
4962 	IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg(%llX:%llX, 0x%X)",
4963 	    port_gid.gid_prefix, port_gid.gid_guid, mc_join_state);
4964 
4965 	if ((rgid.gid_prefix == 0) || (rgid.gid_guid == 0)) {
4966 		IBTF_DPRINTF_L2(cmlog, "ibt_leave_mcg: RequestGID is required");
4967 		return (IBT_INVALID_PARAM);
4968 	}
4969 
4970 	bzero(&mcg_req, sizeof (sa_mcmember_record_t));
4971 
4972 	IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg: MGID: %llX%llX",
4973 	    mc_gid.gid_prefix, mc_gid.gid_guid);
4974 
4975 	/* Validate MGID */
4976 	if ((mc_gid.gid_prefix >> 56ULL & 0xFF) == 0xFF) {
4977 		mcg_req.MGID = mc_gid;
4978 		component_mask |= SA_MC_COMPMASK_MGID;
4979 	} else if ((mc_gid.gid_prefix != 0) || (mc_gid.gid_guid != 0)) {
4980 		IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg: Invalid MGID specified");
4981 		return (IBT_MC_MGID_INVALID);
4982 	}
4983 
4984 	if ((port_gid.gid_prefix == 0) || (port_gid.gid_guid == 0)) {
4985 		mcg_req.PortGID = rgid;
4986 	} else {
4987 		IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg: Performing PROXY Leave");
4988 		mcg_req.PortGID = port_gid;
4989 	}
4990 	component_mask |= SA_MC_COMPMASK_PORTGID;
4991 
4992 	IBTF_DPRINTF_L3(cmlog, "ibt_leave_mcg: Port GID <%llX:%llX>",
4993 	    mcg_req.PortGID.gid_prefix, mcg_req.PortGID.gid_guid);
4994 
4995 	/* Join State */
4996 	mcg_req.JoinState = mc_join_state;
4997 	component_mask |= SA_MC_COMPMASK_JOINSTATE;
4998 
4999 	retval = ibtl_cm_get_hca_port(rgid, 0, &hca_port);
5000 	if (retval != IBT_SUCCESS) {
5001 		IBTF_DPRINTF_L2(cmlog, "ibt_leave_mcg: Failed to get port info "
5002 		    "from specified RGID : status = %d", retval);
5003 		return (retval);
5004 	}
5005 
5006 	/* Get SA Access Handle. */
5007 	hcap = ibcm_find_hca_entry(hca_port.hp_hca_guid);
5008 	if (hcap == NULL) {
5009 		IBTF_DPRINTF_L2(cmlog, "ibt_leave_mcg: "
5010 		    "NO HCA found");
5011 		return (IBT_HCA_BUSY_DETACHING);
5012 	}
5013 
5014 	saa_handle = ibcm_get_saa_handle(hcap, hca_port.hp_port);
5015 	if (saa_handle == NULL) {
5016 		IBTF_DPRINTF_L2(cmlog, "ibt_leave_mcg: saa_handle is NULL");
5017 		ibcm_dec_hca_acc_cnt(hcap);
5018 		return (IBT_HCA_PORT_NOT_ACTIVE);
5019 	}
5020 
5021 	/* Contact SA Access */
5022 	access_args.sq_attr_id = SA_MCMEMBERRECORD_ATTRID;
5023 	access_args.sq_access_type = IBMF_SAA_DELETE;
5024 	access_args.sq_component_mask = component_mask;
5025 	access_args.sq_template = &mcg_req;
5026 	access_args.sq_template_length = sizeof (sa_mcmember_record_t);
5027 	access_args.sq_callback = NULL;
5028 	access_args.sq_callback_arg = NULL;
5029 
5030 	ibcm_sa_access_enter();
5031 
5032 	sa_retval = ibmf_sa_access(saa_handle, &access_args, 0, &length,
5033 	    &results_p);
5034 	if (sa_retval != IBMF_SUCCESS) {
5035 		IBTF_DPRINTF_L2(cmlog, "ibt_leave_mcg: SA access Failed: %d",
5036 		    sa_retval);
5037 		(void) ibcm_ibmf_analyze_error(sa_retval);
5038 		retval = IBT_MC_GROUP_INVALID;
5039 	}
5040 
5041 	ibcm_sa_access_exit();
5042 
5043 	ibcm_dec_hca_acc_cnt(hcap);
5044 
5045 	return (retval);
5046 }
5047 
5048 
5049 /*
5050  * Function:
5051  *	ibt_query_mcg
5052  * Input:
5053  *	rgid		The request GID that defines the HCA port upon which
5054  *			to send the request to the Subnet Administrator, to
5055  *			retrieve Multicast Records matching attributes as
5056  *			specified through 'mcg_attr' argument.
5057  *
5058  *	mcg_attr	NULL or a pointer to an ibt_mcg_attr_t structure that
5059  *			specifies MCG attributes that are to be matched.
5060  *			Attributes that are not required can be wild carded
5061  *			by specifying as '0'.
5062  *
5063  *	mcgs_max_num	The maximum number of matching multicast groups to
5064  *			return.  If zero, then all available matching multicast
5065  *			groups are returned.
5066  * Output:
5067  *	mcgs_info_p	The address of an ibt_mcg_info_t pointer, where
5068  *			multicast group information is returned. The actual
5069  *			number of entries filled in the array is returned in
5070  *			entries_p.
5071  *
5072  *	entries_p	The number of ibt_mcg_attr_t entries returned.
5073  * Returns:
5074  *	IBT_SUCCESS
5075  *	IBT_INVALID_PARAM
5076  *	IBT_MCG_RECORDS_NOT_FOUND
5077  * Description:
5078  *	Request information on multicast groups that match the parameters
5079  *	specified in mcg_attr. Information on each multicast group is returned
5080  *	to the caller in the form of an array of ibt_mcg_info_t.
5081  *	ibt_query_mcg() allocates the memory for this array and returns a
5082  *	pointer to the array (mcgs_p) and the number of entries in the array
5083  *	(entries_p). This memory should be freed by the client using
5084  *	ibt_free_mcg_info().
5085  */
5086 ibt_status_t
5087 ibt_query_mcg(ib_gid_t rgid, ibt_mcg_attr_t *mcg_attr, uint_t mcgs_max_num,
5088     ibt_mcg_info_t **mcgs_info_p, uint_t *entries_p)
5089 {
5090 	sa_mcmember_record_t	mcg_req;
5091 	sa_mcmember_record_t	*mcg_resp;
5092 	ibt_mcg_info_t		*mcg_infop;
5093 	ibmf_saa_access_args_t	access_args;
5094 	ibmf_saa_handle_t	saa_handle;
5095 	uint64_t		component_mask = 0;
5096 	ibt_status_t		retval;
5097 	ibtl_cm_hca_port_t	hport;
5098 	uint_t			num_records;
5099 	size_t			length;
5100 	void			*results_p;
5101 	ib_gid_t		port_gid;
5102 	ibcm_hca_info_t		*hcap;
5103 
5104 	IBTF_DPRINTF_L3(cmlog, "ibt_query_mcg(%p, %d)", mcg_attr, mcgs_max_num);
5105 
5106 	if ((entries_p == NULL) || (mcgs_info_p == NULL)) {
5107 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: "
5108 		    "entries_p or mcgs_info_p is NULL");
5109 		return (IBT_INVALID_PARAM);
5110 	}
5111 
5112 	if ((rgid.gid_prefix == 0) || (rgid.gid_guid == 0)) {
5113 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: RequestGID is required");
5114 		return (IBT_INVALID_PARAM);
5115 	}
5116 	IBTF_DPRINTF_L4(cmlog, "ibt_query_mcg: Request GID <%llX:%llX>",
5117 	    rgid.gid_prefix, rgid.gid_guid);
5118 
5119 	bzero(&mcg_req, sizeof (sa_mcmember_record_t));
5120 	port_gid.gid_prefix = port_gid.gid_guid = 0;
5121 
5122 	if (mcg_attr != NULL) {
5123 		port_gid = mcg_attr->mc_pgid;
5124 
5125 		if ((port_gid.gid_prefix != 0) && (port_gid.gid_guid != 0)) {
5126 			mcg_req.PortGID = mcg_attr->mc_pgid;
5127 			component_mask |= SA_MC_COMPMASK_PORTGID;
5128 
5129 			IBTF_DPRINTF_L4(cmlog, "ibt_query_mcg: PGID %llX:%llX",
5130 			    port_gid.gid_prefix, port_gid.gid_guid);
5131 		}
5132 
5133 		/* Is Q_Key specified. */
5134 		if (mcg_attr->mc_qkey != 0) {
5135 			mcg_req.Q_Key = mcg_attr->mc_qkey;
5136 			component_mask |= SA_MC_COMPMASK_QKEY;
5137 		}
5138 
5139 		/* Is P_Key specified. */
5140 		if (mcg_attr->mc_pkey != 0) {
5141 			mcg_req.P_Key = mcg_attr->mc_pkey;
5142 			component_mask |= SA_MC_COMPMASK_PKEY;
5143 		}
5144 
5145 		/* Is MGID specified. */
5146 		if ((mcg_attr->mc_mgid.gid_prefix >> 56ULL & 0xFF) == 0xFF) {
5147 			mcg_req.MGID = mcg_attr->mc_mgid;
5148 			component_mask |= SA_MC_COMPMASK_MGID;
5149 		}
5150 
5151 		/* Is MTU specified. */
5152 		if (mcg_attr->mc_mtu_req.r_mtu) {
5153 			mcg_req.MTU = mcg_attr->mc_mtu_req.r_mtu;
5154 			mcg_req.MTUSelector = mcg_attr->mc_mtu_req.r_selector;
5155 
5156 			component_mask |= SA_MC_COMPMASK_MTUSELECTOR |
5157 			    SA_MC_COMPMASK_MTU;
5158 		}
5159 
5160 		if (mcg_attr->mc_tclass) {
5161 			mcg_req.TClass = mcg_attr->mc_tclass;
5162 			component_mask |= SA_MC_COMPMASK_TCLASS;
5163 		}
5164 
5165 		/* Is RATE specified. */
5166 		if (mcg_attr->mc_rate_req.r_srate) {
5167 			mcg_req.Rate = mcg_attr->mc_rate_req.r_srate;
5168 			mcg_req.RateSelector = mcg_attr->mc_rate_req.r_selector;
5169 
5170 			component_mask |= SA_MC_COMPMASK_RATESELECTOR |
5171 			    SA_MC_COMPMASK_RATE;
5172 		}
5173 
5174 		/* Is Packet Life Time specified. */
5175 		if (mcg_attr->mc_pkt_lt_req.p_pkt_lt) {
5176 			mcg_req.Rate = mcg_attr->mc_pkt_lt_req.p_pkt_lt;
5177 			mcg_req.RateSelector =
5178 			    mcg_attr->mc_pkt_lt_req.p_selector;
5179 
5180 			component_mask |= SA_MC_COMPMASK_PKTLTSELECTOR |
5181 			    SA_MC_COMPMASK_PKTLT;
5182 		}
5183 
5184 		if (mcg_attr->mc_hop) {
5185 			mcg_req.HopLimit = mcg_attr->mc_hop;
5186 			component_mask |= SA_MC_COMPMASK_HOPLIMIT;
5187 		}
5188 
5189 		if (mcg_attr->mc_flow) {
5190 			mcg_req.FlowLabel = mcg_attr->mc_flow;
5191 			component_mask |= SA_MC_COMPMASK_FLOWLABEL;
5192 		}
5193 
5194 		if (mcg_attr->mc_sl) {
5195 			mcg_req.SL = mcg_attr->mc_sl;
5196 			component_mask |= SA_MC_COMPMASK_SL;
5197 		}
5198 
5199 		if (mcg_attr->mc_scope) {
5200 			mcg_req.Scope = mcg_attr->mc_scope;
5201 			component_mask |= SA_MC_COMPMASK_SCOPE;
5202 		}
5203 
5204 		if (mcg_attr->mc_join_state) {
5205 			mcg_req.JoinState = mcg_attr->mc_join_state;
5206 			component_mask |= SA_MC_COMPMASK_JOINSTATE;
5207 		}
5208 
5209 		if (mcg_attr->mc_mlid) {
5210 			mcg_req.MLID = mcg_attr->mc_mlid;
5211 			component_mask |= SA_MC_COMPMASK_MLID;
5212 		}
5213 	}
5214 
5215 	retval = ibtl_cm_get_hca_port(rgid, 0, &hport);
5216 	if (retval != IBT_SUCCESS) {
5217 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: Failed to get port info "
5218 		    "from specified RGID : status = %d", retval);
5219 		return (retval);
5220 	}
5221 
5222 	/* Get SA Access Handle. */
5223 	hcap = ibcm_find_hca_entry(hport.hp_hca_guid);
5224 	if (hcap == NULL) {
5225 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: NO HCA found");
5226 		return (IBT_HCA_BUSY_DETACHING);
5227 	}
5228 
5229 	saa_handle = ibcm_get_saa_handle(hcap, hport.hp_port);
5230 	if (saa_handle == NULL) {
5231 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: saa_handle is NULL");
5232 		ibcm_dec_hca_acc_cnt(hcap);
5233 		return (IBT_HCA_PORT_NOT_ACTIVE);
5234 	}
5235 
5236 	/* Contact SA Access */
5237 	access_args.sq_attr_id = SA_MCMEMBERRECORD_ATTRID;
5238 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
5239 	access_args.sq_component_mask = component_mask;
5240 	access_args.sq_template = &mcg_req;
5241 	access_args.sq_template_length = sizeof (sa_mcmember_record_t);
5242 	access_args.sq_callback = NULL;
5243 	access_args.sq_callback_arg = NULL;
5244 
5245 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
5246 	    &results_p);
5247 	if (retval != IBT_SUCCESS) {
5248 		IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: SA access Failed");
5249 		ibcm_dec_hca_acc_cnt(hcap);
5250 		return (retval);
5251 	}
5252 
5253 	num_records = length/sizeof (sa_mcmember_record_t);
5254 
5255 	IBTF_DPRINTF_L4(cmlog, "ibt_query_mcg: Found %d MCMember Records",
5256 	    num_records);
5257 
5258 	/* Validate the returned number of records. */
5259 	if ((results_p != NULL) && (num_records > 0)) {
5260 		uint_t	i;
5261 
5262 		/*
5263 		 * If mcgs_max_num is zero, then return all records else
5264 		 * return only requested number of records
5265 		 */
5266 		if ((mcgs_max_num != 0) && (num_records > mcgs_max_num)) {
5267 			/* we are interested in only mcgs_max_num records */
5268 			num_records = mcgs_max_num;
5269 		}
5270 
5271 		/*
5272 		 * The SGID returned in "mcg_info_p" buffer should be PortGID,
5273 		 * (mcg_attr->mc_pgid), if 'mcg_attr->mc_pgid' was specified,
5274 		 * else RequestGID (rgid) should be returned.
5275 		 */
5276 		if ((port_gid.gid_prefix != 0) && (port_gid.gid_guid != 0)) {
5277 
5278 			/* Get sgid_ix and port number of 'port_gid' */
5279 			retval = ibtl_cm_get_hca_port(port_gid, 0, &hport);
5280 			if (retval != IBT_SUCCESS) {
5281 				IBTF_DPRINTF_L2(cmlog, "ibt_query_mcg: "
5282 				    "Failed to Get Portinfo for PortGID :"
5283 				    "status = %d", retval);
5284 				return (retval);
5285 			}
5286 		} else {
5287 			/*
5288 			 * The sgid_ix and port number related to RequestGID
5289 			 * are already obtained at the beginning.
5290 			 */
5291 			port_gid = rgid;
5292 		}
5293 
5294 		/*
5295 		 * Allocate memory for return buffer, to be freed in
5296 		 * ibt_free_mcg_info().
5297 		 */
5298 		mcg_infop = kmem_alloc((num_records * sizeof (ibt_mcg_info_t)),
5299 		    KM_SLEEP);
5300 
5301 		*mcgs_info_p = mcg_infop;
5302 		*entries_p = num_records;
5303 
5304 		/* Update the return values. */
5305 		for (i = 0; i < num_records; i++) {
5306 
5307 			mcg_resp = (sa_mcmember_record_t *)((uchar_t *)
5308 			    results_p + i * sizeof (sa_mcmember_record_t));
5309 
5310 			mcg_infop[i].mc_adds_vect.av_dgid = mcg_resp->MGID;
5311 			mcg_infop[i].mc_adds_vect.av_sgid = port_gid;
5312 			mcg_infop[i].mc_adds_vect.av_srate = mcg_resp->Rate;
5313 			mcg_infop[i].mc_adds_vect.av_srvl = mcg_resp->SL;
5314 			mcg_infop[i].mc_adds_vect.av_flow = mcg_resp->FlowLabel;
5315 			mcg_infop[i].mc_adds_vect.av_tclass = mcg_resp->TClass;
5316 			mcg_infop[i].mc_adds_vect.av_hop = mcg_resp->HopLimit;
5317 			mcg_infop[i].mc_adds_vect.av_port_num = hport.hp_port;
5318 			mcg_infop[i].mc_adds_vect.av_send_grh = B_TRUE;
5319 			mcg_infop[i].mc_adds_vect.av_dlid = mcg_resp->MLID;
5320 			mcg_infop[i].mc_adds_vect.av_sgid_ix = hport.hp_sgid_ix;
5321 			mcg_infop[i].mc_adds_vect.av_src_path = 0;
5322 			mcg_infop[i].mc_mtu = mcg_resp->MTU;
5323 			mcg_infop[i].mc_qkey = mcg_resp->Q_Key;
5324 			mcg_infop[i].mc_scope = mcg_resp->Scope;
5325 			mcg_infop[i].mc_pkt_lt = mcg_resp->PacketLifeTime;
5326 
5327 			if (ibt_pkey2index_byguid(hport.hp_hca_guid,
5328 			    hport.hp_port, mcg_resp->P_Key,
5329 			    &mcg_infop[i].mc_pkey_ix) != IBT_SUCCESS) {
5330 				IBTF_DPRINTF_L3(cmlog, "ibt_query_mcg: "
5331 				    "Pkey2Index Conversion failed");
5332 				mcg_infop[i].mc_pkey_ix = 0;
5333 			}
5334 		}
5335 
5336 		/*
5337 		 * Deallocate the memory allocated by SA for results_p.
5338 		 */
5339 		kmem_free(results_p, length);
5340 		retval = IBT_SUCCESS;
5341 
5342 		IBTF_DPRINTF_L3(cmlog, "ibt_query_mcg: returning %d MCGRecords",
5343 		    num_records);
5344 
5345 	} else {
5346 		retval = IBT_MCG_RECORDS_NOT_FOUND;
5347 		*entries_p = 0;
5348 
5349 		IBTF_DPRINTF_L3(cmlog, "ibt_query_mcg: MCG RECORDS NOT FOUND");
5350 	}
5351 
5352 	ibcm_dec_hca_acc_cnt(hcap);
5353 
5354 	return (retval);
5355 }
5356 
5357 
5358 /*
5359  * ibt_free_mcg_info()
5360  *	Free the memory allocated by successful ibt_query_mcg()
5361  *
5362  *	mcgs_info	Pointer returned by ibt_query_mcg().
5363  *
5364  *	entries		The number of ibt_mcg_info_t entries to free.
5365  */
5366 void
5367 ibt_free_mcg_info(ibt_mcg_info_t *mcgs_info, uint_t entries)
5368 {
5369 	IBTF_DPRINTF_L3(cmlog, "ibt_free_mcg_info: "
5370 	    "Free <%d> entries from 0x%p", entries, mcgs_info);
5371 
5372 	if ((mcgs_info != NULL) && (entries > 0))
5373 		kmem_free(mcgs_info, entries * sizeof (ibt_mcg_info_t));
5374 	else
5375 		IBTF_DPRINTF_L2(cmlog, "ibt_free_mcg_info: "
5376 		    "ERROR: NULL buf pointer or length specified.");
5377 }
5378 
5379 
5380 /*
5381  * Function:
5382  *	ibt_gid_to_node_info()
5383  * Input:
5384  *	gid		Identifies the IB Node and port for which to obtain
5385  *			Node information.
5386  * Output:
5387  *	node_info_p	A pointer to an ibt_node_info_t structure (allocated
5388  *			by the caller) in which to return the node information.
5389  * Returns:
5390  *	IBT_SUCCESS
5391  *	IBT_INVALID_PARAM
5392  *	IBT_NODE_RECORDS_NOT_FOUND
5393  *	IBT_NO_HCAS_AVAILABLE
5394  * Description:
5395  *	Retrieve Node Information for the specified GID.
5396  */
5397 ibt_status_t
5398 ibt_gid_to_node_info(ib_gid_t gid, ibt_node_info_t *node_info_p)
5399 {
5400 	sa_node_record_t	nr_req, *nr_resp;
5401 	ibmf_saa_handle_t	saa_handle;
5402 	ibt_status_t		retval;
5403 	ibcm_hca_info_t		*hcap;
5404 	ibtl_cm_hca_port_t	hport;
5405 	int			i, j;
5406 	uint_t			num_rec;
5407 	ib_guid_t		*guid_array = NULL;
5408 	sa_path_record_t	*path;
5409 	size_t			len;
5410 	uint8_t			npaths;
5411 	uint32_t		num_hcas = 0;
5412 	ib_lid_t		node_lid;
5413 	boolean_t		local_node = B_FALSE;
5414 	void			*res_p;
5415 	uint8_t			num_ports = 0;
5416 
5417 
5418 	IBTF_DPRINTF_L4(cmlog, "ibt_gid_to_node_info(%llX:%llX, %p)",
5419 	    gid.gid_prefix, gid.gid_guid, node_info_p);
5420 
5421 	if ((gid.gid_prefix == 0) || (gid.gid_guid == 0)) {
5422 		IBTF_DPRINTF_L2(cmlog, "ibt_gid_to_node_info: GID is required");
5423 		return (IBT_INVALID_PARAM);
5424 	}
5425 
5426 	if (node_info_p == NULL) {
5427 		IBTF_DPRINTF_L2(cmlog, "ibt_gid_to_node_info: "
5428 		    "Return Buf (node_info_p) is NULL.");
5429 		return (IBT_INVALID_PARAM);
5430 	}
5431 
5432 	/*
5433 	 * If 'gid' is on local node, then get node lid (i.e. base lid of the
5434 	 * associated port) info via ibtl_cm_get_hca_port() call.
5435 	 */
5436 	bzero(&hport, sizeof (ibtl_cm_hca_port_t));
5437 	if (ibtl_cm_get_hca_port(gid, 0, &hport) == IBT_SUCCESS) {
5438 
5439 		hcap = ibcm_find_hca_entry(hport.hp_hca_guid);
5440 		if (hcap == NULL) {
5441 			IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: "
5442 			    "HCA(%llX) info not found", hport.hp_hca_guid);
5443 			return (IBT_NO_HCAS_AVAILABLE);
5444 		}
5445 		num_ports = 1;
5446 		num_hcas = 1;
5447 		node_lid = hport.hp_base_lid;
5448 		local_node = B_TRUE;
5449 		IBTF_DPRINTF_L4(cmlog, "ibt_gid_to_node_info: Local Node: "
5450 		    "LID = 0x%X", node_lid);
5451 	} else {
5452 		/* Get the number of HCAs and their GUIDs */
5453 		num_hcas = ibt_get_hca_list(&guid_array);
5454 		IBTF_DPRINTF_L4(cmlog, "ibt_gid_to_node_info: ibt_get_hca_list "
5455 		    "returned %d hcas", num_hcas);
5456 
5457 		if (num_hcas == 0) {
5458 			IBTF_DPRINTF_L2(cmlog, "ibt_gid_to_node_info: "
5459 			    "NO HCA's Found on this system");
5460 			return (IBT_NO_HCAS_AVAILABLE);
5461 		}
5462 	}
5463 
5464 	for (i = 0; i < num_hcas; i++) {
5465 		if (local_node == B_FALSE) {
5466 			hcap = ibcm_find_hca_entry(guid_array[i]);
5467 			if (hcap == NULL) {
5468 				IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: "
5469 				    "HCA(%llX) info not found", guid_array[i]);
5470 				retval = IBT_NO_HCAS_AVAILABLE;
5471 				continue;
5472 			}
5473 			num_ports = hcap->hca_num_ports;
5474 		}
5475 
5476 		for (j = 0; j < num_ports; j++) {
5477 			uint8_t		port = 0;
5478 
5479 			if (local_node == B_TRUE)
5480 				port = hport.hp_port;
5481 			else
5482 				port = j + 1;
5483 
5484 			/* Get SA Access Handle. */
5485 			saa_handle = ibcm_get_saa_handle(hcap, port);
5486 			if (saa_handle == NULL) {
5487 				IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: "
5488 				    "Port %d of HCA (%llX) is NOT ACTIVE",
5489 				    port, hport.hp_hca_guid);
5490 				retval = IBT_NODE_RECORDS_NOT_FOUND;
5491 				continue;
5492 			}
5493 
5494 			if (local_node == B_FALSE) {
5495 				ib_gid_t	sgid;
5496 				int		sa_ret;
5497 
5498 				/*
5499 				 * Check whether 'gid' and this port has same
5500 				 * subnet prefix. If not, then there is no use
5501 				 * in searching from this port.
5502 				 */
5503 				sgid = hcap->hca_port_info[j].port_sgid0;
5504 				if (gid.gid_prefix != sgid.gid_prefix) {
5505 					IBTF_DPRINTF_L3(cmlog,
5506 					    "ibt_gid_to_node_info:Sn_Prefix of "
5507 					    "GID(%llX) and Port's(%llX) differ",
5508 					    gid.gid_prefix, sgid.gid_prefix);
5509 					retval = IBT_NODE_RECORDS_NOT_FOUND;
5510 					continue;
5511 				}
5512 
5513 				/*
5514 				 * First Get Path Records for the specified DGID
5515 				 * from this port (SGID). From Path Records,
5516 				 * note down DLID, then use this DLID as Input
5517 				 * attribute to get NodeRecords from SA Access.
5518 				 */
5519 				npaths = 1;
5520 				path = NULL;
5521 
5522 				sa_ret = ibmf_saa_gid_to_pathrecords(saa_handle,
5523 				    sgid, gid, 0, 0, B_TRUE, &npaths, 0, &len,
5524 				    &path);
5525 				if (sa_ret != IBMF_SUCCESS) {
5526 					IBTF_DPRINTF_L2(cmlog,
5527 					    "ibt_gid_to_node_info: "
5528 					    "ibmf_saa_gid_to_pathrecords() "
5529 					    "returned error: %d ", sa_ret);
5530 					retval =
5531 					    ibcm_ibmf_analyze_error(sa_ret);
5532 					continue;
5533 				} else if ((npaths == 0) || (path == NULL)) {
5534 					IBTF_DPRINTF_L3(cmlog,
5535 					    "ibt_gid_to_node_info: failed (%d) "
5536 					    "to get path records for the DGID "
5537 					    "0x%llX from SGID 0x%llX", sa_ret,
5538 					    gid.gid_guid, sgid.gid_guid);
5539 					retval = IBT_NODE_RECORDS_NOT_FOUND;
5540 					continue;
5541 				}
5542 				node_lid = path->DLID;	/* LID */
5543 
5544 				IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: "
5545 				    "Remote Node: LID = 0x%X", node_lid);
5546 
5547 				/* Free SA_Access memory for path record. */
5548 				kmem_free(path, len);
5549 			}
5550 
5551 			/* Retrieve Node Records from SA Access. */
5552 			bzero(&nr_req, sizeof (sa_node_record_t));
5553 
5554 			nr_req.LID = node_lid;	/* LID */
5555 
5556 			retval = ibcm_get_node_rec(saa_handle, &nr_req,
5557 			    SA_NODEINFO_COMPMASK_NODELID, &res_p, &len);
5558 			if (retval == IBT_NODE_RECORDS_NOT_FOUND) {
5559 				IBTF_DPRINTF_L2(cmlog, "ibt_gid_to_node_info: "
5560 				    "failed (%d) to get Node records", retval);
5561 				continue;
5562 			} else if (retval != IBT_SUCCESS) {
5563 				IBTF_DPRINTF_L2(cmlog, "ibt_gid_to_node_info: "
5564 				    "failed (%d) to get Node records", retval);
5565 				ibcm_dec_hca_acc_cnt(hcap);
5566 				goto gid_to_ni_exit;
5567 			}
5568 
5569 			num_rec = len/sizeof (sa_node_record_t);
5570 			nr_resp = (sa_node_record_t *)(uchar_t *)res_p;
5571 
5572 			/* Validate the returned number of records. */
5573 			if ((nr_resp != NULL) && (num_rec > 0)) {
5574 
5575 				IBCM_DUMP_NODE_REC(nr_resp);
5576 
5577 				_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(
5578 				    *node_info_p))
5579 
5580 				node_info_p->n_sys_img_guid =
5581 				    nr_resp->NodeInfo.SystemImageGUID;
5582 				node_info_p->n_node_guid =
5583 				    nr_resp->NodeInfo.NodeGUID;
5584 				node_info_p->n_port_guid =
5585 				    nr_resp->NodeInfo.PortGUID;
5586 				node_info_p->n_dev_id =
5587 				    nr_resp->NodeInfo.DeviceID;
5588 				node_info_p->n_revision =
5589 				    nr_resp->NodeInfo.Revision;
5590 				node_info_p->n_vendor_id =
5591 				    nr_resp->NodeInfo.VendorID;
5592 				node_info_p->n_num_ports =
5593 				    nr_resp->NodeInfo.NumPorts;
5594 				node_info_p->n_port_num =
5595 				    nr_resp->NodeInfo.LocalPortNum;
5596 				node_info_p->n_node_type =
5597 				    nr_resp->NodeInfo.NodeType;
5598 				(void) strncpy(node_info_p->n_description,
5599 				    (char *)&nr_resp->NodeDescription, 64);
5600 
5601 				_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(
5602 				    *node_info_p))
5603 
5604 				/*
5605 				 * Deallocate the memory allocated by SA for
5606 				 * 'nr_resp'.
5607 				 */
5608 				ibcm_dec_hca_acc_cnt(hcap);
5609 				kmem_free(nr_resp, len);
5610 				retval = IBT_SUCCESS;
5611 
5612 				goto gid_to_ni_exit;
5613 			} else {
5614 				retval = IBT_NODE_RECORDS_NOT_FOUND;
5615 				IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: "
5616 				    "Node Records NOT found - PortGUID %016llX",
5617 				    gid.gid_guid);
5618 			}
5619 		}
5620 		ibcm_dec_hca_acc_cnt(hcap);
5621 
5622 		if (local_node == B_TRUE)
5623 			break;
5624 	}
5625 
5626 gid_to_ni_exit:
5627 	if (guid_array)
5628 		ibt_free_hca_list(guid_array, num_hcas);
5629 
5630 	IBTF_DPRINTF_L3(cmlog, "ibt_gid_to_node_info: done. Status %d", retval);
5631 
5632 	return (retval);
5633 }
5634 
5635 
5636 static ibt_status_t
5637 ibcm_get_node_rec(ibmf_saa_handle_t saa_handle, sa_node_record_t *nr_req,
5638     uint64_t component_mask, void *result_p, size_t *len)
5639 {
5640 	ibmf_saa_access_args_t  args;
5641 	size_t			length;
5642 	ibt_status_t		retval;
5643 
5644 	args.sq_attr_id = SA_NODERECORD_ATTRID;
5645 	args.sq_template = nr_req;
5646 	args.sq_access_type = IBMF_SAA_RETRIEVE;
5647 	args.sq_template_length = sizeof (sa_node_record_t);
5648 	args.sq_component_mask = component_mask;
5649 	args.sq_callback = NULL;
5650 	args.sq_callback_arg = NULL;
5651 
5652 	retval = ibcm_contact_sa_access(saa_handle, &args, &length, result_p);
5653 	if (retval != IBT_SUCCESS) {
5654 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_node_rec: SA Call Failed");
5655 		return (retval);
5656 	}
5657 
5658 	*len = length;
5659 
5660 	/* Validate the returned number of records. */
5661 	if ((result_p != NULL) && (length > 0)) {
5662 		IBTF_DPRINTF_L3(cmlog, "ibcm_get_node_rec: Node Records FOUND");
5663 
5664 		/* Got it, done!. */
5665 		return (IBT_SUCCESS);
5666 	} else {
5667 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_node_rec: Node Rec NOT found");
5668 		return (IBT_NODE_RECORDS_NOT_FOUND);
5669 	}
5670 }
5671 
5672 
5673 /*
5674  * Function:
5675  *	ibt_get_companion_port_gids()
5676  * Description:
5677  *	Get list of GID's available on a companion port(s) of the specified
5678  *	GID or list of GIDs available on a specified Node GUID/SystemImage GUID.
5679  */
5680 ibt_status_t
5681 ibt_get_companion_port_gids(ib_gid_t gid, ib_guid_t hca_guid,
5682     ib_guid_t sysimg_guid, ib_gid_t **gids_p, uint_t *num_gids_p)
5683 {
5684 	sa_node_record_t	nr_req, *nr_resp;
5685 	void			*res_p;
5686 	ibmf_saa_handle_t	saa_handle;
5687 	int			sa_ret;
5688 	ibt_status_t		retval = IBT_SUCCESS;
5689 	ibcm_hca_info_t		*hcap;
5690 	ibtl_cm_hca_port_t	hport;
5691 	int			i, j;
5692 	uint_t			num_rec;
5693 	ib_guid_t		*guid_array = NULL;
5694 	sa_path_record_t	*path;
5695 	size_t			len;
5696 	uint8_t			npaths;
5697 	uint32_t		num_hcas = 0;
5698 	boolean_t		local_node = B_FALSE;
5699 	boolean_t		local_hca = B_FALSE;
5700 	ib_guid_t		h_guid = hca_guid;
5701 	ib_gid_t		*gidp = NULL, *t_gidp = NULL;
5702 	int			multi_hca_loop = 0;
5703 
5704 	IBTF_DPRINTF_L4(cmlog, "ibt_get_companion_port_gids(%llX:%llX, %llX, "
5705 	    "%llX)", gid.gid_prefix, gid.gid_guid, hca_guid, sysimg_guid);
5706 
5707 	if (((gid.gid_prefix == 0) || (gid.gid_guid == 0)) && (hca_guid == 0) &&
5708 	    (sysimg_guid == 0)) {
5709 		IBTF_DPRINTF_L2(cmlog, "ibt_get_companion_port_gids: "
5710 		    "Null Input attribute specified.");
5711 		return (IBT_INVALID_PARAM);
5712 	}
5713 
5714 	if ((num_gids_p == NULL) || (gids_p == NULL)) {
5715 		IBTF_DPRINTF_L2(cmlog, "ibt_get_companion_port_gids: "
5716 		    "num_gids_p or gids_p is NULL");
5717 		return (IBT_INVALID_PARAM);
5718 	}
5719 
5720 	*num_gids_p = 0;
5721 
5722 	/* Get the number of HCAs and their GUIDs */
5723 	if ((num_hcas = ibt_get_hca_list(&guid_array)) == 0) {
5724 		IBTF_DPRINTF_L2(cmlog, "ibt_get_companion_port_gids: "
5725 		    "NO HCA's Found on this system");
5726 		return (IBT_NO_HCAS_AVAILABLE);
5727 	}
5728 
5729 	IBTF_DPRINTF_L4(cmlog, "ibt_get_companion_port_gids: "
5730 	    "ibt_get_hca_list() returned %d hcas", num_hcas);
5731 
5732 	/*
5733 	 * If 'gid' is on local node, then get node lid (i.e. base lid of the
5734 	 * associated port) info via ibtl_cm_get_hca_port() call.
5735 	 */
5736 	bzero(&hport, sizeof (ibtl_cm_hca_port_t));
5737 	if ((gid.gid_prefix != 0) && (gid.gid_guid != 0) &&
5738 	    (ibtl_cm_get_hca_port(gid, 0, &hport) == IBT_SUCCESS)) {
5739 
5740 		if ((hca_guid != 0) && (hca_guid != hport.hp_hca_guid)) {
5741 			IBTF_DPRINTF_L2(cmlog, "ibt_get_companion_port_gids: "
5742 			    "Invalid GID<->HCAGUID combination specified.");
5743 			retval = IBT_INVALID_PARAM;
5744 			goto get_comp_pgid_exit;
5745 		}
5746 		h_guid = hport.hp_hca_guid;
5747 		local_node = B_TRUE;
5748 
5749 		IBTF_DPRINTF_L4(cmlog, "ibt_get_companion_port_gids: "
5750 		    "Local Node: HCA (0x%llX)", h_guid);
5751 	} else if (h_guid) {	/* Is specified HCA GUID - local? */
5752 		for (i = 0; i < num_hcas; i++) {
5753 			if (h_guid == guid_array[i]) {
5754 				local_hca = B_TRUE;
5755 				break;
5756 			}
5757 		}
5758 	} else if (sysimg_guid) { /* Is specified SystemImage GUID - local? */
5759 		for (i = 0; i < num_hcas; i++) {
5760 			ibt_status_t	ret;
5761 			ibt_hca_attr_t	hca_attr;
5762 
5763 			ret = ibt_query_hca_byguid(guid_array[i], &hca_attr);
5764 			if (ret != IBT_SUCCESS) {
5765 				IBTF_DPRINTF_L2(cmlog,
5766 				    "ibt_get_companion_port_gids: HCA(%llX) "
5767 				    "info not found", guid_array[i]);
5768 				retval = IBT_NO_HCAS_AVAILABLE;
5769 				continue;
5770 			}
5771 			if (hca_attr.hca_si_guid == sysimg_guid) {
5772 				if ((hca_guid != 0) &&
5773 				    (hca_guid != hca_attr.hca_node_guid)) {
5774 					IBTF_DPRINTF_L2(cmlog,
5775 					    "ibt_get_companion_port_gids: "
5776 					    "Invalid SysImg<->HCA GUID "
5777 					    "combination specified.");
5778 					retval = IBT_INVALID_PARAM;
5779 					goto get_comp_pgid_exit;
5780 				}
5781 				local_hca = B_TRUE;
5782 				h_guid = hca_attr.hca_node_guid;
5783 				break;
5784 			}
5785 		}
5786 	}
5787 
5788 	if ((local_node == B_TRUE) || (local_hca == B_TRUE)) {
5789 		retval = ibtl_cm_get_local_comp_gids(h_guid, gid, gids_p,
5790 		    num_gids_p);
5791 		goto get_comp_pgid_exit;
5792 	}
5793 
5794 get_comp_for_multihca:
5795 	/* We will be here, if request is for remote node */
5796 	for (i = 0; i < num_hcas; i++) {
5797 		int		multism;
5798 		uint_t		count = 0;
5799 		int		multi_sm_loop = 0;
5800 		uint_t		k = 0, l;
5801 
5802 		hcap = ibcm_find_hca_entry(guid_array[i]);
5803 		if (hcap == NULL) {
5804 			IBTF_DPRINTF_L3(cmlog, "ibt_get_companion_port_gids: "
5805 			    "HCA(%llX) info not found", guid_array[i]);
5806 			retval = IBT_NO_HCAS_AVAILABLE;
5807 			continue;
5808 		}
5809 
5810 		/* 1 - MultiSM, 0 - Single SM */
5811 		multism = ibtl_cm_is_multi_sm(guid_array[i]);
5812 
5813 		for (j = 0; j < hcap->hca_num_ports; j++) {
5814 			ib_gid_t	sgid;
5815 			uint64_t	c_mask = 0;
5816 			ib_guid_t	pg;
5817 			uint_t		port = j;
5818 
5819 get_comp_for_multism:
5820 			IBTF_DPRINTF_L3(cmlog, "ibt_get_companion_port_gids: "
5821 			    "Port %d, HCA %llX, MultiSM= %d, Loop=%d",
5822 			    port + 1, h_guid, multism, multi_sm_loop);
5823 
5824 			/* Get SA Access Handle. */
5825 			saa_handle = ibcm_get_saa_handle(hcap, port + 1);
5826 			if (saa_handle == NULL) {
5827 				IBTF_DPRINTF_L2(cmlog,
5828 				    "ibt_get_companion_port_gids: "
5829 				    "Port (%d)  - NOT ACTIVE", port + 1);
5830 				retval = IBT_GIDS_NOT_FOUND;
5831 				continue;
5832 			}
5833 
5834 			/*
5835 			 * Check whether 'gid' and this port has same subnet
5836 			 * prefix. If not, then there is no use in searching
5837 			 * from this port.
5838 			 */
5839 			sgid = hcap->hca_port_info[port].port_sgid0;
5840 			if ((h_guid == 0) && (gid.gid_prefix != 0) &&
5841 			    (multi_sm_loop == 0) &&
5842 			    (gid.gid_prefix != sgid.gid_prefix)) {
5843 				IBTF_DPRINTF_L2(cmlog,
5844 				    "ibt_get_companion_port_gids: SnPrefix of "
5845 				    "GID(%llX) and Port SN_Pfx(%llX) differ",
5846 				    gid.gid_prefix, sgid.gid_prefix);
5847 				retval = IBT_GIDS_NOT_FOUND;
5848 				continue;
5849 			}
5850 
5851 			/*
5852 			 * If HCA GUID or System Image GUID is specified, then
5853 			 * we can achieve our goal sooner!.
5854 			 */
5855 			if ((h_guid == 0) && (sysimg_guid == 0)) {
5856 				/* So only GID info is provided. */
5857 
5858 				/*
5859 				 * First Get Path Records for the specified DGID
5860 				 * from this port (SGID). From Path Records,
5861 				 * note down DLID, then use this DLID as Input
5862 				 * attribute to get NodeRecords.
5863 				 */
5864 				npaths = 1;
5865 				path = NULL;
5866 
5867 				sa_ret = ibmf_saa_gid_to_pathrecords(saa_handle,
5868 				    sgid, gid, 0, 0, B_TRUE, &npaths, 0, &len,
5869 				    &path);
5870 				if (sa_ret != IBMF_SUCCESS) {
5871 					IBTF_DPRINTF_L2(cmlog,
5872 					    "ibt_get_companion_port_gids: "
5873 					    "ibmf_saa_gid_to_pathrecords() "
5874 					    "returned error: %d ", sa_ret);
5875 					retval =
5876 					    ibcm_ibmf_analyze_error(sa_ret);
5877 					ibcm_dec_hca_acc_cnt(hcap);
5878 					goto get_comp_pgid_exit;
5879 				} else if ((npaths == 0) || (path == NULL)) {
5880 					IBTF_DPRINTF_L2(cmlog,
5881 					    "ibt_get_companion_port_gids: "
5882 					    "failed (%d) to get path records "
5883 					    "for the DGID (0x%llX) from SGID "
5884 					    "(0x%llX)", sa_ret, gid.gid_guid,
5885 					    sgid.gid_guid);
5886 					retval = IBT_GIDS_NOT_FOUND;
5887 					continue;
5888 				}
5889 
5890 				bzero(&nr_req, sizeof (sa_node_record_t));
5891 				nr_req.LID = path->DLID;	/* LID */
5892 
5893 				IBTF_DPRINTF_L3(cmlog,
5894 				    "ibt_get_companion_port_gids: "
5895 				    "Remote Node: LID = 0x%X", nr_req.LID);
5896 
5897 				/* Free SA_Access memory for path record. */
5898 				kmem_free(path, len);
5899 
5900 				IBTF_DPRINTF_L3(cmlog,
5901 				    "ibt_get_companion_port_gids: SAA Call: "
5902 				    "based on LID ");
5903 
5904 				retval = ibcm_get_node_rec(saa_handle, &nr_req,
5905 				    SA_NODEINFO_COMPMASK_NODELID, &res_p, &len);
5906 				if (retval == IBT_NODE_RECORDS_NOT_FOUND) {
5907 					IBTF_DPRINTF_L2(cmlog,
5908 					    "ibt_get_companion_port_gids: "
5909 					    "failed (%d) to get Node records",
5910 					    retval);
5911 					continue;
5912 				} else if (retval != IBT_SUCCESS) {
5913 					IBTF_DPRINTF_L2(cmlog,
5914 					    "ibt_get_companion_port_gids: "
5915 					    "failed (%d) to get Node records",
5916 					    retval);
5917 					ibcm_dec_hca_acc_cnt(hcap);
5918 					goto get_comp_pgid_exit;
5919 				}
5920 
5921 				nr_resp = (sa_node_record_t *)(uchar_t *)res_p;
5922 				/* Note down HCA GUID info. */
5923 				h_guid = nr_resp->NodeInfo.NodeGUID;
5924 
5925 				IBTF_DPRINTF_L3(cmlog,
5926 				    "ibt_get_companion_port_gids: "
5927 				    "Remote HCA GUID: 0x%llX", h_guid);
5928 
5929 				IBCM_DUMP_NODE_REC(nr_resp);
5930 
5931 				kmem_free(res_p, len);
5932 			}
5933 
5934 			bzero(&nr_req, sizeof (sa_node_record_t));
5935 			if (h_guid != 0) {
5936 				nr_req.NodeInfo.NodeGUID = h_guid;
5937 				c_mask = SA_NODEINFO_COMPMASK_NODEGUID;
5938 			}
5939 
5940 			if (sysimg_guid != 0) {
5941 				nr_req.NodeInfo.SystemImageGUID = sysimg_guid;
5942 				c_mask |= SA_NODEINFO_COMPMASK_SYSIMAGEGUID;
5943 			}
5944 
5945 			IBTF_DPRINTF_L3(cmlog, "ibt_get_companion_port_gids: "
5946 			    "SAA Call: CMASK= 0x%llX", c_mask);
5947 
5948 			retval = ibcm_get_node_rec(saa_handle, &nr_req, c_mask,
5949 			    &res_p, &len);
5950 			if (retval == IBT_NODE_RECORDS_NOT_FOUND) {
5951 				IBTF_DPRINTF_L3(cmlog,
5952 				    "ibt_get_companion_port_gids: "
5953 				    "failed (%d) to get Node records", retval);
5954 				continue;
5955 			} else if (retval != IBT_SUCCESS) {
5956 				IBTF_DPRINTF_L2(cmlog,
5957 				    "ibt_get_companion_port_gids: Error: (%d) "
5958 				    "while getting Node records", retval);
5959 				ibcm_dec_hca_acc_cnt(hcap);
5960 				goto get_comp_pgid_exit;
5961 			}
5962 
5963 			num_rec = len/sizeof (sa_node_record_t);
5964 
5965 			/* We will be here, only if we found some NodeRec */
5966 			if (gid.gid_prefix && gid.gid_guid) {
5967 				nr_resp = (sa_node_record_t *)res_p;
5968 				for (l = 0; l < num_rec; l++, nr_resp++) {
5969 					pg = nr_resp->NodeInfo.PortGUID;
5970 					if (gid.gid_guid != pg)
5971 						count++;
5972 				}
5973 			} else {
5974 				count = num_rec;
5975 			}
5976 
5977 			if (count != 0) {
5978 				if (multi_sm_loop == 1) {
5979 					count += k;
5980 					t_gidp = kmem_zalloc(count *
5981 					    sizeof (ib_gid_t), KM_SLEEP);
5982 
5983 					if ((k != 0) && (gidp != NULL)) {
5984 						bcopy(gidp, t_gidp,
5985 						    k * sizeof (ib_gid_t));
5986 						kmem_free(gidp,
5987 						    k * sizeof (ib_gid_t));
5988 					}
5989 					gidp = t_gidp;
5990 				} else {
5991 					gidp = kmem_zalloc(count *
5992 					    sizeof (ib_gid_t), KM_SLEEP);
5993 				}
5994 				*num_gids_p = count;
5995 				*gids_p = gidp;
5996 
5997 				nr_resp = (sa_node_record_t *)res_p;
5998 				for (l = 0; l < num_rec; l++, nr_resp++) {
5999 					IBCM_DUMP_NODE_REC(nr_resp);
6000 
6001 					pg = nr_resp->NodeInfo.PortGUID;
6002 					IBTF_DPRINTF_L4(cmlog,
6003 					    "ibt_get_companion_port_gids: "
6004 					    "PortGID %llX", pg);
6005 
6006 					if (pg != gid.gid_guid) {
6007 						gidp[k].gid_prefix =
6008 						    sgid.gid_prefix;
6009 						gidp[k].gid_guid = pg;
6010 
6011 						IBTF_DPRINTF_L3(cmlog,
6012 						    "ibt_get_companion_pgids: "
6013 						    "GID[%d] = %llX:%llX", k,
6014 						    gidp[k].gid_prefix,
6015 						    gidp[k].gid_guid);
6016 
6017 						k++;
6018 						if (k == count)
6019 							break;
6020 					}
6021 				}
6022 				retval = IBT_SUCCESS;	/* done!. */
6023 				kmem_free(res_p, len);
6024 				ibcm_dec_hca_acc_cnt(hcap);
6025 				goto get_comp_pgid_exit;
6026 			} else {
6027 				IBTF_DPRINTF_L2(cmlog,
6028 				    "ibt_get_companion_port_gids: "
6029 				    "Companion PortGIDs not available");
6030 				retval = IBT_GIDS_NOT_FOUND;
6031 			}
6032 			/* Deallocate the memory for 'res_p'. */
6033 			kmem_free(res_p, len);
6034 
6035 			/*
6036 			 * If we are on MultiSM setup, then we need to lookout
6037 			 * from that subnet port too.
6038 			 */
6039 			if (multism) {
6040 				/* break if already searched both the subnet */
6041 				if (multi_sm_loop == 1)
6042 					break;
6043 
6044 				port = (j == 0) ? 1 : 0;
6045 				multi_sm_loop = 1;
6046 				goto get_comp_for_multism;
6047 			} else {
6048 				break;
6049 			}
6050 		}
6051 		ibcm_dec_hca_acc_cnt(hcap);
6052 
6053 		/*
6054 		 * We may be on dual HCA with dual SM configured system.  And
6055 		 * the input attr GID was visible from second HCA. So in order
6056 		 * to get the companion portgid we need to re-look from the
6057 		 * first HCA ports.
6058 		 */
6059 		if ((num_hcas > 1) && (i > 0) && (h_guid != 0) &&
6060 		    (multi_hca_loop != 1)) {
6061 			multi_hca_loop = 1;
6062 			goto get_comp_for_multihca;
6063 		}
6064 	}
6065 	if (*num_gids_p == 0)
6066 		retval = IBT_GIDS_NOT_FOUND;
6067 
6068 get_comp_pgid_exit:
6069 	if (guid_array)
6070 		ibt_free_hca_list(guid_array, num_hcas);
6071 
6072 	if ((retval != IBT_SUCCESS) && (*num_gids_p != 0)) {
6073 		retval = IBT_SUCCESS;
6074 	}
6075 
6076 	IBTF_DPRINTF_L3(cmlog, "ibt_get_companion_port_gids: done. Status %d, "
6077 	    "Found %d GIDs", retval, *num_gids_p);
6078 
6079 	return (retval);
6080 }
6081 
6082 /* RDMA IP CM Support routines */
6083 ibt_status_t
6084 ibt_get_src_ip(ib_gid_t gid, ib_pkey_t pkey, ibt_ip_addr_t *src_ip)
6085 {
6086 	ibcm_arp_ip_t		*ipp;
6087 	ibcm_arp_ibd_insts_t	ibds;
6088 	int			i;
6089 	boolean_t		found = B_FALSE;
6090 	ibt_status_t		retval = IBT_SUCCESS;
6091 
6092 	IBTF_DPRINTF_L4(cmlog, "ibt_get_src_ip(%llX:%llX, %X, %p)",
6093 	    gid.gid_prefix, gid.gid_guid, pkey, src_ip);
6094 
6095 	if (gid.gid_prefix == 0 || gid.gid_guid == 0) {
6096 		IBTF_DPRINTF_L3(cmlog, "ibt_get_src_ip: Invalid GID.");
6097 		return (IBT_INVALID_PARAM);
6098 	}
6099 
6100 	if (src_ip == NULL) {
6101 		IBTF_DPRINTF_L3(cmlog, "ibt_get_src_ip: ERROR: src_ip NULL");
6102 		return (IBT_INVALID_PARAM);
6103 	}
6104 
6105 	bzero(&ibds, sizeof (ibcm_arp_ibd_insts_t));
6106 	ibds.ibcm_arp_ibd_alloc = IBCM_ARP_IBD_INSTANCES;
6107 	ibds.ibcm_arp_ibd_cnt = 0;
6108 	ibds.ibcm_arp_ip = (ibcm_arp_ip_t *)kmem_zalloc(
6109 	    ibds.ibcm_arp_ibd_alloc * sizeof (ibcm_arp_ip_t), KM_SLEEP);
6110 
6111 	retval = ibcm_arp_get_ibds(&ibds);
6112 	if (retval != IBT_SUCCESS) {
6113 		IBTF_DPRINTF_L2(cmlog, "ibt_get_src_ip: ibcm_arp_get_ibds "
6114 		    "failed to get IBD Instances: ret 0x%x", retval);
6115 		goto get_src_ip_end;
6116 	}
6117 
6118 	for (i = 0, ipp = ibds.ibcm_arp_ip; i < ibds.ibcm_arp_ibd_cnt;
6119 	    i++, ipp++) {
6120 		if (ipp->ip_port_gid.gid_prefix == gid.gid_prefix &&
6121 		    ipp->ip_port_gid.gid_guid == gid.gid_guid) {
6122 			if (pkey) {
6123 				if (ipp->ip_pkey == pkey) {
6124 					found = B_TRUE;
6125 					break;
6126 				} else
6127 					continue;
6128 			}
6129 			found = B_TRUE;
6130 			break;
6131 		}
6132 	}
6133 
6134 	if (found == B_FALSE) {
6135 		retval = IBT_SRC_IP_NOT_FOUND;
6136 	} else {
6137 		src_ip->family = ipp->ip_inet_family;
6138 		if (src_ip->family == AF_INET) {
6139 			bcopy(&ipp->ip_cm_sin.sin_addr, &src_ip->un.ip4addr,
6140 			    sizeof (in_addr_t));
6141 			IBTF_DPRINTF_L4(cmlog, "ibt_get_src_ip: Got %lX",
6142 			    src_ip->un.ip4addr);
6143 		} else if (src_ip->family == AF_INET6) {
6144 			bcopy(&ipp->ip_cm_sin6.sin6_addr, &src_ip->un.ip6addr,
6145 			    sizeof (in6_addr_t));
6146 		}
6147 	}
6148 
6149 get_src_ip_end:
6150 	if (ibds.ibcm_arp_ip)
6151 		kmem_free(ibds.ibcm_arp_ip,
6152 		    ibds.ibcm_arp_ibd_alloc * sizeof (ibcm_arp_ip_t));
6153 
6154 	return (retval);
6155 }
6156 
6157 ib_svc_id_t
6158 ibt_get_ip_sid(uint8_t protocol_num, in_port_t dst_port)
6159 {
6160 	ib_svc_id_t	sid;
6161 
6162 	IBTF_DPRINTF_L4(cmlog, "ibt_get_ip_sid(%X, %lX)", protocol_num,
6163 	    dst_port);
6164 
6165 	/*
6166 	 * If protocol_num is non-zero, then formulate the SID and return it.
6167 	 * If protocol_num is zero, then we need to assign a locally generated
6168 	 * IP SID with IB_SID_IPADDR_PREFIX.
6169 	 */
6170 	if (protocol_num) {
6171 		sid = IB_SID_IPADDR_PREFIX | protocol_num << 16 | dst_port;
6172 	} else {
6173 		sid = ibcm_alloc_ip_sid();
6174 	}
6175 
6176 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_sid: SID: 0x%016llX", sid);
6177 	return (sid);
6178 }
6179 
6180 ibt_status_t
6181 ibt_release_ip_sid(ib_svc_id_t ip_sid)
6182 {
6183 	IBTF_DPRINTF_L4(cmlog, "ibt_release_ip_sid(%llX)", ip_sid);
6184 
6185 	if (((ip_sid & IB_SID_IPADDR_PREFIX_MASK) != 0) ||
6186 	    (!(ip_sid & IB_SID_IPADDR_PREFIX))) {
6187 		IBTF_DPRINTF_L2(cmlog, "ibt_release_ip_sid(0x%016llX): ERROR: "
6188 		    "Called for Non-RDMA IP SID", ip_sid);
6189 		return (IBT_INVALID_PARAM);
6190 	}
6191 
6192 	/*
6193 	 * If protocol_num in ip_sid are all ZEROs, then this SID is allocated
6194 	 * by IBTF. If not, then the specified ip_sid is invalid.
6195 	 */
6196 	if (ip_sid & IB_SID_IPADDR_IPNUM_MASK) {
6197 		IBTF_DPRINTF_L2(cmlog, "ibt_release_ip_sid(0x%016llX): ERROR: "
6198 		    "Called for Non-IBTF assigned RDMA IP SID", ip_sid);
6199 		return (IBT_INVALID_PARAM);
6200 	}
6201 
6202 	ibcm_free_ip_sid(ip_sid);
6203 
6204 	return (IBT_SUCCESS);
6205 }
6206 
6207 
6208 uint8_t
6209 ibt_get_ip_protocol_num(ib_svc_id_t sid)
6210 {
6211 	return ((sid & IB_SID_IPADDR_IPNUM_MASK) >> 16);
6212 }
6213 
6214 in_port_t
6215 ibt_get_ip_dst_port(ib_svc_id_t sid)
6216 {
6217 	return (sid & IB_SID_IPADDR_PORTNUM_MASK);
6218 }
6219 
6220 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", ibt_ip_cm_info_t))
6221 _NOTE(SCHEME_PROTECTS_DATA("Unshared data", ibcm_ip_pvtdata_t))
6222 
6223 ibt_status_t
6224 ibt_format_ip_private_data(ibt_ip_cm_info_t *ip_cm_info,
6225     ibt_priv_data_len_t priv_data_len, void *priv_data_p)
6226 {
6227 	ibcm_ip_pvtdata_t	ip_data;
6228 
6229 	IBTF_DPRINTF_L4(cmlog, "ibt_format_ip_private_data(%p, %d, %p)",
6230 	    ip_cm_info, priv_data_len, priv_data_p);
6231 
6232 	if ((ip_cm_info == NULL) || (priv_data_p == NULL) ||
6233 	    (priv_data_len < IBT_IP_HDR_PRIV_DATA_SZ)) {
6234 		IBTF_DPRINTF_L2(cmlog, "ibt_format_ip_private_data: ERROR "
6235 		    "Invalid Inputs.");
6236 		return (IBT_INVALID_PARAM);
6237 	}
6238 
6239 	bzero(&ip_data, sizeof (ibcm_ip_pvtdata_t));
6240 	ip_data.ip_srcport = ip_cm_info->src_port; /* Source Port */
6241 
6242 	/* IPV = 0x4, if IP-Addr are IPv4 format, else 0x6 for IPv6 */
6243 	if (ip_cm_info->src_addr.family == AF_INET) {
6244 		ip_data.ip_ipv = IBT_CM_IP_IPV_V4;
6245 		ip_data.ip_srcv4 = ip_cm_info->src_addr.un.ip4addr;
6246 		ip_data.ip_dstv4 = ip_cm_info->dst_addr.un.ip4addr;
6247 	} else if (ip_cm_info->src_addr.family == AF_INET6) {
6248 		ip_data.ip_ipv = IBT_CM_IP_IPV_V6;
6249 		bcopy(&ip_cm_info->src_addr.un.ip6addr,
6250 		    &ip_data.ip_srcv6, sizeof (in6_addr_t));
6251 		bcopy(&ip_cm_info->dst_addr.un.ip6addr,
6252 		    &ip_data.ip_dstv6, sizeof (in6_addr_t));
6253 	} else {
6254 		IBTF_DPRINTF_L2(cmlog, "ibt_format_ip_private_data: ERROR "
6255 		    "IP Addr needs to be either AF_INET or AF_INET6 family.");
6256 		return (IBT_INVALID_PARAM);
6257 	}
6258 
6259 	ip_data.ip_MajV = IBT_CM_IP_MAJ_VER;
6260 	ip_data.ip_MinV = IBT_CM_IP_MIN_VER;
6261 
6262 	bcopy(&ip_data, priv_data_p, IBT_IP_HDR_PRIV_DATA_SZ);
6263 
6264 	return (IBT_SUCCESS);
6265 }
6266 
6267 
6268 ibt_status_t
6269 ibt_get_ip_data(ibt_priv_data_len_t priv_data_len, void *priv_data,
6270     ibt_ip_cm_info_t *ip_cm_infop)
6271 {
6272 	ibcm_ip_pvtdata_t	ip_data;
6273 
6274 	IBTF_DPRINTF_L4(cmlog, "ibt_get_ip_data(%d, %p, %p)",
6275 	    priv_data_len, priv_data, ip_cm_infop);
6276 
6277 	if ((ip_cm_infop == NULL) || (priv_data == NULL) ||
6278 	    (priv_data_len < IBT_IP_HDR_PRIV_DATA_SZ)) {
6279 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_data: ERROR Invalid Inputs");
6280 		return (IBT_INVALID_PARAM);
6281 	}
6282 
6283 	bcopy(priv_data, &ip_data, IBT_IP_HDR_PRIV_DATA_SZ);
6284 	ip_cm_infop->src_port = ip_data.ip_srcport; /* Source Port */
6285 
6286 	/* IPV = 0x4, if IP Address are IPv4 format, else 0x6 for IPv6 */
6287 	if (ip_data.ip_ipv == IBT_CM_IP_IPV_V4) {
6288 		/* Copy IPv4 Addr */
6289 		ip_cm_infop->src_addr.family = AF_INET;
6290 		ip_cm_infop->src_addr.un.ip4addr = ip_data.ip_srcv4;
6291 		ip_cm_infop->dst_addr.family = AF_INET;
6292 		ip_cm_infop->dst_addr.un.ip4addr = ip_data.ip_dstv4;
6293 	} else if (ip_data.ip_ipv == IBT_CM_IP_IPV_V6) {
6294 		/* Copy IPv6 Addr */
6295 		ip_cm_infop->src_addr.family = AF_INET6;
6296 		bcopy(&ip_data.ip_srcv6, &ip_cm_infop->src_addr.un.ip6addr,
6297 		    sizeof (in6_addr_t));
6298 		ip_cm_infop->dst_addr.family = AF_INET6;
6299 		bcopy(&ip_data.ip_dstv6, &ip_cm_infop->dst_addr.un.ip6addr,
6300 		    sizeof (in6_addr_t));
6301 	} else {
6302 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_data: ERROR: IP Addr needs"
6303 		    " to be either AF_INET or AF_INET6 family.");
6304 		return (IBT_INVALID_PARAM);
6305 	}
6306 
6307 	return (IBT_SUCCESS);
6308 }
6309 
6310 
6311 /* Routines for warlock */
6312 
6313 /* ARGSUSED */
6314 static void
6315 ibcm_dummy_mcg_handler(void *arg, ibt_status_t retval, ibt_mcg_info_t *minfo)
6316 {
6317 	ibcm_join_mcg_tqarg_t	dummy_mcg;
6318 
6319 	dummy_mcg.func = ibcm_dummy_mcg_handler;
6320 
6321 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_mcg_handler: "
6322 	    "dummy_mcg.func %p", dummy_mcg.func);
6323 }
6324 
6325 
6326 /* ARGSUSED */
6327 static void
6328 ibcm_dummy_recycle_rc_handler(ibt_status_t retval, void *arg)
6329 {
6330 	ibcm_taskq_recycle_arg_t	dummy_rc_recycle;
6331 
6332 	dummy_rc_recycle.func = ibcm_dummy_recycle_rc_handler;
6333 
6334 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_recycle_rc_handler: "
6335 	    "dummy_rc_recycle.func %p", dummy_rc_recycle.func);
6336 }
6337 
6338 
6339 /* ARGSUSED */
6340 static ibt_cm_status_t
6341 ibcm_dummy_ud_handler(void *priv, ibt_cm_ud_event_t *event,
6342     ibt_cm_ud_return_args_t *ret_args,
6343     void *priv_data, ibt_priv_data_len_t len)
6344 {
6345 	/*
6346 	 * Let warlock see that ibcm_local_handler_s::actual_cm_handler
6347 	 * points to this routine.
6348 	 */
6349 	ibcm_local_handler_t	p;
6350 	ibcm_ud_state_data_t	dummy_ud;
6351 
6352 	p.actual_cm_handler = ibcm_dummy_ud_handler;
6353 	dummy_ud.ud_cm_handler = ibcm_dummy_ud_handler;
6354 
6355 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_ud_handler: p.actual_cm_handler %p"
6356 	    "dummy_ud.ud_cm_handler %p", p.actual_cm_handler,
6357 	    dummy_ud.ud_cm_handler);
6358 	/*
6359 	 * Call all routines that the client's callback routine could call.
6360 	 */
6361 
6362 	return (IBT_CM_ACCEPT);
6363 }
6364 
6365 /* ARGSUSED */
6366 static ibt_cm_status_t
6367 ibcm_dummy_rc_handler(void *priv, ibt_cm_event_t *event,
6368     ibt_cm_return_args_t *ret_args, void *priv_data, ibt_priv_data_len_t len)
6369 {
6370 	ibcm_state_data_t	dummy_rc;
6371 
6372 	dummy_rc.cm_handler = ibcm_dummy_rc_handler;
6373 
6374 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_rc_handler: "
6375 	    "dummy_ud.ud_cm_handler %p", dummy_rc.cm_handler);
6376 	/*
6377 	 * Call all routines that the client's callback routine could call.
6378 	 */
6379 
6380 	return (IBT_CM_ACCEPT);
6381 }
6382