i915: Add support for MSI and interrupt mitigation.
[linux-2.6/sactl.git] / drivers / net / mlx4 / alloc.c
blobb411b79d72ad136322f8909a0cc3241111918fa5
1 /*
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
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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
31 * SOFTWARE.
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/mm.h>
37 #include <linux/bitmap.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vmalloc.h>
41 #include "mlx4.h"
43 u32 mlx4_bitmap_alloc(struct mlx4_bitmap *bitmap)
45 u32 obj;
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);
58 obj |= bitmap->top;
59 } else
60 obj = -1;
62 spin_unlock(&bitmap->lock);
64 return obj;
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)
80 int i;
82 /* num must be a power of 2 */
83 if (num != roundup_pow_of_two(num))
84 return -EINVAL;
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;
95 for (i = 0; i < reserved; ++i)
96 set_bit(i, bitmap->table);
98 return 0;
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)
116 dma_addr_t t;
118 if (size <= max_direct) {
119 buf->nbufs = 1;
120 buf->npages = 1;
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)
125 return -ENOMEM;
127 buf->direct.map = t;
129 while (t & ((1 << buf->page_shift) - 1)) {
130 --buf->page_shift;
131 buf->npages *= 2;
134 memset(buf->direct.buf, 0, size);
135 } else {
136 int i;
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,
142 GFP_KERNEL);
143 if (!buf->page_list)
144 return -ENOMEM;
146 for (i = 0; i < buf->nbufs; ++i) {
147 buf->page_list[i].buf =
148 dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
149 &t, GFP_KERNEL);
150 if (!buf->page_list[i].buf)
151 goto err_free;
153 buf->page_list[i].map = t;
155 memset(buf->page_list[i].buf, 0, PAGE_SIZE);
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)
164 pages[i] = virt_to_page(buf->page_list[i].buf);
165 buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
166 kfree(pages);
167 if (!buf->direct.buf)
168 goto err_free;
172 return 0;
174 err_free:
175 mlx4_buf_free(dev, size, buf);
177 return -ENOMEM;
179 EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
181 void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
183 int i;
185 if (buf->nbufs == 1)
186 dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
187 buf->direct.map);
188 else {
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);
207 if (!pgdir)
208 return NULL;
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;
220 return pgdir;
223 static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir *pgdir,
224 struct mlx4_db *db, int order)
226 int o;
227 int i;
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;
235 return -ENOMEM;
237 found:
238 clear_bit(i, pgdir->bits[o]);
240 i <<= o;
242 if (o > order)
243 set_bit(i ^ 1, pgdir->bits[order]);
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;
251 return 0;
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;
258 int ret = 0;
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))
264 goto out;
266 pgdir = mlx4_alloc_db_pgdir(&(dev->pdev->dev));
267 if (!pgdir) {
268 ret = -ENOMEM;
269 goto out;
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));
277 out:
278 mutex_unlock(&priv->pgdir_mutex);
280 return ret;
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);
287 int o;
288 int i;
290 mutex_lock(&priv->pgdir_mutex);
292 o = db->order;
293 i = db->index;
295 if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
296 clear_bit(i ^ 1, db->u.pgdir->order0);
297 ++o;
299 i >>= o;
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);
306 kfree(db->u.pgdir);
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)
316 int err;
318 err = mlx4_db_alloc(dev, &wqres->db, 1);
319 if (err)
320 return err;
322 *wqres->db.db = 0;
324 err = mlx4_buf_alloc(dev, size, max_direct, &wqres->buf);
325 if (err)
326 goto err_db;
328 err = mlx4_mtt_init(dev, wqres->buf.npages, wqres->buf.page_shift,
329 &wqres->mtt);
330 if (err)
331 goto err_buf;
333 err = mlx4_buf_write_mtt(dev, &wqres->mtt, &wqres->buf);
334 if (err)
335 goto err_mtt;
337 return 0;
339 err_mtt:
340 mlx4_mtt_cleanup(dev, &wqres->mtt);
341 err_buf:
342 mlx4_buf_free(dev, size, &wqres->buf);
343 err_db:
344 mlx4_db_free(dev, &wqres->db);
346 return err;
348 EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
350 void mlx4_free_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
351 int size)
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);