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/kernel.h>
34 #include <linux/module.h>
35 #include <linux/io-mapping.h>
36 #include <linux/mlx5/driver.h>
37 #include <linux/mlx5/cmd.h>
38 #include "mlx5_core.h"
40 int mlx5_cmd_alloc_uar(struct mlx5_core_dev *dev, u32 *uarn)
42 u32 out[MLX5_ST_SZ_DW(alloc_uar_out)] = {0};
43 u32 in[MLX5_ST_SZ_DW(alloc_uar_in)] = {0};
46 MLX5_SET(alloc_uar_in, in, opcode, MLX5_CMD_OP_ALLOC_UAR);
47 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
49 *uarn = MLX5_GET(alloc_uar_out, out, uar);
52 EXPORT_SYMBOL(mlx5_cmd_alloc_uar);
54 int mlx5_cmd_free_uar(struct mlx5_core_dev *dev, u32 uarn)
56 u32 out[MLX5_ST_SZ_DW(dealloc_uar_out)] = {0};
57 u32 in[MLX5_ST_SZ_DW(dealloc_uar_in)] = {0};
59 MLX5_SET(dealloc_uar_in, in, opcode, MLX5_CMD_OP_DEALLOC_UAR);
60 MLX5_SET(dealloc_uar_in, in, uar, uarn);
61 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
63 EXPORT_SYMBOL(mlx5_cmd_free_uar);
65 static int uars_per_sys_page(struct mlx5_core_dev *mdev)
67 if (MLX5_CAP_GEN(mdev, uar_4k))
68 return MLX5_CAP_GEN(mdev, num_of_uars_per_page);
73 static u64 uar2pfn(struct mlx5_core_dev *mdev, u32 index)
75 u32 system_page_index;
77 if (MLX5_CAP_GEN(mdev, uar_4k))
78 system_page_index = index >> (PAGE_SHIFT - MLX5_ADAPTER_PAGE_SHIFT);
80 system_page_index = index;
82 return (pci_resource_start(mdev->pdev, 0) >> PAGE_SHIFT) + system_page_index;
85 static void up_rel_func(struct kref *kref)
87 struct mlx5_uars_page *up = container_of(kref, struct mlx5_uars_page, ref_count);
91 if (mlx5_cmd_free_uar(up->mdev, up->index))
92 mlx5_core_warn(up->mdev, "failed to free uar index %d\n", up->index);
93 kfree(up->reg_bitmap);
98 static struct mlx5_uars_page *alloc_uars_page(struct mlx5_core_dev *mdev,
101 struct mlx5_uars_page *up;
107 bfregs = uars_per_sys_page(mdev) * MLX5_BFREGS_PER_UAR;
108 up = kzalloc(sizeof(*up), GFP_KERNEL);
113 up->reg_bitmap = kcalloc(BITS_TO_LONGS(bfregs), sizeof(unsigned long), GFP_KERNEL);
117 up->fp_bitmap = kcalloc(BITS_TO_LONGS(bfregs), sizeof(unsigned long), GFP_KERNEL);
121 for (i = 0; i < bfregs; i++)
122 if ((i % MLX5_BFREGS_PER_UAR) < MLX5_NON_FP_BFREGS_PER_UAR)
123 set_bit(i, up->reg_bitmap);
125 set_bit(i, up->fp_bitmap);
128 up->fp_avail = bfregs * MLX5_FP_BFREGS_PER_UAR / MLX5_BFREGS_PER_UAR;
129 up->reg_avail = bfregs * MLX5_NON_FP_BFREGS_PER_UAR / MLX5_BFREGS_PER_UAR;
131 err = mlx5_cmd_alloc_uar(mdev, &up->index);
133 mlx5_core_warn(mdev, "mlx5_cmd_alloc_uar() failed, %d\n", err);
137 pfn = uar2pfn(mdev, up->index);
139 up->map = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
145 up->map = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE);
151 kref_init(&up->ref_count);
152 mlx5_core_dbg(mdev, "allocated UAR page: index %d, total bfregs %d\n",
153 up->index, up->bfregs);
157 if (mlx5_cmd_free_uar(mdev, up->index))
158 mlx5_core_warn(mdev, "failed to free uar index %d\n", up->index);
160 kfree(up->fp_bitmap);
161 kfree(up->reg_bitmap);
166 struct mlx5_uars_page *mlx5_get_uars_page(struct mlx5_core_dev *mdev)
168 struct mlx5_uars_page *ret;
170 mutex_lock(&mdev->priv.bfregs.reg_head.lock);
171 if (list_empty(&mdev->priv.bfregs.reg_head.list)) {
172 ret = alloc_uars_page(mdev, false);
177 list_add(&ret->list, &mdev->priv.bfregs.reg_head.list);
179 ret = list_first_entry(&mdev->priv.bfregs.reg_head.list,
180 struct mlx5_uars_page, list);
181 kref_get(&ret->ref_count);
184 mutex_unlock(&mdev->priv.bfregs.reg_head.lock);
188 EXPORT_SYMBOL(mlx5_get_uars_page);
190 void mlx5_put_uars_page(struct mlx5_core_dev *mdev, struct mlx5_uars_page *up)
192 mutex_lock(&mdev->priv.bfregs.reg_head.lock);
193 kref_put(&up->ref_count, up_rel_func);
194 mutex_unlock(&mdev->priv.bfregs.reg_head.lock);
196 EXPORT_SYMBOL(mlx5_put_uars_page);
198 static unsigned long map_offset(struct mlx5_core_dev *mdev, int dbi)
200 /* return the offset in bytes from the start of the page to the
201 * blue flame area of the UAR
203 return dbi / MLX5_BFREGS_PER_UAR * MLX5_ADAPTER_PAGE_SIZE +
204 (dbi % MLX5_BFREGS_PER_UAR) *
205 (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) + MLX5_BF_OFFSET;
208 static int alloc_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg,
209 bool map_wc, bool fast_path)
211 struct mlx5_bfreg_data *bfregs;
212 struct mlx5_uars_page *up;
213 struct list_head *head;
214 unsigned long *bitmap;
216 struct mutex *lock; /* pointer to right mutex */
219 bfregs = &mdev->priv.bfregs;
221 head = &bfregs->wc_head.list;
222 lock = &bfregs->wc_head.lock;
224 head = &bfregs->reg_head.list;
225 lock = &bfregs->reg_head.lock;
228 if (list_empty(head)) {
229 up = alloc_uars_page(mdev, map_wc);
234 list_add(&up->list, head);
236 up = list_entry(head->next, struct mlx5_uars_page, list);
237 kref_get(&up->ref_count);
240 bitmap = up->fp_bitmap;
241 avail = &up->fp_avail;
243 bitmap = up->reg_bitmap;
244 avail = &up->reg_avail;
246 dbi = find_first_bit(bitmap, up->bfregs);
247 clear_bit(dbi, bitmap);
252 bfreg->map = up->map + map_offset(mdev, dbi);
255 bfreg->index = up->index + dbi / MLX5_BFREGS_PER_UAR;
261 int mlx5_alloc_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg,
262 bool map_wc, bool fast_path)
266 err = alloc_bfreg(mdev, bfreg, map_wc, fast_path);
270 if (err == -EAGAIN && map_wc)
271 return alloc_bfreg(mdev, bfreg, false, fast_path);
275 EXPORT_SYMBOL(mlx5_alloc_bfreg);
277 static unsigned int addr_to_dbi_in_syspage(struct mlx5_core_dev *dev,
278 struct mlx5_uars_page *up,
279 struct mlx5_sq_bfreg *bfreg)
281 unsigned int uar_idx;
282 unsigned int bfreg_idx;
283 unsigned int bf_reg_size;
285 bf_reg_size = 1 << MLX5_CAP_GEN(dev, log_bf_reg_size);
287 uar_idx = (bfreg->map - up->map) >> MLX5_ADAPTER_PAGE_SHIFT;
288 bfreg_idx = (((uintptr_t)bfreg->map % MLX5_ADAPTER_PAGE_SIZE) - MLX5_BF_OFFSET) / bf_reg_size;
290 return uar_idx * MLX5_BFREGS_PER_UAR + bfreg_idx;
293 void mlx5_free_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg)
295 struct mlx5_bfreg_data *bfregs;
296 struct mlx5_uars_page *up;
297 struct mutex *lock; /* pointer to right mutex */
301 unsigned long *bitmap;
302 struct list_head *head;
304 bfregs = &mdev->priv.bfregs;
306 head = &bfregs->wc_head.list;
307 lock = &bfregs->wc_head.lock;
309 head = &bfregs->reg_head.list;
310 lock = &bfregs->reg_head.lock;
313 dbi = addr_to_dbi_in_syspage(mdev, up, bfreg);
314 fp = (dbi % MLX5_BFREGS_PER_UAR) >= MLX5_NON_FP_BFREGS_PER_UAR;
316 avail = &up->fp_avail;
317 bitmap = up->fp_bitmap;
319 avail = &up->reg_avail;
320 bitmap = up->reg_bitmap;
324 set_bit(dbi, bitmap);
326 list_add_tail(&up->list, head);
328 kref_put(&up->ref_count, up_rel_func);
331 EXPORT_SYMBOL(mlx5_free_bfreg);