xref: /linux/drivers/net/ethernet/qlogic/qed/qed_hw.c (revision 663eacd899ac131dcfc2279184c1ce3c4fd2815f)
11f4d4ed6SAlexander Lobakin // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2fe56b9e6SYuval Mintz /* QLogic qed NIC Driver
3e8f1cb50SMintz, Yuval  * Copyright (c) 2015-2017  QLogic Corporation
4*663eacd8SAlexander Lobakin  * Copyright (c) 2019-2020 Marvell International Ltd.
5fe56b9e6SYuval Mintz  */
6fe56b9e6SYuval Mintz 
7fe56b9e6SYuval Mintz #include <linux/types.h>
8fe56b9e6SYuval Mintz #include <linux/io.h>
9fe56b9e6SYuval Mintz #include <linux/delay.h>
10fe56b9e6SYuval Mintz #include <linux/dma-mapping.h>
11fe56b9e6SYuval Mintz #include <linux/errno.h>
12fe56b9e6SYuval Mintz #include <linux/kernel.h>
13fe56b9e6SYuval Mintz #include <linux/list.h>
14fe56b9e6SYuval Mintz #include <linux/mutex.h>
15fe56b9e6SYuval Mintz #include <linux/pci.h>
16fe56b9e6SYuval Mintz #include <linux/slab.h>
17fe56b9e6SYuval Mintz #include <linux/spinlock.h>
18fe56b9e6SYuval Mintz #include <linux/string.h>
19fe56b9e6SYuval Mintz #include <linux/qed/qed_chain.h>
20fe56b9e6SYuval Mintz #include "qed.h"
21fe56b9e6SYuval Mintz #include "qed_hsi.h"
22fe56b9e6SYuval Mintz #include "qed_hw.h"
23fe56b9e6SYuval Mintz #include "qed_reg_addr.h"
241408cc1fSYuval Mintz #include "qed_sriov.h"
25fe56b9e6SYuval Mintz 
26fe56b9e6SYuval Mintz #define QED_BAR_ACQUIRE_TIMEOUT 1000
27fe56b9e6SYuval Mintz 
28fe56b9e6SYuval Mintz /* Invalid values */
29fe56b9e6SYuval Mintz #define QED_BAR_INVALID_OFFSET          (cpu_to_le32(-1))
30fe56b9e6SYuval Mintz 
31fe56b9e6SYuval Mintz struct qed_ptt {
32fe56b9e6SYuval Mintz 	struct list_head	list_entry;
33fe56b9e6SYuval Mintz 	unsigned int		idx;
34fe56b9e6SYuval Mintz 	struct pxp_ptt_entry	pxp;
353a50d351SMintz, Yuval 	u8			hwfn_id;
36fe56b9e6SYuval Mintz };
37fe56b9e6SYuval Mintz 
38fe56b9e6SYuval Mintz struct qed_ptt_pool {
39fe56b9e6SYuval Mintz 	struct list_head	free_list;
40fe56b9e6SYuval Mintz 	spinlock_t		lock; /* ptt synchronized access */
41fe56b9e6SYuval Mintz 	struct qed_ptt		ptts[PXP_EXTERNAL_BAR_PF_WINDOW_NUM];
42fe56b9e6SYuval Mintz };
43fe56b9e6SYuval Mintz 
44fe56b9e6SYuval Mintz int qed_ptt_pool_alloc(struct qed_hwfn *p_hwfn)
45fe56b9e6SYuval Mintz {
461a635e48SYuval Mintz 	struct qed_ptt_pool *p_pool = kmalloc(sizeof(*p_pool), GFP_KERNEL);
47fe56b9e6SYuval Mintz 	int i;
48fe56b9e6SYuval Mintz 
49fe56b9e6SYuval Mintz 	if (!p_pool)
50fe56b9e6SYuval Mintz 		return -ENOMEM;
51fe56b9e6SYuval Mintz 
52fe56b9e6SYuval Mintz 	INIT_LIST_HEAD(&p_pool->free_list);
53fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
54fe56b9e6SYuval Mintz 		p_pool->ptts[i].idx = i;
55fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.offset = QED_BAR_INVALID_OFFSET;
56fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.pretend.control = 0;
573a50d351SMintz, Yuval 		p_pool->ptts[i].hwfn_id = p_hwfn->my_id;
58fe56b9e6SYuval Mintz 		if (i >= RESERVED_PTT_MAX)
59fe56b9e6SYuval Mintz 			list_add(&p_pool->ptts[i].list_entry,
60fe56b9e6SYuval Mintz 				 &p_pool->free_list);
61fe56b9e6SYuval Mintz 	}
62fe56b9e6SYuval Mintz 
63fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = p_pool;
64fe56b9e6SYuval Mintz 	spin_lock_init(&p_pool->lock);
65fe56b9e6SYuval Mintz 
66fe56b9e6SYuval Mintz 	return 0;
67fe56b9e6SYuval Mintz }
68fe56b9e6SYuval Mintz 
69fe56b9e6SYuval Mintz void qed_ptt_invalidate(struct qed_hwfn *p_hwfn)
70fe56b9e6SYuval Mintz {
71fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
72fe56b9e6SYuval Mintz 	int i;
73fe56b9e6SYuval Mintz 
74fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
75fe56b9e6SYuval Mintz 		p_ptt = &p_hwfn->p_ptt_pool->ptts[i];
76fe56b9e6SYuval Mintz 		p_ptt->pxp.offset = QED_BAR_INVALID_OFFSET;
77fe56b9e6SYuval Mintz 	}
78fe56b9e6SYuval Mintz }
79fe56b9e6SYuval Mintz 
80fe56b9e6SYuval Mintz void qed_ptt_pool_free(struct qed_hwfn *p_hwfn)
81fe56b9e6SYuval Mintz {
82fe56b9e6SYuval Mintz 	kfree(p_hwfn->p_ptt_pool);
83fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = NULL;
84fe56b9e6SYuval Mintz }
85fe56b9e6SYuval Mintz 
86fe56b9e6SYuval Mintz struct qed_ptt *qed_ptt_acquire(struct qed_hwfn *p_hwfn)
87fe56b9e6SYuval Mintz {
88fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
89fe56b9e6SYuval Mintz 	unsigned int i;
90fe56b9e6SYuval Mintz 
91fe56b9e6SYuval Mintz 	/* Take the free PTT from the list */
92fe56b9e6SYuval Mintz 	for (i = 0; i < QED_BAR_ACQUIRE_TIMEOUT; i++) {
93fe56b9e6SYuval Mintz 		spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
94fe56b9e6SYuval Mintz 
95fe56b9e6SYuval Mintz 		if (!list_empty(&p_hwfn->p_ptt_pool->free_list)) {
96fe56b9e6SYuval Mintz 			p_ptt = list_first_entry(&p_hwfn->p_ptt_pool->free_list,
97fe56b9e6SYuval Mintz 						 struct qed_ptt, list_entry);
98fe56b9e6SYuval Mintz 			list_del(&p_ptt->list_entry);
99fe56b9e6SYuval Mintz 
100fe56b9e6SYuval Mintz 			spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
101fe56b9e6SYuval Mintz 
102fe56b9e6SYuval Mintz 			DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
103fe56b9e6SYuval Mintz 				   "allocated ptt %d\n", p_ptt->idx);
104fe56b9e6SYuval Mintz 			return p_ptt;
105fe56b9e6SYuval Mintz 		}
106fe56b9e6SYuval Mintz 
107fe56b9e6SYuval Mintz 		spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
108fe56b9e6SYuval Mintz 		usleep_range(1000, 2000);
109fe56b9e6SYuval Mintz 	}
110fe56b9e6SYuval Mintz 
111fe56b9e6SYuval Mintz 	DP_NOTICE(p_hwfn, "PTT acquire timeout - failed to allocate PTT\n");
112fe56b9e6SYuval Mintz 	return NULL;
113fe56b9e6SYuval Mintz }
114fe56b9e6SYuval Mintz 
1151a635e48SYuval Mintz void qed_ptt_release(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
116fe56b9e6SYuval Mintz {
117fe56b9e6SYuval Mintz 	spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
118fe56b9e6SYuval Mintz 	list_add(&p_ptt->list_entry, &p_hwfn->p_ptt_pool->free_list);
119fe56b9e6SYuval Mintz 	spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
120fe56b9e6SYuval Mintz }
121fe56b9e6SYuval Mintz 
1221a635e48SYuval Mintz u32 qed_ptt_get_hw_addr(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
123fe56b9e6SYuval Mintz {
124fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and we need to translate it to Bytes */
125fe56b9e6SYuval Mintz 	return le32_to_cpu(p_ptt->pxp.offset) << 2;
126fe56b9e6SYuval Mintz }
127fe56b9e6SYuval Mintz 
128fe56b9e6SYuval Mintz static u32 qed_ptt_config_addr(struct qed_ptt *p_ptt)
129fe56b9e6SYuval Mintz {
130fe56b9e6SYuval Mintz 	return PXP_PF_WINDOW_ADMIN_PER_PF_START +
131fe56b9e6SYuval Mintz 	       p_ptt->idx * sizeof(struct pxp_ptt_entry);
132fe56b9e6SYuval Mintz }
133fe56b9e6SYuval Mintz 
134fe56b9e6SYuval Mintz u32 qed_ptt_get_bar_addr(struct qed_ptt *p_ptt)
135fe56b9e6SYuval Mintz {
136fe56b9e6SYuval Mintz 	return PXP_EXTERNAL_BAR_PF_WINDOW_START +
137fe56b9e6SYuval Mintz 	       p_ptt->idx * PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE;
138fe56b9e6SYuval Mintz }
139fe56b9e6SYuval Mintz 
140fe56b9e6SYuval Mintz void qed_ptt_set_win(struct qed_hwfn *p_hwfn,
1411a635e48SYuval Mintz 		     struct qed_ptt *p_ptt, u32 new_hw_addr)
142fe56b9e6SYuval Mintz {
143fe56b9e6SYuval Mintz 	u32 prev_hw_addr;
144fe56b9e6SYuval Mintz 
145fe56b9e6SYuval Mintz 	prev_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
146fe56b9e6SYuval Mintz 
147fe56b9e6SYuval Mintz 	if (new_hw_addr == prev_hw_addr)
148fe56b9e6SYuval Mintz 		return;
149fe56b9e6SYuval Mintz 
150fe56b9e6SYuval Mintz 	/* Update PTT entery in admin window */
151fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
152fe56b9e6SYuval Mintz 		   "Updating PTT entry %d to offset 0x%x\n",
153fe56b9e6SYuval Mintz 		   p_ptt->idx, new_hw_addr);
154fe56b9e6SYuval Mintz 
155fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and the address is in Bytes */
156fe56b9e6SYuval Mintz 	p_ptt->pxp.offset = cpu_to_le32(new_hw_addr >> 2);
157fe56b9e6SYuval Mintz 
158fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
159fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
160fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, offset),
161fe56b9e6SYuval Mintz 	       le32_to_cpu(p_ptt->pxp.offset));
162fe56b9e6SYuval Mintz }
163fe56b9e6SYuval Mintz 
164fe56b9e6SYuval Mintz static u32 qed_set_ptt(struct qed_hwfn *p_hwfn,
1651a635e48SYuval Mintz 		       struct qed_ptt *p_ptt, u32 hw_addr)
166fe56b9e6SYuval Mintz {
167fe56b9e6SYuval Mintz 	u32 win_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
168fe56b9e6SYuval Mintz 	u32 offset;
169fe56b9e6SYuval Mintz 
170fe56b9e6SYuval Mintz 	offset = hw_addr - win_hw_addr;
171fe56b9e6SYuval Mintz 
1723a50d351SMintz, Yuval 	if (p_ptt->hwfn_id != p_hwfn->my_id)
1733a50d351SMintz, Yuval 		DP_NOTICE(p_hwfn,
1743a50d351SMintz, Yuval 			  "ptt[%d] of hwfn[%02x] is used by hwfn[%02x]!\n",
1753a50d351SMintz, Yuval 			  p_ptt->idx, p_ptt->hwfn_id, p_hwfn->my_id);
1763a50d351SMintz, Yuval 
177fe56b9e6SYuval Mintz 	/* Verify the address is within the window */
178fe56b9e6SYuval Mintz 	if (hw_addr < win_hw_addr ||
179fe56b9e6SYuval Mintz 	    offset >= PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE) {
180fe56b9e6SYuval Mintz 		qed_ptt_set_win(p_hwfn, p_ptt, hw_addr);
181fe56b9e6SYuval Mintz 		offset = 0;
182fe56b9e6SYuval Mintz 	}
183fe56b9e6SYuval Mintz 
184fe56b9e6SYuval Mintz 	return qed_ptt_get_bar_addr(p_ptt) + offset;
185fe56b9e6SYuval Mintz }
186fe56b9e6SYuval Mintz 
187fe56b9e6SYuval Mintz struct qed_ptt *qed_get_reserved_ptt(struct qed_hwfn *p_hwfn,
188fe56b9e6SYuval Mintz 				     enum reserved_ptts ptt_idx)
189fe56b9e6SYuval Mintz {
190fe56b9e6SYuval Mintz 	if (ptt_idx >= RESERVED_PTT_MAX) {
191fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
192fe56b9e6SYuval Mintz 			  "Requested PTT %d is out of range\n", ptt_idx);
193fe56b9e6SYuval Mintz 		return NULL;
194fe56b9e6SYuval Mintz 	}
195fe56b9e6SYuval Mintz 
196fe56b9e6SYuval Mintz 	return &p_hwfn->p_ptt_pool->ptts[ptt_idx];
197fe56b9e6SYuval Mintz }
198fe56b9e6SYuval Mintz 
199fe56b9e6SYuval Mintz void qed_wr(struct qed_hwfn *p_hwfn,
200fe56b9e6SYuval Mintz 	    struct qed_ptt *p_ptt,
201fe56b9e6SYuval Mintz 	    u32 hw_addr, u32 val)
202fe56b9e6SYuval Mintz {
203fe56b9e6SYuval Mintz 	u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
204fe56b9e6SYuval Mintz 
205fe56b9e6SYuval Mintz 	REG_WR(p_hwfn, bar_addr, val);
206fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
207fe56b9e6SYuval Mintz 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
208fe56b9e6SYuval Mintz 		   bar_addr, hw_addr, val);
209fe56b9e6SYuval Mintz }
210fe56b9e6SYuval Mintz 
211fe56b9e6SYuval Mintz u32 qed_rd(struct qed_hwfn *p_hwfn,
212fe56b9e6SYuval Mintz 	   struct qed_ptt *p_ptt,
213fe56b9e6SYuval Mintz 	   u32 hw_addr)
214fe56b9e6SYuval Mintz {
215fe56b9e6SYuval Mintz 	u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
216fe56b9e6SYuval Mintz 	u32 val = REG_RD(p_hwfn, bar_addr);
217fe56b9e6SYuval Mintz 
218fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
219fe56b9e6SYuval Mintz 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
220fe56b9e6SYuval Mintz 		   bar_addr, hw_addr, val);
221fe56b9e6SYuval Mintz 
222fe56b9e6SYuval Mintz 	return val;
223fe56b9e6SYuval Mintz }
224fe56b9e6SYuval Mintz 
225fe56b9e6SYuval Mintz static void qed_memcpy_hw(struct qed_hwfn *p_hwfn,
226fe56b9e6SYuval Mintz 			  struct qed_ptt *p_ptt,
2271a635e48SYuval Mintz 			  void *addr, u32 hw_addr, size_t n, bool to_device)
228fe56b9e6SYuval Mintz {
229fe56b9e6SYuval Mintz 	u32 dw_count, *host_addr, hw_offset;
230fe56b9e6SYuval Mintz 	size_t quota, done = 0;
231fe56b9e6SYuval Mintz 	u32 __iomem *reg_addr;
232fe56b9e6SYuval Mintz 
233fe56b9e6SYuval Mintz 	while (done < n) {
234fe56b9e6SYuval Mintz 		quota = min_t(size_t, n - done,
235fe56b9e6SYuval Mintz 			      PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE);
236fe56b9e6SYuval Mintz 
2371408cc1fSYuval Mintz 		if (IS_PF(p_hwfn->cdev)) {
238fe56b9e6SYuval Mintz 			qed_ptt_set_win(p_hwfn, p_ptt, hw_addr + done);
239fe56b9e6SYuval Mintz 			hw_offset = qed_ptt_get_bar_addr(p_ptt);
2401408cc1fSYuval Mintz 		} else {
2411408cc1fSYuval Mintz 			hw_offset = hw_addr + done;
2421408cc1fSYuval Mintz 		}
243fe56b9e6SYuval Mintz 
244fe56b9e6SYuval Mintz 		dw_count = quota / 4;
245fe56b9e6SYuval Mintz 		host_addr = (u32 *)((u8 *)addr + done);
246fe56b9e6SYuval Mintz 		reg_addr = (u32 __iomem *)REG_ADDR(p_hwfn, hw_offset);
247fe56b9e6SYuval Mintz 		if (to_device)
248fe56b9e6SYuval Mintz 			while (dw_count--)
249fe56b9e6SYuval Mintz 				DIRECT_REG_WR(reg_addr++, *host_addr++);
250fe56b9e6SYuval Mintz 		else
251fe56b9e6SYuval Mintz 			while (dw_count--)
252fe56b9e6SYuval Mintz 				*host_addr++ = DIRECT_REG_RD(reg_addr++);
253fe56b9e6SYuval Mintz 
254fe56b9e6SYuval Mintz 		done += quota;
255fe56b9e6SYuval Mintz 	}
256fe56b9e6SYuval Mintz }
257fe56b9e6SYuval Mintz 
258fe56b9e6SYuval Mintz void qed_memcpy_from(struct qed_hwfn *p_hwfn,
2591a635e48SYuval Mintz 		     struct qed_ptt *p_ptt, void *dest, u32 hw_addr, size_t n)
260fe56b9e6SYuval Mintz {
261fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
262fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, dest %p hw_addr 0x%x, size %lu\n",
263fe56b9e6SYuval Mintz 		   hw_addr, dest, hw_addr, (unsigned long)n);
264fe56b9e6SYuval Mintz 
265fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, dest, hw_addr, n, false);
266fe56b9e6SYuval Mintz }
267fe56b9e6SYuval Mintz 
268fe56b9e6SYuval Mintz void qed_memcpy_to(struct qed_hwfn *p_hwfn,
2691a635e48SYuval Mintz 		   struct qed_ptt *p_ptt, u32 hw_addr, void *src, size_t n)
270fe56b9e6SYuval Mintz {
271fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
272fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, hw_addr 0x%x, src %p size %lu\n",
273fe56b9e6SYuval Mintz 		   hw_addr, hw_addr, src, (unsigned long)n);
274fe56b9e6SYuval Mintz 
275fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, src, hw_addr, n, true);
276fe56b9e6SYuval Mintz }
277fe56b9e6SYuval Mintz 
2781a635e48SYuval Mintz void qed_fid_pretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt, u16 fid)
279fe56b9e6SYuval Mintz {
280fe56b9e6SYuval Mintz 	u16 control = 0;
281fe56b9e6SYuval Mintz 
282fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
283fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
284fe56b9e6SYuval Mintz 
285fe56b9e6SYuval Mintz 	/* Every pretend undos previous pretends, including
286fe56b9e6SYuval Mintz 	 * previous port pretend.
287fe56b9e6SYuval Mintz 	 */
288fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
289fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
290fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
291fe56b9e6SYuval Mintz 
292fe56b9e6SYuval Mintz 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
293fe56b9e6SYuval Mintz 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
294fe56b9e6SYuval Mintz 
295fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
296fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
297fe56b9e6SYuval Mintz 
298fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
299fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
300fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
301fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
302fe56b9e6SYuval Mintz }
303fe56b9e6SYuval Mintz 
304fe56b9e6SYuval Mintz void qed_port_pretend(struct qed_hwfn *p_hwfn,
3051a635e48SYuval Mintz 		      struct qed_ptt *p_ptt, u8 port_id)
306fe56b9e6SYuval Mintz {
307fe56b9e6SYuval Mintz 	u16 control = 0;
308fe56b9e6SYuval Mintz 
309fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
310fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
311fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
312fe56b9e6SYuval Mintz 
313fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
314fe56b9e6SYuval Mintz 
315fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
316fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
317fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
318fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
319fe56b9e6SYuval Mintz }
320fe56b9e6SYuval Mintz 
3211a635e48SYuval Mintz void qed_port_unpretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
322fe56b9e6SYuval Mintz {
323fe56b9e6SYuval Mintz 	u16 control = 0;
324fe56b9e6SYuval Mintz 
325fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
326fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
327fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
328fe56b9e6SYuval Mintz 
329fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
330fe56b9e6SYuval Mintz 
331fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
332fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
333fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
334fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
335fe56b9e6SYuval Mintz }
336fe56b9e6SYuval Mintz 
337d52c89f1SMichal Kalderon void qed_port_fid_pretend(struct qed_hwfn *p_hwfn,
338d52c89f1SMichal Kalderon 			  struct qed_ptt *p_ptt, u8 port_id, u16 fid)
339d52c89f1SMichal Kalderon {
340d52c89f1SMichal Kalderon 	u16 control = 0;
341d52c89f1SMichal Kalderon 
342d52c89f1SMichal Kalderon 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
343d52c89f1SMichal Kalderon 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
344d52c89f1SMichal Kalderon 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
345d52c89f1SMichal Kalderon 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
346d52c89f1SMichal Kalderon 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
347d52c89f1SMichal Kalderon 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
348d52c89f1SMichal Kalderon 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
349d52c89f1SMichal Kalderon 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
350d52c89f1SMichal Kalderon 	p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
351d52c89f1SMichal Kalderon 	REG_WR(p_hwfn,
352d52c89f1SMichal Kalderon 	       qed_ptt_config_addr(p_ptt) +
353d52c89f1SMichal Kalderon 	       offsetof(struct pxp_ptt_entry, pretend),
354d52c89f1SMichal Kalderon 	       *(u32 *)&p_ptt->pxp.pretend);
355d52c89f1SMichal Kalderon }
356d52c89f1SMichal Kalderon 
35732a47e72SYuval Mintz u32 qed_vfid_to_concrete(struct qed_hwfn *p_hwfn, u8 vfid)
35832a47e72SYuval Mintz {
35932a47e72SYuval Mintz 	u32 concrete_fid = 0;
36032a47e72SYuval Mintz 
36132a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_PFID, p_hwfn->rel_pf_id);
36232a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFID, vfid);
36332a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFVALID, 1);
36432a47e72SYuval Mintz 
36532a47e72SYuval Mintz 	return concrete_fid;
36632a47e72SYuval Mintz }
36732a47e72SYuval Mintz 
368fe56b9e6SYuval Mintz /* DMAE */
36983bf76e3SMichal Kalderon #define QED_DMAE_FLAGS_IS_SET(params, flag) \
370804c5702SMichal Kalderon 	((params) != NULL && GET_FIELD((params)->flags, QED_DMAE_PARAMS_##flag))
37183bf76e3SMichal Kalderon 
372fe56b9e6SYuval Mintz static void qed_dmae_opcode(struct qed_hwfn *p_hwfn,
373fe56b9e6SYuval Mintz 			    const u8 is_src_type_grc,
374fe56b9e6SYuval Mintz 			    const u8 is_dst_type_grc,
375fe56b9e6SYuval Mintz 			    struct qed_dmae_params *p_params)
376fe56b9e6SYuval Mintz {
37783bf76e3SMichal Kalderon 	u8 src_pfid, dst_pfid, port_id;
37837bff2b9SYuval Mintz 	u16 opcode_b = 0;
379fe56b9e6SYuval Mintz 	u32 opcode = 0;
380fe56b9e6SYuval Mintz 
381fe56b9e6SYuval Mintz 	/* Whether the source is the PCIe or the GRC.
382fe56b9e6SYuval Mintz 	 * 0- The source is the PCIe
383fe56b9e6SYuval Mintz 	 * 1- The source is the GRC.
384fe56b9e6SYuval Mintz 	 */
385804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_SRC,
386804c5702SMichal Kalderon 		  (is_src_type_grc ? dmae_cmd_src_grc : dmae_cmd_src_pcie));
387804c5702SMichal Kalderon 	src_pfid = QED_DMAE_FLAGS_IS_SET(p_params, SRC_PF_VALID) ?
38883bf76e3SMichal Kalderon 	    p_params->src_pfid : p_hwfn->rel_pf_id;
389804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_SRC_PF_ID, src_pfid);
390fe56b9e6SYuval Mintz 
391fe56b9e6SYuval Mintz 	/* The destination of the DMA can be: 0-None 1-PCIe 2-GRC 3-None */
392804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_DST,
393804c5702SMichal Kalderon 		  (is_dst_type_grc ? dmae_cmd_dst_grc : dmae_cmd_dst_pcie));
394804c5702SMichal Kalderon 	dst_pfid = QED_DMAE_FLAGS_IS_SET(p_params, DST_PF_VALID) ?
39583bf76e3SMichal Kalderon 	    p_params->dst_pfid : p_hwfn->rel_pf_id;
396804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_DST_PF_ID, dst_pfid);
397804c5702SMichal Kalderon 
398fe56b9e6SYuval Mintz 
399fe56b9e6SYuval Mintz 	/* Whether to write a completion word to the completion destination:
400fe56b9e6SYuval Mintz 	 * 0-Do not write a completion word
401fe56b9e6SYuval Mintz 	 * 1-Write the completion word
402fe56b9e6SYuval Mintz 	 */
403804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_COMP_WORD_EN, 1);
404804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_SRC_ADDR_RESET, 1);
405fe56b9e6SYuval Mintz 
40683bf76e3SMichal Kalderon 	if (QED_DMAE_FLAGS_IS_SET(p_params, COMPLETION_DST))
407804c5702SMichal Kalderon 		SET_FIELD(opcode, DMAE_CMD_COMP_FUNC, 1);
408fe56b9e6SYuval Mintz 
409804c5702SMichal Kalderon 	/* swapping mode 3 - big endian */
410804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_ENDIANITY_MODE, DMAE_CMD_ENDIANITY);
411fe56b9e6SYuval Mintz 
412804c5702SMichal Kalderon 	port_id = (QED_DMAE_FLAGS_IS_SET(p_params, PORT_VALID)) ?
41383bf76e3SMichal Kalderon 	    p_params->port_id : p_hwfn->port_id;
414804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_PORT_ID, port_id);
415fe56b9e6SYuval Mintz 
416fe56b9e6SYuval Mintz 	/* reset source address in next go */
417804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_SRC_ADDR_RESET, 1);
418fe56b9e6SYuval Mintz 
419fe56b9e6SYuval Mintz 	/* reset dest address in next go */
420804c5702SMichal Kalderon 	SET_FIELD(opcode, DMAE_CMD_DST_ADDR_RESET, 1);
421fe56b9e6SYuval Mintz 
42237bff2b9SYuval Mintz 	/* SRC/DST VFID: all 1's - pf, otherwise VF id */
423804c5702SMichal Kalderon 	if (QED_DMAE_FLAGS_IS_SET(p_params, SRC_VF_VALID)) {
424804c5702SMichal Kalderon 		SET_FIELD(opcode, DMAE_CMD_SRC_VF_ID_VALID, 1);
425804c5702SMichal Kalderon 		SET_FIELD(opcode_b, DMAE_CMD_SRC_VF_ID, p_params->src_vfid);
42637bff2b9SYuval Mintz 	} else {
427804c5702SMichal Kalderon 		SET_FIELD(opcode_b, DMAE_CMD_SRC_VF_ID, 0xFF);
42837bff2b9SYuval Mintz 	}
429804c5702SMichal Kalderon 	if (QED_DMAE_FLAGS_IS_SET(p_params, DST_VF_VALID)) {
430804c5702SMichal Kalderon 		SET_FIELD(opcode, DMAE_CMD_DST_VF_ID_VALID, 1);
431804c5702SMichal Kalderon 		SET_FIELD(opcode_b, DMAE_CMD_DST_VF_ID, p_params->dst_vfid);
43237bff2b9SYuval Mintz 	} else {
433804c5702SMichal Kalderon 		SET_FIELD(opcode_b, DMAE_CMD_DST_VF_ID, 0xFF);
43437bff2b9SYuval Mintz 	}
435fe56b9e6SYuval Mintz 
436fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode = cpu_to_le32(opcode);
43737bff2b9SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode_b = cpu_to_le16(opcode_b);
438fe56b9e6SYuval Mintz }
439fe56b9e6SYuval Mintz 
440fe56b9e6SYuval Mintz u32 qed_dmae_idx_to_go_cmd(u8 idx)
441fe56b9e6SYuval Mintz {
442fe56b9e6SYuval Mintz 	/* All the DMAE 'go' registers form an array in internal memory */
443fe56b9e6SYuval Mintz 	return DMAE_REG_GO_C0 + (idx << 2);
444fe56b9e6SYuval Mintz }
445fe56b9e6SYuval Mintz 
4461a635e48SYuval Mintz static int qed_dmae_post_command(struct qed_hwfn *p_hwfn,
447fe56b9e6SYuval Mintz 				 struct qed_ptt *p_ptt)
448fe56b9e6SYuval Mintz {
4491a635e48SYuval Mintz 	struct dmae_cmd *p_command = p_hwfn->dmae_info.p_dmae_cmd;
450fe56b9e6SYuval Mintz 	u8 idx_cmd = p_hwfn->dmae_info.channel, i;
451fe56b9e6SYuval Mintz 	int qed_status = 0;
452fe56b9e6SYuval Mintz 
453fe56b9e6SYuval Mintz 	/* verify address is not NULL */
4541a635e48SYuval Mintz 	if ((((!p_command->dst_addr_lo) && (!p_command->dst_addr_hi)) ||
4551a635e48SYuval Mintz 	     ((!p_command->src_addr_lo) && (!p_command->src_addr_hi)))) {
456fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
457fe56b9e6SYuval Mintz 			  "source or destination address 0 idx_cmd=%d\n"
458fe56b9e6SYuval Mintz 			  "opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
459fe56b9e6SYuval Mintz 			  idx_cmd,
4601a635e48SYuval Mintz 			  le32_to_cpu(p_command->opcode),
4611a635e48SYuval Mintz 			  le16_to_cpu(p_command->opcode_b),
4621a635e48SYuval Mintz 			  le16_to_cpu(p_command->length_dw),
4631a635e48SYuval Mintz 			  le32_to_cpu(p_command->src_addr_hi),
4641a635e48SYuval Mintz 			  le32_to_cpu(p_command->src_addr_lo),
4651a635e48SYuval Mintz 			  le32_to_cpu(p_command->dst_addr_hi),
4661a635e48SYuval Mintz 			  le32_to_cpu(p_command->dst_addr_lo));
467fe56b9e6SYuval Mintz 
468fe56b9e6SYuval Mintz 		return -EINVAL;
469fe56b9e6SYuval Mintz 	}
470fe56b9e6SYuval Mintz 
471fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn,
472fe56b9e6SYuval Mintz 		   NETIF_MSG_HW,
473fe56b9e6SYuval Mintz 		   "Posting DMAE command [idx %d]: opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
474fe56b9e6SYuval Mintz 		   idx_cmd,
4751a635e48SYuval Mintz 		   le32_to_cpu(p_command->opcode),
4761a635e48SYuval Mintz 		   le16_to_cpu(p_command->opcode_b),
4771a635e48SYuval Mintz 		   le16_to_cpu(p_command->length_dw),
4781a635e48SYuval Mintz 		   le32_to_cpu(p_command->src_addr_hi),
4791a635e48SYuval Mintz 		   le32_to_cpu(p_command->src_addr_lo),
4801a635e48SYuval Mintz 		   le32_to_cpu(p_command->dst_addr_hi),
4811a635e48SYuval Mintz 		   le32_to_cpu(p_command->dst_addr_lo));
482fe56b9e6SYuval Mintz 
483fe56b9e6SYuval Mintz 	/* Copy the command to DMAE - need to do it before every call
484fe56b9e6SYuval Mintz 	 * for source/dest address no reset.
485fe56b9e6SYuval Mintz 	 * The first 9 DWs are the command registers, the 10 DW is the
486fe56b9e6SYuval Mintz 	 * GO register, and the rest are result registers
487fe56b9e6SYuval Mintz 	 * (which are read only by the client).
488fe56b9e6SYuval Mintz 	 */
489fe56b9e6SYuval Mintz 	for (i = 0; i < DMAE_CMD_SIZE; i++) {
490fe56b9e6SYuval Mintz 		u32 data = (i < DMAE_CMD_SIZE_TO_FILL) ?
4911a635e48SYuval Mintz 			   *(((u32 *)p_command) + i) : 0;
492fe56b9e6SYuval Mintz 
493fe56b9e6SYuval Mintz 		qed_wr(p_hwfn, p_ptt,
494fe56b9e6SYuval Mintz 		       DMAE_REG_CMD_MEM +
495fe56b9e6SYuval Mintz 		       (idx_cmd * DMAE_CMD_SIZE * sizeof(u32)) +
496fe56b9e6SYuval Mintz 		       (i * sizeof(u32)), data);
497fe56b9e6SYuval Mintz 	}
498fe56b9e6SYuval Mintz 
4991a635e48SYuval Mintz 	qed_wr(p_hwfn, p_ptt, qed_dmae_idx_to_go_cmd(idx_cmd), DMAE_GO_VALUE);
500fe56b9e6SYuval Mintz 
501fe56b9e6SYuval Mintz 	return qed_status;
502fe56b9e6SYuval Mintz }
503fe56b9e6SYuval Mintz 
504fe56b9e6SYuval Mintz int qed_dmae_info_alloc(struct qed_hwfn *p_hwfn)
505fe56b9e6SYuval Mintz {
506fe56b9e6SYuval Mintz 	dma_addr_t *p_addr = &p_hwfn->dmae_info.completion_word_phys_addr;
507fe56b9e6SYuval Mintz 	struct dmae_cmd **p_cmd = &p_hwfn->dmae_info.p_dmae_cmd;
508fe56b9e6SYuval Mintz 	u32 **p_buff = &p_hwfn->dmae_info.p_intermediate_buffer;
509fe56b9e6SYuval Mintz 	u32 **p_comp = &p_hwfn->dmae_info.p_completion_word;
510fe56b9e6SYuval Mintz 
511fe56b9e6SYuval Mintz 	*p_comp = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
5121a635e48SYuval Mintz 				     sizeof(u32), p_addr, GFP_KERNEL);
5132591c280SJoe Perches 	if (!*p_comp)
514fe56b9e6SYuval Mintz 		goto err;
515fe56b9e6SYuval Mintz 
516fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.dmae_cmd_phys_addr;
517fe56b9e6SYuval Mintz 	*p_cmd = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
518fe56b9e6SYuval Mintz 				    sizeof(struct dmae_cmd),
519fe56b9e6SYuval Mintz 				    p_addr, GFP_KERNEL);
5202591c280SJoe Perches 	if (!*p_cmd)
521fe56b9e6SYuval Mintz 		goto err;
522fe56b9e6SYuval Mintz 
523fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.intermediate_buffer_phys_addr;
524fe56b9e6SYuval Mintz 	*p_buff = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
525fe56b9e6SYuval Mintz 				     sizeof(u32) * DMAE_MAX_RW_SIZE,
526fe56b9e6SYuval Mintz 				     p_addr, GFP_KERNEL);
5272591c280SJoe Perches 	if (!*p_buff)
528fe56b9e6SYuval Mintz 		goto err;
529fe56b9e6SYuval Mintz 
530fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.channel = p_hwfn->rel_pf_id;
531fe56b9e6SYuval Mintz 
532fe56b9e6SYuval Mintz 	return 0;
533fe56b9e6SYuval Mintz err:
534fe56b9e6SYuval Mintz 	qed_dmae_info_free(p_hwfn);
535fe56b9e6SYuval Mintz 	return -ENOMEM;
536fe56b9e6SYuval Mintz }
537fe56b9e6SYuval Mintz 
538fe56b9e6SYuval Mintz void qed_dmae_info_free(struct qed_hwfn *p_hwfn)
539fe56b9e6SYuval Mintz {
540fe56b9e6SYuval Mintz 	dma_addr_t p_phys;
541fe56b9e6SYuval Mintz 
542fe56b9e6SYuval Mintz 	/* Just make sure no one is in the middle */
543fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
544fe56b9e6SYuval Mintz 
545fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_completion_word) {
546fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.completion_word_phys_addr;
547fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
548fe56b9e6SYuval Mintz 				  sizeof(u32),
5491a635e48SYuval Mintz 				  p_hwfn->dmae_info.p_completion_word, p_phys);
550fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_completion_word = NULL;
551fe56b9e6SYuval Mintz 	}
552fe56b9e6SYuval Mintz 
553fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_dmae_cmd) {
554fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.dmae_cmd_phys_addr;
555fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
556fe56b9e6SYuval Mintz 				  sizeof(struct dmae_cmd),
5571a635e48SYuval Mintz 				  p_hwfn->dmae_info.p_dmae_cmd, p_phys);
558fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_dmae_cmd = NULL;
559fe56b9e6SYuval Mintz 	}
560fe56b9e6SYuval Mintz 
561fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_intermediate_buffer) {
562fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
563fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
564fe56b9e6SYuval Mintz 				  sizeof(u32) * DMAE_MAX_RW_SIZE,
565fe56b9e6SYuval Mintz 				  p_hwfn->dmae_info.p_intermediate_buffer,
566fe56b9e6SYuval Mintz 				  p_phys);
567fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_intermediate_buffer = NULL;
568fe56b9e6SYuval Mintz 	}
569fe56b9e6SYuval Mintz 
570fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
571fe56b9e6SYuval Mintz }
572fe56b9e6SYuval Mintz 
573fe56b9e6SYuval Mintz static int qed_dmae_operation_wait(struct qed_hwfn *p_hwfn)
574fe56b9e6SYuval Mintz {
5751a635e48SYuval Mintz 	u32 wait_cnt_limit = 10000, wait_cnt = 0;
576fe56b9e6SYuval Mintz 	int qed_status = 0;
577fe56b9e6SYuval Mintz 
578fe56b9e6SYuval Mintz 	barrier();
579fe56b9e6SYuval Mintz 	while (*p_hwfn->dmae_info.p_completion_word != DMAE_COMPLETION_VAL) {
580fe56b9e6SYuval Mintz 		udelay(DMAE_MIN_WAIT_TIME);
581fe56b9e6SYuval Mintz 		if (++wait_cnt > wait_cnt_limit) {
582fe56b9e6SYuval Mintz 			DP_NOTICE(p_hwfn->cdev,
583fe56b9e6SYuval Mintz 				  "Timed-out waiting for operation to complete. Completion word is 0x%08x expected 0x%08x.\n",
584fe56b9e6SYuval Mintz 				  *p_hwfn->dmae_info.p_completion_word,
585fe56b9e6SYuval Mintz 				 DMAE_COMPLETION_VAL);
586fe56b9e6SYuval Mintz 			qed_status = -EBUSY;
587fe56b9e6SYuval Mintz 			break;
588fe56b9e6SYuval Mintz 		}
589fe56b9e6SYuval Mintz 
590fe56b9e6SYuval Mintz 		/* to sync the completion_word since we are not
591fe56b9e6SYuval Mintz 		 * using the volatile keyword for p_completion_word
592fe56b9e6SYuval Mintz 		 */
593fe56b9e6SYuval Mintz 		barrier();
594fe56b9e6SYuval Mintz 	}
595fe56b9e6SYuval Mintz 
596fe56b9e6SYuval Mintz 	if (qed_status == 0)
597fe56b9e6SYuval Mintz 		*p_hwfn->dmae_info.p_completion_word = 0;
598fe56b9e6SYuval Mintz 
599fe56b9e6SYuval Mintz 	return qed_status;
600fe56b9e6SYuval Mintz }
601fe56b9e6SYuval Mintz 
602fe56b9e6SYuval Mintz static int qed_dmae_execute_sub_operation(struct qed_hwfn *p_hwfn,
603fe56b9e6SYuval Mintz 					  struct qed_ptt *p_ptt,
604fe56b9e6SYuval Mintz 					  u64 src_addr,
605fe56b9e6SYuval Mintz 					  u64 dst_addr,
606fe56b9e6SYuval Mintz 					  u8 src_type,
607fe56b9e6SYuval Mintz 					  u8 dst_type,
6081a635e48SYuval Mintz 					  u32 length_dw)
609fe56b9e6SYuval Mintz {
610fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
611fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
612fe56b9e6SYuval Mintz 	int qed_status = 0;
613fe56b9e6SYuval Mintz 
614fe56b9e6SYuval Mintz 	switch (src_type) {
615fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
616fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
617fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(src_addr));
618fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(src_addr));
619fe56b9e6SYuval Mintz 		break;
620fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
621fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
622fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(phys));
623fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(phys));
624fe56b9e6SYuval Mintz 		memcpy(&p_hwfn->dmae_info.p_intermediate_buffer[0],
625fe56b9e6SYuval Mintz 		       (void *)(uintptr_t)src_addr,
6261a635e48SYuval Mintz 		       length_dw * sizeof(u32));
627fe56b9e6SYuval Mintz 		break;
628fe56b9e6SYuval Mintz 	default:
629fe56b9e6SYuval Mintz 		return -EINVAL;
630fe56b9e6SYuval Mintz 	}
631fe56b9e6SYuval Mintz 
632fe56b9e6SYuval Mintz 	switch (dst_type) {
633fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
634fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
635fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(dst_addr));
636fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(dst_addr));
637fe56b9e6SYuval Mintz 		break;
638fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
639fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
640fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(phys));
641fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(phys));
642fe56b9e6SYuval Mintz 		break;
643fe56b9e6SYuval Mintz 	default:
644fe56b9e6SYuval Mintz 		return -EINVAL;
645fe56b9e6SYuval Mintz 	}
646fe56b9e6SYuval Mintz 
6471a635e48SYuval Mintz 	cmd->length_dw = cpu_to_le16((u16)length_dw);
648fe56b9e6SYuval Mintz 
649fe56b9e6SYuval Mintz 	qed_dmae_post_command(p_hwfn, p_ptt);
650fe56b9e6SYuval Mintz 
651fe56b9e6SYuval Mintz 	qed_status = qed_dmae_operation_wait(p_hwfn);
652fe56b9e6SYuval Mintz 
653fe56b9e6SYuval Mintz 	if (qed_status) {
654fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
655fe56b9e6SYuval Mintz 			  "qed_dmae_host2grc: Wait Failed. source_addr 0x%llx, grc_addr 0x%llx, size_in_dwords 0x%x\n",
6561a635e48SYuval Mintz 			  src_addr, dst_addr, length_dw);
657fe56b9e6SYuval Mintz 		return qed_status;
658fe56b9e6SYuval Mintz 	}
659fe56b9e6SYuval Mintz 
660fe56b9e6SYuval Mintz 	if (dst_type == QED_DMAE_ADDRESS_HOST_VIRT)
661fe56b9e6SYuval Mintz 		memcpy((void *)(uintptr_t)(dst_addr),
662fe56b9e6SYuval Mintz 		       &p_hwfn->dmae_info.p_intermediate_buffer[0],
6631a635e48SYuval Mintz 		       length_dw * sizeof(u32));
664fe56b9e6SYuval Mintz 
665fe56b9e6SYuval Mintz 	return 0;
666fe56b9e6SYuval Mintz }
667fe56b9e6SYuval Mintz 
668fe56b9e6SYuval Mintz static int qed_dmae_execute_command(struct qed_hwfn *p_hwfn,
669fe56b9e6SYuval Mintz 				    struct qed_ptt *p_ptt,
670fe56b9e6SYuval Mintz 				    u64 src_addr, u64 dst_addr,
671fe56b9e6SYuval Mintz 				    u8 src_type, u8 dst_type,
672fe56b9e6SYuval Mintz 				    u32 size_in_dwords,
673fe56b9e6SYuval Mintz 				    struct qed_dmae_params *p_params)
674fe56b9e6SYuval Mintz {
675fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.completion_word_phys_addr;
676fe56b9e6SYuval Mintz 	u16 length_cur = 0, i = 0, cnt_split = 0, length_mod = 0;
677fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
678fe56b9e6SYuval Mintz 	u64 src_addr_split = 0, dst_addr_split = 0;
679fe56b9e6SYuval Mintz 	u16 length_limit = DMAE_MAX_RW_SIZE;
680fe56b9e6SYuval Mintz 	int qed_status = 0;
681fe56b9e6SYuval Mintz 	u32 offset = 0;
682fe56b9e6SYuval Mintz 
68364515dc8STomer Tayar 	if (p_hwfn->cdev->recov_in_prog) {
68464515dc8STomer Tayar 		DP_VERBOSE(p_hwfn,
68564515dc8STomer Tayar 			   NETIF_MSG_HW,
68664515dc8STomer Tayar 			   "Recovery is in progress. Avoid DMAE transaction [{src: addr 0x%llx, type %d}, {dst: addr 0x%llx, type %d}, size %d].\n",
68764515dc8STomer Tayar 			   src_addr, src_type, dst_addr, dst_type,
68864515dc8STomer Tayar 			   size_in_dwords);
68964515dc8STomer Tayar 
69064515dc8STomer Tayar 		/* Let the flow complete w/o any error handling */
69164515dc8STomer Tayar 		return 0;
69264515dc8STomer Tayar 	}
69364515dc8STomer Tayar 
694fe56b9e6SYuval Mintz 	qed_dmae_opcode(p_hwfn,
695fe56b9e6SYuval Mintz 			(src_type == QED_DMAE_ADDRESS_GRC),
696fe56b9e6SYuval Mintz 			(dst_type == QED_DMAE_ADDRESS_GRC),
697fe56b9e6SYuval Mintz 			p_params);
698fe56b9e6SYuval Mintz 
699fe56b9e6SYuval Mintz 	cmd->comp_addr_lo = cpu_to_le32(lower_32_bits(phys));
700fe56b9e6SYuval Mintz 	cmd->comp_addr_hi = cpu_to_le32(upper_32_bits(phys));
701fe56b9e6SYuval Mintz 	cmd->comp_val = cpu_to_le32(DMAE_COMPLETION_VAL);
702fe56b9e6SYuval Mintz 
703fe56b9e6SYuval Mintz 	/* Check if the grc_addr is valid like < MAX_GRC_OFFSET */
704fe56b9e6SYuval Mintz 	cnt_split = size_in_dwords / length_limit;
705fe56b9e6SYuval Mintz 	length_mod = size_in_dwords % length_limit;
706fe56b9e6SYuval Mintz 
707fe56b9e6SYuval Mintz 	src_addr_split = src_addr;
708fe56b9e6SYuval Mintz 	dst_addr_split = dst_addr;
709fe56b9e6SYuval Mintz 
710fe56b9e6SYuval Mintz 	for (i = 0; i <= cnt_split; i++) {
711fe56b9e6SYuval Mintz 		offset = length_limit * i;
712fe56b9e6SYuval Mintz 
71383bf76e3SMichal Kalderon 		if (!QED_DMAE_FLAGS_IS_SET(p_params, RW_REPL_SRC)) {
714fe56b9e6SYuval Mintz 			if (src_type == QED_DMAE_ADDRESS_GRC)
715fe56b9e6SYuval Mintz 				src_addr_split = src_addr + offset;
716fe56b9e6SYuval Mintz 			else
717fe56b9e6SYuval Mintz 				src_addr_split = src_addr + (offset * 4);
718fe56b9e6SYuval Mintz 		}
719fe56b9e6SYuval Mintz 
720fe56b9e6SYuval Mintz 		if (dst_type == QED_DMAE_ADDRESS_GRC)
721fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + offset;
722fe56b9e6SYuval Mintz 		else
723fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + (offset * 4);
724fe56b9e6SYuval Mintz 
725fe56b9e6SYuval Mintz 		length_cur = (cnt_split == i) ? length_mod : length_limit;
726fe56b9e6SYuval Mintz 
727fe56b9e6SYuval Mintz 		/* might be zero on last iteration */
728fe56b9e6SYuval Mintz 		if (!length_cur)
729fe56b9e6SYuval Mintz 			continue;
730fe56b9e6SYuval Mintz 
731fe56b9e6SYuval Mintz 		qed_status = qed_dmae_execute_sub_operation(p_hwfn,
732fe56b9e6SYuval Mintz 							    p_ptt,
733fe56b9e6SYuval Mintz 							    src_addr_split,
734fe56b9e6SYuval Mintz 							    dst_addr_split,
735fe56b9e6SYuval Mintz 							    src_type,
736fe56b9e6SYuval Mintz 							    dst_type,
737fe56b9e6SYuval Mintz 							    length_cur);
738fe56b9e6SYuval Mintz 		if (qed_status) {
7392ec276d5SIgor Russkikh 			qed_hw_err_notify(p_hwfn, p_ptt, QED_HW_ERR_DMAE_FAIL,
740fe56b9e6SYuval Mintz 					  "qed_dmae_execute_sub_operation Failed with error 0x%x. source_addr 0x%llx, destination addr 0x%llx, size_in_dwords 0x%x\n",
7412ec276d5SIgor Russkikh 					  qed_status, src_addr,
7422ec276d5SIgor Russkikh 					  dst_addr, length_cur);
743fe56b9e6SYuval Mintz 			break;
744fe56b9e6SYuval Mintz 		}
745fe56b9e6SYuval Mintz 	}
746fe56b9e6SYuval Mintz 
747fe56b9e6SYuval Mintz 	return qed_status;
748fe56b9e6SYuval Mintz }
749fe56b9e6SYuval Mintz 
750fe56b9e6SYuval Mintz int qed_dmae_host2grc(struct qed_hwfn *p_hwfn,
751fe56b9e6SYuval Mintz 		      struct qed_ptt *p_ptt,
75283bf76e3SMichal Kalderon 		      u64 source_addr, u32 grc_addr, u32 size_in_dwords,
75383bf76e3SMichal Kalderon 		      struct qed_dmae_params *p_params)
754fe56b9e6SYuval Mintz {
755fe56b9e6SYuval Mintz 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
756fe56b9e6SYuval Mintz 	int rc;
757fe56b9e6SYuval Mintz 
758fe56b9e6SYuval Mintz 
759fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
760fe56b9e6SYuval Mintz 
761fe56b9e6SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
762fe56b9e6SYuval Mintz 				      grc_addr_in_dw,
763fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_VIRT,
764fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_GRC,
76583bf76e3SMichal Kalderon 				      size_in_dwords, p_params);
766fe56b9e6SYuval Mintz 
767fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
768fe56b9e6SYuval Mintz 
769fe56b9e6SYuval Mintz 	return rc;
770fe56b9e6SYuval Mintz }
771fe56b9e6SYuval Mintz 
7721a635e48SYuval Mintz int qed_dmae_grc2host(struct qed_hwfn *p_hwfn,
7731a635e48SYuval Mintz 		      struct qed_ptt *p_ptt,
7741a635e48SYuval Mintz 		      u32 grc_addr,
77583bf76e3SMichal Kalderon 		      dma_addr_t dest_addr, u32 size_in_dwords,
77683bf76e3SMichal Kalderon 		      struct qed_dmae_params *p_params)
777722003acSSudarsana Reddy Kalluru {
778722003acSSudarsana Reddy Kalluru 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
779722003acSSudarsana Reddy Kalluru 	int rc;
780722003acSSudarsana Reddy Kalluru 
781722003acSSudarsana Reddy Kalluru 
782722003acSSudarsana Reddy Kalluru 	mutex_lock(&p_hwfn->dmae_info.mutex);
783722003acSSudarsana Reddy Kalluru 
784722003acSSudarsana Reddy Kalluru 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, grc_addr_in_dw,
785722003acSSudarsana Reddy Kalluru 				      dest_addr, QED_DMAE_ADDRESS_GRC,
786722003acSSudarsana Reddy Kalluru 				      QED_DMAE_ADDRESS_HOST_VIRT,
78783bf76e3SMichal Kalderon 				      size_in_dwords, p_params);
788722003acSSudarsana Reddy Kalluru 
789722003acSSudarsana Reddy Kalluru 	mutex_unlock(&p_hwfn->dmae_info.mutex);
790722003acSSudarsana Reddy Kalluru 
791722003acSSudarsana Reddy Kalluru 	return rc;
792722003acSSudarsana Reddy Kalluru }
793722003acSSudarsana Reddy Kalluru 
7941a635e48SYuval Mintz int qed_dmae_host2host(struct qed_hwfn *p_hwfn,
79537bff2b9SYuval Mintz 		       struct qed_ptt *p_ptt,
79637bff2b9SYuval Mintz 		       dma_addr_t source_addr,
79737bff2b9SYuval Mintz 		       dma_addr_t dest_addr,
79837bff2b9SYuval Mintz 		       u32 size_in_dwords, struct qed_dmae_params *p_params)
79937bff2b9SYuval Mintz {
80037bff2b9SYuval Mintz 	int rc;
80137bff2b9SYuval Mintz 
80237bff2b9SYuval Mintz 	mutex_lock(&(p_hwfn->dmae_info.mutex));
80337bff2b9SYuval Mintz 
80437bff2b9SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
80537bff2b9SYuval Mintz 				      dest_addr,
80637bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
80737bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
80837bff2b9SYuval Mintz 				      size_in_dwords, p_params);
80937bff2b9SYuval Mintz 
81037bff2b9SYuval Mintz 	mutex_unlock(&(p_hwfn->dmae_info.mutex));
81137bff2b9SYuval Mintz 
81237bff2b9SYuval Mintz 	return rc;
81337bff2b9SYuval Mintz }
81437bff2b9SYuval Mintz 
815d639836aSIgor Russkikh void qed_hw_err_notify(struct qed_hwfn *p_hwfn,
816d639836aSIgor Russkikh 		       struct qed_ptt *p_ptt,
817d639836aSIgor Russkikh 		       enum qed_hw_err_type err_type, char *fmt, ...)
818d639836aSIgor Russkikh {
819d639836aSIgor Russkikh 	char buf[QED_HW_ERR_MAX_STR_SIZE];
820d639836aSIgor Russkikh 	va_list vl;
821d639836aSIgor Russkikh 	int len;
822d639836aSIgor Russkikh 
823d639836aSIgor Russkikh 	if (fmt) {
824d639836aSIgor Russkikh 		va_start(vl, fmt);
825d639836aSIgor Russkikh 		len = vsnprintf(buf, QED_HW_ERR_MAX_STR_SIZE, fmt, vl);
826d639836aSIgor Russkikh 		va_end(vl);
827d639836aSIgor Russkikh 
828d639836aSIgor Russkikh 		if (len > QED_HW_ERR_MAX_STR_SIZE - 1)
829d639836aSIgor Russkikh 			len = QED_HW_ERR_MAX_STR_SIZE - 1;
830d639836aSIgor Russkikh 
831d639836aSIgor Russkikh 		DP_NOTICE(p_hwfn, "%s", buf);
832d639836aSIgor Russkikh 	}
833d639836aSIgor Russkikh 
834d639836aSIgor Russkikh 	/* Fan failure cannot be masked by handling of another HW error */
835d639836aSIgor Russkikh 	if (p_hwfn->cdev->recov_in_prog &&
836d639836aSIgor Russkikh 	    err_type != QED_HW_ERR_FAN_FAIL) {
837d639836aSIgor Russkikh 		DP_VERBOSE(p_hwfn,
838d639836aSIgor Russkikh 			   NETIF_MSG_DRV,
839d639836aSIgor Russkikh 			   "Recovery is in progress. Avoid notifying about HW error %d.\n",
840d639836aSIgor Russkikh 			   err_type);
841d639836aSIgor Russkikh 		return;
842d639836aSIgor Russkikh 	}
843d639836aSIgor Russkikh 
844d639836aSIgor Russkikh 	qed_hw_error_occurred(p_hwfn, err_type);
845d8d6c5a7SIgor Russkikh 
846d8d6c5a7SIgor Russkikh 	if (fmt)
847d8d6c5a7SIgor Russkikh 		qed_mcp_send_raw_debug_data(p_hwfn, p_ptt, buf, len);
848d639836aSIgor Russkikh }
849d639836aSIgor Russkikh 
850da090917STomer Tayar int qed_dmae_sanity(struct qed_hwfn *p_hwfn,
851da090917STomer Tayar 		    struct qed_ptt *p_ptt, const char *phase)
852da090917STomer Tayar {
853da090917STomer Tayar 	u32 size = PAGE_SIZE / 2, val;
854da090917STomer Tayar 	int rc = 0;
855da090917STomer Tayar 	dma_addr_t p_phys;
856da090917STomer Tayar 	void *p_virt;
857da090917STomer Tayar 	u32 *p_tmp;
858da090917STomer Tayar 
859da090917STomer Tayar 	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
860da090917STomer Tayar 				    2 * size, &p_phys, GFP_KERNEL);
861da090917STomer Tayar 	if (!p_virt) {
862da090917STomer Tayar 		DP_NOTICE(p_hwfn,
863da090917STomer Tayar 			  "DMAE sanity [%s]: failed to allocate memory\n",
864da090917STomer Tayar 			  phase);
865da090917STomer Tayar 		return -ENOMEM;
866da090917STomer Tayar 	}
867da090917STomer Tayar 
868da090917STomer Tayar 	/* Fill the bottom half of the allocated memory with a known pattern */
869da090917STomer Tayar 	for (p_tmp = (u32 *)p_virt;
870da090917STomer Tayar 	     p_tmp < (u32 *)((u8 *)p_virt + size); p_tmp++) {
871da090917STomer Tayar 		/* Save the address itself as the value */
872da090917STomer Tayar 		val = (u32)(uintptr_t)p_tmp;
873da090917STomer Tayar 		*p_tmp = val;
874da090917STomer Tayar 	}
875da090917STomer Tayar 
876da090917STomer Tayar 	/* Zero the top half of the allocated memory */
877da090917STomer Tayar 	memset((u8 *)p_virt + size, 0, size);
878da090917STomer Tayar 
879da090917STomer Tayar 	DP_VERBOSE(p_hwfn,
880da090917STomer Tayar 		   QED_MSG_SP,
881da090917STomer Tayar 		   "DMAE sanity [%s]: src_addr={phys 0x%llx, virt %p}, dst_addr={phys 0x%llx, virt %p}, size 0x%x\n",
882da090917STomer Tayar 		   phase,
883da090917STomer Tayar 		   (u64)p_phys,
884da090917STomer Tayar 		   p_virt, (u64)(p_phys + size), (u8 *)p_virt + size, size);
885da090917STomer Tayar 
886da090917STomer Tayar 	rc = qed_dmae_host2host(p_hwfn, p_ptt, p_phys, p_phys + size,
88783bf76e3SMichal Kalderon 				size / 4, NULL);
888da090917STomer Tayar 	if (rc) {
889da090917STomer Tayar 		DP_NOTICE(p_hwfn,
890da090917STomer Tayar 			  "DMAE sanity [%s]: qed_dmae_host2host() failed. rc = %d.\n",
891da090917STomer Tayar 			  phase, rc);
892da090917STomer Tayar 		goto out;
893da090917STomer Tayar 	}
894da090917STomer Tayar 
895da090917STomer Tayar 	/* Verify that the top half of the allocated memory has the pattern */
896da090917STomer Tayar 	for (p_tmp = (u32 *)((u8 *)p_virt + size);
897da090917STomer Tayar 	     p_tmp < (u32 *)((u8 *)p_virt + (2 * size)); p_tmp++) {
898da090917STomer Tayar 		/* The corresponding address in the bottom half */
899da090917STomer Tayar 		val = (u32)(uintptr_t)p_tmp - size;
900da090917STomer Tayar 
901da090917STomer Tayar 		if (*p_tmp != val) {
902da090917STomer Tayar 			DP_NOTICE(p_hwfn,
903da090917STomer Tayar 				  "DMAE sanity [%s]: addr={phys 0x%llx, virt %p}, read_val 0x%08x, expected_val 0x%08x\n",
904da090917STomer Tayar 				  phase,
905da090917STomer Tayar 				  (u64)p_phys + ((u8 *)p_tmp - (u8 *)p_virt),
906da090917STomer Tayar 				  p_tmp, *p_tmp, val);
907da090917STomer Tayar 			rc = -EINVAL;
908da090917STomer Tayar 			goto out;
909da090917STomer Tayar 		}
910da090917STomer Tayar 	}
911da090917STomer Tayar 
912da090917STomer Tayar out:
913da090917STomer Tayar 	dma_free_coherent(&p_hwfn->cdev->pdev->dev, 2 * size, p_virt, p_phys);
914da090917STomer Tayar 	return rc;
915da090917STomer Tayar }
916