#!/bin/sh
# system init for ofed layer

sysfs=/sys
node_id=`sed -e 's/^.*://' -e 's/,.*$//' $sysfs/class/micscif/scif/nodes`

# scif, ccl-direct, kmv mods:
start_scif=ibscif\ ib_uverbs
start_drv=ibp_client\ ibp_mlx5\ ibp_mlx4
start_km=ibp_sa_client\ ibp_cm_client
start_core=ib_qib\ rdma_ucm

stop_core=rdma_ucm\ rdma_cm\ iw_cm\ ib_addr\ ib_qib
stop_km=ibp_cm_client\ ibp_sa_client
stop_drv=ibp_mlx5\ ibp_mlx4\ ibp_client
stop_scif=ibscif\ ib_uverbs\ ib_core

start_order=$start_scif\ $start_drv\ $start_km\ $start_core
stop_order=$stop_core\ $stop_km\ $stop_drv\ $stop_scif

let err=0

case "$1" in
  start)
        echo -n "    Starting ofed layer ... "

	depmod

	for i in $start_order; do
		modprobe $i || let err++
	done

	# setup the special address "mic0:ib" for supporting RDMA CM over
	# the emulated IB interface (ibscif). this is used for address
	# resolution only.
	ip address add 192.0.2.`expr 100 + $node_id`/24 dev mic0 label mic0:ib

        echo "Done."
        ;;
  stop)
        echo -n "    Stopping ofed layer ... "

	ip address del 192.0.2.`expr 100 + $node_id`/24 dev mic0

	# try to ipoib, don't fail:
	rmmod ib_ipoib &> /dev/null

	for i in $stop_order; do
		if [ `grep -c ^$i\  /proc/modules` -ne 0 ] ; then
			rmmod $i || let err++
		fi
	done

        echo "Done."
        ;;
  restart)
        $0 stop		&&
        $0 start	||
	exit 1
        ;;
  status)
        for i in $start_order; do
		grep -w $i /proc/modules || let err++
	done
        ;;
esac

exit $err
