1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (c) 2016-2018 Oracle. All rights reserved. 4 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved. 5 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved. 6 * 7 * This software is available to you under a choice of one of two 8 * licenses. You may choose to be licensed under the terms of the GNU 9 * General Public License (GPL) Version 2, available from the file 10 * COPYING in the main directory of this source tree, or the BSD-type 11 * license below: 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials provided 23 * with the distribution. 24 * 25 * Neither the name of the Network Appliance, Inc. nor the names of 26 * its contributors may be used to endorse or promote products 27 * derived from this software without specific prior written 28 * permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 * 42 * Author: Tom Tucker <tom@opengridcomputing.com> 43 */ 44 45 /* Operation 46 * 47 * The main entry point is svc_rdma_sendto. This is called by the 48 * RPC server when an RPC Reply is ready to be transmitted to a client. 49 * 50 * The passed-in svc_rqst contains a struct xdr_buf which holds an 51 * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA 52 * transport header, post all Write WRs needed for this Reply, then post 53 * a Send WR conveying the transport header and the RPC message itself to 54 * the client. 55 * 56 * svc_rdma_sendto must fully transmit the Reply before returning, as 57 * the svc_rqst will be recycled as soon as sendto returns. Remaining 58 * resources referred to by the svc_rqst are also recycled at that time. 59 * Therefore any resources that must remain longer must be detached 60 * from the svc_rqst and released later. 61 * 62 * Page Management 63 * 64 * The I/O that performs Reply transmission is asynchronous, and may 65 * complete well after sendto returns. Thus pages under I/O must be 66 * removed from the svc_rqst before sendto returns. 67 * 68 * The logic here depends on Send Queue and completion ordering. Since 69 * the Send WR is always posted last, it will always complete last. Thus 70 * when it completes, it is guaranteed that all previous Write WRs have 71 * also completed. 72 * 73 * Write WRs are constructed and posted. Each Write segment gets its own 74 * svc_rdma_rw_ctxt, allowing the Write completion handler to find and 75 * DMA-unmap the pages under I/O for that Write segment. The Write 76 * completion handler does not release any pages. 77 * 78 * When the Send WR is constructed, it also gets its own svc_rdma_send_ctxt. 79 * The ownership of all of the Reply's pages are transferred into that 80 * ctxt, the Send WR is posted, and sendto returns. 81 * 82 * The svc_rdma_send_ctxt is presented when the Send WR completes. The 83 * Send completion handler finally releases the Reply's pages. 84 * 85 * This mechanism also assumes that completions on the transport's Send 86 * Completion Queue do not run in parallel. Otherwise a Write completion 87 * and Send completion running at the same time could release pages that 88 * are still DMA-mapped. 89 * 90 * Error Handling 91 * 92 * - If the Send WR is posted successfully, it will either complete 93 * successfully, or get flushed. Either way, the Send completion 94 * handler releases the Reply's pages. 95 * - If the Send WR cannot be not posted, the forward path releases 96 * the Reply's pages. 97 * 98 * This handles the case, without the use of page reference counting, 99 * where two different Write segments send portions of the same page. 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 xdr_buf_init(&ctxt->sc_hdrbuf, ctxt->sc_xprt_buf, 155 rdma->sc_max_req_size); 156 ctxt->sc_sges[0].addr = addr; 157 158 for (i = 0; i < rdma->sc_max_send_sges; i++) 159 ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey; 160 return ctxt; 161 162 fail2: 163 kfree(buffer); 164 fail1: 165 kfree(ctxt); 166 fail0: 167 return NULL; 168 } 169 170 /** 171 * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt 172 * @rdma: svcxprt_rdma being torn down 173 * 174 */ 175 void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma) 176 { 177 struct svc_rdma_send_ctxt *ctxt; 178 179 while ((ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts))) { 180 list_del(&ctxt->sc_list); 181 ib_dma_unmap_single(rdma->sc_pd->device, 182 ctxt->sc_sges[0].addr, 183 rdma->sc_max_req_size, 184 DMA_TO_DEVICE); 185 kfree(ctxt->sc_xprt_buf); 186 kfree(ctxt); 187 } 188 } 189 190 /** 191 * svc_rdma_send_ctxt_get - Get a free send_ctxt 192 * @rdma: controlling svcxprt_rdma 193 * 194 * Returns a ready-to-use send_ctxt, or NULL if none are 195 * available and a fresh one cannot be allocated. 196 */ 197 struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma) 198 { 199 struct svc_rdma_send_ctxt *ctxt; 200 201 spin_lock(&rdma->sc_send_lock); 202 ctxt = svc_rdma_next_send_ctxt(&rdma->sc_send_ctxts); 203 if (!ctxt) 204 goto out_empty; 205 list_del(&ctxt->sc_list); 206 spin_unlock(&rdma->sc_send_lock); 207 208 out: 209 rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0); 210 xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf, 211 ctxt->sc_xprt_buf, NULL); 212 213 ctxt->sc_send_wr.num_sge = 0; 214 ctxt->sc_cur_sge_no = 0; 215 ctxt->sc_page_count = 0; 216 return ctxt; 217 218 out_empty: 219 spin_unlock(&rdma->sc_send_lock); 220 ctxt = svc_rdma_send_ctxt_alloc(rdma); 221 if (!ctxt) 222 return NULL; 223 goto out; 224 } 225 226 /** 227 * svc_rdma_send_ctxt_put - Return send_ctxt to free list 228 * @rdma: controlling svcxprt_rdma 229 * @ctxt: object to return to the free list 230 * 231 * Pages left in sc_pages are DMA unmapped and released. 232 */ 233 void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma, 234 struct svc_rdma_send_ctxt *ctxt) 235 { 236 struct ib_device *device = rdma->sc_cm_id->device; 237 unsigned int i; 238 239 /* The first SGE contains the transport header, which 240 * remains mapped until @ctxt is destroyed. 241 */ 242 for (i = 1; i < ctxt->sc_send_wr.num_sge; i++) { 243 ib_dma_unmap_page(device, 244 ctxt->sc_sges[i].addr, 245 ctxt->sc_sges[i].length, 246 DMA_TO_DEVICE); 247 trace_svcrdma_dma_unmap_page(rdma, 248 ctxt->sc_sges[i].addr, 249 ctxt->sc_sges[i].length); 250 } 251 252 for (i = 0; i < ctxt->sc_page_count; ++i) 253 put_page(ctxt->sc_pages[i]); 254 255 spin_lock(&rdma->sc_send_lock); 256 list_add(&ctxt->sc_list, &rdma->sc_send_ctxts); 257 spin_unlock(&rdma->sc_send_lock); 258 } 259 260 /** 261 * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC 262 * @cq: Completion Queue context 263 * @wc: Work Completion object 264 * 265 * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that 266 * the Send completion handler could be running. 267 */ 268 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc) 269 { 270 struct svcxprt_rdma *rdma = cq->cq_context; 271 struct ib_cqe *cqe = wc->wr_cqe; 272 struct svc_rdma_send_ctxt *ctxt; 273 274 trace_svcrdma_wc_send(wc); 275 276 atomic_inc(&rdma->sc_sq_avail); 277 wake_up(&rdma->sc_send_wait); 278 279 ctxt = container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe); 280 svc_rdma_send_ctxt_put(rdma, ctxt); 281 282 if (unlikely(wc->status != IB_WC_SUCCESS)) { 283 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags); 284 svc_xprt_enqueue(&rdma->sc_xprt); 285 } 286 287 svc_xprt_put(&rdma->sc_xprt); 288 } 289 290 /** 291 * svc_rdma_send - Post a single Send WR 292 * @rdma: transport on which to post the WR 293 * @wr: prepared Send WR to post 294 * 295 * Returns zero the Send WR was posted successfully. Otherwise, a 296 * negative errno is returned. 297 */ 298 int svc_rdma_send(struct svcxprt_rdma *rdma, struct ib_send_wr *wr) 299 { 300 int ret; 301 302 might_sleep(); 303 304 /* Sync the transport header buffer */ 305 ib_dma_sync_single_for_device(rdma->sc_pd->device, 306 wr->sg_list[0].addr, 307 wr->sg_list[0].length, 308 DMA_TO_DEVICE); 309 310 /* If the SQ is full, wait until an SQ entry is available */ 311 while (1) { 312 if ((atomic_dec_return(&rdma->sc_sq_avail) < 0)) { 313 atomic_inc(&rdma_stat_sq_starve); 314 trace_svcrdma_sq_full(rdma); 315 atomic_inc(&rdma->sc_sq_avail); 316 wait_event(rdma->sc_send_wait, 317 atomic_read(&rdma->sc_sq_avail) > 1); 318 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags)) 319 return -ENOTCONN; 320 trace_svcrdma_sq_retry(rdma); 321 continue; 322 } 323 324 svc_xprt_get(&rdma->sc_xprt); 325 ret = ib_post_send(rdma->sc_qp, wr, NULL); 326 trace_svcrdma_post_send(wr, ret); 327 if (ret) { 328 set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags); 329 svc_xprt_put(&rdma->sc_xprt); 330 wake_up(&rdma->sc_send_wait); 331 } 332 break; 333 } 334 return ret; 335 } 336 337 /** 338 * svc_rdma_encode_read_list - Encode RPC Reply's Read chunk list 339 * @sctxt: Send context for the RPC Reply 340 * 341 * Return values: 342 * On success, returns length in bytes of the Reply XDR buffer 343 * that was consumed by the Reply Read list 344 * %-EMSGSIZE on XDR buffer overflow 345 */ 346 static ssize_t svc_rdma_encode_read_list(struct svc_rdma_send_ctxt *sctxt) 347 { 348 /* RPC-over-RDMA version 1 replies never have a Read list. */ 349 return xdr_stream_encode_item_absent(&sctxt->sc_stream); 350 } 351 352 /** 353 * svc_rdma_encode_write_segment - Encode one Write segment 354 * @src: matching Write chunk in the RPC Call header 355 * @sctxt: Send context for the RPC Reply 356 * @remaining: remaining bytes of the payload left in the Write chunk 357 * 358 * Return values: 359 * On success, returns length in bytes of the Reply XDR buffer 360 * that was consumed by the Write segment 361 * %-EMSGSIZE on XDR buffer overflow 362 */ 363 static ssize_t svc_rdma_encode_write_segment(__be32 *src, 364 struct svc_rdma_send_ctxt *sctxt, 365 unsigned int *remaining) 366 { 367 __be32 *p; 368 const size_t len = rpcrdma_segment_maxsz * sizeof(*p); 369 u32 handle, length; 370 u64 offset; 371 372 p = xdr_reserve_space(&sctxt->sc_stream, len); 373 if (!p) 374 return -EMSGSIZE; 375 376 handle = be32_to_cpup(src++); 377 length = be32_to_cpup(src++); 378 xdr_decode_hyper(src, &offset); 379 380 *p++ = cpu_to_be32(handle); 381 if (*remaining < length) { 382 /* segment only partly filled */ 383 length = *remaining; 384 *remaining = 0; 385 } else { 386 /* entire segment was consumed */ 387 *remaining -= length; 388 } 389 *p++ = cpu_to_be32(length); 390 xdr_encode_hyper(p, offset); 391 392 trace_svcrdma_encode_wseg(handle, length, offset); 393 return len; 394 } 395 396 /** 397 * svc_rdma_encode_write_chunk - Encode one Write chunk 398 * @src: matching Write chunk in the RPC Call header 399 * @sctxt: Send context for the RPC Reply 400 * @remaining: size in bytes of the payload in the Write chunk 401 * 402 * Copy a Write chunk from the Call transport header to the 403 * Reply transport header. Update each segment's length field 404 * to reflect the number of bytes written in that segment. 405 * 406 * Return values: 407 * On success, returns length in bytes of the Reply XDR buffer 408 * that was consumed by the Write chunk 409 * %-EMSGSIZE on XDR buffer overflow 410 */ 411 static ssize_t svc_rdma_encode_write_chunk(__be32 *src, 412 struct svc_rdma_send_ctxt *sctxt, 413 unsigned int remaining) 414 { 415 unsigned int i, nsegs; 416 ssize_t len, ret; 417 418 len = 0; 419 trace_svcrdma_encode_write_chunk(remaining); 420 421 src++; 422 ret = xdr_stream_encode_item_present(&sctxt->sc_stream); 423 if (ret < 0) 424 return -EMSGSIZE; 425 len += ret; 426 427 nsegs = be32_to_cpup(src++); 428 ret = xdr_stream_encode_u32(&sctxt->sc_stream, nsegs); 429 if (ret < 0) 430 return -EMSGSIZE; 431 len += ret; 432 433 for (i = nsegs; i; i--) { 434 ret = svc_rdma_encode_write_segment(src, sctxt, &remaining); 435 if (ret < 0) 436 return -EMSGSIZE; 437 src += rpcrdma_segment_maxsz; 438 len += ret; 439 } 440 441 return len; 442 } 443 444 /** 445 * svc_rdma_encode_write_list - Encode RPC Reply's Write chunk list 446 * @rctxt: Reply context with information about the RPC Call 447 * @sctxt: Send context for the RPC Reply 448 * @length: size in bytes of the payload in the first Write chunk 449 * 450 * The client provides a Write chunk list in the Call message. Fill 451 * in the segments in the first Write chunk in the Reply's transport 452 * header with the number of bytes consumed in each segment. 453 * Remaining chunks are returned unused. 454 * 455 * Assumptions: 456 * - Client has provided only one Write chunk 457 * 458 * Return values: 459 * On success, returns length in bytes of the Reply XDR buffer 460 * that was consumed by the Reply's Write list 461 * %-EMSGSIZE on XDR buffer overflow 462 */ 463 static ssize_t 464 svc_rdma_encode_write_list(const struct svc_rdma_recv_ctxt *rctxt, 465 struct svc_rdma_send_ctxt *sctxt, 466 unsigned int length) 467 { 468 ssize_t len, ret; 469 470 ret = svc_rdma_encode_write_chunk(rctxt->rc_write_list, sctxt, length); 471 if (ret < 0) 472 return ret; 473 len = ret; 474 475 /* Terminate the Write list */ 476 ret = xdr_stream_encode_item_absent(&sctxt->sc_stream); 477 if (ret < 0) 478 return ret; 479 480 return len + ret; 481 } 482 483 /** 484 * svc_rdma_encode_reply_chunk - Encode RPC Reply's Reply chunk 485 * @rctxt: Reply context with information about the RPC Call 486 * @sctxt: Send context for the RPC Reply 487 * @length: size in bytes of the payload in the Reply chunk 488 * 489 * Assumptions: 490 * - Reply can always fit in the client-provided Reply chunk 491 * 492 * Return values: 493 * On success, returns length in bytes of the Reply XDR buffer 494 * that was consumed by the Reply's Reply chunk 495 * %-EMSGSIZE on XDR buffer overflow 496 */ 497 static ssize_t 498 svc_rdma_encode_reply_chunk(const struct svc_rdma_recv_ctxt *rctxt, 499 struct svc_rdma_send_ctxt *sctxt, 500 unsigned int length) 501 { 502 return svc_rdma_encode_write_chunk(rctxt->rc_reply_chunk, sctxt, 503 length); 504 } 505 506 static int svc_rdma_dma_map_page(struct svcxprt_rdma *rdma, 507 struct svc_rdma_send_ctxt *ctxt, 508 struct page *page, 509 unsigned long offset, 510 unsigned int len) 511 { 512 struct ib_device *dev = rdma->sc_cm_id->device; 513 dma_addr_t dma_addr; 514 515 dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE); 516 trace_svcrdma_dma_map_page(rdma, dma_addr, len); 517 if (ib_dma_mapping_error(dev, dma_addr)) 518 goto out_maperr; 519 520 ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr; 521 ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len; 522 ctxt->sc_send_wr.num_sge++; 523 return 0; 524 525 out_maperr: 526 return -EIO; 527 } 528 529 /* ib_dma_map_page() is used here because svc_rdma_dma_unmap() 530 * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively. 531 */ 532 static int svc_rdma_dma_map_buf(struct svcxprt_rdma *rdma, 533 struct svc_rdma_send_ctxt *ctxt, 534 unsigned char *base, 535 unsigned int len) 536 { 537 return svc_rdma_dma_map_page(rdma, ctxt, virt_to_page(base), 538 offset_in_page(base), len); 539 } 540 541 /** 542 * svc_rdma_pull_up_needed - Determine whether to use pull-up 543 * @rdma: controlling transport 544 * @sctxt: send_ctxt for the Send WR 545 * @rctxt: Write and Reply chunks provided by client 546 * @xdr: xdr_buf containing RPC message to transmit 547 * 548 * Returns: 549 * %true if pull-up must be used 550 * %false otherwise 551 */ 552 static bool svc_rdma_pull_up_needed(struct svcxprt_rdma *rdma, 553 struct svc_rdma_send_ctxt *sctxt, 554 const struct svc_rdma_recv_ctxt *rctxt, 555 struct xdr_buf *xdr) 556 { 557 int elements; 558 559 /* For small messages, copying bytes is cheaper than DMA mapping. 560 */ 561 if (sctxt->sc_hdrbuf.len + xdr->len < RPCRDMA_PULLUP_THRESH) 562 return true; 563 564 /* Check whether the xdr_buf has more elements than can 565 * fit in a single RDMA Send. 566 */ 567 /* xdr->head */ 568 elements = 1; 569 570 /* xdr->pages */ 571 if (!rctxt || !rctxt->rc_write_list) { 572 unsigned int remaining; 573 unsigned long pageoff; 574 575 pageoff = xdr->page_base & ~PAGE_MASK; 576 remaining = xdr->page_len; 577 while (remaining) { 578 ++elements; 579 remaining -= min_t(u32, PAGE_SIZE - pageoff, 580 remaining); 581 pageoff = 0; 582 } 583 } 584 585 /* xdr->tail */ 586 if (xdr->tail[0].iov_len) 587 ++elements; 588 589 /* assume 1 SGE is needed for the transport header */ 590 return elements >= rdma->sc_max_send_sges; 591 } 592 593 /** 594 * svc_rdma_pull_up_reply_msg - Copy Reply into a single buffer 595 * @rdma: controlling transport 596 * @sctxt: send_ctxt for the Send WR; xprt hdr is already prepared 597 * @rctxt: Write and Reply chunks provided by client 598 * @xdr: prepared xdr_buf containing RPC message 599 * 600 * The device is not capable of sending the reply directly. 601 * Assemble the elements of @xdr into the transport header buffer. 602 * 603 * Returns zero on success, or a negative errno on failure. 604 */ 605 static int svc_rdma_pull_up_reply_msg(struct svcxprt_rdma *rdma, 606 struct svc_rdma_send_ctxt *sctxt, 607 const struct svc_rdma_recv_ctxt *rctxt, 608 const struct xdr_buf *xdr) 609 { 610 unsigned char *dst, *tailbase; 611 unsigned int taillen; 612 613 dst = sctxt->sc_xprt_buf + sctxt->sc_hdrbuf.len; 614 memcpy(dst, xdr->head[0].iov_base, xdr->head[0].iov_len); 615 dst += xdr->head[0].iov_len; 616 617 tailbase = xdr->tail[0].iov_base; 618 taillen = xdr->tail[0].iov_len; 619 if (rctxt && rctxt->rc_write_list) { 620 u32 xdrpad; 621 622 xdrpad = xdr_pad_size(xdr->page_len); 623 if (taillen && xdrpad) { 624 tailbase += xdrpad; 625 taillen -= xdrpad; 626 } 627 } else { 628 unsigned int len, remaining; 629 unsigned long pageoff; 630 struct page **ppages; 631 632 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT); 633 pageoff = xdr->page_base & ~PAGE_MASK; 634 remaining = xdr->page_len; 635 while (remaining) { 636 len = min_t(u32, PAGE_SIZE - pageoff, remaining); 637 638 memcpy(dst, page_address(*ppages), len); 639 remaining -= len; 640 dst += len; 641 pageoff = 0; 642 } 643 } 644 645 if (taillen) 646 memcpy(dst, tailbase, taillen); 647 648 sctxt->sc_sges[0].length += xdr->len; 649 trace_svcrdma_send_pullup(sctxt->sc_sges[0].length); 650 return 0; 651 } 652 653 /* svc_rdma_map_reply_msg - DMA map the buffer holding RPC message 654 * @rdma: controlling transport 655 * @sctxt: send_ctxt for the Send WR 656 * @rctxt: Write and Reply chunks provided by client 657 * @xdr: prepared xdr_buf containing RPC message 658 * 659 * Load the xdr_buf into the ctxt's sge array, and DMA map each 660 * element as it is added. The Send WR's num_sge field is set. 661 * 662 * Returns zero on success, or a negative errno on failure. 663 */ 664 int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma, 665 struct svc_rdma_send_ctxt *sctxt, 666 const struct svc_rdma_recv_ctxt *rctxt, 667 struct xdr_buf *xdr) 668 { 669 unsigned int len, remaining; 670 unsigned long page_off; 671 struct page **ppages; 672 unsigned char *base; 673 u32 xdr_pad; 674 int ret; 675 676 /* Set up the (persistently-mapped) transport header SGE. */ 677 sctxt->sc_send_wr.num_sge = 1; 678 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len; 679 680 /* If there is a Reply chunk, nothing follows the transport 681 * header, and we're done here. 682 */ 683 if (rctxt && rctxt->rc_reply_chunk) 684 return 0; 685 686 /* For pull-up, svc_rdma_send() will sync the transport header. 687 * No additional DMA mapping is necessary. 688 */ 689 if (svc_rdma_pull_up_needed(rdma, sctxt, rctxt, xdr)) 690 return svc_rdma_pull_up_reply_msg(rdma, sctxt, rctxt, xdr); 691 692 ++sctxt->sc_cur_sge_no; 693 ret = svc_rdma_dma_map_buf(rdma, sctxt, 694 xdr->head[0].iov_base, 695 xdr->head[0].iov_len); 696 if (ret < 0) 697 return ret; 698 699 /* If a Write chunk is present, the xdr_buf's page list 700 * is not included inline. However the Upper Layer may 701 * have added XDR padding in the tail buffer, and that 702 * should not be included inline. 703 */ 704 if (rctxt && rctxt->rc_write_list) { 705 base = xdr->tail[0].iov_base; 706 len = xdr->tail[0].iov_len; 707 xdr_pad = xdr_pad_size(xdr->page_len); 708 709 if (len && xdr_pad) { 710 base += xdr_pad; 711 len -= xdr_pad; 712 } 713 714 goto tail; 715 } 716 717 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT); 718 page_off = xdr->page_base & ~PAGE_MASK; 719 remaining = xdr->page_len; 720 while (remaining) { 721 len = min_t(u32, PAGE_SIZE - page_off, remaining); 722 723 ++sctxt->sc_cur_sge_no; 724 ret = svc_rdma_dma_map_page(rdma, sctxt, *ppages++, 725 page_off, len); 726 if (ret < 0) 727 return ret; 728 729 remaining -= len; 730 page_off = 0; 731 } 732 733 base = xdr->tail[0].iov_base; 734 len = xdr->tail[0].iov_len; 735 tail: 736 if (len) { 737 ++sctxt->sc_cur_sge_no; 738 ret = svc_rdma_dma_map_buf(rdma, sctxt, base, len); 739 if (ret < 0) 740 return ret; 741 } 742 743 return 0; 744 } 745 746 /* The svc_rqst and all resources it owns are released as soon as 747 * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt 748 * so they are released by the Send completion handler. 749 */ 750 static void svc_rdma_save_io_pages(struct svc_rqst *rqstp, 751 struct svc_rdma_send_ctxt *ctxt) 752 { 753 int i, pages = rqstp->rq_next_page - rqstp->rq_respages; 754 755 ctxt->sc_page_count += pages; 756 for (i = 0; i < pages; i++) { 757 ctxt->sc_pages[i] = rqstp->rq_respages[i]; 758 rqstp->rq_respages[i] = NULL; 759 } 760 761 /* Prevent svc_xprt_release from releasing pages in rq_pages */ 762 rqstp->rq_next_page = rqstp->rq_respages; 763 } 764 765 /* Prepare the portion of the RPC Reply that will be transmitted 766 * via RDMA Send. The RPC-over-RDMA transport header is prepared 767 * in sc_sges[0], and the RPC xdr_buf is prepared in following sges. 768 * 769 * Depending on whether a Write list or Reply chunk is present, 770 * the server may send all, a portion of, or none of the xdr_buf. 771 * In the latter case, only the transport header (sc_sges[0]) is 772 * transmitted. 773 * 774 * RDMA Send is the last step of transmitting an RPC reply. Pages 775 * involved in the earlier RDMA Writes are here transferred out 776 * of the rqstp and into the sctxt's page array. These pages are 777 * DMA unmapped by each Write completion, but the subsequent Send 778 * completion finally releases these pages. 779 * 780 * Assumptions: 781 * - The Reply's transport header will never be larger than a page. 782 */ 783 static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma, 784 struct svc_rdma_send_ctxt *sctxt, 785 const struct svc_rdma_recv_ctxt *rctxt, 786 struct svc_rqst *rqstp) 787 { 788 int ret; 789 790 ret = svc_rdma_map_reply_msg(rdma, sctxt, rctxt, &rqstp->rq_res); 791 if (ret < 0) 792 return ret; 793 794 svc_rdma_save_io_pages(rqstp, sctxt); 795 796 if (rctxt->rc_inv_rkey) { 797 sctxt->sc_send_wr.opcode = IB_WR_SEND_WITH_INV; 798 sctxt->sc_send_wr.ex.invalidate_rkey = rctxt->rc_inv_rkey; 799 } else { 800 sctxt->sc_send_wr.opcode = IB_WR_SEND; 801 } 802 return svc_rdma_send(rdma, &sctxt->sc_send_wr); 803 } 804 805 /* Given the client-provided Write and Reply chunks, the server was not 806 * able to form a complete reply. Return an RDMA_ERROR message so the 807 * client can retire this RPC transaction. As above, the Send completion 808 * routine releases payload pages that were part of a previous RDMA Write. 809 * 810 * Remote Invalidation is skipped for simplicity. 811 */ 812 static int svc_rdma_send_error_msg(struct svcxprt_rdma *rdma, 813 struct svc_rdma_send_ctxt *ctxt, 814 struct svc_rqst *rqstp) 815 { 816 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt; 817 __be32 *rdma_argp = rctxt->rc_recv_buf; 818 __be32 *p; 819 820 rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0); 821 xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf, ctxt->sc_xprt_buf, 822 NULL); 823 824 p = xdr_reserve_space(&ctxt->sc_stream, RPCRDMA_HDRLEN_ERR); 825 if (!p) 826 return -ENOMSG; 827 828 *p++ = *rdma_argp; 829 *p++ = *(rdma_argp + 1); 830 *p++ = rdma->sc_fc_credits; 831 *p++ = rdma_error; 832 *p = err_chunk; 833 trace_svcrdma_err_chunk(*rdma_argp); 834 835 svc_rdma_save_io_pages(rqstp, ctxt); 836 837 ctxt->sc_send_wr.num_sge = 1; 838 ctxt->sc_send_wr.opcode = IB_WR_SEND; 839 ctxt->sc_sges[0].length = ctxt->sc_hdrbuf.len; 840 return svc_rdma_send(rdma, &ctxt->sc_send_wr); 841 } 842 843 /** 844 * svc_rdma_sendto - Transmit an RPC reply 845 * @rqstp: processed RPC request, reply XDR already in ::rq_res 846 * 847 * Any resources still associated with @rqstp are released upon return. 848 * If no reply message was possible, the connection is closed. 849 * 850 * Returns: 851 * %0 if an RPC reply has been successfully posted, 852 * %-ENOMEM if a resource shortage occurred (connection is lost), 853 * %-ENOTCONN if posting failed (connection is lost). 854 */ 855 int svc_rdma_sendto(struct svc_rqst *rqstp) 856 { 857 struct svc_xprt *xprt = rqstp->rq_xprt; 858 struct svcxprt_rdma *rdma = 859 container_of(xprt, struct svcxprt_rdma, sc_xprt); 860 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt; 861 __be32 *rdma_argp = rctxt->rc_recv_buf; 862 __be32 *wr_lst = rctxt->rc_write_list; 863 __be32 *rp_ch = rctxt->rc_reply_chunk; 864 struct xdr_buf *xdr = &rqstp->rq_res; 865 struct svc_rdma_send_ctxt *sctxt; 866 __be32 *p; 867 int ret; 868 869 /* Create the RDMA response header. xprt->xpt_mutex, 870 * acquired in svc_send(), serializes RPC replies. The 871 * code path below that inserts the credit grant value 872 * into each transport header runs only inside this 873 * critical section. 874 */ 875 ret = -ENOMEM; 876 sctxt = svc_rdma_send_ctxt_get(rdma); 877 if (!sctxt) 878 goto err0; 879 880 p = xdr_reserve_space(&sctxt->sc_stream, 881 rpcrdma_fixed_maxsz * sizeof(*p)); 882 if (!p) 883 goto err0; 884 *p++ = *rdma_argp; 885 *p++ = *(rdma_argp + 1); 886 *p++ = rdma->sc_fc_credits; 887 *p = rp_ch ? rdma_nomsg : rdma_msg; 888 889 if (svc_rdma_encode_read_list(sctxt) < 0) 890 goto err0; 891 if (wr_lst) { 892 /* XXX: Presume the client sent only one Write chunk */ 893 unsigned long offset; 894 unsigned int length; 895 896 if (rctxt->rc_read_payload_length) { 897 offset = rctxt->rc_read_payload_offset; 898 length = rctxt->rc_read_payload_length; 899 } else { 900 offset = xdr->head[0].iov_len; 901 length = xdr->page_len; 902 } 903 ret = svc_rdma_send_write_chunk(rdma, wr_lst, xdr, offset, 904 length); 905 if (ret < 0) 906 goto err2; 907 if (svc_rdma_encode_write_list(rctxt, sctxt, length) < 0) 908 goto err0; 909 } else { 910 if (xdr_stream_encode_item_absent(&sctxt->sc_stream) < 0) 911 goto err0; 912 } 913 if (rp_ch) { 914 ret = svc_rdma_send_reply_chunk(rdma, rctxt, &rqstp->rq_res); 915 if (ret < 0) 916 goto err2; 917 if (svc_rdma_encode_reply_chunk(rctxt, sctxt, ret) < 0) 918 goto err0; 919 } else { 920 if (xdr_stream_encode_item_absent(&sctxt->sc_stream) < 0) 921 goto err0; 922 } 923 924 ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp); 925 if (ret < 0) 926 goto err1; 927 ret = 0; 928 929 out: 930 rqstp->rq_xprt_ctxt = NULL; 931 svc_rdma_recv_ctxt_put(rdma, rctxt); 932 return ret; 933 934 err2: 935 if (ret != -E2BIG && ret != -EINVAL) 936 goto err1; 937 938 ret = svc_rdma_send_error_msg(rdma, sctxt, rqstp); 939 if (ret < 0) 940 goto err1; 941 ret = 0; 942 goto out; 943 944 err1: 945 svc_rdma_send_ctxt_put(rdma, sctxt); 946 err0: 947 trace_svcrdma_send_failed(rqstp, ret); 948 set_bit(XPT_CLOSE, &xprt->xpt_flags); 949 ret = -ENOTCONN; 950 goto out; 951 } 952 953 /** 954 * svc_rdma_read_payload - special processing for a READ payload 955 * @rqstp: svc_rqst to operate on 956 * @offset: payload's byte offset in @xdr 957 * @length: size of payload, in bytes 958 * 959 * Returns zero on success. 960 * 961 * For the moment, just record the xdr_buf location of the READ 962 * payload. svc_rdma_sendto will use that location later when 963 * we actually send the payload. 964 */ 965 int svc_rdma_read_payload(struct svc_rqst *rqstp, unsigned int offset, 966 unsigned int length) 967 { 968 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt; 969 970 /* XXX: Just one READ payload slot for now, since our 971 * transport implementation currently supports only one 972 * Write chunk. 973 */ 974 rctxt->rc_read_payload_offset = offset; 975 rctxt->rc_read_payload_length = length; 976 977 return 0; 978 } 979