mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / net / sunrpc / xprtrdma / svc_rdma_transport.c
blobed36cb52cd8678422e45a773ecdafac6c3a1447e
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/interrupt.h>
46 #include <linux/sched.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/workqueue.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/rdma_cm.h>
52 #include <linux/sunrpc/svc_rdma.h>
53 #include <linux/export.h>
54 #include "xprt_rdma.h"
56 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
58 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
59 struct net *net,
60 struct sockaddr *sa, int salen,
61 int flags);
62 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
63 static void svc_rdma_release_rqst(struct svc_rqst *);
64 static void dto_tasklet_func(unsigned long data);
65 static void svc_rdma_detach(struct svc_xprt *xprt);
66 static void svc_rdma_free(struct svc_xprt *xprt);
67 static int svc_rdma_has_wspace(struct svc_xprt *xprt);
68 static void rq_cq_reap(struct svcxprt_rdma *xprt);
69 static void sq_cq_reap(struct svcxprt_rdma *xprt);
71 static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
72 static DEFINE_SPINLOCK(dto_lock);
73 static LIST_HEAD(dto_xprt_q);
75 static struct svc_xprt_ops svc_rdma_ops = {
76 .xpo_create = svc_rdma_create,
77 .xpo_recvfrom = svc_rdma_recvfrom,
78 .xpo_sendto = svc_rdma_sendto,
79 .xpo_release_rqst = svc_rdma_release_rqst,
80 .xpo_detach = svc_rdma_detach,
81 .xpo_free = svc_rdma_free,
82 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
83 .xpo_has_wspace = svc_rdma_has_wspace,
84 .xpo_accept = svc_rdma_accept,
87 struct svc_xprt_class svc_rdma_class = {
88 .xcl_name = "rdma",
89 .xcl_owner = THIS_MODULE,
90 .xcl_ops = &svc_rdma_ops,
91 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
92 .xcl_ident = XPRT_TRANSPORT_RDMA,
95 struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
97 struct svc_rdma_op_ctxt *ctxt;
99 while (1) {
100 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
101 if (ctxt)
102 break;
103 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
105 ctxt->xprt = xprt;
106 INIT_LIST_HEAD(&ctxt->dto_q);
107 ctxt->count = 0;
108 ctxt->frmr = NULL;
109 atomic_inc(&xprt->sc_ctxt_used);
110 return ctxt;
113 void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
115 struct svcxprt_rdma *xprt = ctxt->xprt;
116 int i;
117 for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
119 * Unmap the DMA addr in the SGE if the lkey matches
120 * the sc_dma_lkey, otherwise, ignore it since it is
121 * an FRMR lkey and will be unmapped later when the
122 * last WR that uses it completes.
124 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
125 atomic_dec(&xprt->sc_dma_used);
126 ib_dma_unmap_page(xprt->sc_cm_id->device,
127 ctxt->sge[i].addr,
128 ctxt->sge[i].length,
129 ctxt->direction);
134 void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
136 struct svcxprt_rdma *xprt;
137 int i;
139 BUG_ON(!ctxt);
140 xprt = ctxt->xprt;
141 if (free_pages)
142 for (i = 0; i < ctxt->count; i++)
143 put_page(ctxt->pages[i]);
145 kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
146 atomic_dec(&xprt->sc_ctxt_used);
150 * Temporary NFS req mappings are shared across all transport
151 * instances. These are short lived and should be bounded by the number
152 * of concurrent server threads * depth of the SQ.
154 struct svc_rdma_req_map *svc_rdma_get_req_map(void)
156 struct svc_rdma_req_map *map;
157 while (1) {
158 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
159 if (map)
160 break;
161 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
163 map->count = 0;
164 map->frmr = NULL;
165 return map;
168 void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
170 kmem_cache_free(svc_rdma_map_cachep, map);
173 /* ib_cq event handler */
174 static void cq_event_handler(struct ib_event *event, void *context)
176 struct svc_xprt *xprt = context;
177 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
178 event->event, context);
179 set_bit(XPT_CLOSE, &xprt->xpt_flags);
182 /* QP event handler */
183 static void qp_event_handler(struct ib_event *event, void *context)
185 struct svc_xprt *xprt = context;
187 switch (event->event) {
188 /* These are considered benign events */
189 case IB_EVENT_PATH_MIG:
190 case IB_EVENT_COMM_EST:
191 case IB_EVENT_SQ_DRAINED:
192 case IB_EVENT_QP_LAST_WQE_REACHED:
193 dprintk("svcrdma: QP event %d received for QP=%p\n",
194 event->event, event->element.qp);
195 break;
196 /* These are considered fatal events */
197 case IB_EVENT_PATH_MIG_ERR:
198 case IB_EVENT_QP_FATAL:
199 case IB_EVENT_QP_REQ_ERR:
200 case IB_EVENT_QP_ACCESS_ERR:
201 case IB_EVENT_DEVICE_FATAL:
202 default:
203 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
204 "closing transport\n",
205 event->event, event->element.qp);
206 set_bit(XPT_CLOSE, &xprt->xpt_flags);
207 break;
212 * Data Transfer Operation Tasklet
214 * Walks a list of transports with I/O pending, removing entries as
215 * they are added to the server's I/O pending list. Two bits indicate
216 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
217 * spinlock that serializes access to the transport list with the RQ
218 * and SQ interrupt handlers.
220 static void dto_tasklet_func(unsigned long data)
222 struct svcxprt_rdma *xprt;
223 unsigned long flags;
225 spin_lock_irqsave(&dto_lock, flags);
226 while (!list_empty(&dto_xprt_q)) {
227 xprt = list_entry(dto_xprt_q.next,
228 struct svcxprt_rdma, sc_dto_q);
229 list_del_init(&xprt->sc_dto_q);
230 spin_unlock_irqrestore(&dto_lock, flags);
232 rq_cq_reap(xprt);
233 sq_cq_reap(xprt);
235 svc_xprt_put(&xprt->sc_xprt);
236 spin_lock_irqsave(&dto_lock, flags);
238 spin_unlock_irqrestore(&dto_lock, flags);
242 * Receive Queue Completion Handler
244 * Since an RQ completion handler is called on interrupt context, we
245 * need to defer the handling of the I/O to a tasklet
247 static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
249 struct svcxprt_rdma *xprt = cq_context;
250 unsigned long flags;
252 /* Guard against unconditional flush call for destroyed QP */
253 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
254 return;
257 * Set the bit regardless of whether or not it's on the list
258 * because it may be on the list already due to an SQ
259 * completion.
261 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
264 * If this transport is not already on the DTO transport queue,
265 * add it
267 spin_lock_irqsave(&dto_lock, flags);
268 if (list_empty(&xprt->sc_dto_q)) {
269 svc_xprt_get(&xprt->sc_xprt);
270 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
272 spin_unlock_irqrestore(&dto_lock, flags);
274 /* Tasklet does all the work to avoid irqsave locks. */
275 tasklet_schedule(&dto_tasklet);
279 * rq_cq_reap - Process the RQ CQ.
281 * Take all completing WC off the CQE and enqueue the associated DTO
282 * context on the dto_q for the transport.
284 * Note that caller must hold a transport reference.
286 static void rq_cq_reap(struct svcxprt_rdma *xprt)
288 int ret;
289 struct ib_wc wc;
290 struct svc_rdma_op_ctxt *ctxt = NULL;
292 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
293 return;
295 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
296 atomic_inc(&rdma_stat_rq_poll);
298 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
299 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
300 ctxt->wc_status = wc.status;
301 ctxt->byte_len = wc.byte_len;
302 svc_rdma_unmap_dma(ctxt);
303 if (wc.status != IB_WC_SUCCESS) {
304 /* Close the transport */
305 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
306 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
307 svc_rdma_put_context(ctxt, 1);
308 svc_xprt_put(&xprt->sc_xprt);
309 continue;
311 spin_lock_bh(&xprt->sc_rq_dto_lock);
312 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
313 spin_unlock_bh(&xprt->sc_rq_dto_lock);
314 svc_xprt_put(&xprt->sc_xprt);
317 if (ctxt)
318 atomic_inc(&rdma_stat_rq_prod);
320 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
322 * If data arrived before established event,
323 * don't enqueue. This defers RPC I/O until the
324 * RDMA connection is complete.
326 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
327 svc_xprt_enqueue(&xprt->sc_xprt);
331 * Process a completion context
333 static void process_context(struct svcxprt_rdma *xprt,
334 struct svc_rdma_op_ctxt *ctxt)
336 svc_rdma_unmap_dma(ctxt);
338 switch (ctxt->wr_op) {
339 case IB_WR_SEND:
340 if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
341 svc_rdma_put_frmr(xprt, ctxt->frmr);
342 svc_rdma_put_context(ctxt, 1);
343 break;
345 case IB_WR_RDMA_WRITE:
346 svc_rdma_put_context(ctxt, 0);
347 break;
349 case IB_WR_RDMA_READ:
350 case IB_WR_RDMA_READ_WITH_INV:
351 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
352 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
353 BUG_ON(!read_hdr);
354 if (test_bit(RDMACTXT_F_FAST_UNREG, &ctxt->flags))
355 svc_rdma_put_frmr(xprt, ctxt->frmr);
356 spin_lock_bh(&xprt->sc_rq_dto_lock);
357 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
358 list_add_tail(&read_hdr->dto_q,
359 &xprt->sc_read_complete_q);
360 spin_unlock_bh(&xprt->sc_rq_dto_lock);
361 svc_xprt_enqueue(&xprt->sc_xprt);
363 svc_rdma_put_context(ctxt, 0);
364 break;
366 default:
367 printk(KERN_ERR "svcrdma: unexpected completion type, "
368 "opcode=%d\n",
369 ctxt->wr_op);
370 break;
375 * Send Queue Completion Handler - potentially called on interrupt context.
377 * Note that caller must hold a transport reference.
379 static void sq_cq_reap(struct svcxprt_rdma *xprt)
381 struct svc_rdma_op_ctxt *ctxt = NULL;
382 struct ib_wc wc;
383 struct ib_cq *cq = xprt->sc_sq_cq;
384 int ret;
386 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
387 return;
389 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
390 atomic_inc(&rdma_stat_sq_poll);
391 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
392 if (wc.status != IB_WC_SUCCESS)
393 /* Close the transport */
394 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
396 /* Decrement used SQ WR count */
397 atomic_dec(&xprt->sc_sq_count);
398 wake_up(&xprt->sc_send_wait);
400 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
401 if (ctxt)
402 process_context(xprt, ctxt);
404 svc_xprt_put(&xprt->sc_xprt);
407 if (ctxt)
408 atomic_inc(&rdma_stat_sq_prod);
411 static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
413 struct svcxprt_rdma *xprt = cq_context;
414 unsigned long flags;
416 /* Guard against unconditional flush call for destroyed QP */
417 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
418 return;
421 * Set the bit regardless of whether or not it's on the list
422 * because it may be on the list already due to an RQ
423 * completion.
425 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
428 * If this transport is not already on the DTO transport queue,
429 * add it
431 spin_lock_irqsave(&dto_lock, flags);
432 if (list_empty(&xprt->sc_dto_q)) {
433 svc_xprt_get(&xprt->sc_xprt);
434 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
436 spin_unlock_irqrestore(&dto_lock, flags);
438 /* Tasklet does all the work to avoid irqsave locks. */
439 tasklet_schedule(&dto_tasklet);
442 static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
443 int listener)
445 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
447 if (!cma_xprt)
448 return NULL;
449 svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
450 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
451 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
452 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
453 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
454 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
455 init_waitqueue_head(&cma_xprt->sc_send_wait);
457 spin_lock_init(&cma_xprt->sc_lock);
458 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
459 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
461 cma_xprt->sc_ord = svcrdma_ord;
463 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
464 cma_xprt->sc_max_requests = svcrdma_max_requests;
465 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
466 atomic_set(&cma_xprt->sc_sq_count, 0);
467 atomic_set(&cma_xprt->sc_ctxt_used, 0);
469 if (listener)
470 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
472 return cma_xprt;
475 struct page *svc_rdma_get_page(void)
477 struct page *page;
479 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
480 /* If we can't get memory, wait a bit and try again */
481 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
482 "jiffies.\n");
483 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
485 return page;
488 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
490 struct ib_recv_wr recv_wr, *bad_recv_wr;
491 struct svc_rdma_op_ctxt *ctxt;
492 struct page *page;
493 dma_addr_t pa;
494 int sge_no;
495 int buflen;
496 int ret;
498 ctxt = svc_rdma_get_context(xprt);
499 buflen = 0;
500 ctxt->direction = DMA_FROM_DEVICE;
501 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
502 BUG_ON(sge_no >= xprt->sc_max_sge);
503 page = svc_rdma_get_page();
504 ctxt->pages[sge_no] = page;
505 pa = ib_dma_map_page(xprt->sc_cm_id->device,
506 page, 0, PAGE_SIZE,
507 DMA_FROM_DEVICE);
508 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
509 goto err_put_ctxt;
510 atomic_inc(&xprt->sc_dma_used);
511 ctxt->sge[sge_no].addr = pa;
512 ctxt->sge[sge_no].length = PAGE_SIZE;
513 ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
514 ctxt->count = sge_no + 1;
515 buflen += PAGE_SIZE;
517 recv_wr.next = NULL;
518 recv_wr.sg_list = &ctxt->sge[0];
519 recv_wr.num_sge = ctxt->count;
520 recv_wr.wr_id = (u64)(unsigned long)ctxt;
522 svc_xprt_get(&xprt->sc_xprt);
523 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
524 if (ret) {
525 svc_rdma_unmap_dma(ctxt);
526 svc_rdma_put_context(ctxt, 1);
527 svc_xprt_put(&xprt->sc_xprt);
529 return ret;
531 err_put_ctxt:
532 svc_rdma_unmap_dma(ctxt);
533 svc_rdma_put_context(ctxt, 1);
534 return -ENOMEM;
538 * This function handles the CONNECT_REQUEST event on a listening
539 * endpoint. It is passed the cma_id for the _new_ connection. The context in
540 * this cma_id is inherited from the listening cma_id and is the svc_xprt
541 * structure for the listening endpoint.
543 * This function creates a new xprt for the new connection and enqueues it on
544 * the accept queue for the listent xprt. When the listen thread is kicked, it
545 * will call the recvfrom method on the listen xprt which will accept the new
546 * connection.
548 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
550 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
551 struct svcxprt_rdma *newxprt;
552 struct sockaddr *sa;
554 /* Create a new transport */
555 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
556 if (!newxprt) {
557 dprintk("svcrdma: failed to create new transport\n");
558 return;
560 newxprt->sc_cm_id = new_cma_id;
561 new_cma_id->context = newxprt;
562 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
563 newxprt, newxprt->sc_cm_id, listen_xprt);
565 /* Save client advertised inbound read limit for use later in accept. */
566 newxprt->sc_ord = client_ird;
568 /* Set the local and remote addresses in the transport */
569 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
570 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
571 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
572 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
575 * Enqueue the new transport on the accept queue of the listening
576 * transport
578 spin_lock_bh(&listen_xprt->sc_lock);
579 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
580 spin_unlock_bh(&listen_xprt->sc_lock);
582 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
583 svc_xprt_enqueue(&listen_xprt->sc_xprt);
587 * Handles events generated on the listening endpoint. These events will be
588 * either be incoming connect requests or adapter removal events.
590 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
591 struct rdma_cm_event *event)
593 struct svcxprt_rdma *xprt = cma_id->context;
594 int ret = 0;
596 switch (event->event) {
597 case RDMA_CM_EVENT_CONNECT_REQUEST:
598 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
599 "event=%d\n", cma_id, cma_id->context, event->event);
600 handle_connect_req(cma_id,
601 event->param.conn.initiator_depth);
602 break;
604 case RDMA_CM_EVENT_ESTABLISHED:
605 /* Accept complete */
606 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
607 "cm_id=%p\n", xprt, cma_id);
608 break;
610 case RDMA_CM_EVENT_DEVICE_REMOVAL:
611 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
612 xprt, cma_id);
613 if (xprt)
614 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
615 break;
617 default:
618 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
619 "event=%d\n", cma_id, event->event);
620 break;
623 return ret;
626 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
627 struct rdma_cm_event *event)
629 struct svc_xprt *xprt = cma_id->context;
630 struct svcxprt_rdma *rdma =
631 container_of(xprt, struct svcxprt_rdma, sc_xprt);
632 switch (event->event) {
633 case RDMA_CM_EVENT_ESTABLISHED:
634 /* Accept complete */
635 svc_xprt_get(xprt);
636 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
637 "cm_id=%p\n", xprt, cma_id);
638 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
639 svc_xprt_enqueue(xprt);
640 break;
641 case RDMA_CM_EVENT_DISCONNECTED:
642 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
643 xprt, cma_id);
644 if (xprt) {
645 set_bit(XPT_CLOSE, &xprt->xpt_flags);
646 svc_xprt_enqueue(xprt);
647 svc_xprt_put(xprt);
649 break;
650 case RDMA_CM_EVENT_DEVICE_REMOVAL:
651 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
652 "event=%d\n", cma_id, xprt, event->event);
653 if (xprt) {
654 set_bit(XPT_CLOSE, &xprt->xpt_flags);
655 svc_xprt_enqueue(xprt);
657 break;
658 default:
659 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
660 "event=%d\n", cma_id, event->event);
661 break;
663 return 0;
667 * Create a listening RDMA service endpoint.
669 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
670 struct net *net,
671 struct sockaddr *sa, int salen,
672 int flags)
674 struct rdma_cm_id *listen_id;
675 struct svcxprt_rdma *cma_xprt;
676 struct svc_xprt *xprt;
677 int ret;
679 dprintk("svcrdma: Creating RDMA socket\n");
680 if (sa->sa_family != AF_INET) {
681 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
682 return ERR_PTR(-EAFNOSUPPORT);
684 cma_xprt = rdma_create_xprt(serv, 1);
685 if (!cma_xprt)
686 return ERR_PTR(-ENOMEM);
687 xprt = &cma_xprt->sc_xprt;
689 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
690 IB_QPT_RC);
691 if (IS_ERR(listen_id)) {
692 ret = PTR_ERR(listen_id);
693 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
694 goto err0;
697 ret = rdma_bind_addr(listen_id, sa);
698 if (ret) {
699 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
700 goto err1;
702 cma_xprt->sc_cm_id = listen_id;
704 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
705 if (ret) {
706 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
707 goto err1;
711 * We need to use the address from the cm_id in case the
712 * caller specified 0 for the port number.
714 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
715 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
717 return &cma_xprt->sc_xprt;
719 err1:
720 rdma_destroy_id(listen_id);
721 err0:
722 kfree(cma_xprt);
723 return ERR_PTR(ret);
726 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
728 struct ib_mr *mr;
729 struct ib_fast_reg_page_list *pl;
730 struct svc_rdma_fastreg_mr *frmr;
732 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
733 if (!frmr)
734 goto err;
736 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
737 if (IS_ERR(mr))
738 goto err_free_frmr;
740 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
741 RPCSVC_MAXPAGES);
742 if (IS_ERR(pl))
743 goto err_free_mr;
745 frmr->mr = mr;
746 frmr->page_list = pl;
747 INIT_LIST_HEAD(&frmr->frmr_list);
748 return frmr;
750 err_free_mr:
751 ib_dereg_mr(mr);
752 err_free_frmr:
753 kfree(frmr);
754 err:
755 return ERR_PTR(-ENOMEM);
758 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
760 struct svc_rdma_fastreg_mr *frmr;
762 while (!list_empty(&xprt->sc_frmr_q)) {
763 frmr = list_entry(xprt->sc_frmr_q.next,
764 struct svc_rdma_fastreg_mr, frmr_list);
765 list_del_init(&frmr->frmr_list);
766 ib_dereg_mr(frmr->mr);
767 ib_free_fast_reg_page_list(frmr->page_list);
768 kfree(frmr);
772 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
774 struct svc_rdma_fastreg_mr *frmr = NULL;
776 spin_lock_bh(&rdma->sc_frmr_q_lock);
777 if (!list_empty(&rdma->sc_frmr_q)) {
778 frmr = list_entry(rdma->sc_frmr_q.next,
779 struct svc_rdma_fastreg_mr, frmr_list);
780 list_del_init(&frmr->frmr_list);
781 frmr->map_len = 0;
782 frmr->page_list_len = 0;
784 spin_unlock_bh(&rdma->sc_frmr_q_lock);
785 if (frmr)
786 return frmr;
788 return rdma_alloc_frmr(rdma);
791 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
792 struct svc_rdma_fastreg_mr *frmr)
794 int page_no;
795 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
796 dma_addr_t addr = frmr->page_list->page_list[page_no];
797 if (ib_dma_mapping_error(frmr->mr->device, addr))
798 continue;
799 atomic_dec(&xprt->sc_dma_used);
800 ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
801 frmr->direction);
805 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
806 struct svc_rdma_fastreg_mr *frmr)
808 if (frmr) {
809 frmr_unmap_dma(rdma, frmr);
810 spin_lock_bh(&rdma->sc_frmr_q_lock);
811 BUG_ON(!list_empty(&frmr->frmr_list));
812 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
813 spin_unlock_bh(&rdma->sc_frmr_q_lock);
818 * This is the xpo_recvfrom function for listening endpoints. Its
819 * purpose is to accept incoming connections. The CMA callback handler
820 * has already created a new transport and attached it to the new CMA
821 * ID.
823 * There is a queue of pending connections hung on the listening
824 * transport. This queue contains the new svc_xprt structure. This
825 * function takes svc_xprt structures off the accept_q and completes
826 * the connection.
828 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
830 struct svcxprt_rdma *listen_rdma;
831 struct svcxprt_rdma *newxprt = NULL;
832 struct rdma_conn_param conn_param;
833 struct ib_qp_init_attr qp_attr;
834 struct ib_device_attr devattr;
835 int uninitialized_var(dma_mr_acc);
836 int need_dma_mr;
837 int ret;
838 int i;
840 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
841 clear_bit(XPT_CONN, &xprt->xpt_flags);
842 /* Get the next entry off the accept list */
843 spin_lock_bh(&listen_rdma->sc_lock);
844 if (!list_empty(&listen_rdma->sc_accept_q)) {
845 newxprt = list_entry(listen_rdma->sc_accept_q.next,
846 struct svcxprt_rdma, sc_accept_q);
847 list_del_init(&newxprt->sc_accept_q);
849 if (!list_empty(&listen_rdma->sc_accept_q))
850 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
851 spin_unlock_bh(&listen_rdma->sc_lock);
852 if (!newxprt)
853 return NULL;
855 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
856 newxprt, newxprt->sc_cm_id);
858 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
859 if (ret) {
860 dprintk("svcrdma: could not query device attributes on "
861 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
862 goto errout;
865 /* Qualify the transport resource defaults with the
866 * capabilities of this particular device */
867 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
868 (size_t)RPCSVC_MAXPAGES);
869 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
870 (size_t)svcrdma_max_requests);
871 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
874 * Limit ORD based on client limit, local device limit, and
875 * configured svcrdma limit.
877 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
878 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
880 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
881 if (IS_ERR(newxprt->sc_pd)) {
882 dprintk("svcrdma: error creating PD for connect request\n");
883 goto errout;
885 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
886 sq_comp_handler,
887 cq_event_handler,
888 newxprt,
889 newxprt->sc_sq_depth,
891 if (IS_ERR(newxprt->sc_sq_cq)) {
892 dprintk("svcrdma: error creating SQ CQ for connect request\n");
893 goto errout;
895 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
896 rq_comp_handler,
897 cq_event_handler,
898 newxprt,
899 newxprt->sc_max_requests,
901 if (IS_ERR(newxprt->sc_rq_cq)) {
902 dprintk("svcrdma: error creating RQ CQ for connect request\n");
903 goto errout;
906 memset(&qp_attr, 0, sizeof qp_attr);
907 qp_attr.event_handler = qp_event_handler;
908 qp_attr.qp_context = &newxprt->sc_xprt;
909 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
910 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
911 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
912 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
913 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
914 qp_attr.qp_type = IB_QPT_RC;
915 qp_attr.send_cq = newxprt->sc_sq_cq;
916 qp_attr.recv_cq = newxprt->sc_rq_cq;
917 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
918 " cm_id->device=%p, sc_pd->device=%p\n"
919 " cap.max_send_wr = %d\n"
920 " cap.max_recv_wr = %d\n"
921 " cap.max_send_sge = %d\n"
922 " cap.max_recv_sge = %d\n",
923 newxprt->sc_cm_id, newxprt->sc_pd,
924 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
925 qp_attr.cap.max_send_wr,
926 qp_attr.cap.max_recv_wr,
927 qp_attr.cap.max_send_sge,
928 qp_attr.cap.max_recv_sge);
930 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
931 if (ret) {
933 * XXX: This is a hack. We need a xx_request_qp interface
934 * that will adjust the qp_attr's with a best-effort
935 * number
937 qp_attr.cap.max_send_sge -= 2;
938 qp_attr.cap.max_recv_sge -= 2;
939 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
940 &qp_attr);
941 if (ret) {
942 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
943 goto errout;
945 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
946 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
947 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
948 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
950 newxprt->sc_qp = newxprt->sc_cm_id->qp;
953 * Use the most secure set of MR resources based on the
954 * transport type and available memory management features in
955 * the device. Here's the table implemented below:
957 * Fast Global DMA Remote WR
958 * Reg LKEY MR Access
959 * Sup'd Sup'd Needed Needed
961 * IWARP N N Y Y
962 * N Y Y Y
963 * Y N Y N
964 * Y Y N -
966 * IB N N Y N
967 * N Y N -
968 * Y N Y N
969 * Y Y N -
971 * NB: iWARP requires remote write access for the data sink
972 * of an RDMA_READ. IB does not.
974 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
975 newxprt->sc_frmr_pg_list_len =
976 devattr.max_fast_reg_page_list_len;
977 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
981 * Determine if a DMA MR is required and if so, what privs are required
983 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
984 case RDMA_TRANSPORT_IWARP:
985 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
986 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
987 need_dma_mr = 1;
988 dma_mr_acc =
989 (IB_ACCESS_LOCAL_WRITE |
990 IB_ACCESS_REMOTE_WRITE);
991 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
992 need_dma_mr = 1;
993 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
994 } else
995 need_dma_mr = 0;
996 break;
997 case RDMA_TRANSPORT_IB:
998 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
999 need_dma_mr = 1;
1000 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1001 } else
1002 need_dma_mr = 0;
1003 break;
1004 default:
1005 goto errout;
1008 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1009 if (need_dma_mr) {
1010 /* Register all of physical memory */
1011 newxprt->sc_phys_mr =
1012 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1013 if (IS_ERR(newxprt->sc_phys_mr)) {
1014 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1015 ret);
1016 goto errout;
1018 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1019 } else
1020 newxprt->sc_dma_lkey =
1021 newxprt->sc_cm_id->device->local_dma_lkey;
1023 /* Post receive buffers */
1024 for (i = 0; i < newxprt->sc_max_requests; i++) {
1025 ret = svc_rdma_post_recv(newxprt);
1026 if (ret) {
1027 dprintk("svcrdma: failure posting receive buffers\n");
1028 goto errout;
1032 /* Swap out the handler */
1033 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1036 * Arm the CQs for the SQ and RQ before accepting so we can't
1037 * miss the first message
1039 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1040 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1042 /* Accept Connection */
1043 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1044 memset(&conn_param, 0, sizeof conn_param);
1045 conn_param.responder_resources = 0;
1046 conn_param.initiator_depth = newxprt->sc_ord;
1047 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1048 if (ret) {
1049 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1050 ret);
1051 goto errout;
1054 dprintk("svcrdma: new connection %p accepted with the following "
1055 "attributes:\n"
1056 " local_ip : %pI4\n"
1057 " local_port : %d\n"
1058 " remote_ip : %pI4\n"
1059 " remote_port : %d\n"
1060 " max_sge : %d\n"
1061 " sq_depth : %d\n"
1062 " max_requests : %d\n"
1063 " ord : %d\n",
1064 newxprt,
1065 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1066 route.addr.src_addr)->sin_addr.s_addr,
1067 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1068 route.addr.src_addr)->sin_port),
1069 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1070 route.addr.dst_addr)->sin_addr.s_addr,
1071 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1072 route.addr.dst_addr)->sin_port),
1073 newxprt->sc_max_sge,
1074 newxprt->sc_sq_depth,
1075 newxprt->sc_max_requests,
1076 newxprt->sc_ord);
1078 return &newxprt->sc_xprt;
1080 errout:
1081 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1082 /* Take a reference in case the DTO handler runs */
1083 svc_xprt_get(&newxprt->sc_xprt);
1084 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1085 ib_destroy_qp(newxprt->sc_qp);
1086 rdma_destroy_id(newxprt->sc_cm_id);
1087 /* This call to put will destroy the transport */
1088 svc_xprt_put(&newxprt->sc_xprt);
1089 return NULL;
1092 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1097 * When connected, an svc_xprt has at least two references:
1099 * - A reference held by the cm_id between the ESTABLISHED and
1100 * DISCONNECTED events. If the remote peer disconnected first, this
1101 * reference could be gone.
1103 * - A reference held by the svc_recv code that called this function
1104 * as part of close processing.
1106 * At a minimum one references should still be held.
1108 static void svc_rdma_detach(struct svc_xprt *xprt)
1110 struct svcxprt_rdma *rdma =
1111 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1112 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1114 /* Disconnect and flush posted WQE */
1115 rdma_disconnect(rdma->sc_cm_id);
1118 static void __svc_rdma_free(struct work_struct *work)
1120 struct svcxprt_rdma *rdma =
1121 container_of(work, struct svcxprt_rdma, sc_work);
1122 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1124 /* We should only be called from kref_put */
1125 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1128 * Destroy queued, but not processed read completions. Note
1129 * that this cleanup has to be done before destroying the
1130 * cm_id because the device ptr is needed to unmap the dma in
1131 * svc_rdma_put_context.
1133 while (!list_empty(&rdma->sc_read_complete_q)) {
1134 struct svc_rdma_op_ctxt *ctxt;
1135 ctxt = list_entry(rdma->sc_read_complete_q.next,
1136 struct svc_rdma_op_ctxt,
1137 dto_q);
1138 list_del_init(&ctxt->dto_q);
1139 svc_rdma_put_context(ctxt, 1);
1142 /* Destroy queued, but not processed recv completions */
1143 while (!list_empty(&rdma->sc_rq_dto_q)) {
1144 struct svc_rdma_op_ctxt *ctxt;
1145 ctxt = list_entry(rdma->sc_rq_dto_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);
1152 /* Warn if we leaked a resource or under-referenced */
1153 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1154 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1156 /* De-allocate fastreg mr */
1157 rdma_dealloc_frmr_q(rdma);
1159 /* Destroy the QP if present (not a listener) */
1160 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1161 ib_destroy_qp(rdma->sc_qp);
1163 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1164 ib_destroy_cq(rdma->sc_sq_cq);
1166 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1167 ib_destroy_cq(rdma->sc_rq_cq);
1169 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1170 ib_dereg_mr(rdma->sc_phys_mr);
1172 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1173 ib_dealloc_pd(rdma->sc_pd);
1175 /* Destroy the CM ID */
1176 rdma_destroy_id(rdma->sc_cm_id);
1178 kfree(rdma);
1181 static void svc_rdma_free(struct svc_xprt *xprt)
1183 struct svcxprt_rdma *rdma =
1184 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1185 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1186 queue_work(svc_rdma_wq, &rdma->sc_work);
1189 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1191 struct svcxprt_rdma *rdma =
1192 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1195 * If there are fewer SQ WR available than required to send a
1196 * simple response, return false.
1198 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1199 return 0;
1202 * ...or there are already waiters on the SQ,
1203 * return false.
1205 if (waitqueue_active(&rdma->sc_send_wait))
1206 return 0;
1208 /* Otherwise return true. */
1209 return 1;
1213 * Attempt to register the kvec representing the RPC memory with the
1214 * device.
1216 * Returns:
1217 * NULL : The device does not support fastreg or there were no more
1218 * fastreg mr.
1219 * frmr : The kvec register request was successfully posted.
1220 * <0 : An error was encountered attempting to register the kvec.
1222 int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1223 struct svc_rdma_fastreg_mr *frmr)
1225 struct ib_send_wr fastreg_wr;
1226 u8 key;
1228 /* Bump the key */
1229 key = (u8)(frmr->mr->lkey & 0x000000FF);
1230 ib_update_fast_reg_key(frmr->mr, ++key);
1232 /* Prepare FASTREG WR */
1233 memset(&fastreg_wr, 0, sizeof fastreg_wr);
1234 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1235 fastreg_wr.send_flags = IB_SEND_SIGNALED;
1236 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1237 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1238 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1239 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1240 fastreg_wr.wr.fast_reg.length = frmr->map_len;
1241 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1242 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1243 return svc_rdma_send(xprt, &fastreg_wr);
1246 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1248 struct ib_send_wr *bad_wr, *n_wr;
1249 int wr_count;
1250 int i;
1251 int ret;
1253 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1254 return -ENOTCONN;
1256 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1257 wr_count = 1;
1258 for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1259 wr_count++;
1261 /* If the SQ is full, wait until an SQ entry is available */
1262 while (1) {
1263 spin_lock_bh(&xprt->sc_lock);
1264 if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
1265 spin_unlock_bh(&xprt->sc_lock);
1266 atomic_inc(&rdma_stat_sq_starve);
1268 /* See if we can opportunistically reap SQ WR to make room */
1269 sq_cq_reap(xprt);
1271 /* Wait until SQ WR available if SQ still full */
1272 wait_event(xprt->sc_send_wait,
1273 atomic_read(&xprt->sc_sq_count) <
1274 xprt->sc_sq_depth);
1275 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1276 return -ENOTCONN;
1277 continue;
1279 /* Take a transport ref for each WR posted */
1280 for (i = 0; i < wr_count; i++)
1281 svc_xprt_get(&xprt->sc_xprt);
1283 /* Bump used SQ WR count and post */
1284 atomic_add(wr_count, &xprt->sc_sq_count);
1285 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1286 if (ret) {
1287 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1288 atomic_sub(wr_count, &xprt->sc_sq_count);
1289 for (i = 0; i < wr_count; i ++)
1290 svc_xprt_put(&xprt->sc_xprt);
1291 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1292 "sc_sq_count=%d, sc_sq_depth=%d\n",
1293 ret, atomic_read(&xprt->sc_sq_count),
1294 xprt->sc_sq_depth);
1296 spin_unlock_bh(&xprt->sc_lock);
1297 if (ret)
1298 wake_up(&xprt->sc_send_wait);
1299 break;
1301 return ret;
1304 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1305 enum rpcrdma_errcode err)
1307 struct ib_send_wr err_wr;
1308 struct page *p;
1309 struct svc_rdma_op_ctxt *ctxt;
1310 u32 *va;
1311 int length;
1312 int ret;
1314 p = svc_rdma_get_page();
1315 va = page_address(p);
1317 /* XDR encode error */
1318 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1320 ctxt = svc_rdma_get_context(xprt);
1321 ctxt->direction = DMA_FROM_DEVICE;
1322 ctxt->count = 1;
1323 ctxt->pages[0] = p;
1325 /* Prepare SGE for local address */
1326 ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
1327 p, 0, length, DMA_FROM_DEVICE);
1328 if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
1329 put_page(p);
1330 svc_rdma_put_context(ctxt, 1);
1331 return;
1333 atomic_inc(&xprt->sc_dma_used);
1334 ctxt->sge[0].lkey = xprt->sc_dma_lkey;
1335 ctxt->sge[0].length = length;
1337 /* Prepare SEND WR */
1338 memset(&err_wr, 0, sizeof err_wr);
1339 ctxt->wr_op = IB_WR_SEND;
1340 err_wr.wr_id = (unsigned long)ctxt;
1341 err_wr.sg_list = ctxt->sge;
1342 err_wr.num_sge = 1;
1343 err_wr.opcode = IB_WR_SEND;
1344 err_wr.send_flags = IB_SEND_SIGNALED;
1346 /* Post It */
1347 ret = svc_rdma_send(xprt, &err_wr);
1348 if (ret) {
1349 dprintk("svcrdma: Error %d posting send for protocol error\n",
1350 ret);
1351 svc_rdma_unmap_dma(ctxt);
1352 svc_rdma_put_context(ctxt, 1);