#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# Copyright (c) 2003 InfiniCon Systems, Inc.  All rights reserved.
#
# THIS SOFTWARE IS PROVIDED BY INFINICON SYSTEMS, INC. ("INFINICON") TO EACH
# PERSON OR COMPANY ("RECIPIENT") ON AN "AS IS" BASIS.  ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL INFINICON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED OR ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# Any agreements between InfiniCon and the Recipient shall apply to Recipient's
# use of the Software.
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: @(#) ./bin/iba_hca_rev 7_4_3_0_2 [01/26/18 09:02]

Usage()
{
	cat <<EOF >&2
Usage: iba_hca_rev [-v]
     -v - more verbose/detailed output about the HCAs
This scans the system and reports hardware and firmware information about
all the HCAs in the system.
EOF
	exit 2
}

verbose=n
while getopts v param
do
	case $param in
	v)	verbose=y;;
	?)	Usage;;
	esac
done
shift $((OPTIND -1))
if [ $# -ge 1 ]
then
	Usage
fi

hcas=`/sbin/lspci | egrep -i 'InfiniBand|Mellanox' | grep -vi bridge | cut -d\  -f1`

if [ -z "$hcas" ]
then 
	echo "No HCAs found."
else
	for hca in $hcas
	do
		echo "######################"
		echo `hostname` " - HCA $hca"
		# prepend to pci address as needed
		if [ -e "/sys/bus/pci/drivers/ib_ipath/0000:$hca" \
			-o -e "/sys/bus/pci/drivers/ib_qib/0000:$hca" \
			-o -e "/sys/bus/pci/drivers/ib_mthca/0000:$hca" \
			-o -e "/sys/bus/pci/drivers/mlx4_core/0000:$hca" ]
		then
			hca_pci="0000:$hca"
		fi
		if [ -e "/sys/bus/pci/drivers/ib_ipath/$hca_pci" ]
		then
			dir=/sys/bus/pci/drivers/ib_ipath/$hca_pci
			# ipath HCA
			eval 2>/dev/null read boardver < $dir/boardversion
			eval 2>/dev/null read serial < $dir/serial
			eval 2>/dev/null read localbus_info < $dir/localbus_info
			eval 2>/dev/null read guid < $dir/guid
			echo "Board: $boardver"
			echo "SN:    $serial"
			echo "Bus:   $localbus_info"
			echo "GUID:  $guid"
		elif [ -e "/sys/bus/pci/drivers/ib_qib/$hca_pci" ]
		then
			dir=/sys/bus/pci/drivers/ib_qib/$hca_pci
			instance=$(cd $dir; echo qib[0-9]*)
			if [ ! -e "$dir/$instance" ]
			then
				instance=$(cd $dir; echo infiniband:qib[0-9]*|sed -e 's/infiniband://')
				if [ ! -e "$dir/infiniband:$instance" ]
				then
					instance=none
					for dev in /sys/class/infiniband/qib[0-9]*
					do
						if [ "$dev/device" -ef "$dir" ]
						then
							instance=$(basename $dev)
						fi
					done
				fi
			fi
			# ipath HCA w/newer driver
			if [ -e "/sys/class/infiniband/$instance" ]
			then
				dir=/sys/class/infiniband/$instance
				eval 2>/dev/null read boardver < $dir/boardversion
				eval 2>/dev/null read serial < $dir/serial
				eval 2>/dev/null read localbus_info < $dir/localbus_info
				eval 2>/dev/null read guid < $dir/node_guid
				echo "Board: $boardver"
				echo "SN:    $serial"
				echo "Bus:   $localbus_info"
				echo "GUID:  $guid"
			fi
		else
			# Mellanox HCA
			if [ -e /sbin/mstvpd ]
			then
   				/sbin/mstvpd $hca
			else
				/usr/bin/mstvpd $hca
			fi
			if [ $? != 0 ]
			then
				serialno="HCA does not have a VPD area."
				echo "SN:    $serialno"
			fi

			if [ -e "/sys/bus/pci/drivers/ib_mthca/$hca_pci" ]
			then
				prefix=mthca
				dir=/sys/bus/pci/drivers/ib_mthca/$hca_pci
			elif [ -e "/sys/bus/pci/drivers/mlx4_core/$hca_pci" ]
			then
				dir=/sys/bus/pci/drivers/mlx4_core/$hca_pci
				prefix="mlx4_"
			fi
			instance=$(cd $dir; echo $prefix[0-9]*)
			if [ ! -e "$dir/$instance" ]
			then
				instance=$(cd $dir; echo infiniband:$prefix[0-9]*|sed -e 's/infiniband://')
				if [ ! -e "$dir/infiniband:$instance" ]
				then
					instance=none
					for dev in /sys/class/infiniband/$prefix[0-9]*
					do
						if [ "$dev/device" -ef "$dir" ]
						then
							instance=$(basename $dev)
						fi
					done
				fi
			fi
			if [ -e "/sys/class/infiniband/$instance" ]
			then
				dir=/sys/class/infiniband/$instance
				eval 2>/dev/null read fw_ver < $dir/fw_ver
				echo "FW: $fw_ver"
			fi

			if [ -e /sbin/mstflint ]
			then
				mstflint=/sbin/mstflint
			else
				mstflint=/usr/bin/mstflint
			fi
			$mstflint -d $hca q
			if [ $verbose = y ]
			then
				echo
				echo "Firmware Configuration:"
				$mstflint -d $hca dc
				echo
				echo "Firmware Verification:"
				$mstflint -d $hca v
			fi
		fi

		echo "######################"
	done
fi
