#!/bin/sh


function errata_58()
{
# Check AMD chipset issue Errata #58
	if test -x /sbin/lspci && test -x /sbin/setpci; then
	     if ( /sbin/lspci -nd 1022:1100 | grep "1100" > /dev/null ) &&
		( /sbin/lspci -nd 1022:7450 | grep "7450" > /dev/null ) &&
		( /sbin/lspci -nd 15b3:5a46 | grep "5a46" > /dev/null ); then

		echo "Fix AMD-8131 Errata #58"
		
		# Set default value for FIX_AMD_8131_ERR58
		if [ -z "$FIX_AMD_8131_ERR58" ]; then
			FIX_AMD_8131_ERR58="YES"
		fi
		
		CURVAL=`/sbin/setpci -d 1022:1100 69`

		for val in $CURVAL
		do
		    if [ "${val}" != "c0" ]; then
			if [ "$FIX_AMD_8131_ERR58" == "YES" ]; then
			    /sbin/setpci -d 1022:1100 69=c0
			    if [ $? -eq 0 ]; then
				# echo "AMD-8131 Errata #58 workaround applied"
				break
			    else
				echo "Failed to apply AMD-8131 Errata #58 workaround"
			    fi
			else
			    echo
			    echo "Invalid configuration found for PCI-X chipset AMD-8131 (Errata #58)"
			    echo "For more details see IB Gold Distribution Release Notes (IBGD_Release_Notes.pdf under docs directory)"
			    echo
			fi
		    fi
		done
	    
	    fi
	fi

}

function errata_56()
{
# Check AMD chipset issue Errata #56
	if test -x /sbin/lspci && test -x /sbin/setpci; then
	     if ( /sbin/lspci -nd 1022:1100 | grep "1100" > /dev/null ) &&
		( /sbin/lspci -nd 1022:7450 | grep "7450" > /dev/null ) &&
		( /sbin/lspci -nd 15b3:5a46 | grep "5a46" > /dev/null ); then
		echo "Fix AMD-8131 Errata #56"

		# Set default value for FIX_AMD_8131_ERR56
		if [ -z "$FIX_AMD_8131_ERR56" ]; then
			FIX_AMD_8131_ERR56="YES"
		fi

		bus=""

		# Look for devices AMD-8131
		for amd_dev in `/sbin/setpci -v -f -d 1022:7450 19 | cut -d':' -f1,2`
		do

			bus=`/sbin/setpci -s $amd_dev 19`
			rev=`/sbin/setpci -s $amd_dev 8`


			# Look for Tavor attach to secondary bus of this devices
			for device in `/sbin/setpci -f -s $bus: -d 15b3:5a46 19`
			do
				if [ $rev -lt 13 ]; then
					if [ "$FIX_AMD_8131_ERR56" == "YES" ]; then			
						/sbin/setpci -d 15b3:5a44 72=14
						if [ $? -eq 0 ]; then
							# echo "AMD-8131 Errata #56 workaround applied"
							break
						else
							echo "Failed to apply AMD-8131 Errata #56 workaround"
						fi
					else
						echo
						echo "Invalid configuration found for PCI-X chipset AMD-8131 (Errata #56)"
						echo "For more details see IB Gold Distribution Release Notes (IBGD_Release_Notes.pdf under docs directory)"
						echo

					fi

				else
					continue
				fi

				# If more than one device is on the bus the issue a
				# warning
				num=`/sbin/setpci -f -s $bus: 0 | wc -l |  sed 's/\ *//g'`

				if [ $num -gt 1 ]; then
					echo "Warning: your current PCI-X configuration might be incorrect."
					echo "see AMD-8131 Errata 56 for more details."
				fi

			done
		done

	     fi
	fi
}

function start_xhh(){
	local ret=0
	lsmod|grep -e mod_[tr]hh > /dev/null
	if [ $? -eq 0 ] ; then
		return $ret
	fi	
	local device_id=$(lspci -n|grep "Class 0c06"|grep 15b3|cut -f4 -d" "|cut -f2 -d:|head -1)
	case $device_id in
	5a44)
        	modprobe  mod_thh
		;;
	6278)
        	modprobe  mod_thh
		;;
	6274)
        	modprobe  mod_rhh
        	;;
	6282)
		modprobe  mod_rhh
                ;;
	*)
		echo ERROR: Dont know how to load driver for PCI device id $device_id
		ret=1
		;;
	esac
	return $ret
}

function usage()
{
	echo "$0 <start|stop|status>"
}

function load_module()
{
        loaded=0
        for (( i=0; i<30; i++ ))
        do
                modprobe $1
                if [ $? -eq 0 ] ; then
                        loaded=1
                        break;
                fi
                sleep 1
        done
        if [ $loaded -eq 0 ] ; then
                echo ERROR: failed to load module $1 
                exit 1
        fi
}

function remove_module()
{
	modprobe -r $1
        if [ $? -ne 0 ] ; then
                echo WARNING: failed to remove module $1
        fi
}
	
case $1  in 
start)
	errata_58
	start_xhh
	load_module  mod_ib_mgt 
	errata_56
	;;
stop)
	remove_module  mod_ib_mgt 
	;;
status)
	vstat
	;;
*)
	echo Unknown option: $1
	usage;
	exit 1
	;;
esac
exit 0
