#
# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
#
#pragma ident   "@(#)148888_SUNWhermon_postinstall.sh 1.2     13/10/04 SMI"
#
### BEGIN: overlay 148888-05/SUNWhermon/install/postinstall
if [ "${PatchID}" = "148888-05" \
	-a `/usr/bin/basename $0` = "postinstall" \
	-a "${PKG}" = "SUNWhermon" ] ; then

postinstall_merge() {

#!/bin/sh
#
# Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
#
# ident	"@(#)postinstall	1.3	10/10/22 SMI"
#

EXIT=0   
PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

MCXNEX_ALIAS="\
	\"pciex15b3,6340\" \
	\"pciex15b3,634a\" \
	\"pciex15b3,6732\" \
	\"pciex15b3,673c\" \
	\"pciex15b3,6746\" \
	\"pciex15b3,6750\" \
	\"pciex15b3,1002\" \
	"
DRVPERM='* 0666 root sys'

# Helper functions
check_installed() {
	driver=$1
	grep "^${driver} " $BASEDIR/etc/name_to_major > /dev/null 2>&1
	return $?
}

check_alias() {
	driver=$1
	grep "^${driver} " $BASEDIR/etc/driver_aliases > /dev/null 2>&1
	return $?
}

# Check if hermon is installed, i.e old driver (non-unified driver)
# if yes, remove the older hermon aliases
check_alias hermon
if [ $? -eq 0 ] ; then
	for i in ${MCXNEX_ALIAS}
	do
		update_drv -b "${BASEDIR}" -d -i "${i}" hermon > /dev/null 2>&1
	done
fi

# Check if mcxnex is already installed (either due to mcxe or new unified 
# driver).
check_installed mcxnex
ret=$?
# If not already installed, install the mcxnex with the updated aliases.
if [ $ret -eq 1 ] ; then
	add_drv -b "${BASEDIR}" \
		-m "${DRVPERM}" \
		-i "${MCXNEX_ALIAS}" \
		mcxnex || \
	EXIT=1
elif [ $ret -eq 0 ] ; then
	# If installed, check if the newer pci alias (VF driver) is updated.
	for i in ${MCXNEX_ALIAS}    
	do
		egrep "^mcxnex [  ]+${i}" ${BASEDIR}/etc/driver_aliases > /dev/null 2>&1
		if [ $? -eq 1 ]
		then
			update_drv -b "${BASEDIR}" -a -i "${i}" mcxnex > /dev/null 2>&1
		fi
	done
fi

check_installed hermon || \
add_drv -b "${BASEDIR}" \
	-m "${DRVPERM}" \
	hermon || \
EXIT=1

check_installed mcxe || \
add_drv -b "${BASEDIR}" \
	-m "${DRVPERM}" \
	mcxe || \
EXIT=1

#
# The IB Unified Driver (UD) project changed the device tree underneath
# the IB HCA node.  The job of this ibud post upgrade script is to replace
# obsolete lines in /etc/path_to_inst with their replacement lines (example
# shown below).  This script is run only once, as it will remove the old paths
# from /etc/path_to_inst which is the condition needed for this script to run.
#
# Here is an example of the old/legacy paths:
#
#	"/pci8086,3410@9/pci15b3,673c@0" 0 "hermon"
#	"/pci8086,3410@9/pci15b3,673c@0/ibport@1,ffff,ipib" 0 "ibd"
#	"/pci8086,3410@9/pci15b3,673c@0/ibport@2,ffff,ipib" 1 "ibd"
#
# which is replaced by the new paths:
#
#	"/pci8086,3410@9/pci15b3,673c@0" 0 "mcxnex"
#	"/pci8086,3410@9/pci15b3,673c@0/hermon@0" 0 "hermon"
#	"/pci8086,3410@9/pci15b3,673c@0/hermon@0/ibport@1,ffff,ipib" 0 "ibd"
#	"/pci8086,3410@9/pci15b3,673c@0/hermon@0/ibport@2,ffff,ipib" 1 "ibd" 
#
# The important feature of this script is to maintain the instance numbers
# within /etc/path_to_inst so that other consumers of device driver instances,
# e.g., dladm, retain their validity during this one-time upgrade from the
# old "hermon" driver to the new "mcxnex/hermon" layered drivers.
#
# Also, we want to retain the old mcxnex entry created for mcxe, that has
# vendorid,devid = pciex15b3,6750
# and pciex15b3,11 or...
#
# Update /etc/path_to_inst entries for IB mcxnex driver (see 16213784)

	PATHTOINST="${PKG_INSTALL_ROOT}/etc/path_to_inst"
	MV="/usr/bin/mv"
	EGREP="/usr/bin/egrep"
	NAWK="/usr/bin/nawk"

	#
	# Check to see if there is a need for an upgrade from legacy driver
	# (hermon) to unified driver (mcxnex/hermon).  The condition is that
	# there exist paths for the hermon driver that do not have the
	# "hermon@" substring.
	#
	$EGREP -v 'hermon@' $PATHTOINST | $EGREP '"hermon"' >/dev/null
	if [ $? -eq 1 ]; then
		# No legacy "hermon", so no need to do anything.
		return
	fi
	# We're doing the "one time" device path conversion.
	
	#
	# Determine lowest mcxnex_inst for our use.  There could have been
	# instances in earlier releases on i386 for Mellanox ethernet cards
	# that have "vendor ID, device id" of "pciex15b3,6750".
	# on some systems with MCXE card running s10, we also saw the following path
	# names
	# x86: "/pci@79,0/pci10de,5d@c/pci15b3,11@0" 0 "mcxnex"
	# sparc: "/pci@7c0/pci@0/pci@9/ethernet@0" 0 "mcxnex"
	#
	hermon_inst=0
	mcxnex_inst=`$NAWK -F' ' 'BEGIN { mcxnex_inst=0 }
	$0 ~ /pciex15b3,6750@.+\"\ .+\ \"mcxnex\"/ ||
	$0 ~     /pci15b3,11@.+\"\ .+\ \"mcxnex\"/ ||
	$0 ~       /ethernet@.+\"\ .+\ \"mcxnex\"/ {
		if ($2>=mcxnex_inst) mcxnex_inst=$2+1;
	}
	END {print mcxnex_inst}' $PATHTOINST`
	# Now we have lowest "mcxnex_inst" for our use.

	#
	# Find all new ConnectX (mcxnex) entries, and destroy them.
	# Find all old/legacy ConnectX (hermon) entries, and convert them.
	#
	$NAWK -F' ' -v mcxnex_inst=$mcxnex_inst '
	$0 ~ /pciex15b3,6750@.+\"\ .+\ \"mcxnex\"/ ||
	$0 ~     /pci15b3,11@.+\"\ .+\ \"mcxnex\"/ ||
	$0 ~       /ethernet@.+\"\ .+\ \"mcxnex\"/ {
		#
		# Discard it unless it is pciex15b3,6750 (which was
		# previously supported by mcxnex ethernet on i386).
		#
		#print "[DEBUG] retain old mcxnex(mcxe) entries",$1;
		print $0;
		next;
	}
	/\"hermon\"/ {
		# Remember hermon instance for use with ibport
		hermon_inst=$2;
		hermon_addr=mcxnex_inst;

		#
		# Create new mcxnex line, and increment
		# mcxnex instance number for the next device.
		#
		#print "[DEBUG] Convert Old hermon to mcxnex |",$0,"| to "$1,$2+mcxnex_inst,"\"mcxnex\"";
		print $1,mcxnex_inst,"\"mcxnex\"";
		mcxnex_inst++;

		# Create new hermon line
		sub("\"\ [0-9]+\ \"","\/hermon@"hermon_addr"\"\ "$2"\ ""\"");
		print $0;
		next;
	}
	$0 ~ /ibport/ && $0 !~ /hermon@.\/ibport/ {
		oldibps=$0;
		# Replace old with new ibport path.
		sub("ibport","hermon@"hermon_addr"\/ibport");
		#print "[DEBUG] Convert ibport from ",oldibps," ===> ",$0;
		print $0;
		next;
	}
	#
	# The path is something else, so just pass it
	# forward.  This includes all comment lines.
	#
	{ print $0 }' $PATHTOINST > $PATHTOINST.tmp

	# replace old p2i with updated IB entries   
	$CP $PATHTOINST ${PATHTOINST}.old
	$MV $PATHTOINST.tmp $PATHTOINST
	chmod 444 $PATHTOINST

return ${EXIT}

}


fi
### END: overlay 148888-05/SUNWhermon/install/postinstall
