#!/bin/sh
# This file is provided under a dual BSD/GPLv2 license.  When using or
# redistributing this file, you may do so under either license.
#
# GPL LICENSE SUMMARY
#
#  Copyright (c) 2015 Intel Corporation, All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# BSD LICENSE
#
#  Copyright (c) 2015 Intel Corporation, All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  - Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  - Neither the name of Intel Corporation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# pkt_cntr_test -- exercise the varius error counters by sending various
# packets via loopback and examining the results via ipathstats.
# the gory details are in pkt_probe, which builds on ipath_pkt_send
# The variable QIB_EXPECTED_CNTRS is used to communicate the expected counter
# values, e.g.
# QIB_EXPECTED_CNTRS="RxDlidFltr=2 RxEBP=2"
# indicates that those counts (and only those counts) should be 2

unit=$1
if [ -z "$unit" ]
then
  unit=0:1
fi

if [ -z "$CHIP_TYPE" ]
then
  bdname=`ipath_control -u $unit -iv | grep InfiniPath | cut -d '_' -f2 | cut -d ',' -f1`
  case "$bdname" in
    "QHT7040" | "QHT7140" )
      CHIP_TYPE=6110
    ;;
    "QLE7140" | "QEM7140" | "QMH7140" | "QMI7140" )
      CHIP_TYPE=6120
    ;;
    "QLE7280" | "QLE7240" )
      CHIP_TYPE=7220
    ;;
    "QLE7340" )
      CHIP_TYPE=7322
      echo Using kernel mode send
      KOPT=-k
      TEST_MODE_ADDS=1 # Unless/until RTL changes, IB pkts only
    ;;
    * )
      echo "Unknown CHIP_TYPE for board type $bdname"
      exit 1
    ;;
  esac
fi

if [ "$TEST_MODE_ADDS" = "" ]
then
  TEST_MODE_ADDS=2 # Pre-7322
fi

TEST_MODE_TX_WDS=`expr 58 + $TEST_MODE_ADDS`

if [ -z $pktdir ]
then
  pktdir=`pwd`
fi

if [ -z "$ptname" ]
then
  ptname="$pktdir/pkt_probe -i $unit $KOPT "
fi

RXNORM="RxPkt=1 RxWords=58"
TXNORM="TxPkt=1 TxWords=58"
PKTNORM="$TXNORM $RXNORM"
PKTTEST="TxPkt=1 TxWords=$TEST_MODE_TX_WDS $RXNORM"

err_cnt=0
if [ -z "$RLID" ]
then
  export RLID=1
fi

echo "-- Normal packet should provoke no errors"
QIB_EXPECTED_CNTRS="$PKTNORM" $ptname -L$RLID -c1 $pktdir/normal
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

echo "-- Normal packet in TestMode should provoke no errors"
QIB_EXPECTED_CNTRS="$PKTTEST" $ptname -L$RLID -c1 $pktdir/normal_egp
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

if [ $CHIP_TYPE = 6120 ]
then
  echo "-- 6120 or earlier do not count EBP, so no errors from otherwise good packet with EBP"
  QIB_EXPECTED_CNTRS="$PKTTEST" $ptname -L$RLID -c1 $pktdir/normal_ebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

if [ $CHIP_TYPE -eq 7220 ]
then
  echo "-- 7220 counts EBP and packet/words, on otherwise good packet with EBP"
  QIB_EXPECTED_CNTRS="RxEBP=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/normal_ebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

if [ $CHIP_TYPE -eq 7322 ]
then
  echo "-- 7322 counts EBP and packet, but no RxWords, on otherwise good packet with EBP"
  QIB_EXPECTED_CNTRS="RxEBP=1 TxPkt=1 TxWords=$TEST_MODE_TX_WDS RxPkt=1" $ptname -L$RLID -c1 $pktdir/normal_ebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

echo "-- Check for detection of ICRC error"
QIB_EXPECTED_CNTRS="RxICRCerr=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_icrc
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

echo "-- Check for detection of VCRC error"
QIB_EXPECTED_CNTRS="RxVCRCerr=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_vcrc
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

if [ $CHIP_TYPE -ge 7220 ]
then
  echo "-- Check for VCRC error priority over ICRC"
  QIB_EXPECTED_CNTRS="RxVCRCerr=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_ivcrc
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

echo "-- Bad length (EBP suppressed) should count only in Rx"
QIB_EXPECTED_CNTRS="RxBadLen=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_len_egp
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

if [ $CHIP_TYPE = 6120 ]
then
  echo "-- Bad length (EBP not suppressed) should count in Tx, receive runt packet"
  QIB_EXPECTED_CNTRS="RxBadLen=1 RxEBP=1 RxVCRCerr=1 TxInvalLen=1 $PKTNORM" $ptname -L$RLID -c1 $pktdir/bad_len_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP suppressed) should count in Tx, Rx Bad Format"
  QIB_EXPECTED_CNTRS="RxBadFormat=1 TxUnsupVL=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/unsupvl8_egp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP not suppressed) should count in Tx, Rx Bad Format (Same as noEBP?)"
  QIB_EXPECTED_CNTRS="RxBadFormat=1 TxUnsupVL=1 $PKTNORM" $ptname -L$RLID -c1 $pktdir/unsupvl8_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

if [ $CHIP_TYPE -eq 7220 ]
then
  echo "-- Bad length (EBP not suppressed) should count in Tx, but receive packet"
  QIB_EXPECTED_CNTRS="RxBadLen=1 RxEBP=1 TxInvalLen=1 $PKTNORM" $ptname -L$RLID -c1 $pktdir/bad_len_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP suppressed) should count in Tx, Rx"
  QIB_EXPECTED_CNTRS="RxVlErr=1 TxUnsupVL=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/unsupvl8_egp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP not suppressed) should count in Tx, Rx (Same as noEBP)"
  QIB_EXPECTED_CNTRS="RxVlErr=1 TxUnsupVL=1 $PKTNORM" $ptname -L$RLID -c1 $pktdir/unsupvl8_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

if [ $CHIP_TYPE -eq 7322 ]
then
  echo "-- Bad length (EBP not suppressed) should count in Tx, EBP on Rx"
  QIB_EXPECTED_CNTRS="RxEBP=1 TxInvalLen=1 $TXNORM RxPkt=1" $ptname -L$RLID -c1 $pktdir/bad_len_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP suppressed) should count in Tx, Rx"
  QIB_EXPECTED_CNTRS="RxVlErr=1 TxUnsupVL=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/unsupvl8_egp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

  echo "-- Unsupported VL (EBP not suppressed) produces no traffic?"
  QIB_EXPECTED_CNTRS="TxUnsupVL=1" $ptname -L$RLID -c1 $pktdir/unsupvl8_mayebp
  if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
fi

echo "-- Wrong P_Key (EBP suppressed) should count only in Rx"
QIB_EXPECTED_CNTRS="RxInvalPKey=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_pkey_egp
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

echo "-- Wrong LRH Version (EBP suppressed) should count only in Rx"
QIB_EXPECTED_CNTRS="RxBadFormat=1 $PKTTEST" $ptname -L$RLID -c1 $pktdir/bad_vers_egp
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

echo "-- Send several normal packets to cool off LLI counter"
QIB_EXPECTED_CNTRS="TxPkt=16 TxWords=928 RxPkt=16 RxWords=928" $ptname -L$RLID -c16 $pktdir/normal
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
echo "-- Send not quite enough (15) VCRC errors to trigger LLI count"
TX_VCRC_WDS=`expr 15 \* 6`
RX_VCRC_WDS=`expr 15 \* 5`
QIB_EXPECTED_CNTRS="RxVCRCerr=15 TxPkt=15 TxWords=$TX_VCRC_WDS RxPkt=15 RxWords=$RX_VCRC_WDS" $ptname -L$RLID -c15 $pktdir/raw_min_vcrc
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi
TX_MIX_WDS=`expr $TX_VCRC_WDS + \( 15 \* 58 \)`
RX_MIX_WDS=`expr $RX_VCRC_WDS + \( 15 \* 58 \)`
echo "-- Interspersed good and bad should not trigger LLI"
QIB_EXPECTED_CNTRS="RxVCRCerr=15 TxPkt=30 TxWords=$TX_MIX_WDS RxPkt=30 RxWords=$RX_MIX_WDS" $ptname -L$RLID -c15 $pktdir/no_lli_2

echo "-- Sustained error packets should trigger Local Link Integrity count"
QIB_EXPECTED_CNTRS="RxVCRCerr=35-50 RxLLIErr=1-10 IBStatusChng=1-25 IBLnkRecov=1-4 IBRxLinkErr=0-4 \
 TxDropped=0-4 RxPktDropped=0-4 TxPkt=46-50 TxWords=276-300 RxPkt=46-50 RxWords=230-250" \
  $ptname -L$RLID -c50 $pktdir/raw_min_vcrc
if [ $? -ne 0 ] ; then err_cnt=`expr $err_cnt + 1` ; fi

echo "=========================================================="
echo $err_cnt total errors
if [ $err_cnt -ne 0 ]
then
  echo TEST FAILED
  exit 1
fi

exit 0

