1 /*
2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/export.h>
37 #include <linux/bitmap.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vmalloc.h>
40 #include <linux/mlx5/driver.h>
41
42 #include "mlx5_core.h"
43
44 struct mlx5_db_pgdir {
45 struct list_head list;
46 unsigned long *bitmap;
47 __be32 *db_page;
48 dma_addr_t db_dma;
49 };
50
51 /* Handling for queue buffers -- we allocate a bunch of memory and
52 * register it in a memory region at HCA virtual address 0.
53 */
54
mlx5_dma_zalloc_coherent_node(struct mlx5_core_dev * dev,size_t size,dma_addr_t * dma_handle,int node)55 static void *mlx5_dma_zalloc_coherent_node(struct mlx5_core_dev *dev,
56 size_t size, dma_addr_t *dma_handle,
57 int node)
58 {
59 struct device *device = mlx5_core_dma_dev(dev);
60 struct mlx5_priv *priv = &dev->priv;
61 int original_node;
62 void *cpu_handle;
63
64 mutex_lock(&priv->alloc_mutex);
65 original_node = dev_to_node(device);
66 set_dev_node(device, node);
67 cpu_handle = dma_alloc_coherent(device, size, dma_handle,
68 GFP_KERNEL);
69 set_dev_node(device, original_node);
70 mutex_unlock(&priv->alloc_mutex);
71 return cpu_handle;
72 }
73
mlx5_frag_buf_alloc_node(struct mlx5_core_dev * dev,int size,struct mlx5_frag_buf * buf,int node)74 int mlx5_frag_buf_alloc_node(struct mlx5_core_dev *dev, int size,
75 struct mlx5_frag_buf *buf, int node)
76 {
77 int i;
78
79 buf->size = size;
80 buf->npages = DIV_ROUND_UP(size, PAGE_SIZE);
81 buf->page_shift = PAGE_SHIFT;
82 buf->frags = kzalloc_objs(struct mlx5_buf_list, buf->npages);
83 if (!buf->frags)
84 goto err_out;
85
86 for (i = 0; i < buf->npages; i++) {
87 struct mlx5_buf_list *frag = &buf->frags[i];
88 int frag_sz = min_t(int, size, PAGE_SIZE);
89
90 frag->buf = mlx5_dma_zalloc_coherent_node(dev, frag_sz,
91 &frag->map, node);
92 if (!frag->buf)
93 goto err_free_buf;
94 if (frag->map & ((1 << buf->page_shift) - 1)) {
95 dma_free_coherent(mlx5_core_dma_dev(dev), frag_sz,
96 buf->frags[i].buf, buf->frags[i].map);
97 mlx5_core_warn(dev, "unexpected map alignment: %pad, page_shift=%d\n",
98 &frag->map, buf->page_shift);
99 goto err_free_buf;
100 }
101 size -= frag_sz;
102 }
103
104 return 0;
105
106 err_free_buf:
107 while (i--)
108 dma_free_coherent(mlx5_core_dma_dev(dev), PAGE_SIZE, buf->frags[i].buf,
109 buf->frags[i].map);
110 kfree(buf->frags);
111 err_out:
112 return -ENOMEM;
113 }
114 EXPORT_SYMBOL_GPL(mlx5_frag_buf_alloc_node);
115
mlx5_frag_buf_free(struct mlx5_core_dev * dev,struct mlx5_frag_buf * buf)116 void mlx5_frag_buf_free(struct mlx5_core_dev *dev, struct mlx5_frag_buf *buf)
117 {
118 int size = buf->size;
119 int i;
120
121 for (i = 0; i < buf->npages; i++) {
122 int frag_sz = min_t(int, size, PAGE_SIZE);
123
124 dma_free_coherent(mlx5_core_dma_dev(dev), frag_sz, buf->frags[i].buf,
125 buf->frags[i].map);
126 size -= frag_sz;
127 }
128 kfree(buf->frags);
129 }
130 EXPORT_SYMBOL_GPL(mlx5_frag_buf_free);
131
mlx5_alloc_db_pgdir(struct mlx5_core_dev * dev,int node)132 static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct mlx5_core_dev *dev,
133 int node)
134 {
135 u32 db_per_page = PAGE_SIZE / cache_line_size();
136 struct mlx5_db_pgdir *pgdir;
137
138 pgdir = kzalloc_node(sizeof(*pgdir), GFP_KERNEL, node);
139 if (!pgdir)
140 return NULL;
141
142 pgdir->bitmap = bitmap_zalloc_node(db_per_page, GFP_KERNEL, node);
143 if (!pgdir->bitmap) {
144 kfree(pgdir);
145 return NULL;
146 }
147
148 bitmap_fill(pgdir->bitmap, db_per_page);
149
150 pgdir->db_page = mlx5_dma_zalloc_coherent_node(dev, PAGE_SIZE,
151 &pgdir->db_dma, node);
152 if (!pgdir->db_page) {
153 bitmap_free(pgdir->bitmap);
154 kfree(pgdir);
155 return NULL;
156 }
157
158 return pgdir;
159 }
160
mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir * pgdir,struct mlx5_db * db)161 static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir *pgdir,
162 struct mlx5_db *db)
163 {
164 u32 db_per_page = PAGE_SIZE / cache_line_size();
165 int offset;
166 int i;
167
168 i = find_first_bit(pgdir->bitmap, db_per_page);
169 if (i >= db_per_page)
170 return -ENOMEM;
171
172 __clear_bit(i, pgdir->bitmap);
173
174 db->u.pgdir = pgdir;
175 db->index = i;
176 offset = db->index * cache_line_size();
177 db->db = pgdir->db_page + offset / sizeof(*pgdir->db_page);
178 db->dma = pgdir->db_dma + offset;
179
180 db->db[0] = 0;
181 db->db[1] = 0;
182
183 return 0;
184 }
185
mlx5_db_alloc_node(struct mlx5_core_dev * dev,struct mlx5_db * db,int node)186 int mlx5_db_alloc_node(struct mlx5_core_dev *dev, struct mlx5_db *db, int node)
187 {
188 struct mlx5_db_pgdir *pgdir;
189 int ret = 0;
190
191 mutex_lock(&dev->priv.pgdir_mutex);
192
193 list_for_each_entry(pgdir, &dev->priv.pgdir_list, list)
194 if (!mlx5_alloc_db_from_pgdir(pgdir, db))
195 goto out;
196
197 pgdir = mlx5_alloc_db_pgdir(dev, node);
198 if (!pgdir) {
199 ret = -ENOMEM;
200 goto out;
201 }
202
203 list_add(&pgdir->list, &dev->priv.pgdir_list);
204
205 /* This should never fail -- we just allocated an empty page: */
206 WARN_ON(mlx5_alloc_db_from_pgdir(pgdir, db));
207
208 out:
209 mutex_unlock(&dev->priv.pgdir_mutex);
210
211 return ret;
212 }
213 EXPORT_SYMBOL_GPL(mlx5_db_alloc_node);
214
mlx5_db_free(struct mlx5_core_dev * dev,struct mlx5_db * db)215 void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db)
216 {
217 u32 db_per_page = PAGE_SIZE / cache_line_size();
218
219 mutex_lock(&dev->priv.pgdir_mutex);
220
221 __set_bit(db->index, db->u.pgdir->bitmap);
222
223 if (bitmap_full(db->u.pgdir->bitmap, db_per_page)) {
224 dma_free_coherent(mlx5_core_dma_dev(dev), PAGE_SIZE,
225 db->u.pgdir->db_page, db->u.pgdir->db_dma);
226 list_del(&db->u.pgdir->list);
227 bitmap_free(db->u.pgdir->bitmap);
228 kfree(db->u.pgdir);
229 }
230
231 mutex_unlock(&dev->priv.pgdir_mutex);
232 }
233 EXPORT_SYMBOL_GPL(mlx5_db_free);
234
mlx5_fill_page_frag_array_perm(struct mlx5_frag_buf * buf,__be64 * pas,u8 perm)235 void mlx5_fill_page_frag_array_perm(struct mlx5_frag_buf *buf, __be64 *pas, u8 perm)
236 {
237 int i;
238
239 WARN_ON(perm & 0xfc);
240 for (i = 0; i < buf->npages; i++)
241 pas[i] = cpu_to_be64(buf->frags[i].map | perm);
242 }
243 EXPORT_SYMBOL_GPL(mlx5_fill_page_frag_array_perm);
244
mlx5_fill_page_frag_array(struct mlx5_frag_buf * buf,__be64 * pas)245 void mlx5_fill_page_frag_array(struct mlx5_frag_buf *buf, __be64 *pas)
246 {
247 mlx5_fill_page_frag_array_perm(buf, pas, 0);
248 }
249 EXPORT_SYMBOL_GPL(mlx5_fill_page_frag_array);
250