This source file includes following definitions.
- mlx5_ib_cont_pages
- umem_dma_to_mtt
- __mlx5_ib_populate_pas
- mlx5_ib_populate_pas
- mlx5_ib_get_buf_offset
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 #include <linux/module.h>
34 #include <rdma/ib_umem.h>
35 #include <rdma/ib_umem_odp.h>
36 #include "mlx5_ib.h"
37
38
39
40
41
42
43
44
45
46 void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
47 unsigned long max_page_shift,
48 int *count, int *shift,
49 int *ncont, int *order)
50 {
51 unsigned long tmp;
52 unsigned long m;
53 u64 base = ~0, p = 0;
54 u64 len, pfn;
55 int i = 0;
56 struct scatterlist *sg;
57 int entry;
58
59 addr = addr >> PAGE_SHIFT;
60 tmp = (unsigned long)addr;
61 m = find_first_bit(&tmp, BITS_PER_LONG);
62 if (max_page_shift)
63 m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
64
65 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
66 len = sg_dma_len(sg) >> PAGE_SHIFT;
67 pfn = sg_dma_address(sg) >> PAGE_SHIFT;
68 if (base + p != pfn) {
69
70
71
72 tmp = (unsigned long)(pfn | p);
73 if (!IS_ALIGNED(tmp, 1 << m))
74 m = find_first_bit(&tmp, BITS_PER_LONG);
75
76 base = pfn;
77 p = 0;
78 }
79
80 p += len;
81 i += len;
82 }
83
84 if (i) {
85 m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
86
87 if (order)
88 *order = ilog2(roundup_pow_of_two(i) >> m);
89
90 *ncont = DIV_ROUND_UP(i, (1 << m));
91 } else {
92 m = 0;
93
94 if (order)
95 *order = 0;
96
97 *ncont = 0;
98 }
99 *shift = PAGE_SHIFT + m;
100 *count = i;
101 }
102
103 static u64 umem_dma_to_mtt(dma_addr_t umem_dma)
104 {
105 u64 mtt_entry = umem_dma & ODP_DMA_ADDR_MASK;
106
107 if (umem_dma & ODP_READ_ALLOWED_BIT)
108 mtt_entry |= MLX5_IB_MTT_READ;
109 if (umem_dma & ODP_WRITE_ALLOWED_BIT)
110 mtt_entry |= MLX5_IB_MTT_WRITE;
111
112 return mtt_entry;
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
129 int page_shift, size_t offset, size_t num_pages,
130 __be64 *pas, int access_flags)
131 {
132 int shift = page_shift - PAGE_SHIFT;
133 int mask = (1 << shift) - 1;
134 int i, k, idx;
135 u64 cur = 0;
136 u64 base;
137 int len;
138 struct scatterlist *sg;
139 int entry;
140
141 if (umem->is_odp) {
142 WARN_ON(shift != 0);
143 WARN_ON(access_flags != (MLX5_IB_MTT_READ | MLX5_IB_MTT_WRITE));
144
145 for (i = 0; i < num_pages; ++i) {
146 dma_addr_t pa =
147 to_ib_umem_odp(umem)->dma_list[offset + i];
148
149 pas[i] = cpu_to_be64(umem_dma_to_mtt(pa));
150 }
151 return;
152 }
153
154 i = 0;
155 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
156 len = sg_dma_len(sg) >> PAGE_SHIFT;
157 base = sg_dma_address(sg);
158
159
160 if (i + len < offset << shift) {
161 i += len;
162 continue;
163 }
164
165
166 if (i < offset << shift) {
167 k = (offset << shift) - i;
168 i = offset << shift;
169 } else {
170 k = 0;
171 }
172
173 for (; k < len; k++) {
174 if (!(i & mask)) {
175 cur = base + (k << PAGE_SHIFT);
176 cur |= access_flags;
177 idx = (i >> shift) - offset;
178
179 pas[idx] = cpu_to_be64(cur);
180 mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
181 i >> shift, be64_to_cpu(pas[idx]));
182 }
183 i++;
184
185
186 if (i >> shift >= offset + num_pages)
187 return;
188 }
189 }
190 }
191
192 void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
193 int page_shift, __be64 *pas, int access_flags)
194 {
195 return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
196 ib_umem_num_pages(umem), pas,
197 access_flags);
198 }
199 int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
200 {
201 u64 page_size;
202 u64 page_mask;
203 u64 off_size;
204 u64 off_mask;
205 u64 buf_off;
206
207 page_size = (u64)1 << page_shift;
208 page_mask = page_size - 1;
209 buf_off = addr & page_mask;
210 off_size = page_size >> 6;
211 off_mask = off_size - 1;
212
213 if (buf_off & off_mask)
214 return -EINVAL;
215
216 *offset = buf_off >> ilog2(off_size);
217 return 0;
218 }