--- read_bw.c.orig	2013-01-29 12:27:20.000000000 -0700
+++ read_bw.c	2013-06-07 17:15:33.931392279 -0600
@@ -37,11 +37,13 @@
 #  include <config.h>
 #endif /* HAVE_CONFIG_H */
 
+#include <errno.h>
+#include <inttypes.h>
+#include <malloc.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
-#include <malloc.h>
+#include <unistd.h>
 
 #include "get_clock.h"
 #include "perftest_resources.h"
@@ -53,14 +55,42 @@
 cycles_t	*tposted;
 cycles_t	*tcompleted;
 
+struct full_pkey_entry {
+	int	idx;
+	struct	full_pkey_entry *next;
+};
+static struct full_pkey_entry *fpk_head;
+
 /****************************************************************************** 
  *
  ******************************************************************************/
 static int pp_connect_ctx(struct pingpong_context *ctx,int my_psn,int my_out_reads,
 						  struct pingpong_dest *dest,struct perftest_parameters *user_parm)
 {
+	struct full_pkey_entry *p;
 	struct ibv_qp_attr attr;
 	memset(&attr, 0, sizeof attr);
+	
+	attr.pkey_index = 0;
+	attr.alt_pkey_index = 0;
+	p = fpk_head;
+	while (p != NULL) {
+		if (attr.pkey_index == 0) 
+			attr.pkey_index = p->idx;		
+		else if (attr.alt_pkey_index == 0)
+			attr.alt_pkey_index = p->idx;
+		p=p->next;
+        }
+	fprintf(stdout,"skc: pkey_index is %i\n", attr.pkey_index);
+	fprintf(stdout,"skc: alt_pkey_index is %i\n", attr.alt_pkey_index);
+
+/*  check pkey value    */
+
+        ret = ibv_query_pkey(ctx, user_parm->ib_port, attr.pkey_index, pkey_n);
+        if (!ret) {
+        	pkey = ntohs(*pkey_n);
+		fprintf(stdout,"skc: pkey_index value is %u\n", pkey);
+        }        
 
 	attr.qp_state 		= IBV_QPS_RTR;
 	attr.path_mtu       = user_parm->curr_mtu;
@@ -166,6 +196,8 @@
 
 	ALLOCATE(wc , struct ibv_wc , DEF_WC_SIZE);
 
+	fprintf(stdout,"skc: ibv_wc allocated\n");
+
 	list.addr   = (uintptr_t)ctx->buf;
 	list.length = user_param->size;
 	list.lkey   = ctx->mr->lkey;
@@ -219,6 +251,8 @@
 				if (ne > 0) {
 					for (i = 0; i < ne; i++) {
 
+						fprintf(stdout,"skc: completion queue %i polled, pkey is %i\n", i, wc[i].pkey_index);
+
 						if (wc[i].status != IBV_WC_SUCCESS) 
 							NOTIFY_COMP_ERROR_SEND(wc[i],scnt,ccnt);
 
@@ -243,12 +277,48 @@
 	return 0;
 }
 
+int check_pkey(struct ibv_context *context, struct perftest_parameters *parms) {
+
+	int idx,ret;
+	uint16_t *pkey_n,pkey;
+	struct ibv_port_attr pattr;
+	struct full_pkey_entry *fpk, *p;
+	
+	ret = ibv_query_port(context, parms->ib_port, &pattr);
+	
+	if(!ret) {
+                pkey_n = malloc(sizeof *pkey_n); 
+		for (idx=0; idx < pattr.pkey_tbl_len; idx++) {
+			ret = ibv_query_pkey(context, parms->ib_port, idx, pkey_n);
+			if (!ret) {
+				pkey = ntohs(*pkey_n);
+				if (pkey > 32767) {
+					fprintf(stdout,"skc: full member pkey is %u at index %i\n", pkey, idx);
+                                	fpk = (struct full_pkey_entry *) malloc(sizeof(struct full_pkey_entry));
+					fpk->idx = idx;
+					fpk->next = NULL;
+   					if (fpk_head == NULL) {
+						fpk_head = fpk;
+					} else {
+						for (p=fpk_head; p->next != NULL; p=p->next);
+						p->next = fpk;
+					}
+				}
+			} else {
+				ret = EPERM;
+			}	
+		}
+	}
+	return ret;
+}
+
 /****************************************************************************** 
  *
  ******************************************************************************/
 int main(int argc, char *argv[]) {
 
 	int                        i = 0;
+	int                        ret = 0;
 	struct ibv_device		   *ib_dev = NULL;
 	struct pingpong_context    ctx;
 	struct pingpong_dest       my_dest,rem_dest;
@@ -280,6 +350,12 @@
 		return 1;
 	}
 
+	ret = check_pkey(ctx.context, &user_param);
+        if (ret) {
+		fprintf(stderr, "PKey stuff borked\n");
+		return FAILURE;
+	}
+
 	// See if MTU and link type are valid and supported.
 	if (check_link_and_mtu(ctx.context,&user_param)) {
 		fprintf(stderr, " Couldn't get context for the device\n");
@@ -362,7 +438,7 @@
 	if (ctx_hand_shake(&user_comm,&my_dest,&rem_dest)) {
         fprintf(stderr,"Failed to exchange date between server and clients\n");
         return 1;    
-    }
+    	}
      
 	// For half duplex tests, server just waits for client to exit 
 	if (user_param.machine == SERVER && !user_param.duplex) {
@@ -390,6 +466,8 @@
 	ALLOCATE(tposted , cycles_t , user_param.iters);
 	ALLOCATE(tcompleted , cycles_t , user_param.iters);
 
+	fprintf(stdout,"skc: ready to run iterations\n");
+
 	if (user_param.all == ON) {
 
 		for (i = 1; i < 24 ; ++i) {
