blob: fc86338bcbb27fc1d39db412d0a2b84009f512c0 [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
87static int rdma_bump_context_cache(struct svcxprt_rdma *xprt)
88{
89 int target;
90 int at_least_one = 0;
91 struct svc_rdma_op_ctxt *ctxt;
92
93 target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump,
94 xprt->sc_ctxt_max);
95
96 spin_lock_bh(&xprt->sc_ctxt_lock);
97 while (xprt->sc_ctxt_cnt < target) {
98 xprt->sc_ctxt_cnt++;
99 spin_unlock_bh(&xprt->sc_ctxt_lock);
100
101 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
102
103 spin_lock_bh(&xprt->sc_ctxt_lock);
104 if (ctxt) {
105 at_least_one = 1;
Tom Tucker87407672008-04-30 20:44:39 -0500106 INIT_LIST_HEAD(&ctxt->free_list);
107 list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
Tom Tucker377f9b22007-12-12 16:13:21 -0600108 } else {
109 /* kmalloc failed...give up for now */
110 xprt->sc_ctxt_cnt--;
111 break;
112 }
113 }
114 spin_unlock_bh(&xprt->sc_ctxt_lock);
115 dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n",
116 xprt->sc_ctxt_max, xprt->sc_ctxt_cnt);
117 return at_least_one;
118}
119
120struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt)
121{
122 struct svc_rdma_op_ctxt *ctxt;
123
124 while (1) {
125 spin_lock_bh(&xprt->sc_ctxt_lock);
Tom Tucker87407672008-04-30 20:44:39 -0500126 if (unlikely(list_empty(&xprt->sc_ctxt_free))) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600127 /* Try to bump my cache. */
128 spin_unlock_bh(&xprt->sc_ctxt_lock);
129
130 if (rdma_bump_context_cache(xprt))
131 continue;
132
133 printk(KERN_INFO "svcrdma: sleeping waiting for "
134 "context memory on xprt=%p\n",
135 xprt);
136 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
137 continue;
138 }
Tom Tucker87407672008-04-30 20:44:39 -0500139 ctxt = list_entry(xprt->sc_ctxt_free.next,
140 struct svc_rdma_op_ctxt,
141 free_list);
142 list_del_init(&ctxt->free_list);
Tom Tucker377f9b22007-12-12 16:13:21 -0600143 spin_unlock_bh(&xprt->sc_ctxt_lock);
144 ctxt->xprt = xprt;
145 INIT_LIST_HEAD(&ctxt->dto_q);
146 ctxt->count = 0;
Tom Tucker87407672008-04-30 20:44:39 -0500147 atomic_inc(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600148 break;
149 }
150 return ctxt;
151}
152
153void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages)
154{
155 struct svcxprt_rdma *xprt;
156 int i;
157
158 BUG_ON(!ctxt);
159 xprt = ctxt->xprt;
160 if (free_pages)
161 for (i = 0; i < ctxt->count; i++)
162 put_page(ctxt->pages[i]);
163
164 for (i = 0; i < ctxt->count; i++)
Tom Tucker97a3df32008-05-01 14:02:45 -0500165 ib_dma_unmap_single(xprt->sc_cm_id->device,
166 ctxt->sge[i].addr,
167 ctxt->sge[i].length,
168 ctxt->direction);
Tom Tucker87407672008-04-30 20:44:39 -0500169
Tom Tucker377f9b22007-12-12 16:13:21 -0600170 spin_lock_bh(&xprt->sc_ctxt_lock);
Tom Tucker87407672008-04-30 20:44:39 -0500171 list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
Tom Tucker377f9b22007-12-12 16:13:21 -0600172 spin_unlock_bh(&xprt->sc_ctxt_lock);
Tom Tucker87407672008-04-30 20:44:39 -0500173 atomic_dec(&xprt->sc_ctxt_used);
Tom Tucker377f9b22007-12-12 16:13:21 -0600174}
175
Tom Tuckerab96ddd2008-05-28 13:54:04 -0500176/* Temporary NFS request map cache. Created in svc_rdma.c */
177extern struct kmem_cache *svc_rdma_map_cachep;
178
179/*
180 * Temporary NFS req mappings are shared across all transport
181 * instances. These are short lived and should be bounded by the number
182 * of concurrent server threads * depth of the SQ.
183 */
184struct svc_rdma_req_map *svc_rdma_get_req_map(void)
185{
186 struct svc_rdma_req_map *map;
187 while (1) {
188 map = kmem_cache_alloc(svc_rdma_map_cachep, GFP_KERNEL);
189 if (map)
190 break;
191 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
192 }
193 map->count = 0;
194 return map;
195}
196
197void svc_rdma_put_req_map(struct svc_rdma_req_map *map)
198{
199 kmem_cache_free(svc_rdma_map_cachep, map);
200}
201
Tom Tucker377f9b22007-12-12 16:13:21 -0600202/* ib_cq event handler */
203static void cq_event_handler(struct ib_event *event, void *context)
204{
205 struct svc_xprt *xprt = context;
206 dprintk("svcrdma: received CQ event id=%d, context=%p\n",
207 event->event, context);
208 set_bit(XPT_CLOSE, &xprt->xpt_flags);
209}
210
211/* QP event handler */
212static void qp_event_handler(struct ib_event *event, void *context)
213{
214 struct svc_xprt *xprt = context;
215
216 switch (event->event) {
217 /* These are considered benign events */
218 case IB_EVENT_PATH_MIG:
219 case IB_EVENT_COMM_EST:
220 case IB_EVENT_SQ_DRAINED:
221 case IB_EVENT_QP_LAST_WQE_REACHED:
222 dprintk("svcrdma: QP event %d received for QP=%p\n",
223 event->event, event->element.qp);
224 break;
225 /* These are considered fatal events */
226 case IB_EVENT_PATH_MIG_ERR:
227 case IB_EVENT_QP_FATAL:
228 case IB_EVENT_QP_REQ_ERR:
229 case IB_EVENT_QP_ACCESS_ERR:
230 case IB_EVENT_DEVICE_FATAL:
231 default:
232 dprintk("svcrdma: QP ERROR event %d received for QP=%p, "
233 "closing transport\n",
234 event->event, event->element.qp);
235 set_bit(XPT_CLOSE, &xprt->xpt_flags);
236 break;
237 }
238}
239
240/*
241 * Data Transfer Operation Tasklet
242 *
243 * Walks a list of transports with I/O pending, removing entries as
244 * they are added to the server's I/O pending list. Two bits indicate
245 * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave
246 * spinlock that serializes access to the transport list with the RQ
247 * and SQ interrupt handlers.
248 */
249static void dto_tasklet_func(unsigned long data)
250{
251 struct svcxprt_rdma *xprt;
252 unsigned long flags;
253
254 spin_lock_irqsave(&dto_lock, flags);
255 while (!list_empty(&dto_xprt_q)) {
256 xprt = list_entry(dto_xprt_q.next,
257 struct svcxprt_rdma, sc_dto_q);
258 list_del_init(&xprt->sc_dto_q);
259 spin_unlock_irqrestore(&dto_lock, flags);
260
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500261 rq_cq_reap(xprt);
262 sq_cq_reap(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600263
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400264 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600265 spin_lock_irqsave(&dto_lock, flags);
266 }
267 spin_unlock_irqrestore(&dto_lock, flags);
268}
269
270/*
271 * Receive Queue Completion Handler
272 *
273 * Since an RQ completion handler is called on interrupt context, we
274 * need to defer the handling of the I/O to a tasklet
275 */
276static void rq_comp_handler(struct ib_cq *cq, void *cq_context)
277{
278 struct svcxprt_rdma *xprt = cq_context;
279 unsigned long flags;
280
Tom Tucker17113862008-05-01 11:13:50 -0500281 /* Guard against unconditional flush call for destroyed QP */
282 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
283 return;
284
Tom Tucker377f9b22007-12-12 16:13:21 -0600285 /*
286 * Set the bit regardless of whether or not it's on the list
287 * because it may be on the list already due to an SQ
288 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500289 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600290 set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags);
291
292 /*
293 * If this transport is not already on the DTO transport queue,
294 * add it
295 */
296 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400297 if (list_empty(&xprt->sc_dto_q)) {
298 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600299 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400300 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600301 spin_unlock_irqrestore(&dto_lock, flags);
302
303 /* Tasklet does all the work to avoid irqsave locks. */
304 tasklet_schedule(&dto_tasklet);
305}
306
307/*
308 * rq_cq_reap - Process the RQ CQ.
309 *
310 * Take all completing WC off the CQE and enqueue the associated DTO
311 * context on the dto_q for the transport.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500312 *
313 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600314 */
315static void rq_cq_reap(struct svcxprt_rdma *xprt)
316{
317 int ret;
318 struct ib_wc wc;
319 struct svc_rdma_op_ctxt *ctxt = NULL;
320
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500321 if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags))
322 return;
323
324 ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600325 atomic_inc(&rdma_stat_rq_poll);
326
Tom Tucker377f9b22007-12-12 16:13:21 -0600327 while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) {
328 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
329 ctxt->wc_status = wc.status;
330 ctxt->byte_len = wc.byte_len;
331 if (wc.status != IB_WC_SUCCESS) {
332 /* Close the transport */
Tom Tucker0905c0f2008-05-01 10:49:03 -0500333 dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600334 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
335 svc_rdma_put_context(ctxt, 1);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500336 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600337 continue;
338 }
Tom Tucker47698e02008-05-06 11:49:05 -0500339 spin_lock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker377f9b22007-12-12 16:13:21 -0600340 list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q);
Tom Tucker47698e02008-05-06 11:49:05 -0500341 spin_unlock_bh(&xprt->sc_rq_dto_lock);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500342 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600343 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600344
345 if (ctxt)
346 atomic_inc(&rdma_stat_rq_prod);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500347
348 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
349 /*
350 * If data arrived before established event,
351 * don't enqueue. This defers RPC I/O until the
352 * RDMA connection is complete.
353 */
354 if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags))
355 svc_xprt_enqueue(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600356}
357
358/*
359 * Send Queue Completion Handler - potentially called on interrupt context.
Tom Tucker0905c0f2008-05-01 10:49:03 -0500360 *
361 * Note that caller must hold a transport reference.
Tom Tucker377f9b22007-12-12 16:13:21 -0600362 */
363static void sq_cq_reap(struct svcxprt_rdma *xprt)
364{
365 struct svc_rdma_op_ctxt *ctxt = NULL;
366 struct ib_wc wc;
367 struct ib_cq *cq = xprt->sc_sq_cq;
368 int ret;
369
Tom Tuckerdbcd00e2008-05-06 11:33:11 -0500370
371 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
372 return;
373
374 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
Tom Tucker377f9b22007-12-12 16:13:21 -0600375 atomic_inc(&rdma_stat_sq_poll);
376 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
377 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
378 xprt = ctxt->xprt;
379
380 if (wc.status != IB_WC_SUCCESS)
381 /* Close the transport */
382 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
383
384 /* Decrement used SQ WR count */
385 atomic_dec(&xprt->sc_sq_count);
386 wake_up(&xprt->sc_send_wait);
387
388 switch (ctxt->wr_op) {
389 case IB_WR_SEND:
Tom Tucker377f9b22007-12-12 16:13:21 -0600390 svc_rdma_put_context(ctxt, 1);
391 break;
392
Tom Tucker34d16e42008-07-02 14:56:13 -0500393 case IB_WR_RDMA_WRITE:
394 svc_rdma_put_context(ctxt, 0);
395 break;
396
Tom Tucker377f9b22007-12-12 16:13:21 -0600397 case IB_WR_RDMA_READ:
398 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
Tom Tucker02e74522008-04-30 19:50:56 -0500399 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
400 BUG_ON(!read_hdr);
Tom Tucker377f9b22007-12-12 16:13:21 -0600401 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
Tom Tucker377f9b22007-12-12 16:13:21 -0600402 spin_lock_bh(&xprt->sc_read_complete_lock);
Tom Tucker02e74522008-04-30 19:50:56 -0500403 list_add_tail(&read_hdr->dto_q,
Tom Tucker377f9b22007-12-12 16:13:21 -0600404 &xprt->sc_read_complete_q);
405 spin_unlock_bh(&xprt->sc_read_complete_lock);
406 svc_xprt_enqueue(&xprt->sc_xprt);
407 }
Tom Tucker02e74522008-04-30 19:50:56 -0500408 svc_rdma_put_context(ctxt, 0);
Tom Tucker377f9b22007-12-12 16:13:21 -0600409 break;
410
411 default:
412 printk(KERN_ERR "svcrdma: unexpected completion type, "
413 "opcode=%d, status=%d\n",
414 wc.opcode, wc.status);
415 break;
416 }
Tom Tucker0905c0f2008-05-01 10:49:03 -0500417 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600418 }
419
420 if (ctxt)
421 atomic_inc(&rdma_stat_sq_prod);
422}
423
424static void sq_comp_handler(struct ib_cq *cq, void *cq_context)
425{
426 struct svcxprt_rdma *xprt = cq_context;
427 unsigned long flags;
428
Tom Tucker17113862008-05-01 11:13:50 -0500429 /* Guard against unconditional flush call for destroyed QP */
430 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
431 return;
432
Tom Tucker377f9b22007-12-12 16:13:21 -0600433 /*
434 * Set the bit regardless of whether or not it's on the list
435 * because it may be on the list already due to an RQ
436 * completion.
Tom Tucker17113862008-05-01 11:13:50 -0500437 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600438 set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags);
439
440 /*
441 * If this transport is not already on the DTO transport queue,
442 * add it
443 */
444 spin_lock_irqsave(&dto_lock, flags);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400445 if (list_empty(&xprt->sc_dto_q)) {
446 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600447 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400448 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600449 spin_unlock_irqrestore(&dto_lock, flags);
450
451 /* Tasklet does all the work to avoid irqsave locks. */
452 tasklet_schedule(&dto_tasklet);
453}
454
455static void create_context_cache(struct svcxprt_rdma *xprt,
456 int ctxt_count, int ctxt_bump, int ctxt_max)
457{
458 struct svc_rdma_op_ctxt *ctxt;
459 int i;
460
461 xprt->sc_ctxt_max = ctxt_max;
462 xprt->sc_ctxt_bump = ctxt_bump;
463 xprt->sc_ctxt_cnt = 0;
Tom Tucker87407672008-04-30 20:44:39 -0500464 atomic_set(&xprt->sc_ctxt_used, 0);
465
466 INIT_LIST_HEAD(&xprt->sc_ctxt_free);
Tom Tucker377f9b22007-12-12 16:13:21 -0600467 for (i = 0; i < ctxt_count; i++) {
468 ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
469 if (ctxt) {
Tom Tucker87407672008-04-30 20:44:39 -0500470 INIT_LIST_HEAD(&ctxt->free_list);
471 list_add(&ctxt->free_list, &xprt->sc_ctxt_free);
Tom Tucker377f9b22007-12-12 16:13:21 -0600472 xprt->sc_ctxt_cnt++;
473 }
474 }
475}
476
Tom Tucker87407672008-04-30 20:44:39 -0500477static void destroy_context_cache(struct svcxprt_rdma *xprt)
Tom Tucker377f9b22007-12-12 16:13:21 -0600478{
Tom Tucker87407672008-04-30 20:44:39 -0500479 while (!list_empty(&xprt->sc_ctxt_free)) {
480 struct svc_rdma_op_ctxt *ctxt;
481 ctxt = list_entry(xprt->sc_ctxt_free.next,
482 struct svc_rdma_op_ctxt,
483 free_list);
484 list_del_init(&ctxt->free_list);
Tom Tucker377f9b22007-12-12 16:13:21 -0600485 kfree(ctxt);
Tom Tucker87407672008-04-30 20:44:39 -0500486 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600487}
488
489static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
490 int listener)
491{
492 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
493
494 if (!cma_xprt)
495 return NULL;
496 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
497 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
498 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
499 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
500 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
501 init_waitqueue_head(&cma_xprt->sc_send_wait);
502
503 spin_lock_init(&cma_xprt->sc_lock);
504 spin_lock_init(&cma_xprt->sc_read_complete_lock);
505 spin_lock_init(&cma_xprt->sc_ctxt_lock);
506 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
507
508 cma_xprt->sc_ord = svcrdma_ord;
509
510 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
511 cma_xprt->sc_max_requests = svcrdma_max_requests;
512 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
513 atomic_set(&cma_xprt->sc_sq_count, 0);
514
515 if (!listener) {
516 int reqs = cma_xprt->sc_max_requests;
517 create_context_cache(cma_xprt,
518 reqs << 1, /* starting size */
519 reqs, /* bump amount */
520 reqs +
521 cma_xprt->sc_sq_depth +
522 RPCRDMA_MAX_THREADS + 1); /* max */
Tom Tucker87407672008-04-30 20:44:39 -0500523 if (list_empty(&cma_xprt->sc_ctxt_free)) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600524 kfree(cma_xprt);
525 return NULL;
526 }
527 clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
528 } else
529 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
530
531 return cma_xprt;
532}
533
534struct page *svc_rdma_get_page(void)
535{
536 struct page *page;
537
538 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
539 /* If we can't get memory, wait a bit and try again */
540 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
541 "jiffies.\n");
542 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
543 }
544 return page;
545}
546
547int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
548{
549 struct ib_recv_wr recv_wr, *bad_recv_wr;
550 struct svc_rdma_op_ctxt *ctxt;
551 struct page *page;
552 unsigned long pa;
553 int sge_no;
554 int buflen;
555 int ret;
556
557 ctxt = svc_rdma_get_context(xprt);
558 buflen = 0;
559 ctxt->direction = DMA_FROM_DEVICE;
560 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
561 BUG_ON(sge_no >= xprt->sc_max_sge);
562 page = svc_rdma_get_page();
563 ctxt->pages[sge_no] = page;
564 pa = ib_dma_map_page(xprt->sc_cm_id->device,
565 page, 0, PAGE_SIZE,
566 DMA_FROM_DEVICE);
567 ctxt->sge[sge_no].addr = pa;
568 ctxt->sge[sge_no].length = PAGE_SIZE;
569 ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
570 buflen += PAGE_SIZE;
571 }
572 ctxt->count = sge_no;
573 recv_wr.next = NULL;
574 recv_wr.sg_list = &ctxt->sge[0];
575 recv_wr.num_sge = ctxt->count;
576 recv_wr.wr_id = (u64)(unsigned long)ctxt;
577
Tom Tucker0905c0f2008-05-01 10:49:03 -0500578 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600579 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500580 if (ret) {
581 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker05a08262008-04-25 14:11:31 -0500582 svc_rdma_put_context(ctxt, 1);
Tom Tucker0905c0f2008-05-01 10:49:03 -0500583 }
Tom Tucker377f9b22007-12-12 16:13:21 -0600584 return ret;
585}
586
587/*
588 * This function handles the CONNECT_REQUEST event on a listening
589 * endpoint. It is passed the cma_id for the _new_ connection. The context in
590 * this cma_id is inherited from the listening cma_id and is the svc_xprt
591 * structure for the listening endpoint.
592 *
593 * This function creates a new xprt for the new connection and enqueues it on
594 * the accept queue for the listent xprt. When the listen thread is kicked, it
595 * will call the recvfrom method on the listen xprt which will accept the new
596 * connection.
597 */
598static void handle_connect_req(struct rdma_cm_id *new_cma_id)
599{
600 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
601 struct svcxprt_rdma *newxprt;
Tom Tuckeraf261af2008-05-07 13:52:42 -0500602 struct sockaddr *sa;
Tom Tucker377f9b22007-12-12 16:13:21 -0600603
604 /* Create a new transport */
605 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
606 if (!newxprt) {
607 dprintk("svcrdma: failed to create new transport\n");
608 return;
609 }
610 newxprt->sc_cm_id = new_cma_id;
611 new_cma_id->context = newxprt;
612 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
613 newxprt, newxprt->sc_cm_id, listen_xprt);
614
Tom Tuckeraf261af2008-05-07 13:52:42 -0500615 /* Set the local and remote addresses in the transport */
616 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
617 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
618 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
619 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
620
Tom Tucker377f9b22007-12-12 16:13:21 -0600621 /*
622 * Enqueue the new transport on the accept queue of the listening
623 * transport
624 */
625 spin_lock_bh(&listen_xprt->sc_lock);
626 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
627 spin_unlock_bh(&listen_xprt->sc_lock);
628
629 /*
630 * Can't use svc_xprt_received here because we are not on a
631 * rqstp thread
632 */
633 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
634 svc_xprt_enqueue(&listen_xprt->sc_xprt);
635}
636
637/*
638 * Handles events generated on the listening endpoint. These events will be
639 * either be incoming connect requests or adapter removal events.
640 */
641static int rdma_listen_handler(struct rdma_cm_id *cma_id,
642 struct rdma_cm_event *event)
643{
644 struct svcxprt_rdma *xprt = cma_id->context;
645 int ret = 0;
646
647 switch (event->event) {
648 case RDMA_CM_EVENT_CONNECT_REQUEST:
649 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
650 "event=%d\n", cma_id, cma_id->context, event->event);
651 handle_connect_req(cma_id);
652 break;
653
654 case RDMA_CM_EVENT_ESTABLISHED:
655 /* Accept complete */
656 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
657 "cm_id=%p\n", xprt, cma_id);
658 break;
659
660 case RDMA_CM_EVENT_DEVICE_REMOVAL:
661 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
662 xprt, cma_id);
663 if (xprt)
664 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
665 break;
666
667 default:
668 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
669 "event=%d\n", cma_id, event->event);
670 break;
671 }
672
673 return ret;
674}
675
676static int rdma_cma_handler(struct rdma_cm_id *cma_id,
677 struct rdma_cm_event *event)
678{
679 struct svc_xprt *xprt = cma_id->context;
680 struct svcxprt_rdma *rdma =
681 container_of(xprt, struct svcxprt_rdma, sc_xprt);
682 switch (event->event) {
683 case RDMA_CM_EVENT_ESTABLISHED:
684 /* Accept complete */
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400685 svc_xprt_get(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600686 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
687 "cm_id=%p\n", xprt, cma_id);
688 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
689 svc_xprt_enqueue(xprt);
690 break;
691 case RDMA_CM_EVENT_DISCONNECTED:
692 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
693 xprt, cma_id);
694 if (xprt) {
695 set_bit(XPT_CLOSE, &xprt->xpt_flags);
696 svc_xprt_enqueue(xprt);
Tom Tucker120693d2008-04-24 14:17:21 -0500697 svc_xprt_put(xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600698 }
699 break;
700 case RDMA_CM_EVENT_DEVICE_REMOVAL:
701 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
702 "event=%d\n", cma_id, xprt, event->event);
703 if (xprt) {
704 set_bit(XPT_CLOSE, &xprt->xpt_flags);
705 svc_xprt_enqueue(xprt);
706 }
707 break;
708 default:
709 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
710 "event=%d\n", cma_id, event->event);
711 break;
712 }
713 return 0;
714}
715
716/*
717 * Create a listening RDMA service endpoint.
718 */
719static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
720 struct sockaddr *sa, int salen,
721 int flags)
722{
723 struct rdma_cm_id *listen_id;
724 struct svcxprt_rdma *cma_xprt;
725 struct svc_xprt *xprt;
726 int ret;
727
728 dprintk("svcrdma: Creating RDMA socket\n");
729
730 cma_xprt = rdma_create_xprt(serv, 1);
731 if (!cma_xprt)
Tom Tucker58e8f622008-05-06 09:45:54 -0500732 return ERR_PTR(-ENOMEM);
Tom Tucker377f9b22007-12-12 16:13:21 -0600733 xprt = &cma_xprt->sc_xprt;
734
735 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
736 if (IS_ERR(listen_id)) {
Tom Tucker58e8f622008-05-06 09:45:54 -0500737 ret = PTR_ERR(listen_id);
738 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
739 goto err0;
Tom Tucker377f9b22007-12-12 16:13:21 -0600740 }
Tom Tucker58e8f622008-05-06 09:45:54 -0500741
Tom Tucker377f9b22007-12-12 16:13:21 -0600742 ret = rdma_bind_addr(listen_id, sa);
743 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600744 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500745 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600746 }
747 cma_xprt->sc_cm_id = listen_id;
748
749 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
750 if (ret) {
Tom Tucker377f9b22007-12-12 16:13:21 -0600751 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
Tom Tucker58e8f622008-05-06 09:45:54 -0500752 goto err1;
Tom Tucker377f9b22007-12-12 16:13:21 -0600753 }
754
755 /*
756 * We need to use the address from the cm_id in case the
757 * caller specified 0 for the port number.
758 */
759 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
760 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
761
762 return &cma_xprt->sc_xprt;
Tom Tucker58e8f622008-05-06 09:45:54 -0500763
764 err1:
765 rdma_destroy_id(listen_id);
766 err0:
767 kfree(cma_xprt);
768 return ERR_PTR(ret);
Tom Tucker377f9b22007-12-12 16:13:21 -0600769}
770
771/*
772 * This is the xpo_recvfrom function for listening endpoints. Its
773 * purpose is to accept incoming connections. The CMA callback handler
774 * has already created a new transport and attached it to the new CMA
775 * ID.
776 *
777 * There is a queue of pending connections hung on the listening
778 * transport. This queue contains the new svc_xprt structure. This
779 * function takes svc_xprt structures off the accept_q and completes
780 * the connection.
781 */
782static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
783{
784 struct svcxprt_rdma *listen_rdma;
785 struct svcxprt_rdma *newxprt = NULL;
786 struct rdma_conn_param conn_param;
787 struct ib_qp_init_attr qp_attr;
788 struct ib_device_attr devattr;
Tom Tucker377f9b22007-12-12 16:13:21 -0600789 int ret;
790 int i;
791
792 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
793 clear_bit(XPT_CONN, &xprt->xpt_flags);
794 /* Get the next entry off the accept list */
795 spin_lock_bh(&listen_rdma->sc_lock);
796 if (!list_empty(&listen_rdma->sc_accept_q)) {
797 newxprt = list_entry(listen_rdma->sc_accept_q.next,
798 struct svcxprt_rdma, sc_accept_q);
799 list_del_init(&newxprt->sc_accept_q);
800 }
801 if (!list_empty(&listen_rdma->sc_accept_q))
802 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
803 spin_unlock_bh(&listen_rdma->sc_lock);
804 if (!newxprt)
805 return NULL;
806
807 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
808 newxprt, newxprt->sc_cm_id);
809
810 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
811 if (ret) {
812 dprintk("svcrdma: could not query device attributes on "
813 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
814 goto errout;
815 }
816
817 /* Qualify the transport resource defaults with the
818 * capabilities of this particular device */
819 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
820 (size_t)RPCSVC_MAXPAGES);
821 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
822 (size_t)svcrdma_max_requests);
823 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
824
825 newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom,
826 (size_t)svcrdma_ord);
827
828 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
829 if (IS_ERR(newxprt->sc_pd)) {
830 dprintk("svcrdma: error creating PD for connect request\n");
831 goto errout;
832 }
833 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
834 sq_comp_handler,
835 cq_event_handler,
836 newxprt,
837 newxprt->sc_sq_depth,
838 0);
839 if (IS_ERR(newxprt->sc_sq_cq)) {
840 dprintk("svcrdma: error creating SQ CQ for connect request\n");
841 goto errout;
842 }
843 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
844 rq_comp_handler,
845 cq_event_handler,
846 newxprt,
847 newxprt->sc_max_requests,
848 0);
849 if (IS_ERR(newxprt->sc_rq_cq)) {
850 dprintk("svcrdma: error creating RQ CQ for connect request\n");
851 goto errout;
852 }
853
854 memset(&qp_attr, 0, sizeof qp_attr);
855 qp_attr.event_handler = qp_event_handler;
856 qp_attr.qp_context = &newxprt->sc_xprt;
857 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
858 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
859 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
860 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
861 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
862 qp_attr.qp_type = IB_QPT_RC;
863 qp_attr.send_cq = newxprt->sc_sq_cq;
864 qp_attr.recv_cq = newxprt->sc_rq_cq;
865 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
866 " cm_id->device=%p, sc_pd->device=%p\n"
867 " cap.max_send_wr = %d\n"
868 " cap.max_recv_wr = %d\n"
869 " cap.max_send_sge = %d\n"
870 " cap.max_recv_sge = %d\n",
871 newxprt->sc_cm_id, newxprt->sc_pd,
872 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
873 qp_attr.cap.max_send_wr,
874 qp_attr.cap.max_recv_wr,
875 qp_attr.cap.max_send_sge,
876 qp_attr.cap.max_recv_sge);
877
878 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
879 if (ret) {
880 /*
881 * XXX: This is a hack. We need a xx_request_qp interface
882 * that will adjust the qp_attr's with a best-effort
883 * number
884 */
885 qp_attr.cap.max_send_sge -= 2;
886 qp_attr.cap.max_recv_sge -= 2;
887 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
888 &qp_attr);
889 if (ret) {
890 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
891 goto errout;
892 }
893 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
894 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
895 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
896 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
897 }
898 newxprt->sc_qp = newxprt->sc_cm_id->qp;
899
900 /* Register all of physical memory */
901 newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd,
902 IB_ACCESS_LOCAL_WRITE |
903 IB_ACCESS_REMOTE_WRITE);
904 if (IS_ERR(newxprt->sc_phys_mr)) {
905 dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret);
906 goto errout;
907 }
908
909 /* Post receive buffers */
910 for (i = 0; i < newxprt->sc_max_requests; i++) {
911 ret = svc_rdma_post_recv(newxprt);
912 if (ret) {
913 dprintk("svcrdma: failure posting receive buffers\n");
914 goto errout;
915 }
916 }
917
918 /* Swap out the handler */
919 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
920
Tom Tuckeraf261af2008-05-07 13:52:42 -0500921 /*
922 * Arm the CQs for the SQ and RQ before accepting so we can't
923 * miss the first message
924 */
925 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
926 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
927
Tom Tucker377f9b22007-12-12 16:13:21 -0600928 /* Accept Connection */
929 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
930 memset(&conn_param, 0, sizeof conn_param);
931 conn_param.responder_resources = 0;
932 conn_param.initiator_depth = newxprt->sc_ord;
933 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
934 if (ret) {
935 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
936 ret);
937 goto errout;
938 }
939
940 dprintk("svcrdma: new connection %p accepted with the following "
941 "attributes:\n"
942 " local_ip : %d.%d.%d.%d\n"
943 " local_port : %d\n"
944 " remote_ip : %d.%d.%d.%d\n"
945 " remote_port : %d\n"
946 " max_sge : %d\n"
947 " sq_depth : %d\n"
948 " max_requests : %d\n"
949 " ord : %d\n",
950 newxprt,
951 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
952 route.addr.src_addr)->sin_addr.s_addr),
953 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
954 route.addr.src_addr)->sin_port),
955 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
956 route.addr.dst_addr)->sin_addr.s_addr),
957 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
958 route.addr.dst_addr)->sin_port),
959 newxprt->sc_max_sge,
960 newxprt->sc_sq_depth,
961 newxprt->sc_max_requests,
962 newxprt->sc_ord);
963
Tom Tucker377f9b22007-12-12 16:13:21 -0600964 return &newxprt->sc_xprt;
965
966 errout:
967 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400968 /* Take a reference in case the DTO handler runs */
969 svc_xprt_get(&newxprt->sc_xprt);
Tom Tucker17113862008-05-01 11:13:50 -0500970 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400971 ib_destroy_qp(newxprt->sc_qp);
Tom Tucker377f9b22007-12-12 16:13:21 -0600972 rdma_destroy_id(newxprt->sc_cm_id);
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400973 /* This call to put will destroy the transport */
974 svc_xprt_put(&newxprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600975 return NULL;
976}
977
Tom Tucker377f9b22007-12-12 16:13:21 -0600978static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
979{
Tom Tucker377f9b22007-12-12 16:13:21 -0600980}
981
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400982/*
Tom Tucker17113862008-05-01 11:13:50 -0500983 * When connected, an svc_xprt has at least two references:
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400984 *
985 * - A reference held by the cm_id between the ESTABLISHED and
986 * DISCONNECTED events. If the remote peer disconnected first, this
987 * reference could be gone.
988 *
989 * - A reference held by the svc_recv code that called this function
990 * as part of close processing.
991 *
Tom Tucker17113862008-05-01 11:13:50 -0500992 * At a minimum one references should still be held.
Tom Tuckerc48cbb42008-03-11 14:31:39 -0400993 */
Tom Tucker377f9b22007-12-12 16:13:21 -0600994static void svc_rdma_detach(struct svc_xprt *xprt)
995{
996 struct svcxprt_rdma *rdma =
997 container_of(xprt, struct svcxprt_rdma, sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600998 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -0600999
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001000 /* Disconnect and flush posted WQE */
1001 rdma_disconnect(rdma->sc_cm_id);
Tom Tucker377f9b22007-12-12 16:13:21 -06001002}
1003
Tom Tucker8da91ea2008-04-30 22:00:46 -05001004static void __svc_rdma_free(struct work_struct *work)
Tom Tucker377f9b22007-12-12 16:13:21 -06001005{
Tom Tucker8da91ea2008-04-30 22:00:46 -05001006 struct svcxprt_rdma *rdma =
1007 container_of(work, struct svcxprt_rdma, sc_work);
Tom Tucker377f9b22007-12-12 16:13:21 -06001008 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
Tom Tucker8da91ea2008-04-30 22:00:46 -05001009
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001010 /* We should only be called from kref_put */
Tom Tucker8da91ea2008-04-30 22:00:46 -05001011 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1012
Tom Tucker356d0a12008-05-01 11:25:02 -05001013 /*
1014 * Destroy queued, but not processed read completions. Note
1015 * that this cleanup has to be done before destroying the
1016 * cm_id because the device ptr is needed to unmap the dma in
1017 * svc_rdma_put_context.
1018 */
1019 spin_lock_bh(&rdma->sc_read_complete_lock);
1020 while (!list_empty(&rdma->sc_read_complete_q)) {
1021 struct svc_rdma_op_ctxt *ctxt;
1022 ctxt = list_entry(rdma->sc_read_complete_q.next,
1023 struct svc_rdma_op_ctxt,
1024 dto_q);
1025 list_del_init(&ctxt->dto_q);
1026 svc_rdma_put_context(ctxt, 1);
1027 }
1028 spin_unlock_bh(&rdma->sc_read_complete_lock);
1029
1030 /* Destroy queued, but not processed recv completions */
1031 spin_lock_bh(&rdma->sc_rq_dto_lock);
1032 while (!list_empty(&rdma->sc_rq_dto_q)) {
1033 struct svc_rdma_op_ctxt *ctxt;
1034 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1035 struct svc_rdma_op_ctxt,
1036 dto_q);
1037 list_del_init(&ctxt->dto_q);
1038 svc_rdma_put_context(ctxt, 1);
1039 }
1040 spin_unlock_bh(&rdma->sc_rq_dto_lock);
1041
1042 /* Warn if we leaked a resource or under-referenced */
1043 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1044
Tom Tucker17113862008-05-01 11:13:50 -05001045 /* Destroy the QP if present (not a listener) */
1046 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1047 ib_destroy_qp(rdma->sc_qp);
1048
Tom Tuckerc48cbb42008-03-11 14:31:39 -04001049 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1050 ib_destroy_cq(rdma->sc_sq_cq);
1051
1052 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1053 ib_destroy_cq(rdma->sc_rq_cq);
1054
1055 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1056 ib_dereg_mr(rdma->sc_phys_mr);
1057
1058 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1059 ib_dealloc_pd(rdma->sc_pd);
1060
Tom Tucker356d0a12008-05-01 11:25:02 -05001061 /* Destroy the CM ID */
1062 rdma_destroy_id(rdma->sc_cm_id);
1063
Tom Tucker87407672008-04-30 20:44:39 -05001064 destroy_context_cache(rdma);
Tom Tucker377f9b22007-12-12 16:13:21 -06001065 kfree(rdma);
1066}
1067
Tom Tucker8da91ea2008-04-30 22:00:46 -05001068static void svc_rdma_free(struct svc_xprt *xprt)
1069{
1070 struct svcxprt_rdma *rdma =
1071 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1072 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1073 schedule_work(&rdma->sc_work);
1074}
1075
Tom Tucker377f9b22007-12-12 16:13:21 -06001076static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1077{
1078 struct svcxprt_rdma *rdma =
1079 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1080
1081 /*
1082 * If there are fewer SQ WR available than required to send a
1083 * simple response, return false.
1084 */
1085 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1086 return 0;
1087
1088 /*
1089 * ...or there are already waiters on the SQ,
1090 * return false.
1091 */
1092 if (waitqueue_active(&rdma->sc_send_wait))
1093 return 0;
1094
1095 /* Otherwise return true. */
1096 return 1;
1097}
1098
1099int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1100{
1101 struct ib_send_wr *bad_wr;
1102 int ret;
1103
1104 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
Tom Tucker9d6347a2008-04-25 15:51:27 -05001105 return -ENOTCONN;
Tom Tucker377f9b22007-12-12 16:13:21 -06001106
1107 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1108 BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
1109 wr->opcode);
1110 /* If the SQ is full, wait until an SQ entry is available */
1111 while (1) {
1112 spin_lock_bh(&xprt->sc_lock);
1113 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1114 spin_unlock_bh(&xprt->sc_lock);
1115 atomic_inc(&rdma_stat_sq_starve);
Tom Tuckerdbcd00e2008-05-06 11:33:11 -05001116
1117 /* See if we can opportunistically reap SQ WR to make room */
Tom Tucker377f9b22007-12-12 16:13:21 -06001118 sq_cq_reap(xprt);
1119
1120 /* Wait until SQ WR available if SQ still full */
1121 wait_event(xprt->sc_send_wait,
1122 atomic_read(&xprt->sc_sq_count) <
1123 xprt->sc_sq_depth);
Tom Tucker830bb592008-03-11 12:44:27 -05001124 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1125 return 0;
Tom Tucker377f9b22007-12-12 16:13:21 -06001126 continue;
1127 }
1128 /* Bumped used SQ WR count and post */
Tom Tucker0905c0f2008-05-01 10:49:03 -05001129 svc_xprt_get(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001130 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1131 if (!ret)
1132 atomic_inc(&xprt->sc_sq_count);
Tom Tucker0905c0f2008-05-01 10:49:03 -05001133 else {
1134 svc_xprt_put(&xprt->sc_xprt);
Tom Tucker377f9b22007-12-12 16:13:21 -06001135 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1136 "sc_sq_count=%d, sc_sq_depth=%d\n",
1137 ret, atomic_read(&xprt->sc_sq_count),
1138 xprt->sc_sq_depth);
Tom Tucker0905c0f2008-05-01 10:49:03 -05001139 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001140 spin_unlock_bh(&xprt->sc_lock);
1141 break;
1142 }
1143 return ret;
1144}
1145
Tom Tucker008fdbc2008-05-07 15:47:42 -05001146void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1147 enum rpcrdma_errcode err)
Tom Tucker377f9b22007-12-12 16:13:21 -06001148{
1149 struct ib_send_wr err_wr;
1150 struct ib_sge sge;
1151 struct page *p;
1152 struct svc_rdma_op_ctxt *ctxt;
1153 u32 *va;
1154 int length;
1155 int ret;
1156
1157 p = svc_rdma_get_page();
1158 va = page_address(p);
1159
1160 /* XDR encode error */
1161 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1162
1163 /* Prepare SGE for local address */
1164 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1165 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1166 sge.lkey = xprt->sc_phys_mr->lkey;
1167 sge.length = length;
1168
1169 ctxt = svc_rdma_get_context(xprt);
1170 ctxt->count = 1;
1171 ctxt->pages[0] = p;
1172
1173 /* Prepare SEND WR */
1174 memset(&err_wr, 0, sizeof err_wr);
1175 ctxt->wr_op = IB_WR_SEND;
1176 err_wr.wr_id = (unsigned long)ctxt;
1177 err_wr.sg_list = &sge;
1178 err_wr.num_sge = 1;
1179 err_wr.opcode = IB_WR_SEND;
1180 err_wr.send_flags = IB_SEND_SIGNALED;
1181
1182 /* Post It */
1183 ret = svc_rdma_send(xprt, &err_wr);
1184 if (ret) {
Tom Tucker008fdbc2008-05-07 15:47:42 -05001185 dprintk("svcrdma: Error %d posting send for protocol error\n",
1186 ret);
Tom Tucker377f9b22007-12-12 16:13:21 -06001187 svc_rdma_put_context(ctxt, 1);
1188 }
Tom Tucker377f9b22007-12-12 16:13:21 -06001189}