#!/bin/bash
#*
#* Copyright (c) 2007 QLogic, Inc.  All rights reserved.
#*
#* This software is available to you under a choice of one of two
#* licenses.  You may choose to be licensed under the terms of the GNU
#* General Public License (GPL) Version 2, available from the file
#* COPYING in the main directory of this source tree, or the
#* OpenIB.org BSD license below:
#*
#*     Redistribution and use in source and binary forms, with or
#*     without modification, are permitted provided that the following
#*     conditions are met:
#*
#*      - Redistributions of source code must retain the above
#*        copyright notice, this list of conditions and the following
#*        disclaimer.
#*
#*      - Redistributions in binary form must reproduce the above
#*        copyright notice, this list of conditions and the following
#*        disclaimer in the documentation and/or other materials
#*        provided with the distribution.
#*
#* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
#* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
#* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#* SOFTWARE.
#*

SYS_QLGC_VNIC_DIR=/sys/class/infiniband_qlgc_vnic
INTERFACES_DIR=interfaces
PRIMARY_PATH="primary_path"
SECONDARY_PATH="secondary_path"
INTERFACE_FILES="vnic_state current_path rx_csum tx_csum"
HOST_FILES="viport_state link_state hca_info heartbeat" 
TARGET_FILES="ioc_string ioc_guid dgid pkey" 

print_sys_file()
{
	FILE_NAME=$1						# sys file name to print
	case $FILE_NAME in
		"current_path")
				echo -n "    Current Path      : "
		;;
		"rx_csum")
				echo -n "    Receive Checksum  : "
		;;
		"tx_csum")
				echo -n "    Transmit checksum : "
		;;
		"vnic_state")
				echo -n "    VNIC State        : "
		;;
		"dgid")
				echo -n "        DGID          : "
		;;
		"hca_info")
				echo -n "        HCA Info.     : "
		;;
		"heartbeat")
				echo -n "        Heartbeat     : "
		;;	
		"ioc_guid")
				echo -n "        IOC GUID      : "
		;;
		"ioc_string")
				echo -n "        IOC String    : "
		;;
		"link_state")
				echo -n "        Link State    : "
		;;
		"pkey")
				echo -n "        P Key         : "
		;;
		"viport_state")
				echo -n "        VIPORT State  : "
		;;
		*)
				echo -n "        $FILE_NAME    : "    
		;;
	esac

	if [ -f $FILE_NAME ]
	then
		cat $FILE_NAME
		if [ $? -ne 0 ] 
		then
			echo " N/A. "
		fi
	else
		echo " N/A. "
	fi
}

# Function to collect the path information for a given VNIC interface.

process_path_info()
{
	VNIC_PATH=$1
	cd $VNIC_PATH

	echo " "
	echo "    $2 Path : "

	for path_entry in $HOST_FILES
	do
		print_sys_file $path_entry
	done

	for path_entry in $TARGET_FILES
	do
		print_sys_file $path_entry
	done

	cd ..
}

# Collects complete infromation about VNIC interface along with its paths.

get_vnic_interface_info()
{
	VNIC_INTERFACE=$1					# VNIC interface name argument to function
	cd $VNIC_INTERFACE
	
	for entry in $INTERFACE_FILES
	do
		print_sys_file $entry 
	done

	if [ -d $PRIMARY_PATH ]
	then
		process_path_info $PRIMARY_PATH "Primary"
	fi

	if [ -d $SECONDARY_PATH ]
	then
		process_path_info $SECONDARY_PATH "Secondary"
	fi

	cd ..
}

#Main
WORKING_DIR=`pwd`
if [ ! -d $SYS_QLGC_VNIC_DIR/$INTERFACES_DIR ]
then
	echo "$SYS_QLGC_VNIC_DIR/$INTERFACES_DIR does not exist. Check if qlgc_vnic module is loaded or not."
	exit 1
fi

cd $SYS_QLGC_VNIC_DIR/$INTERFACES_DIR

# Loop to read VNIC interfaces directories from /sys/class/infiniband_qlgc_vnic/interfaces
# We will ignore the symbolic links.

VNIC_EXISTS=0
for interface in $(ls)
do
	if [[ ( -d $interface) && ( ! -L $interface) ]]
	then
		VNIC_EXISTS=1
		echo " "
		echo "VNIC Interface : $interface"
		get_vnic_interface_info $interface
	fi
done

if [ $VNIC_EXISTS -eq 0 ]
then
	echo "No VNIC Interfaces have been configured on this host."
fi

cd $WORKING_DIR
