santosh.shilimkar@oracle.com | f6df683 | 2016-03-01 15:20:46 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016 Oracle. All rights reserved. |
| 3 | * |
| 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: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 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. |
| 22 | * |
| 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 |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #include "ib_mr.h" |
| 34 | |
| 35 | struct rds_ib_mr *rds_ib_alloc_fmr(struct rds_ib_device *rds_ibdev, int npages) |
| 36 | { |
| 37 | struct rds_ib_mr_pool *pool; |
| 38 | struct rds_ib_mr *ibmr = NULL; |
| 39 | int err = 0, iter = 0; |
| 40 | |
| 41 | if (npages <= RDS_MR_8K_MSG_SIZE) |
| 42 | pool = rds_ibdev->mr_8k_pool; |
| 43 | else |
| 44 | pool = rds_ibdev->mr_1m_pool; |
| 45 | |
| 46 | if (atomic_read(&pool->dirty_count) >= pool->max_items / 10) |
| 47 | queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10); |
| 48 | |
| 49 | /* Switch pools if one of the pool is reaching upper limit */ |
| 50 | if (atomic_read(&pool->dirty_count) >= pool->max_items * 9 / 10) { |
| 51 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 52 | pool = rds_ibdev->mr_1m_pool; |
| 53 | else |
| 54 | pool = rds_ibdev->mr_8k_pool; |
| 55 | } |
| 56 | |
| 57 | while (1) { |
| 58 | ibmr = rds_ib_reuse_mr(pool); |
| 59 | if (ibmr) |
| 60 | return ibmr; |
| 61 | |
| 62 | /* No clean MRs - now we have the choice of either |
| 63 | * allocating a fresh MR up to the limit imposed by the |
| 64 | * driver, or flush any dirty unused MRs. |
| 65 | * We try to avoid stalling in the send path if possible, |
| 66 | * so we allocate as long as we're allowed to. |
| 67 | * |
| 68 | * We're fussy with enforcing the FMR limit, though. If the |
| 69 | * driver tells us we can't use more than N fmrs, we shouldn't |
| 70 | * start arguing with it |
| 71 | */ |
| 72 | if (atomic_inc_return(&pool->item_count) <= pool->max_items) |
| 73 | break; |
| 74 | |
| 75 | atomic_dec(&pool->item_count); |
| 76 | |
| 77 | if (++iter > 2) { |
| 78 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 79 | rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_depleted); |
| 80 | else |
| 81 | rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_depleted); |
| 82 | return ERR_PTR(-EAGAIN); |
| 83 | } |
| 84 | |
| 85 | /* We do have some empty MRs. Flush them out. */ |
| 86 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 87 | rds_ib_stats_inc(s_ib_rdma_mr_8k_pool_wait); |
| 88 | else |
| 89 | rds_ib_stats_inc(s_ib_rdma_mr_1m_pool_wait); |
| 90 | rds_ib_flush_mr_pool(pool, 0, &ibmr); |
| 91 | if (ibmr) |
| 92 | return ibmr; |
| 93 | } |
| 94 | |
| 95 | ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL, |
| 96 | rdsibdev_to_node(rds_ibdev)); |
| 97 | if (!ibmr) { |
| 98 | err = -ENOMEM; |
| 99 | goto out_no_cigar; |
| 100 | } |
| 101 | |
| 102 | ibmr->fmr = ib_alloc_fmr(rds_ibdev->pd, |
| 103 | (IB_ACCESS_LOCAL_WRITE | |
| 104 | IB_ACCESS_REMOTE_READ | |
| 105 | IB_ACCESS_REMOTE_WRITE | |
| 106 | IB_ACCESS_REMOTE_ATOMIC), |
| 107 | &pool->fmr_attr); |
| 108 | if (IS_ERR(ibmr->fmr)) { |
| 109 | err = PTR_ERR(ibmr->fmr); |
| 110 | ibmr->fmr = NULL; |
| 111 | pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, err); |
| 112 | goto out_no_cigar; |
| 113 | } |
| 114 | |
| 115 | ibmr->pool = pool; |
| 116 | if (pool->pool_type == RDS_IB_MR_8K_POOL) |
| 117 | rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc); |
| 118 | else |
| 119 | rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc); |
| 120 | |
| 121 | return ibmr; |
| 122 | |
| 123 | out_no_cigar: |
| 124 | if (ibmr) { |
| 125 | if (ibmr->fmr) |
| 126 | ib_dealloc_fmr(ibmr->fmr); |
| 127 | kfree(ibmr); |
| 128 | } |
| 129 | atomic_dec(&pool->item_count); |
| 130 | return ERR_PTR(err); |
| 131 | } |
| 132 | |
| 133 | int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr, |
| 134 | struct scatterlist *sg, unsigned int nents) |
| 135 | { |
| 136 | struct ib_device *dev = rds_ibdev->dev; |
| 137 | struct scatterlist *scat = sg; |
| 138 | u64 io_addr = 0; |
| 139 | u64 *dma_pages; |
| 140 | u32 len; |
| 141 | int page_cnt, sg_dma_len; |
| 142 | int i, j; |
| 143 | int ret; |
| 144 | |
| 145 | sg_dma_len = ib_dma_map_sg(dev, sg, nents, DMA_BIDIRECTIONAL); |
| 146 | if (unlikely(!sg_dma_len)) { |
| 147 | pr_warn("RDS/IB: %s failed!\n", __func__); |
| 148 | return -EBUSY; |
| 149 | } |
| 150 | |
| 151 | len = 0; |
| 152 | page_cnt = 0; |
| 153 | |
| 154 | for (i = 0; i < sg_dma_len; ++i) { |
| 155 | unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]); |
| 156 | u64 dma_addr = ib_sg_dma_address(dev, &scat[i]); |
| 157 | |
| 158 | if (dma_addr & ~PAGE_MASK) { |
| 159 | if (i > 0) |
| 160 | return -EINVAL; |
| 161 | else |
| 162 | ++page_cnt; |
| 163 | } |
| 164 | if ((dma_addr + dma_len) & ~PAGE_MASK) { |
| 165 | if (i < sg_dma_len - 1) |
| 166 | return -EINVAL; |
| 167 | else |
| 168 | ++page_cnt; |
| 169 | } |
| 170 | |
| 171 | len += dma_len; |
| 172 | } |
| 173 | |
| 174 | page_cnt += len >> PAGE_SHIFT; |
| 175 | if (page_cnt > ibmr->pool->fmr_attr.max_pages) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC, |
| 179 | rdsibdev_to_node(rds_ibdev)); |
| 180 | if (!dma_pages) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | page_cnt = 0; |
| 184 | for (i = 0; i < sg_dma_len; ++i) { |
| 185 | unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]); |
| 186 | u64 dma_addr = ib_sg_dma_address(dev, &scat[i]); |
| 187 | |
| 188 | for (j = 0; j < dma_len; j += PAGE_SIZE) |
| 189 | dma_pages[page_cnt++] = |
| 190 | (dma_addr & PAGE_MASK) + j; |
| 191 | } |
| 192 | |
| 193 | ret = ib_map_phys_fmr(ibmr->fmr, dma_pages, page_cnt, io_addr); |
| 194 | if (ret) |
| 195 | goto out; |
| 196 | |
| 197 | /* Success - we successfully remapped the MR, so we can |
| 198 | * safely tear down the old mapping. |
| 199 | */ |
| 200 | rds_ib_teardown_mr(ibmr); |
| 201 | |
| 202 | ibmr->sg = scat; |
| 203 | ibmr->sg_len = nents; |
| 204 | ibmr->sg_dma_len = sg_dma_len; |
| 205 | ibmr->remap_count++; |
| 206 | |
| 207 | if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL) |
| 208 | rds_ib_stats_inc(s_ib_rdma_mr_8k_used); |
| 209 | else |
| 210 | rds_ib_stats_inc(s_ib_rdma_mr_1m_used); |
| 211 | ret = 0; |
| 212 | |
| 213 | out: |
| 214 | kfree(dma_pages); |
| 215 | |
| 216 | return ret; |
| 217 | } |