#!/bin/sh

# (C) Copyright 2002-2005, 2009 Hewlett-Packard Development Company, L.P.


KVER="$(uname -r)"
LOGFILE="/var/spool/compaq/cma.log"
MODBIN="/opt/hp/hp-OpenIPMI/bin/$KVER"

# **
# ** HP recommends that only the iLO 2 interface on PCI Function 6 be used
# ** since it is interrupt driven.  You may, however, enable the legacy
# ** (Polled) interface and run both simultaneously by setting this to "no".
# ** Not all versions of the IPMI drivers support disabling function 2.
# **
DISABLE_FUNCTION_TWO=yes

mod_supports_param() {
    local mod="$1"   # Filesystem path
    local param="$2"

    if modinfo -p "$mod" | grep -q "$param"; then
	return 0
    fi
    return 1
}

if [ ! -d /var/spool/compaq ]; then
	mkdir -p /var/spool/compaq
fi


case "$1" in
  start)
	echo  " " "Starting hp-OpenIPMI: " | tee -a "$LOGFILE"
#
# First test to see if IPMI driver is loaded
#

	OPENWBEM=`pidof owcimomd`

	if lsmod | grep -q ipmi_si; then
#
# **** SLES 10 OpenWBEM stuff
#
		if [ -f /etc/init.d/owcimomd ]; then
			echo "Stopping SuSE OpenWBEM CIMOM Monitor to stop IPMI"
			sh /etc/init.d/owcimomd stop
		fi

		echo "Attempting to stop existing IPMI drivers"
		if ! rmmod ipmi_si; then
			echo "hp-OpenIPMI: Not able to stop ipmi_si" | tee -a "$LOGFILE"
			exit 1
		fi
	fi

	for mod in ipmi_devintf ipmi_msghandler; do
		if lsmod | grep -q "$mod"; then
			if ! rmmod "$mod"; then
				echo "hp-OpenIPMI: Not able to stop $mod" | tee -a "$LOGFILE"
				exit 1
			fi
		fi
	done

	rm -f /dev/ipmi0
	rm -f /dev/ipmi1

	if ! /opt/hp/hp-OpenIPMI/check_install_kernel.sh >> "$LOGFILE"; then
		exit 1
	fi

	insmod "$MODBIN/ipmi_msghandler.ko"
	if ! lsmod | grep -q ipmi_msghandler; then
		echo "hp-OpenIPMI: Not able to start ipmi_msghandler.ko" | tee -a "$LOGFILE"
		exit 1
	fi

	insmod "$MODBIN/ipmi_devintf.ko"
	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_devintf.ko" | tee -a "$LOGFILE"
		exit 1
	fi

# ** 
# ** We have multiple flavors of drivers out there today.  We can tell by 
# ** the parameters passed in so we know how to load the driver.
# **
	modargs=""
#
# ** For 2.6.18 kernels, we leverage the kcs_debug and quiesce_intfs parameters.
# ** The "quiesce_intfs" is used to idle the legacy KCS interface at 0xca2 
# ** when the port is not opened.  This helps with performance.
#
	if mod_supports_param "$MODBIN/ipmi_si.ko" quiesce_intfs; then
		modargs="$modargs quiesce_intfs=1,0"
	fi

#
# ** Uncomment these lines to debug IPMI messages with the health driver.
#
	#if mod_supports_param "$MODBIN/ipmi_si.ko" kcs_debug; then
	#	modargs="$modargs kcs_debug=3"
	#fi

	if mod_supports_param "$MODBIN/ipmi_si.ko" pci_vend_devs; then
		if [ "$DISABLE_FUNCTION_TWO" = "yes" ]; then
			modargs="$modargs type=kcs"
		else
			modargs="$modargs type=kcs,kcs ports=0xCA0,0"
		fi
		modargs="$modargs pci_vend_devs=0x103c3302"
	fi

	if mod_supports_param "$MODBIN/ipmi_si.ko" debug_intfs; then
		if [ "$DISABLE_FUNCTION_TWO" = "yes" ]; then
			modargs="$modargs debug_intfs=0"
		else
			modargs="$modargs debug_intfs=0,0"
		fi
	fi

	if ! insmod "$MODBIN/ipmi_si.ko" $modargs; then
		echo "hp-OpenIPMI: Not able to start ipmi_si.ko" | tee -a "$LOGFILE"
		exit 1
	fi

	major=`awk "\\$2==\"ipmidev\" {print \\$1}" /proc/devices`
	mknod /dev/ipmi0 c $major 0 > /dev/null 2>&1
	if [ "$DISABLE_FUNCTION_TWO" != "yes" ]; then
		mknod /dev/ipmi1 c $major 1
	fi
	if [ ${#OPENWBEM} -ne 0 ]; then
		echo "Restarting OpenWBEM CIMOM monitor"
		/etc/init.d/owcimomd start
	fi
	;;


  stop)
	echo " " "Stopping hp-OpenIPMI: " | tee -a "$LOGFILE"

#
# **** SLES 10 OpenWBEM stuff
#
	if [ -f /etc/init.d/owcimomd ]; then
		echo "Stopping OpenWBEM CIMOM monitor to stop IPMI drivers . . ."
		sh /etc/init.d/owcimomd stop
	fi

#
# Due to a "Chicken and Egg" issue, we need to postpone our removal to allow
# applications that are using the OpenIPMI driver to finish closing their file
# handles.  We will call ourselves in "x" seconds to allow other applications 
# to cleanly stop.  Not sure what we'll do in the case of OEM applications.
#
	RUNNING=`pidof hpasmxld owcimomd`
	while [ ${#RUNNING} -ne 0 ]
	do
		sleep 1
		RUNNING=`pidof hpasmxld owcimomd`
	done

#
# Check to see if other OEM software might be using this module.
#
	COUNT=`cat /proc/modules | grep -m 1 ipmi_si | cut -d ' ' -f 3`
	if [ "${COUNT}" != "" -a "${COUNT}" != "0" ]; then
		echo "hp-OpenIPMI: ipmi_si is being used by another application"
		echo "The openwbem application does connect to ipmi_si."
		echo "Try service owcimomd stop to see if this works."
		exit 1
	fi


	for mod in ipmi_si ipmi_devintf ipmi_msghandler; do
		if lsmod | grep -q "$mod"; then
			if ! rmmod "$mod"; then
				echo "hp-OpenIPMI: Not able to stop $mod"
				exit 1
			fi
		fi
	done

	;;


  restart)
	exit 0
        ;;
  status)
	echo ""
	echo "hp-OpenIPMI Status:"
	lsmod | grep -e "Module" -e "ipmi"
	if ! lsmod | grep -q ipmi; then
		echo "WARNING:  The hp-OpenIPMI driver does not appear to be loaded!"
		exit 1
	fi
        ;;
  configure)
	exit 0
        ;;
  reconfigure)
	exit 0
        ;;
  unconfigure)
	exit 0
        ;;

  *)
	exit 1

esac

exit 0
