xref: /freebsd/sys/contrib/rdma/krping/krping.c (revision 0c428864495af9dc7d2af4d0a5ae21732af9c739)
1 /*
2  * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
3  * Copyright (c) 2006-2009 Open Grid Computing, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/slab.h>
40 #include <linux/err.h>
41 #include <linux/string.h>
42 #include <linux/list.h>
43 #include <linux/in.h>
44 #include <linux/device.h>
45 #include <linux/pci.h>
46 #include <linux/sched.h>
47 #include <linux/wait.h>
48 
49 #include <asm/atomic.h>
50 
51 #include <rdma/ib_verbs.h>
52 #include <rdma/rdma_cm.h>
53 
54 #include "krping.h"
55 #include "getopt.h"
56 
57 #define PFX "krping: "
58 
59 extern int krping_debug;
60 #define DEBUG_LOG(...) do { if (krping_debug) log(LOG_INFO, __VA_ARGS__); } while (0)
61 #define BIND_INFO 1
62 
63 MODULE_AUTHOR("Steve Wise");
64 MODULE_DESCRIPTION("RDMA ping server");
65 MODULE_LICENSE("Dual BSD/GPL");
66 MODULE_VERSION(krping, 1);
67 MODULE_DEPEND(krping, linuxkpi, 1, 1, 1);
68 
69 static __inline uint64_t
70 get_cycles(void)
71 {
72 #if defined(__amd64__) || defined(__i386__)
73 	uint32_t low, high;
74 	__asm __volatile("rdtsc" : "=a" (low), "=d" (high));
75 	return (low | ((u_int64_t)high << 32));
76 #elif defined(__powerpc64__)
77 	return __builtin_readcyclecounter();
78 #endif
79 }
80 
81 typedef uint64_t cycles_t;
82 
83 enum mem_type {
84 	DMA = 1,
85 	REG = 2,
86 };
87 
88 static const struct krping_option krping_opts[] = {
89 	{"count", OPT_INT, 'C'},
90 	{"size", OPT_INT, 'S'},
91 	{"addr", OPT_STRING, 'a'},
92 	{"addr6", OPT_STRING, 'A'},
93 	{"port", OPT_INT, 'p'},
94 	{"verbose", OPT_NOPARAM, 'v'},
95 	{"validate", OPT_NOPARAM, 'V'},
96 	{"server", OPT_NOPARAM, 's'},
97 	{"client", OPT_NOPARAM, 'c'},
98 	{"server_inv", OPT_NOPARAM, 'I'},
99  	{"wlat", OPT_NOPARAM, 'l'},
100  	{"rlat", OPT_NOPARAM, 'L'},
101  	{"bw", OPT_NOPARAM, 'B'},
102  	{"duplex", OPT_NOPARAM, 'd'},
103 	{"tos", OPT_INT, 't'},
104  	{"txdepth", OPT_INT, 'T'},
105  	{"poll", OPT_NOPARAM, 'P'},
106  	{"local_dma_lkey", OPT_NOPARAM, 'Z'},
107  	{"read_inv", OPT_NOPARAM, 'R'},
108  	{"fr", OPT_NOPARAM, 'f'},
109 	{NULL, 0, 0}
110 };
111 
112 #define htonll(x) cpu_to_be64((x))
113 #define ntohll(x) cpu_to_be64((x))
114 
115 static DEFINE_MUTEX(krping_mutex);
116 
117 /*
118  * List of running krping threads.
119  */
120 static LIST_HEAD(krping_cbs);
121 
122 /*
123  * Invoke like this, one on each side, using the server's address on
124  * the RDMA device (iw%d):
125  *
126  * /bin/echo server,port=9999,addr=192.168.69.142,validate > /proc/krping
127  * /bin/echo client,port=9999,addr=192.168.69.142,validate > /proc/krping
128  * /bin/echo client,port=9999,addr6=2001:db8:0:f101::1,validate > /proc/krping
129  *
130  * krping "ping/pong" loop:
131  * 	client sends source rkey/addr/len
132  *	server receives source rkey/add/len
133  *	server rdma reads "ping" data from source
134  * 	server sends "go ahead" on rdma read completion
135  *	client sends sink rkey/addr/len
136  * 	server receives sink rkey/addr/len
137  * 	server rdma writes "pong" data to sink
138  * 	server sends "go ahead" on rdma write completion
139  * 	<repeat loop>
140  */
141 
142 /*
143  * These states are used to signal events between the completion handler
144  * and the main client or server thread.
145  *
146  * Once CONNECTED, they cycle through RDMA_READ_ADV, RDMA_WRITE_ADV,
147  * and RDMA_WRITE_COMPLETE for each ping.
148  */
149 enum test_state {
150 	IDLE = 1,
151 	CONNECT_REQUEST,
152 	ADDR_RESOLVED,
153 	ROUTE_RESOLVED,
154 	CONNECTED,
155 	RDMA_READ_ADV,
156 	RDMA_READ_COMPLETE,
157 	RDMA_WRITE_ADV,
158 	RDMA_WRITE_COMPLETE,
159 	ERROR
160 };
161 
162 struct krping_rdma_info {
163 	uint64_t buf;
164 	uint32_t rkey;
165 	uint32_t size;
166 };
167 
168 /*
169  * Default max buffer size for IO...
170  */
171 #define RPING_BUFSIZE 128*1024
172 #define RPING_SQ_DEPTH 64
173 
174 /*
175  * Control block struct.
176  */
177 struct krping_cb {
178 	int server;			/* 0 iff client */
179 	struct ib_cq *cq;
180 	struct ib_pd *pd;
181 	struct ib_qp *qp;
182 
183 	struct ib_mr *dma_mr;
184 
185 	struct ib_fast_reg_page_list *page_list;
186 	int page_list_len;
187 	struct ib_reg_wr reg_mr_wr;
188 	struct ib_send_wr invalidate_wr;
189 	struct ib_mr *reg_mr;
190 	int server_invalidate;
191 	int read_inv;
192 	u8 key;
193 
194 	struct ib_recv_wr rq_wr;	/* recv work request record */
195 	struct ib_sge recv_sgl;		/* recv single SGE */
196 	struct krping_rdma_info recv_buf __aligned(16);	/* malloc'd buffer */
197 	u64 recv_dma_addr;
198 	DECLARE_PCI_UNMAP_ADDR(recv_mapping)
199 
200 	struct ib_send_wr sq_wr;	/* send work requrest record */
201 	struct ib_sge send_sgl;
202 	struct krping_rdma_info send_buf __aligned(16); /* single send buf */
203 	u64 send_dma_addr;
204 	DECLARE_PCI_UNMAP_ADDR(send_mapping)
205 
206 	struct ib_rdma_wr rdma_sq_wr;	/* rdma work request record */
207 	struct ib_sge rdma_sgl;		/* rdma single SGE */
208 	char *rdma_buf;			/* used as rdma sink */
209 	u64  rdma_dma_addr;
210 	DECLARE_PCI_UNMAP_ADDR(rdma_mapping)
211 	struct ib_mr *rdma_mr;
212 
213 	uint32_t remote_rkey;		/* remote guys RKEY */
214 	uint64_t remote_addr;		/* remote guys TO */
215 	uint32_t remote_len;		/* remote guys LEN */
216 
217 	char *start_buf;		/* rdma read src */
218 	u64  start_dma_addr;
219 	DECLARE_PCI_UNMAP_ADDR(start_mapping)
220 	struct ib_mr *start_mr;
221 
222 	enum test_state state;		/* used for cond/signalling */
223 	wait_queue_head_t sem;
224 	struct krping_stats stats;
225 
226 	uint16_t port;			/* dst port in NBO */
227 	u8 addr[16] __aligned(8);	/* dst addr in NBO */
228 	char *addr_str;			/* dst addr string */
229 	uint8_t addr_type;		/* ADDR_FAMILY - IPv4/V6 */
230 	int verbose;			/* verbose logging */
231 	int count;			/* ping count */
232 	int size;			/* ping data size */
233 	int validate;			/* validate ping data */
234 	int wlat;			/* run wlat test */
235 	int rlat;			/* run rlat test */
236 	int bw;				/* run bw test */
237 	int duplex;			/* run bw full duplex test */
238 	int poll;			/* poll or block for rlat test */
239 	int txdepth;			/* SQ depth */
240 	int local_dma_lkey;		/* use 0 for lkey */
241 	int frtest;			/* reg test */
242 	int tos;			/* type of service */
243 
244 	/* CM stuff */
245 	struct rdma_cm_id *cm_id;	/* connection on client side,*/
246 					/* listener on server side. */
247 	struct rdma_cm_id *child_cm_id;	/* connection on server side */
248 	struct list_head list;
249 };
250 
251 static int krping_cma_event_handler(struct rdma_cm_id *cma_id,
252 				   struct rdma_cm_event *event)
253 {
254 	int ret;
255 	struct krping_cb *cb = cma_id->context;
256 
257 	DEBUG_LOG("cma_event type %d cma_id %p (%s)\n", event->event, cma_id,
258 		  (cma_id == cb->cm_id) ? "parent" : "child");
259 
260 	switch (event->event) {
261 	case RDMA_CM_EVENT_ADDR_RESOLVED:
262 		cb->state = ADDR_RESOLVED;
263 		ret = rdma_resolve_route(cma_id, 2000);
264 		if (ret) {
265 			printk(KERN_ERR PFX "rdma_resolve_route error %d\n",
266 			       ret);
267 			wake_up_interruptible(&cb->sem);
268 		}
269 		break;
270 
271 	case RDMA_CM_EVENT_ROUTE_RESOLVED:
272 		cb->state = ROUTE_RESOLVED;
273 		wake_up_interruptible(&cb->sem);
274 		break;
275 
276 	case RDMA_CM_EVENT_CONNECT_REQUEST:
277 		cb->state = CONNECT_REQUEST;
278 		cb->child_cm_id = cma_id;
279 		DEBUG_LOG("child cma %p\n", cb->child_cm_id);
280 		wake_up_interruptible(&cb->sem);
281 		break;
282 
283 	case RDMA_CM_EVENT_ESTABLISHED:
284 		DEBUG_LOG("ESTABLISHED\n");
285 		if (!cb->server) {
286 			cb->state = CONNECTED;
287 		}
288 		wake_up_interruptible(&cb->sem);
289 		break;
290 
291 	case RDMA_CM_EVENT_ADDR_ERROR:
292 	case RDMA_CM_EVENT_ROUTE_ERROR:
293 	case RDMA_CM_EVENT_CONNECT_ERROR:
294 	case RDMA_CM_EVENT_UNREACHABLE:
295 	case RDMA_CM_EVENT_REJECTED:
296 		printk(KERN_ERR PFX "cma event %d, error %d\n", event->event,
297 		       event->status);
298 		cb->state = ERROR;
299 		wake_up_interruptible(&cb->sem);
300 		break;
301 
302 	case RDMA_CM_EVENT_DISCONNECTED:
303 		printk(KERN_ERR PFX "DISCONNECT EVENT...\n");
304 		cb->state = ERROR;
305 		wake_up_interruptible(&cb->sem);
306 		break;
307 
308 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
309 		printk(KERN_ERR PFX "cma detected device removal!!!!\n");
310 		cb->state = ERROR;
311 		wake_up_interruptible(&cb->sem);
312 		break;
313 
314 	default:
315 		printk(KERN_ERR PFX "oof bad type!\n");
316 		wake_up_interruptible(&cb->sem);
317 		break;
318 	}
319 	return 0;
320 }
321 
322 static int server_recv(struct krping_cb *cb, struct ib_wc *wc)
323 {
324 	if (wc->byte_len != sizeof(cb->recv_buf)) {
325 		printk(KERN_ERR PFX "Received bogus data, size %d\n",
326 		       wc->byte_len);
327 		return -1;
328 	}
329 
330 	cb->remote_rkey = ntohl(cb->recv_buf.rkey);
331 	cb->remote_addr = ntohll(cb->recv_buf.buf);
332 	cb->remote_len  = ntohl(cb->recv_buf.size);
333 	DEBUG_LOG("Received rkey %x addr %llx len %d from peer\n",
334 		  cb->remote_rkey, (unsigned long long)cb->remote_addr,
335 		  cb->remote_len);
336 
337 	if (cb->state <= CONNECTED || cb->state == RDMA_WRITE_COMPLETE)
338 		cb->state = RDMA_READ_ADV;
339 	else
340 		cb->state = RDMA_WRITE_ADV;
341 
342 	return 0;
343 }
344 
345 static int client_recv(struct krping_cb *cb, struct ib_wc *wc)
346 {
347 	if (wc->byte_len != sizeof(cb->recv_buf)) {
348 		printk(KERN_ERR PFX "Received bogus data, size %d\n",
349 		       wc->byte_len);
350 		return -1;
351 	}
352 
353 	if (cb->state == RDMA_READ_ADV)
354 		cb->state = RDMA_WRITE_ADV;
355 	else
356 		cb->state = RDMA_WRITE_COMPLETE;
357 
358 	return 0;
359 }
360 
361 static void krping_cq_event_handler(struct ib_cq *cq, void *ctx)
362 {
363 	struct krping_cb *cb = ctx;
364 	struct ib_wc wc;
365 	const struct ib_recv_wr *bad_wr;
366 	int ret;
367 
368 	BUG_ON(cb->cq != cq);
369 	if (cb->frtest) {
370 		printk(KERN_ERR PFX "cq completion event in frtest!\n");
371 		return;
372 	}
373 	if (!cb->wlat && !cb->rlat && !cb->bw)
374 		ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
375 	while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) {
376 		if (wc.status) {
377 			if (wc.status == IB_WC_WR_FLUSH_ERR) {
378 				DEBUG_LOG("cq flushed\n");
379 				continue;
380 			} else {
381 				printk(KERN_ERR PFX "cq completion failed with "
382 				       "wr_id %jx status %d opcode %d vender_err %x\n",
383 					(uintmax_t)wc.wr_id, wc.status, wc.opcode, wc.vendor_err);
384 				goto error;
385 			}
386 		}
387 		if (cb->state == ERROR) {
388 			printk(KERN_ERR PFX "cq completion in ERROR state\n");
389 			return;
390 		}
391 		switch (wc.opcode) {
392 		case IB_WC_SEND:
393 			DEBUG_LOG("send completion\n");
394 			cb->stats.send_bytes += cb->send_sgl.length;
395 			cb->stats.send_msgs++;
396 			break;
397 
398 		case IB_WC_RDMA_WRITE:
399 			DEBUG_LOG("rdma write completion\n");
400 			cb->stats.write_bytes += cb->rdma_sq_wr.wr.sg_list->length;
401 			cb->stats.write_msgs++;
402 			cb->state = RDMA_WRITE_COMPLETE;
403 			wake_up_interruptible(&cb->sem);
404 			break;
405 
406 		case IB_WC_RDMA_READ:
407 			DEBUG_LOG("rdma read completion\n");
408 			cb->stats.read_bytes += cb->rdma_sq_wr.wr.sg_list->length;
409 			cb->stats.read_msgs++;
410 			cb->state = RDMA_READ_COMPLETE;
411 			wake_up_interruptible(&cb->sem);
412 			break;
413 
414 		case IB_WC_RECV:
415 			DEBUG_LOG("recv completion\n");
416 			cb->stats.recv_bytes += sizeof(cb->recv_buf);
417 			cb->stats.recv_msgs++;
418 			if (cb->wlat || cb->rlat || cb->bw)
419 				ret = server_recv(cb, &wc);
420 			else
421 				ret = cb->server ? server_recv(cb, &wc) :
422 						   client_recv(cb, &wc);
423 			if (ret) {
424 				printk(KERN_ERR PFX "recv wc error: %d\n", ret);
425 				goto error;
426 			}
427 
428 			ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
429 			if (ret) {
430 				printk(KERN_ERR PFX "post recv error: %d\n",
431 				       ret);
432 				goto error;
433 			}
434 			wake_up_interruptible(&cb->sem);
435 			break;
436 
437 		default:
438 			printk(KERN_ERR PFX
439 			       "%s:%d Unexpected opcode %d, Shutting down\n",
440 			       __func__, __LINE__, wc.opcode);
441 			goto error;
442 		}
443 	}
444 	if (ret) {
445 		printk(KERN_ERR PFX "poll error %d\n", ret);
446 		goto error;
447 	}
448 	return;
449 error:
450 	cb->state = ERROR;
451 	wake_up_interruptible(&cb->sem);
452 }
453 
454 static int krping_accept(struct krping_cb *cb)
455 {
456 	struct rdma_conn_param conn_param;
457 	int ret;
458 
459 	DEBUG_LOG("accepting client connection request\n");
460 
461 	memset(&conn_param, 0, sizeof conn_param);
462 	conn_param.responder_resources = 1;
463 	conn_param.initiator_depth = 1;
464 
465 	ret = rdma_accept(cb->child_cm_id, &conn_param);
466 	if (ret) {
467 		printk(KERN_ERR PFX "rdma_accept error: %d\n", ret);
468 		return ret;
469 	}
470 
471 	if (!cb->wlat && !cb->rlat && !cb->bw) {
472 		wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
473 		if (cb->state == ERROR) {
474 			printk(KERN_ERR PFX "wait for CONNECTED state %d\n",
475 				cb->state);
476 			return -1;
477 		}
478 	}
479 	return 0;
480 }
481 
482 static void krping_setup_wr(struct krping_cb *cb)
483 {
484 	cb->recv_sgl.addr = cb->recv_dma_addr;
485 	cb->recv_sgl.length = sizeof cb->recv_buf;
486 	cb->recv_sgl.lkey = cb->pd->local_dma_lkey;
487 	cb->rq_wr.sg_list = &cb->recv_sgl;
488 	cb->rq_wr.num_sge = 1;
489 
490 	cb->send_sgl.addr = cb->send_dma_addr;
491 	cb->send_sgl.length = sizeof cb->send_buf;
492 	cb->send_sgl.lkey = cb->pd->local_dma_lkey;
493 
494 	cb->sq_wr.opcode = IB_WR_SEND;
495 	cb->sq_wr.send_flags = IB_SEND_SIGNALED;
496 	cb->sq_wr.sg_list = &cb->send_sgl;
497 	cb->sq_wr.num_sge = 1;
498 
499 	if (cb->server || cb->wlat || cb->rlat || cb->bw) {
500 		cb->rdma_sgl.addr = cb->rdma_dma_addr;
501 		cb->rdma_sq_wr.wr.send_flags = IB_SEND_SIGNALED;
502 		cb->rdma_sq_wr.wr.sg_list = &cb->rdma_sgl;
503 		cb->rdma_sq_wr.wr.num_sge = 1;
504 	}
505 
506 	/*
507 	 * A chain of 2 WRs, INVALDATE_MR + REG_MR.
508 	 * both unsignaled.  The client uses them to reregister
509 	 * the rdma buffers with a new key each iteration.
510 	 */
511 	cb->reg_mr_wr.wr.opcode = IB_WR_REG_MR;
512 	cb->reg_mr_wr.mr = cb->reg_mr;
513 
514 	cb->invalidate_wr.next = &cb->reg_mr_wr.wr;
515 	cb->invalidate_wr.opcode = IB_WR_LOCAL_INV;
516 }
517 
518 static int krping_setup_buffers(struct krping_cb *cb)
519 {
520 	int ret;
521 
522 	DEBUG_LOG(PFX "krping_setup_buffers called on cb %p\n", cb);
523 
524 	cb->recv_dma_addr = ib_dma_map_single(cb->pd->device,
525 				   &cb->recv_buf,
526 				   sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
527 	pci_unmap_addr_set(cb, recv_mapping, cb->recv_dma_addr);
528 	cb->send_dma_addr = ib_dma_map_single(cb->pd->device,
529 					   &cb->send_buf, sizeof(cb->send_buf),
530 					   DMA_BIDIRECTIONAL);
531 	pci_unmap_addr_set(cb, send_mapping, cb->send_dma_addr);
532 
533 	cb->rdma_buf = ib_dma_alloc_coherent(cb->pd->device, cb->size,
534 					     &cb->rdma_dma_addr,
535 					     GFP_KERNEL);
536 	if (!cb->rdma_buf) {
537 		DEBUG_LOG(PFX "rdma_buf allocation failed\n");
538 		ret = -ENOMEM;
539 		goto bail;
540 	}
541 	pci_unmap_addr_set(cb, rdma_mapping, cb->rdma_dma_addr);
542 	cb->page_list_len = (((cb->size - 1) & PAGE_MASK) + PAGE_SIZE)
543 				>> PAGE_SHIFT;
544 	cb->reg_mr = ib_alloc_mr(cb->pd,  IB_MR_TYPE_MEM_REG,
545 				 cb->page_list_len);
546 	if (IS_ERR(cb->reg_mr)) {
547 		ret = PTR_ERR(cb->reg_mr);
548 		DEBUG_LOG(PFX "recv_buf reg_mr failed %d\n", ret);
549 		goto bail;
550 	}
551 	DEBUG_LOG(PFX "reg rkey 0x%x page_list_len %u\n",
552 		cb->reg_mr->rkey, cb->page_list_len);
553 
554 	if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
555 
556 		cb->start_buf = ib_dma_alloc_coherent(cb->pd->device, cb->size,
557 						      &cb->start_dma_addr,
558 						      GFP_KERNEL);
559 		if (!cb->start_buf) {
560 			DEBUG_LOG(PFX "start_buf malloc failed\n");
561 			ret = -ENOMEM;
562 			goto bail;
563 		}
564 		pci_unmap_addr_set(cb, start_mapping, cb->start_dma_addr);
565 	}
566 
567 	krping_setup_wr(cb);
568 	DEBUG_LOG(PFX "allocated & registered buffers...\n");
569 	return 0;
570 bail:
571 	if (cb->reg_mr && !IS_ERR(cb->reg_mr))
572 		ib_dereg_mr(cb->reg_mr);
573 	if (cb->rdma_mr && !IS_ERR(cb->rdma_mr))
574 		ib_dereg_mr(cb->rdma_mr);
575 	if (cb->dma_mr && !IS_ERR(cb->dma_mr))
576 		ib_dereg_mr(cb->dma_mr);
577 	if (cb->rdma_buf) {
578 		ib_dma_free_coherent(cb->pd->device, cb->size, cb->rdma_buf,
579 				     cb->rdma_dma_addr);
580 	}
581 	if (cb->start_buf) {
582 		ib_dma_free_coherent(cb->pd->device, cb->size, cb->start_buf,
583 				     cb->start_dma_addr);
584 	}
585 	return ret;
586 }
587 
588 static void krping_free_buffers(struct krping_cb *cb)
589 {
590 	DEBUG_LOG("krping_free_buffers called on cb %p\n", cb);
591 
592 	if (cb->dma_mr)
593 		ib_dereg_mr(cb->dma_mr);
594 	if (cb->rdma_mr)
595 		ib_dereg_mr(cb->rdma_mr);
596 	if (cb->start_mr)
597 		ib_dereg_mr(cb->start_mr);
598 	if (cb->reg_mr)
599 		ib_dereg_mr(cb->reg_mr);
600 
601 	dma_unmap_single(cb->pd->device->dma_device,
602 			 pci_unmap_addr(cb, recv_mapping),
603 			 sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
604 	dma_unmap_single(cb->pd->device->dma_device,
605 			 pci_unmap_addr(cb, send_mapping),
606 			 sizeof(cb->send_buf), DMA_BIDIRECTIONAL);
607 
608 	ib_dma_free_coherent(cb->pd->device, cb->size, cb->rdma_buf,
609 			     cb->rdma_dma_addr);
610 
611 	if (cb->start_buf) {
612 		ib_dma_free_coherent(cb->pd->device, cb->size, cb->start_buf,
613 				     cb->start_dma_addr);
614 	}
615 }
616 
617 static int krping_create_qp(struct krping_cb *cb)
618 {
619 	struct ib_qp_init_attr init_attr;
620 	int ret;
621 
622 	memset(&init_attr, 0, sizeof(init_attr));
623 	init_attr.cap.max_send_wr = cb->txdepth;
624 	init_attr.cap.max_recv_wr = 2;
625 
626 	/* For flush_qp() */
627 	init_attr.cap.max_send_wr++;
628 	init_attr.cap.max_recv_wr++;
629 
630 	init_attr.cap.max_recv_sge = 1;
631 	init_attr.cap.max_send_sge = 1;
632 	init_attr.qp_type = IB_QPT_RC;
633 	init_attr.send_cq = cb->cq;
634 	init_attr.recv_cq = cb->cq;
635 	init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
636 
637 	if (cb->server) {
638 		ret = rdma_create_qp(cb->child_cm_id, cb->pd, &init_attr);
639 		if (!ret)
640 			cb->qp = cb->child_cm_id->qp;
641 	} else {
642 		ret = rdma_create_qp(cb->cm_id, cb->pd, &init_attr);
643 		if (!ret)
644 			cb->qp = cb->cm_id->qp;
645 	}
646 
647 	return ret;
648 }
649 
650 static void krping_free_qp(struct krping_cb *cb)
651 {
652 	ib_destroy_qp(cb->qp);
653 	ib_destroy_cq(cb->cq);
654 	ib_dealloc_pd(cb->pd);
655 }
656 
657 static int krping_setup_qp(struct krping_cb *cb, struct rdma_cm_id *cm_id)
658 {
659 	int ret;
660 	struct ib_cq_init_attr attr = {0};
661 
662 	cb->pd = ib_alloc_pd(cm_id->device, 0);
663 	if (IS_ERR(cb->pd)) {
664 		printk(KERN_ERR PFX "ib_alloc_pd failed\n");
665 		return PTR_ERR(cb->pd);
666 	}
667 	DEBUG_LOG("created pd %p\n", cb->pd);
668 
669 	strlcpy(cb->stats.name, cb->pd->device->name, sizeof(cb->stats.name));
670 
671 	attr.cqe = cb->txdepth * 2;
672 	attr.comp_vector = 0;
673 	cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL,
674 			      cb, &attr);
675 	if (IS_ERR(cb->cq)) {
676 		printk(KERN_ERR PFX "ib_create_cq failed\n");
677 		ret = PTR_ERR(cb->cq);
678 		goto err1;
679 	}
680 	DEBUG_LOG("created cq %p\n", cb->cq);
681 
682 	if (!cb->wlat && !cb->rlat && !cb->bw && !cb->frtest) {
683 		ret = ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
684 		if (ret) {
685 			printk(KERN_ERR PFX "ib_create_cq failed\n");
686 			goto err2;
687 		}
688 	}
689 
690 	ret = krping_create_qp(cb);
691 	if (ret) {
692 		printk(KERN_ERR PFX "krping_create_qp failed: %d\n", ret);
693 		goto err2;
694 	}
695 	DEBUG_LOG("created qp %p\n", cb->qp);
696 	return 0;
697 err2:
698 	ib_destroy_cq(cb->cq);
699 err1:
700 	ib_dealloc_pd(cb->pd);
701 	return ret;
702 }
703 
704 /*
705  * return the (possibly rebound) rkey for the rdma buffer.
706  * REG mode: invalidate and rebind via reg wr.
707  * other modes: just return the mr rkey.
708  */
709 static u32 krping_rdma_rkey(struct krping_cb *cb, u64 buf, int post_inv)
710 {
711 	u32 rkey;
712 	const struct ib_send_wr *bad_wr;
713 	int ret;
714 	struct scatterlist sg = {0};
715 
716 	cb->invalidate_wr.ex.invalidate_rkey = cb->reg_mr->rkey;
717 
718 	/*
719 	 * Update the reg key.
720 	 */
721 	ib_update_fast_reg_key(cb->reg_mr, ++cb->key);
722 	cb->reg_mr_wr.key = cb->reg_mr->rkey;
723 
724 	/*
725 	 * Update the reg WR with new buf info.
726 	 */
727 	if (buf == (u64)cb->start_dma_addr)
728 		cb->reg_mr_wr.access = IB_ACCESS_REMOTE_READ;
729 	else
730 		cb->reg_mr_wr.access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
731 	sg_dma_address(&sg) = buf;
732 	sg_dma_len(&sg) = cb->size;
733 
734 	ret = ib_map_mr_sg(cb->reg_mr, &sg, 1, NULL, PAGE_SIZE);
735 	BUG_ON(ret <= 0 || ret > cb->page_list_len);
736 
737 	DEBUG_LOG(PFX "post_inv = %d, reg_mr new rkey 0x%x pgsz %u len %u"
738 		" iova_start %llx\n",
739 		post_inv,
740 		cb->reg_mr_wr.key,
741 		cb->reg_mr->page_size,
742 		(unsigned)cb->reg_mr->length,
743 	        (unsigned long long)cb->reg_mr->iova);
744 
745 	if (post_inv)
746 		ret = ib_post_send(cb->qp, &cb->invalidate_wr, &bad_wr);
747 	else
748 		ret = ib_post_send(cb->qp, &cb->reg_mr_wr.wr, &bad_wr);
749 	if (ret) {
750 		printk(KERN_ERR PFX "post send error %d\n", ret);
751 		cb->state = ERROR;
752 	}
753 	rkey = cb->reg_mr->rkey;
754 	return rkey;
755 }
756 
757 static void krping_format_send(struct krping_cb *cb, u64 buf)
758 {
759 	struct krping_rdma_info *info = &cb->send_buf;
760 	u32 rkey;
761 
762 	/*
763 	 * Client side will do reg or mw bind before
764 	 * advertising the rdma buffer.  Server side
765 	 * sends have no data.
766 	 */
767 	if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
768 		rkey = krping_rdma_rkey(cb, buf, !cb->server_invalidate);
769 		info->buf = htonll(buf);
770 		info->rkey = htonl(rkey);
771 		info->size = htonl(cb->size);
772 		DEBUG_LOG("RDMA addr %llx rkey %x len %d\n",
773 			  (unsigned long long)buf, rkey, cb->size);
774 	}
775 }
776 
777 static void krping_test_server(struct krping_cb *cb)
778 {
779 	const struct ib_send_wr *bad_wr;
780 	struct ib_send_wr inv;
781 	int ret;
782 
783 	while (1) {
784 		/* Wait for client's Start STAG/TO/Len */
785 		wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_ADV);
786 		if (cb->state != RDMA_READ_ADV) {
787 			printk(KERN_ERR PFX "wait for RDMA_READ_ADV state %d\n",
788 				cb->state);
789 			break;
790 		}
791 
792 		DEBUG_LOG("server received sink adv\n");
793 
794 		cb->rdma_sq_wr.rkey = cb->remote_rkey;
795 		cb->rdma_sq_wr.remote_addr = cb->remote_addr;
796 		cb->rdma_sq_wr.wr.sg_list->length = cb->remote_len;
797 		cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, !cb->read_inv);
798 		cb->rdma_sq_wr.wr.next = NULL;
799 
800 		/* Issue RDMA Read. */
801 		if (cb->read_inv)
802 			cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ_WITH_INV;
803 		else {
804 
805 			cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ;
806 			/*
807 			 * Immediately follow the read with a
808 			 * fenced LOCAL_INV.
809 			 */
810 			cb->rdma_sq_wr.wr.next = &inv;
811 			memset(&inv, 0, sizeof inv);
812 			inv.opcode = IB_WR_LOCAL_INV;
813 			inv.ex.invalidate_rkey = cb->reg_mr->rkey;
814 			inv.send_flags = IB_SEND_FENCE;
815 		}
816 
817 		ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
818 		if (ret) {
819 			printk(KERN_ERR PFX "post send error %d\n", ret);
820 			break;
821 		}
822 		cb->rdma_sq_wr.wr.next = NULL;
823 
824 		DEBUG_LOG("server posted rdma read req \n");
825 
826 		/* Wait for read completion */
827 		wait_event_interruptible(cb->sem,
828 					 cb->state >= RDMA_READ_COMPLETE);
829 		if (cb->state != RDMA_READ_COMPLETE) {
830 			printk(KERN_ERR PFX
831 			       "wait for RDMA_READ_COMPLETE state %d\n",
832 			       cb->state);
833 			break;
834 		}
835 		DEBUG_LOG("server received read complete\n");
836 
837 		/* Display data in recv buf */
838 		if (cb->verbose)
839 			printk(KERN_INFO PFX "server ping data: %s\n",
840 				cb->rdma_buf);
841 
842 		/* Tell client to continue */
843 		if (cb->server && cb->server_invalidate) {
844 			cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
845 			cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
846 			DEBUG_LOG("send-w-inv rkey 0x%x\n", cb->remote_rkey);
847 		}
848 		ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
849 		if (ret) {
850 			printk(KERN_ERR PFX "post send error %d\n", ret);
851 			break;
852 		}
853 		DEBUG_LOG("server posted go ahead\n");
854 
855 		/* Wait for client's RDMA STAG/TO/Len */
856 		wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV);
857 		if (cb->state != RDMA_WRITE_ADV) {
858 			printk(KERN_ERR PFX
859 			       "wait for RDMA_WRITE_ADV state %d\n",
860 			       cb->state);
861 			break;
862 		}
863 		DEBUG_LOG("server received sink adv\n");
864 
865 		/* RDMA Write echo data */
866 		cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE;
867 		cb->rdma_sq_wr.rkey = cb->remote_rkey;
868 		cb->rdma_sq_wr.remote_addr = cb->remote_addr;
869 		cb->rdma_sq_wr.wr.sg_list->length = strlen(cb->rdma_buf) + 1;
870 		if (cb->local_dma_lkey)
871 			cb->rdma_sgl.lkey = cb->pd->local_dma_lkey;
872 		else
873 			cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 0);
874 
875 		DEBUG_LOG("rdma write from lkey %x laddr %llx len %d\n",
876 			  cb->rdma_sq_wr.wr.sg_list->lkey,
877 			  (unsigned long long)cb->rdma_sq_wr.wr.sg_list->addr,
878 			  cb->rdma_sq_wr.wr.sg_list->length);
879 
880 		ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
881 		if (ret) {
882 			printk(KERN_ERR PFX "post send error %d\n", ret);
883 			break;
884 		}
885 
886 		/* Wait for completion */
887 		ret = wait_event_interruptible(cb->sem, cb->state >=
888 							 RDMA_WRITE_COMPLETE);
889 		if (cb->state != RDMA_WRITE_COMPLETE) {
890 			printk(KERN_ERR PFX
891 			       "wait for RDMA_WRITE_COMPLETE state %d\n",
892 			       cb->state);
893 			break;
894 		}
895 		DEBUG_LOG("server rdma write complete \n");
896 
897 		cb->state = CONNECTED;
898 
899 		/* Tell client to begin again */
900 		if (cb->server && cb->server_invalidate) {
901 			cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
902 			cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
903 			DEBUG_LOG("send-w-inv rkey 0x%x\n", cb->remote_rkey);
904 		}
905 		ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
906 		if (ret) {
907 			printk(KERN_ERR PFX "post send error %d\n", ret);
908 			break;
909 		}
910 		DEBUG_LOG("server posted go ahead\n");
911 	}
912 }
913 
914 static void rlat_test(struct krping_cb *cb)
915 {
916 	int scnt;
917 	int iters = cb->count;
918 	struct timeval start_tv, stop_tv;
919 	int ret;
920 	struct ib_wc wc;
921 	const struct ib_send_wr *bad_wr;
922 	int ne;
923 
924 	scnt = 0;
925 	cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ;
926 	cb->rdma_sq_wr.rkey = cb->remote_rkey;
927 	cb->rdma_sq_wr.remote_addr = cb->remote_addr;
928 	cb->rdma_sq_wr.wr.sg_list->length = cb->size;
929 
930 	microtime(&start_tv);
931 	if (!cb->poll) {
932 		cb->state = RDMA_READ_ADV;
933 		ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
934 	}
935 	while (scnt < iters) {
936 
937 		cb->state = RDMA_READ_ADV;
938 		ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr);
939 		if (ret) {
940 			printk(KERN_ERR PFX
941 				"Couldn't post send: ret=%d scnt %d\n",
942 				ret, scnt);
943 			return;
944 		}
945 
946 		do {
947 			if (!cb->poll) {
948 				wait_event_interruptible(cb->sem,
949 					cb->state != RDMA_READ_ADV);
950 				if (cb->state == RDMA_READ_COMPLETE) {
951 					ne = 1;
952 					ib_req_notify_cq(cb->cq,
953 						IB_CQ_NEXT_COMP);
954 				} else {
955 					ne = -1;
956 				}
957 			} else
958 				ne = ib_poll_cq(cb->cq, 1, &wc);
959 			if (cb->state == ERROR) {
960 				printk(KERN_ERR PFX
961 					"state == ERROR...bailing scnt %d\n",
962 					scnt);
963 				return;
964 			}
965 		} while (ne == 0);
966 
967 		if (ne < 0) {
968 			printk(KERN_ERR PFX "poll CQ failed %d\n", ne);
969 			return;
970 		}
971 		if (cb->poll && wc.status != IB_WC_SUCCESS) {
972 			printk(KERN_ERR PFX "Completion wth error at %s:\n",
973 				cb->server ? "server" : "client");
974 			printk(KERN_ERR PFX "Failed status %d: wr_id %d\n",
975 				wc.status, (int) wc.wr_id);
976 			return;
977 		}
978 		++scnt;
979 	}
980 	microtime(&stop_tv);
981 
982         if (stop_tv.tv_usec < start_tv.tv_usec) {
983                 stop_tv.tv_usec += 1000000;
984                 stop_tv.tv_sec  -= 1;
985         }
986 
987 	printk(KERN_ERR PFX "delta sec %lu delta usec %lu iter %d size %d\n",
988 		(unsigned long)(stop_tv.tv_sec - start_tv.tv_sec),
989 		(unsigned long)(stop_tv.tv_usec - start_tv.tv_usec),
990 		scnt, cb->size);
991 }
992 
993 static void wlat_test(struct krping_cb *cb)
994 {
995 	int ccnt, scnt, rcnt;
996 	int iters=cb->count;
997 	volatile char *poll_buf = (char *) cb->start_buf;
998 	char *buf = (char *)cb->rdma_buf;
999 	struct timeval start_tv, stop_tv;
1000 	cycles_t *post_cycles_start = NULL;
1001 	cycles_t *post_cycles_stop = NULL;
1002 	cycles_t *poll_cycles_start = NULL;
1003 	cycles_t *poll_cycles_stop = NULL;
1004 	cycles_t *last_poll_cycles_start = NULL;
1005 	cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0;
1006 	int i;
1007 	int cycle_iters = 1000;
1008 
1009 	ccnt = 0;
1010 	scnt = 0;
1011 	rcnt = 0;
1012 
1013 	post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1014 	if (!post_cycles_start) {
1015 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1016 		goto done;
1017 	}
1018 	post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1019 	if (!post_cycles_stop) {
1020 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1021 		goto done;
1022 	}
1023 	poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1024 	if (!poll_cycles_start) {
1025 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1026 		goto done;
1027 	}
1028 	poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1029 	if (!poll_cycles_stop) {
1030 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1031 		goto done;
1032 	}
1033 	last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t),
1034 		GFP_KERNEL);
1035 	if (!last_poll_cycles_start) {
1036 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1037 		goto done;
1038 	}
1039 	cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE;
1040 	cb->rdma_sq_wr.rkey = cb->remote_rkey;
1041 	cb->rdma_sq_wr.remote_addr = cb->remote_addr;
1042 	cb->rdma_sq_wr.wr.sg_list->length = cb->size;
1043 
1044 	if (cycle_iters > iters)
1045 		cycle_iters = iters;
1046 	microtime(&start_tv);
1047 	while (scnt < iters || ccnt < iters || rcnt < iters) {
1048 
1049 		/* Wait till buffer changes. */
1050 		if (rcnt < iters && !(scnt < 1 && !cb->server)) {
1051 			++rcnt;
1052 			while (*poll_buf != (char)rcnt) {
1053 				if (cb->state == ERROR) {
1054 					printk(KERN_ERR PFX
1055 						"state = ERROR, bailing\n");
1056 					goto done;
1057 				}
1058 			}
1059 		}
1060 
1061 		if (scnt < iters) {
1062 			const struct ib_send_wr *bad_wr;
1063 
1064 			*buf = (char)scnt+1;
1065 			if (scnt < cycle_iters)
1066 				post_cycles_start[scnt] = get_cycles();
1067 			if (ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr)) {
1068 				printk(KERN_ERR PFX
1069 					"Couldn't post send: scnt=%d\n",
1070 					scnt);
1071 				goto done;
1072 			}
1073 			if (scnt < cycle_iters)
1074 				post_cycles_stop[scnt] = get_cycles();
1075 			scnt++;
1076 		}
1077 
1078 		if (ccnt < iters) {
1079 			struct ib_wc wc;
1080 			int ne;
1081 
1082 			if (ccnt < cycle_iters)
1083 				poll_cycles_start[ccnt] = get_cycles();
1084 			do {
1085 				if (ccnt < cycle_iters)
1086 					last_poll_cycles_start[ccnt] =
1087 						get_cycles();
1088 				ne = ib_poll_cq(cb->cq, 1, &wc);
1089 			} while (ne == 0);
1090 			if (ccnt < cycle_iters)
1091 				poll_cycles_stop[ccnt] = get_cycles();
1092 			++ccnt;
1093 
1094 			if (ne < 0) {
1095 				printk(KERN_ERR PFX "poll CQ failed %d\n", ne);
1096 				goto done;
1097 			}
1098 			if (wc.status != IB_WC_SUCCESS) {
1099 				printk(KERN_ERR PFX
1100 					"Completion wth error at %s:\n",
1101 					cb->server ? "server" : "client");
1102 				printk(KERN_ERR PFX
1103 					"Failed status %d: wr_id %d\n",
1104 					wc.status, (int) wc.wr_id);
1105 				printk(KERN_ERR PFX
1106 					"scnt=%d, rcnt=%d, ccnt=%d\n",
1107 					scnt, rcnt, ccnt);
1108 				goto done;
1109 			}
1110 		}
1111 	}
1112 	microtime(&stop_tv);
1113 
1114         if (stop_tv.tv_usec < start_tv.tv_usec) {
1115                 stop_tv.tv_usec += 1000000;
1116                 stop_tv.tv_sec  -= 1;
1117         }
1118 
1119 	for (i=0; i < cycle_iters; i++) {
1120 		sum_post += post_cycles_stop[i] - post_cycles_start[i];
1121 		sum_poll += poll_cycles_stop[i] - poll_cycles_start[i];
1122 		sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i];
1123 	}
1124 	printk(KERN_ERR PFX
1125 		"delta sec %lu delta usec %lu iter %d size %d cycle_iters %d"
1126 		" sum_post %llu sum_poll %llu sum_last_poll %llu\n",
1127 		(unsigned long)(stop_tv.tv_sec - start_tv.tv_sec),
1128 		(unsigned long)(stop_tv.tv_usec - start_tv.tv_usec),
1129 		scnt, cb->size, cycle_iters,
1130 		(unsigned long long)sum_post, (unsigned long long)sum_poll,
1131 		(unsigned long long)sum_last_poll);
1132 done:
1133 	kfree(post_cycles_start);
1134 	kfree(post_cycles_stop);
1135 	kfree(poll_cycles_start);
1136 	kfree(poll_cycles_stop);
1137 	kfree(last_poll_cycles_start);
1138 }
1139 
1140 static void bw_test(struct krping_cb *cb)
1141 {
1142 	int ccnt, scnt;
1143 	int iters=cb->count;
1144 	struct timeval start_tv, stop_tv;
1145 	cycles_t *post_cycles_start = NULL;
1146 	cycles_t *post_cycles_stop = NULL;
1147 	cycles_t *poll_cycles_start = NULL;
1148 	cycles_t *poll_cycles_stop = NULL;
1149 	cycles_t *last_poll_cycles_start = NULL;
1150 	cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0;
1151 	int i;
1152 	int cycle_iters = 1000;
1153 
1154 	ccnt = 0;
1155 	scnt = 0;
1156 
1157 	post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1158 	if (!post_cycles_start) {
1159 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1160 		goto done;
1161 	}
1162 	post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1163 	if (!post_cycles_stop) {
1164 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1165 		goto done;
1166 	}
1167 	poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1168 	if (!poll_cycles_start) {
1169 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1170 		goto done;
1171 	}
1172 	poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1173 	if (!poll_cycles_stop) {
1174 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1175 		goto done;
1176 	}
1177 	last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t),
1178 		GFP_KERNEL);
1179 	if (!last_poll_cycles_start) {
1180 		printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__);
1181 		goto done;
1182 	}
1183 	cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE;
1184 	cb->rdma_sq_wr.rkey = cb->remote_rkey;
1185 	cb->rdma_sq_wr.remote_addr = cb->remote_addr;
1186 	cb->rdma_sq_wr.wr.sg_list->length = cb->size;
1187 
1188 	if (cycle_iters > iters)
1189 		cycle_iters = iters;
1190 	microtime(&start_tv);
1191 	while (scnt < iters || ccnt < iters) {
1192 
1193 		while (scnt < iters && scnt - ccnt < cb->txdepth) {
1194 			const struct ib_send_wr *bad_wr;
1195 
1196 			if (scnt < cycle_iters)
1197 				post_cycles_start[scnt] = get_cycles();
1198 			if (ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr)) {
1199 				printk(KERN_ERR PFX
1200 					"Couldn't post send: scnt=%d\n",
1201 					scnt);
1202 				goto done;
1203 			}
1204 			if (scnt < cycle_iters)
1205 				post_cycles_stop[scnt] = get_cycles();
1206 			++scnt;
1207 		}
1208 
1209 		if (ccnt < iters) {
1210 			int ne;
1211 			struct ib_wc wc;
1212 
1213 			if (ccnt < cycle_iters)
1214 				poll_cycles_start[ccnt] = get_cycles();
1215 			do {
1216 				if (ccnt < cycle_iters)
1217 					last_poll_cycles_start[ccnt] =
1218 						get_cycles();
1219 				ne = ib_poll_cq(cb->cq, 1, &wc);
1220 			} while (ne == 0);
1221 			if (ccnt < cycle_iters)
1222 				poll_cycles_stop[ccnt] = get_cycles();
1223 			ccnt += 1;
1224 
1225 			if (ne < 0) {
1226 				printk(KERN_ERR PFX "poll CQ failed %d\n", ne);
1227 				goto done;
1228 			}
1229 			if (wc.status != IB_WC_SUCCESS) {
1230 				printk(KERN_ERR PFX
1231 					"Completion wth error at %s:\n",
1232 					cb->server ? "server" : "client");
1233 				printk(KERN_ERR PFX
1234 					"Failed status %d: wr_id %d\n",
1235 					wc.status, (int) wc.wr_id);
1236 				goto done;
1237 			}
1238 		}
1239 	}
1240 	microtime(&stop_tv);
1241 
1242         if (stop_tv.tv_usec < start_tv.tv_usec) {
1243                 stop_tv.tv_usec += 1000000;
1244                 stop_tv.tv_sec  -= 1;
1245         }
1246 
1247 	for (i=0; i < cycle_iters; i++) {
1248 		sum_post += post_cycles_stop[i] - post_cycles_start[i];
1249 		sum_poll += poll_cycles_stop[i] - poll_cycles_start[i];
1250 		sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i];
1251 	}
1252 	printk(KERN_ERR PFX
1253 		"delta sec %lu delta usec %lu iter %d size %d cycle_iters %d"
1254 		" sum_post %llu sum_poll %llu sum_last_poll %llu\n",
1255 		(unsigned long)(stop_tv.tv_sec - start_tv.tv_sec),
1256 		(unsigned long)(stop_tv.tv_usec - start_tv.tv_usec),
1257 		scnt, cb->size, cycle_iters,
1258 		(unsigned long long)sum_post, (unsigned long long)sum_poll,
1259 		(unsigned long long)sum_last_poll);
1260 done:
1261 	kfree(post_cycles_start);
1262 	kfree(post_cycles_stop);
1263 	kfree(poll_cycles_start);
1264 	kfree(poll_cycles_stop);
1265 	kfree(last_poll_cycles_start);
1266 }
1267 
1268 static void krping_rlat_test_server(struct krping_cb *cb)
1269 {
1270 	const struct ib_send_wr *bad_wr;
1271 	struct ib_wc wc;
1272 	int ret;
1273 
1274 	/* Spin waiting for client's Start STAG/TO/Len */
1275 	while (cb->state < RDMA_READ_ADV) {
1276 		krping_cq_event_handler(cb->cq, cb);
1277 	}
1278 
1279 	/* Send STAG/TO/Len to client */
1280 	krping_format_send(cb, cb->start_dma_addr);
1281 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1282 	if (ret) {
1283 		printk(KERN_ERR PFX "post send error %d\n", ret);
1284 		return;
1285 	}
1286 
1287 	/* Spin waiting for send completion */
1288 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1289 	if (ret < 0) {
1290 		printk(KERN_ERR PFX "poll error %d\n", ret);
1291 		return;
1292 	}
1293 	if (wc.status) {
1294 		printk(KERN_ERR PFX "send completiong error %d\n", wc.status);
1295 		return;
1296 	}
1297 
1298 	wait_event_interruptible(cb->sem, cb->state == ERROR);
1299 }
1300 
1301 static void krping_wlat_test_server(struct krping_cb *cb)
1302 {
1303 	const struct ib_send_wr *bad_wr;
1304 	struct ib_wc wc;
1305 	int ret;
1306 
1307 	/* Spin waiting for client's Start STAG/TO/Len */
1308 	while (cb->state < RDMA_READ_ADV) {
1309 		krping_cq_event_handler(cb->cq, cb);
1310 	}
1311 
1312 	/* Send STAG/TO/Len to client */
1313 	krping_format_send(cb, cb->start_dma_addr);
1314 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1315 	if (ret) {
1316 		printk(KERN_ERR PFX "post send error %d\n", ret);
1317 		return;
1318 	}
1319 
1320 	/* Spin waiting for send completion */
1321 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1322 	if (ret < 0) {
1323 		printk(KERN_ERR PFX "poll error %d\n", ret);
1324 		return;
1325 	}
1326 	if (wc.status) {
1327 		printk(KERN_ERR PFX "send completiong error %d\n", wc.status);
1328 		return;
1329 	}
1330 
1331 	wlat_test(cb);
1332 	wait_event_interruptible(cb->sem, cb->state == ERROR);
1333 }
1334 
1335 static void krping_bw_test_server(struct krping_cb *cb)
1336 {
1337 	const struct ib_send_wr *bad_wr;
1338 	struct ib_wc wc;
1339 	int ret;
1340 
1341 	/* Spin waiting for client's Start STAG/TO/Len */
1342 	while (cb->state < RDMA_READ_ADV) {
1343 		krping_cq_event_handler(cb->cq, cb);
1344 	}
1345 
1346 	/* Send STAG/TO/Len to client */
1347 	krping_format_send(cb, cb->start_dma_addr);
1348 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1349 	if (ret) {
1350 		printk(KERN_ERR PFX "post send error %d\n", ret);
1351 		return;
1352 	}
1353 
1354 	/* Spin waiting for send completion */
1355 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1356 	if (ret < 0) {
1357 		printk(KERN_ERR PFX "poll error %d\n", ret);
1358 		return;
1359 	}
1360 	if (wc.status) {
1361 		printk(KERN_ERR PFX "send completiong error %d\n", wc.status);
1362 		return;
1363 	}
1364 
1365 	if (cb->duplex)
1366 		bw_test(cb);
1367 	wait_event_interruptible(cb->sem, cb->state == ERROR);
1368 }
1369 
1370 static int reg_supported(struct ib_device *dev)
1371 {
1372 	u64 needed_flags = IB_DEVICE_MEM_MGT_EXTENSIONS;
1373 
1374 	if ((dev->attrs.device_cap_flags & needed_flags) != needed_flags) {
1375 		printk(KERN_ERR PFX
1376 			"Fastreg not supported - device_cap_flags 0x%llx\n",
1377 			(unsigned long long)dev->attrs.device_cap_flags);
1378 		return 0;
1379 	}
1380 	DEBUG_LOG("Fastreg supported - device_cap_flags 0x%llx\n",
1381 		(unsigned long long)dev->attrs.device_cap_flags);
1382 	return 1;
1383 }
1384 
1385 static void fill_sockaddr(struct sockaddr_storage *sin, struct krping_cb *cb)
1386 {
1387 	memset(sin, 0, sizeof(*sin));
1388 
1389 	if (cb->addr_type == AF_INET) {
1390 		struct sockaddr_in *sin4 = (struct sockaddr_in *)sin;
1391 		sin4->sin_len = sizeof(*sin4);
1392 		sin4->sin_family = AF_INET;
1393 		memcpy((void *)&sin4->sin_addr.s_addr, cb->addr, 4);
1394 		sin4->sin_port = cb->port;
1395 	} else if (cb->addr_type == AF_INET6) {
1396 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
1397 		sin6->sin6_len = sizeof(*sin6);
1398 		sin6->sin6_family = AF_INET6;
1399 		memcpy((void *)&sin6->sin6_addr, cb->addr, 16);
1400 		sin6->sin6_port = cb->port;
1401 	}
1402 }
1403 
1404 static int krping_bind_server(struct krping_cb *cb)
1405 {
1406 	struct sockaddr_storage sin;
1407 	int ret;
1408 
1409 
1410 	fill_sockaddr(&sin, cb);
1411 
1412 	ret = rdma_bind_addr(cb->cm_id, (struct sockaddr *)&sin);
1413 	if (ret) {
1414 		printk(KERN_ERR PFX "rdma_bind_addr error %d\n", ret);
1415 		return ret;
1416 	}
1417 	DEBUG_LOG("rdma_bind_addr successful\n");
1418 
1419 	DEBUG_LOG("rdma_listen\n");
1420 	ret = rdma_listen(cb->cm_id, 3);
1421 	if (ret) {
1422 		printk(KERN_ERR PFX "rdma_listen failed: %d\n", ret);
1423 		return ret;
1424 	}
1425 
1426 	wait_event_interruptible(cb->sem, cb->state >= CONNECT_REQUEST);
1427 	if (cb->state != CONNECT_REQUEST) {
1428 		printk(KERN_ERR PFX "wait for CONNECT_REQUEST state %d\n",
1429 			cb->state);
1430 		return -1;
1431 	}
1432 
1433 	if (!reg_supported(cb->child_cm_id->device))
1434 		return -EINVAL;
1435 
1436 	return 0;
1437 }
1438 
1439 static void krping_run_server(struct krping_cb *cb)
1440 {
1441 	const struct ib_recv_wr *bad_wr;
1442 	int ret;
1443 
1444 	ret = krping_bind_server(cb);
1445 	if (ret)
1446 		return;
1447 
1448 	ret = krping_setup_qp(cb, cb->child_cm_id);
1449 	if (ret) {
1450 		printk(KERN_ERR PFX "setup_qp failed: %d\n", ret);
1451 		goto err0;
1452 	}
1453 
1454 	ret = krping_setup_buffers(cb);
1455 	if (ret) {
1456 		printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret);
1457 		goto err1;
1458 	}
1459 
1460 	ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
1461 	if (ret) {
1462 		printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
1463 		goto err2;
1464 	}
1465 
1466 	ret = krping_accept(cb);
1467 	if (ret) {
1468 		printk(KERN_ERR PFX "connect error %d\n", ret);
1469 		goto err2;
1470 	}
1471 
1472 	if (cb->wlat)
1473 		krping_wlat_test_server(cb);
1474 	else if (cb->rlat)
1475 		krping_rlat_test_server(cb);
1476 	else if (cb->bw)
1477 		krping_bw_test_server(cb);
1478 	else
1479 		krping_test_server(cb);
1480 	rdma_disconnect(cb->child_cm_id);
1481 err2:
1482 	krping_free_buffers(cb);
1483 err1:
1484 	krping_free_qp(cb);
1485 err0:
1486 	rdma_destroy_id(cb->child_cm_id);
1487 }
1488 
1489 static void krping_test_client(struct krping_cb *cb)
1490 {
1491 	int ping, start, cc, i, ret;
1492 	const struct ib_send_wr *bad_wr;
1493 	unsigned char c;
1494 
1495 	start = 65;
1496 	for (ping = 0; !cb->count || ping < cb->count; ping++) {
1497 		cb->state = RDMA_READ_ADV;
1498 
1499 		/* Put some ascii text in the buffer. */
1500 		cc = sprintf(cb->start_buf, "rdma-ping-%d: ", ping);
1501 		for (i = cc, c = start; i < cb->size; i++) {
1502 			cb->start_buf[i] = c;
1503 			c++;
1504 			if (c > 122)
1505 				c = 65;
1506 		}
1507 		start++;
1508 		if (start > 122)
1509 			start = 65;
1510 		cb->start_buf[cb->size - 1] = 0;
1511 
1512 		krping_format_send(cb, cb->start_dma_addr);
1513 		if (cb->state == ERROR) {
1514 			printk(KERN_ERR PFX "krping_format_send failed\n");
1515 			break;
1516 		}
1517 		ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1518 		if (ret) {
1519 			printk(KERN_ERR PFX "post send error %d\n", ret);
1520 			break;
1521 		}
1522 
1523 		/* Wait for server to ACK */
1524 		wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV);
1525 		if (cb->state != RDMA_WRITE_ADV) {
1526 			printk(KERN_ERR PFX
1527 			       "wait for RDMA_WRITE_ADV state %d\n",
1528 			       cb->state);
1529 			break;
1530 		}
1531 
1532 		krping_format_send(cb, cb->rdma_dma_addr);
1533 		ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1534 		if (ret) {
1535 			printk(KERN_ERR PFX "post send error %d\n", ret);
1536 			break;
1537 		}
1538 
1539 		/* Wait for the server to say the RDMA Write is complete. */
1540 		wait_event_interruptible(cb->sem,
1541 					 cb->state >= RDMA_WRITE_COMPLETE);
1542 		if (cb->state != RDMA_WRITE_COMPLETE) {
1543 			printk(KERN_ERR PFX
1544 			       "wait for RDMA_WRITE_COMPLETE state %d\n",
1545 			       cb->state);
1546 			break;
1547 		}
1548 
1549 		if (cb->validate)
1550 			if (memcmp(cb->start_buf, cb->rdma_buf, cb->size)) {
1551 				printk(KERN_ERR PFX "data mismatch!\n");
1552 				break;
1553 			}
1554 
1555 		if (cb->verbose)
1556 			printk(KERN_INFO PFX "ping data: %s\n", cb->rdma_buf);
1557 #ifdef SLOW_KRPING
1558 		wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ);
1559 #endif
1560 	}
1561 }
1562 
1563 static void krping_rlat_test_client(struct krping_cb *cb)
1564 {
1565 	const struct ib_send_wr *bad_wr;
1566 	struct ib_wc wc;
1567 	int ret;
1568 
1569 	cb->state = RDMA_READ_ADV;
1570 
1571 	/* Send STAG/TO/Len to client */
1572 	krping_format_send(cb, cb->start_dma_addr);
1573 	if (cb->state == ERROR) {
1574 		printk(KERN_ERR PFX "krping_format_send failed\n");
1575 		return;
1576 	}
1577 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1578 	if (ret) {
1579 		printk(KERN_ERR PFX "post send error %d\n", ret);
1580 		return;
1581 	}
1582 
1583 	/* Spin waiting for send completion */
1584 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1585 	if (ret < 0) {
1586 		printk(KERN_ERR PFX "poll error %d\n", ret);
1587 		return;
1588 	}
1589 	if (wc.status) {
1590 		printk(KERN_ERR PFX "send completion error %d\n", wc.status);
1591 		return;
1592 	}
1593 
1594 	/* Spin waiting for server's Start STAG/TO/Len */
1595 	while (cb->state < RDMA_WRITE_ADV) {
1596 		krping_cq_event_handler(cb->cq, cb);
1597 	}
1598 
1599 #if 0
1600 {
1601 	int i;
1602 	struct timeval start, stop;
1603 	time_t sec;
1604 	suseconds_t usec;
1605 	unsigned long long elapsed;
1606 	struct ib_wc wc;
1607 	const struct ib_send_wr *bad_wr;
1608 	int ne;
1609 
1610 	cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE;
1611 	cb->rdma_sq_wr.rkey = cb->remote_rkey;
1612 	cb->rdma_sq_wr.remote_addr = cb->remote_addr;
1613 	cb->rdma_sq_wr.wr.sg_list->length = 0;
1614 	cb->rdma_sq_wr.wr.num_sge = 0;
1615 
1616 	microtime(&start);
1617 	for (i=0; i < 100000; i++) {
1618 		if (ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr)) {
1619 			printk(KERN_ERR PFX  "Couldn't post send\n");
1620 			return;
1621 		}
1622 		do {
1623 			ne = ib_poll_cq(cb->cq, 1, &wc);
1624 		} while (ne == 0);
1625 		if (ne < 0) {
1626 			printk(KERN_ERR PFX "poll CQ failed %d\n", ne);
1627 			return;
1628 		}
1629 		if (wc.status != IB_WC_SUCCESS) {
1630 			printk(KERN_ERR PFX "Completion wth error at %s:\n",
1631 				cb->server ? "server" : "client");
1632 			printk(KERN_ERR PFX "Failed status %d: wr_id %d\n",
1633 				wc.status, (int) wc.wr_id);
1634 			return;
1635 		}
1636 	}
1637 	microtime(&stop);
1638 
1639 	if (stop.tv_usec < start.tv_usec) {
1640 		stop.tv_usec += 1000000;
1641 		stop.tv_sec  -= 1;
1642 	}
1643 	sec     = stop.tv_sec - start.tv_sec;
1644 	usec    = stop.tv_usec - start.tv_usec;
1645 	elapsed = sec * 1000000 + usec;
1646 	printk(KERN_ERR PFX "0B-write-lat iters 100000 usec %llu\n", elapsed);
1647 }
1648 #endif
1649 
1650 	rlat_test(cb);
1651 }
1652 
1653 static void krping_wlat_test_client(struct krping_cb *cb)
1654 {
1655 	const struct ib_send_wr *bad_wr;
1656 	struct ib_wc wc;
1657 	int ret;
1658 
1659 	cb->state = RDMA_READ_ADV;
1660 
1661 	/* Send STAG/TO/Len to client */
1662 	krping_format_send(cb, cb->start_dma_addr);
1663 	if (cb->state == ERROR) {
1664 		printk(KERN_ERR PFX "krping_format_send failed\n");
1665 		return;
1666 	}
1667 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1668 	if (ret) {
1669 		printk(KERN_ERR PFX "post send error %d\n", ret);
1670 		return;
1671 	}
1672 
1673 	/* Spin waiting for send completion */
1674 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1675 	if (ret < 0) {
1676 		printk(KERN_ERR PFX "poll error %d\n", ret);
1677 		return;
1678 	}
1679 	if (wc.status) {
1680 		printk(KERN_ERR PFX "send completion error %d\n", wc.status);
1681 		return;
1682 	}
1683 
1684 	/* Spin waiting for server's Start STAG/TO/Len */
1685 	while (cb->state < RDMA_WRITE_ADV) {
1686 		krping_cq_event_handler(cb->cq, cb);
1687 	}
1688 
1689 	wlat_test(cb);
1690 }
1691 
1692 static void krping_bw_test_client(struct krping_cb *cb)
1693 {
1694 	const struct ib_send_wr *bad_wr;
1695 	struct ib_wc wc;
1696 	int ret;
1697 
1698 	cb->state = RDMA_READ_ADV;
1699 
1700 	/* Send STAG/TO/Len to client */
1701 	krping_format_send(cb, cb->start_dma_addr);
1702 	if (cb->state == ERROR) {
1703 		printk(KERN_ERR PFX "krping_format_send failed\n");
1704 		return;
1705 	}
1706 	ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1707 	if (ret) {
1708 		printk(KERN_ERR PFX "post send error %d\n", ret);
1709 		return;
1710 	}
1711 
1712 	/* Spin waiting for send completion */
1713 	while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1714 	if (ret < 0) {
1715 		printk(KERN_ERR PFX "poll error %d\n", ret);
1716 		return;
1717 	}
1718 	if (wc.status) {
1719 		printk(KERN_ERR PFX "send completion error %d\n", wc.status);
1720 		return;
1721 	}
1722 
1723 	/* Spin waiting for server's Start STAG/TO/Len */
1724 	while (cb->state < RDMA_WRITE_ADV) {
1725 		krping_cq_event_handler(cb->cq, cb);
1726 	}
1727 
1728 	bw_test(cb);
1729 }
1730 
1731 /*
1732  * Manual qp flush test
1733  */
1734 static void flush_qp(struct krping_cb *cb)
1735 {
1736 	struct ib_send_wr wr = { 0 };
1737 	const struct ib_send_wr *bad;
1738 	struct ib_recv_wr recv_wr = { 0 };
1739 	const struct ib_recv_wr *recv_bad;
1740 	struct ib_wc wc;
1741 	int ret;
1742 	int flushed = 0;
1743 	int ccnt = 0;
1744 
1745 	rdma_disconnect(cb->cm_id);
1746 	DEBUG_LOG("disconnected!\n");
1747 
1748 	wr.opcode = IB_WR_SEND;
1749 	wr.wr_id = 0xdeadbeefcafebabe;
1750 	ret = ib_post_send(cb->qp, &wr, &bad);
1751 	if (ret) {
1752 		printk(KERN_ERR PFX "%s post_send failed ret %d\n", __func__, ret);
1753 		return;
1754 	}
1755 
1756 	recv_wr.wr_id = 0xcafebabedeadbeef;
1757 	ret = ib_post_recv(cb->qp, &recv_wr, &recv_bad);
1758 	if (ret) {
1759 		printk(KERN_ERR PFX "%s post_recv failed ret %d\n", __func__, ret);
1760 		return;
1761 	}
1762 
1763 	/* poll until the flush WRs complete */
1764 	do {
1765 		ret = ib_poll_cq(cb->cq, 1, &wc);
1766 		if (ret < 0) {
1767 			printk(KERN_ERR PFX "ib_poll_cq failed %d\n", ret);
1768 			return;
1769 		}
1770 		if (ret == 0)
1771 			continue;
1772 		ccnt++;
1773 		if (wc.wr_id == 0xdeadbeefcafebabe ||
1774 		    wc.wr_id == 0xcafebabedeadbeef)
1775 			flushed++;
1776 	} while (flushed != 2);
1777 	DEBUG_LOG("qp_flushed! ccnt %u\n", ccnt);
1778 }
1779 
1780 static void krping_fr_test(struct krping_cb *cb)
1781 {
1782 	struct ib_send_wr inv;
1783 	const struct ib_send_wr *bad;
1784 	struct ib_reg_wr fr;
1785 	struct ib_wc wc;
1786 	u8 key = 0;
1787 	struct ib_mr *mr;
1788 	int ret;
1789 	int size = cb->size;
1790 	int plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1791 	unsigned long start;
1792 	int count = 0;
1793 	int scnt = 0;
1794 	struct scatterlist sg = {0};
1795 
1796 	mr = ib_alloc_mr(cb->pd, IB_MR_TYPE_MEM_REG, plen);
1797 	if (IS_ERR(mr)) {
1798 		printk(KERN_ERR PFX "ib_alloc_mr failed %ld\n", PTR_ERR(mr));
1799 		return;
1800 	}
1801 
1802 	sg_dma_address(&sg) = (dma_addr_t)0xcafebabe0000ULL;
1803 	sg_dma_len(&sg) = size;
1804 	ret = ib_map_mr_sg(mr, &sg, 1, NULL, PAGE_SIZE);
1805 	if (ret <= 0) {
1806 		printk(KERN_ERR PFX "ib_map_mr_sge err %d\n", ret);
1807 		goto err2;
1808 	}
1809 
1810 	memset(&fr, 0, sizeof fr);
1811 	fr.wr.opcode = IB_WR_REG_MR;
1812 	fr.access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
1813 	fr.mr = mr;
1814 	fr.wr.next = &inv;
1815 
1816 	memset(&inv, 0, sizeof inv);
1817 	inv.opcode = IB_WR_LOCAL_INV;
1818 	inv.send_flags = IB_SEND_SIGNALED;
1819 
1820 	DEBUG_LOG("fr_test: stag index 0x%x plen %u size %u depth %u\n", mr->rkey >> 8, plen, cb->size, cb->txdepth);
1821 	start = time_uptime;
1822 	while (!cb->count || count <= cb->count) {
1823 		if (SIGPENDING(curthread)) {
1824 			printk(KERN_ERR PFX "signal!\n");
1825 			break;
1826 		}
1827 		if ((time_uptime - start) >= 9) {
1828 			DEBUG_LOG("fr_test: pausing 1 second! count %u latest size %u plen %u\n", count, size, plen);
1829 			wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ);
1830 			if (cb->state == ERROR)
1831 				break;
1832 			start = time_uptime;
1833 		}
1834 		while (scnt < (cb->txdepth>>1)) {
1835 			ib_update_fast_reg_key(mr, ++key);
1836 			fr.key = mr->rkey;
1837 			inv.ex.invalidate_rkey = mr->rkey;
1838 
1839 			size = arc4random() % cb->size;
1840 			if (size == 0)
1841 				size = cb->size;
1842 			sg_dma_len(&sg) = size;
1843 			ret = ib_map_mr_sg(mr, &sg, 1, NULL, PAGE_SIZE);
1844 			if (ret <= 0) {
1845 				printk(KERN_ERR PFX "ib_map_mr_sge err %d\n", ret);
1846 				goto err2;
1847 			}
1848 			ret = ib_post_send(cb->qp, &fr.wr, &bad);
1849 			if (ret) {
1850 				printk(KERN_ERR PFX "ib_post_send failed %d\n", ret);
1851 				goto err2;
1852 			}
1853 			scnt++;
1854 		}
1855 
1856 		ret = ib_poll_cq(cb->cq, 1, &wc);
1857 		if (ret < 0) {
1858 			printk(KERN_ERR PFX "ib_poll_cq failed %d\n", ret);
1859 			goto err2;
1860 		}
1861 		if (ret == 1) {
1862 			if (wc.status) {
1863 				printk(KERN_ERR PFX "completion error %u\n", wc.status);
1864 				goto err2;
1865 			}
1866 			count++;
1867 			scnt--;
1868 		}
1869 	}
1870 err2:
1871 	flush_qp(cb);
1872 	DEBUG_LOG("fr_test: done!\n");
1873 	ib_dereg_mr(mr);
1874 }
1875 
1876 static int krping_connect_client(struct krping_cb *cb)
1877 {
1878 	struct rdma_conn_param conn_param;
1879 	int ret;
1880 
1881 	memset(&conn_param, 0, sizeof conn_param);
1882 	conn_param.responder_resources = 1;
1883 	conn_param.initiator_depth = 1;
1884 	conn_param.retry_count = 10;
1885 
1886 	ret = rdma_connect(cb->cm_id, &conn_param);
1887 	if (ret) {
1888 		printk(KERN_ERR PFX "rdma_connect error %d\n", ret);
1889 		return ret;
1890 	}
1891 
1892 	wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
1893 	if (cb->state == ERROR) {
1894 		printk(KERN_ERR PFX "wait for CONNECTED state %d\n", cb->state);
1895 		return -1;
1896 	}
1897 
1898 	DEBUG_LOG("rdma_connect successful\n");
1899 	return 0;
1900 }
1901 
1902 static int krping_bind_client(struct krping_cb *cb)
1903 {
1904 	struct sockaddr_storage sin;
1905 	int ret;
1906 
1907 	fill_sockaddr(&sin, cb);
1908 
1909 	ret = rdma_resolve_addr(cb->cm_id, NULL, (struct sockaddr *)&sin, 2000);
1910 	if (ret) {
1911 		printk(KERN_ERR PFX "rdma_resolve_addr error %d\n", ret);
1912 		return ret;
1913 	}
1914 
1915 	wait_event_interruptible(cb->sem, cb->state >= ROUTE_RESOLVED);
1916 	if (cb->state != ROUTE_RESOLVED) {
1917 		printk(KERN_ERR PFX
1918 		       "addr/route resolution did not resolve: state %d\n",
1919 		       cb->state);
1920 		return -EINTR;
1921 	}
1922 
1923 	if (!reg_supported(cb->cm_id->device))
1924 		return -EINVAL;
1925 
1926 	DEBUG_LOG("rdma_resolve_addr - rdma_resolve_route successful\n");
1927 	return 0;
1928 }
1929 
1930 static void krping_run_client(struct krping_cb *cb)
1931 {
1932 	const struct ib_recv_wr *bad_wr;
1933 	int ret;
1934 
1935 	/* set type of service, if any */
1936 	if (cb->tos != 0)
1937 		rdma_set_service_type(cb->cm_id, cb->tos);
1938 
1939 	ret = krping_bind_client(cb);
1940 	if (ret)
1941 		return;
1942 
1943 	ret = krping_setup_qp(cb, cb->cm_id);
1944 	if (ret) {
1945 		printk(KERN_ERR PFX "setup_qp failed: %d\n", ret);
1946 		return;
1947 	}
1948 
1949 	ret = krping_setup_buffers(cb);
1950 	if (ret) {
1951 		printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret);
1952 		goto err1;
1953 	}
1954 
1955 	ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
1956 	if (ret) {
1957 		printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
1958 		goto err2;
1959 	}
1960 
1961 	ret = krping_connect_client(cb);
1962 	if (ret) {
1963 		printk(KERN_ERR PFX "connect error %d\n", ret);
1964 		goto err2;
1965 	}
1966 
1967 	if (cb->wlat)
1968 		krping_wlat_test_client(cb);
1969 	else if (cb->rlat)
1970 		krping_rlat_test_client(cb);
1971 	else if (cb->bw)
1972 		krping_bw_test_client(cb);
1973 	else if (cb->frtest)
1974 		krping_fr_test(cb);
1975 	else
1976 		krping_test_client(cb);
1977 	rdma_disconnect(cb->cm_id);
1978 err2:
1979 	krping_free_buffers(cb);
1980 err1:
1981 	krping_free_qp(cb);
1982 }
1983 
1984 static uint16_t
1985 krping_get_ipv6_scope_id(char *name)
1986 {
1987 	struct ifnet *ifp;
1988 	uint16_t retval;
1989 
1990 	if (name == NULL)
1991 		return (0);
1992 	CURVNET_SET_QUIET(TD_TO_VNET(curthread));
1993 	ifp = ifunit_ref(name);
1994 	CURVNET_RESTORE();
1995 	if (ifp == NULL)
1996 		return (0);
1997 	retval = ifp->if_index;
1998 	if_rele(ifp);
1999 	return (retval);
2000 }
2001 
2002 int krping_doit(char *cmd)
2003 {
2004 	struct krping_cb *cb;
2005 	int op;
2006 	int ret = 0;
2007 	char *optarg;
2008 	char *scope;
2009 	unsigned long optint;
2010 
2011 	cb = kzalloc(sizeof(*cb), GFP_KERNEL);
2012 	if (!cb)
2013 		return -ENOMEM;
2014 
2015 	mutex_lock(&krping_mutex);
2016 	list_add_tail(&cb->list, &krping_cbs);
2017 	mutex_unlock(&krping_mutex);
2018 
2019 	cb->server = -1;
2020 	cb->state = IDLE;
2021 	cb->size = 64;
2022 	cb->txdepth = RPING_SQ_DEPTH;
2023 	init_waitqueue_head(&cb->sem);
2024 
2025 	while ((op = krping_getopt("krping", &cmd, krping_opts, NULL, &optarg,
2026 			      &optint)) != 0) {
2027 		switch (op) {
2028 		case 'a':
2029 			cb->addr_str = optarg;
2030 			cb->addr_type = AF_INET;
2031 			DEBUG_LOG("ipaddr (%s)\n", optarg);
2032 			if (inet_pton(AF_INET, optarg, cb->addr) != 1) {
2033 				printk(KERN_ERR PFX "bad addr string %s\n",
2034 				    optarg);
2035 				ret = EINVAL;
2036 			}
2037 			break;
2038 		case 'A':
2039 			cb->addr_str = optarg;
2040 			cb->addr_type = AF_INET6;
2041 			DEBUG_LOG("ipv6addr (%s)\n", optarg);
2042 			scope = strstr(optarg, "%");
2043 			/* extract scope ID, if any */
2044 			if (scope != NULL)
2045 				*scope++ = 0;
2046 			/* extract IPv6 network address */
2047 			if (inet_pton(AF_INET6, optarg, cb->addr) != 1) {
2048 				printk(KERN_ERR PFX "bad addr string %s\n",
2049 				    optarg);
2050 				ret = EINVAL;
2051 			} else if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)cb->addr) ||
2052 			    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)cb->addr)) {
2053 				uint16_t scope_id = krping_get_ipv6_scope_id(scope);
2054 				DEBUG_LOG("ipv6 scope ID = %d\n", scope_id);
2055 				cb->addr[2] = scope_id >> 8;
2056 				cb->addr[3] = scope_id & 0xFF;
2057 			}
2058 			break;
2059 		case 'p':
2060 			cb->port = htons(optint);
2061 			DEBUG_LOG("port %d\n", (int)optint);
2062 			break;
2063 		case 'P':
2064 			cb->poll = 1;
2065 			DEBUG_LOG("server\n");
2066 			break;
2067 		case 's':
2068 			cb->server = 1;
2069 			DEBUG_LOG("server\n");
2070 			break;
2071 		case 'c':
2072 			cb->server = 0;
2073 			DEBUG_LOG("client\n");
2074 			break;
2075 		case 'S':
2076 			cb->size = optint;
2077 			if ((cb->size < 1) ||
2078 			    (cb->size > RPING_BUFSIZE)) {
2079 				printk(KERN_ERR PFX "Invalid size %d "
2080 				       "(valid range is 1 to %d)\n",
2081 				       cb->size, RPING_BUFSIZE);
2082 				ret = EINVAL;
2083 			} else
2084 				DEBUG_LOG("size %d\n", (int)optint);
2085 			break;
2086 		case 'C':
2087 			cb->count = optint;
2088 			if (cb->count < 0) {
2089 				printk(KERN_ERR PFX "Invalid count %d\n",
2090 					cb->count);
2091 				ret = EINVAL;
2092 			} else
2093 				DEBUG_LOG("count %d\n", (int) cb->count);
2094 			break;
2095 		case 'v':
2096 			cb->verbose++;
2097 			DEBUG_LOG("verbose\n");
2098 			break;
2099 		case 'V':
2100 			cb->validate++;
2101 			DEBUG_LOG("validate data\n");
2102 			break;
2103 		case 'l':
2104 			cb->wlat++;
2105 			break;
2106 		case 'L':
2107 			cb->rlat++;
2108 			break;
2109 		case 'B':
2110 			cb->bw++;
2111 			break;
2112 		case 'd':
2113 			cb->duplex++;
2114 			break;
2115 		case 'I':
2116 			cb->server_invalidate = 1;
2117 			break;
2118 		case 't':
2119 			cb->tos = optint;
2120 			DEBUG_LOG("type of service, tos=%d\n", (int) cb->tos);
2121 			break;
2122 		case 'T':
2123 			cb->txdepth = optint;
2124 			DEBUG_LOG("txdepth %d\n", (int) cb->txdepth);
2125 			break;
2126 		case 'Z':
2127 			cb->local_dma_lkey = 1;
2128 			DEBUG_LOG("using local dma lkey\n");
2129 			break;
2130 		case 'R':
2131 			cb->read_inv = 1;
2132 			DEBUG_LOG("using read-with-inv\n");
2133 			break;
2134 		case 'f':
2135 			cb->frtest = 1;
2136 			DEBUG_LOG("fast-reg test!\n");
2137 			break;
2138 		default:
2139 			printk(KERN_ERR PFX "unknown opt %s\n", optarg);
2140 			ret = -EINVAL;
2141 			break;
2142 		}
2143 	}
2144 	if (ret)
2145 		goto out;
2146 
2147 	if (cb->server == -1) {
2148 		printk(KERN_ERR PFX "must be either client or server\n");
2149 		ret = -EINVAL;
2150 		goto out;
2151 	}
2152 
2153 	if (cb->server && cb->frtest) {
2154 		printk(KERN_ERR PFX "must be client to run frtest\n");
2155 		ret = -EINVAL;
2156 		goto out;
2157 	}
2158 
2159 	if ((cb->frtest + cb->bw + cb->rlat + cb->wlat) > 1) {
2160 		printk(KERN_ERR PFX "Pick only one test: fr, bw, rlat, wlat\n");
2161 		ret = -EINVAL;
2162 		goto out;
2163 	}
2164 
2165 	if (cb->wlat || cb->rlat || cb->bw) {
2166 		printk(KERN_ERR PFX "wlat, rlat, and bw tests only support mem_mode MR - which is no longer supported\n");
2167 		ret = -EINVAL;
2168 		goto out;
2169 	}
2170 
2171 	cb->cm_id = rdma_create_id(TD_TO_VNET(curthread), krping_cma_event_handler, cb, RDMA_PS_TCP, IB_QPT_RC);
2172 	if (IS_ERR(cb->cm_id)) {
2173 		ret = PTR_ERR(cb->cm_id);
2174 		printk(KERN_ERR PFX "rdma_create_id error %d\n", ret);
2175 		goto out;
2176 	}
2177 	DEBUG_LOG("created cm_id %p\n", cb->cm_id);
2178 
2179 	if (cb->server)
2180 		krping_run_server(cb);
2181 	else
2182 		krping_run_client(cb);
2183 
2184 	DEBUG_LOG("destroy cm_id %p\n", cb->cm_id);
2185 	rdma_destroy_id(cb->cm_id);
2186 out:
2187 	mutex_lock(&krping_mutex);
2188 	list_del(&cb->list);
2189 	mutex_unlock(&krping_mutex);
2190 	kfree(cb);
2191 	return ret;
2192 }
2193 
2194 void
2195 krping_walk_cb_list(void (*f)(struct krping_stats *, void *), void *arg)
2196 {
2197 	struct krping_cb *cb;
2198 
2199 	mutex_lock(&krping_mutex);
2200 	list_for_each_entry(cb, &krping_cbs, list)
2201 	    (*f)(cb->pd ? &cb->stats : NULL, arg);
2202 	mutex_unlock(&krping_mutex);
2203 }
2204 
2205 void
2206 krping_cancel_all(void)
2207 {
2208 	struct krping_cb *cb;
2209 
2210 	mutex_lock(&krping_mutex);
2211 	list_for_each_entry(cb, &krping_cbs, list) {
2212 		cb->state = ERROR;
2213 		wake_up_interruptible(&cb->sem);
2214 	}
2215 	mutex_unlock(&krping_mutex);
2216 }
2217 
2218