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