#!/bin/ksh
#*******************************************************************************
#
# NAME:		hot_add
# SUMMARY:	%description%
# COMPONENT:	solsysd
# VERSION:	rm6_2
# UPDATE DATE:	%date_modified: Thu May 04 11:01:00 2000 %
# PROGRAMMER:	%created_by:    awilner %
# 
#		Copyright 1998 by Symbios Logic Inc.
#
# DESCRIPTION:
# hot_add carries out those operations needed for SYMplicity storage manager
# to be able to see a new array module.  
#
# NOTES:
#
# REFERENCE:
#
# CODING STANDARD WAIVERS:
#
#*******************************************************************************

#*******************************************************************************
# PROCEDURE:	MutexAcquire
# SUMMARY:	get exclusive ownership of mutex file
#
# DESCRIPTION:
# MutexAcquire will try repeatedly to acquire a "mutex file" - success means
# the thread has been admitted to a "critical region"
#
# SYNTAX:
# MutexAcquire <mutex file>
# <mutex file> - name of the mutex guarding the critical region
#
# NOTES:
# MutexAcquire and the program it calls, rm_mutex_acquire, rely on O_CREAT
# semantics
#
# RETURNS:
# Nothing

MutexAcquire()
{
	while true
	do
		$RM_HOME/bin/rm_mutex_acquire $1
		if [ $? = 0 ]
		then
			break;
		fi
		sleep 1
	done;
}

#*******************************************************************************
# PROCEDURE:	MutexRelease
# SUMMARY:	relenquish mutex file ownership
#
# DESCRIPTION:
# MutexRelease releases mutex file ownership, so another thread can enter the
# critical region
#
# SYNTAX:
# MutexRelease <mutex file>
# <mutex file> - name of the mutex guarding the critical region
#
# NOTES:
#
# RETURNS:
# Nothing

MutexRelease()
{
	/bin/rm $1;
}

#*******************************************************************************
# PROCEDURE:	hot_add (main)
# SUMMARY:	Make RAID Modules visible
#
# DESCRIPTION:
# hot_add calls "drvconfig" for the Native SCSI drivers defined in rmparams.
# It generates a new rdriver.conf, uses rdacconfig to notify the RDAC driver of the
# new module(s). It then calls "rdac_disks" which is the RDAC replacement
# script for "disks".
#
# SYNTAX:
# hot_add
#
# NOTES:
#
# RETURNS:
# 0     - success
# non-0 - failure (currently not returned)


# H O T A D D   M A I N   P R O C E D U R E
#
RMPARAMS_FILE=/etc/raid/rmparams
RM_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmHomeDirectory | cut -d= -f2`
RM_BOOT_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmBootHomeDirectory | cut -d= -f2`
NATIVE_DRIVERS=`grep -v "^#" $RMPARAMS_FILE | grep Rdac_NativeScsiDrivers`

RDAC_DR_TO=`grep -v "^#" $RMPARAMS_FILE | grep "Rdac_DR_Timeout" | cut -d= -f2`
if [ -z "${RDAC_DR_TO}" ]
then
	RDAC_DR_TO=15
fi

RMDEVROOT=`grep -v "^#" $RMPARAMS_FILE | grep "System_AltDevDirRoot" | cut -d= -f2 | awk -F/ '{ ORS = "/"; for (i = NF; i > 1; --i) print $i; ORS = "\n"; print $1 }' | cut -d"/" -f3-32 |  awk -F/ '{ ORS = "/"; for (i = NF; i > 1; --i) print $i; ORS = "\n"; print $1 }'`
RDACCONFIG=$RM_BOOT_HOME/rdacconfig

if grep -v "^#" $RMPARAMS_FILE | grep "Rdac_SupportDisabled.*=.*TRUE" >/dev/null 2>&1
then
	RDAC_DISABLED=TRUE
fi

ISALIST_EXIST=`which isalist | grep no`
if [ -z "${ISALIST_EXIST}" ]
then
        ISALIST="isalist"
else
        ISALIST="echo not"
fi

_INIT_RECONFIG=YES; export _INIT_RECONFIG

MutexAcquire /tmp/mutex.hot_add	# Begin critical region

for DRIVER in 2 3 4 5 6 7 8 9
do
	NATIVE=`echo $NATIVE_DRIVERS | cut -d: -f$DRIVER`
	if [ -z "${NATIVE}" ]
	then
		break
	fi

	if [ "${NATIVE}" = "ssd" ]
	then
		/etc/raid/bin/lad  >/dev/null 2>/dev/null
		drvconfig -i $NATIVE
	else
		drvconfig -i $NATIVE
	fi
done

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd off
fi

/etc/raid/bin/rdac_disks -r $RMDEVROOT

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd on
fi

$RM_BOOT_HOME/symconf >/kernel/drv/rdriver.conf 
sc_err=$?
if [ $sc_err -ne 0 ]
then
	if [ $sc_err -eq 10 ]
	then
		echo "Array Hot Add error: /kernel/drv/rdnexus.conf has run out of pseudo-busses."
		echo "Some disk array controllers may not be accessible until more entries are"
		echo "added to rdnexus.conf."
	else
		echo "Array Hot Add error: Unable to build the RDAC Driver rdriver.conf file"
		MutexRelease /tmp/mutex.hot_add	# End of critical region
		exit 1
	fi
fi

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd off
fi

drvconfig -i rdriver

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd on
fi





# make sure rdriver.conf attributes are as they should be
chmod 644 /kernel/drv/rdriver.conf
chown root /kernel/drv/rdriver.conf
chgrp sys  /kernel/drv/rdriver.conf

if [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	cat /kernel/drv/rdriver.conf | grep mname >/tmp/hot_add.$$

	cat /tmp/hot_add.$$ |
	while read MODULE_LUN_INFO
	do
		MODULE_NAME=`echo $MODULE_LUN_INFO | cut -d= -f7 | cut -d\" -f2`
		MODULE_NUMBER=`echo $MODULE_LUN_INFO | cut -d= -f3 | cut -d" " -f1`
		NEXUS_BUS=`echo $MODULE_LUN_INFO | cut -d= -f8 | cut -d" " -f1`
		TARGET=`echo $MODULE_LUN_INFO | cut -d= -f5 | cut -d" " -f1`
		LUN=`echo $MODULE_LUN_INFO | cut -d= -f4 | cut -d" " -f1`
     		C_PATH=`echo $MODULE_LUN_INFO | cut -d= -f9 | cut -d" " -f1 | grep A`
		if [ -n "${C_PATH}"  ]
		then
			$RDACCONFIG $RMDEVROOT/dev/rdsk/rdac "$MODULE_NAME" $MODULE_NUMBER $NEXUS_BUS $TARGET $LUN 0 >/dev/null $2>$1
		else
			$RDACCONFIG $RMDEVROOT/dev/rdsk/rdac "$MODULE_NAME" $MODULE_NUMBER $NEXUS_BUS $TARGET $LUN 1  >/dev/null $2>$1
		fi
	done
fi

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd off
fi

drvconfig -i rdriver

RDAC_LOADED=`modinfo | grep rdriver`
if [ -n "${RDAC_LOADED}" ] && [ -c $RMDEVROOT/dev/rdsk/rdac ]
then
	/etc/raid/bin/hide_sd on
fi



##########################
# start the RDAC daemons #
##########################

if [ -z "${RDAC_DISABLED}" ] # if RDAC *not* disabled ...
then
        nbr_dmns=`ps -e | grep rdaemon | grep -v grep | wc -l`
        if test $nbr_dmns -ne 2
	then
		modnum=`modinfo | grep rdriver | cut -c1-3`
		majnum=`modinfo | grep rdriver | cut -c20-23`
		if [ -n "`$ISALIST | grep sparcv9`" ]
		then
			priocntl -e -c RT $RM_BOOT_HOME/sparcv9/rdaemon $modnum $majnum $RDAC_DR_TO &
		else
			priocntl -e -c RT $RM_BOOT_HOME/rdaemon $modnum $majnum $RDAC_DR_TO &
		fi
		echo "RDAC daemons initiated" 
	fi
fi

/etc/raid/bin/rdac_disks

/etc/raid/bin/set_drivers

MutexRelease /tmp/mutex.hot_add	# End of critical region

exit 0
