xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_cq.c (revision 95ee2897)
112515907SHans Petter Selasky /*-
2b633e08cSHans Petter Selasky  * Copyright (c) 2013-2020, Mellanox Technologies.  All rights reserved.
312515907SHans Petter Selasky  *
412515907SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
512515907SHans Petter Selasky  * modification, are permitted provided that the following conditions
612515907SHans Petter Selasky  * are met:
712515907SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
812515907SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
912515907SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
1012515907SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
1112515907SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
1212515907SHans Petter Selasky  *
1312515907SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
1412515907SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1512515907SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1612515907SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1712515907SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1812515907SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1912515907SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2012515907SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2112515907SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2212515907SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2312515907SHans Petter Selasky  * SUCH DAMAGE.
2412515907SHans Petter Selasky  */
2512515907SHans Petter Selasky 
2670600979SKonstantin Belousov #include "opt_rss.h"
2770600979SKonstantin Belousov #include "opt_ratelimit.h"
2870600979SKonstantin Belousov 
2912515907SHans Petter Selasky #include <linux/kref.h>
3012515907SHans Petter Selasky #include <rdma/ib_umem.h>
3112515907SHans Petter Selasky #include <rdma/ib_user_verbs.h>
328e6e287fSHans Petter Selasky #include <rdma/ib_cache.h>
33b633e08cSHans Petter Selasky #include <rdma/uverbs_ioctl.h>
34028130b8SKonstantin Belousov #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
3512515907SHans Petter Selasky 
mlx5_ib_cq_comp(struct mlx5_core_cq * cq,struct mlx5_eqe * eqe __unused)36f34f0a65SHans Petter Selasky static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq, struct mlx5_eqe *eqe __unused)
3712515907SHans Petter Selasky {
3812515907SHans Petter Selasky 	struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
3912515907SHans Petter Selasky 
4012515907SHans Petter Selasky 	ibcq->comp_handler(ibcq, ibcq->cq_context);
4112515907SHans Petter Selasky }
4212515907SHans Petter Selasky 
mlx5_ib_cq_event(struct mlx5_core_cq * mcq,int type)4312515907SHans Petter Selasky static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, int type)
4412515907SHans Petter Selasky {
4512515907SHans Petter Selasky 	struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
4612515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
4712515907SHans Petter Selasky 	struct ib_cq *ibcq = &cq->ibcq;
4812515907SHans Petter Selasky 	struct ib_event event;
4912515907SHans Petter Selasky 
5012515907SHans Petter Selasky 	if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
5112515907SHans Petter Selasky 		mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
5212515907SHans Petter Selasky 			     type, mcq->cqn);
5312515907SHans Petter Selasky 		return;
5412515907SHans Petter Selasky 	}
5512515907SHans Petter Selasky 
5612515907SHans Petter Selasky 	if (ibcq->event_handler) {
5712515907SHans Petter Selasky 		event.device     = &dev->ib_dev;
5812515907SHans Petter Selasky 		event.event      = IB_EVENT_CQ_ERR;
5912515907SHans Petter Selasky 		event.element.cq = ibcq;
6012515907SHans Petter Selasky 		ibcq->event_handler(&event, ibcq->cq_context);
6112515907SHans Petter Selasky 	}
6212515907SHans Petter Selasky }
6312515907SHans Petter Selasky 
get_cqe_from_buf(struct mlx5_ib_cq_buf * buf,int n,int size)6412515907SHans Petter Selasky static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
6512515907SHans Petter Selasky {
6612515907SHans Petter Selasky 	return mlx5_buf_offset(&buf->buf, n * size);
6712515907SHans Petter Selasky }
6812515907SHans Petter Selasky 
get_cqe(struct mlx5_ib_cq * cq,int n)6912515907SHans Petter Selasky static void *get_cqe(struct mlx5_ib_cq *cq, int n)
7012515907SHans Petter Selasky {
7112515907SHans Petter Selasky 	return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
7212515907SHans Petter Selasky }
7312515907SHans Petter Selasky 
sw_ownership_bit(int n,int nent)7412515907SHans Petter Selasky static u8 sw_ownership_bit(int n, int nent)
7512515907SHans Petter Selasky {
7612515907SHans Petter Selasky 	return (n & nent) ? 1 : 0;
7712515907SHans Petter Selasky }
7812515907SHans Petter Selasky 
get_sw_cqe(struct mlx5_ib_cq * cq,int n)7912515907SHans Petter Selasky static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
8012515907SHans Petter Selasky {
8112515907SHans Petter Selasky 	void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
8212515907SHans Petter Selasky 	struct mlx5_cqe64 *cqe64;
8312515907SHans Petter Selasky 
8412515907SHans Petter Selasky 	cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
8512515907SHans Petter Selasky 
8612515907SHans Petter Selasky 	if (likely((cqe64->op_own) >> 4 != MLX5_CQE_INVALID) &&
8712515907SHans Petter Selasky 	    !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & (cq->ibcq.cqe + 1)))) {
8812515907SHans Petter Selasky 		return cqe;
8912515907SHans Petter Selasky 	} else {
9012515907SHans Petter Selasky 		return NULL;
9112515907SHans Petter Selasky 	}
9212515907SHans Petter Selasky }
9312515907SHans Petter Selasky 
next_cqe_sw(struct mlx5_ib_cq * cq)9412515907SHans Petter Selasky static void *next_cqe_sw(struct mlx5_ib_cq *cq)
9512515907SHans Petter Selasky {
9612515907SHans Petter Selasky 	return get_sw_cqe(cq, cq->mcq.cons_index);
9712515907SHans Petter Selasky }
9812515907SHans Petter Selasky 
get_umr_comp(struct mlx5_ib_wq * wq,int idx)9912515907SHans Petter Selasky static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
10012515907SHans Petter Selasky {
1018e6e287fSHans Petter Selasky 	switch (wq->wr_data[idx]) {
1028e6e287fSHans Petter Selasky 	case MLX5_IB_WR_UMR:
1038e6e287fSHans Petter Selasky 		return 0;
1048e6e287fSHans Petter Selasky 
10512515907SHans Petter Selasky 	case IB_WR_LOCAL_INV:
10612515907SHans Petter Selasky 		return IB_WC_LOCAL_INV;
10712515907SHans Petter Selasky 
1088e6e287fSHans Petter Selasky 	case IB_WR_REG_MR:
1098e6e287fSHans Petter Selasky 		return IB_WC_REG_MR;
11012515907SHans Petter Selasky 
11112515907SHans Petter Selasky 	default:
1128e6e287fSHans Petter Selasky 		pr_warn("unknown completion status\n");
11312515907SHans Petter Selasky 		return 0;
11412515907SHans Petter Selasky 	}
11512515907SHans Petter Selasky }
11612515907SHans Petter Selasky 
handle_good_req(struct ib_wc * wc,struct mlx5_cqe64 * cqe,struct mlx5_ib_wq * wq,int idx)11712515907SHans Petter Selasky static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
11812515907SHans Petter Selasky 			    struct mlx5_ib_wq *wq, int idx)
11912515907SHans Petter Selasky {
12012515907SHans Petter Selasky 	wc->wc_flags = 0;
12112515907SHans Petter Selasky 	switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
12212515907SHans Petter Selasky 	case MLX5_OPCODE_RDMA_WRITE_IMM:
12312515907SHans Petter Selasky 		wc->wc_flags |= IB_WC_WITH_IMM;
12412515907SHans Petter Selasky 	case MLX5_OPCODE_RDMA_WRITE:
12512515907SHans Petter Selasky 		wc->opcode    = IB_WC_RDMA_WRITE;
12612515907SHans Petter Selasky 		break;
12712515907SHans Petter Selasky 	case MLX5_OPCODE_SEND_IMM:
12812515907SHans Petter Selasky 		wc->wc_flags |= IB_WC_WITH_IMM;
12912515907SHans Petter Selasky 	case MLX5_OPCODE_SEND:
13012515907SHans Petter Selasky 	case MLX5_OPCODE_SEND_INVAL:
13112515907SHans Petter Selasky 		wc->opcode    = IB_WC_SEND;
13212515907SHans Petter Selasky 		break;
13312515907SHans Petter Selasky 	case MLX5_OPCODE_RDMA_READ:
13412515907SHans Petter Selasky 		wc->opcode    = IB_WC_RDMA_READ;
13512515907SHans Petter Selasky 		wc->byte_len  = be32_to_cpu(cqe->byte_cnt);
13612515907SHans Petter Selasky 		break;
13712515907SHans Petter Selasky 	case MLX5_OPCODE_ATOMIC_CS:
13812515907SHans Petter Selasky 		wc->opcode    = IB_WC_COMP_SWAP;
13912515907SHans Petter Selasky 		wc->byte_len  = 8;
14012515907SHans Petter Selasky 		break;
14112515907SHans Petter Selasky 	case MLX5_OPCODE_ATOMIC_FA:
14212515907SHans Petter Selasky 		wc->opcode    = IB_WC_FETCH_ADD;
14312515907SHans Petter Selasky 		wc->byte_len  = 8;
14412515907SHans Petter Selasky 		break;
14512515907SHans Petter Selasky 	case MLX5_OPCODE_ATOMIC_MASKED_CS:
14612515907SHans Petter Selasky 		wc->opcode    = IB_WC_MASKED_COMP_SWAP;
14712515907SHans Petter Selasky 		wc->byte_len  = 8;
14812515907SHans Petter Selasky 		break;
14912515907SHans Petter Selasky 	case MLX5_OPCODE_ATOMIC_MASKED_FA:
15012515907SHans Petter Selasky 		wc->opcode    = IB_WC_MASKED_FETCH_ADD;
15112515907SHans Petter Selasky 		wc->byte_len  = 8;
15212515907SHans Petter Selasky 		break;
15312515907SHans Petter Selasky 	case MLX5_OPCODE_UMR:
15412515907SHans Petter Selasky 		wc->opcode = get_umr_comp(wq, idx);
15512515907SHans Petter Selasky 		break;
15612515907SHans Petter Selasky 	}
15712515907SHans Petter Selasky }
15812515907SHans Petter Selasky 
15912515907SHans Petter Selasky enum {
16012515907SHans Petter Selasky 	MLX5_GRH_IN_BUFFER = 1,
16112515907SHans Petter Selasky 	MLX5_GRH_IN_CQE	   = 2,
16212515907SHans Petter Selasky };
16312515907SHans Petter Selasky 
handle_responder(struct ib_wc * wc,struct mlx5_cqe64 * cqe,struct mlx5_ib_qp * qp)16412515907SHans Petter Selasky static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
16512515907SHans Petter Selasky 			     struct mlx5_ib_qp *qp)
16612515907SHans Petter Selasky {
1678e6e287fSHans Petter Selasky 	enum rdma_link_layer ll = rdma_port_get_link_layer(qp->ibqp.device, 1);
16812515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
16912515907SHans Petter Selasky 	struct mlx5_ib_srq *srq;
17012515907SHans Petter Selasky 	struct mlx5_ib_wq *wq;
17112515907SHans Petter Selasky 	u16 wqe_ctr;
17250e3ba10SHans Petter Selasky 	u8  roce_packet_type;
17350e3ba10SHans Petter Selasky 	bool vlan_present;
17412515907SHans Petter Selasky 	u8 g;
17512515907SHans Petter Selasky 
17612515907SHans Petter Selasky 	if (qp->ibqp.srq || qp->ibqp.xrcd) {
17712515907SHans Petter Selasky 		struct mlx5_core_srq *msrq = NULL;
17812515907SHans Petter Selasky 
17912515907SHans Petter Selasky 		if (qp->ibqp.xrcd) {
18012515907SHans Petter Selasky 			msrq = mlx5_core_get_srq(dev->mdev,
18112515907SHans Petter Selasky 						 be32_to_cpu(cqe->srqn));
18212515907SHans Petter Selasky 			srq = to_mibsrq(msrq);
18312515907SHans Petter Selasky 		} else {
18412515907SHans Petter Selasky 			srq = to_msrq(qp->ibqp.srq);
18512515907SHans Petter Selasky 		}
18612515907SHans Petter Selasky 		if (srq) {
18712515907SHans Petter Selasky 			wqe_ctr = be16_to_cpu(cqe->wqe_counter);
18812515907SHans Petter Selasky 			wc->wr_id = srq->wrid[wqe_ctr];
18912515907SHans Petter Selasky 			mlx5_ib_free_srq_wqe(srq, wqe_ctr);
19012515907SHans Petter Selasky 			if (msrq && atomic_dec_and_test(&msrq->refcount))
19112515907SHans Petter Selasky 				complete(&msrq->free);
19212515907SHans Petter Selasky 		}
19312515907SHans Petter Selasky 	} else {
19412515907SHans Petter Selasky 		wq	  = &qp->rq;
1958e6e287fSHans Petter Selasky 		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
19612515907SHans Petter Selasky 		++wq->tail;
19712515907SHans Petter Selasky 	}
19812515907SHans Petter Selasky 	wc->byte_len = be32_to_cpu(cqe->byte_cnt);
19912515907SHans Petter Selasky 
20012515907SHans Petter Selasky 	switch (cqe->op_own >> 4) {
20112515907SHans Petter Selasky 	case MLX5_CQE_RESP_WR_IMM:
20212515907SHans Petter Selasky 		wc->opcode	= IB_WC_RECV_RDMA_WITH_IMM;
20312515907SHans Petter Selasky 		wc->wc_flags	= IB_WC_WITH_IMM;
20412515907SHans Petter Selasky 		wc->ex.imm_data = cqe->imm_inval_pkey;
20512515907SHans Petter Selasky 		break;
20612515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND:
20712515907SHans Petter Selasky 		wc->opcode   = IB_WC_RECV;
2088e6e287fSHans Petter Selasky 		wc->wc_flags = IB_WC_IP_CSUM_OK;
2098e6e287fSHans Petter Selasky 		if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
2108e6e287fSHans Petter Selasky 			       (cqe->hds_ip_ext & CQE_L4_OK))))
21112515907SHans Petter Selasky 			wc->wc_flags = 0;
21212515907SHans Petter Selasky 		break;
21312515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND_IMM:
21412515907SHans Petter Selasky 		wc->opcode	= IB_WC_RECV;
21512515907SHans Petter Selasky 		wc->wc_flags	= IB_WC_WITH_IMM;
21612515907SHans Petter Selasky 		wc->ex.imm_data = cqe->imm_inval_pkey;
21712515907SHans Petter Selasky 		break;
21812515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND_INV:
21912515907SHans Petter Selasky 		wc->opcode	= IB_WC_RECV;
22012515907SHans Petter Selasky 		wc->wc_flags	= IB_WC_WITH_INVALIDATE;
22112515907SHans Petter Selasky 		wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
22212515907SHans Petter Selasky 		break;
22312515907SHans Petter Selasky 	}
22412515907SHans Petter Selasky 	wc->src_qp	   = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
22512515907SHans Petter Selasky 	wc->dlid_path_bits = cqe->ml_path;
22612515907SHans Petter Selasky 	g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
22712515907SHans Petter Selasky 	wc->wc_flags |= g ? IB_WC_GRH : 0;
2288e6e287fSHans Petter Selasky 	if (unlikely(is_qp1(qp->ibqp.qp_type))) {
2298e6e287fSHans Petter Selasky 		u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
23012515907SHans Petter Selasky 
2318e6e287fSHans Petter Selasky 		ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
2328e6e287fSHans Petter Selasky 				    &wc->pkey_index);
2338e6e287fSHans Petter Selasky 	} else {
2348e6e287fSHans Petter Selasky 		wc->pkey_index = 0;
2358e6e287fSHans Petter Selasky 	}
23612515907SHans Petter Selasky 
23750e3ba10SHans Petter Selasky 	if (ll != IB_LINK_LAYER_ETHERNET) {
23830416d4eSHans Petter Selasky 		wc->slid = be16_to_cpu(cqe->slid);
23950e3ba10SHans Petter Selasky 		wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
2408e6e287fSHans Petter Selasky 		return;
24150e3ba10SHans Petter Selasky 	}
2428e6e287fSHans Petter Selasky 
24330416d4eSHans Petter Selasky 	wc->slid = 0;
24450e3ba10SHans Petter Selasky 	vlan_present = cqe_has_vlan(cqe);
24550e3ba10SHans Petter Selasky 	roce_packet_type   = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0x3;
24650e3ba10SHans Petter Selasky 	if (vlan_present) {
24750e3ba10SHans Petter Selasky 		wc->vlan_id = (be16_to_cpu(cqe->vlan_info)) & 0xfff;
24850e3ba10SHans Petter Selasky 		wc->sl = (be16_to_cpu(cqe->vlan_info) >> 13) & 0x7;
24950e3ba10SHans Petter Selasky 		wc->wc_flags |= IB_WC_WITH_VLAN;
25050e3ba10SHans Petter Selasky 	} else {
25150e3ba10SHans Petter Selasky 		wc->sl = 0;
25250e3ba10SHans Petter Selasky 	}
25350e3ba10SHans Petter Selasky 
25450e3ba10SHans Petter Selasky 	switch (roce_packet_type) {
2558e6e287fSHans Petter Selasky 	case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
2568e6e287fSHans Petter Selasky 		wc->network_hdr_type = RDMA_NETWORK_IB;
2578e6e287fSHans Petter Selasky 		break;
2588e6e287fSHans Petter Selasky 	case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
2598e6e287fSHans Petter Selasky 		wc->network_hdr_type = RDMA_NETWORK_IPV6;
2608e6e287fSHans Petter Selasky 		break;
2618e6e287fSHans Petter Selasky 	case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
2628e6e287fSHans Petter Selasky 		wc->network_hdr_type = RDMA_NETWORK_IPV4;
2638e6e287fSHans Petter Selasky 		break;
2648e6e287fSHans Petter Selasky 	}
2658e6e287fSHans Petter Selasky 	wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
26612515907SHans Petter Selasky }
26712515907SHans Petter Selasky 
dump_cqe(struct mlx5_ib_dev * dev,struct mlx5_err_cqe * cqe)26812515907SHans Petter Selasky static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
26912515907SHans Petter Selasky {
27012515907SHans Petter Selasky 	__be32 *p = (__be32 *)cqe;
27112515907SHans Petter Selasky 	int i;
27212515907SHans Petter Selasky 
27312515907SHans Petter Selasky 	mlx5_ib_warn(dev, "dump error cqe\n");
27412515907SHans Petter Selasky 	for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
2758e6e287fSHans Petter Selasky 		pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
2768e6e287fSHans Petter Selasky 			be32_to_cpu(p[1]), be32_to_cpu(p[2]),
2778e6e287fSHans Petter Selasky 			be32_to_cpu(p[3]));
27812515907SHans Petter Selasky }
27912515907SHans Petter Selasky 
mlx5_handle_error_cqe(struct mlx5_ib_dev * dev,struct mlx5_err_cqe * cqe,struct ib_wc * wc)28012515907SHans Petter Selasky static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
28112515907SHans Petter Selasky 				  struct mlx5_err_cqe *cqe,
28212515907SHans Petter Selasky 				  struct ib_wc *wc)
28312515907SHans Petter Selasky {
28412515907SHans Petter Selasky 	int dump = 1;
28512515907SHans Petter Selasky 
28612515907SHans Petter Selasky 	switch (cqe->syndrome) {
28712515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
28812515907SHans Petter Selasky 		wc->status = IB_WC_LOC_LEN_ERR;
28912515907SHans Petter Selasky 		break;
29012515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
29112515907SHans Petter Selasky 		wc->status = IB_WC_LOC_QP_OP_ERR;
29212515907SHans Petter Selasky 		break;
29312515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
29412515907SHans Petter Selasky 		wc->status = IB_WC_LOC_PROT_ERR;
29512515907SHans Petter Selasky 		break;
29612515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
29712515907SHans Petter Selasky 		dump = 0;
29812515907SHans Petter Selasky 		wc->status = IB_WC_WR_FLUSH_ERR;
29912515907SHans Petter Selasky 		break;
30012515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_MW_BIND_ERR:
30112515907SHans Petter Selasky 		wc->status = IB_WC_MW_BIND_ERR;
30212515907SHans Petter Selasky 		break;
30312515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
30412515907SHans Petter Selasky 		wc->status = IB_WC_BAD_RESP_ERR;
30512515907SHans Petter Selasky 		break;
30612515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
30712515907SHans Petter Selasky 		wc->status = IB_WC_LOC_ACCESS_ERR;
30812515907SHans Petter Selasky 		break;
30912515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
31012515907SHans Petter Selasky 		wc->status = IB_WC_REM_INV_REQ_ERR;
31112515907SHans Petter Selasky 		break;
31212515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
31312515907SHans Petter Selasky 		wc->status = IB_WC_REM_ACCESS_ERR;
31412515907SHans Petter Selasky 		break;
31512515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
31612515907SHans Petter Selasky 		wc->status = IB_WC_REM_OP_ERR;
31712515907SHans Petter Selasky 		break;
31812515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
31912515907SHans Petter Selasky 		wc->status = IB_WC_RETRY_EXC_ERR;
32012515907SHans Petter Selasky 		dump = 0;
32112515907SHans Petter Selasky 		break;
32212515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
32312515907SHans Petter Selasky 		wc->status = IB_WC_RNR_RETRY_EXC_ERR;
32412515907SHans Petter Selasky 		dump = 0;
32512515907SHans Petter Selasky 		break;
32612515907SHans Petter Selasky 	case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
32712515907SHans Petter Selasky 		wc->status = IB_WC_REM_ABORT_ERR;
32812515907SHans Petter Selasky 		break;
32912515907SHans Petter Selasky 	default:
33012515907SHans Petter Selasky 		wc->status = IB_WC_GENERAL_ERR;
33112515907SHans Petter Selasky 		break;
33212515907SHans Petter Selasky 	}
33312515907SHans Petter Selasky 
33412515907SHans Petter Selasky 	wc->vendor_err = cqe->vendor_err_synd;
33512515907SHans Petter Selasky 	if (dump)
33612515907SHans Petter Selasky 		dump_cqe(dev, cqe);
33712515907SHans Petter Selasky }
33812515907SHans Petter Selasky 
is_atomic_response(struct mlx5_ib_qp * qp,uint16_t idx)3398e6e287fSHans Petter Selasky static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
34012515907SHans Petter Selasky {
34112515907SHans Petter Selasky 	/* TBD: waiting decision
34212515907SHans Petter Selasky 	*/
34312515907SHans Petter Selasky 	return 0;
34412515907SHans Petter Selasky }
34512515907SHans Petter Selasky 
mlx5_get_atomic_laddr(struct mlx5_ib_qp * qp,uint16_t idx)3468e6e287fSHans Petter Selasky static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
34712515907SHans Petter Selasky {
34812515907SHans Petter Selasky 	struct mlx5_wqe_data_seg *dpseg;
34912515907SHans Petter Selasky 	void *addr;
35012515907SHans Petter Selasky 
35112515907SHans Petter Selasky 	dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
35212515907SHans Petter Selasky 		sizeof(struct mlx5_wqe_raddr_seg) +
35312515907SHans Petter Selasky 		sizeof(struct mlx5_wqe_atomic_seg);
3548e6e287fSHans Petter Selasky 	addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
35512515907SHans Petter Selasky 	return addr;
35612515907SHans Petter Selasky }
35712515907SHans Petter Selasky 
handle_atomic(struct mlx5_ib_qp * qp,struct mlx5_cqe64 * cqe64,uint16_t idx)35812515907SHans Petter Selasky static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
3598e6e287fSHans Petter Selasky 			  uint16_t idx)
36012515907SHans Petter Selasky {
36112515907SHans Petter Selasky 	void *addr;
36212515907SHans Petter Selasky 	int byte_count;
36312515907SHans Petter Selasky 	int i;
36412515907SHans Petter Selasky 
36512515907SHans Petter Selasky 	if (!is_atomic_response(qp, idx))
36612515907SHans Petter Selasky 		return;
36712515907SHans Petter Selasky 
36812515907SHans Petter Selasky 	byte_count = be32_to_cpu(cqe64->byte_cnt);
36912515907SHans Petter Selasky 	addr = mlx5_get_atomic_laddr(qp, idx);
37012515907SHans Petter Selasky 
37112515907SHans Petter Selasky 	if (byte_count == 4) {
3728e6e287fSHans Petter Selasky 		*(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
37312515907SHans Petter Selasky 	} else {
37412515907SHans Petter Selasky 		for (i = 0; i < byte_count; i += 8) {
3758e6e287fSHans Petter Selasky 			*(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
37612515907SHans Petter Selasky 			addr += 8;
37712515907SHans Petter Selasky 		}
37812515907SHans Petter Selasky 	}
37912515907SHans Petter Selasky 
38012515907SHans Petter Selasky 	return;
38112515907SHans Petter Selasky }
38212515907SHans Petter Selasky 
handle_atomics(struct mlx5_ib_qp * qp,struct mlx5_cqe64 * cqe64,u16 tail,u16 head)38312515907SHans Petter Selasky static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
38412515907SHans Petter Selasky 			   u16 tail, u16 head)
38512515907SHans Petter Selasky {
38612515907SHans Petter Selasky 	u16 idx;
38712515907SHans Petter Selasky 
38812515907SHans Petter Selasky 	do {
38912515907SHans Petter Selasky 		idx = tail & (qp->sq.wqe_cnt - 1);
39012515907SHans Petter Selasky 		handle_atomic(qp, cqe64, idx);
39112515907SHans Petter Selasky 		if (idx == head)
39212515907SHans Petter Selasky 			break;
39312515907SHans Petter Selasky 
3948e6e287fSHans Petter Selasky 		tail = qp->sq.w_list[idx].next;
39512515907SHans Petter Selasky 	} while (1);
3968e6e287fSHans Petter Selasky 	tail = qp->sq.w_list[idx].next;
39712515907SHans Petter Selasky 	qp->sq.last_poll = tail;
39812515907SHans Petter Selasky }
39912515907SHans Petter Selasky 
free_cq_buf(struct mlx5_ib_dev * dev,struct mlx5_ib_cq_buf * buf)40012515907SHans Petter Selasky static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
40112515907SHans Petter Selasky {
40212515907SHans Petter Selasky 	mlx5_buf_free(dev->mdev, &buf->buf);
40312515907SHans Petter Selasky }
40412515907SHans Petter Selasky 
get_sig_err_item(struct mlx5_sig_err_cqe * cqe,struct ib_sig_err * item)4058e6e287fSHans Petter Selasky static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
4068e6e287fSHans Petter Selasky 			     struct ib_sig_err *item)
4078e6e287fSHans Petter Selasky {
4088e6e287fSHans Petter Selasky 	u16 syndrome = be16_to_cpu(cqe->syndrome);
4098e6e287fSHans Petter Selasky 
4108e6e287fSHans Petter Selasky #define GUARD_ERR   (1 << 13)
4118e6e287fSHans Petter Selasky #define APPTAG_ERR  (1 << 12)
4128e6e287fSHans Petter Selasky #define REFTAG_ERR  (1 << 11)
4138e6e287fSHans Petter Selasky 
4148e6e287fSHans Petter Selasky 	if (syndrome & GUARD_ERR) {
4158e6e287fSHans Petter Selasky 		item->err_type = IB_SIG_BAD_GUARD;
4168e6e287fSHans Petter Selasky 		item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
4178e6e287fSHans Petter Selasky 		item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
4188e6e287fSHans Petter Selasky 	} else
4198e6e287fSHans Petter Selasky 	if (syndrome & REFTAG_ERR) {
4208e6e287fSHans Petter Selasky 		item->err_type = IB_SIG_BAD_REFTAG;
4218e6e287fSHans Petter Selasky 		item->expected = be32_to_cpu(cqe->expected_reftag);
4228e6e287fSHans Petter Selasky 		item->actual = be32_to_cpu(cqe->actual_reftag);
4238e6e287fSHans Petter Selasky 	} else
4248e6e287fSHans Petter Selasky 	if (syndrome & APPTAG_ERR) {
4258e6e287fSHans Petter Selasky 		item->err_type = IB_SIG_BAD_APPTAG;
4268e6e287fSHans Petter Selasky 		item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
4278e6e287fSHans Petter Selasky 		item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
4288e6e287fSHans Petter Selasky 	} else {
4298e6e287fSHans Petter Selasky 		pr_err("Got signature completion error with bad syndrome %04x\n",
4308e6e287fSHans Petter Selasky 		       syndrome);
4318e6e287fSHans Petter Selasky 	}
4328e6e287fSHans Petter Selasky 
4338e6e287fSHans Petter Selasky 	item->sig_err_offset = be64_to_cpu(cqe->err_offset);
4348e6e287fSHans Petter Selasky 	item->key = be32_to_cpu(cqe->mkey);
4358e6e287fSHans Petter Selasky }
4368e6e287fSHans Petter Selasky 
sw_send_comp(struct mlx5_ib_qp * qp,int num_entries,struct ib_wc * wc,int * npolled)43712515907SHans Petter Selasky static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
43812515907SHans Petter Selasky 			 struct ib_wc *wc, int *npolled)
43912515907SHans Petter Selasky {
44012515907SHans Petter Selasky 	struct mlx5_ib_wq *wq;
4418e6e287fSHans Petter Selasky 	unsigned int cur;
4428e6e287fSHans Petter Selasky 	unsigned int idx;
44312515907SHans Petter Selasky 	int np;
44412515907SHans Petter Selasky 	int i;
44512515907SHans Petter Selasky 
44612515907SHans Petter Selasky 	wq = &qp->sq;
44712515907SHans Petter Selasky 	cur = wq->head - wq->tail;
44812515907SHans Petter Selasky 	np = *npolled;
44912515907SHans Petter Selasky 
45012515907SHans Petter Selasky 	if (cur == 0)
45112515907SHans Petter Selasky 		return;
45212515907SHans Petter Selasky 
45312515907SHans Petter Selasky 	for (i = 0;  i < cur && np < num_entries; i++) {
45412515907SHans Petter Selasky 		idx = wq->last_poll & (wq->wqe_cnt - 1);
4558e6e287fSHans Petter Selasky 		wc->wr_id = wq->wrid[idx];
45612515907SHans Petter Selasky 		wc->status = IB_WC_WR_FLUSH_ERR;
45712515907SHans Petter Selasky 		wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
45812515907SHans Petter Selasky 		wq->tail++;
45912515907SHans Petter Selasky 		np++;
46012515907SHans Petter Selasky 		wc->qp = &qp->ibqp;
46112515907SHans Petter Selasky 		wc++;
4628e6e287fSHans Petter Selasky 		wq->last_poll = wq->w_list[idx].next;
46312515907SHans Petter Selasky 	}
46412515907SHans Petter Selasky 	*npolled = np;
46512515907SHans Petter Selasky }
46612515907SHans Petter Selasky 
sw_recv_comp(struct mlx5_ib_qp * qp,int num_entries,struct ib_wc * wc,int * npolled)46712515907SHans Petter Selasky static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
46812515907SHans Petter Selasky 			 struct ib_wc *wc, int *npolled)
46912515907SHans Petter Selasky {
47012515907SHans Petter Selasky 	struct mlx5_ib_wq *wq;
4718e6e287fSHans Petter Selasky 	unsigned int cur;
47212515907SHans Petter Selasky 	int np;
47312515907SHans Petter Selasky 	int i;
47412515907SHans Petter Selasky 
47512515907SHans Petter Selasky 	wq = &qp->rq;
47612515907SHans Petter Selasky 	cur = wq->head - wq->tail;
47712515907SHans Petter Selasky 	np = *npolled;
47812515907SHans Petter Selasky 
47912515907SHans Petter Selasky 	if (cur == 0)
48012515907SHans Petter Selasky 		return;
48112515907SHans Petter Selasky 
48212515907SHans Petter Selasky 	for (i = 0;  i < cur && np < num_entries; i++) {
4838e6e287fSHans Petter Selasky 		wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
48412515907SHans Petter Selasky 		wc->status = IB_WC_WR_FLUSH_ERR;
48512515907SHans Petter Selasky 		wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
48612515907SHans Petter Selasky 		wq->tail++;
48712515907SHans Petter Selasky 		np++;
48812515907SHans Petter Selasky 		wc->qp = &qp->ibqp;
48912515907SHans Petter Selasky 		wc++;
49012515907SHans Petter Selasky 	}
49112515907SHans Petter Selasky 	*npolled = np;
49212515907SHans Petter Selasky }
49312515907SHans Petter Selasky 
mlx5_ib_poll_sw_comp(struct mlx5_ib_cq * cq,int num_entries,struct ib_wc * wc,int * npolled)49412515907SHans Petter Selasky static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
49512515907SHans Petter Selasky 				 struct ib_wc *wc, int *npolled)
49612515907SHans Petter Selasky {
49712515907SHans Petter Selasky 	struct mlx5_ib_qp *qp;
49812515907SHans Petter Selasky 
49912515907SHans Petter Selasky 	*npolled = 0;
50012515907SHans Petter Selasky 	/* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
50112515907SHans Petter Selasky 	list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
50212515907SHans Petter Selasky 		sw_send_comp(qp, num_entries, wc + *npolled, npolled);
50312515907SHans Petter Selasky 		if (*npolled >= num_entries)
50412515907SHans Petter Selasky 			return;
50512515907SHans Petter Selasky 	}
50612515907SHans Petter Selasky 
50712515907SHans Petter Selasky 	list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
50812515907SHans Petter Selasky 		sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
50912515907SHans Petter Selasky 		if (*npolled >= num_entries)
51012515907SHans Petter Selasky 			return;
51112515907SHans Petter Selasky 	}
51212515907SHans Petter Selasky }
51312515907SHans Petter Selasky 
mlx5_poll_one(struct mlx5_ib_cq * cq,struct mlx5_ib_qp ** cur_qp,struct ib_wc * wc)51412515907SHans Petter Selasky static int mlx5_poll_one(struct mlx5_ib_cq *cq,
51512515907SHans Petter Selasky 			 struct mlx5_ib_qp **cur_qp,
51612515907SHans Petter Selasky 			 struct ib_wc *wc)
51712515907SHans Petter Selasky {
51812515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
51912515907SHans Petter Selasky 	struct mlx5_err_cqe *err_cqe;
52012515907SHans Petter Selasky 	struct mlx5_cqe64 *cqe64;
52112515907SHans Petter Selasky 	struct mlx5_core_qp *mqp;
52212515907SHans Petter Selasky 	struct mlx5_ib_wq *wq;
52312515907SHans Petter Selasky 	struct mlx5_sig_err_cqe *sig_err_cqe;
524b633e08cSHans Petter Selasky 	struct mlx5_core_mkey *mmkey;
52512515907SHans Petter Selasky 	struct mlx5_ib_mr *mr;
52612515907SHans Petter Selasky 	unsigned long flags;
5278e6e287fSHans Petter Selasky 	uint8_t opcode;
5288e6e287fSHans Petter Selasky 	uint32_t qpn;
52912515907SHans Petter Selasky 	u16 wqe_ctr;
53012515907SHans Petter Selasky 	void *cqe;
53112515907SHans Petter Selasky 	int idx;
53212515907SHans Petter Selasky 
53312515907SHans Petter Selasky repoll:
53412515907SHans Petter Selasky 	cqe = next_cqe_sw(cq);
53512515907SHans Petter Selasky 	if (!cqe)
53612515907SHans Petter Selasky 		return -EAGAIN;
53712515907SHans Petter Selasky 
53812515907SHans Petter Selasky 	cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
53912515907SHans Petter Selasky 
54012515907SHans Petter Selasky 	++cq->mcq.cons_index;
54112515907SHans Petter Selasky 
54212515907SHans Petter Selasky 	/* Make sure we read CQ entry contents after we've checked the
54312515907SHans Petter Selasky 	 * ownership bit.
54412515907SHans Petter Selasky 	 */
54512515907SHans Petter Selasky 	rmb();
54612515907SHans Petter Selasky 
54712515907SHans Petter Selasky 	opcode = cqe64->op_own >> 4;
54812515907SHans Petter Selasky 	if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
54912515907SHans Petter Selasky 		if (likely(cq->resize_buf)) {
55012515907SHans Petter Selasky 			free_cq_buf(dev, &cq->buf);
55112515907SHans Petter Selasky 			cq->buf = *cq->resize_buf;
55212515907SHans Petter Selasky 			kfree(cq->resize_buf);
55312515907SHans Petter Selasky 			cq->resize_buf = NULL;
55412515907SHans Petter Selasky 			goto repoll;
55512515907SHans Petter Selasky 		} else {
55612515907SHans Petter Selasky 			mlx5_ib_warn(dev, "unexpected resize cqe\n");
55712515907SHans Petter Selasky 		}
55812515907SHans Petter Selasky 	}
55912515907SHans Petter Selasky 
56012515907SHans Petter Selasky 	qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
56112515907SHans Petter Selasky 	if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
56212515907SHans Petter Selasky 		/* We do not have to take the QP table lock here,
56312515907SHans Petter Selasky 		 * because CQs will be locked while QPs are removed
56412515907SHans Petter Selasky 		 * from the table.
56512515907SHans Petter Selasky 		 */
56612515907SHans Petter Selasky 		mqp = __mlx5_qp_lookup(dev->mdev, qpn);
56712515907SHans Petter Selasky 		*cur_qp = to_mibqp(mqp);
56812515907SHans Petter Selasky 	}
56912515907SHans Petter Selasky 
57012515907SHans Petter Selasky 	wc->qp  = &(*cur_qp)->ibqp;
57112515907SHans Petter Selasky 	switch (opcode) {
57212515907SHans Petter Selasky 	case MLX5_CQE_REQ:
57312515907SHans Petter Selasky 		wq = &(*cur_qp)->sq;
57412515907SHans Petter Selasky 		wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
57512515907SHans Petter Selasky 		idx = wqe_ctr & (wq->wqe_cnt - 1);
57612515907SHans Petter Selasky 		handle_good_req(wc, cqe64, wq, idx);
57712515907SHans Petter Selasky 		handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
5788e6e287fSHans Petter Selasky 		wc->wr_id = wq->wrid[idx];
5798e6e287fSHans Petter Selasky 		wq->tail = wq->wqe_head[idx] + 1;
58012515907SHans Petter Selasky 		wc->status = IB_WC_SUCCESS;
58112515907SHans Petter Selasky 		break;
58212515907SHans Petter Selasky 	case MLX5_CQE_RESP_WR_IMM:
58312515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND:
58412515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND_IMM:
58512515907SHans Petter Selasky 	case MLX5_CQE_RESP_SEND_INV:
58612515907SHans Petter Selasky 		handle_responder(wc, cqe64, *cur_qp);
58712515907SHans Petter Selasky 		wc->status = IB_WC_SUCCESS;
58812515907SHans Petter Selasky 		break;
58912515907SHans Petter Selasky 	case MLX5_CQE_RESIZE_CQ:
59012515907SHans Petter Selasky 		break;
59112515907SHans Petter Selasky 	case MLX5_CQE_REQ_ERR:
59212515907SHans Petter Selasky 	case MLX5_CQE_RESP_ERR:
59312515907SHans Petter Selasky 		err_cqe = (struct mlx5_err_cqe *)cqe64;
59412515907SHans Petter Selasky 		mlx5_handle_error_cqe(dev, err_cqe, wc);
59512515907SHans Petter Selasky 		mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
59612515907SHans Petter Selasky 			    opcode == MLX5_CQE_REQ_ERR ?
59712515907SHans Petter Selasky 			    "Requestor" : "Responder", cq->mcq.cqn);
59812515907SHans Petter Selasky 		mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
59912515907SHans Petter Selasky 			    err_cqe->syndrome, err_cqe->vendor_err_synd);
60012515907SHans Petter Selasky 		if (opcode == MLX5_CQE_REQ_ERR) {
60112515907SHans Petter Selasky 			wq = &(*cur_qp)->sq;
60212515907SHans Petter Selasky 			wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
60312515907SHans Petter Selasky 			idx = wqe_ctr & (wq->wqe_cnt - 1);
6048e6e287fSHans Petter Selasky 			wc->wr_id = wq->wrid[idx];
6058e6e287fSHans Petter Selasky 			wq->tail = wq->wqe_head[idx] + 1;
60612515907SHans Petter Selasky 		} else {
60712515907SHans Petter Selasky 			struct mlx5_ib_srq *srq;
60812515907SHans Petter Selasky 
60912515907SHans Petter Selasky 			if ((*cur_qp)->ibqp.srq) {
61012515907SHans Petter Selasky 				srq = to_msrq((*cur_qp)->ibqp.srq);
61112515907SHans Petter Selasky 				wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
61212515907SHans Petter Selasky 				wc->wr_id = srq->wrid[wqe_ctr];
61312515907SHans Petter Selasky 				mlx5_ib_free_srq_wqe(srq, wqe_ctr);
61412515907SHans Petter Selasky 			} else {
61512515907SHans Petter Selasky 				wq = &(*cur_qp)->rq;
6168e6e287fSHans Petter Selasky 				wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
61712515907SHans Petter Selasky 				++wq->tail;
61812515907SHans Petter Selasky 			}
61912515907SHans Petter Selasky 		}
62012515907SHans Petter Selasky 		break;
62112515907SHans Petter Selasky 	case MLX5_CQE_SIG_ERR:
62212515907SHans Petter Selasky 		sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
62312515907SHans Petter Selasky 
62412515907SHans Petter Selasky 		spin_lock_irqsave(&dev->mdev->priv.mr_table.lock, flags);
6258e6e287fSHans Petter Selasky 		mmkey = __mlx5_mr_lookup(dev->mdev,
6268e6e287fSHans Petter Selasky 					 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
6278e6e287fSHans Petter Selasky 		mr = to_mibmr(mmkey);
6288e6e287fSHans Petter Selasky 		get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
62912515907SHans Petter Selasky 		mr->sig->sig_err_exists = true;
63012515907SHans Petter Selasky 		mr->sig->sigerr_count++;
63112515907SHans Petter Selasky 
6328e6e287fSHans Petter Selasky 		mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
6338e6e287fSHans Petter Selasky 			     cq->mcq.cqn, mr->sig->err_item.key,
6348e6e287fSHans Petter Selasky 			     mr->sig->err_item.err_type,
6358e6e287fSHans Petter Selasky 			     (long long)mr->sig->err_item.sig_err_offset,
6368e6e287fSHans Petter Selasky 			     mr->sig->err_item.expected,
6378e6e287fSHans Petter Selasky 			     mr->sig->err_item.actual);
63812515907SHans Petter Selasky 
63912515907SHans Petter Selasky 		spin_unlock_irqrestore(&dev->mdev->priv.mr_table.lock, flags);
64012515907SHans Petter Selasky 		goto repoll;
64112515907SHans Petter Selasky 	}
64212515907SHans Petter Selasky 
64312515907SHans Petter Selasky 	return 0;
64412515907SHans Petter Selasky }
64512515907SHans Petter Selasky 
poll_soft_wc(struct mlx5_ib_cq * cq,int num_entries,struct ib_wc * wc)6468e6e287fSHans Petter Selasky static int poll_soft_wc(struct mlx5_ib_cq *cq, int num_entries,
6478e6e287fSHans Petter Selasky 			struct ib_wc *wc)
6488e6e287fSHans Petter Selasky {
6498e6e287fSHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
6508e6e287fSHans Petter Selasky 	struct mlx5_ib_wc *soft_wc, *next;
6518e6e287fSHans Petter Selasky 	int npolled = 0;
6528e6e287fSHans Petter Selasky 
6538e6e287fSHans Petter Selasky 	list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
6548e6e287fSHans Petter Selasky 		if (npolled >= num_entries)
6558e6e287fSHans Petter Selasky 			break;
6568e6e287fSHans Petter Selasky 
6578e6e287fSHans Petter Selasky 		mlx5_ib_dbg(dev, "polled software generated completion on CQ 0x%x\n",
6588e6e287fSHans Petter Selasky 			    cq->mcq.cqn);
6598e6e287fSHans Petter Selasky 
6608e6e287fSHans Petter Selasky 		wc[npolled++] = soft_wc->wc;
6618e6e287fSHans Petter Selasky 		list_del(&soft_wc->list);
6628e6e287fSHans Petter Selasky 		kfree(soft_wc);
6638e6e287fSHans Petter Selasky 	}
6648e6e287fSHans Petter Selasky 
6658e6e287fSHans Petter Selasky 	return npolled;
6668e6e287fSHans Petter Selasky }
6678e6e287fSHans Petter Selasky 
mlx5_ib_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * wc)66812515907SHans Petter Selasky int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
66912515907SHans Petter Selasky {
67012515907SHans Petter Selasky 	struct mlx5_ib_cq *cq = to_mcq(ibcq);
67112515907SHans Petter Selasky 	struct mlx5_ib_qp *cur_qp = NULL;
67212515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
67312515907SHans Petter Selasky 	struct mlx5_core_dev *mdev = dev->mdev;
67412515907SHans Petter Selasky 	unsigned long flags;
6758e6e287fSHans Petter Selasky 	int soft_polled = 0;
67612515907SHans Petter Selasky 	int npolled;
67712515907SHans Petter Selasky 
67812515907SHans Petter Selasky 	spin_lock_irqsave(&cq->lock, flags);
6796428c27fSHans Petter Selasky 	if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)) {
68012515907SHans Petter Selasky 		mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
68112515907SHans Petter Selasky 		goto out;
68212515907SHans Petter Selasky 	}
68312515907SHans Petter Selasky 
6848e6e287fSHans Petter Selasky 	if (unlikely(!list_empty(&cq->wc_list)))
6858e6e287fSHans Petter Selasky 		soft_polled = poll_soft_wc(cq, num_entries, wc);
6868e6e287fSHans Petter Selasky 
6878e6e287fSHans Petter Selasky 	for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
6888e6e287fSHans Petter Selasky 		if (mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled))
68912515907SHans Petter Selasky 			break;
69012515907SHans Petter Selasky 	}
69112515907SHans Petter Selasky 
69212515907SHans Petter Selasky 	if (npolled)
69312515907SHans Petter Selasky 		mlx5_cq_set_ci(&cq->mcq);
69412515907SHans Petter Selasky out:
69512515907SHans Petter Selasky 	spin_unlock_irqrestore(&cq->lock, flags);
69612515907SHans Petter Selasky 
6978e6e287fSHans Petter Selasky 	return soft_polled + npolled;
69812515907SHans Petter Selasky }
69912515907SHans Petter Selasky 
mlx5_ib_arm_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)70012515907SHans Petter Selasky int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
70112515907SHans Petter Selasky {
70212515907SHans Petter Selasky 	struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
7038e6e287fSHans Petter Selasky 	struct mlx5_ib_cq *cq = to_mcq(ibcq);
704f8f5b459SHans Petter Selasky 	void __iomem *uar_page = mdev->priv.uar->map;
7058e6e287fSHans Petter Selasky 	unsigned long irq_flags;
7068e6e287fSHans Petter Selasky 	int ret = 0;
70712515907SHans Petter Selasky 
7086428c27fSHans Petter Selasky 	if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR))
7096428c27fSHans Petter Selasky 		return -1;
7106428c27fSHans Petter Selasky 
7118e6e287fSHans Petter Selasky 	spin_lock_irqsave(&cq->lock, irq_flags);
7128e6e287fSHans Petter Selasky 	if (cq->notify_flags != IB_CQ_NEXT_COMP)
7138e6e287fSHans Petter Selasky 		cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
71412515907SHans Petter Selasky 
7158e6e287fSHans Petter Selasky 	if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
7168e6e287fSHans Petter Selasky 		ret = 1;
7178e6e287fSHans Petter Selasky 	spin_unlock_irqrestore(&cq->lock, irq_flags);
7188e6e287fSHans Petter Selasky 
7198e6e287fSHans Petter Selasky 	mlx5_cq_arm(&cq->mcq,
72012515907SHans Petter Selasky 		    (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
72112515907SHans Petter Selasky 		    MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
72212515907SHans Petter Selasky 		    uar_page,
72312515907SHans Petter Selasky 		    MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
7246428c27fSHans Petter Selasky 		    cq->mcq.cons_index);
72512515907SHans Petter Selasky 
7268e6e287fSHans Petter Selasky 	return ret;
72712515907SHans Petter Selasky }
72812515907SHans Petter Selasky 
alloc_cq_buf(struct mlx5_ib_dev * dev,struct mlx5_ib_cq_buf * buf,int nent,int cqe_size)72912515907SHans Petter Selasky static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
73012515907SHans Petter Selasky 			int nent, int cqe_size)
73112515907SHans Petter Selasky {
73212515907SHans Petter Selasky 	int err;
73312515907SHans Petter Selasky 
73412515907SHans Petter Selasky 	err = mlx5_buf_alloc(dev->mdev, nent * cqe_size,
7358e6e287fSHans Petter Selasky 	    2 * PAGE_SIZE, &buf->buf);
7368e6e287fSHans Petter Selasky 	if (err)
73712515907SHans Petter Selasky 		return err;
73812515907SHans Petter Selasky 
73912515907SHans Petter Selasky 	buf->cqe_size = cqe_size;
74012515907SHans Petter Selasky 	buf->nent = nent;
74112515907SHans Petter Selasky 
74212515907SHans Petter Selasky 	return 0;
74312515907SHans Petter Selasky }
74412515907SHans Petter Selasky 
create_cq_user(struct mlx5_ib_dev * dev,struct ib_udata * udata,struct mlx5_ib_cq * cq,int entries,u32 ** cqb,int * cqe_size,int * index,int * inlen)74512515907SHans Petter Selasky static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
746b633e08cSHans Petter Selasky 			  struct mlx5_ib_cq *cq, int entries, u32 **cqb,
74712515907SHans Petter Selasky 			  int *cqe_size, int *index, int *inlen)
74812515907SHans Petter Selasky {
749b633e08cSHans Petter Selasky 	struct mlx5_ib_create_cq ucmd = {};
75012515907SHans Petter Selasky 	size_t ucmdlen;
75112515907SHans Petter Selasky 	int page_shift;
7528e6e287fSHans Petter Selasky 	__be64 *pas;
75312515907SHans Petter Selasky 	int npages;
75412515907SHans Petter Selasky 	int ncont;
7558e6e287fSHans Petter Selasky 	void *cqc;
75612515907SHans Petter Selasky 	int err;
757b633e08cSHans Petter Selasky 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
758b633e08cSHans Petter Selasky 		udata, struct mlx5_ib_ucontext, ibucontext);
75912515907SHans Petter Selasky 
760f8f5b459SHans Petter Selasky 	ucmdlen = min(udata->inlen, sizeof(ucmd));
761f8f5b459SHans Petter Selasky 	if (ucmdlen < offsetof(struct mlx5_ib_create_cq, flags))
762f8f5b459SHans Petter Selasky 		return -EINVAL;
76312515907SHans Petter Selasky 
7648e6e287fSHans Petter Selasky 	if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
76512515907SHans Petter Selasky 		return -EFAULT;
76612515907SHans Petter Selasky 
767f8f5b459SHans Petter Selasky 	if ((ucmd.flags & ~(MLX5_IB_CREATE_CQ_FLAGS_UAR_PAGE_INDEX)))
76812515907SHans Petter Selasky 		return -EINVAL;
76912515907SHans Petter Selasky 
7708e6e287fSHans Petter Selasky 	if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
77112515907SHans Petter Selasky 		return -EINVAL;
77212515907SHans Petter Selasky 
77312515907SHans Petter Selasky 	*cqe_size = ucmd.cqe_size;
77412515907SHans Petter Selasky 
775b633e08cSHans Petter Selasky 	cq->buf.umem = ib_umem_get(&context->ibucontext, ucmd.buf_addr,
77612515907SHans Petter Selasky 				   entries * ucmd.cqe_size,
77712515907SHans Petter Selasky 				   IB_ACCESS_LOCAL_WRITE, 1);
77812515907SHans Petter Selasky 	if (IS_ERR(cq->buf.umem)) {
77912515907SHans Petter Selasky 		err = PTR_ERR(cq->buf.umem);
78012515907SHans Petter Selasky 		return err;
78112515907SHans Petter Selasky 	}
78212515907SHans Petter Selasky 
783b633e08cSHans Petter Selasky 	err = mlx5_ib_db_map_user(context, ucmd.db_addr,
78412515907SHans Petter Selasky 				  &cq->db);
7858e6e287fSHans Petter Selasky 	if (err)
78612515907SHans Petter Selasky 		goto err_umem;
78712515907SHans Petter Selasky 
788565cb4e8SHans Petter Selasky 	mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, 0, &npages, &page_shift,
78912515907SHans Petter Selasky 			   &ncont, NULL);
79012515907SHans Petter Selasky 	mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
7918e6e287fSHans Petter Selasky 		    (long long)ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
79212515907SHans Petter Selasky 
7938e6e287fSHans Petter Selasky 	*inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
7948e6e287fSHans Petter Selasky 		 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
79512515907SHans Petter Selasky 	*cqb = mlx5_vzalloc(*inlen);
79612515907SHans Petter Selasky 	if (!*cqb) {
79712515907SHans Petter Selasky 		err = -ENOMEM;
79812515907SHans Petter Selasky 		goto err_db;
79912515907SHans Petter Selasky 	}
80012515907SHans Petter Selasky 
8018e6e287fSHans Petter Selasky 	pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
8028e6e287fSHans Petter Selasky 	mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
8038e6e287fSHans Petter Selasky 
8048e6e287fSHans Petter Selasky 	cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
8058e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, log_page_size,
8068e6e287fSHans Petter Selasky 		 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
80712515907SHans Petter Selasky 
808f8f5b459SHans Petter Selasky 	if (ucmd.flags & MLX5_IB_CREATE_CQ_FLAGS_UAR_PAGE_INDEX) {
809f8f5b459SHans Petter Selasky 		*index = ucmd.uar_page_index;
810b633e08cSHans Petter Selasky 	} else if (context->bfregi.lib_uar_dyn) {
811f8f5b459SHans Petter Selasky 		err = -EINVAL;
812f8f5b459SHans Petter Selasky 		goto err_cqb;
813f8f5b459SHans Petter Selasky 	} else {
814b633e08cSHans Petter Selasky 		*index = context->bfregi.sys_pages[0];
815f8f5b459SHans Petter Selasky 	}
81612515907SHans Petter Selasky 
817b633e08cSHans Petter Selasky 	MLX5_SET(create_cq_in, *cqb, uid, context->devx_uid);
81812515907SHans Petter Selasky 	return 0;
81912515907SHans Petter Selasky 
820f8f5b459SHans Petter Selasky err_cqb:
821f8f5b459SHans Petter Selasky 	kvfree(*cqb);
822f8f5b459SHans Petter Selasky 
82312515907SHans Petter Selasky err_db:
824b633e08cSHans Petter Selasky 	mlx5_ib_db_unmap_user(context, &cq->db);
82512515907SHans Petter Selasky 
82612515907SHans Petter Selasky err_umem:
82712515907SHans Petter Selasky 	ib_umem_release(cq->buf.umem);
82812515907SHans Petter Selasky 	return err;
82912515907SHans Petter Selasky }
83012515907SHans Petter Selasky 
destroy_cq_user(struct mlx5_ib_cq * cq,struct ib_udata * udata)831b633e08cSHans Petter Selasky static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_udata *udata)
83212515907SHans Petter Selasky {
833b633e08cSHans Petter Selasky 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
834b633e08cSHans Petter Selasky 		udata, struct mlx5_ib_ucontext, ibucontext);
835b633e08cSHans Petter Selasky 
836b633e08cSHans Petter Selasky 	mlx5_ib_db_unmap_user(context, &cq->db);
83712515907SHans Petter Selasky 	ib_umem_release(cq->buf.umem);
83812515907SHans Petter Selasky }
83912515907SHans Petter Selasky 
init_cq_buf(struct mlx5_ib_cq * cq,struct mlx5_ib_cq_buf * buf)84012515907SHans Petter Selasky static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
84112515907SHans Petter Selasky {
84212515907SHans Petter Selasky 	int i;
84312515907SHans Petter Selasky 	void *cqe;
84412515907SHans Petter Selasky 	struct mlx5_cqe64 *cqe64;
84512515907SHans Petter Selasky 
84612515907SHans Petter Selasky 	for (i = 0; i < buf->nent; i++) {
84712515907SHans Petter Selasky 		cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
84812515907SHans Petter Selasky 		cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
84912515907SHans Petter Selasky 		cqe64->op_own = MLX5_CQE_INVALID << 4;
85012515907SHans Petter Selasky 	}
85112515907SHans Petter Selasky }
85212515907SHans Petter Selasky 
create_cq_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_cq * cq,int entries,int cqe_size,u32 ** cqb,int * index,int * inlen)85312515907SHans Petter Selasky static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
85412515907SHans Petter Selasky 			    int entries, int cqe_size,
8558e6e287fSHans Petter Selasky 			    u32 **cqb, int *index, int *inlen)
85612515907SHans Petter Selasky {
8578e6e287fSHans Petter Selasky 	__be64 *pas;
8588e6e287fSHans Petter Selasky 	void *cqc;
85912515907SHans Petter Selasky 	int err;
86012515907SHans Petter Selasky 
86112515907SHans Petter Selasky 	err = mlx5_db_alloc(dev->mdev, &cq->db);
86212515907SHans Petter Selasky 	if (err)
86312515907SHans Petter Selasky 		return err;
86412515907SHans Petter Selasky 
86512515907SHans Petter Selasky 	cq->mcq.set_ci_db  = cq->db.db;
86612515907SHans Petter Selasky 	cq->mcq.arm_db     = cq->db.db + 1;
86712515907SHans Petter Selasky 	cq->mcq.cqe_sz = cqe_size;
86812515907SHans Petter Selasky 
86912515907SHans Petter Selasky 	err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
87012515907SHans Petter Selasky 	if (err)
87112515907SHans Petter Selasky 		goto err_db;
87212515907SHans Petter Selasky 
87312515907SHans Petter Selasky 	init_cq_buf(cq, &cq->buf);
87412515907SHans Petter Selasky 
8758e6e287fSHans Petter Selasky 	*inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
8768e6e287fSHans Petter Selasky 		 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
87712515907SHans Petter Selasky 	*cqb = mlx5_vzalloc(*inlen);
87812515907SHans Petter Selasky 	if (!*cqb) {
87912515907SHans Petter Selasky 		err = -ENOMEM;
88012515907SHans Petter Selasky 		goto err_buf;
88112515907SHans Petter Selasky 	}
88212515907SHans Petter Selasky 
8838e6e287fSHans Petter Selasky 	pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
8848e6e287fSHans Petter Selasky 	mlx5_fill_page_array(&cq->buf.buf, pas);
8858e6e287fSHans Petter Selasky 
8868e6e287fSHans Petter Selasky 	cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
8878e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, log_page_size,
8888e6e287fSHans Petter Selasky 		 cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
8898e6e287fSHans Petter Selasky 
890f8f5b459SHans Petter Selasky 	*index = dev->mdev->priv.uar->index;
89112515907SHans Petter Selasky 
89212515907SHans Petter Selasky 	return 0;
89312515907SHans Petter Selasky 
89412515907SHans Petter Selasky err_buf:
89512515907SHans Petter Selasky 	free_cq_buf(dev, &cq->buf);
89612515907SHans Petter Selasky 
89712515907SHans Petter Selasky err_db:
89812515907SHans Petter Selasky 	mlx5_db_free(dev->mdev, &cq->db);
89912515907SHans Petter Selasky 	return err;
90012515907SHans Petter Selasky }
90112515907SHans Petter Selasky 
destroy_cq_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_cq * cq)90212515907SHans Petter Selasky static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
90312515907SHans Petter Selasky {
90412515907SHans Petter Selasky 	free_cq_buf(dev, &cq->buf);
90512515907SHans Petter Selasky 	mlx5_db_free(dev->mdev, &cq->db);
90612515907SHans Petter Selasky }
90712515907SHans Petter Selasky 
notify_soft_wc_handler(struct work_struct * work)9088e6e287fSHans Petter Selasky static void notify_soft_wc_handler(struct work_struct *work)
9098e6e287fSHans Petter Selasky {
9108e6e287fSHans Petter Selasky 	struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
9118e6e287fSHans Petter Selasky 					     notify_work);
9128e6e287fSHans Petter Selasky 
9138e6e287fSHans Petter Selasky 	cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
9148e6e287fSHans Petter Selasky }
9158e6e287fSHans Petter Selasky 
mlx5_ib_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)916b633e08cSHans Petter Selasky int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
91712515907SHans Petter Selasky 		      struct ib_udata *udata)
91812515907SHans Petter Selasky {
919b633e08cSHans Petter Selasky 	struct ib_device *ibdev = ibcq->device;
92012515907SHans Petter Selasky 	int entries = attr->cqe;
92112515907SHans Petter Selasky 	int vector = attr->comp_vector;
9228e6e287fSHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
923ffdb195fSHans Petter Selasky 	u32 out[MLX5_ST_SZ_DW(create_cq_out)];
924b633e08cSHans Petter Selasky 	struct mlx5_ib_cq *cq = to_mcq(ibcq);
92512515907SHans Petter Selasky 	int uninitialized_var(index);
92612515907SHans Petter Selasky 	int uninitialized_var(inlen);
9278e6e287fSHans Petter Selasky 	u32 *cqb = NULL;
9288e6e287fSHans Petter Selasky 	void *cqc;
92912515907SHans Petter Selasky 	int cqe_size;
9308e6e287fSHans Petter Selasky 	unsigned int irqn;
93112515907SHans Petter Selasky 	int eqn;
93212515907SHans Petter Selasky 	int err;
93312515907SHans Petter Selasky 
9348e6e287fSHans Petter Selasky 	if (entries < 0 ||
9358e6e287fSHans Petter Selasky 	    (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
936b633e08cSHans Petter Selasky 		return -EINVAL;
9378e6e287fSHans Petter Selasky 
9388e6e287fSHans Petter Selasky 	if (check_cq_create_flags(attr->flags))
939b633e08cSHans Petter Selasky 		return -EOPNOTSUPP;
94012515907SHans Petter Selasky 
94112515907SHans Petter Selasky 	entries = roundup_pow_of_two(entries + 1);
9428e6e287fSHans Petter Selasky 	if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
943b633e08cSHans Petter Selasky 		return -EINVAL;
94412515907SHans Petter Selasky 
94512515907SHans Petter Selasky 	cq->ibcq.cqe = entries - 1;
94612515907SHans Petter Selasky 	mutex_init(&cq->resize_mutex);
94712515907SHans Petter Selasky 	spin_lock_init(&cq->lock);
94812515907SHans Petter Selasky 	cq->resize_buf = NULL;
94912515907SHans Petter Selasky 	cq->resize_umem = NULL;
9508e6e287fSHans Petter Selasky 	cq->create_flags = attr->flags;
95112515907SHans Petter Selasky 	INIT_LIST_HEAD(&cq->list_send_qp);
95212515907SHans Petter Selasky 	INIT_LIST_HEAD(&cq->list_recv_qp);
95312515907SHans Petter Selasky 
954b633e08cSHans Petter Selasky 	if (udata) {
955b633e08cSHans Petter Selasky 		err = create_cq_user(dev, udata, cq, entries, &cqb, &cqe_size,
956b633e08cSHans Petter Selasky 				     &index, &inlen);
95712515907SHans Petter Selasky 		if (err)
958b633e08cSHans Petter Selasky 			return err;
95912515907SHans Petter Selasky 	} else {
9608e6e287fSHans Petter Selasky 		cqe_size = cache_line_size() == 128 ? 128 : 64;
96112515907SHans Petter Selasky 		err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
96212515907SHans Petter Selasky 				       &index, &inlen);
96312515907SHans Petter Selasky 		if (err)
964b633e08cSHans Petter Selasky 			return err;
9658e6e287fSHans Petter Selasky 
9668e6e287fSHans Petter Selasky 		INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
96712515907SHans Petter Selasky 	}
96812515907SHans Petter Selasky 
96912515907SHans Petter Selasky 	err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
97012515907SHans Petter Selasky 	if (err)
97112515907SHans Petter Selasky 		goto err_cqb;
97212515907SHans Petter Selasky 
9738e6e287fSHans Petter Selasky 	cq->cqe_size = cqe_size;
97412515907SHans Petter Selasky 
9758e6e287fSHans Petter Selasky 	cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
9768e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
9778e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
9788e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, uar_page, index);
9798e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, c_eqn, eqn);
9808e6e287fSHans Petter Selasky 	MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
9818e6e287fSHans Petter Selasky 	if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
9828e6e287fSHans Petter Selasky 		MLX5_SET(cqc, cqc, oi, 1);
9838e6e287fSHans Petter Selasky 
984ffdb195fSHans Petter Selasky 	err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen, out, sizeof(out));
98512515907SHans Petter Selasky 	if (err)
98612515907SHans Petter Selasky 		goto err_cqb;
98712515907SHans Petter Selasky 
98812515907SHans Petter Selasky 	mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
98912515907SHans Petter Selasky 	cq->mcq.irqn = irqn;
99012515907SHans Petter Selasky 	cq->mcq.comp  = mlx5_ib_cq_comp;
99112515907SHans Petter Selasky 	cq->mcq.event = mlx5_ib_cq_event;
99212515907SHans Petter Selasky 
9938e6e287fSHans Petter Selasky 	INIT_LIST_HEAD(&cq->wc_list);
9948e6e287fSHans Petter Selasky 
995b633e08cSHans Petter Selasky 	if (udata)
99612515907SHans Petter Selasky 		if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
99712515907SHans Petter Selasky 			err = -EFAULT;
99812515907SHans Petter Selasky 			goto err_cmd;
99912515907SHans Petter Selasky 		}
100012515907SHans Petter Selasky 
100112515907SHans Petter Selasky 
100212515907SHans Petter Selasky 	kvfree(cqb);
1003b633e08cSHans Petter Selasky 	return 0;
100412515907SHans Petter Selasky 
100512515907SHans Petter Selasky err_cmd:
100612515907SHans Petter Selasky 	mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
100712515907SHans Petter Selasky 
100812515907SHans Petter Selasky err_cqb:
100912515907SHans Petter Selasky 	kvfree(cqb);
1010b633e08cSHans Petter Selasky 	if (udata)
1011b633e08cSHans Petter Selasky 		destroy_cq_user(cq, udata);
101212515907SHans Petter Selasky 	else
101312515907SHans Petter Selasky 		destroy_cq_kernel(dev, cq);
1014b633e08cSHans Petter Selasky 	return err;
101512515907SHans Petter Selasky }
101612515907SHans Petter Selasky 
mlx5_ib_destroy_cq(struct ib_cq * cq,struct ib_udata * udata)1017b633e08cSHans Petter Selasky void mlx5_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata)
101812515907SHans Petter Selasky {
101912515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->device);
102012515907SHans Petter Selasky 	struct mlx5_ib_cq *mcq = to_mcq(cq);
102112515907SHans Petter Selasky 
102212515907SHans Petter Selasky 	mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
1023b633e08cSHans Petter Selasky 	if (udata)
1024b633e08cSHans Petter Selasky 		destroy_cq_user(mcq, udata);
102512515907SHans Petter Selasky 	else
102612515907SHans Petter Selasky 		destroy_cq_kernel(dev, mcq);
102712515907SHans Petter Selasky }
102812515907SHans Petter Selasky 
is_equal_rsn(struct mlx5_cqe64 * cqe64,u32 rsn)102912515907SHans Petter Selasky static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
103012515907SHans Petter Selasky {
103112515907SHans Petter Selasky 	return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
103212515907SHans Petter Selasky }
103312515907SHans Petter Selasky 
__mlx5_ib_cq_clean(struct mlx5_ib_cq * cq,u32 rsn,struct mlx5_ib_srq * srq)103412515907SHans Petter Selasky void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
103512515907SHans Petter Selasky {
103612515907SHans Petter Selasky 	struct mlx5_cqe64 *cqe64, *dest64;
103712515907SHans Petter Selasky 	void *cqe, *dest;
103812515907SHans Petter Selasky 	u32 prod_index;
103912515907SHans Petter Selasky 	int nfreed = 0;
104012515907SHans Petter Selasky 	u8 owner_bit;
104112515907SHans Petter Selasky 
104212515907SHans Petter Selasky 	if (!cq)
104312515907SHans Petter Selasky 		return;
104412515907SHans Petter Selasky 
104512515907SHans Petter Selasky 	/* First we need to find the current producer index, so we
104612515907SHans Petter Selasky 	 * know where to start cleaning from.  It doesn't matter if HW
104712515907SHans Petter Selasky 	 * adds new entries after this loop -- the QP we're worried
104812515907SHans Petter Selasky 	 * about is already in RESET, so the new entries won't come
104912515907SHans Petter Selasky 	 * from our QP and therefore don't need to be checked.
105012515907SHans Petter Selasky 	 */
105112515907SHans Petter Selasky 	for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
105212515907SHans Petter Selasky 		if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
105312515907SHans Petter Selasky 			break;
105412515907SHans Petter Selasky 
105512515907SHans Petter Selasky 	/* Now sweep backwards through the CQ, removing CQ entries
105612515907SHans Petter Selasky 	 * that match our QP by copying older entries on top of them.
105712515907SHans Petter Selasky 	 */
105812515907SHans Petter Selasky 	while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
105912515907SHans Petter Selasky 		cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
106012515907SHans Petter Selasky 		cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
106112515907SHans Petter Selasky 		if (is_equal_rsn(cqe64, rsn)) {
106212515907SHans Petter Selasky 			if (srq && (ntohl(cqe64->srqn) & 0xffffff))
106312515907SHans Petter Selasky 				mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
106412515907SHans Petter Selasky 			++nfreed;
106512515907SHans Petter Selasky 		} else if (nfreed) {
106612515907SHans Petter Selasky 			dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
106712515907SHans Petter Selasky 			dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
106812515907SHans Petter Selasky 			owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
106912515907SHans Petter Selasky 			memcpy(dest, cqe, cq->mcq.cqe_sz);
107012515907SHans Petter Selasky 			dest64->op_own = owner_bit |
107112515907SHans Petter Selasky 				(dest64->op_own & ~MLX5_CQE_OWNER_MASK);
107212515907SHans Petter Selasky 		}
107312515907SHans Petter Selasky 	}
107412515907SHans Petter Selasky 
107512515907SHans Petter Selasky 	if (nfreed) {
107612515907SHans Petter Selasky 		cq->mcq.cons_index += nfreed;
107712515907SHans Petter Selasky 		/* Make sure update of buffer contents is done before
107812515907SHans Petter Selasky 		 * updating consumer index.
107912515907SHans Petter Selasky 		 */
108012515907SHans Petter Selasky 		wmb();
108112515907SHans Petter Selasky 		mlx5_cq_set_ci(&cq->mcq);
108212515907SHans Petter Selasky 	}
108312515907SHans Petter Selasky }
108412515907SHans Petter Selasky 
mlx5_ib_cq_clean(struct mlx5_ib_cq * cq,u32 qpn,struct mlx5_ib_srq * srq)108512515907SHans Petter Selasky void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
108612515907SHans Petter Selasky {
108712515907SHans Petter Selasky 	if (!cq)
108812515907SHans Petter Selasky 		return;
108912515907SHans Petter Selasky 
109012515907SHans Petter Selasky 	spin_lock_irq(&cq->lock);
109112515907SHans Petter Selasky 	__mlx5_ib_cq_clean(cq, qpn, srq);
109212515907SHans Petter Selasky 	spin_unlock_irq(&cq->lock);
109312515907SHans Petter Selasky }
109412515907SHans Petter Selasky 
mlx5_ib_modify_cq(struct ib_cq * cq,u16 cq_count,u16 cq_period)10958e6e287fSHans Petter Selasky int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
109612515907SHans Petter Selasky {
109712515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->device);
109812515907SHans Petter Selasky 	struct mlx5_ib_cq *mcq = to_mcq(cq);
109912515907SHans Petter Selasky 	int err;
110012515907SHans Petter Selasky 
11018e6e287fSHans Petter Selasky 	if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
11028e6e287fSHans Petter Selasky 		return -ENOSYS;
110312515907SHans Petter Selasky 
11048e6e287fSHans Petter Selasky 	err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
11058e6e287fSHans Petter Selasky 					     cq_period, cq_count);
110612515907SHans Petter Selasky 	if (err)
110712515907SHans Petter Selasky 		mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
110812515907SHans Petter Selasky 
110912515907SHans Petter Selasky 	return err;
111012515907SHans Petter Selasky }
111112515907SHans Petter Selasky 
resize_user(struct mlx5_ib_dev * dev,struct mlx5_ib_cq * cq,int entries,struct ib_udata * udata,int * npas,int * page_shift,int * cqe_size)111212515907SHans Petter Selasky static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
111312515907SHans Petter Selasky 		       int entries, struct ib_udata *udata, int *npas,
111412515907SHans Petter Selasky 		       int *page_shift, int *cqe_size)
111512515907SHans Petter Selasky {
111612515907SHans Petter Selasky 	struct mlx5_ib_resize_cq ucmd;
111712515907SHans Petter Selasky 	struct ib_umem *umem;
111812515907SHans Petter Selasky 	int err;
111912515907SHans Petter Selasky 	int npages;
112012515907SHans Petter Selasky 	struct ib_ucontext *context = cq->buf.umem->context;
112112515907SHans Petter Selasky 
112212515907SHans Petter Selasky 	err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
112312515907SHans Petter Selasky 	if (err)
112412515907SHans Petter Selasky 		return err;
112512515907SHans Petter Selasky 
112612515907SHans Petter Selasky 	if (ucmd.reserved0 || ucmd.reserved1)
112712515907SHans Petter Selasky 		return -EINVAL;
112812515907SHans Petter Selasky 
11292bf40c36SSlava Shwartsman 	/* check multiplication overflow */
11302bf40c36SSlava Shwartsman 	if (ucmd.cqe_size && SIZE_MAX / ucmd.cqe_size <= entries - 1)
11312bf40c36SSlava Shwartsman 		return -EINVAL;
11322bf40c36SSlava Shwartsman 
11332bf40c36SSlava Shwartsman 	umem = ib_umem_get(context, ucmd.buf_addr,
11342bf40c36SSlava Shwartsman 			   (size_t)ucmd.cqe_size * entries,
113512515907SHans Petter Selasky 			   IB_ACCESS_LOCAL_WRITE, 1);
113612515907SHans Petter Selasky 	if (IS_ERR(umem)) {
113712515907SHans Petter Selasky 		err = PTR_ERR(umem);
113812515907SHans Petter Selasky 		return err;
113912515907SHans Petter Selasky 	}
114012515907SHans Petter Selasky 
1141565cb4e8SHans Petter Selasky 	mlx5_ib_cont_pages(umem, ucmd.buf_addr, 0, &npages, page_shift,
114212515907SHans Petter Selasky 			   npas, NULL);
114312515907SHans Petter Selasky 
114412515907SHans Petter Selasky 	cq->resize_umem = umem;
114512515907SHans Petter Selasky 	*cqe_size = ucmd.cqe_size;
114612515907SHans Petter Selasky 
114712515907SHans Petter Selasky 	return 0;
114812515907SHans Petter Selasky }
114912515907SHans Petter Selasky 
un_resize_user(struct mlx5_ib_cq * cq)115012515907SHans Petter Selasky static void un_resize_user(struct mlx5_ib_cq *cq)
115112515907SHans Petter Selasky {
115212515907SHans Petter Selasky 	ib_umem_release(cq->resize_umem);
115312515907SHans Petter Selasky }
115412515907SHans Petter Selasky 
resize_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_cq * cq,int entries,int cqe_size)115512515907SHans Petter Selasky static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
115612515907SHans Petter Selasky 			 int entries, int cqe_size)
115712515907SHans Petter Selasky {
115812515907SHans Petter Selasky 	int err;
115912515907SHans Petter Selasky 
116012515907SHans Petter Selasky 	cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
116112515907SHans Petter Selasky 	if (!cq->resize_buf)
116212515907SHans Petter Selasky 		return -ENOMEM;
116312515907SHans Petter Selasky 
116412515907SHans Petter Selasky 	err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
116512515907SHans Petter Selasky 	if (err)
116612515907SHans Petter Selasky 		goto ex;
116712515907SHans Petter Selasky 
116812515907SHans Petter Selasky 	init_cq_buf(cq, cq->resize_buf);
116912515907SHans Petter Selasky 
117012515907SHans Petter Selasky 	return 0;
117112515907SHans Petter Selasky 
117212515907SHans Petter Selasky ex:
117312515907SHans Petter Selasky 	kfree(cq->resize_buf);
117412515907SHans Petter Selasky 	return err;
117512515907SHans Petter Selasky }
117612515907SHans Petter Selasky 
un_resize_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_cq * cq)117712515907SHans Petter Selasky static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
117812515907SHans Petter Selasky {
117912515907SHans Petter Selasky 	free_cq_buf(dev, cq->resize_buf);
118012515907SHans Petter Selasky 	cq->resize_buf = NULL;
118112515907SHans Petter Selasky }
118212515907SHans Petter Selasky 
copy_resize_cqes(struct mlx5_ib_cq * cq)118312515907SHans Petter Selasky static int copy_resize_cqes(struct mlx5_ib_cq *cq)
118412515907SHans Petter Selasky {
118512515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
118612515907SHans Petter Selasky 	struct mlx5_cqe64 *scqe64;
118712515907SHans Petter Selasky 	struct mlx5_cqe64 *dcqe64;
118812515907SHans Petter Selasky 	void *start_cqe;
118912515907SHans Petter Selasky 	void *scqe;
119012515907SHans Petter Selasky 	void *dcqe;
119112515907SHans Petter Selasky 	int ssize;
119212515907SHans Petter Selasky 	int dsize;
119312515907SHans Petter Selasky 	int i;
119412515907SHans Petter Selasky 	u8 sw_own;
119512515907SHans Petter Selasky 
119612515907SHans Petter Selasky 	ssize = cq->buf.cqe_size;
119712515907SHans Petter Selasky 	dsize = cq->resize_buf->cqe_size;
119812515907SHans Petter Selasky 	if (ssize != dsize) {
119912515907SHans Petter Selasky 		mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
120012515907SHans Petter Selasky 		return -EINVAL;
120112515907SHans Petter Selasky 	}
120212515907SHans Petter Selasky 
120312515907SHans Petter Selasky 	i = cq->mcq.cons_index;
120412515907SHans Petter Selasky 	scqe = get_sw_cqe(cq, i);
120512515907SHans Petter Selasky 	scqe64 = ssize == 64 ? scqe : scqe + 64;
120612515907SHans Petter Selasky 	start_cqe = scqe;
120712515907SHans Petter Selasky 	if (!scqe) {
120812515907SHans Petter Selasky 		mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
120912515907SHans Petter Selasky 		return -EINVAL;
121012515907SHans Petter Selasky 	}
121112515907SHans Petter Selasky 
121212515907SHans Petter Selasky 	while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
121312515907SHans Petter Selasky 		dcqe = get_cqe_from_buf(cq->resize_buf,
121412515907SHans Petter Selasky 					(i + 1) & (cq->resize_buf->nent),
121512515907SHans Petter Selasky 					dsize);
121612515907SHans Petter Selasky 		dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
121712515907SHans Petter Selasky 		sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
121812515907SHans Petter Selasky 		memcpy(dcqe, scqe, dsize);
121912515907SHans Petter Selasky 		dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
122012515907SHans Petter Selasky 
122112515907SHans Petter Selasky 		++i;
122212515907SHans Petter Selasky 		scqe = get_sw_cqe(cq, i);
122312515907SHans Petter Selasky 		scqe64 = ssize == 64 ? scqe : scqe + 64;
122412515907SHans Petter Selasky 		if (!scqe) {
122512515907SHans Petter Selasky 			mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
122612515907SHans Petter Selasky 			return -EINVAL;
122712515907SHans Petter Selasky 		}
122812515907SHans Petter Selasky 
122912515907SHans Petter Selasky 		if (scqe == start_cqe) {
12308e6e287fSHans Petter Selasky 			pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
12318e6e287fSHans Petter Selasky 				cq->mcq.cqn);
123212515907SHans Petter Selasky 			return -ENOMEM;
123312515907SHans Petter Selasky 		}
123412515907SHans Petter Selasky 	}
123512515907SHans Petter Selasky 	++cq->mcq.cons_index;
123612515907SHans Petter Selasky 	return 0;
123712515907SHans Petter Selasky }
123812515907SHans Petter Selasky 
mlx5_ib_resize_cq(struct ib_cq * ibcq,int entries,struct ib_udata * udata)123912515907SHans Petter Selasky int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
124012515907SHans Petter Selasky {
124112515907SHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
124212515907SHans Petter Selasky 	struct mlx5_ib_cq *cq = to_mcq(ibcq);
12438e6e287fSHans Petter Selasky 	void *cqc;
12448e6e287fSHans Petter Selasky 	u32 *in;
124512515907SHans Petter Selasky 	int err;
124612515907SHans Petter Selasky 	int npas;
12478e6e287fSHans Petter Selasky 	__be64 *pas;
124812515907SHans Petter Selasky 	int page_shift;
124912515907SHans Petter Selasky 	int inlen;
125012515907SHans Petter Selasky 	int uninitialized_var(cqe_size);
125112515907SHans Petter Selasky 	unsigned long flags;
125212515907SHans Petter Selasky 
125312515907SHans Petter Selasky 	if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
12548e6e287fSHans Petter Selasky 		pr_info("Firmware does not support resize CQ\n");
125512515907SHans Petter Selasky 		return -ENOSYS;
125612515907SHans Petter Selasky 	}
125712515907SHans Petter Selasky 
12588e6e287fSHans Petter Selasky 	if (entries < 1 ||
12598e6e287fSHans Petter Selasky 	    entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
12608e6e287fSHans Petter Selasky 		mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
12618e6e287fSHans Petter Selasky 			     entries,
126212515907SHans Petter Selasky 			     1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
126312515907SHans Petter Selasky 		return -EINVAL;
126412515907SHans Petter Selasky 	}
126512515907SHans Petter Selasky 
126612515907SHans Petter Selasky 	entries = roundup_pow_of_two(entries + 1);
12678e6e287fSHans Petter Selasky 	if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
12688e6e287fSHans Petter Selasky 		return -EINVAL;
126912515907SHans Petter Selasky 
127012515907SHans Petter Selasky 	if (entries == ibcq->cqe + 1)
127112515907SHans Petter Selasky 		return 0;
127212515907SHans Petter Selasky 
127312515907SHans Petter Selasky 	mutex_lock(&cq->resize_mutex);
127412515907SHans Petter Selasky 	if (udata) {
127512515907SHans Petter Selasky 		err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
127612515907SHans Petter Selasky 				  &cqe_size);
127712515907SHans Petter Selasky 	} else {
127812515907SHans Petter Selasky 		cqe_size = 64;
127912515907SHans Petter Selasky 		err = resize_kernel(dev, cq, entries, cqe_size);
128012515907SHans Petter Selasky 		if (!err) {
128112515907SHans Petter Selasky 			npas = cq->resize_buf->buf.npages;
128212515907SHans Petter Selasky 			page_shift = cq->resize_buf->buf.page_shift;
128312515907SHans Petter Selasky 		}
128412515907SHans Petter Selasky 	}
128512515907SHans Petter Selasky 
12868e6e287fSHans Petter Selasky 	if (err)
128712515907SHans Petter Selasky 		goto ex;
128812515907SHans Petter Selasky 
12898e6e287fSHans Petter Selasky 	inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
12908e6e287fSHans Petter Selasky 		MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
12918e6e287fSHans Petter Selasky 
129212515907SHans Petter Selasky 	in = mlx5_vzalloc(inlen);
129312515907SHans Petter Selasky 	if (!in) {
129412515907SHans Petter Selasky 		err = -ENOMEM;
129512515907SHans Petter Selasky 		goto ex_resize;
129612515907SHans Petter Selasky 	}
129712515907SHans Petter Selasky 
12988e6e287fSHans Petter Selasky 	pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
129912515907SHans Petter Selasky 	if (udata)
130012515907SHans Petter Selasky 		mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
13018e6e287fSHans Petter Selasky 				     pas, 0);
130212515907SHans Petter Selasky 	else
13038e6e287fSHans Petter Selasky 		mlx5_fill_page_array(&cq->resize_buf->buf, pas);
130412515907SHans Petter Selasky 
13058e6e287fSHans Petter Selasky 	MLX5_SET(modify_cq_in, in,
13068e6e287fSHans Petter Selasky 		 modify_field_select_resize_field_select.resize_field_select.resize_field_select,
13078e6e287fSHans Petter Selasky 		 MLX5_MODIFY_CQ_MASK_LOG_SIZE  |
130812515907SHans Petter Selasky 		 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
130912515907SHans Petter Selasky 		 MLX5_MODIFY_CQ_MASK_PG_SIZE);
131012515907SHans Petter Selasky 
13118e6e287fSHans Petter Selasky 	cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
13128e6e287fSHans Petter Selasky 
13138e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, log_page_size,
13148e6e287fSHans Petter Selasky 		 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
13158e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
13168e6e287fSHans Petter Selasky 	MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
13178e6e287fSHans Petter Selasky 
13188e6e287fSHans Petter Selasky 	MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
13198e6e287fSHans Petter Selasky 	MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
13208e6e287fSHans Petter Selasky 
1321788333d9SHans Petter Selasky 	err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
13228e6e287fSHans Petter Selasky 	if (err)
132312515907SHans Petter Selasky 		goto ex_alloc;
132412515907SHans Petter Selasky 
132512515907SHans Petter Selasky 	if (udata) {
132612515907SHans Petter Selasky 		cq->ibcq.cqe = entries - 1;
132712515907SHans Petter Selasky 		ib_umem_release(cq->buf.umem);
132812515907SHans Petter Selasky 		cq->buf.umem = cq->resize_umem;
132912515907SHans Petter Selasky 		cq->resize_umem = NULL;
133012515907SHans Petter Selasky 	} else {
133112515907SHans Petter Selasky 		struct mlx5_ib_cq_buf tbuf;
133212515907SHans Petter Selasky 		int resized = 0;
133312515907SHans Petter Selasky 
133412515907SHans Petter Selasky 		spin_lock_irqsave(&cq->lock, flags);
133512515907SHans Petter Selasky 		if (cq->resize_buf) {
133612515907SHans Petter Selasky 			err = copy_resize_cqes(cq);
133712515907SHans Petter Selasky 			if (!err) {
133812515907SHans Petter Selasky 				tbuf = cq->buf;
133912515907SHans Petter Selasky 				cq->buf = *cq->resize_buf;
134012515907SHans Petter Selasky 				kfree(cq->resize_buf);
134112515907SHans Petter Selasky 				cq->resize_buf = NULL;
134212515907SHans Petter Selasky 				resized = 1;
134312515907SHans Petter Selasky 			}
134412515907SHans Petter Selasky 		}
134512515907SHans Petter Selasky 		cq->ibcq.cqe = entries - 1;
134612515907SHans Petter Selasky 		spin_unlock_irqrestore(&cq->lock, flags);
134712515907SHans Petter Selasky 		if (resized)
134812515907SHans Petter Selasky 			free_cq_buf(dev, &tbuf);
134912515907SHans Petter Selasky 	}
135012515907SHans Petter Selasky 	mutex_unlock(&cq->resize_mutex);
135112515907SHans Petter Selasky 
135212515907SHans Petter Selasky 	kvfree(in);
135312515907SHans Petter Selasky 	return 0;
135412515907SHans Petter Selasky 
135512515907SHans Petter Selasky ex_alloc:
135612515907SHans Petter Selasky 	kvfree(in);
135712515907SHans Petter Selasky 
135812515907SHans Petter Selasky ex_resize:
135912515907SHans Petter Selasky 	if (udata)
136012515907SHans Petter Selasky 		un_resize_user(cq);
136112515907SHans Petter Selasky 	else
136212515907SHans Petter Selasky 		un_resize_kernel(dev, cq);
136312515907SHans Petter Selasky ex:
136412515907SHans Petter Selasky 	mutex_unlock(&cq->resize_mutex);
136512515907SHans Petter Selasky 	return err;
136612515907SHans Petter Selasky }
136712515907SHans Petter Selasky 
mlx5_ib_get_cqe_size(struct mlx5_ib_dev * dev,struct ib_cq * ibcq)136812515907SHans Petter Selasky int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
136912515907SHans Petter Selasky {
137012515907SHans Petter Selasky 	struct mlx5_ib_cq *cq;
137112515907SHans Petter Selasky 
137212515907SHans Petter Selasky 	if (!ibcq)
137312515907SHans Petter Selasky 		return 128;
137412515907SHans Petter Selasky 
137512515907SHans Petter Selasky 	cq = to_mcq(ibcq);
137612515907SHans Petter Selasky 	return cq->cqe_size;
137712515907SHans Petter Selasky }
13788e6e287fSHans Petter Selasky 
13798e6e287fSHans Petter Selasky /* Called from atomic context */
mlx5_ib_generate_wc(struct ib_cq * ibcq,struct ib_wc * wc)13808e6e287fSHans Petter Selasky int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
13818e6e287fSHans Petter Selasky {
13828e6e287fSHans Petter Selasky 	struct mlx5_ib_wc *soft_wc;
13838e6e287fSHans Petter Selasky 	struct mlx5_ib_cq *cq = to_mcq(ibcq);
13848e6e287fSHans Petter Selasky 	unsigned long flags;
13858e6e287fSHans Petter Selasky 
13868e6e287fSHans Petter Selasky 	soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
13878e6e287fSHans Petter Selasky 	if (!soft_wc)
13888e6e287fSHans Petter Selasky 		return -ENOMEM;
13898e6e287fSHans Petter Selasky 
13908e6e287fSHans Petter Selasky 	soft_wc->wc = *wc;
13918e6e287fSHans Petter Selasky 	spin_lock_irqsave(&cq->lock, flags);
13928e6e287fSHans Petter Selasky 	list_add_tail(&soft_wc->list, &cq->wc_list);
13938e6e287fSHans Petter Selasky 	if (cq->notify_flags == IB_CQ_NEXT_COMP ||
13948e6e287fSHans Petter Selasky 	    wc->status != IB_WC_SUCCESS) {
13958e6e287fSHans Petter Selasky 		cq->notify_flags = 0;
13968e6e287fSHans Petter Selasky 		schedule_work(&cq->notify_work);
13978e6e287fSHans Petter Selasky 	}
13988e6e287fSHans Petter Selasky 	spin_unlock_irqrestore(&cq->lock, flags);
13998e6e287fSHans Petter Selasky 
14008e6e287fSHans Petter Selasky 	return 0;
14018e6e287fSHans Petter Selasky }
1402