xref: /onnv-gate/usr/src/uts/common/io/ib/mgt/ibcm/ibcm_path.c (revision 11951:8e81dc1bbfea)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/ib/mgt/ibcm/ibcm_impl.h>
27 #include <sys/ib/mgt/ibcm/ibcm_arp.h>
28 
29 /*
30  * ibcm_path.c
31  *
32  * ibt_get_paths() implement the Path Informations related functionality.
33  */
34 
35 /* ibcm_saa_service_rec() fills in ServiceID and DGID. */
36 typedef struct ibcm_dest_s {
37 	ib_gid_t	d_gid;
38 	ib_svc_id_t	d_sid;
39 	ibt_srv_data_t	d_sdata;
40 	ib_pkey_t	d_pkey;
41 	uint_t		d_tag;	/* 0 = Unicast, 1 = Multicast */
42 } ibcm_dest_t;
43 
44 /* Holds Destination information needed to fill in ibt_path_info_t. */
45 typedef struct ibcm_dinfo_s {
46 	uint8_t		num_dest;
47 	ib_pkey_t	p_key;
48 	ibcm_dest_t	dest[1];
49 } ibcm_dinfo_t;
50 
51 _NOTE(SCHEME_PROTECTS_DATA("Temporary path storage", ibcm_dinfo_s))
52 _NOTE(READ_ONLY_DATA(ibt_path_attr_s))
53 
54 typedef struct ibcm_path_tqargs_s {
55 	ibt_path_attr_t		attr;
56 	ibt_path_info_t		*paths;
57 	uint8_t			*num_paths_p;
58 	ibt_path_handler_t	func;
59 	void			*arg;
60 	ibt_path_flags_t	flags;
61 	uint8_t			max_paths;
62 } ibcm_path_tqargs_t;
63 
64 
65 /* Prototype Declarations. */
66 static ibt_status_t ibcm_saa_path_rec(ibcm_path_tqargs_t *,
67     ibtl_cm_port_list_t *, ibcm_dinfo_t *, uint8_t *);
68 
69 static ibt_status_t ibcm_update_cep_info(sa_path_record_t *,
70     ibtl_cm_port_list_t *, ibtl_cm_hca_port_t *, ibt_cep_path_t *);
71 
72 static ibt_status_t ibcm_saa_service_rec(ibcm_path_tqargs_t *,
73     ibtl_cm_port_list_t *, ibcm_dinfo_t *);
74 
75 static ibt_status_t ibcm_get_single_pathrec(ibcm_path_tqargs_t *,
76     ibtl_cm_port_list_t *, ibcm_dinfo_t *, uint8_t,
77     uint8_t *, ibt_path_info_t *);
78 
79 static ibt_status_t ibcm_get_multi_pathrec(ibcm_path_tqargs_t *,
80     ibtl_cm_port_list_t *, ibcm_dinfo_t *dinfo,
81     uint8_t *, ibt_path_info_t *);
82 
83 static ibt_status_t ibcm_validate_path_attributes(ibt_path_attr_t *attrp,
84     ibt_path_flags_t flags, uint8_t max_paths);
85 
86 static ibt_status_t ibcm_handle_get_path(ibt_path_attr_t *attrp,
87     ibt_path_flags_t flags, uint8_t max_paths, ibt_path_info_t *paths,
88     uint8_t *num_path_p, ibt_path_handler_t func, void  *arg);
89 
90 static void ibcm_process_async_get_paths(void *tq_arg);
91 
92 static ibt_status_t ibcm_process_get_paths(void *tq_arg);
93 
94 static ibt_status_t ibcm_get_comp_pgids(ib_gid_t, ib_gid_t, ib_guid_t,
95     ib_gid_t **, uint_t *);
96 
97 /*
98  * Function:
99  *	ibt_aget_paths
100  * Input:
101  *	ibt_hdl		The handle returned to the client by the IBTF from an
102  *			ibt_attach() call. Can be used by the IBTF Policy module
103  *			and CM in the determination of the "best" path to the
104  *			specified destination for this class of driver.
105  *	flags		Path flags.
106  *	attrp		Points to an ibt_path_attr_t struct that contains
107  *			required and optional attributes.
108  *	func		A pointer to an ibt_path_handler_t function to call
109  *			when ibt_aget_paths() completes.
110  *	arg		The argument to 'func'.
111  * Returns:
112  *	IBT_SUCCESS on early validation of attributes else appropriate error.
113  * Description:
114  *	Finds the best path to a specified destination or service
115  *	asynchronously (as determined by the IBTL) that satisfies the
116  *	requirements specified in an ibt_path_attr_t struct.
117  *	ibt_aget_paths() is a Non-Blocking version of ibt_get_paths().
118  */
119 ibt_status_t
120 ibt_aget_paths(ibt_clnt_hdl_t ibt_hdl, ibt_path_flags_t flags,
121     ibt_path_attr_t *attrp, uint8_t max_paths, ibt_path_handler_t func,
122     void  *arg)
123 {
124 	IBTF_DPRINTF_L3(cmlog, "ibt_aget_paths(%p(%s), 0x%X, %p, %d, %p)",
125 	    ibt_hdl, ibtl_cm_get_clnt_name(ibt_hdl), flags, attrp, max_paths,
126 	    func);
127 
128 	if (func == NULL) {
129 		IBTF_DPRINTF_L2(cmlog, "ibt_aget_paths: Function Pointer is "
130 		    "NULL - ERROR ");
131 		return (IBT_INVALID_PARAM);
132 	}
133 
134 	/* Memory for path info will be allocated in ibcm_process_get_paths() */
135 	return (ibcm_handle_get_path(attrp, flags, max_paths, NULL, NULL,
136 	    func, arg));
137 }
138 
139 
140 /*
141  * ibt_get_paths() cache consists of one or more of:
142  *
143  *	ib_gid_t dgid (attrp->pa_dgids[0])
144  *	ibt_path_attr_t attr
145  *	ibt_path_flags_t flags
146  *	ibt_path_info_t path
147  *
148  * If the first 3 match, max_paths is 1, sname is NULL, and sid is 0,
149  * then the path is returned immediately.
150  *
151  * Note that a compare of "attr" is non-trivial.  Only accept ones
152  * that memcmp() succeeds, i.e., basically assume a bzero was done.
153  *
154  * Cache must be invalidated if PORT_DOWN event or GID_UNAVAIL occurs.
155  * Cache must be freed as part of _fini.
156  */
157 
158 #define	IBCM_PATH_CACHE_SIZE	16	/* keep small for linear search */
159 #define	IBCM_PATH_CACHE_TIMEOUT	60	/* purge cache after 60 seconds */
160 
161 typedef struct ibcm_path_cache_s {
162 	ib_gid_t		dgid;
163 	ibt_path_attr_t		attr;
164 	ibt_path_flags_t	flags;
165 	ibt_path_info_t		path;
166 } ibcm_path_cache_t;
167 
168 kmutex_t ibcm_path_cache_mutex;
169 int ibcm_path_cache_invalidate;	/* invalidate cache on next ibt_get_paths */
170 clock_t ibcm_path_cache_timeout = IBCM_PATH_CACHE_TIMEOUT; /* tunable */
171 timeout_id_t ibcm_path_cache_timeout_id;
172 int ibcm_path_cache_size_init = IBCM_PATH_CACHE_SIZE;	/* tunable */
173 int ibcm_path_cache_size;
174 ibcm_path_cache_t *ibcm_path_cachep;
175 
176 /* tunable, set to 1 to not allow link-local address */
177 int	ibcm_ip6_linklocal_addr_ok = 0;
178 
179 struct ibcm_path_cache_stat_s {
180 	int hits;
181 	int misses;
182 	int adds;
183 	int already_in_cache;
184 	int bad_path_for_cache;
185 	int purges;
186 	int timeouts;
187 } ibcm_path_cache_stats;
188 
189 /*ARGSUSED*/
190 static void
191 ibcm_path_cache_timeout_cb(void *arg)
192 {
193 	clock_t timeout_in_hz;
194 
195 	timeout_in_hz = drv_usectohz(ibcm_path_cache_timeout * 1000000);
196 	mutex_enter(&ibcm_path_cache_mutex);
197 	ibcm_path_cache_invalidate = 1;	/* invalidate cache on next check */
198 	if (ibcm_path_cache_timeout_id)
199 		ibcm_path_cache_timeout_id = timeout(ibcm_path_cache_timeout_cb,
200 		    NULL, timeout_in_hz);
201 	/* else we're in _fini */
202 	mutex_exit(&ibcm_path_cache_mutex);
203 }
204 
205 void
206 ibcm_path_cache_init(void)
207 {
208 	clock_t timeout_in_hz;
209 	int cache_size = ibcm_path_cache_size_init;
210 	ibcm_path_cache_t *path_cachep;
211 
212 	timeout_in_hz = drv_usectohz(ibcm_path_cache_timeout * 1000000);
213 	path_cachep = kmem_zalloc(cache_size * sizeof (*path_cachep), KM_SLEEP);
214 	mutex_init(&ibcm_path_cache_mutex, NULL, MUTEX_DEFAULT, NULL);
215 	mutex_enter(&ibcm_path_cache_mutex);
216 	ibcm_path_cache_size = cache_size;
217 	ibcm_path_cachep = path_cachep;
218 	ibcm_path_cache_timeout_id = timeout(ibcm_path_cache_timeout_cb,
219 	    NULL, timeout_in_hz);
220 	mutex_exit(&ibcm_path_cache_mutex);
221 }
222 
223 void
224 ibcm_path_cache_fini(void)
225 {
226 	timeout_id_t tmp_timeout_id;
227 	int cache_size;
228 	ibcm_path_cache_t *path_cachep;
229 
230 	mutex_enter(&ibcm_path_cache_mutex);
231 	if (ibcm_path_cache_timeout_id) {
232 		tmp_timeout_id = ibcm_path_cache_timeout_id;
233 		ibcm_path_cache_timeout_id = 0;	/* no more timeouts */
234 	}
235 	cache_size = ibcm_path_cache_size;
236 	path_cachep = ibcm_path_cachep;
237 	mutex_exit(&ibcm_path_cache_mutex);
238 	if (tmp_timeout_id)
239 		(void) untimeout(tmp_timeout_id);
240 	mutex_destroy(&ibcm_path_cache_mutex);
241 	kmem_free(path_cachep, cache_size * sizeof (*path_cachep));
242 }
243 
244 static ibcm_status_t
245 ibcm_path_cache_check(ibt_path_flags_t flags, ibt_path_attr_t *attrp,
246     uint8_t max_paths, ibt_path_info_t *path, uint8_t *num_paths_p)
247 {
248 	int i;
249 	ib_gid_t dgid;
250 	ibcm_path_cache_t *path_cachep;
251 
252 	if (max_paths != 1 || attrp->pa_num_dgids != 1 ||
253 	    attrp->pa_sname != NULL || attrp->pa_sid != 0) {
254 		mutex_enter(&ibcm_path_cache_mutex);
255 		ibcm_path_cache_stats.bad_path_for_cache++;
256 		mutex_exit(&ibcm_path_cache_mutex);
257 		return (IBCM_FAILURE);
258 	}
259 
260 	dgid = attrp->pa_dgids[0];
261 	if ((dgid.gid_guid | dgid.gid_prefix) == 0ULL)
262 		return (IBCM_FAILURE);
263 
264 	mutex_enter(&ibcm_path_cache_mutex);
265 	if (ibcm_path_cache_invalidate) {	/* invalidate all entries */
266 		ibcm_path_cache_stats.timeouts++;
267 		ibcm_path_cache_invalidate = 0;
268 		path_cachep = ibcm_path_cachep;
269 		for (i = 0; i < ibcm_path_cache_size; i++, path_cachep++) {
270 			path_cachep->dgid.gid_guid = 0ULL;
271 			path_cachep->dgid.gid_prefix = 0ULL;
272 		}
273 		mutex_exit(&ibcm_path_cache_mutex);
274 		return (IBCM_FAILURE);
275 	}
276 
277 	path_cachep = ibcm_path_cachep;
278 	for (i = 0; i < ibcm_path_cache_size; i++, path_cachep++) {
279 		if (path_cachep->dgid.gid_guid == 0ULL)
280 			break;	/* end of search, no more valid cache entries */
281 
282 		/* make pa_dgids pointers match, so we can use memcmp */
283 		path_cachep->attr.pa_dgids = attrp->pa_dgids;
284 		if (path_cachep->flags != flags ||
285 		    path_cachep->dgid.gid_guid != dgid.gid_guid ||
286 		    path_cachep->dgid.gid_prefix != dgid.gid_prefix ||
287 		    memcmp(&(path_cachep->attr), attrp, sizeof (*attrp)) != 0) {
288 			/* make pa_dgids NULL again */
289 			path_cachep->attr.pa_dgids = NULL;
290 			continue;
291 		}
292 		/* else we have a match */
293 		/* make pa_dgids NULL again */
294 		path_cachep->attr.pa_dgids = NULL;
295 		*path = path_cachep->path;	/* retval */
296 		if (num_paths_p)
297 			*num_paths_p = 1;	/* retval */
298 		ibcm_path_cache_stats.hits++;
299 		mutex_exit(&ibcm_path_cache_mutex);
300 		return (IBCM_SUCCESS);
301 	}
302 	ibcm_path_cache_stats.misses++;
303 	mutex_exit(&ibcm_path_cache_mutex);
304 	return (IBCM_FAILURE);
305 }
306 
307 static void
308 ibcm_path_cache_add(ibt_path_flags_t flags,
309     ibt_path_attr_t *attrp, uint8_t max_paths, ibt_path_info_t *path)
310 {
311 	int i;
312 	ib_gid_t dgid;
313 	ibcm_path_cache_t *path_cachep;
314 
315 	if (max_paths != 1 || attrp->pa_num_dgids != 1 ||
316 	    attrp->pa_sname != NULL || attrp->pa_sid != 0)
317 		return;
318 
319 	dgid = attrp->pa_dgids[0];
320 	if ((dgid.gid_guid | dgid.gid_prefix) == 0ULL)
321 		return;
322 
323 	mutex_enter(&ibcm_path_cache_mutex);
324 	path_cachep = ibcm_path_cachep;
325 	for (i = 0; i < ibcm_path_cache_size; i++, path_cachep++) {
326 		path_cachep->attr.pa_dgids = attrp->pa_dgids;
327 		if (path_cachep->flags == flags &&
328 		    path_cachep->dgid.gid_guid == dgid.gid_guid &&
329 		    path_cachep->dgid.gid_prefix == dgid.gid_prefix &&
330 		    memcmp(&(path_cachep->attr), attrp, sizeof (*attrp)) == 0) {
331 			/* already in cache */
332 			ibcm_path_cache_stats.already_in_cache++;
333 			path_cachep->attr.pa_dgids = NULL;
334 			mutex_exit(&ibcm_path_cache_mutex);
335 			return;
336 		}
337 		if (path_cachep->dgid.gid_guid != 0ULL) {
338 			path_cachep->attr.pa_dgids = NULL;
339 			continue;
340 		}
341 		/* else the rest of the entries are free, so use this one */
342 		ibcm_path_cache_stats.adds++;
343 		path_cachep->flags = flags;
344 		path_cachep->attr = *attrp;
345 		path_cachep->attr.pa_dgids = NULL;
346 		path_cachep->dgid = attrp->pa_dgids[0];
347 		path_cachep->path = *path;
348 		mutex_exit(&ibcm_path_cache_mutex);
349 		return;
350 	}
351 	mutex_exit(&ibcm_path_cache_mutex);
352 }
353 
354 void
355 ibcm_path_cache_purge(void)
356 {
357 	mutex_enter(&ibcm_path_cache_mutex);
358 	ibcm_path_cache_invalidate = 1;	/* invalidate cache on next check */
359 	ibcm_path_cache_stats.purges++;
360 	mutex_exit(&ibcm_path_cache_mutex);
361 }
362 
363 /*
364  * Function:
365  *	ibt_get_paths
366  * Input:
367  *	ibt_hdl		The handle returned to the client by the IBTF from an
368  *			ibt_attach() call. Can be used by the IBTF Policy module
369  *			and CM in the determination of the "best" path to the
370  *			specified destination for this class of driver.
371  *	flags		Path flags.
372  *	attrp		Points to an ibt_path_attr_t struct that contains
373  *			required and optional attributes.
374  *	max_paths	The size of the "paths" array argument. Also, this
375  *			is the limit on the number of paths returned.
376  *			max_paths indicates the number of requested paths to
377  *			the specified destination(s).
378  * Output:
379  *	paths		An array of ibt_path_info_t structs filled in by
380  *			ibt_get_paths() as output parameters. Upon return,
381  *			array elements with non-NULL HCA GUIDs are valid.
382  *	num_paths_p	If non-NULL, return the actual number of paths found.
383  * Returns:
384  *	IBT_SUCCESS on Success else appropriate error.
385  * Description:
386  *	Finds the best path to a specified destination (as determined by the
387  *	IBTL) that satisfies the requirements specified in an ibt_path_attr_t
388  *	struct.
389  *
390  *	This routine can not be called from interrupt context.
391  */
392 ibt_status_t
393 ibt_get_paths(ibt_clnt_hdl_t ibt_hdl, ibt_path_flags_t flags,
394     ibt_path_attr_t *attrp, uint8_t max_paths, ibt_path_info_t *paths,
395     uint8_t *num_paths_p)
396 {
397 	ibt_status_t	retval;
398 
399 	ASSERT(paths != NULL);
400 
401 	IBTF_DPRINTF_L3(cmlog, "ibt_get_paths(%p(%s), 0x%X, %p, %d)",
402 	    ibt_hdl, ibtl_cm_get_clnt_name(ibt_hdl), flags, attrp, max_paths);
403 
404 	if (paths == NULL) {
405 		IBTF_DPRINTF_L2(cmlog, "ibt_get_paths: Path Info Pointer is "
406 		    "NULL - ERROR ");
407 		return (IBT_INVALID_PARAM);
408 	}
409 
410 	if (num_paths_p != NULL)
411 		*num_paths_p = 0;
412 
413 	if (ibcm_path_cache_check(flags, attrp, max_paths, paths,
414 	    num_paths_p) == IBCM_SUCCESS)
415 		return (IBT_SUCCESS);
416 
417 	retval = ibcm_handle_get_path(attrp, flags, max_paths, paths,
418 	    num_paths_p, NULL, NULL);
419 
420 	if (retval == IBT_SUCCESS)
421 		ibcm_path_cache_add(flags, attrp, max_paths, paths);
422 	return (retval);
423 }
424 
425 
426 static ibt_status_t
427 ibcm_handle_get_path(ibt_path_attr_t *attrp, ibt_path_flags_t flags,
428     uint8_t max_paths, ibt_path_info_t *paths, uint8_t *num_path_p,
429     ibt_path_handler_t func, void  *arg)
430 {
431 	ibcm_path_tqargs_t	*path_tq;
432 	int		sleep_flag = ((func == NULL) ? KM_SLEEP : KM_NOSLEEP);
433 	int		len;
434 	ibt_status_t	retval;
435 
436 	retval = ibcm_validate_path_attributes(attrp, flags, max_paths);
437 	if (retval != IBT_SUCCESS)
438 		return (retval);
439 
440 	len = (attrp->pa_num_dgids * sizeof (ib_gid_t)) +
441 	    sizeof (ibcm_path_tqargs_t);
442 
443 	path_tq = kmem_alloc(len, sleep_flag);
444 	if (path_tq == NULL) {
445 		IBTF_DPRINTF_L2(cmlog, "ibcm_handle_get_path: "
446 		    "Unable to allocate memory for local usage.");
447 		return (IBT_INSUFF_KERNEL_RESOURCE);
448 	}
449 
450 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*path_tq))
451 
452 	bcopy(attrp, &path_tq->attr, sizeof (ibt_path_attr_t));
453 
454 	if (attrp->pa_num_dgids) {
455 		path_tq->attr.pa_dgids = (ib_gid_t *)(((uchar_t *)path_tq) +
456 		    sizeof (ibcm_path_tqargs_t));
457 
458 		bcopy(attrp->pa_dgids, path_tq->attr.pa_dgids,
459 		    sizeof (ib_gid_t) * attrp->pa_num_dgids);
460 	} else {
461 		path_tq->attr.pa_dgids = NULL;
462 	}
463 
464 	/* Ignore IBT_PATH_AVAIL flag, if only one path is requested. */
465 	if ((flags & IBT_PATH_AVAIL) && (max_paths == 1)) {
466 		flags &= ~IBT_PATH_AVAIL;
467 
468 		IBTF_DPRINTF_L4(cmlog, "ibcm_handle_get_path: "
469 		    "Ignoring IBT_PATH_AVAIL flag, as only ONE path "
470 		    "information is requested.");
471 	}
472 
473 	path_tq->flags = flags;
474 	path_tq->max_paths = max_paths;
475 	path_tq->paths = paths;
476 	path_tq->num_paths_p = num_path_p;
477 	path_tq->func = func;
478 	path_tq->arg = arg;
479 
480 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*path_tq))
481 
482 	if (func != NULL) {		/* Non-Blocking */
483 		IBTF_DPRINTF_L3(cmlog, "ibcm_handle_get_path: Non Blocking");
484 		if (taskq_dispatch(ibcm_taskq, ibcm_process_async_get_paths,
485 		    path_tq, TQ_NOSLEEP) == 0) {
486 			IBTF_DPRINTF_L2(cmlog, "ibcm_handle_get_path: "
487 			    "Failed to dispatch the TaskQ");
488 			kmem_free(path_tq, len);
489 			return (IBT_INSUFF_KERNEL_RESOURCE);
490 		} else
491 			return (IBT_SUCCESS);
492 	} else {		/* Blocking */
493 		IBTF_DPRINTF_L3(cmlog, "ibcm_handle_get_path: Blocking");
494 		return (ibcm_process_get_paths(path_tq));
495 	}
496 }
497 
498 
499 static void
500 ibcm_process_async_get_paths(void *tq_arg)
501 {
502 	(void) ibcm_process_get_paths(tq_arg);
503 }
504 
505 
506 static ibt_status_t
507 ibcm_validate_path_attributes(ibt_path_attr_t *attrp, ibt_path_flags_t flags,
508     uint8_t max_paths)
509 {
510 	uint_t			i;
511 
512 	IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: Inputs are: "
513 	    "HCA (%llX, %d),\n\tSGID(%llX:%llX), SName=\"%s\",\n\tSID= %llX, "
514 	    "Maxpath= %d, Flags= 0x%X, #Dgid= %d, SDFlag= 0x%llX",
515 	    attrp->pa_hca_guid, attrp->pa_hca_port_num,
516 	    attrp->pa_sgid.gid_prefix, attrp->pa_sgid.gid_guid,
517 	    ((attrp->pa_sname != NULL) ? attrp->pa_sname : ""), attrp->pa_sid,
518 	    max_paths, flags, attrp->pa_num_dgids, attrp->pa_sd_flags);
519 
520 	/*
521 	 * Validate Path Flags.
522 	 * IBT_PATH_AVAIL & IBT_PATH_PERF are mutually exclusive.
523 	 */
524 	if ((flags & IBT_PATH_AVAIL) && (flags & IBT_PATH_PERF)) {
525 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
526 		    "Invalid Flags: 0x%X,\n\t AVAIL and PERF flags cannot "
527 		    "specified together.", flags);
528 		return (IBT_INVALID_PARAM);
529 	}
530 
531 	/* Validate number of records requested. */
532 	if ((flags & (IBT_PATH_AVAIL | IBT_PATH_PERF)) &&
533 	    (max_paths > IBT_MAX_SPECIAL_PATHS)) {
534 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
535 		    "Max records that can be requested is <%d> \n"
536 		    "when IBT_PATH_AVAIL or IBT_PATH_PERF flag is specified.",
537 		    IBT_MAX_SPECIAL_PATHS);
538 		return (IBT_INVALID_PARAM);
539 	}
540 
541 	/* Only 2 destinations can be specified w/ APM flag. */
542 	if ((flags & IBT_PATH_APM) && (attrp->pa_num_dgids > 2)) {
543 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes:\n\t Max "
544 		    "number of DGIDs that can be specified w/APM flag is 2");
545 		return (IBT_INVALID_PARAM);
546 	}
547 
548 	/*
549 	 * Max_paths of "0" is invalid.
550 	 * w/ IBT_PATH_MULTI_SVC_DEST flag, max_paths must be greater than "1".
551 	 */
552 	if ((max_paths == 0) ||
553 	    ((flags & IBT_PATH_MULTI_SVC_DEST) && (max_paths < 2))) {
554 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
555 		    "Invalid number of records requested:\n flags 0x%X, "
556 		    "max_paths %d", flags, max_paths);
557 		return (IBT_INVALID_PARAM);
558 	}
559 
560 	/*
561 	 * If IBT_PATH_MULTI_SVC_DEST is set, then ServiceName and/or Service ID
562 	 * must be specified and DGIDs SHOULD NOT be specified.
563 	 */
564 	if ((flags & IBT_PATH_MULTI_SVC_DEST) && ((attrp->pa_num_dgids > 0) ||
565 	    ((attrp->pa_sid == 0) && ((attrp->pa_sname == NULL) ||
566 	    ((attrp->pa_sname != NULL) && (strlen(attrp->pa_sname) == 0)))))) {
567 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
568 		    "Invalid Flags: 0x%X, IBT_PATH_MULTI_SVC_DEST flag set "
569 		    "but Service Name \n or Service ID NOT specified or DGIDs "
570 		    "are specified.", flags);
571 		return (IBT_INVALID_PARAM);
572 	}
573 
574 	/*
575 	 * User need to specify the destination information, which can be
576 	 * provided as one or more of the following.
577 	 *	o ServiceName
578 	 *	o ServiceID
579 	 *	o Array of DGIDs w/Num of DGIDs, (max of 2)
580 	 */
581 	if ((attrp->pa_sid == 0) && (attrp->pa_num_dgids == 0) &&
582 	    ((attrp->pa_sname == NULL) || ((attrp->pa_sname != NULL) &&
583 	    (strlen(attrp->pa_sname) == 0)))) {
584 		/* Destination information not provided, bail out. */
585 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
586 		    "Client's MUST supply DestInfo.");
587 		return (IBT_INVALID_PARAM);
588 	}
589 
590 	/* If DGIDs are provided, validate them. */
591 	if (attrp->pa_num_dgids > 0) {
592 		if (attrp->pa_dgids == NULL) {
593 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
594 			    "pa_dgids NULL, but pa_num_dgids : %d",
595 			    attrp->pa_num_dgids);
596 			return (IBT_INVALID_PARAM);
597 		}
598 
599 		/* Validate DGIDs */
600 		for (i = 0; i < attrp->pa_num_dgids; i++) {
601 			ib_gid_t	gid = attrp->pa_dgids[i];
602 
603 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
604 			    "DGID[%d] = %llX:%llX", i, gid.gid_prefix,
605 			    gid.gid_guid);
606 
607 			/* APM request for MultiCast destination is invalid. */
608 			if ((gid.gid_prefix >> 56ULL & 0xFF) == 0xFF) {
609 				if (flags & IBT_PATH_APM) {
610 					IBTF_DPRINTF_L2(cmlog,
611 					    "ibcm_validate_path_attributes: "
612 					    "APM for MGIDs not supported.");
613 					return (IBT_INVALID_PARAM);
614 				}
615 			} else if ((gid.gid_prefix == 0) ||
616 			    (gid.gid_guid == 0)) {
617 				IBTF_DPRINTF_L2(cmlog,
618 				    "ibcm_validate_path_attributes: ERROR: "
619 				    "Invalid DGIDs specified");
620 				return (IBT_INVALID_PARAM);
621 			}
622 		}
623 	}
624 
625 	/* Check for valid Service Name length. */
626 	if ((attrp->pa_sname != NULL) &&
627 	    (strlen(attrp->pa_sname) >= IB_SVC_NAME_LEN)) {
628 		IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
629 		    "ServiceName too long");
630 		return (IBT_INVALID_PARAM);
631 	}
632 
633 	/* If P_Key is specified, check for invalid p_key's */
634 	if (flags & IBT_PATH_PKEY) {
635 		/* Limited P_Key is NOT supported as of now!. */
636 		if ((attrp->pa_pkey == IB_PKEY_INVALID_FULL) ||
637 		    (attrp->pa_pkey & 0x8000) == 0) {
638 			IBTF_DPRINTF_L2(cmlog, "ibcm_validate_path_attributes: "
639 			    "Specified P_Key is invalid: 0x%X", attrp->pa_pkey);
640 			return (IBT_INVALID_PARAM);
641 		}
642 		IBTF_DPRINTF_L3(cmlog, "ibcm_validate_path_attributes: "
643 		    "P_Key= 0x%X", attrp->pa_pkey);
644 	}
645 
646 	return (IBT_SUCCESS);
647 }
648 
649 
650 static ibt_status_t
651 ibcm_process_get_paths(void *tq_arg)
652 {
653 	ibcm_path_tqargs_t	*p_arg = (ibcm_path_tqargs_t *)tq_arg;
654 	ibcm_dinfo_t		*dinfo;
655 	int			len;
656 	uint8_t			max_paths, num_path;
657 	ibt_status_t		retval;
658 	ib_gid_t		*d_gids_p = NULL;
659 	ibtl_cm_port_list_t	*slistp = NULL;
660 	uint_t			dnum = 0;
661 	uint8_t			num_dest, i, j;
662 	ibcm_hca_info_t		*hcap;
663 	ibmf_saa_handle_t	saa_handle;
664 
665 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths(%p, 0x%X, %d) ",
666 	    p_arg, p_arg->flags, p_arg->max_paths);
667 
668 	max_paths = num_path = p_arg->max_paths;
669 
670 	/*
671 	 * Prepare the Destination list based on the input DGIDs and
672 	 * other attributes.
673 	 *
674 	 * APM is requested and pa_dgids are specified.  If multiple DGIDs are
675 	 * specified, check out whether they are companion to each other or if
676 	 * only one DGID is specified, then get the companion port GID for that.
677 	 */
678 	if (p_arg->attr.pa_num_dgids) {
679 		if (p_arg->flags & IBT_PATH_APM) {
680 			ib_gid_t	c_gid, n_gid;
681 
682 			IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: "
683 			    "DGIDs specified w/ APM Flag");
684 
685 			c_gid = p_arg->attr.pa_dgids[0];
686 			if (p_arg->attr.pa_num_dgids > 1)
687 				n_gid = p_arg->attr.pa_dgids[1];
688 			else
689 				n_gid.gid_prefix = n_gid.gid_guid = 0;
690 
691 			retval = ibcm_get_comp_pgids(c_gid, n_gid, 0, &d_gids_p,
692 			    &dnum);
693 			if ((retval != IBT_SUCCESS) &&
694 			    (retval != IBT_GIDS_NOT_FOUND)) {
695 				IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths:"
696 				    " Invalid DGIDs specified w/ APM Flag");
697 				goto path_error2;
698 			}
699 			IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: "
700 			    "Found %d Comp DGID", dnum);
701 		}
702 
703 		if (dnum) {
704 			len = 1;
705 		} else {
706 			len = p_arg->attr.pa_num_dgids - 1;
707 		}
708 		num_dest = len + 1;
709 
710 		IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: #dgid %d, dnum "
711 		    "%d, #dest %d", p_arg->attr.pa_num_dgids, dnum, num_dest);
712 	} else {
713 		if (p_arg->flags & IBT_PATH_MULTI_SVC_DEST) {
714 			IBTF_DPRINTF_L4(cmlog, "ibcm_process_get_paths: "
715 			    "IBT_PATH_MULTI_SVC_DEST flags set");
716 			len = max_paths - 1;
717 		} else if (p_arg->flags & IBT_PATH_APM) {
718 			len = 1;
719 		} else {
720 			len = 0;
721 		}
722 		num_dest = 0;
723 	}
724 
725 	/* Allocate memory and accumulate all destination information */
726 	len = (len * sizeof (ibcm_dest_t)) + sizeof (ibcm_dinfo_t);
727 
728 	dinfo = kmem_zalloc(len, KM_SLEEP);
729 	dinfo->num_dest = num_dest;
730 	if (p_arg->flags & IBT_PATH_PKEY)
731 		dinfo->p_key = p_arg->attr.pa_pkey;
732 
733 	for (i = 0, j = 0; i < num_dest; i++) {
734 		if (i < p_arg->attr.pa_num_dgids)
735 			dinfo->dest[i].d_gid = p_arg->attr.pa_dgids[i];
736 		else
737 			dinfo->dest[i].d_gid = d_gids_p[j++];
738 	}
739 
740 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*p_arg))
741 
742 	/* IBTF allocates memory for path_info in case of Async Get Paths */
743 	if (p_arg->paths == NULL)
744 		p_arg->paths = kmem_zalloc(sizeof (ibt_path_info_t) * max_paths,
745 		    KM_SLEEP);
746 
747 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*p_arg))
748 
749 	/*
750 	 * Get list of active HCA<->Port list, that matches input specified attr
751 	 */
752 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: Get Paths from \n HCA "
753 	    "(%llX:%d), SGID  %llX:%llX", p_arg->attr.pa_hca_guid,
754 	    p_arg->attr.pa_hca_port_num, p_arg->attr.pa_sgid.gid_prefix,
755 	    p_arg->attr.pa_sgid.gid_guid);
756 
757 	retval = ibtl_cm_get_active_plist(&p_arg->attr, p_arg->flags, &slistp);
758 	if (retval != IBT_SUCCESS) {
759 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths: HCA capable of "
760 		    "requested source attributes NOT available.");
761 		goto path_error;
762 	}
763 
764 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: HCA (%llX, %d)",
765 	    slistp->p_hca_guid, slistp->p_port_num);
766 
767 	hcap = ibcm_find_hca_entry(slistp->p_hca_guid);
768 	if (hcap == NULL) {
769 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths: "
770 		    "NO HCA found");
771 		retval = IBT_HCA_BUSY_DETACHING;
772 		goto path_error;
773 	}
774 
775 	/* Get SA Access Handle. */
776 	for (i = 0; i < slistp->p_count; i++) {
777 		if (i == 0) {
778 			/* Validate whether this HCA supports APM */
779 			if ((p_arg->flags & IBT_PATH_APM) &&
780 			    (!(hcap->hca_caps & IBT_HCA_AUTO_PATH_MIG))) {
781 				IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths:"
782 				    " HCA (%llX): APM NOT SUPPORTED ",
783 				    slistp[i].p_hca_guid);
784 				retval = IBT_APM_NOT_SUPPORTED;
785 				goto path_error1;
786 			}
787 		}
788 
789 		saa_handle = ibcm_get_saa_handle(hcap, slistp[i].p_port_num);
790 		if (saa_handle == NULL) {
791 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths: "
792 			    "SAA HDL NULL, HCA (%llX:%d) NOT ACTIVE",
793 			    slistp[i].p_hca_guid, slistp[i].p_port_num);
794 			retval = IBT_HCA_PORT_NOT_ACTIVE;
795 			goto path_error1;
796 		}
797 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*slistp))
798 		slistp[i].p_saa_hdl = saa_handle;
799 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*slistp))
800 	}
801 
802 	/*
803 	 * If Service Name or Service ID are specified, first retrieve
804 	 * Service Records.
805 	 */
806 	if ((p_arg->attr.pa_sid != 0) || ((p_arg->attr.pa_sname != NULL) &&
807 	    (strlen(p_arg->attr.pa_sname) != 0))) {
808 
809 		IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_paths: Get Service "
810 		    "Record for \n\t(%llX, \"%s\")", p_arg->attr.pa_sid,
811 		    ((p_arg->attr.pa_sname != NULL) ?
812 		    p_arg->attr.pa_sname : ""));
813 
814 		/* Get Service Records. */
815 		retval = ibcm_saa_service_rec(p_arg, slistp, dinfo);
816 		if ((retval != IBT_SUCCESS) && (retval != IBT_INSUFF_DATA)) {
817 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths: Status="
818 			    "%d, Failed to get Service Record for \n\t"
819 			    "(%llX, \"%s\")", retval, p_arg->attr.pa_sid,
820 			    ((p_arg->attr.pa_sname != NULL) ?
821 			    p_arg->attr.pa_sname : ""));
822 			goto path_error1;
823 		}
824 	}
825 
826 	/* Get Path Records. */
827 	retval = ibcm_saa_path_rec(p_arg, slistp, dinfo, &num_path);
828 
829 path_error1:
830 	ibcm_dec_hca_acc_cnt(hcap);
831 
832 path_error:
833 	if (slistp)
834 		ibtl_cm_free_active_plist(slistp);
835 
836 	if (dinfo)
837 		kmem_free(dinfo, len);
838 
839 path_error2:
840 	if ((retval != IBT_SUCCESS) && (retval != IBT_INSUFF_DATA))
841 		num_path = 0;
842 
843 	if (p_arg->num_paths_p != NULL)
844 		*p_arg->num_paths_p = num_path;
845 
846 	if ((dnum) && (d_gids_p))
847 		kmem_free(d_gids_p, dnum * sizeof (ib_gid_t));
848 
849 	if (p_arg->func) {   /* Do these only for Async Get Paths */
850 		ibt_path_info_t *tmp_path_p;
851 
852 		if (retval == IBT_INSUFF_DATA) {
853 			/*
854 			 * We allocated earlier memory based on "max_paths",
855 			 * but we got lesser path-records, so re-adjust that
856 			 * buffer so that caller can free the correct memory.
857 			 */
858 			tmp_path_p = kmem_alloc(
859 			    sizeof (ibt_path_info_t) * num_path, KM_SLEEP);
860 
861 			bcopy(p_arg->paths, tmp_path_p,
862 			    num_path * sizeof (ibt_path_info_t));
863 
864 			kmem_free(p_arg->paths,
865 			    sizeof (ibt_path_info_t) * max_paths);
866 		} else if (retval != IBT_SUCCESS) {
867 			if (p_arg->paths)
868 				kmem_free(p_arg->paths,
869 				    sizeof (ibt_path_info_t) * max_paths);
870 			tmp_path_p = NULL;
871 		} else {
872 			tmp_path_p = p_arg->paths;
873 		}
874 		(*(p_arg->func))(p_arg->arg, retval, tmp_path_p, num_path);
875 	}
876 
877 	len = (sizeof (ib_gid_t) * p_arg->attr.pa_num_dgids) +
878 	    sizeof (ibcm_path_tqargs_t);
879 
880 	if (p_arg && len)
881 		kmem_free(p_arg, len);
882 
883 	IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_paths: done: status %d, "
884 	    "Found %d/%d Path Records", retval, num_path, max_paths);
885 
886 	return (retval);
887 }
888 
889 
890 /*
891  * Perform SA Access to retrieve Path Records.
892  */
893 static ibt_status_t
894 ibcm_saa_path_rec(ibcm_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
895     ibcm_dinfo_t *dinfo, uint8_t *max_count)
896 {
897 	uint8_t		num_path = *max_count;
898 	uint8_t		num_path_plus;
899 	uint8_t		extra, idx, rec_found = 0;
900 	ibt_status_t	retval = IBT_SUCCESS;
901 	int		unicast_dgid_present = 0;
902 	uint8_t		i;
903 
904 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec(%p, %p, %p, 0x%X, %d)",
905 	    p_arg, sl, dinfo, p_arg->flags, *max_count);
906 
907 	if ((dinfo->num_dest == 0) || (num_path == 0) || (sl == NULL)) {
908 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: Invalid Counters");
909 		return (IBT_INVALID_PARAM);
910 	}
911 
912 	/*
913 	 * Of the total needed "X" number of paths to "Y" number of destination
914 	 * we need to get X/Y plus X%Y extra paths to each destination,
915 	 * We do this so that we can choose the required number of path records
916 	 * for the specific destination.
917 	 */
918 	num_path /= dinfo->num_dest;
919 	extra = (*max_count % dinfo->num_dest);
920 
921 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: numpath %d extra %d dest %d",
922 	    num_path, extra, dinfo->num_dest);
923 
924 	/* Find out whether we need to get PathRecord for a MGID as DGID. */
925 	for (idx = 0; idx < dinfo->num_dest; idx++) {
926 		ib_gid_t	dgid = dinfo->dest[idx].d_gid;
927 
928 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: DGID[%d]: %llX:%llX",
929 		    idx, dgid.gid_prefix, dgid.gid_guid);
930 
931 		if ((dgid.gid_prefix >> 56ULL & 0xFF) == 0xFF) {
932 			if (extra)
933 				num_path_plus = num_path + 1;
934 			else
935 				num_path_plus = num_path;
936 
937 			IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: Get %d Paths"
938 			    "- MGID(%016llX%016llX)", num_path_plus,
939 			    dgid.gid_prefix, dgid.gid_guid);
940 
941 			dinfo->dest[idx].d_tag = 1; /* MultiCast */
942 
943 			/* Yes, it's Single PathRec query for MGID as DGID. */
944 			retval = ibcm_get_single_pathrec(p_arg, sl, dinfo, idx,
945 			    &num_path_plus, &p_arg->paths[rec_found]);
946 			if ((retval != IBT_SUCCESS) &&
947 			    (retval != IBT_INSUFF_DATA)) {
948 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: "
949 				    "Failed to get PathRec for MGID %d",
950 				    retval);
951 				continue;
952 			}
953 			if (extra)
954 				extra--;
955 
956 			rec_found += num_path_plus;
957 		}
958 		if (rec_found == *max_count)
959 			break;
960 	}
961 
962 	for (i = 0; i < dinfo->num_dest; i++) {
963 		if (dinfo->dest[i].d_tag == 0) {
964 			unicast_dgid_present++;
965 		}
966 	}
967 
968 	num_path_plus = *max_count - rec_found;
969 
970 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: Recfound: %d, need to find "
971 	    "%d, UniCastGID present %d", rec_found, num_path_plus,
972 	    unicast_dgid_present);
973 
974 	if ((unicast_dgid_present != 0) && (num_path_plus > 0)) {
975 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: MultiSM=%X, #SRC=%d,"
976 		    "Dest%d", sl->p_multi, sl->p_count, unicast_dgid_present);
977 
978 		if ((sl->p_multi != IBTL_CM_SIMPLE_SETUP) ||
979 		    ((unicast_dgid_present == 1) && (sl->p_count == 1))) {
980 			/*
981 			 * Use SinglePathRec if we are dealing w/ MultiSM or
982 			 * request is for one SGID to one DGID.
983 			 */
984 			retval = ibcm_get_single_pathrec(p_arg, sl, dinfo, 0xFF,
985 			    &num_path_plus, &p_arg->paths[rec_found]);
986 		} else {
987 			uint8_t old_num_path_plus = num_path_plus;
988 
989 			/* MultiPathRec will be used for other queries. */
990 			retval = ibcm_get_multi_pathrec(p_arg, sl, dinfo,
991 			    &num_path_plus, &p_arg->paths[rec_found]);
992 			if ((retval != IBT_SUCCESS) &&
993 			    (retval != IBT_INSUFF_DATA) &&
994 			    (sl->p_count > 0) &&
995 			    (dinfo->num_dest > 0)) {
996 				ibtl_cm_port_list_t sl_tmp = *sl;
997 				ibcm_dinfo_t dinfo_tmp = *dinfo;
998 
999 				sl_tmp.p_count = 1;
1000 				dinfo_tmp.num_dest = 1;
1001 				num_path_plus = old_num_path_plus;
1002 				retval = ibcm_get_single_pathrec(p_arg, &sl_tmp,
1003 				    &dinfo_tmp, 0xFF, &num_path_plus,
1004 				    &p_arg->paths[rec_found]);
1005 			}
1006 		}
1007 		if ((retval != IBT_SUCCESS) && (retval != IBT_INSUFF_DATA)) {
1008 			IBTF_DPRINTF_L2(cmlog, "ibcm_saa_path_rec: "
1009 			    "Failed to get PathRec: Status %d", retval);
1010 		} else {
1011 			rec_found += num_path_plus;
1012 		}
1013 	}
1014 
1015 	if (rec_found == 0)  {
1016 		if (retval == IBT_SUCCESS)
1017 			retval = IBT_PATH_RECORDS_NOT_FOUND;
1018 	} else if (rec_found != *max_count)
1019 		retval = IBT_INSUFF_DATA;
1020 	else if (rec_found != 0)
1021 		retval = IBT_SUCCESS;
1022 
1023 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_path_rec: done. Status = %d, "
1024 	    "Found %d/%d Paths", retval, rec_found, *max_count);
1025 
1026 	*max_count = rec_found; /* Update the return count. */
1027 
1028 	return (retval);
1029 }
1030 
1031 ibt_status_t
1032 ibcm_contact_sa_access(ibmf_saa_handle_t saa_handle,
1033     ibmf_saa_access_args_t *access_args, size_t *length, void **results_p)
1034 {
1035 	int	retry;
1036 	int	sa_retval;
1037 
1038 	IBTF_DPRINTF_L3(cmlog, "ibcm_contact_sa_access(%p, %p)",
1039 	    saa_handle, access_args);
1040 
1041 	ibcm_sa_access_enter();
1042 
1043 	for (retry = 0; retry < ibcm_max_sa_retries; retry++) {
1044 		sa_retval = ibmf_sa_access(saa_handle, access_args, 0,
1045 		    length, results_p);
1046 		if (sa_retval != IBMF_TRANS_TIMEOUT)
1047 			break;
1048 
1049 		IBTF_DPRINTF_L2(cmlog, "ibcm_contact_sa_access: "
1050 		    "ibmf_sa_access() - Timed Out (%d)", sa_retval);
1051 		delay(ibcm_sa_timeout_delay);
1052 	}
1053 
1054 	ibcm_sa_access_exit();
1055 
1056 	if ((sa_retval == IBMF_SUCCESS) || (sa_retval == IBMF_NO_RECORDS) ||
1057 	    (sa_retval == IBMF_REQ_INVALID)) {
1058 		IBTF_DPRINTF_L3(cmlog, "ibcm_contact_sa_access: "
1059 		    "ibmf_sa_access() returned (%d)", sa_retval);
1060 		return (IBT_SUCCESS);
1061 	} else  {
1062 		IBTF_DPRINTF_L2(cmlog, "ibcm_contact_sa_access: "
1063 		    "ibmf_sa_access(): Failed (%d)", sa_retval);
1064 		return (ibcm_ibmf_analyze_error(sa_retval));
1065 	}
1066 }
1067 
1068 
1069 static ibt_status_t
1070 ibcm_update_pri(sa_path_record_t *pr_resp, ibtl_cm_port_list_t *sl,
1071     ibcm_dinfo_t *dinfo, ibt_path_info_t *paths)
1072 {
1073 	ibt_status_t	retval = IBT_SUCCESS;
1074 	int		d, s;
1075 
1076 	retval = ibcm_update_cep_info(pr_resp, sl, NULL,
1077 	    &paths->pi_prim_cep_path);
1078 	if (retval != IBT_SUCCESS)
1079 		return (retval);
1080 
1081 	/* Update some leftovers */
1082 	paths->pi_prim_pkt_lt = pr_resp->PacketLifeTime;
1083 	paths->pi_path_mtu = pr_resp->Mtu;
1084 
1085 	for (d = 0; d < dinfo->num_dest; d++) {
1086 		if (pr_resp->DGID.gid_guid == dinfo->dest[d].d_gid.gid_guid) {
1087 			paths->pi_sid = dinfo->dest[d].d_sid;
1088 			if (paths->pi_sid != 0) {
1089 				bcopy(&dinfo->dest[d].d_sdata,
1090 				    &paths->pi_sdata, sizeof (ibt_srv_data_t));
1091 			}
1092 			break;
1093 		}
1094 	}
1095 
1096 	for (s = 0; s < sl->p_count; s++) {
1097 		if (pr_resp->SGID.gid_guid == sl[s].p_sgid.gid_guid) {
1098 			paths->pi_hca_guid = sl[s].p_hca_guid;
1099 		}
1100 	}
1101 
1102 	/* Set Alternate Path to invalid state. */
1103 	paths->pi_alt_cep_path.cep_hca_port_num = 0;
1104 	paths->pi_alt_cep_path.cep_adds_vect.av_dlid = 0;
1105 
1106 	IBTF_DPRINTF_L5(cmlog, "Path: HCA GUID  = 0x%llX", paths->pi_hca_guid);
1107 	IBTF_DPRINTF_L5(cmlog, "Path: ServiceID = 0x%llX", paths->pi_sid);
1108 
1109 	return (retval);
1110 }
1111 
1112 
1113 static ibt_status_t
1114 ibcm_get_single_pathrec(ibcm_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
1115     ibcm_dinfo_t *dinfo, uint8_t idx, uint8_t *num_path, ibt_path_info_t *paths)
1116 {
1117 	sa_path_record_t	pathrec_req;
1118 	sa_path_record_t	*pr_resp;
1119 	ibmf_saa_access_args_t	access_args;
1120 	uint64_t		c_mask = 0;
1121 	void			*results_p;
1122 	uint8_t			num_rec;
1123 	size_t			length;
1124 	ibt_status_t		retval;
1125 	int			i, j, k;
1126 	uint8_t			found, p_fnd;
1127 	ibt_path_attr_t		*attrp = &p_arg->attr;
1128 	ibmf_saa_handle_t	saa_handle;
1129 
1130 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec(%p, %p, %p, %d)",
1131 	    p_arg, sl, dinfo, *num_path);
1132 
1133 	bzero(&pathrec_req, sizeof (sa_path_record_t));
1134 
1135 	/* Is Flow Label Specified. */
1136 	if (attrp->pa_flow) {
1137 		pathrec_req.FlowLabel = attrp->pa_flow;
1138 		c_mask |= SA_PR_COMPMASK_FLOWLABEL;
1139 	}
1140 
1141 	/* Is HopLimit Specified. */
1142 	if (p_arg->flags & IBT_PATH_HOP) {
1143 		pathrec_req.HopLimit = attrp->pa_hop;
1144 		c_mask |= SA_PR_COMPMASK_HOPLIMIT;
1145 	}
1146 
1147 	/* Is P_Key Specified. */
1148 	if (dinfo->p_key) {
1149 		IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec: "
1150 		    "Specified or Global PKEY 0x%X", dinfo->p_key);
1151 		pathrec_req.P_Key = dinfo->p_key;
1152 		c_mask |= SA_PR_COMPMASK_PKEY;
1153 	}
1154 
1155 	/* Is TClass Specified. */
1156 	if (attrp->pa_tclass) {
1157 		pathrec_req.TClass = attrp->pa_tclass;
1158 		c_mask |= SA_PR_COMPMASK_TCLASS;
1159 	}
1160 
1161 	/* Is SL specified. */
1162 	if (attrp->pa_sl) {
1163 		pathrec_req.SL = attrp->pa_sl;
1164 		c_mask |= SA_PR_COMPMASK_SL;
1165 	}
1166 
1167 	/* If IBT_PATH_PERF is set, then mark all selectors to BEST. */
1168 	if (p_arg->flags & IBT_PATH_PERF) {
1169 		pathrec_req.PacketLifeTimeSelector = IBT_BEST;
1170 		pathrec_req.MtuSelector = IBT_BEST;
1171 		pathrec_req.RateSelector = IBT_BEST;
1172 
1173 		c_mask |= SA_PR_COMPMASK_PKTLTSELECTOR |
1174 		    SA_PR_COMPMASK_RATESELECTOR | SA_PR_COMPMASK_MTUSELECTOR;
1175 	} else {
1176 		if (attrp->pa_pkt_lt.p_selector == IBT_BEST) {
1177 			pathrec_req.PacketLifeTimeSelector = IBT_BEST;
1178 			c_mask |= SA_PR_COMPMASK_PKTLTSELECTOR;
1179 		}
1180 
1181 		if (attrp->pa_srate.r_selector == IBT_BEST) {
1182 			pathrec_req.RateSelector = IBT_BEST;
1183 			c_mask |= SA_PR_COMPMASK_RATESELECTOR;
1184 		}
1185 
1186 		if (attrp->pa_mtu.r_selector == IBT_BEST) {
1187 			pathrec_req.MtuSelector = IBT_BEST;
1188 			c_mask |= SA_PR_COMPMASK_MTUSELECTOR;
1189 		}
1190 	}
1191 
1192 	/*
1193 	 * Honor individual selection of these attributes,
1194 	 * even if IBT_PATH_PERF is set.
1195 	 */
1196 	/* Check out whether Packet Life Time is specified. */
1197 	if (attrp->pa_pkt_lt.p_pkt_lt) {
1198 		pathrec_req.PacketLifeTime =
1199 		    ibt_usec2ib(attrp->pa_pkt_lt.p_pkt_lt);
1200 		pathrec_req.PacketLifeTimeSelector =
1201 		    attrp->pa_pkt_lt.p_selector;
1202 
1203 		c_mask |= SA_PR_COMPMASK_PKTLT | SA_PR_COMPMASK_PKTLTSELECTOR;
1204 	}
1205 
1206 	/* Is SRATE specified. */
1207 	if (attrp->pa_srate.r_srate) {
1208 		pathrec_req.Rate = attrp->pa_srate.r_srate;
1209 		pathrec_req.RateSelector = attrp->pa_srate.r_selector;
1210 
1211 		c_mask |= SA_PR_COMPMASK_RATE | SA_PR_COMPMASK_RATESELECTOR;
1212 	}
1213 
1214 	/* Is MTU specified. */
1215 	if (attrp->pa_mtu.r_mtu) {
1216 		pathrec_req.Mtu = attrp->pa_mtu.r_mtu;
1217 		pathrec_req.MtuSelector = attrp->pa_mtu.r_selector;
1218 
1219 		c_mask |= SA_PR_COMPMASK_MTU | SA_PR_COMPMASK_MTUSELECTOR;
1220 	}
1221 
1222 	/* We always get REVERSIBLE paths. */
1223 	pathrec_req.Reversible = 1;
1224 	c_mask |= SA_PR_COMPMASK_REVERSIBLE;
1225 
1226 	pathrec_req.NumbPath = *num_path;
1227 	c_mask |= SA_PR_COMPMASK_NUMBPATH;
1228 
1229 	if (idx != 0xFF) {
1230 		/* MGID */
1231 		pathrec_req.DGID = dinfo->dest[idx].d_gid;
1232 		c_mask |= SA_PR_COMPMASK_DGID;
1233 	}
1234 
1235 	p_fnd = found = 0;
1236 
1237 	for (i = 0; i < sl->p_count; i++) {
1238 		/* SGID */
1239 		pathrec_req.SGID = sl[i].p_sgid;
1240 		c_mask |= SA_PR_COMPMASK_SGID;
1241 		saa_handle = sl[i].p_saa_hdl;
1242 
1243 		for (k = 0; k < dinfo->num_dest; k++) {
1244 			if (idx == 0xFF) {		/* DGID */
1245 				if (dinfo->dest[k].d_tag != 0)
1246 					continue;
1247 
1248 				if (pathrec_req.SGID.gid_prefix !=
1249 				    dinfo->dest[k].d_gid.gid_prefix) {
1250 					IBTF_DPRINTF_L3(cmlog,
1251 					    "ibcm_get_single_pathrec: SGID_pfx="
1252 					    "%llX, DGID_pfx=%llX doesn't match",
1253 					    pathrec_req.SGID.gid_prefix,
1254 					    dinfo->dest[k].d_gid.gid_prefix);
1255 					continue;
1256 				}
1257 
1258 				pathrec_req.DGID = dinfo->dest[k].d_gid;
1259 				c_mask |= SA_PR_COMPMASK_DGID;
1260 
1261 				/*
1262 				 * If we had performed Service Look-up, then we
1263 				 * got P_Key from ServiceRecord, so get path
1264 				 * records that satisfy this particular P_Key.
1265 				 */
1266 				if ((dinfo->p_key == 0) &&
1267 				    (dinfo->dest[k].d_pkey != 0)) {
1268 					pathrec_req.P_Key =
1269 					    dinfo->dest[k].d_pkey;
1270 					c_mask |= SA_PR_COMPMASK_PKEY;
1271 				}
1272 			}
1273 
1274 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec: "
1275 			    "Get %d Path(s) between\nSGID %llX:%llX "
1276 			    "DGID %llX:%llX", pathrec_req.NumbPath,
1277 			    pathrec_req.SGID.gid_prefix,
1278 			    pathrec_req.SGID.gid_guid,
1279 			    pathrec_req.DGID.gid_prefix,
1280 			    pathrec_req.DGID.gid_guid);
1281 
1282 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec: CMask"
1283 			    "=0x%llX, PKey=0x%X", c_mask, pathrec_req.P_Key);
1284 
1285 			/* Contact SA Access to retrieve Path Records. */
1286 			access_args.sq_attr_id = SA_PATHRECORD_ATTRID;
1287 			access_args.sq_template = &pathrec_req;
1288 			access_args.sq_access_type = IBMF_SAA_RETRIEVE;
1289 			access_args.sq_template_length =
1290 			    sizeof (sa_path_record_t);
1291 			access_args.sq_component_mask = c_mask;
1292 			access_args.sq_callback = NULL;
1293 			access_args.sq_callback_arg = NULL;
1294 
1295 			retval = ibcm_contact_sa_access(saa_handle,
1296 			    &access_args, &length, &results_p);
1297 			if (retval != IBT_SUCCESS) {
1298 				*num_path = 0;
1299 				return (retval);
1300 			}
1301 
1302 			num_rec = length / sizeof (sa_path_record_t);
1303 
1304 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec: "
1305 			    "FOUND %d/%d path requested", num_rec, *num_path);
1306 
1307 			if ((results_p == NULL) || (num_rec == 0)) {
1308 				if (idx != 0xFF)
1309 					break;
1310 				else
1311 					continue;
1312 			}
1313 
1314 			/* Update the PathInfo from the response. */
1315 			pr_resp = (sa_path_record_t *)results_p;
1316 			for (j = 0; j < num_rec; j++, pr_resp++) {
1317 				if ((p_fnd != 0) &&
1318 				    (p_arg->flags & IBT_PATH_APM)) {
1319 					IBTF_DPRINTF_L3(cmlog,
1320 					    "ibcm_get_single_pathrec: "
1321 					    "Fill Alternate Path");
1322 					retval = ibcm_update_cep_info(pr_resp,
1323 					    sl, NULL,
1324 					    &paths[found - 1].pi_alt_cep_path);
1325 					if (retval != IBT_SUCCESS)
1326 						continue;
1327 
1328 					/* Update some leftovers */
1329 					paths[found - 1].pi_alt_pkt_lt =
1330 					    pr_resp->PacketLifeTime;
1331 					p_fnd = 0;
1332 				} else {
1333 					IBTF_DPRINTF_L3(cmlog,
1334 					    "ibcm_get_single_pathrec: "
1335 					    "Fill Primary Path");
1336 
1337 					if (found == *num_path)
1338 						break;
1339 
1340 					retval = ibcm_update_pri(pr_resp, sl,
1341 					    dinfo, &paths[found]);
1342 					if (retval != IBT_SUCCESS)
1343 						continue;
1344 					p_fnd = 1;
1345 					found++;
1346 				}
1347 
1348 			}
1349 			/* Deallocate the memory for results_p. */
1350 			kmem_free(results_p, length);
1351 
1352 			if (idx != 0xFF)
1353 				break;		/* We r here for MGID */
1354 		}
1355 		if ((idx != 0xFF) && (found == *num_path))
1356 			break;		/* We r here for MGID */
1357 	}
1358 
1359 	if (found == 0)
1360 		retval = IBT_PATH_RECORDS_NOT_FOUND;
1361 	else if (found != *num_path)
1362 		retval = IBT_INSUFF_DATA;
1363 	else
1364 		retval = IBT_SUCCESS;
1365 
1366 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_single_pathrec: done. Status %d, "
1367 	    "Found %d/%d Paths", retval, found, *num_path);
1368 
1369 	*num_path = found;
1370 
1371 	return (retval);
1372 }
1373 
1374 
1375 static ibt_status_t
1376 ibcm_get_multi_pathrec(ibcm_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
1377     ibcm_dinfo_t *dinfo, uint8_t *num_path, ibt_path_info_t *paths)
1378 {
1379 	sa_multipath_record_t	*mpr_req;
1380 	sa_path_record_t	*pr_resp;
1381 	ibmf_saa_access_args_t	access_args;
1382 	void			*results_p;
1383 	uint64_t		c_mask = 0;
1384 	ib_gid_t		*gid_ptr, *gid_s_ptr;
1385 	size_t			length;
1386 	int			template_len;
1387 	uint8_t			found, num_rec;
1388 	int			i, k;
1389 	ibt_status_t		retval;
1390 	uint8_t			sgid_cnt, dgid_cnt;
1391 	ibt_path_attr_t		*attrp = &p_arg->attr;
1392 
1393 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec(%p, %p, %p, %d)",
1394 	    attrp, sl, dinfo, *num_path);
1395 
1396 	for (i = 0, dgid_cnt = 0; i < dinfo->num_dest; i++) {
1397 		if (dinfo->dest[i].d_tag == 0)
1398 			dgid_cnt++;
1399 	}
1400 
1401 	sgid_cnt = sl->p_count;
1402 
1403 	if ((sgid_cnt == 0) || (dgid_cnt == 0)) {
1404 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_multi_pathrec: sgid_cnt(%d) or"
1405 		    " dgid_cnt(%d) is zero", sgid_cnt, dgid_cnt);
1406 		return (IBT_INVALID_PARAM);
1407 	}
1408 
1409 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: Get %d records between "
1410 	    "%d Src(s) <=> %d Dest(s)", *num_path, sgid_cnt, dgid_cnt);
1411 
1412 	/*
1413 	 * Calculate the size for multi-path records template, which includes
1414 	 * constant portion of the multipath record, plus variable size for
1415 	 * SGID (sgid_cnt) and DGID (dgid_cnt).
1416 	 */
1417 	template_len = ((dgid_cnt + sgid_cnt) * sizeof (ib_gid_t)) +
1418 	    sizeof (sa_multipath_record_t);
1419 
1420 	mpr_req = kmem_zalloc(template_len, KM_SLEEP);
1421 
1422 	ASSERT(mpr_req != NULL);
1423 
1424 	gid_ptr = (ib_gid_t *)(((uchar_t *)mpr_req) +
1425 	    sizeof (sa_multipath_record_t));
1426 
1427 	/* Get the starting pointer where GIDs are stored. */
1428 	gid_s_ptr = gid_ptr;
1429 
1430 	/* SGID */
1431 	for (i = 0; i < sgid_cnt; i++) {
1432 		*gid_ptr = sl[i].p_sgid;
1433 
1434 		IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: SGID[%d] = "
1435 		    "(%llX:%llX)", i, gid_ptr->gid_prefix, gid_ptr->gid_guid);
1436 
1437 		gid_ptr++;
1438 	}
1439 
1440 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mpr_req))
1441 
1442 	mpr_req->SGIDCount = sgid_cnt;
1443 	c_mask = SA_MPR_COMPMASK_SGIDCOUNT;
1444 
1445 	/* DGIDs */
1446 	for (i = 0; i < dinfo->num_dest; i++) {
1447 		if (dinfo->dest[i].d_tag == 0) {
1448 			*gid_ptr = dinfo->dest[i].d_gid;
1449 
1450 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: "
1451 			    "DGID[%d] = (%llX:%llX)", i, gid_ptr->gid_prefix,
1452 			    gid_ptr->gid_guid);
1453 			gid_ptr++;
1454 		}
1455 	}
1456 
1457 	mpr_req->DGIDCount = dgid_cnt;
1458 	c_mask |= SA_MPR_COMPMASK_DGIDCOUNT;
1459 
1460 	/* Is Flow Label Specified. */
1461 	if (attrp->pa_flow) {
1462 		mpr_req->FlowLabel = attrp->pa_flow;
1463 		c_mask |= SA_MPR_COMPMASK_FLOWLABEL;
1464 	}
1465 
1466 	/* Is HopLimit Specified. */
1467 	if (p_arg->flags & IBT_PATH_HOP) {
1468 		mpr_req->HopLimit = attrp->pa_hop;
1469 		c_mask |= SA_MPR_COMPMASK_HOPLIMIT;
1470 	}
1471 
1472 	/* Is TClass Specified. */
1473 	if (attrp->pa_tclass) {
1474 		mpr_req->TClass = attrp->pa_tclass;
1475 		c_mask |= SA_MPR_COMPMASK_TCLASS;
1476 	}
1477 
1478 	/* Is SL specified. */
1479 	if (attrp->pa_sl) {
1480 		mpr_req->SL = attrp->pa_sl;
1481 		c_mask |= SA_MPR_COMPMASK_SL;
1482 	}
1483 
1484 	if (p_arg->flags & IBT_PATH_PERF) {
1485 		mpr_req->PacketLifeTimeSelector = IBT_BEST;
1486 		mpr_req->RateSelector = IBT_BEST;
1487 		mpr_req->MtuSelector = IBT_BEST;
1488 
1489 		c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR |
1490 		    SA_MPR_COMPMASK_RATESELECTOR | SA_MPR_COMPMASK_MTUSELECTOR;
1491 	} else {
1492 		if (attrp->pa_pkt_lt.p_selector == IBT_BEST) {
1493 			mpr_req->PacketLifeTimeSelector = IBT_BEST;
1494 			c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR;
1495 		}
1496 
1497 		if (attrp->pa_srate.r_selector == IBT_BEST) {
1498 			mpr_req->RateSelector = IBT_BEST;
1499 			c_mask |= SA_MPR_COMPMASK_RATESELECTOR;
1500 		}
1501 
1502 		if (attrp->pa_mtu.r_selector == IBT_BEST) {
1503 			mpr_req->MtuSelector = IBT_BEST;
1504 			c_mask |= SA_MPR_COMPMASK_MTUSELECTOR;
1505 		}
1506 	}
1507 
1508 	/*
1509 	 * Honor individual selection of these attributes,
1510 	 * even if IBT_PATH_PERF is set.
1511 	 */
1512 	/* Check out whether Packet Life Time is specified. */
1513 	if (attrp->pa_pkt_lt.p_pkt_lt) {
1514 		mpr_req->PacketLifeTime =
1515 		    ibt_usec2ib(attrp->pa_pkt_lt.p_pkt_lt);
1516 		mpr_req->PacketLifeTimeSelector =
1517 		    attrp->pa_pkt_lt.p_selector;
1518 
1519 		c_mask |= SA_MPR_COMPMASK_PKTLT |
1520 		    SA_MPR_COMPMASK_PKTLTSELECTOR;
1521 	}
1522 
1523 	/* Is SRATE specified. */
1524 	if (attrp->pa_srate.r_srate) {
1525 		mpr_req->Rate = attrp->pa_srate.r_srate;
1526 		mpr_req->RateSelector = attrp->pa_srate.r_selector;
1527 
1528 		c_mask |= SA_MPR_COMPMASK_RATE |
1529 		    SA_MPR_COMPMASK_RATESELECTOR;
1530 	}
1531 
1532 	/* Is MTU specified. */
1533 	if (attrp->pa_mtu.r_mtu) {
1534 		mpr_req->Mtu = attrp->pa_mtu.r_mtu;
1535 		mpr_req->MtuSelector = attrp->pa_mtu.r_selector;
1536 
1537 		c_mask |= SA_MPR_COMPMASK_MTU |
1538 		    SA_MPR_COMPMASK_MTUSELECTOR;
1539 	}
1540 
1541 	/* Is P_Key Specified or obtained during Service Look-up. */
1542 	if (dinfo->p_key) {
1543 		mpr_req->P_Key = dinfo->p_key;
1544 		c_mask |= SA_MPR_COMPMASK_PKEY;
1545 	}
1546 
1547 	/* We always get REVERSIBLE paths. */
1548 	mpr_req->Reversible = 1;
1549 	c_mask |= SA_MPR_COMPMASK_REVERSIBLE;
1550 
1551 	if (p_arg->flags & IBT_PATH_AVAIL) {
1552 		mpr_req->IndependenceSelector = 1;
1553 		c_mask |= SA_MPR_COMPMASK_INDEPSEL;
1554 	}
1555 
1556 	/* we will not specify how many records we want. */
1557 
1558 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mpr_req))
1559 
1560 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: CMask: %llX Pkey: %X",
1561 	    c_mask, mpr_req->P_Key);
1562 
1563 	/* Contact SA Access to retrieve Path Records. */
1564 	access_args.sq_attr_id = SA_MULTIPATHRECORD_ATTRID;
1565 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
1566 	access_args.sq_component_mask = c_mask;
1567 	access_args.sq_template = mpr_req;
1568 	access_args.sq_template_length = sizeof (sa_multipath_record_t);
1569 	access_args.sq_callback = NULL;
1570 	access_args.sq_callback_arg = NULL;
1571 
1572 	retval = ibcm_contact_sa_access(sl->p_saa_hdl, &access_args, &length,
1573 	    &results_p);
1574 	if (retval != IBT_SUCCESS) {
1575 		*num_path = 0;  /* Update the return count. */
1576 		kmem_free(mpr_req, template_len);
1577 		return (retval);
1578 	}
1579 
1580 	num_rec = length / sizeof (sa_path_record_t);
1581 
1582 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: Found %d Paths",
1583 	    num_rec);
1584 
1585 	found = 0;
1586 	if ((results_p != NULL) && (num_rec > 0)) {
1587 		/* Update the PathInfo with the response Path Records */
1588 		pr_resp = (sa_path_record_t *)results_p;
1589 
1590 		for (i = 0; i < num_rec; i++) {
1591 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: "
1592 			    "P[%d]: SG %llX, DG %llX", i,
1593 			    pr_resp[i].SGID.gid_guid, pr_resp[i].DGID.gid_guid);
1594 		}
1595 
1596 		if (p_arg->flags & (IBT_PATH_APM | IBT_PATH_AVAIL)) {
1597 			sa_path_record_t *p_resp = NULL, *a_resp = NULL;
1598 			sa_path_record_t *p_tmp = NULL, *a_tmp = NULL;
1599 			int		p_found = 0, a_found = 0;
1600 			ib_gid_t	p_sg, a_sg, p_dg, a_dg;
1601 			int		p_tmp_found = 0, a_tmp_found = 0;
1602 
1603 			p_sg = gid_s_ptr[0];
1604 			if (sgid_cnt > 1)
1605 				a_sg = gid_s_ptr[1];
1606 			else
1607 				a_sg = p_sg;
1608 
1609 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: "
1610 			    "REQ: P_SG: %llX, A_SG: %llX",
1611 			    p_sg.gid_guid, a_sg.gid_guid);
1612 
1613 			p_dg = gid_s_ptr[sgid_cnt];
1614 			if (dgid_cnt > 1)
1615 				a_dg = gid_s_ptr[sgid_cnt + 1];
1616 			else
1617 				a_dg = p_dg;
1618 
1619 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: "
1620 			    "REQ: P_DG: %llX, A_DG: %llX",
1621 			    p_dg.gid_guid, a_dg.gid_guid);
1622 
1623 			/*
1624 			 * If SGID and/or DGID is specified by user, make sure
1625 			 * he gets his primary-path on those node points.
1626 			 */
1627 			for (i = 0; i < num_rec; i++, pr_resp++) {
1628 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec:"
1629 				    " PF %d, AF %d,\n\t\t P[%d] = SG: %llX, "
1630 				    "DG: %llX", p_found, a_found, i,
1631 				    pr_resp->SGID.gid_guid,
1632 				    pr_resp->DGID.gid_guid);
1633 
1634 				if ((!p_found) &&
1635 				    (p_dg.gid_guid == pr_resp->DGID.gid_guid)) {
1636 					IBTF_DPRINTF_L3(cmlog,
1637 					    "ibcm_get_multi_pathrec: "
1638 					    "Pri DGID Match.. ");
1639 					if (p_sg.gid_guid ==
1640 					    pr_resp->SGID.gid_guid) {
1641 						p_found = 1;
1642 						p_resp = pr_resp;
1643 						IBTF_DPRINTF_L3(cmlog,
1644 						    "ibcm_get_multi_pathrec: "
1645 						    "Primary Path Found");
1646 
1647 						if (a_found)
1648 							break;
1649 						else
1650 							continue;
1651 					} else if ((!p_tmp_found) &&
1652 					    (a_sg.gid_guid ==
1653 					    pr_resp->SGID.gid_guid)) {
1654 						p_tmp_found = 1;
1655 						p_tmp = pr_resp;
1656 						IBTF_DPRINTF_L3(cmlog,
1657 						    "ibcm_get_multi_pathrec: "
1658 						    "Tmp Pri Path Found");
1659 					}
1660 					IBTF_DPRINTF_L3(cmlog,
1661 					    "ibcm_get_multi_pathrec:"
1662 					    "Pri SGID Don't Match.. ");
1663 				}
1664 
1665 				if ((!a_found) &&
1666 				    (a_dg.gid_guid == pr_resp->DGID.gid_guid)) {
1667 					IBTF_DPRINTF_L3(cmlog,
1668 					    "ibcm_get_multi_pathrec:"
1669 					    "Alt DGID Match.. ");
1670 					if (a_sg.gid_guid ==
1671 					    pr_resp->SGID.gid_guid) {
1672 						a_found = 1;
1673 						a_resp = pr_resp;
1674 
1675 						IBTF_DPRINTF_L3(cmlog,
1676 						    "ibcm_get_multi_pathrec:"
1677 						    "Alternate Path Found ");
1678 
1679 						if (p_found)
1680 							break;
1681 						else
1682 							continue;
1683 					} else if ((!a_tmp_found) &&
1684 					    (p_sg.gid_guid ==
1685 					    pr_resp->SGID.gid_guid)) {
1686 						a_tmp_found = 1;
1687 						a_tmp = pr_resp;
1688 
1689 						IBTF_DPRINTF_L3(cmlog,
1690 						    "ibcm_get_multi_pathrec:"
1691 						    "Tmp Alt Path Found ");
1692 					}
1693 					IBTF_DPRINTF_L3(cmlog,
1694 					    "ibcm_get_multi_pathrec:"
1695 					    "Alt SGID Don't Match.. ");
1696 				}
1697 			}
1698 
1699 			if ((p_found == 0) && (a_found == 0) &&
1700 			    (p_tmp_found == 0) && (a_tmp_found == 0)) {
1701 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec:"
1702 				    " Path to desired node points NOT "
1703 				    "Available.");
1704 				retval = IBT_PATH_RECORDS_NOT_FOUND;
1705 				goto get_mpr_end;
1706 			}
1707 
1708 			if (p_resp == NULL) {
1709 				if (a_resp != NULL) {
1710 					p_resp = a_resp;
1711 					a_resp = NULL;
1712 				} else if (p_tmp != NULL) {
1713 					p_resp = p_tmp;
1714 					p_tmp = NULL;
1715 				} else if (a_tmp != NULL) {
1716 					p_resp = a_tmp;
1717 					a_tmp = NULL;
1718 				}
1719 			}
1720 			if (a_resp == NULL) {
1721 				if (a_tmp != NULL) {
1722 					a_resp = a_tmp;
1723 					a_tmp = NULL;
1724 				} else if (p_tmp != NULL) {
1725 					a_resp = p_tmp;
1726 					p_tmp = NULL;
1727 				}
1728 			}
1729 
1730 			/* Fill in Primary Path */
1731 			retval = ibcm_update_pri(p_resp, sl, dinfo,
1732 			    &paths[found]);
1733 			if (retval != IBT_SUCCESS)
1734 				goto get_mpr_end;
1735 
1736 			if (p_arg->flags & IBT_PATH_APM) {
1737 				/* Fill in Alternate Path */
1738 				if (a_resp != NULL) {
1739 					/*
1740 					 * a_resp will point to AltPathInfo
1741 					 * buffer.
1742 					 */
1743 					retval = ibcm_update_cep_info(a_resp,
1744 					    sl, NULL,
1745 					    &paths[found].pi_alt_cep_path);
1746 					if (retval != IBT_SUCCESS)
1747 						goto get_mpr_end;
1748 
1749 					/* Update some leftovers */
1750 					paths[found].pi_alt_pkt_lt =
1751 					    a_resp->PacketLifeTime;
1752 				} else {
1753 					IBTF_DPRINTF_L3(cmlog,
1754 					    "ibcm_get_multi_pathrec:"
1755 					    " Alternate Path NOT Available.");
1756 					retval = IBT_INSUFF_DATA;
1757 				}
1758 				found++;
1759 			} else if (p_arg->flags & IBT_PATH_AVAIL) {
1760 				found++;
1761 
1762 				if (found < *num_path) {
1763 
1764 					/* Fill in second Path */
1765 					if (a_resp != NULL) {
1766 						retval = ibcm_update_pri(a_resp,
1767 						    sl, dinfo, &paths[found]);
1768 						if (retval != IBT_SUCCESS)
1769 							goto get_mpr_end;
1770 						else
1771 							found++;
1772 					} else {
1773 						IBTF_DPRINTF_L3(cmlog,
1774 						    "ibcm_get_multi_pathrec: "
1775 						    "SecondPath NOT Available");
1776 						retval = IBT_INSUFF_DATA;
1777 					}
1778 				}
1779 			}
1780 		} else {	/* If NOT APM */
1781 			boolean_t	check_pkey = B_FALSE;
1782 
1783 			/* mark flag whether to validate PKey or not. */
1784 			if ((dinfo->p_key == 0) && (dinfo->dest[0].d_pkey != 0))
1785 				check_pkey = B_TRUE;
1786 
1787 			for (i = 0; i < num_rec; i++, pr_resp++) {
1788 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec:"
1789 				    " PKeyCheck - %s, PKey=0x%X, DGID(%llX)",
1790 				    ((check_pkey == B_TRUE)?"REQD":"NOT_REQD"),
1791 				    pr_resp->P_Key, pr_resp->DGID.gid_guid);
1792 
1793 				if (check_pkey) {
1794 					boolean_t	match_found = B_FALSE;
1795 
1796 					/* For all DGIDs */
1797 					for (k = 0; k < dinfo->num_dest; k++) {
1798 						if (dinfo->dest[k].d_tag != 0)
1799 							continue;
1800 
1801 						if ((dinfo->dest[k].d_gid.
1802 						    gid_guid ==
1803 						    pr_resp->DGID.gid_guid) &&
1804 						    (dinfo->dest[k].d_pkey ==
1805 						    pr_resp->P_Key)) {
1806 							match_found = B_TRUE;
1807 							break;
1808 						}
1809 					}
1810 					if (!match_found)
1811 						continue;
1812 				}
1813 				/* Fill in Primary Path */
1814 				retval = ibcm_update_pri(pr_resp, sl, dinfo,
1815 				    &paths[found]);
1816 				if (retval != IBT_SUCCESS)
1817 					continue;
1818 
1819 				if (++found == *num_path)
1820 					break;
1821 			}
1822 		}
1823 get_mpr_end:
1824 		kmem_free(results_p, length);
1825 	}
1826 	kmem_free(mpr_req, template_len);
1827 
1828 	if (found == 0)
1829 		retval = IBT_PATH_RECORDS_NOT_FOUND;
1830 	else if (found != *num_path)
1831 		retval = IBT_INSUFF_DATA;
1832 	else
1833 		retval = IBT_SUCCESS;
1834 
1835 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_multi_pathrec: Done (status %d). "
1836 	    "Found %d/%d Paths", retval, found, *num_path);
1837 
1838 	*num_path = found;	/* Update the return count. */
1839 
1840 	return (retval);
1841 }
1842 
1843 
1844 /*
1845  * Update the output path records buffer with the values as obtained from
1846  * SA Access retrieve call results for Path Records.
1847  */
1848 static ibt_status_t
1849 ibcm_update_cep_info(sa_path_record_t *prec_resp, ibtl_cm_port_list_t *sl,
1850     ibtl_cm_hca_port_t *hport, ibt_cep_path_t *cep_p)
1851 {
1852 	ibt_status_t	retval;
1853 	int		i;
1854 
1855 	IBCM_DUMP_PATH_REC(prec_resp);
1856 
1857 	/*
1858 	 * If path's packet life time is more than 4 seconds, IBCM cannot
1859 	 * handle this path connection, so discard this path record.
1860 	 */
1861 	if (prec_resp->PacketLifeTime > ibcm_max_ib_pkt_lt) {
1862 		IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: Path's Packet "
1863 		    "LifeTime too high %d, Maximum allowed %d IB Time (4 sec)",
1864 		    prec_resp->PacketLifeTime, ibcm_max_ib_pkt_lt);
1865 		return (ibt_get_module_failure(IBT_FAILURE_IBSM, 0));
1866 	}
1867 
1868 	if ((prec_resp->Mtu > IB_MTU_4K) || (prec_resp->Mtu < IB_MTU_256)) {
1869 		IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: MTU (%d) from "
1870 		    "pathrecord is invalid, reject it.", prec_resp->Mtu);
1871 		return (ibt_get_module_failure(IBT_FAILURE_IBSM, 0));
1872 	}
1873 
1874 	/* Source Node Information. */
1875 	cep_p->cep_adds_vect.av_sgid = prec_resp->SGID;
1876 	if (hport != NULL) {
1877 		/* Convert P_Key to P_Key_Index */
1878 		retval = ibt_pkey2index_byguid(hport->hp_hca_guid,
1879 		    hport->hp_port, prec_resp->P_Key, &cep_p->cep_pkey_ix);
1880 		if (retval != IBT_SUCCESS) {
1881 			/* Failed to get pkey_index from pkey */
1882 			IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: "
1883 			    "Pkey2Index (PKey = %X) conversion failed: %d",
1884 			    prec_resp->P_Key, retval);
1885 			return (ibt_get_module_failure(IBT_FAILURE_IBSM, 0));
1886 		}
1887 		cep_p->cep_adds_vect.av_sgid_ix = hport->hp_sgid_ix;
1888 		cep_p->cep_adds_vect.av_src_path =
1889 		    prec_resp->SLID - hport->hp_base_lid;
1890 		cep_p->cep_adds_vect.av_port_num = cep_p->cep_hca_port_num =
1891 		    hport->hp_port;
1892 	} else if (sl != NULL) {
1893 		for (i = 0; i < sl->p_count; i++) {
1894 			if (prec_resp->SGID.gid_guid == sl[i].p_sgid.gid_guid) {
1895 				/* Convert P_Key to P_Key_Index */
1896 				retval = ibt_pkey2index_byguid(sl[i].p_hca_guid,
1897 				    sl[i].p_port_num, prec_resp->P_Key,
1898 				    &cep_p->cep_pkey_ix);
1899 				if (retval != IBT_SUCCESS) {
1900 					/* Failed to get pkey_index from pkey */
1901 					IBTF_DPRINTF_L2(cmlog,
1902 					    "ibcm_update_cep_info: Pkey2Index "
1903 					    "(PKey = %X) conversion failed: %d",
1904 					    prec_resp->P_Key, retval);
1905 					return (ibt_get_module_failure(
1906 					    IBT_FAILURE_IBSM, 0));
1907 				}
1908 
1909 				cep_p->cep_adds_vect.av_sgid_ix =
1910 				    sl[i].p_sgid_ix;
1911 				cep_p->cep_adds_vect.av_src_path =
1912 				    prec_resp->SLID - sl[i].p_base_lid;
1913 				cep_p->cep_adds_vect.av_port_num =
1914 				    sl[i].p_port_num;
1915 				cep_p->cep_hca_port_num = sl[i].p_port_num;
1916 
1917 				break;
1918 			}
1919 		}
1920 	} else {
1921 		IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: Sl or Hport "
1922 		    "must be non-null");
1923 		return (IBT_INVALID_PARAM);
1924 	}
1925 
1926 	if (prec_resp->Rate) {
1927 		cep_p->cep_adds_vect.av_srate = prec_resp->Rate;
1928 	} else {
1929 		IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: SRate (%d) from "
1930 		    "pathrecord is invalid, reject it.", prec_resp->Rate);
1931 		return (ibt_get_module_failure(IBT_FAILURE_IBSM, 0));
1932 	}
1933 	/*
1934 	 * If both Source and Destination GID prefix are same, then GRH is not
1935 	 * valid, so make it as false, else set this field as true.
1936 	 */
1937 	if (prec_resp->SGID.gid_prefix == prec_resp->DGID.gid_prefix)
1938 		cep_p->cep_adds_vect.av_send_grh = B_FALSE;
1939 	else
1940 		cep_p->cep_adds_vect.av_send_grh = B_TRUE;
1941 
1942 	/* SGID and SGID Index. */
1943 	cep_p->cep_adds_vect.av_sgid = prec_resp->SGID;
1944 	cep_p->cep_adds_vect.av_flow = prec_resp->FlowLabel;
1945 	cep_p->cep_adds_vect.av_tclass = prec_resp->TClass;
1946 	cep_p->cep_adds_vect.av_hop = prec_resp->HopLimit;
1947 
1948 	/* Address Vector Definition. */
1949 	cep_p->cep_adds_vect.av_dlid = prec_resp->DLID;
1950 	cep_p->cep_adds_vect.av_srvl = prec_resp->SL;
1951 
1952 	/* DGID */
1953 	cep_p->cep_adds_vect.av_dgid = prec_resp->DGID;
1954 
1955 	/* CEP Timeout is NOT filled in by PATH routines. */
1956 	cep_p->cep_timeout = 0;
1957 
1958 	IBTF_DPRINTF_L2(cmlog, "ibcm_update_cep_info: Done. Port=%d, PKey=%X\n"
1959 	    "SGID=%llX:%llX DGID=%llX:%llX", cep_p->cep_adds_vect.av_port_num,
1960 	    prec_resp->P_Key,
1961 	    prec_resp->SGID.gid_prefix, prec_resp->SGID.gid_guid,
1962 	    prec_resp->DGID.gid_prefix, prec_resp->DGID.gid_guid);
1963 
1964 	return (IBT_SUCCESS);
1965 }
1966 
1967 
1968 static void
1969 ibcm_fill_svcinfo(sa_service_record_t *sr_resp, ibcm_dest_t *dest)
1970 {
1971 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*dest))
1972 
1973 	dest->d_gid = sr_resp->ServiceGID;
1974 	dest->d_sid = sr_resp->ServiceID;
1975 	ibcm_swizzle_to_srv(sr_resp->ServiceData, &dest->d_sdata);
1976 	dest->d_pkey = sr_resp->ServiceP_Key;
1977 
1978 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*dest))
1979 
1980 	IBTF_DPRINTF_L3(cmlog, "ibcm_fill_svcinfo: SID(%llX), GID(%llX:%llX)"
1981 	    "\n\tSvcPKey 0x%X", dest->d_sid, dest->d_gid.gid_prefix,
1982 	    dest->d_gid.gid_guid, dest->d_pkey);
1983 }
1984 
1985 
1986 static ib_gid_t
1987 ibcm_saa_get_agid(ibtl_cm_port_list_t *sl, ib_gid_t *gidp, uint_t ngid)
1988 {
1989 	int		k, l;
1990 	ib_gid_t	a_gid;
1991 
1992 	a_gid.gid_prefix = a_gid.gid_guid = 0;
1993 
1994 	for (k = 0; k < sl->p_count; k++) {
1995 		for (l = 0; l < ngid; l++) {
1996 
1997 			if (gidp->gid_prefix == sl->p_sgid.gid_prefix) {
1998 				a_gid = *gidp;
1999 				break;
2000 			}
2001 			if (a_gid.gid_guid && a_gid.gid_prefix)
2002 				break;
2003 			gidp++;
2004 		}
2005 		if (a_gid.gid_guid && a_gid.gid_prefix)
2006 			break;
2007 		sl++;
2008 	}
2009 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_get_agid: AltGID = %llX:%llX",
2010 	    a_gid.gid_prefix, a_gid.gid_guid);
2011 
2012 	return (a_gid);
2013 }
2014 
2015 /*
2016  * Perform SA Access to retrieve Service Records.
2017  * On Success, returns ServiceID and ServiceGID info in '*dinfo'.
2018  */
2019 static ibt_status_t
2020 ibcm_saa_service_rec(ibcm_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
2021     ibcm_dinfo_t *dinfo)
2022 {
2023 	sa_service_record_t	svcrec_req;
2024 	sa_service_record_t	*svcrec_resp;
2025 	void			*results_p;
2026 	uint64_t		component_mask = 0;
2027 	size_t			length;
2028 	uint8_t			i, j, k, rec_found, s;
2029 	ibmf_saa_access_args_t	access_args;
2030 	ibt_status_t		retval;
2031 	ibt_path_attr_t		*attrp = &p_arg->attr;
2032 	uint64_t		tmp_sd_flag = attrp->pa_sd_flags;
2033 	uint8_t			num_req;
2034 
2035 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec(%p, %p)", p_arg, sl);
2036 
2037 	bzero(&svcrec_req, sizeof (svcrec_req));
2038 
2039 	/* Service Name */
2040 	if ((attrp->pa_sname != NULL) && (strlen(attrp->pa_sname) != 0)) {
2041 		(void) strncpy((char *)(svcrec_req.ServiceName),
2042 		    attrp->pa_sname, IB_SVC_NAME_LEN);
2043 
2044 		component_mask |= SA_SR_COMPMASK_NAME;
2045 	}
2046 
2047 	/* Service ID */
2048 	if (attrp->pa_sid) {
2049 		svcrec_req.ServiceID = attrp->pa_sid;
2050 		component_mask |= SA_SR_COMPMASK_ID;
2051 	}
2052 
2053 	/* Is P_Key Specified. */
2054 	if (p_arg->flags & IBT_PATH_PKEY) {
2055 		svcrec_req.ServiceP_Key = attrp->pa_pkey;
2056 		component_mask |= SA_SR_COMPMASK_PKEY;
2057 	}
2058 
2059 	/* Is ServiceData Specified. */
2060 	if (attrp->pa_sd_flags != IBT_NO_SDATA) {
2061 		/* Handle endianess for service data. */
2062 		ibcm_swizzle_from_srv(&attrp->pa_sdata, svcrec_req.ServiceData);
2063 
2064 		/*
2065 		 * Lets not interpret each and every ServiceData flags,
2066 		 * just pass it on to SAA. Shift the flag, to suit
2067 		 * SA_SR_COMPMASK_ALL_DATA definition.
2068 		 */
2069 		component_mask |= (tmp_sd_flag << 7);
2070 	}
2071 
2072 	if (dinfo->num_dest == 1) {
2073 
2074 		/* If a single DGID is specified, provide it */
2075 		svcrec_req.ServiceGID = dinfo->dest->d_gid;
2076 		component_mask |= SA_SR_COMPMASK_GID;
2077 
2078 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec:%llX:%llX",
2079 		    svcrec_req.ServiceGID.gid_prefix,
2080 		    svcrec_req.ServiceGID.gid_guid);
2081 	}
2082 
2083 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2084 	    "Perform SA Access: Mask: 0x%X", component_mask);
2085 
2086 	/*
2087 	 * Call in SA Access retrieve routine to get Service Records.
2088 	 *
2089 	 * SA Access framework allocated memory for the "results_p".
2090 	 * Make sure to deallocate once we are done with the results_p.
2091 	 * The size of the buffer allocated will be as returned in
2092 	 * "length" field.
2093 	 */
2094 	access_args.sq_attr_id = SA_SERVICERECORD_ATTRID;
2095 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
2096 	access_args.sq_component_mask = component_mask;
2097 	access_args.sq_template = &svcrec_req;
2098 	access_args.sq_template_length = sizeof (sa_service_record_t);
2099 	access_args.sq_callback = NULL;
2100 	access_args.sq_callback_arg = NULL;
2101 
2102 	for (s = 0; s < sl->p_count; s++) {
2103 		retval = ibcm_contact_sa_access(sl[s].p_saa_hdl, &access_args,
2104 		    &length, &results_p);
2105 		if (retval != IBT_SUCCESS)
2106 			if (sl[s].p_multi & IBTL_CM_MULTI_SM)
2107 				continue;
2108 			else
2109 				return (retval);
2110 
2111 		if ((results_p == NULL) || (length == 0)) {
2112 			IBTF_DPRINTF_L2(cmlog, "ibcm_saa_service_rec: SvcRec "
2113 			    "Not Found: res_p %p, len %d", results_p, length);
2114 			if (sl[s].p_multi & IBTL_CM_MULTI_SM) {
2115 				retval = IBT_SERVICE_RECORDS_NOT_FOUND;
2116 				continue;
2117 			} else
2118 				return (IBT_SERVICE_RECORDS_NOT_FOUND);
2119 		}
2120 
2121 		/* if we are here, we got some records. so break. */
2122 		break;
2123 	}
2124 
2125 	if (retval != IBT_SUCCESS)
2126 		return (retval);
2127 
2128 	num_req = length / sizeof (sa_service_record_t);
2129 
2130 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: Got %d Service Records.",
2131 	    num_req);
2132 
2133 	svcrec_resp = (sa_service_record_t *)results_p;
2134 	rec_found = 0;
2135 
2136 	/* Update the return values. */
2137 	if (dinfo->num_dest) {
2138 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: Get ServiceRec "
2139 		    "for Specified DGID: %d", dinfo->num_dest);
2140 
2141 		for (i = 0; i < num_req; i++, svcrec_resp++) {
2142 			/* Limited P_Key is NOT supported as of now!. */
2143 			if ((svcrec_resp->ServiceP_Key & 0x8000) == 0) {
2144 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2145 				    "SvcPkey 0x%X limited, reject the record.",
2146 				    svcrec_resp->ServiceP_Key);
2147 				continue;
2148 			}
2149 
2150 			for (j = 0; j < dinfo->num_dest; j++) {
2151 				if (dinfo->dest[j].d_gid.gid_guid ==
2152 				    svcrec_resp->ServiceGID.gid_guid) {
2153 					ibcm_fill_svcinfo(svcrec_resp,
2154 					    &dinfo->dest[j]);
2155 					rec_found++;
2156 				}
2157 				if (rec_found == dinfo->num_dest)
2158 					break;
2159 			}
2160 			if (rec_found == dinfo->num_dest)
2161 				break;
2162 		}
2163 		if (rec_found != dinfo->num_dest) {
2164 			IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: Did NOT "
2165 			    "find ServiceRec for all DGIDs: (%d/%d)", rec_found,
2166 			    dinfo->num_dest);
2167 			retval = IBT_INSUFF_DATA;
2168 		}
2169 	} else if (p_arg->flags & IBT_PATH_APM) {
2170 		ib_gid_t		p_gid, a_gid, last_p_gid;
2171 		ib_gid_t		*gidp = NULL;
2172 		uint_t			n_gids;
2173 		sa_service_record_t	*stmp;
2174 		boolean_t		pri_fill_done = B_FALSE;
2175 		boolean_t		alt_fill_done = B_FALSE;
2176 		ib_pkey_t		p_pkey = 0, a_pkey = 0;
2177 
2178 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: Need to "
2179 		    "find ServiceRec that can satisfy APM");
2180 
2181 		p_gid.gid_prefix = p_gid.gid_guid = 0;
2182 		a_gid.gid_prefix = a_gid.gid_guid = 0;
2183 		last_p_gid.gid_prefix = last_p_gid.gid_guid = 0;
2184 
2185 		for (i = 0; i < num_req; i++, svcrec_resp++) {
2186 			ibt_status_t	ret;
2187 			boolean_t	is_this_on_local_node = B_FALSE;
2188 
2189 			/* Limited P_Key is NOT supported as of now!. */
2190 			if ((svcrec_resp->ServiceP_Key & 0x8000) == 0) {
2191 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2192 				    "SvcPkey 0x%X limited, reject the record.",
2193 				    svcrec_resp->ServiceP_Key);
2194 				continue;
2195 			}
2196 
2197 			p_gid = svcrec_resp->ServiceGID;
2198 
2199 			/* Let's avoid LoopBack Nodes. */
2200 			for (j = 0; j < sl->p_count; j++) {
2201 				if (p_gid.gid_guid == sl[j].p_sgid.gid_guid) {
2202 					is_this_on_local_node = B_TRUE;
2203 
2204 					IBTF_DPRINTF_L3(cmlog,
2205 					    "ibcm_saa_service_rec: ServiceGID "
2206 					    "%llX:%llX is on Local Node, "
2207 					    "search for remote.",
2208 					    p_gid.gid_prefix, p_gid.gid_guid);
2209 				}
2210 			}
2211 
2212 			if (is_this_on_local_node) {
2213 				if ((i + 1) < num_req) {
2214 					p_gid.gid_prefix = 0;
2215 					p_gid.gid_guid = 0;
2216 					continue;
2217 				} else if (last_p_gid.gid_prefix != 0) {
2218 					p_gid = last_p_gid;
2219 					break;
2220 				}
2221 			}
2222 
2223 			IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2224 			    "Finally let Primary DGID = %llX:%llX",
2225 			    p_gid.gid_prefix, p_gid.gid_guid);
2226 
2227 			ret = ibt_get_companion_port_gids(p_gid, 0, 0,
2228 			    &gidp, &n_gids);
2229 			if (ret == IBT_SUCCESS) {
2230 				IBTF_DPRINTF_L3(cmlog,
2231 				    "ibcm_saa_service_rec: Found %d "
2232 				    "CompGID for %llX:%llX", n_gids,
2233 				    p_gid.gid_prefix, p_gid.gid_guid);
2234 
2235 				stmp = (sa_service_record_t *)results_p;
2236 				a_gid.gid_prefix = a_gid.gid_guid = 0;
2237 
2238 				if (sl->p_multi & IBTL_CM_MULTI_SM) {
2239 					/* validate sn_pfx */
2240 					a_gid = ibcm_saa_get_agid(sl,
2241 					    gidp, n_gids);
2242 				} else {
2243 					for (k = 0; k < num_req; k++) {
2244 						ib_gid_t sg = stmp->ServiceGID;
2245 
2246 						IBTF_DPRINTF_L3(cmlog,
2247 						    "ibcm_saa_service_rec: "
2248 						    "SvcGID[%d] = %llX:%llX", k,
2249 						    sg.gid_prefix, sg.gid_guid);
2250 
2251 						for (j = 0; j < n_gids; j++) {
2252 							if (gidp[j].gid_guid ==
2253 							    sg.gid_guid) {
2254 								a_gid = gidp[j];
2255 								break;
2256 							}
2257 						}
2258 						if (a_gid.gid_guid)
2259 							break;
2260 						stmp++;
2261 					}
2262 					if (a_gid.gid_guid == 0) {
2263 						/* Rec not found for Alt. */
2264 						for (j = 0; j < n_gids; j++) {
2265 							if (gidp[j].gid_prefix
2266 							    == p_gid.
2267 							    gid_prefix) {
2268 								a_gid = gidp[j];
2269 								break;
2270 							}
2271 						}
2272 					}
2273 				}
2274 				kmem_free(gidp,
2275 				    n_gids * sizeof (ib_gid_t));
2276 
2277 				if (a_gid.gid_guid)
2278 					break;
2279 			} else if (ret == IBT_GIDS_NOT_FOUND) {
2280 				last_p_gid = p_gid;
2281 				IBTF_DPRINTF_L3(cmlog,
2282 				    "ibcm_saa_service_rec: Didn't find "
2283 				    "CompGID for %llX:%llX, ret=%d",
2284 				    p_gid.gid_prefix, p_gid.gid_guid,
2285 				    ret);
2286 			} else {
2287 				IBTF_DPRINTF_L3(cmlog,
2288 				    "ibcm_saa_service_rec: Call to "
2289 				    "ibt_get_companion_port_gids(%llX:"
2290 				    "%llX) Failed = %d",
2291 				    p_gid.gid_prefix, p_gid.gid_guid,
2292 				    ret);
2293 			}
2294 		}
2295 
2296 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: \n\t"
2297 		    "Pri DGID(%llX:%llX), Alt DGID(%llX:%llX)",
2298 		    p_gid.gid_prefix, p_gid.gid_guid, a_gid.gid_prefix,
2299 		    a_gid.gid_guid);
2300 
2301 		svcrec_resp = (sa_service_record_t *)results_p;
2302 
2303 		for (i = 0, j = 0; i < num_req; i++, svcrec_resp++) {
2304 			/* Limited P_Key is NOT supported as of now!. */
2305 			if ((svcrec_resp->ServiceP_Key & 0x8000) == 0) {
2306 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2307 				    "SvcPkey 0x%X limited, reject the record.",
2308 				    svcrec_resp->ServiceP_Key);
2309 				continue;
2310 			}
2311 
2312 			if ((!pri_fill_done) && (p_gid.gid_guid ==
2313 			    svcrec_resp->ServiceGID.gid_guid)) {
2314 				p_pkey = svcrec_resp->ServiceP_Key;
2315 				if ((a_pkey != 0) &&
2316 				    (a_pkey != p_pkey)) {
2317 					IBTF_DPRINTF_L3(cmlog,
2318 					    "ibcm_saa_service_rec: "
2319 					    "Pri(0x%X) & Alt (0x%X) "
2320 					    "PKey must match.",
2321 					    p_pkey, a_pkey);
2322 					p_pkey = 0;
2323 					continue;
2324 				}
2325 				ibcm_fill_svcinfo(svcrec_resp,
2326 				    &dinfo->dest[j++]);
2327 				rec_found++;
2328 				pri_fill_done = B_TRUE;
2329 			} else if ((!alt_fill_done) && (a_gid.gid_guid ==
2330 			    svcrec_resp->ServiceGID.gid_guid)) {
2331 				a_pkey = svcrec_resp->ServiceP_Key;
2332 				if ((p_pkey != 0) &&
2333 				    (a_pkey != p_pkey)) {
2334 					IBTF_DPRINTF_L3(cmlog,
2335 					    "ibcm_saa_service_rec: "
2336 					    "Pri(0x%X) & Alt (0x%X) "
2337 					    "PKey must match.",
2338 					    p_pkey, a_pkey);
2339 					a_pkey = 0;
2340 					continue;
2341 				}
2342 				ibcm_fill_svcinfo(svcrec_resp,
2343 				    &dinfo->dest[j++]);
2344 				rec_found++;
2345 				alt_fill_done = B_TRUE;
2346 			}
2347 
2348 			if (rec_found == 2)
2349 				break;
2350 		}
2351 		if ((!alt_fill_done) && (a_gid.gid_guid)) {
2352 			dinfo->dest[j].d_gid = a_gid;
2353 			dinfo->dest[j].d_pkey = p_pkey;
2354 			rec_found++;
2355 			IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2356 			    "Let Alt Pkey=%X, DGID=%llX:%llX", p_pkey,
2357 			    a_gid.gid_prefix, a_gid.gid_guid);
2358 		}
2359 
2360 		if (rec_found == 1)
2361 			retval = IBT_INSUFF_DATA;
2362 	} else if (p_arg->flags & IBT_PATH_MULTI_SVC_DEST) {
2363 		for (i = 0; i < num_req; i++, svcrec_resp++) {
2364 			ib_gid_t	p_gid;
2365 			boolean_t	is_this_on_local_node = B_FALSE;
2366 
2367 			/* Limited P_Key is NOT supported as of now!. */
2368 			if ((svcrec_resp->ServiceP_Key & 0x8000) == 0) {
2369 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2370 				    "SvcPkey 0x%X limited, reject the record.",
2371 				    svcrec_resp->ServiceP_Key);
2372 				continue;
2373 			}
2374 
2375 			p_gid = svcrec_resp->ServiceGID;
2376 
2377 			/* Let's avoid LoopBack Nodes. */
2378 			for (j = 0; j < sl->p_count; j++) {
2379 				if (p_gid.gid_guid == sl[j].p_sgid.gid_guid) {
2380 					is_this_on_local_node = B_TRUE;
2381 					IBTF_DPRINTF_L3(cmlog,
2382 					    "ibcm_saa_service_rec: ServiceGID "
2383 					    "%llX:%llX is on Local Node, "
2384 					    "search for remote.",
2385 					    p_gid.gid_prefix, p_gid.gid_guid);
2386 				}
2387 			}
2388 
2389 			if (is_this_on_local_node)
2390 				if ((i + 1) < num_req)
2391 					continue;
2392 
2393 			IBTF_DPRINTF_L4(cmlog, "ibcm_saa_service_rec: "
2394 			    "Found ServiceGID = %llX:%llX",
2395 			    p_gid.gid_prefix, p_gid.gid_guid);
2396 
2397 			ibcm_fill_svcinfo(svcrec_resp,
2398 			    &dinfo->dest[rec_found]);
2399 			rec_found++;
2400 			if (rec_found == p_arg->max_paths)
2401 				break;
2402 		}
2403 
2404 		if (rec_found < p_arg->max_paths)
2405 			retval = IBT_INSUFF_DATA;
2406 	} else {
2407 		for (i = 0; i < num_req; i++) {
2408 			/* Limited P_Key is NOT supported as of now!. */
2409 			if ((svcrec_resp->ServiceP_Key & 0x8000) == 0) {
2410 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2411 				    "SvcPkey 0x%X limited, reject the record.",
2412 				    svcrec_resp->ServiceP_Key);
2413 				svcrec_resp++;
2414 				continue;
2415 			}
2416 
2417 			ibcm_fill_svcinfo(svcrec_resp, &dinfo->dest[0]);
2418 			rec_found = 1;
2419 
2420 			/* Avoid having loopback node */
2421 			if (svcrec_resp->ServiceGID.gid_guid !=
2422 			    sl->p_sgid.gid_guid) {
2423 				break;
2424 			} else {
2425 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2426 				    "avoid LoopBack node.");
2427 				svcrec_resp++;
2428 			}
2429 		}
2430 	}
2431 
2432 	/* Deallocate the memory for results_p. */
2433 	kmem_free(results_p, length);
2434 	if (dinfo->num_dest == 0)
2435 		dinfo->num_dest = rec_found;
2436 
2437 	/*
2438 	 * Check out whether all Service Path we looking for are on the same
2439 	 * P_key. If yes, then set the global p_key field with that value,
2440 	 * to make it easy during SA Path Query.
2441 	 */
2442 	if ((dinfo->num_dest) && (dinfo->p_key == 0)) {
2443 		ib_pkey_t	pk = dinfo->dest[0].d_pkey;
2444 
2445 		if (dinfo->num_dest == 1) {
2446 			dinfo->p_key = pk;
2447 		} else {
2448 			for (i = 1; i < (dinfo->num_dest - 1); i++) {
2449 				IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: "
2450 				    "pk= 0x%X, pk[%d]= 0x%X", pk, i,
2451 				    dinfo->dest[i].d_pkey);
2452 				if (pk != dinfo->dest[i].d_pkey) {
2453 					dinfo->p_key = 0;
2454 					break;
2455 				} else {
2456 					dinfo->p_key = pk;
2457 				}
2458 			}
2459 		}
2460 	}
2461 
2462 	if (rec_found == 0) {
2463 		IBTF_DPRINTF_L2(cmlog, "ibcm_saa_service_rec: "
2464 		    "ServiceRec NOT Found");
2465 		retval = IBT_SERVICE_RECORDS_NOT_FOUND;
2466 	}
2467 
2468 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_service_rec: done. Status %d, "
2469 	    "PKey 0x%X, Found %d SvcRec", retval, dinfo->p_key, rec_found);
2470 
2471 	return (retval);
2472 }
2473 
2474 
2475 static boolean_t
2476 ibcm_compare_paths(sa_path_record_t *pr_resp, ibt_cep_path_t *rc_path,
2477     ibtl_cm_hca_port_t *c_hp)
2478 {
2479 	if ((rc_path->cep_hca_port_num == c_hp->hp_port) &&
2480 	    (rc_path->cep_adds_vect.av_src_path ==
2481 	    (pr_resp->SLID - c_hp->hp_base_lid)) &&
2482 	    (rc_path->cep_adds_vect.av_dlid == pr_resp->DLID) &&
2483 	    (rc_path->cep_adds_vect.av_srate == pr_resp->Rate)) {
2484 		return (B_TRUE);
2485 	} else {
2486 		return (B_FALSE);
2487 	}
2488 }
2489 
2490 /*
2491  * ibcm_get_comp_pgids() routine gets the companion port for 'gid'.
2492  *
2493  * On success:
2494  *	If 'n_gid' is specified, then verify whether 'n_gid' is indeed a
2495  *	companion portgid of 'gid'.  If matches return success or else error.
2496  *
2497  *	If 'n_gid' is NOT specified, then return back SUCCESS along with
2498  *	obtained Companion PortGids 'gid_p', where 'num' indicated number
2499  *	of companion portgids returned in 'gid_p'.
2500  */
2501 
2502 static ibt_status_t
2503 ibcm_get_comp_pgids(ib_gid_t gid, ib_gid_t n_gid, ib_guid_t hca_guid,
2504     ib_gid_t **gid_p, uint_t *num)
2505 {
2506 	ibt_status_t    ret;
2507 	int		i;
2508 
2509 	ret = ibt_get_companion_port_gids(gid, hca_guid, 0, gid_p, num);
2510 	if ((ret != IBT_SUCCESS) && (ret != IBT_GIDS_NOT_FOUND)) {
2511 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_comp_pgids: "
2512 		    "ibt_get_companion_port_gids(%llX:%llX) Failed: %d",
2513 		    gid.gid_prefix, gid.gid_guid, ret);
2514 	} else if ((ret == IBT_GIDS_NOT_FOUND) && (n_gid.gid_guid != 0)) {
2515 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_comp_pgids: Specified GID "
2516 		    "(%llX:%llX) is NOT a Companion \n\t to current channel's "
2517 		    "GID(%llX:%llX)", n_gid.gid_prefix, n_gid.gid_guid,
2518 		    gid.gid_prefix, gid.gid_guid);
2519 		ret = IBT_INVALID_PARAM;
2520 	} else if (n_gid.gid_guid != 0) {
2521 		/*
2522 		 * We found some Comp GIDs and n_gid is specified. Validate
2523 		 * whether the 'n_gid' specified is indeed the companion port
2524 		 * GID of 'gid'.
2525 		 */
2526 		for (i = 0; i < *num; i++) {
2527 			if ((n_gid.gid_prefix == gid_p[i]->gid_prefix) &&
2528 			    (n_gid.gid_guid == gid_p[i]->gid_guid)) {
2529 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_comp_pgids: "
2530 				    "Matching Found!. Done.");
2531 				return (IBT_SUCCESS);
2532 			}
2533 		}
2534 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_comp_pgids: GID (%llX:%llX)\n"
2535 		    "\t and (%llX:%llX) are NOT Companion Port GIDS",
2536 		    n_gid.gid_prefix, n_gid.gid_guid, gid.gid_prefix,
2537 		    gid.gid_guid);
2538 		ret = IBT_INVALID_PARAM;
2539 	} else {
2540 		ret = IBT_SUCCESS;
2541 	}
2542 
2543 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_comp_pgids: done. Status = %d", ret);
2544 	return (ret);
2545 }
2546 
2547 /*
2548  * Function:
2549  *	ibt_get_alt_path
2550  * Input:
2551  *	rc_chan		An RC channel handle returned in a previous call
2552  *			ibt_alloc_rc_channel(9F), specifies the channel to open.
2553  *	flags		Path flags.
2554  *	attrp		A pointer to an ibt_alt_path_attr_t(9S) structure that
2555  *			specifies required attributes of the selected path(s).
2556  * Output:
2557  *	api_p		An ibt_alt_path_info_t(9S) struct filled in as output
2558  *			parameters.
2559  * Returns:
2560  *	IBT_SUCCESS on Success else appropriate error.
2561  * Description:
2562  *      Finds the best alternate path to a specified channel (as determined by
2563  *      the IBTL) that satisfies the requirements specified in an
2564  *      ibt_alt_path_attr_t struct.  The specified channel must have been
2565  *      previously opened successfully using ibt_open_rc_channel.
2566  *      This function also ensures that the service being accessed by the
2567  *      channel is available at the selected alternate port.
2568  *
2569  *      Note: The apa_dgid must be on the same destination channel adapter,
2570  *      if specified.
2571  *	This routine can not be called from interrupt context.
2572  */
2573 ibt_status_t
2574 ibt_get_alt_path(ibt_channel_hdl_t rc_chan, ibt_path_flags_t flags,
2575     ibt_alt_path_attr_t *attrp, ibt_alt_path_info_t *api_p)
2576 {
2577 	sa_multipath_record_t	*mpr_req;
2578 	sa_path_record_t	*pr_resp;
2579 	ibmf_saa_access_args_t	access_args;
2580 	ibt_qp_query_attr_t	qp_attr;
2581 	ibtl_cm_hca_port_t	c_hp, n_hp;
2582 	ibcm_hca_info_t		*hcap;
2583 	void			*results_p;
2584 	uint64_t		c_mask = 0;
2585 	ib_gid_t		*gid_ptr = NULL;
2586 	ib_gid_t		*sgids_p = NULL,  *dgids_p = NULL;
2587 	ib_gid_t		cur_dgid, cur_sgid;
2588 	ib_gid_t		new_dgid, new_sgid;
2589 	ibmf_saa_handle_t	saa_handle;
2590 	size_t			length;
2591 	int			i, j, template_len, rec_found;
2592 	uint_t			snum = 0, dnum = 0, num_rec;
2593 	ibt_status_t		retval;
2594 	ib_mtu_t		prim_mtu;
2595 
2596 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path(%p, %x, %p, %p)",
2597 	    rc_chan, flags, attrp, api_p);
2598 
2599 	/* validate channel */
2600 	if (IBCM_INVALID_CHANNEL(rc_chan)) {
2601 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: invalid channel");
2602 		return (IBT_CHAN_HDL_INVALID);
2603 	}
2604 
2605 	if (api_p == NULL) {
2606 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: invalid attribute: "
2607 		    " AltPathInfo can't be NULL");
2608 		return (IBT_INVALID_PARAM);
2609 	}
2610 
2611 	retval = ibt_query_qp(rc_chan, &qp_attr);
2612 	if (retval != IBT_SUCCESS) {
2613 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: ibt_query_qp(%p) "
2614 		    "failed %d", rc_chan, retval);
2615 		return (retval);
2616 	}
2617 
2618 	if (qp_attr.qp_info.qp_trans != IBT_RC_SRV) {
2619 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: "
2620 		    "Invalid Channel type: Applicable only to RC Channel");
2621 		return (IBT_CHAN_SRV_TYPE_INVALID);
2622 	}
2623 
2624 	cur_dgid =
2625 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_adds_vect.av_dgid;
2626 	cur_sgid =
2627 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_adds_vect.av_sgid;
2628 	prim_mtu = qp_attr.qp_info.qp_transport.rc.rc_path_mtu;
2629 
2630 	/* If optional attributes are specified, validate them. */
2631 	if (attrp) {
2632 		new_dgid = attrp->apa_dgid;
2633 		new_sgid = attrp->apa_sgid;
2634 	} else {
2635 		new_dgid.gid_prefix = 0;
2636 		new_dgid.gid_guid = 0;
2637 		new_sgid.gid_prefix = 0;
2638 		new_sgid.gid_guid = 0;
2639 	}
2640 
2641 	if ((new_dgid.gid_prefix != 0) && (new_sgid.gid_prefix != 0) &&
2642 	    (new_dgid.gid_prefix != new_sgid.gid_prefix)) {
2643 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: Specified SGID's "
2644 		    "SNprefix (%llX) doesn't match with \n specified DGID's "
2645 		    "SNprefix: %llX", new_sgid.gid_prefix, new_dgid.gid_prefix);
2646 		return (IBT_INVALID_PARAM);
2647 	}
2648 
2649 	/* For the specified SGID, get HCA information. */
2650 	retval = ibtl_cm_get_hca_port(cur_sgid, 0, &c_hp);
2651 	if (retval != IBT_SUCCESS) {
2652 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: "
2653 		    "Get HCA Port Failed: %d", retval);
2654 		return (retval);
2655 	}
2656 
2657 	hcap = ibcm_find_hca_entry(c_hp.hp_hca_guid);
2658 	if (hcap == NULL) {
2659 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: NO HCA found");
2660 		return (IBT_HCA_BUSY_DETACHING);
2661 	}
2662 
2663 	/* Validate whether this HCA support APM */
2664 	if (!(hcap->hca_caps & IBT_HCA_AUTO_PATH_MIG)) {
2665 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: "
2666 		    "HCA (%llX) - APM NOT SUPPORTED ", c_hp.hp_hca_guid);
2667 		retval = IBT_APM_NOT_SUPPORTED;
2668 		goto get_alt_path_done;
2669 	}
2670 
2671 	/* Get Companion Port GID of the current Channel's SGID */
2672 	if ((new_sgid.gid_guid == 0) || ((new_sgid.gid_guid != 0) &&
2673 	    (new_sgid.gid_guid != cur_sgid.gid_guid))) {
2674 		IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: SRC: "
2675 		    "Get Companion PortGids for - %llX:%llX",
2676 		    cur_sgid.gid_prefix, cur_sgid.gid_guid);
2677 
2678 		retval = ibcm_get_comp_pgids(cur_sgid, new_sgid,
2679 		    c_hp.hp_hca_guid, &sgids_p, &snum);
2680 		if (retval != IBT_SUCCESS)
2681 			goto get_alt_path_done;
2682 	}
2683 
2684 	/* Get Companion Port GID of the current Channel's DGID */
2685 	if ((new_dgid.gid_guid == 0) || ((new_dgid.gid_guid != 0) &&
2686 	    (new_dgid.gid_guid != cur_dgid.gid_guid))) {
2687 
2688 		IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: DEST: "
2689 		    "Get Companion PortGids for - %llX:%llX",
2690 		    cur_dgid.gid_prefix, cur_dgid.gid_guid);
2691 
2692 		retval = ibcm_get_comp_pgids(cur_dgid, new_dgid, 0, &dgids_p,
2693 		    &dnum);
2694 		if (retval != IBT_SUCCESS)
2695 			goto get_alt_path_done;
2696 	}
2697 
2698 	if ((new_dgid.gid_guid == 0) || (new_sgid.gid_guid == 0)) {
2699 		if (new_sgid.gid_guid == 0) {
2700 			for (i = 0; i < snum; i++) {
2701 				if (new_dgid.gid_guid == 0) {
2702 					for (j = 0; j < dnum; j++) {
2703 						if (sgids_p[i].gid_prefix ==
2704 						    dgids_p[j].gid_prefix) {
2705 							new_dgid = dgids_p[j];
2706 							new_sgid = sgids_p[i];
2707 
2708 							goto get_alt_proceed;
2709 						}
2710 					}
2711 					/*  Current DGID */
2712 					if (sgids_p[i].gid_prefix ==
2713 					    cur_dgid.gid_prefix) {
2714 						new_sgid = sgids_p[i];
2715 						goto get_alt_proceed;
2716 					}
2717 				} else {
2718 					if (sgids_p[i].gid_prefix ==
2719 					    new_dgid.gid_prefix) {
2720 						new_sgid = sgids_p[i];
2721 						goto get_alt_proceed;
2722 					}
2723 				}
2724 			}
2725 			/* Current SGID */
2726 			if (new_dgid.gid_guid == 0) {
2727 				for (j = 0; j < dnum; j++) {
2728 					if (cur_sgid.gid_prefix ==
2729 					    dgids_p[j].gid_prefix) {
2730 						new_dgid = dgids_p[j];
2731 
2732 						goto get_alt_proceed;
2733 					}
2734 				}
2735 			}
2736 		} else if (new_dgid.gid_guid == 0) {
2737 			for (i = 0; i < dnum; i++) {
2738 				if (dgids_p[i].gid_prefix ==
2739 				    new_sgid.gid_prefix) {
2740 					new_dgid = dgids_p[i];
2741 					goto get_alt_proceed;
2742 				}
2743 			}
2744 			/* Current DGID */
2745 			if (cur_dgid.gid_prefix == new_sgid.gid_prefix) {
2746 				goto get_alt_proceed;
2747 			}
2748 		}
2749 		/*
2750 		 * hmm... No Companion Ports available.
2751 		 * so we will be using current or specified attributes only.
2752 		 */
2753 	}
2754 
2755 get_alt_proceed:
2756 
2757 	if (new_sgid.gid_guid != 0) {
2758 		retval = ibtl_cm_get_hca_port(new_sgid, 0, &n_hp);
2759 		if (retval != IBT_SUCCESS) {
2760 			IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: "
2761 			    "Get HCA Port Failed: %d", retval);
2762 			goto get_alt_path_done;
2763 		}
2764 	}
2765 
2766 	/* Calculate the size for multi-path records template */
2767 	template_len = (2 * sizeof (ib_gid_t)) + sizeof (sa_multipath_record_t);
2768 
2769 	mpr_req = kmem_zalloc(template_len, KM_SLEEP);
2770 
2771 	ASSERT(mpr_req != NULL);
2772 
2773 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mpr_req))
2774 
2775 	gid_ptr = (ib_gid_t *)(((uchar_t *)mpr_req) +
2776 	    sizeof (sa_multipath_record_t));
2777 
2778 	/* SGID */
2779 	if (new_sgid.gid_guid == 0)
2780 		*gid_ptr = cur_sgid;
2781 	else
2782 		*gid_ptr = new_sgid;
2783 
2784 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: Get Path Between "
2785 	    " SGID : %llX:%llX", gid_ptr->gid_prefix, gid_ptr->gid_guid);
2786 
2787 	gid_ptr++;
2788 
2789 	/* DGID */
2790 	if (new_dgid.gid_guid == 0)
2791 		*gid_ptr = cur_dgid;
2792 	else
2793 		*gid_ptr = new_dgid;
2794 
2795 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path:\t\t    DGID : %llX:%llX",
2796 	    gid_ptr->gid_prefix, gid_ptr->gid_guid);
2797 
2798 	mpr_req->SGIDCount = 1;
2799 	c_mask = SA_MPR_COMPMASK_SGIDCOUNT;
2800 
2801 	mpr_req->DGIDCount = 1;
2802 	c_mask |= SA_MPR_COMPMASK_DGIDCOUNT;
2803 
2804 	/* Is Flow Label Specified. */
2805 	if (attrp) {
2806 		if (attrp->apa_flow) {
2807 			mpr_req->FlowLabel = attrp->apa_flow;
2808 			c_mask |= SA_MPR_COMPMASK_FLOWLABEL;
2809 		}
2810 
2811 		/* Is HopLimit Specified. */
2812 		if (flags & IBT_PATH_HOP) {
2813 			mpr_req->HopLimit = attrp->apa_hop;
2814 			c_mask |= SA_MPR_COMPMASK_HOPLIMIT;
2815 		}
2816 
2817 		/* Is TClass Specified. */
2818 		if (attrp->apa_tclass) {
2819 			mpr_req->TClass = attrp->apa_tclass;
2820 			c_mask |= SA_MPR_COMPMASK_TCLASS;
2821 		}
2822 
2823 		/* Is SL specified. */
2824 		if (attrp->apa_sl) {
2825 			mpr_req->SL = attrp->apa_sl;
2826 			c_mask |= SA_MPR_COMPMASK_SL;
2827 		}
2828 
2829 		if (flags & IBT_PATH_PERF) {
2830 			mpr_req->PacketLifeTimeSelector = IBT_BEST;
2831 			mpr_req->RateSelector = IBT_BEST;
2832 
2833 			c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR |
2834 			    SA_MPR_COMPMASK_RATESELECTOR;
2835 		} else {
2836 			if (attrp->apa_pkt_lt.p_selector == IBT_BEST) {
2837 				mpr_req->PacketLifeTimeSelector = IBT_BEST;
2838 				c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR;
2839 			}
2840 
2841 			if (attrp->apa_srate.r_selector == IBT_BEST) {
2842 				mpr_req->RateSelector = IBT_BEST;
2843 				c_mask |= SA_MPR_COMPMASK_RATESELECTOR;
2844 			}
2845 		}
2846 
2847 		/*
2848 		 * Honor individual selection of these attributes,
2849 		 * even if IBT_PATH_PERF is set.
2850 		 */
2851 		/* Check out whether Packet Life Time is specified. */
2852 		if (attrp->apa_pkt_lt.p_pkt_lt) {
2853 			mpr_req->PacketLifeTime =
2854 			    ibt_usec2ib(attrp->apa_pkt_lt.p_pkt_lt);
2855 			mpr_req->PacketLifeTimeSelector =
2856 			    attrp->apa_pkt_lt.p_selector;
2857 
2858 			c_mask |= SA_MPR_COMPMASK_PKTLT |
2859 			    SA_MPR_COMPMASK_PKTLTSELECTOR;
2860 		}
2861 
2862 		/* Is SRATE specified. */
2863 		if (attrp->apa_srate.r_srate) {
2864 			mpr_req->Rate = attrp->apa_srate.r_srate;
2865 			mpr_req->RateSelector = attrp->apa_srate.r_selector;
2866 
2867 			c_mask |= SA_MPR_COMPMASK_RATE |
2868 			    SA_MPR_COMPMASK_RATESELECTOR;
2869 		}
2870 	}
2871 
2872 	/* Alt PathMTU can be GT or EQU to current channel's Pri PathMTU */
2873 
2874 	/* P_Key must be same as that of primary path */
2875 	retval = ibt_index2pkey_byguid(c_hp.hp_hca_guid, c_hp.hp_port,
2876 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_pkey_ix,
2877 	    &mpr_req->P_Key);
2878 	if (retval != IBT_SUCCESS) {
2879 		IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: Idx2Pkey Failed: %d",
2880 		    retval);
2881 		goto get_alt_path_done;
2882 	}
2883 	c_mask |= SA_MPR_COMPMASK_PKEY;
2884 
2885 	mpr_req->Reversible = 1;	/* We always get REVERSIBLE paths. */
2886 	mpr_req->IndependenceSelector = 1;
2887 	c_mask |= SA_MPR_COMPMASK_REVERSIBLE | SA_MPR_COMPMASK_INDEPSEL;
2888 
2889 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mpr_req))
2890 
2891 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: CMask: 0x%llX", c_mask);
2892 
2893 	/* NOTE: We will **NOT** specify how many records we want. */
2894 
2895 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: Primary: MTU %d, PKey[%d]="
2896 	    "0x%X\n\tSGID = %llX:%llX, DGID = %llX:%llX", prim_mtu,
2897 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_pkey_ix, mpr_req->P_Key,
2898 	    cur_sgid.gid_prefix, cur_sgid.gid_guid, cur_dgid.gid_prefix,
2899 	    cur_dgid.gid_guid);
2900 
2901 	/* Get SA Access Handle. */
2902 	if (new_sgid.gid_guid != 0)
2903 		saa_handle = ibcm_get_saa_handle(hcap, n_hp.hp_port);
2904 	else
2905 		saa_handle = ibcm_get_saa_handle(hcap, c_hp.hp_port);
2906 	if (saa_handle == NULL) {
2907 		retval = IBT_HCA_PORT_NOT_ACTIVE;
2908 		goto get_alt_path_done;
2909 	}
2910 
2911 	/* Contact SA Access to retrieve Path Records. */
2912 	access_args.sq_attr_id = SA_MULTIPATHRECORD_ATTRID;
2913 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
2914 	access_args.sq_component_mask = c_mask;
2915 	access_args.sq_template = mpr_req;
2916 	access_args.sq_template_length = sizeof (sa_multipath_record_t);
2917 	access_args.sq_callback = NULL;
2918 	access_args.sq_callback_arg = NULL;
2919 
2920 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
2921 	    &results_p);
2922 	if (retval != IBT_SUCCESS) {
2923 		goto get_alt_path_done;
2924 	}
2925 
2926 	num_rec = length / sizeof (sa_path_record_t);
2927 
2928 	kmem_free(mpr_req, template_len);
2929 
2930 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: Found %d Paths", num_rec);
2931 
2932 	rec_found = 0;
2933 	if ((results_p != NULL) && (num_rec > 0)) {
2934 		/* Update the PathInfo with the response Path Records */
2935 		pr_resp = (sa_path_record_t *)results_p;
2936 		for (i = 0; i < num_rec; i++, pr_resp++) {
2937 			if (prim_mtu > pr_resp->Mtu) {
2938 				IBTF_DPRINTF_L2(cmlog, "ibt_get_alt_path: "
2939 				    "Alt PathMTU(%d) must be GT or EQU to Pri "
2940 				    "PathMTU(%d). Ignore this rec",
2941 				    pr_resp->Mtu, prim_mtu);
2942 				continue;
2943 			}
2944 
2945 			if ((new_sgid.gid_guid == 0) &&
2946 			    (new_dgid.gid_guid == 0)) {
2947 				/* Reject PathRec if it same as Primary Path. */
2948 				if (ibcm_compare_paths(pr_resp,
2949 				    &qp_attr.qp_info.qp_transport.rc.rc_path,
2950 				    &c_hp)) {
2951 					IBTF_DPRINTF_L3(cmlog,
2952 					    "ibt_get_alt_path: PathRec obtained"
2953 					    " is similar to Prim Path, ignore "
2954 					    "this record");
2955 					continue;
2956 				}
2957 			}
2958 
2959 			if (new_sgid.gid_guid == 0) {
2960 				retval = ibcm_update_cep_info(pr_resp, NULL,
2961 				    &c_hp, &api_p->ap_alt_cep_path);
2962 			} else {
2963 				retval = ibcm_update_cep_info(pr_resp, NULL,
2964 				    &n_hp, &api_p->ap_alt_cep_path);
2965 			}
2966 			if (retval != IBT_SUCCESS)
2967 				continue;
2968 
2969 			/* Update some leftovers */
2970 			_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*api_p))
2971 
2972 			api_p->ap_alt_pkt_lt = pr_resp->PacketLifeTime;
2973 
2974 			_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*api_p))
2975 
2976 			rec_found = 1;
2977 			break;
2978 		}
2979 		kmem_free(results_p, length);
2980 	}
2981 
2982 	if (rec_found == 0) {
2983 		IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: Alternate Path cannot"
2984 		    " be established");
2985 		retval = IBT_PATH_RECORDS_NOT_FOUND;
2986 	} else
2987 		retval = IBT_SUCCESS;
2988 
2989 get_alt_path_done:
2990 	if ((snum) && (sgids_p))
2991 		kmem_free(sgids_p, snum * sizeof (ib_gid_t));
2992 
2993 	if ((dnum) && (dgids_p))
2994 		kmem_free(dgids_p, dnum * sizeof (ib_gid_t));
2995 
2996 	ibcm_dec_hca_acc_cnt(hcap);
2997 
2998 	IBTF_DPRINTF_L3(cmlog, "ibt_get_alt_path: Done (status %d).", retval);
2999 
3000 	return (retval);
3001 }
3002 
3003 
3004 
3005 /*
3006  * IP Path API
3007  */
3008 
3009 typedef struct ibcm_ip_path_tqargs_s {
3010 	ibt_ip_path_attr_t	attr;
3011 	ibt_path_info_t		*paths;
3012 	ibt_path_ip_src_t	*src_ip_p;
3013 	uint8_t			*num_paths_p;
3014 	ibt_ip_path_handler_t	func;
3015 	void			*arg;
3016 	ibt_path_flags_t	flags;
3017 	ibt_clnt_hdl_t		ibt_hdl;
3018 	kmutex_t		ip_lock;
3019 	kcondvar_t		ip_cv;
3020 	ibt_status_t		retval;
3021 	uint_t			len;
3022 } ibcm_ip_path_tqargs_t;
3023 
3024 /* Holds destination information needed to fill in ibt_path_info_t. */
3025 typedef struct ibcm_ip_dinfo_s {
3026 	uint8_t		num_dest;
3027 	ib_gid_t	d_gid[1];
3028 } ibcm_ip_dinfo_t;
3029 
3030 _NOTE(SCHEME_PROTECTS_DATA("Temporary path storage", ibcm_ip_dinfo_s))
3031 
3032 /* Prototype Declarations. */
3033 static void ibcm_process_get_ip_paths(void *tq_arg);
3034 static ibt_status_t ibcm_get_ip_spr(ibcm_ip_path_tqargs_t *,
3035     ibtl_cm_port_list_t *, ibcm_ip_dinfo_t *, uint8_t *, ibt_path_info_t *);
3036 static ibt_status_t ibcm_get_ip_mpr(ibcm_ip_path_tqargs_t *,
3037     ibtl_cm_port_list_t *, ibcm_ip_dinfo_t *dinfo,
3038     uint8_t *, ibt_path_info_t *);
3039 
3040 /*
3041  * Perform SA Access to retrieve Path Records.
3042  */
3043 static ibt_status_t
3044 ibcm_saa_ip_pr(ibcm_ip_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
3045     ibcm_ip_dinfo_t *dinfo, uint8_t *max_count)
3046 {
3047 	uint8_t		num_path = *max_count;
3048 	uint8_t		rec_found = 0;
3049 	ibt_status_t	retval = IBT_SUCCESS;
3050 	uint8_t		i, j;
3051 
3052 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_ip_pr(%p, %p, %p, 0x%X, %d)",
3053 	    p_arg, sl, dinfo, p_arg->flags, *max_count);
3054 
3055 	if ((dinfo->num_dest == 0) || (num_path == 0) || (sl == NULL)) {
3056 		IBTF_DPRINTF_L3(cmlog, "ibcm_saa_ip_pr: Invalid Counters");
3057 		return (IBT_INVALID_PARAM);
3058 	}
3059 
3060 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_ip_pr: MultiSM=%X, #SRC=%d, "
3061 	    "#Dest=%d, #Path %d", sl->p_multi, sl->p_count, dinfo->num_dest,
3062 	    num_path);
3063 
3064 	if ((sl->p_multi != IBTL_CM_SIMPLE_SETUP) ||
3065 	    ((dinfo->num_dest == 1) && (sl->p_count == 1))) {
3066 		/*
3067 		 * Use SinglePathRec if we are dealing w/ MultiSM or
3068 		 * request is for one SGID to one DGID.
3069 		 */
3070 		retval = ibcm_get_ip_spr(p_arg, sl, dinfo,
3071 		    &num_path, &p_arg->paths[rec_found]);
3072 	} else {
3073 		/* MultiPathRec will be used for other queries. */
3074 		retval = ibcm_get_ip_mpr(p_arg, sl, dinfo,
3075 		    &num_path, &p_arg->paths[rec_found]);
3076 	}
3077 	if ((retval != IBT_SUCCESS) && (retval != IBT_INSUFF_DATA))
3078 		IBTF_DPRINTF_L2(cmlog, "ibcm_saa_ip_pr: "
3079 		    "Failed to get PathRec: Status %d", retval);
3080 	else
3081 		rec_found += num_path;
3082 
3083 	if (rec_found == 0)  {
3084 		if (retval == IBT_SUCCESS)
3085 			retval = IBT_PATH_RECORDS_NOT_FOUND;
3086 	} else if (rec_found != *max_count)
3087 		retval = IBT_INSUFF_DATA;
3088 	else if (rec_found != 0)
3089 		retval = IBT_SUCCESS;
3090 
3091 	if ((p_arg->src_ip_p != NULL) && (rec_found != 0)) {
3092 		for (i = 0; i < rec_found; i++) {
3093 			for (j = 0; j < sl->p_count; j++) {
3094 				if (sl[j].p_sgid.gid_guid == p_arg->paths[i].
3095 				    pi_prim_cep_path.cep_adds_vect.
3096 				    av_sgid.gid_guid) {
3097 					bcopy(&sl[j].p_src_ip,
3098 					    &p_arg->src_ip_p[i].ip_primary,
3099 					    sizeof (ibt_ip_addr_t));
3100 				}
3101 				/* Is Alt Path present */
3102 				if (p_arg->paths[i].pi_alt_cep_path.
3103 				    cep_hca_port_num) {
3104 					if (sl[j].p_sgid.gid_guid ==
3105 					    p_arg->paths[i].pi_alt_cep_path.
3106 					    cep_adds_vect.av_sgid.gid_guid) {
3107 						bcopy(&sl[j].p_src_ip,
3108 						    &p_arg->src_ip_p[i].
3109 						    ip_alternate,
3110 						    sizeof (ibt_ip_addr_t));
3111 					}
3112 				}
3113 			}
3114 		}
3115 	}
3116 	IBTF_DPRINTF_L3(cmlog, "ibcm_saa_ip_pr: done. Status = %d, "
3117 	    "Found %d/%d Paths", retval, rec_found, *max_count);
3118 
3119 	*max_count = rec_found; /* Update the return count. */
3120 
3121 	return (retval);
3122 }
3123 
3124 static ibt_status_t
3125 ibcm_ip_update_pri(sa_path_record_t *pr_resp, ibtl_cm_port_list_t *sl,
3126     ibt_path_info_t *paths)
3127 {
3128 	ibt_status_t	retval = IBT_SUCCESS;
3129 	int		s;
3130 
3131 	retval = ibcm_update_cep_info(pr_resp, sl, NULL,
3132 	    &paths->pi_prim_cep_path);
3133 	if (retval != IBT_SUCCESS)
3134 		return (retval);
3135 
3136 	/* Update some leftovers */
3137 	paths->pi_prim_pkt_lt = pr_resp->PacketLifeTime;
3138 	paths->pi_path_mtu = pr_resp->Mtu;
3139 
3140 	for (s = 0; s < sl->p_count; s++) {
3141 		if (pr_resp->SGID.gid_guid == sl[s].p_sgid.gid_guid)
3142 			paths->pi_hca_guid = sl[s].p_hca_guid;
3143 	}
3144 
3145 	/* Set Alternate Path to invalid state. */
3146 	paths->pi_alt_cep_path.cep_hca_port_num = 0;
3147 	paths->pi_alt_cep_path.cep_adds_vect.av_dlid = 0;
3148 
3149 	IBTF_DPRINTF_L5(cmlog, "ibcm_ip_update_pri: Path HCA GUID 0x%llX",
3150 	    paths->pi_hca_guid);
3151 
3152 	return (retval);
3153 }
3154 
3155 
3156 static ibt_status_t
3157 ibcm_get_ip_spr(ibcm_ip_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
3158     ibcm_ip_dinfo_t *dinfo, uint8_t *num_path, ibt_path_info_t *paths)
3159 {
3160 	sa_path_record_t	pathrec_req;
3161 	sa_path_record_t	*pr_resp;
3162 	ibmf_saa_access_args_t	access_args;
3163 	uint64_t		c_mask = 0;
3164 	void			*results_p;
3165 	uint8_t			num_rec;
3166 	size_t			length;
3167 	ibt_status_t		retval;
3168 	int			i, j, k;
3169 	uint8_t			found, p_fnd;
3170 	ibt_ip_path_attr_t	*attrp = &p_arg->attr;
3171 	ibmf_saa_handle_t	saa_handle;
3172 
3173 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr(%p, %p, %p, %d)",
3174 	    p_arg, sl, dinfo, *num_path);
3175 
3176 	bzero(&pathrec_req, sizeof (sa_path_record_t));
3177 
3178 	/* Is Flow Label Specified. */
3179 	if (attrp->ipa_flow) {
3180 		pathrec_req.FlowLabel = attrp->ipa_flow;
3181 		c_mask |= SA_PR_COMPMASK_FLOWLABEL;
3182 	}
3183 
3184 	/* Is HopLimit Specified. */
3185 	if (p_arg->flags & IBT_PATH_HOP) {
3186 		pathrec_req.HopLimit = attrp->ipa_hop;
3187 		c_mask |= SA_PR_COMPMASK_HOPLIMIT;
3188 	}
3189 
3190 	/* Is TClass Specified. */
3191 	if (attrp->ipa_tclass) {
3192 		pathrec_req.TClass = attrp->ipa_tclass;
3193 		c_mask |= SA_PR_COMPMASK_TCLASS;
3194 	}
3195 
3196 	/* Is SL specified. */
3197 	if (attrp->ipa_sl) {
3198 		pathrec_req.SL = attrp->ipa_sl;
3199 		c_mask |= SA_PR_COMPMASK_SL;
3200 	}
3201 
3202 	/* If IBT_PATH_PERF is set, then mark all selectors to BEST. */
3203 	if (p_arg->flags & IBT_PATH_PERF) {
3204 		pathrec_req.PacketLifeTimeSelector = IBT_BEST;
3205 		pathrec_req.MtuSelector = IBT_BEST;
3206 		pathrec_req.RateSelector = IBT_BEST;
3207 
3208 		c_mask |= SA_PR_COMPMASK_PKTLTSELECTOR |
3209 		    SA_PR_COMPMASK_RATESELECTOR | SA_PR_COMPMASK_MTUSELECTOR;
3210 	} else {
3211 		if (attrp->ipa_pkt_lt.p_selector == IBT_BEST) {
3212 			pathrec_req.PacketLifeTimeSelector = IBT_BEST;
3213 			c_mask |= SA_PR_COMPMASK_PKTLTSELECTOR;
3214 		}
3215 
3216 		if (attrp->ipa_srate.r_selector == IBT_BEST) {
3217 			pathrec_req.RateSelector = IBT_BEST;
3218 			c_mask |= SA_PR_COMPMASK_RATESELECTOR;
3219 		}
3220 
3221 		if (attrp->ipa_mtu.r_selector == IBT_BEST) {
3222 			pathrec_req.MtuSelector = IBT_BEST;
3223 			c_mask |= SA_PR_COMPMASK_MTUSELECTOR;
3224 		}
3225 	}
3226 
3227 	/*
3228 	 * Honor individual selection of these attributes,
3229 	 * even if IBT_PATH_PERF is set.
3230 	 */
3231 	/* Check out whether Packet Life Time is specified. */
3232 	if (attrp->ipa_pkt_lt.p_pkt_lt) {
3233 		pathrec_req.PacketLifeTime =
3234 		    ibt_usec2ib(attrp->ipa_pkt_lt.p_pkt_lt);
3235 		pathrec_req.PacketLifeTimeSelector =
3236 		    attrp->ipa_pkt_lt.p_selector;
3237 
3238 		c_mask |= SA_PR_COMPMASK_PKTLT | SA_PR_COMPMASK_PKTLTSELECTOR;
3239 	}
3240 
3241 	/* Is SRATE specified. */
3242 	if (attrp->ipa_srate.r_srate) {
3243 		pathrec_req.Rate = attrp->ipa_srate.r_srate;
3244 		pathrec_req.RateSelector = attrp->ipa_srate.r_selector;
3245 
3246 		c_mask |= SA_PR_COMPMASK_RATE | SA_PR_COMPMASK_RATESELECTOR;
3247 	}
3248 
3249 	/* Is MTU specified. */
3250 	if (attrp->ipa_mtu.r_mtu) {
3251 		pathrec_req.Mtu = attrp->ipa_mtu.r_mtu;
3252 		pathrec_req.MtuSelector = attrp->ipa_mtu.r_selector;
3253 
3254 		c_mask |= SA_PR_COMPMASK_MTU | SA_PR_COMPMASK_MTUSELECTOR;
3255 	}
3256 
3257 	/* We always get REVERSIBLE paths. */
3258 	pathrec_req.Reversible = 1;
3259 	c_mask |= SA_PR_COMPMASK_REVERSIBLE;
3260 
3261 	pathrec_req.NumbPath = *num_path;
3262 	c_mask |= SA_PR_COMPMASK_NUMBPATH;
3263 
3264 	p_fnd = found = 0;
3265 
3266 	for (i = 0; i < sl->p_count; i++) {
3267 		/* SGID */
3268 		pathrec_req.SGID = sl[i].p_sgid;
3269 		c_mask |= SA_PR_COMPMASK_SGID;
3270 		saa_handle = sl[i].p_saa_hdl;
3271 
3272 		for (k = 0; k < dinfo->num_dest; k++) {
3273 			if (pathrec_req.SGID.gid_prefix !=
3274 			    dinfo->d_gid[k].gid_prefix) {
3275 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr: "
3276 				    "SGID_pfx=%llX DGID_pfx=%llX doesn't match",
3277 				    pathrec_req.SGID.gid_prefix,
3278 				    dinfo->d_gid[k].gid_prefix);
3279 				continue;
3280 			}
3281 
3282 			pathrec_req.DGID = dinfo->d_gid[k];
3283 			c_mask |= SA_PR_COMPMASK_DGID;
3284 
3285 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr: "
3286 			    "Get %d Path(s) between\n SGID %llX:%llX "
3287 			    "DGID %llX:%llX", pathrec_req.NumbPath,
3288 			    pathrec_req.SGID.gid_prefix,
3289 			    pathrec_req.SGID.gid_guid,
3290 			    pathrec_req.DGID.gid_prefix,
3291 			    pathrec_req.DGID.gid_guid);
3292 
3293 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr: CMask=0x%llX, "
3294 			    "PKey=0x%X", c_mask, pathrec_req.P_Key);
3295 
3296 			/* Contact SA Access to retrieve Path Records. */
3297 			access_args.sq_attr_id = SA_PATHRECORD_ATTRID;
3298 			access_args.sq_template = &pathrec_req;
3299 			access_args.sq_access_type = IBMF_SAA_RETRIEVE;
3300 			access_args.sq_template_length =
3301 			    sizeof (sa_path_record_t);
3302 			access_args.sq_component_mask = c_mask;
3303 			access_args.sq_callback = NULL;
3304 			access_args.sq_callback_arg = NULL;
3305 
3306 			retval = ibcm_contact_sa_access(saa_handle,
3307 			    &access_args, &length, &results_p);
3308 			if (retval != IBT_SUCCESS) {
3309 				*num_path = 0;
3310 				return (retval);
3311 			}
3312 
3313 			num_rec = length / sizeof (sa_path_record_t);
3314 
3315 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr: "
3316 			    "FOUND %d/%d path requested", num_rec, *num_path);
3317 
3318 			if ((results_p == NULL) || (num_rec == 0))
3319 				continue;
3320 
3321 			/* Update the PathInfo from the response. */
3322 			pr_resp = (sa_path_record_t *)results_p;
3323 			for (j = 0; j < num_rec; j++, pr_resp++) {
3324 				if ((p_fnd != 0) &&
3325 				    (p_arg->flags & IBT_PATH_APM)) {
3326 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr"
3327 					    ": Fill Alternate Path");
3328 					retval = ibcm_update_cep_info(pr_resp,
3329 					    sl, NULL,
3330 					    &paths[found - 1].pi_alt_cep_path);
3331 					if (retval != IBT_SUCCESS)
3332 						continue;
3333 
3334 					/* Update some leftovers */
3335 					paths[found - 1].pi_alt_pkt_lt =
3336 					    pr_resp->PacketLifeTime;
3337 					p_fnd = 0;
3338 				} else {
3339 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr"
3340 					    ": Fill Primary Path");
3341 
3342 					if (found == *num_path)
3343 						break;
3344 
3345 					retval = ibcm_ip_update_pri(pr_resp, sl,
3346 					    &paths[found]);
3347 					if (retval != IBT_SUCCESS)
3348 						continue;
3349 					p_fnd = 1;
3350 					found++;
3351 				}
3352 
3353 			}
3354 			/* Deallocate the memory for results_p. */
3355 			kmem_free(results_p, length);
3356 		}
3357 	}
3358 
3359 	if (found == 0)
3360 		retval = IBT_PATH_RECORDS_NOT_FOUND;
3361 	else if (found != *num_path)
3362 		retval = IBT_INSUFF_DATA;
3363 	else
3364 		retval = IBT_SUCCESS;
3365 
3366 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_spr: done. Status %d, "
3367 	    "Found %d/%d Paths", retval, found, *num_path);
3368 
3369 	*num_path = found;
3370 
3371 	return (retval);
3372 }
3373 
3374 
3375 static ibt_status_t
3376 ibcm_get_ip_mpr(ibcm_ip_path_tqargs_t *p_arg, ibtl_cm_port_list_t *sl,
3377     ibcm_ip_dinfo_t *dinfo, uint8_t *num_path, ibt_path_info_t *paths)
3378 {
3379 	sa_multipath_record_t	*mpr_req;
3380 	sa_path_record_t	*pr_resp;
3381 	ibmf_saa_access_args_t	access_args;
3382 	void			*results_p;
3383 	uint64_t		c_mask = 0;
3384 	ib_gid_t		*gid_ptr, *gid_s_ptr;
3385 	size_t			length;
3386 	int			template_len;
3387 	uint8_t			found, num_rec;
3388 	int			i;
3389 	ibt_status_t		retval;
3390 	uint8_t			sgid_cnt, dgid_cnt;
3391 	ibt_ip_path_attr_t	*attrp = &p_arg->attr;
3392 
3393 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr(%p, %p, %p, %d)",
3394 	    attrp, sl, dinfo, *num_path);
3395 
3396 	dgid_cnt = dinfo->num_dest;
3397 	sgid_cnt = sl->p_count;
3398 
3399 	if ((sgid_cnt == 0) || (dgid_cnt == 0)) {
3400 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_ip_mpr: sgid_cnt(%d) or"
3401 		    " dgid_cnt(%d) is zero", sgid_cnt, dgid_cnt);
3402 		return (IBT_INVALID_PARAM);
3403 	}
3404 
3405 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: Get %d records between "
3406 	    "%d Src(s) <=> %d Dest(s)", *num_path, sgid_cnt, dgid_cnt);
3407 
3408 	/*
3409 	 * Calculate the size for multi-path records template, which includes
3410 	 * constant portion of the multipath record, plus variable size for
3411 	 * SGID (sgid_cnt) and DGID (dgid_cnt).
3412 	 */
3413 	template_len = ((dgid_cnt + sgid_cnt) * sizeof (ib_gid_t)) +
3414 	    sizeof (sa_multipath_record_t);
3415 
3416 	mpr_req = kmem_zalloc(template_len, KM_SLEEP);
3417 
3418 	ASSERT(mpr_req != NULL);
3419 
3420 	gid_ptr = (ib_gid_t *)(((uchar_t *)mpr_req) +
3421 	    sizeof (sa_multipath_record_t));
3422 
3423 	/* Get the starting pointer where GIDs are stored. */
3424 	gid_s_ptr = gid_ptr;
3425 
3426 	/* SGID */
3427 	for (i = 0; i < sgid_cnt; i++) {
3428 		*gid_ptr = sl[i].p_sgid;
3429 
3430 		IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: SGID[%d] = %llX:%llX",
3431 		    i, gid_ptr->gid_prefix, gid_ptr->gid_guid);
3432 
3433 		gid_ptr++;
3434 	}
3435 
3436 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mpr_req))
3437 
3438 	mpr_req->SGIDCount = sgid_cnt;
3439 	c_mask = SA_MPR_COMPMASK_SGIDCOUNT;
3440 
3441 	/* DGIDs */
3442 	for (i = 0; i < dgid_cnt; i++) {
3443 		*gid_ptr = dinfo->d_gid[i];
3444 
3445 		IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: DGID[%d] = "
3446 		    "%llX:%llX", i, gid_ptr->gid_prefix, gid_ptr->gid_guid);
3447 		gid_ptr++;
3448 	}
3449 
3450 	mpr_req->DGIDCount = dgid_cnt;
3451 	c_mask |= SA_MPR_COMPMASK_DGIDCOUNT;
3452 
3453 	/* Is Flow Label Specified. */
3454 	if (attrp->ipa_flow) {
3455 		mpr_req->FlowLabel = attrp->ipa_flow;
3456 		c_mask |= SA_MPR_COMPMASK_FLOWLABEL;
3457 	}
3458 
3459 	/* Is HopLimit Specified. */
3460 	if (p_arg->flags & IBT_PATH_HOP) {
3461 		mpr_req->HopLimit = attrp->ipa_hop;
3462 		c_mask |= SA_MPR_COMPMASK_HOPLIMIT;
3463 	}
3464 
3465 	/* Is TClass Specified. */
3466 	if (attrp->ipa_tclass) {
3467 		mpr_req->TClass = attrp->ipa_tclass;
3468 		c_mask |= SA_MPR_COMPMASK_TCLASS;
3469 	}
3470 
3471 	/* Is SL specified. */
3472 	if (attrp->ipa_sl) {
3473 		mpr_req->SL = attrp->ipa_sl;
3474 		c_mask |= SA_MPR_COMPMASK_SL;
3475 	}
3476 
3477 	if (p_arg->flags & IBT_PATH_PERF) {
3478 		mpr_req->PacketLifeTimeSelector = IBT_BEST;
3479 		mpr_req->RateSelector = IBT_BEST;
3480 		mpr_req->MtuSelector = IBT_BEST;
3481 
3482 		c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR |
3483 		    SA_MPR_COMPMASK_RATESELECTOR | SA_MPR_COMPMASK_MTUSELECTOR;
3484 	} else {
3485 		if (attrp->ipa_pkt_lt.p_selector == IBT_BEST) {
3486 			mpr_req->PacketLifeTimeSelector = IBT_BEST;
3487 			c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR;
3488 		}
3489 
3490 		if (attrp->ipa_srate.r_selector == IBT_BEST) {
3491 			mpr_req->RateSelector = IBT_BEST;
3492 			c_mask |= SA_MPR_COMPMASK_RATESELECTOR;
3493 		}
3494 
3495 		if (attrp->ipa_mtu.r_selector == IBT_BEST) {
3496 			mpr_req->MtuSelector = IBT_BEST;
3497 			c_mask |= SA_MPR_COMPMASK_MTUSELECTOR;
3498 		}
3499 	}
3500 
3501 	/*
3502 	 * Honor individual selection of these attributes,
3503 	 * even if IBT_PATH_PERF is set.
3504 	 */
3505 	/* Check out whether Packet Life Time is specified. */
3506 	if (attrp->ipa_pkt_lt.p_pkt_lt) {
3507 		mpr_req->PacketLifeTime =
3508 		    ibt_usec2ib(attrp->ipa_pkt_lt.p_pkt_lt);
3509 		mpr_req->PacketLifeTimeSelector =
3510 		    attrp->ipa_pkt_lt.p_selector;
3511 
3512 		c_mask |= SA_MPR_COMPMASK_PKTLT |
3513 		    SA_MPR_COMPMASK_PKTLTSELECTOR;
3514 	}
3515 
3516 	/* Is SRATE specified. */
3517 	if (attrp->ipa_srate.r_srate) {
3518 		mpr_req->Rate = attrp->ipa_srate.r_srate;
3519 		mpr_req->RateSelector = attrp->ipa_srate.r_selector;
3520 
3521 		c_mask |= SA_MPR_COMPMASK_RATE |
3522 		    SA_MPR_COMPMASK_RATESELECTOR;
3523 	}
3524 
3525 	/* Is MTU specified. */
3526 	if (attrp->ipa_mtu.r_mtu) {
3527 		mpr_req->Mtu = attrp->ipa_mtu.r_mtu;
3528 		mpr_req->MtuSelector = attrp->ipa_mtu.r_selector;
3529 
3530 		c_mask |= SA_MPR_COMPMASK_MTU |
3531 		    SA_MPR_COMPMASK_MTUSELECTOR;
3532 	}
3533 
3534 	/* We always get REVERSIBLE paths. */
3535 	mpr_req->Reversible = 1;
3536 	c_mask |= SA_MPR_COMPMASK_REVERSIBLE;
3537 
3538 	if (p_arg->flags & IBT_PATH_AVAIL) {
3539 		mpr_req->IndependenceSelector = 1;
3540 		c_mask |= SA_MPR_COMPMASK_INDEPSEL;
3541 	}
3542 
3543 	/* we will not specify how many records we want. */
3544 
3545 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mpr_req))
3546 
3547 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: CMask: %llX Pkey: %X",
3548 	    c_mask, mpr_req->P_Key);
3549 
3550 	/* Contact SA Access to retrieve Path Records. */
3551 	access_args.sq_attr_id = SA_MULTIPATHRECORD_ATTRID;
3552 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
3553 	access_args.sq_component_mask = c_mask;
3554 	access_args.sq_template = mpr_req;
3555 	access_args.sq_template_length = sizeof (sa_multipath_record_t);
3556 	access_args.sq_callback = NULL;
3557 	access_args.sq_callback_arg = NULL;
3558 
3559 	retval = ibcm_contact_sa_access(sl->p_saa_hdl, &access_args, &length,
3560 	    &results_p);
3561 	if (retval != IBT_SUCCESS) {
3562 		*num_path = 0;  /* Update the return count. */
3563 		kmem_free(mpr_req, template_len);
3564 		return (retval);
3565 	}
3566 
3567 	num_rec = length / sizeof (sa_path_record_t);
3568 
3569 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: Found %d Paths", num_rec);
3570 
3571 	found = 0;
3572 	if ((results_p != NULL) && (num_rec > 0)) {
3573 		/* Update the PathInfo with the response Path Records */
3574 		pr_resp = (sa_path_record_t *)results_p;
3575 
3576 		for (i = 0; i < num_rec; i++) {
3577 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: "
3578 			    "P[%d]: SG %llX, DG %llX", i,
3579 			    pr_resp[i].SGID.gid_guid, pr_resp[i].DGID.gid_guid);
3580 		}
3581 
3582 		if (p_arg->flags & IBT_PATH_APM) {
3583 			sa_path_record_t *p_resp = NULL, *a_resp = NULL;
3584 			int		p_found = 0, a_found = 0;
3585 			ib_gid_t	p_sg, a_sg, p_dg, a_dg;
3586 			int		s_spec;
3587 
3588 			s_spec =
3589 			    p_arg->attr.ipa_src_ip.family != AF_UNSPEC ? 1 : 0;
3590 
3591 			p_sg = gid_s_ptr[0];
3592 			if (sgid_cnt > 1)
3593 				a_sg = gid_s_ptr[1];
3594 			else
3595 				a_sg = p_sg;
3596 
3597 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: P_SG: %llX, "
3598 			    "A_SG: %llX", p_sg.gid_guid, a_sg.gid_guid);
3599 
3600 			p_dg = gid_s_ptr[sgid_cnt];
3601 			if (dgid_cnt > 1)
3602 				a_dg = gid_s_ptr[sgid_cnt + 1];
3603 			else
3604 				a_dg = p_dg;
3605 
3606 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: P_DG: %llX, "
3607 			    "A_DG: %llX", p_dg.gid_guid, a_dg.gid_guid);
3608 
3609 			/*
3610 			 * If SGID and/or DGID is specified by user, make sure
3611 			 * he gets his primary-path on those node points.
3612 			 */
3613 			for (i = 0; i < num_rec; i++, pr_resp++) {
3614 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: "
3615 				    "PF %d, AF %d,\n\t\t P[%d] = SG: %llX, "
3616 				    "DG: %llX", p_found, a_found, i,
3617 				    pr_resp->SGID.gid_guid,
3618 				    pr_resp->DGID.gid_guid);
3619 
3620 				if ((!p_found) &&
3621 				    (p_dg.gid_guid == pr_resp->DGID.gid_guid)) {
3622 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr"
3623 					    ": Pri DGID Match.. ");
3624 					if ((s_spec == 0) || (p_sg.gid_guid ==
3625 					    pr_resp->SGID.gid_guid)) {
3626 						p_found = 1;
3627 						p_resp = pr_resp;
3628 						IBTF_DPRINTF_L3(cmlog,
3629 						    "ibcm_get_ip_mpr: "
3630 						    "Primary Path Found");
3631 
3632 						if (a_found)
3633 							break;
3634 						else
3635 							continue;
3636 					}
3637 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr"
3638 					    ": Pri SGID Don't Match.. ");
3639 				}
3640 
3641 				if ((!a_found) &&
3642 				    (a_dg.gid_guid == pr_resp->DGID.gid_guid)) {
3643 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr"
3644 					    ": Alt DGID Match.. ");
3645 					if ((s_spec == 0) || (a_sg.gid_guid ==
3646 					    pr_resp->SGID.gid_guid)) {
3647 						a_found = 1;
3648 						a_resp = pr_resp;
3649 
3650 						IBTF_DPRINTF_L3(cmlog,
3651 						    "ibcm_get_ip_mpr:"
3652 						    "Alternate Path Found ");
3653 
3654 						if (p_found)
3655 							break;
3656 						else
3657 							continue;
3658 					}
3659 					IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr"
3660 					    ": Alt SGID Don't Match.. ");
3661 				}
3662 			}
3663 
3664 			if ((p_found == 0) && (a_found == 0)) {
3665 				IBTF_DPRINTF_L2(cmlog, "ibcm_get_ip_mpr: Path "
3666 				    "to desired node points NOT Available.");
3667 				retval = IBT_PATH_RECORDS_NOT_FOUND;
3668 				goto get_ip_mpr_end;
3669 			}
3670 
3671 			if ((p_resp == NULL) && (a_resp != NULL)) {
3672 				p_resp = a_resp;
3673 				a_resp = NULL;
3674 			}
3675 
3676 			/* Fill in Primary Path */
3677 			retval = ibcm_ip_update_pri(p_resp, sl, &paths[found]);
3678 			if (retval != IBT_SUCCESS)
3679 				goto get_ip_mpr_end;
3680 
3681 			/* Fill in Alternate Path */
3682 			if (a_resp != NULL) {
3683 				/* a_resp will point to AltPathInfo buffer. */
3684 				retval = ibcm_update_cep_info(a_resp, sl,
3685 				    NULL, &paths[found].pi_alt_cep_path);
3686 				if (retval != IBT_SUCCESS)
3687 					goto get_ip_mpr_end;
3688 
3689 				/* Update some leftovers */
3690 				paths[found].pi_alt_pkt_lt =
3691 				    a_resp->PacketLifeTime;
3692 			} else {
3693 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: "
3694 				    "Alternate Path NOT Available.");
3695 				retval = IBT_INSUFF_DATA;
3696 			}
3697 			found++;
3698 		} else {	/* If NOT APM */
3699 			for (i = 0; i < num_rec; i++, pr_resp++) {
3700 				IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: "
3701 				    "DGID(%llX)", pr_resp->DGID.gid_guid);
3702 
3703 				/* Fill in Primary Path */
3704 				retval = ibcm_ip_update_pri(pr_resp, sl,
3705 				    &paths[found]);
3706 				if (retval != IBT_SUCCESS)
3707 					continue;
3708 
3709 				if (++found == *num_path)
3710 					break;
3711 			}
3712 		}
3713 get_ip_mpr_end:
3714 		kmem_free(results_p, length);
3715 	}
3716 	kmem_free(mpr_req, template_len);
3717 
3718 	if (found == 0)
3719 		retval = IBT_PATH_RECORDS_NOT_FOUND;
3720 	else if (found != *num_path)
3721 		retval = IBT_INSUFF_DATA;
3722 	else
3723 		retval = IBT_SUCCESS;
3724 
3725 	IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_mpr: Done (status %d). "
3726 	    "Found %d/%d Paths", retval, found, *num_path);
3727 
3728 	*num_path = found;	/* Update the return count. */
3729 
3730 	return (retval);
3731 }
3732 
3733 
3734 static void
3735 ibcm_process_get_ip_paths(void *tq_arg)
3736 {
3737 	ibcm_ip_path_tqargs_t	*p_arg = (ibcm_ip_path_tqargs_t *)tq_arg;
3738 	ibcm_ip_dinfo_t		*dinfo = NULL;
3739 	int			len = 0;
3740 	uint8_t			max_paths, num_path;
3741 	ib_gid_t		*d_gids_p = NULL;
3742 	ib_gid_t		sgid, dgid1, dgid2;
3743 	ibt_status_t		retval = IBT_SUCCESS;
3744 	ibtl_cm_port_list_t	*sl = NULL;
3745 	uint_t			dnum = 0;
3746 	uint8_t			i;
3747 	ibcm_hca_info_t		*hcap;
3748 	ibmf_saa_handle_t	saa_handle;
3749 	ibt_path_attr_t		attr;
3750 	ibt_ip_addr_t		src_ip_p;
3751 
3752 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_ip_paths(%p, 0x%X) ",
3753 	    p_arg, p_arg->flags);
3754 
3755 	max_paths = num_path = p_arg->attr.ipa_max_paths;
3756 
3757 	/*
3758 	 * Prepare the Source and Destination GID list based on the input
3759 	 * attributes.  We contact ARP module to perform IP to MAC
3760 	 * i.e. GID conversion.  We use this GID for path look-up.
3761 	 *
3762 	 * If APM is requested and if multiple Dest IPs are specified, check
3763 	 * out whether they are companion to each other.  But, if only one
3764 	 * Dest IP is specified, then it is beyond our scope to verify that
3765 	 * the companion port GID obtained has IP-Service enabled.
3766 	 */
3767 	dgid1.gid_prefix = dgid1.gid_guid = 0;
3768 	sgid.gid_prefix = sgid.gid_guid = 0;
3769 
3770 	retval = ibcm_arp_get_ibaddr(getzoneid(), p_arg->attr.ipa_src_ip,
3771 	    p_arg->attr.ipa_dst_ip[0], &sgid, &dgid1, &src_ip_p);
3772 	if (retval) {
3773 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_ip_paths: "
3774 		    "ibcm_arp_get_ibaddr() failed: %d", retval);
3775 		goto ippath_error;
3776 	}
3777 
3778 	bzero(&attr, sizeof (ibt_path_attr_t));
3779 	attr.pa_hca_guid = p_arg->attr.ipa_hca_guid;
3780 	attr.pa_hca_port_num = p_arg->attr.ipa_hca_port_num;
3781 	attr.pa_sgid = sgid;
3782 	bcopy(&p_arg->attr.ipa_mtu, &attr.pa_mtu, sizeof (ibt_mtu_req_t));
3783 	bcopy(&p_arg->attr.ipa_srate, &attr.pa_srate, sizeof (ibt_srate_req_t));
3784 	bcopy(&p_arg->attr.ipa_pkt_lt, &attr.pa_pkt_lt,
3785 	    sizeof (ibt_pkt_lt_req_t));
3786 	retval = ibtl_cm_get_active_plist(&attr, p_arg->flags, &sl);
3787 	if (retval == IBT_SUCCESS) {
3788 		bcopy(&src_ip_p, &sl->p_src_ip, sizeof (ibt_ip_addr_t));
3789 	} else {
3790 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_ip_paths: "
3791 		    "ibtl_cm_get_active_plist: Failed %d", retval);
3792 		goto ippath_error;
3793 	}
3794 
3795 	IBTF_DPRINTF_L4(cmlog, "ibcm_process_get_ip_paths: SGID %llX:%llX, "
3796 	    "DGID0: %llX:%llX", sl->p_sgid.gid_prefix, sl->p_sgid.gid_guid,
3797 	    dgid1.gid_prefix, dgid1.gid_guid);
3798 
3799 	len = p_arg->attr.ipa_ndst - 1;
3800 	len = (len * sizeof (ib_gid_t)) + sizeof (ibcm_ip_dinfo_t);
3801 	dinfo = kmem_zalloc(len, KM_SLEEP);
3802 
3803 	dinfo->d_gid[0] = dgid1;
3804 
3805 	i = 1;
3806 	if (p_arg->attr.ipa_ndst > 1) {
3807 		/* Get DGID for all specified Dest IP Addr */
3808 		for (; i < p_arg->attr.ipa_ndst; i++) {
3809 			retval = ibcm_arp_get_ibaddr(getzoneid(),
3810 			    p_arg->attr.ipa_src_ip, p_arg->attr.ipa_dst_ip[i],
3811 			    NULL, &dgid2, NULL);
3812 			if (retval) {
3813 				IBTF_DPRINTF_L2(cmlog,
3814 				    "ibcm_process_get_ip_paths: "
3815 				    "ibcm_arp_get_ibaddr failed: %d", retval);
3816 				goto ippath_error2;
3817 			}
3818 			dinfo->d_gid[i] = dgid2;
3819 
3820 			IBTF_DPRINTF_L4(cmlog, "ibcm_process_get_ip_paths: "
3821 			    "DGID%d: %llX:%llX", i, dgid2.gid_prefix,
3822 			    dgid2.gid_guid);
3823 		}
3824 
3825 		if (p_arg->flags & IBT_PATH_APM) {
3826 			dgid2 = dinfo->d_gid[1];
3827 
3828 			retval = ibcm_get_comp_pgids(dgid1, dgid2, 0,
3829 			    &d_gids_p, &dnum);
3830 			if ((retval != IBT_SUCCESS) &&
3831 			    (retval != IBT_GIDS_NOT_FOUND)) {
3832 				IBTF_DPRINTF_L2(cmlog,
3833 				    "ibcm_process_get_ip_paths: "
3834 				    "Invalid DGIDs specified w/ APM Flag");
3835 				goto ippath_error2;
3836 			}
3837 			IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_ip_paths: "
3838 			    "Found %d Comp DGID", dnum);
3839 
3840 			if (dnum) {
3841 				dinfo->d_gid[i] = d_gids_p[0];
3842 				i++;
3843 			}
3844 		}
3845 	}
3846 
3847 	/* "i" will get us num_dest count. */
3848 	dinfo->num_dest = i;
3849 
3850 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*p_arg))
3851 
3852 	/*
3853 	 * IBTF allocates memory for path_info & src_ip in case of
3854 	 * Async Get IP Paths
3855 	 */
3856 	if (p_arg->func) {   /* Do these only for Async Get Paths */
3857 		p_arg->paths = kmem_zalloc(sizeof (ibt_path_info_t) * max_paths,
3858 		    KM_SLEEP);
3859 		if (p_arg->src_ip_p == NULL)
3860 			p_arg->src_ip_p = kmem_zalloc(
3861 			    sizeof (ibt_path_ip_src_t) * max_paths, KM_SLEEP);
3862 	}
3863 
3864 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*p_arg))
3865 
3866 	IBTF_DPRINTF_L3(cmlog, "ibcm_process_get_ip_paths: HCA (%llX, %d)",
3867 	    sl->p_hca_guid, sl->p_port_num);
3868 
3869 	hcap = ibcm_find_hca_entry(sl->p_hca_guid);
3870 	if (hcap == NULL) {
3871 		IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_ip_paths: "
3872 		    "NO HCA found");
3873 		retval = IBT_HCA_BUSY_DETACHING;
3874 		goto ippath_error2;
3875 	}
3876 
3877 	/* Get SA Access Handle. */
3878 	for (i = 0; i < sl->p_count; i++) {
3879 		if (i == 0) {
3880 			/* Validate whether this HCA supports APM */
3881 			if ((p_arg->flags & IBT_PATH_APM) &&
3882 			    (!(hcap->hca_caps & IBT_HCA_AUTO_PATH_MIG))) {
3883 				IBTF_DPRINTF_L2(cmlog,
3884 				    "ibcm_process_get_ip_paths: HCA (%llX): "
3885 				    "APM NOT SUPPORTED", sl[i].p_hca_guid);
3886 				retval = IBT_APM_NOT_SUPPORTED;
3887 				goto ippath_error3;
3888 			}
3889 		}
3890 
3891 		saa_handle = ibcm_get_saa_handle(hcap, sl[i].p_port_num);
3892 		if (saa_handle == NULL) {
3893 			IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_ip_paths: "
3894 			    "SAA HDL NULL, HCA (%llX:%d) NOT ACTIVE",
3895 			    sl[i].p_hca_guid, sl[i].p_port_num);
3896 			retval = IBT_HCA_PORT_NOT_ACTIVE;
3897 			goto ippath_error3;
3898 		}
3899 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*sl))
3900 		sl[i].p_saa_hdl = saa_handle;
3901 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*sl))
3902 	}
3903 
3904 	/* Get Path Records. */
3905 	retval = ibcm_saa_ip_pr(p_arg, sl, dinfo, &num_path);
3906 
3907 ippath_error3:
3908 	ibcm_dec_hca_acc_cnt(hcap);
3909 
3910 ippath_error2:
3911 	if (dinfo && len)
3912 		kmem_free(dinfo, len);
3913 
3914 ippath_error1:
3915 	if (sl)
3916 		ibtl_cm_free_active_plist(sl);
3917 
3918 ippath_error:
3919 	if ((retval != IBT_SUCCESS) && (retval != IBT_INSUFF_DATA))
3920 		num_path = 0;
3921 
3922 	if (p_arg->num_paths_p != NULL)
3923 		*p_arg->num_paths_p = num_path;
3924 
3925 	if (p_arg->func) {   /* Do these only for Async Get Paths */
3926 		ibt_path_info_t *tmp_path_p;
3927 
3928 		_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*p_arg))
3929 		p_arg->retval = retval;
3930 		_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*p_arg))
3931 
3932 		if (retval == IBT_INSUFF_DATA) {
3933 			/*
3934 			 * We allocated earlier memory based on "max_paths",
3935 			 * but we got lesser path-records, so re-adjust that
3936 			 * buffer so that caller can free the correct memory.
3937 			 */
3938 			tmp_path_p = kmem_alloc(
3939 			    sizeof (ibt_path_info_t) * num_path, KM_SLEEP);
3940 
3941 			bcopy(p_arg->paths, tmp_path_p,
3942 			    num_path * sizeof (ibt_path_info_t));
3943 
3944 			kmem_free(p_arg->paths,
3945 			    sizeof (ibt_path_info_t) * max_paths);
3946 		} else if (retval != IBT_SUCCESS) {
3947 			if (p_arg->paths)
3948 				kmem_free(p_arg->paths,
3949 				    sizeof (ibt_path_info_t) * max_paths);
3950 			if (p_arg->src_ip_p)
3951 				kmem_free(p_arg->src_ip_p,
3952 				    sizeof (ibt_path_ip_src_t) * max_paths);
3953 			tmp_path_p = NULL;
3954 		} else {
3955 			tmp_path_p = p_arg->paths;
3956 		}
3957 		(*(p_arg->func))(p_arg->arg, retval, tmp_path_p, num_path,
3958 		    p_arg->src_ip_p);
3959 
3960 		cv_destroy(&p_arg->ip_cv);
3961 		mutex_destroy(&p_arg->ip_lock);
3962 		len = p_arg->len;
3963 		if (p_arg && len)
3964 			kmem_free(p_arg, len);
3965 	} else {
3966 		mutex_enter(&p_arg->ip_lock);
3967 		p_arg->retval = retval;
3968 		cv_signal(&p_arg->ip_cv);
3969 		mutex_exit(&p_arg->ip_lock);
3970 	}
3971 
3972 	IBTF_DPRINTF_L2(cmlog, "ibcm_process_get_ip_paths: done: status %d, "
3973 	    "Found %d/%d Path Records", retval, num_path, max_paths);
3974 }
3975 
3976 
3977 static ibt_status_t
3978 ibcm_val_ipattr(ibt_ip_path_attr_t *attrp, ibt_path_flags_t flags)
3979 {
3980 	uint_t			i;
3981 
3982 	if (attrp == NULL) {
3983 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: IP Path Attr is NULL");
3984 		return (IBT_INVALID_PARAM);
3985 	}
3986 
3987 	IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: Inputs are: HCA %llX:%d, "
3988 	    "Maxpath= %d, \n Flags= 0x%X, #Dest %d", attrp->ipa_hca_guid,
3989 	    attrp->ipa_hca_port_num, attrp->ipa_max_paths, flags,
3990 	    attrp->ipa_ndst);
3991 
3992 	/*
3993 	 * Validate Path Flags.
3994 	 * IBT_PATH_AVAIL & IBT_PATH_PERF are mutually exclusive.
3995 	 */
3996 	if ((flags & IBT_PATH_AVAIL) && (flags & IBT_PATH_PERF)) {
3997 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: Invalid Flags: 0x%X,"
3998 		    "\n\t AVAIL and PERF flags specified together", flags);
3999 		return (IBT_INVALID_PARAM);
4000 	}
4001 
4002 	/*
4003 	 * Validate number of records requested.
4004 	 *
4005 	 * Max_paths of "0" is invalid.
4006 	 * Max_paths <= IBT_MAX_SPECIAL_PATHS, if AVAIL or PERF is set.
4007 	 */
4008 	if (attrp->ipa_max_paths == 0) {
4009 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: Invalid max_paths %d",
4010 		    attrp->ipa_max_paths);
4011 		return (IBT_INVALID_PARAM);
4012 	}
4013 
4014 	if ((flags & (IBT_PATH_AVAIL | IBT_PATH_PERF)) &&
4015 	    (attrp->ipa_max_paths > IBT_MAX_SPECIAL_PATHS)) {
4016 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: MaxPaths that can be "
4017 		    "requested is <%d> \n when IBT_PATH_AVAIL or IBT_PATH_PERF"
4018 		    " flag is specified.", IBT_MAX_SPECIAL_PATHS);
4019 		return (IBT_INVALID_PARAM);
4020 	}
4021 
4022 	/* Only 2 destinations can be specified w/ APM flag. */
4023 	if ((flags & IBT_PATH_APM) && (attrp->ipa_ndst > 2)) {
4024 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: Max #Dest is 2, with "
4025 		    "APM flag");
4026 		return (IBT_INVALID_PARAM);
4027 	}
4028 
4029 	/* Validate the destination info */
4030 	if ((attrp->ipa_ndst == 0) || (attrp->ipa_ndst == NULL)) {
4031 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: DstIP Not provided "
4032 		    "dst_ip %p, ndst %d", attrp->ipa_dst_ip, attrp->ipa_ndst);
4033 		return (IBT_INVALID_PARAM);
4034 	}
4035 
4036 	/* Basic validation of Source IPADDR (if provided). */
4037 	IBCM_PRINT_IP("ibcm_val_ipattr SrcIP", &attrp->ipa_src_ip);
4038 	if ((attrp->ipa_src_ip.family == AF_INET) &&
4039 	    (attrp->ipa_src_ip.un.ip4addr == htonl(INADDR_LOOPBACK) ||
4040 	    attrp->ipa_src_ip.un.ip4addr == INADDR_ANY)) {
4041 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: SrcIP specified is "
4042 		    "LOOPBACK/ZEROs: NOT SUPPORTED");
4043 		return (IBT_NOT_SUPPORTED);
4044 	} else if ((attrp->ipa_src_ip.family == AF_INET6) &&
4045 	    (IN6_IS_ADDR_UNSPECIFIED(&attrp->ipa_src_ip.un.ip6addr) ||
4046 	    IN6_IS_ADDR_LOOPBACK(&attrp->ipa_src_ip.un.ip6addr))) {
4047 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: SrcIP specified is "
4048 		    "LOOPBACK/ZEROs: NOT SUPPORTED");
4049 		return (IBT_NOT_SUPPORTED);
4050 	}
4051 
4052 	if (ibcm_ip6_linklocal_addr_ok &&
4053 	    (attrp->ipa_src_ip.family == AF_INET6) &&
4054 	    (IN6_IS_ADDR_LINKLOCAL(&attrp->ipa_src_ip.un.ip6addr))) {
4055 		IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: SrcIP specified is "
4056 		    "Link Local Address: NOT SUPPORTED");
4057 		return (IBT_NOT_SUPPORTED);
4058 	}
4059 
4060 	/* Basic validation of Dest IPADDR. */
4061 	for (i = 0; i < attrp->ipa_ndst; i++) {
4062 		ibt_ip_addr_t	dst_ip = attrp->ipa_dst_ip[i];
4063 
4064 		IBCM_PRINT_IP("ibcm_val_ipattr DstIP", &dst_ip);
4065 
4066 		if (dst_ip.family == AF_UNSPEC) {
4067 			IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: ERROR: "
4068 			    "Invalid DstIP specified");
4069 			return (IBT_INVALID_PARAM);
4070 		} else if ((dst_ip.family == AF_INET) &&
4071 		    (dst_ip.un.ip4addr == htonl(INADDR_LOOPBACK) ||
4072 		    dst_ip.un.ip4addr == INADDR_ANY)) {
4073 			IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: DstIP "
4074 			    "specified is LOOPBACK/ZEROs: NOT SUPPORTED");
4075 			return (IBT_NOT_SUPPORTED);
4076 		} else if ((dst_ip.family == AF_INET6) &&
4077 		    (IN6_IS_ADDR_UNSPECIFIED(&dst_ip.un.ip6addr) ||
4078 		    IN6_IS_ADDR_LOOPBACK(&dst_ip.un.ip6addr))) {
4079 			IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: DstIP "
4080 			    "specified is LOOPBACK/ZEROs: NOT SUPPORTED");
4081 			return (IBT_NOT_SUPPORTED);
4082 		}
4083 
4084 		/*
4085 		 * If SrcIP is specified, make sure that SrcIP and DstIP
4086 		 * belong to same family.
4087 		 */
4088 		if ((attrp->ipa_src_ip.family != AF_UNSPEC) &&
4089 		    (attrp->ipa_src_ip.family != dst_ip.family)) {
4090 			IBTF_DPRINTF_L2(cmlog, "ibcm_val_ipattr: ERROR: "
4091 			    "Specified SrcIP (%d) and DstIP(%d) family diffs.",
4092 			    attrp->ipa_src_ip.family, dst_ip.family);
4093 			return (IBT_INVALID_PARAM);
4094 		}
4095 	}
4096 
4097 	return (IBT_SUCCESS);
4098 }
4099 
4100 
4101 static ibt_status_t
4102 ibcm_get_ip_path(ibt_clnt_hdl_t ibt_hdl, ibt_path_flags_t flags,
4103     ibt_ip_path_attr_t *attrp, ibt_path_info_t *paths, uint8_t *num_path_p,
4104     ibt_path_ip_src_t *src_ip_p, ibt_ip_path_handler_t func, void  *arg)
4105 {
4106 	ibcm_ip_path_tqargs_t	*path_tq;
4107 	int		sleep_flag = ((func == NULL) ? KM_SLEEP : KM_NOSLEEP);
4108 	uint_t		len, ret;
4109 	ibt_status_t	retval;
4110 
4111 	retval = ibcm_val_ipattr(attrp, flags);
4112 	if (retval != IBT_SUCCESS)
4113 		return (retval);
4114 
4115 	len = (attrp->ipa_ndst * sizeof (ibt_ip_addr_t)) +
4116 	    sizeof (ibcm_ip_path_tqargs_t);
4117 	path_tq = kmem_zalloc(len, sleep_flag);
4118 	if (path_tq == NULL) {
4119 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_ip_path: "
4120 		    "Unable to allocate memory for local usage.");
4121 		return (IBT_INSUFF_KERNEL_RESOURCE);
4122 	}
4123 
4124 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*path_tq))
4125 	mutex_init(&path_tq->ip_lock, NULL, MUTEX_DEFAULT, NULL);
4126 	cv_init(&path_tq->ip_cv, NULL, CV_DRIVER, NULL);
4127 	bcopy(attrp, &path_tq->attr, sizeof (ibt_ip_path_attr_t));
4128 
4129 	path_tq->attr.ipa_dst_ip = (ibt_ip_addr_t *)(((uchar_t *)path_tq) +
4130 	    sizeof (ibcm_ip_path_tqargs_t));
4131 	bcopy(attrp->ipa_dst_ip, path_tq->attr.ipa_dst_ip,
4132 	    sizeof (ibt_ip_addr_t) * attrp->ipa_ndst);
4133 
4134 	/* Ignore IBT_PATH_AVAIL flag, if only one path is requested. */
4135 	if ((flags & IBT_PATH_AVAIL) && (attrp->ipa_max_paths == 1)) {
4136 		flags &= ~IBT_PATH_AVAIL;
4137 
4138 		IBTF_DPRINTF_L4(cmlog, "ibcm_get_ip_path: Ignoring "
4139 		    "IBT_PATH_AVAIL flag, as only ONE path info is requested.");
4140 	}
4141 
4142 	path_tq->flags = flags;
4143 	path_tq->ibt_hdl = ibt_hdl;
4144 	path_tq->paths = paths;
4145 	path_tq->src_ip_p = src_ip_p;
4146 	path_tq->num_paths_p = num_path_p;
4147 	path_tq->func = func;
4148 	path_tq->arg = arg;
4149 	path_tq->len = len;
4150 
4151 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*path_tq))
4152 
4153 	sleep_flag = ((func == NULL) ? TQ_SLEEP : TQ_NOSLEEP);
4154 	mutex_enter(&path_tq->ip_lock);
4155 	ret = taskq_dispatch(ibcm_taskq, ibcm_process_get_ip_paths, path_tq,
4156 	    sleep_flag);
4157 	if (ret == 0) {
4158 		IBTF_DPRINTF_L2(cmlog, "ibcm_get_ip_path: Failed to dispatch "
4159 		    "the TaskQ");
4160 		mutex_exit(&path_tq->ip_lock);
4161 		cv_destroy(&path_tq->ip_cv);
4162 		mutex_destroy(&path_tq->ip_lock);
4163 		kmem_free(path_tq, len);
4164 		retval = IBT_INSUFF_KERNEL_RESOURCE;
4165 	} else {
4166 		if (func != NULL) {		/* Non-Blocking */
4167 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_path: NonBlocking");
4168 			retval = IBT_SUCCESS;
4169 			mutex_exit(&path_tq->ip_lock);
4170 		} else {		/* Blocking */
4171 			IBTF_DPRINTF_L3(cmlog, "ibcm_get_ip_path: Blocking");
4172 			cv_wait(&path_tq->ip_cv, &path_tq->ip_lock);
4173 			retval = path_tq->retval;
4174 			mutex_exit(&path_tq->ip_lock);
4175 			cv_destroy(&path_tq->ip_cv);
4176 			mutex_destroy(&path_tq->ip_lock);
4177 			kmem_free(path_tq, len);
4178 		}
4179 	}
4180 
4181 	return (retval);
4182 }
4183 
4184 
4185 ibt_status_t
4186 ibt_aget_ip_paths(ibt_clnt_hdl_t ibt_hdl, ibt_path_flags_t flags,
4187     ibt_ip_path_attr_t *attrp, ibt_ip_path_handler_t func, void  *arg)
4188 {
4189 	IBTF_DPRINTF_L3(cmlog, "ibt_aget_ip_paths(%p (%s), 0x%X, %p, %p, %p)",
4190 	    ibt_hdl, ibtl_cm_get_clnt_name(ibt_hdl), flags, attrp, func, arg);
4191 
4192 	if (func == NULL) {
4193 		IBTF_DPRINTF_L2(cmlog, "ibt_aget_ip_paths: Function Pointer is "
4194 		    "NULL - ERROR ");
4195 		return (IBT_INVALID_PARAM);
4196 	}
4197 
4198 	/* path info will be allocated in ibcm_process_get_ip_paths() */
4199 	return (ibcm_get_ip_path(ibt_hdl, flags, attrp, NULL, NULL,
4200 	    NULL, func, arg));
4201 }
4202 
4203 
4204 ibt_status_t
4205 ibt_get_ip_paths(ibt_clnt_hdl_t ibt_hdl, ibt_path_flags_t flags,
4206     ibt_ip_path_attr_t *attrp, ibt_path_info_t *paths, uint8_t *num_paths_p,
4207     ibt_path_ip_src_t *src_ip_p)
4208 {
4209 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_paths(%p(%s), 0x%X, %p, %p, %p, %p)",
4210 	    ibt_hdl, ibtl_cm_get_clnt_name(ibt_hdl), flags, attrp, paths,
4211 	    num_paths_p, src_ip_p);
4212 
4213 	if (paths == NULL) {
4214 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_paths: Path Info Pointer is "
4215 		    "NULL - ERROR ");
4216 		return (IBT_INVALID_PARAM);
4217 	}
4218 
4219 	if (num_paths_p != NULL)
4220 		*num_paths_p = 0;
4221 
4222 	return (ibcm_get_ip_path(ibt_hdl, flags, attrp, paths, num_paths_p,
4223 	    src_ip_p, NULL, NULL));
4224 }
4225 
4226 
4227 ibt_status_t
4228 ibt_get_ip_alt_path(ibt_channel_hdl_t rc_chan, ibt_path_flags_t flags,
4229     ibt_alt_ip_path_attr_t *attrp, ibt_alt_path_info_t *api_p)
4230 {
4231 	sa_multipath_record_t	*mpr_req;
4232 	sa_path_record_t	*pr_resp;
4233 	ibmf_saa_access_args_t	access_args;
4234 	ibt_qp_query_attr_t	qp_attr;
4235 	ibtl_cm_hca_port_t	c_hp, n_hp;
4236 	ibcm_hca_info_t		*hcap;
4237 	void			*results_p;
4238 	uint64_t		c_mask = 0;
4239 	ib_gid_t		*gid_ptr = NULL;
4240 	ib_gid_t		*sgids_p = NULL,  *dgids_p = NULL;
4241 	ib_gid_t		cur_dgid, cur_sgid;
4242 	ib_gid_t		new_dgid, new_sgid;
4243 	ibmf_saa_handle_t	saa_handle;
4244 	size_t			length;
4245 	int			i, j, template_len, rec_found;
4246 	uint_t			snum = 0, dnum = 0, num_rec;
4247 	ibt_status_t		retval;
4248 	ib_mtu_t		prim_mtu;
4249 
4250 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path(%p, %x, %p, %p)",
4251 	    rc_chan, flags, attrp, api_p);
4252 
4253 	/* validate channel */
4254 	if (IBCM_INVALID_CHANNEL(rc_chan)) {
4255 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: invalid channel");
4256 		return (IBT_CHAN_HDL_INVALID);
4257 	}
4258 
4259 	if (api_p == NULL) {
4260 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: invalid attribute:"
4261 		    " AltPathInfo can't be NULL");
4262 		return (IBT_INVALID_PARAM);
4263 	}
4264 
4265 	retval = ibt_query_qp(rc_chan, &qp_attr);
4266 	if (retval != IBT_SUCCESS) {
4267 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: ibt_query_qp(%p) "
4268 		    "failed %d", rc_chan, retval);
4269 		return (retval);
4270 	}
4271 
4272 	if (qp_attr.qp_info.qp_trans != IBT_RC_SRV) {
4273 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4274 		    "Invalid Channel type: Applicable only to RC Channel");
4275 		return (IBT_CHAN_SRV_TYPE_INVALID);
4276 	}
4277 
4278 	cur_dgid =
4279 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_adds_vect.av_dgid;
4280 	cur_sgid =
4281 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_adds_vect.av_sgid;
4282 	prim_mtu = qp_attr.qp_info.qp_transport.rc.rc_path_mtu;
4283 
4284 	/* If optional attributes are specified, validate them. */
4285 	if (attrp) {
4286 		/* Get SGID and DGID for the specified input ip-addr */
4287 		retval = ibcm_arp_get_ibaddr(getzoneid(), attrp->apa_src_ip,
4288 		    attrp->apa_dst_ip, &new_sgid, &new_dgid, NULL);
4289 		if (retval) {
4290 			IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4291 			    "ibcm_arp_get_ibaddr() failed: %d", retval);
4292 			return (retval);
4293 		}
4294 	} else {
4295 		new_dgid.gid_prefix = 0;
4296 		new_dgid.gid_guid = 0;
4297 		new_sgid.gid_prefix = 0;
4298 		new_sgid.gid_guid = 0;
4299 	}
4300 
4301 	if ((new_dgid.gid_prefix != 0) && (new_sgid.gid_prefix != 0) &&
4302 	    (new_dgid.gid_prefix != new_sgid.gid_prefix)) {
4303 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: Specified SGID's "
4304 		    "SNprefix (%llX) doesn't match with \n specified DGID's "
4305 		    "SNprefix: %llX", new_sgid.gid_prefix, new_dgid.gid_prefix);
4306 		return (IBT_INVALID_PARAM);
4307 	}
4308 
4309 	/* For the specified SGID, get HCA information. */
4310 	retval = ibtl_cm_get_hca_port(cur_sgid, 0, &c_hp);
4311 	if (retval != IBT_SUCCESS) {
4312 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4313 		    "Get HCA Port Failed: %d", retval);
4314 		return (retval);
4315 	}
4316 
4317 	hcap = ibcm_find_hca_entry(c_hp.hp_hca_guid);
4318 	if (hcap == NULL) {
4319 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: NO HCA found");
4320 		return (IBT_HCA_BUSY_DETACHING);
4321 	}
4322 
4323 	/* Validate whether this HCA support APM */
4324 	if (!(hcap->hca_caps & IBT_HCA_AUTO_PATH_MIG)) {
4325 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4326 		    "HCA (%llX) - APM NOT SUPPORTED ", c_hp.hp_hca_guid);
4327 		retval = IBT_APM_NOT_SUPPORTED;
4328 		goto get_ip_alt_path_done;
4329 	}
4330 
4331 	/* Get Companion Port GID of the current Channel's SGID */
4332 	if ((new_sgid.gid_guid == 0) || ((new_sgid.gid_guid != 0) &&
4333 	    (new_sgid.gid_guid != cur_sgid.gid_guid))) {
4334 		IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: SRC: "
4335 		    "Get Companion PortGids for - %llX:%llX",
4336 		    cur_sgid.gid_prefix, cur_sgid.gid_guid);
4337 
4338 		retval = ibcm_get_comp_pgids(cur_sgid, new_sgid,
4339 		    c_hp.hp_hca_guid, &sgids_p, &snum);
4340 		if (retval != IBT_SUCCESS)
4341 			goto get_ip_alt_path_done;
4342 	}
4343 
4344 	/* Get Companion Port GID of the current Channel's DGID */
4345 	if ((new_dgid.gid_guid == 0) || ((new_dgid.gid_guid != 0) &&
4346 	    (new_dgid.gid_guid != cur_dgid.gid_guid))) {
4347 
4348 		IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: DEST: "
4349 		    "Get Companion PortGids for - %llX:%llX",
4350 		    cur_dgid.gid_prefix, cur_dgid.gid_guid);
4351 
4352 		retval = ibcm_get_comp_pgids(cur_dgid, new_dgid, 0, &dgids_p,
4353 		    &dnum);
4354 		if (retval != IBT_SUCCESS)
4355 			goto get_ip_alt_path_done;
4356 	}
4357 
4358 	if ((new_dgid.gid_guid == 0) || (new_sgid.gid_guid == 0)) {
4359 		if (new_sgid.gid_guid == 0) {
4360 			for (i = 0; i < snum; i++) {
4361 				if (new_dgid.gid_guid == 0) {
4362 					for (j = 0; j < dnum; j++) {
4363 						if (sgids_p[i].gid_prefix ==
4364 						    dgids_p[j].gid_prefix) {
4365 							new_dgid = dgids_p[j];
4366 							new_sgid = sgids_p[i];
4367 
4368 							goto get_ip_alt_proceed;
4369 						}
4370 					}
4371 					/*  Current DGID */
4372 					if (sgids_p[i].gid_prefix ==
4373 					    cur_dgid.gid_prefix) {
4374 						new_sgid = sgids_p[i];
4375 						goto get_ip_alt_proceed;
4376 					}
4377 				} else {
4378 					if (sgids_p[i].gid_prefix ==
4379 					    new_dgid.gid_prefix) {
4380 						new_sgid = sgids_p[i];
4381 						goto get_ip_alt_proceed;
4382 					}
4383 				}
4384 			}
4385 			/* Current SGID */
4386 			if (new_dgid.gid_guid == 0) {
4387 				for (j = 0; j < dnum; j++) {
4388 					if (cur_sgid.gid_prefix ==
4389 					    dgids_p[j].gid_prefix) {
4390 						new_dgid = dgids_p[j];
4391 
4392 						goto get_ip_alt_proceed;
4393 					}
4394 				}
4395 			}
4396 		} else if (new_dgid.gid_guid == 0) {
4397 			for (i = 0; i < dnum; i++) {
4398 				if (dgids_p[i].gid_prefix ==
4399 				    new_sgid.gid_prefix) {
4400 					new_dgid = dgids_p[i];
4401 					goto get_ip_alt_proceed;
4402 				}
4403 			}
4404 			/* Current DGID */
4405 			if (cur_dgid.gid_prefix == new_sgid.gid_prefix) {
4406 				goto get_ip_alt_proceed;
4407 			}
4408 		}
4409 		/*
4410 		 * hmm... No Companion Ports available.
4411 		 * so we will be using current or specified attributes only.
4412 		 */
4413 	}
4414 
4415 get_ip_alt_proceed:
4416 	if (new_sgid.gid_guid != 0) {
4417 		retval = ibtl_cm_get_hca_port(new_sgid, 0, &n_hp);
4418 		if (retval != IBT_SUCCESS) {
4419 			IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4420 			    "Get HCA Port Failed: %d", retval);
4421 			goto get_ip_alt_path_done;
4422 		}
4423 	}
4424 
4425 	/* Calculate the size for multi-path records template */
4426 	template_len = (2 * sizeof (ib_gid_t)) + sizeof (sa_multipath_record_t);
4427 
4428 	mpr_req = kmem_zalloc(template_len, KM_SLEEP);
4429 
4430 	ASSERT(mpr_req != NULL);
4431 
4432 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*mpr_req))
4433 
4434 	gid_ptr = (ib_gid_t *)(((uchar_t *)mpr_req) +
4435 	    sizeof (sa_multipath_record_t));
4436 
4437 	/* SGID */
4438 	if (new_sgid.gid_guid == 0)
4439 		*gid_ptr = cur_sgid;
4440 	else
4441 		*gid_ptr = new_sgid;
4442 
4443 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: Get Path Between "
4444 	    " SGID : %llX:%llX", gid_ptr->gid_prefix, gid_ptr->gid_guid);
4445 
4446 	gid_ptr++;
4447 
4448 	/* DGID */
4449 	if (new_dgid.gid_guid == 0)
4450 		*gid_ptr = cur_dgid;
4451 	else
4452 		*gid_ptr = new_dgid;
4453 
4454 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path:\t\t    DGID : %llX:%llX",
4455 	    gid_ptr->gid_prefix, gid_ptr->gid_guid);
4456 
4457 	mpr_req->SGIDCount = 1;
4458 	c_mask = SA_MPR_COMPMASK_SGIDCOUNT;
4459 
4460 	mpr_req->DGIDCount = 1;
4461 	c_mask |= SA_MPR_COMPMASK_DGIDCOUNT;
4462 
4463 	/* Is Flow Label Specified. */
4464 	if (attrp) {
4465 		if (attrp->apa_flow) {
4466 			mpr_req->FlowLabel = attrp->apa_flow;
4467 			c_mask |= SA_MPR_COMPMASK_FLOWLABEL;
4468 		}
4469 
4470 		/* Is HopLimit Specified. */
4471 		if (flags & IBT_PATH_HOP) {
4472 			mpr_req->HopLimit = attrp->apa_hop;
4473 			c_mask |= SA_MPR_COMPMASK_HOPLIMIT;
4474 		}
4475 
4476 		/* Is TClass Specified. */
4477 		if (attrp->apa_tclass) {
4478 			mpr_req->TClass = attrp->apa_tclass;
4479 			c_mask |= SA_MPR_COMPMASK_TCLASS;
4480 		}
4481 
4482 		/* Is SL specified. */
4483 		if (attrp->apa_sl) {
4484 			mpr_req->SL = attrp->apa_sl;
4485 			c_mask |= SA_MPR_COMPMASK_SL;
4486 		}
4487 
4488 		if (flags & IBT_PATH_PERF) {
4489 			mpr_req->PacketLifeTimeSelector = IBT_BEST;
4490 			mpr_req->RateSelector = IBT_BEST;
4491 
4492 			c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR |
4493 			    SA_MPR_COMPMASK_RATESELECTOR;
4494 		} else {
4495 			if (attrp->apa_pkt_lt.p_selector == IBT_BEST) {
4496 				mpr_req->PacketLifeTimeSelector = IBT_BEST;
4497 				c_mask |= SA_MPR_COMPMASK_PKTLTSELECTOR;
4498 			}
4499 
4500 			if (attrp->apa_srate.r_selector == IBT_BEST) {
4501 				mpr_req->RateSelector = IBT_BEST;
4502 				c_mask |= SA_MPR_COMPMASK_RATESELECTOR;
4503 			}
4504 		}
4505 
4506 		/*
4507 		 * Honor individual selection of these attributes,
4508 		 * even if IBT_PATH_PERF is set.
4509 		 */
4510 		/* Check out whether Packet Life Time is specified. */
4511 		if (attrp->apa_pkt_lt.p_pkt_lt) {
4512 			mpr_req->PacketLifeTime =
4513 			    ibt_usec2ib(attrp->apa_pkt_lt.p_pkt_lt);
4514 			mpr_req->PacketLifeTimeSelector =
4515 			    attrp->apa_pkt_lt.p_selector;
4516 
4517 			c_mask |= SA_MPR_COMPMASK_PKTLT |
4518 			    SA_MPR_COMPMASK_PKTLTSELECTOR;
4519 		}
4520 
4521 		/* Is SRATE specified. */
4522 		if (attrp->apa_srate.r_srate) {
4523 			mpr_req->Rate = attrp->apa_srate.r_srate;
4524 			mpr_req->RateSelector = attrp->apa_srate.r_selector;
4525 
4526 			c_mask |= SA_MPR_COMPMASK_RATE |
4527 			    SA_MPR_COMPMASK_RATESELECTOR;
4528 		}
4529 	}
4530 
4531 	/* Alt PathMTU can be GT or EQU to current channel's Pri PathMTU */
4532 
4533 	/* P_Key must be same as that of primary path */
4534 	retval = ibt_index2pkey_byguid(c_hp.hp_hca_guid, c_hp.hp_port,
4535 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_pkey_ix,
4536 	    &mpr_req->P_Key);
4537 	if (retval != IBT_SUCCESS) {
4538 		IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: PKeyIdx2Pkey "
4539 		    "Failed: %d", retval);
4540 		goto get_ip_alt_path_done;
4541 	}
4542 	c_mask |= SA_MPR_COMPMASK_PKEY;
4543 
4544 	mpr_req->Reversible = 1;	/* We always get REVERSIBLE paths. */
4545 	mpr_req->IndependenceSelector = 1;
4546 	c_mask |= SA_MPR_COMPMASK_REVERSIBLE | SA_MPR_COMPMASK_INDEPSEL;
4547 
4548 	_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*mpr_req))
4549 
4550 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: CMask: 0x%llX", c_mask);
4551 
4552 	/* NOTE: We will **NOT** specify how many records we want. */
4553 
4554 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: Primary: MTU %d, PKey[%d]="
4555 	    "0x%X\n\tSGID = %llX:%llX, DGID = %llX:%llX", prim_mtu,
4556 	    qp_attr.qp_info.qp_transport.rc.rc_path.cep_pkey_ix, mpr_req->P_Key,
4557 	    cur_sgid.gid_prefix, cur_sgid.gid_guid, cur_dgid.gid_prefix,
4558 	    cur_dgid.gid_guid);
4559 
4560 	/* Get SA Access Handle. */
4561 	if (new_sgid.gid_guid != 0)
4562 		saa_handle = ibcm_get_saa_handle(hcap, n_hp.hp_port);
4563 	else
4564 		saa_handle = ibcm_get_saa_handle(hcap, c_hp.hp_port);
4565 	if (saa_handle == NULL) {
4566 		retval = IBT_HCA_PORT_NOT_ACTIVE;
4567 		goto get_ip_alt_path_done;
4568 	}
4569 
4570 	/* Contact SA Access to retrieve Path Records. */
4571 	access_args.sq_attr_id = SA_MULTIPATHRECORD_ATTRID;
4572 	access_args.sq_access_type = IBMF_SAA_RETRIEVE;
4573 	access_args.sq_component_mask = c_mask;
4574 	access_args.sq_template = mpr_req;
4575 	access_args.sq_template_length = sizeof (sa_multipath_record_t);
4576 	access_args.sq_callback = NULL;
4577 	access_args.sq_callback_arg = NULL;
4578 
4579 	retval = ibcm_contact_sa_access(saa_handle, &access_args, &length,
4580 	    &results_p);
4581 	if (retval != IBT_SUCCESS) {
4582 		goto get_ip_alt_path_done;
4583 	}
4584 
4585 	num_rec = length / sizeof (sa_path_record_t);
4586 
4587 	kmem_free(mpr_req, template_len);
4588 
4589 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: Found %d Paths", num_rec);
4590 
4591 	rec_found = 0;
4592 	if ((results_p != NULL) && (num_rec > 0)) {
4593 		/* Update the PathInfo with the response Path Records */
4594 		pr_resp = (sa_path_record_t *)results_p;
4595 		for (i = 0; i < num_rec; i++, pr_resp++) {
4596 			if (prim_mtu > pr_resp->Mtu) {
4597 				IBTF_DPRINTF_L2(cmlog, "ibt_get_ip_alt_path: "
4598 				    "Alt PathMTU(%d) must be GT or EQU to Pri "
4599 				    "PathMTU(%d). Ignore this rec",
4600 				    pr_resp->Mtu, prim_mtu);
4601 				continue;
4602 			}
4603 
4604 			if ((new_sgid.gid_guid == 0) &&
4605 			    (new_dgid.gid_guid == 0)) {
4606 				/* Reject PathRec if it same as Primary Path. */
4607 				if (ibcm_compare_paths(pr_resp,
4608 				    &qp_attr.qp_info.qp_transport.rc.rc_path,
4609 				    &c_hp)) {
4610 					IBTF_DPRINTF_L3(cmlog,
4611 					    "ibt_get_ip_alt_path: PathRec "
4612 					    "obtained is similar to Prim Path, "
4613 					    "ignore this record");
4614 					continue;
4615 				}
4616 			}
4617 
4618 			if (new_sgid.gid_guid == 0) {
4619 				retval = ibcm_update_cep_info(pr_resp, NULL,
4620 				    &c_hp, &api_p->ap_alt_cep_path);
4621 			} else {
4622 				retval = ibcm_update_cep_info(pr_resp, NULL,
4623 				    &n_hp, &api_p->ap_alt_cep_path);
4624 			}
4625 			if (retval != IBT_SUCCESS)
4626 				continue;
4627 
4628 			/* Update some leftovers */
4629 			_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*api_p))
4630 
4631 			api_p->ap_alt_pkt_lt = pr_resp->PacketLifeTime;
4632 
4633 			_NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*api_p))
4634 
4635 			rec_found = 1;
4636 			break;
4637 		}
4638 		kmem_free(results_p, length);
4639 	}
4640 
4641 	if (rec_found == 0) {
4642 		IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: AltPath cannot"
4643 		    " be established");
4644 		retval = IBT_PATH_RECORDS_NOT_FOUND;
4645 	} else
4646 		retval = IBT_SUCCESS;
4647 
4648 get_ip_alt_path_done:
4649 	if ((snum) && (sgids_p))
4650 		kmem_free(sgids_p, snum * sizeof (ib_gid_t));
4651 
4652 	if ((dnum) && (dgids_p))
4653 		kmem_free(dgids_p, dnum * sizeof (ib_gid_t));
4654 
4655 	ibcm_dec_hca_acc_cnt(hcap);
4656 
4657 	IBTF_DPRINTF_L3(cmlog, "ibt_get_ip_alt_path: Done (status %d)", retval);
4658 
4659 	return (retval);
4660 }
4661 
4662 
4663 /* Routines for warlock */
4664 
4665 /* ARGSUSED */
4666 static void
4667 ibcm_dummy_path_handler(void *arg, ibt_status_t retval, ibt_path_info_t *paths,
4668     uint8_t num_path)
4669 {
4670 	ibcm_path_tqargs_t	dummy_path;
4671 
4672 	dummy_path.func = ibcm_dummy_path_handler;
4673 
4674 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_path_handler: "
4675 	    "dummy_path.func %p", dummy_path.func);
4676 }
4677 
4678 /* ARGSUSED */
4679 static void
4680 ibcm_dummy_ip_path_handler(void *arg, ibt_status_t retval,
4681     ibt_path_info_t *paths, uint8_t num_path, ibt_path_ip_src_t *src_ip)
4682 {
4683 	ibcm_ip_path_tqargs_t	dummy_path;
4684 
4685 	dummy_path.func = ibcm_dummy_ip_path_handler;
4686 
4687 	IBTF_DPRINTF_L5(cmlog, "ibcm_dummy_ip_path_handler: "
4688 	    "dummy_path.func %p", dummy_path.func);
4689 }
4690