]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/mlx4/alloc.c
sh: asm/gpio.h needs linux/kernel.h for might_sleep()/WARN_ON().
[linux-2.6-omap-h63xx.git] / drivers / net / mlx4 / alloc.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4  *
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>
36 #include <linux/mm.h>
37 #include <linux/bitmap.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/vmalloc.h>
40
41 #include "mlx4.h"
42
43 u32 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);
57                 bitmap->last = (obj + 1) & (bitmap->max - 1);
58                 obj |= bitmap->top;
59         } else
60                 obj = -1;
61
62         spin_unlock(&bitmap->lock);
63
64         return obj;
65 }
66
67 void 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
78 int 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
101 void 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
113 int 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;
122                 buf->direct.buf   = dma_alloc_coherent(&dev->pdev->dev,
123                                                        size, &t, GFP_KERNEL);
124                 if (!buf->direct.buf)
125                         return -ENOMEM;
126
127                 buf->direct.map = t;
128
129                 while (t & ((1 << buf->page_shift) - 1)) {
130                         --buf->page_shift;
131                         buf->npages *= 2;
132                 }
133
134                 memset(buf->direct.buf, 0, size);
135         } 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;
141                 buf->page_list   = kzalloc(buf->nbufs * sizeof *buf->page_list,
142                                            GFP_KERNEL);
143                 if (!buf->page_list)
144                         return -ENOMEM;
145
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;
152
153                         buf->page_list[i].map = t;
154
155                         memset(buf->page_list[i].buf, 0, PAGE_SIZE);
156                 }
157
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;
169                 }
170         }
171
172         return 0;
173
174 err_free:
175         mlx4_buf_free(dev, size, buf);
176
177         return -ENOMEM;
178 }
179 EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
180
181 void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
182 {
183         int i;
184
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);
191
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);
198         }
199 }
200 EXPORT_SYMBOL_GPL(mlx4_buf_free);
201
202 static 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
223 static 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
237 found:
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
254 int 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
277 out:
278         mutex_unlock(&priv->pgdir_mutex);
279
280         return ret;
281 }
282 EXPORT_SYMBOL_GPL(mlx4_db_alloc);
283
284 void 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 }
311 EXPORT_SYMBOL_GPL(mlx4_db_free);
312
313 int 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
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);
345
346         return err;
347 }
348 EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
349
350 void 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 }
357 EXPORT_SYMBOL_GPL(mlx4_free_hwq_res);