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