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"
42 NUM_LOW_LAT_BFREGS = 4,
45 int mlx5_cmd_alloc_uar(struct mlx5_core_dev *dev, u32 *uarn)
47 u32 out[MLX5_ST_SZ_DW(alloc_uar_out)] = {0};
48 u32 in[MLX5_ST_SZ_DW(alloc_uar_in)] = {0};
51 MLX5_SET(alloc_uar_in, in, opcode, MLX5_CMD_OP_ALLOC_UAR);
52 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
54 *uarn = MLX5_GET(alloc_uar_out, out, uar);
57 EXPORT_SYMBOL(mlx5_cmd_alloc_uar);
59 int mlx5_cmd_free_uar(struct mlx5_core_dev *dev, u32 uarn)
61 u32 out[MLX5_ST_SZ_DW(dealloc_uar_out)] = {0};
62 u32 in[MLX5_ST_SZ_DW(dealloc_uar_in)] = {0};
64 MLX5_SET(dealloc_uar_in, in, opcode, MLX5_CMD_OP_DEALLOC_UAR);
65 MLX5_SET(dealloc_uar_in, in, uar, uarn);
66 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
68 EXPORT_SYMBOL(mlx5_cmd_free_uar);
70 static int need_bfreg_lock(int bfregn)
72 int tot_bfregs = NUM_DRIVER_UARS * MLX5_BFREGS_PER_UAR;
74 if (bfregn == 0 || tot_bfregs - NUM_LOW_LAT_BFREGS)
80 int mlx5_alloc_bfregs(struct mlx5_core_dev *dev, struct mlx5_bfreg_info *bfregi)
82 int tot_bfregs = NUM_DRIVER_UARS * MLX5_BFREGS_PER_UAR;
88 bfregi->num_uars = NUM_DRIVER_UARS;
89 bfregi->num_low_latency_bfregs = NUM_LOW_LAT_BFREGS;
91 mutex_init(&bfregi->lock);
92 bfregi->uars = kcalloc(bfregi->num_uars, sizeof(*bfregi->uars), GFP_KERNEL);
96 bfregi->bfs = kcalloc(tot_bfregs, sizeof(*bfregi->bfs), GFP_KERNEL);
102 bfregi->bitmap = kcalloc(BITS_TO_LONGS(tot_bfregs), sizeof(*bfregi->bitmap),
104 if (!bfregi->bitmap) {
109 bfregi->count = kcalloc(tot_bfregs, sizeof(*bfregi->count), GFP_KERNEL);
110 if (!bfregi->count) {
115 for (i = 0; i < bfregi->num_uars; i++) {
116 err = mlx5_cmd_alloc_uar(dev, &bfregi->uars[i].index);
120 addr = dev->iseg_base + ((phys_addr_t)(bfregi->uars[i].index) << PAGE_SHIFT);
121 bfregi->uars[i].map = ioremap(addr, PAGE_SIZE);
122 if (!bfregi->uars[i].map) {
123 mlx5_cmd_free_uar(dev, bfregi->uars[i].index);
127 mlx5_core_dbg(dev, "allocated uar index 0x%x, mmaped at %p\n",
128 bfregi->uars[i].index, bfregi->uars[i].map);
131 for (i = 0; i < tot_bfregs; i++) {
132 bf = &bfregi->bfs[i];
134 bf->buf_size = (1 << MLX5_CAP_GEN(dev, log_bf_reg_size)) / 2;
135 bf->uar = &bfregi->uars[i / MLX5_BFREGS_PER_UAR];
136 bf->regreg = bfregi->uars[i / MLX5_BFREGS_PER_UAR].map;
137 bf->reg = NULL; /* Add WC support */
138 bf->offset = (i % MLX5_BFREGS_PER_UAR) *
139 (1 << MLX5_CAP_GEN(dev, log_bf_reg_size)) +
141 bf->need_lock = need_bfreg_lock(i);
142 spin_lock_init(&bf->lock);
143 spin_lock_init(&bf->lock32);
150 for (i--; i >= 0; i--) {
151 iounmap(bfregi->uars[i].map);
152 mlx5_cmd_free_uar(dev, bfregi->uars[i].index);
154 kfree(bfregi->count);
157 kfree(bfregi->bitmap);
167 int mlx5_free_bfregs(struct mlx5_core_dev *dev, struct mlx5_bfreg_info *bfregi)
169 int i = bfregi->num_uars;
171 for (i--; i >= 0; i--) {
172 iounmap(bfregi->uars[i].map);
173 mlx5_cmd_free_uar(dev, bfregi->uars[i].index);
176 kfree(bfregi->count);
177 kfree(bfregi->bitmap);
184 int mlx5_alloc_map_uar(struct mlx5_core_dev *mdev, struct mlx5_uar *uar,
188 phys_addr_t uar_bar_start;
191 err = mlx5_cmd_alloc_uar(mdev, &uar->index);
193 mlx5_core_warn(mdev, "mlx5_cmd_alloc_uar() failed, %d\n", err);
197 uar_bar_start = pci_resource_start(mdev->pdev, 0);
198 pfn = (uar_bar_start >> PAGE_SHIFT) + uar->index;
201 uar->bf_map = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
203 mlx5_core_warn(mdev, "ioremap_wc() failed\n");
204 uar->map = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE);
209 uar->map = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE);
217 mlx5_core_warn(mdev, "ioremap() failed\n");
219 mlx5_cmd_free_uar(mdev, uar->index);
223 EXPORT_SYMBOL(mlx5_alloc_map_uar);
225 void mlx5_unmap_free_uar(struct mlx5_core_dev *mdev, struct mlx5_uar *uar)
230 iounmap(uar->bf_map);
231 mlx5_cmd_free_uar(mdev, uar->index);
233 EXPORT_SYMBOL(mlx5_unmap_free_uar);
235 static int uars_per_sys_page(struct mlx5_core_dev *mdev)
237 if (MLX5_CAP_GEN(mdev, uar_4k))
238 return MLX5_CAP_GEN(mdev, num_of_uars_per_page);
243 static u64 uar2pfn(struct mlx5_core_dev *mdev, u32 index)
245 u32 system_page_index;
247 if (MLX5_CAP_GEN(mdev, uar_4k))
248 system_page_index = index >> (PAGE_SHIFT - MLX5_ADAPTER_PAGE_SHIFT);
250 system_page_index = index;
252 return (pci_resource_start(mdev->pdev, 0) >> PAGE_SHIFT) + system_page_index;
255 static void up_rel_func(struct kref *kref)
257 struct mlx5_uars_page *up = container_of(kref, struct mlx5_uars_page, ref_count);
260 if (mlx5_cmd_free_uar(up->mdev, up->index))
261 mlx5_core_warn(up->mdev, "failed to free uar index %d\n", up->index);
262 kfree(up->reg_bitmap);
263 kfree(up->fp_bitmap);
267 static struct mlx5_uars_page *alloc_uars_page(struct mlx5_core_dev *mdev,
270 struct mlx5_uars_page *up;
276 bfregs = uars_per_sys_page(mdev) * MLX5_BFREGS_PER_UAR;
277 up = kzalloc(sizeof(*up), GFP_KERNEL);
282 up->reg_bitmap = kcalloc(BITS_TO_LONGS(bfregs), sizeof(unsigned long), GFP_KERNEL);
286 up->fp_bitmap = kcalloc(BITS_TO_LONGS(bfregs), sizeof(unsigned long), GFP_KERNEL);
290 for (i = 0; i < bfregs; i++)
291 if ((i % MLX5_BFREGS_PER_UAR) < MLX5_NON_FP_BFREGS_PER_UAR)
292 set_bit(i, up->reg_bitmap);
294 set_bit(i, up->fp_bitmap);
297 up->fp_avail = bfregs * MLX5_FP_BFREGS_PER_UAR / MLX5_BFREGS_PER_UAR;
298 up->reg_avail = bfregs * MLX5_NON_FP_BFREGS_PER_UAR / MLX5_BFREGS_PER_UAR;
300 err = mlx5_cmd_alloc_uar(mdev, &up->index);
302 mlx5_core_warn(mdev, "mlx5_cmd_alloc_uar() failed, %d\n", err);
306 pfn = uar2pfn(mdev, up->index);
308 up->map = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
314 up->map = ioremap(pfn << PAGE_SHIFT, PAGE_SIZE);
320 kref_init(&up->ref_count);
321 mlx5_core_dbg(mdev, "allocated UAR page: index %d, total bfregs %d\n",
322 up->index, up->bfregs);
326 if (mlx5_cmd_free_uar(mdev, up->index))
327 mlx5_core_warn(mdev, "failed to free uar index %d\n", up->index);
329 kfree(up->fp_bitmap);
330 kfree(up->reg_bitmap);
335 struct mlx5_uars_page *mlx5_get_uars_page(struct mlx5_core_dev *mdev)
337 struct mlx5_uars_page *ret;
339 mutex_lock(&mdev->priv.bfregs.reg_head.lock);
340 if (list_empty(&mdev->priv.bfregs.reg_head.list)) {
341 ret = alloc_uars_page(mdev, false);
346 list_add(&ret->list, &mdev->priv.bfregs.reg_head.list);
348 ret = list_first_entry(&mdev->priv.bfregs.reg_head.list,
349 struct mlx5_uars_page, list);
350 kref_get(&ret->ref_count);
353 mutex_unlock(&mdev->priv.bfregs.reg_head.lock);
357 EXPORT_SYMBOL(mlx5_get_uars_page);
359 void mlx5_put_uars_page(struct mlx5_core_dev *mdev, struct mlx5_uars_page *up)
361 mutex_lock(&mdev->priv.bfregs.reg_head.lock);
362 kref_put(&up->ref_count, up_rel_func);
363 mutex_unlock(&mdev->priv.bfregs.reg_head.lock);
365 EXPORT_SYMBOL(mlx5_put_uars_page);
367 static unsigned long map_offset(struct mlx5_core_dev *mdev, int dbi)
369 /* return the offset in bytes from the start of the page to the
370 * blue flame area of the UAR
372 return dbi / MLX5_BFREGS_PER_UAR * MLX5_ADAPTER_PAGE_SIZE +
373 (dbi % MLX5_BFREGS_PER_UAR) *
374 (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) + MLX5_BF_OFFSET;
377 static int alloc_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg,
378 bool map_wc, bool fast_path)
380 struct mlx5_bfreg_data *bfregs;
381 struct mlx5_uars_page *up;
382 struct list_head *head;
383 unsigned long *bitmap;
385 struct mutex *lock; /* pointer to right mutex */
388 bfregs = &mdev->priv.bfregs;
390 head = &bfregs->wc_head.list;
391 lock = &bfregs->wc_head.lock;
393 head = &bfregs->reg_head.list;
394 lock = &bfregs->reg_head.lock;
397 if (list_empty(head)) {
398 up = alloc_uars_page(mdev, map_wc);
403 list_add(&up->list, head);
405 up = list_entry(head->next, struct mlx5_uars_page, list);
406 kref_get(&up->ref_count);
409 bitmap = up->fp_bitmap;
410 avail = &up->fp_avail;
412 bitmap = up->reg_bitmap;
413 avail = &up->reg_avail;
415 dbi = find_first_bit(bitmap, up->bfregs);
416 clear_bit(dbi, bitmap);
421 bfreg->map = up->map + map_offset(mdev, dbi);
424 bfreg->index = up->index + dbi / MLX5_BFREGS_PER_UAR;
430 int mlx5_alloc_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg,
431 bool map_wc, bool fast_path)
435 err = alloc_bfreg(mdev, bfreg, map_wc, fast_path);
439 if (err == -EAGAIN && map_wc)
440 return alloc_bfreg(mdev, bfreg, false, fast_path);
444 EXPORT_SYMBOL(mlx5_alloc_bfreg);
446 static unsigned int addr_to_dbi_in_syspage(struct mlx5_core_dev *dev,
447 struct mlx5_uars_page *up,
448 struct mlx5_sq_bfreg *bfreg)
450 unsigned int uar_idx;
451 unsigned int bfreg_idx;
452 unsigned int bf_reg_size;
454 bf_reg_size = 1 << MLX5_CAP_GEN(dev, log_bf_reg_size);
456 uar_idx = (bfreg->map - up->map) >> MLX5_ADAPTER_PAGE_SHIFT;
457 bfreg_idx = (((uintptr_t)bfreg->map % MLX5_ADAPTER_PAGE_SIZE) - MLX5_BF_OFFSET) / bf_reg_size;
459 return uar_idx * MLX5_BFREGS_PER_UAR + bfreg_idx;
462 void mlx5_free_bfreg(struct mlx5_core_dev *mdev, struct mlx5_sq_bfreg *bfreg)
464 struct mlx5_bfreg_data *bfregs;
465 struct mlx5_uars_page *up;
466 struct mutex *lock; /* pointer to right mutex */
470 unsigned long *bitmap;
471 struct list_head *head;
473 bfregs = &mdev->priv.bfregs;
475 head = &bfregs->wc_head.list;
476 lock = &bfregs->wc_head.lock;
478 head = &bfregs->reg_head.list;
479 lock = &bfregs->reg_head.lock;
482 dbi = addr_to_dbi_in_syspage(mdev, up, bfreg);
483 fp = (dbi % MLX5_BFREGS_PER_UAR) >= MLX5_NON_FP_BFREGS_PER_UAR;
485 avail = &up->fp_avail;
486 bitmap = up->fp_bitmap;
488 avail = &up->reg_avail;
489 bitmap = up->reg_bitmap;
493 set_bit(dbi, bitmap);
495 list_add_tail(&up->list, head);
497 kref_put(&up->ref_count, up_rel_func);
500 EXPORT_SYMBOL(mlx5_free_bfreg);