kvm tools, setup: Create private directory
[linux-2.6/next.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
blobc3c232a88d94daa8a9facb1c9cbd3a3025d0354e
1 /*
2 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 * Author: Tom Tucker <tom@opengridcomputing.com>
42 #include <linux/sunrpc/svc_xprt.h>
43 #include <linux/sunrpc/debug.h>
44 #include <linux/sunrpc/rpc_rdma.h>
45 #include <linux/sched.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/workqueue.h>
49 #include <rdma/ib_verbs.h>
50 #include <rdma/rdma_cm.h>
51 #include <linux/sunrpc/svc_rdma.h>
53 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
55 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
56 struct net *net,
57 struct sockaddr *sa, int salen,
58 int flags);
59 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
60 static void svc_rdma_release_rqst(struct svc_rqst *);
61 static void dto_tasklet_func(unsigned long data);
62 static void svc_rdma_detach(struct svc_xprt *xprt);
63 static void svc_rdma_free(struct svc_xprt *xprt);
64 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
65 static void rq_cq_reap(struct svcxprt_rdma *xprt);
66 static void sq_cq_reap(struct svcxprt_rdma *xprt);
68 static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
69 static DEFINE_SPINLOCK(dto_lock);
70 static LIST_HEAD(dto_xprt_q);
72 static struct svc_xprt_ops svc_rdma_ops = {
73 .xpo_create = svc_rdma_create,
74 .xpo_recvfrom = svc_rdma_recvfrom,
75 .xpo_sendto = svc_rdma_sendto,
76 .xpo_release_rqst = svc_rdma_release_rqst,
77 .xpo_detach = svc_rdma_detach,
78 .xpo_free = svc_rdma_free,
79 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
80 .xpo_has_wspace = svc_rdma_has_wspace,
81 .xpo_accept = svc_rdma_accept,
84 struct svc_xprt_class svc_rdma_class = {
85 .xcl_name = "rdma",
86 .xcl_owner = THIS_MODULE,
87 .xcl_ops = &svc_rdma_ops,
88 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
91 /* WR context cache. Created in svc_rdma.c */
92 extern struct kmem_cache *svc_rdma_ctxt_cachep;
94 /* Workqueue created in svc_rdma.c */
95 extern struct workqueue_struct *svc_rdma_wq;
97 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
99 struct svc_rdma_op_ctxt *ctxt;
101 while (1) {
102 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
103 if (ctxt)
104 break;
105 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
107 ctxt->xprt = xprt;
108 INIT_LIST_HEAD(&ctxt->dto_q);
109 ctxt->count = 0;
110 ctxt->frmr = NULL;
111 atomic_inc(&xprt->sc_ctxt_used);
112 return ctxt;
115 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
117 struct svcxprt_rdma *xprt = ctxt->xprt;
118 int i;
119 for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
121 * Unmap the DMA addr in the SGE if the lkey matches
122 * the sc_dma_lkey, otherwise, ignore it since it is
123 * an FRMR lkey and will be unmapped later when the
124 * last WR that uses it completes.
126 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
127 atomic_dec(&xprt->sc_dma_used);
128 ib_dma_unmap_page(xprt->sc_cm_id->device,
129 ctxt->sge[i].addr,
130 ctxt->sge[i].length,
131 ctxt->direction);
136 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
138 struct svcxprt_rdma *xprt;
139 int i;
141 BUG_ON(!ctxt);
142 xprt = ctxt->xprt;
143 if (free_pages)
144 for (i = 0; i < ctxt->count; i++)
145 put_page(ctxt->pages[i]);
147 kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
148 atomic_dec(&xprt->sc_ctxt_used);
151 /* Temporary NFS request map cache. Created in svc_rdma.c */
152 extern struct kmem_cache *svc_rdma_map_cachep;
155 * Temporary NFS req mappings are shared across all transport
156 * instances. These are short lived and should be bounded by the number
157 * of concurrent server threads * depth of the SQ.
159 struct svc_rdma_req_map *svc_rdma_get_req_map(void)
161 struct svc_rdma_req_map *map;
162 while (1) {
163 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
164 if (map)
165 break;
166 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
168 map->count = 0;
169 map->frmr = NULL;
170 return map;
173 void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
175 kmem_cache_free(svc_rdma_map_cachep, map);
178 /* ib_cq event handler */
179 static void cq_event_handler(struct ib_event *event, void *context)
181 struct svc_xprt *xprt = context;
182 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
183 event->event, context);
184 set_bit(XPT_CLOSE, &xprt->xpt_flags);
187 /* QP event handler */
188 static void qp_event_handler(struct ib_event *event, void *context)
190 struct svc_xprt *xprt = context;
192 switch (event->event) {
193 /* These are considered benign events */
194 case IB_EVENT_PATH_MIG:
195 case IB_EVENT_COMM_EST:
196 case IB_EVENT_SQ_DRAINED:
197 case IB_EVENT_QP_LAST_WQE_REACHED:
198 dprintk("svcrdma: QP event %d received for QP=%p\n",
199 event->event, event->element.qp);
200 break;
201 /* These are considered fatal events */
202 case IB_EVENT_PATH_MIG_ERR:
203 case IB_EVENT_QP_FATAL:
204 case IB_EVENT_QP_REQ_ERR:
205 case IB_EVENT_QP_ACCESS_ERR:
206 case IB_EVENT_DEVICE_FATAL:
207 default:
208 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
209 "closing transport\n",
210 event->event, event->element.qp);
211 set_bit(XPT_CLOSE, &xprt->xpt_flags);
212 break;
217 * Data Transfer Operation Tasklet
219 * Walks a list of transports with I/O pending, removing entries as
220 * they are added to the server's I/O pending list. Two bits indicate
221 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
222 * spinlock that serializes access to the transport list with the RQ
223 * and SQ interrupt handlers.
225 static void dto_tasklet_func(unsigned long data)
227 struct svcxprt_rdma *xprt;
228 unsigned long flags;
230 spin_lock_irqsave(&dto_lock, flags);
231 while (!list_empty(&dto_xprt_q)) {
232 xprt = list_entry(dto_xprt_q.next,
233 struct svcxprt_rdma, sc_dto_q);
234 list_del_init(&xprt->sc_dto_q);
235 spin_unlock_irqrestore(&dto_lock, flags);
237 rq_cq_reap(xprt);
238 sq_cq_reap(xprt);
240 svc_xprt_put(&xprt->sc_xprt);
241 spin_lock_irqsave(&dto_lock, flags);
243 spin_unlock_irqrestore(&dto_lock, flags);
247 * Receive Queue Completion Handler
249 * Since an RQ completion handler is called on interrupt context, we
250 * need to defer the handling of the I/O to a tasklet
252 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
254 struct svcxprt_rdma *xprt = cq_context;
255 unsigned long flags;
257 /* Guard against unconditional flush call for destroyed QP */
258 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
259 return;
262 * Set the bit regardless of whether or not it's on the list
263 * because it may be on the list already due to an SQ
264 * completion.
266 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
269 * If this transport is not already on the DTO transport queue,
270 * add it
272 spin_lock_irqsave(&dto_lock, flags);
273 if (list_empty(&xprt->sc_dto_q)) {
274 svc_xprt_get(&xprt->sc_xprt);
275 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
277 spin_unlock_irqrestore(&dto_lock, flags);
279 /* Tasklet does all the work to avoid irqsave locks. */
280 tasklet_schedule(&dto_tasklet);
284 * rq_cq_reap - Process the RQ CQ.
286 * Take all completing WC off the CQE and enqueue the associated DTO
287 * context on the dto_q for the transport.
289 * Note that caller must hold a transport reference.
291 static void rq_cq_reap(struct svcxprt_rdma *xprt)
293 int ret;
294 struct ib_wc wc;
295 struct svc_rdma_op_ctxt *ctxt = NULL;
297 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
298 return;
300 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
301 atomic_inc(&rdma_stat_rq_poll);
303 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
304 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
305 ctxt->wc_status = wc.status;
306 ctxt->byte_len = wc.byte_len;
307 svc_rdma_unmap_dma(ctxt);
308 if (wc.status != IB_WC_SUCCESS) {
309 /* Close the transport */
310 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
311 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
312 svc_rdma_put_context(ctxt, 1);
313 svc_xprt_put(&xprt->sc_xprt);
314 continue;
316 spin_lock_bh(&xprt->sc_rq_dto_lock);
317 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
318 spin_unlock_bh(&xprt->sc_rq_dto_lock);
319 svc_xprt_put(&xprt->sc_xprt);
322 if (ctxt)
323 atomic_inc(&rdma_stat_rq_prod);
325 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
327 * If data arrived before established event,
328 * don't enqueue. This defers RPC I/O until the
329 * RDMA connection is complete.
331 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
332 svc_xprt_enqueue(&xprt->sc_xprt);
336 * Process a completion context
338 static void process_context(struct svcxprt_rdma *xprt,
339 struct svc_rdma_op_ctxt *ctxt)
341 svc_rdma_unmap_dma(ctxt);
343 switch (ctxt->wr_op) {
344 case IB_WR_SEND:
345 if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
346 svc_rdma_put_frmr(xprt, ctxt->frmr);
347 svc_rdma_put_context(ctxt, 1);
348 break;
350 case IB_WR_RDMA_WRITE:
351 svc_rdma_put_context(ctxt, 0);
352 break;
354 case IB_WR_RDMA_READ:
355 case IB_WR_RDMA_READ_WITH_INV:
356 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
357 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
358 BUG_ON(!read_hdr);
359 if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
360 svc_rdma_put_frmr(xprt, ctxt->frmr);
361 spin_lock_bh(&xprt->sc_rq_dto_lock);
362 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
363 list_add_tail(&read_hdr->dto_q,
364 &xprt->sc_read_complete_q);
365 spin_unlock_bh(&xprt->sc_rq_dto_lock);
366 svc_xprt_enqueue(&xprt->sc_xprt);
368 svc_rdma_put_context(ctxt, 0);
369 break;
371 default:
372 printk(KERN_ERR "svcrdma: unexpected completion type, "
373 "opcode=%d\n",
374 ctxt->wr_op);
375 break;
380 * Send Queue Completion Handler - potentially called on interrupt context.
382 * Note that caller must hold a transport reference.
384 static void sq_cq_reap(struct svcxprt_rdma *xprt)
386 struct svc_rdma_op_ctxt *ctxt = NULL;
387 struct ib_wc wc;
388 struct ib_cq *cq = xprt->sc_sq_cq;
389 int ret;
391 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
392 return;
394 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
395 atomic_inc(&rdma_stat_sq_poll);
396 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
397 if (wc.status != IB_WC_SUCCESS)
398 /* Close the transport */
399 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
401 /* Decrement used SQ WR count */
402 atomic_dec(&xprt->sc_sq_count);
403 wake_up(&xprt->sc_send_wait);
405 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
406 if (ctxt)
407 process_context(xprt, ctxt);
409 svc_xprt_put(&xprt->sc_xprt);
412 if (ctxt)
413 atomic_inc(&rdma_stat_sq_prod);
416 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
418 struct svcxprt_rdma *xprt = cq_context;
419 unsigned long flags;
421 /* Guard against unconditional flush call for destroyed QP */
422 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
423 return;
426 * Set the bit regardless of whether or not it's on the list
427 * because it may be on the list already due to an RQ
428 * completion.
430 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
433 * If this transport is not already on the DTO transport queue,
434 * add it
436 spin_lock_irqsave(&dto_lock, flags);
437 if (list_empty(&xprt->sc_dto_q)) {
438 svc_xprt_get(&xprt->sc_xprt);
439 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
441 spin_unlock_irqrestore(&dto_lock, flags);
443 /* Tasklet does all the work to avoid irqsave locks. */
444 tasklet_schedule(&dto_tasklet);
447 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
448 int listener)
450 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
452 if (!cma_xprt)
453 return NULL;
454 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
455 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
456 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
457 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
458 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
459 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
460 init_waitqueue_head(&cma_xprt->sc_send_wait);
462 spin_lock_init(&cma_xprt->sc_lock);
463 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
464 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
466 cma_xprt->sc_ord = svcrdma_ord;
468 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
469 cma_xprt->sc_max_requests = svcrdma_max_requests;
470 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
471 atomic_set(&cma_xprt->sc_sq_count, 0);
472 atomic_set(&cma_xprt->sc_ctxt_used, 0);
474 if (listener)
475 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
477 return cma_xprt;
480 struct page *svc_rdma_get_page(void)
482 struct page *page;
484 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
485 /* If we can't get memory, wait a bit and try again */
486 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
487 "jiffies.\n");
488 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
490 return page;
493 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
495 struct ib_recv_wr recv_wr, *bad_recv_wr;
496 struct svc_rdma_op_ctxt *ctxt;
497 struct page *page;
498 dma_addr_t pa;
499 int sge_no;
500 int buflen;
501 int ret;
503 ctxt = svc_rdma_get_context(xprt);
504 buflen = 0;
505 ctxt->direction = DMA_FROM_DEVICE;
506 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
507 BUG_ON(sge_no >= xprt->sc_max_sge);
508 page = svc_rdma_get_page();
509 ctxt->pages[sge_no] = page;
510 pa = ib_dma_map_page(xprt->sc_cm_id->device,
511 page, 0, PAGE_SIZE,
512 DMA_FROM_DEVICE);
513 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
514 goto err_put_ctxt;
515 atomic_inc(&xprt->sc_dma_used);
516 ctxt->sge[sge_no].addr = pa;
517 ctxt->sge[sge_no].length = PAGE_SIZE;
518 ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
519 ctxt->count = sge_no + 1;
520 buflen += PAGE_SIZE;
522 recv_wr.next = NULL;
523 recv_wr.sg_list = &ctxt->sge[0];
524 recv_wr.num_sge = ctxt->count;
525 recv_wr.wr_id = (u64)(unsigned long)ctxt;
527 svc_xprt_get(&xprt->sc_xprt);
528 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
529 if (ret) {
530 svc_rdma_unmap_dma(ctxt);
531 svc_rdma_put_context(ctxt, 1);
532 svc_xprt_put(&xprt->sc_xprt);
534 return ret;
536 err_put_ctxt:
537 svc_rdma_unmap_dma(ctxt);
538 svc_rdma_put_context(ctxt, 1);
539 return -ENOMEM;
543 * This function handles the CONNECT_REQUEST event on a listening
544 * endpoint. It is passed the cma_id for the _new_ connection. The context in
545 * this cma_id is inherited from the listening cma_id and is the svc_xprt
546 * structure for the listening endpoint.
548 * This function creates a new xprt for the new connection and enqueues it on
549 * the accept queue for the listent xprt. When the listen thread is kicked, it
550 * will call the recvfrom method on the listen xprt which will accept the new
551 * connection.
553 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
555 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
556 struct svcxprt_rdma *newxprt;
557 struct sockaddr *sa;
559 /* Create a new transport */
560 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
561 if (!newxprt) {
562 dprintk("svcrdma: failed to create new transport\n");
563 return;
565 newxprt->sc_cm_id = new_cma_id;
566 new_cma_id->context = newxprt;
567 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
568 newxprt, newxprt->sc_cm_id, listen_xprt);
570 /* Save client advertised inbound read limit for use later in accept. */
571 newxprt->sc_ord = client_ird;
573 /* Set the local and remote addresses in the transport */
574 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
575 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
576 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
577 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
580 * Enqueue the new transport on the accept queue of the listening
581 * transport
583 spin_lock_bh(&listen_xprt->sc_lock);
584 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
585 spin_unlock_bh(&listen_xprt->sc_lock);
588 * Can't use svc_xprt_received here because we are not on a
589 * rqstp thread
591 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
592 svc_xprt_enqueue(&listen_xprt->sc_xprt);
596 * Handles events generated on the listening endpoint. These events will be
597 * either be incoming connect requests or adapter removal events.
599 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
600 struct rdma_cm_event *event)
602 struct svcxprt_rdma *xprt = cma_id->context;
603 int ret = 0;
605 switch (event->event) {
606 case RDMA_CM_EVENT_CONNECT_REQUEST:
607 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
608 "event=%d\n", cma_id, cma_id->context, event->event);
609 handle_connect_req(cma_id,
610 event->param.conn.initiator_depth);
611 break;
613 case RDMA_CM_EVENT_ESTABLISHED:
614 /* Accept complete */
615 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
616 "cm_id=%p\n", xprt, cma_id);
617 break;
619 case RDMA_CM_EVENT_DEVICE_REMOVAL:
620 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
621 xprt, cma_id);
622 if (xprt)
623 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
624 break;
626 default:
627 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
628 "event=%d\n", cma_id, event->event);
629 break;
632 return ret;
635 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
636 struct rdma_cm_event *event)
638 struct svc_xprt *xprt = cma_id->context;
639 struct svcxprt_rdma *rdma =
640 container_of(xprt, struct svcxprt_rdma, sc_xprt);
641 switch (event->event) {
642 case RDMA_CM_EVENT_ESTABLISHED:
643 /* Accept complete */
644 svc_xprt_get(xprt);
645 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
646 "cm_id=%p\n", xprt, cma_id);
647 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
648 svc_xprt_enqueue(xprt);
649 break;
650 case RDMA_CM_EVENT_DISCONNECTED:
651 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
652 xprt, cma_id);
653 if (xprt) {
654 set_bit(XPT_CLOSE, &xprt->xpt_flags);
655 svc_xprt_enqueue(xprt);
656 svc_xprt_put(xprt);
658 break;
659 case RDMA_CM_EVENT_DEVICE_REMOVAL:
660 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
661 "event=%d\n", cma_id, xprt, event->event);
662 if (xprt) {
663 set_bit(XPT_CLOSE, &xprt->xpt_flags);
664 svc_xprt_enqueue(xprt);
666 break;
667 default:
668 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
669 "event=%d\n", cma_id, event->event);
670 break;
672 return 0;
676 * Create a listening RDMA service endpoint.
678 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
679 struct net *net,
680 struct sockaddr *sa, int salen,
681 int flags)
683 struct rdma_cm_id *listen_id;
684 struct svcxprt_rdma *cma_xprt;
685 struct svc_xprt *xprt;
686 int ret;
688 dprintk("svcrdma: Creating RDMA socket\n");
689 if (sa->sa_family != AF_INET) {
690 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
691 return ERR_PTR(-EAFNOSUPPORT);
693 cma_xprt = rdma_create_xprt(serv, 1);
694 if (!cma_xprt)
695 return ERR_PTR(-ENOMEM);
696 xprt = &cma_xprt->sc_xprt;
698 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
699 IB_QPT_RC);
700 if (IS_ERR(listen_id)) {
701 ret = PTR_ERR(listen_id);
702 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
703 goto err0;
706 ret = rdma_bind_addr(listen_id, sa);
707 if (ret) {
708 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
709 goto err1;
711 cma_xprt->sc_cm_id = listen_id;
713 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
714 if (ret) {
715 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
716 goto err1;
720 * We need to use the address from the cm_id in case the
721 * caller specified 0 for the port number.
723 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
724 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
726 return &cma_xprt->sc_xprt;
728 err1:
729 rdma_destroy_id(listen_id);
730 err0:
731 kfree(cma_xprt);
732 return ERR_PTR(ret);
735 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
737 struct ib_mr *mr;
738 struct ib_fast_reg_page_list *pl;
739 struct svc_rdma_fastreg_mr *frmr;
741 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
742 if (!frmr)
743 goto err;
745 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
746 if (IS_ERR(mr))
747 goto err_free_frmr;
749 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
750 RPCSVC_MAXPAGES);
751 if (IS_ERR(pl))
752 goto err_free_mr;
754 frmr->mr = mr;
755 frmr->page_list = pl;
756 INIT_LIST_HEAD(&frmr->frmr_list);
757 return frmr;
759 err_free_mr:
760 ib_dereg_mr(mr);
761 err_free_frmr:
762 kfree(frmr);
763 err:
764 return ERR_PTR(-ENOMEM);
767 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
769 struct svc_rdma_fastreg_mr *frmr;
771 while (!list_empty(&xprt->sc_frmr_q)) {
772 frmr = list_entry(xprt->sc_frmr_q.next,
773 struct svc_rdma_fastreg_mr, frmr_list);
774 list_del_init(&frmr->frmr_list);
775 ib_dereg_mr(frmr->mr);
776 ib_free_fast_reg_page_list(frmr->page_list);
777 kfree(frmr);
781 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
783 struct svc_rdma_fastreg_mr *frmr = NULL;
785 spin_lock_bh(&rdma->sc_frmr_q_lock);
786 if (!list_empty(&rdma->sc_frmr_q)) {
787 frmr = list_entry(rdma->sc_frmr_q.next,
788 struct svc_rdma_fastreg_mr, frmr_list);
789 list_del_init(&frmr->frmr_list);
790 frmr->map_len = 0;
791 frmr->page_list_len = 0;
793 spin_unlock_bh(&rdma->sc_frmr_q_lock);
794 if (frmr)
795 return frmr;
797 return rdma_alloc_frmr(rdma);
800 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
801 struct svc_rdma_fastreg_mr *frmr)
803 int page_no;
804 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
805 dma_addr_t addr = frmr->page_list->page_list[page_no];
806 if (ib_dma_mapping_error(frmr->mr->device, addr))
807 continue;
808 atomic_dec(&xprt->sc_dma_used);
809 ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
810 frmr->direction);
814 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
815 struct svc_rdma_fastreg_mr *frmr)
817 if (frmr) {
818 frmr_unmap_dma(rdma, frmr);
819 spin_lock_bh(&rdma->sc_frmr_q_lock);
820 BUG_ON(!list_empty(&frmr->frmr_list));
821 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
822 spin_unlock_bh(&rdma->sc_frmr_q_lock);
827 * This is the xpo_recvfrom function for listening endpoints. Its
828 * purpose is to accept incoming connections. The CMA callback handler
829 * has already created a new transport and attached it to the new CMA
830 * ID.
832 * There is a queue of pending connections hung on the listening
833 * transport. This queue contains the new svc_xprt structure. This
834 * function takes svc_xprt structures off the accept_q and completes
835 * the connection.
837 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
839 struct svcxprt_rdma *listen_rdma;
840 struct svcxprt_rdma *newxprt = NULL;
841 struct rdma_conn_param conn_param;
842 struct ib_qp_init_attr qp_attr;
843 struct ib_device_attr devattr;
844 int uninitialized_var(dma_mr_acc);
845 int need_dma_mr;
846 int ret;
847 int i;
849 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
850 clear_bit(XPT_CONN, &xprt->xpt_flags);
851 /* Get the next entry off the accept list */
852 spin_lock_bh(&listen_rdma->sc_lock);
853 if (!list_empty(&listen_rdma->sc_accept_q)) {
854 newxprt = list_entry(listen_rdma->sc_accept_q.next,
855 struct svcxprt_rdma, sc_accept_q);
856 list_del_init(&newxprt->sc_accept_q);
858 if (!list_empty(&listen_rdma->sc_accept_q))
859 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
860 spin_unlock_bh(&listen_rdma->sc_lock);
861 if (!newxprt)
862 return NULL;
864 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
865 newxprt, newxprt->sc_cm_id);
867 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
868 if (ret) {
869 dprintk("svcrdma: could not query device attributes on "
870 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
871 goto errout;
874 /* Qualify the transport resource defaults with the
875 * capabilities of this particular device */
876 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
877 (size_t)RPCSVC_MAXPAGES);
878 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
879 (size_t)svcrdma_max_requests);
880 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
883 * Limit ORD based on client limit, local device limit, and
884 * configured svcrdma limit.
886 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
887 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
889 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
890 if (IS_ERR(newxprt->sc_pd)) {
891 dprintk("svcrdma: error creating PD for connect request\n");
892 goto errout;
894 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
895 sq_comp_handler,
896 cq_event_handler,
897 newxprt,
898 newxprt->sc_sq_depth,
900 if (IS_ERR(newxprt->sc_sq_cq)) {
901 dprintk("svcrdma: error creating SQ CQ for connect request\n");
902 goto errout;
904 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
905 rq_comp_handler,
906 cq_event_handler,
907 newxprt,
908 newxprt->sc_max_requests,
910 if (IS_ERR(newxprt->sc_rq_cq)) {
911 dprintk("svcrdma: error creating RQ CQ for connect request\n");
912 goto errout;
915 memset(&qp_attr, 0, sizeof qp_attr);
916 qp_attr.event_handler = qp_event_handler;
917 qp_attr.qp_context = &newxprt->sc_xprt;
918 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
919 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
920 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
921 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
922 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
923 qp_attr.qp_type = IB_QPT_RC;
924 qp_attr.send_cq = newxprt->sc_sq_cq;
925 qp_attr.recv_cq = newxprt->sc_rq_cq;
926 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
927 " cm_id->device=%p, sc_pd->device=%p\n"
928 " cap.max_send_wr = %d\n"
929 " cap.max_recv_wr = %d\n"
930 " cap.max_send_sge = %d\n"
931 " cap.max_recv_sge = %d\n",
932 newxprt->sc_cm_id, newxprt->sc_pd,
933 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
934 qp_attr.cap.max_send_wr,
935 qp_attr.cap.max_recv_wr,
936 qp_attr.cap.max_send_sge,
937 qp_attr.cap.max_recv_sge);
939 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
940 if (ret) {
942 * XXX: This is a hack. We need a xx_request_qp interface
943 * that will adjust the qp_attr's with a best-effort
944 * number
946 qp_attr.cap.max_send_sge -= 2;
947 qp_attr.cap.max_recv_sge -= 2;
948 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
949 &qp_attr);
950 if (ret) {
951 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
952 goto errout;
954 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
955 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
956 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
957 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
959 newxprt->sc_qp = newxprt->sc_cm_id->qp;
962 * Use the most secure set of MR resources based on the
963 * transport type and available memory management features in
964 * the device. Here's the table implemented below:
966 * Fast Global DMA Remote WR
967 * Reg LKEY MR Access
968 * Sup'd Sup'd Needed Needed
970 * IWARP N N Y Y
971 * N Y Y Y
972 * Y N Y N
973 * Y Y N -
975 * IB N N Y N
976 * N Y N -
977 * Y N Y N
978 * Y Y N -
980 * NB: iWARP requires remote write access for the data sink
981 * of an RDMA_READ. IB does not.
983 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
984 newxprt->sc_frmr_pg_list_len =
985 devattr.max_fast_reg_page_list_len;
986 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
990 * Determine if a DMA MR is required and if so, what privs are required
992 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
993 case RDMA_TRANSPORT_IWARP:
994 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
995 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
996 need_dma_mr = 1;
997 dma_mr_acc =
998 (IB_ACCESS_LOCAL_WRITE |
999 IB_ACCESS_REMOTE_WRITE);
1000 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1001 need_dma_mr = 1;
1002 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1003 } else
1004 need_dma_mr = 0;
1005 break;
1006 case RDMA_TRANSPORT_IB:
1007 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1008 need_dma_mr = 1;
1009 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1010 } else
1011 need_dma_mr = 0;
1012 break;
1013 default:
1014 goto errout;
1017 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1018 if (need_dma_mr) {
1019 /* Register all of physical memory */
1020 newxprt->sc_phys_mr =
1021 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1022 if (IS_ERR(newxprt->sc_phys_mr)) {
1023 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1024 ret);
1025 goto errout;
1027 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1028 } else
1029 newxprt->sc_dma_lkey =
1030 newxprt->sc_cm_id->device->local_dma_lkey;
1032 /* Post receive buffers */
1033 for (i = 0; i < newxprt->sc_max_requests; i++) {
1034 ret = svc_rdma_post_recv(newxprt);
1035 if (ret) {
1036 dprintk("svcrdma: failure posting receive buffers\n");
1037 goto errout;
1041 /* Swap out the handler */
1042 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1045 * Arm the CQs for the SQ and RQ before accepting so we can't
1046 * miss the first message
1048 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1049 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1051 /* Accept Connection */
1052 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1053 memset(&conn_param, 0, sizeof conn_param);
1054 conn_param.responder_resources = 0;
1055 conn_param.initiator_depth = newxprt->sc_ord;
1056 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1057 if (ret) {
1058 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1059 ret);
1060 goto errout;
1063 dprintk("svcrdma: new connection %p accepted with the following "
1064 "attributes:\n"
1065 " local_ip : %pI4\n"
1066 " local_port : %d\n"
1067 " remote_ip : %pI4\n"
1068 " remote_port : %d\n"
1069 " max_sge : %d\n"
1070 " sq_depth : %d\n"
1071 " max_requests : %d\n"
1072 " ord : %d\n",
1073 newxprt,
1074 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1075 route.addr.src_addr)->sin_addr.s_addr,
1076 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1077 route.addr.src_addr)->sin_port),
1078 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1079 route.addr.dst_addr)->sin_addr.s_addr,
1080 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1081 route.addr.dst_addr)->sin_port),
1082 newxprt->sc_max_sge,
1083 newxprt->sc_sq_depth,
1084 newxprt->sc_max_requests,
1085 newxprt->sc_ord);
1087 return &newxprt->sc_xprt;
1089 errout:
1090 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1091 /* Take a reference in case the DTO handler runs */
1092 svc_xprt_get(&newxprt->sc_xprt);
1093 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1094 ib_destroy_qp(newxprt->sc_qp);
1095 rdma_destroy_id(newxprt->sc_cm_id);
1096 /* This call to put will destroy the transport */
1097 svc_xprt_put(&newxprt->sc_xprt);
1098 return NULL;
1101 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1106 * When connected, an svc_xprt has at least two references:
1108 * - A reference held by the cm_id between the ESTABLISHED and
1109 * DISCONNECTED events. If the remote peer disconnected first, this
1110 * reference could be gone.
1112 * - A reference held by the svc_recv code that called this function
1113 * as part of close processing.
1115 * At a minimum one references should still be held.
1117 static void svc_rdma_detach(struct svc_xprt *xprt)
1119 struct svcxprt_rdma *rdma =
1120 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1121 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1123 /* Disconnect and flush posted WQE */
1124 rdma_disconnect(rdma->sc_cm_id);
1127 static void __svc_rdma_free(struct work_struct *work)
1129 struct svcxprt_rdma *rdma =
1130 container_of(work, struct svcxprt_rdma, sc_work);
1131 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1133 /* We should only be called from kref_put */
1134 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1137 * Destroy queued, but not processed read completions. Note
1138 * that this cleanup has to be done before destroying the
1139 * cm_id because the device ptr is needed to unmap the dma in
1140 * svc_rdma_put_context.
1142 while (!list_empty(&rdma->sc_read_complete_q)) {
1143 struct svc_rdma_op_ctxt *ctxt;
1144 ctxt = list_entry(rdma->sc_read_complete_q.next,
1145 struct svc_rdma_op_ctxt,
1146 dto_q);
1147 list_del_init(&ctxt->dto_q);
1148 svc_rdma_put_context(ctxt, 1);
1151 /* Destroy queued, but not processed recv completions */
1152 while (!list_empty(&rdma->sc_rq_dto_q)) {
1153 struct svc_rdma_op_ctxt *ctxt;
1154 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1155 struct svc_rdma_op_ctxt,
1156 dto_q);
1157 list_del_init(&ctxt->dto_q);
1158 svc_rdma_put_context(ctxt, 1);
1161 /* Warn if we leaked a resource or under-referenced */
1162 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1163 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1165 /* De-allocate fastreg mr */
1166 rdma_dealloc_frmr_q(rdma);
1168 /* Destroy the QP if present (not a listener) */
1169 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1170 ib_destroy_qp(rdma->sc_qp);
1172 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1173 ib_destroy_cq(rdma->sc_sq_cq);
1175 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1176 ib_destroy_cq(rdma->sc_rq_cq);
1178 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1179 ib_dereg_mr(rdma->sc_phys_mr);
1181 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1182 ib_dealloc_pd(rdma->sc_pd);
1184 /* Destroy the CM ID */
1185 rdma_destroy_id(rdma->sc_cm_id);
1187 kfree(rdma);
1190 static void svc_rdma_free(struct svc_xprt *xprt)
1192 struct svcxprt_rdma *rdma =
1193 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1194 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1195 queue_work(svc_rdma_wq, &rdma->sc_work);
1198 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1200 struct svcxprt_rdma *rdma =
1201 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1204 * If there are fewer SQ WR available than required to send a
1205 * simple response, return false.
1207 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1208 return 0;
1211 * ...or there are already waiters on the SQ,
1212 * return false.
1214 if (waitqueue_active(&rdma->sc_send_wait))
1215 return 0;
1217 /* Otherwise return true. */
1218 return 1;
1222 * Attempt to register the kvec representing the RPC memory with the
1223 * device.
1225 * Returns:
1226 * NULL : The device does not support fastreg or there were no more
1227 * fastreg mr.
1228 * frmr : The kvec register request was successfully posted.
1229 * <0 : An error was encountered attempting to register the kvec.
1231 int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1232 struct svc_rdma_fastreg_mr *frmr)
1234 struct ib_send_wr fastreg_wr;
1235 u8 key;
1237 /* Bump the key */
1238 key = (u8)(frmr->mr->lkey & 0x000000FF);
1239 ib_update_fast_reg_key(frmr->mr, ++key);
1241 /* Prepare FASTREG WR */
1242 memset(&fastreg_wr, 0, sizeof fastreg_wr);
1243 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1244 fastreg_wr.send_flags = IB_SEND_SIGNALED;
1245 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1246 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1247 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1248 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1249 fastreg_wr.wr.fast_reg.length = frmr->map_len;
1250 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1251 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1252 return svc_rdma_send(xprt, &fastreg_wr);
1255 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1257 struct ib_send_wr *bad_wr, *n_wr;
1258 int wr_count;
1259 int i;
1260 int ret;
1262 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1263 return -ENOTCONN;
1265 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1266 wr_count = 1;
1267 for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1268 wr_count++;
1270 /* If the SQ is full, wait until an SQ entry is available */
1271 while (1) {
1272 spin_lock_bh(&xprt->sc_lock);
1273 if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
1274 spin_unlock_bh(&xprt->sc_lock);
1275 atomic_inc(&rdma_stat_sq_starve);
1277 /* See if we can opportunistically reap SQ WR to make room */
1278 sq_cq_reap(xprt);
1280 /* Wait until SQ WR available if SQ still full */
1281 wait_event(xprt->sc_send_wait,
1282 atomic_read(&xprt->sc_sq_count) <
1283 xprt->sc_sq_depth);
1284 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1285 return -ENOTCONN;
1286 continue;
1288 /* Take a transport ref for each WR posted */
1289 for (i = 0; i < wr_count; i++)
1290 svc_xprt_get(&xprt->sc_xprt);
1292 /* Bump used SQ WR count and post */
1293 atomic_add(wr_count, &xprt->sc_sq_count);
1294 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1295 if (ret) {
1296 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1297 atomic_sub(wr_count, &xprt->sc_sq_count);
1298 for (i = 0; i < wr_count; i ++)
1299 svc_xprt_put(&xprt->sc_xprt);
1300 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1301 "sc_sq_count=%d, sc_sq_depth=%d\n",
1302 ret, atomic_read(&xprt->sc_sq_count),
1303 xprt->sc_sq_depth);
1305 spin_unlock_bh(&xprt->sc_lock);
1306 if (ret)
1307 wake_up(&xprt->sc_send_wait);
1308 break;
1310 return ret;
1313 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1314 enum rpcrdma_errcode err)
1316 struct ib_send_wr err_wr;
1317 struct page *p;
1318 struct svc_rdma_op_ctxt *ctxt;
1319 u32 *va;
1320 int length;
1321 int ret;
1323 p = svc_rdma_get_page();
1324 va = page_address(p);
1326 /* XDR encode error */
1327 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1329 ctxt = svc_rdma_get_context(xprt);
1330 ctxt->direction = DMA_FROM_DEVICE;
1331 ctxt->count = 1;
1332 ctxt->pages[0] = p;
1334 /* Prepare SGE for local address */
1335 ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
1336 p, 0, length, DMA_FROM_DEVICE);
1337 if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
1338 put_page(p);
1339 svc_rdma_put_context(ctxt, 1);
1340 return;
1342 atomic_inc(&xprt->sc_dma_used);
1343 ctxt->sge[0].lkey = xprt->sc_dma_lkey;
1344 ctxt->sge[0].length = length;
1346 /* Prepare SEND WR */
1347 memset(&err_wr, 0, sizeof err_wr);
1348 ctxt->wr_op = IB_WR_SEND;
1349 err_wr.wr_id = (unsigned long)ctxt;
1350 err_wr.sg_list = ctxt->sge;
1351 err_wr.num_sge = 1;
1352 err_wr.opcode = IB_WR_SEND;
1353 err_wr.send_flags = IB_SEND_SIGNALED;
1355 /* Post It */
1356 ret = svc_rdma_send(xprt, &err_wr);
1357 if (ret) {
1358 dprintk("svcrdma: Error %d posting send for protocol error\n",
1359 ret);
1360 svc_rdma_unmap_dma(ctxt);
1361 svc_rdma_put_context(ctxt, 1);