#/bin/bash
# name: set_parm
# copyright: (c)2003-2006 Hewlett-Packard Company
#
# description: prompts user to set driver parameters to be
#              put in /etc/hp_qla2x00.conf
#
# Chad Dupuis 01/15/03 Adding code to build a new initrd when the
#                      parameters are changed
# Chad Dupuis 03/14/04 Removed setting queue depth to 4 for
#                      failover configs
# Chad Dupuis 04/08/04 Added detection of where execution
#                      directory is
# Chad Dupuis 04/12/04 Run edit_conf whenever we make a change to
#                      to the config instead of just when we create
#                      a new initrd
# Chad Dupuis 08/30/04 Set port down retry count to 3 for failover
#                      configs; set default parameters for failover
# Chad Dupuis 04/21/05 Add setting load balancing type and exclude model
#                      parameters; default port down retry count is 30
# Chad Dupuis 11/16/05 Add autorestore settings (ql2xautorestore)
# Chad Dupuis 11/30/05 Change ql2xautorestore for QLogic failover configurations
#		       to 0x0
# Chad Dupuis 01/23/06 Change ql2xautorestore for QLogic failover configurations
#                      to 0x80
# Chad Dupuis 01/31/06 Fixed missing bracket in manual() when choosing the
#                      autorestore parameter
# Chad Dupuis 03/30/06 Added a dynamic LB flag; Improved the output of the help
#                      message
# Chad Dupusi 09/22/06 Removed ql2xexclude_model from help output

#
# Defines
#

PARAMFILE=/etc/hp_qla2x00.conf

if [ -x /opt/hp/hp_qla2x00/set_parm ]
then
 SRCDIR=/opt/hp/hp_qla2x00
else
 SRCDIR=/opt/hp/src/hp_qla2x00src
fi

# this parameter sets whether the script asks the user if they
# want to write out the changes to the initrd

MAKE_NEW_INITRD=1

#
# Functions
#

# set default parameters

get_default_parameters () {
 if [ -a $PARAMFILE ]
 then
	# read values from file

	QDEPTH=`cat $PARAMFILE | grep qdepth | awk '{print $3}'`
	PDRC=`cat $PARAMFILE | grep port_down_retry_count | awk '{print $3}'`
	LRC=`cat $PARAMFILE | grep login_retry_count | awk '{print $3}'`
	FAILOVER=`cat $PARAMFILE | grep failover | awk '{print $3}'`
	LB=`cat $PARAMFILE | grep load_balancing | awk '{print $3}'`
	AUTORESTORE=`cat $PARAMFILE | grep auto_restore | awk '{print $3}'`
 else
 	# set defaults here

	QDEPTH=16
	PDRC=3
	LRC=16
	FAILOVER=1
	LB=1
	AUTORESTORE=0x80
 fi
}

# wait for user to press enter

pause () {
 echo ""
 echo -n "---Press ENTER to continue---"
 read garbage
}

# main menu

main_menu () {
 clear
 echo ""
 echo ""
 echo "   Driver Parameters"
 echo "-----------------------"
 echo ""
 echo "1) XP/MSA/EVA Failover"
 echo "2) Single Path"
 echo "3) Set Paramters Manually"
 echo "4) Quit"
 echo ""
 echo -n "Choice: "
}

# prints out a help message

print_help () {
 echo "Usage: set_parm [-xshl <load balancing type>]"
 echo ""
 echo "-x: setup $PARAMFILE for XP/MSA/EVA failover"
 echo "-s: setup $PARAMFILE for single path"
 echo "-l: sets the load balancing type"
 echo "    the load balancing types are:"
 echo "    0 - No load balancing"
 echo "    1 - Static load balancing"
 echo "    2 - Dynamic load balancing - Least Recently Used (LRU)"
 echo "    3 - Dynamic load balancing - Least Service Time (LST)"
 echo "-h: prints out this help menu"
 echo ""
 echo "The following parameters this scripts sets on the QLogic kernel modules"
 echo "are:"
 echo ""
 echo "ql2xmaxqdepth - Maximum queue depth per LUN"
 echo "qlport_down_retry - Number of retries to perform when a storage port has"
 echo "                    done down"
 echo "qlogin_retry_count - Number of retries to perform when logging into a"
 echo "                     storage port"
 echo "ql2xfailover - Enable/Disable failover mode"
 echo "ql2xlbType - Type of load balancing to do over the number of active paths"
 echo "             to a active-active device.  This only works when the driver"
 echo "             is in failover mode"
 echo "ql2xautorestore - Make a device go back to its original path when that path"
 echo "                  comes back online"
}

# writes out /etc/hp_qla2x00.conf

write_conf () {
 echo -n "Writing new $PARAMFILE..."
 echo -n "" > $PARAMFILE
 echo "qdepth = $QDEPTH" >> $PARAMFILE
 echo "port_down_retry_count = $PDRC" >> $PARAMFILE
 echo "login_retry_count = $LRC" >> $PARAMFILE
 echo "failover = $FAILOVER" >> $PARAMFILE
 echo "load_balancing = $LB" >> $PARAMFILE
 echo "auto_restore = $AUTORESTORE" >> $PARAMFILE
 echo "done"

 # write config changes to /etc/modules.conf if driver
 # RPM is installed

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
  cd $SRCDIR
  ./edit_conf
 fi
}

# creates a new initrd with the changed parameters

write_initrd () {

 echo ""
 echo -n "Would you like to create a new initrd to reflect the changes in the parameters (Y/n)? "
 read ANS

 if [ "$ANS" = "n" ] || [ "$ANS" = "N" ]
 then
 	return
 fi

 cd $SRCDIR
 ./make_initrd

 if [ $INTERACTIVE -eq 1 ]
 then
 	pause
 fi
}

# sets parameters for XP/EVA/MSA failover

xp_failover () {
 PDRC=30
 LRC=30
 FAILOVER=1
 
 # If LB doesn't have a value yet we want to set it 1; However if there is
 # already a setting we want to preserve it

 if [ "$LB" = "" ] || [ $LB -lt 0 ] || [ $LB -gt 3 ] 
 then
	LB=1
 fi

 AUTORESTORE=0x80
}

# set parameters for single path

single_path () {
 PDRC=64
 FAILOVER=0
 LB=0
 AUTORESTORE=0x0
}

# lets user manually set parameters

manual () {
 echo ""
 echo "Default values in []."
 echo "If you are not sure what values you should use, accept the defaults."
 echo ""
 echo -n "queue depth [$QDEPTH]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	QDEPTH=$ANS
 fi

 echo -n "port down retry count [$PDRC]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	PDRC=$ANS
 fi

 echo -n "login retry count [$LRC]: "
 read ANS

 if [ "$ANS" != "" ]
 then
 	LRC=$ANS
 fi

 echo -n "failover [$FAILOVER]: "
 read ANS

 if [ "$ANS" != "" ]
 then
	FAILOVER=$ANS
 fi

 echo -n "load balancing type [$LB]: "
 read ANS

 if [ "$ANS" != "" ]
 then
        LB=$ANS
 fi

 echo -n "auto restore flags [$AUTORESTORE]: "
 read ANS

 if [ "$ANS" != "" ]
 then
	AUTORESTORE=$ANS
 fi

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
        write_initrd
 fi
}

# Set the load balancing flag

set_lbtype ()
{
 FUNCARG=$1

 # If we are not in failover mode, then don't even bother with this
 # function

 if [ $FAILOVER -eq 0 ]
 then
 	return
 fi

 # Bascially we go through the possible values which range from 0 to 3.
 # If the argument into this function is within that range we set it;
 # otherwise we don't do anything

 case $FUNCARG in
	0) LB=0;;
	1) LB=1;;
	2) LB=2;;
	3) LB=3;;
 esac
}

# Calls edit_conf to write out the new configuration and then make_initrd
# to create a new initrd

write_configuration () {
 # After we've set all the parameters, write out the new .conf file and
 # create a newinitrd

 write_conf

 if [ $MAKE_NEW_INITRD -eq 1 ]
 then
	write_initrd
 fi
}

#
# Script Main
#

# Get default values before we do anything
get_default_parameters

# determine if we should to to interactive mode or command
# line mode

if [ $# -eq 0 ]
then
 # interactive mode
 DONE=0
 INTERACTIVE=1

 while [ $DONE -ne 1 ]
 do
 	main_menu
 	read CHOICE

 	case $CHOICE in
 		1) xp_failover
		   write_configuration;;
 		2) single_path
		   write_configuration;;
 		3) manual;;
		4) exit 0;;
 		*) echo ""
    	   	   echo "Invalid Choice!!!"
    	   	   sleep 2;;
 	esac
 done
else
 # command line mode

 INTERACTIVE=0
 XPFLAG=0
 SINGLEFLAG=0
 HELPFLAG=0
 LBFLAG=0

 # test first argument to make sure that it has a "-" in it

 if [ `echo $1 | grep -c "-"` -eq 0 ]
 then
 	echo "Bad argument"
	exit 1
 fi

 # parse command line into arguments

 getopt xl:sh $* 1>/dev/null 2>/dev/null

 # check result of parsing

 if [ $? != 0 ]
 then
  echo "Bad argument or missing argument"
  exit 1
 fi

 set -- `getopt xl:sh $*`

 while [ $1 != -- ]
 do
 	case $1 in
  		-x) XPFLAG=1;;
  		-s) SINGLEFLAG=1;;
		-l) LBFLAG=1
		    CMDLINE_LBTYPE=$2
		    shift 1;;
  		-h) HELPFLAG=1;;
	esac
 	shift   # next flag
 done

 shift   # skip --

 # execute correct function based on flag

 if [ $HELPFLAG -eq 1 ]
 then
 	print_help
 	exit 1
 fi

 # If the -l flag is specified, we want to make sure that there is also
 # some mode specified as well

 if [ $LBFLAG -eq 1 ]
 then
 	if [ $XPFLAG -eq 0 ] && [ $SINGLEFLAG -eq 0 ]
	then
		echo "A driver mode needs to be specified"
		exit 1
	fi
 fi

 if [ $XPFLAG -eq 1 ]
 then
 	xp_failover
 elif [ $SINGLEFLAG -eq 1 ]
 then
 	single_path
 fi

 # Override load balancing type with the one specified at the command
 # line if needed

 if [ $LBFLAG -eq 1 ]
 then
        set_lbtype $CMDLINE_LBTYPE
 fi

 # After we've set all the parameters, write out the new .conf file and
 # create a newinitrd

 write_configuration
fi
