blob: 4a53ad5ab4b36722e5597888985987b60edf6cc9 [file] [log] [blame]
Chuck Levera0ce85f2015-03-30 14:34:21 -04001/*
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 Memory Regions (FMR).
7 * Referred to sometimes as MTHCAFMR mode.
8 *
9 * FMR uses synchronous memory registration and deregistration.
10 * FMR registration is known to be fast, but FMR deregistration
11 * can take tens of usecs to complete.
12 */
13
Chuck Leverfc7fbb52015-05-26 11:52:16 -040014/* Normal operation
15 *
16 * A Memory Region is prepared for RDMA READ or WRITE using the
17 * ib_map_phys_fmr verb (fmr_op_map). When the RDMA operation is
18 * finished, the Memory Region is unmapped using the ib_unmap_fmr
19 * verb (fmr_op_unmap).
20 */
21
22/* Transport recovery
23 *
24 * After a transport reconnect, fmr_op_map re-uses the MR already
25 * allocated for the RPC, but generates a fresh rkey then maps the
26 * MR again. This process is synchronous.
27 */
28
Chuck Levera0ce85f2015-03-30 14:34:21 -040029#include "xprt_rdma.h"
30
31#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
32# define RPCDBG_FACILITY RPCDBG_TRANS
33#endif
34
Chuck Lever1c9351e2015-03-30 14:34:30 -040035/* Maximum scatter/gather per FMR */
36#define RPCRDMA_MAX_FMR_SGES (64)
37
Chuck Lever3968cb52015-03-30 14:35:26 -040038static int
39fmr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
40 struct rpcrdma_create_data_internal *cdata)
41{
42 return 0;
43}
44
Chuck Lever1c9351e2015-03-30 14:34:30 -040045/* FMR mode conveys up to 64 pages of payload per chunk segment.
46 */
47static size_t
48fmr_op_maxpages(struct rpcrdma_xprt *r_xprt)
49{
50 return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
51 rpcrdma_max_segments(r_xprt) * RPCRDMA_MAX_FMR_SGES);
52}
53
Chuck Lever91e70e72015-03-30 14:34:58 -040054static int
55fmr_op_init(struct rpcrdma_xprt *r_xprt)
56{
57 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
58 int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ;
59 struct ib_fmr_attr fmr_attr = {
60 .max_pages = RPCRDMA_MAX_FMR_SGES,
61 .max_maps = 1,
62 .page_shift = PAGE_SHIFT
63 };
64 struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
65 struct rpcrdma_mw *r;
66 int i, rc;
67
Chuck Lever58d1dcf2015-05-26 11:53:13 -040068 spin_lock_init(&buf->rb_mwlock);
Chuck Lever91e70e72015-03-30 14:34:58 -040069 INIT_LIST_HEAD(&buf->rb_mws);
70 INIT_LIST_HEAD(&buf->rb_all);
71
72 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
Linus Torvalds59953fb2015-04-26 17:33:59 -070073 dprintk("RPC: %s: initializing %d FMRs\n", __func__, i);
Chuck Lever91e70e72015-03-30 14:34:58 -040074
Chuck Leveracb9da72015-05-26 11:53:23 -040075 rc = -ENOMEM;
Chuck Lever91e70e72015-03-30 14:34:58 -040076 while (i--) {
77 r = kzalloc(sizeof(*r), GFP_KERNEL);
78 if (!r)
Chuck Leveracb9da72015-05-26 11:53:23 -040079 goto out;
Chuck Lever91e70e72015-03-30 14:34:58 -040080
Chuck Leveracb9da72015-05-26 11:53:23 -040081 r->r.fmr.physaddrs = kmalloc(RPCRDMA_MAX_FMR_SGES *
82 sizeof(u64), GFP_KERNEL);
83 if (!r->r.fmr.physaddrs)
84 goto out_free;
85
86 r->r.fmr.fmr = ib_alloc_fmr(pd, mr_access_flags, &fmr_attr);
87 if (IS_ERR(r->r.fmr.fmr))
Chuck Lever91e70e72015-03-30 14:34:58 -040088 goto out_fmr_err;
89
90 list_add(&r->mw_list, &buf->rb_mws);
91 list_add(&r->mw_all, &buf->rb_all);
92 }
93 return 0;
94
95out_fmr_err:
Chuck Leveracb9da72015-05-26 11:53:23 -040096 rc = PTR_ERR(r->r.fmr.fmr);
Chuck Lever91e70e72015-03-30 14:34:58 -040097 dprintk("RPC: %s: ib_alloc_fmr status %i\n", __func__, rc);
Chuck Leveracb9da72015-05-26 11:53:23 -040098 kfree(r->r.fmr.physaddrs);
99out_free:
Chuck Lever91e70e72015-03-30 14:34:58 -0400100 kfree(r);
Chuck Leveracb9da72015-05-26 11:53:23 -0400101out:
Chuck Lever91e70e72015-03-30 14:34:58 -0400102 return rc;
103}
104
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400105static int
106__fmr_unmap(struct rpcrdma_mw *r)
107{
108 LIST_HEAD(l);
109
Chuck Leveracb9da72015-05-26 11:53:23 -0400110 list_add(&r->r.fmr.fmr->list, &l);
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400111 return ib_unmap_fmr(&l);
112}
113
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400114/* Use the ib_map_phys_fmr() verb to register a memory region
115 * for remote access via RDMA READ or RDMA WRITE.
116 */
117static int
118fmr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
119 int nsegs, bool writing)
120{
121 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400122 struct ib_device *device = ia->ri_device;
Chuck Leverd6547882015-03-30 14:35:44 -0400123 enum dma_data_direction direction = rpcrdma_data_dir(writing);
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400124 struct rpcrdma_mr_seg *seg1 = seg;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400125 int len, pageoff, i, rc;
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400126 struct rpcrdma_mw *mw;
127
128 mw = seg1->rl_mw;
129 seg1->rl_mw = NULL;
130 if (!mw) {
131 mw = rpcrdma_get_mw(r_xprt);
132 if (!mw)
133 return -ENOMEM;
134 } else {
135 /* this is a retransmit; generate a fresh rkey */
136 rc = __fmr_unmap(mw);
137 if (rc)
138 return rc;
139 }
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400140
141 pageoff = offset_in_page(seg1->mr_offset);
142 seg1->mr_offset -= pageoff; /* start of page */
143 seg1->mr_len += pageoff;
144 len = -pageoff;
145 if (nsegs > RPCRDMA_MAX_FMR_SGES)
146 nsegs = RPCRDMA_MAX_FMR_SGES;
147 for (i = 0; i < nsegs;) {
Chuck Leverd6547882015-03-30 14:35:44 -0400148 rpcrdma_map_one(device, seg, direction);
Chuck Leveracb9da72015-05-26 11:53:23 -0400149 mw->r.fmr.physaddrs[i] = seg->mr_dma;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400150 len += seg->mr_len;
151 ++seg;
152 ++i;
153 /* Check for holes */
154 if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
155 offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
156 break;
157 }
158
Chuck Leveracb9da72015-05-26 11:53:23 -0400159 rc = ib_map_phys_fmr(mw->r.fmr.fmr, mw->r.fmr.physaddrs,
160 i, seg1->mr_dma);
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400161 if (rc)
162 goto out_maperr;
163
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400164 seg1->rl_mw = mw;
Chuck Leveracb9da72015-05-26 11:53:23 -0400165 seg1->mr_rkey = mw->r.fmr.fmr->rkey;
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400166 seg1->mr_base = seg1->mr_dma + pageoff;
167 seg1->mr_nsegs = i;
168 seg1->mr_len = len;
169 return i;
170
171out_maperr:
172 dprintk("RPC: %s: ib_map_phys_fmr %u@0x%llx+%i (%d) status %i\n",
173 __func__, len, (unsigned long long)seg1->mr_dma,
174 pageoff, i, rc);
175 while (i--)
Chuck Leverd6547882015-03-30 14:35:44 -0400176 rpcrdma_unmap_one(device, --seg);
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400177 return rc;
178}
179
Chuck Lever6814bae2015-03-30 14:34:48 -0400180/* Use the ib_unmap_fmr() verb to prevent further remote
181 * access via RDMA READ or RDMA WRITE.
182 */
183static int
184fmr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg)
185{
186 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
187 struct rpcrdma_mr_seg *seg1 = seg;
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400188 struct rpcrdma_mw *mw = seg1->rl_mw;
Chuck Lever6814bae2015-03-30 14:34:48 -0400189 int rc, nsegs = seg->mr_nsegs;
Chuck Lever6814bae2015-03-30 14:34:48 -0400190
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400191 dprintk("RPC: %s: FMR %p\n", __func__, mw);
192
193 seg1->rl_mw = NULL;
Chuck Lever6814bae2015-03-30 14:34:48 -0400194 while (seg1->mr_nsegs--)
Chuck Lever89e0d1122015-05-26 11:51:56 -0400195 rpcrdma_unmap_one(ia->ri_device, seg++);
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400196 rc = __fmr_unmap(mw);
Chuck Lever6814bae2015-03-30 14:34:48 -0400197 if (rc)
198 goto out_err;
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400199 rpcrdma_put_mw(r_xprt, mw);
Chuck Lever6814bae2015-03-30 14:34:48 -0400200 return nsegs;
201
202out_err:
Chuck Leverfc7fbb52015-05-26 11:52:16 -0400203 /* The FMR is abandoned, but remains in rb_all. fmr_op_destroy
204 * will attempt to release it when the transport is destroyed.
205 */
Chuck Lever6814bae2015-03-30 14:34:48 -0400206 dprintk("RPC: %s: ib_unmap_fmr status %i\n", __func__, rc);
207 return nsegs;
208}
209
Chuck Lever4561f342015-03-30 14:35:17 -0400210static void
211fmr_op_destroy(struct rpcrdma_buffer *buf)
212{
213 struct rpcrdma_mw *r;
214 int rc;
215
216 while (!list_empty(&buf->rb_all)) {
217 r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
218 list_del(&r->mw_all);
Chuck Leveracb9da72015-05-26 11:53:23 -0400219 kfree(r->r.fmr.physaddrs);
220
221 rc = ib_dealloc_fmr(r->r.fmr.fmr);
Chuck Lever4561f342015-03-30 14:35:17 -0400222 if (rc)
223 dprintk("RPC: %s: ib_dealloc_fmr failed %i\n",
224 __func__, rc);
Chuck Leveracb9da72015-05-26 11:53:23 -0400225
Chuck Lever4561f342015-03-30 14:35:17 -0400226 kfree(r);
227 }
228}
229
Chuck Levera0ce85f2015-03-30 14:34:21 -0400230const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = {
Chuck Lever9c1b4d72015-03-30 14:34:39 -0400231 .ro_map = fmr_op_map,
Chuck Lever6814bae2015-03-30 14:34:48 -0400232 .ro_unmap = fmr_op_unmap,
Chuck Lever3968cb52015-03-30 14:35:26 -0400233 .ro_open = fmr_op_open,
Chuck Lever1c9351e2015-03-30 14:34:30 -0400234 .ro_maxpages = fmr_op_maxpages,
Chuck Lever91e70e72015-03-30 14:34:58 -0400235 .ro_init = fmr_op_init,
Chuck Lever4561f342015-03-30 14:35:17 -0400236 .ro_destroy = fmr_op_destroy,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400237 .ro_displayname = "fmr",
238};