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