#***********************************************************************#
#                                                                       #
#                               NOTICE                                  #
#                                                                       #
#                  COPYRIGHT 2005 QLOGIC CORPORATION               	#
#                        ALL RIGHTS RESERVED                            #
#                                                                       #
#  This computer program is CONFIDENTIAL  and contains TRADE SECRETS of #
#  QLOGIC CORPORATION.  The  receipt or possession of this program does #
#  not convey any rights to reproduce or disclose  its contents,  or to #
#  manufacture, use, or sell anything that it may describe, in whole or #
#  in part, without the specific written consent of QLOGIC CORPORATION. #
#  Any reproduction of this program without the express written consent #
#  of QLOGIC CORPORATION  is a violation of the copyright laws and  may #
#  subject you to civil liability and criminal prosecution.             #
#                                                                       #
#***********************************************************************#

#*************************************************#
#               set_driver_param                  #
#	       QLogic Corporation		  #
#              February 23, 2005                  #
#                                                 #
#            Part of Ease of installation.	  #
#*************************************************#
#global array
if [ $QL_FC_INSTALLATION = 1 ]; then
	QL_DRIVER_PARAMS=( "ql2xmaxqdepth" "ql2xfailover" "qlport_down_retry" "ql2xlogintimeout" )

	ql2xmaxqdepthMIN=1
	if [ "$KERNEL_MAJ_MIN" = "2.4" ]; then
        	ql2xmaxqdepthMAX=255
	else # 2.6
	QL_DRIVER_PARAMS=( "${QL_DRIVER_PARAMS[@]}" "ql2xdoinitscan" )
        	ql2xmaxqdepthMAX=65535
	fi

	qlport_down_retryMIN=-1
	ql2xlogintimeoutMIN=-1
	ql2xdoinitscanMIN=0
	ql2xdoinitscanMAX=1
	ql2xfailoverMIN=0
	ql2xfailoverMAX=1
fi

if [ $QL_iSCSI_INSTALLATION = 1 ]; then
	QL_DRIVER_PARAMS=( "ql4xmaxqdepth" "ql4xdiscoverywait" "ql4xcmdretrycount" "extended_error_logging" )

	ql4xdiscoverywaitMIN=-1
	ql4xcmdretrycountMIN=-1
	ql4xmaxqdepthMIN=1
	ql4xmaxqdepthMAX=255
	extended_error_loggingMIN=0
	extended_error_loggingMAX=1
fi

# --------------------------------------------------------- #
# display_options_help ()	                            #
# Prints the help message.			            #
# Parameter: None 		                            #
# Returns: None		 				    #
# --------------------------------------------------------- #
function display_options_help() {
	echo ""
	echo "Following command line parameters for the driver can be set:"
	echo ""
	COUNT=1
	for PARAM in ${QL_DRIVER_PARAMS[@]}
	do
	echo "$COUNT. $PARAM"
	(( COUNT += 1 ))
	done

	echo ""
	if [ $QL_FC_INSTALLATION = 1 ]; then
		echo "For example: "
		echo "./qlinstall -o ql2xmaxqdepth=32 qlport_down_retry=20 "
		echo ""
		
		echo "For detailed description of each parameter please refer to driver README:"
		if [ "$KERNEL_MAJ_MIN" = "2.6" ]; then
			echo "$QL_DRIVER_SRC/qla2xxx-`echo $QL_FC_DRIVER_VERSION | sed "s/v//"`/README.qla2xxx" 
		else
			echo " $QL_DRIVER_SRC/README.qla2x00" 
		fi
	else
		echo "Use '-ignore' option to not apply the parameters immediately."
		echo "This will prevent driver unload & load after setting the driver parameters."
		echo "Note: This will be useful when machine is remote booted and we want to "
		echo "change driver parameters. Modified parameters will be in effect at next reboot"
		echo ""
		echo "For example: "
		echo "./qlinstall -o ql4xmaxqdepth=32 ql4xdiscoverywait=16"
		echo "./qlinstall -o -ignore ql4xmaxqdepth=32 ql4xdiscoverywait=16"
		echo ""
	fi
}


# --------------------------------------------------------- #
# set_options ()	                                    #
# Sets different options in the /etc/modules.conf	    #
# of /etc/modprobe.conf/.local				    #
# This will set the same option for all the modules	    #
# like qla2200 and qla2300				    #
# Parameter:      		                            #
#	$*	(Param1=Value1) (ParamN=ValueN)....	    #	
# Returns: QL_SUCCESS on success, QL_FAIL on failure.       #
# --------------------------------------------------------- #
function set_options() {

	ret_val=$QL_FAIL

	if [ $QL_FC_INSTALLATION = 1 ]; then	
		QL_FAILOVER="ql2xfailover"
	fi
	
	set_modules_for_options
	# Load each of the module
	for ISP in "${QL_HBA_MODULES[@]}" 
	do
		#check if the line "options qla****" is there
		# if line there then store it in a variable
		OPTION_LINE=`grep "^options[[:space:]]\+qla${ISP}.*" $QL_MOD_CONF`
		# if not then we need to add all the input param=value 
		if [ "$OPTION_LINE" = "" ]; then #line not ther
			OPTION_LINE="options qla$ISP"
		fi	
		#Parse the input
		ALL_IN_OPTIONS=( $* )
		for OPTION in ${ALL_IN_OPTIONS[@]}
		do
			PARAM=`echo $OPTION | cut -d = -f 1`
			VALUE=`echo $OPTION | cut -d = -f 2`
			#echo "PARAM = $PARAM"
			#echo "VALUE = $VLAUE"
			# is this a valid option
			echo ${QL_DRIVER_PARAMS[@]} | grep -w "$PARAM" >& /dev/null
			if [ $? != 0 ]; then
				echo "Driver parameter $PARAM not supported"
				echo "Cannot set this parameter..."
			else	
#				echo "Value: $VALUE"
#				echo "MIN: $[${PARAM}MIN]"
#				echo "MAXl: $[${PARAM}MAX]"
				# check if value is numeric if not then continue to next
				echo $VALUE | grep -w "^[0-9]\+$" >& /dev/null
				if [ $? != 0 ]; then
					echo "The driver parameter should be numeric"
					echo "Cannot set value $VALUE to $PARAM"
					continue
				fi
			
				# is the value in range
				if [ "$[${PARAM}MIN]" != "-1" ]; then
					if [ "$VALUE" -lt "$[${PARAM}MIN]" ] ||
					   [ "$VALUE" -gt "$[${PARAM}MAX]" ]; then
						echo "For driver parameter: $PARAM the value should be in range $[${PARAM}MIN] to $[${PARAM}MAX]"
						echo "Cannot set this parameter..."
						continue
			
					fi	
				fi

				# is the option already set
				echo $OPTION_LINE | grep "$PARAM=.*" >& /dev/null
				if [ $? = 0 ]; then #param already set, set to new value
					OPTION_LINE=`echo $OPTION_LINE | sed "s/${PARAM}=[[:alnum:]]\+/$OPTION/"`
					ret_val=$QL_SUCCESS
				else
					#just add it to the options line
					OPTION_LINE="$OPTION_LINE $OPTION "
					#store it in our delete file as well
					if [ $QL_FC_INSTALLATION = 1 ]; then
						echo $OPTION >> $QL_FC_BACKUP_DIR/qla${ISP}_options_delete
					fi

					if [ $QL_iSCSI_INSTALLATION = 1 ]; then
						echo $OPTION >> $QL_iSCSI_BACKUP_DIR/qla${ISP}_options_delete
					fi

					ret_val=$QL_SUCCESS
				fi
				if [ $QL_FC_INSTALLATION = 1 ]; then
					# check if the param was ql2xfailover if yes then 
					# disable persistent binding
					if [ "$PARAM" = "$QL_FAILOVER" ]; then
						if [ "$VALUE" = "0" ]; then
							#Enable persistent bind	
							VALUE=1
						else # value is 1
							#disable persistent bind
							VALUE=0
						fi
						PARAM="ConfigRequired"
						OPTION="${PARAM}=${VALUE}"
						echo $OPTION_LINE | grep "$PARAM=.*" >& /dev/null
						if [ $? = 0 ]; then #param already set, set to new value
							OPTION_LINE=`echo $OPTION_LINE | sed "s/${PARAM}=[[:alnum:]]\+/$OPTION/"`
							ret_val=$QL_SUCCESS
						else
							#just add it to the options line
							OPTION_LINE="$OPTION_LINE $OPTION "
							ret_val=$QL_SUCCESS
						fi
					fi
				fi   
			fi
		done #done for all the input parameters
		# replace the line in original file
		grep "^options[[:space:]]\+qla${ISP}.*" $QL_MOD_CONF >& /dev/null
		if [ $? = $QL_SUCCESS ]; then
			cat "$QL_MOD_CONF" | sed "s/^options[[:space:]]\+qla${ISP}.*/$OPTION_LINE/" > $QL_TMP_MOD_FILE
			if [ -f $QL_TOM_MOD_FILE ]; then
				mv -f $QL_TMP_MOD_FILE $QL_MOD_CONF
			fi
		else
			echo "${OPTION_LINE}" >> $QL_MOD_CONF
		fi
	done #done for all the modules
	return $ret_val
}

# --------------------------------------------------------- #
# restore_options ()	                                    #
# Restore the original options qla2300/qla2xxx line         #
#							    #
# Parameter: None     		                            #
# Returns: None                                             #
# --------------------------------------------------------- #
function restore_options() {
	set_modules_for_options
	for ISP in "${QL_HBA_MODULES[@]}"  #need to do same stuff for qla2200 & qla2300
	do
		# remove the current options qla2300..line
		cat "$QL_MOD_CONF" | sed "s/^options[[:space:]]\+qla${ISP}.*//" > $QL_TMP_MOD_FILE
		mv -f $QL_TMP_MOD_FILE $QL_MOD_CONF
	done
	# now move in the original options
	if [ -f $QL_FC_BACKUP_DIR/ql_org_options ]; then
		cat $QL_FC_BACKUP_DIR/ql_org_options >> $QL_MOD_CONF
	fi

	if [ -f $QL_iSCSI_BACKUP_DIR/ql_org_options ]; then
		cat $QL_iSCSI_BACKUP_DIR/ql_org_options >> $QL_MOD_CONF
	fi
}

# --------------------------------------------------------- #
# backup_options ()	                                    #
# Backup the original options qla2300/qla2xxx line          #
#							    #
# Parameter: None     		                            #
# Returns: None                                             #
# --------------------------------------------------------- #
function backup_options() {
	set_modules_for_options
	for ISP in "${QL_HBA_MODULES[@]}"  #need to do same stuff for qla2200 & qla2300
	do
		if [ QL_FC_INSTALLATION = 1 ]; then
			grep "^options[[:space:]]\+qla${ISP}.*" $QL_MOD_CONF >> $QL_FC_BACKUP_DIR/ql_org_options
		fi

		if [ QL_iSCSI_INSTALLATION = 1 ]; then
			grep "^options[[:space:]]\+qla${ISP}.*" $QL_MOD_CONF >> $QL_iSCSI_BACKUP_DIR/ql_org_options
		fi
	done
}

# --------------------------------------------------------- #
# backup_conf ()	                                    #
# Backup the /etc/qla2***.conf file before installation     #
#							    #
# Parameter: None     		                            #
# Returns: None                                             #
# --------------------------------------------------------- #
function backup_conf() {
	set_modules_for_options
	for ISP in "${QL_HBA_MODULES[@]}"  #need to do same stuff for qla2200 & qla2300
	do
		FILE=/etc/qla${ISP}.conf
		if [ -f $FILE ]; then
			cp -f $FILE $QL_FC_BACKUP_DIR/qla${ISP}.conf 
		fi
	done
}

# --------------------------------------------------------- #
# restore_conf ()	                                    #
# Restore the /etc/qla2***.conf file after uninstallation   #
#							    #
# Parameter: None     		                            #
# Returns: None                                             #
# --------------------------------------------------------- #
function restore_conf() {
	set_modules_for_options
	for ISP in "${QL_HBA_MODULES[@]}"  #need to do same stuff for qla2200 & qla2300
	do
		FILE=/etc/qla${ISP}.conf
		BACKUP=$QL_FC_BACKUP_DIR/qla${ISP}.conf 

		if [ -f $BACKUP ]; then
			mv -f $BACKUP $FILE 		
		else 
			#Remove the file if there is no backup file
			if [ -f $FILE ]; then
				rm -rf $FILE
			fi
		fi
	done
}
