#!/bin/ksh -p
#*******************************************************************************
#
# NAME:		add_disk
# SUMMARY:	%description%
# COMPONENT:	solsysd
# VERSION:	rm6_14
# UPDATE DATE:	%date_modified: Thu Jun 28 12:10:49 2001 %
# PROGRAMMER:	%created_by:    jbeadles %
# 
#		Copyright 1997 by Symbios Logic Inc.
#
# DESCRIPTION:
# add_disk carries out those operations needed for SYMplicity storage manager
# to be able to see a logical unit (or units).  The LUNs may have not been
# seen previously for various reasons, e.g., the LUN may have just been
# created, or a controller may have just been brought online.
#
# 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:	add_disk (main)
# SUMMARY:	Make RAID logical units visible
#
# DESCRIPTION:
# add_disk calls "drvconfig" for the Native SCSI drivers defined in rmparams.
# It then calls "disks" which may be the Solaris "disks" or the RDAC replacement
# script, to make previously unseen RAID logical units visible to SYMsm and Solaris.
#
# SYNTAX:
# add_disk
#
# NOTES:
#
# RETURNS:
# 0     - success
# non-0 - failure (currently not returned)


# A D D _ D I S K   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`
RM_NAME_FILE=`grep -v "^#" $RMPARAMS_FILE | grep System_NamefileDirPath | cut -d= -f2`

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 }'`

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

if grep -v "^#" $RMPARAMS_FILE | grep "Rdac_HotAddDisabled.*=.*PARTIAL" >/dev/null 2>&1
then
	RDAC_HOTADD_IDS=`grep -v "^#" $RMPARAMS_FILE | grep Rdac_HotAddIDs`
fi

_INIT_RECONFIG=YES; export _INIT_RECONFIG
CREATE_PATH=$1
MutexAcquire /tmp/mutex.add_disk	# Begin critical region

if [ -n "${CREATE_PATH}" ]
then
	CREATE_CNTRL=`echo $CREATE_PATH | cut -dd -f1`
	LUN=`echo $CREATE_PATH | cut -dd -f2 | cut -ds -f1`
	ID=`echo $CREATE_PATH | cut -dd -f1 | cut -dt -f2`
	
	NAME_FILE_ENTRY=`cat $RM_NAME_FILE | grep ${CREATE_CNTRL}d`
	MODULE_NAME=`echo $NAME_FILE_ENTRY | cut -d~ -f1`
	A_CNTRL=`echo $NAME_FILE_ENTRY | cut -d~ -f9 | cut -dd -f1`
	B_CNTRL=`echo $NAME_FILE_ENTRY | cut -d~ -f10 | cut -dd -f1`
	A_PATH_EXISTS=`ls -l /dev/rdsk/$A_CNTRL"d$LUN""s0" 2>/dev/null | grep rdnexus`
	B_PATH_EXISTS=`ls -l /dev/rdsk/$B_CNTRL"d$LUN""s0" 2>/dev/null | grep rdnexus`
	
	LUN_FIELD=`expr $LUN + 1`
	RDAC_ADRS_ENTRY=`cat /etc/raid/rdac_address | grep "^${MODULE_NAME}:"`
	if [ -z "${RDAC_ADRS_ENTRY}" ]
	then
		$RM_BOOT_HOME/symconf >/dev/null
		RDAC_ADRS_ENTRY=`cat /etc/raid/rdac_address | grep "^${MODULE_NAME}:"`
	fi
	ADRS_FIRST_PART=`echo $RDAC_ADRS_ENTRY | cut -d: -f1-$LUN_FIELD`
	LUN_FIELD=`expr $LUN_FIELD + 2`
	ADRS_SECOND_PART=`echo $RDAC_ADRS_ENTRY | cut -d: -f$LUN_FIELD-34`
	if [ "${CREATE_CNTRL}" = "${A_CNTRL}" ]
	then
		NEW_ADRS_ENTRY="$ADRS_FIRST_PART"":A:""$ADRS_SECOND_PART"
	else
		NEW_ADRS_ENTRY="$ADRS_FIRST_PART"":B:""$ADRS_SECOND_PART"
	fi
	cat  /etc/raid/rdac_address | grep -v "^${MODULE_NAME}:" >/tmp/rdac_address.$$
	echo $NEW_ADRS_ENTRY >>/tmp/rdac_address.$$
	diff /tmp/rdac_address.$$ /etc/raid/rdac_address >/dev/null
	if [ $? -eq 1 ]
	then
		cp /tmp/rdac_address.$$ /etc/raid/rdac_address
		rm /tmp/rdac_address.$$
		NEW_ADRS_CREATED="YES"
		$RM_BOOT_HOME/symconf >/kernel/drv/rdriver.conf
		$RM_BOOT_HOME/hot_add 
		MutexRelease /tmp/mutex.add_disk	# End of critical region
		exit 0
	fi
	rm /tmp/rdac_address.$$
	
fi



if [ -z "${NATIVE_DRIVERS}" ]
then
	drvconfig -i sd
else
	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 -y  >/dev/null 2>/dev/null
			drvconfig -i $NATIVE
		else
			drvconfig -i $NATIVE
		fi
	done
fi

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

$RM_BOOT_HOME/lad -y >/dev/null 2>/dev/null

/etc/raid/bin/rdac_disks

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

exit 0
