#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      ptphantomshow - display the quick-loop CAM tables
# 
# SYNOPSIS
#      ptphantomshow [slot/]port
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This   command   will  display  the  quick-loop  CAM(Content
#      Addressable Memory) tables.
# 
# OPTIONS
#      [slot/]port
#                Specify the index of the  port  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.
# 
# EXAMPLES
#      > ptphantomshow 2/1
#      Port Routing table for slot: 2, port: 1
# 
#      The following alpas are private on some switch ports:
#      (alpa, UI port bitmap on the blade)
# 
#      plt_cam table and plt_alpa table:
#      index      sid                plt_alpas      ......
# 
#      plt_did table for this port:
#      ali  alpa  sid          ali  alpa  sid       ......
# 
# SEE ALSO
#      setslot(1d)
# 
# NOTES
#      This command not intended for normal user operation  and  is
#      available  to provide supplemental information during system
#      debug.
# 

def_slot=0
shtyp=phantom

# 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


#
# ptPhantomShow()
#

slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi

# First check if slot ID supports phantom; if not, exit silently.
# From condor chip, this command is not supported
bladeID=`getBladeID $slot`
case $bladeID in
"PORT" )       _phantom="1" ;; # dagger
"CP128" )      _phantom="1" ;;
"STILETTO" )   _phantom="1" ;;
"TERMINATOR" ) _phantom="1" ;;
"DAZZLER" )    _phantom="1" ;;
"DAZZLERJR" )  _phantom="1" ;;
"BLAZER" )     _phantom="1" ;;
"HAMPTON" )    _phantom="1" ;;
"FORTE" )      _phantom="1" ;;
* ) _phantom="0" ;;
esac

if [ "$_phantom" -ne "1" ] ; then
    multiBlade=`isMultiBlade`
    if [ $multiBlade = TRUE ] ; then
        echo "Not supported on this blade"
    else
        echo "Not supported on this platform"
	fi
    exit 0
fi

port=`getValue $port`
if [ $? != 0 ] ; then exit 3 ; fi
enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi

path="/proc/fabos/blade/$slot"
file=`/usr/bin/find $path -name "$shtyp" -print 2> /dev/null | /bin/grep asic$port/$shtyp 2> /dev/null`
if [ -z $file ] ; then
   err "Port: $port not found"
else
   echo "Port Routing table for port: $port"
   echo ""
   /bin/cat $file
fi

# disable the proc entries of this slot
disableSlotProc $slot
