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> | |
45 | #include <linux/spinlock.h> | |
46 | #include <rdma/ib_verbs.h> | |
47 | #include <rdma/rdma_cm.h> | |
48 | #include <linux/sunrpc/svc_rdma.h> | |
49 | ||
50 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT | |
51 | ||
52 | static struct svc_xprt *svc_rdma_create(struct svc_serv *serv, | |
53 | struct sockaddr *sa, int salen, | |
54 | int flags); | |
55 | static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt); | |
56 | static void svc_rdma_release_rqst(struct svc_rqst *); | |
377f9b2f TT |
57 | static void dto_tasklet_func(unsigned long data); |
58 | static void svc_rdma_detach(struct svc_xprt *xprt); | |
59 | static void svc_rdma_free(struct svc_xprt *xprt); | |
60 | static int svc_rdma_has_wspace(struct svc_xprt *xprt); | |
61 | static void rq_cq_reap(struct svcxprt_rdma *xprt); | |
62 | static void sq_cq_reap(struct svcxprt_rdma *xprt); | |
63 | ||
64 | DECLARE_TASKLET(dto_tasklet, dto_tasklet_func, 0UL); | |
65 | static DEFINE_SPINLOCK(dto_lock); | |
66 | static LIST_HEAD(dto_xprt_q); | |
67 | ||
68 | static struct svc_xprt_ops svc_rdma_ops = { | |
69 | .xpo_create = svc_rdma_create, | |
70 | .xpo_recvfrom = svc_rdma_recvfrom, | |
71 | .xpo_sendto = svc_rdma_sendto, | |
72 | .xpo_release_rqst = svc_rdma_release_rqst, | |
73 | .xpo_detach = svc_rdma_detach, | |
74 | .xpo_free = svc_rdma_free, | |
75 | .xpo_prep_reply_hdr = svc_rdma_prep_reply_hdr, | |
76 | .xpo_has_wspace = svc_rdma_has_wspace, | |
77 | .xpo_accept = svc_rdma_accept, | |
78 | }; | |
79 | ||
80 | struct svc_xprt_class svc_rdma_class = { | |
81 | .xcl_name = "rdma", | |
82 | .xcl_owner = THIS_MODULE, | |
83 | .xcl_ops = &svc_rdma_ops, | |
84 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, | |
85 | }; | |
86 | ||
87 | static int rdma_bump_context_cache(struct svcxprt_rdma *xprt) | |
88 | { | |
89 | int target; | |
90 | int at_least_one = 0; | |
91 | struct svc_rdma_op_ctxt *ctxt; | |
92 | ||
93 | target = min(xprt->sc_ctxt_cnt + xprt->sc_ctxt_bump, | |
94 | xprt->sc_ctxt_max); | |
95 | ||
96 | spin_lock_bh(&xprt->sc_ctxt_lock); | |
97 | while (xprt->sc_ctxt_cnt < target) { | |
98 | xprt->sc_ctxt_cnt++; | |
99 | spin_unlock_bh(&xprt->sc_ctxt_lock); | |
100 | ||
101 | ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL); | |
102 | ||
103 | spin_lock_bh(&xprt->sc_ctxt_lock); | |
104 | if (ctxt) { | |
105 | at_least_one = 1; | |
87407673 TT |
106 | INIT_LIST_HEAD(&ctxt->free_list); |
107 | list_add(&ctxt->free_list, &xprt->sc_ctxt_free); | |
377f9b2f TT |
108 | } else { |
109 | /* kmalloc failed...give up for now */ | |
110 | xprt->sc_ctxt_cnt--; | |
111 | break; | |
112 | } | |
113 | } | |
114 | spin_unlock_bh(&xprt->sc_ctxt_lock); | |
115 | dprintk("svcrdma: sc_ctxt_max=%d, sc_ctxt_cnt=%d\n", | |
116 | xprt->sc_ctxt_max, xprt->sc_ctxt_cnt); | |
117 | return at_least_one; | |
118 | } | |
119 | ||
120 | struct svc_rdma_op_ctxt *svc_rdma_get_context(struct svcxprt_rdma *xprt) | |
121 | { | |
122 | struct svc_rdma_op_ctxt *ctxt; | |
123 | ||
124 | while (1) { | |
125 | spin_lock_bh(&xprt->sc_ctxt_lock); | |
87407673 | 126 | if (unlikely(list_empty(&xprt->sc_ctxt_free))) { |
377f9b2f TT |
127 | /* Try to bump my cache. */ |
128 | spin_unlock_bh(&xprt->sc_ctxt_lock); | |
129 | ||
130 | if (rdma_bump_context_cache(xprt)) | |
131 | continue; | |
132 | ||
133 | printk(KERN_INFO "svcrdma: sleeping waiting for " | |
134 | "context memory on xprt=%p\n", | |
135 | xprt); | |
136 | schedule_timeout_uninterruptible(msecs_to_jiffies(500)); | |
137 | continue; | |
138 | } | |
87407673 TT |
139 | ctxt = list_entry(xprt->sc_ctxt_free.next, |
140 | struct svc_rdma_op_ctxt, | |
141 | free_list); | |
142 | list_del_init(&ctxt->free_list); | |
377f9b2f TT |
143 | spin_unlock_bh(&xprt->sc_ctxt_lock); |
144 | ctxt->xprt = xprt; | |
145 | INIT_LIST_HEAD(&ctxt->dto_q); | |
146 | ctxt->count = 0; | |
87407673 | 147 | atomic_inc(&xprt->sc_ctxt_used); |
377f9b2f TT |
148 | break; |
149 | } | |
150 | return ctxt; | |
151 | } | |
152 | ||
153 | void svc_rdma_put_context(struct svc_rdma_op_ctxt *ctxt, int free_pages) | |
154 | { | |
155 | struct svcxprt_rdma *xprt; | |
156 | int i; | |
157 | ||
158 | BUG_ON(!ctxt); | |
159 | xprt = ctxt->xprt; | |
160 | if (free_pages) | |
161 | for (i = 0; i < ctxt->count; i++) | |
162 | put_page(ctxt->pages[i]); | |
163 | ||
164 | for (i = 0; i < ctxt->count; i++) | |
165 | dma_unmap_single(xprt->sc_cm_id->device->dma_device, | |
166 | ctxt->sge[i].addr, | |
167 | ctxt->sge[i].length, | |
168 | ctxt->direction); | |
87407673 | 169 | |
377f9b2f | 170 | spin_lock_bh(&xprt->sc_ctxt_lock); |
87407673 | 171 | list_add(&ctxt->free_list, &xprt->sc_ctxt_free); |
377f9b2f | 172 | spin_unlock_bh(&xprt->sc_ctxt_lock); |
87407673 | 173 | atomic_dec(&xprt->sc_ctxt_used); |
377f9b2f TT |
174 | } |
175 | ||
176 | /* ib_cq event handler */ | |
177 | static void cq_event_handler(struct ib_event *event, void *context) | |
178 | { | |
179 | struct svc_xprt *xprt = context; | |
180 | dprintk("svcrdma: received CQ event id=%d, context=%p\n", | |
181 | event->event, context); | |
182 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
183 | } | |
184 | ||
185 | /* QP event handler */ | |
186 | static void qp_event_handler(struct ib_event *event, void *context) | |
187 | { | |
188 | struct svc_xprt *xprt = context; | |
189 | ||
190 | switch (event->event) { | |
191 | /* These are considered benign events */ | |
192 | case IB_EVENT_PATH_MIG: | |
193 | case IB_EVENT_COMM_EST: | |
194 | case IB_EVENT_SQ_DRAINED: | |
195 | case IB_EVENT_QP_LAST_WQE_REACHED: | |
196 | dprintk("svcrdma: QP event %d received for QP=%p\n", | |
197 | event->event, event->element.qp); | |
198 | break; | |
199 | /* These are considered fatal events */ | |
200 | case IB_EVENT_PATH_MIG_ERR: | |
201 | case IB_EVENT_QP_FATAL: | |
202 | case IB_EVENT_QP_REQ_ERR: | |
203 | case IB_EVENT_QP_ACCESS_ERR: | |
204 | case IB_EVENT_DEVICE_FATAL: | |
205 | default: | |
206 | dprintk("svcrdma: QP ERROR event %d received for QP=%p, " | |
207 | "closing transport\n", | |
208 | event->event, event->element.qp); | |
209 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
214 | /* | |
215 | * Data Transfer Operation Tasklet | |
216 | * | |
217 | * Walks a list of transports with I/O pending, removing entries as | |
218 | * they are added to the server's I/O pending list. Two bits indicate | |
219 | * if SQ, RQ, or both have I/O pending. The dto_lock is an irqsave | |
220 | * spinlock that serializes access to the transport list with the RQ | |
221 | * and SQ interrupt handlers. | |
222 | */ | |
223 | static void dto_tasklet_func(unsigned long data) | |
224 | { | |
225 | struct svcxprt_rdma *xprt; | |
226 | unsigned long flags; | |
227 | ||
228 | spin_lock_irqsave(&dto_lock, flags); | |
229 | while (!list_empty(&dto_xprt_q)) { | |
230 | xprt = list_entry(dto_xprt_q.next, | |
231 | struct svcxprt_rdma, sc_dto_q); | |
232 | list_del_init(&xprt->sc_dto_q); | |
233 | spin_unlock_irqrestore(&dto_lock, flags); | |
234 | ||
dbcd00eb TT |
235 | rq_cq_reap(xprt); |
236 | sq_cq_reap(xprt); | |
377f9b2f | 237 | |
c48cbb40 | 238 | svc_xprt_put(&xprt->sc_xprt); |
377f9b2f TT |
239 | spin_lock_irqsave(&dto_lock, flags); |
240 | } | |
241 | spin_unlock_irqrestore(&dto_lock, flags); | |
242 | } | |
243 | ||
244 | /* | |
245 | * Receive Queue Completion Handler | |
246 | * | |
247 | * Since an RQ completion handler is called on interrupt context, we | |
248 | * need to defer the handling of the I/O to a tasklet | |
249 | */ | |
250 | static void rq_comp_handler(struct ib_cq *cq, void *cq_context) | |
251 | { | |
252 | struct svcxprt_rdma *xprt = cq_context; | |
253 | unsigned long flags; | |
254 | ||
1711386c TT |
255 | /* Guard against unconditional flush call for destroyed QP */ |
256 | if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0) | |
257 | return; | |
258 | ||
377f9b2f TT |
259 | /* |
260 | * Set the bit regardless of whether or not it's on the list | |
261 | * because it may be on the list already due to an SQ | |
262 | * completion. | |
1711386c | 263 | */ |
377f9b2f TT |
264 | set_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags); |
265 | ||
266 | /* | |
267 | * If this transport is not already on the DTO transport queue, | |
268 | * add it | |
269 | */ | |
270 | spin_lock_irqsave(&dto_lock, flags); | |
c48cbb40 TT |
271 | if (list_empty(&xprt->sc_dto_q)) { |
272 | svc_xprt_get(&xprt->sc_xprt); | |
377f9b2f | 273 | list_add_tail(&xprt->sc_dto_q, &dto_xprt_q); |
c48cbb40 | 274 | } |
377f9b2f TT |
275 | spin_unlock_irqrestore(&dto_lock, flags); |
276 | ||
277 | /* Tasklet does all the work to avoid irqsave locks. */ | |
278 | tasklet_schedule(&dto_tasklet); | |
279 | } | |
280 | ||
281 | /* | |
282 | * rq_cq_reap - Process the RQ CQ. | |
283 | * | |
284 | * Take all completing WC off the CQE and enqueue the associated DTO | |
285 | * context on the dto_q for the transport. | |
0905c0f0 TT |
286 | * |
287 | * Note that caller must hold a transport reference. | |
377f9b2f TT |
288 | */ |
289 | static void rq_cq_reap(struct svcxprt_rdma *xprt) | |
290 | { | |
291 | int ret; | |
292 | struct ib_wc wc; | |
293 | struct svc_rdma_op_ctxt *ctxt = NULL; | |
294 | ||
dbcd00eb TT |
295 | if (!test_and_clear_bit(RDMAXPRT_RQ_PENDING, &xprt->sc_flags)) |
296 | return; | |
297 | ||
298 | ib_req_notify_cq(xprt->sc_rq_cq, IB_CQ_NEXT_COMP); | |
377f9b2f TT |
299 | atomic_inc(&rdma_stat_rq_poll); |
300 | ||
377f9b2f TT |
301 | while ((ret = ib_poll_cq(xprt->sc_rq_cq, 1, &wc)) > 0) { |
302 | ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id; | |
303 | ctxt->wc_status = wc.status; | |
304 | ctxt->byte_len = wc.byte_len; | |
305 | if (wc.status != IB_WC_SUCCESS) { | |
306 | /* Close the transport */ | |
0905c0f0 | 307 | dprintk("svcrdma: transport closing putting ctxt %p\n", ctxt); |
377f9b2f TT |
308 | set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags); |
309 | svc_rdma_put_context(ctxt, 1); | |
0905c0f0 | 310 | svc_xprt_put(&xprt->sc_xprt); |
377f9b2f TT |
311 | continue; |
312 | } | |
47698e08 | 313 | spin_lock_bh(&xprt->sc_rq_dto_lock); |
377f9b2f | 314 | list_add_tail(&ctxt->dto_q, &xprt->sc_rq_dto_q); |
47698e08 | 315 | spin_unlock_bh(&xprt->sc_rq_dto_lock); |
0905c0f0 | 316 | svc_xprt_put(&xprt->sc_xprt); |
377f9b2f | 317 | } |
377f9b2f TT |
318 | |
319 | if (ctxt) | |
320 | atomic_inc(&rdma_stat_rq_prod); | |
dbcd00eb TT |
321 | |
322 | set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags); | |
323 | /* | |
324 | * If data arrived before established event, | |
325 | * don't enqueue. This defers RPC I/O until the | |
326 | * RDMA connection is complete. | |
327 | */ | |
328 | if (!test_bit(RDMAXPRT_CONN_PENDING, &xprt->sc_flags)) | |
329 | svc_xprt_enqueue(&xprt->sc_xprt); | |
377f9b2f TT |
330 | } |
331 | ||
332 | /* | |
333 | * Send Queue Completion Handler - potentially called on interrupt context. | |
0905c0f0 TT |
334 | * |
335 | * Note that caller must hold a transport reference. | |
377f9b2f TT |
336 | */ |
337 | static void sq_cq_reap(struct svcxprt_rdma *xprt) | |
338 | { | |
339 | struct svc_rdma_op_ctxt *ctxt = NULL; | |
340 | struct ib_wc wc; | |
341 | struct ib_cq *cq = xprt->sc_sq_cq; | |
342 | int ret; | |
343 | ||
dbcd00eb TT |
344 | |
345 | if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags)) | |
346 | return; | |
347 | ||
348 | ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP); | |
377f9b2f TT |
349 | atomic_inc(&rdma_stat_sq_poll); |
350 | while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) { | |
351 | ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id; | |
352 | xprt = ctxt->xprt; | |
353 | ||
354 | if (wc.status != IB_WC_SUCCESS) | |
355 | /* Close the transport */ | |
356 | set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags); | |
357 | ||
358 | /* Decrement used SQ WR count */ | |
359 | atomic_dec(&xprt->sc_sq_count); | |
360 | wake_up(&xprt->sc_send_wait); | |
361 | ||
362 | switch (ctxt->wr_op) { | |
363 | case IB_WR_SEND: | |
364 | case IB_WR_RDMA_WRITE: | |
365 | svc_rdma_put_context(ctxt, 1); | |
366 | break; | |
367 | ||
368 | case IB_WR_RDMA_READ: | |
369 | if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) { | |
02e7452d TT |
370 | struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr; |
371 | BUG_ON(!read_hdr); | |
377f9b2f | 372 | set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags); |
377f9b2f | 373 | spin_lock_bh(&xprt->sc_read_complete_lock); |
02e7452d | 374 | list_add_tail(&read_hdr->dto_q, |
377f9b2f TT |
375 | &xprt->sc_read_complete_q); |
376 | spin_unlock_bh(&xprt->sc_read_complete_lock); | |
377 | svc_xprt_enqueue(&xprt->sc_xprt); | |
378 | } | |
02e7452d | 379 | svc_rdma_put_context(ctxt, 0); |
377f9b2f TT |
380 | break; |
381 | ||
382 | default: | |
383 | printk(KERN_ERR "svcrdma: unexpected completion type, " | |
384 | "opcode=%d, status=%d\n", | |
385 | wc.opcode, wc.status); | |
386 | break; | |
387 | } | |
0905c0f0 | 388 | svc_xprt_put(&xprt->sc_xprt); |
377f9b2f TT |
389 | } |
390 | ||
391 | if (ctxt) | |
392 | atomic_inc(&rdma_stat_sq_prod); | |
393 | } | |
394 | ||
395 | static void sq_comp_handler(struct ib_cq *cq, void *cq_context) | |
396 | { | |
397 | struct svcxprt_rdma *xprt = cq_context; | |
398 | unsigned long flags; | |
399 | ||
1711386c TT |
400 | /* Guard against unconditional flush call for destroyed QP */ |
401 | if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0) | |
402 | return; | |
403 | ||
377f9b2f TT |
404 | /* |
405 | * Set the bit regardless of whether or not it's on the list | |
406 | * because it may be on the list already due to an RQ | |
407 | * completion. | |
1711386c | 408 | */ |
377f9b2f TT |
409 | set_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags); |
410 | ||
411 | /* | |
412 | * If this transport is not already on the DTO transport queue, | |
413 | * add it | |
414 | */ | |
415 | spin_lock_irqsave(&dto_lock, flags); | |
c48cbb40 TT |
416 | if (list_empty(&xprt->sc_dto_q)) { |
417 | svc_xprt_get(&xprt->sc_xprt); | |
377f9b2f | 418 | list_add_tail(&xprt->sc_dto_q, &dto_xprt_q); |
c48cbb40 | 419 | } |
377f9b2f TT |
420 | spin_unlock_irqrestore(&dto_lock, flags); |
421 | ||
422 | /* Tasklet does all the work to avoid irqsave locks. */ | |
423 | tasklet_schedule(&dto_tasklet); | |
424 | } | |
425 | ||
426 | static void create_context_cache(struct svcxprt_rdma *xprt, | |
427 | int ctxt_count, int ctxt_bump, int ctxt_max) | |
428 | { | |
429 | struct svc_rdma_op_ctxt *ctxt; | |
430 | int i; | |
431 | ||
432 | xprt->sc_ctxt_max = ctxt_max; | |
433 | xprt->sc_ctxt_bump = ctxt_bump; | |
434 | xprt->sc_ctxt_cnt = 0; | |
87407673 TT |
435 | atomic_set(&xprt->sc_ctxt_used, 0); |
436 | ||
437 | INIT_LIST_HEAD(&xprt->sc_ctxt_free); | |
377f9b2f TT |
438 | for (i = 0; i < ctxt_count; i++) { |
439 | ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL); | |
440 | if (ctxt) { | |
87407673 TT |
441 | INIT_LIST_HEAD(&ctxt->free_list); |
442 | list_add(&ctxt->free_list, &xprt->sc_ctxt_free); | |
377f9b2f TT |
443 | xprt->sc_ctxt_cnt++; |
444 | } | |
445 | } | |
446 | } | |
447 | ||
87407673 | 448 | static void destroy_context_cache(struct svcxprt_rdma *xprt) |
377f9b2f | 449 | { |
87407673 TT |
450 | while (!list_empty(&xprt->sc_ctxt_free)) { |
451 | struct svc_rdma_op_ctxt *ctxt; | |
452 | ctxt = list_entry(xprt->sc_ctxt_free.next, | |
453 | struct svc_rdma_op_ctxt, | |
454 | free_list); | |
455 | list_del_init(&ctxt->free_list); | |
377f9b2f | 456 | kfree(ctxt); |
87407673 | 457 | } |
377f9b2f TT |
458 | } |
459 | ||
460 | static struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv, | |
461 | int listener) | |
462 | { | |
463 | struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL); | |
464 | ||
465 | if (!cma_xprt) | |
466 | return NULL; | |
467 | svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv); | |
468 | INIT_LIST_HEAD(&cma_xprt->sc_accept_q); | |
469 | INIT_LIST_HEAD(&cma_xprt->sc_dto_q); | |
470 | INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q); | |
471 | INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q); | |
472 | init_waitqueue_head(&cma_xprt->sc_send_wait); | |
473 | ||
474 | spin_lock_init(&cma_xprt->sc_lock); | |
475 | spin_lock_init(&cma_xprt->sc_read_complete_lock); | |
476 | spin_lock_init(&cma_xprt->sc_ctxt_lock); | |
477 | spin_lock_init(&cma_xprt->sc_rq_dto_lock); | |
478 | ||
479 | cma_xprt->sc_ord = svcrdma_ord; | |
480 | ||
481 | cma_xprt->sc_max_req_size = svcrdma_max_req_size; | |
482 | cma_xprt->sc_max_requests = svcrdma_max_requests; | |
483 | cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT; | |
484 | atomic_set(&cma_xprt->sc_sq_count, 0); | |
485 | ||
486 | if (!listener) { | |
487 | int reqs = cma_xprt->sc_max_requests; | |
488 | create_context_cache(cma_xprt, | |
489 | reqs << 1, /* starting size */ | |
490 | reqs, /* bump amount */ | |
491 | reqs + | |
492 | cma_xprt->sc_sq_depth + | |
493 | RPCRDMA_MAX_THREADS + 1); /* max */ | |
87407673 | 494 | if (list_empty(&cma_xprt->sc_ctxt_free)) { |
377f9b2f TT |
495 | kfree(cma_xprt); |
496 | return NULL; | |
497 | } | |
498 | clear_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags); | |
499 | } else | |
500 | set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags); | |
501 | ||
502 | return cma_xprt; | |
503 | } | |
504 | ||
505 | struct page *svc_rdma_get_page(void) | |
506 | { | |
507 | struct page *page; | |
508 | ||
509 | while ((page = alloc_page(GFP_KERNEL)) == NULL) { | |
510 | /* If we can't get memory, wait a bit and try again */ | |
511 | printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 " | |
512 | "jiffies.\n"); | |
513 | schedule_timeout_uninterruptible(msecs_to_jiffies(1000)); | |
514 | } | |
515 | return page; | |
516 | } | |
517 | ||
518 | int svc_rdma_post_recv(struct svcxprt_rdma *xprt) | |
519 | { | |
520 | struct ib_recv_wr recv_wr, *bad_recv_wr; | |
521 | struct svc_rdma_op_ctxt *ctxt; | |
522 | struct page *page; | |
523 | unsigned long pa; | |
524 | int sge_no; | |
525 | int buflen; | |
526 | int ret; | |
527 | ||
528 | ctxt = svc_rdma_get_context(xprt); | |
529 | buflen = 0; | |
530 | ctxt->direction = DMA_FROM_DEVICE; | |
531 | for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) { | |
532 | BUG_ON(sge_no >= xprt->sc_max_sge); | |
533 | page = svc_rdma_get_page(); | |
534 | ctxt->pages[sge_no] = page; | |
535 | pa = ib_dma_map_page(xprt->sc_cm_id->device, | |
536 | page, 0, PAGE_SIZE, | |
537 | DMA_FROM_DEVICE); | |
538 | ctxt->sge[sge_no].addr = pa; | |
539 | ctxt->sge[sge_no].length = PAGE_SIZE; | |
540 | ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey; | |
541 | buflen += PAGE_SIZE; | |
542 | } | |
543 | ctxt->count = sge_no; | |
544 | recv_wr.next = NULL; | |
545 | recv_wr.sg_list = &ctxt->sge[0]; | |
546 | recv_wr.num_sge = ctxt->count; | |
547 | recv_wr.wr_id = (u64)(unsigned long)ctxt; | |
548 | ||
0905c0f0 | 549 | svc_xprt_get(&xprt->sc_xprt); |
377f9b2f | 550 | ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr); |
0905c0f0 TT |
551 | if (ret) { |
552 | svc_xprt_put(&xprt->sc_xprt); | |
05a0826a | 553 | svc_rdma_put_context(ctxt, 1); |
0905c0f0 | 554 | } |
377f9b2f TT |
555 | return ret; |
556 | } | |
557 | ||
558 | /* | |
559 | * This function handles the CONNECT_REQUEST event on a listening | |
560 | * endpoint. It is passed the cma_id for the _new_ connection. The context in | |
561 | * this cma_id is inherited from the listening cma_id and is the svc_xprt | |
562 | * structure for the listening endpoint. | |
563 | * | |
564 | * This function creates a new xprt for the new connection and enqueues it on | |
565 | * the accept queue for the listent xprt. When the listen thread is kicked, it | |
566 | * will call the recvfrom method on the listen xprt which will accept the new | |
567 | * connection. | |
568 | */ | |
569 | static void handle_connect_req(struct rdma_cm_id *new_cma_id) | |
570 | { | |
571 | struct svcxprt_rdma *listen_xprt = new_cma_id->context; | |
572 | struct svcxprt_rdma *newxprt; | |
573 | ||
574 | /* Create a new transport */ | |
575 | newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0); | |
576 | if (!newxprt) { | |
577 | dprintk("svcrdma: failed to create new transport\n"); | |
578 | return; | |
579 | } | |
580 | newxprt->sc_cm_id = new_cma_id; | |
581 | new_cma_id->context = newxprt; | |
582 | dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n", | |
583 | newxprt, newxprt->sc_cm_id, listen_xprt); | |
584 | ||
585 | /* | |
586 | * Enqueue the new transport on the accept queue of the listening | |
587 | * transport | |
588 | */ | |
589 | spin_lock_bh(&listen_xprt->sc_lock); | |
590 | list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q); | |
591 | spin_unlock_bh(&listen_xprt->sc_lock); | |
592 | ||
593 | /* | |
594 | * Can't use svc_xprt_received here because we are not on a | |
595 | * rqstp thread | |
596 | */ | |
597 | set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags); | |
598 | svc_xprt_enqueue(&listen_xprt->sc_xprt); | |
599 | } | |
600 | ||
601 | /* | |
602 | * Handles events generated on the listening endpoint. These events will be | |
603 | * either be incoming connect requests or adapter removal events. | |
604 | */ | |
605 | static int rdma_listen_handler(struct rdma_cm_id *cma_id, | |
606 | struct rdma_cm_event *event) | |
607 | { | |
608 | struct svcxprt_rdma *xprt = cma_id->context; | |
609 | int ret = 0; | |
610 | ||
611 | switch (event->event) { | |
612 | case RDMA_CM_EVENT_CONNECT_REQUEST: | |
613 | dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, " | |
614 | "event=%d\n", cma_id, cma_id->context, event->event); | |
615 | handle_connect_req(cma_id); | |
616 | break; | |
617 | ||
618 | case RDMA_CM_EVENT_ESTABLISHED: | |
619 | /* Accept complete */ | |
620 | dprintk("svcrdma: Connection completed on LISTEN xprt=%p, " | |
621 | "cm_id=%p\n", xprt, cma_id); | |
622 | break; | |
623 | ||
624 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
625 | dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n", | |
626 | xprt, cma_id); | |
627 | if (xprt) | |
628 | set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags); | |
629 | break; | |
630 | ||
631 | default: | |
632 | dprintk("svcrdma: Unexpected event on listening endpoint %p, " | |
633 | "event=%d\n", cma_id, event->event); | |
634 | break; | |
635 | } | |
636 | ||
637 | return ret; | |
638 | } | |
639 | ||
640 | static int rdma_cma_handler(struct rdma_cm_id *cma_id, | |
641 | struct rdma_cm_event *event) | |
642 | { | |
643 | struct svc_xprt *xprt = cma_id->context; | |
644 | struct svcxprt_rdma *rdma = | |
645 | container_of(xprt, struct svcxprt_rdma, sc_xprt); | |
646 | switch (event->event) { | |
647 | case RDMA_CM_EVENT_ESTABLISHED: | |
648 | /* Accept complete */ | |
c48cbb40 | 649 | svc_xprt_get(xprt); |
377f9b2f TT |
650 | dprintk("svcrdma: Connection completed on DTO xprt=%p, " |
651 | "cm_id=%p\n", xprt, cma_id); | |
652 | clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags); | |
653 | svc_xprt_enqueue(xprt); | |
654 | break; | |
655 | case RDMA_CM_EVENT_DISCONNECTED: | |
656 | dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n", | |
657 | xprt, cma_id); | |
658 | if (xprt) { | |
659 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
660 | svc_xprt_enqueue(xprt); | |
120693d1 | 661 | svc_xprt_put(xprt); |
377f9b2f TT |
662 | } |
663 | break; | |
664 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
665 | dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, " | |
666 | "event=%d\n", cma_id, xprt, event->event); | |
667 | if (xprt) { | |
668 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
669 | svc_xprt_enqueue(xprt); | |
670 | } | |
671 | break; | |
672 | default: | |
673 | dprintk("svcrdma: Unexpected event on DTO endpoint %p, " | |
674 | "event=%d\n", cma_id, event->event); | |
675 | break; | |
676 | } | |
677 | return 0; | |
678 | } | |
679 | ||
680 | /* | |
681 | * Create a listening RDMA service endpoint. | |
682 | */ | |
683 | static struct svc_xprt *svc_rdma_create(struct svc_serv *serv, | |
684 | struct sockaddr *sa, int salen, | |
685 | int flags) | |
686 | { | |
687 | struct rdma_cm_id *listen_id; | |
688 | struct svcxprt_rdma *cma_xprt; | |
689 | struct svc_xprt *xprt; | |
690 | int ret; | |
691 | ||
692 | dprintk("svcrdma: Creating RDMA socket\n"); | |
693 | ||
694 | cma_xprt = rdma_create_xprt(serv, 1); | |
695 | if (!cma_xprt) | |
58e8f621 | 696 | return ERR_PTR(-ENOMEM); |
377f9b2f TT |
697 | xprt = &cma_xprt->sc_xprt; |
698 | ||
699 | listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP); | |
700 | if (IS_ERR(listen_id)) { | |
58e8f621 TT |
701 | ret = PTR_ERR(listen_id); |
702 | dprintk("svcrdma: rdma_create_id failed = %d\n", ret); | |
703 | goto err0; | |
377f9b2f | 704 | } |
58e8f621 | 705 | |
377f9b2f TT |
706 | ret = rdma_bind_addr(listen_id, sa); |
707 | if (ret) { | |
377f9b2f | 708 | dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret); |
58e8f621 | 709 | goto err1; |
377f9b2f TT |
710 | } |
711 | cma_xprt->sc_cm_id = listen_id; | |
712 | ||
713 | ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG); | |
714 | if (ret) { | |
377f9b2f | 715 | dprintk("svcrdma: rdma_listen failed = %d\n", ret); |
58e8f621 | 716 | goto err1; |
377f9b2f TT |
717 | } |
718 | ||
719 | /* | |
720 | * We need to use the address from the cm_id in case the | |
721 | * caller specified 0 for the port number. | |
722 | */ | |
723 | sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr; | |
724 | svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen); | |
725 | ||
726 | return &cma_xprt->sc_xprt; | |
58e8f621 TT |
727 | |
728 | err1: | |
729 | rdma_destroy_id(listen_id); | |
730 | err0: | |
731 | kfree(cma_xprt); | |
732 | return ERR_PTR(ret); | |
377f9b2f TT |
733 | } |
734 | ||
735 | /* | |
736 | * This is the xpo_recvfrom function for listening endpoints. Its | |
737 | * purpose is to accept incoming connections. The CMA callback handler | |
738 | * has already created a new transport and attached it to the new CMA | |
739 | * ID. | |
740 | * | |
741 | * There is a queue of pending connections hung on the listening | |
742 | * transport. This queue contains the new svc_xprt structure. This | |
743 | * function takes svc_xprt structures off the accept_q and completes | |
744 | * the connection. | |
745 | */ | |
746 | static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt) | |
747 | { | |
748 | struct svcxprt_rdma *listen_rdma; | |
749 | struct svcxprt_rdma *newxprt = NULL; | |
750 | struct rdma_conn_param conn_param; | |
751 | struct ib_qp_init_attr qp_attr; | |
752 | struct ib_device_attr devattr; | |
753 | struct sockaddr *sa; | |
754 | int ret; | |
755 | int i; | |
756 | ||
757 | listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt); | |
758 | clear_bit(XPT_CONN, &xprt->xpt_flags); | |
759 | /* Get the next entry off the accept list */ | |
760 | spin_lock_bh(&listen_rdma->sc_lock); | |
761 | if (!list_empty(&listen_rdma->sc_accept_q)) { | |
762 | newxprt = list_entry(listen_rdma->sc_accept_q.next, | |
763 | struct svcxprt_rdma, sc_accept_q); | |
764 | list_del_init(&newxprt->sc_accept_q); | |
765 | } | |
766 | if (!list_empty(&listen_rdma->sc_accept_q)) | |
767 | set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags); | |
768 | spin_unlock_bh(&listen_rdma->sc_lock); | |
769 | if (!newxprt) | |
770 | return NULL; | |
771 | ||
772 | dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n", | |
773 | newxprt, newxprt->sc_cm_id); | |
774 | ||
775 | ret = ib_query_device(newxprt->sc_cm_id->device, &devattr); | |
776 | if (ret) { | |
777 | dprintk("svcrdma: could not query device attributes on " | |
778 | "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret); | |
779 | goto errout; | |
780 | } | |
781 | ||
782 | /* Qualify the transport resource defaults with the | |
783 | * capabilities of this particular device */ | |
784 | newxprt->sc_max_sge = min((size_t)devattr.max_sge, | |
785 | (size_t)RPCSVC_MAXPAGES); | |
786 | newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr, | |
787 | (size_t)svcrdma_max_requests); | |
788 | newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests; | |
789 | ||
790 | newxprt->sc_ord = min((size_t)devattr.max_qp_rd_atom, | |
791 | (size_t)svcrdma_ord); | |
792 | ||
793 | newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device); | |
794 | if (IS_ERR(newxprt->sc_pd)) { | |
795 | dprintk("svcrdma: error creating PD for connect request\n"); | |
796 | goto errout; | |
797 | } | |
798 | newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device, | |
799 | sq_comp_handler, | |
800 | cq_event_handler, | |
801 | newxprt, | |
802 | newxprt->sc_sq_depth, | |
803 | 0); | |
804 | if (IS_ERR(newxprt->sc_sq_cq)) { | |
805 | dprintk("svcrdma: error creating SQ CQ for connect request\n"); | |
806 | goto errout; | |
807 | } | |
808 | newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device, | |
809 | rq_comp_handler, | |
810 | cq_event_handler, | |
811 | newxprt, | |
812 | newxprt->sc_max_requests, | |
813 | 0); | |
814 | if (IS_ERR(newxprt->sc_rq_cq)) { | |
815 | dprintk("svcrdma: error creating RQ CQ for connect request\n"); | |
816 | goto errout; | |
817 | } | |
818 | ||
819 | memset(&qp_attr, 0, sizeof qp_attr); | |
820 | qp_attr.event_handler = qp_event_handler; | |
821 | qp_attr.qp_context = &newxprt->sc_xprt; | |
822 | qp_attr.cap.max_send_wr = newxprt->sc_sq_depth; | |
823 | qp_attr.cap.max_recv_wr = newxprt->sc_max_requests; | |
824 | qp_attr.cap.max_send_sge = newxprt->sc_max_sge; | |
825 | qp_attr.cap.max_recv_sge = newxprt->sc_max_sge; | |
826 | qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; | |
827 | qp_attr.qp_type = IB_QPT_RC; | |
828 | qp_attr.send_cq = newxprt->sc_sq_cq; | |
829 | qp_attr.recv_cq = newxprt->sc_rq_cq; | |
830 | dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n" | |
831 | " cm_id->device=%p, sc_pd->device=%p\n" | |
832 | " cap.max_send_wr = %d\n" | |
833 | " cap.max_recv_wr = %d\n" | |
834 | " cap.max_send_sge = %d\n" | |
835 | " cap.max_recv_sge = %d\n", | |
836 | newxprt->sc_cm_id, newxprt->sc_pd, | |
837 | newxprt->sc_cm_id->device, newxprt->sc_pd->device, | |
838 | qp_attr.cap.max_send_wr, | |
839 | qp_attr.cap.max_recv_wr, | |
840 | qp_attr.cap.max_send_sge, | |
841 | qp_attr.cap.max_recv_sge); | |
842 | ||
843 | ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr); | |
844 | if (ret) { | |
845 | /* | |
846 | * XXX: This is a hack. We need a xx_request_qp interface | |
847 | * that will adjust the qp_attr's with a best-effort | |
848 | * number | |
849 | */ | |
850 | qp_attr.cap.max_send_sge -= 2; | |
851 | qp_attr.cap.max_recv_sge -= 2; | |
852 | ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, | |
853 | &qp_attr); | |
854 | if (ret) { | |
855 | dprintk("svcrdma: failed to create QP, ret=%d\n", ret); | |
856 | goto errout; | |
857 | } | |
858 | newxprt->sc_max_sge = qp_attr.cap.max_send_sge; | |
859 | newxprt->sc_max_sge = qp_attr.cap.max_recv_sge; | |
860 | newxprt->sc_sq_depth = qp_attr.cap.max_send_wr; | |
861 | newxprt->sc_max_requests = qp_attr.cap.max_recv_wr; | |
862 | } | |
863 | newxprt->sc_qp = newxprt->sc_cm_id->qp; | |
864 | ||
865 | /* Register all of physical memory */ | |
866 | newxprt->sc_phys_mr = ib_get_dma_mr(newxprt->sc_pd, | |
867 | IB_ACCESS_LOCAL_WRITE | | |
868 | IB_ACCESS_REMOTE_WRITE); | |
869 | if (IS_ERR(newxprt->sc_phys_mr)) { | |
870 | dprintk("svcrdma: Failed to create DMA MR ret=%d\n", ret); | |
871 | goto errout; | |
872 | } | |
873 | ||
874 | /* Post receive buffers */ | |
875 | for (i = 0; i < newxprt->sc_max_requests; i++) { | |
876 | ret = svc_rdma_post_recv(newxprt); | |
877 | if (ret) { | |
878 | dprintk("svcrdma: failure posting receive buffers\n"); | |
879 | goto errout; | |
880 | } | |
881 | } | |
882 | ||
883 | /* Swap out the handler */ | |
884 | newxprt->sc_cm_id->event_handler = rdma_cma_handler; | |
885 | ||
886 | /* Accept Connection */ | |
887 | set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags); | |
888 | memset(&conn_param, 0, sizeof conn_param); | |
889 | conn_param.responder_resources = 0; | |
890 | conn_param.initiator_depth = newxprt->sc_ord; | |
891 | ret = rdma_accept(newxprt->sc_cm_id, &conn_param); | |
892 | if (ret) { | |
893 | dprintk("svcrdma: failed to accept new connection, ret=%d\n", | |
894 | ret); | |
895 | goto errout; | |
896 | } | |
897 | ||
898 | dprintk("svcrdma: new connection %p accepted with the following " | |
899 | "attributes:\n" | |
900 | " local_ip : %d.%d.%d.%d\n" | |
901 | " local_port : %d\n" | |
902 | " remote_ip : %d.%d.%d.%d\n" | |
903 | " remote_port : %d\n" | |
904 | " max_sge : %d\n" | |
905 | " sq_depth : %d\n" | |
906 | " max_requests : %d\n" | |
907 | " ord : %d\n", | |
908 | newxprt, | |
909 | NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id-> | |
910 | route.addr.src_addr)->sin_addr.s_addr), | |
911 | ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id-> | |
912 | route.addr.src_addr)->sin_port), | |
913 | NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id-> | |
914 | route.addr.dst_addr)->sin_addr.s_addr), | |
915 | ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id-> | |
916 | route.addr.dst_addr)->sin_port), | |
917 | newxprt->sc_max_sge, | |
918 | newxprt->sc_sq_depth, | |
919 | newxprt->sc_max_requests, | |
920 | newxprt->sc_ord); | |
921 | ||
922 | /* Set the local and remote addresses in the transport */ | |
923 | sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr; | |
924 | svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa)); | |
925 | sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr; | |
926 | svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa)); | |
927 | ||
928 | ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP); | |
929 | ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP); | |
930 | return &newxprt->sc_xprt; | |
931 | ||
932 | errout: | |
933 | dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret); | |
c48cbb40 TT |
934 | /* Take a reference in case the DTO handler runs */ |
935 | svc_xprt_get(&newxprt->sc_xprt); | |
1711386c | 936 | if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp)) |
c48cbb40 | 937 | ib_destroy_qp(newxprt->sc_qp); |
377f9b2f | 938 | rdma_destroy_id(newxprt->sc_cm_id); |
c48cbb40 TT |
939 | /* This call to put will destroy the transport */ |
940 | svc_xprt_put(&newxprt->sc_xprt); | |
377f9b2f TT |
941 | return NULL; |
942 | } | |
943 | ||
377f9b2f TT |
944 | static void svc_rdma_release_rqst(struct svc_rqst *rqstp) |
945 | { | |
377f9b2f TT |
946 | } |
947 | ||
c48cbb40 | 948 | /* |
1711386c | 949 | * When connected, an svc_xprt has at least two references: |
c48cbb40 TT |
950 | * |
951 | * - A reference held by the cm_id between the ESTABLISHED and | |
952 | * DISCONNECTED events. If the remote peer disconnected first, this | |
953 | * reference could be gone. | |
954 | * | |
955 | * - A reference held by the svc_recv code that called this function | |
956 | * as part of close processing. | |
957 | * | |
1711386c | 958 | * At a minimum one references should still be held. |
c48cbb40 | 959 | */ |
377f9b2f TT |
960 | static void svc_rdma_detach(struct svc_xprt *xprt) |
961 | { | |
962 | struct svcxprt_rdma *rdma = | |
963 | container_of(xprt, struct svcxprt_rdma, sc_xprt); | |
377f9b2f | 964 | dprintk("svc: svc_rdma_detach(%p)\n", xprt); |
c48cbb40 TT |
965 | |
966 | /* Disconnect and flush posted WQE */ | |
377f9b2f | 967 | rdma_disconnect(rdma->sc_cm_id); |
377f9b2f TT |
968 | } |
969 | ||
8da91ea8 | 970 | static void __svc_rdma_free(struct work_struct *work) |
377f9b2f | 971 | { |
8da91ea8 TT |
972 | struct svcxprt_rdma *rdma = |
973 | container_of(work, struct svcxprt_rdma, sc_work); | |
377f9b2f | 974 | dprintk("svcrdma: svc_rdma_free(%p)\n", rdma); |
8da91ea8 | 975 | |
c48cbb40 | 976 | /* We should only be called from kref_put */ |
8da91ea8 TT |
977 | BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0); |
978 | ||
1711386c TT |
979 | /* Destroy the QP if present (not a listener) */ |
980 | if (rdma->sc_qp && !IS_ERR(rdma->sc_qp)) | |
981 | ib_destroy_qp(rdma->sc_qp); | |
982 | ||
983 | /* Destroy the CM ID */ | |
984 | rdma_destroy_id(rdma->sc_cm_id); | |
985 | ||
c48cbb40 TT |
986 | if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq)) |
987 | ib_destroy_cq(rdma->sc_sq_cq); | |
377f9b2f | 988 | |
c48cbb40 TT |
989 | if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq)) |
990 | ib_destroy_cq(rdma->sc_rq_cq); | |
377f9b2f | 991 | |
c48cbb40 TT |
992 | if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr)) |
993 | ib_dereg_mr(rdma->sc_phys_mr); | |
377f9b2f | 994 | |
c48cbb40 TT |
995 | if (rdma->sc_pd && !IS_ERR(rdma->sc_pd)) |
996 | ib_dealloc_pd(rdma->sc_pd); | |
377f9b2f | 997 | |
87407673 | 998 | destroy_context_cache(rdma); |
c48cbb40 | 999 | kfree(rdma); |
377f9b2f TT |
1000 | } |
1001 | ||
8da91ea8 TT |
1002 | static void svc_rdma_free(struct svc_xprt *xprt) |
1003 | { | |
1004 | struct svcxprt_rdma *rdma = | |
1005 | container_of(xprt, struct svcxprt_rdma, sc_xprt); | |
1006 | INIT_WORK(&rdma->sc_work, __svc_rdma_free); | |
1007 | schedule_work(&rdma->sc_work); | |
1008 | } | |
1009 | ||
377f9b2f TT |
1010 | static int svc_rdma_has_wspace(struct svc_xprt *xprt) |
1011 | { | |
1012 | struct svcxprt_rdma *rdma = | |
1013 | container_of(xprt, struct svcxprt_rdma, sc_xprt); | |
1014 | ||
1015 | /* | |
1016 | * If there are fewer SQ WR available than required to send a | |
1017 | * simple response, return false. | |
1018 | */ | |
1019 | if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3)) | |
1020 | return 0; | |
1021 | ||
1022 | /* | |
1023 | * ...or there are already waiters on the SQ, | |
1024 | * return false. | |
1025 | */ | |
1026 | if (waitqueue_active(&rdma->sc_send_wait)) | |
1027 | return 0; | |
1028 | ||
1029 | /* Otherwise return true. */ | |
1030 | return 1; | |
1031 | } | |
1032 | ||
1033 | int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr) | |
1034 | { | |
1035 | struct ib_send_wr *bad_wr; | |
1036 | int ret; | |
1037 | ||
1038 | if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags)) | |
9d6347ac | 1039 | return -ENOTCONN; |
377f9b2f TT |
1040 | |
1041 | BUG_ON(wr->send_flags != IB_SEND_SIGNALED); | |
1042 | BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op != | |
1043 | wr->opcode); | |
1044 | /* If the SQ is full, wait until an SQ entry is available */ | |
1045 | while (1) { | |
1046 | spin_lock_bh(&xprt->sc_lock); | |
1047 | if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) { | |
1048 | spin_unlock_bh(&xprt->sc_lock); | |
1049 | atomic_inc(&rdma_stat_sq_starve); | |
dbcd00eb TT |
1050 | |
1051 | /* See if we can opportunistically reap SQ WR to make room */ | |
377f9b2f TT |
1052 | sq_cq_reap(xprt); |
1053 | ||
1054 | /* Wait until SQ WR available if SQ still full */ | |
1055 | wait_event(xprt->sc_send_wait, | |
1056 | atomic_read(&xprt->sc_sq_count) < | |
1057 | xprt->sc_sq_depth); | |
830bb59b TT |
1058 | if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags)) |
1059 | return 0; | |
377f9b2f TT |
1060 | continue; |
1061 | } | |
1062 | /* Bumped used SQ WR count and post */ | |
0905c0f0 | 1063 | svc_xprt_get(&xprt->sc_xprt); |
377f9b2f TT |
1064 | ret = ib_post_send(xprt->sc_qp, wr, &bad_wr); |
1065 | if (!ret) | |
1066 | atomic_inc(&xprt->sc_sq_count); | |
0905c0f0 TT |
1067 | else { |
1068 | svc_xprt_put(&xprt->sc_xprt); | |
377f9b2f TT |
1069 | dprintk("svcrdma: failed to post SQ WR rc=%d, " |
1070 | "sc_sq_count=%d, sc_sq_depth=%d\n", | |
1071 | ret, atomic_read(&xprt->sc_sq_count), | |
1072 | xprt->sc_sq_depth); | |
0905c0f0 | 1073 | } |
377f9b2f TT |
1074 | spin_unlock_bh(&xprt->sc_lock); |
1075 | break; | |
1076 | } | |
1077 | return ret; | |
1078 | } | |
1079 | ||
1080 | int svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp, | |
1081 | enum rpcrdma_errcode err) | |
1082 | { | |
1083 | struct ib_send_wr err_wr; | |
1084 | struct ib_sge sge; | |
1085 | struct page *p; | |
1086 | struct svc_rdma_op_ctxt *ctxt; | |
1087 | u32 *va; | |
1088 | int length; | |
1089 | int ret; | |
1090 | ||
1091 | p = svc_rdma_get_page(); | |
1092 | va = page_address(p); | |
1093 | ||
1094 | /* XDR encode error */ | |
1095 | length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va); | |
1096 | ||
1097 | /* Prepare SGE for local address */ | |
1098 | sge.addr = ib_dma_map_page(xprt->sc_cm_id->device, | |
1099 | p, 0, PAGE_SIZE, DMA_FROM_DEVICE); | |
1100 | sge.lkey = xprt->sc_phys_mr->lkey; | |
1101 | sge.length = length; | |
1102 | ||
1103 | ctxt = svc_rdma_get_context(xprt); | |
1104 | ctxt->count = 1; | |
1105 | ctxt->pages[0] = p; | |
1106 | ||
1107 | /* Prepare SEND WR */ | |
1108 | memset(&err_wr, 0, sizeof err_wr); | |
1109 | ctxt->wr_op = IB_WR_SEND; | |
1110 | err_wr.wr_id = (unsigned long)ctxt; | |
1111 | err_wr.sg_list = &sge; | |
1112 | err_wr.num_sge = 1; | |
1113 | err_wr.opcode = IB_WR_SEND; | |
1114 | err_wr.send_flags = IB_SEND_SIGNALED; | |
1115 | ||
1116 | /* Post It */ | |
1117 | ret = svc_rdma_send(xprt, &err_wr); | |
1118 | if (ret) { | |
1119 | dprintk("svcrdma: Error posting send = %d\n", ret); | |
1120 | svc_rdma_put_context(ctxt, 1); | |
1121 | } | |
1122 | ||
1123 | return ret; | |
1124 | } |