18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci/* QLogic qed NIC Driver
38c2ecf20Sopenharmony_ci * Copyright (c) 2015-2017  QLogic Corporation
48c2ecf20Sopenharmony_ci * Copyright (c) 2019-2020 Marvell International Ltd.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/types.h>
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
108c2ecf20Sopenharmony_ci#include <linux/errno.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/list.h>
138c2ecf20Sopenharmony_ci#include <linux/log2.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/string.h>
178c2ecf20Sopenharmony_ci#include "qed.h"
188c2ecf20Sopenharmony_ci#include "qed_cxt.h"
198c2ecf20Sopenharmony_ci#include "qed_dev_api.h"
208c2ecf20Sopenharmony_ci#include "qed_hsi.h"
218c2ecf20Sopenharmony_ci#include "qed_hw.h"
228c2ecf20Sopenharmony_ci#include "qed_init_ops.h"
238c2ecf20Sopenharmony_ci#include "qed_rdma.h"
248c2ecf20Sopenharmony_ci#include "qed_reg_addr.h"
258c2ecf20Sopenharmony_ci#include "qed_sriov.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* QM constants */
288c2ecf20Sopenharmony_ci#define QM_PQ_ELEMENT_SIZE	4 /* in bytes */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Doorbell-Queue constants */
318c2ecf20Sopenharmony_ci#define DQ_RANGE_SHIFT		4
328c2ecf20Sopenharmony_ci#define DQ_RANGE_ALIGN		BIT(DQ_RANGE_SHIFT)
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* Searcher constants */
358c2ecf20Sopenharmony_ci#define SRC_MIN_NUM_ELEMS 256
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* Timers constants */
388c2ecf20Sopenharmony_ci#define TM_SHIFT        7
398c2ecf20Sopenharmony_ci#define TM_ALIGN        BIT(TM_SHIFT)
408c2ecf20Sopenharmony_ci#define TM_ELEM_SIZE    4
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define ILT_DEFAULT_HW_P_SIZE	4
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define ILT_PAGE_IN_BYTES(hw_p_size)	(1U << ((hw_p_size) + 12))
458c2ecf20Sopenharmony_ci#define ILT_CFG_REG(cli, reg)	PSWRQ2_REG_ ## cli ## _ ## reg ## _RT_OFFSET
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* ILT entry structure */
488c2ecf20Sopenharmony_ci#define ILT_ENTRY_PHY_ADDR_MASK		(~0ULL >> 12)
498c2ecf20Sopenharmony_ci#define ILT_ENTRY_PHY_ADDR_SHIFT	0
508c2ecf20Sopenharmony_ci#define ILT_ENTRY_VALID_MASK		0x1ULL
518c2ecf20Sopenharmony_ci#define ILT_ENTRY_VALID_SHIFT		52
528c2ecf20Sopenharmony_ci#define ILT_ENTRY_IN_REGS		2
538c2ecf20Sopenharmony_ci#define ILT_REG_SIZE_IN_BYTES		4
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* connection context union */
568c2ecf20Sopenharmony_ciunion conn_context {
578c2ecf20Sopenharmony_ci	struct e4_core_conn_context core_ctx;
588c2ecf20Sopenharmony_ci	struct e4_eth_conn_context eth_ctx;
598c2ecf20Sopenharmony_ci	struct e4_iscsi_conn_context iscsi_ctx;
608c2ecf20Sopenharmony_ci	struct e4_fcoe_conn_context fcoe_ctx;
618c2ecf20Sopenharmony_ci	struct e4_roce_conn_context roce_ctx;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* TYPE-0 task context - iSCSI, FCOE */
658c2ecf20Sopenharmony_ciunion type0_task_context {
668c2ecf20Sopenharmony_ci	struct e4_iscsi_task_context iscsi_ctx;
678c2ecf20Sopenharmony_ci	struct e4_fcoe_task_context fcoe_ctx;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* TYPE-1 task context - ROCE */
718c2ecf20Sopenharmony_ciunion type1_task_context {
728c2ecf20Sopenharmony_ci	struct e4_rdma_task_context roce_ctx;
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistruct src_ent {
768c2ecf20Sopenharmony_ci	__u8				opaque[56];
778c2ecf20Sopenharmony_ci	__be64				next;
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define CDUT_SEG_ALIGNMET		3 /* in 4k chunks */
818c2ecf20Sopenharmony_ci#define CDUT_SEG_ALIGNMET_IN_BYTES	BIT(CDUT_SEG_ALIGNMET + 12)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define CONN_CXT_SIZE(p_hwfn) \
848c2ecf20Sopenharmony_ci	ALIGNED_TYPE_SIZE(union conn_context, p_hwfn)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define SRQ_CXT_SIZE (sizeof(struct rdma_srq_context))
878c2ecf20Sopenharmony_ci#define XRC_SRQ_CXT_SIZE (sizeof(struct rdma_xrc_srq_context))
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define TYPE0_TASK_CXT_SIZE(p_hwfn) \
908c2ecf20Sopenharmony_ci	ALIGNED_TYPE_SIZE(union type0_task_context, p_hwfn)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* Alignment is inherent to the type1_task_context structure */
938c2ecf20Sopenharmony_ci#define TYPE1_TASK_CXT_SIZE(p_hwfn) sizeof(union type1_task_context)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic bool src_proto(enum protocol_type type)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	return type == PROTOCOLID_ISCSI ||
988c2ecf20Sopenharmony_ci	       type == PROTOCOLID_FCOE ||
998c2ecf20Sopenharmony_ci	       type == PROTOCOLID_IWARP;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic bool tm_cid_proto(enum protocol_type type)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	return type == PROTOCOLID_ISCSI ||
1058c2ecf20Sopenharmony_ci	       type == PROTOCOLID_FCOE ||
1068c2ecf20Sopenharmony_ci	       type == PROTOCOLID_ROCE ||
1078c2ecf20Sopenharmony_ci	       type == PROTOCOLID_IWARP;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic bool tm_tid_proto(enum protocol_type type)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return type == PROTOCOLID_FCOE;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* counts the iids for the CDU/CDUC ILT client configuration */
1168c2ecf20Sopenharmony_cistruct qed_cdu_iids {
1178c2ecf20Sopenharmony_ci	u32 pf_cids;
1188c2ecf20Sopenharmony_ci	u32 per_vf_cids;
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void qed_cxt_cdu_iids(struct qed_cxt_mngr *p_mngr,
1228c2ecf20Sopenharmony_ci			     struct qed_cdu_iids *iids)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	u32 type;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	for (type = 0; type < MAX_CONN_TYPES; type++) {
1278c2ecf20Sopenharmony_ci		iids->pf_cids += p_mngr->conn_cfg[type].cid_count;
1288c2ecf20Sopenharmony_ci		iids->per_vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* counts the iids for the Searcher block configuration */
1338c2ecf20Sopenharmony_cistruct qed_src_iids {
1348c2ecf20Sopenharmony_ci	u32 pf_cids;
1358c2ecf20Sopenharmony_ci	u32 per_vf_cids;
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic void qed_cxt_src_iids(struct qed_cxt_mngr *p_mngr,
1398c2ecf20Sopenharmony_ci			     struct qed_src_iids *iids)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	u32 i;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_CONN_TYPES; i++) {
1448c2ecf20Sopenharmony_ci		if (!src_proto(i))
1458c2ecf20Sopenharmony_ci			continue;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		iids->pf_cids += p_mngr->conn_cfg[i].cid_count;
1488c2ecf20Sopenharmony_ci		iids->per_vf_cids += p_mngr->conn_cfg[i].cids_per_vf;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* Add L2 filtering filters in addition */
1528c2ecf20Sopenharmony_ci	iids->pf_cids += p_mngr->arfs_count;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* counts the iids for the Timers block configuration */
1568c2ecf20Sopenharmony_cistruct qed_tm_iids {
1578c2ecf20Sopenharmony_ci	u32 pf_cids;
1588c2ecf20Sopenharmony_ci	u32 pf_tids[NUM_TASK_PF_SEGMENTS];	/* per segment */
1598c2ecf20Sopenharmony_ci	u32 pf_tids_total;
1608c2ecf20Sopenharmony_ci	u32 per_vf_cids;
1618c2ecf20Sopenharmony_ci	u32 per_vf_tids;
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void qed_cxt_tm_iids(struct qed_hwfn *p_hwfn,
1658c2ecf20Sopenharmony_ci			    struct qed_cxt_mngr *p_mngr,
1668c2ecf20Sopenharmony_ci			    struct qed_tm_iids *iids)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	bool tm_vf_required = false;
1698c2ecf20Sopenharmony_ci	bool tm_required = false;
1708c2ecf20Sopenharmony_ci	int i, j;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* Timers is a special case -> we don't count how many cids require
1738c2ecf20Sopenharmony_ci	 * timers but what's the max cid that will be used by the timer block.
1748c2ecf20Sopenharmony_ci	 * therefore we traverse in reverse order, and once we hit a protocol
1758c2ecf20Sopenharmony_ci	 * that requires the timers memory, we'll sum all the protocols up
1768c2ecf20Sopenharmony_ci	 * to that one.
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	for (i = MAX_CONN_TYPES - 1; i >= 0; i--) {
1798c2ecf20Sopenharmony_ci		struct qed_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[i];
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		if (tm_cid_proto(i) || tm_required) {
1828c2ecf20Sopenharmony_ci			if (p_cfg->cid_count)
1838c2ecf20Sopenharmony_ci				tm_required = true;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci			iids->pf_cids += p_cfg->cid_count;
1868c2ecf20Sopenharmony_ci		}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		if (tm_cid_proto(i) || tm_vf_required) {
1898c2ecf20Sopenharmony_ci			if (p_cfg->cids_per_vf)
1908c2ecf20Sopenharmony_ci				tm_vf_required = true;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci			iids->per_vf_cids += p_cfg->cids_per_vf;
1938c2ecf20Sopenharmony_ci		}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		if (tm_tid_proto(i)) {
1968c2ecf20Sopenharmony_ci			struct qed_tid_seg *segs = p_cfg->tid_seg;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci			/* for each segment there is at most one
1998c2ecf20Sopenharmony_ci			 * protocol for which count is not 0.
2008c2ecf20Sopenharmony_ci			 */
2018c2ecf20Sopenharmony_ci			for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
2028c2ecf20Sopenharmony_ci				iids->pf_tids[j] += segs[j].count;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci			/* The last array elelment is for the VFs. As for PF
2058c2ecf20Sopenharmony_ci			 * segments there can be only one protocol for
2068c2ecf20Sopenharmony_ci			 * which this value is not 0.
2078c2ecf20Sopenharmony_ci			 */
2088c2ecf20Sopenharmony_ci			iids->per_vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	iids->pf_cids = roundup(iids->pf_cids, TM_ALIGN);
2138c2ecf20Sopenharmony_ci	iids->per_vf_cids = roundup(iids->per_vf_cids, TM_ALIGN);
2148c2ecf20Sopenharmony_ci	iids->per_vf_tids = roundup(iids->per_vf_tids, TM_ALIGN);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	for (iids->pf_tids_total = 0, j = 0; j < NUM_TASK_PF_SEGMENTS; j++) {
2178c2ecf20Sopenharmony_ci		iids->pf_tids[j] = roundup(iids->pf_tids[j], TM_ALIGN);
2188c2ecf20Sopenharmony_ci		iids->pf_tids_total += iids->pf_tids[j];
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic void qed_cxt_qm_iids(struct qed_hwfn *p_hwfn,
2238c2ecf20Sopenharmony_ci			    struct qed_qm_iids *iids)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2268c2ecf20Sopenharmony_ci	struct qed_tid_seg *segs;
2278c2ecf20Sopenharmony_ci	u32 vf_cids = 0, type, j;
2288c2ecf20Sopenharmony_ci	u32 vf_tids = 0;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	for (type = 0; type < MAX_CONN_TYPES; type++) {
2318c2ecf20Sopenharmony_ci		iids->cids += p_mngr->conn_cfg[type].cid_count;
2328c2ecf20Sopenharmony_ci		vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		segs = p_mngr->conn_cfg[type].tid_seg;
2358c2ecf20Sopenharmony_ci		/* for each segment there is at most one
2368c2ecf20Sopenharmony_ci		 * protocol for which count is not 0.
2378c2ecf20Sopenharmony_ci		 */
2388c2ecf20Sopenharmony_ci		for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
2398c2ecf20Sopenharmony_ci			iids->tids += segs[j].count;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		/* The last array elelment is for the VFs. As for PF
2428c2ecf20Sopenharmony_ci		 * segments there can be only one protocol for
2438c2ecf20Sopenharmony_ci		 * which this value is not 0.
2448c2ecf20Sopenharmony_ci		 */
2458c2ecf20Sopenharmony_ci		vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	iids->vf_cids = vf_cids;
2498c2ecf20Sopenharmony_ci	iids->tids += vf_tids * p_mngr->vf_count;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
2528c2ecf20Sopenharmony_ci		   "iids: CIDS %08x vf_cids %08x tids %08x vf_tids %08x\n",
2538c2ecf20Sopenharmony_ci		   iids->cids, iids->vf_cids, iids->tids, vf_tids);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic struct qed_tid_seg *qed_cxt_tid_seg_info(struct qed_hwfn *p_hwfn,
2578c2ecf20Sopenharmony_ci						u32 seg)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_cfg = p_hwfn->p_cxt_mngr;
2608c2ecf20Sopenharmony_ci	u32 i;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* Find the protocol with tid count > 0 for this segment.
2638c2ecf20Sopenharmony_ci	 * Note: there can only be one and this is already validated.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_CONN_TYPES; i++)
2668c2ecf20Sopenharmony_ci		if (p_cfg->conn_cfg[i].tid_seg[seg].count)
2678c2ecf20Sopenharmony_ci			return &p_cfg->conn_cfg[i].tid_seg[seg];
2688c2ecf20Sopenharmony_ci	return NULL;
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic void qed_cxt_set_srq_count(struct qed_hwfn *p_hwfn,
2728c2ecf20Sopenharmony_ci				  u32 num_srqs, u32 num_xrc_srqs)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	p_mgr->srq_count = num_srqs;
2778c2ecf20Sopenharmony_ci	p_mgr->xrc_srq_count = num_xrc_srqs;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ciu32 qed_cxt_get_ilt_page_size(struct qed_hwfn *p_hwfn,
2818c2ecf20Sopenharmony_ci			      enum ilt_clients ilt_client)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2848c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli = &p_mngr->clients[ilt_client];
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return ILT_PAGE_IN_BYTES(p_cli->p_size.val);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic u32 qed_cxt_xrc_srqs_per_page(struct qed_hwfn *p_hwfn)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	u32 page_size;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	page_size = qed_cxt_get_ilt_page_size(p_hwfn, ILT_CLI_TSDM);
2948c2ecf20Sopenharmony_ci	return page_size / XRC_SRQ_CXT_SIZE;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciu32 qed_cxt_get_total_srq_count(struct qed_hwfn *p_hwfn)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
3008c2ecf20Sopenharmony_ci	u32 total_srqs;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	total_srqs = p_mgr->srq_count + p_mgr->xrc_srq_count;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return total_srqs;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci/* set the iids count per protocol */
3088c2ecf20Sopenharmony_cistatic void qed_cxt_set_proto_cid_count(struct qed_hwfn *p_hwfn,
3098c2ecf20Sopenharmony_ci					enum protocol_type type,
3108c2ecf20Sopenharmony_ci					u32 cid_count, u32 vf_cid_cnt)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
3138c2ecf20Sopenharmony_ci	struct qed_conn_type_cfg *p_conn = &p_mgr->conn_cfg[type];
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	p_conn->cid_count = roundup(cid_count, DQ_RANGE_ALIGN);
3168c2ecf20Sopenharmony_ci	p_conn->cids_per_vf = roundup(vf_cid_cnt, DQ_RANGE_ALIGN);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if (type == PROTOCOLID_ROCE) {
3198c2ecf20Sopenharmony_ci		u32 page_sz = p_mgr->clients[ILT_CLI_CDUC].p_size.val;
3208c2ecf20Sopenharmony_ci		u32 cxt_size = CONN_CXT_SIZE(p_hwfn);
3218c2ecf20Sopenharmony_ci		u32 elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
3228c2ecf20Sopenharmony_ci		u32 align = elems_per_page * DQ_RANGE_ALIGN;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		p_conn->cid_count = roundup(p_conn->cid_count, align);
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ciu32 qed_cxt_get_proto_cid_count(struct qed_hwfn *p_hwfn,
3298c2ecf20Sopenharmony_ci				enum protocol_type type, u32 *vf_cid)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	if (vf_cid)
3328c2ecf20Sopenharmony_ci		*vf_cid = p_hwfn->p_cxt_mngr->conn_cfg[type].cids_per_vf;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	return p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ciu32 qed_cxt_get_proto_cid_start(struct qed_hwfn *p_hwfn,
3388c2ecf20Sopenharmony_ci				enum protocol_type type)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	return p_hwfn->p_cxt_mngr->acquired[type].start_cid;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ciu32 qed_cxt_get_proto_tid_count(struct qed_hwfn *p_hwfn,
3448c2ecf20Sopenharmony_ci				enum protocol_type type)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	u32 cnt = 0;
3478c2ecf20Sopenharmony_ci	int i;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	for (i = 0; i < TASK_SEGMENTS; i++)
3508c2ecf20Sopenharmony_ci		cnt += p_hwfn->p_cxt_mngr->conn_cfg[type].tid_seg[i].count;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	return cnt;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic void qed_cxt_set_proto_tid_count(struct qed_hwfn *p_hwfn,
3568c2ecf20Sopenharmony_ci					enum protocol_type proto,
3578c2ecf20Sopenharmony_ci					u8 seg,
3588c2ecf20Sopenharmony_ci					u8 seg_type, u32 count, bool has_fl)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
3618c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg = &p_mngr->conn_cfg[proto].tid_seg[seg];
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	p_seg->count = count;
3648c2ecf20Sopenharmony_ci	p_seg->has_fl_mem = has_fl;
3658c2ecf20Sopenharmony_ci	p_seg->type = seg_type;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic void qed_ilt_cli_blk_fill(struct qed_ilt_client_cfg *p_cli,
3698c2ecf20Sopenharmony_ci				 struct qed_ilt_cli_blk *p_blk,
3708c2ecf20Sopenharmony_ci				 u32 start_line, u32 total_size, u32 elem_size)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	u32 ilt_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* verify thatits called only once for each block */
3758c2ecf20Sopenharmony_ci	if (p_blk->total_size)
3768c2ecf20Sopenharmony_ci		return;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	p_blk->total_size = total_size;
3798c2ecf20Sopenharmony_ci	p_blk->real_size_in_page = 0;
3808c2ecf20Sopenharmony_ci	if (elem_size)
3818c2ecf20Sopenharmony_ci		p_blk->real_size_in_page = (ilt_size / elem_size) * elem_size;
3828c2ecf20Sopenharmony_ci	p_blk->start_line = start_line;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic void qed_ilt_cli_adv_line(struct qed_hwfn *p_hwfn,
3868c2ecf20Sopenharmony_ci				 struct qed_ilt_client_cfg *p_cli,
3878c2ecf20Sopenharmony_ci				 struct qed_ilt_cli_blk *p_blk,
3888c2ecf20Sopenharmony_ci				 u32 *p_line, enum ilt_clients client_id)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	if (!p_blk->total_size)
3918c2ecf20Sopenharmony_ci		return;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (!p_cli->active)
3948c2ecf20Sopenharmony_ci		p_cli->first.val = *p_line;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	p_cli->active = true;
3978c2ecf20Sopenharmony_ci	*p_line += DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
3988c2ecf20Sopenharmony_ci	p_cli->last.val = *p_line - 1;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
4018c2ecf20Sopenharmony_ci		   "ILT[Client %d] - Lines: [%08x - %08x]. Block - Size %08x [Real %08x] Start line %d\n",
4028c2ecf20Sopenharmony_ci		   client_id, p_cli->first.val,
4038c2ecf20Sopenharmony_ci		   p_cli->last.val, p_blk->total_size,
4048c2ecf20Sopenharmony_ci		   p_blk->real_size_in_page, p_blk->start_line);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic u32 qed_ilt_get_dynamic_line_cnt(struct qed_hwfn *p_hwfn,
4088c2ecf20Sopenharmony_ci					enum ilt_clients ilt_client)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u32 cid_count = p_hwfn->p_cxt_mngr->conn_cfg[PROTOCOLID_ROCE].cid_count;
4118c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
4128c2ecf20Sopenharmony_ci	u32 lines_to_skip = 0;
4138c2ecf20Sopenharmony_ci	u32 cxts_per_p;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	if (ilt_client == ILT_CLI_CDUC) {
4168c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		cxts_per_p = ILT_PAGE_IN_BYTES(p_cli->p_size.val) /
4198c2ecf20Sopenharmony_ci		    (u32) CONN_CXT_SIZE(p_hwfn);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		lines_to_skip = cid_count / cxts_per_p;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return lines_to_skip;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic struct qed_ilt_client_cfg *qed_cxt_set_cli(struct qed_ilt_client_cfg
4288c2ecf20Sopenharmony_ci						  *p_cli)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	p_cli->active = false;
4318c2ecf20Sopenharmony_ci	p_cli->first.val = 0;
4328c2ecf20Sopenharmony_ci	p_cli->last.val = 0;
4338c2ecf20Sopenharmony_ci	return p_cli;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic struct qed_ilt_cli_blk *qed_cxt_set_blk(struct qed_ilt_cli_blk *p_blk)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	p_blk->total_size = 0;
4398c2ecf20Sopenharmony_ci	return p_blk;
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic void qed_cxt_ilt_blk_reset(struct qed_hwfn *p_hwfn)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *clients = p_hwfn->p_cxt_mngr->clients;
4458c2ecf20Sopenharmony_ci	u32 cli_idx, blk_idx;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	for (cli_idx = 0; cli_idx < MAX_ILT_CLIENTS; cli_idx++) {
4488c2ecf20Sopenharmony_ci		for (blk_idx = 0; blk_idx < ILT_CLI_PF_BLOCKS; blk_idx++)
4498c2ecf20Sopenharmony_ci			clients[cli_idx].pf_blks[blk_idx].total_size = 0;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci		for (blk_idx = 0; blk_idx < ILT_CLI_VF_BLOCKS; blk_idx++)
4528c2ecf20Sopenharmony_ci			clients[cli_idx].vf_blks[blk_idx].total_size = 0;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ciint qed_cxt_cfg_ilt_compute(struct qed_hwfn *p_hwfn, u32 *line_count)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
4598c2ecf20Sopenharmony_ci	u32 curr_line, total, i, task_size, line;
4608c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
4618c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
4628c2ecf20Sopenharmony_ci	struct qed_cdu_iids cdu_iids;
4638c2ecf20Sopenharmony_ci	struct qed_src_iids src_iids;
4648c2ecf20Sopenharmony_ci	struct qed_qm_iids qm_iids;
4658c2ecf20Sopenharmony_ci	struct qed_tm_iids tm_iids;
4668c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	memset(&qm_iids, 0, sizeof(qm_iids));
4698c2ecf20Sopenharmony_ci	memset(&cdu_iids, 0, sizeof(cdu_iids));
4708c2ecf20Sopenharmony_ci	memset(&src_iids, 0, sizeof(src_iids));
4718c2ecf20Sopenharmony_ci	memset(&tm_iids, 0, sizeof(tm_iids));
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	p_mngr->pf_start_line = RESC_START(p_hwfn, QED_ILT);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* Reset all ILT blocks at the beginning of ILT computing in order
4768c2ecf20Sopenharmony_ci	 * to prevent memory allocation for irrelevant blocks afterwards.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	qed_cxt_ilt_blk_reset(p_hwfn);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
4818c2ecf20Sopenharmony_ci		   "hwfn [%d] - Set context manager starting line to be 0x%08x\n",
4828c2ecf20Sopenharmony_ci		   p_hwfn->my_id, p_hwfn->p_cxt_mngr->pf_start_line);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* CDUC */
4858c2ecf20Sopenharmony_ci	p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_CDUC]);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	curr_line = p_mngr->pf_start_line;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* CDUC PF */
4908c2ecf20Sopenharmony_ci	p_cli->pf_total_lines = 0;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* get the counters for the CDUC and QM clients  */
4938c2ecf20Sopenharmony_ci	qed_cxt_cdu_iids(p_mngr, &cdu_iids);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	p_blk = qed_cxt_set_blk(&p_cli->pf_blks[CDUC_BLK]);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	total = cdu_iids.pf_cids * CONN_CXT_SIZE(p_hwfn);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
5008c2ecf20Sopenharmony_ci			     total, CONN_CXT_SIZE(p_hwfn));
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
5038c2ecf20Sopenharmony_ci	p_cli->pf_total_lines = curr_line - p_blk->start_line;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	p_blk->dynamic_line_cnt = qed_ilt_get_dynamic_line_cnt(p_hwfn,
5068c2ecf20Sopenharmony_ci							       ILT_CLI_CDUC);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	/* CDUC VF */
5098c2ecf20Sopenharmony_ci	p_blk = qed_cxt_set_blk(&p_cli->vf_blks[CDUC_BLK]);
5108c2ecf20Sopenharmony_ci	total = cdu_iids.per_vf_cids * CONN_CXT_SIZE(p_hwfn);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
5138c2ecf20Sopenharmony_ci			     total, CONN_CXT_SIZE(p_hwfn));
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
5168c2ecf20Sopenharmony_ci	p_cli->vf_total_lines = curr_line - p_blk->start_line;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	for (i = 1; i < p_mngr->vf_count; i++)
5198c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
5208c2ecf20Sopenharmony_ci				     ILT_CLI_CDUC);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	/* CDUT PF */
5238c2ecf20Sopenharmony_ci	p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_CDUT]);
5248c2ecf20Sopenharmony_ci	p_cli->first.val = curr_line;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	/* first the 'working' task memory */
5278c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
5288c2ecf20Sopenharmony_ci		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
5298c2ecf20Sopenharmony_ci		if (!p_seg || p_seg->count == 0)
5308c2ecf20Sopenharmony_ci			continue;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->pf_blks[CDUT_SEG_BLK(i)]);
5338c2ecf20Sopenharmony_ci		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
5348c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total,
5358c2ecf20Sopenharmony_ci				     p_mngr->task_type_size[p_seg->type]);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
5388c2ecf20Sopenharmony_ci				     ILT_CLI_CDUT);
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* next the 'init' task memory (forced load memory) */
5428c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
5438c2ecf20Sopenharmony_ci		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
5448c2ecf20Sopenharmony_ci		if (!p_seg || p_seg->count == 0)
5458c2ecf20Sopenharmony_ci			continue;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci		p_blk =
5488c2ecf20Sopenharmony_ci		    qed_cxt_set_blk(&p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)]);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		if (!p_seg->has_fl_mem) {
5518c2ecf20Sopenharmony_ci			/* The segment is active (total size pf 'working'
5528c2ecf20Sopenharmony_ci			 * memory is > 0) but has no FL (forced-load, Init)
5538c2ecf20Sopenharmony_ci			 * memory. Thus:
5548c2ecf20Sopenharmony_ci			 *
5558c2ecf20Sopenharmony_ci			 * 1.   The total-size in the corrsponding FL block of
5568c2ecf20Sopenharmony_ci			 *      the ILT client is set to 0 - No ILT line are
5578c2ecf20Sopenharmony_ci			 *      provisioned and no ILT memory allocated.
5588c2ecf20Sopenharmony_ci			 *
5598c2ecf20Sopenharmony_ci			 * 2.   The start-line of said block is set to the
5608c2ecf20Sopenharmony_ci			 *      start line of the matching working memory
5618c2ecf20Sopenharmony_ci			 *      block in the ILT client. This is later used to
5628c2ecf20Sopenharmony_ci			 *      configure the CDU segment offset registers and
5638c2ecf20Sopenharmony_ci			 *      results in an FL command for TIDs of this
5648c2ecf20Sopenharmony_ci			 *      segement behaves as regular load commands
5658c2ecf20Sopenharmony_ci			 *      (loading TIDs from the working memory).
5668c2ecf20Sopenharmony_ci			 */
5678c2ecf20Sopenharmony_ci			line = p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
5708c2ecf20Sopenharmony_ci			continue;
5718c2ecf20Sopenharmony_ci		}
5728c2ecf20Sopenharmony_ci		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk,
5758c2ecf20Sopenharmony_ci				     curr_line, total,
5768c2ecf20Sopenharmony_ci				     p_mngr->task_type_size[p_seg->type]);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
5798c2ecf20Sopenharmony_ci				     ILT_CLI_CDUT);
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci	p_cli->pf_total_lines = curr_line - p_cli->pf_blks[0].start_line;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	/* CDUT VF */
5848c2ecf20Sopenharmony_ci	p_seg = qed_cxt_tid_seg_info(p_hwfn, TASK_SEGMENT_VF);
5858c2ecf20Sopenharmony_ci	if (p_seg && p_seg->count) {
5868c2ecf20Sopenharmony_ci		/* Stricly speaking we need to iterate over all VF
5878c2ecf20Sopenharmony_ci		 * task segment types, but a VF has only 1 segment
5888c2ecf20Sopenharmony_ci		 */
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci		/* 'working' memory */
5918c2ecf20Sopenharmony_ci		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->vf_blks[CDUT_SEG_BLK(0)]);
5948c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk,
5958c2ecf20Sopenharmony_ci				     curr_line, total,
5968c2ecf20Sopenharmony_ci				     p_mngr->task_type_size[p_seg->type]);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
5998c2ecf20Sopenharmony_ci				     ILT_CLI_CDUT);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci		/* 'init' memory */
6028c2ecf20Sopenharmony_ci		p_blk =
6038c2ecf20Sopenharmony_ci		    qed_cxt_set_blk(&p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)]);
6048c2ecf20Sopenharmony_ci		if (!p_seg->has_fl_mem) {
6058c2ecf20Sopenharmony_ci			/* see comment above */
6068c2ecf20Sopenharmony_ci			line = p_cli->vf_blks[CDUT_SEG_BLK(0)].start_line;
6078c2ecf20Sopenharmony_ci			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
6088c2ecf20Sopenharmony_ci		} else {
6098c2ecf20Sopenharmony_ci			task_size = p_mngr->task_type_size[p_seg->type];
6108c2ecf20Sopenharmony_ci			qed_ilt_cli_blk_fill(p_cli, p_blk,
6118c2ecf20Sopenharmony_ci					     curr_line, total, task_size);
6128c2ecf20Sopenharmony_ci			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6138c2ecf20Sopenharmony_ci					     ILT_CLI_CDUT);
6148c2ecf20Sopenharmony_ci		}
6158c2ecf20Sopenharmony_ci		p_cli->vf_total_lines = curr_line -
6168c2ecf20Sopenharmony_ci		    p_cli->vf_blks[0].start_line;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		/* Now for the rest of the VFs */
6198c2ecf20Sopenharmony_ci		for (i = 1; i < p_mngr->vf_count; i++) {
6208c2ecf20Sopenharmony_ci			p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
6218c2ecf20Sopenharmony_ci			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6228c2ecf20Sopenharmony_ci					     ILT_CLI_CDUT);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci			p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
6258c2ecf20Sopenharmony_ci			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6268c2ecf20Sopenharmony_ci					     ILT_CLI_CDUT);
6278c2ecf20Sopenharmony_ci		}
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* QM */
6318c2ecf20Sopenharmony_ci	p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_QM]);
6328c2ecf20Sopenharmony_ci	p_blk = qed_cxt_set_blk(&p_cli->pf_blks[0]);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	qed_cxt_qm_iids(p_hwfn, &qm_iids);
6358c2ecf20Sopenharmony_ci	total = qed_qm_pf_mem_size(qm_iids.cids,
6368c2ecf20Sopenharmony_ci				   qm_iids.vf_cids, qm_iids.tids,
6378c2ecf20Sopenharmony_ci				   p_hwfn->qm_info.num_pqs,
6388c2ecf20Sopenharmony_ci				   p_hwfn->qm_info.num_vf_pqs);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn,
6418c2ecf20Sopenharmony_ci		   QED_MSG_ILT,
6428c2ecf20Sopenharmony_ci		   "QM ILT Info, (cids=%d, vf_cids=%d, tids=%d, num_pqs=%d, num_vf_pqs=%d, memory_size=%d)\n",
6438c2ecf20Sopenharmony_ci		   qm_iids.cids,
6448c2ecf20Sopenharmony_ci		   qm_iids.vf_cids,
6458c2ecf20Sopenharmony_ci		   qm_iids.tids,
6468c2ecf20Sopenharmony_ci		   p_hwfn->qm_info.num_pqs, p_hwfn->qm_info.num_vf_pqs, total);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	qed_ilt_cli_blk_fill(p_cli, p_blk,
6498c2ecf20Sopenharmony_ci			     curr_line, total * 0x1000,
6508c2ecf20Sopenharmony_ci			     QM_PQ_ELEMENT_SIZE);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_QM);
6538c2ecf20Sopenharmony_ci	p_cli->pf_total_lines = curr_line - p_blk->start_line;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	/* SRC */
6568c2ecf20Sopenharmony_ci	p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_SRC]);
6578c2ecf20Sopenharmony_ci	qed_cxt_src_iids(p_mngr, &src_iids);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	/* Both the PF and VFs searcher connections are stored in the per PF
6608c2ecf20Sopenharmony_ci	 * database. Thus sum the PF searcher cids and all the VFs searcher
6618c2ecf20Sopenharmony_ci	 * cids.
6628c2ecf20Sopenharmony_ci	 */
6638c2ecf20Sopenharmony_ci	total = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
6648c2ecf20Sopenharmony_ci	if (total) {
6658c2ecf20Sopenharmony_ci		u32 local_max = max_t(u32, total,
6668c2ecf20Sopenharmony_ci				      SRC_MIN_NUM_ELEMS);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci		total = roundup_pow_of_two(local_max);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->pf_blks[0]);
6718c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
6728c2ecf20Sopenharmony_ci				     total * sizeof(struct src_ent),
6738c2ecf20Sopenharmony_ci				     sizeof(struct src_ent));
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6768c2ecf20Sopenharmony_ci				     ILT_CLI_SRC);
6778c2ecf20Sopenharmony_ci		p_cli->pf_total_lines = curr_line - p_blk->start_line;
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	/* TM PF */
6818c2ecf20Sopenharmony_ci	p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_TM]);
6828c2ecf20Sopenharmony_ci	qed_cxt_tm_iids(p_hwfn, p_mngr, &tm_iids);
6838c2ecf20Sopenharmony_ci	total = tm_iids.pf_cids + tm_iids.pf_tids_total;
6848c2ecf20Sopenharmony_ci	if (total) {
6858c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->pf_blks[0]);
6868c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
6878c2ecf20Sopenharmony_ci				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6908c2ecf20Sopenharmony_ci				     ILT_CLI_TM);
6918c2ecf20Sopenharmony_ci		p_cli->pf_total_lines = curr_line - p_blk->start_line;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	/* TM VF */
6958c2ecf20Sopenharmony_ci	total = tm_iids.per_vf_cids + tm_iids.per_vf_tids;
6968c2ecf20Sopenharmony_ci	if (total) {
6978c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->vf_blks[0]);
6988c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
6998c2ecf20Sopenharmony_ci				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
7028c2ecf20Sopenharmony_ci				     ILT_CLI_TM);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		p_cli->vf_total_lines = curr_line - p_blk->start_line;
7058c2ecf20Sopenharmony_ci		for (i = 1; i < p_mngr->vf_count; i++)
7068c2ecf20Sopenharmony_ci			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
7078c2ecf20Sopenharmony_ci					     ILT_CLI_TM);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* TSDM (SRQ CONTEXT) */
7118c2ecf20Sopenharmony_ci	total = qed_cxt_get_total_srq_count(p_hwfn);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	if (total) {
7148c2ecf20Sopenharmony_ci		p_cli = qed_cxt_set_cli(&p_mngr->clients[ILT_CLI_TSDM]);
7158c2ecf20Sopenharmony_ci		p_blk = qed_cxt_set_blk(&p_cli->pf_blks[SRQ_BLK]);
7168c2ecf20Sopenharmony_ci		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
7178c2ecf20Sopenharmony_ci				     total * SRQ_CXT_SIZE, SRQ_CXT_SIZE);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
7208c2ecf20Sopenharmony_ci				     ILT_CLI_TSDM);
7218c2ecf20Sopenharmony_ci		p_cli->pf_total_lines = curr_line - p_blk->start_line;
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	*line_count = curr_line - p_hwfn->p_cxt_mngr->pf_start_line;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	if (curr_line - p_hwfn->p_cxt_mngr->pf_start_line >
7278c2ecf20Sopenharmony_ci	    RESC_NUM(p_hwfn, QED_ILT))
7288c2ecf20Sopenharmony_ci		return -EINVAL;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	return 0;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ciu32 qed_cxt_cfg_ilt_compute_excess(struct qed_hwfn *p_hwfn, u32 used_lines)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
7368c2ecf20Sopenharmony_ci	u32 excess_lines, available_lines;
7378c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr;
7388c2ecf20Sopenharmony_ci	u32 ilt_page_size, elem_size;
7398c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg;
7408c2ecf20Sopenharmony_ci	int i;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	available_lines = RESC_NUM(p_hwfn, QED_ILT);
7438c2ecf20Sopenharmony_ci	excess_lines = used_lines - available_lines;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	if (!excess_lines)
7468c2ecf20Sopenharmony_ci		return 0;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	if (!QED_IS_RDMA_PERSONALITY(p_hwfn))
7498c2ecf20Sopenharmony_ci		return 0;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	p_mngr = p_hwfn->p_cxt_mngr;
7528c2ecf20Sopenharmony_ci	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
7538c2ecf20Sopenharmony_ci	ilt_page_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
7568c2ecf20Sopenharmony_ci		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
7578c2ecf20Sopenharmony_ci		if (!p_seg || p_seg->count == 0)
7588c2ecf20Sopenharmony_ci			continue;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci		elem_size = p_mngr->task_type_size[p_seg->type];
7618c2ecf20Sopenharmony_ci		if (!elem_size)
7628c2ecf20Sopenharmony_ci			continue;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		return (ilt_page_size / elem_size) * excess_lines;
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	DP_NOTICE(p_hwfn, "failed computing excess ILT lines\n");
7688c2ecf20Sopenharmony_ci	return 0;
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic void qed_cxt_src_t2_free(struct qed_hwfn *p_hwfn)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	struct qed_src_t2 *p_t2 = &p_hwfn->p_cxt_mngr->src_t2;
7748c2ecf20Sopenharmony_ci	u32 i;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	if (!p_t2 || !p_t2->dma_mem)
7778c2ecf20Sopenharmony_ci		return;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	for (i = 0; i < p_t2->num_pages; i++)
7808c2ecf20Sopenharmony_ci		if (p_t2->dma_mem[i].virt_addr)
7818c2ecf20Sopenharmony_ci			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
7828c2ecf20Sopenharmony_ci					  p_t2->dma_mem[i].size,
7838c2ecf20Sopenharmony_ci					  p_t2->dma_mem[i].virt_addr,
7848c2ecf20Sopenharmony_ci					  p_t2->dma_mem[i].phys_addr);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	kfree(p_t2->dma_mem);
7878c2ecf20Sopenharmony_ci	p_t2->dma_mem = NULL;
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic int
7918c2ecf20Sopenharmony_ciqed_cxt_t2_alloc_pages(struct qed_hwfn *p_hwfn,
7928c2ecf20Sopenharmony_ci		       struct qed_src_t2 *p_t2, u32 total_size, u32 page_size)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	void **p_virt;
7958c2ecf20Sopenharmony_ci	u32 size, i;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	if (!p_t2 || !p_t2->dma_mem)
7988c2ecf20Sopenharmony_ci		return -EINVAL;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	for (i = 0; i < p_t2->num_pages; i++) {
8018c2ecf20Sopenharmony_ci		size = min_t(u32, total_size, page_size);
8028c2ecf20Sopenharmony_ci		p_virt = &p_t2->dma_mem[i].virt_addr;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci		*p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
8058c2ecf20Sopenharmony_ci					     size,
8068c2ecf20Sopenharmony_ci					     &p_t2->dma_mem[i].phys_addr,
8078c2ecf20Sopenharmony_ci					     GFP_KERNEL);
8088c2ecf20Sopenharmony_ci		if (!p_t2->dma_mem[i].virt_addr)
8098c2ecf20Sopenharmony_ci			return -ENOMEM;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci		memset(*p_virt, 0, size);
8128c2ecf20Sopenharmony_ci		p_t2->dma_mem[i].size = size;
8138c2ecf20Sopenharmony_ci		total_size -= size;
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return 0;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cistatic int qed_cxt_src_t2_alloc(struct qed_hwfn *p_hwfn)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
8228c2ecf20Sopenharmony_ci	u32 conn_num, total_size, ent_per_page, psz, i;
8238c2ecf20Sopenharmony_ci	struct phys_mem_desc *p_t2_last_page;
8248c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_src;
8258c2ecf20Sopenharmony_ci	struct qed_src_iids src_iids;
8268c2ecf20Sopenharmony_ci	struct qed_src_t2 *p_t2;
8278c2ecf20Sopenharmony_ci	int rc;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	memset(&src_iids, 0, sizeof(src_iids));
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/* if the SRC ILT client is inactive - there are no connection
8328c2ecf20Sopenharmony_ci	 * requiring the searcer, leave.
8338c2ecf20Sopenharmony_ci	 */
8348c2ecf20Sopenharmony_ci	p_src = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_SRC];
8358c2ecf20Sopenharmony_ci	if (!p_src->active)
8368c2ecf20Sopenharmony_ci		return 0;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	qed_cxt_src_iids(p_mngr, &src_iids);
8398c2ecf20Sopenharmony_ci	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
8408c2ecf20Sopenharmony_ci	total_size = conn_num * sizeof(struct src_ent);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	/* use the same page size as the SRC ILT client */
8438c2ecf20Sopenharmony_ci	psz = ILT_PAGE_IN_BYTES(p_src->p_size.val);
8448c2ecf20Sopenharmony_ci	p_t2 = &p_mngr->src_t2;
8458c2ecf20Sopenharmony_ci	p_t2->num_pages = DIV_ROUND_UP(total_size, psz);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	/* allocate t2 */
8488c2ecf20Sopenharmony_ci	p_t2->dma_mem = kcalloc(p_t2->num_pages, sizeof(struct phys_mem_desc),
8498c2ecf20Sopenharmony_ci				GFP_KERNEL);
8508c2ecf20Sopenharmony_ci	if (!p_t2->dma_mem) {
8518c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "Failed to allocate t2 table\n");
8528c2ecf20Sopenharmony_ci		rc = -ENOMEM;
8538c2ecf20Sopenharmony_ci		goto t2_fail;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	rc = qed_cxt_t2_alloc_pages(p_hwfn, p_t2, total_size, psz);
8578c2ecf20Sopenharmony_ci	if (rc)
8588c2ecf20Sopenharmony_ci		goto t2_fail;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	/* Set the t2 pointers */
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* entries per page - must be a power of two */
8638c2ecf20Sopenharmony_ci	ent_per_page = psz / sizeof(struct src_ent);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	p_t2->first_free = (u64)p_t2->dma_mem[0].phys_addr;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	p_t2_last_page = &p_t2->dma_mem[(conn_num - 1) / ent_per_page];
8688c2ecf20Sopenharmony_ci	p_t2->last_free = (u64)p_t2_last_page->phys_addr +
8698c2ecf20Sopenharmony_ci	    ((conn_num - 1) & (ent_per_page - 1)) * sizeof(struct src_ent);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	for (i = 0; i < p_t2->num_pages; i++) {
8728c2ecf20Sopenharmony_ci		u32 ent_num = min_t(u32,
8738c2ecf20Sopenharmony_ci				    ent_per_page,
8748c2ecf20Sopenharmony_ci				    conn_num);
8758c2ecf20Sopenharmony_ci		struct src_ent *entries = p_t2->dma_mem[i].virt_addr;
8768c2ecf20Sopenharmony_ci		u64 p_ent_phys = (u64)p_t2->dma_mem[i].phys_addr, val;
8778c2ecf20Sopenharmony_ci		u32 j;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		for (j = 0; j < ent_num - 1; j++) {
8808c2ecf20Sopenharmony_ci			val = p_ent_phys + (j + 1) * sizeof(struct src_ent);
8818c2ecf20Sopenharmony_ci			entries[j].next = cpu_to_be64(val);
8828c2ecf20Sopenharmony_ci		}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci		if (i < p_t2->num_pages - 1)
8858c2ecf20Sopenharmony_ci			val = (u64)p_t2->dma_mem[i + 1].phys_addr;
8868c2ecf20Sopenharmony_ci		else
8878c2ecf20Sopenharmony_ci			val = 0;
8888c2ecf20Sopenharmony_ci		entries[j].next = cpu_to_be64(val);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci		conn_num -= ent_num;
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	return 0;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cit2_fail:
8968c2ecf20Sopenharmony_ci	qed_cxt_src_t2_free(p_hwfn);
8978c2ecf20Sopenharmony_ci	return rc;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci#define for_each_ilt_valid_client(pos, clients)	\
9018c2ecf20Sopenharmony_ci	for (pos = 0; pos < MAX_ILT_CLIENTS; pos++)	\
9028c2ecf20Sopenharmony_ci		if (!clients[pos].active) {	\
9038c2ecf20Sopenharmony_ci			continue;		\
9048c2ecf20Sopenharmony_ci		} else				\
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/* Total number of ILT lines used by this PF */
9078c2ecf20Sopenharmony_cistatic u32 qed_cxt_ilt_shadow_size(struct qed_ilt_client_cfg *ilt_clients)
9088c2ecf20Sopenharmony_ci{
9098c2ecf20Sopenharmony_ci	u32 size = 0;
9108c2ecf20Sopenharmony_ci	u32 i;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	for_each_ilt_valid_client(i, ilt_clients)
9138c2ecf20Sopenharmony_ci	    size += (ilt_clients[i].last.val - ilt_clients[i].first.val + 1);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	return size;
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic void qed_ilt_shadow_free(struct qed_hwfn *p_hwfn)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli = p_hwfn->p_cxt_mngr->clients;
9218c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
9228c2ecf20Sopenharmony_ci	u32 ilt_size, i;
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	ilt_size = qed_cxt_ilt_shadow_size(p_cli);
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	for (i = 0; p_mngr->ilt_shadow && i < ilt_size; i++) {
9278c2ecf20Sopenharmony_ci		struct phys_mem_desc *p_dma = &p_mngr->ilt_shadow[i];
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci		if (p_dma->virt_addr)
9308c2ecf20Sopenharmony_ci			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
9318c2ecf20Sopenharmony_ci					  p_dma->size, p_dma->virt_addr,
9328c2ecf20Sopenharmony_ci					  p_dma->phys_addr);
9338c2ecf20Sopenharmony_ci		p_dma->virt_addr = NULL;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci	kfree(p_mngr->ilt_shadow);
9368c2ecf20Sopenharmony_ci	p_mngr->ilt_shadow = NULL;
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic int qed_ilt_blk_alloc(struct qed_hwfn *p_hwfn,
9408c2ecf20Sopenharmony_ci			     struct qed_ilt_cli_blk *p_blk,
9418c2ecf20Sopenharmony_ci			     enum ilt_clients ilt_client,
9428c2ecf20Sopenharmony_ci			     u32 start_line_offset)
9438c2ecf20Sopenharmony_ci{
9448c2ecf20Sopenharmony_ci	struct phys_mem_desc *ilt_shadow = p_hwfn->p_cxt_mngr->ilt_shadow;
9458c2ecf20Sopenharmony_ci	u32 lines, line, sz_left, lines_to_skip = 0;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	/* Special handling for RoCE that supports dynamic allocation */
9488c2ecf20Sopenharmony_ci	if (QED_IS_RDMA_PERSONALITY(p_hwfn) &&
9498c2ecf20Sopenharmony_ci	    ((ilt_client == ILT_CLI_CDUT) || ilt_client == ILT_CLI_TSDM))
9508c2ecf20Sopenharmony_ci		return 0;
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	lines_to_skip = p_blk->dynamic_line_cnt;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	if (!p_blk->total_size)
9558c2ecf20Sopenharmony_ci		return 0;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	sz_left = p_blk->total_size;
9588c2ecf20Sopenharmony_ci	lines = DIV_ROUND_UP(sz_left, p_blk->real_size_in_page) - lines_to_skip;
9598c2ecf20Sopenharmony_ci	line = p_blk->start_line + start_line_offset -
9608c2ecf20Sopenharmony_ci	    p_hwfn->p_cxt_mngr->pf_start_line + lines_to_skip;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	for (; lines; lines--) {
9638c2ecf20Sopenharmony_ci		dma_addr_t p_phys;
9648c2ecf20Sopenharmony_ci		void *p_virt;
9658c2ecf20Sopenharmony_ci		u32 size;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci		size = min_t(u32, sz_left, p_blk->real_size_in_page);
9688c2ecf20Sopenharmony_ci		p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev, size,
9698c2ecf20Sopenharmony_ci					    &p_phys, GFP_KERNEL);
9708c2ecf20Sopenharmony_ci		if (!p_virt)
9718c2ecf20Sopenharmony_ci			return -ENOMEM;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci		ilt_shadow[line].phys_addr = p_phys;
9748c2ecf20Sopenharmony_ci		ilt_shadow[line].virt_addr = p_virt;
9758c2ecf20Sopenharmony_ci		ilt_shadow[line].size = size;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci		DP_VERBOSE(p_hwfn, QED_MSG_ILT,
9788c2ecf20Sopenharmony_ci			   "ILT shadow: Line [%d] Physical 0x%llx Virtual %p Size %d\n",
9798c2ecf20Sopenharmony_ci			    line, (u64)p_phys, p_virt, size);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci		sz_left -= size;
9828c2ecf20Sopenharmony_ci		line++;
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return 0;
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cistatic int qed_ilt_shadow_alloc(struct qed_hwfn *p_hwfn)
9898c2ecf20Sopenharmony_ci{
9908c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
9918c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *clients = p_mngr->clients;
9928c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
9938c2ecf20Sopenharmony_ci	u32 size, i, j, k;
9948c2ecf20Sopenharmony_ci	int rc;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	size = qed_cxt_ilt_shadow_size(clients);
9978c2ecf20Sopenharmony_ci	p_mngr->ilt_shadow = kcalloc(size, sizeof(struct phys_mem_desc),
9988c2ecf20Sopenharmony_ci				     GFP_KERNEL);
9998c2ecf20Sopenharmony_ci	if (!p_mngr->ilt_shadow) {
10008c2ecf20Sopenharmony_ci		rc = -ENOMEM;
10018c2ecf20Sopenharmony_ci		goto ilt_shadow_fail;
10028c2ecf20Sopenharmony_ci	}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
10058c2ecf20Sopenharmony_ci		   "Allocated 0x%x bytes for ilt shadow\n",
10068c2ecf20Sopenharmony_ci		   (u32)(size * sizeof(struct phys_mem_desc)));
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	for_each_ilt_valid_client(i, clients) {
10098c2ecf20Sopenharmony_ci		for (j = 0; j < ILT_CLI_PF_BLOCKS; j++) {
10108c2ecf20Sopenharmony_ci			p_blk = &clients[i].pf_blks[j];
10118c2ecf20Sopenharmony_ci			rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, 0);
10128c2ecf20Sopenharmony_ci			if (rc)
10138c2ecf20Sopenharmony_ci				goto ilt_shadow_fail;
10148c2ecf20Sopenharmony_ci		}
10158c2ecf20Sopenharmony_ci		for (k = 0; k < p_mngr->vf_count; k++) {
10168c2ecf20Sopenharmony_ci			for (j = 0; j < ILT_CLI_VF_BLOCKS; j++) {
10178c2ecf20Sopenharmony_ci				u32 lines = clients[i].vf_total_lines * k;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci				p_blk = &clients[i].vf_blks[j];
10208c2ecf20Sopenharmony_ci				rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, lines);
10218c2ecf20Sopenharmony_ci				if (rc)
10228c2ecf20Sopenharmony_ci					goto ilt_shadow_fail;
10238c2ecf20Sopenharmony_ci			}
10248c2ecf20Sopenharmony_ci		}
10258c2ecf20Sopenharmony_ci	}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	return 0;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ciilt_shadow_fail:
10308c2ecf20Sopenharmony_ci	qed_ilt_shadow_free(p_hwfn);
10318c2ecf20Sopenharmony_ci	return rc;
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_cistatic void qed_cid_map_free(struct qed_hwfn *p_hwfn)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
10378c2ecf20Sopenharmony_ci	u32 type, vf;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	for (type = 0; type < MAX_CONN_TYPES; type++) {
10408c2ecf20Sopenharmony_ci		kfree(p_mngr->acquired[type].cid_map);
10418c2ecf20Sopenharmony_ci		p_mngr->acquired[type].max_count = 0;
10428c2ecf20Sopenharmony_ci		p_mngr->acquired[type].start_cid = 0;
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		for (vf = 0; vf < MAX_NUM_VFS; vf++) {
10458c2ecf20Sopenharmony_ci			kfree(p_mngr->acquired_vf[type][vf].cid_map);
10468c2ecf20Sopenharmony_ci			p_mngr->acquired_vf[type][vf].max_count = 0;
10478c2ecf20Sopenharmony_ci			p_mngr->acquired_vf[type][vf].start_cid = 0;
10488c2ecf20Sopenharmony_ci		}
10498c2ecf20Sopenharmony_ci	}
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_cistatic int
10538c2ecf20Sopenharmony_ciqed_cid_map_alloc_single(struct qed_hwfn *p_hwfn,
10548c2ecf20Sopenharmony_ci			 u32 type,
10558c2ecf20Sopenharmony_ci			 u32 cid_start,
10568c2ecf20Sopenharmony_ci			 u32 cid_count, struct qed_cid_acquired_map *p_map)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	u32 size;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	if (!cid_count)
10618c2ecf20Sopenharmony_ci		return 0;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	size = DIV_ROUND_UP(cid_count,
10648c2ecf20Sopenharmony_ci			    sizeof(unsigned long) * BITS_PER_BYTE) *
10658c2ecf20Sopenharmony_ci	       sizeof(unsigned long);
10668c2ecf20Sopenharmony_ci	p_map->cid_map = kzalloc(size, GFP_KERNEL);
10678c2ecf20Sopenharmony_ci	if (!p_map->cid_map)
10688c2ecf20Sopenharmony_ci		return -ENOMEM;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	p_map->max_count = cid_count;
10718c2ecf20Sopenharmony_ci	p_map->start_cid = cid_start;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_CXT,
10748c2ecf20Sopenharmony_ci		   "Type %08x start: %08x count %08x\n",
10758c2ecf20Sopenharmony_ci		   type, p_map->start_cid, p_map->max_count);
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	return 0;
10788c2ecf20Sopenharmony_ci}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_cistatic int qed_cid_map_alloc(struct qed_hwfn *p_hwfn)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
10838c2ecf20Sopenharmony_ci	u32 start_cid = 0, vf_start_cid = 0;
10848c2ecf20Sopenharmony_ci	u32 type, vf;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	for (type = 0; type < MAX_CONN_TYPES; type++) {
10878c2ecf20Sopenharmony_ci		struct qed_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[type];
10888c2ecf20Sopenharmony_ci		struct qed_cid_acquired_map *p_map;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci		/* Handle PF maps */
10918c2ecf20Sopenharmony_ci		p_map = &p_mngr->acquired[type];
10928c2ecf20Sopenharmony_ci		if (qed_cid_map_alloc_single(p_hwfn, type, start_cid,
10938c2ecf20Sopenharmony_ci					     p_cfg->cid_count, p_map))
10948c2ecf20Sopenharmony_ci			goto cid_map_fail;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci		/* Handle VF maps */
10978c2ecf20Sopenharmony_ci		for (vf = 0; vf < MAX_NUM_VFS; vf++) {
10988c2ecf20Sopenharmony_ci			p_map = &p_mngr->acquired_vf[type][vf];
10998c2ecf20Sopenharmony_ci			if (qed_cid_map_alloc_single(p_hwfn, type,
11008c2ecf20Sopenharmony_ci						     vf_start_cid,
11018c2ecf20Sopenharmony_ci						     p_cfg->cids_per_vf, p_map))
11028c2ecf20Sopenharmony_ci				goto cid_map_fail;
11038c2ecf20Sopenharmony_ci		}
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci		start_cid += p_cfg->cid_count;
11068c2ecf20Sopenharmony_ci		vf_start_cid += p_cfg->cids_per_vf;
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	return 0;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_cicid_map_fail:
11128c2ecf20Sopenharmony_ci	qed_cid_map_free(p_hwfn);
11138c2ecf20Sopenharmony_ci	return -ENOMEM;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ciint qed_cxt_mngr_alloc(struct qed_hwfn *p_hwfn)
11178c2ecf20Sopenharmony_ci{
11188c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *clients;
11198c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr;
11208c2ecf20Sopenharmony_ci	u32 i;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	p_mngr = kzalloc(sizeof(*p_mngr), GFP_KERNEL);
11238c2ecf20Sopenharmony_ci	if (!p_mngr)
11248c2ecf20Sopenharmony_ci		return -ENOMEM;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	/* Initialize ILT client registers */
11278c2ecf20Sopenharmony_ci	clients = p_mngr->clients;
11288c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUC].first.reg = ILT_CFG_REG(CDUC, FIRST_ILT);
11298c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUC].last.reg = ILT_CFG_REG(CDUC, LAST_ILT);
11308c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUC].p_size.reg = ILT_CFG_REG(CDUC, P_SIZE);
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	clients[ILT_CLI_QM].first.reg = ILT_CFG_REG(QM, FIRST_ILT);
11338c2ecf20Sopenharmony_ci	clients[ILT_CLI_QM].last.reg = ILT_CFG_REG(QM, LAST_ILT);
11348c2ecf20Sopenharmony_ci	clients[ILT_CLI_QM].p_size.reg = ILT_CFG_REG(QM, P_SIZE);
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	clients[ILT_CLI_TM].first.reg = ILT_CFG_REG(TM, FIRST_ILT);
11378c2ecf20Sopenharmony_ci	clients[ILT_CLI_TM].last.reg = ILT_CFG_REG(TM, LAST_ILT);
11388c2ecf20Sopenharmony_ci	clients[ILT_CLI_TM].p_size.reg = ILT_CFG_REG(TM, P_SIZE);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	clients[ILT_CLI_SRC].first.reg = ILT_CFG_REG(SRC, FIRST_ILT);
11418c2ecf20Sopenharmony_ci	clients[ILT_CLI_SRC].last.reg = ILT_CFG_REG(SRC, LAST_ILT);
11428c2ecf20Sopenharmony_ci	clients[ILT_CLI_SRC].p_size.reg = ILT_CFG_REG(SRC, P_SIZE);
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUT].first.reg = ILT_CFG_REG(CDUT, FIRST_ILT);
11458c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUT].last.reg = ILT_CFG_REG(CDUT, LAST_ILT);
11468c2ecf20Sopenharmony_ci	clients[ILT_CLI_CDUT].p_size.reg = ILT_CFG_REG(CDUT, P_SIZE);
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	clients[ILT_CLI_TSDM].first.reg = ILT_CFG_REG(TSDM, FIRST_ILT);
11498c2ecf20Sopenharmony_ci	clients[ILT_CLI_TSDM].last.reg = ILT_CFG_REG(TSDM, LAST_ILT);
11508c2ecf20Sopenharmony_ci	clients[ILT_CLI_TSDM].p_size.reg = ILT_CFG_REG(TSDM, P_SIZE);
11518c2ecf20Sopenharmony_ci	/* default ILT page size for all clients is 64K */
11528c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_ILT_CLIENTS; i++)
11538c2ecf20Sopenharmony_ci		p_mngr->clients[i].p_size.val = ILT_DEFAULT_HW_P_SIZE;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	p_mngr->conn_ctx_size = CONN_CXT_SIZE(p_hwfn);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	/* Initialize task sizes */
11588c2ecf20Sopenharmony_ci	p_mngr->task_type_size[0] = TYPE0_TASK_CXT_SIZE(p_hwfn);
11598c2ecf20Sopenharmony_ci	p_mngr->task_type_size[1] = TYPE1_TASK_CXT_SIZE(p_hwfn);
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	if (p_hwfn->cdev->p_iov_info) {
11628c2ecf20Sopenharmony_ci		p_mngr->vf_count = p_hwfn->cdev->p_iov_info->total_vfs;
11638c2ecf20Sopenharmony_ci		p_mngr->first_vf_in_pf =
11648c2ecf20Sopenharmony_ci			p_hwfn->cdev->p_iov_info->first_vf_in_pf;
11658c2ecf20Sopenharmony_ci	}
11668c2ecf20Sopenharmony_ci	/* Initialize the dynamic ILT allocation mutex */
11678c2ecf20Sopenharmony_ci	mutex_init(&p_mngr->mutex);
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	/* Set the cxt mangr pointer priori to further allocations */
11708c2ecf20Sopenharmony_ci	p_hwfn->p_cxt_mngr = p_mngr;
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	return 0;
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ciint qed_cxt_tables_alloc(struct qed_hwfn *p_hwfn)
11768c2ecf20Sopenharmony_ci{
11778c2ecf20Sopenharmony_ci	int rc;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	/* Allocate the ILT shadow table */
11808c2ecf20Sopenharmony_ci	rc = qed_ilt_shadow_alloc(p_hwfn);
11818c2ecf20Sopenharmony_ci	if (rc)
11828c2ecf20Sopenharmony_ci		goto tables_alloc_fail;
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	/* Allocate the T2  table */
11858c2ecf20Sopenharmony_ci	rc = qed_cxt_src_t2_alloc(p_hwfn);
11868c2ecf20Sopenharmony_ci	if (rc)
11878c2ecf20Sopenharmony_ci		goto tables_alloc_fail;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	/* Allocate and initialize the acquired cids bitmaps */
11908c2ecf20Sopenharmony_ci	rc = qed_cid_map_alloc(p_hwfn);
11918c2ecf20Sopenharmony_ci	if (rc)
11928c2ecf20Sopenharmony_ci		goto tables_alloc_fail;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	return 0;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_citables_alloc_fail:
11978c2ecf20Sopenharmony_ci	qed_cxt_mngr_free(p_hwfn);
11988c2ecf20Sopenharmony_ci	return rc;
11998c2ecf20Sopenharmony_ci}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_civoid qed_cxt_mngr_free(struct qed_hwfn *p_hwfn)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	if (!p_hwfn->p_cxt_mngr)
12048c2ecf20Sopenharmony_ci		return;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	qed_cid_map_free(p_hwfn);
12078c2ecf20Sopenharmony_ci	qed_cxt_src_t2_free(p_hwfn);
12088c2ecf20Sopenharmony_ci	qed_ilt_shadow_free(p_hwfn);
12098c2ecf20Sopenharmony_ci	kfree(p_hwfn->p_cxt_mngr);
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	p_hwfn->p_cxt_mngr = NULL;
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_civoid qed_cxt_mngr_setup(struct qed_hwfn *p_hwfn)
12158c2ecf20Sopenharmony_ci{
12168c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
12178c2ecf20Sopenharmony_ci	struct qed_cid_acquired_map *p_map;
12188c2ecf20Sopenharmony_ci	struct qed_conn_type_cfg *p_cfg;
12198c2ecf20Sopenharmony_ci	int type;
12208c2ecf20Sopenharmony_ci	u32 len;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	/* Reset acquired cids */
12238c2ecf20Sopenharmony_ci	for (type = 0; type < MAX_CONN_TYPES; type++) {
12248c2ecf20Sopenharmony_ci		u32 vf;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci		p_cfg = &p_mngr->conn_cfg[type];
12278c2ecf20Sopenharmony_ci		if (p_cfg->cid_count) {
12288c2ecf20Sopenharmony_ci			p_map = &p_mngr->acquired[type];
12298c2ecf20Sopenharmony_ci			len = DIV_ROUND_UP(p_map->max_count,
12308c2ecf20Sopenharmony_ci					   sizeof(unsigned long) *
12318c2ecf20Sopenharmony_ci					   BITS_PER_BYTE) *
12328c2ecf20Sopenharmony_ci			      sizeof(unsigned long);
12338c2ecf20Sopenharmony_ci			memset(p_map->cid_map, 0, len);
12348c2ecf20Sopenharmony_ci		}
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci		if (!p_cfg->cids_per_vf)
12378c2ecf20Sopenharmony_ci			continue;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci		for (vf = 0; vf < MAX_NUM_VFS; vf++) {
12408c2ecf20Sopenharmony_ci			p_map = &p_mngr->acquired_vf[type][vf];
12418c2ecf20Sopenharmony_ci			len = DIV_ROUND_UP(p_map->max_count,
12428c2ecf20Sopenharmony_ci					   sizeof(unsigned long) *
12438c2ecf20Sopenharmony_ci					   BITS_PER_BYTE) *
12448c2ecf20Sopenharmony_ci			      sizeof(unsigned long);
12458c2ecf20Sopenharmony_ci			memset(p_map->cid_map, 0, len);
12468c2ecf20Sopenharmony_ci		}
12478c2ecf20Sopenharmony_ci	}
12488c2ecf20Sopenharmony_ci}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci/* CDU Common */
12518c2ecf20Sopenharmony_ci#define CDUC_CXT_SIZE_SHIFT \
12528c2ecf20Sopenharmony_ci	CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE_SHIFT
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci#define CDUC_CXT_SIZE_MASK \
12558c2ecf20Sopenharmony_ci	(CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE >> CDUC_CXT_SIZE_SHIFT)
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci#define CDUC_BLOCK_WASTE_SHIFT \
12588c2ecf20Sopenharmony_ci	CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE_SHIFT
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci#define CDUC_BLOCK_WASTE_MASK \
12618c2ecf20Sopenharmony_ci	(CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE >> CDUC_BLOCK_WASTE_SHIFT)
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci#define CDUC_NCIB_SHIFT	\
12648c2ecf20Sopenharmony_ci	CDU_REG_CID_ADDR_PARAMS_NCIB_SHIFT
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci#define CDUC_NCIB_MASK \
12678c2ecf20Sopenharmony_ci	(CDU_REG_CID_ADDR_PARAMS_NCIB >> CDUC_NCIB_SHIFT)
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci#define CDUT_TYPE0_CXT_SIZE_SHIFT \
12708c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE_SHIFT
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci#define CDUT_TYPE0_CXT_SIZE_MASK		\
12738c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE >>	\
12748c2ecf20Sopenharmony_ci	 CDUT_TYPE0_CXT_SIZE_SHIFT)
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci#define CDUT_TYPE0_BLOCK_WASTE_SHIFT \
12778c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE_SHIFT
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci#define CDUT_TYPE0_BLOCK_WASTE_MASK		       \
12808c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE >> \
12818c2ecf20Sopenharmony_ci	 CDUT_TYPE0_BLOCK_WASTE_SHIFT)
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci#define CDUT_TYPE0_NCIB_SHIFT \
12848c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK_SHIFT
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci#define CDUT_TYPE0_NCIB_MASK				 \
12878c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK >> \
12888c2ecf20Sopenharmony_ci	 CDUT_TYPE0_NCIB_SHIFT)
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci#define CDUT_TYPE1_CXT_SIZE_SHIFT \
12918c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE_SHIFT
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci#define CDUT_TYPE1_CXT_SIZE_MASK		\
12948c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE >>	\
12958c2ecf20Sopenharmony_ci	 CDUT_TYPE1_CXT_SIZE_SHIFT)
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci#define CDUT_TYPE1_BLOCK_WASTE_SHIFT \
12988c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE_SHIFT
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci#define CDUT_TYPE1_BLOCK_WASTE_MASK		       \
13018c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE >> \
13028c2ecf20Sopenharmony_ci	 CDUT_TYPE1_BLOCK_WASTE_SHIFT)
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci#define CDUT_TYPE1_NCIB_SHIFT \
13058c2ecf20Sopenharmony_ci	CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK_SHIFT
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci#define CDUT_TYPE1_NCIB_MASK				 \
13088c2ecf20Sopenharmony_ci	(CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK >> \
13098c2ecf20Sopenharmony_ci	 CDUT_TYPE1_NCIB_SHIFT)
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_cistatic void qed_cdu_init_common(struct qed_hwfn *p_hwfn)
13128c2ecf20Sopenharmony_ci{
13138c2ecf20Sopenharmony_ci	u32 page_sz, elems_per_page, block_waste, cxt_size, cdu_params = 0;
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	/* CDUC - connection configuration */
13168c2ecf20Sopenharmony_ci	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
13178c2ecf20Sopenharmony_ci	cxt_size = CONN_CXT_SIZE(p_hwfn);
13188c2ecf20Sopenharmony_ci	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
13198c2ecf20Sopenharmony_ci	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUC_CXT_SIZE, cxt_size);
13228c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUC_BLOCK_WASTE, block_waste);
13238c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUC_NCIB, elems_per_page);
13248c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, CDU_REG_CID_ADDR_PARAMS_RT_OFFSET, cdu_params);
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	/* CDUT - type-0 tasks configuration */
13278c2ecf20Sopenharmony_ci	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT].p_size.val;
13288c2ecf20Sopenharmony_ci	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[0];
13298c2ecf20Sopenharmony_ci	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
13308c2ecf20Sopenharmony_ci	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	/* cxt size and block-waste are multipes of 8 */
13338c2ecf20Sopenharmony_ci	cdu_params = 0;
13348c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE0_CXT_SIZE, (cxt_size >> 3));
13358c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE0_BLOCK_WASTE, (block_waste >> 3));
13368c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE0_NCIB, elems_per_page);
13378c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT0_PARAMS_RT_OFFSET, cdu_params);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	/* CDUT - type-1 tasks configuration */
13408c2ecf20Sopenharmony_ci	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[1];
13418c2ecf20Sopenharmony_ci	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
13428c2ecf20Sopenharmony_ci	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	/* cxt size and block-waste are multipes of 8 */
13458c2ecf20Sopenharmony_ci	cdu_params = 0;
13468c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE1_CXT_SIZE, (cxt_size >> 3));
13478c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE1_BLOCK_WASTE, (block_waste >> 3));
13488c2ecf20Sopenharmony_ci	SET_FIELD(cdu_params, CDUT_TYPE1_NCIB, elems_per_page);
13498c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT1_PARAMS_RT_OFFSET, cdu_params);
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci/* CDU PF */
13538c2ecf20Sopenharmony_ci#define CDU_SEG_REG_TYPE_SHIFT          CDU_SEG_TYPE_OFFSET_REG_TYPE_SHIFT
13548c2ecf20Sopenharmony_ci#define CDU_SEG_REG_TYPE_MASK           0x1
13558c2ecf20Sopenharmony_ci#define CDU_SEG_REG_OFFSET_SHIFT        0
13568c2ecf20Sopenharmony_ci#define CDU_SEG_REG_OFFSET_MASK         CDU_SEG_TYPE_OFFSET_REG_OFFSET_MASK
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_cistatic void qed_cdu_init_pf(struct qed_hwfn *p_hwfn)
13598c2ecf20Sopenharmony_ci{
13608c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
13618c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg;
13628c2ecf20Sopenharmony_ci	u32 cdu_seg_params, offset;
13638c2ecf20Sopenharmony_ci	int i;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	static const u32 rt_type_offset_arr[] = {
13668c2ecf20Sopenharmony_ci		CDU_REG_PF_SEG0_TYPE_OFFSET_RT_OFFSET,
13678c2ecf20Sopenharmony_ci		CDU_REG_PF_SEG1_TYPE_OFFSET_RT_OFFSET,
13688c2ecf20Sopenharmony_ci		CDU_REG_PF_SEG2_TYPE_OFFSET_RT_OFFSET,
13698c2ecf20Sopenharmony_ci		CDU_REG_PF_SEG3_TYPE_OFFSET_RT_OFFSET
13708c2ecf20Sopenharmony_ci	};
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	static const u32 rt_type_offset_fl_arr[] = {
13738c2ecf20Sopenharmony_ci		CDU_REG_PF_FL_SEG0_TYPE_OFFSET_RT_OFFSET,
13748c2ecf20Sopenharmony_ci		CDU_REG_PF_FL_SEG1_TYPE_OFFSET_RT_OFFSET,
13758c2ecf20Sopenharmony_ci		CDU_REG_PF_FL_SEG2_TYPE_OFFSET_RT_OFFSET,
13768c2ecf20Sopenharmony_ci		CDU_REG_PF_FL_SEG3_TYPE_OFFSET_RT_OFFSET
13778c2ecf20Sopenharmony_ci	};
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	/* There are initializations only for CDUT during pf Phase */
13828c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
13838c2ecf20Sopenharmony_ci		/* Segment 0 */
13848c2ecf20Sopenharmony_ci		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
13858c2ecf20Sopenharmony_ci		if (!p_seg)
13868c2ecf20Sopenharmony_ci			continue;
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci		/* Note: start_line is already adjusted for the CDU
13898c2ecf20Sopenharmony_ci		 * segment register granularity, so we just need to
13908c2ecf20Sopenharmony_ci		 * divide. Adjustment is implicit as we assume ILT
13918c2ecf20Sopenharmony_ci		 * Page size is larger than 32K!
13928c2ecf20Sopenharmony_ci		 */
13938c2ecf20Sopenharmony_ci		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
13948c2ecf20Sopenharmony_ci			  (p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line -
13958c2ecf20Sopenharmony_ci			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci		cdu_seg_params = 0;
13988c2ecf20Sopenharmony_ci		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
13998c2ecf20Sopenharmony_ci		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
14008c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn, rt_type_offset_arr[i], cdu_seg_params);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
14038c2ecf20Sopenharmony_ci			  (p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)].start_line -
14048c2ecf20Sopenharmony_ci			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci		cdu_seg_params = 0;
14078c2ecf20Sopenharmony_ci		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
14088c2ecf20Sopenharmony_ci		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
14098c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn, rt_type_offset_fl_arr[i], cdu_seg_params);
14108c2ecf20Sopenharmony_ci	}
14118c2ecf20Sopenharmony_ci}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_civoid qed_qm_init_pf(struct qed_hwfn *p_hwfn,
14148c2ecf20Sopenharmony_ci		    struct qed_ptt *p_ptt, bool is_pf_loading)
14158c2ecf20Sopenharmony_ci{
14168c2ecf20Sopenharmony_ci	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
14178c2ecf20Sopenharmony_ci	struct qed_qm_pf_rt_init_params params;
14188c2ecf20Sopenharmony_ci	struct qed_qm_iids iids;
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	memset(&iids, 0, sizeof(iids));
14218c2ecf20Sopenharmony_ci	qed_cxt_qm_iids(p_hwfn, &iids);
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	memset(&params, 0, sizeof(params));
14248c2ecf20Sopenharmony_ci	params.port_id = p_hwfn->port_id;
14258c2ecf20Sopenharmony_ci	params.pf_id = p_hwfn->rel_pf_id;
14268c2ecf20Sopenharmony_ci	params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
14278c2ecf20Sopenharmony_ci	params.is_pf_loading = is_pf_loading;
14288c2ecf20Sopenharmony_ci	params.num_pf_cids = iids.cids;
14298c2ecf20Sopenharmony_ci	params.num_vf_cids = iids.vf_cids;
14308c2ecf20Sopenharmony_ci	params.num_tids = iids.tids;
14318c2ecf20Sopenharmony_ci	params.start_pq = qm_info->start_pq;
14328c2ecf20Sopenharmony_ci	params.num_pf_pqs = qm_info->num_pqs - qm_info->num_vf_pqs;
14338c2ecf20Sopenharmony_ci	params.num_vf_pqs = qm_info->num_vf_pqs;
14348c2ecf20Sopenharmony_ci	params.start_vport = qm_info->start_vport;
14358c2ecf20Sopenharmony_ci	params.num_vports = qm_info->num_vports;
14368c2ecf20Sopenharmony_ci	params.pf_wfq = qm_info->pf_wfq;
14378c2ecf20Sopenharmony_ci	params.pf_rl = qm_info->pf_rl;
14388c2ecf20Sopenharmony_ci	params.pq_params = qm_info->qm_pq_params;
14398c2ecf20Sopenharmony_ci	params.vport_params = qm_info->qm_vport_params;
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	qed_qm_pf_rt_init(p_hwfn, p_ptt, &params);
14428c2ecf20Sopenharmony_ci}
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci/* CM PF */
14458c2ecf20Sopenharmony_cistatic void qed_cm_init_pf(struct qed_hwfn *p_hwfn)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	/* XCM pure-LB queue */
14488c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, XCM_REG_CON_PHY_Q3_RT_OFFSET,
14498c2ecf20Sopenharmony_ci		     qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB));
14508c2ecf20Sopenharmony_ci}
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci/* DQ PF */
14538c2ecf20Sopenharmony_cistatic void qed_dq_init_pf(struct qed_hwfn *p_hwfn)
14548c2ecf20Sopenharmony_ci{
14558c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
14568c2ecf20Sopenharmony_ci	u32 dq_pf_max_cid = 0, dq_vf_max_cid = 0;
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[0].cid_count >> DQ_RANGE_SHIFT);
14598c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_0_RT_OFFSET, dq_pf_max_cid);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[0].cids_per_vf >> DQ_RANGE_SHIFT);
14628c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_0_RT_OFFSET, dq_vf_max_cid);
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[1].cid_count >> DQ_RANGE_SHIFT);
14658c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_1_RT_OFFSET, dq_pf_max_cid);
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[1].cids_per_vf >> DQ_RANGE_SHIFT);
14688c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_1_RT_OFFSET, dq_vf_max_cid);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[2].cid_count >> DQ_RANGE_SHIFT);
14718c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_2_RT_OFFSET, dq_pf_max_cid);
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[2].cids_per_vf >> DQ_RANGE_SHIFT);
14748c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_2_RT_OFFSET, dq_vf_max_cid);
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[3].cid_count >> DQ_RANGE_SHIFT);
14778c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_3_RT_OFFSET, dq_pf_max_cid);
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[3].cids_per_vf >> DQ_RANGE_SHIFT);
14808c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_3_RT_OFFSET, dq_vf_max_cid);
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[4].cid_count >> DQ_RANGE_SHIFT);
14838c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_4_RT_OFFSET, dq_pf_max_cid);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[4].cids_per_vf >> DQ_RANGE_SHIFT);
14868c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_4_RT_OFFSET, dq_vf_max_cid);
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	dq_pf_max_cid += (p_mngr->conn_cfg[5].cid_count >> DQ_RANGE_SHIFT);
14898c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_5_RT_OFFSET, dq_pf_max_cid);
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	dq_vf_max_cid += (p_mngr->conn_cfg[5].cids_per_vf >> DQ_RANGE_SHIFT);
14928c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_5_RT_OFFSET, dq_vf_max_cid);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	/* Connection types 6 & 7 are not in use, yet they must be configured
14958c2ecf20Sopenharmony_ci	 * as the highest possible connection. Not configuring them means the
14968c2ecf20Sopenharmony_ci	 * defaults will be  used, and with a large number of cids a bug may
14978c2ecf20Sopenharmony_ci	 * occur, if the defaults will be smaller than dq_pf_max_cid /
14988c2ecf20Sopenharmony_ci	 * dq_vf_max_cid.
14998c2ecf20Sopenharmony_ci	 */
15008c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_6_RT_OFFSET, dq_pf_max_cid);
15018c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_6_RT_OFFSET, dq_vf_max_cid);
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_7_RT_OFFSET, dq_pf_max_cid);
15048c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_7_RT_OFFSET, dq_vf_max_cid);
15058c2ecf20Sopenharmony_ci}
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_cistatic void qed_ilt_bounds_init(struct qed_hwfn *p_hwfn)
15088c2ecf20Sopenharmony_ci{
15098c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *ilt_clients;
15108c2ecf20Sopenharmony_ci	int i;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	ilt_clients = p_hwfn->p_cxt_mngr->clients;
15138c2ecf20Sopenharmony_ci	for_each_ilt_valid_client(i, ilt_clients) {
15148c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15158c2ecf20Sopenharmony_ci			     ilt_clients[i].first.reg,
15168c2ecf20Sopenharmony_ci			     ilt_clients[i].first.val);
15178c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15188c2ecf20Sopenharmony_ci			     ilt_clients[i].last.reg, ilt_clients[i].last.val);
15198c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15208c2ecf20Sopenharmony_ci			     ilt_clients[i].p_size.reg,
15218c2ecf20Sopenharmony_ci			     ilt_clients[i].p_size.val);
15228c2ecf20Sopenharmony_ci	}
15238c2ecf20Sopenharmony_ci}
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_cistatic void qed_ilt_vf_bounds_init(struct qed_hwfn *p_hwfn)
15268c2ecf20Sopenharmony_ci{
15278c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
15288c2ecf20Sopenharmony_ci	u32 blk_factor;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	/* For simplicty  we set the 'block' to be an ILT page */
15318c2ecf20Sopenharmony_ci	if (p_hwfn->cdev->p_iov_info) {
15328c2ecf20Sopenharmony_ci		struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15358c2ecf20Sopenharmony_ci			     PSWRQ2_REG_VF_BASE_RT_OFFSET,
15368c2ecf20Sopenharmony_ci			     p_iov->first_vf_in_pf);
15378c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15388c2ecf20Sopenharmony_ci			     PSWRQ2_REG_VF_LAST_ILT_RT_OFFSET,
15398c2ecf20Sopenharmony_ci			     p_iov->first_vf_in_pf + p_iov->total_vfs);
15408c2ecf20Sopenharmony_ci	}
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
15438c2ecf20Sopenharmony_ci	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
15448c2ecf20Sopenharmony_ci	if (p_cli->active) {
15458c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15468c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUC_BLOCKS_FACTOR_RT_OFFSET,
15478c2ecf20Sopenharmony_ci			     blk_factor);
15488c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15498c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUC_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
15508c2ecf20Sopenharmony_ci			     p_cli->pf_total_lines);
15518c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15528c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUC_VF_BLOCKS_RT_OFFSET,
15538c2ecf20Sopenharmony_ci			     p_cli->vf_total_lines);
15548c2ecf20Sopenharmony_ci	}
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
15578c2ecf20Sopenharmony_ci	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
15588c2ecf20Sopenharmony_ci	if (p_cli->active) {
15598c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15608c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUT_BLOCKS_FACTOR_RT_OFFSET,
15618c2ecf20Sopenharmony_ci			     blk_factor);
15628c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15638c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUT_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
15648c2ecf20Sopenharmony_ci			     p_cli->pf_total_lines);
15658c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15668c2ecf20Sopenharmony_ci			     PSWRQ2_REG_CDUT_VF_BLOCKS_RT_OFFSET,
15678c2ecf20Sopenharmony_ci			     p_cli->vf_total_lines);
15688c2ecf20Sopenharmony_ci	}
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TM];
15718c2ecf20Sopenharmony_ci	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
15728c2ecf20Sopenharmony_ci	if (p_cli->active) {
15738c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15748c2ecf20Sopenharmony_ci			     PSWRQ2_REG_TM_BLOCKS_FACTOR_RT_OFFSET, blk_factor);
15758c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15768c2ecf20Sopenharmony_ci			     PSWRQ2_REG_TM_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
15778c2ecf20Sopenharmony_ci			     p_cli->pf_total_lines);
15788c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
15798c2ecf20Sopenharmony_ci			     PSWRQ2_REG_TM_VF_BLOCKS_RT_OFFSET,
15808c2ecf20Sopenharmony_ci			     p_cli->vf_total_lines);
15818c2ecf20Sopenharmony_ci	}
15828c2ecf20Sopenharmony_ci}
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci/* ILT (PSWRQ2) PF */
15858c2ecf20Sopenharmony_cistatic void qed_ilt_init_pf(struct qed_hwfn *p_hwfn)
15868c2ecf20Sopenharmony_ci{
15878c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *clients;
15888c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr;
15898c2ecf20Sopenharmony_ci	struct phys_mem_desc *p_shdw;
15908c2ecf20Sopenharmony_ci	u32 line, rt_offst, i;
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	qed_ilt_bounds_init(p_hwfn);
15938c2ecf20Sopenharmony_ci	qed_ilt_vf_bounds_init(p_hwfn);
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	p_mngr = p_hwfn->p_cxt_mngr;
15968c2ecf20Sopenharmony_ci	p_shdw = p_mngr->ilt_shadow;
15978c2ecf20Sopenharmony_ci	clients = p_hwfn->p_cxt_mngr->clients;
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	for_each_ilt_valid_client(i, clients) {
16008c2ecf20Sopenharmony_ci		/** Client's 1st val and RT array are absolute, ILT shadows'
16018c2ecf20Sopenharmony_ci		 *  lines are relative.
16028c2ecf20Sopenharmony_ci		 */
16038c2ecf20Sopenharmony_ci		line = clients[i].first.val - p_mngr->pf_start_line;
16048c2ecf20Sopenharmony_ci		rt_offst = PSWRQ2_REG_ILT_MEMORY_RT_OFFSET +
16058c2ecf20Sopenharmony_ci			   clients[i].first.val * ILT_ENTRY_IN_REGS;
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci		for (; line <= clients[i].last.val - p_mngr->pf_start_line;
16088c2ecf20Sopenharmony_ci		     line++, rt_offst += ILT_ENTRY_IN_REGS) {
16098c2ecf20Sopenharmony_ci			u64 ilt_hw_entry = 0;
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci			/** p_virt could be NULL incase of dynamic
16128c2ecf20Sopenharmony_ci			 *  allocation
16138c2ecf20Sopenharmony_ci			 */
16148c2ecf20Sopenharmony_ci			if (p_shdw[line].virt_addr) {
16158c2ecf20Sopenharmony_ci				SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
16168c2ecf20Sopenharmony_ci				SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
16178c2ecf20Sopenharmony_ci					  (p_shdw[line].phys_addr >> 12));
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci				DP_VERBOSE(p_hwfn, QED_MSG_ILT,
16208c2ecf20Sopenharmony_ci					   "Setting RT[0x%08x] from ILT[0x%08x] [Client is %d] to Physical addr: 0x%llx\n",
16218c2ecf20Sopenharmony_ci					   rt_offst, line, i,
16228c2ecf20Sopenharmony_ci					   (u64)(p_shdw[line].phys_addr >> 12));
16238c2ecf20Sopenharmony_ci			}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci			STORE_RT_REG_AGG(p_hwfn, rt_offst, ilt_hw_entry);
16268c2ecf20Sopenharmony_ci		}
16278c2ecf20Sopenharmony_ci	}
16288c2ecf20Sopenharmony_ci}
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_ci/* SRC (Searcher) PF */
16318c2ecf20Sopenharmony_cistatic void qed_src_init_pf(struct qed_hwfn *p_hwfn)
16328c2ecf20Sopenharmony_ci{
16338c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
16348c2ecf20Sopenharmony_ci	u32 rounded_conn_num, conn_num, conn_max;
16358c2ecf20Sopenharmony_ci	struct qed_src_iids src_iids;
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	memset(&src_iids, 0, sizeof(src_iids));
16388c2ecf20Sopenharmony_ci	qed_cxt_src_iids(p_mngr, &src_iids);
16398c2ecf20Sopenharmony_ci	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
16408c2ecf20Sopenharmony_ci	if (!conn_num)
16418c2ecf20Sopenharmony_ci		return;
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	conn_max = max_t(u32, conn_num, SRC_MIN_NUM_ELEMS);
16448c2ecf20Sopenharmony_ci	rounded_conn_num = roundup_pow_of_two(conn_max);
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, SRC_REG_COUNTFREE_RT_OFFSET, conn_num);
16478c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, SRC_REG_NUMBER_HASH_BITS_RT_OFFSET,
16488c2ecf20Sopenharmony_ci		     ilog2(rounded_conn_num));
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci	STORE_RT_REG_AGG(p_hwfn, SRC_REG_FIRSTFREE_RT_OFFSET,
16518c2ecf20Sopenharmony_ci			 p_hwfn->p_cxt_mngr->src_t2.first_free);
16528c2ecf20Sopenharmony_ci	STORE_RT_REG_AGG(p_hwfn, SRC_REG_LASTFREE_RT_OFFSET,
16538c2ecf20Sopenharmony_ci			 p_hwfn->p_cxt_mngr->src_t2.last_free);
16548c2ecf20Sopenharmony_ci}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci/* Timers PF */
16578c2ecf20Sopenharmony_ci#define TM_CFG_NUM_IDS_SHIFT            0
16588c2ecf20Sopenharmony_ci#define TM_CFG_NUM_IDS_MASK             0xFFFFULL
16598c2ecf20Sopenharmony_ci#define TM_CFG_PRE_SCAN_OFFSET_SHIFT    16
16608c2ecf20Sopenharmony_ci#define TM_CFG_PRE_SCAN_OFFSET_MASK     0x1FFULL
16618c2ecf20Sopenharmony_ci#define TM_CFG_PARENT_PF_SHIFT          25
16628c2ecf20Sopenharmony_ci#define TM_CFG_PARENT_PF_MASK           0x7ULL
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ci#define TM_CFG_CID_PRE_SCAN_ROWS_SHIFT  30
16658c2ecf20Sopenharmony_ci#define TM_CFG_CID_PRE_SCAN_ROWS_MASK   0x1FFULL
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci#define TM_CFG_TID_OFFSET_SHIFT         30
16688c2ecf20Sopenharmony_ci#define TM_CFG_TID_OFFSET_MASK          0x7FFFFULL
16698c2ecf20Sopenharmony_ci#define TM_CFG_TID_PRE_SCAN_ROWS_SHIFT  49
16708c2ecf20Sopenharmony_ci#define TM_CFG_TID_PRE_SCAN_ROWS_MASK   0x1FFULL
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_cistatic void qed_tm_init_pf(struct qed_hwfn *p_hwfn)
16738c2ecf20Sopenharmony_ci{
16748c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
16758c2ecf20Sopenharmony_ci	u32 active_seg_mask = 0, tm_offset, rt_reg;
16768c2ecf20Sopenharmony_ci	struct qed_tm_iids tm_iids;
16778c2ecf20Sopenharmony_ci	u64 cfg_word;
16788c2ecf20Sopenharmony_ci	u8 i;
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	memset(&tm_iids, 0, sizeof(tm_iids));
16818c2ecf20Sopenharmony_ci	qed_cxt_tm_iids(p_hwfn, p_mngr, &tm_iids);
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	/* @@@TBD No pre-scan for now */
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	/* Note: We assume consecutive VFs for a PF */
16868c2ecf20Sopenharmony_ci	for (i = 0; i < p_mngr->vf_count; i++) {
16878c2ecf20Sopenharmony_ci		cfg_word = 0;
16888c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_cids);
16898c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
16908c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
16918c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);
16928c2ecf20Sopenharmony_ci		rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
16938c2ecf20Sopenharmony_ci		    (sizeof(cfg_word) / sizeof(u32)) *
16948c2ecf20Sopenharmony_ci		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
16958c2ecf20Sopenharmony_ci		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
16968c2ecf20Sopenharmony_ci	}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	cfg_word = 0;
16998c2ecf20Sopenharmony_ci	SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_cids);
17008c2ecf20Sopenharmony_ci	SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
17018c2ecf20Sopenharmony_ci	SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);	/* n/a for PF */
17028c2ecf20Sopenharmony_ci	SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);	/* scan all   */
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
17058c2ecf20Sopenharmony_ci	    (sizeof(cfg_word) / sizeof(u32)) *
17068c2ecf20Sopenharmony_ci	    (NUM_OF_VFS(p_hwfn->cdev) + p_hwfn->rel_pf_id);
17078c2ecf20Sopenharmony_ci	STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	/* enale scan */
17108c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_CONN_RT_OFFSET,
17118c2ecf20Sopenharmony_ci		     tm_iids.pf_cids ? 0x1 : 0x0);
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	/* @@@TBD how to enable the scan for the VFs */
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	tm_offset = tm_iids.per_vf_cids;
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_ci	/* Note: We assume consecutive VFs for a PF */
17188c2ecf20Sopenharmony_ci	for (i = 0; i < p_mngr->vf_count; i++) {
17198c2ecf20Sopenharmony_ci		cfg_word = 0;
17208c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_tids);
17218c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
17228c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
17238c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
17248c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
17278c2ecf20Sopenharmony_ci		    (sizeof(cfg_word) / sizeof(u32)) *
17288c2ecf20Sopenharmony_ci		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
17318c2ecf20Sopenharmony_ci	}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	tm_offset = tm_iids.pf_cids;
17348c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
17358c2ecf20Sopenharmony_ci		cfg_word = 0;
17368c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_tids[i]);
17378c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
17388c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);
17398c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
17408c2ecf20Sopenharmony_ci		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
17438c2ecf20Sopenharmony_ci		    (sizeof(cfg_word) / sizeof(u32)) *
17448c2ecf20Sopenharmony_ci		    (NUM_OF_VFS(p_hwfn->cdev) +
17458c2ecf20Sopenharmony_ci		     p_hwfn->rel_pf_id * NUM_TASK_PF_SEGMENTS + i);
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
17488c2ecf20Sopenharmony_ci		active_seg_mask |= (tm_iids.pf_tids[i] ? BIT(i) : 0);
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci		tm_offset += tm_iids.pf_tids[i];
17518c2ecf20Sopenharmony_ci	}
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	if (QED_IS_RDMA_PERSONALITY(p_hwfn))
17548c2ecf20Sopenharmony_ci		active_seg_mask = 0;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_TASK_RT_OFFSET, active_seg_mask);
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	/* @@@TBD how to enable the scan for the VFs */
17598c2ecf20Sopenharmony_ci}
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_cistatic void qed_prs_init_common(struct qed_hwfn *p_hwfn)
17628c2ecf20Sopenharmony_ci{
17638c2ecf20Sopenharmony_ci	if ((p_hwfn->hw_info.personality == QED_PCI_FCOE) &&
17648c2ecf20Sopenharmony_ci	    p_hwfn->pf_params.fcoe_pf_params.is_target)
17658c2ecf20Sopenharmony_ci		STORE_RT_REG(p_hwfn,
17668c2ecf20Sopenharmony_ci			     PRS_REG_SEARCH_RESP_INITIATOR_TYPE_RT_OFFSET, 0);
17678c2ecf20Sopenharmony_ci}
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_cistatic void qed_prs_init_pf(struct qed_hwfn *p_hwfn)
17708c2ecf20Sopenharmony_ci{
17718c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
17728c2ecf20Sopenharmony_ci	struct qed_conn_type_cfg *p_fcoe;
17738c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_tid;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	p_fcoe = &p_mngr->conn_cfg[PROTOCOLID_FCOE];
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	/* If FCoE is active set the MAX OX_ID (tid) in the Parser */
17788c2ecf20Sopenharmony_ci	if (!p_fcoe->cid_count)
17798c2ecf20Sopenharmony_ci		return;
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	p_tid = &p_fcoe->tid_seg[QED_CXT_FCOE_TID_SEG];
17828c2ecf20Sopenharmony_ci	if (p_hwfn->pf_params.fcoe_pf_params.is_target) {
17838c2ecf20Sopenharmony_ci		STORE_RT_REG_AGG(p_hwfn,
17848c2ecf20Sopenharmony_ci				 PRS_REG_TASK_ID_MAX_TARGET_PF_RT_OFFSET,
17858c2ecf20Sopenharmony_ci				 p_tid->count);
17868c2ecf20Sopenharmony_ci	} else {
17878c2ecf20Sopenharmony_ci		STORE_RT_REG_AGG(p_hwfn,
17888c2ecf20Sopenharmony_ci				 PRS_REG_TASK_ID_MAX_INITIATOR_PF_RT_OFFSET,
17898c2ecf20Sopenharmony_ci				 p_tid->count);
17908c2ecf20Sopenharmony_ci	}
17918c2ecf20Sopenharmony_ci}
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_civoid qed_cxt_hw_init_common(struct qed_hwfn *p_hwfn)
17948c2ecf20Sopenharmony_ci{
17958c2ecf20Sopenharmony_ci	qed_cdu_init_common(p_hwfn);
17968c2ecf20Sopenharmony_ci	qed_prs_init_common(p_hwfn);
17978c2ecf20Sopenharmony_ci}
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_civoid qed_cxt_hw_init_pf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
18008c2ecf20Sopenharmony_ci{
18018c2ecf20Sopenharmony_ci	qed_qm_init_pf(p_hwfn, p_ptt, true);
18028c2ecf20Sopenharmony_ci	qed_cm_init_pf(p_hwfn);
18038c2ecf20Sopenharmony_ci	qed_dq_init_pf(p_hwfn);
18048c2ecf20Sopenharmony_ci	qed_cdu_init_pf(p_hwfn);
18058c2ecf20Sopenharmony_ci	qed_ilt_init_pf(p_hwfn);
18068c2ecf20Sopenharmony_ci	qed_src_init_pf(p_hwfn);
18078c2ecf20Sopenharmony_ci	qed_tm_init_pf(p_hwfn);
18088c2ecf20Sopenharmony_ci	qed_prs_init_pf(p_hwfn);
18098c2ecf20Sopenharmony_ci}
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ciint _qed_cxt_acquire_cid(struct qed_hwfn *p_hwfn,
18128c2ecf20Sopenharmony_ci			 enum protocol_type type, u32 *p_cid, u8 vfid)
18138c2ecf20Sopenharmony_ci{
18148c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
18158c2ecf20Sopenharmony_ci	struct qed_cid_acquired_map *p_map;
18168c2ecf20Sopenharmony_ci	u32 rel_cid;
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci	if (type >= MAX_CONN_TYPES) {
18198c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "Invalid protocol type %d", type);
18208c2ecf20Sopenharmony_ci		return -EINVAL;
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci	if (vfid >= MAX_NUM_VFS && vfid != QED_CXT_PF_CID) {
18248c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "VF [%02x] is out of range\n", vfid);
18258c2ecf20Sopenharmony_ci		return -EINVAL;
18268c2ecf20Sopenharmony_ci	}
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci	/* Determine the right map to take this CID from */
18298c2ecf20Sopenharmony_ci	if (vfid == QED_CXT_PF_CID)
18308c2ecf20Sopenharmony_ci		p_map = &p_mngr->acquired[type];
18318c2ecf20Sopenharmony_ci	else
18328c2ecf20Sopenharmony_ci		p_map = &p_mngr->acquired_vf[type][vfid];
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	if (!p_map->cid_map) {
18358c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "Invalid protocol type %d", type);
18368c2ecf20Sopenharmony_ci		return -EINVAL;
18378c2ecf20Sopenharmony_ci	}
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_ci	rel_cid = find_first_zero_bit(p_map->cid_map, p_map->max_count);
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_ci	if (rel_cid >= p_map->max_count) {
18428c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "no CID available for protocol %d\n", type);
18438c2ecf20Sopenharmony_ci		return -EINVAL;
18448c2ecf20Sopenharmony_ci	}
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci	__set_bit(rel_cid, p_map->cid_map);
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	*p_cid = rel_cid + p_map->start_cid;
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_CXT,
18518c2ecf20Sopenharmony_ci		   "Acquired cid 0x%08x [rel. %08x] vfid %02x type %d\n",
18528c2ecf20Sopenharmony_ci		   *p_cid, rel_cid, vfid, type);
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci	return 0;
18558c2ecf20Sopenharmony_ci}
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ciint qed_cxt_acquire_cid(struct qed_hwfn *p_hwfn,
18588c2ecf20Sopenharmony_ci			enum protocol_type type, u32 *p_cid)
18598c2ecf20Sopenharmony_ci{
18608c2ecf20Sopenharmony_ci	return _qed_cxt_acquire_cid(p_hwfn, type, p_cid, QED_CXT_PF_CID);
18618c2ecf20Sopenharmony_ci}
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_cistatic bool qed_cxt_test_cid_acquired(struct qed_hwfn *p_hwfn,
18648c2ecf20Sopenharmony_ci				      u32 cid,
18658c2ecf20Sopenharmony_ci				      u8 vfid,
18668c2ecf20Sopenharmony_ci				      enum protocol_type *p_type,
18678c2ecf20Sopenharmony_ci				      struct qed_cid_acquired_map **pp_map)
18688c2ecf20Sopenharmony_ci{
18698c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
18708c2ecf20Sopenharmony_ci	u32 rel_cid;
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci	/* Iterate over protocols and find matching cid range */
18738c2ecf20Sopenharmony_ci	for (*p_type = 0; *p_type < MAX_CONN_TYPES; (*p_type)++) {
18748c2ecf20Sopenharmony_ci		if (vfid == QED_CXT_PF_CID)
18758c2ecf20Sopenharmony_ci			*pp_map = &p_mngr->acquired[*p_type];
18768c2ecf20Sopenharmony_ci		else
18778c2ecf20Sopenharmony_ci			*pp_map = &p_mngr->acquired_vf[*p_type][vfid];
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci		if (!((*pp_map)->cid_map))
18808c2ecf20Sopenharmony_ci			continue;
18818c2ecf20Sopenharmony_ci		if (cid >= (*pp_map)->start_cid &&
18828c2ecf20Sopenharmony_ci		    cid < (*pp_map)->start_cid + (*pp_map)->max_count)
18838c2ecf20Sopenharmony_ci			break;
18848c2ecf20Sopenharmony_ci	}
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	if (*p_type == MAX_CONN_TYPES) {
18878c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "Invalid CID %d vfid %02x", cid, vfid);
18888c2ecf20Sopenharmony_ci		goto fail;
18898c2ecf20Sopenharmony_ci	}
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	rel_cid = cid - (*pp_map)->start_cid;
18928c2ecf20Sopenharmony_ci	if (!test_bit(rel_cid, (*pp_map)->cid_map)) {
18938c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "CID %d [vifd %02x] not acquired",
18948c2ecf20Sopenharmony_ci			  cid, vfid);
18958c2ecf20Sopenharmony_ci		goto fail;
18968c2ecf20Sopenharmony_ci	}
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	return true;
18998c2ecf20Sopenharmony_cifail:
19008c2ecf20Sopenharmony_ci	*p_type = MAX_CONN_TYPES;
19018c2ecf20Sopenharmony_ci	*pp_map = NULL;
19028c2ecf20Sopenharmony_ci	return false;
19038c2ecf20Sopenharmony_ci}
19048c2ecf20Sopenharmony_ci
19058c2ecf20Sopenharmony_civoid _qed_cxt_release_cid(struct qed_hwfn *p_hwfn, u32 cid, u8 vfid)
19068c2ecf20Sopenharmony_ci{
19078c2ecf20Sopenharmony_ci	struct qed_cid_acquired_map *p_map = NULL;
19088c2ecf20Sopenharmony_ci	enum protocol_type type;
19098c2ecf20Sopenharmony_ci	bool b_acquired;
19108c2ecf20Sopenharmony_ci	u32 rel_cid;
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_ci	if (vfid != QED_CXT_PF_CID && vfid > MAX_NUM_VFS) {
19138c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn,
19148c2ecf20Sopenharmony_ci			  "Trying to return incorrect CID belonging to VF %02x\n",
19158c2ecf20Sopenharmony_ci			  vfid);
19168c2ecf20Sopenharmony_ci		return;
19178c2ecf20Sopenharmony_ci	}
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	/* Test acquired and find matching per-protocol map */
19208c2ecf20Sopenharmony_ci	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, cid, vfid,
19218c2ecf20Sopenharmony_ci					       &type, &p_map);
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci	if (!b_acquired)
19248c2ecf20Sopenharmony_ci		return;
19258c2ecf20Sopenharmony_ci
19268c2ecf20Sopenharmony_ci	rel_cid = cid - p_map->start_cid;
19278c2ecf20Sopenharmony_ci	clear_bit(rel_cid, p_map->cid_map);
19288c2ecf20Sopenharmony_ci
19298c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, QED_MSG_CXT,
19308c2ecf20Sopenharmony_ci		   "Released CID 0x%08x [rel. %08x] vfid %02x type %d\n",
19318c2ecf20Sopenharmony_ci		   cid, rel_cid, vfid, type);
19328c2ecf20Sopenharmony_ci}
19338c2ecf20Sopenharmony_ci
19348c2ecf20Sopenharmony_civoid qed_cxt_release_cid(struct qed_hwfn *p_hwfn, u32 cid)
19358c2ecf20Sopenharmony_ci{
19368c2ecf20Sopenharmony_ci	_qed_cxt_release_cid(p_hwfn, cid, QED_CXT_PF_CID);
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_ciint qed_cxt_get_cid_info(struct qed_hwfn *p_hwfn, struct qed_cxt_info *p_info)
19408c2ecf20Sopenharmony_ci{
19418c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
19428c2ecf20Sopenharmony_ci	struct qed_cid_acquired_map *p_map = NULL;
19438c2ecf20Sopenharmony_ci	u32 conn_cxt_size, hw_p_size, cxts_per_p, line;
19448c2ecf20Sopenharmony_ci	enum protocol_type type;
19458c2ecf20Sopenharmony_ci	bool b_acquired;
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	/* Test acquired and find matching per-protocol map */
19488c2ecf20Sopenharmony_ci	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, p_info->iid,
19498c2ecf20Sopenharmony_ci					       QED_CXT_PF_CID, &type, &p_map);
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ci	if (!b_acquired)
19528c2ecf20Sopenharmony_ci		return -EINVAL;
19538c2ecf20Sopenharmony_ci
19548c2ecf20Sopenharmony_ci	/* set the protocl type */
19558c2ecf20Sopenharmony_ci	p_info->type = type;
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	/* compute context virtual pointer */
19588c2ecf20Sopenharmony_ci	hw_p_size = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci	conn_cxt_size = CONN_CXT_SIZE(p_hwfn);
19618c2ecf20Sopenharmony_ci	cxts_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / conn_cxt_size;
19628c2ecf20Sopenharmony_ci	line = p_info->iid / cxts_per_p;
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci	/* Make sure context is allocated (dynamic allocation) */
19658c2ecf20Sopenharmony_ci	if (!p_mngr->ilt_shadow[line].virt_addr)
19668c2ecf20Sopenharmony_ci		return -EINVAL;
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci	p_info->p_cxt = p_mngr->ilt_shadow[line].virt_addr +
19698c2ecf20Sopenharmony_ci			p_info->iid % cxts_per_p * conn_cxt_size;
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	DP_VERBOSE(p_hwfn, (QED_MSG_ILT | QED_MSG_CXT),
19728c2ecf20Sopenharmony_ci		   "Accessing ILT shadow[%d]: CXT pointer is at %p (for iid %d)\n",
19738c2ecf20Sopenharmony_ci		   p_info->iid / cxts_per_p, p_info->p_cxt, p_info->iid);
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_ci	return 0;
19768c2ecf20Sopenharmony_ci}
19778c2ecf20Sopenharmony_ci
19788c2ecf20Sopenharmony_cistatic void qed_rdma_set_pf_params(struct qed_hwfn *p_hwfn,
19798c2ecf20Sopenharmony_ci				   struct qed_rdma_pf_params *p_params,
19808c2ecf20Sopenharmony_ci				   u32 num_tasks)
19818c2ecf20Sopenharmony_ci{
19828c2ecf20Sopenharmony_ci	u32 num_cons, num_qps;
19838c2ecf20Sopenharmony_ci	enum protocol_type proto;
19848c2ecf20Sopenharmony_ci
19858c2ecf20Sopenharmony_ci	if (p_hwfn->mcp_info->func_info.protocol == QED_PCI_ETH_RDMA) {
19868c2ecf20Sopenharmony_ci		DP_VERBOSE(p_hwfn, QED_MSG_SP,
19878c2ecf20Sopenharmony_ci			   "Current day drivers don't support RoCE & iWARP simultaneously on the same PF. Default to RoCE-only\n");
19888c2ecf20Sopenharmony_ci		p_hwfn->hw_info.personality = QED_PCI_ETH_ROCE;
19898c2ecf20Sopenharmony_ci	}
19908c2ecf20Sopenharmony_ci
19918c2ecf20Sopenharmony_ci	switch (p_hwfn->hw_info.personality) {
19928c2ecf20Sopenharmony_ci	case QED_PCI_ETH_IWARP:
19938c2ecf20Sopenharmony_ci		/* Each QP requires one connection */
19948c2ecf20Sopenharmony_ci		num_cons = min_t(u32, IWARP_MAX_QPS, p_params->num_qps);
19958c2ecf20Sopenharmony_ci		proto = PROTOCOLID_IWARP;
19968c2ecf20Sopenharmony_ci		break;
19978c2ecf20Sopenharmony_ci	case QED_PCI_ETH_ROCE:
19988c2ecf20Sopenharmony_ci		num_qps = min_t(u32, ROCE_MAX_QPS, p_params->num_qps);
19998c2ecf20Sopenharmony_ci		num_cons = num_qps * 2;	/* each QP requires two connections */
20008c2ecf20Sopenharmony_ci		proto = PROTOCOLID_ROCE;
20018c2ecf20Sopenharmony_ci		break;
20028c2ecf20Sopenharmony_ci	default:
20038c2ecf20Sopenharmony_ci		return;
20048c2ecf20Sopenharmony_ci	}
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ci	if (num_cons && num_tasks) {
20078c2ecf20Sopenharmony_ci		u32 num_srqs, num_xrc_srqs;
20088c2ecf20Sopenharmony_ci
20098c2ecf20Sopenharmony_ci		qed_cxt_set_proto_cid_count(p_hwfn, proto, num_cons, 0);
20108c2ecf20Sopenharmony_ci
20118c2ecf20Sopenharmony_ci		/* Deliberatly passing ROCE for tasks id. This is because
20128c2ecf20Sopenharmony_ci		 * iWARP / RoCE share the task id.
20138c2ecf20Sopenharmony_ci		 */
20148c2ecf20Sopenharmony_ci		qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_ROCE,
20158c2ecf20Sopenharmony_ci					    QED_CXT_ROCE_TID_SEG, 1,
20168c2ecf20Sopenharmony_ci					    num_tasks, false);
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_ci		num_srqs = min_t(u32, QED_RDMA_MAX_SRQS, p_params->num_srqs);
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ci		/* XRC SRQs populate a single ILT page */
20218c2ecf20Sopenharmony_ci		num_xrc_srqs = qed_cxt_xrc_srqs_per_page(p_hwfn);
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci		qed_cxt_set_srq_count(p_hwfn, num_srqs, num_xrc_srqs);
20248c2ecf20Sopenharmony_ci	} else {
20258c2ecf20Sopenharmony_ci		DP_INFO(p_hwfn->cdev,
20268c2ecf20Sopenharmony_ci			"RDMA personality used without setting params!\n");
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci}
20298c2ecf20Sopenharmony_ci
20308c2ecf20Sopenharmony_ciint qed_cxt_set_pf_params(struct qed_hwfn *p_hwfn, u32 rdma_tasks)
20318c2ecf20Sopenharmony_ci{
20328c2ecf20Sopenharmony_ci	/* Set the number of required CORE connections */
20338c2ecf20Sopenharmony_ci	u32 core_cids = 1; /* SPQ */
20348c2ecf20Sopenharmony_ci
20358c2ecf20Sopenharmony_ci	if (p_hwfn->using_ll2)
20368c2ecf20Sopenharmony_ci		core_cids += 4;
20378c2ecf20Sopenharmony_ci	qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_CORE, core_cids, 0);
20388c2ecf20Sopenharmony_ci
20398c2ecf20Sopenharmony_ci	switch (p_hwfn->hw_info.personality) {
20408c2ecf20Sopenharmony_ci	case QED_PCI_ETH_RDMA:
20418c2ecf20Sopenharmony_ci	case QED_PCI_ETH_IWARP:
20428c2ecf20Sopenharmony_ci	case QED_PCI_ETH_ROCE:
20438c2ecf20Sopenharmony_ci	{
20448c2ecf20Sopenharmony_ci			qed_rdma_set_pf_params(p_hwfn,
20458c2ecf20Sopenharmony_ci					       &p_hwfn->
20468c2ecf20Sopenharmony_ci					       pf_params.rdma_pf_params,
20478c2ecf20Sopenharmony_ci					       rdma_tasks);
20488c2ecf20Sopenharmony_ci		/* no need for break since RoCE coexist with Ethernet */
20498c2ecf20Sopenharmony_ci	}
20508c2ecf20Sopenharmony_ci		fallthrough;
20518c2ecf20Sopenharmony_ci	case QED_PCI_ETH:
20528c2ecf20Sopenharmony_ci	{
20538c2ecf20Sopenharmony_ci		struct qed_eth_pf_params *p_params =
20548c2ecf20Sopenharmony_ci		    &p_hwfn->pf_params.eth_pf_params;
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci		if (!p_params->num_vf_cons)
20578c2ecf20Sopenharmony_ci			p_params->num_vf_cons =
20588c2ecf20Sopenharmony_ci			    ETH_PF_PARAMS_VF_CONS_DEFAULT;
20598c2ecf20Sopenharmony_ci		qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
20608c2ecf20Sopenharmony_ci					    p_params->num_cons,
20618c2ecf20Sopenharmony_ci					    p_params->num_vf_cons);
20628c2ecf20Sopenharmony_ci		p_hwfn->p_cxt_mngr->arfs_count = p_params->num_arfs_filters;
20638c2ecf20Sopenharmony_ci		break;
20648c2ecf20Sopenharmony_ci	}
20658c2ecf20Sopenharmony_ci	case QED_PCI_FCOE:
20668c2ecf20Sopenharmony_ci	{
20678c2ecf20Sopenharmony_ci		struct qed_fcoe_pf_params *p_params;
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ci		p_params = &p_hwfn->pf_params.fcoe_pf_params;
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci		if (p_params->num_cons && p_params->num_tasks) {
20728c2ecf20Sopenharmony_ci			qed_cxt_set_proto_cid_count(p_hwfn,
20738c2ecf20Sopenharmony_ci						    PROTOCOLID_FCOE,
20748c2ecf20Sopenharmony_ci						    p_params->num_cons,
20758c2ecf20Sopenharmony_ci						    0);
20768c2ecf20Sopenharmony_ci
20778c2ecf20Sopenharmony_ci			qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_FCOE,
20788c2ecf20Sopenharmony_ci						    QED_CXT_FCOE_TID_SEG, 0,
20798c2ecf20Sopenharmony_ci						    p_params->num_tasks, true);
20808c2ecf20Sopenharmony_ci		} else {
20818c2ecf20Sopenharmony_ci			DP_INFO(p_hwfn->cdev,
20828c2ecf20Sopenharmony_ci				"Fcoe personality used without setting params!\n");
20838c2ecf20Sopenharmony_ci		}
20848c2ecf20Sopenharmony_ci		break;
20858c2ecf20Sopenharmony_ci	}
20868c2ecf20Sopenharmony_ci	case QED_PCI_ISCSI:
20878c2ecf20Sopenharmony_ci	{
20888c2ecf20Sopenharmony_ci		struct qed_iscsi_pf_params *p_params;
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci		p_params = &p_hwfn->pf_params.iscsi_pf_params;
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci		if (p_params->num_cons && p_params->num_tasks) {
20938c2ecf20Sopenharmony_ci			qed_cxt_set_proto_cid_count(p_hwfn,
20948c2ecf20Sopenharmony_ci						    PROTOCOLID_ISCSI,
20958c2ecf20Sopenharmony_ci						    p_params->num_cons,
20968c2ecf20Sopenharmony_ci						    0);
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci			qed_cxt_set_proto_tid_count(p_hwfn,
20998c2ecf20Sopenharmony_ci						    PROTOCOLID_ISCSI,
21008c2ecf20Sopenharmony_ci						    QED_CXT_ISCSI_TID_SEG,
21018c2ecf20Sopenharmony_ci						    0,
21028c2ecf20Sopenharmony_ci						    p_params->num_tasks,
21038c2ecf20Sopenharmony_ci						    true);
21048c2ecf20Sopenharmony_ci		} else {
21058c2ecf20Sopenharmony_ci			DP_INFO(p_hwfn->cdev,
21068c2ecf20Sopenharmony_ci				"Iscsi personality used without setting params!\n");
21078c2ecf20Sopenharmony_ci		}
21088c2ecf20Sopenharmony_ci		break;
21098c2ecf20Sopenharmony_ci	}
21108c2ecf20Sopenharmony_ci	default:
21118c2ecf20Sopenharmony_ci		return -EINVAL;
21128c2ecf20Sopenharmony_ci	}
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	return 0;
21158c2ecf20Sopenharmony_ci}
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ciint qed_cxt_get_tid_mem_info(struct qed_hwfn *p_hwfn,
21188c2ecf20Sopenharmony_ci			     struct qed_tid_mem *p_info)
21198c2ecf20Sopenharmony_ci{
21208c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
21218c2ecf20Sopenharmony_ci	u32 proto, seg, total_lines, i, shadow_line;
21228c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
21238c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_fl_seg;
21248c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg_info;
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci	/* Verify the personality */
21278c2ecf20Sopenharmony_ci	switch (p_hwfn->hw_info.personality) {
21288c2ecf20Sopenharmony_ci	case QED_PCI_FCOE:
21298c2ecf20Sopenharmony_ci		proto = PROTOCOLID_FCOE;
21308c2ecf20Sopenharmony_ci		seg = QED_CXT_FCOE_TID_SEG;
21318c2ecf20Sopenharmony_ci		break;
21328c2ecf20Sopenharmony_ci	case QED_PCI_ISCSI:
21338c2ecf20Sopenharmony_ci		proto = PROTOCOLID_ISCSI;
21348c2ecf20Sopenharmony_ci		seg = QED_CXT_ISCSI_TID_SEG;
21358c2ecf20Sopenharmony_ci		break;
21368c2ecf20Sopenharmony_ci	default:
21378c2ecf20Sopenharmony_ci		return -EINVAL;
21388c2ecf20Sopenharmony_ci	}
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
21418c2ecf20Sopenharmony_ci	if (!p_cli->active)
21428c2ecf20Sopenharmony_ci		return -EINVAL;
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
21458c2ecf20Sopenharmony_ci	if (!p_seg_info->has_fl_mem)
21468c2ecf20Sopenharmony_ci		return -EINVAL;
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_ci	p_fl_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
21498c2ecf20Sopenharmony_ci	total_lines = DIV_ROUND_UP(p_fl_seg->total_size,
21508c2ecf20Sopenharmony_ci				   p_fl_seg->real_size_in_page);
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	for (i = 0; i < total_lines; i++) {
21538c2ecf20Sopenharmony_ci		shadow_line = i + p_fl_seg->start_line -
21548c2ecf20Sopenharmony_ci		    p_hwfn->p_cxt_mngr->pf_start_line;
21558c2ecf20Sopenharmony_ci		p_info->blocks[i] = p_mngr->ilt_shadow[shadow_line].virt_addr;
21568c2ecf20Sopenharmony_ci	}
21578c2ecf20Sopenharmony_ci	p_info->waste = ILT_PAGE_IN_BYTES(p_cli->p_size.val) -
21588c2ecf20Sopenharmony_ci	    p_fl_seg->real_size_in_page;
21598c2ecf20Sopenharmony_ci	p_info->tid_size = p_mngr->task_type_size[p_seg_info->type];
21608c2ecf20Sopenharmony_ci	p_info->num_tids_per_block = p_fl_seg->real_size_in_page /
21618c2ecf20Sopenharmony_ci	    p_info->tid_size;
21628c2ecf20Sopenharmony_ci
21638c2ecf20Sopenharmony_ci	return 0;
21648c2ecf20Sopenharmony_ci}
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci/* This function is very RoCE oriented, if another protocol in the future
21678c2ecf20Sopenharmony_ci * will want this feature we'll need to modify the function to be more generic
21688c2ecf20Sopenharmony_ci */
21698c2ecf20Sopenharmony_ciint
21708c2ecf20Sopenharmony_ciqed_cxt_dynamic_ilt_alloc(struct qed_hwfn *p_hwfn,
21718c2ecf20Sopenharmony_ci			  enum qed_cxt_elem_type elem_type, u32 iid)
21728c2ecf20Sopenharmony_ci{
21738c2ecf20Sopenharmony_ci	u32 reg_offset, shadow_line, elem_size, hw_p_size, elems_per_p, line;
21748c2ecf20Sopenharmony_ci	struct tdif_task_context *tdif_context;
21758c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
21768c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
21778c2ecf20Sopenharmony_ci	struct qed_ptt *p_ptt;
21788c2ecf20Sopenharmony_ci	dma_addr_t p_phys;
21798c2ecf20Sopenharmony_ci	u64 ilt_hw_entry;
21808c2ecf20Sopenharmony_ci	void *p_virt;
21818c2ecf20Sopenharmony_ci	u32 flags1;
21828c2ecf20Sopenharmony_ci	int rc = 0;
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	switch (elem_type) {
21858c2ecf20Sopenharmony_ci	case QED_ELEM_CXT:
21868c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
21878c2ecf20Sopenharmony_ci		elem_size = CONN_CXT_SIZE(p_hwfn);
21888c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUC_BLK];
21898c2ecf20Sopenharmony_ci		break;
21908c2ecf20Sopenharmony_ci	case QED_ELEM_SRQ:
21918c2ecf20Sopenharmony_ci		/* The first ILT page is not used for regular SRQs. Skip it. */
21928c2ecf20Sopenharmony_ci		iid += p_hwfn->p_cxt_mngr->xrc_srq_count;
21938c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
21948c2ecf20Sopenharmony_ci		elem_size = SRQ_CXT_SIZE;
21958c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[SRQ_BLK];
21968c2ecf20Sopenharmony_ci		break;
21978c2ecf20Sopenharmony_ci	case QED_ELEM_XRC_SRQ:
21988c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
21998c2ecf20Sopenharmony_ci		elem_size = XRC_SRQ_CXT_SIZE;
22008c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[SRQ_BLK];
22018c2ecf20Sopenharmony_ci		break;
22028c2ecf20Sopenharmony_ci	case QED_ELEM_TASK:
22038c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
22048c2ecf20Sopenharmony_ci		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
22058c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
22068c2ecf20Sopenharmony_ci		break;
22078c2ecf20Sopenharmony_ci	default:
22088c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
22098c2ecf20Sopenharmony_ci		return -EINVAL;
22108c2ecf20Sopenharmony_ci	}
22118c2ecf20Sopenharmony_ci
22128c2ecf20Sopenharmony_ci	/* Calculate line in ilt */
22138c2ecf20Sopenharmony_ci	hw_p_size = p_cli->p_size.val;
22148c2ecf20Sopenharmony_ci	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
22158c2ecf20Sopenharmony_ci	line = p_blk->start_line + (iid / elems_per_p);
22168c2ecf20Sopenharmony_ci	shadow_line = line - p_hwfn->p_cxt_mngr->pf_start_line;
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci	/* If line is already allocated, do nothing, otherwise allocate it and
22198c2ecf20Sopenharmony_ci	 * write it to the PSWRQ2 registers.
22208c2ecf20Sopenharmony_ci	 * This section can be run in parallel from different contexts and thus
22218c2ecf20Sopenharmony_ci	 * a mutex protection is needed.
22228c2ecf20Sopenharmony_ci	 */
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	mutex_lock(&p_hwfn->p_cxt_mngr->mutex);
22258c2ecf20Sopenharmony_ci
22268c2ecf20Sopenharmony_ci	if (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].virt_addr)
22278c2ecf20Sopenharmony_ci		goto out0;
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	p_ptt = qed_ptt_acquire(p_hwfn);
22308c2ecf20Sopenharmony_ci	if (!p_ptt) {
22318c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn,
22328c2ecf20Sopenharmony_ci			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
22338c2ecf20Sopenharmony_ci		rc = -EBUSY;
22348c2ecf20Sopenharmony_ci		goto out0;
22358c2ecf20Sopenharmony_ci	}
22368c2ecf20Sopenharmony_ci
22378c2ecf20Sopenharmony_ci	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
22388c2ecf20Sopenharmony_ci				    p_blk->real_size_in_page, &p_phys,
22398c2ecf20Sopenharmony_ci				    GFP_KERNEL);
22408c2ecf20Sopenharmony_ci	if (!p_virt) {
22418c2ecf20Sopenharmony_ci		rc = -ENOMEM;
22428c2ecf20Sopenharmony_ci		goto out1;
22438c2ecf20Sopenharmony_ci	}
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ci	/* configuration of refTagMask to 0xF is required for RoCE DIF MR only,
22468c2ecf20Sopenharmony_ci	 * to compensate for a HW bug, but it is configured even if DIF is not
22478c2ecf20Sopenharmony_ci	 * enabled. This is harmless and allows us to avoid a dedicated API. We
22488c2ecf20Sopenharmony_ci	 * configure the field for all of the contexts on the newly allocated
22498c2ecf20Sopenharmony_ci	 * page.
22508c2ecf20Sopenharmony_ci	 */
22518c2ecf20Sopenharmony_ci	if (elem_type == QED_ELEM_TASK) {
22528c2ecf20Sopenharmony_ci		u32 elem_i;
22538c2ecf20Sopenharmony_ci		u8 *elem_start = (u8 *)p_virt;
22548c2ecf20Sopenharmony_ci		union type1_task_context *elem;
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ci		for (elem_i = 0; elem_i < elems_per_p; elem_i++) {
22578c2ecf20Sopenharmony_ci			elem = (union type1_task_context *)elem_start;
22588c2ecf20Sopenharmony_ci			tdif_context = &elem->roce_ctx.tdif_context;
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci			flags1 = le32_to_cpu(tdif_context->flags1);
22618c2ecf20Sopenharmony_ci			SET_FIELD(flags1, TDIF_TASK_CONTEXT_REF_TAG_MASK, 0xf);
22628c2ecf20Sopenharmony_ci			tdif_context->flags1 = cpu_to_le32(flags1);
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_ci			elem_start += TYPE1_TASK_CXT_SIZE(p_hwfn);
22658c2ecf20Sopenharmony_ci		}
22668c2ecf20Sopenharmony_ci	}
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].virt_addr = p_virt;
22698c2ecf20Sopenharmony_ci	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].phys_addr = p_phys;
22708c2ecf20Sopenharmony_ci	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].size =
22718c2ecf20Sopenharmony_ci	    p_blk->real_size_in_page;
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ci	/* compute absolute offset */
22748c2ecf20Sopenharmony_ci	reg_offset = PSWRQ2_REG_ILT_MEMORY +
22758c2ecf20Sopenharmony_ci	    (line * ILT_REG_SIZE_IN_BYTES * ILT_ENTRY_IN_REGS);
22768c2ecf20Sopenharmony_ci
22778c2ecf20Sopenharmony_ci	ilt_hw_entry = 0;
22788c2ecf20Sopenharmony_ci	SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
22798c2ecf20Sopenharmony_ci	SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
22808c2ecf20Sopenharmony_ci		  (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].phys_addr
22818c2ecf20Sopenharmony_ci		   >> 12));
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a wide-bus */
22848c2ecf20Sopenharmony_ci	qed_dmae_host2grc(p_hwfn, p_ptt, (u64) (uintptr_t)&ilt_hw_entry,
22858c2ecf20Sopenharmony_ci			  reg_offset, sizeof(ilt_hw_entry) / sizeof(u32),
22868c2ecf20Sopenharmony_ci			  NULL);
22878c2ecf20Sopenharmony_ci
22888c2ecf20Sopenharmony_ci	if (elem_type == QED_ELEM_CXT) {
22898c2ecf20Sopenharmony_ci		u32 last_cid_allocated = (1 + (iid / elems_per_p)) *
22908c2ecf20Sopenharmony_ci		    elems_per_p;
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_ci		/* Update the relevant register in the parser */
22938c2ecf20Sopenharmony_ci		qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF,
22948c2ecf20Sopenharmony_ci		       last_cid_allocated - 1);
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci		if (!p_hwfn->b_rdma_enabled_in_prs) {
22978c2ecf20Sopenharmony_ci			/* Enable RDMA search */
22988c2ecf20Sopenharmony_ci			qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
22998c2ecf20Sopenharmony_ci			p_hwfn->b_rdma_enabled_in_prs = true;
23008c2ecf20Sopenharmony_ci		}
23018c2ecf20Sopenharmony_ci	}
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ciout1:
23048c2ecf20Sopenharmony_ci	qed_ptt_release(p_hwfn, p_ptt);
23058c2ecf20Sopenharmony_ciout0:
23068c2ecf20Sopenharmony_ci	mutex_unlock(&p_hwfn->p_cxt_mngr->mutex);
23078c2ecf20Sopenharmony_ci
23088c2ecf20Sopenharmony_ci	return rc;
23098c2ecf20Sopenharmony_ci}
23108c2ecf20Sopenharmony_ci
23118c2ecf20Sopenharmony_ci/* This function is very RoCE oriented, if another protocol in the future
23128c2ecf20Sopenharmony_ci * will want this feature we'll need to modify the function to be more generic
23138c2ecf20Sopenharmony_ci */
23148c2ecf20Sopenharmony_cistatic int
23158c2ecf20Sopenharmony_ciqed_cxt_free_ilt_range(struct qed_hwfn *p_hwfn,
23168c2ecf20Sopenharmony_ci		       enum qed_cxt_elem_type elem_type,
23178c2ecf20Sopenharmony_ci		       u32 start_iid, u32 count)
23188c2ecf20Sopenharmony_ci{
23198c2ecf20Sopenharmony_ci	u32 start_line, end_line, shadow_start_line, shadow_end_line;
23208c2ecf20Sopenharmony_ci	u32 reg_offset, elem_size, hw_p_size, elems_per_p;
23218c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
23228c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
23238c2ecf20Sopenharmony_ci	u32 end_iid = start_iid + count;
23248c2ecf20Sopenharmony_ci	struct qed_ptt *p_ptt;
23258c2ecf20Sopenharmony_ci	u64 ilt_hw_entry = 0;
23268c2ecf20Sopenharmony_ci	u32 i;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci	switch (elem_type) {
23298c2ecf20Sopenharmony_ci	case QED_ELEM_CXT:
23308c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
23318c2ecf20Sopenharmony_ci		elem_size = CONN_CXT_SIZE(p_hwfn);
23328c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUC_BLK];
23338c2ecf20Sopenharmony_ci		break;
23348c2ecf20Sopenharmony_ci	case QED_ELEM_SRQ:
23358c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
23368c2ecf20Sopenharmony_ci		elem_size = SRQ_CXT_SIZE;
23378c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[SRQ_BLK];
23388c2ecf20Sopenharmony_ci		break;
23398c2ecf20Sopenharmony_ci	case QED_ELEM_XRC_SRQ:
23408c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
23418c2ecf20Sopenharmony_ci		elem_size = XRC_SRQ_CXT_SIZE;
23428c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[SRQ_BLK];
23438c2ecf20Sopenharmony_ci		break;
23448c2ecf20Sopenharmony_ci	case QED_ELEM_TASK:
23458c2ecf20Sopenharmony_ci		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
23468c2ecf20Sopenharmony_ci		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
23478c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
23488c2ecf20Sopenharmony_ci		break;
23498c2ecf20Sopenharmony_ci	default:
23508c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
23518c2ecf20Sopenharmony_ci		return -EINVAL;
23528c2ecf20Sopenharmony_ci	}
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_ci	/* Calculate line in ilt */
23558c2ecf20Sopenharmony_ci	hw_p_size = p_cli->p_size.val;
23568c2ecf20Sopenharmony_ci	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
23578c2ecf20Sopenharmony_ci	start_line = p_blk->start_line + (start_iid / elems_per_p);
23588c2ecf20Sopenharmony_ci	end_line = p_blk->start_line + (end_iid / elems_per_p);
23598c2ecf20Sopenharmony_ci	if (((end_iid + 1) / elems_per_p) != (end_iid / elems_per_p))
23608c2ecf20Sopenharmony_ci		end_line--;
23618c2ecf20Sopenharmony_ci
23628c2ecf20Sopenharmony_ci	shadow_start_line = start_line - p_hwfn->p_cxt_mngr->pf_start_line;
23638c2ecf20Sopenharmony_ci	shadow_end_line = end_line - p_hwfn->p_cxt_mngr->pf_start_line;
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci	p_ptt = qed_ptt_acquire(p_hwfn);
23668c2ecf20Sopenharmony_ci	if (!p_ptt) {
23678c2ecf20Sopenharmony_ci		DP_NOTICE(p_hwfn,
23688c2ecf20Sopenharmony_ci			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
23698c2ecf20Sopenharmony_ci		return -EBUSY;
23708c2ecf20Sopenharmony_ci	}
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_ci	for (i = shadow_start_line; i < shadow_end_line; i++) {
23738c2ecf20Sopenharmony_ci		if (!p_hwfn->p_cxt_mngr->ilt_shadow[i].virt_addr)
23748c2ecf20Sopenharmony_ci			continue;
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
23778c2ecf20Sopenharmony_ci				  p_hwfn->p_cxt_mngr->ilt_shadow[i].size,
23788c2ecf20Sopenharmony_ci				  p_hwfn->p_cxt_mngr->ilt_shadow[i].virt_addr,
23798c2ecf20Sopenharmony_ci				  p_hwfn->p_cxt_mngr->ilt_shadow[i].phys_addr);
23808c2ecf20Sopenharmony_ci
23818c2ecf20Sopenharmony_ci		p_hwfn->p_cxt_mngr->ilt_shadow[i].virt_addr = NULL;
23828c2ecf20Sopenharmony_ci		p_hwfn->p_cxt_mngr->ilt_shadow[i].phys_addr = 0;
23838c2ecf20Sopenharmony_ci		p_hwfn->p_cxt_mngr->ilt_shadow[i].size = 0;
23848c2ecf20Sopenharmony_ci
23858c2ecf20Sopenharmony_ci		/* compute absolute offset */
23868c2ecf20Sopenharmony_ci		reg_offset = PSWRQ2_REG_ILT_MEMORY +
23878c2ecf20Sopenharmony_ci		    ((start_line++) * ILT_REG_SIZE_IN_BYTES *
23888c2ecf20Sopenharmony_ci		     ILT_ENTRY_IN_REGS);
23898c2ecf20Sopenharmony_ci
23908c2ecf20Sopenharmony_ci		/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a
23918c2ecf20Sopenharmony_ci		 * wide-bus.
23928c2ecf20Sopenharmony_ci		 */
23938c2ecf20Sopenharmony_ci		qed_dmae_host2grc(p_hwfn, p_ptt,
23948c2ecf20Sopenharmony_ci				  (u64) (uintptr_t) &ilt_hw_entry,
23958c2ecf20Sopenharmony_ci				  reg_offset,
23968c2ecf20Sopenharmony_ci				  sizeof(ilt_hw_entry) / sizeof(u32),
23978c2ecf20Sopenharmony_ci				  NULL);
23988c2ecf20Sopenharmony_ci	}
23998c2ecf20Sopenharmony_ci
24008c2ecf20Sopenharmony_ci	qed_ptt_release(p_hwfn, p_ptt);
24018c2ecf20Sopenharmony_ci
24028c2ecf20Sopenharmony_ci	return 0;
24038c2ecf20Sopenharmony_ci}
24048c2ecf20Sopenharmony_ci
24058c2ecf20Sopenharmony_ciint qed_cxt_free_proto_ilt(struct qed_hwfn *p_hwfn, enum protocol_type proto)
24068c2ecf20Sopenharmony_ci{
24078c2ecf20Sopenharmony_ci	int rc;
24088c2ecf20Sopenharmony_ci	u32 cid;
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_ci	/* Free Connection CXT */
24118c2ecf20Sopenharmony_ci	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_CXT,
24128c2ecf20Sopenharmony_ci				    qed_cxt_get_proto_cid_start(p_hwfn,
24138c2ecf20Sopenharmony_ci								proto),
24148c2ecf20Sopenharmony_ci				    qed_cxt_get_proto_cid_count(p_hwfn,
24158c2ecf20Sopenharmony_ci								proto, &cid));
24168c2ecf20Sopenharmony_ci
24178c2ecf20Sopenharmony_ci	if (rc)
24188c2ecf20Sopenharmony_ci		return rc;
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci	/* Free Task CXT ( Intentionally RoCE as task-id is shared between
24218c2ecf20Sopenharmony_ci	 * RoCE and iWARP )
24228c2ecf20Sopenharmony_ci	 */
24238c2ecf20Sopenharmony_ci	proto = PROTOCOLID_ROCE;
24248c2ecf20Sopenharmony_ci	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_TASK, 0,
24258c2ecf20Sopenharmony_ci				    qed_cxt_get_proto_tid_count(p_hwfn, proto));
24268c2ecf20Sopenharmony_ci	if (rc)
24278c2ecf20Sopenharmony_ci		return rc;
24288c2ecf20Sopenharmony_ci
24298c2ecf20Sopenharmony_ci	/* Free TSDM CXT */
24308c2ecf20Sopenharmony_ci	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_XRC_SRQ, 0,
24318c2ecf20Sopenharmony_ci				    p_hwfn->p_cxt_mngr->xrc_srq_count);
24328c2ecf20Sopenharmony_ci
24338c2ecf20Sopenharmony_ci	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_SRQ,
24348c2ecf20Sopenharmony_ci				    p_hwfn->p_cxt_mngr->xrc_srq_count,
24358c2ecf20Sopenharmony_ci				    p_hwfn->p_cxt_mngr->srq_count);
24368c2ecf20Sopenharmony_ci
24378c2ecf20Sopenharmony_ci	return rc;
24388c2ecf20Sopenharmony_ci}
24398c2ecf20Sopenharmony_ci
24408c2ecf20Sopenharmony_ciint qed_cxt_get_task_ctx(struct qed_hwfn *p_hwfn,
24418c2ecf20Sopenharmony_ci			 u32 tid, u8 ctx_type, void **pp_task_ctx)
24428c2ecf20Sopenharmony_ci{
24438c2ecf20Sopenharmony_ci	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
24448c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
24458c2ecf20Sopenharmony_ci	struct qed_tid_seg *p_seg_info;
24468c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_seg;
24478c2ecf20Sopenharmony_ci	u32 num_tids_per_block;
24488c2ecf20Sopenharmony_ci	u32 tid_size, ilt_idx;
24498c2ecf20Sopenharmony_ci	u32 total_lines;
24508c2ecf20Sopenharmony_ci	u32 proto, seg;
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci	/* Verify the personality */
24538c2ecf20Sopenharmony_ci	switch (p_hwfn->hw_info.personality) {
24548c2ecf20Sopenharmony_ci	case QED_PCI_FCOE:
24558c2ecf20Sopenharmony_ci		proto = PROTOCOLID_FCOE;
24568c2ecf20Sopenharmony_ci		seg = QED_CXT_FCOE_TID_SEG;
24578c2ecf20Sopenharmony_ci		break;
24588c2ecf20Sopenharmony_ci	case QED_PCI_ISCSI:
24598c2ecf20Sopenharmony_ci		proto = PROTOCOLID_ISCSI;
24608c2ecf20Sopenharmony_ci		seg = QED_CXT_ISCSI_TID_SEG;
24618c2ecf20Sopenharmony_ci		break;
24628c2ecf20Sopenharmony_ci	default:
24638c2ecf20Sopenharmony_ci		return -EINVAL;
24648c2ecf20Sopenharmony_ci	}
24658c2ecf20Sopenharmony_ci
24668c2ecf20Sopenharmony_ci	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
24678c2ecf20Sopenharmony_ci	if (!p_cli->active)
24688c2ecf20Sopenharmony_ci		return -EINVAL;
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_ci	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
24718c2ecf20Sopenharmony_ci
24728c2ecf20Sopenharmony_ci	if (ctx_type == QED_CTX_WORKING_MEM) {
24738c2ecf20Sopenharmony_ci		p_seg = &p_cli->pf_blks[CDUT_SEG_BLK(seg)];
24748c2ecf20Sopenharmony_ci	} else if (ctx_type == QED_CTX_FL_MEM) {
24758c2ecf20Sopenharmony_ci		if (!p_seg_info->has_fl_mem)
24768c2ecf20Sopenharmony_ci			return -EINVAL;
24778c2ecf20Sopenharmony_ci		p_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
24788c2ecf20Sopenharmony_ci	} else {
24798c2ecf20Sopenharmony_ci		return -EINVAL;
24808c2ecf20Sopenharmony_ci	}
24818c2ecf20Sopenharmony_ci	total_lines = DIV_ROUND_UP(p_seg->total_size, p_seg->real_size_in_page);
24828c2ecf20Sopenharmony_ci	tid_size = p_mngr->task_type_size[p_seg_info->type];
24838c2ecf20Sopenharmony_ci	num_tids_per_block = p_seg->real_size_in_page / tid_size;
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_ci	if (total_lines < tid / num_tids_per_block)
24868c2ecf20Sopenharmony_ci		return -EINVAL;
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci	ilt_idx = tid / num_tids_per_block + p_seg->start_line -
24898c2ecf20Sopenharmony_ci		  p_mngr->pf_start_line;
24908c2ecf20Sopenharmony_ci	*pp_task_ctx = (u8 *)p_mngr->ilt_shadow[ilt_idx].virt_addr +
24918c2ecf20Sopenharmony_ci		       (tid % num_tids_per_block) * tid_size;
24928c2ecf20Sopenharmony_ci
24938c2ecf20Sopenharmony_ci	return 0;
24948c2ecf20Sopenharmony_ci}
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_cistatic u16 qed_blk_calculate_pages(struct qed_ilt_cli_blk *p_blk)
24978c2ecf20Sopenharmony_ci{
24988c2ecf20Sopenharmony_ci	if (p_blk->real_size_in_page == 0)
24998c2ecf20Sopenharmony_ci		return 0;
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_ci	return DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
25028c2ecf20Sopenharmony_ci}
25038c2ecf20Sopenharmony_ci
25048c2ecf20Sopenharmony_ciu16 qed_get_cdut_num_pf_init_pages(struct qed_hwfn *p_hwfn)
25058c2ecf20Sopenharmony_ci{
25068c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
25078c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
25088c2ecf20Sopenharmony_ci	u16 i, pages = 0;
25098c2ecf20Sopenharmony_ci
25108c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
25118c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
25128c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)];
25138c2ecf20Sopenharmony_ci		pages += qed_blk_calculate_pages(p_blk);
25148c2ecf20Sopenharmony_ci	}
25158c2ecf20Sopenharmony_ci
25168c2ecf20Sopenharmony_ci	return pages;
25178c2ecf20Sopenharmony_ci}
25188c2ecf20Sopenharmony_ci
25198c2ecf20Sopenharmony_ciu16 qed_get_cdut_num_vf_init_pages(struct qed_hwfn *p_hwfn)
25208c2ecf20Sopenharmony_ci{
25218c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
25228c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
25238c2ecf20Sopenharmony_ci	u16 i, pages = 0;
25248c2ecf20Sopenharmony_ci
25258c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
25268c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_VF_SEGMENTS; i++) {
25278c2ecf20Sopenharmony_ci		p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(i, VF)];
25288c2ecf20Sopenharmony_ci		pages += qed_blk_calculate_pages(p_blk);
25298c2ecf20Sopenharmony_ci	}
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci	return pages;
25328c2ecf20Sopenharmony_ci}
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ciu16 qed_get_cdut_num_pf_work_pages(struct qed_hwfn *p_hwfn)
25358c2ecf20Sopenharmony_ci{
25368c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
25378c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
25388c2ecf20Sopenharmony_ci	u16 i, pages = 0;
25398c2ecf20Sopenharmony_ci
25408c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
25418c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
25428c2ecf20Sopenharmony_ci		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(i)];
25438c2ecf20Sopenharmony_ci		pages += qed_blk_calculate_pages(p_blk);
25448c2ecf20Sopenharmony_ci	}
25458c2ecf20Sopenharmony_ci
25468c2ecf20Sopenharmony_ci	return pages;
25478c2ecf20Sopenharmony_ci}
25488c2ecf20Sopenharmony_ci
25498c2ecf20Sopenharmony_ciu16 qed_get_cdut_num_vf_work_pages(struct qed_hwfn *p_hwfn)
25508c2ecf20Sopenharmony_ci{
25518c2ecf20Sopenharmony_ci	struct qed_ilt_client_cfg *p_cli;
25528c2ecf20Sopenharmony_ci	struct qed_ilt_cli_blk *p_blk;
25538c2ecf20Sopenharmony_ci	u16 pages = 0, i;
25548c2ecf20Sopenharmony_ci
25558c2ecf20Sopenharmony_ci	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
25568c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_TASK_VF_SEGMENTS; i++) {
25578c2ecf20Sopenharmony_ci		p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(i)];
25588c2ecf20Sopenharmony_ci		pages += qed_blk_calculate_pages(p_blk);
25598c2ecf20Sopenharmony_ci	}
25608c2ecf20Sopenharmony_ci
25618c2ecf20Sopenharmony_ci	return pages;
25628c2ecf20Sopenharmony_ci}
2563