1 /*
2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34 #include <linux/platform_device.h>
35 #include <linux/vmalloc.h>
36 #include "hns_roce_device.h"
37 #include <rdma/ib_umem.h>
38
hns_roce_buf_free(struct hns_roce_dev * hr_dev,struct hns_roce_buf * buf)39 void hns_roce_buf_free(struct hns_roce_dev *hr_dev, struct hns_roce_buf *buf)
40 {
41 struct hns_roce_buf_list *trunks;
42 u32 i;
43
44 if (!buf)
45 return;
46
47 trunks = buf->trunk_list;
48 if (trunks) {
49 buf->trunk_list = NULL;
50 for (i = 0; i < buf->ntrunks; i++)
51 dma_free_coherent(hr_dev->dev, 1 << buf->trunk_shift,
52 trunks[i].buf, trunks[i].map);
53
54 kfree(trunks);
55 }
56
57 kfree(buf);
58 }
59
60 /*
61 * Allocate the dma buffer for storing ROCEE table entries
62 *
63 * @size: required size
64 * @page_shift: the unit size in a continuous dma address range
65 * @flags: HNS_ROCE_BUF_ flags to control the allocation flow.
66 */
hns_roce_buf_alloc(struct hns_roce_dev * hr_dev,u32 size,u32 page_shift,u32 flags)67 struct hns_roce_buf *hns_roce_buf_alloc(struct hns_roce_dev *hr_dev, u32 size,
68 u32 page_shift, u32 flags)
69 {
70 u32 trunk_size, page_size, alloced_size;
71 struct hns_roce_buf_list *trunks;
72 struct hns_roce_buf *buf;
73 gfp_t gfp_flags;
74 u32 ntrunk, i;
75
76 /* The minimum shift of the page accessed by hw is HNS_HW_PAGE_SHIFT */
77 if (WARN_ON(page_shift < HNS_HW_PAGE_SHIFT))
78 return ERR_PTR(-EINVAL);
79
80 gfp_flags = (flags & HNS_ROCE_BUF_NOSLEEP) ? GFP_ATOMIC : GFP_KERNEL;
81 buf = kzalloc(sizeof(*buf), gfp_flags);
82 if (!buf)
83 return ERR_PTR(-ENOMEM);
84
85 buf->page_shift = page_shift;
86 page_size = 1 << buf->page_shift;
87
88 /* Calc the trunk size and num by required size and page_shift */
89 if (flags & HNS_ROCE_BUF_DIRECT) {
90 buf->trunk_shift = order_base_2(ALIGN(size, PAGE_SIZE));
91 ntrunk = 1;
92 } else {
93 buf->trunk_shift = order_base_2(ALIGN(page_size, PAGE_SIZE));
94 ntrunk = DIV_ROUND_UP(size, 1 << buf->trunk_shift);
95 }
96
97 trunks = kcalloc(ntrunk, sizeof(*trunks), gfp_flags);
98 if (!trunks) {
99 kfree(buf);
100 return ERR_PTR(-ENOMEM);
101 }
102
103 trunk_size = 1 << buf->trunk_shift;
104 alloced_size = 0;
105 for (i = 0; i < ntrunk; i++) {
106 trunks[i].buf = dma_alloc_coherent(hr_dev->dev, trunk_size,
107 &trunks[i].map, gfp_flags);
108 if (!trunks[i].buf)
109 break;
110
111 alloced_size += trunk_size;
112 }
113
114 buf->ntrunks = i;
115
116 /* In nofail mode, it's only failed when the alloced size is 0 */
117 if ((flags & HNS_ROCE_BUF_NOFAIL) ? i == 0 : i != ntrunk) {
118 for (i = 0; i < buf->ntrunks; i++)
119 dma_free_coherent(hr_dev->dev, trunk_size,
120 trunks[i].buf, trunks[i].map);
121
122 kfree(trunks);
123 kfree(buf);
124 return ERR_PTR(-ENOMEM);
125 }
126
127 buf->npages = DIV_ROUND_UP(alloced_size, page_size);
128 buf->trunk_list = trunks;
129
130 return buf;
131 }
132
hns_roce_get_kmem_bufs(struct hns_roce_dev * hr_dev,dma_addr_t * bufs,int buf_cnt,struct hns_roce_buf * buf,unsigned int page_shift)133 int hns_roce_get_kmem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
134 int buf_cnt, struct hns_roce_buf *buf,
135 unsigned int page_shift)
136 {
137 unsigned int offset, max_size;
138 int total = 0;
139 int i;
140
141 if (page_shift > buf->trunk_shift) {
142 dev_err(hr_dev->dev, "failed to check kmem buf shift %u > %u\n",
143 page_shift, buf->trunk_shift);
144 return -EINVAL;
145 }
146
147 offset = 0;
148 max_size = buf->ntrunks << buf->trunk_shift;
149 for (i = 0; i < buf_cnt && offset < max_size; i++) {
150 bufs[total++] = hns_roce_buf_dma_addr(buf, offset);
151 offset += (1 << page_shift);
152 }
153
154 return total;
155 }
156
hns_roce_get_umem_bufs(struct hns_roce_dev * hr_dev,dma_addr_t * bufs,int buf_cnt,struct ib_umem * umem,unsigned int page_shift)157 int hns_roce_get_umem_bufs(struct hns_roce_dev *hr_dev, dma_addr_t *bufs,
158 int buf_cnt, struct ib_umem *umem,
159 unsigned int page_shift)
160 {
161 struct ib_block_iter biter;
162 int total = 0;
163
164 /* convert system page cnt to hw page cnt */
165 rdma_umem_for_each_dma_block(umem, &biter, 1 << page_shift) {
166 bufs[total++] = rdma_block_iter_dma_address(&biter);
167 if (total >= buf_cnt)
168 goto done;
169 }
170
171 done:
172 return total;
173 }
174
hns_roce_cleanup_bitmap(struct hns_roce_dev * hr_dev)175 void hns_roce_cleanup_bitmap(struct hns_roce_dev *hr_dev)
176 {
177 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)
178 ida_destroy(&hr_dev->xrcd_ida.ida);
179
180 if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_SRQ)
181 ida_destroy(&hr_dev->srq_table.srq_ida.ida);
182 hns_roce_cleanup_qp_table(hr_dev);
183 hns_roce_cleanup_cq_table(hr_dev);
184 ida_destroy(&hr_dev->mr_table.mtpt_ida.ida);
185 ida_destroy(&hr_dev->pd_ida.ida);
186 ida_destroy(&hr_dev->uar_ida.ida);
187 }
188