Blame libvendor/osm_vendor_mlx_hca_pfs.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit 13e616
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
#if HAVE_CONFIG_H
Packit 13e616
#  include <config.h>
Packit 13e616
#endif				/* HAVE_CONFIG_H */
Packit 13e616
Packit 13e616
#if defined(OSM_VENDOR_INTF_MTL) | defined(OSM_VENDOR_INTF_TS)
Packit 13e616
#undef IN
Packit 13e616
#undef OUT
Packit 13e616
#include <vendor/osm_vendor_api.h>
Packit 13e616
#include <opensm/osm_log.h>
Packit 13e616
#include <stdlib.h>
Packit 13e616
#include <stdio.h>
Packit 13e616
#include <sys/types.h>
Packit 13e616
#include <dirent.h>
Packit 13e616
#include <stdio.h>
Packit 13e616
#include <sys/stat.h>
Packit 13e616
#include <stdint.h>
Packit 13e616
#include <fcntl.h>
Packit 13e616
Packit 13e616
/********************************************************************************
Packit 13e616
 *
Packit 13e616
 * Provides the functionality for selecting an HCA Port and Obtaining it's guid.
Packit 13e616
 * This version is based on /proc/infiniband file system. So it is limited to
Packit 13e616
 * The gen1 of openib.org stack.
Packit 13e616
 *
Packit 13e616
 ********************************************************************************/
Packit 13e616
Packit 13e616
typedef struct _osm_ca_info {
Packit 13e616
	ib_net64_t guid;
Packit 13e616
	size_t attr_size;
Packit 13e616
	ib_ca_attr_t *p_attr;
Packit 13e616
Packit 13e616
} osm_ca_info_t;
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Returns a pointer to the port attribute of the specified port
Packit 13e616
 * owned by this CA.
Packit 13e616
 ************************************************************************/
Packit 13e616
static ib_port_attr_t *__osm_ca_info_get_port_attr_ptr(IN const osm_ca_info_t *
Packit 13e616
						       const p_ca_info,
Packit 13e616
						       IN const uint8_t index)
Packit 13e616
{
Packit 13e616
	return (&p_ca_info->p_attr->p_port_attr[index]);
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Obtain the number of local CAs by scanning /proc/infiniband/core
Packit 13e616
 **********************************************************************/
Packit 13e616
int __hca_pfs_get_num_cas()
Packit 13e616
{
Packit 13e616
	int num_cas = 0;
Packit 13e616
	DIR *dp;
Packit 13e616
	struct dirent *ep;
Packit 13e616
Packit 13e616
	dp = opendir("/proc/infiniband/core");
Packit 13e616
	if (dp != NULL) {
Packit 13e616
		while ((ep = readdir(dp))) {
Packit 13e616
			/* CAs are directories with the format ca[1-9][0-9]* */
Packit 13e616
			if ((ep->d_type == DT_DIR)
Packit 13e616
			    && !strncmp(ep->d_name, "ca", 2)) {
Packit 13e616
				num_cas++;
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
		closedir(dp);
Packit 13e616
	}
Packit 13e616
	return num_cas;
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
  name:          InfiniHost0
Packit 13e616
  provider:      tavor
Packit 13e616
  node GUID:     0002:c900:0120:3470
Packit 13e616
  ports:         2
Packit 13e616
  vendor ID:     0x2c9
Packit 13e616
  device ID:     0x5a44
Packit 13e616
  HW revision:   0xa1
Packit 13e616
  FW revision:   0x300020080
Packit 13e616
*/
Packit 13e616
typedef struct _pfs_ca_info {
Packit 13e616
	char name[32];
Packit 13e616
	char provider[32];
Packit 13e616
	uint64_t guid;
Packit 13e616
	uint8_t num_ports;
Packit 13e616
	uint32_t vend_id;
Packit 13e616
	uint16_t dev_id;
Packit 13e616
	uint16_t rev_id;
Packit 13e616
	uint64_t fw_rev;
Packit 13e616
} pfs_ca_info_t;
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Parse the CA Info file available in /proc/infiniband/core/caN/info
Packit 13e616
 **********************************************************************/
Packit 13e616
static ib_api_status_t
Packit 13e616
__parse_ca_info_file(IN osm_vendor_t * const p_vend,
Packit 13e616
		     IN uint32_t idx, OUT pfs_ca_info_t * pfs_ca_info)
Packit 13e616
{
Packit 13e616
	ib_api_status_t status = IB_ERROR;
Packit 13e616
	int info_file;
Packit 13e616
	char file_name[256];
Packit 13e616
	char file_buffer[3200];
Packit 13e616
	char *p_ch;
Packit 13e616
	int g1, g2, g3, g4;
Packit 13e616
	int num_ports;
Packit 13e616
	uint32_t len;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"__parse_ca_info_file: " "Querying CA %d.\n", idx);
Packit 13e616
Packit 13e616
	/* we use the proc file system so we must be able to open the info file .. */
Packit 13e616
	sprintf(file_name, "/proc/infiniband/core/ca%d/info", idx);
Packit 13e616
	info_file = open(file_name, O_RDONLY);
Packit 13e616
	if (!info_file) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5205: "
Packit 13e616
			"Fail to open HCA:%d info file:(%s).\n", idx,
Packit 13e616
			file_name);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* read in the file */
Packit 13e616
	len = read(info_file, file_buffer, 3200);
Packit 13e616
	close(info_file);
Packit 13e616
	file_buffer[len] = '\0';
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	   parse the file ...
Packit 13e616
	   name:          InfiniHost0
Packit 13e616
	   provider:      tavor
Packit 13e616
	   node GUID:     0002:c900:0120:3470
Packit 13e616
	   ports:         2
Packit 13e616
	   vendor ID:     0x2c9
Packit 13e616
	   device ID:     0x5a44
Packit 13e616
	   HW revision:   0xa1
Packit 13e616
	   FW revision:   0x300020080
Packit 13e616
	 */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "name:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5206: "
Packit 13e616
			"Fail to obtain HCA name. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "name: %s", pfs_ca_info->name) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5207: "
Packit 13e616
			"Fail to parse name in info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* get the guid of the HCA */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "node GUID:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5208: "
Packit 13e616
			"Fail to obtain GUID in info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "node GUID: %x:%x:%x:%x", &g1, &g2, &g3, &g4) != 4) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5209: "
Packit 13e616
			"Fail to parse GUID in info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_ca_info->guid = (uint64_t) g1 << 48 | (uint64_t) g1 << 32
Packit 13e616
	    | (uint64_t) g1 << 16 | (uint64_t) g3;
Packit 13e616
Packit 13e616
	/* obtain number of ports */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "ports:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5210: "
Packit 13e616
			"Fail to obtain number of ports in info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "ports: %d", &num_ports) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_ca_info_file: ERR 5211: "
Packit 13e616
			"Fail to parse num ports in info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_ca_info->num_ports = num_ports;
Packit 13e616
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"__parse_ca_info_file: "
Packit 13e616
		"CA1 = name:%s guid:0x%016llx ports:%d\n",
Packit 13e616
		pfs_ca_info->name, pfs_ca_info->guid, pfs_ca_info->num_ports);
Packit 13e616
Packit 13e616
	status = IB_SUCCESS;
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return status;
Packit 13e616
}
Packit 13e616
Packit 13e616
/*
Packit 13e616
  state:         ACTIVE
Packit 13e616
  LID:           0x0001
Packit 13e616
  LMC:           0x0000
Packit 13e616
  SM LID:        0x0001
Packit 13e616
  SM SL:         0x0000
Packit 13e616
  Capabilities:  IsSM
Packit 13e616
  IsTrapSupported
Packit 13e616
  IsAutomaticMigrationSupported
Packit 13e616
  IsSLMappingSupported
Packit 13e616
  IsLEDInfoSupported
Packit 13e616
  IsSystemImageGUIDSupported
Packit 13e616
  IsVendorClassSupported
Packit 13e616
  IsCapabilityMaskNoticeSupported
Packit 13e616
*/
Packit 13e616
typedef struct _pfs_port_info {
Packit 13e616
	uint8_t state;
Packit 13e616
	uint16_t lid;
Packit 13e616
	uint8_t lmc;
Packit 13e616
	uint16_t sm_lid;
Packit 13e616
	uint8_t sm_sl;
Packit 13e616
} pfs_port_info_t;
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Parse the Port Info file available in /proc/infiniband/core/caN/portM/info
Packit 13e616
 * Port num is 1..N
Packit 13e616
 **********************************************************************/
Packit 13e616
static ib_api_status_t
Packit 13e616
__parse_port_info_file(IN osm_vendor_t * const p_vend,
Packit 13e616
		       IN uint32_t hca_idx,
Packit 13e616
		       IN uint8_t port_num, OUT pfs_port_info_t * pfs_port_info)
Packit 13e616
{
Packit 13e616
	ib_api_status_t status = IB_ERROR;
Packit 13e616
	int info_file;
Packit 13e616
	char file_name[256];
Packit 13e616
	char file_buffer[3200];
Packit 13e616
	char state[12];
Packit 13e616
	char *p_ch;
Packit 13e616
	int lid, sm_lid, lmc, sm_sl;
Packit 13e616
	uint32_t len;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"__parse_port_info_file: "
Packit 13e616
		"Parsing Proc File System Port Info CA %d Port %d.\n", hca_idx,
Packit 13e616
		port_num);
Packit 13e616
Packit 13e616
	/* we use the proc file system so we must be able to open the info file .. */
Packit 13e616
	sprintf(file_name, "/proc/infiniband/core/ca%d/port%d/info", hca_idx,
Packit 13e616
		port_num);
Packit 13e616
	info_file = open(file_name, O_RDONLY);
Packit 13e616
	if (!info_file) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5212: "
Packit 13e616
			"Fail to open HCA:%d Port:%d info file:(%s).\n",
Packit 13e616
			hca_idx, port_num, file_name);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* read in the file */
Packit 13e616
	len = read(info_file, file_buffer, 3200);
Packit 13e616
	close(info_file);
Packit 13e616
	file_buffer[len] = '\0';
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	   parse the file ...
Packit 13e616
	   state:         ACTIVE
Packit 13e616
	   LID:           0x0001
Packit 13e616
	   LMC:           0x0000
Packit 13e616
	   SM LID:        0x0001
Packit 13e616
	   SM SL:         0x0000
Packit 13e616
	   ...
Packit 13e616
	 */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "state:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5213: "
Packit 13e616
			"Fail to obtain port state. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "state: %s", state) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5214: "
Packit 13e616
			"Fail to parse state from info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	if (!strcmp(state, "ACTIVE"))
Packit 13e616
		pfs_port_info->state = IB_LINK_ACTIVE;
Packit 13e616
	else if (!strcmp(state, "DOWN"))
Packit 13e616
		pfs_port_info->state = IB_LINK_DOWN;
Packit 13e616
	else if (!strcmp(state, "INIT"))
Packit 13e616
		pfs_port_info->state = IB_LINK_INIT;
Packit 13e616
	else if (!strcmp(state, "ARMED"))
Packit 13e616
		pfs_port_info->state = IB_LINK_ARMED;
Packit 13e616
	else
Packit 13e616
		pfs_port_info->state = 0;
Packit 13e616
Packit 13e616
	/* get lid */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "LID:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5215: "
Packit 13e616
			"Fail to obtain port lid. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "LID: %x", &lid) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5216: "
Packit 13e616
			"Fail to parse lid from info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_port_info->lid = lid;
Packit 13e616
	/* get LMC */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "LMC:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5217: "
Packit 13e616
			"Fail to obtain port LMC. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "LMC: %x", &lmc) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5218: "
Packit 13e616
			"Fail to parse LMC from info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_port_info->lmc = lmc;
Packit 13e616
Packit 13e616
	/* get SM LID */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "SM LID:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5219: "
Packit 13e616
			"Fail to obtain port SM LID. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "SM LID: %x", &sm_lid) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5220: "
Packit 13e616
			"Fail to parse SM LID from info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_port_info->sm_lid = sm_lid;
Packit 13e616
Packit 13e616
	/* get SM LID */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "SM SL:"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5221: "
Packit 13e616
			"Fail to obtain port SM SL. In info file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch, "SM SL: %x", &sm_sl) != 1) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__parse_port_info_file: ERR 5222: "
Packit 13e616
			"Fail to parse SM SL from info file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	pfs_port_info->sm_sl = sm_sl;
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"__parse_port_info_file:  "
Packit 13e616
		"Obtained Port:%d = state:%d, lid:0x%04X, lmc:%d, sm_lid:0x%04X, sm_sl:%d\n",
Packit 13e616
		port_num, pfs_port_info->state, pfs_port_info->lid,
Packit 13e616
		pfs_port_info->lmc, pfs_port_info->sm_lid,
Packit 13e616
		pfs_port_info->sm_sl);
Packit 13e616
Packit 13e616
	status = IB_SUCCESS;
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return status;
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Parse the port guid_tbl file to obtain the port guid.
Packit 13e616
 * File format is:
Packit 13e616
 * [  0] fe80:0000:0000:0000:0002:c900:0120:3472
Packit 13e616
 **********************************************************************/
Packit 13e616
static ib_api_status_t
Packit 13e616
__get_port_guid_from_port_gid_tbl(IN osm_vendor_t * const p_vend,
Packit 13e616
				  IN uint32_t hca_idx,
Packit 13e616
				  IN uint8_t port_num, OUT uint64_t * port_guid)
Packit 13e616
{
Packit 13e616
	ib_api_status_t status = IB_ERROR;
Packit 13e616
	int info_file;
Packit 13e616
	char file_name[256];
Packit 13e616
	char file_buffer[3200];
Packit 13e616
	char *p_ch;
Packit 13e616
	int g[8];
Packit 13e616
	uint32_t len;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"__get_port_guid_from_port_gid_tbl: "
Packit 13e616
		"Parsing Proc File System Port Guid Table CA %d Port %d.\n",
Packit 13e616
		hca_idx, port_num);
Packit 13e616
Packit 13e616
	/* we use the proc file system so we must be able to open the info file .. */
Packit 13e616
	sprintf(file_name, "/proc/infiniband/core/ca%d/port%d/gid_table",
Packit 13e616
		hca_idx, port_num);
Packit 13e616
	info_file = open(file_name, O_RDONLY);
Packit 13e616
	if (!info_file) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__get_port_guid_from_port_gid_tbl: ERR 5223: "
Packit 13e616
			"Fail to open HCA:%d Port:%d gid_table file:(%s).\n",
Packit 13e616
			hca_idx, port_num, file_name);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* read in the file */
Packit 13e616
	len = read(info_file, file_buffer, 3200);
Packit 13e616
	close(info_file);
Packit 13e616
	file_buffer[len] = '\0';
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	   parse the file ...
Packit 13e616
	   [  0] fe80:0000:0000:0000:0002:c900:0120:3472
Packit 13e616
	   ...
Packit 13e616
	 */
Packit 13e616
	if (!(p_ch = strstr(file_buffer, "[  0]"))) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__get_port_guid_from_port_gid_tbl: ERR 5224: "
Packit 13e616
			"Fail to obtain first gid index. In gid_table file:(%s).\n",
Packit 13e616
			file_buffer);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
	if (sscanf(p_ch + 6, "%x:%x:%x:%x:%x:%x:%x:%x",
Packit 13e616
		   &g[7], &g[6], &g[5], &g[4], &g[3], &g[2], &g[1], &g[0]) != 8)
Packit 13e616
	{
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"__get_port_guid_from_port_gid_tbl: ERR 5225: "
Packit 13e616
			"Fail to parse gid from gid_table file:(%s).\n", p_ch);
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	*port_guid =
Packit 13e616
	    (uint64_t) g[3] << 48 | (uint64_t) g[2] << 32 | (uint64_t) g[1] <<
Packit 13e616
	    16 | g[0];
Packit 13e616
	status = IB_SUCCESS;
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return status;
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Initialize an Info Struct for the Given HCA by its index 1..N
Packit 13e616
 **********************************************************************/
Packit 13e616
static ib_api_status_t
Packit 13e616
__osm_ca_info_init(IN osm_vendor_t * const p_vend,
Packit 13e616
		   IN uint32_t const idx, OUT osm_ca_info_t * const p_ca_info)
Packit 13e616
{
Packit 13e616
	ib_api_status_t status = IB_ERROR;
Packit 13e616
	uint8_t port_num;
Packit 13e616
	uint64_t port_guid;
Packit 13e616
Packit 13e616
	pfs_ca_info_t pfs_ca_info;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	/* parse the CA info file */
Packit 13e616
	if (__parse_ca_info_file(p_vend, idx, &pfs_ca_info) != IB_SUCCESS)
Packit 13e616
		goto Exit;
Packit 13e616
Packit 13e616
	p_ca_info->guid = cl_hton64(pfs_ca_info.guid);
Packit 13e616
Packit 13e616
	/* set size of attributes and allocate them */
Packit 13e616
	p_ca_info->attr_size = 1;
Packit 13e616
	p_ca_info->p_attr = (ib_ca_attr_t *) malloc(sizeof(ib_ca_attr_t));
Packit 13e616
Packit 13e616
	p_ca_info->p_attr->ca_guid = p_ca_info->guid;
Packit 13e616
	p_ca_info->p_attr->num_ports = pfs_ca_info.num_ports;
Packit 13e616
Packit 13e616
	/* now obtain the attributes of the ports */
Packit 13e616
	p_ca_info->p_attr->p_port_attr =
Packit 13e616
	    (ib_port_attr_t *) malloc(pfs_ca_info.num_ports *
Packit 13e616
				      sizeof(ib_port_attr_t));
Packit 13e616
Packit 13e616
	/* get all the ports info */
Packit 13e616
	for (port_num = 1; port_num <= pfs_ca_info.num_ports; port_num++) {
Packit 13e616
		pfs_port_info_t pfs_port_info;
Packit 13e616
		/* query the port attributes */
Packit 13e616
		if (__parse_port_info_file
Packit 13e616
		    (p_vend, idx, port_num, &pfs_port_info)) {
Packit 13e616
			osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
				"__osm_ca_info_init: ERR 5226: "
Packit 13e616
				"Fail to get HCA:%d Port:%d Attributes.\n", idx,
Packit 13e616
				port_num);
Packit 13e616
			goto Exit;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		/* HACK: the lids should have been converted to network but the rest of the code
Packit 13e616
		   is wrong and provdes them as is (host order) - so we stick with it. */
Packit 13e616
		p_ca_info->p_attr->p_port_attr[port_num - 1].lid =
Packit 13e616
		    pfs_port_info.lid;
Packit 13e616
		p_ca_info->p_attr->p_port_attr[port_num - 1].link_state =
Packit 13e616
		    pfs_port_info.state;
Packit 13e616
		p_ca_info->p_attr->p_port_attr[port_num - 1].sm_lid =
Packit 13e616
		    pfs_port_info.sm_lid;
Packit 13e616
Packit 13e616
		/* get the port guid */
Packit 13e616
		if (__get_port_guid_from_port_gid_tbl
Packit 13e616
		    (p_vend, idx, port_num, &port_guid)) {
Packit 13e616
			osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
				"__osm_ca_info_init: ERR 5227: "
Packit 13e616
				"Fail to get HCA:%d Port:%d Guid.\n", idx,
Packit 13e616
				port_num);
Packit 13e616
			goto Exit;
Packit 13e616
		}
Packit 13e616
		p_ca_info->p_attr->p_port_attr[port_num - 1].port_guid =
Packit 13e616
		    cl_hton64(port_guid);
Packit 13e616
	}
Packit 13e616
Packit 13e616
	status = IB_SUCCESS;
Packit 13e616
Exit:
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return (status);
Packit 13e616
}
Packit 13e616
Packit 13e616
void
Packit 13e616
osm_ca_info_destroy(IN osm_vendor_t * const p_vend,
Packit 13e616
		    IN osm_ca_info_t * const p_ca_info, IN uint8_t num_ca)
Packit 13e616
{
Packit 13e616
	osm_ca_info_t *p_ca;
Packit 13e616
	uint8_t i;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	for (i = 0; i < num_ca; i++) {
Packit 13e616
		p_ca = &p_ca_info[i];
Packit 13e616
Packit 13e616
		if (NULL != p_ca->p_attr) {
Packit 13e616
			if (0 != p_ca->p_attr->num_ports) {
Packit 13e616
				free(p_ca->p_attr->p_port_attr);
Packit 13e616
			}
Packit 13e616
Packit 13e616
			free(p_ca->p_attr);
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	free(p_ca_info);
Packit 13e616
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Fill in the array of port_attr with all available ports on ALL the
Packit 13e616
 * avilable CAs on this machine.
Packit 13e616
 **********************************************************************/
Packit 13e616
ib_api_status_t
Packit 13e616
osm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
Packit 13e616
			     IN ib_port_attr_t * const p_attr_array,
Packit 13e616
			     IN uint32_t * const p_num_ports)
Packit 13e616
{
Packit 13e616
	ib_api_status_t status = IB_SUCCESS;
Packit 13e616
Packit 13e616
	uint32_t caIdx;
Packit 13e616
	uint32_t ca_count = 0;
Packit 13e616
	uint32_t port_count = 0;
Packit 13e616
	uint8_t port_num;
Packit 13e616
	uint32_t total_ports = 0;
Packit 13e616
	osm_ca_info_t *p_ca_infos = NULL;
Packit 13e616
	uint32_t attr_array_sz = *p_num_ports;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	CL_ASSERT(p_vend);
Packit 13e616
Packit 13e616
	/* determine the number of CA's */
Packit 13e616
	ca_count = __hca_pfs_get_num_cas();
Packit 13e616
	if (!ca_count) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"osm_vendor_get_all_port_attr: ERR 5228: "
Packit 13e616
			"Fail to get Any CA Ids.\n");
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/* Allocate an array big enough to hold the ca info objects */
Packit 13e616
	p_ca_infos = malloc(ca_count * sizeof(osm_ca_info_t));
Packit 13e616
	if (p_ca_infos == NULL) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"osm_vendor_get_all_port_attr: ERR 5229: "
Packit 13e616
			"Unable to allocate CA information array.\n");
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	memset(p_ca_infos, 0, ca_count * sizeof(osm_ca_info_t));
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	 * For each CA, retrieve the CA info attributes
Packit 13e616
	 */
Packit 13e616
	for (caIdx = 1; caIdx <= ca_count; caIdx++) {
Packit 13e616
		status =
Packit 13e616
		    __osm_ca_info_init(p_vend, caIdx, &p_ca_infos[caIdx - 1]);
Packit 13e616
		if (status != IB_SUCCESS) {
Packit 13e616
			osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
				"osm_vendor_get_all_port_attr: ERR 5230: "
Packit 13e616
				"Unable to initialize CA Info object (%s).\n",
Packit 13e616
				ib_get_err_str(status));
Packit 13e616
			goto Exit;
Packit 13e616
		}
Packit 13e616
		total_ports += p_ca_infos[caIdx - 1].p_attr->num_ports;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	*p_num_ports = total_ports;
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_DEBUG,
Packit 13e616
		"osm_vendor_get_all_port_attr: total ports:%u \n", total_ports);
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	 * If the user supplied enough storage, return the port guids,
Packit 13e616
	 * otherwise, return the appropriate error.
Packit 13e616
	 */
Packit 13e616
	if (attr_array_sz >= total_ports) {
Packit 13e616
		for (caIdx = 1; caIdx <= ca_count; caIdx++) {
Packit 13e616
			uint32_t num_ports;
Packit 13e616
Packit 13e616
			num_ports = p_ca_infos[caIdx - 1].p_attr->num_ports;
Packit 13e616
Packit 13e616
			for (port_num = 0; port_num < num_ports; port_num++) {
Packit 13e616
				p_attr_array[port_count] =
Packit 13e616
				    *__osm_ca_info_get_port_attr_ptr(&p_ca_infos
Packit 13e616
								     [caIdx -
Packit 13e616
								      1],
Packit 13e616
								     port_num);
Packit 13e616
				port_count++;
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
	} else {
Packit 13e616
		status = IB_INSUFFICIENT_MEMORY;
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	status = IB_SUCCESS;
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
	if (p_ca_infos) {
Packit 13e616
		osm_ca_info_destroy(p_vend, p_ca_infos, ca_count);
Packit 13e616
	}
Packit 13e616
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return (status);
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Given the vendor obj and a port guid
Packit 13e616
 * return the ca id and port number that have that guid
Packit 13e616
 **********************************************************************/
Packit 13e616
Packit 13e616
ib_api_status_t
Packit 13e616
osm_vendor_get_guid_ca_and_port(IN osm_vendor_t * const p_vend,
Packit 13e616
				IN ib_net64_t const guid,
Packit 13e616
				OUT uint32_t * p_hca_hndl,
Packit 13e616
				OUT char *p_hca_id,
Packit 13e616
				OUT uint8_t * p_hca_idx,
Packit 13e616
				OUT uint32_t * p_port_num)
Packit 13e616
{
Packit 13e616
	uint32_t caIdx;
Packit 13e616
	uint32_t ca_count = 0;
Packit 13e616
	uint8_t port_num;
Packit 13e616
	ib_api_status_t status = IB_ERROR;
Packit 13e616
Packit 13e616
	OSM_LOG_ENTER(p_vend->p_log);
Packit 13e616
Packit 13e616
	CL_ASSERT(p_vend);
Packit 13e616
Packit 13e616
	/* determine the number of CA's */
Packit 13e616
	ca_count = __hca_pfs_get_num_cas();
Packit 13e616
	if (!ca_count) {
Packit 13e616
		osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
			"osm_vendor_get_guid_ca_and_port: ERR 5231: "
Packit 13e616
			"Fail to get Any CA Ids.\n");
Packit 13e616
		goto Exit;
Packit 13e616
	}
Packit 13e616
Packit 13e616
	/*
Packit 13e616
	 * For each CA, retrieve the CA info attributes
Packit 13e616
	 */
Packit 13e616
	for (caIdx = 1; caIdx <= ca_count; caIdx++) {
Packit 13e616
		pfs_ca_info_t pfs_ca_info;
Packit 13e616
		if (__parse_ca_info_file(p_vend, caIdx, &pfs_ca_info) ==
Packit 13e616
		    IB_SUCCESS) {
Packit 13e616
			/* get all the ports info */
Packit 13e616
			for (port_num = 1; port_num <= pfs_ca_info.num_ports;
Packit 13e616
			     port_num++) {
Packit 13e616
				uint64_t port_guid;
Packit 13e616
				if (!__get_port_guid_from_port_gid_tbl
Packit 13e616
				    (p_vend, caIdx, port_num, &port_guid)) {
Packit 13e616
					if (cl_hton64(port_guid) == guid) {
Packit 13e616
						osm_log(p_vend->p_log,
Packit 13e616
							OSM_LOG_DEBUG,
Packit 13e616
							"osm_vendor_get_guid_ca_and_port: "
Packit 13e616
							"Found Matching guid on HCA:%d Port:%d.\n",
Packit 13e616
							caIdx, port_num);
Packit 13e616
						strcpy(p_hca_id,
Packit 13e616
						       pfs_ca_info.name);
Packit 13e616
						*p_port_num = port_num;
Packit 13e616
						*p_hca_idx = caIdx - 1;
Packit 13e616
						*p_hca_hndl = 0;
Packit 13e616
						status = IB_SUCCESS;
Packit 13e616
						goto Exit;
Packit 13e616
					}
Packit 13e616
				}
Packit 13e616
			}
Packit 13e616
		}
Packit 13e616
	}
Packit 13e616
Packit 13e616
	osm_log(p_vend->p_log, OSM_LOG_ERROR,
Packit 13e616
		"osm_vendor_get_guid_ca_and_port: ERR 5232: "
Packit 13e616
		"Fail to find HCA and Port for Port Guid 0x%" PRIx64 "\n",
Packit 13e616
		cl_ntoh64(guid));
Packit 13e616
	status = IB_INVALID_GUID;
Packit 13e616
Packit 13e616
Exit:
Packit 13e616
Packit 13e616
	OSM_LOG_EXIT(p_vend->p_log);
Packit 13e616
	return (status);
Packit 13e616
}
Packit 13e616
Packit 13e616
#endif