xref: /linux/drivers/net/ethernet/qlogic/qed/qed_cxt.c (revision 51ff17251c9c2c2e71974149d22bc73ea09c27cc)
1fe56b9e6SYuval Mintz /* QLogic qed NIC Driver
2fe56b9e6SYuval Mintz  * Copyright (c) 2015 QLogic Corporation
3fe56b9e6SYuval Mintz  *
4fe56b9e6SYuval Mintz  * This software is available under the terms of the GNU General Public License
5fe56b9e6SYuval Mintz  * (GPL) Version 2, available from the file COPYING in the main directory of
6fe56b9e6SYuval Mintz  * this source tree.
7fe56b9e6SYuval Mintz  */
8fe56b9e6SYuval Mintz 
9fe56b9e6SYuval Mintz #include <linux/types.h>
10fe56b9e6SYuval Mintz #include <linux/bitops.h>
11fe56b9e6SYuval Mintz #include <linux/dma-mapping.h>
12fe56b9e6SYuval Mintz #include <linux/errno.h>
13fe56b9e6SYuval Mintz #include <linux/kernel.h>
14fe56b9e6SYuval Mintz #include <linux/list.h>
15fe56b9e6SYuval Mintz #include <linux/log2.h>
16fe56b9e6SYuval Mintz #include <linux/pci.h>
17fe56b9e6SYuval Mintz #include <linux/slab.h>
18fe56b9e6SYuval Mintz #include <linux/string.h>
19fe56b9e6SYuval Mintz #include <linux/bitops.h>
20fe56b9e6SYuval Mintz #include "qed.h"
21fe56b9e6SYuval Mintz #include "qed_cxt.h"
22fe56b9e6SYuval Mintz #include "qed_dev_api.h"
23fe56b9e6SYuval Mintz #include "qed_hsi.h"
24fe56b9e6SYuval Mintz #include "qed_hw.h"
25fe56b9e6SYuval Mintz #include "qed_init_ops.h"
26fe56b9e6SYuval Mintz #include "qed_reg_addr.h"
271408cc1fSYuval Mintz #include "qed_sriov.h"
28fe56b9e6SYuval Mintz 
29fe56b9e6SYuval Mintz /* Max number of connection types in HW (DQ/CDU etc.) */
30fe56b9e6SYuval Mintz #define MAX_CONN_TYPES		PROTOCOLID_COMMON
31fe56b9e6SYuval Mintz #define NUM_TASK_TYPES		2
32fe56b9e6SYuval Mintz #define NUM_TASK_PF_SEGMENTS	4
331408cc1fSYuval Mintz #define NUM_TASK_VF_SEGMENTS	1
34fe56b9e6SYuval Mintz 
35fe56b9e6SYuval Mintz /* QM constants */
36fe56b9e6SYuval Mintz #define QM_PQ_ELEMENT_SIZE	4 /* in bytes */
37fe56b9e6SYuval Mintz 
38fe56b9e6SYuval Mintz /* Doorbell-Queue constants */
39fe56b9e6SYuval Mintz #define DQ_RANGE_SHIFT		4
40fe56b9e6SYuval Mintz #define DQ_RANGE_ALIGN		BIT(DQ_RANGE_SHIFT)
41fe56b9e6SYuval Mintz 
42dbb799c3SYuval Mintz /* Searcher constants */
43dbb799c3SYuval Mintz #define SRC_MIN_NUM_ELEMS 256
44dbb799c3SYuval Mintz 
45dbb799c3SYuval Mintz /* Timers constants */
46dbb799c3SYuval Mintz #define TM_SHIFT        7
47dbb799c3SYuval Mintz #define TM_ALIGN        BIT(TM_SHIFT)
48dbb799c3SYuval Mintz #define TM_ELEM_SIZE    4
49dbb799c3SYuval Mintz 
50fe56b9e6SYuval Mintz /* ILT constants */
51*51ff1725SRam Amrani #if IS_ENABLED(CONFIG_INFINIBAND_QEDR)
52*51ff1725SRam Amrani /* For RoCE we configure to 64K to cover for RoCE max tasks 256K purpose. */
53*51ff1725SRam Amrani #define ILT_DEFAULT_HW_P_SIZE		4
54*51ff1725SRam Amrani #else
55fe56b9e6SYuval Mintz #define ILT_DEFAULT_HW_P_SIZE		3
56*51ff1725SRam Amrani #endif
57*51ff1725SRam Amrani 
58fe56b9e6SYuval Mintz #define ILT_PAGE_IN_BYTES(hw_p_size)	(1U << ((hw_p_size) + 12))
59fe56b9e6SYuval Mintz #define ILT_CFG_REG(cli, reg)	PSWRQ2_REG_ ## cli ## _ ## reg ## _RT_OFFSET
60fe56b9e6SYuval Mintz 
61fe56b9e6SYuval Mintz /* ILT entry structure */
62fe56b9e6SYuval Mintz #define ILT_ENTRY_PHY_ADDR_MASK		0x000FFFFFFFFFFFULL
63fe56b9e6SYuval Mintz #define ILT_ENTRY_PHY_ADDR_SHIFT	0
64fe56b9e6SYuval Mintz #define ILT_ENTRY_VALID_MASK		0x1ULL
65fe56b9e6SYuval Mintz #define ILT_ENTRY_VALID_SHIFT		52
66fe56b9e6SYuval Mintz #define ILT_ENTRY_IN_REGS		2
67fe56b9e6SYuval Mintz #define ILT_REG_SIZE_IN_BYTES		4
68fe56b9e6SYuval Mintz 
69fe56b9e6SYuval Mintz /* connection context union */
70fe56b9e6SYuval Mintz union conn_context {
71fe56b9e6SYuval Mintz 	struct core_conn_context core_ctx;
72fe56b9e6SYuval Mintz 	struct eth_conn_context eth_ctx;
73dbb799c3SYuval Mintz 	struct iscsi_conn_context iscsi_ctx;
74dbb799c3SYuval Mintz 	struct roce_conn_context roce_ctx;
75fe56b9e6SYuval Mintz };
76fe56b9e6SYuval Mintz 
77dbb799c3SYuval Mintz /* TYPE-0 task context - iSCSI */
78dbb799c3SYuval Mintz union type0_task_context {
79dbb799c3SYuval Mintz 	struct iscsi_task_context iscsi_ctx;
80dbb799c3SYuval Mintz };
81dbb799c3SYuval Mintz 
82dbb799c3SYuval Mintz /* TYPE-1 task context - ROCE */
83dbb799c3SYuval Mintz union type1_task_context {
84dbb799c3SYuval Mintz 	struct rdma_task_context roce_ctx;
85dbb799c3SYuval Mintz };
86dbb799c3SYuval Mintz 
87dbb799c3SYuval Mintz struct src_ent {
88dbb799c3SYuval Mintz 	u8 opaque[56];
89dbb799c3SYuval Mintz 	u64 next;
90dbb799c3SYuval Mintz };
91dbb799c3SYuval Mintz 
92dbb799c3SYuval Mintz #define CDUT_SEG_ALIGNMET 3	/* in 4k chunks */
93dbb799c3SYuval Mintz #define CDUT_SEG_ALIGNMET_IN_BYTES (1 << (CDUT_SEG_ALIGNMET + 12))
94dbb799c3SYuval Mintz 
95fe56b9e6SYuval Mintz #define CONN_CXT_SIZE(p_hwfn) \
96fe56b9e6SYuval Mintz 	ALIGNED_TYPE_SIZE(union conn_context, p_hwfn)
97fe56b9e6SYuval Mintz 
98dbb799c3SYuval Mintz #define SRQ_CXT_SIZE (sizeof(struct rdma_srq_context))
99dbb799c3SYuval Mintz 
100dbb799c3SYuval Mintz #define TYPE0_TASK_CXT_SIZE(p_hwfn) \
101dbb799c3SYuval Mintz 	ALIGNED_TYPE_SIZE(union type0_task_context, p_hwfn)
102dbb799c3SYuval Mintz 
103dbb799c3SYuval Mintz /* Alignment is inherent to the type1_task_context structure */
104dbb799c3SYuval Mintz #define TYPE1_TASK_CXT_SIZE(p_hwfn) sizeof(union type1_task_context)
105dbb799c3SYuval Mintz 
106fe56b9e6SYuval Mintz /* PF per protocl configuration object */
107dbb799c3SYuval Mintz #define TASK_SEGMENTS   (NUM_TASK_PF_SEGMENTS + NUM_TASK_VF_SEGMENTS)
108dbb799c3SYuval Mintz #define TASK_SEGMENT_VF (NUM_TASK_PF_SEGMENTS)
109dbb799c3SYuval Mintz 
110dbb799c3SYuval Mintz struct qed_tid_seg {
111dbb799c3SYuval Mintz 	u32 count;
112dbb799c3SYuval Mintz 	u8 type;
113dbb799c3SYuval Mintz 	bool has_fl_mem;
114dbb799c3SYuval Mintz };
115dbb799c3SYuval Mintz 
116fe56b9e6SYuval Mintz struct qed_conn_type_cfg {
117fe56b9e6SYuval Mintz 	u32 cid_count;
118fe56b9e6SYuval Mintz 	u32 cid_start;
1191408cc1fSYuval Mintz 	u32 cids_per_vf;
120dbb799c3SYuval Mintz 	struct qed_tid_seg tid_seg[TASK_SEGMENTS];
121fe56b9e6SYuval Mintz };
122fe56b9e6SYuval Mintz 
123fe56b9e6SYuval Mintz /* ILT Client configuration, Per connection type (protocol) resources. */
124fe56b9e6SYuval Mintz #define ILT_CLI_PF_BLOCKS	(1 + NUM_TASK_PF_SEGMENTS * 2)
1251408cc1fSYuval Mintz #define ILT_CLI_VF_BLOCKS       (1 + NUM_TASK_VF_SEGMENTS * 2)
126fe56b9e6SYuval Mintz #define CDUC_BLK		(0)
127dbb799c3SYuval Mintz #define SRQ_BLK                 (0)
128dbb799c3SYuval Mintz #define CDUT_SEG_BLK(n)         (1 + (u8)(n))
129dbb799c3SYuval Mintz #define CDUT_FL_SEG_BLK(n, X)   (1 + (n) + NUM_TASK_ ## X ## _SEGMENTS)
130fe56b9e6SYuval Mintz 
131fe56b9e6SYuval Mintz enum ilt_clients {
132fe56b9e6SYuval Mintz 	ILT_CLI_CDUC,
133dbb799c3SYuval Mintz 	ILT_CLI_CDUT,
134fe56b9e6SYuval Mintz 	ILT_CLI_QM,
135dbb799c3SYuval Mintz 	ILT_CLI_TM,
136dbb799c3SYuval Mintz 	ILT_CLI_SRC,
137dbb799c3SYuval Mintz 	ILT_CLI_TSDM,
138fe56b9e6SYuval Mintz 	ILT_CLI_MAX
139fe56b9e6SYuval Mintz };
140fe56b9e6SYuval Mintz 
141fe56b9e6SYuval Mintz struct ilt_cfg_pair {
142fe56b9e6SYuval Mintz 	u32 reg;
143fe56b9e6SYuval Mintz 	u32 val;
144fe56b9e6SYuval Mintz };
145fe56b9e6SYuval Mintz 
146fe56b9e6SYuval Mintz struct qed_ilt_cli_blk {
147fe56b9e6SYuval Mintz 	u32 total_size; /* 0 means not active */
148fe56b9e6SYuval Mintz 	u32 real_size_in_page;
149fe56b9e6SYuval Mintz 	u32 start_line;
150dbb799c3SYuval Mintz 	u32 dynamic_line_cnt;
151fe56b9e6SYuval Mintz };
152fe56b9e6SYuval Mintz 
153fe56b9e6SYuval Mintz struct qed_ilt_client_cfg {
154fe56b9e6SYuval Mintz 	bool active;
155fe56b9e6SYuval Mintz 
156fe56b9e6SYuval Mintz 	/* ILT boundaries */
157fe56b9e6SYuval Mintz 	struct ilt_cfg_pair first;
158fe56b9e6SYuval Mintz 	struct ilt_cfg_pair last;
159fe56b9e6SYuval Mintz 	struct ilt_cfg_pair p_size;
160fe56b9e6SYuval Mintz 
161fe56b9e6SYuval Mintz 	/* ILT client blocks for PF */
162fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk pf_blks[ILT_CLI_PF_BLOCKS];
163fe56b9e6SYuval Mintz 	u32 pf_total_lines;
1641408cc1fSYuval Mintz 
1651408cc1fSYuval Mintz 	/* ILT client blocks for VFs */
1661408cc1fSYuval Mintz 	struct qed_ilt_cli_blk vf_blks[ILT_CLI_VF_BLOCKS];
1671408cc1fSYuval Mintz 	u32 vf_total_lines;
168fe56b9e6SYuval Mintz };
169fe56b9e6SYuval Mintz 
170fe56b9e6SYuval Mintz /* Per Path -
171fe56b9e6SYuval Mintz  *      ILT shadow table
172fe56b9e6SYuval Mintz  *      Protocol acquired CID lists
173fe56b9e6SYuval Mintz  *      PF start line in ILT
174fe56b9e6SYuval Mintz  */
175fe56b9e6SYuval Mintz struct qed_dma_mem {
176fe56b9e6SYuval Mintz 	dma_addr_t p_phys;
177fe56b9e6SYuval Mintz 	void *p_virt;
178fe56b9e6SYuval Mintz 	size_t size;
179fe56b9e6SYuval Mintz };
180fe56b9e6SYuval Mintz 
181fe56b9e6SYuval Mintz struct qed_cid_acquired_map {
182fe56b9e6SYuval Mintz 	u32		start_cid;
183fe56b9e6SYuval Mintz 	u32		max_count;
184fe56b9e6SYuval Mintz 	unsigned long	*cid_map;
185fe56b9e6SYuval Mintz };
186fe56b9e6SYuval Mintz 
187fe56b9e6SYuval Mintz struct qed_cxt_mngr {
188fe56b9e6SYuval Mintz 	/* Per protocl configuration */
189fe56b9e6SYuval Mintz 	struct qed_conn_type_cfg	conn_cfg[MAX_CONN_TYPES];
190fe56b9e6SYuval Mintz 
191fe56b9e6SYuval Mintz 	/* computed ILT structure */
192fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg	clients[ILT_CLI_MAX];
193fe56b9e6SYuval Mintz 
194dbb799c3SYuval Mintz 	/* Task type sizes */
195dbb799c3SYuval Mintz 	u32 task_type_size[NUM_TASK_TYPES];
196dbb799c3SYuval Mintz 
1971408cc1fSYuval Mintz 	/* total number of VFs for this hwfn -
1981408cc1fSYuval Mintz 	 * ALL VFs are symmetric in terms of HW resources
1991408cc1fSYuval Mintz 	 */
2001408cc1fSYuval Mintz 	u32				vf_count;
2011408cc1fSYuval Mintz 
202dbb799c3SYuval Mintz 	/* total number of SRQ's for this hwfn */
203dbb799c3SYuval Mintz 	u32 srq_count;
204dbb799c3SYuval Mintz 
205fe56b9e6SYuval Mintz 	/* Acquired CIDs */
206fe56b9e6SYuval Mintz 	struct qed_cid_acquired_map	acquired[MAX_CONN_TYPES];
207fe56b9e6SYuval Mintz 
208fe56b9e6SYuval Mintz 	/* ILT  shadow table */
209fe56b9e6SYuval Mintz 	struct qed_dma_mem		*ilt_shadow;
210fe56b9e6SYuval Mintz 	u32				pf_start_line;
211dbb799c3SYuval Mintz 
212dbb799c3SYuval Mintz 	/* Mutex for a dynamic ILT allocation */
213dbb799c3SYuval Mintz 	struct mutex mutex;
214dbb799c3SYuval Mintz 
215dbb799c3SYuval Mintz 	/* SRC T2 */
216dbb799c3SYuval Mintz 	struct qed_dma_mem *t2;
217dbb799c3SYuval Mintz 	u32 t2_num_pages;
218dbb799c3SYuval Mintz 	u64 first_free;
219dbb799c3SYuval Mintz 	u64 last_free;
220fe56b9e6SYuval Mintz };
221dbb799c3SYuval Mintz static bool src_proto(enum protocol_type type)
222dbb799c3SYuval Mintz {
223dbb799c3SYuval Mintz 	return type == PROTOCOLID_ISCSI ||
224dbb799c3SYuval Mintz 	       type == PROTOCOLID_ROCE;
225dbb799c3SYuval Mintz }
226dbb799c3SYuval Mintz 
227dbb799c3SYuval Mintz static bool tm_cid_proto(enum protocol_type type)
228dbb799c3SYuval Mintz {
229dbb799c3SYuval Mintz 	return type == PROTOCOLID_ISCSI ||
230dbb799c3SYuval Mintz 	       type == PROTOCOLID_ROCE;
231dbb799c3SYuval Mintz }
232fe56b9e6SYuval Mintz 
2331408cc1fSYuval Mintz /* counts the iids for the CDU/CDUC ILT client configuration */
2341408cc1fSYuval Mintz struct qed_cdu_iids {
2351408cc1fSYuval Mintz 	u32 pf_cids;
2361408cc1fSYuval Mintz 	u32 per_vf_cids;
2371408cc1fSYuval Mintz };
2381408cc1fSYuval Mintz 
2391408cc1fSYuval Mintz static void qed_cxt_cdu_iids(struct qed_cxt_mngr *p_mngr,
2401408cc1fSYuval Mintz 			     struct qed_cdu_iids *iids)
241fe56b9e6SYuval Mintz {
2421408cc1fSYuval Mintz 	u32 type;
243fe56b9e6SYuval Mintz 
2441408cc1fSYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
2451408cc1fSYuval Mintz 		iids->pf_cids += p_mngr->conn_cfg[type].cid_count;
2461408cc1fSYuval Mintz 		iids->per_vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
2471408cc1fSYuval Mintz 	}
248fe56b9e6SYuval Mintz }
249fe56b9e6SYuval Mintz 
250dbb799c3SYuval Mintz /* counts the iids for the Searcher block configuration */
251dbb799c3SYuval Mintz struct qed_src_iids {
252dbb799c3SYuval Mintz 	u32 pf_cids;
253dbb799c3SYuval Mintz 	u32 per_vf_cids;
254dbb799c3SYuval Mintz };
255dbb799c3SYuval Mintz 
256dbb799c3SYuval Mintz static void qed_cxt_src_iids(struct qed_cxt_mngr *p_mngr,
257dbb799c3SYuval Mintz 			     struct qed_src_iids *iids)
258dbb799c3SYuval Mintz {
259dbb799c3SYuval Mintz 	u32 i;
260dbb799c3SYuval Mintz 
261dbb799c3SYuval Mintz 	for (i = 0; i < MAX_CONN_TYPES; i++) {
262dbb799c3SYuval Mintz 		if (!src_proto(i))
263dbb799c3SYuval Mintz 			continue;
264dbb799c3SYuval Mintz 
265dbb799c3SYuval Mintz 		iids->pf_cids += p_mngr->conn_cfg[i].cid_count;
266dbb799c3SYuval Mintz 		iids->per_vf_cids += p_mngr->conn_cfg[i].cids_per_vf;
267dbb799c3SYuval Mintz 	}
268dbb799c3SYuval Mintz }
269dbb799c3SYuval Mintz 
270dbb799c3SYuval Mintz /* counts the iids for the Timers block configuration */
271dbb799c3SYuval Mintz struct qed_tm_iids {
272dbb799c3SYuval Mintz 	u32 pf_cids;
273dbb799c3SYuval Mintz 	u32 pf_tids[NUM_TASK_PF_SEGMENTS];	/* per segment */
274dbb799c3SYuval Mintz 	u32 pf_tids_total;
275dbb799c3SYuval Mintz 	u32 per_vf_cids;
276dbb799c3SYuval Mintz 	u32 per_vf_tids;
277dbb799c3SYuval Mintz };
278dbb799c3SYuval Mintz 
279dbb799c3SYuval Mintz static void qed_cxt_tm_iids(struct qed_cxt_mngr *p_mngr,
280dbb799c3SYuval Mintz 			    struct qed_tm_iids *iids)
281dbb799c3SYuval Mintz {
282dbb799c3SYuval Mintz 	u32 i, j;
283dbb799c3SYuval Mintz 
284dbb799c3SYuval Mintz 	for (i = 0; i < MAX_CONN_TYPES; i++) {
285dbb799c3SYuval Mintz 		struct qed_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[i];
286dbb799c3SYuval Mintz 
287dbb799c3SYuval Mintz 		if (tm_cid_proto(i)) {
288dbb799c3SYuval Mintz 			iids->pf_cids += p_cfg->cid_count;
289dbb799c3SYuval Mintz 			iids->per_vf_cids += p_cfg->cids_per_vf;
290dbb799c3SYuval Mintz 		}
291dbb799c3SYuval Mintz 	}
292dbb799c3SYuval Mintz 
293dbb799c3SYuval Mintz 	iids->pf_cids = roundup(iids->pf_cids, TM_ALIGN);
294dbb799c3SYuval Mintz 	iids->per_vf_cids = roundup(iids->per_vf_cids, TM_ALIGN);
295dbb799c3SYuval Mintz 	iids->per_vf_tids = roundup(iids->per_vf_tids, TM_ALIGN);
296dbb799c3SYuval Mintz 
297dbb799c3SYuval Mintz 	for (iids->pf_tids_total = 0, j = 0; j < NUM_TASK_PF_SEGMENTS; j++) {
298dbb799c3SYuval Mintz 		iids->pf_tids[j] = roundup(iids->pf_tids[j], TM_ALIGN);
299dbb799c3SYuval Mintz 		iids->pf_tids_total += iids->pf_tids[j];
300dbb799c3SYuval Mintz 	}
301dbb799c3SYuval Mintz }
302dbb799c3SYuval Mintz 
303fe56b9e6SYuval Mintz static void qed_cxt_qm_iids(struct qed_hwfn *p_hwfn,
304fe56b9e6SYuval Mintz 			    struct qed_qm_iids *iids)
305fe56b9e6SYuval Mintz {
306fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
307dbb799c3SYuval Mintz 	struct qed_tid_seg *segs;
308dbb799c3SYuval Mintz 	u32 vf_cids = 0, type, j;
309dbb799c3SYuval Mintz 	u32 vf_tids = 0;
310fe56b9e6SYuval Mintz 
3111408cc1fSYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
312fe56b9e6SYuval Mintz 		iids->cids += p_mngr->conn_cfg[type].cid_count;
3131408cc1fSYuval Mintz 		vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
314dbb799c3SYuval Mintz 
315dbb799c3SYuval Mintz 		segs = p_mngr->conn_cfg[type].tid_seg;
316dbb799c3SYuval Mintz 		/* for each segment there is at most one
317dbb799c3SYuval Mintz 		 * protocol for which count is not 0.
318dbb799c3SYuval Mintz 		 */
319dbb799c3SYuval Mintz 		for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
320dbb799c3SYuval Mintz 			iids->tids += segs[j].count;
321dbb799c3SYuval Mintz 
322dbb799c3SYuval Mintz 		/* The last array elelment is for the VFs. As for PF
323dbb799c3SYuval Mintz 		 * segments there can be only one protocol for
324dbb799c3SYuval Mintz 		 * which this value is not 0.
325dbb799c3SYuval Mintz 		 */
326dbb799c3SYuval Mintz 		vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
3271408cc1fSYuval Mintz 	}
328fe56b9e6SYuval Mintz 
3291408cc1fSYuval Mintz 	iids->vf_cids += vf_cids * p_mngr->vf_count;
330dbb799c3SYuval Mintz 	iids->tids += vf_tids * p_mngr->vf_count;
331dbb799c3SYuval Mintz 
3321408cc1fSYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
333dbb799c3SYuval Mintz 		   "iids: CIDS %08x vf_cids %08x tids %08x vf_tids %08x\n",
334dbb799c3SYuval Mintz 		   iids->cids, iids->vf_cids, iids->tids, vf_tids);
335dbb799c3SYuval Mintz }
336dbb799c3SYuval Mintz 
337dbb799c3SYuval Mintz static struct qed_tid_seg *qed_cxt_tid_seg_info(struct qed_hwfn *p_hwfn,
338dbb799c3SYuval Mintz 						u32 seg)
339dbb799c3SYuval Mintz {
340dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_cfg = p_hwfn->p_cxt_mngr;
341dbb799c3SYuval Mintz 	u32 i;
342dbb799c3SYuval Mintz 
343dbb799c3SYuval Mintz 	/* Find the protocol with tid count > 0 for this segment.
344dbb799c3SYuval Mintz 	 * Note: there can only be one and this is already validated.
345dbb799c3SYuval Mintz 	 */
346dbb799c3SYuval Mintz 	for (i = 0; i < MAX_CONN_TYPES; i++)
347dbb799c3SYuval Mintz 		if (p_cfg->conn_cfg[i].tid_seg[seg].count)
348dbb799c3SYuval Mintz 			return &p_cfg->conn_cfg[i].tid_seg[seg];
349dbb799c3SYuval Mintz 	return NULL;
350dbb799c3SYuval Mintz }
351dbb799c3SYuval Mintz 
352dbb799c3SYuval Mintz void qed_cxt_set_srq_count(struct qed_hwfn *p_hwfn, u32 num_srqs)
353dbb799c3SYuval Mintz {
354dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
355dbb799c3SYuval Mintz 
356dbb799c3SYuval Mintz 	p_mgr->srq_count = num_srqs;
357dbb799c3SYuval Mintz }
358dbb799c3SYuval Mintz 
359dbb799c3SYuval Mintz u32 qed_cxt_get_srq_count(struct qed_hwfn *p_hwfn)
360dbb799c3SYuval Mintz {
361dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
362dbb799c3SYuval Mintz 
363dbb799c3SYuval Mintz 	return p_mgr->srq_count;
364fe56b9e6SYuval Mintz }
365fe56b9e6SYuval Mintz 
366fe56b9e6SYuval Mintz /* set the iids count per protocol */
367fe56b9e6SYuval Mintz static void qed_cxt_set_proto_cid_count(struct qed_hwfn *p_hwfn,
368fe56b9e6SYuval Mintz 					enum protocol_type type,
3691408cc1fSYuval Mintz 					u32 cid_count, u32 vf_cid_cnt)
370fe56b9e6SYuval Mintz {
371fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
372fe56b9e6SYuval Mintz 	struct qed_conn_type_cfg *p_conn = &p_mgr->conn_cfg[type];
373fe56b9e6SYuval Mintz 
374fe56b9e6SYuval Mintz 	p_conn->cid_count = roundup(cid_count, DQ_RANGE_ALIGN);
3751408cc1fSYuval Mintz 	p_conn->cids_per_vf = roundup(vf_cid_cnt, DQ_RANGE_ALIGN);
376dbb799c3SYuval Mintz 
377dbb799c3SYuval Mintz 	if (type == PROTOCOLID_ROCE) {
378dbb799c3SYuval Mintz 		u32 page_sz = p_mgr->clients[ILT_CLI_CDUC].p_size.val;
379dbb799c3SYuval Mintz 		u32 cxt_size = CONN_CXT_SIZE(p_hwfn);
380dbb799c3SYuval Mintz 		u32 elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
381dbb799c3SYuval Mintz 
382dbb799c3SYuval Mintz 		p_conn->cid_count = roundup(p_conn->cid_count, elems_per_page);
383dbb799c3SYuval Mintz 	}
3841408cc1fSYuval Mintz }
3851408cc1fSYuval Mintz 
3861408cc1fSYuval Mintz u32 qed_cxt_get_proto_cid_count(struct qed_hwfn *p_hwfn,
3871a635e48SYuval Mintz 				enum protocol_type type, u32 *vf_cid)
3881408cc1fSYuval Mintz {
3891408cc1fSYuval Mintz 	if (vf_cid)
3901408cc1fSYuval Mintz 		*vf_cid = p_hwfn->p_cxt_mngr->conn_cfg[type].cids_per_vf;
3911408cc1fSYuval Mintz 
3921408cc1fSYuval Mintz 	return p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
393fe56b9e6SYuval Mintz }
394fe56b9e6SYuval Mintz 
395dbb799c3SYuval Mintz u32 qed_cxt_get_proto_cid_start(struct qed_hwfn *p_hwfn,
396dbb799c3SYuval Mintz 				enum protocol_type type)
397dbb799c3SYuval Mintz {
398dbb799c3SYuval Mintz 	return p_hwfn->p_cxt_mngr->acquired[type].start_cid;
399dbb799c3SYuval Mintz }
400dbb799c3SYuval Mintz 
401dbb799c3SYuval Mintz u32 qed_cxt_get_proto_tid_count(struct qed_hwfn *p_hwfn,
402dbb799c3SYuval Mintz 				enum protocol_type type)
403dbb799c3SYuval Mintz {
404dbb799c3SYuval Mintz 	u32 cnt = 0;
405dbb799c3SYuval Mintz 	int i;
406dbb799c3SYuval Mintz 
407dbb799c3SYuval Mintz 	for (i = 0; i < TASK_SEGMENTS; i++)
408dbb799c3SYuval Mintz 		cnt += p_hwfn->p_cxt_mngr->conn_cfg[type].tid_seg[i].count;
409dbb799c3SYuval Mintz 
410dbb799c3SYuval Mintz 	return cnt;
411dbb799c3SYuval Mintz }
412dbb799c3SYuval Mintz 
4131a635e48SYuval Mintz static void qed_cxt_set_proto_tid_count(struct qed_hwfn *p_hwfn,
414dbb799c3SYuval Mintz 					enum protocol_type proto,
4151a635e48SYuval Mintz 					u8 seg,
4161a635e48SYuval Mintz 					u8 seg_type, u32 count, bool has_fl)
417dbb799c3SYuval Mintz {
418dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
419dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg = &p_mngr->conn_cfg[proto].tid_seg[seg];
420dbb799c3SYuval Mintz 
421dbb799c3SYuval Mintz 	p_seg->count = count;
422dbb799c3SYuval Mintz 	p_seg->has_fl_mem = has_fl;
423dbb799c3SYuval Mintz 	p_seg->type = seg_type;
424dbb799c3SYuval Mintz }
425dbb799c3SYuval Mintz 
426fe56b9e6SYuval Mintz static void qed_ilt_cli_blk_fill(struct qed_ilt_client_cfg *p_cli,
427fe56b9e6SYuval Mintz 				 struct qed_ilt_cli_blk *p_blk,
4281a635e48SYuval Mintz 				 u32 start_line, u32 total_size, u32 elem_size)
429fe56b9e6SYuval Mintz {
430fe56b9e6SYuval Mintz 	u32 ilt_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
431fe56b9e6SYuval Mintz 
432fe56b9e6SYuval Mintz 	/* verify thatits called only once for each block */
433fe56b9e6SYuval Mintz 	if (p_blk->total_size)
434fe56b9e6SYuval Mintz 		return;
435fe56b9e6SYuval Mintz 
436fe56b9e6SYuval Mintz 	p_blk->total_size = total_size;
437fe56b9e6SYuval Mintz 	p_blk->real_size_in_page = 0;
438fe56b9e6SYuval Mintz 	if (elem_size)
439fe56b9e6SYuval Mintz 		p_blk->real_size_in_page = (ilt_size / elem_size) * elem_size;
440fe56b9e6SYuval Mintz 	p_blk->start_line = start_line;
441fe56b9e6SYuval Mintz }
442fe56b9e6SYuval Mintz 
443fe56b9e6SYuval Mintz static void qed_ilt_cli_adv_line(struct qed_hwfn *p_hwfn,
444fe56b9e6SYuval Mintz 				 struct qed_ilt_client_cfg *p_cli,
445fe56b9e6SYuval Mintz 				 struct qed_ilt_cli_blk *p_blk,
446fe56b9e6SYuval Mintz 				 u32 *p_line, enum ilt_clients client_id)
447fe56b9e6SYuval Mintz {
448fe56b9e6SYuval Mintz 	if (!p_blk->total_size)
449fe56b9e6SYuval Mintz 		return;
450fe56b9e6SYuval Mintz 
451fe56b9e6SYuval Mintz 	if (!p_cli->active)
452fe56b9e6SYuval Mintz 		p_cli->first.val = *p_line;
453fe56b9e6SYuval Mintz 
454fe56b9e6SYuval Mintz 	p_cli->active = true;
4551a635e48SYuval Mintz 	*p_line += DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
456fe56b9e6SYuval Mintz 	p_cli->last.val = *p_line - 1;
457fe56b9e6SYuval Mintz 
458fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
459fe56b9e6SYuval Mintz 		   "ILT[Client %d] - Lines: [%08x - %08x]. Block - Size %08x [Real %08x] Start line %d\n",
460fe56b9e6SYuval Mintz 		   client_id, p_cli->first.val,
461fe56b9e6SYuval Mintz 		   p_cli->last.val, p_blk->total_size,
462fe56b9e6SYuval Mintz 		   p_blk->real_size_in_page, p_blk->start_line);
463fe56b9e6SYuval Mintz }
464fe56b9e6SYuval Mintz 
465dbb799c3SYuval Mintz static u32 qed_ilt_get_dynamic_line_cnt(struct qed_hwfn *p_hwfn,
466dbb799c3SYuval Mintz 					enum ilt_clients ilt_client)
467dbb799c3SYuval Mintz {
468dbb799c3SYuval Mintz 	u32 cid_count = p_hwfn->p_cxt_mngr->conn_cfg[PROTOCOLID_ROCE].cid_count;
469dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
470dbb799c3SYuval Mintz 	u32 lines_to_skip = 0;
471dbb799c3SYuval Mintz 	u32 cxts_per_p;
472dbb799c3SYuval Mintz 
473dbb799c3SYuval Mintz 	if (ilt_client == ILT_CLI_CDUC) {
474dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
475dbb799c3SYuval Mintz 
476dbb799c3SYuval Mintz 		cxts_per_p = ILT_PAGE_IN_BYTES(p_cli->p_size.val) /
477dbb799c3SYuval Mintz 		    (u32) CONN_CXT_SIZE(p_hwfn);
478dbb799c3SYuval Mintz 
479dbb799c3SYuval Mintz 		lines_to_skip = cid_count / cxts_per_p;
480dbb799c3SYuval Mintz 	}
481dbb799c3SYuval Mintz 
482dbb799c3SYuval Mintz 	return lines_to_skip;
483dbb799c3SYuval Mintz }
484dbb799c3SYuval Mintz 
485fe56b9e6SYuval Mintz int qed_cxt_cfg_ilt_compute(struct qed_hwfn *p_hwfn)
486fe56b9e6SYuval Mintz {
487fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
488dbb799c3SYuval Mintz 	u32 curr_line, total, i, task_size, line;
489fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
490fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
4911408cc1fSYuval Mintz 	struct qed_cdu_iids cdu_iids;
492dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
493fe56b9e6SYuval Mintz 	struct qed_qm_iids qm_iids;
494dbb799c3SYuval Mintz 	struct qed_tm_iids tm_iids;
495dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg;
496fe56b9e6SYuval Mintz 
497fe56b9e6SYuval Mintz 	memset(&qm_iids, 0, sizeof(qm_iids));
4981408cc1fSYuval Mintz 	memset(&cdu_iids, 0, sizeof(cdu_iids));
499dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
500dbb799c3SYuval Mintz 	memset(&tm_iids, 0, sizeof(tm_iids));
501fe56b9e6SYuval Mintz 
502fe56b9e6SYuval Mintz 	p_mngr->pf_start_line = RESC_START(p_hwfn, QED_ILT);
503fe56b9e6SYuval Mintz 
504fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
505fe56b9e6SYuval Mintz 		   "hwfn [%d] - Set context manager starting line to be 0x%08x\n",
506fe56b9e6SYuval Mintz 		   p_hwfn->my_id, p_hwfn->p_cxt_mngr->pf_start_line);
507fe56b9e6SYuval Mintz 
508fe56b9e6SYuval Mintz 	/* CDUC */
509fe56b9e6SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUC];
510fe56b9e6SYuval Mintz 	curr_line = p_mngr->pf_start_line;
5111408cc1fSYuval Mintz 
5121408cc1fSYuval Mintz 	/* CDUC PF */
513fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = 0;
514fe56b9e6SYuval Mintz 
515fe56b9e6SYuval Mintz 	/* get the counters for the CDUC and QM clients  */
5161408cc1fSYuval Mintz 	qed_cxt_cdu_iids(p_mngr, &cdu_iids);
517fe56b9e6SYuval Mintz 
518fe56b9e6SYuval Mintz 	p_blk = &p_cli->pf_blks[CDUC_BLK];
519fe56b9e6SYuval Mintz 
5201408cc1fSYuval Mintz 	total = cdu_iids.pf_cids * CONN_CXT_SIZE(p_hwfn);
521fe56b9e6SYuval Mintz 
522fe56b9e6SYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
523fe56b9e6SYuval Mintz 			     total, CONN_CXT_SIZE(p_hwfn));
524fe56b9e6SYuval Mintz 
525fe56b9e6SYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
526fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_blk->start_line;
527fe56b9e6SYuval Mintz 
528dbb799c3SYuval Mintz 	p_blk->dynamic_line_cnt = qed_ilt_get_dynamic_line_cnt(p_hwfn,
529dbb799c3SYuval Mintz 							       ILT_CLI_CDUC);
530dbb799c3SYuval Mintz 
5311408cc1fSYuval Mintz 	/* CDUC VF */
5321408cc1fSYuval Mintz 	p_blk = &p_cli->vf_blks[CDUC_BLK];
5331408cc1fSYuval Mintz 	total = cdu_iids.per_vf_cids * CONN_CXT_SIZE(p_hwfn);
5341408cc1fSYuval Mintz 
5351408cc1fSYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
5361408cc1fSYuval Mintz 			     total, CONN_CXT_SIZE(p_hwfn));
5371408cc1fSYuval Mintz 
5381408cc1fSYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
5391408cc1fSYuval Mintz 	p_cli->vf_total_lines = curr_line - p_blk->start_line;
5401408cc1fSYuval Mintz 
5411408cc1fSYuval Mintz 	for (i = 1; i < p_mngr->vf_count; i++)
5421408cc1fSYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
5431408cc1fSYuval Mintz 				     ILT_CLI_CDUC);
5441408cc1fSYuval Mintz 
545dbb799c3SYuval Mintz 	/* CDUT PF */
546dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
547dbb799c3SYuval Mintz 	p_cli->first.val = curr_line;
548dbb799c3SYuval Mintz 
549dbb799c3SYuval Mintz 	/* first the 'working' task memory */
550dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
551dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
552dbb799c3SYuval Mintz 		if (!p_seg || p_seg->count == 0)
553dbb799c3SYuval Mintz 			continue;
554dbb799c3SYuval Mintz 
555dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(i)];
556dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
557dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total,
558dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
559dbb799c3SYuval Mintz 
560dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
561dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
562dbb799c3SYuval Mintz 	}
563dbb799c3SYuval Mintz 
564dbb799c3SYuval Mintz 	/* next the 'init' task memory (forced load memory) */
565dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
566dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
567dbb799c3SYuval Mintz 		if (!p_seg || p_seg->count == 0)
568dbb799c3SYuval Mintz 			continue;
569dbb799c3SYuval Mintz 
570dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)];
571dbb799c3SYuval Mintz 
572dbb799c3SYuval Mintz 		if (!p_seg->has_fl_mem) {
573dbb799c3SYuval Mintz 			/* The segment is active (total size pf 'working'
574dbb799c3SYuval Mintz 			 * memory is > 0) but has no FL (forced-load, Init)
575dbb799c3SYuval Mintz 			 * memory. Thus:
576dbb799c3SYuval Mintz 			 *
577dbb799c3SYuval Mintz 			 * 1.   The total-size in the corrsponding FL block of
578dbb799c3SYuval Mintz 			 *      the ILT client is set to 0 - No ILT line are
579dbb799c3SYuval Mintz 			 *      provisioned and no ILT memory allocated.
580dbb799c3SYuval Mintz 			 *
581dbb799c3SYuval Mintz 			 * 2.   The start-line of said block is set to the
582dbb799c3SYuval Mintz 			 *      start line of the matching working memory
583dbb799c3SYuval Mintz 			 *      block in the ILT client. This is later used to
584dbb799c3SYuval Mintz 			 *      configure the CDU segment offset registers and
585dbb799c3SYuval Mintz 			 *      results in an FL command for TIDs of this
586dbb799c3SYuval Mintz 			 *      segement behaves as regular load commands
587dbb799c3SYuval Mintz 			 *      (loading TIDs from the working memory).
588dbb799c3SYuval Mintz 			 */
589dbb799c3SYuval Mintz 			line = p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line;
590dbb799c3SYuval Mintz 
591dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
592dbb799c3SYuval Mintz 			continue;
593dbb799c3SYuval Mintz 		}
594dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
595dbb799c3SYuval Mintz 
596dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk,
597dbb799c3SYuval Mintz 				     curr_line, total,
598dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
599dbb799c3SYuval Mintz 
600dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
601dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
602dbb799c3SYuval Mintz 	}
603dbb799c3SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_cli->pf_blks[0].start_line;
604dbb799c3SYuval Mintz 
605dbb799c3SYuval Mintz 	/* CDUT VF */
606dbb799c3SYuval Mintz 	p_seg = qed_cxt_tid_seg_info(p_hwfn, TASK_SEGMENT_VF);
607dbb799c3SYuval Mintz 	if (p_seg && p_seg->count) {
608dbb799c3SYuval Mintz 		/* Stricly speaking we need to iterate over all VF
609dbb799c3SYuval Mintz 		 * task segment types, but a VF has only 1 segment
610dbb799c3SYuval Mintz 		 */
611dbb799c3SYuval Mintz 
612dbb799c3SYuval Mintz 		/* 'working' memory */
613dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
614dbb799c3SYuval Mintz 
615dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
616dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk,
617dbb799c3SYuval Mintz 				     curr_line, total,
618dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
619dbb799c3SYuval Mintz 
620dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
621dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
622dbb799c3SYuval Mintz 
623dbb799c3SYuval Mintz 		/* 'init' memory */
624dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
625dbb799c3SYuval Mintz 		if (!p_seg->has_fl_mem) {
626dbb799c3SYuval Mintz 			/* see comment above */
627dbb799c3SYuval Mintz 			line = p_cli->vf_blks[CDUT_SEG_BLK(0)].start_line;
628dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
629dbb799c3SYuval Mintz 		} else {
630dbb799c3SYuval Mintz 			task_size = p_mngr->task_type_size[p_seg->type];
631dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk,
632dbb799c3SYuval Mintz 					     curr_line, total, task_size);
633dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
634dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
635dbb799c3SYuval Mintz 		}
636dbb799c3SYuval Mintz 		p_cli->vf_total_lines = curr_line -
637dbb799c3SYuval Mintz 		    p_cli->vf_blks[0].start_line;
638dbb799c3SYuval Mintz 
639dbb799c3SYuval Mintz 		/* Now for the rest of the VFs */
640dbb799c3SYuval Mintz 		for (i = 1; i < p_mngr->vf_count; i++) {
641dbb799c3SYuval Mintz 			p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
642dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
643dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
644dbb799c3SYuval Mintz 
645dbb799c3SYuval Mintz 			p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
646dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
647dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
648dbb799c3SYuval Mintz 		}
649dbb799c3SYuval Mintz 	}
650dbb799c3SYuval Mintz 
651fe56b9e6SYuval Mintz 	/* QM */
652fe56b9e6SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_QM];
653fe56b9e6SYuval Mintz 	p_blk = &p_cli->pf_blks[0];
654fe56b9e6SYuval Mintz 
655fe56b9e6SYuval Mintz 	qed_cxt_qm_iids(p_hwfn, &qm_iids);
6561408cc1fSYuval Mintz 	total = qed_qm_pf_mem_size(p_hwfn->rel_pf_id, qm_iids.cids,
657dbb799c3SYuval Mintz 				   qm_iids.vf_cids, qm_iids.tids,
6581408cc1fSYuval Mintz 				   p_hwfn->qm_info.num_pqs,
6591408cc1fSYuval Mintz 				   p_hwfn->qm_info.num_vf_pqs);
660fe56b9e6SYuval Mintz 
6611408cc1fSYuval Mintz 	DP_VERBOSE(p_hwfn,
6621408cc1fSYuval Mintz 		   QED_MSG_ILT,
663dbb799c3SYuval Mintz 		   "QM ILT Info, (cids=%d, vf_cids=%d, tids=%d, num_pqs=%d, num_vf_pqs=%d, memory_size=%d)\n",
6641408cc1fSYuval Mintz 		   qm_iids.cids,
6651408cc1fSYuval Mintz 		   qm_iids.vf_cids,
666dbb799c3SYuval Mintz 		   qm_iids.tids,
6671408cc1fSYuval Mintz 		   p_hwfn->qm_info.num_pqs, p_hwfn->qm_info.num_vf_pqs, total);
668fe56b9e6SYuval Mintz 
669fe56b9e6SYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk,
670fe56b9e6SYuval Mintz 			     curr_line, total * 0x1000,
671fe56b9e6SYuval Mintz 			     QM_PQ_ELEMENT_SIZE);
672fe56b9e6SYuval Mintz 
673fe56b9e6SYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_QM);
674fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_blk->start_line;
675fe56b9e6SYuval Mintz 
676dbb799c3SYuval Mintz 	/* SRC */
677dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_SRC];
678dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
679dbb799c3SYuval Mintz 
680dbb799c3SYuval Mintz 	/* Both the PF and VFs searcher connections are stored in the per PF
681dbb799c3SYuval Mintz 	 * database. Thus sum the PF searcher cids and all the VFs searcher
682dbb799c3SYuval Mintz 	 * cids.
683dbb799c3SYuval Mintz 	 */
684dbb799c3SYuval Mintz 	total = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
685dbb799c3SYuval Mintz 	if (total) {
686dbb799c3SYuval Mintz 		u32 local_max = max_t(u32, total,
687dbb799c3SYuval Mintz 				      SRC_MIN_NUM_ELEMS);
688dbb799c3SYuval Mintz 
689dbb799c3SYuval Mintz 		total = roundup_pow_of_two(local_max);
690dbb799c3SYuval Mintz 
691dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[0];
692dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
693dbb799c3SYuval Mintz 				     total * sizeof(struct src_ent),
694dbb799c3SYuval Mintz 				     sizeof(struct src_ent));
695dbb799c3SYuval Mintz 
696dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
697dbb799c3SYuval Mintz 				     ILT_CLI_SRC);
698dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
699dbb799c3SYuval Mintz 	}
700dbb799c3SYuval Mintz 
701dbb799c3SYuval Mintz 	/* TM PF */
702dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_TM];
703dbb799c3SYuval Mintz 	qed_cxt_tm_iids(p_mngr, &tm_iids);
704dbb799c3SYuval Mintz 	total = tm_iids.pf_cids + tm_iids.pf_tids_total;
705dbb799c3SYuval Mintz 	if (total) {
706dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[0];
707dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
708dbb799c3SYuval Mintz 				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
709dbb799c3SYuval Mintz 
710dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
711dbb799c3SYuval Mintz 				     ILT_CLI_TM);
712dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
713dbb799c3SYuval Mintz 	}
714dbb799c3SYuval Mintz 
715dbb799c3SYuval Mintz 	/* TM VF */
716dbb799c3SYuval Mintz 	total = tm_iids.per_vf_cids + tm_iids.per_vf_tids;
717dbb799c3SYuval Mintz 	if (total) {
718dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[0];
719dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
720dbb799c3SYuval Mintz 				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
721dbb799c3SYuval Mintz 
722dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
723dbb799c3SYuval Mintz 				     ILT_CLI_TM);
724dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
725dbb799c3SYuval Mintz 
726dbb799c3SYuval Mintz 		for (i = 1; i < p_mngr->vf_count; i++)
727dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
728dbb799c3SYuval Mintz 					     ILT_CLI_TM);
729dbb799c3SYuval Mintz 	}
730dbb799c3SYuval Mintz 
731dbb799c3SYuval Mintz 	/* TSDM (SRQ CONTEXT) */
732dbb799c3SYuval Mintz 	total = qed_cxt_get_srq_count(p_hwfn);
733dbb799c3SYuval Mintz 
734dbb799c3SYuval Mintz 	if (total) {
735dbb799c3SYuval Mintz 		p_cli = &p_mngr->clients[ILT_CLI_TSDM];
736dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
737dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
738dbb799c3SYuval Mintz 				     total * SRQ_CXT_SIZE, SRQ_CXT_SIZE);
739dbb799c3SYuval Mintz 
740dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
741dbb799c3SYuval Mintz 				     ILT_CLI_TSDM);
742dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
743dbb799c3SYuval Mintz 	}
744dbb799c3SYuval Mintz 
745fe56b9e6SYuval Mintz 	if (curr_line - p_hwfn->p_cxt_mngr->pf_start_line >
746fe56b9e6SYuval Mintz 	    RESC_NUM(p_hwfn, QED_ILT)) {
747fe56b9e6SYuval Mintz 		DP_ERR(p_hwfn, "too many ilt lines...#lines=%d\n",
748fe56b9e6SYuval Mintz 		       curr_line - p_hwfn->p_cxt_mngr->pf_start_line);
749fe56b9e6SYuval Mintz 		return -EINVAL;
750fe56b9e6SYuval Mintz 	}
751fe56b9e6SYuval Mintz 
752fe56b9e6SYuval Mintz 	return 0;
753fe56b9e6SYuval Mintz }
754fe56b9e6SYuval Mintz 
755dbb799c3SYuval Mintz static void qed_cxt_src_t2_free(struct qed_hwfn *p_hwfn)
756dbb799c3SYuval Mintz {
757dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
758dbb799c3SYuval Mintz 	u32 i;
759dbb799c3SYuval Mintz 
760dbb799c3SYuval Mintz 	if (!p_mngr->t2)
761dbb799c3SYuval Mintz 		return;
762dbb799c3SYuval Mintz 
763dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++)
764dbb799c3SYuval Mintz 		if (p_mngr->t2[i].p_virt)
765dbb799c3SYuval Mintz 			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
766dbb799c3SYuval Mintz 					  p_mngr->t2[i].size,
767dbb799c3SYuval Mintz 					  p_mngr->t2[i].p_virt,
768dbb799c3SYuval Mintz 					  p_mngr->t2[i].p_phys);
769dbb799c3SYuval Mintz 
770dbb799c3SYuval Mintz 	kfree(p_mngr->t2);
771dbb799c3SYuval Mintz 	p_mngr->t2 = NULL;
772dbb799c3SYuval Mintz }
773dbb799c3SYuval Mintz 
774dbb799c3SYuval Mintz static int qed_cxt_src_t2_alloc(struct qed_hwfn *p_hwfn)
775dbb799c3SYuval Mintz {
776dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
777dbb799c3SYuval Mintz 	u32 conn_num, total_size, ent_per_page, psz, i;
778dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_src;
779dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
780dbb799c3SYuval Mintz 	struct qed_dma_mem *p_t2;
781dbb799c3SYuval Mintz 	int rc;
782dbb799c3SYuval Mintz 
783dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
784dbb799c3SYuval Mintz 
785dbb799c3SYuval Mintz 	/* if the SRC ILT client is inactive - there are no connection
786dbb799c3SYuval Mintz 	 * requiring the searcer, leave.
787dbb799c3SYuval Mintz 	 */
788dbb799c3SYuval Mintz 	p_src = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_SRC];
789dbb799c3SYuval Mintz 	if (!p_src->active)
790dbb799c3SYuval Mintz 		return 0;
791dbb799c3SYuval Mintz 
792dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
793dbb799c3SYuval Mintz 	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
794dbb799c3SYuval Mintz 	total_size = conn_num * sizeof(struct src_ent);
795dbb799c3SYuval Mintz 
796dbb799c3SYuval Mintz 	/* use the same page size as the SRC ILT client */
797dbb799c3SYuval Mintz 	psz = ILT_PAGE_IN_BYTES(p_src->p_size.val);
798dbb799c3SYuval Mintz 	p_mngr->t2_num_pages = DIV_ROUND_UP(total_size, psz);
799dbb799c3SYuval Mintz 
800dbb799c3SYuval Mintz 	/* allocate t2 */
8012591c280SJoe Perches 	p_mngr->t2 = kcalloc(p_mngr->t2_num_pages, sizeof(struct qed_dma_mem),
802dbb799c3SYuval Mintz 			     GFP_KERNEL);
803dbb799c3SYuval Mintz 	if (!p_mngr->t2) {
804dbb799c3SYuval Mintz 		rc = -ENOMEM;
805dbb799c3SYuval Mintz 		goto t2_fail;
806dbb799c3SYuval Mintz 	}
807dbb799c3SYuval Mintz 
808dbb799c3SYuval Mintz 	/* allocate t2 pages */
809dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++) {
810dbb799c3SYuval Mintz 		u32 size = min_t(u32, total_size, psz);
811dbb799c3SYuval Mintz 		void **p_virt = &p_mngr->t2[i].p_virt;
812dbb799c3SYuval Mintz 
813dbb799c3SYuval Mintz 		*p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
814dbb799c3SYuval Mintz 					     size,
815dbb799c3SYuval Mintz 					     &p_mngr->t2[i].p_phys, GFP_KERNEL);
816dbb799c3SYuval Mintz 		if (!p_mngr->t2[i].p_virt) {
817dbb799c3SYuval Mintz 			rc = -ENOMEM;
818dbb799c3SYuval Mintz 			goto t2_fail;
819dbb799c3SYuval Mintz 		}
820dbb799c3SYuval Mintz 		memset(*p_virt, 0, size);
821dbb799c3SYuval Mintz 		p_mngr->t2[i].size = size;
822dbb799c3SYuval Mintz 		total_size -= size;
823dbb799c3SYuval Mintz 	}
824dbb799c3SYuval Mintz 
825dbb799c3SYuval Mintz 	/* Set the t2 pointers */
826dbb799c3SYuval Mintz 
827dbb799c3SYuval Mintz 	/* entries per page - must be a power of two */
828dbb799c3SYuval Mintz 	ent_per_page = psz / sizeof(struct src_ent);
829dbb799c3SYuval Mintz 
830dbb799c3SYuval Mintz 	p_mngr->first_free = (u64) p_mngr->t2[0].p_phys;
831dbb799c3SYuval Mintz 
832dbb799c3SYuval Mintz 	p_t2 = &p_mngr->t2[(conn_num - 1) / ent_per_page];
833dbb799c3SYuval Mintz 	p_mngr->last_free = (u64) p_t2->p_phys +
834dbb799c3SYuval Mintz 	    ((conn_num - 1) & (ent_per_page - 1)) * sizeof(struct src_ent);
835dbb799c3SYuval Mintz 
836dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++) {
837dbb799c3SYuval Mintz 		u32 ent_num = min_t(u32,
838dbb799c3SYuval Mintz 				    ent_per_page,
839dbb799c3SYuval Mintz 				    conn_num);
840dbb799c3SYuval Mintz 		struct src_ent *entries = p_mngr->t2[i].p_virt;
841dbb799c3SYuval Mintz 		u64 p_ent_phys = (u64) p_mngr->t2[i].p_phys, val;
842dbb799c3SYuval Mintz 		u32 j;
843dbb799c3SYuval Mintz 
844dbb799c3SYuval Mintz 		for (j = 0; j < ent_num - 1; j++) {
845dbb799c3SYuval Mintz 			val = p_ent_phys + (j + 1) * sizeof(struct src_ent);
846dbb799c3SYuval Mintz 			entries[j].next = cpu_to_be64(val);
847dbb799c3SYuval Mintz 		}
848dbb799c3SYuval Mintz 
849dbb799c3SYuval Mintz 		if (i < p_mngr->t2_num_pages - 1)
850dbb799c3SYuval Mintz 			val = (u64) p_mngr->t2[i + 1].p_phys;
851dbb799c3SYuval Mintz 		else
852dbb799c3SYuval Mintz 			val = 0;
853dbb799c3SYuval Mintz 		entries[j].next = cpu_to_be64(val);
854dbb799c3SYuval Mintz 
85501e517f1SDan Carpenter 		conn_num -= ent_num;
856dbb799c3SYuval Mintz 	}
857dbb799c3SYuval Mintz 
858dbb799c3SYuval Mintz 	return 0;
859dbb799c3SYuval Mintz 
860dbb799c3SYuval Mintz t2_fail:
861dbb799c3SYuval Mintz 	qed_cxt_src_t2_free(p_hwfn);
862dbb799c3SYuval Mintz 	return rc;
863dbb799c3SYuval Mintz }
864dbb799c3SYuval Mintz 
865fe56b9e6SYuval Mintz #define for_each_ilt_valid_client(pos, clients)	\
866dbb799c3SYuval Mintz 	for (pos = 0; pos < ILT_CLI_MAX; pos++)	\
867dbb799c3SYuval Mintz 		if (!clients[pos].active) {	\
868dbb799c3SYuval Mintz 			continue;		\
869dbb799c3SYuval Mintz 		} else				\
870fe56b9e6SYuval Mintz 
871fe56b9e6SYuval Mintz /* Total number of ILT lines used by this PF */
872fe56b9e6SYuval Mintz static u32 qed_cxt_ilt_shadow_size(struct qed_ilt_client_cfg *ilt_clients)
873fe56b9e6SYuval Mintz {
874fe56b9e6SYuval Mintz 	u32 size = 0;
875fe56b9e6SYuval Mintz 	u32 i;
876fe56b9e6SYuval Mintz 
877dbb799c3SYuval Mintz 	for_each_ilt_valid_client(i, ilt_clients)
878dbb799c3SYuval Mintz 	    size += (ilt_clients[i].last.val - ilt_clients[i].first.val + 1);
879fe56b9e6SYuval Mintz 
880fe56b9e6SYuval Mintz 	return size;
881fe56b9e6SYuval Mintz }
882fe56b9e6SYuval Mintz 
883fe56b9e6SYuval Mintz static void qed_ilt_shadow_free(struct qed_hwfn *p_hwfn)
884fe56b9e6SYuval Mintz {
885fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *p_cli = p_hwfn->p_cxt_mngr->clients;
886fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
887fe56b9e6SYuval Mintz 	u32 ilt_size, i;
888fe56b9e6SYuval Mintz 
889fe56b9e6SYuval Mintz 	ilt_size = qed_cxt_ilt_shadow_size(p_cli);
890fe56b9e6SYuval Mintz 
891fe56b9e6SYuval Mintz 	for (i = 0; p_mngr->ilt_shadow && i < ilt_size; i++) {
892fe56b9e6SYuval Mintz 		struct qed_dma_mem *p_dma = &p_mngr->ilt_shadow[i];
893fe56b9e6SYuval Mintz 
894fe56b9e6SYuval Mintz 		if (p_dma->p_virt)
895fe56b9e6SYuval Mintz 			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
896fe56b9e6SYuval Mintz 					  p_dma->size, p_dma->p_virt,
897fe56b9e6SYuval Mintz 					  p_dma->p_phys);
898fe56b9e6SYuval Mintz 		p_dma->p_virt = NULL;
899fe56b9e6SYuval Mintz 	}
900fe56b9e6SYuval Mintz 	kfree(p_mngr->ilt_shadow);
901fe56b9e6SYuval Mintz }
902fe56b9e6SYuval Mintz 
903fe56b9e6SYuval Mintz static int qed_ilt_blk_alloc(struct qed_hwfn *p_hwfn,
904fe56b9e6SYuval Mintz 			     struct qed_ilt_cli_blk *p_blk,
905fe56b9e6SYuval Mintz 			     enum ilt_clients ilt_client,
906fe56b9e6SYuval Mintz 			     u32 start_line_offset)
907fe56b9e6SYuval Mintz {
908fe56b9e6SYuval Mintz 	struct qed_dma_mem *ilt_shadow = p_hwfn->p_cxt_mngr->ilt_shadow;
909dbb799c3SYuval Mintz 	u32 lines, line, sz_left, lines_to_skip = 0;
910dbb799c3SYuval Mintz 
911dbb799c3SYuval Mintz 	/* Special handling for RoCE that supports dynamic allocation */
912dbb799c3SYuval Mintz 	if ((p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) &&
913dbb799c3SYuval Mintz 	    ((ilt_client == ILT_CLI_CDUT) || ilt_client == ILT_CLI_TSDM))
914dbb799c3SYuval Mintz 		return 0;
915dbb799c3SYuval Mintz 
916dbb799c3SYuval Mintz 	lines_to_skip = p_blk->dynamic_line_cnt;
917fe56b9e6SYuval Mintz 
918fe56b9e6SYuval Mintz 	if (!p_blk->total_size)
919fe56b9e6SYuval Mintz 		return 0;
920fe56b9e6SYuval Mintz 
921fe56b9e6SYuval Mintz 	sz_left = p_blk->total_size;
922dbb799c3SYuval Mintz 	lines = DIV_ROUND_UP(sz_left, p_blk->real_size_in_page) - lines_to_skip;
923fe56b9e6SYuval Mintz 	line = p_blk->start_line + start_line_offset -
924dbb799c3SYuval Mintz 	    p_hwfn->p_cxt_mngr->pf_start_line + lines_to_skip;
925fe56b9e6SYuval Mintz 
926fe56b9e6SYuval Mintz 	for (; lines; lines--) {
927fe56b9e6SYuval Mintz 		dma_addr_t p_phys;
928fe56b9e6SYuval Mintz 		void *p_virt;
929fe56b9e6SYuval Mintz 		u32 size;
930fe56b9e6SYuval Mintz 
9311a635e48SYuval Mintz 		size = min_t(u32, sz_left, p_blk->real_size_in_page);
932fe56b9e6SYuval Mintz 		p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
9331a635e48SYuval Mintz 					    size, &p_phys, GFP_KERNEL);
934fe56b9e6SYuval Mintz 		if (!p_virt)
935fe56b9e6SYuval Mintz 			return -ENOMEM;
936fe56b9e6SYuval Mintz 		memset(p_virt, 0, size);
937fe56b9e6SYuval Mintz 
938fe56b9e6SYuval Mintz 		ilt_shadow[line].p_phys = p_phys;
939fe56b9e6SYuval Mintz 		ilt_shadow[line].p_virt = p_virt;
940fe56b9e6SYuval Mintz 		ilt_shadow[line].size = size;
941fe56b9e6SYuval Mintz 
942fe56b9e6SYuval Mintz 		DP_VERBOSE(p_hwfn, QED_MSG_ILT,
943fe56b9e6SYuval Mintz 			   "ILT shadow: Line [%d] Physical 0x%llx Virtual %p Size %d\n",
944fe56b9e6SYuval Mintz 			    line, (u64)p_phys, p_virt, size);
945fe56b9e6SYuval Mintz 
946fe56b9e6SYuval Mintz 		sz_left -= size;
947fe56b9e6SYuval Mintz 		line++;
948fe56b9e6SYuval Mintz 	}
949fe56b9e6SYuval Mintz 
950fe56b9e6SYuval Mintz 	return 0;
951fe56b9e6SYuval Mintz }
952fe56b9e6SYuval Mintz 
953fe56b9e6SYuval Mintz static int qed_ilt_shadow_alloc(struct qed_hwfn *p_hwfn)
954fe56b9e6SYuval Mintz {
955fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
956fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *clients = p_mngr->clients;
957fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
9581408cc1fSYuval Mintz 	u32 size, i, j, k;
959fe56b9e6SYuval Mintz 	int rc;
960fe56b9e6SYuval Mintz 
961fe56b9e6SYuval Mintz 	size = qed_cxt_ilt_shadow_size(clients);
962fe56b9e6SYuval Mintz 	p_mngr->ilt_shadow = kcalloc(size, sizeof(struct qed_dma_mem),
963fe56b9e6SYuval Mintz 				     GFP_KERNEL);
964fe56b9e6SYuval Mintz 	if (!p_mngr->ilt_shadow) {
965fe56b9e6SYuval Mintz 		rc = -ENOMEM;
966fe56b9e6SYuval Mintz 		goto ilt_shadow_fail;
967fe56b9e6SYuval Mintz 	}
968fe56b9e6SYuval Mintz 
969fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
970fe56b9e6SYuval Mintz 		   "Allocated 0x%x bytes for ilt shadow\n",
971fe56b9e6SYuval Mintz 		   (u32)(size * sizeof(struct qed_dma_mem)));
972fe56b9e6SYuval Mintz 
973fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, clients) {
974fe56b9e6SYuval Mintz 		for (j = 0; j < ILT_CLI_PF_BLOCKS; j++) {
975fe56b9e6SYuval Mintz 			p_blk = &clients[i].pf_blks[j];
976fe56b9e6SYuval Mintz 			rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, 0);
9771a635e48SYuval Mintz 			if (rc)
978fe56b9e6SYuval Mintz 				goto ilt_shadow_fail;
979fe56b9e6SYuval Mintz 		}
9801408cc1fSYuval Mintz 		for (k = 0; k < p_mngr->vf_count; k++) {
9811408cc1fSYuval Mintz 			for (j = 0; j < ILT_CLI_VF_BLOCKS; j++) {
9821408cc1fSYuval Mintz 				u32 lines = clients[i].vf_total_lines * k;
9831408cc1fSYuval Mintz 
9841408cc1fSYuval Mintz 				p_blk = &clients[i].vf_blks[j];
9851408cc1fSYuval Mintz 				rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, lines);
9861a635e48SYuval Mintz 				if (rc)
9871408cc1fSYuval Mintz 					goto ilt_shadow_fail;
9881408cc1fSYuval Mintz 			}
9891408cc1fSYuval Mintz 		}
990fe56b9e6SYuval Mintz 	}
991fe56b9e6SYuval Mintz 
992fe56b9e6SYuval Mintz 	return 0;
993fe56b9e6SYuval Mintz 
994fe56b9e6SYuval Mintz ilt_shadow_fail:
995fe56b9e6SYuval Mintz 	qed_ilt_shadow_free(p_hwfn);
996fe56b9e6SYuval Mintz 	return rc;
997fe56b9e6SYuval Mintz }
998fe56b9e6SYuval Mintz 
999fe56b9e6SYuval Mintz static void qed_cid_map_free(struct qed_hwfn *p_hwfn)
1000fe56b9e6SYuval Mintz {
1001fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1002fe56b9e6SYuval Mintz 	u32 type;
1003fe56b9e6SYuval Mintz 
1004fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1005fe56b9e6SYuval Mintz 		kfree(p_mngr->acquired[type].cid_map);
1006fe56b9e6SYuval Mintz 		p_mngr->acquired[type].max_count = 0;
1007fe56b9e6SYuval Mintz 		p_mngr->acquired[type].start_cid = 0;
1008fe56b9e6SYuval Mintz 	}
1009fe56b9e6SYuval Mintz }
1010fe56b9e6SYuval Mintz 
1011fe56b9e6SYuval Mintz static int qed_cid_map_alloc(struct qed_hwfn *p_hwfn)
1012fe56b9e6SYuval Mintz {
1013fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1014fe56b9e6SYuval Mintz 	u32 start_cid = 0;
1015fe56b9e6SYuval Mintz 	u32 type;
1016fe56b9e6SYuval Mintz 
1017fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1018fe56b9e6SYuval Mintz 		u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1019fe56b9e6SYuval Mintz 		u32 size;
1020fe56b9e6SYuval Mintz 
1021fe56b9e6SYuval Mintz 		if (cid_cnt == 0)
1022fe56b9e6SYuval Mintz 			continue;
1023fe56b9e6SYuval Mintz 
1024fe56b9e6SYuval Mintz 		size = DIV_ROUND_UP(cid_cnt,
1025fe56b9e6SYuval Mintz 				    sizeof(unsigned long) * BITS_PER_BYTE) *
1026fe56b9e6SYuval Mintz 		       sizeof(unsigned long);
1027fe56b9e6SYuval Mintz 		p_mngr->acquired[type].cid_map = kzalloc(size, GFP_KERNEL);
1028fe56b9e6SYuval Mintz 		if (!p_mngr->acquired[type].cid_map)
1029fe56b9e6SYuval Mintz 			goto cid_map_fail;
1030fe56b9e6SYuval Mintz 
1031fe56b9e6SYuval Mintz 		p_mngr->acquired[type].max_count = cid_cnt;
1032fe56b9e6SYuval Mintz 		p_mngr->acquired[type].start_cid = start_cid;
1033fe56b9e6SYuval Mintz 
1034fe56b9e6SYuval Mintz 		p_hwfn->p_cxt_mngr->conn_cfg[type].cid_start = start_cid;
1035fe56b9e6SYuval Mintz 
1036fe56b9e6SYuval Mintz 		DP_VERBOSE(p_hwfn, QED_MSG_CXT,
1037fe56b9e6SYuval Mintz 			   "Type %08x start: %08x count %08x\n",
1038fe56b9e6SYuval Mintz 			   type, p_mngr->acquired[type].start_cid,
1039fe56b9e6SYuval Mintz 			   p_mngr->acquired[type].max_count);
1040fe56b9e6SYuval Mintz 		start_cid += cid_cnt;
1041fe56b9e6SYuval Mintz 	}
1042fe56b9e6SYuval Mintz 
1043fe56b9e6SYuval Mintz 	return 0;
1044fe56b9e6SYuval Mintz 
1045fe56b9e6SYuval Mintz cid_map_fail:
1046fe56b9e6SYuval Mintz 	qed_cid_map_free(p_hwfn);
1047fe56b9e6SYuval Mintz 	return -ENOMEM;
1048fe56b9e6SYuval Mintz }
1049fe56b9e6SYuval Mintz 
1050fe56b9e6SYuval Mintz int qed_cxt_mngr_alloc(struct qed_hwfn *p_hwfn)
1051fe56b9e6SYuval Mintz {
1052dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *clients;
1053fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr;
1054fe56b9e6SYuval Mintz 	u32 i;
1055fe56b9e6SYuval Mintz 
105660fffb3bSYuval Mintz 	p_mngr = kzalloc(sizeof(*p_mngr), GFP_KERNEL);
10572591c280SJoe Perches 	if (!p_mngr)
1058fe56b9e6SYuval Mintz 		return -ENOMEM;
1059fe56b9e6SYuval Mintz 
1060fe56b9e6SYuval Mintz 	/* Initialize ILT client registers */
1061dbb799c3SYuval Mintz 	clients = p_mngr->clients;
1062dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].first.reg = ILT_CFG_REG(CDUC, FIRST_ILT);
1063dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].last.reg = ILT_CFG_REG(CDUC, LAST_ILT);
1064dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].p_size.reg = ILT_CFG_REG(CDUC, P_SIZE);
1065fe56b9e6SYuval Mintz 
1066dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].first.reg = ILT_CFG_REG(QM, FIRST_ILT);
1067dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].last.reg = ILT_CFG_REG(QM, LAST_ILT);
1068dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].p_size.reg = ILT_CFG_REG(QM, P_SIZE);
1069fe56b9e6SYuval Mintz 
1070dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].first.reg = ILT_CFG_REG(TM, FIRST_ILT);
1071dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].last.reg = ILT_CFG_REG(TM, LAST_ILT);
1072dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].p_size.reg = ILT_CFG_REG(TM, P_SIZE);
1073dbb799c3SYuval Mintz 
1074dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].first.reg = ILT_CFG_REG(SRC, FIRST_ILT);
1075dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].last.reg = ILT_CFG_REG(SRC, LAST_ILT);
1076dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].p_size.reg = ILT_CFG_REG(SRC, P_SIZE);
1077dbb799c3SYuval Mintz 
1078dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].first.reg = ILT_CFG_REG(CDUT, FIRST_ILT);
1079dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].last.reg = ILT_CFG_REG(CDUT, LAST_ILT);
1080dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].p_size.reg = ILT_CFG_REG(CDUT, P_SIZE);
1081dbb799c3SYuval Mintz 
1082dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].first.reg = ILT_CFG_REG(TSDM, FIRST_ILT);
1083dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].last.reg = ILT_CFG_REG(TSDM, LAST_ILT);
1084dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].p_size.reg = ILT_CFG_REG(TSDM, P_SIZE);
1085fe56b9e6SYuval Mintz 	/* default ILT page size for all clients is 32K */
1086fe56b9e6SYuval Mintz 	for (i = 0; i < ILT_CLI_MAX; i++)
1087fe56b9e6SYuval Mintz 		p_mngr->clients[i].p_size.val = ILT_DEFAULT_HW_P_SIZE;
1088fe56b9e6SYuval Mintz 
1089dbb799c3SYuval Mintz 	/* Initialize task sizes */
1090dbb799c3SYuval Mintz 	p_mngr->task_type_size[0] = TYPE0_TASK_CXT_SIZE(p_hwfn);
1091dbb799c3SYuval Mintz 	p_mngr->task_type_size[1] = TYPE1_TASK_CXT_SIZE(p_hwfn);
1092dbb799c3SYuval Mintz 
10931408cc1fSYuval Mintz 	if (p_hwfn->cdev->p_iov_info)
10941408cc1fSYuval Mintz 		p_mngr->vf_count = p_hwfn->cdev->p_iov_info->total_vfs;
1095dbb799c3SYuval Mintz 	/* Initialize the dynamic ILT allocation mutex */
1096dbb799c3SYuval Mintz 	mutex_init(&p_mngr->mutex);
10971408cc1fSYuval Mintz 
1098fe56b9e6SYuval Mintz 	/* Set the cxt mangr pointer priori to further allocations */
1099fe56b9e6SYuval Mintz 	p_hwfn->p_cxt_mngr = p_mngr;
1100fe56b9e6SYuval Mintz 
1101fe56b9e6SYuval Mintz 	return 0;
1102fe56b9e6SYuval Mintz }
1103fe56b9e6SYuval Mintz 
1104fe56b9e6SYuval Mintz int qed_cxt_tables_alloc(struct qed_hwfn *p_hwfn)
1105fe56b9e6SYuval Mintz {
1106fe56b9e6SYuval Mintz 	int rc;
1107fe56b9e6SYuval Mintz 
1108fe56b9e6SYuval Mintz 	/* Allocate the ILT shadow table */
1109fe56b9e6SYuval Mintz 	rc = qed_ilt_shadow_alloc(p_hwfn);
11102591c280SJoe Perches 	if (rc)
1111fe56b9e6SYuval Mintz 		goto tables_alloc_fail;
1112fe56b9e6SYuval Mintz 
1113dbb799c3SYuval Mintz 	/* Allocate the T2  table */
1114dbb799c3SYuval Mintz 	rc = qed_cxt_src_t2_alloc(p_hwfn);
11152591c280SJoe Perches 	if (rc)
1116dbb799c3SYuval Mintz 		goto tables_alloc_fail;
1117dbb799c3SYuval Mintz 
1118fe56b9e6SYuval Mintz 	/* Allocate and initialize the acquired cids bitmaps */
1119fe56b9e6SYuval Mintz 	rc = qed_cid_map_alloc(p_hwfn);
11202591c280SJoe Perches 	if (rc)
1121fe56b9e6SYuval Mintz 		goto tables_alloc_fail;
1122fe56b9e6SYuval Mintz 
1123fe56b9e6SYuval Mintz 	return 0;
1124fe56b9e6SYuval Mintz 
1125fe56b9e6SYuval Mintz tables_alloc_fail:
1126fe56b9e6SYuval Mintz 	qed_cxt_mngr_free(p_hwfn);
1127fe56b9e6SYuval Mintz 	return rc;
1128fe56b9e6SYuval Mintz }
1129fe56b9e6SYuval Mintz 
1130fe56b9e6SYuval Mintz void qed_cxt_mngr_free(struct qed_hwfn *p_hwfn)
1131fe56b9e6SYuval Mintz {
1132fe56b9e6SYuval Mintz 	if (!p_hwfn->p_cxt_mngr)
1133fe56b9e6SYuval Mintz 		return;
1134fe56b9e6SYuval Mintz 
1135fe56b9e6SYuval Mintz 	qed_cid_map_free(p_hwfn);
1136dbb799c3SYuval Mintz 	qed_cxt_src_t2_free(p_hwfn);
1137fe56b9e6SYuval Mintz 	qed_ilt_shadow_free(p_hwfn);
1138fe56b9e6SYuval Mintz 	kfree(p_hwfn->p_cxt_mngr);
1139fe56b9e6SYuval Mintz 
1140fe56b9e6SYuval Mintz 	p_hwfn->p_cxt_mngr = NULL;
1141fe56b9e6SYuval Mintz }
1142fe56b9e6SYuval Mintz 
1143fe56b9e6SYuval Mintz void qed_cxt_mngr_setup(struct qed_hwfn *p_hwfn)
1144fe56b9e6SYuval Mintz {
1145fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1146fe56b9e6SYuval Mintz 	int type;
1147fe56b9e6SYuval Mintz 
1148fe56b9e6SYuval Mintz 	/* Reset acquired cids */
1149fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1150fe56b9e6SYuval Mintz 		u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1151fe56b9e6SYuval Mintz 
1152fe56b9e6SYuval Mintz 		if (cid_cnt == 0)
1153fe56b9e6SYuval Mintz 			continue;
1154fe56b9e6SYuval Mintz 
1155fe56b9e6SYuval Mintz 		memset(p_mngr->acquired[type].cid_map, 0,
1156fe56b9e6SYuval Mintz 		       DIV_ROUND_UP(cid_cnt,
1157fe56b9e6SYuval Mintz 				    sizeof(unsigned long) * BITS_PER_BYTE) *
1158fe56b9e6SYuval Mintz 		       sizeof(unsigned long));
1159fe56b9e6SYuval Mintz 	}
1160fe56b9e6SYuval Mintz }
1161fe56b9e6SYuval Mintz 
1162fe56b9e6SYuval Mintz /* CDU Common */
1163fe56b9e6SYuval Mintz #define CDUC_CXT_SIZE_SHIFT \
1164fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE_SHIFT
1165fe56b9e6SYuval Mintz 
1166fe56b9e6SYuval Mintz #define CDUC_CXT_SIZE_MASK \
1167fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE >> CDUC_CXT_SIZE_SHIFT)
1168fe56b9e6SYuval Mintz 
1169fe56b9e6SYuval Mintz #define CDUC_BLOCK_WASTE_SHIFT \
1170fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE_SHIFT
1171fe56b9e6SYuval Mintz 
1172fe56b9e6SYuval Mintz #define CDUC_BLOCK_WASTE_MASK \
1173fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE >> CDUC_BLOCK_WASTE_SHIFT)
1174fe56b9e6SYuval Mintz 
1175fe56b9e6SYuval Mintz #define CDUC_NCIB_SHIFT	\
1176fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_NCIB_SHIFT
1177fe56b9e6SYuval Mintz 
1178fe56b9e6SYuval Mintz #define CDUC_NCIB_MASK \
1179fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_NCIB >> CDUC_NCIB_SHIFT)
1180fe56b9e6SYuval Mintz 
1181dbb799c3SYuval Mintz #define CDUT_TYPE0_CXT_SIZE_SHIFT \
1182dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE_SHIFT
1183dbb799c3SYuval Mintz 
1184dbb799c3SYuval Mintz #define CDUT_TYPE0_CXT_SIZE_MASK		\
1185dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE >>	\
1186dbb799c3SYuval Mintz 	 CDUT_TYPE0_CXT_SIZE_SHIFT)
1187dbb799c3SYuval Mintz 
1188dbb799c3SYuval Mintz #define CDUT_TYPE0_BLOCK_WASTE_SHIFT \
1189dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE_SHIFT
1190dbb799c3SYuval Mintz 
1191dbb799c3SYuval Mintz #define CDUT_TYPE0_BLOCK_WASTE_MASK		       \
1192dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE >> \
1193dbb799c3SYuval Mintz 	 CDUT_TYPE0_BLOCK_WASTE_SHIFT)
1194dbb799c3SYuval Mintz 
1195dbb799c3SYuval Mintz #define CDUT_TYPE0_NCIB_SHIFT \
1196dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK_SHIFT
1197dbb799c3SYuval Mintz 
1198dbb799c3SYuval Mintz #define CDUT_TYPE0_NCIB_MASK				 \
1199dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK >> \
1200dbb799c3SYuval Mintz 	 CDUT_TYPE0_NCIB_SHIFT)
1201dbb799c3SYuval Mintz 
1202dbb799c3SYuval Mintz #define CDUT_TYPE1_CXT_SIZE_SHIFT \
1203dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE_SHIFT
1204dbb799c3SYuval Mintz 
1205dbb799c3SYuval Mintz #define CDUT_TYPE1_CXT_SIZE_MASK		\
1206dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE >>	\
1207dbb799c3SYuval Mintz 	 CDUT_TYPE1_CXT_SIZE_SHIFT)
1208dbb799c3SYuval Mintz 
1209dbb799c3SYuval Mintz #define CDUT_TYPE1_BLOCK_WASTE_SHIFT \
1210dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE_SHIFT
1211dbb799c3SYuval Mintz 
1212dbb799c3SYuval Mintz #define CDUT_TYPE1_BLOCK_WASTE_MASK		       \
1213dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE >> \
1214dbb799c3SYuval Mintz 	 CDUT_TYPE1_BLOCK_WASTE_SHIFT)
1215dbb799c3SYuval Mintz 
1216dbb799c3SYuval Mintz #define CDUT_TYPE1_NCIB_SHIFT \
1217dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK_SHIFT
1218dbb799c3SYuval Mintz 
1219dbb799c3SYuval Mintz #define CDUT_TYPE1_NCIB_MASK				 \
1220dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK >> \
1221dbb799c3SYuval Mintz 	 CDUT_TYPE1_NCIB_SHIFT)
1222dbb799c3SYuval Mintz 
1223fe56b9e6SYuval Mintz static void qed_cdu_init_common(struct qed_hwfn *p_hwfn)
1224fe56b9e6SYuval Mintz {
1225fe56b9e6SYuval Mintz 	u32 page_sz, elems_per_page, block_waste, cxt_size, cdu_params = 0;
1226fe56b9e6SYuval Mintz 
1227fe56b9e6SYuval Mintz 	/* CDUC - connection configuration */
1228fe56b9e6SYuval Mintz 	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1229fe56b9e6SYuval Mintz 	cxt_size = CONN_CXT_SIZE(p_hwfn);
1230fe56b9e6SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1231fe56b9e6SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1232fe56b9e6SYuval Mintz 
1233fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_CXT_SIZE, cxt_size);
1234fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_BLOCK_WASTE, block_waste);
1235fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_NCIB, elems_per_page);
1236fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_CID_ADDR_PARAMS_RT_OFFSET, cdu_params);
1237dbb799c3SYuval Mintz 
1238dbb799c3SYuval Mintz 	/* CDUT - type-0 tasks configuration */
1239dbb799c3SYuval Mintz 	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT].p_size.val;
1240dbb799c3SYuval Mintz 	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[0];
1241dbb799c3SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1242dbb799c3SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1243dbb799c3SYuval Mintz 
1244dbb799c3SYuval Mintz 	/* cxt size and block-waste are multipes of 8 */
1245dbb799c3SYuval Mintz 	cdu_params = 0;
1246dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_CXT_SIZE, (cxt_size >> 3));
1247dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_BLOCK_WASTE, (block_waste >> 3));
1248dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_NCIB, elems_per_page);
1249dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT0_PARAMS_RT_OFFSET, cdu_params);
1250dbb799c3SYuval Mintz 
1251dbb799c3SYuval Mintz 	/* CDUT - type-1 tasks configuration */
1252dbb799c3SYuval Mintz 	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[1];
1253dbb799c3SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1254dbb799c3SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1255dbb799c3SYuval Mintz 
1256dbb799c3SYuval Mintz 	/* cxt size and block-waste are multipes of 8 */
1257dbb799c3SYuval Mintz 	cdu_params = 0;
1258dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_CXT_SIZE, (cxt_size >> 3));
1259dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_BLOCK_WASTE, (block_waste >> 3));
1260dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_NCIB, elems_per_page);
1261dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT1_PARAMS_RT_OFFSET, cdu_params);
1262dbb799c3SYuval Mintz }
1263dbb799c3SYuval Mintz 
1264dbb799c3SYuval Mintz /* CDU PF */
1265dbb799c3SYuval Mintz #define CDU_SEG_REG_TYPE_SHIFT          CDU_SEG_TYPE_OFFSET_REG_TYPE_SHIFT
1266dbb799c3SYuval Mintz #define CDU_SEG_REG_TYPE_MASK           0x1
1267dbb799c3SYuval Mintz #define CDU_SEG_REG_OFFSET_SHIFT        0
1268dbb799c3SYuval Mintz #define CDU_SEG_REG_OFFSET_MASK         CDU_SEG_TYPE_OFFSET_REG_OFFSET_MASK
1269dbb799c3SYuval Mintz 
1270dbb799c3SYuval Mintz static void qed_cdu_init_pf(struct qed_hwfn *p_hwfn)
1271dbb799c3SYuval Mintz {
1272dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
1273dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg;
1274dbb799c3SYuval Mintz 	u32 cdu_seg_params, offset;
1275dbb799c3SYuval Mintz 	int i;
1276dbb799c3SYuval Mintz 
1277dbb799c3SYuval Mintz 	static const u32 rt_type_offset_arr[] = {
1278dbb799c3SYuval Mintz 		CDU_REG_PF_SEG0_TYPE_OFFSET_RT_OFFSET,
1279dbb799c3SYuval Mintz 		CDU_REG_PF_SEG1_TYPE_OFFSET_RT_OFFSET,
1280dbb799c3SYuval Mintz 		CDU_REG_PF_SEG2_TYPE_OFFSET_RT_OFFSET,
1281dbb799c3SYuval Mintz 		CDU_REG_PF_SEG3_TYPE_OFFSET_RT_OFFSET
1282dbb799c3SYuval Mintz 	};
1283dbb799c3SYuval Mintz 
1284dbb799c3SYuval Mintz 	static const u32 rt_type_offset_fl_arr[] = {
1285dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG0_TYPE_OFFSET_RT_OFFSET,
1286dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG1_TYPE_OFFSET_RT_OFFSET,
1287dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG2_TYPE_OFFSET_RT_OFFSET,
1288dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG3_TYPE_OFFSET_RT_OFFSET
1289dbb799c3SYuval Mintz 	};
1290dbb799c3SYuval Mintz 
1291dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1292dbb799c3SYuval Mintz 
1293dbb799c3SYuval Mintz 	/* There are initializations only for CDUT during pf Phase */
1294dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1295dbb799c3SYuval Mintz 		/* Segment 0 */
1296dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
1297dbb799c3SYuval Mintz 		if (!p_seg)
1298dbb799c3SYuval Mintz 			continue;
1299dbb799c3SYuval Mintz 
1300dbb799c3SYuval Mintz 		/* Note: start_line is already adjusted for the CDU
1301dbb799c3SYuval Mintz 		 * segment register granularity, so we just need to
1302dbb799c3SYuval Mintz 		 * divide. Adjustment is implicit as we assume ILT
1303dbb799c3SYuval Mintz 		 * Page size is larger than 32K!
1304dbb799c3SYuval Mintz 		 */
1305dbb799c3SYuval Mintz 		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1306dbb799c3SYuval Mintz 			  (p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line -
1307dbb799c3SYuval Mintz 			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1308dbb799c3SYuval Mintz 
1309dbb799c3SYuval Mintz 		cdu_seg_params = 0;
1310dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1311dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1312dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn, rt_type_offset_arr[i], cdu_seg_params);
1313dbb799c3SYuval Mintz 
1314dbb799c3SYuval Mintz 		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1315dbb799c3SYuval Mintz 			  (p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)].start_line -
1316dbb799c3SYuval Mintz 			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1317dbb799c3SYuval Mintz 
1318dbb799c3SYuval Mintz 		cdu_seg_params = 0;
1319dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1320dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1321dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn, rt_type_offset_fl_arr[i], cdu_seg_params);
1322dbb799c3SYuval Mintz 	}
1323fe56b9e6SYuval Mintz }
1324fe56b9e6SYuval Mintz 
1325fe56b9e6SYuval Mintz void qed_qm_init_pf(struct qed_hwfn *p_hwfn)
1326fe56b9e6SYuval Mintz {
1327fe56b9e6SYuval Mintz 	struct qed_qm_pf_rt_init_params params;
1328fe56b9e6SYuval Mintz 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1329fe56b9e6SYuval Mintz 	struct qed_qm_iids iids;
1330fe56b9e6SYuval Mintz 
1331fe56b9e6SYuval Mintz 	memset(&iids, 0, sizeof(iids));
1332fe56b9e6SYuval Mintz 	qed_cxt_qm_iids(p_hwfn, &iids);
1333fe56b9e6SYuval Mintz 
1334fe56b9e6SYuval Mintz 	memset(&params, 0, sizeof(params));
1335fe56b9e6SYuval Mintz 	params.port_id = p_hwfn->port_id;
1336fe56b9e6SYuval Mintz 	params.pf_id = p_hwfn->rel_pf_id;
1337fe56b9e6SYuval Mintz 	params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
1338fe56b9e6SYuval Mintz 	params.is_first_pf = p_hwfn->first_on_engine;
1339fe56b9e6SYuval Mintz 	params.num_pf_cids = iids.cids;
13401408cc1fSYuval Mintz 	params.num_vf_cids = iids.vf_cids;
1341fe56b9e6SYuval Mintz 	params.start_pq = qm_info->start_pq;
13421408cc1fSYuval Mintz 	params.num_pf_pqs = qm_info->num_pqs - qm_info->num_vf_pqs;
13431408cc1fSYuval Mintz 	params.num_vf_pqs = qm_info->num_vf_pqs;
1344fc48b7a6SYuval Mintz 	params.start_vport = qm_info->start_vport;
1345fc48b7a6SYuval Mintz 	params.num_vports = qm_info->num_vports;
1346fe56b9e6SYuval Mintz 	params.pf_wfq = qm_info->pf_wfq;
1347fe56b9e6SYuval Mintz 	params.pf_rl = qm_info->pf_rl;
1348fe56b9e6SYuval Mintz 	params.pq_params = qm_info->qm_pq_params;
1349fe56b9e6SYuval Mintz 	params.vport_params = qm_info->qm_vport_params;
1350fe56b9e6SYuval Mintz 
1351fe56b9e6SYuval Mintz 	qed_qm_pf_rt_init(p_hwfn, p_hwfn->p_main_ptt, &params);
1352fe56b9e6SYuval Mintz }
1353fe56b9e6SYuval Mintz 
1354fe56b9e6SYuval Mintz /* CM PF */
1355fe56b9e6SYuval Mintz static int qed_cm_init_pf(struct qed_hwfn *p_hwfn)
1356fe56b9e6SYuval Mintz {
1357fe56b9e6SYuval Mintz 	union qed_qm_pq_params pq_params;
1358fe56b9e6SYuval Mintz 	u16 pq;
1359fe56b9e6SYuval Mintz 
1360fe56b9e6SYuval Mintz 	/* XCM pure-LB queue */
1361fe56b9e6SYuval Mintz 	memset(&pq_params, 0, sizeof(pq_params));
1362fe56b9e6SYuval Mintz 	pq_params.core.tc = LB_TC;
1363fe56b9e6SYuval Mintz 	pq = qed_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
1364fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, XCM_REG_CON_PHY_Q3_RT_OFFSET, pq);
1365fe56b9e6SYuval Mintz 
1366fe56b9e6SYuval Mintz 	return 0;
1367fe56b9e6SYuval Mintz }
1368fe56b9e6SYuval Mintz 
1369fe56b9e6SYuval Mintz /* DQ PF */
1370fe56b9e6SYuval Mintz static void qed_dq_init_pf(struct qed_hwfn *p_hwfn)
1371fe56b9e6SYuval Mintz {
1372fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
13731408cc1fSYuval Mintz 	u32 dq_pf_max_cid = 0, dq_vf_max_cid = 0;
1374fe56b9e6SYuval Mintz 
1375fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[0].cid_count >> DQ_RANGE_SHIFT);
1376fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_0_RT_OFFSET, dq_pf_max_cid);
1377fe56b9e6SYuval Mintz 
13781408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[0].cids_per_vf >> DQ_RANGE_SHIFT);
13791408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_0_RT_OFFSET, dq_vf_max_cid);
13801408cc1fSYuval Mintz 
1381fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[1].cid_count >> DQ_RANGE_SHIFT);
1382fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_1_RT_OFFSET, dq_pf_max_cid);
1383fe56b9e6SYuval Mintz 
13841408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[1].cids_per_vf >> DQ_RANGE_SHIFT);
13851408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_1_RT_OFFSET, dq_vf_max_cid);
13861408cc1fSYuval Mintz 
1387fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[2].cid_count >> DQ_RANGE_SHIFT);
1388fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_2_RT_OFFSET, dq_pf_max_cid);
1389fe56b9e6SYuval Mintz 
13901408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[2].cids_per_vf >> DQ_RANGE_SHIFT);
13911408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_2_RT_OFFSET, dq_vf_max_cid);
13921408cc1fSYuval Mintz 
1393fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[3].cid_count >> DQ_RANGE_SHIFT);
1394fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_3_RT_OFFSET, dq_pf_max_cid);
1395fe56b9e6SYuval Mintz 
13961408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[3].cids_per_vf >> DQ_RANGE_SHIFT);
13971408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_3_RT_OFFSET, dq_vf_max_cid);
13981408cc1fSYuval Mintz 
1399fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[4].cid_count >> DQ_RANGE_SHIFT);
1400fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_4_RT_OFFSET, dq_pf_max_cid);
1401fe56b9e6SYuval Mintz 
14021408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[4].cids_per_vf >> DQ_RANGE_SHIFT);
14031408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_4_RT_OFFSET, dq_vf_max_cid);
14041408cc1fSYuval Mintz 
1405fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[5].cid_count >> DQ_RANGE_SHIFT);
1406fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_5_RT_OFFSET, dq_pf_max_cid);
14071408cc1fSYuval Mintz 
14081408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[5].cids_per_vf >> DQ_RANGE_SHIFT);
14091408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_5_RT_OFFSET, dq_vf_max_cid);
14101408cc1fSYuval Mintz 
14111408cc1fSYuval Mintz 	/* Connection types 6 & 7 are not in use, yet they must be configured
14121408cc1fSYuval Mintz 	 * as the highest possible connection. Not configuring them means the
14131408cc1fSYuval Mintz 	 * defaults will be  used, and with a large number of cids a bug may
14141408cc1fSYuval Mintz 	 * occur, if the defaults will be smaller than dq_pf_max_cid /
14151408cc1fSYuval Mintz 	 * dq_vf_max_cid.
14161408cc1fSYuval Mintz 	 */
14171408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_6_RT_OFFSET, dq_pf_max_cid);
14181408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_6_RT_OFFSET, dq_vf_max_cid);
14191408cc1fSYuval Mintz 
14201408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_7_RT_OFFSET, dq_pf_max_cid);
14211408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_7_RT_OFFSET, dq_vf_max_cid);
1422fe56b9e6SYuval Mintz }
1423fe56b9e6SYuval Mintz 
1424fe56b9e6SYuval Mintz static void qed_ilt_bounds_init(struct qed_hwfn *p_hwfn)
1425fe56b9e6SYuval Mintz {
1426fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *ilt_clients;
1427fe56b9e6SYuval Mintz 	int i;
1428fe56b9e6SYuval Mintz 
1429fe56b9e6SYuval Mintz 	ilt_clients = p_hwfn->p_cxt_mngr->clients;
1430fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, ilt_clients) {
1431fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1432fe56b9e6SYuval Mintz 			     ilt_clients[i].first.reg,
1433fe56b9e6SYuval Mintz 			     ilt_clients[i].first.val);
1434fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1435dbb799c3SYuval Mintz 			     ilt_clients[i].last.reg, ilt_clients[i].last.val);
1436fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1437fe56b9e6SYuval Mintz 			     ilt_clients[i].p_size.reg,
1438fe56b9e6SYuval Mintz 			     ilt_clients[i].p_size.val);
1439fe56b9e6SYuval Mintz 	}
1440fe56b9e6SYuval Mintz }
1441fe56b9e6SYuval Mintz 
14421408cc1fSYuval Mintz static void qed_ilt_vf_bounds_init(struct qed_hwfn *p_hwfn)
14431408cc1fSYuval Mintz {
14441408cc1fSYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
14451408cc1fSYuval Mintz 	u32 blk_factor;
14461408cc1fSYuval Mintz 
14471408cc1fSYuval Mintz 	/* For simplicty  we set the 'block' to be an ILT page */
14481408cc1fSYuval Mintz 	if (p_hwfn->cdev->p_iov_info) {
14491408cc1fSYuval Mintz 		struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
14501408cc1fSYuval Mintz 
14511408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
14521408cc1fSYuval Mintz 			     PSWRQ2_REG_VF_BASE_RT_OFFSET,
14531408cc1fSYuval Mintz 			     p_iov->first_vf_in_pf);
14541408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
14551408cc1fSYuval Mintz 			     PSWRQ2_REG_VF_LAST_ILT_RT_OFFSET,
14561408cc1fSYuval Mintz 			     p_iov->first_vf_in_pf + p_iov->total_vfs);
14571408cc1fSYuval Mintz 	}
14581408cc1fSYuval Mintz 
14591408cc1fSYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
14601408cc1fSYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
14611408cc1fSYuval Mintz 	if (p_cli->active) {
14621408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
14631408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_BLOCKS_FACTOR_RT_OFFSET,
14641408cc1fSYuval Mintz 			     blk_factor);
14651408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
14661408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
14671408cc1fSYuval Mintz 			     p_cli->pf_total_lines);
14681408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
14691408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_VF_BLOCKS_RT_OFFSET,
14701408cc1fSYuval Mintz 			     p_cli->vf_total_lines);
14711408cc1fSYuval Mintz 	}
1472dbb799c3SYuval Mintz 
1473dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1474dbb799c3SYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1475dbb799c3SYuval Mintz 	if (p_cli->active) {
1476dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1477dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_BLOCKS_FACTOR_RT_OFFSET,
1478dbb799c3SYuval Mintz 			     blk_factor);
1479dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1480dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1481dbb799c3SYuval Mintz 			     p_cli->pf_total_lines);
1482dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1483dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_VF_BLOCKS_RT_OFFSET,
1484dbb799c3SYuval Mintz 			     p_cli->vf_total_lines);
1485dbb799c3SYuval Mintz 	}
1486dbb799c3SYuval Mintz 
1487dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TM];
1488dbb799c3SYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1489dbb799c3SYuval Mintz 	if (p_cli->active) {
1490dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1491dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_BLOCKS_FACTOR_RT_OFFSET, blk_factor);
1492dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1493dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1494dbb799c3SYuval Mintz 			     p_cli->pf_total_lines);
1495dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1496dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_VF_BLOCKS_RT_OFFSET,
1497dbb799c3SYuval Mintz 			     p_cli->vf_total_lines);
1498dbb799c3SYuval Mintz 	}
14991408cc1fSYuval Mintz }
15001408cc1fSYuval Mintz 
1501fe56b9e6SYuval Mintz /* ILT (PSWRQ2) PF */
1502fe56b9e6SYuval Mintz static void qed_ilt_init_pf(struct qed_hwfn *p_hwfn)
1503fe56b9e6SYuval Mintz {
1504fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *clients;
1505fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr;
1506fe56b9e6SYuval Mintz 	struct qed_dma_mem *p_shdw;
1507fe56b9e6SYuval Mintz 	u32 line, rt_offst, i;
1508fe56b9e6SYuval Mintz 
1509fe56b9e6SYuval Mintz 	qed_ilt_bounds_init(p_hwfn);
15101408cc1fSYuval Mintz 	qed_ilt_vf_bounds_init(p_hwfn);
1511fe56b9e6SYuval Mintz 
1512fe56b9e6SYuval Mintz 	p_mngr = p_hwfn->p_cxt_mngr;
1513fe56b9e6SYuval Mintz 	p_shdw = p_mngr->ilt_shadow;
1514fe56b9e6SYuval Mintz 	clients = p_hwfn->p_cxt_mngr->clients;
1515fe56b9e6SYuval Mintz 
1516fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, clients) {
1517fe56b9e6SYuval Mintz 		/** Client's 1st val and RT array are absolute, ILT shadows'
1518fe56b9e6SYuval Mintz 		 *  lines are relative.
1519fe56b9e6SYuval Mintz 		 */
1520fe56b9e6SYuval Mintz 		line = clients[i].first.val - p_mngr->pf_start_line;
1521fe56b9e6SYuval Mintz 		rt_offst = PSWRQ2_REG_ILT_MEMORY_RT_OFFSET +
1522fe56b9e6SYuval Mintz 			   clients[i].first.val * ILT_ENTRY_IN_REGS;
1523fe56b9e6SYuval Mintz 
1524fe56b9e6SYuval Mintz 		for (; line <= clients[i].last.val - p_mngr->pf_start_line;
1525fe56b9e6SYuval Mintz 		     line++, rt_offst += ILT_ENTRY_IN_REGS) {
1526fe56b9e6SYuval Mintz 			u64 ilt_hw_entry = 0;
1527fe56b9e6SYuval Mintz 
1528fe56b9e6SYuval Mintz 			/** p_virt could be NULL incase of dynamic
1529fe56b9e6SYuval Mintz 			 *  allocation
1530fe56b9e6SYuval Mintz 			 */
1531fe56b9e6SYuval Mintz 			if (p_shdw[line].p_virt) {
1532fe56b9e6SYuval Mintz 				SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
1533fe56b9e6SYuval Mintz 				SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
1534fe56b9e6SYuval Mintz 					  (p_shdw[line].p_phys >> 12));
1535fe56b9e6SYuval Mintz 
1536fe56b9e6SYuval Mintz 				DP_VERBOSE(p_hwfn, QED_MSG_ILT,
1537fe56b9e6SYuval Mintz 					   "Setting RT[0x%08x] from ILT[0x%08x] [Client is %d] to Physical addr: 0x%llx\n",
1538fe56b9e6SYuval Mintz 					   rt_offst, line, i,
1539fe56b9e6SYuval Mintz 					   (u64)(p_shdw[line].p_phys >> 12));
1540fe56b9e6SYuval Mintz 			}
1541fe56b9e6SYuval Mintz 
1542fe56b9e6SYuval Mintz 			STORE_RT_REG_AGG(p_hwfn, rt_offst, ilt_hw_entry);
1543fe56b9e6SYuval Mintz 		}
1544fe56b9e6SYuval Mintz 	}
1545fe56b9e6SYuval Mintz }
1546fe56b9e6SYuval Mintz 
1547dbb799c3SYuval Mintz /* SRC (Searcher) PF */
1548dbb799c3SYuval Mintz static void qed_src_init_pf(struct qed_hwfn *p_hwfn)
1549dbb799c3SYuval Mintz {
1550dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1551dbb799c3SYuval Mintz 	u32 rounded_conn_num, conn_num, conn_max;
1552dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
1553dbb799c3SYuval Mintz 
1554dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
1555dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
1556dbb799c3SYuval Mintz 	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
1557dbb799c3SYuval Mintz 	if (!conn_num)
1558dbb799c3SYuval Mintz 		return;
1559dbb799c3SYuval Mintz 
1560dbb799c3SYuval Mintz 	conn_max = max_t(u32, conn_num, SRC_MIN_NUM_ELEMS);
1561dbb799c3SYuval Mintz 	rounded_conn_num = roundup_pow_of_two(conn_max);
1562dbb799c3SYuval Mintz 
1563dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, SRC_REG_COUNTFREE_RT_OFFSET, conn_num);
1564dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, SRC_REG_NUMBER_HASH_BITS_RT_OFFSET,
1565dbb799c3SYuval Mintz 		     ilog2(rounded_conn_num));
1566dbb799c3SYuval Mintz 
1567dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, SRC_REG_FIRSTFREE_RT_OFFSET,
1568dbb799c3SYuval Mintz 			 p_hwfn->p_cxt_mngr->first_free);
1569dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, SRC_REG_LASTFREE_RT_OFFSET,
1570dbb799c3SYuval Mintz 			 p_hwfn->p_cxt_mngr->last_free);
1571dbb799c3SYuval Mintz }
1572dbb799c3SYuval Mintz 
1573dbb799c3SYuval Mintz /* Timers PF */
1574dbb799c3SYuval Mintz #define TM_CFG_NUM_IDS_SHIFT            0
1575dbb799c3SYuval Mintz #define TM_CFG_NUM_IDS_MASK             0xFFFFULL
1576dbb799c3SYuval Mintz #define TM_CFG_PRE_SCAN_OFFSET_SHIFT    16
1577dbb799c3SYuval Mintz #define TM_CFG_PRE_SCAN_OFFSET_MASK     0x1FFULL
1578dbb799c3SYuval Mintz #define TM_CFG_PARENT_PF_SHIFT          25
1579dbb799c3SYuval Mintz #define TM_CFG_PARENT_PF_MASK           0x7ULL
1580dbb799c3SYuval Mintz 
1581dbb799c3SYuval Mintz #define TM_CFG_CID_PRE_SCAN_ROWS_SHIFT  30
1582dbb799c3SYuval Mintz #define TM_CFG_CID_PRE_SCAN_ROWS_MASK   0x1FFULL
1583dbb799c3SYuval Mintz 
1584dbb799c3SYuval Mintz #define TM_CFG_TID_OFFSET_SHIFT         30
1585dbb799c3SYuval Mintz #define TM_CFG_TID_OFFSET_MASK          0x7FFFFULL
1586dbb799c3SYuval Mintz #define TM_CFG_TID_PRE_SCAN_ROWS_SHIFT  49
1587dbb799c3SYuval Mintz #define TM_CFG_TID_PRE_SCAN_ROWS_MASK   0x1FFULL
1588dbb799c3SYuval Mintz 
1589dbb799c3SYuval Mintz static void qed_tm_init_pf(struct qed_hwfn *p_hwfn)
1590dbb799c3SYuval Mintz {
1591dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1592dbb799c3SYuval Mintz 	u32 active_seg_mask = 0, tm_offset, rt_reg;
1593dbb799c3SYuval Mintz 	struct qed_tm_iids tm_iids;
1594dbb799c3SYuval Mintz 	u64 cfg_word;
1595dbb799c3SYuval Mintz 	u8 i;
1596dbb799c3SYuval Mintz 
1597dbb799c3SYuval Mintz 	memset(&tm_iids, 0, sizeof(tm_iids));
1598dbb799c3SYuval Mintz 	qed_cxt_tm_iids(p_mngr, &tm_iids);
1599dbb799c3SYuval Mintz 
1600dbb799c3SYuval Mintz 	/* @@@TBD No pre-scan for now */
1601dbb799c3SYuval Mintz 
1602dbb799c3SYuval Mintz 	/* Note: We assume consecutive VFs for a PF */
1603dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->vf_count; i++) {
1604dbb799c3SYuval Mintz 		cfg_word = 0;
1605dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_cids);
1606dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1607dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1608dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);
1609dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1610dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1611dbb799c3SYuval Mintz 		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
1612dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1613dbb799c3SYuval Mintz 	}
1614dbb799c3SYuval Mintz 
1615dbb799c3SYuval Mintz 	cfg_word = 0;
1616dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_cids);
1617dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1618dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);	/* n/a for PF */
1619dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);	/* scan all   */
1620dbb799c3SYuval Mintz 
1621dbb799c3SYuval Mintz 	rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1622dbb799c3SYuval Mintz 	    (sizeof(cfg_word) / sizeof(u32)) *
1623dbb799c3SYuval Mintz 	    (NUM_OF_VFS(p_hwfn->cdev) + p_hwfn->rel_pf_id);
1624dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1625dbb799c3SYuval Mintz 
1626dbb799c3SYuval Mintz 	/* enale scan */
1627dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_CONN_RT_OFFSET,
1628dbb799c3SYuval Mintz 		     tm_iids.pf_cids ? 0x1 : 0x0);
1629dbb799c3SYuval Mintz 
1630dbb799c3SYuval Mintz 	/* @@@TBD how to enable the scan for the VFs */
1631dbb799c3SYuval Mintz 
1632dbb799c3SYuval Mintz 	tm_offset = tm_iids.per_vf_cids;
1633dbb799c3SYuval Mintz 
1634dbb799c3SYuval Mintz 	/* Note: We assume consecutive VFs for a PF */
1635dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->vf_count; i++) {
1636dbb799c3SYuval Mintz 		cfg_word = 0;
1637dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_tids);
1638dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1639dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1640dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1641dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
1642dbb799c3SYuval Mintz 
1643dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1644dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1645dbb799c3SYuval Mintz 		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
1646dbb799c3SYuval Mintz 
1647dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1648dbb799c3SYuval Mintz 	}
1649dbb799c3SYuval Mintz 
1650dbb799c3SYuval Mintz 	tm_offset = tm_iids.pf_cids;
1651dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1652dbb799c3SYuval Mintz 		cfg_word = 0;
1653dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_tids[i]);
1654dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1655dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);
1656dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1657dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
1658dbb799c3SYuval Mintz 
1659dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1660dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1661dbb799c3SYuval Mintz 		    (NUM_OF_VFS(p_hwfn->cdev) +
1662dbb799c3SYuval Mintz 		     p_hwfn->rel_pf_id * NUM_TASK_PF_SEGMENTS + i);
1663dbb799c3SYuval Mintz 
1664dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
16651a635e48SYuval Mintz 		active_seg_mask |= (tm_iids.pf_tids[i] ? BIT(i) : 0);
1666dbb799c3SYuval Mintz 
1667dbb799c3SYuval Mintz 		tm_offset += tm_iids.pf_tids[i];
1668dbb799c3SYuval Mintz 	}
1669dbb799c3SYuval Mintz 
1670dbb799c3SYuval Mintz 	if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE)
1671dbb799c3SYuval Mintz 		active_seg_mask = 0;
1672dbb799c3SYuval Mintz 
1673dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_TASK_RT_OFFSET, active_seg_mask);
1674dbb799c3SYuval Mintz 
1675dbb799c3SYuval Mintz 	/* @@@TBD how to enable the scan for the VFs */
1676dbb799c3SYuval Mintz }
1677dbb799c3SYuval Mintz 
1678fe56b9e6SYuval Mintz void qed_cxt_hw_init_common(struct qed_hwfn *p_hwfn)
1679fe56b9e6SYuval Mintz {
1680fe56b9e6SYuval Mintz 	qed_cdu_init_common(p_hwfn);
1681fe56b9e6SYuval Mintz }
1682fe56b9e6SYuval Mintz 
1683fe56b9e6SYuval Mintz void qed_cxt_hw_init_pf(struct qed_hwfn *p_hwfn)
1684fe56b9e6SYuval Mintz {
1685fe56b9e6SYuval Mintz 	qed_qm_init_pf(p_hwfn);
1686fe56b9e6SYuval Mintz 	qed_cm_init_pf(p_hwfn);
1687fe56b9e6SYuval Mintz 	qed_dq_init_pf(p_hwfn);
1688dbb799c3SYuval Mintz 	qed_cdu_init_pf(p_hwfn);
1689fe56b9e6SYuval Mintz 	qed_ilt_init_pf(p_hwfn);
1690dbb799c3SYuval Mintz 	qed_src_init_pf(p_hwfn);
1691dbb799c3SYuval Mintz 	qed_tm_init_pf(p_hwfn);
1692fe56b9e6SYuval Mintz }
1693fe56b9e6SYuval Mintz 
1694fe56b9e6SYuval Mintz int qed_cxt_acquire_cid(struct qed_hwfn *p_hwfn,
16951a635e48SYuval Mintz 			enum protocol_type type, u32 *p_cid)
1696fe56b9e6SYuval Mintz {
1697fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1698fe56b9e6SYuval Mintz 	u32 rel_cid;
1699fe56b9e6SYuval Mintz 
1700fe56b9e6SYuval Mintz 	if (type >= MAX_CONN_TYPES || !p_mngr->acquired[type].cid_map) {
1701fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Invalid protocol type %d", type);
1702fe56b9e6SYuval Mintz 		return -EINVAL;
1703fe56b9e6SYuval Mintz 	}
1704fe56b9e6SYuval Mintz 
1705fe56b9e6SYuval Mintz 	rel_cid = find_first_zero_bit(p_mngr->acquired[type].cid_map,
1706fe56b9e6SYuval Mintz 				      p_mngr->acquired[type].max_count);
1707fe56b9e6SYuval Mintz 
1708fe56b9e6SYuval Mintz 	if (rel_cid >= p_mngr->acquired[type].max_count) {
17091a635e48SYuval Mintz 		DP_NOTICE(p_hwfn, "no CID available for protocol %d\n", type);
1710fe56b9e6SYuval Mintz 		return -EINVAL;
1711fe56b9e6SYuval Mintz 	}
1712fe56b9e6SYuval Mintz 
1713fe56b9e6SYuval Mintz 	__set_bit(rel_cid, p_mngr->acquired[type].cid_map);
1714fe56b9e6SYuval Mintz 
1715fe56b9e6SYuval Mintz 	*p_cid = rel_cid + p_mngr->acquired[type].start_cid;
1716fe56b9e6SYuval Mintz 
1717fe56b9e6SYuval Mintz 	return 0;
1718fe56b9e6SYuval Mintz }
1719fe56b9e6SYuval Mintz 
1720fe56b9e6SYuval Mintz static bool qed_cxt_test_cid_acquired(struct qed_hwfn *p_hwfn,
17211a635e48SYuval Mintz 				      u32 cid, enum protocol_type *p_type)
1722fe56b9e6SYuval Mintz {
1723fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1724fe56b9e6SYuval Mintz 	struct qed_cid_acquired_map *p_map;
1725fe56b9e6SYuval Mintz 	enum protocol_type p;
1726fe56b9e6SYuval Mintz 	u32 rel_cid;
1727fe56b9e6SYuval Mintz 
1728fe56b9e6SYuval Mintz 	/* Iterate over protocols and find matching cid range */
1729fe56b9e6SYuval Mintz 	for (p = 0; p < MAX_CONN_TYPES; p++) {
1730fe56b9e6SYuval Mintz 		p_map = &p_mngr->acquired[p];
1731fe56b9e6SYuval Mintz 
1732fe56b9e6SYuval Mintz 		if (!p_map->cid_map)
1733fe56b9e6SYuval Mintz 			continue;
1734fe56b9e6SYuval Mintz 		if (cid >= p_map->start_cid &&
1735fe56b9e6SYuval Mintz 		    cid < p_map->start_cid + p_map->max_count)
1736fe56b9e6SYuval Mintz 			break;
1737fe56b9e6SYuval Mintz 	}
1738fe56b9e6SYuval Mintz 	*p_type = p;
1739fe56b9e6SYuval Mintz 
1740fe56b9e6SYuval Mintz 	if (p == MAX_CONN_TYPES) {
1741fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Invalid CID %d", cid);
1742fe56b9e6SYuval Mintz 		return false;
1743fe56b9e6SYuval Mintz 	}
1744fe56b9e6SYuval Mintz 
1745fe56b9e6SYuval Mintz 	rel_cid = cid - p_map->start_cid;
1746fe56b9e6SYuval Mintz 	if (!test_bit(rel_cid, p_map->cid_map)) {
1747fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "CID %d not acquired", cid);
1748fe56b9e6SYuval Mintz 		return false;
1749fe56b9e6SYuval Mintz 	}
1750fe56b9e6SYuval Mintz 	return true;
1751fe56b9e6SYuval Mintz }
1752fe56b9e6SYuval Mintz 
17531a635e48SYuval Mintz void qed_cxt_release_cid(struct qed_hwfn *p_hwfn, u32 cid)
1754fe56b9e6SYuval Mintz {
1755fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1756fe56b9e6SYuval Mintz 	enum protocol_type type;
1757fe56b9e6SYuval Mintz 	bool b_acquired;
1758fe56b9e6SYuval Mintz 	u32 rel_cid;
1759fe56b9e6SYuval Mintz 
1760fe56b9e6SYuval Mintz 	/* Test acquired and find matching per-protocol map */
1761fe56b9e6SYuval Mintz 	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, cid, &type);
1762fe56b9e6SYuval Mintz 
1763fe56b9e6SYuval Mintz 	if (!b_acquired)
1764fe56b9e6SYuval Mintz 		return;
1765fe56b9e6SYuval Mintz 
1766fe56b9e6SYuval Mintz 	rel_cid = cid - p_mngr->acquired[type].start_cid;
1767fe56b9e6SYuval Mintz 	__clear_bit(rel_cid, p_mngr->acquired[type].cid_map);
1768fe56b9e6SYuval Mintz }
1769fe56b9e6SYuval Mintz 
17701a635e48SYuval Mintz int qed_cxt_get_cid_info(struct qed_hwfn *p_hwfn, struct qed_cxt_info *p_info)
1771fe56b9e6SYuval Mintz {
1772fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1773fe56b9e6SYuval Mintz 	u32 conn_cxt_size, hw_p_size, cxts_per_p, line;
1774fe56b9e6SYuval Mintz 	enum protocol_type type;
1775fe56b9e6SYuval Mintz 	bool b_acquired;
1776fe56b9e6SYuval Mintz 
1777fe56b9e6SYuval Mintz 	/* Test acquired and find matching per-protocol map */
1778fe56b9e6SYuval Mintz 	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, p_info->iid, &type);
1779fe56b9e6SYuval Mintz 
1780fe56b9e6SYuval Mintz 	if (!b_acquired)
1781fe56b9e6SYuval Mintz 		return -EINVAL;
1782fe56b9e6SYuval Mintz 
1783fe56b9e6SYuval Mintz 	/* set the protocl type */
1784fe56b9e6SYuval Mintz 	p_info->type = type;
1785fe56b9e6SYuval Mintz 
1786fe56b9e6SYuval Mintz 	/* compute context virtual pointer */
1787fe56b9e6SYuval Mintz 	hw_p_size = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1788fe56b9e6SYuval Mintz 
1789fe56b9e6SYuval Mintz 	conn_cxt_size = CONN_CXT_SIZE(p_hwfn);
1790fe56b9e6SYuval Mintz 	cxts_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / conn_cxt_size;
1791fe56b9e6SYuval Mintz 	line = p_info->iid / cxts_per_p;
1792fe56b9e6SYuval Mintz 
1793fe56b9e6SYuval Mintz 	/* Make sure context is allocated (dynamic allocation) */
1794fe56b9e6SYuval Mintz 	if (!p_mngr->ilt_shadow[line].p_virt)
1795fe56b9e6SYuval Mintz 		return -EINVAL;
1796fe56b9e6SYuval Mintz 
1797fe56b9e6SYuval Mintz 	p_info->p_cxt = p_mngr->ilt_shadow[line].p_virt +
1798fe56b9e6SYuval Mintz 			p_info->iid % cxts_per_p * conn_cxt_size;
1799fe56b9e6SYuval Mintz 
1800fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, (QED_MSG_ILT | QED_MSG_CXT),
1801fe56b9e6SYuval Mintz 		   "Accessing ILT shadow[%d]: CXT pointer is at %p (for iid %d)\n",
1802fe56b9e6SYuval Mintz 		   p_info->iid / cxts_per_p, p_info->p_cxt, p_info->iid);
1803fe56b9e6SYuval Mintz 
1804fe56b9e6SYuval Mintz 	return 0;
1805fe56b9e6SYuval Mintz }
1806fe56b9e6SYuval Mintz 
1807dbb799c3SYuval Mintz void qed_rdma_set_pf_params(struct qed_hwfn *p_hwfn,
1808dbb799c3SYuval Mintz 			    struct qed_rdma_pf_params *p_params)
1809dbb799c3SYuval Mintz {
1810dbb799c3SYuval Mintz 	u32 num_cons, num_tasks, num_qps, num_mrs, num_srqs;
1811dbb799c3SYuval Mintz 	enum protocol_type proto;
1812dbb799c3SYuval Mintz 
1813dbb799c3SYuval Mintz 	num_mrs = min_t(u32, RDMA_MAX_TIDS, p_params->num_mrs);
1814dbb799c3SYuval Mintz 	num_tasks = num_mrs;	/* each mr uses a single task id */
1815dbb799c3SYuval Mintz 	num_srqs = min_t(u32, 32 * 1024, p_params->num_srqs);
1816dbb799c3SYuval Mintz 
1817dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
1818dbb799c3SYuval Mintz 	case QED_PCI_ETH_ROCE:
1819dbb799c3SYuval Mintz 		num_qps = min_t(u32, ROCE_MAX_QPS, p_params->num_qps);
1820dbb799c3SYuval Mintz 		num_cons = num_qps * 2;	/* each QP requires two connections */
1821dbb799c3SYuval Mintz 		proto = PROTOCOLID_ROCE;
1822dbb799c3SYuval Mintz 		break;
1823dbb799c3SYuval Mintz 	default:
1824dbb799c3SYuval Mintz 		return;
1825dbb799c3SYuval Mintz 	}
1826dbb799c3SYuval Mintz 
1827dbb799c3SYuval Mintz 	if (num_cons && num_tasks) {
1828dbb799c3SYuval Mintz 		qed_cxt_set_proto_cid_count(p_hwfn, proto, num_cons, 0);
1829dbb799c3SYuval Mintz 
1830dbb799c3SYuval Mintz 		/* Deliberatly passing ROCE for tasks id. This is because
1831dbb799c3SYuval Mintz 		 * iWARP / RoCE share the task id.
1832dbb799c3SYuval Mintz 		 */
1833dbb799c3SYuval Mintz 		qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_ROCE,
1834dbb799c3SYuval Mintz 					    QED_CXT_ROCE_TID_SEG, 1,
1835dbb799c3SYuval Mintz 					    num_tasks, false);
1836dbb799c3SYuval Mintz 		qed_cxt_set_srq_count(p_hwfn, num_srqs);
1837dbb799c3SYuval Mintz 	} else {
1838dbb799c3SYuval Mintz 		DP_INFO(p_hwfn->cdev,
1839dbb799c3SYuval Mintz 			"RDMA personality used without setting params!\n");
1840dbb799c3SYuval Mintz 	}
1841dbb799c3SYuval Mintz }
1842dbb799c3SYuval Mintz 
1843fe56b9e6SYuval Mintz int qed_cxt_set_pf_params(struct qed_hwfn *p_hwfn)
1844fe56b9e6SYuval Mintz {
1845fe56b9e6SYuval Mintz 	/* Set the number of required CORE connections */
1846fe56b9e6SYuval Mintz 	u32 core_cids = 1; /* SPQ */
1847fe56b9e6SYuval Mintz 
18480a7fb11cSYuval Mintz 	if (p_hwfn->using_ll2)
18490a7fb11cSYuval Mintz 		core_cids += 4;
18501408cc1fSYuval Mintz 	qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_CORE, core_cids, 0);
1851fe56b9e6SYuval Mintz 
1852dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
1853dbb799c3SYuval Mintz 	case QED_PCI_ETH_ROCE:
1854dbb799c3SYuval Mintz 	{
1855dbb799c3SYuval Mintz 		qed_rdma_set_pf_params(p_hwfn,
1856dbb799c3SYuval Mintz 				       &p_hwfn->
1857dbb799c3SYuval Mintz 				       pf_params.rdma_pf_params);
1858dbb799c3SYuval Mintz 		/* no need for break since RoCE coexist with Ethernet */
1859dbb799c3SYuval Mintz 	}
1860dbb799c3SYuval Mintz 	case QED_PCI_ETH:
1861dbb799c3SYuval Mintz 	{
1862dbb799c3SYuval Mintz 		struct qed_eth_pf_params *p_params =
1863dbb799c3SYuval Mintz 		    &p_hwfn->pf_params.eth_pf_params;
1864dbb799c3SYuval Mintz 
1865fe56b9e6SYuval Mintz 		qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
18661408cc1fSYuval Mintz 					    p_params->num_cons, 1);
1867dbb799c3SYuval Mintz 		break;
1868dbb799c3SYuval Mintz 	}
1869dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
1870dbb799c3SYuval Mintz 	{
1871dbb799c3SYuval Mintz 		struct qed_iscsi_pf_params *p_params;
1872dbb799c3SYuval Mintz 
1873dbb799c3SYuval Mintz 		p_params = &p_hwfn->pf_params.iscsi_pf_params;
1874dbb799c3SYuval Mintz 
1875dbb799c3SYuval Mintz 		if (p_params->num_cons && p_params->num_tasks) {
1876dbb799c3SYuval Mintz 			qed_cxt_set_proto_cid_count(p_hwfn,
1877dbb799c3SYuval Mintz 						    PROTOCOLID_ISCSI,
1878dbb799c3SYuval Mintz 						    p_params->num_cons,
1879dbb799c3SYuval Mintz 						    0);
1880dbb799c3SYuval Mintz 
1881dbb799c3SYuval Mintz 			qed_cxt_set_proto_tid_count(p_hwfn,
1882dbb799c3SYuval Mintz 						    PROTOCOLID_ISCSI,
1883dbb799c3SYuval Mintz 						    QED_CXT_ISCSI_TID_SEG,
1884dbb799c3SYuval Mintz 						    0,
1885dbb799c3SYuval Mintz 						    p_params->num_tasks,
1886dbb799c3SYuval Mintz 						    true);
1887dbb799c3SYuval Mintz 		} else {
1888dbb799c3SYuval Mintz 			DP_INFO(p_hwfn->cdev,
1889dbb799c3SYuval Mintz 				"Iscsi personality used without setting params!\n");
1890dbb799c3SYuval Mintz 		}
1891dbb799c3SYuval Mintz 		break;
1892dbb799c3SYuval Mintz 	}
1893dbb799c3SYuval Mintz 	default:
1894dbb799c3SYuval Mintz 		return -EINVAL;
1895dbb799c3SYuval Mintz 	}
1896dbb799c3SYuval Mintz 
1897dbb799c3SYuval Mintz 	return 0;
1898dbb799c3SYuval Mintz }
1899dbb799c3SYuval Mintz 
1900dbb799c3SYuval Mintz int qed_cxt_get_tid_mem_info(struct qed_hwfn *p_hwfn,
1901dbb799c3SYuval Mintz 			     struct qed_tid_mem *p_info)
1902dbb799c3SYuval Mintz {
1903dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1904dbb799c3SYuval Mintz 	u32 proto, seg, total_lines, i, shadow_line;
1905dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
1906dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_fl_seg;
1907dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg_info;
1908dbb799c3SYuval Mintz 
1909dbb799c3SYuval Mintz 	/* Verify the personality */
1910dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
1911dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
1912dbb799c3SYuval Mintz 		proto = PROTOCOLID_ISCSI;
1913dbb799c3SYuval Mintz 		seg = QED_CXT_ISCSI_TID_SEG;
1914dbb799c3SYuval Mintz 		break;
1915dbb799c3SYuval Mintz 	default:
1916dbb799c3SYuval Mintz 		return -EINVAL;
1917dbb799c3SYuval Mintz 	}
1918dbb799c3SYuval Mintz 
1919dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
1920dbb799c3SYuval Mintz 	if (!p_cli->active)
1921dbb799c3SYuval Mintz 		return -EINVAL;
1922dbb799c3SYuval Mintz 
1923dbb799c3SYuval Mintz 	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
1924dbb799c3SYuval Mintz 	if (!p_seg_info->has_fl_mem)
1925dbb799c3SYuval Mintz 		return -EINVAL;
1926dbb799c3SYuval Mintz 
1927dbb799c3SYuval Mintz 	p_fl_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
1928dbb799c3SYuval Mintz 	total_lines = DIV_ROUND_UP(p_fl_seg->total_size,
1929dbb799c3SYuval Mintz 				   p_fl_seg->real_size_in_page);
1930dbb799c3SYuval Mintz 
1931dbb799c3SYuval Mintz 	for (i = 0; i < total_lines; i++) {
1932dbb799c3SYuval Mintz 		shadow_line = i + p_fl_seg->start_line -
1933dbb799c3SYuval Mintz 		    p_hwfn->p_cxt_mngr->pf_start_line;
1934dbb799c3SYuval Mintz 		p_info->blocks[i] = p_mngr->ilt_shadow[shadow_line].p_virt;
1935dbb799c3SYuval Mintz 	}
1936dbb799c3SYuval Mintz 	p_info->waste = ILT_PAGE_IN_BYTES(p_cli->p_size.val) -
1937dbb799c3SYuval Mintz 	    p_fl_seg->real_size_in_page;
1938dbb799c3SYuval Mintz 	p_info->tid_size = p_mngr->task_type_size[p_seg_info->type];
1939dbb799c3SYuval Mintz 	p_info->num_tids_per_block = p_fl_seg->real_size_in_page /
1940dbb799c3SYuval Mintz 	    p_info->tid_size;
1941dbb799c3SYuval Mintz 
1942dbb799c3SYuval Mintz 	return 0;
1943dbb799c3SYuval Mintz }
1944dbb799c3SYuval Mintz 
1945dbb799c3SYuval Mintz /* This function is very RoCE oriented, if another protocol in the future
1946dbb799c3SYuval Mintz  * will want this feature we'll need to modify the function to be more generic
1947dbb799c3SYuval Mintz  */
1948dbb799c3SYuval Mintz int
1949dbb799c3SYuval Mintz qed_cxt_dynamic_ilt_alloc(struct qed_hwfn *p_hwfn,
1950dbb799c3SYuval Mintz 			  enum qed_cxt_elem_type elem_type, u32 iid)
1951dbb799c3SYuval Mintz {
1952dbb799c3SYuval Mintz 	u32 reg_offset, shadow_line, elem_size, hw_p_size, elems_per_p, line;
1953dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
1954dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
1955dbb799c3SYuval Mintz 	struct qed_ptt *p_ptt;
1956dbb799c3SYuval Mintz 	dma_addr_t p_phys;
1957dbb799c3SYuval Mintz 	u64 ilt_hw_entry;
1958dbb799c3SYuval Mintz 	void *p_virt;
1959dbb799c3SYuval Mintz 	int rc = 0;
1960dbb799c3SYuval Mintz 
1961dbb799c3SYuval Mintz 	switch (elem_type) {
1962dbb799c3SYuval Mintz 	case QED_ELEM_CXT:
1963dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
1964dbb799c3SYuval Mintz 		elem_size = CONN_CXT_SIZE(p_hwfn);
1965dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUC_BLK];
1966dbb799c3SYuval Mintz 		break;
1967dbb799c3SYuval Mintz 	case QED_ELEM_SRQ:
1968dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
1969dbb799c3SYuval Mintz 		elem_size = SRQ_CXT_SIZE;
1970dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
1971dbb799c3SYuval Mintz 		break;
1972dbb799c3SYuval Mintz 	case QED_ELEM_TASK:
1973dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1974dbb799c3SYuval Mintz 		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
1975dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
1976dbb799c3SYuval Mintz 		break;
1977dbb799c3SYuval Mintz 	default:
1978dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
1979dbb799c3SYuval Mintz 		return -EINVAL;
1980dbb799c3SYuval Mintz 	}
1981dbb799c3SYuval Mintz 
1982dbb799c3SYuval Mintz 	/* Calculate line in ilt */
1983dbb799c3SYuval Mintz 	hw_p_size = p_cli->p_size.val;
1984dbb799c3SYuval Mintz 	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
1985dbb799c3SYuval Mintz 	line = p_blk->start_line + (iid / elems_per_p);
1986dbb799c3SYuval Mintz 	shadow_line = line - p_hwfn->p_cxt_mngr->pf_start_line;
1987dbb799c3SYuval Mintz 
1988dbb799c3SYuval Mintz 	/* If line is already allocated, do nothing, otherwise allocate it and
1989dbb799c3SYuval Mintz 	 * write it to the PSWRQ2 registers.
1990dbb799c3SYuval Mintz 	 * This section can be run in parallel from different contexts and thus
1991dbb799c3SYuval Mintz 	 * a mutex protection is needed.
1992dbb799c3SYuval Mintz 	 */
1993dbb799c3SYuval Mintz 
1994dbb799c3SYuval Mintz 	mutex_lock(&p_hwfn->p_cxt_mngr->mutex);
1995dbb799c3SYuval Mintz 
1996dbb799c3SYuval Mintz 	if (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt)
1997dbb799c3SYuval Mintz 		goto out0;
1998dbb799c3SYuval Mintz 
1999dbb799c3SYuval Mintz 	p_ptt = qed_ptt_acquire(p_hwfn);
2000dbb799c3SYuval Mintz 	if (!p_ptt) {
2001dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn,
2002dbb799c3SYuval Mintz 			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
2003dbb799c3SYuval Mintz 		rc = -EBUSY;
2004dbb799c3SYuval Mintz 		goto out0;
2005dbb799c3SYuval Mintz 	}
2006dbb799c3SYuval Mintz 
2007dbb799c3SYuval Mintz 	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
2008dbb799c3SYuval Mintz 				    p_blk->real_size_in_page,
2009dbb799c3SYuval Mintz 				    &p_phys, GFP_KERNEL);
2010dbb799c3SYuval Mintz 	if (!p_virt) {
2011dbb799c3SYuval Mintz 		rc = -ENOMEM;
2012dbb799c3SYuval Mintz 		goto out1;
2013dbb799c3SYuval Mintz 	}
2014dbb799c3SYuval Mintz 	memset(p_virt, 0, p_blk->real_size_in_page);
2015dbb799c3SYuval Mintz 
2016dbb799c3SYuval Mintz 	/* configuration of refTagMask to 0xF is required for RoCE DIF MR only,
2017dbb799c3SYuval Mintz 	 * to compensate for a HW bug, but it is configured even if DIF is not
2018dbb799c3SYuval Mintz 	 * enabled. This is harmless and allows us to avoid a dedicated API. We
2019dbb799c3SYuval Mintz 	 * configure the field for all of the contexts on the newly allocated
2020dbb799c3SYuval Mintz 	 * page.
2021dbb799c3SYuval Mintz 	 */
2022dbb799c3SYuval Mintz 	if (elem_type == QED_ELEM_TASK) {
2023dbb799c3SYuval Mintz 		u32 elem_i;
2024dbb799c3SYuval Mintz 		u8 *elem_start = (u8 *)p_virt;
2025dbb799c3SYuval Mintz 		union type1_task_context *elem;
2026dbb799c3SYuval Mintz 
2027dbb799c3SYuval Mintz 		for (elem_i = 0; elem_i < elems_per_p; elem_i++) {
2028dbb799c3SYuval Mintz 			elem = (union type1_task_context *)elem_start;
2029dbb799c3SYuval Mintz 			SET_FIELD(elem->roce_ctx.tdif_context.flags1,
2030dbb799c3SYuval Mintz 				  TDIF_TASK_CONTEXT_REFTAGMASK, 0xf);
2031dbb799c3SYuval Mintz 			elem_start += TYPE1_TASK_CXT_SIZE(p_hwfn);
2032dbb799c3SYuval Mintz 		}
2033dbb799c3SYuval Mintz 	}
2034dbb799c3SYuval Mintz 
2035dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt = p_virt;
2036dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys = p_phys;
2037dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].size =
2038dbb799c3SYuval Mintz 	    p_blk->real_size_in_page;
2039dbb799c3SYuval Mintz 
2040dbb799c3SYuval Mintz 	/* compute absolute offset */
2041dbb799c3SYuval Mintz 	reg_offset = PSWRQ2_REG_ILT_MEMORY +
2042dbb799c3SYuval Mintz 	    (line * ILT_REG_SIZE_IN_BYTES * ILT_ENTRY_IN_REGS);
2043dbb799c3SYuval Mintz 
2044dbb799c3SYuval Mintz 	ilt_hw_entry = 0;
2045dbb799c3SYuval Mintz 	SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
2046dbb799c3SYuval Mintz 	SET_FIELD(ilt_hw_entry,
2047dbb799c3SYuval Mintz 		  ILT_ENTRY_PHY_ADDR,
2048dbb799c3SYuval Mintz 		  (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys >> 12));
2049dbb799c3SYuval Mintz 
2050dbb799c3SYuval Mintz 	/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a wide-bus */
2051dbb799c3SYuval Mintz 	qed_dmae_host2grc(p_hwfn, p_ptt, (u64) (uintptr_t)&ilt_hw_entry,
2052dbb799c3SYuval Mintz 			  reg_offset, sizeof(ilt_hw_entry) / sizeof(u32), 0);
2053dbb799c3SYuval Mintz 
2054dbb799c3SYuval Mintz 	if (elem_type == QED_ELEM_CXT) {
2055dbb799c3SYuval Mintz 		u32 last_cid_allocated = (1 + (iid / elems_per_p)) *
2056dbb799c3SYuval Mintz 		    elems_per_p;
2057dbb799c3SYuval Mintz 
2058dbb799c3SYuval Mintz 		/* Update the relevant register in the parser */
2059dbb799c3SYuval Mintz 		qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF,
2060dbb799c3SYuval Mintz 		       last_cid_allocated - 1);
2061dbb799c3SYuval Mintz 
2062dbb799c3SYuval Mintz 		if (!p_hwfn->b_rdma_enabled_in_prs) {
2063dbb799c3SYuval Mintz 			/* Enable RoCE search */
2064dbb799c3SYuval Mintz 			qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
2065dbb799c3SYuval Mintz 			p_hwfn->b_rdma_enabled_in_prs = true;
2066dbb799c3SYuval Mintz 		}
2067dbb799c3SYuval Mintz 	}
2068dbb799c3SYuval Mintz 
2069dbb799c3SYuval Mintz out1:
2070dbb799c3SYuval Mintz 	qed_ptt_release(p_hwfn, p_ptt);
2071dbb799c3SYuval Mintz out0:
2072dbb799c3SYuval Mintz 	mutex_unlock(&p_hwfn->p_cxt_mngr->mutex);
2073dbb799c3SYuval Mintz 
2074dbb799c3SYuval Mintz 	return rc;
2075dbb799c3SYuval Mintz }
2076dbb799c3SYuval Mintz 
2077dbb799c3SYuval Mintz /* This function is very RoCE oriented, if another protocol in the future
2078dbb799c3SYuval Mintz  * will want this feature we'll need to modify the function to be more generic
2079dbb799c3SYuval Mintz  */
2080dbb799c3SYuval Mintz static int
2081dbb799c3SYuval Mintz qed_cxt_free_ilt_range(struct qed_hwfn *p_hwfn,
2082dbb799c3SYuval Mintz 		       enum qed_cxt_elem_type elem_type,
2083dbb799c3SYuval Mintz 		       u32 start_iid, u32 count)
2084dbb799c3SYuval Mintz {
2085dbb799c3SYuval Mintz 	u32 start_line, end_line, shadow_start_line, shadow_end_line;
2086dbb799c3SYuval Mintz 	u32 reg_offset, elem_size, hw_p_size, elems_per_p;
2087dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2088dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
2089dbb799c3SYuval Mintz 	u32 end_iid = start_iid + count;
2090dbb799c3SYuval Mintz 	struct qed_ptt *p_ptt;
2091dbb799c3SYuval Mintz 	u64 ilt_hw_entry = 0;
2092dbb799c3SYuval Mintz 	u32 i;
2093dbb799c3SYuval Mintz 
2094dbb799c3SYuval Mintz 	switch (elem_type) {
2095dbb799c3SYuval Mintz 	case QED_ELEM_CXT:
2096dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
2097dbb799c3SYuval Mintz 		elem_size = CONN_CXT_SIZE(p_hwfn);
2098dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUC_BLK];
2099dbb799c3SYuval Mintz 		break;
2100dbb799c3SYuval Mintz 	case QED_ELEM_SRQ:
2101dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
2102dbb799c3SYuval Mintz 		elem_size = SRQ_CXT_SIZE;
2103dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
2104dbb799c3SYuval Mintz 		break;
2105dbb799c3SYuval Mintz 	case QED_ELEM_TASK:
2106dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
2107dbb799c3SYuval Mintz 		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
2108dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
2109dbb799c3SYuval Mintz 		break;
2110dbb799c3SYuval Mintz 	default:
2111dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
2112dbb799c3SYuval Mintz 		return -EINVAL;
2113dbb799c3SYuval Mintz 	}
2114dbb799c3SYuval Mintz 
2115dbb799c3SYuval Mintz 	/* Calculate line in ilt */
2116dbb799c3SYuval Mintz 	hw_p_size = p_cli->p_size.val;
2117dbb799c3SYuval Mintz 	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
2118dbb799c3SYuval Mintz 	start_line = p_blk->start_line + (start_iid / elems_per_p);
2119dbb799c3SYuval Mintz 	end_line = p_blk->start_line + (end_iid / elems_per_p);
2120dbb799c3SYuval Mintz 	if (((end_iid + 1) / elems_per_p) != (end_iid / elems_per_p))
2121dbb799c3SYuval Mintz 		end_line--;
2122dbb799c3SYuval Mintz 
2123dbb799c3SYuval Mintz 	shadow_start_line = start_line - p_hwfn->p_cxt_mngr->pf_start_line;
2124dbb799c3SYuval Mintz 	shadow_end_line = end_line - p_hwfn->p_cxt_mngr->pf_start_line;
2125dbb799c3SYuval Mintz 
2126dbb799c3SYuval Mintz 	p_ptt = qed_ptt_acquire(p_hwfn);
2127dbb799c3SYuval Mintz 	if (!p_ptt) {
2128dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn,
2129dbb799c3SYuval Mintz 			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
2130dbb799c3SYuval Mintz 		return -EBUSY;
2131dbb799c3SYuval Mintz 	}
2132dbb799c3SYuval Mintz 
2133dbb799c3SYuval Mintz 	for (i = shadow_start_line; i < shadow_end_line; i++) {
2134dbb799c3SYuval Mintz 		if (!p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt)
2135dbb799c3SYuval Mintz 			continue;
2136dbb799c3SYuval Mintz 
2137dbb799c3SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2138dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].size,
2139dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt,
2140dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys);
2141dbb799c3SYuval Mintz 
2142dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt = NULL;
2143dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys = 0;
2144dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].size = 0;
2145dbb799c3SYuval Mintz 
2146dbb799c3SYuval Mintz 		/* compute absolute offset */
2147dbb799c3SYuval Mintz 		reg_offset = PSWRQ2_REG_ILT_MEMORY +
2148dbb799c3SYuval Mintz 		    ((start_line++) * ILT_REG_SIZE_IN_BYTES *
2149dbb799c3SYuval Mintz 		     ILT_ENTRY_IN_REGS);
2150dbb799c3SYuval Mintz 
2151dbb799c3SYuval Mintz 		/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a
2152dbb799c3SYuval Mintz 		 * wide-bus.
2153dbb799c3SYuval Mintz 		 */
2154dbb799c3SYuval Mintz 		qed_dmae_host2grc(p_hwfn, p_ptt,
2155dbb799c3SYuval Mintz 				  (u64) (uintptr_t) &ilt_hw_entry,
2156dbb799c3SYuval Mintz 				  reg_offset,
2157dbb799c3SYuval Mintz 				  sizeof(ilt_hw_entry) / sizeof(u32),
2158dbb799c3SYuval Mintz 				  0);
2159dbb799c3SYuval Mintz 	}
2160dbb799c3SYuval Mintz 
2161dbb799c3SYuval Mintz 	qed_ptt_release(p_hwfn, p_ptt);
2162dbb799c3SYuval Mintz 
2163dbb799c3SYuval Mintz 	return 0;
2164dbb799c3SYuval Mintz }
2165dbb799c3SYuval Mintz 
2166dbb799c3SYuval Mintz int qed_cxt_free_proto_ilt(struct qed_hwfn *p_hwfn, enum protocol_type proto)
2167dbb799c3SYuval Mintz {
2168dbb799c3SYuval Mintz 	int rc;
2169dbb799c3SYuval Mintz 	u32 cid;
2170dbb799c3SYuval Mintz 
2171dbb799c3SYuval Mintz 	/* Free Connection CXT */
2172dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_CXT,
2173dbb799c3SYuval Mintz 				    qed_cxt_get_proto_cid_start(p_hwfn,
2174dbb799c3SYuval Mintz 								proto),
2175dbb799c3SYuval Mintz 				    qed_cxt_get_proto_cid_count(p_hwfn,
2176dbb799c3SYuval Mintz 								proto, &cid));
2177dbb799c3SYuval Mintz 
2178dbb799c3SYuval Mintz 	if (rc)
2179dbb799c3SYuval Mintz 		return rc;
2180dbb799c3SYuval Mintz 
2181dbb799c3SYuval Mintz 	/* Free Task CXT */
2182dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_TASK, 0,
2183dbb799c3SYuval Mintz 				    qed_cxt_get_proto_tid_count(p_hwfn, proto));
2184dbb799c3SYuval Mintz 	if (rc)
2185dbb799c3SYuval Mintz 		return rc;
2186dbb799c3SYuval Mintz 
2187dbb799c3SYuval Mintz 	/* Free TSDM CXT */
2188dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_SRQ, 0,
2189dbb799c3SYuval Mintz 				    qed_cxt_get_srq_count(p_hwfn));
2190dbb799c3SYuval Mintz 
2191dbb799c3SYuval Mintz 	return rc;
2192dbb799c3SYuval Mintz }
2193dbb799c3SYuval Mintz 
2194dbb799c3SYuval Mintz int qed_cxt_get_task_ctx(struct qed_hwfn *p_hwfn,
2195dbb799c3SYuval Mintz 			 u32 tid, u8 ctx_type, void **pp_task_ctx)
2196dbb799c3SYuval Mintz {
2197dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2198dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2199dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_seg;
2200dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg_info;
2201dbb799c3SYuval Mintz 	u32 proto, seg;
2202dbb799c3SYuval Mintz 	u32 total_lines;
2203dbb799c3SYuval Mintz 	u32 tid_size, ilt_idx;
2204dbb799c3SYuval Mintz 	u32 num_tids_per_block;
2205dbb799c3SYuval Mintz 
2206dbb799c3SYuval Mintz 	/* Verify the personality */
2207dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
2208dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
2209dbb799c3SYuval Mintz 		proto = PROTOCOLID_ISCSI;
2210dbb799c3SYuval Mintz 		seg = QED_CXT_ISCSI_TID_SEG;
2211dbb799c3SYuval Mintz 		break;
2212dbb799c3SYuval Mintz 	default:
2213dbb799c3SYuval Mintz 		return -EINVAL;
2214dbb799c3SYuval Mintz 	}
2215dbb799c3SYuval Mintz 
2216dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
2217dbb799c3SYuval Mintz 	if (!p_cli->active)
2218dbb799c3SYuval Mintz 		return -EINVAL;
2219dbb799c3SYuval Mintz 
2220dbb799c3SYuval Mintz 	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
2221dbb799c3SYuval Mintz 
2222dbb799c3SYuval Mintz 	if (ctx_type == QED_CTX_WORKING_MEM) {
2223dbb799c3SYuval Mintz 		p_seg = &p_cli->pf_blks[CDUT_SEG_BLK(seg)];
2224dbb799c3SYuval Mintz 	} else if (ctx_type == QED_CTX_FL_MEM) {
2225dbb799c3SYuval Mintz 		if (!p_seg_info->has_fl_mem)
2226dbb799c3SYuval Mintz 			return -EINVAL;
2227dbb799c3SYuval Mintz 		p_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
2228dbb799c3SYuval Mintz 	} else {
2229dbb799c3SYuval Mintz 		return -EINVAL;
2230dbb799c3SYuval Mintz 	}
2231dbb799c3SYuval Mintz 	total_lines = DIV_ROUND_UP(p_seg->total_size, p_seg->real_size_in_page);
2232dbb799c3SYuval Mintz 	tid_size = p_mngr->task_type_size[p_seg_info->type];
2233dbb799c3SYuval Mintz 	num_tids_per_block = p_seg->real_size_in_page / tid_size;
2234dbb799c3SYuval Mintz 
2235dbb799c3SYuval Mintz 	if (total_lines < tid / num_tids_per_block)
2236dbb799c3SYuval Mintz 		return -EINVAL;
2237dbb799c3SYuval Mintz 
2238dbb799c3SYuval Mintz 	ilt_idx = tid / num_tids_per_block + p_seg->start_line -
2239dbb799c3SYuval Mintz 		  p_mngr->pf_start_line;
2240dbb799c3SYuval Mintz 	*pp_task_ctx = (u8 *)p_mngr->ilt_shadow[ilt_idx].p_virt +
2241dbb799c3SYuval Mintz 		       (tid % num_tids_per_block) * tid_size;
2242fe56b9e6SYuval Mintz 
2243fe56b9e6SYuval Mintz 	return 0;
2244fe56b9e6SYuval Mintz }
2245