#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      minisregshow - display contents of mini-switch registers.
# 
# SYNOPSIS
#      minisregshow [slot/]minis [ filter ]
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display the mini-switch register contents.
# 
# OPTIONS
#      [slot/]chip
#                Specify the index of the  mini-switch  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 the filter string.
# 
# EXAMPLES
#      > minisregshow 1/3 ffffff
# 
#      Port Registers for slot: 1, port: 48
#      0xc9ed003c: toc_ctl        0000         ......
#      0xc9ed004c: flist_stat     0014037e     ......
#      0xc9ed005c: plist_stat     ffffffff     ......
#      0xc9ed006c: aulist_stat    ffffffff     ......
#      0xc9ed2ca4: port_speed     ffffffff     ......
#      0xc9ed2cb6: epi2_stat      0000         ......
#      0xc9ed2ccc: listB_stat     ffffffff     ......
#      0xc9ed54c8: frzfrm_did     000101ee     ......
# 
# 
#      Port Registers for slot: 1, port: 49
#      0xc9ee003c: toc_ctl        0000         ......
#      0xc9ee004c: flist_stat     0014037e     ......
#      0xc9ee005c: plist_stat     ffffffff     ......
#      0xc9ee006c: aulist_stat    ffffffff     ......
#      0xc9ee2ca4: port_speed     ffffffff     ......
#      0xc9ee2cb6: epi2_stat      0000         ......
#      0xc9ee2ccc: listB_stat     ffffffff     ......
#      0xc9ee54c8: frzfrm_did     00fffffd     ......
#      --- <output truncated> ---
# 
# SEE ALSO
#      chipregshow(1d),  minispropshow(1d),   ptregshow(1d),   set-
#      slot(1d)
# 
# 
# NOTES
#      This  command  not intended for normal user operation and is
#      available to provide supplemental information during  system
#      debug.
# 

def_slot=0
shtyp=reg

# 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"
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

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

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}

dashslot_found=0
permslot_set=0
permmini_set=0
tempmini_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

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

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

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

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

#
# minisregshow()
#
slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi
enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi

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

miniS=`getValue $miniS`
if [ $? != 0 ] ; then exit 3 ; fi
if [ $miniS -lt 0 ] || [ $miniS -gt 3 ] ; then
   err "Minis: $miniS invalid number"
   exit 4
fi

path="/proc/fabos/blade/$slot"
if [ ! -e "$path" ] ; then
   err "MiniS: $miniS not found"
   exit 4
fi

let total=16
let port="$miniS * $total"
while [ $total -ne 0 ]; do
      echo ""
      if [ ! -z $filter ]; then
	 echo "Port Registers for$slot_message port: $port"
	 ptregshow -slot $slot $port | /bin/grep -i $filter
      else
         ptregshow -slot $slot $port 
      fi
      let total="$total-1"
      let port="$port+1"
done
echo ""

# disable the proc entries of this slot
disableSlotProc $slot
