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 | |
| 72 | i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; |
Linus Torvalds | 59953fb | 2015-04-26 17:33:59 -0700 | [diff] [blame] | 73 | dprintk("RPC: %s: initializing %d FMRs\n", __func__, i); |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 74 | |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 75 | rc = -ENOMEM; |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 76 | while (i--) { |
| 77 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 78 | if (!r) |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 79 | goto out; |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 80 | |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 81 | 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 Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 88 | 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 | |
| 95 | out_fmr_err: |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 96 | rc = PTR_ERR(r->r.fmr.fmr); |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 97 | dprintk("RPC: %s: ib_alloc_fmr status %i\n", __func__, rc); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 98 | kfree(r->r.fmr.physaddrs); |
| 99 | out_free: |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 100 | kfree(r); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 101 | out: |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 102 | return rc; |
| 103 | } |
| 104 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 105 | static int |
| 106 | __fmr_unmap(struct rpcrdma_mw *r) |
| 107 | { |
| 108 | LIST_HEAD(l); |
| 109 | |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 110 | list_add(&r->r.fmr.fmr->list, &l); |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 111 | return ib_unmap_fmr(&l); |
| 112 | } |
| 113 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 114 | /* Use the ib_map_phys_fmr() verb to register a memory region |
| 115 | * for remote access via RDMA READ or RDMA WRITE. |
| 116 | */ |
| 117 | static int |
| 118 | fmr_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 Lever | 89e0d112 | 2015-05-26 11:51:56 -0400 | [diff] [blame] | 122 | struct ib_device *device = ia->ri_device; |
Chuck Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 123 | enum dma_data_direction direction = rpcrdma_data_dir(writing); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 124 | struct rpcrdma_mr_seg *seg1 = seg; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 125 | int len, pageoff, i, rc; |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 126 | 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 Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 140 | |
| 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 Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 148 | rpcrdma_map_one(device, seg, direction); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 149 | mw->r.fmr.physaddrs[i] = seg->mr_dma; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 150 | 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 Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 159 | rc = ib_map_phys_fmr(mw->r.fmr.fmr, mw->r.fmr.physaddrs, |
| 160 | i, seg1->mr_dma); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 161 | if (rc) |
| 162 | goto out_maperr; |
| 163 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 164 | seg1->rl_mw = mw; |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 165 | seg1->mr_rkey = mw->r.fmr.fmr->rkey; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 166 | seg1->mr_base = seg1->mr_dma + pageoff; |
| 167 | seg1->mr_nsegs = i; |
| 168 | seg1->mr_len = len; |
| 169 | return i; |
| 170 | |
| 171 | out_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 Lever | d654788 | 2015-03-30 14:35:44 -0400 | [diff] [blame] | 176 | rpcrdma_unmap_one(device, --seg); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 177 | return rc; |
| 178 | } |
| 179 | |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 180 | /* Use the ib_unmap_fmr() verb to prevent further remote |
| 181 | * access via RDMA READ or RDMA WRITE. |
| 182 | */ |
| 183 | static int |
| 184 | fmr_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 Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 188 | struct rpcrdma_mw *mw = seg1->rl_mw; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 189 | int rc, nsegs = seg->mr_nsegs; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 190 | |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 191 | dprintk("RPC: %s: FMR %p\n", __func__, mw); |
| 192 | |
| 193 | seg1->rl_mw = NULL; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 194 | while (seg1->mr_nsegs--) |
Chuck Lever | 89e0d112 | 2015-05-26 11:51:56 -0400 | [diff] [blame] | 195 | rpcrdma_unmap_one(ia->ri_device, seg++); |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 196 | rc = __fmr_unmap(mw); |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 197 | if (rc) |
| 198 | goto out_err; |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 199 | rpcrdma_put_mw(r_xprt, mw); |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 200 | return nsegs; |
| 201 | |
| 202 | out_err: |
Chuck Lever | fc7fbb5 | 2015-05-26 11:52:16 -0400 | [diff] [blame] | 203 | /* 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 Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 206 | dprintk("RPC: %s: ib_unmap_fmr status %i\n", __func__, rc); |
| 207 | return nsegs; |
| 208 | } |
| 209 | |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 210 | static void |
| 211 | fmr_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 Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 219 | kfree(r->r.fmr.physaddrs); |
| 220 | |
| 221 | rc = ib_dealloc_fmr(r->r.fmr.fmr); |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 222 | if (rc) |
| 223 | dprintk("RPC: %s: ib_dealloc_fmr failed %i\n", |
| 224 | __func__, rc); |
Chuck Lever | acb9da7 | 2015-05-26 11:53:23 -0400 | [diff] [blame^] | 225 | |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 226 | kfree(r); |
| 227 | } |
| 228 | } |
| 229 | |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 230 | const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = { |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 231 | .ro_map = fmr_op_map, |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 232 | .ro_unmap = fmr_op_unmap, |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 233 | .ro_open = fmr_op_open, |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 234 | .ro_maxpages = fmr_op_maxpages, |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 235 | .ro_init = fmr_op_init, |
Chuck Lever | 4561f34 | 2015-03-30 14:35:17 -0400 | [diff] [blame] | 236 | .ro_destroy = fmr_op_destroy, |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 237 | .ro_displayname = "fmr", |
| 238 | }; |