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 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 | |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 14 | /* Normal operation |
| 15 | * |
| 16 | * A Memory Region is prepared for RDMA READ or WRITE using a FAST_REG |
| 17 | * Work Request (frmr_op_map). When the RDMA operation is finished, this |
| 18 | * Memory Region is invalidated using a LOCAL_INV Work Request |
| 19 | * (frmr_op_unmap). |
| 20 | * |
| 21 | * Typically these Work Requests are not signaled, and neither are RDMA |
| 22 | * SEND Work Requests (with the exception of signaling occasionally to |
| 23 | * prevent provider work queue overflows). This greatly reduces HCA |
| 24 | * interrupt workload. |
| 25 | * |
| 26 | * As an optimization, frwr_op_unmap marks MRs INVALID before the |
| 27 | * LOCAL_INV WR is posted. If posting succeeds, the MR is placed on |
| 28 | * rb_mws immediately so that no work (like managing a linked list |
| 29 | * under a spinlock) is needed in the completion upcall. |
| 30 | * |
| 31 | * But this means that frwr_op_map() can occasionally encounter an MR |
| 32 | * that is INVALID but the LOCAL_INV WR has not completed. Work Queue |
| 33 | * ordering prevents a subsequent FAST_REG WR from executing against |
| 34 | * that MR while it is still being invalidated. |
| 35 | */ |
| 36 | |
| 37 | /* Transport recovery |
| 38 | * |
| 39 | * ->op_map and the transport connect worker cannot run at the same |
| 40 | * time, but ->op_unmap can fire while the transport connect worker |
| 41 | * is running. Thus MR recovery is handled in ->op_map, to guarantee |
| 42 | * that recovered MRs are owned by a sending RPC, and not one where |
| 43 | * ->op_unmap could fire at the same time transport reconnect is |
| 44 | * being done. |
| 45 | * |
| 46 | * When the underlying transport disconnects, MRs are left in one of |
| 47 | * three states: |
| 48 | * |
| 49 | * INVALID: The MR was not in use before the QP entered ERROR state. |
| 50 | * (Or, the LOCAL_INV WR has not completed or flushed yet). |
| 51 | * |
| 52 | * STALE: The MR was being registered or unregistered when the QP |
| 53 | * entered ERROR state, and the pending WR was flushed. |
| 54 | * |
| 55 | * VALID: The MR was registered before the QP entered ERROR state. |
| 56 | * |
| 57 | * When frwr_op_map encounters STALE and VALID MRs, they are recovered |
| 58 | * with ib_dereg_mr and then are re-initialized. Beause MR recovery |
| 59 | * allocates fresh resources, it is deferred to a workqueue, and the |
| 60 | * recovered MRs are placed back on the rb_mws list when recovery is |
| 61 | * complete. frwr_op_map allocates another MR for the current RPC while |
| 62 | * the broken MR is reset. |
| 63 | * |
| 64 | * To ensure that frwr_op_map doesn't encounter an MR that is marked |
| 65 | * INVALID but that is about to be flushed due to a previous transport |
| 66 | * disconnect, the transport connect worker attempts to drain all |
| 67 | * pending send queue WRs before the transport is reconnected. |
| 68 | */ |
| 69 | |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 70 | #include "xprt_rdma.h" |
| 71 | |
| 72 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
| 73 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 74 | #endif |
| 75 | |
Chuck Lever | b54054c | 2016-06-29 13:53:27 -0400 | [diff] [blame] | 76 | bool |
| 77 | frwr_is_supported(struct rpcrdma_ia *ia) |
| 78 | { |
| 79 | struct ib_device_attr *attrs = &ia->ri_device->attrs; |
| 80 | |
| 81 | if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) |
| 82 | goto out_not_supported; |
| 83 | if (attrs->max_fast_reg_page_list_len == 0) |
| 84 | goto out_not_supported; |
| 85 | return true; |
| 86 | |
| 87 | out_not_supported: |
| 88 | pr_info("rpcrdma: 'frwr' mode is not supported by device %s\n", |
| 89 | ia->ri_device->name); |
| 90 | return false; |
| 91 | } |
| 92 | |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 93 | static int |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 94 | frwr_op_init_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r) |
Chuck Lever | d48b1d2 | 2016-06-29 13:52:29 -0400 | [diff] [blame] | 95 | { |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 96 | unsigned int depth = ia->ri_max_frmr_depth; |
Chuck Lever | d48b1d2 | 2016-06-29 13:52:29 -0400 | [diff] [blame] | 97 | struct rpcrdma_frmr *f = &r->frmr; |
| 98 | int rc; |
| 99 | |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 100 | f->fr_mr = ib_alloc_mr(ia->ri_pd, IB_MR_TYPE_MEM_REG, depth); |
Chuck Lever | d48b1d2 | 2016-06-29 13:52:29 -0400 | [diff] [blame] | 101 | if (IS_ERR(f->fr_mr)) |
| 102 | goto out_mr_err; |
| 103 | |
| 104 | r->mw_sg = kcalloc(depth, sizeof(*r->mw_sg), GFP_KERNEL); |
| 105 | if (!r->mw_sg) |
| 106 | goto out_list_err; |
| 107 | |
| 108 | sg_init_table(r->mw_sg, depth); |
| 109 | init_completion(&f->fr_linv_done); |
| 110 | return 0; |
| 111 | |
| 112 | out_mr_err: |
| 113 | rc = PTR_ERR(f->fr_mr); |
| 114 | dprintk("RPC: %s: ib_alloc_mr status %i\n", |
| 115 | __func__, rc); |
| 116 | return rc; |
| 117 | |
| 118 | out_list_err: |
| 119 | rc = -ENOMEM; |
| 120 | dprintk("RPC: %s: sg allocation failure\n", |
| 121 | __func__); |
| 122 | ib_dereg_mr(f->fr_mr); |
| 123 | return rc; |
| 124 | } |
| 125 | |
| 126 | static void |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 127 | frwr_op_release_mr(struct rpcrdma_mw *r) |
Chuck Lever | d48b1d2 | 2016-06-29 13:52:29 -0400 | [diff] [blame] | 128 | { |
| 129 | int rc; |
| 130 | |
| 131 | rc = ib_dereg_mr(r->frmr.fr_mr); |
| 132 | if (rc) |
| 133 | pr_err("rpcrdma: final ib_dereg_mr for %p returned %i\n", |
| 134 | r, rc); |
| 135 | kfree(r->mw_sg); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 136 | kfree(r); |
Chuck Lever | d48b1d2 | 2016-06-29 13:52:29 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static int |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 140 | __frwr_reset_mr(struct rpcrdma_ia *ia, struct rpcrdma_mw *r) |
| 141 | { |
| 142 | struct rpcrdma_frmr *f = &r->frmr; |
| 143 | int rc; |
| 144 | |
| 145 | rc = ib_dereg_mr(f->fr_mr); |
| 146 | if (rc) { |
| 147 | pr_warn("rpcrdma: ib_dereg_mr status %d, frwr %p orphaned\n", |
| 148 | rc, r); |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | f->fr_mr = ib_alloc_mr(ia->ri_pd, IB_MR_TYPE_MEM_REG, |
| 153 | ia->ri_max_frmr_depth); |
| 154 | if (IS_ERR(f->fr_mr)) { |
| 155 | pr_warn("rpcrdma: ib_alloc_mr status %ld, frwr %p orphaned\n", |
| 156 | PTR_ERR(f->fr_mr), r); |
| 157 | return PTR_ERR(f->fr_mr); |
| 158 | } |
| 159 | |
| 160 | dprintk("RPC: %s: recovered FRMR %p\n", __func__, r); |
| 161 | f->fr_state = FRMR_IS_INVALID; |
| 162 | return 0; |
| 163 | } |
| 164 | |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 165 | /* Reset of a single FRMR. Generate a fresh rkey by replacing the MR. |
| 166 | * |
| 167 | * There's no recovery if this fails. The FRMR is abandoned, but |
| 168 | * remains in rb_all. It will be cleaned up when the transport is |
| 169 | * destroyed. |
| 170 | */ |
Chuck Lever | 660bb49 | 2016-05-02 14:42:21 -0400 | [diff] [blame] | 171 | static void |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 172 | frwr_op_recover_mr(struct rpcrdma_mw *mw) |
Chuck Lever | 660bb49 | 2016-05-02 14:42:21 -0400 | [diff] [blame] | 173 | { |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 174 | struct rpcrdma_xprt *r_xprt = mw->mw_xprt; |
Chuck Lever | 660bb49 | 2016-05-02 14:42:21 -0400 | [diff] [blame] | 175 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
Chuck Lever | 660bb49 | 2016-05-02 14:42:21 -0400 | [diff] [blame] | 176 | int rc; |
| 177 | |
| 178 | rc = __frwr_reset_mr(ia, mw); |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 179 | ib_dma_unmap_sg(ia->ri_device, mw->mw_sg, mw->mw_nents, mw->mw_dir); |
Chuck Lever | 2ffc871 | 2016-06-29 13:54:08 -0400 | [diff] [blame^] | 180 | if (rc) |
| 181 | goto out_release; |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 182 | |
Chuck Lever | 660bb49 | 2016-05-02 14:42:21 -0400 | [diff] [blame] | 183 | rpcrdma_put_mw(r_xprt, mw); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 184 | r_xprt->rx_stats.mrs_recovered++; |
Chuck Lever | 2ffc871 | 2016-06-29 13:54:08 -0400 | [diff] [blame^] | 185 | return; |
| 186 | |
| 187 | out_release: |
| 188 | pr_err("rpcrdma: FRMR reset failed %d, %p release\n", rc, mw); |
| 189 | r_xprt->rx_stats.mrs_orphaned++; |
| 190 | |
| 191 | spin_lock(&r_xprt->rx_buf.rb_mwlock); |
| 192 | list_del(&mw->mw_all); |
| 193 | spin_unlock(&r_xprt->rx_buf.rb_mwlock); |
| 194 | |
| 195 | frwr_op_release_mr(mw); |
Chuck Lever | 951e721 | 2015-05-26 11:52:25 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame] | 198 | static int |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 199 | frwr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep, |
| 200 | struct rpcrdma_create_data_internal *cdata) |
| 201 | { |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 202 | int depth, delta; |
| 203 | |
| 204 | ia->ri_max_frmr_depth = |
| 205 | min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS, |
Or Gerlitz | e3e45b1 | 2015-12-18 10:59:48 +0200 | [diff] [blame] | 206 | ia->ri_device->attrs.max_fast_reg_page_list_len); |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 207 | dprintk("RPC: %s: device's max FR page list len = %u\n", |
| 208 | __func__, ia->ri_max_frmr_depth); |
| 209 | |
| 210 | /* Add room for frmr register and invalidate WRs. |
| 211 | * 1. FRMR reg WR for head |
| 212 | * 2. FRMR invalidate WR for head |
| 213 | * 3. N FRMR reg WRs for pagelist |
| 214 | * 4. N FRMR invalidate WRs for pagelist |
| 215 | * 5. FRMR reg WR for tail |
| 216 | * 6. FRMR invalidate WR for tail |
| 217 | * 7. The RDMA_SEND WR |
| 218 | */ |
| 219 | depth = 7; |
| 220 | |
| 221 | /* Calculate N if the device max FRMR depth is smaller than |
| 222 | * RPCRDMA_MAX_DATA_SEGS. |
| 223 | */ |
| 224 | if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) { |
| 225 | delta = RPCRDMA_MAX_DATA_SEGS - ia->ri_max_frmr_depth; |
| 226 | do { |
| 227 | depth += 2; /* FRMR reg + invalidate */ |
| 228 | delta -= ia->ri_max_frmr_depth; |
| 229 | } while (delta > 0); |
| 230 | } |
| 231 | |
| 232 | ep->rep_attr.cap.max_send_wr *= depth; |
Or Gerlitz | e3e45b1 | 2015-12-18 10:59:48 +0200 | [diff] [blame] | 233 | if (ep->rep_attr.cap.max_send_wr > ia->ri_device->attrs.max_qp_wr) { |
| 234 | cdata->max_requests = ia->ri_device->attrs.max_qp_wr / depth; |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 235 | if (!cdata->max_requests) |
| 236 | return -EINVAL; |
| 237 | ep->rep_attr.cap.max_send_wr = cdata->max_requests * |
| 238 | depth; |
| 239 | } |
| 240 | |
Chuck Lever | 302d3de | 2016-05-02 14:41:05 -0400 | [diff] [blame] | 241 | rpcrdma_set_max_header_sizes(ia, cdata, max_t(unsigned int, 1, |
| 242 | RPCRDMA_MAX_DATA_SEGS / |
| 243 | ia->ri_max_frmr_depth)); |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 247 | /* FRWR mode conveys a list of pages per chunk segment. The |
| 248 | * maximum length of that list is the FRWR page list depth. |
| 249 | */ |
| 250 | static size_t |
| 251 | frwr_op_maxpages(struct rpcrdma_xprt *r_xprt) |
| 252 | { |
| 253 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 254 | |
| 255 | return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS, |
Chuck Lever | 9493174 | 2016-05-02 14:40:56 -0400 | [diff] [blame] | 256 | RPCRDMA_MAX_HDR_SEGS * ia->ri_max_frmr_depth); |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Chuck Lever | e46ac34 | 2015-03-30 14:35:35 -0400 | [diff] [blame] | 259 | static void |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 260 | __frwr_sendcompletion_flush(struct ib_wc *wc, struct rpcrdma_frmr *frmr, |
| 261 | const char *wr) |
Chuck Lever | e46ac34 | 2015-03-30 14:35:35 -0400 | [diff] [blame] | 262 | { |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 263 | frmr->fr_state = FRMR_IS_STALE; |
| 264 | if (wc->status != IB_WC_WR_FLUSH_ERR) |
| 265 | pr_err("rpcrdma: %s: %s (%u/0x%x)\n", |
| 266 | wr, ib_wc_status_msg(wc->status), |
| 267 | wc->status, wc->vendor_err); |
Chuck Lever | e46ac34 | 2015-03-30 14:35:35 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 270 | /** |
| 271 | * frwr_wc_fastreg - Invoked by RDMA provider for each polled FastReg WC |
| 272 | * @cq: completion queue (ignored) |
| 273 | * @wc: completed WR |
| 274 | * |
| 275 | */ |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 276 | static void |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 277 | frwr_wc_fastreg(struct ib_cq *cq, struct ib_wc *wc) |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 278 | { |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 279 | struct rpcrdma_frmr *frmr; |
| 280 | struct ib_cqe *cqe; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 281 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 282 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
| 283 | if (wc->status != IB_WC_SUCCESS) { |
| 284 | cqe = wc->wr_cqe; |
| 285 | frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe); |
| 286 | __frwr_sendcompletion_flush(wc, frmr, "fastreg"); |
| 287 | } |
| 288 | } |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 289 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 290 | /** |
| 291 | * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC |
| 292 | * @cq: completion queue (ignored) |
| 293 | * @wc: completed WR |
| 294 | * |
| 295 | */ |
| 296 | static void |
| 297 | frwr_wc_localinv(struct ib_cq *cq, struct ib_wc *wc) |
| 298 | { |
| 299 | struct rpcrdma_frmr *frmr; |
| 300 | struct ib_cqe *cqe; |
| 301 | |
| 302 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
| 303 | if (wc->status != IB_WC_SUCCESS) { |
| 304 | cqe = wc->wr_cqe; |
| 305 | frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe); |
| 306 | __frwr_sendcompletion_flush(wc, frmr, "localinv"); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * frwr_wc_localinv - Invoked by RDMA provider for each polled LocalInv WC |
| 312 | * @cq: completion queue (ignored) |
| 313 | * @wc: completed WR |
| 314 | * |
| 315 | * Awaken anyone waiting for an MR to finish being fenced. |
| 316 | */ |
| 317 | static void |
| 318 | frwr_wc_localinv_wake(struct ib_cq *cq, struct ib_wc *wc) |
| 319 | { |
| 320 | struct rpcrdma_frmr *frmr; |
| 321 | struct ib_cqe *cqe; |
| 322 | |
| 323 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
| 324 | cqe = wc->wr_cqe; |
| 325 | frmr = container_of(cqe, struct rpcrdma_frmr, fr_cqe); |
| 326 | if (wc->status != IB_WC_SUCCESS) |
| 327 | __frwr_sendcompletion_flush(wc, frmr, "localinv"); |
| 328 | complete_all(&frmr->fr_linv_done); |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 329 | } |
| 330 | |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 331 | /* Post a REG_MR Work Request to register a memory region |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 332 | * for remote access via RDMA READ or RDMA WRITE. |
| 333 | */ |
| 334 | static int |
| 335 | frwr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg, |
| 336 | int nsegs, bool writing) |
| 337 | { |
| 338 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 339 | struct rpcrdma_mr_seg *seg1 = seg; |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 340 | struct rpcrdma_mw *mw; |
| 341 | struct rpcrdma_frmr *frmr; |
| 342 | struct ib_mr *mr; |
Chuck Lever | 3cf4e16 | 2015-12-16 17:22:31 -0500 | [diff] [blame] | 343 | struct ib_reg_wr *reg_wr; |
Christoph Hellwig | e622f2f | 2015-10-08 09:16:33 +0100 | [diff] [blame] | 344 | struct ib_send_wr *bad_wr; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 345 | int rc, i, n, dma_nents; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 346 | u8 key; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 347 | |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 348 | mw = seg1->rl_mw; |
| 349 | seg1->rl_mw = NULL; |
| 350 | do { |
| 351 | if (mw) |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 352 | rpcrdma_defer_mr_recovery(mw); |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 353 | mw = rpcrdma_get_mw(r_xprt); |
| 354 | if (!mw) |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 355 | return -ENOBUFS; |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 356 | } while (mw->frmr.fr_state != FRMR_IS_INVALID); |
| 357 | frmr = &mw->frmr; |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 358 | frmr->fr_state = FRMR_IS_VALID; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 359 | mr = frmr->fr_mr; |
Chuck Lever | 3cf4e16 | 2015-12-16 17:22:31 -0500 | [diff] [blame] | 360 | reg_wr = &frmr->fr_regwr; |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 361 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 362 | if (nsegs > ia->ri_max_frmr_depth) |
| 363 | nsegs = ia->ri_max_frmr_depth; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 364 | for (i = 0; i < nsegs;) { |
| 365 | if (seg->mr_page) |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 366 | sg_set_page(&mw->mw_sg[i], |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 367 | seg->mr_page, |
| 368 | seg->mr_len, |
| 369 | offset_in_page(seg->mr_offset)); |
| 370 | else |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 371 | sg_set_buf(&mw->mw_sg[i], seg->mr_offset, |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 372 | seg->mr_len); |
| 373 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 374 | ++seg; |
| 375 | ++i; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 376 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 377 | /* Check for holes */ |
| 378 | if ((i < nsegs && offset_in_page(seg->mr_offset)) || |
| 379 | offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) |
| 380 | break; |
| 381 | } |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 382 | mw->mw_nents = i; |
| 383 | mw->mw_dir = rpcrdma_data_dir(writing); |
Chuck Lever | a54d405 | 2016-06-29 13:53:52 -0400 | [diff] [blame] | 384 | if (i == 0) |
| 385 | goto out_dmamap_err; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 386 | |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 387 | dma_nents = ib_dma_map_sg(ia->ri_device, |
| 388 | mw->mw_sg, mw->mw_nents, mw->mw_dir); |
| 389 | if (!dma_nents) |
| 390 | goto out_dmamap_err; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 391 | |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 392 | n = ib_map_mr_sg(mr, mw->mw_sg, mw->mw_nents, NULL, PAGE_SIZE); |
| 393 | if (unlikely(n != mw->mw_nents)) |
| 394 | goto out_mapmr_err; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 395 | |
| 396 | dprintk("RPC: %s: Using frmr %p to map %u segments (%u bytes)\n", |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 397 | __func__, mw, mw->mw_nents, mr->length); |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 398 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 399 | key = (u8)(mr->rkey & 0x000000FF); |
| 400 | ib_update_fast_reg_key(mr, ++key); |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 401 | |
Chuck Lever | 3cf4e16 | 2015-12-16 17:22:31 -0500 | [diff] [blame] | 402 | reg_wr->wr.next = NULL; |
| 403 | reg_wr->wr.opcode = IB_WR_REG_MR; |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 404 | frmr->fr_cqe.done = frwr_wc_fastreg; |
| 405 | reg_wr->wr.wr_cqe = &frmr->fr_cqe; |
Chuck Lever | 3cf4e16 | 2015-12-16 17:22:31 -0500 | [diff] [blame] | 406 | reg_wr->wr.num_sge = 0; |
| 407 | reg_wr->wr.send_flags = 0; |
| 408 | reg_wr->mr = mr; |
| 409 | reg_wr->key = mr->rkey; |
| 410 | reg_wr->access = writing ? |
| 411 | IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE : |
| 412 | IB_ACCESS_REMOTE_READ; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 413 | |
| 414 | DECR_CQCOUNT(&r_xprt->rx_ep); |
Chuck Lever | 3cf4e16 | 2015-12-16 17:22:31 -0500 | [diff] [blame] | 415 | rc = ib_post_send(ia->ri_id->qp, ®_wr->wr, &bad_wr); |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 416 | if (rc) |
| 417 | goto out_senderr; |
| 418 | |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 419 | seg1->rl_mw = mw; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 420 | seg1->mr_rkey = mr->rkey; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 421 | seg1->mr_base = mr->iova; |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 422 | seg1->mr_nsegs = mw->mw_nents; |
Sagi Grimberg | 4143f34 | 2015-10-13 19:11:35 +0300 | [diff] [blame] | 423 | seg1->mr_len = mr->length; |
| 424 | |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 425 | return mw->mw_nents; |
| 426 | |
| 427 | out_dmamap_err: |
| 428 | pr_err("rpcrdma: failed to dma map sg %p sg_nents %u\n", |
| 429 | mw->mw_sg, mw->mw_nents); |
Chuck Lever | 42fe28f | 2016-06-29 13:53:02 -0400 | [diff] [blame] | 430 | rpcrdma_defer_mr_recovery(mw); |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 431 | return -EIO; |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 432 | |
| 433 | out_mapmr_err: |
| 434 | pr_err("rpcrdma: failed to map mr %p (%u/%u)\n", |
| 435 | frmr->fr_mr, n, mw->mw_nents); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 436 | rpcrdma_defer_mr_recovery(mw); |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 437 | return -EIO; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 438 | |
| 439 | out_senderr: |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 440 | pr_err("rpcrdma: FRMR registration ib_post_send returned %i\n", rc); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 441 | rpcrdma_defer_mr_recovery(mw); |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 442 | return -ENOTCONN; |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 445 | static struct ib_send_wr * |
| 446 | __frwr_prepare_linv_wr(struct rpcrdma_mr_seg *seg) |
| 447 | { |
| 448 | struct rpcrdma_mw *mw = seg->rl_mw; |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 449 | struct rpcrdma_frmr *f = &mw->frmr; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 450 | struct ib_send_wr *invalidate_wr; |
| 451 | |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 452 | f->fr_state = FRMR_IS_INVALID; |
| 453 | invalidate_wr = &f->fr_invwr; |
| 454 | |
| 455 | memset(invalidate_wr, 0, sizeof(*invalidate_wr)); |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 456 | f->fr_cqe.done = frwr_wc_localinv; |
| 457 | invalidate_wr->wr_cqe = &f->fr_cqe; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 458 | invalidate_wr->opcode = IB_WR_LOCAL_INV; |
| 459 | invalidate_wr->ex.invalidate_rkey = f->fr_mr->rkey; |
| 460 | |
| 461 | return invalidate_wr; |
| 462 | } |
| 463 | |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 464 | /* Invalidate all memory regions that were registered for "req". |
| 465 | * |
| 466 | * Sleeps until it is safe for the host CPU to access the |
| 467 | * previously mapped memory regions. |
| 468 | */ |
| 469 | static void |
| 470 | frwr_op_unmap_sync(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req) |
| 471 | { |
| 472 | struct ib_send_wr *invalidate_wrs, *pos, *prev, *bad_wr; |
| 473 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 474 | struct rpcrdma_mr_seg *seg; |
| 475 | unsigned int i, nchunks; |
| 476 | struct rpcrdma_frmr *f; |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 477 | struct rpcrdma_mw *mw; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 478 | int rc; |
| 479 | |
| 480 | dprintk("RPC: %s: req %p\n", __func__, req); |
| 481 | |
| 482 | /* ORDER: Invalidate all of the req's MRs first |
| 483 | * |
| 484 | * Chain the LOCAL_INV Work Requests and post them with |
| 485 | * a single ib_post_send() call. |
| 486 | */ |
| 487 | invalidate_wrs = pos = prev = NULL; |
| 488 | seg = NULL; |
| 489 | for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) { |
| 490 | seg = &req->rl_segments[i]; |
| 491 | |
| 492 | pos = __frwr_prepare_linv_wr(seg); |
| 493 | |
| 494 | if (!invalidate_wrs) |
| 495 | invalidate_wrs = pos; |
| 496 | else |
| 497 | prev->next = pos; |
| 498 | prev = pos; |
| 499 | |
| 500 | i += seg->mr_nsegs; |
| 501 | } |
Chuck Lever | c882a65 | 2016-03-04 11:28:45 -0500 | [diff] [blame] | 502 | f = &seg->rl_mw->frmr; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 503 | |
| 504 | /* Strong send queue ordering guarantees that when the |
| 505 | * last WR in the chain completes, all WRs in the chain |
| 506 | * are complete. |
| 507 | */ |
| 508 | f->fr_invwr.send_flags = IB_SEND_SIGNALED; |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 509 | f->fr_cqe.done = frwr_wc_localinv_wake; |
| 510 | reinit_completion(&f->fr_linv_done); |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 511 | INIT_CQCOUNT(&r_xprt->rx_ep); |
| 512 | |
| 513 | /* Transport disconnect drains the receive CQ before it |
| 514 | * replaces the QP. The RPC reply handler won't call us |
| 515 | * unless ri_id->qp is a valid pointer. |
| 516 | */ |
| 517 | rc = ib_post_send(ia->ri_id->qp, invalidate_wrs, &bad_wr); |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 518 | if (rc) |
| 519 | goto reset_mrs; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 520 | |
| 521 | wait_for_completion(&f->fr_linv_done); |
| 522 | |
| 523 | /* ORDER: Now DMA unmap all of the req's MRs, and return |
| 524 | * them to the free MW list. |
| 525 | */ |
Chuck Lever | b892a69 | 2016-03-04 11:28:01 -0500 | [diff] [blame] | 526 | unmap: |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 527 | for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) { |
| 528 | seg = &req->rl_segments[i]; |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 529 | mw = seg->rl_mw; |
| 530 | seg->rl_mw = NULL; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 531 | |
Chuck Lever | 564471d | 2016-06-29 13:52:21 -0400 | [diff] [blame] | 532 | ib_dma_unmap_sg(ia->ri_device, |
| 533 | mw->mw_sg, mw->mw_nents, mw->mw_dir); |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 534 | rpcrdma_put_mw(r_xprt, mw); |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 535 | |
| 536 | i += seg->mr_nsegs; |
| 537 | seg->mr_nsegs = 0; |
| 538 | } |
| 539 | |
| 540 | req->rl_nchunks = 0; |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 541 | return; |
| 542 | |
| 543 | reset_mrs: |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 544 | pr_err("rpcrdma: FRMR invalidate ib_post_send returned %i\n", rc); |
| 545 | rdma_disconnect(ia->ri_id); |
Chuck Lever | d7a21c1 | 2016-05-02 14:42:12 -0400 | [diff] [blame] | 546 | |
| 547 | /* Find and reset the MRs in the LOCAL_INV WRs that did not |
| 548 | * get posted. This is synchronous, and slow. |
| 549 | */ |
| 550 | for (i = 0, nchunks = req->rl_nchunks; nchunks; nchunks--) { |
| 551 | seg = &req->rl_segments[i]; |
| 552 | mw = seg->rl_mw; |
| 553 | f = &mw->frmr; |
| 554 | |
| 555 | if (mw->frmr.fr_mr->rkey == bad_wr->ex.invalidate_rkey) { |
| 556 | __frwr_reset_mr(ia, mw); |
| 557 | bad_wr = bad_wr->next; |
| 558 | } |
| 559 | |
| 560 | i += seg->mr_nsegs; |
| 561 | } |
| 562 | goto unmap; |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 563 | } |
| 564 | |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 565 | /* Use a slow, safe mechanism to invalidate all memory regions |
| 566 | * that were registered for "req". |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 567 | */ |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 568 | static void |
| 569 | frwr_op_unmap_safe(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req, |
| 570 | bool sync) |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 571 | { |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 572 | struct rpcrdma_mr_seg *seg; |
| 573 | struct rpcrdma_mw *mw; |
| 574 | unsigned int i; |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 575 | |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 576 | for (i = 0; req->rl_nchunks; req->rl_nchunks--) { |
| 577 | seg = &req->rl_segments[i]; |
| 578 | mw = seg->rl_mw; |
Chuck Lever | c14d86e | 2015-05-26 11:52:35 -0400 | [diff] [blame] | 579 | |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 580 | if (sync) |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 581 | frwr_op_recover_mr(mw); |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 582 | else |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 583 | rpcrdma_defer_mr_recovery(mw); |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 584 | |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 585 | i += seg->mr_nsegs; |
| 586 | seg->mr_nsegs = 0; |
| 587 | seg->rl_mw = NULL; |
| 588 | } |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 591 | const struct rpcrdma_memreg_ops rpcrdma_frwr_memreg_ops = { |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 592 | .ro_map = frwr_op_map, |
Chuck Lever | c9918ff | 2015-12-16 17:22:47 -0500 | [diff] [blame] | 593 | .ro_unmap_sync = frwr_op_unmap_sync, |
Chuck Lever | ead3f26 | 2016-05-02 14:42:46 -0400 | [diff] [blame] | 594 | .ro_unmap_safe = frwr_op_unmap_safe, |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 595 | .ro_recover_mr = frwr_op_recover_mr, |
Chuck Lever | 3968cb5 | 2015-03-30 14:35:26 -0400 | [diff] [blame] | 596 | .ro_open = frwr_op_open, |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 597 | .ro_maxpages = frwr_op_maxpages, |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 598 | .ro_init_mr = frwr_op_init_mr, |
| 599 | .ro_release_mr = frwr_op_release_mr, |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 600 | .ro_displayname = "frwr", |
| 601 | }; |