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