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