xref: /illumos-gate/usr/src/uts/common/io/ib/mgt/ibcm/ibcm_ti.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 2007 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 
349 	/* If no HCA found return failure */
350 	if ((hcap = ibcm_find_hca_entry(hca_guid)) == NULL) {
351 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
352 		    "hcap is NULL. Probably hca is not in active state",
353 		    channel);
354 		return (IBT_CHAN_HDL_INVALID);
355 	}
356 
357 	rdma_out = chan_args->oc_rdma_ra_out;
358 	rdma_in = chan_args->oc_rdma_ra_in;
359 
360 	if ((rdma_in > hcap->hca_max_rdma_in_qp) ||
361 	    (rdma_out > hcap->hca_max_rdma_out_qp)) {
362 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
363 		    "rdma in %d/out %d values exceed hca limits", channel,
364 		    rdma_in, rdma_out);
365 		ibcm_dec_hca_acc_cnt(hcap);
366 		return (IBT_INVALID_PARAM);
367 	}
368 
369 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
370 	    "rdma_in %d rdma_out %d", channel, rdma_in, rdma_out);
371 
372 	if (chan_args->oc_path->pi_prim_pkt_lt > ibcm_max_ib_pkt_lt) {
373 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
374 		    "Huge Primary Pkt lt %d", channel,
375 		    chan_args->oc_path->pi_prim_pkt_lt);
376 		ibcm_dec_hca_acc_cnt(hcap);
377 		return (IBT_PATH_PKT_LT_TOO_HIGH);
378 	}
379 
380 	status = ibt_get_port_state_byguid(hcap->hca_guid, port_no,
381 	    NULL, &base_lid);
382 	if (status != IBT_SUCCESS) {
383 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
384 		    "primary port_num %d not active", channel, port_no);
385 		ibcm_dec_hca_acc_cnt(hcap);
386 		return (status);
387 	}
388 
389 	/* Validate P_KEY Index */
390 	status = ibt_index2pkey_byguid(hcap->hca_guid, port_no,
391 	    IBCM_PRIM_CEP_PATH(chan_args).cep_pkey_ix, &prim_pkey);
392 	if (status != IBT_SUCCESS) {
393 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
394 		    "Invalid Primary PKeyIx %x", channel,
395 		    IBCM_PRIM_CEP_PATH(chan_args).cep_pkey_ix);
396 		ibcm_dec_hca_acc_cnt(hcap);
397 		return (status);
398 	}
399 
400 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
401 	    "primary_port_num %d primary_pkey 0x%x", channel, port_no,
402 	    prim_pkey);
403 
404 	if ((hcap->hca_port_info[port_no - 1].port_ibmf_hdl == NULL) &&
405 	    ((status = ibcm_hca_reinit_port(hcap, port_no - 1))
406 	    != IBT_SUCCESS)) {
407 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
408 		    "ibmf reg or callback setup failed during re-initialize",
409 		    channel);
410 		ibcm_dec_hca_acc_cnt(hcap);
411 		return (status);
412 	}
413 
414 	ibmf_hdl = hcap->hca_port_info[port_no - 1].port_ibmf_hdl;
415 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
416 	    "primary ibmf_hdl = 0x%p", channel, ibmf_hdl);
417 
418 
419 	primary_slid = base_lid + IBCM_PRIM_ADDS_VECT(chan_args).av_src_path;
420 
421 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: channel 0x%p "
422 	    "primary SLID = %x", channel, primary_slid);
423 
424 	/* check first if alternate path exists or not as it is OPTIONAL */
425 	if (IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num != 0) {
426 		uint8_t	alt_port_no;
427 
428 		alt_port_no = IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num;
429 
430 		if (chan_args->oc_path->pi_alt_pkt_lt > ibcm_max_ib_pkt_lt) {
431 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
432 			    "Huge Alt Pkt lt %d", channel,
433 			    chan_args->oc_path->pi_alt_pkt_lt);
434 			ibcm_dec_hca_acc_cnt(hcap);
435 			return (IBT_PATH_PKT_LT_TOO_HIGH);
436 		}
437 
438 		if (port_no != alt_port_no) {
439 
440 			status = ibt_get_port_state_byguid(hcap->hca_guid,
441 			    alt_port_no, NULL, &base_lid);
442 			if (status != IBT_SUCCESS) {
443 
444 				IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
445 				    "chan 0x%p alt_port_num %d inactive %d",
446 				    channel, alt_port_no, status);
447 				ibcm_dec_hca_acc_cnt(hcap);
448 				return (status);
449 			}
450 
451 		}
452 		alternate_slid =
453 		    base_lid + IBCM_ALT_ADDS_VECT(chan_args).av_src_path;
454 
455 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan %0xp "
456 		    "alternate SLID = %x", channel, alternate_slid);
457 	}
458 
459 	/*
460 	 * only pkey needs to be zero'ed, because all other fields are set in
461 	 * in ibcm_init_reply_addr. But, let's bzero the complete struct for
462 	 * any future modifications.
463 	 */
464 	bzero(&cm_reply_addr, sizeof (cm_reply_addr));
465 
466 	/* Initialize the MAD destination address in stored_reply_addr */
467 	if ((status = ibcm_init_reply_addr(hcap, &cm_reply_addr, chan_args,
468 	    flags, &cm_pkt_lt, primary_slid)) != IBT_SUCCESS) {
469 
470 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
471 		    "ibcm_init_reply_addr failed status %d ", channel, status);
472 		ibcm_dec_hca_acc_cnt(hcap);
473 		return (status);
474 	}
475 
476 
477 	/* Initialize the pkey for CM MAD communication */
478 	if (cm_reply_addr.rcvd_addr.ia_p_key == 0)
479 		cm_reply_addr.rcvd_addr.ia_p_key = prim_pkey;
480 
481 #ifdef DEBUG
482 	ibcm_print_reply_addr(channel, &cm_reply_addr);
483 #endif
484 
485 	/* Retrieve an ibmf qp for sending CM MADs */
486 	if ((cm_qp_entry = ibcm_find_qp(hcap, port_no,
487 	    cm_reply_addr.rcvd_addr.ia_p_key)) == NULL) {
488 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p "
489 		    "unable to allocate ibmf qp for CM MADs", channel);
490 		ibcm_dec_hca_acc_cnt(hcap);
491 		return (IBT_INSUFF_RESOURCE);
492 	}
493 
494 
495 	if (ibcm_alloc_comid(hcap, &local_comid) != IBCM_SUCCESS) {
496 		ibcm_release_qp(cm_qp_entry);
497 		ibcm_dec_hca_acc_cnt(hcap);
498 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan 0x%p"
499 		    " Unable to allocate comid", channel);
500 		return (IBT_INSUFF_KERNEL_RESOURCE);
501 	}
502 
503 	/* allocate an IBMF mad buffer (REQ) */
504 	if ((status = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg,
505 	    MAD_METHOD_SEND)) != IBT_SUCCESS) {
506 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
507 		    "chan 0x%p ibcm_alloc_out_msg failed", channel);
508 		ibcm_release_qp(cm_qp_entry);
509 		ibcm_free_comid(hcap, local_comid);
510 		ibcm_dec_hca_acc_cnt(hcap);
511 		return (status);
512 	}
513 
514 	/* allocate an IBMF mad buffer (DREQ) */
515 	if ((status = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg_dreq,
516 	    MAD_METHOD_SEND)) != IBT_SUCCESS) {
517 		IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
518 		    "chan 0x%p ibcm_alloc_out_msg failed", channel);
519 		(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg);
520 		ibcm_release_qp(cm_qp_entry);
521 		ibcm_free_comid(hcap, local_comid);
522 		ibcm_dec_hca_acc_cnt(hcap);
523 		return (status);
524 	}
525 
526 	/* Init to Init, if QP's port does not match with path information */
527 	if (qp_query_attr.qp_info.qp_transport.rc.rc_path.cep_hca_port_num !=
528 	    IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num) {
529 
530 		ibt_qp_info_t		qp_info;
531 		ibt_cep_modify_flags_t	cep_flags;
532 
533 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
534 		    "chan 0x%p chan port %d", channel,
535 		    qp_query_attr.qp_info.qp_transport.rc.rc_path.\
536 		    cep_hca_port_num);
537 
538 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
539 		    "chan 0x%p path port %d", channel, port_no);
540 
541 		bzero(&qp_info, sizeof (qp_info));
542 		/* For now, set it to RC type */
543 
544 		qp_info.qp_trans = IBT_RC_SRV;
545 		qp_info.qp_state = IBT_STATE_INIT;
546 		qp_info.qp_transport.rc.rc_path.cep_hca_port_num = port_no;
547 
548 		cep_flags = IBT_CEP_SET_STATE | IBT_CEP_SET_PORT;
549 
550 		status = ibt_modify_qp(channel, cep_flags, &qp_info, NULL);
551 
552 		if (status != IBT_SUCCESS) {
553 			IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: "
554 			    "chan 0x%p ibt_modify_qp() = %d", channel, status);
555 			ibcm_release_qp(cm_qp_entry);
556 			ibcm_free_comid(hcap, local_comid);
557 			ibcm_dec_hca_acc_cnt(hcap);
558 			(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg);
559 			(void) ibcm_free_out_msg(ibmf_hdl, &ibmf_msg_dreq);
560 			return (status);
561 		} else
562 			IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: "
563 			    "chan 0x%p ibt_modify_qp() = %d", channel, status);
564 	}
565 
566 	/* allocate ibcm_state_data_t before grabbing the WRITER lock */
567 	statep = kmem_zalloc(sizeof (ibcm_state_data_t), KM_SLEEP);
568 	rw_enter(&hcap->hca_state_rwlock, RW_WRITER);
569 	lkup_status = ibcm_lookup_msg(IBCM_OUTGOING_REQ, local_comid, 0, 0,
570 	    hcap, &statep);
571 	rw_exit(&hcap->hca_state_rwlock);
572 
573 	/* CM should be seeing this for the first time */
574 	ASSERT(lkup_status == IBCM_LOOKUP_NEW);
575 
576 	/* Increment the hca's resource count */
577 	ibcm_inc_hca_res_cnt(hcap);
578 
579 	/* Once a resource created on hca, no need to hold the acc cnt */
580 	ibcm_dec_hca_acc_cnt(hcap);
581 
582 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*statep))
583 
584 	statep->timerid = 0;
585 	statep->local_hca_guid = hca_guid;
586 	statep->local_qpn = local_qpn;
587 	statep->stored_reply_addr.cm_qp_entry = cm_qp_entry;
588 	statep->prim_port = IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num;
589 	statep->alt_port = IBCM_ALT_CEP_PATH(chan_args).cep_hca_port_num;
590 
591 
592 	/* Save "statep" as channel's CM private data.  */
593 	statep->channel = channel;
594 	IBCM_SET_CHAN_PRIVATE(statep->channel, statep);
595 
596 	statep->stored_msg = ibmf_msg;
597 	statep->dreq_msg = ibmf_msg_dreq;
598 
599 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*req_msgp))
600 
601 	/* Start filling in the REQ MAD */
602 	req_msgp = (ibcm_req_msg_t *)IBCM_OUT_MSGP(statep->stored_msg);
603 	req_msgp->req_local_comm_id = h2b32(local_comid);
604 	req_msgp->req_svc_id = h2b64(chan_args->oc_path->pi_sid);
605 	req_msgp->req_local_ca_guid = h2b64(hca_guid);
606 	req_msgp->req_local_qkey = h2b32(local_qkey);	/* for EEC/RD */
607 
608 	/* Bytes 32-35 are req_local_qpn and req_off_resp_resources */
609 	req_msgp->req_local_qpn_plus = h2b32(local_qpn << 8 | rdma_in);
610 
611 	/* Bytes 36-39 are req_local_eec_no and req_off_initiator_depth */
612 	req_msgp->req_local_eec_no_plus = h2b32(local_eecn << 8 | rdma_out);
613 
614 	if (flags & IBT_OCHAN_REMOTE_CM_TM)
615 		remote_cm_resp_time = chan_args->oc_remote_cm_time;
616 	else
617 		remote_cm_resp_time = ibcm_remote_response_time;
618 
619 	/*
620 	 * Bytes 40-43 - remote_eecn, remote_cm_resp_time, tran_type,
621 	 * IBT_CM_FLOW_CONTROL is always set by default.
622 	 */
623 	req_msgp->req_remote_eecn_plus = h2b32(
624 	    remote_eecn << 8 | (ibt_usec2ib(remote_cm_resp_time) & 0x1f) << 3 |
625 	    IBT_RC_SRV << 1 | IBT_CM_FLOW_CONTROL);
626 
627 	if (flags & IBT_OCHAN_LOCAL_CM_TM)
628 		local_cm_proc_time = chan_args->oc_local_cm_time;
629 	else
630 		local_cm_proc_time = ibcm_local_processing_time;
631 
632 	local_cm_resp_time = ibt_usec2ib(local_cm_proc_time +
633 	    2 * ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt) +
634 	    ibcm_sw_delay);
635 
636 	/* save retry count */
637 	statep->cep_retry_cnt = chan_args->oc_path_retry_cnt;
638 
639 	if (flags & IBT_OCHAN_STARTING_PSN)
640 		starting_psn = chan_args->oc_starting_psn;
641 
642 	if (local_cm_resp_time > 0x1f)
643 		local_cm_resp_time = 0x1f;
644 
645 	/* Bytes 44-47 are req_starting_psn, local_cm_resp_time and retry_cnt */
646 	req_msgp->req_starting_psn_plus = h2b32(starting_psn << 8 |
647 	    local_cm_resp_time << 3 | statep->cep_retry_cnt);
648 
649 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
650 	    "Prim Pkt lt (IB time) 0x%x", channel,
651 	    chan_args->oc_path->pi_prim_pkt_lt);
652 
653 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
654 	    "local_cm_proc_time(usec) %d ", channel, local_cm_proc_time);
655 
656 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
657 	    "local_cm_resp_time(ib_time) %d", channel, local_cm_resp_time);
658 
659 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
660 	    "remote_cm_resp_time (usec) %d", channel, remote_cm_resp_time);
661 
662 	statep->starting_psn = starting_psn;
663 
664 	/* Pkey - bytes 48-49 */
665 	req_msgp->req_part_key = h2b16(prim_pkey);
666 
667 	if (flags & IBT_OCHAN_CM_RETRY)
668 		cm_retries = chan_args->oc_cm_retry_cnt;
669 	else
670 		cm_retries = ibcm_max_retries;
671 
672 	statep->max_cm_retries = statep->remaining_retry_cnt = cm_retries;
673 	req_msgp->req_max_cm_retries_plus = statep->max_cm_retries << 4;
674 
675 	/*
676 	 * Check whether SRQ is associated with this Channel, if yes, then
677 	 * set the SRQ Exists bit in the REQ.
678 	 */
679 	if (qp_query_attr.qp_srq != NULL) {
680 		req_msgp->req_max_cm_retries_plus |= (1 << 3);
681 	}
682 
683 	/*
684 	 * By default on Tavor, we override the PathMTU to 1K.
685 	 * To turn this off, set ibcm_override_path_mtu = 0.
686 	 */
687 	if (ibcm_override_path_mtu && IBCM_IS_HCA_TAVOR(hcap) &&
688 	    (chan_args->oc_path->pi_path_mtu > IB_MTU_1K)) {
689 		req_msgp->req_mtu_plus = IB_MTU_1K << 4 |
690 		    chan_args->oc_path_rnr_retry_cnt;
691 		IBTF_DPRINTF_L3(cmlog, "ibt_open_rc_channel: chan 0x%p PathMTU"
692 		    " overidden to IB_MTU_1K(%d) from %d", channel, IB_MTU_1K,
693 		    chan_args->oc_path->pi_path_mtu);
694 	} else
695 		req_msgp->req_mtu_plus = chan_args->oc_path->pi_path_mtu << 4 |
696 		    chan_args->oc_path_rnr_retry_cnt;
697 
698 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p CM retry cnt %d"
699 	    " staring PSN %x", channel, cm_retries, starting_psn);
700 
701 
702 #ifdef	NO_EEC_SUPPORT_YET
703 	if (flags & IBT_OCHAN_RDC_EXISTS)
704 		req_msgp->req_mtu_plus |= 8;
705 #endif
706 
707 	/* Initialize the "primary" port stuff next - bytes 52-95 */
708 	req_msgp->req_primary_l_port_lid = h2b16(primary_slid);
709 	req_msgp->req_primary_r_port_lid =
710 	    h2b16(IBCM_PRIM_ADDS_VECT(chan_args).av_dlid);
711 	req_msgp->req_primary_l_port_gid.gid_prefix =
712 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_sgid.gid_prefix);
713 	req_msgp->req_primary_l_port_gid.gid_guid =
714 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_sgid.gid_guid);
715 	req_msgp->req_primary_r_port_gid.gid_prefix =
716 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_prefix);
717 	req_msgp->req_primary_r_port_gid.gid_guid =
718 	    h2b64(IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid);
719 	primary_grh = IBCM_PRIM_ADDS_VECT(chan_args).av_send_grh;
720 
721 	statep->remote_hca_guid = /* not correct, but helpful for debugging */
722 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid;
723 
724 	/* Bytes 88-91 - primary_flowlbl, and primary_srate */
725 	req_msgp->req_primary_flow_label_plus =
726 	    h2b32(((primary_grh == B_TRUE) ?
727 	    (IBCM_PRIM_ADDS_VECT(chan_args).av_flow << 12) : 0) |
728 	    IBCM_PRIM_ADDS_VECT(chan_args).av_srate);
729 	req_msgp->req_primary_traffic_class = (primary_grh == B_TRUE) ?
730 	    IBCM_PRIM_ADDS_VECT(chan_args).av_tclass : 0;
731 	req_msgp->req_primary_hop_limit = (primary_grh == B_TRUE) ?
732 	    IBCM_PRIM_ADDS_VECT(chan_args).av_hop : 1;
733 	req_msgp->req_primary_sl_plus =
734 	    IBCM_PRIM_ADDS_VECT(chan_args).av_srvl << 4 |
735 	    ((primary_grh == B_TRUE) ? 0 : 8);
736 
737 	req_msgp->req_primary_localtime_plus =
738 	    ibt_usec2ib((2 * ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt)) +
739 	    ibt_ib2usec(hcap->hca_ack_delay)) << 3;
740 
741 	IBTF_DPRINTF_L2(cmlog, "ibt_open_rc_channel: chan %p statep %p",
742 	    channel, statep);
743 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
744 	    "active hca_ack_delay (usec) %d", channel,
745 	    req_msgp->req_primary_localtime_plus);
746 
747 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
748 	    "Sent primary cep timeout (IB Time) %d", channel,
749 	    hcap->hca_ack_delay);
750 
751 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p prim_dlid %x ",
752 	    channel, IBCM_PRIM_ADDS_VECT(chan_args).av_dlid);
753 
754 	IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
755 	    "prim GID %llX:%llX", channel,
756 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_prefix,
757 	    IBCM_PRIM_ADDS_VECT(chan_args).av_dgid.gid_guid);
758 
759 	/* Initialize the "alternate" port stuff - optional */
760 	if (chan_args->oc_path->pi_alt_cep_path.cep_hca_port_num != 0) {
761 		ib_gid_t	tmp_gid;
762 
763 		req_msgp->req_alt_l_port_lid = h2b16(alternate_slid);
764 		req_msgp->req_alt_r_port_lid =
765 		    h2b16(IBCM_ALT_ADDS_VECT(chan_args).av_dlid);
766 		/*
767 		 * doing all this as req_alt_r/l_port_gid is at offset
768 		 * 100, 116 which is not divisible by 8
769 		 */
770 
771 		tmp_gid.gid_prefix =
772 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_prefix);
773 		tmp_gid.gid_guid =
774 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_guid);
775 		bcopy(&tmp_gid, &req_msgp->req_alt_r_port_gid[0],
776 		    sizeof (ib_gid_t));
777 		tmp_gid.gid_prefix =
778 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_sgid.gid_prefix);
779 		tmp_gid.gid_guid =
780 		    h2b64(IBCM_ALT_ADDS_VECT(chan_args).av_sgid.gid_guid);
781 
782 		bcopy(&tmp_gid, &req_msgp->req_alt_l_port_gid[0],
783 		    sizeof (ib_gid_t));
784 		alternate_grh = IBCM_ALT_ADDS_VECT(chan_args).av_send_grh;
785 
786 		/* Bytes 132-135 - alternate_flow_label, and alternate srate */
787 		req_msgp->req_alt_flow_label_plus = h2b32(
788 		    (((alternate_grh == B_TRUE) ?
789 		    (IBCM_ALT_ADDS_VECT(chan_args).av_flow << 12) : 0) |
790 		    IBCM_ALT_ADDS_VECT(chan_args).av_srate));
791 		req_msgp->req_alt_traffic_class = (alternate_grh == B_TRUE) ?
792 		    IBCM_ALT_ADDS_VECT(chan_args).av_tclass : 0;
793 		req_msgp->req_alt_hop_limit = (alternate_grh == B_TRUE) ?
794 		    IBCM_ALT_ADDS_VECT(chan_args).av_hop : 1;
795 		req_msgp->req_alt_sl_plus =
796 		    IBCM_ALT_ADDS_VECT(chan_args).av_srvl << 4 |
797 		    ((alternate_grh == B_TRUE) ? 0 : 8);
798 		req_msgp->req_alt_localtime_plus = ibt_usec2ib((2 *
799 		    ibt_ib2usec(chan_args->oc_path->pi_alt_pkt_lt)) +
800 		    ibt_ib2usec(hcap->hca_ack_delay)) << 3;
801 
802 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
803 		    "alt_dlid %x ", channel,
804 		    IBCM_ALT_ADDS_VECT(chan_args).av_dlid);
805 
806 		IBTF_DPRINTF_L5(cmlog, "ibt_open_rc_channel: chan 0x%p "
807 		    "alt GID %llX:%llX", channel,
808 		    IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_prefix,
809 		    IBCM_ALT_ADDS_VECT(chan_args).av_dgid.gid_guid);
810 	}
811 
812 	len = min(chan_args->oc_priv_data_len, IBT_REQ_PRIV_DATA_SZ);
813 	if ((len > 0) && chan_args->oc_priv_data)
814 		bcopy(chan_args->oc_priv_data, req_msgp->req_private_data, len);
815 
816 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*req_msgp))
817 
818 	/* return_data is filled up in the state machine code */
819 	if (ret_args != NULL) {
820 		statep->open_return_data = ret_args;
821 	}
822 
823 	/* initialize some statep fields here */
824 	statep->mode = IBCM_ACTIVE_MODE;
825 	statep->hcap = hcap;
826 
827 	statep->cm_handler = chan_args->oc_cm_handler;
828 	statep->state_cm_private = chan_args->oc_cm_clnt_private;
829 
830 	statep->pkt_life_time =
831 	    ibt_ib2usec(chan_args->oc_path->pi_prim_pkt_lt);
832 
833 	statep->timer_value = ibt_ib2usec(ibt_usec2ib(
834 	    2 * ibt_ib2usec(cm_pkt_lt) + remote_cm_resp_time));
835 
836 	/* Initialize statep->stored_reply_addr */
837 	statep->stored_reply_addr.ibmf_hdl = ibmf_hdl;
838 
839 	/* Initialize stored reply addr fields */
840 	statep->stored_reply_addr.grh_hdr = cm_reply_addr.grh_hdr;
841 	statep->stored_reply_addr.rcvd_addr = cm_reply_addr.rcvd_addr;
842 	statep->stored_reply_addr.grh_exists = cm_reply_addr.grh_exists;
843 	statep->stored_reply_addr.port_num = cm_reply_addr.port_num;
844 
845 	/*
846 	 * The IPD on local/active side is calculated by path functions,
847 	 * hence available in the args of ibt_open_rc_channel
848 	 */
849 	statep->local_srate = IBCM_PRIM_ADDS_VECT(chan_args).av_srate;
850 	statep->local_alt_srate = IBCM_ALT_ADDS_VECT(chan_args).av_srate;
851 
852 	/* Store the source path bits for primary and alt paths */
853 	statep->prim_src_path_bits = IBCM_PRIM_ADDS_VECT(chan_args).av_src_path;
854 	statep->alt_src_path_bits = IBCM_ALT_ADDS_VECT(chan_args).av_src_path;
855 
856 	statep->open_flow = 1;
857 	statep->open_done = B_FALSE;
858 	statep->state = statep->timer_stored_state = IBCM_STATE_REQ_SENT;
859 	IBCM_REF_CNT_INCR(statep);	/* Decremented before return */
860 	IBCM_REF_CNT_INCR(statep);	/* Decremented after REQ is posted */
861 	statep->send_mad_flags |= IBCM_REQ_POST_BUSY;
862 
863 	IBCM_OUT_HDRP(statep->stored_msg)->AttributeID =
864 	    h2b16(IBCM_INCOMING_REQ + IBCM_ATTR_BASE_ID);
865 
866 	IBCM_OUT_HDRP(statep->stored_msg)->TransactionID =
867 	    h2b64(ibcm_generate_tranid(IBCM_INCOMING_REQ, statep->local_comid,
868 	    0));
869 
870 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*statep))
871 
872 	ibcm_open_enqueue(statep);
873 
874 	mutex_enter(&statep->state_mutex);
875 
876 	if (mode == IBT_BLOCKING) {
877 
878 		/* wait for REQ/REP/RTU */
879 		while (statep->open_done != B_TRUE) {
880 			cv_wait(&statep->block_client_cv, &statep->state_mutex);
881 		}
882 
883 		/*
884 		 * In the case that open_channel() fails because of a
885 		 * REJ or timeout, change retval to IBT_CM_FAILURE
886 		 */
887 		if (statep->open_return_data->rc_status != IBT_CM_ACCEPT)
888 			status = IBT_CM_FAILURE;
889 
890 		IBTF_DPRINTF_L3(cmlog, "ibt_open_rc_channel: chan 0x%p "
891 		    "ret status %d cm status %d", channel, status,
892 		    statep->open_return_data->rc_status);
893 	}
894 
895 	/* decrement the ref-count before leaving here */
896 	IBCM_REF_CNT_DECR(statep);
897 
898 	mutex_exit(&statep->state_mutex);
899 
900 	IBTF_DPRINTF_L4(cmlog, "ibt_open_rc_channel: chan 0x%p done", channel);
901 	return (status);
902 }
903 
904 /*
905  * ibcm_init_reply_addr:
906  *
907  * The brief description of functionality below.
908  *
909  * For IBT_OCHAN_PORT_REDIRECTED (ie., port redirected case):
910  *	Build CM path from chan_args->oc_cm_cep_path
911  *	Set CM pkt lt (ie.,life time) to chan_args->oc_cm_pkt_lt
912  *
913  * For IBT_OCHAN_REDIRECTED (ie., port and CM redirected case):
914  *	If Redirect LID is specified,
915  *		If Redirect GID is not specified or specified to be on the same
916  *		    subnet, then
917  *			Build CM path from chan_args->oc_cm_redirect_info
918  *			Set CM pkt lt to subnet timeout
919  *		Else (ie., GID specified, but on a different subnet)
920  *			Do a path lookup to build CM Path and set CM pkt lt
921  *
922  */
923 static ibt_status_t
924 ibcm_init_reply_addr(ibcm_hca_info_t *hcap, ibcm_mad_addr_t *reply_addr,
925     ibt_chan_open_args_t *chan_args, ibt_chan_open_flags_t flags,
926     ib_time_t *cm_pkt_lt, ib_lid_t prim_slid)
927 {
928 	ibt_adds_vect_t	*cm_adds;
929 	ibt_path_info_t	path;
930 	boolean_t	cm_grh;
931 	ibt_status_t	status;
932 
933 	IBTF_DPRINTF_L5(cmlog, "ibcm_init_reply_addr:");
934 
935 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*reply_addr))
936 
937 	/*
938 	 * sending side CM lid/gid/port num are not based on any redirect
939 	 * params. These values are set to primary RC path lid/gid/port num.
940 	 * In the future, these values can be set based on framework policy
941 	 * decisions ensuring reachability.
942 	 */
943 	reply_addr->grh_hdr.ig_sender_gid =
944 	    IBCM_PRIM_ADDS_VECT(chan_args).av_sgid;
945 	reply_addr->rcvd_addr.ia_local_lid = prim_slid;
946 	reply_addr->port_num = IBCM_PRIM_CEP_PATH(chan_args).cep_hca_port_num;
947 
948 	if (flags & IBT_OCHAN_PORT_REDIRECTED) {
949 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: "
950 		    "IBT_OCHAN_PORT_REDIRECTED specified");
951 
952 		status = ibt_index2pkey_byguid(hcap->hca_guid,
953 		    chan_args->oc_cm_cep_path->cep_hca_port_num,
954 		    chan_args->oc_cm_cep_path->cep_pkey_ix,
955 		    &reply_addr->rcvd_addr.ia_p_key);
956 
957 		if (status != IBT_SUCCESS) {
958 			IBTF_DPRINTF_L2(cmlog, "ibcm_init_rely_addr: Invalid "
959 			    "CM PKeyIx %x port_num %x",
960 			    chan_args->oc_cm_cep_path->cep_pkey_ix,
961 			    chan_args->oc_cm_cep_path->cep_hca_port_num);
962 			return (status);
963 		}
964 
965 		cm_adds = &(chan_args->oc_cm_cep_path->cep_adds_vect);
966 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: dlid = %x",
967 		    cm_adds->av_dlid);
968 
969 		reply_addr->rcvd_addr.ia_q_key = IB_GSI_QKEY;
970 		reply_addr->rcvd_addr.ia_remote_qno = 1;
971 		*cm_pkt_lt = chan_args->oc_cm_pkt_lt;
972 
973 	} else if (flags & IBT_OCHAN_REDIRECTED) {
974 		ibt_redirect_info_t	*redirect_info;
975 		ibt_hca_portinfo_t	*port_infop;
976 		uint_t			psize, nports;
977 
978 		IBTF_DPRINTF_L4(cmlog, "ibcm_init_rely_addr: "
979 		    "IBT_OCHAN_REDIRECTED specified");
980 
981 		redirect_info = chan_args->oc_cm_redirect_info;
982 
983 		if ((redirect_info->rdi_gid.gid_prefix == 0) ||
984 		    (redirect_info->rdi_gid.gid_guid == 0)) {
985 			IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
986 			    "ERROR: Re-direct GID value NOT Provided.");
987 			return (IBT_INVALID_PARAM);
988 		}
989 
990 		/* As per spec definition 1.1, it's always IB_GSI_QKEY */
991 		reply_addr->rcvd_addr.ia_q_key = redirect_info->rdi_qkey;
992 		reply_addr->rcvd_addr.ia_remote_qno = redirect_info->rdi_qpn;
993 		reply_addr->rcvd_addr.ia_p_key = redirect_info->rdi_pkey;
994 
995 		/*
996 		 * if LID is non-zero in classportinfo then use classportinfo
997 		 * fields to form CM MAD destination address.
998 		 */
999 		if (redirect_info->rdi_dlid != 0) {
1000 			status = ibtl_cm_query_hca_ports_byguid(hcap->hca_guid,
1001 			    reply_addr->port_num, &port_infop, &nports, &psize);
1002 			if ((status != IBT_SUCCESS) || (nports == 0)) {
1003 				IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
1004 				    "Query Ports Failed: %d", status);
1005 				return (status);
1006 			} else if (port_infop->p_subnet_timeout >
1007 			    IBCM_MAX_IB_PKT_LT) {
1008 				IBTF_DPRINTF_L2(cmlog, "ibcm_init_reply_addr: "
1009 				    "large subnet timeout %x port_no %x",
1010 				    port_infop->p_subnet_timeout,
1011 				    reply_addr->port_num);
1012 				ibt_free_portinfo(port_infop, psize);
1013 				return (IBT_PATH_PKT_LT_TOO_HIGH);
1014 			} else {
1015 				IBTF_DPRINTF_L3(cmlog, "ibcm_init_reply_addr: "
1016 				    "subnet timeout %x port_no %x",
1017 				    port_infop->p_subnet_timeout,
1018 				    reply_addr->port_num);
1019 
1020 				*cm_pkt_lt =
1021 				    ibt_ib2usec(min(ibcm_max_ib_mad_pkt_lt,
1022 				    port_infop->p_subnet_timeout));
1023 
1024 				ibt_free_portinfo(port_infop, psize);
1025 			}
1026 
1027 			reply_addr->rcvd_addr.ia_remote_lid =
1028 			    redirect_info->rdi_dlid;
1029 			reply_addr->rcvd_addr.ia_service_level =
1030 			    redirect_info->rdi_sl;
1031 			reply_addr->grh_exists = B_TRUE;
1032 			reply_addr->grh_hdr.ig_recver_gid =
1033 			    redirect_info->rdi_gid;
1034 			reply_addr->grh_hdr.ig_tclass =
1035 			    redirect_info->rdi_tclass;
1036 			reply_addr->grh_hdr.ig_flow_label =
1037 			    redirect_info->rdi_flow;
1038 
1039 			/* Classportinfo doesn't have hoplimit field */
1040 			reply_addr->grh_hdr.ig_hop_limit = 1;
1041 			return (IBT_SUCCESS);
1042 
1043 		} else {
1044 			ibt_path_attr_t	path_attr;
1045 			ib_gid_t	path_dgid[1];
1046 
1047 			/*
1048 			 * If GID is specified, and LID is zero in classportinfo
1049 			 * do a path lookup using specified GID, Pkey,
1050 			 * in classportinfo
1051 			 */
1052 
1053 			bzero(&path_attr, sizeof (path_attr));
1054 
1055 			path_attr.pa_dgids = &path_dgid[0];
1056 			path_attr.pa_dgids[0] = redirect_info->rdi_gid;
1057 
1058 			/*
1059 			 * use reply_addr below, as sender_gid in reply_addr
1060 			 * may have been set above based on some policy decision
1061 			 * for originating end point for CM MADs above
1062 			 */
1063 			path_attr.pa_sgid = reply_addr->grh_hdr.ig_sender_gid;
1064 			path_attr.pa_num_dgids = 1;
1065 			path_attr.pa_pkey = redirect_info->rdi_pkey;
1066 
1067 			if ((status = ibt_get_paths(ibcm_ibt_handle,
1068 			    IBT_PATH_PKEY, &path_attr, 1, &path, NULL)) !=
1069 			    IBT_SUCCESS)
1070 				return (status);
1071 
1072 			/* Initialize cm_adds */
1073 			cm_adds = &path.pi_prim_cep_path.cep_adds_vect;
1074 			*cm_pkt_lt = path.pi_prim_pkt_lt;
1075 		}
1076 
1077 	} else	{ /* cm_pkey initialized in ibt_open_rc_channel */
1078 		reply_addr->rcvd_addr.ia_q_key = IB_GSI_QKEY;
1079 		reply_addr->rcvd_addr.ia_remote_qno = 1;
1080 		*cm_pkt_lt = chan_args->oc_path->pi_prim_pkt_lt;
1081 		cm_adds = &(IBCM_PRIM_ADDS_VECT(chan_args));
1082 	}
1083 
1084 
1085 	cm_grh = cm_adds->av_send_grh;
1086 	reply_addr->grh_exists = cm_grh;
1087 
1088 	reply_addr->rcvd_addr.ia_remote_lid =
1089 	    cm_adds->av_dlid;
1090 	reply_addr->grh_hdr.ig_recver_gid =
1091 	    cm_adds->av_dgid;
1092 	reply_addr->grh_hdr.ig_flow_label =
1093 	    cm_adds->av_flow & IB_GRH_FLOW_LABEL_MASK;
1094 	reply_addr->grh_hdr.ig_tclass =
1095 	    (cm_grh == B_TRUE) ? cm_adds->av_tclass : 0;
1096 	reply_addr->grh_hdr.ig_hop_limit =
1097 	    (cm_grh == B_TRUE) ? cm_adds->av_hop : 1;
1098 	reply_addr->rcvd_addr.ia_service_level =
1099 	    cm_adds->av_srvl;
1100 
1101 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*reply_addr))
1102 
1103 	return (IBT_SUCCESS);
1104 }
1105 
1106 
1107 /*
1108  * ibt_prime_close_rc_channel()
1109  *	It allocates resources required for close channel operation, so
1110  *	ibt_close_rc_channel can be called from interrupt routine.
1111  *
1112  * INPUTS:
1113  *	channel			The address of an ibt_channel_t struct that
1114  *				specifies the channel to open.
1115  *
1116  * RETURN VALUES:
1117  *	IBT_SUCCESS	on success(or respective failure on error)
1118  *
1119  * Clients are typically expected to call this function in established state
1120  */
1121 ibt_status_t
1122 ibt_prime_close_rc_channel(ibt_channel_hdl_t channel)
1123 {
1124 	ibcm_state_data_t	*statep;
1125 	ibt_status_t		status = IBT_SUCCESS;
1126 
1127 	IBTF_DPRINTF_L3(cmlog, "ibt_prime_close_rc_channel(%p)", channel);
1128 
1129 	/* validate channel, first */
1130 	if (IBCM_INVALID_CHANNEL(channel)) {
1131 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1132 		    "invalid channel", channel);
1133 		return (IBT_CHAN_HDL_INVALID);
1134 	}
1135 
1136 	if (ibtl_cm_get_chan_type(channel) != IBT_RC_SRV) {
1137 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1138 		    "Invalid Channel type: Applicable only to RC Channel",
1139 		    channel);
1140 		return (IBT_CHAN_SRV_TYPE_INVALID);
1141 	}
1142 
1143 	/* get the statep */
1144 	IBCM_GET_CHAN_PRIVATE(channel, statep);
1145 
1146 	/*
1147 	 * This can happen, if the statep is already gone by a DREQ from
1148 	 * the remote side
1149 	 */
1150 
1151 	if (statep == NULL) {
1152 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1153 		    "statep NULL", channel);
1154 		return (IBT_SUCCESS);
1155 	}
1156 
1157 	mutex_enter(&statep->state_mutex);
1158 	IBCM_RELEASE_CHAN_PRIVATE(channel);
1159 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1160 		mutex_exit(&statep->state_mutex);
1161 		return (IBT_CHAN_STATE_INVALID);
1162 	}
1163 	IBCM_REF_CNT_INCR(statep);
1164 	IBTF_DPRINTF_L4(cmlog, "ibt_prime_close_rc_channel: chan 0x%p statep %p"
1165 	    " state %x", channel, statep, statep->state);
1166 	mutex_exit(&statep->state_mutex);
1167 
1168 	/* clients could pre-allocate dreq mad, even before connection est */
1169 	if (statep->dreq_msg == NULL)
1170 		status = ibcm_alloc_out_msg(statep->stored_reply_addr.ibmf_hdl,
1171 		    &statep->dreq_msg, MAD_METHOD_SEND);
1172 
1173 	mutex_enter(&statep->state_mutex);
1174 	IBCM_REF_CNT_DECR(statep);
1175 	mutex_exit(&statep->state_mutex);
1176 
1177 	if (status != IBT_SUCCESS) {
1178 		IBTF_DPRINTF_L2(cmlog, "ibt_prime_close_rc_channel: chan 0x%p "
1179 		    "ibcm_alloc_out_msg failed ", channel);
1180 		return (status);
1181 	}
1182 
1183 	/* If this message isn't seen then ibt_prime_close_rc_channel failed */
1184 	IBTF_DPRINTF_L5(cmlog, "ibt_prime_close_rc_channel: chan 0x%p done",
1185 	    channel);
1186 
1187 	return (IBT_SUCCESS);
1188 }
1189 
1190 /*
1191  * ibt_close_rc_channel()
1192  *	It closes an established channel.
1193  *
1194  * RETURN VALUES:
1195  *	IBT_SUCCESS	on success(or respective failure on error)
1196  */
1197 ibt_status_t
1198 ibt_close_rc_channel(ibt_channel_hdl_t channel, ibt_execution_mode_t mode,
1199     void *priv_data, ibt_priv_data_len_t priv_data_len, uint8_t *ret_status,
1200     void *ret_priv_data, ibt_priv_data_len_t *ret_priv_data_len_p)
1201 {
1202 	ibcm_state_data_t	*statep;
1203 
1204 	IBTF_DPRINTF_L3(cmlog, "ibt_close_rc_channel(%p, %x, %p, %d, %p)",
1205 	    channel, mode, priv_data, priv_data_len,
1206 	    (ret_priv_data_len_p == NULL) ? 0 : *ret_priv_data_len_p);
1207 
1208 	/* validate channel, first */
1209 	if (IBCM_INVALID_CHANNEL(channel)) {
1210 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1211 		    "invalid channel", channel);
1212 		return (IBT_CHAN_HDL_INVALID);
1213 	}
1214 
1215 	if (ibtl_cm_get_chan_type(channel) != IBT_RC_SRV) {
1216 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1217 		    "Invalid Channel type: Applicable only to RC Channel",
1218 		    channel);
1219 		return (IBT_CHAN_SRV_TYPE_INVALID);
1220 	}
1221 
1222 	if (mode == IBT_BLOCKING) {
1223 		/* valid only for BLOCKING MODE */
1224 		if ((ret_priv_data_len_p != NULL) &&
1225 		    (*ret_priv_data_len_p > IBT_DREP_PRIV_DATA_SZ)) {
1226 			IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p"
1227 			    " private data len %d is too large", channel,
1228 			    *ret_priv_data_len_p);
1229 			return (IBT_INVALID_PARAM);
1230 		}
1231 	} else if ((mode != IBT_NONBLOCKING) && (mode != IBT_NOCALLBACKS)) {
1232 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1233 		    "invalid mode %x specified", channel, mode);
1234 		return (IBT_INVALID_PARAM);
1235 	}
1236 
1237 	if (ibtl_cm_is_chan_closing(channel) ||
1238 	    ibtl_cm_is_chan_closed(channel)) {
1239 		if (ret_status)
1240 			*ret_status = IBT_CM_CLOSED_ALREADY;
1241 
1242 		/* No private data to return to the client */
1243 		if (ret_priv_data_len_p != NULL)
1244 			*ret_priv_data_len_p = 0;
1245 
1246 		if ((mode == IBT_BLOCKING) ||
1247 		    (mode == IBT_NOCALLBACKS)) {
1248 			IBCM_GET_CHAN_PRIVATE(channel, statep);
1249 			if (statep == NULL)
1250 				return (IBT_SUCCESS);
1251 			mutex_enter(&statep->state_mutex);
1252 			IBCM_RELEASE_CHAN_PRIVATE(channel);
1253 			IBCM_REF_CNT_INCR(statep);
1254 			while (statep->close_done != B_TRUE)
1255 				cv_wait(&statep->block_client_cv,
1256 				    &statep->state_mutex);
1257 			IBCM_REF_CNT_DECR(statep);
1258 			mutex_exit(&statep->state_mutex);
1259 		}
1260 
1261 		IBTF_DPRINTF_L3(cmlog, "ibt_close_rc_channel: chan 0x%p "
1262 		    "already marked for closing", channel);
1263 
1264 		return (IBT_SUCCESS);
1265 	}
1266 
1267 	/* get the statep */
1268 	IBCM_GET_CHAN_PRIVATE(channel, statep);
1269 	if (statep == NULL) {
1270 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1271 		    "statep NULL", channel);
1272 		return (IBT_CHAN_STATE_INVALID);
1273 	}
1274 
1275 	mutex_enter(&statep->state_mutex);
1276 
1277 	if (statep->dreq_msg == NULL) {
1278 		IBTF_DPRINTF_L2(cmlog, "ibt_close_rc_channel: chan 0x%p "
1279 		    "Fatal Error: dreq_msg is NULL", channel);
1280 		IBCM_RELEASE_CHAN_PRIVATE(channel);
1281 		mutex_exit(&statep->state_mutex);
1282 		return (IBT_CHAN_STATE_INVALID);
1283 	}
1284 
1285 	if ((ret_priv_data == NULL) || (ret_priv_data_len_p == NULL)) {
1286 		statep->close_ret_priv_data = NULL;
1287 		statep->close_ret_priv_data_len = NULL;
1288 	} else {
1289 		statep->close_ret_priv_data = ret_priv_data;
1290 		statep->close_ret_priv_data_len = ret_priv_data_len_p;
1291 	}
1292 
1293 	priv_data_len = min(priv_data_len, IBT_DREQ_PRIV_DATA_SZ);
1294 	if ((priv_data != NULL) && (priv_data_len > 0)) {
1295 		bcopy(priv_data, ((ibcm_dreq_msg_t *)
1296 		    IBCM_OUT_MSGP(statep->dreq_msg))->dreq_private_data,
1297 		    priv_data_len);
1298 	}
1299 	statep->close_ret_status = ret_status;
1300 
1301 	IBCM_RELEASE_CHAN_PRIVATE(channel);
1302 	IBCM_REF_CNT_INCR(statep);
1303 
1304 	if (mode != IBT_NONBLOCKING) {
1305 		return (ibcm_close_rc_channel(channel, statep, mode));
1306 	}
1307 
1308 	/* IBT_NONBLOCKING */
1309 	ibcm_close_enqueue(statep);
1310 	mutex_exit(&statep->state_mutex);
1311 
1312 	return (IBT_SUCCESS);
1313 }
1314 
1315 void
1316 ibcm_close_start(ibcm_state_data_t *statep)
1317 {
1318 	mutex_enter(&statep->state_mutex);
1319 	(void) ibcm_close_rc_channel(statep->channel, statep, IBT_NONBLOCKING);
1320 }
1321 
1322 static
1323 ibt_status_t
1324 ibcm_close_rc_channel(ibt_channel_hdl_t channel, ibcm_state_data_t *statep,
1325     ibt_execution_mode_t mode)
1326 {
1327 	ibcm_hca_info_t		*hcap;
1328 
1329 	_NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&statep->state_mutex));
1330 	ASSERT(MUTEX_HELD(&statep->state_mutex));
1331 
1332 	IBTF_DPRINTF_L3(cmlog, "ibcm_close_rc_channel: chan 0x%p statep %p",
1333 	    channel, statep);
1334 
1335 	hcap = statep->hcap;
1336 
1337 	/* HCA must have been in active state. If not, it's a client bug */
1338 	if (!IBCM_ACCESS_HCA_OK(hcap)) {
1339 		IBTF_DPRINTF_L2(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1340 		    "hcap 0x%p not active", channel, hcap);
1341 		IBCM_REF_CNT_DECR(statep);
1342 		mutex_exit(&statep->state_mutex);
1343 		return (IBT_CHAN_HDL_INVALID);
1344 	}
1345 
1346 	if (statep->state == IBCM_STATE_TRANSIENT_ESTABLISHED) {
1347 		while (statep->cep_in_rts == IBCM_BLOCK)
1348 			cv_wait(&statep->block_mad_cv, &statep->state_mutex);
1349 	}
1350 
1351 	/* Do TRANSIENT_DREQ check after TRANSIENT_ESTABLISHED check */
1352 	while (statep->state == IBCM_STATE_TRANSIENT_DREQ_SENT)
1353 		cv_wait(&statep->block_mad_cv, &statep->state_mutex);
1354 
1355 	IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1356 	    "connection state is %x", channel, statep->state);
1357 
1358 	/* If state is in pre-established states, abort the connection est */
1359 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1360 		statep->cm_retries++;	/* ensure connection trace is dumped */
1361 
1362 		/* No DREP private data possible */
1363 		if (statep->close_ret_priv_data_len != NULL)
1364 			*statep->close_ret_priv_data_len = 0;
1365 
1366 		/*
1367 		 * If waiting for a response mad, then cancel the timer,
1368 		 * and delete the connection
1369 		 */
1370 		if (statep->state == IBCM_STATE_REQ_SENT ||
1371 		    statep->state == IBCM_STATE_REP_SENT ||
1372 		    statep->state == IBCM_STATE_REP_WAIT ||
1373 		    statep->state == IBCM_STATE_MRA_REP_RCVD) {
1374 			timeout_id_t		timer_val = statep->timerid;
1375 			ibcm_conn_state_t	old_state;
1376 
1377 			IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1378 			    "chan 0x%p connection aborted in state %x", channel,
1379 			    statep->state);
1380 
1381 			old_state = statep->state;
1382 			statep->state = IBCM_STATE_DELETE;
1383 
1384 			if (mode == IBT_NONBLOCKING) {
1385 				if (taskq_dispatch(ibcm_taskq,
1386 				    ibcm_process_abort_via_taskq, statep,
1387 				    TQ_NOSLEEP) == 0) {
1388 
1389 					IBCM_REF_CNT_DECR(statep);
1390 					statep->state = old_state;
1391 					mutex_exit(&statep->state_mutex);
1392 					return (IBT_INSUFF_KERNEL_RESOURCE);
1393 				}	/* if taskq_dispatch succeeds */
1394 				/* Cancel the timer */
1395 				statep->timerid = 0;
1396 				mutex_exit(&statep->state_mutex);
1397 			} else {
1398 				/* Cancel the timer */
1399 				statep->timerid = 0;
1400 				mutex_exit(&statep->state_mutex);
1401 				(void) taskq_dispatch(ibcm_taskq,
1402 				    ibcm_process_abort_via_taskq, statep,
1403 				    TQ_SLEEP);
1404 			}
1405 
1406 			/* cancel the currently running timer */
1407 			if (timer_val != 0)
1408 				(void) untimeout(timer_val);
1409 
1410 			/* wait until cm handler returns for BLOCKING cases */
1411 			mutex_enter(&statep->state_mutex);
1412 			if ((mode == IBT_BLOCKING) ||
1413 			    (mode == IBT_NOCALLBACKS)) {
1414 				while (statep->close_done != B_TRUE)
1415 					cv_wait(&statep->block_client_cv,
1416 					    &statep->state_mutex);
1417 			}
1418 
1419 			if (statep->close_ret_status)
1420 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1421 			mutex_exit(&statep->state_mutex);
1422 
1423 			/*
1424 			 * It would ideal to post a REJ MAD, but that would
1425 			 * be non-conformance to spec. Hence, delete the state
1426 			 * data. Assuming that happens quickly, any retransmits
1427 			 * from the remote are replied by CM with reject
1428 			 * reason " no valid com id". That would stop remote
1429 			 * sending any more MADs.
1430 			 */
1431 			ibcm_delete_state_data(statep);
1432 			return (IBT_SUCCESS);
1433 
1434 		/* if CM busy in cm handler, wait until cm handler returns */
1435 		} else if (statep->state == IBCM_STATE_REQ_RCVD ||
1436 		    statep->state == IBCM_STATE_REP_RCVD ||
1437 		    statep->state == IBCM_STATE_MRA_SENT ||
1438 		    statep->state == IBCM_STATE_MRA_REP_SENT) {
1439 
1440 			/* take control of statep */
1441 			statep->abort_flag |= IBCM_ABORT_CLIENT;
1442 
1443 			IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1444 			    "chan 0x%p connection aborted in state = %x",
1445 			    channel, statep->state);
1446 
1447 			/*
1448 			 * wait until state machine modifies qp state to error,
1449 			 * including disassociating statep and QP
1450 			 */
1451 			if ((mode == IBT_BLOCKING) || (mode == IBT_NOCALLBACKS))
1452 				while (statep->close_done != B_TRUE)
1453 					cv_wait(&statep->block_client_cv,
1454 					    &statep->state_mutex);
1455 
1456 			/* a sanity setting */
1457 			if (mode == IBT_NOCALLBACKS)
1458 				statep->cm_handler = NULL;
1459 			IBCM_REF_CNT_DECR(statep);
1460 
1461 			/*
1462 			 * In rare situations, connection attempt could be
1463 			 * terminated for some other reason, before abort is
1464 			 * processed, but CM still returns ret_status as abort
1465 			 */
1466 			if (statep->close_ret_status)
1467 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1468 			mutex_exit(&statep->state_mutex);
1469 
1470 			/*
1471 			 * REJ MAD is posted by the CM state machine for this
1472 			 * case, hence state structure is deleted in the
1473 			 * state machine processing.
1474 			 */
1475 			return (IBT_SUCCESS);
1476 
1477 		} else if ((statep->state == IBCM_STATE_TIMEWAIT) ||
1478 		    (statep->state == IBCM_STATE_DELETE)) {
1479 
1480 			/* State already in timewait, so no return priv data */
1481 			IBCM_REF_CNT_DECR(statep);
1482 
1483 			/* The teardown has already been done */
1484 			if (statep->close_ret_status)
1485 				*statep->close_ret_status =
1486 				    IBT_CM_CLOSED_ALREADY;
1487 			mutex_exit(&statep->state_mutex);
1488 
1489 			return (IBT_SUCCESS);
1490 
1491 		} else if ((statep->state == IBCM_STATE_DREQ_RCVD) ||
1492 		    (statep->state == IBCM_STATE_DREQ_SENT) ||
1493 		    (statep->state == IBCM_STATE_DREP_RCVD) ||
1494 		    ((statep->state == IBCM_STATE_TIMED_OUT) &&
1495 		    (statep->timedout_state == IBCM_STATE_DREQ_SENT))) {
1496 
1497 			/*
1498 			 * Either the remote or local client has already
1499 			 * initiated the teardown.  IBCM_STATE_DREP_RCVD is
1500 			 * possible, if CM initiated teardown without client's
1501 			 * knowledge, for stale handling, etc.,
1502 			 */
1503 			if (mode == IBT_NOCALLBACKS) {
1504 				if (statep->close_nocb_state == IBCM_UNBLOCK) {
1505 					statep->close_nocb_state = IBCM_FAIL;
1506 					/* enable free qp after return */
1507 					ibtl_cm_chan_is_closing(
1508 					    statep->channel);
1509 				} else while (statep->close_nocb_state ==
1510 				    IBCM_BLOCK)
1511 					cv_wait(&statep->block_client_cv,
1512 					    &statep->state_mutex);
1513 				statep->cm_handler = NULL; /* sanity setting */
1514 				if (statep->close_ret_status)
1515 					*statep->close_ret_status =
1516 					    IBT_CM_CLOSED_ALREADY;
1517 			} else if (mode == IBT_BLOCKING) {
1518 				/* wait until state is moved to timewait */
1519 				while (statep->close_done != B_TRUE)
1520 					cv_wait(&statep->block_client_cv,
1521 					    &statep->state_mutex);
1522 			}
1523 
1524 			IBCM_REF_CNT_DECR(statep);
1525 			mutex_exit(&statep->state_mutex);
1526 
1527 			/* ret_status is set in state machine code */
1528 			return (IBT_SUCCESS);
1529 
1530 		} else if (statep->state == IBCM_STATE_TIMED_OUT) {
1531 
1532 			if ((mode == IBT_BLOCKING) ||
1533 			    (mode == IBT_NOCALLBACKS)) {
1534 
1535 				/*
1536 				 * wait until cm handler invocation and
1537 				 * disassociation between statep and channel
1538 				 * is complete
1539 				 */
1540 				while (statep->close_done != B_TRUE)
1541 					cv_wait(&statep->block_client_cv,
1542 					    &statep->state_mutex);
1543 			}
1544 
1545 			if (statep->close_ret_status)
1546 				*statep->close_ret_status = IBT_CM_CLOSED_ABORT;
1547 			IBCM_REF_CNT_DECR(statep);
1548 			mutex_exit(&statep->state_mutex);
1549 
1550 			return (IBT_SUCCESS);
1551 		} else {
1552 			IBCM_REF_CNT_DECR(statep);
1553 			mutex_exit(&statep->state_mutex);
1554 
1555 			return (IBT_CM_FAILURE);
1556 		}
1557 	}
1558 
1559 	ASSERT(statep->close_nocb_state != IBCM_BLOCK);
1560 
1561 	if (mode == IBT_NOCALLBACKS) {
1562 		statep->close_nocb_state = IBCM_FAIL;
1563 		statep->cm_handler = NULL;
1564 		ibtl_cm_chan_is_closing(statep->channel);
1565 		IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: "
1566 		    "NOCALLBACKS on in statep = %p", statep);
1567 	}
1568 
1569 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1570 		goto lost_race;
1571 	}
1572 
1573 	/*
1574 	 * Cancel/wait for any pending ibt_set_alt_path, and
1575 	 * release state mutex
1576 	 */
1577 	ibcm_sync_lapr_idle(statep);
1578 
1579 	ibcm_close_enter();
1580 
1581 	mutex_enter(&statep->state_mutex);
1582 	if (statep->state != IBCM_STATE_ESTABLISHED) {
1583 		ibcm_close_exit();
1584 		goto lost_race;
1585 	}
1586 
1587 	statep->state = IBCM_STATE_TRANSIENT_DREQ_SENT;
1588 	statep->timerid = 0;
1589 	statep->close_done = B_FALSE;
1590 	statep->close_flow = 1;
1591 	mutex_exit(&statep->state_mutex);
1592 
1593 	ibcm_post_dreq_mad(statep);
1594 
1595 	mutex_enter(&statep->state_mutex);
1596 
1597 lost_race:
1598 	if (mode == IBT_BLOCKING) {
1599 
1600 		/* wait for DREP */
1601 		while (statep->close_done != B_TRUE)
1602 			cv_wait(&statep->block_client_cv,
1603 			    &statep->state_mutex);
1604 
1605 		IBTF_DPRINTF_L4(cmlog, "ibcm_close_rc_channel: chan 0x%p "
1606 		    "done blocking", channel);
1607 	}
1608 
1609 	IBCM_REF_CNT_DECR(statep);
1610 	mutex_exit(&statep->state_mutex);
1611 
1612 	/* If this message isn't seen then ibt_close_rc_channel failed */
1613 	IBTF_DPRINTF_L5(cmlog, "ibcm_close_rc_channel: chan 0x%p done",
1614 	    channel);
1615 
1616 	return (IBT_SUCCESS);
1617 }
1618 
1619 ibt_status_t
1620 ibt_recycle_rc(ibt_channel_hdl_t rc_chan, ibt_cep_flags_t control,
1621     uint8_t hca_port_num, ibt_recycle_handler_t func, void *arg)
1622 {
1623 	ibcm_state_data_t		*statep;
1624 	ibcm_taskq_recycle_arg_t	*ibcm_tq_recycle_arg;
1625 	ibt_qp_query_attr_t		qp_attr;
1626 	ibt_status_t			retval;
1627 
1628 	IBTF_DPRINTF_L3(cmlog, "ibt_recycle_rc (%p, 0x%X, %d, %p, %p)", rc_chan,
1629 	    control, hca_port_num, func, arg);
1630 
1631 	if (IBCM_INVALID_CHANNEL(rc_chan)) {
1632 		IBTF_DPRINTF_L2(cmlog, "ibt_recycle_rc: invalid channel");
1633 		return (IBT_CHAN_HDL_INVALID);
1634 	}
1635 
1636 	/* check qp state */
1637 	retval = ibt_query_qp(rc_chan, &qp_attr);
1638 
1639 	if (retval != IBT_SUCCESS)
1640 		return (retval);
1641 
1642 	if (qp_attr.qp_info.qp_trans != IBT_RC_SRV)
1643 		return (IBT_CHAN_SRV_TYPE_INVALID);
1644 
1645 	if (qp_attr.qp_info.qp_state != IBT_STATE_ERROR)
1646 		return (IBT_CHAN_STATE_INVALID);
1647 
1648 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*ibcm_tq_recycle_arg))
1649 
1650 	ibcm_tq_recycle_arg = kmem_alloc(sizeof (ibcm_taskq_recycle_arg_t),
1651 	    KM_SLEEP);
1652 
1653 	ibcm_tq_recycle_arg->rc_chan		= rc_chan;
1654 	ibcm_tq_recycle_arg->control		= control;
1655 	ibcm_tq_recycle_arg->hca_port_num	= hca_port_num;
1656 	ibcm_tq_recycle_arg->func		= func;
1657 	ibcm_tq_recycle_arg->arg		= arg;
1658 
1659 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*ibcm_tq_recycle_arg))
1660 
1661 	IBCM_GET_CHAN_PRIVATE(rc_chan, statep);
1662 
1663 	/*
1664 	 * If non-blocking ie., func specified and channel has not yet completed
1665 	 * the timewait, then schedule the work for later
1666 	 */
1667 	if ((func != NULL) && (statep != NULL)) {
1668 		IBCM_RELEASE_CHAN_PRIVATE(rc_chan);
1669 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(statep->recycle_arg))
1670 		statep->recycle_arg = ibcm_tq_recycle_arg;
1671 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(statep->recycle_arg))
1672 		return (IBT_SUCCESS);
1673 	}
1674 
1675 	/*
1676 	 * if blocking ie., func specified, and channel has not yet completed
1677 	 * the timewait, then block until the channel completes the timewait
1678 	 */
1679 	if (statep != NULL)
1680 		IBCM_RELEASE_CHAN_PRIVATE(rc_chan);
1681 	IBCM_WAIT_CHAN_PRIVATE(rc_chan);
1682 
1683 	if (func) {	/* NON BLOCKING case. Taskq for QP state change */
1684 		(void) taskq_dispatch(ibcm_taskq, ibcm_process_rc_recycle,
1685 		    ibcm_tq_recycle_arg, TQ_SLEEP);
1686 		return (IBT_SUCCESS);
1687 	} else	/* BLOCKING case */
1688 		return (ibcm_process_rc_recycle_ret(ibcm_tq_recycle_arg));
1689 }
1690 
1691 void
1692 ibcm_process_rc_recycle(void *recycle_arg)
1693 {
1694 	(void) ibcm_process_rc_recycle_ret(recycle_arg);
1695 }
1696 
1697 static ibt_status_t
1698 ibcm_process_rc_recycle_ret(void *recycle_arg)
1699 {
1700 	ibt_qp_info_t			qp_info;
1701 	ibt_status_t			ibt_status = IBT_SUCCESS;
1702 	ibt_cep_modify_flags_t		cep_flags;
1703 	ibt_qp_query_attr_t		qp_attr;
1704 	ibcm_taskq_recycle_arg_t	*ibcm_tq_recycle_arg =
1705 	    (ibcm_taskq_recycle_arg_t *)recycle_arg;
1706 
1707 	/* QP must have been in error state */
1708 	ibt_status = ibt_query_qp(ibcm_tq_recycle_arg->rc_chan, &qp_attr);
1709 	if (ibt_status != IBT_SUCCESS)
1710 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1711 		    "chanp %p ibt_query_qp() = %d",
1712 		    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1713 	else {
1714 		/* perform the QP state change from ERROR to RESET */
1715 		bzero(&qp_info, sizeof (qp_info));
1716 
1717 		qp_info.qp_trans = IBT_RC_SRV;
1718 		qp_info.qp_state = IBT_STATE_RESET;
1719 
1720 		/* Call modify_qp to move to RESET state */
1721 		ibt_status = ibt_modify_qp(ibcm_tq_recycle_arg->rc_chan,
1722 		    IBT_CEP_SET_STATE, &qp_info, NULL);
1723 
1724 		if (ibt_status != IBT_SUCCESS)
1725 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1726 			    "chanp %p ibt_modify_qp() = %d for ERROR to RESET",
1727 			    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1728 	}
1729 
1730 	if (ibt_status == IBT_SUCCESS) {
1731 
1732 		qp_info.qp_state = IBT_STATE_INIT;
1733 
1734 		/* set flags for all mandatory args from RESET to INIT */
1735 		cep_flags = IBT_CEP_SET_STATE | IBT_CEP_SET_PORT;
1736 		cep_flags |= IBT_CEP_SET_RDMA_R | IBT_CEP_SET_RDMA_W;
1737 		cep_flags |= IBT_CEP_SET_ATOMIC;
1738 
1739 		qp_info.qp_transport.rc.rc_path.cep_hca_port_num =
1740 		    ibcm_tq_recycle_arg->hca_port_num;
1741 		qp_info.qp_flags |=
1742 		    ibcm_tq_recycle_arg->control & IBT_CEP_RDMA_RD;
1743 		qp_info.qp_flags |=
1744 		    ibcm_tq_recycle_arg->control & IBT_CEP_RDMA_WR;
1745 		qp_info.qp_flags |=
1746 		    ibcm_tq_recycle_arg->control & IBT_CEP_ATOMIC;
1747 
1748 		/* Always use the existing pkey */
1749 		qp_info.qp_transport.rc.rc_path.cep_pkey_ix =
1750 		    qp_attr. qp_info.qp_transport.rc.rc_path.cep_pkey_ix;
1751 
1752 		/* Call modify_qp to move to INIT state */
1753 		ibt_status = ibt_modify_qp(ibcm_tq_recycle_arg->rc_chan,
1754 		    cep_flags, &qp_info, NULL);
1755 
1756 		if (ibt_status != IBT_SUCCESS)
1757 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_rc_recycle_ret: "
1758 			    "chanp %p ibt_modify_qp() = %d for RESET to INIT",
1759 			    ibcm_tq_recycle_arg->rc_chan, ibt_status);
1760 	}
1761 
1762 	/* Change the QP CM state to indicate QP being re-used */
1763 	if (ibt_status == IBT_SUCCESS)
1764 		ibtl_cm_chan_is_reused(ibcm_tq_recycle_arg->rc_chan);
1765 
1766 	/* Call func, if defined */
1767 	if (ibcm_tq_recycle_arg->func)
1768 		(*(ibcm_tq_recycle_arg->func))(ibt_status,
1769 		    ibcm_tq_recycle_arg->arg);
1770 
1771 	kmem_free(ibcm_tq_recycle_arg, sizeof (ibcm_taskq_recycle_arg_t));
1772 
1773 	return (ibt_status);
1774 }
1775 
1776 static void
1777 ibcm_process_abort_via_taskq(void *args)
1778 {
1779 	ibcm_state_data_t	*statep = (ibcm_state_data_t *)args;
1780 
1781 	ibcm_process_abort(statep);
1782 	mutex_enter(&statep->state_mutex);
1783 	IBCM_REF_CNT_DECR(statep);
1784 	mutex_exit(&statep->state_mutex);
1785 }
1786 
1787 /*
1788  * Local UD CM Handler's private data, used during ibt_request_ud_dest() in
1789  * Non-Blocking mode operations.
1790  */
1791 typedef struct ibcm_local_handler_s {
1792 	ibt_cm_ud_handler_t	actual_cm_handler;
1793 	void			*actual_cm_private;
1794 	ibt_ud_dest_t		*dest_hdl;
1795 } ibcm_local_handler_t;
1796 
1797 _NOTE(READ_ONLY_DATA(ibcm_local_handler_s))
1798 
1799 /*
1800  * Local UD CM Handler, used when ibt_alloc_ud_dest() is issued in
1801  * NON-Blocking mode.
1802  *
1803  * Out here, we update the UD Destination handle with
1804  * the obtained DQPN and QKey (from SIDR REP) and invokes actual client
1805  * handler that was specified by the client.
1806  */
1807 static ibt_cm_status_t
1808 ibcm_local_cm_handler(void *priv, ibt_cm_ud_event_t *event,
1809     ibt_cm_ud_return_args_t *ret_args, void *priv_data, ibt_priv_data_len_t len)
1810 {
1811 	ibcm_local_handler_t	*handler_priv = (ibcm_local_handler_t *)priv;
1812 
1813 	IBTF_DPRINTF_L4(cmlog, "ibcm_local_cm_handler: event %d",
1814 	    event->cm_type);
1815 
1816 	ASSERT(handler_priv != NULL);
1817 
1818 	switch (event->cm_type) {
1819 	case IBT_CM_UD_EVENT_SIDR_REP:
1820 		/* Update QPN & QKey from event into destination handle. */
1821 		if (handler_priv->dest_hdl != NULL) {
1822 			handler_priv->dest_hdl->ud_dst_qpn =
1823 			    event->cm_event.sidr_rep.srep_remote_qpn;
1824 			handler_priv->dest_hdl->ud_qkey =
1825 			    event->cm_event.sidr_rep.srep_remote_qkey;
1826 		}
1827 
1828 		/* Invoke the client handler - inform only, so ignore retval */
1829 		(void) handler_priv->actual_cm_handler(
1830 		    handler_priv->actual_cm_private, event, ret_args, priv_data,
1831 		    len);
1832 
1833 		/* Free memory allocated for local handler's private data. */
1834 		if (handler_priv != NULL)
1835 			kmem_free(handler_priv, sizeof (*handler_priv));
1836 
1837 		break;
1838 	default:
1839 		IBTF_DPRINTF_L2(cmlog, "ibcm_local_cm_handler: ERROR");
1840 		break;
1841 	}
1842 
1843 	return (IBT_CM_ACCEPT);
1844 }
1845 
1846 
1847 /* Validate the input UD destination attributes.  */
1848 static ibt_status_t
1849 ibcm_validate_dqpn_data(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
1850     ibt_ud_returns_t *ret_args)
1851 {
1852 	/* cm handler must always be specified */
1853 	if (mode == IBT_NONBLOCKING && attr->ud_cm_handler == NULL) {
1854 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1855 		    "CM handler is not specified ");
1856 		return (IBT_INVALID_PARAM);
1857 	}
1858 
1859 	if (mode == IBT_NONBLOCKING) {
1860 		if (ret_args != NULL) {
1861 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1862 			    "ret_args should be NULL when called in "
1863 			    "non-blocking mode");
1864 			return (IBT_INVALID_PARAM);
1865 		}
1866 	} else if (mode == IBT_BLOCKING) {
1867 		if (ret_args == NULL) {
1868 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1869 			    "ret_args should be Non-NULL when called in "
1870 			    "blocking mode");
1871 			return (IBT_INVALID_PARAM);
1872 		}
1873 	} else {
1874 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1875 		    "invalid mode %x specified ", mode);
1876 		return (IBT_INVALID_PARAM);
1877 	}
1878 
1879 	if (attr->ud_sid == 0) {
1880 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1881 		    "ServiceID must be specified. ");
1882 		return (IBT_INVALID_PARAM);
1883 	}
1884 
1885 	if (attr->ud_addr == NULL) {
1886 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: "
1887 		    "Address Info NULL");
1888 		return (IBT_INVALID_PARAM);
1889 	}
1890 
1891 	/* Validate SGID */
1892 	if ((attr->ud_addr->av_sgid.gid_prefix == 0) ||
1893 	    (attr->ud_addr->av_sgid.gid_guid == 0)) {
1894 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: Invalid SGID");
1895 		return (IBT_INVALID_PARAM);
1896 	}
1897 	IBTF_DPRINTF_L3(cmlog, "ibcm_validate_dqpn_data: SGID<%llX:%llX>",
1898 	    attr->ud_addr->av_sgid.gid_prefix,
1899 	    attr->ud_addr->av_sgid.gid_guid);
1900 
1901 	/* Validate DGID */
1902 	if ((attr->ud_addr->av_dgid.gid_prefix == 0) ||
1903 	    (attr->ud_addr->av_dgid.gid_guid == 0)) {
1904 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_dqpn_data: Invalid DGID");
1905 		return (IBT_INVALID_PARAM);
1906 	}
1907 	IBTF_DPRINTF_L3(cmlog, "ibcm_validate_dqpn_data: DGID<%llX:%llX>",
1908 	    attr->ud_addr->av_dgid.gid_prefix,
1909 	    attr->ud_addr->av_dgid.gid_guid);
1910 
1911 	return (IBT_SUCCESS);
1912 }
1913 
1914 
1915 /* Perform SIDR to retrieve DQPN and QKey.  */
1916 static ibt_status_t
1917 ibcm_ud_get_dqpn(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
1918     ibt_ud_returns_t *ret_args)
1919 {
1920 	ibt_status_t		retval;
1921 	ib_pkey_t		ud_pkey;
1922 	ibmf_handle_t		ibmf_hdl;
1923 	ibmf_msg_t		*ibmf_msg;
1924 	ibcm_hca_info_t		*hcap;
1925 	ibcm_sidr_req_msg_t	*sidr_req_msgp;
1926 	ibcm_ud_state_data_t	*ud_statep;
1927 	ibtl_cm_hca_port_t	port;
1928 	ibcm_sidr_srch_t	sidr_entry;
1929 	ibcm_qp_list_t		*cm_qp_entry;
1930 
1931 	/* Retrieve HCA GUID value from the available SGID info. */
1932 	retval = ibtl_cm_get_hca_port(attr->ud_addr->av_sgid, 0, &port);
1933 	if ((retval != IBT_SUCCESS) || (port.hp_port == 0)) {
1934 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1935 		    "ibtl_cm_get_hca_port failed: %d", retval);
1936 		return (retval);
1937 	}
1938 
1939 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: "
1940 	    "HCA GUID:%llX, port_num:%d", port.hp_hca_guid, port.hp_port);
1941 
1942 	/* Lookup the HCA info for this GUID */
1943 	if ((hcap = ibcm_find_hca_entry(port.hp_hca_guid)) == NULL) {
1944 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: hcap is NULL");
1945 		return (IBT_HCA_INVALID);
1946 	}
1947 
1948 	/* Return failure if the HCA device or Port is not operational */
1949 
1950 	if ((retval = ibt_get_port_state_byguid(port.hp_hca_guid, port.hp_port,
1951 	    NULL, NULL)) != IBT_SUCCESS) {
1952 		/* Device Port is not in good state, don't use it. */
1953 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: Invalid "
1954 		    "port specified or port not active");
1955 		ibcm_dec_hca_acc_cnt(hcap);
1956 		return (retval);
1957 	}
1958 
1959 	retval = ibt_index2pkey_byguid(port.hp_hca_guid, port.hp_port,
1960 	    attr->ud_pkey_ix, &ud_pkey);
1961 	if (retval != IBT_SUCCESS) {
1962 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1963 		    "Failed to convert index2pkey: %d", retval);
1964 		ibcm_dec_hca_acc_cnt(hcap);
1965 		return (retval);
1966 	}
1967 
1968 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(sidr_entry))
1969 
1970 	/* Allocate a new request id */
1971 	if (ibcm_alloc_reqid(hcap, &sidr_entry.srch_req_id) == IBCM_FAILURE) {
1972 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1973 		    "no req id available");
1974 		ibcm_dec_hca_acc_cnt(hcap);
1975 		return (IBT_INSUFF_KERNEL_RESOURCE);
1976 	}
1977 
1978 	if ((hcap->hca_port_info[port.hp_port - 1].port_ibmf_hdl == NULL) &&
1979 	    ((retval = ibcm_hca_reinit_port(hcap, port.hp_port - 1))
1980 	    != IBT_SUCCESS)) {
1981 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: "
1982 		    "ibmf reg or callback setup failed during re-initialize");
1983 		return (retval);
1984 	}
1985 
1986 	ibmf_hdl = hcap->hca_port_info[port.hp_port - 1].port_ibmf_hdl;
1987 
1988 	/* find the ibmf QP to post the SIDR REQ */
1989 	if ((cm_qp_entry = ibcm_find_qp(hcap, port.hp_port, ud_pkey)) ==
1990 	    NULL) {
1991 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: IBMF QP allocation"
1992 		    " failed");
1993 		ibcm_dec_hca_acc_cnt(hcap);
1994 		return (IBT_INSUFF_RESOURCE);
1995 	}
1996 
1997 	if ((retval = ibcm_alloc_out_msg(ibmf_hdl, &ibmf_msg, MAD_METHOD_SEND))
1998 	    != IBT_SUCCESS) {
1999 		IBTF_DPRINTF_L2(cmlog, "ibcm_ud_get_dqpn: IBMF MSG allocation"
2000 		    " failed");
2001 		ibcm_release_qp(cm_qp_entry);
2002 		ibcm_dec_hca_acc_cnt(hcap);
2003 		return (retval);
2004 	}
2005 
2006 	sidr_entry.srch_lid = port.hp_base_lid;
2007 	sidr_entry.srch_gid = attr->ud_addr->av_sgid;
2008 	sidr_entry.srch_grh_exists = attr->ud_addr->av_send_grh;
2009 	sidr_entry.srch_mode = IBCM_ACTIVE_MODE;
2010 
2011 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(sidr_entry))
2012 
2013 	/* do various allocations needed here */
2014 	rw_enter(&hcap->hca_sidr_list_lock, RW_WRITER);
2015 
2016 	(void) ibcm_find_sidr_entry(&sidr_entry, hcap, &ud_statep,
2017 	    IBCM_FLAG_ADD);
2018 	rw_exit(&hcap->hca_sidr_list_lock);
2019 
2020 	/* Increment hca's resource count */
2021 	ibcm_inc_hca_res_cnt(hcap);
2022 
2023 	/* After a resource created on hca, no need to hold the acc cnt */
2024 	ibcm_dec_hca_acc_cnt(hcap);
2025 
2026 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*ud_statep))
2027 
2028 	/* Initialize some ud_statep fields */
2029 	ud_statep->ud_stored_msg = ibmf_msg;
2030 	ud_statep->ud_svc_id = attr->ud_sid;
2031 	ud_statep->ud_pkt_life_time =
2032 	    ibt_ib2usec(attr->ud_pkt_lt);
2033 	ud_statep->ud_stored_reply_addr.cm_qp_entry = cm_qp_entry;
2034 
2035 	/* set remaining retry cnt */
2036 	ud_statep->ud_remaining_retry_cnt = ud_statep->ud_max_cm_retries;
2037 
2038 	/*
2039 	 * Get UD handler and corresponding args which is pass it back
2040 	 * as first argument for the handler.
2041 	 */
2042 	ud_statep->ud_state_cm_private = attr->ud_cm_private;
2043 
2044 	if (mode == IBT_BLOCKING)
2045 		ud_statep->ud_return_data = ret_args;
2046 	else
2047 		ud_statep->ud_cm_handler = attr->ud_cm_handler;
2048 
2049 	/* Initialize the fields of ud_statep->ud_stored_reply_addr */
2050 	ud_statep->ud_stored_reply_addr.grh_exists = attr->ud_addr->av_send_grh;
2051 	ud_statep->ud_stored_reply_addr.ibmf_hdl = ibmf_hdl;
2052 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_hop_limit =
2053 	    attr->ud_addr->av_hop;
2054 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_sender_gid =
2055 	    attr->ud_addr->av_sgid;
2056 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_recver_gid =
2057 	    attr->ud_addr->av_dgid;
2058 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_tclass =
2059 	    attr->ud_addr->av_tclass;
2060 	ud_statep->ud_stored_reply_addr.grh_hdr.ig_flow_label =
2061 	    attr->ud_addr->av_flow & IB_GRH_FLOW_LABEL_MASK;
2062 
2063 	/* needs to be derived based on the base LID and path bits */
2064 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_local_lid =
2065 	    port.hp_base_lid;
2066 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_remote_lid =
2067 	    attr->ud_addr->av_dlid;
2068 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_p_key = ud_pkey;
2069 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_q_key = IB_GSI_QKEY;
2070 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_service_level =
2071 	    attr->ud_addr->av_srvl;
2072 
2073 	/*
2074 	 * This may be enchanced later, to use a remote qno based on past
2075 	 * redirect rej mad responses. This would be the place to specify
2076 	 * appropriate remote qno
2077 	 */
2078 	ud_statep->ud_stored_reply_addr.rcvd_addr.ia_remote_qno = 1;
2079 
2080 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sidr_req_msgp))
2081 
2082 	/* Initialize the SIDR REQ message fields */
2083 	sidr_req_msgp =
2084 	    (ibcm_sidr_req_msg_t *)IBCM_OUT_MSGP(ud_statep->ud_stored_msg);
2085 
2086 	sidr_req_msgp->sidr_req_request_id = h2b32(ud_statep->ud_req_id);
2087 	sidr_req_msgp->sidr_req_service_id = h2b64(attr->ud_sid);
2088 	sidr_req_msgp->sidr_req_pkey = h2b16(ud_pkey);
2089 	IBCM_OUT_HDRP(ud_statep->ud_stored_msg)->AttributeID =
2090 	    h2b16(IBCM_INCOMING_SIDR_REQ + IBCM_ATTR_BASE_ID);
2091 
2092 	if ((attr->ud_priv_data != NULL) && (attr->ud_priv_data_len > 0)) {
2093 		bcopy(attr->ud_priv_data, sidr_req_msgp->sidr_req_private_data,
2094 		    min(attr->ud_priv_data_len, IBT_SIDR_REQ_PRIV_DATA_SZ));
2095 	}
2096 
2097 	/* Send out the SIDR REQ message */
2098 	ud_statep->ud_state = IBCM_STATE_SIDR_REQ_SENT;
2099 	ud_statep->ud_timer_stored_state = IBCM_STATE_SIDR_REQ_SENT;
2100 	IBCM_UD_REF_CNT_INCR(ud_statep); /* for non-blocking SIDR REQ post */
2101 	ud_statep->ud_timer_value = ibt_ib2usec(ibcm_max_sidr_rep_proctime) +
2102 	    (ud_statep->ud_pkt_life_time * 2);
2103 
2104 	IBCM_OUT_HDRP(ud_statep->ud_stored_msg)->TransactionID =
2105 	    h2b64(ibcm_generate_tranid(IBCM_INCOMING_SIDR_REQ,
2106 	    ud_statep->ud_req_id, 0));
2107 
2108 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: timer_value in HZ = %x",
2109 	    ud_statep->ud_timer_value);
2110 
2111 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*ud_statep))
2112 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sidr_req_msgp))
2113 
2114 	ibcm_post_ud_mad(ud_statep, ud_statep->ud_stored_msg,
2115 	    ibcm_post_sidr_req_complete, ud_statep);
2116 
2117 	mutex_enter(&ud_statep->ud_state_mutex);
2118 
2119 	/* Wait for SIDR_REP */
2120 	if (mode == IBT_BLOCKING) {
2121 		IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: blocking");
2122 
2123 		while (ud_statep->ud_blocking_done != B_TRUE) {
2124 			cv_wait(&ud_statep->ud_block_client_cv,
2125 			    &ud_statep->ud_state_mutex);
2126 		}
2127 
2128 		IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: finished blocking");
2129 
2130 		if (ret_args->ud_status == IBT_CM_SREP_QPN_VALID) {
2131 			IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: DQPN = %x, "
2132 			    "status = %x, QKey = %x", ret_args->ud_dqpn,
2133 			    ret_args->ud_status, ret_args->ud_qkey);
2134 
2135 		} else {
2136 			IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: Status<%x>",
2137 			    ret_args->ud_status);
2138 			retval = IBT_CM_FAILURE;
2139 		}
2140 	}
2141 
2142 	IBCM_UD_REF_CNT_DECR(ud_statep);
2143 	mutex_exit(&ud_statep->ud_state_mutex);
2144 
2145 	IBTF_DPRINTF_L4(cmlog, "ibcm_ud_get_dqpn: done");
2146 
2147 	return (retval);
2148 }
2149 
2150 
2151 /*
2152  * Function:
2153  *	ibt_request_ud_dest
2154  * Input:
2155  *	ud_dest		A previously allocated UD destination handle.
2156  *	mode		This function can execute in blocking or non blocking
2157  *			modes.
2158  *	attr		UD destination attributes to be modified.
2159  * Output:
2160  *	ud_ret_args	If the function is called in blocking mode, ud_ret_args
2161  *			should be a pointer to an ibt_ud_returns_t struct.
2162  * Returns:
2163  *	IBT_SUCCESS
2164  * Description:
2165  *	Modify a previously allocated UD destination handle based on the
2166  *	results of doing the SIDR protocol.
2167  */
2168 ibt_status_t
2169 ibt_request_ud_dest(ibt_ud_dest_hdl_t ud_dest, ibt_execution_mode_t mode,
2170     ibt_ud_dest_attr_t *attr, ibt_ud_returns_t *ud_ret_args)
2171 {
2172 	ibt_status_t		retval;
2173 	ibt_ud_dest_t		*ud_destp;
2174 	ibcm_local_handler_t	*local_handler_priv = NULL;
2175 
2176 	IBTF_DPRINTF_L3(cmlog, "ibt_request_ud_dest(%p, %x, %p, %p)",
2177 	    ud_dest, mode, attr, ud_ret_args);
2178 
2179 	retval = ibcm_validate_dqpn_data(attr, mode, ud_ret_args);
2180 	if (retval != IBT_SUCCESS) {
2181 		return (retval);
2182 	}
2183 
2184 	ud_destp = ud_dest;
2185 
2186 	/* Allocate an Address handle. */
2187 	retval = ibt_modify_ah(ud_destp->ud_dest_hca, ud_destp->ud_ah,
2188 	    attr->ud_addr);
2189 	if (retval != IBT_SUCCESS) {
2190 		IBTF_DPRINTF_L2(cmlog, "ibt_request_ud_dest: "
2191 		    "Address Handle Modification failed: %d", retval);
2192 		return (retval);
2193 	}
2194 
2195 	if (mode == IBT_NONBLOCKING) {
2196 		/*
2197 		 * In NON-BLOCKING mode, and we need to update the destination
2198 		 * handle with the DQPN and QKey that are obtained from
2199 		 * SIDR REP, hook-up our own handler, so that we can catch
2200 		 * the event, and we ourselves call the actual client's
2201 		 * ud_cm_handler, in our handler.
2202 		 */
2203 
2204 		/* Allocate memory for local handler's private data. */
2205 		local_handler_priv =
2206 		    kmem_alloc(sizeof (*local_handler_priv), KM_SLEEP);
2207 
2208 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*local_handler_priv))
2209 
2210 		local_handler_priv->actual_cm_handler = attr->ud_cm_handler;
2211 		local_handler_priv->actual_cm_private = attr->ud_cm_private;
2212 		local_handler_priv->dest_hdl = ud_destp;
2213 
2214 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*local_handler_priv))
2215 
2216 		attr->ud_cm_handler = ibcm_local_cm_handler;
2217 		attr->ud_cm_private = local_handler_priv;
2218 	}
2219 
2220 	/* In order to get DQPN and Destination QKey, perform SIDR */
2221 	retval = ibcm_ud_get_dqpn(attr, mode, ud_ret_args);
2222 	if (retval != IBT_SUCCESS) {
2223 		IBTF_DPRINTF_L2(cmlog, "ibt_request_ud_dest: "
2224 		    "Failed to get DQPN: %d", retval);
2225 
2226 		/* Free memory allocated for local handler's private data. */
2227 		if (local_handler_priv != NULL)
2228 			kmem_free(local_handler_priv,
2229 			    sizeof (*local_handler_priv));
2230 		return (retval);
2231 	}
2232 
2233 	/*
2234 	 * Fill in the dqpn and dqkey as obtained from ud_ret_args,
2235 	 * values will be valid only on BLOCKING mode.
2236 	 */
2237 	if (mode == IBT_BLOCKING) {
2238 		ud_destp->ud_dst_qpn = ud_ret_args->ud_dqpn;
2239 		ud_destp->ud_qkey = ud_ret_args->ud_qkey;
2240 	}
2241 
2242 	return (retval);
2243 }
2244 
2245 /*
2246  * Function:
2247  *	ibt_ud_get_dqpn
2248  * Input:
2249  *	attr		A pointer to an ibt_ud_dest_attr_t struct that are
2250  *			required for SIDR REQ message. Not specified attributes
2251  *			should be set to "NULL" or "0".
2252  *			ud_sid, ud_addr and ud_pkt_lt must be specified.
2253  *	mode		This function can execute in blocking or non blocking
2254  *			modes.
2255  * Output:
2256  *	returns		If the function is called in blocking mode, returns
2257  *			should be a pointer to an ibt_ud_returns_t struct.
2258  * Return:
2259  *	IBT_SUCCESS	on success or respective failure on error.
2260  * Description:
2261  *	Finds the destination QPN at the specified destination that the
2262  *	specified service can be reached on. The IBTF CM initiates the
2263  *	service ID resolution protocol (SIDR) to determine a destination QPN.
2264  *
2265  * NOTE: SIDR_REQ is initiated from active side.
2266  */
2267 ibt_status_t
2268 ibt_ud_get_dqpn(ibt_ud_dest_attr_t *attr, ibt_execution_mode_t mode,
2269     ibt_ud_returns_t *returns)
2270 {
2271 	ibt_status_t		retval;
2272 
2273 	IBTF_DPRINTF_L3(cmlog, "ibt_ud_get_dqpn(%p, %x, %p)",
2274 	    attr, mode, returns);
2275 
2276 	retval = ibcm_validate_dqpn_data(attr, mode, returns);
2277 	if (retval != IBT_SUCCESS) {
2278 		return (retval);
2279 	}
2280 
2281 	return (ibcm_ud_get_dqpn(attr, mode, returns));
2282 }
2283 
2284 
2285 /*
2286  * ibt_cm_delay:
2287  *	A client CM handler function can call this function
2288  *	to extend its response time to a CM event.
2289  * INPUTS:
2290  *	flags		Indicates what CM message processing is being delayed
2291  *			by the CM handler, valid values are:
2292  *				IBT_CM_DELAY_REQ
2293  *				IBT_CM_DELAY_REP
2294  *				IBT_CM_DELAY_LAP
2295  *	cm_session_id	The session ID that was passed to client srv_handler
2296  *			by the CM
2297  *	service_time	The extended service time
2298  *	priv_data	Vendor specific data to be sent in the CM generated
2299  *			MRA message. Should be NULL if not specified.
2300  *	len		The number of bytes of data specified by priv_data.
2301  *
2302  * RETURN VALUES:
2303  *	IBT_SUCCESS	on success (or respective failure on error)
2304  */
2305 ibt_status_t
2306 ibt_cm_delay(ibt_cmdelay_flags_t flags, void *cm_session_id,
2307     clock_t service_time, void *priv_data, ibt_priv_data_len_t len)
2308 {
2309 	uint8_t			msg_typ = 0;
2310 	ibcm_mra_msg_t		*mra_msgp;
2311 	ibcm_state_data_t	*statep;
2312 	ibt_status_t		status;
2313 
2314 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_delay(0x%x, %p, 0x%x)",
2315 	    flags, cm_session_id, service_time);
2316 
2317 	/*
2318 	 * Make sure channel is associated with a statep
2319 	 */
2320 	statep = (ibcm_state_data_t *)cm_session_id;
2321 
2322 	if (statep == NULL) {
2323 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: statep NULL");
2324 		return (IBT_INVALID_PARAM);
2325 	}
2326 
2327 	IBTF_DPRINTF_L4(cmlog, "ibt_cm_delay: statep %p", statep);
2328 
2329 	/* Allocate an ibmf msg for mra, if not allocated yet */
2330 	if (statep->mra_msg == NULL) {
2331 		if ((status = ibcm_alloc_out_msg(
2332 		    statep->stored_reply_addr.ibmf_hdl, &statep->mra_msg,
2333 		    MAD_METHOD_SEND)) != IBT_SUCCESS) {
2334 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: chan 0x%p"
2335 			    "IBMF MSG allocation failed", statep->channel);
2336 			return (status);
2337 		}
2338 	}
2339 
2340 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mra_msgp))
2341 
2342 	mra_msgp = (ibcm_mra_msg_t *)IBCM_OUT_MSGP(statep->mra_msg);
2343 	mra_msgp->mra_local_comm_id = h2b32(statep->local_comid);
2344 	mra_msgp->mra_remote_comm_id = h2b32(statep->remote_comid);
2345 
2346 	/* fill in rest of MRA's fields - Message MRAed and Service Timeout */
2347 	if (flags == IBT_CM_DELAY_REQ) {
2348 		msg_typ = IBT_CM_MRA_TYPE_REQ;
2349 	} else if (flags == IBT_CM_DELAY_REP) {
2350 		msg_typ = IBT_CM_MRA_TYPE_REP;
2351 	} else if (flags == IBT_CM_DELAY_LAP) {
2352 		msg_typ = IBT_CM_MRA_TYPE_LAP;
2353 	}
2354 
2355 	mra_msgp->mra_message_type_plus = msg_typ << 6;
2356 	mra_msgp->mra_service_timeout_plus = ibt_usec2ib(service_time) << 3;
2357 
2358 	len = min(len, IBT_MRA_PRIV_DATA_SZ);
2359 	if (priv_data && (len > 0))
2360 		bcopy(priv_data, mra_msgp->mra_private_data, len);
2361 
2362 	IBCM_OUT_HDRP(statep->mra_msg)->AttributeID =
2363 	    h2b16(IBCM_INCOMING_MRA + IBCM_ATTR_BASE_ID);
2364 
2365 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mra_msgp))
2366 
2367 	mutex_enter(&statep->state_mutex);
2368 
2369 	if ((statep->mode == IBCM_ACTIVE_MODE) &&
2370 	    (statep->state == IBCM_STATE_REP_RCVD)) {
2371 		statep->state = IBCM_STATE_MRA_REP_SENT;
2372 	} else if (statep->mode == IBCM_PASSIVE_MODE) {
2373 		if (statep->state == IBCM_STATE_REQ_RCVD) {
2374 			statep->state = IBCM_STATE_MRA_SENT;
2375 		} else if (statep->ap_state == IBCM_AP_STATE_LAP_RCVD) {
2376 			statep->ap_state = IBCM_AP_STATE_MRA_LAP_RCVD;
2377 		} else {
2378 			IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: invalid state "
2379 			    "/ap_state/mode %x, %x, %x", statep->state,
2380 			    statep->ap_state, statep->mode);
2381 			mutex_exit(&statep->state_mutex);
2382 			return (IBT_CHAN_STATE_INVALID);
2383 		}
2384 	} else {
2385 		IBTF_DPRINTF_L2(cmlog, "ibt_cm_delay: invalid state "
2386 		    "/ap_state/mode %x, %x, %x", statep->state,
2387 		    statep->ap_state, statep->mode);
2388 		mutex_exit(&statep->state_mutex);
2389 
2390 		return (IBT_CHAN_STATE_INVALID);
2391 	}
2392 	/* service time is usecs, stale_clock is nsecs */
2393 	statep->stale_clock = gethrtime() +
2394 	    (hrtime_t)ibt_ib2usec(ibt_usec2ib(service_time)) * (1000 *
2395 	    statep->max_cm_retries);
2396 
2397 	statep->send_mad_flags |= IBCM_MRA_POST_BUSY;
2398 	IBCM_REF_CNT_INCR(statep);	/* for ibcm_post_mra_complete */
2399 	mutex_exit(&statep->state_mutex);
2400 
2401 	IBCM_OUT_HDRP(statep->mra_msg)->TransactionID =
2402 	    IBCM_OUT_HDRP(statep->stored_msg)->TransactionID;
2403 
2404 	/* post the MRA mad in blocking mode, as no timers involved */
2405 	ibcm_post_rc_mad(statep, statep->mra_msg, ibcm_post_mra_complete,
2406 	    statep);
2407 	ibcm_insert_trace(statep, IBCM_TRACE_OUTGOING_MRA);
2408 	/* If this message isn't seen then ibt_cm_delay failed */
2409 	IBTF_DPRINTF_L3(cmlog, "ibt_cm_delay: done !!");
2410 
2411 	return (IBT_SUCCESS);
2412 }
2413 
2414 
2415 /*
2416  * ibt_register_service()
2417  *	Register a service with the IBCM
2418  *
2419  * INPUTS:
2420  *	ibt_hdl		The IBT client handle returned to the client
2421  *			on an ibt_attach() call.
2422  *
2423  *	srv		The address of a ibt_srv_desc_t that describes
2424  *			the service, containing the following:
2425  *
2426  *		sd_ud_handler	The Service CM UD event Handler.
2427  *		sd_handler	The Service CM RC/UC/RD event Handler.
2428  *		sd_flags	Service flags (peer-to-peer, or not).
2429  *
2430  *	sid		This tells CM if the service is local (sid is 0) or
2431  *			wellknown (sid is the starting service id of the range).
2432  *
2433  *	num_sids	The number of contiguous service-ids to reserve.
2434  *
2435  *	srv_hdl		The address of a service identification handle, used
2436  *			to deregister a service, and to bind GIDs to.
2437  *
2438  *	ret_sid		The address to store the Service ID return value.
2439  *			If num_sids > 1, ret_sid is the first Service ID
2440  *			in the range.
2441  *
2442  * ibt_register_service() returns:
2443  *	IBT_SUCCESS		- added a service successfully.
2444  *	IBT_INVALID_PARAM	- invalid input parameter.
2445  *	IBT_CM_FAILURE		- failed to add the service.
2446  *	IBT_CM_SERVICE_EXISTS	- service already exists.
2447  *	IBT_INSUFF_KERNEL_RESOURCE - ran out of local service ids (should
2448  *				     never happen).
2449  */
2450 ibt_status_t
2451 ibt_register_service(ibt_clnt_hdl_t ibt_hdl, ibt_srv_desc_t *srv,
2452     ib_svc_id_t sid, int num_sids, ibt_srv_hdl_t *srv_hdl, ib_svc_id_t *ret_sid)
2453 {
2454 	ibcm_svc_info_t		*svcinfop;
2455 
2456 	IBTF_DPRINTF_L2(cmlog, "ibt_register_service(%p, %p, %llx, %d)",
2457 	    ibt_hdl, srv, (longlong_t)sid, num_sids);
2458 
2459 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*svcinfop))
2460 
2461 	*srv_hdl = NULL;
2462 
2463 	if (num_sids <= 0) {
2464 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2465 		    "Invalid number of service-ids specified (%d)", num_sids);
2466 		return (IBT_INVALID_PARAM);
2467 	}
2468 
2469 	if (sid == 0) {
2470 		if (ret_sid == NULL)
2471 			return (IBT_INVALID_PARAM);
2472 		sid = ibcm_alloc_local_sids(num_sids);
2473 		if (sid == 0)
2474 			return (IBT_INSUFF_KERNEL_RESOURCE);
2475 
2476 	/* Make sure that the ServiceId specified is not of LOCAL AGN type. */
2477 	} else if ((sid & IB_SID_AGN_MASK) == IB_SID_AGN_LOCAL) {
2478 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2479 		    "Invalid non-LOCAL SID specified: 0x%llX",
2480 		    (longlong_t)sid);
2481 		return (IBT_INVALID_PARAM);
2482 	}
2483 
2484 	svcinfop = ibcm_create_svc_entry(sid, num_sids);
2485 
2486 	if (svcinfop == NULL) {
2487 		IBTF_DPRINTF_L2(cmlog, "ibt_register_service: "
2488 		    "Service-ID 0x%llx already registered", (longlong_t)sid);
2489 		return (IBT_CM_SERVICE_EXISTS);
2490 	}
2491 
2492 	/*
2493 	 * 'sid' and 'num_sids' are filled in ibcm_create_svc_entry()
2494 	 */
2495 	svcinfop->svc_flags = srv->sd_flags;
2496 	svcinfop->svc_rc_handler = srv->sd_handler;
2497 	svcinfop->svc_ud_handler = srv->sd_ud_handler;
2498 
2499 	if (ret_sid != NULL)
2500 		*ret_sid = sid;
2501 
2502 	*srv_hdl = svcinfop;
2503 
2504 	ibtl_cm_change_service_cnt(ibt_hdl, num_sids);
2505 
2506 	/* If this message isn't seen, then ibt_register_service failed. */
2507 	IBTF_DPRINTF_L2(cmlog, "ibt_register_service: done (%p, %llX)",
2508 	    svcinfop, sid);
2509 
2510 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*svcinfop))
2511 
2512 	return (IBT_SUCCESS);
2513 }
2514 
2515 
2516 static ibt_status_t
2517 ibcm_write_service_record(ibmf_saa_handle_t saa_handle,
2518     sa_service_record_t *srv_recp, ibmf_saa_access_type_t saa_type)
2519 {
2520 	int	rval;
2521 	int	retry;
2522 
2523 	ibcm_sa_access_enter();
2524 	for (retry = 0; retry < ibcm_max_sa_retries; retry++) {
2525 		rval = ibmf_saa_update_service_record(
2526 		    saa_handle, srv_recp, saa_type, 0);
2527 		if (rval != IBMF_TRANS_TIMEOUT) {
2528 			break;
2529 		}
2530 		IBTF_DPRINTF_L2(cmlog, "ibcm_write_service_record: "
2531 		    "ibmf_saa_update_service_record timed out"
2532 		    " SID = %llX, rval = %d, saa_type = %d",
2533 		    (longlong_t)srv_recp->ServiceID, rval, saa_type);
2534 		delay(ibcm_sa_timeout_delay);
2535 	}
2536 	ibcm_sa_access_exit();
2537 
2538 	if (rval != IBMF_SUCCESS) {
2539 		IBTF_DPRINTF_L2(cmlog, "ibcm_write_service_record: "
2540 		    "ibmf_saa_update_service_record() : Failed - %d", rval);
2541 		return (ibcm_ibmf_analyze_error(rval));
2542 	} else
2543 		return (IBT_SUCCESS);
2544 }
2545 
2546 
2547 static void
2548 ibcm_rem_stale_srec(ibmf_saa_handle_t saa_handle, sa_service_record_t *srec)
2549 {
2550 	ibt_status_t		retval;
2551 	uint_t			num_found;
2552 	size_t			length;
2553 	sa_service_record_t	*srv_resp;
2554 	void			*results_p;
2555 	uint_t			i;
2556 	uint64_t		component_mask;
2557 	ibmf_saa_access_args_t	access_args;
2558 
2559 	component_mask =
2560 	    SA_SR_COMPMASK_PKEY | SA_SR_COMPMASK_NAME | SA_SR_COMPMASK_GID;
2561 
2562 	/* Call in SA Access retrieve routine to get Service Records. */
2563 	access_args.sq_attr_id = SA_SERVICERECORD_ATTRID;
2564 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
2565 	access_args.sq_component_mask = component_mask;
2566 	access_args.sq_template = srec;
2567 	access_args.sq_template_length = sizeof (sa_service_record_t);
2568 	access_args.sq_callback = NULL;
2569 	access_args.sq_callback_arg = NULL;
2570 
2571 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
2572 	    &results_p);
2573 	if (retval != IBT_SUCCESS) {
2574 		IBTF_DPRINTF_L2(cmlog, "ibcm_rem_stale_srec: "
2575 		    "SA Access Failure");
2576 		return;
2577 	}
2578 
2579 	num_found = length / sizeof (sa_service_record_t);
2580 
2581 	if (num_found)
2582 		IBTF_DPRINTF_L3(cmlog, "ibcm_rem_stale_srec: "
2583 		    "Found %d matching Service Records.", num_found);
2584 
2585 	/* Validate the returned number of records. */
2586 	if ((results_p != NULL) && (num_found > 0)) {
2587 
2588 		/* Remove all the records. */
2589 		for (i = 0; i < num_found; i++) {
2590 
2591 			srv_resp = (sa_service_record_t *)
2592 			    ((uchar_t *)results_p +
2593 			    i * sizeof (sa_service_record_t));
2594 
2595 			/*
2596 			 * Found some matching records, but check out whether
2597 			 * this Record is really stale or just happens to match
2598 			 * the current session records. If yes, don't remove it.
2599 			 */
2600 			mutex_enter(&ibcm_svc_info_lock);
2601 			if (ibcm_find_svc_entry(srv_resp->ServiceID) != NULL) {
2602 				/* This record is NOT STALE. */
2603 				mutex_exit(&ibcm_svc_info_lock);
2604 				IBTF_DPRINTF_L3(cmlog, "ibcm_rem_stale_srec: "
2605 				    "This is not Stale, it's an active record");
2606 				continue;
2607 			}
2608 			mutex_exit(&ibcm_svc_info_lock);
2609 
2610 			IBTF_DPRINTF_L2(cmlog, "ibcm_rem_stale_srec: "
2611 			    "Removing Stale Rec: %s, %llX",
2612 			    srv_resp->ServiceName, srv_resp->ServiceID);
2613 
2614 			IBCM_DUMP_SERVICE_REC(srv_resp);
2615 
2616 			/*
2617 			 * Remove the Service Record Entry from SA.
2618 			 *
2619 			 * Get ServiceID info from Response Buf, other
2620 			 * attributes are already filled-in.
2621 			 */
2622 
2623 			 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(srec->ServiceID))
2624 
2625 			srec->ServiceID = srv_resp->ServiceID;
2626 
2627 			 _NOTE(NOW_VISIBLE_TO_OTHER_THREADS(srec->ServiceID))
2628 
2629 			(void) ibcm_write_service_record(saa_handle, srec,
2630 			    IBMF_SAA_DELETE);
2631 		}
2632 
2633 		/* Deallocate the memory for results_p. */
2634 		kmem_free(results_p, length);
2635 	}
2636 }
2637 
2638 
2639 
2640 /*
2641  * ibt_bind_service()
2642  *	Register a service with the IBCM
2643  *
2644  * INPUTS:
2645  *	srv_hdl		The service id handle returned to the client
2646  *			on an ibt_service_register() call.
2647  *
2648  *	gid		The GID to which to bind the service.
2649  *
2650  *	srv_bind	The address of a ibt_srv_bind_t that describes
2651  *			the service record.  This should be NULL if there
2652  *			is to be no service record.  This contains:
2653  *
2654  *		sb_lease	Lease period
2655  *		sb_pkey		Partition
2656  *		sb_name		pointer to ASCII string Service Name,
2657  *				NULL terminated.
2658  *		sb_key[]	Key to secure the service record.
2659  *		sb_data		Service Data structure (64-byte)
2660  *
2661  *	cm_private	First argument of Service handler.
2662  *
2663  *	sb_hdl_p	The address of a service bind handle, used
2664  *			to undo the service binding.
2665  *
2666  * ibt_bind_service() returns:
2667  *	IBT_SUCCESS		- added a service successfully.
2668  *	IBT_INVALID_PARAM	- invalid input parameter.
2669  *	IBT_CM_FAILURE		- failed to add the service.
2670  *	IBT_CM_SERVICE_EXISTS	- service already exists.
2671  */
2672 ibt_status_t
2673 ibt_bind_service(ibt_srv_hdl_t srv_hdl, ib_gid_t gid, ibt_srv_bind_t *srv_bind,
2674     void *cm_private, ibt_sbind_hdl_t *sb_hdl_p)
2675 {
2676 	ibt_status_t		status;
2677 	ibtl_cm_hca_port_t	port;
2678 	ibcm_svc_bind_t		*sbindp, *sbp;
2679 	ibcm_hca_info_t		*hcap;
2680 	ib_svc_id_t		sid, start_sid, end_sid;
2681 	ibmf_saa_handle_t	saa_handle;
2682 	sa_service_record_t	srv_rec;
2683 	uint16_t		pkey_ix;
2684 
2685 	if (sb_hdl_p != NULL)
2686 		*sb_hdl_p = NULL;	/* return value for error cases */
2687 
2688 	IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: srv_hdl %p, gid (%llX:%llX)",
2689 	    srv_hdl, (longlong_t)gid.gid_prefix, (longlong_t)gid.gid_guid);
2690 
2691 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sbindp))
2692 
2693 	/* Call ibtl_cm_get_hca_port to get the port number and the HCA GUID. */
2694 	if ((status = ibtl_cm_get_hca_port(gid, 0, &port)) != IBT_SUCCESS) {
2695 		IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2696 		    "ibtl_cm_get_hca_port failed: %d", status);
2697 		return (status);
2698 	}
2699 	IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: Port:%d HCA GUID:%llX",
2700 	    port.hp_port, port.hp_hca_guid);
2701 
2702 	hcap = ibcm_find_hca_entry(port.hp_hca_guid);
2703 	if (hcap == NULL) {
2704 		IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: NO HCA found");
2705 		return (IBT_HCA_BUSY_DETACHING);
2706 	}
2707 	IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: hcap = %p", hcap);
2708 
2709 	if (srv_bind != NULL) {
2710 		saa_handle = ibcm_get_saa_handle(hcap, port.hp_port);
2711 		if (saa_handle == NULL) {
2712 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2713 			    "saa_handle is NULL");
2714 			ibcm_dec_hca_acc_cnt(hcap);
2715 			return (IBT_HCA_PORT_NOT_ACTIVE);
2716 		}
2717 		if (srv_bind->sb_pkey == 0) {
2718 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2719 			    "P_Key must not be 0");
2720 			ibcm_dec_hca_acc_cnt(hcap);
2721 			return (IBT_INVALID_PARAM);
2722 		}
2723 		if (strlen(srv_bind->sb_name) >= IB_SVC_NAME_LEN) {
2724 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2725 			    "Service Name is too long");
2726 			ibcm_dec_hca_acc_cnt(hcap);
2727 			return (IBT_INVALID_PARAM);
2728 		} else
2729 			IBTF_DPRINTF_L3(cmlog, "ibt_bind_service: "
2730 			    "Service Name='%s'", srv_bind->sb_name);
2731 		status = ibt_pkey2index_byguid(port.hp_hca_guid,
2732 		    port.hp_port, srv_bind->sb_pkey, &pkey_ix);
2733 		if (status != IBT_SUCCESS) {
2734 			IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2735 			    "P_Key 0x%x not found in P_Key_Table",
2736 			    srv_bind->sb_pkey);
2737 			ibcm_dec_hca_acc_cnt(hcap);
2738 			return (status);
2739 		}
2740 	}
2741 
2742 	/* assume success - allocate before locking */
2743 	sbindp = kmem_zalloc(sizeof (*sbindp), KM_SLEEP);
2744 	sbindp->sbind_cm_private = cm_private;
2745 	sbindp->sbind_gid = gid;
2746 	sbindp->sbind_hcaguid = port.hp_hca_guid;
2747 	sbindp->sbind_port = port.hp_port;
2748 
2749 	mutex_enter(&ibcm_svc_info_lock);
2750 
2751 	sbp = srv_hdl->svc_bind_list;
2752 	while (sbp != NULL) {
2753 		if (sbp->sbind_gid.gid_guid == gid.gid_guid &&
2754 		    sbp->sbind_gid.gid_prefix == gid.gid_prefix) {
2755 			if (srv_bind == NULL ||
2756 			    srv_bind->sb_pkey == sbp->sbind_pkey) {
2757 				IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: "
2758 				    "failed: GID %llX:%llX and PKEY %x is "
2759 				    "already bound", gid.gid_guid,
2760 				    gid.gid_prefix, sbp->sbind_pkey);
2761 				mutex_exit(&ibcm_svc_info_lock);
2762 				ibcm_dec_hca_acc_cnt(hcap);
2763 				kmem_free(sbindp, sizeof (*sbindp));
2764 				return (IBT_CM_SERVICE_EXISTS);
2765 			}
2766 		}
2767 		sbp = sbp->sbind_link;
2768 	}
2769 	/* no entry found */
2770 
2771 	sbindp->sbind_link = srv_hdl->svc_bind_list;
2772 	srv_hdl->svc_bind_list = sbindp;
2773 
2774 	mutex_exit(&ibcm_svc_info_lock);
2775 
2776 	if (srv_bind != NULL) {
2777 		bzero(&srv_rec, sizeof (srv_rec));
2778 
2779 		srv_rec.ServiceLease =
2780 		    sbindp->sbind_lease = srv_bind->sb_lease;
2781 		srv_rec.ServiceP_Key =
2782 		    sbindp->sbind_pkey = srv_bind->sb_pkey;
2783 		srv_rec.ServiceKey_hi =
2784 		    sbindp->sbind_key[0] = srv_bind->sb_key[0];
2785 		srv_rec.ServiceKey_lo =
2786 		    sbindp->sbind_key[1] = srv_bind->sb_key[1];
2787 		(void) strcpy(sbindp->sbind_name, srv_bind->sb_name);
2788 		(void) strcpy((char *)srv_rec.ServiceName, srv_bind->sb_name);
2789 		srv_rec.ServiceGID = gid;
2790 
2791 		/*
2792 		 * Find out whether we have any stale Local Service records
2793 		 * matching the current attributes.  If yes, we shall try to
2794 		 * remove them from SA using the current request's ServiceKey.
2795 		 *
2796 		 * We will perform this operation only for Local Services, as
2797 		 * it is handled by SA automatically for WellKnown Services.
2798 		 *
2799 		 * Ofcourse, clients can specify NOT to do this clean-up by
2800 		 * setting IBT_SBIND_NO_CLEANUP flag (srv_bind->sb_flag).
2801 		 */
2802 		if ((srv_hdl->svc_id & IB_SID_AGN_LOCAL) &&
2803 		    (!(srv_bind->sb_flag & IBT_SBIND_NO_CLEANUP))) {
2804 			ibcm_rem_stale_srec(saa_handle, &srv_rec);
2805 		}
2806 
2807 		/* Handle endianess for service data. */
2808 		ibcm_swizzle_from_srv(&srv_bind->sb_data, sbindp->sbind_data);
2809 
2810 		bcopy(sbindp->sbind_data, srv_rec.ServiceData, IB_SVC_DATA_LEN);
2811 
2812 		/* insert srv record into the SA */
2813 		start_sid = srv_hdl->svc_id;
2814 		end_sid = start_sid + srv_hdl->svc_num_sids - 1;
2815 		for (sid = start_sid; sid <= end_sid; sid++) {
2816 
2817 			srv_rec.ServiceID = sid;
2818 
2819 			IBCM_DUMP_SERVICE_REC(&srv_rec);
2820 
2821 			IBTF_DPRINTF_L4(cmlog, "ibt_bind_service: "
2822 			    "ibmf_saa_write_service_record, SvcId = %llX",
2823 			    (longlong_t)sid);
2824 
2825 			status = ibcm_write_service_record(saa_handle, &srv_rec,
2826 			    IBMF_SAA_UPDATE);
2827 			if (status != IBT_SUCCESS) {
2828 				IBTF_DPRINTF_L2(cmlog, "ibt_bind_service:"
2829 				    " ibcm_write_service_record fails %d, "
2830 				    "sid %llX", status, (longlong_t)sid);
2831 
2832 				if (sid != start_sid) {
2833 					/*
2834 					 * Bind failed while bind SID other than
2835 					 * first in the sid_range.  So we need
2836 					 * to unbind those, which are passed.
2837 					 *
2838 					 * Need to increment svc count to
2839 					 * compensate for ibt_unbind_service().
2840 					 */
2841 					ibcm_inc_hca_svc_cnt(hcap);
2842 					ibcm_dec_hca_acc_cnt(hcap);
2843 
2844 					(void) ibt_unbind_service(srv_hdl,
2845 					    sbindp);
2846 				} else {
2847 					ibcm_svc_bind_t		**sbpp;
2848 
2849 					/*
2850 					 * Bind failed for the first SID or the
2851 					 * only SID in question, then no need
2852 					 * to unbind, just free memory and
2853 					 * return error.
2854 					 */
2855 					mutex_enter(&ibcm_svc_info_lock);
2856 
2857 					sbpp = &srv_hdl->svc_bind_list;
2858 					sbp = *sbpp;
2859 					while (sbp != NULL) {
2860 						if (sbp == sbindp) {
2861 							*sbpp = sbp->sbind_link;
2862 							break;
2863 						}
2864 						sbpp = &sbp->sbind_link;
2865 						sbp = *sbpp;
2866 					}
2867 					mutex_exit(&ibcm_svc_info_lock);
2868 					ibcm_dec_hca_acc_cnt(hcap);
2869 
2870 					kmem_free(sbindp, sizeof (*sbindp));
2871 				}
2872 				return (status);
2873 			}
2874 		}
2875 	}
2876 	ibcm_inc_hca_svc_cnt(hcap);
2877 	ibcm_dec_hca_acc_cnt(hcap);
2878 
2879 	/* If this message isn't seen then ibt_bind_service failed */
2880 	IBTF_DPRINTF_L2(cmlog, "ibt_bind_service: DONE (%p, %llX:%llX)",
2881 	    srv_hdl, gid.gid_prefix, gid.gid_guid);
2882 
2883 	if (sb_hdl_p != NULL)
2884 		*sb_hdl_p = sbindp;
2885 
2886 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sbindp))
2887 
2888 	return (IBT_SUCCESS);
2889 }
2890 
2891 ibt_status_t
2892 ibt_unbind_service(ibt_srv_hdl_t srv_hdl, ibt_sbind_hdl_t sbindp)
2893 {
2894 	ib_svc_id_t	sid, end_sid;
2895 	ibt_status_t	rval;
2896 	ibcm_hca_info_t	*hcap;
2897 	ibcm_svc_bind_t	*sbp, **sbpp;
2898 
2899 	IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service(%p, %p)",
2900 	    srv_hdl, sbindp);
2901 
2902 	hcap = ibcm_find_hca_entry(sbindp->sbind_hcaguid);
2903 
2904 	/* If there is a service on hca, respective hcap cannot go away */
2905 	ASSERT(hcap != NULL);
2906 
2907 	mutex_enter(&ibcm_svc_info_lock);
2908 
2909 	sbpp = &srv_hdl->svc_bind_list;
2910 	sbp = *sbpp;
2911 	while (sbp != NULL) {
2912 		if (sbp == sbindp) {
2913 			*sbpp = sbp->sbind_link;
2914 			break;
2915 		}
2916 		sbpp = &sbp->sbind_link;
2917 		sbp = *sbpp;
2918 	}
2919 	sid = srv_hdl->svc_id;
2920 	end_sid = srv_hdl->svc_id + srv_hdl->svc_num_sids - 1;
2921 	if (sbp != NULL)
2922 		while (sbp->sbind_rewrite_state == IBCM_REWRITE_BUSY)
2923 			cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
2924 	mutex_exit(&ibcm_svc_info_lock);
2925 
2926 	if (sbp == NULL) {
2927 		IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2928 		    "service binding not found: srv_hdl %p, srv_bind %p",
2929 		    srv_hdl, sbindp);
2930 		ibcm_dec_hca_acc_cnt(hcap);
2931 		return (IBT_INVALID_PARAM);
2932 	}
2933 
2934 	if (sbindp->sbind_pkey != 0) {	/* Are there service records? */
2935 		ibtl_cm_hca_port_t	port;
2936 		sa_service_record_t	srv_rec;
2937 		ibmf_saa_handle_t	saa_handle;
2938 		ibt_status_t		status;
2939 
2940 		/* get the default SGID of the port */
2941 		if ((status = ibtl_cm_get_hca_port(sbindp->sbind_gid, 0, &port))
2942 		    != IBT_SUCCESS) {
2943 			IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2944 			    "ibtl_cm_get_hca_port failed: %d", status);
2945 			/* we're done, but there may be stale service records */
2946 			goto done;
2947 		}
2948 
2949 		saa_handle = ibcm_get_saa_handle(hcap, port.hp_port);
2950 		if (saa_handle == NULL) {
2951 			IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2952 			    "saa_handle is NULL");
2953 			/* we're done, but there may be stale service records */
2954 			goto done;
2955 		}
2956 
2957 		/* Fill in fields of srv_rec */
2958 		bzero(&srv_rec, sizeof (srv_rec));
2959 
2960 		srv_rec.ServiceP_Key = sbindp->sbind_pkey;
2961 		srv_rec.ServiceKey_hi = sbindp->sbind_key[0];
2962 		srv_rec.ServiceKey_lo = sbindp->sbind_key[1];
2963 		srv_rec.ServiceGID = sbindp->sbind_gid;
2964 		(void) strcpy((char *)srv_rec.ServiceName, sbindp->sbind_name);
2965 
2966 		while (sid <= end_sid) {
2967 
2968 			srv_rec.ServiceID = sid;
2969 			IBCM_DUMP_SERVICE_REC(&srv_rec);
2970 
2971 			rval = ibcm_write_service_record(saa_handle, &srv_rec,
2972 			    IBMF_SAA_DELETE);
2973 
2974 			IBTF_DPRINTF_L4(cmlog, "ibt_unbind_service: "
2975 			    "ibcm_write_service_record rval = %d, SID %llx",
2976 			    rval, sid);
2977 			if (rval != IBT_SUCCESS) {
2978 				/* this is not considered a reason to fail */
2979 				IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: "
2980 				    "ibcm_write_service_record fails %d, "
2981 				    "sid %llx", rval, sid);
2982 			}
2983 			sid++;
2984 		}
2985 	}
2986 done:
2987 	ibcm_dec_hca_svc_cnt(hcap);
2988 	ibcm_dec_hca_acc_cnt(hcap);
2989 	kmem_free(sbindp, sizeof (*sbindp));
2990 
2991 	/* If this message isn't seen then ibt_unbind_service failed */
2992 	IBTF_DPRINTF_L2(cmlog, "ibt_unbind_service: done !!");
2993 
2994 	return (IBT_SUCCESS);
2995 }
2996 
2997 /*
2998  * Simply pull off each binding from the list and unbind it.
2999  * If any of the unbind calls fail, we fail.
3000  */
3001 ibt_status_t
3002 ibt_unbind_all_services(ibt_srv_hdl_t srv_hdl)
3003 {
3004 	ibt_status_t	status;
3005 	ibcm_svc_bind_t	*sbp;
3006 
3007 	mutex_enter(&ibcm_svc_info_lock);
3008 	sbp = NULL;
3009 
3010 	/* this compare keeps the loop from being infinite */
3011 	while (sbp != srv_hdl->svc_bind_list) {
3012 		sbp = srv_hdl->svc_bind_list;
3013 		mutex_exit(&ibcm_svc_info_lock);
3014 		status = ibt_unbind_service(srv_hdl, sbp);
3015 		if (status != IBT_SUCCESS)
3016 			return (status);
3017 		mutex_enter(&ibcm_svc_info_lock);
3018 		if (srv_hdl->svc_bind_list == NULL)
3019 			break;
3020 	}
3021 	mutex_exit(&ibcm_svc_info_lock);
3022 	return (IBT_SUCCESS);
3023 }
3024 
3025 /*
3026  * ibt_deregister_service()
3027  *	Deregister a service with the IBCM
3028  *
3029  * INPUTS:
3030  *	ibt_hdl		The IBT client handle returned to the client
3031  *			on an ibt_attach() call.
3032  *
3033  *	srv_hdl		The address of a service identification handle, used
3034  *			to de-register a service.
3035  * RETURN VALUES:
3036  *	IBT_SUCCESS	on success (or respective failure on error)
3037  */
3038 ibt_status_t
3039 ibt_deregister_service(ibt_clnt_hdl_t ibt_hdl, ibt_srv_hdl_t srv_hdl)
3040 {
3041 	ibcm_svc_info_t		*svcp;
3042 	ibcm_svc_lookup_t	svc;
3043 
3044 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service(%p, %p)",
3045 	    ibt_hdl, srv_hdl);
3046 
3047 	mutex_enter(&ibcm_svc_info_lock);
3048 
3049 	if (srv_hdl->svc_bind_list != NULL) {
3050 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service:"
3051 		    " srv_hdl %p still has bindings", srv_hdl);
3052 		mutex_exit(&ibcm_svc_info_lock);
3053 		return (IBT_CM_SERVICE_BUSY);
3054 	}
3055 	svc.sid = srv_hdl->svc_id;
3056 	svc.num_sids = 1;
3057 	IBTF_DPRINTF_L3(cmlog, "ibt_deregister_service: SID 0x%llX, numsids %d",
3058 	    srv_hdl->svc_id, srv_hdl->svc_num_sids);
3059 
3060 #ifdef __lock_lint
3061 	ibcm_svc_compare(NULL, NULL);
3062 #endif
3063 	svcp = avl_find(&ibcm_svc_avl_tree, &svc, NULL);
3064 	if (svcp != srv_hdl) {
3065 		mutex_exit(&ibcm_svc_info_lock);
3066 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service(): "
3067 		    "srv_hdl %p not found", srv_hdl);
3068 		return (IBT_INVALID_PARAM);
3069 	}
3070 	avl_remove(&ibcm_svc_avl_tree, svcp);
3071 
3072 	/* wait for active REQ/SREQ handling to be done */
3073 	svcp->svc_to_delete = 1;
3074 	while (svcp->svc_ref_cnt != 0)
3075 		cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
3076 
3077 	mutex_exit(&ibcm_svc_info_lock);
3078 
3079 	if ((srv_hdl->svc_id & IB_SID_AGN_MASK) == IB_SID_AGN_LOCAL)
3080 		ibcm_free_local_sids(srv_hdl->svc_id, srv_hdl->svc_num_sids);
3081 
3082 	ibtl_cm_change_service_cnt(ibt_hdl, -srv_hdl->svc_num_sids);
3083 	kmem_free(srv_hdl, sizeof (*srv_hdl));
3084 
3085 	/* If this message isn't seen then ibt_deregister_service failed */
3086 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_service: done !!");
3087 
3088 	return (IBT_SUCCESS);
3089 }
3090 
3091 ibcm_status_t
3092 ibcm_ar_init(void)
3093 {
3094 	ib_svc_id_t	sid = IBCM_DAPL_ATS_SID;
3095 	ibcm_svc_info_t *tmp_svcp;
3096 
3097 	IBTF_DPRINTF_L3(cmlog, "ibcm_ar_init()");
3098 
3099 	/* remove this special SID from the pool of available SIDs */
3100 	if ((tmp_svcp = ibcm_create_svc_entry(sid, 1)) == NULL) {
3101 		IBTF_DPRINTF_L2(cmlog, "ibcm_ar_init: "
3102 		    "DAPL ATS SID 0x%llx already registered", (longlong_t)sid);
3103 		return (IBCM_FAILURE);
3104 	}
3105 	mutex_enter(&ibcm_svc_info_lock);
3106 	ibcm_ar_svcinfop = tmp_svcp;
3107 	ibcm_ar_list = NULL;	/* no address records registered yet */
3108 	mutex_exit(&ibcm_svc_info_lock);
3109 	return (IBCM_SUCCESS);
3110 }
3111 
3112 ibcm_status_t
3113 ibcm_ar_fini(void)
3114 {
3115 	ibcm_ar_t	*ar_list;
3116 	ibcm_svc_info_t	*tmp_svcp;
3117 
3118 	mutex_enter(&ibcm_svc_info_lock);
3119 	ar_list = ibcm_ar_list;
3120 
3121 	if (ar_list == NULL &&
3122 	    avl_numnodes(&ibcm_svc_avl_tree) == 1 &&
3123 	    avl_first(&ibcm_svc_avl_tree) == ibcm_ar_svcinfop) {
3124 		avl_remove(&ibcm_svc_avl_tree, ibcm_ar_svcinfop);
3125 		tmp_svcp = ibcm_ar_svcinfop;
3126 		mutex_exit(&ibcm_svc_info_lock);
3127 		kmem_free(tmp_svcp, sizeof (*ibcm_ar_svcinfop));
3128 		return (IBCM_SUCCESS);
3129 	}
3130 	mutex_exit(&ibcm_svc_info_lock);
3131 	return (IBCM_FAILURE);
3132 }
3133 
3134 
3135 /*
3136  * Return to the caller:
3137  *	IBT_SUCCESS		Found a perfect match.
3138  *				*arpp is set to the record.
3139  *	IBT_INCONSISTENT_AR	Found a record that's inconsistent.
3140  *	IBT_AR_NOT_REGISTERED	Found no record with same GID/pkey and
3141  *				found no record with same data.
3142  */
3143 static ibt_status_t
3144 ibcm_search_ar(ibt_ar_t *arp, ibcm_ar_t **arpp)
3145 {
3146 	ibcm_ar_t	*tmp;
3147 	int		i;
3148 
3149 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
3150 	tmp = ibcm_ar_list;
3151 	while (tmp != NULL) {
3152 		if (tmp->ar.ar_gid.gid_prefix == arp->ar_gid.gid_prefix &&
3153 		    tmp->ar.ar_gid.gid_guid == arp->ar_gid.gid_guid &&
3154 		    tmp->ar.ar_pkey == arp->ar_pkey) {
3155 			for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++)
3156 				if (tmp->ar.ar_data[i] != arp->ar_data[i])
3157 					return (IBT_INCONSISTENT_AR);
3158 			*arpp = tmp;
3159 			return (IBT_SUCCESS);
3160 		} else {
3161 			/* if all the data bytes match, we have inconsistency */
3162 			for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++)
3163 				if (tmp->ar.ar_data[i] != arp->ar_data[i])
3164 					break;
3165 			if (i == IBCM_DAPL_ATS_NBYTES)
3166 				return (IBT_INCONSISTENT_AR);
3167 			/* try next address record */
3168 		}
3169 		tmp = tmp->ar_link;
3170 	}
3171 	return (IBT_AR_NOT_REGISTERED);
3172 }
3173 
3174 ibt_status_t
3175 ibt_register_ar(ibt_clnt_hdl_t ibt_hdl, ibt_ar_t *arp)
3176 {
3177 	ibcm_ar_t		*found;
3178 	ibcm_ar_t		*tmp;
3179 	ibt_status_t		status;
3180 	ibt_status_t		s1, s2;
3181 	char			*s;
3182 	ibcm_ar_ref_t		*hdlp;
3183 	ibcm_ar_t		*new;
3184 	ibcm_ar_t		**linkp;
3185 	ibtl_cm_hca_port_t	cm_port;
3186 	uint16_t		pkey_ix;
3187 	ibcm_hca_info_t		*hcap;
3188 	ibmf_saa_handle_t	saa_handle;
3189 	sa_service_record_t	*srv_recp;
3190 	uint64_t		gid_ored;
3191 
3192 	IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: PKey 0x%X GID %llX:%llX",
3193 	    arp->ar_pkey, (longlong_t)arp->ar_gid.gid_prefix,
3194 	    (longlong_t)arp->ar_gid.gid_guid);
3195 
3196 	/*
3197 	 * If P_Key is 0, but GID is not, this query is invalid.
3198 	 * If GID is 0, but P_Key is not, this query is invalid.
3199 	 */
3200 	gid_ored = arp->ar_gid.gid_guid | arp->ar_gid.gid_prefix;
3201 	if ((arp->ar_pkey == 0 && gid_ored != 0ULL) ||
3202 	    (arp->ar_pkey != 0 && gid_ored == 0ULL)) {
3203 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3204 		    "GID/P_Key is not valid");
3205 		return (IBT_INVALID_PARAM);
3206 	}
3207 
3208 	/* assume success, so these might be needed */
3209 	hdlp = kmem_alloc(sizeof (*hdlp), KM_SLEEP);
3210 	new = kmem_zalloc(sizeof (*new), KM_SLEEP);
3211 
3212 	mutex_enter(&ibcm_svc_info_lock);
3213 	/* search for existing GID/pkey (there can be at most 1) */
3214 	status = ibcm_search_ar(arp, &found);
3215 	if (status == IBT_INCONSISTENT_AR) {
3216 		mutex_exit(&ibcm_svc_info_lock);
3217 		kmem_free(new, sizeof (*new));
3218 		kmem_free(hdlp, sizeof (*hdlp));
3219 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3220 		    "address record is inconsistent with a known one");
3221 		return (IBT_INCONSISTENT_AR);
3222 	} else if (status == IBT_SUCCESS) {
3223 		if (found->ar_flags == IBCM_AR_INITING) {
3224 			found->ar_waiters++;
3225 			cv_wait(&found->ar_cv, &ibcm_svc_info_lock);
3226 			found->ar_waiters--;
3227 		}
3228 		if (found->ar_flags == IBCM_AR_FAILED) {
3229 			if (found->ar_waiters == 0) {
3230 				cv_destroy(&found->ar_cv);
3231 				kmem_free(found, sizeof (*found));
3232 			}
3233 			mutex_exit(&ibcm_svc_info_lock);
3234 			kmem_free(new, sizeof (*new));
3235 			kmem_free(hdlp, sizeof (*hdlp));
3236 			return (ibt_get_module_failure(IBT_FAILURE_IBCM, 0));
3237 		}
3238 		hdlp->ar_ibt_hdl = ibt_hdl;
3239 		hdlp->ar_ref_link = found->ar_ibt_hdl_list;
3240 		found->ar_ibt_hdl_list = hdlp;
3241 		mutex_exit(&ibcm_svc_info_lock);
3242 		kmem_free(new, sizeof (*new));
3243 		ibtl_cm_change_service_cnt(ibt_hdl, 1);
3244 		return (IBT_SUCCESS);
3245 	} else {
3246 		ASSERT(status == IBT_AR_NOT_REGISTERED);
3247 	}
3248 	hdlp->ar_ref_link = NULL;
3249 	hdlp->ar_ibt_hdl = ibt_hdl;
3250 	new->ar_ibt_hdl_list = hdlp;
3251 	new->ar = *arp;
3252 	new->ar_flags = IBCM_AR_INITING;
3253 	new->ar_waiters = 0;
3254 	cv_init(&new->ar_cv, NULL, CV_DEFAULT, NULL);
3255 	new->ar_link = ibcm_ar_list;
3256 	ibcm_ar_list = new;
3257 
3258 	/* verify GID/pkey is valid for a local port, etc. */
3259 	hcap = NULL;
3260 	if ((s1 = ibtl_cm_get_hca_port(arp->ar_gid, 0, &cm_port))
3261 	    != IBT_SUCCESS ||
3262 	    (s2 = ibt_pkey2index_byguid(cm_port.hp_hca_guid, cm_port.hp_port,
3263 	    arp->ar_pkey, &pkey_ix)) != IBT_SUCCESS ||
3264 	    (hcap = ibcm_find_hca_entry(cm_port.hp_hca_guid)) == NULL) {
3265 		cv_destroy(&new->ar_cv);
3266 		ibcm_ar_list = new->ar_link;
3267 		mutex_exit(&ibcm_svc_info_lock);
3268 		kmem_free(new, sizeof (*new));
3269 		kmem_free(hdlp, sizeof (*hdlp));
3270 		status = IBT_INVALID_PARAM;
3271 		if (s1 == IBT_HCA_PORT_NOT_ACTIVE) {
3272 			s = "PORT DOWN";
3273 			status = IBT_HCA_PORT_NOT_ACTIVE;
3274 		} else if (s1 != IBT_SUCCESS)
3275 			s = "GID not found";
3276 		else if (s2 != IBT_SUCCESS)
3277 			s = "PKEY not found";
3278 		else
3279 			s = "CM could not find its HCA entry";
3280 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: %s, status = %d",
3281 		    s, status);
3282 		return (status);
3283 	}
3284 	mutex_exit(&ibcm_svc_info_lock);
3285 	saa_handle = ibcm_get_saa_handle(hcap, cm_port.hp_port);
3286 
3287 	/* create service record */
3288 	srv_recp = kmem_zalloc(sizeof (*srv_recp), KM_SLEEP);
3289 	srv_recp->ServiceLease = 0xFFFFFFFF;	/* infinite */
3290 	srv_recp->ServiceP_Key = arp->ar_pkey;
3291 	srv_recp->ServiceKey_hi = 0xDA410000ULL;	/* DAPL */
3292 	srv_recp->ServiceKey_lo = 0xA7500000ULL;	/* ATS */
3293 	(void) strcpy((char *)srv_recp->ServiceName, IBCM_DAPL_ATS_NAME);
3294 	srv_recp->ServiceGID = arp->ar_gid;
3295 	bcopy(arp->ar_data, srv_recp->ServiceData, IBCM_DAPL_ATS_NBYTES);
3296 	srv_recp->ServiceID = IBCM_DAPL_ATS_SID;
3297 
3298 	/* insert service record into the SA */
3299 
3300 	IBCM_DUMP_SERVICE_REC(srv_recp);
3301 
3302 	if (saa_handle != NULL)
3303 		status = ibcm_write_service_record(saa_handle, srv_recp,
3304 		    IBMF_SAA_UPDATE);
3305 	else
3306 		status = IBT_HCA_PORT_NOT_ACTIVE;
3307 
3308 	if (status != IBT_SUCCESS) {
3309 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: sa access fails %d, "
3310 		    "sid %llX", status, (longlong_t)srv_recp->ServiceID);
3311 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: FAILED for gid "
3312 		    "%llX:%llX pkey 0x%X", (longlong_t)arp->ar_gid.gid_prefix,
3313 		    (longlong_t)arp->ar_gid.gid_guid, arp->ar_pkey);
3314 
3315 		kmem_free(srv_recp, sizeof (*srv_recp));
3316 		kmem_free(hdlp, sizeof (*hdlp));
3317 
3318 		mutex_enter(&ibcm_svc_info_lock);
3319 		linkp = &ibcm_ar_list;
3320 		tmp = *linkp;
3321 		while (tmp != NULL) {
3322 			if (tmp == new) {
3323 				*linkp = new->ar_link;
3324 				break;
3325 			}
3326 			linkp = &tmp->ar_link;
3327 			tmp = *linkp;
3328 		}
3329 		if (new->ar_waiters > 0) {
3330 			new->ar_flags = IBCM_AR_FAILED;
3331 			cv_broadcast(&new->ar_cv);
3332 			mutex_exit(&ibcm_svc_info_lock);
3333 		} else {
3334 			cv_destroy(&new->ar_cv);
3335 			mutex_exit(&ibcm_svc_info_lock);
3336 			kmem_free(new, sizeof (*new));
3337 		}
3338 		ibcm_dec_hca_acc_cnt(hcap);
3339 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: "
3340 		    "IBMF_SAA failed to write address record");
3341 	} else {					/* SUCCESS */
3342 		uint8_t		*b;
3343 
3344 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar: SUCCESS for gid "
3345 		    "%llx:%llx pkey %x", (longlong_t)arp->ar_gid.gid_prefix,
3346 		    (longlong_t)arp->ar_gid.gid_guid, arp->ar_pkey);
3347 		b = arp->ar_data;
3348 
3349 		IBTF_DPRINTF_L2(cmlog, "ibt_register_ar:"
3350 		    " data %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
3351 		    b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9],
3352 		    b[10], b[11], b[12], b[13], b[14], b[15]);
3353 		mutex_enter(&ibcm_svc_info_lock);
3354 		new->ar_srv_recp = srv_recp;
3355 		new->ar_saa_handle = saa_handle;
3356 		new->ar_port = cm_port.hp_port;
3357 		new->ar_hcap = hcap;
3358 		new->ar_flags = IBCM_AR_SUCCESS;
3359 		if (new->ar_waiters > 0)
3360 			cv_broadcast(&new->ar_cv);
3361 		mutex_exit(&ibcm_svc_info_lock);
3362 		ibtl_cm_change_service_cnt(ibt_hdl, 1);
3363 		/* do not call ibcm_dec_hca_acc_cnt(hcap) until deregister */
3364 	}
3365 	return (status);
3366 }
3367 
3368 ibt_status_t
3369 ibt_deregister_ar(ibt_clnt_hdl_t ibt_hdl, ibt_ar_t *arp)
3370 {
3371 	ibcm_ar_t		*found;
3372 	ibcm_ar_t		*tmp;
3373 	ibcm_ar_t		**linkp;
3374 	ibcm_ar_ref_t		*hdlp;
3375 	ibcm_ar_ref_t		**hdlpp;
3376 	ibt_status_t		status;
3377 	ibmf_saa_handle_t	saa_handle;
3378 	sa_service_record_t	*srv_recp;
3379 	uint64_t		gid_ored;
3380 
3381 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: pkey %x", arp->ar_pkey);
3382 	IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: gid %llx:%llx",
3383 	    (longlong_t)arp->ar_gid.gid_prefix,
3384 	    (longlong_t)arp->ar_gid.gid_guid);
3385 
3386 	/*
3387 	 * If P_Key is 0, but GID is not, this query is invalid.
3388 	 * If GID is 0, but P_Key is not, this query is invalid.
3389 	 */
3390 	gid_ored = arp->ar_gid.gid_guid | arp->ar_gid.gid_prefix;
3391 	if ((arp->ar_pkey == 0 && gid_ored != 0ULL) ||
3392 	    (arp->ar_pkey != 0 && gid_ored == 0ULL)) {
3393 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3394 		    "GID/P_Key is not valid");
3395 		return (IBT_INVALID_PARAM);
3396 	}
3397 
3398 	mutex_enter(&ibcm_svc_info_lock);
3399 	/* search for existing GID/pkey (there can be at most 1) */
3400 	status = ibcm_search_ar(arp, &found);
3401 	if (status == IBT_INCONSISTENT_AR || status == IBT_AR_NOT_REGISTERED) {
3402 		mutex_exit(&ibcm_svc_info_lock);
3403 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3404 		    "address record not found");
3405 		return (IBT_AR_NOT_REGISTERED);
3406 	}
3407 	ASSERT(status == IBT_SUCCESS);
3408 
3409 	hdlpp = &found->ar_ibt_hdl_list;
3410 	hdlp = *hdlpp;
3411 	while (hdlp != NULL) {
3412 		if (hdlp->ar_ibt_hdl == ibt_hdl)
3413 			break;
3414 		hdlpp = &hdlp->ar_ref_link;
3415 		hdlp = *hdlpp;
3416 	}
3417 	if (hdlp == NULL) {	/* could not find ibt_hdl on list */
3418 		mutex_exit(&ibcm_svc_info_lock);
3419 		IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3420 		    "address record found, but not for this client");
3421 		return (IBT_AR_NOT_REGISTERED);
3422 	}
3423 	*hdlpp = hdlp->ar_ref_link;	/* remove ref for this client */
3424 	if (found->ar_ibt_hdl_list == NULL && found->ar_waiters == 0) {
3425 		/* last entry was removed */
3426 		found->ar_flags = IBCM_AR_INITING; /* hold off register_ar */
3427 		saa_handle = found->ar_saa_handle;
3428 		srv_recp = found->ar_srv_recp;
3429 
3430 		/* wait if this service record is being rewritten */
3431 		while (found->ar_rewrite_state == IBCM_REWRITE_BUSY)
3432 			cv_wait(&ibcm_svc_info_cv, &ibcm_svc_info_lock);
3433 		mutex_exit(&ibcm_svc_info_lock);
3434 
3435 		/* remove service record */
3436 		status = ibcm_write_service_record(saa_handle, srv_recp,
3437 		    IBMF_SAA_DELETE);
3438 		if (status != IBT_SUCCESS)
3439 			IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3440 			    "IBMF_SAA failed to delete address record");
3441 		mutex_enter(&ibcm_svc_info_lock);
3442 		if (found->ar_waiters == 0) {	/* still no waiters */
3443 			linkp = &ibcm_ar_list;
3444 			tmp = *linkp;
3445 			while (tmp != found) {
3446 				linkp = &tmp->ar_link;
3447 				tmp = *linkp;
3448 			}
3449 			*linkp = tmp->ar_link;
3450 			ibcm_dec_hca_acc_cnt(found->ar_hcap);
3451 			kmem_free(srv_recp, sizeof (*srv_recp));
3452 			cv_destroy(&found->ar_cv);
3453 			kmem_free(found, sizeof (*found));
3454 		} else {
3455 			/* add service record back in for the waiters */
3456 			mutex_exit(&ibcm_svc_info_lock);
3457 			status = ibcm_write_service_record(saa_handle, srv_recp,
3458 			    IBMF_SAA_UPDATE);
3459 			mutex_enter(&ibcm_svc_info_lock);
3460 			if (status == IBT_SUCCESS)
3461 				found->ar_flags = IBCM_AR_SUCCESS;
3462 			else {
3463 				found->ar_flags = IBCM_AR_FAILED;
3464 				IBTF_DPRINTF_L2(cmlog, "ibt_deregister_ar: "
3465 				    "IBMF_SAA failed to write address record");
3466 			}
3467 			cv_broadcast(&found->ar_cv);
3468 		}
3469 	}
3470 	mutex_exit(&ibcm_svc_info_lock);
3471 	kmem_free(hdlp, sizeof (*hdlp));
3472 	ibtl_cm_change_service_cnt(ibt_hdl, -1);
3473 	return (status);
3474 }
3475 
3476 ibt_status_t
3477 ibt_query_ar(ib_gid_t *sgid, ibt_ar_t *queryp, ibt_ar_t *resultp)
3478 {
3479 	sa_service_record_t	svcrec_req;
3480 	sa_service_record_t	*svcrec_resp;
3481 	void			*results_p;
3482 	uint64_t		component_mask = 0;
3483 	uint64_t		gid_ored;
3484 	size_t			length;
3485 	int			num_rec;
3486 	int			i;
3487 	ibmf_saa_access_args_t	access_args;
3488 	ibt_status_t		retval;
3489 	ibtl_cm_hca_port_t	cm_port;
3490 	ibcm_hca_info_t		*hcap;
3491 	ibmf_saa_handle_t	saa_handle;
3492 
3493 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar(%p, %p)", queryp, resultp);
3494 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: sgid %llx:%llx",
3495 	    (longlong_t)sgid->gid_prefix, (longlong_t)sgid->gid_guid);
3496 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: query_pkey %x", queryp->ar_pkey);
3497 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: query_gid %llx:%llx",
3498 	    (longlong_t)queryp->ar_gid.gid_prefix,
3499 	    (longlong_t)queryp->ar_gid.gid_guid);
3500 
3501 	/*
3502 	 * If P_Key is 0, but GID is not, this query is invalid.
3503 	 * If GID is 0, but P_Key is not, this query is invalid.
3504 	 */
3505 	gid_ored = queryp->ar_gid.gid_guid | queryp->ar_gid.gid_prefix;
3506 	if ((queryp->ar_pkey == 0 && gid_ored != 0ULL) ||
3507 	    (queryp->ar_pkey != 0 && gid_ored == 0ULL)) {
3508 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: GID/P_Key is not valid");
3509 		return (IBT_INVALID_PARAM);
3510 	}
3511 
3512 	hcap = NULL;
3513 	if (ibtl_cm_get_hca_port(*sgid, 0, &cm_port) != IBT_SUCCESS ||
3514 	    (hcap = ibcm_find_hca_entry(cm_port.hp_hca_guid)) == NULL ||
3515 	    (saa_handle = ibcm_get_saa_handle(hcap, cm_port.hp_port)) == NULL) {
3516 		if (hcap != NULL)
3517 			ibcm_dec_hca_acc_cnt(hcap);
3518 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: sgid is not valid");
3519 		return (IBT_INVALID_PARAM);
3520 	}
3521 
3522 	bzero(&svcrec_req, sizeof (svcrec_req));
3523 
3524 	/* Is GID/P_Key Specified. */
3525 	if (queryp->ar_pkey != 0) {	/* GID is non-zero from check above */
3526 		svcrec_req.ServiceP_Key = queryp->ar_pkey;
3527 		component_mask |= SA_SR_COMPMASK_PKEY;
3528 		IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: P_Key %X",
3529 		    queryp->ar_pkey);
3530 		svcrec_req.ServiceGID = queryp->ar_gid;
3531 		component_mask |= SA_SR_COMPMASK_GID;
3532 		IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: GID %llX:%llX",
3533 		    (longlong_t)queryp->ar_gid.gid_prefix,
3534 		    (longlong_t)queryp->ar_gid.gid_guid);
3535 	}
3536 
3537 	/* Is ServiceData Specified. */
3538 	for (i = 0; i < IBCM_DAPL_ATS_NBYTES; i++) {
3539 		if (queryp->ar_data[i] != 0) {
3540 			bcopy(queryp->ar_data, svcrec_req.ServiceData,
3541 			    IBCM_DAPL_ATS_NBYTES);
3542 			component_mask |= 0xFFFF << 7;	/* all 16 Data8 */
3543 							/* components */
3544 			break;
3545 		}
3546 	}
3547 
3548 	/* Service Name */
3549 	(void) strcpy((char *)svcrec_req.ServiceName, IBCM_DAPL_ATS_NAME);
3550 	component_mask |= SA_SR_COMPMASK_NAME;
3551 
3552 	svcrec_req.ServiceID = IBCM_DAPL_ATS_SID;
3553 	component_mask |= SA_SR_COMPMASK_ID;
3554 
3555 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: "
3556 	    "Perform SA Access: Mask: 0x%X", component_mask);
3557 
3558 	/*
3559 	 * Call in SA Access retrieve routine to get Service Records.
3560 	 *
3561 	 * SA Access framework allocated memory for the "results_p".
3562 	 * Make sure to deallocate once we are done with the results_p.
3563 	 * The size of the buffer allocated will be as returned in
3564 	 * "length" field.
3565 	 */
3566 	access_args.sq_attr_id = SA_SERVICERECORD_ATTRID;
3567 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
3568 	access_args.sq_component_mask = component_mask;
3569 	access_args.sq_template = &svcrec_req;
3570 	access_args.sq_template_length = sizeof (sa_service_record_t);
3571 	access_args.sq_callback = NULL;
3572 	access_args.sq_callback_arg = NULL;
3573 
3574 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
3575 	    &results_p);
3576 
3577 	ibcm_dec_hca_acc_cnt(hcap);
3578 	if (retval != IBT_SUCCESS) {
3579 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: SA Access Failed");
3580 		return (retval);
3581 	}
3582 
3583 	num_rec = length / sizeof (sa_service_record_t);
3584 
3585 	IBTF_DPRINTF_L3(cmlog, "ibt_query_ar: "
3586 	    "Found %d Service Records.", num_rec);
3587 
3588 	/* Validate the returned number of records. */
3589 	if ((results_p != NULL) && (num_rec > 0)) {
3590 		uint8_t		*b;
3591 
3592 		/* Just return info from the first service record. */
3593 		svcrec_resp = (sa_service_record_t *)results_p;
3594 
3595 		/* The Service GID and Service ID */
3596 		resultp->ar_gid = svcrec_resp->ServiceGID;
3597 		resultp->ar_pkey = svcrec_resp->ServiceP_Key;
3598 		bcopy(svcrec_resp->ServiceData,
3599 		    resultp->ar_data, IBCM_DAPL_ATS_NBYTES);
3600 
3601 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: "
3602 		    "Found: pkey %x dgid %llX:%llX", resultp->ar_pkey,
3603 		    (longlong_t)resultp->ar_gid.gid_prefix,
3604 		    (longlong_t)resultp->ar_gid.gid_guid);
3605 		b = resultp->ar_data;
3606 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar:"
3607 		    " data %d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
3608 		    b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9],
3609 		    b[10], b[11], b[12], b[13], b[14], b[15]);
3610 
3611 		/* Deallocate the memory for results_p. */
3612 		kmem_free(results_p, length);
3613 		if (num_rec > 1)
3614 			retval = IBT_MULTIPLE_AR;
3615 		else
3616 			retval = IBT_SUCCESS;
3617 	} else {
3618 		IBTF_DPRINTF_L2(cmlog, "ibt_query_ar: "
3619 		    "ibmf_sa_access found 0 matching records");
3620 		retval = IBT_AR_NOT_REGISTERED;
3621 	}
3622 	return (retval);
3623 }
3624 
3625 /* mark all ATS service records associated with the port */
3626 static void
3627 ibcm_mark_ar(ib_guid_t hca_guid, uint8_t port)
3628 {
3629 	ibcm_ar_t	*tmp;
3630 
3631 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
3632 	for (tmp = ibcm_ar_list; tmp != NULL; tmp = tmp->ar_link) {
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 		uint8_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'ing just IP_HDR part */
6240 	bzero(priv_data_p, IBT_IP_HDR_PRIV_DATA_SZ);
6241 	ip_data = (ibcm_ip_pvtdata_t *)priv_data_p;
6242 	ip_data->ip_srcport = b2h16(ip_cm_info->src_port); /* Source Port */
6243 
6244 	/* IPV = 0x4, if IP-Addr are IPv4 format, else 0x6 for IPv6 */
6245 	if (ip_cm_info->src_addr.family == AF_INET) {
6246 		ip_data->ip_ipv = IBT_CM_IP_IPV_V4;
6247 		ip_data->ip_srcv4 = ntohl(ip_cm_info->src_addr.un.ip4addr);
6248 		ip_data->ip_dstv4 = ntohl(ip_cm_info->dst_addr.un.ip4addr);
6249 	} else if (ip_cm_info->src_addr.family == AF_INET6) {
6250 		ip_data->ip_ipv = IBT_CM_IP_IPV_V6;
6251 		bcopy(&ip_cm_info->src_addr.un.ip6addr,
6252 		    &ip_data->ip_srcv6, sizeof (in6_addr_t));
6253 		bcopy(&ip_cm_info->dst_addr.un.ip6addr,
6254 		    &ip_data->ip_dstv6, sizeof (in6_addr_t));
6255 	} else {
6256 		IBTF_DPRINTF_L2(cmlog, "ibt_format_ip_private_data: ERROR "
6257 		    "IP Addr needs to be either AF_INET or AF_INET6 family.");
6258 		return (IBT_INVALID_PARAM);
6259 	}
6260 
6261 	ip_data->ip_MajV = IBT_CM_IP_MAJ_VER;
6262 	ip_data->ip_MinV = IBT_CM_IP_MIN_VER;
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 	bzero(ip_cm_infop, sizeof (ibt_ip_cm_info_t));
6284 
6285 	ip_data = (ibcm_ip_pvtdata_t *)priv_data;
6286 	ip_cm_infop->src_port = b2h16(ip_data->ip_srcport); /* Source Port */
6287 
6288 	/* IPV = 0x4, if IP Address are IPv4 format, else 0x6 for IPv6 */
6289 	if (ip_data->ip_ipv == IBT_CM_IP_IPV_V4) {
6290 		/* Copy IPv4 Addr */
6291 		ip_cm_infop->src_addr.family = AF_INET;
6292 		ip_cm_infop->src_addr.un.ip4addr = ntohl(ip_data->ip_srcv4);
6293 		ip_cm_infop->dst_addr.family = AF_INET;
6294 		ip_cm_infop->dst_addr.un.ip4addr = ntohl(ip_data->ip_dstv4);
6295 	} else if (ip_data->ip_ipv == IBT_CM_IP_IPV_V6) {
6296 		/* Copy IPv6 Addr */
6297 		ip_cm_infop->src_addr.family = AF_INET6;
6298 		bcopy(&ip_data->ip_srcv6, &ip_cm_infop->src_addr.un.ip6addr,
6299 		    sizeof (in6_addr_t));
6300 		ip_cm_infop->dst_addr.family = AF_INET6;
6301 		bcopy(&ip_data->ip_dstv6, &ip_cm_infop->dst_addr.un.ip6addr,
6302 		    sizeof (in6_addr_t));
6303 	} else {
6304 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_data: ERROR: IP Addr needs"
6305 		    " to be either AF_INET or AF_INET6 family.");
6306 		return (IBT_INVALID_PARAM);
6307 	}
6308 
6309 	return (IBT_SUCCESS);
6310 }
6311 
6312 
6313 /* Routines for warlock */
6314 
6315 /* ARGSUSED */
6316 static void
6317 ibcm_dummy_mcg_handler(void *arg, ibt_status_t retval, ibt_mcg_info_t *minfo)
6318 {
6319 	ibcm_join_mcg_tqarg_t	dummy_mcg;
6320 
6321 	dummy_mcg.func = ibcm_dummy_mcg_handler;
6322 
6323 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_mcg_handler: "
6324 	    "dummy_mcg.func %p", dummy_mcg.func);
6325 }
6326 
6327 
6328 /* ARGSUSED */
6329 static void
6330 ibcm_dummy_recycle_rc_handler(ibt_status_t retval, void *arg)
6331 {
6332 	ibcm_taskq_recycle_arg_t	dummy_rc_recycle;
6333 
6334 	dummy_rc_recycle.func = ibcm_dummy_recycle_rc_handler;
6335 
6336 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_recycle_rc_handler: "
6337 	    "dummy_rc_recycle.func %p", dummy_rc_recycle.func);
6338 }
6339 
6340 
6341 /* ARGSUSED */
6342 static ibt_cm_status_t
6343 ibcm_dummy_ud_handler(void *priv, ibt_cm_ud_event_t *event,
6344     ibt_cm_ud_return_args_t *ret_args,
6345     void *priv_data, ibt_priv_data_len_t len)
6346 {
6347 	/*
6348 	 * Let warlock see that ibcm_local_handler_s::actual_cm_handler
6349 	 * points to this routine.
6350 	 */
6351 	ibcm_local_handler_t	p;
6352 	ibcm_ud_state_data_t	dummy_ud;
6353 
6354 	p.actual_cm_handler = ibcm_dummy_ud_handler;
6355 	dummy_ud.ud_cm_handler = ibcm_dummy_ud_handler;
6356 
6357 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_ud_handler: p.actual_cm_handler %p"
6358 	    "dummy_ud.ud_cm_handler %p", p.actual_cm_handler,
6359 	    dummy_ud.ud_cm_handler);
6360 	/*
6361 	 * Call all routines that the client's callback routine could call.
6362 	 */
6363 
6364 	return (IBT_CM_ACCEPT);
6365 }
6366 
6367 /* ARGSUSED */
6368 static ibt_cm_status_t
6369 ibcm_dummy_rc_handler(void *priv, ibt_cm_event_t *event,
6370     ibt_cm_return_args_t *ret_args, void *priv_data, ibt_priv_data_len_t len)
6371 {
6372 	ibcm_state_data_t	dummy_rc;
6373 
6374 	dummy_rc.cm_handler = ibcm_dummy_rc_handler;
6375 
6376 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_rc_handler: "
6377 	    "dummy_ud.ud_cm_handler %p", dummy_rc.cm_handler);
6378 	/*
6379 	 * Call all routines that the client's callback routine could call.
6380 	 */
6381 
6382 	return (IBT_CM_ACCEPT);
6383 }
6384