#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      ptcreditshow - display port credits
# 
# SYNOPSIS
#      ptcreditshow [slot/]port | [ slotno ] -all
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display port credits.
# 
# OPTIONS
#      [slot/]port
#                Specify  the  index  of  the port within the blade
#                specified by slot to be  displayed.  If  slot  not
#                provided, the current slot will be assumed.
# 
#      [ slotno ] -all
#                Specify which slot to operate on. The entire ports
#                in the slot will be displayed. If  slotno  is  not
#                specified,  the  current slot will be assumed. You
#                can set the current slot by execution  of  setslot
#                command.
# 
# EXAMPLES (Bloom)
#      > ptcreditshow 1/1
#      slot: 1
#               tcc_vc            rqc_vc            rct_vc  ......
#      bpt01: E 4 0 5 5 5 5 1 1   0 0 0 0 0 0 0 0   1 1 1 1 ......
# 
# EXAMPLES (Condor)
#      > ptcreditshow 1/1
#      slot: 1
#
#      ------ blade port  1: E_port ------------------------------------------
#      0xe5bfc400: bbc_trc        0000 0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc424: bbc_trc        0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc480: bbc_rqc        0000 0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc4a4: bbc_rqc        0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc500: bbc_mbc        0000 0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc524: bbc_mbc        0000 0000 0000 0000 0000 0000 0000 0000 
#      0xe5bfc580: bbc_rqc_inc    00000000     0xe5bfc584: bbc_threshold  00000000
#      0xe5bfc588: bbc_fbpc       00000000     0xe5bfc58c: bbc_csc        00000000
#      0xe5bfc590: bbc_tcc_inc    00000000
#      
# SEE ALSO
#      ptregshow(1d), ptpropshow(1d), setslot(1d)
# 

def_slot=0
shtyp=cebs

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

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

#
#
# 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 \| [ slotno ] -all"
else
     syntax="`/bin/basename $0` port \| -all"
fi

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}
port=$slot

dashslot_found=0
permslot_set=0
permport_set=0
tempport_set=0

for param in $@ ; do

    #
    # if the previous param was "--slot" :
    #
    # 1) we check if the current param is a number.
    # 2) we check if slot/port parameter already set the slot number.
    # 3) we set $slot_num with current parameter if none of the above
    #    condition is met. otherwise we just shift or generate error.
    #
    if [ $dashslot_found -eq 1 ] ; then
	num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
	if [ -z $num_matched ] ; then
	    if [ $permslot_set -ne 1 ] ; then
		slot=$param
	    fi
	    dashslot_found=0
	    shift ; continue
	else
	    echo "Invalid slot number : $param"
	    exit 3
	fi
    fi

    dashslot_matched=`echo $param | /bin/sed 's/[-]\{1,\}slot//'`
    if [ -z $dashslot_matched ] ; then
	dashslot_found=1
	shift ;	continue
    fi

    slotport_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}\/[0-9]\{1,\}//'`
    if [ -z $slotport_matched ] ; then
	slot=`echo $param | /bin/sed 's/\/[0-9]\{1,\}//'`
	port=`echo $param | /bin/sed 's/[0-9]\{1,\}\///'`
	permslot_set=1
	permport_set=1
	shift ;	continue
    fi

    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ -z $num_matched ] ; then
	if [ $permport_set -ne 1 ] && [ $tempport_set -ne 1 ] ; then
	    port=$param
	    tempport_set=1
	    shift ; continue
	fi
    fi

    #
    # if "--all" found, set the port to this.
    #
    all_matched=`echo $param | /bin/sed 's/[-]\{1,\}all//'`
    if [ -z $all_matched ] ; then
	slot=$port
	port=$param
	tempport_set=1
	shift ; continue
    fi
done

#
# At this point, $port should have a valid value and no parameter
# should remain.
#
if [ -z $port ] ; then
    err "Invalid command"
    exit 4
fi

case "$#" in
    0)  ;;
    *)  err "Invalid command" ; exit 4 ;;
esac

prt_title()
{
   echo -n "         tcc_vc            rqc_vc"
   echo "            rct_vc            rcc_vc"
}   

asic_type()
{
file=$1/portmap
if [ -z $file ] ; then
   echo "JUNK"
else
   next_line=0

   # Here we read the portmap to find all the appropriate ports.  This is
   # platform independent.
   get_BPt_portmap_list $file -bl 0 -BPt $2 | while read line ; do
      if [ "$line" = "" ] ; then
         continue
      fi

      STR_CTL=`/bin/echo $line | /usr/bin/cut -d" " -f1`
      if [ "$STR_CTL" = "CTL" ] ; then
         next_line=1
         continue
      fi

      if [ $next_line -eq 1 ] ; then
         next_line=2
         continue
      fi

      if [ $next_line -ne 2 ] ; then
         continue
      fi

      ASIC=`/bin/echo $line | /usr/bin/cut -d" " -f12`
      echo "$ASIC"
   done
fi
}

#
# ptCreditShow()
#
slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi
# extra checking due to "--all" support
if [ ! -e /proc/fabos/blade/$slot ] ; then
   err "Not found"
   exit 3
fi
enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi
_s=`/usr/bin/expr "$port" : "[-]*all"`
if [ "0" -ne "$_s" ]
then
    let port=0
    # use the maximum value for future product
    let total=128
else
    port=`getValue $port`
    if [ $? != 0 ] ; then exit 3 ; fi
    let total=1
fi

path="/proc/fabos/blade/$slot"

ASIC=`asic_type $path $port`

file=`/usr/bin/find $path -name "$shtyp" -print 2> /dev/null | /bin/grep asic$port/$shtyp 2> /dev/null`

if [ ! -e "$file" ] ; then
   err "Port: $port not found."
else
   if [ $multiBlade = TRUE ]; then
        echo "slot: $slot"
   fi

   newPage=TRUE
   let port=0
   let page=1

   while [ $port -lt $total ]; do
   # for -all option, exit if no more blade ports for showing
      if [ ! $file ] && [ $total -ne 1 ]; then
	   disableSlotProc $slot
	   exit 0;
      fi

      if [ $newPage = TRUE ]; then
         if [ "$ASIC" = "CONDOR" ] ; then
              newPage=FALSE
         else
              prt_title
              newPage=FALSE
         fi
      fi

      # skip the first one
      if [ $port -ne 0 ]  && [ $port -lt $total ] && [ $page -eq 0 ]; then
         echo -n "Type <CR> to continue, Q<CR> to stop: "
         read choice
         case "$choice" in	
            q | Q)
            disableSlotProc $slot
            exit 0;
            ;; 
         esac
         echo ""
         newPage=TRUE
      fi

      /bin/cat $file | /bin/grep "^C:" | /bin/sed s/C://

      let port="$port+1"
      let page="$port % 16"

      file=`/usr/bin/find $path -name "$shtyp" -print 2> /dev/null | /bin/grep asic$port/$shtyp 2> /dev/null`

   done
fi
echo ""

# disable the proc entries of this slot
disableSlotProc $slot
