1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (c) 2016-2018 Oracle. All rights reserved.
4 * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the BSD-type
11 * license below:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
24 *
25 * Neither the name of the Network Appliance, Inc. nor the names of
26 * its contributors may be used to endorse or promote products
27 * derived from this software without specific prior written
28 * permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Author: Tom Tucker <tom@opengridcomputing.com>
43 */
44
45 /* Operation
46 *
47 * The main entry point is svc_rdma_sendto. This is called by the
48 * RPC server when an RPC Reply is ready to be transmitted to a client.
49 *
50 * The passed-in svc_rqst contains a struct xdr_buf which holds an
51 * XDR-encoded RPC Reply message. sendto must construct the RPC-over-RDMA
52 * transport header, post all Write WRs needed for this Reply, then post
53 * a Send WR conveying the transport header and the RPC message itself to
54 * the client.
55 *
56 * svc_rdma_sendto must fully transmit the Reply before returning, as
57 * the svc_rqst will be recycled as soon as sendto returns. Remaining
58 * resources referred to by the svc_rqst are also recycled at that time.
59 * Therefore any resources that must remain longer must be detached
60 * from the svc_rqst and released later.
61 *
62 * Page Management
63 *
64 * The I/O that performs Reply transmission is asynchronous, and may
65 * complete well after sendto returns. Thus pages under I/O must be
66 * removed from the svc_rqst before sendto returns.
67 *
68 * The logic here depends on Send Queue and completion ordering. Since
69 * the Send WR is always posted last, it will always complete last. Thus
70 * when it completes, it is guaranteed that all previous Write WRs have
71 * also completed.
72 *
73 * Write WRs are constructed and posted. Each Write segment gets its own
74 * svc_rdma_rw_ctxt, allowing the Write completion handler to find and
75 * DMA-unmap the pages under I/O for that Write segment. The Write
76 * completion handler does not release any pages.
77 *
78 * When the Send WR is constructed, it also gets its own svc_rdma_send_ctxt.
79 * The ownership of all of the Reply's pages are transferred into that
80 * ctxt, the Send WR is posted, and sendto returns.
81 *
82 * The svc_rdma_send_ctxt is presented when the Send WR completes.
83 * The Send completion handler queues the send_ctxt onto the
84 * per-transport sc_send_release_list (a lock-free llist). The
85 * nfsd thread drains sc_send_release_list in xpo_release_ctxt
86 * between RPCs, DMA-unmapping SGEs, releasing chunk I/O
87 * resources and pages, and returning send_ctxts to the free
88 * list in a batch.
89 *
90 * Error Handling
91 *
92 * - If the Send WR is posted successfully, it will either complete
93 * successfully, or get flushed. Either way, the Send completion
94 * handler queues the send_ctxt for deferred release.
95 * - If the Send WR cannot be posted, the forward path releases the
96 * Reply's pages.
97 *
98 * This handles the case, without the use of page reference counting,
99 * where two different Write segments send portions of the same page.
100 */
101
102 #include <linux/spinlock.h>
103 #include <linux/unaligned.h>
104
105 #include <rdma/ib_verbs.h>
106 #include <rdma/rdma_cm.h>
107
108 #include <linux/sunrpc/debug.h>
109 #include <linux/sunrpc/svc_rdma.h>
110
111 #include "xprt_rdma.h"
112 #include <trace/events/rpcrdma.h>
113
114 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc);
115
116 static struct svc_rdma_send_ctxt *
svc_rdma_send_ctxt_alloc(struct svcxprt_rdma * rdma)117 svc_rdma_send_ctxt_alloc(struct svcxprt_rdma *rdma)
118 {
119 struct ib_device *device = rdma->sc_cm_id->device;
120 int node = ibdev_to_node(device);
121 struct svc_rdma_send_ctxt *ctxt;
122 unsigned long pages;
123 dma_addr_t addr;
124 void *buffer;
125 int i;
126
127 ctxt = kzalloc_node(struct_size(ctxt, sc_sges, rdma->sc_max_send_sges),
128 GFP_KERNEL, node);
129 if (!ctxt)
130 goto fail0;
131 pages = svc_serv_maxpages(rdma->sc_xprt.xpt_server);
132 ctxt->sc_pages = kcalloc_node(pages, sizeof(struct page *),
133 GFP_KERNEL, node);
134 if (!ctxt->sc_pages)
135 goto fail1;
136 ctxt->sc_maxpages = pages;
137 buffer = kmalloc_node(rdma->sc_max_req_size, GFP_KERNEL, node);
138 if (!buffer)
139 goto fail2;
140 addr = ib_dma_map_single(device, buffer, rdma->sc_max_req_size,
141 DMA_TO_DEVICE);
142 if (ib_dma_mapping_error(device, addr))
143 goto fail3;
144
145 svc_rdma_send_cid_init(rdma, &ctxt->sc_cid);
146
147 ctxt->sc_rdma = rdma;
148 ctxt->sc_send_wr.next = NULL;
149 ctxt->sc_send_wr.wr_cqe = &ctxt->sc_cqe;
150 ctxt->sc_send_wr.sg_list = ctxt->sc_sges;
151 ctxt->sc_send_wr.send_flags = IB_SEND_SIGNALED;
152 ctxt->sc_cqe.done = svc_rdma_wc_send;
153 INIT_LIST_HEAD(&ctxt->sc_write_info_list);
154 ctxt->sc_xprt_buf = buffer;
155 xdr_buf_init(&ctxt->sc_hdrbuf, ctxt->sc_xprt_buf,
156 rdma->sc_max_req_size);
157 ctxt->sc_sges[0].addr = addr;
158
159 for (i = 0; i < rdma->sc_max_send_sges; i++)
160 ctxt->sc_sges[i].lkey = rdma->sc_pd->local_dma_lkey;
161 return ctxt;
162
163 fail3:
164 kfree(buffer);
165 fail2:
166 kfree(ctxt->sc_pages);
167 fail1:
168 kfree(ctxt);
169 fail0:
170 return NULL;
171 }
172
173 /**
174 * svc_rdma_send_ctxts_destroy - Release all send_ctxt's for an xprt
175 * @rdma: svcxprt_rdma being torn down
176 *
177 */
svc_rdma_send_ctxts_destroy(struct svcxprt_rdma * rdma)178 void svc_rdma_send_ctxts_destroy(struct svcxprt_rdma *rdma)
179 {
180 struct ib_device *device = rdma->sc_cm_id->device;
181 struct svc_rdma_send_ctxt *ctxt;
182 struct llist_node *node;
183
184 while ((node = llist_del_first(&rdma->sc_send_ctxts)) != NULL) {
185 ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
186 ib_dma_unmap_single(device, ctxt->sc_sges[0].addr,
187 rdma->sc_max_req_size, DMA_TO_DEVICE);
188 kfree(ctxt->sc_xprt_buf);
189 kfree(ctxt->sc_pages);
190 kfree(ctxt);
191 }
192 }
193
194 /**
195 * svc_rdma_send_ctxt_get - Get a free send_ctxt
196 * @rdma: controlling svcxprt_rdma
197 *
198 * Returns a ready-to-use send_ctxt, or NULL if none are
199 * available and a fresh one cannot be allocated.
200 */
svc_rdma_send_ctxt_get(struct svcxprt_rdma * rdma)201 struct svc_rdma_send_ctxt *svc_rdma_send_ctxt_get(struct svcxprt_rdma *rdma)
202 {
203 struct svc_rdma_send_ctxt *ctxt;
204 struct llist_node *node;
205
206 spin_lock(&rdma->sc_send_lock);
207 node = llist_del_first(&rdma->sc_send_ctxts);
208 spin_unlock(&rdma->sc_send_lock);
209 if (!node)
210 goto out_empty;
211
212 ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
213
214 out:
215 rpcrdma_set_xdrlen(&ctxt->sc_hdrbuf, 0);
216 xdr_init_encode(&ctxt->sc_stream, &ctxt->sc_hdrbuf,
217 ctxt->sc_xprt_buf, NULL);
218
219 svc_rdma_cc_init(rdma, &ctxt->sc_reply_info.wi_cc);
220 ctxt->sc_send_wr.num_sge = 0;
221 ctxt->sc_cur_sge_no = 0;
222 ctxt->sc_page_count = 0;
223 ctxt->sc_wr_chain = &ctxt->sc_send_wr;
224 ctxt->sc_sqecount = 1;
225
226 return ctxt;
227
228 out_empty:
229 svc_rdma_send_ctxts_drain(rdma);
230
231 spin_lock(&rdma->sc_send_lock);
232 node = llist_del_first(&rdma->sc_send_ctxts);
233 spin_unlock(&rdma->sc_send_lock);
234 if (node) {
235 ctxt = llist_entry(node, struct svc_rdma_send_ctxt, sc_node);
236 goto out;
237 }
238
239 ctxt = svc_rdma_send_ctxt_alloc(rdma);
240 if (!ctxt)
241 return NULL;
242 goto out;
243 }
244
245 /* Release chunk I/O resources and DMA-unmap SGEs. */
svc_rdma_send_ctxt_unmap(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * ctxt)246 static void svc_rdma_send_ctxt_unmap(struct svcxprt_rdma *rdma,
247 struct svc_rdma_send_ctxt *ctxt)
248 {
249 struct ib_device *device = rdma->sc_cm_id->device;
250 unsigned int i;
251
252 svc_rdma_write_chunk_release(rdma, ctxt);
253 svc_rdma_reply_chunk_release(rdma, ctxt);
254
255 /* The first SGE contains the transport header, which
256 * remains mapped until @ctxt is destroyed.
257 */
258 for (i = 1; i < ctxt->sc_send_wr.num_sge; i++) {
259 trace_svcrdma_dma_unmap_page(&ctxt->sc_cid,
260 ctxt->sc_sges[i].addr,
261 ctxt->sc_sges[i].length);
262 ib_dma_unmap_page(device,
263 ctxt->sc_sges[i].addr,
264 ctxt->sc_sges[i].length,
265 DMA_TO_DEVICE);
266 }
267 }
268
269 /* Unmap, release pages, and return send_ctxt to the free list. */
svc_rdma_send_ctxt_release(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * ctxt)270 static void svc_rdma_send_ctxt_release(struct svcxprt_rdma *rdma,
271 struct svc_rdma_send_ctxt *ctxt)
272 {
273 svc_rdma_send_ctxt_unmap(rdma, ctxt);
274
275 if (ctxt->sc_page_count)
276 release_pages(ctxt->sc_pages, ctxt->sc_page_count);
277
278 llist_add(&ctxt->sc_node, &rdma->sc_send_ctxts);
279 }
280
281 /**
282 * svc_rdma_send_ctxts_drain - Release completed send_ctxts
283 * @rdma: controlling svcxprt_rdma
284 */
svc_rdma_send_ctxts_drain(struct svcxprt_rdma * rdma)285 void svc_rdma_send_ctxts_drain(struct svcxprt_rdma *rdma)
286 {
287 struct svc_rdma_send_ctxt *ctxt, *next;
288 struct llist_node *node;
289
290 node = llist_del_all(&rdma->sc_send_release_list);
291 llist_for_each_entry_safe(ctxt, next, node, sc_node)
292 svc_rdma_send_ctxt_release(rdma, ctxt);
293 }
294
295 /**
296 * svc_rdma_send_ctxt_put - Queue send_ctxt for deferred release
297 * @rdma: controlling svcxprt_rdma
298 * @ctxt: send_ctxt to queue for deferred release
299 *
300 * Queues @ctxt onto sc_send_release_list. DMA unmap and
301 * page release run later in svc_rdma_send_ctxts_drain(),
302 * typically from xpo_release_ctxt.
303 *
304 * On the empty-to-non-empty transition, set XPT_DATA and
305 * enqueue the transport. Without this self-trigger, a Send
306 * completion arriving after the last xpo_release_ctxt on an
307 * idle connection would leave the send_ctxt's DMA mappings
308 * and reply pages pinned until another drain occurred.
309 */
svc_rdma_send_ctxt_put(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * ctxt)310 void svc_rdma_send_ctxt_put(struct svcxprt_rdma *rdma,
311 struct svc_rdma_send_ctxt *ctxt)
312 {
313 if (llist_add(&ctxt->sc_node, &rdma->sc_send_release_list)) {
314 set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
315 svc_xprt_enqueue(&rdma->sc_xprt);
316 }
317 }
318
319 /**
320 * svc_rdma_wake_send_waiters - manage Send Queue accounting
321 * @rdma: controlling transport
322 * @avail: Number of additional SQEs that are now available
323 *
324 */
svc_rdma_wake_send_waiters(struct svcxprt_rdma * rdma,int avail)325 void svc_rdma_wake_send_waiters(struct svcxprt_rdma *rdma, int avail)
326 {
327 atomic_add(avail, &rdma->sc_sq_avail);
328 smp_mb__after_atomic();
329 if (unlikely(waitqueue_active(&rdma->sc_send_wait)))
330 wake_up(&rdma->sc_send_wait);
331 }
332
333 /**
334 * svc_rdma_sq_wait - Wait for SQ slots using fair queuing
335 * @rdma: controlling transport
336 * @cid: completion ID for tracing
337 * @sqecount: number of SQ entries needed
338 *
339 * A ticket-based system ensures fair ordering when multiple threads
340 * wait for Send Queue capacity. Each waiter takes a ticket and is
341 * served in order, preventing starvation.
342 *
343 * Protocol invariant: every ticket holder must increment
344 * sc_sq_ticket_tail exactly once, whether the reservation
345 * succeeds or the connection closes. Failing to advance the
346 * tail stalls all subsequent waiters.
347 *
348 * The ticket counters are signed 32-bit atomics. After
349 * wrapping through INT_MAX, the equality check
350 * (tail == ticket) remains correct because both counters
351 * advance monotonically and the comparison uses exact
352 * equality rather than relational operators.
353 *
354 * Return values:
355 * %0: SQ slots were reserved successfully
356 * %-ENOTCONN: The connection was lost
357 */
svc_rdma_sq_wait(struct svcxprt_rdma * rdma,const struct rpc_rdma_cid * cid,int sqecount)358 int svc_rdma_sq_wait(struct svcxprt_rdma *rdma,
359 const struct rpc_rdma_cid *cid, int sqecount)
360 {
361 int ticket;
362
363 /* Fast path: try to reserve SQ slots without waiting.
364 *
365 * A failed reservation temporarily understates sc_sq_avail
366 * until the compensating atomic_add restores it. A Send
367 * completion arriving in that window sees a lower count
368 * than reality, but the value self-corrects once the add
369 * completes. No ordering guarantee is needed here because
370 * the slow path serializes all contended waiters.
371 */
372 if (likely(atomic_sub_return(sqecount, &rdma->sc_sq_avail) >= 0))
373 return 0;
374 atomic_add(sqecount, &rdma->sc_sq_avail);
375
376 /* Slow path: take a ticket and wait in line */
377 ticket = atomic_fetch_inc(&rdma->sc_sq_ticket_head);
378
379 percpu_counter_inc(&svcrdma_stat_sq_starve);
380 trace_svcrdma_sq_full(rdma, cid);
381
382 /* Wait until all earlier tickets have been served */
383 wait_event(rdma->sc_sq_ticket_wait,
384 test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags) ||
385 atomic_read(&rdma->sc_sq_ticket_tail) == ticket);
386 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
387 goto out_close;
388
389 /* It's our turn. Wait for enough SQ slots to be available. */
390 while (atomic_sub_return(sqecount, &rdma->sc_sq_avail) < 0) {
391 atomic_add(sqecount, &rdma->sc_sq_avail);
392
393 wait_event(rdma->sc_send_wait,
394 test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags) ||
395 atomic_read(&rdma->sc_sq_avail) >= sqecount);
396 if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
397 goto out_close;
398 }
399
400 /* Slots reserved successfully. Let the next waiter proceed. */
401 atomic_inc(&rdma->sc_sq_ticket_tail);
402 wake_up(&rdma->sc_sq_ticket_wait);
403 trace_svcrdma_sq_retry(rdma, cid);
404
405 /*
406 * While this thread sat on sc_send_wait or sc_sq_ticket_wait,
407 * Send completions that tried to enqueue this transport for a
408 * release-list drain were rejected: svc_rdma_has_wspace returns
409 * 0 while either waitqueue is active, and svc_xprt_ready
410 * rejects the enqueue. Drain the release list now.
411 */
412 svc_rdma_send_ctxts_drain(rdma);
413 return 0;
414
415 out_close:
416 atomic_inc(&rdma->sc_sq_ticket_tail);
417 wake_up(&rdma->sc_sq_ticket_wait);
418 return -ENOTCONN;
419 }
420
421 /**
422 * svc_rdma_post_send_err - Handle ib_post_send failure
423 * @rdma: controlling transport
424 * @cid: completion ID for tracing
425 * @bad_wr: first WR that was not posted
426 * @first_wr: first WR in the chain
427 * @sqecount: number of SQ entries that were reserved
428 * @ret: error code from ib_post_send
429 *
430 * Return values:
431 * %0: At least one WR was posted; a completion handles cleanup
432 * %-ENOTCONN: No WRs were posted; SQ slots are released
433 */
svc_rdma_post_send_err(struct svcxprt_rdma * rdma,const struct rpc_rdma_cid * cid,const struct ib_send_wr * bad_wr,const struct ib_send_wr * first_wr,int sqecount,int ret)434 int svc_rdma_post_send_err(struct svcxprt_rdma *rdma,
435 const struct rpc_rdma_cid *cid,
436 const struct ib_send_wr *bad_wr,
437 const struct ib_send_wr *first_wr,
438 int sqecount, int ret)
439 {
440 trace_svcrdma_sq_post_err(rdma, cid, ret);
441 svc_rdma_xprt_deferred_close(rdma);
442
443 /* If even one WR was posted, a Send completion will
444 * return the reserved SQ slots.
445 */
446 if (bad_wr != first_wr)
447 return 0;
448
449 svc_rdma_wake_send_waiters(rdma, sqecount);
450 return -ENOTCONN;
451 }
452
453 /**
454 * svc_rdma_wc_send - Invoked by RDMA provider for each polled Send WC
455 * @cq: Completion Queue context
456 * @wc: Work Completion object
457 *
458 * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
459 * the Send completion handler could be running.
460 */
svc_rdma_wc_send(struct ib_cq * cq,struct ib_wc * wc)461 static void svc_rdma_wc_send(struct ib_cq *cq, struct ib_wc *wc)
462 {
463 struct svcxprt_rdma *rdma = cq->cq_context;
464 struct ib_cqe *cqe = wc->wr_cqe;
465 struct svc_rdma_send_ctxt *ctxt =
466 container_of(cqe, struct svc_rdma_send_ctxt, sc_cqe);
467
468 svc_rdma_wake_send_waiters(rdma, ctxt->sc_sqecount);
469
470 if (unlikely(wc->status != IB_WC_SUCCESS))
471 goto flushed;
472
473 trace_svcrdma_wc_send(&ctxt->sc_cid);
474 svc_rdma_send_ctxt_put(rdma, ctxt);
475 return;
476
477 flushed:
478 if (wc->status != IB_WC_WR_FLUSH_ERR)
479 trace_svcrdma_wc_send_err(wc, &ctxt->sc_cid);
480 else
481 trace_svcrdma_wc_send_flush(wc, &ctxt->sc_cid);
482 svc_rdma_send_ctxt_put(rdma, ctxt);
483 svc_rdma_xprt_deferred_close(rdma);
484 }
485
486 /**
487 * svc_rdma_post_send - Post a WR chain to the Send Queue
488 * @rdma: transport context
489 * @ctxt: WR chain to post
490 *
491 * Copy fields in @ctxt to stack variables in order to guarantee
492 * that these values remain available after the ib_post_send() call.
493 * In some error flow cases, svc_rdma_wc_send() releases @ctxt.
494 *
495 * Return values:
496 * %0: @ctxt's WR chain was posted successfully
497 * %-ENOTCONN: The connection was lost
498 */
svc_rdma_post_send(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * ctxt)499 int svc_rdma_post_send(struct svcxprt_rdma *rdma,
500 struct svc_rdma_send_ctxt *ctxt)
501 {
502 struct ib_send_wr *first_wr = ctxt->sc_wr_chain;
503 struct ib_send_wr *send_wr = &ctxt->sc_send_wr;
504 const struct ib_send_wr *bad_wr = first_wr;
505 struct rpc_rdma_cid cid = ctxt->sc_cid;
506 int ret, sqecount = ctxt->sc_sqecount;
507
508 might_sleep();
509
510 /* Sync the transport header buffer */
511 ib_dma_sync_single_for_device(rdma->sc_cm_id->device,
512 send_wr->sg_list[0].addr,
513 send_wr->sg_list[0].length,
514 DMA_TO_DEVICE);
515
516 ret = svc_rdma_sq_wait(rdma, &cid, sqecount);
517 if (ret < 0)
518 return ret;
519
520 trace_svcrdma_post_send(ctxt);
521 ret = ib_post_send(rdma->sc_qp, first_wr, &bad_wr);
522 if (ret)
523 return svc_rdma_post_send_err(rdma, &cid, bad_wr,
524 first_wr, sqecount, ret);
525 return 0;
526 }
527
528 /**
529 * svc_rdma_encode_read_list - Encode RPC Reply's Read chunk list
530 * @sctxt: Send context for the RPC Reply
531 *
532 * Return values:
533 * On success, returns length in bytes of the Reply XDR buffer
534 * that was consumed by the Reply Read list
535 * %-EMSGSIZE on XDR buffer overflow
536 */
svc_rdma_encode_read_list(struct svc_rdma_send_ctxt * sctxt)537 static ssize_t svc_rdma_encode_read_list(struct svc_rdma_send_ctxt *sctxt)
538 {
539 /* RPC-over-RDMA version 1 replies never have a Read list. */
540 return xdr_stream_encode_item_absent(&sctxt->sc_stream);
541 }
542
543 /**
544 * svc_rdma_encode_write_segment - Encode one Write segment
545 * @sctxt: Send context for the RPC Reply
546 * @chunk: Write chunk to push
547 * @remaining: remaining bytes of the payload left in the Write chunk
548 * @segno: which segment in the chunk
549 *
550 * Return values:
551 * On success, returns length in bytes of the Reply XDR buffer
552 * that was consumed by the Write segment, and updates @remaining
553 * %-EMSGSIZE on XDR buffer overflow
554 */
svc_rdma_encode_write_segment(struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_chunk * chunk,u32 * remaining,unsigned int segno)555 static ssize_t svc_rdma_encode_write_segment(struct svc_rdma_send_ctxt *sctxt,
556 const struct svc_rdma_chunk *chunk,
557 u32 *remaining, unsigned int segno)
558 {
559 const struct svc_rdma_segment *segment = &chunk->ch_segments[segno];
560 const size_t len = rpcrdma_segment_maxsz * sizeof(__be32);
561 u32 length;
562 __be32 *p;
563
564 p = xdr_reserve_space(&sctxt->sc_stream, len);
565 if (!p)
566 return -EMSGSIZE;
567
568 length = min_t(u32, *remaining, segment->rs_length);
569 *remaining -= length;
570 xdr_encode_rdma_segment(p, segment->rs_handle, length,
571 segment->rs_offset);
572 trace_svcrdma_encode_wseg(sctxt, segno, segment->rs_handle, length,
573 segment->rs_offset);
574 return len;
575 }
576
577 /**
578 * svc_rdma_encode_write_chunk - Encode one Write chunk
579 * @sctxt: Send context for the RPC Reply
580 * @chunk: Write chunk to push
581 *
582 * Copy a Write chunk from the Call transport header to the
583 * Reply transport header. Update each segment's length field
584 * to reflect the number of bytes written in that segment.
585 *
586 * Return values:
587 * On success, returns length in bytes of the Reply XDR buffer
588 * that was consumed by the Write chunk
589 * %-EMSGSIZE on XDR buffer overflow
590 */
svc_rdma_encode_write_chunk(struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_chunk * chunk)591 static ssize_t svc_rdma_encode_write_chunk(struct svc_rdma_send_ctxt *sctxt,
592 const struct svc_rdma_chunk *chunk)
593 {
594 u32 remaining = chunk->ch_payload_length;
595 unsigned int segno;
596 ssize_t len, ret;
597
598 len = 0;
599 ret = xdr_stream_encode_item_present(&sctxt->sc_stream);
600 if (ret < 0)
601 return ret;
602 len += ret;
603
604 ret = xdr_stream_encode_u32(&sctxt->sc_stream, chunk->ch_segcount);
605 if (ret < 0)
606 return ret;
607 len += ret;
608
609 for (segno = 0; segno < chunk->ch_segcount; segno++) {
610 ret = svc_rdma_encode_write_segment(sctxt, chunk, &remaining, segno);
611 if (ret < 0)
612 return ret;
613 len += ret;
614 }
615
616 return len;
617 }
618
619 /**
620 * svc_rdma_encode_write_list - Encode RPC Reply's Write chunk list
621 * @rctxt: Reply context with information about the RPC Call
622 * @sctxt: Send context for the RPC Reply
623 *
624 * Return values:
625 * On success, returns length in bytes of the Reply XDR buffer
626 * that was consumed by the Reply's Write list
627 * %-EMSGSIZE on XDR buffer overflow
628 */
svc_rdma_encode_write_list(struct svc_rdma_recv_ctxt * rctxt,struct svc_rdma_send_ctxt * sctxt)629 static ssize_t svc_rdma_encode_write_list(struct svc_rdma_recv_ctxt *rctxt,
630 struct svc_rdma_send_ctxt *sctxt)
631 {
632 struct svc_rdma_chunk *chunk;
633 ssize_t len, ret;
634
635 len = 0;
636 pcl_for_each_chunk(chunk, &rctxt->rc_write_pcl) {
637 ret = svc_rdma_encode_write_chunk(sctxt, chunk);
638 if (ret < 0)
639 return ret;
640 len += ret;
641 }
642
643 /* Terminate the Write list */
644 ret = xdr_stream_encode_item_absent(&sctxt->sc_stream);
645 if (ret < 0)
646 return ret;
647
648 return len + ret;
649 }
650
651 /**
652 * svc_rdma_encode_reply_chunk - Encode RPC Reply's Reply chunk
653 * @rctxt: Reply context with information about the RPC Call
654 * @sctxt: Send context for the RPC Reply
655 * @length: size in bytes of the payload in the Reply chunk
656 *
657 * Return values:
658 * On success, returns length in bytes of the Reply XDR buffer
659 * that was consumed by the Reply's Reply chunk
660 * %-EMSGSIZE on XDR buffer overflow
661 * %-E2BIG if the RPC message is larger than the Reply chunk
662 */
663 static ssize_t
svc_rdma_encode_reply_chunk(struct svc_rdma_recv_ctxt * rctxt,struct svc_rdma_send_ctxt * sctxt,unsigned int length)664 svc_rdma_encode_reply_chunk(struct svc_rdma_recv_ctxt *rctxt,
665 struct svc_rdma_send_ctxt *sctxt,
666 unsigned int length)
667 {
668 struct svc_rdma_chunk *chunk;
669
670 if (pcl_is_empty(&rctxt->rc_reply_pcl))
671 return xdr_stream_encode_item_absent(&sctxt->sc_stream);
672
673 chunk = pcl_first_chunk(&rctxt->rc_reply_pcl);
674 if (length > chunk->ch_length)
675 return -E2BIG;
676
677 chunk->ch_payload_length = length;
678 return svc_rdma_encode_write_chunk(sctxt, chunk);
679 }
680
681 struct svc_rdma_map_data {
682 struct svcxprt_rdma *md_rdma;
683 struct svc_rdma_send_ctxt *md_ctxt;
684 };
685
686 /**
687 * svc_rdma_page_dma_map - DMA map one page
688 * @data: pointer to arguments
689 * @page: struct page to DMA map
690 * @offset: offset into the page
691 * @len: number of bytes to map
692 *
693 * Returns:
694 * %0 if DMA mapping was successful
695 * %-EIO if the page cannot be DMA mapped
696 */
svc_rdma_page_dma_map(void * data,struct page * page,unsigned long offset,unsigned int len)697 static int svc_rdma_page_dma_map(void *data, struct page *page,
698 unsigned long offset, unsigned int len)
699 {
700 struct svc_rdma_map_data *args = data;
701 struct svcxprt_rdma *rdma = args->md_rdma;
702 struct svc_rdma_send_ctxt *ctxt = args->md_ctxt;
703 struct ib_device *dev = rdma->sc_cm_id->device;
704 dma_addr_t dma_addr;
705
706 ++ctxt->sc_cur_sge_no;
707
708 dma_addr = ib_dma_map_page(dev, page, offset, len, DMA_TO_DEVICE);
709 if (ib_dma_mapping_error(dev, dma_addr))
710 goto out_maperr;
711
712 trace_svcrdma_dma_map_page(&ctxt->sc_cid, dma_addr, len);
713 ctxt->sc_sges[ctxt->sc_cur_sge_no].addr = dma_addr;
714 ctxt->sc_sges[ctxt->sc_cur_sge_no].length = len;
715 ctxt->sc_send_wr.num_sge++;
716 return 0;
717
718 out_maperr:
719 trace_svcrdma_dma_map_err(&ctxt->sc_cid, dma_addr, len);
720 return -EIO;
721 }
722
723 /**
724 * svc_rdma_iov_dma_map - DMA map an iovec
725 * @data: pointer to arguments
726 * @iov: kvec to DMA map
727 *
728 * ib_dma_map_page() is used here because svc_rdma_dma_unmap()
729 * handles DMA-unmap and it uses ib_dma_unmap_page() exclusively.
730 *
731 * Returns:
732 * %0 if DMA mapping was successful
733 * %-EIO if the iovec cannot be DMA mapped
734 */
svc_rdma_iov_dma_map(void * data,const struct kvec * iov)735 static int svc_rdma_iov_dma_map(void *data, const struct kvec *iov)
736 {
737 if (!iov->iov_len)
738 return 0;
739 return svc_rdma_page_dma_map(data, virt_to_page(iov->iov_base),
740 offset_in_page(iov->iov_base),
741 iov->iov_len);
742 }
743
744 /**
745 * svc_rdma_xb_dma_map - DMA map all segments of an xdr_buf
746 * @xdr: xdr_buf containing portion of an RPC message to transmit
747 * @data: pointer to arguments
748 *
749 * Returns:
750 * %0 if DMA mapping was successful
751 * %-EIO if DMA mapping failed
752 *
753 * On failure, any DMA mappings that have been already done must be
754 * unmapped by the caller.
755 */
svc_rdma_xb_dma_map(const struct xdr_buf * xdr,void * data)756 static int svc_rdma_xb_dma_map(const struct xdr_buf *xdr, void *data)
757 {
758 unsigned int len, remaining;
759 unsigned long pageoff;
760 struct page **ppages;
761 int ret;
762
763 ret = svc_rdma_iov_dma_map(data, &xdr->head[0]);
764 if (ret < 0)
765 return ret;
766
767 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
768 pageoff = offset_in_page(xdr->page_base);
769 remaining = xdr->page_len;
770 while (remaining) {
771 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
772
773 ret = svc_rdma_page_dma_map(data, *ppages++, pageoff, len);
774 if (ret < 0)
775 return ret;
776
777 remaining -= len;
778 pageoff = 0;
779 }
780
781 ret = svc_rdma_iov_dma_map(data, &xdr->tail[0]);
782 if (ret < 0)
783 return ret;
784
785 return xdr->len;
786 }
787
788 struct svc_rdma_pullup_data {
789 u8 *pd_dest;
790 unsigned int pd_length;
791 unsigned int pd_num_sges;
792 };
793
794 /**
795 * svc_rdma_xb_count_sges - Count how many SGEs will be needed
796 * @xdr: xdr_buf containing portion of an RPC message to transmit
797 * @data: pointer to arguments
798 *
799 * Returns:
800 * Number of SGEs needed to Send the contents of @xdr inline
801 */
svc_rdma_xb_count_sges(const struct xdr_buf * xdr,void * data)802 static int svc_rdma_xb_count_sges(const struct xdr_buf *xdr,
803 void *data)
804 {
805 struct svc_rdma_pullup_data *args = data;
806 unsigned int remaining;
807 unsigned long offset;
808
809 if (xdr->head[0].iov_len)
810 ++args->pd_num_sges;
811
812 offset = offset_in_page(xdr->page_base);
813 remaining = xdr->page_len;
814 while (remaining) {
815 ++args->pd_num_sges;
816 remaining -= min_t(u32, PAGE_SIZE - offset, remaining);
817 offset = 0;
818 }
819
820 if (xdr->tail[0].iov_len)
821 ++args->pd_num_sges;
822
823 args->pd_length += xdr->len;
824 return 0;
825 }
826
827 /**
828 * svc_rdma_check_pull_up - Determine whether to use pull-up
829 * @rdma: controlling transport
830 * @sctxt: send_ctxt for the Send WR
831 * @write_pcl: Write chunk list provided by client
832 * @xdr: xdr_buf containing RPC message to transmit
833 *
834 * Returns:
835 * %1 if pull-up must be used
836 * %0 if pull-up is not needed
837 * %-E2BIG if the reply is too large to be pulled up
838 */
svc_rdma_check_pull_up(const struct svcxprt_rdma * rdma,const struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_pcl * write_pcl,const struct xdr_buf * xdr)839 static int svc_rdma_check_pull_up(const struct svcxprt_rdma *rdma,
840 const struct svc_rdma_send_ctxt *sctxt,
841 const struct svc_rdma_pcl *write_pcl,
842 const struct xdr_buf *xdr)
843 {
844 /* Resources needed for the transport header */
845 struct svc_rdma_pullup_data args = {
846 .pd_length = sctxt->sc_hdrbuf.len,
847 .pd_num_sges = 1,
848 };
849 int ret;
850
851 ret = pcl_process_nonpayloads(write_pcl, xdr,
852 svc_rdma_xb_count_sges, &args);
853 if (ret < 0)
854 return 0;
855
856 if (args.pd_length < RPCRDMA_PULLUP_THRESH)
857 return 1;
858 if (args.pd_num_sges < rdma->sc_max_send_sges)
859 return 0;
860
861 /*
862 * The reply has too many SGEs to Send inline, so it has to be
863 * linearized into sc_xprt_buf. That buffer holds only
864 * sc_max_req_size bytes, so a larger reply cannot be pulled up.
865 * RFC 8166 Section 4.5.3 requires responding with ERR_CHUNK.
866 */
867 if (args.pd_length > rdma->sc_max_req_size)
868 return -E2BIG;
869 return 1;
870 }
871
872 /**
873 * svc_rdma_xb_linearize - Copy region of xdr_buf to flat buffer
874 * @xdr: xdr_buf containing portion of an RPC message to copy
875 * @data: pointer to arguments
876 *
877 * Returns:
878 * Always zero.
879 */
svc_rdma_xb_linearize(const struct xdr_buf * xdr,void * data)880 static int svc_rdma_xb_linearize(const struct xdr_buf *xdr,
881 void *data)
882 {
883 struct svc_rdma_pullup_data *args = data;
884 unsigned int len, remaining;
885 unsigned long pageoff;
886 struct page **ppages;
887
888 if (xdr->head[0].iov_len) {
889 memcpy(args->pd_dest, xdr->head[0].iov_base, xdr->head[0].iov_len);
890 args->pd_dest += xdr->head[0].iov_len;
891 }
892
893 ppages = xdr->pages + (xdr->page_base >> PAGE_SHIFT);
894 pageoff = offset_in_page(xdr->page_base);
895 remaining = xdr->page_len;
896 while (remaining) {
897 len = min_t(u32, PAGE_SIZE - pageoff, remaining);
898 memcpy(args->pd_dest, page_address(*ppages) + pageoff, len);
899 remaining -= len;
900 args->pd_dest += len;
901 pageoff = 0;
902 ppages++;
903 }
904
905 if (xdr->tail[0].iov_len) {
906 memcpy(args->pd_dest, xdr->tail[0].iov_base, xdr->tail[0].iov_len);
907 args->pd_dest += xdr->tail[0].iov_len;
908 }
909
910 args->pd_length += xdr->len;
911 return 0;
912 }
913
914 /**
915 * svc_rdma_pull_up_reply_msg - Copy Reply into a single buffer
916 * @rdma: controlling transport
917 * @sctxt: send_ctxt for the Send WR; xprt hdr is already prepared
918 * @write_pcl: Write chunk list provided by client
919 * @xdr: prepared xdr_buf containing RPC message
920 *
921 * The device is not capable of sending the reply directly.
922 * Assemble the elements of @xdr into the transport header buffer.
923 *
924 * Assumptions:
925 * check_pull_up has determined that @xdr will fit in the buffer.
926 *
927 * Returns:
928 * %0 if pull-up was successful
929 * %-EMSGSIZE if a buffer manipulation problem occurred
930 */
svc_rdma_pull_up_reply_msg(const struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_pcl * write_pcl,const struct xdr_buf * xdr)931 static int svc_rdma_pull_up_reply_msg(const struct svcxprt_rdma *rdma,
932 struct svc_rdma_send_ctxt *sctxt,
933 const struct svc_rdma_pcl *write_pcl,
934 const struct xdr_buf *xdr)
935 {
936 struct svc_rdma_pullup_data args = {
937 .pd_dest = sctxt->sc_xprt_buf + sctxt->sc_hdrbuf.len,
938 };
939 int ret;
940
941 ret = pcl_process_nonpayloads(write_pcl, xdr,
942 svc_rdma_xb_linearize, &args);
943 if (ret < 0)
944 return ret;
945
946 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len + args.pd_length;
947 trace_svcrdma_send_pullup(sctxt, args.pd_length);
948 return 0;
949 }
950
951 /* svc_rdma_map_reply_msg - DMA map the buffer holding RPC message
952 * @rdma: controlling transport
953 * @sctxt: send_ctxt for the Send WR
954 * @write_pcl: Write chunk list provided by client
955 * @reply_pcl: Reply chunk provided by client
956 * @xdr: prepared xdr_buf containing RPC message
957 *
958 * Returns:
959 * %0 if DMA mapping was successful.
960 * %-E2BIG if the reply is too large to be pulled up
961 * %-EMSGSIZE if a buffer manipulation problem occurred
962 * %-EIO if DMA mapping failed
963 *
964 * The Send WR's num_sge field is set in all cases.
965 */
svc_rdma_map_reply_msg(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_pcl * write_pcl,const struct svc_rdma_pcl * reply_pcl,const struct xdr_buf * xdr)966 int svc_rdma_map_reply_msg(struct svcxprt_rdma *rdma,
967 struct svc_rdma_send_ctxt *sctxt,
968 const struct svc_rdma_pcl *write_pcl,
969 const struct svc_rdma_pcl *reply_pcl,
970 const struct xdr_buf *xdr)
971 {
972 struct svc_rdma_map_data args = {
973 .md_rdma = rdma,
974 .md_ctxt = sctxt,
975 };
976 int ret;
977
978 /* Set up the (persistently-mapped) transport header SGE. */
979 sctxt->sc_send_wr.num_sge = 1;
980 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
981
982 /* If there is a Reply chunk, nothing follows the transport
983 * header, so there is nothing to map.
984 */
985 if (!pcl_is_empty(reply_pcl))
986 return 0;
987
988 /* For pull-up, svc_rdma_send() will sync the transport header.
989 * No additional DMA mapping is necessary.
990 */
991 ret = svc_rdma_check_pull_up(rdma, sctxt, write_pcl, xdr);
992 if (ret < 0)
993 return ret;
994 if (ret)
995 return svc_rdma_pull_up_reply_msg(rdma, sctxt, write_pcl, xdr);
996
997 return pcl_process_nonpayloads(write_pcl, xdr,
998 svc_rdma_xb_dma_map, &args);
999 }
1000
1001 /* The svc_rqst and all resources it owns are released as soon as
1002 * svc_rdma_sendto returns. Transfer pages under I/O to the ctxt
1003 * so they are released only after Send completion, and not by
1004 * svc_rqst_release_pages().
1005 */
svc_rdma_save_io_pages(struct svc_rqst * rqstp,struct svc_rdma_send_ctxt * ctxt)1006 static void svc_rdma_save_io_pages(struct svc_rqst *rqstp,
1007 struct svc_rdma_send_ctxt *ctxt)
1008 {
1009 int i, pages = rqstp->rq_next_page - rqstp->rq_respages;
1010
1011 ctxt->sc_page_count += pages;
1012 for (i = 0; i < pages; i++) {
1013 ctxt->sc_pages[i] = rqstp->rq_respages[i];
1014 rqstp->rq_respages[i] = NULL;
1015 }
1016 }
1017
1018 /* Prepare the portion of the RPC Reply that will be transmitted
1019 * via RDMA Send. The RPC-over-RDMA transport header is prepared
1020 * in sc_sges[0], and the RPC xdr_buf is prepared in following sges.
1021 *
1022 * Depending on whether a Write list or Reply chunk is present,
1023 * the server may Send all, a portion of, or none of the xdr_buf.
1024 * In the latter case, only the transport header (sc_sges[0]) is
1025 * transmitted.
1026 *
1027 * Assumptions:
1028 * - The Reply's transport header will never be larger than a page.
1029 */
svc_rdma_send_reply_msg(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * sctxt,const struct svc_rdma_recv_ctxt * rctxt,struct svc_rqst * rqstp)1030 static int svc_rdma_send_reply_msg(struct svcxprt_rdma *rdma,
1031 struct svc_rdma_send_ctxt *sctxt,
1032 const struct svc_rdma_recv_ctxt *rctxt,
1033 struct svc_rqst *rqstp)
1034 {
1035 struct ib_send_wr *send_wr = &sctxt->sc_send_wr;
1036 int ret;
1037
1038 ret = svc_rdma_map_reply_msg(rdma, sctxt, &rctxt->rc_write_pcl,
1039 &rctxt->rc_reply_pcl, &rqstp->rq_res);
1040 if (ret < 0)
1041 return ret;
1042
1043 /* Transfer pages involved in RDMA Writes to the sctxt's
1044 * page array. Completion handling releases these pages.
1045 */
1046 svc_rdma_save_io_pages(rqstp, sctxt);
1047
1048 if (rctxt->rc_inv_rkey) {
1049 send_wr->opcode = IB_WR_SEND_WITH_INV;
1050 send_wr->ex.invalidate_rkey = rctxt->rc_inv_rkey;
1051 } else {
1052 send_wr->opcode = IB_WR_SEND;
1053 }
1054
1055 return svc_rdma_post_send(rdma, sctxt);
1056 }
1057
1058 /**
1059 * svc_rdma_send_error_msg - Send an RPC/RDMA v1 error response
1060 * @rdma: controlling transport context
1061 * @sctxt: Send context for the response
1062 * @rctxt: Receive context for incoming bad message
1063 * @status: negative errno indicating error that occurred
1064 *
1065 * Given the client-provided Read, Write, and Reply chunks, the
1066 * server was not able to parse the Call or form a complete Reply.
1067 * Return an RDMA_ERROR message so the client can retire the RPC
1068 * transaction.
1069 *
1070 * The caller does not have to release @sctxt. It is released by
1071 * Send completion, or by this function on error.
1072 */
svc_rdma_send_error_msg(struct svcxprt_rdma * rdma,struct svc_rdma_send_ctxt * sctxt,struct svc_rdma_recv_ctxt * rctxt,int status)1073 void svc_rdma_send_error_msg(struct svcxprt_rdma *rdma,
1074 struct svc_rdma_send_ctxt *sctxt,
1075 struct svc_rdma_recv_ctxt *rctxt,
1076 int status)
1077 {
1078 __be32 *rdma_argp = rctxt->rc_recv_buf;
1079 __be32 *p;
1080
1081 rpcrdma_set_xdrlen(&sctxt->sc_hdrbuf, 0);
1082 xdr_init_encode(&sctxt->sc_stream, &sctxt->sc_hdrbuf,
1083 sctxt->sc_xprt_buf, NULL);
1084
1085 p = xdr_reserve_space(&sctxt->sc_stream,
1086 rpcrdma_fixed_maxsz * sizeof(*p));
1087 if (!p)
1088 goto put_ctxt;
1089
1090 *p++ = *rdma_argp;
1091 *p++ = *(rdma_argp + 1);
1092 *p++ = rdma->sc_fc_credits;
1093 *p = rdma_error;
1094
1095 switch (status) {
1096 case -EPROTONOSUPPORT:
1097 p = xdr_reserve_space(&sctxt->sc_stream, 3 * sizeof(*p));
1098 if (!p)
1099 goto put_ctxt;
1100
1101 *p++ = err_vers;
1102 *p++ = rpcrdma_version;
1103 *p = rpcrdma_version;
1104 trace_svcrdma_err_vers(*rdma_argp);
1105 break;
1106 default:
1107 p = xdr_reserve_space(&sctxt->sc_stream, sizeof(*p));
1108 if (!p)
1109 goto put_ctxt;
1110
1111 *p = err_chunk;
1112 trace_svcrdma_err_chunk(*rdma_argp);
1113 }
1114
1115 /* Remote Invalidation is skipped for simplicity. */
1116 sctxt->sc_send_wr.num_sge = 1;
1117 sctxt->sc_send_wr.opcode = IB_WR_SEND;
1118 sctxt->sc_sges[0].length = sctxt->sc_hdrbuf.len;
1119
1120 /* Ensure only the error message is posted, not any previously
1121 * prepared Write chunk WRs.
1122 */
1123 sctxt->sc_wr_chain = &sctxt->sc_send_wr;
1124 sctxt->sc_sqecount = 1;
1125 if (svc_rdma_post_send(rdma, sctxt))
1126 goto put_ctxt;
1127 return;
1128
1129 put_ctxt:
1130 svc_rdma_send_ctxt_put(rdma, sctxt);
1131 }
1132
1133 /**
1134 * svc_rdma_sendto - Transmit an RPC reply
1135 * @rqstp: processed RPC request, reply XDR already in ::rq_res
1136 *
1137 * Any resources still associated with @rqstp are released upon return.
1138 * If no reply message was possible, the connection is closed.
1139 *
1140 * Returns:
1141 * %0 if an RPC reply has been successfully posted,
1142 * %-ENOMEM if a resource shortage occurred (connection is lost),
1143 * %-ENOTCONN if posting failed (connection is lost).
1144 */
svc_rdma_sendto(struct svc_rqst * rqstp)1145 int svc_rdma_sendto(struct svc_rqst *rqstp)
1146 {
1147 struct svc_xprt *xprt = rqstp->rq_xprt;
1148 struct svcxprt_rdma *rdma =
1149 container_of(xprt, struct svcxprt_rdma, sc_xprt);
1150 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
1151 __be32 *rdma_argp = rctxt->rc_recv_buf;
1152 struct svc_rdma_send_ctxt *sctxt;
1153 unsigned int rc_size;
1154 __be32 *p;
1155 int ret;
1156
1157 ret = -ENOTCONN;
1158 if (svc_xprt_is_dead(xprt))
1159 goto drop_connection;
1160
1161 ret = -ENOMEM;
1162 sctxt = svc_rdma_send_ctxt_get(rdma);
1163 if (!sctxt)
1164 goto drop_connection;
1165
1166 ret = -EMSGSIZE;
1167 p = xdr_reserve_space(&sctxt->sc_stream,
1168 rpcrdma_fixed_maxsz * sizeof(*p));
1169 if (!p)
1170 goto put_ctxt;
1171
1172 ret = svc_rdma_prepare_write_list(rdma, rctxt, sctxt, &rqstp->rq_res);
1173 if (ret < 0)
1174 goto put_ctxt;
1175
1176 rc_size = 0;
1177 if (!pcl_is_empty(&rctxt->rc_reply_pcl)) {
1178 ret = svc_rdma_prepare_reply_chunk(rdma, &rctxt->rc_write_pcl,
1179 &rctxt->rc_reply_pcl, sctxt,
1180 &rqstp->rq_res);
1181 if (ret < 0)
1182 goto send_err;
1183 rc_size = ret;
1184 }
1185
1186 *p++ = *rdma_argp;
1187 *p++ = *(rdma_argp + 1);
1188 *p++ = rdma->sc_fc_credits;
1189 *p = pcl_is_empty(&rctxt->rc_reply_pcl) ? rdma_msg : rdma_nomsg;
1190
1191 ret = svc_rdma_encode_read_list(sctxt);
1192 if (ret < 0)
1193 goto put_ctxt;
1194 ret = svc_rdma_encode_write_list(rctxt, sctxt);
1195 if (ret < 0)
1196 goto put_ctxt;
1197 ret = svc_rdma_encode_reply_chunk(rctxt, sctxt, rc_size);
1198 if (ret < 0)
1199 goto put_ctxt;
1200
1201 ret = svc_rdma_send_reply_msg(rdma, sctxt, rctxt, rqstp);
1202 if (ret < 0)
1203 goto send_err;
1204 return 0;
1205
1206 send_err:
1207 if (ret != -E2BIG && ret != -EINVAL)
1208 goto put_ctxt;
1209
1210 /* Send completion releases payload pages that were part
1211 * of previously posted RDMA Writes.
1212 */
1213 svc_rdma_save_io_pages(rqstp, sctxt);
1214 svc_rdma_send_error_msg(rdma, sctxt, rctxt, ret);
1215 return 0;
1216
1217 put_ctxt:
1218 svc_rdma_send_ctxt_put(rdma, sctxt);
1219 drop_connection:
1220 trace_svcrdma_send_err(rqstp, ret);
1221 svc_rdma_xprt_deferred_close(rdma);
1222 return -ENOTCONN;
1223 }
1224
1225 /**
1226 * svc_rdma_result_payload - special processing for a result payload
1227 * @rqstp: RPC transaction context
1228 * @offset: payload's byte offset in @rqstp->rq_res
1229 * @length: size of payload, in bytes
1230 *
1231 * Assign the passed-in result payload to the current Write chunk,
1232 * and advance to cur_result_payload to the next Write chunk, if
1233 * there is one.
1234 *
1235 * Return values:
1236 * %0 if successful or nothing needed to be done
1237 * %-E2BIG if the payload was larger than the Write chunk
1238 */
svc_rdma_result_payload(struct svc_rqst * rqstp,unsigned int offset,unsigned int length)1239 int svc_rdma_result_payload(struct svc_rqst *rqstp, unsigned int offset,
1240 unsigned int length)
1241 {
1242 struct svc_rdma_recv_ctxt *rctxt = rqstp->rq_xprt_ctxt;
1243 struct svc_rdma_chunk *chunk;
1244
1245 chunk = rctxt->rc_cur_result_payload;
1246 if (!length || !chunk)
1247 return 0;
1248 rctxt->rc_cur_result_payload =
1249 pcl_next_chunk(&rctxt->rc_write_pcl, chunk);
1250
1251 if (length > chunk->ch_length)
1252 return -E2BIG;
1253 chunk->ch_position = offset;
1254 chunk->ch_payload_length = length;
1255 return 0;
1256 }
1257