2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/errno.h>
35 #include <linux/slab.h>
37 #include <linux/bitmap.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vmalloc.h>
43 u32
mlx4_bitmap_alloc(struct mlx4_bitmap
*bitmap
)
47 spin_lock(&bitmap
->lock
);
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
);
55 if (obj
< bitmap
->max
) {
56 set_bit(obj
, bitmap
->table
);
57 bitmap
->last
= (obj
+ 1) & (bitmap
->max
- 1);
62 spin_unlock(&bitmap
->lock
);
67 void mlx4_bitmap_free(struct mlx4_bitmap
*bitmap
, u32 obj
)
69 obj
&= bitmap
->max
- 1;
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
);
78 int mlx4_bitmap_init(struct mlx4_bitmap
*bitmap
, u32 num
, u32 mask
, u32 reserved
)
82 /* num must be a power of 2 */
83 if (num
!= roundup_pow_of_two(num
))
90 spin_lock_init(&bitmap
->lock
);
91 bitmap
->table
= kzalloc(BITS_TO_LONGS(num
) * sizeof (long), GFP_KERNEL
);
95 for (i
= 0; i
< reserved
; ++i
)
96 set_bit(i
, bitmap
->table
);
101 void mlx4_bitmap_cleanup(struct mlx4_bitmap
*bitmap
)
103 kfree(bitmap
->table
);
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.
113 int mlx4_buf_alloc(struct mlx4_dev
*dev
, int size
, int max_direct
,
114 struct mlx4_buf
*buf
)
118 if (size
<= max_direct
) {
121 buf
->page_shift
= get_order(size
) + PAGE_SHIFT
;
122 buf
->direct
.buf
= dma_alloc_coherent(&dev
->pdev
->dev
,
123 size
, &t
, GFP_KERNEL
);
124 if (!buf
->direct
.buf
)
129 while (t
& ((1 << buf
->page_shift
) - 1)) {
134 memset(buf
->direct
.buf
, 0, size
);
138 buf
->nbufs
= (size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
139 buf
->npages
= buf
->nbufs
;
140 buf
->page_shift
= PAGE_SHIFT
;
141 buf
->page_list
= kzalloc(buf
->nbufs
* sizeof *buf
->page_list
,
146 for (i
= 0; i
< buf
->nbufs
; ++i
) {
147 buf
->page_list
[i
].buf
=
148 dma_alloc_coherent(&dev
->pdev
->dev
, PAGE_SIZE
,
150 if (!buf
->page_list
[i
].buf
)
153 buf
->page_list
[i
].map
= t
;
155 memset(buf
->page_list
[i
].buf
, 0, PAGE_SIZE
);
158 if (BITS_PER_LONG
== 64) {
160 pages
= kmalloc(sizeof *pages
* buf
->nbufs
, GFP_KERNEL
);
163 for (i
= 0; i
< buf
->nbufs
; ++i
)
164 pages
[i
] = virt_to_page(buf
->page_list
[i
].buf
);
165 buf
->direct
.buf
= vmap(pages
, buf
->nbufs
, VM_MAP
, PAGE_KERNEL
);
167 if (!buf
->direct
.buf
)
175 mlx4_buf_free(dev
, size
, buf
);
179 EXPORT_SYMBOL_GPL(mlx4_buf_alloc
);
181 void mlx4_buf_free(struct mlx4_dev
*dev
, int size
, struct mlx4_buf
*buf
)
186 dma_free_coherent(&dev
->pdev
->dev
, size
, buf
->direct
.buf
,
189 if (BITS_PER_LONG
== 64)
190 vunmap(buf
->direct
.buf
);
192 for (i
= 0; i
< buf
->nbufs
; ++i
)
193 if (buf
->page_list
[i
].buf
)
194 dma_free_coherent(&dev
->pdev
->dev
, PAGE_SIZE
,
195 buf
->page_list
[i
].buf
,
196 buf
->page_list
[i
].map
);
197 kfree(buf
->page_list
);
200 EXPORT_SYMBOL_GPL(mlx4_buf_free
);
202 static struct mlx4_db_pgdir
*mlx4_alloc_db_pgdir(struct device
*dma_device
)
204 struct mlx4_db_pgdir
*pgdir
;
206 pgdir
= kzalloc(sizeof *pgdir
, GFP_KERNEL
);
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
) {
223 static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir
*pgdir
,
224 struct mlx4_db
*db
, int order
)
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
)
238 clear_bit(i
, pgdir
->bits
[o
]);
243 set_bit(i
^ 1, pgdir
->bits
[order
]);
247 db
->db
= pgdir
->db_page
+ db
->index
;
248 db
->dma
= pgdir
->db_dma
+ db
->index
* 4;
254 int mlx4_db_alloc(struct mlx4_dev
*dev
, struct mlx4_db
*db
, int order
)
256 struct mlx4_priv
*priv
= mlx4_priv(dev
);
257 struct mlx4_db_pgdir
*pgdir
;
260 mutex_lock(&priv
->pgdir_mutex
);
262 list_for_each_entry(pgdir
, &priv
->pgdir_list
, list
)
263 if (!mlx4_alloc_db_from_pgdir(pgdir
, db
, order
))
266 pgdir
= mlx4_alloc_db_pgdir(&(dev
->pdev
->dev
));
272 list_add(&pgdir
->list
, &priv
->pgdir_list
);
274 /* This should never fail -- we just allocated an empty page: */
275 WARN_ON(mlx4_alloc_db_from_pgdir(pgdir
, db
, order
));
278 mutex_unlock(&priv
->pgdir_mutex
);
282 EXPORT_SYMBOL_GPL(mlx4_db_alloc
);
284 void mlx4_db_free(struct mlx4_dev
*dev
, struct mlx4_db
*db
)
286 struct mlx4_priv
*priv
= mlx4_priv(dev
);
290 mutex_lock(&priv
->pgdir_mutex
);
295 if (db
->order
== 0 && test_bit(i
^ 1, db
->u
.pgdir
->order0
)) {
296 clear_bit(i
^ 1, db
->u
.pgdir
->order0
);
300 set_bit(i
, db
->u
.pgdir
->bits
[o
]);
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
);
309 mutex_unlock(&priv
->pgdir_mutex
);
311 EXPORT_SYMBOL_GPL(mlx4_db_free
);
313 int mlx4_alloc_hwq_res(struct mlx4_dev
*dev
, struct mlx4_hwq_resources
*wqres
,
314 int size
, int max_direct
)
318 err
= mlx4_db_alloc(dev
, &wqres
->db
, 1);
324 err
= mlx4_buf_alloc(dev
, size
, max_direct
, &wqres
->buf
);
328 err
= mlx4_mtt_init(dev
, wqres
->buf
.npages
, wqres
->buf
.page_shift
,
333 err
= mlx4_buf_write_mtt(dev
, &wqres
->mtt
, &wqres
->buf
);
340 mlx4_mtt_cleanup(dev
, &wqres
->mtt
);
342 mlx4_buf_free(dev
, size
, &wqres
->buf
);
344 mlx4_db_free(dev
, &wqres
->db
);
348 EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res
);
350 void mlx4_free_hwq_res(struct mlx4_dev
*dev
, struct mlx4_hwq_resources
*wqres
,
353 mlx4_mtt_cleanup(dev
, &wqres
->mtt
);
354 mlx4_buf_free(dev
, size
, &wqres
->buf
);
355 mlx4_db_free(dev
, &wqres
->db
);
357 EXPORT_SYMBOL_GPL(mlx4_free_hwq_res
);