xref: /dpdk/drivers/net/qede/base/ecore_hw.c (revision 58bb1ee4a50a3ce79241e5b033985a5697081fc2)
13126df22SRasesh Mody /* SPDX-License-Identifier: BSD-3-Clause
29adde217SRasesh Mody  * Copyright (c) 2016 - 2018 Cavium Inc.
3ec94dbc5SRasesh Mody  * All rights reserved.
49adde217SRasesh Mody  * www.cavium.com
5ec94dbc5SRasesh Mody  */
6ec94dbc5SRasesh Mody 
7ec94dbc5SRasesh Mody #include "bcm_osal.h"
8ec94dbc5SRasesh Mody #include "ecore_hsi_common.h"
9ec94dbc5SRasesh Mody #include "ecore_status.h"
10ec94dbc5SRasesh Mody #include "ecore.h"
11ec94dbc5SRasesh Mody #include "ecore_hw.h"
12ec94dbc5SRasesh Mody #include "reg_addr.h"
13ec94dbc5SRasesh Mody #include "ecore_utils.h"
1486a2265eSRasesh Mody #include "ecore_iov_api.h"
157ed1cd53SRasesh Mody #include "ecore_gtt_values.h"
167ed1cd53SRasesh Mody #include "ecore_dev_api.h"
17ec94dbc5SRasesh Mody 
18ec94dbc5SRasesh Mody #ifndef ASIC_ONLY
19ec94dbc5SRasesh Mody #define ECORE_EMUL_FACTOR 2000
20ec94dbc5SRasesh Mody #define ECORE_FPGA_FACTOR 200
21ec94dbc5SRasesh Mody #endif
22ec94dbc5SRasesh Mody 
23ec94dbc5SRasesh Mody #define ECORE_BAR_ACQUIRE_TIMEOUT 1000
24ec94dbc5SRasesh Mody 
25ec94dbc5SRasesh Mody /* Invalid values */
2622d07d93SRasesh Mody #define ECORE_BAR_INVALID_OFFSET	(OSAL_CPU_TO_LE32(-1))
27ec94dbc5SRasesh Mody 
28ec94dbc5SRasesh Mody struct ecore_ptt {
29ec94dbc5SRasesh Mody 	osal_list_entry_t list_entry;
30ec94dbc5SRasesh Mody 	unsigned int idx;
31ec94dbc5SRasesh Mody 	struct pxp_ptt_entry pxp;
3222d07d93SRasesh Mody 	u8 hwfn_id;
33ec94dbc5SRasesh Mody };
34ec94dbc5SRasesh Mody 
35ec94dbc5SRasesh Mody struct ecore_ptt_pool {
36ec94dbc5SRasesh Mody 	osal_list_t free_list;
3722d07d93SRasesh Mody 	osal_spinlock_t lock; /* ptt synchronized access */
38ec94dbc5SRasesh Mody 	struct ecore_ptt ptts[PXP_EXTERNAL_BAR_PF_WINDOW_NUM];
39ec94dbc5SRasesh Mody };
40ec94dbc5SRasesh Mody 
__ecore_ptt_pool_free(struct ecore_hwfn * p_hwfn)4198abf84eSRasesh Mody void __ecore_ptt_pool_free(struct ecore_hwfn *p_hwfn)
4298abf84eSRasesh Mody {
4398abf84eSRasesh Mody 	OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_ptt_pool);
4498abf84eSRasesh Mody 	p_hwfn->p_ptt_pool = OSAL_NULL;
4598abf84eSRasesh Mody }
4698abf84eSRasesh Mody 
ecore_ptt_pool_alloc(struct ecore_hwfn * p_hwfn)47ec94dbc5SRasesh Mody enum _ecore_status_t ecore_ptt_pool_alloc(struct ecore_hwfn *p_hwfn)
48ec94dbc5SRasesh Mody {
4922d07d93SRasesh Mody 	struct ecore_ptt_pool *p_pool = OSAL_ALLOC(p_hwfn->p_dev,
5022d07d93SRasesh Mody 						   GFP_KERNEL,
5122d07d93SRasesh Mody 						   sizeof(*p_pool));
52ec94dbc5SRasesh Mody 	int i;
53ec94dbc5SRasesh Mody 
54ec94dbc5SRasesh Mody 	if (!p_pool)
55ec94dbc5SRasesh Mody 		return ECORE_NOMEM;
56ec94dbc5SRasesh Mody 
57ec94dbc5SRasesh Mody 	OSAL_LIST_INIT(&p_pool->free_list);
58ec94dbc5SRasesh Mody 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
59ec94dbc5SRasesh Mody 		p_pool->ptts[i].idx = i;
60ec94dbc5SRasesh Mody 		p_pool->ptts[i].pxp.offset = ECORE_BAR_INVALID_OFFSET;
61ec94dbc5SRasesh Mody 		p_pool->ptts[i].pxp.pretend.control = 0;
6222d07d93SRasesh Mody 		p_pool->ptts[i].hwfn_id = p_hwfn->my_id;
63ec94dbc5SRasesh Mody 
64ec94dbc5SRasesh Mody 		/* There are special PTT entries that are taken only by design.
65ec94dbc5SRasesh Mody 		 * The rest are added ot the list for general usage.
66ec94dbc5SRasesh Mody 		 */
67ec94dbc5SRasesh Mody 		if (i >= RESERVED_PTT_MAX)
68ec94dbc5SRasesh Mody 			OSAL_LIST_PUSH_HEAD(&p_pool->ptts[i].list_entry,
69ec94dbc5SRasesh Mody 					    &p_pool->free_list);
70ec94dbc5SRasesh Mody 	}
71ec94dbc5SRasesh Mody 
72ec94dbc5SRasesh Mody 	p_hwfn->p_ptt_pool = p_pool;
7322c99696SRasesh Mody #ifdef CONFIG_ECORE_LOCK_ALLOC
7498abf84eSRasesh Mody 	if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_pool->lock)) {
7598abf84eSRasesh Mody 		__ecore_ptt_pool_free(p_hwfn);
7698abf84eSRasesh Mody 		return ECORE_NOMEM;
7798abf84eSRasesh Mody 	}
7822c99696SRasesh Mody #endif
79ec94dbc5SRasesh Mody 	OSAL_SPIN_LOCK_INIT(&p_pool->lock);
80ec94dbc5SRasesh Mody 	return ECORE_SUCCESS;
81ec94dbc5SRasesh Mody }
82ec94dbc5SRasesh Mody 
ecore_gtt_init(struct ecore_hwfn * p_hwfn)837ed1cd53SRasesh Mody void ecore_gtt_init(struct ecore_hwfn *p_hwfn)
847ed1cd53SRasesh Mody {
857ed1cd53SRasesh Mody 	u32 gtt_base;
867ed1cd53SRasesh Mody 	u32 i;
877ed1cd53SRasesh Mody 
887ed1cd53SRasesh Mody 	/* Set the global windows */
897ed1cd53SRasesh Mody 	gtt_base = PXP_PF_WINDOW_ADMIN_START + PXP_PF_WINDOW_ADMIN_GLOBAL_START;
907ed1cd53SRasesh Mody 
917ed1cd53SRasesh Mody 	for (i = 0; i < OSAL_ARRAY_SIZE(pxp_global_win); i++)
927ed1cd53SRasesh Mody 		if (pxp_global_win[i])
937ed1cd53SRasesh Mody 			REG_WR(p_hwfn, gtt_base + i * PXP_GLOBAL_ENTRY_SIZE,
947ed1cd53SRasesh Mody 			       pxp_global_win[i]);
957ed1cd53SRasesh Mody }
967ed1cd53SRasesh Mody 
ecore_ptt_invalidate(struct ecore_hwfn * p_hwfn)97ec94dbc5SRasesh Mody void ecore_ptt_invalidate(struct ecore_hwfn *p_hwfn)
98ec94dbc5SRasesh Mody {
99ec94dbc5SRasesh Mody 	struct ecore_ptt *p_ptt;
100ec94dbc5SRasesh Mody 	int i;
101ec94dbc5SRasesh Mody 
102ec94dbc5SRasesh Mody 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
103ec94dbc5SRasesh Mody 		p_ptt = &p_hwfn->p_ptt_pool->ptts[i];
104ec94dbc5SRasesh Mody 		p_ptt->pxp.offset = ECORE_BAR_INVALID_OFFSET;
105ec94dbc5SRasesh Mody 	}
106ec94dbc5SRasesh Mody }
107ec94dbc5SRasesh Mody 
ecore_ptt_pool_free(struct ecore_hwfn * p_hwfn)108ec94dbc5SRasesh Mody void ecore_ptt_pool_free(struct ecore_hwfn *p_hwfn)
109ec94dbc5SRasesh Mody {
11022c99696SRasesh Mody #ifdef CONFIG_ECORE_LOCK_ALLOC
111ec94dbc5SRasesh Mody 	if (p_hwfn->p_ptt_pool)
112ec94dbc5SRasesh Mody 		OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->p_ptt_pool->lock);
11322c99696SRasesh Mody #endif
11498abf84eSRasesh Mody 	__ecore_ptt_pool_free(p_hwfn);
115ec94dbc5SRasesh Mody }
116ec94dbc5SRasesh Mody 
ecore_ptt_acquire(struct ecore_hwfn * p_hwfn)117ec94dbc5SRasesh Mody struct ecore_ptt *ecore_ptt_acquire(struct ecore_hwfn *p_hwfn)
118ec94dbc5SRasesh Mody {
119ec94dbc5SRasesh Mody 	struct ecore_ptt *p_ptt;
120ec94dbc5SRasesh Mody 	unsigned int i;
121ec94dbc5SRasesh Mody 
122ec94dbc5SRasesh Mody 	/* Take the free PTT from the list */
123ec94dbc5SRasesh Mody 	for (i = 0; i < ECORE_BAR_ACQUIRE_TIMEOUT; i++) {
124ec94dbc5SRasesh Mody 		OSAL_SPIN_LOCK(&p_hwfn->p_ptt_pool->lock);
12522d07d93SRasesh Mody 		if (!OSAL_LIST_IS_EMPTY(&p_hwfn->p_ptt_pool->free_list)) {
12622d07d93SRasesh Mody 			p_ptt = OSAL_LIST_FIRST_ENTRY(
12722d07d93SRasesh Mody 						&p_hwfn->p_ptt_pool->free_list,
12822d07d93SRasesh Mody 						struct ecore_ptt, list_entry);
12922d07d93SRasesh Mody 			OSAL_LIST_REMOVE_ENTRY(&p_ptt->list_entry,
13022d07d93SRasesh Mody 					       &p_hwfn->p_ptt_pool->free_list);
13122d07d93SRasesh Mody 
13222d07d93SRasesh Mody 			OSAL_SPIN_UNLOCK(&p_hwfn->p_ptt_pool->lock);
13322d07d93SRasesh Mody 
13422d07d93SRasesh Mody 			DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
13522d07d93SRasesh Mody 				   "allocated ptt %d\n", p_ptt->idx);
13622d07d93SRasesh Mody 
13722d07d93SRasesh Mody 			return p_ptt;
13822d07d93SRasesh Mody 		}
13922d07d93SRasesh Mody 
140ec94dbc5SRasesh Mody 		OSAL_SPIN_UNLOCK(&p_hwfn->p_ptt_pool->lock);
141ec94dbc5SRasesh Mody 		OSAL_MSLEEP(1);
142ec94dbc5SRasesh Mody 	}
143ec94dbc5SRasesh Mody 
14422d07d93SRasesh Mody 	DP_NOTICE(p_hwfn, true,
14522d07d93SRasesh Mody 		  "PTT acquire timeout - failed to allocate PTT\n");
146ec94dbc5SRasesh Mody 	return OSAL_NULL;
147ec94dbc5SRasesh Mody }
148ec94dbc5SRasesh Mody 
ecore_ptt_release(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt)149ec94dbc5SRasesh Mody void ecore_ptt_release(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
150ec94dbc5SRasesh Mody {
151ec94dbc5SRasesh Mody 	/* This PTT should not be set to pretend if it is being released */
15222d07d93SRasesh Mody 	/* TODO - add some pretend sanity checks, to make sure pretend
15322d07d93SRasesh Mody 	 * isn't set on this ptt
15422d07d93SRasesh Mody 	 */
155ec94dbc5SRasesh Mody 
156ec94dbc5SRasesh Mody 	OSAL_SPIN_LOCK(&p_hwfn->p_ptt_pool->lock);
157ec94dbc5SRasesh Mody 	OSAL_LIST_PUSH_HEAD(&p_ptt->list_entry, &p_hwfn->p_ptt_pool->free_list);
158ec94dbc5SRasesh Mody 	OSAL_SPIN_UNLOCK(&p_hwfn->p_ptt_pool->lock);
159ec94dbc5SRasesh Mody }
160ec94dbc5SRasesh Mody 
ecore_ptt_get_hw_addr(struct ecore_ptt * p_ptt)16110409fabSRasesh Mody static u32 ecore_ptt_get_hw_addr(struct ecore_ptt *p_ptt)
162ec94dbc5SRasesh Mody {
163ec94dbc5SRasesh Mody 	/* The HW is using DWORDS and we need to translate it to Bytes */
16422d07d93SRasesh Mody 	return OSAL_LE32_TO_CPU(p_ptt->pxp.offset) << 2;
165ec94dbc5SRasesh Mody }
166ec94dbc5SRasesh Mody 
ecore_ptt_config_addr(struct ecore_ptt * p_ptt)167ec94dbc5SRasesh Mody static u32 ecore_ptt_config_addr(struct ecore_ptt *p_ptt)
168ec94dbc5SRasesh Mody {
169ec94dbc5SRasesh Mody 	return PXP_PF_WINDOW_ADMIN_PER_PF_START +
170ec94dbc5SRasesh Mody 	    p_ptt->idx * sizeof(struct pxp_ptt_entry);
171ec94dbc5SRasesh Mody }
172ec94dbc5SRasesh Mody 
ecore_ptt_get_bar_addr(struct ecore_ptt * p_ptt)173ec94dbc5SRasesh Mody u32 ecore_ptt_get_bar_addr(struct ecore_ptt *p_ptt)
174ec94dbc5SRasesh Mody {
175ec94dbc5SRasesh Mody 	return PXP_EXTERNAL_BAR_PF_WINDOW_START +
176ec94dbc5SRasesh Mody 	    p_ptt->idx * PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE;
177ec94dbc5SRasesh Mody }
178ec94dbc5SRasesh Mody 
ecore_ptt_set_win(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 new_hw_addr)179ec94dbc5SRasesh Mody void ecore_ptt_set_win(struct ecore_hwfn *p_hwfn,
180ec94dbc5SRasesh Mody 		       struct ecore_ptt *p_ptt, u32 new_hw_addr)
181ec94dbc5SRasesh Mody {
182ec94dbc5SRasesh Mody 	u32 prev_hw_addr;
183ec94dbc5SRasesh Mody 
18430ecf673SRasesh Mody 	prev_hw_addr = ecore_ptt_get_hw_addr(p_ptt);
185ec94dbc5SRasesh Mody 
186ec94dbc5SRasesh Mody 	if (new_hw_addr == prev_hw_addr)
187ec94dbc5SRasesh Mody 		return;
188ec94dbc5SRasesh Mody 
189ec94dbc5SRasesh Mody 	/* Update PTT entery in admin window */
190ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
191ec94dbc5SRasesh Mody 		   "Updating PTT entry %d to offset 0x%x\n",
192ec94dbc5SRasesh Mody 		   p_ptt->idx, new_hw_addr);
193ec94dbc5SRasesh Mody 
194ec94dbc5SRasesh Mody 	/* The HW is using DWORDS and the address is in Bytes */
19522d07d93SRasesh Mody 	p_ptt->pxp.offset = OSAL_CPU_TO_LE32(new_hw_addr >> 2);
196ec94dbc5SRasesh Mody 
197ec94dbc5SRasesh Mody 	REG_WR(p_hwfn,
198ec94dbc5SRasesh Mody 	       ecore_ptt_config_addr(p_ptt) +
19922d07d93SRasesh Mody 	       OFFSETOF(struct pxp_ptt_entry, offset),
20022d07d93SRasesh Mody 	       OSAL_LE32_TO_CPU(p_ptt->pxp.offset));
201ec94dbc5SRasesh Mody }
202ec94dbc5SRasesh Mody 
ecore_set_ptt(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 hw_addr)203ec94dbc5SRasesh Mody static u32 ecore_set_ptt(struct ecore_hwfn *p_hwfn,
204ec94dbc5SRasesh Mody 			 struct ecore_ptt *p_ptt, u32 hw_addr)
205ec94dbc5SRasesh Mody {
20630ecf673SRasesh Mody 	u32 win_hw_addr = ecore_ptt_get_hw_addr(p_ptt);
207ec94dbc5SRasesh Mody 	u32 offset;
208ec94dbc5SRasesh Mody 
209ec94dbc5SRasesh Mody 	offset = hw_addr - win_hw_addr;
210ec94dbc5SRasesh Mody 
21122d07d93SRasesh Mody 	if (p_ptt->hwfn_id != p_hwfn->my_id)
21222d07d93SRasesh Mody 		DP_NOTICE(p_hwfn, true,
21322d07d93SRasesh Mody 			  "ptt[%d] of hwfn[%02x] is used by hwfn[%02x]!\n",
21422d07d93SRasesh Mody 			  p_ptt->idx, p_ptt->hwfn_id, p_hwfn->my_id);
21522d07d93SRasesh Mody 
216ec94dbc5SRasesh Mody 	/* Verify the address is within the window */
217ec94dbc5SRasesh Mody 	if (hw_addr < win_hw_addr ||
218ec94dbc5SRasesh Mody 	    offset >= PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE) {
219ec94dbc5SRasesh Mody 		ecore_ptt_set_win(p_hwfn, p_ptt, hw_addr);
220ec94dbc5SRasesh Mody 		offset = 0;
221ec94dbc5SRasesh Mody 	}
222ec94dbc5SRasesh Mody 
223ec94dbc5SRasesh Mody 	return ecore_ptt_get_bar_addr(p_ptt) + offset;
224ec94dbc5SRasesh Mody }
225ec94dbc5SRasesh Mody 
ecore_get_reserved_ptt(struct ecore_hwfn * p_hwfn,enum reserved_ptts ptt_idx)226ec94dbc5SRasesh Mody struct ecore_ptt *ecore_get_reserved_ptt(struct ecore_hwfn *p_hwfn,
227ec94dbc5SRasesh Mody 					 enum reserved_ptts ptt_idx)
228ec94dbc5SRasesh Mody {
229ec94dbc5SRasesh Mody 	if (ptt_idx >= RESERVED_PTT_MAX) {
230ec94dbc5SRasesh Mody 		DP_NOTICE(p_hwfn, true,
231ec94dbc5SRasesh Mody 			  "Requested PTT %d is out of range\n", ptt_idx);
232ec94dbc5SRasesh Mody 		return OSAL_NULL;
233ec94dbc5SRasesh Mody 	}
234ec94dbc5SRasesh Mody 
235ec94dbc5SRasesh Mody 	return &p_hwfn->p_ptt_pool->ptts[ptt_idx];
236ec94dbc5SRasesh Mody }
237ec94dbc5SRasesh Mody 
ecore_is_reg_fifo_empty(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt)23822d07d93SRasesh Mody static bool ecore_is_reg_fifo_empty(struct ecore_hwfn *p_hwfn,
23922d07d93SRasesh Mody 				    struct ecore_ptt *p_ptt)
24022d07d93SRasesh Mody {
24122d07d93SRasesh Mody 	bool is_empty = true;
24222d07d93SRasesh Mody 	u32 bar_addr;
24322d07d93SRasesh Mody 
24422d07d93SRasesh Mody 	if (!p_hwfn->p_dev->chk_reg_fifo)
24522d07d93SRasesh Mody 		goto out;
24622d07d93SRasesh Mody 
24722d07d93SRasesh Mody 	/* ecore_rd() cannot be used here since it calls this function */
24822d07d93SRasesh Mody 	bar_addr = ecore_set_ptt(p_hwfn, p_ptt, GRC_REG_TRACE_FIFO_VALID_DATA);
24922d07d93SRasesh Mody 	is_empty = REG_RD(p_hwfn, bar_addr) == 0;
25022d07d93SRasesh Mody 
25122d07d93SRasesh Mody #ifndef ASIC_ONLY
25222d07d93SRasesh Mody 	if (CHIP_REV_IS_SLOW(p_hwfn->p_dev))
25322d07d93SRasesh Mody 		OSAL_UDELAY(100);
25422d07d93SRasesh Mody #endif
25522d07d93SRasesh Mody 
25622d07d93SRasesh Mody out:
25722d07d93SRasesh Mody 	return is_empty;
25822d07d93SRasesh Mody }
25922d07d93SRasesh Mody 
ecore_wr(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 hw_addr,u32 val)260ec94dbc5SRasesh Mody void ecore_wr(struct ecore_hwfn *p_hwfn,
261ec94dbc5SRasesh Mody 	      struct ecore_ptt *p_ptt, u32 hw_addr, u32 val)
262ec94dbc5SRasesh Mody {
26322d07d93SRasesh Mody 	bool prev_fifo_err;
26422d07d93SRasesh Mody 	u32 bar_addr;
265ec94dbc5SRasesh Mody 
26622d07d93SRasesh Mody 	prev_fifo_err = !ecore_is_reg_fifo_empty(p_hwfn, p_ptt);
26722d07d93SRasesh Mody 
26822d07d93SRasesh Mody 	bar_addr = ecore_set_ptt(p_hwfn, p_ptt, hw_addr);
269ec94dbc5SRasesh Mody 	REG_WR(p_hwfn, bar_addr, val);
270ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
271ec94dbc5SRasesh Mody 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
272ec94dbc5SRasesh Mody 		   bar_addr, hw_addr, val);
273ec94dbc5SRasesh Mody 
274ec94dbc5SRasesh Mody #ifndef ASIC_ONLY
275ec94dbc5SRasesh Mody 	if (CHIP_REV_IS_SLOW(p_hwfn->p_dev))
276ec94dbc5SRasesh Mody 		OSAL_UDELAY(100);
277ec94dbc5SRasesh Mody #endif
27822d07d93SRasesh Mody 
27922d07d93SRasesh Mody 	OSAL_WARN(!prev_fifo_err && !ecore_is_reg_fifo_empty(p_hwfn, p_ptt),
28022d07d93SRasesh Mody 		  "reg_fifo err was caused by a call to ecore_wr(0x%x, 0x%x)\n",
28122d07d93SRasesh Mody 		  hw_addr, val);
282ec94dbc5SRasesh Mody }
283ec94dbc5SRasesh Mody 
ecore_rd(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 hw_addr)284ec94dbc5SRasesh Mody u32 ecore_rd(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt, u32 hw_addr)
285ec94dbc5SRasesh Mody {
28622d07d93SRasesh Mody 	bool prev_fifo_err;
28722d07d93SRasesh Mody 	u32 bar_addr, val;
28822d07d93SRasesh Mody 
28922d07d93SRasesh Mody 	prev_fifo_err = !ecore_is_reg_fifo_empty(p_hwfn, p_ptt);
29022d07d93SRasesh Mody 
29122d07d93SRasesh Mody 	bar_addr = ecore_set_ptt(p_hwfn, p_ptt, hw_addr);
29222d07d93SRasesh Mody 	val = REG_RD(p_hwfn, bar_addr);
293ec94dbc5SRasesh Mody 
294ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
295ec94dbc5SRasesh Mody 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
296ec94dbc5SRasesh Mody 		   bar_addr, hw_addr, val);
297ec94dbc5SRasesh Mody 
298ec94dbc5SRasesh Mody #ifndef ASIC_ONLY
299ec94dbc5SRasesh Mody 	if (CHIP_REV_IS_SLOW(p_hwfn->p_dev))
300ec94dbc5SRasesh Mody 		OSAL_UDELAY(100);
301ec94dbc5SRasesh Mody #endif
302ec94dbc5SRasesh Mody 
30322d07d93SRasesh Mody 	OSAL_WARN(!prev_fifo_err && !ecore_is_reg_fifo_empty(p_hwfn, p_ptt),
30422d07d93SRasesh Mody 		  "reg_fifo error was caused by a call to ecore_rd(0x%x)\n",
30522d07d93SRasesh Mody 		  hw_addr);
30622d07d93SRasesh Mody 
307ec94dbc5SRasesh Mody 	return val;
308ec94dbc5SRasesh Mody }
309ec94dbc5SRasesh Mody 
ecore_memcpy_hw(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,void * addr,u32 hw_addr,osal_size_t n,bool to_device)310ec94dbc5SRasesh Mody static void ecore_memcpy_hw(struct ecore_hwfn *p_hwfn,
311ec94dbc5SRasesh Mody 			    struct ecore_ptt *p_ptt,
312ec94dbc5SRasesh Mody 			    void *addr,
313ec94dbc5SRasesh Mody 			    u32 hw_addr, osal_size_t n, bool to_device)
314ec94dbc5SRasesh Mody {
315ec94dbc5SRasesh Mody 	u32 dw_count, *host_addr, hw_offset;
316ec94dbc5SRasesh Mody 	osal_size_t quota, done = 0;
317ec94dbc5SRasesh Mody 	u32 OSAL_IOMEM *reg_addr;
318ec94dbc5SRasesh Mody 
319ec94dbc5SRasesh Mody 	while (done < n) {
320ec94dbc5SRasesh Mody 		quota = OSAL_MIN_T(osal_size_t, n - done,
321ec94dbc5SRasesh Mody 				   PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE);
322ec94dbc5SRasesh Mody 
32386a2265eSRasesh Mody 		if (IS_PF(p_hwfn->p_dev)) {
324ec94dbc5SRasesh Mody 			ecore_ptt_set_win(p_hwfn, p_ptt, hw_addr + done);
325ec94dbc5SRasesh Mody 			hw_offset = ecore_ptt_get_bar_addr(p_ptt);
32686a2265eSRasesh Mody 		} else {
32786a2265eSRasesh Mody 			hw_offset = hw_addr + done;
32886a2265eSRasesh Mody 		}
329ec94dbc5SRasesh Mody 
330ec94dbc5SRasesh Mody 		dw_count = quota / 4;
331ec94dbc5SRasesh Mody 		host_addr = (u32 *)((u8 *)addr + done);
332ec94dbc5SRasesh Mody 		reg_addr = (u32 OSAL_IOMEM *)OSAL_REG_ADDR(p_hwfn, hw_offset);
333ec94dbc5SRasesh Mody 
334ec94dbc5SRasesh Mody 		if (to_device)
335ec94dbc5SRasesh Mody 			while (dw_count--)
336ec94dbc5SRasesh Mody 				DIRECT_REG_WR(p_hwfn, reg_addr++, *host_addr++);
337ec94dbc5SRasesh Mody 		else
338ec94dbc5SRasesh Mody 			while (dw_count--)
339ec94dbc5SRasesh Mody 				*host_addr++ = DIRECT_REG_RD(p_hwfn,
340ec94dbc5SRasesh Mody 							     reg_addr++);
341ec94dbc5SRasesh Mody 
342ec94dbc5SRasesh Mody 		done += quota;
343ec94dbc5SRasesh Mody 	}
344ec94dbc5SRasesh Mody }
345ec94dbc5SRasesh Mody 
ecore_memcpy_from(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,void * dest,u32 hw_addr,osal_size_t n)346ec94dbc5SRasesh Mody void ecore_memcpy_from(struct ecore_hwfn *p_hwfn,
347ec94dbc5SRasesh Mody 		       struct ecore_ptt *p_ptt,
348ec94dbc5SRasesh Mody 		       void *dest, u32 hw_addr, osal_size_t n)
349ec94dbc5SRasesh Mody {
350ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
351ec94dbc5SRasesh Mody 		   "hw_addr 0x%x, dest %p hw_addr 0x%x, size %lu\n",
352ec94dbc5SRasesh Mody 		   hw_addr, dest, hw_addr, (unsigned long)n);
353ec94dbc5SRasesh Mody 
354ec94dbc5SRasesh Mody 	ecore_memcpy_hw(p_hwfn, p_ptt, dest, hw_addr, n, false);
355ec94dbc5SRasesh Mody }
356ec94dbc5SRasesh Mody 
ecore_memcpy_to(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 hw_addr,void * src,osal_size_t n)357ec94dbc5SRasesh Mody void ecore_memcpy_to(struct ecore_hwfn *p_hwfn,
358ec94dbc5SRasesh Mody 		     struct ecore_ptt *p_ptt,
359ec94dbc5SRasesh Mody 		     u32 hw_addr, void *src, osal_size_t n)
360ec94dbc5SRasesh Mody {
361ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
362ec94dbc5SRasesh Mody 		   "hw_addr 0x%x, hw_addr 0x%x, src %p size %lu\n",
363ec94dbc5SRasesh Mody 		   hw_addr, hw_addr, src, (unsigned long)n);
364ec94dbc5SRasesh Mody 
365ec94dbc5SRasesh Mody 	ecore_memcpy_hw(p_hwfn, p_ptt, src, hw_addr, n, true);
366ec94dbc5SRasesh Mody }
367ec94dbc5SRasesh Mody 
ecore_fid_pretend(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u16 fid)368ec94dbc5SRasesh Mody void ecore_fid_pretend(struct ecore_hwfn *p_hwfn,
369ec94dbc5SRasesh Mody 		       struct ecore_ptt *p_ptt, u16 fid)
370ec94dbc5SRasesh Mody {
371ec94dbc5SRasesh Mody 	u16 control = 0;
372ec94dbc5SRasesh Mody 
373ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
374ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
375ec94dbc5SRasesh Mody 
376ec94dbc5SRasesh Mody /* Every pretend undos prev pretends, including previous port pretend */
37722d07d93SRasesh Mody 
378ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
379ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
380ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
381ec94dbc5SRasesh Mody 
382ec94dbc5SRasesh Mody 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
383ec94dbc5SRasesh Mody 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
384ec94dbc5SRasesh Mody 
38522d07d93SRasesh Mody 	p_ptt->pxp.pretend.control = OSAL_CPU_TO_LE16(control);
386ec94dbc5SRasesh Mody 	p_ptt->pxp.pretend.fid.concrete_fid.fid = OSAL_CPU_TO_LE16(fid);
387ec94dbc5SRasesh Mody 
388ec94dbc5SRasesh Mody 	REG_WR(p_hwfn,
389ec94dbc5SRasesh Mody 	       ecore_ptt_config_addr(p_ptt) +
39022d07d93SRasesh Mody 	       OFFSETOF(struct pxp_ptt_entry, pretend),
39122d07d93SRasesh Mody 			*(u32 *)&p_ptt->pxp.pretend);
392ec94dbc5SRasesh Mody }
393ec94dbc5SRasesh Mody 
ecore_port_pretend(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u8 port_id)394ec94dbc5SRasesh Mody void ecore_port_pretend(struct ecore_hwfn *p_hwfn,
395ec94dbc5SRasesh Mody 			struct ecore_ptt *p_ptt, u8 port_id)
396ec94dbc5SRasesh Mody {
397ec94dbc5SRasesh Mody 	u16 control = 0;
398ec94dbc5SRasesh Mody 
399ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
400ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
401ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
40222d07d93SRasesh Mody 	p_ptt->pxp.pretend.control = OSAL_CPU_TO_LE16(control);
403ec94dbc5SRasesh Mody 
404ec94dbc5SRasesh Mody 	REG_WR(p_hwfn,
405ec94dbc5SRasesh Mody 	       ecore_ptt_config_addr(p_ptt) +
40622d07d93SRasesh Mody 	       OFFSETOF(struct pxp_ptt_entry, pretend),
40722d07d93SRasesh Mody 			*(u32 *)&p_ptt->pxp.pretend);
408ec94dbc5SRasesh Mody }
409ec94dbc5SRasesh Mody 
ecore_port_unpretend(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt)410ec94dbc5SRasesh Mody void ecore_port_unpretend(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
411ec94dbc5SRasesh Mody {
412ec94dbc5SRasesh Mody 	u16 control = 0;
413ec94dbc5SRasesh Mody 
414ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
415ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
416ec94dbc5SRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
417ec94dbc5SRasesh Mody 
41822d07d93SRasesh Mody 	p_ptt->pxp.pretend.control = OSAL_CPU_TO_LE16(control);
41922d07d93SRasesh Mody 
420ec94dbc5SRasesh Mody 	REG_WR(p_hwfn,
421ec94dbc5SRasesh Mody 	       ecore_ptt_config_addr(p_ptt) +
42222d07d93SRasesh Mody 	       OFFSETOF(struct pxp_ptt_entry, pretend),
42322d07d93SRasesh Mody 			*(u32 *)&p_ptt->pxp.pretend);
424ec94dbc5SRasesh Mody }
425ec94dbc5SRasesh Mody 
ecore_port_fid_pretend(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u8 port_id,u16 fid)4269585053bSRasesh Mody void ecore_port_fid_pretend(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
4279585053bSRasesh Mody 			    u8 port_id, u16 fid)
4289585053bSRasesh Mody {
4299585053bSRasesh Mody 	u16 control = 0;
4309585053bSRasesh Mody 
4319585053bSRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
4329585053bSRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
4339585053bSRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
4349585053bSRasesh Mody 
4359585053bSRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
4369585053bSRasesh Mody 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
4379585053bSRasesh Mody 
4389585053bSRasesh Mody 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
4399585053bSRasesh Mody 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
4409585053bSRasesh Mody 
4419585053bSRasesh Mody 	p_ptt->pxp.pretend.control = OSAL_CPU_TO_LE16(control);
4429585053bSRasesh Mody 	p_ptt->pxp.pretend.fid.concrete_fid.fid = OSAL_CPU_TO_LE16(fid);
4439585053bSRasesh Mody 
4449585053bSRasesh Mody 	REG_WR(p_hwfn,
4459585053bSRasesh Mody 	       ecore_ptt_config_addr(p_ptt) +
4469585053bSRasesh Mody 	       OFFSETOF(struct pxp_ptt_entry, pretend),
4479585053bSRasesh Mody 	       *(u32 *)&p_ptt->pxp.pretend);
4489585053bSRasesh Mody }
4499585053bSRasesh Mody 
ecore_vfid_to_concrete(struct ecore_hwfn * p_hwfn,u8 vfid)450ec94dbc5SRasesh Mody u32 ecore_vfid_to_concrete(struct ecore_hwfn *p_hwfn, u8 vfid)
451ec94dbc5SRasesh Mody {
452ec94dbc5SRasesh Mody 	u32 concrete_fid = 0;
453ec94dbc5SRasesh Mody 
454ec94dbc5SRasesh Mody 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_PFID, p_hwfn->rel_pf_id);
455ec94dbc5SRasesh Mody 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFID, vfid);
456ec94dbc5SRasesh Mody 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFVALID, 1);
457ec94dbc5SRasesh Mody 
458ec94dbc5SRasesh Mody 	return concrete_fid;
459ec94dbc5SRasesh Mody }
460ec94dbc5SRasesh Mody 
461ec94dbc5SRasesh Mody /* Not in use @DPDK
462ec94dbc5SRasesh Mody  * Ecore HW lock
463ec94dbc5SRasesh Mody  * =============
464ec94dbc5SRasesh Mody  * Although the implementation is ready, today we don't have any flow that
465ec94dbc5SRasesh Mody  * utliizes said locks - and we want to keep it this way.
466ec94dbc5SRasesh Mody  * If this changes, this needs to be revisted.
467ec94dbc5SRasesh Mody  */
468ec94dbc5SRasesh Mody 
4693eed444aSRasesh Mody /* DMAE */
4703eed444aSRasesh Mody 
4713eed444aSRasesh Mody #define ECORE_DMAE_FLAGS_IS_SET(params, flag)	\
472ea85629fSRasesh Mody 	((params) != OSAL_NULL && \
473ea85629fSRasesh Mody 	 GET_FIELD((params)->flags, DMAE_PARAMS_##flag))
4743eed444aSRasesh Mody 
ecore_dmae_opcode(struct ecore_hwfn * p_hwfn,const u8 is_src_type_grc,const u8 is_dst_type_grc,struct dmae_params * p_params)475ec94dbc5SRasesh Mody static void ecore_dmae_opcode(struct ecore_hwfn *p_hwfn,
476ec94dbc5SRasesh Mody 			      const u8 is_src_type_grc,
477ec94dbc5SRasesh Mody 			      const u8 is_dst_type_grc,
478ea85629fSRasesh Mody 			      struct dmae_params *p_params)
479ec94dbc5SRasesh Mody {
480ea85629fSRasesh Mody 	u8 src_pf_id, dst_pf_id, port_id;
481ec94dbc5SRasesh Mody 	u16 opcode_b = 0;
482ec94dbc5SRasesh Mody 	u32 opcode = 0;
483ec94dbc5SRasesh Mody 
484ec94dbc5SRasesh Mody 	/* Whether the source is the PCIe or the GRC.
485ec94dbc5SRasesh Mody 	 * 0- The source is the PCIe
486ec94dbc5SRasesh Mody 	 * 1- The source is the GRC.
487ec94dbc5SRasesh Mody 	 */
488ea85629fSRasesh Mody 	opcode |= (is_src_type_grc ? dmae_cmd_src_grc : dmae_cmd_src_pcie) <<
489ea85629fSRasesh Mody 		  DMAE_CMD_SRC_SHIFT;
490ea85629fSRasesh Mody 	src_pf_id = ECORE_DMAE_FLAGS_IS_SET(p_params, SRC_PF_VALID) ?
491ea85629fSRasesh Mody 		    p_params->src_pf_id : p_hwfn->rel_pf_id;
492ea85629fSRasesh Mody 	opcode |= (src_pf_id & DMAE_CMD_SRC_PF_ID_MASK) <<
493ec94dbc5SRasesh Mody 		  DMAE_CMD_SRC_PF_ID_SHIFT;
494ec94dbc5SRasesh Mody 
495ec94dbc5SRasesh Mody 	/* The destination of the DMA can be: 0-None 1-PCIe 2-GRC 3-None */
496ea85629fSRasesh Mody 	opcode |= (is_dst_type_grc ? dmae_cmd_dst_grc : dmae_cmd_dst_pcie) <<
497ea85629fSRasesh Mody 		  DMAE_CMD_DST_SHIFT;
498ea85629fSRasesh Mody 	dst_pf_id = ECORE_DMAE_FLAGS_IS_SET(p_params, DST_PF_VALID) ?
499ea85629fSRasesh Mody 		    p_params->dst_pf_id : p_hwfn->rel_pf_id;
500ea85629fSRasesh Mody 	opcode |= (dst_pf_id & DMAE_CMD_DST_PF_ID_MASK) <<
501ec94dbc5SRasesh Mody 		  DMAE_CMD_DST_PF_ID_SHIFT;
502ec94dbc5SRasesh Mody 
5033eed444aSRasesh Mody 	/* DMAE_E4_TODO need to check which value to specify here. */
504ec94dbc5SRasesh Mody 	/* opcode |= (!b_complete_to_host)<< DMAE_CMD_C_DST_SHIFT; */
505ec94dbc5SRasesh Mody 
506ec94dbc5SRasesh Mody 	/* Whether to write a completion word to the completion destination:
507ec94dbc5SRasesh Mody 	 * 0-Do not write a completion word
508ec94dbc5SRasesh Mody 	 * 1-Write the completion word
509ec94dbc5SRasesh Mody 	 */
510ec94dbc5SRasesh Mody 	opcode |= DMAE_CMD_COMP_WORD_EN_MASK << DMAE_CMD_COMP_WORD_EN_SHIFT;
511ec94dbc5SRasesh Mody 	opcode |= DMAE_CMD_SRC_ADDR_RESET_MASK << DMAE_CMD_SRC_ADDR_RESET_SHIFT;
512ec94dbc5SRasesh Mody 
5133eed444aSRasesh Mody 	if (ECORE_DMAE_FLAGS_IS_SET(p_params, COMPLETION_DST))
514ec94dbc5SRasesh Mody 		opcode |= 1 << DMAE_CMD_COMP_FUNC_SHIFT;
515ec94dbc5SRasesh Mody 
516ec94dbc5SRasesh Mody 	/* swapping mode 3 - big endian there should be a define ifdefed in
517ec94dbc5SRasesh Mody 	 * the HSI somewhere. Since it is currently
518ec94dbc5SRasesh Mody 	 */
519ec94dbc5SRasesh Mody 	opcode |= DMAE_CMD_ENDIANITY << DMAE_CMD_ENDIANITY_MODE_SHIFT;
520ec94dbc5SRasesh Mody 
521ea85629fSRasesh Mody 	port_id = (ECORE_DMAE_FLAGS_IS_SET(p_params, PORT_VALID)) ?
5223eed444aSRasesh Mody 		  p_params->port_id : p_hwfn->port_id;
5233eed444aSRasesh Mody 	opcode |= port_id << DMAE_CMD_PORT_ID_SHIFT;
524ec94dbc5SRasesh Mody 
525ec94dbc5SRasesh Mody 	/* reset source address in next go */
526ec94dbc5SRasesh Mody 	opcode |= DMAE_CMD_SRC_ADDR_RESET_MASK << DMAE_CMD_SRC_ADDR_RESET_SHIFT;
527ec94dbc5SRasesh Mody 
528ec94dbc5SRasesh Mody 	/* reset dest address in next go */
529ec94dbc5SRasesh Mody 	opcode |= DMAE_CMD_DST_ADDR_RESET_MASK << DMAE_CMD_DST_ADDR_RESET_SHIFT;
530ec94dbc5SRasesh Mody 
531ec94dbc5SRasesh Mody 	/* SRC/DST VFID: all 1's - pf, otherwise VF id */
532ea85629fSRasesh Mody 	if (ECORE_DMAE_FLAGS_IS_SET(p_params, SRC_VF_VALID)) {
533ec94dbc5SRasesh Mody 		opcode |= (1 << DMAE_CMD_SRC_VF_ID_VALID_SHIFT);
534ea85629fSRasesh Mody 		opcode_b |= (p_params->src_vf_id <<  DMAE_CMD_SRC_VF_ID_SHIFT);
535ec94dbc5SRasesh Mody 	} else {
536ec94dbc5SRasesh Mody 		opcode_b |= (DMAE_CMD_SRC_VF_ID_MASK <<
537ec94dbc5SRasesh Mody 			     DMAE_CMD_SRC_VF_ID_SHIFT);
538ec94dbc5SRasesh Mody 	}
539ea85629fSRasesh Mody 	if (ECORE_DMAE_FLAGS_IS_SET(p_params, DST_VF_VALID)) {
540ec94dbc5SRasesh Mody 		opcode |= 1 << DMAE_CMD_DST_VF_ID_VALID_SHIFT;
541ea85629fSRasesh Mody 		opcode_b |= p_params->dst_vf_id << DMAE_CMD_DST_VF_ID_SHIFT;
542ec94dbc5SRasesh Mody 	} else {
543ec94dbc5SRasesh Mody 		opcode_b |= DMAE_CMD_DST_VF_ID_MASK << DMAE_CMD_DST_VF_ID_SHIFT;
544ec94dbc5SRasesh Mody 	}
545ec94dbc5SRasesh Mody 
546ec94dbc5SRasesh Mody 	p_hwfn->dmae_info.p_dmae_cmd->opcode = OSAL_CPU_TO_LE32(opcode);
547ec94dbc5SRasesh Mody 	p_hwfn->dmae_info.p_dmae_cmd->opcode_b = OSAL_CPU_TO_LE16(opcode_b);
548ec94dbc5SRasesh Mody }
549ec94dbc5SRasesh Mody 
ecore_dmae_idx_to_go_cmd(u8 idx)550ec94dbc5SRasesh Mody static u32 ecore_dmae_idx_to_go_cmd(u8 idx)
551ec94dbc5SRasesh Mody {
552ec94dbc5SRasesh Mody 	OSAL_BUILD_BUG_ON((DMAE_REG_GO_C31 - DMAE_REG_GO_C0) != 31 * 4);
553ec94dbc5SRasesh Mody 
55422d07d93SRasesh Mody 	/* All the DMAE 'go' registers form an array in internal memory */
55522d07d93SRasesh Mody 	return DMAE_REG_GO_C0 + (idx << 2);
556ec94dbc5SRasesh Mody }
557ec94dbc5SRasesh Mody 
ecore_dmae_post_command(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt)558ababb520SRasesh Mody static enum _ecore_status_t ecore_dmae_post_command(struct ecore_hwfn *p_hwfn,
559ababb520SRasesh Mody 						    struct ecore_ptt *p_ptt)
560ec94dbc5SRasesh Mody {
561ec94dbc5SRasesh Mody 	struct dmae_cmd *p_command = p_hwfn->dmae_info.p_dmae_cmd;
562ec94dbc5SRasesh Mody 	u8 idx_cmd = p_hwfn->dmae_info.channel, i;
56322d07d93SRasesh Mody 	enum _ecore_status_t ecore_status = ECORE_SUCCESS;
564ec94dbc5SRasesh Mody 
565ec94dbc5SRasesh Mody 	/* verify address is not OSAL_NULL */
566ec94dbc5SRasesh Mody 	if ((((!p_command->dst_addr_lo) && (!p_command->dst_addr_hi)) ||
567ec94dbc5SRasesh Mody 	     ((!p_command->src_addr_lo) && (!p_command->src_addr_hi)))) {
568ec94dbc5SRasesh Mody 		DP_NOTICE(p_hwfn, true,
569ec94dbc5SRasesh Mody 			  "source or destination address 0 idx_cmd=%d\n"
570ec94dbc5SRasesh Mody 			  "opcode = [0x%08x,0x%04x] len=0x%x"
571ec94dbc5SRasesh Mody 			  " src=0x%x:%x dst=0x%x:%x\n",
57222d07d93SRasesh Mody 			  idx_cmd,
57322d07d93SRasesh Mody 			  OSAL_LE32_TO_CPU(p_command->opcode),
57422d07d93SRasesh Mody 			  OSAL_LE16_TO_CPU(p_command->opcode_b),
57522d07d93SRasesh Mody 			  OSAL_LE16_TO_CPU(p_command->length_dw),
57622d07d93SRasesh Mody 			  OSAL_LE32_TO_CPU(p_command->src_addr_hi),
57722d07d93SRasesh Mody 			  OSAL_LE32_TO_CPU(p_command->src_addr_lo),
57822d07d93SRasesh Mody 			  OSAL_LE32_TO_CPU(p_command->dst_addr_hi),
57922d07d93SRasesh Mody 			  OSAL_LE32_TO_CPU(p_command->dst_addr_lo));
580ec94dbc5SRasesh Mody 
581ec94dbc5SRasesh Mody 		return ECORE_INVAL;
582ec94dbc5SRasesh Mody 	}
583ec94dbc5SRasesh Mody 
584ec94dbc5SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
585ec94dbc5SRasesh Mody 		   "Posting DMAE command [idx %d]: opcode = [0x%08x,0x%04x]"
586ec94dbc5SRasesh Mody 		   "len=0x%x src=0x%x:%x dst=0x%x:%x\n",
58722d07d93SRasesh Mody 		   idx_cmd,
58822d07d93SRasesh Mody 		   OSAL_LE32_TO_CPU(p_command->opcode),
58922d07d93SRasesh Mody 		   OSAL_LE16_TO_CPU(p_command->opcode_b),
59022d07d93SRasesh Mody 		   OSAL_LE16_TO_CPU(p_command->length_dw),
59122d07d93SRasesh Mody 		   OSAL_LE32_TO_CPU(p_command->src_addr_hi),
59222d07d93SRasesh Mody 		   OSAL_LE32_TO_CPU(p_command->src_addr_lo),
59322d07d93SRasesh Mody 		   OSAL_LE32_TO_CPU(p_command->dst_addr_hi),
59422d07d93SRasesh Mody 		   OSAL_LE32_TO_CPU(p_command->dst_addr_lo));
595ec94dbc5SRasesh Mody 
596ec94dbc5SRasesh Mody 	/* Copy the command to DMAE - need to do it before every call
597ec94dbc5SRasesh Mody 	 * for source/dest address no reset.
598ec94dbc5SRasesh Mody 	 * The number of commands have been increased to 16 (previous was 14)
599ec94dbc5SRasesh Mody 	 * The first 9 DWs are the command registers, the 10 DW is the
600ec94dbc5SRasesh Mody 	 * GO register, and
601ec94dbc5SRasesh Mody 	 * the rest are result registers (which are read only by the client).
602ec94dbc5SRasesh Mody 	 */
603ec94dbc5SRasesh Mody 	for (i = 0; i < DMAE_CMD_SIZE; i++) {
604ec94dbc5SRasesh Mody 		u32 data = (i < DMAE_CMD_SIZE_TO_FILL) ?
605ec94dbc5SRasesh Mody 		    *(((u32 *)p_command) + i) : 0;
606ec94dbc5SRasesh Mody 
607ec94dbc5SRasesh Mody 		ecore_wr(p_hwfn, p_ptt,
608ec94dbc5SRasesh Mody 			 DMAE_REG_CMD_MEM +
609ec94dbc5SRasesh Mody 			 (idx_cmd * DMAE_CMD_SIZE * sizeof(u32)) +
610ec94dbc5SRasesh Mody 			 (i * sizeof(u32)), data);
611ec94dbc5SRasesh Mody 	}
612ec94dbc5SRasesh Mody 
613ec94dbc5SRasesh Mody 	ecore_wr(p_hwfn, p_ptt,
614ec94dbc5SRasesh Mody 		 ecore_dmae_idx_to_go_cmd(idx_cmd), DMAE_GO_VALUE);
615ec94dbc5SRasesh Mody 
616ec94dbc5SRasesh Mody 	return ecore_status;
617ec94dbc5SRasesh Mody }
618ec94dbc5SRasesh Mody 
ecore_dmae_info_alloc(struct ecore_hwfn * p_hwfn)619ec94dbc5SRasesh Mody enum _ecore_status_t ecore_dmae_info_alloc(struct ecore_hwfn *p_hwfn)
620ec94dbc5SRasesh Mody {
621ec94dbc5SRasesh Mody 	dma_addr_t *p_addr = &p_hwfn->dmae_info.completion_word_phys_addr;
622ec94dbc5SRasesh Mody 	struct dmae_cmd **p_cmd = &p_hwfn->dmae_info.p_dmae_cmd;
623ec94dbc5SRasesh Mody 	u32 **p_buff = &p_hwfn->dmae_info.p_intermediate_buffer;
624ec94dbc5SRasesh Mody 	u32 **p_comp = &p_hwfn->dmae_info.p_completion_word;
625ec94dbc5SRasesh Mody 
626ec94dbc5SRasesh Mody 	*p_comp = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, p_addr, sizeof(u32));
627ec94dbc5SRasesh Mody 	if (*p_comp == OSAL_NULL) {
62898abf84eSRasesh Mody 		DP_NOTICE(p_hwfn, false,
629ec94dbc5SRasesh Mody 			  "Failed to allocate `p_completion_word'\n");
63022d07d93SRasesh Mody 		goto err;
631ec94dbc5SRasesh Mody 	}
632ec94dbc5SRasesh Mody 
633ec94dbc5SRasesh Mody 	p_addr = &p_hwfn->dmae_info.dmae_cmd_phys_addr;
634ec94dbc5SRasesh Mody 	*p_cmd = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, p_addr,
635ec94dbc5SRasesh Mody 					 sizeof(struct dmae_cmd));
636ec94dbc5SRasesh Mody 	if (*p_cmd == OSAL_NULL) {
63798abf84eSRasesh Mody 		DP_NOTICE(p_hwfn, false,
638ec94dbc5SRasesh Mody 			  "Failed to allocate `struct dmae_cmd'\n");
63922d07d93SRasesh Mody 		goto err;
640ec94dbc5SRasesh Mody 	}
641ec94dbc5SRasesh Mody 
642ec94dbc5SRasesh Mody 	p_addr = &p_hwfn->dmae_info.intermediate_buffer_phys_addr;
643ec94dbc5SRasesh Mody 	*p_buff = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, p_addr,
644ec94dbc5SRasesh Mody 					  sizeof(u32) * DMAE_MAX_RW_SIZE);
645ec94dbc5SRasesh Mody 	if (*p_buff == OSAL_NULL) {
64698abf84eSRasesh Mody 		DP_NOTICE(p_hwfn, false,
647ec94dbc5SRasesh Mody 			  "Failed to allocate `intermediate_buffer'\n");
64822d07d93SRasesh Mody 		goto err;
649ec94dbc5SRasesh Mody 	}
650ec94dbc5SRasesh Mody 
651ec94dbc5SRasesh Mody 		p_hwfn->dmae_info.channel = p_hwfn->rel_pf_id;
65278e7fcefSRasesh Mody 		p_hwfn->dmae_info.b_mem_ready = true;
653ec94dbc5SRasesh Mody 
654ec94dbc5SRasesh Mody 	return ECORE_SUCCESS;
65522d07d93SRasesh Mody err:
65622d07d93SRasesh Mody 	ecore_dmae_info_free(p_hwfn);
65722d07d93SRasesh Mody 	return ECORE_NOMEM;
658ec94dbc5SRasesh Mody }
659ec94dbc5SRasesh Mody 
ecore_dmae_info_free(struct ecore_hwfn * p_hwfn)660ec94dbc5SRasesh Mody void ecore_dmae_info_free(struct ecore_hwfn *p_hwfn)
661ec94dbc5SRasesh Mody {
662ec94dbc5SRasesh Mody 	dma_addr_t p_phys;
663ec94dbc5SRasesh Mody 
66478e7fcefSRasesh Mody 	OSAL_SPIN_LOCK(&p_hwfn->dmae_info.lock);
66578e7fcefSRasesh Mody 	p_hwfn->dmae_info.b_mem_ready = false;
66678e7fcefSRasesh Mody 	OSAL_SPIN_UNLOCK(&p_hwfn->dmae_info.lock);
667ec94dbc5SRasesh Mody 
668ec94dbc5SRasesh Mody 	if (p_hwfn->dmae_info.p_completion_word != OSAL_NULL) {
669ec94dbc5SRasesh Mody 		p_phys = p_hwfn->dmae_info.completion_word_phys_addr;
670ec94dbc5SRasesh Mody 		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
671ec94dbc5SRasesh Mody 				       p_hwfn->dmae_info.p_completion_word,
672ec94dbc5SRasesh Mody 				       p_phys, sizeof(u32));
673ec94dbc5SRasesh Mody 		p_hwfn->dmae_info.p_completion_word = OSAL_NULL;
674ec94dbc5SRasesh Mody 	}
675ec94dbc5SRasesh Mody 
676ec94dbc5SRasesh Mody 	if (p_hwfn->dmae_info.p_dmae_cmd != OSAL_NULL) {
677ec94dbc5SRasesh Mody 		p_phys = p_hwfn->dmae_info.dmae_cmd_phys_addr;
678ec94dbc5SRasesh Mody 		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
679ec94dbc5SRasesh Mody 				       p_hwfn->dmae_info.p_dmae_cmd,
680ec94dbc5SRasesh Mody 				       p_phys, sizeof(struct dmae_cmd));
681ec94dbc5SRasesh Mody 		p_hwfn->dmae_info.p_dmae_cmd = OSAL_NULL;
682ec94dbc5SRasesh Mody 	}
683ec94dbc5SRasesh Mody 
684ec94dbc5SRasesh Mody 	if (p_hwfn->dmae_info.p_intermediate_buffer != OSAL_NULL) {
685ec94dbc5SRasesh Mody 		p_phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
686ec94dbc5SRasesh Mody 		OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
687ec94dbc5SRasesh Mody 				       p_hwfn->dmae_info.p_intermediate_buffer,
688ec94dbc5SRasesh Mody 				       p_phys, sizeof(u32) * DMAE_MAX_RW_SIZE);
689ec94dbc5SRasesh Mody 		p_hwfn->dmae_info.p_intermediate_buffer = OSAL_NULL;
690ec94dbc5SRasesh Mody 	}
691ec94dbc5SRasesh Mody }
692ec94dbc5SRasesh Mody 
ecore_dmae_operation_wait(struct ecore_hwfn * p_hwfn)693ec94dbc5SRasesh Mody static enum _ecore_status_t ecore_dmae_operation_wait(struct ecore_hwfn *p_hwfn)
694ec94dbc5SRasesh Mody {
695ec94dbc5SRasesh Mody 	u32 wait_cnt_limit = 10000, wait_cnt = 0;
6969455b556SRasesh Mody 	enum _ecore_status_t ecore_status = ECORE_SUCCESS;
697ec94dbc5SRasesh Mody 
698ec94dbc5SRasesh Mody #ifndef ASIC_ONLY
699ec94dbc5SRasesh Mody 	u32 factor = (CHIP_REV_IS_EMUL(p_hwfn->p_dev) ?
700ec94dbc5SRasesh Mody 		      ECORE_EMUL_FACTOR :
701ec94dbc5SRasesh Mody 		      (CHIP_REV_IS_FPGA(p_hwfn->p_dev) ?
702ec94dbc5SRasesh Mody 		       ECORE_FPGA_FACTOR : 1));
703ec94dbc5SRasesh Mody 
704ec94dbc5SRasesh Mody 	wait_cnt_limit *= factor;
705ec94dbc5SRasesh Mody #endif
706ec94dbc5SRasesh Mody 
707ec94dbc5SRasesh Mody 	/* DMAE_E4_TODO : TODO check if we have to call any other function
708ec94dbc5SRasesh Mody 	 * other than BARRIER to sync the completion_word since we are not
709ec94dbc5SRasesh Mody 	 * using the volatile keyword for this
710ec94dbc5SRasesh Mody 	 */
711ec94dbc5SRasesh Mody 	OSAL_BARRIER(p_hwfn->p_dev);
712ec94dbc5SRasesh Mody 	while (*p_hwfn->dmae_info.p_completion_word != DMAE_COMPLETION_VAL) {
713ec94dbc5SRasesh Mody 		OSAL_UDELAY(DMAE_MIN_WAIT_TIME);
714ec94dbc5SRasesh Mody 		if (++wait_cnt > wait_cnt_limit) {
715*58bb1ee4SRasesh Mody 			DP_NOTICE(p_hwfn->p_dev, false,
716ec94dbc5SRasesh Mody 				  "Timed-out waiting for operation to"
717ec94dbc5SRasesh Mody 				  " complete. Completion word is 0x%08x"
718ec94dbc5SRasesh Mody 				  " expected 0x%08x.\n",
719ec94dbc5SRasesh Mody 				  *p_hwfn->dmae_info.p_completion_word,
720ec94dbc5SRasesh Mody 				  DMAE_COMPLETION_VAL);
721ec94dbc5SRasesh Mody 			ecore_status = ECORE_TIMEOUT;
722ec94dbc5SRasesh Mody 			break;
723ec94dbc5SRasesh Mody 		}
724ec94dbc5SRasesh Mody 		/* to sync the completion_word since we are not
725ec94dbc5SRasesh Mody 		 * using the volatile keyword for p_completion_word
726ec94dbc5SRasesh Mody 		 */
727ec94dbc5SRasesh Mody 		OSAL_BARRIER(p_hwfn->p_dev);
728ec94dbc5SRasesh Mody 	}
729ec94dbc5SRasesh Mody 
730ec94dbc5SRasesh Mody 	if (ecore_status == ECORE_SUCCESS)
731ec94dbc5SRasesh Mody 		*p_hwfn->dmae_info.p_completion_word = 0;
732ec94dbc5SRasesh Mody 
733ec94dbc5SRasesh Mody 	return ecore_status;
734ec94dbc5SRasesh Mody }
735ec94dbc5SRasesh Mody 
736ea85629fSRasesh Mody enum ecore_dmae_address_type {
737ea85629fSRasesh Mody 	ECORE_DMAE_ADDRESS_HOST_VIRT,
738ea85629fSRasesh Mody 	ECORE_DMAE_ADDRESS_HOST_PHYS,
739ea85629fSRasesh Mody 	ECORE_DMAE_ADDRESS_GRC
740ea85629fSRasesh Mody };
741ea85629fSRasesh Mody 
742ec94dbc5SRasesh Mody static enum _ecore_status_t
ecore_dmae_execute_sub_operation(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u64 src_addr,u64 dst_addr,u8 src_type,u8 dst_type,u32 length_dw)743ec94dbc5SRasesh Mody ecore_dmae_execute_sub_operation(struct ecore_hwfn *p_hwfn,
744ec94dbc5SRasesh Mody 				 struct ecore_ptt *p_ptt,
745ec94dbc5SRasesh Mody 				 u64 src_addr,
746ec94dbc5SRasesh Mody 				 u64 dst_addr,
74722d07d93SRasesh Mody 				 u8 src_type, u8 dst_type, u32 length_dw)
748ec94dbc5SRasesh Mody {
749ec94dbc5SRasesh Mody 	dma_addr_t phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
750ec94dbc5SRasesh Mody 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
751ec94dbc5SRasesh Mody 	enum _ecore_status_t ecore_status = ECORE_SUCCESS;
752ec94dbc5SRasesh Mody 
753ec94dbc5SRasesh Mody 	switch (src_type) {
754ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_GRC:
755ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_HOST_PHYS:
75622d07d93SRasesh Mody 		cmd->src_addr_hi = OSAL_CPU_TO_LE32(DMA_HI(src_addr));
75722d07d93SRasesh Mody 		cmd->src_addr_lo = OSAL_CPU_TO_LE32(DMA_LO(src_addr));
758ec94dbc5SRasesh Mody 		break;
759ec94dbc5SRasesh Mody 		/* for virt source addresses we use the intermediate buffer. */
760ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_HOST_VIRT:
76122d07d93SRasesh Mody 		cmd->src_addr_hi = OSAL_CPU_TO_LE32(DMA_HI(phys));
76222d07d93SRasesh Mody 		cmd->src_addr_lo = OSAL_CPU_TO_LE32(DMA_LO(phys));
763ec94dbc5SRasesh Mody 		OSAL_MEMCPY(&p_hwfn->dmae_info.p_intermediate_buffer[0],
764ec94dbc5SRasesh Mody 			    (void *)(osal_uintptr_t)src_addr,
76522d07d93SRasesh Mody 			    length_dw * sizeof(u32));
766ec94dbc5SRasesh Mody 		break;
767ec94dbc5SRasesh Mody 	default:
768ec94dbc5SRasesh Mody 		return ECORE_INVAL;
769ec94dbc5SRasesh Mody 	}
770ec94dbc5SRasesh Mody 
771ec94dbc5SRasesh Mody 	switch (dst_type) {
772ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_GRC:
773ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_HOST_PHYS:
77422d07d93SRasesh Mody 		cmd->dst_addr_hi = OSAL_CPU_TO_LE32(DMA_HI(dst_addr));
77522d07d93SRasesh Mody 		cmd->dst_addr_lo = OSAL_CPU_TO_LE32(DMA_LO(dst_addr));
776ec94dbc5SRasesh Mody 		break;
777ec94dbc5SRasesh Mody 		/* for virt destination address we use the intermediate buff. */
778ec94dbc5SRasesh Mody 	case ECORE_DMAE_ADDRESS_HOST_VIRT:
77922d07d93SRasesh Mody 		cmd->dst_addr_hi = OSAL_CPU_TO_LE32(DMA_HI(phys));
78022d07d93SRasesh Mody 		cmd->dst_addr_lo = OSAL_CPU_TO_LE32(DMA_LO(phys));
781ec94dbc5SRasesh Mody 		break;
782ec94dbc5SRasesh Mody 	default:
783ec94dbc5SRasesh Mody 		return ECORE_INVAL;
784ec94dbc5SRasesh Mody 	}
785ec94dbc5SRasesh Mody 
78622d07d93SRasesh Mody 	cmd->length_dw = OSAL_CPU_TO_LE16((u16)length_dw);
787ec94dbc5SRasesh Mody 
788ec94dbc5SRasesh Mody 	if (src_type == ECORE_DMAE_ADDRESS_HOST_VIRT ||
789ec94dbc5SRasesh Mody 	    src_type == ECORE_DMAE_ADDRESS_HOST_PHYS)
790ec94dbc5SRasesh Mody 		OSAL_DMA_SYNC(p_hwfn->p_dev,
791ec94dbc5SRasesh Mody 			      (void *)HILO_U64(cmd->src_addr_hi,
792ec94dbc5SRasesh Mody 					       cmd->src_addr_lo),
79322d07d93SRasesh Mody 			      length_dw * sizeof(u32), false);
794ec94dbc5SRasesh Mody 
795ec94dbc5SRasesh Mody 	ecore_dmae_post_command(p_hwfn, p_ptt);
796ec94dbc5SRasesh Mody 
797ec94dbc5SRasesh Mody 	ecore_status = ecore_dmae_operation_wait(p_hwfn);
798ec94dbc5SRasesh Mody 
799ec94dbc5SRasesh Mody 	/* TODO - is it true ? */
800ec94dbc5SRasesh Mody 	if (src_type == ECORE_DMAE_ADDRESS_HOST_VIRT ||
801ec94dbc5SRasesh Mody 	    src_type == ECORE_DMAE_ADDRESS_HOST_PHYS)
802ec94dbc5SRasesh Mody 		OSAL_DMA_SYNC(p_hwfn->p_dev,
803ec94dbc5SRasesh Mody 			      (void *)HILO_U64(cmd->src_addr_hi,
804ec94dbc5SRasesh Mody 					       cmd->src_addr_lo),
80522d07d93SRasesh Mody 			      length_dw * sizeof(u32), true);
806ec94dbc5SRasesh Mody 
807ec94dbc5SRasesh Mody 	if (ecore_status != ECORE_SUCCESS) {
808*58bb1ee4SRasesh Mody 		DP_NOTICE(p_hwfn, false,
80949f4b9dcSRasesh Mody 			  "Wait Failed. source_addr 0x%lx, grc_addr 0x%lx, size_in_dwords 0x%x, intermediate buffer 0x%lx.\n",
810ec94dbc5SRasesh Mody 			  (unsigned long)src_addr, (unsigned long)dst_addr,
81149f4b9dcSRasesh Mody 			  length_dw,
81249f4b9dcSRasesh Mody 			  (unsigned long)p_hwfn->dmae_info.intermediate_buffer_phys_addr);
813ec94dbc5SRasesh Mody 		return ecore_status;
814ec94dbc5SRasesh Mody 	}
815ec94dbc5SRasesh Mody 
816ec94dbc5SRasesh Mody 	if (dst_type == ECORE_DMAE_ADDRESS_HOST_VIRT)
817ec94dbc5SRasesh Mody 		OSAL_MEMCPY((void *)(osal_uintptr_t)(dst_addr),
818ec94dbc5SRasesh Mody 			    &p_hwfn->dmae_info.p_intermediate_buffer[0],
81922d07d93SRasesh Mody 			    length_dw * sizeof(u32));
820ec94dbc5SRasesh Mody 
821ec94dbc5SRasesh Mody 	return ECORE_SUCCESS;
822ec94dbc5SRasesh Mody }
823ec94dbc5SRasesh Mody 
824ec94dbc5SRasesh Mody static enum _ecore_status_t
ecore_dmae_execute_command(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u64 src_addr,u64 dst_addr,u8 src_type,u8 dst_type,u32 size_in_dwords,struct dmae_params * p_params)825ec94dbc5SRasesh Mody ecore_dmae_execute_command(struct ecore_hwfn *p_hwfn,
826ec94dbc5SRasesh Mody 			   struct ecore_ptt *p_ptt,
827ec94dbc5SRasesh Mody 			   u64 src_addr,
828ec94dbc5SRasesh Mody 			   u64 dst_addr,
829ec94dbc5SRasesh Mody 			   u8 src_type,
830ec94dbc5SRasesh Mody 			   u8 dst_type,
831ec94dbc5SRasesh Mody 			   u32 size_in_dwords,
832ea85629fSRasesh Mody 			   struct dmae_params *p_params)
833ec94dbc5SRasesh Mody {
834ec94dbc5SRasesh Mody 	dma_addr_t phys = p_hwfn->dmae_info.completion_word_phys_addr;
835ec94dbc5SRasesh Mody 	u16 length_cur = 0, i = 0, cnt_split = 0, length_mod = 0;
836ec94dbc5SRasesh Mody 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
837ec94dbc5SRasesh Mody 	u64 src_addr_split = 0, dst_addr_split = 0;
838ec94dbc5SRasesh Mody 	u16 length_limit = DMAE_MAX_RW_SIZE;
83922d07d93SRasesh Mody 	enum _ecore_status_t ecore_status = ECORE_SUCCESS;
840ec94dbc5SRasesh Mody 	u32 offset = 0;
841ec94dbc5SRasesh Mody 
84278e7fcefSRasesh Mody 	if (!p_hwfn->dmae_info.b_mem_ready) {
84378e7fcefSRasesh Mody 		DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
84478e7fcefSRasesh Mody 			   "No buffers allocated. Avoid DMAE transaction [{src: addr 0x%lx, type %d}, {dst: addr 0x%lx, type %d}, size %d].\n",
84578e7fcefSRasesh Mody 			   (unsigned long)src_addr, src_type,
84678e7fcefSRasesh Mody 			   (unsigned long)dst_addr, dst_type,
84778e7fcefSRasesh Mody 			   size_in_dwords);
84878e7fcefSRasesh Mody 		return ECORE_NOMEM;
84978e7fcefSRasesh Mody 	}
85078e7fcefSRasesh Mody 
851eb6088c1SRasesh Mody 	if (p_hwfn->p_dev->recov_in_prog) {
852eb6088c1SRasesh Mody 		DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
853eb6088c1SRasesh Mody 			   "Recovery is in progress. Avoid DMAE transaction [{src: addr 0x%lx, type %d}, {dst: addr 0x%lx, type %d}, size %d].\n",
854eb6088c1SRasesh Mody 			   (unsigned long)src_addr, src_type,
855eb6088c1SRasesh Mody 			   (unsigned long)dst_addr, dst_type,
856eb6088c1SRasesh Mody 			   size_in_dwords);
857eb6088c1SRasesh Mody 		/* Return success to let the flow to be completed successfully
858eb6088c1SRasesh Mody 		 * w/o any error handling.
859eb6088c1SRasesh Mody 		 */
860eb6088c1SRasesh Mody 		return ECORE_SUCCESS;
861eb6088c1SRasesh Mody 	}
862eb6088c1SRasesh Mody 
863b21e05c9SRasesh Mody 	if (!cmd) {
864b21e05c9SRasesh Mody 		DP_NOTICE(p_hwfn, true,
865b21e05c9SRasesh Mody 			  "ecore_dmae_execute_sub_operation failed. Invalid state. source_addr 0x%lx, destination addr 0x%lx, size_in_dwords 0x%x\n",
866b21e05c9SRasesh Mody 			  (unsigned long)src_addr,
867b21e05c9SRasesh Mody 			  (unsigned long)dst_addr,
868b21e05c9SRasesh Mody 			  length_cur);
869b21e05c9SRasesh Mody 		return ECORE_INVAL;
870b21e05c9SRasesh Mody 	}
871b21e05c9SRasesh Mody 
872ec94dbc5SRasesh Mody 	ecore_dmae_opcode(p_hwfn,
873ec94dbc5SRasesh Mody 			  (src_type == ECORE_DMAE_ADDRESS_GRC),
874ec94dbc5SRasesh Mody 			  (dst_type == ECORE_DMAE_ADDRESS_GRC), p_params);
875ec94dbc5SRasesh Mody 
87622d07d93SRasesh Mody 	cmd->comp_addr_lo = OSAL_CPU_TO_LE32(DMA_LO(phys));
87722d07d93SRasesh Mody 	cmd->comp_addr_hi = OSAL_CPU_TO_LE32(DMA_HI(phys));
87822d07d93SRasesh Mody 	cmd->comp_val = OSAL_CPU_TO_LE32(DMAE_COMPLETION_VAL);
879ec94dbc5SRasesh Mody 
880ec94dbc5SRasesh Mody 	/* Check if the grc_addr is valid like < MAX_GRC_OFFSET */
881ec94dbc5SRasesh Mody 	cnt_split = size_in_dwords / length_limit;
882ec94dbc5SRasesh Mody 	length_mod = size_in_dwords % length_limit;
883ec94dbc5SRasesh Mody 
884ec94dbc5SRasesh Mody 	src_addr_split = src_addr;
885ec94dbc5SRasesh Mody 	dst_addr_split = dst_addr;
886ec94dbc5SRasesh Mody 
887ec94dbc5SRasesh Mody 	for (i = 0; i <= cnt_split; i++) {
888ec94dbc5SRasesh Mody 		offset = length_limit * i;
889ec94dbc5SRasesh Mody 
8903eed444aSRasesh Mody 		if (!ECORE_DMAE_FLAGS_IS_SET(p_params, RW_REPL_SRC)) {
891ec94dbc5SRasesh Mody 			if (src_type == ECORE_DMAE_ADDRESS_GRC)
892ec94dbc5SRasesh Mody 				src_addr_split = src_addr + offset;
893ec94dbc5SRasesh Mody 			else
894ec94dbc5SRasesh Mody 				src_addr_split = src_addr + (offset * 4);
895ec94dbc5SRasesh Mody 		}
896ec94dbc5SRasesh Mody 
897ec94dbc5SRasesh Mody 		if (dst_type == ECORE_DMAE_ADDRESS_GRC)
898ec94dbc5SRasesh Mody 			dst_addr_split = dst_addr + offset;
899ec94dbc5SRasesh Mody 		else
900ec94dbc5SRasesh Mody 			dst_addr_split = dst_addr + (offset * 4);
901ec94dbc5SRasesh Mody 
902ec94dbc5SRasesh Mody 		length_cur = (cnt_split == i) ? length_mod : length_limit;
903ec94dbc5SRasesh Mody 
904ec94dbc5SRasesh Mody 		/* might be zero on last iteration */
905ec94dbc5SRasesh Mody 		if (!length_cur)
906ec94dbc5SRasesh Mody 			continue;
907ec94dbc5SRasesh Mody 
908ec94dbc5SRasesh Mody 		ecore_status = ecore_dmae_execute_sub_operation(p_hwfn,
909ec94dbc5SRasesh Mody 								p_ptt,
910ec94dbc5SRasesh Mody 								src_addr_split,
911ec94dbc5SRasesh Mody 								dst_addr_split,
912ec94dbc5SRasesh Mody 								src_type,
913ec94dbc5SRasesh Mody 								dst_type,
914ec94dbc5SRasesh Mody 								length_cur);
915ec94dbc5SRasesh Mody 		if (ecore_status != ECORE_SUCCESS) {
916ec94dbc5SRasesh Mody 			DP_NOTICE(p_hwfn, false,
917ec94dbc5SRasesh Mody 				  "ecore_dmae_execute_sub_operation Failed"
918ec94dbc5SRasesh Mody 				  " with error 0x%x. source_addr 0x%lx,"
919ec94dbc5SRasesh Mody 				  " dest addr 0x%lx, size_in_dwords 0x%x\n",
920ec94dbc5SRasesh Mody 				  ecore_status, (unsigned long)src_addr,
921ec94dbc5SRasesh Mody 				  (unsigned long)dst_addr, length_cur);
922ec94dbc5SRasesh Mody 
923ec94dbc5SRasesh Mody 			ecore_hw_err_notify(p_hwfn, ECORE_HW_ERR_DMAE_FAIL);
924ec94dbc5SRasesh Mody 			break;
925ec94dbc5SRasesh Mody 		}
926ec94dbc5SRasesh Mody 	}
927ec94dbc5SRasesh Mody 
928ec94dbc5SRasesh Mody 	return ecore_status;
929ec94dbc5SRasesh Mody }
930ec94dbc5SRasesh Mody 
ecore_dmae_host2grc(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u64 source_addr,u32 grc_addr,u32 size_in_dwords,struct dmae_params * p_params)9313eed444aSRasesh Mody enum _ecore_status_t ecore_dmae_host2grc(struct ecore_hwfn *p_hwfn,
932ec94dbc5SRasesh Mody 					 struct ecore_ptt *p_ptt,
933ec94dbc5SRasesh Mody 					 u64 source_addr,
9343eed444aSRasesh Mody 					 u32 grc_addr,
9353eed444aSRasesh Mody 					 u32 size_in_dwords,
936ea85629fSRasesh Mody 					 struct dmae_params *p_params)
937ec94dbc5SRasesh Mody {
938ec94dbc5SRasesh Mody 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
939ec94dbc5SRasesh Mody 	enum _ecore_status_t rc;
940ec94dbc5SRasesh Mody 
94178e7fcefSRasesh Mody 	OSAL_SPIN_LOCK(&p_hwfn->dmae_info.lock);
942ec94dbc5SRasesh Mody 
943ec94dbc5SRasesh Mody 	rc = ecore_dmae_execute_command(p_hwfn, p_ptt, source_addr,
944ec94dbc5SRasesh Mody 					grc_addr_in_dw,
945ec94dbc5SRasesh Mody 					ECORE_DMAE_ADDRESS_HOST_VIRT,
946ec94dbc5SRasesh Mody 					ECORE_DMAE_ADDRESS_GRC,
9473eed444aSRasesh Mody 					size_in_dwords, p_params);
948ec94dbc5SRasesh Mody 
94978e7fcefSRasesh Mody 	OSAL_SPIN_UNLOCK(&p_hwfn->dmae_info.lock);
950ec94dbc5SRasesh Mody 
951ec94dbc5SRasesh Mody 	return rc;
952ec94dbc5SRasesh Mody }
953ec94dbc5SRasesh Mody 
ecore_dmae_grc2host(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u32 grc_addr,dma_addr_t dest_addr,u32 size_in_dwords,struct dmae_params * p_params)9543eed444aSRasesh Mody enum _ecore_status_t ecore_dmae_grc2host(struct ecore_hwfn *p_hwfn,
955ec94dbc5SRasesh Mody 					 struct ecore_ptt *p_ptt,
956ec94dbc5SRasesh Mody 					 u32 grc_addr,
9573eed444aSRasesh Mody 					 dma_addr_t dest_addr,
9583eed444aSRasesh Mody 					 u32 size_in_dwords,
959ea85629fSRasesh Mody 					 struct dmae_params *p_params)
960ec94dbc5SRasesh Mody {
961ec94dbc5SRasesh Mody 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
962ec94dbc5SRasesh Mody 	enum _ecore_status_t rc;
963ec94dbc5SRasesh Mody 
96478e7fcefSRasesh Mody 	OSAL_SPIN_LOCK(&p_hwfn->dmae_info.lock);
965ec94dbc5SRasesh Mody 
966ec94dbc5SRasesh Mody 	rc = ecore_dmae_execute_command(p_hwfn, p_ptt, grc_addr_in_dw,
967ec94dbc5SRasesh Mody 					dest_addr, ECORE_DMAE_ADDRESS_GRC,
968ec94dbc5SRasesh Mody 					ECORE_DMAE_ADDRESS_HOST_VIRT,
9693eed444aSRasesh Mody 					size_in_dwords, p_params);
970ec94dbc5SRasesh Mody 
97178e7fcefSRasesh Mody 	OSAL_SPIN_UNLOCK(&p_hwfn->dmae_info.lock);
972ec94dbc5SRasesh Mody 
973ec94dbc5SRasesh Mody 	return rc;
974ec94dbc5SRasesh Mody }
975ec94dbc5SRasesh Mody 
976ec94dbc5SRasesh Mody enum _ecore_status_t
ecore_dmae_host2host(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,dma_addr_t source_addr,dma_addr_t dest_addr,u32 size_in_dwords,struct dmae_params * p_params)977ec94dbc5SRasesh Mody ecore_dmae_host2host(struct ecore_hwfn *p_hwfn,
978ec94dbc5SRasesh Mody 		     struct ecore_ptt *p_ptt,
979ec94dbc5SRasesh Mody 		     dma_addr_t source_addr,
980ec94dbc5SRasesh Mody 		     dma_addr_t dest_addr,
981ea85629fSRasesh Mody 		     u32 size_in_dwords,
982ea85629fSRasesh Mody 					  struct dmae_params *p_params)
983ec94dbc5SRasesh Mody {
984ec94dbc5SRasesh Mody 	enum _ecore_status_t rc;
985ec94dbc5SRasesh Mody 
98678e7fcefSRasesh Mody 	OSAL_SPIN_LOCK(&p_hwfn->dmae_info.lock);
987ec94dbc5SRasesh Mody 
988ec94dbc5SRasesh Mody 	rc = ecore_dmae_execute_command(p_hwfn, p_ptt, source_addr,
989ec94dbc5SRasesh Mody 					dest_addr,
990ec94dbc5SRasesh Mody 					ECORE_DMAE_ADDRESS_HOST_PHYS,
991ec94dbc5SRasesh Mody 					ECORE_DMAE_ADDRESS_HOST_PHYS,
992ec94dbc5SRasesh Mody 					size_in_dwords, p_params);
993ec94dbc5SRasesh Mody 
99478e7fcefSRasesh Mody 	OSAL_SPIN_UNLOCK(&p_hwfn->dmae_info.lock);
995ec94dbc5SRasesh Mody 
996ec94dbc5SRasesh Mody 	return rc;
997ec94dbc5SRasesh Mody }
998ec94dbc5SRasesh Mody 
ecore_hw_err_notify(struct ecore_hwfn * p_hwfn,enum ecore_hw_err_type err_type)999ec94dbc5SRasesh Mody void ecore_hw_err_notify(struct ecore_hwfn *p_hwfn,
1000ec94dbc5SRasesh Mody 			 enum ecore_hw_err_type err_type)
1001ec94dbc5SRasesh Mody {
1002ec94dbc5SRasesh Mody 	/* Fan failure cannot be masked by handling of another HW error */
1003ec94dbc5SRasesh Mody 	if (p_hwfn->p_dev->recov_in_prog && err_type != ECORE_HW_ERR_FAN_FAIL) {
1004ec94dbc5SRasesh Mody 		DP_VERBOSE(p_hwfn, ECORE_MSG_DRV,
1005ec94dbc5SRasesh Mody 			   "Recovery is in progress."
1006ec94dbc5SRasesh Mody 			   "Avoid notifying about HW error %d.\n",
1007ec94dbc5SRasesh Mody 			   err_type);
1008ec94dbc5SRasesh Mody 		return;
1009ec94dbc5SRasesh Mody 	}
1010ec94dbc5SRasesh Mody 
1011ec94dbc5SRasesh Mody 	OSAL_HW_ERROR_OCCURRED(p_hwfn, err_type);
1012ec94dbc5SRasesh Mody }
10131d86cc99SRasesh Mody 
ecore_dmae_sanity(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,const char * phase)10141d86cc99SRasesh Mody enum _ecore_status_t ecore_dmae_sanity(struct ecore_hwfn *p_hwfn,
10151d86cc99SRasesh Mody 				       struct ecore_ptt *p_ptt,
10161d86cc99SRasesh Mody 				       const char *phase)
10171d86cc99SRasesh Mody {
10181d86cc99SRasesh Mody 	u32 size = OSAL_PAGE_SIZE / 2, val;
10191d86cc99SRasesh Mody 	enum _ecore_status_t rc = ECORE_SUCCESS;
10201d86cc99SRasesh Mody 	dma_addr_t p_phys;
10211d86cc99SRasesh Mody 	void *p_virt;
10221d86cc99SRasesh Mody 	u32 *p_tmp;
10231d86cc99SRasesh Mody 
10241d86cc99SRasesh Mody 	p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, &p_phys, 2 * size);
10251d86cc99SRasesh Mody 	if (!p_virt) {
10261d86cc99SRasesh Mody 		DP_NOTICE(p_hwfn, false,
10271d86cc99SRasesh Mody 			  "DMAE sanity [%s]: failed to allocate memory\n",
10281d86cc99SRasesh Mody 			  phase);
10291d86cc99SRasesh Mody 		return ECORE_NOMEM;
10301d86cc99SRasesh Mody 	}
10311d86cc99SRasesh Mody 
10321d86cc99SRasesh Mody 	/* Fill the bottom half of the allocated memory with a known pattern */
10331d86cc99SRasesh Mody 	for (p_tmp = (u32 *)p_virt;
10341d86cc99SRasesh Mody 	     p_tmp < (u32 *)((u8 *)p_virt + size);
10351d86cc99SRasesh Mody 	     p_tmp++) {
10361d86cc99SRasesh Mody 		/* Save the address itself as the value */
10371d86cc99SRasesh Mody 		val = (u32)(osal_uintptr_t)p_tmp;
10381d86cc99SRasesh Mody 		*p_tmp = val;
10391d86cc99SRasesh Mody 	}
10401d86cc99SRasesh Mody 
10411d86cc99SRasesh Mody 	/* Zero the top half of the allocated memory */
10421d86cc99SRasesh Mody 	OSAL_MEM_ZERO((u8 *)p_virt + size, size);
10431d86cc99SRasesh Mody 
10441d86cc99SRasesh Mody 	DP_VERBOSE(p_hwfn, ECORE_MSG_SP,
10451d86cc99SRasesh Mody 		   "DMAE sanity [%s]: src_addr={phys 0x%lx, virt %p}, dst_addr={phys 0x%lx, virt %p}, size 0x%x\n",
10461d86cc99SRasesh Mody 		   phase, (unsigned long)p_phys, p_virt,
10471d86cc99SRasesh Mody 		   (unsigned long)(p_phys + size),
10481d86cc99SRasesh Mody 		   (u8 *)p_virt + size, size);
10491d86cc99SRasesh Mody 
10501d86cc99SRasesh Mody 	rc = ecore_dmae_host2host(p_hwfn, p_ptt, p_phys, p_phys + size,
10513eed444aSRasesh Mody 				  size / 4 /* size_in_dwords */,
10523eed444aSRasesh Mody 				  OSAL_NULL /* default parameters */);
10531d86cc99SRasesh Mody 	if (rc != ECORE_SUCCESS) {
10541d86cc99SRasesh Mody 		DP_NOTICE(p_hwfn, false,
10551d86cc99SRasesh Mody 			  "DMAE sanity [%s]: ecore_dmae_host2host() failed. rc = %d.\n",
10561d86cc99SRasesh Mody 			  phase, rc);
10571d86cc99SRasesh Mody 		goto out;
10581d86cc99SRasesh Mody 	}
10591d86cc99SRasesh Mody 
10601d86cc99SRasesh Mody 	/* Verify that the top half of the allocated memory has the pattern */
10611d86cc99SRasesh Mody 	for (p_tmp = (u32 *)((u8 *)p_virt + size);
10621d86cc99SRasesh Mody 	     p_tmp < (u32 *)((u8 *)p_virt + (2 * size));
10631d86cc99SRasesh Mody 	     p_tmp++) {
10641d86cc99SRasesh Mody 		/* The corresponding address in the bottom half */
10651d86cc99SRasesh Mody 		val = (u32)(osal_uintptr_t)p_tmp - size;
10661d86cc99SRasesh Mody 
10671d86cc99SRasesh Mody 		if (*p_tmp != val) {
10681d86cc99SRasesh Mody 			DP_NOTICE(p_hwfn, false,
10691d86cc99SRasesh Mody 				  "DMAE sanity [%s]: addr={phys 0x%lx, virt %p}, read_val 0x%08x, expected_val 0x%08x\n",
10701d86cc99SRasesh Mody 				  phase,
10711d86cc99SRasesh Mody 				  (unsigned long)p_phys +
10721d86cc99SRasesh Mody 				   ((u8 *)p_tmp - (u8 *)p_virt),
10731d86cc99SRasesh Mody 				  p_tmp, *p_tmp, val);
10741d86cc99SRasesh Mody 			rc = ECORE_UNKNOWN_ERROR;
10751d86cc99SRasesh Mody 			goto out;
10761d86cc99SRasesh Mody 		}
10771d86cc99SRasesh Mody 	}
10781d86cc99SRasesh Mody 
10791d86cc99SRasesh Mody out:
10801d86cc99SRasesh Mody 	OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev, p_virt, p_phys, 2 * size);
10811d86cc99SRasesh Mody 	return rc;
10821d86cc99SRasesh Mody }
10833eed444aSRasesh Mody 
ecore_ppfid_wr(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u8 abs_ppfid,u32 hw_addr,u32 val)10843eed444aSRasesh Mody void ecore_ppfid_wr(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
10853eed444aSRasesh Mody 		    u8 abs_ppfid, u32 hw_addr, u32 val)
10863eed444aSRasesh Mody {
10873eed444aSRasesh Mody 	u8 pfid = ECORE_PFID_BY_PPFID(p_hwfn, abs_ppfid);
10883eed444aSRasesh Mody 
10893eed444aSRasesh Mody 	ecore_fid_pretend(p_hwfn, p_ptt,
10903eed444aSRasesh Mody 			  pfid << PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
10913eed444aSRasesh Mody 	ecore_wr(p_hwfn, p_ptt, hw_addr, val);
10923eed444aSRasesh Mody 	ecore_fid_pretend(p_hwfn, p_ptt,
10933eed444aSRasesh Mody 			  p_hwfn->rel_pf_id <<
10943eed444aSRasesh Mody 			  PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
10953eed444aSRasesh Mody }
10963eed444aSRasesh Mody 
ecore_ppfid_rd(struct ecore_hwfn * p_hwfn,struct ecore_ptt * p_ptt,u8 abs_ppfid,u32 hw_addr)10973eed444aSRasesh Mody u32 ecore_ppfid_rd(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
10983eed444aSRasesh Mody 		   u8 abs_ppfid, u32 hw_addr)
10993eed444aSRasesh Mody {
11003eed444aSRasesh Mody 	u8 pfid = ECORE_PFID_BY_PPFID(p_hwfn, abs_ppfid);
11013eed444aSRasesh Mody 	u32 val;
11023eed444aSRasesh Mody 
11033eed444aSRasesh Mody 	ecore_fid_pretend(p_hwfn, p_ptt,
11043eed444aSRasesh Mody 			  pfid << PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
11053eed444aSRasesh Mody 	val = ecore_rd(p_hwfn, p_ptt, hw_addr);
11063eed444aSRasesh Mody 	ecore_fid_pretend(p_hwfn, p_ptt,
11073eed444aSRasesh Mody 			  p_hwfn->rel_pf_id <<
11083eed444aSRasesh Mody 			  PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
11093eed444aSRasesh Mody 
11103eed444aSRasesh Mody 	return val;
11113eed444aSRasesh Mody }
1112