#!/bin/sh
#
#    Copyright (c) 2011 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#		Hawk/Wolverine Multi-cast view throughput show
#
set +x
PROGNAME=`basename $0`
UTIL_PROG=util_cmd

HAWK_NAME=hawk

# IME/IME Mcast counters
HAWK_MC_STAT_CNTRS=64
HAWK_MAX_MCTYPE=4

# Number of ICB/ECB MGID stats counters
MGID_CNTRS=2048

# priority
HAWK_NUM_PRIO=8

#
# Wolverine internal port mapping
# 0-11		: EMC GE ports
# 12-19		: HMC GE ports
WLV_NAME=wlv
WLV_NUM_PORTS=20
WLV_LAST_PORT=$(( $WLV_NUM_PORTS - 1 ))

DEFAULT_POLL_INT=2
MAX_POLL_INT=3600
DEFAULT_POLL_CNT=5
DEFAULT_START_IDX=0
DEFAULT_NUM_CTRS=$MGID_CNTRS

prog_banner()
{
	echo
	echo "###############################################################"
	echo "# Script to monitor Hawk/Wolverine Multi-cast view throughput #"
	echo "###############################################################"
	echo
}

usage()
{
#	prog_banner
	echo "Usage: $PROGNAME <slot/mini/chip> <-ipvdbc>"
	echo "  -i polling interval in sec [default $DEFAULT_POLL_INT]"
	echo "  -p number of polls to do [default $DEFAULT_POLL_CNT]"
	echo "  -b start stats slot [default $DEFAULT_START_IDX]"
	echo "  -c number of stats slots [default $DEFAULT_NUM_CTRS]"
	echo "  -v Verbose"
	echo "  -d Debug"
	exit 1
}

#
# Poll Hawk multi-cast stats
# Use "dbg peek" to collect the counters.
# Expected "dbg peek" output as below:
# sw0:L4/0: >db 10/2/0 dbg peek 0x0022e000 -max 5
# ASIC offset  Data        Register
# ---------------------------------------------------
# 0x0022e000:  0x00000000  ime_mc_stat_cntr[0]
# 0x0022e004:  0x00000000  ime_mc_stat_cntr[1]
# 0x0022e008:  0x00000000  ime_mc_stat_cntr[2]
# 0x0022e00c:  0x00000000  ime_mc_stat_cntr[3]
# 0x0022e010:  0x00000000  ime_mc_stat_cntr[4]
# Output limit reached
#
# Inputs:
# $1	: buffer index (0 or 1)
hawk_rd_stats()
{
	####################################
	# IME and EME MC stats counters:
	# There are 64 each
	####################################
	idx=$(( $HAWK_MC_STAT_CNTRS * $1 ))
	max=$(( $idx + $HAWK_MC_STAT_CNTRS ))

	# IME MC stats (14.6.3)
	i=$idx
	for x in `$db_rd_multi_addr $ime_mc_addr -max $HAWK_MC_STAT_CNTRS | grep 0x | cut -d ' ' -f3` ; do
		ime_mc[$i]=$x
		if [ -z "${ime_mc[$i]}" ] || [ ${#ime_mc[$i]} -gt 10 ]; then
			# echo "x=$x ime_mc default to 0x0"
			ime_mc[$i]=0x0
		fi
		(( i += 1 ))
		if [ $i -ge $max ]; then
			break;
		fi
	done

	# EME MC stats (13.6.4)
	i=$idx
	for x in `$db_rd_multi_addr $eme_mc_addr -max $HAWK_MC_STAT_CNTRS | grep 0x | cut -d ' ' -f3` ; do
		eme_mc[$i]=$x
		if [ -z "${eme_mc[$i]}" ] || [ ${#eme_mc[$i]} -gt 10 ]; then
			# echo "x=$x eme_mc default to 0x0"
			eme_mc[$i]=0x0
		fi
		(( i += 1 ))
		if [ $i -ge $max ]; then
			break;
		fi
	done

	# ICB per priority stats counters:
	# There are 8 each [8][2]
	# Lower bits 0-32 are at RAM [x][0]
	# Read x2 number of counters as each counter use two elements.
	cnt=$(( $HAWK_NUM_PRIO * 2))
	idx=$(( $HAWK_NUM_PRIO * $1 ))
	max=$(( $idx + $HAWK_NUM_PRIO ))

	i=$idx
	lsb=1
	# ICB RX packets per priority (4.5.2.76)
	for x in `$db_rd_multi_addr $icb_rx_p_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_p[$i]=$x
			if [ -z "${icb_rx_p[$i]}" ] || [ ${#icb_rx_p[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_p default to 0x0"
				icb_rx_p[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_p_hi[$i]=$x
			if [ -z "${icb_rx_p_hi[$i]}" ] || [ ${#icb_rx_p_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_p_hi default to 0x0"
				icb_rx_p_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX bytes per priority (4.5.2.80)
	for x in `$db_rd_multi_addr $icb_rx_b_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_b[$i]=$x
			if [ -z "${icb_rx_b[$i]}" ] || [ ${#icb_rx_b[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_b default to 0x0"
				icb_rx_b[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_b_hi[$i]=$x
			if [ -z "${icb_rx_b_hi[$i]}" ] || [ ${#icb_rx_b_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_b_hi default to 0x0"
				icb_rx_b_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX drop packets per priority (4.5.2.78)
	for x in `$db_rd_multi_addr $icb_rx_dp_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_dp[$i]=$x
			if [ -z "${icb_rx_dp[$i]}" ] || [ ${#icb_rx_dp[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dp default to 0x0"
				icb_rx_dp[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_dp_hi[$i]=$x
			if [ -z "${icb_rx_dp_hi[$i]}" ] || [ ${#icb_rx_dp_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dp_hi default to 0x0"
				icb_rx_dp_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX drop bytes per priority (4.5.2.82)
	for x in `$db_rd_multi_addr $icb_rx_db_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_db[$i]=$x
			if [ -z "${icb_rx_db[$i]}" ] || [ ${#icb_rx_db[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_db default to 0x0"
				icb_rx_db[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_db_hi[$i]=$x
			if [ -z "${icb_rx_db_hi[$i]}" ] || [ ${#icb_rx_db_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_db_hi default to 0x0"
				icb_rx_db_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	# ICB and ECB MC stats counters
	# There are 2048 each, but optionally user can specify number of slots and
	# starting slot.
	# Lower bits 0-32 are at RAM [x][0]
	# Read x2 number of counters as each counter use two elements.
	cnt=$(( $num_ctrs * 2))
	idx=$(( $num_ctrs * $1 ))
	max=$(( $idx + $num_ctrs ))
	# echo "Buf idx=$1, ctrs=$cnt, idx=$idx, max=$max"

	i=$idx
	lsb=1
	# ICB RX packets per MGID (4.5.2.84)
	for x in `$db_rd_multi_addr $start_icb_rx_mgp_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_mgp[$i]=$x
			if [ -z "${icb_rx_mgp[$i]}" ] || [ ${#icb_rx_mgp[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_mgp default to 0x0"
				icb_rx_mgp[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_mgp_hi[$i]=$x
			if [ -z "${icb_rx_mgp_hi[$i]}" ] || [ ${#icb_rx_mgp_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_mgp_hi default to 0x0"
				icb_rx_mgp_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX drop packets per MGID (4.5.2.86)
	for x in `$db_rd_multi_addr $start_icb_rx_dmgp_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_dmgp[$i]=$x
			if [ -z "${icb_rx_dmgp[$i]}" ] || [ ${#icb_rx_dmgp[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dmgp default to 0x0"
				icb_rx_dmgp[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_dmgp_hi[$i]=$x
			if [ -z "${icb_rx_dmgp_hi[$i]}" ] || [ ${#icb_rx_dmgp_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dmgp_hi default to 0x0"
				icb_rx_dmgp_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX bytes per MGID (4.5.2.88)
	for x in `$db_rd_multi_addr $start_icb_rx_mgb_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_mgb[$i]=$x
			if [ -z "${icb_rx_mgb[$i]}" ] || [ ${#icb_rx_mgb[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_mgb default to 0x0"
				icb_rx_mgb[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_mgb_hi[$i]=$x
			if [ -z "${icb_rx_mgb_hi[$i]}" ] || [ ${#icb_rx_mgb_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_mgb_hi default to 0x0"
				icb_rx_mgb_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ICB RX drop bytes per MGID (4.5.2.90)
	for x in `$db_rd_multi_addr $start_icb_rx_dmgb_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			icb_rx_dmgb[$i]=$x
			if [ -z "${icb_rx_dmgb[$i]}" ] || [ ${#icb_rx_dmgb[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dmgb default to 0x0"
				icb_rx_dmgb[$i]=0x0
			fi
			lsb=0
		else
			icb_rx_dmgb_hi[$i]=$x
			if [ -z "${icb_rx_dmgb_hi[$i]}" ] || [ ${#icb_rx_dmgb_hi[$i]} -gt 10 ]; then
				# echo "x=$x icb_rx_dmgb_hi default to 0x0"
				icb_rx_dmgb_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ECB TX packets per MGID (12.4.2.52)
	for x in `$db_rd_multi_addr $start_ecb_tx_mgp_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			ecb_tx_mgp[$i]=$x
			if [ -z "${ecb_tx_mgp[$i]}" ] || [ ${#ecb_tx_mgp[$i]} -gt 10 ]; then
				# echo "x=$x ecb_tx_mgp default to 0x0"
				ecb_tx_mgp[$i]=0x0
			fi
			lsb=0
		else
			ecb_tx_mgp_hi[$i]=$x
			if [ -z "${ecb_tx_mgp_hi[$i]}" ] || [ ${#ecb_tx_mgp_hi[$i]} -gt 10 ]; then
				# echo "x=$x ecb_tx_mgp_hi default to 0x0"
				ecb_tx_mgp_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done

	i=$idx
	lsb=1
	# ECB TX bytes per MGID (12.4.2.54)
	for x in `$db_rd_multi_addr $start_ecb_tx_mgb_addr -max $cnt | grep 0x | cut -d ' ' -f3` ; do
		if [ $lsb -eq 1 ]; then
			ecb_tx_mgb[$i]=$x
			if [ -z "${ecb_tx_mgb[$i]}" ] || [ ${#ecb_tx_mgb[$i]} -gt 10 ]; then
				# echo "x=$x ecb_tx_mgb default to 0x0"
				ecb_tx_mgb[$i]=0x0
			fi
			lsb=0
		else
			ecb_tx_mgb_hi[$i]=$x
			if [ -z "${ecb_tx_mgb_hi[$i]}" ] || [ ${#ecb_tx_mgb_hi[$i]} -gt 10 ]; then
				# echo "x=$x ecb_tx_mgb_hi default to 0x0"
				ecb_tx_mgb_hi[$i]=0x0
			fi
			(( i += 1 ))
			if [ $i -ge $max ]; then
				break;
			fi
			lsb=1
		fi
	done
}

hawk_pri_banner()
{
	echo
	echo "ICB per priority stats"
	printf "Pri  %-18s  %-18s  %-22s  %s\n" \
	"RX bits" "RX packets" "RX bytes drop" "RX packets drop"
	printf "===  %-18s  %-18s  %-22s  %s\n" \
	"==================" "==================" "======================" "======================"
}

hawk_mc_banner_()
{
	echo
	echo "IME/EME MC stats copies=all copies of MC frames were made, error=at least one copy was not made"
	printf "Pri  mctype  %-18s  %-18s  %-18s  %-18s\n" \
	"IME copies" "IME error" "EME copies" "EME error"
	printf "===  ======  %-18s  %-18s  %-18s  %-18s\n" \
	"==================" "==================" "==================" "=================="
}

hawk_mc_banner()
{
	echo
	echo "IME/EME MC stats copies=all copies of MC frames were made, error=at least one copy was not made"
	printf "Pri  mctype  IDX  %-18s  %-18s  IDX  %-18s  %-18s\n" \
	"IME copies" "EME copies" "IME error" "EME error"
	printf "===  ======  ===  %-18s  %-18s  ===  %-18s  %-18s\n" \
	"==================" "==================" "==================" "=================="
}

hawk_mgid_banner()
{
	echo
	echo "ICB/ECB MGID stats counters, poll $num_ctrs stats slots ($start_idx-$end_idx)"
	printf "IDX  %-14s  %-14s  %-14s  %-14s  %-20s  %s\n" \
	"RX bits" "RX packets" "TX bits" "TX packets" "RX bytes drop" "RX packets drop"
	printf "===  %-14s  %-14s  %-14s  %-14s  %-20s  %s\n" \
	"==============" "==============" "==============" "==============" "====================" "===================="
}

hawk_icb_mgid_banner()
{
	echo
	echo "ICB MGID stats counters"
	printf "IDX  %-18s  %-18s  %-22s  %s\n" \
	"RX bits" "RX packets" "RX bytes drop" "RX packets drop"
	printf "===  %-18s  %-18s  %-22s  %s\n" \
	"==================" "==================" "======================" "======================"
}

hawk_ecb_mgid_banner()
{
	echo
	echo "ECB MGID stats counters"
	printf "IDX  %-18s  %-18s\n" "TX bits" "TX packets"
	printf "===  %-18s  %-18s\n" "==================" "=================="
}

#
# Hawk display multicast stats
#
hawk_disp_stats()
{
	if [ -n "$DETAIL_FLAGS" ]; then
		str=""
	else
		str="(only non-zero shown) "
	fi
	echo
	echo "****  ASIC: $CHP_NAME, poll# $poll_num/$poll_cnt, interval: $poll_int ($poll_int_x) $str ****"

	####################################
	# IME and EME MC stats counters:
	# There are 2 sets of {per qpri, mctyp} 32 counters.
	# Lower 32 count frames for which all copies of Mulicast frames were made.
	# Upper 32 count multicast frames for which at least one copy was not made
	# due to lack of MIDs
	####################################
	old=$(( $HAWK_MC_STAT_CNTRS * $old_idx ))
	new=$(( $HAWK_MC_STAT_CNTRS * $new_idx ))

	o_cp_i=$old
	n_cp_i=$new
	o_er_i=$(( $old + $HAWK_MAX_MCTYPE ))
	n_er_i=$(( $new + $HAWK_MAX_MCTYPE ))
	cnt=0
	idx=0
	for ((pri = 0; pri < $HAWK_NUM_PRIO; pri++)); do
		idx=$(( $pri * $HAWK_NUM_PRIO ))
		o_cp_i=$(( $old + $idx ))
		n_cp_i=$(( $new + $idx ))
		o_er_i=$(( $o_cp_i + $HAWK_MAX_MCTYPE ))
		n_er_i=$(( $n_cp_i + $HAWK_MAX_MCTYPE ))
		for ((mctype = 0; mctype < $HAWK_MAX_MCTYPE; mctype++)); do
			trafic=0
			# IME copies packets
			bw=`$UTIL_PROG bw32 ${ime_mc[$o_cp_i]} ${ime_mc[$n_cp_i]} 1 $poll_int_x`
			if [ "$bw" != "0" ]; then
				trafic=1
			fi
			bw_ime_cp="${bw}Pps"

			# IME error packets
			bw=`$UTIL_PROG bw32 ${ime_mc[$o_er_i]} ${ime_mc[$n_er_i]} 1 $poll_int_x`
			if [ "$bw" != "0" ]; then
				trafic=1
			fi
			bw_ime_er="${bw}Pps"

			# EME copies packets
			bw=`$UTIL_PROG bw32 ${eme_mc[$o_cp_i]} ${eme_mc[$n_cp_i]} 1 $poll_int_x`
			if [ "$bw" != "0" ]; then
				trafic=1
			fi
			bw_eme_cp="${bw}Pps"

			# EME error packets
			bw=`$UTIL_PROG bw32 ${eme_mc[$o_er_i]} ${eme_mc[$n_er_i]} 1 $poll_int_x`
			if [ "$bw" != "0" ]; then
				trafic=1
			fi
			bw_eme_er="${bw}Pps"

			if [ $trafic -ne 0 ] || [ -n "$DETAIL_FLAGS" ]; then
				if [ $cnt -eq 0 ]; then
					hawk_mc_banner
				fi
				e_idx=$(( $idx + $HAWK_MAX_MCTYPE ))
				printf "%-3d  %-6d  %-3d  %-18s  %-18s  %-3d  %-18s  %s\n" \
				$pri $mctype $idx ${bw_ime_cp} ${bw_eme_cp} $e_idx ${bw_ime_er} ${bw_eme_er}
				(( cnt += 1 ))
			fi
			(( n_cp_i += 1 ))
			(( o_cp_i += 1 ))
			(( n_er_i += 1 ))
			(( o_er_i += 1 ))
			(( idx += 1 ))
		done
	done
	if [ $cnt -eq 0 ]; then
		echo "****  No IO detected on all IME and EME MC counters  ****"
	fi

	##################################################
	# ICB per priority stats counters:
	# RAM size [8]
	##################################################
	old=$(( $HAWK_NUM_PRIO * $old_idx ))
	new=$(( $HAWK_NUM_PRIO * $new_idx ))

	o_idx=$old
	n_idx=$new
	cnt=0

	for ((pri = 0; pri < $HAWK_NUM_PRIO; pri++)); do
		trafic=0
		# ICB RX packets per priority
		bw=`$UTIL_PROG bw64 ${icb_rx_p_hi[$o_idx]} ${icb_rx_p[$o_idx]} \
			${icb_rx_p_hi[$n_idx]} ${icb_rx_p[$n_idx]} 1 $poll_int_x 0xff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_rx_p="${bw}Pps"

		# ICB RX bytes per priority
		bw=`$UTIL_PROG bw64 ${icb_rx_b_hi[$o_idx]} ${icb_rx_b[$o_idx]} \
			${icb_rx_b_hi[$n_idx]} ${icb_rx_b[$n_idx]} 8 $poll_int_x 0xffff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_rx_b="${bw}bps"

		# ICB RX drop packets per priority
		bw=`$UTIL_PROG bw64 ${icb_rx_dp_hi[$o_idx]} ${icb_rx_dp[$o_idx]} \
			${icb_rx_dp_hi[$n_idx]} ${icb_rx_dp[$n_idx]} 1 $poll_int_x 0xff`
		delta=`$UTIL_PROG delta64 ${icb_rx_dp_hi[$o_idx]} ${icb_rx_dp[$o_idx]} \
			${icb_rx_dp_hi[$n_idx]} ${icb_rx_dp[$n_idx]} 0xff $poll_int_x`
		bw_rx_pd="${delta}(${bw}Pps)"

		# ICB RX drop bytes per priority
		bw=`$UTIL_PROG bw64 ${icb_rx_db_hi[$o_idx]} ${icb_rx_db[$o_idx]} \
			${icb_rx_db_hi[$n_idx]} ${icb_rx_db[$n_idx]} 8 $poll_int_x 0xffff`
		delta=`$UTIL_PROG delta64 ${icb_rx_db_hi[$o_idx]} ${icb_rx_db[$o_idx]} \
			${icb_rx_db_hi[$n_idx]} ${icb_rx_db[$n_idx]} 0xffff $poll_int_x`
		bw_rx_bd="${delta}(${bw}bps)"

		if [ $trafic -ne 0 ] || [ -n "$DETAIL_FLAGS" ]; then
			if [ $cnt -eq 0 ]; then
				hawk_pri_banner
			fi
			printf "%-3d  %-18s  %-18s  %-22s  %s\n" \
			$pri ${bw_rx_b} ${bw_rx_p} ${bw_rx_bd} ${bw_rx_pd}
			(( cnt += 1 ))
		fi
		(( o_idx += 1 ))
		(( n_idx += 1 ))
	done
	if [ $cnt -eq 0 ]; then
		echo "****  No IO detected on all ICB priority stats  ****"
	fi

	##################################################
	# ICB/ECB MC stats counters
	# RAM size [2048][2]
	##################################################
	old=$(( $num_ctrs * $old_idx ))
	new=$(( $num_ctrs * $new_idx ))

	o_idx=$old
	n_idx=$new
	cnt=0
	z_cnt=0
	for ((i = 0; i < $num_ctrs; i++)); do
		trafic=0
		# ICB RX packets per MGID
		bw=`$UTIL_PROG bw64 ${icb_rx_mgp_hi[$o_idx]} ${icb_rx_mgp[$o_idx]} \
			${icb_rx_mgp_hi[$n_idx]} ${icb_rx_mgp[$n_idx]} 1 $poll_int_x 0xff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_rx_p="${bw}Pps"

		# ICB RX bytes per MGID
		bw=`$UTIL_PROG bw64 ${icb_rx_mgb_hi[$o_idx]} ${icb_rx_mgb[$o_idx]} \
			${icb_rx_mgb_hi[$n_idx]} ${icb_rx_mgb[$n_idx]} 8 $poll_int_x 0xffff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_rx_b="${bw}bps"

		# ICB RX drop packets per MGID
		bw=`$UTIL_PROG bw64 ${icb_rx_dmgp_hi[$o_idx]} ${icb_rx_dmgp[$o_idx]} \
			${icb_rx_dmgp_hi[$n_idx]} ${icb_rx_dmgp[$n_idx]} 1 $poll_int_x 0xff`
		delta=`$UTIL_PROG delta64 ${icb_rx_dmgp_hi[$o_idx]} ${icb_rx_dmgp[$o_idx]} \
			${icb_rx_dmgp_hi[$n_idx]} ${icb_rx_dmgp[$n_idx]} 0xff $poll_int_x`
		bw_rx_pd="${delta}(${bw}Pps)"

		# ICB RX drop bytes per MGID
		bw=`$UTIL_PROG bw64 ${icb_rx_dmgb_hi[$o_idx]} ${icb_rx_dmgb[$o_idx]} \
			${icb_rx_dmgb_hi[$n_idx]} ${icb_rx_dmgb[$n_idx]} 8 $poll_int_x 0xffff`
		delta=`$UTIL_PROG delta64 ${icb_rx_dmgb_hi[$o_idx]} ${icb_rx_dmgb[$o_idx]} \
			${icb_rx_dmgb_hi[$n_idx]} ${icb_rx_dmgb[$n_idx]} 0xffff $poll_int_x`
		bw_rx_bd="${delta}(${bw}bps)"

		# ECB TX packets per MGID
		bw=`$UTIL_PROG bw64 ${ecb_tx_mgp_hi[$o_idx]} ${ecb_tx_mgp[$o_idx]} \
			${ecb_tx_mgp_hi[$n_idx]} ${ecb_tx_mgp[$n_idx]} 1 $poll_int_x 0xff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_tx_p="${bw}Pps"

		# ECB TX bytes per MGID
		bw=`$UTIL_PROG bw64 ${ecb_tx_mgb_hi[$o_idx]} ${ecb_tx_mgb[$o_idx]} \
			${ecb_tx_mgb_hi[$n_idx]} ${ecb_tx_mgb[$n_idx]} 8 $poll_int_x 0xffff`
		if [ "$bw" != "0" ]; then
			trafic=1
		fi
		bw_tx_b="${bw}bps"

		if [ $trafic -ne 0 ] || [ -n "$DETAIL_FLAGS" ]; then
			do_print=1
			if [ $trafic -eq 0 ]; then
				(( z_cnt += 1 ))
				if [ $z_cnt -gt $max_zero_prints ]; then
					do_print=0
				fi
			fi
			if [ $do_print -eq 1 ]; then
				if [ $cnt -eq 0 ]; then
					hawk_mgid_banner
				fi
				idx=$(( $i + $start_idx ))
				printf "%-3d  %-14s  %-14s  %-14s  %-14s  %-20s  %s\n" \
				$idx ${bw_rx_b} ${bw_rx_p} ${bw_tx_b} ${bw_tx_p} ${bw_rx_bd} ${bw_rx_pd}
				(( cnt += 1 ))
			fi
		fi
		(( o_idx += 1 ))
		(( n_idx += 1 ))
	done
	if [ $cnt -eq 0 ]; then
		echo "****  No IO detected on all ICB/ECB MC counters  ****"
	fi
}

#
# Delay seconds
#
# input:
# $1	: seconds to delay
#
delay_1()
{
	SECONDS=0
	# echo "sleep_1 $1"
	# date +%s
	while [ $SECONDS -lt $1 ]; do
		sleep 1
	done
	# date +%s
}

#
# Delay seconds using sleep
#
# input:
# $1	: seconds to delay
#
delay_2()
{
	# echo "sleep_2 $1"
	# date +%s
	sleep $1
	# date +%s
}

#
# adjust the poll interval to compensate for the time it took to poll the stats.
#
# input:
# $time_1	: Poll start time
# $time_2	: Poll done time
#
# output:
# $sleep_time	: adjusted seconds to sleep
# $poll_int_x	: adjusted poll interval.
#
adjust_interval()
{
	diff_secs=$(( $time_2 - $time_1 ))
	sleep_time=$poll_int
	poll_int_x=$(( $diff_secs + $poll_int ))

	if [ -n "$VERBOSE" ]; then
		echo "#### Took $diff_secs secs to poll (interval $poll_int -> $poll_int_x)"
	fi
#	echo "#### Took $diff_secs secs to poll (interval $poll_int -> $poll_int_x)"
}

#
# start stats monitoring
#
start_mon()
{
	poll_num=0
	poll_int_x=$poll_int
	echo
	echo "#### ASIC: $CHP_NAME, poll# $poll_num/$poll_cnt, interval: $poll_int ####"
	SECONDS=0
	# time_1=`date +%s 2>/dev/null`
	time_1=$SECONDS
	$poll_fn $old_idx
	# time_2=`date +%s 2>/dev/null`
	time_2=$SECONDS
	adjust_interval

	while [ $poll_num -lt $poll_cnt ]; do
		$DELAY $sleep_time
		(( poll_num += 1 ))
		SECONDS=0
		# time_1=`date +%s 2>/dev/null`
		time_1=$SECONDS
		$poll_fn $new_idx
		$disp_fn

		# time_2=`date +%s 2>/dev/null`
		time_2=$SECONDS
		adjust_interval

		# Switch new/old buffers
		if [ $new_idx -eq 0 ]; then
			old_idx=0
			new_idx=1
		else
			old_idx=1
			new_idx=0
		fi
	done
}

#
# Get Hawk multi-cast stats counters address.
# Do "dbg rd name" to get stats counters offsets
# Expected rdname output as below:
# sw0:FID128:> db 0/1/0 dbg rdname crm_igr_pkt_cnt[0]
# ASIC offset  Data        Register
# ---------------------------------------------------
# 0x00042e94:  0x00000000  crm_igr_pkt_cnt[0]
#
hawk_get_stats_addr()
{
	# ICB RX packets per priority (4.5.2.76)
	# RAM size [8][2]
	reg_name="icb_rmp_ram[0][0]"
	icb_rx_p_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "ICB RX packets base address: $icb_rx_p_addr"

	# ICB RX bytes per priority (4.5.2.80)
	# RAM size [8][2]
	reg_name="icb_rmb_ram[0][0]"
	icb_rx_b_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "ICB RX bytes base address: $icb_rx_b_addr"

	# ICB RX drop packets per priority (4.5.2.78)
	# RAM size [8][2]
	reg_name="icb_drmp_ram[0][0]"
	icb_rx_dp_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "ICB RX drop packets base address: $icb_rx_dp_addr"

	# ICB RX drop bytes per priority (4.5.2.82)
	# RAM size [8][2]
	reg_name="icb_drmb_ram[0][0]"
	icb_rx_db_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "ICB RX drop bytes base address: $icb_rx_db_addr"

	# Start stats slot address offset
	start_offset=$(( $start_idx * 8 ))

	# ICB RX packets per MGID (4.5.2.84)
	# RAM size [2048][2]
	reg_name="icb_rmgp_ram[0][0]"
	icb_rx_mgp_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_icb_rx_mgp_addr=`hexfcn $icb_rx_mgp_addr "+" $start_offset`
	echo "ICB RX MGID packets base address: $icb_rx_mgp_addr [$start_idx]=$start_icb_rx_mgp_addr"

	# ICB RX drop packets per MGID (4.5.2.86)
	# RAM size [2048][2]
	reg_name="icb_drmgp_ram[0][0]"
	icb_rx_dmgp_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_icb_rx_dmgp_addr=`hexfcn $icb_rx_dmgp_addr "+" $start_offset`
	echo "ICB RX MGID drop packets base address: $icb_rx_dmgp_addr [$start_idx]=$start_icb_rx_dmgp_addr"

	# ICB RX bytes per MGID (4.5.2.88)
	# RAM size [2048][2]
	reg_name="icb_rmgb_ram[0][0]"
	icb_rx_mgb_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_icb_rx_mgb_addr=`hexfcn $icb_rx_mgb_addr "+" $start_offset`
	echo "ICB RX MGID bytes base address: $icb_rx_mgb_addr [$start_idx]=$start_icb_rx_mgb_addr"

	# ICB RX drop bytes per MGID (4.5.2.90)
	# RAM size [2048][2]
	reg_name="icb_drmgb_ram[0][0]"
	icb_rx_dmgb_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_icb_rx_dmgb_addr=`hexfcn $icb_rx_dmgb_addr "+" $start_offset`
	echo "ICB RX MGID drop bytes base address: $icb_rx_dmgb_addr [$start_idx]=$start_icb_rx_dmgb_addr"

	# ECB TX packets per MGID (12.4.2.52)
	# RAM size [2048][2]
	reg_name="ecb_dmp_ram[0][0]"
	ecb_tx_mgp_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_ecb_tx_mgp_addr=`hexfcn $ecb_tx_mgp_addr "+" $start_offset`
	echo "ECB TX MGID packets base address: $ecb_tx_mgp_addr [$start_idx]=$start_ecb_tx_mgp_addr"

	# ECB TX bytes per MGID (12.4.2.54)
	# RAM size [2048][2]
	reg_name="ecb_dmb_ram[0][0]"
	ecb_tx_mgb_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	start_ecb_tx_mgb_addr=`hexfcn $ecb_tx_mgb_addr "+" $start_offset`
	echo "ECB TX MGID bytes base address: $ecb_tx_mgb_addr [$start_idx]=$start_ecb_tx_mgb_addr"

	##########################################################################
	# IME and EME MC stats counters:
	# There are 2 sets of {per qpri, mctyp} 32 counters.
	# Lower 32 count frames for which all copies of Mulicast frames were made.
	# Upper 32 count multicast frames for which at least one copy was not made
	# due to lack of MIDs
	##########################################################################

	# IME MC stats (14.6.3)
	# RAM size [64]
	reg_name="ime_mc_stat_cntr[0]"
	ime_mc_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "IME MC base address: $ime_mc_addr"

	# EME MC stats (13.6.4)
	# RAM size [64]
	reg_name="eme_mc_stat_cntr[0]"
	eme_mc_addr=`$db_rd_name $reg_name | grep -Fw $reg_name | cut -b1-10`
	echo "EME MC base address: $eme_mc_addr"
}

#
# Validate user-input start stats slot.
# Use default if not valid number.
#
# input:
# $start_idx	: Start stats slot to poll.
#
validate_start_slot()
{
	if [[ "$start_idx" == *[^0-9]* ]]; then
		echo "$start_idx is not valid for stats slot (Default value $DEFAULT_START_IDX is used)"
		start_idx=$DEFAULT_START_IDX
	elif [ $start_idx -lt 0 ]; then
		echo "Start stats slot $start_idx should be >= 0 (Default value $DEFAULT_START_IDX is used)"
		start_idx=$DEFAULT_START_IDX
	elif [ $start_idx -ge $MGID_CNTRS ]; then
		echo "Start stats slot $start_idx should be < $MGID_CNTRS (Default value $DEFAULT_START_IDX is used)"
		start_idx=$DEFAULT_START_IDX
	fi
}

#
# Validate user-input number of stats slots.
# Use default if not valid number.
#
# input:
# $num_ctrs	: Number of counters to poll
#
validate_num_ctrs()
{
	max_ctrs=$(( $MGID_CNTRS - $start_idx ))
	if [[ "$num_ctrs" == *[^0-9]* ]]; then
		echo "$num_ctrs is not valid for number of counters (Default value $max_ctrs is used)"
		num_ctrs=$max_ctrs
	elif [ $num_ctrs -lt 1 ]; then
		echo "Number of counters $num_ctrs should be > 0 (Default value $max_ctrs is used)"
		num_ctrs=$max_ctrs
	elif [ $num_ctrs -gt $max_ctrs ]; then
		echo "Number of counters $num_ctrs should be <= $max_ctrs (Default value $max_ctrs is used)"
		num_ctrs=$max_ctrs
	fi
}

#
# Validate user-input poll interval.
# Use default if not valid number.
#
# input:
# $poll_int	: poll interval in seconds
#
validate_poll_int()
{
	if [[ "$poll_int" == *[^0-9]* ]]; then
		echo "$poll_int is not valid for poll interval (Default value $DEFAULT_POLL_INT is used)"
		poll_int=$DEFAULT_POLL_INT
	elif [ $poll_int -lt 1 ]; then
		echo "poll interval $poll_int should be > 0 (Default value $DEFAULT_POLL_INT is used)"
		poll_int=$DEFAULT_POLL_INT
	fi
}

#
# Validate user-input poll count.
# Use default if not valid number.
#
# input:
# $poll_cnt	: poll count
#
validate_poll_cnt()
{
	if [[ "$poll_cnt" == *[^0-9]* ]]; then
		echo "$poll_cnt is not valid for poll count (Default value $DEFAULT_POLL_CNT is used)"
		poll_cnt=$DEFAULT_POLL_CNT
	elif [ $poll_cnt -lt 1 ]; then
		echo "poll count $poll_cnt should be > 0 (Default value $DEFAULT_POLL_CNT is used)"
		poll_cnt=$DEFAULT_POLL_CNT
	fi
}

#
# Return 0 if slot/mini/chip is hawk or wlv
#
# input:
# $1	: slot/mini/chip
#
# output:
# $CHP_NAME	: hawk or wlv
#
asic_db_present()
{
	if [ ! -e /proc/fabos/blade/$1/debug ]; then
		# Return error
		err_str="Unable to locate: /proc/fabos/blade/$1/debug"
		echo_xx > /dev/null 2>&1
		return
	fi

	# Make sure dbg module is registered
	db $1 | grep dbg > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# Return error
		err_str="Unable to locate dbg module at /proc/fabos/blade/$1/debug"
		echo_xx > /dev/null 2>&1
		return
	fi

	# This asic is hawk if hawk_stat module is registered
	db $1 | grep ${HAWK_NAME}_stat > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		CHP_NAME=$HAWK_NAME
		return
	fi

	# This asic is wlv if wlv_stat module is registered
	db $1 | grep ${WLV_NAME}_stat > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		CHP_NAME=$WLV_NAME
		return
	fi

	# Not hawk or wlv
	# Return error
	err_str="Unable to determine ASIC on $1"
	echo_xx > /dev/null 2>&1
}

#
# Find and display hawk/wlv
#
asic_scan()
{
	CWD=`pwd`
	echo
	cd /proc/fabos/blade
	for slot in `ls -d [0-9]* | sort -n`; do
		if test -f /proc/fabos/blade/$slot/info; then
			bladeid=`cat /proc/fabos/blade/$slot/info | /bin/grep --binary-files=text bladeId | sed 's/[^0-9]//g'`
			echo "Scanning slot: $slot blade id: $bladeid"
			cd $slot
			for mini in `ls -d [0-9]* | sort -n`; do
				cd $mini
				for chp in `ls -d [0-9]* | sort -n`; do
					asic_db_present $slot/$mini/$chp
					if [ "$?" -eq 0 ]; then
						echo "Found $CHP_NAME at $slot/$mini/$chp"
					fi
				done
				cd ..
			done
			cd ..
		fi
	done
	cd $CWD
	echo
}

#
# DB path input parser.
#
# input:
# $DB_PATH	: slot/mini/chip
#
# output:
# All tables filled base on which chip found at slot/mini/chip
#
validate_db_path()
{
	asic_db_present $DB_PATH
	if [ "$?" -ne 0 ]; then
		echo "$err_str"
		exit 1
	fi

	# Check for hawk_stat
	if [ "$CHP_NAME" = "$HAWK_NAME" ]; then
		hawk_get_stats_addr
		poll_fn="hawk_rd_stats"
		disp_fn="hawk_disp_stats"
		validate_start_slot
		validate_num_ctrs
		end_idx=$(( $start_idx + $num_ctrs - 1 ))
		echo "Num of counters: $num_ctrs, Stats slots: $start_idx-$end_idx"
		return
	fi

	# Check for wlv_stat
	if [ "$CHP_NAME" = "$WLV_NAME" ]; then
		echo "Wolverine is not implemented yet"
		exit 1
		return
	fi

	# Can't find wlv_stat/hawk_stat
	echo "Unable to determine ASIC on $DB_PATH"
	exit 1
}

## Main ##

# -s is expected as $1
# Just scan all slot/mini/chip and exit
if [ $# -gt 0 ] && [ "$1" = "-s" ]; then
	asic_scan
	exit
fi

prog_banner

# Needed util programe
$UTIL_PROG add32 0x1 0x2 > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
	echo "Unable to locate $UTIL_PROG util"
	exit 1
fi

# Stats buffers index
old_idx=0
new_idx=1

DB_PATH=
poll_int=$DEFAULT_POLL_INT
poll_cnt=$DEFAULT_POLL_CNT

if [ $# -lt 1 ]; then
	usage
fi

DB_PATH=$1
CHP_NAME=
VERBOSE=
DETAIL_FLAGS=
# DETAIL_FLAGS=yes
DELAY="delay_1"
max_zero_prints=20
start_idx=$DEFAULT_START_IDX
num_ctrs=$DEFAULT_NUM_CTRS

# hidden options for debug purpose
if [ $# -gt 1 ]; then
	shift 1
	# Get the command-line options
	while getopts "i:p:b:c:dvnhs" Option
	do
		case $Option in
		i ) poll_int=$OPTARG; validate_poll_int ;;
		p ) poll_cnt=$OPTARG; validate_poll_cnt ;;
		b ) start_idx=$OPTARG ;;
		c ) num_ctrs=$OPTARG ;;
		n ) DETAIL_FLAGS=yes ;;
		v ) VERBOSE=yes ;;
		d ) set -x ;;
		s ) DELAY="delay_2" ;;
		h ) usage ;;
		* ) ;;
		esac
	done
fi

db_rd_name="db $DB_PATH dbg peek"
db_rd_addr="db $DB_PATH dbg peek"
db_rd_multi_addr="db $DB_PATH dbg peek"

validate_db_path
start_mon

