1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/errno.h>
34#include <linux/slab.h>
35#include <linux/mm.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>
41
42#include "mlx5_core.h"
43
44
45
46
47
48int mlx5_buf_alloc(struct mlx5_core_dev *dev, int size, struct mlx5_buf *buf)
49{
50 dma_addr_t t;
51
52 buf->size = size;
53 buf->npages = 1;
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);
57 if (!buf->direct.buf)
58 return -ENOMEM;
59
60 buf->direct.map = t;
61
62 while (t & ((1 << buf->page_shift) - 1)) {
63 --buf->page_shift;
64 buf->npages *= 2;
65 }
66
67 return 0;
68}
69EXPORT_SYMBOL_GPL(mlx5_buf_alloc);
70
71void mlx5_buf_free(struct mlx5_core_dev *dev, struct mlx5_buf *buf)
72{
73 dma_free_coherent(&dev->pdev->dev, buf->size, buf->direct.buf,
74 buf->direct.map);
75}
76EXPORT_SYMBOL_GPL(mlx5_buf_free);
77
78static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct device *dma_device)
79{
80 struct mlx5_db_pgdir *pgdir;
81
82 pgdir = kzalloc(sizeof(*pgdir), GFP_KERNEL);
83 if (!pgdir)
84 return NULL;
85
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) {
90 kfree(pgdir);
91 return NULL;
92 }
93
94 return pgdir;
95}
96
97static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir *pgdir,
98 struct mlx5_db *db)
99{
100 int offset;
101 int i;
102
103 i = find_first_bit(pgdir->bitmap, MLX5_DB_PER_PAGE);
104 if (i >= MLX5_DB_PER_PAGE)
105 return -ENOMEM;
106
107 __clear_bit(i, pgdir->bitmap);
108
109 db->u.pgdir = pgdir;
110 db->index = i;
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;
114
115 db->db[0] = 0;
116 db->db[1] = 0;
117
118 return 0;
119}
120
121int mlx5_db_alloc(struct mlx5_core_dev *dev, struct mlx5_db *db)
122{
123 struct mlx5_db_pgdir *pgdir;
124 int ret = 0;
125
126 mutex_lock(&dev->priv.pgdir_mutex);
127
128 list_for_each_entry(pgdir, &dev->priv.pgdir_list, list)
129 if (!mlx5_alloc_db_from_pgdir(pgdir, db))
130 goto out;
131
132 pgdir = mlx5_alloc_db_pgdir(&(dev->pdev->dev));
133 if (!pgdir) {
134 ret = -ENOMEM;
135 goto out;
136 }
137
138 list_add(&pgdir->list, &dev->priv.pgdir_list);
139
140
141 WARN_ON(mlx5_alloc_db_from_pgdir(pgdir, db));
142
143out:
144 mutex_unlock(&dev->priv.pgdir_mutex);
145
146 return ret;
147}
148EXPORT_SYMBOL_GPL(mlx5_db_alloc);
149
150void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db)
151{
152 mutex_lock(&dev->priv.pgdir_mutex);
153
154 __set_bit(db->index, db->u.pgdir->bitmap);
155
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);
160 kfree(db->u.pgdir);
161 }
162
163 mutex_unlock(&dev->priv.pgdir_mutex);
164}
165EXPORT_SYMBOL_GPL(mlx5_db_free);
166
167
168void mlx5_fill_page_array(struct mlx5_buf *buf, __be64 *pas)
169{
170 u64 addr;
171 int i;
172
173 for (i = 0; i < buf->npages; i++) {
174 addr = buf->direct.map + (i << buf->page_shift);
175
176 pas[i] = cpu_to_be64(addr);
177 }
178}
179EXPORT_SYMBOL_GPL(mlx5_fill_page_array);
180