1/*
2 * Copyright (c) 2015 Oracle.  All rights reserved.
3 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
4 */
5
6/* Lightweight memory registration using Fast Registration Work
7 * Requests (FRWR). Also referred to sometimes as FRMR mode.
8 *
9 * FRWR features ordered asynchronous registration and deregistration
10 * of arbitrarily sized memory regions. This is the fastest and safest
11 * but most complex memory registration mode.
12 */
13
14#include "xprt_rdma.h"
15
16#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
17# define RPCDBG_FACILITY	RPCDBG_TRANS
18#endif
19
20static int
21__frwr_init(struct rpcrdma_mw *r, struct ib_pd *pd, struct ib_device *device,
22	    unsigned int depth)
23{
24	struct rpcrdma_frmr *f = &r->r.frmr;
25	int rc;
26
27	f->fr_mr = ib_alloc_fast_reg_mr(pd, depth);
28	if (IS_ERR(f->fr_mr))
29		goto out_mr_err;
30	f->fr_pgl = ib_alloc_fast_reg_page_list(device, depth);
31	if (IS_ERR(f->fr_pgl))
32		goto out_list_err;
33	return 0;
34
35out_mr_err:
36	rc = PTR_ERR(f->fr_mr);
37	dprintk("RPC:       %s: ib_alloc_fast_reg_mr status %i\n",
38		__func__, rc);
39	return rc;
40
41out_list_err:
42	rc = PTR_ERR(f->fr_pgl);
43	dprintk("RPC:       %s: ib_alloc_fast_reg_page_list status %i\n",
44		__func__, rc);
45	ib_dereg_mr(f->fr_mr);
46	return rc;
47}
48
49static void
50__frwr_release(struct rpcrdma_mw *r)
51{
52	int rc;
53
54	rc = ib_dereg_mr(r->r.frmr.fr_mr);
55	if (rc)
56		dprintk("RPC:       %s: ib_dereg_mr status %i\n",
57			__func__, rc);
58	ib_free_fast_reg_page_list(r->r.frmr.fr_pgl);
59}
60
61static int
62frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
63	     struct rpcrdma_create_data_internal *cdata)
64{
65	struct ib_device_attr *devattr = &ia->ri_devattr;
66	int depth, delta;
67
68	ia->ri_max_frmr_depth =
69			min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
70			      devattr->max_fast_reg_page_list_len);
71	dprintk("RPC:       %s: device's max FR page list len = %u\n",
72		__func__, ia->ri_max_frmr_depth);
73
74	/* Add room for frmr register and invalidate WRs.
75	 * 1. FRMR reg WR for head
76	 * 2. FRMR invalidate WR for head
77	 * 3. N FRMR reg WRs for pagelist
78	 * 4. N FRMR invalidate WRs for pagelist
79	 * 5. FRMR reg WR for tail
80	 * 6. FRMR invalidate WR for tail
81	 * 7. The RDMA_SEND WR
82	 */
83	depth = 7;
84
85	/* Calculate N if the device max FRMR depth is smaller than
86	 * RPCRDMA_MAX_DATA_SEGS.
87	 */
88	if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) {
89		delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth;
90		do {
91			depth += 2; /* FRMR reg + invalidate */
92			delta -= ia->ri_max_frmr_depth;
93		} while (delta > 0);
94	}
95
96	ep->rep_attr.cap.max_send_wr *= depth;
97	if (ep->rep_attr.cap.max_send_wr > devattr->max_qp_wr) {
98		cdata->max_requests = devattr->max_qp_wr / depth;
99		if (!cdata->max_requests)
100			return -EINVAL;
101		ep->rep_attr.cap.max_send_wr = cdata->max_requests *
102					       depth;
103	}
104
105	return 0;
106}
107
108/* FRWR mode conveys a list of pages per chunk segment. The
109 * maximum length of that list is the FRWR page list depth.
110 */
111static size_t
112frwr_op_maxpages(struct rpcrdma_xprt *r_xprt)
113{
114	struct rpcrdma_ia *ia = &r_xprt->rx_ia;
115
116	return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
117		     rpcrdma_max_segments(r_xprt) * ia->ri_max_frmr_depth);
118}
119
120/* If FAST_REG or LOCAL_INV failed, indicate the frmr needs to be reset. */
121static void
122frwr_sendcompletion(struct ib_wc *wc)
123{
124	struct rpcrdma_mw *r;
125
126	if (likely(wc->status == IB_WC_SUCCESS))
127		return;
128
129	/* WARNING: Only wr_id and status are reliable at this point */
130	r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
131	dprintk("RPC:       %s: frmr %p (stale), status %d\n",
132		__func__, r, wc->status);
133	r->r.frmr.fr_state = FRMR_IS_STALE;
134}
135
136static int
137frwr_op_init(struct rpcrdma_xprt *r_xprt)
138{
139	struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
140	struct ib_device *device = r_xprt->rx_ia.ri_id->device;
141	unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
142	struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
143	int i;
144
145	INIT_LIST_HEAD(&buf->rb_mws);
146	INIT_LIST_HEAD(&buf->rb_all);
147
148	i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
149	dprintk("RPC:       %s: initializing %d FRMRs\n", __func__, i);
150
151	while (i--) {
152		struct rpcrdma_mw *r;
153		int rc;
154
155		r = kzalloc(sizeof(*r), GFP_KERNEL);
156		if (!r)
157			return -ENOMEM;
158
159		rc = __frwr_init(r, pd, device, depth);
160		if (rc) {
161			kfree(r);
162			return rc;
163		}
164
165		list_add(&r->mw_list, &buf->rb_mws);
166		list_add(&r->mw_all, &buf->rb_all);
167		r->mw_sendcompletion = frwr_sendcompletion;
168	}
169
170	return 0;
171}
172
173/* Post a FAST_REG Work Request to register a memory region
174 * for remote access via RDMA READ or RDMA WRITE.
175 */
176static int
177frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
178	    int nsegs, bool writing)
179{
180	struct rpcrdma_ia *ia = &r_xprt->rx_ia;
181	struct ib_device *device = ia->ri_id->device;
182	enum dma_data_direction direction = rpcrdma_data_dir(writing);
183	struct rpcrdma_mr_seg *seg1 = seg;
184	struct rpcrdma_mw *mw = seg1->rl_mw;
185	struct rpcrdma_frmr *frmr = &mw->r.frmr;
186	struct ib_mr *mr = frmr->fr_mr;
187	struct ib_send_wr fastreg_wr, *bad_wr;
188	u8 key;
189	int len, pageoff;
190	int i, rc;
191	int seg_len;
192	u64 pa;
193	int page_no;
194
195	pageoff = offset_in_page(seg1->mr_offset);
196	seg1->mr_offset -= pageoff;	/* start of page */
197	seg1->mr_len += pageoff;
198	len = -pageoff;
199	if (nsegs > ia->ri_max_frmr_depth)
200		nsegs = ia->ri_max_frmr_depth;
201	for (page_no = i = 0; i < nsegs;) {
202		rpcrdma_map_one(device, seg, direction);
203		pa = seg->mr_dma;
204		for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) {
205			frmr->fr_pgl->page_list[page_no++] = pa;
206			pa += PAGE_SIZE;
207		}
208		len += seg->mr_len;
209		++seg;
210		++i;
211		/* Check for holes */
212		if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
213		    offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
214			break;
215	}
216	dprintk("RPC:       %s: Using frmr %p to map %d segments (%d bytes)\n",
217		__func__, mw, i, len);
218
219	frmr->fr_state = FRMR_IS_VALID;
220
221	memset(&fastreg_wr, 0, sizeof(fastreg_wr));
222	fastreg_wr.wr_id = (unsigned long)(void *)mw;
223	fastreg_wr.opcode = IB_WR_FAST_REG_MR;
224	fastreg_wr.wr.fast_reg.iova_start = seg1->mr_dma + pageoff;
225	fastreg_wr.wr.fast_reg.page_list = frmr->fr_pgl;
226	fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT;
227	fastreg_wr.wr.fast_reg.page_list_len = page_no;
228	fastreg_wr.wr.fast_reg.length = len;
229	fastreg_wr.wr.fast_reg.access_flags = writing ?
230				IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
231				IB_ACCESS_REMOTE_READ;
232	key = (u8)(mr->rkey & 0x000000FF);
233	ib_update_fast_reg_key(mr, ++key);
234	fastreg_wr.wr.fast_reg.rkey = mr->rkey;
235
236	DECR_CQCOUNT(&r_xprt->rx_ep);
237	rc = ib_post_send(ia->ri_id->qp, &fastreg_wr, &bad_wr);
238	if (rc)
239		goto out_senderr;
240
241	seg1->mr_rkey = mr->rkey;
242	seg1->mr_base = seg1->mr_dma + pageoff;
243	seg1->mr_nsegs = i;
244	seg1->mr_len = len;
245	return i;
246
247out_senderr:
248	dprintk("RPC:       %s: ib_post_send status %i\n", __func__, rc);
249	ib_update_fast_reg_key(mr, --key);
250	frmr->fr_state = FRMR_IS_INVALID;
251	while (i--)
252		rpcrdma_unmap_one(device, --seg);
253	return rc;
254}
255
256/* Post a LOCAL_INV Work Request to prevent further remote access
257 * via RDMA READ or RDMA WRITE.
258 */
259static int
260frwr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg)
261{
262	struct rpcrdma_mr_seg *seg1 = seg;
263	struct rpcrdma_ia *ia = &r_xprt->rx_ia;
264	struct ib_send_wr invalidate_wr, *bad_wr;
265	int rc, nsegs = seg->mr_nsegs;
266	struct ib_device *device;
267
268	seg1->rl_mw->r.frmr.fr_state = FRMR_IS_INVALID;
269
270	memset(&invalidate_wr, 0, sizeof(invalidate_wr));
271	invalidate_wr.wr_id = (unsigned long)(void *)seg1->rl_mw;
272	invalidate_wr.opcode = IB_WR_LOCAL_INV;
273	invalidate_wr.ex.invalidate_rkey = seg1->rl_mw->r.frmr.fr_mr->rkey;
274	DECR_CQCOUNT(&r_xprt->rx_ep);
275
276	read_lock(&ia->ri_qplock);
277	device = ia->ri_id->device;
278	while (seg1->mr_nsegs--)
279		rpcrdma_unmap_one(device, seg++);
280	rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
281	read_unlock(&ia->ri_qplock);
282	if (rc)
283		goto out_err;
284	return nsegs;
285
286out_err:
287	/* Force rpcrdma_buffer_get() to retry */
288	seg1->rl_mw->r.frmr.fr_state = FRMR_IS_STALE;
289	dprintk("RPC:       %s: ib_post_send status %i\n", __func__, rc);
290	return nsegs;
291}
292
293/* After a disconnect, a flushed FAST_REG_MR can leave an FRMR in
294 * an unusable state. Find FRMRs in this state and dereg / reg
295 * each.  FRMRs that are VALID and attached to an rpcrdma_req are
296 * also torn down.
297 *
298 * This gives all in-use FRMRs a fresh rkey and leaves them INVALID.
299 *
300 * This is invoked only in the transport connect worker in order
301 * to serialize with rpcrdma_register_frmr_external().
302 */
303static void
304frwr_op_reset(struct rpcrdma_xprt *r_xprt)
305{
306	struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
307	struct ib_device *device = r_xprt->rx_ia.ri_id->device;
308	unsigned int depth = r_xprt->rx_ia.ri_max_frmr_depth;
309	struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
310	struct rpcrdma_mw *r;
311	int rc;
312
313	list_for_each_entry(r, &buf->rb_all, mw_all) {
314		if (r->r.frmr.fr_state == FRMR_IS_INVALID)
315			continue;
316
317		__frwr_release(r);
318		rc = __frwr_init(r, pd, device, depth);
319		if (rc) {
320			dprintk("RPC:       %s: mw %p left %s\n",
321				__func__, r,
322				(r->r.frmr.fr_state == FRMR_IS_STALE ?
323					"stale" : "valid"));
324			continue;
325		}
326
327		r->r.frmr.fr_state = FRMR_IS_INVALID;
328	}
329}
330
331static void
332frwr_op_destroy(struct rpcrdma_buffer *buf)
333{
334	struct rpcrdma_mw *r;
335
336	while (!list_empty(&buf->rb_all)) {
337		r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
338		list_del(&r->mw_all);
339		__frwr_release(r);
340		kfree(r);
341	}
342}
343
344const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = {
345	.ro_map				= frwr_op_map,
346	.ro_unmap			= frwr_op_unmap,
347	.ro_open			= frwr_op_open,
348	.ro_maxpages			= frwr_op_maxpages,
349	.ro_init			= frwr_op_init,
350	.ro_reset			= frwr_op_reset,
351	.ro_destroy			= frwr_op_destroy,
352	.ro_displayname			= "frwr",
353};
354