svcrdma: Query device for Fast Reg support during connection setup
[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 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 static 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 * Send Queue Completion Handler - potentially called on interrupt context.
330 *
331 * Note that caller must hold a transport reference.
332 */
333 static void sq_cq_reap(struct svcxprt_rdma *xprt)
334 {
335 struct svc_rdma_op_ctxt *ctxt = NULL;
336 struct ib_wc wc;
337 struct ib_cq *cq = xprt->sc_sq_cq;
338 int ret;
339
340
341 if (!test_and_clear_bit(RDMAXPRT_SQ_PENDING, &xprt->sc_flags))
342 return;
343
344 ib_req_notify_cq(xprt->sc_sq_cq, IB_CQ_NEXT_COMP);
345 atomic_inc(&rdma_stat_sq_poll);
346 while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) {
347 ctxt = (struct svc_rdma_op_ctxt *)(unsigned long)wc.wr_id;
348 xprt = ctxt->xprt;
349
350 svc_rdma_unmap_dma(ctxt);
351 if (wc.status != IB_WC_SUCCESS)
352 /* Close the transport */
353 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
354
355 /* Decrement used SQ WR count */
356 atomic_dec(&xprt->sc_sq_count);
357 wake_up(&xprt->sc_send_wait);
358
359 switch (ctxt->wr_op) {
360 case IB_WR_SEND:
361 svc_rdma_put_context(ctxt, 1);
362 break;
363
364 case IB_WR_RDMA_WRITE:
365 svc_rdma_put_context(ctxt, 0);
366 break;
367
368 case IB_WR_RDMA_READ:
369 if (test_bit(RDMACTXT_F_LAST_CTXT, &ctxt->flags)) {
370 struct svc_rdma_op_ctxt *read_hdr = ctxt->read_hdr;
371 BUG_ON(!read_hdr);
372 spin_lock_bh(&xprt->sc_rq_dto_lock);
373 set_bit(XPT_DATA, &xprt->sc_xprt.xpt_flags);
374 list_add_tail(&read_hdr->dto_q,
375 &xprt->sc_read_complete_q);
376 spin_unlock_bh(&xprt->sc_rq_dto_lock);
377 svc_xprt_enqueue(&xprt->sc_xprt);
378 }
379 svc_rdma_put_context(ctxt, 0);
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 }
388 svc_xprt_put(&xprt->sc_xprt);
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
400 /* Guard against unconditional flush call for destroyed QP */
401 if (atomic_read(&xprt->sc_xprt.xpt_ref.refcount)==0)
402 return;
403
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.
408 */
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);
416 if (list_empty(&xprt->sc_dto_q)) {
417 svc_xprt_get(&xprt->sc_xprt);
418 list_add_tail(&xprt->sc_dto_q, &dto_xprt_q);
419 }
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 struct svcxprt_rdma *rdma_create_xprt(struct svc_serv *serv,
427 int listener)
428 {
429 struct svcxprt_rdma *cma_xprt = kzalloc(sizeof *cma_xprt, GFP_KERNEL);
430
431 if (!cma_xprt)
432 return NULL;
433 svc_xprt_init(&svc_rdma_class, &cma_xprt->sc_xprt, serv);
434 INIT_LIST_HEAD(&cma_xprt->sc_accept_q);
435 INIT_LIST_HEAD(&cma_xprt->sc_dto_q);
436 INIT_LIST_HEAD(&cma_xprt->sc_rq_dto_q);
437 INIT_LIST_HEAD(&cma_xprt->sc_read_complete_q);
438 INIT_LIST_HEAD(&cma_xprt->sc_frmr_q);
439 init_waitqueue_head(&cma_xprt->sc_send_wait);
440
441 spin_lock_init(&cma_xprt->sc_lock);
442 spin_lock_init(&cma_xprt->sc_rq_dto_lock);
443 spin_lock_init(&cma_xprt->sc_frmr_q_lock);
444
445 cma_xprt->sc_ord = svcrdma_ord;
446
447 cma_xprt->sc_max_req_size = svcrdma_max_req_size;
448 cma_xprt->sc_max_requests = svcrdma_max_requests;
449 cma_xprt->sc_sq_depth = svcrdma_max_requests * RPCRDMA_SQ_DEPTH_MULT;
450 atomic_set(&cma_xprt->sc_sq_count, 0);
451 atomic_set(&cma_xprt->sc_ctxt_used, 0);
452
453 if (listener)
454 set_bit(XPT_LISTENER, &cma_xprt->sc_xprt.xpt_flags);
455
456 return cma_xprt;
457 }
458
459 struct page *svc_rdma_get_page(void)
460 {
461 struct page *page;
462
463 while ((page = alloc_page(GFP_KERNEL)) == NULL) {
464 /* If we can't get memory, wait a bit and try again */
465 printk(KERN_INFO "svcrdma: out of memory...retrying in 1000 "
466 "jiffies.\n");
467 schedule_timeout_uninterruptible(msecs_to_jiffies(1000));
468 }
469 return page;
470 }
471
472 int svc_rdma_post_recv(struct svcxprt_rdma *xprt)
473 {
474 struct ib_recv_wr recv_wr, *bad_recv_wr;
475 struct svc_rdma_op_ctxt *ctxt;
476 struct page *page;
477 unsigned long pa;
478 int sge_no;
479 int buflen;
480 int ret;
481
482 ctxt = svc_rdma_get_context(xprt);
483 buflen = 0;
484 ctxt->direction = DMA_FROM_DEVICE;
485 for (sge_no = 0; buflen < xprt->sc_max_req_size; sge_no++) {
486 BUG_ON(sge_no >= xprt->sc_max_sge);
487 page = svc_rdma_get_page();
488 ctxt->pages[sge_no] = page;
489 atomic_inc(&xprt->sc_dma_used);
490 pa = ib_dma_map_page(xprt->sc_cm_id->device,
491 page, 0, PAGE_SIZE,
492 DMA_FROM_DEVICE);
493 ctxt->sge[sge_no].addr = pa;
494 ctxt->sge[sge_no].length = PAGE_SIZE;
495 ctxt->sge[sge_no].lkey = xprt->sc_phys_mr->lkey;
496 buflen += PAGE_SIZE;
497 }
498 ctxt->count = sge_no;
499 recv_wr.next = NULL;
500 recv_wr.sg_list = &ctxt->sge[0];
501 recv_wr.num_sge = ctxt->count;
502 recv_wr.wr_id = (u64)(unsigned long)ctxt;
503
504 svc_xprt_get(&xprt->sc_xprt);
505 ret = ib_post_recv(xprt->sc_qp, &recv_wr, &bad_recv_wr);
506 if (ret) {
507 svc_xprt_put(&xprt->sc_xprt);
508 svc_rdma_put_context(ctxt, 1);
509 }
510 return ret;
511 }
512
513 /*
514 * This function handles the CONNECT_REQUEST event on a listening
515 * endpoint. It is passed the cma_id for the _new_ connection. The context in
516 * this cma_id is inherited from the listening cma_id and is the svc_xprt
517 * structure for the listening endpoint.
518 *
519 * This function creates a new xprt for the new connection and enqueues it on
520 * the accept queue for the listent xprt. When the listen thread is kicked, it
521 * will call the recvfrom method on the listen xprt which will accept the new
522 * connection.
523 */
524 static void handle_connect_req(struct rdma_cm_id *new_cma_id, size_t client_ird)
525 {
526 struct svcxprt_rdma *listen_xprt = new_cma_id->context;
527 struct svcxprt_rdma *newxprt;
528 struct sockaddr *sa;
529
530 /* Create a new transport */
531 newxprt = rdma_create_xprt(listen_xprt->sc_xprt.xpt_server, 0);
532 if (!newxprt) {
533 dprintk("svcrdma: failed to create new transport\n");
534 return;
535 }
536 newxprt->sc_cm_id = new_cma_id;
537 new_cma_id->context = newxprt;
538 dprintk("svcrdma: Creating newxprt=%p, cm_id=%p, listenxprt=%p\n",
539 newxprt, newxprt->sc_cm_id, listen_xprt);
540
541 /* Save client advertised inbound read limit for use later in accept. */
542 newxprt->sc_ord = client_ird;
543
544 /* Set the local and remote addresses in the transport */
545 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.dst_addr;
546 svc_xprt_set_remote(&newxprt->sc_xprt, sa, svc_addr_len(sa));
547 sa = (struct sockaddr *)&newxprt->sc_cm_id->route.addr.src_addr;
548 svc_xprt_set_local(&newxprt->sc_xprt, sa, svc_addr_len(sa));
549
550 /*
551 * Enqueue the new transport on the accept queue of the listening
552 * transport
553 */
554 spin_lock_bh(&listen_xprt->sc_lock);
555 list_add_tail(&newxprt->sc_accept_q, &listen_xprt->sc_accept_q);
556 spin_unlock_bh(&listen_xprt->sc_lock);
557
558 /*
559 * Can't use svc_xprt_received here because we are not on a
560 * rqstp thread
561 */
562 set_bit(XPT_CONN, &listen_xprt->sc_xprt.xpt_flags);
563 svc_xprt_enqueue(&listen_xprt->sc_xprt);
564 }
565
566 /*
567 * Handles events generated on the listening endpoint. These events will be
568 * either be incoming connect requests or adapter removal events.
569 */
570 static int rdma_listen_handler(struct rdma_cm_id *cma_id,
571 struct rdma_cm_event *event)
572 {
573 struct svcxprt_rdma *xprt = cma_id->context;
574 int ret = 0;
575
576 switch (event->event) {
577 case RDMA_CM_EVENT_CONNECT_REQUEST:
578 dprintk("svcrdma: Connect request on cma_id=%p, xprt = %p, "
579 "event=%d\n", cma_id, cma_id->context, event->event);
580 handle_connect_req(cma_id,
581 event->param.conn.responder_resources);
582 break;
583
584 case RDMA_CM_EVENT_ESTABLISHED:
585 /* Accept complete */
586 dprintk("svcrdma: Connection completed on LISTEN xprt=%p, "
587 "cm_id=%p\n", xprt, cma_id);
588 break;
589
590 case RDMA_CM_EVENT_DEVICE_REMOVAL:
591 dprintk("svcrdma: Device removal xprt=%p, cm_id=%p\n",
592 xprt, cma_id);
593 if (xprt)
594 set_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags);
595 break;
596
597 default:
598 dprintk("svcrdma: Unexpected event on listening endpoint %p, "
599 "event=%d\n", cma_id, event->event);
600 break;
601 }
602
603 return ret;
604 }
605
606 static int rdma_cma_handler(struct rdma_cm_id *cma_id,
607 struct rdma_cm_event *event)
608 {
609 struct svc_xprt *xprt = cma_id->context;
610 struct svcxprt_rdma *rdma =
611 container_of(xprt, struct svcxprt_rdma, sc_xprt);
612 switch (event->event) {
613 case RDMA_CM_EVENT_ESTABLISHED:
614 /* Accept complete */
615 svc_xprt_get(xprt);
616 dprintk("svcrdma: Connection completed on DTO xprt=%p, "
617 "cm_id=%p\n", xprt, cma_id);
618 clear_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags);
619 svc_xprt_enqueue(xprt);
620 break;
621 case RDMA_CM_EVENT_DISCONNECTED:
622 dprintk("svcrdma: Disconnect on DTO xprt=%p, cm_id=%p\n",
623 xprt, cma_id);
624 if (xprt) {
625 set_bit(XPT_CLOSE, &xprt->xpt_flags);
626 svc_xprt_enqueue(xprt);
627 svc_xprt_put(xprt);
628 }
629 break;
630 case RDMA_CM_EVENT_DEVICE_REMOVAL:
631 dprintk("svcrdma: Device removal cma_id=%p, xprt = %p, "
632 "event=%d\n", cma_id, xprt, event->event);
633 if (xprt) {
634 set_bit(XPT_CLOSE, &xprt->xpt_flags);
635 svc_xprt_enqueue(xprt);
636 }
637 break;
638 default:
639 dprintk("svcrdma: Unexpected event on DTO endpoint %p, "
640 "event=%d\n", cma_id, event->event);
641 break;
642 }
643 return 0;
644 }
645
646 /*
647 * Create a listening RDMA service endpoint.
648 */
649 static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
650 struct sockaddr *sa, int salen,
651 int flags)
652 {
653 struct rdma_cm_id *listen_id;
654 struct svcxprt_rdma *cma_xprt;
655 struct svc_xprt *xprt;
656 int ret;
657
658 dprintk("svcrdma: Creating RDMA socket\n");
659
660 cma_xprt = rdma_create_xprt(serv, 1);
661 if (!cma_xprt)
662 return ERR_PTR(-ENOMEM);
663 xprt = &cma_xprt->sc_xprt;
664
665 listen_id = rdma_create_id(rdma_listen_handler, cma_xprt, RDMA_PS_TCP);
666 if (IS_ERR(listen_id)) {
667 ret = PTR_ERR(listen_id);
668 dprintk("svcrdma: rdma_create_id failed = %d\n", ret);
669 goto err0;
670 }
671
672 ret = rdma_bind_addr(listen_id, sa);
673 if (ret) {
674 dprintk("svcrdma: rdma_bind_addr failed = %d\n", ret);
675 goto err1;
676 }
677 cma_xprt->sc_cm_id = listen_id;
678
679 ret = rdma_listen(listen_id, RPCRDMA_LISTEN_BACKLOG);
680 if (ret) {
681 dprintk("svcrdma: rdma_listen failed = %d\n", ret);
682 goto err1;
683 }
684
685 /*
686 * We need to use the address from the cm_id in case the
687 * caller specified 0 for the port number.
688 */
689 sa = (struct sockaddr *)&cma_xprt->sc_cm_id->route.addr.src_addr;
690 svc_xprt_set_local(&cma_xprt->sc_xprt, sa, salen);
691
692 return &cma_xprt->sc_xprt;
693
694 err1:
695 rdma_destroy_id(listen_id);
696 err0:
697 kfree(cma_xprt);
698 return ERR_PTR(ret);
699 }
700
701 static struct svc_rdma_fastreg_mr *rdma_alloc_frmr(struct svcxprt_rdma *xprt)
702 {
703 struct ib_mr *mr;
704 struct ib_fast_reg_page_list *pl;
705 struct svc_rdma_fastreg_mr *frmr;
706
707 frmr = kmalloc(sizeof(*frmr), GFP_KERNEL);
708 if (!frmr)
709 goto err;
710
711 mr = ib_alloc_fast_reg_mr(xprt->sc_pd, RPCSVC_MAXPAGES);
712 if (!mr)
713 goto err_free_frmr;
714
715 pl = ib_alloc_fast_reg_page_list(xprt->sc_cm_id->device,
716 RPCSVC_MAXPAGES);
717 if (!pl)
718 goto err_free_mr;
719
720 frmr->mr = mr;
721 frmr->page_list = pl;
722 INIT_LIST_HEAD(&frmr->frmr_list);
723 return frmr;
724
725 err_free_mr:
726 ib_dereg_mr(mr);
727 err_free_frmr:
728 kfree(frmr);
729 err:
730 return ERR_PTR(-ENOMEM);
731 }
732
733 static void rdma_dealloc_frmr_q(struct svcxprt_rdma *xprt)
734 {
735 struct svc_rdma_fastreg_mr *frmr;
736
737 while (!list_empty(&xprt->sc_frmr_q)) {
738 frmr = list_entry(xprt->sc_frmr_q.next,
739 struct svc_rdma_fastreg_mr, frmr_list);
740 list_del_init(&frmr->frmr_list);
741 ib_dereg_mr(frmr->mr);
742 ib_free_fast_reg_page_list(frmr->page_list);
743 kfree(frmr);
744 }
745 }
746
747 struct svc_rdma_fastreg_mr *svc_rdma_get_frmr(struct svcxprt_rdma *rdma)
748 {
749 struct svc_rdma_fastreg_mr *frmr = NULL;
750
751 spin_lock_bh(&rdma->sc_frmr_q_lock);
752 if (!list_empty(&rdma->sc_frmr_q)) {
753 frmr = list_entry(rdma->sc_frmr_q.next,
754 struct svc_rdma_fastreg_mr, frmr_list);
755 list_del_init(&frmr->frmr_list);
756 frmr->map_len = 0;
757 frmr->page_list_len = 0;
758 }
759 spin_unlock_bh(&rdma->sc_frmr_q_lock);
760 if (frmr)
761 return frmr;
762
763 return rdma_alloc_frmr(rdma);
764 }
765
766 static void frmr_unmap_dma(struct svcxprt_rdma *xprt,
767 struct svc_rdma_fastreg_mr *frmr)
768 {
769 int page_no;
770 for (page_no = 0; page_no < frmr->page_list_len; page_no++) {
771 dma_addr_t addr = frmr->page_list->page_list[page_no];
772 if (ib_dma_mapping_error(frmr->mr->device, addr))
773 continue;
774 atomic_dec(&xprt->sc_dma_used);
775 ib_dma_unmap_single(frmr->mr->device, addr, PAGE_SIZE,
776 frmr->direction);
777 }
778 }
779
780 void svc_rdma_put_frmr(struct svcxprt_rdma *rdma,
781 struct svc_rdma_fastreg_mr *frmr)
782 {
783 if (frmr) {
784 frmr_unmap_dma(rdma, frmr);
785 spin_lock_bh(&rdma->sc_frmr_q_lock);
786 BUG_ON(!list_empty(&frmr->frmr_list));
787 list_add(&frmr->frmr_list, &rdma->sc_frmr_q);
788 spin_unlock_bh(&rdma->sc_frmr_q_lock);
789 }
790 }
791
792 /*
793 * This is the xpo_recvfrom function for listening endpoints. Its
794 * purpose is to accept incoming connections. The CMA callback handler
795 * has already created a new transport and attached it to the new CMA
796 * ID.
797 *
798 * There is a queue of pending connections hung on the listening
799 * transport. This queue contains the new svc_xprt structure. This
800 * function takes svc_xprt structures off the accept_q and completes
801 * the connection.
802 */
803 static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt)
804 {
805 struct svcxprt_rdma *listen_rdma;
806 struct svcxprt_rdma *newxprt = NULL;
807 struct rdma_conn_param conn_param;
808 struct ib_qp_init_attr qp_attr;
809 struct ib_device_attr devattr;
810 int dma_mr_acc;
811 int need_dma_mr;
812 int ret;
813 int i;
814
815 listen_rdma = container_of(xprt, struct svcxprt_rdma, sc_xprt);
816 clear_bit(XPT_CONN, &xprt->xpt_flags);
817 /* Get the next entry off the accept list */
818 spin_lock_bh(&listen_rdma->sc_lock);
819 if (!list_empty(&listen_rdma->sc_accept_q)) {
820 newxprt = list_entry(listen_rdma->sc_accept_q.next,
821 struct svcxprt_rdma, sc_accept_q);
822 list_del_init(&newxprt->sc_accept_q);
823 }
824 if (!list_empty(&listen_rdma->sc_accept_q))
825 set_bit(XPT_CONN, &listen_rdma->sc_xprt.xpt_flags);
826 spin_unlock_bh(&listen_rdma->sc_lock);
827 if (!newxprt)
828 return NULL;
829
830 dprintk("svcrdma: newxprt from accept queue = %p, cm_id=%p\n",
831 newxprt, newxprt->sc_cm_id);
832
833 ret = ib_query_device(newxprt->sc_cm_id->device, &devattr);
834 if (ret) {
835 dprintk("svcrdma: could not query device attributes on "
836 "device %p, rc=%d\n", newxprt->sc_cm_id->device, ret);
837 goto errout;
838 }
839
840 /* Qualify the transport resource defaults with the
841 * capabilities of this particular device */
842 newxprt->sc_max_sge = min((size_t)devattr.max_sge,
843 (size_t)RPCSVC_MAXPAGES);
844 newxprt->sc_max_requests = min((size_t)devattr.max_qp_wr,
845 (size_t)svcrdma_max_requests);
846 newxprt->sc_sq_depth = RPCRDMA_SQ_DEPTH_MULT * newxprt->sc_max_requests;
847
848 /*
849 * Limit ORD based on client limit, local device limit, and
850 * configured svcrdma limit.
851 */
852 newxprt->sc_ord = min_t(size_t, devattr.max_qp_rd_atom, newxprt->sc_ord);
853 newxprt->sc_ord = min_t(size_t, svcrdma_ord, newxprt->sc_ord);
854
855 newxprt->sc_pd = ib_alloc_pd(newxprt->sc_cm_id->device);
856 if (IS_ERR(newxprt->sc_pd)) {
857 dprintk("svcrdma: error creating PD for connect request\n");
858 goto errout;
859 }
860 newxprt->sc_sq_cq = ib_create_cq(newxprt->sc_cm_id->device,
861 sq_comp_handler,
862 cq_event_handler,
863 newxprt,
864 newxprt->sc_sq_depth,
865 0);
866 if (IS_ERR(newxprt->sc_sq_cq)) {
867 dprintk("svcrdma: error creating SQ CQ for connect request\n");
868 goto errout;
869 }
870 newxprt->sc_rq_cq = ib_create_cq(newxprt->sc_cm_id->device,
871 rq_comp_handler,
872 cq_event_handler,
873 newxprt,
874 newxprt->sc_max_requests,
875 0);
876 if (IS_ERR(newxprt->sc_rq_cq)) {
877 dprintk("svcrdma: error creating RQ CQ for connect request\n");
878 goto errout;
879 }
880
881 memset(&qp_attr, 0, sizeof qp_attr);
882 qp_attr.event_handler = qp_event_handler;
883 qp_attr.qp_context = &newxprt->sc_xprt;
884 qp_attr.cap.max_send_wr = newxprt->sc_sq_depth;
885 qp_attr.cap.max_recv_wr = newxprt->sc_max_requests;
886 qp_attr.cap.max_send_sge = newxprt->sc_max_sge;
887 qp_attr.cap.max_recv_sge = newxprt->sc_max_sge;
888 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
889 qp_attr.qp_type = IB_QPT_RC;
890 qp_attr.send_cq = newxprt->sc_sq_cq;
891 qp_attr.recv_cq = newxprt->sc_rq_cq;
892 dprintk("svcrdma: newxprt->sc_cm_id=%p, newxprt->sc_pd=%p\n"
893 " cm_id->device=%p, sc_pd->device=%p\n"
894 " cap.max_send_wr = %d\n"
895 " cap.max_recv_wr = %d\n"
896 " cap.max_send_sge = %d\n"
897 " cap.max_recv_sge = %d\n",
898 newxprt->sc_cm_id, newxprt->sc_pd,
899 newxprt->sc_cm_id->device, newxprt->sc_pd->device,
900 qp_attr.cap.max_send_wr,
901 qp_attr.cap.max_recv_wr,
902 qp_attr.cap.max_send_sge,
903 qp_attr.cap.max_recv_sge);
904
905 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd, &qp_attr);
906 if (ret) {
907 /*
908 * XXX: This is a hack. We need a xx_request_qp interface
909 * that will adjust the qp_attr's with a best-effort
910 * number
911 */
912 qp_attr.cap.max_send_sge -= 2;
913 qp_attr.cap.max_recv_sge -= 2;
914 ret = rdma_create_qp(newxprt->sc_cm_id, newxprt->sc_pd,
915 &qp_attr);
916 if (ret) {
917 dprintk("svcrdma: failed to create QP, ret=%d\n", ret);
918 goto errout;
919 }
920 newxprt->sc_max_sge = qp_attr.cap.max_send_sge;
921 newxprt->sc_max_sge = qp_attr.cap.max_recv_sge;
922 newxprt->sc_sq_depth = qp_attr.cap.max_send_wr;
923 newxprt->sc_max_requests = qp_attr.cap.max_recv_wr;
924 }
925 newxprt->sc_qp = newxprt->sc_cm_id->qp;
926
927 /*
928 * Use the most secure set of MR resources based on the
929 * transport type and available memory management features in
930 * the device. Here's the table implemented below:
931 *
932 * Fast Global DMA Remote WR
933 * Reg LKEY MR Access
934 * Sup'd Sup'd Needed Needed
935 *
936 * IWARP N N Y Y
937 * N Y Y Y
938 * Y N Y N
939 * Y Y N -
940 *
941 * IB N N Y N
942 * N Y N -
943 * Y N Y N
944 * Y Y N -
945 *
946 * NB: iWARP requires remote write access for the data sink
947 * of an RDMA_READ. IB does not.
948 */
949 if (devattr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
950 newxprt->sc_frmr_pg_list_len =
951 devattr.max_fast_reg_page_list_len;
952 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_FAST_REG;
953 }
954
955 /*
956 * Determine if a DMA MR is required and if so, what privs are required
957 */
958 switch (rdma_node_get_transport(newxprt->sc_cm_id->device->node_type)) {
959 case RDMA_TRANSPORT_IWARP:
960 newxprt->sc_dev_caps |= SVCRDMA_DEVCAP_READ_W_INV;
961 if (!(newxprt->sc_dev_caps & SVCRDMA_DEVCAP_FAST_REG)) {
962 need_dma_mr = 1;
963 dma_mr_acc =
964 (IB_ACCESS_LOCAL_WRITE |
965 IB_ACCESS_REMOTE_WRITE);
966 } else if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
967 need_dma_mr = 1;
968 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
969 } else
970 need_dma_mr = 0;
971 break;
972 case RDMA_TRANSPORT_IB:
973 if (!(devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) {
974 need_dma_mr = 1;
975 dma_mr_acc = IB_ACCESS_LOCAL_WRITE;
976 } else
977 need_dma_mr = 0;
978 break;
979 default:
980 goto errout;
981 }
982
983 /* Create the DMA MR if needed, otherwise, use the DMA LKEY */
984 if (need_dma_mr) {
985 /* Register all of physical memory */
986 newxprt->sc_phys_mr =
987 ib_get_dma_mr(newxprt->sc_pd, dma_mr_acc);
988 if (IS_ERR(newxprt->sc_phys_mr)) {
989 dprintk("svcrdma: Failed to create DMA MR ret=%d\n",
990 ret);
991 goto errout;
992 }
993 newxprt->sc_dma_lkey = newxprt->sc_phys_mr->lkey;
994 } else
995 newxprt->sc_dma_lkey =
996 newxprt->sc_cm_id->device->local_dma_lkey;
997
998 /* Post receive buffers */
999 for (i = 0; i < newxprt->sc_max_requests; i++) {
1000 ret = svc_rdma_post_recv(newxprt);
1001 if (ret) {
1002 dprintk("svcrdma: failure posting receive buffers\n");
1003 goto errout;
1004 }
1005 }
1006
1007 /* Swap out the handler */
1008 newxprt->sc_cm_id->event_handler = rdma_cma_handler;
1009
1010 /*
1011 * Arm the CQs for the SQ and RQ before accepting so we can't
1012 * miss the first message
1013 */
1014 ib_req_notify_cq(newxprt->sc_sq_cq, IB_CQ_NEXT_COMP);
1015 ib_req_notify_cq(newxprt->sc_rq_cq, IB_CQ_NEXT_COMP);
1016
1017 /* Accept Connection */
1018 set_bit(RDMAXPRT_CONN_PENDING, &newxprt->sc_flags);
1019 memset(&conn_param, 0, sizeof conn_param);
1020 conn_param.responder_resources = 0;
1021 conn_param.initiator_depth = newxprt->sc_ord;
1022 ret = rdma_accept(newxprt->sc_cm_id, &conn_param);
1023 if (ret) {
1024 dprintk("svcrdma: failed to accept new connection, ret=%d\n",
1025 ret);
1026 goto errout;
1027 }
1028
1029 dprintk("svcrdma: new connection %p accepted with the following "
1030 "attributes:\n"
1031 " local_ip : %d.%d.%d.%d\n"
1032 " local_port : %d\n"
1033 " remote_ip : %d.%d.%d.%d\n"
1034 " remote_port : %d\n"
1035 " max_sge : %d\n"
1036 " sq_depth : %d\n"
1037 " max_requests : %d\n"
1038 " ord : %d\n",
1039 newxprt,
1040 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1041 route.addr.src_addr)->sin_addr.s_addr),
1042 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1043 route.addr.src_addr)->sin_port),
1044 NIPQUAD(((struct sockaddr_in *)&newxprt->sc_cm_id->
1045 route.addr.dst_addr)->sin_addr.s_addr),
1046 ntohs(((struct sockaddr_in *)&newxprt->sc_cm_id->
1047 route.addr.dst_addr)->sin_port),
1048 newxprt->sc_max_sge,
1049 newxprt->sc_sq_depth,
1050 newxprt->sc_max_requests,
1051 newxprt->sc_ord);
1052
1053 return &newxprt->sc_xprt;
1054
1055 errout:
1056 dprintk("svcrdma: failure accepting new connection rc=%d.\n", ret);
1057 /* Take a reference in case the DTO handler runs */
1058 svc_xprt_get(&newxprt->sc_xprt);
1059 if (newxprt->sc_qp && !IS_ERR(newxprt->sc_qp))
1060 ib_destroy_qp(newxprt->sc_qp);
1061 rdma_destroy_id(newxprt->sc_cm_id);
1062 /* This call to put will destroy the transport */
1063 svc_xprt_put(&newxprt->sc_xprt);
1064 return NULL;
1065 }
1066
1067 static void svc_rdma_release_rqst(struct svc_rqst *rqstp)
1068 {
1069 }
1070
1071 /*
1072 * When connected, an svc_xprt has at least two references:
1073 *
1074 * - A reference held by the cm_id between the ESTABLISHED and
1075 * DISCONNECTED events. If the remote peer disconnected first, this
1076 * reference could be gone.
1077 *
1078 * - A reference held by the svc_recv code that called this function
1079 * as part of close processing.
1080 *
1081 * At a minimum one references should still be held.
1082 */
1083 static void svc_rdma_detach(struct svc_xprt *xprt)
1084 {
1085 struct svcxprt_rdma *rdma =
1086 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1087 dprintk("svc: svc_rdma_detach(%p)\n", xprt);
1088
1089 /* Disconnect and flush posted WQE */
1090 rdma_disconnect(rdma->sc_cm_id);
1091 }
1092
1093 static void __svc_rdma_free(struct work_struct *work)
1094 {
1095 struct svcxprt_rdma *rdma =
1096 container_of(work, struct svcxprt_rdma, sc_work);
1097 dprintk("svcrdma: svc_rdma_free(%p)\n", rdma);
1098
1099 /* We should only be called from kref_put */
1100 BUG_ON(atomic_read(&rdma->sc_xprt.xpt_ref.refcount) != 0);
1101
1102 /*
1103 * Destroy queued, but not processed read completions. Note
1104 * that this cleanup has to be done before destroying the
1105 * cm_id because the device ptr is needed to unmap the dma in
1106 * svc_rdma_put_context.
1107 */
1108 while (!list_empty(&rdma->sc_read_complete_q)) {
1109 struct svc_rdma_op_ctxt *ctxt;
1110 ctxt = list_entry(rdma->sc_read_complete_q.next,
1111 struct svc_rdma_op_ctxt,
1112 dto_q);
1113 list_del_init(&ctxt->dto_q);
1114 svc_rdma_put_context(ctxt, 1);
1115 }
1116
1117 /* Destroy queued, but not processed recv completions */
1118 while (!list_empty(&rdma->sc_rq_dto_q)) {
1119 struct svc_rdma_op_ctxt *ctxt;
1120 ctxt = list_entry(rdma->sc_rq_dto_q.next,
1121 struct svc_rdma_op_ctxt,
1122 dto_q);
1123 list_del_init(&ctxt->dto_q);
1124 svc_rdma_put_context(ctxt, 1);
1125 }
1126
1127 /* Warn if we leaked a resource or under-referenced */
1128 WARN_ON(atomic_read(&rdma->sc_ctxt_used) != 0);
1129 WARN_ON(atomic_read(&rdma->sc_dma_used) != 0);
1130
1131 /* De-allocate fastreg mr */
1132 rdma_dealloc_frmr_q(rdma);
1133
1134 /* Destroy the QP if present (not a listener) */
1135 if (rdma->sc_qp && !IS_ERR(rdma->sc_qp))
1136 ib_destroy_qp(rdma->sc_qp);
1137
1138 if (rdma->sc_sq_cq && !IS_ERR(rdma->sc_sq_cq))
1139 ib_destroy_cq(rdma->sc_sq_cq);
1140
1141 if (rdma->sc_rq_cq && !IS_ERR(rdma->sc_rq_cq))
1142 ib_destroy_cq(rdma->sc_rq_cq);
1143
1144 if (rdma->sc_phys_mr && !IS_ERR(rdma->sc_phys_mr))
1145 ib_dereg_mr(rdma->sc_phys_mr);
1146
1147 if (rdma->sc_pd && !IS_ERR(rdma->sc_pd))
1148 ib_dealloc_pd(rdma->sc_pd);
1149
1150 /* Destroy the CM ID */
1151 rdma_destroy_id(rdma->sc_cm_id);
1152
1153 kfree(rdma);
1154 }
1155
1156 static void svc_rdma_free(struct svc_xprt *xprt)
1157 {
1158 struct svcxprt_rdma *rdma =
1159 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1160 INIT_WORK(&rdma->sc_work, __svc_rdma_free);
1161 schedule_work(&rdma->sc_work);
1162 }
1163
1164 static int svc_rdma_has_wspace(struct svc_xprt *xprt)
1165 {
1166 struct svcxprt_rdma *rdma =
1167 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1168
1169 /*
1170 * If there are fewer SQ WR available than required to send a
1171 * simple response, return false.
1172 */
1173 if ((rdma->sc_sq_depth - atomic_read(&rdma->sc_sq_count) < 3))
1174 return 0;
1175
1176 /*
1177 * ...or there are already waiters on the SQ,
1178 * return false.
1179 */
1180 if (waitqueue_active(&rdma->sc_send_wait))
1181 return 0;
1182
1183 /* Otherwise return true. */
1184 return 1;
1185 }
1186
1187 int svc_rdma_send(struct svcxprt_rdma *xprt, struct ib_send_wr *wr)
1188 {
1189 struct ib_send_wr *bad_wr;
1190 int ret;
1191
1192 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1193 return -ENOTCONN;
1194
1195 BUG_ON(wr->send_flags != IB_SEND_SIGNALED);
1196 BUG_ON(((struct svc_rdma_op_ctxt *)(unsigned long)wr->wr_id)->wr_op !=
1197 wr->opcode);
1198 /* If the SQ is full, wait until an SQ entry is available */
1199 while (1) {
1200 spin_lock_bh(&xprt->sc_lock);
1201 if (xprt->sc_sq_depth == atomic_read(&xprt->sc_sq_count)) {
1202 spin_unlock_bh(&xprt->sc_lock);
1203 atomic_inc(&rdma_stat_sq_starve);
1204
1205 /* See if we can opportunistically reap SQ WR to make room */
1206 sq_cq_reap(xprt);
1207
1208 /* Wait until SQ WR available if SQ still full */
1209 wait_event(xprt->sc_send_wait,
1210 atomic_read(&xprt->sc_sq_count) <
1211 xprt->sc_sq_depth);
1212 if (test_bit(XPT_CLOSE, &xprt->sc_xprt.xpt_flags))
1213 return 0;
1214 continue;
1215 }
1216 /* Bumped used SQ WR count and post */
1217 svc_xprt_get(&xprt->sc_xprt);
1218 ret = ib_post_send(xprt->sc_qp, wr, &bad_wr);
1219 if (!ret)
1220 atomic_inc(&xprt->sc_sq_count);
1221 else {
1222 svc_xprt_put(&xprt->sc_xprt);
1223 dprintk("svcrdma: failed to post SQ WR rc=%d, "
1224 "sc_sq_count=%d, sc_sq_depth=%d\n",
1225 ret, atomic_read(&xprt->sc_sq_count),
1226 xprt->sc_sq_depth);
1227 }
1228 spin_unlock_bh(&xprt->sc_lock);
1229 break;
1230 }
1231 return ret;
1232 }
1233
1234 void svc_rdma_send_error(struct svcxprt_rdma *xprt, struct rpcrdma_msg *rmsgp,
1235 enum rpcrdma_errcode err)
1236 {
1237 struct ib_send_wr err_wr;
1238 struct ib_sge sge;
1239 struct page *p;
1240 struct svc_rdma_op_ctxt *ctxt;
1241 u32 *va;
1242 int length;
1243 int ret;
1244
1245 p = svc_rdma_get_page();
1246 va = page_address(p);
1247
1248 /* XDR encode error */
1249 length = svc_rdma_xdr_encode_error(xprt, rmsgp, err, va);
1250
1251 /* Prepare SGE for local address */
1252 atomic_inc(&xprt->sc_dma_used);
1253 sge.addr = ib_dma_map_page(xprt->sc_cm_id->device,
1254 p, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1255 sge.lkey = xprt->sc_phys_mr->lkey;
1256 sge.length = length;
1257
1258 ctxt = svc_rdma_get_context(xprt);
1259 ctxt->count = 1;
1260 ctxt->pages[0] = p;
1261
1262 /* Prepare SEND WR */
1263 memset(&err_wr, 0, sizeof err_wr);
1264 ctxt->wr_op = IB_WR_SEND;
1265 err_wr.wr_id = (unsigned long)ctxt;
1266 err_wr.sg_list = &sge;
1267 err_wr.num_sge = 1;
1268 err_wr.opcode = IB_WR_SEND;
1269 err_wr.send_flags = IB_SEND_SIGNALED;
1270
1271 /* Post It */
1272 ret = svc_rdma_send(xprt, &err_wr);
1273 if (ret) {
1274 dprintk("svcrdma: Error %d posting send for protocol error\n",
1275 ret);
1276 svc_rdma_put_context(ctxt, 1);
1277 }
1278 }
This page took 0.108832 seconds and 6 git commands to generate.