1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff#if HAVE_CONFIG_H
37219820Sjeff#  include <config.h>
38219820Sjeff#endif				/* HAVE_CONFIG_H */
39219820Sjeff
40219820Sjeff#include <stdlib.h>
41219820Sjeff#include <string.h>
42219820Sjeff#if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
43219820Sjeff#undef IN
44219820Sjeff#undef OUT
45219820Sjeff#include <vapi_types.h>
46219820Sjeff#include <evapi.h>
47219820Sjeff#include <vendor/osm_vendor_api.h>
48219820Sjeff#include <opensm/osm_log.h>
49219820Sjeff#include <stdlib.h>
50219820Sjeff#include <stdio.h>
51219820Sjeff
52219820Sjeff/********************************************************************************
53219820Sjeff *
54219820Sjeff * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
55219820Sjeff *
56219820Sjeff ********************************************************************************/
57219820Sjeff
58219820Sjefftypedef struct _osm_ca_info {
59219820Sjeff	ib_net64_t guid;
60219820Sjeff	size_t attr_size;
61219820Sjeff	ib_ca_attr_t *p_attr;
62219820Sjeff} osm_ca_info_t;
63219820Sjeff
64219820Sjeff/**********************************************************************
65219820Sjeff * Convert the given GID to GUID by copy of it's upper 8 bytes
66219820Sjeff **********************************************************************/
67219820Sjeffib_api_status_t
68219820Sjeff__osm_vendor_gid_to_guid(IN u_int8_t * gid, OUT VAPI_gid_t * guid)
69219820Sjeff{
70219820Sjeff	memcpy(guid, gid + 8, 8);
71219820Sjeff	return (IB_SUCCESS);
72219820Sjeff}
73219820Sjeff
74219820Sjeff/**********************************************************************
75219820Sjeff * Returns a pointer to the port attribute of the specified port
76219820Sjeff * owned by this CA.
77219820Sjeff ************************************************************************/
78219820Sjeffstatic ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
79219820Sjeff						       const p_ca_info,
80219820Sjeff						       IN const uint8_t index)
81219820Sjeff{
82219820Sjeff	return (&p_ca_info->p_attr->p_port_attr[index]);
83219820Sjeff}
84219820Sjeff
85219820Sjeff/********************************************************************************
86219820Sjeff * get the CA names available on the system
87219820Sjeff * NOTE: user of this function needs to deallocate p_hca_ids after usage.
88219820Sjeff ********************************************************************************/
89219820Sjeffstatic ib_api_status_t
90219820Sjeff__osm_vendor_get_ca_ids(IN osm_vendor_t * const p_vend,
91219820Sjeff			IN VAPI_hca_id_t ** const p_hca_ids,
92219820Sjeff			IN uint32_t * const p_num_guids)
93219820Sjeff{
94219820Sjeff	ib_api_status_t status;
95219820Sjeff	VAPI_ret_t vapi_res;
96219820Sjeff
97219820Sjeff	OSM_LOG_ENTER(p_vend->p_log);
98219820Sjeff
99219820Sjeff	CL_ASSERT(p_hca_ids);
100219820Sjeff	CL_ASSERT(p_num_guids);
101219820Sjeff
102219820Sjeff	/* first call is just to get the number */
103219820Sjeff	vapi_res = EVAPI_list_hcas(0, p_num_guids, NULL);
104219820Sjeff
105219820Sjeff	/* fail ? */
106219820Sjeff	if (vapi_res == VAPI_EINVAL_PARAM) {
107219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
108219820Sjeff			"__osm_vendor_get_ca_ids: ERR 3D08: : "
109219820Sjeff			"Bad parameter in calling: EVAPI_list_hcas. (%d)\n",
110219820Sjeff			vapi_res);
111219820Sjeff		status = IB_ERROR;
112219820Sjeff		goto Exit;
113219820Sjeff	}
114219820Sjeff
115219820Sjeff	/* NO HCA ? */
116219820Sjeff	if (*p_num_guids == 0) {
117219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
118219820Sjeff			"__osm_vendor_get_ca_ids: ERR 3D09: "
119219820Sjeff			"No available channel adapters.\n");
120219820Sjeff		status = IB_INSUFFICIENT_RESOURCES;
121219820Sjeff		goto Exit;
122219820Sjeff	}
123219820Sjeff
124219820Sjeff	/* allocate and really call - user of this function needs to deallocate it */
125219820Sjeff	*p_hca_ids =
126219820Sjeff	    (VAPI_hca_id_t *) malloc(*p_num_guids * sizeof(VAPI_hca_id_t));
127219820Sjeff
128219820Sjeff	/* now call it really */
129219820Sjeff	vapi_res = EVAPI_list_hcas(*p_num_guids, p_num_guids, *p_hca_ids);
130219820Sjeff
131219820Sjeff	/* too many ? */
132219820Sjeff	if (vapi_res == VAPI_EAGAIN) {
133219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
134219820Sjeff			"__osm_vendor_get_ca_ids: ERR 3D10: "
135219820Sjeff			"More CA GUIDs than allocated array (%d).\n",
136219820Sjeff			*p_num_guids);
137219820Sjeff		status = IB_ERROR;
138219820Sjeff		goto Exit;
139219820Sjeff	}
140219820Sjeff
141219820Sjeff	/* fail ? */
142219820Sjeff	if (vapi_res != VAPI_OK) {
143219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
144219820Sjeff			"__osm_vendor_get_ca_ids: ERR 3D11: : "
145219820Sjeff			"Bad parameter in calling: EVAPI_list_hcas.\n");
146219820Sjeff		status = IB_ERROR;
147219820Sjeff		goto Exit;
148219820Sjeff	}
149219820Sjeff
150219820Sjeff	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
151219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
152219820Sjeff			"__osm_vendor_get_ca_ids: "
153219820Sjeff			"Detected %u local channel adapters.\n", *p_num_guids);
154219820Sjeff	}
155219820Sjeff
156219820Sjeff	status = IB_SUCCESS;
157219820Sjeff
158219820SjeffExit:
159219820Sjeff	OSM_LOG_EXIT(p_vend->p_log);
160219820Sjeff	return (status);
161219820Sjeff}
162219820Sjeff
163219820Sjeff/**********************************************************************
164219820Sjeff * Initialize an Info Struct for the Given HCA by its Id
165219820Sjeff **********************************************************************/
166219820Sjeffstatic ib_api_status_t
167219820Sjeff__osm_ca_info_init(IN osm_vendor_t * const p_vend,
168219820Sjeff		   IN VAPI_hca_id_t ca_id, OUT osm_ca_info_t * const p_ca_info)
169219820Sjeff{
170219820Sjeff	ib_api_status_t status = IB_ERROR;
171219820Sjeff	VAPI_ret_t vapi_res;
172219820Sjeff	VAPI_hca_hndl_t hca_hndl;
173219820Sjeff	VAPI_hca_vendor_t hca_vendor;
174219820Sjeff	VAPI_hca_cap_t hca_cap;
175219820Sjeff	VAPI_hca_port_t hca_port;
176219820Sjeff	uint8_t port_num;
177219820Sjeff	IB_gid_t *p_port_gid;
178219820Sjeff	uint16_t maxNumGids;
179219820Sjeff
180219820Sjeff	OSM_LOG_ENTER(p_vend->p_log);
181219820Sjeff
182219820Sjeff	/* get the HCA handle */
183219820Sjeff	vapi_res = EVAPI_get_hca_hndl(ca_id, &hca_hndl);
184219820Sjeff	if (vapi_res != VAPI_OK) {
185219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
186219820Sjeff			"__osm_ca_info_init: ERR 3D05: "
187219820Sjeff			"Fail to get HCA handle (%u).\n", vapi_res);
188219820Sjeff		goto Exit;
189219820Sjeff	}
190219820Sjeff
191219820Sjeff	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
192219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
193219820Sjeff			"__osm_ca_info_init: " "Querying CA %s.\n", ca_id);
194219820Sjeff	}
195219820Sjeff
196219820Sjeff	/* query and get the HCA capability */
197219820Sjeff	vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
198219820Sjeff	if (vapi_res != VAPI_OK) {
199219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
200219820Sjeff			"__osm_ca_info_init: ERR 3D06: "
201219820Sjeff			"Fail to get HCA Capabilities (%u).\n", vapi_res);
202219820Sjeff		goto Exit;
203219820Sjeff	}
204219820Sjeff
205219820Sjeff	/* get the guid of the HCA */
206219820Sjeff	memcpy(&(p_ca_info->guid), hca_cap.node_guid, 8 * sizeof(u_int8_t));
207219820Sjeff	p_ca_info->attr_size = 1;
208219820Sjeff	p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
209219820Sjeff	memcpy(&(p_ca_info->p_attr->ca_guid), hca_cap.node_guid,
210219820Sjeff	       8 * sizeof(u_int8_t));
211219820Sjeff
212219820Sjeff	/* now obtain the attributes of the ports */
213219820Sjeff	p_ca_info->p_attr->num_ports = hca_cap.phys_port_num;
214219820Sjeff	p_ca_info->p_attr->p_port_attr =
215219820Sjeff	    (ib_port_attr_t *) malloc(hca_cap.phys_port_num *
216219820Sjeff				      sizeof(ib_port_attr_t));
217219820Sjeff
218219820Sjeff	for (port_num = 0; port_num < p_ca_info->p_attr->num_ports; port_num++) {
219219820Sjeff
220219820Sjeff		/* query the port attributes */
221219820Sjeff		vapi_res =
222219820Sjeff		    VAPI_query_hca_port_prop(hca_hndl, port_num + 1, &hca_port);
223219820Sjeff		if (vapi_res != VAPI_OK) {
224219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_ERROR,
225219820Sjeff				"__osm_ca_info_init: ERR 3D07: "
226219820Sjeff				"Fail to get HCA Port Attributes (%d).\n",
227219820Sjeff				vapi_res);
228219820Sjeff			goto Exit;
229219820Sjeff		}
230219820Sjeff
231219820Sjeff		/* first call to know the size of the gid table */
232219820Sjeff		vapi_res =
233219820Sjeff		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, 0,
234219820Sjeff					   &maxNumGids, NULL);
235219820Sjeff		p_port_gid = (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
236219820Sjeff
237219820Sjeff		vapi_res =
238219820Sjeff		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, maxNumGids,
239219820Sjeff					   &maxNumGids, p_port_gid);
240219820Sjeff		if (vapi_res != VAPI_OK) {
241219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_ERROR,
242219820Sjeff				"__osm_ca_info_init: ERR 3D12: "
243219820Sjeff				"Fail to get HCA Port GID (%d).\n", vapi_res);
244219820Sjeff			goto Exit;
245219820Sjeff		}
246219820Sjeff
247219820Sjeff		__osm_vendor_gid_to_guid(p_port_gid[0],
248219820Sjeff					 (IB_gid_t *) & p_ca_info->p_attr->
249219820Sjeff					 p_port_attr[port_num].port_guid);
250219820Sjeff		p_ca_info->p_attr->p_port_attr[port_num].lid = hca_port.lid;
251219820Sjeff		p_ca_info->p_attr->p_port_attr[port_num].link_state =
252219820Sjeff		    hca_port.state;
253219820Sjeff		p_ca_info->p_attr->p_port_attr[port_num].sm_lid =
254219820Sjeff		    hca_port.sm_lid;
255219820Sjeff
256219820Sjeff		free(p_port_gid);
257219820Sjeff	}
258219820Sjeff
259219820Sjeff	status = IB_SUCCESS;
260219820SjeffExit:
261219820Sjeff	OSM_LOG_EXIT(p_vend->p_log);
262219820Sjeff	return (status);
263219820Sjeff}
264219820Sjeff
265219820Sjeff/**********************************************************************
266219820Sjeff **********************************************************************/
267219820Sjeffvoid
268219820Sjeffosm_ca_info_destroy(IN osm_vendor_t * const p_vend,
269219820Sjeff		    IN osm_ca_info_t * const p_ca_info, IN uint8_t num_ca)
270219820Sjeff{
271219820Sjeff	osm_ca_info_t *p_ca;
272219820Sjeff	uint8_t i;
273219820Sjeff
274219820Sjeff	OSM_LOG_ENTER(p_vend->p_log);
275219820Sjeff
276219820Sjeff	for (i = 0; i < num_ca; i++) {
277219820Sjeff		p_ca = &p_ca_info[i];
278219820Sjeff
279219820Sjeff		if (NULL != p_ca->p_attr) {
280219820Sjeff			if (0 != p_ca->p_attr->num_ports) {
281219820Sjeff				free(p_ca->p_attr->p_port_attr);
282219820Sjeff			}
283219820Sjeff
284219820Sjeff			free(p_ca->p_attr);
285219820Sjeff		}
286219820Sjeff	}
287219820Sjeff
288219820Sjeff	free(p_ca_info);
289219820Sjeff
290219820Sjeff	OSM_LOG_EXIT(p_vend->p_log);
291219820Sjeff}
292219820Sjeff
293219820Sjeff/**********************************************************************
294219820Sjeff * Fill in the array of port_attr with all available ports on ALL the
295219820Sjeff * avilable CAs on this machine.
296219820Sjeff * ALSO -
297219820Sjeff * Update the vendor object list of ca_info structs
298219820Sjeff **********************************************************************/
299219820Sjeffib_api_status_t
300219820Sjeffosm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
301219820Sjeff			     IN ib_port_attr_t * const p_attr_array,
302219820Sjeff			     IN uint32_t * const p_num_ports)
303219820Sjeff{
304219820Sjeff	ib_api_status_t status;
305219820Sjeff
306219820Sjeff	uint32_t ca;
307219820Sjeff	uint32_t ca_count = 0;
308219820Sjeff	uint32_t port_count = 0;
309219820Sjeff	uint8_t port_num;
310219820Sjeff	uint32_t total_ports = 0;
311219820Sjeff	VAPI_hca_id_t *p_ca_ids = NULL;
312219820Sjeff	osm_ca_info_t *p_ca_infos = NULL;
313219820Sjeff	uint32_t attr_array_sz = *p_num_ports;
314219820Sjeff
315219820Sjeff	OSM_LOG_ENTER(p_vend->p_log);
316219820Sjeff
317219820Sjeff	CL_ASSERT(p_vend);
318219820Sjeff
319219820Sjeff	/* determine the number of CA's */
320219820Sjeff	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
321219820Sjeff	if (status != IB_SUCCESS) {
322219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
323219820Sjeff			"osm_vendor_get_all_port_attr: ERR 3D13: "
324219820Sjeff			"Fail to get CA Ids.\n");
325219820Sjeff		goto Exit;
326219820Sjeff	}
327219820Sjeff
328219820Sjeff	/* Allocate an array big enough to hold the ca info objects */
329219820Sjeff	p_ca_infos = malloc(ca_count * sizeof(osm_ca_info_t));
330219820Sjeff	if (p_ca_infos == NULL) {
331219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
332219820Sjeff			"osm_vendor_get_all_port_attr: ERR 3D14: "
333219820Sjeff			"Unable to allocate CA information array.\n");
334219820Sjeff		goto Exit;
335219820Sjeff	}
336219820Sjeff
337219820Sjeff	memset(p_ca_infos, 0, ca_count * sizeof(osm_ca_info_t));
338219820Sjeff
339219820Sjeff	/*
340219820Sjeff	 * For each CA, retrieve the CA info attributes
341219820Sjeff	 */
342219820Sjeff	for (ca = 0; ca < ca_count; ca++) {
343219820Sjeff		status =
344219820Sjeff		    __osm_ca_info_init(p_vend, p_ca_ids[ca], &p_ca_infos[ca]);
345219820Sjeff		if (status != IB_SUCCESS) {
346219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_ERROR,
347219820Sjeff				"osm_vendor_get_all_port_attr: ERR 3D15: "
348219820Sjeff				"Unable to initialize CA Info object (%s).\n",
349219820Sjeff				ib_get_err_str(status));
350219820Sjeff			goto Exit;
351219820Sjeff		}
352219820Sjeff		total_ports += p_ca_infos[ca].p_attr->num_ports;
353219820Sjeff	}
354219820Sjeff
355219820Sjeff	*p_num_ports = total_ports;
356219820Sjeff	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
357219820Sjeff		"osm_vendor_get_all_port_attr: total ports:%u \n", total_ports);
358219820Sjeff
359219820Sjeff	/*
360219820Sjeff	 * If the user supplied enough storage, return the port guids,
361219820Sjeff	 * otherwise, return the appropriate error.
362219820Sjeff	 */
363219820Sjeff	if (attr_array_sz >= total_ports) {
364219820Sjeff		for (ca = 0; ca < ca_count; ca++) {
365219820Sjeff			uint32_t num_ports;
366219820Sjeff
367219820Sjeff			num_ports = p_ca_infos[ca].p_attr->num_ports;
368219820Sjeff
369219820Sjeff			for (port_num = 0; port_num < num_ports; port_num++) {
370219820Sjeff				p_attr_array[port_count] =
371219820Sjeff				    *__osm_ca_info_get_port_attr_ptr(&p_ca_infos
372219820Sjeff								     [ca],
373219820Sjeff								     port_num);
374219820Sjeff				port_count++;
375219820Sjeff			}
376219820Sjeff		}
377219820Sjeff	} else {
378219820Sjeff		status = IB_INSUFFICIENT_MEMORY;
379219820Sjeff		goto Exit;
380219820Sjeff	}
381219820Sjeff
382219820Sjeff	status = IB_SUCCESS;
383219820Sjeff
384219820SjeffExit:
385219820Sjeff	if (p_ca_ids)
386219820Sjeff		free(p_ca_ids);
387219820Sjeff
388219820Sjeff	if (p_ca_infos) {
389219820Sjeff		osm_ca_info_destroy(p_vend, p_ca_infos, ca_count);
390219820Sjeff	}
391219820Sjeff
392219820Sjeff	OSM_LOG_EXIT(p_vend->p_log);
393219820Sjeff	return (status);
394219820Sjeff}
395219820Sjeff
396219820Sjeff/**********************************************************************
397219820Sjeff * Given the vendor obj and a guid
398219820Sjeff * return the ca id and port number that have that guid
399219820Sjeff **********************************************************************/
400219820Sjeff
401219820Sjeffib_api_status_t
402219820Sjeffosm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
403219820Sjeff				IN ib_net64_t const guid,
404219820Sjeff				OUT VAPI_hca_hndl_t * p_hca_hndl,
405219820Sjeff				OUT VAPI_hca_id_t * p_hca_id,
406219820Sjeff				OUT uint8_t * p_hca_idx,
407219820Sjeff				OUT uint32_t * p_port_num)
408219820Sjeff{
409219820Sjeff
410219820Sjeff	ib_api_status_t status;
411219820Sjeff	VAPI_hca_id_t *p_ca_ids = NULL;
412219820Sjeff	VAPI_ret_t vapi_res;
413219820Sjeff	VAPI_hca_hndl_t hca_hndl;
414219820Sjeff	VAPI_hca_vendor_t hca_vendor;
415219820Sjeff	VAPI_hca_cap_t hca_cap;
416219820Sjeff	IB_gid_t *p_port_gid = NULL;
417219820Sjeff	uint16_t maxNumGids;
418219820Sjeff	ib_net64_t port_guid;
419219820Sjeff	uint32_t ca, portIdx, ca_count;
420219820Sjeff
421219820Sjeff	OSM_LOG_ENTER(p_vend->p_log);
422219820Sjeff
423219820Sjeff	CL_ASSERT(p_vend);
424219820Sjeff
425219820Sjeff	/*
426219820Sjeff	 * 1) Determine the number of CA's
427219820Sjeff	 * 2) Allocate an array big enough to hold the ca info objects.
428219820Sjeff	 * 3) Call again to retrieve the guids.
429219820Sjeff	 */
430219820Sjeff	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
431219820Sjeff	if (status != IB_SUCCESS) {
432219820Sjeff		osm_log(p_vend->p_log, OSM_LOG_ERROR,
433219820Sjeff			"osm_vendor_get_guid_ca_and_port: ERR 3D16: "
434219820Sjeff			"Fail to get CA Ids.\n");
435219820Sjeff		goto Exit;
436219820Sjeff	}
437219820Sjeff
438219820Sjeff	/*
439219820Sjeff	 * For each CA, retrieve the CA info attributes
440219820Sjeff	 */
441219820Sjeff	for (ca = 0; ca < ca_count; ca++) {
442219820Sjeff		/* get the HCA handle */
443219820Sjeff		vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
444219820Sjeff		if (vapi_res != VAPI_OK) {
445219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_ERROR,
446219820Sjeff				"osm_vendor_get_guid_ca_and_port: ERR 3D17: "
447219820Sjeff				"Fail to get HCA handle (%u).\n", vapi_res);
448219820Sjeff			goto Exit;
449219820Sjeff		}
450219820Sjeff
451219820Sjeff		/* get the CA attributes - to know how many ports it has: */
452219820Sjeff		if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
453219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_DEBUG,
454219820Sjeff				"osm_vendor_get_guid_ca_and_port: "
455219820Sjeff				"Querying CA %s.\n", p_ca_ids[ca]);
456219820Sjeff		}
457219820Sjeff
458219820Sjeff		/* query and get the HCA capability */
459219820Sjeff		vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
460219820Sjeff		if (vapi_res != VAPI_OK) {
461219820Sjeff			osm_log(p_vend->p_log, OSM_LOG_ERROR,
462219820Sjeff				"osm_vendor_get_guid_ca_and_port: ERR 3D18: "
463219820Sjeff				"Fail to get HCA Capabilities (%u).\n",
464219820Sjeff				vapi_res);
465219820Sjeff			goto Exit;
466219820Sjeff		}
467219820Sjeff
468219820Sjeff		/* go over all ports - to obtail their guids */
469219820Sjeff		for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
470219820Sjeff			vapi_res =
471219820Sjeff			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
472219820Sjeff						   &maxNumGids, NULL);
473219820Sjeff			p_port_gid =
474219820Sjeff			    (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
475219820Sjeff
476219820Sjeff			/* get the port guid */
477219820Sjeff			vapi_res =
478219820Sjeff			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
479219820Sjeff						   maxNumGids, &maxNumGids,
480219820Sjeff						   p_port_gid);
481219820Sjeff			if (vapi_res != VAPI_OK) {
482219820Sjeff				osm_log(p_vend->p_log, OSM_LOG_ERROR,
483219820Sjeff					"osm_vendor_get_guid_ca_and_port: ERR 3D19: "
484219820Sjeff					"Fail to get HCA Port GID (%d).\n",
485219820Sjeff					vapi_res);
486219820Sjeff				goto Exit;
487219820Sjeff			}
488219820Sjeff
489219820Sjeff			/* convert to SF style */
490219820Sjeff			__osm_vendor_gid_to_guid(p_port_gid[0],
491219820Sjeff						 (VAPI_gid_t *) & port_guid);
492219820Sjeff
493219820Sjeff			/* finally did we find it ? */
494219820Sjeff			if (port_guid == guid) {
495219820Sjeff				*p_hca_hndl = hca_hndl;
496219820Sjeff				memcpy(p_hca_id, p_ca_ids[ca],
497219820Sjeff				       sizeof(VAPI_hca_id_t));
498219820Sjeff				*p_hca_idx = ca;
499219820Sjeff				*p_port_num = portIdx + 1;
500219820Sjeff				status = IB_SUCCESS;
501219820Sjeff				goto Exit;
502219820Sjeff			}
503219820Sjeff
504219820Sjeff			free(p_port_gid);
505219820Sjeff			p_port_gid = NULL;
506219820Sjeff		}		/*  ALL PORTS  */
507219820Sjeff	}			/*  all HCAs */
508219820Sjeff
509219820Sjeff	osm_log(p_vend->p_log, OSM_LOG_ERROR,
510219820Sjeff		"osm_vendor_get_guid_ca_and_port: ERR 3D20: "
511219820Sjeff		"Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
512219820Sjeff		cl_ntoh64(guid));
513219820Sjeff	status = IB_INVALID_GUID;
514219820Sjeff
515219820SjeffExit:
516219820Sjeff	if (p_ca_ids != NULL)
517219820Sjeff		free(p_ca_ids);
518219820Sjeff	if (p_port_gid != NULL)
519219820Sjeff		free(p_port_gid);
520219820Sjeff	OSM_LOG_EXIT(p_vend->p_log);
521219820Sjeff	return (status);
522219820Sjeff}
523219820Sjeff
524219820Sjeff#endif
525