18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
48c2ecf20Sopenharmony_ci/* Copyright (c) 2008-2019, IBM Corporation */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/errno.h>
78c2ecf20Sopenharmony_ci#include <linux/types.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <rdma/ib_verbs.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "siw.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic int map_wc_opcode[SIW_NUM_OPCODES] = {
148c2ecf20Sopenharmony_ci	[SIW_OP_WRITE] = IB_WC_RDMA_WRITE,
158c2ecf20Sopenharmony_ci	[SIW_OP_SEND] = IB_WC_SEND,
168c2ecf20Sopenharmony_ci	[SIW_OP_SEND_WITH_IMM] = IB_WC_SEND,
178c2ecf20Sopenharmony_ci	[SIW_OP_READ] = IB_WC_RDMA_READ,
188c2ecf20Sopenharmony_ci	[SIW_OP_READ_LOCAL_INV] = IB_WC_RDMA_READ,
198c2ecf20Sopenharmony_ci	[SIW_OP_COMP_AND_SWAP] = IB_WC_COMP_SWAP,
208c2ecf20Sopenharmony_ci	[SIW_OP_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
218c2ecf20Sopenharmony_ci	[SIW_OP_INVAL_STAG] = IB_WC_LOCAL_INV,
228c2ecf20Sopenharmony_ci	[SIW_OP_REG_MR] = IB_WC_REG_MR,
238c2ecf20Sopenharmony_ci	[SIW_OP_RECEIVE] = IB_WC_RECV,
248c2ecf20Sopenharmony_ci	[SIW_OP_READ_RESPONSE] = -1 /* not used */
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct {
288c2ecf20Sopenharmony_ci	enum siw_wc_status siw;
298c2ecf20Sopenharmony_ci	enum ib_wc_status ib;
308c2ecf20Sopenharmony_ci} map_cqe_status[SIW_NUM_WC_STATUS] = {
318c2ecf20Sopenharmony_ci	{ SIW_WC_SUCCESS, IB_WC_SUCCESS },
328c2ecf20Sopenharmony_ci	{ SIW_WC_LOC_LEN_ERR, IB_WC_LOC_LEN_ERR },
338c2ecf20Sopenharmony_ci	{ SIW_WC_LOC_PROT_ERR, IB_WC_LOC_PROT_ERR },
348c2ecf20Sopenharmony_ci	{ SIW_WC_LOC_QP_OP_ERR, IB_WC_LOC_QP_OP_ERR },
358c2ecf20Sopenharmony_ci	{ SIW_WC_WR_FLUSH_ERR, IB_WC_WR_FLUSH_ERR },
368c2ecf20Sopenharmony_ci	{ SIW_WC_BAD_RESP_ERR, IB_WC_BAD_RESP_ERR },
378c2ecf20Sopenharmony_ci	{ SIW_WC_LOC_ACCESS_ERR, IB_WC_LOC_ACCESS_ERR },
388c2ecf20Sopenharmony_ci	{ SIW_WC_REM_ACCESS_ERR, IB_WC_REM_ACCESS_ERR },
398c2ecf20Sopenharmony_ci	{ SIW_WC_REM_INV_REQ_ERR, IB_WC_REM_INV_REQ_ERR },
408c2ecf20Sopenharmony_ci	{ SIW_WC_GENERAL_ERR, IB_WC_GENERAL_ERR }
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * Reap one CQE from the CQ. Only used by kernel clients
458c2ecf20Sopenharmony_ci * during CQ normal operation. Might be called during CQ
468c2ecf20Sopenharmony_ci * flush for user mapped CQE array as well.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ciint siw_reap_cqe(struct siw_cq *cq, struct ib_wc *wc)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct siw_cqe *cqe;
518c2ecf20Sopenharmony_ci	unsigned long flags;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cq->lock, flags);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	cqe = &cq->queue[cq->cq_get % cq->num_cqe];
568c2ecf20Sopenharmony_ci	if (READ_ONCE(cqe->flags) & SIW_WQE_VALID) {
578c2ecf20Sopenharmony_ci		memset(wc, 0, sizeof(*wc));
588c2ecf20Sopenharmony_ci		wc->wr_id = cqe->id;
598c2ecf20Sopenharmony_ci		wc->byte_len = cqe->bytes;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci		/*
628c2ecf20Sopenharmony_ci		 * During CQ flush, also user land CQE's may get
638c2ecf20Sopenharmony_ci		 * reaped here, which do not hold a QP reference
648c2ecf20Sopenharmony_ci		 * and do not qualify for memory extension verbs.
658c2ecf20Sopenharmony_ci		 */
668c2ecf20Sopenharmony_ci		if (likely(rdma_is_kernel_res(&cq->base_cq.res))) {
678c2ecf20Sopenharmony_ci			if (cqe->flags & SIW_WQE_REM_INVAL) {
688c2ecf20Sopenharmony_ci				wc->ex.invalidate_rkey = cqe->inval_stag;
698c2ecf20Sopenharmony_ci				wc->wc_flags = IB_WC_WITH_INVALIDATE;
708c2ecf20Sopenharmony_ci			}
718c2ecf20Sopenharmony_ci			wc->qp = cqe->base_qp;
728c2ecf20Sopenharmony_ci			wc->opcode = map_wc_opcode[cqe->opcode];
738c2ecf20Sopenharmony_ci			wc->status = map_cqe_status[cqe->status].ib;
748c2ecf20Sopenharmony_ci			siw_dbg_cq(cq,
758c2ecf20Sopenharmony_ci				   "idx %u, type %d, flags %2x, id 0x%pK\n",
768c2ecf20Sopenharmony_ci				   cq->cq_get % cq->num_cqe, cqe->opcode,
778c2ecf20Sopenharmony_ci				   cqe->flags, (void *)(uintptr_t)cqe->id);
788c2ecf20Sopenharmony_ci		} else {
798c2ecf20Sopenharmony_ci			/*
808c2ecf20Sopenharmony_ci			 * A malicious user may set invalid opcode or
818c2ecf20Sopenharmony_ci			 * status in the user mmapped CQE array.
828c2ecf20Sopenharmony_ci			 * Sanity check and correct values in that case
838c2ecf20Sopenharmony_ci			 * to avoid out-of-bounds access to global arrays
848c2ecf20Sopenharmony_ci			 * for opcode and status mapping.
858c2ecf20Sopenharmony_ci			 */
868c2ecf20Sopenharmony_ci			u8 opcode = cqe->opcode;
878c2ecf20Sopenharmony_ci			u16 status = cqe->status;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci			if (opcode >= SIW_NUM_OPCODES) {
908c2ecf20Sopenharmony_ci				opcode = 0;
918c2ecf20Sopenharmony_ci				status = SIW_WC_GENERAL_ERR;
928c2ecf20Sopenharmony_ci			} else if (status >= SIW_NUM_WC_STATUS) {
938c2ecf20Sopenharmony_ci				status = SIW_WC_GENERAL_ERR;
948c2ecf20Sopenharmony_ci			}
958c2ecf20Sopenharmony_ci			wc->opcode = map_wc_opcode[opcode];
968c2ecf20Sopenharmony_ci			wc->status = map_cqe_status[status].ib;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		WRITE_ONCE(cqe->flags, 0);
1008c2ecf20Sopenharmony_ci		cq->cq_get++;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&cq->lock, flags);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		return 1;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cq->lock, flags);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * siw_cq_flush()
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * Flush all CQ elements.
1158c2ecf20Sopenharmony_ci */
1168c2ecf20Sopenharmony_civoid siw_cq_flush(struct siw_cq *cq)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct ib_wc wc;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	while (siw_reap_cqe(cq, &wc))
1218c2ecf20Sopenharmony_ci		;
1228c2ecf20Sopenharmony_ci}
123