#!/bin/ksh
# @(#) *----------------------------------------------------------------*
# @(#) |  [ricinit] Initialize and Configure up to 14 ARTIC960 adapters |
# @(#) |  ARTIC960 - korn shell script                                  |
# @(#) *----------------------------------------------------------------*
#*----------------------------------------------------------------------------*
#| Filename: ricinit                                                          |
#|                                                                            |
#| Input Parameters :                                                         |
#|       parameter 1 : Card number to configure [ 0 .. 13]                    |
#|       ....                                                                 |
#|       parameter n : Card number to configure [ 0 .. 13]                    |
#|                                                                            |
#| Dependencies :                                                             |
#|       RICPATH variable definition                                          |
#|                                                                            |
#*----------------------------------------------------------------------------*
trap "echo '\n[Break] ricinit interrupted, script terminated ...';exit 1" 2 3 9 15

function err1
{
echo "*--------------------------------------------------------------------*"
echo "|                              *--------*                            |"
echo "|                              |        |                            |"
echo "|                              v        |                            |"
echo "| Command Syntax :  ricinit ----- <cn> ----------*                   |"
echo "|                            |              |                        |"
echo "|                            *--- ? --------*                        |"
echo "|   where:                                                           |"
echo "|                                                                    |"
echo "|       <cn> : ARTIC960 logical card number                          |"
echo "|                                                                    |"
echo "|   example:                                                         |"
echo "|                                                                    |"
echo "|       ricinitd 0 1 3 4 5 10                                        |"
echo "|                                                                    |"
echo "|       This resets and loads card numbers 0, 1, 3, 4, 5, and 10     |"
echo "*--------------------------------------------------------------------*"
exit 1
}
function err2
{
echo "*--------------------------------------------------------------------*"
echo "| [Fatal Error] : RICPATH not set, cannot continue.                  |"
echo "|                 Action : Set the RICPATH environment variable.     |"
echo "*--------------------------------------------------------------------*"
exit 1
}
function err3
{
echo "*--------------------------------------------------------------------*"
echo "| [Fatal Error] : ARTIC960 run-time product not found at the         |"
echo "|                 specified RICPATH location, cannot continue.       |"
echo "|                 Action : Check the RICPATH environment variable.   |"
echo "*--------------------------------------------------------------------*"
exit 1
}

# ---------------------------------------------------------------------------
#                          Command line parameters checking
# ---------------------------------------------------------------------------
if test $# -lt 1 || test $# -gt 14 || [ "${1}" = "?" ] ; then
   err1 ; fi                                # invalid command line parameter
                                            # or help syntax requested

if [ "${RICPATH}" = "" ] ; then
   err2 ; fi                                # RICPATH not defined

if [ ! -d "${RICPATH}/bin" ] ; then
   err3 ; fi                                # ARTIC960 product not installed

what ${RICPATH}/bin/ricinit                 # display information message

if test $# -gt 1 ; then multi960="s" ; else multi960="" ; fi

# ---------------------------------------------------------------------------
#                        Kill the Mailbox process
# ---------------------------------------------------------------------------
echo "[Info] Killing system unit mailbox process ..."
${RICPATH}/bin/ricmbx -K
retcode=$?
if [[ $retcode != 0 && $retcode != 33 ]] ; then
   echo [Info] Mailbox Process could not be killed. Exit code = $retcode
   exit $retcode
fi

# ---------------------------------------------------------------------------
#                        Configure defined cards
# ---------------------------------------------------------------------------
echo "[Info] Configuring $# ARTIC960 card${multi960} ..."
for cn in $*
do
   echo "[Info] Resetting card ${cn} ..."
   ${RICPATH}/bin/ricreset ${cn}
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Resetting card ${cn} failed. Exit code = $retcode
      exit $retcode
   fi

   echo "[Info] Loading system files on card ${cn} ..."
   ${RICPATH}/bin/ricload ${cn} ${RICPATH}/bin/ric_kern.rel -f ${RICPATH}/bin/ric_kern.cfg
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Loading ARTIC960 Kernel failed. Exit code = $retcode
      exit $retcode
   fi
   ${RICPATH}/bin/ricload ${cn} ${RICPATH}/bin/ric_base.rel
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Loading ARTIC960 Base Subsystem failed. Exit code = $retcode
      exit $retcode
   fi
   ${RICPATH}/bin/ricload ${cn} ${RICPATH}/bin/ric_mcio.rel -f ${RICPATH}/bin/ric_mcio.cfg -w 5
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Loading ARTIC960 DMA Subsystem failed. Exit code = $retcode
      exit $retcode
   fi
   ${RICPATH}/bin/ricload ${cn} ${RICPATH}/bin/ric_scb.rel  -f ${RICPATH}/bin/ric_scb.cfg  -w 5
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Loading ARTIC960 SCB Subsystem failed. Exit code = $retcode
      exit $retcode
   fi

   echo "[Info] Setting date-time of day on card ${cn} ..."
   ${RICPATH}/bin/ricload ${cn} -t
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Setting date-time of day failed. Exit code = $retcode
      exit $retcode
   fi

done

# ---------------------------------------------------------------------------
#                        Configure Peer-to-Peer communications
# ---------------------------------------------------------------------------
echo "[Info] Configuring Peer-to-Peer communications ..."

let i1=0
set +A cntab $*
# Configure card to system unit pipes
while [ "${cntab[${i1}]}" != "" ] ; do
                                        # init card to system unit pipes
   ${RICPATH}/bin/riccnfg -L ${cntab[${i1}]}
   retcode=$?
   if [ $retcode != 0 ] ; then
      echo [Info] Configuring Peer-to-Peer communications failed. Exit code = $retcode
      exit $retcode
   fi
   let i1=${i1}+1
done

# Uncomment the next 10 lines if peer-to-peer communications are supported.
# let i1=0
# while [ "${cntab[${i1}]}" != "" ] ; do
#    let i2=${i1}+1
#    while [ "${cntab[${i2}]}" != "" ] ; do
#                                         # init card to card pipes
#       ${RICPATH}/bin/riccnfg -L ${cntab[${i1}]} ${cntab[${i2}]}
#       let i2=${i2}+1
#    done
#    let i1=${i1}+1
# done

# ---------------------------------------------------------------------------
#                        Start the Mailbox process
# ---------------------------------------------------------------------------
echo "[Info] Starting system unit Mailbox process ..."
${RICPATH}/bin/ricmbx -C ${RICPATH}/bin/ricmbx.cfg
retcode=$?
if [ $retcode != 0 ] ; then
   echo [Info] Starting the Mailbox Process failed. Exit code = $retcode
   exit $retcode
fi

# ---------------------------------------------------------------------------
#                        End of ricinit script file
# ---------------------------------------------------------------------------
echo "[Info] Successful loading ..."

exit 0
