xref: /linux/drivers/net/ethernet/qlogic/qed/qed_hw.c (revision 722003ac40c2c397bd5bc2b714125bc82ab27043)
1fe56b9e6SYuval Mintz /* QLogic qed NIC Driver
2fe56b9e6SYuval Mintz  * Copyright (c) 2015 QLogic Corporation
3fe56b9e6SYuval Mintz  *
4fe56b9e6SYuval Mintz  * This software is available under the terms of the GNU General Public License
5fe56b9e6SYuval Mintz  * (GPL) Version 2, available from the file COPYING in the main directory of
6fe56b9e6SYuval Mintz  * this source tree.
7fe56b9e6SYuval Mintz  */
8fe56b9e6SYuval Mintz 
9fe56b9e6SYuval Mintz #include <linux/types.h>
10fe56b9e6SYuval Mintz #include <linux/io.h>
11fe56b9e6SYuval Mintz #include <linux/delay.h>
12fe56b9e6SYuval Mintz #include <linux/dma-mapping.h>
13fe56b9e6SYuval Mintz #include <linux/errno.h>
14fe56b9e6SYuval Mintz #include <linux/kernel.h>
15fe56b9e6SYuval Mintz #include <linux/list.h>
16fe56b9e6SYuval Mintz #include <linux/mutex.h>
17fe56b9e6SYuval Mintz #include <linux/pci.h>
18fe56b9e6SYuval Mintz #include <linux/slab.h>
19fe56b9e6SYuval Mintz #include <linux/spinlock.h>
20fe56b9e6SYuval Mintz #include <linux/string.h>
21fe56b9e6SYuval Mintz #include <linux/qed/qed_chain.h>
22fe56b9e6SYuval Mintz #include "qed.h"
23fe56b9e6SYuval Mintz #include "qed_hsi.h"
24fe56b9e6SYuval Mintz #include "qed_hw.h"
25fe56b9e6SYuval Mintz #include "qed_reg_addr.h"
261408cc1fSYuval Mintz #include "qed_sriov.h"
27fe56b9e6SYuval Mintz 
28fe56b9e6SYuval Mintz #define QED_BAR_ACQUIRE_TIMEOUT 1000
29fe56b9e6SYuval Mintz 
30fe56b9e6SYuval Mintz /* Invalid values */
31fe56b9e6SYuval Mintz #define QED_BAR_INVALID_OFFSET          (cpu_to_le32(-1))
32fe56b9e6SYuval Mintz 
33fe56b9e6SYuval Mintz struct qed_ptt {
34fe56b9e6SYuval Mintz 	struct list_head	list_entry;
35fe56b9e6SYuval Mintz 	unsigned int		idx;
36fe56b9e6SYuval Mintz 	struct pxp_ptt_entry	pxp;
37fe56b9e6SYuval Mintz };
38fe56b9e6SYuval Mintz 
39fe56b9e6SYuval Mintz struct qed_ptt_pool {
40fe56b9e6SYuval Mintz 	struct list_head	free_list;
41fe56b9e6SYuval Mintz 	spinlock_t		lock; /* ptt synchronized access */
42fe56b9e6SYuval Mintz 	struct qed_ptt		ptts[PXP_EXTERNAL_BAR_PF_WINDOW_NUM];
43fe56b9e6SYuval Mintz };
44fe56b9e6SYuval Mintz 
45fe56b9e6SYuval Mintz int qed_ptt_pool_alloc(struct qed_hwfn *p_hwfn)
46fe56b9e6SYuval Mintz {
47fe56b9e6SYuval Mintz 	struct qed_ptt_pool *p_pool = kmalloc(sizeof(*p_pool),
4860fffb3bSYuval Mintz 					      GFP_KERNEL);
49fe56b9e6SYuval Mintz 	int i;
50fe56b9e6SYuval Mintz 
51fe56b9e6SYuval Mintz 	if (!p_pool)
52fe56b9e6SYuval Mintz 		return -ENOMEM;
53fe56b9e6SYuval Mintz 
54fe56b9e6SYuval Mintz 	INIT_LIST_HEAD(&p_pool->free_list);
55fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
56fe56b9e6SYuval Mintz 		p_pool->ptts[i].idx = i;
57fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.offset = QED_BAR_INVALID_OFFSET;
58fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.pretend.control = 0;
59fe56b9e6SYuval Mintz 		if (i >= RESERVED_PTT_MAX)
60fe56b9e6SYuval Mintz 			list_add(&p_pool->ptts[i].list_entry,
61fe56b9e6SYuval Mintz 				 &p_pool->free_list);
62fe56b9e6SYuval Mintz 	}
63fe56b9e6SYuval Mintz 
64fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = p_pool;
65fe56b9e6SYuval Mintz 	spin_lock_init(&p_pool->lock);
66fe56b9e6SYuval Mintz 
67fe56b9e6SYuval Mintz 	return 0;
68fe56b9e6SYuval Mintz }
69fe56b9e6SYuval Mintz 
70fe56b9e6SYuval Mintz void qed_ptt_invalidate(struct qed_hwfn *p_hwfn)
71fe56b9e6SYuval Mintz {
72fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
73fe56b9e6SYuval Mintz 	int i;
74fe56b9e6SYuval Mintz 
75fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
76fe56b9e6SYuval Mintz 		p_ptt = &p_hwfn->p_ptt_pool->ptts[i];
77fe56b9e6SYuval Mintz 		p_ptt->pxp.offset = QED_BAR_INVALID_OFFSET;
78fe56b9e6SYuval Mintz 	}
79fe56b9e6SYuval Mintz }
80fe56b9e6SYuval Mintz 
81fe56b9e6SYuval Mintz void qed_ptt_pool_free(struct qed_hwfn *p_hwfn)
82fe56b9e6SYuval Mintz {
83fe56b9e6SYuval Mintz 	kfree(p_hwfn->p_ptt_pool);
84fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = NULL;
85fe56b9e6SYuval Mintz }
86fe56b9e6SYuval Mintz 
87fe56b9e6SYuval Mintz struct qed_ptt *qed_ptt_acquire(struct qed_hwfn *p_hwfn)
88fe56b9e6SYuval Mintz {
89fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
90fe56b9e6SYuval Mintz 	unsigned int i;
91fe56b9e6SYuval Mintz 
92fe56b9e6SYuval Mintz 	/* Take the free PTT from the list */
93fe56b9e6SYuval Mintz 	for (i = 0; i < QED_BAR_ACQUIRE_TIMEOUT; i++) {
94fe56b9e6SYuval Mintz 		spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
95fe56b9e6SYuval Mintz 
96fe56b9e6SYuval Mintz 		if (!list_empty(&p_hwfn->p_ptt_pool->free_list)) {
97fe56b9e6SYuval Mintz 			p_ptt = list_first_entry(&p_hwfn->p_ptt_pool->free_list,
98fe56b9e6SYuval Mintz 						 struct qed_ptt, list_entry);
99fe56b9e6SYuval Mintz 			list_del(&p_ptt->list_entry);
100fe56b9e6SYuval Mintz 
101fe56b9e6SYuval Mintz 			spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
102fe56b9e6SYuval Mintz 
103fe56b9e6SYuval Mintz 			DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
104fe56b9e6SYuval Mintz 				   "allocated ptt %d\n", p_ptt->idx);
105fe56b9e6SYuval Mintz 			return p_ptt;
106fe56b9e6SYuval Mintz 		}
107fe56b9e6SYuval Mintz 
108fe56b9e6SYuval Mintz 		spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
109fe56b9e6SYuval Mintz 		usleep_range(1000, 2000);
110fe56b9e6SYuval Mintz 	}
111fe56b9e6SYuval Mintz 
112fe56b9e6SYuval Mintz 	DP_NOTICE(p_hwfn, "PTT acquire timeout - failed to allocate PTT\n");
113fe56b9e6SYuval Mintz 	return NULL;
114fe56b9e6SYuval Mintz }
115fe56b9e6SYuval Mintz 
116fe56b9e6SYuval Mintz void qed_ptt_release(struct qed_hwfn *p_hwfn,
117fe56b9e6SYuval Mintz 		     struct qed_ptt *p_ptt)
118fe56b9e6SYuval Mintz {
119fe56b9e6SYuval Mintz 	spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
120fe56b9e6SYuval Mintz 	list_add(&p_ptt->list_entry, &p_hwfn->p_ptt_pool->free_list);
121fe56b9e6SYuval Mintz 	spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
122fe56b9e6SYuval Mintz }
123fe56b9e6SYuval Mintz 
124fe56b9e6SYuval Mintz u32 qed_ptt_get_hw_addr(struct qed_hwfn *p_hwfn,
125fe56b9e6SYuval Mintz 			struct qed_ptt *p_ptt)
126fe56b9e6SYuval Mintz {
127fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and we need to translate it to Bytes */
128fe56b9e6SYuval Mintz 	return le32_to_cpu(p_ptt->pxp.offset) << 2;
129fe56b9e6SYuval Mintz }
130fe56b9e6SYuval Mintz 
131fe56b9e6SYuval Mintz static u32 qed_ptt_config_addr(struct qed_ptt *p_ptt)
132fe56b9e6SYuval Mintz {
133fe56b9e6SYuval Mintz 	return PXP_PF_WINDOW_ADMIN_PER_PF_START +
134fe56b9e6SYuval Mintz 	       p_ptt->idx * sizeof(struct pxp_ptt_entry);
135fe56b9e6SYuval Mintz }
136fe56b9e6SYuval Mintz 
137fe56b9e6SYuval Mintz u32 qed_ptt_get_bar_addr(struct qed_ptt *p_ptt)
138fe56b9e6SYuval Mintz {
139fe56b9e6SYuval Mintz 	return PXP_EXTERNAL_BAR_PF_WINDOW_START +
140fe56b9e6SYuval Mintz 	       p_ptt->idx * PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE;
141fe56b9e6SYuval Mintz }
142fe56b9e6SYuval Mintz 
143fe56b9e6SYuval Mintz void qed_ptt_set_win(struct qed_hwfn *p_hwfn,
144fe56b9e6SYuval Mintz 		     struct qed_ptt *p_ptt,
145fe56b9e6SYuval Mintz 		     u32 new_hw_addr)
146fe56b9e6SYuval Mintz {
147fe56b9e6SYuval Mintz 	u32 prev_hw_addr;
148fe56b9e6SYuval Mintz 
149fe56b9e6SYuval Mintz 	prev_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
150fe56b9e6SYuval Mintz 
151fe56b9e6SYuval Mintz 	if (new_hw_addr == prev_hw_addr)
152fe56b9e6SYuval Mintz 		return;
153fe56b9e6SYuval Mintz 
154fe56b9e6SYuval Mintz 	/* Update PTT entery in admin window */
155fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
156fe56b9e6SYuval Mintz 		   "Updating PTT entry %d to offset 0x%x\n",
157fe56b9e6SYuval Mintz 		   p_ptt->idx, new_hw_addr);
158fe56b9e6SYuval Mintz 
159fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and the address is in Bytes */
160fe56b9e6SYuval Mintz 	p_ptt->pxp.offset = cpu_to_le32(new_hw_addr >> 2);
161fe56b9e6SYuval Mintz 
162fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
163fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
164fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, offset),
165fe56b9e6SYuval Mintz 	       le32_to_cpu(p_ptt->pxp.offset));
166fe56b9e6SYuval Mintz }
167fe56b9e6SYuval Mintz 
168fe56b9e6SYuval Mintz static u32 qed_set_ptt(struct qed_hwfn *p_hwfn,
169fe56b9e6SYuval Mintz 		       struct qed_ptt *p_ptt,
170fe56b9e6SYuval Mintz 		       u32 hw_addr)
171fe56b9e6SYuval Mintz {
172fe56b9e6SYuval Mintz 	u32 win_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
173fe56b9e6SYuval Mintz 	u32 offset;
174fe56b9e6SYuval Mintz 
175fe56b9e6SYuval Mintz 	offset = hw_addr - win_hw_addr;
176fe56b9e6SYuval Mintz 
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,
227fe56b9e6SYuval Mintz 			  void *addr,
228fe56b9e6SYuval Mintz 			  u32 hw_addr,
229fe56b9e6SYuval Mintz 			  size_t n,
230fe56b9e6SYuval Mintz 			  bool to_device)
231fe56b9e6SYuval Mintz {
232fe56b9e6SYuval Mintz 	u32 dw_count, *host_addr, hw_offset;
233fe56b9e6SYuval Mintz 	size_t quota, done = 0;
234fe56b9e6SYuval Mintz 	u32 __iomem *reg_addr;
235fe56b9e6SYuval Mintz 
236fe56b9e6SYuval Mintz 	while (done < n) {
237fe56b9e6SYuval Mintz 		quota = min_t(size_t, n - done,
238fe56b9e6SYuval Mintz 			      PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE);
239fe56b9e6SYuval Mintz 
2401408cc1fSYuval Mintz 		if (IS_PF(p_hwfn->cdev)) {
241fe56b9e6SYuval Mintz 			qed_ptt_set_win(p_hwfn, p_ptt, hw_addr + done);
242fe56b9e6SYuval Mintz 			hw_offset = qed_ptt_get_bar_addr(p_ptt);
2431408cc1fSYuval Mintz 		} else {
2441408cc1fSYuval Mintz 			hw_offset = hw_addr + done;
2451408cc1fSYuval Mintz 		}
246fe56b9e6SYuval Mintz 
247fe56b9e6SYuval Mintz 		dw_count = quota / 4;
248fe56b9e6SYuval Mintz 		host_addr = (u32 *)((u8 *)addr + done);
249fe56b9e6SYuval Mintz 		reg_addr = (u32 __iomem *)REG_ADDR(p_hwfn, hw_offset);
250fe56b9e6SYuval Mintz 		if (to_device)
251fe56b9e6SYuval Mintz 			while (dw_count--)
252fe56b9e6SYuval Mintz 				DIRECT_REG_WR(reg_addr++, *host_addr++);
253fe56b9e6SYuval Mintz 		else
254fe56b9e6SYuval Mintz 			while (dw_count--)
255fe56b9e6SYuval Mintz 				*host_addr++ = DIRECT_REG_RD(reg_addr++);
256fe56b9e6SYuval Mintz 
257fe56b9e6SYuval Mintz 		done += quota;
258fe56b9e6SYuval Mintz 	}
259fe56b9e6SYuval Mintz }
260fe56b9e6SYuval Mintz 
261fe56b9e6SYuval Mintz void qed_memcpy_from(struct qed_hwfn *p_hwfn,
262fe56b9e6SYuval Mintz 		     struct qed_ptt *p_ptt,
263fe56b9e6SYuval Mintz 		     void *dest, u32 hw_addr, size_t n)
264fe56b9e6SYuval Mintz {
265fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
266fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, dest %p hw_addr 0x%x, size %lu\n",
267fe56b9e6SYuval Mintz 		   hw_addr, dest, hw_addr, (unsigned long)n);
268fe56b9e6SYuval Mintz 
269fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, dest, hw_addr, n, false);
270fe56b9e6SYuval Mintz }
271fe56b9e6SYuval Mintz 
272fe56b9e6SYuval Mintz void qed_memcpy_to(struct qed_hwfn *p_hwfn,
273fe56b9e6SYuval Mintz 		   struct qed_ptt *p_ptt,
274fe56b9e6SYuval Mintz 		   u32 hw_addr, void *src, size_t n)
275fe56b9e6SYuval Mintz {
276fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
277fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, hw_addr 0x%x, src %p size %lu\n",
278fe56b9e6SYuval Mintz 		   hw_addr, hw_addr, src, (unsigned long)n);
279fe56b9e6SYuval Mintz 
280fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, src, hw_addr, n, true);
281fe56b9e6SYuval Mintz }
282fe56b9e6SYuval Mintz 
283fe56b9e6SYuval Mintz void qed_fid_pretend(struct qed_hwfn *p_hwfn,
284fe56b9e6SYuval Mintz 		     struct qed_ptt *p_ptt,
285fe56b9e6SYuval Mintz 		     u16 fid)
286fe56b9e6SYuval Mintz {
287fe56b9e6SYuval Mintz 	u16 control = 0;
288fe56b9e6SYuval Mintz 
289fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
290fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
291fe56b9e6SYuval Mintz 
292fe56b9e6SYuval Mintz 	/* Every pretend undos previous pretends, including
293fe56b9e6SYuval Mintz 	 * previous port pretend.
294fe56b9e6SYuval Mintz 	 */
295fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
296fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
297fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
298fe56b9e6SYuval Mintz 
299fe56b9e6SYuval Mintz 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
300fe56b9e6SYuval Mintz 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
301fe56b9e6SYuval Mintz 
302fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
303fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
304fe56b9e6SYuval Mintz 
305fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
306fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
307fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
308fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
309fe56b9e6SYuval Mintz }
310fe56b9e6SYuval Mintz 
311fe56b9e6SYuval Mintz void qed_port_pretend(struct qed_hwfn *p_hwfn,
312fe56b9e6SYuval Mintz 		      struct qed_ptt *p_ptt,
313fe56b9e6SYuval Mintz 		      u8 port_id)
314fe56b9e6SYuval Mintz {
315fe56b9e6SYuval Mintz 	u16 control = 0;
316fe56b9e6SYuval Mintz 
317fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
318fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
319fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
320fe56b9e6SYuval Mintz 
321fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
322fe56b9e6SYuval Mintz 
323fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
324fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
325fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
326fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
327fe56b9e6SYuval Mintz }
328fe56b9e6SYuval Mintz 
329fe56b9e6SYuval Mintz void qed_port_unpretend(struct qed_hwfn *p_hwfn,
330fe56b9e6SYuval Mintz 			struct qed_ptt *p_ptt)
331fe56b9e6SYuval Mintz {
332fe56b9e6SYuval Mintz 	u16 control = 0;
333fe56b9e6SYuval Mintz 
334fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
335fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
336fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
337fe56b9e6SYuval Mintz 
338fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
339fe56b9e6SYuval Mintz 
340fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
341fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
342fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
343fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
344fe56b9e6SYuval Mintz }
345fe56b9e6SYuval Mintz 
34632a47e72SYuval Mintz u32 qed_vfid_to_concrete(struct qed_hwfn *p_hwfn, u8 vfid)
34732a47e72SYuval Mintz {
34832a47e72SYuval Mintz 	u32 concrete_fid = 0;
34932a47e72SYuval Mintz 
35032a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_PFID, p_hwfn->rel_pf_id);
35132a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFID, vfid);
35232a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFVALID, 1);
35332a47e72SYuval Mintz 
35432a47e72SYuval Mintz 	return concrete_fid;
35532a47e72SYuval Mintz }
35632a47e72SYuval Mintz 
357fe56b9e6SYuval Mintz /* DMAE */
358fe56b9e6SYuval Mintz static void qed_dmae_opcode(struct qed_hwfn *p_hwfn,
359fe56b9e6SYuval Mintz 			    const u8 is_src_type_grc,
360fe56b9e6SYuval Mintz 			    const u8 is_dst_type_grc,
361fe56b9e6SYuval Mintz 			    struct qed_dmae_params *p_params)
362fe56b9e6SYuval Mintz {
36337bff2b9SYuval Mintz 	u16 opcode_b = 0;
364fe56b9e6SYuval Mintz 	u32 opcode = 0;
365fe56b9e6SYuval Mintz 
366fe56b9e6SYuval Mintz 	/* Whether the source is the PCIe or the GRC.
367fe56b9e6SYuval Mintz 	 * 0- The source is the PCIe
368fe56b9e6SYuval Mintz 	 * 1- The source is the GRC.
369fe56b9e6SYuval Mintz 	 */
370fe56b9e6SYuval Mintz 	opcode |= (is_src_type_grc ? DMAE_CMD_SRC_MASK_GRC
371fe56b9e6SYuval Mintz 				   : DMAE_CMD_SRC_MASK_PCIE) <<
372fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_SHIFT;
373fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_SRC_PF_ID_MASK) <<
374fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_PF_ID_SHIFT);
375fe56b9e6SYuval Mintz 
376fe56b9e6SYuval Mintz 	/* The destination of the DMA can be: 0-None 1-PCIe 2-GRC 3-None */
377fe56b9e6SYuval Mintz 	opcode |= (is_dst_type_grc ? DMAE_CMD_DST_MASK_GRC
378fe56b9e6SYuval Mintz 				   : DMAE_CMD_DST_MASK_PCIE) <<
379fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_SHIFT;
380fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_DST_PF_ID_MASK) <<
381fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_PF_ID_SHIFT);
382fe56b9e6SYuval Mintz 
383fe56b9e6SYuval Mintz 	/* Whether to write a completion word to the completion destination:
384fe56b9e6SYuval Mintz 	 * 0-Do not write a completion word
385fe56b9e6SYuval Mintz 	 * 1-Write the completion word
386fe56b9e6SYuval Mintz 	 */
387fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_COMP_WORD_EN_MASK << DMAE_CMD_COMP_WORD_EN_SHIFT);
388fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
389fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_ADDR_RESET_SHIFT);
390fe56b9e6SYuval Mintz 
391fe56b9e6SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_COMPLETION_DST)
392fe56b9e6SYuval Mintz 		opcode |= (1 << DMAE_CMD_COMP_FUNC_SHIFT);
393fe56b9e6SYuval Mintz 
394fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_ENDIANITY << DMAE_CMD_ENDIANITY_MODE_SHIFT);
395fe56b9e6SYuval Mintz 
396fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->port_id) << DMAE_CMD_PORT_ID_SHIFT);
397fe56b9e6SYuval Mintz 
398fe56b9e6SYuval Mintz 	/* reset source address in next go */
399fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
400fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_ADDR_RESET_SHIFT);
401fe56b9e6SYuval Mintz 
402fe56b9e6SYuval Mintz 	/* reset dest address in next go */
403fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_DST_ADDR_RESET_MASK <<
404fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_ADDR_RESET_SHIFT);
405fe56b9e6SYuval Mintz 
40637bff2b9SYuval Mintz 	/* SRC/DST VFID: all 1's - pf, otherwise VF id */
40737bff2b9SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_VF_SRC) {
40837bff2b9SYuval Mintz 		opcode |= 1 << DMAE_CMD_SRC_VF_ID_VALID_SHIFT;
40937bff2b9SYuval Mintz 		opcode_b |= p_params->src_vfid << DMAE_CMD_SRC_VF_ID_SHIFT;
41037bff2b9SYuval Mintz 	} else {
41137bff2b9SYuval Mintz 		opcode_b |= DMAE_CMD_SRC_VF_ID_MASK <<
41237bff2b9SYuval Mintz 			    DMAE_CMD_SRC_VF_ID_SHIFT;
41337bff2b9SYuval Mintz 	}
414fe56b9e6SYuval Mintz 
41537bff2b9SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_VF_DST) {
41637bff2b9SYuval Mintz 		opcode |= 1 << DMAE_CMD_DST_VF_ID_VALID_SHIFT;
41737bff2b9SYuval Mintz 		opcode_b |= p_params->dst_vfid << DMAE_CMD_DST_VF_ID_SHIFT;
41837bff2b9SYuval Mintz 	} else {
41937bff2b9SYuval Mintz 		opcode_b |= DMAE_CMD_DST_VF_ID_MASK << DMAE_CMD_DST_VF_ID_SHIFT;
42037bff2b9SYuval Mintz 	}
421fe56b9e6SYuval Mintz 
422fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode = cpu_to_le32(opcode);
42337bff2b9SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode_b = cpu_to_le16(opcode_b);
424fe56b9e6SYuval Mintz }
425fe56b9e6SYuval Mintz 
426fe56b9e6SYuval Mintz u32 qed_dmae_idx_to_go_cmd(u8 idx)
427fe56b9e6SYuval Mintz {
428fe56b9e6SYuval Mintz 	/* All the DMAE 'go' registers form an array in internal memory */
429fe56b9e6SYuval Mintz 	return DMAE_REG_GO_C0 + (idx << 2);
430fe56b9e6SYuval Mintz }
431fe56b9e6SYuval Mintz 
432fe56b9e6SYuval Mintz static int
433fe56b9e6SYuval Mintz qed_dmae_post_command(struct qed_hwfn *p_hwfn,
434fe56b9e6SYuval Mintz 		      struct qed_ptt *p_ptt)
435fe56b9e6SYuval Mintz {
436fe56b9e6SYuval Mintz 	struct dmae_cmd *command = p_hwfn->dmae_info.p_dmae_cmd;
437fe56b9e6SYuval Mintz 	u8 idx_cmd = p_hwfn->dmae_info.channel, i;
438fe56b9e6SYuval Mintz 	int qed_status = 0;
439fe56b9e6SYuval Mintz 
440fe56b9e6SYuval Mintz 	/* verify address is not NULL */
441fe56b9e6SYuval Mintz 	if ((((command->dst_addr_lo == 0) && (command->dst_addr_hi == 0)) ||
442fe56b9e6SYuval Mintz 	     ((command->src_addr_lo == 0) && (command->src_addr_hi == 0)))) {
443fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
444fe56b9e6SYuval Mintz 			  "source or destination address 0 idx_cmd=%d\n"
445fe56b9e6SYuval Mintz 			  "opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
446fe56b9e6SYuval Mintz 			   idx_cmd,
447fe56b9e6SYuval Mintz 			   le32_to_cpu(command->opcode),
448fe56b9e6SYuval Mintz 			   le16_to_cpu(command->opcode_b),
449351a4dedSYuval Mintz 			   le16_to_cpu(command->length_dw),
450fe56b9e6SYuval Mintz 			   le32_to_cpu(command->src_addr_hi),
451fe56b9e6SYuval Mintz 			   le32_to_cpu(command->src_addr_lo),
452fe56b9e6SYuval Mintz 			   le32_to_cpu(command->dst_addr_hi),
453fe56b9e6SYuval Mintz 			   le32_to_cpu(command->dst_addr_lo));
454fe56b9e6SYuval Mintz 
455fe56b9e6SYuval Mintz 		return -EINVAL;
456fe56b9e6SYuval Mintz 	}
457fe56b9e6SYuval Mintz 
458fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn,
459fe56b9e6SYuval Mintz 		   NETIF_MSG_HW,
460fe56b9e6SYuval Mintz 		   "Posting DMAE command [idx %d]: opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
461fe56b9e6SYuval Mintz 		   idx_cmd,
462fe56b9e6SYuval Mintz 		   le32_to_cpu(command->opcode),
463fe56b9e6SYuval Mintz 		   le16_to_cpu(command->opcode_b),
464351a4dedSYuval Mintz 		   le16_to_cpu(command->length_dw),
465fe56b9e6SYuval Mintz 		   le32_to_cpu(command->src_addr_hi),
466fe56b9e6SYuval Mintz 		   le32_to_cpu(command->src_addr_lo),
467fe56b9e6SYuval Mintz 		   le32_to_cpu(command->dst_addr_hi),
468fe56b9e6SYuval Mintz 		   le32_to_cpu(command->dst_addr_lo));
469fe56b9e6SYuval Mintz 
470fe56b9e6SYuval Mintz 	/* Copy the command to DMAE - need to do it before every call
471fe56b9e6SYuval Mintz 	 * for source/dest address no reset.
472fe56b9e6SYuval Mintz 	 * The first 9 DWs are the command registers, the 10 DW is the
473fe56b9e6SYuval Mintz 	 * GO register, and the rest are result registers
474fe56b9e6SYuval Mintz 	 * (which are read only by the client).
475fe56b9e6SYuval Mintz 	 */
476fe56b9e6SYuval Mintz 	for (i = 0; i < DMAE_CMD_SIZE; i++) {
477fe56b9e6SYuval Mintz 		u32 data = (i < DMAE_CMD_SIZE_TO_FILL) ?
478fe56b9e6SYuval Mintz 			   *(((u32 *)command) + i) : 0;
479fe56b9e6SYuval Mintz 
480fe56b9e6SYuval Mintz 		qed_wr(p_hwfn, p_ptt,
481fe56b9e6SYuval Mintz 		       DMAE_REG_CMD_MEM +
482fe56b9e6SYuval Mintz 		       (idx_cmd * DMAE_CMD_SIZE * sizeof(u32)) +
483fe56b9e6SYuval Mintz 		       (i * sizeof(u32)), data);
484fe56b9e6SYuval Mintz 	}
485fe56b9e6SYuval Mintz 
486fe56b9e6SYuval Mintz 	qed_wr(p_hwfn, p_ptt,
487fe56b9e6SYuval Mintz 	       qed_dmae_idx_to_go_cmd(idx_cmd),
488fe56b9e6SYuval Mintz 	       DMAE_GO_VALUE);
489fe56b9e6SYuval Mintz 
490fe56b9e6SYuval Mintz 	return qed_status;
491fe56b9e6SYuval Mintz }
492fe56b9e6SYuval Mintz 
493fe56b9e6SYuval Mintz int qed_dmae_info_alloc(struct qed_hwfn *p_hwfn)
494fe56b9e6SYuval Mintz {
495fe56b9e6SYuval Mintz 	dma_addr_t *p_addr = &p_hwfn->dmae_info.completion_word_phys_addr;
496fe56b9e6SYuval Mintz 	struct dmae_cmd **p_cmd = &p_hwfn->dmae_info.p_dmae_cmd;
497fe56b9e6SYuval Mintz 	u32 **p_buff = &p_hwfn->dmae_info.p_intermediate_buffer;
498fe56b9e6SYuval Mintz 	u32 **p_comp = &p_hwfn->dmae_info.p_completion_word;
499fe56b9e6SYuval Mintz 
500fe56b9e6SYuval Mintz 	*p_comp = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
501fe56b9e6SYuval Mintz 				     sizeof(u32),
502fe56b9e6SYuval Mintz 				     p_addr,
503fe56b9e6SYuval Mintz 				     GFP_KERNEL);
504fe56b9e6SYuval Mintz 	if (!*p_comp) {
505fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Failed to allocate `p_completion_word'\n");
506fe56b9e6SYuval Mintz 		goto err;
507fe56b9e6SYuval Mintz 	}
508fe56b9e6SYuval Mintz 
509fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.dmae_cmd_phys_addr;
510fe56b9e6SYuval Mintz 	*p_cmd = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
511fe56b9e6SYuval Mintz 				    sizeof(struct dmae_cmd),
512fe56b9e6SYuval Mintz 				    p_addr, GFP_KERNEL);
513fe56b9e6SYuval Mintz 	if (!*p_cmd) {
514fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Failed to allocate `struct dmae_cmd'\n");
515fe56b9e6SYuval Mintz 		goto err;
516fe56b9e6SYuval Mintz 	}
517fe56b9e6SYuval Mintz 
518fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.intermediate_buffer_phys_addr;
519fe56b9e6SYuval Mintz 	*p_buff = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
520fe56b9e6SYuval Mintz 				     sizeof(u32) * DMAE_MAX_RW_SIZE,
521fe56b9e6SYuval Mintz 				     p_addr, GFP_KERNEL);
522fe56b9e6SYuval Mintz 	if (!*p_buff) {
523fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn, "Failed to allocate `intermediate_buffer'\n");
524fe56b9e6SYuval Mintz 		goto err;
525fe56b9e6SYuval Mintz 	}
526fe56b9e6SYuval Mintz 
527fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.channel = p_hwfn->rel_pf_id;
528fe56b9e6SYuval Mintz 
529fe56b9e6SYuval Mintz 	return 0;
530fe56b9e6SYuval Mintz err:
531fe56b9e6SYuval Mintz 	qed_dmae_info_free(p_hwfn);
532fe56b9e6SYuval Mintz 	return -ENOMEM;
533fe56b9e6SYuval Mintz }
534fe56b9e6SYuval Mintz 
535fe56b9e6SYuval Mintz void qed_dmae_info_free(struct qed_hwfn *p_hwfn)
536fe56b9e6SYuval Mintz {
537fe56b9e6SYuval Mintz 	dma_addr_t p_phys;
538fe56b9e6SYuval Mintz 
539fe56b9e6SYuval Mintz 	/* Just make sure no one is in the middle */
540fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
541fe56b9e6SYuval Mintz 
542fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_completion_word) {
543fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.completion_word_phys_addr;
544fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
545fe56b9e6SYuval Mintz 				  sizeof(u32),
546fe56b9e6SYuval Mintz 				  p_hwfn->dmae_info.p_completion_word,
547fe56b9e6SYuval Mintz 				  p_phys);
548fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_completion_word = NULL;
549fe56b9e6SYuval Mintz 	}
550fe56b9e6SYuval Mintz 
551fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_dmae_cmd) {
552fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.dmae_cmd_phys_addr;
553fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
554fe56b9e6SYuval Mintz 				  sizeof(struct dmae_cmd),
555fe56b9e6SYuval Mintz 				  p_hwfn->dmae_info.p_dmae_cmd,
556fe56b9e6SYuval Mintz 				  p_phys);
557fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_dmae_cmd = NULL;
558fe56b9e6SYuval Mintz 	}
559fe56b9e6SYuval Mintz 
560fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_intermediate_buffer) {
561fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
562fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
563fe56b9e6SYuval Mintz 				  sizeof(u32) * DMAE_MAX_RW_SIZE,
564fe56b9e6SYuval Mintz 				  p_hwfn->dmae_info.p_intermediate_buffer,
565fe56b9e6SYuval Mintz 				  p_phys);
566fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_intermediate_buffer = NULL;
567fe56b9e6SYuval Mintz 	}
568fe56b9e6SYuval Mintz 
569fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
570fe56b9e6SYuval Mintz }
571fe56b9e6SYuval Mintz 
572fe56b9e6SYuval Mintz static int qed_dmae_operation_wait(struct qed_hwfn *p_hwfn)
573fe56b9e6SYuval Mintz {
574fe56b9e6SYuval Mintz 	u32 wait_cnt = 0;
575fe56b9e6SYuval Mintz 	u32 wait_cnt_limit = 10000;
576fe56b9e6SYuval Mintz 
577fe56b9e6SYuval Mintz 	int qed_status = 0;
578fe56b9e6SYuval Mintz 
579fe56b9e6SYuval Mintz 	barrier();
580fe56b9e6SYuval Mintz 	while (*p_hwfn->dmae_info.p_completion_word != DMAE_COMPLETION_VAL) {
581fe56b9e6SYuval Mintz 		udelay(DMAE_MIN_WAIT_TIME);
582fe56b9e6SYuval Mintz 		if (++wait_cnt > wait_cnt_limit) {
583fe56b9e6SYuval Mintz 			DP_NOTICE(p_hwfn->cdev,
584fe56b9e6SYuval Mintz 				  "Timed-out waiting for operation to complete. Completion word is 0x%08x expected 0x%08x.\n",
585fe56b9e6SYuval Mintz 				  *p_hwfn->dmae_info.p_completion_word,
586fe56b9e6SYuval Mintz 				 DMAE_COMPLETION_VAL);
587fe56b9e6SYuval Mintz 			qed_status = -EBUSY;
588fe56b9e6SYuval Mintz 			break;
589fe56b9e6SYuval Mintz 		}
590fe56b9e6SYuval Mintz 
591fe56b9e6SYuval Mintz 		/* to sync the completion_word since we are not
592fe56b9e6SYuval Mintz 		 * using the volatile keyword for p_completion_word
593fe56b9e6SYuval Mintz 		 */
594fe56b9e6SYuval Mintz 		barrier();
595fe56b9e6SYuval Mintz 	}
596fe56b9e6SYuval Mintz 
597fe56b9e6SYuval Mintz 	if (qed_status == 0)
598fe56b9e6SYuval Mintz 		*p_hwfn->dmae_info.p_completion_word = 0;
599fe56b9e6SYuval Mintz 
600fe56b9e6SYuval Mintz 	return qed_status;
601fe56b9e6SYuval Mintz }
602fe56b9e6SYuval Mintz 
603fe56b9e6SYuval Mintz static int qed_dmae_execute_sub_operation(struct qed_hwfn *p_hwfn,
604fe56b9e6SYuval Mintz 					  struct qed_ptt *p_ptt,
605fe56b9e6SYuval Mintz 					  u64 src_addr,
606fe56b9e6SYuval Mintz 					  u64 dst_addr,
607fe56b9e6SYuval Mintz 					  u8 src_type,
608fe56b9e6SYuval Mintz 					  u8 dst_type,
609fe56b9e6SYuval Mintz 					  u32 length)
610fe56b9e6SYuval Mintz {
611fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
612fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
613fe56b9e6SYuval Mintz 	int qed_status = 0;
614fe56b9e6SYuval Mintz 
615fe56b9e6SYuval Mintz 	switch (src_type) {
616fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
617fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
618fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(src_addr));
619fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(src_addr));
620fe56b9e6SYuval Mintz 		break;
621fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
622fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
623fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(phys));
624fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(phys));
625fe56b9e6SYuval Mintz 		memcpy(&p_hwfn->dmae_info.p_intermediate_buffer[0],
626fe56b9e6SYuval Mintz 		       (void *)(uintptr_t)src_addr,
627fe56b9e6SYuval Mintz 		       length * sizeof(u32));
628fe56b9e6SYuval Mintz 		break;
629fe56b9e6SYuval Mintz 	default:
630fe56b9e6SYuval Mintz 		return -EINVAL;
631fe56b9e6SYuval Mintz 	}
632fe56b9e6SYuval Mintz 
633fe56b9e6SYuval Mintz 	switch (dst_type) {
634fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
635fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
636fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(dst_addr));
637fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(dst_addr));
638fe56b9e6SYuval Mintz 		break;
639fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
640fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
641fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(phys));
642fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(phys));
643fe56b9e6SYuval Mintz 		break;
644fe56b9e6SYuval Mintz 	default:
645fe56b9e6SYuval Mintz 		return -EINVAL;
646fe56b9e6SYuval Mintz 	}
647fe56b9e6SYuval Mintz 
648351a4dedSYuval Mintz 	cmd->length_dw = cpu_to_le16((u16)length);
649fe56b9e6SYuval Mintz 
650fe56b9e6SYuval Mintz 	qed_dmae_post_command(p_hwfn, p_ptt);
651fe56b9e6SYuval Mintz 
652fe56b9e6SYuval Mintz 	qed_status = qed_dmae_operation_wait(p_hwfn);
653fe56b9e6SYuval Mintz 
654fe56b9e6SYuval Mintz 	if (qed_status) {
655fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
656fe56b9e6SYuval Mintz 			  "qed_dmae_host2grc: Wait Failed. source_addr 0x%llx, grc_addr 0x%llx, size_in_dwords 0x%x\n",
657fe56b9e6SYuval Mintz 			  src_addr,
658fe56b9e6SYuval Mintz 			  dst_addr,
659fe56b9e6SYuval Mintz 			  length);
660fe56b9e6SYuval Mintz 		return qed_status;
661fe56b9e6SYuval Mintz 	}
662fe56b9e6SYuval Mintz 
663fe56b9e6SYuval Mintz 	if (dst_type == QED_DMAE_ADDRESS_HOST_VIRT)
664fe56b9e6SYuval Mintz 		memcpy((void *)(uintptr_t)(dst_addr),
665fe56b9e6SYuval Mintz 		       &p_hwfn->dmae_info.p_intermediate_buffer[0],
666fe56b9e6SYuval Mintz 		       length * sizeof(u32));
667fe56b9e6SYuval Mintz 
668fe56b9e6SYuval Mintz 	return 0;
669fe56b9e6SYuval Mintz }
670fe56b9e6SYuval Mintz 
671fe56b9e6SYuval Mintz static int qed_dmae_execute_command(struct qed_hwfn *p_hwfn,
672fe56b9e6SYuval Mintz 				    struct qed_ptt *p_ptt,
673fe56b9e6SYuval Mintz 				    u64 src_addr, u64 dst_addr,
674fe56b9e6SYuval Mintz 				    u8 src_type, u8 dst_type,
675fe56b9e6SYuval Mintz 				    u32 size_in_dwords,
676fe56b9e6SYuval Mintz 				    struct qed_dmae_params *p_params)
677fe56b9e6SYuval Mintz {
678fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.completion_word_phys_addr;
679fe56b9e6SYuval Mintz 	u16 length_cur = 0, i = 0, cnt_split = 0, length_mod = 0;
680fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
681fe56b9e6SYuval Mintz 	u64 src_addr_split = 0, dst_addr_split = 0;
682fe56b9e6SYuval Mintz 	u16 length_limit = DMAE_MAX_RW_SIZE;
683fe56b9e6SYuval Mintz 	int qed_status = 0;
684fe56b9e6SYuval Mintz 	u32 offset = 0;
685fe56b9e6SYuval Mintz 
686fe56b9e6SYuval Mintz 	qed_dmae_opcode(p_hwfn,
687fe56b9e6SYuval Mintz 			(src_type == QED_DMAE_ADDRESS_GRC),
688fe56b9e6SYuval Mintz 			(dst_type == QED_DMAE_ADDRESS_GRC),
689fe56b9e6SYuval Mintz 			p_params);
690fe56b9e6SYuval Mintz 
691fe56b9e6SYuval Mintz 	cmd->comp_addr_lo = cpu_to_le32(lower_32_bits(phys));
692fe56b9e6SYuval Mintz 	cmd->comp_addr_hi = cpu_to_le32(upper_32_bits(phys));
693fe56b9e6SYuval Mintz 	cmd->comp_val = cpu_to_le32(DMAE_COMPLETION_VAL);
694fe56b9e6SYuval Mintz 
695fe56b9e6SYuval Mintz 	/* Check if the grc_addr is valid like < MAX_GRC_OFFSET */
696fe56b9e6SYuval Mintz 	cnt_split = size_in_dwords / length_limit;
697fe56b9e6SYuval Mintz 	length_mod = size_in_dwords % length_limit;
698fe56b9e6SYuval Mintz 
699fe56b9e6SYuval Mintz 	src_addr_split = src_addr;
700fe56b9e6SYuval Mintz 	dst_addr_split = dst_addr;
701fe56b9e6SYuval Mintz 
702fe56b9e6SYuval Mintz 	for (i = 0; i <= cnt_split; i++) {
703fe56b9e6SYuval Mintz 		offset = length_limit * i;
704fe56b9e6SYuval Mintz 
705fe56b9e6SYuval Mintz 		if (!(p_params->flags & QED_DMAE_FLAG_RW_REPL_SRC)) {
706fe56b9e6SYuval Mintz 			if (src_type == QED_DMAE_ADDRESS_GRC)
707fe56b9e6SYuval Mintz 				src_addr_split = src_addr + offset;
708fe56b9e6SYuval Mintz 			else
709fe56b9e6SYuval Mintz 				src_addr_split = src_addr + (offset * 4);
710fe56b9e6SYuval Mintz 		}
711fe56b9e6SYuval Mintz 
712fe56b9e6SYuval Mintz 		if (dst_type == QED_DMAE_ADDRESS_GRC)
713fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + offset;
714fe56b9e6SYuval Mintz 		else
715fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + (offset * 4);
716fe56b9e6SYuval Mintz 
717fe56b9e6SYuval Mintz 		length_cur = (cnt_split == i) ? length_mod : length_limit;
718fe56b9e6SYuval Mintz 
719fe56b9e6SYuval Mintz 		/* might be zero on last iteration */
720fe56b9e6SYuval Mintz 		if (!length_cur)
721fe56b9e6SYuval Mintz 			continue;
722fe56b9e6SYuval Mintz 
723fe56b9e6SYuval Mintz 		qed_status = qed_dmae_execute_sub_operation(p_hwfn,
724fe56b9e6SYuval Mintz 							    p_ptt,
725fe56b9e6SYuval Mintz 							    src_addr_split,
726fe56b9e6SYuval Mintz 							    dst_addr_split,
727fe56b9e6SYuval Mintz 							    src_type,
728fe56b9e6SYuval Mintz 							    dst_type,
729fe56b9e6SYuval Mintz 							    length_cur);
730fe56b9e6SYuval Mintz 		if (qed_status) {
731fe56b9e6SYuval Mintz 			DP_NOTICE(p_hwfn,
732fe56b9e6SYuval 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",
733fe56b9e6SYuval Mintz 				  qed_status,
734fe56b9e6SYuval Mintz 				  src_addr,
735fe56b9e6SYuval Mintz 				  dst_addr,
736fe56b9e6SYuval Mintz 				  length_cur);
737fe56b9e6SYuval Mintz 			break;
738fe56b9e6SYuval Mintz 		}
739fe56b9e6SYuval Mintz 	}
740fe56b9e6SYuval Mintz 
741fe56b9e6SYuval Mintz 	return qed_status;
742fe56b9e6SYuval Mintz }
743fe56b9e6SYuval Mintz 
744fe56b9e6SYuval Mintz int qed_dmae_host2grc(struct qed_hwfn *p_hwfn,
745fe56b9e6SYuval Mintz 		      struct qed_ptt *p_ptt,
746fe56b9e6SYuval Mintz 		      u64 source_addr,
747fe56b9e6SYuval Mintz 		      u32 grc_addr,
748fe56b9e6SYuval Mintz 		      u32 size_in_dwords,
749fe56b9e6SYuval Mintz 		      u32 flags)
750fe56b9e6SYuval Mintz {
751fe56b9e6SYuval Mintz 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
752fe56b9e6SYuval Mintz 	struct qed_dmae_params params;
753fe56b9e6SYuval Mintz 	int rc;
754fe56b9e6SYuval Mintz 
755fe56b9e6SYuval Mintz 	memset(&params, 0, sizeof(struct qed_dmae_params));
756fe56b9e6SYuval Mintz 	params.flags = flags;
757fe56b9e6SYuval Mintz 
758fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
759fe56b9e6SYuval Mintz 
760fe56b9e6SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
761fe56b9e6SYuval Mintz 				      grc_addr_in_dw,
762fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_VIRT,
763fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_GRC,
764fe56b9e6SYuval Mintz 				      size_in_dwords, &params);
765fe56b9e6SYuval Mintz 
766fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
767fe56b9e6SYuval Mintz 
768fe56b9e6SYuval Mintz 	return rc;
769fe56b9e6SYuval Mintz }
770fe56b9e6SYuval Mintz 
77137bff2b9SYuval Mintz int
772*722003acSSudarsana Reddy Kalluru qed_dmae_grc2host(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt, u32 grc_addr,
773*722003acSSudarsana Reddy Kalluru 		  dma_addr_t dest_addr, u32 size_in_dwords, u32 flags)
774*722003acSSudarsana Reddy Kalluru {
775*722003acSSudarsana Reddy Kalluru 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
776*722003acSSudarsana Reddy Kalluru 	struct qed_dmae_params params;
777*722003acSSudarsana Reddy Kalluru 	int rc;
778*722003acSSudarsana Reddy Kalluru 
779*722003acSSudarsana Reddy Kalluru 	memset(&params, 0, sizeof(struct qed_dmae_params));
780*722003acSSudarsana Reddy Kalluru 	params.flags = flags;
781*722003acSSudarsana Reddy Kalluru 
782*722003acSSudarsana Reddy Kalluru 	mutex_lock(&p_hwfn->dmae_info.mutex);
783*722003acSSudarsana Reddy Kalluru 
784*722003acSSudarsana Reddy Kalluru 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, grc_addr_in_dw,
785*722003acSSudarsana Reddy Kalluru 				      dest_addr, QED_DMAE_ADDRESS_GRC,
786*722003acSSudarsana Reddy Kalluru 				      QED_DMAE_ADDRESS_HOST_VIRT,
787*722003acSSudarsana Reddy Kalluru 				      size_in_dwords, &params);
788*722003acSSudarsana Reddy Kalluru 
789*722003acSSudarsana Reddy Kalluru 	mutex_unlock(&p_hwfn->dmae_info.mutex);
790*722003acSSudarsana Reddy Kalluru 
791*722003acSSudarsana Reddy Kalluru 	return rc;
792*722003acSSudarsana Reddy Kalluru }
793*722003acSSudarsana Reddy Kalluru 
794*722003acSSudarsana Reddy Kalluru int
79537bff2b9SYuval Mintz qed_dmae_host2host(struct qed_hwfn *p_hwfn,
79637bff2b9SYuval Mintz 		   struct qed_ptt *p_ptt,
79737bff2b9SYuval Mintz 		   dma_addr_t source_addr,
79837bff2b9SYuval Mintz 		   dma_addr_t dest_addr,
79937bff2b9SYuval Mintz 		   u32 size_in_dwords, struct qed_dmae_params *p_params)
80037bff2b9SYuval Mintz {
80137bff2b9SYuval Mintz 	int rc;
80237bff2b9SYuval Mintz 
80337bff2b9SYuval Mintz 	mutex_lock(&(p_hwfn->dmae_info.mutex));
80437bff2b9SYuval Mintz 
80537bff2b9SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
80637bff2b9SYuval Mintz 				      dest_addr,
80737bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
80837bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
80937bff2b9SYuval Mintz 				      size_in_dwords, p_params);
81037bff2b9SYuval Mintz 
81137bff2b9SYuval Mintz 	mutex_unlock(&(p_hwfn->dmae_info.mutex));
81237bff2b9SYuval Mintz 
81337bff2b9SYuval Mintz 	return rc;
81437bff2b9SYuval Mintz }
81537bff2b9SYuval Mintz 
816fe56b9e6SYuval Mintz u16 qed_get_qm_pq(struct qed_hwfn *p_hwfn,
817dbb799c3SYuval Mintz 		  enum protocol_type proto, union qed_qm_pq_params *p_params)
818fe56b9e6SYuval Mintz {
819fe56b9e6SYuval Mintz 	u16 pq_id = 0;
820fe56b9e6SYuval Mintz 
821dbb799c3SYuval Mintz 	if ((proto == PROTOCOLID_CORE ||
822dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ETH ||
823dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ISCSI ||
824dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ROCE) && !p_params) {
825fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
826dbb799c3SYuval Mintz 			  "Protocol %d received NULL PQ params\n", proto);
827fe56b9e6SYuval Mintz 		return 0;
828fe56b9e6SYuval Mintz 	}
829fe56b9e6SYuval Mintz 
830fe56b9e6SYuval Mintz 	switch (proto) {
831fe56b9e6SYuval Mintz 	case PROTOCOLID_CORE:
832fe56b9e6SYuval Mintz 		if (p_params->core.tc == LB_TC)
833fe56b9e6SYuval Mintz 			pq_id = p_hwfn->qm_info.pure_lb_pq;
834dbb799c3SYuval Mintz 		else if (p_params->core.tc == OOO_LB_TC)
835dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.ooo_pq;
836fe56b9e6SYuval Mintz 		else
837fe56b9e6SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
838fe56b9e6SYuval Mintz 		break;
839fe56b9e6SYuval Mintz 	case PROTOCOLID_ETH:
840fe56b9e6SYuval Mintz 		pq_id = p_params->eth.tc;
8411408cc1fSYuval Mintz 		if (p_params->eth.is_vf)
8421408cc1fSYuval Mintz 			pq_id += p_hwfn->qm_info.vf_queues_offset +
8431408cc1fSYuval Mintz 				 p_params->eth.vf_id;
844fe56b9e6SYuval Mintz 		break;
845dbb799c3SYuval Mintz 	case PROTOCOLID_ISCSI:
846dbb799c3SYuval Mintz 		if (p_params->iscsi.q_idx == 1)
847dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.pure_ack_pq;
848dbb799c3SYuval Mintz 		break;
849dbb799c3SYuval Mintz 	case PROTOCOLID_ROCE:
850dbb799c3SYuval Mintz 		if (p_params->roce.dcqcn)
851dbb799c3SYuval Mintz 			pq_id = p_params->roce.qpid;
852dbb799c3SYuval Mintz 		else
853dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
854dbb799c3SYuval Mintz 		if (pq_id > p_hwfn->qm_info.num_pf_rls)
855dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
856dbb799c3SYuval Mintz 		break;
857fe56b9e6SYuval Mintz 	default:
858fe56b9e6SYuval Mintz 		pq_id = 0;
859fe56b9e6SYuval Mintz 	}
860fe56b9e6SYuval Mintz 
861fe56b9e6SYuval Mintz 	pq_id = CM_TX_PQ_BASE + pq_id + RESC_START(p_hwfn, QED_PQ);
862fe56b9e6SYuval Mintz 
863fe56b9e6SYuval Mintz 	return pq_id;
864fe56b9e6SYuval Mintz }
865