#!/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
	cp /tmp/todd.patch.diff kernel_patches/fixes/qib_zzzz_snoop.patch
	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"
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/Makefile ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/Makefile
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/Makefile	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/Makefile	2012-01-19 03:31:24.000000000 -0500
@@ -40,6 +40,7 @@
 	qib_sd7220.o \
 	qib_sd7220_img.o \
 	qib_iba7322.o \
+	qib_snoop.o \
 	qib_verbs.o
 
 # 6120 has no fallback if no MSI interrupts, others can do INTx
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_driver.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_driver.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_driver.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_driver.c	2012-01-23 04:21:06.000000000 -0500
@@ -706,6 +706,8 @@
 	int last;
 	u64 lval;
 	struct qib_qp *qp, *nqp;
+	struct snoop_packet *packet = NULL;
+	u32 hdr_len = 0;
 
 	l = rcd->head;
 	rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
@@ -734,6 +736,16 @@
 		/* total length */
 		tlen = qib_hdrget_length_in_bytes(rhf_addr);
 		ebuf = NULL;
+		hdr_len = (u32)rhf_addr - (u32)hdr;
+		/* applicable only for capture */
+		if (snoop_enable  && (ppd->mode_flag & QIB_PORT_CAPTURE_MODE)) {
+			packet = kzalloc(sizeof(*packet) + tlen, GFP_ATOMIC);
+			if (packet) {
+				/* copy header first */
+				packet->total_len = tlen;
+				memcpy(packet->data, hdr, hdr_len);
+			}
+		}
 		if ((dd->flags & QIB_NODMA_RTAIL) ?
 		    qib_hdrget_use_egr_buf(rhf_addr) :
 		    (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
@@ -782,6 +794,10 @@
 			crcs += qib_rcv_hdrerr(rcd, ppd, rcd->ctxt, eflags, l,
 					       etail, rhf_addr, hdr);
 		else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
+			/* copy packet data */
+			if (ebuf && packet)
+				memcpy((packet->data + hdr_len), ebuf,
+					(tlen - hdr_len));
 			qib_ib_rcv(rcd, hdr, ebuf, tlen);
 			if (crcs)
 				crcs--;
@@ -823,6 +839,16 @@
 			}
 		}
 move_along:
+		if (packet) {
+			int nomatch = 0;
+			if (ppd->filter_callback)
+				nomatch = ppd->filter_callback(hdr, ebuf,
+					ppd->filter_value);
+			if (nomatch == 0)
+				qib_snoop_send_queue_packet(ppd, packet);
+			else
+				kfree(packet);
+		}
 		l += rsize;
 		if (l >= maxcnt)
 			l = 0;
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_file_ops.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_file_ops.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_file_ops.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_file_ops.c	2012-01-19 03:31:24.000000000 -0500
@@ -2639,4 +2639,6 @@
 {
 	qib_user_remove(dd);
 	qib_diag_remove(dd);
+	if (snoop_enable)
+		qib_snoop_remove(dd);
 }
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib.h ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib.h
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib.h	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib.h	2012-01-23 04:37:48.000000000 -0500
@@ -528,6 +528,11 @@
  * clarifies things a bit. Note that to conform to IB conventions,
  * port-numbers are one-based. The first or only port is port1.
  */
+#define QIB_CHAR_DEVICES_PER_PORT	2
+/* Extract packet length from LRH header */
+#define QIB_GET_PKT_LEN(x)	(((be16_to_cpu((x)->lrh[2]) & 0x7FF)) << 2)
+#define QIB_SNOOP_DEV_INDEX	0
+#define QIB_CAPTURE_DEV_INDEX	1
 struct qib_pportdata {
 	struct qib_ibport ibport_data;
 
@@ -653,8 +658,24 @@
 	struct timer_list led_override_timer;
 	struct xmit_wait cong_stats;
 	struct timer_list symerr_clear_timer;
+
+	unsigned int mode_flag;
+	void *filter_value;
+	int (*filter_callback)(void *hdr, void *data, void *value);
+	/* lock while sending packet out */
+	spinlock_t snoop_write_lock;
+	struct qib_aux_device {
+		struct cdev *snoop_cdev;
+		struct device *snoop_class_dev;
+		/* snooping lock */
+		spinlock_t snoop_lock;
+		struct list_head snoop_queue;
+		wait_queue_head_t snoop_waitq;
+		struct qib_pportdata *pport;
+	} sc_device[QIB_CHAR_DEVICES_PER_PORT];
 };
 
+
 /* Observers. Not to be taken lightly, possibly not to ship. */
 /*
  * If a diag read or write is to (bottom <= offset <= top),
@@ -1199,6 +1220,7 @@
 extern unsigned long *qib_cpulist;
 
 extern unsigned qib_wc_pat;
+extern unsigned int snoop_enable;
 int qib_init(struct qib_devdata *, int);
 int init_chip_wc_pat(struct qib_devdata *dd, u32);
 int qib_enable_wc(struct qib_devdata *dd);
@@ -1230,6 +1252,7 @@
 /* check to see if we have to force ordering for write combining */
 int qib_unordered_wc(void);
 void qib_pio_copy(void __iomem *to, const void *from, size_t count);
+void qib_pio_copy_nonmmio(void *to, const void *from, size_t count);
 
 void qib_disarm_piobufs(struct qib_devdata *, unsigned, unsigned);
 int qib_disarm_piobufs_ifneeded(struct qib_ctxtdata *);
@@ -1257,6 +1280,26 @@
 void qib_disable_after_error(struct qib_devdata *);
 int qib_set_uevent_bits(struct qib_pportdata *, const int);
 
+#define QIB_PORT_NORMAL_MODE	0
+#define QIB_PORT_SNOOP_MODE	1U
+#define QIB_PORT_CAPTURE_MODE	2U
+#define QIB_PORT_MIX_MODE	3U
+
+struct snoop_packet {
+	struct list_head list;
+	u32 total_len;
+	u8 data[];
+};
+
+int qib_snoop_add(struct qib_devdata *);
+void qib_snoop_remove(struct qib_devdata *);
+int qib_snoop_rcv_queue_packet(struct qib_pportdata *, void *,
+				void *, u32);
+void qib_snoop_send_queue_packet(struct qib_pportdata *,
+				struct snoop_packet *);
+int snoop_get_header_size(struct qib_devdata *, struct qib_ib_header *,
+				void *, u32);
+
 /* for use in system calls, where we want to know device type, etc. */
 #define ctxt_fp(fp) \
 	(((struct qib_filedata *)(fp)->private_data)->rcd)
@@ -1386,7 +1429,7 @@
 void __qib_sdma_intr(struct qib_pportdata *);
 void qib_sdma_intr(struct qib_pportdata *);
 int qib_sdma_verbs_send(struct qib_pportdata *, struct qib_sge_state *,
-			u32, struct qib_verbs_txreq *);
+			u32, struct qib_verbs_txreq *, struct snoop_packet *);
 /* ppd->sdma_lock should be locked before calling this. */
 int qib_sdma_make_progress(struct qib_pportdata *dd);
 
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_init.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_init.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_init.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_init.c	2012-01-19 03:31:24.000000000 -0500
@@ -42,6 +42,11 @@
 #include "qib.h"
 #include "qib_common.h"
 
+unsigned int snoop_enable; /* By default (0) snooping is disabled */
+
+module_param_named(snoop_enable, snoop_enable , int, 0644);
+MODULE_PARM_DESC(snoop_enable, "snooping mode ");
+
 /*
  * min buffers we want to have per context, after driver
  */
@@ -673,6 +678,11 @@
 			lastfail = -ENETDOWN;
 			continue;
 		}
+		if (snoop_enable) {
+			ppd->filter_callback = NULL;
+			ppd->filter_value = NULL;
+			ppd->mode_flag = QIB_PORT_NORMAL_MODE;
+		}
 
 		portok++;
 	}
@@ -1449,6 +1459,8 @@
 	}
 
 	qib_verify_pioperf(dd);
+	if (snoop_enable)
+		qib_snoop_add(dd);
 bail:
 	return ret;
 }
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_pio_copy.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_pio_copy.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_pio_copy.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_pio_copy.c	2012-01-19 03:31:24.000000000 -0500
@@ -62,3 +62,42 @@
 		__raw_writel(*src++, dst++);
 #endif
 }
+
+/**
+ * qib_pio_copy_nonmmio - copy data to kernel buffer, in multiples of 32-bits
+ * @to: destination, in buffer space
+ * @from: source
+ * @count: number of 32-bit quantities to copy
+ *
+ * Copy data from kernel space to kernel space buffer, in multiples of 32 bits
+ * at a time.
+ */
+
+void qib_pio_copy_nonmmio(void *to, const void *from, size_t count)
+{
+#ifdef CONFIG_64BIT
+	u64 *dst = to;
+	const u64 *src = from;
+	const u64 *end = src + (count >> 1);
+
+	while (src < end) {
+		*dst = *src;
+		dst++;
+		src++;
+	}
+	if (count & 1)
+		*dst = *(const u32 *)src;
+#else
+	u32 *dst = to;
+	const u32 *src = from;
+	const u32 *end = src + count;
+
+	while (src < end) {
+		*dst = *src;
+		dst++;
+		src++;
+	}
+#endif
+}
+
+
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_sdma.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_sdma.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_sdma.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_sdma.c	2012-01-19 03:31:24.000000000 -0500
@@ -526,7 +526,8 @@
  */
 int qib_sdma_verbs_send(struct qib_pportdata *ppd,
 			struct qib_sge_state *ss, u32 dwords,
-			struct qib_verbs_txreq *tx)
+			struct qib_verbs_txreq *tx,
+			struct snoop_packet *packet)
 {
 	unsigned long flags;
 	struct qib_sge *sge;
@@ -537,6 +538,10 @@
 	u64 sdmadesc[2];
 	u32 dwoffset;
 	dma_addr_t addr;
+	u8 *packet_data = NULL;
+
+	if (packet)
+		packet_data = packet->data + ((tx->hdr_dwords-2) << 2);
 
 	spin_lock_irqsave(&ppd->sdma_lock, flags);
 
@@ -593,6 +598,10 @@
 				      dw << 2, DMA_TO_DEVICE);
 		if (dma_mapping_error(&ppd->dd->pcidev->dev, addr))
 			goto unmap;
+		if (packet) {
+			memcpy(packet_data, sge->vaddr, len);
+			packet_data += len;
+		}
 		sdmadesc[0] = 0;
 		make_sdma_desc(ppd, sdmadesc, (u64) addr, dw, dwoffset);
 		/* SDmaUseLargeBuf has to be set in every descriptor */
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_snoop.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_snoop.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_snoop.c	1969-12-31 19:00:00.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_snoop.c	2012-01-23 06:52:07.000000000 -0500
@@ -0,0 +1,974 @@
+/*
+ * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
+ * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/*
+ * This file implements a raw read/raw write interface for snooping raw
+ * packets from the wire and injecting raw packets to the wire.
+ *
+ * Other things that this interface could do at somepoint are:
+ *   - Allow packets to be injected back into the stack
+ *   - Provide an intercept for packets coming from the upper layers to
+ *     move them back into user-space.
+ */
+
+#include <linux/pci.h>
+#include <linux/vmalloc.h>
+#include <linux/uaccess.h>
+
+#include <rdma/ib_user_mad.h> /* for ioctl constants */
+#include <rdma/ib_smi.h>
+
+
+#include "qib.h"
+#include "qib_verbs.h"
+#include "qib_common.h"
+#include <linux/poll.h>
+
+#define QIB_SNOOP_IOC_MAGIC IB_IOCTL_MAGIC
+#define QIB_SNOOP_IOC_BASE_SEQ 0x80
+/* This starts our ioctl sequence
+ * numbers *way* off from the ones
+ * defined in ib_core
+					*/
+#define QIB_SNOOP_IOCGETLINKSTATE \
+	_IO(QIB_SNOOP_IOC_MAGIC, QIB_SNOOP_IOC_BASE_SEQ)
+#define QIB_SNOOP_IOCSETLINKSTATE \
+	_IO(QIB_SNOOP_IOC_MAGIC, QIB_SNOOP_IOC_BASE_SEQ+1)
+#define QIB_SNOOP_IOCCLEARQUEUE \
+	_IO(QIB_SNOOP_IOC_MAGIC, QIB_SNOOP_IOC_BASE_SEQ+2)
+#define QIB_SNOOP_IOCCLEARFILTER \
+	_IO(QIB_SNOOP_IOC_MAGIC, QIB_SNOOP_IOC_BASE_SEQ+3)
+#define QIB_SNOOP_IOCSETFILTER \
+	_IO(QIB_SNOOP_IOC_MAGIC, QIB_SNOOP_IOC_BASE_SEQ+4)
+
+/* local prototypes */
+static int qib_snoop_open(struct inode *in, struct file *fp);
+static unsigned int qib_snoop_poll(struct file *fp,
+					struct poll_table_struct *wait);
+static ssize_t qib_snoop_read(struct file *fp, char __user *data,
+				size_t pkt_len, loff_t *off);
+static int qib_snoop_release(struct inode *in, struct file *fp);
+
+static int qib_ioctl(struct inode *inode, struct file *fp,
+			unsigned int cmd, unsigned long arg);
+
+static ssize_t qib_snoop_write(struct file *fp, const char __user *data,
+				  size_t pkt_len, loff_t *off);
+
+#include <linux/delay.h>
+
+struct qib_packet_filter_command {
+	int opcode;
+	int length;
+	void *value_ptr;
+};
+
+enum qib_packet_filter_opcodes {
+	FILTER_BY_LID,
+	FILTER_BY_DLID,
+	FILTER_BY_MAD_MGMT_CLASS,
+	FILTER_BY_QP_NUMBER,
+	FILTER_BY_PKT_TYPE,
+	FILTER_BY_SERVICE_LEVEL,
+	FILTER_BY_PKEY
+};
+
+static const struct file_operations snoop_file_ops = {
+	.owner = THIS_MODULE,
+	.open = qib_snoop_open,
+	.read = qib_snoop_read,
+	.ioctl = qib_ioctl,
+	.poll = qib_snoop_poll,
+	.write = qib_snoop_write,
+	.release = qib_snoop_release
+};
+
+struct qib_filter_array {
+	int (*filter)(void *, void *, void *);
+};
+
+static int qib_filter_lid(void *ibhdr, void *packet_data, void *value);
+static int qib_filter_dlid(void *ibhdr, void *packet_data, void *value);
+static int qib_filter_mad_mgmt_class(void *ibhdr, void *packet_data,
+				     void *value);
+static int qib_filter_qp_number(void *ibhdr, void *packet_data, void *value);
+static int qib_filter_ibpacket_type(void *ibhdr, void *packet_data,
+				    void *value);
+static int qib_filter_ib_service_level(void *ibhdr, void *packet_data,
+				       void *value);
+static int qib_filter_ib_pkey(void *ibhdr, void *packet_data, void *value);
+
+static struct qib_filter_array qib_filters[] = {
+	{ qib_filter_lid },
+	{ qib_filter_dlid },
+	{ qib_filter_mad_mgmt_class },
+	{ qib_filter_qp_number },
+	{ qib_filter_ibpacket_type },
+	{ qib_filter_ib_service_level },
+	{ qib_filter_ib_pkey }
+};
+
+#define QIB_MAX_FILTERS	ARRAY_SIZE(qib_filters)
+#define QIB_DRV_NAME		"ib_qib"
+#define QIB_MAJOR		233
+#define QIB_USER_MINOR_BASE	0
+#define QIB_DIAG_MINOR_BASE	129
+#define QIB_SNOOP_MINOR_BASE	160
+#define QIB_CAPTURE_MINOR_BASE	200
+#define QIB_NMINORS		255
+#define PORT_BITS		2
+#define PORT_MASK		((1U << PORT_BITS) - 1)
+#define GET_HCA(x)		((unsigned int)((x) >> PORT_BITS))
+#define GET_PORT(x)		((unsigned int)((x) & PORT_MASK))
+
+int qib_snoop_add(struct qib_devdata *dd)
+{
+	char name[32];
+	int ret = 0;
+	int i;
+	int j;
+	int minor = 0;
+
+	for (i = 0; i < dd->num_pports; i++) {
+		spin_lock_init(&dd->pport[i].snoop_write_lock);
+		for (j = 0; j < QIB_CHAR_DEVICES_PER_PORT; j++) {
+			spin_lock_init(&dd->pport[i].sc_device[j].snoop_lock);
+			INIT_LIST_HEAD(
+				&(dd->pport[i].sc_device[j].snoop_queue));
+			init_waitqueue_head(
+				&dd->pport[i].sc_device[j].snoop_waitq);
+
+			if (j == 0) {
+				minor = (((dd->unit << PORT_BITS) | i)) +
+					QIB_SNOOP_MINOR_BASE;
+				snprintf(name, sizeof(name),
+					"ipath_snoop_%02d_%02d", dd->unit, i+1);
+			} else {
+				minor = (((dd->unit << PORT_BITS) | i)) +
+						QIB_CAPTURE_MINOR_BASE;
+				snprintf(name, sizeof(name),
+					"ipath_capture_%02d_%02d",
+					dd->unit, i+1);
+			}
+
+			ret = qib_cdev_init(
+				minor, name,
+				&snoop_file_ops,
+				&dd->pport[i].sc_device[j].snoop_cdev,
+				&dd->pport[i].sc_device[j].snoop_class_dev);
+			if (ret) {
+				qib_dev_err(dd, "Couldn't create %s device: %d",
+					name, ret);
+				i--;
+				if (i != dd->num_pports) {
+					for (; i >= 0 ; i--) {
+						for (j = 0;
+						j < QIB_CHAR_DEVICES_PER_PORT;
+						j++)
+							qib_cdev_cleanup(
+							 &dd->pport[i].
+							 sc_device[j].
+							 snoop_cdev,
+							 &dd->pport[i].
+							 sc_device[j].
+							 snoop_class_dev);
+						dd->pport[i].mode_flag =
+							QIB_PORT_NORMAL_MODE;
+					}
+				}
+				return ret;
+			}
+		}
+		printk(KERN_INFO "qib%d: snoop device for hca %02d enabled"
+				 " for Port %02d\n"
+				"qib%d: capture device for hca %02d enabled"
+				" for port %02d\n",
+			dd->unit, dd->unit, i+1, dd->unit, dd->unit, i+1);
+		dd->pport[i].mode_flag = QIB_PORT_NORMAL_MODE;
+	}
+	return ret;
+}
+
+/* this must be called w/ dd->snoop_in_lock held */
+static void drain_snoop_list(struct qib_aux_device *sc_device)
+{
+	struct list_head *pos, *q;
+	struct snoop_packet *packet;
+
+	list_for_each_safe(pos, q, &(sc_device->snoop_queue)) {
+		packet = list_entry(pos, struct snoop_packet, list);
+		list_del(pos);
+		kfree(packet);
+	}
+}
+
+void qib_snoop_remove(struct qib_devdata *dd)
+{
+	unsigned long flags = 0;
+	int i;
+	int j;
+
+	for (i = 0; i < dd->num_pports; i++) {
+		for (j = 0; j < QIB_CHAR_DEVICES_PER_PORT; j++) {
+			spin_lock_irqsave(&dd->pport[i].sc_device[j].snoop_lock,
+				flags);
+			drain_snoop_list(&dd->pport[i].sc_device[j]);
+			qib_cdev_cleanup(&dd->pport[i].sc_device[j].snoop_cdev,
+				&dd->pport[i].sc_device[j].snoop_class_dev);
+			spin_unlock_irqrestore(
+				&dd->pport[i].sc_device[j].snoop_lock,
+				flags);
+		}
+	}
+	dd->pport[i].mode_flag = QIB_PORT_NORMAL_MODE;
+}
+
+static int qib_snoop_open(struct inode *in, struct file *fp)
+{
+	int unit = iminor(in);
+	int devnum;
+	int portnum = 0;
+	int ret;
+	int mode_flag = QIB_PORT_NORMAL_MODE;
+	unsigned long flags;
+	struct qib_devdata *dd;
+
+	mutex_lock(&qib_mutex);
+
+	if (unit >= QIB_CAPTURE_MINOR_BASE) {
+		unit -= QIB_CAPTURE_MINOR_BASE;
+		devnum = 1;
+		mode_flag = QIB_PORT_CAPTURE_MODE;
+	} else {
+		unit -= QIB_SNOOP_MINOR_BASE;
+		devnum = 0;
+		mode_flag = QIB_PORT_SNOOP_MODE;
+	}
+
+	dd = qib_lookup(GET_HCA(unit));
+	if (dd == NULL || !(dd->flags & QIB_PRESENT) ||
+	    !dd->kregbase) {
+		ret = -ENODEV;
+		goto bail;
+	}
+	portnum = GET_PORT(unit);
+
+	spin_lock_irqsave(&dd->pport[portnum].sc_device[devnum].snoop_lock,
+		flags);
+
+	if (dd->pport[portnum].mode_flag & mode_flag) {
+		ret = -EBUSY;
+		spin_unlock_irqrestore(
+			&dd->pport[portnum].sc_device[devnum].snoop_lock,
+			flags);
+		goto bail;
+	}
+
+	drain_snoop_list(&dd->pport[portnum].sc_device[devnum]);
+	spin_unlock_irqrestore(
+		&dd->pport[portnum].sc_device[devnum].snoop_lock, flags);
+	if (devnum)
+		printk(KERN_ALERT
+			"capture device for hca %02d port %02d is opened\n",
+			GET_HCA(unit), portnum+1);
+	else
+		printk(KERN_ALERT
+			"snoop device for hca %02d port %02d is opened\n",
+			GET_HCA(unit), portnum+1);
+
+	dd->pport[portnum].sc_device[devnum].pport = &dd->pport[portnum];
+	fp->private_data = &dd->pport[portnum].sc_device[devnum];
+	ret = 0;
+	dd->pport[portnum].mode_flag |= mode_flag;
+
+bail:
+	mutex_unlock(&qib_mutex);
+
+	return ret;
+}
+
+static int qib_snoop_release(struct inode *in, struct file *fp)
+{
+	struct qib_aux_device *sc_device = fp->private_data;
+	struct qib_pportdata *pport = sc_device->pport;
+	unsigned long flags = 0;
+	int devnum = iminor(in);
+
+	if (devnum >= QIB_CAPTURE_MINOR_BASE)
+		devnum = 1;
+	else
+		devnum = 0;
+
+	spin_lock_irqsave(&sc_device->snoop_lock, flags);
+	if (devnum)
+		pport->mode_flag = pport->mode_flag & (~QIB_PORT_CAPTURE_MODE);
+	else
+		pport->mode_flag = pport->mode_flag & (~QIB_PORT_SNOOP_MODE);
+
+	drain_snoop_list(sc_device);
+	spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+
+	if (devnum)
+		printk(KERN_ALERT"capture device for hca %02d "
+				 "port %02d is closed\n",
+				 pport->dd->unit, pport->port);
+	else
+		printk(KERN_ALERT"snoop device for hca %02d "
+				 "port %02d is closed\n",
+				 pport->dd->unit, pport->port);
+
+	fp->private_data = NULL;
+	return 0;
+}
+
+static unsigned int qib_snoop_poll(struct file *fp,
+		struct poll_table_struct *wait)
+{
+	struct qib_aux_device *sc_device = fp->private_data;
+	int ret = 0;
+	unsigned long flags = 0;
+
+	spin_lock_irqsave(&sc_device->snoop_lock, flags);
+
+	poll_wait(fp, &sc_device->snoop_waitq, wait);
+	if (!list_empty(&sc_device->snoop_queue))
+		ret |= POLLIN | POLLRDNORM;
+
+	spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+	return ret;
+
+}
+
+static ssize_t qib_snoop_read(struct file *fp, char __user *data,
+			       size_t pkt_len, loff_t *off)
+{
+	struct qib_aux_device *sc_device = fp->private_data;
+	ssize_t ret = 0;
+	unsigned long flags = 0;
+	struct snoop_packet *packet = NULL;
+
+	spin_lock_irqsave(&sc_device->snoop_lock, flags);
+
+	while (list_empty(&sc_device->snoop_queue)) {
+		spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+
+		if (fp->f_flags & O_NONBLOCK)
+			return -EAGAIN;
+
+
+		if (wait_event_interruptible(sc_device->snoop_waitq,
+					!list_empty(&sc_device->snoop_queue)))
+			return -ERESTARTSYS;
+
+		spin_lock_irqsave(&sc_device->snoop_lock, flags);
+	}
+
+	if (!list_empty(&(sc_device->snoop_queue))) {
+		packet = list_entry(sc_device->snoop_queue.next,
+				struct snoop_packet, list);
+		list_del(&packet->list);
+		spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+		if (pkt_len >= packet->total_len) {
+			ret = copy_to_user(data, packet->data,
+				packet->total_len);
+			if (ret < 0)
+				goto out_free_packet;
+			else
+				ret = packet->total_len;
+		} else
+			ret = -EINVAL;
+out_free_packet:
+		kfree(packet);
+	} else
+		spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+
+	return ret;
+}
+
+static int qib_ioctl(struct inode *inode, struct file *fp,
+		unsigned int cmd, unsigned long arg)
+{
+	struct qib_aux_device *sc_device = fp->private_data;
+	struct qib_pportdata *ppd = sc_device->pport;
+	struct qib_devdata *dd = ppd->dd;
+	int ret = 0;
+	int value = 0;
+	u8 physState = 0;
+	u8 linkState = 0;
+	u16 devState = 0;
+	unsigned long flags;
+	unsigned long *argp = NULL;
+	struct qib_packet_filter_command filter_cmd = {0};
+
+	if (((_IOC_DIR(cmd) & _IOC_READ)
+	&& !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd)))
+	|| ((_IOC_DIR(cmd) & _IOC_WRITE)
+	&& !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd)))) {
+		ret = -EFAULT;
+	} else if (!capable(CAP_SYS_ADMIN)) {
+		ret = -EPERM;
+	} else if (sc_device != (&ppd->sc_device[QIB_SNOOP_DEV_INDEX])
+		&& cmd != QIB_SNOOP_IOCCLEARQUEUE
+		&& cmd != QIB_SNOOP_IOCCLEARFILTER
+		&& cmd != QIB_SNOOP_IOCSETFILTER) {
+		/* Capture devices are allowed only 3 operations
+		 * 1.Clear capture queue
+		 * 2.Clear capture filter
+		 * 3.Set capture filter
+		 * Other are invalid.
+		 */
+		ret = -EINVAL;
+	} else {
+		switch (cmd) {
+		case QIB_SNOOP_IOCSETLINKSTATE:
+			ret = __get_user(value, (int __user *) arg);
+			if (ret !=  0)
+				break;
+
+			physState = (value >> 4) & 0xF;
+			linkState = value & 0xF;
+
+			switch (linkState) {
+			case IB_PORT_NOP:
+				if (physState == 0)
+					break;
+					/* fall through */
+			case IB_PORT_DOWN:
+				switch (physState) {
+				case 0:
+					if (dd->f_ibphys_portstate &&
+				(dd->f_ibphys_portstate(ppd->lastibcstat)
+					& 0xF & IB_PHYSPORTSTATE_SLEEP))
+						devState =
+						QIB_IB_LINKDOWN_SLEEP;
+					else
+						devState =
+						QIB_IB_LINKDOWN;
+						break;
+				case 1:
+					devState = QIB_IB_LINKDOWN_SLEEP;
+					break;
+				case 2:
+					devState = QIB_IB_LINKDOWN;
+					break;
+				case 3:
+					devState = QIB_IB_LINKDOWN_DISABLE;
+					break;
+				default:
+					ret = -EINVAL;
+					goto done;
+					break;
+				}
+				ret = qib_set_linkstate(ppd, devState);
+				break;
+			case IB_PORT_ARMED:
+				if (!(dd->flags &
+				(QIB_IB_LINKARM | QIB_IB_LINKACTIVE))) {
+						ret = -EINVAL;
+						break;
+					}
+				ret = qib_set_linkstate(ppd, QIB_IB_LINKARM);
+				break;
+			case IB_PORT_ACTIVE:
+				if (!(dd->flags & QIB_IB_LINKARM)) {
+					ret = -EINVAL;
+					break;
+				}
+				ret = qib_set_linkstate(ppd, QIB_IB_LINKACTIVE);
+				break;
+			default:
+				ret = -EINVAL;
+				break;
+			}
+
+			if (ret)
+				break;
+			/* fall through */
+
+		case QIB_SNOOP_IOCGETLINKSTATE:
+			value = dd->f_iblink_state(ppd->lastibcstat & 0xF) << 4;
+			value |= ((ppd->lastibcstat >> 4) & 0x3) + 1;
+
+			ret = __put_user(value, (int __user *)arg);
+			break;
+
+		case QIB_SNOOP_IOCCLEARQUEUE:
+			spin_lock_irqsave(&sc_device->snoop_lock, flags);
+			drain_snoop_list(sc_device);
+			spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+			break;
+
+		case QIB_SNOOP_IOCCLEARFILTER:
+			if (ppd->filter_callback == NULL) {
+				/* no filter set just return true */
+				ret = 0;
+				break;
+			}
+			/* Drain packets first */
+			spin_lock_irqsave(&sc_device->snoop_lock, flags);
+			drain_snoop_list(sc_device);
+			ppd->filter_callback = NULL;
+			kfree(ppd->filter_value);
+			ppd->filter_value = NULL;
+			spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+			break;
+
+		case QIB_SNOOP_IOCSETFILTER:
+			/* just copy command structure */
+			argp = (unsigned long *)arg;
+			ret = copy_from_user(&filter_cmd, (u8 *)argp,
+				sizeof(filter_cmd));
+			if (ret < 0) {
+				printk(KERN_ALERT
+					"Error copying filter command\n");
+				break;
+			}
+			if (filter_cmd.opcode >= QIB_MAX_FILTERS) {
+				printk(KERN_ALERT"Invalid opcode in request\n");
+				ret = -EINVAL;
+				break;
+			}
+			ppd->filter_value = kzalloc(
+						filter_cmd.length * sizeof(u8),
+						GFP_KERNEL);
+			if (ppd->filter_value == NULL) {
+				printk(KERN_ALERT"Not enough memory\n");
+				ret = -ENOMEM;
+				break;
+			}
+			/* copy remaining data from userspace */
+			ret = copy_from_user((u8 *)ppd->filter_value,
+					(u8 *)filter_cmd.value_ptr,
+					filter_cmd.length);
+			if (ret < 0) {
+				printk(KERN_ALERT"Error copying filter data\n");
+				break;
+			}
+			/* Drain packets first */
+			spin_lock_irqsave(&sc_device->snoop_lock, flags);
+			drain_snoop_list(sc_device);
+			ppd->filter_callback =
+				qib_filters[filter_cmd.opcode].filter;
+			spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+			break;
+
+		default:
+			ret = -ENOTTY;
+			break;
+		}
+	}
+done:
+	return ret;
+}
+
+
+static ssize_t qib_pio_send_pkt(struct qib_pportdata *ppd,
+				u32 *data, u32 pkt_len)
+{
+	int i;
+	u64 pbc;
+	u32 __iomem *piobuf;
+	u32 pnum, control, len;
+	struct qib_devdata *dd = ppd->dd;
+	u32 dwords = pkt_len >> 2;
+	unsigned long flags;
+	ssize_t retVal = -EINVAL;
+
+	i = 0;
+	len = dwords + 1;
+	control = dd->f_setpbc_control(ppd, len, 0,
+		  (((u8 *)data)[0] >> 4) & 0xf);
+	pbc = ((u64) control << 32) | len;
+	while (!(piobuf = dd->f_getsendbuf(ppd, pbc, &pnum))) {
+		if (i > 15) {
+			qib_dbg("Couldn't get pio buffer for send\n");
+			retVal = -ENOMEM;
+			goto Err;
+		}
+		i++;
+		/* lets try to flush all of it */
+		dd->f_sendctrl(ppd, QIB_SENDCTRL_DISARM_ALL);
+		udelay(100);
+	}
+	spin_lock_irqsave(&ppd->snoop_write_lock, flags);
+	/* disable header check on this packet, since it can't be valid */
+	dd->f_txchk_change(dd, pnum, 1, TXCHK_CHG_TYPE_DIS1, NULL);
+	writeq(pbc, piobuf);
+	qib_flush_wc();
+	if (dd->flags & QIB_PIO_FLUSH_WC) {
+		qib_flush_wc();
+		qib_pio_copy(piobuf + 2, data, dwords - 1);
+		qib_flush_wc();
+		__raw_writel(data[dwords - 1], piobuf + dwords + 1);
+	} else
+		qib_pio_copy(piobuf + 2, data, dwords);
+	if (dd->flags & QIB_USE_SPCL_TRIG) {
+		u32 spcl_off = (pnum >= dd->piobcnt2k) ? 2047 : 1023;
+
+		qib_flush_wc();
+		__raw_writel(0xaebecede, piobuf + spcl_off);
+	}
+	qib_sendbuf_done(dd, pnum);
+	qib_flush_wc();
+	/* and re-enable hdr check */
+	dd->f_txchk_change(dd, pnum, 1, TXCHK_CHG_TYPE_ENAB1, NULL);
+	spin_unlock_irqrestore(&ppd->snoop_write_lock, flags);
+	retVal = pkt_len;
+Err:
+	return retVal;
+}
+
+
+static ssize_t qib_snoop_write(struct file *fp, const char __user *data,
+					size_t pkt_len, loff_t *off)
+{
+	struct qib_aux_device *sc_device = fp->private_data;
+	struct qib_pportdata *ppd = sc_device->pport;
+	struct qib_devdata *dd = ppd->dd;
+	ssize_t ret = 0;
+	u32 *buffer = NULL;
+	u32 plen, clen;
+
+	/* capture device should not be entertaining writes */
+	if (sc_device != (&ppd->sc_device[QIB_SNOOP_DEV_INDEX])) {
+		ret = -EINVAL;
+		goto bail;
+	}
+
+	if (pkt_len == 0)
+		goto bail;
+
+	if (pkt_len & 3) {
+		ret = -EINVAL;
+		goto bail;
+	}
+
+	clen = pkt_len >> 2;
+
+	if (!dd || !(dd->flags & QIB_PRESENT) ||
+			!dd->kregbase) {
+		ret = -ENODEV;
+		goto bail;
+	}
+
+	if (!(dd->flags & QIB_INITTED)) {
+		/* no hardware, freeze, etc. */
+		ret = -ENODEV;
+		goto bail;
+	}
+
+	plen = sizeof(u32) + pkt_len;
+
+	if ((plen + 4) > ppd->ibmaxlen) {
+		ret = -EINVAL;
+		goto bail;  /* before writing pbc */
+	}
+
+	buffer = vmalloc(plen);
+	if (!buffer) {
+		ret = -ENOMEM;
+		goto bail;
+	}
+	if (copy_from_user(buffer,
+		(const void __user *) (unsigned long) data, pkt_len)) {
+		ret = -EFAULT;
+		goto bail;
+	}
+
+	ret = qib_pio_send_pkt(ppd, buffer, pkt_len);
+
+bail:
+	if (buffer != NULL)
+		vfree(buffer);
+
+	return ret;
+}
+
+int snoop_get_header_size(struct qib_devdata *dd,
+			struct qib_ib_header *hdr,
+			void *data, u32 tlen)
+{
+	int lnh, header_size = -1;
+	u8 opcode, opcode_major;
+	struct qib_other_headers *ohdr;
+
+	lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
+
+	if (lnh == QIB_LRH_BTH)
+		ohdr = &hdr->u.oth;
+	else if (lnh == QIB_LRH_GRH)
+		ohdr = &hdr->u.l.oth;
+	else
+		goto bail;
+
+	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
+
+	opcode_major = (opcode >> 5) & 0x7;
+
+	switch (opcode_major) {
+	case 0x03: /* UD */
+		if (lnh == QIB_LRH_BTH)
+			header_size = 8 + 12 + 8 /* LRH + BTH + DETH */;
+		else if (lnh == QIB_LRH_GRH) {
+
+			/* LRH + GRH + BTH + DETH */;
+			header_size = 8 + 40 + 12 + 8;
+			/* Some of the header data is in the data segment */
+			if (dd->rcvhdrentsize == 16)
+				header_size -= 12;
+		} else
+			header_size = -1;
+
+		break;
+	case 0x0: /* RC */
+	case 0x1: /* UC */
+	case 0x2: /* RD */
+	default:
+		header_size = -1;
+		break;
+	}
+
+bail:
+	return header_size;
+}
+
+static void qib_snoop_list_add_tail(struct snoop_packet *packet,
+					struct qib_pportdata *ppd,
+					int dev_index)
+{
+	unsigned long flags = 0;
+	struct qib_aux_device *sc_device = &ppd->sc_device[dev_index];
+
+	spin_lock_irqsave(&sc_device->snoop_lock, flags);
+	list_add_tail(&(packet->list), &sc_device->snoop_queue);
+	spin_unlock_irqrestore(&sc_device->snoop_lock, flags);
+	wake_up_interruptible(&sc_device->snoop_waitq);
+}
+
+void qib_snoop_send_queue_packet(struct qib_pportdata *ppd,
+				struct snoop_packet *packet)
+{
+	struct snoop_packet *pcopy = NULL;
+
+	/* If we are dealing with mix mode then we need to make another copy
+	 * of same packet and queue it in snoop device as well.
+	 * However if we do not get sufficient memory here then we just
+	 * add packet to capture queue by default so that we atleast have one
+	 * packet with us in capture queue.
+	 */
+	if (ppd->mode_flag == QIB_PORT_MIX_MODE) {
+		pcopy = kmalloc(sizeof(*pcopy) + packet->total_len, GFP_ATOMIC);
+		if (pcopy == NULL)
+			goto add_to_capture_queue;
+		else
+			memcpy(pcopy, packet,
+				packet->total_len + sizeof(*pcopy));
+		}
+
+add_to_capture_queue:
+	if (ppd->mode_flag & QIB_PORT_CAPTURE_MODE)
+		qib_snoop_list_add_tail(packet, ppd, QIB_CAPTURE_DEV_INDEX);
+
+	if (ppd->mode_flag & QIB_PORT_SNOOP_MODE) {
+		if (pcopy)
+			packet = pcopy;
+		qib_snoop_list_add_tail(packet, ppd, QIB_SNOOP_DEV_INDEX);
+	}
+}
+
+int qib_snoop_rcv_queue_packet(struct qib_pportdata *port, void *rhdr,
+				void *data, u32 tlen)
+{
+	int rc = 0;
+	int header_size = 0;
+	struct qib_ib_header *hdr = rhdr;
+	struct snoop_packet *packet = NULL;
+	struct snoop_packet *pcopy = NULL;
+
+	header_size = snoop_get_header_size(port->dd, hdr, data, tlen);
+	if (header_size <= 0) {
+		rc = 0;
+		goto bail;
+	}
+
+	packet = kmalloc(sizeof(struct snoop_packet) + tlen,
+					GFP_ATOMIC);
+	if (packet == NULL)
+		return 0;
+	else {
+		memcpy(packet->data, rhdr, header_size);
+		memcpy(packet->data + header_size, data,
+		tlen - header_size);
+		packet->total_len = tlen;
+	}
+
+	if (port->mode_flag == QIB_PORT_MIX_MODE) {
+		pcopy = kmalloc(sizeof(*pcopy) + packet->total_len, GFP_ATOMIC);
+		if (pcopy == NULL)
+			goto add_to_capture_queue;
+		else
+			memcpy(pcopy, packet,
+				packet->total_len + sizeof(*pcopy));
+	}
+
+add_to_capture_queue:
+	if (port->mode_flag & QIB_PORT_CAPTURE_MODE) {
+		qib_snoop_list_add_tail(packet, port, QIB_CAPTURE_DEV_INDEX);
+		rc = 0;
+	}
+
+	if (port->mode_flag & QIB_PORT_SNOOP_MODE) {
+		if (pcopy)
+			packet = pcopy;
+		else
+			rc = 1;
+		qib_snoop_list_add_tail(packet, port, QIB_SNOOP_DEV_INDEX);
+	}
+
+bail:
+	return rc;
+}
+
+static int qib_filter_lid(void *ibhdr, void *packet_data, void *value)
+{
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	if (*((u16 *)value) == be16_to_cpu(hdr->lrh[3]))
+		return 0; /* matched */
+	return 1; /* Not matched */
+}
+
+static int qib_filter_dlid(void *ibhdr, void *packet_data, void *value)
+{
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	if (*((u16 *)value) == be16_to_cpu(hdr->lrh[1]))
+		return 0;
+	return 1;
+}
+
+static int qib_filter_mad_mgmt_class(void *ibhdr, void *packet_data,
+					 void *value)
+{
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	struct qib_other_headers *ohdr = NULL;
+	struct ib_smp *smp = NULL;
+	u32 qpn = 0;
+
+	/* packet_data could be null if only header is captured */
+	if (packet_data == NULL)
+		return 1;
+	/* Check for GRH */
+	if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
+		ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
+	else
+		ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
+	qpn = be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF;
+	if (qpn <= 1) {
+		smp = (struct ib_smp *)packet_data;
+		if (*((u8 *)value) == smp->mgmt_class)
+			return 0;
+		else
+			return 1;
+		}
+	return 1;
+}
+
+static int qib_filter_qp_number(void *ibhdr, void *packet_data, void *value)
+{
+
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	struct qib_other_headers *ohdr = NULL;
+
+	/* Check for GRH */
+	if ((be16_to_cpu(hdr->lrh[0]) & 3) == QIB_LRH_BTH)
+		ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
+	else
+		ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
+	if (*((u32 *)value) == (be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF))
+		return 0;
+	return 1;
+}
+
+
+static int qib_filter_ibpacket_type(void *ibhdr, void *packet_data,
+					void *value)
+{
+	u32 lnh = 0;
+	u8 opcode = 0;
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	struct qib_other_headers *ohdr = NULL;
+
+	lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
+
+	if (lnh == QIB_LRH_BTH)
+		ohdr = &hdr->u.oth;
+	else if (lnh == QIB_LRH_GRH)
+		ohdr = &hdr->u.l.oth;
+	else
+		return 1;
+
+	opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
+
+	if (*((u8 *)value) == ((opcode >> 5) & 0x7))
+		return 0;
+	return 1;
+}
+
+static int qib_filter_ib_service_level(void *ibhdr, void *packet_data,
+					   void *value)
+{
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+
+	if ((*((u8 *)value)) == (be16_to_cpu(hdr->lrh[0] >> 4) & 0xF))
+		return 0;
+	return 1;
+}
+
+static int qib_filter_ib_pkey(void *ibhdr, void *packet_data, void *value)
+{
+
+	u32 lnh = 0;
+	struct qib_ib_header *hdr = (struct qib_ib_header *)ibhdr;
+	struct qib_other_headers *ohdr = NULL;
+
+	lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
+	if (lnh == QIB_LRH_BTH)
+		ohdr = &hdr->u.oth;
+	else if (lnh == QIB_LRH_GRH)
+		ohdr = &hdr->u.l.oth;
+	else
+		return 1;
+
+	if (*((u16 *)value) == ((be32_to_cpu(ohdr->bth[0])) & 0xFFFF))
+		return 0;
+	return 1;
+}
diff -ubBrN ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_verbs.c ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_verbs.c
--- ofa_kernel-1.5.4/drivers/infiniband/hw/qib/qib_verbs.c	2012-01-23 06:44:16.000000000 -0500
+++ ofa_kernel-1.5.4_changed/drivers/infiniband/hw/qib/qib_verbs.c	2012-01-19 03:32:49.000000000 -0500
@@ -776,6 +776,15 @@
 	if (unlikely(tlen < 24))
 		goto drop;
 
+	if (snoop_enable  &&  (ppd->mode_flag & QIB_PORT_SNOOP_MODE)) {
+		int nomatch = 0;
+		if (ppd->filter_callback)
+			nomatch = ppd->filter_callback(hdr, data,
+				ppd->filter_value);
+		if (nomatch == 0 &&
+			qib_snoop_rcv_queue_packet(ppd, rhdr, data, tlen))
+			goto drop;
+	}
 	/* Check for a valid destination LID (see ch. 7.11.1). */
 	lid = be16_to_cpu(hdr->lrh[1]);
 	if (lid < QIB_MULTICAST_LID_BASE) {
@@ -946,11 +955,17 @@
 #endif
 
 static void copy_io(u32 __iomem *piobuf, struct qib_sge_state *ss,
-		    u32 length, unsigned flush_wc)
+		    u32 length, unsigned flush_wc, struct snoop_packet *packet,
+		    u8 *data_orig)
 {
 	u32 extra = 0;
 	u32 data = 0;
 	u32 last;
+	u32 *packet_data = NULL;
+
+	/* This ensures copying word at a time */
+	if (packet)
+		packet_data = (u32 *)data_orig;
 
 	qib_cdbg(VPKT, "UDATA len %u (bytes) %u %lu\n", length, ss->num_sge,
 		(unsigned long)ss->sge.vaddr & (sizeof(u32) - 1));
@@ -984,6 +999,10 @@
 				}
 				__raw_writel(data, piobuf);
 				piobuf++;
+				if (packet_data) {
+					*packet_data = data;
+					packet_data++;
+				}
 				extra = 0;
 				data = 0;
 			} else {
@@ -1010,6 +1029,10 @@
 				data = get_upper_bits(v, ushift);
 				piobuf++;
 				addr++;
+				if (packet_data) {
+					*packet_data = data;
+					packet_data++;
+				}
 				l -= sizeof(u32);
 			}
 			/*
@@ -1027,6 +1050,10 @@
 					}
 					__raw_writel(data, piobuf);
 					piobuf++;
+					if (packet_data) {
+						*packet_data = data;
+						packet_data++;
+					}
 					extra = 0;
 					data = 0;
 				} else {
@@ -1054,6 +1081,11 @@
 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
 			piobuf += w - 1;
 			last = ((u32 *) ss->sge.vaddr)[w - 1];
+			if (packet_data) {
+				qib_pio_copy_nonmmio(packet_data,
+					ss->sge.vaddr, w);
+				packet_data += w;
+			}
 			break;
 		} else {
 			u32 w = len >> 2;
@@ -1061,6 +1093,11 @@
 			qib_dump_data("UDATA", (__be32 *) ss->sge.vaddr, w);
 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
 			piobuf += w;
+			if (packet_data) {
+				qib_pio_copy_nonmmio(packet_data,
+					ss->sge.vaddr, w);
+				packet_data += w;
+			}
 
 			extra = len & (sizeof(u32) - 1);
 			if (extra) {
@@ -1307,12 +1344,13 @@
 	u32 control;
 	u32 ndesc;
 	int ret;
+	struct snoop_packet *packet = NULL;
 
 	tx = qp->s_tx;
 	if (tx) {
 		qp->s_tx = NULL;
 		/* resend previously constructed packet */
-		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
+		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx, NULL);
 		goto bail;
 	}
 
@@ -1336,6 +1374,13 @@
 	if (plen + 1 > dd->piosize2kmax_dwords)
 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
 
+	if (snoop_enable && ppd->mode_flag) {
+		packet = kzalloc(sizeof(*packet)+QIB_GET_PKT_LEN(hdr),
+				GFP_ATOMIC);
+		if (packet)
+			packet->total_len = QIB_GET_PKT_LEN(hdr);
+	}
+
 	if (len) {
 		/*
 		 * Don't try to DMA if it takes more descriptors than
@@ -1356,7 +1401,9 @@
 		tx->txreq.addr = dev->pio_hdrs_phys +
 			tx->hdr_inx * sizeof(struct qib_pio_header);
 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
-		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
+		if (packet)
+			memcpy(packet->data, hdr, (hdrwords << 2));
+		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx, packet);
 		goto bail;
 	}
 
@@ -1369,6 +1416,12 @@
 	phdr->pbc[1] = cpu_to_le32(control);
 	memcpy_string_op(&phdr->hdr, hdr, hdrwords << 2);
 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
+	if (packet) {
+		memcpy(packet->data, &phdr->hdr, (hdrwords << 2));
+		memcpy(packet->data+(hdrwords << 2),
+			(u8 *)((u32 *) &phdr->hdr + hdrwords),
+			len);
+	}
 
 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
@@ -1377,7 +1430,7 @@
 	tx->align_buf = phdr;
 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
 	tx->txreq.sg_count = 1;
-	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
+	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx, NULL);
 	goto unaligned;
 
 map_err:
@@ -1385,9 +1438,35 @@
 err_tx:
 	qib_put_txreq(tx);
 	ret = wait_kmem(dev, qp);
+	/* If wait_kmem returns 0 then
+	 * (ret==0) will hold true and we don't want
+	 * that as it will add ignored packet in list,
+	 * so free packet here.
+	 */
+	kfree(packet);
+	packet = NULL;
 unaligned:
 	ibp->n_unaligned++;
 bail:
+	if (packet) {
+		int nomatch = 0;
+		u8 *packet_data = packet->data;
+		packet_data += (hdrwords << 2); /* skip header */
+		if (ret == 0) {
+			if (ppd->filter_callback)
+				nomatch = ppd->filter_callback(hdr, packet_data,
+					ppd->filter_value);
+			if (nomatch == 0)
+				qib_snoop_send_queue_packet(ppd, packet);
+			else {
+				kfree(packet);
+				packet = NULL;
+			}
+		} else {
+			kfree(packet);
+			packet = NULL;
+		}
+	}
 	return ret;
 }
 
@@ -1440,6 +1519,8 @@
 	unsigned flush_wc;
 	u32 control;
 	u32 pbufn;
+	u8 *data_orig = NULL;
+	struct snoop_packet *packet = NULL;
 
 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
@@ -1448,6 +1529,12 @@
 	if (unlikely(piobuf == NULL))
 		return no_bufs_available(qp);
 
+	if (snoop_enable && ppd->mode_flag) {
+		packet = kzalloc(sizeof(*packet)+QIB_GET_PKT_LEN(ibhdr),
+				GFP_ATOMIC);
+		if (packet)
+			packet->total_len = QIB_GET_PKT_LEN(ibhdr);
+	}
 	/*
 	 * Write the pbc.
 	 * We have to flush after the PBC for correctness on some cpus
@@ -1458,6 +1545,12 @@
 	piobuf_orig = piobuf;
 	piobuf += 2;
 
+	if (packet) {
+		/* Copy header */
+		data_orig = packet->data;
+		memcpy(data_orig, hdr, (hdrwords << 2));
+		data_orig += (hdrwords << 2);
+	}
 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
 	if (len == 0) {
 		/*
@@ -1498,10 +1591,29 @@
 			qib_flush_wc();
 		} else
 			qib_pio_copy(piobuf, addr, dwords);
+		if (packet) {
+			/* Copy data */
+			memcpy(data_orig, addr, len);
+			data_orig += len;
+		}
 		goto done;
 	}
-	copy_io(piobuf, ss, len, flush_wc);
+	copy_io(piobuf, ss, len, flush_wc, packet, data_orig);
 done:
+	if (packet) {
+		int nomatch = 0;
+		u8 *packet_data = packet->data;
+		packet_data += (hdrwords << 2); /* Skip header */
+		if (ppd->filter_callback)
+			nomatch = ppd->filter_callback(ibhdr, packet_data,
+				ppd->filter_value);
+		if (nomatch == 0)
+			qib_snoop_send_queue_packet(ppd, packet);
+		else {
+			kfree(packet);
+			packet = NULL;
+		}
+	}
 	if (dd->flags & QIB_USE_SPCL_TRIG) {
 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
 		qib_flush_wc();
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
