2015-03-31 01:34:21 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Oracle. All rights reserved.
|
|
|
|
* Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Lightweight memory registration using Fast Memory Regions (FMR).
|
|
|
|
* Referred to sometimes as MTHCAFMR mode.
|
|
|
|
*
|
|
|
|
* FMR uses synchronous memory registration and deregistration.
|
|
|
|
* FMR registration is known to be fast, but FMR deregistration
|
|
|
|
* can take tens of usecs to complete.
|
|
|
|
*/
|
|
|
|
|
2015-05-26 22:52:16 +07:00
|
|
|
/* Normal operation
|
|
|
|
*
|
|
|
|
* A Memory Region is prepared for RDMA READ or WRITE using the
|
|
|
|
* ib_map_phys_fmr verb (fmr_op_map). When the RDMA operation is
|
|
|
|
* finished, the Memory Region is unmapped using the ib_unmap_fmr
|
|
|
|
* verb (fmr_op_unmap).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Transport recovery
|
|
|
|
*
|
|
|
|
* After a transport reconnect, fmr_op_map re-uses the MR already
|
|
|
|
* allocated for the RPC, but generates a fresh rkey then maps the
|
|
|
|
* MR again. This process is synchronous.
|
|
|
|
*/
|
|
|
|
|
2015-03-31 01:34:21 +07:00
|
|
|
#include "xprt_rdma.h"
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
|
|
|
|
# define RPCDBG_FACILITY RPCDBG_TRANS
|
|
|
|
#endif
|
|
|
|
|
2015-03-31 01:34:30 +07:00
|
|
|
/* Maximum scatter/gather per FMR */
|
|
|
|
#define RPCRDMA_MAX_FMR_SGES (64)
|
|
|
|
|
2015-03-31 01:35:26 +07:00
|
|
|
static int
|
|
|
|
fmr_op_open(struct rpcrdma_ia *ia, struct rpcrdma_ep *ep,
|
|
|
|
struct rpcrdma_create_data_internal *cdata)
|
|
|
|
{
|
2015-08-04 00:03:30 +07:00
|
|
|
struct ib_device_attr *devattr = &ia->ri_devattr;
|
|
|
|
struct ib_mr *mr;
|
|
|
|
|
|
|
|
/* Obtain an lkey to use for the regbufs, which are
|
|
|
|
* protected from remote access.
|
|
|
|
*/
|
|
|
|
if (devattr->device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
|
|
|
|
ia->ri_dma_lkey = ia->ri_device->local_dma_lkey;
|
|
|
|
} else {
|
|
|
|
mr = ib_get_dma_mr(ia->ri_pd, IB_ACCESS_LOCAL_WRITE);
|
|
|
|
if (IS_ERR(mr)) {
|
|
|
|
pr_err("%s: ib_get_dma_mr for failed with %lX\n",
|
|
|
|
__func__, PTR_ERR(mr));
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
ia->ri_dma_lkey = ia->ri_dma_mr->lkey;
|
|
|
|
ia->ri_dma_mr = mr;
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:35:26 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:34:30 +07:00
|
|
|
/* FMR mode conveys up to 64 pages of payload per chunk segment.
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
fmr_op_maxpages(struct rpcrdma_xprt *r_xprt)
|
|
|
|
{
|
|
|
|
return min_t(unsigned int, RPCRDMA_MAX_DATA_SEGS,
|
|
|
|
rpcrdma_max_segments(r_xprt) * RPCRDMA_MAX_FMR_SGES);
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:34:58 +07:00
|
|
|
static int
|
|
|
|
fmr_op_init(struct rpcrdma_xprt *r_xprt)
|
|
|
|
{
|
|
|
|
struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
|
|
|
|
int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ;
|
|
|
|
struct ib_fmr_attr fmr_attr = {
|
|
|
|
.max_pages = RPCRDMA_MAX_FMR_SGES,
|
|
|
|
.max_maps = 1,
|
|
|
|
.page_shift = PAGE_SHIFT
|
|
|
|
};
|
|
|
|
struct ib_pd *pd = r_xprt->rx_ia.ri_pd;
|
|
|
|
struct rpcrdma_mw *r;
|
|
|
|
int i, rc;
|
|
|
|
|
2015-05-26 22:53:13 +07:00
|
|
|
spin_lock_init(&buf->rb_mwlock);
|
2015-03-31 01:34:58 +07:00
|
|
|
INIT_LIST_HEAD(&buf->rb_mws);
|
|
|
|
INIT_LIST_HEAD(&buf->rb_all);
|
|
|
|
|
2015-05-26 22:53:33 +07:00
|
|
|
i = max_t(int, RPCRDMA_MAX_DATA_SEGS / RPCRDMA_MAX_FMR_SGES, 1);
|
|
|
|
i += 2; /* head + tail */
|
|
|
|
i *= buf->rb_max_requests; /* one set for each RPC slot */
|
|
|
|
dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i);
|
2015-03-31 01:34:58 +07:00
|
|
|
|
2015-05-26 22:53:23 +07:00
|
|
|
rc = -ENOMEM;
|
2015-03-31 01:34:58 +07:00
|
|
|
while (i--) {
|
|
|
|
r = kzalloc(sizeof(*r), GFP_KERNEL);
|
|
|
|
if (!r)
|
2015-05-26 22:53:23 +07:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
r->r.fmr.physaddrs = kmalloc(RPCRDMA_MAX_FMR_SGES *
|
|
|
|
sizeof(u64), GFP_KERNEL);
|
|
|
|
if (!r->r.fmr.physaddrs)
|
|
|
|
goto out_free;
|
2015-03-31 01:34:58 +07:00
|
|
|
|
2015-05-26 22:53:23 +07:00
|
|
|
r->r.fmr.fmr = ib_alloc_fmr(pd, mr_access_flags, &fmr_attr);
|
|
|
|
if (IS_ERR(r->r.fmr.fmr))
|
2015-03-31 01:34:58 +07:00
|
|
|
goto out_fmr_err;
|
|
|
|
|
|
|
|
list_add(&r->mw_list, &buf->rb_mws);
|
|
|
|
list_add(&r->mw_all, &buf->rb_all);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_fmr_err:
|
2015-05-26 22:53:23 +07:00
|
|
|
rc = PTR_ERR(r->r.fmr.fmr);
|
2015-03-31 01:34:58 +07:00
|
|
|
dprintk("RPC: %s: ib_alloc_fmr status %i\n", __func__, rc);
|
2015-05-26 22:53:23 +07:00
|
|
|
kfree(r->r.fmr.physaddrs);
|
|
|
|
out_free:
|
2015-03-31 01:34:58 +07:00
|
|
|
kfree(r);
|
2015-05-26 22:53:23 +07:00
|
|
|
out:
|
2015-03-31 01:34:58 +07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-26 22:52:16 +07:00
|
|
|
static int
|
|
|
|
__fmr_unmap(struct rpcrdma_mw *r)
|
|
|
|
{
|
|
|
|
LIST_HEAD(l);
|
|
|
|
|
2015-05-26 22:53:23 +07:00
|
|
|
list_add(&r->r.fmr.fmr->list, &l);
|
2015-05-26 22:52:16 +07:00
|
|
|
return ib_unmap_fmr(&l);
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:34:39 +07:00
|
|
|
/* Use the ib_map_phys_fmr() verb to register a memory region
|
|
|
|
* for remote access via RDMA READ or RDMA WRITE.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fmr_op_map(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg,
|
|
|
|
int nsegs, bool writing)
|
|
|
|
{
|
|
|
|
struct rpcrdma_ia *ia = &r_xprt->rx_ia;
|
2015-05-26 22:51:56 +07:00
|
|
|
struct ib_device *device = ia->ri_device;
|
2015-03-31 01:35:44 +07:00
|
|
|
enum dma_data_direction direction = rpcrdma_data_dir(writing);
|
2015-03-31 01:34:39 +07:00
|
|
|
struct rpcrdma_mr_seg *seg1 = seg;
|
|
|
|
int len, pageoff, i, rc;
|
2015-05-26 22:52:16 +07:00
|
|
|
struct rpcrdma_mw *mw;
|
|
|
|
|
|
|
|
mw = seg1->rl_mw;
|
|
|
|
seg1->rl_mw = NULL;
|
|
|
|
if (!mw) {
|
|
|
|
mw = rpcrdma_get_mw(r_xprt);
|
|
|
|
if (!mw)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
|
|
|
/* this is a retransmit; generate a fresh rkey */
|
|
|
|
rc = __fmr_unmap(mw);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
2015-03-31 01:34:39 +07:00
|
|
|
|
|
|
|
pageoff = offset_in_page(seg1->mr_offset);
|
|
|
|
seg1->mr_offset -= pageoff; /* start of page */
|
|
|
|
seg1->mr_len += pageoff;
|
|
|
|
len = -pageoff;
|
|
|
|
if (nsegs > RPCRDMA_MAX_FMR_SGES)
|
|
|
|
nsegs = RPCRDMA_MAX_FMR_SGES;
|
|
|
|
for (i = 0; i < nsegs;) {
|
2015-03-31 01:35:44 +07:00
|
|
|
rpcrdma_map_one(device, seg, direction);
|
2015-05-26 22:53:23 +07:00
|
|
|
mw->r.fmr.physaddrs[i] = seg->mr_dma;
|
2015-03-31 01:34:39 +07:00
|
|
|
len += seg->mr_len;
|
|
|
|
++seg;
|
|
|
|
++i;
|
|
|
|
/* Check for holes */
|
|
|
|
if ((i < nsegs && offset_in_page(seg->mr_offset)) ||
|
|
|
|
offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-26 22:53:23 +07:00
|
|
|
rc = ib_map_phys_fmr(mw->r.fmr.fmr, mw->r.fmr.physaddrs,
|
|
|
|
i, seg1->mr_dma);
|
2015-03-31 01:34:39 +07:00
|
|
|
if (rc)
|
|
|
|
goto out_maperr;
|
|
|
|
|
2015-05-26 22:52:16 +07:00
|
|
|
seg1->rl_mw = mw;
|
2015-05-26 22:53:23 +07:00
|
|
|
seg1->mr_rkey = mw->r.fmr.fmr->rkey;
|
2015-03-31 01:34:39 +07:00
|
|
|
seg1->mr_base = seg1->mr_dma + pageoff;
|
|
|
|
seg1->mr_nsegs = i;
|
|
|
|
seg1->mr_len = len;
|
|
|
|
return i;
|
|
|
|
|
|
|
|
out_maperr:
|
|
|
|
dprintk("RPC: %s: ib_map_phys_fmr %u@0x%llx+%i (%d) status %i\n",
|
|
|
|
__func__, len, (unsigned long long)seg1->mr_dma,
|
|
|
|
pageoff, i, rc);
|
|
|
|
while (i--)
|
2015-03-31 01:35:44 +07:00
|
|
|
rpcrdma_unmap_one(device, --seg);
|
2015-03-31 01:34:39 +07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:34:48 +07:00
|
|
|
/* Use the ib_unmap_fmr() verb to prevent further remote
|
|
|
|
* access via RDMA READ or RDMA WRITE.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fmr_op_unmap(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mr_seg *seg)
|
|
|
|
{
|
|
|
|
struct rpcrdma_ia *ia = &r_xprt->rx_ia;
|
|
|
|
struct rpcrdma_mr_seg *seg1 = seg;
|
2015-05-26 22:52:16 +07:00
|
|
|
struct rpcrdma_mw *mw = seg1->rl_mw;
|
2015-03-31 01:34:48 +07:00
|
|
|
int rc, nsegs = seg->mr_nsegs;
|
|
|
|
|
2015-05-26 22:52:16 +07:00
|
|
|
dprintk("RPC: %s: FMR %p\n", __func__, mw);
|
|
|
|
|
|
|
|
seg1->rl_mw = NULL;
|
2015-03-31 01:34:48 +07:00
|
|
|
while (seg1->mr_nsegs--)
|
2015-05-26 22:51:56 +07:00
|
|
|
rpcrdma_unmap_one(ia->ri_device, seg++);
|
2015-05-26 22:52:16 +07:00
|
|
|
rc = __fmr_unmap(mw);
|
2015-03-31 01:34:48 +07:00
|
|
|
if (rc)
|
|
|
|
goto out_err;
|
2015-05-26 22:52:16 +07:00
|
|
|
rpcrdma_put_mw(r_xprt, mw);
|
2015-03-31 01:34:48 +07:00
|
|
|
return nsegs;
|
|
|
|
|
|
|
|
out_err:
|
2015-05-26 22:52:16 +07:00
|
|
|
/* The FMR is abandoned, but remains in rb_all. fmr_op_destroy
|
|
|
|
* will attempt to release it when the transport is destroyed.
|
|
|
|
*/
|
2015-03-31 01:34:48 +07:00
|
|
|
dprintk("RPC: %s: ib_unmap_fmr status %i\n", __func__, rc);
|
|
|
|
return nsegs;
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:35:17 +07:00
|
|
|
static void
|
|
|
|
fmr_op_destroy(struct rpcrdma_buffer *buf)
|
|
|
|
{
|
|
|
|
struct rpcrdma_mw *r;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
while (!list_empty(&buf->rb_all)) {
|
|
|
|
r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all);
|
|
|
|
list_del(&r->mw_all);
|
2015-05-26 22:53:23 +07:00
|
|
|
kfree(r->r.fmr.physaddrs);
|
|
|
|
|
|
|
|
rc = ib_dealloc_fmr(r->r.fmr.fmr);
|
2015-03-31 01:35:17 +07:00
|
|
|
if (rc)
|
|
|
|
dprintk("RPC: %s: ib_dealloc_fmr failed %i\n",
|
|
|
|
__func__, rc);
|
2015-05-26 22:53:23 +07:00
|
|
|
|
2015-03-31 01:35:17 +07:00
|
|
|
kfree(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 01:34:21 +07:00
|
|
|
const struct rpcrdma_memreg_ops rpcrdma_fmr_memreg_ops = {
|
2015-03-31 01:34:39 +07:00
|
|
|
.ro_map = fmr_op_map,
|
2015-03-31 01:34:48 +07:00
|
|
|
.ro_unmap = fmr_op_unmap,
|
2015-03-31 01:35:26 +07:00
|
|
|
.ro_open = fmr_op_open,
|
2015-03-31 01:34:30 +07:00
|
|
|
.ro_maxpages = fmr_op_maxpages,
|
2015-03-31 01:34:58 +07:00
|
|
|
.ro_init = fmr_op_init,
|
2015-03-31 01:35:17 +07:00
|
|
|
.ro_destroy = fmr_op_destroy,
|
2015-03-31 01:34:21 +07:00
|
|
|
.ro_displayname = "fmr",
|
|
|
|
};
|