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 struct rds_ib_mr
*rds_ib_alloc_fmr(struct rds_ib_device
*rds_ibdev
, int npages
)
37 struct rds_ib_mr_pool
*pool
;
38 struct rds_ib_mr
*ibmr
= NULL
;
39 struct rds_ib_fmr
*fmr
;
42 if (npages
<= RDS_MR_8K_MSG_SIZE
)
43 pool
= rds_ibdev
->mr_8k_pool
;
45 pool
= rds_ibdev
->mr_1m_pool
;
47 ibmr
= rds_ib_try_reuse_ibmr(pool
);
51 ibmr
= kzalloc_node(sizeof(*ibmr
), GFP_KERNEL
,
52 rdsibdev_to_node(rds_ibdev
));
59 fmr
->fmr
= ib_alloc_fmr(rds_ibdev
->pd
,
60 (IB_ACCESS_LOCAL_WRITE
|
61 IB_ACCESS_REMOTE_READ
|
62 IB_ACCESS_REMOTE_WRITE
|
63 IB_ACCESS_REMOTE_ATOMIC
),
65 if (IS_ERR(fmr
->fmr
)) {
66 err
= PTR_ERR(fmr
->fmr
);
68 pr_warn("RDS/IB: %s failed (err=%d)\n", __func__
, err
);
73 if (pool
->pool_type
== RDS_IB_MR_8K_POOL
)
74 rds_ib_stats_inc(s_ib_rdma_mr_8k_alloc
);
76 rds_ib_stats_inc(s_ib_rdma_mr_1m_alloc
);
82 atomic_dec(&pool
->item_count
);
87 static int rds_ib_map_fmr(struct rds_ib_device
*rds_ibdev
,
88 struct rds_ib_mr
*ibmr
, struct scatterlist
*sg
,
91 struct ib_device
*dev
= rds_ibdev
->dev
;
92 struct rds_ib_fmr
*fmr
= &ibmr
->u
.fmr
;
93 struct scatterlist
*scat
= sg
;
97 int page_cnt
, sg_dma_len
;
101 sg_dma_len
= ib_dma_map_sg(dev
, sg
, nents
, DMA_BIDIRECTIONAL
);
102 if (unlikely(!sg_dma_len
)) {
103 pr_warn("RDS/IB: %s failed!\n", __func__
);
110 for (i
= 0; i
< sg_dma_len
; ++i
) {
111 unsigned int dma_len
= ib_sg_dma_len(dev
, &scat
[i
]);
112 u64 dma_addr
= ib_sg_dma_address(dev
, &scat
[i
]);
114 if (dma_addr
& ~PAGE_MASK
) {
116 ib_dma_unmap_sg(dev
, sg
, nents
,
123 if ((dma_addr
+ dma_len
) & ~PAGE_MASK
) {
124 if (i
< sg_dma_len
- 1) {
125 ib_dma_unmap_sg(dev
, sg
, nents
,
136 page_cnt
+= len
>> PAGE_SHIFT
;
137 if (page_cnt
> ibmr
->pool
->fmr_attr
.max_pages
) {
138 ib_dma_unmap_sg(dev
, sg
, nents
, DMA_BIDIRECTIONAL
);
142 dma_pages
= kmalloc_array_node(sizeof(u64
), page_cnt
, GFP_ATOMIC
,
143 rdsibdev_to_node(rds_ibdev
));
145 ib_dma_unmap_sg(dev
, sg
, nents
, DMA_BIDIRECTIONAL
);
150 for (i
= 0; i
< sg_dma_len
; ++i
) {
151 unsigned int dma_len
= ib_sg_dma_len(dev
, &scat
[i
]);
152 u64 dma_addr
= ib_sg_dma_address(dev
, &scat
[i
]);
154 for (j
= 0; j
< dma_len
; j
+= PAGE_SIZE
)
155 dma_pages
[page_cnt
++] =
156 (dma_addr
& PAGE_MASK
) + j
;
159 ret
= ib_map_phys_fmr(fmr
->fmr
, dma_pages
, page_cnt
, io_addr
);
161 ib_dma_unmap_sg(dev
, sg
, nents
, DMA_BIDIRECTIONAL
);
165 /* Success - we successfully remapped the MR, so we can
166 * safely tear down the old mapping.
168 rds_ib_teardown_mr(ibmr
);
171 ibmr
->sg_len
= nents
;
172 ibmr
->sg_dma_len
= sg_dma_len
;
175 if (ibmr
->pool
->pool_type
== RDS_IB_MR_8K_POOL
)
176 rds_ib_stats_inc(s_ib_rdma_mr_8k_used
);
178 rds_ib_stats_inc(s_ib_rdma_mr_1m_used
);
187 struct rds_ib_mr
*rds_ib_reg_fmr(struct rds_ib_device
*rds_ibdev
,
188 struct scatterlist
*sg
,
192 struct rds_ib_mr
*ibmr
= NULL
;
193 struct rds_ib_fmr
*fmr
;
196 ibmr
= rds_ib_alloc_fmr(rds_ibdev
, nents
);
200 ibmr
->device
= rds_ibdev
;
202 ret
= rds_ib_map_fmr(rds_ibdev
, ibmr
, sg
, nents
);
204 *key
= fmr
->fmr
->rkey
;
206 rds_ib_free_mr(ibmr
, 0);
211 void rds_ib_unreg_fmr(struct list_head
*list
, unsigned int *nfreed
,
212 unsigned long *unpinned
, unsigned int goal
)
214 struct rds_ib_mr
*ibmr
, *next
;
215 struct rds_ib_fmr
*fmr
;
218 unsigned int freed
= *nfreed
;
220 /* String all ib_mr's onto one list and hand them to ib_unmap_fmr */
221 list_for_each_entry(ibmr
, list
, unmap_list
) {
223 list_add(&fmr
->fmr
->list
, &fmr_list
);
226 ret
= ib_unmap_fmr(&fmr_list
);
228 pr_warn("RDS/IB: FMR invalidation failed (err=%d)\n", ret
);
230 /* Now we can destroy the DMA mapping and unpin any pages */
231 list_for_each_entry_safe(ibmr
, next
, list
, unmap_list
) {
233 *unpinned
+= ibmr
->sg_len
;
234 __rds_ib_teardown_mr(ibmr
);
236 ibmr
->remap_count
>= ibmr
->pool
->fmr_attr
.max_maps
) {
237 if (ibmr
->pool
->pool_type
== RDS_IB_MR_8K_POOL
)
238 rds_ib_stats_inc(s_ib_rdma_mr_8k_free
);
240 rds_ib_stats_inc(s_ib_rdma_mr_1m_free
);
241 list_del(&ibmr
->unmap_list
);
242 ib_dealloc_fmr(fmr
->fmr
);
250 void rds_ib_free_fmr_list(struct rds_ib_mr
*ibmr
)
252 struct rds_ib_mr_pool
*pool
= ibmr
->pool
;
254 if (ibmr
->remap_count
>= pool
->fmr_attr
.max_maps
)
255 llist_add(&ibmr
->llnode
, &pool
->drop_list
);
257 llist_add(&ibmr
->llnode
, &pool
->free_list
);