#!/bin/sh
#
# Copyright (c) 2012 Brocade Communications Systems, Inc.
# All rights reserved.
#
# Description:
#       Script to set the PRBS and Loopback mode for Dragon2T.
#

DEV_NULL="/dev/null"

# register offsets of port 0-15
declare -a reg=( 0xe5782030 0xe5782030 0xe5782030 0xe5782030 0xe57a2030 0xe57a2030 0xe57a2030 0xe57a2030 0xe57a2030 0xe57a2030 0xe5792030 0xe5792030 0xe5792030 0xe5792030 0xe5792030 0xe5792030 )

# PRBS setting of port 0-15
declare -a prbsval=( 0x02018002 0x01018032 0x01018022 0x01018012 0x02018012 0x02018002 0x01018032 0x01018022 0x01018012 0x01018002 0x02018032 0x02018022 0x02018012 0x02018002 0x01018032 0x01018022 )

# register offset of loop_intr_enable of port 0-15
declare -a lp_intr_enable=( 0xe59c2020 0xe59b2020 0xe59a2020 0xe5992020 0xe5ad2020 0xe5ac2020 0xe5ab2020 0xe5aa2020 0xe5a92020 0xe5a82020 0xe5a72020 0xe5a62020 0xe5a52020 0xe5a42020 0xe5a32020 0xe5a22020 )

# register offset of loop_ctl of port 0-15
declare -a lp_ctl=( 0xe59c2018 0xe59b2018 0xe59a2018 0xe5992018 0xe5ad2018 0xe5ac2018 0xe5ab2018 0xe5aa2018 0xe5a92018 0xe5a82018 0xe5a72018 0xe5a62018 0xe5a52018 0xe5a42018 0xe5a32018 0xe5a22018 )

# register offset of loop_intr_status of port 0-15
declare -a lp_intr_stat=( 0xe59c201c 0xe59b201c 0xe59a201c 0xe599201c 0xe5ad201c 0xe5ac201c 0xe5ab201c 0xe5aa201c 0xe5a9201c 0xe5a8201c 0xe5a7201c 0xe5a6201c 0xe5a5201c 0xe5a4201c 0xe5a3201c 0xe5a2201c )

# register offset of loop_config of port 0-15
declare -a lp_cfg=( 0xe59c2014 0xe59b2014 0xe59a2014 0xe5992014 0xe5ad2014 0xe5ac2014 0xe5ab2014 0xe5aa2014 0xe5a92014 0xe5a82014 0xe5a72014 0xe5a62014 0xe5a52014 0xe5a42014 0xe5a32014 0xe5a22014 )


function usage
{
	echo
	echo "Usage: `basename $0` [--prbs|--bypass] port_num"
	echo "  --prbs: set the PRBS mode"
	echo "  --bypass: set the bypass mode" 
	echo "  port_num: user port number"
	echo
}

function show_warning
{
	echo "Warning: this tool can be used to set the PRBS or Bypass mode of the ports."
	echo "The port status must be \"Online\" before starting the tests and the HBA must be"
	echo "configured as fixed speed; traffic must be stopped before configuring the modes."
	echo "After the test, the switch should be rebooted to restore the normal mode."
	PROMPT="Do you want to proceed ? [N] "
	while true
	do
	 read -ep "$PROMPT" REPLY
	 case `echo $REPLY | /usr/bin/tr [:lower:] [:upper:]` in
	  N|NO|"") exit 0 ;;
	  Y|YES) break ;;
	 esac
	done
}

function check_para
{
	if [[ $2 -lt 0 ]] || [[ $2 -gt 15 ]]; then
		echo "Invalid port number; range 0-15"
		usage
		exit 1;
	fi

	case $1 in
	--prbs|--bypass)
		;;
	*)
	echo "Invalid mode: [--prbs|--bypass]"
		usage
		exit 1;
		;;
	esac
}

function bypass_mode_set
{
	declare -i regstat
	declare -i newstat
	declare -i cfgstat
	declare -i cfgstat1
	declare -i cfgstat0
	declare -i finalstat
	declare -i finalstat1

	reg1=${lp_intr_enable[$1]}
	reg2=${lp_ctl[$1]}
	reg3=${lp_intr_stat[$1]}
	reg4=${lp_cfg[$1]}
	echo "Port mode is being changed; please wait..."
	# step 1: poke fpl_loop_intr_enable to 0x40  # bit 7 set to 1
	#echo "step 1: poke fpl_loop_intr_enable register $reg1"
	orireg=`/fabos/cliexec/exec_diag peek -addr $reg1 | awk '{ print $3 }'`
	let "newreg=$orireg | 64"
	/fabos/cliexec/exec_diag poke -addr $reg1 -data $newreg >${DEV_NULL} 2>&1
	sleep 1
	newval=`/fabos/cliexec/exec_diag peek -addr $reg1 | awk '{ print $3 }'`
	#echo "fpl_loop_intr_enable: $newval"

	# step 2: poke fpl_loop_ctl to 0x40       # bit 7 set to 1; request monitoring
	#echo "step2: poke fpl_loop_ctl register $reg2"
	orireg=`/fabos/cliexec/exec_diag peek -addr $reg2 | awk '{ print $3 }'`
	let "newreg=$orireg | 64"
	/fabos/cliexec/exec_diag poke -addr $reg2 -data $newreg >${DEV_NULL} 2>&1
	# wait for 1 second
	sleep 1

	# step 3: verify: peek fpl_loop_intr_status is 0x40  # already in monitoring state
	regstat=`/fabos/cliexec/exec_diag peek -addr $reg3 | awk '{ print $3 }'`
	#let "newstat=($regstat >> 6) & 1"	
	#printf "step 3: port=%d, fpl_loop_intr_status regstat=%x valueofbit7=%x\n"\
	#	$1 $regstat $newstat

	#if [[ $newstat == 0 ]]; then
	#	echo "Error: not in monitoring state"
	#	exit 1
	#fi

	# step 4: set bit 4 of fpl_loop_config to 1 then 0:
	cfgstat=`/fabos/cliexec/exec_diag peek -addr $reg4 | awk '{ print $3 }'`
	let "cfgstat1=$cfgstat | 16"
	let "cfgstat0=$cfgstat & ~16"
	#printf "step 4: set fpl_loop_config cfgstat=%x, bit 4 to 1 cfgstat1=%x, bit 4 to 0 cfgstat0=%x\n"\
	#	$cfgstat $cfgstat1 $cfgstat0

	/fabos/cliexec/exec_diag poke -addr $reg4 -data $cfgstat1 >${DEV_NULL} 2>&1
	sleep 10
	/fabos/cliexec/exec_diag poke -addr $reg4 -data $cfgstat >${DEV_NULL} 2>&1
	sleep 1

	# step 5: verify: peek fpl_loop_intr_status is still 0x40
	#finalstat=`/fabos/cliexec/exec_diag peek -addr $reg3 | awk '{ print $3 }'`
	#let "finalstat1=($finalstat >> 6) & 1"
	#printf "step 5: port=%d,  peak fpl_loop_intr_status finalstat=%x, valueofbit7=%x\n"\
	#        $1 $finalstat $finalstat1

	#if [[ $finalstat1 == 0 ]]; then
	 #       echo "Error: invalid monitoring state fpl_loop_intr_status: $finalstat1"
	 #       exit 1
	#fi
}

if [[ $# -lt 2 ]] || [[ $# -ge 3 ]] ;then
	usage
	exit 1;
fi

check_para $1 $2
show_warning

case $1 in
--prbs)
	regoffset=${reg[$2]}
	val=${prbsval[$2]}
	/fabos/cliexec/exec_diag poke -addr $regoffset -data $val >${DEV_NULL} 2>&1
	echo "Completed \"prbs\" mode setting for port $2."
	;;
--bypass)
	bypass_mode_set $2
	echo "Completed \"bypass\" mode setting for port $2."
	;;
*)
	usage
	exit 1
esac

exit 0
