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