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 */
42 #if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
45 #include <vapi_types.h>
47 #include <vendor/osm_vendor_api.h>
48 #include <opensm/osm_log.h>
52 /********************************************************************************
54 * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
56 ********************************************************************************/
58 typedef struct _osm_ca_info {
64 /**********************************************************************
65 * Convert the given GID to GUID by copy of it's upper 8 bytes
66 **********************************************************************/
68 __osm_vendor_gid_to_guid(IN u_int8_t * gid, OUT VAPI_gid_t * guid)
70 memcpy(guid, gid + 8, 8);
74 /**********************************************************************
75 * Returns a pointer to the port attribute of the specified port
77 ************************************************************************/
78 static ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
80 IN const uint8_t index)
82 return (&p_ca_info->p_attr->p_port_attr[index]);
85 /********************************************************************************
86 * get the CA names available on the system
87 * NOTE: user of this function needs to deallocate p_hca_ids after usage.
88 ********************************************************************************/
89 static ib_api_status_t
90 __osm_vendor_get_ca_ids(IN osm_vendor_t * const p_vend,
91 IN VAPI_hca_id_t ** const p_hca_ids,
92 IN uint32_t * const p_num_guids)
94 ib_api_status_t status;
97 OSM_LOG_ENTER(p_vend->p_log);
100 CL_ASSERT(p_num_guids);
102 /* first call is just to get the number */
103 vapi_res = EVAPI_list_hcas(0, p_num_guids, NULL);
106 if (vapi_res == VAPI_EINVAL_PARAM) {
107 osm_log(p_vend->p_log, OSM_LOG_ERROR,
108 "__osm_vendor_get_ca_ids: ERR 3D08: : "
109 "Bad parameter in calling: EVAPI_list_hcas. (%d)\n",
116 if (*p_num_guids == 0) {
117 osm_log(p_vend->p_log, OSM_LOG_ERROR,
118 "__osm_vendor_get_ca_ids: ERR 3D09: "
119 "No available channel adapters.\n");
120 status = IB_INSUFFICIENT_RESOURCES;
124 /* allocate and really call - user of this function needs to deallocate it */
126 (VAPI_hca_id_t *) malloc(*p_num_guids * sizeof(VAPI_hca_id_t));
128 /* now call it really */
129 vapi_res = EVAPI_list_hcas(*p_num_guids, p_num_guids, *p_hca_ids);
132 if (vapi_res == VAPI_EAGAIN) {
133 osm_log(p_vend->p_log, OSM_LOG_ERROR,
134 "__osm_vendor_get_ca_ids: ERR 3D10: "
135 "More CA GUIDs than allocated array (%d).\n",
142 if (vapi_res != VAPI_OK) {
143 osm_log(p_vend->p_log, OSM_LOG_ERROR,
144 "__osm_vendor_get_ca_ids: ERR 3D11: : "
145 "Bad parameter in calling: EVAPI_list_hcas.\n");
150 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
151 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
152 "__osm_vendor_get_ca_ids: "
153 "Detected %u local channel adapters.\n", *p_num_guids);
159 OSM_LOG_EXIT(p_vend->p_log);
163 /**********************************************************************
164 * Initialize an Info Struct for the Given HCA by its Id
165 **********************************************************************/
166 static ib_api_status_t
167 __osm_ca_info_init(IN osm_vendor_t * const p_vend,
168 IN VAPI_hca_id_t ca_id, OUT osm_ca_info_t * const p_ca_info)
170 ib_api_status_t status = IB_ERROR;
172 VAPI_hca_hndl_t hca_hndl;
173 VAPI_hca_vendor_t hca_vendor;
174 VAPI_hca_cap_t hca_cap;
175 VAPI_hca_port_t hca_port;
177 IB_gid_t *p_port_gid;
180 OSM_LOG_ENTER(p_vend->p_log);
182 /* get the HCA handle */
183 vapi_res = EVAPI_get_hca_hndl(ca_id, &hca_hndl);
184 if (vapi_res != VAPI_OK) {
185 osm_log(p_vend->p_log, OSM_LOG_ERROR,
186 "__osm_ca_info_init: ERR 3D05: "
187 "Fail to get HCA handle (%u).\n", vapi_res);
191 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
192 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
193 "__osm_ca_info_init: " "Querying CA %s.\n", ca_id);
196 /* query and get the HCA capability */
197 vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
198 if (vapi_res != VAPI_OK) {
199 osm_log(p_vend->p_log, OSM_LOG_ERROR,
200 "__osm_ca_info_init: ERR 3D06: "
201 "Fail to get HCA Capabilities (%u).\n", vapi_res);
205 /* get the guid of the HCA */
206 memcpy(&(p_ca_info->guid), hca_cap.node_guid, 8 * sizeof(u_int8_t));
207 p_ca_info->attr_size = 1;
208 p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
209 memcpy(&(p_ca_info->p_attr->ca_guid), hca_cap.node_guid,
210 8 * sizeof(u_int8_t));
212 /* now obtain the attributes of the ports */
213 p_ca_info->p_attr->num_ports = hca_cap.phys_port_num;
214 p_ca_info->p_attr->p_port_attr =
215 (ib_port_attr_t *) malloc(hca_cap.phys_port_num *
216 sizeof(ib_port_attr_t));
218 for (port_num = 0; port_num < p_ca_info->p_attr->num_ports; port_num++) {
220 /* query the port attributes */
222 VAPI_query_hca_port_prop(hca_hndl, port_num + 1, &hca_port);
223 if (vapi_res != VAPI_OK) {
224 osm_log(p_vend->p_log, OSM_LOG_ERROR,
225 "__osm_ca_info_init: ERR 3D07: "
226 "Fail to get HCA Port Attributes (%d).\n",
231 /* first call to know the size of the gid table */
233 VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, 0,
235 p_port_gid = (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
238 VAPI_query_hca_gid_tbl(hca_hndl, port_num + 1, maxNumGids,
239 &maxNumGids, p_port_gid);
240 if (vapi_res != VAPI_OK) {
241 osm_log(p_vend->p_log, OSM_LOG_ERROR,
242 "__osm_ca_info_init: ERR 3D12: "
243 "Fail to get HCA Port GID (%d).\n", vapi_res);
247 __osm_vendor_gid_to_guid(p_port_gid[0],
248 (IB_gid_t *) & p_ca_info->p_attr->
249 p_port_attr[port_num].port_guid);
250 p_ca_info->p_attr->p_port_attr[port_num].lid = hca_port.lid;
251 p_ca_info->p_attr->p_port_attr[port_num].link_state =
253 p_ca_info->p_attr->p_port_attr[port_num].sm_lid =
261 OSM_LOG_EXIT(p_vend->p_log);
265 /**********************************************************************
266 **********************************************************************/
268 osm_ca_info_destroy(IN osm_vendor_t * const p_vend,
269 IN osm_ca_info_t * const p_ca_info, IN uint8_t num_ca)
274 OSM_LOG_ENTER(p_vend->p_log);
276 for (i = 0; i < num_ca; i++) {
277 p_ca = &p_ca_info[i];
279 if (NULL != p_ca->p_attr) {
280 if (0 != p_ca->p_attr->num_ports) {
281 free(p_ca->p_attr->p_port_attr);
290 OSM_LOG_EXIT(p_vend->p_log);
293 /**********************************************************************
294 * Fill in the array of port_attr with all available ports on ALL the
295 * avilable CAs on this machine.
297 * Update the vendor object list of ca_info structs
298 **********************************************************************/
300 osm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
301 IN ib_port_attr_t * const p_attr_array,
302 IN uint32_t * const p_num_ports)
304 ib_api_status_t status;
307 uint32_t ca_count = 0;
308 uint32_t port_count = 0;
310 uint32_t total_ports = 0;
311 VAPI_hca_id_t *p_ca_ids = NULL;
312 osm_ca_info_t *p_ca_infos = NULL;
313 uint32_t attr_array_sz = *p_num_ports;
315 OSM_LOG_ENTER(p_vend->p_log);
319 /* determine the number of CA's */
320 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
321 if (status != IB_SUCCESS) {
322 osm_log(p_vend->p_log, OSM_LOG_ERROR,
323 "osm_vendor_get_all_port_attr: ERR 3D13: "
324 "Fail to get CA Ids.\n");
328 /* Allocate an array big enough to hold the ca info objects */
329 p_ca_infos = malloc(ca_count * sizeof(osm_ca_info_t));
330 if (p_ca_infos == NULL) {
331 osm_log(p_vend->p_log, OSM_LOG_ERROR,
332 "osm_vendor_get_all_port_attr: ERR 3D14: "
333 "Unable to allocate CA information array.\n");
337 memset(p_ca_infos, 0, ca_count * sizeof(osm_ca_info_t));
340 * For each CA, retrieve the CA info attributes
342 for (ca = 0; ca < ca_count; ca++) {
344 __osm_ca_info_init(p_vend, p_ca_ids[ca], &p_ca_infos[ca]);
345 if (status != IB_SUCCESS) {
346 osm_log(p_vend->p_log, OSM_LOG_ERROR,
347 "osm_vendor_get_all_port_attr: ERR 3D15: "
348 "Unable to initialize CA Info object (%s).\n",
349 ib_get_err_str(status));
352 total_ports += p_ca_infos[ca].p_attr->num_ports;
355 *p_num_ports = total_ports;
356 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
357 "osm_vendor_get_all_port_attr: total ports:%u \n", total_ports);
360 * If the user supplied enough storage, return the port guids,
361 * otherwise, return the appropriate error.
363 if (attr_array_sz >= total_ports) {
364 for (ca = 0; ca < ca_count; ca++) {
367 num_ports = p_ca_infos[ca].p_attr->num_ports;
369 for (port_num = 0; port_num < num_ports; port_num++) {
370 p_attr_array[port_count] =
371 *__osm_ca_info_get_port_attr_ptr(&p_ca_infos
378 status = IB_INSUFFICIENT_MEMORY;
389 osm_ca_info_destroy(p_vend, p_ca_infos, ca_count);
392 OSM_LOG_EXIT(p_vend->p_log);
396 /**********************************************************************
397 * Given the vendor obj and a guid
398 * return the ca id and port number that have that guid
399 **********************************************************************/
402 osm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
403 IN ib_net64_t const guid,
404 OUT VAPI_hca_hndl_t * p_hca_hndl,
405 OUT VAPI_hca_id_t * p_hca_id,
406 OUT uint8_t * p_hca_idx,
407 OUT uint32_t * p_port_num)
410 ib_api_status_t status;
411 VAPI_hca_id_t *p_ca_ids = NULL;
413 VAPI_hca_hndl_t hca_hndl;
414 VAPI_hca_vendor_t hca_vendor;
415 VAPI_hca_cap_t hca_cap;
416 IB_gid_t *p_port_gid = NULL;
418 ib_net64_t port_guid;
419 uint32_t ca, portIdx, ca_count;
421 OSM_LOG_ENTER(p_vend->p_log);
426 * 1) Determine the number of CA's
427 * 2) Allocate an array big enough to hold the ca info objects.
428 * 3) Call again to retrieve the guids.
430 status = __osm_vendor_get_ca_ids(p_vend, &p_ca_ids, &ca_count);
431 if (status != IB_SUCCESS) {
432 osm_log(p_vend->p_log, OSM_LOG_ERROR,
433 "osm_vendor_get_guid_ca_and_port: ERR 3D16: "
434 "Fail to get CA Ids.\n");
439 * For each CA, retrieve the CA info attributes
441 for (ca = 0; ca < ca_count; ca++) {
442 /* get the HCA handle */
443 vapi_res = EVAPI_get_hca_hndl(p_ca_ids[ca], &hca_hndl);
444 if (vapi_res != VAPI_OK) {
445 osm_log(p_vend->p_log, OSM_LOG_ERROR,
446 "osm_vendor_get_guid_ca_and_port: ERR 3D17: "
447 "Fail to get HCA handle (%u).\n", vapi_res);
451 /* get the CA attributes - to know how many ports it has: */
452 if (osm_log_is_active(p_vend->p_log, OSM_LOG_DEBUG)) {
453 osm_log(p_vend->p_log, OSM_LOG_DEBUG,
454 "osm_vendor_get_guid_ca_and_port: "
455 "Querying CA %s.\n", p_ca_ids[ca]);
458 /* query and get the HCA capability */
459 vapi_res = VAPI_query_hca_cap(hca_hndl, &hca_vendor, &hca_cap);
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 3D18: "
463 "Fail to get HCA Capabilities (%u).\n",
468 /* go over all ports - to obtail their guids */
469 for (portIdx = 0; portIdx < hca_cap.phys_port_num; portIdx++) {
471 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1, 0,
474 (IB_gid_t *) malloc(maxNumGids * sizeof(IB_gid_t));
476 /* get the port guid */
478 VAPI_query_hca_gid_tbl(hca_hndl, portIdx + 1,
479 maxNumGids, &maxNumGids,
481 if (vapi_res != VAPI_OK) {
482 osm_log(p_vend->p_log, OSM_LOG_ERROR,
483 "osm_vendor_get_guid_ca_and_port: ERR 3D19: "
484 "Fail to get HCA Port GID (%d).\n",
489 /* convert to SF style */
490 __osm_vendor_gid_to_guid(p_port_gid[0],
491 (VAPI_gid_t *) & port_guid);
493 /* finally did we find it ? */
494 if (port_guid == guid) {
495 *p_hca_hndl = hca_hndl;
496 memcpy(p_hca_id, p_ca_ids[ca],
497 sizeof(VAPI_hca_id_t));
499 *p_port_num = portIdx + 1;
509 osm_log(p_vend->p_log, OSM_LOG_ERROR,
510 "osm_vendor_get_guid_ca_and_port: ERR 3D20: "
511 "Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
513 status = IB_INVALID_GUID;
516 if (p_ca_ids != NULL)
518 if (p_port_gid != NULL)
520 OSM_LOG_EXIT(p_vend->p_log);