This source file includes following definitions.
- svc_rdma_next_send_ctxt
- svc_rdma_send_ctxt_alloc
- svc_rdma_send_ctxts_destroy
- svc_rdma_send_ctxt_get
- svc_rdma_send_ctxt_put
- svc_rdma_wc_send
- svc_rdma_send
- xdr_padsize
- svc_rdma_reply_hdr_len
- xdr_encode_write_chunk
- svc_rdma_xdr_encode_write_list
- svc_rdma_xdr_encode_reply_chunk
- svc_rdma_get_write_arrays
- svc_rdma_dma_map_page
- svc_rdma_dma_map_buf
- svc_rdma_sync_reply_hdr
- svc_rdma_pull_up_needed
- svc_rdma_pull_up_reply_msg
- svc_rdma_map_reply_msg
- svc_rdma_save_io_pages
- svc_rdma_send_reply_msg
- svc_rdma_send_error_msg
- svc_rdma_sendto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 #include <linux/spinlock.h>
103 #include <asm/unaligned.h>
104
105 #include <rdma/ib_verbs.h>
106 #include <rdma/rdma_cm.h>
107
108 #include <linux/sunrpc/debug.h>
109 #include <linux/sunrpc/rpc_rdma.h>
110 #include <linux/sunrpc/svc_rdma.h>
111
112 #include "xprt_rdma.h"
113 #include <trace/events/rpcrdma.h>
114
115 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
116
117 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
118
119 static inline struct svc_rdma_send_ctxt *
120 svc_rdma_next_send_ctxt(struct list_head *list)
121 {
122 return list_first_entry_or_null(list, struct svc_rdma_send_ctxt,
123 sc_list);
124 }
125
126 static struct svc_rdma_send_ctxt *
127 svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
128 {
129 struct svc_rdma_send_ctxt *ctxt;
130 dma_addr_t addr;
131 void *buffer;
132 size_t size;
133 int i;
134
135 size = sizeof(*ctxt);
136 size += rdma->sc_max_send_sges * sizeof(struct ib_sge);
137 ctxt = kmalloc(size, GFP_KERNEL);
138 if (!ctxt)
139 goto fail0;
140 buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
141 if (!buffer)
142 goto fail1;
143 addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
144 rdma->sc_max_req_size, DMA_TO_DEVICE);
145 if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
146 goto fail2;
147
148 ctxt->sc_send_wr.next = NULL;
149 ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
150 ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
151 ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
152 ctxt->sc_cqe.done = svc_rdma_wc_send;
153 ctxt->sc_xprt_buf = buffer;
154 ctxt->sc_sges[0].addr = addr;
155
156 for (i = 0; i < rdma->sc_max_send_sges; i++)
157 ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
158 return ctxt;
159
160 fail2:
161 kfree(buffer);
162 fail1:
163 kfree(ctxt);
164 fail0:
165 return NULL;
166 }
167
168
169
170
171
172
173 void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
174 {
175 struct svc_rdma_send_ctxt *ctxt;
176
177 while ((ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts))) {
178 list_del(&ctxt->sc_list);
179 ib_dma_unmap_single(rdma->sc_pd->device,
180 ctxt->sc_sges[0].addr,
181 rdma->sc_max_req_size,
182 DMA_TO_DEVICE);
183 kfree(ctxt->sc_xprt_buf);
184 kfree(ctxt);
185 }
186 }
187
188
189
190
191
192
193
194
195 struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
196 {
197 struct svc_rdma_send_ctxt *ctxt;
198
199 spin_lock(&rdma->sc_send_lock);
200 ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts);
201 if (!ctxt)
202 goto out_empty;
203 list_del(&ctxt->sc_list);
204 spin_unlock(&rdma->sc_send_lock);
205
206 out:
207 ctxt->sc_send_wr.num_sge = 0;
208 ctxt->sc_cur_sge_no = 0;
209 ctxt->sc_page_count = 0;
210 return ctxt;
211
212 out_empty:
213 spin_unlock(&rdma->sc_send_lock);
214 ctxt = svc_rdma_send_ctxt_alloc(rdma);
215 if (!ctxt)
216 return NULL;
217 goto out;
218 }
219
220
221
222
223
224
225
226
227 void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
228 struct svc_rdma_send_ctxt *ctxt)
229 {
230 struct ib_device *device = rdma->sc_cm_id->device;
231 unsigned int i;
232
233
234
235
236 for (i = 1; i < ctxt->sc_send_wr.num_sge; i++)
237 ib_dma_unmap_page(device,
238 ctxt->sc_sges[i].addr,
239 ctxt->sc_sges[i].length,
240 DMA_TO_DEVICE);
241
242 for (i = 0; i < ctxt->sc_page_count; ++i)
243 put_page(ctxt->sc_pages[i]);
244
245 spin_lock(&rdma->sc_send_lock);
246 list_add(&ctxt->sc_list, &rdma->sc_send_ctxts);
247 spin_unlock(&rdma->sc_send_lock);
248 }
249
250
251
252
253
254
255
256
257
258 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
259 {
260 struct svcxprt_rdma *rdma = cq->cq_context;
261 struct ib_cqe *cqe = wc->wr_cqe;
262 struct svc_rdma_send_ctxt *ctxt;
263
264 trace_svcrdma_wc_send(wc);
265
266 atomic_inc(&rdma->sc_sq_avail);
267 wake_up(&rdma->sc_send_wait);
268
269 ctxt = container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
270 svc_rdma_send_ctxt_put(rdma, ctxt);
271
272 if (unlikely(wc->status != IB_WC_SUCCESS)) {
273 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
274 svc_xprt_enqueue(&rdma->sc_xprt);
275 }
276
277 svc_xprt_put(&rdma->sc_xprt);
278 }
279
280
281
282
283
284
285
286
287
288 int svc_rdma_send(struct svcxprt_rdma *rdma, struct ib_send_wr *wr)
289 {
290 int ret;
291
292 might_sleep();
293
294
295 while (1) {
296 if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) {
297 atomic_inc(&rdma_stat_sq_starve);
298 trace_svcrdma_sq_full(rdma);
299 atomic_inc(&rdma->sc_sq_avail);
300 wait_event(rdma->sc_send_wait,
301 atomic_read(&rdma->sc_sq_avail) > 1);
302 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
303 return -ENOTCONN;
304 trace_svcrdma_sq_retry(rdma);
305 continue;
306 }
307
308 svc_xprt_get(&rdma->sc_xprt);
309 trace_svcrdma_post_send(wr);
310 ret = ib_post_send(rdma->sc_qp, wr, NULL);
311 if (ret)
312 break;
313 return 0;
314 }
315
316 trace_svcrdma_sq_post_err(rdma, ret);
317 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
318 svc_xprt_put(&rdma->sc_xprt);
319 wake_up(&rdma->sc_send_wait);
320 return ret;
321 }
322
323 static u32 xdr_padsize(u32 len)
324 {
325 return (len & 3) ? (4 - (len & 3)) : 0;
326 }
327
328
329
330 static unsigned int svc_rdma_reply_hdr_len(__be32 *rdma_resp)
331 {
332 unsigned int nsegs;
333 __be32 *p;
334
335 p = rdma_resp;
336
337
338 p += rpcrdma_fixed_maxsz + 1;
339
340
341 while (*p++ != xdr_zero) {
342 nsegs = be32_to_cpup(p++);
343 p += nsegs * rpcrdma_segment_maxsz;
344 }
345
346
347 if (*p++ != xdr_zero) {
348 nsegs = be32_to_cpup(p++);
349 p += nsegs * rpcrdma_segment_maxsz;
350 }
351
352 return (unsigned long)p - (unsigned long)rdma_resp;
353 }
354
355
356
357
358
359
360
361 static unsigned int xdr_encode_write_chunk(__be32 *dst, __be32 *src,
362 unsigned int remaining)
363 {
364 unsigned int i, nsegs;
365 u32 seg_len;
366
367
368 *dst++ = *src++;
369
370
371 nsegs = be32_to_cpup(src);
372 *dst++ = *src++;
373
374 for (i = nsegs; i; i--) {
375
376 *dst++ = *src++;
377
378
379 seg_len = be32_to_cpu(*src);
380 if (remaining >= seg_len) {
381
382 *dst = *src;
383 remaining -= seg_len;
384 } else {
385
386 *dst = cpu_to_be32(remaining);
387 remaining = 0;
388 }
389 dst++; src++;
390
391
392 *dst++ = *src++;
393 *dst++ = *src++;
394 }
395
396 return nsegs;
397 }
398
399
400
401
402
403
404
405
406
407 static void svc_rdma_xdr_encode_write_list(__be32 *rdma_resp, __be32 *wr_ch,
408 unsigned int consumed)
409 {
410 unsigned int nsegs;
411 __be32 *p, *q;
412
413
414 p = rdma_resp + rpcrdma_fixed_maxsz + 1;
415
416 q = wr_ch;
417 while (*q != xdr_zero) {
418 nsegs = xdr_encode_write_chunk(p, q, consumed);
419 q += 2 + nsegs * rpcrdma_segment_maxsz;
420 p += 2 + nsegs * rpcrdma_segment_maxsz;
421 consumed = 0;
422 }
423
424
425 *p++ = xdr_zero;
426
427
428 *p = xdr_zero;
429 }
430
431
432
433
434
435
436
437
438 static void svc_rdma_xdr_encode_reply_chunk(__be32 *rdma_resp, __be32 *rp_ch,
439 unsigned int consumed)
440 {
441 __be32 *p;
442
443
444
445
446 p = rdma_resp + rpcrdma_fixed_maxsz + 1;
447
448
449 while (*p++ != xdr_zero)
450 p += 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
451
452 xdr_encode_write_chunk(p, rp_ch, consumed);
453 }
454
455
456
457 static void svc_rdma_get_write_arrays(__be32 *rdma_argp,
458 __be32 **write, __be32 **reply)
459 {
460 __be32 *p;
461
462 p = rdma_argp + rpcrdma_fixed_maxsz;
463
464
465 while (*p++ != xdr_zero)
466 p += 5;
467
468
469 if (*p != xdr_zero) {
470 *write = p;
471 while (*p++ != xdr_zero)
472 p += 1 + be32_to_cpu(*p) * 4;
473 } else {
474 *write = NULL;
475 p++;
476 }
477
478
479 if (*p != xdr_zero)
480 *reply = p;
481 else
482 *reply = NULL;
483 }
484
485 static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma,
486 struct svc_rdma_send_ctxt *ctxt,
487 struct page *page,
488 unsigned long offset,
489 unsigned int len)
490 {
491 struct ib_device *dev = rdma->sc_cm_id->device;
492 dma_addr_t dma_addr;
493
494 dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
495 if (ib_dma_mapping_error(dev, dma_addr))
496 goto out_maperr;
497
498 ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
499 ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
500 ctxt->sc_send_wr.num_sge++;
501 return 0;
502
503 out_maperr:
504 trace_svcrdma_dma_map_page(rdma, page);
505 return -EIO;
506 }
507
508
509
510
511 static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma,
512 struct svc_rdma_send_ctxt *ctxt,
513 unsigned char *base,
514 unsigned int len)
515 {
516 return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base),
517 offset_in_page(base), len);
518 }
519
520
521
522
523
524
525
526
527 void svc_rdma_sync_reply_hdr(struct svcxprt_rdma *rdma,
528 struct svc_rdma_send_ctxt *ctxt,
529 unsigned int len)
530 {
531 ctxt->sc_sges[0].length = len;
532 ctxt->sc_send_wr.num_sge++;
533 ib_dma_sync_single_for_device(rdma->sc_pd->device,
534 ctxt->sc_sges[0].addr, len,
535 DMA_TO_DEVICE);
536 }
537
538
539
540
541
542 static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma,
543 struct xdr_buf *xdr,
544 __be32 *wr_lst)
545 {
546 int elements;
547
548
549 elements = 1;
550
551
552 if (!wr_lst) {
553 unsigned int remaining;
554 unsigned long pageoff;
555
556 pageoff = xdr->page_base & ~PAGE_MASK;
557 remaining = xdr->page_len;
558 while (remaining) {
559 ++elements;
560 remaining -= min_t(u32, PAGE_SIZE - pageoff,
561 remaining);
562 pageoff = 0;
563 }
564 }
565
566
567 if (xdr->tail[0].iov_len)
568 ++elements;
569
570
571 return elements >= rdma->sc_max_send_sges;
572 }
573
574
575
576
577
578 static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma,
579 struct svc_rdma_send_ctxt *ctxt,
580 struct xdr_buf *xdr, __be32 *wr_lst)
581 {
582 unsigned char *dst, *tailbase;
583 unsigned int taillen;
584
585 dst = ctxt->sc_xprt_buf;
586 dst += ctxt->sc_sges[0].length;
587
588 memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len);
589 dst += xdr->head[0].iov_len;
590
591 tailbase = xdr->tail[0].iov_base;
592 taillen = xdr->tail[0].iov_len;
593 if (wr_lst) {
594 u32 xdrpad;
595
596 xdrpad = xdr_padsize(xdr->page_len);
597 if (taillen && xdrpad) {
598 tailbase += xdrpad;
599 taillen -= xdrpad;
600 }
601 } else {
602 unsigned int len, remaining;
603 unsigned long pageoff;
604 struct page **ppages;
605
606 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
607 pageoff = xdr->page_base & ~PAGE_MASK;
608 remaining = xdr->page_len;
609 while (remaining) {
610 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
611
612 memcpy(dst, page_address(*ppages), len);
613 remaining -= len;
614 dst += len;
615 pageoff = 0;
616 }
617 }
618
619 if (taillen)
620 memcpy(dst, tailbase, taillen);
621
622 ctxt->sc_sges[0].length += xdr->len;
623 ib_dma_sync_single_for_device(rdma->sc_pd->device,
624 ctxt->sc_sges[0].addr,
625 ctxt->sc_sges[0].length,
626 DMA_TO_DEVICE);
627
628 return 0;
629 }
630
631
632
633
634
635
636
637
638
639
640
641
642 int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
643 struct svc_rdma_send_ctxt *ctxt,
644 struct xdr_buf *xdr, __be32 *wr_lst)
645 {
646 unsigned int len, remaining;
647 unsigned long page_off;
648 struct page **ppages;
649 unsigned char *base;
650 u32 xdr_pad;
651 int ret;
652
653 if (svc_rdma_pull_up_needed(rdma, xdr, wr_lst))
654 return svc_rdma_pull_up_reply_msg(rdma, ctxt, xdr, wr_lst);
655
656 ++ctxt->sc_cur_sge_no;
657 ret = svc_rdma_dma_map_buf(rdma, ctxt,
658 xdr->head[0].iov_base,
659 xdr->head[0].iov_len);
660 if (ret < 0)
661 return ret;
662
663
664
665
666
667
668 if (wr_lst) {
669 base = xdr->tail[0].iov_base;
670 len = xdr->tail[0].iov_len;
671 xdr_pad = xdr_padsize(xdr->page_len);
672
673 if (len && xdr_pad) {
674 base += xdr_pad;
675 len -= xdr_pad;
676 }
677
678 goto tail;
679 }
680
681 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
682 page_off = xdr->page_base & ~PAGE_MASK;
683 remaining = xdr->page_len;
684 while (remaining) {
685 len = min_t(u32, PAGE_SIZE - page_off, remaining);
686
687 ++ctxt->sc_cur_sge_no;
688 ret = svc_rdma_dma_map_page(rdma, ctxt, *ppages++,
689 page_off, len);
690 if (ret < 0)
691 return ret;
692
693 remaining -= len;
694 page_off = 0;
695 }
696
697 base = xdr->tail[0].iov_base;
698 len = xdr->tail[0].iov_len;
699 tail:
700 if (len) {
701 ++ctxt->sc_cur_sge_no;
702 ret = svc_rdma_dma_map_buf(rdma, ctxt, base, len);
703 if (ret < 0)
704 return ret;
705 }
706
707 return 0;
708 }
709
710
711
712
713
714 static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
715 struct svc_rdma_send_ctxt *ctxt)
716 {
717 int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
718
719 ctxt->sc_page_count += pages;
720 for (i = 0; i < pages; i++) {
721 ctxt->sc_pages[i] = rqstp->rq_respages[i];
722 rqstp->rq_respages[i] = NULL;
723 }
724
725
726 rqstp->rq_next_page = rqstp->rq_respages;
727 }
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
748 struct svc_rdma_send_ctxt *sctxt,
749 struct svc_rdma_recv_ctxt *rctxt,
750 struct svc_rqst *rqstp,
751 __be32 *wr_lst, __be32 *rp_ch)
752 {
753 int ret;
754
755 if (!rp_ch) {
756 ret = svc_rdma_map_reply_msg(rdma, sctxt,
757 &rqstp->rq_res, wr_lst);
758 if (ret < 0)
759 return ret;
760 }
761
762 svc_rdma_save_io_pages(rqstp, sctxt);
763
764 if (rctxt->rc_inv_rkey) {
765 sctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV;
766 sctxt->sc_send_wr.ex.invalidate_rkey = rctxt->rc_inv_rkey;
767 } else {
768 sctxt->sc_send_wr.opcode = IB_WR_SEND;
769 }
770 dprintk("svcrdma: posting Send WR with %u sge(s)\n",
771 sctxt->sc_send_wr.num_sge);
772 return svc_rdma_send(rdma, &sctxt->sc_send_wr);
773 }
774
775
776
777
778
779
780
781
782 static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
783 struct svc_rdma_send_ctxt *ctxt,
784 struct svc_rqst *rqstp)
785 {
786 __be32 *p;
787 int ret;
788
789 p = ctxt->sc_xprt_buf;
790 trace_svcrdma_err_chunk(*p);
791 p += 3;
792 *p++ = rdma_error;
793 *p = err_chunk;
794 svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_ERR);
795
796 svc_rdma_save_io_pages(rqstp, ctxt);
797
798 ctxt->sc_send_wr.opcode = IB_WR_SEND;
799 ret = svc_rdma_send(rdma, &ctxt->sc_send_wr);
800 if (ret) {
801 svc_rdma_send_ctxt_put(rdma, ctxt);
802 return ret;
803 }
804
805 return 0;
806 }
807
808
809
810
811
812
813
814
815
816
817
818
819
820 int svc_rdma_sendto(struct svc_rqst *rqstp)
821 {
822 struct svc_xprt *xprt = rqstp->rq_xprt;
823 struct svcxprt_rdma *rdma =
824 container_of(xprt, struct svcxprt_rdma, sc_xprt);
825 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
826 __be32 *p, *rdma_argp, *rdma_resp, *wr_lst, *rp_ch;
827 struct xdr_buf *xdr = &rqstp->rq_res;
828 struct svc_rdma_send_ctxt *sctxt;
829 int ret;
830
831 rdma_argp = rctxt->rc_recv_buf;
832 svc_rdma_get_write_arrays(rdma_argp, &wr_lst, &rp_ch);
833
834
835
836
837
838
839
840 ret = -ENOMEM;
841 sctxt = svc_rdma_send_ctxt_get(rdma);
842 if (!sctxt)
843 goto err0;
844 rdma_resp = sctxt->sc_xprt_buf;
845
846 p = rdma_resp;
847 *p++ = *rdma_argp;
848 *p++ = *(rdma_argp + 1);
849 *p++ = rdma->sc_fc_credits;
850 *p++ = rp_ch ? rdma_nomsg : rdma_msg;
851
852
853 *p++ = xdr_zero;
854 *p++ = xdr_zero;
855 *p = xdr_zero;
856
857 if (wr_lst) {
858
859 ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr);
860 if (ret < 0)
861 goto err2;
862 svc_rdma_xdr_encode_write_list(rdma_resp, wr_lst, ret);
863 }
864 if (rp_ch) {
865 ret = svc_rdma_send_reply_chunk(rdma, rp_ch, wr_lst, xdr);
866 if (ret < 0)
867 goto err2;
868 svc_rdma_xdr_encode_reply_chunk(rdma_resp, rp_ch, ret);
869 }
870
871 svc_rdma_sync_reply_hdr(rdma, sctxt, svc_rdma_reply_hdr_len(rdma_resp));
872 ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp,
873 wr_lst, rp_ch);
874 if (ret < 0)
875 goto err1;
876 return 0;
877
878 err2:
879 if (ret != -E2BIG && ret != -EINVAL)
880 goto err1;
881
882 ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp);
883 if (ret < 0)
884 goto err1;
885 return 0;
886
887 err1:
888 svc_rdma_send_ctxt_put(rdma, sctxt);
889 err0:
890 trace_svcrdma_send_failed(rqstp, ret);
891 set_bit(XPT_CLOSE, &xprt->xpt_flags);
892 return -ENOTCONN;
893 }