blob: e7323fbbd348c5e30a8a99763df73c37fa6b5a26 [file] [log] [blame]
Tom Tucker377f9b22007-12-12 16:13:21 -06001/*
Steve Wise0bf48282014-05-28 15:12:01 -05002 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
Tom Tucker377f9b22007-12-12 16:13:21 -06003 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the BSD-type
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided
21 * with the distribution.
22 *
23 * Neither the name of the Network Appliance, Inc. nor the names of
24 * its contributors may be used to endorse or promote products
25 * derived from this software without specific prior written
26 * permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Author: Tom Tucker <tom@opengridcomputing.com>
41 */
42
43#include <linux/sunrpc/svc_xprt.h>
44#include <linux/sunrpc/debug.h>
45#include <linux/sunrpc/rpc_rdma.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000046#include <linux/interrupt.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040047#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Tom Tucker377f9b22007-12-12 16:13:21 -060049#include <linux/spinlock.h>
Tejun Heoa25e7582010-10-15 17:49:27 +020050#include <linux/workqueue.h>
Tom Tucker377f9b22007-12-12 16:13:21 -060051#include <rdma/ib_verbs.h>
52#include <rdma/rdma_cm.h>
53#include <linux/sunrpc/svc_rdma.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040054#include <linux/export.h>
Tom Tuckercec56c82012-02-15 11:30:00 -060055#include "xprt_rdma.h"
Tom Tucker377f9b22007-12-12 16:13:21 -060056
57#define RPCDBG_FACILITY RPCDBG_SVCXPRT
58
59static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040060 struct net *net,
Tom Tucker377f9b22007-12-12 16:13:21 -060061 struct sockaddr *sa, int salen,
62 int flags);
63static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
64static void svc_rdma_release_rqst(struct svc_rqst *);
Tom Tucker377f9b22007-12-12 16:13:21 -060065static void dto_tasklet_func(unsigned long data);
66static void svc_rdma_detach(struct svc_xprt *xprt);
67static void svc_rdma_free(struct svc_xprt *xprt);
68static int svc_rdma_has_wspace(struct svc_xprt *xprt);
Chuck Lever16e4d932014-05-19 13:40:22 -040069static int svc_rdma_secure_port(struct svc_rqst *);
Tom Tucker377f9b22007-12-12 16:13:21 -060070static void rq_cq_reap(struct svcxprt_rdma *xprt);
71static void sq_cq_reap(struct svcxprt_rdma *xprt);
72
Roel Kluin5eaa65b2008-12-10 15:18:31 -080073static DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
Tom Tucker377f9b22007-12-12 16:13:21 -060074static DEFINE_SPINLOCK(dto_lock);
75static LIST_HEAD(dto_xprt_q);
76
77static struct svc_xprt_ops svc_rdma_ops = {
78 .xpo_create = svc_rdma_create,
79 .xpo_recvfrom = svc_rdma_recvfrom,
80 .xpo_sendto = svc_rdma_sendto,
81 .xpo_release_rqst = svc_rdma_release_rqst,
82 .xpo_detach = svc_rdma_detach,
83 .xpo_free = svc_rdma_free,
84 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
85 .xpo_has_wspace = svc_rdma_has_wspace,
86 .xpo_accept = svc_rdma_accept,
Chuck Lever16e4d932014-05-19 13:40:22 -040087 .xpo_secure_port = svc_rdma_secure_port,
Tom Tucker377f9b22007-12-12 16:13:21 -060088};
89
90struct svc_xprt_class svc_rdma_class = {
91 .xcl_name = "rdma",
92 .xcl_owner = THIS_MODULE,
93 .xcl_ops = &svc_rdma_ops,
94 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
95};
96
Tom Tucker377f9b22007-12-12 16:13:21 -060097struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
98{
99 struct svc_rdma_op_ctxt *ctxt;
100
101 while (1) {
Tom Tucker89488962008-05-28 15:14:02 -0500102 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
103 if (ctxt)
104 break;
105 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
Tom Tucker377f9b22007-12-12 16:13:21 -0600106 }
Tom Tucker89488962008-05-28 15:14:02 -0500107 ctxt->xprt = xprt;
108 INIT_LIST_HEAD(&ctxt->dto_q);
109 ctxt->count = 0;
Tom Tucker64be86082008-10-06 14:45:18 -0500110 ctxt->frmr = NULL;
Tom Tucker89488962008-05-28 15:14:02 -0500111 atomic_inc(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600112 return ctxt;
113}
114
Tom Tucker146b6df2008-08-12 15:12:10 -0500115void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
Tom Tuckere6ab9142008-05-28 12:08:48 -0500116{
117 struct svcxprt_rdma *xprt = ctxt->xprt;
118 int i;
119 for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
Tom Tucker64be86082008-10-06 14:45:18 -0500120 /*
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.
125 */
126 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
127 atomic_dec(&xprt->sc_dma_used);
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500128 ib_dma_unmap_page(xprt->sc_cm_id->device,
Tom Tucker64be86082008-10-06 14:45:18 -0500129 ctxt->sge[i].addr,
130 ctxt->sge[i].length,
131 ctxt->direction);
132 }
Tom Tuckere6ab9142008-05-28 12:08:48 -0500133 }
134}
135
Tom Tucker377f9b22007-12-12 16:13:21 -0600136void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
137{
138 struct svcxprt_rdma *xprt;
139 int i;
140
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]);
146
Tom Tucker89488962008-05-28 15:14:02 -0500147 kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
Tom Tucker87407672008-04-30 20:44:39 -0500148 atomic_dec(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600149}
150
Tom Tuckerab96ddd2008-05-28 13:54:04 -0500151/*
152 * Temporary NFS req mappings are shared across all transport
153 * instances. These are short lived and should be bounded by the number
154 * of concurrent server threads * depth of the SQ.
155 */
156struct svc_rdma_req_map *svc_rdma_get_req_map(void)
157{
158 struct svc_rdma_req_map *map;
159 while (1) {
160 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
161 if (map)
162 break;
163 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
164 }
165 map->count = 0;
166 return map;
167}
168
169void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
170{
171 kmem_cache_free(svc_rdma_map_cachep, map);
172}
173
Tom Tucker377f9b22007-12-12 16:13:21 -0600174/* ib_cq event handler */
175static void cq_event_handler(struct ib_event *event, void *context)
176{
177 struct svc_xprt *xprt = context;
178 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
179 event->event, context);
180 set_bit(XPT_CLOSE, &xprt->xpt_flags);
181}
182
183/* QP event handler */
184static void qp_event_handler(struct ib_event *event, void *context)
185{
186 struct svc_xprt *xprt = context;
187
188 switch (event->event) {
189 /* These are considered benign events */
190 case IB_EVENT_PATH_MIG:
191 case IB_EVENT_COMM_EST:
192 case IB_EVENT_SQ_DRAINED:
193 case IB_EVENT_QP_LAST_WQE_REACHED:
194 dprintk("svcrdma: QP event %d received for QP=%p\n",
195 event->event, event->element.qp);
196 break;
197 /* These are considered fatal events */
198 case IB_EVENT_PATH_MIG_ERR:
199 case IB_EVENT_QP_FATAL:
200 case IB_EVENT_QP_REQ_ERR:
201 case IB_EVENT_QP_ACCESS_ERR:
202 case IB_EVENT_DEVICE_FATAL:
203 default:
204 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
205 "closing transport\n",
206 event->event, event->element.qp);
207 set_bit(XPT_CLOSE, &xprt->xpt_flags);
208 break;
209 }
210}
211
212/*
213 * Data Transfer Operation Tasklet
214 *
215 * Walks a list of transports with I/O pending, removing entries as
216 * they are added to the server's I/O pending list. Two bits indicate
217 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
218 * spinlock that serializes access to the transport list with the RQ
219 * and SQ interrupt handlers.
220 */
221static void dto_tasklet_func(unsigned long data)
222{
223 struct svcxprt_rdma *xprt;
224 unsigned long flags;
225
226 spin_lock_irqsave(&dto_lock, flags);
227 while (!list_empty(&dto_xprt_q)) {
228 xprt = list_entry(dto_xprt_q.next,
229 struct svcxprt_rdma, sc_dto_q);
230 list_del_init(&xprt->sc_dto_q);
231 spin_unlock_irqrestore(&dto_lock, flags);
232
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500233 rq_cq_reap(xprt);
234 sq_cq_reap(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600235
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400236 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600237 spin_lock_irqsave(&dto_lock, flags);
238 }
239 spin_unlock_irqrestore(&dto_lock, flags);
240}
241
242/*
243 * Receive Queue Completion Handler
244 *
245 * Since an RQ completion handler is called on interrupt context, we
246 * need to defer the handling of the I/O to a tasklet
247 */
248static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
249{
250 struct svcxprt_rdma *xprt = cq_context;
251 unsigned long flags;
252
Tom Tucker17113862008-05-01 11:13:50 -0500253 /* Guard against unconditional flush call for destroyed QP */
254 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
255 return;
256
Tom Tucker377f9b22007-12-12 16:13:21 -0600257 /*
258 * Set the bit regardless of whether or not it's on the list
259 * because it may be on the list already due to an SQ
260 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500261 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600262 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
263
264 /*
265 * If this transport is not already on the DTO transport queue,
266 * add it
267 */
268 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400269 if (list_empty(&xprt->sc_dto_q)) {
270 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600271 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400272 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600273 spin_unlock_irqrestore(&dto_lock, flags);
274
275 /* Tasklet does all the work to avoid irqsave locks. */
276 tasklet_schedule(&dto_tasklet);
277}
278
279/*
280 * rq_cq_reap - Process the RQ CQ.
281 *
282 * Take all completing WC off the CQE and enqueue the associated DTO
283 * context on the dto_q for the transport.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500284 *
285 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600286 */
287static void rq_cq_reap(struct svcxprt_rdma *xprt)
288{
289 int ret;
290 struct ib_wc wc;
291 struct svc_rdma_op_ctxt *ctxt = NULL;
292
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500293 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
294 return;
295
296 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600297 atomic_inc(&rdma_stat_rq_poll);
298
Tom Tucker377f9b22007-12-12 16:13:21 -0600299 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
300 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
301 ctxt->wc_status = wc.status;
302 ctxt->byte_len = wc.byte_len;
Tom Tuckere6ab9142008-05-28 12:08:48 -0500303 svc_rdma_unmap_dma(ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600304 if (wc.status != IB_WC_SUCCESS) {
305 /* Close the transport */
Tom Tucker0905c0f2008-05-01 10:49:03 -0500306 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600307 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
308 svc_rdma_put_context(ctxt, 1);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500309 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600310 continue;
311 }
Tom Tucker47698e02008-05-06 11:49:05 -0500312 spin_lock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600313 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
Tom Tucker47698e02008-05-06 11:49:05 -0500314 spin_unlock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500315 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600316 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600317
318 if (ctxt)
319 atomic_inc(&rdma_stat_rq_prod);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500320
321 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
322 /*
323 * If data arrived before established event,
324 * don't enqueue. This defers RPC I/O until the
325 * RDMA connection is complete.
326 */
327 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
328 svc_xprt_enqueue(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600329}
330
331/*
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200332 * Process a completion context
Tom Tuckere1183212008-10-03 15:22:18 -0500333 */
334static void process_context(struct svcxprt_rdma *xprt,
335 struct svc_rdma_op_ctxt *ctxt)
336{
337 svc_rdma_unmap_dma(ctxt);
338
339 switch (ctxt->wr_op) {
340 case IB_WR_SEND:
Steve Wise0bf48282014-05-28 15:12:01 -0500341 BUG_ON(ctxt->frmr);
Tom Tuckere1183212008-10-03 15:22:18 -0500342 svc_rdma_put_context(ctxt, 1);
343 break;
344
345 case IB_WR_RDMA_WRITE:
Steve Wise0bf48282014-05-28 15:12:01 -0500346 BUG_ON(ctxt->frmr);
Tom Tuckere1183212008-10-03 15:22:18 -0500347 svc_rdma_put_context(ctxt, 0);
348 break;
349
350 case IB_WR_RDMA_READ:
Tom Tucker146b6df2008-08-12 15:12:10 -0500351 case IB_WR_RDMA_READ_WITH_INV:
Steve Wise0bf48282014-05-28 15:12:01 -0500352 svc_rdma_put_frmr(xprt, ctxt->frmr);
Tom Tuckere1183212008-10-03 15:22:18 -0500353 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
354 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
355 BUG_ON(!read_hdr);
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);
362 }
363 svc_rdma_put_context(ctxt, 0);
364 break;
365
366 default:
Steve Wise0bf48282014-05-28 15:12:01 -0500367 BUG_ON(1);
Tom Tuckere1183212008-10-03 15:22:18 -0500368 printk(KERN_ERR "svcrdma: unexpected completion type, "
369 "opcode=%d\n",
370 ctxt->wr_op);
371 break;
372 }
373}
374
375/*
Tom Tucker377f9b22007-12-12 16:13:21 -0600376 * Send Queue Completion Handler - potentially called on interrupt context.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500377 *
378 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600379 */
380static void sq_cq_reap(struct svcxprt_rdma *xprt)
381{
382 struct svc_rdma_op_ctxt *ctxt = NULL;
Steve Wise0bf48282014-05-28 15:12:01 -0500383 struct ib_wc wc_a[6];
384 struct ib_wc *wc;
Tom Tucker377f9b22007-12-12 16:13:21 -0600385 struct ib_cq *cq = xprt->sc_sq_cq;
386 int ret;
387
Steve Wise0bf48282014-05-28 15:12:01 -0500388 memset(wc_a, 0, sizeof(wc_a));
389
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500390 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
391 return;
392
393 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600394 atomic_inc(&rdma_stat_sq_poll);
Steve Wise0bf48282014-05-28 15:12:01 -0500395 while ((ret = ib_poll_cq(cq, ARRAY_SIZE(wc_a), wc_a)) > 0) {
396 int i;
Tom Tucker377f9b22007-12-12 16:13:21 -0600397
Steve Wise0bf48282014-05-28 15:12:01 -0500398 for (i = 0; i < ret; i++) {
399 wc = &wc_a[i];
400 if (wc->status != IB_WC_SUCCESS) {
401 dprintk("svcrdma: sq wc err status %d\n",
402 wc->status);
Tom Tucker377f9b22007-12-12 16:13:21 -0600403
Steve Wise0bf48282014-05-28 15:12:01 -0500404 /* Close the transport */
405 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
406 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600407
Steve Wise0bf48282014-05-28 15:12:01 -0500408 /* Decrement used SQ WR count */
409 atomic_dec(&xprt->sc_sq_count);
410 wake_up(&xprt->sc_send_wait);
411
412 ctxt = (struct svc_rdma_op_ctxt *)
413 (unsigned long)wc->wr_id;
414 if (ctxt)
415 process_context(xprt, ctxt);
416
417 svc_xprt_put(&xprt->sc_xprt);
418 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600419 }
420
421 if (ctxt)
422 atomic_inc(&rdma_stat_sq_prod);
423}
424
425static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
426{
427 struct svcxprt_rdma *xprt = cq_context;
428 unsigned long flags;
429
Tom Tucker17113862008-05-01 11:13:50 -0500430 /* Guard against unconditional flush call for destroyed QP */
431 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
432 return;
433
Tom Tucker377f9b22007-12-12 16:13:21 -0600434 /*
435 * Set the bit regardless of whether or not it's on the list
436 * because it may be on the list already due to an RQ
437 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500438 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600439 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
440
441 /*
442 * If this transport is not already on the DTO transport queue,
443 * add it
444 */
445 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400446 if (list_empty(&xprt->sc_dto_q)) {
447 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600448 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400449 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600450 spin_unlock_irqrestore(&dto_lock, flags);
451
452 /* Tasklet does all the work to avoid irqsave locks. */
453 tasklet_schedule(&dto_tasklet);
454}
455
Tom Tucker377f9b22007-12-12 16:13:21 -0600456static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
457 int listener)
458{
459 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
460
461 if (!cma_xprt)
462 return NULL;
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300463 svc_xprt_init(&init_net, &svc_rdma_class, &cma_xprt->sc_xprt, serv);
Tom Tucker377f9b22007-12-12 16:13:21 -0600464 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
465 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
466 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
467 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
Tom Tucker64be86082008-10-06 14:45:18 -0500468 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
Tom Tucker377f9b22007-12-12 16:13:21 -0600469 init_waitqueue_head(&cma_xprt->sc_send_wait);
470
471 spin_lock_init(&cma_xprt->sc_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600472 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
Tom Tucker64be86082008-10-06 14:45:18 -0500473 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600474
475 cma_xprt->sc_ord = svcrdma_ord;
476
477 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
478 cma_xprt->sc_max_requests = svcrdma_max_requests;
479 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
480 atomic_set(&cma_xprt->sc_sq_count, 0);
Tom Tucker87295b62008-05-28 13:17:44 -0500481 atomic_set(&cma_xprt->sc_ctxt_used, 0);
Tom Tucker377f9b22007-12-12 16:13:21 -0600482
Tom Tucker89488962008-05-28 15:14:02 -0500483 if (listener)
Tom Tucker377f9b22007-12-12 16:13:21 -0600484 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
485
486 return cma_xprt;
487}
488
489struct page *svc_rdma_get_page(void)
490{
491 struct page *page;
492
493 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
494 /* If we can't get memory, wait a bit and try again */
Jeff Laytonc42a01e2014-03-10 11:33:48 -0400495 printk(KERN_INFO "svcrdma: out of memory...retrying in 1s\n");
Tom Tucker377f9b22007-12-12 16:13:21 -0600496 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
497 }
498 return page;
499}
500
501int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
502{
503 struct ib_recv_wr recv_wr, *bad_recv_wr;
504 struct svc_rdma_op_ctxt *ctxt;
505 struct page *page;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500506 dma_addr_t pa;
Tom Tucker377f9b22007-12-12 16:13:21 -0600507 int sge_no;
508 int buflen;
509 int ret;
510
511 ctxt = svc_rdma_get_context(xprt);
512 buflen = 0;
513 ctxt->direction = DMA_FROM_DEVICE;
514 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
515 BUG_ON(sge_no >= xprt->sc_max_sge);
516 page = svc_rdma_get_page();
517 ctxt->pages[sge_no] = page;
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500518 pa = ib_dma_map_page(xprt->sc_cm_id->device,
519 page, 0, PAGE_SIZE,
Tom Tucker377f9b22007-12-12 16:13:21 -0600520 DMA_FROM_DEVICE);
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500521 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
522 goto err_put_ctxt;
523 atomic_inc(&xprt->sc_dma_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600524 ctxt->sge[sge_no].addr = pa;
525 ctxt->sge[sge_no].length = PAGE_SIZE;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500526 ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
Tom Tucker4a843862010-10-12 15:33:57 -0500527 ctxt->count = sge_no + 1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600528 buflen += PAGE_SIZE;
529 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600530 recv_wr.next = NULL;
531 recv_wr.sg_list = &ctxt->sge[0];
532 recv_wr.num_sge = ctxt->count;
533 recv_wr.wr_id = (u64)(unsigned long)ctxt;
534
Tom Tucker0905c0f2008-05-01 10:49:03 -0500535 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600536 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500537 if (ret) {
Steve Wise21515e42009-04-29 14:14:00 -0500538 svc_rdma_unmap_dma(ctxt);
Tom Tucker05a08262008-04-25 14:11:31 -0500539 svc_rdma_put_context(ctxt, 1);
Steve Wise21515e42009-04-29 14:14:00 -0500540 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500541 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600542 return ret;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500543
544 err_put_ctxt:
Tom Tucker4a843862010-10-12 15:33:57 -0500545 svc_rdma_unmap_dma(ctxt);
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500546 svc_rdma_put_context(ctxt, 1);
547 return -ENOMEM;
Tom Tucker377f9b22007-12-12 16:13:21 -0600548}
549
550/*
551 * This function handles the CONNECT_REQUEST event on a listening
552 * endpoint. It is passed the cma_id for the _new_ connection. The context in
553 * this cma_id is inherited from the listening cma_id and is the svc_xprt
554 * structure for the listening endpoint.
555 *
556 * This function creates a new xprt for the new connection and enqueues it on
557 * the accept queue for the listent xprt. When the listen thread is kicked, it
558 * will call the recvfrom method on the listen xprt which will accept the new
559 * connection.
560 */
Tom Tucker36ef25e2008-05-19 19:00:24 -0500561static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
Tom Tucker377f9b22007-12-12 16:13:21 -0600562{
563 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
564 struct svcxprt_rdma *newxprt;
Tom Tuckeraf261af2008-05-07 13:52:42 -0500565 struct sockaddr *sa;
Tom Tucker377f9b22007-12-12 16:13:21 -0600566
567 /* Create a new transport */
568 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
569 if (!newxprt) {
570 dprintk("svcrdma: failed to create new transport\n");
571 return;
572 }
573 newxprt->sc_cm_id = new_cma_id;
574 new_cma_id->context = newxprt;
575 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
576 newxprt, newxprt->sc_cm_id, listen_xprt);
577
Tom Tucker36ef25e2008-05-19 19:00:24 -0500578 /* Save client advertised inbound read limit for use later in accept. */
579 newxprt->sc_ord = client_ird;
580
Tom Tuckeraf261af2008-05-07 13:52:42 -0500581 /* Set the local and remote addresses in the transport */
582 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
583 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
584 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
585 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
586
Tom Tucker377f9b22007-12-12 16:13:21 -0600587 /*
588 * Enqueue the new transport on the accept queue of the listening
589 * transport
590 */
591 spin_lock_bh(&listen_xprt->sc_lock);
592 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
593 spin_unlock_bh(&listen_xprt->sc_lock);
594
Tom Tucker377f9b22007-12-12 16:13:21 -0600595 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
596 svc_xprt_enqueue(&listen_xprt->sc_xprt);
597}
598
599/*
600 * Handles events generated on the listening endpoint. These events will be
601 * either be incoming connect requests or adapter removal events.
602 */
603static int rdma_listen_handler(struct rdma_cm_id *cma_id,
604 struct rdma_cm_event *event)
605{
606 struct svcxprt_rdma *xprt = cma_id->context;
607 int ret = 0;
608
609 switch (event->event) {
610 case RDMA_CM_EVENT_CONNECT_REQUEST:
611 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
612 "event=%d\n", cma_id, cma_id->context, event->event);
Tom Tucker36ef25e2008-05-19 19:00:24 -0500613 handle_connect_req(cma_id,
Tom Tucker67080c82008-10-03 12:41:14 -0500614 event->param.conn.initiator_depth);
Tom Tucker377f9b22007-12-12 16:13:21 -0600615 break;
616
617 case RDMA_CM_EVENT_ESTABLISHED:
618 /* Accept complete */
619 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
620 "cm_id=%p\n", xprt, cma_id);
621 break;
622
623 case RDMA_CM_EVENT_DEVICE_REMOVAL:
624 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
625 xprt, cma_id);
626 if (xprt)
627 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
628 break;
629
630 default:
631 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
632 "event=%d\n", cma_id, event->event);
633 break;
634 }
635
636 return ret;
637}
638
639static int rdma_cma_handler(struct rdma_cm_id *cma_id,
640 struct rdma_cm_event *event)
641{
642 struct svc_xprt *xprt = cma_id->context;
643 struct svcxprt_rdma *rdma =
644 container_of(xprt, struct svcxprt_rdma, sc_xprt);
645 switch (event->event) {
646 case RDMA_CM_EVENT_ESTABLISHED:
647 /* Accept complete */
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400648 svc_xprt_get(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600649 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
650 "cm_id=%p\n", xprt, cma_id);
651 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
652 svc_xprt_enqueue(xprt);
653 break;
654 case RDMA_CM_EVENT_DISCONNECTED:
655 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
656 xprt, cma_id);
657 if (xprt) {
658 set_bit(XPT_CLOSE, &xprt->xpt_flags);
659 svc_xprt_enqueue(xprt);
Tom Tucker120693d2008-04-24 14:17:21 -0500660 svc_xprt_put(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600661 }
662 break;
663 case RDMA_CM_EVENT_DEVICE_REMOVAL:
664 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
665 "event=%d\n", cma_id, xprt, event->event);
666 if (xprt) {
667 set_bit(XPT_CLOSE, &xprt->xpt_flags);
668 svc_xprt_enqueue(xprt);
669 }
670 break;
671 default:
672 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
673 "event=%d\n", cma_id, event->event);
674 break;
675 }
676 return 0;
677}
678
679/*
680 * Create a listening RDMA service endpoint.
681 */
682static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
Pavel Emelyanov62832c02010-09-29 16:04:18 +0400683 struct net *net,
Tom Tucker377f9b22007-12-12 16:13:21 -0600684 struct sockaddr *sa, int salen,
685 int flags)
686{
687 struct rdma_cm_id *listen_id;
688 struct svcxprt_rdma *cma_xprt;
689 struct svc_xprt *xprt;
690 int ret;
691
692 dprintk("svcrdma: Creating RDMA socket\n");
Tom Tuckerbade7322010-04-03 08:27:29 -0500693 if (sa->sa_family != AF_INET) {
694 dprintk("svcrdma: Address family %d is not supported.\n", sa->sa_family);
695 return ERR_PTR(-EAFNOSUPPORT);
696 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600697 cma_xprt = rdma_create_xprt(serv, 1);
698 if (!cma_xprt)
Tom Tucker58e8f622008-05-06 09:45:54 -0500699 return ERR_PTR(-ENOMEM);
Tom Tucker377f9b22007-12-12 16:13:21 -0600700 xprt = &cma_xprt->sc_xprt;
701
Sean Heftyb26f9b92010-04-01 17:08:41 +0000702 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP,
703 IB_QPT_RC);
Tom Tucker377f9b22007-12-12 16:13:21 -0600704 if (IS_ERR(listen_id)) {
Tom Tucker58e8f622008-05-06 09:45:54 -0500705 ret = PTR_ERR(listen_id);
706 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
707 goto err0;
Tom Tucker377f9b22007-12-12 16:13:21 -0600708 }
Tom Tucker58e8f622008-05-06 09:45:54 -0500709
Tom Tucker377f9b22007-12-12 16:13:21 -0600710 ret = rdma_bind_addr(listen_id, sa);
711 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600712 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500713 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600714 }
715 cma_xprt->sc_cm_id = listen_id;
716
717 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
718 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600719 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500720 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600721 }
722
723 /*
724 * We need to use the address from the cm_id in case the
725 * caller specified 0 for the port number.
726 */
727 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
728 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
729
730 return &cma_xprt->sc_xprt;
Tom Tucker58e8f622008-05-06 09:45:54 -0500731
732 err1:
733 rdma_destroy_id(listen_id);
734 err0:
735 kfree(cma_xprt);
736 return ERR_PTR(ret);
Tom Tucker377f9b22007-12-12 16:13:21 -0600737}
738
Tom Tucker64be86082008-10-06 14:45:18 -0500739static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
740{
741 struct ib_mr *mr;
742 struct ib_fast_reg_page_list *pl;
743 struct svc_rdma_fastreg_mr *frmr;
744
745 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
746 if (!frmr)
747 goto err;
748
749 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
Wei Yongjun846d8e72009-06-25 16:35:44 +0800750 if (IS_ERR(mr))
Tom Tucker64be86082008-10-06 14:45:18 -0500751 goto err_free_frmr;
752
753 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
754 RPCSVC_MAXPAGES);
Wei Yongjun846d8e72009-06-25 16:35:44 +0800755 if (IS_ERR(pl))
Tom Tucker64be86082008-10-06 14:45:18 -0500756 goto err_free_mr;
757
758 frmr->mr = mr;
759 frmr->page_list = pl;
760 INIT_LIST_HEAD(&frmr->frmr_list);
761 return frmr;
762
763 err_free_mr:
764 ib_dereg_mr(mr);
765 err_free_frmr:
766 kfree(frmr);
767 err:
768 return ERR_PTR(-ENOMEM);
769}
770
771static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
772{
773 struct svc_rdma_fastreg_mr *frmr;
774
775 while (!list_empty(&xprt->sc_frmr_q)) {
776 frmr = list_entry(xprt->sc_frmr_q.next,
777 struct svc_rdma_fastreg_mr, frmr_list);
778 list_del_init(&frmr->frmr_list);
779 ib_dereg_mr(frmr->mr);
780 ib_free_fast_reg_page_list(frmr->page_list);
781 kfree(frmr);
782 }
783}
784
785struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
786{
787 struct svc_rdma_fastreg_mr *frmr = NULL;
788
789 spin_lock_bh(&rdma->sc_frmr_q_lock);
790 if (!list_empty(&rdma->sc_frmr_q)) {
791 frmr = list_entry(rdma->sc_frmr_q.next,
792 struct svc_rdma_fastreg_mr, frmr_list);
793 list_del_init(&frmr->frmr_list);
794 frmr->map_len = 0;
795 frmr->page_list_len = 0;
796 }
797 spin_unlock_bh(&rdma->sc_frmr_q_lock);
798 if (frmr)
799 return frmr;
800
801 return rdma_alloc_frmr(rdma);
802}
803
804static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
805 struct svc_rdma_fastreg_mr *frmr)
806{
807 int page_no;
808 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
809 dma_addr_t addr = frmr->page_list->page_list[page_no];
810 if (ib_dma_mapping_error(frmr->mr->device, addr))
811 continue;
812 atomic_dec(&xprt->sc_dma_used);
Tom Tuckerb432e6b2010-10-12 15:33:52 -0500813 ib_dma_unmap_page(frmr->mr->device, addr, PAGE_SIZE,
814 frmr->direction);
Tom Tucker64be86082008-10-06 14:45:18 -0500815 }
816}
817
818void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
819 struct svc_rdma_fastreg_mr *frmr)
820{
821 if (frmr) {
822 frmr_unmap_dma(rdma, frmr);
823 spin_lock_bh(&rdma->sc_frmr_q_lock);
824 BUG_ON(!list_empty(&frmr->frmr_list));
825 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
826 spin_unlock_bh(&rdma->sc_frmr_q_lock);
827 }
828}
829
Tom Tucker377f9b22007-12-12 16:13:21 -0600830/*
831 * This is the xpo_recvfrom function for listening endpoints. Its
832 * purpose is to accept incoming connections. The CMA callback handler
833 * has already created a new transport and attached it to the new CMA
834 * ID.
835 *
836 * There is a queue of pending connections hung on the listening
837 * transport. This queue contains the new svc_xprt structure. This
838 * function takes svc_xprt structures off the accept_q and completes
839 * the connection.
840 */
841static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
842{
843 struct svcxprt_rdma *listen_rdma;
844 struct svcxprt_rdma *newxprt = NULL;
845 struct rdma_conn_param conn_param;
846 struct ib_qp_init_attr qp_attr;
847 struct ib_device_attr devattr;
Ingo Molnared72b9c2008-11-25 16:49:37 -0800848 int uninitialized_var(dma_mr_acc);
Tom Tucker3a5c6382008-09-30 13:46:13 -0500849 int need_dma_mr;
Tom Tucker377f9b22007-12-12 16:13:21 -0600850 int ret;
851 int i;
852
853 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
854 clear_bit(XPT_CONN, &xprt->xpt_flags);
855 /* Get the next entry off the accept list */
856 spin_lock_bh(&listen_rdma->sc_lock);
857 if (!list_empty(&listen_rdma->sc_accept_q)) {
858 newxprt = list_entry(listen_rdma->sc_accept_q.next,
859 struct svcxprt_rdma, sc_accept_q);
860 list_del_init(&newxprt->sc_accept_q);
861 }
862 if (!list_empty(&listen_rdma->sc_accept_q))
863 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
864 spin_unlock_bh(&listen_rdma->sc_lock);
865 if (!newxprt)
866 return NULL;
867
868 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
869 newxprt, newxprt->sc_cm_id);
870
871 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
872 if (ret) {
873 dprintk("svcrdma: could not query device attributes on "
874 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
875 goto errout;
876 }
877
878 /* Qualify the transport resource defaults with the
879 * capabilities of this particular device */
880 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
881 (size_t)RPCSVC_MAXPAGES);
882 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
883 (size_t)svcrdma_max_requests);
884 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
885
Tom Tucker36ef25e2008-05-19 19:00:24 -0500886 /*
887 * Limit ORD based on client limit, local device limit, and
888 * configured svcrdma limit.
889 */
890 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
891 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
Tom Tucker377f9b22007-12-12 16:13:21 -0600892
893 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
894 if (IS_ERR(newxprt->sc_pd)) {
895 dprintk("svcrdma: error creating PD for connect request\n");
896 goto errout;
897 }
898 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
899 sq_comp_handler,
900 cq_event_handler,
901 newxprt,
902 newxprt->sc_sq_depth,
903 0);
904 if (IS_ERR(newxprt->sc_sq_cq)) {
905 dprintk("svcrdma: error creating SQ CQ for connect request\n");
906 goto errout;
907 }
908 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
909 rq_comp_handler,
910 cq_event_handler,
911 newxprt,
912 newxprt->sc_max_requests,
913 0);
914 if (IS_ERR(newxprt->sc_rq_cq)) {
915 dprintk("svcrdma: error creating RQ CQ for connect request\n");
916 goto errout;
917 }
918
919 memset(&qp_attr, 0, sizeof qp_attr);
920 qp_attr.event_handler = qp_event_handler;
921 qp_attr.qp_context = &newxprt->sc_xprt;
922 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
923 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
924 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
925 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
926 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
927 qp_attr.qp_type = IB_QPT_RC;
928 qp_attr.send_cq = newxprt->sc_sq_cq;
929 qp_attr.recv_cq = newxprt->sc_rq_cq;
930 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
931 " cm_id->device=%p, sc_pd->device=%p\n"
932 " cap.max_send_wr = %d\n"
933 " cap.max_recv_wr = %d\n"
934 " cap.max_send_sge = %d\n"
935 " cap.max_recv_sge = %d\n",
936 newxprt->sc_cm_id, newxprt->sc_pd,
937 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
938 qp_attr.cap.max_send_wr,
939 qp_attr.cap.max_recv_wr,
940 qp_attr.cap.max_send_sge,
941 qp_attr.cap.max_recv_sge);
942
943 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
944 if (ret) {
945 /*
946 * XXX: This is a hack. We need a xx_request_qp interface
947 * that will adjust the qp_attr's with a best-effort
948 * number
949 */
950 qp_attr.cap.max_send_sge -= 2;
951 qp_attr.cap.max_recv_sge -= 2;
952 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
953 &qp_attr);
954 if (ret) {
955 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
956 goto errout;
957 }
958 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
959 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
960 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
961 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
962 }
963 newxprt->sc_qp = newxprt->sc_cm_id->qp;
964
Tom Tucker3a5c6382008-09-30 13:46:13 -0500965 /*
966 * Use the most secure set of MR resources based on the
967 * transport type and available memory management features in
968 * the device. Here's the table implemented below:
969 *
970 * Fast Global DMA Remote WR
971 * Reg LKEY MR Access
972 * Sup'd Sup'd Needed Needed
973 *
974 * IWARP N N Y Y
975 * N Y Y Y
976 * Y N Y N
977 * Y Y N -
978 *
979 * IB N N Y N
980 * N Y N -
981 * Y N Y N
982 * Y Y N -
983 *
984 * NB: iWARP requires remote write access for the data sink
985 * of an RDMA_READ. IB does not.
986 */
987 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
988 newxprt->sc_frmr_pg_list_len =
989 devattr.max_fast_reg_page_list_len;
990 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
991 }
992
993 /*
994 * Determine if a DMA MR is required and if so, what privs are required
995 */
996 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
997 case RDMA_TRANSPORT_IWARP:
998 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
999 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
1000 need_dma_mr = 1;
1001 dma_mr_acc =
1002 (IB_ACCESS_LOCAL_WRITE |
1003 IB_ACCESS_REMOTE_WRITE);
1004 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
1005 need_dma_mr = 1;
1006 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1007 } else
1008 need_dma_mr = 0;
1009 break;
1010 case RDMA_TRANSPORT_IB:
Steve Wise0bf48282014-05-28 15:12:01 -05001011 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
1012 need_dma_mr = 1;
1013 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1014 } else if (!(devattr.device_cap_flags &
1015 IB_DEVICE_LOCAL_DMA_LKEY)) {
Tom Tucker3a5c6382008-09-30 13:46:13 -05001016 need_dma_mr = 1;
1017 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
1018 } else
1019 need_dma_mr = 0;
1020 break;
1021 default:
Tom Tucker377f9b22007-12-12 16:13:21 -06001022 goto errout;
1023 }
1024
Tom Tucker3a5c6382008-09-30 13:46:13 -05001025 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
1026 if (need_dma_mr) {
1027 /* Register all of physical memory */
1028 newxprt->sc_phys_mr =
1029 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1030 if (IS_ERR(newxprt->sc_phys_mr)) {
1031 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1032 ret);
1033 goto errout;
1034 }
1035 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1036 } else
1037 newxprt->sc_dma_lkey =
1038 newxprt->sc_cm_id->device->local_dma_lkey;
1039
Tom Tucker377f9b22007-12-12 16:13:21 -06001040 /* Post receive buffers */
1041 for (i = 0; i < newxprt->sc_max_requests; i++) {
1042 ret = svc_rdma_post_recv(newxprt);
1043 if (ret) {
1044 dprintk("svcrdma: failure posting receive buffers\n");
1045 goto errout;
1046 }
1047 }
1048
1049 /* Swap out the handler */
1050 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1051
Tom Tuckeraf261af2008-05-07 13:52:42 -05001052 /*
1053 * Arm the CQs for the SQ and RQ before accepting so we can't
1054 * miss the first message
1055 */
1056 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1057 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1058
Tom Tucker377f9b22007-12-12 16:13:21 -06001059 /* Accept Connection */
1060 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1061 memset(&conn_param, 0, sizeof conn_param);
1062 conn_param.responder_resources = 0;
1063 conn_param.initiator_depth = newxprt->sc_ord;
1064 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1065 if (ret) {
1066 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1067 ret);
1068 goto errout;
1069 }
1070
1071 dprintk("svcrdma: new connection %p accepted with the following "
1072 "attributes:\n"
Harvey Harrison21454aa2008-10-31 00:54:56 -07001073 " local_ip : %pI4\n"
Tom Tucker377f9b22007-12-12 16:13:21 -06001074 " local_port : %d\n"
Harvey Harrison21454aa2008-10-31 00:54:56 -07001075 " remote_ip : %pI4\n"
Tom Tucker377f9b22007-12-12 16:13:21 -06001076 " remote_port : %d\n"
1077 " max_sge : %d\n"
1078 " sq_depth : %d\n"
1079 " max_requests : %d\n"
1080 " ord : %d\n",
1081 newxprt,
Harvey Harrison21454aa2008-10-31 00:54:56 -07001082 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1083 route.addr.src_addr)->sin_addr.s_addr,
Tom Tucker377f9b22007-12-12 16:13:21 -06001084 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1085 route.addr.src_addr)->sin_port),
Harvey Harrison21454aa2008-10-31 00:54:56 -07001086 &((struct sockaddr_in *)&newxprt->sc_cm_id->
1087 route.addr.dst_addr)->sin_addr.s_addr,
Tom Tucker377f9b22007-12-12 16:13:21 -06001088 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1089 route.addr.dst_addr)->sin_port),
1090 newxprt->sc_max_sge,
1091 newxprt->sc_sq_depth,
1092 newxprt->sc_max_requests,
1093 newxprt->sc_ord);
1094
Tom Tucker377f9b22007-12-12 16:13:21 -06001095 return &newxprt->sc_xprt;
1096
1097 errout:
1098 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001099 /* Take a reference in case the DTO handler runs */
1100 svc_xprt_get(&newxprt->sc_xprt);
Tom Tucker17113862008-05-01 11:13:50 -05001101 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001102 ib_destroy_qp(newxprt->sc_qp);
Tom Tucker377f9b22007-12-12 16:13:21 -06001103 rdma_destroy_id(newxprt->sc_cm_id);
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001104 /* This call to put will destroy the transport */
1105 svc_xprt_put(&newxprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001106 return NULL;
1107}
1108
Tom Tucker377f9b22007-12-12 16:13:21 -06001109static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1110{
Tom Tucker377f9b22007-12-12 16:13:21 -06001111}
1112
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001113/*
Tom Tucker17113862008-05-01 11:13:50 -05001114 * When connected, an svc_xprt has at least two references:
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001115 *
1116 * - A reference held by the cm_id between the ESTABLISHED and
1117 * DISCONNECTED events. If the remote peer disconnected first, this
1118 * reference could be gone.
1119 *
1120 * - A reference held by the svc_recv code that called this function
1121 * as part of close processing.
1122 *
Tom Tucker17113862008-05-01 11:13:50 -05001123 * At a minimum one references should still be held.
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001124 */
Tom Tucker377f9b22007-12-12 16:13:21 -06001125static void svc_rdma_detach(struct svc_xprt *xprt)
1126{
1127 struct svcxprt_rdma *rdma =
1128 container_of(xprt, struct svcxprt_rdma, sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001129 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001130
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001131 /* Disconnect and flush posted WQE */
1132 rdma_disconnect(rdma->sc_cm_id);
Tom Tucker377f9b22007-12-12 16:13:21 -06001133}
1134
Tom Tucker8da91ea2008-04-30 22:00:46 -05001135static void __svc_rdma_free(struct work_struct *work)
Tom Tucker377f9b22007-12-12 16:13:21 -06001136{
Tom Tucker8da91ea2008-04-30 22:00:46 -05001137 struct svcxprt_rdma *rdma =
1138 container_of(work, struct svcxprt_rdma, sc_work);
Tom Tucker377f9b22007-12-12 16:13:21 -06001139 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
Tom Tucker8da91ea2008-04-30 22:00:46 -05001140
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001141 /* We should only be called from kref_put */
Tom Tucker8da91ea2008-04-30 22:00:46 -05001142 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1143
Tom Tucker356d0a12008-05-01 11:25:02 -05001144 /*
1145 * Destroy queued, but not processed read completions. Note
1146 * that this cleanup has to be done before destroying the
1147 * cm_id because the device ptr is needed to unmap the dma in
1148 * svc_rdma_put_context.
1149 */
Tom Tucker356d0a12008-05-01 11:25:02 -05001150 while (!list_empty(&rdma->sc_read_complete_q)) {
1151 struct svc_rdma_op_ctxt *ctxt;
1152 ctxt = list_entry(rdma->sc_read_complete_q.next,
1153 struct svc_rdma_op_ctxt,
1154 dto_q);
1155 list_del_init(&ctxt->dto_q);
1156 svc_rdma_put_context(ctxt, 1);
1157 }
Tom Tucker356d0a12008-05-01 11:25:02 -05001158
1159 /* Destroy queued, but not processed recv completions */
Tom Tucker356d0a12008-05-01 11:25:02 -05001160 while (!list_empty(&rdma->sc_rq_dto_q)) {
1161 struct svc_rdma_op_ctxt *ctxt;
1162 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1163 struct svc_rdma_op_ctxt,
1164 dto_q);
1165 list_del_init(&ctxt->dto_q);
1166 svc_rdma_put_context(ctxt, 1);
1167 }
Tom Tucker356d0a12008-05-01 11:25:02 -05001168
1169 /* Warn if we leaked a resource or under-referenced */
1170 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
Tom Tucker87295b62008-05-28 13:17:44 -05001171 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
Tom Tucker356d0a12008-05-01 11:25:02 -05001172
Tom Tucker64be86082008-10-06 14:45:18 -05001173 /* De-allocate fastreg mr */
1174 rdma_dealloc_frmr_q(rdma);
1175
Tom Tucker17113862008-05-01 11:13:50 -05001176 /* Destroy the QP if present (not a listener) */
1177 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1178 ib_destroy_qp(rdma->sc_qp);
1179
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001180 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1181 ib_destroy_cq(rdma->sc_sq_cq);
1182
1183 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1184 ib_destroy_cq(rdma->sc_rq_cq);
1185
1186 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1187 ib_dereg_mr(rdma->sc_phys_mr);
1188
1189 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1190 ib_dealloc_pd(rdma->sc_pd);
1191
Tom Tucker356d0a12008-05-01 11:25:02 -05001192 /* Destroy the CM ID */
1193 rdma_destroy_id(rdma->sc_cm_id);
1194
Tom Tucker377f9b22007-12-12 16:13:21 -06001195 kfree(rdma);
1196}
1197
Tom Tucker8da91ea2008-04-30 22:00:46 -05001198static void svc_rdma_free(struct svc_xprt *xprt)
1199{
1200 struct svcxprt_rdma *rdma =
1201 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1202 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
Tejun Heoa25e7582010-10-15 17:49:27 +02001203 queue_work(svc_rdma_wq, &rdma->sc_work);
Tom Tucker8da91ea2008-04-30 22:00:46 -05001204}
1205
Tom Tucker377f9b22007-12-12 16:13:21 -06001206static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1207{
1208 struct svcxprt_rdma *rdma =
1209 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1210
1211 /*
Steve Wise0bf48282014-05-28 15:12:01 -05001212 * If there are already waiters on the SQ,
Tom Tucker377f9b22007-12-12 16:13:21 -06001213 * return false.
1214 */
1215 if (waitqueue_active(&rdma->sc_send_wait))
1216 return 0;
1217
1218 /* Otherwise return true. */
1219 return 1;
1220}
1221
Chuck Lever16e4d932014-05-19 13:40:22 -04001222static int svc_rdma_secure_port(struct svc_rqst *rqstp)
1223{
1224 return 1;
1225}
1226
Tom Tuckere1183212008-10-03 15:22:18 -05001227/*
1228 * Attempt to register the kvec representing the RPC memory with the
1229 * device.
1230 *
1231 * Returns:
1232 * NULL : The device does not support fastreg or there were no more
1233 * fastreg mr.
1234 * frmr : The kvec register request was successfully posted.
1235 * <0 : An error was encountered attempting to register the kvec.
1236 */
1237int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1238 struct svc_rdma_fastreg_mr *frmr)
1239{
1240 struct ib_send_wr fastreg_wr;
1241 u8 key;
1242
1243 /* Bump the key */
1244 key = (u8)(frmr->mr->lkey & 0x000000FF);
1245 ib_update_fast_reg_key(frmr->mr, ++key);
1246
1247 /* Prepare FASTREG WR */
1248 memset(&fastreg_wr, 0, sizeof fastreg_wr);
1249 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1250 fastreg_wr.send_flags = IB_SEND_SIGNALED;
1251 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1252 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1253 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1254 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1255 fastreg_wr.wr.fast_reg.length = frmr->map_len;
1256 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1257 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1258 return svc_rdma_send(xprt, &fastreg_wr);
1259}
1260
Tom Tucker377f9b22007-12-12 16:13:21 -06001261int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1262{
Tom Tucker5b180a92008-08-11 14:10:19 -05001263 struct ib_send_wr *bad_wr, *n_wr;
1264 int wr_count;
1265 int i;
Tom Tucker377f9b22007-12-12 16:13:21 -06001266 int ret;
1267
1268 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
Tom Tucker9d6347a2008-04-25 15:51:27 -05001269 return -ENOTCONN;
Tom Tucker377f9b22007-12-12 16:13:21 -06001270
1271 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
Tom Tucker5b180a92008-08-11 14:10:19 -05001272 wr_count = 1;
1273 for (n_wr = wr->next; n_wr; n_wr = n_wr->next)
1274 wr_count++;
1275
Tom Tucker377f9b22007-12-12 16:13:21 -06001276 /* If the SQ is full, wait until an SQ entry is available */
1277 while (1) {
1278 spin_lock_bh(&xprt->sc_lock);
Tom Tucker5b180a92008-08-11 14:10:19 -05001279 if (xprt->sc_sq_depth < atomic_read(&xprt->sc_sq_count) + wr_count) {
Tom Tucker377f9b22007-12-12 16:13:21 -06001280 spin_unlock_bh(&xprt->sc_lock);
1281 atomic_inc(&rdma_stat_sq_starve);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -05001282
1283 /* See if we can opportunistically reap SQ WR to make room */
Tom Tucker377f9b22007-12-12 16:13:21 -06001284 sq_cq_reap(xprt);
1285
1286 /* Wait until SQ WR available if SQ still full */
1287 wait_event(xprt->sc_send_wait,
1288 atomic_read(&xprt->sc_sq_count) <
1289 xprt->sc_sq_depth);
Tom Tucker830bb592008-03-11 12:44:27 -05001290 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
Tom Tuckerb432e6b2010-10-12 15:33:52 -05001291 return -ENOTCONN;
Tom Tucker377f9b22007-12-12 16:13:21 -06001292 continue;
1293 }
Tom Tucker5b180a92008-08-11 14:10:19 -05001294 /* Take a transport ref for each WR posted */
1295 for (i = 0; i < wr_count; i++)
1296 svc_xprt_get(&xprt->sc_xprt);
1297
1298 /* Bump used SQ WR count and post */
1299 atomic_add(wr_count, &xprt->sc_sq_count);
Tom Tucker377f9b22007-12-12 16:13:21 -06001300 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
Tom Tucker5b180a92008-08-11 14:10:19 -05001301 if (ret) {
1302 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
1303 atomic_sub(wr_count, &xprt->sc_sq_count);
1304 for (i = 0; i < wr_count; i ++)
1305 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001306 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1307 "sc_sq_count=%d, sc_sq_depth=%d\n",
1308 ret, atomic_read(&xprt->sc_sq_count),
1309 xprt->sc_sq_depth);
Tom Tucker0905c0f2008-05-01 10:49:03 -05001310 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001311 spin_unlock_bh(&xprt->sc_lock);
Tom Tucker5b180a92008-08-11 14:10:19 -05001312 if (ret)
1313 wake_up(&xprt->sc_send_wait);
Tom Tucker377f9b22007-12-12 16:13:21 -06001314 break;
1315 }
1316 return ret;
1317}
1318
Tom Tucker008fdbc2008-05-07 15:47:42 -05001319void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1320 enum rpcrdma_errcode err)
Tom Tucker377f9b22007-12-12 16:13:21 -06001321{
1322 struct ib_send_wr err_wr;
Tom Tucker377f9b22007-12-12 16:13:21 -06001323 struct page *p;
1324 struct svc_rdma_op_ctxt *ctxt;
1325 u32 *va;
1326 int length;
1327 int ret;
1328
1329 p = svc_rdma_get_page();
1330 va = page_address(p);
1331
1332 /* XDR encode error */
1333 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1334
Tom Tucker4a843862010-10-12 15:33:57 -05001335 ctxt = svc_rdma_get_context(xprt);
1336 ctxt->direction = DMA_FROM_DEVICE;
1337 ctxt->count = 1;
1338 ctxt->pages[0] = p;
1339
Tom Tucker377f9b22007-12-12 16:13:21 -06001340 /* Prepare SGE for local address */
Tom Tucker4a843862010-10-12 15:33:57 -05001341 ctxt->sge[0].addr = ib_dma_map_page(xprt->sc_cm_id->device,
1342 p, 0, length, DMA_FROM_DEVICE);
1343 if (ib_dma_mapping_error(xprt->sc_cm_id->device, ctxt->sge[0].addr)) {
Tom Tucker04911b52008-08-11 15:14:53 -05001344 put_page(p);
Jesper Juhla5e50262011-01-22 21:40:20 +00001345 svc_rdma_put_context(ctxt, 1);
Tom Tucker04911b52008-08-11 15:14:53 -05001346 return;
1347 }
1348 atomic_inc(&xprt->sc_dma_used);
Tom Tucker4a843862010-10-12 15:33:57 -05001349 ctxt->sge[0].lkey = xprt->sc_dma_lkey;
1350 ctxt->sge[0].length = length;
Tom Tucker377f9b22007-12-12 16:13:21 -06001351
1352 /* Prepare SEND WR */
1353 memset(&err_wr, 0, sizeof err_wr);
1354 ctxt->wr_op = IB_WR_SEND;
1355 err_wr.wr_id = (unsigned long)ctxt;
Tom Tucker4a843862010-10-12 15:33:57 -05001356 err_wr.sg_list = ctxt->sge;
Tom Tucker377f9b22007-12-12 16:13:21 -06001357 err_wr.num_sge = 1;
1358 err_wr.opcode = IB_WR_SEND;
1359 err_wr.send_flags = IB_SEND_SIGNALED;
1360
1361 /* Post It */
1362 ret = svc_rdma_send(xprt, &err_wr);
1363 if (ret) {
Tom Tucker008fdbc2008-05-07 15:47:42 -05001364 dprintk("svcrdma: Error %d posting send for protocol error\n",
1365 ret);
Tom Tucker4a843862010-10-12 15:33:57 -05001366 svc_rdma_unmap_dma(ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001367 svc_rdma_put_context(ctxt, 1);
1368 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001369}