#!/bin/bash
# run this script from within the IntelIB-Basic/*OFED* or IntelIB-IFS/*OFED*
# tree which is to be patched.  It will edit the ofa_kernel SRPMS

patch_ofed()
{
	# $1 of -R will reverse patch
	# $1 of -C will remove all signs we patched it
	clean=
	reverse=
	if [ "x$1" = "x-R" ]
	then
		reverse=$1
	elif [ "x$1" = "x-C" ]
	then
		clean=y
	fi 

	# this will force rebuild from srpm
	rm -f RPMS/*/kernel-ib-1*.rpm

	# untar the srpm so we can patch it
	cd SRPMS
	srpm=`ls ofa_kernel-1*.src.rpm`
	if [ ! -e $srpm-save ]
	then
		mv $srpm $srpm-save
	else
		if [ "x$reverse" = "x-R" ]
		then
			cp $srpm-save $srpm
			return
		else
			rm $srpm
		fi
	fi
	rm -rf orig patched
	mkdir orig
	cd orig
	rpm2cpio ../$srpm-save|cpio -ic
	tgz=`ls *.tgz`
	base=`basename $tgz .tgz`
	cd ..

	# now patch the source
	cp -r orig patched
	cd patched
	tar xfz $tgz
	cd $base
	patch -p1 $reverse < /tmp/todd.patch.diff
	cd ..
	tar cfz $tgz $base
	
	# regenerate the patched srpm
	rm -rf SOURCES SPECS SRPMS
	mkdir SOURCES SPECS SRPMS
	cp $tgz SOURCES/
	cp *.spec SPECS/
	rpmbuild -bs --define "_topdir `pwd`" SPECS/*.spec
	cp SRPMS/*.src.rpm ../
	cd ..
	if [ "x$clean" = "xy" ]
	then
		rm -rf orig patched $srpm-save
	fi
}

rm -f /tmp/todd.patch.diff
cat > /tmp/todd.patch.diff <<EOF
This is the set of patches to OFED to permit Mellanox and TrueScale HCAs
perform the approach3 by sending to DLID 0x61ff for all outbound DR SMP packets
when connected to S20.  This is required for all HSM nodes.

The patch is against the ofa_kernel srpm in 1.4rc2 build.

diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_driver.c ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_driver.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_driver.c	2008-10-19 08:15:06.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_driver.c	2008-11-13 17:01:26.000000000 -0500
@@ -2358,6 +2358,7 @@
 
 	ipath_hol_up(dd); /* make sure user processes aren't suspended */
 
+	dd->std_mode_flag = 0;
 	dd->ipath_flags |= IPATH_LINKUNK;
 	dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
 			     IPATH_LINKINIT | IPATH_LINKARMED |
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_init_chip.c ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_init_chip.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_init_chip.c	2008-10-19 08:15:07.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_init_chip.c	2008-11-13 17:01:26.000000000 -0500
@@ -658,6 +658,7 @@
 	if (ret)
 		goto done;
 
+	dd->std_mode_flag = 0;
 	if (reinit)
 		ret = init_chip_reset(dd);
 	else
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_intr.c ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_intr.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_intr.c	2008-10-19 08:15:07.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_intr.c	2008-11-13 17:01:26.000000000 -0500
@@ -363,6 +363,7 @@
 			/* and no more until active again */
 			dd->ipath_lastlinkrecov = 0;
 			ipath_set_linkstate(dd, IPATH_IB_LINKDOWN);
+			dd->std_mode_flag = 0;
 			goto skip_ibchange;
 		}
 	}
@@ -423,6 +425,7 @@
 	} else { /* down */
 		if (dd->ipath_flags & IPATH_LINKACTIVE)
 			signal_ib_event(dd, IB_EVENT_PORT_ERR);
+		dd->std_mode_flag = 0;
 		dd->ipath_flags |= IPATH_LINKDOWN;
 		dd->ipath_flags &= ~(IPATH_LINKUNK | IPATH_LINKINIT
 				     | IPATH_LINKACTIVE |
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_kernel.h ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_kernel.h
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_kernel.h	2008-10-19 08:15:07.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_kernel.h	2008-11-13 17:01:26.000000000 -0500
@@ -675,6 +675,7 @@
 	 * ASCII serial number, from flash, large enough for original
 	 * all digit strings, and longer QLogic serial number format
 	 */
+	u32 std_mode_flag;
 	u8 ipath_serial[16];
 	/* human readable board version */
 	u8 ipath_boardversion[96];
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_mad.c ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_mad.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_mad.c	2008-10-19 08:15:07.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_mad.c	2008-11-13 18:35:09.000000000 -0500
@@ -1389,6 +1389,31 @@
 	struct ipath_ibdev *dev = to_idev(ibdev);
 	int ret;
 
+	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_SET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO)
+	{
+		struct ib_port_info *pip = (struct ib_port_info *)(((struct ib_smp*)in_mad)->data);
+		if (((struct ib_smp*)in_mad)->hop_cnt > 60
+			&& ((struct ib_smp*)in_mad)->hop_ptr > 60) {
+			dev->dd->std_mode_flag = 1;
+			in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
+		} else if (((pip->portphysstate_linkdown >> 4) & 0xF) == 3) {
+			/* disabled */
+			dev->dd->std_mode_flag = 0;
+		}
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->hop_cnt > 60
+		&& ((struct ib_smp*)in_mad)->hop_ptr > 60
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_GET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_INFO) {
+		dev->dd->std_mode_flag = 1;
+		in_mad->mad_hdr.attr_id = IB_SMP_ATTR_PORT_INFO;
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->dr_dlid == cpu_to_be16(0x61ff)) {
+		dev->dd->std_mode_flag = 1;
+	}
+
 	*out_mad = *in_mad;
 	if (smp->class_version != 1) {
 		smp->status |= IB_SMP_UNSUP_VERSION;
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_ud.c ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_ud.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_ud.c	2008-10-19 08:15:07.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/ipath/ipath_ud.c	2008-11-13 17:01:26.000000000 -0500
@@ -376,6 +376,10 @@
 		qp->s_hdr.lrh[3] = cpu_to_be16(lid);
 	} else
 		qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
+	if (qp->s_hdr.lrh[1] == IB_LID_PERMISSIVE
+		&& dev->dd->std_mode_flag) {
+			qp->s_hdr.lrh[1] = cpu_to_be16(0x61ff);
+	}
 	if (wqe->wr.send_flags & IB_SEND_SOLICITED)
 		bth0 |= 1 << 23;
 	bth0 |= extra_bytes << 20;
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mad.c ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mad.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mad.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mad.c	2008-11-13 18:35:27.000000000 -0500
@@ -232,6 +232,30 @@
 	int err;
 
 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
+	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_SET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO)
+	{
+		struct ib_port_info *pip = (struct ib_port_info *)(((struct ib_smp*)in_mad)->data);
+		if (((struct ib_smp*)in_mad)->hop_cnt > 60
+			&& ((struct ib_smp*)in_mad)->hop_ptr > 60) {
+			to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+			in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
+		} else if (((pip->portphysstate_linkdown >> 4) & 0xF) == 3) {
+			/* disabled */
+			to_mdev(ibdev)->std_mode_flag[port_num-1] = 0;
+		}
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->hop_cnt > 60
+		&& ((struct ib_smp*)in_mad)->hop_ptr > 60
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_GET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_INFO) {
+		to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+		in_mad->mad_hdr.attr_id = IB_SMP_ATTR_PORT_INFO;
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->dr_dlid == cpu_to_be16(0x61ff)) {
+		to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+	}
 
 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
 		forward_trap(to_mdev(ibdev), port_num, in_mad);
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/main.c ofa_kernel-1.4/drivers/infiniband/hw/mlx4/main.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/main.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mlx4/main.c	2008-11-13 17:01:26.000000000 -0500
@@ -714,6 +714,7 @@
 
 	case MLX4_DEV_EVENT_PORT_DOWN:
 		ibev.event = IB_EVENT_PORT_ERR;
+		to_mdev((struct ib_device*)ibdev_ptr)->std_mode_flag[port-1] = 0;
 		break;
 
 	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mlx4_ib.h ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mlx4_ib.h
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mlx4_ib.h	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mlx4/mlx4_ib.h	2008-11-13 17:01:26.000000000 -0500
@@ -168,6 +168,7 @@
 	u32			priv_pdn;
 	MLX4_DECLARE_DOORBELL_LOCK(uar_lock);
 
+	u32				std_mode_flag[MLX4_MAX_PORTS];
 	struct ib_mad_agent    *send_agent[MLX4_MAX_PORTS][2];
 	struct ib_ah	       *sm_ah[MLX4_MAX_PORTS];
 	spinlock_t		sm_lock;
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/qp.c ofa_kernel-1.4/drivers/infiniband/hw/mlx4/qp.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mlx4/qp.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mlx4/qp.c	2008-11-13 17:01:26.000000000 -0500
@@ -1243,8 +1243,13 @@
 	}
 
 	sqp->ud_header.lrh.virtual_lane    = !sqp->qp.ibqp.qp_num ? 15 : 0;
-	if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
+	if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE) {
 		sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
+		if (to_mdev(ib_dev)->std_mode_flag[sqp->qp.port-1]) {
+			sqp->ud_header.lrh.destination_lid = cpu_to_be16(0x61ff);
+			mlx->rlid   = cpu_to_be16(0x61ff);
+		}
+	}
 	sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
 	if (!sqp->qp.ibqp.qp_num)
 		ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_dev.h ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_dev.h
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_dev.h	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_dev.h	2008-11-13 17:01:26.000000000 -0500
@@ -346,6 +346,7 @@
 	struct mthca_mcg_table mcg_table;
 
 	struct mthca_catas_err catas_err;
+	u32                    std_mode_flag[MTHCA_MAX_PORTS];
 
 	struct mthca_uar       driver_uar;
 	struct mthca_db_table *db_tab;
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_eq.c ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_eq.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_eq.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_eq.c	2008-11-13 17:01:26.000000000 -0500
@@ -249,6 +249,9 @@
 	mthca_dbg(dev, "Port change to %s for port %d\n",
 		  active ? "active" : "down", port);
 
+	if (! active) {
+		dev->std_mode_flag[port-1] = 0;
+	}
 	record.device = &dev->ib_dev;
 	record.event  = active ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
 	record.element.port_num = port;
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_mad.c ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_mad.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_mad.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_mad.c	2008-11-13 18:35:43.000000000 -0500
@@ -197,6 +197,32 @@
 	u8 status;
 	u16 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
 
+	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_SET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO)
+	{
+		/* enter standard operational mode */
+		struct ib_port_info *pip = (struct ib_port_info *)(((struct ib_smp*)in_mad)->data);
+		if (((struct ib_smp*)in_mad)->hop_cnt > 60
+			&& ((struct ib_smp*)in_mad)->hop_ptr > 60) {
+			to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+			in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
+		} else if (((pip->portphysstate_linkdown >> 4) & 0xF) == 3) {
+			/* disabled */
+			to_mdev(ibdev)->std_mode_flag[port_num-1] = 0;
+		}
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->hop_cnt > 60
+		&& ((struct ib_smp*)in_mad)->hop_ptr > 60
+		&& in_mad->mad_hdr.method == IB_MGMT_METHOD_GET
+		&& in_mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_INFO) {
+		to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+		in_mad->mad_hdr.attr_id = IB_SMP_ATTR_PORT_INFO;
+	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
+		&& ((struct ib_smp*)in_mad)->dr_dlid == cpu_to_be16(0x61ff)) {
+		to_mdev(ibdev)->std_mode_flag[port_num-1] = 1;
+	}
+
 	/* Forward locally generated traps to the SM */
 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP &&
 	    slid == 0) {
diff -r -u ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_qp.c ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_qp.c
--- ../orig/ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_qp.c	2008-10-19 08:15:08.000000000 -0400
+++ ofa_kernel-1.4/drivers/infiniband/hw/mthca/mthca_qp.c	2008-11-13 17:01:26.000000000 -0500
@@ -1519,8 +1519,13 @@
 	}
 
 	sqp->ud_header.lrh.virtual_lane    = !sqp->qp.ibqp.qp_num ? 15 : 0;
-	if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
+	if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE) {
 		sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
+		if (dev->std_mode_flag[sqp->qp.port-1]) {
+			sqp->ud_header.lrh.destination_lid = cpu_to_be16(0x61ff);
+			mlx->rlid   = cpu_to_be16(0x61ff);
+		}
+	}
 	sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
 	if (!sqp->qp.ibqp.qp_num)
 		ib_get_cached_pkey(&dev->ib_dev, sqp->qp.port,
EOF

if [ -d ../*OFED* ]
then
	patch_ofed $1
	echo "This OFED has been patched and may now be installed"
else
	echo "This must be run within the IntelIB-*/*OFED* directory" 2>/dev/null
fi
