#! /bin/sh -c "echo This script should not be run directly.; exit 1"
################################################################
# Myricom GM networking software and documentation
# Copyright 2001, 2002 by Myricom, Inc.
# All rights reserved.	 See the file `COPYING' for copyright notice.
################################################################
# author: glenn@myri.com
# $Source: /repository/gm/drivers/solaris2.5/gm_arch_install,v $

################################################################
# Required entry points
################################################################

# Make sure we find the right tools on this machine, regardless of
# the user's path.
PATH=/usr/ccs/bin:/usr/ucb:/usr/bin:/usr/sbin:/sbin
export PATH

gm_solaris_version_checked=0

gm_arch_driver_start_command="/etc/init.d/gm start"
gm_arch_install_program=install

# Tell the generic scripts that this architecture needs
# LD_LIBRARY_PATH_64 set, and needs it to point to the following
# install subdirectory.  Most architectures don't need this.
gm_arch_ld_library_path_64="lib/sparcv9"

gm_debug_arch_stop_gm=0
gm_arch_stop_gm ()
{
    gm_arch_stop_ethernet
    gm_do rem_drv gm || gm_die "Could not remove GM driver"
    rm -f /dev/gm* /dev/gmp* /dev/myri* > /dev/null 2>&1
}

gm_arch_start_gm ()
{
    gm_solaris_check_version \
    || gm_die "Solaris version is not supported"
    
    gm_do sync \
    || gm_die "Could not sync filesystem."
    
    gm_do add_drv -v -i "\"pci10e8,8043\" \"pci14c1,8043\" \"pci103c,106b\" \"MYRICOM,mlanai\"" -m "* 0666 bin bin" /kernel/drv/${gm_solaris_driver_subdir}/gm 2>&1 > /dev/null \
    || gm_die "Could not add GM driver"

    # Run devlinks
    devlinks

    # Wait for the GM driver to become useable before starting ethernet.
    gm_wait_for_driver_to_load || gm_die Failed to run simple test program.

    gm_arch_start_ethernet
}

gm_arch_stop_ethernet ()
{
    for i in 0 1 2 3
    do
	gm_do /sbin/ifconfig myri${i} down unplumb > /dev/null 2>&1
    done
    
    gm_do /usr/sbin/rem_drv myri > /dev/null 2>&1
    rm -f /dev/myri* > /dev/null 2>&1
}

gm_arch_start_ethernet ()
{
    gm_solaris_check_version || gm_die "Solaris version is not supported"
    
    gm_do sync || gm_die "Could not sync filesystem"
    
    gm_do add_drv -v -i "myri" -m "* 0666 bin bin" /kernel/drv/${gm_solaris_driver_subdir}/myri
    > /dev/null 2>&1 \
    || gm_die "Could not add GM driver"
}

gm_arch_install_ethernet ()
{
    gm_mkdir /kernel/drv/${gm_solaris_driver_subdir} \
    || gm_die "Could not mkdir /kernel/drv/${gm_solaris_driver_subdir}"
    
    gm_cp ${prefix}/sbin/myri /kernel/drv/${gm_solaris_driver_subdir}/myri \
    || gm_die "Could not create /kernel/drv/${gm_solaris_driver_subdir}/myri"
    
    gm_cp ${prefix}/kernel/drv/myri.conf /kernel/drv/myri.conf \
    || gm_die "Could not create /kernel/drv/myri.conf"
    
    gm_install_program ${prefix}/etc/init.d/myri /etc/init.d/myri \
    || gm_die "Could not create /etc/init.d/myri"
    
    gm_do sync \
    || gm_die "Could not sync filesystem"
    
    gm_log_uninstall_command "/etc/init.d/myri stop" \
    || gm_die "Problem writing uninstall file"
    
#    gm_do /etc/init.d/myri start \
#    || gm_die "Could not start myri driver"
}

gm_arch_install_gm ()
{
    gm_solaris_check_version \
    || gm_die "Solaris version is not supported"
    
    gm_solaris_fix_lomempages \
    || gm_die "Could not fix lomempages"
    
    gm_solaris_setup_devlink_tab \
    || gm_die "Could not setup devlink.tab"
    
    gm_cp ${prefix}/sbin/gm /kernel/drv/${gm_solaris_driver_subdir}/gm \
    || gm_die "Could not create /kernel/drv/${gm_solaris_driver_subdir}/gm"
    
    gm_install_program ${prefix}/etc/init.d/gm /etc/init.d/gm \
    || gm_die "Could not create /etc/init.d/gm"
    
    gm_do sync \
    || gm_die "Could not sync filesystems"
    
    gm_log_uninstall_command "/etc/init.d/gm stop" \
    || gm_die "Problem writing uninstall file"
    
#    gm_do /etc/init.d/gm start \
#    || gm_die "Could not start GM driver"
}

################################################################
# Solaris-specific functions
################################################################

gm_solaris_check_version ()
{
    if test $gm_solaris_version_checked != 0
    then
	return 0
    fi

    version=`uname -r`
    case "$version" in
    [1234].* | 5.[012345] | 5.[012345].*)
	gm_echo Solaris-"$version" is not supported by this release.
	status=1
	;;
    5.6)
	file ${prefix}/sbin/gm | fgrep 32-bit > /dev/null 2>&1
	status=$?
	if test $status != 0
	then
	    gm_echo \
 "GM is not compiled in 32-bit mode to match the running kernel."
	    return $status
	fi
	;;
    *)
       arch=`isainfo -b`
       gm_verbose_echo "arch=$arch"
       if test $arch = "64" ; then
	    file ${prefix}/sbin/gm | grep 64-bit > /dev/null
	    status=$?
	    if test $status != 0
	    then
		gm_echo \
 "GM is not compiled in 64-bit mode to match the running kernel."
		return $status
	    fi
	    gm_solaris_driver_subdir=sparcv9
       else
	    file ${prefix}/sbin/gm | grep 32-bit > /dev/null 2>&1
	    status=$?
	    if test $status != 0
	    then
		gm_echo \
 "GM is not compiled in 32-bit mode to match the running kernel."
		return $status
	    fi
	    gm_solaris_driver_subdir=.
       fi
       ;;
    esac
    gm_solaris_version_checked=1
    return 0
}

gm_solaris_fix_lomempages ()
{
    if [ x`arch` != xi86pc ]
    then
	status=0
    else
    	if `grep "lomempages=130" /etc/system > /dev/null`; then
    	    status=0
    	else
    	    sed -e '/set lomempages/d' /etc/system > /tmp/system1$$
	    status=$?
	    if test $status = 0
	    then
		cat > /tmp/gm_sed_$$ <<-"EOF"
		$a\
		set lomempages=130
EOF
		status=$?
		if test $status = 0
		then
		    sed -f /tmp/gm_sed_$$ /tmp/system1$$ > /tmp/system2$$
		    status=$?
		    if test $status = 0
		    then
			cp /etc/system /etc/system.save
			status=$?
			if test $status = 0
			then
			    mv /tmp/system2$$ /etc/system
			    status=$?
			    if test $status = 0
			    then
    gm_echo
    gm_echo 'Added "lomempages=130" to your /etc/system.'
    gm_echo 'Please reboot now so this can take effect, and then'
    gm_echo 'install GM again once the machine has come back up.'
    gm_echo
    gm_warn "The machine must be rebooted before GM will be fully operational."
			        return 0
			    fi
			    # Restore backed up config file.  This is not 
			    # likely to succeed since the mv above failed,
			    # but we might as well try.
			    mv /etc/system.save /etc/system
			fi
			# Do not delete the backup file.
		    fi
		    rm -f /tmp/system2$$
		fi
		rm -f /tmp/gm_sed_$$
	    fi
	    rm -f /tmp/system1$$
    	fi
    fi

    return $status
}

gm_solaris_setup_devlink_tab ()
{
    # Create a sed script for removing out of date lines from devlink.tab
    
    cat > /tmp/gm_sed_$$ <<-"EOF"
		/^.*name=pci10e8,8043[ 	]*.*$/d
		/^.*name=pci14c1,8043[ 	]*.*$/d
		/^.*name=pci103c,106b[ 	]*.*$/d
		/^.*name=gm[ 	]*.*$/d
		/^.*name=myri[ 	]*.*$/d
		/^.*name=mlanai[ 	]*.*$/d
		/^.*name=MYRICOM,mlanai[ 	]*.*$/d
EOF
    status=$?
    if test $status = 0
    then
	####
	# Create devlink1_$$
	####
    
	# Process devlink.tab with this sed file
        sed -f /tmp/gm_sed_$$ /etc/devlink.tab > /tmp/gm_devlink1_$$
	status=$?
        # Append lines for PCI to /etc/devlink.tab
        cat >> /tmp/gm_devlink1_$$ <<-"EOF" || status=$?
			type=ddi_pseudo;name=pci10e8,8043	\M0
			type=ddi_pseudo;name=pci14c1,8043	\M0
			type=ddi_pseudo;name=pci103c,106b	\M0
EOF
	
        cat >> /tmp/gm_devlink1_$$ <<-"EOF" || status=$?
			type=ddi_pseudo;name=myri	\M0
EOF

	####
	# Done trying to create devlink1_$$
	####
	
	if test $status = 0
	then
            # Back up /etc/devlink.tab, unless it's already backed up.
            if [ ! -f /etc/devlink.tab.save ]; then
                cp /etc/devlink.tab /etc/devlink.tab.save
		status=$?
	    fi
	fi
	
	if test $status = 0
	then
	    # Replace the devlink.tab with the new file
	    mv -f /tmp/gm_devlink1_$$ /etc/devlink.tab
	    status=$?
	fi
	rm -f /tmp/gm_devlink1_$$
    fi
    rm -f /tmp/gm_sed_$$
    return $status
}
