#!/bin/sh
### BEGIN INIT INFO
# Provides:          sx_low_level_if
# Required-Start:
# Required-Stop:
# X-Start-Before:
# X-Stop-After:
# Default-Start:
# Default-Stop:
# Short-Description: Starting and stopping SX low level interafces
# Description: Starts and stops SX low level interafces
### END INIT INFO

# chkconfig: 3 05 95
#

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

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
prog="sx_low_level_if"

create_char_dev_mellaggra()
{
    if [ ! -e /dev/mlnxif ]; then
        mknod /dev/mlnxif c 243 0
    fi
    if [ ! -e /dev/mlnxif_unb ]; then
        mknod /dev/mlnxif_unb c 244 0
    fi

}

del_char_dev_mellaggra()
{
    if [ -e /dev/mlnxif_unb ]; then
        rm -f /dev/mlnxif_unb
    fi
    if [ -e /dev/mlnxif ]; then
        rm -f /dev/mlnxif
    fi
}

create_char_dev_cpld()
{
    if [ ! -f /dev/cpld ]; then
        mkdir -p /dev/cpld
    fi
    if [ ! -e /dev/cpld/cpld0 ]; then
        mknod -m 666 /dev/cpld/cpld0 c 232 250
    fi
    if [ ! -e /dev/cpld/cpld1 ]; then
        mknod -m 666 /dev/cpld/cpld1 c 232 251
    fi
    if [ ! -e /dev/cpld/cpld2 ]; then
        mknod -m 666 /dev/cpld/cpld2 c 232 252
    fi
}

load_lpci2c()
{
        if  lsmod | grep lpci2c > /dev/null
        then
            echo "    NOTE: lpci2c module already loaded"
        else
            action "Loading lpci2c " modprobe lpci2c
        fi
}

del_char_dev_cpld()
{
    if [ -f /dev/cpld ]; then
        rm -rf /dev/cpld
    fi
}

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

    # Temporary:
    # Dangerous commands. To be removed after power sequence is fixed in x86 hardware.
#    if [ -e /sbin/poweroff ]; then
#	#rm -rf /sbin/poweroff
#    fi
#    if [ -e /sbin/shutdown ]; then
#	#rm -rf /sbin/shutdown
#    fi

    if [ "$MACH" = "i686" -o "$MACH" = "x86_64" ]; then
        depmod -a > /dev/null 2>&1
        #modprobe ehci_pci
        modprobe i2c-mux
        modprobe cpldmux
        #modprobe i2c-mux-pca954x
        modprobe pca954x
        modprobe ucd9200
        modprobe pmbus
        #modprobe sbs-battery
    fi
    if  lsmod | grep sx_glue_if > /dev/null
    then
        echo "    NOTE: sx low level if module already loaded"
    else
        action "Loading glue logic low level " modprobe sx_glue_if
    fi
    if  lsmod | grep watchdog > /dev/null
    then
        echo "    NOTE: watchdog module already loaded"
    else
        action "Loading watchdog " modprobe watchdog
    fi
    if  lsmod | grep cpld_handler > /dev/null
    then
        echo "    NOTE: cpld handler module already loaded"
    else
        action "Loading cpld handler " modprobe cpld_handler
    fi
    create_char_dev_cpld
    if  lsmod | grep mellaggra_mod > /dev/null
    then
        echo "    NOTE: mellaggra module already loaded"
    else
        action "Loading mellaggra module " modprobe mellaggra_mod
    fi
    if [ "$MACH" = "i686" -o "$MACH" = "x86_64" ]; then
        load_lpci2c
	if  lsmod | grep coretemp > /dev/null
        then
            echo "    NOTE: coretemp module already loaded"
        else
            action "Loading coretemp " modprobe coretemp
        fi
    fi
    create_char_dev_mellaggra
    if  lsmod | grep switchx > /dev/null
    then
        echo "    NOTE: i2c sx module already loaded"
    else
        action "Loading sx i2c module " modprobe switchx
    fi
    if [ "$MACH" = "i686" -o "$MACH" = "x86_64" ]; then
#        /usr/bin/mlxi2c --master-mode --dontclose -without_modules show devs > /dev/null 2>&1
        if  lsmod | grep gpiopch > /dev/null
        then
            echo "    NOTE: GPIO PCH module already loaded"
        else
            action "Loading GPIO PCH module " modprobe gpiopch
            # Export smbus reset pin
            echo 8 > /sys/devices/platform/gpio_pch/export
        fi
    fi
    if [ ! -d /dev/mst ]; then
        mkdir -p /dev/mst
    fi
    if [ ! -d /dev/mst/dev/i2c-1 ]; then
        ln -s /dev/i2c-1 /dev/mst/dev-i2c-1 > /dev/null 2>&1
    fi
    if [ ! -d /dev/mst/i2c-0 ]; then
        ln -s /dev/i2c-0 /dev/mst/dev-i2c-0 > /dev/null 2>&1
    fi
    sleep 1
}

# Main function for boot floppy start
start_bf()
{
    echo "    Starting $prog: "

    if [ "$MACH" == "i686" -o "$MACH" == "x86_64" ]; then
        depmod -a > /dev/null 2>&1
        modprobe i2c-mux
        modprobe cpldmux
        #modprobe i2c-mux-pca954x
        modprobe pca954x
    fi
    if  lsmod | grep mellaggra_mod > /dev/null
    then
        echo "    NOTE: mellaggra module already loaded"
    else
        action "Loading mellaggra module " modprobe mellaggra_mod
    fi
    if [ "$MACH" = "i686" ]; then
        if [ ! -h /dev/i2c-0 ]; then
            ln -s /dev/i2c-1 /dev/i2c-0
        fi
    fi
    create_char_dev_mellaggra
    if [ "$MACH" == "i686" -o "$MACH" == "x86_64" ]; then
        load_lpci2c
    fi
    if [ ! -d /dev/mst ]; then
        mkdir -p /dev/mst
    fi
    if [ ! -d /dev/mst/dev/i2c-1 ]; then
        ln -s /dev/i2c-1 /dev/mst/dev-i2c-1 > /dev/null 2>&1
    fi
    if [ ! -d /dev/mst/i2c-0 ]; then
        ln -s /dev/i2c-0 /dev/mst/dev-i2c-0 > /dev/null 2>&1
    fi
    sleep 1
}

stop()
{
    echo "Stopping $prog"
    if  lsmod | grep gpiopch > /dev/null
    then
        rmmod gpiopch > /dev/null 2>&1
        echo "gpiopch module unloaded"
    fi
    if  lsmod | grep switchx > /dev/null
    then
        rmmod switchx > /dev/null 2>&1
        echo "sx i2c module unloaded"
    fi
    #if  lsmod | grep mellaggra_mod > /dev/null
    #then
    #    rmmod mellaggra_mod > /dev/null 2>&1
    #    echo "mellaggra module unloaded"
    #fi
    #del_char_dev_mellaggra
    #if  lsmod | grep lpci2c > /dev/null 2>&1
    #then
    #    rmmod lpci2c > /dev/null 2>&1
    #    echo "lpci2c module unloaded"
    #fi
    if  lsmod | grep cpld_handler > /dev/null 2>&1
    then
        rmmod cpld_handler > /dev/null 2>&1
        echo "cpld handler module unloaded"
    fi
    del_char_dev_cpld
    if  lsmod | grep watchdog > /dev/null 2>&1
    then
        rmmod watchdog > /dev/null 2>&1
        echo "watchdog module unloaded"
    fi
    if  lsmod | grep sx_glue_if > /dev/null 2>&1
    then
        rmmod sx_glue_if > /dev/null 2>&1
        echo "sx low level if module unloaded"
    fi
    if [ -d /dev/mst ]; then
        rm -rf /dev/mst
    fi
    if [ -h /dev/mst/dev-i2c-0 ]; then
        rm /dev/mst/dev-i2c-0
    fi
	if [ -h /dev/mst/dev-i2c-1 ]; then
        rm /dev/mst/dev-i2c-1
    fi
    rm -rf /dev/mst
}

print_status()
{
    # Check modules
    echo "Modules status"
    echo "--------------"
    for M in sx_glue_if cpld_handler switchx mellaggra_mod watchdog
    do
    if  lsmod | grep ${M} > /dev/null
    then
        echo "    ${M} module loaded"
    else
        echo "    ${M} module is not loaded"
    fi
    done
}

# Main body

# Test machine on which script is running (don't run if not x86)
MACH=`uname -m`

case "$1" in
    help)
        cat <<END
   $0 start
       Load appropriate kernel modules.
   $0 stop
       Unload kernel modules.
   $0 restart
       Just like "stop" followed by  "start"
   $0 status
       Print current status of Mellanox modules.
END
        ;;
    start)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        start
        ;;
    start_bf)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        start_bf
        ;;
    stop)
        if [ `id -u` -ne 0 ]; then
            echo "You must be root to do that"
            exit 1
        fi
        stop
        ;;
    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
