2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/vmalloc.h>
35 #include <rdma/ib_umem.h>
36 #include "hns_roce_device.h"
38 void hns_roce_buf_free(struct hns_roce_dev
*hr_dev
, struct hns_roce_buf
*buf
)
40 struct hns_roce_buf_list
*trunks
;
46 trunks
= buf
->trunk_list
;
48 buf
->trunk_list
= NULL
;
49 for (i
= 0; i
< buf
->ntrunks
; i
++)
50 dma_free_coherent(hr_dev
->dev
, 1 << buf
->trunk_shift
,
51 trunks
[i
].buf
, trunks
[i
].map
);
60 * Allocate the dma buffer for storing ROCEE table entries
62 * @size: required size
63 * @page_shift: the unit size in a continuous dma address range
64 * @flags: HNS_ROCE_BUF_ flags to control the allocation flow.
66 struct hns_roce_buf
*hns_roce_buf_alloc(struct hns_roce_dev
*hr_dev
, u32 size
,
67 u32 page_shift
, u32 flags
)
69 u32 trunk_size
, page_size
, alloced_size
;
70 struct hns_roce_buf_list
*trunks
;
71 struct hns_roce_buf
*buf
;
75 /* The minimum shift of the page accessed by hw is HNS_HW_PAGE_SHIFT */
76 if (WARN_ON(page_shift
< HNS_HW_PAGE_SHIFT
))
77 return ERR_PTR(-EINVAL
);
79 gfp_flags
= (flags
& HNS_ROCE_BUF_NOSLEEP
) ? GFP_ATOMIC
: GFP_KERNEL
;
80 buf
= kzalloc(sizeof(*buf
), gfp_flags
);
82 return ERR_PTR(-ENOMEM
);
84 buf
->page_shift
= page_shift
;
85 page_size
= 1 << buf
->page_shift
;
87 /* Calc the trunk size and num by required size and page_shift */
88 if (flags
& HNS_ROCE_BUF_DIRECT
) {
89 buf
->trunk_shift
= order_base_2(ALIGN(size
, PAGE_SIZE
));
92 buf
->trunk_shift
= order_base_2(ALIGN(page_size
, PAGE_SIZE
));
93 ntrunk
= DIV_ROUND_UP(size
, 1 << buf
->trunk_shift
);
96 trunks
= kcalloc(ntrunk
, sizeof(*trunks
), gfp_flags
);
99 return ERR_PTR(-ENOMEM
);
102 trunk_size
= 1 << buf
->trunk_shift
;
104 for (i
= 0; i
< ntrunk
; i
++) {
105 trunks
[i
].buf
= dma_alloc_coherent(hr_dev
->dev
, trunk_size
,
106 &trunks
[i
].map
, gfp_flags
);
110 alloced_size
+= trunk_size
;
115 /* In nofail mode, it's only failed when the alloced size is 0 */
116 if ((flags
& HNS_ROCE_BUF_NOFAIL
) ? i
== 0 : i
!= ntrunk
) {
117 for (i
= 0; i
< buf
->ntrunks
; i
++)
118 dma_free_coherent(hr_dev
->dev
, trunk_size
,
119 trunks
[i
].buf
, trunks
[i
].map
);
123 return ERR_PTR(-ENOMEM
);
126 buf
->npages
= DIV_ROUND_UP(alloced_size
, page_size
);
127 buf
->trunk_list
= trunks
;
132 int hns_roce_get_kmem_bufs(struct hns_roce_dev
*hr_dev
, dma_addr_t
*bufs
,
133 int buf_cnt
, struct hns_roce_buf
*buf
,
134 unsigned int page_shift
)
136 unsigned int offset
, max_size
;
140 if (page_shift
> buf
->trunk_shift
) {
141 dev_err(hr_dev
->dev
, "failed to check kmem buf shift %u > %u\n",
142 page_shift
, buf
->trunk_shift
);
147 max_size
= buf
->ntrunks
<< buf
->trunk_shift
;
148 for (i
= 0; i
< buf_cnt
&& offset
< max_size
; i
++) {
149 bufs
[total
++] = hns_roce_buf_dma_addr(buf
, offset
);
150 offset
+= (1 << page_shift
);
156 int hns_roce_get_umem_bufs(dma_addr_t
*bufs
, int buf_cnt
, struct ib_umem
*umem
,
157 unsigned int page_shift
)
159 struct ib_block_iter biter
;
162 /* convert system page cnt to hw page cnt */
163 rdma_umem_for_each_dma_block(umem
, &biter
, 1 << page_shift
) {
164 bufs
[total
++] = rdma_block_iter_dma_address(&biter
);
165 if (total
>= buf_cnt
)
173 void hns_roce_cleanup_bitmap(struct hns_roce_dev
*hr_dev
)
175 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_XRC
)
176 ida_destroy(&hr_dev
->xrcd_ida
.ida
);
178 if (hr_dev
->caps
.flags
& HNS_ROCE_CAP_FLAG_SRQ
)
179 ida_destroy(&hr_dev
->srq_table
.srq_ida
.ida
);
180 hns_roce_cleanup_qp_table(hr_dev
);
181 hns_roce_cleanup_cq_table(hr_dev
);
182 ida_destroy(&hr_dev
->mr_table
.mtpt_ida
.ida
);
183 ida_destroy(&hr_dev
->pd_ida
.ida
);
184 ida_destroy(&hr_dev
->uar_ida
.ida
);