\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 1 | /* |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 2 | * Copyright (c) 2003-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 | |
| 40 | /* |
| 41 | * rpc_rdma.c |
| 42 | * |
| 43 | * This file contains the guts of the RPC RDMA protocol, and |
| 44 | * does marshaling/unmarshaling, etc. It is also where interfacing |
| 45 | * to the Linux RPC framework lives. |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 46 | */ |
| 47 | |
| 48 | #include "xprt_rdma.h" |
| 49 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 50 | #include <linux/highmem.h> |
| 51 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 52 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 53 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 54 | #endif |
| 55 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 56 | static const char transfertypes[][12] = { |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 57 | "inline", /* no chunks */ |
| 58 | "read list", /* some argument via rdma read */ |
| 59 | "*read list", /* entire request via rdma read */ |
| 60 | "write list", /* some result via rdma write */ |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 61 | "reply chunk" /* entire reply via rdma write */ |
| 62 | }; |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 63 | |
| 64 | /* Returns size of largest RPC-over-RDMA header in a Call message |
| 65 | * |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 66 | * The largest Call header contains a full-size Read list and a |
| 67 | * minimal Reply chunk. |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 68 | */ |
| 69 | static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs) |
| 70 | { |
| 71 | unsigned int size; |
| 72 | |
| 73 | /* Fixed header fields and list discriminators */ |
| 74 | size = RPCRDMA_HDRLEN_MIN; |
| 75 | |
| 76 | /* Maximum Read list size */ |
| 77 | maxsegs += 2; /* segment for head and tail buffers */ |
| 78 | size = maxsegs * sizeof(struct rpcrdma_read_chunk); |
| 79 | |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 80 | /* Minimal Read chunk size */ |
| 81 | size += sizeof(__be32); /* segment count */ |
| 82 | size += sizeof(struct rpcrdma_segment); |
| 83 | size += sizeof(__be32); /* list discriminator */ |
| 84 | |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 85 | dprintk("RPC: %s: max call header size = %u\n", |
| 86 | __func__, size); |
| 87 | return size; |
| 88 | } |
| 89 | |
| 90 | /* Returns size of largest RPC-over-RDMA header in a Reply message |
| 91 | * |
| 92 | * There is only one Write list or one Reply chunk per Reply |
| 93 | * message. The larger list is the Write list. |
| 94 | */ |
| 95 | static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs) |
| 96 | { |
| 97 | unsigned int size; |
| 98 | |
| 99 | /* Fixed header fields and list discriminators */ |
| 100 | size = RPCRDMA_HDRLEN_MIN; |
| 101 | |
| 102 | /* Maximum Write list size */ |
| 103 | maxsegs += 2; /* segment for head and tail buffers */ |
| 104 | size = sizeof(__be32); /* segment count */ |
| 105 | size += maxsegs * sizeof(struct rpcrdma_segment); |
| 106 | size += sizeof(__be32); /* list discriminator */ |
| 107 | |
| 108 | dprintk("RPC: %s: max reply header size = %u\n", |
| 109 | __func__, size); |
| 110 | return size; |
| 111 | } |
| 112 | |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 113 | void rpcrdma_set_max_header_sizes(struct rpcrdma_xprt *r_xprt) |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 114 | { |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 115 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
| 116 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 117 | unsigned int maxsegs = ia->ri_max_segs; |
| 118 | |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 119 | ia->ri_max_inline_write = cdata->inline_wsize - |
| 120 | rpcrdma_max_call_header_size(maxsegs); |
| 121 | ia->ri_max_inline_read = cdata->inline_rsize - |
| 122 | rpcrdma_max_reply_header_size(maxsegs); |
| 123 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 124 | |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 125 | /* The client can send a request inline as long as the RPCRDMA header |
| 126 | * plus the RPC call fit under the transport's inline limit. If the |
| 127 | * combined call message size exceeds that limit, the client must use |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 128 | * a Read chunk for this operation. |
| 129 | * |
| 130 | * A Read chunk is also required if sending the RPC call inline would |
| 131 | * exceed this device's max_sge limit. |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 132 | */ |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 133 | static bool rpcrdma_args_inline(struct rpcrdma_xprt *r_xprt, |
| 134 | struct rpc_rqst *rqst) |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 135 | { |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 136 | struct xdr_buf *xdr = &rqst->rq_snd_buf; |
| 137 | unsigned int count, remaining, offset; |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 138 | |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 139 | if (xdr->len > r_xprt->rx_ia.ri_max_inline_write) |
| 140 | return false; |
| 141 | |
| 142 | if (xdr->page_len) { |
| 143 | remaining = xdr->page_len; |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 144 | offset = offset_in_page(xdr->page_base); |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 145 | count = 0; |
| 146 | while (remaining) { |
| 147 | remaining -= min_t(unsigned int, |
| 148 | PAGE_SIZE - offset, remaining); |
| 149 | offset = 0; |
| 150 | if (++count > r_xprt->rx_ia.ri_max_send_sges) |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return true; |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* The client can't know how large the actual reply will be. Thus it |
| 159 | * plans for the largest possible reply for that particular ULP |
| 160 | * operation. If the maximum combined reply message size exceeds that |
| 161 | * limit, the client must provide a write list or a reply chunk for |
| 162 | * this request. |
| 163 | */ |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 164 | static bool rpcrdma_results_inline(struct rpcrdma_xprt *r_xprt, |
| 165 | struct rpc_rqst *rqst) |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 166 | { |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 167 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 168 | |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 169 | return rqst->rq_rcv_buf.buflen <= ia->ri_max_inline_read; |
Chuck Lever | 5457ced | 2015-08-03 13:03:49 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 172 | /* Split @vec on page boundaries into SGEs. FMR registers pages, not |
| 173 | * a byte range. Other modes coalesce these SGEs into a single MR |
| 174 | * when they can. |
| 175 | * |
| 176 | * Returns pointer to next available SGE, and bumps the total number |
| 177 | * of SGEs consumed. |
Chuck Lever | 821c791 | 2016-03-04 11:27:52 -0500 | [diff] [blame] | 178 | */ |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 179 | static struct rpcrdma_mr_seg * |
| 180 | rpcrdma_convert_kvec(struct kvec *vec, struct rpcrdma_mr_seg *seg, |
| 181 | unsigned int *n) |
Chuck Lever | 821c791 | 2016-03-04 11:27:52 -0500 | [diff] [blame] | 182 | { |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 183 | u32 remaining, page_offset; |
Chuck Lever | 821c791 | 2016-03-04 11:27:52 -0500 | [diff] [blame] | 184 | char *base; |
| 185 | |
| 186 | base = vec->iov_base; |
| 187 | page_offset = offset_in_page(base); |
| 188 | remaining = vec->iov_len; |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 189 | while (remaining) { |
| 190 | seg->mr_page = NULL; |
| 191 | seg->mr_offset = base; |
| 192 | seg->mr_len = min_t(u32, PAGE_SIZE - page_offset, remaining); |
| 193 | remaining -= seg->mr_len; |
| 194 | base += seg->mr_len; |
| 195 | ++seg; |
| 196 | ++(*n); |
Chuck Lever | 821c791 | 2016-03-04 11:27:52 -0500 | [diff] [blame] | 197 | page_offset = 0; |
| 198 | } |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 199 | return seg; |
Chuck Lever | 821c791 | 2016-03-04 11:27:52 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 202 | /* Convert @xdrbuf into SGEs no larger than a page each. As they |
| 203 | * are registered, these SGEs are then coalesced into RDMA segments |
| 204 | * when the selected memreg mode supports it. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 205 | * |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 206 | * Returns positive number of SGEs consumed, or a negative errno. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 207 | */ |
| 208 | |
| 209 | static int |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 210 | rpcrdma_convert_iovs(struct rpcrdma_xprt *r_xprt, struct xdr_buf *xdrbuf, |
| 211 | unsigned int pos, enum rpcrdma_chunktype type, |
| 212 | struct rpcrdma_mr_seg *seg) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 213 | { |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 214 | unsigned long page_base; |
| 215 | unsigned int len, n; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 216 | struct page **ppages; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 217 | |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 218 | n = 0; |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 219 | if (pos == 0) |
| 220 | seg = rpcrdma_convert_kvec(&xdrbuf->head[0], seg, &n); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 221 | |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 222 | len = xdrbuf->page_len; |
| 223 | ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT); |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 224 | page_base = offset_in_page(xdrbuf->page_base); |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 225 | while (len) { |
| 226 | if (unlikely(!*ppages)) { |
| 227 | /* XXX: Certain upper layer operations do |
| 228 | * not provide receive buffer pages. |
| 229 | */ |
| 230 | *ppages = alloc_page(GFP_ATOMIC); |
| 231 | if (!*ppages) |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 232 | return -EAGAIN; |
Shirley Ma | 196c699 | 2014-05-28 10:34:24 -0400 | [diff] [blame] | 233 | } |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 234 | seg->mr_page = *ppages; |
| 235 | seg->mr_offset = (char *)page_base; |
| 236 | seg->mr_len = min_t(u32, PAGE_SIZE - page_base, len); |
| 237 | len -= seg->mr_len; |
| 238 | ++ppages; |
| 239 | ++seg; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 240 | ++n; |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 241 | page_base = 0; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Chuck Lever | 24abdf1 | 2017-02-08 16:59:46 -0500 | [diff] [blame] | 244 | /* When encoding a Read chunk, the tail iovec contains an |
| 245 | * XDR pad and may be omitted. |
| 246 | */ |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 247 | if (type == rpcrdma_readch && r_xprt->rx_ia.ri_implicit_roundup) |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 248 | goto out; |
Chuck Lever | 677eb17 | 2015-08-03 13:04:17 -0400 | [diff] [blame] | 249 | |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 250 | /* When encoding a Write chunk, some servers need to see an |
| 251 | * extra segment for non-XDR-aligned Write chunks. The upper |
| 252 | * layer provides space in the tail iovec that may be used |
| 253 | * for this purpose. |
Chuck Lever | c8b920b | 2016-09-15 10:57:16 -0400 | [diff] [blame] | 254 | */ |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 255 | if (type == rpcrdma_writech && r_xprt->rx_ia.ri_implicit_roundup) |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 256 | goto out; |
Chuck Lever | c8b920b | 2016-09-15 10:57:16 -0400 | [diff] [blame] | 257 | |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 258 | if (xdrbuf->tail[0].iov_len) |
| 259 | seg = rpcrdma_convert_kvec(&xdrbuf->tail[0], seg, &n); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 260 | |
Chuck Lever | 28d9d56 | 2017-08-14 15:38:22 -0400 | [diff] [blame] | 261 | out: |
| 262 | if (unlikely(n > RPCRDMA_MAX_SEGS)) |
| 263 | return -EIO; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 264 | return n; |
| 265 | } |
| 266 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 267 | static inline int |
| 268 | encode_item_present(struct xdr_stream *xdr) |
| 269 | { |
| 270 | __be32 *p; |
| 271 | |
| 272 | p = xdr_reserve_space(xdr, sizeof(*p)); |
| 273 | if (unlikely(!p)) |
| 274 | return -EMSGSIZE; |
| 275 | |
| 276 | *p = xdr_one; |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static inline int |
| 281 | encode_item_not_present(struct xdr_stream *xdr) |
| 282 | { |
| 283 | __be32 *p; |
| 284 | |
| 285 | p = xdr_reserve_space(xdr, sizeof(*p)); |
| 286 | if (unlikely(!p)) |
| 287 | return -EMSGSIZE; |
| 288 | |
| 289 | *p = xdr_zero; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static void |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 294 | xdr_encode_rdma_segment(__be32 *iptr, struct rpcrdma_mw *mw) |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 295 | { |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 296 | *iptr++ = cpu_to_be32(mw->mw_handle); |
| 297 | *iptr++ = cpu_to_be32(mw->mw_length); |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 298 | xdr_encode_hyper(iptr, mw->mw_offset); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 301 | static int |
| 302 | encode_rdma_segment(struct xdr_stream *xdr, struct rpcrdma_mw *mw) |
| 303 | { |
| 304 | __be32 *p; |
| 305 | |
| 306 | p = xdr_reserve_space(xdr, 4 * sizeof(*p)); |
| 307 | if (unlikely(!p)) |
| 308 | return -EMSGSIZE; |
| 309 | |
| 310 | xdr_encode_rdma_segment(p, mw); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int |
| 315 | encode_read_segment(struct xdr_stream *xdr, struct rpcrdma_mw *mw, |
| 316 | u32 position) |
| 317 | { |
| 318 | __be32 *p; |
| 319 | |
| 320 | p = xdr_reserve_space(xdr, 6 * sizeof(*p)); |
| 321 | if (unlikely(!p)) |
| 322 | return -EMSGSIZE; |
| 323 | |
| 324 | *p++ = xdr_one; /* Item present */ |
| 325 | *p++ = cpu_to_be32(position); |
| 326 | xdr_encode_rdma_segment(p, mw); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* Register and XDR encode the Read list. Supports encoding a list of read |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 331 | * segments that belong to a single read chunk. |
| 332 | * |
| 333 | * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64): |
| 334 | * |
| 335 | * Read chunklist (a linked list): |
| 336 | * N elements, position P (same P for all chunks of same arg!): |
| 337 | * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0 |
| 338 | * |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 339 | * Returns zero on success, or a negative errno if a failure occurred. |
| 340 | * @xdr is advanced to the next position in the stream. |
| 341 | * |
| 342 | * Only a single @pos value is currently supported. |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 343 | */ |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 344 | static noinline int |
| 345 | rpcrdma_encode_read_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req, |
| 346 | struct rpc_rqst *rqst, enum rpcrdma_chunktype rtype) |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 347 | { |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 348 | struct xdr_stream *xdr = &req->rl_stream; |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 349 | struct rpcrdma_mr_seg *seg; |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 350 | struct rpcrdma_mw *mw; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 351 | unsigned int pos; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 352 | int nsegs; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 353 | |
| 354 | pos = rqst->rq_snd_buf.head[0].iov_len; |
| 355 | if (rtype == rpcrdma_areadch) |
| 356 | pos = 0; |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 357 | seg = req->rl_segments; |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 358 | nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_snd_buf, pos, |
| 359 | rtype, seg); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 360 | if (nsegs < 0) |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 361 | return nsegs; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 362 | |
| 363 | do { |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 364 | seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs, |
| 365 | false, &mw); |
| 366 | if (IS_ERR(seg)) |
| 367 | return PTR_ERR(seg); |
Chuck Lever | 9a5c63e | 2017-02-08 17:00:43 -0500 | [diff] [blame] | 368 | rpcrdma_push_mw(mw, &req->rl_registered); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 369 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 370 | if (encode_read_segment(xdr, mw, pos) < 0) |
| 371 | return -EMSGSIZE; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 372 | |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 373 | dprintk("RPC: %5u %s: pos %u %u@0x%016llx:0x%08x (%s)\n", |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 374 | rqst->rq_task->tk_pid, __func__, pos, |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 375 | mw->mw_length, (unsigned long long)mw->mw_offset, |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 376 | mw->mw_handle, mw->mw_nents < nsegs ? "more" : "last"); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 377 | |
| 378 | r_xprt->rx_stats.read_chunk_count++; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 379 | nsegs -= mw->mw_nents; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 380 | } while (nsegs); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 381 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 382 | return 0; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 385 | /* Register and XDR encode the Write list. Supports encoding a list |
| 386 | * containing one array of plain segments that belong to a single |
| 387 | * write chunk. |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 388 | * |
| 389 | * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64): |
| 390 | * |
| 391 | * Write chunklist (a list of (one) counted array): |
| 392 | * N elements: |
| 393 | * 1 - N - HLOO - HLOO - ... - HLOO - 0 |
| 394 | * |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 395 | * Returns zero on success, or a negative errno if a failure occurred. |
| 396 | * @xdr is advanced to the next position in the stream. |
| 397 | * |
| 398 | * Only a single Write chunk is currently supported. |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 399 | */ |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 400 | static noinline int |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 401 | rpcrdma_encode_write_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req, |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 402 | struct rpc_rqst *rqst, enum rpcrdma_chunktype wtype) |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 403 | { |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 404 | struct xdr_stream *xdr = &req->rl_stream; |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 405 | struct rpcrdma_mr_seg *seg; |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 406 | struct rpcrdma_mw *mw; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 407 | int nsegs, nchunks; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 408 | __be32 *segcount; |
| 409 | |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 410 | seg = req->rl_segments; |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 411 | nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf, |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 412 | rqst->rq_rcv_buf.head[0].iov_len, |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 413 | wtype, seg); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 414 | if (nsegs < 0) |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 415 | return nsegs; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 416 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 417 | if (encode_item_present(xdr) < 0) |
| 418 | return -EMSGSIZE; |
| 419 | segcount = xdr_reserve_space(xdr, sizeof(*segcount)); |
| 420 | if (unlikely(!segcount)) |
| 421 | return -EMSGSIZE; |
| 422 | /* Actual value encoded below */ |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 423 | |
| 424 | nchunks = 0; |
| 425 | do { |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 426 | seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs, |
| 427 | true, &mw); |
| 428 | if (IS_ERR(seg)) |
| 429 | return PTR_ERR(seg); |
Chuck Lever | 9a5c63e | 2017-02-08 17:00:43 -0500 | [diff] [blame] | 430 | rpcrdma_push_mw(mw, &req->rl_registered); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 431 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 432 | if (encode_rdma_segment(xdr, mw) < 0) |
| 433 | return -EMSGSIZE; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 434 | |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 435 | dprintk("RPC: %5u %s: %u@0x016%llx:0x%08x (%s)\n", |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 436 | rqst->rq_task->tk_pid, __func__, |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 437 | mw->mw_length, (unsigned long long)mw->mw_offset, |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 438 | mw->mw_handle, mw->mw_nents < nsegs ? "more" : "last"); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 439 | |
| 440 | r_xprt->rx_stats.write_chunk_count++; |
| 441 | r_xprt->rx_stats.total_rdma_request += seg->mr_len; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 442 | nchunks++; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 443 | nsegs -= mw->mw_nents; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 444 | } while (nsegs); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 445 | |
| 446 | /* Update count of segments in this Write chunk */ |
| 447 | *segcount = cpu_to_be32(nchunks); |
| 448 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 449 | return 0; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 452 | /* Register and XDR encode the Reply chunk. Supports encoding an array |
| 453 | * of plain segments that belong to a single write (reply) chunk. |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 454 | * |
| 455 | * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64): |
| 456 | * |
| 457 | * Reply chunk (a counted array): |
| 458 | * N elements: |
| 459 | * 1 - N - HLOO - HLOO - ... - HLOO |
| 460 | * |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 461 | * Returns zero on success, or a negative errno if a failure occurred. |
| 462 | * @xdr is advanced to the next position in the stream. |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 463 | */ |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 464 | static noinline int |
| 465 | rpcrdma_encode_reply_chunk(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req, |
| 466 | struct rpc_rqst *rqst, enum rpcrdma_chunktype wtype) |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 467 | { |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 468 | struct xdr_stream *xdr = &req->rl_stream; |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 469 | struct rpcrdma_mr_seg *seg; |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 470 | struct rpcrdma_mw *mw; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 471 | int nsegs, nchunks; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 472 | __be32 *segcount; |
| 473 | |
Chuck Lever | 5ab8142 | 2016-06-29 13:54:25 -0400 | [diff] [blame] | 474 | seg = req->rl_segments; |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 475 | nsegs = rpcrdma_convert_iovs(r_xprt, &rqst->rq_rcv_buf, 0, wtype, seg); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 476 | if (nsegs < 0) |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 477 | return nsegs; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 478 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 479 | if (encode_item_present(xdr) < 0) |
| 480 | return -EMSGSIZE; |
| 481 | segcount = xdr_reserve_space(xdr, sizeof(*segcount)); |
| 482 | if (unlikely(!segcount)) |
| 483 | return -EMSGSIZE; |
| 484 | /* Actual value encoded below */ |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 485 | |
| 486 | nchunks = 0; |
| 487 | do { |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 488 | seg = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs, |
| 489 | true, &mw); |
| 490 | if (IS_ERR(seg)) |
| 491 | return PTR_ERR(seg); |
Chuck Lever | 9a5c63e | 2017-02-08 17:00:43 -0500 | [diff] [blame] | 492 | rpcrdma_push_mw(mw, &req->rl_registered); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 493 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 494 | if (encode_rdma_segment(xdr, mw) < 0) |
| 495 | return -EMSGSIZE; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 496 | |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 497 | dprintk("RPC: %5u %s: %u@0x%016llx:0x%08x (%s)\n", |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 498 | rqst->rq_task->tk_pid, __func__, |
Chuck Lever | 9d6b040 | 2016-06-29 13:54:16 -0400 | [diff] [blame] | 499 | mw->mw_length, (unsigned long long)mw->mw_offset, |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 500 | mw->mw_handle, mw->mw_nents < nsegs ? "more" : "last"); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 501 | |
| 502 | r_xprt->rx_stats.reply_chunk_count++; |
| 503 | r_xprt->rx_stats.total_rdma_request += seg->mr_len; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 504 | nchunks++; |
Chuck Lever | 6748b0c | 2017-08-14 15:38:30 -0400 | [diff] [blame] | 505 | nsegs -= mw->mw_nents; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 506 | } while (nsegs); |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 507 | |
| 508 | /* Update count of segments in the Reply chunk */ |
| 509 | *segcount = cpu_to_be32(nchunks); |
| 510 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 511 | return 0; |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 512 | } |
| 513 | |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 514 | /** |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 515 | * rpcrdma_unmap_sendctx - DMA-unmap Send buffers |
| 516 | * @sc: sendctx containing SGEs to unmap |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 517 | * |
| 518 | */ |
| 519 | void |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 520 | rpcrdma_unmap_sendctx(struct rpcrdma_sendctx *sc) |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 521 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 522 | struct rpcrdma_ia *ia = &sc->sc_xprt->rx_ia; |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 523 | struct ib_sge *sge; |
| 524 | unsigned int count; |
| 525 | |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 526 | dprintk("RPC: %s: unmapping %u sges for sc=%p\n", |
| 527 | __func__, sc->sc_unmap_count, sc); |
| 528 | |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 529 | /* The first two SGEs contain the transport header and |
| 530 | * the inline buffer. These are always left mapped so |
| 531 | * they can be cheaply re-used. |
| 532 | */ |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 533 | sge = &sc->sc_sges[2]; |
| 534 | for (count = sc->sc_unmap_count; count; ++sge, --count) |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 535 | ib_dma_unmap_page(ia->ri_device, |
| 536 | sge->addr, sge->length, DMA_TO_DEVICE); |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame^] | 537 | |
| 538 | if (test_and_clear_bit(RPCRDMA_REQ_F_TX_RESOURCES, &sc->sc_req->rl_flags)) { |
| 539 | smp_mb__after_atomic(); |
| 540 | wake_up_bit(&sc->sc_req->rl_flags, RPCRDMA_REQ_F_TX_RESOURCES); |
| 541 | } |
Chuck Lever | 394b2c7 | 2017-10-20 10:47:47 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Chuck Lever | a062a2a | 2017-10-20 10:48:03 -0400 | [diff] [blame] | 544 | /* Prepare an SGE for the RPC-over-RDMA transport header. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 545 | */ |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 546 | static bool |
| 547 | rpcrdma_prepare_hdr_sge(struct rpcrdma_ia *ia, struct rpcrdma_req *req, |
| 548 | u32 len) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 549 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 550 | struct rpcrdma_sendctx *sc = req->rl_sendctx; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 551 | struct rpcrdma_regbuf *rb = req->rl_rdmabuf; |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 552 | struct ib_sge *sge = sc->sc_sges; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 553 | |
Chuck Lever | a062a2a | 2017-10-20 10:48:03 -0400 | [diff] [blame] | 554 | if (!rpcrdma_dma_map_regbuf(ia, rb)) |
| 555 | goto out_regbuf; |
| 556 | sge->addr = rdmab_addr(rb); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 557 | sge->length = len; |
Chuck Lever | a062a2a | 2017-10-20 10:48:03 -0400 | [diff] [blame] | 558 | sge->lkey = rdmab_lkey(rb); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 559 | |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 560 | ib_dma_sync_single_for_device(rdmab_device(rb), sge->addr, |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 561 | sge->length, DMA_TO_DEVICE); |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 562 | sc->sc_wr.num_sge++; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 563 | return true; |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 564 | |
| 565 | out_regbuf: |
| 566 | pr_err("rpcrdma: failed to DMA map a Send buffer\n"); |
| 567 | return false; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 568 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 569 | |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 570 | /* Prepare the Send SGEs. The head and tail iovec, and each entry |
| 571 | * in the page list, gets its own SGE. |
| 572 | */ |
| 573 | static bool |
| 574 | rpcrdma_prepare_msg_sges(struct rpcrdma_ia *ia, struct rpcrdma_req *req, |
| 575 | struct xdr_buf *xdr, enum rpcrdma_chunktype rtype) |
| 576 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 577 | struct rpcrdma_sendctx *sc = req->rl_sendctx; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 578 | unsigned int sge_no, page_base, len, remaining; |
| 579 | struct rpcrdma_regbuf *rb = req->rl_sendbuf; |
| 580 | struct ib_device *device = ia->ri_device; |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 581 | struct ib_sge *sge = sc->sc_sges; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 582 | u32 lkey = ia->ri_pd->local_dma_lkey; |
| 583 | struct page *page, **ppages; |
Tom Talpey | b38ab40 | 2009-03-11 14:37:55 -0400 | [diff] [blame] | 584 | |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 585 | /* The head iovec is straightforward, as it is already |
| 586 | * DMA-mapped. Sync the content that has changed. |
| 587 | */ |
| 588 | if (!rpcrdma_dma_map_regbuf(ia, rb)) |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 589 | goto out_regbuf; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 590 | sge_no = 1; |
| 591 | sge[sge_no].addr = rdmab_addr(rb); |
| 592 | sge[sge_no].length = xdr->head[0].iov_len; |
| 593 | sge[sge_no].lkey = rdmab_lkey(rb); |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 594 | ib_dma_sync_single_for_device(rdmab_device(rb), sge[sge_no].addr, |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 595 | sge[sge_no].length, DMA_TO_DEVICE); |
| 596 | |
| 597 | /* If there is a Read chunk, the page list is being handled |
| 598 | * via explicit RDMA, and thus is skipped here. However, the |
| 599 | * tail iovec may include an XDR pad for the page list, as |
| 600 | * well as additional content, and may not reside in the |
| 601 | * same page as the head iovec. |
| 602 | */ |
| 603 | if (rtype == rpcrdma_readch) { |
| 604 | len = xdr->tail[0].iov_len; |
| 605 | |
| 606 | /* Do not include the tail if it is only an XDR pad */ |
| 607 | if (len < 4) |
| 608 | goto out; |
| 609 | |
| 610 | page = virt_to_page(xdr->tail[0].iov_base); |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 611 | page_base = offset_in_page(xdr->tail[0].iov_base); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 612 | |
| 613 | /* If the content in the page list is an odd length, |
| 614 | * xdr_write_pages() has added a pad at the beginning |
| 615 | * of the tail iovec. Force the tail's non-pad content |
| 616 | * to land at the next XDR position in the Send message. |
| 617 | */ |
| 618 | page_base += len & 3; |
| 619 | len -= len & 3; |
| 620 | goto map_tail; |
| 621 | } |
| 622 | |
| 623 | /* If there is a page list present, temporarily DMA map |
| 624 | * and prepare an SGE for each page to be sent. |
| 625 | */ |
| 626 | if (xdr->page_len) { |
| 627 | ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT); |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 628 | page_base = offset_in_page(xdr->page_base); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 629 | remaining = xdr->page_len; |
| 630 | while (remaining) { |
| 631 | sge_no++; |
| 632 | if (sge_no > RPCRDMA_MAX_SEND_SGES - 2) |
| 633 | goto out_mapping_overflow; |
| 634 | |
| 635 | len = min_t(u32, PAGE_SIZE - page_base, remaining); |
| 636 | sge[sge_no].addr = ib_dma_map_page(device, *ppages, |
| 637 | page_base, len, |
| 638 | DMA_TO_DEVICE); |
| 639 | if (ib_dma_mapping_error(device, sge[sge_no].addr)) |
| 640 | goto out_mapping_err; |
| 641 | sge[sge_no].length = len; |
| 642 | sge[sge_no].lkey = lkey; |
| 643 | |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 644 | sc->sc_unmap_count++; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 645 | ppages++; |
| 646 | remaining -= len; |
| 647 | page_base = 0; |
Tom Talpey | b38ab40 | 2009-03-11 14:37:55 -0400 | [diff] [blame] | 648 | } |
Tom Talpey | b38ab40 | 2009-03-11 14:37:55 -0400 | [diff] [blame] | 649 | } |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 650 | |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 651 | /* The tail iovec is not always constructed in the same |
| 652 | * page where the head iovec resides (see, for example, |
| 653 | * gss_wrap_req_priv). To neatly accommodate that case, |
| 654 | * DMA map it separately. |
| 655 | */ |
| 656 | if (xdr->tail[0].iov_len) { |
| 657 | page = virt_to_page(xdr->tail[0].iov_base); |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 658 | page_base = offset_in_page(xdr->tail[0].iov_base); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 659 | len = xdr->tail[0].iov_len; |
| 660 | |
| 661 | map_tail: |
| 662 | sge_no++; |
| 663 | sge[sge_no].addr = ib_dma_map_page(device, page, |
| 664 | page_base, len, |
| 665 | DMA_TO_DEVICE); |
| 666 | if (ib_dma_mapping_error(device, sge[sge_no].addr)) |
| 667 | goto out_mapping_err; |
| 668 | sge[sge_no].length = len; |
| 669 | sge[sge_no].lkey = lkey; |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 670 | sc->sc_unmap_count++; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 671 | } |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 672 | |
| 673 | out: |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 674 | sc->sc_wr.num_sge += sge_no; |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame^] | 675 | if (sc->sc_unmap_count) |
| 676 | __set_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 677 | return true; |
| 678 | |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 679 | out_regbuf: |
| 680 | pr_err("rpcrdma: failed to DMA map a Send buffer\n"); |
| 681 | return false; |
| 682 | |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 683 | out_mapping_overflow: |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 684 | rpcrdma_unmap_sendctx(sc); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 685 | pr_err("rpcrdma: too many Send SGEs (%u)\n", sge_no); |
| 686 | return false; |
| 687 | |
| 688 | out_mapping_err: |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 689 | rpcrdma_unmap_sendctx(sc); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 690 | pr_err("rpcrdma: Send mapping error\n"); |
| 691 | return false; |
| 692 | } |
| 693 | |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 694 | /** |
| 695 | * rpcrdma_prepare_send_sges - Construct SGEs for a Send WR |
| 696 | * @r_xprt: controlling transport |
| 697 | * @req: context of RPC Call being marshalled |
| 698 | * @hdrlen: size of transport header, in bytes |
| 699 | * @xdr: xdr_buf containing RPC Call |
| 700 | * @rtype: chunk type being encoded |
| 701 | * |
| 702 | * Returns 0 on success; otherwise a negative errno is returned. |
| 703 | */ |
| 704 | int |
| 705 | rpcrdma_prepare_send_sges(struct rpcrdma_xprt *r_xprt, |
| 706 | struct rpcrdma_req *req, u32 hdrlen, |
| 707 | struct xdr_buf *xdr, enum rpcrdma_chunktype rtype) |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 708 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 709 | req->rl_sendctx = rpcrdma_sendctx_get_locked(&r_xprt->rx_buf); |
| 710 | if (!req->rl_sendctx) |
| 711 | return -ENOBUFS; |
| 712 | req->rl_sendctx->sc_wr.num_sge = 0; |
| 713 | req->rl_sendctx->sc_unmap_count = 0; |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame^] | 714 | req->rl_sendctx->sc_req = req; |
| 715 | __clear_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags); |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 716 | |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 717 | if (!rpcrdma_prepare_hdr_sge(&r_xprt->rx_ia, req, hdrlen)) |
| 718 | return -EIO; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 719 | |
| 720 | if (rtype != rpcrdma_areadch) |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 721 | if (!rpcrdma_prepare_msg_sges(&r_xprt->rx_ia, req, xdr, rtype)) |
| 722 | return -EIO; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 723 | |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 724 | return 0; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Chuck Lever | 09e6064 | 2017-08-10 12:47:12 -0400 | [diff] [blame] | 727 | /** |
| 728 | * rpcrdma_marshal_req - Marshal and send one RPC request |
| 729 | * @r_xprt: controlling transport |
| 730 | * @rqst: RPC request to be marshaled |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 731 | * |
Chuck Lever | 09e6064 | 2017-08-10 12:47:12 -0400 | [diff] [blame] | 732 | * For the RPC in "rqst", this function: |
| 733 | * - Chooses the transfer mode (eg., RDMA_MSG or RDMA_NOMSG) |
| 734 | * - Registers Read, Write, and Reply chunks |
| 735 | * - Constructs the transport header |
| 736 | * - Posts a Send WR to send the transport header and request |
| 737 | * |
| 738 | * Returns: |
| 739 | * %0 if the RPC was sent successfully, |
| 740 | * %-ENOTCONN if the connection was lost, |
| 741 | * %-EAGAIN if not enough pages are available for on-demand reply buffer, |
| 742 | * %-ENOBUFS if no MRs are available to register chunks, |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 743 | * %-EMSGSIZE if the transport header is too small, |
Chuck Lever | 09e6064 | 2017-08-10 12:47:12 -0400 | [diff] [blame] | 744 | * %-EIO if a permanent problem occurred while marshaling. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 745 | */ |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 746 | int |
Chuck Lever | 09e6064 | 2017-08-10 12:47:12 -0400 | [diff] [blame] | 747 | rpcrdma_marshal_req(struct rpcrdma_xprt *r_xprt, struct rpc_rqst *rqst) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 748 | { |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 749 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 750 | struct xdr_stream *xdr = &req->rl_stream; |
Chuck Lever | e237794 | 2015-03-30 14:33:53 -0400 | [diff] [blame] | 751 | enum rpcrdma_chunktype rtype, wtype; |
Chuck Lever | 65b8017 | 2016-06-29 13:55:06 -0400 | [diff] [blame] | 752 | bool ddp_allowed; |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 753 | __be32 *p; |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 754 | int ret; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 755 | |
Chuck Lever | 83128a6 | 2015-10-24 17:27:59 -0400 | [diff] [blame] | 756 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
| 757 | if (test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state)) |
| 758 | return rpcrdma_bc_marshal_reply(rqst); |
| 759 | #endif |
| 760 | |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 761 | rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0); |
| 762 | xdr_init_encode(xdr, &req->rl_hdrbuf, |
| 763 | req->rl_rdmabuf->rg_base); |
| 764 | |
| 765 | /* Fixed header fields */ |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 766 | ret = -EMSGSIZE; |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 767 | p = xdr_reserve_space(xdr, 4 * sizeof(*p)); |
| 768 | if (!p) |
| 769 | goto out_err; |
| 770 | *p++ = rqst->rq_xid; |
| 771 | *p++ = rpcrdma_version; |
| 772 | *p++ = cpu_to_be32(r_xprt->rx_buf.rb_max_requests); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 773 | |
Chuck Lever | 65b8017 | 2016-06-29 13:55:06 -0400 | [diff] [blame] | 774 | /* When the ULP employs a GSS flavor that guarantees integrity |
| 775 | * or privacy, direct data placement of individual data items |
| 776 | * is not allowed. |
| 777 | */ |
| 778 | ddp_allowed = !(rqst->rq_cred->cr_auth->au_flags & |
| 779 | RPCAUTH_AUTH_DATATOUCH); |
| 780 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 781 | /* |
| 782 | * Chunks needed for results? |
| 783 | * |
| 784 | * o If the expected result is under the inline threshold, all ops |
Chuck Lever | 33943b2 | 2015-08-03 13:04:08 -0400 | [diff] [blame] | 785 | * return as inline. |
Chuck Lever | cce6dee | 2016-05-02 14:41:14 -0400 | [diff] [blame] | 786 | * o Large read ops return data as write chunk(s), header as |
| 787 | * inline. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 788 | * o Large non-read ops return as a single reply chunk. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 789 | */ |
Chuck Lever | cce6dee | 2016-05-02 14:41:14 -0400 | [diff] [blame] | 790 | if (rpcrdma_results_inline(r_xprt, rqst)) |
Chuck Lever | 02eb57d8 | 2015-08-03 13:03:58 -0400 | [diff] [blame] | 791 | wtype = rpcrdma_noch; |
Chuck Lever | 65b8017 | 2016-06-29 13:55:06 -0400 | [diff] [blame] | 792 | else if (ddp_allowed && rqst->rq_rcv_buf.flags & XDRBUF_READ) |
Chuck Lever | cce6dee | 2016-05-02 14:41:14 -0400 | [diff] [blame] | 793 | wtype = rpcrdma_writech; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 794 | else |
Chuck Lever | e237794 | 2015-03-30 14:33:53 -0400 | [diff] [blame] | 795 | wtype = rpcrdma_replych; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Chunks needed for arguments? |
| 799 | * |
| 800 | * o If the total request is under the inline threshold, all ops |
| 801 | * are sent as inline. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 802 | * o Large write ops transmit data as read chunk(s), header as |
| 803 | * inline. |
Chuck Lever | 2fcc213 | 2015-08-03 13:04:26 -0400 | [diff] [blame] | 804 | * o Large non-write ops are sent with the entire message as a |
| 805 | * single read chunk (protocol 0-position special case). |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 806 | * |
Chuck Lever | 2fcc213 | 2015-08-03 13:04:26 -0400 | [diff] [blame] | 807 | * This assumes that the upper layer does not present a request |
| 808 | * that both has a data payload, and whose non-data arguments |
| 809 | * by themselves are larger than the inline threshold. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 810 | */ |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 811 | if (rpcrdma_args_inline(r_xprt, rqst)) { |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 812 | *p++ = rdma_msg; |
Chuck Lever | e237794 | 2015-03-30 14:33:53 -0400 | [diff] [blame] | 813 | rtype = rpcrdma_noch; |
Chuck Lever | 65b8017 | 2016-06-29 13:55:06 -0400 | [diff] [blame] | 814 | } else if (ddp_allowed && rqst->rq_snd_buf.flags & XDRBUF_WRITE) { |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 815 | *p++ = rdma_msg; |
Chuck Lever | e237794 | 2015-03-30 14:33:53 -0400 | [diff] [blame] | 816 | rtype = rpcrdma_readch; |
Chuck Lever | 2fcc213 | 2015-08-03 13:04:26 -0400 | [diff] [blame] | 817 | } else { |
Chuck Lever | 860477d | 2015-08-03 13:04:45 -0400 | [diff] [blame] | 818 | r_xprt->rx_stats.nomsg_call_count++; |
Chuck Lever | 7a80f3f | 2017-08-10 12:47:28 -0400 | [diff] [blame] | 819 | *p++ = rdma_nomsg; |
Chuck Lever | 2fcc213 | 2015-08-03 13:04:26 -0400 | [diff] [blame] | 820 | rtype = rpcrdma_areadch; |
Chuck Lever | 2fcc213 | 2015-08-03 13:04:26 -0400 | [diff] [blame] | 821 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 822 | |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 823 | /* This implementation supports the following combinations |
| 824 | * of chunk lists in one RPC-over-RDMA Call message: |
| 825 | * |
| 826 | * - Read list |
| 827 | * - Write list |
| 828 | * - Reply chunk |
| 829 | * - Read list + Reply chunk |
| 830 | * |
| 831 | * It might not yet support the following combinations: |
| 832 | * |
| 833 | * - Read list + Write list |
| 834 | * |
| 835 | * It does not support the following combinations: |
| 836 | * |
| 837 | * - Write list + Reply chunk |
| 838 | * - Read list + Write list + Reply chunk |
| 839 | * |
| 840 | * This implementation supports only a single chunk in each |
| 841 | * Read or Write list. Thus for example the client cannot |
| 842 | * send a Call message with a Position Zero Read chunk and a |
| 843 | * regular Read chunk at the same time. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 844 | */ |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 845 | if (rtype != rpcrdma_noch) { |
| 846 | ret = rpcrdma_encode_read_list(r_xprt, req, rqst, rtype); |
| 847 | if (ret) |
| 848 | goto out_err; |
| 849 | } |
| 850 | ret = encode_item_not_present(xdr); |
| 851 | if (ret) |
Chuck Lever | 18c0fb3 | 2017-02-08 17:00:27 -0500 | [diff] [blame] | 852 | goto out_err; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 853 | |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 854 | if (wtype == rpcrdma_writech) { |
| 855 | ret = rpcrdma_encode_write_list(r_xprt, req, rqst, wtype); |
| 856 | if (ret) |
| 857 | goto out_err; |
| 858 | } |
| 859 | ret = encode_item_not_present(xdr); |
| 860 | if (ret) |
| 861 | goto out_err; |
| 862 | |
| 863 | if (wtype != rpcrdma_replych) |
| 864 | ret = encode_item_not_present(xdr); |
| 865 | else |
| 866 | ret = rpcrdma_encode_reply_chunk(r_xprt, req, rqst, wtype); |
| 867 | if (ret) |
| 868 | goto out_err; |
| 869 | |
| 870 | dprintk("RPC: %5u %s: %s/%s: hdrlen %u rpclen\n", |
Chuck Lever | 94f58c5 | 2016-05-02 14:41:30 -0400 | [diff] [blame] | 871 | rqst->rq_task->tk_pid, __func__, |
| 872 | transfertypes[rtype], transfertypes[wtype], |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 873 | xdr_stream_pos(xdr)); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 874 | |
Chuck Lever | 857f9ac | 2017-10-20 10:47:55 -0400 | [diff] [blame] | 875 | ret = rpcrdma_prepare_send_sges(r_xprt, req, xdr_stream_pos(xdr), |
| 876 | &rqst->rq_snd_buf, rtype); |
| 877 | if (ret) |
Chuck Lever | 18c0fb3 | 2017-02-08 17:00:27 -0500 | [diff] [blame] | 878 | goto out_err; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 879 | return 0; |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 880 | |
Chuck Lever | 18c0fb3 | 2017-02-08 17:00:27 -0500 | [diff] [blame] | 881 | out_err: |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 882 | if (ret != -ENOBUFS) { |
| 883 | pr_err("rpcrdma: header marshaling failed (%d)\n", ret); |
Chuck Lever | 0031e47 | 2017-04-11 13:23:51 -0400 | [diff] [blame] | 884 | r_xprt->rx_stats.failed_marshal_count++; |
| 885 | } |
Chuck Lever | 39f4cd9 | 2017-08-10 12:47:36 -0400 | [diff] [blame] | 886 | return ret; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 889 | /** |
| 890 | * rpcrdma_inline_fixup - Scatter inline received data into rqst's iovecs |
| 891 | * @rqst: controlling RPC request |
| 892 | * @srcp: points to RPC message payload in receive buffer |
| 893 | * @copy_len: remaining length of receive buffer content |
| 894 | * @pad: Write chunk pad bytes needed (zero for pure inline) |
| 895 | * |
| 896 | * The upper layer has set the maximum number of bytes it can |
| 897 | * receive in each component of rq_rcv_buf. These values are set in |
| 898 | * the head.iov_len, page_len, tail.iov_len, and buflen fields. |
Chuck Lever | cfabe2c | 2016-06-29 13:54:49 -0400 | [diff] [blame] | 899 | * |
| 900 | * Unlike the TCP equivalent (xdr_partial_copy_from_skb), in |
| 901 | * many cases this function simply updates iov_base pointers in |
| 902 | * rq_rcv_buf to point directly to the received reply data, to |
| 903 | * avoid copying reply data. |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 904 | * |
| 905 | * Returns the count of bytes which had to be memcopied. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 906 | */ |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 907 | static unsigned long |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 908 | rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 909 | { |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 910 | unsigned long fixup_copy_count; |
| 911 | int i, npages, curlen; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 912 | char *destp; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 913 | struct page **ppages; |
| 914 | int page_base; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 915 | |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 916 | /* The head iovec is redirected to the RPC reply message |
| 917 | * in the receive buffer, to avoid a memcopy. |
| 918 | */ |
| 919 | rqst->rq_rcv_buf.head[0].iov_base = srcp; |
Chuck Lever | cfabe2c | 2016-06-29 13:54:49 -0400 | [diff] [blame] | 920 | rqst->rq_private_buf.head[0].iov_base = srcp; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 921 | |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 922 | /* The contents of the receive buffer that follow |
| 923 | * head.iov_len bytes are copied into the page list. |
| 924 | */ |
| 925 | curlen = rqst->rq_rcv_buf.head[0].iov_len; |
| 926 | if (curlen > copy_len) |
| 927 | curlen = copy_len; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 928 | dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n", |
| 929 | __func__, srcp, copy_len, curlen); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 930 | srcp += curlen; |
| 931 | copy_len -= curlen; |
| 932 | |
Chuck Lever | d933cc3 | 2017-06-08 11:53:16 -0400 | [diff] [blame] | 933 | ppages = rqst->rq_rcv_buf.pages + |
| 934 | (rqst->rq_rcv_buf.page_base >> PAGE_SHIFT); |
| 935 | page_base = offset_in_page(rqst->rq_rcv_buf.page_base); |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 936 | fixup_copy_count = 0; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 937 | if (copy_len && rqst->rq_rcv_buf.page_len) { |
Chuck Lever | 80414ab | 2016-06-29 13:54:33 -0400 | [diff] [blame] | 938 | int pagelist_len; |
| 939 | |
| 940 | pagelist_len = rqst->rq_rcv_buf.page_len; |
| 941 | if (pagelist_len > copy_len) |
| 942 | pagelist_len = copy_len; |
| 943 | npages = PAGE_ALIGN(page_base + pagelist_len) >> PAGE_SHIFT; |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 944 | for (i = 0; i < npages; i++) { |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 945 | curlen = PAGE_SIZE - page_base; |
Chuck Lever | 80414ab | 2016-06-29 13:54:33 -0400 | [diff] [blame] | 946 | if (curlen > pagelist_len) |
| 947 | curlen = pagelist_len; |
| 948 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 949 | dprintk("RPC: %s: page %d" |
| 950 | " srcp 0x%p len %d curlen %d\n", |
| 951 | __func__, i, srcp, copy_len, curlen); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 952 | destp = kmap_atomic(ppages[i]); |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 953 | memcpy(destp + page_base, srcp, curlen); |
| 954 | flush_dcache_page(ppages[i]); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 955 | kunmap_atomic(destp); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 956 | srcp += curlen; |
| 957 | copy_len -= curlen; |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 958 | fixup_copy_count += curlen; |
Chuck Lever | 80414ab | 2016-06-29 13:54:33 -0400 | [diff] [blame] | 959 | pagelist_len -= curlen; |
| 960 | if (!pagelist_len) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 961 | break; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 962 | page_base = 0; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 963 | } |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 964 | |
| 965 | /* Implicit padding for the last segment in a Write |
| 966 | * chunk is inserted inline at the front of the tail |
| 967 | * iovec. The upper layer ignores the content of |
| 968 | * the pad. Simply ensure inline content in the tail |
| 969 | * that follows the Write chunk is properly aligned. |
| 970 | */ |
| 971 | if (pad) |
| 972 | srcp -= pad; |
Chuck Lever | 2b7bbc9 | 2014-03-12 12:51:30 -0400 | [diff] [blame] | 973 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 974 | |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 975 | /* The tail iovec is redirected to the remaining data |
| 976 | * in the receive buffer, to avoid a memcopy. |
| 977 | */ |
Chuck Lever | cfabe2c | 2016-06-29 13:54:49 -0400 | [diff] [blame] | 978 | if (copy_len || pad) { |
Chuck Lever | cb0ae1f | 2016-06-29 13:54:41 -0400 | [diff] [blame] | 979 | rqst->rq_rcv_buf.tail[0].iov_base = srcp; |
Chuck Lever | cfabe2c | 2016-06-29 13:54:49 -0400 | [diff] [blame] | 980 | rqst->rq_private_buf.tail[0].iov_base = srcp; |
| 981 | } |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 982 | |
Chuck Lever | 64695bde | 2016-06-29 13:54:58 -0400 | [diff] [blame] | 983 | return fixup_copy_count; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 984 | } |
| 985 | |
Chuck Lever | 4b196dc6 | 2017-06-08 11:51:56 -0400 | [diff] [blame] | 986 | /* Caller must guarantee @rep remains stable during this call. |
| 987 | */ |
| 988 | static void |
| 989 | rpcrdma_mark_remote_invalidation(struct list_head *mws, |
| 990 | struct rpcrdma_rep *rep) |
| 991 | { |
| 992 | struct rpcrdma_mw *mw; |
| 993 | |
| 994 | if (!(rep->rr_wc_flags & IB_WC_WITH_INVALIDATE)) |
| 995 | return; |
| 996 | |
| 997 | list_for_each_entry(mw, mws, mw_list) |
| 998 | if (mw->mw_handle == rep->rr_inv_rkey) { |
| 999 | mw->mw_flags = RPCRDMA_MW_F_RI; |
| 1000 | break; /* only one invalidated MR per RPC */ |
| 1001 | } |
| 1002 | } |
| 1003 | |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1004 | /* By convention, backchannel calls arrive via rdma_msg type |
| 1005 | * messages, and never populate the chunk lists. This makes |
| 1006 | * the RPC/RDMA header small and fixed in size, so it is |
| 1007 | * straightforward to check the RPC header's direction field. |
| 1008 | */ |
| 1009 | static bool |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1010 | rpcrdma_is_bcall(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep) |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1011 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1012 | { |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1013 | struct xdr_stream *xdr = &rep->rr_stream; |
| 1014 | __be32 *p; |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1015 | |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1016 | if (rep->rr_proc != rdma_msg) |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1017 | return false; |
| 1018 | |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1019 | /* Peek at stream contents without advancing. */ |
| 1020 | p = xdr_inline_decode(xdr, 0); |
| 1021 | |
| 1022 | /* Chunk lists */ |
| 1023 | if (*p++ != xdr_zero) |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1024 | return false; |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1025 | if (*p++ != xdr_zero) |
| 1026 | return false; |
| 1027 | if (*p++ != xdr_zero) |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1028 | return false; |
| 1029 | |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1030 | /* RPC header */ |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1031 | if (*p++ != rep->rr_xid) |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1032 | return false; |
| 1033 | if (*p != cpu_to_be32(RPC_CALL)) |
| 1034 | return false; |
| 1035 | |
| 1036 | /* Now that we are sure this is a backchannel call, |
| 1037 | * advance to the RPC header. |
| 1038 | */ |
| 1039 | p = xdr_inline_decode(xdr, 3 * sizeof(*p)); |
| 1040 | if (unlikely(!p)) |
| 1041 | goto out_short; |
| 1042 | |
| 1043 | rpcrdma_bc_receive_call(r_xprt, rep); |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1044 | return true; |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1045 | |
| 1046 | out_short: |
| 1047 | pr_warn("RPC/RDMA short backward direction call\n"); |
| 1048 | if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep)) |
| 1049 | xprt_disconnect_done(&r_xprt->rx_xprt); |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1050 | return true; |
| 1051 | } |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1052 | #else /* CONFIG_SUNRPC_BACKCHANNEL */ |
| 1053 | { |
| 1054 | return false; |
Chuck Lever | 63cae47 | 2015-10-24 17:28:08 -0400 | [diff] [blame] | 1055 | } |
| 1056 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
| 1057 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1058 | static int decode_rdma_segment(struct xdr_stream *xdr, u32 *length) |
| 1059 | { |
| 1060 | __be32 *p; |
| 1061 | |
| 1062 | p = xdr_inline_decode(xdr, 4 * sizeof(*p)); |
| 1063 | if (unlikely(!p)) |
| 1064 | return -EIO; |
| 1065 | |
| 1066 | ifdebug(FACILITY) { |
| 1067 | u64 offset; |
| 1068 | u32 handle; |
| 1069 | |
| 1070 | handle = be32_to_cpup(p++); |
| 1071 | *length = be32_to_cpup(p++); |
| 1072 | xdr_decode_hyper(p, &offset); |
| 1073 | dprintk("RPC: %s: segment %u@0x%016llx:0x%08x\n", |
| 1074 | __func__, *length, (unsigned long long)offset, |
| 1075 | handle); |
| 1076 | } else { |
| 1077 | *length = be32_to_cpup(p + 1); |
| 1078 | } |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | static int decode_write_chunk(struct xdr_stream *xdr, u32 *length) |
| 1084 | { |
| 1085 | u32 segcount, seglength; |
| 1086 | __be32 *p; |
| 1087 | |
| 1088 | p = xdr_inline_decode(xdr, sizeof(*p)); |
| 1089 | if (unlikely(!p)) |
| 1090 | return -EIO; |
| 1091 | |
| 1092 | *length = 0; |
| 1093 | segcount = be32_to_cpup(p); |
| 1094 | while (segcount--) { |
| 1095 | if (decode_rdma_segment(xdr, &seglength)) |
| 1096 | return -EIO; |
| 1097 | *length += seglength; |
| 1098 | } |
| 1099 | |
| 1100 | dprintk("RPC: %s: segcount=%u, %u bytes\n", |
| 1101 | __func__, be32_to_cpup(p), *length); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | /* In RPC-over-RDMA Version One replies, a Read list is never |
| 1106 | * expected. This decoder is a stub that returns an error if |
| 1107 | * a Read list is present. |
| 1108 | */ |
| 1109 | static int decode_read_list(struct xdr_stream *xdr) |
| 1110 | { |
| 1111 | __be32 *p; |
| 1112 | |
| 1113 | p = xdr_inline_decode(xdr, sizeof(*p)); |
| 1114 | if (unlikely(!p)) |
| 1115 | return -EIO; |
| 1116 | if (unlikely(*p != xdr_zero)) |
| 1117 | return -EIO; |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | /* Supports only one Write chunk in the Write list |
| 1122 | */ |
| 1123 | static int decode_write_list(struct xdr_stream *xdr, u32 *length) |
| 1124 | { |
| 1125 | u32 chunklen; |
| 1126 | bool first; |
| 1127 | __be32 *p; |
| 1128 | |
| 1129 | *length = 0; |
| 1130 | first = true; |
| 1131 | do { |
| 1132 | p = xdr_inline_decode(xdr, sizeof(*p)); |
| 1133 | if (unlikely(!p)) |
| 1134 | return -EIO; |
| 1135 | if (*p == xdr_zero) |
| 1136 | break; |
| 1137 | if (!first) |
| 1138 | return -EIO; |
| 1139 | |
| 1140 | if (decode_write_chunk(xdr, &chunklen)) |
| 1141 | return -EIO; |
| 1142 | *length += chunklen; |
| 1143 | first = false; |
| 1144 | } while (true); |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | static int decode_reply_chunk(struct xdr_stream *xdr, u32 *length) |
| 1149 | { |
| 1150 | __be32 *p; |
| 1151 | |
| 1152 | p = xdr_inline_decode(xdr, sizeof(*p)); |
| 1153 | if (unlikely(!p)) |
| 1154 | return -EIO; |
| 1155 | |
| 1156 | *length = 0; |
| 1157 | if (*p != xdr_zero) |
| 1158 | if (decode_write_chunk(xdr, length)) |
| 1159 | return -EIO; |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1163 | static int |
| 1164 | rpcrdma_decode_msg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep, |
| 1165 | struct rpc_rqst *rqst) |
| 1166 | { |
| 1167 | struct xdr_stream *xdr = &rep->rr_stream; |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1168 | u32 writelist, replychunk, rpclen; |
| 1169 | char *base; |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1170 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1171 | /* Decode the chunk lists */ |
| 1172 | if (decode_read_list(xdr)) |
| 1173 | return -EIO; |
| 1174 | if (decode_write_list(xdr, &writelist)) |
| 1175 | return -EIO; |
| 1176 | if (decode_reply_chunk(xdr, &replychunk)) |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1177 | return -EIO; |
| 1178 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1179 | /* RDMA_MSG sanity checks */ |
| 1180 | if (unlikely(replychunk)) |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1181 | return -EIO; |
| 1182 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1183 | /* Build the RPC reply's Payload stream in rqst->rq_rcv_buf */ |
| 1184 | base = (char *)xdr_inline_decode(xdr, 0); |
| 1185 | rpclen = xdr_stream_remaining(xdr); |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1186 | r_xprt->rx_stats.fixup_copy_count += |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1187 | rpcrdma_inline_fixup(rqst, base, rpclen, writelist & 3); |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1188 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1189 | r_xprt->rx_stats.total_rdma_reply += writelist; |
| 1190 | return rpclen + xdr_align_size(writelist); |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | static noinline int |
| 1194 | rpcrdma_decode_nomsg(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep) |
| 1195 | { |
| 1196 | struct xdr_stream *xdr = &rep->rr_stream; |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1197 | u32 writelist, replychunk; |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1198 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1199 | /* Decode the chunk lists */ |
| 1200 | if (decode_read_list(xdr)) |
| 1201 | return -EIO; |
| 1202 | if (decode_write_list(xdr, &writelist)) |
| 1203 | return -EIO; |
| 1204 | if (decode_reply_chunk(xdr, &replychunk)) |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1205 | return -EIO; |
| 1206 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1207 | /* RDMA_NOMSG sanity checks */ |
| 1208 | if (unlikely(writelist)) |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1209 | return -EIO; |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1210 | if (unlikely(!replychunk)) |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1211 | return -EIO; |
| 1212 | |
Chuck Lever | 264b0cd | 2017-08-03 14:30:27 -0400 | [diff] [blame] | 1213 | /* Reply chunk buffer already is the reply vector */ |
| 1214 | r_xprt->rx_stats.total_rdma_reply += replychunk; |
| 1215 | return replychunk; |
Chuck Lever | 07ff2dd | 2017-08-03 14:30:19 -0400 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | static noinline int |
| 1219 | rpcrdma_decode_error(struct rpcrdma_xprt *r_xprt, struct rpcrdma_rep *rep, |
| 1220 | struct rpc_rqst *rqst) |
| 1221 | { |
| 1222 | struct xdr_stream *xdr = &rep->rr_stream; |
| 1223 | __be32 *p; |
| 1224 | |
| 1225 | p = xdr_inline_decode(xdr, sizeof(*p)); |
| 1226 | if (unlikely(!p)) |
| 1227 | return -EIO; |
| 1228 | |
| 1229 | switch (*p) { |
| 1230 | case err_vers: |
| 1231 | p = xdr_inline_decode(xdr, 2 * sizeof(*p)); |
| 1232 | if (!p) |
| 1233 | break; |
| 1234 | dprintk("RPC: %5u: %s: server reports version error (%u-%u)\n", |
| 1235 | rqst->rq_task->tk_pid, __func__, |
| 1236 | be32_to_cpup(p), be32_to_cpu(*(p + 1))); |
| 1237 | break; |
| 1238 | case err_chunk: |
| 1239 | dprintk("RPC: %5u: %s: server reports header decoding error\n", |
| 1240 | rqst->rq_task->tk_pid, __func__); |
| 1241 | break; |
| 1242 | default: |
| 1243 | dprintk("RPC: %5u: %s: server reports unrecognized error %d\n", |
| 1244 | rqst->rq_task->tk_pid, __func__, be32_to_cpup(p)); |
| 1245 | } |
| 1246 | |
| 1247 | r_xprt->rx_stats.bad_reply_count++; |
| 1248 | return -EREMOTEIO; |
| 1249 | } |
| 1250 | |
Chuck Lever | e1352c9 | 2017-10-16 15:01:22 -0400 | [diff] [blame] | 1251 | /* Perform XID lookup, reconstruction of the RPC reply, and |
| 1252 | * RPC completion while holding the transport lock to ensure |
| 1253 | * the rep, rqst, and rq_task pointers remain stable. |
| 1254 | */ |
| 1255 | void rpcrdma_complete_rqst(struct rpcrdma_rep *rep) |
| 1256 | { |
| 1257 | struct rpcrdma_xprt *r_xprt = rep->rr_rxprt; |
| 1258 | struct rpc_xprt *xprt = &r_xprt->rx_xprt; |
| 1259 | struct rpc_rqst *rqst = rep->rr_rqst; |
| 1260 | unsigned long cwnd; |
| 1261 | int status; |
| 1262 | |
| 1263 | xprt->reestablish_timeout = 0; |
| 1264 | |
| 1265 | switch (rep->rr_proc) { |
| 1266 | case rdma_msg: |
| 1267 | status = rpcrdma_decode_msg(r_xprt, rep, rqst); |
| 1268 | break; |
| 1269 | case rdma_nomsg: |
| 1270 | status = rpcrdma_decode_nomsg(r_xprt, rep); |
| 1271 | break; |
| 1272 | case rdma_error: |
| 1273 | status = rpcrdma_decode_error(r_xprt, rep, rqst); |
| 1274 | break; |
| 1275 | default: |
| 1276 | status = -EIO; |
| 1277 | } |
| 1278 | if (status < 0) |
| 1279 | goto out_badheader; |
| 1280 | |
| 1281 | out: |
| 1282 | spin_lock(&xprt->recv_lock); |
| 1283 | cwnd = xprt->cwnd; |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1284 | xprt->cwnd = r_xprt->rx_buf.rb_credits << RPC_CWNDSHIFT; |
Chuck Lever | e1352c9 | 2017-10-16 15:01:22 -0400 | [diff] [blame] | 1285 | if (xprt->cwnd > cwnd) |
| 1286 | xprt_release_rqst_cong(rqst->rq_task); |
| 1287 | |
| 1288 | xprt_complete_rqst(rqst->rq_task, status); |
| 1289 | xprt_unpin_rqst(rqst); |
| 1290 | spin_unlock(&xprt->recv_lock); |
| 1291 | return; |
| 1292 | |
| 1293 | /* If the incoming reply terminated a pending RPC, the next |
| 1294 | * RPC call will post a replacement receive buffer as it is |
| 1295 | * being marshaled. |
| 1296 | */ |
| 1297 | out_badheader: |
| 1298 | dprintk("RPC: %5u %s: invalid rpcrdma reply (type %u)\n", |
| 1299 | rqst->rq_task->tk_pid, __func__, be32_to_cpu(rep->rr_proc)); |
| 1300 | r_xprt->rx_stats.bad_reply_count++; |
| 1301 | status = -EIO; |
| 1302 | goto out; |
| 1303 | } |
| 1304 | |
Chuck Lever | 0ba6f37 | 2017-10-20 10:48:28 -0400 | [diff] [blame] | 1305 | void rpcrdma_release_rqst(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req) |
| 1306 | { |
| 1307 | /* Invalidate and unmap the data payloads before waking |
| 1308 | * the waiting application. This guarantees the memory |
| 1309 | * regions are properly fenced from the server before the |
| 1310 | * application accesses the data. It also ensures proper |
| 1311 | * send flow control: waking the next RPC waits until this |
| 1312 | * RPC has relinquished all its Send Queue entries. |
| 1313 | */ |
| 1314 | if (!list_empty(&req->rl_registered)) |
| 1315 | r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt, |
| 1316 | &req->rl_registered); |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame^] | 1317 | |
| 1318 | /* Ensure that any DMA mapped pages associated with |
| 1319 | * the Send of the RPC Call have been unmapped before |
| 1320 | * allowing the RPC to complete. This protects argument |
| 1321 | * memory not controlled by the RPC client from being |
| 1322 | * re-used before we're done with it. |
| 1323 | */ |
| 1324 | if (test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) { |
| 1325 | r_xprt->rx_stats.reply_waits_for_send++; |
| 1326 | out_of_line_wait_on_bit(&req->rl_flags, |
| 1327 | RPCRDMA_REQ_F_TX_RESOURCES, |
| 1328 | bit_wait, |
| 1329 | TASK_UNINTERRUPTIBLE); |
| 1330 | } |
Chuck Lever | 0ba6f37 | 2017-10-20 10:48:28 -0400 | [diff] [blame] | 1331 | } |
| 1332 | |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1333 | /* Reply handling runs in the poll worker thread. Anything that |
| 1334 | * might wait is deferred to a separate workqueue. |
| 1335 | */ |
| 1336 | void rpcrdma_deferred_completion(struct work_struct *work) |
| 1337 | { |
| 1338 | struct rpcrdma_rep *rep = |
| 1339 | container_of(work, struct rpcrdma_rep, rr_work); |
| 1340 | struct rpcrdma_req *req = rpcr_to_rdmar(rep->rr_rqst); |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1341 | |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1342 | rpcrdma_mark_remote_invalidation(&req->rl_registered, rep); |
Chuck Lever | 0ba6f37 | 2017-10-20 10:48:28 -0400 | [diff] [blame] | 1343 | rpcrdma_release_rqst(rep->rr_rxprt, req); |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1344 | rpcrdma_complete_rqst(rep); |
| 1345 | } |
| 1346 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 1347 | /* Process received RPC/RDMA messages. |
| 1348 | * |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1349 | * Errors must result in the RPC task either being awakened, or |
| 1350 | * allowed to timeout, to discover the errors at that time. |
| 1351 | */ |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1352 | void rpcrdma_reply_handler(struct rpcrdma_rep *rep) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1353 | { |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1354 | struct rpcrdma_xprt *r_xprt = rep->rr_rxprt; |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1355 | struct rpc_xprt *xprt = &r_xprt->rx_xprt; |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1356 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1357 | struct rpcrdma_req *req; |
| 1358 | struct rpc_rqst *rqst; |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1359 | u32 credits; |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1360 | __be32 *p; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1361 | |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1362 | dprintk("RPC: %s: incoming rep %p\n", __func__, rep); |
| 1363 | |
Chuck Lever | e2a6719 | 2017-08-03 14:30:44 -0400 | [diff] [blame] | 1364 | if (rep->rr_hdrbuf.head[0].iov_len == 0) |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1365 | goto out_badstatus; |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1366 | |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1367 | xdr_init_decode(&rep->rr_stream, &rep->rr_hdrbuf, |
Chuck Lever | 96f8778 | 2017-08-03 14:30:03 -0400 | [diff] [blame] | 1368 | rep->rr_hdrbuf.head[0].iov_base); |
| 1369 | |
| 1370 | /* Fixed transport header fields */ |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1371 | p = xdr_inline_decode(&rep->rr_stream, 4 * sizeof(*p)); |
Chuck Lever | 96f8778 | 2017-08-03 14:30:03 -0400 | [diff] [blame] | 1372 | if (unlikely(!p)) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1373 | goto out_shortreply; |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1374 | rep->rr_xid = *p++; |
| 1375 | rep->rr_vers = *p++; |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1376 | credits = be32_to_cpu(*p++); |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1377 | rep->rr_proc = *p++; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1378 | |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1379 | if (rep->rr_vers != rpcrdma_version) |
Chuck Lever | 61433af | 2017-10-16 15:01:06 -0400 | [diff] [blame] | 1380 | goto out_badversion; |
| 1381 | |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1382 | if (rpcrdma_is_bcall(r_xprt, rep)) |
Chuck Lever | 41c8f70 | 2017-08-03 14:30:11 -0400 | [diff] [blame] | 1383 | return; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1384 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 1385 | /* Match incoming rpcrdma_rep to an rpcrdma_req to |
| 1386 | * get context for handling any incoming chunks. |
| 1387 | */ |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 1388 | spin_lock(&xprt->recv_lock); |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1389 | rqst = xprt_lookup_rqst(xprt, rep->rr_xid); |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 1390 | if (!rqst) |
| 1391 | goto out_norqst; |
| 1392 | xprt_pin_rqst(rqst); |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1393 | |
| 1394 | if (credits == 0) |
| 1395 | credits = 1; /* don't deadlock */ |
| 1396 | else if (credits > buf->rb_max_requests) |
| 1397 | credits = buf->rb_max_requests; |
| 1398 | buf->rb_credits = credits; |
| 1399 | |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 1400 | spin_unlock(&xprt->recv_lock); |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 1401 | |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 1402 | req = rpcr_to_rdmar(rqst); |
Chuck Lever | 4b196dc6 | 2017-06-08 11:51:56 -0400 | [diff] [blame] | 1403 | req->rl_reply = rep; |
Chuck Lever | e1352c9 | 2017-10-16 15:01:22 -0400 | [diff] [blame] | 1404 | rep->rr_rqst = rqst; |
Chuck Lever | 0ba6f37 | 2017-10-20 10:48:28 -0400 | [diff] [blame] | 1405 | clear_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags); |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1406 | |
Chuck Lever | af0f16e | 2016-03-04 11:27:43 -0500 | [diff] [blame] | 1407 | dprintk("RPC: %s: reply %p completes request %p (xid 0x%08x)\n", |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1408 | __func__, rep, req, be32_to_cpu(rep->rr_xid)); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1409 | |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame^] | 1410 | if (list_empty(&req->rl_registered) && |
| 1411 | !test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1412 | rpcrdma_complete_rqst(rep); |
| 1413 | else |
| 1414 | queue_work(rpcrdma_receive_wq, &rep->rr_work); |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1415 | return; |
| 1416 | |
| 1417 | out_badstatus: |
| 1418 | rpcrdma_recv_buffer_put(rep); |
| 1419 | if (r_xprt->rx_ep.rep_connected == 1) { |
| 1420 | r_xprt->rx_ep.rep_connected = -EIO; |
| 1421 | rpcrdma_conn_func(&r_xprt->rx_ep); |
| 1422 | } |
| 1423 | return; |
| 1424 | |
Chuck Lever | 61433af | 2017-10-16 15:01:06 -0400 | [diff] [blame] | 1425 | out_badversion: |
| 1426 | dprintk("RPC: %s: invalid version %d\n", |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1427 | __func__, be32_to_cpu(rep->rr_vers)); |
Chuck Lever | 61433af | 2017-10-16 15:01:06 -0400 | [diff] [blame] | 1428 | goto repost; |
| 1429 | |
Chuck Lever | e1352c9 | 2017-10-16 15:01:22 -0400 | [diff] [blame] | 1430 | /* The RPC transaction has already been terminated, or the header |
| 1431 | * is corrupt. |
Chuck Lever | 59aa1f9 | 2016-03-04 11:28:18 -0500 | [diff] [blame] | 1432 | */ |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1433 | out_norqst: |
Trond Myklebust | ce7c252 | 2017-08-16 15:30:35 -0400 | [diff] [blame] | 1434 | spin_unlock(&xprt->recv_lock); |
Chuck Lever | 96f8778 | 2017-08-03 14:30:03 -0400 | [diff] [blame] | 1435 | dprintk("RPC: %s: no match for incoming xid 0x%08x\n", |
Chuck Lever | 5381e0e | 2017-10-16 15:01:14 -0400 | [diff] [blame] | 1436 | __func__, be32_to_cpu(rep->rr_xid)); |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1437 | goto repost; |
| 1438 | |
Chuck Lever | 9590d08 | 2017-08-23 17:05:58 -0400 | [diff] [blame] | 1439 | out_shortreply: |
| 1440 | dprintk("RPC: %s: short/invalid reply\n", __func__); |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1441 | |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1442 | /* If no pending RPC transaction was matched, post a replacement |
| 1443 | * receive buffer before returning. |
| 1444 | */ |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1445 | repost: |
| 1446 | r_xprt->rx_stats.bad_reply_count++; |
Chuck Lever | b157380 | 2016-09-15 10:56:35 -0400 | [diff] [blame] | 1447 | if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep)) |
Chuck Lever | b0e178a | 2015-10-24 17:26:54 -0400 | [diff] [blame] | 1448 | rpcrdma_recv_buffer_put(rep); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 1449 | } |