xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Driver for Marvell PPv2 network controller for Armada 375 SoC.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2014 Marvell
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Marcin Wojtas <mw@semihalf.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/acpi.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/netdevice.h>
13*4882a593Smuzhiyun #include <linux/etherdevice.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/skbuff.h>
16*4882a593Smuzhiyun #include <linux/inetdevice.h>
17*4882a593Smuzhiyun #include <linux/mbus.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
20*4882a593Smuzhiyun #include <linux/interrupt.h>
21*4882a593Smuzhiyun #include <linux/cpumask.h>
22*4882a593Smuzhiyun #include <linux/of.h>
23*4882a593Smuzhiyun #include <linux/of_irq.h>
24*4882a593Smuzhiyun #include <linux/of_mdio.h>
25*4882a593Smuzhiyun #include <linux/of_net.h>
26*4882a593Smuzhiyun #include <linux/of_address.h>
27*4882a593Smuzhiyun #include <linux/of_device.h>
28*4882a593Smuzhiyun #include <linux/phy.h>
29*4882a593Smuzhiyun #include <linux/phylink.h>
30*4882a593Smuzhiyun #include <linux/phy/phy.h>
31*4882a593Smuzhiyun #include <linux/ptp_classify.h>
32*4882a593Smuzhiyun #include <linux/clk.h>
33*4882a593Smuzhiyun #include <linux/hrtimer.h>
34*4882a593Smuzhiyun #include <linux/ktime.h>
35*4882a593Smuzhiyun #include <linux/regmap.h>
36*4882a593Smuzhiyun #include <uapi/linux/ppp_defs.h>
37*4882a593Smuzhiyun #include <net/ip.h>
38*4882a593Smuzhiyun #include <net/ipv6.h>
39*4882a593Smuzhiyun #include <net/tso.h>
40*4882a593Smuzhiyun #include <linux/bpf_trace.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "mvpp2.h"
43*4882a593Smuzhiyun #include "mvpp2_prs.h"
44*4882a593Smuzhiyun #include "mvpp2_cls.h"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun enum mvpp2_bm_pool_log_num {
47*4882a593Smuzhiyun 	MVPP2_BM_SHORT,
48*4882a593Smuzhiyun 	MVPP2_BM_LONG,
49*4882a593Smuzhiyun 	MVPP2_BM_JUMBO,
50*4882a593Smuzhiyun 	MVPP2_BM_POOLS_NUM
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static struct {
54*4882a593Smuzhiyun 	int pkt_size;
55*4882a593Smuzhiyun 	int buf_num;
56*4882a593Smuzhiyun } mvpp2_pools[MVPP2_BM_POOLS_NUM];
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* The prototype is added here to be used in start_dev when using ACPI. This
59*4882a593Smuzhiyun  * will be removed once phylink is used for all modes (dt+ACPI).
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun static void mvpp2_acpi_start(struct mvpp2_port *port);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* Queue modes */
64*4882a593Smuzhiyun #define MVPP2_QDIST_SINGLE_MODE	0
65*4882a593Smuzhiyun #define MVPP2_QDIST_MULTI_MODE	1
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static int queue_mode = MVPP2_QDIST_MULTI_MODE;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun module_param(queue_mode, int, 0444);
70*4882a593Smuzhiyun MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* Utility/helper methods */
73*4882a593Smuzhiyun 
mvpp2_write(struct mvpp2 * priv,u32 offset,u32 data)74*4882a593Smuzhiyun void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	writel(data, priv->swth_base[0] + offset);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
mvpp2_read(struct mvpp2 * priv,u32 offset)79*4882a593Smuzhiyun u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	return readl(priv->swth_base[0] + offset);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
mvpp2_read_relaxed(struct mvpp2 * priv,u32 offset)84*4882a593Smuzhiyun static u32 mvpp2_read_relaxed(struct mvpp2 *priv, u32 offset)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return readl_relaxed(priv->swth_base[0] + offset);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
mvpp2_cpu_to_thread(struct mvpp2 * priv,int cpu)89*4882a593Smuzhiyun static inline u32 mvpp2_cpu_to_thread(struct mvpp2 *priv, int cpu)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	return cpu % priv->nthreads;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun static struct page_pool *
mvpp2_create_page_pool(struct device * dev,int num,int len,enum dma_data_direction dma_dir)95*4882a593Smuzhiyun mvpp2_create_page_pool(struct device *dev, int num, int len,
96*4882a593Smuzhiyun 		       enum dma_data_direction dma_dir)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct page_pool_params pp_params = {
99*4882a593Smuzhiyun 		/* internal DMA mapping in page_pool */
100*4882a593Smuzhiyun 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
101*4882a593Smuzhiyun 		.pool_size = num,
102*4882a593Smuzhiyun 		.nid = NUMA_NO_NODE,
103*4882a593Smuzhiyun 		.dev = dev,
104*4882a593Smuzhiyun 		.dma_dir = dma_dir,
105*4882a593Smuzhiyun 		.offset = MVPP2_SKB_HEADROOM,
106*4882a593Smuzhiyun 		.max_len = len,
107*4882a593Smuzhiyun 	};
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	return page_pool_create(&pp_params);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /* These accessors should be used to access:
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * - per-thread registers, where each thread has its own copy of the
115*4882a593Smuzhiyun  *   register.
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  *   MVPP2_BM_VIRT_ALLOC_REG
118*4882a593Smuzhiyun  *   MVPP2_BM_ADDR_HIGH_ALLOC
119*4882a593Smuzhiyun  *   MVPP22_BM_ADDR_HIGH_RLS_REG
120*4882a593Smuzhiyun  *   MVPP2_BM_VIRT_RLS_REG
121*4882a593Smuzhiyun  *   MVPP2_ISR_RX_TX_CAUSE_REG
122*4882a593Smuzhiyun  *   MVPP2_ISR_RX_TX_MASK_REG
123*4882a593Smuzhiyun  *   MVPP2_TXQ_NUM_REG
124*4882a593Smuzhiyun  *   MVPP2_AGGR_TXQ_UPDATE_REG
125*4882a593Smuzhiyun  *   MVPP2_TXQ_RSVD_REQ_REG
126*4882a593Smuzhiyun  *   MVPP2_TXQ_RSVD_RSLT_REG
127*4882a593Smuzhiyun  *   MVPP2_TXQ_SENT_REG
128*4882a593Smuzhiyun  *   MVPP2_RXQ_NUM_REG
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * - global registers that must be accessed through a specific thread
131*4882a593Smuzhiyun  *   window, because they are related to an access to a per-thread
132*4882a593Smuzhiyun  *   register
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  *   MVPP2_BM_PHY_ALLOC_REG    (related to MVPP2_BM_VIRT_ALLOC_REG)
135*4882a593Smuzhiyun  *   MVPP2_BM_PHY_RLS_REG      (related to MVPP2_BM_VIRT_RLS_REG)
136*4882a593Smuzhiyun  *   MVPP2_RXQ_THRESH_REG      (related to MVPP2_RXQ_NUM_REG)
137*4882a593Smuzhiyun  *   MVPP2_RXQ_DESC_ADDR_REG   (related to MVPP2_RXQ_NUM_REG)
138*4882a593Smuzhiyun  *   MVPP2_RXQ_DESC_SIZE_REG   (related to MVPP2_RXQ_NUM_REG)
139*4882a593Smuzhiyun  *   MVPP2_RXQ_INDEX_REG       (related to MVPP2_RXQ_NUM_REG)
140*4882a593Smuzhiyun  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
141*4882a593Smuzhiyun  *   MVPP2_TXQ_DESC_ADDR_REG   (related to MVPP2_TXQ_NUM_REG)
142*4882a593Smuzhiyun  *   MVPP2_TXQ_DESC_SIZE_REG   (related to MVPP2_TXQ_NUM_REG)
143*4882a593Smuzhiyun  *   MVPP2_TXQ_INDEX_REG       (related to MVPP2_TXQ_NUM_REG)
144*4882a593Smuzhiyun  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
145*4882a593Smuzhiyun  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
146*4882a593Smuzhiyun  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
147*4882a593Smuzhiyun  */
mvpp2_thread_write(struct mvpp2 * priv,unsigned int thread,u32 offset,u32 data)148*4882a593Smuzhiyun static void mvpp2_thread_write(struct mvpp2 *priv, unsigned int thread,
149*4882a593Smuzhiyun 			       u32 offset, u32 data)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	writel(data, priv->swth_base[thread] + offset);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
mvpp2_thread_read(struct mvpp2 * priv,unsigned int thread,u32 offset)154*4882a593Smuzhiyun static u32 mvpp2_thread_read(struct mvpp2 *priv, unsigned int thread,
155*4882a593Smuzhiyun 			     u32 offset)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	return readl(priv->swth_base[thread] + offset);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
mvpp2_thread_write_relaxed(struct mvpp2 * priv,unsigned int thread,u32 offset,u32 data)160*4882a593Smuzhiyun static void mvpp2_thread_write_relaxed(struct mvpp2 *priv, unsigned int thread,
161*4882a593Smuzhiyun 				       u32 offset, u32 data)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	writel_relaxed(data, priv->swth_base[thread] + offset);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
mvpp2_thread_read_relaxed(struct mvpp2 * priv,unsigned int thread,u32 offset)166*4882a593Smuzhiyun static u32 mvpp2_thread_read_relaxed(struct mvpp2 *priv, unsigned int thread,
167*4882a593Smuzhiyun 				     u32 offset)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	return readl_relaxed(priv->swth_base[thread] + offset);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
mvpp2_txdesc_dma_addr_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)172*4882a593Smuzhiyun static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
173*4882a593Smuzhiyun 					    struct mvpp2_tx_desc *tx_desc)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
176*4882a593Smuzhiyun 		return le32_to_cpu(tx_desc->pp21.buf_dma_addr);
177*4882a593Smuzhiyun 	else
178*4882a593Smuzhiyun 		return le64_to_cpu(tx_desc->pp22.buf_dma_addr_ptp) &
179*4882a593Smuzhiyun 		       MVPP2_DESC_DMA_MASK;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
mvpp2_txdesc_dma_addr_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,dma_addr_t dma_addr)182*4882a593Smuzhiyun static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
183*4882a593Smuzhiyun 				      struct mvpp2_tx_desc *tx_desc,
184*4882a593Smuzhiyun 				      dma_addr_t dma_addr)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	dma_addr_t addr, offset;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
189*4882a593Smuzhiyun 	offset = dma_addr & MVPP2_TX_DESC_ALIGN;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21) {
192*4882a593Smuzhiyun 		tx_desc->pp21.buf_dma_addr = cpu_to_le32(addr);
193*4882a593Smuzhiyun 		tx_desc->pp21.packet_offset = offset;
194*4882a593Smuzhiyun 	} else {
195*4882a593Smuzhiyun 		__le64 val = cpu_to_le64(addr);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		tx_desc->pp22.buf_dma_addr_ptp &= ~cpu_to_le64(MVPP2_DESC_DMA_MASK);
198*4882a593Smuzhiyun 		tx_desc->pp22.buf_dma_addr_ptp |= val;
199*4882a593Smuzhiyun 		tx_desc->pp22.packet_offset = offset;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
mvpp2_txdesc_size_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)203*4882a593Smuzhiyun static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
204*4882a593Smuzhiyun 				    struct mvpp2_tx_desc *tx_desc)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
207*4882a593Smuzhiyun 		return le16_to_cpu(tx_desc->pp21.data_size);
208*4882a593Smuzhiyun 	else
209*4882a593Smuzhiyun 		return le16_to_cpu(tx_desc->pp22.data_size);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
mvpp2_txdesc_size_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,size_t size)212*4882a593Smuzhiyun static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
213*4882a593Smuzhiyun 				  struct mvpp2_tx_desc *tx_desc,
214*4882a593Smuzhiyun 				  size_t size)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
217*4882a593Smuzhiyun 		tx_desc->pp21.data_size = cpu_to_le16(size);
218*4882a593Smuzhiyun 	else
219*4882a593Smuzhiyun 		tx_desc->pp22.data_size = cpu_to_le16(size);
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
mvpp2_txdesc_txq_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,unsigned int txq)222*4882a593Smuzhiyun static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
223*4882a593Smuzhiyun 				 struct mvpp2_tx_desc *tx_desc,
224*4882a593Smuzhiyun 				 unsigned int txq)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
227*4882a593Smuzhiyun 		tx_desc->pp21.phys_txq = txq;
228*4882a593Smuzhiyun 	else
229*4882a593Smuzhiyun 		tx_desc->pp22.phys_txq = txq;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
mvpp2_txdesc_cmd_set(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,unsigned int command)232*4882a593Smuzhiyun static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
233*4882a593Smuzhiyun 				 struct mvpp2_tx_desc *tx_desc,
234*4882a593Smuzhiyun 				 unsigned int command)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
237*4882a593Smuzhiyun 		tx_desc->pp21.command = cpu_to_le32(command);
238*4882a593Smuzhiyun 	else
239*4882a593Smuzhiyun 		tx_desc->pp22.command = cpu_to_le32(command);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
mvpp2_txdesc_offset_get(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc)242*4882a593Smuzhiyun static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
243*4882a593Smuzhiyun 					    struct mvpp2_tx_desc *tx_desc)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
246*4882a593Smuzhiyun 		return tx_desc->pp21.packet_offset;
247*4882a593Smuzhiyun 	else
248*4882a593Smuzhiyun 		return tx_desc->pp22.packet_offset;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
mvpp2_rxdesc_dma_addr_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)251*4882a593Smuzhiyun static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
252*4882a593Smuzhiyun 					    struct mvpp2_rx_desc *rx_desc)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
255*4882a593Smuzhiyun 		return le32_to_cpu(rx_desc->pp21.buf_dma_addr);
256*4882a593Smuzhiyun 	else
257*4882a593Smuzhiyun 		return le64_to_cpu(rx_desc->pp22.buf_dma_addr_key_hash) &
258*4882a593Smuzhiyun 		       MVPP2_DESC_DMA_MASK;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
mvpp2_rxdesc_cookie_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)261*4882a593Smuzhiyun static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
262*4882a593Smuzhiyun 					     struct mvpp2_rx_desc *rx_desc)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
265*4882a593Smuzhiyun 		return le32_to_cpu(rx_desc->pp21.buf_cookie);
266*4882a593Smuzhiyun 	else
267*4882a593Smuzhiyun 		return le64_to_cpu(rx_desc->pp22.buf_cookie_misc) &
268*4882a593Smuzhiyun 		       MVPP2_DESC_DMA_MASK;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
mvpp2_rxdesc_size_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)271*4882a593Smuzhiyun static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
272*4882a593Smuzhiyun 				    struct mvpp2_rx_desc *rx_desc)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
275*4882a593Smuzhiyun 		return le16_to_cpu(rx_desc->pp21.data_size);
276*4882a593Smuzhiyun 	else
277*4882a593Smuzhiyun 		return le16_to_cpu(rx_desc->pp22.data_size);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
mvpp2_rxdesc_status_get(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)280*4882a593Smuzhiyun static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
281*4882a593Smuzhiyun 				   struct mvpp2_rx_desc *rx_desc)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
284*4882a593Smuzhiyun 		return le32_to_cpu(rx_desc->pp21.status);
285*4882a593Smuzhiyun 	else
286*4882a593Smuzhiyun 		return le32_to_cpu(rx_desc->pp22.status);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
mvpp2_txq_inc_get(struct mvpp2_txq_pcpu * txq_pcpu)289*4882a593Smuzhiyun static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	txq_pcpu->txq_get_index++;
292*4882a593Smuzhiyun 	if (txq_pcpu->txq_get_index == txq_pcpu->size)
293*4882a593Smuzhiyun 		txq_pcpu->txq_get_index = 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
mvpp2_txq_inc_put(struct mvpp2_port * port,struct mvpp2_txq_pcpu * txq_pcpu,void * data,struct mvpp2_tx_desc * tx_desc,enum mvpp2_tx_buf_type buf_type)296*4882a593Smuzhiyun static void mvpp2_txq_inc_put(struct mvpp2_port *port,
297*4882a593Smuzhiyun 			      struct mvpp2_txq_pcpu *txq_pcpu,
298*4882a593Smuzhiyun 			      void *data,
299*4882a593Smuzhiyun 			      struct mvpp2_tx_desc *tx_desc,
300*4882a593Smuzhiyun 			      enum mvpp2_tx_buf_type buf_type)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu_buf *tx_buf =
303*4882a593Smuzhiyun 		txq_pcpu->buffs + txq_pcpu->txq_put_index;
304*4882a593Smuzhiyun 	tx_buf->type = buf_type;
305*4882a593Smuzhiyun 	if (buf_type == MVPP2_TYPE_SKB)
306*4882a593Smuzhiyun 		tx_buf->skb = data;
307*4882a593Smuzhiyun 	else
308*4882a593Smuzhiyun 		tx_buf->xdpf = data;
309*4882a593Smuzhiyun 	tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
310*4882a593Smuzhiyun 	tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
311*4882a593Smuzhiyun 		mvpp2_txdesc_offset_get(port, tx_desc);
312*4882a593Smuzhiyun 	txq_pcpu->txq_put_index++;
313*4882a593Smuzhiyun 	if (txq_pcpu->txq_put_index == txq_pcpu->size)
314*4882a593Smuzhiyun 		txq_pcpu->txq_put_index = 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun /* Get number of maximum RXQ */
mvpp2_get_nrxqs(struct mvpp2 * priv)318*4882a593Smuzhiyun static int mvpp2_get_nrxqs(struct mvpp2 *priv)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun 	unsigned int nrxqs;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_SINGLE_MODE)
323*4882a593Smuzhiyun 		return 1;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/* According to the PPv2.2 datasheet and our experiments on
326*4882a593Smuzhiyun 	 * PPv2.1, RX queues have an allocation granularity of 4 (when
327*4882a593Smuzhiyun 	 * more than a single one on PPv2.2).
328*4882a593Smuzhiyun 	 * Round up to nearest multiple of 4.
329*4882a593Smuzhiyun 	 */
330*4882a593Smuzhiyun 	nrxqs = (num_possible_cpus() + 3) & ~0x3;
331*4882a593Smuzhiyun 	if (nrxqs > MVPP2_PORT_MAX_RXQ)
332*4882a593Smuzhiyun 		nrxqs = MVPP2_PORT_MAX_RXQ;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return nrxqs;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /* Get number of physical egress port */
mvpp2_egress_port(struct mvpp2_port * port)338*4882a593Smuzhiyun static inline int mvpp2_egress_port(struct mvpp2_port *port)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	return MVPP2_MAX_TCONT + port->id;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /* Get number of physical TXQ */
mvpp2_txq_phys(int port,int txq)344*4882a593Smuzhiyun static inline int mvpp2_txq_phys(int port, int txq)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /* Returns a struct page if page_pool is set, otherwise a buffer */
mvpp2_frag_alloc(const struct mvpp2_bm_pool * pool,struct page_pool * page_pool)350*4882a593Smuzhiyun static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool,
351*4882a593Smuzhiyun 			      struct page_pool *page_pool)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	if (page_pool)
354*4882a593Smuzhiyun 		return page_pool_dev_alloc_pages(page_pool);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (likely(pool->frag_size <= PAGE_SIZE))
357*4882a593Smuzhiyun 		return netdev_alloc_frag(pool->frag_size);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	return kmalloc(pool->frag_size, GFP_ATOMIC);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
mvpp2_frag_free(const struct mvpp2_bm_pool * pool,struct page_pool * page_pool,void * data)362*4882a593Smuzhiyun static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool,
363*4882a593Smuzhiyun 			    struct page_pool *page_pool, void *data)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	if (page_pool)
366*4882a593Smuzhiyun 		page_pool_put_full_page(page_pool, virt_to_head_page(data), false);
367*4882a593Smuzhiyun 	else if (likely(pool->frag_size <= PAGE_SIZE))
368*4882a593Smuzhiyun 		skb_free_frag(data);
369*4882a593Smuzhiyun 	else
370*4882a593Smuzhiyun 		kfree(data);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /* Buffer Manager configuration routines */
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun /* Create pool */
mvpp2_bm_pool_create(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int size)376*4882a593Smuzhiyun static int mvpp2_bm_pool_create(struct device *dev, struct mvpp2 *priv,
377*4882a593Smuzhiyun 				struct mvpp2_bm_pool *bm_pool, int size)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	u32 val;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/* Number of buffer pointers must be a multiple of 16, as per
382*4882a593Smuzhiyun 	 * hardware constraints
383*4882a593Smuzhiyun 	 */
384*4882a593Smuzhiyun 	if (!IS_ALIGNED(size, 16))
385*4882a593Smuzhiyun 		return -EINVAL;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
388*4882a593Smuzhiyun 	 * bytes per buffer pointer
389*4882a593Smuzhiyun 	 */
390*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
391*4882a593Smuzhiyun 		bm_pool->size_bytes = 2 * sizeof(u32) * size;
392*4882a593Smuzhiyun 	else
393*4882a593Smuzhiyun 		bm_pool->size_bytes = 2 * sizeof(u64) * size;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	bm_pool->virt_addr = dma_alloc_coherent(dev, bm_pool->size_bytes,
396*4882a593Smuzhiyun 						&bm_pool->dma_addr,
397*4882a593Smuzhiyun 						GFP_KERNEL);
398*4882a593Smuzhiyun 	if (!bm_pool->virt_addr)
399*4882a593Smuzhiyun 		return -ENOMEM;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
402*4882a593Smuzhiyun 			MVPP2_BM_POOL_PTR_ALIGN)) {
403*4882a593Smuzhiyun 		dma_free_coherent(dev, bm_pool->size_bytes,
404*4882a593Smuzhiyun 				  bm_pool->virt_addr, bm_pool->dma_addr);
405*4882a593Smuzhiyun 		dev_err(dev, "BM pool %d is not %d bytes aligned\n",
406*4882a593Smuzhiyun 			bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
407*4882a593Smuzhiyun 		return -ENOMEM;
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
411*4882a593Smuzhiyun 		    lower_32_bits(bm_pool->dma_addr));
412*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
415*4882a593Smuzhiyun 	val |= MVPP2_BM_START_MASK;
416*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	bm_pool->size = size;
419*4882a593Smuzhiyun 	bm_pool->pkt_size = 0;
420*4882a593Smuzhiyun 	bm_pool->buf_num = 0;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return 0;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /* Set pool buffer size */
mvpp2_bm_pool_bufsize_set(struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int buf_size)426*4882a593Smuzhiyun static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
427*4882a593Smuzhiyun 				      struct mvpp2_bm_pool *bm_pool,
428*4882a593Smuzhiyun 				      int buf_size)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	u32 val;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	bm_pool->buf_size = buf_size;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
435*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
mvpp2_bm_bufs_get_addrs(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,dma_addr_t * dma_addr,phys_addr_t * phys_addr)438*4882a593Smuzhiyun static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
439*4882a593Smuzhiyun 				    struct mvpp2_bm_pool *bm_pool,
440*4882a593Smuzhiyun 				    dma_addr_t *dma_addr,
441*4882a593Smuzhiyun 				    phys_addr_t *phys_addr)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(priv, get_cpu());
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	*dma_addr = mvpp2_thread_read(priv, thread,
446*4882a593Smuzhiyun 				      MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
447*4882a593Smuzhiyun 	*phys_addr = mvpp2_thread_read(priv, thread, MVPP2_BM_VIRT_ALLOC_REG);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22) {
450*4882a593Smuzhiyun 		u32 val;
451*4882a593Smuzhiyun 		u32 dma_addr_highbits, phys_addr_highbits;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 		val = mvpp2_thread_read(priv, thread, MVPP22_BM_ADDR_HIGH_ALLOC);
454*4882a593Smuzhiyun 		dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
455*4882a593Smuzhiyun 		phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
456*4882a593Smuzhiyun 			MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		if (sizeof(dma_addr_t) == 8)
459*4882a593Smuzhiyun 			*dma_addr |= (u64)dma_addr_highbits << 32;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		if (sizeof(phys_addr_t) == 8)
462*4882a593Smuzhiyun 			*phys_addr |= (u64)phys_addr_highbits << 32;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	put_cpu();
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun /* Free all buffers from the pool */
mvpp2_bm_bufs_free(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool,int buf_num)469*4882a593Smuzhiyun static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
470*4882a593Smuzhiyun 			       struct mvpp2_bm_pool *bm_pool, int buf_num)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct page_pool *pp = NULL;
473*4882a593Smuzhiyun 	int i;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if (buf_num > bm_pool->buf_num) {
476*4882a593Smuzhiyun 		WARN(1, "Pool does not have so many bufs pool(%d) bufs(%d)\n",
477*4882a593Smuzhiyun 		     bm_pool->id, buf_num);
478*4882a593Smuzhiyun 		buf_num = bm_pool->buf_num;
479*4882a593Smuzhiyun 	}
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	if (priv->percpu_pools)
482*4882a593Smuzhiyun 		pp = priv->page_pool[bm_pool->id];
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	for (i = 0; i < buf_num; i++) {
485*4882a593Smuzhiyun 		dma_addr_t buf_dma_addr;
486*4882a593Smuzhiyun 		phys_addr_t buf_phys_addr;
487*4882a593Smuzhiyun 		void *data;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 		mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
490*4882a593Smuzhiyun 					&buf_dma_addr, &buf_phys_addr);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		if (!pp)
493*4882a593Smuzhiyun 			dma_unmap_single(dev, buf_dma_addr,
494*4882a593Smuzhiyun 					 bm_pool->buf_size, DMA_FROM_DEVICE);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		data = (void *)phys_to_virt(buf_phys_addr);
497*4882a593Smuzhiyun 		if (!data)
498*4882a593Smuzhiyun 			break;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		mvpp2_frag_free(bm_pool, pp, data);
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	/* Update BM driver with number of buffers removed from pool */
504*4882a593Smuzhiyun 	bm_pool->buf_num -= i;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun /* Check number of buffers in BM pool */
mvpp2_check_hw_buf_num(struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool)508*4882a593Smuzhiyun static int mvpp2_check_hw_buf_num(struct mvpp2 *priv, struct mvpp2_bm_pool *bm_pool)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	int buf_num = 0;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	buf_num += mvpp2_read(priv, MVPP2_BM_POOL_PTRS_NUM_REG(bm_pool->id)) &
513*4882a593Smuzhiyun 				    MVPP22_BM_POOL_PTRS_NUM_MASK;
514*4882a593Smuzhiyun 	buf_num += mvpp2_read(priv, MVPP2_BM_BPPI_PTRS_NUM_REG(bm_pool->id)) &
515*4882a593Smuzhiyun 				    MVPP2_BM_BPPI_PTR_NUM_MASK;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	/* HW has one buffer ready which is not reflected in the counters */
518*4882a593Smuzhiyun 	if (buf_num)
519*4882a593Smuzhiyun 		buf_num += 1;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	return buf_num;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /* Cleanup pool */
mvpp2_bm_pool_destroy(struct device * dev,struct mvpp2 * priv,struct mvpp2_bm_pool * bm_pool)525*4882a593Smuzhiyun static int mvpp2_bm_pool_destroy(struct device *dev, struct mvpp2 *priv,
526*4882a593Smuzhiyun 				 struct mvpp2_bm_pool *bm_pool)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	int buf_num;
529*4882a593Smuzhiyun 	u32 val;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
532*4882a593Smuzhiyun 	mvpp2_bm_bufs_free(dev, priv, bm_pool, buf_num);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	/* Check buffer counters after free */
535*4882a593Smuzhiyun 	buf_num = mvpp2_check_hw_buf_num(priv, bm_pool);
536*4882a593Smuzhiyun 	if (buf_num) {
537*4882a593Smuzhiyun 		WARN(1, "cannot free all buffers in pool %d, buf_num left %d\n",
538*4882a593Smuzhiyun 		     bm_pool->id, bm_pool->buf_num);
539*4882a593Smuzhiyun 		return 0;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
543*4882a593Smuzhiyun 	val |= MVPP2_BM_STOP_MASK;
544*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	if (priv->percpu_pools) {
547*4882a593Smuzhiyun 		page_pool_destroy(priv->page_pool[bm_pool->id]);
548*4882a593Smuzhiyun 		priv->page_pool[bm_pool->id] = NULL;
549*4882a593Smuzhiyun 	}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	dma_free_coherent(dev, bm_pool->size_bytes,
552*4882a593Smuzhiyun 			  bm_pool->virt_addr,
553*4882a593Smuzhiyun 			  bm_pool->dma_addr);
554*4882a593Smuzhiyun 	return 0;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
mvpp2_bm_pools_init(struct device * dev,struct mvpp2 * priv)557*4882a593Smuzhiyun static int mvpp2_bm_pools_init(struct device *dev, struct mvpp2 *priv)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun 	int i, err, size, poolnum = MVPP2_BM_POOLS_NUM;
560*4882a593Smuzhiyun 	struct mvpp2_bm_pool *bm_pool;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (priv->percpu_pools)
563*4882a593Smuzhiyun 		poolnum = mvpp2_get_nrxqs(priv) * 2;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* Create all pools with maximum size */
566*4882a593Smuzhiyun 	size = MVPP2_BM_POOL_SIZE_MAX;
567*4882a593Smuzhiyun 	for (i = 0; i < poolnum; i++) {
568*4882a593Smuzhiyun 		bm_pool = &priv->bm_pools[i];
569*4882a593Smuzhiyun 		bm_pool->id = i;
570*4882a593Smuzhiyun 		err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
571*4882a593Smuzhiyun 		if (err)
572*4882a593Smuzhiyun 			goto err_unroll_pools;
573*4882a593Smuzhiyun 		mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 	return 0;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun err_unroll_pools:
578*4882a593Smuzhiyun 	dev_err(dev, "failed to create BM pool %d, size %d\n", i, size);
579*4882a593Smuzhiyun 	for (i = i - 1; i >= 0; i--)
580*4882a593Smuzhiyun 		mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
581*4882a593Smuzhiyun 	return err;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
mvpp2_bm_init(struct device * dev,struct mvpp2 * priv)584*4882a593Smuzhiyun static int mvpp2_bm_init(struct device *dev, struct mvpp2 *priv)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
587*4882a593Smuzhiyun 	int i, err, poolnum = MVPP2_BM_POOLS_NUM;
588*4882a593Smuzhiyun 	struct mvpp2_port *port;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	if (priv->percpu_pools) {
591*4882a593Smuzhiyun 		for (i = 0; i < priv->port_count; i++) {
592*4882a593Smuzhiyun 			port = priv->port_list[i];
593*4882a593Smuzhiyun 			if (port->xdp_prog) {
594*4882a593Smuzhiyun 				dma_dir = DMA_BIDIRECTIONAL;
595*4882a593Smuzhiyun 				break;
596*4882a593Smuzhiyun 			}
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		poolnum = mvpp2_get_nrxqs(priv) * 2;
600*4882a593Smuzhiyun 		for (i = 0; i < poolnum; i++) {
601*4882a593Smuzhiyun 			/* the pool in use */
602*4882a593Smuzhiyun 			int pn = i / (poolnum / 2);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 			priv->page_pool[i] =
605*4882a593Smuzhiyun 				mvpp2_create_page_pool(dev,
606*4882a593Smuzhiyun 						       mvpp2_pools[pn].buf_num,
607*4882a593Smuzhiyun 						       mvpp2_pools[pn].pkt_size,
608*4882a593Smuzhiyun 						       dma_dir);
609*4882a593Smuzhiyun 			if (IS_ERR(priv->page_pool[i])) {
610*4882a593Smuzhiyun 				int j;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 				for (j = 0; j < i; j++) {
613*4882a593Smuzhiyun 					page_pool_destroy(priv->page_pool[j]);
614*4882a593Smuzhiyun 					priv->page_pool[j] = NULL;
615*4882a593Smuzhiyun 				}
616*4882a593Smuzhiyun 				return PTR_ERR(priv->page_pool[i]);
617*4882a593Smuzhiyun 			}
618*4882a593Smuzhiyun 		}
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	dev_info(dev, "using %d %s buffers\n", poolnum,
622*4882a593Smuzhiyun 		 priv->percpu_pools ? "per-cpu" : "shared");
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	for (i = 0; i < poolnum; i++) {
625*4882a593Smuzhiyun 		/* Mask BM all interrupts */
626*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
627*4882a593Smuzhiyun 		/* Clear BM cause register */
628*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
629*4882a593Smuzhiyun 	}
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	/* Allocate and initialize BM pools */
632*4882a593Smuzhiyun 	priv->bm_pools = devm_kcalloc(dev, poolnum,
633*4882a593Smuzhiyun 				      sizeof(*priv->bm_pools), GFP_KERNEL);
634*4882a593Smuzhiyun 	if (!priv->bm_pools)
635*4882a593Smuzhiyun 		return -ENOMEM;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	err = mvpp2_bm_pools_init(dev, priv);
638*4882a593Smuzhiyun 	if (err < 0)
639*4882a593Smuzhiyun 		return err;
640*4882a593Smuzhiyun 	return 0;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun 
mvpp2_setup_bm_pool(void)643*4882a593Smuzhiyun static void mvpp2_setup_bm_pool(void)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun 	/* Short pool */
646*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_SHORT].buf_num  = MVPP2_BM_SHORT_BUF_NUM;
647*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_SHORT].pkt_size = MVPP2_BM_SHORT_PKT_SIZE;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	/* Long pool */
650*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_LONG].buf_num  = MVPP2_BM_LONG_BUF_NUM;
651*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_LONG].pkt_size = MVPP2_BM_LONG_PKT_SIZE;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/* Jumbo pool */
654*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_JUMBO].buf_num  = MVPP2_BM_JUMBO_BUF_NUM;
655*4882a593Smuzhiyun 	mvpp2_pools[MVPP2_BM_JUMBO].pkt_size = MVPP2_BM_JUMBO_PKT_SIZE;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /* Attach long pool to rxq */
mvpp2_rxq_long_pool_set(struct mvpp2_port * port,int lrxq,int long_pool)659*4882a593Smuzhiyun static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
660*4882a593Smuzhiyun 				    int lrxq, int long_pool)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun 	u32 val, mask;
663*4882a593Smuzhiyun 	int prxq;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	/* Get queue physical ID */
666*4882a593Smuzhiyun 	prxq = port->rxqs[lrxq]->id;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
669*4882a593Smuzhiyun 		mask = MVPP21_RXQ_POOL_LONG_MASK;
670*4882a593Smuzhiyun 	else
671*4882a593Smuzhiyun 		mask = MVPP22_RXQ_POOL_LONG_MASK;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
674*4882a593Smuzhiyun 	val &= ~mask;
675*4882a593Smuzhiyun 	val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
676*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun /* Attach short pool to rxq */
mvpp2_rxq_short_pool_set(struct mvpp2_port * port,int lrxq,int short_pool)680*4882a593Smuzhiyun static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
681*4882a593Smuzhiyun 				     int lrxq, int short_pool)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun 	u32 val, mask;
684*4882a593Smuzhiyun 	int prxq;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/* Get queue physical ID */
687*4882a593Smuzhiyun 	prxq = port->rxqs[lrxq]->id;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
690*4882a593Smuzhiyun 		mask = MVPP21_RXQ_POOL_SHORT_MASK;
691*4882a593Smuzhiyun 	else
692*4882a593Smuzhiyun 		mask = MVPP22_RXQ_POOL_SHORT_MASK;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
695*4882a593Smuzhiyun 	val &= ~mask;
696*4882a593Smuzhiyun 	val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
697*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
mvpp2_buf_alloc(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,struct page_pool * page_pool,dma_addr_t * buf_dma_addr,phys_addr_t * buf_phys_addr,gfp_t gfp_mask)700*4882a593Smuzhiyun static void *mvpp2_buf_alloc(struct mvpp2_port *port,
701*4882a593Smuzhiyun 			     struct mvpp2_bm_pool *bm_pool,
702*4882a593Smuzhiyun 			     struct page_pool *page_pool,
703*4882a593Smuzhiyun 			     dma_addr_t *buf_dma_addr,
704*4882a593Smuzhiyun 			     phys_addr_t *buf_phys_addr,
705*4882a593Smuzhiyun 			     gfp_t gfp_mask)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	dma_addr_t dma_addr;
708*4882a593Smuzhiyun 	struct page *page;
709*4882a593Smuzhiyun 	void *data;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	data = mvpp2_frag_alloc(bm_pool, page_pool);
712*4882a593Smuzhiyun 	if (!data)
713*4882a593Smuzhiyun 		return NULL;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	if (page_pool) {
716*4882a593Smuzhiyun 		page = (struct page *)data;
717*4882a593Smuzhiyun 		dma_addr = page_pool_get_dma_addr(page);
718*4882a593Smuzhiyun 		data = page_to_virt(page);
719*4882a593Smuzhiyun 	} else {
720*4882a593Smuzhiyun 		dma_addr = dma_map_single(port->dev->dev.parent, data,
721*4882a593Smuzhiyun 					  MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
722*4882a593Smuzhiyun 					  DMA_FROM_DEVICE);
723*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
724*4882a593Smuzhiyun 			mvpp2_frag_free(bm_pool, NULL, data);
725*4882a593Smuzhiyun 			return NULL;
726*4882a593Smuzhiyun 		}
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun 	*buf_dma_addr = dma_addr;
729*4882a593Smuzhiyun 	*buf_phys_addr = virt_to_phys(data);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	return data;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun /* Release buffer to BM */
mvpp2_bm_pool_put(struct mvpp2_port * port,int pool,dma_addr_t buf_dma_addr,phys_addr_t buf_phys_addr)735*4882a593Smuzhiyun static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
736*4882a593Smuzhiyun 				     dma_addr_t buf_dma_addr,
737*4882a593Smuzhiyun 				     phys_addr_t buf_phys_addr)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
740*4882a593Smuzhiyun 	unsigned long flags = 0;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	if (test_bit(thread, &port->priv->lock_map))
743*4882a593Smuzhiyun 		spin_lock_irqsave(&port->bm_lock[thread], flags);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP22) {
746*4882a593Smuzhiyun 		u32 val = 0;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		if (sizeof(dma_addr_t) == 8)
749*4882a593Smuzhiyun 			val |= upper_32_bits(buf_dma_addr) &
750*4882a593Smuzhiyun 				MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 		if (sizeof(phys_addr_t) == 8)
753*4882a593Smuzhiyun 			val |= (upper_32_bits(buf_phys_addr)
754*4882a593Smuzhiyun 				<< MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
755*4882a593Smuzhiyun 				MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 		mvpp2_thread_write_relaxed(port->priv, thread,
758*4882a593Smuzhiyun 					   MVPP22_BM_ADDR_HIGH_RLS_REG, val);
759*4882a593Smuzhiyun 	}
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	/* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
762*4882a593Smuzhiyun 	 * returned in the "cookie" field of the RX
763*4882a593Smuzhiyun 	 * descriptor. Instead of storing the virtual address, we
764*4882a593Smuzhiyun 	 * store the physical address
765*4882a593Smuzhiyun 	 */
766*4882a593Smuzhiyun 	mvpp2_thread_write_relaxed(port->priv, thread,
767*4882a593Smuzhiyun 				   MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
768*4882a593Smuzhiyun 	mvpp2_thread_write_relaxed(port->priv, thread,
769*4882a593Smuzhiyun 				   MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	if (test_bit(thread, &port->priv->lock_map))
772*4882a593Smuzhiyun 		spin_unlock_irqrestore(&port->bm_lock[thread], flags);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	put_cpu();
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun /* Allocate buffers for the pool */
mvpp2_bm_bufs_add(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,int buf_num)778*4882a593Smuzhiyun static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
779*4882a593Smuzhiyun 			     struct mvpp2_bm_pool *bm_pool, int buf_num)
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun 	int i, buf_size, total_size;
782*4882a593Smuzhiyun 	dma_addr_t dma_addr;
783*4882a593Smuzhiyun 	phys_addr_t phys_addr;
784*4882a593Smuzhiyun 	struct page_pool *pp = NULL;
785*4882a593Smuzhiyun 	void *buf;
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	if (port->priv->percpu_pools &&
788*4882a593Smuzhiyun 	    bm_pool->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
789*4882a593Smuzhiyun 		netdev_err(port->dev,
790*4882a593Smuzhiyun 			   "attempted to use jumbo frames with per-cpu pools");
791*4882a593Smuzhiyun 		return 0;
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
795*4882a593Smuzhiyun 	total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	if (buf_num < 0 ||
798*4882a593Smuzhiyun 	    (buf_num + bm_pool->buf_num > bm_pool->size)) {
799*4882a593Smuzhiyun 		netdev_err(port->dev,
800*4882a593Smuzhiyun 			   "cannot allocate %d buffers for pool %d\n",
801*4882a593Smuzhiyun 			   buf_num, bm_pool->id);
802*4882a593Smuzhiyun 		return 0;
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	if (port->priv->percpu_pools)
806*4882a593Smuzhiyun 		pp = port->priv->page_pool[bm_pool->id];
807*4882a593Smuzhiyun 	for (i = 0; i < buf_num; i++) {
808*4882a593Smuzhiyun 		buf = mvpp2_buf_alloc(port, bm_pool, pp, &dma_addr,
809*4882a593Smuzhiyun 				      &phys_addr, GFP_KERNEL);
810*4882a593Smuzhiyun 		if (!buf)
811*4882a593Smuzhiyun 			break;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 		mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
814*4882a593Smuzhiyun 				  phys_addr);
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* Update BM driver with number of buffers added to pool */
818*4882a593Smuzhiyun 	bm_pool->buf_num += i;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	netdev_dbg(port->dev,
821*4882a593Smuzhiyun 		   "pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
822*4882a593Smuzhiyun 		   bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	netdev_dbg(port->dev,
825*4882a593Smuzhiyun 		   "pool %d: %d of %d buffers added\n",
826*4882a593Smuzhiyun 		   bm_pool->id, i, buf_num);
827*4882a593Smuzhiyun 	return i;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun /* Notify the driver that BM pool is being used as specific type and return the
831*4882a593Smuzhiyun  * pool pointer on success
832*4882a593Smuzhiyun  */
833*4882a593Smuzhiyun static struct mvpp2_bm_pool *
mvpp2_bm_pool_use(struct mvpp2_port * port,unsigned pool,int pkt_size)834*4882a593Smuzhiyun mvpp2_bm_pool_use(struct mvpp2_port *port, unsigned pool, int pkt_size)
835*4882a593Smuzhiyun {
836*4882a593Smuzhiyun 	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
837*4882a593Smuzhiyun 	int num;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	if ((port->priv->percpu_pools && pool > mvpp2_get_nrxqs(port->priv) * 2) ||
840*4882a593Smuzhiyun 	    (!port->priv->percpu_pools && pool >= MVPP2_BM_POOLS_NUM)) {
841*4882a593Smuzhiyun 		netdev_err(port->dev, "Invalid pool %d\n", pool);
842*4882a593Smuzhiyun 		return NULL;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	/* Allocate buffers in case BM pool is used as long pool, but packet
846*4882a593Smuzhiyun 	 * size doesn't match MTU or BM pool hasn't being used yet
847*4882a593Smuzhiyun 	 */
848*4882a593Smuzhiyun 	if (new_pool->pkt_size == 0) {
849*4882a593Smuzhiyun 		int pkts_num;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 		/* Set default buffer number or free all the buffers in case
852*4882a593Smuzhiyun 		 * the pool is not empty
853*4882a593Smuzhiyun 		 */
854*4882a593Smuzhiyun 		pkts_num = new_pool->buf_num;
855*4882a593Smuzhiyun 		if (pkts_num == 0) {
856*4882a593Smuzhiyun 			if (port->priv->percpu_pools) {
857*4882a593Smuzhiyun 				if (pool < port->nrxqs)
858*4882a593Smuzhiyun 					pkts_num = mvpp2_pools[MVPP2_BM_SHORT].buf_num;
859*4882a593Smuzhiyun 				else
860*4882a593Smuzhiyun 					pkts_num = mvpp2_pools[MVPP2_BM_LONG].buf_num;
861*4882a593Smuzhiyun 			} else {
862*4882a593Smuzhiyun 				pkts_num = mvpp2_pools[pool].buf_num;
863*4882a593Smuzhiyun 			}
864*4882a593Smuzhiyun 		} else {
865*4882a593Smuzhiyun 			mvpp2_bm_bufs_free(port->dev->dev.parent,
866*4882a593Smuzhiyun 					   port->priv, new_pool, pkts_num);
867*4882a593Smuzhiyun 		}
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 		new_pool->pkt_size = pkt_size;
870*4882a593Smuzhiyun 		new_pool->frag_size =
871*4882a593Smuzhiyun 			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
872*4882a593Smuzhiyun 			MVPP2_SKB_SHINFO_SIZE;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 		/* Allocate buffers for this pool */
875*4882a593Smuzhiyun 		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
876*4882a593Smuzhiyun 		if (num != pkts_num) {
877*4882a593Smuzhiyun 			WARN(1, "pool %d: %d of %d allocated\n",
878*4882a593Smuzhiyun 			     new_pool->id, num, pkts_num);
879*4882a593Smuzhiyun 			return NULL;
880*4882a593Smuzhiyun 		}
881*4882a593Smuzhiyun 	}
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
884*4882a593Smuzhiyun 				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	return new_pool;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun static struct mvpp2_bm_pool *
mvpp2_bm_pool_use_percpu(struct mvpp2_port * port,int type,unsigned int pool,int pkt_size)890*4882a593Smuzhiyun mvpp2_bm_pool_use_percpu(struct mvpp2_port *port, int type,
891*4882a593Smuzhiyun 			 unsigned int pool, int pkt_size)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun 	struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
894*4882a593Smuzhiyun 	int num;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	if (pool > port->nrxqs * 2) {
897*4882a593Smuzhiyun 		netdev_err(port->dev, "Invalid pool %d\n", pool);
898*4882a593Smuzhiyun 		return NULL;
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/* Allocate buffers in case BM pool is used as long pool, but packet
902*4882a593Smuzhiyun 	 * size doesn't match MTU or BM pool hasn't being used yet
903*4882a593Smuzhiyun 	 */
904*4882a593Smuzhiyun 	if (new_pool->pkt_size == 0) {
905*4882a593Smuzhiyun 		int pkts_num;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 		/* Set default buffer number or free all the buffers in case
908*4882a593Smuzhiyun 		 * the pool is not empty
909*4882a593Smuzhiyun 		 */
910*4882a593Smuzhiyun 		pkts_num = new_pool->buf_num;
911*4882a593Smuzhiyun 		if (pkts_num == 0)
912*4882a593Smuzhiyun 			pkts_num = mvpp2_pools[type].buf_num;
913*4882a593Smuzhiyun 		else
914*4882a593Smuzhiyun 			mvpp2_bm_bufs_free(port->dev->dev.parent,
915*4882a593Smuzhiyun 					   port->priv, new_pool, pkts_num);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 		new_pool->pkt_size = pkt_size;
918*4882a593Smuzhiyun 		new_pool->frag_size =
919*4882a593Smuzhiyun 			SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
920*4882a593Smuzhiyun 			MVPP2_SKB_SHINFO_SIZE;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 		/* Allocate buffers for this pool */
923*4882a593Smuzhiyun 		num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
924*4882a593Smuzhiyun 		if (num != pkts_num) {
925*4882a593Smuzhiyun 			WARN(1, "pool %d: %d of %d allocated\n",
926*4882a593Smuzhiyun 			     new_pool->id, num, pkts_num);
927*4882a593Smuzhiyun 			return NULL;
928*4882a593Smuzhiyun 		}
929*4882a593Smuzhiyun 	}
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
932*4882a593Smuzhiyun 				  MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	return new_pool;
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun /* Initialize pools for swf, shared buffers variant */
mvpp2_swf_bm_pool_init_shared(struct mvpp2_port * port)938*4882a593Smuzhiyun static int mvpp2_swf_bm_pool_init_shared(struct mvpp2_port *port)
939*4882a593Smuzhiyun {
940*4882a593Smuzhiyun 	enum mvpp2_bm_pool_log_num long_log_pool, short_log_pool;
941*4882a593Smuzhiyun 	int rxq;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	/* If port pkt_size is higher than 1518B:
944*4882a593Smuzhiyun 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
945*4882a593Smuzhiyun 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
946*4882a593Smuzhiyun 	 */
947*4882a593Smuzhiyun 	if (port->pkt_size > MVPP2_BM_LONG_PKT_SIZE) {
948*4882a593Smuzhiyun 		long_log_pool = MVPP2_BM_JUMBO;
949*4882a593Smuzhiyun 		short_log_pool = MVPP2_BM_LONG;
950*4882a593Smuzhiyun 	} else {
951*4882a593Smuzhiyun 		long_log_pool = MVPP2_BM_LONG;
952*4882a593Smuzhiyun 		short_log_pool = MVPP2_BM_SHORT;
953*4882a593Smuzhiyun 	}
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	if (!port->pool_long) {
956*4882a593Smuzhiyun 		port->pool_long =
957*4882a593Smuzhiyun 			mvpp2_bm_pool_use(port, long_log_pool,
958*4882a593Smuzhiyun 					  mvpp2_pools[long_log_pool].pkt_size);
959*4882a593Smuzhiyun 		if (!port->pool_long)
960*4882a593Smuzhiyun 			return -ENOMEM;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		port->pool_long->port_map |= BIT(port->id);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 		for (rxq = 0; rxq < port->nrxqs; rxq++)
965*4882a593Smuzhiyun 			mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
966*4882a593Smuzhiyun 	}
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	if (!port->pool_short) {
969*4882a593Smuzhiyun 		port->pool_short =
970*4882a593Smuzhiyun 			mvpp2_bm_pool_use(port, short_log_pool,
971*4882a593Smuzhiyun 					  mvpp2_pools[short_log_pool].pkt_size);
972*4882a593Smuzhiyun 		if (!port->pool_short)
973*4882a593Smuzhiyun 			return -ENOMEM;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 		port->pool_short->port_map |= BIT(port->id);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 		for (rxq = 0; rxq < port->nrxqs; rxq++)
978*4882a593Smuzhiyun 			mvpp2_rxq_short_pool_set(port, rxq,
979*4882a593Smuzhiyun 						 port->pool_short->id);
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	return 0;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun /* Initialize pools for swf, percpu buffers variant */
mvpp2_swf_bm_pool_init_percpu(struct mvpp2_port * port)986*4882a593Smuzhiyun static int mvpp2_swf_bm_pool_init_percpu(struct mvpp2_port *port)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	struct mvpp2_bm_pool *bm_pool;
989*4882a593Smuzhiyun 	int i;
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	for (i = 0; i < port->nrxqs; i++) {
992*4882a593Smuzhiyun 		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_SHORT, i,
993*4882a593Smuzhiyun 						   mvpp2_pools[MVPP2_BM_SHORT].pkt_size);
994*4882a593Smuzhiyun 		if (!bm_pool)
995*4882a593Smuzhiyun 			return -ENOMEM;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 		bm_pool->port_map |= BIT(port->id);
998*4882a593Smuzhiyun 		mvpp2_rxq_short_pool_set(port, i, bm_pool->id);
999*4882a593Smuzhiyun 	}
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	for (i = 0; i < port->nrxqs; i++) {
1002*4882a593Smuzhiyun 		bm_pool = mvpp2_bm_pool_use_percpu(port, MVPP2_BM_LONG, i + port->nrxqs,
1003*4882a593Smuzhiyun 						   mvpp2_pools[MVPP2_BM_LONG].pkt_size);
1004*4882a593Smuzhiyun 		if (!bm_pool)
1005*4882a593Smuzhiyun 			return -ENOMEM;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 		bm_pool->port_map |= BIT(port->id);
1008*4882a593Smuzhiyun 		mvpp2_rxq_long_pool_set(port, i, bm_pool->id);
1009*4882a593Smuzhiyun 	}
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	port->pool_long = NULL;
1012*4882a593Smuzhiyun 	port->pool_short = NULL;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	return 0;
1015*4882a593Smuzhiyun }
1016*4882a593Smuzhiyun 
mvpp2_swf_bm_pool_init(struct mvpp2_port * port)1017*4882a593Smuzhiyun static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun 	if (port->priv->percpu_pools)
1020*4882a593Smuzhiyun 		return mvpp2_swf_bm_pool_init_percpu(port);
1021*4882a593Smuzhiyun 	else
1022*4882a593Smuzhiyun 		return mvpp2_swf_bm_pool_init_shared(port);
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
mvpp2_set_hw_csum(struct mvpp2_port * port,enum mvpp2_bm_pool_log_num new_long_pool)1025*4882a593Smuzhiyun static void mvpp2_set_hw_csum(struct mvpp2_port *port,
1026*4882a593Smuzhiyun 			      enum mvpp2_bm_pool_log_num new_long_pool)
1027*4882a593Smuzhiyun {
1028*4882a593Smuzhiyun 	const netdev_features_t csums = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	/* Update L4 checksum when jumbo enable/disable on port.
1031*4882a593Smuzhiyun 	 * Only port 0 supports hardware checksum offload due to
1032*4882a593Smuzhiyun 	 * the Tx FIFO size limitation.
1033*4882a593Smuzhiyun 	 * Also, don't set NETIF_F_HW_CSUM because L3_offset in TX descriptor
1034*4882a593Smuzhiyun 	 * has 7 bits, so the maximum L3 offset is 128.
1035*4882a593Smuzhiyun 	 */
1036*4882a593Smuzhiyun 	if (new_long_pool == MVPP2_BM_JUMBO && port->id != 0) {
1037*4882a593Smuzhiyun 		port->dev->features &= ~csums;
1038*4882a593Smuzhiyun 		port->dev->hw_features &= ~csums;
1039*4882a593Smuzhiyun 	} else {
1040*4882a593Smuzhiyun 		port->dev->features |= csums;
1041*4882a593Smuzhiyun 		port->dev->hw_features |= csums;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun 
mvpp2_bm_update_mtu(struct net_device * dev,int mtu)1045*4882a593Smuzhiyun static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
1048*4882a593Smuzhiyun 	enum mvpp2_bm_pool_log_num new_long_pool;
1049*4882a593Smuzhiyun 	int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	if (port->priv->percpu_pools)
1052*4882a593Smuzhiyun 		goto out_set;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	/* If port MTU is higher than 1518B:
1055*4882a593Smuzhiyun 	 * HW Long pool - SW Jumbo pool, HW Short pool - SW Long pool
1056*4882a593Smuzhiyun 	 * else: HW Long pool - SW Long pool, HW Short pool - SW Short pool
1057*4882a593Smuzhiyun 	 */
1058*4882a593Smuzhiyun 	if (pkt_size > MVPP2_BM_LONG_PKT_SIZE)
1059*4882a593Smuzhiyun 		new_long_pool = MVPP2_BM_JUMBO;
1060*4882a593Smuzhiyun 	else
1061*4882a593Smuzhiyun 		new_long_pool = MVPP2_BM_LONG;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	if (new_long_pool != port->pool_long->id) {
1064*4882a593Smuzhiyun 		/* Remove port from old short & long pool */
1065*4882a593Smuzhiyun 		port->pool_long = mvpp2_bm_pool_use(port, port->pool_long->id,
1066*4882a593Smuzhiyun 						    port->pool_long->pkt_size);
1067*4882a593Smuzhiyun 		port->pool_long->port_map &= ~BIT(port->id);
1068*4882a593Smuzhiyun 		port->pool_long = NULL;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 		port->pool_short = mvpp2_bm_pool_use(port, port->pool_short->id,
1071*4882a593Smuzhiyun 						     port->pool_short->pkt_size);
1072*4882a593Smuzhiyun 		port->pool_short->port_map &= ~BIT(port->id);
1073*4882a593Smuzhiyun 		port->pool_short = NULL;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 		port->pkt_size =  pkt_size;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 		/* Add port to new short & long pool */
1078*4882a593Smuzhiyun 		mvpp2_swf_bm_pool_init(port);
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 		mvpp2_set_hw_csum(port, new_long_pool);
1081*4882a593Smuzhiyun 	}
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun out_set:
1084*4882a593Smuzhiyun 	dev->mtu = mtu;
1085*4882a593Smuzhiyun 	dev->wanted_features = dev->features;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	netdev_update_features(dev);
1088*4882a593Smuzhiyun 	return 0;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun 
mvpp2_interrupts_enable(struct mvpp2_port * port)1091*4882a593Smuzhiyun static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
1092*4882a593Smuzhiyun {
1093*4882a593Smuzhiyun 	int i, sw_thread_mask = 0;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
1096*4882a593Smuzhiyun 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1099*4882a593Smuzhiyun 		    MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
mvpp2_interrupts_disable(struct mvpp2_port * port)1102*4882a593Smuzhiyun static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
1103*4882a593Smuzhiyun {
1104*4882a593Smuzhiyun 	int i, sw_thread_mask = 0;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
1107*4882a593Smuzhiyun 		sw_thread_mask |= port->qvecs[i].sw_thread_mask;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1110*4882a593Smuzhiyun 		    MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun 
mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector * qvec)1113*4882a593Smuzhiyun static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct mvpp2_port *port = qvec->port;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1118*4882a593Smuzhiyun 		    MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun 
mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector * qvec)1121*4882a593Smuzhiyun static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun 	struct mvpp2_port *port = qvec->port;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
1126*4882a593Smuzhiyun 		    MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun /* Mask the current thread's Rx/Tx interrupts
1130*4882a593Smuzhiyun  * Called by on_each_cpu(), guaranteed to run with migration disabled,
1131*4882a593Smuzhiyun  * using smp_processor_id() is OK.
1132*4882a593Smuzhiyun  */
mvpp2_interrupts_mask(void * arg)1133*4882a593Smuzhiyun static void mvpp2_interrupts_mask(void *arg)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun 	struct mvpp2_port *port = arg;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	/* If the thread isn't used, don't do anything */
1138*4882a593Smuzhiyun 	if (smp_processor_id() > port->priv->nthreads)
1139*4882a593Smuzhiyun 		return;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv,
1142*4882a593Smuzhiyun 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1143*4882a593Smuzhiyun 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun /* Unmask the current thread's Rx/Tx interrupts.
1147*4882a593Smuzhiyun  * Called by on_each_cpu(), guaranteed to run with migration disabled,
1148*4882a593Smuzhiyun  * using smp_processor_id() is OK.
1149*4882a593Smuzhiyun  */
mvpp2_interrupts_unmask(void * arg)1150*4882a593Smuzhiyun static void mvpp2_interrupts_unmask(void *arg)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	struct mvpp2_port *port = arg;
1153*4882a593Smuzhiyun 	u32 val;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	/* If the thread isn't used, don't do anything */
1156*4882a593Smuzhiyun 	if (smp_processor_id() >= port->priv->nthreads)
1157*4882a593Smuzhiyun 		return;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	val = MVPP2_CAUSE_MISC_SUM_MASK |
1160*4882a593Smuzhiyun 		MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
1161*4882a593Smuzhiyun 	if (port->has_tx_irqs)
1162*4882a593Smuzhiyun 		val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv,
1165*4882a593Smuzhiyun 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
1166*4882a593Smuzhiyun 			   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun static void
mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port * port,bool mask)1170*4882a593Smuzhiyun mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	u32 val;
1173*4882a593Smuzhiyun 	int i;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	if (port->priv->hw_version != MVPP22)
1176*4882a593Smuzhiyun 		return;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	if (mask)
1179*4882a593Smuzhiyun 		val = 0;
1180*4882a593Smuzhiyun 	else
1181*4882a593Smuzhiyun 		val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(MVPP22);
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
1184*4882a593Smuzhiyun 		struct mvpp2_queue_vector *v = port->qvecs + i;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 		if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
1187*4882a593Smuzhiyun 			continue;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 		mvpp2_thread_write(port->priv, v->sw_thread_id,
1190*4882a593Smuzhiyun 				   MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
1191*4882a593Smuzhiyun 	}
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun /* Only GOP port 0 has an XLG MAC */
mvpp2_port_supports_xlg(struct mvpp2_port * port)1195*4882a593Smuzhiyun static bool mvpp2_port_supports_xlg(struct mvpp2_port *port)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun 	return port->gop_id == 0;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun 
mvpp2_port_supports_rgmii(struct mvpp2_port * port)1200*4882a593Smuzhiyun static bool mvpp2_port_supports_rgmii(struct mvpp2_port *port)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	return !(port->priv->hw_version == MVPP22 && port->gop_id == 0);
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun /* Port configuration routines */
mvpp2_is_xlg(phy_interface_t interface)1206*4882a593Smuzhiyun static bool mvpp2_is_xlg(phy_interface_t interface)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	return interface == PHY_INTERFACE_MODE_10GBASER ||
1209*4882a593Smuzhiyun 	       interface == PHY_INTERFACE_MODE_XAUI;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun 
mvpp2_modify(void __iomem * ptr,u32 mask,u32 set)1212*4882a593Smuzhiyun static void mvpp2_modify(void __iomem *ptr, u32 mask, u32 set)
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun 	u32 old, val;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	old = val = readl(ptr);
1217*4882a593Smuzhiyun 	val &= ~mask;
1218*4882a593Smuzhiyun 	val |= set;
1219*4882a593Smuzhiyun 	if (old != val)
1220*4882a593Smuzhiyun 		writel(val, ptr);
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun 
mvpp22_gop_init_rgmii(struct mvpp2_port * port)1223*4882a593Smuzhiyun static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1226*4882a593Smuzhiyun 	u32 val;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1229*4882a593Smuzhiyun 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
1230*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1233*4882a593Smuzhiyun 	if (port->gop_id == 2)
1234*4882a593Smuzhiyun 		val |= GENCONF_CTRL0_PORT0_RGMII;
1235*4882a593Smuzhiyun 	else if (port->gop_id == 3)
1236*4882a593Smuzhiyun 		val |= GENCONF_CTRL0_PORT1_RGMII_MII;
1237*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun 
mvpp22_gop_init_sgmii(struct mvpp2_port * port)1240*4882a593Smuzhiyun static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1243*4882a593Smuzhiyun 	u32 val;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1246*4882a593Smuzhiyun 	val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
1247*4882a593Smuzhiyun 	       GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
1248*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	if (port->gop_id > 1) {
1251*4882a593Smuzhiyun 		regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
1252*4882a593Smuzhiyun 		if (port->gop_id == 2)
1253*4882a593Smuzhiyun 			val &= ~GENCONF_CTRL0_PORT0_RGMII;
1254*4882a593Smuzhiyun 		else if (port->gop_id == 3)
1255*4882a593Smuzhiyun 			val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
1256*4882a593Smuzhiyun 		regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
1257*4882a593Smuzhiyun 	}
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun 
mvpp22_gop_init_10gkr(struct mvpp2_port * port)1260*4882a593Smuzhiyun static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1263*4882a593Smuzhiyun 	void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1264*4882a593Smuzhiyun 	void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1265*4882a593Smuzhiyun 	u32 val;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 	val = readl(xpcs + MVPP22_XPCS_CFG0);
1268*4882a593Smuzhiyun 	val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
1269*4882a593Smuzhiyun 		 MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
1270*4882a593Smuzhiyun 	val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
1271*4882a593Smuzhiyun 	writel(val, xpcs + MVPP22_XPCS_CFG0);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	val = readl(mpcs + MVPP22_MPCS_CTRL);
1274*4882a593Smuzhiyun 	val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
1275*4882a593Smuzhiyun 	writel(val, mpcs + MVPP22_MPCS_CTRL);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1278*4882a593Smuzhiyun 	val &= ~MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7);
1279*4882a593Smuzhiyun 	val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
1280*4882a593Smuzhiyun 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun 
mvpp22_gop_init(struct mvpp2_port * port)1283*4882a593Smuzhiyun static int mvpp22_gop_init(struct mvpp2_port *port)
1284*4882a593Smuzhiyun {
1285*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1286*4882a593Smuzhiyun 	u32 val;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	if (!priv->sysctrl_base)
1289*4882a593Smuzhiyun 		return 0;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	switch (port->phy_interface) {
1292*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII:
1293*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_ID:
1294*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_RXID:
1295*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_TXID:
1296*4882a593Smuzhiyun 		if (!mvpp2_port_supports_rgmii(port))
1297*4882a593Smuzhiyun 			goto invalid_conf;
1298*4882a593Smuzhiyun 		mvpp22_gop_init_rgmii(port);
1299*4882a593Smuzhiyun 		break;
1300*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_SGMII:
1301*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_1000BASEX:
1302*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_2500BASEX:
1303*4882a593Smuzhiyun 		mvpp22_gop_init_sgmii(port);
1304*4882a593Smuzhiyun 		break;
1305*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_10GBASER:
1306*4882a593Smuzhiyun 		if (!mvpp2_port_supports_xlg(port))
1307*4882a593Smuzhiyun 			goto invalid_conf;
1308*4882a593Smuzhiyun 		mvpp22_gop_init_10gkr(port);
1309*4882a593Smuzhiyun 		break;
1310*4882a593Smuzhiyun 	default:
1311*4882a593Smuzhiyun 		goto unsupported_conf;
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
1315*4882a593Smuzhiyun 	val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
1316*4882a593Smuzhiyun 	       GENCONF_PORT_CTRL1_EN(port->gop_id);
1317*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
1320*4882a593Smuzhiyun 	val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
1321*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
1324*4882a593Smuzhiyun 	val |= GENCONF_SOFT_RESET1_GOP;
1325*4882a593Smuzhiyun 	regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun unsupported_conf:
1328*4882a593Smuzhiyun 	return 0;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun invalid_conf:
1331*4882a593Smuzhiyun 	netdev_err(port->dev, "Invalid port configuration\n");
1332*4882a593Smuzhiyun 	return -EINVAL;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun 
mvpp22_gop_unmask_irq(struct mvpp2_port * port)1335*4882a593Smuzhiyun static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	u32 val;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1340*4882a593Smuzhiyun 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1341*4882a593Smuzhiyun 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1342*4882a593Smuzhiyun 		/* Enable the GMAC link status irq for this port */
1343*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1344*4882a593Smuzhiyun 		val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1345*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1346*4882a593Smuzhiyun 	}
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port)) {
1349*4882a593Smuzhiyun 		/* Enable the XLG/GIG irqs for this port */
1350*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1351*4882a593Smuzhiyun 		if (mvpp2_is_xlg(port->phy_interface))
1352*4882a593Smuzhiyun 			val |= MVPP22_XLG_EXT_INT_MASK_XLG;
1353*4882a593Smuzhiyun 		else
1354*4882a593Smuzhiyun 			val |= MVPP22_XLG_EXT_INT_MASK_GIG;
1355*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1356*4882a593Smuzhiyun 	}
1357*4882a593Smuzhiyun }
1358*4882a593Smuzhiyun 
mvpp22_gop_mask_irq(struct mvpp2_port * port)1359*4882a593Smuzhiyun static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
1360*4882a593Smuzhiyun {
1361*4882a593Smuzhiyun 	u32 val;
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port)) {
1364*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
1365*4882a593Smuzhiyun 		val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
1366*4882a593Smuzhiyun 			 MVPP22_XLG_EXT_INT_MASK_GIG);
1367*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
1368*4882a593Smuzhiyun 	}
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
1371*4882a593Smuzhiyun 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1372*4882a593Smuzhiyun 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1373*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
1374*4882a593Smuzhiyun 		val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
1375*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
1376*4882a593Smuzhiyun 	}
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun 
mvpp22_gop_setup_irq(struct mvpp2_port * port)1379*4882a593Smuzhiyun static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
1380*4882a593Smuzhiyun {
1381*4882a593Smuzhiyun 	u32 val;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	mvpp2_modify(port->base + MVPP22_GMAC_INT_SUM_MASK,
1384*4882a593Smuzhiyun 		     MVPP22_GMAC_INT_SUM_MASK_PTP,
1385*4882a593Smuzhiyun 		     MVPP22_GMAC_INT_SUM_MASK_PTP);
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	if (port->phylink ||
1388*4882a593Smuzhiyun 	    phy_interface_mode_is_rgmii(port->phy_interface) ||
1389*4882a593Smuzhiyun 	    phy_interface_mode_is_8023z(port->phy_interface) ||
1390*4882a593Smuzhiyun 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1391*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_GMAC_INT_MASK);
1392*4882a593Smuzhiyun 		val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
1393*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_GMAC_INT_MASK);
1394*4882a593Smuzhiyun 	}
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port)) {
1397*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_INT_MASK);
1398*4882a593Smuzhiyun 		val |= MVPP22_XLG_INT_MASK_LINK;
1399*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_INT_MASK);
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 		mvpp2_modify(port->base + MVPP22_XLG_EXT_INT_MASK,
1402*4882a593Smuzhiyun 			     MVPP22_XLG_EXT_INT_MASK_PTP,
1403*4882a593Smuzhiyun 			     MVPP22_XLG_EXT_INT_MASK_PTP);
1404*4882a593Smuzhiyun 	}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	mvpp22_gop_unmask_irq(port);
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun /* Sets the PHY mode of the COMPHY (which configures the serdes lanes).
1410*4882a593Smuzhiyun  *
1411*4882a593Smuzhiyun  * The PHY mode used by the PPv2 driver comes from the network subsystem, while
1412*4882a593Smuzhiyun  * the one given to the COMPHY comes from the generic PHY subsystem. Hence they
1413*4882a593Smuzhiyun  * differ.
1414*4882a593Smuzhiyun  *
1415*4882a593Smuzhiyun  * The COMPHY configures the serdes lanes regardless of the actual use of the
1416*4882a593Smuzhiyun  * lanes by the physical layer. This is why configurations like
1417*4882a593Smuzhiyun  * "PPv2 (2500BaseX) - COMPHY (2500SGMII)" are valid.
1418*4882a593Smuzhiyun  */
mvpp22_comphy_init(struct mvpp2_port * port)1419*4882a593Smuzhiyun static int mvpp22_comphy_init(struct mvpp2_port *port)
1420*4882a593Smuzhiyun {
1421*4882a593Smuzhiyun 	int ret;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	if (!port->comphy)
1424*4882a593Smuzhiyun 		return 0;
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	ret = phy_set_mode_ext(port->comphy, PHY_MODE_ETHERNET,
1427*4882a593Smuzhiyun 			       port->phy_interface);
1428*4882a593Smuzhiyun 	if (ret)
1429*4882a593Smuzhiyun 		return ret;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	return phy_power_on(port->comphy);
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun 
mvpp2_port_enable(struct mvpp2_port * port)1434*4882a593Smuzhiyun static void mvpp2_port_enable(struct mvpp2_port *port)
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun 	u32 val;
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port) &&
1439*4882a593Smuzhiyun 	    mvpp2_is_xlg(port->phy_interface)) {
1440*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1441*4882a593Smuzhiyun 		val |= MVPP22_XLG_CTRL0_PORT_EN;
1442*4882a593Smuzhiyun 		val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
1443*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1444*4882a593Smuzhiyun 	} else {
1445*4882a593Smuzhiyun 		val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1446*4882a593Smuzhiyun 		val |= MVPP2_GMAC_PORT_EN_MASK;
1447*4882a593Smuzhiyun 		val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
1448*4882a593Smuzhiyun 		writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1449*4882a593Smuzhiyun 	}
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun 
mvpp2_port_disable(struct mvpp2_port * port)1452*4882a593Smuzhiyun static void mvpp2_port_disable(struct mvpp2_port *port)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	u32 val;
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port) &&
1457*4882a593Smuzhiyun 	    mvpp2_is_xlg(port->phy_interface)) {
1458*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
1459*4882a593Smuzhiyun 		val &= ~MVPP22_XLG_CTRL0_PORT_EN;
1460*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1461*4882a593Smuzhiyun 	}
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1464*4882a593Smuzhiyun 	val &= ~(MVPP2_GMAC_PORT_EN_MASK);
1465*4882a593Smuzhiyun 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
mvpp2_port_periodic_xon_disable(struct mvpp2_port * port)1469*4882a593Smuzhiyun static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun 	u32 val;
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
1474*4882a593Smuzhiyun 		    ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
1475*4882a593Smuzhiyun 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun /* Configure loopback port */
mvpp2_port_loopback_set(struct mvpp2_port * port,const struct phylink_link_state * state)1479*4882a593Smuzhiyun static void mvpp2_port_loopback_set(struct mvpp2_port *port,
1480*4882a593Smuzhiyun 				    const struct phylink_link_state *state)
1481*4882a593Smuzhiyun {
1482*4882a593Smuzhiyun 	u32 val;
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	if (state->speed == 1000)
1487*4882a593Smuzhiyun 		val |= MVPP2_GMAC_GMII_LB_EN_MASK;
1488*4882a593Smuzhiyun 	else
1489*4882a593Smuzhiyun 		val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	if (phy_interface_mode_is_8023z(state->interface) ||
1492*4882a593Smuzhiyun 	    state->interface == PHY_INTERFACE_MODE_SGMII)
1493*4882a593Smuzhiyun 		val |= MVPP2_GMAC_PCS_LB_EN_MASK;
1494*4882a593Smuzhiyun 	else
1495*4882a593Smuzhiyun 		val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun enum {
1501*4882a593Smuzhiyun 	ETHTOOL_XDP_REDIRECT,
1502*4882a593Smuzhiyun 	ETHTOOL_XDP_PASS,
1503*4882a593Smuzhiyun 	ETHTOOL_XDP_DROP,
1504*4882a593Smuzhiyun 	ETHTOOL_XDP_TX,
1505*4882a593Smuzhiyun 	ETHTOOL_XDP_TX_ERR,
1506*4882a593Smuzhiyun 	ETHTOOL_XDP_XMIT,
1507*4882a593Smuzhiyun 	ETHTOOL_XDP_XMIT_ERR,
1508*4882a593Smuzhiyun };
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun struct mvpp2_ethtool_counter {
1511*4882a593Smuzhiyun 	unsigned int offset;
1512*4882a593Smuzhiyun 	const char string[ETH_GSTRING_LEN];
1513*4882a593Smuzhiyun 	bool reg_is_64b;
1514*4882a593Smuzhiyun };
1515*4882a593Smuzhiyun 
mvpp2_read_count(struct mvpp2_port * port,const struct mvpp2_ethtool_counter * counter)1516*4882a593Smuzhiyun static u64 mvpp2_read_count(struct mvpp2_port *port,
1517*4882a593Smuzhiyun 			    const struct mvpp2_ethtool_counter *counter)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun 	u64 val;
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun 	val = readl(port->stats_base + counter->offset);
1522*4882a593Smuzhiyun 	if (counter->reg_is_64b)
1523*4882a593Smuzhiyun 		val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	return val;
1526*4882a593Smuzhiyun }
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun /* Some counters are accessed indirectly by first writing an index to
1529*4882a593Smuzhiyun  * MVPP2_CTRS_IDX. The index can represent various resources depending on the
1530*4882a593Smuzhiyun  * register we access, it can be a hit counter for some classification tables,
1531*4882a593Smuzhiyun  * a counter specific to a rxq, a txq or a buffer pool.
1532*4882a593Smuzhiyun  */
mvpp2_read_index(struct mvpp2 * priv,u32 index,u32 reg)1533*4882a593Smuzhiyun static u32 mvpp2_read_index(struct mvpp2 *priv, u32 index, u32 reg)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_CTRS_IDX, index);
1536*4882a593Smuzhiyun 	return mvpp2_read(priv, reg);
1537*4882a593Smuzhiyun }
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun /* Due to the fact that software statistics and hardware statistics are, by
1540*4882a593Smuzhiyun  * design, incremented at different moments in the chain of packet processing,
1541*4882a593Smuzhiyun  * it is very likely that incoming packets could have been dropped after being
1542*4882a593Smuzhiyun  * counted by hardware but before reaching software statistics (most probably
1543*4882a593Smuzhiyun  * multicast packets), and in the oppposite way, during transmission, FCS bytes
1544*4882a593Smuzhiyun  * are added in between as well as TSO skb will be split and header bytes added.
1545*4882a593Smuzhiyun  * Hence, statistics gathered from userspace with ifconfig (software) and
1546*4882a593Smuzhiyun  * ethtool (hardware) cannot be compared.
1547*4882a593Smuzhiyun  */
1548*4882a593Smuzhiyun static const struct mvpp2_ethtool_counter mvpp2_ethtool_mib_regs[] = {
1549*4882a593Smuzhiyun 	{ MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
1550*4882a593Smuzhiyun 	{ MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
1551*4882a593Smuzhiyun 	{ MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
1552*4882a593Smuzhiyun 	{ MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
1553*4882a593Smuzhiyun 	{ MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
1554*4882a593Smuzhiyun 	{ MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
1555*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
1556*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
1557*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
1558*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
1559*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
1560*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
1561*4882a593Smuzhiyun 	{ MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
1562*4882a593Smuzhiyun 	{ MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
1563*4882a593Smuzhiyun 	{ MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
1564*4882a593Smuzhiyun 	{ MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
1565*4882a593Smuzhiyun 	{ MVPP2_MIB_FC_SENT, "fc_sent" },
1566*4882a593Smuzhiyun 	{ MVPP2_MIB_FC_RCVD, "fc_received" },
1567*4882a593Smuzhiyun 	{ MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
1568*4882a593Smuzhiyun 	{ MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
1569*4882a593Smuzhiyun 	{ MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
1570*4882a593Smuzhiyun 	{ MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
1571*4882a593Smuzhiyun 	{ MVPP2_MIB_JABBER_RCVD, "jabber_received" },
1572*4882a593Smuzhiyun 	{ MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
1573*4882a593Smuzhiyun 	{ MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
1574*4882a593Smuzhiyun 	{ MVPP2_MIB_COLLISION, "collision" },
1575*4882a593Smuzhiyun 	{ MVPP2_MIB_LATE_COLLISION, "late_collision" },
1576*4882a593Smuzhiyun };
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun static const struct mvpp2_ethtool_counter mvpp2_ethtool_port_regs[] = {
1579*4882a593Smuzhiyun 	{ MVPP2_OVERRUN_ETH_DROP, "rx_fifo_or_parser_overrun_drops" },
1580*4882a593Smuzhiyun 	{ MVPP2_CLS_ETH_DROP, "rx_classifier_drops" },
1581*4882a593Smuzhiyun };
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun static const struct mvpp2_ethtool_counter mvpp2_ethtool_txq_regs[] = {
1584*4882a593Smuzhiyun 	{ MVPP2_TX_DESC_ENQ_CTR, "txq_%d_desc_enqueue" },
1585*4882a593Smuzhiyun 	{ MVPP2_TX_DESC_ENQ_TO_DDR_CTR, "txq_%d_desc_enqueue_to_ddr" },
1586*4882a593Smuzhiyun 	{ MVPP2_TX_BUFF_ENQ_TO_DDR_CTR, "txq_%d_buff_euqueue_to_ddr" },
1587*4882a593Smuzhiyun 	{ MVPP2_TX_DESC_ENQ_HW_FWD_CTR, "txq_%d_desc_hardware_forwarded" },
1588*4882a593Smuzhiyun 	{ MVPP2_TX_PKTS_DEQ_CTR, "txq_%d_packets_dequeued" },
1589*4882a593Smuzhiyun 	{ MVPP2_TX_PKTS_FULL_QUEUE_DROP_CTR, "txq_%d_queue_full_drops" },
1590*4882a593Smuzhiyun 	{ MVPP2_TX_PKTS_EARLY_DROP_CTR, "txq_%d_packets_early_drops" },
1591*4882a593Smuzhiyun 	{ MVPP2_TX_PKTS_BM_DROP_CTR, "txq_%d_packets_bm_drops" },
1592*4882a593Smuzhiyun 	{ MVPP2_TX_PKTS_BM_MC_DROP_CTR, "txq_%d_packets_rep_bm_drops" },
1593*4882a593Smuzhiyun };
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun static const struct mvpp2_ethtool_counter mvpp2_ethtool_rxq_regs[] = {
1596*4882a593Smuzhiyun 	{ MVPP2_RX_DESC_ENQ_CTR, "rxq_%d_desc_enqueue" },
1597*4882a593Smuzhiyun 	{ MVPP2_RX_PKTS_FULL_QUEUE_DROP_CTR, "rxq_%d_queue_full_drops" },
1598*4882a593Smuzhiyun 	{ MVPP2_RX_PKTS_EARLY_DROP_CTR, "rxq_%d_packets_early_drops" },
1599*4882a593Smuzhiyun 	{ MVPP2_RX_PKTS_BM_DROP_CTR, "rxq_%d_packets_bm_drops" },
1600*4882a593Smuzhiyun };
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun static const struct mvpp2_ethtool_counter mvpp2_ethtool_xdp[] = {
1603*4882a593Smuzhiyun 	{ ETHTOOL_XDP_REDIRECT, "rx_xdp_redirect", },
1604*4882a593Smuzhiyun 	{ ETHTOOL_XDP_PASS, "rx_xdp_pass", },
1605*4882a593Smuzhiyun 	{ ETHTOOL_XDP_DROP, "rx_xdp_drop", },
1606*4882a593Smuzhiyun 	{ ETHTOOL_XDP_TX, "rx_xdp_tx", },
1607*4882a593Smuzhiyun 	{ ETHTOOL_XDP_TX_ERR, "rx_xdp_tx_errors", },
1608*4882a593Smuzhiyun 	{ ETHTOOL_XDP_XMIT, "tx_xdp_xmit", },
1609*4882a593Smuzhiyun 	{ ETHTOOL_XDP_XMIT_ERR, "tx_xdp_xmit_errors", },
1610*4882a593Smuzhiyun };
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun #define MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs)	(ARRAY_SIZE(mvpp2_ethtool_mib_regs) + \
1613*4882a593Smuzhiyun 						 ARRAY_SIZE(mvpp2_ethtool_port_regs) + \
1614*4882a593Smuzhiyun 						 (ARRAY_SIZE(mvpp2_ethtool_txq_regs) * (ntxqs)) + \
1615*4882a593Smuzhiyun 						 (ARRAY_SIZE(mvpp2_ethtool_rxq_regs) * (nrxqs)) + \
1616*4882a593Smuzhiyun 						 ARRAY_SIZE(mvpp2_ethtool_xdp))
1617*4882a593Smuzhiyun 
mvpp2_ethtool_get_strings(struct net_device * netdev,u32 sset,u8 * data)1618*4882a593Smuzhiyun static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
1619*4882a593Smuzhiyun 				      u8 *data)
1620*4882a593Smuzhiyun {
1621*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(netdev);
1622*4882a593Smuzhiyun 	int i, q;
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 	if (sset != ETH_SS_STATS)
1625*4882a593Smuzhiyun 		return;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++) {
1628*4882a593Smuzhiyun 		strscpy(data, mvpp2_ethtool_mib_regs[i].string,
1629*4882a593Smuzhiyun 			ETH_GSTRING_LEN);
1630*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
1631*4882a593Smuzhiyun 	}
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++) {
1634*4882a593Smuzhiyun 		strscpy(data, mvpp2_ethtool_port_regs[i].string,
1635*4882a593Smuzhiyun 			ETH_GSTRING_LEN);
1636*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
1637*4882a593Smuzhiyun 	}
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun 	for (q = 0; q < port->ntxqs; q++) {
1640*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++) {
1641*4882a593Smuzhiyun 			snprintf(data, ETH_GSTRING_LEN,
1642*4882a593Smuzhiyun 				 mvpp2_ethtool_txq_regs[i].string, q);
1643*4882a593Smuzhiyun 			data += ETH_GSTRING_LEN;
1644*4882a593Smuzhiyun 		}
1645*4882a593Smuzhiyun 	}
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 	for (q = 0; q < port->nrxqs; q++) {
1648*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++) {
1649*4882a593Smuzhiyun 			snprintf(data, ETH_GSTRING_LEN,
1650*4882a593Smuzhiyun 				 mvpp2_ethtool_rxq_regs[i].string,
1651*4882a593Smuzhiyun 				 q);
1652*4882a593Smuzhiyun 			data += ETH_GSTRING_LEN;
1653*4882a593Smuzhiyun 		}
1654*4882a593Smuzhiyun 	}
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_xdp); i++) {
1657*4882a593Smuzhiyun 		strscpy(data, mvpp2_ethtool_xdp[i].string,
1658*4882a593Smuzhiyun 			ETH_GSTRING_LEN);
1659*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
1660*4882a593Smuzhiyun 	}
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun static void
mvpp2_get_xdp_stats(struct mvpp2_port * port,struct mvpp2_pcpu_stats * xdp_stats)1664*4882a593Smuzhiyun mvpp2_get_xdp_stats(struct mvpp2_port *port, struct mvpp2_pcpu_stats *xdp_stats)
1665*4882a593Smuzhiyun {
1666*4882a593Smuzhiyun 	unsigned int start;
1667*4882a593Smuzhiyun 	unsigned int cpu;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	/* Gather XDP Statistics */
1670*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
1671*4882a593Smuzhiyun 		struct mvpp2_pcpu_stats *cpu_stats;
1672*4882a593Smuzhiyun 		u64	xdp_redirect;
1673*4882a593Smuzhiyun 		u64	xdp_pass;
1674*4882a593Smuzhiyun 		u64	xdp_drop;
1675*4882a593Smuzhiyun 		u64	xdp_xmit;
1676*4882a593Smuzhiyun 		u64	xdp_xmit_err;
1677*4882a593Smuzhiyun 		u64	xdp_tx;
1678*4882a593Smuzhiyun 		u64	xdp_tx_err;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 		cpu_stats = per_cpu_ptr(port->stats, cpu);
1681*4882a593Smuzhiyun 		do {
1682*4882a593Smuzhiyun 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1683*4882a593Smuzhiyun 			xdp_redirect = cpu_stats->xdp_redirect;
1684*4882a593Smuzhiyun 			xdp_pass   = cpu_stats->xdp_pass;
1685*4882a593Smuzhiyun 			xdp_drop = cpu_stats->xdp_drop;
1686*4882a593Smuzhiyun 			xdp_xmit   = cpu_stats->xdp_xmit;
1687*4882a593Smuzhiyun 			xdp_xmit_err   = cpu_stats->xdp_xmit_err;
1688*4882a593Smuzhiyun 			xdp_tx   = cpu_stats->xdp_tx;
1689*4882a593Smuzhiyun 			xdp_tx_err   = cpu_stats->xdp_tx_err;
1690*4882a593Smuzhiyun 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 		xdp_stats->xdp_redirect += xdp_redirect;
1693*4882a593Smuzhiyun 		xdp_stats->xdp_pass   += xdp_pass;
1694*4882a593Smuzhiyun 		xdp_stats->xdp_drop += xdp_drop;
1695*4882a593Smuzhiyun 		xdp_stats->xdp_xmit   += xdp_xmit;
1696*4882a593Smuzhiyun 		xdp_stats->xdp_xmit_err   += xdp_xmit_err;
1697*4882a593Smuzhiyun 		xdp_stats->xdp_tx   += xdp_tx;
1698*4882a593Smuzhiyun 		xdp_stats->xdp_tx_err   += xdp_tx_err;
1699*4882a593Smuzhiyun 	}
1700*4882a593Smuzhiyun }
1701*4882a593Smuzhiyun 
mvpp2_read_stats(struct mvpp2_port * port)1702*4882a593Smuzhiyun static void mvpp2_read_stats(struct mvpp2_port *port)
1703*4882a593Smuzhiyun {
1704*4882a593Smuzhiyun 	struct mvpp2_pcpu_stats xdp_stats = {};
1705*4882a593Smuzhiyun 	const struct mvpp2_ethtool_counter *s;
1706*4882a593Smuzhiyun 	u64 *pstats;
1707*4882a593Smuzhiyun 	int i, q;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	pstats = port->ethtool_stats;
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_mib_regs); i++)
1712*4882a593Smuzhiyun 		*pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_mib_regs[i]);
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_port_regs); i++)
1715*4882a593Smuzhiyun 		*pstats++ += mvpp2_read(port->priv,
1716*4882a593Smuzhiyun 					mvpp2_ethtool_port_regs[i].offset +
1717*4882a593Smuzhiyun 					4 * port->id);
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	for (q = 0; q < port->ntxqs; q++)
1720*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_txq_regs); i++)
1721*4882a593Smuzhiyun 			*pstats++ += mvpp2_read_index(port->priv,
1722*4882a593Smuzhiyun 						      MVPP22_CTRS_TX_CTR(port->id, q),
1723*4882a593Smuzhiyun 						      mvpp2_ethtool_txq_regs[i].offset);
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	/* Rxqs are numbered from 0 from the user standpoint, but not from the
1726*4882a593Smuzhiyun 	 * driver's. We need to add the  port->first_rxq offset.
1727*4882a593Smuzhiyun 	 */
1728*4882a593Smuzhiyun 	for (q = 0; q < port->nrxqs; q++)
1729*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_rxq_regs); i++)
1730*4882a593Smuzhiyun 			*pstats++ += mvpp2_read_index(port->priv,
1731*4882a593Smuzhiyun 						      port->first_rxq + q,
1732*4882a593Smuzhiyun 						      mvpp2_ethtool_rxq_regs[i].offset);
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 	/* Gather XDP Statistics */
1735*4882a593Smuzhiyun 	mvpp2_get_xdp_stats(port, &xdp_stats);
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	for (i = 0, s = mvpp2_ethtool_xdp;
1738*4882a593Smuzhiyun 		 s < mvpp2_ethtool_xdp + ARRAY_SIZE(mvpp2_ethtool_xdp);
1739*4882a593Smuzhiyun 	     s++, i++) {
1740*4882a593Smuzhiyun 		switch (s->offset) {
1741*4882a593Smuzhiyun 		case ETHTOOL_XDP_REDIRECT:
1742*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_redirect;
1743*4882a593Smuzhiyun 			break;
1744*4882a593Smuzhiyun 		case ETHTOOL_XDP_PASS:
1745*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_pass;
1746*4882a593Smuzhiyun 			break;
1747*4882a593Smuzhiyun 		case ETHTOOL_XDP_DROP:
1748*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_drop;
1749*4882a593Smuzhiyun 			break;
1750*4882a593Smuzhiyun 		case ETHTOOL_XDP_TX:
1751*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_tx;
1752*4882a593Smuzhiyun 			break;
1753*4882a593Smuzhiyun 		case ETHTOOL_XDP_TX_ERR:
1754*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_tx_err;
1755*4882a593Smuzhiyun 			break;
1756*4882a593Smuzhiyun 		case ETHTOOL_XDP_XMIT:
1757*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_xmit;
1758*4882a593Smuzhiyun 			break;
1759*4882a593Smuzhiyun 		case ETHTOOL_XDP_XMIT_ERR:
1760*4882a593Smuzhiyun 			*pstats++ = xdp_stats.xdp_xmit_err;
1761*4882a593Smuzhiyun 			break;
1762*4882a593Smuzhiyun 		}
1763*4882a593Smuzhiyun 	}
1764*4882a593Smuzhiyun }
1765*4882a593Smuzhiyun 
mvpp2_gather_hw_statistics(struct work_struct * work)1766*4882a593Smuzhiyun static void mvpp2_gather_hw_statistics(struct work_struct *work)
1767*4882a593Smuzhiyun {
1768*4882a593Smuzhiyun 	struct delayed_work *del_work = to_delayed_work(work);
1769*4882a593Smuzhiyun 	struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
1770*4882a593Smuzhiyun 					       stats_work);
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	mutex_lock(&port->gather_stats_lock);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	mvpp2_read_stats(port);
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	/* No need to read again the counters right after this function if it
1777*4882a593Smuzhiyun 	 * was called asynchronously by the user (ie. use of ethtool).
1778*4882a593Smuzhiyun 	 */
1779*4882a593Smuzhiyun 	cancel_delayed_work(&port->stats_work);
1780*4882a593Smuzhiyun 	queue_delayed_work(port->priv->stats_queue, &port->stats_work,
1781*4882a593Smuzhiyun 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	mutex_unlock(&port->gather_stats_lock);
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun 
mvpp2_ethtool_get_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)1786*4882a593Smuzhiyun static void mvpp2_ethtool_get_stats(struct net_device *dev,
1787*4882a593Smuzhiyun 				    struct ethtool_stats *stats, u64 *data)
1788*4882a593Smuzhiyun {
1789*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	/* Update statistics for the given port, then take the lock to avoid
1792*4882a593Smuzhiyun 	 * concurrent accesses on the ethtool_stats structure during its copy.
1793*4882a593Smuzhiyun 	 */
1794*4882a593Smuzhiyun 	mvpp2_gather_hw_statistics(&port->stats_work.work);
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	mutex_lock(&port->gather_stats_lock);
1797*4882a593Smuzhiyun 	memcpy(data, port->ethtool_stats,
1798*4882a593Smuzhiyun 	       sizeof(u64) * MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs));
1799*4882a593Smuzhiyun 	mutex_unlock(&port->gather_stats_lock);
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun 
mvpp2_ethtool_get_sset_count(struct net_device * dev,int sset)1802*4882a593Smuzhiyun static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
1803*4882a593Smuzhiyun {
1804*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	if (sset == ETH_SS_STATS)
1807*4882a593Smuzhiyun 		return MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs);
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	return -EOPNOTSUPP;
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun 
mvpp2_mac_reset_assert(struct mvpp2_port * port)1812*4882a593Smuzhiyun static void mvpp2_mac_reset_assert(struct mvpp2_port *port)
1813*4882a593Smuzhiyun {
1814*4882a593Smuzhiyun 	u32 val;
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) |
1817*4882a593Smuzhiyun 	      MVPP2_GMAC_PORT_RESET_MASK;
1818*4882a593Smuzhiyun 	writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP22 && port->gop_id == 0) {
1821*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_CTRL0_REG) &
1822*4882a593Smuzhiyun 		      ~MVPP22_XLG_CTRL0_MAC_RESET_DIS;
1823*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
1824*4882a593Smuzhiyun 	}
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun 
mvpp22_pcs_reset_assert(struct mvpp2_port * port)1827*4882a593Smuzhiyun static void mvpp22_pcs_reset_assert(struct mvpp2_port *port)
1828*4882a593Smuzhiyun {
1829*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1830*4882a593Smuzhiyun 	void __iomem *mpcs, *xpcs;
1831*4882a593Smuzhiyun 	u32 val;
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
1834*4882a593Smuzhiyun 		return;
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1837*4882a593Smuzhiyun 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun 	val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1840*4882a593Smuzhiyun 	val &= ~(MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
1841*4882a593Smuzhiyun 	val |= MVPP22_MPCS_CLK_RESET_DIV_SET;
1842*4882a593Smuzhiyun 	writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 	val = readl(xpcs + MVPP22_XPCS_CFG0);
1845*4882a593Smuzhiyun 	writel(val & ~MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
1846*4882a593Smuzhiyun }
1847*4882a593Smuzhiyun 
mvpp22_pcs_reset_deassert(struct mvpp2_port * port)1848*4882a593Smuzhiyun static void mvpp22_pcs_reset_deassert(struct mvpp2_port *port)
1849*4882a593Smuzhiyun {
1850*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
1851*4882a593Smuzhiyun 	void __iomem *mpcs, *xpcs;
1852*4882a593Smuzhiyun 	u32 val;
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	if (port->priv->hw_version != MVPP22 || port->gop_id != 0)
1855*4882a593Smuzhiyun 		return;
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
1858*4882a593Smuzhiyun 	xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	switch (port->phy_interface) {
1861*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_10GBASER:
1862*4882a593Smuzhiyun 		val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
1863*4882a593Smuzhiyun 		val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX |
1864*4882a593Smuzhiyun 		       MAC_CLK_RESET_SD_TX;
1865*4882a593Smuzhiyun 		val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
1866*4882a593Smuzhiyun 		writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
1867*4882a593Smuzhiyun 		break;
1868*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_XAUI:
1869*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RXAUI:
1870*4882a593Smuzhiyun 		val = readl(xpcs + MVPP22_XPCS_CFG0);
1871*4882a593Smuzhiyun 		writel(val | MVPP22_XPCS_CFG0_RESET_DIS, xpcs + MVPP22_XPCS_CFG0);
1872*4882a593Smuzhiyun 		break;
1873*4882a593Smuzhiyun 	default:
1874*4882a593Smuzhiyun 		break;
1875*4882a593Smuzhiyun 	}
1876*4882a593Smuzhiyun }
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun /* Change maximum receive size of the port */
mvpp2_gmac_max_rx_size_set(struct mvpp2_port * port)1879*4882a593Smuzhiyun static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
1880*4882a593Smuzhiyun {
1881*4882a593Smuzhiyun 	u32 val;
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
1884*4882a593Smuzhiyun 	val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
1885*4882a593Smuzhiyun 	val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
1886*4882a593Smuzhiyun 		    MVPP2_GMAC_MAX_RX_SIZE_OFFS);
1887*4882a593Smuzhiyun 	writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
1888*4882a593Smuzhiyun }
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun /* Change maximum receive size of the port */
mvpp2_xlg_max_rx_size_set(struct mvpp2_port * port)1891*4882a593Smuzhiyun static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun 	u32 val;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	val =  readl(port->base + MVPP22_XLG_CTRL1_REG);
1896*4882a593Smuzhiyun 	val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
1897*4882a593Smuzhiyun 	val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
1898*4882a593Smuzhiyun 	       MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
1899*4882a593Smuzhiyun 	writel(val, port->base + MVPP22_XLG_CTRL1_REG);
1900*4882a593Smuzhiyun }
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun /* Set defaults to the MVPP2 port */
mvpp2_defaults_set(struct mvpp2_port * port)1903*4882a593Smuzhiyun static void mvpp2_defaults_set(struct mvpp2_port *port)
1904*4882a593Smuzhiyun {
1905*4882a593Smuzhiyun 	int tx_port_num, val, queue, lrxq;
1906*4882a593Smuzhiyun 
1907*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21) {
1908*4882a593Smuzhiyun 		/* Update TX FIFO MIN Threshold */
1909*4882a593Smuzhiyun 		val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
1910*4882a593Smuzhiyun 		val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
1911*4882a593Smuzhiyun 		/* Min. TX threshold must be less than minimal packet length */
1912*4882a593Smuzhiyun 		val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
1913*4882a593Smuzhiyun 		writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
1914*4882a593Smuzhiyun 	}
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1917*4882a593Smuzhiyun 	tx_port_num = mvpp2_egress_port(port);
1918*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
1919*4882a593Smuzhiyun 		    tx_port_num);
1920*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	/* Set TXQ scheduling to Round-Robin */
1923*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_FIXED_PRIO_REG, 0);
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	/* Close bandwidth for all queues */
1926*4882a593Smuzhiyun 	for (queue = 0; queue < MVPP2_MAX_TXQ; queue++)
1927*4882a593Smuzhiyun 		mvpp2_write(port->priv,
1928*4882a593Smuzhiyun 			    MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(queue), 0);
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	/* Set refill period to 1 usec, refill tokens
1931*4882a593Smuzhiyun 	 * and bucket size to maximum
1932*4882a593Smuzhiyun 	 */
1933*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
1934*4882a593Smuzhiyun 		    port->priv->tclk / USEC_PER_SEC);
1935*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
1936*4882a593Smuzhiyun 	val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
1937*4882a593Smuzhiyun 	val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
1938*4882a593Smuzhiyun 	val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
1939*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
1940*4882a593Smuzhiyun 	val = MVPP2_TXP_TOKEN_SIZE_MAX;
1941*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	/* Set MaximumLowLatencyPacketSize value to 256 */
1944*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
1945*4882a593Smuzhiyun 		    MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
1946*4882a593Smuzhiyun 		    MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	/* Enable Rx cache snoop */
1949*4882a593Smuzhiyun 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1950*4882a593Smuzhiyun 		queue = port->rxqs[lrxq]->id;
1951*4882a593Smuzhiyun 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1952*4882a593Smuzhiyun 		val |= MVPP2_SNOOP_PKT_SIZE_MASK |
1953*4882a593Smuzhiyun 			   MVPP2_SNOOP_BUF_HDR_MASK;
1954*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1955*4882a593Smuzhiyun 	}
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 	/* At default, mask all interrupts to all present cpus */
1958*4882a593Smuzhiyun 	mvpp2_interrupts_disable(port);
1959*4882a593Smuzhiyun }
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun /* Enable/disable receiving packets */
mvpp2_ingress_enable(struct mvpp2_port * port)1962*4882a593Smuzhiyun static void mvpp2_ingress_enable(struct mvpp2_port *port)
1963*4882a593Smuzhiyun {
1964*4882a593Smuzhiyun 	u32 val;
1965*4882a593Smuzhiyun 	int lrxq, queue;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1968*4882a593Smuzhiyun 		queue = port->rxqs[lrxq]->id;
1969*4882a593Smuzhiyun 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1970*4882a593Smuzhiyun 		val &= ~MVPP2_RXQ_DISABLE_MASK;
1971*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1972*4882a593Smuzhiyun 	}
1973*4882a593Smuzhiyun }
1974*4882a593Smuzhiyun 
mvpp2_ingress_disable(struct mvpp2_port * port)1975*4882a593Smuzhiyun static void mvpp2_ingress_disable(struct mvpp2_port *port)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun 	u32 val;
1978*4882a593Smuzhiyun 	int lrxq, queue;
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
1981*4882a593Smuzhiyun 		queue = port->rxqs[lrxq]->id;
1982*4882a593Smuzhiyun 		val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
1983*4882a593Smuzhiyun 		val |= MVPP2_RXQ_DISABLE_MASK;
1984*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
1985*4882a593Smuzhiyun 	}
1986*4882a593Smuzhiyun }
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun /* Enable transmit via physical egress queue
1989*4882a593Smuzhiyun  * - HW starts take descriptors from DRAM
1990*4882a593Smuzhiyun  */
mvpp2_egress_enable(struct mvpp2_port * port)1991*4882a593Smuzhiyun static void mvpp2_egress_enable(struct mvpp2_port *port)
1992*4882a593Smuzhiyun {
1993*4882a593Smuzhiyun 	u32 qmap;
1994*4882a593Smuzhiyun 	int queue;
1995*4882a593Smuzhiyun 	int tx_port_num = mvpp2_egress_port(port);
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 	/* Enable all initialized TXs. */
1998*4882a593Smuzhiyun 	qmap = 0;
1999*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
2000*4882a593Smuzhiyun 		struct mvpp2_tx_queue *txq = port->txqs[queue];
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 		if (txq->descs)
2003*4882a593Smuzhiyun 			qmap |= (1 << queue);
2004*4882a593Smuzhiyun 	}
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2007*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
2008*4882a593Smuzhiyun }
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun /* Disable transmit via physical egress queue
2011*4882a593Smuzhiyun  * - HW doesn't take descriptors from DRAM
2012*4882a593Smuzhiyun  */
mvpp2_egress_disable(struct mvpp2_port * port)2013*4882a593Smuzhiyun static void mvpp2_egress_disable(struct mvpp2_port *port)
2014*4882a593Smuzhiyun {
2015*4882a593Smuzhiyun 	u32 reg_data;
2016*4882a593Smuzhiyun 	int delay;
2017*4882a593Smuzhiyun 	int tx_port_num = mvpp2_egress_port(port);
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 	/* Issue stop command for active channels only */
2020*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2021*4882a593Smuzhiyun 	reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
2022*4882a593Smuzhiyun 		    MVPP2_TXP_SCHED_ENQ_MASK;
2023*4882a593Smuzhiyun 	if (reg_data != 0)
2024*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
2025*4882a593Smuzhiyun 			    (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 	/* Wait for all Tx activity to terminate. */
2028*4882a593Smuzhiyun 	delay = 0;
2029*4882a593Smuzhiyun 	do {
2030*4882a593Smuzhiyun 		if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
2031*4882a593Smuzhiyun 			netdev_warn(port->dev,
2032*4882a593Smuzhiyun 				    "Tx stop timed out, status=0x%08x\n",
2033*4882a593Smuzhiyun 				    reg_data);
2034*4882a593Smuzhiyun 			break;
2035*4882a593Smuzhiyun 		}
2036*4882a593Smuzhiyun 		mdelay(1);
2037*4882a593Smuzhiyun 		delay++;
2038*4882a593Smuzhiyun 
2039*4882a593Smuzhiyun 		/* Check port TX Command register that all
2040*4882a593Smuzhiyun 		 * Tx queues are stopped
2041*4882a593Smuzhiyun 		 */
2042*4882a593Smuzhiyun 		reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
2043*4882a593Smuzhiyun 	} while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
2044*4882a593Smuzhiyun }
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun /* Rx descriptors helper methods */
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun /* Get number of Rx descriptors occupied by received packets */
2049*4882a593Smuzhiyun static inline int
mvpp2_rxq_received(struct mvpp2_port * port,int rxq_id)2050*4882a593Smuzhiyun mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
2051*4882a593Smuzhiyun {
2052*4882a593Smuzhiyun 	u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	return val & MVPP2_RXQ_OCCUPIED_MASK;
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun /* Update Rx queue status with the number of occupied and available
2058*4882a593Smuzhiyun  * Rx descriptor slots.
2059*4882a593Smuzhiyun  */
2060*4882a593Smuzhiyun static inline void
mvpp2_rxq_status_update(struct mvpp2_port * port,int rxq_id,int used_count,int free_count)2061*4882a593Smuzhiyun mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
2062*4882a593Smuzhiyun 			int used_count, int free_count)
2063*4882a593Smuzhiyun {
2064*4882a593Smuzhiyun 	/* Decrement the number of used descriptors and increment count
2065*4882a593Smuzhiyun 	 * increment the number of free descriptors.
2066*4882a593Smuzhiyun 	 */
2067*4882a593Smuzhiyun 	u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun /* Get pointer to next RX descriptor to be processed by SW */
2073*4882a593Smuzhiyun static inline struct mvpp2_rx_desc *
mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue * rxq)2074*4882a593Smuzhiyun mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
2075*4882a593Smuzhiyun {
2076*4882a593Smuzhiyun 	int rx_desc = rxq->next_desc_to_proc;
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
2079*4882a593Smuzhiyun 	prefetch(rxq->descs + rxq->next_desc_to_proc);
2080*4882a593Smuzhiyun 	return rxq->descs + rx_desc;
2081*4882a593Smuzhiyun }
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun /* Set rx queue offset */
mvpp2_rxq_offset_set(struct mvpp2_port * port,int prxq,int offset)2084*4882a593Smuzhiyun static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
2085*4882a593Smuzhiyun 				 int prxq, int offset)
2086*4882a593Smuzhiyun {
2087*4882a593Smuzhiyun 	u32 val;
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun 	/* Convert offset from bytes to units of 32 bytes */
2090*4882a593Smuzhiyun 	offset = offset >> 5;
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
2093*4882a593Smuzhiyun 	val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 	/* Offset is in */
2096*4882a593Smuzhiyun 	val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
2097*4882a593Smuzhiyun 		    MVPP2_RXQ_PACKET_OFFSET_MASK);
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
2100*4882a593Smuzhiyun }
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun /* Tx descriptors helper methods */
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun /* Get pointer to next Tx descriptor to be processed (send) by HW */
2105*4882a593Smuzhiyun static struct mvpp2_tx_desc *
mvpp2_txq_next_desc_get(struct mvpp2_tx_queue * txq)2106*4882a593Smuzhiyun mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
2107*4882a593Smuzhiyun {
2108*4882a593Smuzhiyun 	int tx_desc = txq->next_desc_to_proc;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
2111*4882a593Smuzhiyun 	return txq->descs + tx_desc;
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun /* Update HW with number of aggregated Tx descriptors to be sent
2115*4882a593Smuzhiyun  *
2116*4882a593Smuzhiyun  * Called only from mvpp2_tx(), so migration is disabled, using
2117*4882a593Smuzhiyun  * smp_processor_id() is OK.
2118*4882a593Smuzhiyun  */
mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port * port,int pending)2119*4882a593Smuzhiyun static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
2120*4882a593Smuzhiyun {
2121*4882a593Smuzhiyun 	/* aggregated access - relevant TXQ number is written in TX desc */
2122*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv,
2123*4882a593Smuzhiyun 			   mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2124*4882a593Smuzhiyun 			   MVPP2_AGGR_TXQ_UPDATE_REG, pending);
2125*4882a593Smuzhiyun }
2126*4882a593Smuzhiyun 
2127*4882a593Smuzhiyun /* Check if there are enough free descriptors in aggregated txq.
2128*4882a593Smuzhiyun  * If not, update the number of occupied descriptors and repeat the check.
2129*4882a593Smuzhiyun  *
2130*4882a593Smuzhiyun  * Called only from mvpp2_tx(), so migration is disabled, using
2131*4882a593Smuzhiyun  * smp_processor_id() is OK.
2132*4882a593Smuzhiyun  */
mvpp2_aggr_desc_num_check(struct mvpp2_port * port,struct mvpp2_tx_queue * aggr_txq,int num)2133*4882a593Smuzhiyun static int mvpp2_aggr_desc_num_check(struct mvpp2_port *port,
2134*4882a593Smuzhiyun 				     struct mvpp2_tx_queue *aggr_txq, int num)
2135*4882a593Smuzhiyun {
2136*4882a593Smuzhiyun 	if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
2137*4882a593Smuzhiyun 		/* Update number of occupied aggregated Tx descriptors */
2138*4882a593Smuzhiyun 		unsigned int thread =
2139*4882a593Smuzhiyun 			mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2140*4882a593Smuzhiyun 		u32 val = mvpp2_read_relaxed(port->priv,
2141*4882a593Smuzhiyun 					     MVPP2_AGGR_TXQ_STATUS_REG(thread));
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 		aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 		if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
2146*4882a593Smuzhiyun 			return -ENOMEM;
2147*4882a593Smuzhiyun 	}
2148*4882a593Smuzhiyun 	return 0;
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun /* Reserved Tx descriptors allocation request
2152*4882a593Smuzhiyun  *
2153*4882a593Smuzhiyun  * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
2154*4882a593Smuzhiyun  * only by mvpp2_tx(), so migration is disabled, using
2155*4882a593Smuzhiyun  * smp_processor_id() is OK.
2156*4882a593Smuzhiyun  */
mvpp2_txq_alloc_reserved_desc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,int num)2157*4882a593Smuzhiyun static int mvpp2_txq_alloc_reserved_desc(struct mvpp2_port *port,
2158*4882a593Smuzhiyun 					 struct mvpp2_tx_queue *txq, int num)
2159*4882a593Smuzhiyun {
2160*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
2161*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2162*4882a593Smuzhiyun 	u32 val;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
2165*4882a593Smuzhiyun 	mvpp2_thread_write_relaxed(priv, thread, MVPP2_TXQ_RSVD_REQ_REG, val);
2166*4882a593Smuzhiyun 
2167*4882a593Smuzhiyun 	val = mvpp2_thread_read_relaxed(priv, thread, MVPP2_TXQ_RSVD_RSLT_REG);
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	return val & MVPP2_TXQ_RSVD_RSLT_MASK;
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun /* Check if there are enough reserved descriptors for transmission.
2173*4882a593Smuzhiyun  * If not, request chunk of reserved descriptors and check again.
2174*4882a593Smuzhiyun  */
mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu,int num)2175*4882a593Smuzhiyun static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2_port *port,
2176*4882a593Smuzhiyun 					    struct mvpp2_tx_queue *txq,
2177*4882a593Smuzhiyun 					    struct mvpp2_txq_pcpu *txq_pcpu,
2178*4882a593Smuzhiyun 					    int num)
2179*4882a593Smuzhiyun {
2180*4882a593Smuzhiyun 	int req, desc_count;
2181*4882a593Smuzhiyun 	unsigned int thread;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	if (txq_pcpu->reserved_num >= num)
2184*4882a593Smuzhiyun 		return 0;
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	/* Not enough descriptors reserved! Update the reserved descriptor
2187*4882a593Smuzhiyun 	 * count and check again.
2188*4882a593Smuzhiyun 	 */
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun 	desc_count = 0;
2191*4882a593Smuzhiyun 	/* Compute total of used descriptors */
2192*4882a593Smuzhiyun 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2193*4882a593Smuzhiyun 		struct mvpp2_txq_pcpu *txq_pcpu_aux;
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 		txq_pcpu_aux = per_cpu_ptr(txq->pcpu, thread);
2196*4882a593Smuzhiyun 		desc_count += txq_pcpu_aux->count;
2197*4882a593Smuzhiyun 		desc_count += txq_pcpu_aux->reserved_num;
2198*4882a593Smuzhiyun 	}
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun 	req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
2201*4882a593Smuzhiyun 	desc_count += req;
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 	if (desc_count >
2204*4882a593Smuzhiyun 	   (txq->size - (MVPP2_MAX_THREADS * MVPP2_CPU_DESC_CHUNK)))
2205*4882a593Smuzhiyun 		return -ENOMEM;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(port, txq, req);
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	/* OK, the descriptor could have been updated: check again. */
2210*4882a593Smuzhiyun 	if (txq_pcpu->reserved_num < num)
2211*4882a593Smuzhiyun 		return -ENOMEM;
2212*4882a593Smuzhiyun 	return 0;
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun /* Release the last allocated Tx descriptor. Useful to handle DMA
2216*4882a593Smuzhiyun  * mapping failures in the Tx path.
2217*4882a593Smuzhiyun  */
mvpp2_txq_desc_put(struct mvpp2_tx_queue * txq)2218*4882a593Smuzhiyun static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
2219*4882a593Smuzhiyun {
2220*4882a593Smuzhiyun 	if (txq->next_desc_to_proc == 0)
2221*4882a593Smuzhiyun 		txq->next_desc_to_proc = txq->last_desc - 1;
2222*4882a593Smuzhiyun 	else
2223*4882a593Smuzhiyun 		txq->next_desc_to_proc--;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun /* Set Tx descriptors fields relevant for CSUM calculation */
mvpp2_txq_desc_csum(int l3_offs,__be16 l3_proto,int ip_hdr_len,int l4_proto)2227*4882a593Smuzhiyun static u32 mvpp2_txq_desc_csum(int l3_offs, __be16 l3_proto,
2228*4882a593Smuzhiyun 			       int ip_hdr_len, int l4_proto)
2229*4882a593Smuzhiyun {
2230*4882a593Smuzhiyun 	u32 command;
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun 	/* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
2233*4882a593Smuzhiyun 	 * G_L4_chk, L4_type required only for checksum calculation
2234*4882a593Smuzhiyun 	 */
2235*4882a593Smuzhiyun 	command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
2236*4882a593Smuzhiyun 	command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
2237*4882a593Smuzhiyun 	command |= MVPP2_TXD_IP_CSUM_DISABLE;
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 	if (l3_proto == htons(ETH_P_IP)) {
2240*4882a593Smuzhiyun 		command &= ~MVPP2_TXD_IP_CSUM_DISABLE;	/* enable IPv4 csum */
2241*4882a593Smuzhiyun 		command &= ~MVPP2_TXD_L3_IP6;		/* enable IPv4 */
2242*4882a593Smuzhiyun 	} else {
2243*4882a593Smuzhiyun 		command |= MVPP2_TXD_L3_IP6;		/* enable IPv6 */
2244*4882a593Smuzhiyun 	}
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	if (l4_proto == IPPROTO_TCP) {
2247*4882a593Smuzhiyun 		command &= ~MVPP2_TXD_L4_UDP;		/* enable TCP */
2248*4882a593Smuzhiyun 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
2249*4882a593Smuzhiyun 	} else if (l4_proto == IPPROTO_UDP) {
2250*4882a593Smuzhiyun 		command |= MVPP2_TXD_L4_UDP;		/* enable UDP */
2251*4882a593Smuzhiyun 		command &= ~MVPP2_TXD_L4_CSUM_FRAG;	/* generate L4 csum */
2252*4882a593Smuzhiyun 	} else {
2253*4882a593Smuzhiyun 		command |= MVPP2_TXD_L4_CSUM_NOT;
2254*4882a593Smuzhiyun 	}
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	return command;
2257*4882a593Smuzhiyun }
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun /* Get number of sent descriptors and decrement counter.
2260*4882a593Smuzhiyun  * The number of sent descriptors is returned.
2261*4882a593Smuzhiyun  * Per-thread access
2262*4882a593Smuzhiyun  *
2263*4882a593Smuzhiyun  * Called only from mvpp2_txq_done(), called from mvpp2_tx()
2264*4882a593Smuzhiyun  * (migration disabled) and from the TX completion tasklet (migration
2265*4882a593Smuzhiyun  * disabled) so using smp_processor_id() is OK.
2266*4882a593Smuzhiyun  */
mvpp2_txq_sent_desc_proc(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2267*4882a593Smuzhiyun static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
2268*4882a593Smuzhiyun 					   struct mvpp2_tx_queue *txq)
2269*4882a593Smuzhiyun {
2270*4882a593Smuzhiyun 	u32 val;
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	/* Reading status reg resets transmitted descriptor counter */
2273*4882a593Smuzhiyun 	val = mvpp2_thread_read_relaxed(port->priv,
2274*4882a593Smuzhiyun 					mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2275*4882a593Smuzhiyun 					MVPP2_TXQ_SENT_REG(txq->id));
2276*4882a593Smuzhiyun 
2277*4882a593Smuzhiyun 	return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
2278*4882a593Smuzhiyun 		MVPP2_TRANSMITTED_COUNT_OFFSET;
2279*4882a593Smuzhiyun }
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun /* Called through on_each_cpu(), so runs on all CPUs, with migration
2282*4882a593Smuzhiyun  * disabled, therefore using smp_processor_id() is OK.
2283*4882a593Smuzhiyun  */
mvpp2_txq_sent_counter_clear(void * arg)2284*4882a593Smuzhiyun static void mvpp2_txq_sent_counter_clear(void *arg)
2285*4882a593Smuzhiyun {
2286*4882a593Smuzhiyun 	struct mvpp2_port *port = arg;
2287*4882a593Smuzhiyun 	int queue;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	/* If the thread isn't used, don't do anything */
2290*4882a593Smuzhiyun 	if (smp_processor_id() >= port->priv->nthreads)
2291*4882a593Smuzhiyun 		return;
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
2294*4882a593Smuzhiyun 		int id = port->txqs[queue]->id;
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun 		mvpp2_thread_read(port->priv,
2297*4882a593Smuzhiyun 				  mvpp2_cpu_to_thread(port->priv, smp_processor_id()),
2298*4882a593Smuzhiyun 				  MVPP2_TXQ_SENT_REG(id));
2299*4882a593Smuzhiyun 	}
2300*4882a593Smuzhiyun }
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun /* Set max sizes for Tx queues */
mvpp2_txp_max_tx_size_set(struct mvpp2_port * port)2303*4882a593Smuzhiyun static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
2304*4882a593Smuzhiyun {
2305*4882a593Smuzhiyun 	u32	val, size, mtu;
2306*4882a593Smuzhiyun 	int	txq, tx_port_num;
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun 	mtu = port->pkt_size * 8;
2309*4882a593Smuzhiyun 	if (mtu > MVPP2_TXP_MTU_MAX)
2310*4882a593Smuzhiyun 		mtu = MVPP2_TXP_MTU_MAX;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 	/* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
2313*4882a593Smuzhiyun 	mtu = 3 * mtu;
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun 	/* Indirect access to registers */
2316*4882a593Smuzhiyun 	tx_port_num = mvpp2_egress_port(port);
2317*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2318*4882a593Smuzhiyun 
2319*4882a593Smuzhiyun 	/* Set MTU */
2320*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
2321*4882a593Smuzhiyun 	val &= ~MVPP2_TXP_MTU_MAX;
2322*4882a593Smuzhiyun 	val |= mtu;
2323*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	/* TXP token size and all TXQs token size must be larger that MTU */
2326*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
2327*4882a593Smuzhiyun 	size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
2328*4882a593Smuzhiyun 	if (size < mtu) {
2329*4882a593Smuzhiyun 		size = mtu;
2330*4882a593Smuzhiyun 		val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
2331*4882a593Smuzhiyun 		val |= size;
2332*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
2333*4882a593Smuzhiyun 	}
2334*4882a593Smuzhiyun 
2335*4882a593Smuzhiyun 	for (txq = 0; txq < port->ntxqs; txq++) {
2336*4882a593Smuzhiyun 		val = mvpp2_read(port->priv,
2337*4882a593Smuzhiyun 				 MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
2338*4882a593Smuzhiyun 		size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun 		if (size < mtu) {
2341*4882a593Smuzhiyun 			size = mtu;
2342*4882a593Smuzhiyun 			val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
2343*4882a593Smuzhiyun 			val |= size;
2344*4882a593Smuzhiyun 			mvpp2_write(port->priv,
2345*4882a593Smuzhiyun 				    MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
2346*4882a593Smuzhiyun 				    val);
2347*4882a593Smuzhiyun 		}
2348*4882a593Smuzhiyun 	}
2349*4882a593Smuzhiyun }
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun /* Set the number of packets that will be received before Rx interrupt
2352*4882a593Smuzhiyun  * will be generated by HW.
2353*4882a593Smuzhiyun  */
mvpp2_rx_pkts_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2354*4882a593Smuzhiyun static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
2355*4882a593Smuzhiyun 				   struct mvpp2_rx_queue *rxq)
2356*4882a593Smuzhiyun {
2357*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
2360*4882a593Smuzhiyun 		rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2363*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_THRESH_REG,
2364*4882a593Smuzhiyun 			   rxq->pkts_coal);
2365*4882a593Smuzhiyun 
2366*4882a593Smuzhiyun 	put_cpu();
2367*4882a593Smuzhiyun }
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun /* For some reason in the LSP this is done on each CPU. Why ? */
mvpp2_tx_pkts_coal_set(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2370*4882a593Smuzhiyun static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
2371*4882a593Smuzhiyun 				   struct mvpp2_tx_queue *txq)
2372*4882a593Smuzhiyun {
2373*4882a593Smuzhiyun 	unsigned int thread;
2374*4882a593Smuzhiyun 	u32 val;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
2377*4882a593Smuzhiyun 		txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
2378*4882a593Smuzhiyun 
2379*4882a593Smuzhiyun 	val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
2380*4882a593Smuzhiyun 	/* PKT-coalescing registers are per-queue + per-thread */
2381*4882a593Smuzhiyun 	for (thread = 0; thread < MVPP2_MAX_THREADS; thread++) {
2382*4882a593Smuzhiyun 		mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2383*4882a593Smuzhiyun 		mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_THRESH_REG, val);
2384*4882a593Smuzhiyun 	}
2385*4882a593Smuzhiyun }
2386*4882a593Smuzhiyun 
mvpp2_usec_to_cycles(u32 usec,unsigned long clk_hz)2387*4882a593Smuzhiyun static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
2388*4882a593Smuzhiyun {
2389*4882a593Smuzhiyun 	u64 tmp = (u64)clk_hz * usec;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 	do_div(tmp, USEC_PER_SEC);
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 	return tmp > U32_MAX ? U32_MAX : tmp;
2394*4882a593Smuzhiyun }
2395*4882a593Smuzhiyun 
mvpp2_cycles_to_usec(u32 cycles,unsigned long clk_hz)2396*4882a593Smuzhiyun static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
2397*4882a593Smuzhiyun {
2398*4882a593Smuzhiyun 	u64 tmp = (u64)cycles * USEC_PER_SEC;
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 	do_div(tmp, clk_hz);
2401*4882a593Smuzhiyun 
2402*4882a593Smuzhiyun 	return tmp > U32_MAX ? U32_MAX : tmp;
2403*4882a593Smuzhiyun }
2404*4882a593Smuzhiyun 
2405*4882a593Smuzhiyun /* Set the time delay in usec before Rx interrupt */
mvpp2_rx_time_coal_set(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2406*4882a593Smuzhiyun static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
2407*4882a593Smuzhiyun 				   struct mvpp2_rx_queue *rxq)
2408*4882a593Smuzhiyun {
2409*4882a593Smuzhiyun 	unsigned long freq = port->priv->tclk;
2410*4882a593Smuzhiyun 	u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2411*4882a593Smuzhiyun 
2412*4882a593Smuzhiyun 	if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
2413*4882a593Smuzhiyun 		rxq->time_coal =
2414*4882a593Smuzhiyun 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun 		/* re-evaluate to get actual register value */
2417*4882a593Smuzhiyun 		val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
2418*4882a593Smuzhiyun 	}
2419*4882a593Smuzhiyun 
2420*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
2421*4882a593Smuzhiyun }
2422*4882a593Smuzhiyun 
mvpp2_tx_time_coal_set(struct mvpp2_port * port)2423*4882a593Smuzhiyun static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
2424*4882a593Smuzhiyun {
2425*4882a593Smuzhiyun 	unsigned long freq = port->priv->tclk;
2426*4882a593Smuzhiyun 	u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 	if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
2429*4882a593Smuzhiyun 		port->tx_time_coal =
2430*4882a593Smuzhiyun 			mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 		/* re-evaluate to get actual register value */
2433*4882a593Smuzhiyun 		val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
2434*4882a593Smuzhiyun 	}
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
2437*4882a593Smuzhiyun }
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun /* Free Tx queue skbuffs */
mvpp2_txq_bufs_free(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu,int num)2440*4882a593Smuzhiyun static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
2441*4882a593Smuzhiyun 				struct mvpp2_tx_queue *txq,
2442*4882a593Smuzhiyun 				struct mvpp2_txq_pcpu *txq_pcpu, int num)
2443*4882a593Smuzhiyun {
2444*4882a593Smuzhiyun 	int i;
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 	for (i = 0; i < num; i++) {
2447*4882a593Smuzhiyun 		struct mvpp2_txq_pcpu_buf *tx_buf =
2448*4882a593Smuzhiyun 			txq_pcpu->buffs + txq_pcpu->txq_get_index;
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 		if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma) &&
2451*4882a593Smuzhiyun 		    tx_buf->type != MVPP2_TYPE_XDP_TX)
2452*4882a593Smuzhiyun 			dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
2453*4882a593Smuzhiyun 					 tx_buf->size, DMA_TO_DEVICE);
2454*4882a593Smuzhiyun 		if (tx_buf->type == MVPP2_TYPE_SKB && tx_buf->skb)
2455*4882a593Smuzhiyun 			dev_kfree_skb_any(tx_buf->skb);
2456*4882a593Smuzhiyun 		else if (tx_buf->type == MVPP2_TYPE_XDP_TX ||
2457*4882a593Smuzhiyun 			 tx_buf->type == MVPP2_TYPE_XDP_NDO)
2458*4882a593Smuzhiyun 			xdp_return_frame(tx_buf->xdpf);
2459*4882a593Smuzhiyun 
2460*4882a593Smuzhiyun 		mvpp2_txq_inc_get(txq_pcpu);
2461*4882a593Smuzhiyun 	}
2462*4882a593Smuzhiyun }
2463*4882a593Smuzhiyun 
mvpp2_get_rx_queue(struct mvpp2_port * port,u32 cause)2464*4882a593Smuzhiyun static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
2465*4882a593Smuzhiyun 							u32 cause)
2466*4882a593Smuzhiyun {
2467*4882a593Smuzhiyun 	int queue = fls(cause) - 1;
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun 	return port->rxqs[queue];
2470*4882a593Smuzhiyun }
2471*4882a593Smuzhiyun 
mvpp2_get_tx_queue(struct mvpp2_port * port,u32 cause)2472*4882a593Smuzhiyun static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
2473*4882a593Smuzhiyun 							u32 cause)
2474*4882a593Smuzhiyun {
2475*4882a593Smuzhiyun 	int queue = fls(cause) - 1;
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 	return port->txqs[queue];
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun 
2480*4882a593Smuzhiyun /* Handle end of transmission */
mvpp2_txq_done(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_txq_pcpu * txq_pcpu)2481*4882a593Smuzhiyun static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
2482*4882a593Smuzhiyun 			   struct mvpp2_txq_pcpu *txq_pcpu)
2483*4882a593Smuzhiyun {
2484*4882a593Smuzhiyun 	struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
2485*4882a593Smuzhiyun 	int tx_done;
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun 	if (txq_pcpu->thread != mvpp2_cpu_to_thread(port->priv, smp_processor_id()))
2488*4882a593Smuzhiyun 		netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
2489*4882a593Smuzhiyun 
2490*4882a593Smuzhiyun 	tx_done = mvpp2_txq_sent_desc_proc(port, txq);
2491*4882a593Smuzhiyun 	if (!tx_done)
2492*4882a593Smuzhiyun 		return;
2493*4882a593Smuzhiyun 	mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
2494*4882a593Smuzhiyun 
2495*4882a593Smuzhiyun 	txq_pcpu->count -= tx_done;
2496*4882a593Smuzhiyun 
2497*4882a593Smuzhiyun 	if (netif_tx_queue_stopped(nq))
2498*4882a593Smuzhiyun 		if (txq_pcpu->count <= txq_pcpu->wake_threshold)
2499*4882a593Smuzhiyun 			netif_tx_wake_queue(nq);
2500*4882a593Smuzhiyun }
2501*4882a593Smuzhiyun 
mvpp2_tx_done(struct mvpp2_port * port,u32 cause,unsigned int thread)2502*4882a593Smuzhiyun static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
2503*4882a593Smuzhiyun 				  unsigned int thread)
2504*4882a593Smuzhiyun {
2505*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq;
2506*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
2507*4882a593Smuzhiyun 	unsigned int tx_todo = 0;
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 	while (cause) {
2510*4882a593Smuzhiyun 		txq = mvpp2_get_tx_queue(port, cause);
2511*4882a593Smuzhiyun 		if (!txq)
2512*4882a593Smuzhiyun 			break;
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2515*4882a593Smuzhiyun 
2516*4882a593Smuzhiyun 		if (txq_pcpu->count) {
2517*4882a593Smuzhiyun 			mvpp2_txq_done(port, txq, txq_pcpu);
2518*4882a593Smuzhiyun 			tx_todo += txq_pcpu->count;
2519*4882a593Smuzhiyun 		}
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun 		cause &= ~(1 << txq->log_id);
2522*4882a593Smuzhiyun 	}
2523*4882a593Smuzhiyun 	return tx_todo;
2524*4882a593Smuzhiyun }
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun /* Rx/Tx queue initialization/cleanup methods */
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun /* Allocate and initialize descriptors for aggr TXQ */
mvpp2_aggr_txq_init(struct platform_device * pdev,struct mvpp2_tx_queue * aggr_txq,unsigned int thread,struct mvpp2 * priv)2529*4882a593Smuzhiyun static int mvpp2_aggr_txq_init(struct platform_device *pdev,
2530*4882a593Smuzhiyun 			       struct mvpp2_tx_queue *aggr_txq,
2531*4882a593Smuzhiyun 			       unsigned int thread, struct mvpp2 *priv)
2532*4882a593Smuzhiyun {
2533*4882a593Smuzhiyun 	u32 txq_dma;
2534*4882a593Smuzhiyun 
2535*4882a593Smuzhiyun 	/* Allocate memory for TX descriptors */
2536*4882a593Smuzhiyun 	aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
2537*4882a593Smuzhiyun 					     MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
2538*4882a593Smuzhiyun 					     &aggr_txq->descs_dma, GFP_KERNEL);
2539*4882a593Smuzhiyun 	if (!aggr_txq->descs)
2540*4882a593Smuzhiyun 		return -ENOMEM;
2541*4882a593Smuzhiyun 
2542*4882a593Smuzhiyun 	aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	/* Aggr TXQ no reset WA */
2545*4882a593Smuzhiyun 	aggr_txq->next_desc_to_proc = mvpp2_read(priv,
2546*4882a593Smuzhiyun 						 MVPP2_AGGR_TXQ_INDEX_REG(thread));
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 	/* Set Tx descriptors queue starting address indirect
2549*4882a593Smuzhiyun 	 * access
2550*4882a593Smuzhiyun 	 */
2551*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
2552*4882a593Smuzhiyun 		txq_dma = aggr_txq->descs_dma;
2553*4882a593Smuzhiyun 	else
2554*4882a593Smuzhiyun 		txq_dma = aggr_txq->descs_dma >>
2555*4882a593Smuzhiyun 			MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
2556*4882a593Smuzhiyun 
2557*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(thread), txq_dma);
2558*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(thread),
2559*4882a593Smuzhiyun 		    MVPP2_AGGR_TXQ_SIZE);
2560*4882a593Smuzhiyun 
2561*4882a593Smuzhiyun 	return 0;
2562*4882a593Smuzhiyun }
2563*4882a593Smuzhiyun 
2564*4882a593Smuzhiyun /* Create a specified Rx queue */
mvpp2_rxq_init(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2565*4882a593Smuzhiyun static int mvpp2_rxq_init(struct mvpp2_port *port,
2566*4882a593Smuzhiyun 			  struct mvpp2_rx_queue *rxq)
2567*4882a593Smuzhiyun {
2568*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
2569*4882a593Smuzhiyun 	unsigned int thread;
2570*4882a593Smuzhiyun 	u32 rxq_dma;
2571*4882a593Smuzhiyun 	int err;
2572*4882a593Smuzhiyun 
2573*4882a593Smuzhiyun 	rxq->size = port->rx_ring_size;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	/* Allocate memory for RX descriptors */
2576*4882a593Smuzhiyun 	rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
2577*4882a593Smuzhiyun 					rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2578*4882a593Smuzhiyun 					&rxq->descs_dma, GFP_KERNEL);
2579*4882a593Smuzhiyun 	if (!rxq->descs)
2580*4882a593Smuzhiyun 		return -ENOMEM;
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun 	rxq->last_desc = rxq->size - 1;
2583*4882a593Smuzhiyun 
2584*4882a593Smuzhiyun 	/* Zero occupied and non-occupied counters - direct access */
2585*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun 	/* Set Rx descriptors queue starting address - indirect access */
2588*4882a593Smuzhiyun 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2589*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2590*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21)
2591*4882a593Smuzhiyun 		rxq_dma = rxq->descs_dma;
2592*4882a593Smuzhiyun 	else
2593*4882a593Smuzhiyun 		rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
2594*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
2595*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
2596*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_INDEX_REG, 0);
2597*4882a593Smuzhiyun 	put_cpu();
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 	/* Set Offset */
2600*4882a593Smuzhiyun 	mvpp2_rxq_offset_set(port, rxq->id, MVPP2_SKB_HEADROOM);
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun 	/* Set coalescing pkts and time */
2603*4882a593Smuzhiyun 	mvpp2_rx_pkts_coal_set(port, rxq);
2604*4882a593Smuzhiyun 	mvpp2_rx_time_coal_set(port, rxq);
2605*4882a593Smuzhiyun 
2606*4882a593Smuzhiyun 	/* Add number of descriptors ready for receiving packets */
2607*4882a593Smuzhiyun 	mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
2608*4882a593Smuzhiyun 
2609*4882a593Smuzhiyun 	if (priv->percpu_pools) {
2610*4882a593Smuzhiyun 		err = xdp_rxq_info_reg(&rxq->xdp_rxq_short, port->dev, rxq->logic_rxq);
2611*4882a593Smuzhiyun 		if (err < 0)
2612*4882a593Smuzhiyun 			goto err_free_dma;
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 		err = xdp_rxq_info_reg(&rxq->xdp_rxq_long, port->dev, rxq->logic_rxq);
2615*4882a593Smuzhiyun 		if (err < 0)
2616*4882a593Smuzhiyun 			goto err_unregister_rxq_short;
2617*4882a593Smuzhiyun 
2618*4882a593Smuzhiyun 		/* Every RXQ has a pool for short and another for long packets */
2619*4882a593Smuzhiyun 		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_short,
2620*4882a593Smuzhiyun 						 MEM_TYPE_PAGE_POOL,
2621*4882a593Smuzhiyun 						 priv->page_pool[rxq->logic_rxq]);
2622*4882a593Smuzhiyun 		if (err < 0)
2623*4882a593Smuzhiyun 			goto err_unregister_rxq_long;
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun 		err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq_long,
2626*4882a593Smuzhiyun 						 MEM_TYPE_PAGE_POOL,
2627*4882a593Smuzhiyun 						 priv->page_pool[rxq->logic_rxq +
2628*4882a593Smuzhiyun 								 port->nrxqs]);
2629*4882a593Smuzhiyun 		if (err < 0)
2630*4882a593Smuzhiyun 			goto err_unregister_mem_rxq_short;
2631*4882a593Smuzhiyun 	}
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun 	return 0;
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun err_unregister_mem_rxq_short:
2636*4882a593Smuzhiyun 	xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq_short);
2637*4882a593Smuzhiyun err_unregister_rxq_long:
2638*4882a593Smuzhiyun 	xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
2639*4882a593Smuzhiyun err_unregister_rxq_short:
2640*4882a593Smuzhiyun 	xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
2641*4882a593Smuzhiyun err_free_dma:
2642*4882a593Smuzhiyun 	dma_free_coherent(port->dev->dev.parent,
2643*4882a593Smuzhiyun 			  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2644*4882a593Smuzhiyun 			  rxq->descs, rxq->descs_dma);
2645*4882a593Smuzhiyun 	return err;
2646*4882a593Smuzhiyun }
2647*4882a593Smuzhiyun 
2648*4882a593Smuzhiyun /* Push packets received by the RXQ to BM pool */
mvpp2_rxq_drop_pkts(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2649*4882a593Smuzhiyun static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
2650*4882a593Smuzhiyun 				struct mvpp2_rx_queue *rxq)
2651*4882a593Smuzhiyun {
2652*4882a593Smuzhiyun 	int rx_received, i;
2653*4882a593Smuzhiyun 
2654*4882a593Smuzhiyun 	rx_received = mvpp2_rxq_received(port, rxq->id);
2655*4882a593Smuzhiyun 	if (!rx_received)
2656*4882a593Smuzhiyun 		return;
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun 	for (i = 0; i < rx_received; i++) {
2659*4882a593Smuzhiyun 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
2660*4882a593Smuzhiyun 		u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
2661*4882a593Smuzhiyun 		int pool;
2662*4882a593Smuzhiyun 
2663*4882a593Smuzhiyun 		pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
2664*4882a593Smuzhiyun 			MVPP2_RXD_BM_POOL_ID_OFFS;
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun 		mvpp2_bm_pool_put(port, pool,
2667*4882a593Smuzhiyun 				  mvpp2_rxdesc_dma_addr_get(port, rx_desc),
2668*4882a593Smuzhiyun 				  mvpp2_rxdesc_cookie_get(port, rx_desc));
2669*4882a593Smuzhiyun 	}
2670*4882a593Smuzhiyun 	mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
2671*4882a593Smuzhiyun }
2672*4882a593Smuzhiyun 
2673*4882a593Smuzhiyun /* Cleanup Rx queue */
mvpp2_rxq_deinit(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq)2674*4882a593Smuzhiyun static void mvpp2_rxq_deinit(struct mvpp2_port *port,
2675*4882a593Smuzhiyun 			     struct mvpp2_rx_queue *rxq)
2676*4882a593Smuzhiyun {
2677*4882a593Smuzhiyun 	unsigned int thread;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_short))
2680*4882a593Smuzhiyun 		xdp_rxq_info_unreg(&rxq->xdp_rxq_short);
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq_long))
2683*4882a593Smuzhiyun 		xdp_rxq_info_unreg(&rxq->xdp_rxq_long);
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	mvpp2_rxq_drop_pkts(port, rxq);
2686*4882a593Smuzhiyun 
2687*4882a593Smuzhiyun 	if (rxq->descs)
2688*4882a593Smuzhiyun 		dma_free_coherent(port->dev->dev.parent,
2689*4882a593Smuzhiyun 				  rxq->size * MVPP2_DESC_ALIGNED_SIZE,
2690*4882a593Smuzhiyun 				  rxq->descs,
2691*4882a593Smuzhiyun 				  rxq->descs_dma);
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun 	rxq->descs             = NULL;
2694*4882a593Smuzhiyun 	rxq->last_desc         = 0;
2695*4882a593Smuzhiyun 	rxq->next_desc_to_proc = 0;
2696*4882a593Smuzhiyun 	rxq->descs_dma         = 0;
2697*4882a593Smuzhiyun 
2698*4882a593Smuzhiyun 	/* Clear Rx descriptors queue starting address and size;
2699*4882a593Smuzhiyun 	 * free descriptor number
2700*4882a593Smuzhiyun 	 */
2701*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
2702*4882a593Smuzhiyun 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2703*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_NUM_REG, rxq->id);
2704*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_ADDR_REG, 0);
2705*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_RXQ_DESC_SIZE_REG, 0);
2706*4882a593Smuzhiyun 	put_cpu();
2707*4882a593Smuzhiyun }
2708*4882a593Smuzhiyun 
2709*4882a593Smuzhiyun /* Create and initialize a Tx queue */
mvpp2_txq_init(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2710*4882a593Smuzhiyun static int mvpp2_txq_init(struct mvpp2_port *port,
2711*4882a593Smuzhiyun 			  struct mvpp2_tx_queue *txq)
2712*4882a593Smuzhiyun {
2713*4882a593Smuzhiyun 	u32 val;
2714*4882a593Smuzhiyun 	unsigned int thread;
2715*4882a593Smuzhiyun 	int desc, desc_per_txq, tx_port_num;
2716*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 	txq->size = port->tx_ring_size;
2719*4882a593Smuzhiyun 
2720*4882a593Smuzhiyun 	/* Allocate memory for Tx descriptors */
2721*4882a593Smuzhiyun 	txq->descs = dma_alloc_coherent(port->dev->dev.parent,
2722*4882a593Smuzhiyun 				txq->size * MVPP2_DESC_ALIGNED_SIZE,
2723*4882a593Smuzhiyun 				&txq->descs_dma, GFP_KERNEL);
2724*4882a593Smuzhiyun 	if (!txq->descs)
2725*4882a593Smuzhiyun 		return -ENOMEM;
2726*4882a593Smuzhiyun 
2727*4882a593Smuzhiyun 	txq->last_desc = txq->size - 1;
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 	/* Set Tx descriptors queue starting address - indirect access */
2730*4882a593Smuzhiyun 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2731*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2732*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG,
2733*4882a593Smuzhiyun 			   txq->descs_dma);
2734*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG,
2735*4882a593Smuzhiyun 			   txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
2736*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_INDEX_REG, 0);
2737*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_RSVD_CLR_REG,
2738*4882a593Smuzhiyun 			   txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
2739*4882a593Smuzhiyun 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PENDING_REG);
2740*4882a593Smuzhiyun 	val &= ~MVPP2_TXQ_PENDING_MASK;
2741*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PENDING_REG, val);
2742*4882a593Smuzhiyun 
2743*4882a593Smuzhiyun 	/* Calculate base address in prefetch buffer. We reserve 16 descriptors
2744*4882a593Smuzhiyun 	 * for each existing TXQ.
2745*4882a593Smuzhiyun 	 * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
2746*4882a593Smuzhiyun 	 * GBE ports assumed to be continuous from 0 to MVPP2_MAX_PORTS
2747*4882a593Smuzhiyun 	 */
2748*4882a593Smuzhiyun 	desc_per_txq = 16;
2749*4882a593Smuzhiyun 	desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
2750*4882a593Smuzhiyun 	       (txq->log_id * desc_per_txq);
2751*4882a593Smuzhiyun 
2752*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG,
2753*4882a593Smuzhiyun 			   MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
2754*4882a593Smuzhiyun 			   MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
2755*4882a593Smuzhiyun 	put_cpu();
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	/* WRR / EJP configuration - indirect access */
2758*4882a593Smuzhiyun 	tx_port_num = mvpp2_egress_port(port);
2759*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
2760*4882a593Smuzhiyun 
2761*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
2762*4882a593Smuzhiyun 	val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
2763*4882a593Smuzhiyun 	val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
2764*4882a593Smuzhiyun 	val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
2765*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun 	val = MVPP2_TXQ_TOKEN_SIZE_MAX;
2768*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
2769*4882a593Smuzhiyun 		    val);
2770*4882a593Smuzhiyun 
2771*4882a593Smuzhiyun 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2772*4882a593Smuzhiyun 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2773*4882a593Smuzhiyun 		txq_pcpu->size = txq->size;
2774*4882a593Smuzhiyun 		txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
2775*4882a593Smuzhiyun 						sizeof(*txq_pcpu->buffs),
2776*4882a593Smuzhiyun 						GFP_KERNEL);
2777*4882a593Smuzhiyun 		if (!txq_pcpu->buffs)
2778*4882a593Smuzhiyun 			return -ENOMEM;
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun 		txq_pcpu->count = 0;
2781*4882a593Smuzhiyun 		txq_pcpu->reserved_num = 0;
2782*4882a593Smuzhiyun 		txq_pcpu->txq_put_index = 0;
2783*4882a593Smuzhiyun 		txq_pcpu->txq_get_index = 0;
2784*4882a593Smuzhiyun 		txq_pcpu->tso_headers = NULL;
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun 		txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
2787*4882a593Smuzhiyun 		txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 		txq_pcpu->tso_headers =
2790*4882a593Smuzhiyun 			dma_alloc_coherent(port->dev->dev.parent,
2791*4882a593Smuzhiyun 					   txq_pcpu->size * TSO_HEADER_SIZE,
2792*4882a593Smuzhiyun 					   &txq_pcpu->tso_headers_dma,
2793*4882a593Smuzhiyun 					   GFP_KERNEL);
2794*4882a593Smuzhiyun 		if (!txq_pcpu->tso_headers)
2795*4882a593Smuzhiyun 			return -ENOMEM;
2796*4882a593Smuzhiyun 	}
2797*4882a593Smuzhiyun 
2798*4882a593Smuzhiyun 	return 0;
2799*4882a593Smuzhiyun }
2800*4882a593Smuzhiyun 
2801*4882a593Smuzhiyun /* Free allocated TXQ resources */
mvpp2_txq_deinit(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2802*4882a593Smuzhiyun static void mvpp2_txq_deinit(struct mvpp2_port *port,
2803*4882a593Smuzhiyun 			     struct mvpp2_tx_queue *txq)
2804*4882a593Smuzhiyun {
2805*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
2806*4882a593Smuzhiyun 	unsigned int thread;
2807*4882a593Smuzhiyun 
2808*4882a593Smuzhiyun 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2809*4882a593Smuzhiyun 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2810*4882a593Smuzhiyun 		kfree(txq_pcpu->buffs);
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun 		if (txq_pcpu->tso_headers)
2813*4882a593Smuzhiyun 			dma_free_coherent(port->dev->dev.parent,
2814*4882a593Smuzhiyun 					  txq_pcpu->size * TSO_HEADER_SIZE,
2815*4882a593Smuzhiyun 					  txq_pcpu->tso_headers,
2816*4882a593Smuzhiyun 					  txq_pcpu->tso_headers_dma);
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun 		txq_pcpu->tso_headers = NULL;
2819*4882a593Smuzhiyun 	}
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun 	if (txq->descs)
2822*4882a593Smuzhiyun 		dma_free_coherent(port->dev->dev.parent,
2823*4882a593Smuzhiyun 				  txq->size * MVPP2_DESC_ALIGNED_SIZE,
2824*4882a593Smuzhiyun 				  txq->descs, txq->descs_dma);
2825*4882a593Smuzhiyun 
2826*4882a593Smuzhiyun 	txq->descs             = NULL;
2827*4882a593Smuzhiyun 	txq->last_desc         = 0;
2828*4882a593Smuzhiyun 	txq->next_desc_to_proc = 0;
2829*4882a593Smuzhiyun 	txq->descs_dma         = 0;
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun 	/* Set minimum bandwidth for disabled TXQs */
2832*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->log_id), 0);
2833*4882a593Smuzhiyun 
2834*4882a593Smuzhiyun 	/* Set Tx descriptors queue starting address and size */
2835*4882a593Smuzhiyun 	thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2836*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2837*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_ADDR_REG, 0);
2838*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_DESC_SIZE_REG, 0);
2839*4882a593Smuzhiyun 	put_cpu();
2840*4882a593Smuzhiyun }
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun /* Cleanup Tx ports */
mvpp2_txq_clean(struct mvpp2_port * port,struct mvpp2_tx_queue * txq)2843*4882a593Smuzhiyun static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
2844*4882a593Smuzhiyun {
2845*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
2846*4882a593Smuzhiyun 	int delay, pending;
2847*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
2848*4882a593Smuzhiyun 	u32 val;
2849*4882a593Smuzhiyun 
2850*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_NUM_REG, txq->id);
2851*4882a593Smuzhiyun 	val = mvpp2_thread_read(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG);
2852*4882a593Smuzhiyun 	val |= MVPP2_TXQ_DRAIN_EN_MASK;
2853*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2854*4882a593Smuzhiyun 
2855*4882a593Smuzhiyun 	/* The napi queue has been stopped so wait for all packets
2856*4882a593Smuzhiyun 	 * to be transmitted.
2857*4882a593Smuzhiyun 	 */
2858*4882a593Smuzhiyun 	delay = 0;
2859*4882a593Smuzhiyun 	do {
2860*4882a593Smuzhiyun 		if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
2861*4882a593Smuzhiyun 			netdev_warn(port->dev,
2862*4882a593Smuzhiyun 				    "port %d: cleaning queue %d timed out\n",
2863*4882a593Smuzhiyun 				    port->id, txq->log_id);
2864*4882a593Smuzhiyun 			break;
2865*4882a593Smuzhiyun 		}
2866*4882a593Smuzhiyun 		mdelay(1);
2867*4882a593Smuzhiyun 		delay++;
2868*4882a593Smuzhiyun 
2869*4882a593Smuzhiyun 		pending = mvpp2_thread_read(port->priv, thread,
2870*4882a593Smuzhiyun 					    MVPP2_TXQ_PENDING_REG);
2871*4882a593Smuzhiyun 		pending &= MVPP2_TXQ_PENDING_MASK;
2872*4882a593Smuzhiyun 	} while (pending);
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun 	val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
2875*4882a593Smuzhiyun 	mvpp2_thread_write(port->priv, thread, MVPP2_TXQ_PREF_BUF_REG, val);
2876*4882a593Smuzhiyun 	put_cpu();
2877*4882a593Smuzhiyun 
2878*4882a593Smuzhiyun 	for (thread = 0; thread < port->priv->nthreads; thread++) {
2879*4882a593Smuzhiyun 		txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
2880*4882a593Smuzhiyun 
2881*4882a593Smuzhiyun 		/* Release all packets */
2882*4882a593Smuzhiyun 		mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun 		/* Reset queue */
2885*4882a593Smuzhiyun 		txq_pcpu->count = 0;
2886*4882a593Smuzhiyun 		txq_pcpu->txq_put_index = 0;
2887*4882a593Smuzhiyun 		txq_pcpu->txq_get_index = 0;
2888*4882a593Smuzhiyun 	}
2889*4882a593Smuzhiyun }
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun /* Cleanup all Tx queues */
mvpp2_cleanup_txqs(struct mvpp2_port * port)2892*4882a593Smuzhiyun static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
2893*4882a593Smuzhiyun {
2894*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq;
2895*4882a593Smuzhiyun 	int queue;
2896*4882a593Smuzhiyun 	u32 val;
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 	val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 	/* Reset Tx ports and delete Tx queues */
2901*4882a593Smuzhiyun 	val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
2902*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
2903*4882a593Smuzhiyun 
2904*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
2905*4882a593Smuzhiyun 		txq = port->txqs[queue];
2906*4882a593Smuzhiyun 		mvpp2_txq_clean(port, txq);
2907*4882a593Smuzhiyun 		mvpp2_txq_deinit(port, txq);
2908*4882a593Smuzhiyun 	}
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
2913*4882a593Smuzhiyun 	mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
2914*4882a593Smuzhiyun }
2915*4882a593Smuzhiyun 
2916*4882a593Smuzhiyun /* Cleanup all Rx queues */
mvpp2_cleanup_rxqs(struct mvpp2_port * port)2917*4882a593Smuzhiyun static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun 	int queue;
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	for (queue = 0; queue < port->nrxqs; queue++)
2922*4882a593Smuzhiyun 		mvpp2_rxq_deinit(port, port->rxqs[queue]);
2923*4882a593Smuzhiyun }
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun /* Init all Rx queues for port */
mvpp2_setup_rxqs(struct mvpp2_port * port)2926*4882a593Smuzhiyun static int mvpp2_setup_rxqs(struct mvpp2_port *port)
2927*4882a593Smuzhiyun {
2928*4882a593Smuzhiyun 	int queue, err;
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	for (queue = 0; queue < port->nrxqs; queue++) {
2931*4882a593Smuzhiyun 		err = mvpp2_rxq_init(port, port->rxqs[queue]);
2932*4882a593Smuzhiyun 		if (err)
2933*4882a593Smuzhiyun 			goto err_cleanup;
2934*4882a593Smuzhiyun 	}
2935*4882a593Smuzhiyun 	return 0;
2936*4882a593Smuzhiyun 
2937*4882a593Smuzhiyun err_cleanup:
2938*4882a593Smuzhiyun 	mvpp2_cleanup_rxqs(port);
2939*4882a593Smuzhiyun 	return err;
2940*4882a593Smuzhiyun }
2941*4882a593Smuzhiyun 
2942*4882a593Smuzhiyun /* Init all tx queues for port */
mvpp2_setup_txqs(struct mvpp2_port * port)2943*4882a593Smuzhiyun static int mvpp2_setup_txqs(struct mvpp2_port *port)
2944*4882a593Smuzhiyun {
2945*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq;
2946*4882a593Smuzhiyun 	int queue, err;
2947*4882a593Smuzhiyun 
2948*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
2949*4882a593Smuzhiyun 		txq = port->txqs[queue];
2950*4882a593Smuzhiyun 		err = mvpp2_txq_init(port, txq);
2951*4882a593Smuzhiyun 		if (err)
2952*4882a593Smuzhiyun 			goto err_cleanup;
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun 		/* Assign this queue to a CPU */
2955*4882a593Smuzhiyun 		if (queue < num_possible_cpus())
2956*4882a593Smuzhiyun 			netif_set_xps_queue(port->dev, cpumask_of(queue), queue);
2957*4882a593Smuzhiyun 	}
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun 	if (port->has_tx_irqs) {
2960*4882a593Smuzhiyun 		mvpp2_tx_time_coal_set(port);
2961*4882a593Smuzhiyun 		for (queue = 0; queue < port->ntxqs; queue++) {
2962*4882a593Smuzhiyun 			txq = port->txqs[queue];
2963*4882a593Smuzhiyun 			mvpp2_tx_pkts_coal_set(port, txq);
2964*4882a593Smuzhiyun 		}
2965*4882a593Smuzhiyun 	}
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
2968*4882a593Smuzhiyun 	return 0;
2969*4882a593Smuzhiyun 
2970*4882a593Smuzhiyun err_cleanup:
2971*4882a593Smuzhiyun 	mvpp2_cleanup_txqs(port);
2972*4882a593Smuzhiyun 	return err;
2973*4882a593Smuzhiyun }
2974*4882a593Smuzhiyun 
2975*4882a593Smuzhiyun /* The callback for per-port interrupt */
mvpp2_isr(int irq,void * dev_id)2976*4882a593Smuzhiyun static irqreturn_t mvpp2_isr(int irq, void *dev_id)
2977*4882a593Smuzhiyun {
2978*4882a593Smuzhiyun 	struct mvpp2_queue_vector *qv = dev_id;
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	mvpp2_qvec_interrupt_disable(qv);
2981*4882a593Smuzhiyun 
2982*4882a593Smuzhiyun 	napi_schedule(&qv->napi);
2983*4882a593Smuzhiyun 
2984*4882a593Smuzhiyun 	return IRQ_HANDLED;
2985*4882a593Smuzhiyun }
2986*4882a593Smuzhiyun 
mvpp2_isr_handle_ptp_queue(struct mvpp2_port * port,int nq)2987*4882a593Smuzhiyun static void mvpp2_isr_handle_ptp_queue(struct mvpp2_port *port, int nq)
2988*4882a593Smuzhiyun {
2989*4882a593Smuzhiyun 	struct skb_shared_hwtstamps shhwtstamps;
2990*4882a593Smuzhiyun 	struct mvpp2_hwtstamp_queue *queue;
2991*4882a593Smuzhiyun 	struct sk_buff *skb;
2992*4882a593Smuzhiyun 	void __iomem *ptp_q;
2993*4882a593Smuzhiyun 	unsigned int id;
2994*4882a593Smuzhiyun 	u32 r0, r1, r2;
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun 	ptp_q = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
2997*4882a593Smuzhiyun 	if (nq)
2998*4882a593Smuzhiyun 		ptp_q += MVPP22_PTP_TX_Q1_R0 - MVPP22_PTP_TX_Q0_R0;
2999*4882a593Smuzhiyun 
3000*4882a593Smuzhiyun 	queue = &port->tx_hwtstamp_queue[nq];
3001*4882a593Smuzhiyun 
3002*4882a593Smuzhiyun 	while (1) {
3003*4882a593Smuzhiyun 		r0 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R0) & 0xffff;
3004*4882a593Smuzhiyun 		if (!r0)
3005*4882a593Smuzhiyun 			break;
3006*4882a593Smuzhiyun 
3007*4882a593Smuzhiyun 		r1 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R1) & 0xffff;
3008*4882a593Smuzhiyun 		r2 = readl_relaxed(ptp_q + MVPP22_PTP_TX_Q0_R2) & 0xffff;
3009*4882a593Smuzhiyun 
3010*4882a593Smuzhiyun 		id = (r0 >> 1) & 31;
3011*4882a593Smuzhiyun 
3012*4882a593Smuzhiyun 		skb = queue->skb[id];
3013*4882a593Smuzhiyun 		queue->skb[id] = NULL;
3014*4882a593Smuzhiyun 		if (skb) {
3015*4882a593Smuzhiyun 			u32 ts = r2 << 19 | r1 << 3 | r0 >> 13;
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun 			mvpp22_tai_tstamp(port->priv->tai, ts, &shhwtstamps);
3018*4882a593Smuzhiyun 			skb_tstamp_tx(skb, &shhwtstamps);
3019*4882a593Smuzhiyun 			dev_kfree_skb_any(skb);
3020*4882a593Smuzhiyun 		}
3021*4882a593Smuzhiyun 	}
3022*4882a593Smuzhiyun }
3023*4882a593Smuzhiyun 
mvpp2_isr_handle_ptp(struct mvpp2_port * port)3024*4882a593Smuzhiyun static void mvpp2_isr_handle_ptp(struct mvpp2_port *port)
3025*4882a593Smuzhiyun {
3026*4882a593Smuzhiyun 	void __iomem *ptp;
3027*4882a593Smuzhiyun 	u32 val;
3028*4882a593Smuzhiyun 
3029*4882a593Smuzhiyun 	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
3030*4882a593Smuzhiyun 	val = readl(ptp + MVPP22_PTP_INT_CAUSE);
3031*4882a593Smuzhiyun 	if (val & MVPP22_PTP_INT_CAUSE_QUEUE0)
3032*4882a593Smuzhiyun 		mvpp2_isr_handle_ptp_queue(port, 0);
3033*4882a593Smuzhiyun 	if (val & MVPP22_PTP_INT_CAUSE_QUEUE1)
3034*4882a593Smuzhiyun 		mvpp2_isr_handle_ptp_queue(port, 1);
3035*4882a593Smuzhiyun }
3036*4882a593Smuzhiyun 
mvpp2_isr_handle_link(struct mvpp2_port * port,bool link)3037*4882a593Smuzhiyun static void mvpp2_isr_handle_link(struct mvpp2_port *port, bool link)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	struct net_device *dev = port->dev;
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 	if (port->phylink) {
3042*4882a593Smuzhiyun 		phylink_mac_change(port->phylink, link);
3043*4882a593Smuzhiyun 		return;
3044*4882a593Smuzhiyun 	}
3045*4882a593Smuzhiyun 
3046*4882a593Smuzhiyun 	if (!netif_running(dev))
3047*4882a593Smuzhiyun 		return;
3048*4882a593Smuzhiyun 
3049*4882a593Smuzhiyun 	if (link) {
3050*4882a593Smuzhiyun 		mvpp2_interrupts_enable(port);
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun 		mvpp2_egress_enable(port);
3053*4882a593Smuzhiyun 		mvpp2_ingress_enable(port);
3054*4882a593Smuzhiyun 		netif_carrier_on(dev);
3055*4882a593Smuzhiyun 		netif_tx_wake_all_queues(dev);
3056*4882a593Smuzhiyun 	} else {
3057*4882a593Smuzhiyun 		netif_tx_stop_all_queues(dev);
3058*4882a593Smuzhiyun 		netif_carrier_off(dev);
3059*4882a593Smuzhiyun 		mvpp2_ingress_disable(port);
3060*4882a593Smuzhiyun 		mvpp2_egress_disable(port);
3061*4882a593Smuzhiyun 
3062*4882a593Smuzhiyun 		mvpp2_interrupts_disable(port);
3063*4882a593Smuzhiyun 	}
3064*4882a593Smuzhiyun }
3065*4882a593Smuzhiyun 
mvpp2_isr_handle_xlg(struct mvpp2_port * port)3066*4882a593Smuzhiyun static void mvpp2_isr_handle_xlg(struct mvpp2_port *port)
3067*4882a593Smuzhiyun {
3068*4882a593Smuzhiyun 	bool link;
3069*4882a593Smuzhiyun 	u32 val;
3070*4882a593Smuzhiyun 
3071*4882a593Smuzhiyun 	val = readl(port->base + MVPP22_XLG_INT_STAT);
3072*4882a593Smuzhiyun 	if (val & MVPP22_XLG_INT_STAT_LINK) {
3073*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_STATUS);
3074*4882a593Smuzhiyun 		link = (val & MVPP22_XLG_STATUS_LINK_UP);
3075*4882a593Smuzhiyun 		mvpp2_isr_handle_link(port, link);
3076*4882a593Smuzhiyun 	}
3077*4882a593Smuzhiyun }
3078*4882a593Smuzhiyun 
mvpp2_isr_handle_gmac_internal(struct mvpp2_port * port)3079*4882a593Smuzhiyun static void mvpp2_isr_handle_gmac_internal(struct mvpp2_port *port)
3080*4882a593Smuzhiyun {
3081*4882a593Smuzhiyun 	bool link;
3082*4882a593Smuzhiyun 	u32 val;
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 	if (phy_interface_mode_is_rgmii(port->phy_interface) ||
3085*4882a593Smuzhiyun 	    phy_interface_mode_is_8023z(port->phy_interface) ||
3086*4882a593Smuzhiyun 	    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
3087*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_GMAC_INT_STAT);
3088*4882a593Smuzhiyun 		if (val & MVPP22_GMAC_INT_STAT_LINK) {
3089*4882a593Smuzhiyun 			val = readl(port->base + MVPP2_GMAC_STATUS0);
3090*4882a593Smuzhiyun 			link = (val & MVPP2_GMAC_STATUS0_LINK_UP);
3091*4882a593Smuzhiyun 			mvpp2_isr_handle_link(port, link);
3092*4882a593Smuzhiyun 		}
3093*4882a593Smuzhiyun 	}
3094*4882a593Smuzhiyun }
3095*4882a593Smuzhiyun 
3096*4882a593Smuzhiyun /* Per-port interrupt for link status changes */
mvpp2_port_isr(int irq,void * dev_id)3097*4882a593Smuzhiyun static irqreturn_t mvpp2_port_isr(int irq, void *dev_id)
3098*4882a593Smuzhiyun {
3099*4882a593Smuzhiyun 	struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
3100*4882a593Smuzhiyun 	u32 val;
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun 	mvpp22_gop_mask_irq(port);
3103*4882a593Smuzhiyun 
3104*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port) &&
3105*4882a593Smuzhiyun 	    mvpp2_is_xlg(port->phy_interface)) {
3106*4882a593Smuzhiyun 		/* Check the external status register */
3107*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_EXT_INT_STAT);
3108*4882a593Smuzhiyun 		if (val & MVPP22_XLG_EXT_INT_STAT_XLG)
3109*4882a593Smuzhiyun 			mvpp2_isr_handle_xlg(port);
3110*4882a593Smuzhiyun 		if (val & MVPP22_XLG_EXT_INT_STAT_PTP)
3111*4882a593Smuzhiyun 			mvpp2_isr_handle_ptp(port);
3112*4882a593Smuzhiyun 	} else {
3113*4882a593Smuzhiyun 		/* If it's not the XLG, we must be using the GMAC.
3114*4882a593Smuzhiyun 		 * Check the summary status.
3115*4882a593Smuzhiyun 		 */
3116*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_GMAC_INT_SUM_STAT);
3117*4882a593Smuzhiyun 		if (val & MVPP22_GMAC_INT_SUM_STAT_INTERNAL)
3118*4882a593Smuzhiyun 			mvpp2_isr_handle_gmac_internal(port);
3119*4882a593Smuzhiyun 		if (val & MVPP22_GMAC_INT_SUM_STAT_PTP)
3120*4882a593Smuzhiyun 			mvpp2_isr_handle_ptp(port);
3121*4882a593Smuzhiyun 	}
3122*4882a593Smuzhiyun 
3123*4882a593Smuzhiyun 	mvpp22_gop_unmask_irq(port);
3124*4882a593Smuzhiyun 	return IRQ_HANDLED;
3125*4882a593Smuzhiyun }
3126*4882a593Smuzhiyun 
mvpp2_hr_timer_cb(struct hrtimer * timer)3127*4882a593Smuzhiyun static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
3128*4882a593Smuzhiyun {
3129*4882a593Smuzhiyun 	struct net_device *dev;
3130*4882a593Smuzhiyun 	struct mvpp2_port *port;
3131*4882a593Smuzhiyun 	struct mvpp2_port_pcpu *port_pcpu;
3132*4882a593Smuzhiyun 	unsigned int tx_todo, cause;
3133*4882a593Smuzhiyun 
3134*4882a593Smuzhiyun 	port_pcpu = container_of(timer, struct mvpp2_port_pcpu, tx_done_timer);
3135*4882a593Smuzhiyun 	dev = port_pcpu->dev;
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	if (!netif_running(dev))
3138*4882a593Smuzhiyun 		return HRTIMER_NORESTART;
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun 	port_pcpu->timer_scheduled = false;
3141*4882a593Smuzhiyun 	port = netdev_priv(dev);
3142*4882a593Smuzhiyun 
3143*4882a593Smuzhiyun 	/* Process all the Tx queues */
3144*4882a593Smuzhiyun 	cause = (1 << port->ntxqs) - 1;
3145*4882a593Smuzhiyun 	tx_todo = mvpp2_tx_done(port, cause,
3146*4882a593Smuzhiyun 				mvpp2_cpu_to_thread(port->priv, smp_processor_id()));
3147*4882a593Smuzhiyun 
3148*4882a593Smuzhiyun 	/* Set the timer in case not all the packets were processed */
3149*4882a593Smuzhiyun 	if (tx_todo && !port_pcpu->timer_scheduled) {
3150*4882a593Smuzhiyun 		port_pcpu->timer_scheduled = true;
3151*4882a593Smuzhiyun 		hrtimer_forward_now(&port_pcpu->tx_done_timer,
3152*4882a593Smuzhiyun 				    MVPP2_TXDONE_HRTIMER_PERIOD_NS);
3153*4882a593Smuzhiyun 
3154*4882a593Smuzhiyun 		return HRTIMER_RESTART;
3155*4882a593Smuzhiyun 	}
3156*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
3157*4882a593Smuzhiyun }
3158*4882a593Smuzhiyun 
3159*4882a593Smuzhiyun /* Main RX/TX processing routines */
3160*4882a593Smuzhiyun 
3161*4882a593Smuzhiyun /* Display more error info */
mvpp2_rx_error(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc)3162*4882a593Smuzhiyun static void mvpp2_rx_error(struct mvpp2_port *port,
3163*4882a593Smuzhiyun 			   struct mvpp2_rx_desc *rx_desc)
3164*4882a593Smuzhiyun {
3165*4882a593Smuzhiyun 	u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
3166*4882a593Smuzhiyun 	size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
3167*4882a593Smuzhiyun 	char *err_str = NULL;
3168*4882a593Smuzhiyun 
3169*4882a593Smuzhiyun 	switch (status & MVPP2_RXD_ERR_CODE_MASK) {
3170*4882a593Smuzhiyun 	case MVPP2_RXD_ERR_CRC:
3171*4882a593Smuzhiyun 		err_str = "crc";
3172*4882a593Smuzhiyun 		break;
3173*4882a593Smuzhiyun 	case MVPP2_RXD_ERR_OVERRUN:
3174*4882a593Smuzhiyun 		err_str = "overrun";
3175*4882a593Smuzhiyun 		break;
3176*4882a593Smuzhiyun 	case MVPP2_RXD_ERR_RESOURCE:
3177*4882a593Smuzhiyun 		err_str = "resource";
3178*4882a593Smuzhiyun 		break;
3179*4882a593Smuzhiyun 	}
3180*4882a593Smuzhiyun 	if (err_str && net_ratelimit())
3181*4882a593Smuzhiyun 		netdev_err(port->dev,
3182*4882a593Smuzhiyun 			   "bad rx status %08x (%s error), size=%zu\n",
3183*4882a593Smuzhiyun 			   status, err_str, sz);
3184*4882a593Smuzhiyun }
3185*4882a593Smuzhiyun 
3186*4882a593Smuzhiyun /* Handle RX checksum offload */
mvpp2_rx_csum(struct mvpp2_port * port,u32 status,struct sk_buff * skb)3187*4882a593Smuzhiyun static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
3188*4882a593Smuzhiyun 			  struct sk_buff *skb)
3189*4882a593Smuzhiyun {
3190*4882a593Smuzhiyun 	if (((status & MVPP2_RXD_L3_IP4) &&
3191*4882a593Smuzhiyun 	     !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
3192*4882a593Smuzhiyun 	    (status & MVPP2_RXD_L3_IP6))
3193*4882a593Smuzhiyun 		if (((status & MVPP2_RXD_L4_UDP) ||
3194*4882a593Smuzhiyun 		     (status & MVPP2_RXD_L4_TCP)) &&
3195*4882a593Smuzhiyun 		     (status & MVPP2_RXD_L4_CSUM_OK)) {
3196*4882a593Smuzhiyun 			skb->csum = 0;
3197*4882a593Smuzhiyun 			skb->ip_summed = CHECKSUM_UNNECESSARY;
3198*4882a593Smuzhiyun 			return;
3199*4882a593Smuzhiyun 		}
3200*4882a593Smuzhiyun 
3201*4882a593Smuzhiyun 	skb->ip_summed = CHECKSUM_NONE;
3202*4882a593Smuzhiyun }
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun /* Allocate a new skb and add it to BM pool */
mvpp2_rx_refill(struct mvpp2_port * port,struct mvpp2_bm_pool * bm_pool,struct page_pool * page_pool,int pool)3205*4882a593Smuzhiyun static int mvpp2_rx_refill(struct mvpp2_port *port,
3206*4882a593Smuzhiyun 			   struct mvpp2_bm_pool *bm_pool,
3207*4882a593Smuzhiyun 			   struct page_pool *page_pool, int pool)
3208*4882a593Smuzhiyun {
3209*4882a593Smuzhiyun 	dma_addr_t dma_addr;
3210*4882a593Smuzhiyun 	phys_addr_t phys_addr;
3211*4882a593Smuzhiyun 	void *buf;
3212*4882a593Smuzhiyun 
3213*4882a593Smuzhiyun 	buf = mvpp2_buf_alloc(port, bm_pool, page_pool,
3214*4882a593Smuzhiyun 			      &dma_addr, &phys_addr, GFP_ATOMIC);
3215*4882a593Smuzhiyun 	if (!buf)
3216*4882a593Smuzhiyun 		return -ENOMEM;
3217*4882a593Smuzhiyun 
3218*4882a593Smuzhiyun 	mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun 	return 0;
3221*4882a593Smuzhiyun }
3222*4882a593Smuzhiyun 
3223*4882a593Smuzhiyun /* Handle tx checksum */
mvpp2_skb_tx_csum(struct mvpp2_port * port,struct sk_buff * skb)3224*4882a593Smuzhiyun static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
3225*4882a593Smuzhiyun {
3226*4882a593Smuzhiyun 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3227*4882a593Smuzhiyun 		int ip_hdr_len = 0;
3228*4882a593Smuzhiyun 		u8 l4_proto;
3229*4882a593Smuzhiyun 		__be16 l3_proto = vlan_get_protocol(skb);
3230*4882a593Smuzhiyun 
3231*4882a593Smuzhiyun 		if (l3_proto == htons(ETH_P_IP)) {
3232*4882a593Smuzhiyun 			struct iphdr *ip4h = ip_hdr(skb);
3233*4882a593Smuzhiyun 
3234*4882a593Smuzhiyun 			/* Calculate IPv4 checksum and L4 checksum */
3235*4882a593Smuzhiyun 			ip_hdr_len = ip4h->ihl;
3236*4882a593Smuzhiyun 			l4_proto = ip4h->protocol;
3237*4882a593Smuzhiyun 		} else if (l3_proto == htons(ETH_P_IPV6)) {
3238*4882a593Smuzhiyun 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
3239*4882a593Smuzhiyun 
3240*4882a593Smuzhiyun 			/* Read l4_protocol from one of IPv6 extra headers */
3241*4882a593Smuzhiyun 			if (skb_network_header_len(skb) > 0)
3242*4882a593Smuzhiyun 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
3243*4882a593Smuzhiyun 			l4_proto = ip6h->nexthdr;
3244*4882a593Smuzhiyun 		} else {
3245*4882a593Smuzhiyun 			return MVPP2_TXD_L4_CSUM_NOT;
3246*4882a593Smuzhiyun 		}
3247*4882a593Smuzhiyun 
3248*4882a593Smuzhiyun 		return mvpp2_txq_desc_csum(skb_network_offset(skb),
3249*4882a593Smuzhiyun 					   l3_proto, ip_hdr_len, l4_proto);
3250*4882a593Smuzhiyun 	}
3251*4882a593Smuzhiyun 
3252*4882a593Smuzhiyun 	return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
3253*4882a593Smuzhiyun }
3254*4882a593Smuzhiyun 
mvpp2_xdp_finish_tx(struct mvpp2_port * port,u16 txq_id,int nxmit,int nxmit_byte)3255*4882a593Smuzhiyun static void mvpp2_xdp_finish_tx(struct mvpp2_port *port, u16 txq_id, int nxmit, int nxmit_byte)
3256*4882a593Smuzhiyun {
3257*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3258*4882a593Smuzhiyun 	struct mvpp2_tx_queue *aggr_txq;
3259*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
3260*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq;
3261*4882a593Smuzhiyun 	struct netdev_queue *nq;
3262*4882a593Smuzhiyun 
3263*4882a593Smuzhiyun 	txq = port->txqs[txq_id];
3264*4882a593Smuzhiyun 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3265*4882a593Smuzhiyun 	nq = netdev_get_tx_queue(port->dev, txq_id);
3266*4882a593Smuzhiyun 	aggr_txq = &port->priv->aggr_txqs[thread];
3267*4882a593Smuzhiyun 
3268*4882a593Smuzhiyun 	txq_pcpu->reserved_num -= nxmit;
3269*4882a593Smuzhiyun 	txq_pcpu->count += nxmit;
3270*4882a593Smuzhiyun 	aggr_txq->count += nxmit;
3271*4882a593Smuzhiyun 
3272*4882a593Smuzhiyun 	/* Enable transmit */
3273*4882a593Smuzhiyun 	wmb();
3274*4882a593Smuzhiyun 	mvpp2_aggr_txq_pend_desc_add(port, nxmit);
3275*4882a593Smuzhiyun 
3276*4882a593Smuzhiyun 	if (txq_pcpu->count >= txq_pcpu->stop_threshold)
3277*4882a593Smuzhiyun 		netif_tx_stop_queue(nq);
3278*4882a593Smuzhiyun 
3279*4882a593Smuzhiyun 	/* Finalize TX processing */
3280*4882a593Smuzhiyun 	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
3281*4882a593Smuzhiyun 		mvpp2_txq_done(port, txq, txq_pcpu);
3282*4882a593Smuzhiyun }
3283*4882a593Smuzhiyun 
3284*4882a593Smuzhiyun static int
mvpp2_xdp_submit_frame(struct mvpp2_port * port,u16 txq_id,struct xdp_frame * xdpf,bool dma_map)3285*4882a593Smuzhiyun mvpp2_xdp_submit_frame(struct mvpp2_port *port, u16 txq_id,
3286*4882a593Smuzhiyun 		       struct xdp_frame *xdpf, bool dma_map)
3287*4882a593Smuzhiyun {
3288*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3289*4882a593Smuzhiyun 	u32 tx_cmd = MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE |
3290*4882a593Smuzhiyun 		     MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
3291*4882a593Smuzhiyun 	enum mvpp2_tx_buf_type buf_type;
3292*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
3293*4882a593Smuzhiyun 	struct mvpp2_tx_queue *aggr_txq;
3294*4882a593Smuzhiyun 	struct mvpp2_tx_desc *tx_desc;
3295*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq;
3296*4882a593Smuzhiyun 	int ret = MVPP2_XDP_TX;
3297*4882a593Smuzhiyun 	dma_addr_t dma_addr;
3298*4882a593Smuzhiyun 
3299*4882a593Smuzhiyun 	txq = port->txqs[txq_id];
3300*4882a593Smuzhiyun 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3301*4882a593Smuzhiyun 	aggr_txq = &port->priv->aggr_txqs[thread];
3302*4882a593Smuzhiyun 
3303*4882a593Smuzhiyun 	/* Check number of available descriptors */
3304*4882a593Smuzhiyun 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, 1) ||
3305*4882a593Smuzhiyun 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, 1)) {
3306*4882a593Smuzhiyun 		ret = MVPP2_XDP_DROPPED;
3307*4882a593Smuzhiyun 		goto out;
3308*4882a593Smuzhiyun 	}
3309*4882a593Smuzhiyun 
3310*4882a593Smuzhiyun 	/* Get a descriptor for the first part of the packet */
3311*4882a593Smuzhiyun 	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3312*4882a593Smuzhiyun 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3313*4882a593Smuzhiyun 	mvpp2_txdesc_size_set(port, tx_desc, xdpf->len);
3314*4882a593Smuzhiyun 
3315*4882a593Smuzhiyun 	if (dma_map) {
3316*4882a593Smuzhiyun 		/* XDP_REDIRECT or AF_XDP */
3317*4882a593Smuzhiyun 		dma_addr = dma_map_single(port->dev->dev.parent, xdpf->data,
3318*4882a593Smuzhiyun 					  xdpf->len, DMA_TO_DEVICE);
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun 		if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
3321*4882a593Smuzhiyun 			mvpp2_txq_desc_put(txq);
3322*4882a593Smuzhiyun 			ret = MVPP2_XDP_DROPPED;
3323*4882a593Smuzhiyun 			goto out;
3324*4882a593Smuzhiyun 		}
3325*4882a593Smuzhiyun 
3326*4882a593Smuzhiyun 		buf_type = MVPP2_TYPE_XDP_NDO;
3327*4882a593Smuzhiyun 	} else {
3328*4882a593Smuzhiyun 		/* XDP_TX */
3329*4882a593Smuzhiyun 		struct page *page = virt_to_page(xdpf->data);
3330*4882a593Smuzhiyun 
3331*4882a593Smuzhiyun 		dma_addr = page_pool_get_dma_addr(page) +
3332*4882a593Smuzhiyun 			   sizeof(*xdpf) + xdpf->headroom;
3333*4882a593Smuzhiyun 		dma_sync_single_for_device(port->dev->dev.parent, dma_addr,
3334*4882a593Smuzhiyun 					   xdpf->len, DMA_BIDIRECTIONAL);
3335*4882a593Smuzhiyun 
3336*4882a593Smuzhiyun 		buf_type = MVPP2_TYPE_XDP_TX;
3337*4882a593Smuzhiyun 	}
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun 	mvpp2_txdesc_dma_addr_set(port, tx_desc, dma_addr);
3340*4882a593Smuzhiyun 
3341*4882a593Smuzhiyun 	mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
3342*4882a593Smuzhiyun 	mvpp2_txq_inc_put(port, txq_pcpu, xdpf, tx_desc, buf_type);
3343*4882a593Smuzhiyun 
3344*4882a593Smuzhiyun out:
3345*4882a593Smuzhiyun 	return ret;
3346*4882a593Smuzhiyun }
3347*4882a593Smuzhiyun 
3348*4882a593Smuzhiyun static int
mvpp2_xdp_xmit_back(struct mvpp2_port * port,struct xdp_buff * xdp)3349*4882a593Smuzhiyun mvpp2_xdp_xmit_back(struct mvpp2_port *port, struct xdp_buff *xdp)
3350*4882a593Smuzhiyun {
3351*4882a593Smuzhiyun 	struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
3352*4882a593Smuzhiyun 	struct xdp_frame *xdpf;
3353*4882a593Smuzhiyun 	u16 txq_id;
3354*4882a593Smuzhiyun 	int ret;
3355*4882a593Smuzhiyun 
3356*4882a593Smuzhiyun 	xdpf = xdp_convert_buff_to_frame(xdp);
3357*4882a593Smuzhiyun 	if (unlikely(!xdpf))
3358*4882a593Smuzhiyun 		return MVPP2_XDP_DROPPED;
3359*4882a593Smuzhiyun 
3360*4882a593Smuzhiyun 	/* The first of the TX queues are used for XPS,
3361*4882a593Smuzhiyun 	 * the second half for XDP_TX
3362*4882a593Smuzhiyun 	 */
3363*4882a593Smuzhiyun 	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3364*4882a593Smuzhiyun 
3365*4882a593Smuzhiyun 	ret = mvpp2_xdp_submit_frame(port, txq_id, xdpf, false);
3366*4882a593Smuzhiyun 	if (ret == MVPP2_XDP_TX) {
3367*4882a593Smuzhiyun 		u64_stats_update_begin(&stats->syncp);
3368*4882a593Smuzhiyun 		stats->tx_bytes += xdpf->len;
3369*4882a593Smuzhiyun 		stats->tx_packets++;
3370*4882a593Smuzhiyun 		stats->xdp_tx++;
3371*4882a593Smuzhiyun 		u64_stats_update_end(&stats->syncp);
3372*4882a593Smuzhiyun 
3373*4882a593Smuzhiyun 		mvpp2_xdp_finish_tx(port, txq_id, 1, xdpf->len);
3374*4882a593Smuzhiyun 	} else {
3375*4882a593Smuzhiyun 		u64_stats_update_begin(&stats->syncp);
3376*4882a593Smuzhiyun 		stats->xdp_tx_err++;
3377*4882a593Smuzhiyun 		u64_stats_update_end(&stats->syncp);
3378*4882a593Smuzhiyun 	}
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun 	return ret;
3381*4882a593Smuzhiyun }
3382*4882a593Smuzhiyun 
3383*4882a593Smuzhiyun static int
mvpp2_xdp_xmit(struct net_device * dev,int num_frame,struct xdp_frame ** frames,u32 flags)3384*4882a593Smuzhiyun mvpp2_xdp_xmit(struct net_device *dev, int num_frame,
3385*4882a593Smuzhiyun 	       struct xdp_frame **frames, u32 flags)
3386*4882a593Smuzhiyun {
3387*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
3388*4882a593Smuzhiyun 	int i, nxmit_byte = 0, nxmit = num_frame;
3389*4882a593Smuzhiyun 	struct mvpp2_pcpu_stats *stats;
3390*4882a593Smuzhiyun 	u16 txq_id;
3391*4882a593Smuzhiyun 	u32 ret;
3392*4882a593Smuzhiyun 
3393*4882a593Smuzhiyun 	if (unlikely(test_bit(0, &port->state)))
3394*4882a593Smuzhiyun 		return -ENETDOWN;
3395*4882a593Smuzhiyun 
3396*4882a593Smuzhiyun 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3397*4882a593Smuzhiyun 		return -EINVAL;
3398*4882a593Smuzhiyun 
3399*4882a593Smuzhiyun 	/* The first of the TX queues are used for XPS,
3400*4882a593Smuzhiyun 	 * the second half for XDP_TX
3401*4882a593Smuzhiyun 	 */
3402*4882a593Smuzhiyun 	txq_id = mvpp2_cpu_to_thread(port->priv, smp_processor_id()) + (port->ntxqs / 2);
3403*4882a593Smuzhiyun 
3404*4882a593Smuzhiyun 	for (i = 0; i < num_frame; i++) {
3405*4882a593Smuzhiyun 		ret = mvpp2_xdp_submit_frame(port, txq_id, frames[i], true);
3406*4882a593Smuzhiyun 		if (ret == MVPP2_XDP_TX) {
3407*4882a593Smuzhiyun 			nxmit_byte += frames[i]->len;
3408*4882a593Smuzhiyun 		} else {
3409*4882a593Smuzhiyun 			xdp_return_frame_rx_napi(frames[i]);
3410*4882a593Smuzhiyun 			nxmit--;
3411*4882a593Smuzhiyun 		}
3412*4882a593Smuzhiyun 	}
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun 	if (likely(nxmit > 0))
3415*4882a593Smuzhiyun 		mvpp2_xdp_finish_tx(port, txq_id, nxmit, nxmit_byte);
3416*4882a593Smuzhiyun 
3417*4882a593Smuzhiyun 	stats = this_cpu_ptr(port->stats);
3418*4882a593Smuzhiyun 	u64_stats_update_begin(&stats->syncp);
3419*4882a593Smuzhiyun 	stats->tx_bytes += nxmit_byte;
3420*4882a593Smuzhiyun 	stats->tx_packets += nxmit;
3421*4882a593Smuzhiyun 	stats->xdp_xmit += nxmit;
3422*4882a593Smuzhiyun 	stats->xdp_xmit_err += num_frame - nxmit;
3423*4882a593Smuzhiyun 	u64_stats_update_end(&stats->syncp);
3424*4882a593Smuzhiyun 
3425*4882a593Smuzhiyun 	return nxmit;
3426*4882a593Smuzhiyun }
3427*4882a593Smuzhiyun 
3428*4882a593Smuzhiyun static int
mvpp2_run_xdp(struct mvpp2_port * port,struct mvpp2_rx_queue * rxq,struct bpf_prog * prog,struct xdp_buff * xdp,struct page_pool * pp,struct mvpp2_pcpu_stats * stats)3429*4882a593Smuzhiyun mvpp2_run_xdp(struct mvpp2_port *port, struct mvpp2_rx_queue *rxq,
3430*4882a593Smuzhiyun 	      struct bpf_prog *prog, struct xdp_buff *xdp,
3431*4882a593Smuzhiyun 	      struct page_pool *pp, struct mvpp2_pcpu_stats *stats)
3432*4882a593Smuzhiyun {
3433*4882a593Smuzhiyun 	unsigned int len, sync, err;
3434*4882a593Smuzhiyun 	struct page *page;
3435*4882a593Smuzhiyun 	u32 ret, act;
3436*4882a593Smuzhiyun 
3437*4882a593Smuzhiyun 	len = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3438*4882a593Smuzhiyun 	act = bpf_prog_run_xdp(prog, xdp);
3439*4882a593Smuzhiyun 
3440*4882a593Smuzhiyun 	/* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
3441*4882a593Smuzhiyun 	sync = xdp->data_end - xdp->data_hard_start - MVPP2_SKB_HEADROOM;
3442*4882a593Smuzhiyun 	sync = max(sync, len);
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	switch (act) {
3445*4882a593Smuzhiyun 	case XDP_PASS:
3446*4882a593Smuzhiyun 		stats->xdp_pass++;
3447*4882a593Smuzhiyun 		ret = MVPP2_XDP_PASS;
3448*4882a593Smuzhiyun 		break;
3449*4882a593Smuzhiyun 	case XDP_REDIRECT:
3450*4882a593Smuzhiyun 		err = xdp_do_redirect(port->dev, xdp, prog);
3451*4882a593Smuzhiyun 		if (unlikely(err)) {
3452*4882a593Smuzhiyun 			ret = MVPP2_XDP_DROPPED;
3453*4882a593Smuzhiyun 			page = virt_to_head_page(xdp->data);
3454*4882a593Smuzhiyun 			page_pool_put_page(pp, page, sync, true);
3455*4882a593Smuzhiyun 		} else {
3456*4882a593Smuzhiyun 			ret = MVPP2_XDP_REDIR;
3457*4882a593Smuzhiyun 			stats->xdp_redirect++;
3458*4882a593Smuzhiyun 		}
3459*4882a593Smuzhiyun 		break;
3460*4882a593Smuzhiyun 	case XDP_TX:
3461*4882a593Smuzhiyun 		ret = mvpp2_xdp_xmit_back(port, xdp);
3462*4882a593Smuzhiyun 		if (ret != MVPP2_XDP_TX) {
3463*4882a593Smuzhiyun 			page = virt_to_head_page(xdp->data);
3464*4882a593Smuzhiyun 			page_pool_put_page(pp, page, sync, true);
3465*4882a593Smuzhiyun 		}
3466*4882a593Smuzhiyun 		break;
3467*4882a593Smuzhiyun 	default:
3468*4882a593Smuzhiyun 		bpf_warn_invalid_xdp_action(act);
3469*4882a593Smuzhiyun 		fallthrough;
3470*4882a593Smuzhiyun 	case XDP_ABORTED:
3471*4882a593Smuzhiyun 		trace_xdp_exception(port->dev, prog, act);
3472*4882a593Smuzhiyun 		fallthrough;
3473*4882a593Smuzhiyun 	case XDP_DROP:
3474*4882a593Smuzhiyun 		page = virt_to_head_page(xdp->data);
3475*4882a593Smuzhiyun 		page_pool_put_page(pp, page, sync, true);
3476*4882a593Smuzhiyun 		ret = MVPP2_XDP_DROPPED;
3477*4882a593Smuzhiyun 		stats->xdp_drop++;
3478*4882a593Smuzhiyun 		break;
3479*4882a593Smuzhiyun 	}
3480*4882a593Smuzhiyun 
3481*4882a593Smuzhiyun 	return ret;
3482*4882a593Smuzhiyun }
3483*4882a593Smuzhiyun 
mvpp2_buff_hdr_pool_put(struct mvpp2_port * port,struct mvpp2_rx_desc * rx_desc,int pool,u32 rx_status)3484*4882a593Smuzhiyun static void mvpp2_buff_hdr_pool_put(struct mvpp2_port *port, struct mvpp2_rx_desc *rx_desc,
3485*4882a593Smuzhiyun 				    int pool, u32 rx_status)
3486*4882a593Smuzhiyun {
3487*4882a593Smuzhiyun 	phys_addr_t phys_addr, phys_addr_next;
3488*4882a593Smuzhiyun 	dma_addr_t dma_addr, dma_addr_next;
3489*4882a593Smuzhiyun 	struct mvpp2_buff_hdr *buff_hdr;
3490*4882a593Smuzhiyun 
3491*4882a593Smuzhiyun 	phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3492*4882a593Smuzhiyun 	dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3493*4882a593Smuzhiyun 
3494*4882a593Smuzhiyun 	do {
3495*4882a593Smuzhiyun 		buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);
3496*4882a593Smuzhiyun 
3497*4882a593Smuzhiyun 		phys_addr_next = le32_to_cpu(buff_hdr->next_phys_addr);
3498*4882a593Smuzhiyun 		dma_addr_next = le32_to_cpu(buff_hdr->next_dma_addr);
3499*4882a593Smuzhiyun 
3500*4882a593Smuzhiyun 		if (port->priv->hw_version >= MVPP22) {
3501*4882a593Smuzhiyun 			phys_addr_next |= ((u64)buff_hdr->next_phys_addr_high << 32);
3502*4882a593Smuzhiyun 			dma_addr_next |= ((u64)buff_hdr->next_dma_addr_high << 32);
3503*4882a593Smuzhiyun 		}
3504*4882a593Smuzhiyun 
3505*4882a593Smuzhiyun 		mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3506*4882a593Smuzhiyun 
3507*4882a593Smuzhiyun 		phys_addr = phys_addr_next;
3508*4882a593Smuzhiyun 		dma_addr = dma_addr_next;
3509*4882a593Smuzhiyun 
3510*4882a593Smuzhiyun 	} while (!MVPP2_B_HDR_INFO_IS_LAST(le16_to_cpu(buff_hdr->info)));
3511*4882a593Smuzhiyun }
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun /* Main rx processing */
mvpp2_rx(struct mvpp2_port * port,struct napi_struct * napi,int rx_todo,struct mvpp2_rx_queue * rxq)3514*4882a593Smuzhiyun static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
3515*4882a593Smuzhiyun 		    int rx_todo, struct mvpp2_rx_queue *rxq)
3516*4882a593Smuzhiyun {
3517*4882a593Smuzhiyun 	struct net_device *dev = port->dev;
3518*4882a593Smuzhiyun 	struct mvpp2_pcpu_stats ps = {};
3519*4882a593Smuzhiyun 	enum dma_data_direction dma_dir;
3520*4882a593Smuzhiyun 	struct bpf_prog *xdp_prog;
3521*4882a593Smuzhiyun 	struct xdp_buff xdp;
3522*4882a593Smuzhiyun 	int rx_received;
3523*4882a593Smuzhiyun 	int rx_done = 0;
3524*4882a593Smuzhiyun 	u32 xdp_ret = 0;
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 	rcu_read_lock();
3527*4882a593Smuzhiyun 
3528*4882a593Smuzhiyun 	xdp_prog = READ_ONCE(port->xdp_prog);
3529*4882a593Smuzhiyun 
3530*4882a593Smuzhiyun 	/* Get number of received packets and clamp the to-do */
3531*4882a593Smuzhiyun 	rx_received = mvpp2_rxq_received(port, rxq->id);
3532*4882a593Smuzhiyun 	if (rx_todo > rx_received)
3533*4882a593Smuzhiyun 		rx_todo = rx_received;
3534*4882a593Smuzhiyun 
3535*4882a593Smuzhiyun 	while (rx_done < rx_todo) {
3536*4882a593Smuzhiyun 		struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
3537*4882a593Smuzhiyun 		struct mvpp2_bm_pool *bm_pool;
3538*4882a593Smuzhiyun 		struct page_pool *pp = NULL;
3539*4882a593Smuzhiyun 		struct sk_buff *skb;
3540*4882a593Smuzhiyun 		unsigned int frag_size;
3541*4882a593Smuzhiyun 		dma_addr_t dma_addr;
3542*4882a593Smuzhiyun 		phys_addr_t phys_addr;
3543*4882a593Smuzhiyun 		u32 rx_status, timestamp;
3544*4882a593Smuzhiyun 		int pool, rx_bytes, err, ret;
3545*4882a593Smuzhiyun 		void *data;
3546*4882a593Smuzhiyun 
3547*4882a593Smuzhiyun 		rx_done++;
3548*4882a593Smuzhiyun 		rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
3549*4882a593Smuzhiyun 		rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
3550*4882a593Smuzhiyun 		rx_bytes -= MVPP2_MH_SIZE;
3551*4882a593Smuzhiyun 		dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
3552*4882a593Smuzhiyun 		phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
3553*4882a593Smuzhiyun 		data = (void *)phys_to_virt(phys_addr);
3554*4882a593Smuzhiyun 
3555*4882a593Smuzhiyun 		pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
3556*4882a593Smuzhiyun 			MVPP2_RXD_BM_POOL_ID_OFFS;
3557*4882a593Smuzhiyun 		bm_pool = &port->priv->bm_pools[pool];
3558*4882a593Smuzhiyun 
3559*4882a593Smuzhiyun 		if (port->priv->percpu_pools) {
3560*4882a593Smuzhiyun 			pp = port->priv->page_pool[pool];
3561*4882a593Smuzhiyun 			dma_dir = page_pool_get_dma_dir(pp);
3562*4882a593Smuzhiyun 		} else {
3563*4882a593Smuzhiyun 			dma_dir = DMA_FROM_DEVICE;
3564*4882a593Smuzhiyun 		}
3565*4882a593Smuzhiyun 
3566*4882a593Smuzhiyun 		dma_sync_single_for_cpu(dev->dev.parent, dma_addr,
3567*4882a593Smuzhiyun 					rx_bytes + MVPP2_MH_SIZE,
3568*4882a593Smuzhiyun 					dma_dir);
3569*4882a593Smuzhiyun 
3570*4882a593Smuzhiyun 		/* Buffer header not supported */
3571*4882a593Smuzhiyun 		if (rx_status & MVPP2_RXD_BUF_HDR)
3572*4882a593Smuzhiyun 			goto err_drop_frame;
3573*4882a593Smuzhiyun 
3574*4882a593Smuzhiyun 		/* In case of an error, release the requested buffer pointer
3575*4882a593Smuzhiyun 		 * to the Buffer Manager. This request process is controlled
3576*4882a593Smuzhiyun 		 * by the hardware, and the information about the buffer is
3577*4882a593Smuzhiyun 		 * comprised by the RX descriptor.
3578*4882a593Smuzhiyun 		 */
3579*4882a593Smuzhiyun 		if (rx_status & MVPP2_RXD_ERR_SUMMARY)
3580*4882a593Smuzhiyun 			goto err_drop_frame;
3581*4882a593Smuzhiyun 
3582*4882a593Smuzhiyun 		/* Prefetch header */
3583*4882a593Smuzhiyun 		prefetch(data);
3584*4882a593Smuzhiyun 
3585*4882a593Smuzhiyun 		if (bm_pool->frag_size > PAGE_SIZE)
3586*4882a593Smuzhiyun 			frag_size = 0;
3587*4882a593Smuzhiyun 		else
3588*4882a593Smuzhiyun 			frag_size = bm_pool->frag_size;
3589*4882a593Smuzhiyun 
3590*4882a593Smuzhiyun 		if (xdp_prog) {
3591*4882a593Smuzhiyun 			xdp.data_hard_start = data;
3592*4882a593Smuzhiyun 			xdp.data = data + MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM;
3593*4882a593Smuzhiyun 			xdp.data_end = xdp.data + rx_bytes;
3594*4882a593Smuzhiyun 			xdp.frame_sz = PAGE_SIZE;
3595*4882a593Smuzhiyun 
3596*4882a593Smuzhiyun 			if (bm_pool->pkt_size == MVPP2_BM_SHORT_PKT_SIZE)
3597*4882a593Smuzhiyun 				xdp.rxq = &rxq->xdp_rxq_short;
3598*4882a593Smuzhiyun 			else
3599*4882a593Smuzhiyun 				xdp.rxq = &rxq->xdp_rxq_long;
3600*4882a593Smuzhiyun 
3601*4882a593Smuzhiyun 			xdp_set_data_meta_invalid(&xdp);
3602*4882a593Smuzhiyun 
3603*4882a593Smuzhiyun 			ret = mvpp2_run_xdp(port, rxq, xdp_prog, &xdp, pp, &ps);
3604*4882a593Smuzhiyun 
3605*4882a593Smuzhiyun 			if (ret) {
3606*4882a593Smuzhiyun 				xdp_ret |= ret;
3607*4882a593Smuzhiyun 				err = mvpp2_rx_refill(port, bm_pool, pp, pool);
3608*4882a593Smuzhiyun 				if (err) {
3609*4882a593Smuzhiyun 					netdev_err(port->dev, "failed to refill BM pools\n");
3610*4882a593Smuzhiyun 					goto err_drop_frame;
3611*4882a593Smuzhiyun 				}
3612*4882a593Smuzhiyun 
3613*4882a593Smuzhiyun 				ps.rx_packets++;
3614*4882a593Smuzhiyun 				ps.rx_bytes += rx_bytes;
3615*4882a593Smuzhiyun 				continue;
3616*4882a593Smuzhiyun 			}
3617*4882a593Smuzhiyun 		}
3618*4882a593Smuzhiyun 
3619*4882a593Smuzhiyun 		skb = build_skb(data, frag_size);
3620*4882a593Smuzhiyun 		if (!skb) {
3621*4882a593Smuzhiyun 			netdev_warn(port->dev, "skb build failed\n");
3622*4882a593Smuzhiyun 			goto err_drop_frame;
3623*4882a593Smuzhiyun 		}
3624*4882a593Smuzhiyun 
3625*4882a593Smuzhiyun 		/* If we have RX hardware timestamping enabled, grab the
3626*4882a593Smuzhiyun 		 * timestamp from the queue and convert.
3627*4882a593Smuzhiyun 		 */
3628*4882a593Smuzhiyun 		if (mvpp22_rx_hwtstamping(port)) {
3629*4882a593Smuzhiyun 			timestamp = le32_to_cpu(rx_desc->pp22.timestamp);
3630*4882a593Smuzhiyun 			mvpp22_tai_tstamp(port->priv->tai, timestamp,
3631*4882a593Smuzhiyun 					 skb_hwtstamps(skb));
3632*4882a593Smuzhiyun 		}
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun 		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
3635*4882a593Smuzhiyun 		if (err) {
3636*4882a593Smuzhiyun 			netdev_err(port->dev, "failed to refill BM pools\n");
3637*4882a593Smuzhiyun 			dev_kfree_skb_any(skb);
3638*4882a593Smuzhiyun 			goto err_drop_frame;
3639*4882a593Smuzhiyun 		}
3640*4882a593Smuzhiyun 
3641*4882a593Smuzhiyun 		if (pp)
3642*4882a593Smuzhiyun 			page_pool_release_page(pp, virt_to_page(data));
3643*4882a593Smuzhiyun 		else
3644*4882a593Smuzhiyun 			dma_unmap_single_attrs(dev->dev.parent, dma_addr,
3645*4882a593Smuzhiyun 					       bm_pool->buf_size, DMA_FROM_DEVICE,
3646*4882a593Smuzhiyun 					       DMA_ATTR_SKIP_CPU_SYNC);
3647*4882a593Smuzhiyun 
3648*4882a593Smuzhiyun 		ps.rx_packets++;
3649*4882a593Smuzhiyun 		ps.rx_bytes += rx_bytes;
3650*4882a593Smuzhiyun 
3651*4882a593Smuzhiyun 		skb_reserve(skb, MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
3652*4882a593Smuzhiyun 		skb_put(skb, rx_bytes);
3653*4882a593Smuzhiyun 		skb->protocol = eth_type_trans(skb, dev);
3654*4882a593Smuzhiyun 		mvpp2_rx_csum(port, rx_status, skb);
3655*4882a593Smuzhiyun 
3656*4882a593Smuzhiyun 		napi_gro_receive(napi, skb);
3657*4882a593Smuzhiyun 		continue;
3658*4882a593Smuzhiyun 
3659*4882a593Smuzhiyun err_drop_frame:
3660*4882a593Smuzhiyun 		dev->stats.rx_errors++;
3661*4882a593Smuzhiyun 		mvpp2_rx_error(port, rx_desc);
3662*4882a593Smuzhiyun 		/* Return the buffer to the pool */
3663*4882a593Smuzhiyun 		if (rx_status & MVPP2_RXD_BUF_HDR)
3664*4882a593Smuzhiyun 			mvpp2_buff_hdr_pool_put(port, rx_desc, pool, rx_status);
3665*4882a593Smuzhiyun 		else
3666*4882a593Smuzhiyun 			mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
3667*4882a593Smuzhiyun 	}
3668*4882a593Smuzhiyun 
3669*4882a593Smuzhiyun 	rcu_read_unlock();
3670*4882a593Smuzhiyun 
3671*4882a593Smuzhiyun 	if (xdp_ret & MVPP2_XDP_REDIR)
3672*4882a593Smuzhiyun 		xdp_do_flush_map();
3673*4882a593Smuzhiyun 
3674*4882a593Smuzhiyun 	if (ps.rx_packets) {
3675*4882a593Smuzhiyun 		struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
3676*4882a593Smuzhiyun 
3677*4882a593Smuzhiyun 		u64_stats_update_begin(&stats->syncp);
3678*4882a593Smuzhiyun 		stats->rx_packets += ps.rx_packets;
3679*4882a593Smuzhiyun 		stats->rx_bytes   += ps.rx_bytes;
3680*4882a593Smuzhiyun 		/* xdp */
3681*4882a593Smuzhiyun 		stats->xdp_redirect += ps.xdp_redirect;
3682*4882a593Smuzhiyun 		stats->xdp_pass += ps.xdp_pass;
3683*4882a593Smuzhiyun 		stats->xdp_drop += ps.xdp_drop;
3684*4882a593Smuzhiyun 		u64_stats_update_end(&stats->syncp);
3685*4882a593Smuzhiyun 	}
3686*4882a593Smuzhiyun 
3687*4882a593Smuzhiyun 	/* Update Rx queue management counters */
3688*4882a593Smuzhiyun 	wmb();
3689*4882a593Smuzhiyun 	mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
3690*4882a593Smuzhiyun 
3691*4882a593Smuzhiyun 	return rx_todo;
3692*4882a593Smuzhiyun }
3693*4882a593Smuzhiyun 
3694*4882a593Smuzhiyun static inline void
tx_desc_unmap_put(struct mvpp2_port * port,struct mvpp2_tx_queue * txq,struct mvpp2_tx_desc * desc)3695*4882a593Smuzhiyun tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
3696*4882a593Smuzhiyun 		  struct mvpp2_tx_desc *desc)
3697*4882a593Smuzhiyun {
3698*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3699*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3700*4882a593Smuzhiyun 
3701*4882a593Smuzhiyun 	dma_addr_t buf_dma_addr =
3702*4882a593Smuzhiyun 		mvpp2_txdesc_dma_addr_get(port, desc);
3703*4882a593Smuzhiyun 	size_t buf_sz =
3704*4882a593Smuzhiyun 		mvpp2_txdesc_size_get(port, desc);
3705*4882a593Smuzhiyun 	if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
3706*4882a593Smuzhiyun 		dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
3707*4882a593Smuzhiyun 				 buf_sz, DMA_TO_DEVICE);
3708*4882a593Smuzhiyun 	mvpp2_txq_desc_put(txq);
3709*4882a593Smuzhiyun }
3710*4882a593Smuzhiyun 
mvpp2_txdesc_clear_ptp(struct mvpp2_port * port,struct mvpp2_tx_desc * desc)3711*4882a593Smuzhiyun static void mvpp2_txdesc_clear_ptp(struct mvpp2_port *port,
3712*4882a593Smuzhiyun 				   struct mvpp2_tx_desc *desc)
3713*4882a593Smuzhiyun {
3714*4882a593Smuzhiyun 	/* We only need to clear the low bits */
3715*4882a593Smuzhiyun 	if (port->priv->hw_version != MVPP21)
3716*4882a593Smuzhiyun 		desc->pp22.ptp_descriptor &=
3717*4882a593Smuzhiyun 			cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
3718*4882a593Smuzhiyun }
3719*4882a593Smuzhiyun 
mvpp2_tx_hw_tstamp(struct mvpp2_port * port,struct mvpp2_tx_desc * tx_desc,struct sk_buff * skb)3720*4882a593Smuzhiyun static bool mvpp2_tx_hw_tstamp(struct mvpp2_port *port,
3721*4882a593Smuzhiyun 			       struct mvpp2_tx_desc *tx_desc,
3722*4882a593Smuzhiyun 			       struct sk_buff *skb)
3723*4882a593Smuzhiyun {
3724*4882a593Smuzhiyun 	struct mvpp2_hwtstamp_queue *queue;
3725*4882a593Smuzhiyun 	unsigned int mtype, type, i;
3726*4882a593Smuzhiyun 	struct ptp_header *hdr;
3727*4882a593Smuzhiyun 	u64 ptpdesc;
3728*4882a593Smuzhiyun 
3729*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21 ||
3730*4882a593Smuzhiyun 	    port->tx_hwtstamp_type == HWTSTAMP_TX_OFF)
3731*4882a593Smuzhiyun 		return false;
3732*4882a593Smuzhiyun 
3733*4882a593Smuzhiyun 	type = ptp_classify_raw(skb);
3734*4882a593Smuzhiyun 	if (!type)
3735*4882a593Smuzhiyun 		return false;
3736*4882a593Smuzhiyun 
3737*4882a593Smuzhiyun 	hdr = ptp_parse_header(skb, type);
3738*4882a593Smuzhiyun 	if (!hdr)
3739*4882a593Smuzhiyun 		return false;
3740*4882a593Smuzhiyun 
3741*4882a593Smuzhiyun 	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3742*4882a593Smuzhiyun 
3743*4882a593Smuzhiyun 	ptpdesc = MVPP22_PTP_MACTIMESTAMPINGEN |
3744*4882a593Smuzhiyun 		  MVPP22_PTP_ACTION_CAPTURE;
3745*4882a593Smuzhiyun 	queue = &port->tx_hwtstamp_queue[0];
3746*4882a593Smuzhiyun 
3747*4882a593Smuzhiyun 	switch (type & PTP_CLASS_VMASK) {
3748*4882a593Smuzhiyun 	case PTP_CLASS_V1:
3749*4882a593Smuzhiyun 		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV1);
3750*4882a593Smuzhiyun 		break;
3751*4882a593Smuzhiyun 
3752*4882a593Smuzhiyun 	case PTP_CLASS_V2:
3753*4882a593Smuzhiyun 		ptpdesc |= MVPP22_PTP_PACKETFORMAT(MVPP22_PTP_PKT_FMT_PTPV2);
3754*4882a593Smuzhiyun 		mtype = hdr->tsmt & 15;
3755*4882a593Smuzhiyun 		/* Direct PTP Sync messages to queue 1 */
3756*4882a593Smuzhiyun 		if (mtype == 0) {
3757*4882a593Smuzhiyun 			ptpdesc |= MVPP22_PTP_TIMESTAMPQUEUESELECT;
3758*4882a593Smuzhiyun 			queue = &port->tx_hwtstamp_queue[1];
3759*4882a593Smuzhiyun 		}
3760*4882a593Smuzhiyun 		break;
3761*4882a593Smuzhiyun 	}
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 	/* Take a reference on the skb and insert into our queue */
3764*4882a593Smuzhiyun 	i = queue->next;
3765*4882a593Smuzhiyun 	queue->next = (i + 1) & 31;
3766*4882a593Smuzhiyun 	if (queue->skb[i])
3767*4882a593Smuzhiyun 		dev_kfree_skb_any(queue->skb[i]);
3768*4882a593Smuzhiyun 	queue->skb[i] = skb_get(skb);
3769*4882a593Smuzhiyun 
3770*4882a593Smuzhiyun 	ptpdesc |= MVPP22_PTP_TIMESTAMPENTRYID(i);
3771*4882a593Smuzhiyun 
3772*4882a593Smuzhiyun 	/*
3773*4882a593Smuzhiyun 	 * 3:0		- PTPAction
3774*4882a593Smuzhiyun 	 * 6:4		- PTPPacketFormat
3775*4882a593Smuzhiyun 	 * 7		- PTP_CF_WraparoundCheckEn
3776*4882a593Smuzhiyun 	 * 9:8		- IngressTimestampSeconds[1:0]
3777*4882a593Smuzhiyun 	 * 10		- Reserved
3778*4882a593Smuzhiyun 	 * 11		- MACTimestampingEn
3779*4882a593Smuzhiyun 	 * 17:12	- PTP_TimestampQueueEntryID[5:0]
3780*4882a593Smuzhiyun 	 * 18		- PTPTimestampQueueSelect
3781*4882a593Smuzhiyun 	 * 19		- UDPChecksumUpdateEn
3782*4882a593Smuzhiyun 	 * 27:20	- TimestampOffset
3783*4882a593Smuzhiyun 	 *			PTP, NTPTransmit, OWAMP/TWAMP - L3 to PTP header
3784*4882a593Smuzhiyun 	 *			NTPTs, Y.1731 - L3 to timestamp entry
3785*4882a593Smuzhiyun 	 * 35:28	- UDP Checksum Offset
3786*4882a593Smuzhiyun 	 *
3787*4882a593Smuzhiyun 	 * stored in tx descriptor bits 75:64 (11:0) and 191:168 (35:12)
3788*4882a593Smuzhiyun 	 */
3789*4882a593Smuzhiyun 	tx_desc->pp22.ptp_descriptor &=
3790*4882a593Smuzhiyun 		cpu_to_le32(~MVPP22_PTP_DESC_MASK_LOW);
3791*4882a593Smuzhiyun 	tx_desc->pp22.ptp_descriptor |=
3792*4882a593Smuzhiyun 		cpu_to_le32(ptpdesc & MVPP22_PTP_DESC_MASK_LOW);
3793*4882a593Smuzhiyun 	tx_desc->pp22.buf_dma_addr_ptp &= cpu_to_le64(~0xffffff0000000000ULL);
3794*4882a593Smuzhiyun 	tx_desc->pp22.buf_dma_addr_ptp |= cpu_to_le64((ptpdesc >> 12) << 40);
3795*4882a593Smuzhiyun 
3796*4882a593Smuzhiyun 	return true;
3797*4882a593Smuzhiyun }
3798*4882a593Smuzhiyun 
3799*4882a593Smuzhiyun /* Handle tx fragmentation processing */
mvpp2_tx_frag_process(struct mvpp2_port * port,struct sk_buff * skb,struct mvpp2_tx_queue * aggr_txq,struct mvpp2_tx_queue * txq)3800*4882a593Smuzhiyun static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
3801*4882a593Smuzhiyun 				 struct mvpp2_tx_queue *aggr_txq,
3802*4882a593Smuzhiyun 				 struct mvpp2_tx_queue *txq)
3803*4882a593Smuzhiyun {
3804*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3805*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3806*4882a593Smuzhiyun 	struct mvpp2_tx_desc *tx_desc;
3807*4882a593Smuzhiyun 	int i;
3808*4882a593Smuzhiyun 	dma_addr_t buf_dma_addr;
3809*4882a593Smuzhiyun 
3810*4882a593Smuzhiyun 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3811*4882a593Smuzhiyun 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3812*4882a593Smuzhiyun 		void *addr = skb_frag_address(frag);
3813*4882a593Smuzhiyun 
3814*4882a593Smuzhiyun 		tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3815*4882a593Smuzhiyun 		mvpp2_txdesc_clear_ptp(port, tx_desc);
3816*4882a593Smuzhiyun 		mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3817*4882a593Smuzhiyun 		mvpp2_txdesc_size_set(port, tx_desc, skb_frag_size(frag));
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun 		buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
3820*4882a593Smuzhiyun 					      skb_frag_size(frag),
3821*4882a593Smuzhiyun 					      DMA_TO_DEVICE);
3822*4882a593Smuzhiyun 		if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
3823*4882a593Smuzhiyun 			mvpp2_txq_desc_put(txq);
3824*4882a593Smuzhiyun 			goto cleanup;
3825*4882a593Smuzhiyun 		}
3826*4882a593Smuzhiyun 
3827*4882a593Smuzhiyun 		mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3828*4882a593Smuzhiyun 
3829*4882a593Smuzhiyun 		if (i == (skb_shinfo(skb)->nr_frags - 1)) {
3830*4882a593Smuzhiyun 			/* Last descriptor */
3831*4882a593Smuzhiyun 			mvpp2_txdesc_cmd_set(port, tx_desc,
3832*4882a593Smuzhiyun 					     MVPP2_TXD_L_DESC);
3833*4882a593Smuzhiyun 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
3834*4882a593Smuzhiyun 		} else {
3835*4882a593Smuzhiyun 			/* Descriptor in the middle: Not First, Not Last */
3836*4882a593Smuzhiyun 			mvpp2_txdesc_cmd_set(port, tx_desc, 0);
3837*4882a593Smuzhiyun 			mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3838*4882a593Smuzhiyun 		}
3839*4882a593Smuzhiyun 	}
3840*4882a593Smuzhiyun 
3841*4882a593Smuzhiyun 	return 0;
3842*4882a593Smuzhiyun cleanup:
3843*4882a593Smuzhiyun 	/* Release all descriptors that were used to map fragments of
3844*4882a593Smuzhiyun 	 * this packet, as well as the corresponding DMA mappings
3845*4882a593Smuzhiyun 	 */
3846*4882a593Smuzhiyun 	for (i = i - 1; i >= 0; i--) {
3847*4882a593Smuzhiyun 		tx_desc = txq->descs + i;
3848*4882a593Smuzhiyun 		tx_desc_unmap_put(port, txq, tx_desc);
3849*4882a593Smuzhiyun 	}
3850*4882a593Smuzhiyun 
3851*4882a593Smuzhiyun 	return -ENOMEM;
3852*4882a593Smuzhiyun }
3853*4882a593Smuzhiyun 
mvpp2_tso_put_hdr(struct sk_buff * skb,struct net_device * dev,struct mvpp2_tx_queue * txq,struct mvpp2_tx_queue * aggr_txq,struct mvpp2_txq_pcpu * txq_pcpu,int hdr_sz)3854*4882a593Smuzhiyun static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
3855*4882a593Smuzhiyun 				     struct net_device *dev,
3856*4882a593Smuzhiyun 				     struct mvpp2_tx_queue *txq,
3857*4882a593Smuzhiyun 				     struct mvpp2_tx_queue *aggr_txq,
3858*4882a593Smuzhiyun 				     struct mvpp2_txq_pcpu *txq_pcpu,
3859*4882a593Smuzhiyun 				     int hdr_sz)
3860*4882a593Smuzhiyun {
3861*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
3862*4882a593Smuzhiyun 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3863*4882a593Smuzhiyun 	dma_addr_t addr;
3864*4882a593Smuzhiyun 
3865*4882a593Smuzhiyun 	mvpp2_txdesc_clear_ptp(port, tx_desc);
3866*4882a593Smuzhiyun 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3867*4882a593Smuzhiyun 	mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
3868*4882a593Smuzhiyun 
3869*4882a593Smuzhiyun 	addr = txq_pcpu->tso_headers_dma +
3870*4882a593Smuzhiyun 	       txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
3871*4882a593Smuzhiyun 	mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
3872*4882a593Smuzhiyun 
3873*4882a593Smuzhiyun 	mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
3874*4882a593Smuzhiyun 					    MVPP2_TXD_F_DESC |
3875*4882a593Smuzhiyun 					    MVPP2_TXD_PADDING_DISABLE);
3876*4882a593Smuzhiyun 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3877*4882a593Smuzhiyun }
3878*4882a593Smuzhiyun 
mvpp2_tso_put_data(struct sk_buff * skb,struct net_device * dev,struct tso_t * tso,struct mvpp2_tx_queue * txq,struct mvpp2_tx_queue * aggr_txq,struct mvpp2_txq_pcpu * txq_pcpu,int sz,bool left,bool last)3879*4882a593Smuzhiyun static inline int mvpp2_tso_put_data(struct sk_buff *skb,
3880*4882a593Smuzhiyun 				     struct net_device *dev, struct tso_t *tso,
3881*4882a593Smuzhiyun 				     struct mvpp2_tx_queue *txq,
3882*4882a593Smuzhiyun 				     struct mvpp2_tx_queue *aggr_txq,
3883*4882a593Smuzhiyun 				     struct mvpp2_txq_pcpu *txq_pcpu,
3884*4882a593Smuzhiyun 				     int sz, bool left, bool last)
3885*4882a593Smuzhiyun {
3886*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
3887*4882a593Smuzhiyun 	struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
3888*4882a593Smuzhiyun 	dma_addr_t buf_dma_addr;
3889*4882a593Smuzhiyun 
3890*4882a593Smuzhiyun 	mvpp2_txdesc_clear_ptp(port, tx_desc);
3891*4882a593Smuzhiyun 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
3892*4882a593Smuzhiyun 	mvpp2_txdesc_size_set(port, tx_desc, sz);
3893*4882a593Smuzhiyun 
3894*4882a593Smuzhiyun 	buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
3895*4882a593Smuzhiyun 				      DMA_TO_DEVICE);
3896*4882a593Smuzhiyun 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
3897*4882a593Smuzhiyun 		mvpp2_txq_desc_put(txq);
3898*4882a593Smuzhiyun 		return -ENOMEM;
3899*4882a593Smuzhiyun 	}
3900*4882a593Smuzhiyun 
3901*4882a593Smuzhiyun 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
3902*4882a593Smuzhiyun 
3903*4882a593Smuzhiyun 	if (!left) {
3904*4882a593Smuzhiyun 		mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
3905*4882a593Smuzhiyun 		if (last) {
3906*4882a593Smuzhiyun 			mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
3907*4882a593Smuzhiyun 			return 0;
3908*4882a593Smuzhiyun 		}
3909*4882a593Smuzhiyun 	} else {
3910*4882a593Smuzhiyun 		mvpp2_txdesc_cmd_set(port, tx_desc, 0);
3911*4882a593Smuzhiyun 	}
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun 	mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
3914*4882a593Smuzhiyun 	return 0;
3915*4882a593Smuzhiyun }
3916*4882a593Smuzhiyun 
mvpp2_tx_tso(struct sk_buff * skb,struct net_device * dev,struct mvpp2_tx_queue * txq,struct mvpp2_tx_queue * aggr_txq,struct mvpp2_txq_pcpu * txq_pcpu)3917*4882a593Smuzhiyun static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
3918*4882a593Smuzhiyun 			struct mvpp2_tx_queue *txq,
3919*4882a593Smuzhiyun 			struct mvpp2_tx_queue *aggr_txq,
3920*4882a593Smuzhiyun 			struct mvpp2_txq_pcpu *txq_pcpu)
3921*4882a593Smuzhiyun {
3922*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
3923*4882a593Smuzhiyun 	int hdr_sz, i, len, descs = 0;
3924*4882a593Smuzhiyun 	struct tso_t tso;
3925*4882a593Smuzhiyun 
3926*4882a593Smuzhiyun 	/* Check number of available descriptors */
3927*4882a593Smuzhiyun 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, tso_count_descs(skb)) ||
3928*4882a593Smuzhiyun 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu,
3929*4882a593Smuzhiyun 					     tso_count_descs(skb)))
3930*4882a593Smuzhiyun 		return 0;
3931*4882a593Smuzhiyun 
3932*4882a593Smuzhiyun 	hdr_sz = tso_start(skb, &tso);
3933*4882a593Smuzhiyun 
3934*4882a593Smuzhiyun 	len = skb->len - hdr_sz;
3935*4882a593Smuzhiyun 	while (len > 0) {
3936*4882a593Smuzhiyun 		int left = min_t(int, skb_shinfo(skb)->gso_size, len);
3937*4882a593Smuzhiyun 		char *hdr = txq_pcpu->tso_headers +
3938*4882a593Smuzhiyun 			    txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
3939*4882a593Smuzhiyun 
3940*4882a593Smuzhiyun 		len -= left;
3941*4882a593Smuzhiyun 		descs++;
3942*4882a593Smuzhiyun 
3943*4882a593Smuzhiyun 		tso_build_hdr(skb, hdr, &tso, left, len == 0);
3944*4882a593Smuzhiyun 		mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
3945*4882a593Smuzhiyun 
3946*4882a593Smuzhiyun 		while (left > 0) {
3947*4882a593Smuzhiyun 			int sz = min_t(int, tso.size, left);
3948*4882a593Smuzhiyun 			left -= sz;
3949*4882a593Smuzhiyun 			descs++;
3950*4882a593Smuzhiyun 
3951*4882a593Smuzhiyun 			if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
3952*4882a593Smuzhiyun 					       txq_pcpu, sz, left, len == 0))
3953*4882a593Smuzhiyun 				goto release;
3954*4882a593Smuzhiyun 			tso_build_data(skb, &tso, sz);
3955*4882a593Smuzhiyun 		}
3956*4882a593Smuzhiyun 	}
3957*4882a593Smuzhiyun 
3958*4882a593Smuzhiyun 	return descs;
3959*4882a593Smuzhiyun 
3960*4882a593Smuzhiyun release:
3961*4882a593Smuzhiyun 	for (i = descs - 1; i >= 0; i--) {
3962*4882a593Smuzhiyun 		struct mvpp2_tx_desc *tx_desc = txq->descs + i;
3963*4882a593Smuzhiyun 		tx_desc_unmap_put(port, txq, tx_desc);
3964*4882a593Smuzhiyun 	}
3965*4882a593Smuzhiyun 	return 0;
3966*4882a593Smuzhiyun }
3967*4882a593Smuzhiyun 
3968*4882a593Smuzhiyun /* Main tx processing */
mvpp2_tx(struct sk_buff * skb,struct net_device * dev)3969*4882a593Smuzhiyun static netdev_tx_t mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
3970*4882a593Smuzhiyun {
3971*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
3972*4882a593Smuzhiyun 	struct mvpp2_tx_queue *txq, *aggr_txq;
3973*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
3974*4882a593Smuzhiyun 	struct mvpp2_tx_desc *tx_desc;
3975*4882a593Smuzhiyun 	dma_addr_t buf_dma_addr;
3976*4882a593Smuzhiyun 	unsigned long flags = 0;
3977*4882a593Smuzhiyun 	unsigned int thread;
3978*4882a593Smuzhiyun 	int frags = 0;
3979*4882a593Smuzhiyun 	u16 txq_id;
3980*4882a593Smuzhiyun 	u32 tx_cmd;
3981*4882a593Smuzhiyun 
3982*4882a593Smuzhiyun 	thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
3983*4882a593Smuzhiyun 
3984*4882a593Smuzhiyun 	txq_id = skb_get_queue_mapping(skb);
3985*4882a593Smuzhiyun 	txq = port->txqs[txq_id];
3986*4882a593Smuzhiyun 	txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
3987*4882a593Smuzhiyun 	aggr_txq = &port->priv->aggr_txqs[thread];
3988*4882a593Smuzhiyun 
3989*4882a593Smuzhiyun 	if (test_bit(thread, &port->priv->lock_map))
3990*4882a593Smuzhiyun 		spin_lock_irqsave(&port->tx_lock[thread], flags);
3991*4882a593Smuzhiyun 
3992*4882a593Smuzhiyun 	if (skb_is_gso(skb)) {
3993*4882a593Smuzhiyun 		frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
3994*4882a593Smuzhiyun 		goto out;
3995*4882a593Smuzhiyun 	}
3996*4882a593Smuzhiyun 	frags = skb_shinfo(skb)->nr_frags + 1;
3997*4882a593Smuzhiyun 
3998*4882a593Smuzhiyun 	/* Check number of available descriptors */
3999*4882a593Smuzhiyun 	if (mvpp2_aggr_desc_num_check(port, aggr_txq, frags) ||
4000*4882a593Smuzhiyun 	    mvpp2_txq_reserved_desc_num_proc(port, txq, txq_pcpu, frags)) {
4001*4882a593Smuzhiyun 		frags = 0;
4002*4882a593Smuzhiyun 		goto out;
4003*4882a593Smuzhiyun 	}
4004*4882a593Smuzhiyun 
4005*4882a593Smuzhiyun 	/* Get a descriptor for the first part of the packet */
4006*4882a593Smuzhiyun 	tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
4007*4882a593Smuzhiyun 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
4008*4882a593Smuzhiyun 	    !mvpp2_tx_hw_tstamp(port, tx_desc, skb))
4009*4882a593Smuzhiyun 		mvpp2_txdesc_clear_ptp(port, tx_desc);
4010*4882a593Smuzhiyun 	mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
4011*4882a593Smuzhiyun 	mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
4012*4882a593Smuzhiyun 
4013*4882a593Smuzhiyun 	buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
4014*4882a593Smuzhiyun 				      skb_headlen(skb), DMA_TO_DEVICE);
4015*4882a593Smuzhiyun 	if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
4016*4882a593Smuzhiyun 		mvpp2_txq_desc_put(txq);
4017*4882a593Smuzhiyun 		frags = 0;
4018*4882a593Smuzhiyun 		goto out;
4019*4882a593Smuzhiyun 	}
4020*4882a593Smuzhiyun 
4021*4882a593Smuzhiyun 	mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
4022*4882a593Smuzhiyun 
4023*4882a593Smuzhiyun 	tx_cmd = mvpp2_skb_tx_csum(port, skb);
4024*4882a593Smuzhiyun 
4025*4882a593Smuzhiyun 	if (frags == 1) {
4026*4882a593Smuzhiyun 		/* First and Last descriptor */
4027*4882a593Smuzhiyun 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
4028*4882a593Smuzhiyun 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4029*4882a593Smuzhiyun 		mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc, MVPP2_TYPE_SKB);
4030*4882a593Smuzhiyun 	} else {
4031*4882a593Smuzhiyun 		/* First but not Last */
4032*4882a593Smuzhiyun 		tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
4033*4882a593Smuzhiyun 		mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
4034*4882a593Smuzhiyun 		mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc, MVPP2_TYPE_SKB);
4035*4882a593Smuzhiyun 
4036*4882a593Smuzhiyun 		/* Continue with other skb fragments */
4037*4882a593Smuzhiyun 		if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
4038*4882a593Smuzhiyun 			tx_desc_unmap_put(port, txq, tx_desc);
4039*4882a593Smuzhiyun 			frags = 0;
4040*4882a593Smuzhiyun 		}
4041*4882a593Smuzhiyun 	}
4042*4882a593Smuzhiyun 
4043*4882a593Smuzhiyun out:
4044*4882a593Smuzhiyun 	if (frags > 0) {
4045*4882a593Smuzhiyun 		struct mvpp2_pcpu_stats *stats = per_cpu_ptr(port->stats, thread);
4046*4882a593Smuzhiyun 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
4047*4882a593Smuzhiyun 
4048*4882a593Smuzhiyun 		txq_pcpu->reserved_num -= frags;
4049*4882a593Smuzhiyun 		txq_pcpu->count += frags;
4050*4882a593Smuzhiyun 		aggr_txq->count += frags;
4051*4882a593Smuzhiyun 
4052*4882a593Smuzhiyun 		/* Enable transmit */
4053*4882a593Smuzhiyun 		wmb();
4054*4882a593Smuzhiyun 		mvpp2_aggr_txq_pend_desc_add(port, frags);
4055*4882a593Smuzhiyun 
4056*4882a593Smuzhiyun 		if (txq_pcpu->count >= txq_pcpu->stop_threshold)
4057*4882a593Smuzhiyun 			netif_tx_stop_queue(nq);
4058*4882a593Smuzhiyun 
4059*4882a593Smuzhiyun 		u64_stats_update_begin(&stats->syncp);
4060*4882a593Smuzhiyun 		stats->tx_packets++;
4061*4882a593Smuzhiyun 		stats->tx_bytes += skb->len;
4062*4882a593Smuzhiyun 		u64_stats_update_end(&stats->syncp);
4063*4882a593Smuzhiyun 	} else {
4064*4882a593Smuzhiyun 		dev->stats.tx_dropped++;
4065*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
4066*4882a593Smuzhiyun 	}
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 	/* Finalize TX processing */
4069*4882a593Smuzhiyun 	if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
4070*4882a593Smuzhiyun 		mvpp2_txq_done(port, txq, txq_pcpu);
4071*4882a593Smuzhiyun 
4072*4882a593Smuzhiyun 	/* Set the timer in case not all frags were processed */
4073*4882a593Smuzhiyun 	if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
4074*4882a593Smuzhiyun 	    txq_pcpu->count > 0) {
4075*4882a593Smuzhiyun 		struct mvpp2_port_pcpu *port_pcpu = per_cpu_ptr(port->pcpu, thread);
4076*4882a593Smuzhiyun 
4077*4882a593Smuzhiyun 		if (!port_pcpu->timer_scheduled) {
4078*4882a593Smuzhiyun 			port_pcpu->timer_scheduled = true;
4079*4882a593Smuzhiyun 			hrtimer_start(&port_pcpu->tx_done_timer,
4080*4882a593Smuzhiyun 				      MVPP2_TXDONE_HRTIMER_PERIOD_NS,
4081*4882a593Smuzhiyun 				      HRTIMER_MODE_REL_PINNED_SOFT);
4082*4882a593Smuzhiyun 		}
4083*4882a593Smuzhiyun 	}
4084*4882a593Smuzhiyun 
4085*4882a593Smuzhiyun 	if (test_bit(thread, &port->priv->lock_map))
4086*4882a593Smuzhiyun 		spin_unlock_irqrestore(&port->tx_lock[thread], flags);
4087*4882a593Smuzhiyun 
4088*4882a593Smuzhiyun 	return NETDEV_TX_OK;
4089*4882a593Smuzhiyun }
4090*4882a593Smuzhiyun 
mvpp2_cause_error(struct net_device * dev,int cause)4091*4882a593Smuzhiyun static inline void mvpp2_cause_error(struct net_device *dev, int cause)
4092*4882a593Smuzhiyun {
4093*4882a593Smuzhiyun 	if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
4094*4882a593Smuzhiyun 		netdev_err(dev, "FCS error\n");
4095*4882a593Smuzhiyun 	if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
4096*4882a593Smuzhiyun 		netdev_err(dev, "rx fifo overrun error\n");
4097*4882a593Smuzhiyun 	if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
4098*4882a593Smuzhiyun 		netdev_err(dev, "tx fifo underrun error\n");
4099*4882a593Smuzhiyun }
4100*4882a593Smuzhiyun 
mvpp2_poll(struct napi_struct * napi,int budget)4101*4882a593Smuzhiyun static int mvpp2_poll(struct napi_struct *napi, int budget)
4102*4882a593Smuzhiyun {
4103*4882a593Smuzhiyun 	u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
4104*4882a593Smuzhiyun 	int rx_done = 0;
4105*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(napi->dev);
4106*4882a593Smuzhiyun 	struct mvpp2_queue_vector *qv;
4107*4882a593Smuzhiyun 	unsigned int thread = mvpp2_cpu_to_thread(port->priv, smp_processor_id());
4108*4882a593Smuzhiyun 
4109*4882a593Smuzhiyun 	qv = container_of(napi, struct mvpp2_queue_vector, napi);
4110*4882a593Smuzhiyun 
4111*4882a593Smuzhiyun 	/* Rx/Tx cause register
4112*4882a593Smuzhiyun 	 *
4113*4882a593Smuzhiyun 	 * Bits 0-15: each bit indicates received packets on the Rx queue
4114*4882a593Smuzhiyun 	 * (bit 0 is for Rx queue 0).
4115*4882a593Smuzhiyun 	 *
4116*4882a593Smuzhiyun 	 * Bits 16-23: each bit indicates transmitted packets on the Tx queue
4117*4882a593Smuzhiyun 	 * (bit 16 is for Tx queue 0).
4118*4882a593Smuzhiyun 	 *
4119*4882a593Smuzhiyun 	 * Each CPU has its own Rx/Tx cause register
4120*4882a593Smuzhiyun 	 */
4121*4882a593Smuzhiyun 	cause_rx_tx = mvpp2_thread_read_relaxed(port->priv, qv->sw_thread_id,
4122*4882a593Smuzhiyun 						MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
4123*4882a593Smuzhiyun 
4124*4882a593Smuzhiyun 	cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
4125*4882a593Smuzhiyun 	if (cause_misc) {
4126*4882a593Smuzhiyun 		mvpp2_cause_error(port->dev, cause_misc);
4127*4882a593Smuzhiyun 
4128*4882a593Smuzhiyun 		/* Clear the cause register */
4129*4882a593Smuzhiyun 		mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
4130*4882a593Smuzhiyun 		mvpp2_thread_write(port->priv, thread,
4131*4882a593Smuzhiyun 				   MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
4132*4882a593Smuzhiyun 				   cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
4133*4882a593Smuzhiyun 	}
4134*4882a593Smuzhiyun 
4135*4882a593Smuzhiyun 	if (port->has_tx_irqs) {
4136*4882a593Smuzhiyun 		cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4137*4882a593Smuzhiyun 		if (cause_tx) {
4138*4882a593Smuzhiyun 			cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
4139*4882a593Smuzhiyun 			mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
4140*4882a593Smuzhiyun 		}
4141*4882a593Smuzhiyun 	}
4142*4882a593Smuzhiyun 
4143*4882a593Smuzhiyun 	/* Process RX packets */
4144*4882a593Smuzhiyun 	cause_rx = cause_rx_tx &
4145*4882a593Smuzhiyun 		   MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK(port->priv->hw_version);
4146*4882a593Smuzhiyun 	cause_rx <<= qv->first_rxq;
4147*4882a593Smuzhiyun 	cause_rx |= qv->pending_cause_rx;
4148*4882a593Smuzhiyun 	while (cause_rx && budget > 0) {
4149*4882a593Smuzhiyun 		int count;
4150*4882a593Smuzhiyun 		struct mvpp2_rx_queue *rxq;
4151*4882a593Smuzhiyun 
4152*4882a593Smuzhiyun 		rxq = mvpp2_get_rx_queue(port, cause_rx);
4153*4882a593Smuzhiyun 		if (!rxq)
4154*4882a593Smuzhiyun 			break;
4155*4882a593Smuzhiyun 
4156*4882a593Smuzhiyun 		count = mvpp2_rx(port, napi, budget, rxq);
4157*4882a593Smuzhiyun 		rx_done += count;
4158*4882a593Smuzhiyun 		budget -= count;
4159*4882a593Smuzhiyun 		if (budget > 0) {
4160*4882a593Smuzhiyun 			/* Clear the bit associated to this Rx queue
4161*4882a593Smuzhiyun 			 * so that next iteration will continue from
4162*4882a593Smuzhiyun 			 * the next Rx queue.
4163*4882a593Smuzhiyun 			 */
4164*4882a593Smuzhiyun 			cause_rx &= ~(1 << rxq->logic_rxq);
4165*4882a593Smuzhiyun 		}
4166*4882a593Smuzhiyun 	}
4167*4882a593Smuzhiyun 
4168*4882a593Smuzhiyun 	if (budget > 0) {
4169*4882a593Smuzhiyun 		cause_rx = 0;
4170*4882a593Smuzhiyun 		napi_complete_done(napi, rx_done);
4171*4882a593Smuzhiyun 
4172*4882a593Smuzhiyun 		mvpp2_qvec_interrupt_enable(qv);
4173*4882a593Smuzhiyun 	}
4174*4882a593Smuzhiyun 	qv->pending_cause_rx = cause_rx;
4175*4882a593Smuzhiyun 	return rx_done;
4176*4882a593Smuzhiyun }
4177*4882a593Smuzhiyun 
mvpp22_mode_reconfigure(struct mvpp2_port * port)4178*4882a593Smuzhiyun static void mvpp22_mode_reconfigure(struct mvpp2_port *port)
4179*4882a593Smuzhiyun {
4180*4882a593Smuzhiyun 	u32 ctrl3;
4181*4882a593Smuzhiyun 
4182*4882a593Smuzhiyun 	/* Set the GMAC & XLG MAC in reset */
4183*4882a593Smuzhiyun 	mvpp2_mac_reset_assert(port);
4184*4882a593Smuzhiyun 
4185*4882a593Smuzhiyun 	/* Set the MPCS and XPCS in reset */
4186*4882a593Smuzhiyun 	mvpp22_pcs_reset_assert(port);
4187*4882a593Smuzhiyun 
4188*4882a593Smuzhiyun 	/* comphy reconfiguration */
4189*4882a593Smuzhiyun 	mvpp22_comphy_init(port);
4190*4882a593Smuzhiyun 
4191*4882a593Smuzhiyun 	/* gop reconfiguration */
4192*4882a593Smuzhiyun 	mvpp22_gop_init(port);
4193*4882a593Smuzhiyun 
4194*4882a593Smuzhiyun 	mvpp22_pcs_reset_deassert(port);
4195*4882a593Smuzhiyun 
4196*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port)) {
4197*4882a593Smuzhiyun 		ctrl3 = readl(port->base + MVPP22_XLG_CTRL3_REG);
4198*4882a593Smuzhiyun 		ctrl3 &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4199*4882a593Smuzhiyun 
4200*4882a593Smuzhiyun 		if (mvpp2_is_xlg(port->phy_interface))
4201*4882a593Smuzhiyun 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4202*4882a593Smuzhiyun 		else
4203*4882a593Smuzhiyun 			ctrl3 |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4204*4882a593Smuzhiyun 
4205*4882a593Smuzhiyun 		writel(ctrl3, port->base + MVPP22_XLG_CTRL3_REG);
4206*4882a593Smuzhiyun 	}
4207*4882a593Smuzhiyun 
4208*4882a593Smuzhiyun 	if (mvpp2_port_supports_xlg(port) && mvpp2_is_xlg(port->phy_interface))
4209*4882a593Smuzhiyun 		mvpp2_xlg_max_rx_size_set(port);
4210*4882a593Smuzhiyun 	else
4211*4882a593Smuzhiyun 		mvpp2_gmac_max_rx_size_set(port);
4212*4882a593Smuzhiyun }
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun /* Set hw internals when starting port */
mvpp2_start_dev(struct mvpp2_port * port)4215*4882a593Smuzhiyun static void mvpp2_start_dev(struct mvpp2_port *port)
4216*4882a593Smuzhiyun {
4217*4882a593Smuzhiyun 	int i;
4218*4882a593Smuzhiyun 
4219*4882a593Smuzhiyun 	mvpp2_txp_max_tx_size_set(port);
4220*4882a593Smuzhiyun 
4221*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
4222*4882a593Smuzhiyun 		napi_enable(&port->qvecs[i].napi);
4223*4882a593Smuzhiyun 
4224*4882a593Smuzhiyun 	/* Enable interrupts on all threads */
4225*4882a593Smuzhiyun 	mvpp2_interrupts_enable(port);
4226*4882a593Smuzhiyun 
4227*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP22)
4228*4882a593Smuzhiyun 		mvpp22_mode_reconfigure(port);
4229*4882a593Smuzhiyun 
4230*4882a593Smuzhiyun 	if (port->phylink) {
4231*4882a593Smuzhiyun 		phylink_start(port->phylink);
4232*4882a593Smuzhiyun 	} else {
4233*4882a593Smuzhiyun 		mvpp2_acpi_start(port);
4234*4882a593Smuzhiyun 	}
4235*4882a593Smuzhiyun 
4236*4882a593Smuzhiyun 	netif_tx_start_all_queues(port->dev);
4237*4882a593Smuzhiyun 
4238*4882a593Smuzhiyun 	clear_bit(0, &port->state);
4239*4882a593Smuzhiyun }
4240*4882a593Smuzhiyun 
4241*4882a593Smuzhiyun /* Set hw internals when stopping port */
mvpp2_stop_dev(struct mvpp2_port * port)4242*4882a593Smuzhiyun static void mvpp2_stop_dev(struct mvpp2_port *port)
4243*4882a593Smuzhiyun {
4244*4882a593Smuzhiyun 	int i;
4245*4882a593Smuzhiyun 
4246*4882a593Smuzhiyun 	set_bit(0, &port->state);
4247*4882a593Smuzhiyun 
4248*4882a593Smuzhiyun 	/* Disable interrupts on all threads */
4249*4882a593Smuzhiyun 	mvpp2_interrupts_disable(port);
4250*4882a593Smuzhiyun 
4251*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
4252*4882a593Smuzhiyun 		napi_disable(&port->qvecs[i].napi);
4253*4882a593Smuzhiyun 
4254*4882a593Smuzhiyun 	if (port->phylink)
4255*4882a593Smuzhiyun 		phylink_stop(port->phylink);
4256*4882a593Smuzhiyun 	phy_power_off(port->comphy);
4257*4882a593Smuzhiyun }
4258*4882a593Smuzhiyun 
mvpp2_check_ringparam_valid(struct net_device * dev,struct ethtool_ringparam * ring)4259*4882a593Smuzhiyun static int mvpp2_check_ringparam_valid(struct net_device *dev,
4260*4882a593Smuzhiyun 				       struct ethtool_ringparam *ring)
4261*4882a593Smuzhiyun {
4262*4882a593Smuzhiyun 	u16 new_rx_pending = ring->rx_pending;
4263*4882a593Smuzhiyun 	u16 new_tx_pending = ring->tx_pending;
4264*4882a593Smuzhiyun 
4265*4882a593Smuzhiyun 	if (ring->rx_pending == 0 || ring->tx_pending == 0)
4266*4882a593Smuzhiyun 		return -EINVAL;
4267*4882a593Smuzhiyun 
4268*4882a593Smuzhiyun 	if (ring->rx_pending > MVPP2_MAX_RXD_MAX)
4269*4882a593Smuzhiyun 		new_rx_pending = MVPP2_MAX_RXD_MAX;
4270*4882a593Smuzhiyun 	else if (!IS_ALIGNED(ring->rx_pending, 16))
4271*4882a593Smuzhiyun 		new_rx_pending = ALIGN(ring->rx_pending, 16);
4272*4882a593Smuzhiyun 
4273*4882a593Smuzhiyun 	if (ring->tx_pending > MVPP2_MAX_TXD_MAX)
4274*4882a593Smuzhiyun 		new_tx_pending = MVPP2_MAX_TXD_MAX;
4275*4882a593Smuzhiyun 	else if (!IS_ALIGNED(ring->tx_pending, 32))
4276*4882a593Smuzhiyun 		new_tx_pending = ALIGN(ring->tx_pending, 32);
4277*4882a593Smuzhiyun 
4278*4882a593Smuzhiyun 	/* The Tx ring size cannot be smaller than the minimum number of
4279*4882a593Smuzhiyun 	 * descriptors needed for TSO.
4280*4882a593Smuzhiyun 	 */
4281*4882a593Smuzhiyun 	if (new_tx_pending < MVPP2_MAX_SKB_DESCS)
4282*4882a593Smuzhiyun 		new_tx_pending = ALIGN(MVPP2_MAX_SKB_DESCS, 32);
4283*4882a593Smuzhiyun 
4284*4882a593Smuzhiyun 	if (ring->rx_pending != new_rx_pending) {
4285*4882a593Smuzhiyun 		netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
4286*4882a593Smuzhiyun 			    ring->rx_pending, new_rx_pending);
4287*4882a593Smuzhiyun 		ring->rx_pending = new_rx_pending;
4288*4882a593Smuzhiyun 	}
4289*4882a593Smuzhiyun 
4290*4882a593Smuzhiyun 	if (ring->tx_pending != new_tx_pending) {
4291*4882a593Smuzhiyun 		netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
4292*4882a593Smuzhiyun 			    ring->tx_pending, new_tx_pending);
4293*4882a593Smuzhiyun 		ring->tx_pending = new_tx_pending;
4294*4882a593Smuzhiyun 	}
4295*4882a593Smuzhiyun 
4296*4882a593Smuzhiyun 	return 0;
4297*4882a593Smuzhiyun }
4298*4882a593Smuzhiyun 
mvpp21_get_mac_address(struct mvpp2_port * port,unsigned char * addr)4299*4882a593Smuzhiyun static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
4300*4882a593Smuzhiyun {
4301*4882a593Smuzhiyun 	u32 mac_addr_l, mac_addr_m, mac_addr_h;
4302*4882a593Smuzhiyun 
4303*4882a593Smuzhiyun 	mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4304*4882a593Smuzhiyun 	mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
4305*4882a593Smuzhiyun 	mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
4306*4882a593Smuzhiyun 	addr[0] = (mac_addr_h >> 24) & 0xFF;
4307*4882a593Smuzhiyun 	addr[1] = (mac_addr_h >> 16) & 0xFF;
4308*4882a593Smuzhiyun 	addr[2] = (mac_addr_h >> 8) & 0xFF;
4309*4882a593Smuzhiyun 	addr[3] = mac_addr_h & 0xFF;
4310*4882a593Smuzhiyun 	addr[4] = mac_addr_m & 0xFF;
4311*4882a593Smuzhiyun 	addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
4312*4882a593Smuzhiyun }
4313*4882a593Smuzhiyun 
mvpp2_irqs_init(struct mvpp2_port * port)4314*4882a593Smuzhiyun static int mvpp2_irqs_init(struct mvpp2_port *port)
4315*4882a593Smuzhiyun {
4316*4882a593Smuzhiyun 	int err, i;
4317*4882a593Smuzhiyun 
4318*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
4319*4882a593Smuzhiyun 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4320*4882a593Smuzhiyun 
4321*4882a593Smuzhiyun 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4322*4882a593Smuzhiyun 			qv->mask = kzalloc(cpumask_size(), GFP_KERNEL);
4323*4882a593Smuzhiyun 			if (!qv->mask) {
4324*4882a593Smuzhiyun 				err = -ENOMEM;
4325*4882a593Smuzhiyun 				goto err;
4326*4882a593Smuzhiyun 			}
4327*4882a593Smuzhiyun 
4328*4882a593Smuzhiyun 			irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
4329*4882a593Smuzhiyun 		}
4330*4882a593Smuzhiyun 
4331*4882a593Smuzhiyun 		err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
4332*4882a593Smuzhiyun 		if (err)
4333*4882a593Smuzhiyun 			goto err;
4334*4882a593Smuzhiyun 
4335*4882a593Smuzhiyun 		if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE) {
4336*4882a593Smuzhiyun 			unsigned int cpu;
4337*4882a593Smuzhiyun 
4338*4882a593Smuzhiyun 			for_each_present_cpu(cpu) {
4339*4882a593Smuzhiyun 				if (mvpp2_cpu_to_thread(port->priv, cpu) ==
4340*4882a593Smuzhiyun 				    qv->sw_thread_id)
4341*4882a593Smuzhiyun 					cpumask_set_cpu(cpu, qv->mask);
4342*4882a593Smuzhiyun 			}
4343*4882a593Smuzhiyun 
4344*4882a593Smuzhiyun 			irq_set_affinity_hint(qv->irq, qv->mask);
4345*4882a593Smuzhiyun 		}
4346*4882a593Smuzhiyun 	}
4347*4882a593Smuzhiyun 
4348*4882a593Smuzhiyun 	return 0;
4349*4882a593Smuzhiyun err:
4350*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
4351*4882a593Smuzhiyun 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4352*4882a593Smuzhiyun 
4353*4882a593Smuzhiyun 		irq_set_affinity_hint(qv->irq, NULL);
4354*4882a593Smuzhiyun 		kfree(qv->mask);
4355*4882a593Smuzhiyun 		qv->mask = NULL;
4356*4882a593Smuzhiyun 		free_irq(qv->irq, qv);
4357*4882a593Smuzhiyun 	}
4358*4882a593Smuzhiyun 
4359*4882a593Smuzhiyun 	return err;
4360*4882a593Smuzhiyun }
4361*4882a593Smuzhiyun 
mvpp2_irqs_deinit(struct mvpp2_port * port)4362*4882a593Smuzhiyun static void mvpp2_irqs_deinit(struct mvpp2_port *port)
4363*4882a593Smuzhiyun {
4364*4882a593Smuzhiyun 	int i;
4365*4882a593Smuzhiyun 
4366*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
4367*4882a593Smuzhiyun 		struct mvpp2_queue_vector *qv = port->qvecs + i;
4368*4882a593Smuzhiyun 
4369*4882a593Smuzhiyun 		irq_set_affinity_hint(qv->irq, NULL);
4370*4882a593Smuzhiyun 		kfree(qv->mask);
4371*4882a593Smuzhiyun 		qv->mask = NULL;
4372*4882a593Smuzhiyun 		irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
4373*4882a593Smuzhiyun 		free_irq(qv->irq, qv);
4374*4882a593Smuzhiyun 	}
4375*4882a593Smuzhiyun }
4376*4882a593Smuzhiyun 
mvpp22_rss_is_supported(void)4377*4882a593Smuzhiyun static bool mvpp22_rss_is_supported(void)
4378*4882a593Smuzhiyun {
4379*4882a593Smuzhiyun 	return queue_mode == MVPP2_QDIST_MULTI_MODE;
4380*4882a593Smuzhiyun }
4381*4882a593Smuzhiyun 
mvpp2_open(struct net_device * dev)4382*4882a593Smuzhiyun static int mvpp2_open(struct net_device *dev)
4383*4882a593Smuzhiyun {
4384*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4385*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
4386*4882a593Smuzhiyun 	unsigned char mac_bcast[ETH_ALEN] = {
4387*4882a593Smuzhiyun 			0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4388*4882a593Smuzhiyun 	bool valid = false;
4389*4882a593Smuzhiyun 	int err;
4390*4882a593Smuzhiyun 
4391*4882a593Smuzhiyun 	err = mvpp2_prs_mac_da_accept(port, mac_bcast, true);
4392*4882a593Smuzhiyun 	if (err) {
4393*4882a593Smuzhiyun 		netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
4394*4882a593Smuzhiyun 		return err;
4395*4882a593Smuzhiyun 	}
4396*4882a593Smuzhiyun 	err = mvpp2_prs_mac_da_accept(port, dev->dev_addr, true);
4397*4882a593Smuzhiyun 	if (err) {
4398*4882a593Smuzhiyun 		netdev_err(dev, "mvpp2_prs_mac_da_accept own addr failed\n");
4399*4882a593Smuzhiyun 		return err;
4400*4882a593Smuzhiyun 	}
4401*4882a593Smuzhiyun 	err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
4402*4882a593Smuzhiyun 	if (err) {
4403*4882a593Smuzhiyun 		netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
4404*4882a593Smuzhiyun 		return err;
4405*4882a593Smuzhiyun 	}
4406*4882a593Smuzhiyun 	err = mvpp2_prs_def_flow(port);
4407*4882a593Smuzhiyun 	if (err) {
4408*4882a593Smuzhiyun 		netdev_err(dev, "mvpp2_prs_def_flow failed\n");
4409*4882a593Smuzhiyun 		return err;
4410*4882a593Smuzhiyun 	}
4411*4882a593Smuzhiyun 
4412*4882a593Smuzhiyun 	/* Allocate the Rx/Tx queues */
4413*4882a593Smuzhiyun 	err = mvpp2_setup_rxqs(port);
4414*4882a593Smuzhiyun 	if (err) {
4415*4882a593Smuzhiyun 		netdev_err(port->dev, "cannot allocate Rx queues\n");
4416*4882a593Smuzhiyun 		return err;
4417*4882a593Smuzhiyun 	}
4418*4882a593Smuzhiyun 
4419*4882a593Smuzhiyun 	err = mvpp2_setup_txqs(port);
4420*4882a593Smuzhiyun 	if (err) {
4421*4882a593Smuzhiyun 		netdev_err(port->dev, "cannot allocate Tx queues\n");
4422*4882a593Smuzhiyun 		goto err_cleanup_rxqs;
4423*4882a593Smuzhiyun 	}
4424*4882a593Smuzhiyun 
4425*4882a593Smuzhiyun 	err = mvpp2_irqs_init(port);
4426*4882a593Smuzhiyun 	if (err) {
4427*4882a593Smuzhiyun 		netdev_err(port->dev, "cannot init IRQs\n");
4428*4882a593Smuzhiyun 		goto err_cleanup_txqs;
4429*4882a593Smuzhiyun 	}
4430*4882a593Smuzhiyun 
4431*4882a593Smuzhiyun 	/* Phylink isn't supported yet in ACPI mode */
4432*4882a593Smuzhiyun 	if (port->of_node) {
4433*4882a593Smuzhiyun 		err = phylink_of_phy_connect(port->phylink, port->of_node, 0);
4434*4882a593Smuzhiyun 		if (err) {
4435*4882a593Smuzhiyun 			netdev_err(port->dev, "could not attach PHY (%d)\n",
4436*4882a593Smuzhiyun 				   err);
4437*4882a593Smuzhiyun 			goto err_free_irq;
4438*4882a593Smuzhiyun 		}
4439*4882a593Smuzhiyun 
4440*4882a593Smuzhiyun 		valid = true;
4441*4882a593Smuzhiyun 	}
4442*4882a593Smuzhiyun 
4443*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22 && port->port_irq) {
4444*4882a593Smuzhiyun 		err = request_irq(port->port_irq, mvpp2_port_isr, 0,
4445*4882a593Smuzhiyun 				  dev->name, port);
4446*4882a593Smuzhiyun 		if (err) {
4447*4882a593Smuzhiyun 			netdev_err(port->dev,
4448*4882a593Smuzhiyun 				   "cannot request port link/ptp IRQ %d\n",
4449*4882a593Smuzhiyun 				   port->port_irq);
4450*4882a593Smuzhiyun 			goto err_free_irq;
4451*4882a593Smuzhiyun 		}
4452*4882a593Smuzhiyun 
4453*4882a593Smuzhiyun 		mvpp22_gop_setup_irq(port);
4454*4882a593Smuzhiyun 
4455*4882a593Smuzhiyun 		/* In default link is down */
4456*4882a593Smuzhiyun 		netif_carrier_off(port->dev);
4457*4882a593Smuzhiyun 
4458*4882a593Smuzhiyun 		valid = true;
4459*4882a593Smuzhiyun 	} else {
4460*4882a593Smuzhiyun 		port->port_irq = 0;
4461*4882a593Smuzhiyun 	}
4462*4882a593Smuzhiyun 
4463*4882a593Smuzhiyun 	if (!valid) {
4464*4882a593Smuzhiyun 		netdev_err(port->dev,
4465*4882a593Smuzhiyun 			   "invalid configuration: no dt or link IRQ");
4466*4882a593Smuzhiyun 		err = -ENOENT;
4467*4882a593Smuzhiyun 		goto err_free_irq;
4468*4882a593Smuzhiyun 	}
4469*4882a593Smuzhiyun 
4470*4882a593Smuzhiyun 	/* Unmask interrupts on all CPUs */
4471*4882a593Smuzhiyun 	on_each_cpu(mvpp2_interrupts_unmask, port, 1);
4472*4882a593Smuzhiyun 	mvpp2_shared_interrupt_mask_unmask(port, false);
4473*4882a593Smuzhiyun 
4474*4882a593Smuzhiyun 	mvpp2_start_dev(port);
4475*4882a593Smuzhiyun 
4476*4882a593Smuzhiyun 	/* Start hardware statistics gathering */
4477*4882a593Smuzhiyun 	queue_delayed_work(priv->stats_queue, &port->stats_work,
4478*4882a593Smuzhiyun 			   MVPP2_MIB_COUNTERS_STATS_DELAY);
4479*4882a593Smuzhiyun 
4480*4882a593Smuzhiyun 	return 0;
4481*4882a593Smuzhiyun 
4482*4882a593Smuzhiyun err_free_irq:
4483*4882a593Smuzhiyun 	mvpp2_irqs_deinit(port);
4484*4882a593Smuzhiyun err_cleanup_txqs:
4485*4882a593Smuzhiyun 	mvpp2_cleanup_txqs(port);
4486*4882a593Smuzhiyun err_cleanup_rxqs:
4487*4882a593Smuzhiyun 	mvpp2_cleanup_rxqs(port);
4488*4882a593Smuzhiyun 	return err;
4489*4882a593Smuzhiyun }
4490*4882a593Smuzhiyun 
mvpp2_stop(struct net_device * dev)4491*4882a593Smuzhiyun static int mvpp2_stop(struct net_device *dev)
4492*4882a593Smuzhiyun {
4493*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4494*4882a593Smuzhiyun 	struct mvpp2_port_pcpu *port_pcpu;
4495*4882a593Smuzhiyun 	unsigned int thread;
4496*4882a593Smuzhiyun 
4497*4882a593Smuzhiyun 	mvpp2_stop_dev(port);
4498*4882a593Smuzhiyun 
4499*4882a593Smuzhiyun 	/* Mask interrupts on all threads */
4500*4882a593Smuzhiyun 	on_each_cpu(mvpp2_interrupts_mask, port, 1);
4501*4882a593Smuzhiyun 	mvpp2_shared_interrupt_mask_unmask(port, true);
4502*4882a593Smuzhiyun 
4503*4882a593Smuzhiyun 	if (port->phylink)
4504*4882a593Smuzhiyun 		phylink_disconnect_phy(port->phylink);
4505*4882a593Smuzhiyun 	if (port->port_irq)
4506*4882a593Smuzhiyun 		free_irq(port->port_irq, port);
4507*4882a593Smuzhiyun 
4508*4882a593Smuzhiyun 	mvpp2_irqs_deinit(port);
4509*4882a593Smuzhiyun 	if (!port->has_tx_irqs) {
4510*4882a593Smuzhiyun 		for (thread = 0; thread < port->priv->nthreads; thread++) {
4511*4882a593Smuzhiyun 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
4512*4882a593Smuzhiyun 
4513*4882a593Smuzhiyun 			hrtimer_cancel(&port_pcpu->tx_done_timer);
4514*4882a593Smuzhiyun 			port_pcpu->timer_scheduled = false;
4515*4882a593Smuzhiyun 		}
4516*4882a593Smuzhiyun 	}
4517*4882a593Smuzhiyun 	mvpp2_cleanup_rxqs(port);
4518*4882a593Smuzhiyun 	mvpp2_cleanup_txqs(port);
4519*4882a593Smuzhiyun 
4520*4882a593Smuzhiyun 	cancel_delayed_work_sync(&port->stats_work);
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	mvpp2_mac_reset_assert(port);
4523*4882a593Smuzhiyun 	mvpp22_pcs_reset_assert(port);
4524*4882a593Smuzhiyun 
4525*4882a593Smuzhiyun 	return 0;
4526*4882a593Smuzhiyun }
4527*4882a593Smuzhiyun 
mvpp2_prs_mac_da_accept_list(struct mvpp2_port * port,struct netdev_hw_addr_list * list)4528*4882a593Smuzhiyun static int mvpp2_prs_mac_da_accept_list(struct mvpp2_port *port,
4529*4882a593Smuzhiyun 					struct netdev_hw_addr_list *list)
4530*4882a593Smuzhiyun {
4531*4882a593Smuzhiyun 	struct netdev_hw_addr *ha;
4532*4882a593Smuzhiyun 	int ret;
4533*4882a593Smuzhiyun 
4534*4882a593Smuzhiyun 	netdev_hw_addr_list_for_each(ha, list) {
4535*4882a593Smuzhiyun 		ret = mvpp2_prs_mac_da_accept(port, ha->addr, true);
4536*4882a593Smuzhiyun 		if (ret)
4537*4882a593Smuzhiyun 			return ret;
4538*4882a593Smuzhiyun 	}
4539*4882a593Smuzhiyun 
4540*4882a593Smuzhiyun 	return 0;
4541*4882a593Smuzhiyun }
4542*4882a593Smuzhiyun 
mvpp2_set_rx_promisc(struct mvpp2_port * port,bool enable)4543*4882a593Smuzhiyun static void mvpp2_set_rx_promisc(struct mvpp2_port *port, bool enable)
4544*4882a593Smuzhiyun {
4545*4882a593Smuzhiyun 	if (!enable && (port->dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
4546*4882a593Smuzhiyun 		mvpp2_prs_vid_enable_filtering(port);
4547*4882a593Smuzhiyun 	else
4548*4882a593Smuzhiyun 		mvpp2_prs_vid_disable_filtering(port);
4549*4882a593Smuzhiyun 
4550*4882a593Smuzhiyun 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
4551*4882a593Smuzhiyun 				  MVPP2_PRS_L2_UNI_CAST, enable);
4552*4882a593Smuzhiyun 
4553*4882a593Smuzhiyun 	mvpp2_prs_mac_promisc_set(port->priv, port->id,
4554*4882a593Smuzhiyun 				  MVPP2_PRS_L2_MULTI_CAST, enable);
4555*4882a593Smuzhiyun }
4556*4882a593Smuzhiyun 
mvpp2_set_rx_mode(struct net_device * dev)4557*4882a593Smuzhiyun static void mvpp2_set_rx_mode(struct net_device *dev)
4558*4882a593Smuzhiyun {
4559*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4560*4882a593Smuzhiyun 
4561*4882a593Smuzhiyun 	/* Clear the whole UC and MC list */
4562*4882a593Smuzhiyun 	mvpp2_prs_mac_del_all(port);
4563*4882a593Smuzhiyun 
4564*4882a593Smuzhiyun 	if (dev->flags & IFF_PROMISC) {
4565*4882a593Smuzhiyun 		mvpp2_set_rx_promisc(port, true);
4566*4882a593Smuzhiyun 		return;
4567*4882a593Smuzhiyun 	}
4568*4882a593Smuzhiyun 
4569*4882a593Smuzhiyun 	mvpp2_set_rx_promisc(port, false);
4570*4882a593Smuzhiyun 
4571*4882a593Smuzhiyun 	if (netdev_uc_count(dev) > MVPP2_PRS_MAC_UC_FILT_MAX ||
4572*4882a593Smuzhiyun 	    mvpp2_prs_mac_da_accept_list(port, &dev->uc))
4573*4882a593Smuzhiyun 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4574*4882a593Smuzhiyun 					  MVPP2_PRS_L2_UNI_CAST, true);
4575*4882a593Smuzhiyun 
4576*4882a593Smuzhiyun 	if (dev->flags & IFF_ALLMULTI) {
4577*4882a593Smuzhiyun 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4578*4882a593Smuzhiyun 					  MVPP2_PRS_L2_MULTI_CAST, true);
4579*4882a593Smuzhiyun 		return;
4580*4882a593Smuzhiyun 	}
4581*4882a593Smuzhiyun 
4582*4882a593Smuzhiyun 	if (netdev_mc_count(dev) > MVPP2_PRS_MAC_MC_FILT_MAX ||
4583*4882a593Smuzhiyun 	    mvpp2_prs_mac_da_accept_list(port, &dev->mc))
4584*4882a593Smuzhiyun 		mvpp2_prs_mac_promisc_set(port->priv, port->id,
4585*4882a593Smuzhiyun 					  MVPP2_PRS_L2_MULTI_CAST, true);
4586*4882a593Smuzhiyun }
4587*4882a593Smuzhiyun 
mvpp2_set_mac_address(struct net_device * dev,void * p)4588*4882a593Smuzhiyun static int mvpp2_set_mac_address(struct net_device *dev, void *p)
4589*4882a593Smuzhiyun {
4590*4882a593Smuzhiyun 	const struct sockaddr *addr = p;
4591*4882a593Smuzhiyun 	int err;
4592*4882a593Smuzhiyun 
4593*4882a593Smuzhiyun 	if (!is_valid_ether_addr(addr->sa_data))
4594*4882a593Smuzhiyun 		return -EADDRNOTAVAIL;
4595*4882a593Smuzhiyun 
4596*4882a593Smuzhiyun 	err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
4597*4882a593Smuzhiyun 	if (err) {
4598*4882a593Smuzhiyun 		/* Reconfigure parser accept the original MAC address */
4599*4882a593Smuzhiyun 		mvpp2_prs_update_mac_da(dev, dev->dev_addr);
4600*4882a593Smuzhiyun 		netdev_err(dev, "failed to change MAC address\n");
4601*4882a593Smuzhiyun 	}
4602*4882a593Smuzhiyun 	return err;
4603*4882a593Smuzhiyun }
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun /* Shut down all the ports, reconfigure the pools as percpu or shared,
4606*4882a593Smuzhiyun  * then bring up again all ports.
4607*4882a593Smuzhiyun  */
mvpp2_bm_switch_buffers(struct mvpp2 * priv,bool percpu)4608*4882a593Smuzhiyun static int mvpp2_bm_switch_buffers(struct mvpp2 *priv, bool percpu)
4609*4882a593Smuzhiyun {
4610*4882a593Smuzhiyun 	int numbufs = MVPP2_BM_POOLS_NUM, i;
4611*4882a593Smuzhiyun 	struct mvpp2_port *port = NULL;
4612*4882a593Smuzhiyun 	bool status[MVPP2_MAX_PORTS];
4613*4882a593Smuzhiyun 
4614*4882a593Smuzhiyun 	for (i = 0; i < priv->port_count; i++) {
4615*4882a593Smuzhiyun 		port = priv->port_list[i];
4616*4882a593Smuzhiyun 		status[i] = netif_running(port->dev);
4617*4882a593Smuzhiyun 		if (status[i])
4618*4882a593Smuzhiyun 			mvpp2_stop(port->dev);
4619*4882a593Smuzhiyun 	}
4620*4882a593Smuzhiyun 
4621*4882a593Smuzhiyun 	/* nrxqs is the same for all ports */
4622*4882a593Smuzhiyun 	if (priv->percpu_pools)
4623*4882a593Smuzhiyun 		numbufs = port->nrxqs * 2;
4624*4882a593Smuzhiyun 
4625*4882a593Smuzhiyun 	for (i = 0; i < numbufs; i++)
4626*4882a593Smuzhiyun 		mvpp2_bm_pool_destroy(port->dev->dev.parent, priv, &priv->bm_pools[i]);
4627*4882a593Smuzhiyun 
4628*4882a593Smuzhiyun 	devm_kfree(port->dev->dev.parent, priv->bm_pools);
4629*4882a593Smuzhiyun 	priv->percpu_pools = percpu;
4630*4882a593Smuzhiyun 	mvpp2_bm_init(port->dev->dev.parent, priv);
4631*4882a593Smuzhiyun 
4632*4882a593Smuzhiyun 	for (i = 0; i < priv->port_count; i++) {
4633*4882a593Smuzhiyun 		port = priv->port_list[i];
4634*4882a593Smuzhiyun 		mvpp2_swf_bm_pool_init(port);
4635*4882a593Smuzhiyun 		if (status[i])
4636*4882a593Smuzhiyun 			mvpp2_open(port->dev);
4637*4882a593Smuzhiyun 	}
4638*4882a593Smuzhiyun 
4639*4882a593Smuzhiyun 	return 0;
4640*4882a593Smuzhiyun }
4641*4882a593Smuzhiyun 
mvpp2_change_mtu(struct net_device * dev,int mtu)4642*4882a593Smuzhiyun static int mvpp2_change_mtu(struct net_device *dev, int mtu)
4643*4882a593Smuzhiyun {
4644*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4645*4882a593Smuzhiyun 	bool running = netif_running(dev);
4646*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
4647*4882a593Smuzhiyun 	int err;
4648*4882a593Smuzhiyun 
4649*4882a593Smuzhiyun 	if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
4650*4882a593Smuzhiyun 		netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
4651*4882a593Smuzhiyun 			    ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
4652*4882a593Smuzhiyun 		mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
4653*4882a593Smuzhiyun 	}
4654*4882a593Smuzhiyun 
4655*4882a593Smuzhiyun 	if (port->xdp_prog && mtu > MVPP2_MAX_RX_BUF_SIZE) {
4656*4882a593Smuzhiyun 		netdev_err(dev, "Illegal MTU value %d (> %d) for XDP mode\n",
4657*4882a593Smuzhiyun 			   mtu, (int)MVPP2_MAX_RX_BUF_SIZE);
4658*4882a593Smuzhiyun 		return -EINVAL;
4659*4882a593Smuzhiyun 	}
4660*4882a593Smuzhiyun 
4661*4882a593Smuzhiyun 	if (MVPP2_RX_PKT_SIZE(mtu) > MVPP2_BM_LONG_PKT_SIZE) {
4662*4882a593Smuzhiyun 		if (priv->percpu_pools) {
4663*4882a593Smuzhiyun 			netdev_warn(dev, "mtu %d too high, switching to shared buffers", mtu);
4664*4882a593Smuzhiyun 			mvpp2_bm_switch_buffers(priv, false);
4665*4882a593Smuzhiyun 		}
4666*4882a593Smuzhiyun 	} else {
4667*4882a593Smuzhiyun 		bool jumbo = false;
4668*4882a593Smuzhiyun 		int i;
4669*4882a593Smuzhiyun 
4670*4882a593Smuzhiyun 		for (i = 0; i < priv->port_count; i++)
4671*4882a593Smuzhiyun 			if (priv->port_list[i] != port &&
4672*4882a593Smuzhiyun 			    MVPP2_RX_PKT_SIZE(priv->port_list[i]->dev->mtu) >
4673*4882a593Smuzhiyun 			    MVPP2_BM_LONG_PKT_SIZE) {
4674*4882a593Smuzhiyun 				jumbo = true;
4675*4882a593Smuzhiyun 				break;
4676*4882a593Smuzhiyun 			}
4677*4882a593Smuzhiyun 
4678*4882a593Smuzhiyun 		/* No port is using jumbo frames */
4679*4882a593Smuzhiyun 		if (!jumbo) {
4680*4882a593Smuzhiyun 			dev_info(port->dev->dev.parent,
4681*4882a593Smuzhiyun 				 "all ports have a low MTU, switching to per-cpu buffers");
4682*4882a593Smuzhiyun 			mvpp2_bm_switch_buffers(priv, true);
4683*4882a593Smuzhiyun 		}
4684*4882a593Smuzhiyun 	}
4685*4882a593Smuzhiyun 
4686*4882a593Smuzhiyun 	if (running)
4687*4882a593Smuzhiyun 		mvpp2_stop_dev(port);
4688*4882a593Smuzhiyun 
4689*4882a593Smuzhiyun 	err = mvpp2_bm_update_mtu(dev, mtu);
4690*4882a593Smuzhiyun 	if (err) {
4691*4882a593Smuzhiyun 		netdev_err(dev, "failed to change MTU\n");
4692*4882a593Smuzhiyun 		/* Reconfigure BM to the original MTU */
4693*4882a593Smuzhiyun 		mvpp2_bm_update_mtu(dev, dev->mtu);
4694*4882a593Smuzhiyun 	} else {
4695*4882a593Smuzhiyun 		port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
4696*4882a593Smuzhiyun 	}
4697*4882a593Smuzhiyun 
4698*4882a593Smuzhiyun 	if (running) {
4699*4882a593Smuzhiyun 		mvpp2_start_dev(port);
4700*4882a593Smuzhiyun 		mvpp2_egress_enable(port);
4701*4882a593Smuzhiyun 		mvpp2_ingress_enable(port);
4702*4882a593Smuzhiyun 	}
4703*4882a593Smuzhiyun 
4704*4882a593Smuzhiyun 	return err;
4705*4882a593Smuzhiyun }
4706*4882a593Smuzhiyun 
mvpp2_check_pagepool_dma(struct mvpp2_port * port)4707*4882a593Smuzhiyun static int mvpp2_check_pagepool_dma(struct mvpp2_port *port)
4708*4882a593Smuzhiyun {
4709*4882a593Smuzhiyun 	enum dma_data_direction dma_dir = DMA_FROM_DEVICE;
4710*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
4711*4882a593Smuzhiyun 	int err = -1, i;
4712*4882a593Smuzhiyun 
4713*4882a593Smuzhiyun 	if (!priv->percpu_pools)
4714*4882a593Smuzhiyun 		return err;
4715*4882a593Smuzhiyun 
4716*4882a593Smuzhiyun 	if (!priv->page_pool[0])
4717*4882a593Smuzhiyun 		return -ENOMEM;
4718*4882a593Smuzhiyun 
4719*4882a593Smuzhiyun 	for (i = 0; i < priv->port_count; i++) {
4720*4882a593Smuzhiyun 		port = priv->port_list[i];
4721*4882a593Smuzhiyun 		if (port->xdp_prog) {
4722*4882a593Smuzhiyun 			dma_dir = DMA_BIDIRECTIONAL;
4723*4882a593Smuzhiyun 			break;
4724*4882a593Smuzhiyun 		}
4725*4882a593Smuzhiyun 	}
4726*4882a593Smuzhiyun 
4727*4882a593Smuzhiyun 	/* All pools are equal in terms of DMA direction */
4728*4882a593Smuzhiyun 	if (priv->page_pool[0]->p.dma_dir != dma_dir)
4729*4882a593Smuzhiyun 		err = mvpp2_bm_switch_buffers(priv, true);
4730*4882a593Smuzhiyun 
4731*4882a593Smuzhiyun 	return err;
4732*4882a593Smuzhiyun }
4733*4882a593Smuzhiyun 
4734*4882a593Smuzhiyun static void
mvpp2_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)4735*4882a593Smuzhiyun mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
4736*4882a593Smuzhiyun {
4737*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4738*4882a593Smuzhiyun 	unsigned int start;
4739*4882a593Smuzhiyun 	unsigned int cpu;
4740*4882a593Smuzhiyun 
4741*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
4742*4882a593Smuzhiyun 		struct mvpp2_pcpu_stats *cpu_stats;
4743*4882a593Smuzhiyun 		u64 rx_packets;
4744*4882a593Smuzhiyun 		u64 rx_bytes;
4745*4882a593Smuzhiyun 		u64 tx_packets;
4746*4882a593Smuzhiyun 		u64 tx_bytes;
4747*4882a593Smuzhiyun 
4748*4882a593Smuzhiyun 		cpu_stats = per_cpu_ptr(port->stats, cpu);
4749*4882a593Smuzhiyun 		do {
4750*4882a593Smuzhiyun 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
4751*4882a593Smuzhiyun 			rx_packets = cpu_stats->rx_packets;
4752*4882a593Smuzhiyun 			rx_bytes   = cpu_stats->rx_bytes;
4753*4882a593Smuzhiyun 			tx_packets = cpu_stats->tx_packets;
4754*4882a593Smuzhiyun 			tx_bytes   = cpu_stats->tx_bytes;
4755*4882a593Smuzhiyun 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
4756*4882a593Smuzhiyun 
4757*4882a593Smuzhiyun 		stats->rx_packets += rx_packets;
4758*4882a593Smuzhiyun 		stats->rx_bytes   += rx_bytes;
4759*4882a593Smuzhiyun 		stats->tx_packets += tx_packets;
4760*4882a593Smuzhiyun 		stats->tx_bytes   += tx_bytes;
4761*4882a593Smuzhiyun 	}
4762*4882a593Smuzhiyun 
4763*4882a593Smuzhiyun 	stats->rx_errors	= dev->stats.rx_errors;
4764*4882a593Smuzhiyun 	stats->rx_dropped	= dev->stats.rx_dropped;
4765*4882a593Smuzhiyun 	stats->tx_dropped	= dev->stats.tx_dropped;
4766*4882a593Smuzhiyun }
4767*4882a593Smuzhiyun 
mvpp2_set_ts_config(struct mvpp2_port * port,struct ifreq * ifr)4768*4882a593Smuzhiyun static int mvpp2_set_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
4769*4882a593Smuzhiyun {
4770*4882a593Smuzhiyun 	struct hwtstamp_config config;
4771*4882a593Smuzhiyun 	void __iomem *ptp;
4772*4882a593Smuzhiyun 	u32 gcr, int_mask;
4773*4882a593Smuzhiyun 
4774*4882a593Smuzhiyun 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
4775*4882a593Smuzhiyun 		return -EFAULT;
4776*4882a593Smuzhiyun 
4777*4882a593Smuzhiyun 	if (config.flags)
4778*4882a593Smuzhiyun 		return -EINVAL;
4779*4882a593Smuzhiyun 
4780*4882a593Smuzhiyun 	if (config.tx_type != HWTSTAMP_TX_OFF &&
4781*4882a593Smuzhiyun 	    config.tx_type != HWTSTAMP_TX_ON)
4782*4882a593Smuzhiyun 		return -ERANGE;
4783*4882a593Smuzhiyun 
4784*4882a593Smuzhiyun 	ptp = port->priv->iface_base + MVPP22_PTP_BASE(port->gop_id);
4785*4882a593Smuzhiyun 
4786*4882a593Smuzhiyun 	int_mask = gcr = 0;
4787*4882a593Smuzhiyun 	if (config.tx_type != HWTSTAMP_TX_OFF) {
4788*4882a593Smuzhiyun 		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_TX_RESET;
4789*4882a593Smuzhiyun 		int_mask |= MVPP22_PTP_INT_MASK_QUEUE1 |
4790*4882a593Smuzhiyun 			    MVPP22_PTP_INT_MASK_QUEUE0;
4791*4882a593Smuzhiyun 	}
4792*4882a593Smuzhiyun 
4793*4882a593Smuzhiyun 	/* It seems we must also release the TX reset when enabling the TSU */
4794*4882a593Smuzhiyun 	if (config.rx_filter != HWTSTAMP_FILTER_NONE)
4795*4882a593Smuzhiyun 		gcr |= MVPP22_PTP_GCR_TSU_ENABLE | MVPP22_PTP_GCR_RX_RESET |
4796*4882a593Smuzhiyun 		       MVPP22_PTP_GCR_TX_RESET;
4797*4882a593Smuzhiyun 
4798*4882a593Smuzhiyun 	if (gcr & MVPP22_PTP_GCR_TSU_ENABLE)
4799*4882a593Smuzhiyun 		mvpp22_tai_start(port->priv->tai);
4800*4882a593Smuzhiyun 
4801*4882a593Smuzhiyun 	if (config.rx_filter != HWTSTAMP_FILTER_NONE) {
4802*4882a593Smuzhiyun 		config.rx_filter = HWTSTAMP_FILTER_ALL;
4803*4882a593Smuzhiyun 		mvpp2_modify(ptp + MVPP22_PTP_GCR,
4804*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_RX_RESET |
4805*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_TX_RESET |
4806*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
4807*4882a593Smuzhiyun 		port->rx_hwtstamp = true;
4808*4882a593Smuzhiyun 	} else {
4809*4882a593Smuzhiyun 		port->rx_hwtstamp = false;
4810*4882a593Smuzhiyun 		mvpp2_modify(ptp + MVPP22_PTP_GCR,
4811*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_RX_RESET |
4812*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_TX_RESET |
4813*4882a593Smuzhiyun 			     MVPP22_PTP_GCR_TSU_ENABLE, gcr);
4814*4882a593Smuzhiyun 	}
4815*4882a593Smuzhiyun 
4816*4882a593Smuzhiyun 	mvpp2_modify(ptp + MVPP22_PTP_INT_MASK,
4817*4882a593Smuzhiyun 		     MVPP22_PTP_INT_MASK_QUEUE1 |
4818*4882a593Smuzhiyun 		     MVPP22_PTP_INT_MASK_QUEUE0, int_mask);
4819*4882a593Smuzhiyun 
4820*4882a593Smuzhiyun 	if (!(gcr & MVPP22_PTP_GCR_TSU_ENABLE))
4821*4882a593Smuzhiyun 		mvpp22_tai_stop(port->priv->tai);
4822*4882a593Smuzhiyun 
4823*4882a593Smuzhiyun 	port->tx_hwtstamp_type = config.tx_type;
4824*4882a593Smuzhiyun 
4825*4882a593Smuzhiyun 	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
4826*4882a593Smuzhiyun 		return -EFAULT;
4827*4882a593Smuzhiyun 
4828*4882a593Smuzhiyun 	return 0;
4829*4882a593Smuzhiyun }
4830*4882a593Smuzhiyun 
mvpp2_get_ts_config(struct mvpp2_port * port,struct ifreq * ifr)4831*4882a593Smuzhiyun static int mvpp2_get_ts_config(struct mvpp2_port *port, struct ifreq *ifr)
4832*4882a593Smuzhiyun {
4833*4882a593Smuzhiyun 	struct hwtstamp_config config;
4834*4882a593Smuzhiyun 
4835*4882a593Smuzhiyun 	memset(&config, 0, sizeof(config));
4836*4882a593Smuzhiyun 
4837*4882a593Smuzhiyun 	config.tx_type = port->tx_hwtstamp_type;
4838*4882a593Smuzhiyun 	config.rx_filter = port->rx_hwtstamp ?
4839*4882a593Smuzhiyun 		HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
4840*4882a593Smuzhiyun 
4841*4882a593Smuzhiyun 	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
4842*4882a593Smuzhiyun 		return -EFAULT;
4843*4882a593Smuzhiyun 
4844*4882a593Smuzhiyun 	return 0;
4845*4882a593Smuzhiyun }
4846*4882a593Smuzhiyun 
mvpp2_ethtool_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)4847*4882a593Smuzhiyun static int mvpp2_ethtool_get_ts_info(struct net_device *dev,
4848*4882a593Smuzhiyun 				     struct ethtool_ts_info *info)
4849*4882a593Smuzhiyun {
4850*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4851*4882a593Smuzhiyun 
4852*4882a593Smuzhiyun 	if (!port->hwtstamp)
4853*4882a593Smuzhiyun 		return -EOPNOTSUPP;
4854*4882a593Smuzhiyun 
4855*4882a593Smuzhiyun 	info->phc_index = mvpp22_tai_ptp_clock_index(port->priv->tai);
4856*4882a593Smuzhiyun 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
4857*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RX_SOFTWARE |
4858*4882a593Smuzhiyun 				SOF_TIMESTAMPING_SOFTWARE |
4859*4882a593Smuzhiyun 				SOF_TIMESTAMPING_TX_HARDWARE |
4860*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RX_HARDWARE |
4861*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RAW_HARDWARE;
4862*4882a593Smuzhiyun 	info->tx_types = BIT(HWTSTAMP_TX_OFF) |
4863*4882a593Smuzhiyun 			 BIT(HWTSTAMP_TX_ON);
4864*4882a593Smuzhiyun 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
4865*4882a593Smuzhiyun 			   BIT(HWTSTAMP_FILTER_ALL);
4866*4882a593Smuzhiyun 
4867*4882a593Smuzhiyun 	return 0;
4868*4882a593Smuzhiyun }
4869*4882a593Smuzhiyun 
mvpp2_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)4870*4882a593Smuzhiyun static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4871*4882a593Smuzhiyun {
4872*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4873*4882a593Smuzhiyun 
4874*4882a593Smuzhiyun 	switch (cmd) {
4875*4882a593Smuzhiyun 	case SIOCSHWTSTAMP:
4876*4882a593Smuzhiyun 		if (port->hwtstamp)
4877*4882a593Smuzhiyun 			return mvpp2_set_ts_config(port, ifr);
4878*4882a593Smuzhiyun 		break;
4879*4882a593Smuzhiyun 
4880*4882a593Smuzhiyun 	case SIOCGHWTSTAMP:
4881*4882a593Smuzhiyun 		if (port->hwtstamp)
4882*4882a593Smuzhiyun 			return mvpp2_get_ts_config(port, ifr);
4883*4882a593Smuzhiyun 		break;
4884*4882a593Smuzhiyun 	}
4885*4882a593Smuzhiyun 
4886*4882a593Smuzhiyun 	if (!port->phylink)
4887*4882a593Smuzhiyun 		return -ENOTSUPP;
4888*4882a593Smuzhiyun 
4889*4882a593Smuzhiyun 	return phylink_mii_ioctl(port->phylink, ifr, cmd);
4890*4882a593Smuzhiyun }
4891*4882a593Smuzhiyun 
mvpp2_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)4892*4882a593Smuzhiyun static int mvpp2_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
4893*4882a593Smuzhiyun {
4894*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4895*4882a593Smuzhiyun 	int ret;
4896*4882a593Smuzhiyun 
4897*4882a593Smuzhiyun 	ret = mvpp2_prs_vid_entry_add(port, vid);
4898*4882a593Smuzhiyun 	if (ret)
4899*4882a593Smuzhiyun 		netdev_err(dev, "rx-vlan-filter offloading cannot accept more than %d VIDs per port\n",
4900*4882a593Smuzhiyun 			   MVPP2_PRS_VLAN_FILT_MAX - 1);
4901*4882a593Smuzhiyun 	return ret;
4902*4882a593Smuzhiyun }
4903*4882a593Smuzhiyun 
mvpp2_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)4904*4882a593Smuzhiyun static int mvpp2_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
4905*4882a593Smuzhiyun {
4906*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4907*4882a593Smuzhiyun 
4908*4882a593Smuzhiyun 	mvpp2_prs_vid_entry_remove(port, vid);
4909*4882a593Smuzhiyun 	return 0;
4910*4882a593Smuzhiyun }
4911*4882a593Smuzhiyun 
mvpp2_set_features(struct net_device * dev,netdev_features_t features)4912*4882a593Smuzhiyun static int mvpp2_set_features(struct net_device *dev,
4913*4882a593Smuzhiyun 			      netdev_features_t features)
4914*4882a593Smuzhiyun {
4915*4882a593Smuzhiyun 	netdev_features_t changed = dev->features ^ features;
4916*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4917*4882a593Smuzhiyun 
4918*4882a593Smuzhiyun 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
4919*4882a593Smuzhiyun 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
4920*4882a593Smuzhiyun 			mvpp2_prs_vid_enable_filtering(port);
4921*4882a593Smuzhiyun 		} else {
4922*4882a593Smuzhiyun 			/* Invalidate all registered VID filters for this
4923*4882a593Smuzhiyun 			 * port
4924*4882a593Smuzhiyun 			 */
4925*4882a593Smuzhiyun 			mvpp2_prs_vid_remove_all(port);
4926*4882a593Smuzhiyun 
4927*4882a593Smuzhiyun 			mvpp2_prs_vid_disable_filtering(port);
4928*4882a593Smuzhiyun 		}
4929*4882a593Smuzhiyun 	}
4930*4882a593Smuzhiyun 
4931*4882a593Smuzhiyun 	if (changed & NETIF_F_RXHASH) {
4932*4882a593Smuzhiyun 		if (features & NETIF_F_RXHASH)
4933*4882a593Smuzhiyun 			mvpp22_port_rss_enable(port);
4934*4882a593Smuzhiyun 		else
4935*4882a593Smuzhiyun 			mvpp22_port_rss_disable(port);
4936*4882a593Smuzhiyun 	}
4937*4882a593Smuzhiyun 
4938*4882a593Smuzhiyun 	return 0;
4939*4882a593Smuzhiyun }
4940*4882a593Smuzhiyun 
mvpp2_xdp_setup(struct mvpp2_port * port,struct netdev_bpf * bpf)4941*4882a593Smuzhiyun static int mvpp2_xdp_setup(struct mvpp2_port *port, struct netdev_bpf *bpf)
4942*4882a593Smuzhiyun {
4943*4882a593Smuzhiyun 	struct bpf_prog *prog = bpf->prog, *old_prog;
4944*4882a593Smuzhiyun 	bool running = netif_running(port->dev);
4945*4882a593Smuzhiyun 	bool reset = !prog != !port->xdp_prog;
4946*4882a593Smuzhiyun 
4947*4882a593Smuzhiyun 	if (port->dev->mtu > MVPP2_MAX_RX_BUF_SIZE) {
4948*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
4949*4882a593Smuzhiyun 		return -EOPNOTSUPP;
4950*4882a593Smuzhiyun 	}
4951*4882a593Smuzhiyun 
4952*4882a593Smuzhiyun 	if (!port->priv->percpu_pools) {
4953*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(bpf->extack, "Per CPU Pools required for XDP");
4954*4882a593Smuzhiyun 		return -EOPNOTSUPP;
4955*4882a593Smuzhiyun 	}
4956*4882a593Smuzhiyun 
4957*4882a593Smuzhiyun 	if (port->ntxqs < num_possible_cpus() * 2) {
4958*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(bpf->extack, "XDP_TX needs two TX queues per CPU");
4959*4882a593Smuzhiyun 		return -EOPNOTSUPP;
4960*4882a593Smuzhiyun 	}
4961*4882a593Smuzhiyun 
4962*4882a593Smuzhiyun 	/* device is up and bpf is added/removed, must setup the RX queues */
4963*4882a593Smuzhiyun 	if (running && reset)
4964*4882a593Smuzhiyun 		mvpp2_stop(port->dev);
4965*4882a593Smuzhiyun 
4966*4882a593Smuzhiyun 	old_prog = xchg(&port->xdp_prog, prog);
4967*4882a593Smuzhiyun 	if (old_prog)
4968*4882a593Smuzhiyun 		bpf_prog_put(old_prog);
4969*4882a593Smuzhiyun 
4970*4882a593Smuzhiyun 	/* bpf is just replaced, RXQ and MTU are already setup */
4971*4882a593Smuzhiyun 	if (!reset)
4972*4882a593Smuzhiyun 		return 0;
4973*4882a593Smuzhiyun 
4974*4882a593Smuzhiyun 	/* device was up, restore the link */
4975*4882a593Smuzhiyun 	if (running)
4976*4882a593Smuzhiyun 		mvpp2_open(port->dev);
4977*4882a593Smuzhiyun 
4978*4882a593Smuzhiyun 	/* Check Page Pool DMA Direction */
4979*4882a593Smuzhiyun 	mvpp2_check_pagepool_dma(port);
4980*4882a593Smuzhiyun 
4981*4882a593Smuzhiyun 	return 0;
4982*4882a593Smuzhiyun }
4983*4882a593Smuzhiyun 
mvpp2_xdp(struct net_device * dev,struct netdev_bpf * xdp)4984*4882a593Smuzhiyun static int mvpp2_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4985*4882a593Smuzhiyun {
4986*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
4987*4882a593Smuzhiyun 
4988*4882a593Smuzhiyun 	switch (xdp->command) {
4989*4882a593Smuzhiyun 	case XDP_SETUP_PROG:
4990*4882a593Smuzhiyun 		return mvpp2_xdp_setup(port, xdp);
4991*4882a593Smuzhiyun 	default:
4992*4882a593Smuzhiyun 		return -EINVAL;
4993*4882a593Smuzhiyun 	}
4994*4882a593Smuzhiyun }
4995*4882a593Smuzhiyun 
4996*4882a593Smuzhiyun /* Ethtool methods */
4997*4882a593Smuzhiyun 
mvpp2_ethtool_nway_reset(struct net_device * dev)4998*4882a593Smuzhiyun static int mvpp2_ethtool_nway_reset(struct net_device *dev)
4999*4882a593Smuzhiyun {
5000*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5001*4882a593Smuzhiyun 
5002*4882a593Smuzhiyun 	if (!port->phylink)
5003*4882a593Smuzhiyun 		return -ENOTSUPP;
5004*4882a593Smuzhiyun 
5005*4882a593Smuzhiyun 	return phylink_ethtool_nway_reset(port->phylink);
5006*4882a593Smuzhiyun }
5007*4882a593Smuzhiyun 
5008*4882a593Smuzhiyun /* Set interrupt coalescing for ethtools */
mvpp2_ethtool_set_coalesce(struct net_device * dev,struct ethtool_coalesce * c)5009*4882a593Smuzhiyun static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
5010*4882a593Smuzhiyun 				      struct ethtool_coalesce *c)
5011*4882a593Smuzhiyun {
5012*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5013*4882a593Smuzhiyun 	int queue;
5014*4882a593Smuzhiyun 
5015*4882a593Smuzhiyun 	for (queue = 0; queue < port->nrxqs; queue++) {
5016*4882a593Smuzhiyun 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
5017*4882a593Smuzhiyun 
5018*4882a593Smuzhiyun 		rxq->time_coal = c->rx_coalesce_usecs;
5019*4882a593Smuzhiyun 		rxq->pkts_coal = c->rx_max_coalesced_frames;
5020*4882a593Smuzhiyun 		mvpp2_rx_pkts_coal_set(port, rxq);
5021*4882a593Smuzhiyun 		mvpp2_rx_time_coal_set(port, rxq);
5022*4882a593Smuzhiyun 	}
5023*4882a593Smuzhiyun 
5024*4882a593Smuzhiyun 	if (port->has_tx_irqs) {
5025*4882a593Smuzhiyun 		port->tx_time_coal = c->tx_coalesce_usecs;
5026*4882a593Smuzhiyun 		mvpp2_tx_time_coal_set(port);
5027*4882a593Smuzhiyun 	}
5028*4882a593Smuzhiyun 
5029*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
5030*4882a593Smuzhiyun 		struct mvpp2_tx_queue *txq = port->txqs[queue];
5031*4882a593Smuzhiyun 
5032*4882a593Smuzhiyun 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
5033*4882a593Smuzhiyun 
5034*4882a593Smuzhiyun 		if (port->has_tx_irqs)
5035*4882a593Smuzhiyun 			mvpp2_tx_pkts_coal_set(port, txq);
5036*4882a593Smuzhiyun 	}
5037*4882a593Smuzhiyun 
5038*4882a593Smuzhiyun 	return 0;
5039*4882a593Smuzhiyun }
5040*4882a593Smuzhiyun 
5041*4882a593Smuzhiyun /* get coalescing for ethtools */
mvpp2_ethtool_get_coalesce(struct net_device * dev,struct ethtool_coalesce * c)5042*4882a593Smuzhiyun static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
5043*4882a593Smuzhiyun 				      struct ethtool_coalesce *c)
5044*4882a593Smuzhiyun {
5045*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5046*4882a593Smuzhiyun 
5047*4882a593Smuzhiyun 	c->rx_coalesce_usecs       = port->rxqs[0]->time_coal;
5048*4882a593Smuzhiyun 	c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
5049*4882a593Smuzhiyun 	c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
5050*4882a593Smuzhiyun 	c->tx_coalesce_usecs       = port->tx_time_coal;
5051*4882a593Smuzhiyun 	return 0;
5052*4882a593Smuzhiyun }
5053*4882a593Smuzhiyun 
mvpp2_ethtool_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)5054*4882a593Smuzhiyun static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
5055*4882a593Smuzhiyun 				      struct ethtool_drvinfo *drvinfo)
5056*4882a593Smuzhiyun {
5057*4882a593Smuzhiyun 	strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
5058*4882a593Smuzhiyun 		sizeof(drvinfo->driver));
5059*4882a593Smuzhiyun 	strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
5060*4882a593Smuzhiyun 		sizeof(drvinfo->version));
5061*4882a593Smuzhiyun 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
5062*4882a593Smuzhiyun 		sizeof(drvinfo->bus_info));
5063*4882a593Smuzhiyun }
5064*4882a593Smuzhiyun 
mvpp2_ethtool_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)5065*4882a593Smuzhiyun static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
5066*4882a593Smuzhiyun 					struct ethtool_ringparam *ring)
5067*4882a593Smuzhiyun {
5068*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5069*4882a593Smuzhiyun 
5070*4882a593Smuzhiyun 	ring->rx_max_pending = MVPP2_MAX_RXD_MAX;
5071*4882a593Smuzhiyun 	ring->tx_max_pending = MVPP2_MAX_TXD_MAX;
5072*4882a593Smuzhiyun 	ring->rx_pending = port->rx_ring_size;
5073*4882a593Smuzhiyun 	ring->tx_pending = port->tx_ring_size;
5074*4882a593Smuzhiyun }
5075*4882a593Smuzhiyun 
mvpp2_ethtool_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring)5076*4882a593Smuzhiyun static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
5077*4882a593Smuzhiyun 				       struct ethtool_ringparam *ring)
5078*4882a593Smuzhiyun {
5079*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5080*4882a593Smuzhiyun 	u16 prev_rx_ring_size = port->rx_ring_size;
5081*4882a593Smuzhiyun 	u16 prev_tx_ring_size = port->tx_ring_size;
5082*4882a593Smuzhiyun 	int err;
5083*4882a593Smuzhiyun 
5084*4882a593Smuzhiyun 	err = mvpp2_check_ringparam_valid(dev, ring);
5085*4882a593Smuzhiyun 	if (err)
5086*4882a593Smuzhiyun 		return err;
5087*4882a593Smuzhiyun 
5088*4882a593Smuzhiyun 	if (!netif_running(dev)) {
5089*4882a593Smuzhiyun 		port->rx_ring_size = ring->rx_pending;
5090*4882a593Smuzhiyun 		port->tx_ring_size = ring->tx_pending;
5091*4882a593Smuzhiyun 		return 0;
5092*4882a593Smuzhiyun 	}
5093*4882a593Smuzhiyun 
5094*4882a593Smuzhiyun 	/* The interface is running, so we have to force a
5095*4882a593Smuzhiyun 	 * reallocation of the queues
5096*4882a593Smuzhiyun 	 */
5097*4882a593Smuzhiyun 	mvpp2_stop_dev(port);
5098*4882a593Smuzhiyun 	mvpp2_cleanup_rxqs(port);
5099*4882a593Smuzhiyun 	mvpp2_cleanup_txqs(port);
5100*4882a593Smuzhiyun 
5101*4882a593Smuzhiyun 	port->rx_ring_size = ring->rx_pending;
5102*4882a593Smuzhiyun 	port->tx_ring_size = ring->tx_pending;
5103*4882a593Smuzhiyun 
5104*4882a593Smuzhiyun 	err = mvpp2_setup_rxqs(port);
5105*4882a593Smuzhiyun 	if (err) {
5106*4882a593Smuzhiyun 		/* Reallocate Rx queues with the original ring size */
5107*4882a593Smuzhiyun 		port->rx_ring_size = prev_rx_ring_size;
5108*4882a593Smuzhiyun 		ring->rx_pending = prev_rx_ring_size;
5109*4882a593Smuzhiyun 		err = mvpp2_setup_rxqs(port);
5110*4882a593Smuzhiyun 		if (err)
5111*4882a593Smuzhiyun 			goto err_out;
5112*4882a593Smuzhiyun 	}
5113*4882a593Smuzhiyun 	err = mvpp2_setup_txqs(port);
5114*4882a593Smuzhiyun 	if (err) {
5115*4882a593Smuzhiyun 		/* Reallocate Tx queues with the original ring size */
5116*4882a593Smuzhiyun 		port->tx_ring_size = prev_tx_ring_size;
5117*4882a593Smuzhiyun 		ring->tx_pending = prev_tx_ring_size;
5118*4882a593Smuzhiyun 		err = mvpp2_setup_txqs(port);
5119*4882a593Smuzhiyun 		if (err)
5120*4882a593Smuzhiyun 			goto err_clean_rxqs;
5121*4882a593Smuzhiyun 	}
5122*4882a593Smuzhiyun 
5123*4882a593Smuzhiyun 	mvpp2_start_dev(port);
5124*4882a593Smuzhiyun 	mvpp2_egress_enable(port);
5125*4882a593Smuzhiyun 	mvpp2_ingress_enable(port);
5126*4882a593Smuzhiyun 
5127*4882a593Smuzhiyun 	return 0;
5128*4882a593Smuzhiyun 
5129*4882a593Smuzhiyun err_clean_rxqs:
5130*4882a593Smuzhiyun 	mvpp2_cleanup_rxqs(port);
5131*4882a593Smuzhiyun err_out:
5132*4882a593Smuzhiyun 	netdev_err(dev, "failed to change ring parameters");
5133*4882a593Smuzhiyun 	return err;
5134*4882a593Smuzhiyun }
5135*4882a593Smuzhiyun 
mvpp2_ethtool_get_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5136*4882a593Smuzhiyun static void mvpp2_ethtool_get_pause_param(struct net_device *dev,
5137*4882a593Smuzhiyun 					  struct ethtool_pauseparam *pause)
5138*4882a593Smuzhiyun {
5139*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5140*4882a593Smuzhiyun 
5141*4882a593Smuzhiyun 	if (!port->phylink)
5142*4882a593Smuzhiyun 		return;
5143*4882a593Smuzhiyun 
5144*4882a593Smuzhiyun 	phylink_ethtool_get_pauseparam(port->phylink, pause);
5145*4882a593Smuzhiyun }
5146*4882a593Smuzhiyun 
mvpp2_ethtool_set_pause_param(struct net_device * dev,struct ethtool_pauseparam * pause)5147*4882a593Smuzhiyun static int mvpp2_ethtool_set_pause_param(struct net_device *dev,
5148*4882a593Smuzhiyun 					 struct ethtool_pauseparam *pause)
5149*4882a593Smuzhiyun {
5150*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5151*4882a593Smuzhiyun 
5152*4882a593Smuzhiyun 	if (!port->phylink)
5153*4882a593Smuzhiyun 		return -ENOTSUPP;
5154*4882a593Smuzhiyun 
5155*4882a593Smuzhiyun 	return phylink_ethtool_set_pauseparam(port->phylink, pause);
5156*4882a593Smuzhiyun }
5157*4882a593Smuzhiyun 
mvpp2_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)5158*4882a593Smuzhiyun static int mvpp2_ethtool_get_link_ksettings(struct net_device *dev,
5159*4882a593Smuzhiyun 					    struct ethtool_link_ksettings *cmd)
5160*4882a593Smuzhiyun {
5161*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5162*4882a593Smuzhiyun 
5163*4882a593Smuzhiyun 	if (!port->phylink)
5164*4882a593Smuzhiyun 		return -ENOTSUPP;
5165*4882a593Smuzhiyun 
5166*4882a593Smuzhiyun 	return phylink_ethtool_ksettings_get(port->phylink, cmd);
5167*4882a593Smuzhiyun }
5168*4882a593Smuzhiyun 
mvpp2_ethtool_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)5169*4882a593Smuzhiyun static int mvpp2_ethtool_set_link_ksettings(struct net_device *dev,
5170*4882a593Smuzhiyun 					    const struct ethtool_link_ksettings *cmd)
5171*4882a593Smuzhiyun {
5172*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5173*4882a593Smuzhiyun 
5174*4882a593Smuzhiyun 	if (!port->phylink)
5175*4882a593Smuzhiyun 		return -ENOTSUPP;
5176*4882a593Smuzhiyun 
5177*4882a593Smuzhiyun 	return phylink_ethtool_ksettings_set(port->phylink, cmd);
5178*4882a593Smuzhiyun }
5179*4882a593Smuzhiyun 
mvpp2_ethtool_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)5180*4882a593Smuzhiyun static int mvpp2_ethtool_get_rxnfc(struct net_device *dev,
5181*4882a593Smuzhiyun 				   struct ethtool_rxnfc *info, u32 *rules)
5182*4882a593Smuzhiyun {
5183*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5184*4882a593Smuzhiyun 	int ret = 0, i, loc = 0;
5185*4882a593Smuzhiyun 
5186*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5187*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5188*4882a593Smuzhiyun 
5189*4882a593Smuzhiyun 	switch (info->cmd) {
5190*4882a593Smuzhiyun 	case ETHTOOL_GRXFH:
5191*4882a593Smuzhiyun 		ret = mvpp2_ethtool_rxfh_get(port, info);
5192*4882a593Smuzhiyun 		break;
5193*4882a593Smuzhiyun 	case ETHTOOL_GRXRINGS:
5194*4882a593Smuzhiyun 		info->data = port->nrxqs;
5195*4882a593Smuzhiyun 		break;
5196*4882a593Smuzhiyun 	case ETHTOOL_GRXCLSRLCNT:
5197*4882a593Smuzhiyun 		info->rule_cnt = port->n_rfs_rules;
5198*4882a593Smuzhiyun 		break;
5199*4882a593Smuzhiyun 	case ETHTOOL_GRXCLSRULE:
5200*4882a593Smuzhiyun 		ret = mvpp2_ethtool_cls_rule_get(port, info);
5201*4882a593Smuzhiyun 		break;
5202*4882a593Smuzhiyun 	case ETHTOOL_GRXCLSRLALL:
5203*4882a593Smuzhiyun 		for (i = 0; i < MVPP2_N_RFS_ENTRIES_PER_FLOW; i++) {
5204*4882a593Smuzhiyun 			if (port->rfs_rules[i])
5205*4882a593Smuzhiyun 				rules[loc++] = i;
5206*4882a593Smuzhiyun 		}
5207*4882a593Smuzhiyun 		break;
5208*4882a593Smuzhiyun 	default:
5209*4882a593Smuzhiyun 		return -ENOTSUPP;
5210*4882a593Smuzhiyun 	}
5211*4882a593Smuzhiyun 
5212*4882a593Smuzhiyun 	return ret;
5213*4882a593Smuzhiyun }
5214*4882a593Smuzhiyun 
mvpp2_ethtool_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)5215*4882a593Smuzhiyun static int mvpp2_ethtool_set_rxnfc(struct net_device *dev,
5216*4882a593Smuzhiyun 				   struct ethtool_rxnfc *info)
5217*4882a593Smuzhiyun {
5218*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5219*4882a593Smuzhiyun 	int ret = 0;
5220*4882a593Smuzhiyun 
5221*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5222*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5223*4882a593Smuzhiyun 
5224*4882a593Smuzhiyun 	switch (info->cmd) {
5225*4882a593Smuzhiyun 	case ETHTOOL_SRXFH:
5226*4882a593Smuzhiyun 		ret = mvpp2_ethtool_rxfh_set(port, info);
5227*4882a593Smuzhiyun 		break;
5228*4882a593Smuzhiyun 	case ETHTOOL_SRXCLSRLINS:
5229*4882a593Smuzhiyun 		ret = mvpp2_ethtool_cls_rule_ins(port, info);
5230*4882a593Smuzhiyun 		break;
5231*4882a593Smuzhiyun 	case ETHTOOL_SRXCLSRLDEL:
5232*4882a593Smuzhiyun 		ret = mvpp2_ethtool_cls_rule_del(port, info);
5233*4882a593Smuzhiyun 		break;
5234*4882a593Smuzhiyun 	default:
5235*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5236*4882a593Smuzhiyun 	}
5237*4882a593Smuzhiyun 	return ret;
5238*4882a593Smuzhiyun }
5239*4882a593Smuzhiyun 
mvpp2_ethtool_get_rxfh_indir_size(struct net_device * dev)5240*4882a593Smuzhiyun static u32 mvpp2_ethtool_get_rxfh_indir_size(struct net_device *dev)
5241*4882a593Smuzhiyun {
5242*4882a593Smuzhiyun 	return mvpp22_rss_is_supported() ? MVPP22_RSS_TABLE_ENTRIES : 0;
5243*4882a593Smuzhiyun }
5244*4882a593Smuzhiyun 
mvpp2_ethtool_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)5245*4882a593Smuzhiyun static int mvpp2_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
5246*4882a593Smuzhiyun 				  u8 *hfunc)
5247*4882a593Smuzhiyun {
5248*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5249*4882a593Smuzhiyun 	int ret = 0;
5250*4882a593Smuzhiyun 
5251*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5252*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5253*4882a593Smuzhiyun 
5254*4882a593Smuzhiyun 	if (indir)
5255*4882a593Smuzhiyun 		ret = mvpp22_port_rss_ctx_indir_get(port, 0, indir);
5256*4882a593Smuzhiyun 
5257*4882a593Smuzhiyun 	if (hfunc)
5258*4882a593Smuzhiyun 		*hfunc = ETH_RSS_HASH_CRC32;
5259*4882a593Smuzhiyun 
5260*4882a593Smuzhiyun 	return ret;
5261*4882a593Smuzhiyun }
5262*4882a593Smuzhiyun 
mvpp2_ethtool_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)5263*4882a593Smuzhiyun static int mvpp2_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
5264*4882a593Smuzhiyun 				  const u8 *key, const u8 hfunc)
5265*4882a593Smuzhiyun {
5266*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5267*4882a593Smuzhiyun 	int ret = 0;
5268*4882a593Smuzhiyun 
5269*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5270*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5271*4882a593Smuzhiyun 
5272*4882a593Smuzhiyun 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
5273*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5274*4882a593Smuzhiyun 
5275*4882a593Smuzhiyun 	if (key)
5276*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5277*4882a593Smuzhiyun 
5278*4882a593Smuzhiyun 	if (indir)
5279*4882a593Smuzhiyun 		ret = mvpp22_port_rss_ctx_indir_set(port, 0, indir);
5280*4882a593Smuzhiyun 
5281*4882a593Smuzhiyun 	return ret;
5282*4882a593Smuzhiyun }
5283*4882a593Smuzhiyun 
mvpp2_ethtool_get_rxfh_context(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc,u32 rss_context)5284*4882a593Smuzhiyun static int mvpp2_ethtool_get_rxfh_context(struct net_device *dev, u32 *indir,
5285*4882a593Smuzhiyun 					  u8 *key, u8 *hfunc, u32 rss_context)
5286*4882a593Smuzhiyun {
5287*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5288*4882a593Smuzhiyun 	int ret = 0;
5289*4882a593Smuzhiyun 
5290*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5291*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5292*4882a593Smuzhiyun 	if (rss_context >= MVPP22_N_RSS_TABLES)
5293*4882a593Smuzhiyun 		return -EINVAL;
5294*4882a593Smuzhiyun 
5295*4882a593Smuzhiyun 	if (hfunc)
5296*4882a593Smuzhiyun 		*hfunc = ETH_RSS_HASH_CRC32;
5297*4882a593Smuzhiyun 
5298*4882a593Smuzhiyun 	if (indir)
5299*4882a593Smuzhiyun 		ret = mvpp22_port_rss_ctx_indir_get(port, rss_context, indir);
5300*4882a593Smuzhiyun 
5301*4882a593Smuzhiyun 	return ret;
5302*4882a593Smuzhiyun }
5303*4882a593Smuzhiyun 
mvpp2_ethtool_set_rxfh_context(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc,u32 * rss_context,bool delete)5304*4882a593Smuzhiyun static int mvpp2_ethtool_set_rxfh_context(struct net_device *dev,
5305*4882a593Smuzhiyun 					  const u32 *indir, const u8 *key,
5306*4882a593Smuzhiyun 					  const u8 hfunc, u32 *rss_context,
5307*4882a593Smuzhiyun 					  bool delete)
5308*4882a593Smuzhiyun {
5309*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5310*4882a593Smuzhiyun 	int ret;
5311*4882a593Smuzhiyun 
5312*4882a593Smuzhiyun 	if (!mvpp22_rss_is_supported())
5313*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5314*4882a593Smuzhiyun 
5315*4882a593Smuzhiyun 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_CRC32)
5316*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5317*4882a593Smuzhiyun 
5318*4882a593Smuzhiyun 	if (key)
5319*4882a593Smuzhiyun 		return -EOPNOTSUPP;
5320*4882a593Smuzhiyun 
5321*4882a593Smuzhiyun 	if (delete)
5322*4882a593Smuzhiyun 		return mvpp22_port_rss_ctx_delete(port, *rss_context);
5323*4882a593Smuzhiyun 
5324*4882a593Smuzhiyun 	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
5325*4882a593Smuzhiyun 		ret = mvpp22_port_rss_ctx_create(port, rss_context);
5326*4882a593Smuzhiyun 		if (ret)
5327*4882a593Smuzhiyun 			return ret;
5328*4882a593Smuzhiyun 	}
5329*4882a593Smuzhiyun 
5330*4882a593Smuzhiyun 	return mvpp22_port_rss_ctx_indir_set(port, *rss_context, indir);
5331*4882a593Smuzhiyun }
5332*4882a593Smuzhiyun /* Device ops */
5333*4882a593Smuzhiyun 
5334*4882a593Smuzhiyun static const struct net_device_ops mvpp2_netdev_ops = {
5335*4882a593Smuzhiyun 	.ndo_open		= mvpp2_open,
5336*4882a593Smuzhiyun 	.ndo_stop		= mvpp2_stop,
5337*4882a593Smuzhiyun 	.ndo_start_xmit		= mvpp2_tx,
5338*4882a593Smuzhiyun 	.ndo_set_rx_mode	= mvpp2_set_rx_mode,
5339*4882a593Smuzhiyun 	.ndo_set_mac_address	= mvpp2_set_mac_address,
5340*4882a593Smuzhiyun 	.ndo_change_mtu		= mvpp2_change_mtu,
5341*4882a593Smuzhiyun 	.ndo_get_stats64	= mvpp2_get_stats64,
5342*4882a593Smuzhiyun 	.ndo_do_ioctl		= mvpp2_ioctl,
5343*4882a593Smuzhiyun 	.ndo_vlan_rx_add_vid	= mvpp2_vlan_rx_add_vid,
5344*4882a593Smuzhiyun 	.ndo_vlan_rx_kill_vid	= mvpp2_vlan_rx_kill_vid,
5345*4882a593Smuzhiyun 	.ndo_set_features	= mvpp2_set_features,
5346*4882a593Smuzhiyun 	.ndo_bpf		= mvpp2_xdp,
5347*4882a593Smuzhiyun 	.ndo_xdp_xmit		= mvpp2_xdp_xmit,
5348*4882a593Smuzhiyun };
5349*4882a593Smuzhiyun 
5350*4882a593Smuzhiyun static const struct ethtool_ops mvpp2_eth_tool_ops = {
5351*4882a593Smuzhiyun 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
5352*4882a593Smuzhiyun 				     ETHTOOL_COALESCE_MAX_FRAMES,
5353*4882a593Smuzhiyun 	.nway_reset		= mvpp2_ethtool_nway_reset,
5354*4882a593Smuzhiyun 	.get_link		= ethtool_op_get_link,
5355*4882a593Smuzhiyun 	.get_ts_info		= mvpp2_ethtool_get_ts_info,
5356*4882a593Smuzhiyun 	.set_coalesce		= mvpp2_ethtool_set_coalesce,
5357*4882a593Smuzhiyun 	.get_coalesce		= mvpp2_ethtool_get_coalesce,
5358*4882a593Smuzhiyun 	.get_drvinfo		= mvpp2_ethtool_get_drvinfo,
5359*4882a593Smuzhiyun 	.get_ringparam		= mvpp2_ethtool_get_ringparam,
5360*4882a593Smuzhiyun 	.set_ringparam		= mvpp2_ethtool_set_ringparam,
5361*4882a593Smuzhiyun 	.get_strings		= mvpp2_ethtool_get_strings,
5362*4882a593Smuzhiyun 	.get_ethtool_stats	= mvpp2_ethtool_get_stats,
5363*4882a593Smuzhiyun 	.get_sset_count		= mvpp2_ethtool_get_sset_count,
5364*4882a593Smuzhiyun 	.get_pauseparam		= mvpp2_ethtool_get_pause_param,
5365*4882a593Smuzhiyun 	.set_pauseparam		= mvpp2_ethtool_set_pause_param,
5366*4882a593Smuzhiyun 	.get_link_ksettings	= mvpp2_ethtool_get_link_ksettings,
5367*4882a593Smuzhiyun 	.set_link_ksettings	= mvpp2_ethtool_set_link_ksettings,
5368*4882a593Smuzhiyun 	.get_rxnfc		= mvpp2_ethtool_get_rxnfc,
5369*4882a593Smuzhiyun 	.set_rxnfc		= mvpp2_ethtool_set_rxnfc,
5370*4882a593Smuzhiyun 	.get_rxfh_indir_size	= mvpp2_ethtool_get_rxfh_indir_size,
5371*4882a593Smuzhiyun 	.get_rxfh		= mvpp2_ethtool_get_rxfh,
5372*4882a593Smuzhiyun 	.set_rxfh		= mvpp2_ethtool_set_rxfh,
5373*4882a593Smuzhiyun 	.get_rxfh_context	= mvpp2_ethtool_get_rxfh_context,
5374*4882a593Smuzhiyun 	.set_rxfh_context	= mvpp2_ethtool_set_rxfh_context,
5375*4882a593Smuzhiyun };
5376*4882a593Smuzhiyun 
5377*4882a593Smuzhiyun /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
5378*4882a593Smuzhiyun  * had a single IRQ defined per-port.
5379*4882a593Smuzhiyun  */
mvpp2_simple_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5380*4882a593Smuzhiyun static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
5381*4882a593Smuzhiyun 					   struct device_node *port_node)
5382*4882a593Smuzhiyun {
5383*4882a593Smuzhiyun 	struct mvpp2_queue_vector *v = &port->qvecs[0];
5384*4882a593Smuzhiyun 
5385*4882a593Smuzhiyun 	v->first_rxq = 0;
5386*4882a593Smuzhiyun 	v->nrxqs = port->nrxqs;
5387*4882a593Smuzhiyun 	v->type = MVPP2_QUEUE_VECTOR_SHARED;
5388*4882a593Smuzhiyun 	v->sw_thread_id = 0;
5389*4882a593Smuzhiyun 	v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
5390*4882a593Smuzhiyun 	v->port = port;
5391*4882a593Smuzhiyun 	v->irq = irq_of_parse_and_map(port_node, 0);
5392*4882a593Smuzhiyun 	if (v->irq <= 0)
5393*4882a593Smuzhiyun 		return -EINVAL;
5394*4882a593Smuzhiyun 	netif_napi_add(port->dev, &v->napi, mvpp2_poll,
5395*4882a593Smuzhiyun 		       NAPI_POLL_WEIGHT);
5396*4882a593Smuzhiyun 
5397*4882a593Smuzhiyun 	port->nqvecs = 1;
5398*4882a593Smuzhiyun 
5399*4882a593Smuzhiyun 	return 0;
5400*4882a593Smuzhiyun }
5401*4882a593Smuzhiyun 
mvpp2_multi_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5402*4882a593Smuzhiyun static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
5403*4882a593Smuzhiyun 					  struct device_node *port_node)
5404*4882a593Smuzhiyun {
5405*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
5406*4882a593Smuzhiyun 	struct mvpp2_queue_vector *v;
5407*4882a593Smuzhiyun 	int i, ret;
5408*4882a593Smuzhiyun 
5409*4882a593Smuzhiyun 	switch (queue_mode) {
5410*4882a593Smuzhiyun 	case MVPP2_QDIST_SINGLE_MODE:
5411*4882a593Smuzhiyun 		port->nqvecs = priv->nthreads + 1;
5412*4882a593Smuzhiyun 		break;
5413*4882a593Smuzhiyun 	case MVPP2_QDIST_MULTI_MODE:
5414*4882a593Smuzhiyun 		port->nqvecs = priv->nthreads;
5415*4882a593Smuzhiyun 		break;
5416*4882a593Smuzhiyun 	}
5417*4882a593Smuzhiyun 
5418*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
5419*4882a593Smuzhiyun 		char irqname[16];
5420*4882a593Smuzhiyun 
5421*4882a593Smuzhiyun 		v = port->qvecs + i;
5422*4882a593Smuzhiyun 
5423*4882a593Smuzhiyun 		v->port = port;
5424*4882a593Smuzhiyun 		v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
5425*4882a593Smuzhiyun 		v->sw_thread_id = i;
5426*4882a593Smuzhiyun 		v->sw_thread_mask = BIT(i);
5427*4882a593Smuzhiyun 
5428*4882a593Smuzhiyun 		if (port->flags & MVPP2_F_DT_COMPAT)
5429*4882a593Smuzhiyun 			snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
5430*4882a593Smuzhiyun 		else
5431*4882a593Smuzhiyun 			snprintf(irqname, sizeof(irqname), "hif%d", i);
5432*4882a593Smuzhiyun 
5433*4882a593Smuzhiyun 		if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
5434*4882a593Smuzhiyun 			v->first_rxq = i;
5435*4882a593Smuzhiyun 			v->nrxqs = 1;
5436*4882a593Smuzhiyun 		} else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
5437*4882a593Smuzhiyun 			   i == (port->nqvecs - 1)) {
5438*4882a593Smuzhiyun 			v->first_rxq = 0;
5439*4882a593Smuzhiyun 			v->nrxqs = port->nrxqs;
5440*4882a593Smuzhiyun 			v->type = MVPP2_QUEUE_VECTOR_SHARED;
5441*4882a593Smuzhiyun 
5442*4882a593Smuzhiyun 			if (port->flags & MVPP2_F_DT_COMPAT)
5443*4882a593Smuzhiyun 				strncpy(irqname, "rx-shared", sizeof(irqname));
5444*4882a593Smuzhiyun 		}
5445*4882a593Smuzhiyun 
5446*4882a593Smuzhiyun 		if (port_node)
5447*4882a593Smuzhiyun 			v->irq = of_irq_get_byname(port_node, irqname);
5448*4882a593Smuzhiyun 		else
5449*4882a593Smuzhiyun 			v->irq = fwnode_irq_get(port->fwnode, i);
5450*4882a593Smuzhiyun 		if (v->irq <= 0) {
5451*4882a593Smuzhiyun 			ret = -EINVAL;
5452*4882a593Smuzhiyun 			goto err;
5453*4882a593Smuzhiyun 		}
5454*4882a593Smuzhiyun 
5455*4882a593Smuzhiyun 		netif_napi_add(port->dev, &v->napi, mvpp2_poll,
5456*4882a593Smuzhiyun 			       NAPI_POLL_WEIGHT);
5457*4882a593Smuzhiyun 	}
5458*4882a593Smuzhiyun 
5459*4882a593Smuzhiyun 	return 0;
5460*4882a593Smuzhiyun 
5461*4882a593Smuzhiyun err:
5462*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
5463*4882a593Smuzhiyun 		irq_dispose_mapping(port->qvecs[i].irq);
5464*4882a593Smuzhiyun 	return ret;
5465*4882a593Smuzhiyun }
5466*4882a593Smuzhiyun 
mvpp2_queue_vectors_init(struct mvpp2_port * port,struct device_node * port_node)5467*4882a593Smuzhiyun static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
5468*4882a593Smuzhiyun 				    struct device_node *port_node)
5469*4882a593Smuzhiyun {
5470*4882a593Smuzhiyun 	if (port->has_tx_irqs)
5471*4882a593Smuzhiyun 		return mvpp2_multi_queue_vectors_init(port, port_node);
5472*4882a593Smuzhiyun 	else
5473*4882a593Smuzhiyun 		return mvpp2_simple_queue_vectors_init(port, port_node);
5474*4882a593Smuzhiyun }
5475*4882a593Smuzhiyun 
mvpp2_queue_vectors_deinit(struct mvpp2_port * port)5476*4882a593Smuzhiyun static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
5477*4882a593Smuzhiyun {
5478*4882a593Smuzhiyun 	int i;
5479*4882a593Smuzhiyun 
5480*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++)
5481*4882a593Smuzhiyun 		irq_dispose_mapping(port->qvecs[i].irq);
5482*4882a593Smuzhiyun }
5483*4882a593Smuzhiyun 
5484*4882a593Smuzhiyun /* Configure Rx queue group interrupt for this port */
mvpp2_rx_irqs_setup(struct mvpp2_port * port)5485*4882a593Smuzhiyun static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
5486*4882a593Smuzhiyun {
5487*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
5488*4882a593Smuzhiyun 	u32 val;
5489*4882a593Smuzhiyun 	int i;
5490*4882a593Smuzhiyun 
5491*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
5492*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
5493*4882a593Smuzhiyun 			    port->nrxqs);
5494*4882a593Smuzhiyun 		return;
5495*4882a593Smuzhiyun 	}
5496*4882a593Smuzhiyun 
5497*4882a593Smuzhiyun 	/* Handle the more complicated PPv2.2 case */
5498*4882a593Smuzhiyun 	for (i = 0; i < port->nqvecs; i++) {
5499*4882a593Smuzhiyun 		struct mvpp2_queue_vector *qv = port->qvecs + i;
5500*4882a593Smuzhiyun 
5501*4882a593Smuzhiyun 		if (!qv->nrxqs)
5502*4882a593Smuzhiyun 			continue;
5503*4882a593Smuzhiyun 
5504*4882a593Smuzhiyun 		val = qv->sw_thread_id;
5505*4882a593Smuzhiyun 		val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
5506*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
5507*4882a593Smuzhiyun 
5508*4882a593Smuzhiyun 		val = qv->first_rxq;
5509*4882a593Smuzhiyun 		val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
5510*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
5511*4882a593Smuzhiyun 	}
5512*4882a593Smuzhiyun }
5513*4882a593Smuzhiyun 
5514*4882a593Smuzhiyun /* Initialize port HW */
mvpp2_port_init(struct mvpp2_port * port)5515*4882a593Smuzhiyun static int mvpp2_port_init(struct mvpp2_port *port)
5516*4882a593Smuzhiyun {
5517*4882a593Smuzhiyun 	struct device *dev = port->dev->dev.parent;
5518*4882a593Smuzhiyun 	struct mvpp2 *priv = port->priv;
5519*4882a593Smuzhiyun 	struct mvpp2_txq_pcpu *txq_pcpu;
5520*4882a593Smuzhiyun 	unsigned int thread;
5521*4882a593Smuzhiyun 	int queue, err, val;
5522*4882a593Smuzhiyun 
5523*4882a593Smuzhiyun 	/* Checks for hardware constraints */
5524*4882a593Smuzhiyun 	if (port->first_rxq + port->nrxqs >
5525*4882a593Smuzhiyun 	    MVPP2_MAX_PORTS * priv->max_port_rxqs)
5526*4882a593Smuzhiyun 		return -EINVAL;
5527*4882a593Smuzhiyun 
5528*4882a593Smuzhiyun 	if (port->nrxqs > priv->max_port_rxqs || port->ntxqs > MVPP2_MAX_TXQ)
5529*4882a593Smuzhiyun 		return -EINVAL;
5530*4882a593Smuzhiyun 
5531*4882a593Smuzhiyun 	/* Disable port */
5532*4882a593Smuzhiyun 	mvpp2_egress_disable(port);
5533*4882a593Smuzhiyun 	mvpp2_port_disable(port);
5534*4882a593Smuzhiyun 
5535*4882a593Smuzhiyun 	if (mvpp2_is_xlg(port->phy_interface)) {
5536*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5537*4882a593Smuzhiyun 		val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
5538*4882a593Smuzhiyun 		val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
5539*4882a593Smuzhiyun 		writel(val, port->base + MVPP22_XLG_CTRL0_REG);
5540*4882a593Smuzhiyun 	} else {
5541*4882a593Smuzhiyun 		val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5542*4882a593Smuzhiyun 		val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
5543*4882a593Smuzhiyun 		val |= MVPP2_GMAC_FORCE_LINK_DOWN;
5544*4882a593Smuzhiyun 		writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5545*4882a593Smuzhiyun 	}
5546*4882a593Smuzhiyun 
5547*4882a593Smuzhiyun 	port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
5548*4882a593Smuzhiyun 
5549*4882a593Smuzhiyun 	port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
5550*4882a593Smuzhiyun 				  GFP_KERNEL);
5551*4882a593Smuzhiyun 	if (!port->txqs)
5552*4882a593Smuzhiyun 		return -ENOMEM;
5553*4882a593Smuzhiyun 
5554*4882a593Smuzhiyun 	/* Associate physical Tx queues to this port and initialize.
5555*4882a593Smuzhiyun 	 * The mapping is predefined.
5556*4882a593Smuzhiyun 	 */
5557*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
5558*4882a593Smuzhiyun 		int queue_phy_id = mvpp2_txq_phys(port->id, queue);
5559*4882a593Smuzhiyun 		struct mvpp2_tx_queue *txq;
5560*4882a593Smuzhiyun 
5561*4882a593Smuzhiyun 		txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
5562*4882a593Smuzhiyun 		if (!txq) {
5563*4882a593Smuzhiyun 			err = -ENOMEM;
5564*4882a593Smuzhiyun 			goto err_free_percpu;
5565*4882a593Smuzhiyun 		}
5566*4882a593Smuzhiyun 
5567*4882a593Smuzhiyun 		txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
5568*4882a593Smuzhiyun 		if (!txq->pcpu) {
5569*4882a593Smuzhiyun 			err = -ENOMEM;
5570*4882a593Smuzhiyun 			goto err_free_percpu;
5571*4882a593Smuzhiyun 		}
5572*4882a593Smuzhiyun 
5573*4882a593Smuzhiyun 		txq->id = queue_phy_id;
5574*4882a593Smuzhiyun 		txq->log_id = queue;
5575*4882a593Smuzhiyun 		txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
5576*4882a593Smuzhiyun 		for (thread = 0; thread < priv->nthreads; thread++) {
5577*4882a593Smuzhiyun 			txq_pcpu = per_cpu_ptr(txq->pcpu, thread);
5578*4882a593Smuzhiyun 			txq_pcpu->thread = thread;
5579*4882a593Smuzhiyun 		}
5580*4882a593Smuzhiyun 
5581*4882a593Smuzhiyun 		port->txqs[queue] = txq;
5582*4882a593Smuzhiyun 	}
5583*4882a593Smuzhiyun 
5584*4882a593Smuzhiyun 	port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
5585*4882a593Smuzhiyun 				  GFP_KERNEL);
5586*4882a593Smuzhiyun 	if (!port->rxqs) {
5587*4882a593Smuzhiyun 		err = -ENOMEM;
5588*4882a593Smuzhiyun 		goto err_free_percpu;
5589*4882a593Smuzhiyun 	}
5590*4882a593Smuzhiyun 
5591*4882a593Smuzhiyun 	/* Allocate and initialize Rx queue for this port */
5592*4882a593Smuzhiyun 	for (queue = 0; queue < port->nrxqs; queue++) {
5593*4882a593Smuzhiyun 		struct mvpp2_rx_queue *rxq;
5594*4882a593Smuzhiyun 
5595*4882a593Smuzhiyun 		/* Map physical Rx queue to port's logical Rx queue */
5596*4882a593Smuzhiyun 		rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
5597*4882a593Smuzhiyun 		if (!rxq) {
5598*4882a593Smuzhiyun 			err = -ENOMEM;
5599*4882a593Smuzhiyun 			goto err_free_percpu;
5600*4882a593Smuzhiyun 		}
5601*4882a593Smuzhiyun 		/* Map this Rx queue to a physical queue */
5602*4882a593Smuzhiyun 		rxq->id = port->first_rxq + queue;
5603*4882a593Smuzhiyun 		rxq->port = port->id;
5604*4882a593Smuzhiyun 		rxq->logic_rxq = queue;
5605*4882a593Smuzhiyun 
5606*4882a593Smuzhiyun 		port->rxqs[queue] = rxq;
5607*4882a593Smuzhiyun 	}
5608*4882a593Smuzhiyun 
5609*4882a593Smuzhiyun 	mvpp2_rx_irqs_setup(port);
5610*4882a593Smuzhiyun 
5611*4882a593Smuzhiyun 	/* Create Rx descriptor rings */
5612*4882a593Smuzhiyun 	for (queue = 0; queue < port->nrxqs; queue++) {
5613*4882a593Smuzhiyun 		struct mvpp2_rx_queue *rxq = port->rxqs[queue];
5614*4882a593Smuzhiyun 
5615*4882a593Smuzhiyun 		rxq->size = port->rx_ring_size;
5616*4882a593Smuzhiyun 		rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
5617*4882a593Smuzhiyun 		rxq->time_coal = MVPP2_RX_COAL_USEC;
5618*4882a593Smuzhiyun 	}
5619*4882a593Smuzhiyun 
5620*4882a593Smuzhiyun 	mvpp2_ingress_disable(port);
5621*4882a593Smuzhiyun 
5622*4882a593Smuzhiyun 	/* Port default configuration */
5623*4882a593Smuzhiyun 	mvpp2_defaults_set(port);
5624*4882a593Smuzhiyun 
5625*4882a593Smuzhiyun 	/* Port's classifier configuration */
5626*4882a593Smuzhiyun 	mvpp2_cls_oversize_rxq_set(port);
5627*4882a593Smuzhiyun 	mvpp2_cls_port_config(port);
5628*4882a593Smuzhiyun 
5629*4882a593Smuzhiyun 	if (mvpp22_rss_is_supported())
5630*4882a593Smuzhiyun 		mvpp22_port_rss_init(port);
5631*4882a593Smuzhiyun 
5632*4882a593Smuzhiyun 	/* Provide an initial Rx packet size */
5633*4882a593Smuzhiyun 	port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
5634*4882a593Smuzhiyun 
5635*4882a593Smuzhiyun 	/* Initialize pools for swf */
5636*4882a593Smuzhiyun 	err = mvpp2_swf_bm_pool_init(port);
5637*4882a593Smuzhiyun 	if (err)
5638*4882a593Smuzhiyun 		goto err_free_percpu;
5639*4882a593Smuzhiyun 
5640*4882a593Smuzhiyun 	/* Clear all port stats */
5641*4882a593Smuzhiyun 	mvpp2_read_stats(port);
5642*4882a593Smuzhiyun 	memset(port->ethtool_stats, 0,
5643*4882a593Smuzhiyun 	       MVPP2_N_ETHTOOL_STATS(port->ntxqs, port->nrxqs) * sizeof(u64));
5644*4882a593Smuzhiyun 
5645*4882a593Smuzhiyun 	return 0;
5646*4882a593Smuzhiyun 
5647*4882a593Smuzhiyun err_free_percpu:
5648*4882a593Smuzhiyun 	for (queue = 0; queue < port->ntxqs; queue++) {
5649*4882a593Smuzhiyun 		if (!port->txqs[queue])
5650*4882a593Smuzhiyun 			continue;
5651*4882a593Smuzhiyun 		free_percpu(port->txqs[queue]->pcpu);
5652*4882a593Smuzhiyun 	}
5653*4882a593Smuzhiyun 	return err;
5654*4882a593Smuzhiyun }
5655*4882a593Smuzhiyun 
mvpp22_port_has_legacy_tx_irqs(struct device_node * port_node,unsigned long * flags)5656*4882a593Smuzhiyun static bool mvpp22_port_has_legacy_tx_irqs(struct device_node *port_node,
5657*4882a593Smuzhiyun 					   unsigned long *flags)
5658*4882a593Smuzhiyun {
5659*4882a593Smuzhiyun 	char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1", "tx-cpu2",
5660*4882a593Smuzhiyun 			  "tx-cpu3" };
5661*4882a593Smuzhiyun 	int i;
5662*4882a593Smuzhiyun 
5663*4882a593Smuzhiyun 	for (i = 0; i < 5; i++)
5664*4882a593Smuzhiyun 		if (of_property_match_string(port_node, "interrupt-names",
5665*4882a593Smuzhiyun 					     irqs[i]) < 0)
5666*4882a593Smuzhiyun 			return false;
5667*4882a593Smuzhiyun 
5668*4882a593Smuzhiyun 	*flags |= MVPP2_F_DT_COMPAT;
5669*4882a593Smuzhiyun 	return true;
5670*4882a593Smuzhiyun }
5671*4882a593Smuzhiyun 
5672*4882a593Smuzhiyun /* Checks if the port dt description has the required Tx interrupts:
5673*4882a593Smuzhiyun  * - PPv2.1: there are no such interrupts.
5674*4882a593Smuzhiyun  * - PPv2.2:
5675*4882a593Smuzhiyun  *   - The old DTs have: "rx-shared", "tx-cpuX" with X in [0...3]
5676*4882a593Smuzhiyun  *   - The new ones have: "hifX" with X in [0..8]
5677*4882a593Smuzhiyun  *
5678*4882a593Smuzhiyun  * All those variants are supported to keep the backward compatibility.
5679*4882a593Smuzhiyun  */
mvpp2_port_has_irqs(struct mvpp2 * priv,struct device_node * port_node,unsigned long * flags)5680*4882a593Smuzhiyun static bool mvpp2_port_has_irqs(struct mvpp2 *priv,
5681*4882a593Smuzhiyun 				struct device_node *port_node,
5682*4882a593Smuzhiyun 				unsigned long *flags)
5683*4882a593Smuzhiyun {
5684*4882a593Smuzhiyun 	char name[5];
5685*4882a593Smuzhiyun 	int i;
5686*4882a593Smuzhiyun 
5687*4882a593Smuzhiyun 	/* ACPI */
5688*4882a593Smuzhiyun 	if (!port_node)
5689*4882a593Smuzhiyun 		return true;
5690*4882a593Smuzhiyun 
5691*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
5692*4882a593Smuzhiyun 		return false;
5693*4882a593Smuzhiyun 
5694*4882a593Smuzhiyun 	if (mvpp22_port_has_legacy_tx_irqs(port_node, flags))
5695*4882a593Smuzhiyun 		return true;
5696*4882a593Smuzhiyun 
5697*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
5698*4882a593Smuzhiyun 		snprintf(name, 5, "hif%d", i);
5699*4882a593Smuzhiyun 		if (of_property_match_string(port_node, "interrupt-names",
5700*4882a593Smuzhiyun 					     name) < 0)
5701*4882a593Smuzhiyun 			return false;
5702*4882a593Smuzhiyun 	}
5703*4882a593Smuzhiyun 
5704*4882a593Smuzhiyun 	return true;
5705*4882a593Smuzhiyun }
5706*4882a593Smuzhiyun 
mvpp2_port_copy_mac_addr(struct net_device * dev,struct mvpp2 * priv,struct fwnode_handle * fwnode,char ** mac_from)5707*4882a593Smuzhiyun static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
5708*4882a593Smuzhiyun 				     struct fwnode_handle *fwnode,
5709*4882a593Smuzhiyun 				     char **mac_from)
5710*4882a593Smuzhiyun {
5711*4882a593Smuzhiyun 	struct mvpp2_port *port = netdev_priv(dev);
5712*4882a593Smuzhiyun 	char hw_mac_addr[ETH_ALEN] = {0};
5713*4882a593Smuzhiyun 	char fw_mac_addr[ETH_ALEN];
5714*4882a593Smuzhiyun 
5715*4882a593Smuzhiyun 	if (fwnode_get_mac_address(fwnode, fw_mac_addr, ETH_ALEN)) {
5716*4882a593Smuzhiyun 		*mac_from = "firmware node";
5717*4882a593Smuzhiyun 		ether_addr_copy(dev->dev_addr, fw_mac_addr);
5718*4882a593Smuzhiyun 		return;
5719*4882a593Smuzhiyun 	}
5720*4882a593Smuzhiyun 
5721*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
5722*4882a593Smuzhiyun 		mvpp21_get_mac_address(port, hw_mac_addr);
5723*4882a593Smuzhiyun 		if (is_valid_ether_addr(hw_mac_addr)) {
5724*4882a593Smuzhiyun 			*mac_from = "hardware";
5725*4882a593Smuzhiyun 			ether_addr_copy(dev->dev_addr, hw_mac_addr);
5726*4882a593Smuzhiyun 			return;
5727*4882a593Smuzhiyun 		}
5728*4882a593Smuzhiyun 	}
5729*4882a593Smuzhiyun 
5730*4882a593Smuzhiyun 	*mac_from = "random";
5731*4882a593Smuzhiyun 	eth_hw_addr_random(dev);
5732*4882a593Smuzhiyun }
5733*4882a593Smuzhiyun 
mvpp2_phylink_to_port(struct phylink_config * config)5734*4882a593Smuzhiyun static struct mvpp2_port *mvpp2_phylink_to_port(struct phylink_config *config)
5735*4882a593Smuzhiyun {
5736*4882a593Smuzhiyun 	return container_of(config, struct mvpp2_port, phylink_config);
5737*4882a593Smuzhiyun }
5738*4882a593Smuzhiyun 
mvpp2_pcs_to_port(struct phylink_pcs * pcs)5739*4882a593Smuzhiyun static struct mvpp2_port *mvpp2_pcs_to_port(struct phylink_pcs *pcs)
5740*4882a593Smuzhiyun {
5741*4882a593Smuzhiyun 	return container_of(pcs, struct mvpp2_port, phylink_pcs);
5742*4882a593Smuzhiyun }
5743*4882a593Smuzhiyun 
mvpp2_xlg_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)5744*4882a593Smuzhiyun static void mvpp2_xlg_pcs_get_state(struct phylink_pcs *pcs,
5745*4882a593Smuzhiyun 				    struct phylink_link_state *state)
5746*4882a593Smuzhiyun {
5747*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5748*4882a593Smuzhiyun 	u32 val;
5749*4882a593Smuzhiyun 
5750*4882a593Smuzhiyun 	state->speed = SPEED_10000;
5751*4882a593Smuzhiyun 	state->duplex = 1;
5752*4882a593Smuzhiyun 	state->an_complete = 1;
5753*4882a593Smuzhiyun 
5754*4882a593Smuzhiyun 	val = readl(port->base + MVPP22_XLG_STATUS);
5755*4882a593Smuzhiyun 	state->link = !!(val & MVPP22_XLG_STATUS_LINK_UP);
5756*4882a593Smuzhiyun 
5757*4882a593Smuzhiyun 	state->pause = 0;
5758*4882a593Smuzhiyun 	val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5759*4882a593Smuzhiyun 	if (val & MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN)
5760*4882a593Smuzhiyun 		state->pause |= MLO_PAUSE_TX;
5761*4882a593Smuzhiyun 	if (val & MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN)
5762*4882a593Smuzhiyun 		state->pause |= MLO_PAUSE_RX;
5763*4882a593Smuzhiyun }
5764*4882a593Smuzhiyun 
mvpp2_xlg_pcs_config(struct phylink_pcs * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)5765*4882a593Smuzhiyun static int mvpp2_xlg_pcs_config(struct phylink_pcs *pcs,
5766*4882a593Smuzhiyun 				unsigned int mode,
5767*4882a593Smuzhiyun 				phy_interface_t interface,
5768*4882a593Smuzhiyun 				const unsigned long *advertising,
5769*4882a593Smuzhiyun 				bool permit_pause_to_mac)
5770*4882a593Smuzhiyun {
5771*4882a593Smuzhiyun 	return 0;
5772*4882a593Smuzhiyun }
5773*4882a593Smuzhiyun 
5774*4882a593Smuzhiyun static const struct phylink_pcs_ops mvpp2_phylink_xlg_pcs_ops = {
5775*4882a593Smuzhiyun 	.pcs_get_state = mvpp2_xlg_pcs_get_state,
5776*4882a593Smuzhiyun 	.pcs_config = mvpp2_xlg_pcs_config,
5777*4882a593Smuzhiyun };
5778*4882a593Smuzhiyun 
mvpp2_gmac_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)5779*4882a593Smuzhiyun static void mvpp2_gmac_pcs_get_state(struct phylink_pcs *pcs,
5780*4882a593Smuzhiyun 				     struct phylink_link_state *state)
5781*4882a593Smuzhiyun {
5782*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5783*4882a593Smuzhiyun 	u32 val;
5784*4882a593Smuzhiyun 
5785*4882a593Smuzhiyun 	val = readl(port->base + MVPP2_GMAC_STATUS0);
5786*4882a593Smuzhiyun 
5787*4882a593Smuzhiyun 	state->an_complete = !!(val & MVPP2_GMAC_STATUS0_AN_COMPLETE);
5788*4882a593Smuzhiyun 	state->link = !!(val & MVPP2_GMAC_STATUS0_LINK_UP);
5789*4882a593Smuzhiyun 	state->duplex = !!(val & MVPP2_GMAC_STATUS0_FULL_DUPLEX);
5790*4882a593Smuzhiyun 
5791*4882a593Smuzhiyun 	switch (port->phy_interface) {
5792*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_1000BASEX:
5793*4882a593Smuzhiyun 		state->speed = SPEED_1000;
5794*4882a593Smuzhiyun 		break;
5795*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_2500BASEX:
5796*4882a593Smuzhiyun 		state->speed = SPEED_2500;
5797*4882a593Smuzhiyun 		break;
5798*4882a593Smuzhiyun 	default:
5799*4882a593Smuzhiyun 		if (val & MVPP2_GMAC_STATUS0_GMII_SPEED)
5800*4882a593Smuzhiyun 			state->speed = SPEED_1000;
5801*4882a593Smuzhiyun 		else if (val & MVPP2_GMAC_STATUS0_MII_SPEED)
5802*4882a593Smuzhiyun 			state->speed = SPEED_100;
5803*4882a593Smuzhiyun 		else
5804*4882a593Smuzhiyun 			state->speed = SPEED_10;
5805*4882a593Smuzhiyun 	}
5806*4882a593Smuzhiyun 
5807*4882a593Smuzhiyun 	state->pause = 0;
5808*4882a593Smuzhiyun 	if (val & MVPP2_GMAC_STATUS0_RX_PAUSE)
5809*4882a593Smuzhiyun 		state->pause |= MLO_PAUSE_RX;
5810*4882a593Smuzhiyun 	if (val & MVPP2_GMAC_STATUS0_TX_PAUSE)
5811*4882a593Smuzhiyun 		state->pause |= MLO_PAUSE_TX;
5812*4882a593Smuzhiyun }
5813*4882a593Smuzhiyun 
mvpp2_gmac_pcs_config(struct phylink_pcs * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)5814*4882a593Smuzhiyun static int mvpp2_gmac_pcs_config(struct phylink_pcs *pcs, unsigned int mode,
5815*4882a593Smuzhiyun 				 phy_interface_t interface,
5816*4882a593Smuzhiyun 				 const unsigned long *advertising,
5817*4882a593Smuzhiyun 				 bool permit_pause_to_mac)
5818*4882a593Smuzhiyun {
5819*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5820*4882a593Smuzhiyun 	u32 mask, val, an, old_an, changed;
5821*4882a593Smuzhiyun 
5822*4882a593Smuzhiyun 	mask = MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
5823*4882a593Smuzhiyun 	       MVPP2_GMAC_IN_BAND_AUTONEG |
5824*4882a593Smuzhiyun 	       MVPP2_GMAC_AN_SPEED_EN |
5825*4882a593Smuzhiyun 	       MVPP2_GMAC_FLOW_CTRL_AUTONEG |
5826*4882a593Smuzhiyun 	       MVPP2_GMAC_AN_DUPLEX_EN;
5827*4882a593Smuzhiyun 
5828*4882a593Smuzhiyun 	if (phylink_autoneg_inband(mode)) {
5829*4882a593Smuzhiyun 		mask |= MVPP2_GMAC_CONFIG_MII_SPEED |
5830*4882a593Smuzhiyun 			MVPP2_GMAC_CONFIG_GMII_SPEED |
5831*4882a593Smuzhiyun 			MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5832*4882a593Smuzhiyun 		val = MVPP2_GMAC_IN_BAND_AUTONEG;
5833*4882a593Smuzhiyun 
5834*4882a593Smuzhiyun 		if (interface == PHY_INTERFACE_MODE_SGMII) {
5835*4882a593Smuzhiyun 			/* SGMII mode receives the speed and duplex from PHY */
5836*4882a593Smuzhiyun 			val |= MVPP2_GMAC_AN_SPEED_EN |
5837*4882a593Smuzhiyun 			       MVPP2_GMAC_AN_DUPLEX_EN;
5838*4882a593Smuzhiyun 		} else {
5839*4882a593Smuzhiyun 			/* 802.3z mode has fixed speed and duplex */
5840*4882a593Smuzhiyun 			val |= MVPP2_GMAC_CONFIG_GMII_SPEED |
5841*4882a593Smuzhiyun 			       MVPP2_GMAC_CONFIG_FULL_DUPLEX;
5842*4882a593Smuzhiyun 
5843*4882a593Smuzhiyun 			/* The FLOW_CTRL_AUTONEG bit selects either the hardware
5844*4882a593Smuzhiyun 			 * automatically or the bits in MVPP22_GMAC_CTRL_4_REG
5845*4882a593Smuzhiyun 			 * manually controls the GMAC pause modes.
5846*4882a593Smuzhiyun 			 */
5847*4882a593Smuzhiyun 			if (permit_pause_to_mac)
5848*4882a593Smuzhiyun 				val |= MVPP2_GMAC_FLOW_CTRL_AUTONEG;
5849*4882a593Smuzhiyun 
5850*4882a593Smuzhiyun 			/* Configure advertisement bits */
5851*4882a593Smuzhiyun 			mask |= MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN;
5852*4882a593Smuzhiyun 			if (phylink_test(advertising, Pause))
5853*4882a593Smuzhiyun 				val |= MVPP2_GMAC_FC_ADV_EN;
5854*4882a593Smuzhiyun 			if (phylink_test(advertising, Asym_Pause))
5855*4882a593Smuzhiyun 				val |= MVPP2_GMAC_FC_ADV_ASM_EN;
5856*4882a593Smuzhiyun 		}
5857*4882a593Smuzhiyun 	} else {
5858*4882a593Smuzhiyun 		val = 0;
5859*4882a593Smuzhiyun 	}
5860*4882a593Smuzhiyun 
5861*4882a593Smuzhiyun 	old_an = an = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5862*4882a593Smuzhiyun 	an = (an & ~mask) | val;
5863*4882a593Smuzhiyun 	changed = an ^ old_an;
5864*4882a593Smuzhiyun 	if (changed)
5865*4882a593Smuzhiyun 		writel(an, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5866*4882a593Smuzhiyun 
5867*4882a593Smuzhiyun 	/* We are only interested in the advertisement bits changing */
5868*4882a593Smuzhiyun 	return changed & (MVPP2_GMAC_FC_ADV_EN | MVPP2_GMAC_FC_ADV_ASM_EN);
5869*4882a593Smuzhiyun }
5870*4882a593Smuzhiyun 
mvpp2_gmac_pcs_an_restart(struct phylink_pcs * pcs)5871*4882a593Smuzhiyun static void mvpp2_gmac_pcs_an_restart(struct phylink_pcs *pcs)
5872*4882a593Smuzhiyun {
5873*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_pcs_to_port(pcs);
5874*4882a593Smuzhiyun 	u32 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5875*4882a593Smuzhiyun 
5876*4882a593Smuzhiyun 	writel(val | MVPP2_GMAC_IN_BAND_RESTART_AN,
5877*4882a593Smuzhiyun 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5878*4882a593Smuzhiyun 	writel(val & ~MVPP2_GMAC_IN_BAND_RESTART_AN,
5879*4882a593Smuzhiyun 	       port->base + MVPP2_GMAC_AUTONEG_CONFIG);
5880*4882a593Smuzhiyun }
5881*4882a593Smuzhiyun 
5882*4882a593Smuzhiyun static const struct phylink_pcs_ops mvpp2_phylink_gmac_pcs_ops = {
5883*4882a593Smuzhiyun 	.pcs_get_state = mvpp2_gmac_pcs_get_state,
5884*4882a593Smuzhiyun 	.pcs_config = mvpp2_gmac_pcs_config,
5885*4882a593Smuzhiyun 	.pcs_an_restart = mvpp2_gmac_pcs_an_restart,
5886*4882a593Smuzhiyun };
5887*4882a593Smuzhiyun 
mvpp2_phylink_validate(struct phylink_config * config,unsigned long * supported,struct phylink_link_state * state)5888*4882a593Smuzhiyun static void mvpp2_phylink_validate(struct phylink_config *config,
5889*4882a593Smuzhiyun 				   unsigned long *supported,
5890*4882a593Smuzhiyun 				   struct phylink_link_state *state)
5891*4882a593Smuzhiyun {
5892*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
5893*4882a593Smuzhiyun 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
5894*4882a593Smuzhiyun 
5895*4882a593Smuzhiyun 	/* Invalid combinations */
5896*4882a593Smuzhiyun 	switch (state->interface) {
5897*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_10GBASER:
5898*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_XAUI:
5899*4882a593Smuzhiyun 		if (!mvpp2_port_supports_xlg(port))
5900*4882a593Smuzhiyun 			goto empty_set;
5901*4882a593Smuzhiyun 		break;
5902*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII:
5903*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_ID:
5904*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_RXID:
5905*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_TXID:
5906*4882a593Smuzhiyun 		if (!mvpp2_port_supports_rgmii(port))
5907*4882a593Smuzhiyun 			goto empty_set;
5908*4882a593Smuzhiyun 		break;
5909*4882a593Smuzhiyun 	default:
5910*4882a593Smuzhiyun 		break;
5911*4882a593Smuzhiyun 	}
5912*4882a593Smuzhiyun 
5913*4882a593Smuzhiyun 	phylink_set(mask, Autoneg);
5914*4882a593Smuzhiyun 	phylink_set_port_modes(mask);
5915*4882a593Smuzhiyun 
5916*4882a593Smuzhiyun 	switch (state->interface) {
5917*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_10GBASER:
5918*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_XAUI:
5919*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_NA:
5920*4882a593Smuzhiyun 		if (mvpp2_port_supports_xlg(port)) {
5921*4882a593Smuzhiyun 			phylink_set(mask, 10000baseT_Full);
5922*4882a593Smuzhiyun 			phylink_set(mask, 10000baseCR_Full);
5923*4882a593Smuzhiyun 			phylink_set(mask, 10000baseSR_Full);
5924*4882a593Smuzhiyun 			phylink_set(mask, 10000baseLR_Full);
5925*4882a593Smuzhiyun 			phylink_set(mask, 10000baseLRM_Full);
5926*4882a593Smuzhiyun 			phylink_set(mask, 10000baseER_Full);
5927*4882a593Smuzhiyun 			phylink_set(mask, 10000baseKR_Full);
5928*4882a593Smuzhiyun 		}
5929*4882a593Smuzhiyun 		if (state->interface != PHY_INTERFACE_MODE_NA)
5930*4882a593Smuzhiyun 			break;
5931*4882a593Smuzhiyun 		fallthrough;
5932*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII:
5933*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_ID:
5934*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_RXID:
5935*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_TXID:
5936*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_SGMII:
5937*4882a593Smuzhiyun 		phylink_set(mask, 10baseT_Half);
5938*4882a593Smuzhiyun 		phylink_set(mask, 10baseT_Full);
5939*4882a593Smuzhiyun 		phylink_set(mask, 100baseT_Half);
5940*4882a593Smuzhiyun 		phylink_set(mask, 100baseT_Full);
5941*4882a593Smuzhiyun 		phylink_set(mask, 1000baseT_Full);
5942*4882a593Smuzhiyun 		phylink_set(mask, 1000baseX_Full);
5943*4882a593Smuzhiyun 		if (state->interface != PHY_INTERFACE_MODE_NA)
5944*4882a593Smuzhiyun 			break;
5945*4882a593Smuzhiyun 		fallthrough;
5946*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_1000BASEX:
5947*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_2500BASEX:
5948*4882a593Smuzhiyun 		if (port->comphy ||
5949*4882a593Smuzhiyun 		    state->interface != PHY_INTERFACE_MODE_2500BASEX) {
5950*4882a593Smuzhiyun 			phylink_set(mask, 1000baseT_Full);
5951*4882a593Smuzhiyun 			phylink_set(mask, 1000baseX_Full);
5952*4882a593Smuzhiyun 		}
5953*4882a593Smuzhiyun 		if (port->comphy ||
5954*4882a593Smuzhiyun 		    state->interface == PHY_INTERFACE_MODE_2500BASEX) {
5955*4882a593Smuzhiyun 			phylink_set(mask, 2500baseT_Full);
5956*4882a593Smuzhiyun 			phylink_set(mask, 2500baseX_Full);
5957*4882a593Smuzhiyun 		}
5958*4882a593Smuzhiyun 		break;
5959*4882a593Smuzhiyun 	default:
5960*4882a593Smuzhiyun 		goto empty_set;
5961*4882a593Smuzhiyun 	}
5962*4882a593Smuzhiyun 
5963*4882a593Smuzhiyun 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
5964*4882a593Smuzhiyun 	bitmap_and(state->advertising, state->advertising, mask,
5965*4882a593Smuzhiyun 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
5966*4882a593Smuzhiyun 
5967*4882a593Smuzhiyun 	phylink_helper_basex_speed(state);
5968*4882a593Smuzhiyun 	return;
5969*4882a593Smuzhiyun 
5970*4882a593Smuzhiyun empty_set:
5971*4882a593Smuzhiyun 	bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
5972*4882a593Smuzhiyun }
5973*4882a593Smuzhiyun 
mvpp2_xlg_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)5974*4882a593Smuzhiyun static void mvpp2_xlg_config(struct mvpp2_port *port, unsigned int mode,
5975*4882a593Smuzhiyun 			     const struct phylink_link_state *state)
5976*4882a593Smuzhiyun {
5977*4882a593Smuzhiyun 	u32 val;
5978*4882a593Smuzhiyun 
5979*4882a593Smuzhiyun 	mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
5980*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL0_MAC_RESET_DIS,
5981*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL0_MAC_RESET_DIS);
5982*4882a593Smuzhiyun 	mvpp2_modify(port->base + MVPP22_XLG_CTRL4_REG,
5983*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL4_MACMODSELECT_GMAC |
5984*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL4_EN_IDLE_CHECK |
5985*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC,
5986*4882a593Smuzhiyun 		     MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC);
5987*4882a593Smuzhiyun 
5988*4882a593Smuzhiyun 	/* Wait for reset to deassert */
5989*4882a593Smuzhiyun 	do {
5990*4882a593Smuzhiyun 		val = readl(port->base + MVPP22_XLG_CTRL0_REG);
5991*4882a593Smuzhiyun 	} while (!(val & MVPP22_XLG_CTRL0_MAC_RESET_DIS));
5992*4882a593Smuzhiyun }
5993*4882a593Smuzhiyun 
mvpp2_gmac_config(struct mvpp2_port * port,unsigned int mode,const struct phylink_link_state * state)5994*4882a593Smuzhiyun static void mvpp2_gmac_config(struct mvpp2_port *port, unsigned int mode,
5995*4882a593Smuzhiyun 			      const struct phylink_link_state *state)
5996*4882a593Smuzhiyun {
5997*4882a593Smuzhiyun 	u32 old_ctrl0, ctrl0;
5998*4882a593Smuzhiyun 	u32 old_ctrl2, ctrl2;
5999*4882a593Smuzhiyun 	u32 old_ctrl4, ctrl4;
6000*4882a593Smuzhiyun 
6001*4882a593Smuzhiyun 	old_ctrl0 = ctrl0 = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
6002*4882a593Smuzhiyun 	old_ctrl2 = ctrl2 = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
6003*4882a593Smuzhiyun 	old_ctrl4 = ctrl4 = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
6004*4882a593Smuzhiyun 
6005*4882a593Smuzhiyun 	ctrl0 &= ~MVPP2_GMAC_PORT_TYPE_MASK;
6006*4882a593Smuzhiyun 	ctrl2 &= ~(MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK);
6007*4882a593Smuzhiyun 
6008*4882a593Smuzhiyun 	/* Configure port type */
6009*4882a593Smuzhiyun 	if (phy_interface_mode_is_8023z(state->interface)) {
6010*4882a593Smuzhiyun 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK;
6011*4882a593Smuzhiyun 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6012*4882a593Smuzhiyun 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6013*4882a593Smuzhiyun 			 MVPP22_CTRL4_DP_CLK_SEL |
6014*4882a593Smuzhiyun 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6015*4882a593Smuzhiyun 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6016*4882a593Smuzhiyun 		ctrl2 |= MVPP2_GMAC_PCS_ENABLE_MASK | MVPP2_GMAC_INBAND_AN_MASK;
6017*4882a593Smuzhiyun 		ctrl4 &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
6018*4882a593Smuzhiyun 		ctrl4 |= MVPP22_CTRL4_SYNC_BYPASS_DIS |
6019*4882a593Smuzhiyun 			 MVPP22_CTRL4_DP_CLK_SEL |
6020*4882a593Smuzhiyun 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6021*4882a593Smuzhiyun 	} else if (phy_interface_mode_is_rgmii(state->interface)) {
6022*4882a593Smuzhiyun 		ctrl4 &= ~MVPP22_CTRL4_DP_CLK_SEL;
6023*4882a593Smuzhiyun 		ctrl4 |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
6024*4882a593Smuzhiyun 			 MVPP22_CTRL4_SYNC_BYPASS_DIS |
6025*4882a593Smuzhiyun 			 MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
6026*4882a593Smuzhiyun 	}
6027*4882a593Smuzhiyun 
6028*4882a593Smuzhiyun 	/* Configure negotiation style */
6029*4882a593Smuzhiyun 	if (!phylink_autoneg_inband(mode)) {
6030*4882a593Smuzhiyun 		/* Phy or fixed speed - no in-band AN, nothing to do, leave the
6031*4882a593Smuzhiyun 		 * configured speed, duplex and flow control as-is.
6032*4882a593Smuzhiyun 		 */
6033*4882a593Smuzhiyun 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
6034*4882a593Smuzhiyun 		/* SGMII in-band mode receives the speed and duplex from
6035*4882a593Smuzhiyun 		 * the PHY. Flow control information is not received. */
6036*4882a593Smuzhiyun 	} else if (phy_interface_mode_is_8023z(state->interface)) {
6037*4882a593Smuzhiyun 		/* 1000BaseX and 2500BaseX ports cannot negotiate speed nor can
6038*4882a593Smuzhiyun 		 * they negotiate duplex: they are always operating with a fixed
6039*4882a593Smuzhiyun 		 * speed of 1000/2500Mbps in full duplex, so force 1000/2500
6040*4882a593Smuzhiyun 		 * speed and full duplex here.
6041*4882a593Smuzhiyun 		 */
6042*4882a593Smuzhiyun 		ctrl0 |= MVPP2_GMAC_PORT_TYPE_MASK;
6043*4882a593Smuzhiyun 	}
6044*4882a593Smuzhiyun 
6045*4882a593Smuzhiyun 	if (old_ctrl0 != ctrl0)
6046*4882a593Smuzhiyun 		writel(ctrl0, port->base + MVPP2_GMAC_CTRL_0_REG);
6047*4882a593Smuzhiyun 	if (old_ctrl2 != ctrl2)
6048*4882a593Smuzhiyun 		writel(ctrl2, port->base + MVPP2_GMAC_CTRL_2_REG);
6049*4882a593Smuzhiyun 	if (old_ctrl4 != ctrl4)
6050*4882a593Smuzhiyun 		writel(ctrl4, port->base + MVPP22_GMAC_CTRL_4_REG);
6051*4882a593Smuzhiyun }
6052*4882a593Smuzhiyun 
mvpp2__mac_prepare(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6053*4882a593Smuzhiyun static int mvpp2__mac_prepare(struct phylink_config *config, unsigned int mode,
6054*4882a593Smuzhiyun 			      phy_interface_t interface)
6055*4882a593Smuzhiyun {
6056*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6057*4882a593Smuzhiyun 
6058*4882a593Smuzhiyun 	/* Check for invalid configuration */
6059*4882a593Smuzhiyun 	if (mvpp2_is_xlg(interface) && port->gop_id != 0) {
6060*4882a593Smuzhiyun 		netdev_err(port->dev, "Invalid mode on %s\n", port->dev->name);
6061*4882a593Smuzhiyun 		return -EINVAL;
6062*4882a593Smuzhiyun 	}
6063*4882a593Smuzhiyun 
6064*4882a593Smuzhiyun 	if (port->phy_interface != interface ||
6065*4882a593Smuzhiyun 	    phylink_autoneg_inband(mode)) {
6066*4882a593Smuzhiyun 		/* Force the link down when changing the interface or if in
6067*4882a593Smuzhiyun 		 * in-band mode to ensure we do not change the configuration
6068*4882a593Smuzhiyun 		 * while the hardware is indicating link is up. We force both
6069*4882a593Smuzhiyun 		 * XLG and GMAC down to ensure that they're both in a known
6070*4882a593Smuzhiyun 		 * state.
6071*4882a593Smuzhiyun 		 */
6072*4882a593Smuzhiyun 		mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6073*4882a593Smuzhiyun 			     MVPP2_GMAC_FORCE_LINK_PASS |
6074*4882a593Smuzhiyun 			     MVPP2_GMAC_FORCE_LINK_DOWN,
6075*4882a593Smuzhiyun 			     MVPP2_GMAC_FORCE_LINK_DOWN);
6076*4882a593Smuzhiyun 
6077*4882a593Smuzhiyun 		if (mvpp2_port_supports_xlg(port))
6078*4882a593Smuzhiyun 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6079*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6080*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN,
6081*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN);
6082*4882a593Smuzhiyun 	}
6083*4882a593Smuzhiyun 
6084*4882a593Smuzhiyun 	/* Make sure the port is disabled when reconfiguring the mode */
6085*4882a593Smuzhiyun 	mvpp2_port_disable(port);
6086*4882a593Smuzhiyun 
6087*4882a593Smuzhiyun 	if (port->phy_interface != interface) {
6088*4882a593Smuzhiyun 		/* Place GMAC into reset */
6089*4882a593Smuzhiyun 		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6090*4882a593Smuzhiyun 			     MVPP2_GMAC_PORT_RESET_MASK,
6091*4882a593Smuzhiyun 			     MVPP2_GMAC_PORT_RESET_MASK);
6092*4882a593Smuzhiyun 
6093*4882a593Smuzhiyun 		if (port->priv->hw_version == MVPP22) {
6094*4882a593Smuzhiyun 			mvpp22_gop_mask_irq(port);
6095*4882a593Smuzhiyun 
6096*4882a593Smuzhiyun 			phy_power_off(port->comphy);
6097*4882a593Smuzhiyun 		}
6098*4882a593Smuzhiyun 	}
6099*4882a593Smuzhiyun 
6100*4882a593Smuzhiyun 	/* Select the appropriate PCS operations depending on the
6101*4882a593Smuzhiyun 	 * configured interface mode. We will only switch to a mode
6102*4882a593Smuzhiyun 	 * that the validate() checks have already passed.
6103*4882a593Smuzhiyun 	 */
6104*4882a593Smuzhiyun 	if (mvpp2_is_xlg(interface))
6105*4882a593Smuzhiyun 		port->phylink_pcs.ops = &mvpp2_phylink_xlg_pcs_ops;
6106*4882a593Smuzhiyun 	else
6107*4882a593Smuzhiyun 		port->phylink_pcs.ops = &mvpp2_phylink_gmac_pcs_ops;
6108*4882a593Smuzhiyun 
6109*4882a593Smuzhiyun 	return 0;
6110*4882a593Smuzhiyun }
6111*4882a593Smuzhiyun 
mvpp2_mac_prepare(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6112*4882a593Smuzhiyun static int mvpp2_mac_prepare(struct phylink_config *config, unsigned int mode,
6113*4882a593Smuzhiyun 			     phy_interface_t interface)
6114*4882a593Smuzhiyun {
6115*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6116*4882a593Smuzhiyun 	int ret;
6117*4882a593Smuzhiyun 
6118*4882a593Smuzhiyun 	ret = mvpp2__mac_prepare(config, mode, interface);
6119*4882a593Smuzhiyun 	if (ret == 0)
6120*4882a593Smuzhiyun 		phylink_set_pcs(port->phylink, &port->phylink_pcs);
6121*4882a593Smuzhiyun 
6122*4882a593Smuzhiyun 	return ret;
6123*4882a593Smuzhiyun }
6124*4882a593Smuzhiyun 
mvpp2_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)6125*4882a593Smuzhiyun static void mvpp2_mac_config(struct phylink_config *config, unsigned int mode,
6126*4882a593Smuzhiyun 			     const struct phylink_link_state *state)
6127*4882a593Smuzhiyun {
6128*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6129*4882a593Smuzhiyun 
6130*4882a593Smuzhiyun 	/* mac (re)configuration */
6131*4882a593Smuzhiyun 	if (mvpp2_is_xlg(state->interface))
6132*4882a593Smuzhiyun 		mvpp2_xlg_config(port, mode, state);
6133*4882a593Smuzhiyun 	else if (phy_interface_mode_is_rgmii(state->interface) ||
6134*4882a593Smuzhiyun 		 phy_interface_mode_is_8023z(state->interface) ||
6135*4882a593Smuzhiyun 		 state->interface == PHY_INTERFACE_MODE_SGMII)
6136*4882a593Smuzhiyun 		mvpp2_gmac_config(port, mode, state);
6137*4882a593Smuzhiyun 
6138*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP21 && port->flags & MVPP2_F_LOOPBACK)
6139*4882a593Smuzhiyun 		mvpp2_port_loopback_set(port, state);
6140*4882a593Smuzhiyun }
6141*4882a593Smuzhiyun 
mvpp2_mac_finish(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6142*4882a593Smuzhiyun static int mvpp2_mac_finish(struct phylink_config *config, unsigned int mode,
6143*4882a593Smuzhiyun 			    phy_interface_t interface)
6144*4882a593Smuzhiyun {
6145*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6146*4882a593Smuzhiyun 
6147*4882a593Smuzhiyun 	if (port->priv->hw_version == MVPP22 &&
6148*4882a593Smuzhiyun 	    port->phy_interface != interface) {
6149*4882a593Smuzhiyun 		port->phy_interface = interface;
6150*4882a593Smuzhiyun 
6151*4882a593Smuzhiyun 		/* Reconfigure the serdes lanes */
6152*4882a593Smuzhiyun 		mvpp22_mode_reconfigure(port);
6153*4882a593Smuzhiyun 
6154*4882a593Smuzhiyun 		/* Unmask interrupts */
6155*4882a593Smuzhiyun 		mvpp22_gop_unmask_irq(port);
6156*4882a593Smuzhiyun 	}
6157*4882a593Smuzhiyun 
6158*4882a593Smuzhiyun 	if (!mvpp2_is_xlg(interface)) {
6159*4882a593Smuzhiyun 		/* Release GMAC reset and wait */
6160*4882a593Smuzhiyun 		mvpp2_modify(port->base + MVPP2_GMAC_CTRL_2_REG,
6161*4882a593Smuzhiyun 			     MVPP2_GMAC_PORT_RESET_MASK, 0);
6162*4882a593Smuzhiyun 
6163*4882a593Smuzhiyun 		while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
6164*4882a593Smuzhiyun 		       MVPP2_GMAC_PORT_RESET_MASK)
6165*4882a593Smuzhiyun 			continue;
6166*4882a593Smuzhiyun 	}
6167*4882a593Smuzhiyun 
6168*4882a593Smuzhiyun 	mvpp2_port_enable(port);
6169*4882a593Smuzhiyun 
6170*4882a593Smuzhiyun 	/* Allow the link to come up if in in-band mode, otherwise the
6171*4882a593Smuzhiyun 	 * link is forced via mac_link_down()/mac_link_up()
6172*4882a593Smuzhiyun 	 */
6173*4882a593Smuzhiyun 	if (phylink_autoneg_inband(mode)) {
6174*4882a593Smuzhiyun 		if (mvpp2_is_xlg(interface))
6175*4882a593Smuzhiyun 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6176*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6177*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN, 0);
6178*4882a593Smuzhiyun 		else
6179*4882a593Smuzhiyun 			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6180*4882a593Smuzhiyun 				     MVPP2_GMAC_FORCE_LINK_PASS |
6181*4882a593Smuzhiyun 				     MVPP2_GMAC_FORCE_LINK_DOWN, 0);
6182*4882a593Smuzhiyun 	}
6183*4882a593Smuzhiyun 
6184*4882a593Smuzhiyun 	return 0;
6185*4882a593Smuzhiyun }
6186*4882a593Smuzhiyun 
mvpp2_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)6187*4882a593Smuzhiyun static void mvpp2_mac_link_up(struct phylink_config *config,
6188*4882a593Smuzhiyun 			      struct phy_device *phy,
6189*4882a593Smuzhiyun 			      unsigned int mode, phy_interface_t interface,
6190*4882a593Smuzhiyun 			      int speed, int duplex,
6191*4882a593Smuzhiyun 			      bool tx_pause, bool rx_pause)
6192*4882a593Smuzhiyun {
6193*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6194*4882a593Smuzhiyun 	u32 val;
6195*4882a593Smuzhiyun 
6196*4882a593Smuzhiyun 	if (mvpp2_is_xlg(interface)) {
6197*4882a593Smuzhiyun 		if (!phylink_autoneg_inband(mode)) {
6198*4882a593Smuzhiyun 			val = MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6199*4882a593Smuzhiyun 			if (tx_pause)
6200*4882a593Smuzhiyun 				val |= MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN;
6201*4882a593Smuzhiyun 			if (rx_pause)
6202*4882a593Smuzhiyun 				val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
6203*4882a593Smuzhiyun 
6204*4882a593Smuzhiyun 			mvpp2_modify(port->base + MVPP22_XLG_CTRL0_REG,
6205*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_DOWN |
6206*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_FORCE_LINK_PASS |
6207*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_TX_FLOW_CTRL_EN |
6208*4882a593Smuzhiyun 				     MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN, val);
6209*4882a593Smuzhiyun 		}
6210*4882a593Smuzhiyun 	} else {
6211*4882a593Smuzhiyun 		if (!phylink_autoneg_inband(mode)) {
6212*4882a593Smuzhiyun 			val = MVPP2_GMAC_FORCE_LINK_PASS;
6213*4882a593Smuzhiyun 
6214*4882a593Smuzhiyun 			if (speed == SPEED_1000 || speed == SPEED_2500)
6215*4882a593Smuzhiyun 				val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6216*4882a593Smuzhiyun 			else if (speed == SPEED_100)
6217*4882a593Smuzhiyun 				val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6218*4882a593Smuzhiyun 
6219*4882a593Smuzhiyun 			if (duplex == DUPLEX_FULL)
6220*4882a593Smuzhiyun 				val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6221*4882a593Smuzhiyun 
6222*4882a593Smuzhiyun 			mvpp2_modify(port->base + MVPP2_GMAC_AUTONEG_CONFIG,
6223*4882a593Smuzhiyun 				     MVPP2_GMAC_FORCE_LINK_DOWN |
6224*4882a593Smuzhiyun 				     MVPP2_GMAC_FORCE_LINK_PASS |
6225*4882a593Smuzhiyun 				     MVPP2_GMAC_CONFIG_MII_SPEED |
6226*4882a593Smuzhiyun 				     MVPP2_GMAC_CONFIG_GMII_SPEED |
6227*4882a593Smuzhiyun 				     MVPP2_GMAC_CONFIG_FULL_DUPLEX, val);
6228*4882a593Smuzhiyun 		}
6229*4882a593Smuzhiyun 
6230*4882a593Smuzhiyun 		/* We can always update the flow control enable bits;
6231*4882a593Smuzhiyun 		 * these will only be effective if flow control AN
6232*4882a593Smuzhiyun 		 * (MVPP2_GMAC_FLOW_CTRL_AUTONEG) is disabled.
6233*4882a593Smuzhiyun 		 */
6234*4882a593Smuzhiyun 		val = 0;
6235*4882a593Smuzhiyun 		if (tx_pause)
6236*4882a593Smuzhiyun 			val |= MVPP22_CTRL4_TX_FC_EN;
6237*4882a593Smuzhiyun 		if (rx_pause)
6238*4882a593Smuzhiyun 			val |= MVPP22_CTRL4_RX_FC_EN;
6239*4882a593Smuzhiyun 
6240*4882a593Smuzhiyun 		mvpp2_modify(port->base + MVPP22_GMAC_CTRL_4_REG,
6241*4882a593Smuzhiyun 			     MVPP22_CTRL4_RX_FC_EN | MVPP22_CTRL4_TX_FC_EN,
6242*4882a593Smuzhiyun 			     val);
6243*4882a593Smuzhiyun 	}
6244*4882a593Smuzhiyun 
6245*4882a593Smuzhiyun 	mvpp2_port_enable(port);
6246*4882a593Smuzhiyun 
6247*4882a593Smuzhiyun 	mvpp2_egress_enable(port);
6248*4882a593Smuzhiyun 	mvpp2_ingress_enable(port);
6249*4882a593Smuzhiyun 	netif_tx_wake_all_queues(port->dev);
6250*4882a593Smuzhiyun }
6251*4882a593Smuzhiyun 
mvpp2_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)6252*4882a593Smuzhiyun static void mvpp2_mac_link_down(struct phylink_config *config,
6253*4882a593Smuzhiyun 				unsigned int mode, phy_interface_t interface)
6254*4882a593Smuzhiyun {
6255*4882a593Smuzhiyun 	struct mvpp2_port *port = mvpp2_phylink_to_port(config);
6256*4882a593Smuzhiyun 	u32 val;
6257*4882a593Smuzhiyun 
6258*4882a593Smuzhiyun 	if (!phylink_autoneg_inband(mode)) {
6259*4882a593Smuzhiyun 		if (mvpp2_is_xlg(interface)) {
6260*4882a593Smuzhiyun 			val = readl(port->base + MVPP22_XLG_CTRL0_REG);
6261*4882a593Smuzhiyun 			val &= ~MVPP22_XLG_CTRL0_FORCE_LINK_PASS;
6262*4882a593Smuzhiyun 			val |= MVPP22_XLG_CTRL0_FORCE_LINK_DOWN;
6263*4882a593Smuzhiyun 			writel(val, port->base + MVPP22_XLG_CTRL0_REG);
6264*4882a593Smuzhiyun 		} else {
6265*4882a593Smuzhiyun 			val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6266*4882a593Smuzhiyun 			val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
6267*4882a593Smuzhiyun 			val |= MVPP2_GMAC_FORCE_LINK_DOWN;
6268*4882a593Smuzhiyun 			writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6269*4882a593Smuzhiyun 		}
6270*4882a593Smuzhiyun 	}
6271*4882a593Smuzhiyun 
6272*4882a593Smuzhiyun 	netif_tx_stop_all_queues(port->dev);
6273*4882a593Smuzhiyun 	mvpp2_egress_disable(port);
6274*4882a593Smuzhiyun 	mvpp2_ingress_disable(port);
6275*4882a593Smuzhiyun 
6276*4882a593Smuzhiyun 	mvpp2_port_disable(port);
6277*4882a593Smuzhiyun }
6278*4882a593Smuzhiyun 
6279*4882a593Smuzhiyun static const struct phylink_mac_ops mvpp2_phylink_ops = {
6280*4882a593Smuzhiyun 	.validate = mvpp2_phylink_validate,
6281*4882a593Smuzhiyun 	.mac_prepare = mvpp2_mac_prepare,
6282*4882a593Smuzhiyun 	.mac_config = mvpp2_mac_config,
6283*4882a593Smuzhiyun 	.mac_finish = mvpp2_mac_finish,
6284*4882a593Smuzhiyun 	.mac_link_up = mvpp2_mac_link_up,
6285*4882a593Smuzhiyun 	.mac_link_down = mvpp2_mac_link_down,
6286*4882a593Smuzhiyun };
6287*4882a593Smuzhiyun 
6288*4882a593Smuzhiyun /* Work-around for ACPI */
mvpp2_acpi_start(struct mvpp2_port * port)6289*4882a593Smuzhiyun static void mvpp2_acpi_start(struct mvpp2_port *port)
6290*4882a593Smuzhiyun {
6291*4882a593Smuzhiyun 	/* Phylink isn't used as of now for ACPI, so the MAC has to be
6292*4882a593Smuzhiyun 	 * configured manually when the interface is started. This will
6293*4882a593Smuzhiyun 	 * be removed as soon as the phylink ACPI support lands in.
6294*4882a593Smuzhiyun 	 */
6295*4882a593Smuzhiyun 	struct phylink_link_state state = {
6296*4882a593Smuzhiyun 		.interface = port->phy_interface,
6297*4882a593Smuzhiyun 	};
6298*4882a593Smuzhiyun 	mvpp2__mac_prepare(&port->phylink_config, MLO_AN_INBAND,
6299*4882a593Smuzhiyun 			   port->phy_interface);
6300*4882a593Smuzhiyun 	mvpp2_mac_config(&port->phylink_config, MLO_AN_INBAND, &state);
6301*4882a593Smuzhiyun 	port->phylink_pcs.ops->pcs_config(&port->phylink_pcs, MLO_AN_INBAND,
6302*4882a593Smuzhiyun 					  port->phy_interface,
6303*4882a593Smuzhiyun 					  state.advertising, false);
6304*4882a593Smuzhiyun 	mvpp2_mac_finish(&port->phylink_config, MLO_AN_INBAND,
6305*4882a593Smuzhiyun 			 port->phy_interface);
6306*4882a593Smuzhiyun 	mvpp2_mac_link_up(&port->phylink_config, NULL,
6307*4882a593Smuzhiyun 			  MLO_AN_INBAND, port->phy_interface,
6308*4882a593Smuzhiyun 			  SPEED_UNKNOWN, DUPLEX_UNKNOWN, false, false);
6309*4882a593Smuzhiyun }
6310*4882a593Smuzhiyun 
6311*4882a593Smuzhiyun /* Ports initialization */
mvpp2_port_probe(struct platform_device * pdev,struct fwnode_handle * port_fwnode,struct mvpp2 * priv)6312*4882a593Smuzhiyun static int mvpp2_port_probe(struct platform_device *pdev,
6313*4882a593Smuzhiyun 			    struct fwnode_handle *port_fwnode,
6314*4882a593Smuzhiyun 			    struct mvpp2 *priv)
6315*4882a593Smuzhiyun {
6316*4882a593Smuzhiyun 	struct phy *comphy = NULL;
6317*4882a593Smuzhiyun 	struct mvpp2_port *port;
6318*4882a593Smuzhiyun 	struct mvpp2_port_pcpu *port_pcpu;
6319*4882a593Smuzhiyun 	struct device_node *port_node = to_of_node(port_fwnode);
6320*4882a593Smuzhiyun 	netdev_features_t features;
6321*4882a593Smuzhiyun 	struct net_device *dev;
6322*4882a593Smuzhiyun 	struct phylink *phylink;
6323*4882a593Smuzhiyun 	char *mac_from = "";
6324*4882a593Smuzhiyun 	unsigned int ntxqs, nrxqs, thread;
6325*4882a593Smuzhiyun 	unsigned long flags = 0;
6326*4882a593Smuzhiyun 	bool has_tx_irqs;
6327*4882a593Smuzhiyun 	u32 id;
6328*4882a593Smuzhiyun 	int phy_mode;
6329*4882a593Smuzhiyun 	int err, i;
6330*4882a593Smuzhiyun 
6331*4882a593Smuzhiyun 	has_tx_irqs = mvpp2_port_has_irqs(priv, port_node, &flags);
6332*4882a593Smuzhiyun 	if (!has_tx_irqs && queue_mode == MVPP2_QDIST_MULTI_MODE) {
6333*4882a593Smuzhiyun 		dev_err(&pdev->dev,
6334*4882a593Smuzhiyun 			"not enough IRQs to support multi queue mode\n");
6335*4882a593Smuzhiyun 		return -EINVAL;
6336*4882a593Smuzhiyun 	}
6337*4882a593Smuzhiyun 
6338*4882a593Smuzhiyun 	ntxqs = MVPP2_MAX_TXQ;
6339*4882a593Smuzhiyun 	nrxqs = mvpp2_get_nrxqs(priv);
6340*4882a593Smuzhiyun 
6341*4882a593Smuzhiyun 	dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
6342*4882a593Smuzhiyun 	if (!dev)
6343*4882a593Smuzhiyun 		return -ENOMEM;
6344*4882a593Smuzhiyun 
6345*4882a593Smuzhiyun 	phy_mode = fwnode_get_phy_mode(port_fwnode);
6346*4882a593Smuzhiyun 	if (phy_mode < 0) {
6347*4882a593Smuzhiyun 		dev_err(&pdev->dev, "incorrect phy mode\n");
6348*4882a593Smuzhiyun 		err = phy_mode;
6349*4882a593Smuzhiyun 		goto err_free_netdev;
6350*4882a593Smuzhiyun 	}
6351*4882a593Smuzhiyun 
6352*4882a593Smuzhiyun 	/*
6353*4882a593Smuzhiyun 	 * Rewrite 10GBASE-KR to 10GBASE-R for compatibility with existing DT.
6354*4882a593Smuzhiyun 	 * Existing usage of 10GBASE-KR is not correct; no backplane
6355*4882a593Smuzhiyun 	 * negotiation is done, and this driver does not actually support
6356*4882a593Smuzhiyun 	 * 10GBASE-KR.
6357*4882a593Smuzhiyun 	 */
6358*4882a593Smuzhiyun 	if (phy_mode == PHY_INTERFACE_MODE_10GKR)
6359*4882a593Smuzhiyun 		phy_mode = PHY_INTERFACE_MODE_10GBASER;
6360*4882a593Smuzhiyun 
6361*4882a593Smuzhiyun 	if (port_node) {
6362*4882a593Smuzhiyun 		comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
6363*4882a593Smuzhiyun 		if (IS_ERR(comphy)) {
6364*4882a593Smuzhiyun 			if (PTR_ERR(comphy) == -EPROBE_DEFER) {
6365*4882a593Smuzhiyun 				err = -EPROBE_DEFER;
6366*4882a593Smuzhiyun 				goto err_free_netdev;
6367*4882a593Smuzhiyun 			}
6368*4882a593Smuzhiyun 			comphy = NULL;
6369*4882a593Smuzhiyun 		}
6370*4882a593Smuzhiyun 	}
6371*4882a593Smuzhiyun 
6372*4882a593Smuzhiyun 	if (fwnode_property_read_u32(port_fwnode, "port-id", &id)) {
6373*4882a593Smuzhiyun 		err = -EINVAL;
6374*4882a593Smuzhiyun 		dev_err(&pdev->dev, "missing port-id value\n");
6375*4882a593Smuzhiyun 		goto err_free_netdev;
6376*4882a593Smuzhiyun 	}
6377*4882a593Smuzhiyun 
6378*4882a593Smuzhiyun 	dev->tx_queue_len = MVPP2_MAX_TXD_MAX;
6379*4882a593Smuzhiyun 	dev->watchdog_timeo = 5 * HZ;
6380*4882a593Smuzhiyun 	dev->netdev_ops = &mvpp2_netdev_ops;
6381*4882a593Smuzhiyun 	dev->ethtool_ops = &mvpp2_eth_tool_ops;
6382*4882a593Smuzhiyun 
6383*4882a593Smuzhiyun 	port = netdev_priv(dev);
6384*4882a593Smuzhiyun 	port->dev = dev;
6385*4882a593Smuzhiyun 	port->fwnode = port_fwnode;
6386*4882a593Smuzhiyun 	port->has_phy = !!of_find_property(port_node, "phy", NULL);
6387*4882a593Smuzhiyun 	port->ntxqs = ntxqs;
6388*4882a593Smuzhiyun 	port->nrxqs = nrxqs;
6389*4882a593Smuzhiyun 	port->priv = priv;
6390*4882a593Smuzhiyun 	port->has_tx_irqs = has_tx_irqs;
6391*4882a593Smuzhiyun 	port->flags = flags;
6392*4882a593Smuzhiyun 
6393*4882a593Smuzhiyun 	err = mvpp2_queue_vectors_init(port, port_node);
6394*4882a593Smuzhiyun 	if (err)
6395*4882a593Smuzhiyun 		goto err_free_netdev;
6396*4882a593Smuzhiyun 
6397*4882a593Smuzhiyun 	if (port_node)
6398*4882a593Smuzhiyun 		port->port_irq = of_irq_get_byname(port_node, "link");
6399*4882a593Smuzhiyun 	else
6400*4882a593Smuzhiyun 		port->port_irq = fwnode_irq_get(port_fwnode, port->nqvecs + 1);
6401*4882a593Smuzhiyun 	if (port->port_irq == -EPROBE_DEFER) {
6402*4882a593Smuzhiyun 		err = -EPROBE_DEFER;
6403*4882a593Smuzhiyun 		goto err_deinit_qvecs;
6404*4882a593Smuzhiyun 	}
6405*4882a593Smuzhiyun 	if (port->port_irq <= 0)
6406*4882a593Smuzhiyun 		/* the link irq is optional */
6407*4882a593Smuzhiyun 		port->port_irq = 0;
6408*4882a593Smuzhiyun 
6409*4882a593Smuzhiyun 	if (fwnode_property_read_bool(port_fwnode, "marvell,loopback"))
6410*4882a593Smuzhiyun 		port->flags |= MVPP2_F_LOOPBACK;
6411*4882a593Smuzhiyun 
6412*4882a593Smuzhiyun 	port->id = id;
6413*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
6414*4882a593Smuzhiyun 		port->first_rxq = port->id * port->nrxqs;
6415*4882a593Smuzhiyun 	else
6416*4882a593Smuzhiyun 		port->first_rxq = port->id * priv->max_port_rxqs;
6417*4882a593Smuzhiyun 
6418*4882a593Smuzhiyun 	port->of_node = port_node;
6419*4882a593Smuzhiyun 	port->phy_interface = phy_mode;
6420*4882a593Smuzhiyun 	port->comphy = comphy;
6421*4882a593Smuzhiyun 
6422*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
6423*4882a593Smuzhiyun 		port->base = devm_platform_ioremap_resource(pdev, 2 + id);
6424*4882a593Smuzhiyun 		if (IS_ERR(port->base)) {
6425*4882a593Smuzhiyun 			err = PTR_ERR(port->base);
6426*4882a593Smuzhiyun 			goto err_free_irq;
6427*4882a593Smuzhiyun 		}
6428*4882a593Smuzhiyun 
6429*4882a593Smuzhiyun 		port->stats_base = port->priv->lms_base +
6430*4882a593Smuzhiyun 				   MVPP21_MIB_COUNTERS_OFFSET +
6431*4882a593Smuzhiyun 				   port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
6432*4882a593Smuzhiyun 	} else {
6433*4882a593Smuzhiyun 		if (fwnode_property_read_u32(port_fwnode, "gop-port-id",
6434*4882a593Smuzhiyun 					     &port->gop_id)) {
6435*4882a593Smuzhiyun 			err = -EINVAL;
6436*4882a593Smuzhiyun 			dev_err(&pdev->dev, "missing gop-port-id value\n");
6437*4882a593Smuzhiyun 			goto err_deinit_qvecs;
6438*4882a593Smuzhiyun 		}
6439*4882a593Smuzhiyun 
6440*4882a593Smuzhiyun 		port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
6441*4882a593Smuzhiyun 		port->stats_base = port->priv->iface_base +
6442*4882a593Smuzhiyun 				   MVPP22_MIB_COUNTERS_OFFSET +
6443*4882a593Smuzhiyun 				   port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
6444*4882a593Smuzhiyun 
6445*4882a593Smuzhiyun 		/* We may want a property to describe whether we should use
6446*4882a593Smuzhiyun 		 * MAC hardware timestamping.
6447*4882a593Smuzhiyun 		 */
6448*4882a593Smuzhiyun 		if (priv->tai)
6449*4882a593Smuzhiyun 			port->hwtstamp = true;
6450*4882a593Smuzhiyun 	}
6451*4882a593Smuzhiyun 
6452*4882a593Smuzhiyun 	/* Alloc per-cpu and ethtool stats */
6453*4882a593Smuzhiyun 	port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
6454*4882a593Smuzhiyun 	if (!port->stats) {
6455*4882a593Smuzhiyun 		err = -ENOMEM;
6456*4882a593Smuzhiyun 		goto err_free_irq;
6457*4882a593Smuzhiyun 	}
6458*4882a593Smuzhiyun 
6459*4882a593Smuzhiyun 	port->ethtool_stats = devm_kcalloc(&pdev->dev,
6460*4882a593Smuzhiyun 					   MVPP2_N_ETHTOOL_STATS(ntxqs, nrxqs),
6461*4882a593Smuzhiyun 					   sizeof(u64), GFP_KERNEL);
6462*4882a593Smuzhiyun 	if (!port->ethtool_stats) {
6463*4882a593Smuzhiyun 		err = -ENOMEM;
6464*4882a593Smuzhiyun 		goto err_free_stats;
6465*4882a593Smuzhiyun 	}
6466*4882a593Smuzhiyun 
6467*4882a593Smuzhiyun 	mutex_init(&port->gather_stats_lock);
6468*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
6469*4882a593Smuzhiyun 
6470*4882a593Smuzhiyun 	mvpp2_port_copy_mac_addr(dev, priv, port_fwnode, &mac_from);
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun 	port->tx_ring_size = MVPP2_MAX_TXD_DFLT;
6473*4882a593Smuzhiyun 	port->rx_ring_size = MVPP2_MAX_RXD_DFLT;
6474*4882a593Smuzhiyun 	SET_NETDEV_DEV(dev, &pdev->dev);
6475*4882a593Smuzhiyun 
6476*4882a593Smuzhiyun 	err = mvpp2_port_init(port);
6477*4882a593Smuzhiyun 	if (err < 0) {
6478*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to init port %d\n", id);
6479*4882a593Smuzhiyun 		goto err_free_stats;
6480*4882a593Smuzhiyun 	}
6481*4882a593Smuzhiyun 
6482*4882a593Smuzhiyun 	mvpp2_port_periodic_xon_disable(port);
6483*4882a593Smuzhiyun 
6484*4882a593Smuzhiyun 	mvpp2_mac_reset_assert(port);
6485*4882a593Smuzhiyun 	mvpp22_pcs_reset_assert(port);
6486*4882a593Smuzhiyun 
6487*4882a593Smuzhiyun 	port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
6488*4882a593Smuzhiyun 	if (!port->pcpu) {
6489*4882a593Smuzhiyun 		err = -ENOMEM;
6490*4882a593Smuzhiyun 		goto err_free_txq_pcpu;
6491*4882a593Smuzhiyun 	}
6492*4882a593Smuzhiyun 
6493*4882a593Smuzhiyun 	if (!port->has_tx_irqs) {
6494*4882a593Smuzhiyun 		for (thread = 0; thread < priv->nthreads; thread++) {
6495*4882a593Smuzhiyun 			port_pcpu = per_cpu_ptr(port->pcpu, thread);
6496*4882a593Smuzhiyun 
6497*4882a593Smuzhiyun 			hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
6498*4882a593Smuzhiyun 				     HRTIMER_MODE_REL_PINNED_SOFT);
6499*4882a593Smuzhiyun 			port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
6500*4882a593Smuzhiyun 			port_pcpu->timer_scheduled = false;
6501*4882a593Smuzhiyun 			port_pcpu->dev = dev;
6502*4882a593Smuzhiyun 		}
6503*4882a593Smuzhiyun 	}
6504*4882a593Smuzhiyun 
6505*4882a593Smuzhiyun 	features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6506*4882a593Smuzhiyun 		   NETIF_F_TSO;
6507*4882a593Smuzhiyun 	dev->features = features | NETIF_F_RXCSUM;
6508*4882a593Smuzhiyun 	dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO |
6509*4882a593Smuzhiyun 			    NETIF_F_HW_VLAN_CTAG_FILTER;
6510*4882a593Smuzhiyun 
6511*4882a593Smuzhiyun 	if (mvpp22_rss_is_supported()) {
6512*4882a593Smuzhiyun 		dev->hw_features |= NETIF_F_RXHASH;
6513*4882a593Smuzhiyun 		dev->features |= NETIF_F_NTUPLE;
6514*4882a593Smuzhiyun 	}
6515*4882a593Smuzhiyun 
6516*4882a593Smuzhiyun 	if (!port->priv->percpu_pools)
6517*4882a593Smuzhiyun 		mvpp2_set_hw_csum(port, port->pool_long->id);
6518*4882a593Smuzhiyun 
6519*4882a593Smuzhiyun 	dev->vlan_features |= features;
6520*4882a593Smuzhiyun 	dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
6521*4882a593Smuzhiyun 	dev->priv_flags |= IFF_UNICAST_FLT;
6522*4882a593Smuzhiyun 
6523*4882a593Smuzhiyun 	/* MTU range: 68 - 9704 */
6524*4882a593Smuzhiyun 	dev->min_mtu = ETH_MIN_MTU;
6525*4882a593Smuzhiyun 	/* 9704 == 9728 - 20 and rounding to 8 */
6526*4882a593Smuzhiyun 	dev->max_mtu = MVPP2_BM_JUMBO_PKT_SIZE;
6527*4882a593Smuzhiyun 	dev->dev.of_node = port_node;
6528*4882a593Smuzhiyun 
6529*4882a593Smuzhiyun 	/* Phylink isn't used w/ ACPI as of now */
6530*4882a593Smuzhiyun 	if (port_node) {
6531*4882a593Smuzhiyun 		port->phylink_config.dev = &dev->dev;
6532*4882a593Smuzhiyun 		port->phylink_config.type = PHYLINK_NETDEV;
6533*4882a593Smuzhiyun 
6534*4882a593Smuzhiyun 		phylink = phylink_create(&port->phylink_config, port_fwnode,
6535*4882a593Smuzhiyun 					 phy_mode, &mvpp2_phylink_ops);
6536*4882a593Smuzhiyun 		if (IS_ERR(phylink)) {
6537*4882a593Smuzhiyun 			err = PTR_ERR(phylink);
6538*4882a593Smuzhiyun 			goto err_free_port_pcpu;
6539*4882a593Smuzhiyun 		}
6540*4882a593Smuzhiyun 		port->phylink = phylink;
6541*4882a593Smuzhiyun 	} else {
6542*4882a593Smuzhiyun 		port->phylink = NULL;
6543*4882a593Smuzhiyun 	}
6544*4882a593Smuzhiyun 
6545*4882a593Smuzhiyun 	/* Cycle the comphy to power it down, saving 270mW per port -
6546*4882a593Smuzhiyun 	 * don't worry about an error powering it up. When the comphy
6547*4882a593Smuzhiyun 	 * driver does this, we can remove this code.
6548*4882a593Smuzhiyun 	 */
6549*4882a593Smuzhiyun 	if (port->comphy) {
6550*4882a593Smuzhiyun 		err = mvpp22_comphy_init(port);
6551*4882a593Smuzhiyun 		if (err == 0)
6552*4882a593Smuzhiyun 			phy_power_off(port->comphy);
6553*4882a593Smuzhiyun 	}
6554*4882a593Smuzhiyun 
6555*4882a593Smuzhiyun 	err = register_netdev(dev);
6556*4882a593Smuzhiyun 	if (err < 0) {
6557*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to register netdev\n");
6558*4882a593Smuzhiyun 		goto err_phylink;
6559*4882a593Smuzhiyun 	}
6560*4882a593Smuzhiyun 	netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
6561*4882a593Smuzhiyun 
6562*4882a593Smuzhiyun 	priv->port_list[priv->port_count++] = port;
6563*4882a593Smuzhiyun 
6564*4882a593Smuzhiyun 	return 0;
6565*4882a593Smuzhiyun 
6566*4882a593Smuzhiyun err_phylink:
6567*4882a593Smuzhiyun 	if (port->phylink)
6568*4882a593Smuzhiyun 		phylink_destroy(port->phylink);
6569*4882a593Smuzhiyun err_free_port_pcpu:
6570*4882a593Smuzhiyun 	free_percpu(port->pcpu);
6571*4882a593Smuzhiyun err_free_txq_pcpu:
6572*4882a593Smuzhiyun 	for (i = 0; i < port->ntxqs; i++)
6573*4882a593Smuzhiyun 		free_percpu(port->txqs[i]->pcpu);
6574*4882a593Smuzhiyun err_free_stats:
6575*4882a593Smuzhiyun 	free_percpu(port->stats);
6576*4882a593Smuzhiyun err_free_irq:
6577*4882a593Smuzhiyun 	if (port->port_irq)
6578*4882a593Smuzhiyun 		irq_dispose_mapping(port->port_irq);
6579*4882a593Smuzhiyun err_deinit_qvecs:
6580*4882a593Smuzhiyun 	mvpp2_queue_vectors_deinit(port);
6581*4882a593Smuzhiyun err_free_netdev:
6582*4882a593Smuzhiyun 	free_netdev(dev);
6583*4882a593Smuzhiyun 	return err;
6584*4882a593Smuzhiyun }
6585*4882a593Smuzhiyun 
6586*4882a593Smuzhiyun /* Ports removal routine */
mvpp2_port_remove(struct mvpp2_port * port)6587*4882a593Smuzhiyun static void mvpp2_port_remove(struct mvpp2_port *port)
6588*4882a593Smuzhiyun {
6589*4882a593Smuzhiyun 	int i;
6590*4882a593Smuzhiyun 
6591*4882a593Smuzhiyun 	unregister_netdev(port->dev);
6592*4882a593Smuzhiyun 	if (port->phylink)
6593*4882a593Smuzhiyun 		phylink_destroy(port->phylink);
6594*4882a593Smuzhiyun 	free_percpu(port->pcpu);
6595*4882a593Smuzhiyun 	free_percpu(port->stats);
6596*4882a593Smuzhiyun 	for (i = 0; i < port->ntxqs; i++)
6597*4882a593Smuzhiyun 		free_percpu(port->txqs[i]->pcpu);
6598*4882a593Smuzhiyun 	mvpp2_queue_vectors_deinit(port);
6599*4882a593Smuzhiyun 	if (port->port_irq)
6600*4882a593Smuzhiyun 		irq_dispose_mapping(port->port_irq);
6601*4882a593Smuzhiyun 	free_netdev(port->dev);
6602*4882a593Smuzhiyun }
6603*4882a593Smuzhiyun 
6604*4882a593Smuzhiyun /* Initialize decoding windows */
mvpp2_conf_mbus_windows(const struct mbus_dram_target_info * dram,struct mvpp2 * priv)6605*4882a593Smuzhiyun static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
6606*4882a593Smuzhiyun 				    struct mvpp2 *priv)
6607*4882a593Smuzhiyun {
6608*4882a593Smuzhiyun 	u32 win_enable;
6609*4882a593Smuzhiyun 	int i;
6610*4882a593Smuzhiyun 
6611*4882a593Smuzhiyun 	for (i = 0; i < 6; i++) {
6612*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
6613*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
6614*4882a593Smuzhiyun 
6615*4882a593Smuzhiyun 		if (i < 4)
6616*4882a593Smuzhiyun 			mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
6617*4882a593Smuzhiyun 	}
6618*4882a593Smuzhiyun 
6619*4882a593Smuzhiyun 	win_enable = 0;
6620*4882a593Smuzhiyun 
6621*4882a593Smuzhiyun 	for (i = 0; i < dram->num_cs; i++) {
6622*4882a593Smuzhiyun 		const struct mbus_dram_window *cs = dram->cs + i;
6623*4882a593Smuzhiyun 
6624*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_WIN_BASE(i),
6625*4882a593Smuzhiyun 			    (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
6626*4882a593Smuzhiyun 			    dram->mbus_dram_target_id);
6627*4882a593Smuzhiyun 
6628*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_WIN_SIZE(i),
6629*4882a593Smuzhiyun 			    (cs->size - 1) & 0xffff0000);
6630*4882a593Smuzhiyun 
6631*4882a593Smuzhiyun 		win_enable |= (1 << i);
6632*4882a593Smuzhiyun 	}
6633*4882a593Smuzhiyun 
6634*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
6635*4882a593Smuzhiyun }
6636*4882a593Smuzhiyun 
6637*4882a593Smuzhiyun /* Initialize Rx FIFO's */
mvpp2_rx_fifo_init(struct mvpp2 * priv)6638*4882a593Smuzhiyun static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
6639*4882a593Smuzhiyun {
6640*4882a593Smuzhiyun 	int port;
6641*4882a593Smuzhiyun 
6642*4882a593Smuzhiyun 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6643*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6644*4882a593Smuzhiyun 			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
6645*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6646*4882a593Smuzhiyun 			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
6647*4882a593Smuzhiyun 	}
6648*4882a593Smuzhiyun 
6649*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6650*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
6651*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6652*4882a593Smuzhiyun }
6653*4882a593Smuzhiyun 
mvpp22_rx_fifo_init(struct mvpp2 * priv)6654*4882a593Smuzhiyun static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
6655*4882a593Smuzhiyun {
6656*4882a593Smuzhiyun 	int port;
6657*4882a593Smuzhiyun 
6658*4882a593Smuzhiyun 	/* The FIFO size parameters are set depending on the maximum speed a
6659*4882a593Smuzhiyun 	 * given port can handle:
6660*4882a593Smuzhiyun 	 * - Port 0: 10Gbps
6661*4882a593Smuzhiyun 	 * - Port 1: 2.5Gbps
6662*4882a593Smuzhiyun 	 * - Ports 2 and 3: 1Gbps
6663*4882a593Smuzhiyun 	 */
6664*4882a593Smuzhiyun 
6665*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
6666*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
6667*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
6668*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
6669*4882a593Smuzhiyun 
6670*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
6671*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
6672*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
6673*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
6674*4882a593Smuzhiyun 
6675*4882a593Smuzhiyun 	for (port = 2; port < MVPP2_MAX_PORTS; port++) {
6676*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
6677*4882a593Smuzhiyun 			    MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
6678*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
6679*4882a593Smuzhiyun 			    MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
6680*4882a593Smuzhiyun 	}
6681*4882a593Smuzhiyun 
6682*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
6683*4882a593Smuzhiyun 		    MVPP2_RX_FIFO_PORT_MIN_PKT);
6684*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
6685*4882a593Smuzhiyun }
6686*4882a593Smuzhiyun 
6687*4882a593Smuzhiyun /* Initialize Tx FIFO's: the total FIFO size is 19kB on PPv2.2 and 10G
6688*4882a593Smuzhiyun  * interfaces must have a Tx FIFO size of 10kB. As only port 0 can do 10G,
6689*4882a593Smuzhiyun  * configure its Tx FIFO size to 10kB and the others ports Tx FIFO size to 3kB.
6690*4882a593Smuzhiyun  */
mvpp22_tx_fifo_init(struct mvpp2 * priv)6691*4882a593Smuzhiyun static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
6692*4882a593Smuzhiyun {
6693*4882a593Smuzhiyun 	int port, size, thrs;
6694*4882a593Smuzhiyun 
6695*4882a593Smuzhiyun 	for (port = 0; port < MVPP2_MAX_PORTS; port++) {
6696*4882a593Smuzhiyun 		if (port == 0) {
6697*4882a593Smuzhiyun 			size = MVPP22_TX_FIFO_DATA_SIZE_10KB;
6698*4882a593Smuzhiyun 			thrs = MVPP2_TX_FIFO_THRESHOLD_10KB;
6699*4882a593Smuzhiyun 		} else {
6700*4882a593Smuzhiyun 			size = MVPP22_TX_FIFO_DATA_SIZE_3KB;
6701*4882a593Smuzhiyun 			thrs = MVPP2_TX_FIFO_THRESHOLD_3KB;
6702*4882a593Smuzhiyun 		}
6703*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port), size);
6704*4882a593Smuzhiyun 		mvpp2_write(priv, MVPP22_TX_FIFO_THRESH_REG(port), thrs);
6705*4882a593Smuzhiyun 	}
6706*4882a593Smuzhiyun }
6707*4882a593Smuzhiyun 
mvpp2_axi_init(struct mvpp2 * priv)6708*4882a593Smuzhiyun static void mvpp2_axi_init(struct mvpp2 *priv)
6709*4882a593Smuzhiyun {
6710*4882a593Smuzhiyun 	u32 val, rdval, wrval;
6711*4882a593Smuzhiyun 
6712*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
6713*4882a593Smuzhiyun 
6714*4882a593Smuzhiyun 	/* AXI Bridge Configuration */
6715*4882a593Smuzhiyun 
6716*4882a593Smuzhiyun 	rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
6717*4882a593Smuzhiyun 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
6718*4882a593Smuzhiyun 	rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6719*4882a593Smuzhiyun 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
6720*4882a593Smuzhiyun 
6721*4882a593Smuzhiyun 	wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
6722*4882a593Smuzhiyun 		<< MVPP22_AXI_ATTR_CACHE_OFFS;
6723*4882a593Smuzhiyun 	wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6724*4882a593Smuzhiyun 		<< MVPP22_AXI_ATTR_DOMAIN_OFFS;
6725*4882a593Smuzhiyun 
6726*4882a593Smuzhiyun 	/* BM */
6727*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
6728*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
6729*4882a593Smuzhiyun 
6730*4882a593Smuzhiyun 	/* Descriptors */
6731*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
6732*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
6733*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
6734*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
6735*4882a593Smuzhiyun 
6736*4882a593Smuzhiyun 	/* Buffer Data */
6737*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
6738*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
6739*4882a593Smuzhiyun 
6740*4882a593Smuzhiyun 	val = MVPP22_AXI_CODE_CACHE_NON_CACHE
6741*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_CACHE_OFFS;
6742*4882a593Smuzhiyun 	val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
6743*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
6744*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
6745*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
6746*4882a593Smuzhiyun 
6747*4882a593Smuzhiyun 	val = MVPP22_AXI_CODE_CACHE_RD_CACHE
6748*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_CACHE_OFFS;
6749*4882a593Smuzhiyun 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6750*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
6751*4882a593Smuzhiyun 
6752*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
6753*4882a593Smuzhiyun 
6754*4882a593Smuzhiyun 	val = MVPP22_AXI_CODE_CACHE_WR_CACHE
6755*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_CACHE_OFFS;
6756*4882a593Smuzhiyun 	val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
6757*4882a593Smuzhiyun 		<< MVPP22_AXI_CODE_DOMAIN_OFFS;
6758*4882a593Smuzhiyun 
6759*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
6760*4882a593Smuzhiyun }
6761*4882a593Smuzhiyun 
6762*4882a593Smuzhiyun /* Initialize network controller common part HW */
mvpp2_init(struct platform_device * pdev,struct mvpp2 * priv)6763*4882a593Smuzhiyun static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
6764*4882a593Smuzhiyun {
6765*4882a593Smuzhiyun 	const struct mbus_dram_target_info *dram_target_info;
6766*4882a593Smuzhiyun 	int err, i;
6767*4882a593Smuzhiyun 	u32 val;
6768*4882a593Smuzhiyun 
6769*4882a593Smuzhiyun 	/* MBUS windows configuration */
6770*4882a593Smuzhiyun 	dram_target_info = mv_mbus_dram_info();
6771*4882a593Smuzhiyun 	if (dram_target_info)
6772*4882a593Smuzhiyun 		mvpp2_conf_mbus_windows(dram_target_info, priv);
6773*4882a593Smuzhiyun 
6774*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22)
6775*4882a593Smuzhiyun 		mvpp2_axi_init(priv);
6776*4882a593Smuzhiyun 
6777*4882a593Smuzhiyun 	/* Disable HW PHY polling */
6778*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
6779*4882a593Smuzhiyun 		val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6780*4882a593Smuzhiyun 		val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
6781*4882a593Smuzhiyun 		writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
6782*4882a593Smuzhiyun 	} else {
6783*4882a593Smuzhiyun 		val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6784*4882a593Smuzhiyun 		val &= ~MVPP22_SMI_POLLING_EN;
6785*4882a593Smuzhiyun 		writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
6786*4882a593Smuzhiyun 	}
6787*4882a593Smuzhiyun 
6788*4882a593Smuzhiyun 	/* Allocate and initialize aggregated TXQs */
6789*4882a593Smuzhiyun 	priv->aggr_txqs = devm_kcalloc(&pdev->dev, MVPP2_MAX_THREADS,
6790*4882a593Smuzhiyun 				       sizeof(*priv->aggr_txqs),
6791*4882a593Smuzhiyun 				       GFP_KERNEL);
6792*4882a593Smuzhiyun 	if (!priv->aggr_txqs)
6793*4882a593Smuzhiyun 		return -ENOMEM;
6794*4882a593Smuzhiyun 
6795*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6796*4882a593Smuzhiyun 		priv->aggr_txqs[i].id = i;
6797*4882a593Smuzhiyun 		priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
6798*4882a593Smuzhiyun 		err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
6799*4882a593Smuzhiyun 		if (err < 0)
6800*4882a593Smuzhiyun 			return err;
6801*4882a593Smuzhiyun 	}
6802*4882a593Smuzhiyun 
6803*4882a593Smuzhiyun 	/* Fifo Init */
6804*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
6805*4882a593Smuzhiyun 		mvpp2_rx_fifo_init(priv);
6806*4882a593Smuzhiyun 	} else {
6807*4882a593Smuzhiyun 		mvpp22_rx_fifo_init(priv);
6808*4882a593Smuzhiyun 		mvpp22_tx_fifo_init(priv);
6809*4882a593Smuzhiyun 	}
6810*4882a593Smuzhiyun 
6811*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
6812*4882a593Smuzhiyun 		writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
6813*4882a593Smuzhiyun 		       priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
6814*4882a593Smuzhiyun 
6815*4882a593Smuzhiyun 	/* Allow cache snoop when transmiting packets */
6816*4882a593Smuzhiyun 	mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
6817*4882a593Smuzhiyun 
6818*4882a593Smuzhiyun 	/* Buffer Manager initialization */
6819*4882a593Smuzhiyun 	err = mvpp2_bm_init(&pdev->dev, priv);
6820*4882a593Smuzhiyun 	if (err < 0)
6821*4882a593Smuzhiyun 		return err;
6822*4882a593Smuzhiyun 
6823*4882a593Smuzhiyun 	/* Parser default initialization */
6824*4882a593Smuzhiyun 	err = mvpp2_prs_default_init(pdev, priv);
6825*4882a593Smuzhiyun 	if (err < 0)
6826*4882a593Smuzhiyun 		return err;
6827*4882a593Smuzhiyun 
6828*4882a593Smuzhiyun 	/* Classifier default initialization */
6829*4882a593Smuzhiyun 	mvpp2_cls_init(priv);
6830*4882a593Smuzhiyun 
6831*4882a593Smuzhiyun 	return 0;
6832*4882a593Smuzhiyun }
6833*4882a593Smuzhiyun 
mvpp2_probe(struct platform_device * pdev)6834*4882a593Smuzhiyun static int mvpp2_probe(struct platform_device *pdev)
6835*4882a593Smuzhiyun {
6836*4882a593Smuzhiyun 	const struct acpi_device_id *acpi_id;
6837*4882a593Smuzhiyun 	struct fwnode_handle *fwnode = pdev->dev.fwnode;
6838*4882a593Smuzhiyun 	struct fwnode_handle *port_fwnode;
6839*4882a593Smuzhiyun 	struct mvpp2 *priv;
6840*4882a593Smuzhiyun 	struct resource *res;
6841*4882a593Smuzhiyun 	void __iomem *base;
6842*4882a593Smuzhiyun 	int i, shared;
6843*4882a593Smuzhiyun 	int err;
6844*4882a593Smuzhiyun 
6845*4882a593Smuzhiyun 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
6846*4882a593Smuzhiyun 	if (!priv)
6847*4882a593Smuzhiyun 		return -ENOMEM;
6848*4882a593Smuzhiyun 
6849*4882a593Smuzhiyun 	if (has_acpi_companion(&pdev->dev)) {
6850*4882a593Smuzhiyun 		acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
6851*4882a593Smuzhiyun 					    &pdev->dev);
6852*4882a593Smuzhiyun 		if (!acpi_id)
6853*4882a593Smuzhiyun 			return -EINVAL;
6854*4882a593Smuzhiyun 		priv->hw_version = (unsigned long)acpi_id->driver_data;
6855*4882a593Smuzhiyun 	} else {
6856*4882a593Smuzhiyun 		priv->hw_version =
6857*4882a593Smuzhiyun 			(unsigned long)of_device_get_match_data(&pdev->dev);
6858*4882a593Smuzhiyun 	}
6859*4882a593Smuzhiyun 
6860*4882a593Smuzhiyun 	/* multi queue mode isn't supported on PPV2.1, fallback to single
6861*4882a593Smuzhiyun 	 * mode
6862*4882a593Smuzhiyun 	 */
6863*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
6864*4882a593Smuzhiyun 		queue_mode = MVPP2_QDIST_SINGLE_MODE;
6865*4882a593Smuzhiyun 
6866*4882a593Smuzhiyun 	base = devm_platform_ioremap_resource(pdev, 0);
6867*4882a593Smuzhiyun 	if (IS_ERR(base))
6868*4882a593Smuzhiyun 		return PTR_ERR(base);
6869*4882a593Smuzhiyun 
6870*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21) {
6871*4882a593Smuzhiyun 		priv->lms_base = devm_platform_ioremap_resource(pdev, 1);
6872*4882a593Smuzhiyun 		if (IS_ERR(priv->lms_base))
6873*4882a593Smuzhiyun 			return PTR_ERR(priv->lms_base);
6874*4882a593Smuzhiyun 	} else {
6875*4882a593Smuzhiyun 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
6876*4882a593Smuzhiyun 		if (!res) {
6877*4882a593Smuzhiyun 			dev_err(&pdev->dev, "Invalid resource\n");
6878*4882a593Smuzhiyun 			return -EINVAL;
6879*4882a593Smuzhiyun 		}
6880*4882a593Smuzhiyun 		if (has_acpi_companion(&pdev->dev)) {
6881*4882a593Smuzhiyun 			/* In case the MDIO memory region is declared in
6882*4882a593Smuzhiyun 			 * the ACPI, it can already appear as 'in-use'
6883*4882a593Smuzhiyun 			 * in the OS. Because it is overlapped by second
6884*4882a593Smuzhiyun 			 * region of the network controller, make
6885*4882a593Smuzhiyun 			 * sure it is released, before requesting it again.
6886*4882a593Smuzhiyun 			 * The care is taken by mvpp2 driver to avoid
6887*4882a593Smuzhiyun 			 * concurrent access to this memory region.
6888*4882a593Smuzhiyun 			 */
6889*4882a593Smuzhiyun 			release_resource(res);
6890*4882a593Smuzhiyun 		}
6891*4882a593Smuzhiyun 		priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
6892*4882a593Smuzhiyun 		if (IS_ERR(priv->iface_base))
6893*4882a593Smuzhiyun 			return PTR_ERR(priv->iface_base);
6894*4882a593Smuzhiyun 	}
6895*4882a593Smuzhiyun 
6896*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22 && dev_of_node(&pdev->dev)) {
6897*4882a593Smuzhiyun 		priv->sysctrl_base =
6898*4882a593Smuzhiyun 			syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
6899*4882a593Smuzhiyun 							"marvell,system-controller");
6900*4882a593Smuzhiyun 		if (IS_ERR(priv->sysctrl_base))
6901*4882a593Smuzhiyun 			/* The system controller regmap is optional for dt
6902*4882a593Smuzhiyun 			 * compatibility reasons. When not provided, the
6903*4882a593Smuzhiyun 			 * configuration of the GoP relies on the
6904*4882a593Smuzhiyun 			 * firmware/bootloader.
6905*4882a593Smuzhiyun 			 */
6906*4882a593Smuzhiyun 			priv->sysctrl_base = NULL;
6907*4882a593Smuzhiyun 	}
6908*4882a593Smuzhiyun 
6909*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22 &&
6910*4882a593Smuzhiyun 	    mvpp2_get_nrxqs(priv) * 2 <= MVPP2_BM_MAX_POOLS)
6911*4882a593Smuzhiyun 		priv->percpu_pools = 1;
6912*4882a593Smuzhiyun 
6913*4882a593Smuzhiyun 	mvpp2_setup_bm_pool();
6914*4882a593Smuzhiyun 
6915*4882a593Smuzhiyun 
6916*4882a593Smuzhiyun 	priv->nthreads = min_t(unsigned int, num_present_cpus(),
6917*4882a593Smuzhiyun 			       MVPP2_MAX_THREADS);
6918*4882a593Smuzhiyun 
6919*4882a593Smuzhiyun 	shared = num_present_cpus() - priv->nthreads;
6920*4882a593Smuzhiyun 	if (shared > 0)
6921*4882a593Smuzhiyun 		bitmap_set(&priv->lock_map, 0,
6922*4882a593Smuzhiyun 			    min_t(int, shared, MVPP2_MAX_THREADS));
6923*4882a593Smuzhiyun 
6924*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
6925*4882a593Smuzhiyun 		u32 addr_space_sz;
6926*4882a593Smuzhiyun 
6927*4882a593Smuzhiyun 		addr_space_sz = (priv->hw_version == MVPP21 ?
6928*4882a593Smuzhiyun 				 MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
6929*4882a593Smuzhiyun 		priv->swth_base[i] = base + i * addr_space_sz;
6930*4882a593Smuzhiyun 	}
6931*4882a593Smuzhiyun 
6932*4882a593Smuzhiyun 	if (priv->hw_version == MVPP21)
6933*4882a593Smuzhiyun 		priv->max_port_rxqs = 8;
6934*4882a593Smuzhiyun 	else
6935*4882a593Smuzhiyun 		priv->max_port_rxqs = 32;
6936*4882a593Smuzhiyun 
6937*4882a593Smuzhiyun 	if (dev_of_node(&pdev->dev)) {
6938*4882a593Smuzhiyun 		priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
6939*4882a593Smuzhiyun 		if (IS_ERR(priv->pp_clk))
6940*4882a593Smuzhiyun 			return PTR_ERR(priv->pp_clk);
6941*4882a593Smuzhiyun 		err = clk_prepare_enable(priv->pp_clk);
6942*4882a593Smuzhiyun 		if (err < 0)
6943*4882a593Smuzhiyun 			return err;
6944*4882a593Smuzhiyun 
6945*4882a593Smuzhiyun 		priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
6946*4882a593Smuzhiyun 		if (IS_ERR(priv->gop_clk)) {
6947*4882a593Smuzhiyun 			err = PTR_ERR(priv->gop_clk);
6948*4882a593Smuzhiyun 			goto err_pp_clk;
6949*4882a593Smuzhiyun 		}
6950*4882a593Smuzhiyun 		err = clk_prepare_enable(priv->gop_clk);
6951*4882a593Smuzhiyun 		if (err < 0)
6952*4882a593Smuzhiyun 			goto err_pp_clk;
6953*4882a593Smuzhiyun 
6954*4882a593Smuzhiyun 		if (priv->hw_version == MVPP22) {
6955*4882a593Smuzhiyun 			priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
6956*4882a593Smuzhiyun 			if (IS_ERR(priv->mg_clk)) {
6957*4882a593Smuzhiyun 				err = PTR_ERR(priv->mg_clk);
6958*4882a593Smuzhiyun 				goto err_gop_clk;
6959*4882a593Smuzhiyun 			}
6960*4882a593Smuzhiyun 
6961*4882a593Smuzhiyun 			err = clk_prepare_enable(priv->mg_clk);
6962*4882a593Smuzhiyun 			if (err < 0)
6963*4882a593Smuzhiyun 				goto err_gop_clk;
6964*4882a593Smuzhiyun 
6965*4882a593Smuzhiyun 			priv->mg_core_clk = devm_clk_get(&pdev->dev, "mg_core_clk");
6966*4882a593Smuzhiyun 			if (IS_ERR(priv->mg_core_clk)) {
6967*4882a593Smuzhiyun 				priv->mg_core_clk = NULL;
6968*4882a593Smuzhiyun 			} else {
6969*4882a593Smuzhiyun 				err = clk_prepare_enable(priv->mg_core_clk);
6970*4882a593Smuzhiyun 				if (err < 0)
6971*4882a593Smuzhiyun 					goto err_mg_clk;
6972*4882a593Smuzhiyun 			}
6973*4882a593Smuzhiyun 		}
6974*4882a593Smuzhiyun 
6975*4882a593Smuzhiyun 		priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
6976*4882a593Smuzhiyun 		if (IS_ERR(priv->axi_clk)) {
6977*4882a593Smuzhiyun 			err = PTR_ERR(priv->axi_clk);
6978*4882a593Smuzhiyun 			if (err == -EPROBE_DEFER)
6979*4882a593Smuzhiyun 				goto err_mg_core_clk;
6980*4882a593Smuzhiyun 			priv->axi_clk = NULL;
6981*4882a593Smuzhiyun 		} else {
6982*4882a593Smuzhiyun 			err = clk_prepare_enable(priv->axi_clk);
6983*4882a593Smuzhiyun 			if (err < 0)
6984*4882a593Smuzhiyun 				goto err_mg_core_clk;
6985*4882a593Smuzhiyun 		}
6986*4882a593Smuzhiyun 
6987*4882a593Smuzhiyun 		/* Get system's tclk rate */
6988*4882a593Smuzhiyun 		priv->tclk = clk_get_rate(priv->pp_clk);
6989*4882a593Smuzhiyun 	} else if (device_property_read_u32(&pdev->dev, "clock-frequency",
6990*4882a593Smuzhiyun 					    &priv->tclk)) {
6991*4882a593Smuzhiyun 		dev_err(&pdev->dev, "missing clock-frequency value\n");
6992*4882a593Smuzhiyun 		return -EINVAL;
6993*4882a593Smuzhiyun 	}
6994*4882a593Smuzhiyun 
6995*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22) {
6996*4882a593Smuzhiyun 		err = dma_set_mask(&pdev->dev, MVPP2_DESC_DMA_MASK);
6997*4882a593Smuzhiyun 		if (err)
6998*4882a593Smuzhiyun 			goto err_axi_clk;
6999*4882a593Smuzhiyun 		/* Sadly, the BM pools all share the same register to
7000*4882a593Smuzhiyun 		 * store the high 32 bits of their address. So they
7001*4882a593Smuzhiyun 		 * must all have the same high 32 bits, which forces
7002*4882a593Smuzhiyun 		 * us to restrict coherent memory to DMA_BIT_MASK(32).
7003*4882a593Smuzhiyun 		 */
7004*4882a593Smuzhiyun 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7005*4882a593Smuzhiyun 		if (err)
7006*4882a593Smuzhiyun 			goto err_axi_clk;
7007*4882a593Smuzhiyun 	}
7008*4882a593Smuzhiyun 
7009*4882a593Smuzhiyun 	/* Initialize network controller */
7010*4882a593Smuzhiyun 	err = mvpp2_init(pdev, priv);
7011*4882a593Smuzhiyun 	if (err < 0) {
7012*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to initialize controller\n");
7013*4882a593Smuzhiyun 		goto err_axi_clk;
7014*4882a593Smuzhiyun 	}
7015*4882a593Smuzhiyun 
7016*4882a593Smuzhiyun 	err = mvpp22_tai_probe(&pdev->dev, priv);
7017*4882a593Smuzhiyun 	if (err < 0)
7018*4882a593Smuzhiyun 		goto err_axi_clk;
7019*4882a593Smuzhiyun 
7020*4882a593Smuzhiyun 	/* Initialize ports */
7021*4882a593Smuzhiyun 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7022*4882a593Smuzhiyun 		err = mvpp2_port_probe(pdev, port_fwnode, priv);
7023*4882a593Smuzhiyun 		if (err < 0)
7024*4882a593Smuzhiyun 			goto err_port_probe;
7025*4882a593Smuzhiyun 	}
7026*4882a593Smuzhiyun 
7027*4882a593Smuzhiyun 	if (priv->port_count == 0) {
7028*4882a593Smuzhiyun 		dev_err(&pdev->dev, "no ports enabled\n");
7029*4882a593Smuzhiyun 		err = -ENODEV;
7030*4882a593Smuzhiyun 		goto err_axi_clk;
7031*4882a593Smuzhiyun 	}
7032*4882a593Smuzhiyun 
7033*4882a593Smuzhiyun 	/* Statistics must be gathered regularly because some of them (like
7034*4882a593Smuzhiyun 	 * packets counters) are 32-bit registers and could overflow quite
7035*4882a593Smuzhiyun 	 * quickly. For instance, a 10Gb link used at full bandwidth with the
7036*4882a593Smuzhiyun 	 * smallest packets (64B) will overflow a 32-bit counter in less than
7037*4882a593Smuzhiyun 	 * 30 seconds. Then, use a workqueue to fill 64-bit counters.
7038*4882a593Smuzhiyun 	 */
7039*4882a593Smuzhiyun 	snprintf(priv->queue_name, sizeof(priv->queue_name),
7040*4882a593Smuzhiyun 		 "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
7041*4882a593Smuzhiyun 		 priv->port_count > 1 ? "+" : "");
7042*4882a593Smuzhiyun 	priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
7043*4882a593Smuzhiyun 	if (!priv->stats_queue) {
7044*4882a593Smuzhiyun 		err = -ENOMEM;
7045*4882a593Smuzhiyun 		goto err_port_probe;
7046*4882a593Smuzhiyun 	}
7047*4882a593Smuzhiyun 
7048*4882a593Smuzhiyun 	mvpp2_dbgfs_init(priv, pdev->name);
7049*4882a593Smuzhiyun 
7050*4882a593Smuzhiyun 	platform_set_drvdata(pdev, priv);
7051*4882a593Smuzhiyun 	return 0;
7052*4882a593Smuzhiyun 
7053*4882a593Smuzhiyun err_port_probe:
7054*4882a593Smuzhiyun 	fwnode_handle_put(port_fwnode);
7055*4882a593Smuzhiyun 
7056*4882a593Smuzhiyun 	i = 0;
7057*4882a593Smuzhiyun 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7058*4882a593Smuzhiyun 		if (priv->port_list[i])
7059*4882a593Smuzhiyun 			mvpp2_port_remove(priv->port_list[i]);
7060*4882a593Smuzhiyun 		i++;
7061*4882a593Smuzhiyun 	}
7062*4882a593Smuzhiyun err_axi_clk:
7063*4882a593Smuzhiyun 	clk_disable_unprepare(priv->axi_clk);
7064*4882a593Smuzhiyun 
7065*4882a593Smuzhiyun err_mg_core_clk:
7066*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22)
7067*4882a593Smuzhiyun 		clk_disable_unprepare(priv->mg_core_clk);
7068*4882a593Smuzhiyun err_mg_clk:
7069*4882a593Smuzhiyun 	if (priv->hw_version == MVPP22)
7070*4882a593Smuzhiyun 		clk_disable_unprepare(priv->mg_clk);
7071*4882a593Smuzhiyun err_gop_clk:
7072*4882a593Smuzhiyun 	clk_disable_unprepare(priv->gop_clk);
7073*4882a593Smuzhiyun err_pp_clk:
7074*4882a593Smuzhiyun 	clk_disable_unprepare(priv->pp_clk);
7075*4882a593Smuzhiyun 	return err;
7076*4882a593Smuzhiyun }
7077*4882a593Smuzhiyun 
mvpp2_remove(struct platform_device * pdev)7078*4882a593Smuzhiyun static int mvpp2_remove(struct platform_device *pdev)
7079*4882a593Smuzhiyun {
7080*4882a593Smuzhiyun 	struct mvpp2 *priv = platform_get_drvdata(pdev);
7081*4882a593Smuzhiyun 	struct fwnode_handle *fwnode = pdev->dev.fwnode;
7082*4882a593Smuzhiyun 	int i = 0, poolnum = MVPP2_BM_POOLS_NUM;
7083*4882a593Smuzhiyun 	struct fwnode_handle *port_fwnode;
7084*4882a593Smuzhiyun 
7085*4882a593Smuzhiyun 	mvpp2_dbgfs_cleanup(priv);
7086*4882a593Smuzhiyun 
7087*4882a593Smuzhiyun 	fwnode_for_each_available_child_node(fwnode, port_fwnode) {
7088*4882a593Smuzhiyun 		if (priv->port_list[i]) {
7089*4882a593Smuzhiyun 			mutex_destroy(&priv->port_list[i]->gather_stats_lock);
7090*4882a593Smuzhiyun 			mvpp2_port_remove(priv->port_list[i]);
7091*4882a593Smuzhiyun 		}
7092*4882a593Smuzhiyun 		i++;
7093*4882a593Smuzhiyun 	}
7094*4882a593Smuzhiyun 
7095*4882a593Smuzhiyun 	destroy_workqueue(priv->stats_queue);
7096*4882a593Smuzhiyun 
7097*4882a593Smuzhiyun 	if (priv->percpu_pools)
7098*4882a593Smuzhiyun 		poolnum = mvpp2_get_nrxqs(priv) * 2;
7099*4882a593Smuzhiyun 
7100*4882a593Smuzhiyun 	for (i = 0; i < poolnum; i++) {
7101*4882a593Smuzhiyun 		struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
7102*4882a593Smuzhiyun 
7103*4882a593Smuzhiyun 		mvpp2_bm_pool_destroy(&pdev->dev, priv, bm_pool);
7104*4882a593Smuzhiyun 	}
7105*4882a593Smuzhiyun 
7106*4882a593Smuzhiyun 	for (i = 0; i < MVPP2_MAX_THREADS; i++) {
7107*4882a593Smuzhiyun 		struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
7108*4882a593Smuzhiyun 
7109*4882a593Smuzhiyun 		dma_free_coherent(&pdev->dev,
7110*4882a593Smuzhiyun 				  MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
7111*4882a593Smuzhiyun 				  aggr_txq->descs,
7112*4882a593Smuzhiyun 				  aggr_txq->descs_dma);
7113*4882a593Smuzhiyun 	}
7114*4882a593Smuzhiyun 
7115*4882a593Smuzhiyun 	if (is_acpi_node(port_fwnode))
7116*4882a593Smuzhiyun 		return 0;
7117*4882a593Smuzhiyun 
7118*4882a593Smuzhiyun 	clk_disable_unprepare(priv->axi_clk);
7119*4882a593Smuzhiyun 	clk_disable_unprepare(priv->mg_core_clk);
7120*4882a593Smuzhiyun 	clk_disable_unprepare(priv->mg_clk);
7121*4882a593Smuzhiyun 	clk_disable_unprepare(priv->pp_clk);
7122*4882a593Smuzhiyun 	clk_disable_unprepare(priv->gop_clk);
7123*4882a593Smuzhiyun 
7124*4882a593Smuzhiyun 	return 0;
7125*4882a593Smuzhiyun }
7126*4882a593Smuzhiyun 
7127*4882a593Smuzhiyun static const struct of_device_id mvpp2_match[] = {
7128*4882a593Smuzhiyun 	{
7129*4882a593Smuzhiyun 		.compatible = "marvell,armada-375-pp2",
7130*4882a593Smuzhiyun 		.data = (void *)MVPP21,
7131*4882a593Smuzhiyun 	},
7132*4882a593Smuzhiyun 	{
7133*4882a593Smuzhiyun 		.compatible = "marvell,armada-7k-pp22",
7134*4882a593Smuzhiyun 		.data = (void *)MVPP22,
7135*4882a593Smuzhiyun 	},
7136*4882a593Smuzhiyun 	{ }
7137*4882a593Smuzhiyun };
7138*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mvpp2_match);
7139*4882a593Smuzhiyun 
7140*4882a593Smuzhiyun #ifdef CONFIG_ACPI
7141*4882a593Smuzhiyun static const struct acpi_device_id mvpp2_acpi_match[] = {
7142*4882a593Smuzhiyun 	{ "MRVL0110", MVPP22 },
7143*4882a593Smuzhiyun 	{ },
7144*4882a593Smuzhiyun };
7145*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, mvpp2_acpi_match);
7146*4882a593Smuzhiyun #endif
7147*4882a593Smuzhiyun 
7148*4882a593Smuzhiyun static struct platform_driver mvpp2_driver = {
7149*4882a593Smuzhiyun 	.probe = mvpp2_probe,
7150*4882a593Smuzhiyun 	.remove = mvpp2_remove,
7151*4882a593Smuzhiyun 	.driver = {
7152*4882a593Smuzhiyun 		.name = MVPP2_DRIVER_NAME,
7153*4882a593Smuzhiyun 		.of_match_table = mvpp2_match,
7154*4882a593Smuzhiyun 		.acpi_match_table = ACPI_PTR(mvpp2_acpi_match),
7155*4882a593Smuzhiyun 	},
7156*4882a593Smuzhiyun };
7157*4882a593Smuzhiyun 
mvpp2_driver_init(void)7158*4882a593Smuzhiyun static int __init mvpp2_driver_init(void)
7159*4882a593Smuzhiyun {
7160*4882a593Smuzhiyun 	return platform_driver_register(&mvpp2_driver);
7161*4882a593Smuzhiyun }
7162*4882a593Smuzhiyun module_init(mvpp2_driver_init);
7163*4882a593Smuzhiyun 
mvpp2_driver_exit(void)7164*4882a593Smuzhiyun static void __exit mvpp2_driver_exit(void)
7165*4882a593Smuzhiyun {
7166*4882a593Smuzhiyun 	platform_driver_unregister(&mvpp2_driver);
7167*4882a593Smuzhiyun 	mvpp2_dbgfs_exit();
7168*4882a593Smuzhiyun }
7169*4882a593Smuzhiyun module_exit(mvpp2_driver_exit);
7170*4882a593Smuzhiyun 
7171*4882a593Smuzhiyun MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
7172*4882a593Smuzhiyun MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
7173*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
7174