162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
462306a36Sopenharmony_ci/* Copyright (c) 2008-2019, IBM Corporation */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/errno.h>
762306a36Sopenharmony_ci#include <linux/types.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <rdma/ib_verbs.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "siw.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistatic int map_wc_opcode[SIW_NUM_OPCODES] = {
1462306a36Sopenharmony_ci	[SIW_OP_WRITE] = IB_WC_RDMA_WRITE,
1562306a36Sopenharmony_ci	[SIW_OP_SEND] = IB_WC_SEND,
1662306a36Sopenharmony_ci	[SIW_OP_SEND_WITH_IMM] = IB_WC_SEND,
1762306a36Sopenharmony_ci	[SIW_OP_READ] = IB_WC_RDMA_READ,
1862306a36Sopenharmony_ci	[SIW_OP_READ_LOCAL_INV] = IB_WC_RDMA_READ,
1962306a36Sopenharmony_ci	[SIW_OP_COMP_AND_SWAP] = IB_WC_COMP_SWAP,
2062306a36Sopenharmony_ci	[SIW_OP_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
2162306a36Sopenharmony_ci	[SIW_OP_INVAL_STAG] = IB_WC_LOCAL_INV,
2262306a36Sopenharmony_ci	[SIW_OP_REG_MR] = IB_WC_REG_MR,
2362306a36Sopenharmony_ci	[SIW_OP_RECEIVE] = IB_WC_RECV,
2462306a36Sopenharmony_ci	[SIW_OP_READ_RESPONSE] = -1 /* not used */
2562306a36Sopenharmony_ci};
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic struct {
2862306a36Sopenharmony_ci	enum siw_wc_status siw;
2962306a36Sopenharmony_ci	enum ib_wc_status ib;
3062306a36Sopenharmony_ci} map_cqe_status[SIW_NUM_WC_STATUS] = {
3162306a36Sopenharmony_ci	{ SIW_WC_SUCCESS, IB_WC_SUCCESS },
3262306a36Sopenharmony_ci	{ SIW_WC_LOC_LEN_ERR, IB_WC_LOC_LEN_ERR },
3362306a36Sopenharmony_ci	{ SIW_WC_LOC_PROT_ERR, IB_WC_LOC_PROT_ERR },
3462306a36Sopenharmony_ci	{ SIW_WC_LOC_QP_OP_ERR, IB_WC_LOC_QP_OP_ERR },
3562306a36Sopenharmony_ci	{ SIW_WC_WR_FLUSH_ERR, IB_WC_WR_FLUSH_ERR },
3662306a36Sopenharmony_ci	{ SIW_WC_BAD_RESP_ERR, IB_WC_BAD_RESP_ERR },
3762306a36Sopenharmony_ci	{ SIW_WC_LOC_ACCESS_ERR, IB_WC_LOC_ACCESS_ERR },
3862306a36Sopenharmony_ci	{ SIW_WC_REM_ACCESS_ERR, IB_WC_REM_ACCESS_ERR },
3962306a36Sopenharmony_ci	{ SIW_WC_REM_INV_REQ_ERR, IB_WC_REM_INV_REQ_ERR },
4062306a36Sopenharmony_ci	{ SIW_WC_GENERAL_ERR, IB_WC_GENERAL_ERR }
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * Reap one CQE from the CQ. Only used by kernel clients
4562306a36Sopenharmony_ci * during CQ normal operation. Might be called during CQ
4662306a36Sopenharmony_ci * flush for user mapped CQE array as well.
4762306a36Sopenharmony_ci */
4862306a36Sopenharmony_ciint siw_reap_cqe(struct siw_cq *cq, struct ib_wc *wc)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	struct siw_cqe *cqe;
5162306a36Sopenharmony_ci	unsigned long flags;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	spin_lock_irqsave(&cq->lock, flags);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	cqe = &cq->queue[cq->cq_get % cq->num_cqe];
5662306a36Sopenharmony_ci	if (READ_ONCE(cqe->flags) & SIW_WQE_VALID) {
5762306a36Sopenharmony_ci		memset(wc, 0, sizeof(*wc));
5862306a36Sopenharmony_ci		wc->wr_id = cqe->id;
5962306a36Sopenharmony_ci		wc->byte_len = cqe->bytes;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci		/*
6262306a36Sopenharmony_ci		 * During CQ flush, also user land CQE's may get
6362306a36Sopenharmony_ci		 * reaped here, which do not hold a QP reference
6462306a36Sopenharmony_ci		 * and do not qualify for memory extension verbs.
6562306a36Sopenharmony_ci		 */
6662306a36Sopenharmony_ci		if (likely(rdma_is_kernel_res(&cq->base_cq.res))) {
6762306a36Sopenharmony_ci			if (cqe->flags & SIW_WQE_REM_INVAL) {
6862306a36Sopenharmony_ci				wc->ex.invalidate_rkey = cqe->inval_stag;
6962306a36Sopenharmony_ci				wc->wc_flags = IB_WC_WITH_INVALIDATE;
7062306a36Sopenharmony_ci			}
7162306a36Sopenharmony_ci			wc->qp = cqe->base_qp;
7262306a36Sopenharmony_ci			wc->opcode = map_wc_opcode[cqe->opcode];
7362306a36Sopenharmony_ci			wc->status = map_cqe_status[cqe->status].ib;
7462306a36Sopenharmony_ci			siw_dbg_cq(cq,
7562306a36Sopenharmony_ci				   "idx %u, type %d, flags %2x, id 0x%pK\n",
7662306a36Sopenharmony_ci				   cq->cq_get % cq->num_cqe, cqe->opcode,
7762306a36Sopenharmony_ci				   cqe->flags, (void *)(uintptr_t)cqe->id);
7862306a36Sopenharmony_ci		} else {
7962306a36Sopenharmony_ci			/*
8062306a36Sopenharmony_ci			 * A malicious user may set invalid opcode or
8162306a36Sopenharmony_ci			 * status in the user mmapped CQE array.
8262306a36Sopenharmony_ci			 * Sanity check and correct values in that case
8362306a36Sopenharmony_ci			 * to avoid out-of-bounds access to global arrays
8462306a36Sopenharmony_ci			 * for opcode and status mapping.
8562306a36Sopenharmony_ci			 */
8662306a36Sopenharmony_ci			u8 opcode = cqe->opcode;
8762306a36Sopenharmony_ci			u16 status = cqe->status;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci			if (opcode >= SIW_NUM_OPCODES) {
9062306a36Sopenharmony_ci				opcode = 0;
9162306a36Sopenharmony_ci				status = SIW_WC_GENERAL_ERR;
9262306a36Sopenharmony_ci			} else if (status >= SIW_NUM_WC_STATUS) {
9362306a36Sopenharmony_ci				status = SIW_WC_GENERAL_ERR;
9462306a36Sopenharmony_ci			}
9562306a36Sopenharmony_ci			wc->opcode = map_wc_opcode[opcode];
9662306a36Sopenharmony_ci			wc->status = map_cqe_status[status].ib;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci		}
9962306a36Sopenharmony_ci		WRITE_ONCE(cqe->flags, 0);
10062306a36Sopenharmony_ci		cq->cq_get++;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci		spin_unlock_irqrestore(&cq->lock, flags);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci		return 1;
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci	spin_unlock_irqrestore(&cq->lock, flags);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	return 0;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/*
11262306a36Sopenharmony_ci * siw_cq_flush()
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * Flush all CQ elements.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_civoid siw_cq_flush(struct siw_cq *cq)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	struct ib_wc wc;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	while (siw_reap_cqe(cq, &wc))
12162306a36Sopenharmony_ci		;
12262306a36Sopenharmony_ci}
123