xref: /linux/net/sunrpc/xprtrdma/svc_rdma_transport.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /*
2  * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the BSD-type
8  * license below:
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  *      Redistributions of source code must retain the above copyright
15  *      notice, this list of conditions and the following disclaimer.
16  *
17  *      Redistributions in binary form must reproduce the above
18  *      copyright notice, this list of conditions and the following
19  *      disclaimer in the documentation and/or other materials provided
20  *      with the distribution.
21  *
22  *      Neither the name of the Network Appliance, Inc. nor the names of
23  *      its contributors may be used to endorse or promote products
24  *      derived from this software without specific prior written
25  *      permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  * Author: Tom Tucker <tom@opengridcomputing.com>
40  */
41 
42 #include <linux/sunrpc/svc_xprt.h>
43 #include <linux/sunrpc/debug.h>
44 #include <linux/sunrpc/rpc_rdma.h>
45 #include <linux/interrupt.h>
46 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/workqueue.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/rdma_cm.h>
52 #include <linux/sunrpc/svc_rdma.h>
53 #include <linux/export.h>
54 
55 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
56 
57 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
58 					struct net *net,
59 					struct sockaddr *sa, int salen,
60 					int flags);
61 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
62 static void svc_rdma_release_rqst(struct svc_rqst *);
63 static void dto_tasklet_func(unsigned long data);
64 static void svc_rdma_detach(struct svc_xprt *xprt);
65 static void svc_rdma_free(struct svc_xprt *xprt);
66 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
67 static void rq_cq_reap(struct svcxprt_rdma *xprt);
68 static void sq_cq_reap(struct svcxprt_rdma *xprt);
69 
70 static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
71 static DEFINE_SPINLOCK(dto_lock);
72 static LIST_HEAD(dto_xprt_q);
73 
74 static struct svc_xprt_ops svc_rdma_ops = {
75 	.xpo_create = svc_rdma_create,
76 	.xpo_recvfrom = svc_rdma_recvfrom,
77 	.xpo_sendto = svc_rdma_sendto,
78 	.xpo_release_rqst = svc_rdma_release_rqst,
79 	.xpo_detach = svc_rdma_detach,
80 	.xpo_free = svc_rdma_free,
81 	.xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
82 	.xpo_has_wspace = svc_rdma_has_wspace,
83 	.xpo_accept = svc_rdma_accept,
84 };
85 
86 struct svc_xprt_class svc_rdma_class = {
87 	.xcl_name = "rdma",
88 	.xcl_owner = THIS_MODULE,
89 	.xcl_ops = &svc_rdma_ops,
90 	.xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
91 };
92 
93 /* WR context cache. Created in svc_rdma.c  */
94 extern struct kmem_cache *svc_rdma_ctxt_cachep;
95 
96 /* Workqueue created in svc_rdma.c */
97 extern struct workqueue_struct *svc_rdma_wq;
98 
99 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
100 {
101 	struct svc_rdma_op_ctxt *ctxt;
102 
103 	while (1) {
104 		ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
105 		if (ctxt)
106 			break;
107 		schedule_timeout_uninterruptible(msecs_to_jiffies(500));
108 	}
109 	ctxt->xprt = xprt;
110 	INIT_LIST_HEAD(&ctxt->dto_q);
111 	ctxt->count = 0;
112 	ctxt->frmr = NULL;
113 	atomic_inc(&xprt->sc_ctxt_used);
114 	return ctxt;
115 }
116 
117 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
118 {
119 	struct svcxprt_rdma *xprt = ctxt->xprt;
120 	int i;
121 	for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
122 		/*
123 		 * Unmap the DMA addr in the SGE if the lkey matches
124 		 * the sc_dma_lkey, otherwise, ignore it since it is
125 		 * an FRMR lkey and will be unmapped later when the
126 		 * last WR that uses it completes.
127 		 */
128 		if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
129 			atomic_dec(&xprt->sc_dma_used);
130 			ib_dma_unmap_page(xprt->sc_cm_id->device,
131 					    ctxt->sge[i].addr,
132 					    ctxt->sge[i].length,
133 					    ctxt->direction);
134 		}
135 	}
136 }
137 
138 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
139 {
140 	struct svcxprt_rdma *xprt;
141 	int i;
142 
143 	BUG_ON(!ctxt);
144 	xprt = ctxt->xprt;
145 	if (free_pages)
146 		for (i = 0; i < ctxt->count; i++)
147 			put_page(ctxt->pages[i]);
148 
149 	kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
150 	atomic_dec(&xprt->sc_ctxt_used);
151 }
152 
153 /* Temporary NFS request map cache. Created in svc_rdma.c  */
154 extern struct kmem_cache *svc_rdma_map_cachep;
155 
156 /*
157  * Temporary NFS req mappings are shared across all transport
158  * instances. These are short lived and should be bounded by the number
159  * of concurrent server threads * depth of the SQ.
160  */
161 struct svc_rdma_req_map *svc_rdma_get_req_map(void)
162 {
163 	struct svc_rdma_req_map *map;
164 	while (1) {
165 		map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
166 		if (map)
167 			break;
168 		schedule_timeout_uninterruptible(msecs_to_jiffies(500));
169 	}
170 	map->count = 0;
171 	map->frmr = NULL;
172 	return map;
173 }
174 
175 void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
176 {
177 	kmem_cache_free(svc_rdma_map_cachep, map);
178 }
179 
180 /* ib_cq event handler */
181 static void cq_event_handler(struct ib_event *event, void *context)
182 {
183 	struct svc_xprt *xprt = context;
184 	dprintk("svcrdma: received CQ event id=%d, context=%p\n",
185 		event->event, context);
186 	set_bit(XPT_CLOSE, &xprt->xpt_flags);
187 }
188 
189 /* QP event handler */
190 static void qp_event_handler(struct ib_event *event, void *context)
191 {
192 	struct svc_xprt *xprt = context;
193 
194 	switch (event->event) {
195 	/* These are considered benign events */
196 	case IB_EVENT_PATH_MIG:
197 	case IB_EVENT_COMM_EST:
198 	case IB_EVENT_SQ_DRAINED:
199 	case IB_EVENT_QP_LAST_WQE_REACHED:
200 		dprintk("svcrdma: QP event %d received for QP=%p\n",
201 			event->event, event->element.qp);
202 		break;
203 	/* These are considered fatal events */
204 	case IB_EVENT_PATH_MIG_ERR:
205 	case IB_EVENT_QP_FATAL:
206 	case IB_EVENT_QP_REQ_ERR:
207 	case IB_EVENT_QP_ACCESS_ERR:
208 	case IB_EVENT_DEVICE_FATAL:
209 	default:
210 		dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
211 			"closing transport\n",
212 			event->event, event->element.qp);
213 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
214 		break;
215 	}
216 }
217 
218 /*
219  * Data Transfer Operation Tasklet
220  *
221  * Walks a list of transports with I/O pending, removing entries as
222  * they are added to the server's I/O pending list. Two bits indicate
223  * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
224  * spinlock that serializes access to the transport list with the RQ
225  * and SQ interrupt handlers.
226  */
227 static void dto_tasklet_func(unsigned long data)
228 {
229 	struct svcxprt_rdma *xprt;
230 	unsigned long flags;
231 
232 	spin_lock_irqsave(&dto_lock, flags);
233 	while (!list_empty(&dto_xprt_q)) {
234 		xprt = list_entry(dto_xprt_q.next,
235 				  struct svcxprt_rdma, sc_dto_q);
236 		list_del_init(&xprt->sc_dto_q);
237 		spin_unlock_irqrestore(&dto_lock, flags);
238 
239 		rq_cq_reap(xprt);
240 		sq_cq_reap(xprt);
241 
242 		svc_xprt_put(&xprt->sc_xprt);
243 		spin_lock_irqsave(&dto_lock, flags);
244 	}
245 	spin_unlock_irqrestore(&dto_lock, flags);
246 }
247 
248 /*
249  * Receive Queue Completion Handler
250  *
251  * Since an RQ completion handler is called on interrupt context, we
252  * need to defer the handling of the I/O to a tasklet
253  */
254 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
255 {
256 	struct svcxprt_rdma *xprt = cq_context;
257 	unsigned long flags;
258 
259 	/* Guard against unconditional flush call for destroyed QP */
260 	if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
261 		return;
262 
263 	/*
264 	 * Set the bit regardless of whether or not it's on the list
265 	 * because it may be on the list already due to an SQ
266 	 * completion.
267 	 */
268 	set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
269 
270 	/*
271 	 * If this transport is not already on the DTO transport queue,
272 	 * add it
273 	 */
274 	spin_lock_irqsave(&dto_lock, flags);
275 	if (list_empty(&xprt->sc_dto_q)) {
276 		svc_xprt_get(&xprt->sc_xprt);
277 		list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
278 	}
279 	spin_unlock_irqrestore(&dto_lock, flags);
280 
281 	/* Tasklet does all the work to avoid irqsave locks. */
282 	tasklet_schedule(&dto_tasklet);
283 }
284 
285 /*
286  * rq_cq_reap - Process the RQ CQ.
287  *
288  * Take all completing WC off the CQE and enqueue the associated DTO
289  * context on the dto_q for the transport.
290  *
291  * Note that caller must hold a transport reference.
292  */
293 static void rq_cq_reap(struct svcxprt_rdma *xprt)
294 {
295 	int ret;
296 	struct ib_wc wc;
297 	struct svc_rdma_op_ctxt *ctxt = NULL;
298 
299 	if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
300 		return;
301 
302 	ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
303 	atomic_inc(&rdma_stat_rq_poll);
304 
305 	while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
306 		ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
307 		ctxt->wc_status = wc.status;
308 		ctxt->byte_len = wc.byte_len;
309 		svc_rdma_unmap_dma(ctxt);
310 		if (wc.status != IB_WC_SUCCESS) {
311 			/* Close the transport */
312 			dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
313 			set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
314 			svc_rdma_put_context(ctxt, 1);
315 			svc_xprt_put(&xprt->sc_xprt);
316 			continue;
317 		}
318 		spin_lock_bh(&xprt->sc_rq_dto_lock);
319 		list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
320 		spin_unlock_bh(&xprt->sc_rq_dto_lock);
321 		svc_xprt_put(&xprt->sc_xprt);
322 	}
323 
324 	if (ctxt)
325 		atomic_inc(&rdma_stat_rq_prod);
326 
327 	set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
328 	/*
329 	 * If data arrived before established event,
330 	 * don't enqueue. This defers RPC I/O until the
331 	 * RDMA connection is complete.
332 	 */
333 	if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
334 		svc_xprt_enqueue(&xprt->sc_xprt);
335 }
336 
337 /*
338  * Process a completion context
339  */
340 static void process_context(struct svcxprt_rdma *xprt,
341 			    struct svc_rdma_op_ctxt *ctxt)
342 {
343 	svc_rdma_unmap_dma(ctxt);
344 
345 	switch (ctxt->wr_op) {
346 	case IB_WR_SEND:
347 		if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
348 			svc_rdma_put_frmr(xprt, ctxt->frmr);
349 		svc_rdma_put_context(ctxt, 1);
350 		break;
351 
352 	case IB_WR_RDMA_WRITE:
353 		svc_rdma_put_context(ctxt, 0);
354 		break;
355 
356 	case IB_WR_RDMA_READ:
357 	case IB_WR_RDMA_READ_WITH_INV:
358 		if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
359 			struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
360 			BUG_ON(!read_hdr);
361 			if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
362 				svc_rdma_put_frmr(xprt, ctxt->frmr);
363 			spin_lock_bh(&xprt->sc_rq_dto_lock);
364 			set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
365 			list_add_tail(&read_hdr->dto_q,
366 				      &xprt->sc_read_complete_q);
367 			spin_unlock_bh(&xprt->sc_rq_dto_lock);
368 			svc_xprt_enqueue(&xprt->sc_xprt);
369 		}
370 		svc_rdma_put_context(ctxt, 0);
371 		break;
372 
373 	default:
374 		printk(KERN_ERR "svcrdma: unexpected completion type, "
375 		       "opcode=%d\n",
376 		       ctxt->wr_op);
377 		break;
378 	}
379 }
380 
381 /*
382  * Send Queue Completion Handler - potentially called on interrupt context.
383  *
384  * Note that caller must hold a transport reference.
385  */
386 static void sq_cq_reap(struct svcxprt_rdma *xprt)
387 {
388 	struct svc_rdma_op_ctxt *ctxt = NULL;
389 	struct ib_wc wc;
390 	struct ib_cq *cq = xprt->sc_sq_cq;
391 	int ret;
392 
393 	if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
394 		return;
395 
396 	ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
397 	atomic_inc(&rdma_stat_sq_poll);
398 	while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
399 		if (wc.status != IB_WC_SUCCESS)
400 			/* Close the transport */
401 			set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
402 
403 		/* Decrement used SQ WR count */
404 		atomic_dec(&xprt->sc_sq_count);
405 		wake_up(&xprt->sc_send_wait);
406 
407 		ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
408 		if (ctxt)
409 			process_context(xprt, ctxt);
410 
411 		svc_xprt_put(&xprt->sc_xprt);
412 	}
413 
414 	if (ctxt)
415 		atomic_inc(&rdma_stat_sq_prod);
416 }
417 
418 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
419 {
420 	struct svcxprt_rdma *xprt = cq_context;
421 	unsigned long flags;
422 
423 	/* Guard against unconditional flush call for destroyed QP */
424 	if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
425 		return;
426 
427 	/*
428 	 * Set the bit regardless of whether or not it's on the list
429 	 * because it may be on the list already due to an RQ
430 	 * completion.
431 	 */
432 	set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
433 
434 	/*
435 	 * If this transport is not already on the DTO transport queue,
436 	 * add it
437 	 */
438 	spin_lock_irqsave(&dto_lock, flags);
439 	if (list_empty(&xprt->sc_dto_q)) {
440 		svc_xprt_get(&xprt->sc_xprt);
441 		list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
442 	}
443 	spin_unlock_irqrestore(&dto_lock, flags);
444 
445 	/* Tasklet does all the work to avoid irqsave locks. */
446 	tasklet_schedule(&dto_tasklet);
447 }
448 
449 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
450 					     int listener)
451 {
452 	struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
453 
454 	if (!cma_xprt)
455 		return NULL;
456 	svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
457 	INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
458 	INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
459 	INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
460 	INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
461 	INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
462 	init_waitqueue_head(&cma_xprt->sc_send_wait);
463 
464 	spin_lock_init(&cma_xprt->sc_lock);
465 	spin_lock_init(&cma_xprt->sc_rq_dto_lock);
466 	spin_lock_init(&cma_xprt->sc_frmr_q_lock);
467 
468 	cma_xprt->sc_ord = svcrdma_ord;
469 
470 	cma_xprt->sc_max_req_size = svcrdma_max_req_size;
471 	cma_xprt->sc_max_requests = svcrdma_max_requests;
472 	cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
473 	atomic_set(&cma_xprt->sc_sq_count, 0);
474 	atomic_set(&cma_xprt->sc_ctxt_used, 0);
475 
476 	if (listener)
477 		set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
478 
479 	return cma_xprt;
480 }
481 
482 struct page *svc_rdma_get_page(void)
483 {
484 	struct page *page;
485 
486 	while ((page = alloc_page(GFP_KERNEL)) == NULL) {
487 		/* If we can't get memory, wait a bit and try again */
488 		printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
489 		       "jiffies.\n");
490 		schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
491 	}
492 	return page;
493 }
494 
495 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
496 {
497 	struct ib_recv_wr recv_wr, *bad_recv_wr;
498 	struct svc_rdma_op_ctxt *ctxt;
499 	struct page *page;
500 	dma_addr_t pa;
501 	int sge_no;
502 	int buflen;
503 	int ret;
504 
505 	ctxt = svc_rdma_get_context(xprt);
506 	buflen = 0;
507 	ctxt->direction = DMA_FROM_DEVICE;
508 	for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
509 		BUG_ON(sge_no >= xprt->sc_max_sge);
510 		page = svc_rdma_get_page();
511 		ctxt->pages[sge_no] = page;
512 		pa = ib_dma_map_page(xprt->sc_cm_id->device,
513 				     page, 0, PAGE_SIZE,
514 				     DMA_FROM_DEVICE);
515 		if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
516 			goto err_put_ctxt;
517 		atomic_inc(&xprt->sc_dma_used);
518 		ctxt->sge[sge_no].addr = pa;
519 		ctxt->sge[sge_no].length = PAGE_SIZE;
520 		ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
521 		ctxt->count = sge_no + 1;
522 		buflen += PAGE_SIZE;
523 	}
524 	recv_wr.next = NULL;
525 	recv_wr.sg_list = &ctxt->sge[0];
526 	recv_wr.num_sge = ctxt->count;
527 	recv_wr.wr_id = (u64)(unsigned long)ctxt;
528 
529 	svc_xprt_get(&xprt->sc_xprt);
530 	ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
531 	if (ret) {
532 		svc_rdma_unmap_dma(ctxt);
533 		svc_rdma_put_context(ctxt, 1);
534 		svc_xprt_put(&xprt->sc_xprt);
535 	}
536 	return ret;
537 
538  err_put_ctxt:
539 	svc_rdma_unmap_dma(ctxt);
540 	svc_rdma_put_context(ctxt, 1);
541 	return -ENOMEM;
542 }
543 
544 /*
545  * This function handles the CONNECT_REQUEST event on a listening
546  * endpoint. It is passed the cma_id for the _new_ connection. The context in
547  * this cma_id is inherited from the listening cma_id and is the svc_xprt
548  * structure for the listening endpoint.
549  *
550  * This function creates a new xprt for the new connection and enqueues it on
551  * the accept queue for the listent xprt. When the listen thread is kicked, it
552  * will call the recvfrom method on the listen xprt which will accept the new
553  * connection.
554  */
555 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
556 {
557 	struct svcxprt_rdma *listen_xprt = new_cma_id->context;
558 	struct svcxprt_rdma *newxprt;
559 	struct sockaddr *sa;
560 
561 	/* Create a new transport */
562 	newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
563 	if (!newxprt) {
564 		dprintk("svcrdma: failed to create new transport\n");
565 		return;
566 	}
567 	newxprt->sc_cm_id = new_cma_id;
568 	new_cma_id->context = newxprt;
569 	dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
570 		newxprt, newxprt->sc_cm_id, listen_xprt);
571 
572 	/* Save client advertised inbound read limit for use later in accept. */
573 	newxprt->sc_ord = client_ird;
574 
575 	/* Set the local and remote addresses in the transport */
576 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
577 	svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
578 	sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
579 	svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
580 
581 	/*
582 	 * Enqueue the new transport on the accept queue of the listening
583 	 * transport
584 	 */
585 	spin_lock_bh(&listen_xprt->sc_lock);
586 	list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
587 	spin_unlock_bh(&listen_xprt->sc_lock);
588 
589 	/*
590 	 * Can't use svc_xprt_received here because we are not on a
591 	 * rqstp thread
592 	*/
593 	set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
594 	svc_xprt_enqueue(&listen_xprt->sc_xprt);
595 }
596 
597 /*
598  * Handles events generated on the listening endpoint. These events will be
599  * either be incoming connect requests or adapter removal  events.
600  */
601 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
602 			       struct rdma_cm_event *event)
603 {
604 	struct svcxprt_rdma *xprt = cma_id->context;
605 	int ret = 0;
606 
607 	switch (event->event) {
608 	case RDMA_CM_EVENT_CONNECT_REQUEST:
609 		dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
610 			"event=%d\n", cma_id, cma_id->context, event->event);
611 		handle_connect_req(cma_id,
612 				   event->param.conn.initiator_depth);
613 		break;
614 
615 	case RDMA_CM_EVENT_ESTABLISHED:
616 		/* Accept complete */
617 		dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
618 			"cm_id=%p\n", xprt, cma_id);
619 		break;
620 
621 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
622 		dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
623 			xprt, cma_id);
624 		if (xprt)
625 			set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
626 		break;
627 
628 	default:
629 		dprintk("svcrdma: Unexpected event on listening endpoint %p, "
630 			"event=%d\n", cma_id, event->event);
631 		break;
632 	}
633 
634 	return ret;
635 }
636 
637 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
638 			    struct rdma_cm_event *event)
639 {
640 	struct svc_xprt *xprt = cma_id->context;
641 	struct svcxprt_rdma *rdma =
642 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
643 	switch (event->event) {
644 	case RDMA_CM_EVENT_ESTABLISHED:
645 		/* Accept complete */
646 		svc_xprt_get(xprt);
647 		dprintk("svcrdma: Connection completed on DTO xprt=%p, "
648 			"cm_id=%p\n", xprt, cma_id);
649 		clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
650 		svc_xprt_enqueue(xprt);
651 		break;
652 	case RDMA_CM_EVENT_DISCONNECTED:
653 		dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
654 			xprt, cma_id);
655 		if (xprt) {
656 			set_bit(XPT_CLOSE, &xprt->xpt_flags);
657 			svc_xprt_enqueue(xprt);
658 			svc_xprt_put(xprt);
659 		}
660 		break;
661 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
662 		dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
663 			"event=%d\n", cma_id, xprt, event->event);
664 		if (xprt) {
665 			set_bit(XPT_CLOSE, &xprt->xpt_flags);
666 			svc_xprt_enqueue(xprt);
667 		}
668 		break;
669 	default:
670 		dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
671 			"event=%d\n", cma_id, event->event);
672 		break;
673 	}
674 	return 0;
675 }
676 
677 /*
678  * Create a listening RDMA service endpoint.
679  */
680 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
681 					struct net *net,
682 					struct sockaddr *sa, int salen,
683 					int flags)
684 {
685 	struct rdma_cm_id *listen_id;
686 	struct svcxprt_rdma *cma_xprt;
687 	struct svc_xprt *xprt;
688 	int ret;
689 
690 	dprintk("svcrdma: Creating RDMA socket\n");
691 	if (sa->sa_family != AF_INET) {
692 		dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
693 		return ERR_PTR(-EAFNOSUPPORT);
694 	}
695 	cma_xprt = rdma_create_xprt(serv, 1);
696 	if (!cma_xprt)
697 		return ERR_PTR(-ENOMEM);
698 	xprt = &cma_xprt->sc_xprt;
699 
700 	listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
701 				   IB_QPT_RC);
702 	if (IS_ERR(listen_id)) {
703 		ret = PTR_ERR(listen_id);
704 		dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
705 		goto err0;
706 	}
707 
708 	ret = rdma_bind_addr(listen_id, sa);
709 	if (ret) {
710 		dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
711 		goto err1;
712 	}
713 	cma_xprt->sc_cm_id = listen_id;
714 
715 	ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
716 	if (ret) {
717 		dprintk("svcrdma: rdma_listen failed = %d\n", ret);
718 		goto err1;
719 	}
720 
721 	/*
722 	 * We need to use the address from the cm_id in case the
723 	 * caller specified 0 for the port number.
724 	 */
725 	sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
726 	svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
727 
728 	return &cma_xprt->sc_xprt;
729 
730  err1:
731 	rdma_destroy_id(listen_id);
732  err0:
733 	kfree(cma_xprt);
734 	return ERR_PTR(ret);
735 }
736 
737 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
738 {
739 	struct ib_mr *mr;
740 	struct ib_fast_reg_page_list *pl;
741 	struct svc_rdma_fastreg_mr *frmr;
742 
743 	frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
744 	if (!frmr)
745 		goto err;
746 
747 	mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
748 	if (IS_ERR(mr))
749 		goto err_free_frmr;
750 
751 	pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
752 					 RPCSVC_MAXPAGES);
753 	if (IS_ERR(pl))
754 		goto err_free_mr;
755 
756 	frmr->mr = mr;
757 	frmr->page_list = pl;
758 	INIT_LIST_HEAD(&frmr->frmr_list);
759 	return frmr;
760 
761  err_free_mr:
762 	ib_dereg_mr(mr);
763  err_free_frmr:
764 	kfree(frmr);
765  err:
766 	return ERR_PTR(-ENOMEM);
767 }
768 
769 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
770 {
771 	struct svc_rdma_fastreg_mr *frmr;
772 
773 	while (!list_empty(&xprt->sc_frmr_q)) {
774 		frmr = list_entry(xprt->sc_frmr_q.next,
775 				  struct svc_rdma_fastreg_mr, frmr_list);
776 		list_del_init(&frmr->frmr_list);
777 		ib_dereg_mr(frmr->mr);
778 		ib_free_fast_reg_page_list(frmr->page_list);
779 		kfree(frmr);
780 	}
781 }
782 
783 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
784 {
785 	struct svc_rdma_fastreg_mr *frmr = NULL;
786 
787 	spin_lock_bh(&rdma->sc_frmr_q_lock);
788 	if (!list_empty(&rdma->sc_frmr_q)) {
789 		frmr = list_entry(rdma->sc_frmr_q.next,
790 				  struct svc_rdma_fastreg_mr, frmr_list);
791 		list_del_init(&frmr->frmr_list);
792 		frmr->map_len = 0;
793 		frmr->page_list_len = 0;
794 	}
795 	spin_unlock_bh(&rdma->sc_frmr_q_lock);
796 	if (frmr)
797 		return frmr;
798 
799 	return rdma_alloc_frmr(rdma);
800 }
801 
802 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
803 			   struct svc_rdma_fastreg_mr *frmr)
804 {
805 	int page_no;
806 	for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
807 		dma_addr_t addr = frmr->page_list->page_list[page_no];
808 		if (ib_dma_mapping_error(frmr->mr->device, addr))
809 			continue;
810 		atomic_dec(&xprt->sc_dma_used);
811 		ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
812 				  frmr->direction);
813 	}
814 }
815 
816 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
817 		       struct svc_rdma_fastreg_mr *frmr)
818 {
819 	if (frmr) {
820 		frmr_unmap_dma(rdma, frmr);
821 		spin_lock_bh(&rdma->sc_frmr_q_lock);
822 		BUG_ON(!list_empty(&frmr->frmr_list));
823 		list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
824 		spin_unlock_bh(&rdma->sc_frmr_q_lock);
825 	}
826 }
827 
828 /*
829  * This is the xpo_recvfrom function for listening endpoints. Its
830  * purpose is to accept incoming connections. The CMA callback handler
831  * has already created a new transport and attached it to the new CMA
832  * ID.
833  *
834  * There is a queue of pending connections hung on the listening
835  * transport. This queue contains the new svc_xprt structure. This
836  * function takes svc_xprt structures off the accept_q and completes
837  * the connection.
838  */
839 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
840 {
841 	struct svcxprt_rdma *listen_rdma;
842 	struct svcxprt_rdma *newxprt = NULL;
843 	struct rdma_conn_param conn_param;
844 	struct ib_qp_init_attr qp_attr;
845 	struct ib_device_attr devattr;
846 	int uninitialized_var(dma_mr_acc);
847 	int need_dma_mr;
848 	int ret;
849 	int i;
850 
851 	listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
852 	clear_bit(XPT_CONN, &xprt->xpt_flags);
853 	/* Get the next entry off the accept list */
854 	spin_lock_bh(&listen_rdma->sc_lock);
855 	if (!list_empty(&listen_rdma->sc_accept_q)) {
856 		newxprt = list_entry(listen_rdma->sc_accept_q.next,
857 				     struct svcxprt_rdma, sc_accept_q);
858 		list_del_init(&newxprt->sc_accept_q);
859 	}
860 	if (!list_empty(&listen_rdma->sc_accept_q))
861 		set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
862 	spin_unlock_bh(&listen_rdma->sc_lock);
863 	if (!newxprt)
864 		return NULL;
865 
866 	dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
867 		newxprt, newxprt->sc_cm_id);
868 
869 	ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
870 	if (ret) {
871 		dprintk("svcrdma: could not query device attributes on "
872 			"device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
873 		goto errout;
874 	}
875 
876 	/* Qualify the transport resource defaults with the
877 	 * capabilities of this particular device */
878 	newxprt->sc_max_sge = min((size_t)devattr.max_sge,
879 				  (size_t)RPCSVC_MAXPAGES);
880 	newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
881 				   (size_t)svcrdma_max_requests);
882 	newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
883 
884 	/*
885 	 * Limit ORD based on client limit, local device limit, and
886 	 * configured svcrdma limit.
887 	 */
888 	newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
889 	newxprt->sc_ord = min_t(size_t,	svcrdma_ord, newxprt->sc_ord);
890 
891 	newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
892 	if (IS_ERR(newxprt->sc_pd)) {
893 		dprintk("svcrdma: error creating PD for connect request\n");
894 		goto errout;
895 	}
896 	newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
897 					 sq_comp_handler,
898 					 cq_event_handler,
899 					 newxprt,
900 					 newxprt->sc_sq_depth,
901 					 0);
902 	if (IS_ERR(newxprt->sc_sq_cq)) {
903 		dprintk("svcrdma: error creating SQ CQ for connect request\n");
904 		goto errout;
905 	}
906 	newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
907 					 rq_comp_handler,
908 					 cq_event_handler,
909 					 newxprt,
910 					 newxprt->sc_max_requests,
911 					 0);
912 	if (IS_ERR(newxprt->sc_rq_cq)) {
913 		dprintk("svcrdma: error creating RQ CQ for connect request\n");
914 		goto errout;
915 	}
916 
917 	memset(&qp_attr, 0, sizeof qp_attr);
918 	qp_attr.event_handler = qp_event_handler;
919 	qp_attr.qp_context = &newxprt->sc_xprt;
920 	qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
921 	qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
922 	qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
923 	qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
924 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
925 	qp_attr.qp_type = IB_QPT_RC;
926 	qp_attr.send_cq = newxprt->sc_sq_cq;
927 	qp_attr.recv_cq = newxprt->sc_rq_cq;
928 	dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
929 		"    cm_id->device=%p, sc_pd->device=%p\n"
930 		"    cap.max_send_wr = %d\n"
931 		"    cap.max_recv_wr = %d\n"
932 		"    cap.max_send_sge = %d\n"
933 		"    cap.max_recv_sge = %d\n",
934 		newxprt->sc_cm_id, newxprt->sc_pd,
935 		newxprt->sc_cm_id->device, newxprt->sc_pd->device,
936 		qp_attr.cap.max_send_wr,
937 		qp_attr.cap.max_recv_wr,
938 		qp_attr.cap.max_send_sge,
939 		qp_attr.cap.max_recv_sge);
940 
941 	ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
942 	if (ret) {
943 		/*
944 		 * XXX: This is a hack. We need a xx_request_qp interface
945 		 * that will adjust the qp_attr's with a best-effort
946 		 * number
947 		 */
948 		qp_attr.cap.max_send_sge -= 2;
949 		qp_attr.cap.max_recv_sge -= 2;
950 		ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
951 				     &qp_attr);
952 		if (ret) {
953 			dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
954 			goto errout;
955 		}
956 		newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
957 		newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
958 		newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
959 		newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
960 	}
961 	newxprt->sc_qp = newxprt->sc_cm_id->qp;
962 
963 	/*
964 	 * Use the most secure set of MR resources based on the
965 	 * transport type and available memory management features in
966 	 * the device. Here's the table implemented below:
967 	 *
968 	 *		Fast	Global	DMA	Remote WR
969 	 *		Reg	LKEY	MR	Access
970 	 *		Sup'd	Sup'd	Needed	Needed
971 	 *
972 	 * IWARP	N	N	Y	Y
973 	 *		N	Y	Y	Y
974 	 *		Y	N	Y	N
975 	 *		Y	Y	N	-
976 	 *
977 	 * IB		N	N	Y	N
978 	 *		N	Y	N	-
979 	 *		Y	N	Y	N
980 	 *		Y	Y	N	-
981 	 *
982 	 * NB:	iWARP requires remote write access for the data sink
983 	 *	of an RDMA_READ. IB does not.
984 	 */
985 	if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
986 		newxprt->sc_frmr_pg_list_len =
987 			devattr.max_fast_reg_page_list_len;
988 		newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
989 	}
990 
991 	/*
992 	 * Determine if a DMA MR is required and if so, what privs are required
993 	 */
994 	switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
995 	case RDMA_TRANSPORT_IWARP:
996 		newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
997 		if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
998 			need_dma_mr = 1;
999 			dma_mr_acc =
1000 				(IB_ACCESS_LOCAL_WRITE |
1001 				 IB_ACCESS_REMOTE_WRITE);
1002 		} else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1003 			need_dma_mr = 1;
1004 			dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1005 		} else
1006 			need_dma_mr = 0;
1007 		break;
1008 	case RDMA_TRANSPORT_IB:
1009 		if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1010 			need_dma_mr = 1;
1011 			dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1012 		} else
1013 			need_dma_mr = 0;
1014 		break;
1015 	default:
1016 		goto errout;
1017 	}
1018 
1019 	/* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1020 	if (need_dma_mr) {
1021 		/* Register all of physical memory */
1022 		newxprt->sc_phys_mr =
1023 			ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1024 		if (IS_ERR(newxprt->sc_phys_mr)) {
1025 			dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1026 				ret);
1027 			goto errout;
1028 		}
1029 		newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1030 	} else
1031 		newxprt->sc_dma_lkey =
1032 			newxprt->sc_cm_id->device->local_dma_lkey;
1033 
1034 	/* Post receive buffers */
1035 	for (i = 0; i < newxprt->sc_max_requests; i++) {
1036 		ret = svc_rdma_post_recv(newxprt);
1037 		if (ret) {
1038 			dprintk("svcrdma: failure posting receive buffers\n");
1039 			goto errout;
1040 		}
1041 	}
1042 
1043 	/* Swap out the handler */
1044 	newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1045 
1046 	/*
1047 	 * Arm the CQs for the SQ and RQ before accepting so we can't
1048 	 * miss the first message
1049 	 */
1050 	ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1051 	ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1052 
1053 	/* Accept Connection */
1054 	set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1055 	memset(&conn_param, 0, sizeof conn_param);
1056 	conn_param.responder_resources = 0;
1057 	conn_param.initiator_depth = newxprt->sc_ord;
1058 	ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1059 	if (ret) {
1060 		dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1061 		       ret);
1062 		goto errout;
1063 	}
1064 
1065 	dprintk("svcrdma: new connection %p accepted with the following "
1066 		"attributes:\n"
1067 		"    local_ip        : %pI4\n"
1068 		"    local_port	     : %d\n"
1069 		"    remote_ip       : %pI4\n"
1070 		"    remote_port     : %d\n"
1071 		"    max_sge         : %d\n"
1072 		"    sq_depth        : %d\n"
1073 		"    max_requests    : %d\n"
1074 		"    ord             : %d\n",
1075 		newxprt,
1076 		&((struct sockaddr_in *)&newxprt->sc_cm_id->
1077 			 route.addr.src_addr)->sin_addr.s_addr,
1078 		ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1079 		       route.addr.src_addr)->sin_port),
1080 		&((struct sockaddr_in *)&newxprt->sc_cm_id->
1081 			 route.addr.dst_addr)->sin_addr.s_addr,
1082 		ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1083 		       route.addr.dst_addr)->sin_port),
1084 		newxprt->sc_max_sge,
1085 		newxprt->sc_sq_depth,
1086 		newxprt->sc_max_requests,
1087 		newxprt->sc_ord);
1088 
1089 	return &newxprt->sc_xprt;
1090 
1091  errout:
1092 	dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1093 	/* Take a reference in case the DTO handler runs */
1094 	svc_xprt_get(&newxprt->sc_xprt);
1095 	if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1096 		ib_destroy_qp(newxprt->sc_qp);
1097 	rdma_destroy_id(newxprt->sc_cm_id);
1098 	/* This call to put will destroy the transport */
1099 	svc_xprt_put(&newxprt->sc_xprt);
1100 	return NULL;
1101 }
1102 
1103 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1104 {
1105 }
1106 
1107 /*
1108  * When connected, an svc_xprt has at least two references:
1109  *
1110  * - A reference held by the cm_id between the ESTABLISHED and
1111  *   DISCONNECTED events. If the remote peer disconnected first, this
1112  *   reference could be gone.
1113  *
1114  * - A reference held by the svc_recv code that called this function
1115  *   as part of close processing.
1116  *
1117  * At a minimum one references should still be held.
1118  */
1119 static void svc_rdma_detach(struct svc_xprt *xprt)
1120 {
1121 	struct svcxprt_rdma *rdma =
1122 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
1123 	dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1124 
1125 	/* Disconnect and flush posted WQE */
1126 	rdma_disconnect(rdma->sc_cm_id);
1127 }
1128 
1129 static void __svc_rdma_free(struct work_struct *work)
1130 {
1131 	struct svcxprt_rdma *rdma =
1132 		container_of(work, struct svcxprt_rdma, sc_work);
1133 	dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1134 
1135 	/* We should only be called from kref_put */
1136 	BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1137 
1138 	/*
1139 	 * Destroy queued, but not processed read completions. Note
1140 	 * that this cleanup has to be done before destroying the
1141 	 * cm_id because the device ptr is needed to unmap the dma in
1142 	 * svc_rdma_put_context.
1143 	 */
1144 	while (!list_empty(&rdma->sc_read_complete_q)) {
1145 		struct svc_rdma_op_ctxt *ctxt;
1146 		ctxt = list_entry(rdma->sc_read_complete_q.next,
1147 				  struct svc_rdma_op_ctxt,
1148 				  dto_q);
1149 		list_del_init(&ctxt->dto_q);
1150 		svc_rdma_put_context(ctxt, 1);
1151 	}
1152 
1153 	/* Destroy queued, but not processed recv completions */
1154 	while (!list_empty(&rdma->sc_rq_dto_q)) {
1155 		struct svc_rdma_op_ctxt *ctxt;
1156 		ctxt = list_entry(rdma->sc_rq_dto_q.next,
1157 				  struct svc_rdma_op_ctxt,
1158 				  dto_q);
1159 		list_del_init(&ctxt->dto_q);
1160 		svc_rdma_put_context(ctxt, 1);
1161 	}
1162 
1163 	/* Warn if we leaked a resource or under-referenced */
1164 	WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1165 	WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1166 
1167 	/* De-allocate fastreg mr */
1168 	rdma_dealloc_frmr_q(rdma);
1169 
1170 	/* Destroy the QP if present (not a listener) */
1171 	if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1172 		ib_destroy_qp(rdma->sc_qp);
1173 
1174 	if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1175 		ib_destroy_cq(rdma->sc_sq_cq);
1176 
1177 	if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1178 		ib_destroy_cq(rdma->sc_rq_cq);
1179 
1180 	if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1181 		ib_dereg_mr(rdma->sc_phys_mr);
1182 
1183 	if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1184 		ib_dealloc_pd(rdma->sc_pd);
1185 
1186 	/* Destroy the CM ID */
1187 	rdma_destroy_id(rdma->sc_cm_id);
1188 
1189 	kfree(rdma);
1190 }
1191 
1192 static void svc_rdma_free(struct svc_xprt *xprt)
1193 {
1194 	struct svcxprt_rdma *rdma =
1195 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
1196 	INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1197 	queue_work(svc_rdma_wq, &rdma->sc_work);
1198 }
1199 
1200 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1201 {
1202 	struct svcxprt_rdma *rdma =
1203 		container_of(xprt, struct svcxprt_rdma, sc_xprt);
1204 
1205 	/*
1206 	 * If there are fewer SQ WR available than required to send a
1207 	 * simple response, return false.
1208 	 */
1209 	if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1210 		return 0;
1211 
1212 	/*
1213 	 * ...or there are already waiters on the SQ,
1214 	 * return false.
1215 	 */
1216 	if (waitqueue_active(&rdma->sc_send_wait))
1217 		return 0;
1218 
1219 	/* Otherwise return true. */
1220 	return 1;
1221 }
1222 
1223 /*
1224  * Attempt to register the kvec representing the RPC memory with the
1225  * device.
1226  *
1227  * Returns:
1228  *  NULL : The device does not support fastreg or there were no more
1229  *         fastreg mr.
1230  *  frmr : The kvec register request was successfully posted.
1231  *    <0 : An error was encountered attempting to register the kvec.
1232  */
1233 int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1234 		     struct svc_rdma_fastreg_mr *frmr)
1235 {
1236 	struct ib_send_wr fastreg_wr;
1237 	u8 key;
1238 
1239 	/* Bump the key */
1240 	key = (u8)(frmr->mr->lkey & 0x000000FF);
1241 	ib_update_fast_reg_key(frmr->mr, ++key);
1242 
1243 	/* Prepare FASTREG WR */
1244 	memset(&fastreg_wr, 0, sizeof fastreg_wr);
1245 	fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1246 	fastreg_wr.send_flags = IB_SEND_SIGNALED;
1247 	fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1248 	fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1249 	fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1250 	fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1251 	fastreg_wr.wr.fast_reg.length = frmr->map_len;
1252 	fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1253 	fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1254 	return svc_rdma_send(xprt, &fastreg_wr);
1255 }
1256 
1257 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1258 {
1259 	struct ib_send_wr *bad_wr, *n_wr;
1260 	int wr_count;
1261 	int i;
1262 	int ret;
1263 
1264 	if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1265 		return -ENOTCONN;
1266 
1267 	BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1268 	wr_count = 1;
1269 	for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1270 		wr_count++;
1271 
1272 	/* If the SQ is full, wait until an SQ entry is available */
1273 	while (1) {
1274 		spin_lock_bh(&xprt->sc_lock);
1275 		if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
1276 			spin_unlock_bh(&xprt->sc_lock);
1277 			atomic_inc(&rdma_stat_sq_starve);
1278 
1279 			/* See if we can opportunistically reap SQ WR to make room */
1280 			sq_cq_reap(xprt);
1281 
1282 			/* Wait until SQ WR available if SQ still full */
1283 			wait_event(xprt->sc_send_wait,
1284 				   atomic_read(&xprt->sc_sq_count) <
1285 				   xprt->sc_sq_depth);
1286 			if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1287 				return -ENOTCONN;
1288 			continue;
1289 		}
1290 		/* Take a transport ref for each WR posted */
1291 		for (i = 0; i < wr_count; i++)
1292 			svc_xprt_get(&xprt->sc_xprt);
1293 
1294 		/* Bump used SQ WR count and post */
1295 		atomic_add(wr_count, &xprt->sc_sq_count);
1296 		ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1297 		if (ret) {
1298 			set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1299 			atomic_sub(wr_count, &xprt->sc_sq_count);
1300 			for (i = 0; i < wr_count; i ++)
1301 				svc_xprt_put(&xprt->sc_xprt);
1302 			dprintk("svcrdma: failed to post SQ WR rc=%d, "
1303 			       "sc_sq_count=%d, sc_sq_depth=%d\n",
1304 			       ret, atomic_read(&xprt->sc_sq_count),
1305 			       xprt->sc_sq_depth);
1306 		}
1307 		spin_unlock_bh(&xprt->sc_lock);
1308 		if (ret)
1309 			wake_up(&xprt->sc_send_wait);
1310 		break;
1311 	}
1312 	return ret;
1313 }
1314 
1315 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1316 			 enum rpcrdma_errcode err)
1317 {
1318 	struct ib_send_wr err_wr;
1319 	struct page *p;
1320 	struct svc_rdma_op_ctxt *ctxt;
1321 	u32 *va;
1322 	int length;
1323 	int ret;
1324 
1325 	p = svc_rdma_get_page();
1326 	va = page_address(p);
1327 
1328 	/* XDR encode error */
1329 	length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1330 
1331 	ctxt = svc_rdma_get_context(xprt);
1332 	ctxt->direction = DMA_FROM_DEVICE;
1333 	ctxt->count = 1;
1334 	ctxt->pages[0] = p;
1335 
1336 	/* Prepare SGE for local address */
1337 	ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
1338 					    p, 0, length, DMA_FROM_DEVICE);
1339 	if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
1340 		put_page(p);
1341 		svc_rdma_put_context(ctxt, 1);
1342 		return;
1343 	}
1344 	atomic_inc(&xprt->sc_dma_used);
1345 	ctxt->sge[0].lkey = xprt->sc_dma_lkey;
1346 	ctxt->sge[0].length = length;
1347 
1348 	/* Prepare SEND WR */
1349 	memset(&err_wr, 0, sizeof err_wr);
1350 	ctxt->wr_op = IB_WR_SEND;
1351 	err_wr.wr_id = (unsigned long)ctxt;
1352 	err_wr.sg_list = ctxt->sge;
1353 	err_wr.num_sge = 1;
1354 	err_wr.opcode = IB_WR_SEND;
1355 	err_wr.send_flags = IB_SEND_SIGNALED;
1356 
1357 	/* Post It */
1358 	ret = svc_rdma_send(xprt, &err_wr);
1359 	if (ret) {
1360 		dprintk("svcrdma: Error %d posting send for protocol error\n",
1361 			ret);
1362 		svc_rdma_unmap_dma(ctxt);
1363 		svc_rdma_put_context(ctxt, 1);
1364 	}
1365 }
1366