18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
68c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
98c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
128c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
138c2ecf20Sopenharmony_ci *     conditions are met:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
168c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
178c2ecf20Sopenharmony_ci *        disclaimer.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
208c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
218c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
228c2ecf20Sopenharmony_ci *        provided with the distribution.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318c2ecf20Sopenharmony_ci * SOFTWARE.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <linux/mlx4/cq.h>
358c2ecf20Sopenharmony_ci#include <linux/mlx4/qp.h>
368c2ecf20Sopenharmony_ci#include <linux/mlx4/srq.h>
378c2ecf20Sopenharmony_ci#include <linux/slab.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include "mlx4_ib.h"
408c2ecf20Sopenharmony_ci#include <rdma/mlx4-abi.h>
418c2ecf20Sopenharmony_ci#include <rdma/uverbs_ioctl.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void mlx4_ib_cq_comp(struct mlx4_cq *cq)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
468c2ecf20Sopenharmony_ci	ibcq->comp_handler(ibcq, ibcq->cq_context);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic void mlx4_ib_cq_event(struct mlx4_cq *cq, enum mlx4_event type)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct ib_event event;
528c2ecf20Sopenharmony_ci	struct ib_cq *ibcq;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (type != MLX4_EVENT_TYPE_CQ_ERROR) {
558c2ecf20Sopenharmony_ci		pr_warn("Unexpected event type %d "
568c2ecf20Sopenharmony_ci		       "on CQ %06x\n", type, cq->cqn);
578c2ecf20Sopenharmony_ci		return;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ibcq = &to_mibcq(cq)->ibcq;
618c2ecf20Sopenharmony_ci	if (ibcq->event_handler) {
628c2ecf20Sopenharmony_ci		event.device     = ibcq->device;
638c2ecf20Sopenharmony_ci		event.event      = IB_EVENT_CQ_ERR;
648c2ecf20Sopenharmony_ci		event.element.cq = ibcq;
658c2ecf20Sopenharmony_ci		ibcq->event_handler(&event, ibcq->cq_context);
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic void *get_cqe_from_buf(struct mlx4_ib_cq_buf *buf, int n)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	return mlx4_buf_offset(&buf->buf, n * buf->entry_size);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void *get_cqe(struct mlx4_ib_cq *cq, int n)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	return get_cqe_from_buf(&cq->buf, n);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void *get_sw_cqe(struct mlx4_ib_cq *cq, int n)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibcq.cqe);
828c2ecf20Sopenharmony_ci	struct mlx4_cqe *tcqe = ((cq->buf.entry_size == 64) ? (cqe + 1) : cqe);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return (!!(tcqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
858c2ecf20Sopenharmony_ci		!!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct mlx4_cqe *next_cqe_sw(struct mlx4_ib_cq *cq)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	return get_sw_cqe(cq, cq->mcq.cons_index);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciint mlx4_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct mlx4_ib_cq *mcq = to_mcq(cq);
968c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *dev = to_mdev(cq->device);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return mlx4_cq_modify(dev->dev, &mcq->mcq, cq_count, cq_period);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int mlx4_ib_alloc_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int nent)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	int err;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	err = mlx4_buf_alloc(dev->dev, nent * dev->dev->caps.cqe_size,
1068c2ecf20Sopenharmony_ci			     PAGE_SIZE * 2, &buf->buf);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (err)
1098c2ecf20Sopenharmony_ci		goto out;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	buf->entry_size = dev->dev->caps.cqe_size;
1128c2ecf20Sopenharmony_ci	err = mlx4_mtt_init(dev->dev, buf->buf.npages, buf->buf.page_shift,
1138c2ecf20Sopenharmony_ci				    &buf->mtt);
1148c2ecf20Sopenharmony_ci	if (err)
1158c2ecf20Sopenharmony_ci		goto err_buf;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	err = mlx4_buf_write_mtt(dev->dev, &buf->mtt, &buf->buf);
1188c2ecf20Sopenharmony_ci	if (err)
1198c2ecf20Sopenharmony_ci		goto err_mtt;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cierr_mtt:
1248c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &buf->mtt);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cierr_buf:
1278c2ecf20Sopenharmony_ci	mlx4_buf_free(dev->dev, nent * buf->entry_size, &buf->buf);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciout:
1308c2ecf20Sopenharmony_ci	return err;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void mlx4_ib_free_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int cqe)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	mlx4_buf_free(dev->dev, (cqe + 1) * buf->entry_size, &buf->buf);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int mlx4_ib_get_cq_umem(struct mlx4_ib_dev *dev, struct ib_udata *udata,
1398c2ecf20Sopenharmony_ci			       struct mlx4_ib_cq_buf *buf,
1408c2ecf20Sopenharmony_ci			       struct ib_umem **umem, u64 buf_addr, int cqe)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	int err;
1438c2ecf20Sopenharmony_ci	int cqe_size = dev->dev->caps.cqe_size;
1448c2ecf20Sopenharmony_ci	int shift;
1458c2ecf20Sopenharmony_ci	int n;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	*umem = ib_umem_get(&dev->ib_dev, buf_addr, cqe * cqe_size,
1488c2ecf20Sopenharmony_ci			    IB_ACCESS_LOCAL_WRITE);
1498c2ecf20Sopenharmony_ci	if (IS_ERR(*umem))
1508c2ecf20Sopenharmony_ci		return PTR_ERR(*umem);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	shift = mlx4_ib_umem_calc_optimal_mtt_size(*umem, 0, &n);
1538c2ecf20Sopenharmony_ci	err = mlx4_mtt_init(dev->dev, n, shift, &buf->mtt);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (err)
1568c2ecf20Sopenharmony_ci		goto err_buf;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	err = mlx4_ib_umem_write_mtt(dev, &buf->mtt, *umem);
1598c2ecf20Sopenharmony_ci	if (err)
1608c2ecf20Sopenharmony_ci		goto err_mtt;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cierr_mtt:
1658c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &buf->mtt);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cierr_buf:
1688c2ecf20Sopenharmony_ci	ib_umem_release(*umem);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return err;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define CQ_CREATE_FLAGS_SUPPORTED IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION
1748c2ecf20Sopenharmony_ciint mlx4_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
1758c2ecf20Sopenharmony_ci		      struct ib_udata *udata)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct ib_device *ibdev = ibcq->device;
1788c2ecf20Sopenharmony_ci	int entries = attr->cqe;
1798c2ecf20Sopenharmony_ci	int vector = attr->comp_vector;
1808c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *dev = to_mdev(ibdev);
1818c2ecf20Sopenharmony_ci	struct mlx4_ib_cq *cq = to_mcq(ibcq);
1828c2ecf20Sopenharmony_ci	struct mlx4_uar *uar;
1838c2ecf20Sopenharmony_ci	void *buf_addr;
1848c2ecf20Sopenharmony_ci	int err;
1858c2ecf20Sopenharmony_ci	struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
1868c2ecf20Sopenharmony_ci		udata, struct mlx4_ib_ucontext, ibucontext);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (entries < 1 || entries > dev->dev->caps.max_cqes)
1898c2ecf20Sopenharmony_ci		return -EINVAL;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (attr->flags & ~CQ_CREATE_FLAGS_SUPPORTED)
1928c2ecf20Sopenharmony_ci		return -EINVAL;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	entries      = roundup_pow_of_two(entries + 1);
1958c2ecf20Sopenharmony_ci	cq->ibcq.cqe = entries - 1;
1968c2ecf20Sopenharmony_ci	mutex_init(&cq->resize_mutex);
1978c2ecf20Sopenharmony_ci	spin_lock_init(&cq->lock);
1988c2ecf20Sopenharmony_ci	cq->resize_buf = NULL;
1998c2ecf20Sopenharmony_ci	cq->resize_umem = NULL;
2008c2ecf20Sopenharmony_ci	cq->create_flags = attr->flags;
2018c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cq->send_qp_list);
2028c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cq->recv_qp_list);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (udata) {
2058c2ecf20Sopenharmony_ci		struct mlx4_ib_create_cq ucmd;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
2088c2ecf20Sopenharmony_ci			err = -EFAULT;
2098c2ecf20Sopenharmony_ci			goto err_cq;
2108c2ecf20Sopenharmony_ci		}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		buf_addr = (void *)(unsigned long)ucmd.buf_addr;
2138c2ecf20Sopenharmony_ci		err = mlx4_ib_get_cq_umem(dev, udata, &cq->buf, &cq->umem,
2148c2ecf20Sopenharmony_ci					  ucmd.buf_addr, entries);
2158c2ecf20Sopenharmony_ci		if (err)
2168c2ecf20Sopenharmony_ci			goto err_cq;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &cq->db);
2198c2ecf20Sopenharmony_ci		if (err)
2208c2ecf20Sopenharmony_ci			goto err_mtt;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		uar = &context->uar;
2238c2ecf20Sopenharmony_ci		cq->mcq.usage = MLX4_RES_USAGE_USER_VERBS;
2248c2ecf20Sopenharmony_ci	} else {
2258c2ecf20Sopenharmony_ci		err = mlx4_db_alloc(dev->dev, &cq->db, 1);
2268c2ecf20Sopenharmony_ci		if (err)
2278c2ecf20Sopenharmony_ci			goto err_cq;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci		cq->mcq.set_ci_db  = cq->db.db;
2308c2ecf20Sopenharmony_ci		cq->mcq.arm_db     = cq->db.db + 1;
2318c2ecf20Sopenharmony_ci		*cq->mcq.set_ci_db = 0;
2328c2ecf20Sopenharmony_ci		*cq->mcq.arm_db    = 0;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		err = mlx4_ib_alloc_cq_buf(dev, &cq->buf, entries);
2358c2ecf20Sopenharmony_ci		if (err)
2368c2ecf20Sopenharmony_ci			goto err_db;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		buf_addr = &cq->buf.buf;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		uar = &dev->priv_uar;
2418c2ecf20Sopenharmony_ci		cq->mcq.usage = MLX4_RES_USAGE_DRIVER;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (dev->eq_table)
2458c2ecf20Sopenharmony_ci		vector = dev->eq_table[vector % ibdev->num_comp_vectors];
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar, cq->db.dma,
2488c2ecf20Sopenharmony_ci			    &cq->mcq, vector, 0,
2498c2ecf20Sopenharmony_ci			    !!(cq->create_flags &
2508c2ecf20Sopenharmony_ci			       IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION),
2518c2ecf20Sopenharmony_ci			    buf_addr, !!udata);
2528c2ecf20Sopenharmony_ci	if (err)
2538c2ecf20Sopenharmony_ci		goto err_dbmap;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	if (udata)
2568c2ecf20Sopenharmony_ci		cq->mcq.tasklet_ctx.comp = mlx4_ib_cq_comp;
2578c2ecf20Sopenharmony_ci	else
2588c2ecf20Sopenharmony_ci		cq->mcq.comp = mlx4_ib_cq_comp;
2598c2ecf20Sopenharmony_ci	cq->mcq.event = mlx4_ib_cq_event;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (udata)
2628c2ecf20Sopenharmony_ci		if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
2638c2ecf20Sopenharmony_ci			err = -EFAULT;
2648c2ecf20Sopenharmony_ci			goto err_cq_free;
2658c2ecf20Sopenharmony_ci		}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cierr_cq_free:
2708c2ecf20Sopenharmony_ci	mlx4_cq_free(dev->dev, &cq->mcq);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cierr_dbmap:
2738c2ecf20Sopenharmony_ci	if (udata)
2748c2ecf20Sopenharmony_ci		mlx4_ib_db_unmap_user(context, &cq->db);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cierr_mtt:
2778c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	ib_umem_release(cq->umem);
2808c2ecf20Sopenharmony_ci	if (!udata)
2818c2ecf20Sopenharmony_ci		mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cierr_db:
2848c2ecf20Sopenharmony_ci	if (!udata)
2858c2ecf20Sopenharmony_ci		mlx4_db_free(dev->dev, &cq->db);
2868c2ecf20Sopenharmony_cierr_cq:
2878c2ecf20Sopenharmony_ci	return err;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic int mlx4_alloc_resize_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
2918c2ecf20Sopenharmony_ci				  int entries)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	int err;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (cq->resize_buf)
2968c2ecf20Sopenharmony_ci		return -EBUSY;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
2998c2ecf20Sopenharmony_ci	if (!cq->resize_buf)
3008c2ecf20Sopenharmony_ci		return -ENOMEM;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	err = mlx4_ib_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
3038c2ecf20Sopenharmony_ci	if (err) {
3048c2ecf20Sopenharmony_ci		kfree(cq->resize_buf);
3058c2ecf20Sopenharmony_ci		cq->resize_buf = NULL;
3068c2ecf20Sopenharmony_ci		return err;
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	cq->resize_buf->cqe = entries - 1;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	return 0;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int mlx4_alloc_resize_umem(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
3158c2ecf20Sopenharmony_ci				   int entries, struct ib_udata *udata)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct mlx4_ib_resize_cq ucmd;
3188c2ecf20Sopenharmony_ci	int err;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (cq->resize_umem)
3218c2ecf20Sopenharmony_ci		return -EBUSY;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
3248c2ecf20Sopenharmony_ci		return -EFAULT;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
3278c2ecf20Sopenharmony_ci	if (!cq->resize_buf)
3288c2ecf20Sopenharmony_ci		return -ENOMEM;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	err = mlx4_ib_get_cq_umem(dev, udata, &cq->resize_buf->buf,
3318c2ecf20Sopenharmony_ci				  &cq->resize_umem, ucmd.buf_addr, entries);
3328c2ecf20Sopenharmony_ci	if (err) {
3338c2ecf20Sopenharmony_ci		kfree(cq->resize_buf);
3348c2ecf20Sopenharmony_ci		cq->resize_buf = NULL;
3358c2ecf20Sopenharmony_ci		return err;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	cq->resize_buf->cqe = entries - 1;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 0;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic int mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq *cq)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	u32 i;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	i = cq->mcq.cons_index;
3488c2ecf20Sopenharmony_ci	while (get_sw_cqe(cq, i))
3498c2ecf20Sopenharmony_ci		++i;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return i - cq->mcq.cons_index;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq *cq)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	struct mlx4_cqe *cqe, *new_cqe;
3578c2ecf20Sopenharmony_ci	int i;
3588c2ecf20Sopenharmony_ci	int cqe_size = cq->buf.entry_size;
3598c2ecf20Sopenharmony_ci	int cqe_inc = cqe_size == 64 ? 1 : 0;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	i = cq->mcq.cons_index;
3628c2ecf20Sopenharmony_ci	cqe = get_cqe(cq, i & cq->ibcq.cqe);
3638c2ecf20Sopenharmony_ci	cqe += cqe_inc;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
3668c2ecf20Sopenharmony_ci		new_cqe = get_cqe_from_buf(&cq->resize_buf->buf,
3678c2ecf20Sopenharmony_ci					   (i + 1) & cq->resize_buf->cqe);
3688c2ecf20Sopenharmony_ci		memcpy(new_cqe, get_cqe(cq, i & cq->ibcq.cqe), cqe_size);
3698c2ecf20Sopenharmony_ci		new_cqe += cqe_inc;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		new_cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
3728c2ecf20Sopenharmony_ci			(((i + 1) & (cq->resize_buf->cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
3738c2ecf20Sopenharmony_ci		cqe = get_cqe(cq, ++i & cq->ibcq.cqe);
3748c2ecf20Sopenharmony_ci		cqe += cqe_inc;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci	++cq->mcq.cons_index;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ciint mlx4_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *dev = to_mdev(ibcq->device);
3828c2ecf20Sopenharmony_ci	struct mlx4_ib_cq *cq = to_mcq(ibcq);
3838c2ecf20Sopenharmony_ci	struct mlx4_mtt mtt;
3848c2ecf20Sopenharmony_ci	int outst_cqe;
3858c2ecf20Sopenharmony_ci	int err;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	mutex_lock(&cq->resize_mutex);
3888c2ecf20Sopenharmony_ci	if (entries < 1 || entries > dev->dev->caps.max_cqes) {
3898c2ecf20Sopenharmony_ci		err = -EINVAL;
3908c2ecf20Sopenharmony_ci		goto out;
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	entries = roundup_pow_of_two(entries + 1);
3948c2ecf20Sopenharmony_ci	if (entries == ibcq->cqe + 1) {
3958c2ecf20Sopenharmony_ci		err = 0;
3968c2ecf20Sopenharmony_ci		goto out;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (entries > dev->dev->caps.max_cqes + 1) {
4008c2ecf20Sopenharmony_ci		err = -EINVAL;
4018c2ecf20Sopenharmony_ci		goto out;
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (ibcq->uobject) {
4058c2ecf20Sopenharmony_ci		err = mlx4_alloc_resize_umem(dev, cq, entries, udata);
4068c2ecf20Sopenharmony_ci		if (err)
4078c2ecf20Sopenharmony_ci			goto out;
4088c2ecf20Sopenharmony_ci	} else {
4098c2ecf20Sopenharmony_ci		/* Can't be smaller than the number of outstanding CQEs */
4108c2ecf20Sopenharmony_ci		outst_cqe = mlx4_ib_get_outstanding_cqes(cq);
4118c2ecf20Sopenharmony_ci		if (entries < outst_cqe + 1) {
4128c2ecf20Sopenharmony_ci			err = -EINVAL;
4138c2ecf20Sopenharmony_ci			goto out;
4148c2ecf20Sopenharmony_ci		}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		err = mlx4_alloc_resize_buf(dev, cq, entries);
4178c2ecf20Sopenharmony_ci		if (err)
4188c2ecf20Sopenharmony_ci			goto out;
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	mtt = cq->buf.mtt;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	err = mlx4_cq_resize(dev->dev, &cq->mcq, entries, &cq->resize_buf->buf.mtt);
4248c2ecf20Sopenharmony_ci	if (err)
4258c2ecf20Sopenharmony_ci		goto err_buf;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &mtt);
4288c2ecf20Sopenharmony_ci	if (ibcq->uobject) {
4298c2ecf20Sopenharmony_ci		cq->buf      = cq->resize_buf->buf;
4308c2ecf20Sopenharmony_ci		cq->ibcq.cqe = cq->resize_buf->cqe;
4318c2ecf20Sopenharmony_ci		ib_umem_release(cq->umem);
4328c2ecf20Sopenharmony_ci		cq->umem     = cq->resize_umem;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		kfree(cq->resize_buf);
4358c2ecf20Sopenharmony_ci		cq->resize_buf = NULL;
4368c2ecf20Sopenharmony_ci		cq->resize_umem = NULL;
4378c2ecf20Sopenharmony_ci	} else {
4388c2ecf20Sopenharmony_ci		struct mlx4_ib_cq_buf tmp_buf;
4398c2ecf20Sopenharmony_ci		int tmp_cqe = 0;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		spin_lock_irq(&cq->lock);
4428c2ecf20Sopenharmony_ci		if (cq->resize_buf) {
4438c2ecf20Sopenharmony_ci			mlx4_ib_cq_resize_copy_cqes(cq);
4448c2ecf20Sopenharmony_ci			tmp_buf = cq->buf;
4458c2ecf20Sopenharmony_ci			tmp_cqe = cq->ibcq.cqe;
4468c2ecf20Sopenharmony_ci			cq->buf      = cq->resize_buf->buf;
4478c2ecf20Sopenharmony_ci			cq->ibcq.cqe = cq->resize_buf->cqe;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci			kfree(cq->resize_buf);
4508c2ecf20Sopenharmony_ci			cq->resize_buf = NULL;
4518c2ecf20Sopenharmony_ci		}
4528c2ecf20Sopenharmony_ci		spin_unlock_irq(&cq->lock);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		if (tmp_cqe)
4558c2ecf20Sopenharmony_ci			mlx4_ib_free_cq_buf(dev, &tmp_buf, tmp_cqe);
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	goto out;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cierr_buf:
4618c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &cq->resize_buf->buf.mtt);
4628c2ecf20Sopenharmony_ci	if (!ibcq->uobject)
4638c2ecf20Sopenharmony_ci		mlx4_ib_free_cq_buf(dev, &cq->resize_buf->buf,
4648c2ecf20Sopenharmony_ci				    cq->resize_buf->cqe);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	kfree(cq->resize_buf);
4678c2ecf20Sopenharmony_ci	cq->resize_buf = NULL;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	ib_umem_release(cq->resize_umem);
4708c2ecf20Sopenharmony_ci	cq->resize_umem = NULL;
4718c2ecf20Sopenharmony_ciout:
4728c2ecf20Sopenharmony_ci	mutex_unlock(&cq->resize_mutex);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	return err;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ciint mlx4_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *dev = to_mdev(cq->device);
4808c2ecf20Sopenharmony_ci	struct mlx4_ib_cq *mcq = to_mcq(cq);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	mlx4_cq_free(dev->dev, &mcq->mcq);
4838c2ecf20Sopenharmony_ci	mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	if (udata) {
4868c2ecf20Sopenharmony_ci		mlx4_ib_db_unmap_user(
4878c2ecf20Sopenharmony_ci			rdma_udata_to_drv_context(
4888c2ecf20Sopenharmony_ci				udata,
4898c2ecf20Sopenharmony_ci				struct mlx4_ib_ucontext,
4908c2ecf20Sopenharmony_ci				ibucontext),
4918c2ecf20Sopenharmony_ci			&mcq->db);
4928c2ecf20Sopenharmony_ci	} else {
4938c2ecf20Sopenharmony_ci		mlx4_ib_free_cq_buf(dev, &mcq->buf, cq->cqe);
4948c2ecf20Sopenharmony_ci		mlx4_db_free(dev->dev, &mcq->db);
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci	ib_umem_release(mcq->umem);
4978c2ecf20Sopenharmony_ci	return 0;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic void dump_cqe(void *cqe)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	__be32 *buf = cqe;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	pr_debug("CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
5058c2ecf20Sopenharmony_ci	       be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
5068c2ecf20Sopenharmony_ci	       be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
5078c2ecf20Sopenharmony_ci	       be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
5118c2ecf20Sopenharmony_ci				     struct ib_wc *wc)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
5148c2ecf20Sopenharmony_ci		pr_debug("local QP operation err "
5158c2ecf20Sopenharmony_ci		       "(QPN %06x, WQE index %x, vendor syndrome %02x, "
5168c2ecf20Sopenharmony_ci		       "opcode = %02x)\n",
5178c2ecf20Sopenharmony_ci		       be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
5188c2ecf20Sopenharmony_ci		       cqe->vendor_err_syndrome,
5198c2ecf20Sopenharmony_ci		       cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
5208c2ecf20Sopenharmony_ci		dump_cqe(cqe);
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	switch (cqe->syndrome) {
5248c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
5258c2ecf20Sopenharmony_ci		wc->status = IB_WC_LOC_LEN_ERR;
5268c2ecf20Sopenharmony_ci		break;
5278c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
5288c2ecf20Sopenharmony_ci		wc->status = IB_WC_LOC_QP_OP_ERR;
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
5318c2ecf20Sopenharmony_ci		wc->status = IB_WC_LOC_PROT_ERR;
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
5348c2ecf20Sopenharmony_ci		wc->status = IB_WC_WR_FLUSH_ERR;
5358c2ecf20Sopenharmony_ci		break;
5368c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_MW_BIND_ERR:
5378c2ecf20Sopenharmony_ci		wc->status = IB_WC_MW_BIND_ERR;
5388c2ecf20Sopenharmony_ci		break;
5398c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
5408c2ecf20Sopenharmony_ci		wc->status = IB_WC_BAD_RESP_ERR;
5418c2ecf20Sopenharmony_ci		break;
5428c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
5438c2ecf20Sopenharmony_ci		wc->status = IB_WC_LOC_ACCESS_ERR;
5448c2ecf20Sopenharmony_ci		break;
5458c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
5468c2ecf20Sopenharmony_ci		wc->status = IB_WC_REM_INV_REQ_ERR;
5478c2ecf20Sopenharmony_ci		break;
5488c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
5498c2ecf20Sopenharmony_ci		wc->status = IB_WC_REM_ACCESS_ERR;
5508c2ecf20Sopenharmony_ci		break;
5518c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
5528c2ecf20Sopenharmony_ci		wc->status = IB_WC_REM_OP_ERR;
5538c2ecf20Sopenharmony_ci		break;
5548c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
5558c2ecf20Sopenharmony_ci		wc->status = IB_WC_RETRY_EXC_ERR;
5568c2ecf20Sopenharmony_ci		break;
5578c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
5588c2ecf20Sopenharmony_ci		wc->status = IB_WC_RNR_RETRY_EXC_ERR;
5598c2ecf20Sopenharmony_ci		break;
5608c2ecf20Sopenharmony_ci	case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
5618c2ecf20Sopenharmony_ci		wc->status = IB_WC_REM_ABORT_ERR;
5628c2ecf20Sopenharmony_ci		break;
5638c2ecf20Sopenharmony_ci	default:
5648c2ecf20Sopenharmony_ci		wc->status = IB_WC_GENERAL_ERR;
5658c2ecf20Sopenharmony_ci		break;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	wc->vendor_err = cqe->vendor_err_syndrome;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cistatic int mlx4_ib_ipoib_csum_ok(__be16 status, u8 badfcs_enc, __be16 checksum)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	return ((badfcs_enc & MLX4_CQE_STATUS_L4_CSUM) ||
5748c2ecf20Sopenharmony_ci		((status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
5758c2ecf20Sopenharmony_ci		 (status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
5768c2ecf20Sopenharmony_ci				       MLX4_CQE_STATUS_UDP)) &&
5778c2ecf20Sopenharmony_ci		 (checksum == cpu_to_be16(0xffff))));
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic void use_tunnel_data(struct mlx4_ib_qp *qp, struct mlx4_ib_cq *cq, struct ib_wc *wc,
5818c2ecf20Sopenharmony_ci			    unsigned tail, struct mlx4_cqe *cqe, int is_eth)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	struct mlx4_ib_proxy_sqp_hdr *hdr;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	ib_dma_sync_single_for_cpu(qp->ibqp.device,
5868c2ecf20Sopenharmony_ci				   qp->sqp_proxy_rcv[tail].map,
5878c2ecf20Sopenharmony_ci				   sizeof (struct mlx4_ib_proxy_sqp_hdr),
5888c2ecf20Sopenharmony_ci				   DMA_FROM_DEVICE);
5898c2ecf20Sopenharmony_ci	hdr = (struct mlx4_ib_proxy_sqp_hdr *) (qp->sqp_proxy_rcv[tail].addr);
5908c2ecf20Sopenharmony_ci	wc->pkey_index	= be16_to_cpu(hdr->tun.pkey_index);
5918c2ecf20Sopenharmony_ci	wc->src_qp	= be32_to_cpu(hdr->tun.flags_src_qp) & 0xFFFFFF;
5928c2ecf20Sopenharmony_ci	wc->wc_flags   |= (hdr->tun.g_ml_path & 0x80) ? (IB_WC_GRH) : 0;
5938c2ecf20Sopenharmony_ci	wc->dlid_path_bits = 0;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	if (is_eth) {
5968c2ecf20Sopenharmony_ci		wc->slid = 0;
5978c2ecf20Sopenharmony_ci		wc->vlan_id = be16_to_cpu(hdr->tun.sl_vid);
5988c2ecf20Sopenharmony_ci		memcpy(&(wc->smac[0]), (char *)&hdr->tun.mac_31_0, 4);
5998c2ecf20Sopenharmony_ci		memcpy(&(wc->smac[4]), (char *)&hdr->tun.slid_mac_47_32, 2);
6008c2ecf20Sopenharmony_ci		wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
6018c2ecf20Sopenharmony_ci	} else {
6028c2ecf20Sopenharmony_ci		wc->slid        = be16_to_cpu(hdr->tun.slid_mac_47_32);
6038c2ecf20Sopenharmony_ci		wc->sl          = (u8) (be16_to_cpu(hdr->tun.sl_vid) >> 12);
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic void mlx4_ib_qp_sw_comp(struct mlx4_ib_qp *qp, int num_entries,
6088c2ecf20Sopenharmony_ci			       struct ib_wc *wc, int *npolled, int is_send)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	struct mlx4_ib_wq *wq;
6118c2ecf20Sopenharmony_ci	unsigned cur;
6128c2ecf20Sopenharmony_ci	int i;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	wq = is_send ? &qp->sq : &qp->rq;
6158c2ecf20Sopenharmony_ci	cur = wq->head - wq->tail;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	if (cur == 0)
6188c2ecf20Sopenharmony_ci		return;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	for (i = 0;  i < cur && *npolled < num_entries; i++) {
6218c2ecf20Sopenharmony_ci		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
6228c2ecf20Sopenharmony_ci		wc->status = IB_WC_WR_FLUSH_ERR;
6238c2ecf20Sopenharmony_ci		wc->vendor_err = MLX4_CQE_SYNDROME_WR_FLUSH_ERR;
6248c2ecf20Sopenharmony_ci		wq->tail++;
6258c2ecf20Sopenharmony_ci		(*npolled)++;
6268c2ecf20Sopenharmony_ci		wc->qp = &qp->ibqp;
6278c2ecf20Sopenharmony_ci		wc++;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic void mlx4_ib_poll_sw_comp(struct mlx4_ib_cq *cq, int num_entries,
6328c2ecf20Sopenharmony_ci				 struct ib_wc *wc, int *npolled)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct mlx4_ib_qp *qp;
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	*npolled = 0;
6378c2ecf20Sopenharmony_ci	/* Find uncompleted WQEs belonging to that cq and return
6388c2ecf20Sopenharmony_ci	 * simulated FLUSH_ERR completions
6398c2ecf20Sopenharmony_ci	 */
6408c2ecf20Sopenharmony_ci	list_for_each_entry(qp, &cq->send_qp_list, cq_send_list) {
6418c2ecf20Sopenharmony_ci		mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 1);
6428c2ecf20Sopenharmony_ci		if (*npolled >= num_entries)
6438c2ecf20Sopenharmony_ci			goto out;
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	list_for_each_entry(qp, &cq->recv_qp_list, cq_recv_list) {
6478c2ecf20Sopenharmony_ci		mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 0);
6488c2ecf20Sopenharmony_ci		if (*npolled >= num_entries)
6498c2ecf20Sopenharmony_ci			goto out;
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ciout:
6538c2ecf20Sopenharmony_ci	return;
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
6578c2ecf20Sopenharmony_ci			    struct mlx4_ib_qp **cur_qp,
6588c2ecf20Sopenharmony_ci			    struct ib_wc *wc)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	struct mlx4_cqe *cqe;
6618c2ecf20Sopenharmony_ci	struct mlx4_qp *mqp;
6628c2ecf20Sopenharmony_ci	struct mlx4_ib_wq *wq;
6638c2ecf20Sopenharmony_ci	struct mlx4_ib_srq *srq;
6648c2ecf20Sopenharmony_ci	struct mlx4_srq *msrq = NULL;
6658c2ecf20Sopenharmony_ci	int is_send;
6668c2ecf20Sopenharmony_ci	int is_error;
6678c2ecf20Sopenharmony_ci	int is_eth;
6688c2ecf20Sopenharmony_ci	u32 g_mlpath_rqpn;
6698c2ecf20Sopenharmony_ci	u16 wqe_ctr;
6708c2ecf20Sopenharmony_ci	unsigned tail = 0;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cirepoll:
6738c2ecf20Sopenharmony_ci	cqe = next_cqe_sw(cq);
6748c2ecf20Sopenharmony_ci	if (!cqe)
6758c2ecf20Sopenharmony_ci		return -EAGAIN;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	if (cq->buf.entry_size == 64)
6788c2ecf20Sopenharmony_ci		cqe++;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	++cq->mcq.cons_index;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/*
6838c2ecf20Sopenharmony_ci	 * Make sure we read CQ entry contents after we've checked the
6848c2ecf20Sopenharmony_ci	 * ownership bit.
6858c2ecf20Sopenharmony_ci	 */
6868c2ecf20Sopenharmony_ci	rmb();
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	is_send  = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
6898c2ecf20Sopenharmony_ci	is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
6908c2ecf20Sopenharmony_ci		MLX4_CQE_OPCODE_ERROR;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	/* Resize CQ in progress */
6938c2ecf20Sopenharmony_ci	if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_CQE_OPCODE_RESIZE)) {
6948c2ecf20Sopenharmony_ci		if (cq->resize_buf) {
6958c2ecf20Sopenharmony_ci			struct mlx4_ib_dev *dev = to_mdev(cq->ibcq.device);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci			mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
6988c2ecf20Sopenharmony_ci			cq->buf      = cq->resize_buf->buf;
6998c2ecf20Sopenharmony_ci			cq->ibcq.cqe = cq->resize_buf->cqe;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci			kfree(cq->resize_buf);
7028c2ecf20Sopenharmony_ci			cq->resize_buf = NULL;
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		goto repoll;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (!*cur_qp ||
7098c2ecf20Sopenharmony_ci	    (be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) != (*cur_qp)->mqp.qpn) {
7108c2ecf20Sopenharmony_ci		/*
7118c2ecf20Sopenharmony_ci		 * We do not have to take the QP table lock here,
7128c2ecf20Sopenharmony_ci		 * because CQs will be locked while QPs are removed
7138c2ecf20Sopenharmony_ci		 * from the table.
7148c2ecf20Sopenharmony_ci		 */
7158c2ecf20Sopenharmony_ci		mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
7168c2ecf20Sopenharmony_ci				       be32_to_cpu(cqe->vlan_my_qpn));
7178c2ecf20Sopenharmony_ci		*cur_qp = to_mibqp(mqp);
7188c2ecf20Sopenharmony_ci	}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	wc->qp = &(*cur_qp)->ibqp;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	if (wc->qp->qp_type == IB_QPT_XRC_TGT) {
7238c2ecf20Sopenharmony_ci		u32 srq_num;
7248c2ecf20Sopenharmony_ci		g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
7258c2ecf20Sopenharmony_ci		srq_num       = g_mlpath_rqpn & 0xffffff;
7268c2ecf20Sopenharmony_ci		/* SRQ is also in the radix tree */
7278c2ecf20Sopenharmony_ci		msrq = mlx4_srq_lookup(to_mdev(cq->ibcq.device)->dev,
7288c2ecf20Sopenharmony_ci				       srq_num);
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	if (is_send) {
7328c2ecf20Sopenharmony_ci		wq = &(*cur_qp)->sq;
7338c2ecf20Sopenharmony_ci		if (!(*cur_qp)->sq_signal_bits) {
7348c2ecf20Sopenharmony_ci			wqe_ctr = be16_to_cpu(cqe->wqe_index);
7358c2ecf20Sopenharmony_ci			wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
7368c2ecf20Sopenharmony_ci		}
7378c2ecf20Sopenharmony_ci		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
7388c2ecf20Sopenharmony_ci		++wq->tail;
7398c2ecf20Sopenharmony_ci	} else if ((*cur_qp)->ibqp.srq) {
7408c2ecf20Sopenharmony_ci		srq = to_msrq((*cur_qp)->ibqp.srq);
7418c2ecf20Sopenharmony_ci		wqe_ctr = be16_to_cpu(cqe->wqe_index);
7428c2ecf20Sopenharmony_ci		wc->wr_id = srq->wrid[wqe_ctr];
7438c2ecf20Sopenharmony_ci		mlx4_ib_free_srq_wqe(srq, wqe_ctr);
7448c2ecf20Sopenharmony_ci	} else if (msrq) {
7458c2ecf20Sopenharmony_ci		srq = to_mibsrq(msrq);
7468c2ecf20Sopenharmony_ci		wqe_ctr = be16_to_cpu(cqe->wqe_index);
7478c2ecf20Sopenharmony_ci		wc->wr_id = srq->wrid[wqe_ctr];
7488c2ecf20Sopenharmony_ci		mlx4_ib_free_srq_wqe(srq, wqe_ctr);
7498c2ecf20Sopenharmony_ci	} else {
7508c2ecf20Sopenharmony_ci		wq	  = &(*cur_qp)->rq;
7518c2ecf20Sopenharmony_ci		tail	  = wq->tail & (wq->wqe_cnt - 1);
7528c2ecf20Sopenharmony_ci		wc->wr_id = wq->wrid[tail];
7538c2ecf20Sopenharmony_ci		++wq->tail;
7548c2ecf20Sopenharmony_ci	}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if (unlikely(is_error)) {
7578c2ecf20Sopenharmony_ci		mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
7588c2ecf20Sopenharmony_ci		return 0;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	wc->status = IB_WC_SUCCESS;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	if (is_send) {
7648c2ecf20Sopenharmony_ci		wc->wc_flags = 0;
7658c2ecf20Sopenharmony_ci		switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
7668c2ecf20Sopenharmony_ci		case MLX4_OPCODE_RDMA_WRITE_IMM:
7678c2ecf20Sopenharmony_ci			wc->wc_flags |= IB_WC_WITH_IMM;
7688c2ecf20Sopenharmony_ci			fallthrough;
7698c2ecf20Sopenharmony_ci		case MLX4_OPCODE_RDMA_WRITE:
7708c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_RDMA_WRITE;
7718c2ecf20Sopenharmony_ci			break;
7728c2ecf20Sopenharmony_ci		case MLX4_OPCODE_SEND_IMM:
7738c2ecf20Sopenharmony_ci			wc->wc_flags |= IB_WC_WITH_IMM;
7748c2ecf20Sopenharmony_ci			fallthrough;
7758c2ecf20Sopenharmony_ci		case MLX4_OPCODE_SEND:
7768c2ecf20Sopenharmony_ci		case MLX4_OPCODE_SEND_INVAL:
7778c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_SEND;
7788c2ecf20Sopenharmony_ci			break;
7798c2ecf20Sopenharmony_ci		case MLX4_OPCODE_RDMA_READ:
7808c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_RDMA_READ;
7818c2ecf20Sopenharmony_ci			wc->byte_len  = be32_to_cpu(cqe->byte_cnt);
7828c2ecf20Sopenharmony_ci			break;
7838c2ecf20Sopenharmony_ci		case MLX4_OPCODE_ATOMIC_CS:
7848c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_COMP_SWAP;
7858c2ecf20Sopenharmony_ci			wc->byte_len  = 8;
7868c2ecf20Sopenharmony_ci			break;
7878c2ecf20Sopenharmony_ci		case MLX4_OPCODE_ATOMIC_FA:
7888c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_FETCH_ADD;
7898c2ecf20Sopenharmony_ci			wc->byte_len  = 8;
7908c2ecf20Sopenharmony_ci			break;
7918c2ecf20Sopenharmony_ci		case MLX4_OPCODE_MASKED_ATOMIC_CS:
7928c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_MASKED_COMP_SWAP;
7938c2ecf20Sopenharmony_ci			wc->byte_len  = 8;
7948c2ecf20Sopenharmony_ci			break;
7958c2ecf20Sopenharmony_ci		case MLX4_OPCODE_MASKED_ATOMIC_FA:
7968c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_MASKED_FETCH_ADD;
7978c2ecf20Sopenharmony_ci			wc->byte_len  = 8;
7988c2ecf20Sopenharmony_ci			break;
7998c2ecf20Sopenharmony_ci		case MLX4_OPCODE_LSO:
8008c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_LSO;
8018c2ecf20Sopenharmony_ci			break;
8028c2ecf20Sopenharmony_ci		case MLX4_OPCODE_FMR:
8038c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_REG_MR;
8048c2ecf20Sopenharmony_ci			break;
8058c2ecf20Sopenharmony_ci		case MLX4_OPCODE_LOCAL_INVAL:
8068c2ecf20Sopenharmony_ci			wc->opcode    = IB_WC_LOCAL_INV;
8078c2ecf20Sopenharmony_ci			break;
8088c2ecf20Sopenharmony_ci		}
8098c2ecf20Sopenharmony_ci	} else {
8108c2ecf20Sopenharmony_ci		wc->byte_len = be32_to_cpu(cqe->byte_cnt);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci		switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
8138c2ecf20Sopenharmony_ci		case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
8148c2ecf20Sopenharmony_ci			wc->opcode	= IB_WC_RECV_RDMA_WITH_IMM;
8158c2ecf20Sopenharmony_ci			wc->wc_flags	= IB_WC_WITH_IMM;
8168c2ecf20Sopenharmony_ci			wc->ex.imm_data = cqe->immed_rss_invalid;
8178c2ecf20Sopenharmony_ci			break;
8188c2ecf20Sopenharmony_ci		case MLX4_RECV_OPCODE_SEND_INVAL:
8198c2ecf20Sopenharmony_ci			wc->opcode	= IB_WC_RECV;
8208c2ecf20Sopenharmony_ci			wc->wc_flags	= IB_WC_WITH_INVALIDATE;
8218c2ecf20Sopenharmony_ci			wc->ex.invalidate_rkey = be32_to_cpu(cqe->immed_rss_invalid);
8228c2ecf20Sopenharmony_ci			break;
8238c2ecf20Sopenharmony_ci		case MLX4_RECV_OPCODE_SEND:
8248c2ecf20Sopenharmony_ci			wc->opcode   = IB_WC_RECV;
8258c2ecf20Sopenharmony_ci			wc->wc_flags = 0;
8268c2ecf20Sopenharmony_ci			break;
8278c2ecf20Sopenharmony_ci		case MLX4_RECV_OPCODE_SEND_IMM:
8288c2ecf20Sopenharmony_ci			wc->opcode	= IB_WC_RECV;
8298c2ecf20Sopenharmony_ci			wc->wc_flags	= IB_WC_WITH_IMM;
8308c2ecf20Sopenharmony_ci			wc->ex.imm_data = cqe->immed_rss_invalid;
8318c2ecf20Sopenharmony_ci			break;
8328c2ecf20Sopenharmony_ci		}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci		is_eth = (rdma_port_get_link_layer(wc->qp->device,
8358c2ecf20Sopenharmony_ci						  (*cur_qp)->port) ==
8368c2ecf20Sopenharmony_ci			  IB_LINK_LAYER_ETHERNET);
8378c2ecf20Sopenharmony_ci		if (mlx4_is_mfunc(to_mdev(cq->ibcq.device)->dev)) {
8388c2ecf20Sopenharmony_ci			if ((*cur_qp)->mlx4_ib_qp_type &
8398c2ecf20Sopenharmony_ci			    (MLX4_IB_QPT_PROXY_SMI_OWNER |
8408c2ecf20Sopenharmony_ci			     MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
8418c2ecf20Sopenharmony_ci				use_tunnel_data(*cur_qp, cq, wc, tail, cqe,
8428c2ecf20Sopenharmony_ci						is_eth);
8438c2ecf20Sopenharmony_ci				return 0;
8448c2ecf20Sopenharmony_ci			}
8458c2ecf20Sopenharmony_ci		}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci		g_mlpath_rqpn	   = be32_to_cpu(cqe->g_mlpath_rqpn);
8488c2ecf20Sopenharmony_ci		wc->src_qp	   = g_mlpath_rqpn & 0xffffff;
8498c2ecf20Sopenharmony_ci		wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
8508c2ecf20Sopenharmony_ci		wc->wc_flags	  |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
8518c2ecf20Sopenharmony_ci		wc->pkey_index     = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
8528c2ecf20Sopenharmony_ci		wc->wc_flags	  |= mlx4_ib_ipoib_csum_ok(cqe->status,
8538c2ecf20Sopenharmony_ci					cqe->badfcs_enc,
8548c2ecf20Sopenharmony_ci					cqe->checksum) ? IB_WC_IP_CSUM_OK : 0;
8558c2ecf20Sopenharmony_ci		if (is_eth) {
8568c2ecf20Sopenharmony_ci			wc->slid = 0;
8578c2ecf20Sopenharmony_ci			wc->sl  = be16_to_cpu(cqe->sl_vid) >> 13;
8588c2ecf20Sopenharmony_ci			if (be32_to_cpu(cqe->vlan_my_qpn) &
8598c2ecf20Sopenharmony_ci					MLX4_CQE_CVLAN_PRESENT_MASK) {
8608c2ecf20Sopenharmony_ci				wc->vlan_id = be16_to_cpu(cqe->sl_vid) &
8618c2ecf20Sopenharmony_ci					MLX4_CQE_VID_MASK;
8628c2ecf20Sopenharmony_ci			} else {
8638c2ecf20Sopenharmony_ci				wc->vlan_id = 0xffff;
8648c2ecf20Sopenharmony_ci			}
8658c2ecf20Sopenharmony_ci			memcpy(wc->smac, cqe->smac, ETH_ALEN);
8668c2ecf20Sopenharmony_ci			wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
8678c2ecf20Sopenharmony_ci		} else {
8688c2ecf20Sopenharmony_ci			wc->slid = be16_to_cpu(cqe->rlid);
8698c2ecf20Sopenharmony_ci			wc->sl  = be16_to_cpu(cqe->sl_vid) >> 12;
8708c2ecf20Sopenharmony_ci			wc->vlan_id = 0xffff;
8718c2ecf20Sopenharmony_ci		}
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	return 0;
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ciint mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	struct mlx4_ib_cq *cq = to_mcq(ibcq);
8808c2ecf20Sopenharmony_ci	struct mlx4_ib_qp *cur_qp = NULL;
8818c2ecf20Sopenharmony_ci	unsigned long flags;
8828c2ecf20Sopenharmony_ci	int npolled;
8838c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *mdev = to_mdev(cq->ibcq.device);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cq->lock, flags);
8868c2ecf20Sopenharmony_ci	if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
8878c2ecf20Sopenharmony_ci		mlx4_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
8888c2ecf20Sopenharmony_ci		goto out;
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	for (npolled = 0; npolled < num_entries; ++npolled) {
8928c2ecf20Sopenharmony_ci		if (mlx4_ib_poll_one(cq, &cur_qp, wc + npolled))
8938c2ecf20Sopenharmony_ci			break;
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	mlx4_cq_set_ci(&cq->mcq);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ciout:
8998c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cq->lock, flags);
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	return npolled;
9028c2ecf20Sopenharmony_ci}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ciint mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	mlx4_cq_arm(&to_mcq(ibcq)->mcq,
9078c2ecf20Sopenharmony_ci		    (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
9088c2ecf20Sopenharmony_ci		    MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
9098c2ecf20Sopenharmony_ci		    to_mdev(ibcq->device)->uar_map,
9108c2ecf20Sopenharmony_ci		    MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	return 0;
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_civoid __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	u32 prod_index;
9188c2ecf20Sopenharmony_ci	int nfreed = 0;
9198c2ecf20Sopenharmony_ci	struct mlx4_cqe *cqe, *dest;
9208c2ecf20Sopenharmony_ci	u8 owner_bit;
9218c2ecf20Sopenharmony_ci	int cqe_inc = cq->buf.entry_size == 64 ? 1 : 0;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	/*
9248c2ecf20Sopenharmony_ci	 * First we need to find the current producer index, so we
9258c2ecf20Sopenharmony_ci	 * know where to start cleaning from.  It doesn't matter if HW
9268c2ecf20Sopenharmony_ci	 * adds new entries after this loop -- the QP we're worried
9278c2ecf20Sopenharmony_ci	 * about is already in RESET, so the new entries won't come
9288c2ecf20Sopenharmony_ci	 * from our QP and therefore don't need to be checked.
9298c2ecf20Sopenharmony_ci	 */
9308c2ecf20Sopenharmony_ci	for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
9318c2ecf20Sopenharmony_ci		if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
9328c2ecf20Sopenharmony_ci			break;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/*
9358c2ecf20Sopenharmony_ci	 * Now sweep backwards through the CQ, removing CQ entries
9368c2ecf20Sopenharmony_ci	 * that match our QP by copying older entries on top of them.
9378c2ecf20Sopenharmony_ci	 */
9388c2ecf20Sopenharmony_ci	while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
9398c2ecf20Sopenharmony_ci		cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
9408c2ecf20Sopenharmony_ci		cqe += cqe_inc;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		if ((be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) == qpn) {
9438c2ecf20Sopenharmony_ci			if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
9448c2ecf20Sopenharmony_ci				mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
9458c2ecf20Sopenharmony_ci			++nfreed;
9468c2ecf20Sopenharmony_ci		} else if (nfreed) {
9478c2ecf20Sopenharmony_ci			dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
9488c2ecf20Sopenharmony_ci			dest += cqe_inc;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci			owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
9518c2ecf20Sopenharmony_ci			memcpy(dest, cqe, sizeof *cqe);
9528c2ecf20Sopenharmony_ci			dest->owner_sr_opcode = owner_bit |
9538c2ecf20Sopenharmony_ci				(dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
9548c2ecf20Sopenharmony_ci		}
9558c2ecf20Sopenharmony_ci	}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	if (nfreed) {
9588c2ecf20Sopenharmony_ci		cq->mcq.cons_index += nfreed;
9598c2ecf20Sopenharmony_ci		/*
9608c2ecf20Sopenharmony_ci		 * Make sure update of buffer contents is done before
9618c2ecf20Sopenharmony_ci		 * updating consumer index.
9628c2ecf20Sopenharmony_ci		 */
9638c2ecf20Sopenharmony_ci		wmb();
9648c2ecf20Sopenharmony_ci		mlx4_cq_set_ci(&cq->mcq);
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_civoid mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
9698c2ecf20Sopenharmony_ci{
9708c2ecf20Sopenharmony_ci	spin_lock_irq(&cq->lock);
9718c2ecf20Sopenharmony_ci	__mlx4_ib_cq_clean(cq, qpn, srq);
9728c2ecf20Sopenharmony_ci	spin_unlock_irq(&cq->lock);
9738c2ecf20Sopenharmony_ci}
974