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