GNU Linux-libre 4.9.314-gnu1
[releases.git] / net / rds / ib_fmr.c
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         struct rds_ib_fmr *fmr;
40         int err = 0;
41
42         if (npages <= RDS_MR_8K_MSG_SIZE)
43                 pool = rds_ibdev->mr_8k_pool;
44         else
45                 pool = rds_ibdev->mr_1m_pool;
46
47         if (atomic_read(&pool->dirty_count) >= pool->max_items / 10)
48                 queue_delayed_work(rds_ib_mr_wq, &pool->flush_worker, 10);
49
50         /* Switch pools if one of the pool is reaching upper limit */
51         if (atomic_read(&pool->dirty_count) >=  pool->max_items * 9 / 10) {
52                 if (pool->pool_type == RDS_IB_MR_8K_POOL)
53                         pool = rds_ibdev->mr_1m_pool;
54                 else
55                         pool = rds_ibdev->mr_8k_pool;
56         }
57
58         ibmr = rds_ib_try_reuse_ibmr(pool);
59         if (ibmr)
60                 return ibmr;
61
62         ibmr = kzalloc_node(sizeof(*ibmr), GFP_KERNEL,
63                             rdsibdev_to_node(rds_ibdev));
64         if (!ibmr) {
65                 err = -ENOMEM;
66                 goto out_no_cigar;
67         }
68
69         fmr = &ibmr->u.fmr;
70         fmr->fmr = ib_alloc_fmr(rds_ibdev->pd,
71                         (IB_ACCESS_LOCAL_WRITE |
72                          IB_ACCESS_REMOTE_READ |
73                          IB_ACCESS_REMOTE_WRITE |
74                          IB_ACCESS_REMOTE_ATOMIC),
75                         &pool->fmr_attr);
76         if (IS_ERR(fmr->fmr)) {
77                 err = PTR_ERR(fmr->fmr);
78                 fmr->fmr = NULL;
79                 pr_warn("RDS/IB: %s failed (err=%d)\n", __func__, err);
80                 goto out_no_cigar;
81         }
82
83         ibmr->pool = pool;
84         if (pool->pool_type == RDS_IB_MR_8K_POOL)
85                 rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc);
86         else
87                 rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc);
88
89         return ibmr;
90
91 out_no_cigar:
92         if (ibmr) {
93                 if (fmr->fmr)
94                         ib_dealloc_fmr(fmr->fmr);
95                 kfree(ibmr);
96         }
97         atomic_dec(&pool->item_count);
98         return ERR_PTR(err);
99 }
100
101 int rds_ib_map_fmr(struct rds_ib_device *rds_ibdev, struct rds_ib_mr *ibmr,
102                    struct scatterlist *sg, unsigned int nents)
103 {
104         struct ib_device *dev = rds_ibdev->dev;
105         struct rds_ib_fmr *fmr = &ibmr->u.fmr;
106         struct scatterlist *scat = sg;
107         u64 io_addr = 0;
108         u64 *dma_pages;
109         u32 len;
110         int page_cnt, sg_dma_len;
111         int i, j;
112         int ret;
113
114         sg_dma_len = ib_dma_map_sg(dev, sg, nents, DMA_BIDIRECTIONAL);
115         if (unlikely(!sg_dma_len)) {
116                 pr_warn("RDS/IB: %s failed!\n", __func__);
117                 return -EBUSY;
118         }
119
120         len = 0;
121         page_cnt = 0;
122
123         for (i = 0; i < sg_dma_len; ++i) {
124                 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
125                 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
126
127                 if (dma_addr & ~PAGE_MASK) {
128                         if (i > 0)
129                                 return -EINVAL;
130                         else
131                                 ++page_cnt;
132                 }
133                 if ((dma_addr + dma_len) & ~PAGE_MASK) {
134                         if (i < sg_dma_len - 1)
135                                 return -EINVAL;
136                         else
137                                 ++page_cnt;
138                 }
139
140                 len += dma_len;
141         }
142
143         page_cnt += len >> PAGE_SHIFT;
144         if (page_cnt > ibmr->pool->fmr_attr.max_pages)
145                 return -EINVAL;
146
147         dma_pages = kmalloc_node(sizeof(u64) * page_cnt, GFP_ATOMIC,
148                                  rdsibdev_to_node(rds_ibdev));
149         if (!dma_pages)
150                 return -ENOMEM;
151
152         page_cnt = 0;
153         for (i = 0; i < sg_dma_len; ++i) {
154                 unsigned int dma_len = ib_sg_dma_len(dev, &scat[i]);
155                 u64 dma_addr = ib_sg_dma_address(dev, &scat[i]);
156
157                 for (j = 0; j < dma_len; j += PAGE_SIZE)
158                         dma_pages[page_cnt++] =
159                                 (dma_addr & PAGE_MASK) + j;
160         }
161
162         ret = ib_map_phys_fmr(fmr->fmr, dma_pages, page_cnt, io_addr);
163         if (ret)
164                 goto out;
165
166         /* Success - we successfully remapped the MR, so we can
167          * safely tear down the old mapping.
168          */
169         rds_ib_teardown_mr(ibmr);
170
171         ibmr->sg = scat;
172         ibmr->sg_len = nents;
173         ibmr->sg_dma_len = sg_dma_len;
174         ibmr->remap_count++;
175
176         if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
177                 rds_ib_stats_inc(s_ib_rdma_mr_8k_used);
178         else
179                 rds_ib_stats_inc(s_ib_rdma_mr_1m_used);
180         ret = 0;
181
182 out:
183         kfree(dma_pages);
184
185         return ret;
186 }
187
188 struct rds_ib_mr *rds_ib_reg_fmr(struct rds_ib_device *rds_ibdev,
189                                  struct scatterlist *sg,
190                                  unsigned long nents,
191                                  u32 *key)
192 {
193         struct rds_ib_mr *ibmr = NULL;
194         struct rds_ib_fmr *fmr;
195         int ret;
196
197         ibmr = rds_ib_alloc_fmr(rds_ibdev, nents);
198         if (IS_ERR(ibmr))
199                 return ibmr;
200
201         ibmr->device = rds_ibdev;
202         fmr = &ibmr->u.fmr;
203         ret = rds_ib_map_fmr(rds_ibdev, ibmr, sg, nents);
204         if (ret == 0)
205                 *key = fmr->fmr->rkey;
206         else
207                 rds_ib_free_mr(ibmr, 0);
208
209         return ibmr;
210 }
211
212 void rds_ib_unreg_fmr(struct list_head *list, unsigned int *nfreed,
213                       unsigned long *unpinned, unsigned int goal)
214 {
215         struct rds_ib_mr *ibmr, *next;
216         struct rds_ib_fmr *fmr;
217         LIST_HEAD(fmr_list);
218         int ret = 0;
219         unsigned int freed = *nfreed;
220
221         /* String all ib_mr's onto one list and hand them to  ib_unmap_fmr */
222         list_for_each_entry(ibmr, list, unmap_list) {
223                 fmr = &ibmr->u.fmr;
224                 list_add(&fmr->fmr->list, &fmr_list);
225         }
226
227         ret = ib_unmap_fmr(&fmr_list);
228         if (ret)
229                 pr_warn("RDS/IB: FMR invalidation failed (err=%d)\n", ret);
230
231         /* Now we can destroy the DMA mapping and unpin any pages */
232         list_for_each_entry_safe(ibmr, next, list, unmap_list) {
233                 fmr = &ibmr->u.fmr;
234                 *unpinned += ibmr->sg_len;
235                 __rds_ib_teardown_mr(ibmr);
236                 if (freed < goal ||
237                     ibmr->remap_count >= ibmr->pool->fmr_attr.max_maps) {
238                         if (ibmr->pool->pool_type == RDS_IB_MR_8K_POOL)
239                                 rds_ib_stats_inc(s_ib_rdma_mr_8k_free);
240                         else
241                                 rds_ib_stats_inc(s_ib_rdma_mr_1m_free);
242                         list_del(&ibmr->unmap_list);
243                         ib_dealloc_fmr(fmr->fmr);
244                         kfree(ibmr);
245                         freed++;
246                 }
247         }
248         *nfreed = freed;
249 }
250
251 void rds_ib_free_fmr_list(struct rds_ib_mr *ibmr)
252 {
253         struct rds_ib_mr_pool *pool = ibmr->pool;
254
255         if (ibmr->remap_count >= pool->fmr_attr.max_maps)
256                 llist_add(&ibmr->llnode, &pool->drop_list);
257         else
258                 llist_add(&ibmr->llnode, &pool->free_list);
259 }