#!/bin/sh

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

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


case "$1" in
  start)
	echo  " " "Starting hp-OpenIPMI: " 
	echo  " " "Starting hp-OpenIPMI: " >> /var/spool/compaq/cma.log
#
# First test to see if IPMI driver is loaded
#

	OPENWBEM=`pidof owcimomd`

	RUNNING=`lsmod | fgrep ipmi_si`
	if [ ${#RUNNING} -ne 0 ]; 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 "Attemting to stop existing IPMI drivers"
		sleep 5
		rmmod ipmi_si
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_si"
			echo "hp-OpenIPMI: Not able to stop ipmi_si" >> /var/spool/compaq/cma.log
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_devintf`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_devintf
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_devintf"
			echo "hp-OpenIPMI: Not able to stop ipmi_devintf" >> /var/spool/compaq/cma.log
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_msghandler`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_msghandler
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_msghandler"
			echo "hp-OpenIPMI: Not able to stop ipmi_msghandler" >> /var/spool/compaq/cma.log
			exit 1
		fi
	fi

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

	/opt/hp/hp-OpenIPMI/check_install_kernel.sh >> /var/spool/compaq/cma.log

	if [ $? != 0 ]; then
		exit 1
	fi

	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_msghandler.ko 2>/tmp/hptest
	if [ "$?" -eq -1 ] ; then
           if [ "`sed -e 's/.*\(File exists\)/\1/' /tmp/hptest`" != "File exists" ]; then
		echo "hp-OpenIPMI: Not able to start ipmi_msghandler.ko"
		echo "hp-OpenIPMI: Not able to start ipmi_msghandler.ko" >> /var/spool/compaq/cma.log
		exit 1
          fi
	fi

	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_devintf.ko
	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_devintf.ko"
		echo "hp-OpenIPMI: Not able to start ipmi_devintf.ko" >> /var/spool/compaq/cma.log
		exit 1
	fi

# **
# ** 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.
# **

# ** 
# ** 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.
# **

	modinfo -p /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_si.ko | grep quiesce_intfs > /dev/null
	if [ "$?" -eq 0 ]; then 
#
# ** For 2.6.18 kernels, we leverage the kcs_debug and quiesce_intfs paramters.
# ** Set kcs_debug=3 to debug IPMI messages with the health driver.
# ** The "quiesce_intfs" is used to idle the legacy KCS interface at 0xca2 
# ** when the port is not opened.  This helps with performance.
#

		insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_si.ko quiesce_intfs=1,0 kcs_debug=0
	else
# ** The following enables function 6 IPMI interface only.
		insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_si.ko type=kcs pci_vend_devs=0x103c3302 debug_intfs=0
	fi
	
# **
# ** If you want both function 2 and Function 6, uncomment the following line
# ** and the "mknod" line that is commented out below.  You need to also
# ** comment out the other "insmod" line above as only one "ipmi_si.ko"
# ** module can be inserted.  NOTE:  This will ONLY work with kernels
# ** before 2.6.18.  Your milage may vary depending on the flavor of Linux
# ** you are using.
# **

#	insmod /opt/hp/hp-OpenIPMI/bin/`uname -r`/ipmi_si.ko type=kcs,kcs ports=0xCA0,0 pci_vend_devs=0,0x103c3302 debug_intfs=0,0

	if [ "$?" -ne 0 ] ; then
		echo "hp-OpenIPMI: Not able to start ipmi_si.ko"
		echo "hp-OpenIPMI: Not able to start ipmi_si.ko" >> /var/spool/compaq/cma.log
		exit 1
	fi

	sleep 2

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


  stop)
	echo " " "Stopping hp-OpenIPMI: "
	echo " " "Stopping hp-OpenIPMI: " >> /var/spool/compaq/cma.log

#
# **** 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


	RUNNING=`lsmod | fgrep ipmi_si`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_si
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_si"
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_devintf`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_devintf
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_devintf"
			exit 1
		fi
	fi

	RUNNING=`lsmod | fgrep ipmi_msghandler`
	if [ ${#RUNNING} -ne 0 ]; then
		rmmod ipmi_msghandler
		if [ "$?" -ne 0 ] ; then
			echo "hp-OpenIPMI: Not able to stop ipmi_msghandler"
			exit 1
		fi
	fi

	;;


  restart)
	exit 0
        ;;
  status)
	echo ""
	echo "hp-OpenIPMI Status:"
	lsmod | grep -e "Module" -e "ipmi"
	IPMIDRIVER=`/sbin/lsmod |fgrep ipmi`
	if [ ${#IPMIDRIVER} -eq 0 ]; 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
