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
34#include <linux/errno.h>
35#include <linux/export.h>
36#include <linux/io-mapping.h>
37
38#include <asm/page.h>
39
40#include "mlx4.h"
41#include "icm.h"
42
43enum {
44 MLX4_NUM_RESERVED_UARS = 8
45};
46
47int mlx4_pd_alloc(struct mlx4_dev *dev, u32 *pdn)
48{
49 struct mlx4_priv *priv = mlx4_priv(dev);
50
51 *pdn = mlx4_bitmap_alloc(&priv->pd_bitmap);
52 if (*pdn == -1)
53 return -ENOMEM;
54
55 return 0;
56}
57EXPORT_SYMBOL_GPL(mlx4_pd_alloc);
58
59void mlx4_pd_free(struct mlx4_dev *dev, u32 pdn)
60{
61 mlx4_bitmap_free(&mlx4_priv(dev)->pd_bitmap, pdn, MLX4_USE_RR);
62}
63EXPORT_SYMBOL_GPL(mlx4_pd_free);
64
65int __mlx4_xrcd_alloc(struct mlx4_dev *dev, u32 *xrcdn)
66{
67 struct mlx4_priv *priv = mlx4_priv(dev);
68
69 *xrcdn = mlx4_bitmap_alloc(&priv->xrcd_bitmap);
70 if (*xrcdn == -1)
71 return -ENOMEM;
72
73 return 0;
74}
75
76int mlx4_xrcd_alloc(struct mlx4_dev *dev, u32 *xrcdn)
77{
78 u64 out_param;
79 int err;
80
81 if (mlx4_is_mfunc(dev)) {
82 err = mlx4_cmd_imm(dev, 0, &out_param,
83 RES_XRCD, RES_OP_RESERVE,
84 MLX4_CMD_ALLOC_RES,
85 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
86 if (err)
87 return err;
88
89 *xrcdn = get_param_l(&out_param);
90 return 0;
91 }
92 return __mlx4_xrcd_alloc(dev, xrcdn);
93}
94EXPORT_SYMBOL_GPL(mlx4_xrcd_alloc);
95
96void __mlx4_xrcd_free(struct mlx4_dev *dev, u32 xrcdn)
97{
98 mlx4_bitmap_free(&mlx4_priv(dev)->xrcd_bitmap, xrcdn, MLX4_USE_RR);
99}
100
101void mlx4_xrcd_free(struct mlx4_dev *dev, u32 xrcdn)
102{
103 u64 in_param = 0;
104 int err;
105
106 if (mlx4_is_mfunc(dev)) {
107 set_param_l(&in_param, xrcdn);
108 err = mlx4_cmd(dev, in_param, RES_XRCD,
109 RES_OP_RESERVE, MLX4_CMD_FREE_RES,
110 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
111 if (err)
112 mlx4_warn(dev, "Failed to release xrcdn %d\n", xrcdn);
113 } else
114 __mlx4_xrcd_free(dev, xrcdn);
115}
116EXPORT_SYMBOL_GPL(mlx4_xrcd_free);
117
118int mlx4_init_pd_table(struct mlx4_dev *dev)
119{
120 struct mlx4_priv *priv = mlx4_priv(dev);
121
122 return mlx4_bitmap_init(&priv->pd_bitmap, dev->caps.num_pds,
123 (1 << NOT_MASKED_PD_BITS) - 1,
124 dev->caps.reserved_pds, 0);
125}
126
127void mlx4_cleanup_pd_table(struct mlx4_dev *dev)
128{
129 mlx4_bitmap_cleanup(&mlx4_priv(dev)->pd_bitmap);
130}
131
132int mlx4_init_xrcd_table(struct mlx4_dev *dev)
133{
134 struct mlx4_priv *priv = mlx4_priv(dev);
135
136 return mlx4_bitmap_init(&priv->xrcd_bitmap, (1 << 16),
137 (1 << 16) - 1, dev->caps.reserved_xrcds + 1, 0);
138}
139
140void mlx4_cleanup_xrcd_table(struct mlx4_dev *dev)
141{
142 mlx4_bitmap_cleanup(&mlx4_priv(dev)->xrcd_bitmap);
143}
144
145int mlx4_uar_alloc(struct mlx4_dev *dev, struct mlx4_uar *uar)
146{
147 int offset;
148
149 uar->index = mlx4_bitmap_alloc(&mlx4_priv(dev)->uar_table.bitmap);
150 if (uar->index == -1)
151 return -ENOMEM;
152
153 if (mlx4_is_slave(dev))
154 offset = uar->index % ((int)pci_resource_len(dev->persist->pdev,
155 2) /
156 dev->caps.uar_page_size);
157 else
158 offset = uar->index;
159 uar->pfn = (pci_resource_start(dev->persist->pdev, 2) >> PAGE_SHIFT)
160 + offset;
161 uar->map = NULL;
162 return 0;
163}
164EXPORT_SYMBOL_GPL(mlx4_uar_alloc);
165
166void mlx4_uar_free(struct mlx4_dev *dev, struct mlx4_uar *uar)
167{
168 mlx4_bitmap_free(&mlx4_priv(dev)->uar_table.bitmap, uar->index, MLX4_USE_RR);
169}
170EXPORT_SYMBOL_GPL(mlx4_uar_free);
171
172int mlx4_bf_alloc(struct mlx4_dev *dev, struct mlx4_bf *bf, int node)
173{
174 struct mlx4_priv *priv = mlx4_priv(dev);
175 struct mlx4_uar *uar;
176 int err = 0;
177 int idx;
178
179 if (!priv->bf_mapping)
180 return -ENOMEM;
181
182 mutex_lock(&priv->bf_mutex);
183 if (!list_empty(&priv->bf_list))
184 uar = list_entry(priv->bf_list.next, struct mlx4_uar, bf_list);
185 else {
186 if (mlx4_bitmap_avail(&priv->uar_table.bitmap) < MLX4_NUM_RESERVED_UARS) {
187 err = -ENOMEM;
188 goto out;
189 }
190 uar = kmalloc_node(sizeof(*uar), GFP_KERNEL, node);
191 if (!uar) {
192 uar = kmalloc(sizeof(*uar), GFP_KERNEL);
193 if (!uar) {
194 err = -ENOMEM;
195 goto out;
196 }
197 }
198 err = mlx4_uar_alloc(dev, uar);
199 if (err)
200 goto free_kmalloc;
201
202 uar->map = ioremap(uar->pfn << PAGE_SHIFT, PAGE_SIZE);
203 if (!uar->map) {
204 err = -ENOMEM;
205 goto free_uar;
206 }
207
208 uar->bf_map = io_mapping_map_wc(priv->bf_mapping, uar->index << PAGE_SHIFT);
209 if (!uar->bf_map) {
210 err = -ENOMEM;
211 goto unamp_uar;
212 }
213 uar->free_bf_bmap = 0;
214 list_add(&uar->bf_list, &priv->bf_list);
215 }
216
217 idx = ffz(uar->free_bf_bmap);
218 uar->free_bf_bmap |= 1 << idx;
219 bf->uar = uar;
220 bf->offset = 0;
221 bf->buf_size = dev->caps.bf_reg_size / 2;
222 bf->reg = uar->bf_map + idx * dev->caps.bf_reg_size;
223 if (uar->free_bf_bmap == (1 << dev->caps.bf_regs_per_page) - 1)
224 list_del_init(&uar->bf_list);
225
226 goto out;
227
228unamp_uar:
229 bf->uar = NULL;
230 iounmap(uar->map);
231
232free_uar:
233 mlx4_uar_free(dev, uar);
234
235free_kmalloc:
236 kfree(uar);
237
238out:
239 mutex_unlock(&priv->bf_mutex);
240 return err;
241}
242EXPORT_SYMBOL_GPL(mlx4_bf_alloc);
243
244void mlx4_bf_free(struct mlx4_dev *dev, struct mlx4_bf *bf)
245{
246 struct mlx4_priv *priv = mlx4_priv(dev);
247 int idx;
248
249 if (!bf->uar || !bf->uar->bf_map)
250 return;
251
252 mutex_lock(&priv->bf_mutex);
253 idx = (bf->reg - bf->uar->bf_map) / dev->caps.bf_reg_size;
254 bf->uar->free_bf_bmap &= ~(1 << idx);
255 if (!bf->uar->free_bf_bmap) {
256 if (!list_empty(&bf->uar->bf_list))
257 list_del(&bf->uar->bf_list);
258
259 io_mapping_unmap(bf->uar->bf_map);
260 iounmap(bf->uar->map);
261 mlx4_uar_free(dev, bf->uar);
262 kfree(bf->uar);
263 } else if (list_empty(&bf->uar->bf_list))
264 list_add(&bf->uar->bf_list, &priv->bf_list);
265
266 mutex_unlock(&priv->bf_mutex);
267}
268EXPORT_SYMBOL_GPL(mlx4_bf_free);
269
270int mlx4_init_uar_table(struct mlx4_dev *dev)
271{
272 if (dev->caps.num_uars <= 128) {
273 mlx4_err(dev, "Only %d UAR pages (need more than 128)\n",
274 dev->caps.num_uars);
275 mlx4_err(dev, "Increase firmware log2_uar_bar_megabytes?\n");
276 return -ENODEV;
277 }
278
279 return mlx4_bitmap_init(&mlx4_priv(dev)->uar_table.bitmap,
280 dev->caps.num_uars, dev->caps.num_uars - 1,
281 dev->caps.reserved_uars, 0);
282}
283
284void mlx4_cleanup_uar_table(struct mlx4_dev *dev)
285{
286 mlx4_bitmap_cleanup(&mlx4_priv(dev)->uar_table.bitmap);
287}
288