Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 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 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 Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 14 | /* 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 Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 29 | #include "xprt_rdma.h" |
| 30 | |
| 31 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
| 32 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 33 | #endif |
| 34 | |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 35 | /* Maximum scatter/gather per FMR */ |
| 36 | #define RPCRDMA_MAX_FMR_SGES (64) |
| 37 | |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 38 | static int |
| 39 | fmr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep, |
| 40 | struct rpcrdma_create_data_internal *cdata) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 45 | /* FMR mode conveys up to 64 pages of payload per chunk segment. |
| 46 | */ |
| 47 | static size_t |
| 48 | fmr_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 Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 54 | static int |
| 55 | fmr_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 Lever | 58d1dcf | 2015-05-26 11:53:13 -0400 | [diff] [blame] | 68 | spin_lock_init(&buf->rb_mwlock); |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 69 | INIT_LIST_HEAD(&buf->rb_mws); |
| 70 | INIT_LIST_HEAD(&buf->rb_all); |
| 71 | |
Chuck Lever | 40c6ed0 | 2015-05-26 11:53:33 -0400 | [diff] [blame] | 72 | i = max_t(int, RPCRDMA_MAX_DATA_SEGS / RPCRDMA_MAX_FMR_SGES, 1); |
| 73 | i += 2; /* head + tail */ |
| 74 | i *= buf->rb_max_requests; /* one set for each RPC slot */ |
| 75 | dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i); |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 76 | |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 77 | rc = -ENOMEM; |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 78 | while (i--) { |
| 79 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 80 | if (!r) |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 81 | goto out; |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 82 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 83 | r->fmr.physaddrs = kmalloc(RPCRDMA_MAX_FMR_SGES * |
| 84 | sizeof(u64), GFP_KERNEL); |
| 85 | if (!r->fmr.physaddrs) |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 86 | goto out_free; |
| 87 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 88 | r->fmr.fmr = ib_alloc_fmr(pd, mr_access_flags, &fmr_attr); |
| 89 | if (IS_ERR(r->fmr.fmr)) |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 90 | goto out_fmr_err; |
| 91 | |
| 92 | list_add(&r->mw_list, &buf->rb_mws); |
| 93 | list_add(&r->mw_all, &buf->rb_all); |
| 94 | } |
| 95 | return 0; |
| 96 | |
| 97 | out_fmr_err: |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 98 | rc = PTR_ERR(r->fmr.fmr); |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 99 | dprintk("RPC: %s: ib_alloc_fmr status %i\n", __func__, rc); |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 100 | kfree(r->fmr.physaddrs); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 101 | out_free: |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 102 | kfree(r); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 103 | out: |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 104 | return rc; |
| 105 | } |
| 106 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 107 | static int |
| 108 | __fmr_unmap(struct rpcrdma_mw *r) |
| 109 | { |
| 110 | LIST_HEAD(l); |
| 111 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 112 | list_add(&r->fmr.fmr->list, &l); |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 113 | return ib_unmap_fmr(&l); |
| 114 | } |
| 115 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 116 | /* Use the ib_map_phys_fmr() verb to register a memory region |
| 117 | * for remote access via RDMA READ or RDMA WRITE. |
| 118 | */ |
| 119 | static int |
| 120 | fmr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg, |
| 121 | int nsegs, bool writing) |
| 122 | { |
| 123 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
Chuck Lever | 89e0d112 | 2015-05-26 11:51:56 -0400 | [diff] [blame] | 124 | struct ib_device *device = ia->ri_device; |
Chuck Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 125 | enum dma_data_direction direction = rpcrdma_data_dir(writing); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 126 | struct rpcrdma_mr_seg *seg1 = seg; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 127 | int len, pageoff, i, rc; |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 128 | struct rpcrdma_mw *mw; |
| 129 | |
| 130 | mw = seg1->rl_mw; |
| 131 | seg1->rl_mw = NULL; |
| 132 | if (!mw) { |
| 133 | mw = rpcrdma_get_mw(r_xprt); |
| 134 | if (!mw) |
| 135 | return -ENOMEM; |
| 136 | } else { |
| 137 | /* this is a retransmit; generate a fresh rkey */ |
| 138 | rc = __fmr_unmap(mw); |
| 139 | if (rc) |
| 140 | return rc; |
| 141 | } |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 142 | |
| 143 | pageoff = offset_in_page(seg1->mr_offset); |
| 144 | seg1->mr_offset -= pageoff; /* start of page */ |
| 145 | seg1->mr_len += pageoff; |
| 146 | len = -pageoff; |
| 147 | if (nsegs > RPCRDMA_MAX_FMR_SGES) |
| 148 | nsegs = RPCRDMA_MAX_FMR_SGES; |
| 149 | for (i = 0; i < nsegs;) { |
Chuck Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 150 | rpcrdma_map_one(device, seg, direction); |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 151 | mw->fmr.physaddrs[i] = seg->mr_dma; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 152 | len += seg->mr_len; |
| 153 | ++seg; |
| 154 | ++i; |
| 155 | /* Check for holes */ |
| 156 | if ((i < nsegs && offset_in_page(seg->mr_offset)) || |
| 157 | offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) |
| 158 | break; |
| 159 | } |
| 160 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 161 | rc = ib_map_phys_fmr(mw->fmr.fmr, mw->fmr.physaddrs, |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 162 | i, seg1->mr_dma); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 163 | if (rc) |
| 164 | goto out_maperr; |
| 165 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 166 | seg1->rl_mw = mw; |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 167 | seg1->mr_rkey = mw->fmr.fmr->rkey; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 168 | seg1->mr_base = seg1->mr_dma + pageoff; |
| 169 | seg1->mr_nsegs = i; |
| 170 | seg1->mr_len = len; |
| 171 | return i; |
| 172 | |
| 173 | out_maperr: |
| 174 | dprintk("RPC: %s: ib_map_phys_fmr %u@0x%llx+%i (%d) status %i\n", |
| 175 | __func__, len, (unsigned long long)seg1->mr_dma, |
| 176 | pageoff, i, rc); |
| 177 | while (i--) |
Chuck Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 178 | rpcrdma_unmap_one(device, --seg); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 179 | return rc; |
| 180 | } |
| 181 | |
Chuck Lever | 7c7a539 | 2015-12-16 17:22:55 -0500 | [diff] [blame] | 182 | static void |
| 183 | __fmr_dma_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg) |
| 184 | { |
| 185 | struct ib_device *device = r_xprt->rx_ia.ri_device; |
| 186 | struct rpcrdma_mw *mw = seg->rl_mw; |
| 187 | int nsegs = seg->mr_nsegs; |
| 188 | |
| 189 | seg->rl_mw = NULL; |
| 190 | |
| 191 | while (nsegs--) |
| 192 | rpcrdma_unmap_one(device, seg++); |
| 193 | |
| 194 | rpcrdma_put_mw(r_xprt, mw); |
| 195 | } |
| 196 | |
| 197 | /* Invalidate all memory regions that were registered for "req". |
| 198 | * |
| 199 | * Sleeps until it is safe for the host CPU to access the |
| 200 | * previously mapped memory regions. |
| 201 | */ |
| 202 | static void |
| 203 | fmr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req) |
| 204 | { |
| 205 | struct rpcrdma_mr_seg *seg; |
| 206 | unsigned int i, nchunks; |
| 207 | struct rpcrdma_mw *mw; |
| 208 | LIST_HEAD(unmap_list); |
| 209 | int rc; |
| 210 | |
| 211 | dprintk("RPC: %s: req %p\n", __func__, req); |
| 212 | |
| 213 | /* ORDER: Invalidate all of the req's MRs first |
| 214 | * |
| 215 | * ib_unmap_fmr() is slow, so use a single call instead |
| 216 | * of one call per mapped MR. |
| 217 | */ |
| 218 | for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) { |
| 219 | seg = &req->rl_segments[i]; |
| 220 | mw = seg->rl_mw; |
| 221 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 222 | list_add(&mw->fmr.fmr->list, &unmap_list); |
Chuck Lever | 7c7a539 | 2015-12-16 17:22:55 -0500 | [diff] [blame] | 223 | |
| 224 | i += seg->mr_nsegs; |
| 225 | } |
| 226 | rc = ib_unmap_fmr(&unmap_list); |
| 227 | if (rc) |
| 228 | pr_warn("%s: ib_unmap_fmr failed (%i)\n", __func__, rc); |
| 229 | |
| 230 | /* ORDER: Now DMA unmap all of the req's MRs, and return |
| 231 | * them to the free MW list. |
| 232 | */ |
| 233 | for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) { |
| 234 | seg = &req->rl_segments[i]; |
| 235 | |
| 236 | __fmr_dma_unmap(r_xprt, seg); |
| 237 | |
| 238 | i += seg->mr_nsegs; |
| 239 | seg->mr_nsegs = 0; |
| 240 | } |
| 241 | |
| 242 | req->rl_nchunks = 0; |
| 243 | } |
| 244 | |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 245 | /* Use the ib_unmap_fmr() verb to prevent further remote |
| 246 | * access via RDMA READ or RDMA WRITE. |
| 247 | */ |
| 248 | static int |
| 249 | fmr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg) |
| 250 | { |
| 251 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 252 | struct rpcrdma_mr_seg *seg1 = seg; |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 253 | struct rpcrdma_mw *mw = seg1->rl_mw; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 254 | int rc, nsegs = seg->mr_nsegs; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 255 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 256 | dprintk("RPC: %s: FMR %p\n", __func__, mw); |
| 257 | |
| 258 | seg1->rl_mw = NULL; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 259 | while (seg1->mr_nsegs--) |
Chuck Lever | 89e0d112 | 2015-05-26 11:51:56 -0400 | [diff] [blame] | 260 | rpcrdma_unmap_one(ia->ri_device, seg++); |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 261 | rc = __fmr_unmap(mw); |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 262 | if (rc) |
| 263 | goto out_err; |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 264 | rpcrdma_put_mw(r_xprt, mw); |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 265 | return nsegs; |
| 266 | |
| 267 | out_err: |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 268 | /* The FMR is abandoned, but remains in rb_all. fmr_op_destroy |
| 269 | * will attempt to release it when the transport is destroyed. |
| 270 | */ |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 271 | dprintk("RPC: %s: ib_unmap_fmr status %i\n", __func__, rc); |
| 272 | return nsegs; |
| 273 | } |
| 274 | |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 275 | static void |
| 276 | fmr_op_destroy(struct rpcrdma_buffer *buf) |
| 277 | { |
| 278 | struct rpcrdma_mw *r; |
| 279 | int rc; |
| 280 | |
| 281 | while (!list_empty(&buf->rb_all)) { |
| 282 | r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all); |
| 283 | list_del(&r->mw_all); |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 284 | kfree(r->fmr.physaddrs); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 285 | |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 286 | rc = ib_dealloc_fmr(r->fmr.fmr); |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 287 | if (rc) |
| 288 | dprintk("RPC: %s: ib_dealloc_fmr failed %i\n", |
| 289 | __func__, rc); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame] | 290 | |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 291 | kfree(r); |
| 292 | } |
| 293 | } |
| 294 | |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 295 | const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = { |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 296 | .ro_map = fmr_op_map, |
Chuck Lever | 7c7a539 | 2015-12-16 17:22:55 -0500 | [diff] [blame] | 297 | .ro_unmap_sync = fmr_op_unmap_sync, |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 298 | .ro_unmap = fmr_op_unmap, |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 299 | .ro_open = fmr_op_open, |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 300 | .ro_maxpages = fmr_op_maxpages, |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 301 | .ro_init = fmr_op_init, |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 302 | .ro_destroy = fmr_op_destroy, |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 303 | .ro_displayname = "fmr", |
| 304 | }; |