2 * Copyright (c) 2004-2006 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);
285 /**********************************************************************
286 **********************************************************************/
288 osm_ca_info_destroy(IN osm_vendor_t * const p_vend,
289 IN osm_ca_info_t * const p_ca_info)
291 OSM_LOG_ENTER(p_vend->p_log);
293 if (p_ca_info->p_attr) {
294 if (p_ca_info->p_attr->num_ports) {
295 free(p_ca_info->p_attr->p_port_attr);
297 free(p_ca_info->p_attr);
302 OSM_LOG_EXIT(p_vend->p_log);
305 /**********************************************************************
306 * Fill in the array of port_attr with all available ports on ALL the
307 * avilable CAs on this machine.
309 * UPDATE THE VENDOR OBJECT LIST OF CA_INFO STRUCTS
310 **********************************************************************/
312 osm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
313 IN ib_port_attr_t * const p_attr_array,
314 IN uint32_t * const p_num_ports)
316 ib_api_status_t status;
320 uint32_t port_count = 0;
322 uint32_t total_ports = 0;
323 VAPI_hca_id_t *p_ca_ids = NULL;
324 osm_ca_info_t *p_ca_info;
326 OSM_LOG_ENTER(p_vend->p_log);
331 * 1) Determine the number of CA's
332 * 2) Allocate an array big enough to hold the ca info objects.
333 * 3) Call again to retrieve the guids.
335 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
336 if (status != IB_SUCCESS) {
337 osm_log(p_vend->p_log, OSM_LOG_ERROR,
338 "osm_vendor_get_all_port_attr: ERR 7109: "
339 "Fail to get CA Ids.\n");
343 /* we keep track of all the CAs in this info array */
344 p_vend->p_ca_info = malloc(ca_count * sizeof(*p_vend->p_ca_info));
345 if (p_vend->p_ca_info == NULL) {
346 osm_log(p_vend->p_log, OSM_LOG_ERROR,
347 "osm_vendor_get_all_port_attr: ERR 7110: "
348 "Unable to allocate CA information array.\n");
352 memset(p_vend->p_ca_info, 0, ca_count * sizeof(*p_vend->p_ca_info));
353 p_vend->ca_count = ca_count;
356 * For each CA, retrieve the CA info attributes
358 for (ca = 0; ca < ca_count; ca++) {
359 p_ca_info = &p_vend->p_ca_info[ca];
361 status = __osm_ca_info_init(p_vend, p_ca_ids[ca], p_ca_info);
363 if (status != IB_SUCCESS) {
364 osm_log(p_vend->p_log, OSM_LOG_ERROR,
365 "osm_vendor_get_all_port_attr: ERR 7111: "
366 "Unable to initialize CA Info object (%s).\n",
367 ib_get_err_str(status));
370 total_ports += osm_ca_info_get_num_ports(p_ca_info);
372 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
373 "osm_vendor_get_all_port_attr: "
374 "osm_vendor_get_all_port_attr: %u got %u ports total:%u\n",
375 ca, osm_ca_info_get_num_ports(p_ca_info), total_ports);
380 * If the user supplied enough storage, return the port guids,
381 * otherwise, return the appropriate error.
383 if (*p_num_ports >= total_ports) {
384 for (ca = 0; ca < ca_count; ca++) {
387 p_ca_info = &p_vend->p_ca_info[ca];
389 num_ports = osm_ca_info_get_num_ports(p_ca_info);
391 for (port_num = 0; port_num < num_ports; port_num++) {
392 p_attr_array[port_count] =
393 *__osm_ca_info_get_port_attr_ptr(p_ca_info,
399 status = IB_INSUFFICIENT_MEMORY;
406 *p_num_ports = total_ports;
411 OSM_LOG_EXIT(p_vend->p_log);
415 /**********************************************************************
416 * Given the vendor obj and a guid
417 * return the ca id and port number that have that guid
418 **********************************************************************/
421 osm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
422 IN ib_net64_t const guid,
423 OUT VAPI_hca_hndl_t * p_hca_hndl,
424 OUT VAPI_hca_id_t * p_hca_id,
425 OUT uint32_t * p_port_num)
428 ib_api_status_t status;
429 VAPI_hca_id_t *p_ca_ids = NULL;
431 VAPI_hca_hndl_t hca_hndl;
432 VAPI_hca_vendor_t hca_vendor;
433 VAPI_hca_cap_t hca_cap;
434 IB_gid_t *p_port_gid = NULL;
436 ib_net64_t port_guid;
437 uint32_t ca, portIdx, ca_count;
439 OSM_LOG_ENTER(p_vend->p_log);
444 * 1) Determine the number of CA's
445 * 2) Allocate an array big enough to hold the ca info objects.
446 * 3) Call again to retrieve the guids.
448 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
449 if (status != IB_SUCCESS) {
450 osm_log(p_vend->p_log, OSM_LOG_ERROR,
451 "osm_vendor_get_guid_ca_and_port: ERR 7112: "
452 "Fail to get CA Ids.\n");
457 * For each CA, retrieve the CA info attributes
459 for (ca = 0; ca < ca_count; ca++) {
460 /* get the HCA handle */
461 vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
462 if (vapi_res != VAPI_OK) {
463 osm_log(p_vend->p_log, OSM_LOG_ERROR,
464 "osm_vendor_get_guid_ca_and_port: ERR 7113: "
465 "Fail to get HCA handle (%u).\n", vapi_res);
469 /* get the CA attributes - to know how many ports it has: */
470 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
471 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
472 "osm_vendor_get_guid_ca_and_port: "
473 "Querying CA %s.\n", p_ca_ids[ca]);
476 /* query and get the HCA capability */
477 vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
478 if (vapi_res != VAPI_OK) {
479 osm_log(p_vend->p_log, OSM_LOG_ERROR,
480 "osm_vendor_get_guid_ca_and_port: ERR 7114: "
481 "Fail to get HCA Capabilities (%u).\n",
486 /* go over all ports - to obtail their guids */
487 for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
489 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
492 (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
494 /* get the port guid */
496 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
497 maxNumGids, &maxNumGids,
499 if (vapi_res != VAPI_OK) {
500 osm_log(p_vend->p_log, OSM_LOG_ERROR,
501 "osm_vendor_get_guid_ca_and_port: ERR 7115: "
502 "Fail to get HCA Port GID (%d).\n",
507 /* convert to SF style */
508 __osm_vendor_gid_to_guid(p_port_gid[0],
509 (VAPI_gid_t *) & port_guid);
511 /* finally did we find it ? */
512 if (port_guid == guid) {
513 *p_hca_hndl = hca_hndl;
514 memcpy(p_hca_id, p_ca_ids[ca],
515 sizeof(VAPI_hca_id_t));
516 *p_port_num = portIdx + 1;
526 osm_log(p_vend->p_log, OSM_LOG_ERROR,
527 "osm_vendor_get_guid_ca_and_port: ERR 7116: "
528 "Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
530 status = IB_INVALID_GUID;
533 if (p_ca_ids != NULL)
535 if (p_port_gid != NULL)
537 OSM_LOG_EXIT(p_vend->p_log);
541 #ifdef __TEST_HCA_GUID__
543 #define GUID_ARRAY_SIZE 64
547 /**********************************************************************
548 **********************************************************************/
549 ib_net64_t get_port_guid()
553 boolean_t done_flag = FALSE;
554 ib_api_status_t status;
555 uint32_t num_ports = GUID_ARRAY_SIZE;
556 ib_port_attr_t attr_array[GUID_ARRAY_SIZE];
560 osm_vendor_t *p_vend;
561 osm_log_t *p_osm_log, tlog;
565 status = osm_log_init(p_osm_log, FALSE);
566 if (status != IB_SUCCESS)
569 osm_log(p_osm_log, OSM_LOG_FUNCS, "get_port_guid: [\n");
572 p_vend->p_log = p_osm_log;
575 * Call the transport layer for a list of local port
578 status = osm_vendor_get_all_port_attr(p_vend, attr_array, &num_ports);
579 if (status != IB_SUCCESS) {
580 printf("\nError from osm_opensm_init (%x)\n", status);
584 if (num_ports == 0) {
585 printf("\nNo local ports detected!\n");
589 while (done_flag == FALSE) {
590 printf("\nChoose a local port number with which to bind:\n\n");
591 for (i = 0; i < num_ports; i++) {
593 * Print the index + 1 since by convention, port numbers
594 * start with 1 on host channel adapters.
597 printf("\t%u: GUID = 0x%8" PRIx64
598 ", lid = 0x%04X, state = %s\n", i + 1,
599 cl_ntoh64(attr_array[i].port_guid),
600 cl_ntoh16(attr_array[i].lid),
601 ib_get_port_state_str(attr_array[i].link_state));
604 printf("\nEnter choice (1-%u): ", i);
606 scanf("%u", &choice);
607 if (choice > num_ports)
608 printf("\nError: Lame choice!\n");
614 osm_vendor_get_guid_ca_and_port(p_vend,
615 attr_array[choice - 1].port_guid,
617 if (status != IB_SUCCESS) {
618 printf("Error obtaining back the HCA and Port\n");
622 printf("Selected: CA:%s Port:%d\n", ca_id, portNum);
624 return (attr_array[choice - 1].port_guid);
627 int main(int argc, char **argv)