2 * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #endif /* HAVE_CONFIG_H */
40 #if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
44 #include <vapi_types.h>
46 #include <vendor/osm_vendor_api.h>
47 #include <opensm/osm_log.h>
50 /********************************************************************************
52 * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
54 ********************************************************************************/
56 /**********************************************************************
57 * Convert the given GID to GUID by copy of it's upper 8 bytes
60 **********************************************************************/
63 __osm_vendor_gid_to_guid(IN u_int8_t * gid, OUT VAPI_gid_t * guid)
65 memcpy(guid, gid + 8, 8);
69 /****f* OpenSM: CA Info/osm_ca_info_get_pi_ptr
71 * osm_ca_info_get_pi_ptr
74 * Returns a pointer to the port attribute of the specified port
79 static ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
81 IN const uint8_t index)
83 return (&p_ca_info->p_attr->p_port_attr[index]);
89 * [in] Pointer to a CA Info object.
92 * [in] Port "index" for which to retrieve the port attribute.
93 * The index is the offset into the ca's internal array
97 * Returns a pointer to the port attribute of the specified port
105 /********************************************************************************
106 * get the CA names ava`ilable on the system
107 * NOTE: user of this function needs to deallocate p_hca_ids after usage.
108 ********************************************************************************/
109 static ib_api_status_t
110 __osm_vendor_get_ca_ids(IN osm_vendor_t * const p_vend,
111 IN VAPI_hca_id_t ** const p_hca_ids,
112 IN uint32_t * const p_num_guids)
114 ib_api_status_t status;
117 OSM_LOG_ENTER(p_vend->p_log);
119 CL_ASSERT(p_hca_ids);
120 CL_ASSERT(p_num_guids);
122 /* first call is just to get the number */
123 vapi_res = EVAPI_list_hcas(0, p_num_guids, NULL);
126 if (vapi_res == VAPI_EINVAL_PARAM) {
127 osm_log(p_vend->p_log, OSM_LOG_ERROR,
128 "__osm_vendor_get_ca_ids: ERR 7101: "
129 "Bad parameter in calling: EVAPI_list_hcas. (%d)\n",
136 if (*p_num_guids == 0) {
137 osm_log(p_vend->p_log, OSM_LOG_ERROR,
138 "__osm_vendor_get_ca_ids: ERR 7102: "
139 "No available channel adapters.\n");
140 status = IB_INSUFFICIENT_RESOURCES;
144 /* allocate and really call - user of this function needs to deallocate it */
146 (VAPI_hca_id_t *) malloc(*p_num_guids * sizeof(VAPI_hca_id_t));
148 /* now call it really */
149 vapi_res = EVAPI_list_hcas(*p_num_guids, p_num_guids, *p_hca_ids);
152 if (vapi_res == VAPI_EAGAIN) {
153 osm_log(p_vend->p_log, OSM_LOG_ERROR,
154 "__osm_vendor_get_ca_ids: ERR 7103: "
155 "More CA GUIDs than allocated array (%d).\n",
162 if (vapi_res != VAPI_OK) {
163 osm_log(p_vend->p_log, OSM_LOG_ERROR,
164 "__osm_vendor_get_ca_ids: ERR 7104: "
165 "Bad parameter in calling: EVAPI_list_hcas.\n");
170 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
171 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
172 "__osm_vendor_get_ca_ids: "
173 "Detected %u local channel adapters.\n", *p_num_guids);
179 OSM_LOG_EXIT(p_vend->p_log);
183 /**********************************************************************
184 * Initialize an Info Struct for the Given HCA by its Id
185 **********************************************************************/
186 static ib_api_status_t
187 __osm_ca_info_init(IN osm_vendor_t * const p_vend,
188 IN VAPI_hca_id_t ca_id, OUT osm_ca_info_t * const p_ca_info)
190 ib_api_status_t status = IB_ERROR;
192 VAPI_hca_hndl_t hca_hndl;
193 VAPI_hca_vendor_t hca_vendor;
194 VAPI_hca_cap_t hca_cap;
195 VAPI_hca_port_t hca_port;
197 IB_gid_t *p_port_gid;
200 OSM_LOG_ENTER(p_vend->p_log);
202 /* get the HCA handle */
203 vapi_res = EVAPI_get_hca_hndl(ca_id, &hca_hndl);
204 if (vapi_res != VAPI_OK) {
205 osm_log(p_vend->p_log, OSM_LOG_ERROR,
206 "__osm_ca_info_init: ERR 7105: "
207 "Fail to get HCA handle (%u).\n", vapi_res);
211 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
212 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
213 "__osm_ca_info_init: " "Querying CA %s.\n", ca_id);
216 /* query and get the HCA capability */
217 vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
218 if (vapi_res != VAPI_OK) {
219 osm_log(p_vend->p_log, OSM_LOG_ERROR,
220 "__osm_ca_info_init: ERR 7106: "
221 "Fail to get HCA Capabilities (%u).\n", vapi_res);
225 /* get the guid of the HCA */
226 memcpy(&(p_ca_info->guid), hca_cap.node_guid, 8 * sizeof(u_int8_t));
227 p_ca_info->attr_size = 1;
228 p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
229 memcpy(&(p_ca_info->p_attr->ca_guid), hca_cap.node_guid,
230 8 * sizeof(u_int8_t));
232 /* now obtain the attributes of the ports */
233 p_ca_info->p_attr->num_ports = hca_cap.phys_port_num;
234 p_ca_info->p_attr->p_port_attr =
235 (ib_port_attr_t *) malloc(hca_cap.phys_port_num *
236 sizeof(ib_port_attr_t));
238 for (port_num = 0; port_num < p_ca_info->p_attr->num_ports; port_num++) {
240 /* query the port attributes */
242 VAPI_query_hca_port_prop(hca_hndl, port_num + 1, &hca_port);
243 if (vapi_res != VAPI_OK) {
244 osm_log(p_vend->p_log, OSM_LOG_ERROR,
245 "__osm_ca_info_init: ERR 7107: "
246 "Fail to get HCA Port Attributes (%d).\n",
251 /* first call to know the size of the gid table */
253 VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, 0,
255 p_port_gid = (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
258 VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, maxNumGids,
259 &maxNumGids, p_port_gid);
260 if (vapi_res != VAPI_OK) {
261 osm_log(p_vend->p_log, OSM_LOG_ERROR,
262 "__osm_ca_info_init: ERR 7108: "
263 "Fail to get HCA Port GID (%d).\n", vapi_res);
267 __osm_vendor_gid_to_guid(p_port_gid[0],
268 (IB_gid_t *) & p_ca_info->p_attr->
269 p_port_attr[port_num].port_guid);
270 p_ca_info->p_attr->p_port_attr[port_num].lid = hca_port.lid;
271 p_ca_info->p_attr->p_port_attr[port_num].link_state =
273 p_ca_info->p_attr->p_port_attr[port_num].sm_lid =
281 OSM_LOG_EXIT(p_vend->p_log);
286 osm_ca_info_destroy(IN osm_vendor_t * const p_vend,
287 IN osm_ca_info_t * const p_ca_info)
289 OSM_LOG_ENTER(p_vend->p_log);
291 if (p_ca_info->p_attr) {
292 if (p_ca_info->p_attr->num_ports) {
293 free(p_ca_info->p_attr->p_port_attr);
295 free(p_ca_info->p_attr);
300 OSM_LOG_EXIT(p_vend->p_log);
303 /**********************************************************************
304 * Fill in the array of port_attr with all available ports on ALL the
305 * avilable CAs on this machine.
307 * UPDATE THE VENDOR OBJECT LIST OF CA_INFO STRUCTS
308 **********************************************************************/
310 osm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
311 IN ib_port_attr_t * const p_attr_array,
312 IN uint32_t * const p_num_ports)
314 ib_api_status_t status;
318 uint32_t port_count = 0;
320 uint32_t total_ports = 0;
321 VAPI_hca_id_t *p_ca_ids = NULL;
322 osm_ca_info_t *p_ca_info;
324 OSM_LOG_ENTER(p_vend->p_log);
329 * 1) Determine the number of CA's
330 * 2) Allocate an array big enough to hold the ca info objects.
331 * 3) Call again to retrieve the guids.
333 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
334 if (status != IB_SUCCESS) {
335 osm_log(p_vend->p_log, OSM_LOG_ERROR,
336 "osm_vendor_get_all_port_attr: ERR 7109: "
337 "Fail to get CA Ids.\n");
341 /* we keep track of all the CAs in this info array */
342 p_vend->p_ca_info = malloc(ca_count * sizeof(*p_vend->p_ca_info));
343 if (p_vend->p_ca_info == NULL) {
344 osm_log(p_vend->p_log, OSM_LOG_ERROR,
345 "osm_vendor_get_all_port_attr: ERR 7110: "
346 "Unable to allocate CA information array.\n");
350 memset(p_vend->p_ca_info, 0, ca_count * sizeof(*p_vend->p_ca_info));
351 p_vend->ca_count = ca_count;
354 * For each CA, retrieve the CA info attributes
356 for (ca = 0; ca < ca_count; ca++) {
357 p_ca_info = &p_vend->p_ca_info[ca];
359 status = __osm_ca_info_init(p_vend, p_ca_ids[ca], p_ca_info);
361 if (status != IB_SUCCESS) {
362 osm_log(p_vend->p_log, OSM_LOG_ERROR,
363 "osm_vendor_get_all_port_attr: ERR 7111: "
364 "Unable to initialize CA Info object (%s).\n",
365 ib_get_err_str(status));
368 total_ports += osm_ca_info_get_num_ports(p_ca_info);
370 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
371 "osm_vendor_get_all_port_attr: "
372 "osm_vendor_get_all_port_attr: %u got %u ports total:%u\n",
373 ca, osm_ca_info_get_num_ports(p_ca_info), total_ports);
378 * If the user supplied enough storage, return the port guids,
379 * otherwise, return the appropriate error.
381 if (*p_num_ports >= total_ports) {
382 for (ca = 0; ca < ca_count; ca++) {
385 p_ca_info = &p_vend->p_ca_info[ca];
387 num_ports = osm_ca_info_get_num_ports(p_ca_info);
389 for (port_num = 0; port_num < num_ports; port_num++) {
390 p_attr_array[port_count] =
391 *__osm_ca_info_get_port_attr_ptr(p_ca_info,
397 status = IB_INSUFFICIENT_MEMORY;
404 *p_num_ports = total_ports;
409 OSM_LOG_EXIT(p_vend->p_log);
413 /**********************************************************************
414 * Given the vendor obj and a guid
415 * return the ca id and port number that have that guid
416 **********************************************************************/
419 osm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
420 IN ib_net64_t const guid,
421 OUT VAPI_hca_hndl_t * p_hca_hndl,
422 OUT VAPI_hca_id_t * p_hca_id,
423 OUT uint32_t * p_port_num)
426 ib_api_status_t status;
427 VAPI_hca_id_t *p_ca_ids = NULL;
429 VAPI_hca_hndl_t hca_hndl;
430 VAPI_hca_vendor_t hca_vendor;
431 VAPI_hca_cap_t hca_cap;
432 IB_gid_t *p_port_gid = NULL;
434 ib_net64_t port_guid;
435 uint32_t ca, portIdx, ca_count;
437 OSM_LOG_ENTER(p_vend->p_log);
442 * 1) Determine the number of CA's
443 * 2) Allocate an array big enough to hold the ca info objects.
444 * 3) Call again to retrieve the guids.
446 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
447 if (status != IB_SUCCESS) {
448 osm_log(p_vend->p_log, OSM_LOG_ERROR,
449 "osm_vendor_get_guid_ca_and_port: ERR 7112: "
450 "Fail to get CA Ids.\n");
455 * For each CA, retrieve the CA info attributes
457 for (ca = 0; ca < ca_count; ca++) {
458 /* get the HCA handle */
459 vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
460 if (vapi_res != VAPI_OK) {
461 osm_log(p_vend->p_log, OSM_LOG_ERROR,
462 "osm_vendor_get_guid_ca_and_port: ERR 7113: "
463 "Fail to get HCA handle (%u).\n", vapi_res);
467 /* get the CA attributes - to know how many ports it has: */
468 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
469 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
470 "osm_vendor_get_guid_ca_and_port: "
471 "Querying CA %s.\n", p_ca_ids[ca]);
474 /* query and get the HCA capability */
475 vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
476 if (vapi_res != VAPI_OK) {
477 osm_log(p_vend->p_log, OSM_LOG_ERROR,
478 "osm_vendor_get_guid_ca_and_port: ERR 7114: "
479 "Fail to get HCA Capabilities (%u).\n",
484 /* go over all ports - to obtail their guids */
485 for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
487 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
490 (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
492 /* get the port guid */
494 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
495 maxNumGids, &maxNumGids,
497 if (vapi_res != VAPI_OK) {
498 osm_log(p_vend->p_log, OSM_LOG_ERROR,
499 "osm_vendor_get_guid_ca_and_port: ERR 7115: "
500 "Fail to get HCA Port GID (%d).\n",
505 /* convert to SF style */
506 __osm_vendor_gid_to_guid(p_port_gid[0],
507 (VAPI_gid_t *) & port_guid);
509 /* finally did we find it ? */
510 if (port_guid == guid) {
511 *p_hca_hndl = hca_hndl;
512 memcpy(p_hca_id, p_ca_ids[ca],
513 sizeof(VAPI_hca_id_t));
514 *p_port_num = portIdx + 1;
524 osm_log(p_vend->p_log, OSM_LOG_ERROR,
525 "osm_vendor_get_guid_ca_and_port: ERR 7116: "
526 "Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
528 status = IB_INVALID_GUID;
531 if (p_ca_ids != NULL)
533 if (p_port_gid != NULL)
535 OSM_LOG_EXIT(p_vend->p_log);
539 #ifdef __TEST_HCA_GUID__
541 #define GUID_ARRAY_SIZE 64
545 ib_net64_t get_port_guid()
549 boolean_t done_flag = FALSE;
550 ib_api_status_t status;
551 uint32_t num_ports = GUID_ARRAY_SIZE;
552 ib_port_attr_t attr_array[GUID_ARRAY_SIZE];
556 osm_vendor_t *p_vend;
557 osm_log_t *p_osm_log, tlog;
561 status = osm_log_init(p_osm_log, FALSE);
562 if (status != IB_SUCCESS)
565 osm_log(p_osm_log, OSM_LOG_FUNCS, "get_port_guid: [\n");
568 p_vend->p_log = p_osm_log;
571 * Call the transport layer for a list of local port
574 status = osm_vendor_get_all_port_attr(p_vend, attr_array, &num_ports);
575 if (status != IB_SUCCESS) {
576 printf("\nError from osm_opensm_init (%x)\n", status);
580 if (num_ports == 0) {
581 printf("\nNo local ports detected!\n");
585 while (done_flag == FALSE) {
586 printf("\nChoose a local port number with which to bind:\n\n");
587 for (i = 0; i < num_ports; i++) {
589 * Print the index + 1 since by convention, port numbers
590 * start with 1 on host channel adapters.
593 printf("\t%u: GUID = 0x%8" PRIx64
594 ", lid = 0x%04X, state = %s\n", i + 1,
595 cl_ntoh64(attr_array[i].port_guid),
596 cl_ntoh16(attr_array[i].lid),
597 ib_get_port_state_str(attr_array[i].link_state));
600 printf("\nEnter choice (1-%u): ", i);
602 scanf("%u", &choice);
603 if (choice > num_ports)
604 printf("\nError: Lame choice!\n");
610 osm_vendor_get_guid_ca_and_port(p_vend,
611 attr_array[choice - 1].port_guid,
613 if (status != IB_SUCCESS) {
614 printf("Error obtaining back the HCA and Port\n");
618 printf("Selected: CA:%s Port:%d\n", ca_id, portNum);
620 return (attr_array[choice - 1].port_guid);
623 int main(int argc, char **argv)