1321936Shselasky/*
2321936Shselasky * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3321936Shselasky * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4321936Shselasky * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5321936Shselasky *
6321936Shselasky * This software is available to you under a choice of one of two
7321936Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
8321936Shselasky * General Public License (GPL) Version 2, available from the file
9321936Shselasky * COPYING in the main directory of this source tree, or the
10321936Shselasky * OpenIB.org BSD license below:
11321936Shselasky *
12321936Shselasky *     Redistribution and use in source and binary forms, with or
13321936Shselasky *     without modification, are permitted provided that the following
14321936Shselasky *     conditions are met:
15321936Shselasky *
16321936Shselasky *      - Redistributions of source code must retain the above
17321936Shselasky *        copyright notice, this list of conditions and the following
18321936Shselasky *        disclaimer.
19321936Shselasky *
20321936Shselasky *      - Redistributions in binary form must reproduce the above
21321936Shselasky *        copyright notice, this list of conditions and the following
22321936Shselasky *        disclaimer in the documentation and/or other materials
23321936Shselasky *        provided with the distribution.
24321936Shselasky *
25321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32321936Shselasky * SOFTWARE.
33321936Shselasky *
34321936Shselasky */
35321936Shselasky
36321936Shselasky#if HAVE_CONFIG_H
37321936Shselasky#  include <config.h>
38321936Shselasky#endif				/* HAVE_CONFIG_H */
39321936Shselasky
40321936Shselasky#include <stdlib.h>
41321936Shselasky#include <string.h>
42321936Shselasky#if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
43321936Shselasky#undef IN
44321936Shselasky#undef OUT
45321936Shselasky#include <vapi_types.h>
46321936Shselasky#include <evapi.h>
47321936Shselasky#include <vendor/osm_vendor_api.h>
48321936Shselasky#include <opensm/osm_log.h>
49321936Shselasky#include <stdlib.h>
50321936Shselasky#include <stdio.h>
51321936Shselasky
52321936Shselasky/********************************************************************************
53321936Shselasky *
54321936Shselasky * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
55321936Shselasky *
56321936Shselasky ********************************************************************************/
57321936Shselasky
58321936Shselaskytypedef struct _osm_ca_info {
59321936Shselasky	ib_net64_t guid;
60321936Shselasky	size_t attr_size;
61321936Shselasky	ib_ca_attr_t *p_attr;
62321936Shselasky} osm_ca_info_t;
63321936Shselasky
64321936Shselasky/**********************************************************************
65321936Shselasky * Convert the given GID to GUID by copy of it's upper 8 bytes
66321936Shselasky **********************************************************************/
67321936Shselaskyib_api_status_t
68321936Shselasky__osm_vendor_gid_to_guid(IN u_int8_t * gid, OUT VAPI_gid_t * guid)
69321936Shselasky{
70321936Shselasky	memcpy(guid, gid + 8, 8);
71321936Shselasky	return (IB_SUCCESS);
72321936Shselasky}
73321936Shselasky
74321936Shselasky/**********************************************************************
75321936Shselasky * Returns a pointer to the port attribute of the specified port
76321936Shselasky * owned by this CA.
77321936Shselasky ************************************************************************/
78321936Shselaskystatic ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
79321936Shselasky						       const p_ca_info,
80321936Shselasky						       IN const uint8_t index)
81321936Shselasky{
82321936Shselasky	return (&p_ca_info->p_attr->p_port_attr[index]);
83321936Shselasky}
84321936Shselasky
85321936Shselasky/********************************************************************************
86321936Shselasky * get the CA names available on the system
87321936Shselasky * NOTE: user of this function needs to deallocate p_hca_ids after usage.
88321936Shselasky ********************************************************************************/
89321936Shselaskystatic ib_api_status_t
90321936Shselasky__osm_vendor_get_ca_ids(IN osm_vendor_t * const p_vend,
91321936Shselasky			IN VAPI_hca_id_t ** const p_hca_ids,
92321936Shselasky			IN uint32_t * const p_num_guids)
93321936Shselasky{
94321936Shselasky	ib_api_status_t status;
95321936Shselasky	VAPI_ret_t vapi_res;
96321936Shselasky
97321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
98321936Shselasky
99321936Shselasky	CL_ASSERT(p_hca_ids);
100321936Shselasky	CL_ASSERT(p_num_guids);
101321936Shselasky
102321936Shselasky	/* first call is just to get the number */
103321936Shselasky	vapi_res = EVAPI_list_hcas(0, p_num_guids, NULL);
104321936Shselasky
105321936Shselasky	/* fail ? */
106321936Shselasky	if (vapi_res == VAPI_EINVAL_PARAM) {
107321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
108321936Shselasky			"__osm_vendor_get_ca_ids: ERR 3D08: : "
109321936Shselasky			"Bad parameter in calling: EVAPI_list_hcas. (%d)\n",
110321936Shselasky			vapi_res);
111321936Shselasky		status = IB_ERROR;
112321936Shselasky		goto Exit;
113321936Shselasky	}
114321936Shselasky
115321936Shselasky	/* NO HCA ? */
116321936Shselasky	if (*p_num_guids == 0) {
117321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
118321936Shselasky			"__osm_vendor_get_ca_ids: ERR 3D09: "
119321936Shselasky			"No available channel adapters.\n");
120321936Shselasky		status = IB_INSUFFICIENT_RESOURCES;
121321936Shselasky		goto Exit;
122321936Shselasky	}
123321936Shselasky
124321936Shselasky	/* allocate and really call - user of this function needs to deallocate it */
125321936Shselasky	*p_hca_ids =
126321936Shselasky	    (VAPI_hca_id_t *) malloc(*p_num_guids * sizeof(VAPI_hca_id_t));
127321936Shselasky
128321936Shselasky	/* now call it really */
129321936Shselasky	vapi_res = EVAPI_list_hcas(*p_num_guids, p_num_guids, *p_hca_ids);
130321936Shselasky
131321936Shselasky	/* too many ? */
132321936Shselasky	if (vapi_res == VAPI_EAGAIN) {
133321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
134321936Shselasky			"__osm_vendor_get_ca_ids: ERR 3D10: "
135321936Shselasky			"More CA GUIDs than allocated array (%d).\n",
136321936Shselasky			*p_num_guids);
137321936Shselasky		status = IB_ERROR;
138321936Shselasky		goto Exit;
139321936Shselasky	}
140321936Shselasky
141321936Shselasky	/* fail ? */
142321936Shselasky	if (vapi_res != VAPI_OK) {
143321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
144321936Shselasky			"__osm_vendor_get_ca_ids: ERR 3D11: : "
145321936Shselasky			"Bad parameter in calling: EVAPI_list_hcas.\n");
146321936Shselasky		status = IB_ERROR;
147321936Shselasky		goto Exit;
148321936Shselasky	}
149321936Shselasky
150321936Shselasky	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
151321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
152321936Shselasky			"__osm_vendor_get_ca_ids: "
153321936Shselasky			"Detected %u local channel adapters.\n", *p_num_guids);
154321936Shselasky	}
155321936Shselasky
156321936Shselasky	status = IB_SUCCESS;
157321936Shselasky
158321936ShselaskyExit:
159321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
160321936Shselasky	return (status);
161321936Shselasky}
162321936Shselasky
163321936Shselasky/**********************************************************************
164321936Shselasky * Initialize an Info Struct for the Given HCA by its Id
165321936Shselasky **********************************************************************/
166321936Shselaskystatic ib_api_status_t
167321936Shselasky__osm_ca_info_init(IN osm_vendor_t * const p_vend,
168321936Shselasky		   IN VAPI_hca_id_t ca_id, OUT osm_ca_info_t * const p_ca_info)
169321936Shselasky{
170321936Shselasky	ib_api_status_t status = IB_ERROR;
171321936Shselasky	VAPI_ret_t vapi_res;
172321936Shselasky	VAPI_hca_hndl_t hca_hndl;
173321936Shselasky	VAPI_hca_vendor_t hca_vendor;
174321936Shselasky	VAPI_hca_cap_t hca_cap;
175321936Shselasky	VAPI_hca_port_t hca_port;
176321936Shselasky	uint8_t port_num;
177321936Shselasky	IB_gid_t *p_port_gid;
178321936Shselasky	uint16_t maxNumGids;
179321936Shselasky
180321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
181321936Shselasky
182321936Shselasky	/* get the HCA handle */
183321936Shselasky	vapi_res = EVAPI_get_hca_hndl(ca_id, &hca_hndl);
184321936Shselasky	if (vapi_res != VAPI_OK) {
185321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
186321936Shselasky			"__osm_ca_info_init: ERR 3D05: "
187321936Shselasky			"Fail to get HCA handle (%u).\n", vapi_res);
188321936Shselasky		goto Exit;
189321936Shselasky	}
190321936Shselasky
191321936Shselasky	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
192321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
193321936Shselasky			"__osm_ca_info_init: " "Querying CA %s.\n", ca_id);
194321936Shselasky	}
195321936Shselasky
196321936Shselasky	/* query and get the HCA capability */
197321936Shselasky	vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
198321936Shselasky	if (vapi_res != VAPI_OK) {
199321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
200321936Shselasky			"__osm_ca_info_init: ERR 3D06: "
201321936Shselasky			"Fail to get HCA Capabilities (%u).\n", vapi_res);
202321936Shselasky		goto Exit;
203321936Shselasky	}
204321936Shselasky
205321936Shselasky	/* get the guid of the HCA */
206321936Shselasky	memcpy(&(p_ca_info->guid), hca_cap.node_guid, 8 * sizeof(u_int8_t));
207321936Shselasky	p_ca_info->attr_size = 1;
208321936Shselasky	p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
209321936Shselasky	memcpy(&(p_ca_info->p_attr->ca_guid), hca_cap.node_guid,
210321936Shselasky	       8 * sizeof(u_int8_t));
211321936Shselasky
212321936Shselasky	/* now obtain the attributes of the ports */
213321936Shselasky	p_ca_info->p_attr->num_ports = hca_cap.phys_port_num;
214321936Shselasky	p_ca_info->p_attr->p_port_attr =
215321936Shselasky	    (ib_port_attr_t *) malloc(hca_cap.phys_port_num *
216321936Shselasky				      sizeof(ib_port_attr_t));
217321936Shselasky
218321936Shselasky	for (port_num = 0; port_num < p_ca_info->p_attr->num_ports; port_num++) {
219321936Shselasky
220321936Shselasky		/* query the port attributes */
221321936Shselasky		vapi_res =
222321936Shselasky		    VAPI_query_hca_port_prop(hca_hndl, port_num + 1, &hca_port);
223321936Shselasky		if (vapi_res != VAPI_OK) {
224321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
225321936Shselasky				"__osm_ca_info_init: ERR 3D07: "
226321936Shselasky				"Fail to get HCA Port Attributes (%d).\n",
227321936Shselasky				vapi_res);
228321936Shselasky			goto Exit;
229321936Shselasky		}
230321936Shselasky
231321936Shselasky		/* first call to know the size of the gid table */
232321936Shselasky		vapi_res =
233321936Shselasky		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, 0,
234321936Shselasky					   &maxNumGids, NULL);
235321936Shselasky		p_port_gid = (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
236321936Shselasky
237321936Shselasky		vapi_res =
238321936Shselasky		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, maxNumGids,
239321936Shselasky					   &maxNumGids, p_port_gid);
240321936Shselasky		if (vapi_res != VAPI_OK) {
241321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
242321936Shselasky				"__osm_ca_info_init: ERR 3D12: "
243321936Shselasky				"Fail to get HCA Port GID (%d).\n", vapi_res);
244321936Shselasky			goto Exit;
245321936Shselasky		}
246321936Shselasky
247321936Shselasky		__osm_vendor_gid_to_guid(p_port_gid[0],
248321936Shselasky					 (IB_gid_t *) & p_ca_info->p_attr->
249321936Shselasky					 p_port_attr[port_num].port_guid);
250321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].lid = hca_port.lid;
251321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].link_state =
252321936Shselasky		    hca_port.state;
253321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].sm_lid =
254321936Shselasky		    hca_port.sm_lid;
255321936Shselasky
256321936Shselasky		free(p_port_gid);
257321936Shselasky	}
258321936Shselasky
259321936Shselasky	status = IB_SUCCESS;
260321936ShselaskyExit:
261321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
262321936Shselasky	return (status);
263321936Shselasky}
264321936Shselasky
265321936Shselaskyvoid
266321936Shselaskyosm_ca_info_destroy(IN osm_vendor_t * const p_vend,
267321936Shselasky		    IN osm_ca_info_t * const p_ca_info, IN uint8_t num_ca)
268321936Shselasky{
269321936Shselasky	osm_ca_info_t *p_ca;
270321936Shselasky	uint8_t i;
271321936Shselasky
272321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
273321936Shselasky
274321936Shselasky	for (i = 0; i < num_ca; i++) {
275321936Shselasky		p_ca = &p_ca_info[i];
276321936Shselasky
277321936Shselasky		if (NULL != p_ca->p_attr) {
278321936Shselasky			if (0 != p_ca->p_attr->num_ports) {
279321936Shselasky				free(p_ca->p_attr->p_port_attr);
280321936Shselasky			}
281321936Shselasky
282321936Shselasky			free(p_ca->p_attr);
283321936Shselasky		}
284321936Shselasky	}
285321936Shselasky
286321936Shselasky	free(p_ca_info);
287321936Shselasky
288321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
289321936Shselasky}
290321936Shselasky
291321936Shselasky/**********************************************************************
292321936Shselasky * Fill in the array of port_attr with all available ports on ALL the
293321936Shselasky * avilable CAs on this machine.
294321936Shselasky * ALSO -
295321936Shselasky * Update the vendor object list of ca_info structs
296321936Shselasky **********************************************************************/
297321936Shselaskyib_api_status_t
298321936Shselaskyosm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
299321936Shselasky			     IN ib_port_attr_t * const p_attr_array,
300321936Shselasky			     IN uint32_t * const p_num_ports)
301321936Shselasky{
302321936Shselasky	ib_api_status_t status;
303321936Shselasky
304321936Shselasky	uint32_t ca;
305321936Shselasky	uint32_t ca_count = 0;
306321936Shselasky	uint32_t port_count = 0;
307321936Shselasky	uint8_t port_num;
308321936Shselasky	uint32_t total_ports = 0;
309321936Shselasky	VAPI_hca_id_t *p_ca_ids = NULL;
310321936Shselasky	osm_ca_info_t *p_ca_infos = NULL;
311321936Shselasky	uint32_t attr_array_sz = *p_num_ports;
312321936Shselasky
313321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
314321936Shselasky
315321936Shselasky	CL_ASSERT(p_vend);
316321936Shselasky
317321936Shselasky	/* determine the number of CA's */
318321936Shselasky	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
319321936Shselasky	if (status != IB_SUCCESS) {
320321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
321321936Shselasky			"osm_vendor_get_all_port_attr: ERR 3D13: "
322321936Shselasky			"Fail to get CA Ids.\n");
323321936Shselasky		goto Exit;
324321936Shselasky	}
325321936Shselasky
326321936Shselasky	/* Allocate an array big enough to hold the ca info objects */
327321936Shselasky	p_ca_infos = malloc(ca_count * sizeof(osm_ca_info_t));
328321936Shselasky	if (p_ca_infos == NULL) {
329321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
330321936Shselasky			"osm_vendor_get_all_port_attr: ERR 3D14: "
331321936Shselasky			"Unable to allocate CA information array.\n");
332321936Shselasky		goto Exit;
333321936Shselasky	}
334321936Shselasky
335321936Shselasky	memset(p_ca_infos, 0, ca_count * sizeof(osm_ca_info_t));
336321936Shselasky
337321936Shselasky	/*
338321936Shselasky	 * For each CA, retrieve the CA info attributes
339321936Shselasky	 */
340321936Shselasky	for (ca = 0; ca < ca_count; ca++) {
341321936Shselasky		status =
342321936Shselasky		    __osm_ca_info_init(p_vend, p_ca_ids[ca], &p_ca_infos[ca]);
343321936Shselasky		if (status != IB_SUCCESS) {
344321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
345321936Shselasky				"osm_vendor_get_all_port_attr: ERR 3D15: "
346321936Shselasky				"Unable to initialize CA Info object (%s).\n",
347321936Shselasky				ib_get_err_str(status));
348321936Shselasky			goto Exit;
349321936Shselasky		}
350321936Shselasky		total_ports += p_ca_infos[ca].p_attr->num_ports;
351321936Shselasky	}
352321936Shselasky
353321936Shselasky	*p_num_ports = total_ports;
354321936Shselasky	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
355321936Shselasky		"osm_vendor_get_all_port_attr: total ports:%u \n", total_ports);
356321936Shselasky
357321936Shselasky	/*
358321936Shselasky	 * If the user supplied enough storage, return the port guids,
359321936Shselasky	 * otherwise, return the appropriate error.
360321936Shselasky	 */
361321936Shselasky	if (attr_array_sz >= total_ports) {
362321936Shselasky		for (ca = 0; ca < ca_count; ca++) {
363321936Shselasky			uint32_t num_ports;
364321936Shselasky
365321936Shselasky			num_ports = p_ca_infos[ca].p_attr->num_ports;
366321936Shselasky
367321936Shselasky			for (port_num = 0; port_num < num_ports; port_num++) {
368321936Shselasky				p_attr_array[port_count] =
369321936Shselasky				    *__osm_ca_info_get_port_attr_ptr(&p_ca_infos
370321936Shselasky								     [ca],
371321936Shselasky								     port_num);
372321936Shselasky				port_count++;
373321936Shselasky			}
374321936Shselasky		}
375321936Shselasky	} else {
376321936Shselasky		status = IB_INSUFFICIENT_MEMORY;
377321936Shselasky		goto Exit;
378321936Shselasky	}
379321936Shselasky
380321936Shselasky	status = IB_SUCCESS;
381321936Shselasky
382321936ShselaskyExit:
383321936Shselasky	if (p_ca_ids)
384321936Shselasky		free(p_ca_ids);
385321936Shselasky
386321936Shselasky	if (p_ca_infos) {
387321936Shselasky		osm_ca_info_destroy(p_vend, p_ca_infos, ca_count);
388321936Shselasky	}
389321936Shselasky
390321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
391321936Shselasky	return (status);
392321936Shselasky}
393321936Shselasky
394321936Shselasky/**********************************************************************
395321936Shselasky * Given the vendor obj and a guid
396321936Shselasky * return the ca id and port number that have that guid
397321936Shselasky **********************************************************************/
398321936Shselasky
399321936Shselaskyib_api_status_t
400321936Shselaskyosm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
401321936Shselasky				IN ib_net64_t const guid,
402321936Shselasky				OUT VAPI_hca_hndl_t * p_hca_hndl,
403321936Shselasky				OUT VAPI_hca_id_t * p_hca_id,
404321936Shselasky				OUT uint8_t * p_hca_idx,
405321936Shselasky				OUT uint32_t * p_port_num)
406321936Shselasky{
407321936Shselasky
408321936Shselasky	ib_api_status_t status;
409321936Shselasky	VAPI_hca_id_t *p_ca_ids = NULL;
410321936Shselasky	VAPI_ret_t vapi_res;
411321936Shselasky	VAPI_hca_hndl_t hca_hndl;
412321936Shselasky	VAPI_hca_vendor_t hca_vendor;
413321936Shselasky	VAPI_hca_cap_t hca_cap;
414321936Shselasky	IB_gid_t *p_port_gid = NULL;
415321936Shselasky	uint16_t maxNumGids;
416321936Shselasky	ib_net64_t port_guid;
417321936Shselasky	uint32_t ca, portIdx, ca_count;
418321936Shselasky
419321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
420321936Shselasky
421321936Shselasky	CL_ASSERT(p_vend);
422321936Shselasky
423321936Shselasky	/*
424321936Shselasky	 * 1) Determine the number of CA's
425321936Shselasky	 * 2) Allocate an array big enough to hold the ca info objects.
426321936Shselasky	 * 3) Call again to retrieve the guids.
427321936Shselasky	 */
428321936Shselasky	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
429321936Shselasky	if (status != IB_SUCCESS) {
430321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
431321936Shselasky			"osm_vendor_get_guid_ca_and_port: ERR 3D16: "
432321936Shselasky			"Fail to get CA Ids.\n");
433321936Shselasky		goto Exit;
434321936Shselasky	}
435321936Shselasky
436321936Shselasky	/*
437321936Shselasky	 * For each CA, retrieve the CA info attributes
438321936Shselasky	 */
439321936Shselasky	for (ca = 0; ca < ca_count; ca++) {
440321936Shselasky		/* get the HCA handle */
441321936Shselasky		vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
442321936Shselasky		if (vapi_res != VAPI_OK) {
443321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
444321936Shselasky				"osm_vendor_get_guid_ca_and_port: ERR 3D17: "
445321936Shselasky				"Fail to get HCA handle (%u).\n", vapi_res);
446321936Shselasky			goto Exit;
447321936Shselasky		}
448321936Shselasky
449321936Shselasky		/* get the CA attributes - to know how many ports it has: */
450321936Shselasky		if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
451321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_DEBUG,
452321936Shselasky				"osm_vendor_get_guid_ca_and_port: "
453321936Shselasky				"Querying CA %s.\n", p_ca_ids[ca]);
454321936Shselasky		}
455321936Shselasky
456321936Shselasky		/* query and get the HCA capability */
457321936Shselasky		vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
458321936Shselasky		if (vapi_res != VAPI_OK) {
459321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
460321936Shselasky				"osm_vendor_get_guid_ca_and_port: ERR 3D18: "
461321936Shselasky				"Fail to get HCA Capabilities (%u).\n",
462321936Shselasky				vapi_res);
463321936Shselasky			goto Exit;
464321936Shselasky		}
465321936Shselasky
466321936Shselasky		/* go over all ports - to obtail their guids */
467321936Shselasky		for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
468321936Shselasky			vapi_res =
469321936Shselasky			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
470321936Shselasky						   &maxNumGids, NULL);
471321936Shselasky			p_port_gid =
472321936Shselasky			    (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
473321936Shselasky
474321936Shselasky			/* get the port guid */
475321936Shselasky			vapi_res =
476321936Shselasky			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
477321936Shselasky						   maxNumGids, &maxNumGids,
478321936Shselasky						   p_port_gid);
479321936Shselasky			if (vapi_res != VAPI_OK) {
480321936Shselasky				osm_log(p_vend->p_log, OSM_LOG_ERROR,
481321936Shselasky					"osm_vendor_get_guid_ca_and_port: ERR 3D19: "
482321936Shselasky					"Fail to get HCA Port GID (%d).\n",
483321936Shselasky					vapi_res);
484321936Shselasky				goto Exit;
485321936Shselasky			}
486321936Shselasky
487321936Shselasky			/* convert to SF style */
488321936Shselasky			__osm_vendor_gid_to_guid(p_port_gid[0],
489321936Shselasky						 (VAPI_gid_t *) & port_guid);
490321936Shselasky
491321936Shselasky			/* finally did we find it ? */
492321936Shselasky			if (port_guid == guid) {
493321936Shselasky				*p_hca_hndl = hca_hndl;
494321936Shselasky				memcpy(p_hca_id, p_ca_ids[ca],
495321936Shselasky				       sizeof(VAPI_hca_id_t));
496321936Shselasky				*p_hca_idx = ca;
497321936Shselasky				*p_port_num = portIdx + 1;
498321936Shselasky				status = IB_SUCCESS;
499321936Shselasky				goto Exit;
500321936Shselasky			}
501321936Shselasky
502321936Shselasky			free(p_port_gid);
503321936Shselasky			p_port_gid = NULL;
504321936Shselasky		}		/*  ALL PORTS  */
505321936Shselasky	}			/*  all HCAs */
506321936Shselasky
507321936Shselasky	osm_log(p_vend->p_log, OSM_LOG_ERROR,
508321936Shselasky		"osm_vendor_get_guid_ca_and_port: ERR 3D20: "
509321936Shselasky		"Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
510321936Shselasky		cl_ntoh64(guid));
511321936Shselasky	status = IB_INVALID_GUID;
512321936Shselasky
513321936ShselaskyExit:
514321936Shselasky	if (p_ca_ids != NULL)
515321936Shselasky		free(p_ca_ids);
516321936Shselasky	if (p_port_gid != NULL)
517321936Shselasky		free(p_port_gid);
518321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
519321936Shselasky	return (status);
520321936Shselasky}
521321936Shselasky
522321936Shselasky#endif
523