xref: /linux/drivers/net/ethernet/qlogic/qed/qed_hw.c (revision e8f1cb507d01205e03f69809af4347ed8ec9db5b)
1fe56b9e6SYuval Mintz /* QLogic qed NIC Driver
2*e8f1cb50SMintz, Yuval  * Copyright (c) 2015-2017  QLogic Corporation
3fe56b9e6SYuval Mintz  *
4*e8f1cb50SMintz, Yuval  * This software is available to you under a choice of one of two
5*e8f1cb50SMintz, Yuval  * licenses.  You may choose to be licensed under the terms of the GNU
6*e8f1cb50SMintz, Yuval  * General Public License (GPL) Version 2, available from the file
7*e8f1cb50SMintz, Yuval  * COPYING in the main directory of this source tree, or the
8*e8f1cb50SMintz, Yuval  * OpenIB.org BSD license below:
9*e8f1cb50SMintz, Yuval  *
10*e8f1cb50SMintz, Yuval  *     Redistribution and use in source and binary forms, with or
11*e8f1cb50SMintz, Yuval  *     without modification, are permitted provided that the following
12*e8f1cb50SMintz, Yuval  *     conditions are met:
13*e8f1cb50SMintz, Yuval  *
14*e8f1cb50SMintz, Yuval  *      - Redistributions of source code must retain the above
15*e8f1cb50SMintz, Yuval  *        copyright notice, this list of conditions and the following
16*e8f1cb50SMintz, Yuval  *        disclaimer.
17*e8f1cb50SMintz, Yuval  *
18*e8f1cb50SMintz, Yuval  *      - Redistributions in binary form must reproduce the above
19*e8f1cb50SMintz, Yuval  *        copyright notice, this list of conditions and the following
20*e8f1cb50SMintz, Yuval  *        disclaimer in the documentation and /or other materials
21*e8f1cb50SMintz, Yuval  *        provided with the distribution.
22*e8f1cb50SMintz, Yuval  *
23*e8f1cb50SMintz, Yuval  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*e8f1cb50SMintz, Yuval  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*e8f1cb50SMintz, Yuval  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*e8f1cb50SMintz, Yuval  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*e8f1cb50SMintz, Yuval  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*e8f1cb50SMintz, Yuval  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*e8f1cb50SMintz, Yuval  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*e8f1cb50SMintz, Yuval  * SOFTWARE.
31fe56b9e6SYuval Mintz  */
32fe56b9e6SYuval Mintz 
33fe56b9e6SYuval Mintz #include <linux/types.h>
34fe56b9e6SYuval Mintz #include <linux/io.h>
35fe56b9e6SYuval Mintz #include <linux/delay.h>
36fe56b9e6SYuval Mintz #include <linux/dma-mapping.h>
37fe56b9e6SYuval Mintz #include <linux/errno.h>
38fe56b9e6SYuval Mintz #include <linux/kernel.h>
39fe56b9e6SYuval Mintz #include <linux/list.h>
40fe56b9e6SYuval Mintz #include <linux/mutex.h>
41fe56b9e6SYuval Mintz #include <linux/pci.h>
42fe56b9e6SYuval Mintz #include <linux/slab.h>
43fe56b9e6SYuval Mintz #include <linux/spinlock.h>
44fe56b9e6SYuval Mintz #include <linux/string.h>
45fe56b9e6SYuval Mintz #include <linux/qed/qed_chain.h>
46fe56b9e6SYuval Mintz #include "qed.h"
47fe56b9e6SYuval Mintz #include "qed_hsi.h"
48fe56b9e6SYuval Mintz #include "qed_hw.h"
49fe56b9e6SYuval Mintz #include "qed_reg_addr.h"
501408cc1fSYuval Mintz #include "qed_sriov.h"
51fe56b9e6SYuval Mintz 
52fe56b9e6SYuval Mintz #define QED_BAR_ACQUIRE_TIMEOUT 1000
53fe56b9e6SYuval Mintz 
54fe56b9e6SYuval Mintz /* Invalid values */
55fe56b9e6SYuval Mintz #define QED_BAR_INVALID_OFFSET          (cpu_to_le32(-1))
56fe56b9e6SYuval Mintz 
57fe56b9e6SYuval Mintz struct qed_ptt {
58fe56b9e6SYuval Mintz 	struct list_head	list_entry;
59fe56b9e6SYuval Mintz 	unsigned int		idx;
60fe56b9e6SYuval Mintz 	struct pxp_ptt_entry	pxp;
61fe56b9e6SYuval Mintz };
62fe56b9e6SYuval Mintz 
63fe56b9e6SYuval Mintz struct qed_ptt_pool {
64fe56b9e6SYuval Mintz 	struct list_head	free_list;
65fe56b9e6SYuval Mintz 	spinlock_t		lock; /* ptt synchronized access */
66fe56b9e6SYuval Mintz 	struct qed_ptt		ptts[PXP_EXTERNAL_BAR_PF_WINDOW_NUM];
67fe56b9e6SYuval Mintz };
68fe56b9e6SYuval Mintz 
69fe56b9e6SYuval Mintz int qed_ptt_pool_alloc(struct qed_hwfn *p_hwfn)
70fe56b9e6SYuval Mintz {
711a635e48SYuval Mintz 	struct qed_ptt_pool *p_pool = kmalloc(sizeof(*p_pool), GFP_KERNEL);
72fe56b9e6SYuval Mintz 	int i;
73fe56b9e6SYuval Mintz 
74fe56b9e6SYuval Mintz 	if (!p_pool)
75fe56b9e6SYuval Mintz 		return -ENOMEM;
76fe56b9e6SYuval Mintz 
77fe56b9e6SYuval Mintz 	INIT_LIST_HEAD(&p_pool->free_list);
78fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
79fe56b9e6SYuval Mintz 		p_pool->ptts[i].idx = i;
80fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.offset = QED_BAR_INVALID_OFFSET;
81fe56b9e6SYuval Mintz 		p_pool->ptts[i].pxp.pretend.control = 0;
82fe56b9e6SYuval Mintz 		if (i >= RESERVED_PTT_MAX)
83fe56b9e6SYuval Mintz 			list_add(&p_pool->ptts[i].list_entry,
84fe56b9e6SYuval Mintz 				 &p_pool->free_list);
85fe56b9e6SYuval Mintz 	}
86fe56b9e6SYuval Mintz 
87fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = p_pool;
88fe56b9e6SYuval Mintz 	spin_lock_init(&p_pool->lock);
89fe56b9e6SYuval Mintz 
90fe56b9e6SYuval Mintz 	return 0;
91fe56b9e6SYuval Mintz }
92fe56b9e6SYuval Mintz 
93fe56b9e6SYuval Mintz void qed_ptt_invalidate(struct qed_hwfn *p_hwfn)
94fe56b9e6SYuval Mintz {
95fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
96fe56b9e6SYuval Mintz 	int i;
97fe56b9e6SYuval Mintz 
98fe56b9e6SYuval Mintz 	for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
99fe56b9e6SYuval Mintz 		p_ptt = &p_hwfn->p_ptt_pool->ptts[i];
100fe56b9e6SYuval Mintz 		p_ptt->pxp.offset = QED_BAR_INVALID_OFFSET;
101fe56b9e6SYuval Mintz 	}
102fe56b9e6SYuval Mintz }
103fe56b9e6SYuval Mintz 
104fe56b9e6SYuval Mintz void qed_ptt_pool_free(struct qed_hwfn *p_hwfn)
105fe56b9e6SYuval Mintz {
106fe56b9e6SYuval Mintz 	kfree(p_hwfn->p_ptt_pool);
107fe56b9e6SYuval Mintz 	p_hwfn->p_ptt_pool = NULL;
108fe56b9e6SYuval Mintz }
109fe56b9e6SYuval Mintz 
110fe56b9e6SYuval Mintz struct qed_ptt *qed_ptt_acquire(struct qed_hwfn *p_hwfn)
111fe56b9e6SYuval Mintz {
112fe56b9e6SYuval Mintz 	struct qed_ptt *p_ptt;
113fe56b9e6SYuval Mintz 	unsigned int i;
114fe56b9e6SYuval Mintz 
115fe56b9e6SYuval Mintz 	/* Take the free PTT from the list */
116fe56b9e6SYuval Mintz 	for (i = 0; i < QED_BAR_ACQUIRE_TIMEOUT; i++) {
117fe56b9e6SYuval Mintz 		spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
118fe56b9e6SYuval Mintz 
119fe56b9e6SYuval Mintz 		if (!list_empty(&p_hwfn->p_ptt_pool->free_list)) {
120fe56b9e6SYuval Mintz 			p_ptt = list_first_entry(&p_hwfn->p_ptt_pool->free_list,
121fe56b9e6SYuval Mintz 						 struct qed_ptt, list_entry);
122fe56b9e6SYuval Mintz 			list_del(&p_ptt->list_entry);
123fe56b9e6SYuval Mintz 
124fe56b9e6SYuval Mintz 			spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
125fe56b9e6SYuval Mintz 
126fe56b9e6SYuval Mintz 			DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
127fe56b9e6SYuval Mintz 				   "allocated ptt %d\n", p_ptt->idx);
128fe56b9e6SYuval Mintz 			return p_ptt;
129fe56b9e6SYuval Mintz 		}
130fe56b9e6SYuval Mintz 
131fe56b9e6SYuval Mintz 		spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
132fe56b9e6SYuval Mintz 		usleep_range(1000, 2000);
133fe56b9e6SYuval Mintz 	}
134fe56b9e6SYuval Mintz 
135fe56b9e6SYuval Mintz 	DP_NOTICE(p_hwfn, "PTT acquire timeout - failed to allocate PTT\n");
136fe56b9e6SYuval Mintz 	return NULL;
137fe56b9e6SYuval Mintz }
138fe56b9e6SYuval Mintz 
1391a635e48SYuval Mintz void qed_ptt_release(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
140fe56b9e6SYuval Mintz {
141fe56b9e6SYuval Mintz 	spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
142fe56b9e6SYuval Mintz 	list_add(&p_ptt->list_entry, &p_hwfn->p_ptt_pool->free_list);
143fe56b9e6SYuval Mintz 	spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
144fe56b9e6SYuval Mintz }
145fe56b9e6SYuval Mintz 
1461a635e48SYuval Mintz u32 qed_ptt_get_hw_addr(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
147fe56b9e6SYuval Mintz {
148fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and we need to translate it to Bytes */
149fe56b9e6SYuval Mintz 	return le32_to_cpu(p_ptt->pxp.offset) << 2;
150fe56b9e6SYuval Mintz }
151fe56b9e6SYuval Mintz 
152fe56b9e6SYuval Mintz static u32 qed_ptt_config_addr(struct qed_ptt *p_ptt)
153fe56b9e6SYuval Mintz {
154fe56b9e6SYuval Mintz 	return PXP_PF_WINDOW_ADMIN_PER_PF_START +
155fe56b9e6SYuval Mintz 	       p_ptt->idx * sizeof(struct pxp_ptt_entry);
156fe56b9e6SYuval Mintz }
157fe56b9e6SYuval Mintz 
158fe56b9e6SYuval Mintz u32 qed_ptt_get_bar_addr(struct qed_ptt *p_ptt)
159fe56b9e6SYuval Mintz {
160fe56b9e6SYuval Mintz 	return PXP_EXTERNAL_BAR_PF_WINDOW_START +
161fe56b9e6SYuval Mintz 	       p_ptt->idx * PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE;
162fe56b9e6SYuval Mintz }
163fe56b9e6SYuval Mintz 
164fe56b9e6SYuval Mintz void qed_ptt_set_win(struct qed_hwfn *p_hwfn,
1651a635e48SYuval Mintz 		     struct qed_ptt *p_ptt, u32 new_hw_addr)
166fe56b9e6SYuval Mintz {
167fe56b9e6SYuval Mintz 	u32 prev_hw_addr;
168fe56b9e6SYuval Mintz 
169fe56b9e6SYuval Mintz 	prev_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
170fe56b9e6SYuval Mintz 
171fe56b9e6SYuval Mintz 	if (new_hw_addr == prev_hw_addr)
172fe56b9e6SYuval Mintz 		return;
173fe56b9e6SYuval Mintz 
174fe56b9e6SYuval Mintz 	/* Update PTT entery in admin window */
175fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
176fe56b9e6SYuval Mintz 		   "Updating PTT entry %d to offset 0x%x\n",
177fe56b9e6SYuval Mintz 		   p_ptt->idx, new_hw_addr);
178fe56b9e6SYuval Mintz 
179fe56b9e6SYuval Mintz 	/* The HW is using DWORDS and the address is in Bytes */
180fe56b9e6SYuval Mintz 	p_ptt->pxp.offset = cpu_to_le32(new_hw_addr >> 2);
181fe56b9e6SYuval Mintz 
182fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
183fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
184fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, offset),
185fe56b9e6SYuval Mintz 	       le32_to_cpu(p_ptt->pxp.offset));
186fe56b9e6SYuval Mintz }
187fe56b9e6SYuval Mintz 
188fe56b9e6SYuval Mintz static u32 qed_set_ptt(struct qed_hwfn *p_hwfn,
1891a635e48SYuval Mintz 		       struct qed_ptt *p_ptt, u32 hw_addr)
190fe56b9e6SYuval Mintz {
191fe56b9e6SYuval Mintz 	u32 win_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
192fe56b9e6SYuval Mintz 	u32 offset;
193fe56b9e6SYuval Mintz 
194fe56b9e6SYuval Mintz 	offset = hw_addr - win_hw_addr;
195fe56b9e6SYuval Mintz 
196fe56b9e6SYuval Mintz 	/* Verify the address is within the window */
197fe56b9e6SYuval Mintz 	if (hw_addr < win_hw_addr ||
198fe56b9e6SYuval Mintz 	    offset >= PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE) {
199fe56b9e6SYuval Mintz 		qed_ptt_set_win(p_hwfn, p_ptt, hw_addr);
200fe56b9e6SYuval Mintz 		offset = 0;
201fe56b9e6SYuval Mintz 	}
202fe56b9e6SYuval Mintz 
203fe56b9e6SYuval Mintz 	return qed_ptt_get_bar_addr(p_ptt) + offset;
204fe56b9e6SYuval Mintz }
205fe56b9e6SYuval Mintz 
206fe56b9e6SYuval Mintz struct qed_ptt *qed_get_reserved_ptt(struct qed_hwfn *p_hwfn,
207fe56b9e6SYuval Mintz 				     enum reserved_ptts ptt_idx)
208fe56b9e6SYuval Mintz {
209fe56b9e6SYuval Mintz 	if (ptt_idx >= RESERVED_PTT_MAX) {
210fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
211fe56b9e6SYuval Mintz 			  "Requested PTT %d is out of range\n", ptt_idx);
212fe56b9e6SYuval Mintz 		return NULL;
213fe56b9e6SYuval Mintz 	}
214fe56b9e6SYuval Mintz 
215fe56b9e6SYuval Mintz 	return &p_hwfn->p_ptt_pool->ptts[ptt_idx];
216fe56b9e6SYuval Mintz }
217fe56b9e6SYuval Mintz 
218fe56b9e6SYuval Mintz void qed_wr(struct qed_hwfn *p_hwfn,
219fe56b9e6SYuval Mintz 	    struct qed_ptt *p_ptt,
220fe56b9e6SYuval Mintz 	    u32 hw_addr, u32 val)
221fe56b9e6SYuval Mintz {
222fe56b9e6SYuval Mintz 	u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
223fe56b9e6SYuval Mintz 
224fe56b9e6SYuval Mintz 	REG_WR(p_hwfn, bar_addr, val);
225fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
226fe56b9e6SYuval Mintz 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
227fe56b9e6SYuval Mintz 		   bar_addr, hw_addr, val);
228fe56b9e6SYuval Mintz }
229fe56b9e6SYuval Mintz 
230fe56b9e6SYuval Mintz u32 qed_rd(struct qed_hwfn *p_hwfn,
231fe56b9e6SYuval Mintz 	   struct qed_ptt *p_ptt,
232fe56b9e6SYuval Mintz 	   u32 hw_addr)
233fe56b9e6SYuval Mintz {
234fe56b9e6SYuval Mintz 	u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
235fe56b9e6SYuval Mintz 	u32 val = REG_RD(p_hwfn, bar_addr);
236fe56b9e6SYuval Mintz 
237fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
238fe56b9e6SYuval Mintz 		   "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
239fe56b9e6SYuval Mintz 		   bar_addr, hw_addr, val);
240fe56b9e6SYuval Mintz 
241fe56b9e6SYuval Mintz 	return val;
242fe56b9e6SYuval Mintz }
243fe56b9e6SYuval Mintz 
244fe56b9e6SYuval Mintz static void qed_memcpy_hw(struct qed_hwfn *p_hwfn,
245fe56b9e6SYuval Mintz 			  struct qed_ptt *p_ptt,
2461a635e48SYuval Mintz 			  void *addr, u32 hw_addr, size_t n, bool to_device)
247fe56b9e6SYuval Mintz {
248fe56b9e6SYuval Mintz 	u32 dw_count, *host_addr, hw_offset;
249fe56b9e6SYuval Mintz 	size_t quota, done = 0;
250fe56b9e6SYuval Mintz 	u32 __iomem *reg_addr;
251fe56b9e6SYuval Mintz 
252fe56b9e6SYuval Mintz 	while (done < n) {
253fe56b9e6SYuval Mintz 		quota = min_t(size_t, n - done,
254fe56b9e6SYuval Mintz 			      PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE);
255fe56b9e6SYuval Mintz 
2561408cc1fSYuval Mintz 		if (IS_PF(p_hwfn->cdev)) {
257fe56b9e6SYuval Mintz 			qed_ptt_set_win(p_hwfn, p_ptt, hw_addr + done);
258fe56b9e6SYuval Mintz 			hw_offset = qed_ptt_get_bar_addr(p_ptt);
2591408cc1fSYuval Mintz 		} else {
2601408cc1fSYuval Mintz 			hw_offset = hw_addr + done;
2611408cc1fSYuval Mintz 		}
262fe56b9e6SYuval Mintz 
263fe56b9e6SYuval Mintz 		dw_count = quota / 4;
264fe56b9e6SYuval Mintz 		host_addr = (u32 *)((u8 *)addr + done);
265fe56b9e6SYuval Mintz 		reg_addr = (u32 __iomem *)REG_ADDR(p_hwfn, hw_offset);
266fe56b9e6SYuval Mintz 		if (to_device)
267fe56b9e6SYuval Mintz 			while (dw_count--)
268fe56b9e6SYuval Mintz 				DIRECT_REG_WR(reg_addr++, *host_addr++);
269fe56b9e6SYuval Mintz 		else
270fe56b9e6SYuval Mintz 			while (dw_count--)
271fe56b9e6SYuval Mintz 				*host_addr++ = DIRECT_REG_RD(reg_addr++);
272fe56b9e6SYuval Mintz 
273fe56b9e6SYuval Mintz 		done += quota;
274fe56b9e6SYuval Mintz 	}
275fe56b9e6SYuval Mintz }
276fe56b9e6SYuval Mintz 
277fe56b9e6SYuval Mintz void qed_memcpy_from(struct qed_hwfn *p_hwfn,
2781a635e48SYuval Mintz 		     struct qed_ptt *p_ptt, void *dest, u32 hw_addr, size_t n)
279fe56b9e6SYuval Mintz {
280fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
281fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, dest %p hw_addr 0x%x, size %lu\n",
282fe56b9e6SYuval Mintz 		   hw_addr, dest, hw_addr, (unsigned long)n);
283fe56b9e6SYuval Mintz 
284fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, dest, hw_addr, n, false);
285fe56b9e6SYuval Mintz }
286fe56b9e6SYuval Mintz 
287fe56b9e6SYuval Mintz void qed_memcpy_to(struct qed_hwfn *p_hwfn,
2881a635e48SYuval Mintz 		   struct qed_ptt *p_ptt, u32 hw_addr, void *src, size_t n)
289fe56b9e6SYuval Mintz {
290fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
291fe56b9e6SYuval Mintz 		   "hw_addr 0x%x, hw_addr 0x%x, src %p size %lu\n",
292fe56b9e6SYuval Mintz 		   hw_addr, hw_addr, src, (unsigned long)n);
293fe56b9e6SYuval Mintz 
294fe56b9e6SYuval Mintz 	qed_memcpy_hw(p_hwfn, p_ptt, src, hw_addr, n, true);
295fe56b9e6SYuval Mintz }
296fe56b9e6SYuval Mintz 
2971a635e48SYuval Mintz void qed_fid_pretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt, u16 fid)
298fe56b9e6SYuval Mintz {
299fe56b9e6SYuval Mintz 	u16 control = 0;
300fe56b9e6SYuval Mintz 
301fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
302fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
303fe56b9e6SYuval Mintz 
304fe56b9e6SYuval Mintz 	/* Every pretend undos previous pretends, including
305fe56b9e6SYuval Mintz 	 * previous port pretend.
306fe56b9e6SYuval Mintz 	 */
307fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
308fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
309fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
310fe56b9e6SYuval Mintz 
311fe56b9e6SYuval Mintz 	if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
312fe56b9e6SYuval Mintz 		fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
313fe56b9e6SYuval Mintz 
314fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
315fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
316fe56b9e6SYuval Mintz 
317fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
318fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
319fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
320fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
321fe56b9e6SYuval Mintz }
322fe56b9e6SYuval Mintz 
323fe56b9e6SYuval Mintz void qed_port_pretend(struct qed_hwfn *p_hwfn,
3241a635e48SYuval Mintz 		      struct qed_ptt *p_ptt, u8 port_id)
325fe56b9e6SYuval Mintz {
326fe56b9e6SYuval Mintz 	u16 control = 0;
327fe56b9e6SYuval Mintz 
328fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
329fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
330fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
331fe56b9e6SYuval Mintz 
332fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
333fe56b9e6SYuval Mintz 
334fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
335fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
336fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
337fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
338fe56b9e6SYuval Mintz }
339fe56b9e6SYuval Mintz 
3401a635e48SYuval Mintz void qed_port_unpretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
341fe56b9e6SYuval Mintz {
342fe56b9e6SYuval Mintz 	u16 control = 0;
343fe56b9e6SYuval Mintz 
344fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
345fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
346fe56b9e6SYuval Mintz 	SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
347fe56b9e6SYuval Mintz 
348fe56b9e6SYuval Mintz 	p_ptt->pxp.pretend.control = cpu_to_le16(control);
349fe56b9e6SYuval Mintz 
350fe56b9e6SYuval Mintz 	REG_WR(p_hwfn,
351fe56b9e6SYuval Mintz 	       qed_ptt_config_addr(p_ptt) +
352fe56b9e6SYuval Mintz 	       offsetof(struct pxp_ptt_entry, pretend),
353fe56b9e6SYuval Mintz 	       *(u32 *)&p_ptt->pxp.pretend);
354fe56b9e6SYuval Mintz }
355fe56b9e6SYuval Mintz 
35632a47e72SYuval Mintz u32 qed_vfid_to_concrete(struct qed_hwfn *p_hwfn, u8 vfid)
35732a47e72SYuval Mintz {
35832a47e72SYuval Mintz 	u32 concrete_fid = 0;
35932a47e72SYuval Mintz 
36032a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_PFID, p_hwfn->rel_pf_id);
36132a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFID, vfid);
36232a47e72SYuval Mintz 	SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFVALID, 1);
36332a47e72SYuval Mintz 
36432a47e72SYuval Mintz 	return concrete_fid;
36532a47e72SYuval Mintz }
36632a47e72SYuval Mintz 
367fe56b9e6SYuval Mintz /* DMAE */
368fe56b9e6SYuval Mintz static void qed_dmae_opcode(struct qed_hwfn *p_hwfn,
369fe56b9e6SYuval Mintz 			    const u8 is_src_type_grc,
370fe56b9e6SYuval Mintz 			    const u8 is_dst_type_grc,
371fe56b9e6SYuval Mintz 			    struct qed_dmae_params *p_params)
372fe56b9e6SYuval Mintz {
37337bff2b9SYuval Mintz 	u16 opcode_b = 0;
374fe56b9e6SYuval Mintz 	u32 opcode = 0;
375fe56b9e6SYuval Mintz 
376fe56b9e6SYuval Mintz 	/* Whether the source is the PCIe or the GRC.
377fe56b9e6SYuval Mintz 	 * 0- The source is the PCIe
378fe56b9e6SYuval Mintz 	 * 1- The source is the GRC.
379fe56b9e6SYuval Mintz 	 */
380fe56b9e6SYuval Mintz 	opcode |= (is_src_type_grc ? DMAE_CMD_SRC_MASK_GRC
381fe56b9e6SYuval Mintz 				   : DMAE_CMD_SRC_MASK_PCIE) <<
382fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_SHIFT;
383fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_SRC_PF_ID_MASK) <<
384fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_PF_ID_SHIFT);
385fe56b9e6SYuval Mintz 
386fe56b9e6SYuval Mintz 	/* The destination of the DMA can be: 0-None 1-PCIe 2-GRC 3-None */
387fe56b9e6SYuval Mintz 	opcode |= (is_dst_type_grc ? DMAE_CMD_DST_MASK_GRC
388fe56b9e6SYuval Mintz 				   : DMAE_CMD_DST_MASK_PCIE) <<
389fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_SHIFT;
390fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_DST_PF_ID_MASK) <<
391fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_PF_ID_SHIFT);
392fe56b9e6SYuval Mintz 
393fe56b9e6SYuval Mintz 	/* Whether to write a completion word to the completion destination:
394fe56b9e6SYuval Mintz 	 * 0-Do not write a completion word
395fe56b9e6SYuval Mintz 	 * 1-Write the completion word
396fe56b9e6SYuval Mintz 	 */
397fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_COMP_WORD_EN_MASK << DMAE_CMD_COMP_WORD_EN_SHIFT);
398fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
399fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_ADDR_RESET_SHIFT);
400fe56b9e6SYuval Mintz 
401fe56b9e6SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_COMPLETION_DST)
402fe56b9e6SYuval Mintz 		opcode |= (1 << DMAE_CMD_COMP_FUNC_SHIFT);
403fe56b9e6SYuval Mintz 
404fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_ENDIANITY << DMAE_CMD_ENDIANITY_MODE_SHIFT);
405fe56b9e6SYuval Mintz 
406fe56b9e6SYuval Mintz 	opcode |= ((p_hwfn->port_id) << DMAE_CMD_PORT_ID_SHIFT);
407fe56b9e6SYuval Mintz 
408fe56b9e6SYuval Mintz 	/* reset source address in next go */
409fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
410fe56b9e6SYuval Mintz 		   DMAE_CMD_SRC_ADDR_RESET_SHIFT);
411fe56b9e6SYuval Mintz 
412fe56b9e6SYuval Mintz 	/* reset dest address in next go */
413fe56b9e6SYuval Mintz 	opcode |= (DMAE_CMD_DST_ADDR_RESET_MASK <<
414fe56b9e6SYuval Mintz 		   DMAE_CMD_DST_ADDR_RESET_SHIFT);
415fe56b9e6SYuval Mintz 
41637bff2b9SYuval Mintz 	/* SRC/DST VFID: all 1's - pf, otherwise VF id */
41737bff2b9SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_VF_SRC) {
41837bff2b9SYuval Mintz 		opcode |= 1 << DMAE_CMD_SRC_VF_ID_VALID_SHIFT;
41937bff2b9SYuval Mintz 		opcode_b |= p_params->src_vfid << DMAE_CMD_SRC_VF_ID_SHIFT;
42037bff2b9SYuval Mintz 	} else {
42137bff2b9SYuval Mintz 		opcode_b |= DMAE_CMD_SRC_VF_ID_MASK <<
42237bff2b9SYuval Mintz 			    DMAE_CMD_SRC_VF_ID_SHIFT;
42337bff2b9SYuval Mintz 	}
424fe56b9e6SYuval Mintz 
42537bff2b9SYuval Mintz 	if (p_params->flags & QED_DMAE_FLAG_VF_DST) {
42637bff2b9SYuval Mintz 		opcode |= 1 << DMAE_CMD_DST_VF_ID_VALID_SHIFT;
42737bff2b9SYuval Mintz 		opcode_b |= p_params->dst_vfid << DMAE_CMD_DST_VF_ID_SHIFT;
42837bff2b9SYuval Mintz 	} else {
42937bff2b9SYuval Mintz 		opcode_b |= DMAE_CMD_DST_VF_ID_MASK << DMAE_CMD_DST_VF_ID_SHIFT;
43037bff2b9SYuval Mintz 	}
431fe56b9e6SYuval Mintz 
432fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode = cpu_to_le32(opcode);
43337bff2b9SYuval Mintz 	p_hwfn->dmae_info.p_dmae_cmd->opcode_b = cpu_to_le16(opcode_b);
434fe56b9e6SYuval Mintz }
435fe56b9e6SYuval Mintz 
436fe56b9e6SYuval Mintz u32 qed_dmae_idx_to_go_cmd(u8 idx)
437fe56b9e6SYuval Mintz {
438fe56b9e6SYuval Mintz 	/* All the DMAE 'go' registers form an array in internal memory */
439fe56b9e6SYuval Mintz 	return DMAE_REG_GO_C0 + (idx << 2);
440fe56b9e6SYuval Mintz }
441fe56b9e6SYuval Mintz 
4421a635e48SYuval Mintz static int qed_dmae_post_command(struct qed_hwfn *p_hwfn,
443fe56b9e6SYuval Mintz 				 struct qed_ptt *p_ptt)
444fe56b9e6SYuval Mintz {
4451a635e48SYuval Mintz 	struct dmae_cmd *p_command = p_hwfn->dmae_info.p_dmae_cmd;
446fe56b9e6SYuval Mintz 	u8 idx_cmd = p_hwfn->dmae_info.channel, i;
447fe56b9e6SYuval Mintz 	int qed_status = 0;
448fe56b9e6SYuval Mintz 
449fe56b9e6SYuval Mintz 	/* verify address is not NULL */
4501a635e48SYuval Mintz 	if ((((!p_command->dst_addr_lo) && (!p_command->dst_addr_hi)) ||
4511a635e48SYuval Mintz 	     ((!p_command->src_addr_lo) && (!p_command->src_addr_hi)))) {
452fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
453fe56b9e6SYuval Mintz 			  "source or destination address 0 idx_cmd=%d\n"
454fe56b9e6SYuval Mintz 			  "opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
455fe56b9e6SYuval Mintz 			  idx_cmd,
4561a635e48SYuval Mintz 			  le32_to_cpu(p_command->opcode),
4571a635e48SYuval Mintz 			  le16_to_cpu(p_command->opcode_b),
4581a635e48SYuval Mintz 			  le16_to_cpu(p_command->length_dw),
4591a635e48SYuval Mintz 			  le32_to_cpu(p_command->src_addr_hi),
4601a635e48SYuval Mintz 			  le32_to_cpu(p_command->src_addr_lo),
4611a635e48SYuval Mintz 			  le32_to_cpu(p_command->dst_addr_hi),
4621a635e48SYuval Mintz 			  le32_to_cpu(p_command->dst_addr_lo));
463fe56b9e6SYuval Mintz 
464fe56b9e6SYuval Mintz 		return -EINVAL;
465fe56b9e6SYuval Mintz 	}
466fe56b9e6SYuval Mintz 
467fe56b9e6SYuval Mintz 	DP_VERBOSE(p_hwfn,
468fe56b9e6SYuval Mintz 		   NETIF_MSG_HW,
469fe56b9e6SYuval Mintz 		   "Posting DMAE command [idx %d]: opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
470fe56b9e6SYuval Mintz 		   idx_cmd,
4711a635e48SYuval Mintz 		   le32_to_cpu(p_command->opcode),
4721a635e48SYuval Mintz 		   le16_to_cpu(p_command->opcode_b),
4731a635e48SYuval Mintz 		   le16_to_cpu(p_command->length_dw),
4741a635e48SYuval Mintz 		   le32_to_cpu(p_command->src_addr_hi),
4751a635e48SYuval Mintz 		   le32_to_cpu(p_command->src_addr_lo),
4761a635e48SYuval Mintz 		   le32_to_cpu(p_command->dst_addr_hi),
4771a635e48SYuval Mintz 		   le32_to_cpu(p_command->dst_addr_lo));
478fe56b9e6SYuval Mintz 
479fe56b9e6SYuval Mintz 	/* Copy the command to DMAE - need to do it before every call
480fe56b9e6SYuval Mintz 	 * for source/dest address no reset.
481fe56b9e6SYuval Mintz 	 * The first 9 DWs are the command registers, the 10 DW is the
482fe56b9e6SYuval Mintz 	 * GO register, and the rest are result registers
483fe56b9e6SYuval Mintz 	 * (which are read only by the client).
484fe56b9e6SYuval Mintz 	 */
485fe56b9e6SYuval Mintz 	for (i = 0; i < DMAE_CMD_SIZE; i++) {
486fe56b9e6SYuval Mintz 		u32 data = (i < DMAE_CMD_SIZE_TO_FILL) ?
4871a635e48SYuval Mintz 			   *(((u32 *)p_command) + i) : 0;
488fe56b9e6SYuval Mintz 
489fe56b9e6SYuval Mintz 		qed_wr(p_hwfn, p_ptt,
490fe56b9e6SYuval Mintz 		       DMAE_REG_CMD_MEM +
491fe56b9e6SYuval Mintz 		       (idx_cmd * DMAE_CMD_SIZE * sizeof(u32)) +
492fe56b9e6SYuval Mintz 		       (i * sizeof(u32)), data);
493fe56b9e6SYuval Mintz 	}
494fe56b9e6SYuval Mintz 
4951a635e48SYuval Mintz 	qed_wr(p_hwfn, p_ptt, qed_dmae_idx_to_go_cmd(idx_cmd), DMAE_GO_VALUE);
496fe56b9e6SYuval Mintz 
497fe56b9e6SYuval Mintz 	return qed_status;
498fe56b9e6SYuval Mintz }
499fe56b9e6SYuval Mintz 
500fe56b9e6SYuval Mintz int qed_dmae_info_alloc(struct qed_hwfn *p_hwfn)
501fe56b9e6SYuval Mintz {
502fe56b9e6SYuval Mintz 	dma_addr_t *p_addr = &p_hwfn->dmae_info.completion_word_phys_addr;
503fe56b9e6SYuval Mintz 	struct dmae_cmd **p_cmd = &p_hwfn->dmae_info.p_dmae_cmd;
504fe56b9e6SYuval Mintz 	u32 **p_buff = &p_hwfn->dmae_info.p_intermediate_buffer;
505fe56b9e6SYuval Mintz 	u32 **p_comp = &p_hwfn->dmae_info.p_completion_word;
506fe56b9e6SYuval Mintz 
507fe56b9e6SYuval Mintz 	*p_comp = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
5081a635e48SYuval Mintz 				     sizeof(u32), p_addr, GFP_KERNEL);
5092591c280SJoe Perches 	if (!*p_comp)
510fe56b9e6SYuval Mintz 		goto err;
511fe56b9e6SYuval Mintz 
512fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.dmae_cmd_phys_addr;
513fe56b9e6SYuval Mintz 	*p_cmd = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
514fe56b9e6SYuval Mintz 				    sizeof(struct dmae_cmd),
515fe56b9e6SYuval Mintz 				    p_addr, GFP_KERNEL);
5162591c280SJoe Perches 	if (!*p_cmd)
517fe56b9e6SYuval Mintz 		goto err;
518fe56b9e6SYuval Mintz 
519fe56b9e6SYuval Mintz 	p_addr = &p_hwfn->dmae_info.intermediate_buffer_phys_addr;
520fe56b9e6SYuval Mintz 	*p_buff = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
521fe56b9e6SYuval Mintz 				     sizeof(u32) * DMAE_MAX_RW_SIZE,
522fe56b9e6SYuval Mintz 				     p_addr, GFP_KERNEL);
5232591c280SJoe Perches 	if (!*p_buff)
524fe56b9e6SYuval Mintz 		goto err;
525fe56b9e6SYuval Mintz 
526fe56b9e6SYuval Mintz 	p_hwfn->dmae_info.channel = p_hwfn->rel_pf_id;
527fe56b9e6SYuval Mintz 
528fe56b9e6SYuval Mintz 	return 0;
529fe56b9e6SYuval Mintz err:
530fe56b9e6SYuval Mintz 	qed_dmae_info_free(p_hwfn);
531fe56b9e6SYuval Mintz 	return -ENOMEM;
532fe56b9e6SYuval Mintz }
533fe56b9e6SYuval Mintz 
534fe56b9e6SYuval Mintz void qed_dmae_info_free(struct qed_hwfn *p_hwfn)
535fe56b9e6SYuval Mintz {
536fe56b9e6SYuval Mintz 	dma_addr_t p_phys;
537fe56b9e6SYuval Mintz 
538fe56b9e6SYuval Mintz 	/* Just make sure no one is in the middle */
539fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
540fe56b9e6SYuval Mintz 
541fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_completion_word) {
542fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.completion_word_phys_addr;
543fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
544fe56b9e6SYuval Mintz 				  sizeof(u32),
5451a635e48SYuval Mintz 				  p_hwfn->dmae_info.p_completion_word, p_phys);
546fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_completion_word = NULL;
547fe56b9e6SYuval Mintz 	}
548fe56b9e6SYuval Mintz 
549fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_dmae_cmd) {
550fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.dmae_cmd_phys_addr;
551fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
552fe56b9e6SYuval Mintz 				  sizeof(struct dmae_cmd),
5531a635e48SYuval Mintz 				  p_hwfn->dmae_info.p_dmae_cmd, p_phys);
554fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_dmae_cmd = NULL;
555fe56b9e6SYuval Mintz 	}
556fe56b9e6SYuval Mintz 
557fe56b9e6SYuval Mintz 	if (p_hwfn->dmae_info.p_intermediate_buffer) {
558fe56b9e6SYuval Mintz 		p_phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
559fe56b9e6SYuval Mintz 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
560fe56b9e6SYuval Mintz 				  sizeof(u32) * DMAE_MAX_RW_SIZE,
561fe56b9e6SYuval Mintz 				  p_hwfn->dmae_info.p_intermediate_buffer,
562fe56b9e6SYuval Mintz 				  p_phys);
563fe56b9e6SYuval Mintz 		p_hwfn->dmae_info.p_intermediate_buffer = NULL;
564fe56b9e6SYuval Mintz 	}
565fe56b9e6SYuval Mintz 
566fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
567fe56b9e6SYuval Mintz }
568fe56b9e6SYuval Mintz 
569fe56b9e6SYuval Mintz static int qed_dmae_operation_wait(struct qed_hwfn *p_hwfn)
570fe56b9e6SYuval Mintz {
5711a635e48SYuval Mintz 	u32 wait_cnt_limit = 10000, wait_cnt = 0;
572fe56b9e6SYuval Mintz 	int qed_status = 0;
573fe56b9e6SYuval Mintz 
574fe56b9e6SYuval Mintz 	barrier();
575fe56b9e6SYuval Mintz 	while (*p_hwfn->dmae_info.p_completion_word != DMAE_COMPLETION_VAL) {
576fe56b9e6SYuval Mintz 		udelay(DMAE_MIN_WAIT_TIME);
577fe56b9e6SYuval Mintz 		if (++wait_cnt > wait_cnt_limit) {
578fe56b9e6SYuval Mintz 			DP_NOTICE(p_hwfn->cdev,
579fe56b9e6SYuval Mintz 				  "Timed-out waiting for operation to complete. Completion word is 0x%08x expected 0x%08x.\n",
580fe56b9e6SYuval Mintz 				  *p_hwfn->dmae_info.p_completion_word,
581fe56b9e6SYuval Mintz 				 DMAE_COMPLETION_VAL);
582fe56b9e6SYuval Mintz 			qed_status = -EBUSY;
583fe56b9e6SYuval Mintz 			break;
584fe56b9e6SYuval Mintz 		}
585fe56b9e6SYuval Mintz 
586fe56b9e6SYuval Mintz 		/* to sync the completion_word since we are not
587fe56b9e6SYuval Mintz 		 * using the volatile keyword for p_completion_word
588fe56b9e6SYuval Mintz 		 */
589fe56b9e6SYuval Mintz 		barrier();
590fe56b9e6SYuval Mintz 	}
591fe56b9e6SYuval Mintz 
592fe56b9e6SYuval Mintz 	if (qed_status == 0)
593fe56b9e6SYuval Mintz 		*p_hwfn->dmae_info.p_completion_word = 0;
594fe56b9e6SYuval Mintz 
595fe56b9e6SYuval Mintz 	return qed_status;
596fe56b9e6SYuval Mintz }
597fe56b9e6SYuval Mintz 
598fe56b9e6SYuval Mintz static int qed_dmae_execute_sub_operation(struct qed_hwfn *p_hwfn,
599fe56b9e6SYuval Mintz 					  struct qed_ptt *p_ptt,
600fe56b9e6SYuval Mintz 					  u64 src_addr,
601fe56b9e6SYuval Mintz 					  u64 dst_addr,
602fe56b9e6SYuval Mintz 					  u8 src_type,
603fe56b9e6SYuval Mintz 					  u8 dst_type,
6041a635e48SYuval Mintz 					  u32 length_dw)
605fe56b9e6SYuval Mintz {
606fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
607fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
608fe56b9e6SYuval Mintz 	int qed_status = 0;
609fe56b9e6SYuval Mintz 
610fe56b9e6SYuval Mintz 	switch (src_type) {
611fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
612fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
613fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(src_addr));
614fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(src_addr));
615fe56b9e6SYuval Mintz 		break;
616fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
617fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
618fe56b9e6SYuval Mintz 		cmd->src_addr_hi = cpu_to_le32(upper_32_bits(phys));
619fe56b9e6SYuval Mintz 		cmd->src_addr_lo = cpu_to_le32(lower_32_bits(phys));
620fe56b9e6SYuval Mintz 		memcpy(&p_hwfn->dmae_info.p_intermediate_buffer[0],
621fe56b9e6SYuval Mintz 		       (void *)(uintptr_t)src_addr,
6221a635e48SYuval Mintz 		       length_dw * sizeof(u32));
623fe56b9e6SYuval Mintz 		break;
624fe56b9e6SYuval Mintz 	default:
625fe56b9e6SYuval Mintz 		return -EINVAL;
626fe56b9e6SYuval Mintz 	}
627fe56b9e6SYuval Mintz 
628fe56b9e6SYuval Mintz 	switch (dst_type) {
629fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_GRC:
630fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_PHYS:
631fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(dst_addr));
632fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(dst_addr));
633fe56b9e6SYuval Mintz 		break;
634fe56b9e6SYuval Mintz 	/* for virtual source addresses we use the intermediate buffer. */
635fe56b9e6SYuval Mintz 	case QED_DMAE_ADDRESS_HOST_VIRT:
636fe56b9e6SYuval Mintz 		cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(phys));
637fe56b9e6SYuval Mintz 		cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(phys));
638fe56b9e6SYuval Mintz 		break;
639fe56b9e6SYuval Mintz 	default:
640fe56b9e6SYuval Mintz 		return -EINVAL;
641fe56b9e6SYuval Mintz 	}
642fe56b9e6SYuval Mintz 
6431a635e48SYuval Mintz 	cmd->length_dw = cpu_to_le16((u16)length_dw);
644fe56b9e6SYuval Mintz 
645fe56b9e6SYuval Mintz 	qed_dmae_post_command(p_hwfn, p_ptt);
646fe56b9e6SYuval Mintz 
647fe56b9e6SYuval Mintz 	qed_status = qed_dmae_operation_wait(p_hwfn);
648fe56b9e6SYuval Mintz 
649fe56b9e6SYuval Mintz 	if (qed_status) {
650fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
651fe56b9e6SYuval Mintz 			  "qed_dmae_host2grc: Wait Failed. source_addr 0x%llx, grc_addr 0x%llx, size_in_dwords 0x%x\n",
6521a635e48SYuval Mintz 			  src_addr, dst_addr, length_dw);
653fe56b9e6SYuval Mintz 		return qed_status;
654fe56b9e6SYuval Mintz 	}
655fe56b9e6SYuval Mintz 
656fe56b9e6SYuval Mintz 	if (dst_type == QED_DMAE_ADDRESS_HOST_VIRT)
657fe56b9e6SYuval Mintz 		memcpy((void *)(uintptr_t)(dst_addr),
658fe56b9e6SYuval Mintz 		       &p_hwfn->dmae_info.p_intermediate_buffer[0],
6591a635e48SYuval Mintz 		       length_dw * sizeof(u32));
660fe56b9e6SYuval Mintz 
661fe56b9e6SYuval Mintz 	return 0;
662fe56b9e6SYuval Mintz }
663fe56b9e6SYuval Mintz 
664fe56b9e6SYuval Mintz static int qed_dmae_execute_command(struct qed_hwfn *p_hwfn,
665fe56b9e6SYuval Mintz 				    struct qed_ptt *p_ptt,
666fe56b9e6SYuval Mintz 				    u64 src_addr, u64 dst_addr,
667fe56b9e6SYuval Mintz 				    u8 src_type, u8 dst_type,
668fe56b9e6SYuval Mintz 				    u32 size_in_dwords,
669fe56b9e6SYuval Mintz 				    struct qed_dmae_params *p_params)
670fe56b9e6SYuval Mintz {
671fe56b9e6SYuval Mintz 	dma_addr_t phys = p_hwfn->dmae_info.completion_word_phys_addr;
672fe56b9e6SYuval Mintz 	u16 length_cur = 0, i = 0, cnt_split = 0, length_mod = 0;
673fe56b9e6SYuval Mintz 	struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
674fe56b9e6SYuval Mintz 	u64 src_addr_split = 0, dst_addr_split = 0;
675fe56b9e6SYuval Mintz 	u16 length_limit = DMAE_MAX_RW_SIZE;
676fe56b9e6SYuval Mintz 	int qed_status = 0;
677fe56b9e6SYuval Mintz 	u32 offset = 0;
678fe56b9e6SYuval Mintz 
679fe56b9e6SYuval Mintz 	qed_dmae_opcode(p_hwfn,
680fe56b9e6SYuval Mintz 			(src_type == QED_DMAE_ADDRESS_GRC),
681fe56b9e6SYuval Mintz 			(dst_type == QED_DMAE_ADDRESS_GRC),
682fe56b9e6SYuval Mintz 			p_params);
683fe56b9e6SYuval Mintz 
684fe56b9e6SYuval Mintz 	cmd->comp_addr_lo = cpu_to_le32(lower_32_bits(phys));
685fe56b9e6SYuval Mintz 	cmd->comp_addr_hi = cpu_to_le32(upper_32_bits(phys));
686fe56b9e6SYuval Mintz 	cmd->comp_val = cpu_to_le32(DMAE_COMPLETION_VAL);
687fe56b9e6SYuval Mintz 
688fe56b9e6SYuval Mintz 	/* Check if the grc_addr is valid like < MAX_GRC_OFFSET */
689fe56b9e6SYuval Mintz 	cnt_split = size_in_dwords / length_limit;
690fe56b9e6SYuval Mintz 	length_mod = size_in_dwords % length_limit;
691fe56b9e6SYuval Mintz 
692fe56b9e6SYuval Mintz 	src_addr_split = src_addr;
693fe56b9e6SYuval Mintz 	dst_addr_split = dst_addr;
694fe56b9e6SYuval Mintz 
695fe56b9e6SYuval Mintz 	for (i = 0; i <= cnt_split; i++) {
696fe56b9e6SYuval Mintz 		offset = length_limit * i;
697fe56b9e6SYuval Mintz 
698fe56b9e6SYuval Mintz 		if (!(p_params->flags & QED_DMAE_FLAG_RW_REPL_SRC)) {
699fe56b9e6SYuval Mintz 			if (src_type == QED_DMAE_ADDRESS_GRC)
700fe56b9e6SYuval Mintz 				src_addr_split = src_addr + offset;
701fe56b9e6SYuval Mintz 			else
702fe56b9e6SYuval Mintz 				src_addr_split = src_addr + (offset * 4);
703fe56b9e6SYuval Mintz 		}
704fe56b9e6SYuval Mintz 
705fe56b9e6SYuval Mintz 		if (dst_type == QED_DMAE_ADDRESS_GRC)
706fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + offset;
707fe56b9e6SYuval Mintz 		else
708fe56b9e6SYuval Mintz 			dst_addr_split = dst_addr + (offset * 4);
709fe56b9e6SYuval Mintz 
710fe56b9e6SYuval Mintz 		length_cur = (cnt_split == i) ? length_mod : length_limit;
711fe56b9e6SYuval Mintz 
712fe56b9e6SYuval Mintz 		/* might be zero on last iteration */
713fe56b9e6SYuval Mintz 		if (!length_cur)
714fe56b9e6SYuval Mintz 			continue;
715fe56b9e6SYuval Mintz 
716fe56b9e6SYuval Mintz 		qed_status = qed_dmae_execute_sub_operation(p_hwfn,
717fe56b9e6SYuval Mintz 							    p_ptt,
718fe56b9e6SYuval Mintz 							    src_addr_split,
719fe56b9e6SYuval Mintz 							    dst_addr_split,
720fe56b9e6SYuval Mintz 							    src_type,
721fe56b9e6SYuval Mintz 							    dst_type,
722fe56b9e6SYuval Mintz 							    length_cur);
723fe56b9e6SYuval Mintz 		if (qed_status) {
724fe56b9e6SYuval Mintz 			DP_NOTICE(p_hwfn,
725fe56b9e6SYuval 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",
7261a635e48SYuval Mintz 				  qed_status, src_addr, dst_addr, length_cur);
727fe56b9e6SYuval Mintz 			break;
728fe56b9e6SYuval Mintz 		}
729fe56b9e6SYuval Mintz 	}
730fe56b9e6SYuval Mintz 
731fe56b9e6SYuval Mintz 	return qed_status;
732fe56b9e6SYuval Mintz }
733fe56b9e6SYuval Mintz 
734fe56b9e6SYuval Mintz int qed_dmae_host2grc(struct qed_hwfn *p_hwfn,
735fe56b9e6SYuval Mintz 		      struct qed_ptt *p_ptt,
7361a635e48SYuval Mintz 		  u64 source_addr, u32 grc_addr, u32 size_in_dwords, u32 flags)
737fe56b9e6SYuval Mintz {
738fe56b9e6SYuval Mintz 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
739fe56b9e6SYuval Mintz 	struct qed_dmae_params params;
740fe56b9e6SYuval Mintz 	int rc;
741fe56b9e6SYuval Mintz 
742fe56b9e6SYuval Mintz 	memset(&params, 0, sizeof(struct qed_dmae_params));
743fe56b9e6SYuval Mintz 	params.flags = flags;
744fe56b9e6SYuval Mintz 
745fe56b9e6SYuval Mintz 	mutex_lock(&p_hwfn->dmae_info.mutex);
746fe56b9e6SYuval Mintz 
747fe56b9e6SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
748fe56b9e6SYuval Mintz 				      grc_addr_in_dw,
749fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_VIRT,
750fe56b9e6SYuval Mintz 				      QED_DMAE_ADDRESS_GRC,
751fe56b9e6SYuval Mintz 				      size_in_dwords, &params);
752fe56b9e6SYuval Mintz 
753fe56b9e6SYuval Mintz 	mutex_unlock(&p_hwfn->dmae_info.mutex);
754fe56b9e6SYuval Mintz 
755fe56b9e6SYuval Mintz 	return rc;
756fe56b9e6SYuval Mintz }
757fe56b9e6SYuval Mintz 
7581a635e48SYuval Mintz int qed_dmae_grc2host(struct qed_hwfn *p_hwfn,
7591a635e48SYuval Mintz 		      struct qed_ptt *p_ptt,
7601a635e48SYuval Mintz 		      u32 grc_addr,
761722003acSSudarsana Reddy Kalluru 		      dma_addr_t dest_addr, u32 size_in_dwords, u32 flags)
762722003acSSudarsana Reddy Kalluru {
763722003acSSudarsana Reddy Kalluru 	u32 grc_addr_in_dw = grc_addr / sizeof(u32);
764722003acSSudarsana Reddy Kalluru 	struct qed_dmae_params params;
765722003acSSudarsana Reddy Kalluru 	int rc;
766722003acSSudarsana Reddy Kalluru 
767722003acSSudarsana Reddy Kalluru 	memset(&params, 0, sizeof(struct qed_dmae_params));
768722003acSSudarsana Reddy Kalluru 	params.flags = flags;
769722003acSSudarsana Reddy Kalluru 
770722003acSSudarsana Reddy Kalluru 	mutex_lock(&p_hwfn->dmae_info.mutex);
771722003acSSudarsana Reddy Kalluru 
772722003acSSudarsana Reddy Kalluru 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, grc_addr_in_dw,
773722003acSSudarsana Reddy Kalluru 				      dest_addr, QED_DMAE_ADDRESS_GRC,
774722003acSSudarsana Reddy Kalluru 				      QED_DMAE_ADDRESS_HOST_VIRT,
775722003acSSudarsana Reddy Kalluru 				      size_in_dwords, &params);
776722003acSSudarsana Reddy Kalluru 
777722003acSSudarsana Reddy Kalluru 	mutex_unlock(&p_hwfn->dmae_info.mutex);
778722003acSSudarsana Reddy Kalluru 
779722003acSSudarsana Reddy Kalluru 	return rc;
780722003acSSudarsana Reddy Kalluru }
781722003acSSudarsana Reddy Kalluru 
7821a635e48SYuval Mintz int qed_dmae_host2host(struct qed_hwfn *p_hwfn,
78337bff2b9SYuval Mintz 		       struct qed_ptt *p_ptt,
78437bff2b9SYuval Mintz 		       dma_addr_t source_addr,
78537bff2b9SYuval Mintz 		       dma_addr_t dest_addr,
78637bff2b9SYuval Mintz 		       u32 size_in_dwords, struct qed_dmae_params *p_params)
78737bff2b9SYuval Mintz {
78837bff2b9SYuval Mintz 	int rc;
78937bff2b9SYuval Mintz 
79037bff2b9SYuval Mintz 	mutex_lock(&(p_hwfn->dmae_info.mutex));
79137bff2b9SYuval Mintz 
79237bff2b9SYuval Mintz 	rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
79337bff2b9SYuval Mintz 				      dest_addr,
79437bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
79537bff2b9SYuval Mintz 				      QED_DMAE_ADDRESS_HOST_PHYS,
79637bff2b9SYuval Mintz 				      size_in_dwords, p_params);
79737bff2b9SYuval Mintz 
79837bff2b9SYuval Mintz 	mutex_unlock(&(p_hwfn->dmae_info.mutex));
79937bff2b9SYuval Mintz 
80037bff2b9SYuval Mintz 	return rc;
80137bff2b9SYuval Mintz }
80237bff2b9SYuval Mintz 
803fe56b9e6SYuval Mintz u16 qed_get_qm_pq(struct qed_hwfn *p_hwfn,
804dbb799c3SYuval Mintz 		  enum protocol_type proto, union qed_qm_pq_params *p_params)
805fe56b9e6SYuval Mintz {
806fe56b9e6SYuval Mintz 	u16 pq_id = 0;
807fe56b9e6SYuval Mintz 
808dbb799c3SYuval Mintz 	if ((proto == PROTOCOLID_CORE ||
809dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ETH ||
810dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ISCSI ||
811dbb799c3SYuval Mintz 	     proto == PROTOCOLID_ROCE) && !p_params) {
812fe56b9e6SYuval Mintz 		DP_NOTICE(p_hwfn,
813dbb799c3SYuval Mintz 			  "Protocol %d received NULL PQ params\n", proto);
814fe56b9e6SYuval Mintz 		return 0;
815fe56b9e6SYuval Mintz 	}
816fe56b9e6SYuval Mintz 
817fe56b9e6SYuval Mintz 	switch (proto) {
818fe56b9e6SYuval Mintz 	case PROTOCOLID_CORE:
819fe56b9e6SYuval Mintz 		if (p_params->core.tc == LB_TC)
820fe56b9e6SYuval Mintz 			pq_id = p_hwfn->qm_info.pure_lb_pq;
821dbb799c3SYuval Mintz 		else if (p_params->core.tc == OOO_LB_TC)
822dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.ooo_pq;
823fe56b9e6SYuval Mintz 		else
824fe56b9e6SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
825fe56b9e6SYuval Mintz 		break;
826fe56b9e6SYuval Mintz 	case PROTOCOLID_ETH:
827fe56b9e6SYuval Mintz 		pq_id = p_params->eth.tc;
8281408cc1fSYuval Mintz 		if (p_params->eth.is_vf)
8291408cc1fSYuval Mintz 			pq_id += p_hwfn->qm_info.vf_queues_offset +
8301408cc1fSYuval Mintz 				 p_params->eth.vf_id;
831fe56b9e6SYuval Mintz 		break;
832dbb799c3SYuval Mintz 	case PROTOCOLID_ISCSI:
833dbb799c3SYuval Mintz 		if (p_params->iscsi.q_idx == 1)
834dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.pure_ack_pq;
835dbb799c3SYuval Mintz 		break;
836dbb799c3SYuval Mintz 	case PROTOCOLID_ROCE:
837dbb799c3SYuval Mintz 		if (p_params->roce.dcqcn)
838dbb799c3SYuval Mintz 			pq_id = p_params->roce.qpid;
839dbb799c3SYuval Mintz 		else
840dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
841dbb799c3SYuval Mintz 		if (pq_id > p_hwfn->qm_info.num_pf_rls)
842dbb799c3SYuval Mintz 			pq_id = p_hwfn->qm_info.offload_pq;
843dbb799c3SYuval Mintz 		break;
844fe56b9e6SYuval Mintz 	default:
845fe56b9e6SYuval Mintz 		pq_id = 0;
846fe56b9e6SYuval Mintz 	}
847fe56b9e6SYuval Mintz 
848fe56b9e6SYuval Mintz 	pq_id = CM_TX_PQ_BASE + pq_id + RESC_START(p_hwfn, QED_PQ);
849fe56b9e6SYuval Mintz 
850fe56b9e6SYuval Mintz 	return pq_id;
851fe56b9e6SYuval Mintz }
852