#!/bin/sh
#
# Copyright (C) 2010-2015, Mellanox Technologies Ltd.  ALL RIGHTS RESERVED.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#

### BEGIN INIT INFO
# Provides:        bsp for msn2410 system
# Required-Start:  $syslog 
# Required-Stop:   $syslog
# Default-Start:   2 3 4 5       
# Default-Stop:    0 1 6
# Short-Description: Mellanox x86 MSN2410 system bsp
# Description:       MLNX MSN2410 BSP
# Available options:
# start        - install all BSP kernel drivers, connect drivers to devices, create BSP dictionary as symbolic
#                links to sysfs entries
# stop         - destroy BSP dictionary, disconnect drivers from devices, uninstall BSP kernel drivers
# restart      - combined stop and start sequence
# reprobe_asic - disconnect ASIC driver from device and connect it again, can be used in case ASIC core
#                driver reloading
### END INIT INFO

. /lib/lsb/init-functions

cpu=/sys/devices/platform/coretemp.0
asic=/sys/bus/i2c/devices/1-0048
sys_eeprom=/sys/bus/i2c/devices/8-0051

ACTION=$1

is_module()
{
        /sbin/lsmod | grep -w "$1" > /dev/null
        RC=$?
        return $RC
}

load_modules()
{
	log_daemon_msg "Starting Mellanox x86 system bsp modules"
	log_end_msg 0

	if is_module mlxcpld-io; then
		log_warning_msg "mlxcpld-io module has already been loaded"
	else
		if modprobe mlxcpld-io 2>/dev/null; then
			log_success_msg "mlxcpld-io module load passed"
		else
			log_failure_msg "mlxcpld-io module load failed"
		fi
	fi

	if is_module i2c-kempld; then
		log_warning_msg "i2c-kempld module has already been loaded"
	else
		if modprobe i2c-kempld 2>/dev/null; then
			log_success_msg "i2c-kempld module load passed"
		else
			log_failure_msg "i2c-kempld module load failed"
		fi
	fi

	if is_module mlnx_asic_drv; then
		log_warning_msg "mlnx_asic_drv module has already been loaded"
	else
		if modprobe mlnx_asic_drv 2>/dev/null; then
			log_success_msg "mlnx_asic_drv module load passed"
		else
			log_failure_msg "mlnx_asic_drv module load failed"
		fi
	fi

	if is_module coretemp; then
		log_warning_msg "coretemp module has already been loaded"
	else
		if modprobe coretemp 2>/dev/null; then
			log_success_msg "coretemp module load passed"
		else
			log_failure_msg "coretemp module load failed"
		fi
	fi

	if is_module at24; then
		log_warning_msg "at24 module has already been loaded"
	else
		if modprobe at24 io_limit=32 2>/dev/null; then
			log_success_msg "at24 module load passed"
		else
			log_failure_msg "at24 module load failed"
		fi
	fi
}

unload_modules()
{
	log_daemon_msg "Stopping Mellanox x86 system bsp module"
	log_end_msg 0

	if is_module at24; then
		if rmmod at24 2>/dev/null ; then
			log_success_msg "at24 module is unloaded"
		else
			log_failure_msg "at24 module unload failed"
		fi
	else
		log_warning_msg "No at24 module loaded"
	fi

	if is_module coretemp; then
		if rmmod coretemp 2>/dev/null ; then
			log_success_msg "coretemp module is unloaded"
		else
			log_failure_msg "coretemp module unload failed"
		fi
	else
		log_warning_msg "No coretemp module loaded"
	fi

	if is_module mlnx_asic_drv; then
		if rmmod mlnx_asic_drv 2>/dev/null ; then
			log_success_msg"mlnx_asic_drv module is unloaded"
		else
			log_failure_msg "mlnx_asic_drv module unload failed"
		fi
	else
		log_warning_msg "No mlnx_aisc_drv module loaded"
	fi

	if is_module i2c-kempld; then
		if rmmod i2c-kempld 2>/dev/null ; then
			log_success_msg "i2c-kempld module is unloaded"
		else
			log_failure_msg "i2c-kempld module unload failed"
		fi
	else
		log_warning_msg "No i2c-kempld module loaded"
	fi

	if is_module mlxcpld-io; then
		if rmmod mlxcpld-io 2>/dev/null ; then
			log_success_msg "mlxcpld-io module is unloaded"
		else
			log_failure_msg "mlxcpld-io module unload failed"
		fi
	else
		log_warning_msg "No mlxcpld-io module loaded"
	fi

	return 0
}

status()
{
    lsmod
}

connect_platform()
{
        if [ ! -d $cpld ]; then
            echo mlnx-cpld-drv 0x60 > /sys/bus/i2c/devices/i2c-2/new_device
        fi
        if [ ! -d $asic ]; then
            echo mlnx-asic-drv-56 0x48 > /sys/bus/i2c/devices/i2c-2/new_device
            attr=`ls $asic/hwmon`
            asic=$asic/hwmon/$attr
        fi
}

disconnect_platform()
{
        if [ -d $asic ]; then
            echo 0x48 > /sys/bus/i2c/devices/i2c-1/delete_device
        fi
}

case $ACTION in
        start)
                load_modules
                connect_platform

                # EEPROM
                if [ ! -L /bsp/eeprom/sys_eeprom ]; then
                    attr=`find $sys_eeprom/ -name eeprom`
                    if [ -e $attr ]; then
                        ln -s $attr /bsp/eeprom/sys_eeprom
                    fi
                fi

               # QSFP
		for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                do
                    if [ ! -L /bsp/qsfp/qsfp"$i" ]; then
                       ln -s $asic/qsfp"$i"_eeprom /bsp/qsfp/qsfp"$i"
                    fi
                done

                for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                do
                    if [ ! -L /bsp/qsfp/qsfp"$i"_status ]; then
                       ln -s $asic/qsfp"$i"_status /bsp/qsfp/qsfp"$i"_status
                    fi
                done

        ;;
        stop)
                # Clean bsp directory
                if [ -d /bsp ]; then
                    rm -rf /bsp
                fi

                disconnect_platform
                unload_modules
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reprobe_asic)
                if [ -d $asic ]; then
                    echo 0x48 > /sys/bus/i2c/devices/i2c-1/delete_device
                fi
                echo mlnx-asic-drv-56 0x48 > /sys/bus/i2c/devices/i2c-2/new_device
        ;;
        status)
                status
        ;;
        *)
                echo
                echo "Usage: `basename $0` {start|stop|restart|reprobe_asic}"
                echo
                exit 1
        ;;
esac
