xref: /illumos-gate/usr/src/uts/common/io/ib/mgt/ibcm/ibcm_utils.c (revision 9d3d2ed09c8e9ba0b2ba44fdd1dd300b2c3f9e8e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * ibcm_utils.c
31  *
32  * contains internal lookup functions of IB CM module
33  * along with some other miscellaneous stuff
34  *
35  * TBD:
36  * 1. Code needed to ensure that if any clients are using a service then
37  * don't de-register it.
38  */
39 
40 #include <sys/ib/mgt/ibcm/ibcm_impl.h>
41 #include <sys/ddi.h>
42 
43 
44 /* statics */
45 static vmem_t		*ibcm_local_sid_arena;
46 static ib_svc_id_t	ibcm_local_sid_seed;
47 static ib_com_id_t	ibcm_local_cid_seed;
48 _NOTE(READ_ONLY_DATA({ibcm_local_sid_arena ibcm_local_sid_seed
49     ibcm_local_cid_seed}))
50 static void		ibcm_delete_state_from_avl(ibcm_state_data_t *statep);
51 static void		ibcm_init_conn_trace(ibcm_state_data_t *statep);
52 static void		ibcm_fini_conn_trace(ibcm_state_data_t *statep);
53 static void		ibcm_dump_conn_trbuf(void *statep, char *line_prefix,
54 			    char *buf, int buf_size);
55 
56 /*
57  * ibcm_lookup_msg:
58  *
59  * Retrieves an existing state structure or creates a new one if none found.
60  * This function is used during
61  *	Passive connection side for INCOMING REQ/REJ/RTU/MRA/DREQ/DREP/LAP msgs
62  *	Active connection side for INCOMING REP/REJ/MRA/DREQ/DREP/APR msgs
63  *	Active side CM for outgoing REQ message.
64  *
65  * NOTE: Only return IBCM_LOOKUP_FAIL if lookup failed to find a match.
66  *
67  * Arguments are:-
68  *	event_type	- type of message
69  *			incoming REQ, REP, REJ, MRA, RTU
70  *	remote_qpn	- Remote QP number
71  *	comid		- local/remote comid
72  *	remote_hca_guid	- Remote HCA GUID
73  *	hcap		- HCA entry ptr
74  *	rstatep		- return statep pointer
75  *
76  * Return Values:
77  *	IBCM_LOOKUP_NEW		- new statep allocated
78  *	IBCM_LOOKUP_EXISTS	- found an existing entry
79  *	IBCM_LOOKUP_FAIL	- No lookup entry found
80  *	IBCM_MEMORY_FAILURE	- Memory allocs failed
81  */
82 ibcm_status_t
83 ibcm_lookup_msg(ibcm_event_type_t event_type, ib_com_id_t comid,
84     ib_qpn_t remote_qpn, ib_guid_t remote_hca_guid, ibcm_hca_info_t *hcap,
85     ibcm_state_data_t **rstatep)
86 {
87 	avl_index_t		where;
88 	ibcm_state_data_t	*sp;
89 
90 	IBTF_DPRINTF_L4(cmlog, "ibcm_lookup_msg: event = 0x%x, comid = 0x%x",
91 	    event_type, comid);
92 	IBTF_DPRINTF_L4(cmlog, "ibcm_lookup_msg: rem_qpn = 0x%lX, "
93 	    "rem_hca_guid = 0x%llX", remote_qpn, remote_hca_guid);
94 
95 	ASSERT(rw_lock_held(&hcap->hca_state_rwlock));
96 
97 	/*
98 	 * Lookup in "hca_passive_tree" for IBCM_INCOMING_REQ and
99 	 * IBCM_INCOMING_REP_STALE;
100 	 *
101 	 * Lookup in "hca_passive_comid_tree" for IBCM_INCOMING_REQ_STALE
102 	 *
103 	 * All other lookups in "hca_active_tree".
104 	 *
105 	 * NOTE: "hca_active_tree" lookups are based on the local comid.
106 	 * "hca_passive_state_tree" lookups are based on remote QPN
107 	 * and remote hca GUID.
108 	 *
109 	 * Call avl_find to lookup in the respective tree and save result in
110 	 * "sp". If "sp" is null it implies that no match was found. If so,
111 	 * allocate a new ibcm_state_data_t and insert it into the AVL tree(s).
112 	 */
113 	if ((event_type == IBCM_INCOMING_REQ) ||
114 	    (event_type == IBCM_INCOMING_REP_STALE)) {
115 		ibcm_passive_node_info_t	info;
116 
117 		info.info_qpn = remote_qpn;
118 		info.info_hca_guid = remote_hca_guid;
119 
120 		/* Lookup based on Remote QPN and Remote GUID in Passive Tree */
121 		sp = avl_find(&hcap->hca_passive_tree, &info, &where);
122 	} else if ((event_type == IBCM_INCOMING_REQ_STALE) ||
123 	    (event_type == IBCM_INCOMING_REJ_RCOMID)) {
124 		ibcm_passive_comid_node_info_t	info;
125 
126 		info.info_comid = comid;
127 		info.info_hca_guid = remote_hca_guid;
128 
129 		/* Lookup based on Remote COMID in Passive Tree */
130 		sp = avl_find(&hcap->hca_passive_comid_tree, &info, &where);
131 	} else {	/* any other event including IBCM_OUTGOING_REQ */
132 		/* Lookup based on Local comid in Active Tree */
133 		sp = avl_find(&hcap->hca_active_tree, &comid, &where);
134 	}
135 
136 	/* matching entry found !! */
137 	if (sp != NULL) {
138 		IBTF_DPRINTF_L4(cmlog, "ibcm_lookup_msg: match found "
139 		    "statep = %p", sp);
140 		if (event_type == IBCM_INCOMING_REQ)
141 			kmem_free(*rstatep, sizeof (ibcm_state_data_t));
142 		*rstatep = sp;		/* return the matched statep */
143 
144 		mutex_enter(&(sp->state_mutex));
145 		IBCM_REF_CNT_INCR(sp); /* increment the ref count */
146 		mutex_exit(&(sp->state_mutex));
147 
148 		return (IBCM_LOOKUP_EXISTS);
149 	}
150 
151 	/*
152 	 * If we came here then it implies that CM didn't
153 	 * find a matching entry. We will create a new entry in avl tree,
154 	 * if event_type is INCOMING/OUTGOING REQ, REQ_STALE/REP_STALE.
155 	 * statep is created for INCOMING/OUTGOING REQ.
156 	 * For all other event_types we return lookup failure
157 	 */
158 	if (!((event_type == IBCM_INCOMING_REQ) ||
159 	    (event_type == IBCM_INCOMING_REQ_STALE) ||
160 	    (event_type == IBCM_INCOMING_REP_STALE) ||
161 	    (event_type == IBCM_OUTGOING_REQ))) {
162 		IBTF_DPRINTF_L2(cmlog, "ibcm_lookup_msg: failed for "
163 		    "event type %x remote_comid = 0x%x",
164 		    event_type, comid);
165 
166 		return (IBCM_LOOKUP_FAIL);
167 	}
168 
169 	if ((event_type == IBCM_INCOMING_REQ) ||
170 	    (event_type == IBCM_OUTGOING_REQ)) {
171 
172 		/* fill in the new ibcm_state_data */
173 		sp = *rstatep;
174 
175 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sp))
176 
177 		/* initialize statep */
178 		mutex_init(&sp->state_mutex, NULL, MUTEX_DEFAULT, NULL);
179 		cv_init(&sp->block_client_cv, NULL, CV_DRIVER, NULL);
180 		cv_init(&sp->block_mad_cv, NULL, CV_DRIVER, NULL);
181 
182 		sp->hcap = hcap;
183 		IBCM_REF_CNT_INCR(sp);
184 		sp->local_comid = comid;
185 
186 		if (ibcm_enable_trace != 0)
187 			ibcm_init_conn_trace(sp);
188 
189 		if (event_type == IBCM_INCOMING_REQ) {	/* Passive side */
190 			sp->state = IBCM_STATE_REQ_RCVD;
191 			sp->clnt_proceed = IBCM_BLOCK;
192 			sp->close_nocb_state = IBCM_UNBLOCK;
193 			sp->remote_hca_guid = remote_hca_guid;
194 			sp->remote_qpn = remote_qpn;
195 
196 		} else if (event_type == IBCM_OUTGOING_REQ) { /* Active side */
197 			sp->close_nocb_state = IBCM_UNBLOCK;
198 			sp->state = IBCM_STATE_IDLE;
199 		}
200 
201 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sp))
202 
203 	} else {
204 		sp = *rstatep;	/* for incoming REQ/REP STALE only */
205 	}
206 
207 	if ((event_type == IBCM_INCOMING_REQ) ||
208 	    (event_type == IBCM_INCOMING_REP_STALE)) {
209 
210 		/* First, insert a new "sp" into "hca_passive_tree" @ "where" */
211 		avl_insert(&(hcap->hca_passive_tree), (void *)sp, where);
212 
213 		if (event_type == IBCM_INCOMING_REQ) {	/* Only INCOMING_REQ */
214 			/*
215 			 * We have to do an avl_find() to figure out
216 			 * "where" to insert the statep into the active tree.
217 			 *
218 			 * CM doesn't care for avl_find's retval.
219 			 */
220 			(void) avl_find(&hcap->hca_active_tree,
221 			    &sp->local_comid, &where);
222 
223 			/* Next, insert the "sp" into "hca_active_tree" */
224 			avl_insert(&hcap->hca_active_tree, (void *)sp, where);
225 		}
226 	} else if (event_type == IBCM_INCOMING_REQ_STALE) {
227 		avl_insert(&(hcap->hca_passive_comid_tree), (void *)sp, where);
228 	} else {	/* IBCM_OUTGOING_REQ */
229 		/* Insert the new sp only into "hca_active_tree", @ "where" */
230 		avl_insert(&(hcap->hca_active_tree), (void *)sp, where);
231 	}
232 
233 	return (IBCM_LOOKUP_NEW);	/* return new lookup */
234 }
235 
236 
237 /*
238  * ibcm_active_node_compare:
239  * 	- AVL active tree node compare
240  *
241  * Arguments:
242  *	p1	: pointer to local comid
243  *	p2	: pointer to passed ibcm_state_data_t
244  *
245  * Return values:
246  *	0	: match found
247  *	-1	: no match but insert to left side of the tree
248  *	+1	: no match but insert to right side of the tree
249  */
250 int
251 ibcm_active_node_compare(const void *p1, const void *p2)
252 {
253 	ib_com_id_t		*local_comid = (ib_com_id_t *)p1;
254 	ibcm_state_data_t	*statep = (ibcm_state_data_t *)p2;
255 
256 	IBTF_DPRINTF_L5(cmlog, "ibcm_active_node_compare: "
257 	    "comid: 0x%x, statep: 0x%p", *local_comid, statep);
258 
259 	if (*local_comid > statep->local_comid) {
260 		return (+1);
261 	} else if (*local_comid < statep->local_comid) {
262 		return (-1);
263 	} else {
264 		return (0);
265 	}
266 }
267 
268 
269 /*
270  * ibcm_passive_node_compare:
271  * 	- AVL passive tree node compare (passive side)
272  *
273  * Arguments:
274  *	p1	: pointer to ibcm_passive_node_info (remote qpn and remote guid)
275  *	p2	: pointer to passed ibcm_state_data_t
276  *
277  * Return values:
278  *	0	: match found
279  *	-1	: no match but insert to left side of the tree
280  *	+1	: no match but insert to right side of the tree
281  */
282 int
283 ibcm_passive_node_compare(const void *p1, const void *p2)
284 {
285 	ibcm_state_data_t		*statep = (ibcm_state_data_t *)p2;
286 	ibcm_passive_node_info_t	*infop = (ibcm_passive_node_info_t *)p1;
287 
288 	IBTF_DPRINTF_L5(cmlog, "ibcm_passive_node_compare: "
289 	    "statep: 0x%p, p1: 0x%p", statep, p1);
290 
291 	/*
292 	 * PASSIVE SIDE: (REQ, REP, MRA, REJ)
293 	 *	always search by active COMID
294 	 */
295 	if (infop->info_qpn > statep->remote_qpn) {
296 		return (+1);
297 	} else if (infop->info_qpn < statep->remote_qpn) {
298 		return (-1);
299 	} else {
300 		if (infop->info_hca_guid < statep->remote_hca_guid) {
301 			return (-1);
302 		} else if (infop->info_hca_guid > statep->remote_hca_guid) {
303 			return (+1);
304 		} else {
305 			return (0);
306 		}
307 	}
308 }
309 
310 /*
311  * ibcm_passive_comid_node_compare:
312  * 	- AVL passive comid tree node compare (passive side)
313  *
314  * Arguments:
315  *	p1	: pointer to ibcm_passive_comid_node_info
316  *		  (remote comid and remote guid)
317  *	p2	: pointer to passed ibcm_state_data_t
318  *
319  * Return values:
320  *	0	: match found
321  *	-1	: no match but insert to left side of the tree
322  *	+1	: no match but insert to right side of the tree
323  */
324 int
325 ibcm_passive_comid_node_compare(const void *p1, const void *p2)
326 {
327 	ibcm_state_data_t		*statep = (ibcm_state_data_t *)p2;
328 	ibcm_passive_comid_node_info_t	*infop =
329 	    (ibcm_passive_comid_node_info_t *)p1;
330 
331 	IBTF_DPRINTF_L5(cmlog, "ibcm_passive_comid_node_compare: "
332 	    "statep: 0x%p, p1: 0x%p", statep, p1);
333 
334 	if (infop->info_comid > statep->remote_comid) {
335 		return (+1);
336 	} else if (infop->info_comid < statep->remote_comid) {
337 		return (-1);
338 	} else {
339 		if (infop->info_hca_guid < statep->remote_hca_guid) {
340 			return (-1);
341 		} else if (infop->info_hca_guid > statep->remote_hca_guid) {
342 			return (+1);
343 		} else {
344 			return (0);
345 		}
346 	}
347 }
348 
349 
350 void
351 ibcm_delete_state_from_avl(ibcm_state_data_t *statep)
352 {
353 	avl_index_t			a_where = 0;
354 	avl_index_t			p_where = 0;
355 	avl_index_t			pcomid_where = 0;
356 	ibcm_hca_info_t			*hcap;
357 	ibcm_state_data_t		*active_nodep, *passive_nodep,
358 					*passive_comid_nodep;
359 	ibcm_passive_node_info_t	info;
360 	ibcm_passive_comid_node_info_t	info_comid;
361 
362 	IBTF_DPRINTF_L4(cmlog, "ibcm_delete_state_from_avl: statep 0x%p",
363 	    statep);
364 
365 	if (statep == NULL) {
366 		IBTF_DPRINTF_L2(cmlog, "ibcm_delete_state_from_avl: statep"
367 		    " NULL");
368 		return;
369 	}
370 
371 	hcap = statep->hcap;
372 
373 	/*
374 	 * Once the avl tree lock is acquired, no other thread can increment
375 	 * ref cnt, until tree lock is exit'ed. Since the statep is removed
376 	 * from the avl's after acquiring lock below, no other thread can
377 	 * increment the ref cnt after acquiring the lock below
378 	 */
379 
380 	rw_enter(&hcap->hca_state_rwlock, RW_WRITER);
381 
382 	/* Lookup based on Local comid in the active tree */
383 	active_nodep = avl_find(&hcap->hca_active_tree, &(statep->local_comid),
384 	    &a_where);
385 
386 	/* Lookup based on Remote QPN and Remote GUID in the passive tree */
387 	info.info_qpn = statep->remote_qpn;
388 	info.info_hca_guid = statep->remote_hca_guid;
389 	passive_nodep =  avl_find(&hcap->hca_passive_tree, &info, &p_where);
390 
391 	/* Lookup based on Remote Comid and Remote GUID in the passive tree */
392 	info_comid.info_comid = statep->remote_comid;
393 	info_comid.info_hca_guid = statep->remote_hca_guid;
394 	passive_comid_nodep =  avl_find(&hcap->hca_passive_comid_tree,
395 	    &info_comid, &pcomid_where);
396 
397 	/* remove it from the tree, destroy record and the nodep */
398 	if (active_nodep == statep) {
399 		avl_remove(&hcap->hca_active_tree, active_nodep);
400 	}
401 
402 	if (passive_nodep == statep) {
403 		avl_remove(&hcap->hca_passive_tree, passive_nodep);
404 	}
405 
406 	if (passive_comid_nodep == statep) {
407 		avl_remove(&hcap->hca_passive_comid_tree, passive_comid_nodep);
408 	}
409 
410 	rw_exit(&hcap->hca_state_rwlock);
411 }
412 
413 /*
414  * ibcm_dealloc_state_data:
415  *	Deallocates all buffers and the memory of state structure
416  * This routine can be called on statep that has ref_cnt of 0, and that is
417  * already deleted from the avl tree's
418  *
419  * Arguments are:-
420  *	statep	- statep to be deleted
421  *
422  * Return Values:	NONE
423  */
424 void
425 ibcm_dealloc_state_data(ibcm_state_data_t *statep)
426 {
427 	timeout_id_t timer_val;
428 	int dump_trace;
429 	IBTF_DPRINTF_L4(cmlog, "ibcm_dealloc_state_data: statep 0x%p", statep);
430 
431 	if (statep == NULL) {
432 		IBTF_DPRINTF_L2(cmlog, "ibcm_dealloc_state_data: statep NULL");
433 		return;
434 	}
435 
436 	/* ref_cnt is 0 */
437 	/* If timer is running - expire it */
438 	mutex_enter(&statep->state_mutex);
439 	timer_val = statep->timerid;
440 	if (timer_val != 0) {
441 		statep->timerid = 0;
442 		mutex_exit(&statep->state_mutex);
443 		(void) untimeout(timer_val);
444 	} else
445 		mutex_exit(&statep->state_mutex);
446 
447 	/* release the ref cnt on the associated ibmf qp */
448 	if (statep->stored_reply_addr.cm_qp_entry != NULL)
449 		ibcm_release_qp(statep->stored_reply_addr.cm_qp_entry);
450 
451 	if (statep->stored_msg != NULL)
452 		(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl,
453 		    &statep->stored_msg);
454 
455 	if (statep->dreq_msg != NULL)
456 		(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl,
457 		    &statep->dreq_msg);
458 
459 	if (statep->drep_msg != NULL)
460 		(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl,
461 		    &statep->drep_msg);
462 
463 	if (statep->mra_msg != NULL)
464 		(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl,
465 		    &statep->mra_msg);
466 
467 	if (statep->lapr_msg != NULL)
468 		(void) ibcm_free_out_msg(statep->stored_reply_addr.ibmf_hdl,
469 		    &statep->lapr_msg);
470 
471 	if (statep->defer_cm_msg != NULL)
472 		kmem_free(statep->defer_cm_msg, IBCM_MSG_SIZE);
473 
474 	IBTF_DPRINTF_L4(cmlog, "ibcm_dealloc_state_data: done for sp = 0x%p",
475 	    statep);
476 
477 	/* Ensure the thread doing ref cnt decr releases the mutex */
478 	mutex_enter(&statep->state_mutex);
479 	dump_trace = statep->cm_retries > 0;
480 	mutex_exit(&statep->state_mutex);
481 
482 	/*
483 	 * now call the mutex_destroy() and cv_destroy()
484 	 */
485 	mutex_destroy(&statep->state_mutex);
486 
487 	cv_destroy(&statep->block_client_cv);
488 	cv_destroy(&statep->block_mad_cv);
489 
490 	/* free the comid */
491 	ibcm_free_comid(statep->hcap, statep->local_comid);
492 
493 	/* Decrement the resource on hcap */
494 	ibcm_dec_hca_res_cnt(statep->hcap);
495 
496 	/* dump the trace data into ibtf_debug_buf */
497 	if ((ibcm_enable_trace & 4) || dump_trace)
498 		ibcm_dump_conn_trace(statep);
499 
500 	ibcm_fini_conn_trace(statep);
501 
502 	/* free the statep */
503 	kmem_free(statep, sizeof (ibcm_state_data_t));
504 }
505 
506 /*
507  * ibcm_delete_state_data:
508  *	Deletes the state from avl trees, and tries to deallocate state
509  *
510  * Arguments are:-
511  *	statep	- statep to be deleted
512  *
513  * Return Values:	NONE
514  */
515 void
516 ibcm_delete_state_data(ibcm_state_data_t *statep)
517 {
518 	IBTF_DPRINTF_L4(cmlog, "ibcm_delete_state_data:");
519 
520 	ibcm_delete_state_from_avl(statep);
521 
522 	/* Must acquire the state mutex to set delete_state_data */
523 	mutex_enter(&statep->state_mutex);
524 	if (statep->ref_cnt > 0) {
525 		statep->delete_state_data = B_TRUE;
526 		IBTF_DPRINTF_L4(cmlog, "ibcm_delete_state_data: statep 0x%p "
527 		    "ref_cnt = %x", statep, statep->ref_cnt);
528 		mutex_exit(&statep->state_mutex);
529 		return;
530 	}
531 	mutex_exit(&statep->state_mutex);
532 
533 	ibcm_dealloc_state_data(statep);
534 }
535 
536 /*
537  * ibcm_find_sidr_entry:
538  *	Routines for CM SIDR state structure list manipulation.
539  *	Finds an entry based on lid, gid and grh exists fields
540  *
541  * INPUTS:
542  *	lid:		LID of incoming SIDR REQ
543  *	gid:		GID of incoming SIDR REQ
544  *	grh_exists:	TRUE if GRH exists in the incoming SIDR REQ
545  *	req_id:		Request ID
546  *	hcap:		CM State table to search for SIDR state structure
547  *	statep:		Returns a valid state structure, if one exists based
548  *			on lid, gid and grh_exists fields
549  *	flag:		IBCM_FLAG_LOOKUP - just lookup
550  *			IBCM_FLAG_LOOKUP_AND_ADD - if lookup fails, add it.
551  * Return Values:
552  *	IBCM_LOOKUP_EXISTS	- found an existing entry
553  *	IBCM_LOOKUP_FAIL	- failed to find an entry
554  *	IBCM_LOOKUP_NEW		- created a new entry
555  */
556 ibcm_status_t
557 ibcm_find_sidr_entry(ibcm_sidr_srch_t *srch_param, ibcm_hca_info_t *hcap,
558     ibcm_ud_state_data_t **ud_statep, ibcm_lookup_flag_t flag)
559 {
560 	ibcm_status_t		status;
561 	ibcm_ud_state_data_t	*usp;
562 
563 	IBTF_DPRINTF_L5(cmlog, "ibcm_find_sidr_entry: srch_params are:"
564 	    "lid=%x, (%llX, %llX), grh: %x, id: %x",
565 	    srch_param->srch_lid, srch_param->srch_gid.gid_prefix,
566 	    srch_param->srch_gid.gid_guid, srch_param->srch_grh_exists,
567 	    srch_param->srch_req_id);
568 
569 	if (flag == IBCM_FLAG_ADD) {
570 		*ud_statep = ibcm_add_sidr_entry(srch_param, hcap);
571 		return (IBCM_LOOKUP_NEW);
572 	}
573 
574 	usp = hcap->hca_sidr_list;	/* Point to the list */
575 
576 	/* traverse the list for a matching entry */
577 	while (usp != NULL) {
578 		IBTF_DPRINTF_L5(cmlog, "ibcm_find_sidr_entry: "
579 		    "lid=%x, (%llX, %llX), grh: %x, id: %x",
580 		    usp->ud_sidr_req_lid, usp->ud_sidr_req_gid.gid_prefix,
581 		    usp->ud_sidr_req_gid.gid_guid, usp->ud_grh_exists,
582 		    usp->ud_req_id);
583 
584 		if ((usp->ud_sidr_req_lid == srch_param->srch_lid) &&
585 		    ((srch_param->srch_gid.gid_prefix == 0) ||
586 		    (srch_param->srch_gid.gid_prefix ==
587 			usp->ud_sidr_req_gid.gid_prefix)) &&
588 		    ((srch_param->srch_gid.gid_guid == 0) ||
589 		    (srch_param->srch_gid.gid_guid ==
590 			usp->ud_sidr_req_gid.gid_guid)) &&
591 		    (srch_param->srch_req_id == usp->ud_req_id) &&
592 		    (usp->ud_grh_exists == srch_param->srch_grh_exists) &&
593 		    (usp->ud_mode == srch_param->srch_mode)) { /* found match */
594 			*ud_statep = usp;
595 			IBTF_DPRINTF_L5(cmlog, "ibcm_find_sidr_entry: "
596 			    "found usp = %p", usp);
597 			mutex_enter(&usp->ud_state_mutex);
598 			IBCM_UD_REF_CNT_INCR(usp);
599 			mutex_exit(&usp->ud_state_mutex);
600 
601 			return (IBCM_LOOKUP_EXISTS);
602 		}
603 		usp = usp->ud_nextp;
604 	}
605 
606 	/*
607 	 * If code came here --> it couldn't find a match.
608 	 *	OR
609 	 * the "hcap->hca_sidr_list" was NULL
610 	 */
611 	if (flag == IBCM_FLAG_LOOKUP) {
612 		IBTF_DPRINTF_L3(cmlog, "ibcm_find_sidr_entry: no match found "
613 		    "lid=%x, (%llX, %llX), grh: %x, id: %x",
614 		    srch_param->srch_lid, srch_param->srch_gid.gid_prefix,
615 		    srch_param->srch_gid.gid_guid, srch_param->srch_grh_exists,
616 		    srch_param->srch_req_id);
617 		status = IBCM_LOOKUP_FAIL;
618 	} else {
619 		*ud_statep = ibcm_add_sidr_entry(srch_param, hcap);
620 		status = IBCM_LOOKUP_NEW;
621 	}
622 
623 	return (status);
624 }
625 
626 
627 /*
628  * ibcm_add_sidr_entry:
629  *	Adds a SIDR entry. Called *ONLY* from ibcm_find_sidr_entry()
630  *
631  * INPUTS:
632  *	lid:		LID of incoming SIDR REQ
633  *	gid:		GID of incoming SIDR REQ
634  *	grh_exists:	TRUE if GRH exists in the incoming SIDR REQ
635  *	req_id:		Request ID
636  *	hcap:		CM State table to search for SIDR state structure
637  * Return Values: NONE
638  */
639 ibcm_ud_state_data_t *
640 ibcm_add_sidr_entry(ibcm_sidr_srch_t *srch_param, ibcm_hca_info_t *hcap)
641 {
642 	ibcm_ud_state_data_t	*ud_statep;
643 
644 	IBTF_DPRINTF_L5(cmlog, "ibcm_add_sidr_entry: lid=%x, guid=%llX, "
645 	    "grh = %x req_id = %x", srch_param->srch_lid,
646 	    srch_param->srch_gid.gid_guid, srch_param->srch_grh_exists,
647 	    srch_param->srch_req_id);
648 
649 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*ud_statep))
650 
651 	/* didn't find the entry - so create new */
652 	ud_statep = kmem_zalloc(sizeof (ibcm_ud_state_data_t), KM_SLEEP);
653 
654 	mutex_init(&ud_statep->ud_state_mutex, NULL, MUTEX_DEFAULT, NULL);
655 	cv_init(&ud_statep->ud_block_client_cv, NULL, CV_DRIVER, NULL);
656 
657 	/* Initialize some ud_statep fields */
658 	mutex_enter(&ud_statep->ud_state_mutex);
659 	ud_statep->ud_hcap		= hcap;
660 	ud_statep->ud_req_id		= srch_param->srch_req_id;
661 	ud_statep->ud_ref_cnt		= 1;
662 	ud_statep->ud_grh_exists	= srch_param->srch_grh_exists;
663 	ud_statep->ud_sidr_req_lid	= srch_param->srch_lid;
664 	ud_statep->ud_sidr_req_gid	= srch_param->srch_gid;
665 	ud_statep->ud_mode		= srch_param->srch_mode;
666 	ud_statep->ud_max_cm_retries	= ibcm_max_retries;
667 	mutex_exit(&ud_statep->ud_state_mutex);
668 
669 	/* Update the list */
670 	ud_statep->ud_nextp = hcap->hca_sidr_list;
671 	hcap->hca_sidr_list = ud_statep;
672 
673 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*ud_statep))
674 
675 	return (ud_statep);
676 }
677 
678 
679 /*
680  * ibcm_delete_ud_state_data:
681  *	Deletes a given state structure
682  *
683  * Arguments are:-
684  *	statep	- statep to be deleted
685  *
686  * Return Values:	NONE
687  */
688 void
689 ibcm_delete_ud_state_data(ibcm_ud_state_data_t *ud_statep)
690 {
691 	ibcm_ud_state_data_t	*prevp, *headp;
692 	ibcm_hca_info_t		*hcap;
693 
694 	IBTF_DPRINTF_L4(cmlog, "ibcm_delete_ud_state_data: ud_statep 0x%p",
695 	    ud_statep);
696 
697 	if (ud_statep == NULL || ud_statep->ud_hcap == NULL) {
698 		IBTF_DPRINTF_L2(cmlog, "ibcm_delete_ud_state_data: "
699 		    "ud_statep or hcap is NULL");
700 		return;
701 	}
702 
703 	hcap = ud_statep->ud_hcap;
704 
705 	rw_enter(&hcap->hca_sidr_list_lock, RW_WRITER);
706 
707 	/* Next, remove this from the HCA SIDR list */
708 	if (hcap->hca_sidr_list != NULL) {
709 		prevp = NULL;
710 		headp = hcap->hca_sidr_list;
711 
712 		while (headp != NULL) {
713 			/* delete the matching entry */
714 			if (headp == ud_statep) {
715 				if (prevp) {
716 					prevp->ud_nextp = headp->ud_nextp;
717 				} else {
718 					prevp = headp->ud_nextp;
719 					hcap->hca_sidr_list = prevp;
720 				}
721 				break;
722 			}
723 			prevp = headp;
724 			headp = headp->ud_nextp;
725 		}
726 	}
727 
728 	rw_exit(&hcap->hca_sidr_list_lock);
729 
730 	/*
731 	 * While ref_cnt >  0
732 	 * - implies someone else is accessing the statep (possibly in
733 	 * a timeout function handler etc.)
734 	 * - don't delete statep unless they are done otherwise potentially
735 	 * one could access released memory and panic.
736 	 */
737 	mutex_enter(&ud_statep->ud_state_mutex);
738 	if (ud_statep->ud_ref_cnt > 0) {
739 		ud_statep->ud_delete_state_data = B_TRUE;
740 		IBTF_DPRINTF_L4(cmlog, "ibcm_delete_ud_state_data: "
741 		    "ud_statep 0x%p ud_ref_cnt = %x", ud_statep,
742 		    ud_statep->ud_ref_cnt);
743 		mutex_exit(&ud_statep->ud_state_mutex);
744 		return;
745 	}
746 	mutex_exit(&ud_statep->ud_state_mutex);
747 
748 	ibcm_dealloc_ud_state_data(ud_statep);
749 }
750 
751 /*
752  * ibcm_ud_dealloc_state_data:
753  *	Deallocates a given ud state structure
754  *
755  * Arguments are:-
756  *	ud statep	- ud statep to be deleted
757  *
758  * Return Values:	NONE
759  */
760 void
761 ibcm_dealloc_ud_state_data(ibcm_ud_state_data_t *ud_statep)
762 {
763 	timeout_id_t		timer_val;
764 
765 	IBTF_DPRINTF_L4(cmlog, "ibcm_dealloc_ud_state_data: ud_statep 0x%p",
766 	    ud_statep);
767 
768 	/* If timer is running - expire it */
769 	mutex_enter(&ud_statep->ud_state_mutex);
770 	if (ud_statep->ud_timerid) {
771 		timer_val = ud_statep->ud_timerid;
772 		ud_statep->ud_timerid = 0;
773 		mutex_exit(&ud_statep->ud_state_mutex);
774 		(void) untimeout(timer_val);
775 		IBTF_DPRINTF_L2(cmlog, "ibcm_dealloc_ud_state_data: "
776 		    "Unexpected timer id 0x%p ud_statep 0x%p", timer_val,
777 		    ud_statep);
778 	} else
779 		mutex_exit(&ud_statep->ud_state_mutex);
780 
781 	if (ud_statep->ud_stored_msg != NULL) {
782 		(void) ibcm_free_out_msg(
783 		    ud_statep->ud_stored_reply_addr.ibmf_hdl,
784 		    &ud_statep->ud_stored_msg);
785 	}
786 
787 	/* release the ref cnt on the associated ibmf qp */
788 	ASSERT(ud_statep->ud_stored_reply_addr.cm_qp_entry != NULL);
789 	ibcm_release_qp(ud_statep->ud_stored_reply_addr.cm_qp_entry);
790 
791 	/* Ensure the thread doing ref cnt decr releases the mutex */
792 	mutex_enter(&ud_statep->ud_state_mutex);
793 	mutex_exit(&ud_statep->ud_state_mutex);
794 
795 	/* now do the mutex_destroy() and cv_destroy() */
796 	mutex_destroy(&ud_statep->ud_state_mutex);
797 
798 	cv_destroy(&ud_statep->ud_block_client_cv);
799 
800 	/* free the req id on SIDR REQ sender side */
801 	if (ud_statep->ud_mode == IBCM_ACTIVE_MODE)
802 		ibcm_free_reqid(ud_statep->ud_hcap, ud_statep->ud_req_id);
803 
804 	/* Decrement the resource on hcap */
805 	ibcm_dec_hca_res_cnt(ud_statep->ud_hcap);
806 
807 	/* free the statep */
808 	kmem_free(ud_statep, sizeof (ibcm_ud_state_data_t));
809 }
810 
811 
812 /*
813  * ibcm_init_ids:
814  *	Create the vmem arenas for the various global ids
815  *
816  * Arguments are:-
817  *	NONE
818  *
819  * Return Values:	ibcm_status_t
820  */
821 
822 ibcm_status_t
823 ibcm_init_ids(void)
824 {
825 	timespec_t tv;
826 
827 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibcm_local_sid_arena))
828 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibcm_local_sid_seed))
829 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(ibcm_local_cid_seed))
830 
831 	ibcm_local_sid_arena = vmem_create("ibcm_local_sid",
832 	    (void *)IBCM_INITIAL_SID, IBCM_MAX_LOCAL_SIDS, 1, NULL, NULL, NULL,
833 	    0, VM_SLEEP | VMC_IDENTIFIER);
834 
835 	if (!ibcm_local_sid_arena)
836 		return (IBCM_FAILURE);
837 
838 	/* create a random starting value for local service ids */
839 	gethrestime(&tv);
840 	ibcm_local_sid_seed = ((uint64_t)tv.tv_sec << 20) & 0x007FFFFFFFF00000;
841 	ASSERT((ibcm_local_sid_seed & IB_SID_AGN_MASK) == 0);
842 	ibcm_local_sid_seed |= IB_SID_AGN_LOCAL;
843 	ibcm_local_cid_seed = (ib_com_id_t)tv.tv_sec;
844 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibcm_local_sid_arena))
845 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibcm_local_sid_seed))
846 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(ibcm_local_cid_seed))
847 
848 	return (IBCM_SUCCESS);
849 }
850 
851 
852 /*
853  * ibcm_init_hca_ids:
854  *	Create the vmem arenas for the various hca level ids
855  *
856  * Arguments are:-
857  *	hcap		pointer to ibcm_hca_info_t
858  *
859  * Return Values:	ibcm_status_t
860  */
861 ibcm_status_t
862 ibcm_init_hca_ids(ibcm_hca_info_t *hcap)
863 {
864 	hcap->hca_comid_arena = vmem_create("ibcm_com_ids",
865 	    (void *)IBCM_INITIAL_COMID, IBCM_MAX_COMIDS,
866 	    1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
867 
868 	if (!hcap->hca_comid_arena)
869 		return (IBCM_FAILURE);
870 
871 	hcap->hca_reqid_arena = vmem_create("ibcm_req_ids",
872 	    (void *)IBCM_INITIAL_REQID, IBCM_MAX_REQIDS,
873 	    1, NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
874 
875 	if (!hcap->hca_reqid_arena) {
876 		vmem_destroy(hcap->hca_comid_arena);
877 		return (IBCM_FAILURE);
878 	}
879 
880 	return (IBCM_SUCCESS);
881 }
882 
883 /*
884  * ibcm_free_ids:
885  *	Destroy the vmem arenas for the various ids
886  *
887  * Arguments are:-
888  *	NONE
889  *
890  * Return Values:	NONE
891  */
892 void
893 ibcm_fini_ids(void)
894 {
895 	/* All arenas shall be valid */
896 	vmem_destroy(ibcm_local_sid_arena);
897 }
898 
899 /*
900  * ibcm_free_hca_ids:
901  *	Destroy the vmem arenas for the various ids
902  *
903  * Arguments are:-
904  *	hcap		pointer to ibcm_hca_info_t
905  *
906  * Return Values:	NONE
907  */
908 void
909 ibcm_fini_hca_ids(ibcm_hca_info_t *hcap)
910 {
911 	/* All arenas shall be valid */
912 	vmem_destroy(hcap->hca_comid_arena);
913 	vmem_destroy(hcap->hca_reqid_arena);
914 }
915 
916 /* Communication id management routines ie., allocate, free up comids */
917 
918 /*
919  * ibcm_alloc_comid:
920  *	Allocate a new communication id
921  *
922  * Arguments are:-
923  *	hcap	:	pointer to ibcm_hca_info_t
924  *	comid:		pointer to the newly allocated communication id
925  *
926  * Return Values:	ibt_status_t
927  */
928 ibcm_status_t
929 ibcm_alloc_comid(ibcm_hca_info_t *hcap, ib_com_id_t *comidp)
930 {
931 	ib_com_id_t comid;
932 
933 	/* Use next fit, so least recently used com id is allocated */
934 	comid = (ib_com_id_t)(uintptr_t)vmem_alloc(hcap->hca_comid_arena, 1,
935 	    VM_SLEEP | VM_NEXTFIT);
936 
937 	IBTF_DPRINTF_L4(cmlog, "ibcm_alloc_comid: hcap 0x%p comid 0x%lX", hcap,
938 	    comid);
939 
940 	/*
941 	 * As comid is 32 bits, and maximum connections possible are 2^24
942 	 * per hca, comid allocation would never fail
943 	 */
944 	*comidp = comid + ibcm_local_cid_seed;
945 	if (comid == 0) {
946 		IBTF_DPRINTF_L2(cmlog, "ibcm_alloc_comid: hcap 0x%p"
947 		    "no more comids available", hcap);
948 		return (IBCM_FAILURE);
949 	}
950 
951 	return (IBCM_SUCCESS);
952 }
953 
954 /*
955  * ibcm_free_comid:
956  *	Releases the given Communication Id
957  *
958  * Arguments are:
959  *	hcap	:	pointer to ibcm_hca_info_t
960  *	comid	:	Communication id to be free'd
961  *
962  * Return Values:	NONE
963  */
964 void
965 ibcm_free_comid(ibcm_hca_info_t *hcap, ib_com_id_t comid)
966 {
967 	IBTF_DPRINTF_L4(cmlog, "ibcm_free_comid: hcap 0x%p"
968 	    "comid %x", hcap, comid);
969 	comid -= ibcm_local_cid_seed;
970 	vmem_free(hcap->hca_comid_arena, (void *)(uintptr_t)comid, 1);
971 }
972 
973 /* Allocate and Free local service ids */
974 
975 /*
976  * ibcm_alloc_local_sids:
977  *	Create and destroy the vmem arenas for the service ids
978  *
979  * Arguments are:-
980  *	Number of contiguous SIDs needed
981  *
982  * Return Values:	starting SID
983  */
984 ib_svc_id_t
985 ibcm_alloc_local_sids(int num_sids)
986 {
987 	ib_svc_id_t sid;
988 
989 	sid = (ib_svc_id_t)(uintptr_t)vmem_alloc(ibcm_local_sid_arena,
990 	    num_sids, VM_SLEEP | VM_NEXTFIT);
991 
992 	IBTF_DPRINTF_L4(cmlog, "ibcm_alloc_local_sids: ServiceID 0x%llX "
993 	    "num_sids %d", sid, num_sids);
994 	if (sid == 0) {
995 		IBTF_DPRINTF_L2(cmlog, "ibcm_alloc_local_sids: "
996 		    "no more local sids available");
997 	} else {
998 		ASSERT((ibcm_local_sid_seed & IB_SID_AGN_MASK) ==
999 		    IB_SID_AGN_LOCAL);
1000 		sid += ibcm_local_sid_seed;
1001 		IBTF_DPRINTF_L4(cmlog, "ibcm_alloc_local_sids: Success: "
1002 		    "allocated 0x%llX:%d", sid, num_sids);
1003 	}
1004 	return (sid);
1005 }
1006 
1007 /*
1008  * ibcm_free_local_sids:
1009  *	Releases the given Local service id
1010  *
1011  * Arguments are:
1012  *	num_sids:	Number of local service id's to be free'd
1013  *	service_id:	Starting local service id that needs to be free'd
1014  *
1015  * Return Values:	NONE
1016  */
1017 void
1018 ibcm_free_local_sids(ib_svc_id_t service_id, int num_sids)
1019 {
1020 	service_id -= ibcm_local_sid_seed;
1021 	IBTF_DPRINTF_L4(cmlog, "ibcm_free_local_sids: "
1022 	    "service_id 0x%llX num_sids %d", service_id, num_sids);
1023 	vmem_free(ibcm_local_sid_arena,
1024 	    (void *)(uintptr_t)service_id, num_sids);
1025 }
1026 
1027 /* Allocate and free request id routines for SIDR */
1028 
1029 /*
1030  * ibcm_alloc_reqid:
1031  *	Allocate a new SIDR REQ request id
1032  *
1033  * Arguments are:-
1034  *	hcap	:	pointer to ibcm_hca_info_t
1035  *	*reqid	:	pointer to the new request id returned
1036  *
1037  * Return Values:	ibcm_status_t
1038  */
1039 ibcm_status_t
1040 ibcm_alloc_reqid(ibcm_hca_info_t *hcap, uint32_t *reqid)
1041 {
1042 	/* Use next fit, so least recently used com id is allocated */
1043 	*reqid = (uint32_t)(uintptr_t)vmem_alloc(hcap->hca_reqid_arena, 1,
1044 	    VM_SLEEP | VM_NEXTFIT);
1045 
1046 	IBTF_DPRINTF_L4(cmlog, "ibcm_alloc_reqid: hcap 0x%p reqid %x", hcap,
1047 	    *reqid);
1048 	if (!(*reqid)) {
1049 		IBTF_DPRINTF_L2(cmlog, "ibcm_alloc_reqid: "
1050 		    "no more req ids available");
1051 		return (IBCM_FAILURE);
1052 	}
1053 	return (IBCM_SUCCESS);
1054 }
1055 
1056 /*
1057  * ibcm_free_reqid:
1058  *	Releases the given SIDR REQ request id
1059  *
1060  * Arguments are:
1061  *	hcap	:	pointer to ibcm_hca_info_t
1062  *	reqid	:	Request id to be free'd
1063  *
1064  * Return Values:	NONE
1065  */
1066 void
1067 ibcm_free_reqid(ibcm_hca_info_t *hcap, uint32_t reqid)
1068 {
1069 	IBTF_DPRINTF_L4(cmlog, "ibcm_free_reqid: hcap 0x%p reqid %x", hcap,
1070 	    reqid);
1071 	vmem_free(hcap->hca_reqid_arena, (void *)(uintptr_t)reqid, 1);
1072 }
1073 
1074 /*
1075  * ibcm_generate_tranid:
1076  *	Generate a new transaction id based on args
1077  *
1078  * Arguments are:-
1079  *	event_type	CM Message REQ/DREQ/LAP
1080  *	id		32 bit identifier
1081  *	cm_tran_priv	CM private data to be filled in top 28 MSB bits of
1082  *			tran id
1083  *
1084  *
1085  * Return Value:	uint64_t
1086  */
1087 uint64_t
1088 ibcm_generate_tranid(uint8_t event, uint32_t id, uint32_t cm_tran_priv)
1089 {
1090 	/*
1091 	 * copy comid to bits 31-0 of tran id,
1092 	 * attr id to bits 35-32 of tran id,
1093 	 * cm_priv to bits 63-36 of tran id
1094 	 */
1095 	if (cm_tran_priv == 0)
1096 		/*
1097 		 * The below ensures that no duplicate transaction id is
1098 		 * generated atleast for next 6 months. Calculations:
1099 		 * (2^28)/(1000 * 60 * 24 * 30) = 6 approx
1100 		 */
1101 		cm_tran_priv = gethrtime() >> 20;	/* ~time in ms */
1102 
1103 	return ((((uint64_t)cm_tran_priv << 36) | (uint64_t)event << 32) | id);
1104 }
1105 
1106 #ifdef DEBUG
1107 
1108 /*
1109  * ibcm_decode_tranid:
1110  *	Decodes a given transaction id, assuming certain format.
1111  *
1112  * Arguments are:-
1113  *	tran_id		Transaction id to be decoded
1114  *	cm_tran_priv	CM private data retrieved from transaction id
1115  *
1116  * Return Value:	None
1117  */
1118 void
1119 ibcm_decode_tranid(uint64_t tran_id, uint32_t *cm_tran_priv)
1120 {
1121 	ib_com_id_t		id;
1122 	ibcm_event_type_t	event;
1123 
1124 	id = tran_id & 0xFFFFFFFF;
1125 	event = (tran_id >> 32) & 0xF;
1126 
1127 	IBTF_DPRINTF_L5(cmlog, "ibcm_decode_tranid: id = 0x%x, event = %x",
1128 	    id, event);
1129 
1130 	if (cm_tran_priv) {
1131 		*cm_tran_priv = tran_id >> 36;
1132 		IBTF_DPRINTF_L5(cmlog, "ibcm_decode_tranid: "
1133 		    "cm_tran_priv = %x", *cm_tran_priv);
1134 	}
1135 }
1136 
1137 #endif
1138 
1139 /*
1140  * Service ID entry create and lookup functions
1141  */
1142 
1143 /*
1144  * ibcm_svc_compare:
1145  * 	- AVL svc tree node compare
1146  *
1147  * Arguments:
1148  *	p1	: pointer to local comid
1149  *	p2	: pointer to passed ibcm_state_data_t
1150  *
1151  * Return values:
1152  *	0	: match found
1153  *	-1	: no match but insert to left side of the tree
1154  *	+1	: no match but insert to right side of the tree
1155  */
1156 int
1157 ibcm_svc_compare(const void *p1, const void *p2)
1158 {
1159 	ibcm_svc_lookup_t	*sidp = (ibcm_svc_lookup_t *)p1;
1160 	ibcm_svc_info_t		*svcp = (ibcm_svc_info_t *)p2;
1161 	ib_svc_id_t		start_sid = sidp->sid;
1162 	ib_svc_id_t		end_sid = start_sid + sidp->num_sids - 1;
1163 
1164 	IBTF_DPRINTF_L5(cmlog, "ibcm_svc_compare: "
1165 	    "sid: 0x%llx, numsids: %d, node_sid: 0x%llx node_num_sids: %d",
1166 	    sidp->sid, sidp->num_sids, svcp->svc_id, svcp->svc_num_sids);
1167 
1168 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
1169 
1170 	if (svcp->svc_id > end_sid)
1171 		return (-1);
1172 	if (svcp->svc_id + svcp->svc_num_sids - 1 < start_sid)
1173 		return (+1);
1174 	return (0);	/* means there is some overlap of SIDs */
1175 }
1176 
1177 
1178 /*
1179  * ibcm_create_svc_entry:
1180  *	Make sure no conflicting entry exists, then allocate it.
1181  *	Fill in the critical "look up" details that are provided
1182  *	in the arguments before dropping the lock.
1183  *
1184  * Return values:
1185  *	Pointer to ibcm_svc_info_t, if created, otherwise NULL.
1186  */
1187 ibcm_svc_info_t *
1188 ibcm_create_svc_entry(ib_svc_id_t sid, int num_sids)
1189 {
1190 	ibcm_svc_info_t	*svcp;
1191 	ibcm_svc_info_t	*svcinfop;
1192 	ibcm_svc_lookup_t svc;
1193 	avl_index_t where = 0;
1194 
1195 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*svcinfop))
1196 
1197 	/* assume success, and avoid kmem while holding the writer lock */
1198 	svcinfop = kmem_zalloc(sizeof (*svcinfop), KM_SLEEP);
1199 	svcinfop->svc_id = sid;
1200 	svcinfop->svc_num_sids = num_sids;
1201 
1202 	svc.sid = sid;
1203 	svc.num_sids = num_sids;
1204 
1205 	mutex_enter(&ibcm_svc_info_lock);
1206 #ifdef __lock_lint
1207 	ibcm_svc_compare(NULL, NULL);
1208 #endif
1209 	svcp = avl_find(&ibcm_svc_avl_tree, &svc, &where);
1210 	if (svcp != NULL) {	/* overlab exists */
1211 		mutex_exit(&ibcm_svc_info_lock);
1212 		kmem_free(svcinfop, sizeof (*svcinfop));
1213 		return (NULL);
1214 	}
1215 	avl_insert(&ibcm_svc_avl_tree, (void *)svcinfop, where);
1216 	mutex_exit(&ibcm_svc_info_lock);
1217 
1218 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*svcinfop))
1219 
1220 	return (svcinfop);
1221 }
1222 
1223 /*
1224  * ibcm_find_svc_entry:
1225  *	Finds a ibcm_svc_info_t entry into the CM's global table.
1226  *	The search done here assumes the list is sorted by SID.
1227  *
1228  * Arguments are:
1229  *	sid		- Service ID to look up
1230  *
1231  * Return values:
1232  *	Pointer to ibcm_svc_info_t, if found, otherwise NULL.
1233  */
1234 ibcm_svc_info_t *
1235 ibcm_find_svc_entry(ib_svc_id_t sid)
1236 {
1237 	ibcm_svc_info_t	*svcp;
1238 	ibcm_svc_lookup_t svc;
1239 
1240 	IBTF_DPRINTF_L3(cmlog, "ibcm_find_svc_entry: finding SID 0x%llX", sid);
1241 
1242 	ASSERT(MUTEX_HELD(&ibcm_svc_info_lock));
1243 
1244 	svc.sid = sid;
1245 	svc.num_sids = 1;
1246 #ifdef __lock_lint
1247 	ibcm_svc_compare(NULL, NULL);
1248 #endif
1249 	svcp = avl_find(&ibcm_svc_avl_tree, &svc, NULL);
1250 	if (svcp != NULL) {
1251 		IBTF_DPRINTF_L3(cmlog, "ibcm_find_svc_entry: "
1252 		    "found SID = 0x%llX", sid);
1253 		return (svcp);	/* found it */
1254 	}
1255 	IBTF_DPRINTF_L3(cmlog, "ibcm_find_svc_entry: SID %llX not found", sid);
1256 	return (NULL);
1257 }
1258 
1259 /*
1260  * ibcm_alloc_ibmf_msg:
1261  * Allocate an ibmf message structure and the additional memory required for
1262  * sending an outgoing CM mad.  The ibmf message structure contains two
1263  * ibmf_msg_bufs_t fields, one for the incoming MAD and one for the outgoing
1264  * MAD.  The CM must allocate the memory for the outgoing MAD.  The msg_buf
1265  * field has three buffers: the mad header, the class header, and the class
1266  * data.  To simplify the code and reduce the number of kmem_zalloc() calls,
1267  * ibcm_alloc_ibmf_msg will allocate one buffer and set the pointers to the
1268  * right offsets.  No class header is needed so only the mad header and class
1269  * data fields are used.
1270  */
1271 ibt_status_t
1272 ibcm_alloc_out_msg(ibmf_handle_t ibmf_handle, ibmf_msg_t **ibmf_msgpp,
1273     uint8_t method)
1274 {
1275 	ib_mad_hdr_t	*output_mad_hdr;
1276 	int		sa_retval;
1277 
1278 	if ((sa_retval =
1279 	    ibmf_alloc_msg(ibmf_handle, IBMF_ALLOC_SLEEP, ibmf_msgpp)) !=
1280 	    IBMF_SUCCESS) {
1281 		IBTF_DPRINTF_L1(cmlog, "ibcm_alloc_out_msg: "
1282 		    "ibmf_alloc_msg failed with IBMF_ALLOC_SLEEP");
1283 		return (ibcm_ibmf_analyze_error(sa_retval));
1284 	}
1285 
1286 	(*ibmf_msgpp)->im_msgbufs_send.im_bufs_mad_hdr = kmem_zalloc(
1287 	    IBCM_MAD_SIZE, KM_SLEEP);
1288 
1289 	(*ibmf_msgpp)->im_msgbufs_send.im_bufs_cl_data_len = IBCM_MSG_SIZE;
1290 	(*ibmf_msgpp)->im_msgbufs_send.im_bufs_cl_data =
1291 	    (uchar_t *)((*ibmf_msgpp)->im_msgbufs_send.im_bufs_mad_hdr) +
1292 	    IBCM_MAD_HDR_SIZE;
1293 
1294 	/* initialize generic CM MAD header fields */
1295 	output_mad_hdr = IBCM_OUT_HDRP((*ibmf_msgpp));
1296 	output_mad_hdr->BaseVersion = IBCM_MAD_BASE_VERSION;
1297 	output_mad_hdr->MgmtClass = MAD_MGMT_CLASS_COMM_MGT;
1298 	output_mad_hdr->ClassVersion = IBCM_MAD_CLASS_VERSION;
1299 	output_mad_hdr->R_Method = method;
1300 
1301 	return (IBT_SUCCESS);
1302 }
1303 
1304 /*
1305  * ibcm_free_ibmf_msg:
1306  * Frees the buffer and ibmf message associated with an outgoing CM message.
1307  * This function should only be used to free messages created by
1308  * ibcm_alloc_out_msg.  Will return IBCM_FAILURE if the ibmf_free_msg() call
1309  * fails and IBCM_SUCCESS otherwise.
1310  */
1311 ibcm_status_t
1312 ibcm_free_out_msg(ibmf_handle_t ibmf_handle, ibmf_msg_t **ibmf_msgpp)
1313 {
1314 	int ibmf_status;
1315 
1316 	kmem_free((*ibmf_msgpp)->im_msgbufs_send.im_bufs_mad_hdr,
1317 	    IBCM_MAD_SIZE);
1318 
1319 	if ((ibmf_status = ibmf_free_msg(ibmf_handle, ibmf_msgpp)) !=
1320 	    IBMF_SUCCESS) {
1321 		IBTF_DPRINTF_L2(cmlog, "ibcm_free_out_msg: "
1322 		    "ibmf_free_msg failed %d", ibmf_status);
1323 		return (IBCM_FAILURE);
1324 	} else
1325 		return (IBCM_SUCCESS);
1326 }
1327 
1328 ibcm_qp_list_t *
1329 ibcm_find_qp(ibcm_hca_info_t *hcap, int port_no, ib_pkey_t pkey)
1330 {
1331 	ibcm_qp_list_t		*entry;
1332 	ibmf_qp_handle_t	ibmf_qp;
1333 	int			ibmf_status;
1334 
1335 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*entry))
1336 
1337 	mutex_enter(&ibcm_qp_list_lock);
1338 
1339 	/*
1340 	 * CM currently does not track port up and down status. If tracking of
1341 	 * " port status" is added in the future, then CM could be optimized to
1342 	 * re-use other ports on hcap, if the port associated with the above
1343 	 * port_no is down. But, the issue of "reachability" needs to be
1344 	 * handled, before selecting an alternative port different from above.
1345 	 */
1346 	entry = hcap->hca_port_info[port_no-1].port_qplist;
1347 	while (entry != NULL) {
1348 		if (entry->qp_pkey == pkey) {
1349 			++entry->qp_ref_cnt;
1350 			mutex_exit(&ibcm_qp_list_lock);
1351 			return (entry);
1352 		}
1353 		entry = entry->qp_next;
1354 	}
1355 
1356 	/*
1357 	 * entry not found, attempt to alloc a qp
1358 	 * This may be optimized in the future, to allocate ibmf qp's
1359 	 * once the "CM mgmt pkeys" are precisely known.
1360 	 */
1361 	ibmf_status = ibmf_alloc_qp(
1362 	    hcap->hca_port_info[port_no-1].port_ibmf_hdl, pkey, IB_GSI_QKEY,
1363 	    IBMF_ALT_QP_MAD_NO_RMPP, &ibmf_qp);
1364 
1365 	if (ibmf_status != IBMF_SUCCESS) {
1366 		mutex_exit(&ibcm_qp_list_lock);
1367 		IBTF_DPRINTF_L2(cmlog, "ibcm_find_qp: failed to alloc IBMF QP"
1368 		    "for Pkey = %x port_no = %x status = %d hcaguid = %llXp",
1369 		    pkey, port_no, ibmf_status, hcap->hca_guid);
1370 		/*
1371 		 * This may be optimized in the future, so as CM would attempt
1372 		 * to re-use other QP's whose ref cnt is 0 in the respective
1373 		 * port_qplist, by doing an ibmf_modify_qp with pkey above.
1374 		 */
1375 		return (NULL);
1376 	}
1377 
1378 	entry = kmem_alloc(sizeof (ibcm_qp_list_t), KM_SLEEP);
1379 	entry->qp_next = hcap->hca_port_info[port_no-1].port_qplist;
1380 	hcap->hca_port_info[port_no-1].port_qplist = entry;
1381 	entry->qp_cm = ibmf_qp;
1382 	entry->qp_ref_cnt = 1;
1383 	entry->qp_pkey = pkey;
1384 	entry->qp_port = &(hcap->hca_port_info[port_no-1]);
1385 
1386 	mutex_exit(&ibcm_qp_list_lock);
1387 
1388 	/* set-up the handler */
1389 	ibmf_status = ibmf_setup_async_cb(
1390 	    hcap->hca_port_info[port_no-1].port_ibmf_hdl, ibmf_qp,
1391 	    ibcm_recv_cb, entry, 0);
1392 
1393 	ASSERT(ibmf_status == IBMF_SUCCESS);
1394 
1395 #ifdef	DEBUG
1396 	ibcm_query_qp(hcap->hca_port_info[port_no-1].port_ibmf_hdl, ibmf_qp);
1397 #endif
1398 
1399 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*entry))
1400 
1401 	return (entry);
1402 }
1403 
1404 void
1405 ibcm_release_qp(ibcm_qp_list_t *cm_qp_entry)
1406 {
1407 	mutex_enter(&ibcm_qp_list_lock);
1408 	--cm_qp_entry->qp_ref_cnt;
1409 	ASSERT(cm_qp_entry->qp_ref_cnt >= 0);
1410 	mutex_exit(&ibcm_qp_list_lock);
1411 }
1412 
1413 
1414 /* called holding the ibcm_qp_list_lock mutex */
1415 ibcm_status_t
1416 ibcm_free_qp(ibcm_qp_list_t *cm_qp_entry)
1417 {
1418 	int	ibmf_status;
1419 
1420 	IBTF_DPRINTF_L5(cmlog, "ibcm_free_qp: qp_hdl %p ref_cnt %d pkey %x",
1421 	    cm_qp_entry->qp_cm, cm_qp_entry->qp_ref_cnt, cm_qp_entry->qp_pkey);
1422 
1423 	/* check, there are no users of this ibmf qp */
1424 	if (cm_qp_entry->qp_ref_cnt != 0)
1425 		return (IBCM_FAILURE);
1426 
1427 	/* Tear down the receive callback */
1428 	ibmf_status = ibmf_tear_down_async_cb(
1429 	    cm_qp_entry->qp_port->port_ibmf_hdl, cm_qp_entry->qp_cm, 0);
1430 	if (ibmf_status != IBMF_SUCCESS) {
1431 		IBTF_DPRINTF_L2(cmlog, "ibcm_free_qp: "
1432 		    "ibmf_tear_down_async_cb failed %d port_num %d",
1433 		    ibmf_status, cm_qp_entry->qp_port->port_num);
1434 		return (IBCM_FAILURE);
1435 	}
1436 
1437 	ibmf_status = ibmf_free_qp(cm_qp_entry->qp_port->port_ibmf_hdl,
1438 	    &cm_qp_entry->qp_cm, 0);
1439 	if (ibmf_status != IBMF_SUCCESS) {
1440 		IBTF_DPRINTF_L2(cmlog, "ibcm_free_qp: ibmf_free_qp failed for"
1441 		    " ibmf_status %d qp hdl %p port_no %x", ibmf_status,
1442 		    cm_qp_entry->qp_cm, cm_qp_entry->qp_port->port_num);
1443 		return (IBCM_FAILURE);
1444 	}
1445 
1446 	return (IBCM_SUCCESS);
1447 }
1448 
1449 ibcm_status_t
1450 ibcm_free_allqps(ibcm_hca_info_t *hcap, int port_no)
1451 {
1452 	ibcm_qp_list_t		*entry, *freed;
1453 	ibcm_status_t		ibcm_status = IBCM_SUCCESS;
1454 
1455 	IBTF_DPRINTF_L5(cmlog, "ibcm_free_allqps: hcap %p port_no %d", hcap,
1456 	    port_no);
1457 
1458 	mutex_enter(&ibcm_qp_list_lock);
1459 	entry = hcap->hca_port_info[port_no-1].port_qplist;
1460 	while ((entry != NULL) &&
1461 	    ((ibcm_status = ibcm_free_qp(entry)) == IBCM_SUCCESS)) {
1462 		freed = entry;
1463 		entry = entry->qp_next;
1464 		kmem_free(freed, sizeof (ibcm_qp_list_t));
1465 	}
1466 
1467 	if (ibcm_status != IBCM_SUCCESS)	/* sanity the linked list */
1468 		hcap->hca_port_info[port_no-1].port_qplist = entry;
1469 	else	/* all ibmf qp's of port must have been free'd successfully */
1470 		hcap->hca_port_info[port_no-1].port_qplist = NULL;
1471 
1472 	mutex_exit(&ibcm_qp_list_lock);
1473 	return (ibcm_status);
1474 }
1475 
1476 /*
1477  * ibt_bind_service() and ibt_get_paths() needs the following helper function
1478  * to handle endianess in case of Service Data.
1479  */
1480 void
1481 ibcm_swizzle_from_srv(ibt_srv_data_t *sb_data, uint8_t *service_bytes)
1482 {
1483 	uint8_t		*p8 = service_bytes;
1484 	uint16_t	*p16;
1485 	uint32_t	*p32;
1486 	uint64_t	*p64;
1487 	int		i;
1488 
1489 	for (i = 0; i < 16; i++)
1490 		*p8++ = sb_data->s_data8[i];
1491 
1492 	p16 = (uint16_t *)p8;
1493 	for (i = 0; i < 8; i++)
1494 		*p16++ = h2b16(sb_data->s_data16[i]);
1495 
1496 	p32 = (uint32_t *)p16;
1497 	for (i = 0; i < 4; i++)
1498 		*p32++ = h2b32(sb_data->s_data32[i]);
1499 
1500 	p64 = (uint64_t *)p32;
1501 	for (i = 0; i < 2; i++)
1502 		*p64++ = h2b64(sb_data->s_data64[i]);
1503 }
1504 
1505 void
1506 ibcm_swizzle_to_srv(uint8_t *service_bytes, ibt_srv_data_t *sb_data)
1507 {
1508 	uint8_t		*p8 = service_bytes;
1509 	uint16_t	*p16;
1510 	uint32_t	*p32;
1511 	uint64_t	*p64;
1512 	int		i;
1513 
1514 	for (i = 0; i < 16; i++)
1515 		sb_data->s_data8[i] = *p8++;
1516 
1517 	p16 = (uint16_t *)p8;
1518 	for (i = 0; i < 8; i++)
1519 		sb_data->s_data16[i] = h2b16(*p16++);
1520 
1521 	p32 = (uint32_t *)p16;
1522 	for (i = 0; i < 4; i++)
1523 		sb_data->s_data32[i] = h2b32(*p32++);
1524 	p64 = (uint64_t *)p32;
1525 
1526 	for (i = 0; i < 2; i++)
1527 		sb_data->s_data64[i] = h2b64(*p64++);
1528 }
1529 
1530 /* Trace related functions */
1531 
1532 void
1533 ibcm_init_conn_trace(ibcm_state_data_t *sp)
1534 {
1535 	IBTF_DPRINTF_L5(cmlog, "ibcm_init_conn_trace: statep %p", sp);
1536 
1537 	/* Initialize trace related fields */
1538 
1539 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sp->conn_trace))
1540 	sp->conn_trace = kmem_zalloc(sizeof (ibcm_conn_trace_t), KM_SLEEP);
1541 	if ((ibcm_enable_trace & 1) == 0)
1542 		sp->conn_trace->conn_base_tm = gethrtime();
1543 	sp->conn_trace->conn_allocated_trcnt = ibcm_conn_max_trcnt;
1544 	sp->conn_trace->conn_trace_events =
1545 	    kmem_zalloc(sp->conn_trace->conn_allocated_trcnt, KM_SLEEP);
1546 	sp->conn_trace->conn_trace_event_times =
1547 	    kmem_zalloc(sp->conn_trace->conn_allocated_trcnt *
1548 	    sizeof (tm_diff_type), KM_SLEEP);
1549 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sp->conn_trace))
1550 }
1551 
1552 void
1553 ibcm_fini_conn_trace(ibcm_state_data_t *statep)
1554 {
1555 	IBTF_DPRINTF_L5(cmlog, "ibcm_fini_conn_trace: statep %p tracep %p",
1556 	    statep, statep->conn_trace);
1557 
1558 	/* free the trace data */
1559 	if (statep->conn_trace) {
1560 		if (statep->conn_trace->conn_trace_events)
1561 			kmem_free(statep->conn_trace->conn_trace_events,
1562 			    statep->conn_trace->conn_allocated_trcnt);
1563 		if (statep->conn_trace->conn_trace_event_times)
1564 			kmem_free(statep->conn_trace->conn_trace_event_times,
1565 			    statep->conn_trace->conn_allocated_trcnt *
1566 			    sizeof (tm_diff_type));
1567 
1568 		kmem_free(statep->conn_trace, sizeof (ibcm_conn_trace_t));
1569 	}
1570 }
1571 
1572 /* mostly used to profile connection establishment times with dtrace */
1573 void
1574 ibcm_established(hrtime_t time_diff)
1575 {
1576 	if (time_diff > 1000000000LL)	/* 1 second */
1577 		IBTF_DPRINTF_L2(cmlog, "slow connection time (%d seconds)",
1578 		    (uint_t)(time_diff >> 30));
1579 }
1580 
1581 void
1582 ibcm_insert_trace(void *statep, ibcm_state_rc_trace_qualifier_t event_qualifier)
1583 {
1584 	ibcm_conn_trace_t	*conn_trace;
1585 	uint8_t			conn_trace_ind;
1586 	hrtime_t		time_diff;
1587 	hrtime_t		hrt;
1588 
1589 	if (!(((ibcm_state_data_t *)statep)->conn_trace))
1590 		return;
1591 
1592 	conn_trace = ((ibcm_state_data_t *)statep)->conn_trace;
1593 
1594 	if (!conn_trace->conn_trace_events)
1595 		return;
1596 
1597 	IBTF_DPRINTF_L5(cmlog, "ibcm_insert_trace: statep %p event %d",
1598 	    statep, event_qualifier);
1599 
1600 	mutex_enter(&ibcm_trace_mutex);
1601 
1602 	/* No more trace memory available, hence return */
1603 	if (conn_trace->conn_trace_ind == conn_trace->conn_allocated_trcnt) {
1604 		mutex_exit(&ibcm_trace_mutex);
1605 		return;
1606 	} else
1607 		++conn_trace->conn_trace_ind;
1608 
1609 	conn_trace_ind = conn_trace->conn_trace_ind - 1;
1610 
1611 	conn_trace->conn_trace_events[conn_trace_ind] = event_qualifier;
1612 
1613 	if ((ibcm_enable_trace & 1) == 0) {
1614 		hrt = gethrtime();
1615 		time_diff = hrt - conn_trace->conn_base_tm;
1616 		if (event_qualifier == IBCM_TRACE_CALLED_CONN_EST_EVENT)
1617 			ibcm_established(time_diff);
1618 		time_diff >>= 10;
1619 		if (time_diff >= TM_DIFF_MAX) {
1620 			/* RESET, future times are relative to new base time. */
1621 			conn_trace->conn_base_tm = hrt;
1622 			time_diff = 0;
1623 		}
1624 		conn_trace->conn_trace_event_times[conn_trace_ind] = time_diff;
1625 	}
1626 
1627 	mutex_exit(&ibcm_trace_mutex);
1628 
1629 	IBTF_DPRINTF_L5(cmlog, "ibcm_insert_trace: statep %p inserted event %d",
1630 	    statep, event_qualifier);
1631 }
1632 
1633 void
1634 ibcm_dump_conn_trace(void *statep)
1635 {
1636 	IBTF_DPRINTF_L5(cmlog, "ibcm_dump_conn_trace: statep %p",
1637 	    statep);
1638 
1639 	mutex_enter(&ibcm_trace_print_mutex);
1640 	ibcm_debug_buf[0] = '\0';
1641 	ibcm_dump_conn_trbuf(statep, "ibcm: ", ibcm_debug_buf,
1642 	    IBCM_DEBUG_BUF_SIZE);
1643 	if (ibcm_debug_buf[0] != '\0')
1644 		IBTF_DPRINTF_L2(cmlog, "\n%s", ibcm_debug_buf);
1645 
1646 #ifdef	DEBUG
1647 
1648 	if (ibcm_test_mode > 1)
1649 		cmn_err(CE_CONT, "IBCM DEBUG TRACE:\n%s", ibcm_debug_buf);
1650 #endif
1651 
1652 	mutex_exit(&ibcm_trace_print_mutex);
1653 }
1654 
1655 void
1656 ibcm_dump_conn_trbuf(void *statep, char *line_prefix, char *buf, int buf_size)
1657 {
1658 	ibcm_conn_trace_t	*conn_trace;
1659 	int			tr_ind;
1660 	ibcm_state_data_t	*sp;
1661 	int	cur_size = 0;	/* size of item copied */
1662 	int	rem_size;	/* remaining size in trace buffer */
1663 	int	next_data = 0;	/* location where next item copied */
1664 
1665 	if ((buf == NULL) || (buf_size <= 0))
1666 		return;
1667 
1668 	sp = (ibcm_state_data_t *)statep;
1669 
1670 	if (!sp->conn_trace)
1671 		return;
1672 
1673 	conn_trace = sp->conn_trace;
1674 
1675 	if (!conn_trace->conn_trace_events)
1676 		return;
1677 
1678 	rem_size = buf_size;
1679 
1680 	/* Print connection level global data */
1681 
1682 	/* Print statep, local comid, local qpn */
1683 	cur_size = snprintf(&buf[next_data], rem_size, "%s%s0x%p\n%s%s0x%p\n"
1684 	    "%s%s0x%x/%llx/%d\n%s%s0x%x\n%s%s0x%x/%llx\n%s%s0x%x\n%s%s%llu\n",
1685 	    line_prefix, event_str[IBCM_DISPLAY_SID], (void *)sp,
1686 	    line_prefix, event_str[IBCM_DISPLAY_CHAN], (void *)sp->channel,
1687 	    line_prefix, event_str[IBCM_DISPLAY_LCID], sp->local_comid,
1688 	    (longlong_t)sp->local_hca_guid, sp->prim_port,
1689 	    line_prefix, event_str[IBCM_DISPLAY_LQPN], sp->local_qpn,
1690 	    line_prefix, event_str[IBCM_DISPLAY_RCID], sp->remote_comid,
1691 	    (longlong_t)sp->remote_hca_guid,
1692 	    line_prefix, event_str[IBCM_DISPLAY_RQPN], sp->remote_qpn,
1693 	    line_prefix, event_str[IBCM_DISPLAY_TM], conn_trace->conn_base_tm);
1694 
1695 	rem_size = rem_size - cur_size;
1696 	if (rem_size <= 0) {
1697 		buf[buf_size-1] = '\n';
1698 		return;
1699 	}
1700 
1701 	next_data = next_data + cur_size;
1702 
1703 	for (tr_ind = 0; tr_ind < conn_trace->conn_trace_ind; tr_ind++) {
1704 		cur_size = snprintf(&buf[next_data], rem_size,
1705 		    "%s%sTM_DIFF %u\n", line_prefix,
1706 		    event_str[conn_trace->conn_trace_events[tr_ind]],
1707 		    conn_trace->conn_trace_event_times[tr_ind]);
1708 		rem_size = rem_size - cur_size;
1709 		if (rem_size <= 0) {
1710 			buf[buf_size-1] = '\n';
1711 			return;
1712 		}
1713 		next_data = next_data + cur_size;
1714 	}
1715 
1716 	buf[next_data] = '\0';
1717 	IBTF_DPRINTF_L5(cmlog, "ibcm_dump_conn_trbuf: statep %p "
1718 	    "debug buf size %d bytes", statep, next_data);
1719 }
1720 
1721 
1722 #ifdef	DEBUG
1723 
1724 void
1725 ibcm_query_qp(ibmf_handle_t ibmf_hdl, ibmf_qp_handle_t ibmf_qp)
1726 {
1727 	uint8_t		qp_port_num;
1728 	ib_qpn_t	qp_num;
1729 	ib_pkey_t	qp_pkey;
1730 	ib_qkey_t	qp_qkey;
1731 	int		ibmf_status;
1732 
1733 	if (ibmf_qp == IBMF_QP_HANDLE_DEFAULT) {
1734 		IBTF_DPRINTF_L4(cmlog, "ibcm_query_qp: QP1");
1735 		return;
1736 	}
1737 
1738 	ibmf_status =
1739 	    ibmf_query_qp(ibmf_hdl, ibmf_qp, &qp_num, &qp_pkey, &qp_qkey,
1740 	    &qp_port_num, 0);
1741 
1742 	ASSERT(ibmf_status == IBMF_SUCCESS);
1743 
1744 	IBTF_DPRINTF_L5(cmlog, "ibcm_query_qp: qpn %x qkey %x pkey %x port %d",
1745 	    qp_num, qp_qkey, qp_pkey, qp_port_num);
1746 }
1747 
1748 /*
1749  * ibcm_dump_raw_message:
1750  *	dumps 256 bytes of data of a raw message (REP/REQ/DREQ ...)
1751  *	(can be called from the kernel debugger w/ the message pointer)
1752  *
1753  * Arguments:
1754  *	msgp	- the messages that needs to be dumped
1755  *
1756  * Return values: NONE
1757  */
1758 void
1759 ibcm_dump_raw_message(uchar_t *c)
1760 {
1761 	int	i;
1762 
1763 	for (i = 0; i < IBCM_MAD_SIZE; i += 16) {
1764 		/* print in batches of 16 chars at a time */
1765 		IBTF_DPRINTF_L4(cmlog,
1766 		    "%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
1767 		    c[i], c[i + 1], c[i + 2], c[i + 3], c[i + 4], c[i + 5],
1768 		    c[i + 6], c[i + 7], c[i + 8], c[i + 9], c[i + 10],
1769 		    c[i + 11], c[i + 12], c[i + 13], c[i + 14], c[i + 15]);
1770 	}
1771 }
1772 
1773 
1774 /*
1775  * ibcm_dump_srv_rec:
1776  *	Dumps Service Records.
1777  *
1778  * Arguments:
1779  *	srv_rec	- the pointer to sa_service_record_t struct.
1780  *
1781  * Return values: NONE
1782  */
1783 void
1784 ibcm_dump_srvrec(sa_service_record_t *srv_rec)
1785 {
1786 	uint8_t		i;
1787 
1788 	IBTF_DPRINTF_L4(cmlog, "ibcm_dump_srvrec: Service Records");
1789 	IBTF_DPRINTF_L4(cmlog, "SID       : 0x%016llX", srv_rec->ServiceID);
1790 	IBTF_DPRINTF_L4(cmlog, "Svc GID   : 0x%016llX:0x%016llX",
1791 	    srv_rec->ServiceGID.gid_prefix, srv_rec->ServiceGID.gid_guid);
1792 	IBTF_DPRINTF_L4(cmlog, "Svc PKey  : 0x%X", srv_rec->ServiceP_Key);
1793 
1794 	IBTF_DPRINTF_L4(cmlog, "Svc Lease : 0x%lX", srv_rec->ServiceLease);
1795 	IBTF_DPRINTF_L4(cmlog, "Svc Key-hi: 0x%016llX", srv_rec->ServiceKey_hi);
1796 	IBTF_DPRINTF_L4(cmlog, "Svc Key-lo: 0x%016llX", srv_rec->ServiceKey_lo);
1797 	IBTF_DPRINTF_L4(cmlog, "Svc Name  : %s", srv_rec->ServiceName);
1798 	IBTF_DPRINTF_L4(cmlog, "Svc Data  : ");
1799 	for (i = 0; i < IB_SVC_DATA_LEN; i += 8) {
1800 		IBTF_DPRINTF_L4(cmlog,
1801 		    "\t 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X, 0x%X",
1802 		    srv_rec->ServiceData[i], srv_rec->ServiceData[i+1],
1803 		    srv_rec->ServiceData[i+2], srv_rec->ServiceData[i+3],
1804 		    srv_rec->ServiceData[i+4], srv_rec->ServiceData[i+5],
1805 		    srv_rec->ServiceData[i+6], srv_rec->ServiceData[i+7]);
1806 	}
1807 }
1808 
1809 
1810 /*
1811  * ibcm_dump_pathrec:
1812  *	Dumps Path Records.
1813  *
1814  * Arguments:
1815  *	path_rec - the pointer to sa_path_record_t struct.
1816  *
1817  * Return values: NONE
1818  */
1819 void
1820 ibcm_dump_pathrec(sa_path_record_t *path_rec)
1821 {
1822 	IBTF_DPRINTF_L5(cmlog, "Path Record:");
1823 	IBTF_DPRINTF_L5(cmlog, "SGID: (sn_prefix)  %016llX",
1824 	    path_rec->SGID.gid_prefix);
1825 	IBTF_DPRINTF_L5(cmlog, "SGID: (GUID)       %016llX",
1826 	    path_rec->SGID.gid_guid);
1827 	IBTF_DPRINTF_L5(cmlog, "DGID: (sn_prefix)  %016llX",
1828 	    path_rec->DGID.gid_prefix);
1829 	IBTF_DPRINTF_L5(cmlog, "DGID: (GUID)       %016llX",
1830 	    path_rec->DGID.gid_guid);
1831 	IBTF_DPRINTF_L5(cmlog, "SLID:              %04X", path_rec->SLID);
1832 	IBTF_DPRINTF_L5(cmlog, "DLID:              %04X", path_rec->DLID);
1833 	IBTF_DPRINTF_L5(cmlog, "Raw Traffic:       %01X", path_rec->RawTraffic);
1834 	IBTF_DPRINTF_L5(cmlog, "Flow Label:        %05X", path_rec->FlowLabel);
1835 	IBTF_DPRINTF_L5(cmlog, "Hop Limit:         %02X", path_rec->HopLimit);
1836 	IBTF_DPRINTF_L5(cmlog, "TClass:            %02X", path_rec->TClass);
1837 	IBTF_DPRINTF_L5(cmlog, "Reversible:	   %01X", path_rec->Reversible);
1838 	IBTF_DPRINTF_L5(cmlog, "Numb Paths:        %02d", path_rec->NumbPath);
1839 	IBTF_DPRINTF_L5(cmlog, "P_Key:             %04X", path_rec->P_Key);
1840 	IBTF_DPRINTF_L5(cmlog, "SL:                %02X", path_rec->SL);
1841 	IBTF_DPRINTF_L5(cmlog, "Path MTU Selector: %01X",
1842 	    path_rec->MtuSelector);
1843 	IBTF_DPRINTF_L5(cmlog, "Path MTU:          %02X", path_rec->Mtu);
1844 	IBTF_DPRINTF_L5(cmlog, "Path Rate Selector:%01X",
1845 	    path_rec->RateSelector);
1846 	IBTF_DPRINTF_L5(cmlog, "Path Rate:         %02X", path_rec->Rate);
1847 	IBTF_DPRINTF_L5(cmlog, "Packet LT Selector:%01X",
1848 	    path_rec->PacketLifeTimeSelector);
1849 	IBTF_DPRINTF_L5(cmlog, "Packet Life Time:  %d (dec)",
1850 	    path_rec->PacketLifeTime);
1851 	IBTF_DPRINTF_L5(cmlog, "Preference Bit:    %02X", path_rec->Preference);
1852 }
1853 
1854 
1855 /*
1856  * ibcm_dump_node_rec:
1857  *	Dumps Node Records.
1858  *
1859  * Arguments:
1860  *	nrec - the pointer to sa_node_record_t struct.
1861  *
1862  * Return values: NONE
1863  */
1864 void
1865 ibcm_dump_noderec(sa_node_record_t *nrec)
1866 {
1867 	IBTF_DPRINTF_L5(cmlog, "ibcm_dump_noderec: Node Info Record");
1868 	IBTF_DPRINTF_L5(cmlog, "LID       : %04X", nrec->LID);
1869 	IBTF_DPRINTF_L5(cmlog, "Base Ver  : %02X", nrec->NodeInfo.BaseVersion);
1870 	IBTF_DPRINTF_L5(cmlog, "Class Ver : %02X", nrec->NodeInfo.ClassVersion);
1871 	IBTF_DPRINTF_L5(cmlog, "Node Type : %02d", nrec->NodeInfo.NodeType);
1872 	IBTF_DPRINTF_L5(cmlog, "Num Ports : %02X", nrec->NodeInfo.NumPorts);
1873 	IBTF_DPRINTF_L5(cmlog, "SysImgGUID: %016llX",
1874 	    nrec->NodeInfo.SystemImageGUID);
1875 	IBTF_DPRINTF_L5(cmlog, "NODE GUID : %016llX", nrec->NodeInfo.NodeGUID);
1876 	IBTF_DPRINTF_L5(cmlog, "Port GUID : %016llX", nrec->NodeInfo.PortGUID);
1877 	IBTF_DPRINTF_L5(cmlog, "PartionCap: %04X", nrec->NodeInfo.PartitionCap);
1878 	IBTF_DPRINTF_L5(cmlog, "Device ID : %04X", nrec->NodeInfo.DeviceID);
1879 	IBTF_DPRINTF_L5(cmlog, "Revision  : %06X", nrec->NodeInfo.Revision);
1880 	IBTF_DPRINTF_L5(cmlog, "LocalPort#: %02X", nrec->NodeInfo.LocalPortNum);
1881 	IBTF_DPRINTF_L5(cmlog, "Vendor ID : %06X", nrec->NodeInfo.VendorID);
1882 	IBTF_DPRINTF_L5(cmlog, "Description: %s",
1883 	    (char *)&nrec->NodeDescription);
1884 }
1885 #endif
1886 
1887 
1888 /*
1889  * ibcm_ibmf_analyze_error:
1890  *	Checks IBMF status and determines appropriate ibt status.
1891  *
1892  * Arguments:
1893  *	ibmf_status - IBMF Status
1894  *
1895  * Return values:
1896  *	ibt_status_t
1897  */
1898 ibt_status_t
1899 ibcm_ibmf_analyze_error(int ibmf_status)
1900 {
1901 	if (ibt_check_failure(ibmf_status, NULL) != IBT_FAILURE_STANDARD) {
1902 		/*
1903 		 * IBMF specific failure, return special error code
1904 		 * to the client so that it can retrieve any associated ENA.
1905 		 */
1906 		return (ibmf_status);
1907 	} else {
1908 		/*
1909 		 * IBMF failed for some other reason, invalid arguments etc.
1910 		 * Analyze, log ENA with IBTF and obtain a special ibt_status_t
1911 		 * that indicates IBMF failure.
1912 		 */
1913 		if ((ibmf_status == IBMF_BAD_CLASS) ||
1914 		    (ibmf_status == IBMF_BAD_HANDLE) ||
1915 		    (ibmf_status == IBMF_BAD_QP_HANDLE) ||
1916 		    (ibmf_status == IBMF_BAD_NODE) ||
1917 		    (ibmf_status == IBMF_BAD_PORT) ||
1918 		    (ibmf_status == IBMF_BAD_VERSION) ||
1919 		    (ibmf_status == IBMF_BAD_FLAGS) ||
1920 		    (ibmf_status == IBMF_BAD_SIZE) ||
1921 		    (ibmf_status == IBMF_INVALID_GID) ||
1922 		    (ibmf_status == IBMF_INVALID_ARG) ||
1923 		    (ibmf_status == IBMF_INVALID_FIELD) ||
1924 		    (ibmf_status == IBMF_UNSUPP_METHOD) ||
1925 		    (ibmf_status == IBMF_UNSUPP_METHOD_ATTR)) {
1926 
1927 			/*
1928 			 * These errors, we should not see...
1929 			 * something really bad happened!.
1930 			 */
1931 			IBTF_DPRINTF_L2(cmlog, "ibcm_ibmf_analyze_error: "
1932 			    "Unexpected ERROR from IBMF - %d", ibmf_status);
1933 		}
1934 		return (ibt_get_module_failure(IBT_FAILURE_IBMF, 0));
1935 	}
1936 }
1937