18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
68c2ecf20Sopenharmony_ci * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
98c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
108c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
118c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
128c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
158c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
168c2ecf20Sopenharmony_ci *     conditions are met:
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
238c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
248c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
258c2ecf20Sopenharmony_ci *        provided with the distribution.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
288c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
298c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
308c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
318c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
328c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
338c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
348c2ecf20Sopenharmony_ci * SOFTWARE.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <linux/hardirq.h>
388c2ecf20Sopenharmony_ci#include <linux/export.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <linux/mlx4/cmd.h>
418c2ecf20Sopenharmony_ci#include <linux/mlx4/cq.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#include "mlx4.h"
448c2ecf20Sopenharmony_ci#include "icm.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define MLX4_CQ_STATUS_OK		( 0 << 28)
478c2ecf20Sopenharmony_ci#define MLX4_CQ_STATUS_OVERFLOW		( 9 << 28)
488c2ecf20Sopenharmony_ci#define MLX4_CQ_STATUS_WRITE_FAIL	(10 << 28)
498c2ecf20Sopenharmony_ci#define MLX4_CQ_FLAG_CC			( 1 << 18)
508c2ecf20Sopenharmony_ci#define MLX4_CQ_FLAG_OI			( 1 << 17)
518c2ecf20Sopenharmony_ci#define MLX4_CQ_STATE_ARMED		( 9 <<  8)
528c2ecf20Sopenharmony_ci#define MLX4_CQ_STATE_ARMED_SOL		( 6 <<  8)
538c2ecf20Sopenharmony_ci#define MLX4_EQ_STATE_FIRED		(10 <<  8)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define TASKLET_MAX_TIME 2
568c2ecf20Sopenharmony_ci#define TASKLET_MAX_TIME_JIFFIES msecs_to_jiffies(TASKLET_MAX_TIME)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_civoid mlx4_cq_tasklet_cb(struct tasklet_struct *t)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned long flags;
618c2ecf20Sopenharmony_ci	unsigned long end = jiffies + TASKLET_MAX_TIME_JIFFIES;
628c2ecf20Sopenharmony_ci	struct mlx4_eq_tasklet *ctx = from_tasklet(ctx, t, task);
638c2ecf20Sopenharmony_ci	struct mlx4_cq *mcq, *temp;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ctx->lock, flags);
668c2ecf20Sopenharmony_ci	list_splice_tail_init(&ctx->list, &ctx->process_list);
678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ctx->lock, flags);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	list_for_each_entry_safe(mcq, temp, &ctx->process_list, tasklet_ctx.list) {
708c2ecf20Sopenharmony_ci		list_del_init(&mcq->tasklet_ctx.list);
718c2ecf20Sopenharmony_ci		mcq->tasklet_ctx.comp(mcq);
728c2ecf20Sopenharmony_ci		if (refcount_dec_and_test(&mcq->refcount))
738c2ecf20Sopenharmony_ci			complete(&mcq->free);
748c2ecf20Sopenharmony_ci		if (time_after(jiffies, end))
758c2ecf20Sopenharmony_ci			break;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (!list_empty(&ctx->process_list))
798c2ecf20Sopenharmony_ci		tasklet_schedule(&ctx->task);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void mlx4_add_cq_to_tasklet(struct mlx4_cq *cq)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct mlx4_eq_tasklet *tasklet_ctx = cq->tasklet_ctx.priv;
858c2ecf20Sopenharmony_ci	unsigned long flags;
868c2ecf20Sopenharmony_ci	bool kick;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&tasklet_ctx->lock, flags);
898c2ecf20Sopenharmony_ci	/* When migrating CQs between EQs will be implemented, please note
908c2ecf20Sopenharmony_ci	 * that you need to sync this point. It is possible that
918c2ecf20Sopenharmony_ci	 * while migrating a CQ, completions on the old EQs could
928c2ecf20Sopenharmony_ci	 * still arrive.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	if (list_empty_careful(&cq->tasklet_ctx.list)) {
958c2ecf20Sopenharmony_ci		refcount_inc(&cq->refcount);
968c2ecf20Sopenharmony_ci		kick = list_empty(&tasklet_ctx->list);
978c2ecf20Sopenharmony_ci		list_add_tail(&cq->tasklet_ctx.list, &tasklet_ctx->list);
988c2ecf20Sopenharmony_ci		if (kick)
998c2ecf20Sopenharmony_ci			tasklet_schedule(&tasklet_ctx->task);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&tasklet_ctx->lock, flags);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_civoid mlx4_cq_completion(struct mlx4_dev *dev, u32 cqn)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct mlx4_cq *cq;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	rcu_read_lock();
1098c2ecf20Sopenharmony_ci	cq = radix_tree_lookup(&mlx4_priv(dev)->cq_table.tree,
1108c2ecf20Sopenharmony_ci			       cqn & (dev->caps.num_cqs - 1));
1118c2ecf20Sopenharmony_ci	rcu_read_unlock();
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (!cq) {
1148c2ecf20Sopenharmony_ci		mlx4_dbg(dev, "Completion event for bogus CQ %08x\n", cqn);
1158c2ecf20Sopenharmony_ci		return;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Acessing the CQ outside of rcu_read_lock is safe, because
1198c2ecf20Sopenharmony_ci	 * the CQ is freed only after interrupt handling is completed.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	++cq->arm_sn;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	cq->comp(cq);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_civoid mlx4_cq_event(struct mlx4_dev *dev, u32 cqn, int event_type)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &mlx4_priv(dev)->cq_table;
1298c2ecf20Sopenharmony_ci	struct mlx4_cq *cq;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	rcu_read_lock();
1328c2ecf20Sopenharmony_ci	cq = radix_tree_lookup(&cq_table->tree, cqn & (dev->caps.num_cqs - 1));
1338c2ecf20Sopenharmony_ci	rcu_read_unlock();
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (!cq) {
1368c2ecf20Sopenharmony_ci		mlx4_dbg(dev, "Async event for bogus CQ %08x\n", cqn);
1378c2ecf20Sopenharmony_ci		return;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* Acessing the CQ outside of rcu_read_lock is safe, because
1418c2ecf20Sopenharmony_ci	 * the CQ is freed only after interrupt handling is completed.
1428c2ecf20Sopenharmony_ci	 */
1438c2ecf20Sopenharmony_ci	cq->event(cq, event_type);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int mlx4_SW2HW_CQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
1478c2ecf20Sopenharmony_ci			 int cq_num, u8 opmod)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	return mlx4_cmd(dev, mailbox->dma, cq_num, opmod,
1508c2ecf20Sopenharmony_ci			MLX4_CMD_SW2HW_CQ, MLX4_CMD_TIME_CLASS_A,
1518c2ecf20Sopenharmony_ci			MLX4_CMD_WRAPPED);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int mlx4_MODIFY_CQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
1558c2ecf20Sopenharmony_ci			 int cq_num, u32 opmod)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	return mlx4_cmd(dev, mailbox->dma, cq_num, opmod, MLX4_CMD_MODIFY_CQ,
1588c2ecf20Sopenharmony_ci			MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int mlx4_HW2SW_CQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
1628c2ecf20Sopenharmony_ci			 int cq_num)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0,
1658c2ecf20Sopenharmony_ci			    cq_num, mailbox ? 0 : 1, MLX4_CMD_HW2SW_CQ,
1668c2ecf20Sopenharmony_ci			    MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciint mlx4_cq_modify(struct mlx4_dev *dev, struct mlx4_cq *cq,
1708c2ecf20Sopenharmony_ci		   u16 count, u16 period)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct mlx4_cmd_mailbox *mailbox;
1738c2ecf20Sopenharmony_ci	struct mlx4_cq_context *cq_context;
1748c2ecf20Sopenharmony_ci	int err;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	mailbox = mlx4_alloc_cmd_mailbox(dev);
1778c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox))
1788c2ecf20Sopenharmony_ci		return PTR_ERR(mailbox);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	cq_context = mailbox->buf;
1818c2ecf20Sopenharmony_ci	cq_context->cq_max_count = cpu_to_be16(count);
1828c2ecf20Sopenharmony_ci	cq_context->cq_period    = cpu_to_be16(period);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	err = mlx4_MODIFY_CQ(dev, mailbox, cq->cqn, 1);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	mlx4_free_cmd_mailbox(dev, mailbox);
1878c2ecf20Sopenharmony_ci	return err;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mlx4_cq_modify);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciint mlx4_cq_resize(struct mlx4_dev *dev, struct mlx4_cq *cq,
1928c2ecf20Sopenharmony_ci		   int entries, struct mlx4_mtt *mtt)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct mlx4_cmd_mailbox *mailbox;
1958c2ecf20Sopenharmony_ci	struct mlx4_cq_context *cq_context;
1968c2ecf20Sopenharmony_ci	u64 mtt_addr;
1978c2ecf20Sopenharmony_ci	int err;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	mailbox = mlx4_alloc_cmd_mailbox(dev);
2008c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox))
2018c2ecf20Sopenharmony_ci		return PTR_ERR(mailbox);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	cq_context = mailbox->buf;
2048c2ecf20Sopenharmony_ci	cq_context->logsize_usrpage = cpu_to_be32(ilog2(entries) << 24);
2058c2ecf20Sopenharmony_ci	cq_context->log_page_size   = mtt->page_shift - 12;
2068c2ecf20Sopenharmony_ci	mtt_addr = mlx4_mtt_addr(dev, mtt);
2078c2ecf20Sopenharmony_ci	cq_context->mtt_base_addr_h = mtt_addr >> 32;
2088c2ecf20Sopenharmony_ci	cq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	err = mlx4_MODIFY_CQ(dev, mailbox, cq->cqn, 0);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	mlx4_free_cmd_mailbox(dev, mailbox);
2138c2ecf20Sopenharmony_ci	return err;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mlx4_cq_resize);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ciint __mlx4_cq_alloc_icm(struct mlx4_dev *dev, int *cqn)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct mlx4_priv *priv = mlx4_priv(dev);
2208c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &priv->cq_table;
2218c2ecf20Sopenharmony_ci	int err;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	*cqn = mlx4_bitmap_alloc(&cq_table->bitmap);
2248c2ecf20Sopenharmony_ci	if (*cqn == -1)
2258c2ecf20Sopenharmony_ci		return -ENOMEM;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	err = mlx4_table_get(dev, &cq_table->table, *cqn);
2288c2ecf20Sopenharmony_ci	if (err)
2298c2ecf20Sopenharmony_ci		goto err_out;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	err = mlx4_table_get(dev, &cq_table->cmpt_table, *cqn);
2328c2ecf20Sopenharmony_ci	if (err)
2338c2ecf20Sopenharmony_ci		goto err_put;
2348c2ecf20Sopenharmony_ci	return 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cierr_put:
2378c2ecf20Sopenharmony_ci	mlx4_table_put(dev, &cq_table->table, *cqn);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cierr_out:
2408c2ecf20Sopenharmony_ci	mlx4_bitmap_free(&cq_table->bitmap, *cqn, MLX4_NO_RR);
2418c2ecf20Sopenharmony_ci	return err;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int mlx4_cq_alloc_icm(struct mlx4_dev *dev, int *cqn, u8 usage)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	u32 in_modifier = RES_CQ | (((u32)usage & 3) << 30);
2478c2ecf20Sopenharmony_ci	u64 out_param;
2488c2ecf20Sopenharmony_ci	int err;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (mlx4_is_mfunc(dev)) {
2518c2ecf20Sopenharmony_ci		err = mlx4_cmd_imm(dev, 0, &out_param, in_modifier,
2528c2ecf20Sopenharmony_ci				   RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
2538c2ecf20Sopenharmony_ci				   MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
2548c2ecf20Sopenharmony_ci		if (err)
2558c2ecf20Sopenharmony_ci			return err;
2568c2ecf20Sopenharmony_ci		else {
2578c2ecf20Sopenharmony_ci			*cqn = get_param_l(&out_param);
2588c2ecf20Sopenharmony_ci			return 0;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	return __mlx4_cq_alloc_icm(dev, cqn);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_civoid __mlx4_cq_free_icm(struct mlx4_dev *dev, int cqn)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	struct mlx4_priv *priv = mlx4_priv(dev);
2678c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &priv->cq_table;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	mlx4_table_put(dev, &cq_table->cmpt_table, cqn);
2708c2ecf20Sopenharmony_ci	mlx4_table_put(dev, &cq_table->table, cqn);
2718c2ecf20Sopenharmony_ci	mlx4_bitmap_free(&cq_table->bitmap, cqn, MLX4_NO_RR);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic void mlx4_cq_free_icm(struct mlx4_dev *dev, int cqn)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	u64 in_param = 0;
2778c2ecf20Sopenharmony_ci	int err;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (mlx4_is_mfunc(dev)) {
2808c2ecf20Sopenharmony_ci		set_param_l(&in_param, cqn);
2818c2ecf20Sopenharmony_ci		err = mlx4_cmd(dev, in_param, RES_CQ, RES_OP_RESERVE_AND_MAP,
2828c2ecf20Sopenharmony_ci			       MLX4_CMD_FREE_RES,
2838c2ecf20Sopenharmony_ci			       MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
2848c2ecf20Sopenharmony_ci		if (err)
2858c2ecf20Sopenharmony_ci			mlx4_warn(dev, "Failed freeing cq:%d\n", cqn);
2868c2ecf20Sopenharmony_ci	} else
2878c2ecf20Sopenharmony_ci		__mlx4_cq_free_icm(dev, cqn);
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic int mlx4_init_user_cqes(void *buf, int entries, int cqe_size)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	int entries_per_copy = PAGE_SIZE / cqe_size;
2938c2ecf20Sopenharmony_ci	void *init_ents;
2948c2ecf20Sopenharmony_ci	int err = 0;
2958c2ecf20Sopenharmony_ci	int i;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	init_ents = kmalloc(PAGE_SIZE, GFP_KERNEL);
2988c2ecf20Sopenharmony_ci	if (!init_ents)
2998c2ecf20Sopenharmony_ci		return -ENOMEM;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/* Populate a list of CQ entries to reduce the number of
3028c2ecf20Sopenharmony_ci	 * copy_to_user calls. 0xcc is the initialization value
3038c2ecf20Sopenharmony_ci	 * required by the FW.
3048c2ecf20Sopenharmony_ci	 */
3058c2ecf20Sopenharmony_ci	memset(init_ents, 0xcc, PAGE_SIZE);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if (entries_per_copy < entries) {
3088c2ecf20Sopenharmony_ci		for (i = 0; i < entries / entries_per_copy; i++) {
3098c2ecf20Sopenharmony_ci			err = copy_to_user((void __user *)buf, init_ents, PAGE_SIZE) ?
3108c2ecf20Sopenharmony_ci				-EFAULT : 0;
3118c2ecf20Sopenharmony_ci			if (err)
3128c2ecf20Sopenharmony_ci				goto out;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci			buf += PAGE_SIZE;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci	} else {
3178c2ecf20Sopenharmony_ci		err = copy_to_user((void __user *)buf, init_ents, entries * cqe_size) ?
3188c2ecf20Sopenharmony_ci			-EFAULT : 0;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ciout:
3228c2ecf20Sopenharmony_ci	kfree(init_ents);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return err;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic void mlx4_init_kernel_cqes(struct mlx4_buf *buf,
3288c2ecf20Sopenharmony_ci				  int entries,
3298c2ecf20Sopenharmony_ci				  int cqe_size)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	int i;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (buf->nbufs == 1)
3348c2ecf20Sopenharmony_ci		memset(buf->direct.buf, 0xcc, entries * cqe_size);
3358c2ecf20Sopenharmony_ci	else
3368c2ecf20Sopenharmony_ci		for (i = 0; i < buf->npages; i++)
3378c2ecf20Sopenharmony_ci			memset(buf->page_list[i].buf, 0xcc,
3388c2ecf20Sopenharmony_ci			       1UL << buf->page_shift);
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ciint mlx4_cq_alloc(struct mlx4_dev *dev, int nent,
3428c2ecf20Sopenharmony_ci		  struct mlx4_mtt *mtt, struct mlx4_uar *uar, u64 db_rec,
3438c2ecf20Sopenharmony_ci		  struct mlx4_cq *cq, unsigned vector, int collapsed,
3448c2ecf20Sopenharmony_ci		  int timestamp_en, void *buf_addr, bool user_cq)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	bool sw_cq_init = dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SW_CQ_INIT;
3478c2ecf20Sopenharmony_ci	struct mlx4_priv *priv = mlx4_priv(dev);
3488c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &priv->cq_table;
3498c2ecf20Sopenharmony_ci	struct mlx4_cmd_mailbox *mailbox;
3508c2ecf20Sopenharmony_ci	struct mlx4_cq_context *cq_context;
3518c2ecf20Sopenharmony_ci	u64 mtt_addr;
3528c2ecf20Sopenharmony_ci	int err;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (vector >= dev->caps.num_comp_vectors)
3558c2ecf20Sopenharmony_ci		return -EINVAL;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	cq->vector = vector;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	err = mlx4_cq_alloc_icm(dev, &cq->cqn, cq->usage);
3608c2ecf20Sopenharmony_ci	if (err)
3618c2ecf20Sopenharmony_ci		return err;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	spin_lock(&cq_table->lock);
3648c2ecf20Sopenharmony_ci	err = radix_tree_insert(&cq_table->tree, cq->cqn, cq);
3658c2ecf20Sopenharmony_ci	spin_unlock(&cq_table->lock);
3668c2ecf20Sopenharmony_ci	if (err)
3678c2ecf20Sopenharmony_ci		goto err_icm;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	mailbox = mlx4_alloc_cmd_mailbox(dev);
3708c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox)) {
3718c2ecf20Sopenharmony_ci		err = PTR_ERR(mailbox);
3728c2ecf20Sopenharmony_ci		goto err_radix;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	cq_context = mailbox->buf;
3768c2ecf20Sopenharmony_ci	cq_context->flags	    = cpu_to_be32(!!collapsed << 18);
3778c2ecf20Sopenharmony_ci	if (timestamp_en)
3788c2ecf20Sopenharmony_ci		cq_context->flags  |= cpu_to_be32(1 << 19);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	cq_context->logsize_usrpage =
3818c2ecf20Sopenharmony_ci		cpu_to_be32((ilog2(nent) << 24) |
3828c2ecf20Sopenharmony_ci			    mlx4_to_hw_uar_index(dev, uar->index));
3838c2ecf20Sopenharmony_ci	cq_context->comp_eqn	    = priv->eq_table.eq[MLX4_CQ_TO_EQ_VECTOR(vector)].eqn;
3848c2ecf20Sopenharmony_ci	cq_context->log_page_size   = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	mtt_addr = mlx4_mtt_addr(dev, mtt);
3878c2ecf20Sopenharmony_ci	cq_context->mtt_base_addr_h = mtt_addr >> 32;
3888c2ecf20Sopenharmony_ci	cq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
3898c2ecf20Sopenharmony_ci	cq_context->db_rec_addr     = cpu_to_be64(db_rec);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (sw_cq_init) {
3928c2ecf20Sopenharmony_ci		if (user_cq) {
3938c2ecf20Sopenharmony_ci			err = mlx4_init_user_cqes(buf_addr, nent,
3948c2ecf20Sopenharmony_ci						  dev->caps.cqe_size);
3958c2ecf20Sopenharmony_ci			if (err)
3968c2ecf20Sopenharmony_ci				sw_cq_init = false;
3978c2ecf20Sopenharmony_ci		} else {
3988c2ecf20Sopenharmony_ci			mlx4_init_kernel_cqes(buf_addr, nent,
3998c2ecf20Sopenharmony_ci					      dev->caps.cqe_size);
4008c2ecf20Sopenharmony_ci		}
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	err = mlx4_SW2HW_CQ(dev, mailbox, cq->cqn, sw_cq_init);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	mlx4_free_cmd_mailbox(dev, mailbox);
4068c2ecf20Sopenharmony_ci	if (err)
4078c2ecf20Sopenharmony_ci		goto err_radix;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	cq->cons_index = 0;
4108c2ecf20Sopenharmony_ci	cq->arm_sn     = 1;
4118c2ecf20Sopenharmony_ci	cq->uar        = uar;
4128c2ecf20Sopenharmony_ci	refcount_set(&cq->refcount, 1);
4138c2ecf20Sopenharmony_ci	init_completion(&cq->free);
4148c2ecf20Sopenharmony_ci	cq->comp = mlx4_add_cq_to_tasklet;
4158c2ecf20Sopenharmony_ci	cq->tasklet_ctx.priv =
4168c2ecf20Sopenharmony_ci		&priv->eq_table.eq[MLX4_CQ_TO_EQ_VECTOR(vector)].tasklet_ctx;
4178c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cq->tasklet_ctx.list);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	cq->irq = priv->eq_table.eq[MLX4_CQ_TO_EQ_VECTOR(vector)].irq;
4218c2ecf20Sopenharmony_ci	return 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cierr_radix:
4248c2ecf20Sopenharmony_ci	spin_lock(&cq_table->lock);
4258c2ecf20Sopenharmony_ci	radix_tree_delete(&cq_table->tree, cq->cqn);
4268c2ecf20Sopenharmony_ci	spin_unlock(&cq_table->lock);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cierr_icm:
4298c2ecf20Sopenharmony_ci	mlx4_cq_free_icm(dev, cq->cqn);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	return err;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mlx4_cq_alloc);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_civoid mlx4_cq_free(struct mlx4_dev *dev, struct mlx4_cq *cq)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct mlx4_priv *priv = mlx4_priv(dev);
4388c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &priv->cq_table;
4398c2ecf20Sopenharmony_ci	int err;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	err = mlx4_HW2SW_CQ(dev, NULL, cq->cqn);
4428c2ecf20Sopenharmony_ci	if (err)
4438c2ecf20Sopenharmony_ci		mlx4_warn(dev, "HW2SW_CQ failed (%d) for CQN %06x\n", err, cq->cqn);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	spin_lock(&cq_table->lock);
4468c2ecf20Sopenharmony_ci	radix_tree_delete(&cq_table->tree, cq->cqn);
4478c2ecf20Sopenharmony_ci	spin_unlock(&cq_table->lock);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	synchronize_irq(priv->eq_table.eq[MLX4_CQ_TO_EQ_VECTOR(cq->vector)].irq);
4508c2ecf20Sopenharmony_ci	if (priv->eq_table.eq[MLX4_CQ_TO_EQ_VECTOR(cq->vector)].irq !=
4518c2ecf20Sopenharmony_ci	    priv->eq_table.eq[MLX4_EQ_ASYNC].irq)
4528c2ecf20Sopenharmony_ci		synchronize_irq(priv->eq_table.eq[MLX4_EQ_ASYNC].irq);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&cq->refcount))
4558c2ecf20Sopenharmony_ci		complete(&cq->free);
4568c2ecf20Sopenharmony_ci	wait_for_completion(&cq->free);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	mlx4_cq_free_icm(dev, cq->cqn);
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mlx4_cq_free);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ciint mlx4_init_cq_table(struct mlx4_dev *dev)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct mlx4_cq_table *cq_table = &mlx4_priv(dev)->cq_table;
4658c2ecf20Sopenharmony_ci	int err;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	spin_lock_init(&cq_table->lock);
4688c2ecf20Sopenharmony_ci	INIT_RADIX_TREE(&cq_table->tree, GFP_ATOMIC);
4698c2ecf20Sopenharmony_ci	if (mlx4_is_slave(dev))
4708c2ecf20Sopenharmony_ci		return 0;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	err = mlx4_bitmap_init(&cq_table->bitmap, dev->caps.num_cqs,
4738c2ecf20Sopenharmony_ci			       dev->caps.num_cqs - 1, dev->caps.reserved_cqs, 0);
4748c2ecf20Sopenharmony_ci	if (err)
4758c2ecf20Sopenharmony_ci		return err;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return 0;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_civoid mlx4_cleanup_cq_table(struct mlx4_dev *dev)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	if (mlx4_is_slave(dev))
4838c2ecf20Sopenharmony_ci		return;
4848c2ecf20Sopenharmony_ci	/* Nothing to do to clean up radix_tree */
4858c2ecf20Sopenharmony_ci	mlx4_bitmap_cleanup(&mlx4_priv(dev)->cq_table.bitmap);
4868c2ecf20Sopenharmony_ci}
487