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#if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
41321936Shselasky#undef IN
42321936Shselasky#undef OUT
43321936Shselasky#include <stdlib.h>
44321936Shselasky#include <vapi_types.h>
45321936Shselasky#include <evapi.h>
46321936Shselasky#include <vendor/osm_vendor_api.h>
47321936Shselasky#include <opensm/osm_log.h>
48321936Shselasky#include <stdio.h>
49321936Shselasky
50321936Shselasky/********************************************************************************
51321936Shselasky *
52321936Shselasky * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
53321936Shselasky *
54321936Shselasky ********************************************************************************/
55321936Shselasky
56321936Shselasky/**********************************************************************
57321936Shselasky * Convert the given GID to GUID by copy of it's upper 8 bytes
58321936Shselasky *
59321936Shselasky *
60321936Shselasky **********************************************************************/
61321936Shselasky
62321936Shselaskyib_api_status_t
63321936Shselasky__osm_vendor_gid_to_guid(IN u_int8_t * gid, OUT VAPI_gid_t * guid)
64321936Shselasky{
65321936Shselasky	memcpy(guid, gid + 8, 8);
66321936Shselasky	return (IB_SUCCESS);
67321936Shselasky}
68321936Shselasky
69321936Shselasky/****f* OpenSM: CA Info/osm_ca_info_get_pi_ptr
70321936Shselasky * NAME
71321936Shselasky * osm_ca_info_get_pi_ptr
72321936Shselasky *
73321936Shselasky * DESCRIPTION
74321936Shselasky * Returns a pointer to the port attribute of the specified port
75321936Shselasky * owned by this CA.
76321936Shselasky *
77321936Shselasky * SYNOPSIS
78321936Shselasky */
79321936Shselaskystatic ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
80321936Shselasky						       const p_ca_info,
81321936Shselasky						       IN const uint8_t index)
82321936Shselasky{
83321936Shselasky	return (&p_ca_info->p_attr->p_port_attr[index]);
84321936Shselasky}
85321936Shselasky
86321936Shselasky/*
87321936Shselasky * PARAMETERS
88321936Shselasky * p_ca_info
89321936Shselasky *    [in] Pointer to a CA Info object.
90321936Shselasky *
91321936Shselasky * index
92321936Shselasky *    [in] Port "index" for which to retrieve the port attribute.
93321936Shselasky *    The index is the offset into the ca's internal array
94321936Shselasky *    of port attributes.
95321936Shselasky *
96321936Shselasky * RETURN VALUE
97321936Shselasky * Returns a pointer to the port attribute of the specified port
98321936Shselasky * owned by this CA.
99321936Shselasky *
100321936Shselasky * NOTES
101321936Shselasky *
102321936Shselasky * SEE ALSO
103321936Shselasky *********/
104321936Shselasky
105321936Shselasky/********************************************************************************
106321936Shselasky * get the CA names ava`ilable on the system
107321936Shselasky * NOTE: user of this function needs to deallocate p_hca_ids after usage.
108321936Shselasky ********************************************************************************/
109321936Shselaskystatic ib_api_status_t
110321936Shselasky__osm_vendor_get_ca_ids(IN osm_vendor_t * const p_vend,
111321936Shselasky			IN VAPI_hca_id_t ** const p_hca_ids,
112321936Shselasky			IN uint32_t * const p_num_guids)
113321936Shselasky{
114321936Shselasky	ib_api_status_t status;
115321936Shselasky	VAPI_ret_t vapi_res;
116321936Shselasky
117321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
118321936Shselasky
119321936Shselasky	CL_ASSERT(p_hca_ids);
120321936Shselasky	CL_ASSERT(p_num_guids);
121321936Shselasky
122321936Shselasky	/* first call is just to get the number */
123321936Shselasky	vapi_res = EVAPI_list_hcas(0, p_num_guids, NULL);
124321936Shselasky
125321936Shselasky	/* fail ? */
126321936Shselasky	if (vapi_res == VAPI_EINVAL_PARAM) {
127321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
128321936Shselasky			"__osm_vendor_get_ca_ids: ERR 7101: "
129321936Shselasky			"Bad parameter in calling: EVAPI_list_hcas. (%d)\n",
130321936Shselasky			vapi_res);
131321936Shselasky		status = IB_ERROR;
132321936Shselasky		goto Exit;
133321936Shselasky	}
134321936Shselasky
135321936Shselasky	/* NO HCA ? */
136321936Shselasky	if (*p_num_guids == 0) {
137321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
138321936Shselasky			"__osm_vendor_get_ca_ids: ERR 7102: "
139321936Shselasky			"No available channel adapters.\n");
140321936Shselasky		status = IB_INSUFFICIENT_RESOURCES;
141321936Shselasky		goto Exit;
142321936Shselasky	}
143321936Shselasky
144321936Shselasky	/* allocate and really call - user of this function needs to deallocate it */
145321936Shselasky	*p_hca_ids =
146321936Shselasky	    (VAPI_hca_id_t *) malloc(*p_num_guids * sizeof(VAPI_hca_id_t));
147321936Shselasky
148321936Shselasky	/* now call it really */
149321936Shselasky	vapi_res = EVAPI_list_hcas(*p_num_guids, p_num_guids, *p_hca_ids);
150321936Shselasky
151321936Shselasky	/* too many ? */
152321936Shselasky	if (vapi_res == VAPI_EAGAIN) {
153321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
154321936Shselasky			"__osm_vendor_get_ca_ids: ERR 7103: "
155321936Shselasky			"More CA GUIDs than allocated array (%d).\n",
156321936Shselasky			*p_num_guids);
157321936Shselasky		status = IB_ERROR;
158321936Shselasky		goto Exit;
159321936Shselasky	}
160321936Shselasky
161321936Shselasky	/* fail ? */
162321936Shselasky	if (vapi_res != VAPI_OK) {
163321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
164321936Shselasky			"__osm_vendor_get_ca_ids: ERR 7104: "
165321936Shselasky			"Bad parameter in calling: EVAPI_list_hcas.\n");
166321936Shselasky		status = IB_ERROR;
167321936Shselasky		goto Exit;
168321936Shselasky	}
169321936Shselasky
170321936Shselasky	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
171321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
172321936Shselasky			"__osm_vendor_get_ca_ids: "
173321936Shselasky			"Detected %u local channel adapters.\n", *p_num_guids);
174321936Shselasky	}
175321936Shselasky
176321936Shselasky	status = IB_SUCCESS;
177321936Shselasky
178321936ShselaskyExit:
179321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
180321936Shselasky	return (status);
181321936Shselasky}
182321936Shselasky
183321936Shselasky/**********************************************************************
184321936Shselasky * Initialize an Info Struct for the Given HCA by its Id
185321936Shselasky **********************************************************************/
186321936Shselaskystatic ib_api_status_t
187321936Shselasky__osm_ca_info_init(IN osm_vendor_t * const p_vend,
188321936Shselasky		   IN VAPI_hca_id_t ca_id, OUT osm_ca_info_t * const p_ca_info)
189321936Shselasky{
190321936Shselasky	ib_api_status_t status = IB_ERROR;
191321936Shselasky	VAPI_ret_t vapi_res;
192321936Shselasky	VAPI_hca_hndl_t hca_hndl;
193321936Shselasky	VAPI_hca_vendor_t hca_vendor;
194321936Shselasky	VAPI_hca_cap_t hca_cap;
195321936Shselasky	VAPI_hca_port_t hca_port;
196321936Shselasky	uint8_t port_num;
197321936Shselasky	IB_gid_t *p_port_gid;
198321936Shselasky	uint16_t maxNumGids;
199321936Shselasky
200321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
201321936Shselasky
202321936Shselasky	/* get the HCA handle */
203321936Shselasky	vapi_res = EVAPI_get_hca_hndl(ca_id, &hca_hndl);
204321936Shselasky	if (vapi_res != VAPI_OK) {
205321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
206321936Shselasky			"__osm_ca_info_init: ERR 7105: "
207321936Shselasky			"Fail to get HCA handle (%u).\n", vapi_res);
208321936Shselasky		goto Exit;
209321936Shselasky	}
210321936Shselasky
211321936Shselasky	if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
212321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
213321936Shselasky			"__osm_ca_info_init: " "Querying CA %s.\n", ca_id);
214321936Shselasky	}
215321936Shselasky
216321936Shselasky	/* query and get the HCA capability */
217321936Shselasky	vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
218321936Shselasky	if (vapi_res != VAPI_OK) {
219321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
220321936Shselasky			"__osm_ca_info_init: ERR 7106: "
221321936Shselasky			"Fail to get HCA Capabilities (%u).\n", vapi_res);
222321936Shselasky		goto Exit;
223321936Shselasky	}
224321936Shselasky
225321936Shselasky	/* get the guid of the HCA */
226321936Shselasky	memcpy(&(p_ca_info->guid), hca_cap.node_guid, 8 * sizeof(u_int8_t));
227321936Shselasky	p_ca_info->attr_size = 1;
228321936Shselasky	p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
229321936Shselasky	memcpy(&(p_ca_info->p_attr->ca_guid), hca_cap.node_guid,
230321936Shselasky	       8 * sizeof(u_int8_t));
231321936Shselasky
232321936Shselasky	/* now obtain the attributes of the ports */
233321936Shselasky	p_ca_info->p_attr->num_ports = hca_cap.phys_port_num;
234321936Shselasky	p_ca_info->p_attr->p_port_attr =
235321936Shselasky	    (ib_port_attr_t *) malloc(hca_cap.phys_port_num *
236321936Shselasky				      sizeof(ib_port_attr_t));
237321936Shselasky
238321936Shselasky	for (port_num = 0; port_num < p_ca_info->p_attr->num_ports; port_num++) {
239321936Shselasky
240321936Shselasky		/* query the port attributes */
241321936Shselasky		vapi_res =
242321936Shselasky		    VAPI_query_hca_port_prop(hca_hndl, port_num + 1, &hca_port);
243321936Shselasky		if (vapi_res != VAPI_OK) {
244321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
245321936Shselasky				"__osm_ca_info_init: ERR 7107: "
246321936Shselasky				"Fail to get HCA Port Attributes (%d).\n",
247321936Shselasky				vapi_res);
248321936Shselasky			goto Exit;
249321936Shselasky		}
250321936Shselasky
251321936Shselasky		/* first call to know the size of the gid table */
252321936Shselasky		vapi_res =
253321936Shselasky		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, 0,
254321936Shselasky					   &maxNumGids, NULL);
255321936Shselasky		p_port_gid = (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
256321936Shselasky
257321936Shselasky		vapi_res =
258321936Shselasky		    VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, maxNumGids,
259321936Shselasky					   &maxNumGids, p_port_gid);
260321936Shselasky		if (vapi_res != VAPI_OK) {
261321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
262321936Shselasky				"__osm_ca_info_init: ERR 7108: "
263321936Shselasky				"Fail to get HCA Port GID (%d).\n", vapi_res);
264321936Shselasky			goto Exit;
265321936Shselasky		}
266321936Shselasky
267321936Shselasky		__osm_vendor_gid_to_guid(p_port_gid[0],
268321936Shselasky					 (IB_gid_t *) & p_ca_info->p_attr->
269321936Shselasky					 p_port_attr[port_num].port_guid);
270321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].lid = hca_port.lid;
271321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].link_state =
272321936Shselasky		    hca_port.state;
273321936Shselasky		p_ca_info->p_attr->p_port_attr[port_num].sm_lid =
274321936Shselasky		    hca_port.sm_lid;
275321936Shselasky
276321936Shselasky		free(p_port_gid);
277321936Shselasky	}
278321936Shselasky
279321936Shselasky	status = IB_SUCCESS;
280321936ShselaskyExit:
281321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
282321936Shselasky	return (status);
283321936Shselasky}
284321936Shselasky
285321936Shselaskyvoid
286321936Shselaskyosm_ca_info_destroy(IN osm_vendor_t * const p_vend,
287321936Shselasky		    IN osm_ca_info_t * const p_ca_info)
288321936Shselasky{
289321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
290321936Shselasky
291321936Shselasky	if (p_ca_info->p_attr) {
292321936Shselasky		if (p_ca_info->p_attr->num_ports) {
293321936Shselasky			free(p_ca_info->p_attr->p_port_attr);
294321936Shselasky		}
295321936Shselasky		free(p_ca_info->p_attr);
296321936Shselasky	}
297321936Shselasky
298321936Shselasky	free(p_ca_info);
299321936Shselasky
300321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
301321936Shselasky}
302321936Shselasky
303321936Shselasky/**********************************************************************
304321936Shselasky * Fill in the array of port_attr with all available ports on ALL the
305321936Shselasky * avilable CAs on this machine.
306321936Shselasky * ALSO -
307321936Shselasky * UPDATE THE VENDOR OBJECT LIST OF CA_INFO STRUCTS
308321936Shselasky **********************************************************************/
309321936Shselaskyib_api_status_t
310321936Shselaskyosm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
311321936Shselasky			     IN ib_port_attr_t * const p_attr_array,
312321936Shselasky			     IN uint32_t * const p_num_ports)
313321936Shselasky{
314321936Shselasky	ib_api_status_t status;
315321936Shselasky
316321936Shselasky	uint32_t ca;
317321936Shselasky	uint32_t ca_count;
318321936Shselasky	uint32_t port_count = 0;
319321936Shselasky	uint8_t port_num;
320321936Shselasky	uint32_t total_ports = 0;
321321936Shselasky	VAPI_hca_id_t *p_ca_ids = NULL;
322321936Shselasky	osm_ca_info_t *p_ca_info;
323321936Shselasky
324321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
325321936Shselasky
326321936Shselasky	CL_ASSERT(p_vend);
327321936Shselasky
328321936Shselasky	/*
329321936Shselasky	 * 1) Determine the number of CA's
330321936Shselasky	 * 2) Allocate an array big enough to hold the ca info objects.
331321936Shselasky	 * 3) Call again to retrieve the guids.
332321936Shselasky	 */
333321936Shselasky	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
334321936Shselasky	if (status != IB_SUCCESS) {
335321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
336321936Shselasky			"osm_vendor_get_all_port_attr: ERR 7109: "
337321936Shselasky			"Fail to get CA Ids.\n");
338321936Shselasky		goto Exit;
339321936Shselasky	}
340321936Shselasky
341321936Shselasky	/* we keep track of all the CAs in this info array */
342321936Shselasky	p_vend->p_ca_info = malloc(ca_count * sizeof(*p_vend->p_ca_info));
343321936Shselasky	if (p_vend->p_ca_info == NULL) {
344321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
345321936Shselasky			"osm_vendor_get_all_port_attr: ERR 7110: "
346321936Shselasky			"Unable to allocate CA information array.\n");
347321936Shselasky		goto Exit;
348321936Shselasky	}
349321936Shselasky
350321936Shselasky	memset(p_vend->p_ca_info, 0, ca_count * sizeof(*p_vend->p_ca_info));
351321936Shselasky	p_vend->ca_count = ca_count;
352321936Shselasky
353321936Shselasky	/*
354321936Shselasky	 * For each CA, retrieve the CA info attributes
355321936Shselasky	 */
356321936Shselasky	for (ca = 0; ca < ca_count; ca++) {
357321936Shselasky		p_ca_info = &p_vend->p_ca_info[ca];
358321936Shselasky
359321936Shselasky		status = __osm_ca_info_init(p_vend, p_ca_ids[ca], p_ca_info);
360321936Shselasky
361321936Shselasky		if (status != IB_SUCCESS) {
362321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
363321936Shselasky				"osm_vendor_get_all_port_attr: ERR 7111: "
364321936Shselasky				"Unable to initialize CA Info object (%s).\n",
365321936Shselasky				ib_get_err_str(status));
366321936Shselasky		}
367321936Shselasky
368321936Shselasky		total_ports += osm_ca_info_get_num_ports(p_ca_info);
369321936Shselasky
370321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_DEBUG,
371321936Shselasky			"osm_vendor_get_all_port_attr: "
372321936Shselasky			"osm_vendor_get_all_port_attr: %u got %u ports total:%u\n",
373321936Shselasky			ca, osm_ca_info_get_num_ports(p_ca_info), total_ports);
374321936Shselasky
375321936Shselasky	}
376321936Shselasky
377321936Shselasky	/*
378321936Shselasky	 * If the user supplied enough storage, return the port guids,
379321936Shselasky	 * otherwise, return the appropriate error.
380321936Shselasky	 */
381321936Shselasky	if (*p_num_ports >= total_ports) {
382321936Shselasky		for (ca = 0; ca < ca_count; ca++) {
383321936Shselasky			uint32_t num_ports;
384321936Shselasky
385321936Shselasky			p_ca_info = &p_vend->p_ca_info[ca];
386321936Shselasky
387321936Shselasky			num_ports = osm_ca_info_get_num_ports(p_ca_info);
388321936Shselasky
389321936Shselasky			for (port_num = 0; port_num < num_ports; port_num++) {
390321936Shselasky				p_attr_array[port_count] =
391321936Shselasky				    *__osm_ca_info_get_port_attr_ptr(p_ca_info,
392321936Shselasky								     port_num);
393321936Shselasky				port_count++;
394321936Shselasky			}
395321936Shselasky		}
396321936Shselasky	} else {
397321936Shselasky		status = IB_INSUFFICIENT_MEMORY;
398321936Shselasky		goto Exit;
399321936Shselasky	}
400321936Shselasky
401321936Shselasky	status = IB_SUCCESS;
402321936Shselasky
403321936ShselaskyExit:
404321936Shselasky	*p_num_ports = total_ports;
405321936Shselasky
406321936Shselasky	if (p_ca_ids)
407321936Shselasky		free(p_ca_ids);
408321936Shselasky
409321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
410321936Shselasky	return (status);
411321936Shselasky}
412321936Shselasky
413321936Shselasky/**********************************************************************
414321936Shselasky * Given the vendor obj and a guid
415321936Shselasky * return the ca id and port number that have that guid
416321936Shselasky **********************************************************************/
417321936Shselasky
418321936Shselaskyib_api_status_t
419321936Shselaskyosm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
420321936Shselasky				IN ib_net64_t const guid,
421321936Shselasky				OUT VAPI_hca_hndl_t * p_hca_hndl,
422321936Shselasky				OUT VAPI_hca_id_t * p_hca_id,
423321936Shselasky				OUT uint32_t * p_port_num)
424321936Shselasky{
425321936Shselasky
426321936Shselasky	ib_api_status_t status;
427321936Shselasky	VAPI_hca_id_t *p_ca_ids = NULL;
428321936Shselasky	VAPI_ret_t vapi_res;
429321936Shselasky	VAPI_hca_hndl_t hca_hndl;
430321936Shselasky	VAPI_hca_vendor_t hca_vendor;
431321936Shselasky	VAPI_hca_cap_t hca_cap;
432321936Shselasky	IB_gid_t *p_port_gid = NULL;
433321936Shselasky	uint16_t maxNumGids;
434321936Shselasky	ib_net64_t port_guid;
435321936Shselasky	uint32_t ca, portIdx, ca_count;
436321936Shselasky
437321936Shselasky	OSM_LOG_ENTER(p_vend->p_log);
438321936Shselasky
439321936Shselasky	CL_ASSERT(p_vend);
440321936Shselasky
441321936Shselasky	/*
442321936Shselasky	 * 1) Determine the number of CA's
443321936Shselasky	 * 2) Allocate an array big enough to hold the ca info objects.
444321936Shselasky	 * 3) Call again to retrieve the guids.
445321936Shselasky	 */
446321936Shselasky	status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
447321936Shselasky	if (status != IB_SUCCESS) {
448321936Shselasky		osm_log(p_vend->p_log, OSM_LOG_ERROR,
449321936Shselasky			"osm_vendor_get_guid_ca_and_port: ERR 7112: "
450321936Shselasky			"Fail to get CA Ids.\n");
451321936Shselasky		goto Exit;
452321936Shselasky	}
453321936Shselasky
454321936Shselasky	/*
455321936Shselasky	 * For each CA, retrieve the CA info attributes
456321936Shselasky	 */
457321936Shselasky	for (ca = 0; ca < ca_count; ca++) {
458321936Shselasky		/* get the HCA handle */
459321936Shselasky		vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
460321936Shselasky		if (vapi_res != VAPI_OK) {
461321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
462321936Shselasky				"osm_vendor_get_guid_ca_and_port: ERR 7113: "
463321936Shselasky				"Fail to get HCA handle (%u).\n", vapi_res);
464321936Shselasky			goto Exit;
465321936Shselasky		}
466321936Shselasky
467321936Shselasky		/* get the CA attributes - to know how many ports it has: */
468321936Shselasky		if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
469321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_DEBUG,
470321936Shselasky				"osm_vendor_get_guid_ca_and_port: "
471321936Shselasky				"Querying CA %s.\n", p_ca_ids[ca]);
472321936Shselasky		}
473321936Shselasky
474321936Shselasky		/* query and get the HCA capability */
475321936Shselasky		vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
476321936Shselasky		if (vapi_res != VAPI_OK) {
477321936Shselasky			osm_log(p_vend->p_log, OSM_LOG_ERROR,
478321936Shselasky				"osm_vendor_get_guid_ca_and_port: ERR 7114: "
479321936Shselasky				"Fail to get HCA Capabilities (%u).\n",
480321936Shselasky				vapi_res);
481321936Shselasky			goto Exit;
482321936Shselasky		}
483321936Shselasky
484321936Shselasky		/* go over all ports - to obtail their guids */
485321936Shselasky		for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
486321936Shselasky			vapi_res =
487321936Shselasky			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
488321936Shselasky						   &maxNumGids, NULL);
489321936Shselasky			p_port_gid =
490321936Shselasky			    (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
491321936Shselasky
492321936Shselasky			/* get the port guid */
493321936Shselasky			vapi_res =
494321936Shselasky			    VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
495321936Shselasky						   maxNumGids, &maxNumGids,
496321936Shselasky						   p_port_gid);
497321936Shselasky			if (vapi_res != VAPI_OK) {
498321936Shselasky				osm_log(p_vend->p_log, OSM_LOG_ERROR,
499321936Shselasky					"osm_vendor_get_guid_ca_and_port: ERR 7115: "
500321936Shselasky					"Fail to get HCA Port GID (%d).\n",
501321936Shselasky					vapi_res);
502321936Shselasky				goto Exit;
503321936Shselasky			}
504321936Shselasky
505321936Shselasky			/* convert to SF style */
506321936Shselasky			__osm_vendor_gid_to_guid(p_port_gid[0],
507321936Shselasky						 (VAPI_gid_t *) & port_guid);
508321936Shselasky
509321936Shselasky			/* finally did we find it ? */
510321936Shselasky			if (port_guid == guid) {
511321936Shselasky				*p_hca_hndl = hca_hndl;
512321936Shselasky				memcpy(p_hca_id, p_ca_ids[ca],
513321936Shselasky				       sizeof(VAPI_hca_id_t));
514321936Shselasky				*p_port_num = portIdx + 1;
515321936Shselasky				status = IB_SUCCESS;
516321936Shselasky				goto Exit;
517321936Shselasky			}
518321936Shselasky
519321936Shselasky			free(p_port_gid);
520321936Shselasky			p_port_gid = NULL;
521321936Shselasky		}		/*  ALL PORTS  */
522321936Shselasky	}			/*  all HCAs */
523321936Shselasky
524321936Shselasky	osm_log(p_vend->p_log, OSM_LOG_ERROR,
525321936Shselasky		"osm_vendor_get_guid_ca_and_port: ERR 7116: "
526321936Shselasky		"Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
527321936Shselasky		cl_ntoh64(guid));
528321936Shselasky	status = IB_INVALID_GUID;
529321936Shselasky
530321936ShselaskyExit:
531321936Shselasky	if (p_ca_ids != NULL)
532321936Shselasky		free(p_ca_ids);
533321936Shselasky	if (p_port_gid != NULL)
534321936Shselasky		free(p_port_gid);
535321936Shselasky	OSM_LOG_EXIT(p_vend->p_log);
536321936Shselasky	return (status);
537321936Shselasky}
538321936Shselasky
539321936Shselasky#ifdef __TEST_HCA_GUID__
540321936Shselasky
541321936Shselasky#define GUID_ARRAY_SIZE 64
542321936Shselasky
543321936Shselasky#include <stdio.h>
544321936Shselasky
545321936Shselaskyib_net64_t get_port_guid()
546321936Shselasky{
547321936Shselasky	uint32_t i;
548321936Shselasky	uint32_t choice = 0;
549321936Shselasky	boolean_t done_flag = FALSE;
550321936Shselasky	ib_api_status_t status;
551321936Shselasky	uint32_t num_ports = GUID_ARRAY_SIZE;
552321936Shselasky	ib_port_attr_t attr_array[GUID_ARRAY_SIZE];
553321936Shselasky	VAPI_hca_id_t ca_id;
554321936Shselasky	uint32_t portNum;
555321936Shselasky	osm_vendor_t vend;
556321936Shselasky	osm_vendor_t *p_vend;
557321936Shselasky	osm_log_t *p_osm_log, tlog;
558321936Shselasky
559321936Shselasky	p_osm_log = &tlog;
560321936Shselasky
561321936Shselasky	status = osm_log_init(p_osm_log, FALSE);
562321936Shselasky	if (status != IB_SUCCESS)
563321936Shselasky		return (status);
564321936Shselasky
565321936Shselasky	osm_log(p_osm_log, OSM_LOG_FUNCS, "get_port_guid: [\n");
566321936Shselasky
567321936Shselasky	p_vend = &vend;
568321936Shselasky	p_vend->p_log = p_osm_log;
569321936Shselasky
570321936Shselasky	/*
571321936Shselasky	 * Call the transport layer for a list of local port
572321936Shselasky	 * GUID values.
573321936Shselasky	 */
574321936Shselasky	status = osm_vendor_get_all_port_attr(p_vend, attr_array, &num_ports);
575321936Shselasky	if (status != IB_SUCCESS) {
576321936Shselasky		printf("\nError from osm_opensm_init (%x)\n", status);
577321936Shselasky		return (0);
578321936Shselasky	}
579321936Shselasky
580321936Shselasky	if (num_ports == 0) {
581321936Shselasky		printf("\nNo local ports detected!\n");
582321936Shselasky		return (0);
583321936Shselasky	}
584321936Shselasky
585321936Shselasky	while (done_flag == FALSE) {
586321936Shselasky		printf("\nChoose a local port number with which to bind:\n\n");
587321936Shselasky		for (i = 0; i < num_ports; i++) {
588321936Shselasky			/*
589321936Shselasky			 * Print the index + 1 since by convention, port numbers
590321936Shselasky			 * start with 1 on host channel adapters.
591321936Shselasky			 */
592321936Shselasky
593321936Shselasky			printf("\t%u: GUID = 0x%8" PRIx64
594321936Shselasky			       ", lid = 0x%04X, state = %s\n", i + 1,
595321936Shselasky			       cl_ntoh64(attr_array[i].port_guid),
596321936Shselasky			       cl_ntoh16(attr_array[i].lid),
597321936Shselasky			       ib_get_port_state_str(attr_array[i].link_state));
598321936Shselasky		}
599321936Shselasky
600321936Shselasky		printf("\nEnter choice (1-%u): ", i);
601321936Shselasky		fflush(stdout);
602321936Shselasky		scanf("%u", &choice);
603321936Shselasky		if (choice > num_ports)
604321936Shselasky			printf("\nError: Lame choice!\n");
605321936Shselasky		else
606321936Shselasky			done_flag = TRUE;
607321936Shselasky	}
608321936Shselasky
609321936Shselasky	status =
610321936Shselasky	    osm_vendor_get_guid_ca_and_port(p_vend,
611321936Shselasky					    attr_array[choice - 1].port_guid,
612321936Shselasky					    &ca_id, &portNum);
613321936Shselasky	if (status != IB_SUCCESS) {
614321936Shselasky		printf("Error obtaining back the HCA and Port\n");
615321936Shselasky		return (0);
616321936Shselasky	}
617321936Shselasky
618321936Shselasky	printf("Selected: CA:%s Port:%d\n", ca_id, portNum);
619321936Shselasky
620321936Shselasky	return (attr_array[choice - 1].port_guid);
621321936Shselasky}
622321936Shselasky
623321936Shselaskyint main(int argc, char **argv)
624321936Shselasky{
625321936Shselasky	get_port_guid();
626321936Shselasky	return (0);
627321936Shselasky}
628321936Shselasky
629321936Shselasky#endif
630321936Shselasky
631321936Shselasky#endif
632