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 | |
| 14 | #include "xprt_rdma.h" |
| 15 | |
| 16 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
| 17 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 18 | #endif |
| 19 | |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 20 | /* Maximum scatter/gather per FMR */ |
| 21 | #define RPCRDMA_MAX_FMR_SGES (64) |
| 22 | |
| 23 | /* FMR mode conveys up to 64 pages of payload per chunk segment. |
| 24 | */ |
| 25 | static size_t |
| 26 | fmr_op_maxpages(struct rpcrdma_xprt *r_xprt) |
| 27 | { |
| 28 | return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS, |
| 29 | rpcrdma_max_segments(r_xprt) * RPCRDMA_MAX_FMR_SGES); |
| 30 | } |
| 31 | |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame^] | 32 | static int |
| 33 | fmr_op_init(struct rpcrdma_xprt *r_xprt) |
| 34 | { |
| 35 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 36 | int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ; |
| 37 | struct ib_fmr_attr fmr_attr = { |
| 38 | .max_pages = RPCRDMA_MAX_FMR_SGES, |
| 39 | .max_maps = 1, |
| 40 | .page_shift = PAGE_SHIFT |
| 41 | }; |
| 42 | struct ib_pd *pd = r_xprt->rx_ia.ri_pd; |
| 43 | struct rpcrdma_mw *r; |
| 44 | int i, rc; |
| 45 | |
| 46 | INIT_LIST_HEAD(&buf->rb_mws); |
| 47 | INIT_LIST_HEAD(&buf->rb_all); |
| 48 | |
| 49 | i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; |
| 50 | dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i); |
| 51 | |
| 52 | while (i--) { |
| 53 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 54 | if (!r) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | r->r.fmr = ib_alloc_fmr(pd, mr_access_flags, &fmr_attr); |
| 58 | if (IS_ERR(r->r.fmr)) |
| 59 | goto out_fmr_err; |
| 60 | |
| 61 | list_add(&r->mw_list, &buf->rb_mws); |
| 62 | list_add(&r->mw_all, &buf->rb_all); |
| 63 | } |
| 64 | return 0; |
| 65 | |
| 66 | out_fmr_err: |
| 67 | rc = PTR_ERR(r->r.fmr); |
| 68 | dprintk("RPC: %s: ib_alloc_fmr status %i\n", __func__, rc); |
| 69 | kfree(r); |
| 70 | return rc; |
| 71 | } |
| 72 | |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 73 | /* Use the ib_map_phys_fmr() verb to register a memory region |
| 74 | * for remote access via RDMA READ or RDMA WRITE. |
| 75 | */ |
| 76 | static int |
| 77 | fmr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg, |
| 78 | int nsegs, bool writing) |
| 79 | { |
| 80 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 81 | struct rpcrdma_mr_seg *seg1 = seg; |
| 82 | struct rpcrdma_mw *mw = seg1->rl_mw; |
| 83 | u64 physaddrs[RPCRDMA_MAX_DATA_SEGS]; |
| 84 | int len, pageoff, i, rc; |
| 85 | |
| 86 | pageoff = offset_in_page(seg1->mr_offset); |
| 87 | seg1->mr_offset -= pageoff; /* start of page */ |
| 88 | seg1->mr_len += pageoff; |
| 89 | len = -pageoff; |
| 90 | if (nsegs > RPCRDMA_MAX_FMR_SGES) |
| 91 | nsegs = RPCRDMA_MAX_FMR_SGES; |
| 92 | for (i = 0; i < nsegs;) { |
| 93 | rpcrdma_map_one(ia, seg, writing); |
| 94 | physaddrs[i] = seg->mr_dma; |
| 95 | len += seg->mr_len; |
| 96 | ++seg; |
| 97 | ++i; |
| 98 | /* Check for holes */ |
| 99 | if ((i < nsegs && offset_in_page(seg->mr_offset)) || |
| 100 | offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | rc = ib_map_phys_fmr(mw->r.fmr, physaddrs, i, seg1->mr_dma); |
| 105 | if (rc) |
| 106 | goto out_maperr; |
| 107 | |
| 108 | seg1->mr_rkey = mw->r.fmr->rkey; |
| 109 | seg1->mr_base = seg1->mr_dma + pageoff; |
| 110 | seg1->mr_nsegs = i; |
| 111 | seg1->mr_len = len; |
| 112 | return i; |
| 113 | |
| 114 | out_maperr: |
| 115 | dprintk("RPC: %s: ib_map_phys_fmr %u@0x%llx+%i (%d) status %i\n", |
| 116 | __func__, len, (unsigned long long)seg1->mr_dma, |
| 117 | pageoff, i, rc); |
| 118 | while (i--) |
| 119 | rpcrdma_unmap_one(ia, --seg); |
| 120 | return rc; |
| 121 | } |
| 122 | |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 123 | /* Use the ib_unmap_fmr() verb to prevent further remote |
| 124 | * access via RDMA READ or RDMA WRITE. |
| 125 | */ |
| 126 | static int |
| 127 | fmr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg) |
| 128 | { |
| 129 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 130 | struct rpcrdma_mr_seg *seg1 = seg; |
| 131 | int rc, nsegs = seg->mr_nsegs; |
| 132 | LIST_HEAD(l); |
| 133 | |
| 134 | list_add(&seg1->rl_mw->r.fmr->list, &l); |
| 135 | rc = ib_unmap_fmr(&l); |
| 136 | read_lock(&ia->ri_qplock); |
| 137 | while (seg1->mr_nsegs--) |
| 138 | rpcrdma_unmap_one(ia, seg++); |
| 139 | read_unlock(&ia->ri_qplock); |
| 140 | if (rc) |
| 141 | goto out_err; |
| 142 | return nsegs; |
| 143 | |
| 144 | out_err: |
| 145 | dprintk("RPC: %s: ib_unmap_fmr status %i\n", __func__, rc); |
| 146 | return nsegs; |
| 147 | } |
| 148 | |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 149 | const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = { |
Chuck Lever | 9c1b4d7 | 2015-03-30 14:34:39 -0400 | [diff] [blame] | 150 | .ro_map = fmr_op_map, |
Chuck Lever | 6814bae | 2015-03-30 14:34:48 -0400 | [diff] [blame] | 151 | .ro_unmap = fmr_op_unmap, |
Chuck Lever | 1c9351e | 2015-03-30 14:34:30 -0400 | [diff] [blame] | 152 | .ro_maxpages = fmr_op_maxpages, |
Chuck Lever | 91e70e7 | 2015-03-30 14:34:58 -0400 | [diff] [blame^] | 153 | .ro_init = fmr_op_init, |
Chuck Lever | a0ce85f | 2015-03-30 14:34:21 -0400 | [diff] [blame] | 154 | .ro_displayname = "fmr", |
| 155 | }; |