#!/bin/sh
#
# Copyright C 2002-2011 by Isilon Systems, LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# PROVIDE: isi_pre_network
# REQUIRE: isi_verify_hardware
# BEFORE: NETWORKING

#
# kick off the if_* drivers
#

# Do any net.bcX_init sysctls here.
#sysctl net.bc0_init.auto_flow_control=0

# The first ioctl kicks off initialization.  ifconfig without
# parameters causes ioctls on all interfaces.  This allows
# preceding init sysctls to function.
/sbin/ifconfig > /dev/null  2>&1

# For bc* and em*, explicitly ifconfig them UP. This is required
# for the unconfigured-node BPF listeners and broadcasters in
# isi_preconfig_transport/isi_join_d. See bug 49537 and and its
# related bugs for all the details. The code further down handles
# the same thing for Infiniband.
for i in `/sbin/ifconfig -l`; do
	case $i in
	bc[0-9]* | em[0-9]* | bxe[0-9]* | cxgb[0-9]* | ix[0-9]*)
		/sbin/ifconfig $i up
		;;
	esac
done

# if bc0 exists, sleep a little to allow link to come up.
# Warning: figuring out if bc0 exists is not as easy as it looks.
# "ifconfig bc0" can cause the if_bc.ko to load!  Also kldstat | grep
# bc0 is unwise as the driver could be compiled into the kernel.)
x=0
/sbin/ifconfig | /usr/bin/grep bc0 > /dev/null  2>&1
if [ $? = 0 ]; then
	while [ $x -lt 10 ]; do
		if [ `/sbin/ifconfig bc0 | /usr/bin/grep -c active` = 0 ]; then
			sleep 1
		else
			x=10
		fi
		x=`expr $x + 1`
	done
fi

#
# Kick off the Infiniband driver and daemons.
#
# Figure out whether we're mlx4 or mthca.  Only support two right now.
sysctl -N dev.mthca.fw_info 2>&1 >> /dev/null
if [ $? -eq 0 ]; then
    is_mthca=1
else
    is_mthca=0
fi

# IPoIB retries its multicast registrations every 16 seconds.  We'd better
# wait longer than that before giving up on link.
ib_link_wait=18

# Make a list of the IPoIB interfaces and count them.
ib_interfaces=
ib_port_count=0
for i in `/sbin/ifconfig -l`; do
	case $i in
	ib[0-9]*)
		ib_interfaces="$ib_interfaces $i"
		ib_port_count=$(($ib_port_count + 1))
		;;
	esac
done

if [ -n "$ib_interfaces" ]; then
	# Force older cards to SDR
	/usr/bin/isi_hwtools/isi_ib_fw -d mthca0 -byte_mode -nofs -qq query | awk -f /usr/bin/isi_ddr_mawk
	case $? in
	1)
		# HW supports DDR
		;;
	2)
		# HW prefers SDR
		# 1=SDR, 2=DDR, 3=AUTO
		ifconfig ib1 down
		ifconfig ib0 down
		if [ $is_mthca -eq 1 ]; then
		    sysctl dev.mthca.port2_enabled_speed=1
		    sysctl dev.mthca.port1_enabled_speed=1
		fi
		ifconfig ib1 up
		ifconfig ib0 up
		;;
	*)
		# HW rev undefined
		;;
	esac

	# Start an opensm instance on each IB port.
	echo -n "Starting opensm on all ports:"
	i=1
	while [ $i -le $ib_port_count ]; do
		echo -n " $i"
		isi_opensm $i start
		i=$(($i + 1))
	done
	echo "."

# For unconfigured nodes (can we sanity-check that), we need
# to temporarily give them any IP address to get them to start
# working.  Not only do we need to do this for our own sake, but
# it also has to happen before the phy reset.
	t=0
	while [ $t -lt $ib_port_count ]; do
		ifconfig ib$t 0.0.0.0 netmask 0xffffffff -arp
		ifconfig ib$t delete
		ifconfig ib$t arp
		t=$(($t + 1))
	done

#       retrigger IB lid assignment
#       this is a workaround pending resolution of a startup
#       race condition
#
	t=1
	while [ $t -le $ib_port_count ]; do
		if [ $is_mthca -eq 1 ]; then
		    sysctl dev.mthca.ports.set_curr_port_num=$t
		    sysctl dev.mthca.ports.phy_reset=1
		fi
		if sysctl -q dev.mlx4_core > /dev/null; then
			sysctl dev.mlx4_core.ports.set_curr_port_num=$t
			sysctl dev.mlx4_core.ports.phy_reset=1
		fi
		t=$(($t + 1))
	done

	# Wait awhile for each IB port to get link.
#	echo -n "Waiting for IB link active:"
#	ib_need_link="$ib_interfaces"
#	t=1
#	while [ $t -le $ib_link_wait ]; do
#		ib_need_link_new=
#		for i in $ib_need_link; do
#			if [ `/sbin/ifconfig $i | \
#			      /usr/bin/grep -c active` -ne 0 ]; then
#				echo -n " $i"
#			else
#				ib_need_link_new="$ib_need_link_new $i"
#			fi
#		done
#		if [ -z "$ib_need_link_new" ]; then
#			break;
#		fi
#		ib_need_link="$ib_need_link_new"
#		sleep 1
#		t=$(($t + 1))
#	done
	echo "."
fi

case ${isi_clear_webui_enable} in
[Yy][Ee][Ss])
    echo -n "Clearing web application sessions:"
	(cd /usr/local/www/webkit/Sessions && rm -rf *.ses)
	echo '.'
	;;
esac
