#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   ptSupportShow.sh.in
#    Module name: diag/scripts
#
#  NAME
#     ptsupportshow - Display port and ASIC information.
#
#  SYNOPSIS
#     ptsupportshow [slotnumber] portnumber | [ slotnumber ] -all
#
#  AVAILABILITY
#     all users
#
#  DESCRIPTION
#     Use  this  command  to  display  port  and ASIC information,
#     include backend ports.
#
#  OPTIONS
#     [slotnumber] portnumber
#               Specify the index of the  port  within  the  blade
#               specified by slotnumber to be displayed.  If slot-
#               number is not specified, The default is set  to  0
#               and  designed to operate on fixed port count prod-
#               ucts.
#
#     [ slotnonumber ] -all
#               Specify which slot to operate on. The entire ports
#               in  the  slot will be displayed.  If slotnumber is
#               not  specified,  The  default  is  set  to  0  and
#               designed  to operate on fixed port count products.
#
#  NOTES
#     This command not intended for normal user operation  and  is
#     available  to provide supplemental information during system
#     debug.
#

export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin

# Check RBAC permission on command
/fabos/libexec/rbac_check `/bin/basename $0`

if [ $? -ne 0 ]; then
    exit 127
fi

#set ROLE to root pass remaining RBAC checks. Don't do it before rbac_check
export ROLE_ID=root

DBGCMD=""
TTY="/dev/null"
sslog=0
sslogfile=/var/log/ss.log
SUPPORTSAVE=FALSE
#
# debug routine to show time for each CLI
#
#
cmd_time() {
echo -n . > $TTY
##echo -e "\n$1 $2 $3 $4 $5 $6 $7 $8 $9:"
time $1 $2 $3 $4 $5 $6 $7 $8 $9
}

#
# debug routine to show dot for each CLI
#
#
cmd_dot() {
echo -n . > $TTY
## /bin/echo -e "\n$1 $2 $3 $4 $5 $6 $7 $8 $9:"
sslog=`/usr/bin/expr $sslog + 1`
/bin/echo "$sslog" >> $sslogfile
/bin/echo "$1 $2 $3 $4 $5 $6 $7 $8 $9"  >> $sslogfile
time $1 $2 $3 $4 $5 $6 $7 $8 $9
}


#
# debug routine to show dot for each CLI
#
#
cmd() {
##echo -e "\n$1 $2 $3 $4 $5 $6 $7 $8 $9:"
$1 $2 $3 $4 $5 $6 $7 $8 $9
}


# pt Commands to issue.  Must take slot/port as argument.
portCmdsList="ptStatsShow ptRegShow ptDataShow ptFilterPortShow ptRouteShow"


#
# Port Commands
#
portShow() {
  if [ $multiBlade != TRUE ]; then
    echo
    echo ----------------  PORT $port_no  ------------------
    for portCmd in $portCmdsList
    do
	  #
	  # Defect 61855
	  # ptstatsshow, ptdatashow commands should not be issued for pinball
	  #
	  if  [ $pinballPort == "TRUE" -o $cpsasPort == "TRUE" ]; then
		if [ $portCmd == "ptRouteShow" -o $portCmd == "ptDataShow" -o $portCmd == "ptFilterPortShow" ]; then
		  continue
		fi
	  fi
      echo
      echo "> "$portCmd $port_no
      $DBGCMD $portCmd $port_no
    done

  else
    echo
    echo ----------------  SLOT $slot_no    PORT $port_no  ------------------
    for portCmd in $portCmdsList
    do
	  if  [ $pinballPort == "TRUE" -o $cpsasPort == "TRUE" ]; then
		if [ $portCmd == "ptRouteShow" -o $portCmd == "ptDataShow" -o $portCmd == "ptFilterPortShow" ]; then
		  continue
		fi
	  fi
      echo
      echo "> "$portCmd $slot_no/$port_no
      $DBGCMD $portCmd $slot_no/$port_no
    done

  fi
}

#
# Chip Commands
#
chipShow() {
  if [ $multiBlade != TRUE ]; then
    echo
    echo ----------------  CHIP $chip_no  ------------------
    echo "> "chipregshow $chip_no
    $DBGCMD chipregshow $chip_no
  else
    echo
    echo ----------------  SLOT $slot_no    CHIP $chip_no  ------------------
    echo "> "chipregshow $slot_no/$chip_no
    $DBGCMD chipregshow $slot_no/$chip_no
  fi
}

cpsasChipShow() {
  if [ $multiBlade != TRUE ]; then
    echo
    echo ----------------  CHIP 0 ------------------
    echo "> "ptchipregshow 0
    $DBGCMD ptchipregshow 0
    echo
    echo ----------------  CHIP 1 ------------------
    echo "> "ptchipregshow 42
    $DBGCMD ptchipregshow 42
  else
    echo
    echo ----------------  SLOT $slot_no    CHIP 0  ------------------
    echo "> "ptchipregshow 0
    $DBGCMD ptchipregshow $slot_no/0
    echo
    echo ----------------  SLOT $slot_no    CHIP 1  ------------------
    echo "> "ptchipregshow 42
    $DBGCMD ptchipregshow $slot_no/42
  fi
}

#
# Chassis Commands
#
chassisShow() {
  if [ $multiBlade == TRUE ]; then
    echo
    echo "> chassisconfig"
    $DBGCMD chassisconfig

    echo
    echo "> "slotshow
    _cmd=`commandPath slotshow`
    $DBGCMD $_cmd
  fi

  echo
  echo "> switchshow"
  $DBGCMD switchshow

  echo
  echo "> portBufferShow"
  $DBGCMD portBufferShow

}


##############
# Main Scripts
##############

#
#
# Load library -- must be first.
#
home="/fabos/share"
util="diagcommon.sh"
portmapuitl="debugcommon.sh"
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
	echo "Error -- could not locate $util"
	exit 3
fi

ok=0
for f in "./$portmapuitl" "$FABOSHOME/share/$portmapuitl" "$home/$portmapuitl" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	echo "Error -- could not locate $portmapuitl"
	exit 3
fi

#
# get sys info for multi blade
#
multiBlade=`isMultiBlade`
if [ $multiBlade = TRUE ]; then
     syntax="`/bin/basename $0` <slot> <port | all>"
else
     syntax="`/bin/basename $0` port \| -all"
fi

echo Command: $# $0 $1 $2
if [ $multiBlade == TRUE ]; then
	if [ $# != 2 ]; then
		echo "Incorrect Arguments"
		echo $syntax
		exit
	fi
	slot_list=$1
	port_list=$2
else
	if [ $# != 1 ]; then
		echo "Incorrect Arguments"
		echo $syntax
		exit
	fi
	slot_list=0
	port_list=$1
fi



# Chassis commands
chassisShow

echo SLOT LIST $slot_list
echo PORT LIST $port_list

# Loop through slots
for slot_no in $slot_list
do
    echo
    if [ $multiBlade == TRUE ]; then
        echo ++++++++++++++++++++  SLOT $slot_no  ++++++++++++++++++++++++++
        echo
        echo "> " bladeportmap $slot_no
        bladeportmap $slot_no
    else
        echo "> " bladeportmap
        bladeportmap
    fi

    # Get list of valid ports from bladeportmap.
    if [ "$port_list" == "-all" ]; then
        blade_ports=`get_bport_list $slot_no`
    else
        blade_ports=$port_list
    fi

    # Loop through ports on slot
    for port_no in $blade_ports; do
		pinballPort=`isPinballPort $slot_no $port_no`
		cpsasPort=`isCPSASPort $slot_no $port_no`
		echo Is $slot_no $port_no CPSAS $cpsasPort
		echo Is $slot_no $port_no PINBL $pinballPort
        portShow
    done

    # Get list of valid chips from bladeportmap.
    blade_chips=`get_chip_list $slot_no`

    bladeType=`getBladeID $slot_no`
    if { [ "$bladeType" = BRADLEY ] || [ "$bladeType" = DRAGON ] ||
    [ "$bladeType" = GLACIER2 ] || [ "$bladeType" = MARATHON ] || 
    [ "$bladeType" = SPRINT ] || [ "$bladeType" = VIKING ] ||
    [ "$bladeType" = IRONMAN ] || [ "$bladeType" = SUPERHAWK ]; }
    then
        echo chipregshow is not supported on these platforms.
    else
	    if { [ "$bladeType" = SCIMITAR ] || [ "$bladeType" = SABRE ]; }
		then
	        cpsasChipShow
		else
	        chipShow
		fi
    fi
done
