2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/errno.h>
34 #include <linux/slab.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>
42 #include "mlx5_core.h"
44 /* Handling for queue buffers -- we allocate a bunch of memory and
45 * register it in a memory region at HCA virtual address 0.
48 int mlx5_buf_alloc(struct mlx5_core_dev
*dev
, int size
, struct mlx5_buf
*buf
)
54 buf
->page_shift
= (u8
)get_order(size
) + PAGE_SHIFT
;
55 buf
->direct
.buf
= dma_zalloc_coherent(&dev
->pdev
->dev
,
56 size
, &t
, GFP_KERNEL
);
62 while (t
& ((1 << buf
->page_shift
) - 1)) {
69 EXPORT_SYMBOL_GPL(mlx5_buf_alloc
);
71 void mlx5_buf_free(struct mlx5_core_dev
*dev
, struct mlx5_buf
*buf
)
73 dma_free_coherent(&dev
->pdev
->dev
, buf
->size
, buf
->direct
.buf
,
76 EXPORT_SYMBOL_GPL(mlx5_buf_free
);
78 static struct mlx5_db_pgdir
*mlx5_alloc_db_pgdir(struct device
*dma_device
)
80 struct mlx5_db_pgdir
*pgdir
;
82 pgdir
= kzalloc(sizeof(*pgdir
), GFP_KERNEL
);
86 bitmap_fill(pgdir
->bitmap
, MLX5_DB_PER_PAGE
);
87 pgdir
->db_page
= dma_alloc_coherent(dma_device
, PAGE_SIZE
,
88 &pgdir
->db_dma
, GFP_KERNEL
);
89 if (!pgdir
->db_page
) {
97 static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir
*pgdir
,
103 i
= find_first_bit(pgdir
->bitmap
, MLX5_DB_PER_PAGE
);
104 if (i
>= MLX5_DB_PER_PAGE
)
107 __clear_bit(i
, pgdir
->bitmap
);
111 offset
= db
->index
* L1_CACHE_BYTES
;
112 db
->db
= pgdir
->db_page
+ offset
/ sizeof(*pgdir
->db_page
);
113 db
->dma
= pgdir
->db_dma
+ offset
;
121 int mlx5_db_alloc(struct mlx5_core_dev
*dev
, struct mlx5_db
*db
)
123 struct mlx5_db_pgdir
*pgdir
;
126 mutex_lock(&dev
->priv
.pgdir_mutex
);
128 list_for_each_entry(pgdir
, &dev
->priv
.pgdir_list
, list
)
129 if (!mlx5_alloc_db_from_pgdir(pgdir
, db
))
132 pgdir
= mlx5_alloc_db_pgdir(&(dev
->pdev
->dev
));
138 list_add(&pgdir
->list
, &dev
->priv
.pgdir_list
);
140 /* This should never fail -- we just allocated an empty page: */
141 WARN_ON(mlx5_alloc_db_from_pgdir(pgdir
, db
));
144 mutex_unlock(&dev
->priv
.pgdir_mutex
);
148 EXPORT_SYMBOL_GPL(mlx5_db_alloc
);
150 void mlx5_db_free(struct mlx5_core_dev
*dev
, struct mlx5_db
*db
)
152 mutex_lock(&dev
->priv
.pgdir_mutex
);
154 __set_bit(db
->index
, db
->u
.pgdir
->bitmap
);
156 if (bitmap_full(db
->u
.pgdir
->bitmap
, MLX5_DB_PER_PAGE
)) {
157 dma_free_coherent(&(dev
->pdev
->dev
), PAGE_SIZE
,
158 db
->u
.pgdir
->db_page
, db
->u
.pgdir
->db_dma
);
159 list_del(&db
->u
.pgdir
->list
);
163 mutex_unlock(&dev
->priv
.pgdir_mutex
);
165 EXPORT_SYMBOL_GPL(mlx5_db_free
);
168 void mlx5_fill_page_array(struct mlx5_buf
*buf
, __be64
*pas
)
173 for (i
= 0; i
< buf
->npages
; i
++) {
174 addr
= buf
->direct
.map
+ (i
<< buf
->page_shift
);
176 pas
[i
] = cpu_to_be64(addr
);
179 EXPORT_SYMBOL_GPL(mlx5_fill_page_array
);
This page took 0.039551 seconds and 5 git commands to generate.