xref: /linux/drivers/net/ethernet/qlogic/qed/qed_cxt.c (revision 5f8cb033f4c814760c9f7f2345278111d06afe47)
1fe56b9e6SYuval Mintz /* QLogic qed NIC Driver
2e8f1cb50SMintz, Yuval  * Copyright (c) 2015-2017  QLogic Corporation
3fe56b9e6SYuval Mintz  *
4e8f1cb50SMintz, Yuval  * This software is available to you under a choice of one of two
5e8f1cb50SMintz, Yuval  * licenses.  You may choose to be licensed under the terms of the GNU
6e8f1cb50SMintz, Yuval  * General Public License (GPL) Version 2, available from the file
7e8f1cb50SMintz, Yuval  * COPYING in the main directory of this source tree, or the
8e8f1cb50SMintz, Yuval  * OpenIB.org BSD license below:
9e8f1cb50SMintz, Yuval  *
10e8f1cb50SMintz, Yuval  *     Redistribution and use in source and binary forms, with or
11e8f1cb50SMintz, Yuval  *     without modification, are permitted provided that the following
12e8f1cb50SMintz, Yuval  *     conditions are met:
13e8f1cb50SMintz, Yuval  *
14e8f1cb50SMintz, Yuval  *      - Redistributions of source code must retain the above
15e8f1cb50SMintz, Yuval  *        copyright notice, this list of conditions and the following
16e8f1cb50SMintz, Yuval  *        disclaimer.
17e8f1cb50SMintz, Yuval  *
18e8f1cb50SMintz, Yuval  *      - Redistributions in binary form must reproduce the above
19e8f1cb50SMintz, Yuval  *        copyright notice, this list of conditions and the following
20e8f1cb50SMintz, Yuval  *        disclaimer in the documentation and /or other materials
21e8f1cb50SMintz, Yuval  *        provided with the distribution.
22e8f1cb50SMintz, Yuval  *
23e8f1cb50SMintz, Yuval  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e8f1cb50SMintz, Yuval  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e8f1cb50SMintz, Yuval  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e8f1cb50SMintz, Yuval  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e8f1cb50SMintz, Yuval  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e8f1cb50SMintz, Yuval  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e8f1cb50SMintz, Yuval  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e8f1cb50SMintz, Yuval  * SOFTWARE.
31fe56b9e6SYuval Mintz  */
32fe56b9e6SYuval Mintz 
33fe56b9e6SYuval Mintz #include <linux/types.h>
34fe56b9e6SYuval Mintz #include <linux/bitops.h>
35fe56b9e6SYuval Mintz #include <linux/dma-mapping.h>
36fe56b9e6SYuval Mintz #include <linux/errno.h>
37fe56b9e6SYuval Mintz #include <linux/kernel.h>
38fe56b9e6SYuval Mintz #include <linux/list.h>
39fe56b9e6SYuval Mintz #include <linux/log2.h>
40fe56b9e6SYuval Mintz #include <linux/pci.h>
41fe56b9e6SYuval Mintz #include <linux/slab.h>
42fe56b9e6SYuval Mintz #include <linux/string.h>
43fe56b9e6SYuval Mintz #include <linux/bitops.h>
44fe56b9e6SYuval Mintz #include "qed.h"
45fe56b9e6SYuval Mintz #include "qed_cxt.h"
46fe56b9e6SYuval Mintz #include "qed_dev_api.h"
47fe56b9e6SYuval Mintz #include "qed_hsi.h"
48fe56b9e6SYuval Mintz #include "qed_hw.h"
49fe56b9e6SYuval Mintz #include "qed_init_ops.h"
50fe56b9e6SYuval Mintz #include "qed_reg_addr.h"
511408cc1fSYuval Mintz #include "qed_sriov.h"
52fe56b9e6SYuval Mintz 
53fe56b9e6SYuval Mintz /* Max number of connection types in HW (DQ/CDU etc.) */
54fe56b9e6SYuval Mintz #define MAX_CONN_TYPES		PROTOCOLID_COMMON
55fe56b9e6SYuval Mintz #define NUM_TASK_TYPES		2
56fe56b9e6SYuval Mintz #define NUM_TASK_PF_SEGMENTS	4
571408cc1fSYuval Mintz #define NUM_TASK_VF_SEGMENTS	1
58fe56b9e6SYuval Mintz 
59fe56b9e6SYuval Mintz /* QM constants */
60fe56b9e6SYuval Mintz #define QM_PQ_ELEMENT_SIZE	4 /* in bytes */
61fe56b9e6SYuval Mintz 
62fe56b9e6SYuval Mintz /* Doorbell-Queue constants */
63fe56b9e6SYuval Mintz #define DQ_RANGE_SHIFT		4
64fe56b9e6SYuval Mintz #define DQ_RANGE_ALIGN		BIT(DQ_RANGE_SHIFT)
65fe56b9e6SYuval Mintz 
66dbb799c3SYuval Mintz /* Searcher constants */
67dbb799c3SYuval Mintz #define SRC_MIN_NUM_ELEMS 256
68dbb799c3SYuval Mintz 
69dbb799c3SYuval Mintz /* Timers constants */
70dbb799c3SYuval Mintz #define TM_SHIFT        7
71dbb799c3SYuval Mintz #define TM_ALIGN        BIT(TM_SHIFT)
72dbb799c3SYuval Mintz #define TM_ELEM_SIZE    4
73dbb799c3SYuval Mintz 
74be086e7cSMintz, Yuval #define ILT_DEFAULT_HW_P_SIZE	4
7551ff1725SRam Amrani 
76fe56b9e6SYuval Mintz #define ILT_PAGE_IN_BYTES(hw_p_size)	(1U << ((hw_p_size) + 12))
77fe56b9e6SYuval Mintz #define ILT_CFG_REG(cli, reg)	PSWRQ2_REG_ ## cli ## _ ## reg ## _RT_OFFSET
78fe56b9e6SYuval Mintz 
79fe56b9e6SYuval Mintz /* ILT entry structure */
80fe56b9e6SYuval Mintz #define ILT_ENTRY_PHY_ADDR_MASK		0x000FFFFFFFFFFFULL
81fe56b9e6SYuval Mintz #define ILT_ENTRY_PHY_ADDR_SHIFT	0
82fe56b9e6SYuval Mintz #define ILT_ENTRY_VALID_MASK		0x1ULL
83fe56b9e6SYuval Mintz #define ILT_ENTRY_VALID_SHIFT		52
84fe56b9e6SYuval Mintz #define ILT_ENTRY_IN_REGS		2
85fe56b9e6SYuval Mintz #define ILT_REG_SIZE_IN_BYTES		4
86fe56b9e6SYuval Mintz 
87fe56b9e6SYuval Mintz /* connection context union */
88fe56b9e6SYuval Mintz union conn_context {
89fe56b9e6SYuval Mintz 	struct core_conn_context core_ctx;
90fe56b9e6SYuval Mintz 	struct eth_conn_context eth_ctx;
91dbb799c3SYuval Mintz 	struct iscsi_conn_context iscsi_ctx;
921e128c81SArun Easi 	struct fcoe_conn_context fcoe_ctx;
93dbb799c3SYuval Mintz 	struct roce_conn_context roce_ctx;
94fe56b9e6SYuval Mintz };
95fe56b9e6SYuval Mintz 
961e128c81SArun Easi /* TYPE-0 task context - iSCSI, FCOE */
97dbb799c3SYuval Mintz union type0_task_context {
98dbb799c3SYuval Mintz 	struct iscsi_task_context iscsi_ctx;
991e128c81SArun Easi 	struct fcoe_task_context fcoe_ctx;
100dbb799c3SYuval Mintz };
101dbb799c3SYuval Mintz 
102dbb799c3SYuval Mintz /* TYPE-1 task context - ROCE */
103dbb799c3SYuval Mintz union type1_task_context {
104dbb799c3SYuval Mintz 	struct rdma_task_context roce_ctx;
105dbb799c3SYuval Mintz };
106dbb799c3SYuval Mintz 
107dbb799c3SYuval Mintz struct src_ent {
108dbb799c3SYuval Mintz 	u8 opaque[56];
109dbb799c3SYuval Mintz 	u64 next;
110dbb799c3SYuval Mintz };
111dbb799c3SYuval Mintz 
112dbb799c3SYuval Mintz #define CDUT_SEG_ALIGNMET 3	/* in 4k chunks */
113dbb799c3SYuval Mintz #define CDUT_SEG_ALIGNMET_IN_BYTES (1 << (CDUT_SEG_ALIGNMET + 12))
114dbb799c3SYuval Mintz 
115fe56b9e6SYuval Mintz #define CONN_CXT_SIZE(p_hwfn) \
116fe56b9e6SYuval Mintz 	ALIGNED_TYPE_SIZE(union conn_context, p_hwfn)
117fe56b9e6SYuval Mintz 
118dbb799c3SYuval Mintz #define SRQ_CXT_SIZE (sizeof(struct rdma_srq_context))
119dbb799c3SYuval Mintz 
120dbb799c3SYuval Mintz #define TYPE0_TASK_CXT_SIZE(p_hwfn) \
121dbb799c3SYuval Mintz 	ALIGNED_TYPE_SIZE(union type0_task_context, p_hwfn)
122dbb799c3SYuval Mintz 
123dbb799c3SYuval Mintz /* Alignment is inherent to the type1_task_context structure */
124dbb799c3SYuval Mintz #define TYPE1_TASK_CXT_SIZE(p_hwfn) sizeof(union type1_task_context)
125dbb799c3SYuval Mintz 
126fe56b9e6SYuval Mintz /* PF per protocl configuration object */
127dbb799c3SYuval Mintz #define TASK_SEGMENTS   (NUM_TASK_PF_SEGMENTS + NUM_TASK_VF_SEGMENTS)
128dbb799c3SYuval Mintz #define TASK_SEGMENT_VF (NUM_TASK_PF_SEGMENTS)
129dbb799c3SYuval Mintz 
130dbb799c3SYuval Mintz struct qed_tid_seg {
131dbb799c3SYuval Mintz 	u32 count;
132dbb799c3SYuval Mintz 	u8 type;
133dbb799c3SYuval Mintz 	bool has_fl_mem;
134dbb799c3SYuval Mintz };
135dbb799c3SYuval Mintz 
136fe56b9e6SYuval Mintz struct qed_conn_type_cfg {
137fe56b9e6SYuval Mintz 	u32 cid_count;
138fe56b9e6SYuval Mintz 	u32 cid_start;
1391408cc1fSYuval Mintz 	u32 cids_per_vf;
140dbb799c3SYuval Mintz 	struct qed_tid_seg tid_seg[TASK_SEGMENTS];
141fe56b9e6SYuval Mintz };
142fe56b9e6SYuval Mintz 
143fe56b9e6SYuval Mintz /* ILT Client configuration, Per connection type (protocol) resources. */
144fe56b9e6SYuval Mintz #define ILT_CLI_PF_BLOCKS	(1 + NUM_TASK_PF_SEGMENTS * 2)
1451408cc1fSYuval Mintz #define ILT_CLI_VF_BLOCKS       (1 + NUM_TASK_VF_SEGMENTS * 2)
146fe56b9e6SYuval Mintz #define CDUC_BLK		(0)
147dbb799c3SYuval Mintz #define SRQ_BLK                 (0)
148dbb799c3SYuval Mintz #define CDUT_SEG_BLK(n)         (1 + (u8)(n))
149dbb799c3SYuval Mintz #define CDUT_FL_SEG_BLK(n, X)   (1 + (n) + NUM_TASK_ ## X ## _SEGMENTS)
150fe56b9e6SYuval Mintz 
151fe56b9e6SYuval Mintz enum ilt_clients {
152fe56b9e6SYuval Mintz 	ILT_CLI_CDUC,
153dbb799c3SYuval Mintz 	ILT_CLI_CDUT,
154fe56b9e6SYuval Mintz 	ILT_CLI_QM,
155dbb799c3SYuval Mintz 	ILT_CLI_TM,
156dbb799c3SYuval Mintz 	ILT_CLI_SRC,
157dbb799c3SYuval Mintz 	ILT_CLI_TSDM,
158fe56b9e6SYuval Mintz 	ILT_CLI_MAX
159fe56b9e6SYuval Mintz };
160fe56b9e6SYuval Mintz 
161fe56b9e6SYuval Mintz struct ilt_cfg_pair {
162fe56b9e6SYuval Mintz 	u32 reg;
163fe56b9e6SYuval Mintz 	u32 val;
164fe56b9e6SYuval Mintz };
165fe56b9e6SYuval Mintz 
166fe56b9e6SYuval Mintz struct qed_ilt_cli_blk {
167fe56b9e6SYuval Mintz 	u32 total_size; /* 0 means not active */
168fe56b9e6SYuval Mintz 	u32 real_size_in_page;
169fe56b9e6SYuval Mintz 	u32 start_line;
170dbb799c3SYuval Mintz 	u32 dynamic_line_cnt;
171fe56b9e6SYuval Mintz };
172fe56b9e6SYuval Mintz 
173fe56b9e6SYuval Mintz struct qed_ilt_client_cfg {
174fe56b9e6SYuval Mintz 	bool active;
175fe56b9e6SYuval Mintz 
176fe56b9e6SYuval Mintz 	/* ILT boundaries */
177fe56b9e6SYuval Mintz 	struct ilt_cfg_pair first;
178fe56b9e6SYuval Mintz 	struct ilt_cfg_pair last;
179fe56b9e6SYuval Mintz 	struct ilt_cfg_pair p_size;
180fe56b9e6SYuval Mintz 
181fe56b9e6SYuval Mintz 	/* ILT client blocks for PF */
182fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk pf_blks[ILT_CLI_PF_BLOCKS];
183fe56b9e6SYuval Mintz 	u32 pf_total_lines;
1841408cc1fSYuval Mintz 
1851408cc1fSYuval Mintz 	/* ILT client blocks for VFs */
1861408cc1fSYuval Mintz 	struct qed_ilt_cli_blk vf_blks[ILT_CLI_VF_BLOCKS];
1871408cc1fSYuval Mintz 	u32 vf_total_lines;
188fe56b9e6SYuval Mintz };
189fe56b9e6SYuval Mintz 
190fe56b9e6SYuval Mintz /* Per Path -
191fe56b9e6SYuval Mintz  *      ILT shadow table
192fe56b9e6SYuval Mintz  *      Protocol acquired CID lists
193fe56b9e6SYuval Mintz  *      PF start line in ILT
194fe56b9e6SYuval Mintz  */
195fe56b9e6SYuval Mintz struct qed_dma_mem {
196fe56b9e6SYuval Mintz 	dma_addr_t p_phys;
197fe56b9e6SYuval Mintz 	void *p_virt;
198fe56b9e6SYuval Mintz 	size_t size;
199fe56b9e6SYuval Mintz };
200fe56b9e6SYuval Mintz 
201fe56b9e6SYuval Mintz struct qed_cid_acquired_map {
202fe56b9e6SYuval Mintz 	u32		start_cid;
203fe56b9e6SYuval Mintz 	u32		max_count;
204fe56b9e6SYuval Mintz 	unsigned long	*cid_map;
205fe56b9e6SYuval Mintz };
206fe56b9e6SYuval Mintz 
207fe56b9e6SYuval Mintz struct qed_cxt_mngr {
208fe56b9e6SYuval Mintz 	/* Per protocl configuration */
209fe56b9e6SYuval Mintz 	struct qed_conn_type_cfg	conn_cfg[MAX_CONN_TYPES];
210fe56b9e6SYuval Mintz 
211fe56b9e6SYuval Mintz 	/* computed ILT structure */
212fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg	clients[ILT_CLI_MAX];
213fe56b9e6SYuval Mintz 
214dbb799c3SYuval Mintz 	/* Task type sizes */
215dbb799c3SYuval Mintz 	u32 task_type_size[NUM_TASK_TYPES];
216dbb799c3SYuval Mintz 
2171408cc1fSYuval Mintz 	/* total number of VFs for this hwfn -
2181408cc1fSYuval Mintz 	 * ALL VFs are symmetric in terms of HW resources
2191408cc1fSYuval Mintz 	 */
2201408cc1fSYuval Mintz 	u32				vf_count;
2211408cc1fSYuval Mintz 
222dbb799c3SYuval Mintz 	/* total number of SRQ's for this hwfn */
223dbb799c3SYuval Mintz 	u32 srq_count;
224dbb799c3SYuval Mintz 
225fe56b9e6SYuval Mintz 	/* Acquired CIDs */
226fe56b9e6SYuval Mintz 	struct qed_cid_acquired_map	acquired[MAX_CONN_TYPES];
227fe56b9e6SYuval Mintz 
228fe56b9e6SYuval Mintz 	/* ILT  shadow table */
229fe56b9e6SYuval Mintz 	struct qed_dma_mem		*ilt_shadow;
230fe56b9e6SYuval Mintz 	u32				pf_start_line;
231dbb799c3SYuval Mintz 
232dbb799c3SYuval Mintz 	/* Mutex for a dynamic ILT allocation */
233dbb799c3SYuval Mintz 	struct mutex mutex;
234dbb799c3SYuval Mintz 
235dbb799c3SYuval Mintz 	/* SRC T2 */
236dbb799c3SYuval Mintz 	struct qed_dma_mem *t2;
237dbb799c3SYuval Mintz 	u32 t2_num_pages;
238dbb799c3SYuval Mintz 	u64 first_free;
239dbb799c3SYuval Mintz 	u64 last_free;
240fe56b9e6SYuval Mintz };
241dbb799c3SYuval Mintz static bool src_proto(enum protocol_type type)
242dbb799c3SYuval Mintz {
243dbb799c3SYuval Mintz 	return type == PROTOCOLID_ISCSI ||
244*5f8cb033SMintz, Yuval 	       type == PROTOCOLID_FCOE;
245dbb799c3SYuval Mintz }
246dbb799c3SYuval Mintz 
247dbb799c3SYuval Mintz static bool tm_cid_proto(enum protocol_type type)
248dbb799c3SYuval Mintz {
249dbb799c3SYuval Mintz 	return type == PROTOCOLID_ISCSI ||
2501e128c81SArun Easi 	       type == PROTOCOLID_FCOE ||
251dbb799c3SYuval Mintz 	       type == PROTOCOLID_ROCE;
252dbb799c3SYuval Mintz }
253fe56b9e6SYuval Mintz 
2541e128c81SArun Easi static bool tm_tid_proto(enum protocol_type type)
2551e128c81SArun Easi {
2561e128c81SArun Easi 	return type == PROTOCOLID_FCOE;
2571e128c81SArun Easi }
2581e128c81SArun Easi 
2591408cc1fSYuval Mintz /* counts the iids for the CDU/CDUC ILT client configuration */
2601408cc1fSYuval Mintz struct qed_cdu_iids {
2611408cc1fSYuval Mintz 	u32 pf_cids;
2621408cc1fSYuval Mintz 	u32 per_vf_cids;
2631408cc1fSYuval Mintz };
2641408cc1fSYuval Mintz 
2651408cc1fSYuval Mintz static void qed_cxt_cdu_iids(struct qed_cxt_mngr *p_mngr,
2661408cc1fSYuval Mintz 			     struct qed_cdu_iids *iids)
267fe56b9e6SYuval Mintz {
2681408cc1fSYuval Mintz 	u32 type;
269fe56b9e6SYuval Mintz 
2701408cc1fSYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
2711408cc1fSYuval Mintz 		iids->pf_cids += p_mngr->conn_cfg[type].cid_count;
2721408cc1fSYuval Mintz 		iids->per_vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
2731408cc1fSYuval Mintz 	}
274fe56b9e6SYuval Mintz }
275fe56b9e6SYuval Mintz 
276dbb799c3SYuval Mintz /* counts the iids for the Searcher block configuration */
277dbb799c3SYuval Mintz struct qed_src_iids {
278dbb799c3SYuval Mintz 	u32 pf_cids;
279dbb799c3SYuval Mintz 	u32 per_vf_cids;
280dbb799c3SYuval Mintz };
281dbb799c3SYuval Mintz 
282dbb799c3SYuval Mintz static void qed_cxt_src_iids(struct qed_cxt_mngr *p_mngr,
283dbb799c3SYuval Mintz 			     struct qed_src_iids *iids)
284dbb799c3SYuval Mintz {
285dbb799c3SYuval Mintz 	u32 i;
286dbb799c3SYuval Mintz 
287dbb799c3SYuval Mintz 	for (i = 0; i < MAX_CONN_TYPES; i++) {
288dbb799c3SYuval Mintz 		if (!src_proto(i))
289dbb799c3SYuval Mintz 			continue;
290dbb799c3SYuval Mintz 
291dbb799c3SYuval Mintz 		iids->pf_cids += p_mngr->conn_cfg[i].cid_count;
292dbb799c3SYuval Mintz 		iids->per_vf_cids += p_mngr->conn_cfg[i].cids_per_vf;
293dbb799c3SYuval Mintz 	}
294dbb799c3SYuval Mintz }
295dbb799c3SYuval Mintz 
296dbb799c3SYuval Mintz /* counts the iids for the Timers block configuration */
297dbb799c3SYuval Mintz struct qed_tm_iids {
298dbb799c3SYuval Mintz 	u32 pf_cids;
299dbb799c3SYuval Mintz 	u32 pf_tids[NUM_TASK_PF_SEGMENTS];	/* per segment */
300dbb799c3SYuval Mintz 	u32 pf_tids_total;
301dbb799c3SYuval Mintz 	u32 per_vf_cids;
302dbb799c3SYuval Mintz 	u32 per_vf_tids;
303dbb799c3SYuval Mintz };
304dbb799c3SYuval Mintz 
30544531ba4SMichal Kalderon static void qed_cxt_tm_iids(struct qed_hwfn *p_hwfn,
30644531ba4SMichal Kalderon 			    struct qed_cxt_mngr *p_mngr,
307dbb799c3SYuval Mintz 			    struct qed_tm_iids *iids)
308dbb799c3SYuval Mintz {
30944531ba4SMichal Kalderon 	bool tm_vf_required = false;
31044531ba4SMichal Kalderon 	bool tm_required = false;
31144531ba4SMichal Kalderon 	int i, j;
312dbb799c3SYuval Mintz 
31344531ba4SMichal Kalderon 	/* Timers is a special case -> we don't count how many cids require
31444531ba4SMichal Kalderon 	 * timers but what's the max cid that will be used by the timer block.
31544531ba4SMichal Kalderon 	 * therefore we traverse in reverse order, and once we hit a protocol
31644531ba4SMichal Kalderon 	 * that requires the timers memory, we'll sum all the protocols up
31744531ba4SMichal Kalderon 	 * to that one.
31844531ba4SMichal Kalderon 	 */
31944531ba4SMichal Kalderon 	for (i = MAX_CONN_TYPES - 1; i >= 0; i--) {
320dbb799c3SYuval Mintz 		struct qed_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[i];
321dbb799c3SYuval Mintz 
32244531ba4SMichal Kalderon 		if (tm_cid_proto(i) || tm_required) {
32344531ba4SMichal Kalderon 			if (p_cfg->cid_count)
32444531ba4SMichal Kalderon 				tm_required = true;
32544531ba4SMichal Kalderon 
326dbb799c3SYuval Mintz 			iids->pf_cids += p_cfg->cid_count;
32744531ba4SMichal Kalderon 		}
32844531ba4SMichal Kalderon 
32944531ba4SMichal Kalderon 		if (tm_cid_proto(i) || tm_vf_required) {
33044531ba4SMichal Kalderon 			if (p_cfg->cids_per_vf)
33144531ba4SMichal Kalderon 				tm_vf_required = true;
33244531ba4SMichal Kalderon 
333dbb799c3SYuval Mintz 			iids->per_vf_cids += p_cfg->cids_per_vf;
334dbb799c3SYuval Mintz 		}
3351e128c81SArun Easi 
3361e128c81SArun Easi 		if (tm_tid_proto(i)) {
3371e128c81SArun Easi 			struct qed_tid_seg *segs = p_cfg->tid_seg;
3381e128c81SArun Easi 
3391e128c81SArun Easi 			/* for each segment there is at most one
3401e128c81SArun Easi 			 * protocol for which count is not 0.
3411e128c81SArun Easi 			 */
3421e128c81SArun Easi 			for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
3431e128c81SArun Easi 				iids->pf_tids[j] += segs[j].count;
3441e128c81SArun Easi 
3451e128c81SArun Easi 			/* The last array elelment is for the VFs. As for PF
3461e128c81SArun Easi 			 * segments there can be only one protocol for
3471e128c81SArun Easi 			 * which this value is not 0.
3481e128c81SArun Easi 			 */
3491e128c81SArun Easi 			iids->per_vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
3501e128c81SArun Easi 		}
351dbb799c3SYuval Mintz 	}
352dbb799c3SYuval Mintz 
353dbb799c3SYuval Mintz 	iids->pf_cids = roundup(iids->pf_cids, TM_ALIGN);
354dbb799c3SYuval Mintz 	iids->per_vf_cids = roundup(iids->per_vf_cids, TM_ALIGN);
355dbb799c3SYuval Mintz 	iids->per_vf_tids = roundup(iids->per_vf_tids, TM_ALIGN);
356dbb799c3SYuval Mintz 
357dbb799c3SYuval Mintz 	for (iids->pf_tids_total = 0, j = 0; j < NUM_TASK_PF_SEGMENTS; j++) {
358dbb799c3SYuval Mintz 		iids->pf_tids[j] = roundup(iids->pf_tids[j], TM_ALIGN);
359dbb799c3SYuval Mintz 		iids->pf_tids_total += iids->pf_tids[j];
360dbb799c3SYuval Mintz 	}
361dbb799c3SYuval Mintz }
362dbb799c3SYuval Mintz 
363fe56b9e6SYuval Mintz static void qed_cxt_qm_iids(struct qed_hwfn *p_hwfn,
364fe56b9e6SYuval Mintz 			    struct qed_qm_iids *iids)
365fe56b9e6SYuval Mintz {
366fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
367dbb799c3SYuval Mintz 	struct qed_tid_seg *segs;
368dbb799c3SYuval Mintz 	u32 vf_cids = 0, type, j;
369dbb799c3SYuval Mintz 	u32 vf_tids = 0;
370fe56b9e6SYuval Mintz 
3711408cc1fSYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
372fe56b9e6SYuval Mintz 		iids->cids += p_mngr->conn_cfg[type].cid_count;
3731408cc1fSYuval Mintz 		vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
374dbb799c3SYuval Mintz 
375dbb799c3SYuval Mintz 		segs = p_mngr->conn_cfg[type].tid_seg;
376dbb799c3SYuval Mintz 		/* for each segment there is at most one
377dbb799c3SYuval Mintz 		 * protocol for which count is not 0.
378dbb799c3SYuval Mintz 		 */
379dbb799c3SYuval Mintz 		for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
380dbb799c3SYuval Mintz 			iids->tids += segs[j].count;
381dbb799c3SYuval Mintz 
382dbb799c3SYuval Mintz 		/* The last array elelment is for the VFs. As for PF
383dbb799c3SYuval Mintz 		 * segments there can be only one protocol for
384dbb799c3SYuval Mintz 		 * which this value is not 0.
385dbb799c3SYuval Mintz 		 */
386dbb799c3SYuval Mintz 		vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
3871408cc1fSYuval Mintz 	}
388fe56b9e6SYuval Mintz 
3891408cc1fSYuval Mintz 	iids->vf_cids += vf_cids * p_mngr->vf_count;
390dbb799c3SYuval Mintz 	iids->tids += vf_tids * p_mngr->vf_count;
391dbb799c3SYuval Mintz 
3921408cc1fSYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
393dbb799c3SYuval Mintz 		   "iids: CIDS %08x vf_cids %08x tids %08x vf_tids %08x\n",
394dbb799c3SYuval Mintz 		   iids->cids, iids->vf_cids, iids->tids, vf_tids);
395dbb799c3SYuval Mintz }
396dbb799c3SYuval Mintz 
397dbb799c3SYuval Mintz static struct qed_tid_seg *qed_cxt_tid_seg_info(struct qed_hwfn *p_hwfn,
398dbb799c3SYuval Mintz 						u32 seg)
399dbb799c3SYuval Mintz {
400dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_cfg = p_hwfn->p_cxt_mngr;
401dbb799c3SYuval Mintz 	u32 i;
402dbb799c3SYuval Mintz 
403dbb799c3SYuval Mintz 	/* Find the protocol with tid count > 0 for this segment.
404dbb799c3SYuval Mintz 	 * Note: there can only be one and this is already validated.
405dbb799c3SYuval Mintz 	 */
406dbb799c3SYuval Mintz 	for (i = 0; i < MAX_CONN_TYPES; i++)
407dbb799c3SYuval Mintz 		if (p_cfg->conn_cfg[i].tid_seg[seg].count)
408dbb799c3SYuval Mintz 			return &p_cfg->conn_cfg[i].tid_seg[seg];
409dbb799c3SYuval Mintz 	return NULL;
410dbb799c3SYuval Mintz }
411dbb799c3SYuval Mintz 
4128c93beafSYuval Mintz static void qed_cxt_set_srq_count(struct qed_hwfn *p_hwfn, u32 num_srqs)
413dbb799c3SYuval Mintz {
414dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
415dbb799c3SYuval Mintz 
416dbb799c3SYuval Mintz 	p_mgr->srq_count = num_srqs;
417dbb799c3SYuval Mintz }
418dbb799c3SYuval Mintz 
4198c93beafSYuval Mintz static u32 qed_cxt_get_srq_count(struct qed_hwfn *p_hwfn)
420dbb799c3SYuval Mintz {
421dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
422dbb799c3SYuval Mintz 
423dbb799c3SYuval Mintz 	return p_mgr->srq_count;
424fe56b9e6SYuval Mintz }
425fe56b9e6SYuval Mintz 
426fe56b9e6SYuval Mintz /* set the iids count per protocol */
427fe56b9e6SYuval Mintz static void qed_cxt_set_proto_cid_count(struct qed_hwfn *p_hwfn,
428fe56b9e6SYuval Mintz 					enum protocol_type type,
4291408cc1fSYuval Mintz 					u32 cid_count, u32 vf_cid_cnt)
430fe56b9e6SYuval Mintz {
431fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
432fe56b9e6SYuval Mintz 	struct qed_conn_type_cfg *p_conn = &p_mgr->conn_cfg[type];
433fe56b9e6SYuval Mintz 
434fe56b9e6SYuval Mintz 	p_conn->cid_count = roundup(cid_count, DQ_RANGE_ALIGN);
4351408cc1fSYuval Mintz 	p_conn->cids_per_vf = roundup(vf_cid_cnt, DQ_RANGE_ALIGN);
436dbb799c3SYuval Mintz 
437dbb799c3SYuval Mintz 	if (type == PROTOCOLID_ROCE) {
438dbb799c3SYuval Mintz 		u32 page_sz = p_mgr->clients[ILT_CLI_CDUC].p_size.val;
439dbb799c3SYuval Mintz 		u32 cxt_size = CONN_CXT_SIZE(p_hwfn);
440dbb799c3SYuval Mintz 		u32 elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
441f3e48119SRam Amrani 		u32 align = elems_per_page * DQ_RANGE_ALIGN;
442dbb799c3SYuval Mintz 
443f3e48119SRam Amrani 		p_conn->cid_count = roundup(p_conn->cid_count, align);
444dbb799c3SYuval Mintz 	}
4451408cc1fSYuval Mintz }
4461408cc1fSYuval Mintz 
4471408cc1fSYuval Mintz u32 qed_cxt_get_proto_cid_count(struct qed_hwfn *p_hwfn,
4481a635e48SYuval Mintz 				enum protocol_type type, u32 *vf_cid)
4491408cc1fSYuval Mintz {
4501408cc1fSYuval Mintz 	if (vf_cid)
4511408cc1fSYuval Mintz 		*vf_cid = p_hwfn->p_cxt_mngr->conn_cfg[type].cids_per_vf;
4521408cc1fSYuval Mintz 
4531408cc1fSYuval Mintz 	return p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
454fe56b9e6SYuval Mintz }
455fe56b9e6SYuval Mintz 
456dbb799c3SYuval Mintz u32 qed_cxt_get_proto_cid_start(struct qed_hwfn *p_hwfn,
457dbb799c3SYuval Mintz 				enum protocol_type type)
458dbb799c3SYuval Mintz {
459dbb799c3SYuval Mintz 	return p_hwfn->p_cxt_mngr->acquired[type].start_cid;
460dbb799c3SYuval Mintz }
461dbb799c3SYuval Mintz 
462dbb799c3SYuval Mintz u32 qed_cxt_get_proto_tid_count(struct qed_hwfn *p_hwfn,
463dbb799c3SYuval Mintz 				enum protocol_type type)
464dbb799c3SYuval Mintz {
465dbb799c3SYuval Mintz 	u32 cnt = 0;
466dbb799c3SYuval Mintz 	int i;
467dbb799c3SYuval Mintz 
468dbb799c3SYuval Mintz 	for (i = 0; i < TASK_SEGMENTS; i++)
469dbb799c3SYuval Mintz 		cnt += p_hwfn->p_cxt_mngr->conn_cfg[type].tid_seg[i].count;
470dbb799c3SYuval Mintz 
471dbb799c3SYuval Mintz 	return cnt;
472dbb799c3SYuval Mintz }
473dbb799c3SYuval Mintz 
4741a635e48SYuval Mintz static void qed_cxt_set_proto_tid_count(struct qed_hwfn *p_hwfn,
475dbb799c3SYuval Mintz 					enum protocol_type proto,
4761a635e48SYuval Mintz 					u8 seg,
4771a635e48SYuval Mintz 					u8 seg_type, u32 count, bool has_fl)
478dbb799c3SYuval Mintz {
479dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
480dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg = &p_mngr->conn_cfg[proto].tid_seg[seg];
481dbb799c3SYuval Mintz 
482dbb799c3SYuval Mintz 	p_seg->count = count;
483dbb799c3SYuval Mintz 	p_seg->has_fl_mem = has_fl;
484dbb799c3SYuval Mintz 	p_seg->type = seg_type;
485dbb799c3SYuval Mintz }
486dbb799c3SYuval Mintz 
487fe56b9e6SYuval Mintz static void qed_ilt_cli_blk_fill(struct qed_ilt_client_cfg *p_cli,
488fe56b9e6SYuval Mintz 				 struct qed_ilt_cli_blk *p_blk,
4891a635e48SYuval Mintz 				 u32 start_line, u32 total_size, u32 elem_size)
490fe56b9e6SYuval Mintz {
491fe56b9e6SYuval Mintz 	u32 ilt_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
492fe56b9e6SYuval Mintz 
493fe56b9e6SYuval Mintz 	/* verify thatits called only once for each block */
494fe56b9e6SYuval Mintz 	if (p_blk->total_size)
495fe56b9e6SYuval Mintz 		return;
496fe56b9e6SYuval Mintz 
497fe56b9e6SYuval Mintz 	p_blk->total_size = total_size;
498fe56b9e6SYuval Mintz 	p_blk->real_size_in_page = 0;
499fe56b9e6SYuval Mintz 	if (elem_size)
500fe56b9e6SYuval Mintz 		p_blk->real_size_in_page = (ilt_size / elem_size) * elem_size;
501fe56b9e6SYuval Mintz 	p_blk->start_line = start_line;
502fe56b9e6SYuval Mintz }
503fe56b9e6SYuval Mintz 
504fe56b9e6SYuval Mintz static void qed_ilt_cli_adv_line(struct qed_hwfn *p_hwfn,
505fe56b9e6SYuval Mintz 				 struct qed_ilt_client_cfg *p_cli,
506fe56b9e6SYuval Mintz 				 struct qed_ilt_cli_blk *p_blk,
507fe56b9e6SYuval Mintz 				 u32 *p_line, enum ilt_clients client_id)
508fe56b9e6SYuval Mintz {
509fe56b9e6SYuval Mintz 	if (!p_blk->total_size)
510fe56b9e6SYuval Mintz 		return;
511fe56b9e6SYuval Mintz 
512fe56b9e6SYuval Mintz 	if (!p_cli->active)
513fe56b9e6SYuval Mintz 		p_cli->first.val = *p_line;
514fe56b9e6SYuval Mintz 
515fe56b9e6SYuval Mintz 	p_cli->active = true;
5161a635e48SYuval Mintz 	*p_line += DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
517fe56b9e6SYuval Mintz 	p_cli->last.val = *p_line - 1;
518fe56b9e6SYuval Mintz 
519fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
520fe56b9e6SYuval Mintz 		   "ILT[Client %d] - Lines: [%08x - %08x]. Block - Size %08x [Real %08x] Start line %d\n",
521fe56b9e6SYuval Mintz 		   client_id, p_cli->first.val,
522fe56b9e6SYuval Mintz 		   p_cli->last.val, p_blk->total_size,
523fe56b9e6SYuval Mintz 		   p_blk->real_size_in_page, p_blk->start_line);
524fe56b9e6SYuval Mintz }
525fe56b9e6SYuval Mintz 
526dbb799c3SYuval Mintz static u32 qed_ilt_get_dynamic_line_cnt(struct qed_hwfn *p_hwfn,
527dbb799c3SYuval Mintz 					enum ilt_clients ilt_client)
528dbb799c3SYuval Mintz {
529dbb799c3SYuval Mintz 	u32 cid_count = p_hwfn->p_cxt_mngr->conn_cfg[PROTOCOLID_ROCE].cid_count;
530dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
531dbb799c3SYuval Mintz 	u32 lines_to_skip = 0;
532dbb799c3SYuval Mintz 	u32 cxts_per_p;
533dbb799c3SYuval Mintz 
534dbb799c3SYuval Mintz 	if (ilt_client == ILT_CLI_CDUC) {
535dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
536dbb799c3SYuval Mintz 
537dbb799c3SYuval Mintz 		cxts_per_p = ILT_PAGE_IN_BYTES(p_cli->p_size.val) /
538dbb799c3SYuval Mintz 		    (u32) CONN_CXT_SIZE(p_hwfn);
539dbb799c3SYuval Mintz 
540dbb799c3SYuval Mintz 		lines_to_skip = cid_count / cxts_per_p;
541dbb799c3SYuval Mintz 	}
542dbb799c3SYuval Mintz 
543dbb799c3SYuval Mintz 	return lines_to_skip;
544dbb799c3SYuval Mintz }
545dbb799c3SYuval Mintz 
546fe56b9e6SYuval Mintz int qed_cxt_cfg_ilt_compute(struct qed_hwfn *p_hwfn)
547fe56b9e6SYuval Mintz {
548fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
549dbb799c3SYuval Mintz 	u32 curr_line, total, i, task_size, line;
550fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
551fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
5521408cc1fSYuval Mintz 	struct qed_cdu_iids cdu_iids;
553dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
554fe56b9e6SYuval Mintz 	struct qed_qm_iids qm_iids;
555dbb799c3SYuval Mintz 	struct qed_tm_iids tm_iids;
556dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg;
557fe56b9e6SYuval Mintz 
558fe56b9e6SYuval Mintz 	memset(&qm_iids, 0, sizeof(qm_iids));
5591408cc1fSYuval Mintz 	memset(&cdu_iids, 0, sizeof(cdu_iids));
560dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
561dbb799c3SYuval Mintz 	memset(&tm_iids, 0, sizeof(tm_iids));
562fe56b9e6SYuval Mintz 
563fe56b9e6SYuval Mintz 	p_mngr->pf_start_line = RESC_START(p_hwfn, QED_ILT);
564fe56b9e6SYuval Mintz 
565fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
566fe56b9e6SYuval Mintz 		   "hwfn [%d] - Set context manager starting line to be 0x%08x\n",
567fe56b9e6SYuval Mintz 		   p_hwfn->my_id, p_hwfn->p_cxt_mngr->pf_start_line);
568fe56b9e6SYuval Mintz 
569fe56b9e6SYuval Mintz 	/* CDUC */
570fe56b9e6SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUC];
571fe56b9e6SYuval Mintz 	curr_line = p_mngr->pf_start_line;
5721408cc1fSYuval Mintz 
5731408cc1fSYuval Mintz 	/* CDUC PF */
574fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = 0;
575fe56b9e6SYuval Mintz 
576fe56b9e6SYuval Mintz 	/* get the counters for the CDUC and QM clients  */
5771408cc1fSYuval Mintz 	qed_cxt_cdu_iids(p_mngr, &cdu_iids);
578fe56b9e6SYuval Mintz 
579fe56b9e6SYuval Mintz 	p_blk = &p_cli->pf_blks[CDUC_BLK];
580fe56b9e6SYuval Mintz 
5811408cc1fSYuval Mintz 	total = cdu_iids.pf_cids * CONN_CXT_SIZE(p_hwfn);
582fe56b9e6SYuval Mintz 
583fe56b9e6SYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
584fe56b9e6SYuval Mintz 			     total, CONN_CXT_SIZE(p_hwfn));
585fe56b9e6SYuval Mintz 
586fe56b9e6SYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
587fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_blk->start_line;
588fe56b9e6SYuval Mintz 
589dbb799c3SYuval Mintz 	p_blk->dynamic_line_cnt = qed_ilt_get_dynamic_line_cnt(p_hwfn,
590dbb799c3SYuval Mintz 							       ILT_CLI_CDUC);
591dbb799c3SYuval Mintz 
5921408cc1fSYuval Mintz 	/* CDUC VF */
5931408cc1fSYuval Mintz 	p_blk = &p_cli->vf_blks[CDUC_BLK];
5941408cc1fSYuval Mintz 	total = cdu_iids.per_vf_cids * CONN_CXT_SIZE(p_hwfn);
5951408cc1fSYuval Mintz 
5961408cc1fSYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
5971408cc1fSYuval Mintz 			     total, CONN_CXT_SIZE(p_hwfn));
5981408cc1fSYuval Mintz 
5991408cc1fSYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
6001408cc1fSYuval Mintz 	p_cli->vf_total_lines = curr_line - p_blk->start_line;
6011408cc1fSYuval Mintz 
6021408cc1fSYuval Mintz 	for (i = 1; i < p_mngr->vf_count; i++)
6031408cc1fSYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
6041408cc1fSYuval Mintz 				     ILT_CLI_CDUC);
6051408cc1fSYuval Mintz 
606dbb799c3SYuval Mintz 	/* CDUT PF */
607dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
608dbb799c3SYuval Mintz 	p_cli->first.val = curr_line;
609dbb799c3SYuval Mintz 
610dbb799c3SYuval Mintz 	/* first the 'working' task memory */
611dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
612dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
613dbb799c3SYuval Mintz 		if (!p_seg || p_seg->count == 0)
614dbb799c3SYuval Mintz 			continue;
615dbb799c3SYuval Mintz 
616dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(i)];
617dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
618dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total,
619dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
620dbb799c3SYuval Mintz 
621dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
622dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
623dbb799c3SYuval Mintz 	}
624dbb799c3SYuval Mintz 
625dbb799c3SYuval Mintz 	/* next the 'init' task memory (forced load memory) */
626dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
627dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
628dbb799c3SYuval Mintz 		if (!p_seg || p_seg->count == 0)
629dbb799c3SYuval Mintz 			continue;
630dbb799c3SYuval Mintz 
631dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)];
632dbb799c3SYuval Mintz 
633dbb799c3SYuval Mintz 		if (!p_seg->has_fl_mem) {
634dbb799c3SYuval Mintz 			/* The segment is active (total size pf 'working'
635dbb799c3SYuval Mintz 			 * memory is > 0) but has no FL (forced-load, Init)
636dbb799c3SYuval Mintz 			 * memory. Thus:
637dbb799c3SYuval Mintz 			 *
638dbb799c3SYuval Mintz 			 * 1.   The total-size in the corrsponding FL block of
639dbb799c3SYuval Mintz 			 *      the ILT client is set to 0 - No ILT line are
640dbb799c3SYuval Mintz 			 *      provisioned and no ILT memory allocated.
641dbb799c3SYuval Mintz 			 *
642dbb799c3SYuval Mintz 			 * 2.   The start-line of said block is set to the
643dbb799c3SYuval Mintz 			 *      start line of the matching working memory
644dbb799c3SYuval Mintz 			 *      block in the ILT client. This is later used to
645dbb799c3SYuval Mintz 			 *      configure the CDU segment offset registers and
646dbb799c3SYuval Mintz 			 *      results in an FL command for TIDs of this
647dbb799c3SYuval Mintz 			 *      segement behaves as regular load commands
648dbb799c3SYuval Mintz 			 *      (loading TIDs from the working memory).
649dbb799c3SYuval Mintz 			 */
650dbb799c3SYuval Mintz 			line = p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line;
651dbb799c3SYuval Mintz 
652dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
653dbb799c3SYuval Mintz 			continue;
654dbb799c3SYuval Mintz 		}
655dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
656dbb799c3SYuval Mintz 
657dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk,
658dbb799c3SYuval Mintz 				     curr_line, total,
659dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
660dbb799c3SYuval Mintz 
661dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
662dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
663dbb799c3SYuval Mintz 	}
664dbb799c3SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_cli->pf_blks[0].start_line;
665dbb799c3SYuval Mintz 
666dbb799c3SYuval Mintz 	/* CDUT VF */
667dbb799c3SYuval Mintz 	p_seg = qed_cxt_tid_seg_info(p_hwfn, TASK_SEGMENT_VF);
668dbb799c3SYuval Mintz 	if (p_seg && p_seg->count) {
669dbb799c3SYuval Mintz 		/* Stricly speaking we need to iterate over all VF
670dbb799c3SYuval Mintz 		 * task segment types, but a VF has only 1 segment
671dbb799c3SYuval Mintz 		 */
672dbb799c3SYuval Mintz 
673dbb799c3SYuval Mintz 		/* 'working' memory */
674dbb799c3SYuval Mintz 		total = p_seg->count * p_mngr->task_type_size[p_seg->type];
675dbb799c3SYuval Mintz 
676dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
677dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk,
678dbb799c3SYuval Mintz 				     curr_line, total,
679dbb799c3SYuval Mintz 				     p_mngr->task_type_size[p_seg->type]);
680dbb799c3SYuval Mintz 
681dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
682dbb799c3SYuval Mintz 				     ILT_CLI_CDUT);
683dbb799c3SYuval Mintz 
684dbb799c3SYuval Mintz 		/* 'init' memory */
685dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
686dbb799c3SYuval Mintz 		if (!p_seg->has_fl_mem) {
687dbb799c3SYuval Mintz 			/* see comment above */
688dbb799c3SYuval Mintz 			line = p_cli->vf_blks[CDUT_SEG_BLK(0)].start_line;
689dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
690dbb799c3SYuval Mintz 		} else {
691dbb799c3SYuval Mintz 			task_size = p_mngr->task_type_size[p_seg->type];
692dbb799c3SYuval Mintz 			qed_ilt_cli_blk_fill(p_cli, p_blk,
693dbb799c3SYuval Mintz 					     curr_line, total, task_size);
694dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
695dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
696dbb799c3SYuval Mintz 		}
697dbb799c3SYuval Mintz 		p_cli->vf_total_lines = curr_line -
698dbb799c3SYuval Mintz 		    p_cli->vf_blks[0].start_line;
699dbb799c3SYuval Mintz 
700dbb799c3SYuval Mintz 		/* Now for the rest of the VFs */
701dbb799c3SYuval Mintz 		for (i = 1; i < p_mngr->vf_count; i++) {
702dbb799c3SYuval Mintz 			p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
703dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
704dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
705dbb799c3SYuval Mintz 
706dbb799c3SYuval Mintz 			p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
707dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
708dbb799c3SYuval Mintz 					     ILT_CLI_CDUT);
709dbb799c3SYuval Mintz 		}
710dbb799c3SYuval Mintz 	}
711dbb799c3SYuval Mintz 
712fe56b9e6SYuval Mintz 	/* QM */
713fe56b9e6SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_QM];
714fe56b9e6SYuval Mintz 	p_blk = &p_cli->pf_blks[0];
715fe56b9e6SYuval Mintz 
716fe56b9e6SYuval Mintz 	qed_cxt_qm_iids(p_hwfn, &qm_iids);
7171408cc1fSYuval Mintz 	total = qed_qm_pf_mem_size(p_hwfn->rel_pf_id, qm_iids.cids,
718dbb799c3SYuval Mintz 				   qm_iids.vf_cids, qm_iids.tids,
7191408cc1fSYuval Mintz 				   p_hwfn->qm_info.num_pqs,
7201408cc1fSYuval Mintz 				   p_hwfn->qm_info.num_vf_pqs);
721fe56b9e6SYuval Mintz 
7221408cc1fSYuval Mintz 	DP_VERBOSE(p_hwfn,
7231408cc1fSYuval Mintz 		   QED_MSG_ILT,
724dbb799c3SYuval Mintz 		   "QM ILT Info, (cids=%d, vf_cids=%d, tids=%d, num_pqs=%d, num_vf_pqs=%d, memory_size=%d)\n",
7251408cc1fSYuval Mintz 		   qm_iids.cids,
7261408cc1fSYuval Mintz 		   qm_iids.vf_cids,
727dbb799c3SYuval Mintz 		   qm_iids.tids,
7281408cc1fSYuval Mintz 		   p_hwfn->qm_info.num_pqs, p_hwfn->qm_info.num_vf_pqs, total);
729fe56b9e6SYuval Mintz 
730fe56b9e6SYuval Mintz 	qed_ilt_cli_blk_fill(p_cli, p_blk,
731fe56b9e6SYuval Mintz 			     curr_line, total * 0x1000,
732fe56b9e6SYuval Mintz 			     QM_PQ_ELEMENT_SIZE);
733fe56b9e6SYuval Mintz 
734fe56b9e6SYuval Mintz 	qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_QM);
735fe56b9e6SYuval Mintz 	p_cli->pf_total_lines = curr_line - p_blk->start_line;
736fe56b9e6SYuval Mintz 
737dbb799c3SYuval Mintz 	/* SRC */
738dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_SRC];
739dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
740dbb799c3SYuval Mintz 
741dbb799c3SYuval Mintz 	/* Both the PF and VFs searcher connections are stored in the per PF
742dbb799c3SYuval Mintz 	 * database. Thus sum the PF searcher cids and all the VFs searcher
743dbb799c3SYuval Mintz 	 * cids.
744dbb799c3SYuval Mintz 	 */
745dbb799c3SYuval Mintz 	total = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
746dbb799c3SYuval Mintz 	if (total) {
747dbb799c3SYuval Mintz 		u32 local_max = max_t(u32, total,
748dbb799c3SYuval Mintz 				      SRC_MIN_NUM_ELEMS);
749dbb799c3SYuval Mintz 
750dbb799c3SYuval Mintz 		total = roundup_pow_of_two(local_max);
751dbb799c3SYuval Mintz 
752dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[0];
753dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
754dbb799c3SYuval Mintz 				     total * sizeof(struct src_ent),
755dbb799c3SYuval Mintz 				     sizeof(struct src_ent));
756dbb799c3SYuval Mintz 
757dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
758dbb799c3SYuval Mintz 				     ILT_CLI_SRC);
759dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
760dbb799c3SYuval Mintz 	}
761dbb799c3SYuval Mintz 
762dbb799c3SYuval Mintz 	/* TM PF */
763dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_TM];
76444531ba4SMichal Kalderon 	qed_cxt_tm_iids(p_hwfn, p_mngr, &tm_iids);
765dbb799c3SYuval Mintz 	total = tm_iids.pf_cids + tm_iids.pf_tids_total;
766dbb799c3SYuval Mintz 	if (total) {
767dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[0];
768dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
769dbb799c3SYuval Mintz 				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
770dbb799c3SYuval Mintz 
771dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
772dbb799c3SYuval Mintz 				     ILT_CLI_TM);
773dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
774dbb799c3SYuval Mintz 	}
775dbb799c3SYuval Mintz 
776dbb799c3SYuval Mintz 	/* TM VF */
777dbb799c3SYuval Mintz 	total = tm_iids.per_vf_cids + tm_iids.per_vf_tids;
778dbb799c3SYuval Mintz 	if (total) {
779dbb799c3SYuval Mintz 		p_blk = &p_cli->vf_blks[0];
780dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
781dbb799c3SYuval Mintz 				     total * TM_ELEM_SIZE, TM_ELEM_SIZE);
782dbb799c3SYuval Mintz 
783dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
784dbb799c3SYuval Mintz 				     ILT_CLI_TM);
785dbb799c3SYuval Mintz 
78670566b42SMintz, Yuval 		p_cli->vf_total_lines = curr_line - p_blk->start_line;
787dbb799c3SYuval Mintz 		for (i = 1; i < p_mngr->vf_count; i++)
788dbb799c3SYuval Mintz 			qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
789dbb799c3SYuval Mintz 					     ILT_CLI_TM);
790dbb799c3SYuval Mintz 	}
791dbb799c3SYuval Mintz 
792dbb799c3SYuval Mintz 	/* TSDM (SRQ CONTEXT) */
793dbb799c3SYuval Mintz 	total = qed_cxt_get_srq_count(p_hwfn);
794dbb799c3SYuval Mintz 
795dbb799c3SYuval Mintz 	if (total) {
796dbb799c3SYuval Mintz 		p_cli = &p_mngr->clients[ILT_CLI_TSDM];
797dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
798dbb799c3SYuval Mintz 		qed_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
799dbb799c3SYuval Mintz 				     total * SRQ_CXT_SIZE, SRQ_CXT_SIZE);
800dbb799c3SYuval Mintz 
801dbb799c3SYuval Mintz 		qed_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
802dbb799c3SYuval Mintz 				     ILT_CLI_TSDM);
803dbb799c3SYuval Mintz 		p_cli->pf_total_lines = curr_line - p_blk->start_line;
804dbb799c3SYuval Mintz 	}
805dbb799c3SYuval Mintz 
806fe56b9e6SYuval Mintz 	if (curr_line - p_hwfn->p_cxt_mngr->pf_start_line >
807fe56b9e6SYuval Mintz 	    RESC_NUM(p_hwfn, QED_ILT)) {
808fe56b9e6SYuval Mintz 		DP_ERR(p_hwfn, "too many ilt lines...#lines=%d\n",
809fe56b9e6SYuval Mintz 		       curr_line - p_hwfn->p_cxt_mngr->pf_start_line);
810fe56b9e6SYuval Mintz 		return -EINVAL;
811fe56b9e6SYuval Mintz 	}
812fe56b9e6SYuval Mintz 
813fe56b9e6SYuval Mintz 	return 0;
814fe56b9e6SYuval Mintz }
815fe56b9e6SYuval Mintz 
816dbb799c3SYuval Mintz static void qed_cxt_src_t2_free(struct qed_hwfn *p_hwfn)
817dbb799c3SYuval Mintz {
818dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
819dbb799c3SYuval Mintz 	u32 i;
820dbb799c3SYuval Mintz 
821dbb799c3SYuval Mintz 	if (!p_mngr->t2)
822dbb799c3SYuval Mintz 		return;
823dbb799c3SYuval Mintz 
824dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++)
825dbb799c3SYuval Mintz 		if (p_mngr->t2[i].p_virt)
826dbb799c3SYuval Mintz 			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
827dbb799c3SYuval Mintz 					  p_mngr->t2[i].size,
828dbb799c3SYuval Mintz 					  p_mngr->t2[i].p_virt,
829dbb799c3SYuval Mintz 					  p_mngr->t2[i].p_phys);
830dbb799c3SYuval Mintz 
831dbb799c3SYuval Mintz 	kfree(p_mngr->t2);
832dbb799c3SYuval Mintz 	p_mngr->t2 = NULL;
833dbb799c3SYuval Mintz }
834dbb799c3SYuval Mintz 
835dbb799c3SYuval Mintz static int qed_cxt_src_t2_alloc(struct qed_hwfn *p_hwfn)
836dbb799c3SYuval Mintz {
837dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
838dbb799c3SYuval Mintz 	u32 conn_num, total_size, ent_per_page, psz, i;
839dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_src;
840dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
841dbb799c3SYuval Mintz 	struct qed_dma_mem *p_t2;
842dbb799c3SYuval Mintz 	int rc;
843dbb799c3SYuval Mintz 
844dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
845dbb799c3SYuval Mintz 
846dbb799c3SYuval Mintz 	/* if the SRC ILT client is inactive - there are no connection
847dbb799c3SYuval Mintz 	 * requiring the searcer, leave.
848dbb799c3SYuval Mintz 	 */
849dbb799c3SYuval Mintz 	p_src = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_SRC];
850dbb799c3SYuval Mintz 	if (!p_src->active)
851dbb799c3SYuval Mintz 		return 0;
852dbb799c3SYuval Mintz 
853dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
854dbb799c3SYuval Mintz 	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
855dbb799c3SYuval Mintz 	total_size = conn_num * sizeof(struct src_ent);
856dbb799c3SYuval Mintz 
857dbb799c3SYuval Mintz 	/* use the same page size as the SRC ILT client */
858dbb799c3SYuval Mintz 	psz = ILT_PAGE_IN_BYTES(p_src->p_size.val);
859dbb799c3SYuval Mintz 	p_mngr->t2_num_pages = DIV_ROUND_UP(total_size, psz);
860dbb799c3SYuval Mintz 
861dbb799c3SYuval Mintz 	/* allocate t2 */
8622591c280SJoe Perches 	p_mngr->t2 = kcalloc(p_mngr->t2_num_pages, sizeof(struct qed_dma_mem),
863dbb799c3SYuval Mintz 			     GFP_KERNEL);
864dbb799c3SYuval Mintz 	if (!p_mngr->t2) {
865dbb799c3SYuval Mintz 		rc = -ENOMEM;
866dbb799c3SYuval Mintz 		goto t2_fail;
867dbb799c3SYuval Mintz 	}
868dbb799c3SYuval Mintz 
869dbb799c3SYuval Mintz 	/* allocate t2 pages */
870dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++) {
871dbb799c3SYuval Mintz 		u32 size = min_t(u32, total_size, psz);
872dbb799c3SYuval Mintz 		void **p_virt = &p_mngr->t2[i].p_virt;
873dbb799c3SYuval Mintz 
874dbb799c3SYuval Mintz 		*p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
875dbb799c3SYuval Mintz 					     size,
876dbb799c3SYuval Mintz 					     &p_mngr->t2[i].p_phys, GFP_KERNEL);
877dbb799c3SYuval Mintz 		if (!p_mngr->t2[i].p_virt) {
878dbb799c3SYuval Mintz 			rc = -ENOMEM;
879dbb799c3SYuval Mintz 			goto t2_fail;
880dbb799c3SYuval Mintz 		}
881dbb799c3SYuval Mintz 		memset(*p_virt, 0, size);
882dbb799c3SYuval Mintz 		p_mngr->t2[i].size = size;
883dbb799c3SYuval Mintz 		total_size -= size;
884dbb799c3SYuval Mintz 	}
885dbb799c3SYuval Mintz 
886dbb799c3SYuval Mintz 	/* Set the t2 pointers */
887dbb799c3SYuval Mintz 
888dbb799c3SYuval Mintz 	/* entries per page - must be a power of two */
889dbb799c3SYuval Mintz 	ent_per_page = psz / sizeof(struct src_ent);
890dbb799c3SYuval Mintz 
891dbb799c3SYuval Mintz 	p_mngr->first_free = (u64) p_mngr->t2[0].p_phys;
892dbb799c3SYuval Mintz 
893dbb799c3SYuval Mintz 	p_t2 = &p_mngr->t2[(conn_num - 1) / ent_per_page];
894dbb799c3SYuval Mintz 	p_mngr->last_free = (u64) p_t2->p_phys +
895dbb799c3SYuval Mintz 	    ((conn_num - 1) & (ent_per_page - 1)) * sizeof(struct src_ent);
896dbb799c3SYuval Mintz 
897dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->t2_num_pages; i++) {
898dbb799c3SYuval Mintz 		u32 ent_num = min_t(u32,
899dbb799c3SYuval Mintz 				    ent_per_page,
900dbb799c3SYuval Mintz 				    conn_num);
901dbb799c3SYuval Mintz 		struct src_ent *entries = p_mngr->t2[i].p_virt;
902dbb799c3SYuval Mintz 		u64 p_ent_phys = (u64) p_mngr->t2[i].p_phys, val;
903dbb799c3SYuval Mintz 		u32 j;
904dbb799c3SYuval Mintz 
905dbb799c3SYuval Mintz 		for (j = 0; j < ent_num - 1; j++) {
906dbb799c3SYuval Mintz 			val = p_ent_phys + (j + 1) * sizeof(struct src_ent);
907dbb799c3SYuval Mintz 			entries[j].next = cpu_to_be64(val);
908dbb799c3SYuval Mintz 		}
909dbb799c3SYuval Mintz 
910dbb799c3SYuval Mintz 		if (i < p_mngr->t2_num_pages - 1)
911dbb799c3SYuval Mintz 			val = (u64) p_mngr->t2[i + 1].p_phys;
912dbb799c3SYuval Mintz 		else
913dbb799c3SYuval Mintz 			val = 0;
914dbb799c3SYuval Mintz 		entries[j].next = cpu_to_be64(val);
915dbb799c3SYuval Mintz 
91601e517f1SDan Carpenter 		conn_num -= ent_num;
917dbb799c3SYuval Mintz 	}
918dbb799c3SYuval Mintz 
919dbb799c3SYuval Mintz 	return 0;
920dbb799c3SYuval Mintz 
921dbb799c3SYuval Mintz t2_fail:
922dbb799c3SYuval Mintz 	qed_cxt_src_t2_free(p_hwfn);
923dbb799c3SYuval Mintz 	return rc;
924dbb799c3SYuval Mintz }
925dbb799c3SYuval Mintz 
926fe56b9e6SYuval Mintz #define for_each_ilt_valid_client(pos, clients)	\
927dbb799c3SYuval Mintz 	for (pos = 0; pos < ILT_CLI_MAX; pos++)	\
928dbb799c3SYuval Mintz 		if (!clients[pos].active) {	\
929dbb799c3SYuval Mintz 			continue;		\
930dbb799c3SYuval Mintz 		} else				\
931fe56b9e6SYuval Mintz 
932fe56b9e6SYuval Mintz /* Total number of ILT lines used by this PF */
933fe56b9e6SYuval Mintz static u32 qed_cxt_ilt_shadow_size(struct qed_ilt_client_cfg *ilt_clients)
934fe56b9e6SYuval Mintz {
935fe56b9e6SYuval Mintz 	u32 size = 0;
936fe56b9e6SYuval Mintz 	u32 i;
937fe56b9e6SYuval Mintz 
938dbb799c3SYuval Mintz 	for_each_ilt_valid_client(i, ilt_clients)
939dbb799c3SYuval Mintz 	    size += (ilt_clients[i].last.val - ilt_clients[i].first.val + 1);
940fe56b9e6SYuval Mintz 
941fe56b9e6SYuval Mintz 	return size;
942fe56b9e6SYuval Mintz }
943fe56b9e6SYuval Mintz 
944fe56b9e6SYuval Mintz static void qed_ilt_shadow_free(struct qed_hwfn *p_hwfn)
945fe56b9e6SYuval Mintz {
946fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *p_cli = p_hwfn->p_cxt_mngr->clients;
947fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
948fe56b9e6SYuval Mintz 	u32 ilt_size, i;
949fe56b9e6SYuval Mintz 
950fe56b9e6SYuval Mintz 	ilt_size = qed_cxt_ilt_shadow_size(p_cli);
951fe56b9e6SYuval Mintz 
952fe56b9e6SYuval Mintz 	for (i = 0; p_mngr->ilt_shadow && i < ilt_size; i++) {
953fe56b9e6SYuval Mintz 		struct qed_dma_mem *p_dma = &p_mngr->ilt_shadow[i];
954fe56b9e6SYuval Mintz 
955fe56b9e6SYuval Mintz 		if (p_dma->p_virt)
956fe56b9e6SYuval Mintz 			dma_free_coherent(&p_hwfn->cdev->pdev->dev,
957fe56b9e6SYuval Mintz 					  p_dma->size, p_dma->p_virt,
958fe56b9e6SYuval Mintz 					  p_dma->p_phys);
959fe56b9e6SYuval Mintz 		p_dma->p_virt = NULL;
960fe56b9e6SYuval Mintz 	}
961fe56b9e6SYuval Mintz 	kfree(p_mngr->ilt_shadow);
962fe56b9e6SYuval Mintz }
963fe56b9e6SYuval Mintz 
964fe56b9e6SYuval Mintz static int qed_ilt_blk_alloc(struct qed_hwfn *p_hwfn,
965fe56b9e6SYuval Mintz 			     struct qed_ilt_cli_blk *p_blk,
966fe56b9e6SYuval Mintz 			     enum ilt_clients ilt_client,
967fe56b9e6SYuval Mintz 			     u32 start_line_offset)
968fe56b9e6SYuval Mintz {
969fe56b9e6SYuval Mintz 	struct qed_dma_mem *ilt_shadow = p_hwfn->p_cxt_mngr->ilt_shadow;
970dbb799c3SYuval Mintz 	u32 lines, line, sz_left, lines_to_skip = 0;
971dbb799c3SYuval Mintz 
972dbb799c3SYuval Mintz 	/* Special handling for RoCE that supports dynamic allocation */
973dbb799c3SYuval Mintz 	if ((p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) &&
974dbb799c3SYuval Mintz 	    ((ilt_client == ILT_CLI_CDUT) || ilt_client == ILT_CLI_TSDM))
975dbb799c3SYuval Mintz 		return 0;
976dbb799c3SYuval Mintz 
977dbb799c3SYuval Mintz 	lines_to_skip = p_blk->dynamic_line_cnt;
978fe56b9e6SYuval Mintz 
979fe56b9e6SYuval Mintz 	if (!p_blk->total_size)
980fe56b9e6SYuval Mintz 		return 0;
981fe56b9e6SYuval Mintz 
982fe56b9e6SYuval Mintz 	sz_left = p_blk->total_size;
983dbb799c3SYuval Mintz 	lines = DIV_ROUND_UP(sz_left, p_blk->real_size_in_page) - lines_to_skip;
984fe56b9e6SYuval Mintz 	line = p_blk->start_line + start_line_offset -
985dbb799c3SYuval Mintz 	    p_hwfn->p_cxt_mngr->pf_start_line + lines_to_skip;
986fe56b9e6SYuval Mintz 
987fe56b9e6SYuval Mintz 	for (; lines; lines--) {
988fe56b9e6SYuval Mintz 		dma_addr_t p_phys;
989fe56b9e6SYuval Mintz 		void *p_virt;
990fe56b9e6SYuval Mintz 		u32 size;
991fe56b9e6SYuval Mintz 
9921a635e48SYuval Mintz 		size = min_t(u32, sz_left, p_blk->real_size_in_page);
993fe56b9e6SYuval Mintz 		p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
9941a635e48SYuval Mintz 					    size, &p_phys, GFP_KERNEL);
995fe56b9e6SYuval Mintz 		if (!p_virt)
996fe56b9e6SYuval Mintz 			return -ENOMEM;
997fe56b9e6SYuval Mintz 		memset(p_virt, 0, size);
998fe56b9e6SYuval Mintz 
999fe56b9e6SYuval Mintz 		ilt_shadow[line].p_phys = p_phys;
1000fe56b9e6SYuval Mintz 		ilt_shadow[line].p_virt = p_virt;
1001fe56b9e6SYuval Mintz 		ilt_shadow[line].size = size;
1002fe56b9e6SYuval Mintz 
1003fe56b9e6SYuval Mintz 		DP_VERBOSE(p_hwfn, QED_MSG_ILT,
1004fe56b9e6SYuval Mintz 			   "ILT shadow: Line [%d] Physical 0x%llx Virtual %p Size %d\n",
1005fe56b9e6SYuval Mintz 			    line, (u64)p_phys, p_virt, size);
1006fe56b9e6SYuval Mintz 
1007fe56b9e6SYuval Mintz 		sz_left -= size;
1008fe56b9e6SYuval Mintz 		line++;
1009fe56b9e6SYuval Mintz 	}
1010fe56b9e6SYuval Mintz 
1011fe56b9e6SYuval Mintz 	return 0;
1012fe56b9e6SYuval Mintz }
1013fe56b9e6SYuval Mintz 
1014fe56b9e6SYuval Mintz static int qed_ilt_shadow_alloc(struct qed_hwfn *p_hwfn)
1015fe56b9e6SYuval Mintz {
1016fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1017fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *clients = p_mngr->clients;
1018fe56b9e6SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
10191408cc1fSYuval Mintz 	u32 size, i, j, k;
1020fe56b9e6SYuval Mintz 	int rc;
1021fe56b9e6SYuval Mintz 
1022fe56b9e6SYuval Mintz 	size = qed_cxt_ilt_shadow_size(clients);
1023fe56b9e6SYuval Mintz 	p_mngr->ilt_shadow = kcalloc(size, sizeof(struct qed_dma_mem),
1024fe56b9e6SYuval Mintz 				     GFP_KERNEL);
1025fe56b9e6SYuval Mintz 	if (!p_mngr->ilt_shadow) {
1026fe56b9e6SYuval Mintz 		rc = -ENOMEM;
1027fe56b9e6SYuval Mintz 		goto ilt_shadow_fail;
1028fe56b9e6SYuval Mintz 	}
1029fe56b9e6SYuval Mintz 
1030fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, QED_MSG_ILT,
1031fe56b9e6SYuval Mintz 		   "Allocated 0x%x bytes for ilt shadow\n",
1032fe56b9e6SYuval Mintz 		   (u32)(size * sizeof(struct qed_dma_mem)));
1033fe56b9e6SYuval Mintz 
1034fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, clients) {
1035fe56b9e6SYuval Mintz 		for (j = 0; j < ILT_CLI_PF_BLOCKS; j++) {
1036fe56b9e6SYuval Mintz 			p_blk = &clients[i].pf_blks[j];
1037fe56b9e6SYuval Mintz 			rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, 0);
10381a635e48SYuval Mintz 			if (rc)
1039fe56b9e6SYuval Mintz 				goto ilt_shadow_fail;
1040fe56b9e6SYuval Mintz 		}
10411408cc1fSYuval Mintz 		for (k = 0; k < p_mngr->vf_count; k++) {
10421408cc1fSYuval Mintz 			for (j = 0; j < ILT_CLI_VF_BLOCKS; j++) {
10431408cc1fSYuval Mintz 				u32 lines = clients[i].vf_total_lines * k;
10441408cc1fSYuval Mintz 
10451408cc1fSYuval Mintz 				p_blk = &clients[i].vf_blks[j];
10461408cc1fSYuval Mintz 				rc = qed_ilt_blk_alloc(p_hwfn, p_blk, i, lines);
10471a635e48SYuval Mintz 				if (rc)
10481408cc1fSYuval Mintz 					goto ilt_shadow_fail;
10491408cc1fSYuval Mintz 			}
10501408cc1fSYuval Mintz 		}
1051fe56b9e6SYuval Mintz 	}
1052fe56b9e6SYuval Mintz 
1053fe56b9e6SYuval Mintz 	return 0;
1054fe56b9e6SYuval Mintz 
1055fe56b9e6SYuval Mintz ilt_shadow_fail:
1056fe56b9e6SYuval Mintz 	qed_ilt_shadow_free(p_hwfn);
1057fe56b9e6SYuval Mintz 	return rc;
1058fe56b9e6SYuval Mintz }
1059fe56b9e6SYuval Mintz 
1060fe56b9e6SYuval Mintz static void qed_cid_map_free(struct qed_hwfn *p_hwfn)
1061fe56b9e6SYuval Mintz {
1062fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1063fe56b9e6SYuval Mintz 	u32 type;
1064fe56b9e6SYuval Mintz 
1065fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1066fe56b9e6SYuval Mintz 		kfree(p_mngr->acquired[type].cid_map);
1067fe56b9e6SYuval Mintz 		p_mngr->acquired[type].max_count = 0;
1068fe56b9e6SYuval Mintz 		p_mngr->acquired[type].start_cid = 0;
1069fe56b9e6SYuval Mintz 	}
1070fe56b9e6SYuval Mintz }
1071fe56b9e6SYuval Mintz 
1072fe56b9e6SYuval Mintz static int qed_cid_map_alloc(struct qed_hwfn *p_hwfn)
1073fe56b9e6SYuval Mintz {
1074fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1075fe56b9e6SYuval Mintz 	u32 start_cid = 0;
1076fe56b9e6SYuval Mintz 	u32 type;
1077fe56b9e6SYuval Mintz 
1078fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1079fe56b9e6SYuval Mintz 		u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1080fe56b9e6SYuval Mintz 		u32 size;
1081fe56b9e6SYuval Mintz 
1082fe56b9e6SYuval Mintz 		if (cid_cnt == 0)
1083fe56b9e6SYuval Mintz 			continue;
1084fe56b9e6SYuval Mintz 
1085fe56b9e6SYuval Mintz 		size = DIV_ROUND_UP(cid_cnt,
1086fe56b9e6SYuval Mintz 				    sizeof(unsigned long) * BITS_PER_BYTE) *
1087fe56b9e6SYuval Mintz 		       sizeof(unsigned long);
1088fe56b9e6SYuval Mintz 		p_mngr->acquired[type].cid_map = kzalloc(size, GFP_KERNEL);
1089fe56b9e6SYuval Mintz 		if (!p_mngr->acquired[type].cid_map)
1090fe56b9e6SYuval Mintz 			goto cid_map_fail;
1091fe56b9e6SYuval Mintz 
1092fe56b9e6SYuval Mintz 		p_mngr->acquired[type].max_count = cid_cnt;
1093fe56b9e6SYuval Mintz 		p_mngr->acquired[type].start_cid = start_cid;
1094fe56b9e6SYuval Mintz 
1095fe56b9e6SYuval Mintz 		p_hwfn->p_cxt_mngr->conn_cfg[type].cid_start = start_cid;
1096fe56b9e6SYuval Mintz 
1097fe56b9e6SYuval Mintz 		DP_VERBOSE(p_hwfn, QED_MSG_CXT,
1098fe56b9e6SYuval Mintz 			   "Type %08x start: %08x count %08x\n",
1099fe56b9e6SYuval Mintz 			   type, p_mngr->acquired[type].start_cid,
1100fe56b9e6SYuval Mintz 			   p_mngr->acquired[type].max_count);
1101fe56b9e6SYuval Mintz 		start_cid += cid_cnt;
1102fe56b9e6SYuval Mintz 	}
1103fe56b9e6SYuval Mintz 
1104fe56b9e6SYuval Mintz 	return 0;
1105fe56b9e6SYuval Mintz 
1106fe56b9e6SYuval Mintz cid_map_fail:
1107fe56b9e6SYuval Mintz 	qed_cid_map_free(p_hwfn);
1108fe56b9e6SYuval Mintz 	return -ENOMEM;
1109fe56b9e6SYuval Mintz }
1110fe56b9e6SYuval Mintz 
1111fe56b9e6SYuval Mintz int qed_cxt_mngr_alloc(struct qed_hwfn *p_hwfn)
1112fe56b9e6SYuval Mintz {
1113dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *clients;
1114fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr;
1115fe56b9e6SYuval Mintz 	u32 i;
1116fe56b9e6SYuval Mintz 
111760fffb3bSYuval Mintz 	p_mngr = kzalloc(sizeof(*p_mngr), GFP_KERNEL);
11182591c280SJoe Perches 	if (!p_mngr)
1119fe56b9e6SYuval Mintz 		return -ENOMEM;
1120fe56b9e6SYuval Mintz 
1121fe56b9e6SYuval Mintz 	/* Initialize ILT client registers */
1122dbb799c3SYuval Mintz 	clients = p_mngr->clients;
1123dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].first.reg = ILT_CFG_REG(CDUC, FIRST_ILT);
1124dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].last.reg = ILT_CFG_REG(CDUC, LAST_ILT);
1125dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUC].p_size.reg = ILT_CFG_REG(CDUC, P_SIZE);
1126fe56b9e6SYuval Mintz 
1127dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].first.reg = ILT_CFG_REG(QM, FIRST_ILT);
1128dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].last.reg = ILT_CFG_REG(QM, LAST_ILT);
1129dbb799c3SYuval Mintz 	clients[ILT_CLI_QM].p_size.reg = ILT_CFG_REG(QM, P_SIZE);
1130fe56b9e6SYuval Mintz 
1131dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].first.reg = ILT_CFG_REG(TM, FIRST_ILT);
1132dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].last.reg = ILT_CFG_REG(TM, LAST_ILT);
1133dbb799c3SYuval Mintz 	clients[ILT_CLI_TM].p_size.reg = ILT_CFG_REG(TM, P_SIZE);
1134dbb799c3SYuval Mintz 
1135dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].first.reg = ILT_CFG_REG(SRC, FIRST_ILT);
1136dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].last.reg = ILT_CFG_REG(SRC, LAST_ILT);
1137dbb799c3SYuval Mintz 	clients[ILT_CLI_SRC].p_size.reg = ILT_CFG_REG(SRC, P_SIZE);
1138dbb799c3SYuval Mintz 
1139dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].first.reg = ILT_CFG_REG(CDUT, FIRST_ILT);
1140dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].last.reg = ILT_CFG_REG(CDUT, LAST_ILT);
1141dbb799c3SYuval Mintz 	clients[ILT_CLI_CDUT].p_size.reg = ILT_CFG_REG(CDUT, P_SIZE);
1142dbb799c3SYuval Mintz 
1143dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].first.reg = ILT_CFG_REG(TSDM, FIRST_ILT);
1144dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].last.reg = ILT_CFG_REG(TSDM, LAST_ILT);
1145dbb799c3SYuval Mintz 	clients[ILT_CLI_TSDM].p_size.reg = ILT_CFG_REG(TSDM, P_SIZE);
1146be086e7cSMintz, Yuval 	/* default ILT page size for all clients is 64K */
1147fe56b9e6SYuval Mintz 	for (i = 0; i < ILT_CLI_MAX; i++)
1148fe56b9e6SYuval Mintz 		p_mngr->clients[i].p_size.val = ILT_DEFAULT_HW_P_SIZE;
1149fe56b9e6SYuval Mintz 
1150dbb799c3SYuval Mintz 	/* Initialize task sizes */
1151dbb799c3SYuval Mintz 	p_mngr->task_type_size[0] = TYPE0_TASK_CXT_SIZE(p_hwfn);
1152dbb799c3SYuval Mintz 	p_mngr->task_type_size[1] = TYPE1_TASK_CXT_SIZE(p_hwfn);
1153dbb799c3SYuval Mintz 
11541408cc1fSYuval Mintz 	if (p_hwfn->cdev->p_iov_info)
11551408cc1fSYuval Mintz 		p_mngr->vf_count = p_hwfn->cdev->p_iov_info->total_vfs;
1156dbb799c3SYuval Mintz 	/* Initialize the dynamic ILT allocation mutex */
1157dbb799c3SYuval Mintz 	mutex_init(&p_mngr->mutex);
11581408cc1fSYuval Mintz 
1159fe56b9e6SYuval Mintz 	/* Set the cxt mangr pointer priori to further allocations */
1160fe56b9e6SYuval Mintz 	p_hwfn->p_cxt_mngr = p_mngr;
1161fe56b9e6SYuval Mintz 
1162fe56b9e6SYuval Mintz 	return 0;
1163fe56b9e6SYuval Mintz }
1164fe56b9e6SYuval Mintz 
1165fe56b9e6SYuval Mintz int qed_cxt_tables_alloc(struct qed_hwfn *p_hwfn)
1166fe56b9e6SYuval Mintz {
1167fe56b9e6SYuval Mintz 	int rc;
1168fe56b9e6SYuval Mintz 
1169fe56b9e6SYuval Mintz 	/* Allocate the ILT shadow table */
1170fe56b9e6SYuval Mintz 	rc = qed_ilt_shadow_alloc(p_hwfn);
11712591c280SJoe Perches 	if (rc)
1172fe56b9e6SYuval Mintz 		goto tables_alloc_fail;
1173fe56b9e6SYuval Mintz 
1174dbb799c3SYuval Mintz 	/* Allocate the T2  table */
1175dbb799c3SYuval Mintz 	rc = qed_cxt_src_t2_alloc(p_hwfn);
11762591c280SJoe Perches 	if (rc)
1177dbb799c3SYuval Mintz 		goto tables_alloc_fail;
1178dbb799c3SYuval Mintz 
1179fe56b9e6SYuval Mintz 	/* Allocate and initialize the acquired cids bitmaps */
1180fe56b9e6SYuval Mintz 	rc = qed_cid_map_alloc(p_hwfn);
11812591c280SJoe Perches 	if (rc)
1182fe56b9e6SYuval Mintz 		goto tables_alloc_fail;
1183fe56b9e6SYuval Mintz 
1184fe56b9e6SYuval Mintz 	return 0;
1185fe56b9e6SYuval Mintz 
1186fe56b9e6SYuval Mintz tables_alloc_fail:
1187fe56b9e6SYuval Mintz 	qed_cxt_mngr_free(p_hwfn);
1188fe56b9e6SYuval Mintz 	return rc;
1189fe56b9e6SYuval Mintz }
1190fe56b9e6SYuval Mintz 
1191fe56b9e6SYuval Mintz void qed_cxt_mngr_free(struct qed_hwfn *p_hwfn)
1192fe56b9e6SYuval Mintz {
1193fe56b9e6SYuval Mintz 	if (!p_hwfn->p_cxt_mngr)
1194fe56b9e6SYuval Mintz 		return;
1195fe56b9e6SYuval Mintz 
1196fe56b9e6SYuval Mintz 	qed_cid_map_free(p_hwfn);
1197dbb799c3SYuval Mintz 	qed_cxt_src_t2_free(p_hwfn);
1198fe56b9e6SYuval Mintz 	qed_ilt_shadow_free(p_hwfn);
1199fe56b9e6SYuval Mintz 	kfree(p_hwfn->p_cxt_mngr);
1200fe56b9e6SYuval Mintz 
1201fe56b9e6SYuval Mintz 	p_hwfn->p_cxt_mngr = NULL;
1202fe56b9e6SYuval Mintz }
1203fe56b9e6SYuval Mintz 
1204fe56b9e6SYuval Mintz void qed_cxt_mngr_setup(struct qed_hwfn *p_hwfn)
1205fe56b9e6SYuval Mintz {
1206fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1207fe56b9e6SYuval Mintz 	int type;
1208fe56b9e6SYuval Mintz 
1209fe56b9e6SYuval Mintz 	/* Reset acquired cids */
1210fe56b9e6SYuval Mintz 	for (type = 0; type < MAX_CONN_TYPES; type++) {
1211fe56b9e6SYuval Mintz 		u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1212fe56b9e6SYuval Mintz 
1213fe56b9e6SYuval Mintz 		if (cid_cnt == 0)
1214fe56b9e6SYuval Mintz 			continue;
1215fe56b9e6SYuval Mintz 
1216fe56b9e6SYuval Mintz 		memset(p_mngr->acquired[type].cid_map, 0,
1217fe56b9e6SYuval Mintz 		       DIV_ROUND_UP(cid_cnt,
1218fe56b9e6SYuval Mintz 				    sizeof(unsigned long) * BITS_PER_BYTE) *
1219fe56b9e6SYuval Mintz 		       sizeof(unsigned long));
1220fe56b9e6SYuval Mintz 	}
1221fe56b9e6SYuval Mintz }
1222fe56b9e6SYuval Mintz 
1223fe56b9e6SYuval Mintz /* CDU Common */
1224fe56b9e6SYuval Mintz #define CDUC_CXT_SIZE_SHIFT \
1225fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE_SHIFT
1226fe56b9e6SYuval Mintz 
1227fe56b9e6SYuval Mintz #define CDUC_CXT_SIZE_MASK \
1228fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE >> CDUC_CXT_SIZE_SHIFT)
1229fe56b9e6SYuval Mintz 
1230fe56b9e6SYuval Mintz #define CDUC_BLOCK_WASTE_SHIFT \
1231fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE_SHIFT
1232fe56b9e6SYuval Mintz 
1233fe56b9e6SYuval Mintz #define CDUC_BLOCK_WASTE_MASK \
1234fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE >> CDUC_BLOCK_WASTE_SHIFT)
1235fe56b9e6SYuval Mintz 
1236fe56b9e6SYuval Mintz #define CDUC_NCIB_SHIFT	\
1237fe56b9e6SYuval Mintz 	CDU_REG_CID_ADDR_PARAMS_NCIB_SHIFT
1238fe56b9e6SYuval Mintz 
1239fe56b9e6SYuval Mintz #define CDUC_NCIB_MASK \
1240fe56b9e6SYuval Mintz 	(CDU_REG_CID_ADDR_PARAMS_NCIB >> CDUC_NCIB_SHIFT)
1241fe56b9e6SYuval Mintz 
1242dbb799c3SYuval Mintz #define CDUT_TYPE0_CXT_SIZE_SHIFT \
1243dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE_SHIFT
1244dbb799c3SYuval Mintz 
1245dbb799c3SYuval Mintz #define CDUT_TYPE0_CXT_SIZE_MASK		\
1246dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE >>	\
1247dbb799c3SYuval Mintz 	 CDUT_TYPE0_CXT_SIZE_SHIFT)
1248dbb799c3SYuval Mintz 
1249dbb799c3SYuval Mintz #define CDUT_TYPE0_BLOCK_WASTE_SHIFT \
1250dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE_SHIFT
1251dbb799c3SYuval Mintz 
1252dbb799c3SYuval Mintz #define CDUT_TYPE0_BLOCK_WASTE_MASK		       \
1253dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE >> \
1254dbb799c3SYuval Mintz 	 CDUT_TYPE0_BLOCK_WASTE_SHIFT)
1255dbb799c3SYuval Mintz 
1256dbb799c3SYuval Mintz #define CDUT_TYPE0_NCIB_SHIFT \
1257dbb799c3SYuval Mintz 	CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK_SHIFT
1258dbb799c3SYuval Mintz 
1259dbb799c3SYuval Mintz #define CDUT_TYPE0_NCIB_MASK				 \
1260dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK >> \
1261dbb799c3SYuval Mintz 	 CDUT_TYPE0_NCIB_SHIFT)
1262dbb799c3SYuval Mintz 
1263dbb799c3SYuval Mintz #define CDUT_TYPE1_CXT_SIZE_SHIFT \
1264dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE_SHIFT
1265dbb799c3SYuval Mintz 
1266dbb799c3SYuval Mintz #define CDUT_TYPE1_CXT_SIZE_MASK		\
1267dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE >>	\
1268dbb799c3SYuval Mintz 	 CDUT_TYPE1_CXT_SIZE_SHIFT)
1269dbb799c3SYuval Mintz 
1270dbb799c3SYuval Mintz #define CDUT_TYPE1_BLOCK_WASTE_SHIFT \
1271dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE_SHIFT
1272dbb799c3SYuval Mintz 
1273dbb799c3SYuval Mintz #define CDUT_TYPE1_BLOCK_WASTE_MASK		       \
1274dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE >> \
1275dbb799c3SYuval Mintz 	 CDUT_TYPE1_BLOCK_WASTE_SHIFT)
1276dbb799c3SYuval Mintz 
1277dbb799c3SYuval Mintz #define CDUT_TYPE1_NCIB_SHIFT \
1278dbb799c3SYuval Mintz 	CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK_SHIFT
1279dbb799c3SYuval Mintz 
1280dbb799c3SYuval Mintz #define CDUT_TYPE1_NCIB_MASK				 \
1281dbb799c3SYuval Mintz 	(CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK >> \
1282dbb799c3SYuval Mintz 	 CDUT_TYPE1_NCIB_SHIFT)
1283dbb799c3SYuval Mintz 
1284fe56b9e6SYuval Mintz static void qed_cdu_init_common(struct qed_hwfn *p_hwfn)
1285fe56b9e6SYuval Mintz {
1286fe56b9e6SYuval Mintz 	u32 page_sz, elems_per_page, block_waste, cxt_size, cdu_params = 0;
1287fe56b9e6SYuval Mintz 
1288fe56b9e6SYuval Mintz 	/* CDUC - connection configuration */
1289fe56b9e6SYuval Mintz 	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1290fe56b9e6SYuval Mintz 	cxt_size = CONN_CXT_SIZE(p_hwfn);
1291fe56b9e6SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1292fe56b9e6SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1293fe56b9e6SYuval Mintz 
1294fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_CXT_SIZE, cxt_size);
1295fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_BLOCK_WASTE, block_waste);
1296fe56b9e6SYuval Mintz 	SET_FIELD(cdu_params, CDUC_NCIB, elems_per_page);
1297fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_CID_ADDR_PARAMS_RT_OFFSET, cdu_params);
1298dbb799c3SYuval Mintz 
1299dbb799c3SYuval Mintz 	/* CDUT - type-0 tasks configuration */
1300dbb799c3SYuval Mintz 	page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT].p_size.val;
1301dbb799c3SYuval Mintz 	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[0];
1302dbb799c3SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1303dbb799c3SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1304dbb799c3SYuval Mintz 
1305dbb799c3SYuval Mintz 	/* cxt size and block-waste are multipes of 8 */
1306dbb799c3SYuval Mintz 	cdu_params = 0;
1307dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_CXT_SIZE, (cxt_size >> 3));
1308dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_BLOCK_WASTE, (block_waste >> 3));
1309dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE0_NCIB, elems_per_page);
1310dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT0_PARAMS_RT_OFFSET, cdu_params);
1311dbb799c3SYuval Mintz 
1312dbb799c3SYuval Mintz 	/* CDUT - type-1 tasks configuration */
1313dbb799c3SYuval Mintz 	cxt_size = p_hwfn->p_cxt_mngr->task_type_size[1];
1314dbb799c3SYuval Mintz 	elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1315dbb799c3SYuval Mintz 	block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1316dbb799c3SYuval Mintz 
1317dbb799c3SYuval Mintz 	/* cxt size and block-waste are multipes of 8 */
1318dbb799c3SYuval Mintz 	cdu_params = 0;
1319dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_CXT_SIZE, (cxt_size >> 3));
1320dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_BLOCK_WASTE, (block_waste >> 3));
1321dbb799c3SYuval Mintz 	SET_FIELD(cdu_params, CDUT_TYPE1_NCIB, elems_per_page);
1322dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT1_PARAMS_RT_OFFSET, cdu_params);
1323dbb799c3SYuval Mintz }
1324dbb799c3SYuval Mintz 
1325dbb799c3SYuval Mintz /* CDU PF */
1326dbb799c3SYuval Mintz #define CDU_SEG_REG_TYPE_SHIFT          CDU_SEG_TYPE_OFFSET_REG_TYPE_SHIFT
1327dbb799c3SYuval Mintz #define CDU_SEG_REG_TYPE_MASK           0x1
1328dbb799c3SYuval Mintz #define CDU_SEG_REG_OFFSET_SHIFT        0
1329dbb799c3SYuval Mintz #define CDU_SEG_REG_OFFSET_MASK         CDU_SEG_TYPE_OFFSET_REG_OFFSET_MASK
1330dbb799c3SYuval Mintz 
1331dbb799c3SYuval Mintz static void qed_cdu_init_pf(struct qed_hwfn *p_hwfn)
1332dbb799c3SYuval Mintz {
1333dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
1334dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg;
1335dbb799c3SYuval Mintz 	u32 cdu_seg_params, offset;
1336dbb799c3SYuval Mintz 	int i;
1337dbb799c3SYuval Mintz 
1338dbb799c3SYuval Mintz 	static const u32 rt_type_offset_arr[] = {
1339dbb799c3SYuval Mintz 		CDU_REG_PF_SEG0_TYPE_OFFSET_RT_OFFSET,
1340dbb799c3SYuval Mintz 		CDU_REG_PF_SEG1_TYPE_OFFSET_RT_OFFSET,
1341dbb799c3SYuval Mintz 		CDU_REG_PF_SEG2_TYPE_OFFSET_RT_OFFSET,
1342dbb799c3SYuval Mintz 		CDU_REG_PF_SEG3_TYPE_OFFSET_RT_OFFSET
1343dbb799c3SYuval Mintz 	};
1344dbb799c3SYuval Mintz 
1345dbb799c3SYuval Mintz 	static const u32 rt_type_offset_fl_arr[] = {
1346dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG0_TYPE_OFFSET_RT_OFFSET,
1347dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG1_TYPE_OFFSET_RT_OFFSET,
1348dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG2_TYPE_OFFSET_RT_OFFSET,
1349dbb799c3SYuval Mintz 		CDU_REG_PF_FL_SEG3_TYPE_OFFSET_RT_OFFSET
1350dbb799c3SYuval Mintz 	};
1351dbb799c3SYuval Mintz 
1352dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1353dbb799c3SYuval Mintz 
1354dbb799c3SYuval Mintz 	/* There are initializations only for CDUT during pf Phase */
1355dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1356dbb799c3SYuval Mintz 		/* Segment 0 */
1357dbb799c3SYuval Mintz 		p_seg = qed_cxt_tid_seg_info(p_hwfn, i);
1358dbb799c3SYuval Mintz 		if (!p_seg)
1359dbb799c3SYuval Mintz 			continue;
1360dbb799c3SYuval Mintz 
1361dbb799c3SYuval Mintz 		/* Note: start_line is already adjusted for the CDU
1362dbb799c3SYuval Mintz 		 * segment register granularity, so we just need to
1363dbb799c3SYuval Mintz 		 * divide. Adjustment is implicit as we assume ILT
1364dbb799c3SYuval Mintz 		 * Page size is larger than 32K!
1365dbb799c3SYuval Mintz 		 */
1366dbb799c3SYuval Mintz 		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1367dbb799c3SYuval Mintz 			  (p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line -
1368dbb799c3SYuval Mintz 			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1369dbb799c3SYuval Mintz 
1370dbb799c3SYuval Mintz 		cdu_seg_params = 0;
1371dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1372dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1373dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn, rt_type_offset_arr[i], cdu_seg_params);
1374dbb799c3SYuval Mintz 
1375dbb799c3SYuval Mintz 		offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1376dbb799c3SYuval Mintz 			  (p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)].start_line -
1377dbb799c3SYuval Mintz 			   p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1378dbb799c3SYuval Mintz 
1379dbb799c3SYuval Mintz 		cdu_seg_params = 0;
1380dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1381dbb799c3SYuval Mintz 		SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1382dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn, rt_type_offset_fl_arr[i], cdu_seg_params);
1383dbb799c3SYuval Mintz 	}
1384fe56b9e6SYuval Mintz }
1385fe56b9e6SYuval Mintz 
1386fe56b9e6SYuval Mintz void qed_qm_init_pf(struct qed_hwfn *p_hwfn)
1387fe56b9e6SYuval Mintz {
1388fe56b9e6SYuval Mintz 	struct qed_qm_pf_rt_init_params params;
1389fe56b9e6SYuval Mintz 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1390fe56b9e6SYuval Mintz 	struct qed_qm_iids iids;
1391fe56b9e6SYuval Mintz 
1392fe56b9e6SYuval Mintz 	memset(&iids, 0, sizeof(iids));
1393fe56b9e6SYuval Mintz 	qed_cxt_qm_iids(p_hwfn, &iids);
1394fe56b9e6SYuval Mintz 
1395fe56b9e6SYuval Mintz 	memset(&params, 0, sizeof(params));
1396fe56b9e6SYuval Mintz 	params.port_id = p_hwfn->port_id;
1397fe56b9e6SYuval Mintz 	params.pf_id = p_hwfn->rel_pf_id;
1398fe56b9e6SYuval Mintz 	params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
1399fe56b9e6SYuval Mintz 	params.is_first_pf = p_hwfn->first_on_engine;
1400fe56b9e6SYuval Mintz 	params.num_pf_cids = iids.cids;
14011408cc1fSYuval Mintz 	params.num_vf_cids = iids.vf_cids;
1402fe56b9e6SYuval Mintz 	params.start_pq = qm_info->start_pq;
14031408cc1fSYuval Mintz 	params.num_pf_pqs = qm_info->num_pqs - qm_info->num_vf_pqs;
14041408cc1fSYuval Mintz 	params.num_vf_pqs = qm_info->num_vf_pqs;
1405fc48b7a6SYuval Mintz 	params.start_vport = qm_info->start_vport;
1406fc48b7a6SYuval Mintz 	params.num_vports = qm_info->num_vports;
1407fe56b9e6SYuval Mintz 	params.pf_wfq = qm_info->pf_wfq;
1408fe56b9e6SYuval Mintz 	params.pf_rl = qm_info->pf_rl;
1409fe56b9e6SYuval Mintz 	params.pq_params = qm_info->qm_pq_params;
1410fe56b9e6SYuval Mintz 	params.vport_params = qm_info->qm_vport_params;
1411fe56b9e6SYuval Mintz 
1412fe56b9e6SYuval Mintz 	qed_qm_pf_rt_init(p_hwfn, p_hwfn->p_main_ptt, &params);
1413fe56b9e6SYuval Mintz }
1414fe56b9e6SYuval Mintz 
1415fe56b9e6SYuval Mintz /* CM PF */
1416b5a9ee7cSAriel Elior void qed_cm_init_pf(struct qed_hwfn *p_hwfn)
1417fe56b9e6SYuval Mintz {
1418fe56b9e6SYuval Mintz 	/* XCM pure-LB queue */
1419b5a9ee7cSAriel Elior 	STORE_RT_REG(p_hwfn, XCM_REG_CON_PHY_Q3_RT_OFFSET,
1420b5a9ee7cSAriel Elior 		     qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB));
1421fe56b9e6SYuval Mintz }
1422fe56b9e6SYuval Mintz 
1423fe56b9e6SYuval Mintz /* DQ PF */
1424fe56b9e6SYuval Mintz static void qed_dq_init_pf(struct qed_hwfn *p_hwfn)
1425fe56b9e6SYuval Mintz {
1426fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
14271408cc1fSYuval Mintz 	u32 dq_pf_max_cid = 0, dq_vf_max_cid = 0;
1428fe56b9e6SYuval Mintz 
1429fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[0].cid_count >> DQ_RANGE_SHIFT);
1430fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_0_RT_OFFSET, dq_pf_max_cid);
1431fe56b9e6SYuval Mintz 
14321408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[0].cids_per_vf >> DQ_RANGE_SHIFT);
14331408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_0_RT_OFFSET, dq_vf_max_cid);
14341408cc1fSYuval Mintz 
1435fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[1].cid_count >> DQ_RANGE_SHIFT);
1436fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_1_RT_OFFSET, dq_pf_max_cid);
1437fe56b9e6SYuval Mintz 
14381408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[1].cids_per_vf >> DQ_RANGE_SHIFT);
14391408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_1_RT_OFFSET, dq_vf_max_cid);
14401408cc1fSYuval Mintz 
1441fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[2].cid_count >> DQ_RANGE_SHIFT);
1442fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_2_RT_OFFSET, dq_pf_max_cid);
1443fe56b9e6SYuval Mintz 
14441408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[2].cids_per_vf >> DQ_RANGE_SHIFT);
14451408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_2_RT_OFFSET, dq_vf_max_cid);
14461408cc1fSYuval Mintz 
1447fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[3].cid_count >> DQ_RANGE_SHIFT);
1448fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_3_RT_OFFSET, dq_pf_max_cid);
1449fe56b9e6SYuval Mintz 
14501408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[3].cids_per_vf >> DQ_RANGE_SHIFT);
14511408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_3_RT_OFFSET, dq_vf_max_cid);
14521408cc1fSYuval Mintz 
1453fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[4].cid_count >> DQ_RANGE_SHIFT);
1454fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_4_RT_OFFSET, dq_pf_max_cid);
1455fe56b9e6SYuval Mintz 
14561408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[4].cids_per_vf >> DQ_RANGE_SHIFT);
14571408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_4_RT_OFFSET, dq_vf_max_cid);
14581408cc1fSYuval Mintz 
1459fe56b9e6SYuval Mintz 	dq_pf_max_cid += (p_mngr->conn_cfg[5].cid_count >> DQ_RANGE_SHIFT);
1460fe56b9e6SYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_5_RT_OFFSET, dq_pf_max_cid);
14611408cc1fSYuval Mintz 
14621408cc1fSYuval Mintz 	dq_vf_max_cid += (p_mngr->conn_cfg[5].cids_per_vf >> DQ_RANGE_SHIFT);
14631408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_5_RT_OFFSET, dq_vf_max_cid);
14641408cc1fSYuval Mintz 
14651408cc1fSYuval Mintz 	/* Connection types 6 & 7 are not in use, yet they must be configured
14661408cc1fSYuval Mintz 	 * as the highest possible connection. Not configuring them means the
14671408cc1fSYuval Mintz 	 * defaults will be  used, and with a large number of cids a bug may
14681408cc1fSYuval Mintz 	 * occur, if the defaults will be smaller than dq_pf_max_cid /
14691408cc1fSYuval Mintz 	 * dq_vf_max_cid.
14701408cc1fSYuval Mintz 	 */
14711408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_6_RT_OFFSET, dq_pf_max_cid);
14721408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_6_RT_OFFSET, dq_vf_max_cid);
14731408cc1fSYuval Mintz 
14741408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_7_RT_OFFSET, dq_pf_max_cid);
14751408cc1fSYuval Mintz 	STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_7_RT_OFFSET, dq_vf_max_cid);
1476fe56b9e6SYuval Mintz }
1477fe56b9e6SYuval Mintz 
1478fe56b9e6SYuval Mintz static void qed_ilt_bounds_init(struct qed_hwfn *p_hwfn)
1479fe56b9e6SYuval Mintz {
1480fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *ilt_clients;
1481fe56b9e6SYuval Mintz 	int i;
1482fe56b9e6SYuval Mintz 
1483fe56b9e6SYuval Mintz 	ilt_clients = p_hwfn->p_cxt_mngr->clients;
1484fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, ilt_clients) {
1485fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1486fe56b9e6SYuval Mintz 			     ilt_clients[i].first.reg,
1487fe56b9e6SYuval Mintz 			     ilt_clients[i].first.val);
1488fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1489dbb799c3SYuval Mintz 			     ilt_clients[i].last.reg, ilt_clients[i].last.val);
1490fe56b9e6SYuval Mintz 		STORE_RT_REG(p_hwfn,
1491fe56b9e6SYuval Mintz 			     ilt_clients[i].p_size.reg,
1492fe56b9e6SYuval Mintz 			     ilt_clients[i].p_size.val);
1493fe56b9e6SYuval Mintz 	}
1494fe56b9e6SYuval Mintz }
1495fe56b9e6SYuval Mintz 
14961408cc1fSYuval Mintz static void qed_ilt_vf_bounds_init(struct qed_hwfn *p_hwfn)
14971408cc1fSYuval Mintz {
14981408cc1fSYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
14991408cc1fSYuval Mintz 	u32 blk_factor;
15001408cc1fSYuval Mintz 
15011408cc1fSYuval Mintz 	/* For simplicty  we set the 'block' to be an ILT page */
15021408cc1fSYuval Mintz 	if (p_hwfn->cdev->p_iov_info) {
15031408cc1fSYuval Mintz 		struct qed_hw_sriov_info *p_iov = p_hwfn->cdev->p_iov_info;
15041408cc1fSYuval Mintz 
15051408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
15061408cc1fSYuval Mintz 			     PSWRQ2_REG_VF_BASE_RT_OFFSET,
15071408cc1fSYuval Mintz 			     p_iov->first_vf_in_pf);
15081408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
15091408cc1fSYuval Mintz 			     PSWRQ2_REG_VF_LAST_ILT_RT_OFFSET,
15101408cc1fSYuval Mintz 			     p_iov->first_vf_in_pf + p_iov->total_vfs);
15111408cc1fSYuval Mintz 	}
15121408cc1fSYuval Mintz 
15131408cc1fSYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
15141408cc1fSYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
15151408cc1fSYuval Mintz 	if (p_cli->active) {
15161408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
15171408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_BLOCKS_FACTOR_RT_OFFSET,
15181408cc1fSYuval Mintz 			     blk_factor);
15191408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
15201408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
15211408cc1fSYuval Mintz 			     p_cli->pf_total_lines);
15221408cc1fSYuval Mintz 		STORE_RT_REG(p_hwfn,
15231408cc1fSYuval Mintz 			     PSWRQ2_REG_CDUC_VF_BLOCKS_RT_OFFSET,
15241408cc1fSYuval Mintz 			     p_cli->vf_total_lines);
15251408cc1fSYuval Mintz 	}
1526dbb799c3SYuval Mintz 
1527dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1528dbb799c3SYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1529dbb799c3SYuval Mintz 	if (p_cli->active) {
1530dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1531dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_BLOCKS_FACTOR_RT_OFFSET,
1532dbb799c3SYuval Mintz 			     blk_factor);
1533dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1534dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1535dbb799c3SYuval Mintz 			     p_cli->pf_total_lines);
1536dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1537dbb799c3SYuval Mintz 			     PSWRQ2_REG_CDUT_VF_BLOCKS_RT_OFFSET,
1538dbb799c3SYuval Mintz 			     p_cli->vf_total_lines);
1539dbb799c3SYuval Mintz 	}
1540dbb799c3SYuval Mintz 
1541dbb799c3SYuval Mintz 	p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TM];
1542dbb799c3SYuval Mintz 	blk_factor = ilog2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1543dbb799c3SYuval Mintz 	if (p_cli->active) {
1544dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1545dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_BLOCKS_FACTOR_RT_OFFSET, blk_factor);
1546dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1547dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1548dbb799c3SYuval Mintz 			     p_cli->pf_total_lines);
1549dbb799c3SYuval Mintz 		STORE_RT_REG(p_hwfn,
1550dbb799c3SYuval Mintz 			     PSWRQ2_REG_TM_VF_BLOCKS_RT_OFFSET,
1551dbb799c3SYuval Mintz 			     p_cli->vf_total_lines);
1552dbb799c3SYuval Mintz 	}
15531408cc1fSYuval Mintz }
15541408cc1fSYuval Mintz 
1555fe56b9e6SYuval Mintz /* ILT (PSWRQ2) PF */
1556fe56b9e6SYuval Mintz static void qed_ilt_init_pf(struct qed_hwfn *p_hwfn)
1557fe56b9e6SYuval Mintz {
1558fe56b9e6SYuval Mintz 	struct qed_ilt_client_cfg *clients;
1559fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr;
1560fe56b9e6SYuval Mintz 	struct qed_dma_mem *p_shdw;
1561fe56b9e6SYuval Mintz 	u32 line, rt_offst, i;
1562fe56b9e6SYuval Mintz 
1563fe56b9e6SYuval Mintz 	qed_ilt_bounds_init(p_hwfn);
15641408cc1fSYuval Mintz 	qed_ilt_vf_bounds_init(p_hwfn);
1565fe56b9e6SYuval Mintz 
1566fe56b9e6SYuval Mintz 	p_mngr = p_hwfn->p_cxt_mngr;
1567fe56b9e6SYuval Mintz 	p_shdw = p_mngr->ilt_shadow;
1568fe56b9e6SYuval Mintz 	clients = p_hwfn->p_cxt_mngr->clients;
1569fe56b9e6SYuval Mintz 
1570fe56b9e6SYuval Mintz 	for_each_ilt_valid_client(i, clients) {
1571fe56b9e6SYuval Mintz 		/** Client's 1st val and RT array are absolute, ILT shadows'
1572fe56b9e6SYuval Mintz 		 *  lines are relative.
1573fe56b9e6SYuval Mintz 		 */
1574fe56b9e6SYuval Mintz 		line = clients[i].first.val - p_mngr->pf_start_line;
1575fe56b9e6SYuval Mintz 		rt_offst = PSWRQ2_REG_ILT_MEMORY_RT_OFFSET +
1576fe56b9e6SYuval Mintz 			   clients[i].first.val * ILT_ENTRY_IN_REGS;
1577fe56b9e6SYuval Mintz 
1578fe56b9e6SYuval Mintz 		for (; line <= clients[i].last.val - p_mngr->pf_start_line;
1579fe56b9e6SYuval Mintz 		     line++, rt_offst += ILT_ENTRY_IN_REGS) {
1580fe56b9e6SYuval Mintz 			u64 ilt_hw_entry = 0;
1581fe56b9e6SYuval Mintz 
1582fe56b9e6SYuval Mintz 			/** p_virt could be NULL incase of dynamic
1583fe56b9e6SYuval Mintz 			 *  allocation
1584fe56b9e6SYuval Mintz 			 */
1585fe56b9e6SYuval Mintz 			if (p_shdw[line].p_virt) {
1586fe56b9e6SYuval Mintz 				SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
1587fe56b9e6SYuval Mintz 				SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
1588fe56b9e6SYuval Mintz 					  (p_shdw[line].p_phys >> 12));
1589fe56b9e6SYuval Mintz 
1590fe56b9e6SYuval Mintz 				DP_VERBOSE(p_hwfn, QED_MSG_ILT,
1591fe56b9e6SYuval Mintz 					   "Setting RT[0x%08x] from ILT[0x%08x] [Client is %d] to Physical addr: 0x%llx\n",
1592fe56b9e6SYuval Mintz 					   rt_offst, line, i,
1593fe56b9e6SYuval Mintz 					   (u64)(p_shdw[line].p_phys >> 12));
1594fe56b9e6SYuval Mintz 			}
1595fe56b9e6SYuval Mintz 
1596fe56b9e6SYuval Mintz 			STORE_RT_REG_AGG(p_hwfn, rt_offst, ilt_hw_entry);
1597fe56b9e6SYuval Mintz 		}
1598fe56b9e6SYuval Mintz 	}
1599fe56b9e6SYuval Mintz }
1600fe56b9e6SYuval Mintz 
1601dbb799c3SYuval Mintz /* SRC (Searcher) PF */
1602dbb799c3SYuval Mintz static void qed_src_init_pf(struct qed_hwfn *p_hwfn)
1603dbb799c3SYuval Mintz {
1604dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1605dbb799c3SYuval Mintz 	u32 rounded_conn_num, conn_num, conn_max;
1606dbb799c3SYuval Mintz 	struct qed_src_iids src_iids;
1607dbb799c3SYuval Mintz 
1608dbb799c3SYuval Mintz 	memset(&src_iids, 0, sizeof(src_iids));
1609dbb799c3SYuval Mintz 	qed_cxt_src_iids(p_mngr, &src_iids);
1610dbb799c3SYuval Mintz 	conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
1611dbb799c3SYuval Mintz 	if (!conn_num)
1612dbb799c3SYuval Mintz 		return;
1613dbb799c3SYuval Mintz 
1614dbb799c3SYuval Mintz 	conn_max = max_t(u32, conn_num, SRC_MIN_NUM_ELEMS);
1615dbb799c3SYuval Mintz 	rounded_conn_num = roundup_pow_of_two(conn_max);
1616dbb799c3SYuval Mintz 
1617dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, SRC_REG_COUNTFREE_RT_OFFSET, conn_num);
1618dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, SRC_REG_NUMBER_HASH_BITS_RT_OFFSET,
1619dbb799c3SYuval Mintz 		     ilog2(rounded_conn_num));
1620dbb799c3SYuval Mintz 
1621dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, SRC_REG_FIRSTFREE_RT_OFFSET,
1622dbb799c3SYuval Mintz 			 p_hwfn->p_cxt_mngr->first_free);
1623dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, SRC_REG_LASTFREE_RT_OFFSET,
1624dbb799c3SYuval Mintz 			 p_hwfn->p_cxt_mngr->last_free);
1625dbb799c3SYuval Mintz }
1626dbb799c3SYuval Mintz 
1627dbb799c3SYuval Mintz /* Timers PF */
1628dbb799c3SYuval Mintz #define TM_CFG_NUM_IDS_SHIFT            0
1629dbb799c3SYuval Mintz #define TM_CFG_NUM_IDS_MASK             0xFFFFULL
1630dbb799c3SYuval Mintz #define TM_CFG_PRE_SCAN_OFFSET_SHIFT    16
1631dbb799c3SYuval Mintz #define TM_CFG_PRE_SCAN_OFFSET_MASK     0x1FFULL
1632dbb799c3SYuval Mintz #define TM_CFG_PARENT_PF_SHIFT          25
1633dbb799c3SYuval Mintz #define TM_CFG_PARENT_PF_MASK           0x7ULL
1634dbb799c3SYuval Mintz 
1635dbb799c3SYuval Mintz #define TM_CFG_CID_PRE_SCAN_ROWS_SHIFT  30
1636dbb799c3SYuval Mintz #define TM_CFG_CID_PRE_SCAN_ROWS_MASK   0x1FFULL
1637dbb799c3SYuval Mintz 
1638dbb799c3SYuval Mintz #define TM_CFG_TID_OFFSET_SHIFT         30
1639dbb799c3SYuval Mintz #define TM_CFG_TID_OFFSET_MASK          0x7FFFFULL
1640dbb799c3SYuval Mintz #define TM_CFG_TID_PRE_SCAN_ROWS_SHIFT  49
1641dbb799c3SYuval Mintz #define TM_CFG_TID_PRE_SCAN_ROWS_MASK   0x1FFULL
1642dbb799c3SYuval Mintz 
1643dbb799c3SYuval Mintz static void qed_tm_init_pf(struct qed_hwfn *p_hwfn)
1644dbb799c3SYuval Mintz {
1645dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1646dbb799c3SYuval Mintz 	u32 active_seg_mask = 0, tm_offset, rt_reg;
1647dbb799c3SYuval Mintz 	struct qed_tm_iids tm_iids;
1648dbb799c3SYuval Mintz 	u64 cfg_word;
1649dbb799c3SYuval Mintz 	u8 i;
1650dbb799c3SYuval Mintz 
1651dbb799c3SYuval Mintz 	memset(&tm_iids, 0, sizeof(tm_iids));
165244531ba4SMichal Kalderon 	qed_cxt_tm_iids(p_hwfn, p_mngr, &tm_iids);
1653dbb799c3SYuval Mintz 
1654dbb799c3SYuval Mintz 	/* @@@TBD No pre-scan for now */
1655dbb799c3SYuval Mintz 
1656dbb799c3SYuval Mintz 	/* Note: We assume consecutive VFs for a PF */
1657dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->vf_count; i++) {
1658dbb799c3SYuval Mintz 		cfg_word = 0;
1659dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_cids);
1660dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1661dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1662dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);
1663dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1664dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1665dbb799c3SYuval Mintz 		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
1666dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1667dbb799c3SYuval Mintz 	}
1668dbb799c3SYuval Mintz 
1669dbb799c3SYuval Mintz 	cfg_word = 0;
1670dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_cids);
1671dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1672dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);	/* n/a for PF */
1673dbb799c3SYuval Mintz 	SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0);	/* scan all   */
1674dbb799c3SYuval Mintz 
1675dbb799c3SYuval Mintz 	rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1676dbb799c3SYuval Mintz 	    (sizeof(cfg_word) / sizeof(u32)) *
1677dbb799c3SYuval Mintz 	    (NUM_OF_VFS(p_hwfn->cdev) + p_hwfn->rel_pf_id);
1678dbb799c3SYuval Mintz 	STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1679dbb799c3SYuval Mintz 
1680dbb799c3SYuval Mintz 	/* enale scan */
1681dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_CONN_RT_OFFSET,
1682dbb799c3SYuval Mintz 		     tm_iids.pf_cids ? 0x1 : 0x0);
1683dbb799c3SYuval Mintz 
1684dbb799c3SYuval Mintz 	/* @@@TBD how to enable the scan for the VFs */
1685dbb799c3SYuval Mintz 
1686dbb799c3SYuval Mintz 	tm_offset = tm_iids.per_vf_cids;
1687dbb799c3SYuval Mintz 
1688dbb799c3SYuval Mintz 	/* Note: We assume consecutive VFs for a PF */
1689dbb799c3SYuval Mintz 	for (i = 0; i < p_mngr->vf_count; i++) {
1690dbb799c3SYuval Mintz 		cfg_word = 0;
1691dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_tids);
1692dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1693dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1694dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1695dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
1696dbb799c3SYuval Mintz 
1697dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1698dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1699dbb799c3SYuval Mintz 		    (p_hwfn->cdev->p_iov_info->first_vf_in_pf + i);
1700dbb799c3SYuval Mintz 
1701dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1702dbb799c3SYuval Mintz 	}
1703dbb799c3SYuval Mintz 
1704dbb799c3SYuval Mintz 	tm_offset = tm_iids.pf_cids;
1705dbb799c3SYuval Mintz 	for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1706dbb799c3SYuval Mintz 		cfg_word = 0;
1707dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_tids[i]);
1708dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1709dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);
1710dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1711dbb799c3SYuval Mintz 		SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64) 0);
1712dbb799c3SYuval Mintz 
1713dbb799c3SYuval Mintz 		rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1714dbb799c3SYuval Mintz 		    (sizeof(cfg_word) / sizeof(u32)) *
1715dbb799c3SYuval Mintz 		    (NUM_OF_VFS(p_hwfn->cdev) +
1716dbb799c3SYuval Mintz 		     p_hwfn->rel_pf_id * NUM_TASK_PF_SEGMENTS + i);
1717dbb799c3SYuval Mintz 
1718dbb799c3SYuval Mintz 		STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
17191a635e48SYuval Mintz 		active_seg_mask |= (tm_iids.pf_tids[i] ? BIT(i) : 0);
1720dbb799c3SYuval Mintz 
1721dbb799c3SYuval Mintz 		tm_offset += tm_iids.pf_tids[i];
1722dbb799c3SYuval Mintz 	}
1723dbb799c3SYuval Mintz 
1724dbb799c3SYuval Mintz 	if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE)
1725dbb799c3SYuval Mintz 		active_seg_mask = 0;
1726dbb799c3SYuval Mintz 
1727dbb799c3SYuval Mintz 	STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_TASK_RT_OFFSET, active_seg_mask);
1728dbb799c3SYuval Mintz 
1729dbb799c3SYuval Mintz 	/* @@@TBD how to enable the scan for the VFs */
1730dbb799c3SYuval Mintz }
1731dbb799c3SYuval Mintz 
17321e128c81SArun Easi static void qed_prs_init_common(struct qed_hwfn *p_hwfn)
17331e128c81SArun Easi {
17341e128c81SArun Easi 	if ((p_hwfn->hw_info.personality == QED_PCI_FCOE) &&
17351e128c81SArun Easi 	    p_hwfn->pf_params.fcoe_pf_params.is_target)
17361e128c81SArun Easi 		STORE_RT_REG(p_hwfn,
17371e128c81SArun Easi 			     PRS_REG_SEARCH_RESP_INITIATOR_TYPE_RT_OFFSET, 0);
17381e128c81SArun Easi }
17391e128c81SArun Easi 
17401e128c81SArun Easi static void qed_prs_init_pf(struct qed_hwfn *p_hwfn)
17411e128c81SArun Easi {
17421e128c81SArun Easi 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
17431e128c81SArun Easi 	struct qed_conn_type_cfg *p_fcoe;
17441e128c81SArun Easi 	struct qed_tid_seg *p_tid;
17451e128c81SArun Easi 
17461e128c81SArun Easi 	p_fcoe = &p_mngr->conn_cfg[PROTOCOLID_FCOE];
17471e128c81SArun Easi 
17481e128c81SArun Easi 	/* If FCoE is active set the MAX OX_ID (tid) in the Parser */
17491e128c81SArun Easi 	if (!p_fcoe->cid_count)
17501e128c81SArun Easi 		return;
17511e128c81SArun Easi 
17521e128c81SArun Easi 	p_tid = &p_fcoe->tid_seg[QED_CXT_FCOE_TID_SEG];
17531e128c81SArun Easi 	if (p_hwfn->pf_params.fcoe_pf_params.is_target) {
17541e128c81SArun Easi 		STORE_RT_REG_AGG(p_hwfn,
17551e128c81SArun Easi 				 PRS_REG_TASK_ID_MAX_TARGET_PF_RT_OFFSET,
17561e128c81SArun Easi 				 p_tid->count);
17571e128c81SArun Easi 	} else {
17581e128c81SArun Easi 		STORE_RT_REG_AGG(p_hwfn,
17591e128c81SArun Easi 				 PRS_REG_TASK_ID_MAX_INITIATOR_PF_RT_OFFSET,
17601e128c81SArun Easi 				 p_tid->count);
17611e128c81SArun Easi 	}
17621e128c81SArun Easi }
17631e128c81SArun Easi 
1764fe56b9e6SYuval Mintz void qed_cxt_hw_init_common(struct qed_hwfn *p_hwfn)
1765fe56b9e6SYuval Mintz {
1766fe56b9e6SYuval Mintz 	qed_cdu_init_common(p_hwfn);
17671e128c81SArun Easi 	qed_prs_init_common(p_hwfn);
1768fe56b9e6SYuval Mintz }
1769fe56b9e6SYuval Mintz 
1770fe56b9e6SYuval Mintz void qed_cxt_hw_init_pf(struct qed_hwfn *p_hwfn)
1771fe56b9e6SYuval Mintz {
1772fe56b9e6SYuval Mintz 	qed_qm_init_pf(p_hwfn);
1773fe56b9e6SYuval Mintz 	qed_cm_init_pf(p_hwfn);
1774fe56b9e6SYuval Mintz 	qed_dq_init_pf(p_hwfn);
1775dbb799c3SYuval Mintz 	qed_cdu_init_pf(p_hwfn);
1776fe56b9e6SYuval Mintz 	qed_ilt_init_pf(p_hwfn);
1777dbb799c3SYuval Mintz 	qed_src_init_pf(p_hwfn);
1778dbb799c3SYuval Mintz 	qed_tm_init_pf(p_hwfn);
17791e128c81SArun Easi 	qed_prs_init_pf(p_hwfn);
1780fe56b9e6SYuval Mintz }
1781fe56b9e6SYuval Mintz 
1782fe56b9e6SYuval Mintz int qed_cxt_acquire_cid(struct qed_hwfn *p_hwfn,
17831a635e48SYuval Mintz 			enum protocol_type type, u32 *p_cid)
1784fe56b9e6SYuval Mintz {
1785fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1786fe56b9e6SYuval Mintz 	u32 rel_cid;
1787fe56b9e6SYuval Mintz 
1788fe56b9e6SYuval Mintz 	if (type >= MAX_CONN_TYPES || !p_mngr->acquired[type].cid_map) {
1789fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Invalid protocol type %d", type);
1790fe56b9e6SYuval Mintz 		return -EINVAL;
1791fe56b9e6SYuval Mintz 	}
1792fe56b9e6SYuval Mintz 
1793fe56b9e6SYuval Mintz 	rel_cid = find_first_zero_bit(p_mngr->acquired[type].cid_map,
1794fe56b9e6SYuval Mintz 				      p_mngr->acquired[type].max_count);
1795fe56b9e6SYuval Mintz 
1796fe56b9e6SYuval Mintz 	if (rel_cid >= p_mngr->acquired[type].max_count) {
17971a635e48SYuval Mintz 		DP_NOTICE(p_hwfn, "no CID available for protocol %d\n", type);
1798fe56b9e6SYuval Mintz 		return -EINVAL;
1799fe56b9e6SYuval Mintz 	}
1800fe56b9e6SYuval Mintz 
1801fe56b9e6SYuval Mintz 	__set_bit(rel_cid, p_mngr->acquired[type].cid_map);
1802fe56b9e6SYuval Mintz 
1803fe56b9e6SYuval Mintz 	*p_cid = rel_cid + p_mngr->acquired[type].start_cid;
1804fe56b9e6SYuval Mintz 
1805fe56b9e6SYuval Mintz 	return 0;
1806fe56b9e6SYuval Mintz }
1807fe56b9e6SYuval Mintz 
1808fe56b9e6SYuval Mintz static bool qed_cxt_test_cid_acquired(struct qed_hwfn *p_hwfn,
18091a635e48SYuval Mintz 				      u32 cid, enum protocol_type *p_type)
1810fe56b9e6SYuval Mintz {
1811fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1812fe56b9e6SYuval Mintz 	struct qed_cid_acquired_map *p_map;
1813fe56b9e6SYuval Mintz 	enum protocol_type p;
1814fe56b9e6SYuval Mintz 	u32 rel_cid;
1815fe56b9e6SYuval Mintz 
1816fe56b9e6SYuval Mintz 	/* Iterate over protocols and find matching cid range */
1817fe56b9e6SYuval Mintz 	for (p = 0; p < MAX_CONN_TYPES; p++) {
1818fe56b9e6SYuval Mintz 		p_map = &p_mngr->acquired[p];
1819fe56b9e6SYuval Mintz 
1820fe56b9e6SYuval Mintz 		if (!p_map->cid_map)
1821fe56b9e6SYuval Mintz 			continue;
1822fe56b9e6SYuval Mintz 		if (cid >= p_map->start_cid &&
1823fe56b9e6SYuval Mintz 		    cid < p_map->start_cid + p_map->max_count)
1824fe56b9e6SYuval Mintz 			break;
1825fe56b9e6SYuval Mintz 	}
1826fe56b9e6SYuval Mintz 	*p_type = p;
1827fe56b9e6SYuval Mintz 
1828fe56b9e6SYuval Mintz 	if (p == MAX_CONN_TYPES) {
1829fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Invalid CID %d", cid);
1830fe56b9e6SYuval Mintz 		return false;
1831fe56b9e6SYuval Mintz 	}
1832fe56b9e6SYuval Mintz 
1833fe56b9e6SYuval Mintz 	rel_cid = cid - p_map->start_cid;
1834fe56b9e6SYuval Mintz 	if (!test_bit(rel_cid, p_map->cid_map)) {
1835fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "CID %d not acquired", cid);
1836fe56b9e6SYuval Mintz 		return false;
1837fe56b9e6SYuval Mintz 	}
1838fe56b9e6SYuval Mintz 	return true;
1839fe56b9e6SYuval Mintz }
1840fe56b9e6SYuval Mintz 
18411a635e48SYuval Mintz void qed_cxt_release_cid(struct qed_hwfn *p_hwfn, u32 cid)
1842fe56b9e6SYuval Mintz {
1843fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1844fe56b9e6SYuval Mintz 	enum protocol_type type;
1845fe56b9e6SYuval Mintz 	bool b_acquired;
1846fe56b9e6SYuval Mintz 	u32 rel_cid;
1847fe56b9e6SYuval Mintz 
1848fe56b9e6SYuval Mintz 	/* Test acquired and find matching per-protocol map */
1849fe56b9e6SYuval Mintz 	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, cid, &type);
1850fe56b9e6SYuval Mintz 
1851fe56b9e6SYuval Mintz 	if (!b_acquired)
1852fe56b9e6SYuval Mintz 		return;
1853fe56b9e6SYuval Mintz 
1854fe56b9e6SYuval Mintz 	rel_cid = cid - p_mngr->acquired[type].start_cid;
1855fe56b9e6SYuval Mintz 	__clear_bit(rel_cid, p_mngr->acquired[type].cid_map);
1856fe56b9e6SYuval Mintz }
1857fe56b9e6SYuval Mintz 
18581a635e48SYuval Mintz int qed_cxt_get_cid_info(struct qed_hwfn *p_hwfn, struct qed_cxt_info *p_info)
1859fe56b9e6SYuval Mintz {
1860fe56b9e6SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1861fe56b9e6SYuval Mintz 	u32 conn_cxt_size, hw_p_size, cxts_per_p, line;
1862fe56b9e6SYuval Mintz 	enum protocol_type type;
1863fe56b9e6SYuval Mintz 	bool b_acquired;
1864fe56b9e6SYuval Mintz 
1865fe56b9e6SYuval Mintz 	/* Test acquired and find matching per-protocol map */
1866fe56b9e6SYuval Mintz 	b_acquired = qed_cxt_test_cid_acquired(p_hwfn, p_info->iid, &type);
1867fe56b9e6SYuval Mintz 
1868fe56b9e6SYuval Mintz 	if (!b_acquired)
1869fe56b9e6SYuval Mintz 		return -EINVAL;
1870fe56b9e6SYuval Mintz 
1871fe56b9e6SYuval Mintz 	/* set the protocl type */
1872fe56b9e6SYuval Mintz 	p_info->type = type;
1873fe56b9e6SYuval Mintz 
1874fe56b9e6SYuval Mintz 	/* compute context virtual pointer */
1875fe56b9e6SYuval Mintz 	hw_p_size = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1876fe56b9e6SYuval Mintz 
1877fe56b9e6SYuval Mintz 	conn_cxt_size = CONN_CXT_SIZE(p_hwfn);
1878fe56b9e6SYuval Mintz 	cxts_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / conn_cxt_size;
1879fe56b9e6SYuval Mintz 	line = p_info->iid / cxts_per_p;
1880fe56b9e6SYuval Mintz 
1881fe56b9e6SYuval Mintz 	/* Make sure context is allocated (dynamic allocation) */
1882fe56b9e6SYuval Mintz 	if (!p_mngr->ilt_shadow[line].p_virt)
1883fe56b9e6SYuval Mintz 		return -EINVAL;
1884fe56b9e6SYuval Mintz 
1885fe56b9e6SYuval Mintz 	p_info->p_cxt = p_mngr->ilt_shadow[line].p_virt +
1886fe56b9e6SYuval Mintz 			p_info->iid % cxts_per_p * conn_cxt_size;
1887fe56b9e6SYuval Mintz 
1888fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, (QED_MSG_ILT | QED_MSG_CXT),
1889fe56b9e6SYuval Mintz 		   "Accessing ILT shadow[%d]: CXT pointer is at %p (for iid %d)\n",
1890fe56b9e6SYuval Mintz 		   p_info->iid / cxts_per_p, p_info->p_cxt, p_info->iid);
1891fe56b9e6SYuval Mintz 
1892fe56b9e6SYuval Mintz 	return 0;
1893fe56b9e6SYuval Mintz }
1894fe56b9e6SYuval Mintz 
18958c93beafSYuval Mintz static void qed_rdma_set_pf_params(struct qed_hwfn *p_hwfn,
1896dbb799c3SYuval Mintz 				   struct qed_rdma_pf_params *p_params)
1897dbb799c3SYuval Mintz {
1898dbb799c3SYuval Mintz 	u32 num_cons, num_tasks, num_qps, num_mrs, num_srqs;
1899dbb799c3SYuval Mintz 	enum protocol_type proto;
1900dbb799c3SYuval Mintz 
1901dbb799c3SYuval Mintz 	num_mrs = min_t(u32, RDMA_MAX_TIDS, p_params->num_mrs);
1902dbb799c3SYuval Mintz 	num_tasks = num_mrs;	/* each mr uses a single task id */
1903dbb799c3SYuval Mintz 	num_srqs = min_t(u32, 32 * 1024, p_params->num_srqs);
1904dbb799c3SYuval Mintz 
1905dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
1906dbb799c3SYuval Mintz 	case QED_PCI_ETH_ROCE:
1907dbb799c3SYuval Mintz 		num_qps = min_t(u32, ROCE_MAX_QPS, p_params->num_qps);
1908dbb799c3SYuval Mintz 		num_cons = num_qps * 2;	/* each QP requires two connections */
1909dbb799c3SYuval Mintz 		proto = PROTOCOLID_ROCE;
1910dbb799c3SYuval Mintz 		break;
1911dbb799c3SYuval Mintz 	default:
1912dbb799c3SYuval Mintz 		return;
1913dbb799c3SYuval Mintz 	}
1914dbb799c3SYuval Mintz 
1915dbb799c3SYuval Mintz 	if (num_cons && num_tasks) {
1916dbb799c3SYuval Mintz 		qed_cxt_set_proto_cid_count(p_hwfn, proto, num_cons, 0);
1917dbb799c3SYuval Mintz 
1918dbb799c3SYuval Mintz 		/* Deliberatly passing ROCE for tasks id. This is because
1919dbb799c3SYuval Mintz 		 * iWARP / RoCE share the task id.
1920dbb799c3SYuval Mintz 		 */
1921dbb799c3SYuval Mintz 		qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_ROCE,
1922dbb799c3SYuval Mintz 					    QED_CXT_ROCE_TID_SEG, 1,
1923dbb799c3SYuval Mintz 					    num_tasks, false);
1924dbb799c3SYuval Mintz 		qed_cxt_set_srq_count(p_hwfn, num_srqs);
1925dbb799c3SYuval Mintz 	} else {
1926dbb799c3SYuval Mintz 		DP_INFO(p_hwfn->cdev,
1927dbb799c3SYuval Mintz 			"RDMA personality used without setting params!\n");
1928dbb799c3SYuval Mintz 	}
1929dbb799c3SYuval Mintz }
1930dbb799c3SYuval Mintz 
1931fe56b9e6SYuval Mintz int qed_cxt_set_pf_params(struct qed_hwfn *p_hwfn)
1932fe56b9e6SYuval Mintz {
1933fe56b9e6SYuval Mintz 	/* Set the number of required CORE connections */
1934fe56b9e6SYuval Mintz 	u32 core_cids = 1; /* SPQ */
1935fe56b9e6SYuval Mintz 
19360a7fb11cSYuval Mintz 	if (p_hwfn->using_ll2)
19370a7fb11cSYuval Mintz 		core_cids += 4;
19381408cc1fSYuval Mintz 	qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_CORE, core_cids, 0);
1939fe56b9e6SYuval Mintz 
1940dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
1941dbb799c3SYuval Mintz 	case QED_PCI_ETH_ROCE:
1942dbb799c3SYuval Mintz 	{
1943dbb799c3SYuval Mintz 		qed_rdma_set_pf_params(p_hwfn,
1944dbb799c3SYuval Mintz 				       &p_hwfn->
1945dbb799c3SYuval Mintz 				       pf_params.rdma_pf_params);
1946dbb799c3SYuval Mintz 		/* no need for break since RoCE coexist with Ethernet */
1947dbb799c3SYuval Mintz 	}
1948dbb799c3SYuval Mintz 	case QED_PCI_ETH:
1949dbb799c3SYuval Mintz 	{
1950dbb799c3SYuval Mintz 		struct qed_eth_pf_params *p_params =
1951dbb799c3SYuval Mintz 		    &p_hwfn->pf_params.eth_pf_params;
1952dbb799c3SYuval Mintz 
1953fe56b9e6SYuval Mintz 		qed_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
19541408cc1fSYuval Mintz 					    p_params->num_cons, 1);
1955dbb799c3SYuval Mintz 		break;
1956dbb799c3SYuval Mintz 	}
19571e128c81SArun Easi 	case QED_PCI_FCOE:
19581e128c81SArun Easi 	{
19591e128c81SArun Easi 		struct qed_fcoe_pf_params *p_params;
19601e128c81SArun Easi 
19611e128c81SArun Easi 		p_params = &p_hwfn->pf_params.fcoe_pf_params;
19621e128c81SArun Easi 
19631e128c81SArun Easi 		if (p_params->num_cons && p_params->num_tasks) {
19641e128c81SArun Easi 			qed_cxt_set_proto_cid_count(p_hwfn,
19651e128c81SArun Easi 						    PROTOCOLID_FCOE,
19661e128c81SArun Easi 						    p_params->num_cons,
19671e128c81SArun Easi 						    0);
19681e128c81SArun Easi 
19691e128c81SArun Easi 			qed_cxt_set_proto_tid_count(p_hwfn, PROTOCOLID_FCOE,
19701e128c81SArun Easi 						    QED_CXT_FCOE_TID_SEG, 0,
19711e128c81SArun Easi 						    p_params->num_tasks, true);
19721e128c81SArun Easi 		} else {
19731e128c81SArun Easi 			DP_INFO(p_hwfn->cdev,
19741e128c81SArun Easi 				"Fcoe personality used without setting params!\n");
19751e128c81SArun Easi 		}
19761e128c81SArun Easi 		break;
19771e128c81SArun Easi 	}
1978dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
1979dbb799c3SYuval Mintz 	{
1980dbb799c3SYuval Mintz 		struct qed_iscsi_pf_params *p_params;
1981dbb799c3SYuval Mintz 
1982dbb799c3SYuval Mintz 		p_params = &p_hwfn->pf_params.iscsi_pf_params;
1983dbb799c3SYuval Mintz 
1984dbb799c3SYuval Mintz 		if (p_params->num_cons && p_params->num_tasks) {
1985dbb799c3SYuval Mintz 			qed_cxt_set_proto_cid_count(p_hwfn,
1986dbb799c3SYuval Mintz 						    PROTOCOLID_ISCSI,
1987dbb799c3SYuval Mintz 						    p_params->num_cons,
1988dbb799c3SYuval Mintz 						    0);
1989dbb799c3SYuval Mintz 
1990dbb799c3SYuval Mintz 			qed_cxt_set_proto_tid_count(p_hwfn,
1991dbb799c3SYuval Mintz 						    PROTOCOLID_ISCSI,
1992dbb799c3SYuval Mintz 						    QED_CXT_ISCSI_TID_SEG,
1993dbb799c3SYuval Mintz 						    0,
1994dbb799c3SYuval Mintz 						    p_params->num_tasks,
1995dbb799c3SYuval Mintz 						    true);
1996dbb799c3SYuval Mintz 		} else {
1997dbb799c3SYuval Mintz 			DP_INFO(p_hwfn->cdev,
1998dbb799c3SYuval Mintz 				"Iscsi personality used without setting params!\n");
1999dbb799c3SYuval Mintz 		}
2000dbb799c3SYuval Mintz 		break;
2001dbb799c3SYuval Mintz 	}
2002dbb799c3SYuval Mintz 	default:
2003dbb799c3SYuval Mintz 		return -EINVAL;
2004dbb799c3SYuval Mintz 	}
2005dbb799c3SYuval Mintz 
2006dbb799c3SYuval Mintz 	return 0;
2007dbb799c3SYuval Mintz }
2008dbb799c3SYuval Mintz 
2009dbb799c3SYuval Mintz int qed_cxt_get_tid_mem_info(struct qed_hwfn *p_hwfn,
2010dbb799c3SYuval Mintz 			     struct qed_tid_mem *p_info)
2011dbb799c3SYuval Mintz {
2012dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2013dbb799c3SYuval Mintz 	u32 proto, seg, total_lines, i, shadow_line;
2014dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2015dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_fl_seg;
2016dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg_info;
2017dbb799c3SYuval Mintz 
2018dbb799c3SYuval Mintz 	/* Verify the personality */
2019dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
20201e128c81SArun Easi 	case QED_PCI_FCOE:
20211e128c81SArun Easi 		proto = PROTOCOLID_FCOE;
20221e128c81SArun Easi 		seg = QED_CXT_FCOE_TID_SEG;
20231e128c81SArun Easi 		break;
2024dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
2025dbb799c3SYuval Mintz 		proto = PROTOCOLID_ISCSI;
2026dbb799c3SYuval Mintz 		seg = QED_CXT_ISCSI_TID_SEG;
2027dbb799c3SYuval Mintz 		break;
2028dbb799c3SYuval Mintz 	default:
2029dbb799c3SYuval Mintz 		return -EINVAL;
2030dbb799c3SYuval Mintz 	}
2031dbb799c3SYuval Mintz 
2032dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
2033dbb799c3SYuval Mintz 	if (!p_cli->active)
2034dbb799c3SYuval Mintz 		return -EINVAL;
2035dbb799c3SYuval Mintz 
2036dbb799c3SYuval Mintz 	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
2037dbb799c3SYuval Mintz 	if (!p_seg_info->has_fl_mem)
2038dbb799c3SYuval Mintz 		return -EINVAL;
2039dbb799c3SYuval Mintz 
2040dbb799c3SYuval Mintz 	p_fl_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
2041dbb799c3SYuval Mintz 	total_lines = DIV_ROUND_UP(p_fl_seg->total_size,
2042dbb799c3SYuval Mintz 				   p_fl_seg->real_size_in_page);
2043dbb799c3SYuval Mintz 
2044dbb799c3SYuval Mintz 	for (i = 0; i < total_lines; i++) {
2045dbb799c3SYuval Mintz 		shadow_line = i + p_fl_seg->start_line -
2046dbb799c3SYuval Mintz 		    p_hwfn->p_cxt_mngr->pf_start_line;
2047dbb799c3SYuval Mintz 		p_info->blocks[i] = p_mngr->ilt_shadow[shadow_line].p_virt;
2048dbb799c3SYuval Mintz 	}
2049dbb799c3SYuval Mintz 	p_info->waste = ILT_PAGE_IN_BYTES(p_cli->p_size.val) -
2050dbb799c3SYuval Mintz 	    p_fl_seg->real_size_in_page;
2051dbb799c3SYuval Mintz 	p_info->tid_size = p_mngr->task_type_size[p_seg_info->type];
2052dbb799c3SYuval Mintz 	p_info->num_tids_per_block = p_fl_seg->real_size_in_page /
2053dbb799c3SYuval Mintz 	    p_info->tid_size;
2054dbb799c3SYuval Mintz 
2055dbb799c3SYuval Mintz 	return 0;
2056dbb799c3SYuval Mintz }
2057dbb799c3SYuval Mintz 
2058dbb799c3SYuval Mintz /* This function is very RoCE oriented, if another protocol in the future
2059dbb799c3SYuval Mintz  * will want this feature we'll need to modify the function to be more generic
2060dbb799c3SYuval Mintz  */
2061dbb799c3SYuval Mintz int
2062dbb799c3SYuval Mintz qed_cxt_dynamic_ilt_alloc(struct qed_hwfn *p_hwfn,
2063dbb799c3SYuval Mintz 			  enum qed_cxt_elem_type elem_type, u32 iid)
2064dbb799c3SYuval Mintz {
2065dbb799c3SYuval Mintz 	u32 reg_offset, shadow_line, elem_size, hw_p_size, elems_per_p, line;
2066dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2067dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
2068dbb799c3SYuval Mintz 	struct qed_ptt *p_ptt;
2069dbb799c3SYuval Mintz 	dma_addr_t p_phys;
2070dbb799c3SYuval Mintz 	u64 ilt_hw_entry;
2071dbb799c3SYuval Mintz 	void *p_virt;
2072dbb799c3SYuval Mintz 	int rc = 0;
2073dbb799c3SYuval Mintz 
2074dbb799c3SYuval Mintz 	switch (elem_type) {
2075dbb799c3SYuval Mintz 	case QED_ELEM_CXT:
2076dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
2077dbb799c3SYuval Mintz 		elem_size = CONN_CXT_SIZE(p_hwfn);
2078dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUC_BLK];
2079dbb799c3SYuval Mintz 		break;
2080dbb799c3SYuval Mintz 	case QED_ELEM_SRQ:
2081dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
2082dbb799c3SYuval Mintz 		elem_size = SRQ_CXT_SIZE;
2083dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
2084dbb799c3SYuval Mintz 		break;
2085dbb799c3SYuval Mintz 	case QED_ELEM_TASK:
2086dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
2087dbb799c3SYuval Mintz 		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
2088dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
2089dbb799c3SYuval Mintz 		break;
2090dbb799c3SYuval Mintz 	default:
2091dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
2092dbb799c3SYuval Mintz 		return -EINVAL;
2093dbb799c3SYuval Mintz 	}
2094dbb799c3SYuval Mintz 
2095dbb799c3SYuval Mintz 	/* Calculate line in ilt */
2096dbb799c3SYuval Mintz 	hw_p_size = p_cli->p_size.val;
2097dbb799c3SYuval Mintz 	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
2098dbb799c3SYuval Mintz 	line = p_blk->start_line + (iid / elems_per_p);
2099dbb799c3SYuval Mintz 	shadow_line = line - p_hwfn->p_cxt_mngr->pf_start_line;
2100dbb799c3SYuval Mintz 
2101dbb799c3SYuval Mintz 	/* If line is already allocated, do nothing, otherwise allocate it and
2102dbb799c3SYuval Mintz 	 * write it to the PSWRQ2 registers.
2103dbb799c3SYuval Mintz 	 * This section can be run in parallel from different contexts and thus
2104dbb799c3SYuval Mintz 	 * a mutex protection is needed.
2105dbb799c3SYuval Mintz 	 */
2106dbb799c3SYuval Mintz 
2107dbb799c3SYuval Mintz 	mutex_lock(&p_hwfn->p_cxt_mngr->mutex);
2108dbb799c3SYuval Mintz 
2109dbb799c3SYuval Mintz 	if (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt)
2110dbb799c3SYuval Mintz 		goto out0;
2111dbb799c3SYuval Mintz 
2112dbb799c3SYuval Mintz 	p_ptt = qed_ptt_acquire(p_hwfn);
2113dbb799c3SYuval Mintz 	if (!p_ptt) {
2114dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn,
2115dbb799c3SYuval Mintz 			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
2116dbb799c3SYuval Mintz 		rc = -EBUSY;
2117dbb799c3SYuval Mintz 		goto out0;
2118dbb799c3SYuval Mintz 	}
2119dbb799c3SYuval Mintz 
2120dbb799c3SYuval Mintz 	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
2121dbb799c3SYuval Mintz 				    p_blk->real_size_in_page,
2122dbb799c3SYuval Mintz 				    &p_phys, GFP_KERNEL);
2123dbb799c3SYuval Mintz 	if (!p_virt) {
2124dbb799c3SYuval Mintz 		rc = -ENOMEM;
2125dbb799c3SYuval Mintz 		goto out1;
2126dbb799c3SYuval Mintz 	}
2127dbb799c3SYuval Mintz 	memset(p_virt, 0, p_blk->real_size_in_page);
2128dbb799c3SYuval Mintz 
2129dbb799c3SYuval Mintz 	/* configuration of refTagMask to 0xF is required for RoCE DIF MR only,
2130dbb799c3SYuval Mintz 	 * to compensate for a HW bug, but it is configured even if DIF is not
2131dbb799c3SYuval Mintz 	 * enabled. This is harmless and allows us to avoid a dedicated API. We
2132dbb799c3SYuval Mintz 	 * configure the field for all of the contexts on the newly allocated
2133dbb799c3SYuval Mintz 	 * page.
2134dbb799c3SYuval Mintz 	 */
2135dbb799c3SYuval Mintz 	if (elem_type == QED_ELEM_TASK) {
2136dbb799c3SYuval Mintz 		u32 elem_i;
2137dbb799c3SYuval Mintz 		u8 *elem_start = (u8 *)p_virt;
2138dbb799c3SYuval Mintz 		union type1_task_context *elem;
2139dbb799c3SYuval Mintz 
2140dbb799c3SYuval Mintz 		for (elem_i = 0; elem_i < elems_per_p; elem_i++) {
2141dbb799c3SYuval Mintz 			elem = (union type1_task_context *)elem_start;
2142dbb799c3SYuval Mintz 			SET_FIELD(elem->roce_ctx.tdif_context.flags1,
2143dbb799c3SYuval Mintz 				  TDIF_TASK_CONTEXT_REFTAGMASK, 0xf);
2144dbb799c3SYuval Mintz 			elem_start += TYPE1_TASK_CXT_SIZE(p_hwfn);
2145dbb799c3SYuval Mintz 		}
2146dbb799c3SYuval Mintz 	}
2147dbb799c3SYuval Mintz 
2148dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt = p_virt;
2149dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys = p_phys;
2150dbb799c3SYuval Mintz 	p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].size =
2151dbb799c3SYuval Mintz 	    p_blk->real_size_in_page;
2152dbb799c3SYuval Mintz 
2153dbb799c3SYuval Mintz 	/* compute absolute offset */
2154dbb799c3SYuval Mintz 	reg_offset = PSWRQ2_REG_ILT_MEMORY +
2155dbb799c3SYuval Mintz 	    (line * ILT_REG_SIZE_IN_BYTES * ILT_ENTRY_IN_REGS);
2156dbb799c3SYuval Mintz 
2157dbb799c3SYuval Mintz 	ilt_hw_entry = 0;
2158dbb799c3SYuval Mintz 	SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
2159dbb799c3SYuval Mintz 	SET_FIELD(ilt_hw_entry,
2160dbb799c3SYuval Mintz 		  ILT_ENTRY_PHY_ADDR,
2161dbb799c3SYuval Mintz 		  (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys >> 12));
2162dbb799c3SYuval Mintz 
2163dbb799c3SYuval Mintz 	/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a wide-bus */
2164dbb799c3SYuval Mintz 	qed_dmae_host2grc(p_hwfn, p_ptt, (u64) (uintptr_t)&ilt_hw_entry,
2165dbb799c3SYuval Mintz 			  reg_offset, sizeof(ilt_hw_entry) / sizeof(u32), 0);
2166dbb799c3SYuval Mintz 
2167dbb799c3SYuval Mintz 	if (elem_type == QED_ELEM_CXT) {
2168dbb799c3SYuval Mintz 		u32 last_cid_allocated = (1 + (iid / elems_per_p)) *
2169dbb799c3SYuval Mintz 		    elems_per_p;
2170dbb799c3SYuval Mintz 
2171dbb799c3SYuval Mintz 		/* Update the relevant register in the parser */
2172dbb799c3SYuval Mintz 		qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF,
2173dbb799c3SYuval Mintz 		       last_cid_allocated - 1);
2174dbb799c3SYuval Mintz 
2175dbb799c3SYuval Mintz 		if (!p_hwfn->b_rdma_enabled_in_prs) {
2176dbb799c3SYuval Mintz 			/* Enable RoCE search */
2177dbb799c3SYuval Mintz 			qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
2178dbb799c3SYuval Mintz 			p_hwfn->b_rdma_enabled_in_prs = true;
2179dbb799c3SYuval Mintz 		}
2180dbb799c3SYuval Mintz 	}
2181dbb799c3SYuval Mintz 
2182dbb799c3SYuval Mintz out1:
2183dbb799c3SYuval Mintz 	qed_ptt_release(p_hwfn, p_ptt);
2184dbb799c3SYuval Mintz out0:
2185dbb799c3SYuval Mintz 	mutex_unlock(&p_hwfn->p_cxt_mngr->mutex);
2186dbb799c3SYuval Mintz 
2187dbb799c3SYuval Mintz 	return rc;
2188dbb799c3SYuval Mintz }
2189dbb799c3SYuval Mintz 
2190dbb799c3SYuval Mintz /* This function is very RoCE oriented, if another protocol in the future
2191dbb799c3SYuval Mintz  * will want this feature we'll need to modify the function to be more generic
2192dbb799c3SYuval Mintz  */
2193dbb799c3SYuval Mintz static int
2194dbb799c3SYuval Mintz qed_cxt_free_ilt_range(struct qed_hwfn *p_hwfn,
2195dbb799c3SYuval Mintz 		       enum qed_cxt_elem_type elem_type,
2196dbb799c3SYuval Mintz 		       u32 start_iid, u32 count)
2197dbb799c3SYuval Mintz {
2198dbb799c3SYuval Mintz 	u32 start_line, end_line, shadow_start_line, shadow_end_line;
2199dbb799c3SYuval Mintz 	u32 reg_offset, elem_size, hw_p_size, elems_per_p;
2200dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2201dbb799c3SYuval Mintz 	struct qed_ilt_cli_blk *p_blk;
2202dbb799c3SYuval Mintz 	u32 end_iid = start_iid + count;
2203dbb799c3SYuval Mintz 	struct qed_ptt *p_ptt;
2204dbb799c3SYuval Mintz 	u64 ilt_hw_entry = 0;
2205dbb799c3SYuval Mintz 	u32 i;
2206dbb799c3SYuval Mintz 
2207dbb799c3SYuval Mintz 	switch (elem_type) {
2208dbb799c3SYuval Mintz 	case QED_ELEM_CXT:
2209dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
2210dbb799c3SYuval Mintz 		elem_size = CONN_CXT_SIZE(p_hwfn);
2211dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUC_BLK];
2212dbb799c3SYuval Mintz 		break;
2213dbb799c3SYuval Mintz 	case QED_ELEM_SRQ:
2214dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
2215dbb799c3SYuval Mintz 		elem_size = SRQ_CXT_SIZE;
2216dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[SRQ_BLK];
2217dbb799c3SYuval Mintz 		break;
2218dbb799c3SYuval Mintz 	case QED_ELEM_TASK:
2219dbb799c3SYuval Mintz 		p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
2220dbb799c3SYuval Mintz 		elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
2221dbb799c3SYuval Mintz 		p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(QED_CXT_ROCE_TID_SEG)];
2222dbb799c3SYuval Mintz 		break;
2223dbb799c3SYuval Mintz 	default:
2224dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn, "-EINVALID elem type = %d", elem_type);
2225dbb799c3SYuval Mintz 		return -EINVAL;
2226dbb799c3SYuval Mintz 	}
2227dbb799c3SYuval Mintz 
2228dbb799c3SYuval Mintz 	/* Calculate line in ilt */
2229dbb799c3SYuval Mintz 	hw_p_size = p_cli->p_size.val;
2230dbb799c3SYuval Mintz 	elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
2231dbb799c3SYuval Mintz 	start_line = p_blk->start_line + (start_iid / elems_per_p);
2232dbb799c3SYuval Mintz 	end_line = p_blk->start_line + (end_iid / elems_per_p);
2233dbb799c3SYuval Mintz 	if (((end_iid + 1) / elems_per_p) != (end_iid / elems_per_p))
2234dbb799c3SYuval Mintz 		end_line--;
2235dbb799c3SYuval Mintz 
2236dbb799c3SYuval Mintz 	shadow_start_line = start_line - p_hwfn->p_cxt_mngr->pf_start_line;
2237dbb799c3SYuval Mintz 	shadow_end_line = end_line - p_hwfn->p_cxt_mngr->pf_start_line;
2238dbb799c3SYuval Mintz 
2239dbb799c3SYuval Mintz 	p_ptt = qed_ptt_acquire(p_hwfn);
2240dbb799c3SYuval Mintz 	if (!p_ptt) {
2241dbb799c3SYuval Mintz 		DP_NOTICE(p_hwfn,
2242dbb799c3SYuval Mintz 			  "QED_TIME_OUT on ptt acquire - dynamic allocation");
2243dbb799c3SYuval Mintz 		return -EBUSY;
2244dbb799c3SYuval Mintz 	}
2245dbb799c3SYuval Mintz 
2246dbb799c3SYuval Mintz 	for (i = shadow_start_line; i < shadow_end_line; i++) {
2247dbb799c3SYuval Mintz 		if (!p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt)
2248dbb799c3SYuval Mintz 			continue;
2249dbb799c3SYuval Mintz 
2250dbb799c3SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
2251dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].size,
2252dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt,
2253dbb799c3SYuval Mintz 				  p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys);
2254dbb799c3SYuval Mintz 
2255dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt = NULL;
2256dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys = 0;
2257dbb799c3SYuval Mintz 		p_hwfn->p_cxt_mngr->ilt_shadow[i].size = 0;
2258dbb799c3SYuval Mintz 
2259dbb799c3SYuval Mintz 		/* compute absolute offset */
2260dbb799c3SYuval Mintz 		reg_offset = PSWRQ2_REG_ILT_MEMORY +
2261dbb799c3SYuval Mintz 		    ((start_line++) * ILT_REG_SIZE_IN_BYTES *
2262dbb799c3SYuval Mintz 		     ILT_ENTRY_IN_REGS);
2263dbb799c3SYuval Mintz 
2264dbb799c3SYuval Mintz 		/* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a
2265dbb799c3SYuval Mintz 		 * wide-bus.
2266dbb799c3SYuval Mintz 		 */
2267dbb799c3SYuval Mintz 		qed_dmae_host2grc(p_hwfn, p_ptt,
2268dbb799c3SYuval Mintz 				  (u64) (uintptr_t) &ilt_hw_entry,
2269dbb799c3SYuval Mintz 				  reg_offset,
2270dbb799c3SYuval Mintz 				  sizeof(ilt_hw_entry) / sizeof(u32),
2271dbb799c3SYuval Mintz 				  0);
2272dbb799c3SYuval Mintz 	}
2273dbb799c3SYuval Mintz 
2274dbb799c3SYuval Mintz 	qed_ptt_release(p_hwfn, p_ptt);
2275dbb799c3SYuval Mintz 
2276dbb799c3SYuval Mintz 	return 0;
2277dbb799c3SYuval Mintz }
2278dbb799c3SYuval Mintz 
2279dbb799c3SYuval Mintz int qed_cxt_free_proto_ilt(struct qed_hwfn *p_hwfn, enum protocol_type proto)
2280dbb799c3SYuval Mintz {
2281dbb799c3SYuval Mintz 	int rc;
2282dbb799c3SYuval Mintz 	u32 cid;
2283dbb799c3SYuval Mintz 
2284dbb799c3SYuval Mintz 	/* Free Connection CXT */
2285dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_CXT,
2286dbb799c3SYuval Mintz 				    qed_cxt_get_proto_cid_start(p_hwfn,
2287dbb799c3SYuval Mintz 								proto),
2288dbb799c3SYuval Mintz 				    qed_cxt_get_proto_cid_count(p_hwfn,
2289dbb799c3SYuval Mintz 								proto, &cid));
2290dbb799c3SYuval Mintz 
2291dbb799c3SYuval Mintz 	if (rc)
2292dbb799c3SYuval Mintz 		return rc;
2293dbb799c3SYuval Mintz 
2294dbb799c3SYuval Mintz 	/* Free Task CXT */
2295dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_TASK, 0,
2296dbb799c3SYuval Mintz 				    qed_cxt_get_proto_tid_count(p_hwfn, proto));
2297dbb799c3SYuval Mintz 	if (rc)
2298dbb799c3SYuval Mintz 		return rc;
2299dbb799c3SYuval Mintz 
2300dbb799c3SYuval Mintz 	/* Free TSDM CXT */
2301dbb799c3SYuval Mintz 	rc = qed_cxt_free_ilt_range(p_hwfn, QED_ELEM_SRQ, 0,
2302dbb799c3SYuval Mintz 				    qed_cxt_get_srq_count(p_hwfn));
2303dbb799c3SYuval Mintz 
2304dbb799c3SYuval Mintz 	return rc;
2305dbb799c3SYuval Mintz }
2306dbb799c3SYuval Mintz 
2307dbb799c3SYuval Mintz int qed_cxt_get_task_ctx(struct qed_hwfn *p_hwfn,
2308dbb799c3SYuval Mintz 			 u32 tid, u8 ctx_type, void **pp_task_ctx)
2309dbb799c3SYuval Mintz {
2310dbb799c3SYuval Mintz 	struct qed_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2311dbb799c3SYuval Mintz 	struct qed_ilt_client_cfg *p_cli;
2312dbb799c3SYuval Mintz 	struct qed_tid_seg *p_seg_info;
23131e128c81SArun Easi 	struct qed_ilt_cli_blk *p_seg;
2314dbb799c3SYuval Mintz 	u32 num_tids_per_block;
23151e128c81SArun Easi 	u32 tid_size, ilt_idx;
23161e128c81SArun Easi 	u32 total_lines;
23171e128c81SArun Easi 	u32 proto, seg;
2318dbb799c3SYuval Mintz 
2319dbb799c3SYuval Mintz 	/* Verify the personality */
2320dbb799c3SYuval Mintz 	switch (p_hwfn->hw_info.personality) {
23211e128c81SArun Easi 	case QED_PCI_FCOE:
23221e128c81SArun Easi 		proto = PROTOCOLID_FCOE;
23231e128c81SArun Easi 		seg = QED_CXT_FCOE_TID_SEG;
23241e128c81SArun Easi 		break;
2325dbb799c3SYuval Mintz 	case QED_PCI_ISCSI:
2326dbb799c3SYuval Mintz 		proto = PROTOCOLID_ISCSI;
2327dbb799c3SYuval Mintz 		seg = QED_CXT_ISCSI_TID_SEG;
2328dbb799c3SYuval Mintz 		break;
2329dbb799c3SYuval Mintz 	default:
2330dbb799c3SYuval Mintz 		return -EINVAL;
2331dbb799c3SYuval Mintz 	}
2332dbb799c3SYuval Mintz 
2333dbb799c3SYuval Mintz 	p_cli = &p_mngr->clients[ILT_CLI_CDUT];
2334dbb799c3SYuval Mintz 	if (!p_cli->active)
2335dbb799c3SYuval Mintz 		return -EINVAL;
2336dbb799c3SYuval Mintz 
2337dbb799c3SYuval Mintz 	p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
2338dbb799c3SYuval Mintz 
2339dbb799c3SYuval Mintz 	if (ctx_type == QED_CTX_WORKING_MEM) {
2340dbb799c3SYuval Mintz 		p_seg = &p_cli->pf_blks[CDUT_SEG_BLK(seg)];
2341dbb799c3SYuval Mintz 	} else if (ctx_type == QED_CTX_FL_MEM) {
2342dbb799c3SYuval Mintz 		if (!p_seg_info->has_fl_mem)
2343dbb799c3SYuval Mintz 			return -EINVAL;
2344dbb799c3SYuval Mintz 		p_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
2345dbb799c3SYuval Mintz 	} else {
2346dbb799c3SYuval Mintz 		return -EINVAL;
2347dbb799c3SYuval Mintz 	}
2348dbb799c3SYuval Mintz 	total_lines = DIV_ROUND_UP(p_seg->total_size, p_seg->real_size_in_page);
2349dbb799c3SYuval Mintz 	tid_size = p_mngr->task_type_size[p_seg_info->type];
2350dbb799c3SYuval Mintz 	num_tids_per_block = p_seg->real_size_in_page / tid_size;
2351dbb799c3SYuval Mintz 
2352dbb799c3SYuval Mintz 	if (total_lines < tid / num_tids_per_block)
2353dbb799c3SYuval Mintz 		return -EINVAL;
2354dbb799c3SYuval Mintz 
2355dbb799c3SYuval Mintz 	ilt_idx = tid / num_tids_per_block + p_seg->start_line -
2356dbb799c3SYuval Mintz 		  p_mngr->pf_start_line;
2357dbb799c3SYuval Mintz 	*pp_task_ctx = (u8 *)p_mngr->ilt_shadow[ilt_idx].p_virt +
2358dbb799c3SYuval Mintz 		       (tid % num_tids_per_block) * tid_size;
2359fe56b9e6SYuval Mintz 
2360fe56b9e6SYuval Mintz 	return 0;
2361fe56b9e6SYuval Mintz }
2362