#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      ptbufshow - dump port buffer contents
# 
# SYNOPSIS
#      ptbufshow [slot/]port
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display the contents of port buffer regis-
#      ters.
# 
# OPTIONS
#      [slot/]port
#                Specify the index of the  port  within  the  blade
#                specified  by slot to be displayed. If slot is not
#                specifed, the current slot will  be  assumed.  You
#                can  set  the current slot by execution of setslot
#                command.
# 
# EXAMPLES (bloom)
#      >ptbufshow 1/1
# 
#      Port Buffers for slot: 1, port: 1
# 
#      putq_list_stat is empty  (0xffffffff == NO_BUFFER)
#      tolist_stat is empty  (0xffffffff == NO_BUFFER)
#      plist_stat  is empty  (0xffffffff == NO_BUFFER)
#      aulist_stat is empty  (0xffffffff == NO_BUFFER)
#      piq_stat: head=0x0 tail=0x0
#      piq_stat: number of mini-buffers = 1
#      flist_stat : head=0x14 tail=0x37e
#        0x014   0x016   0x018   0x01a   0x01c   0x01e   0x020
#        0x022   0x024   0x026   0x028   0x02a   0x02c   0x02e
#        0x030   0x032   0x034   0x036   0x038   0x03a   0x03c
#      --- <output truncated> ---
#
# EXAMPLES (Condor)
#      >ptbufshow 1/1
#      Port Buffers for Slot: 1 port: 1
#      
#      0xe59ea000: txq_bufdesc_word[000]       00004000 00400000 00004000 00400000 
#      0xe59ea010: txq_bufdesc_word[004]       00004000 00400000 00004000 00400000 
#      0xe59ea020: txq_bufdesc_word[008]       00004000 00400000 00004000 00400000 
#      --- <output truncated> ---
# 
# SEE ALSO
#      ptregshow(1d), setslot(1d)
# 

def_slot=0
shtyp=cebs
shtyp1=chip_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/]port"
else
     syntax="`/bin/basename $0` port"
fi

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_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
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

#
# ptBufshow()
#
slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi
port=`getValue $port`
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

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

file=`/usr/bin/find $path -name "$shtyp1" -print 2> /dev/null | /bin/grep asic$port/$shtyp1 2> /dev/null`
if [ ! -e "$file" ] ; then
file=`/usr/bin/find $path -name "$shtyp" -print 2> /dev/null | /bin/grep asic$port/$shtyp 2> /dev/null`
fi

if [ ! -e "$file" ] ; then
   err "Port: $port not found."
else
   echo "Port Buffers for$slot_message port: $port"
   echo ""
   /bin/cat $file | /bin/grep "^B:" | /bin/sed s/B://
fi

# disable the proc entries of this slot
disableSlotProc $slot


