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