blob: b411b79d72ad136322f8909a0cc3241111918fa5 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 *
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/errno.h>
35#include <linux/slab.h>
Andrew Morton65261282008-09-05 14:04:07 -070036#include <linux/mm.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070037#include <linux/bitmap.h>
Al Viro9cbe05c2007-05-15 20:36:30 +010038#include <linux/dma-mapping.h>
Olof Johansson29c27112008-02-10 20:22:57 -060039#include <linux/vmalloc.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070040
41#include "mlx4.h"
42
43u32 mlx4_bitmap_alloc(struct mlx4_bitmap *bitmap)
44{
45 u32 obj;
46
47 spin_lock(&bitmap->lock);
48
49 obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last);
50 if (obj >= bitmap->max) {
51 bitmap->top = (bitmap->top + bitmap->max) & bitmap->mask;
52 obj = find_first_zero_bit(bitmap->table, bitmap->max);
53 }
54
55 if (obj < bitmap->max) {
56 set_bit(obj, bitmap->table);
Roland Dreiera2cb4a92007-05-29 16:07:09 -070057 bitmap->last = (obj + 1) & (bitmap->max - 1);
Roland Dreier225c7b12007-05-08 18:00:38 -070058 obj |= bitmap->top;
Roland Dreier225c7b12007-05-08 18:00:38 -070059 } else
60 obj = -1;
61
62 spin_unlock(&bitmap->lock);
63
64 return obj;
65}
66
67void mlx4_bitmap_free(struct mlx4_bitmap *bitmap, u32 obj)
68{
69 obj &= bitmap->max - 1;
70
71 spin_lock(&bitmap->lock);
72 clear_bit(obj, bitmap->table);
73 bitmap->last = min(bitmap->last, obj);
74 bitmap->top = (bitmap->top + bitmap->max) & bitmap->mask;
75 spin_unlock(&bitmap->lock);
76}
77
78int mlx4_bitmap_init(struct mlx4_bitmap *bitmap, u32 num, u32 mask, u32 reserved)
79{
80 int i;
81
82 /* num must be a power of 2 */
83 if (num != roundup_pow_of_two(num))
84 return -EINVAL;
85
86 bitmap->last = 0;
87 bitmap->top = 0;
88 bitmap->max = num;
89 bitmap->mask = mask;
90 spin_lock_init(&bitmap->lock);
91 bitmap->table = kzalloc(BITS_TO_LONGS(num) * sizeof (long), GFP_KERNEL);
92 if (!bitmap->table)
93 return -ENOMEM;
94
95 for (i = 0; i < reserved; ++i)
96 set_bit(i, bitmap->table);
97
98 return 0;
99}
100
101void mlx4_bitmap_cleanup(struct mlx4_bitmap *bitmap)
102{
103 kfree(bitmap->table);
104}
105
106/*
107 * Handling for queue buffers -- we allocate a bunch of memory and
108 * register it in a memory region at HCA virtual address 0. If the
109 * requested size is > max_direct, we split the allocation into
110 * multiple pages, so we don't require too much contiguous memory.
111 */
112
113int mlx4_buf_alloc(struct mlx4_dev *dev, int size, int max_direct,
114 struct mlx4_buf *buf)
115{
116 dma_addr_t t;
117
118 if (size <= max_direct) {
119 buf->nbufs = 1;
120 buf->npages = 1;
121 buf->page_shift = get_order(size) + PAGE_SHIFT;
Roland Dreierb57aacf2008-02-06 21:17:59 -0800122 buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
Roland Dreier225c7b12007-05-08 18:00:38 -0700123 size, &t, GFP_KERNEL);
Roland Dreierb57aacf2008-02-06 21:17:59 -0800124 if (!buf->direct.buf)
Roland Dreier225c7b12007-05-08 18:00:38 -0700125 return -ENOMEM;
126
Roland Dreierb57aacf2008-02-06 21:17:59 -0800127 buf->direct.map = t;
Roland Dreier225c7b12007-05-08 18:00:38 -0700128
129 while (t & ((1 << buf->page_shift) - 1)) {
130 --buf->page_shift;
131 buf->npages *= 2;
132 }
133
Roland Dreierb57aacf2008-02-06 21:17:59 -0800134 memset(buf->direct.buf, 0, size);
Roland Dreier225c7b12007-05-08 18:00:38 -0700135 } else {
136 int i;
137
138 buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
139 buf->npages = buf->nbufs;
140 buf->page_shift = PAGE_SHIFT;
Roland Dreierb57aacf2008-02-06 21:17:59 -0800141 buf->page_list = kzalloc(buf->nbufs * sizeof *buf->page_list,
Roland Dreier225c7b12007-05-08 18:00:38 -0700142 GFP_KERNEL);
Roland Dreierb57aacf2008-02-06 21:17:59 -0800143 if (!buf->page_list)
Roland Dreier225c7b12007-05-08 18:00:38 -0700144 return -ENOMEM;
145
146 for (i = 0; i < buf->nbufs; ++i) {
Roland Dreierb57aacf2008-02-06 21:17:59 -0800147 buf->page_list[i].buf =
Roland Dreier225c7b12007-05-08 18:00:38 -0700148 dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
149 &t, GFP_KERNEL);
Roland Dreierb57aacf2008-02-06 21:17:59 -0800150 if (!buf->page_list[i].buf)
Roland Dreier225c7b12007-05-08 18:00:38 -0700151 goto err_free;
152
Roland Dreierb57aacf2008-02-06 21:17:59 -0800153 buf->page_list[i].map = t;
Roland Dreier225c7b12007-05-08 18:00:38 -0700154
Roland Dreierb57aacf2008-02-06 21:17:59 -0800155 memset(buf->page_list[i].buf, 0, PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700156 }
Jack Morgenstein313abe52008-01-28 10:40:51 +0200157
158 if (BITS_PER_LONG == 64) {
159 struct page **pages;
160 pages = kmalloc(sizeof *pages * buf->nbufs, GFP_KERNEL);
161 if (!pages)
162 goto err_free;
163 for (i = 0; i < buf->nbufs; ++i)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800164 pages[i] = virt_to_page(buf->page_list[i].buf);
165 buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
Jack Morgenstein313abe52008-01-28 10:40:51 +0200166 kfree(pages);
Roland Dreierb57aacf2008-02-06 21:17:59 -0800167 if (!buf->direct.buf)
Jack Morgenstein313abe52008-01-28 10:40:51 +0200168 goto err_free;
169 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700170 }
171
172 return 0;
173
174err_free:
175 mlx4_buf_free(dev, size, buf);
176
177 return -ENOMEM;
178}
179EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
180
181void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
182{
183 int i;
184
185 if (buf->nbufs == 1)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800186 dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
187 buf->direct.map);
Roland Dreier225c7b12007-05-08 18:00:38 -0700188 else {
Jack Morgenstein313abe52008-01-28 10:40:51 +0200189 if (BITS_PER_LONG == 64)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800190 vunmap(buf->direct.buf);
Jack Morgenstein313abe52008-01-28 10:40:51 +0200191
Roland Dreier225c7b12007-05-08 18:00:38 -0700192 for (i = 0; i < buf->nbufs; ++i)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800193 if (buf->page_list[i].buf)
Ali Ayoub3bba11e2007-11-13 15:26:57 -0800194 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
Roland Dreierb57aacf2008-02-06 21:17:59 -0800195 buf->page_list[i].buf,
196 buf->page_list[i].map);
197 kfree(buf->page_list);
Roland Dreier225c7b12007-05-08 18:00:38 -0700198 }
199}
200EXPORT_SYMBOL_GPL(mlx4_buf_free);
Yevgeny Petrilin62968832008-04-23 11:55:45 -0700201
202static struct mlx4_db_pgdir *mlx4_alloc_db_pgdir(struct device *dma_device)
203{
204 struct mlx4_db_pgdir *pgdir;
205
206 pgdir = kzalloc(sizeof *pgdir, GFP_KERNEL);
207 if (!pgdir)
208 return NULL;
209
210 bitmap_fill(pgdir->order1, MLX4_DB_PER_PAGE / 2);
211 pgdir->bits[0] = pgdir->order0;
212 pgdir->bits[1] = pgdir->order1;
213 pgdir->db_page = dma_alloc_coherent(dma_device, PAGE_SIZE,
214 &pgdir->db_dma, GFP_KERNEL);
215 if (!pgdir->db_page) {
216 kfree(pgdir);
217 return NULL;
218 }
219
220 return pgdir;
221}
222
223static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir *pgdir,
224 struct mlx4_db *db, int order)
225{
226 int o;
227 int i;
228
229 for (o = order; o <= 1; ++o) {
230 i = find_first_bit(pgdir->bits[o], MLX4_DB_PER_PAGE >> o);
231 if (i < MLX4_DB_PER_PAGE >> o)
232 goto found;
233 }
234
235 return -ENOMEM;
236
237found:
238 clear_bit(i, pgdir->bits[o]);
239
240 i <<= o;
241
242 if (o > order)
243 set_bit(i ^ 1, pgdir->bits[order]);
244
245 db->u.pgdir = pgdir;
246 db->index = i;
247 db->db = pgdir->db_page + db->index;
248 db->dma = pgdir->db_dma + db->index * 4;
249 db->order = order;
250
251 return 0;
252}
253
254int mlx4_db_alloc(struct mlx4_dev *dev, struct mlx4_db *db, int order)
255{
256 struct mlx4_priv *priv = mlx4_priv(dev);
257 struct mlx4_db_pgdir *pgdir;
258 int ret = 0;
259
260 mutex_lock(&priv->pgdir_mutex);
261
262 list_for_each_entry(pgdir, &priv->pgdir_list, list)
263 if (!mlx4_alloc_db_from_pgdir(pgdir, db, order))
264 goto out;
265
266 pgdir = mlx4_alloc_db_pgdir(&(dev->pdev->dev));
267 if (!pgdir) {
268 ret = -ENOMEM;
269 goto out;
270 }
271
272 list_add(&pgdir->list, &priv->pgdir_list);
273
274 /* This should never fail -- we just allocated an empty page: */
275 WARN_ON(mlx4_alloc_db_from_pgdir(pgdir, db, order));
276
277out:
278 mutex_unlock(&priv->pgdir_mutex);
279
280 return ret;
281}
282EXPORT_SYMBOL_GPL(mlx4_db_alloc);
283
284void mlx4_db_free(struct mlx4_dev *dev, struct mlx4_db *db)
285{
286 struct mlx4_priv *priv = mlx4_priv(dev);
287 int o;
288 int i;
289
290 mutex_lock(&priv->pgdir_mutex);
291
292 o = db->order;
293 i = db->index;
294
295 if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
296 clear_bit(i ^ 1, db->u.pgdir->order0);
297 ++o;
298 }
299 i >>= o;
300 set_bit(i, db->u.pgdir->bits[o]);
301
302 if (bitmap_full(db->u.pgdir->order1, MLX4_DB_PER_PAGE / 2)) {
303 dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
304 db->u.pgdir->db_page, db->u.pgdir->db_dma);
305 list_del(&db->u.pgdir->list);
306 kfree(db->u.pgdir);
307 }
308
309 mutex_unlock(&priv->pgdir_mutex);
310}
311EXPORT_SYMBOL_GPL(mlx4_db_free);
Yevgeny Petrilin38ae6a52008-04-25 14:27:08 -0700312
313int mlx4_alloc_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
314 int size, int max_direct)
315{
316 int err;
317
318 err = mlx4_db_alloc(dev, &wqres->db, 1);
319 if (err)
320 return err;
321
322 *wqres->db.db = 0;
323
324 err = mlx4_buf_alloc(dev, size, max_direct, &wqres->buf);
325 if (err)
326 goto err_db;
327
328 err = mlx4_mtt_init(dev, wqres->buf.npages, wqres->buf.page_shift,
329 &wqres->mtt);
330 if (err)
331 goto err_buf;
332
333 err = mlx4_buf_write_mtt(dev, &wqres->mtt, &wqres->buf);
334 if (err)
335 goto err_mtt;
336
337 return 0;
338
339err_mtt:
340 mlx4_mtt_cleanup(dev, &wqres->mtt);
341err_buf:
342 mlx4_buf_free(dev, size, &wqres->buf);
343err_db:
344 mlx4_db_free(dev, &wqres->db);
345
346 return err;
347}
348EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
349
350void mlx4_free_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
351 int size)
352{
353 mlx4_mtt_cleanup(dev, &wqres->mtt);
354 mlx4_buf_free(dev, size, &wqres->buf);
355 mlx4_db_free(dev, &wqres->db);
356}
357EXPORT_SYMBOL_GPL(mlx4_free_hwq_res);