1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/errno.h>
47#include <linux/slab.h>
48#include <linux/bitmap.h>
49
50#include "pvrdma.h"
51
52int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir,
53 u64 npages, bool alloc_pages)
54{
55 u64 i;
56
57 if (npages > PVRDMA_PAGE_DIR_MAX_PAGES)
58 return -EINVAL;
59
60 memset(pdir, 0, sizeof(*pdir));
61
62 pdir->dir = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
63 &pdir->dir_dma, GFP_KERNEL);
64 if (!pdir->dir)
65 goto err;
66
67 pdir->ntables = PVRDMA_PAGE_DIR_TABLE(npages - 1) + 1;
68 pdir->tables = kcalloc(pdir->ntables, sizeof(*pdir->tables),
69 GFP_KERNEL);
70 if (!pdir->tables)
71 goto err;
72
73 for (i = 0; i < pdir->ntables; i++) {
74 pdir->tables[i] = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
75 (dma_addr_t *)&pdir->dir[i],
76 GFP_KERNEL);
77 if (!pdir->tables[i])
78 goto err;
79 }
80
81 pdir->npages = npages;
82
83 if (alloc_pages) {
84 pdir->pages = kcalloc(npages, sizeof(*pdir->pages),
85 GFP_KERNEL);
86 if (!pdir->pages)
87 goto err;
88
89 for (i = 0; i < pdir->npages; i++) {
90 dma_addr_t page_dma;
91
92 pdir->pages[i] = dma_alloc_coherent(&dev->pdev->dev,
93 PAGE_SIZE,
94 &page_dma,
95 GFP_KERNEL);
96 if (!pdir->pages[i])
97 goto err;
98
99 pvrdma_page_dir_insert_dma(pdir, i, page_dma);
100 }
101 }
102
103 return 0;
104
105err:
106 pvrdma_page_dir_cleanup(dev, pdir);
107
108 return -ENOMEM;
109}
110
111static u64 *pvrdma_page_dir_table(struct pvrdma_page_dir *pdir, u64 idx)
112{
113 return pdir->tables[PVRDMA_PAGE_DIR_TABLE(idx)];
114}
115
116dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx)
117{
118 return pvrdma_page_dir_table(pdir, idx)[PVRDMA_PAGE_DIR_PAGE(idx)];
119}
120
121static void pvrdma_page_dir_cleanup_pages(struct pvrdma_dev *dev,
122 struct pvrdma_page_dir *pdir)
123{
124 if (pdir->pages) {
125 u64 i;
126
127 for (i = 0; i < pdir->npages && pdir->pages[i]; i++) {
128 dma_addr_t page_dma = pvrdma_page_dir_get_dma(pdir, i);
129
130 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
131 pdir->pages[i], page_dma);
132 }
133
134 kfree(pdir->pages);
135 }
136}
137
138static void pvrdma_page_dir_cleanup_tables(struct pvrdma_dev *dev,
139 struct pvrdma_page_dir *pdir)
140{
141 if (pdir->tables) {
142 int i;
143
144 pvrdma_page_dir_cleanup_pages(dev, pdir);
145
146 for (i = 0; i < pdir->ntables; i++) {
147 u64 *table = pdir->tables[i];
148
149 if (table)
150 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
151 table, pdir->dir[i]);
152 }
153
154 kfree(pdir->tables);
155 }
156}
157
158void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev,
159 struct pvrdma_page_dir *pdir)
160{
161 if (pdir->dir) {
162 pvrdma_page_dir_cleanup_tables(dev, pdir);
163 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
164 pdir->dir, pdir->dir_dma);
165 }
166}
167
168int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx,
169 dma_addr_t daddr)
170{
171 u64 *table;
172
173 if (idx >= pdir->npages)
174 return -EINVAL;
175
176 table = pvrdma_page_dir_table(pdir, idx);
177 table[PVRDMA_PAGE_DIR_PAGE(idx)] = daddr;
178
179 return 0;
180}
181
182int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir,
183 struct ib_umem *umem, u64 offset)
184{
185 u64 i = offset;
186 int ret = 0;
187 struct sg_dma_page_iter sg_iter;
188
189 if (offset >= pdir->npages)
190 return -EINVAL;
191
192 for_each_sg_dma_page(umem->sg_head.sgl, &sg_iter, umem->nmap, 0) {
193 dma_addr_t addr = sg_page_iter_dma_address(&sg_iter);
194
195 ret = pvrdma_page_dir_insert_dma(pdir, i, addr);
196 if (ret)
197 goto exit;
198
199 i++;
200 }
201
202exit:
203 return ret;
204}
205
206int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir,
207 u64 *page_list,
208 int num_pages)
209{
210 int i;
211 int ret;
212
213 if (num_pages > pdir->npages)
214 return -EINVAL;
215
216 for (i = 0; i < num_pages; i++) {
217 ret = pvrdma_page_dir_insert_dma(pdir, i, page_list[i]);
218 if (ret)
219 return ret;
220 }
221
222 return 0;
223}
224
225void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst, const struct pvrdma_qp_cap *src)
226{
227 dst->max_send_wr = src->max_send_wr;
228 dst->max_recv_wr = src->max_recv_wr;
229 dst->max_send_sge = src->max_send_sge;
230 dst->max_recv_sge = src->max_recv_sge;
231 dst->max_inline_data = src->max_inline_data;
232}
233
234void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst, const struct ib_qp_cap *src)
235{
236 dst->max_send_wr = src->max_send_wr;
237 dst->max_recv_wr = src->max_recv_wr;
238 dst->max_send_sge = src->max_send_sge;
239 dst->max_recv_sge = src->max_recv_sge;
240 dst->max_inline_data = src->max_inline_data;
241}
242
243void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src)
244{
245 BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
246 memcpy(dst, src, sizeof(*src));
247}
248
249void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src)
250{
251 BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid));
252 memcpy(dst, src, sizeof(*src));
253}
254
255void pvrdma_global_route_to_ib(struct ib_global_route *dst,
256 const struct pvrdma_global_route *src)
257{
258 pvrdma_gid_to_ib(&dst->dgid, &src->dgid);
259 dst->flow_label = src->flow_label;
260 dst->sgid_index = src->sgid_index;
261 dst->hop_limit = src->hop_limit;
262 dst->traffic_class = src->traffic_class;
263}
264
265void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst,
266 const struct ib_global_route *src)
267{
268 ib_gid_to_pvrdma(&dst->dgid, &src->dgid);
269 dst->flow_label = src->flow_label;
270 dst->sgid_index = src->sgid_index;
271 dst->hop_limit = src->hop_limit;
272 dst->traffic_class = src->traffic_class;
273}
274
275void pvrdma_ah_attr_to_rdma(struct rdma_ah_attr *dst,
276 const struct pvrdma_ah_attr *src)
277{
278 dst->type = RDMA_AH_ATTR_TYPE_ROCE;
279 pvrdma_global_route_to_ib(rdma_ah_retrieve_grh(dst), &src->grh);
280 rdma_ah_set_dlid(dst, src->dlid);
281 rdma_ah_set_sl(dst, src->sl);
282 rdma_ah_set_path_bits(dst, src->src_path_bits);
283 rdma_ah_set_static_rate(dst, src->static_rate);
284 rdma_ah_set_ah_flags(dst, src->ah_flags);
285 rdma_ah_set_port_num(dst, src->port_num);
286 memcpy(dst->roce.dmac, &src->dmac, ETH_ALEN);
287}
288
289void rdma_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst,
290 const struct rdma_ah_attr *src)
291{
292 ib_global_route_to_pvrdma(&dst->grh, rdma_ah_read_grh(src));
293 dst->dlid = rdma_ah_get_dlid(src);
294 dst->sl = rdma_ah_get_sl(src);
295 dst->src_path_bits = rdma_ah_get_path_bits(src);
296 dst->static_rate = rdma_ah_get_static_rate(src);
297 dst->ah_flags = rdma_ah_get_ah_flags(src);
298 dst->port_num = rdma_ah_get_port_num(src);
299 memcpy(&dst->dmac, src->roce.dmac, sizeof(dst->dmac));
300}
301
302u8 ib_gid_type_to_pvrdma(enum ib_gid_type gid_type)
303{
304 return (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
305 PVRDMA_GID_TYPE_FLAG_ROCE_V2 :
306 PVRDMA_GID_TYPE_FLAG_ROCE_V1;
307}
308