162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
262306a36Sopenharmony_ci/* Copyright (c) 2016 - 2021 Intel Corporation */
362306a36Sopenharmony_ci#include <linux/etherdevice.h>
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include "osdep.h"
662306a36Sopenharmony_ci#include "hmc.h"
762306a36Sopenharmony_ci#include "defs.h"
862306a36Sopenharmony_ci#include "type.h"
962306a36Sopenharmony_ci#include "protos.h"
1062306a36Sopenharmony_ci#include "uda.h"
1162306a36Sopenharmony_ci#include "uda_d.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/**
1462306a36Sopenharmony_ci * irdma_sc_access_ah() - Create, modify or delete AH
1562306a36Sopenharmony_ci * @cqp: struct for cqp hw
1662306a36Sopenharmony_ci * @info: ah information
1762306a36Sopenharmony_ci * @op: Operation
1862306a36Sopenharmony_ci * @scratch: u64 saved to be used during cqp completion
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ciint irdma_sc_access_ah(struct irdma_sc_cqp *cqp, struct irdma_ah_info *info,
2162306a36Sopenharmony_ci		       u32 op, u64 scratch)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	__le64 *wqe;
2462306a36Sopenharmony_ci	u64 qw1, qw2;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
2762306a36Sopenharmony_ci	if (!wqe)
2862306a36Sopenharmony_ci		return -ENOMEM;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci	set_64bit_val(wqe, 0, ether_addr_to_u64(info->mac_addr) << 16);
3162306a36Sopenharmony_ci	qw1 = FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_PDINDEXLO, info->pd_idx) |
3262306a36Sopenharmony_ci	      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_TC, info->tc_tos) |
3362306a36Sopenharmony_ci	      FIELD_PREP(IRDMA_UDAQPC_VLANTAG, info->vlan_tag);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	qw2 = FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ARPINDEX, info->dst_arpindex) |
3662306a36Sopenharmony_ci	      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_FLOWLABEL, info->flow_label) |
3762306a36Sopenharmony_ci	      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_HOPLIMIT, info->hop_ttl) |
3862306a36Sopenharmony_ci	      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_PDINDEXHI, info->pd_idx >> 16);
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	if (!info->ipv4_valid) {
4162306a36Sopenharmony_ci		set_64bit_val(wqe, 40,
4262306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR0, info->dest_ip_addr[0]) |
4362306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR1, info->dest_ip_addr[1]));
4462306a36Sopenharmony_ci		set_64bit_val(wqe, 32,
4562306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR2, info->dest_ip_addr[2]) |
4662306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->dest_ip_addr[3]));
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci		set_64bit_val(wqe, 56,
4962306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR0, info->src_ip_addr[0]) |
5062306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR1, info->src_ip_addr[1]));
5162306a36Sopenharmony_ci		set_64bit_val(wqe, 48,
5262306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR2, info->src_ip_addr[2]) |
5362306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->src_ip_addr[3]));
5462306a36Sopenharmony_ci	} else {
5562306a36Sopenharmony_ci		set_64bit_val(wqe, 32,
5662306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->dest_ip_addr[0]));
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci		set_64bit_val(wqe, 48,
5962306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->src_ip_addr[0]));
6062306a36Sopenharmony_ci	}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	set_64bit_val(wqe, 8, qw1);
6362306a36Sopenharmony_ci	set_64bit_val(wqe, 16, qw2);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	dma_wmb(); /* need write block before writing WQE header */
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	set_64bit_val(
6862306a36Sopenharmony_ci		wqe, 24,
6962306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_WQEVALID, cqp->polarity) |
7062306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_OPCODE, op) |
7162306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_DOLOOPBACKK, info->do_lpbk) |
7262306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_IPV4VALID, info->ipv4_valid) |
7362306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_AVIDX, info->ah_idx) |
7462306a36Sopenharmony_ci		FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_INSERTVLANTAG, info->insert_vlan_tag));
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	print_hex_dump_debug("WQE: MANAGE_AH WQE", DUMP_PREFIX_OFFSET, 16, 8,
7762306a36Sopenharmony_ci			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
7862306a36Sopenharmony_ci	irdma_sc_cqp_post_sq(cqp);
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	return 0;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/**
8462306a36Sopenharmony_ci * irdma_create_mg_ctx() - create a mcg context
8562306a36Sopenharmony_ci * @info: multicast group context info
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_cistatic void irdma_create_mg_ctx(struct irdma_mcast_grp_info *info)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct irdma_mcast_grp_ctx_entry_info *entry_info = NULL;
9062306a36Sopenharmony_ci	u8 idx = 0; /* index in the array */
9162306a36Sopenharmony_ci	u8 ctx_idx = 0; /* index in the MG context */
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	memset(info->dma_mem_mc.va, 0, IRDMA_MAX_MGS_PER_CTX * sizeof(u64));
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	for (idx = 0; idx < IRDMA_MAX_MGS_PER_CTX; idx++) {
9662306a36Sopenharmony_ci		entry_info = &info->mg_ctx_info[idx];
9762306a36Sopenharmony_ci		if (entry_info->valid_entry) {
9862306a36Sopenharmony_ci			set_64bit_val((__le64 *)info->dma_mem_mc.va,
9962306a36Sopenharmony_ci				      ctx_idx * sizeof(u64),
10062306a36Sopenharmony_ci				      FIELD_PREP(IRDMA_UDA_MGCTX_DESTPORT, entry_info->dest_port) |
10162306a36Sopenharmony_ci				      FIELD_PREP(IRDMA_UDA_MGCTX_VALIDENT, entry_info->valid_entry) |
10262306a36Sopenharmony_ci				      FIELD_PREP(IRDMA_UDA_MGCTX_QPID, entry_info->qp_id));
10362306a36Sopenharmony_ci			ctx_idx++;
10462306a36Sopenharmony_ci		}
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/**
10962306a36Sopenharmony_ci * irdma_access_mcast_grp() - Access mcast group based on op
11062306a36Sopenharmony_ci * @cqp: Control QP
11162306a36Sopenharmony_ci * @info: multicast group context info
11262306a36Sopenharmony_ci * @op: operation to perform
11362306a36Sopenharmony_ci * @scratch: u64 saved to be used during cqp completion
11462306a36Sopenharmony_ci */
11562306a36Sopenharmony_ciint irdma_access_mcast_grp(struct irdma_sc_cqp *cqp,
11662306a36Sopenharmony_ci			   struct irdma_mcast_grp_info *info, u32 op,
11762306a36Sopenharmony_ci			   u64 scratch)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	__le64 *wqe;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	if (info->mg_id >= IRDMA_UDA_MAX_FSI_MGS) {
12262306a36Sopenharmony_ci		ibdev_dbg(to_ibdev(cqp->dev), "WQE: mg_id out of range\n");
12362306a36Sopenharmony_ci		return -EINVAL;
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	wqe = irdma_sc_cqp_get_next_send_wqe(cqp, scratch);
12762306a36Sopenharmony_ci	if (!wqe) {
12862306a36Sopenharmony_ci		ibdev_dbg(to_ibdev(cqp->dev), "WQE: ring full\n");
12962306a36Sopenharmony_ci		return -ENOMEM;
13062306a36Sopenharmony_ci	}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	irdma_create_mg_ctx(info);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	set_64bit_val(wqe, 32, info->dma_mem_mc.pa);
13562306a36Sopenharmony_ci	set_64bit_val(wqe, 16,
13662306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_VLANID, info->vlan_id) |
13762306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_QS_HANDLE, info->qs_handle));
13862306a36Sopenharmony_ci	set_64bit_val(wqe, 0, ether_addr_to_u64(info->dest_mac_addr));
13962306a36Sopenharmony_ci	set_64bit_val(wqe, 8,
14062306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_HMC_FCN_ID, info->hmc_fcn_id));
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	if (!info->ipv4_valid) {
14362306a36Sopenharmony_ci		set_64bit_val(wqe, 56,
14462306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR0, info->dest_ip_addr[0]) |
14562306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR1, info->dest_ip_addr[1]));
14662306a36Sopenharmony_ci		set_64bit_val(wqe, 48,
14762306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR2, info->dest_ip_addr[2]) |
14862306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->dest_ip_addr[3]));
14962306a36Sopenharmony_ci	} else {
15062306a36Sopenharmony_ci		set_64bit_val(wqe, 48,
15162306a36Sopenharmony_ci			      FIELD_PREP(IRDMA_UDA_CQPSQ_MAV_ADDR3, info->dest_ip_addr[0]));
15262306a36Sopenharmony_ci	}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	dma_wmb(); /* need write memory block before writing the WQE header. */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	set_64bit_val(wqe, 24,
15762306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_WQEVALID, cqp->polarity) |
15862306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_OPCODE, op) |
15962306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_MGIDX, info->mg_id) |
16062306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_VLANVALID, info->vlan_valid) |
16162306a36Sopenharmony_ci		      FIELD_PREP(IRDMA_UDA_CQPSQ_MG_IPV4VALID, info->ipv4_valid));
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	print_hex_dump_debug("WQE: MANAGE_MCG WQE", DUMP_PREFIX_OFFSET, 16, 8,
16462306a36Sopenharmony_ci			     wqe, IRDMA_CQP_WQE_SIZE * 8, false);
16562306a36Sopenharmony_ci	print_hex_dump_debug("WQE: MCG_HOST CTX WQE", DUMP_PREFIX_OFFSET, 16,
16662306a36Sopenharmony_ci			     8, info->dma_mem_mc.va,
16762306a36Sopenharmony_ci			     IRDMA_MAX_MGS_PER_CTX * 8, false);
16862306a36Sopenharmony_ci	irdma_sc_cqp_post_sq(cqp);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	return 0;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/**
17462306a36Sopenharmony_ci * irdma_compare_mgs - Compares two multicast group structures
17562306a36Sopenharmony_ci * @entry1: Multcast group info
17662306a36Sopenharmony_ci * @entry2: Multcast group info in context
17762306a36Sopenharmony_ci */
17862306a36Sopenharmony_cistatic bool irdma_compare_mgs(struct irdma_mcast_grp_ctx_entry_info *entry1,
17962306a36Sopenharmony_ci			      struct irdma_mcast_grp_ctx_entry_info *entry2)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	if (entry1->dest_port == entry2->dest_port &&
18262306a36Sopenharmony_ci	    entry1->qp_id == entry2->qp_id)
18362306a36Sopenharmony_ci		return true;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	return false;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/**
18962306a36Sopenharmony_ci * irdma_sc_add_mcast_grp - Allocates mcast group entry in ctx
19062306a36Sopenharmony_ci * @ctx: Multcast group context
19162306a36Sopenharmony_ci * @mg: Multcast group info
19262306a36Sopenharmony_ci */
19362306a36Sopenharmony_ciint irdma_sc_add_mcast_grp(struct irdma_mcast_grp_info *ctx,
19462306a36Sopenharmony_ci			   struct irdma_mcast_grp_ctx_entry_info *mg)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	u32 idx;
19762306a36Sopenharmony_ci	bool free_entry_found = false;
19862306a36Sopenharmony_ci	u32 free_entry_idx = 0;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	/* find either an identical or a free entry for a multicast group */
20162306a36Sopenharmony_ci	for (idx = 0; idx < IRDMA_MAX_MGS_PER_CTX; idx++) {
20262306a36Sopenharmony_ci		if (ctx->mg_ctx_info[idx].valid_entry) {
20362306a36Sopenharmony_ci			if (irdma_compare_mgs(&ctx->mg_ctx_info[idx], mg)) {
20462306a36Sopenharmony_ci				ctx->mg_ctx_info[idx].use_cnt++;
20562306a36Sopenharmony_ci				return 0;
20662306a36Sopenharmony_ci			}
20762306a36Sopenharmony_ci			continue;
20862306a36Sopenharmony_ci		}
20962306a36Sopenharmony_ci		if (!free_entry_found) {
21062306a36Sopenharmony_ci			free_entry_found = true;
21162306a36Sopenharmony_ci			free_entry_idx = idx;
21262306a36Sopenharmony_ci		}
21362306a36Sopenharmony_ci	}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	if (free_entry_found) {
21662306a36Sopenharmony_ci		ctx->mg_ctx_info[free_entry_idx] = *mg;
21762306a36Sopenharmony_ci		ctx->mg_ctx_info[free_entry_idx].valid_entry = true;
21862306a36Sopenharmony_ci		ctx->mg_ctx_info[free_entry_idx].use_cnt = 1;
21962306a36Sopenharmony_ci		ctx->no_of_mgs++;
22062306a36Sopenharmony_ci		return 0;
22162306a36Sopenharmony_ci	}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	return -ENOMEM;
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci/**
22762306a36Sopenharmony_ci * irdma_sc_del_mcast_grp - Delete mcast group
22862306a36Sopenharmony_ci * @ctx: Multcast group context
22962306a36Sopenharmony_ci * @mg: Multcast group info
23062306a36Sopenharmony_ci *
23162306a36Sopenharmony_ci * Finds and removes a specific mulicast group from context, all
23262306a36Sopenharmony_ci * parameters must match to remove a multicast group.
23362306a36Sopenharmony_ci */
23462306a36Sopenharmony_ciint irdma_sc_del_mcast_grp(struct irdma_mcast_grp_info *ctx,
23562306a36Sopenharmony_ci			   struct irdma_mcast_grp_ctx_entry_info *mg)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	u32 idx;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* find an entry in multicast group context */
24062306a36Sopenharmony_ci	for (idx = 0; idx < IRDMA_MAX_MGS_PER_CTX; idx++) {
24162306a36Sopenharmony_ci		if (!ctx->mg_ctx_info[idx].valid_entry)
24262306a36Sopenharmony_ci			continue;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci		if (irdma_compare_mgs(mg, &ctx->mg_ctx_info[idx])) {
24562306a36Sopenharmony_ci			ctx->mg_ctx_info[idx].use_cnt--;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci			if (!ctx->mg_ctx_info[idx].use_cnt) {
24862306a36Sopenharmony_ci				ctx->mg_ctx_info[idx].valid_entry = false;
24962306a36Sopenharmony_ci				ctx->no_of_mgs--;
25062306a36Sopenharmony_ci				/* Remove gap if element was not the last */
25162306a36Sopenharmony_ci				if (idx != ctx->no_of_mgs &&
25262306a36Sopenharmony_ci				    ctx->no_of_mgs > 0) {
25362306a36Sopenharmony_ci					memcpy(&ctx->mg_ctx_info[idx],
25462306a36Sopenharmony_ci					       &ctx->mg_ctx_info[ctx->no_of_mgs - 1],
25562306a36Sopenharmony_ci					       sizeof(ctx->mg_ctx_info[idx]));
25662306a36Sopenharmony_ci					ctx->mg_ctx_info[ctx->no_of_mgs - 1].valid_entry = false;
25762306a36Sopenharmony_ci				}
25862306a36Sopenharmony_ci			}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci			return 0;
26162306a36Sopenharmony_ci		}
26262306a36Sopenharmony_ci	}
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	return -EINVAL;
26562306a36Sopenharmony_ci}
266