blob: 951ae20485f34cac173b09134034c9f17db2f1ab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chuck Levera0ce85f2015-03-30 14:34:21 -04002/*
Chuck Leverce5b3712017-12-14 20:57:47 -05003 * Copyright (c) 2015, 2017 Oracle. All rights reserved.
Chuck Levera0ce85f2015-03-30 14:34:21 -04004 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
5 */
6
7/* Lightweight memory registration using Fast Registration Work
Chuck Leverce5b3712017-12-14 20:57:47 -05008 * Requests (FRWR).
Chuck Levera0ce85f2015-03-30 14:34:21 -04009 *
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040010 * FRWR features ordered asynchronous registration and invalidation
11 * of arbitrarily-sized memory regions. This is the fastest and safest
Chuck Levera0ce85f2015-03-30 14:34:21 -040012 * but most complex memory registration mode.
13 */
14
Chuck Leverc14d86e2015-05-26 11:52:35 -040015/* Normal operation
16 *
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040017 * A Memory Region is prepared for RDMA Read or Write using a FAST_REG
Chuck Lever5f624122018-12-19 10:59:01 -050018 * Work Request (frwr_map). When the RDMA operation is finished, this
Chuck Leverc14d86e2015-05-26 11:52:35 -040019 * Memory Region is invalidated using a LOCAL_INV Work Request
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040020 * (frwr_unmap_async and frwr_unmap_sync).
Chuck Leverc14d86e2015-05-26 11:52:35 -040021 *
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040022 * Typically FAST_REG Work Requests are not signaled, and neither are
23 * RDMA Send Work Requests (with the exception of signaling occasionally
24 * to prevent provider work queue overflows). This greatly reduces HCA
Chuck Leverc14d86e2015-05-26 11:52:35 -040025 * interrupt workload.
Chuck Leverc14d86e2015-05-26 11:52:35 -040026 */
27
28/* Transport recovery
29 *
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040030 * frwr_map and frwr_unmap_* cannot run at the same time the transport
31 * connect worker is running. The connect worker holds the transport
32 * send lock, just as ->send_request does. This prevents frwr_map and
33 * the connect worker from running concurrently. When a connection is
34 * closed, the Receive completion queue is drained before the allowing
35 * the connect worker to get control. This prevents frwr_unmap and the
36 * connect worker from running concurrently.
Chuck Leverc14d86e2015-05-26 11:52:35 -040037 *
Chuck Lever2fb2a4d2019-08-19 18:37:52 -040038 * When the underlying transport disconnects, MRs that are in flight
Chuck Lever9d2da4f2019-10-09 13:07:48 -040039 * are flushed and are likely unusable. Thus all MRs are destroyed.
40 * New MRs are created on demand.
Chuck Leverc14d86e2015-05-26 11:52:35 -040041 */
42
Chuck Leverbd2abef2018-05-07 15:27:16 -040043#include <linux/sunrpc/svc_rdma.h>
Chuck Leverc8b920b2016-09-15 10:57:16 -040044
Chuck Levera0ce85f2015-03-30 14:34:21 -040045#include "xprt_rdma.h"
Chuck Leverb6e717cb2018-05-07 15:27:05 -040046#include <trace/events/rpcrdma.h>
Chuck Levera0ce85f2015-03-30 14:34:21 -040047
48#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
49# define RPCDBG_FACILITY RPCDBG_TRANS
50#endif
51
Chuck Leveref2be592020-11-09 14:40:14 -050052static void frwr_mr_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
Chuck Lever61da8862018-10-01 14:25:25 -040053{
Chuck Lever7a03aeb62020-11-09 14:40:19 -050054 if (mr->mr_device) {
Chuck Leverd379eaa2018-10-01 14:25:30 -040055 trace_xprtrdma_mr_unmap(mr);
Chuck Lever7a03aeb62020-11-09 14:40:19 -050056 ib_dma_unmap_sg(mr->mr_device, mr->mr_sg, mr->mr_nents,
57 mr->mr_dir);
58 mr->mr_device = NULL;
Chuck Lever61da8862018-10-01 14:25:25 -040059 }
Chuck Leveref2be592020-11-09 14:40:14 -050060}
61
Chuck Levere4b52ca2021-04-19 14:03:12 -040062/**
63 * frwr_mr_release - Destroy one MR
64 * @mr: MR allocated by frwr_mr_init
65 *
66 */
67void frwr_mr_release(struct rpcrdma_mr *mr)
Chuck Leveref2be592020-11-09 14:40:14 -050068{
Chuck Levere4b52ca2021-04-19 14:03:12 -040069 int rc;
Chuck Leveref2be592020-11-09 14:40:14 -050070
Chuck Levere4b52ca2021-04-19 14:03:12 -040071 frwr_mr_unmap(mr->mr_xprt, mr);
Chuck Leveref2be592020-11-09 14:40:14 -050072
Chuck Levere4b52ca2021-04-19 14:03:12 -040073 rc = ib_dereg_mr(mr->frwr.fr_mr);
74 if (rc)
75 trace_xprtrdma_frwr_dereg(mr, rc);
76 kfree(mr->mr_sg);
77 kfree(mr);
Chuck Lever61da8862018-10-01 14:25:25 -040078}
79
Chuck Leveref2be592020-11-09 14:40:14 -050080static void frwr_mr_put(struct rpcrdma_mr *mr)
81{
82 frwr_mr_unmap(mr->mr_xprt, mr);
83
84 /* The MR is returned to the req's MR free list instead
85 * of to the xprt's MR free list. No spinlock is needed.
86 */
87 rpcrdma_mr_push(mr, &mr->mr_req->rl_free_mrs);
88}
89
Chuck Lever40088f02019-06-19 10:33:04 -040090/* frwr_reset - Place MRs back on the free list
91 * @req: request to reset
92 *
93 * Used after a failed marshal. For FRWR, this means the MRs
94 * don't have to be fully released and recreated.
95 *
96 * NB: This is safe only as long as none of @req's MRs are
97 * involved with an ongoing asynchronous FAST_REG or LOCAL_INV
98 * Work Request.
99 */
100void frwr_reset(struct rpcrdma_req *req)
101{
Chuck Lever265a38d2019-08-19 18:44:04 -0400102 struct rpcrdma_mr *mr;
Chuck Lever40088f02019-06-19 10:33:04 -0400103
Chuck Lever265a38d2019-08-19 18:44:04 -0400104 while ((mr = rpcrdma_mr_pop(&req->rl_registered)))
Chuck Leveref2be592020-11-09 14:40:14 -0500105 frwr_mr_put(mr);
Chuck Lever40088f02019-06-19 10:33:04 -0400106}
107
Chuck Lever5f624122018-12-19 10:59:01 -0500108/**
Chuck Lever253a5162020-02-21 17:00:17 -0500109 * frwr_mr_init - Initialize one MR
110 * @r_xprt: controlling transport instance
Chuck Lever5f624122018-12-19 10:59:01 -0500111 * @mr: generic MR to prepare for FRWR
112 *
113 * Returns zero if successful. Otherwise a negative errno
114 * is returned.
115 */
Chuck Lever253a5162020-02-21 17:00:17 -0500116int frwr_mr_init(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr *mr)
Chuck Leverd48b1d22016-06-29 13:52:29 -0400117{
Chuck Levere28ce902020-02-21 17:01:05 -0500118 struct rpcrdma_ep *ep = r_xprt->rx_ep;
Chuck Lever93aa8e02020-02-21 17:00:54 -0500119 unsigned int depth = ep->re_max_fr_depth;
Chuck Leverf85adb12018-12-19 11:00:48 -0500120 struct scatterlist *sg;
121 struct ib_mr *frmr;
Chuck Leverd48b1d22016-06-29 13:52:29 -0400122 int rc;
123
Chuck Lever93aa8e02020-02-21 17:00:54 -0500124 frmr = ib_alloc_mr(ep->re_pd, ep->re_mrtype, depth);
Chuck Leverf85adb12018-12-19 11:00:48 -0500125 if (IS_ERR(frmr))
Chuck Leverd48b1d22016-06-29 13:52:29 -0400126 goto out_mr_err;
127
Julia Lawalled38c332020-09-20 13:26:20 +0200128 sg = kmalloc_array(depth, sizeof(*sg), GFP_NOFS);
Chuck Leverf85adb12018-12-19 11:00:48 -0500129 if (!sg)
Chuck Leverd48b1d22016-06-29 13:52:29 -0400130 goto out_list_err;
131
Chuck Lever253a5162020-02-21 17:00:17 -0500132 mr->mr_xprt = r_xprt;
Chuck Leverf85adb12018-12-19 11:00:48 -0500133 mr->frwr.fr_mr = frmr;
Chuck Lever7a03aeb62020-11-09 14:40:19 -0500134 mr->mr_device = NULL;
Chuck Lever054f1552018-05-01 11:37:14 -0400135 INIT_LIST_HEAD(&mr->mr_list);
Chuck Leverf85adb12018-12-19 11:00:48 -0500136 init_completion(&mr->frwr.fr_linv_done);
137
138 sg_init_table(sg, depth);
139 mr->mr_sg = sg;
Chuck Leverd48b1d22016-06-29 13:52:29 -0400140 return 0;
141
142out_mr_err:
Chuck Leverf85adb12018-12-19 11:00:48 -0500143 rc = PTR_ERR(frmr);
Chuck Lever53b2c1c2018-12-19 11:00:06 -0500144 trace_xprtrdma_frwr_alloc(mr, rc);
Chuck Leverd48b1d22016-06-29 13:52:29 -0400145 return rc;
146
147out_list_err:
Chuck Leverf85adb12018-12-19 11:00:48 -0500148 ib_dereg_mr(frmr);
149 return -ENOMEM;
Chuck Leverd48b1d22016-06-29 13:52:29 -0400150}
151
Chuck Lever5f624122018-12-19 10:59:01 -0500152/**
Chuck Lever25868e62020-01-03 11:56:48 -0500153 * frwr_query_device - Prepare a transport for use with FRWR
Chuck Lever93aa8e02020-02-21 17:00:54 -0500154 * @ep: endpoint to fill in
Chuck Lever25868e62020-01-03 11:56:48 -0500155 * @device: RDMA device to query
Chuck Lever5f624122018-12-19 10:59:01 -0500156 *
157 * On success, sets:
Chuck Lever93aa8e02020-02-21 17:00:54 -0500158 * ep->re_attr
159 * ep->re_max_requests
160 * ep->re_max_rdma_segs
161 * ep->re_max_fr_depth
162 * ep->re_mrtype
Chuck Lever5f624122018-12-19 10:59:01 -0500163 *
Chuck Lever25868e62020-01-03 11:56:48 -0500164 * Return values:
165 * On success, returns zero.
166 * %-EINVAL - the device does not support FRWR memory registration
167 * %-ENOMEM - the device is not sufficiently capable for NFS/RDMA
Chuck Lever914fcad2018-05-04 15:34:48 -0400168 */
Chuck Lever93aa8e02020-02-21 17:00:54 -0500169int frwr_query_device(struct rpcrdma_ep *ep, const struct ib_device *device)
Chuck Lever3968cb52015-03-30 14:35:26 -0400170{
Chuck Lever25868e62020-01-03 11:56:48 -0500171 const struct ib_device_attr *attrs = &device->attrs;
Chuck Lever914fcad2018-05-04 15:34:48 -0400172 int max_qp_wr, depth, delta;
Chuck Lever2e870362020-01-03 11:56:27 -0500173 unsigned int max_sge;
174
Chuck Lever25868e62020-01-03 11:56:48 -0500175 if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
176 attrs->max_fast_reg_page_list_len == 0) {
177 pr_err("rpcrdma: 'frwr' mode is not supported by device %s\n",
178 device->name);
179 return -EINVAL;
180 }
181
Chuck Lever2e870362020-01-03 11:56:27 -0500182 max_sge = min_t(unsigned int, attrs->max_send_sge,
183 RPCRDMA_MAX_SEND_SGES);
184 if (max_sge < RPCRDMA_MIN_SEND_SGES) {
185 pr_err("rpcrdma: HCA provides only %u send SGEs\n", max_sge);
186 return -ENOMEM;
187 }
Chuck Lever93aa8e02020-02-21 17:00:54 -0500188 ep->re_attr.cap.max_send_sge = max_sge;
189 ep->re_attr.cap.max_recv_sge = 1;
Chuck Lever3968cb52015-03-30 14:35:26 -0400190
Chuck Lever93aa8e02020-02-21 17:00:54 -0500191 ep->re_mrtype = IB_MR_TYPE_MEM_REG;
Chuck Lever5e9fc6a2016-11-29 10:52:24 -0500192 if (attrs->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
Chuck Lever93aa8e02020-02-21 17:00:54 -0500193 ep->re_mrtype = IB_MR_TYPE_SG_GAPS;
Chuck Lever5e9fc6a2016-11-29 10:52:24 -0500194
Chuck Levera7886842018-12-19 10:58:51 -0500195 /* Quirk: Some devices advertise a large max_fast_reg_page_list_len
196 * capability, but perform optimally when the MRs are not larger
197 * than a page.
198 */
Chuck Lever18d065a2020-01-03 11:56:43 -0500199 if (attrs->max_sge_rd > RPCRDMA_MAX_HDR_SEGS)
Chuck Lever93aa8e02020-02-21 17:00:54 -0500200 ep->re_max_fr_depth = attrs->max_sge_rd;
Chuck Levera7886842018-12-19 10:58:51 -0500201 else
Chuck Lever93aa8e02020-02-21 17:00:54 -0500202 ep->re_max_fr_depth = attrs->max_fast_reg_page_list_len;
203 if (ep->re_max_fr_depth > RPCRDMA_MAX_DATA_SEGS)
204 ep->re_max_fr_depth = RPCRDMA_MAX_DATA_SEGS;
Chuck Lever3968cb52015-03-30 14:35:26 -0400205
Chuck Leverce5b3712017-12-14 20:57:47 -0500206 /* Add room for frwr register and invalidate WRs.
207 * 1. FRWR reg WR for head
208 * 2. FRWR invalidate WR for head
209 * 3. N FRWR reg WRs for pagelist
210 * 4. N FRWR invalidate WRs for pagelist
211 * 5. FRWR reg WR for tail
212 * 6. FRWR invalidate WR for tail
Chuck Lever3968cb52015-03-30 14:35:26 -0400213 * 7. The RDMA_SEND WR
214 */
215 depth = 7;
216
Chuck Leverce5b3712017-12-14 20:57:47 -0500217 /* Calculate N if the device max FRWR depth is smaller than
Chuck Lever3968cb52015-03-30 14:35:26 -0400218 * RPCRDMA_MAX_DATA_SEGS.
219 */
Chuck Lever93aa8e02020-02-21 17:00:54 -0500220 if (ep->re_max_fr_depth < RPCRDMA_MAX_DATA_SEGS) {
221 delta = RPCRDMA_MAX_DATA_SEGS - ep->re_max_fr_depth;
Chuck Lever3968cb52015-03-30 14:35:26 -0400222 do {
Chuck Leverce5b3712017-12-14 20:57:47 -0500223 depth += 2; /* FRWR reg + invalidate */
Chuck Lever93aa8e02020-02-21 17:00:54 -0500224 delta -= ep->re_max_fr_depth;
Chuck Lever3968cb52015-03-30 14:35:26 -0400225 } while (delta > 0);
226 }
227
Chuck Lever25868e62020-01-03 11:56:48 -0500228 max_qp_wr = attrs->max_qp_wr;
Chuck Lever914fcad2018-05-04 15:34:48 -0400229 max_qp_wr -= RPCRDMA_BACKWARD_WRS;
230 max_qp_wr -= 1;
231 if (max_qp_wr < RPCRDMA_MIN_SLOT_TABLE)
232 return -ENOMEM;
Chuck Lever93aa8e02020-02-21 17:00:54 -0500233 if (ep->re_max_requests > max_qp_wr)
234 ep->re_max_requests = max_qp_wr;
235 ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
236 if (ep->re_attr.cap.max_send_wr > max_qp_wr) {
237 ep->re_max_requests = max_qp_wr / depth;
238 if (!ep->re_max_requests)
Chuck Lever25868e62020-01-03 11:56:48 -0500239 return -ENOMEM;
Chuck Lever93aa8e02020-02-21 17:00:54 -0500240 ep->re_attr.cap.max_send_wr = ep->re_max_requests * depth;
Chuck Lever3968cb52015-03-30 14:35:26 -0400241 }
Chuck Lever93aa8e02020-02-21 17:00:54 -0500242 ep->re_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
243 ep->re_attr.cap.max_send_wr += 1; /* for ib_drain_sq */
244 ep->re_attr.cap.max_recv_wr = ep->re_max_requests;
245 ep->re_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever32e6b682021-04-19 14:02:03 -0400246 ep->re_attr.cap.max_recv_wr += RPCRDMA_MAX_RECV_BATCH;
Chuck Lever93aa8e02020-02-21 17:00:54 -0500247 ep->re_attr.cap.max_recv_wr += 1; /* for ib_drain_rq */
Chuck Lever3968cb52015-03-30 14:35:26 -0400248
Chuck Lever93aa8e02020-02-21 17:00:54 -0500249 ep->re_max_rdma_segs =
250 DIV_ROUND_UP(RPCRDMA_MAX_DATA_SEGS, ep->re_max_fr_depth);
Chuck Lever6946f822018-12-19 10:58:45 -0500251 /* Reply chunks require segments for head and tail buffers */
Chuck Lever93aa8e02020-02-21 17:00:54 -0500252 ep->re_max_rdma_segs += 2;
253 if (ep->re_max_rdma_segs > RPCRDMA_MAX_HDR_SEGS)
254 ep->re_max_rdma_segs = RPCRDMA_MAX_HDR_SEGS;
Chuck Lever18d065a2020-01-03 11:56:43 -0500255
256 /* Ensure the underlying device is capable of conveying the
257 * largest r/wsize NFS will ask for. This guarantees that
258 * failing over from one RDMA device to another will not
259 * break NFS I/O.
260 */
Chuck Lever93aa8e02020-02-21 17:00:54 -0500261 if ((ep->re_max_rdma_segs * ep->re_max_fr_depth) < RPCRDMA_MAX_SEGS)
Chuck Lever18d065a2020-01-03 11:56:43 -0500262 return -ENOMEM;
263
Chuck Lever3968cb52015-03-30 14:35:26 -0400264 return 0;
265}
266
Chuck Lever5f624122018-12-19 10:59:01 -0500267/**
Chuck Lever5f624122018-12-19 10:59:01 -0500268 * frwr_map - Register a memory region
269 * @r_xprt: controlling transport
270 * @seg: memory region co-ordinates
271 * @nsegs: number of segments remaining
272 * @writing: true when RDMA Write will be used
Chuck Lever0a93fbc2018-12-19 10:59:07 -0500273 * @xid: XID of RPC using the registered memory
Chuck Lever3b39f522019-08-19 18:45:37 -0400274 * @mr: MR to fill in
Chuck Lever5f624122018-12-19 10:59:01 -0500275 *
276 * Prepare a REG_MR Work Request to register a memory region
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400277 * for remote access via RDMA READ or RDMA WRITE.
Chuck Lever5f624122018-12-19 10:59:01 -0500278 *
279 * Returns the next segment or a negative errno pointer.
Chuck Lever3b39f522019-08-19 18:45:37 -0400280 * On success, @mr is filled in.
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400281 */
Chuck Lever5f624122018-12-19 10:59:01 -0500282struct rpcrdma_mr_seg *frwr_map(struct rpcrdma_xprt *r_xprt,
283 struct rpcrdma_mr_seg *seg,
Chuck Leverec482cc2019-02-11 11:23:44 -0500284 int nsegs, bool writing, __be32 xid,
Chuck Lever3b39f522019-08-19 18:45:37 -0400285 struct rpcrdma_mr *mr)
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400286{
Chuck Levere28ce902020-02-21 17:01:05 -0500287 struct rpcrdma_ep *ep = r_xprt->rx_ep;
Chuck Lever3cf4e162015-12-16 17:22:31 -0500288 struct ib_reg_wr *reg_wr;
Chuck Leverca1c6712020-02-12 11:12:30 -0500289 int i, n, dma_nents;
Chuck Lever3b39f522019-08-19 18:45:37 -0400290 struct ib_mr *ibmr;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400291 u8 key;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400292
Chuck Lever93aa8e02020-02-21 17:00:54 -0500293 if (nsegs > ep->re_max_fr_depth)
294 nsegs = ep->re_max_fr_depth;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300295 for (i = 0; i < nsegs;) {
Chuck Lever67b16622021-02-04 11:59:13 -0500296 sg_set_page(&mr->mr_sg[i], seg->mr_page,
297 seg->mr_len, seg->mr_offset);
Sagi Grimberg4143f342015-10-13 19:11:35 +0300298
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400299 ++seg;
300 ++i;
Chuck Lever93aa8e02020-02-21 17:00:54 -0500301 if (ep->re_mrtype == IB_MR_TYPE_SG_GAPS)
Chuck Lever5e9fc6a2016-11-29 10:52:24 -0500302 continue;
Chuck Lever67b16622021-02-04 11:59:13 -0500303 if ((i < nsegs && seg->mr_offset) ||
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400304 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
305 break;
306 }
Chuck Lever96cedde2017-12-14 20:57:55 -0500307 mr->mr_dir = rpcrdma_data_dir(writing);
Chuck Leverca1c6712020-02-12 11:12:30 -0500308 mr->mr_nents = i;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400309
Chuck Lever93aa8e02020-02-21 17:00:54 -0500310 dma_nents = ib_dma_map_sg(ep->re_id->device, mr->mr_sg, mr->mr_nents,
Chuck Leverca1c6712020-02-12 11:12:30 -0500311 mr->mr_dir);
312 if (!dma_nents)
Chuck Lever564471d2016-06-29 13:52:21 -0400313 goto out_dmamap_err;
Chuck Lever7a03aeb62020-11-09 14:40:19 -0500314 mr->mr_device = ep->re_id->device;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300315
Chuck Lever84756892019-06-19 10:32:59 -0400316 ibmr = mr->frwr.fr_mr;
Chuck Leverca1c6712020-02-12 11:12:30 -0500317 n = ib_map_mr_sg(ibmr, mr->mr_sg, dma_nents, NULL, PAGE_SIZE);
318 if (n != dma_nents)
Chuck Lever564471d2016-06-29 13:52:21 -0400319 goto out_mapmr_err;
Sagi Grimberg4143f342015-10-13 19:11:35 +0300320
Chuck Lever0a93fbc2018-12-19 10:59:07 -0500321 ibmr->iova &= 0x00000000ffffffff;
Chuck Leverec482cc2019-02-11 11:23:44 -0500322 ibmr->iova |= ((u64)be32_to_cpu(xid)) << 32;
Chuck Lever96cedde2017-12-14 20:57:55 -0500323 key = (u8)(ibmr->rkey & 0x000000FF);
324 ib_update_fast_reg_key(ibmr, ++key);
Sagi Grimberg4143f342015-10-13 19:11:35 +0300325
Chuck Lever84756892019-06-19 10:32:59 -0400326 reg_wr = &mr->frwr.fr_regwr;
Chuck Lever96cedde2017-12-14 20:57:55 -0500327 reg_wr->mr = ibmr;
328 reg_wr->key = ibmr->rkey;
Chuck Lever3cf4e162015-12-16 17:22:31 -0500329 reg_wr->access = writing ?
330 IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
331 IB_ACCESS_REMOTE_READ;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400332
Chuck Lever96cedde2017-12-14 20:57:55 -0500333 mr->mr_handle = ibmr->rkey;
334 mr->mr_length = ibmr->length;
335 mr->mr_offset = ibmr->iova;
Chuck Leverba217ec2018-12-19 10:59:55 -0500336 trace_xprtrdma_mr_map(mr);
Sagi Grimberg4143f342015-10-13 19:11:35 +0300337
Chuck Lever6748b0ca2017-08-14 15:38:30 -0400338 return seg;
Chuck Lever564471d2016-06-29 13:52:21 -0400339
340out_dmamap_err:
Chuck Lever53b2c1c2018-12-19 11:00:06 -0500341 trace_xprtrdma_frwr_sgerr(mr, i);
Chuck Lever6748b0ca2017-08-14 15:38:30 -0400342 return ERR_PTR(-EIO);
Chuck Lever564471d2016-06-29 13:52:21 -0400343
344out_mapmr_err:
Chuck Lever53b2c1c2018-12-19 11:00:06 -0500345 trace_xprtrdma_frwr_maperr(mr, n);
Chuck Lever6748b0ca2017-08-14 15:38:30 -0400346 return ERR_PTR(-EIO);
Chuck Leverf2877622018-02-28 15:30:59 -0500347}
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400348
Chuck Lever5f624122018-12-19 10:59:01 -0500349/**
Chuck Lever84756892019-06-19 10:32:59 -0400350 * frwr_wc_fastreg - Invoked by RDMA provider for a flushed FastReg WC
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500351 * @cq: completion queue
352 * @wc: WCE for a completed FastReg WR
Chuck Lever84756892019-06-19 10:32:59 -0400353 *
Chuck Levere4b52ca2021-04-19 14:03:12 -0400354 * Each flushed MR gets destroyed after the QP has drained.
Chuck Lever84756892019-06-19 10:32:59 -0400355 */
356static void frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc)
357{
358 struct ib_cqe *cqe = wc->wr_cqe;
359 struct rpcrdma_frwr *frwr =
360 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
361
362 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500363 trace_xprtrdma_wc_fastreg(wc, &frwr->fr_cid);
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500364
Chuck Leverf423f752020-06-15 09:21:02 -0400365 rpcrdma_flush_disconnect(cq->cq_context, wc);
Chuck Lever84756892019-06-19 10:32:59 -0400366}
367
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500368static void frwr_cid_init(struct rpcrdma_ep *ep,
369 struct rpcrdma_frwr *frwr)
370{
371 struct rpc_rdma_cid *cid = &frwr->fr_cid;
372
373 cid->ci_queue_id = ep->re_attr.send_cq->res.id;
374 cid->ci_completion_id = frwr->fr_mr->res.id;
375}
376
Chuck Lever84756892019-06-19 10:32:59 -0400377/**
Chuck Lever97d0de82020-02-21 17:00:23 -0500378 * frwr_send - post Send WRs containing the RPC Call message
379 * @r_xprt: controlling transport instance
380 * @req: prepared RPC Call
Chuck Leverf2877622018-02-28 15:30:59 -0500381 *
Chuck Levere0f86bc2018-12-19 11:00:27 -0500382 * For FRWR, chain any FastReg WRs to the Send WR. Only a
Chuck Leverf2877622018-02-28 15:30:59 -0500383 * single ib_post_send call is needed to register memory
384 * and then post the Send WR.
Chuck Lever5f624122018-12-19 10:59:01 -0500385 *
Chuck Lever97d0de82020-02-21 17:00:23 -0500386 * Returns the return code from ib_post_send.
387 *
388 * Caller must hold the transport send lock to ensure that the
389 * pointers to the transport's rdma_cm_id and QP are stable.
Chuck Leverf2877622018-02-28 15:30:59 -0500390 */
Chuck Lever97d0de82020-02-21 17:00:23 -0500391int frwr_send(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
Chuck Leverf2877622018-02-28 15:30:59 -0500392{
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500393 struct rpcrdma_ep *ep = r_xprt->rx_ep;
Bart Van Asscheed288d72018-07-18 09:25:31 -0700394 struct ib_send_wr *post_wr;
Chuck Leverf2877622018-02-28 15:30:59 -0500395 struct rpcrdma_mr *mr;
396
Chuck Leverdc15c3d2019-10-17 14:31:35 -0400397 post_wr = &req->rl_wr;
Chuck Leverf2877622018-02-28 15:30:59 -0500398 list_for_each_entry(mr, &req->rl_registered, mr_list) {
399 struct rpcrdma_frwr *frwr;
400
401 frwr = &mr->frwr;
402
403 frwr->fr_cqe.done = frwr_wc_fastreg;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500404 frwr_cid_init(ep, frwr);
Chuck Leverf2877622018-02-28 15:30:59 -0500405 frwr->fr_regwr.wr.next = post_wr;
406 frwr->fr_regwr.wr.wr_cqe = &frwr->fr_cqe;
407 frwr->fr_regwr.wr.num_sge = 0;
408 frwr->fr_regwr.wr.opcode = IB_WR_REG_MR;
409 frwr->fr_regwr.wr.send_flags = 0;
410
411 post_wr = &frwr->fr_regwr.wr;
412 }
413
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500414 return ib_post_send(ep->re_id->qp, post_wr, NULL);
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400415}
416
Chuck Lever5f624122018-12-19 10:59:01 -0500417/**
418 * frwr_reminv - handle a remotely invalidated mr on the @mrs list
419 * @rep: Received reply
420 * @mrs: list of MRs to check
421 *
Chuck Leverc3441612017-12-14 20:56:26 -0500422 */
Chuck Lever5f624122018-12-19 10:59:01 -0500423void frwr_reminv(struct rpcrdma_rep *rep, struct list_head *mrs)
Chuck Leverc3441612017-12-14 20:56:26 -0500424{
Chuck Lever96cedde2017-12-14 20:57:55 -0500425 struct rpcrdma_mr *mr;
Chuck Leverc3441612017-12-14 20:56:26 -0500426
Chuck Lever96cedde2017-12-14 20:57:55 -0500427 list_for_each_entry(mr, mrs, mr_list)
428 if (mr->mr_handle == rep->rr_inv_rkey) {
Chuck Lever054f1552018-05-01 11:37:14 -0400429 list_del_init(&mr->mr_list);
Chuck Leveref2be592020-11-09 14:40:14 -0500430 frwr_mr_put(mr);
Chuck Leverc3441612017-12-14 20:56:26 -0500431 break; /* only one invalidated MR per RPC */
432 }
433}
434
Chuck Leveref2be592020-11-09 14:40:14 -0500435static void frwr_mr_done(struct ib_wc *wc, struct rpcrdma_mr *mr)
Chuck Lever84756892019-06-19 10:32:59 -0400436{
Chuck Levere4b52ca2021-04-19 14:03:12 -0400437 if (likely(wc->status == IB_WC_SUCCESS))
Chuck Leveref2be592020-11-09 14:40:14 -0500438 frwr_mr_put(mr);
Chuck Lever84756892019-06-19 10:32:59 -0400439}
440
441/**
442 * frwr_wc_localinv - Invoked by RDMA provider for a LOCAL_INV WC
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500443 * @cq: completion queue
444 * @wc: WCE for a completed LocalInv WR
Chuck Lever84756892019-06-19 10:32:59 -0400445 *
446 */
447static void frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc)
448{
449 struct ib_cqe *cqe = wc->wr_cqe;
450 struct rpcrdma_frwr *frwr =
451 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
452 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
453
454 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500455 trace_xprtrdma_wc_li(wc, &frwr->fr_cid);
Chuck Leveref2be592020-11-09 14:40:14 -0500456 frwr_mr_done(wc, mr);
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500457
Chuck Leverf423f752020-06-15 09:21:02 -0400458 rpcrdma_flush_disconnect(cq->cq_context, wc);
Chuck Lever84756892019-06-19 10:32:59 -0400459}
460
461/**
462 * frwr_wc_localinv_wake - Invoked by RDMA provider for a LOCAL_INV WC
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500463 * @cq: completion queue
464 * @wc: WCE for a completed LocalInv WR
Chuck Lever84756892019-06-19 10:32:59 -0400465 *
466 * Awaken anyone waiting for an MR to finish being fenced.
467 */
468static void frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc)
469{
470 struct ib_cqe *cqe = wc->wr_cqe;
471 struct rpcrdma_frwr *frwr =
472 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
473 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
474
475 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500476 trace_xprtrdma_wc_li_wake(wc, &frwr->fr_cid);
Chuck Leveref2be592020-11-09 14:40:14 -0500477 frwr_mr_done(wc, mr);
Chuck Lever6dc6ec92019-08-19 18:47:10 -0400478 complete(&frwr->fr_linv_done);
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500479
Chuck Leverf423f752020-06-15 09:21:02 -0400480 rpcrdma_flush_disconnect(cq->cq_context, wc);
Chuck Lever84756892019-06-19 10:32:59 -0400481}
482
Chuck Lever5f624122018-12-19 10:59:01 -0500483/**
484 * frwr_unmap_sync - invalidate memory regions that were registered for @req
Chuck Lever84756892019-06-19 10:32:59 -0400485 * @r_xprt: controlling transport instance
486 * @req: rpcrdma_req with a non-empty list of MRs to process
Chuck Leverc9918ff2015-12-16 17:22:47 -0500487 *
Chuck Lever84756892019-06-19 10:32:59 -0400488 * Sleeps until it is safe for the host CPU to access the previously mapped
Chuck Leverd8099fe2019-06-19 10:33:10 -0400489 * memory regions. This guarantees that registered MRs are properly fenced
490 * from the server before the RPC consumer accesses the data in them. It
491 * also ensures proper Send flow control: waking the next RPC waits until
492 * this RPC has relinquished all its Send Queue entries.
Chuck Leverc9918ff2015-12-16 17:22:47 -0500493 */
Chuck Lever84756892019-06-19 10:32:59 -0400494void frwr_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
Chuck Leverc9918ff2015-12-16 17:22:47 -0500495{
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700496 struct ib_send_wr *first, **prev, *last;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500497 struct rpcrdma_ep *ep = r_xprt->rx_ep;
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700498 const struct ib_send_wr *bad_wr;
Chuck Leverce5b3712017-12-14 20:57:47 -0500499 struct rpcrdma_frwr *frwr;
Chuck Lever96cedde2017-12-14 20:57:55 -0500500 struct rpcrdma_mr *mr;
Chuck Lever84756892019-06-19 10:32:59 -0400501 int rc;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500502
Chuck Lever451d26e2017-06-08 11:52:04 -0400503 /* ORDER: Invalidate all of the MRs first
Chuck Leverc9918ff2015-12-16 17:22:47 -0500504 *
505 * Chain the LOCAL_INV Work Requests and post them with
506 * a single ib_post_send() call.
507 */
Chuck Leverce5b3712017-12-14 20:57:47 -0500508 frwr = NULL;
Chuck Levera100fda2016-11-29 10:52:57 -0500509 prev = &first;
Chuck Lever265a38d2019-08-19 18:44:04 -0400510 while ((mr = rpcrdma_mr_pop(&req->rl_registered))) {
Chuck Lever84756892019-06-19 10:32:59 -0400511
512 trace_xprtrdma_mr_localinv(mr);
513 r_xprt->rx_stats.local_inv_needed++;
Chuck Leverc8b920b2016-09-15 10:57:16 -0400514
Chuck Lever96cedde2017-12-14 20:57:55 -0500515 frwr = &mr->frwr;
Chuck Leverce5b3712017-12-14 20:57:47 -0500516 frwr->fr_cqe.done = frwr_wc_localinv;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500517 frwr_cid_init(ep, frwr);
Chuck Leverce5b3712017-12-14 20:57:47 -0500518 last = &frwr->fr_invwr;
Chuck Lever84756892019-06-19 10:32:59 -0400519 last->next = NULL;
Chuck Leverce5b3712017-12-14 20:57:47 -0500520 last->wr_cqe = &frwr->fr_cqe;
Chuck Lever84756892019-06-19 10:32:59 -0400521 last->sg_list = NULL;
522 last->num_sge = 0;
Chuck Levera100fda2016-11-29 10:52:57 -0500523 last->opcode = IB_WR_LOCAL_INV;
Chuck Lever84756892019-06-19 10:32:59 -0400524 last->send_flags = IB_SEND_SIGNALED;
Chuck Lever96cedde2017-12-14 20:57:55 -0500525 last->ex.invalidate_rkey = mr->mr_handle;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500526
Chuck Levera100fda2016-11-29 10:52:57 -0500527 *prev = last;
528 prev = &last->next;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500529 }
Chuck Leverc9918ff2015-12-16 17:22:47 -0500530
531 /* Strong send queue ordering guarantees that when the
532 * last WR in the chain completes, all WRs in the chain
533 * are complete.
534 */
Chuck Leverce5b3712017-12-14 20:57:47 -0500535 frwr->fr_cqe.done = frwr_wc_localinv_wake;
536 reinit_completion(&frwr->fr_linv_done);
Chuck Lever8d38de62016-11-29 10:52:16 -0500537
Chuck Leverc9918ff2015-12-16 17:22:47 -0500538 /* Transport disconnect drains the receive CQ before it
539 * replaces the QP. The RPC reply handler won't call us
Chuck Lever93aa8e02020-02-21 17:00:54 -0500540 * unless re_id->qp is a valid pointer.
Chuck Leverc9918ff2015-12-16 17:22:47 -0500541 */
Chuck Lever8d754832017-06-08 11:52:28 -0400542 bad_wr = NULL;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500543 rc = ib_post_send(ep->re_id->qp, first, &bad_wr);
Chuck Lever84756892019-06-19 10:32:59 -0400544
545 /* The final LOCAL_INV WR in the chain is supposed to
546 * do the wake. If it was never posted, the wake will
547 * not happen, so don't wait in that case.
548 */
Chuck Lever8d754832017-06-08 11:52:28 -0400549 if (bad_wr != first)
Chuck Leverce5b3712017-12-14 20:57:47 -0500550 wait_for_completion(&frwr->fr_linv_done);
Chuck Lever84756892019-06-19 10:32:59 -0400551 if (!rc)
552 return;
Chuck Leverc9918ff2015-12-16 17:22:47 -0500553
Chuck Levere4b52ca2021-04-19 14:03:12 -0400554 /* On error, the MRs get destroyed once the QP has drained. */
Chuck Lever36a55ed2020-11-09 14:39:37 -0500555 trace_xprtrdma_post_linv_err(req, rc);
Chuck Leverc9918ff2015-12-16 17:22:47 -0500556}
Chuck Leverd8099fe2019-06-19 10:33:10 -0400557
558/**
559 * frwr_wc_localinv_done - Invoked by RDMA provider for a signaled LOCAL_INV WC
Chuck Leverd6ccebf2020-02-21 17:00:49 -0500560 * @cq: completion queue
561 * @wc: WCE for a completed LocalInv WR
Chuck Leverd8099fe2019-06-19 10:33:10 -0400562 *
563 */
564static void frwr_wc_localinv_done(struct ib_cq *cq, struct ib_wc *wc)
565{
566 struct ib_cqe *cqe = wc->wr_cqe;
567 struct rpcrdma_frwr *frwr =
568 container_of(cqe, struct rpcrdma_frwr, fr_cqe);
569 struct rpcrdma_mr *mr = container_of(frwr, struct rpcrdma_mr, frwr);
Chuck Lever44438ad2021-04-19 14:03:06 -0400570 struct rpcrdma_rep *rep;
Chuck Leverd8099fe2019-06-19 10:33:10 -0400571
572 /* WARNING: Only wr_cqe and status are reliable at this point */
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500573 trace_xprtrdma_wc_li_done(wc, &frwr->fr_cid);
Chuck Lever6dc6ec92019-08-19 18:47:10 -0400574
Chuck Lever44438ad2021-04-19 14:03:06 -0400575 /* Ensure that @rep is generated before the MR is released */
576 rep = mr->mr_req->rl_reply;
Chuck Lever6dc6ec92019-08-19 18:47:10 -0400577 smp_rmb();
Chuck Lever44438ad2021-04-19 14:03:06 -0400578
Chuck Lever8a053432021-04-19 14:03:19 -0400579 if (wc->status != IB_WC_SUCCESS) {
580 if (rep)
581 rpcrdma_unpin_rqst(rep);
582 rpcrdma_flush_disconnect(cq->cq_context, wc);
583 return;
584 }
585 frwr_mr_put(mr);
Chuck Lever6dc6ec92019-08-19 18:47:10 -0400586 rpcrdma_complete_rqst(rep);
Chuck Leverd8099fe2019-06-19 10:33:10 -0400587}
588
589/**
590 * frwr_unmap_async - invalidate memory regions that were registered for @req
591 * @r_xprt: controlling transport instance
592 * @req: rpcrdma_req with a non-empty list of MRs to process
593 *
594 * This guarantees that registered MRs are properly fenced from the
595 * server before the RPC consumer accesses the data in them. It also
596 * ensures proper Send flow control: waking the next RPC waits until
597 * this RPC has relinquished all its Send Queue entries.
598 */
599void frwr_unmap_async(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req)
600{
601 struct ib_send_wr *first, *last, **prev;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500602 struct rpcrdma_ep *ep = r_xprt->rx_ep;
Chuck Leverd8099fe2019-06-19 10:33:10 -0400603 struct rpcrdma_frwr *frwr;
604 struct rpcrdma_mr *mr;
605 int rc;
606
607 /* Chain the LOCAL_INV Work Requests and post them with
608 * a single ib_post_send() call.
609 */
610 frwr = NULL;
611 prev = &first;
Chuck Lever265a38d2019-08-19 18:44:04 -0400612 while ((mr = rpcrdma_mr_pop(&req->rl_registered))) {
Chuck Leverd8099fe2019-06-19 10:33:10 -0400613
614 trace_xprtrdma_mr_localinv(mr);
615 r_xprt->rx_stats.local_inv_needed++;
616
617 frwr = &mr->frwr;
618 frwr->fr_cqe.done = frwr_wc_localinv;
Chuck Lever5ecef9c2020-11-09 14:39:31 -0500619 frwr_cid_init(ep, frwr);
Chuck Leverd8099fe2019-06-19 10:33:10 -0400620 last = &frwr->fr_invwr;
621 last->next = NULL;
622 last->wr_cqe = &frwr->fr_cqe;
623 last->sg_list = NULL;
624 last->num_sge = 0;
625 last->opcode = IB_WR_LOCAL_INV;
626 last->send_flags = IB_SEND_SIGNALED;
627 last->ex.invalidate_rkey = mr->mr_handle;
628
629 *prev = last;
630 prev = &last->next;
631 }
632
633 /* Strong send queue ordering guarantees that when the
634 * last WR in the chain completes, all WRs in the chain
635 * are complete. The last completion will wake up the
636 * RPC waiter.
637 */
638 frwr->fr_cqe.done = frwr_wc_localinv_done;
639
640 /* Transport disconnect drains the receive CQ before it
641 * replaces the QP. The RPC reply handler won't call us
Chuck Lever93aa8e02020-02-21 17:00:54 -0500642 * unless re_id->qp is a valid pointer.
Chuck Leverd8099fe2019-06-19 10:33:10 -0400643 */
Chuck Levere4b52ca2021-04-19 14:03:12 -0400644 rc = ib_post_send(ep->re_id->qp, first, NULL);
Chuck Leverd8099fe2019-06-19 10:33:10 -0400645 if (!rc)
646 return;
647
Chuck Levere4b52ca2021-04-19 14:03:12 -0400648 /* On error, the MRs get destroyed once the QP has drained. */
Chuck Lever36a55ed2020-11-09 14:39:37 -0500649 trace_xprtrdma_post_linv_err(req, rc);
Chuck Leverd8099fe2019-06-19 10:33:10 -0400650
651 /* The final LOCAL_INV WR in the chain is supposed to
Chuck Lever8a053432021-04-19 14:03:19 -0400652 * do the wake. If it was never posted, the wake does
653 * not happen. Unpin the rqst in preparation for its
654 * retransmission.
Chuck Leverd8099fe2019-06-19 10:33:10 -0400655 */
Chuck Lever8a053432021-04-19 14:03:19 -0400656 rpcrdma_unpin_rqst(req->rl_reply);
Chuck Leverd8099fe2019-06-19 10:33:10 -0400657}