#!/bin/sh
#
#    Copyright (c) 1996-2008 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Initialization script to start/stop Blade OS services and
#      interfaces
#


#
# Set a signal handler for the script.
#
trap sig_handler SIGBUS

#
# Signal handler for this script.
#
sig_handler()
{
	echo "**********************************************************"
	echo "RECEIVED UNEXPECTED SIGNAL. PROCESS INFORMATION:"
	echo "**********************************************************"
	ps auxw
	exit 1
}

# Source common library
. /etc/init.d/functions

PATH=${PATH}":/fabos/sbin:/fabos/bin:/fabos/libexec"

# setup library link path
LD_LIBRARY_PATH="/fabos/lib:/lib"
export LD_LIBRARY_PATH

FABOSHOME=/fabos
export FABOSHOME

# Determine the system platform identifier.
SWBD=`sin | swbd 2> ${NULL}`
MODE="d`bootenv startup 2> ${NULL}`"

echo "Platform $SWBD"

# Determine the state-sync transport based on the platform identifier.
case ${SWBD##SWBD} in
    '10' | '21' | '42')
	XPT="XCP"
	;;

    *)
	XPT="XLO"
	;;
esac

#
# modutil <load | unload> <module name>
#
# Load a kernel module
#
INSMOD_SUCESS=0
modutil()
{

    case $1 in

    load)
	insmod /fabos/modules/$2.ko $3 $4 $5 $6> ${NULL} 2>&1
	RETURN_VALUE=$?
	if [ $RETURN_VALUE -ne $INSMOD_SUCESS ]; then
	  echo "Error " $RETURN_VALUE " returned by insmod while loading " $2
	fi
	;;

    *)
	;;
    esac
}

#
# Load modules on Marathon platform.
#
swbd_modules()
{
	case "${SWBD##SWBD}" in
	'36')
		modutil load msbd-module
		modutil load fcip-module
		modutil load bcm-module
		;;
	'47')
		modutil load ibd-module
 		modutil load pcmsg-module
 		modutil load iscsi-module
 		modutil load iflpr-chip-module
		;;
	'50')
		modutil load msbd-module
		modutil load int1250-module
		modutil load bpsas-module crash_dump_check=1
		modutil load mpi-module
		modutil load csmsg-module
		;;
	'55')
		modutil load msbd-module
		modutil load int1250-module
		modutil load bpsas-module crash_dump_check=1
		modutil load mpi-module
		modutil load csmsg-module
		echo "loading bonding module"
		insmod /lib/modules/default/kernel/drivers/net/bonding/bonding.ko \
			mode=1 miimon=100
		;;
	'78' | '84')
		modutil load dcebd-module platform=${SWBD##SWBD}
		;;
	'88')
		modutil load dcebd-module platform=${SWBD##SWBD}
		modutil load altera-module
		;;
	'68')
		modutil load lance-module
 		modutil load vader-module
 		modutil load tape-module
		echo "loading bonding module"
		insmod /lib/modules/default/kernel/drivers/net/bonding/bonding.ko \
			mode=1 miimon=100
		modutil load diag-module
		;;
	'81' | '85')
		modutil load msbd-module
		# modutil load bp-enetswch-module
		modutil load mpi-module
		modutil load csmsg-module
		modutil load virtex-module
		;;
	'89')
		modutil load msbd-module
		modutil load linux-kernel-bde dmasize=8M himem=1
		modutil load linux-bcm-core
		modutil load bcm-switch-module
		modutil load linux-uk-proxy
		modutil load linux-bcm-diag
		modutil load gpio-module
		modutil load virtex-module
		;;
	esac

	return 0
}

#
# Create node for each device driver.
# mk_device <module name> {<device name> <minor-dev-num>}
#
mk_device()
{
	set $@
	node=$1
	shift 1
	major=`grep $node\$ /proc/devices | cut -d ' ' -f 1`
	while [ $# -gt 0 ]; do
		if [ ! $major ]; then
			exit 1;
		fi
		rm -f /dev/$1
		mknod --mode=666 /dev/$1 c $major $2
		shift 2
	done
}

#
# Create device driver for Marathon Blade
#
swbd_devices()
{	

	# example of platform specific devices
	case "${SWBD##SWBD}" in
	'36')
    	echo "creating mbd bswd devices..."
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 21
		mk_device bcm bcm0 0 bcm1 1
		mk_device bswd bswd 0
		;;
	'47')
		mk_device ibd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3\
				  mbd_ip 4 mbd_intl 5 mbd_link 6 mbd_cfg 21
		mk_device pcmsg pcmsg0 0 pcmsg1 1
		mk_device bswd bswd 0
		# TBD: add lance control path devices
		#mk_device lance lanceTest 0\
		#	lance_tape0 7 lance_tape1 8 lance_tape2 9\
		#	lance_rekey0 10 lance_rekey1 11 lance_rekey2 12\
		#	lance_vtvi0  13 lance_vtvi1 14  lance_vtvi2 15
		;;
	'50' | '55')
		# Minor number of mbd_cfg is CBD_GETCFG_MINORNUM
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 21
		mk_device csmsg csmsg0 0 csmsg1 1 csmsg_ctrl 2
		mk_device bswd bswd 0
                mk_device int1250 int1250-0 1 int1250-1 2
                rm /dev/net/tun > /dev/null 2>&1
                mkdir /dev/net > /dev/null 2>&1
                mknod --mode=666 /dev/net/tun c 10 200
                echo 1 > /proc/sys/net/ipv4/ip_forward
		;;
	'78' | '84')
    	echo "creating mbd bswd devices..."
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3
		mk_device bswd bswd 0
		;;
	'88')
    	echo "creating mbd bswd altera devices..."
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 21
		mk_device bswd bswd 0
		mk_device altera altera 0
		;;
	'68')
		mk_device lance mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 4\
				mbd_intl 5\
				lance_rekey0 10 lance_rekey1 11 lance_rekey2 12\
				lance_vtvi0  13 lance_vtvi1 14  lance_vtvi2 15
		mk_device tape	lance_tape0 0
		mk_device bswd bswd 0
                mk_device diag diag 0
		;;
	'81' | '85')
		# No ISC on BP util rebase happens with v6.2
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 21
		mk_device csmsg csmsg0 0 csmsg1 1 csmsg_ctrl 2
		mk_device virtex virtex0 0
		;;
	'89')
		mk_device mbd mbd0 0 mbd1 1 mbd2 2 mbd_ctl 3 mbd_cfg 21
		mk_device virtex virtex0 0 virtex1 1
		mk_device linux-uk-proxy linux-uk-proxy 0
		;;
	esac
}

#
# Launch appications/daemons
#
swbd_daemons()
{
	case "${SWBD##SWBD}" in
	'47')
		/fabos/libexec/raslogapd &
		/fabos/libexec/bswd -p ${SWBD##SWBD} &
		/fabos/libexec/sb &
		# modify this once mbd driver delay is taken out
		/fabos/libexec/bc &
		/fabos/libexec/ipc &
		sleep 2
#		/fabos/libexec/ppcimux &
		sleep 2
#		/fabos/libexec/hifn &
		/fabos/libexec/ibpd &
		sleep 2
		/fabos/libexec/diag &
		sleep 2
		/fabos/libexec/hasmd -p ${SWBD##SWBD} -d ${MODE} &
		/fabos/libexec/traced -p ${SWBD##SWBD} &

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;

	'68')
		# we launch raslogapd first to display RAS-LOG
		/fabos/libexec/raslogapd &
		echo "start ipadmd on 68"
		# bind two ethernet if to bond0
		ifconfig bond0 up
		ifenslave bond0 eth0
		ifenslave bond0 eth3
		/fabos/libexec/ipadmd ${SWBD##SWBD}
		/fabos/libexec/bswd -p ${SWBD##SWBD} &
		sleep 4
		/fabos/libexec/sb &
		sleep 3
		/fabos/sbin/pcscd
		sleep 3
		/fabos/libexec/taped &
		sleep 4
		/fabos/libexec/rekeyd &
		sleep 2
		/fabos/libexec/cvlcd &
		sleep 3
		/fabos/libexec/bc -p${SWBD##SWBD} &
		/fabos/libexec/lpc &
		sleep 2
		/fabos/libexec/spcd &
		sleep 2
		/fabos/libexec/bdiagd -p${SWBD##SWBD} &
		sleep 2

		# we launch traced
		/fabos/libexec/traced -p ${SWBD##SWBD} &

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;

	'36'| '50' | '55')
		# We launch raslogd first to display RAS_LOG
		/fabos/libexec/raslogapd &
		if  [ "${SWBD##SWBD}" = "55" ] ; then
			echo "start ipadmd on 55"
			# bind two ethernet if to bond0
			ifconfig bond0 up
			ifenslave bond0 eth0
			ifenslave bond0 eth1
			/fabos/libexec/ipadmd ${SWBD##SWBD}
			/fabos/libexec/vnet1250 &
		fi

		/fabos/libexec/bswd -p ${SWBD##SWBD} &
		sleep 4
		/fabos/libexec/sb &
		# modify this once mbd driver delay is taken out
		/fabos/libexec/bc -d 2 &
		/fabos/libexec/hifn &
		sleep 1
		/fabos/libexec/mpc &

		/fabos/libexec/hasmd -p ${SWBD##SWBD} -d ${MODE} &
		/fabos/libexec/traced -p ${SWBD##SWBD} &
		sleep 2
		/fabos/sbin/ftraced 0 &
		/fabos/sbin/ftraced 1 &
		sleep 4

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;
	'78' | '84' | '88')
		# we launch raslogd first to display RAS-LOG
		/fabos/libexec/raslogapd &		
		/fabos/libexec/bswd -p ${SWBD##SWBD} &
		sleep 4
		/fabos/libexec/sb &
		# modify this once mbd driver delay is taken out
		sleep 2
		/fabos/libexec/bc -p ${SWBD##SWBD} &
		# As of now comment for Leda
		#/fabos/libexec/mpc &
		sleep 2
		/fabos/libexec/hasmd -p ${SWBD##SWBD} -d ${MODE} &
		sleep 2

		# we launch traced
		/fabos/libexec/traced -p ${SWBD##SWBD} &

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;

	'81' | '85')
		/fabos/libexec/hasmd -p ${SWBD##SWBD} -d ${MODE} &
		sleep 4
		# we launch raslogd first to display RAS-LOG
		if [ "${SWBD##SWBD}" = "85" ] ; then
			/fabos/libexec/raslogd &
		else
			/fabos/libexec/raslogapd &
		fi
		sleep 4
		/fabos/libexec/sb &
		# bc setup eth2 thus ensure tftpboot not accessble before that
		# modify this once mbd driver delay is taken out
		/fabos/libexec/bc -p ${SWBD##SWBD} -d 2 &
		sleep 1 
		/fabos/libexec/mpc &

		# we launch traced
		/fabos/libexec/traced -p ${SWBD##SWBD} &
		sleep 2

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;

	'89')
		/fabos/libexec/hasmd -p ${SWBD##SWBD} -d ${MODE} &
		sleep 4
		# we launch raslogd first to display RAS-LOG
		/fabos/libexec/raslogd &
		sleep 4
		/fabos/libexec/sb &
		# bc setup eth2 thus ensure tftpboot not accessble before that
		# modify this once mbd driver delay is taken out
		/fabos/libexec/bc -p ${SWBD##SWBD} -d 2 &
		sleep 1 
		/fabos/libexec/mpc &

		# we launch traced
		/fabos/libexec/traced -p ${SWBD##SWBD} &
		sleep 2

		# Launch rmd here
		#/fabos/libexec/rmd &
		#sleep 2
		;;
	esac

	return 0
}
#
#
# this function will load the "panic dump" related modules
# These modules need to be loaded first and we have to call
# "pdcheck.sh" to check for a panic dump from a previous reboot
# if found we will save them in CF (it is all done in pdcheck.sh)
#
pd_setup() {	
	modutil load raslog-module
	modutil	load basic-module
	modutil load trace-module platform=${SWBD##SWBD}
	mk_device raslog raslog 0
	mk_device trace trace 0 trace1 1
	/fabos/sbin/tracecfg -w ${SWBD##SWBD} > /var/log/tracecfg.log
	modutil load xprt-module
	modutil load isc-module
	modutil load tp-module
#	modutil load ki-module
	modutil load hasm-module
	modutil load fss-module
	modutil load bswd-module

	# common devices need to be make before pdcheck
	#mk_device raslog raslog 0
	#mk_device trace trace 0 trace1 1
	#/fabos/sbin/tracecfg -w ${SWBD##SWBD} > /var/log/tracecfg.log
	mk_device isc_mgmt iscm 0
	mk_device isc_data iscd 0
	mk_device tp tp 0
#	mk_device ki ki 0
	mk_device ham ham 0
	mk_device fss_mgmt fssm 0
	mk_device fss_data fssd 0
}

#
# load Brocade ECC module on platforms for which we have a module for that processor.
# for now just freescale 8548 based platforms.
#
load_brcdeccmod() {
	case ${SWBD##SWBD} in
	'55' | '68' | '89' )
		insmod /lib/modules/default/kernel/drivers/brcdecc/brcdeccmod.ko
		;;
	esac
}

# Code to set the limit on the size of the corefile
# 
setulimit(){
        set -- $(/bin/df -k ${1:-/core_files})
        typeset -i size=${11}
        if ((size > 4000000))
        then    ulimit -c unlimited
        elif ((size < 0))
        then    ulimit -c 0
        else    ulimit -c $((size*7/10))
        fi
}

#
# main
#
case "$1" in

'start')

    ##################################################################
    echo "Starting Fabric Blade OS Services..."

    # workaround for telnet. remove later on
    if [ ! -x /fabos/etc/swInst ] ; then
        echo '#!/bin/sh' > /fabos/etc/swInst
        echo 'echo sw0' >> /fabos/etc/swInst
        chmod +x /fabos/etc/swInst
    fi
    if [ ! -x /fabos/bin/secModeGet ]; then
        echo '#!/bin/sh' > /fabos/bin/secModeGet
        echo 'echo 0' >> /fabos/bin/secModeGet
        chmod +x /fabos/bin/secModeGet
    fi

    if [ -f /etc/preempt_fos ];
    then

    	echo "Entering BP sys fpga jtag configuration mode"
    	rm -rf /etc/preempt_fos

    	case ${SWBD##SWBD} in
    	'85')

    	    echo "Configure eth0 interface for Spike"
    	    ifconfig eth0 127.1.10.1 up
    	    ;;

    	'89')

            BPIP=`cat /etc/bp_ip_addr 2> ${NULL}`
            rm -rf /etc/bp_ip_addr
            echo "Configure eth1 interface for Striker $BPIP"
            ifconfig eth1 ${BPIP} up
            ;;
    	'68')

            BPIP=`cat /etc/bp_ip_addr 2> ${NULL}`

	    # For Mace and Lance we should be able to connect to BP incase sysfpga upgrade is not successful.
            touch /etc/preempt_fos

            echo "Configuring eth2 interface"
            ifconfig eth2 ${BPIP} netmask 255.255.255.0 up 
	    sleep 2
	    /sbin/ethtool -s eth2 autoneg off speed 100 duplex full
            ;;
    	esac

    	exit 0

    fi

    if $CHECK fabos; then
	# Set the limit on the core file size
	setulimit

	# load basic-module before any other modules are loaded
	# (pd modules)
	pd_setup

	# load_brcdeccmod
	# if ECC module was loaded, start up the ECC reporting daemon
	if [ -f /proc/driver/ecc/model ]; then
		/fabos/libexec/eccd
	fi

	# load platform modules
	swbd_modules

	# Make device nodes
	swbd_devices

	# check for panic dump and save if exists
	/fabos/bin/pdcheck.sh
	# Upload any PD_MISC parameters in kernel that can be obtained
	# before the full fledged fabos
	/fabos/bin/pdul_misc_pre.sh
	
	# launch user space applications/daemons
	swbd_daemons

	# Let us log errors that are saved earlier and log them
	/fabos/bin/pdcheck_errs.sh
	# Upload any PD_MISC parameters in kernel that can be obtained
	# _AFTER_ the fabos is loaded full and running
	/fabos/bin/pdul_misc_post.sh &

	# set up flow control for the console
	# /fabos/sbin/shellFlowControl.sh setup > /dev/null
    fi

    $ECHO "."
    ;;

'stop')
    $ECHO "Stopping Fabric OS services..."

	# First, we should shutdown SWD, otherwise it will blow the whistle
	# for every daemon's death
	# /sbin/swdadmin shutdown > ${NULL} 2>&1

	# stop the heartbeating
	# /fabos/sbin/hashutdown > ${NULL} 2>&1

        #stop uptime clock for both switches
    	#/fabos/bin/switchuptime stopall > ${NULL} 2>&1

	if  [ "${SWBD##SWBD}" = "85" ] || [ "${SWBD##SWBD}" = "89" ] ; then
		DAEMONS='raslogd traced bdiagd'
	else
		DAEMONS='raslogapd traced bdiagd'
	fi
	${KILLALL} -TERM $DAEMONS > ${NULL} 2>&1
	${KILLALL} $DAEMONS > ${NULL} 2>&1

    $ECHO "Done."
    ;;

*)
    echo "Usage: $0 {start | stop | fdet_error <sno>}"
    ;;

esac
