#!/bin/sh
#
#
#    Copyright (c) 2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      bladediag - run a suit of diagnostic tests on a switch blade
# 
# SYNOPSIS
#      bladediag [[ --slot ] slotno ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This command will run a suite of diagnostics  tests  on  the
#      specified  switch  blade,  which  includes portregtest, cen-
#      tralmemorytest,  cmitest,  camtest,  filtertest,  statstest,
#      portloopbacktest,   txdpath,   crossporttest,  turboramtest,
#      spinsilk, backport and diagshow which will show  the  status
#      of  the blade. Compared to bladediagshort, this is a compre-
#      hensive test for blade  functionality  which  also  involves
#      backplane connectivity test.
# 
#      (CAUTION)  To run this test, it is required to install loop-
#      back plugs on every port.  Run systemtest otherwise.
# 
# OPTIONS
#      [ --slot ] slotno
#                Specify which slot to operate on. If  this  option
#                is not specifed, the current slot will be assumed.
#                You can set the current slot by execution of  set-
#                slot command.
# 
# EXAMPLES
#      > bladediag --slot 1
#      Testing slot: 1, user ports: 3 2 1 0 7 6 5 4 11 10 ......
#      PortRegTest
#      Running Port Register Test ....
#      passed.
#      Test Complete: "portregtest" Pass 1 of 1
#      Duration 0 hr, 2 min & 14 sec (0:2:14:655).
#      passed.
#      Test return status: 0
#      CentralMemoryTest
#      Running centralmemorytest ............ passed.
#      Test Complete: "centralmemorytest" Pass 1 of 1
#      Duration 0 hr, 0 min & 18 sec (0:0:18:415).
#      passed.
#      Test return status: 0
#      --- <output truncated> ---
# 
# DIAGNOSTICS
#      Each  diagnostic test in this suit may report its own set of
#      error messages when it detects failure(s).  See  DIAGNOSTICS
#      section of individual diagnostic test help pages.
# 
# SEE ALSO
#      backport(1d),  bladediagshort(1d), camtest(1d), centralmemo-
#      rytest(1d),  cmitest(1d),  crossporttest(1d),  diagshow(1d),
#      filtertest(1d),  portloopbacktest(1d), portregtest(1d), set-
#      slot(1d),   spinsilk(1d),   statstest(1d),   systemtest(1d),
#      turboramtest(1d), txdpath(1d)
# 

#
# Get list of active slots and current default.
#
slot_list=`diagshow | /bin/grep Slot | /usr/bin/cut -d" " -f 2 | /usr/bin/xargs /bin/echo`
if [ -z "$slot_list" ] ; then slot_list="0"; fi
def_slot=`/bin/echo $slot_list | /usr/bin/cut -d" " -f 1`


# okToRun
#
# Test if required blades are disabled.
# if not whine and exit.
#
# $1 command path to exec_diag
# $FABOS_SLOTNO Current slot to test.
#
okToRun () {
	#
	# Find user ports with POST disabled so we don't get extra
	# bogus error status messages.
	#
	export DIAG_POST=0

	#
	# Check that selected slot is a valid switch blade.
	#
	_rval="0";
	for _tmp in $slot_list ; do
	    if [ "$_tmp" == "$FABOS_SLOTNO" ] ; then _rval=1; fi
	done
	if [ "0" == "$_rval" ] ; then
		if [ $multiBlade = TRUE ]
		then
		    /bin/echo "Can not run test on the slot ($slot)."
		    /bin/echo "Valid slot numbers are: $slot_list"
		else
		    /bin/echo "Can not run test."
		fi
		exit -3
	fi

	#
	# Make sure that there are no pending errors on the
	# current blade.
	#
	diagshow -use_bports 1 -silent 1 > /dev/null
	if [ "0" != "$?" ] ; then
		/bin/echo "WARNING -- There are errors from a previous test."
		/bin/echo ""
		/bin/echo "They will be cleared now so that we can continue."
		diagclearerror --slot $FABOS_SLOTNO
	fi


	#
	# Make sure we have some user ports
	#
	user_ports=`diagshow -use_bports 1 | /bin/grep "   OK" | /usr/bin/cut -d " " -f 2 | /bin/grep -v "\-\-" | /bin/sort -n | /usr/bin/xargs /bin/echo`

	if [ -z "$user_ports" ] ;
	then
	     echo "Could not find any ports to test."
	     exit 3 ;
	fi

	#
	# Make sure that the switch is disabled.
	#
	_cmd=`commandPath diagOkToRun`
	$_cmd -silent 1 > /dev/null 2>&1
	_rval=$?
	if [ "1" -ne "$_rval" ]; then
		echo ""
		echo "WARNING:  Switch is not disabled.  This test can not"
        	echo "          be performed on a fabric operational switch."
		echo ""
		echo "Please disable the entire switch with the switchDisable"
		echo "command and try again."
		exit -3
    	fi

}

# do_command will perform the command passed in as arguments.
# Each command can be processed by a common method.
#
# Arguments $* is the commands to perform
#
do_command() {
    $*
    _testresult=$?
    if [ `getSWBD` = SWBD12 ]
    then
	if [ $_testresult -eq 139 -o $_testresult -eq 237 ]
	then
	    echo " SKIPPED $1 due to Known Issue. Please run following command manually."
	    echo "$*"
	else
	    if [ $_testresult -ne 0 ]
	    then
		status=`expr $status + 1`
	    fi
	fi
    else
	if [ $_testresult -ne 0 ]
	then
	    status=`expr $status + 1`
	fi
    fi
}

#
#
# Find exec_diag
#
status=0
#
# source diagcommon
#
home="/fabos/share"
util="diagcommon.sh"
export syntax="$0 -slot [slot]"
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	err "Could not locate $util"
	exit 3
fi

multiBlade=`isMultiBlade`

# Support switchdiag
if [ "`echo $0 | /bin/grep blade`" = "" ]
then
     # invoked as switch command
     if [ $multiBlade = TRUE ]
     then
	  /bin/echo "Please use bladediag on Modular Platform products."
	  /bin/echo "Invoking bladediag command for you."
     fi
else
     # invoked as blade command
     if [ $multiBlade = FALSE ]
     then
	  /bin/echo "Please use switchdiag on fixed Port Count products."
	  /bin/echo "Invoking switchdiag command for you."
     fi
fi

#
# Get slot number
#
s=`/usr/bin/expr "$1" : "[-]*slot"`
if [ "0" -ne "$s" ] ; then
	x=`/usr/bin/expr "$2" : "\([0-9]\+\)"`
else
	x=`/usr/bin/expr "$1" : "\([0-9]\+\)"`
fi

if [ -z $x ] ; then
     ans="`isValidSlot $FABOS_SLOTNO`"
     if [ "$ans" = TRUE ] ; then
        slot=$FABOS_SLOTNO
     else
        slot=$def_slot
     fi
else
     ans="`isValidSlot $x`"
     avail=`getAvailSlots`
     if [ "$ans" = TRUE ] ; then
        slot=$x
     else 
        err "$x is an invalid slot number"
	if [ $multiBlade = TRUE ] ; then
	     /bin/echo "Available slots are: $avail."
	fi
        exit 4
     fi
fi

#
# bladediag()
#

# Set up slot number.
export FABOS_SLOTNO=$slot

#
# Make sure everything is OK for this run.
# Also set user_ports list.
#
okToRun


# Enable POST mode e.g. no stop on error.
export DIAG_POST=1

echo "Testing user ports: $user_ports"
echo ""

# detect and handle bplb_mode
bplb_mode=`config get diag.mode.bplb 2`
if [ -z "$bplb_mode" ]
then
    bplb_mode=0
fi
if [ "$bplb_mode" != "0" ]
then
  echo "NOTE: BPLB mode is ON"
  do_all_tests=FALSE
  ptlb_mode=1
  mcyc_megs=20
else
  do_all_tests=TRUE
  ptlb_mode=2
  mcyc_megs=7
fi
if [ $multiBlade = TRUE ]
then
  bprt_nmegs=20
else
  bprt_nmegs=10
fi
#
# Allow port reg test and central memory to test all ports
#
export DCOMM_USR_FAILLIM=64
echo ""
echo "TurboRamTest"
do_command turboramtest -passcnt 1
echo ""
echo "PortRegTest"
do_command portRegTest
echo ""
echo "CentralMemoryTest"
do_command centralMemoryTest
echo ""
echo "CmiTest"
do_command cmiTest
echo ""
echo "CamTest"
do_command camTest
echo ""
echo "FilterTest"
do_command filtertest -passcnt 50 -txports $user_ports
echo ""
echo "StatsTest"
do_command statstest -passcnt 25 -ports $user_ports
echo ""
echo "PortLoopbackTest -- internal"
do_command portLoopbackTest -nframes 100 -lb_mode 5 -spd_mode 0
echo ""
echo "PortLoopbackTest -- serdes"
do_command portLoopbackTest -nframes 100 -lb_mode $ptlb_mode -spd_mode 2
echo ""
echo "Txdpath -- internal"
do_command txdpath -nframes 25 -spd_mode 2
echo ""
echo "Txdpath -- serdes"
do_command txdpath -nframes 25 -lb_mode 2 -spd_mode 2
echo ""
echo "CrossPortTest -- serdes"
do_command crossPortTest -nframes 200 -lb_mode 2 -ports $user_ports
echo ""
echo "CrossPortTest -- cable 1G"
do_command crossPortTest -nframes 200 -lb_mode 1 -spd_mode 1 -ports $user_ports
echo ""
echo "CrossPortTest -- cable 2G"
do_command crossPortTest -nframes 200 -lb_mode 1 -spd_mode 2 -ports $user_ports
echo ""
echo "SpinSilk -- internal"
do_command spinSilk -lb_mode 5 -ports $user_ports -nmegs 50
echo ""
echo "SpinSilk -- serdes"
do_command spinSilk -lb_mode 2 -ports $user_ports -nmegs 50
echo ""
echo "MiniCycle -- cable"
do_command minicycle -nmegs $mcyc_megs -lb_mode 1
echo ""
echo "Spinsilk -- loopback cable all blades"
do_command spinSilk -lb_mode 1 -nmegs 50
echo ""
echo "BackPort -- current blade"
do_command backPort -nframes $bprt_nmegs -lb_mode 5 -ports $user_ports
if [ "$do_all_tests" = "TRUE" ]
then
    echo ""
    echo "BackPort -- all blades"
    do_command backPort -nframes $bprt_nmegs -lb_mode 5 -extonly 1
fi
echo ""
echo "Test complete, overall status"
do_command diagShow -use_bports 1 > /dev/null
if [ $status -ne 0 ]
then
    echo "bladeDiag FAILED"
else
    echo "bladeDiag PASSED"
fi
