blob: d9183cbcd9672fadbc13e41d1f7369aafb9b82f5 [file] [log] [blame]
Tom Tucker377f9b22007-12-12 16:13:21 -06001/*
2 * Copyright (c) 2005-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * Author: Tom Tucker <tom@opengridcomputing.com>
40 */
41
42#include <linux/sunrpc/svc_xprt.h>
43#include <linux/sunrpc/debug.h>
44#include <linux/sunrpc/rpc_rdma.h>
45#include <linux/spinlock.h>
46#include <rdma/ib_verbs.h>
47#include <rdma/rdma_cm.h>
48#include <linux/sunrpc/svc_rdma.h>
49
50#define RPCDBG_FACILITY RPCDBG_SVCXPRT
51
52static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
53 struct sockaddr *sa, int salen,
54 int flags);
55static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt);
56static void svc_rdma_release_rqst(struct svc_rqst *);
Tom Tucker377f9b22007-12-12 16:13:21 -060057static void dto_tasklet_func(unsigned long data);
58static void svc_rdma_detach(struct svc_xprt *xprt);
59static void svc_rdma_free(struct svc_xprt *xprt);
60static int svc_rdma_has_wspace(struct svc_xprt *xprt);
61static void rq_cq_reap(struct svcxprt_rdma *xprt);
62static void sq_cq_reap(struct svcxprt_rdma *xprt);
63
64DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL);
65static DEFINE_SPINLOCK(dto_lock);
66static LIST_HEAD(dto_xprt_q);
67
68static struct svc_xprt_ops svc_rdma_ops = {
69 .xpo_create = svc_rdma_create,
70 .xpo_recvfrom = svc_rdma_recvfrom,
71 .xpo_sendto = svc_rdma_sendto,
72 .xpo_release_rqst = svc_rdma_release_rqst,
73 .xpo_detach = svc_rdma_detach,
74 .xpo_free = svc_rdma_free,
75 .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr,
76 .xpo_has_wspace = svc_rdma_has_wspace,
77 .xpo_accept = svc_rdma_accept,
78};
79
80struct svc_xprt_class svc_rdma_class = {
81 .xcl_name = "rdma",
82 .xcl_owner = THIS_MODULE,
83 .xcl_ops = &svc_rdma_ops,
84 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
85};
86
Tom Tucker89488962008-05-28 15:14:02 -050087/* WR context cache. Created in svc_rdma.c */
88extern struct kmem_cache *svc_rdma_ctxt_cachep;
Tom Tucker377f9b22007-12-12 16:13:21 -060089
90struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
91{
92 struct svc_rdma_op_ctxt *ctxt;
93
94 while (1) {
Tom Tucker89488962008-05-28 15:14:02 -050095 ctxt = kmem_cache_alloc(svc_rdma_ctxt_cachep, GFP_KERNEL);
96 if (ctxt)
97 break;
98 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
Tom Tucker377f9b22007-12-12 16:13:21 -060099 }
Tom Tucker89488962008-05-28 15:14:02 -0500100 ctxt->xprt = xprt;
101 INIT_LIST_HEAD(&ctxt->dto_q);
102 ctxt->count = 0;
Tom Tucker64be86082008-10-06 14:45:18 -0500103 ctxt->frmr = NULL;
Tom Tucker89488962008-05-28 15:14:02 -0500104 atomic_inc(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600105 return ctxt;
106}
107
Tom Tuckere6ab9142008-05-28 12:08:48 -0500108static void svc_rdma_unmap_dma(struct svc_rdma_op_ctxt *ctxt)
109{
110 struct svcxprt_rdma *xprt = ctxt->xprt;
111 int i;
112 for (i = 0; i < ctxt->count && ctxt->sge[i].length; i++) {
Tom Tucker64be86082008-10-06 14:45:18 -0500113 /*
114 * Unmap the DMA addr in the SGE if the lkey matches
115 * the sc_dma_lkey, otherwise, ignore it since it is
116 * an FRMR lkey and will be unmapped later when the
117 * last WR that uses it completes.
118 */
119 if (ctxt->sge[i].lkey == xprt->sc_dma_lkey) {
120 atomic_dec(&xprt->sc_dma_used);
121 ib_dma_unmap_single(xprt->sc_cm_id->device,
122 ctxt->sge[i].addr,
123 ctxt->sge[i].length,
124 ctxt->direction);
125 }
Tom Tuckere6ab9142008-05-28 12:08:48 -0500126 }
127}
128
Tom Tucker377f9b22007-12-12 16:13:21 -0600129void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
130{
131 struct svcxprt_rdma *xprt;
132 int i;
133
134 BUG_ON(!ctxt);
135 xprt = ctxt->xprt;
136 if (free_pages)
137 for (i = 0; i < ctxt->count; i++)
138 put_page(ctxt->pages[i]);
139
Tom Tucker89488962008-05-28 15:14:02 -0500140 kmem_cache_free(svc_rdma_ctxt_cachep, ctxt);
Tom Tucker87407672008-04-30 20:44:39 -0500141 atomic_dec(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600142}
143
Tom Tuckerab96ddd2008-05-28 13:54:04 -0500144/* Temporary NFS request map cache. Created in svc_rdma.c */
145extern struct kmem_cache *svc_rdma_map_cachep;
146
147/*
148 * Temporary NFS req mappings are shared across all transport
149 * instances. These are short lived and should be bounded by the number
150 * of concurrent server threads * depth of the SQ.
151 */
152struct svc_rdma_req_map *svc_rdma_get_req_map(void)
153{
154 struct svc_rdma_req_map *map;
155 while (1) {
156 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
157 if (map)
158 break;
159 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
160 }
161 map->count = 0;
Tom Tucker64be86082008-10-06 14:45:18 -0500162 map->frmr = NULL;
Tom Tuckerab96ddd2008-05-28 13:54:04 -0500163 return map;
164}
165
166void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
167{
168 kmem_cache_free(svc_rdma_map_cachep, map);
169}
170
Tom Tucker377f9b22007-12-12 16:13:21 -0600171/* ib_cq event handler */
172static void cq_event_handler(struct ib_event *event, void *context)
173{
174 struct svc_xprt *xprt = context;
175 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
176 event->event, context);
177 set_bit(XPT_CLOSE, &xprt->xpt_flags);
178}
179
180/* QP event handler */
181static void qp_event_handler(struct ib_event *event, void *context)
182{
183 struct svc_xprt *xprt = context;
184
185 switch (event->event) {
186 /* These are considered benign events */
187 case IB_EVENT_PATH_MIG:
188 case IB_EVENT_COMM_EST:
189 case IB_EVENT_SQ_DRAINED:
190 case IB_EVENT_QP_LAST_WQE_REACHED:
191 dprintk("svcrdma: QP event %d received for QP=%p\n",
192 event->event, event->element.qp);
193 break;
194 /* These are considered fatal events */
195 case IB_EVENT_PATH_MIG_ERR:
196 case IB_EVENT_QP_FATAL:
197 case IB_EVENT_QP_REQ_ERR:
198 case IB_EVENT_QP_ACCESS_ERR:
199 case IB_EVENT_DEVICE_FATAL:
200 default:
201 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
202 "closing transport\n",
203 event->event, event->element.qp);
204 set_bit(XPT_CLOSE, &xprt->xpt_flags);
205 break;
206 }
207}
208
209/*
210 * Data Transfer Operation Tasklet
211 *
212 * Walks a list of transports with I/O pending, removing entries as
213 * they are added to the server's I/O pending list. Two bits indicate
214 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
215 * spinlock that serializes access to the transport list with the RQ
216 * and SQ interrupt handlers.
217 */
218static void dto_tasklet_func(unsigned long data)
219{
220 struct svcxprt_rdma *xprt;
221 unsigned long flags;
222
223 spin_lock_irqsave(&dto_lock, flags);
224 while (!list_empty(&dto_xprt_q)) {
225 xprt = list_entry(dto_xprt_q.next,
226 struct svcxprt_rdma, sc_dto_q);
227 list_del_init(&xprt->sc_dto_q);
228 spin_unlock_irqrestore(&dto_lock, flags);
229
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500230 rq_cq_reap(xprt);
231 sq_cq_reap(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600232
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400233 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600234 spin_lock_irqsave(&dto_lock, flags);
235 }
236 spin_unlock_irqrestore(&dto_lock, flags);
237}
238
239/*
240 * Receive Queue Completion Handler
241 *
242 * Since an RQ completion handler is called on interrupt context, we
243 * need to defer the handling of the I/O to a tasklet
244 */
245static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
246{
247 struct svcxprt_rdma *xprt = cq_context;
248 unsigned long flags;
249
Tom Tucker17113862008-05-01 11:13:50 -0500250 /* Guard against unconditional flush call for destroyed QP */
251 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
252 return;
253
Tom Tucker377f9b22007-12-12 16:13:21 -0600254 /*
255 * Set the bit regardless of whether or not it's on the list
256 * because it may be on the list already due to an SQ
257 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500258 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600259 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
260
261 /*
262 * If this transport is not already on the DTO transport queue,
263 * add it
264 */
265 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400266 if (list_empty(&xprt->sc_dto_q)) {
267 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600268 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400269 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600270 spin_unlock_irqrestore(&dto_lock, flags);
271
272 /* Tasklet does all the work to avoid irqsave locks. */
273 tasklet_schedule(&dto_tasklet);
274}
275
276/*
277 * rq_cq_reap - Process the RQ CQ.
278 *
279 * Take all completing WC off the CQE and enqueue the associated DTO
280 * context on the dto_q for the transport.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500281 *
282 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600283 */
284static void rq_cq_reap(struct svcxprt_rdma *xprt)
285{
286 int ret;
287 struct ib_wc wc;
288 struct svc_rdma_op_ctxt *ctxt = NULL;
289
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500290 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
291 return;
292
293 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600294 atomic_inc(&rdma_stat_rq_poll);
295
Tom Tucker377f9b22007-12-12 16:13:21 -0600296 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
297 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
298 ctxt->wc_status = wc.status;
299 ctxt->byte_len = wc.byte_len;
Tom Tuckere6ab9142008-05-28 12:08:48 -0500300 svc_rdma_unmap_dma(ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600301 if (wc.status != IB_WC_SUCCESS) {
302 /* Close the transport */
Tom Tucker0905c0f2008-05-01 10:49:03 -0500303 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600304 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
305 svc_rdma_put_context(ctxt, 1);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500306 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600307 continue;
308 }
Tom Tucker47698e02008-05-06 11:49:05 -0500309 spin_lock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600310 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
Tom Tucker47698e02008-05-06 11:49:05 -0500311 spin_unlock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500312 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600313 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600314
315 if (ctxt)
316 atomic_inc(&rdma_stat_rq_prod);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500317
318 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
319 /*
320 * If data arrived before established event,
321 * don't enqueue. This defers RPC I/O until the
322 * RDMA connection is complete.
323 */
324 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
325 svc_xprt_enqueue(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600326}
327
328/*
Tom Tuckere1183212008-10-03 15:22:18 -0500329 * Processs a completion context
330 */
331static void process_context(struct svcxprt_rdma *xprt,
332 struct svc_rdma_op_ctxt *ctxt)
333{
334 svc_rdma_unmap_dma(ctxt);
335
336 switch (ctxt->wr_op) {
337 case IB_WR_SEND:
338 svc_rdma_put_context(ctxt, 1);
339 break;
340
341 case IB_WR_RDMA_WRITE:
342 svc_rdma_put_context(ctxt, 0);
343 break;
344
345 case IB_WR_RDMA_READ:
346 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
347 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
348 BUG_ON(!read_hdr);
349 spin_lock_bh(&xprt->sc_rq_dto_lock);
350 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
351 list_add_tail(&read_hdr->dto_q,
352 &xprt->sc_read_complete_q);
353 spin_unlock_bh(&xprt->sc_rq_dto_lock);
354 svc_xprt_enqueue(&xprt->sc_xprt);
355 }
356 svc_rdma_put_context(ctxt, 0);
357 break;
358
359 default:
360 printk(KERN_ERR "svcrdma: unexpected completion type, "
361 "opcode=%d\n",
362 ctxt->wr_op);
363 break;
364 }
365}
366
367/*
Tom Tucker377f9b22007-12-12 16:13:21 -0600368 * Send Queue Completion Handler - potentially called on interrupt context.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500369 *
370 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600371 */
372static void sq_cq_reap(struct svcxprt_rdma *xprt)
373{
374 struct svc_rdma_op_ctxt *ctxt = NULL;
375 struct ib_wc wc;
376 struct ib_cq *cq = xprt->sc_sq_cq;
377 int ret;
378
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500379 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
380 return;
381
382 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600383 atomic_inc(&rdma_stat_sq_poll);
384 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600385 if (wc.status != IB_WC_SUCCESS)
386 /* Close the transport */
387 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
388
389 /* Decrement used SQ WR count */
390 atomic_dec(&xprt->sc_sq_count);
391 wake_up(&xprt->sc_send_wait);
392
Tom Tuckere1183212008-10-03 15:22:18 -0500393 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
394 if (ctxt)
395 process_context(xprt, ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600396
Tom Tucker0905c0f2008-05-01 10:49:03 -0500397 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600398 }
399
400 if (ctxt)
401 atomic_inc(&rdma_stat_sq_prod);
402}
403
404static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
405{
406 struct svcxprt_rdma *xprt = cq_context;
407 unsigned long flags;
408
Tom Tucker17113862008-05-01 11:13:50 -0500409 /* Guard against unconditional flush call for destroyed QP */
410 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
411 return;
412
Tom Tucker377f9b22007-12-12 16:13:21 -0600413 /*
414 * Set the bit regardless of whether or not it's on the list
415 * because it may be on the list already due to an RQ
416 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500417 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600418 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
419
420 /*
421 * If this transport is not already on the DTO transport queue,
422 * add it
423 */
424 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400425 if (list_empty(&xprt->sc_dto_q)) {
426 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600427 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400428 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600429 spin_unlock_irqrestore(&dto_lock, flags);
430
431 /* Tasklet does all the work to avoid irqsave locks. */
432 tasklet_schedule(&dto_tasklet);
433}
434
Tom Tucker377f9b22007-12-12 16:13:21 -0600435static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
436 int listener)
437{
438 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
439
440 if (!cma_xprt)
441 return NULL;
442 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
443 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
444 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
445 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
446 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
Tom Tucker64be86082008-10-06 14:45:18 -0500447 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
Tom Tucker377f9b22007-12-12 16:13:21 -0600448 init_waitqueue_head(&cma_xprt->sc_send_wait);
449
450 spin_lock_init(&cma_xprt->sc_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600451 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
Tom Tucker64be86082008-10-06 14:45:18 -0500452 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600453
454 cma_xprt->sc_ord = svcrdma_ord;
455
456 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
457 cma_xprt->sc_max_requests = svcrdma_max_requests;
458 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
459 atomic_set(&cma_xprt->sc_sq_count, 0);
Tom Tucker87295b62008-05-28 13:17:44 -0500460 atomic_set(&cma_xprt->sc_ctxt_used, 0);
Tom Tucker377f9b22007-12-12 16:13:21 -0600461
Tom Tucker89488962008-05-28 15:14:02 -0500462 if (listener)
Tom Tucker377f9b22007-12-12 16:13:21 -0600463 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
464
465 return cma_xprt;
466}
467
468struct page *svc_rdma_get_page(void)
469{
470 struct page *page;
471
472 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
473 /* If we can't get memory, wait a bit and try again */
474 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
475 "jiffies.\n");
476 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
477 }
478 return page;
479}
480
481int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
482{
483 struct ib_recv_wr recv_wr, *bad_recv_wr;
484 struct svc_rdma_op_ctxt *ctxt;
485 struct page *page;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500486 dma_addr_t pa;
Tom Tucker377f9b22007-12-12 16:13:21 -0600487 int sge_no;
488 int buflen;
489 int ret;
490
491 ctxt = svc_rdma_get_context(xprt);
492 buflen = 0;
493 ctxt->direction = DMA_FROM_DEVICE;
494 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
495 BUG_ON(sge_no >= xprt->sc_max_sge);
496 page = svc_rdma_get_page();
497 ctxt->pages[sge_no] = page;
498 pa = ib_dma_map_page(xprt->sc_cm_id->device,
499 page, 0, PAGE_SIZE,
500 DMA_FROM_DEVICE);
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500501 if (ib_dma_mapping_error(xprt->sc_cm_id->device, pa))
502 goto err_put_ctxt;
503 atomic_inc(&xprt->sc_dma_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600504 ctxt->sge[sge_no].addr = pa;
505 ctxt->sge[sge_no].length = PAGE_SIZE;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500506 ctxt->sge[sge_no].lkey = xprt->sc_dma_lkey;
Tom Tucker377f9b22007-12-12 16:13:21 -0600507 buflen += PAGE_SIZE;
508 }
509 ctxt->count = sge_no;
510 recv_wr.next = NULL;
511 recv_wr.sg_list = &ctxt->sge[0];
512 recv_wr.num_sge = ctxt->count;
513 recv_wr.wr_id = (u64)(unsigned long)ctxt;
514
Tom Tucker0905c0f2008-05-01 10:49:03 -0500515 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600516 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500517 if (ret) {
518 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker05a08262008-04-25 14:11:31 -0500519 svc_rdma_put_context(ctxt, 1);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500520 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600521 return ret;
Tom Tuckera5abf4e2008-09-30 14:05:41 -0500522
523 err_put_ctxt:
524 svc_rdma_put_context(ctxt, 1);
525 return -ENOMEM;
Tom Tucker377f9b22007-12-12 16:13:21 -0600526}
527
528/*
529 * This function handles the CONNECT_REQUEST event on a listening
530 * endpoint. It is passed the cma_id for the _new_ connection. The context in
531 * this cma_id is inherited from the listening cma_id and is the svc_xprt
532 * structure for the listening endpoint.
533 *
534 * This function creates a new xprt for the new connection and enqueues it on
535 * the accept queue for the listent xprt. When the listen thread is kicked, it
536 * will call the recvfrom method on the listen xprt which will accept the new
537 * connection.
538 */
Tom Tucker36ef25e2008-05-19 19:00:24 -0500539static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
Tom Tucker377f9b22007-12-12 16:13:21 -0600540{
541 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
542 struct svcxprt_rdma *newxprt;
Tom Tuckeraf261af2008-05-07 13:52:42 -0500543 struct sockaddr *sa;
Tom Tucker377f9b22007-12-12 16:13:21 -0600544
545 /* Create a new transport */
546 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
547 if (!newxprt) {
548 dprintk("svcrdma: failed to create new transport\n");
549 return;
550 }
551 newxprt->sc_cm_id = new_cma_id;
552 new_cma_id->context = newxprt;
553 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
554 newxprt, newxprt->sc_cm_id, listen_xprt);
555
Tom Tucker36ef25e2008-05-19 19:00:24 -0500556 /* Save client advertised inbound read limit for use later in accept. */
557 newxprt->sc_ord = client_ird;
558
Tom Tuckeraf261af2008-05-07 13:52:42 -0500559 /* Set the local and remote addresses in the transport */
560 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
561 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
562 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
563 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
564
Tom Tucker377f9b22007-12-12 16:13:21 -0600565 /*
566 * Enqueue the new transport on the accept queue of the listening
567 * transport
568 */
569 spin_lock_bh(&listen_xprt->sc_lock);
570 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
571 spin_unlock_bh(&listen_xprt->sc_lock);
572
573 /*
574 * Can't use svc_xprt_received here because we are not on a
575 * rqstp thread
576 */
577 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
578 svc_xprt_enqueue(&listen_xprt->sc_xprt);
579}
580
581/*
582 * Handles events generated on the listening endpoint. These events will be
583 * either be incoming connect requests or adapter removal events.
584 */
585static int rdma_listen_handler(struct rdma_cm_id *cma_id,
586 struct rdma_cm_event *event)
587{
588 struct svcxprt_rdma *xprt = cma_id->context;
589 int ret = 0;
590
591 switch (event->event) {
592 case RDMA_CM_EVENT_CONNECT_REQUEST:
593 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
594 "event=%d\n", cma_id, cma_id->context, event->event);
Tom Tucker36ef25e2008-05-19 19:00:24 -0500595 handle_connect_req(cma_id,
596 event->param.conn.responder_resources);
Tom Tucker377f9b22007-12-12 16:13:21 -0600597 break;
598
599 case RDMA_CM_EVENT_ESTABLISHED:
600 /* Accept complete */
601 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
602 "cm_id=%p\n", xprt, cma_id);
603 break;
604
605 case RDMA_CM_EVENT_DEVICE_REMOVAL:
606 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
607 xprt, cma_id);
608 if (xprt)
609 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
610 break;
611
612 default:
613 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
614 "event=%d\n", cma_id, event->event);
615 break;
616 }
617
618 return ret;
619}
620
621static int rdma_cma_handler(struct rdma_cm_id *cma_id,
622 struct rdma_cm_event *event)
623{
624 struct svc_xprt *xprt = cma_id->context;
625 struct svcxprt_rdma *rdma =
626 container_of(xprt, struct svcxprt_rdma, sc_xprt);
627 switch (event->event) {
628 case RDMA_CM_EVENT_ESTABLISHED:
629 /* Accept complete */
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400630 svc_xprt_get(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600631 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
632 "cm_id=%p\n", xprt, cma_id);
633 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
634 svc_xprt_enqueue(xprt);
635 break;
636 case RDMA_CM_EVENT_DISCONNECTED:
637 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
638 xprt, cma_id);
639 if (xprt) {
640 set_bit(XPT_CLOSE, &xprt->xpt_flags);
641 svc_xprt_enqueue(xprt);
Tom Tucker120693d2008-04-24 14:17:21 -0500642 svc_xprt_put(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600643 }
644 break;
645 case RDMA_CM_EVENT_DEVICE_REMOVAL:
646 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
647 "event=%d\n", cma_id, xprt, event->event);
648 if (xprt) {
649 set_bit(XPT_CLOSE, &xprt->xpt_flags);
650 svc_xprt_enqueue(xprt);
651 }
652 break;
653 default:
654 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
655 "event=%d\n", cma_id, event->event);
656 break;
657 }
658 return 0;
659}
660
661/*
662 * Create a listening RDMA service endpoint.
663 */
664static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
665 struct sockaddr *sa, int salen,
666 int flags)
667{
668 struct rdma_cm_id *listen_id;
669 struct svcxprt_rdma *cma_xprt;
670 struct svc_xprt *xprt;
671 int ret;
672
673 dprintk("svcrdma: Creating RDMA socket\n");
674
675 cma_xprt = rdma_create_xprt(serv, 1);
676 if (!cma_xprt)
Tom Tucker58e8f622008-05-06 09:45:54 -0500677 return ERR_PTR(-ENOMEM);
Tom Tucker377f9b22007-12-12 16:13:21 -0600678 xprt = &cma_xprt->sc_xprt;
679
680 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
681 if (IS_ERR(listen_id)) {
Tom Tucker58e8f622008-05-06 09:45:54 -0500682 ret = PTR_ERR(listen_id);
683 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
684 goto err0;
Tom Tucker377f9b22007-12-12 16:13:21 -0600685 }
Tom Tucker58e8f622008-05-06 09:45:54 -0500686
Tom Tucker377f9b22007-12-12 16:13:21 -0600687 ret = rdma_bind_addr(listen_id, sa);
688 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600689 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500690 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600691 }
692 cma_xprt->sc_cm_id = listen_id;
693
694 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
695 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600696 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500697 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600698 }
699
700 /*
701 * We need to use the address from the cm_id in case the
702 * caller specified 0 for the port number.
703 */
704 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
705 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
706
707 return &cma_xprt->sc_xprt;
Tom Tucker58e8f622008-05-06 09:45:54 -0500708
709 err1:
710 rdma_destroy_id(listen_id);
711 err0:
712 kfree(cma_xprt);
713 return ERR_PTR(ret);
Tom Tucker377f9b22007-12-12 16:13:21 -0600714}
715
Tom Tucker64be86082008-10-06 14:45:18 -0500716static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
717{
718 struct ib_mr *mr;
719 struct ib_fast_reg_page_list *pl;
720 struct svc_rdma_fastreg_mr *frmr;
721
722 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
723 if (!frmr)
724 goto err;
725
726 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
727 if (!mr)
728 goto err_free_frmr;
729
730 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
731 RPCSVC_MAXPAGES);
732 if (!pl)
733 goto err_free_mr;
734
735 frmr->mr = mr;
736 frmr->page_list = pl;
737 INIT_LIST_HEAD(&frmr->frmr_list);
738 return frmr;
739
740 err_free_mr:
741 ib_dereg_mr(mr);
742 err_free_frmr:
743 kfree(frmr);
744 err:
745 return ERR_PTR(-ENOMEM);
746}
747
748static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
749{
750 struct svc_rdma_fastreg_mr *frmr;
751
752 while (!list_empty(&xprt->sc_frmr_q)) {
753 frmr = list_entry(xprt->sc_frmr_q.next,
754 struct svc_rdma_fastreg_mr, frmr_list);
755 list_del_init(&frmr->frmr_list);
756 ib_dereg_mr(frmr->mr);
757 ib_free_fast_reg_page_list(frmr->page_list);
758 kfree(frmr);
759 }
760}
761
762struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
763{
764 struct svc_rdma_fastreg_mr *frmr = NULL;
765
766 spin_lock_bh(&rdma->sc_frmr_q_lock);
767 if (!list_empty(&rdma->sc_frmr_q)) {
768 frmr = list_entry(rdma->sc_frmr_q.next,
769 struct svc_rdma_fastreg_mr, frmr_list);
770 list_del_init(&frmr->frmr_list);
771 frmr->map_len = 0;
772 frmr->page_list_len = 0;
773 }
774 spin_unlock_bh(&rdma->sc_frmr_q_lock);
775 if (frmr)
776 return frmr;
777
778 return rdma_alloc_frmr(rdma);
779}
780
781static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
782 struct svc_rdma_fastreg_mr *frmr)
783{
784 int page_no;
785 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
786 dma_addr_t addr = frmr->page_list->page_list[page_no];
787 if (ib_dma_mapping_error(frmr->mr->device, addr))
788 continue;
789 atomic_dec(&xprt->sc_dma_used);
790 ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
791 frmr->direction);
792 }
793}
794
795void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
796 struct svc_rdma_fastreg_mr *frmr)
797{
798 if (frmr) {
799 frmr_unmap_dma(rdma, frmr);
800 spin_lock_bh(&rdma->sc_frmr_q_lock);
801 BUG_ON(!list_empty(&frmr->frmr_list));
802 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
803 spin_unlock_bh(&rdma->sc_frmr_q_lock);
804 }
805}
806
Tom Tucker377f9b22007-12-12 16:13:21 -0600807/*
808 * This is the xpo_recvfrom function for listening endpoints. Its
809 * purpose is to accept incoming connections. The CMA callback handler
810 * has already created a new transport and attached it to the new CMA
811 * ID.
812 *
813 * There is a queue of pending connections hung on the listening
814 * transport. This queue contains the new svc_xprt structure. This
815 * function takes svc_xprt structures off the accept_q and completes
816 * the connection.
817 */
818static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
819{
820 struct svcxprt_rdma *listen_rdma;
821 struct svcxprt_rdma *newxprt = NULL;
822 struct rdma_conn_param conn_param;
823 struct ib_qp_init_attr qp_attr;
824 struct ib_device_attr devattr;
Tom Tucker3a5c6382008-09-30 13:46:13 -0500825 int dma_mr_acc;
826 int need_dma_mr;
Tom Tucker377f9b22007-12-12 16:13:21 -0600827 int ret;
828 int i;
829
830 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
831 clear_bit(XPT_CONN, &xprt->xpt_flags);
832 /* Get the next entry off the accept list */
833 spin_lock_bh(&listen_rdma->sc_lock);
834 if (!list_empty(&listen_rdma->sc_accept_q)) {
835 newxprt = list_entry(listen_rdma->sc_accept_q.next,
836 struct svcxprt_rdma, sc_accept_q);
837 list_del_init(&newxprt->sc_accept_q);
838 }
839 if (!list_empty(&listen_rdma->sc_accept_q))
840 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
841 spin_unlock_bh(&listen_rdma->sc_lock);
842 if (!newxprt)
843 return NULL;
844
845 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
846 newxprt, newxprt->sc_cm_id);
847
848 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
849 if (ret) {
850 dprintk("svcrdma: could not query device attributes on "
851 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
852 goto errout;
853 }
854
855 /* Qualify the transport resource defaults with the
856 * capabilities of this particular device */
857 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
858 (size_t)RPCSVC_MAXPAGES);
859 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
860 (size_t)svcrdma_max_requests);
861 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
862
Tom Tucker36ef25e2008-05-19 19:00:24 -0500863 /*
864 * Limit ORD based on client limit, local device limit, and
865 * configured svcrdma limit.
866 */
867 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
868 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
Tom Tucker377f9b22007-12-12 16:13:21 -0600869
870 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
871 if (IS_ERR(newxprt->sc_pd)) {
872 dprintk("svcrdma: error creating PD for connect request\n");
873 goto errout;
874 }
875 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
876 sq_comp_handler,
877 cq_event_handler,
878 newxprt,
879 newxprt->sc_sq_depth,
880 0);
881 if (IS_ERR(newxprt->sc_sq_cq)) {
882 dprintk("svcrdma: error creating SQ CQ for connect request\n");
883 goto errout;
884 }
885 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
886 rq_comp_handler,
887 cq_event_handler,
888 newxprt,
889 newxprt->sc_max_requests,
890 0);
891 if (IS_ERR(newxprt->sc_rq_cq)) {
892 dprintk("svcrdma: error creating RQ CQ for connect request\n");
893 goto errout;
894 }
895
896 memset(&qp_attr, 0, sizeof qp_attr);
897 qp_attr.event_handler = qp_event_handler;
898 qp_attr.qp_context = &newxprt->sc_xprt;
899 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
900 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
901 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
902 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
903 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
904 qp_attr.qp_type = IB_QPT_RC;
905 qp_attr.send_cq = newxprt->sc_sq_cq;
906 qp_attr.recv_cq = newxprt->sc_rq_cq;
907 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
908 " cm_id->device=%p, sc_pd->device=%p\n"
909 " cap.max_send_wr = %d\n"
910 " cap.max_recv_wr = %d\n"
911 " cap.max_send_sge = %d\n"
912 " cap.max_recv_sge = %d\n",
913 newxprt->sc_cm_id, newxprt->sc_pd,
914 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
915 qp_attr.cap.max_send_wr,
916 qp_attr.cap.max_recv_wr,
917 qp_attr.cap.max_send_sge,
918 qp_attr.cap.max_recv_sge);
919
920 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
921 if (ret) {
922 /*
923 * XXX: This is a hack. We need a xx_request_qp interface
924 * that will adjust the qp_attr's with a best-effort
925 * number
926 */
927 qp_attr.cap.max_send_sge -= 2;
928 qp_attr.cap.max_recv_sge -= 2;
929 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
930 &qp_attr);
931 if (ret) {
932 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
933 goto errout;
934 }
935 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
936 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
937 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
938 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
939 }
940 newxprt->sc_qp = newxprt->sc_cm_id->qp;
941
Tom Tucker3a5c6382008-09-30 13:46:13 -0500942 /*
943 * Use the most secure set of MR resources based on the
944 * transport type and available memory management features in
945 * the device. Here's the table implemented below:
946 *
947 * Fast Global DMA Remote WR
948 * Reg LKEY MR Access
949 * Sup'd Sup'd Needed Needed
950 *
951 * IWARP N N Y Y
952 * N Y Y Y
953 * Y N Y N
954 * Y Y N -
955 *
956 * IB N N Y N
957 * N Y N -
958 * Y N Y N
959 * Y Y N -
960 *
961 * NB: iWARP requires remote write access for the data sink
962 * of an RDMA_READ. IB does not.
963 */
964 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
965 newxprt->sc_frmr_pg_list_len =
966 devattr.max_fast_reg_page_list_len;
967 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
968 }
969
970 /*
971 * Determine if a DMA MR is required and if so, what privs are required
972 */
973 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
974 case RDMA_TRANSPORT_IWARP:
975 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
976 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
977 need_dma_mr = 1;
978 dma_mr_acc =
979 (IB_ACCESS_LOCAL_WRITE |
980 IB_ACCESS_REMOTE_WRITE);
981 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
982 need_dma_mr = 1;
983 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
984 } else
985 need_dma_mr = 0;
986 break;
987 case RDMA_TRANSPORT_IB:
988 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
989 need_dma_mr = 1;
990 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
991 } else
992 need_dma_mr = 0;
993 break;
994 default:
Tom Tucker377f9b22007-12-12 16:13:21 -0600995 goto errout;
996 }
997
Tom Tucker3a5c6382008-09-30 13:46:13 -0500998 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
999 if (need_dma_mr) {
1000 /* Register all of physical memory */
1001 newxprt->sc_phys_mr =
1002 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
1003 if (IS_ERR(newxprt->sc_phys_mr)) {
1004 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
1005 ret);
1006 goto errout;
1007 }
1008 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
1009 } else
1010 newxprt->sc_dma_lkey =
1011 newxprt->sc_cm_id->device->local_dma_lkey;
1012
Tom Tucker377f9b22007-12-12 16:13:21 -06001013 /* Post receive buffers */
1014 for (i = 0; i < newxprt->sc_max_requests; i++) {
1015 ret = svc_rdma_post_recv(newxprt);
1016 if (ret) {
1017 dprintk("svcrdma: failure posting receive buffers\n");
1018 goto errout;
1019 }
1020 }
1021
1022 /* Swap out the handler */
1023 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1024
Tom Tuckeraf261af2008-05-07 13:52:42 -05001025 /*
1026 * Arm the CQs for the SQ and RQ before accepting so we can't
1027 * miss the first message
1028 */
1029 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1030 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1031
Tom Tucker377f9b22007-12-12 16:13:21 -06001032 /* Accept Connection */
1033 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1034 memset(&conn_param, 0, sizeof conn_param);
1035 conn_param.responder_resources = 0;
1036 conn_param.initiator_depth = newxprt->sc_ord;
1037 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1038 if (ret) {
1039 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1040 ret);
1041 goto errout;
1042 }
1043
1044 dprintk("svcrdma: new connection %p accepted with the following "
1045 "attributes:\n"
1046 " local_ip : %d.%d.%d.%d\n"
1047 " local_port : %d\n"
1048 " remote_ip : %d.%d.%d.%d\n"
1049 " remote_port : %d\n"
1050 " max_sge : %d\n"
1051 " sq_depth : %d\n"
1052 " max_requests : %d\n"
1053 " ord : %d\n",
1054 newxprt,
1055 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1056 route.addr.src_addr)->sin_addr.s_addr),
1057 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1058 route.addr.src_addr)->sin_port),
1059 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1060 route.addr.dst_addr)->sin_addr.s_addr),
1061 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1062 route.addr.dst_addr)->sin_port),
1063 newxprt->sc_max_sge,
1064 newxprt->sc_sq_depth,
1065 newxprt->sc_max_requests,
1066 newxprt->sc_ord);
1067
Tom Tucker377f9b22007-12-12 16:13:21 -06001068 return &newxprt->sc_xprt;
1069
1070 errout:
1071 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001072 /* Take a reference in case the DTO handler runs */
1073 svc_xprt_get(&newxprt->sc_xprt);
Tom Tucker17113862008-05-01 11:13:50 -05001074 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001075 ib_destroy_qp(newxprt->sc_qp);
Tom Tucker377f9b22007-12-12 16:13:21 -06001076 rdma_destroy_id(newxprt->sc_cm_id);
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001077 /* This call to put will destroy the transport */
1078 svc_xprt_put(&newxprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001079 return NULL;
1080}
1081
Tom Tucker377f9b22007-12-12 16:13:21 -06001082static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1083{
Tom Tucker377f9b22007-12-12 16:13:21 -06001084}
1085
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001086/*
Tom Tucker17113862008-05-01 11:13:50 -05001087 * When connected, an svc_xprt has at least two references:
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001088 *
1089 * - A reference held by the cm_id between the ESTABLISHED and
1090 * DISCONNECTED events. If the remote peer disconnected first, this
1091 * reference could be gone.
1092 *
1093 * - A reference held by the svc_recv code that called this function
1094 * as part of close processing.
1095 *
Tom Tucker17113862008-05-01 11:13:50 -05001096 * At a minimum one references should still be held.
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001097 */
Tom Tucker377f9b22007-12-12 16:13:21 -06001098static void svc_rdma_detach(struct svc_xprt *xprt)
1099{
1100 struct svcxprt_rdma *rdma =
1101 container_of(xprt, struct svcxprt_rdma, sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001102 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001103
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001104 /* Disconnect and flush posted WQE */
1105 rdma_disconnect(rdma->sc_cm_id);
Tom Tucker377f9b22007-12-12 16:13:21 -06001106}
1107
Tom Tucker8da91ea2008-04-30 22:00:46 -05001108static void __svc_rdma_free(struct work_struct *work)
Tom Tucker377f9b22007-12-12 16:13:21 -06001109{
Tom Tucker8da91ea2008-04-30 22:00:46 -05001110 struct svcxprt_rdma *rdma =
1111 container_of(work, struct svcxprt_rdma, sc_work);
Tom Tucker377f9b22007-12-12 16:13:21 -06001112 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
Tom Tucker8da91ea2008-04-30 22:00:46 -05001113
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001114 /* We should only be called from kref_put */
Tom Tucker8da91ea2008-04-30 22:00:46 -05001115 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1116
Tom Tucker356d0a12008-05-01 11:25:02 -05001117 /*
1118 * Destroy queued, but not processed read completions. Note
1119 * that this cleanup has to be done before destroying the
1120 * cm_id because the device ptr is needed to unmap the dma in
1121 * svc_rdma_put_context.
1122 */
Tom Tucker356d0a12008-05-01 11:25:02 -05001123 while (!list_empty(&rdma->sc_read_complete_q)) {
1124 struct svc_rdma_op_ctxt *ctxt;
1125 ctxt = list_entry(rdma->sc_read_complete_q.next,
1126 struct svc_rdma_op_ctxt,
1127 dto_q);
1128 list_del_init(&ctxt->dto_q);
1129 svc_rdma_put_context(ctxt, 1);
1130 }
Tom Tucker356d0a12008-05-01 11:25:02 -05001131
1132 /* Destroy queued, but not processed recv completions */
Tom Tucker356d0a12008-05-01 11:25:02 -05001133 while (!list_empty(&rdma->sc_rq_dto_q)) {
1134 struct svc_rdma_op_ctxt *ctxt;
1135 ctxt = list_entry(rdma->sc_rq_dto_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);
1140 }
Tom Tucker356d0a12008-05-01 11:25:02 -05001141
1142 /* Warn if we leaked a resource or under-referenced */
1143 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
Tom Tucker87295b62008-05-28 13:17:44 -05001144 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
Tom Tucker356d0a12008-05-01 11:25:02 -05001145
Tom Tucker64be86082008-10-06 14:45:18 -05001146 /* De-allocate fastreg mr */
1147 rdma_dealloc_frmr_q(rdma);
1148
Tom Tucker17113862008-05-01 11:13:50 -05001149 /* Destroy the QP if present (not a listener) */
1150 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1151 ib_destroy_qp(rdma->sc_qp);
1152
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001153 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1154 ib_destroy_cq(rdma->sc_sq_cq);
1155
1156 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1157 ib_destroy_cq(rdma->sc_rq_cq);
1158
1159 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1160 ib_dereg_mr(rdma->sc_phys_mr);
1161
1162 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1163 ib_dealloc_pd(rdma->sc_pd);
1164
Tom Tucker356d0a12008-05-01 11:25:02 -05001165 /* Destroy the CM ID */
1166 rdma_destroy_id(rdma->sc_cm_id);
1167
Tom Tucker377f9b22007-12-12 16:13:21 -06001168 kfree(rdma);
1169}
1170
Tom Tucker8da91ea2008-04-30 22:00:46 -05001171static void svc_rdma_free(struct svc_xprt *xprt)
1172{
1173 struct svcxprt_rdma *rdma =
1174 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1175 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1176 schedule_work(&rdma->sc_work);
1177}
1178
Tom Tucker377f9b22007-12-12 16:13:21 -06001179static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1180{
1181 struct svcxprt_rdma *rdma =
1182 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1183
1184 /*
1185 * If there are fewer SQ WR available than required to send a
1186 * simple response, return false.
1187 */
1188 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1189 return 0;
1190
1191 /*
1192 * ...or there are already waiters on the SQ,
1193 * return false.
1194 */
1195 if (waitqueue_active(&rdma->sc_send_wait))
1196 return 0;
1197
1198 /* Otherwise return true. */
1199 return 1;
1200}
1201
Tom Tuckere1183212008-10-03 15:22:18 -05001202/*
1203 * Attempt to register the kvec representing the RPC memory with the
1204 * device.
1205 *
1206 * Returns:
1207 * NULL : The device does not support fastreg or there were no more
1208 * fastreg mr.
1209 * frmr : The kvec register request was successfully posted.
1210 * <0 : An error was encountered attempting to register the kvec.
1211 */
1212int svc_rdma_fastreg(struct svcxprt_rdma *xprt,
1213 struct svc_rdma_fastreg_mr *frmr)
1214{
1215 struct ib_send_wr fastreg_wr;
1216 u8 key;
1217
1218 /* Bump the key */
1219 key = (u8)(frmr->mr->lkey & 0x000000FF);
1220 ib_update_fast_reg_key(frmr->mr, ++key);
1221
1222 /* Prepare FASTREG WR */
1223 memset(&fastreg_wr, 0, sizeof fastreg_wr);
1224 fastreg_wr.opcode = IB_WR_FAST_REG_MR;
1225 fastreg_wr.send_flags = IB_SEND_SIGNALED;
1226 fastreg_wr.wr.fast_reg.iova_start = (unsigned long)frmr->kva;
1227 fastreg_wr.wr.fast_reg.page_list = frmr->page_list;
1228 fastreg_wr.wr.fast_reg.page_list_len = frmr->page_list_len;
1229 fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
1230 fastreg_wr.wr.fast_reg.length = frmr->map_len;
1231 fastreg_wr.wr.fast_reg.access_flags = frmr->access_flags;
1232 fastreg_wr.wr.fast_reg.rkey = frmr->mr->lkey;
1233 return svc_rdma_send(xprt, &fastreg_wr);
1234}
1235
Tom Tucker377f9b22007-12-12 16:13:21 -06001236int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1237{
1238 struct ib_send_wr *bad_wr;
1239 int ret;
1240
1241 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
Tom Tucker9d6347a2008-04-25 15:51:27 -05001242 return -ENOTCONN;
Tom Tucker377f9b22007-12-12 16:13:21 -06001243
1244 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
Tom Tucker377f9b22007-12-12 16:13:21 -06001245 /* If the SQ is full, wait until an SQ entry is available */
1246 while (1) {
1247 spin_lock_bh(&xprt->sc_lock);
1248 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1249 spin_unlock_bh(&xprt->sc_lock);
1250 atomic_inc(&rdma_stat_sq_starve);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -05001251
1252 /* See if we can opportunistically reap SQ WR to make room */
Tom Tucker377f9b22007-12-12 16:13:21 -06001253 sq_cq_reap(xprt);
1254
1255 /* Wait until SQ WR available if SQ still full */
1256 wait_event(xprt->sc_send_wait,
1257 atomic_read(&xprt->sc_sq_count) <
1258 xprt->sc_sq_depth);
Tom Tucker830bb592008-03-11 12:44:27 -05001259 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1260 return 0;
Tom Tucker377f9b22007-12-12 16:13:21 -06001261 continue;
1262 }
1263 /* Bumped used SQ WR count and post */
Tom Tucker0905c0f2008-05-01 10:49:03 -05001264 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001265 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1266 if (!ret)
1267 atomic_inc(&xprt->sc_sq_count);
Tom Tucker0905c0f2008-05-01 10:49:03 -05001268 else {
1269 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001270 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1271 "sc_sq_count=%d, sc_sq_depth=%d\n",
1272 ret, atomic_read(&xprt->sc_sq_count),
1273 xprt->sc_sq_depth);
Tom Tucker0905c0f2008-05-01 10:49:03 -05001274 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001275 spin_unlock_bh(&xprt->sc_lock);
1276 break;
1277 }
1278 return ret;
1279}
1280
Tom Tucker008fdbc2008-05-07 15:47:42 -05001281void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1282 enum rpcrdma_errcode err)
Tom Tucker377f9b22007-12-12 16:13:21 -06001283{
1284 struct ib_send_wr err_wr;
1285 struct ib_sge sge;
1286 struct page *p;
1287 struct svc_rdma_op_ctxt *ctxt;
1288 u32 *va;
1289 int length;
1290 int ret;
1291
1292 p = svc_rdma_get_page();
1293 va = page_address(p);
1294
1295 /* XDR encode error */
1296 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1297
1298 /* Prepare SGE for local address */
Tom Tucker87295b62008-05-28 13:17:44 -05001299 atomic_inc(&xprt->sc_dma_used);
Tom Tucker377f9b22007-12-12 16:13:21 -06001300 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1301 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1302 sge.lkey = xprt->sc_phys_mr->lkey;
1303 sge.length = length;
1304
1305 ctxt = svc_rdma_get_context(xprt);
1306 ctxt->count = 1;
1307 ctxt->pages[0] = p;
1308
1309 /* Prepare SEND WR */
1310 memset(&err_wr, 0, sizeof err_wr);
1311 ctxt->wr_op = IB_WR_SEND;
1312 err_wr.wr_id = (unsigned long)ctxt;
1313 err_wr.sg_list = &sge;
1314 err_wr.num_sge = 1;
1315 err_wr.opcode = IB_WR_SEND;
1316 err_wr.send_flags = IB_SEND_SIGNALED;
1317
1318 /* Post It */
1319 ret = svc_rdma_send(xprt, &err_wr);
1320 if (ret) {
Tom Tucker008fdbc2008-05-07 15:47:42 -05001321 dprintk("svcrdma: Error %d posting send for protocol error\n",
1322 ret);
Tom Tucker377f9b22007-12-12 16:13:21 -06001323 svc_rdma_put_context(ctxt, 1);
1324 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001325}