#!/bin/sh
#
#
#    Copyright (c) 2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#     chipregshow - disply registers for all ports in a chip
# 
# SYNOPSIS
#      chipregshow [slot/]chip [ filter ]
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display the register contents for all ports
#      in the specified chip
# 
# OPTIONS
#      [slot/]chip
#                Specify  the  index  of  the chip within the blade
#                specified by slot to be displayed. If slot is  not
#                specified,  the  current slot will be assumed. You
#                can set the current slot by execution  of  setslot
#                command.
# 
#      filter    Specify filter string.
#
# EXAMPLES (Bloom)
#      > chipregshow 1/1 ffffffff
#      Port Registers for slot: 1, port: 8
#      0xc9b8803c: toc_ctl        0000         0xc9b88044: ...
#      0xc9b8804c: flist_stat     0018037e     0xc9b88054: ...
#      0xc9b8805c: plist_stat     ffffffff     0xc9b88064: ...
#      0xc9b8806c: aulist_stat    ffffffff     0xc9b88070: ...
#      0xc9b8aca4: port_speed     ffffffff     0xc9b8aca8: ...
#      0xc9b8acb6: epi2_stat      0000         0xc9b8acc4: ...
#      0xc9b8accc: listB_stat     ffffffff     0xc9b8acdc: ...
#      0xc9b8d4c8: frzfrm_did     00ad0701     0xc9b8d4cc: ...
# 
# 
#      Port Registers for slot: 1, port: 9
#      0xc9b9803c: toc_ctl        0000         0xc9b98044: ...
#      0xc9b9804c: flist_stat     0018037e     0xc9b98054: ...
#      0xc9b9805c: plist_stat     ffffffff     0xc9b98064: ...
#      0xc9b9806c: aulist_stat    ffffffff     0xc9b98070: ...
#      0xc9b9aca4: port_speed     ffffffff     0xc9b9aca8: ...
#      0xc9b9acb6: epi2_stat      0000         0xc9b9acc4: ...
#      0xc9b9accc: listB_stat     ffffffff     0xc9b9acdc: ...
#      0xc9b9d4c8: frzfrm_did     00ad0301     0xc9b9d4cc: ...
#      --- <output truncated> ---
# 
# EXAMPLES (Condor)
#      > chipregshow 1/1 _er_
#      
#      Slot= 1, Chip= 1
#      miniS=1 chips/Minis=1 bports/chip=32
#      
#      ============================================================================================
#      Port Registers for Slot: 1 port: 32
#      0xe6bfa704: fps_er_enc_in           00000000    0xe6bfa708: fps_er_crc              00000000
#      0xe6bfa70c: fps_er_trunc            00000000    0xe6bfa710: fps_er_toolong          00000000
#      0xe6bfa714: fps_er_bad_eof          00000000    0xe6bfa718: fps_er_enc_out          00000000
#      0xe6bfa71c: fps_er_bad_os           00000000    0xe6bfa720: fps_fl_open             00000000
#      0xe6bfa734: fps_dev_loop_tenancy    00000000    0xe6bfa738: fps_er_crc_good_eof     00000000
#      
#      ============================================================================================
#      Port Registers for Slot: 1 port: 33
#      0xe6c0a704: fps_er_enc_in           00000000    0xe6c0a708: fps_er_crc              00000000
#      0xe6c0a70c: fps_er_trunc            00000000    0xe6c0a710: fps_er_toolong          00000000
#      0xe6c0a714: fps_er_bad_eof          00000000    0xe6c0a718: fps_er_enc_out          00000000
#      0xe6c0a71c: fps_er_bad_os           00000000    0xe6c0a720: fps_fl_open             00000000
#      0xe6c0a734: fps_dev_loop_tenancy    00000000    0xe6c0a738: fps_er_crc_good_eof     00000000
#      --- <output truncated> ---
# 
# SEE ALSO
#      chippropshow(1d),   minisregshow(1d),   ptregshow(1d),  set-
#      slot(1d)
#

def_slot=0
shtyp=portmap

# 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 $util"
	exit 3
fi

#
# get sys info for multi blade
#
multiBlade=`isMultiBlade`
if [ $multiBlade = TRUE ]
then
     syntax="`/bin/basename $0` [slot/]chip [ filter ]"
else
     syntax="`/bin/basename $0` chip [ filter ]"
fi

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}

dashslot_found=0
permslot_set=0
permchip_set=0
tempchip_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

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

    #
    # filter string can be composed of number(s) so we only consider
    # the first match as chip number only if chip value hasn't been
    # set so far.
    #
    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ -z $num_matched ] ; then
	if [ $permchip_set -ne 1 ] && [ $tempchip_set -ne 1 ] ; then
	    chip=$param
	    tempchip_set=1
	    shift ; continue
	fi
    fi
done

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

case "$#" in
    0 | 1)  filter=$1 ;;
        *)  err "Invalid command" ; exit 4 ;;
esac

#
# If the optional filter value is provided, here we can grab
# that and set the filter value.
#

#
# chipregshow()
#
slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi
bladeType=`getBladeID $slot`
if { [ "$bladeType" = MARATHON ] || [ "$bladeType" = SPRINT ] || [ "$bladeType" = IRONMAN ] ||
     [ "$bladeType" = VIKING ] || [ "$bladeType" = SCIMITAR ] || [ "$bladeType" = SABRE ] ; }
then 
   echo "chipregshow: Command not applicable to this platform."
   exit 3
fi
chip=`getValue $chip`
if [ $? != 0 ] ; then exit 3 ; fi
if [ $chip -lt 0 ] || [ $chip -gt 7 ] ; then
   err "Chip: $chip invalid number"
   exit 4
fi
enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi

if [ $multiBlade = TRUE ]
then
    slot_message=" Slot: $slot"
else
    slot_message=""
fi

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

file=$path/$shtyp
file_chip=$path/$chip
if [ ! -e "$file" ] ; then
   err "Slot: $slot not found."
   exit 4
   fi

if [ ! -e "$file_chip" ] ; then
   err "Slot: $slot, Chip: $chip, chip not found."
else
   echo ""
   echo "Slot= $slot, Chip= $chip"
   get_chip_line_in_minis_chip_port_list $file $chip
   echo ""

   # First time through loop needs to display chip information
   first_past=1

   # find the file location based on the slot

   next_line=0

   # Here we read the portmap to find all the appropriate ports.  This is
   # platform independent.  We used to hard-code the ports list.
   get_CH_portmap_list $file -CH $chip | 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

      _bport=`/bin/echo $line | /usr/bin/cut -d" " -f2`

      # first past through loop, dump chip reg inform
      if [ $first_past -eq 1 ] ; then
            first_past=0
      echo "============================================================================================"
	    echo "Chip Registers for$slot_message chip: $chip"
            /fabos/bin/ptChipRegShow $slot/$_bport
            echo
      fi

      echo "============================================================================================"

      if [ ! -z $filter ]; then
	    echo "Port Registers for$slot_message port: $_bport"
            ptregshow -slot $slot $_bport | /bin/grep -i $filter
      else
            ptregshow -slot $slot $_bport 
      fi
      echo ""
   done

fi
# disable the proc entries of this slot
disableSlotProc $slot



