#!/bin/bash
#
# mst        This starts and stops mst (Mellanox Software tools) drivers
#
# chkconfig: 3 25 75
# description: Starts and stops IB drivers used to burn FW and do simple IO
#

#insmod_flags="-f"
action() {
  STRING=$1
  echo -n "$STRING"
  shift
  $*
  rc=$?
  if test $rc -ne 0
  then
    echo " - Failure: $rc"
  else
    echo " - Success"
  fi
  return $rc
}
echo_success() {
    echo " - Success"
}
echo_failure() {
    echo " - Failure: $?"
}
failure() {
  echo -n "$* - Failure"
}

RETVAL=0

# Mellanox dev directory
mdir="/dev/mst"        # Directory where MST devices created
mbindir="$prefix/bin" # Binary directory where MST-utils and modules are located

if [ `uname -m` == "ppc" ]; then
    pcidir="/var/mst_pci"
else
    pcidir="$prefix/etc/pci" # Directory where PCI info should be saved
fi


PATH=${PATH}:/sbin:/usr/bin:/bin:${mbindir}

kver=`uname -r`
modir="$prefix/lib/$kver"
mdir="/dev/int_devs"

### create group of PCI devices per one instance of tavor (ddr,cr,uar)
### ------------------------------------------------------------------
create_char_dev()
{
    mknod -m 666 /dev/int_devs/intcdev c 231 250
    mknod -m 666 /dev/int_devs/intcdev1 c 231 251
    mknod -m 666 /dev/int_devs/intcdev2 c 231 252
}
PCI_MOD=int_hndl_main

# Main function
start()
{
    echo "    Starting $prog: "

    # Create empty MST devices directory
    rm -fr ${mdir}
    mkdir ${mdir}

    # Load all related modules
    # action "Loading MST Info module" insmod ${insmod_flags} ${modir}/mst_info.o

    if  lsmod | grep int_hndl > /dev/null
    then
        echo
        echo "    WARNING: int handler module already loaded"
        echo
    fi

    action "Loading int handler module" modprobe ${insmod_flags} ${PCI_MOD}

    create_char_dev

}

stop()
{
    echo "Stopping $prog"

    rmmod ${PCI_MOD} > /dev/null 2>&1
    rm -rf ${mdir}
}

print_status()
{
    # Check modules
    echo "Interupt modules:"
    echo "------------"
    if  lsmod | grep ${PCI_MOD} > /dev/null
    then
        echo "    PCI module loaded"
    else
        echo "    PCI module is not loaded"
    fi
}


# See how we were called.
case "$1" in
    help)
        cat <<END

        MST (Mellanox Software Tools) service
        =====================================

   This script is used to start MST service, to stop it,
   and for some other operations with Mellanox devices
   like reset or enabling remote access.

   The mst commands are:
   -----------------------

   mst start

       Create special files that represent Mellanox devices in
       directory ${mdir}. Load appropriate kernel modules and
       saves PCI configuration headers in directory ${pcidir}.
       After successfully completion of this command the MST driver
       is ready to work and you can invoke other Mellanox
       tools like Infiniburn or tdevmon.

   mst stop

       Stop Mellanox MST driver service, remove all special files/directories
       and unload kernel modules.

   mst restart

       Just like "mst stop" followed by  "mst start"

   mst server start [port]
       Start MST server to allow incoming connection.
       Default port is 23108

   mst server stop
       Stop MST server.

   mst remote add <hostname>[:port]
       Establish connection with specified host on specified port
       (default port is 23108). Add devices on remote peer to local
       devices list. <hostname> may be host name as well as an IP address.

   mst remote del <hostname>[:port]
       Remove all remote devices on specified hostname. <hostname>[:port] should
       be specified exactly as in the "mst remote add" command.

   mst ib add [local_hca_id] [local_hca_port] [lst-file]
       Add devices found in the IB fabric for inband access.
       Requires OFED installation and an active IB link.
           If local_hca_id and local_hca_port are given, the IB subnet connected
           to the given port is scanned. Otherwise, the default subnet is scanned.
       If an lst file is specified, devices are taken from this file.
       Otherwise, ibdiagnet tool is run to discover the fabric.

   mst ib del
       Remove all inband devices.

   mst status

       Print current status of Mellanox devices

   mst save

       Save PCI configuration headers in directory ${pcidir}.
       Invoked automatically by "mst start"

   mst load

        Load PCI configuration headers from directory ${pcidir}.
        Invoked automatically by "mst reset"

   mst reset <device_name>

       Reset device <device_name> and then do "mst load".
       For list of currently available Mellanox devices
       type "ls ${mdir}" or "mst status". Note that it
       may hang your device/computer when used inappropriately.

END
        ;;
    start)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        start
        ;;
    stop)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        stop
        rm -fr $pcidir
        ;;
    status)
        print_status
        ;;
    restart)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        stop
        start
        ;;
    *)
        echo "Usage:"
        echo "    $0 {start|stop|status|restart|help}"
        echo
        echo "Type \"$0 help\" for detailed help"
        RETVAL=1
esac
exit $RETVAL
