2 * Copyright (c) 2016 Oracle. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
38 struct rds_ib_mr_pool *pool;
39 struct rds_ib_mr *ibmr = NULL;
40 struct rds_ib_frmr *frmr;
43 if (npages <= RDS_MR_8K_MSG_SIZE)
44 pool = rds_ibdev->mr_8k_pool;
46 pool = rds_ibdev->mr_1m_pool;
48 ibmr = rds_ib_try_reuse_ibmr(pool);
52 ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
53 rdsibdev_to_node(rds_ibdev));
60 frmr->mr = ib_alloc_mr(rds_ibdev->pd, IB_MR_TYPE_MEM_REG,
61 pool->fmr_attr.max_pages);
62 if (IS_ERR(frmr->mr)) {
63 pr_warn("RDS/IB: %s failed to allocate MR", __func__);
68 if (pool->pool_type == RDS_IB_MR_8K_POOL)
69 rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
71 rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
73 if (atomic_read(&pool->item_count) > pool->max_items_soft)
74 pool->max_items_soft = pool->max_items;
76 frmr->fr_state = FRMR_IS_FREE;
81 atomic_dec(&pool->item_count);
85 static void rds_ib_free_frmr(struct rds_ib_mr *ibmr, bool drop)
87 struct rds_ib_mr_pool *pool = ibmr->pool;
90 llist_add(&ibmr->llnode, &pool->drop_list);
92 llist_add(&ibmr->llnode, &pool->free_list);
93 atomic_add(ibmr->sg_len, &pool->free_pinned);
94 atomic_inc(&pool->dirty_count);
96 /* If we've pinned too many pages, request a flush */
97 if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned ||
98 atomic_read(&pool->dirty_count) >= pool->max_items / 5)
99 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
102 static int rds_ib_post_reg_frmr(struct rds_ib_mr *ibmr)
104 struct rds_ib_frmr *frmr = &ibmr->u.frmr;
105 struct ib_reg_wr reg_wr;
108 while (atomic_dec_return(&ibmr->ic->i_fastreg_wrs) <= 0) {
109 atomic_inc(&ibmr->ic->i_fastreg_wrs);
113 ret = ib_map_mr_sg_zbva(frmr->mr, ibmr->sg, ibmr->sg_len,
115 if (unlikely(ret != ibmr->sg_len))
116 return ret < 0 ? ret : -EINVAL;
118 /* Perform a WR for the fast_reg_mr. Each individual page
119 * in the sg list is added to the fast reg page list and placed
120 * inside the fast_reg_mr WR. The key used is a rolling 8bit
121 * counter, which should guarantee uniqueness.
123 ib_update_fast_reg_key(frmr->mr, ibmr->remap_count++);
124 frmr->fr_state = FRMR_IS_INUSE;
126 memset(®_wr, 0, sizeof(reg_wr));
127 reg_wr.wr.wr_id = (unsigned long)(void *)ibmr;
128 reg_wr.wr.opcode = IB_WR_REG_MR;
129 reg_wr.wr.num_sge = 0;
130 reg_wr.mr = frmr->mr;
131 reg_wr.key = frmr->mr->rkey;
132 reg_wr.access = IB_ACCESS_LOCAL_WRITE |
133 IB_ACCESS_REMOTE_READ |
134 IB_ACCESS_REMOTE_WRITE;
135 reg_wr.wr.send_flags = IB_SEND_SIGNALED;
137 ret = ib_post_send(ibmr->ic->i_cm_id->qp, ®_wr.wr, NULL);
139 /* Failure here can be because of -ENOMEM as well */
140 frmr->fr_state = FRMR_IS_STALE;
141 atomic_inc(&ibmr->ic->i_fastreg_wrs);
142 if (printk_ratelimit())
143 pr_warn("RDS/IB: %s returned error(%d)\n",
149 static int rds_ib_map_frmr(struct rds_ib_device *rds_ibdev,
150 struct rds_ib_mr_pool *pool,
151 struct rds_ib_mr *ibmr,
152 struct scatterlist *sg, unsigned int sg_len)
154 struct ib_device *dev = rds_ibdev->dev;
155 struct rds_ib_frmr *frmr = &ibmr->u.frmr;
160 /* We want to teardown old ibmr values here and fill it up with
163 rds_ib_teardown_mr(ibmr);
166 ibmr->sg_len = sg_len;
167 ibmr->sg_dma_len = 0;
168 frmr->sg_byte_len = 0;
169 WARN_ON(ibmr->sg_dma_len);
170 ibmr->sg_dma_len = ib_dma_map_sg(dev, ibmr->sg, ibmr->sg_len,
172 if (unlikely(!ibmr->sg_dma_len)) {
173 pr_warn("RDS/IB: %s failed!\n", __func__);
177 frmr->sg_byte_len = 0;
178 frmr->dma_npages = 0;
182 for (i = 0; i < ibmr->sg_dma_len; ++i) {
183 unsigned int dma_len = ib_sg_dma_len(dev, &ibmr->sg[i]);
184 u64 dma_addr = ib_sg_dma_address(dev, &ibmr->sg[i]);
186 frmr->sg_byte_len += dma_len;
187 if (dma_addr & ~PAGE_MASK) {
194 if ((dma_addr + dma_len) & ~PAGE_MASK) {
195 if (i < ibmr->sg_dma_len - 1)
203 frmr->dma_npages += len >> PAGE_SHIFT;
205 if (frmr->dma_npages > ibmr->pool->fmr_attr.max_pages) {
210 ret = rds_ib_post_reg_frmr(ibmr);
214 if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
215 rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
217 rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
222 ib_dma_unmap_sg(rds_ibdev->dev, ibmr->sg, ibmr->sg_len,
224 ibmr->sg_dma_len = 0;
228 static int rds_ib_post_inv(struct rds_ib_mr *ibmr)
230 struct ib_send_wr *s_wr;
231 struct rds_ib_frmr *frmr = &ibmr->u.frmr;
232 struct rdma_cm_id *i_cm_id = ibmr->ic->i_cm_id;
235 if (!i_cm_id || !i_cm_id->qp || !frmr->mr)
238 if (frmr->fr_state != FRMR_IS_INUSE)
241 while (atomic_dec_return(&ibmr->ic->i_fastunreg_wrs) <= 0) {
242 atomic_inc(&ibmr->ic->i_fastunreg_wrs);
249 memset(s_wr, 0, sizeof(*s_wr));
250 s_wr->wr_id = (unsigned long)(void *)ibmr;
251 s_wr->opcode = IB_WR_LOCAL_INV;
252 s_wr->ex.invalidate_rkey = frmr->mr->rkey;
253 s_wr->send_flags = IB_SEND_SIGNALED;
255 ret = ib_post_send(i_cm_id->qp, s_wr, NULL);
257 frmr->fr_state = FRMR_IS_STALE;
258 frmr->fr_inv = false;
259 atomic_inc(&ibmr->ic->i_fastunreg_wrs);
260 pr_err("RDS/IB: %s returned error(%d)\n", __func__, ret);
267 void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc)
269 struct rds_ib_mr *ibmr = (void *)(unsigned long)wc->wr_id;
270 struct rds_ib_frmr *frmr = &ibmr->u.frmr;
272 if (wc->status != IB_WC_SUCCESS) {
273 frmr->fr_state = FRMR_IS_STALE;
274 if (rds_conn_up(ic->conn))
275 rds_ib_conn_error(ic->conn,
276 "frmr completion <%pI4,%pI4> status %u(%s), vendor_err 0x%x, disconnecting and reconnecting\n",
280 ib_wc_status_msg(wc->status),
285 frmr->fr_state = FRMR_IS_FREE;
286 frmr->fr_inv = false;
287 atomic_inc(&ic->i_fastreg_wrs);
289 atomic_inc(&ic->i_fastunreg_wrs);
293 void rds_ib_unreg_frmr(struct list_head *list, unsigned int *nfreed,
294 unsigned long *unpinned, unsigned int goal)
296 struct rds_ib_mr *ibmr, *next;
297 struct rds_ib_frmr *frmr;
299 unsigned int freed = *nfreed;
301 /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
302 list_for_each_entry(ibmr, list, unmap_list) {
303 if (ibmr->sg_dma_len)
304 ret |= rds_ib_post_inv(ibmr);
307 pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, ret);
309 /* Now we can destroy the DMA mapping and unpin any pages */
310 list_for_each_entry_safe(ibmr, next, list, unmap_list) {
311 *unpinned += ibmr->sg_len;
312 frmr = &ibmr->u.frmr;
313 __rds_ib_teardown_mr(ibmr);
314 if (freed < goal || frmr->fr_state == FRMR_IS_STALE) {
315 /* Don't de-allocate if the MR is not free yet */
316 if (frmr->fr_state == FRMR_IS_INUSE)
319 if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
320 rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
322 rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
323 list_del(&ibmr->unmap_list);
325 ib_dereg_mr(frmr->mr);
333 struct rds_ib_mr *rds_ib_reg_frmr(struct rds_ib_device *rds_ibdev,
334 struct rds_ib_connection *ic,
335 struct scatterlist *sg,
336 unsigned long nents, u32 *key)
338 struct rds_ib_mr *ibmr = NULL;
339 struct rds_ib_frmr *frmr;
343 /* TODO: Add FRWR support for RDS_GET_MR using proxy qp*/
344 return ERR_PTR(-EOPNOTSUPP);
349 rds_ib_free_frmr(ibmr, true);
350 ibmr = rds_ib_alloc_frmr(rds_ibdev, nents);
353 frmr = &ibmr->u.frmr;
354 } while (frmr->fr_state != FRMR_IS_FREE);
357 ibmr->device = rds_ibdev;
358 ret = rds_ib_map_frmr(rds_ibdev, ibmr->pool, ibmr, sg, nents);
360 *key = frmr->mr->rkey;
362 rds_ib_free_frmr(ibmr, false);
369 void rds_ib_free_frmr_list(struct rds_ib_mr *ibmr)
371 struct rds_ib_mr_pool *pool = ibmr->pool;
372 struct rds_ib_frmr *frmr = &ibmr->u.frmr;
374 if (frmr->fr_state == FRMR_IS_STALE)
375 llist_add(&ibmr->llnode, &pool->drop_list);
377 llist_add(&ibmr->llnode, &pool->free_list);