svcrdma: Remove unused READ_DONE context flags bit
[deliverable/linux.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
CommitLineData
377f9b2f
TT
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/spinlock.h>
46#include <rdma/ib_verbs.h>
47#include <rdma/rdma_cm.h>
48#include <linux/sunrpc/svc_rdma.h>
49
50#define RPCDBG_FACILITY RPCDBG_SVCXPRT
51
52static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
53 struct sockaddr *sa, int salen,
54 int flags);
55static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
56static void svc_rdma_release_rqst(struct svc_rqst *);
377f9b2f
TT
57static void dto_tasklet_func(unsigned long data);
58static void svc_rdma_detach(struct svc_xprt *xprt);
59static void svc_rdma_free(struct svc_xprt *xprt);
60static int svc_rdma_has_wspace(struct svc_xprt *xprt);
61static void rq_cq_reap(struct svcxprt_rdma *xprt);
62static void sq_cq_reap(struct svcxprt_rdma *xprt);
63
64DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
65static DEFINE_SPINLOCK(dto_lock);
66static LIST_HEAD(dto_xprt_q);
67
68static struct svc_xprt_ops svc_rdma_ops = {
69 .xpo_create = svc_rdma_create,
70 .xpo_recvfrom = svc_rdma_recvfrom,
71 .xpo_sendto = svc_rdma_sendto,
72 .xpo_release_rqst = svc_rdma_release_rqst,
73 .xpo_detach = svc_rdma_detach,
74 .xpo_free = svc_rdma_free,
75 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
76 .xpo_has_wspace = svc_rdma_has_wspace,
77 .xpo_accept = svc_rdma_accept,
78};
79
80struct svc_xprt_class svc_rdma_class = {
81 .xcl_name = "rdma",
82 .xcl_owner = THIS_MODULE,
83 .xcl_ops = &svc_rdma_ops,
84 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
85};
86
87static int rdma_bump_context_cache(struct svcxprt_rdma *xprt)
88{
89 int target;
90 int at_least_one = 0;
91 struct svc_rdma_op_ctxt *ctxt;
92
93 target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump,
94 xprt->sc_ctxt_max);
95
96 spin_lock_bh(&xprt->sc_ctxt_lock);
97 while (xprt->sc_ctxt_cnt < target) {
98 xprt->sc_ctxt_cnt++;
99 spin_unlock_bh(&xprt->sc_ctxt_lock);
100
101 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
102
103 spin_lock_bh(&xprt->sc_ctxt_lock);
104 if (ctxt) {
105 at_least_one = 1;
106 ctxt->next = xprt->sc_ctxt_head;
107 xprt->sc_ctxt_head = ctxt;
108 } else {
109 /* kmalloc failed...give up for now */
110 xprt->sc_ctxt_cnt--;
111 break;
112 }
113 }
114 spin_unlock_bh(&xprt->sc_ctxt_lock);
115 dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n",
116 xprt->sc_ctxt_max, xprt->sc_ctxt_cnt);
117 return at_least_one;
118}
119
120struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
121{
122 struct svc_rdma_op_ctxt *ctxt;
123
124 while (1) {
125 spin_lock_bh(&xprt->sc_ctxt_lock);
126 if (unlikely(xprt->sc_ctxt_head == NULL)) {
127 /* Try to bump my cache. */
128 spin_unlock_bh(&xprt->sc_ctxt_lock);
129
130 if (rdma_bump_context_cache(xprt))
131 continue;
132
133 printk(KERN_INFO "svcrdma: sleeping waiting for "
134 "context memory on xprt=%p\n",
135 xprt);
136 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
137 continue;
138 }
139 ctxt = xprt->sc_ctxt_head;
140 xprt->sc_ctxt_head = ctxt->next;
141 spin_unlock_bh(&xprt->sc_ctxt_lock);
142 ctxt->xprt = xprt;
143 INIT_LIST_HEAD(&ctxt->dto_q);
144 ctxt->count = 0;
145 break;
146 }
147 return ctxt;
148}
149
150void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
151{
152 struct svcxprt_rdma *xprt;
153 int i;
154
155 BUG_ON(!ctxt);
156 xprt = ctxt->xprt;
157 if (free_pages)
158 for (i = 0; i < ctxt->count; i++)
159 put_page(ctxt->pages[i]);
160
161 for (i = 0; i < ctxt->count; i++)
162 dma_unmap_single(xprt->sc_cm_id->device->dma_device,
163 ctxt->sge[i].addr,
164 ctxt->sge[i].length,
165 ctxt->direction);
166 spin_lock_bh(&xprt->sc_ctxt_lock);
167 ctxt->next = xprt->sc_ctxt_head;
168 xprt->sc_ctxt_head = ctxt;
169 spin_unlock_bh(&xprt->sc_ctxt_lock);
170}
171
172/* ib_cq event handler */
173static void cq_event_handler(struct ib_event *event, void *context)
174{
175 struct svc_xprt *xprt = context;
176 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
177 event->event, context);
178 set_bit(XPT_CLOSE, &xprt->xpt_flags);
179}
180
181/* QP event handler */
182static void qp_event_handler(struct ib_event *event, void *context)
183{
184 struct svc_xprt *xprt = context;
185
186 switch (event->event) {
187 /* These are considered benign events */
188 case IB_EVENT_PATH_MIG:
189 case IB_EVENT_COMM_EST:
190 case IB_EVENT_SQ_DRAINED:
191 case IB_EVENT_QP_LAST_WQE_REACHED:
192 dprintk("svcrdma: QP event %d received for QP=%p\n",
193 event->event, event->element.qp);
194 break;
195 /* These are considered fatal events */
196 case IB_EVENT_PATH_MIG_ERR:
197 case IB_EVENT_QP_FATAL:
198 case IB_EVENT_QP_REQ_ERR:
199 case IB_EVENT_QP_ACCESS_ERR:
200 case IB_EVENT_DEVICE_FATAL:
201 default:
202 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
203 "closing transport\n",
204 event->event, event->element.qp);
205 set_bit(XPT_CLOSE, &xprt->xpt_flags);
206 break;
207 }
208}
209
210/*
211 * Data Transfer Operation Tasklet
212 *
213 * Walks a list of transports with I/O pending, removing entries as
214 * they are added to the server's I/O pending list. Two bits indicate
215 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
216 * spinlock that serializes access to the transport list with the RQ
217 * and SQ interrupt handlers.
218 */
219static void dto_tasklet_func(unsigned long data)
220{
221 struct svcxprt_rdma *xprt;
222 unsigned long flags;
223
224 spin_lock_irqsave(&dto_lock, flags);
225 while (!list_empty(&dto_xprt_q)) {
226 xprt = list_entry(dto_xprt_q.next,
227 struct svcxprt_rdma, sc_dto_q);
228 list_del_init(&xprt->sc_dto_q);
229 spin_unlock_irqrestore(&dto_lock, flags);
230
dbcd00eb
TT
231 rq_cq_reap(xprt);
232 sq_cq_reap(xprt);
377f9b2f 233
c48cbb40 234 svc_xprt_put(&xprt->sc_xprt);
377f9b2f
TT
235 spin_lock_irqsave(&dto_lock, flags);
236 }
237 spin_unlock_irqrestore(&dto_lock, flags);
238}
239
240/*
241 * Receive Queue Completion Handler
242 *
243 * Since an RQ completion handler is called on interrupt context, we
244 * need to defer the handling of the I/O to a tasklet
245 */
246static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
247{
248 struct svcxprt_rdma *xprt = cq_context;
249 unsigned long flags;
250
251 /*
252 * Set the bit regardless of whether or not it's on the list
253 * because it may be on the list already due to an SQ
254 * completion.
255 */
256 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
257
258 /*
259 * If this transport is not already on the DTO transport queue,
260 * add it
261 */
262 spin_lock_irqsave(&dto_lock, flags);
c48cbb40
TT
263 if (list_empty(&xprt->sc_dto_q)) {
264 svc_xprt_get(&xprt->sc_xprt);
377f9b2f 265 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
c48cbb40 266 }
377f9b2f
TT
267 spin_unlock_irqrestore(&dto_lock, flags);
268
269 /* Tasklet does all the work to avoid irqsave locks. */
270 tasklet_schedule(&dto_tasklet);
271}
272
273/*
274 * rq_cq_reap - Process the RQ CQ.
275 *
276 * Take all completing WC off the CQE and enqueue the associated DTO
277 * context on the dto_q for the transport.
278 */
279static void rq_cq_reap(struct svcxprt_rdma *xprt)
280{
281 int ret;
282 struct ib_wc wc;
283 struct svc_rdma_op_ctxt *ctxt = NULL;
284
dbcd00eb
TT
285 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
286 return;
287
288 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
377f9b2f
TT
289 atomic_inc(&rdma_stat_rq_poll);
290
291 spin_lock_bh(&xprt->sc_rq_dto_lock);
292 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
293 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
294 ctxt->wc_status = wc.status;
295 ctxt->byte_len = wc.byte_len;
296 if (wc.status != IB_WC_SUCCESS) {
297 /* Close the transport */
298 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
299 svc_rdma_put_context(ctxt, 1);
300 continue;
301 }
302 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
303 }
304 spin_unlock_bh(&xprt->sc_rq_dto_lock);
305
306 if (ctxt)
307 atomic_inc(&rdma_stat_rq_prod);
dbcd00eb
TT
308
309 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
310 /*
311 * If data arrived before established event,
312 * don't enqueue. This defers RPC I/O until the
313 * RDMA connection is complete.
314 */
315 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
316 svc_xprt_enqueue(&xprt->sc_xprt);
377f9b2f
TT
317}
318
319/*
320 * Send Queue Completion Handler - potentially called on interrupt context.
321 */
322static void sq_cq_reap(struct svcxprt_rdma *xprt)
323{
324 struct svc_rdma_op_ctxt *ctxt = NULL;
325 struct ib_wc wc;
326 struct ib_cq *cq = xprt->sc_sq_cq;
327 int ret;
328
dbcd00eb
TT
329
330 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
331 return;
332
333 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
377f9b2f
TT
334 atomic_inc(&rdma_stat_sq_poll);
335 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
336 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
337 xprt = ctxt->xprt;
338
339 if (wc.status != IB_WC_SUCCESS)
340 /* Close the transport */
341 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
342
343 /* Decrement used SQ WR count */
344 atomic_dec(&xprt->sc_sq_count);
345 wake_up(&xprt->sc_send_wait);
346
347 switch (ctxt->wr_op) {
348 case IB_WR_SEND:
349 case IB_WR_RDMA_WRITE:
350 svc_rdma_put_context(ctxt, 1);
351 break;
352
353 case IB_WR_RDMA_READ:
354 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
355 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
377f9b2f
TT
356 spin_lock_bh(&xprt->sc_read_complete_lock);
357 list_add_tail(&ctxt->dto_q,
358 &xprt->sc_read_complete_q);
359 spin_unlock_bh(&xprt->sc_read_complete_lock);
360 svc_xprt_enqueue(&xprt->sc_xprt);
361 }
362 break;
363
364 default:
365 printk(KERN_ERR "svcrdma: unexpected completion type, "
366 "opcode=%d, status=%d\n",
367 wc.opcode, wc.status);
368 break;
369 }
370 }
371
372 if (ctxt)
373 atomic_inc(&rdma_stat_sq_prod);
374}
375
376static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
377{
378 struct svcxprt_rdma *xprt = cq_context;
379 unsigned long flags;
380
381 /*
382 * Set the bit regardless of whether or not it's on the list
383 * because it may be on the list already due to an RQ
384 * completion.
385 */
386 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
387
388 /*
389 * If this transport is not already on the DTO transport queue,
390 * add it
391 */
392 spin_lock_irqsave(&dto_lock, flags);
c48cbb40
TT
393 if (list_empty(&xprt->sc_dto_q)) {
394 svc_xprt_get(&xprt->sc_xprt);
377f9b2f 395 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
c48cbb40 396 }
377f9b2f
TT
397 spin_unlock_irqrestore(&dto_lock, flags);
398
399 /* Tasklet does all the work to avoid irqsave locks. */
400 tasklet_schedule(&dto_tasklet);
401}
402
403static void create_context_cache(struct svcxprt_rdma *xprt,
404 int ctxt_count, int ctxt_bump, int ctxt_max)
405{
406 struct svc_rdma_op_ctxt *ctxt;
407 int i;
408
409 xprt->sc_ctxt_max = ctxt_max;
410 xprt->sc_ctxt_bump = ctxt_bump;
411 xprt->sc_ctxt_cnt = 0;
412 xprt->sc_ctxt_head = NULL;
413 for (i = 0; i < ctxt_count; i++) {
414 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
415 if (ctxt) {
416 ctxt->next = xprt->sc_ctxt_head;
417 xprt->sc_ctxt_head = ctxt;
418 xprt->sc_ctxt_cnt++;
419 }
420 }
421}
422
423static void destroy_context_cache(struct svc_rdma_op_ctxt *ctxt)
424{
425 struct svc_rdma_op_ctxt *next;
426 if (!ctxt)
427 return;
428
429 do {
430 next = ctxt->next;
431 kfree(ctxt);
432 ctxt = next;
433 } while (next);
434}
435
436static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
437 int listener)
438{
439 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
440
441 if (!cma_xprt)
442 return NULL;
443 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
444 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
445 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
446 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
447 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
448 init_waitqueue_head(&cma_xprt->sc_send_wait);
449
450 spin_lock_init(&cma_xprt->sc_lock);
451 spin_lock_init(&cma_xprt->sc_read_complete_lock);
452 spin_lock_init(&cma_xprt->sc_ctxt_lock);
453 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
454
455 cma_xprt->sc_ord = svcrdma_ord;
456
457 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
458 cma_xprt->sc_max_requests = svcrdma_max_requests;
459 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
460 atomic_set(&cma_xprt->sc_sq_count, 0);
461
462 if (!listener) {
463 int reqs = cma_xprt->sc_max_requests;
464 create_context_cache(cma_xprt,
465 reqs << 1, /* starting size */
466 reqs, /* bump amount */
467 reqs +
468 cma_xprt->sc_sq_depth +
469 RPCRDMA_MAX_THREADS + 1); /* max */
470 if (!cma_xprt->sc_ctxt_head) {
471 kfree(cma_xprt);
472 return NULL;
473 }
474 clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
475 } else
476 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
477
478 return cma_xprt;
479}
480
481struct page *svc_rdma_get_page(void)
482{
483 struct page *page;
484
485 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
486 /* If we can't get memory, wait a bit and try again */
487 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
488 "jiffies.\n");
489 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
490 }
491 return page;
492}
493
494int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
495{
496 struct ib_recv_wr recv_wr, *bad_recv_wr;
497 struct svc_rdma_op_ctxt *ctxt;
498 struct page *page;
499 unsigned long pa;
500 int sge_no;
501 int buflen;
502 int ret;
503
504 ctxt = svc_rdma_get_context(xprt);
505 buflen = 0;
506 ctxt->direction = DMA_FROM_DEVICE;
507 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
508 BUG_ON(sge_no >= xprt->sc_max_sge);
509 page = svc_rdma_get_page();
510 ctxt->pages[sge_no] = page;
511 pa = ib_dma_map_page(xprt->sc_cm_id->device,
512 page, 0, PAGE_SIZE,
513 DMA_FROM_DEVICE);
514 ctxt->sge[sge_no].addr = pa;
515 ctxt->sge[sge_no].length = PAGE_SIZE;
516 ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
517 buflen += PAGE_SIZE;
518 }
519 ctxt->count = sge_no;
520 recv_wr.next = NULL;
521 recv_wr.sg_list = &ctxt->sge[0];
522 recv_wr.num_sge = ctxt->count;
523 recv_wr.wr_id = (u64)(unsigned long)ctxt;
524
525 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
05a0826a
TT
526 if (ret)
527 svc_rdma_put_context(ctxt, 1);
377f9b2f
TT
528 return ret;
529}
530
531/*
532 * This function handles the CONNECT_REQUEST event on a listening
533 * endpoint. It is passed the cma_id for the _new_ connection. The context in
534 * this cma_id is inherited from the listening cma_id and is the svc_xprt
535 * structure for the listening endpoint.
536 *
537 * This function creates a new xprt for the new connection and enqueues it on
538 * the accept queue for the listent xprt. When the listen thread is kicked, it
539 * will call the recvfrom method on the listen xprt which will accept the new
540 * connection.
541 */
542static void handle_connect_req(struct rdma_cm_id *new_cma_id)
543{
544 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
545 struct svcxprt_rdma *newxprt;
546
547 /* Create a new transport */
548 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
549 if (!newxprt) {
550 dprintk("svcrdma: failed to create new transport\n");
551 return;
552 }
553 newxprt->sc_cm_id = new_cma_id;
554 new_cma_id->context = newxprt;
555 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
556 newxprt, newxprt->sc_cm_id, listen_xprt);
557
558 /*
559 * Enqueue the new transport on the accept queue of the listening
560 * transport
561 */
562 spin_lock_bh(&listen_xprt->sc_lock);
563 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
564 spin_unlock_bh(&listen_xprt->sc_lock);
565
566 /*
567 * Can't use svc_xprt_received here because we are not on a
568 * rqstp thread
569 */
570 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
571 svc_xprt_enqueue(&listen_xprt->sc_xprt);
572}
573
574/*
575 * Handles events generated on the listening endpoint. These events will be
576 * either be incoming connect requests or adapter removal events.
577 */
578static int rdma_listen_handler(struct rdma_cm_id *cma_id,
579 struct rdma_cm_event *event)
580{
581 struct svcxprt_rdma *xprt = cma_id->context;
582 int ret = 0;
583
584 switch (event->event) {
585 case RDMA_CM_EVENT_CONNECT_REQUEST:
586 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
587 "event=%d\n", cma_id, cma_id->context, event->event);
588 handle_connect_req(cma_id);
589 break;
590
591 case RDMA_CM_EVENT_ESTABLISHED:
592 /* Accept complete */
593 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
594 "cm_id=%p\n", xprt, cma_id);
595 break;
596
597 case RDMA_CM_EVENT_DEVICE_REMOVAL:
598 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
599 xprt, cma_id);
600 if (xprt)
601 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
602 break;
603
604 default:
605 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
606 "event=%d\n", cma_id, event->event);
607 break;
608 }
609
610 return ret;
611}
612
613static int rdma_cma_handler(struct rdma_cm_id *cma_id,
614 struct rdma_cm_event *event)
615{
616 struct svc_xprt *xprt = cma_id->context;
617 struct svcxprt_rdma *rdma =
618 container_of(xprt, struct svcxprt_rdma, sc_xprt);
619 switch (event->event) {
620 case RDMA_CM_EVENT_ESTABLISHED:
621 /* Accept complete */
c48cbb40 622 svc_xprt_get(xprt);
377f9b2f
TT
623 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
624 "cm_id=%p\n", xprt, cma_id);
625 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
626 svc_xprt_enqueue(xprt);
627 break;
628 case RDMA_CM_EVENT_DISCONNECTED:
629 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
630 xprt, cma_id);
631 if (xprt) {
632 set_bit(XPT_CLOSE, &xprt->xpt_flags);
633 svc_xprt_enqueue(xprt);
120693d1 634 svc_xprt_put(xprt);
377f9b2f
TT
635 }
636 break;
637 case RDMA_CM_EVENT_DEVICE_REMOVAL:
638 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
639 "event=%d\n", cma_id, xprt, event->event);
640 if (xprt) {
641 set_bit(XPT_CLOSE, &xprt->xpt_flags);
642 svc_xprt_enqueue(xprt);
643 }
644 break;
645 default:
646 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
647 "event=%d\n", cma_id, event->event);
648 break;
649 }
650 return 0;
651}
652
653/*
654 * Create a listening RDMA service endpoint.
655 */
656static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
657 struct sockaddr *sa, int salen,
658 int flags)
659{
660 struct rdma_cm_id *listen_id;
661 struct svcxprt_rdma *cma_xprt;
662 struct svc_xprt *xprt;
663 int ret;
664
665 dprintk("svcrdma: Creating RDMA socket\n");
666
667 cma_xprt = rdma_create_xprt(serv, 1);
668 if (!cma_xprt)
58e8f621 669 return ERR_PTR(-ENOMEM);
377f9b2f
TT
670 xprt = &cma_xprt->sc_xprt;
671
672 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
673 if (IS_ERR(listen_id)) {
58e8f621
TT
674 ret = PTR_ERR(listen_id);
675 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
676 goto err0;
377f9b2f 677 }
58e8f621 678
377f9b2f
TT
679 ret = rdma_bind_addr(listen_id, sa);
680 if (ret) {
377f9b2f 681 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
58e8f621 682 goto err1;
377f9b2f
TT
683 }
684 cma_xprt->sc_cm_id = listen_id;
685
686 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
687 if (ret) {
377f9b2f 688 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
58e8f621 689 goto err1;
377f9b2f
TT
690 }
691
692 /*
693 * We need to use the address from the cm_id in case the
694 * caller specified 0 for the port number.
695 */
696 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
697 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
698
699 return &cma_xprt->sc_xprt;
58e8f621
TT
700
701 err1:
702 rdma_destroy_id(listen_id);
703 err0:
704 kfree(cma_xprt);
705 return ERR_PTR(ret);
377f9b2f
TT
706}
707
708/*
709 * This is the xpo_recvfrom function for listening endpoints. Its
710 * purpose is to accept incoming connections. The CMA callback handler
711 * has already created a new transport and attached it to the new CMA
712 * ID.
713 *
714 * There is a queue of pending connections hung on the listening
715 * transport. This queue contains the new svc_xprt structure. This
716 * function takes svc_xprt structures off the accept_q and completes
717 * the connection.
718 */
719static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
720{
721 struct svcxprt_rdma *listen_rdma;
722 struct svcxprt_rdma *newxprt = NULL;
723 struct rdma_conn_param conn_param;
724 struct ib_qp_init_attr qp_attr;
725 struct ib_device_attr devattr;
726 struct sockaddr *sa;
727 int ret;
728 int i;
729
730 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
731 clear_bit(XPT_CONN, &xprt->xpt_flags);
732 /* Get the next entry off the accept list */
733 spin_lock_bh(&listen_rdma->sc_lock);
734 if (!list_empty(&listen_rdma->sc_accept_q)) {
735 newxprt = list_entry(listen_rdma->sc_accept_q.next,
736 struct svcxprt_rdma, sc_accept_q);
737 list_del_init(&newxprt->sc_accept_q);
738 }
739 if (!list_empty(&listen_rdma->sc_accept_q))
740 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
741 spin_unlock_bh(&listen_rdma->sc_lock);
742 if (!newxprt)
743 return NULL;
744
745 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
746 newxprt, newxprt->sc_cm_id);
747
748 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
749 if (ret) {
750 dprintk("svcrdma: could not query device attributes on "
751 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
752 goto errout;
753 }
754
755 /* Qualify the transport resource defaults with the
756 * capabilities of this particular device */
757 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
758 (size_t)RPCSVC_MAXPAGES);
759 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
760 (size_t)svcrdma_max_requests);
761 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
762
763 newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
764 (size_t)svcrdma_ord);
765
766 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
767 if (IS_ERR(newxprt->sc_pd)) {
768 dprintk("svcrdma: error creating PD for connect request\n");
769 goto errout;
770 }
771 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
772 sq_comp_handler,
773 cq_event_handler,
774 newxprt,
775 newxprt->sc_sq_depth,
776 0);
777 if (IS_ERR(newxprt->sc_sq_cq)) {
778 dprintk("svcrdma: error creating SQ CQ for connect request\n");
779 goto errout;
780 }
781 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
782 rq_comp_handler,
783 cq_event_handler,
784 newxprt,
785 newxprt->sc_max_requests,
786 0);
787 if (IS_ERR(newxprt->sc_rq_cq)) {
788 dprintk("svcrdma: error creating RQ CQ for connect request\n");
789 goto errout;
790 }
791
792 memset(&qp_attr, 0, sizeof qp_attr);
793 qp_attr.event_handler = qp_event_handler;
794 qp_attr.qp_context = &newxprt->sc_xprt;
795 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
796 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
797 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
798 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
799 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
800 qp_attr.qp_type = IB_QPT_RC;
801 qp_attr.send_cq = newxprt->sc_sq_cq;
802 qp_attr.recv_cq = newxprt->sc_rq_cq;
803 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
804 " cm_id->device=%p, sc_pd->device=%p\n"
805 " cap.max_send_wr = %d\n"
806 " cap.max_recv_wr = %d\n"
807 " cap.max_send_sge = %d\n"
808 " cap.max_recv_sge = %d\n",
809 newxprt->sc_cm_id, newxprt->sc_pd,
810 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
811 qp_attr.cap.max_send_wr,
812 qp_attr.cap.max_recv_wr,
813 qp_attr.cap.max_send_sge,
814 qp_attr.cap.max_recv_sge);
815
816 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
817 if (ret) {
818 /*
819 * XXX: This is a hack. We need a xx_request_qp interface
820 * that will adjust the qp_attr's with a best-effort
821 * number
822 */
823 qp_attr.cap.max_send_sge -= 2;
824 qp_attr.cap.max_recv_sge -= 2;
825 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
826 &qp_attr);
827 if (ret) {
828 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
829 goto errout;
830 }
831 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
832 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
833 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
834 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
835 }
c48cbb40 836 svc_xprt_get(&newxprt->sc_xprt);
377f9b2f
TT
837 newxprt->sc_qp = newxprt->sc_cm_id->qp;
838
839 /* Register all of physical memory */
840 newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
841 IB_ACCESS_LOCAL_WRITE |
842 IB_ACCESS_REMOTE_WRITE);
843 if (IS_ERR(newxprt->sc_phys_mr)) {
844 dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
845 goto errout;
846 }
847
848 /* Post receive buffers */
849 for (i = 0; i < newxprt->sc_max_requests; i++) {
850 ret = svc_rdma_post_recv(newxprt);
851 if (ret) {
852 dprintk("svcrdma: failure posting receive buffers\n");
853 goto errout;
854 }
855 }
856
857 /* Swap out the handler */
858 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
859
860 /* Accept Connection */
861 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
862 memset(&conn_param, 0, sizeof conn_param);
863 conn_param.responder_resources = 0;
864 conn_param.initiator_depth = newxprt->sc_ord;
865 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
866 if (ret) {
867 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
868 ret);
869 goto errout;
870 }
871
872 dprintk("svcrdma: new connection %p accepted with the following "
873 "attributes:\n"
874 " local_ip : %d.%d.%d.%d\n"
875 " local_port : %d\n"
876 " remote_ip : %d.%d.%d.%d\n"
877 " remote_port : %d\n"
878 " max_sge : %d\n"
879 " sq_depth : %d\n"
880 " max_requests : %d\n"
881 " ord : %d\n",
882 newxprt,
883 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
884 route.addr.src_addr)->sin_addr.s_addr),
885 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
886 route.addr.src_addr)->sin_port),
887 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
888 route.addr.dst_addr)->sin_addr.s_addr),
889 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
890 route.addr.dst_addr)->sin_port),
891 newxprt->sc_max_sge,
892 newxprt->sc_sq_depth,
893 newxprt->sc_max_requests,
894 newxprt->sc_ord);
895
896 /* Set the local and remote addresses in the transport */
897 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
898 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
899 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
900 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
901
902 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
903 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
904 return &newxprt->sc_xprt;
905
906 errout:
907 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
c48cbb40
TT
908 /* Take a reference in case the DTO handler runs */
909 svc_xprt_get(&newxprt->sc_xprt);
910 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp)) {
911 ib_destroy_qp(newxprt->sc_qp);
912 svc_xprt_put(&newxprt->sc_xprt);
913 }
377f9b2f 914 rdma_destroy_id(newxprt->sc_cm_id);
c48cbb40
TT
915 /* This call to put will destroy the transport */
916 svc_xprt_put(&newxprt->sc_xprt);
377f9b2f
TT
917 return NULL;
918}
919
377f9b2f
TT
920static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
921{
377f9b2f
TT
922}
923
c48cbb40
TT
924/*
925 * When connected, an svc_xprt has at least three references:
926 *
927 * - A reference held by the QP. We still hold that here because this
928 * code deletes the QP and puts the reference.
929 *
930 * - A reference held by the cm_id between the ESTABLISHED and
931 * DISCONNECTED events. If the remote peer disconnected first, this
932 * reference could be gone.
933 *
934 * - A reference held by the svc_recv code that called this function
935 * as part of close processing.
936 *
937 * At a minimum two references should still be held.
938 */
377f9b2f
TT
939static void svc_rdma_detach(struct svc_xprt *xprt)
940{
941 struct svcxprt_rdma *rdma =
942 container_of(xprt, struct svcxprt_rdma, sc_xprt);
377f9b2f 943 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
c48cbb40
TT
944
945 /* Disconnect and flush posted WQE */
377f9b2f 946 rdma_disconnect(rdma->sc_cm_id);
377f9b2f 947
c48cbb40
TT
948 /* Destroy the QP if present (not a listener) */
949 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp)) {
950 ib_destroy_qp(rdma->sc_qp);
951 svc_xprt_put(xprt);
952 }
953
954 /* Destroy the CM ID */
955 rdma_destroy_id(rdma->sc_cm_id);
377f9b2f
TT
956}
957
958static void svc_rdma_free(struct svc_xprt *xprt)
959{
960 struct svcxprt_rdma *rdma = (struct svcxprt_rdma *)xprt;
961 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
c48cbb40
TT
962 /* We should only be called from kref_put */
963 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) != 0);
964 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
965 ib_destroy_cq(rdma->sc_sq_cq);
377f9b2f 966
c48cbb40
TT
967 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
968 ib_destroy_cq(rdma->sc_rq_cq);
377f9b2f 969
c48cbb40
TT
970 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
971 ib_dereg_mr(rdma->sc_phys_mr);
377f9b2f 972
c48cbb40
TT
973 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
974 ib_dealloc_pd(rdma->sc_pd);
377f9b2f 975
c48cbb40
TT
976 destroy_context_cache(rdma->sc_ctxt_head);
977 kfree(rdma);
377f9b2f
TT
978}
979
980static int svc_rdma_has_wspace(struct svc_xprt *xprt)
981{
982 struct svcxprt_rdma *rdma =
983 container_of(xprt, struct svcxprt_rdma, sc_xprt);
984
985 /*
986 * If there are fewer SQ WR available than required to send a
987 * simple response, return false.
988 */
989 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
990 return 0;
991
992 /*
993 * ...or there are already waiters on the SQ,
994 * return false.
995 */
996 if (waitqueue_active(&rdma->sc_send_wait))
997 return 0;
998
999 /* Otherwise return true. */
1000 return 1;
1001}
1002
1003int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1004{
1005 struct ib_send_wr *bad_wr;
1006 int ret;
1007
1008 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
9d6347ac 1009 return -ENOTCONN;
377f9b2f
TT
1010
1011 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1012 BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
1013 wr->opcode);
1014 /* If the SQ is full, wait until an SQ entry is available */
1015 while (1) {
1016 spin_lock_bh(&xprt->sc_lock);
1017 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1018 spin_unlock_bh(&xprt->sc_lock);
1019 atomic_inc(&rdma_stat_sq_starve);
dbcd00eb
TT
1020
1021 /* See if we can opportunistically reap SQ WR to make room */
377f9b2f
TT
1022 sq_cq_reap(xprt);
1023
1024 /* Wait until SQ WR available if SQ still full */
1025 wait_event(xprt->sc_send_wait,
1026 atomic_read(&xprt->sc_sq_count) <
1027 xprt->sc_sq_depth);
830bb59b
TT
1028 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1029 return 0;
377f9b2f
TT
1030 continue;
1031 }
1032 /* Bumped used SQ WR count and post */
1033 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1034 if (!ret)
1035 atomic_inc(&xprt->sc_sq_count);
1036 else
1037 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1038 "sc_sq_count=%d, sc_sq_depth=%d\n",
1039 ret, atomic_read(&xprt->sc_sq_count),
1040 xprt->sc_sq_depth);
1041 spin_unlock_bh(&xprt->sc_lock);
1042 break;
1043 }
1044 return ret;
1045}
1046
1047int svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1048 enum rpcrdma_errcode err)
1049{
1050 struct ib_send_wr err_wr;
1051 struct ib_sge sge;
1052 struct page *p;
1053 struct svc_rdma_op_ctxt *ctxt;
1054 u32 *va;
1055 int length;
1056 int ret;
1057
1058 p = svc_rdma_get_page();
1059 va = page_address(p);
1060
1061 /* XDR encode error */
1062 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1063
1064 /* Prepare SGE for local address */
1065 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1066 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1067 sge.lkey = xprt->sc_phys_mr->lkey;
1068 sge.length = length;
1069
1070 ctxt = svc_rdma_get_context(xprt);
1071 ctxt->count = 1;
1072 ctxt->pages[0] = p;
1073
1074 /* Prepare SEND WR */
1075 memset(&err_wr, 0, sizeof err_wr);
1076 ctxt->wr_op = IB_WR_SEND;
1077 err_wr.wr_id = (unsigned long)ctxt;
1078 err_wr.sg_list = &sge;
1079 err_wr.num_sge = 1;
1080 err_wr.opcode = IB_WR_SEND;
1081 err_wr.send_flags = IB_SEND_SIGNALED;
1082
1083 /* Post It */
1084 ret = svc_rdma_send(xprt, &err_wr);
1085 if (ret) {
1086 dprintk("svcrdma: Error posting send = %d\n", ret);
1087 svc_rdma_put_context(ctxt, 1);
1088 }
1089
1090 return ret;
1091}
This page took 0.107255 seconds and 5 git commands to generate.