]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mlx5/mlx5_ib/mlx5_ib_mem.c
MFC r322810 and r322830:
[FreeBSD/stable/10.git] / sys / dev / mlx5 / mlx5_ib / mlx5_ib_mem.c
1 /*-
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 #include <linux/module.h>
29 #include <rdma/ib_umem.h>
30 #include "mlx5_ib.h"
31
32 CTASSERT(sizeof(uintptr_t) == sizeof(unsigned long));
33
34 /* @umem: umem object to scan
35  * @addr: ib virtual address requested by the user
36  * @count: number of PAGE_SIZE pages covered by umem
37  * @shift: page shift for the compound pages found in the region
38  * @ncont: number of compund pages
39  * @order: log2 of the number of compound pages
40  */
41
42 void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr, int *count, int *shift,
43                         int *ncont, int *order)
44 {
45         struct ib_umem_chunk *chunk;
46         unsigned long tmp;
47         unsigned long m;
48         int i, j, k;
49         u64 base = 0;
50         int p = 0;
51         int skip;
52         int mask;
53         u64 len;
54         u64 pfn;
55         struct scatterlist *sg;
56         unsigned long page_shift = ilog2(umem->page_size);
57
58         addr = addr >> page_shift;
59         tmp = (uintptr_t)addr;
60         m = find_first_bit(&tmp, 8 * sizeof(tmp));
61         skip = 1 << m;
62         mask = skip - 1;
63         i = 0;
64
65         list_for_each_entry(chunk, &umem->chunk_list, list) {
66             for (j = 0; j < chunk->nmap; j++) {
67                 sg = chunk->page_list + j;
68                 len = sg_dma_len(sg) >> page_shift;
69                 pfn = sg_dma_address(sg) >> page_shift;
70                 for (k = 0; k < len; k++) {
71                         if (!(i & mask)) {
72                                 tmp = (uintptr_t)pfn;
73                                 m = min_t(unsigned long, m,
74                                           find_first_bit(&tmp, 8 * sizeof(tmp)));
75                                 skip = 1 << m;
76                                 mask = skip - 1;
77                                 base = pfn;
78                                 p = 0;
79                         } else {
80                                 if (base + p != pfn) {
81                                         tmp = (uintptr_t)p;
82                                         m = find_first_bit(&tmp, 8 * sizeof(tmp));
83                                         skip = 1 << m;
84                                         mask = skip - 1;
85                                         base = pfn;
86                                         p = 0;
87                                 }
88                         }
89                         p++;
90                         i++;
91                 }
92             }
93         }
94
95         if (i) {
96                 m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
97
98                 if (order)
99                         *order = ilog2(roundup_pow_of_two(i) >> m);
100
101                 *ncont = DIV_ROUND_UP(i, (1 << m));
102         } else {
103                 m  = 0;
104
105                 if (order)
106                         *order = 0;
107
108                 *ncont = 0;
109         }
110         *shift = page_shift + m;
111         *count = i;
112 }
113
114 /*
115  * Populate the given array with bus addresses from the umem.
116  *
117  * dev - mlx5_ib device
118  * umem - umem to use to fill the pages
119  * page_shift - determines the page size used in the resulting array
120  * offset - offset into the umem to start from,
121  *          only implemented for ODP umems
122  * num_pages - total number of pages to fill
123  * pas - bus addresses array to fill
124  * access_flags - access flags to set on all present pages.
125                   use enum mlx5_ib_mtt_access_flags for this.
126  */
127 static void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
128                             int page_shift, size_t offset,
129                             __be64 *pas, int access_flags)
130 {
131         unsigned long umem_page_shift = ilog2(umem->page_size);
132         struct ib_umem_chunk *chunk;
133         int shift = page_shift - umem_page_shift;
134         int mask = (1 << shift) - 1;
135         int i, j, k;
136         u64 cur = 0;
137         u64 base;
138         int len;
139         struct scatterlist *sg;
140
141         i = 0;
142         list_for_each_entry(chunk, &umem->chunk_list, list) {
143             for (j = 0; j < chunk->nmap; j++) {
144                 sg = chunk->page_list + j;
145                 len = sg_dma_len(sg) >> umem_page_shift;
146                 base = sg_dma_address(sg);
147                 for (k = 0; k < len; k++) {
148                         if (!(i & mask)) {
149                                 cur = base + (k << umem_page_shift);
150                                 cur |= access_flags;
151
152                                 pas[i >> shift] = cpu_to_be64(cur);
153                                 mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
154                                             i >> shift, (unsigned long long)
155                                             be64_to_cpu(pas[i >> shift]));
156                         }  else
157                                 mlx5_ib_dbg(dev, "=====> 0x%llx\n",
158                                             (unsigned long long)
159                                             (base + (k << umem_page_shift)));
160                         i++;
161                 }
162             }
163         }
164 }
165
166 void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
167                           int page_shift, __be64 *pas, int access_flags)
168 {
169         return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
170                                       pas,
171                                       access_flags);
172 }
173
174 int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
175 {
176         u64 page_size;
177         u64 page_mask;
178         u64 off_size;
179         u64 off_mask;
180         u64 buf_off;
181
182         page_size = (u64)1 << page_shift;
183         page_mask = page_size - 1;
184         buf_off = addr & page_mask;
185         off_size = page_size >> 6;
186         off_mask = off_size - 1;
187
188         if (buf_off & off_mask)
189                 return -EINVAL;
190
191         *offset = (u32)(buf_off >> ilog2(off_size));
192         return 0;
193 }